2 * Linux V4L2 radio driver for the Griffin radioSHARK2 USB radio receiver
4 * Note the radioSHARK2 offers the audio through a regular USB audio device,
5 * this driver only handles the tuning.
7 * The info necessary to drive the shark2 was taken from the small userspace
8 * shark2.c program by Hisaaki Shibata, which he kindly placed in the Public
11 * Copyright (c) 2012 Hans de Goede <hdegoede@redhat.com>
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
24 #include <linux/init.h>
25 #include <linux/kernel.h>
26 #include <linux/leds.h>
27 #include <linux/module.h>
28 #include <linux/slab.h>
29 #include <linux/usb.h>
30 #include <linux/workqueue.h>
31 #include <media/v4l2-device.h>
32 #include "radio-tea5777.h"
34 #if defined(CONFIG_LEDS_CLASS) || \
35 (defined(CONFIG_LEDS_CLASS_MODULE) && defined(CONFIG_RADIO_SHARK2_MODULE))
36 #define SHARK_USE_LEDS 1
39 MODULE_AUTHOR("Hans de Goede <hdegoede@redhat.com>");
40 MODULE_DESCRIPTION("Griffin radioSHARK2, USB radio receiver driver");
41 MODULE_LICENSE("GPL");
44 module_param(debug
, int, 0);
45 MODULE_PARM_DESC(debug
, "Debug level (0-1)");
47 #define SHARK_IN_EP 0x83
48 #define SHARK_OUT_EP 0x05
51 #define DRV_NAME "radioshark2"
53 #define v4l2_dev_to_shark(d) container_of(d, struct shark_device, v4l2_dev)
55 enum { BLUE_LED
, RED_LED
, NO_LEDS
};
58 struct usb_device
*usbdev
;
59 struct v4l2_device v4l2_dev
;
60 struct radio_tea5777 tea
;
63 struct work_struct led_work
;
64 struct led_classdev leds
[NO_LEDS
];
65 char led_names
[NO_LEDS
][32];
66 atomic_t brightness
[NO_LEDS
];
67 unsigned long brightness_new
;
73 static atomic_t shark_instance
= ATOMIC_INIT(0);
75 static int shark_write_reg(struct radio_tea5777
*tea
, u64 reg
)
77 struct shark_device
*shark
= tea
->private_data
;
78 int i
, res
, actual_len
;
80 memset(shark
->transfer_buffer
, 0, TB_LEN
);
81 shark
->transfer_buffer
[0] = 0x81; /* Write register command */
82 for (i
= 0; i
< 6; i
++)
83 shark
->transfer_buffer
[i
+ 1] = (reg
>> (40 - i
* 8)) & 0xff;
85 v4l2_dbg(1, debug
, tea
->v4l2_dev
, "shark2-write: %*ph\n",
86 7, shark
->transfer_buffer
);
88 res
= usb_interrupt_msg(shark
->usbdev
,
89 usb_sndintpipe(shark
->usbdev
, SHARK_OUT_EP
),
90 shark
->transfer_buffer
, TB_LEN
,
93 v4l2_err(tea
->v4l2_dev
, "write error: %d\n", res
);
100 static int shark_read_reg(struct radio_tea5777
*tea
, u32
*reg_ret
)
102 struct shark_device
*shark
= tea
->private_data
;
103 int i
, res
, actual_len
;
106 memset(shark
->transfer_buffer
, 0, TB_LEN
);
107 shark
->transfer_buffer
[0] = 0x82;
108 res
= usb_interrupt_msg(shark
->usbdev
,
109 usb_sndintpipe(shark
->usbdev
, SHARK_OUT_EP
),
110 shark
->transfer_buffer
, TB_LEN
,
113 v4l2_err(tea
->v4l2_dev
, "request-read error: %d\n", res
);
117 res
= usb_interrupt_msg(shark
->usbdev
,
118 usb_rcvintpipe(shark
->usbdev
, SHARK_IN_EP
),
119 shark
->transfer_buffer
, TB_LEN
,
122 v4l2_err(tea
->v4l2_dev
, "read error: %d\n", res
);
126 for (i
= 0; i
< 3; i
++)
127 reg
|= shark
->transfer_buffer
[i
] << (16 - i
* 8);
129 v4l2_dbg(1, debug
, tea
->v4l2_dev
, "shark2-read: %*ph\n",
130 3, shark
->transfer_buffer
);
136 static const struct radio_tea5777_ops shark_tea_ops
= {
137 .write_reg
= shark_write_reg
,
138 .read_reg
= shark_read_reg
,
141 #ifdef SHARK_USE_LEDS
142 static void shark_led_work(struct work_struct
*work
)
144 struct shark_device
*shark
=
145 container_of(work
, struct shark_device
, led_work
);
146 int i
, res
, brightness
, actual_len
;
148 for (i
= 0; i
< 2; i
++) {
149 if (!test_and_clear_bit(i
, &shark
->brightness_new
))
152 brightness
= atomic_read(&shark
->brightness
[i
]);
153 memset(shark
->transfer_buffer
, 0, TB_LEN
);
154 shark
->transfer_buffer
[0] = 0x83 + i
;
155 shark
->transfer_buffer
[1] = brightness
;
156 res
= usb_interrupt_msg(shark
->usbdev
,
157 usb_sndintpipe(shark
->usbdev
,
159 shark
->transfer_buffer
, TB_LEN
,
162 v4l2_err(&shark
->v4l2_dev
, "set LED %s error: %d\n",
163 shark
->led_names
[i
], res
);
167 static void shark_led_set_blue(struct led_classdev
*led_cdev
,
168 enum led_brightness value
)
170 struct shark_device
*shark
=
171 container_of(led_cdev
, struct shark_device
, leds
[BLUE_LED
]);
173 atomic_set(&shark
->brightness
[BLUE_LED
], value
);
174 set_bit(BLUE_LED
, &shark
->brightness_new
);
175 schedule_work(&shark
->led_work
);
178 static void shark_led_set_red(struct led_classdev
*led_cdev
,
179 enum led_brightness value
)
181 struct shark_device
*shark
=
182 container_of(led_cdev
, struct shark_device
, leds
[RED_LED
]);
184 atomic_set(&shark
->brightness
[RED_LED
], value
);
185 set_bit(RED_LED
, &shark
->brightness_new
);
186 schedule_work(&shark
->led_work
);
189 static const struct led_classdev shark_led_templates
[NO_LEDS
] = {
192 .brightness
= LED_OFF
,
193 .max_brightness
= 127,
194 .brightness_set
= shark_led_set_blue
,
198 .brightness
= LED_OFF
,
200 .brightness_set
= shark_led_set_red
,
204 static int shark_register_leds(struct shark_device
*shark
, struct device
*dev
)
208 atomic_set(&shark
->brightness
[BLUE_LED
], 127);
209 INIT_WORK(&shark
->led_work
, shark_led_work
);
210 for (i
= 0; i
< NO_LEDS
; i
++) {
211 shark
->leds
[i
] = shark_led_templates
[i
];
212 snprintf(shark
->led_names
[i
], sizeof(shark
->led_names
[0]),
213 shark
->leds
[i
].name
, shark
->v4l2_dev
.name
);
214 shark
->leds
[i
].name
= shark
->led_names
[i
];
215 retval
= led_classdev_register(dev
, &shark
->leds
[i
]);
217 v4l2_err(&shark
->v4l2_dev
,
218 "couldn't register led: %s\n",
219 shark
->led_names
[i
]);
226 static void shark_unregister_leds(struct shark_device
*shark
)
230 for (i
= 0; i
< NO_LEDS
; i
++)
231 led_classdev_unregister(&shark
->leds
[i
]);
233 cancel_work_sync(&shark
->led_work
);
236 static inline void shark_resume_leds(struct shark_device
*shark
)
240 for (i
= 0; i
< NO_LEDS
; i
++)
241 set_bit(i
, &shark
->brightness_new
);
243 schedule_work(&shark
->led_work
);
246 static int shark_register_leds(struct shark_device
*shark
, struct device
*dev
)
248 v4l2_warn(&shark
->v4l2_dev
,
249 "CONFIG_LEDS_CLASS not enabled, LED support disabled\n");
252 static inline void shark_unregister_leds(struct shark_device
*shark
) { }
253 static inline void shark_resume_leds(struct shark_device
*shark
) { }
256 static void usb_shark_disconnect(struct usb_interface
*intf
)
258 struct v4l2_device
*v4l2_dev
= usb_get_intfdata(intf
);
259 struct shark_device
*shark
= v4l2_dev_to_shark(v4l2_dev
);
261 mutex_lock(&shark
->tea
.mutex
);
262 v4l2_device_disconnect(&shark
->v4l2_dev
);
263 radio_tea5777_exit(&shark
->tea
);
264 mutex_unlock(&shark
->tea
.mutex
);
266 shark_unregister_leds(shark
);
268 v4l2_device_put(&shark
->v4l2_dev
);
271 static void usb_shark_release(struct v4l2_device
*v4l2_dev
)
273 struct shark_device
*shark
= v4l2_dev_to_shark(v4l2_dev
);
275 v4l2_device_unregister(&shark
->v4l2_dev
);
276 kfree(shark
->transfer_buffer
);
280 static int usb_shark_probe(struct usb_interface
*intf
,
281 const struct usb_device_id
*id
)
283 struct shark_device
*shark
;
284 int retval
= -ENOMEM
;
286 shark
= kzalloc(sizeof(struct shark_device
), GFP_KERNEL
);
290 shark
->transfer_buffer
= kmalloc(TB_LEN
, GFP_KERNEL
);
291 if (!shark
->transfer_buffer
)
292 goto err_alloc_buffer
;
294 v4l2_device_set_name(&shark
->v4l2_dev
, DRV_NAME
, &shark_instance
);
296 retval
= shark_register_leds(shark
, &intf
->dev
);
300 shark
->v4l2_dev
.release
= usb_shark_release
;
301 retval
= v4l2_device_register(&intf
->dev
, &shark
->v4l2_dev
);
303 v4l2_err(&shark
->v4l2_dev
, "couldn't register v4l2_device\n");
307 shark
->usbdev
= interface_to_usbdev(intf
);
308 shark
->tea
.v4l2_dev
= &shark
->v4l2_dev
;
309 shark
->tea
.private_data
= shark
;
310 shark
->tea
.ops
= &shark_tea_ops
;
311 shark
->tea
.has_am
= true;
312 shark
->tea
.write_before_read
= true;
313 strscpy(shark
->tea
.card
, "Griffin radioSHARK2",
314 sizeof(shark
->tea
.card
));
315 usb_make_path(shark
->usbdev
, shark
->tea
.bus_info
,
316 sizeof(shark
->tea
.bus_info
));
318 retval
= radio_tea5777_init(&shark
->tea
, THIS_MODULE
);
320 v4l2_err(&shark
->v4l2_dev
, "couldn't init tea5777\n");
327 v4l2_device_unregister(&shark
->v4l2_dev
);
329 shark_unregister_leds(shark
);
331 kfree(shark
->transfer_buffer
);
339 static int usb_shark_suspend(struct usb_interface
*intf
, pm_message_t message
)
344 static int usb_shark_resume(struct usb_interface
*intf
)
346 struct v4l2_device
*v4l2_dev
= usb_get_intfdata(intf
);
347 struct shark_device
*shark
= v4l2_dev_to_shark(v4l2_dev
);
350 mutex_lock(&shark
->tea
.mutex
);
351 ret
= radio_tea5777_set_freq(&shark
->tea
);
352 mutex_unlock(&shark
->tea
.mutex
);
354 shark_resume_leds(shark
);
360 /* Specify the bcdDevice value, as the radioSHARK and radioSHARK2 share ids */
361 static const struct usb_device_id usb_shark_device_table
[] = {
362 { .match_flags
= USB_DEVICE_ID_MATCH_DEVICE_AND_VERSION
|
363 USB_DEVICE_ID_MATCH_INT_CLASS
,
366 .bcdDevice_lo
= 0x0010,
367 .bcdDevice_hi
= 0x0010,
368 .bInterfaceClass
= 3,
372 MODULE_DEVICE_TABLE(usb
, usb_shark_device_table
);
374 static struct usb_driver usb_shark_driver
= {
376 .probe
= usb_shark_probe
,
377 .disconnect
= usb_shark_disconnect
,
378 .id_table
= usb_shark_device_table
,
380 .suspend
= usb_shark_suspend
,
381 .resume
= usb_shark_resume
,
382 .reset_resume
= usb_shark_resume
,
385 module_usb_driver(usb_shark_driver
);