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.
23 * You should have received a copy of the GNU General Public License
24 * along with this program; if not, write to the Free Software
25 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
28 #include <linux/init.h>
29 #include <linux/kernel.h>
30 #include <linux/leds.h>
31 #include <linux/module.h>
32 #include <linux/slab.h>
33 #include <linux/usb.h>
34 #include <linux/workqueue.h>
35 #include <media/v4l2-device.h>
36 #include "radio-tea5777.h"
38 MODULE_AUTHOR("Hans de Goede <hdegoede@redhat.com>");
39 MODULE_DESCRIPTION("Griffin radioSHARK2, USB radio receiver driver");
40 MODULE_LICENSE("GPL");
43 module_param(debug, int, 0);
44 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 };
57 static void shark_led_set_blue(struct led_classdev *led_cdev,
58 enum led_brightness value);
59 static void shark_led_set_red(struct led_classdev *led_cdev,
60 enum led_brightness value);
62 static const struct led_classdev shark_led_templates[NO_LEDS] = {
65 .brightness = LED_OFF,
66 .max_brightness = 127,
67 .brightness_set = shark_led_set_blue,
71 .brightness = LED_OFF,
73 .brightness_set = shark_led_set_red,
78 struct usb_device *usbdev;
79 struct v4l2_device v4l2_dev;
80 struct radio_tea5777 tea;
82 struct work_struct led_work;
83 struct led_classdev leds[NO_LEDS];
84 char led_names[NO_LEDS][32];
85 atomic_t brightness[NO_LEDS];
86 unsigned long brightness_new;
91 static atomic_t shark_instance = ATOMIC_INIT(0);
93 static int shark_write_reg(struct radio_tea5777 *tea, u64 reg)
95 struct shark_device *shark = tea->private_data;
96 int i, res, actual_len;
98 memset(shark->transfer_buffer, 0, TB_LEN);
99 shark->transfer_buffer[0] = 0x81; /* Write register command */
100 for (i = 0; i < 6; i++)
101 shark->transfer_buffer[i + 1] = (reg >> (40 - i * 8)) & 0xff;
103 v4l2_dbg(1, debug, tea->v4l2_dev,
104 "shark2-write: %02x %02x %02x %02x %02x %02x %02x\n",
105 shark->transfer_buffer[0], shark->transfer_buffer[1],
106 shark->transfer_buffer[2], shark->transfer_buffer[3],
107 shark->transfer_buffer[4], shark->transfer_buffer[5],
108 shark->transfer_buffer[6]);
110 res = usb_interrupt_msg(shark->usbdev,
111 usb_sndintpipe(shark->usbdev, SHARK_OUT_EP),
112 shark->transfer_buffer, TB_LEN,
115 v4l2_err(tea->v4l2_dev, "write error: %d\n", res);
122 static int shark_read_reg(struct radio_tea5777 *tea, u32 *reg_ret)
124 struct shark_device *shark = tea->private_data;
125 int i, res, actual_len;
128 memset(shark->transfer_buffer, 0, TB_LEN);
129 shark->transfer_buffer[0] = 0x82;
130 res = usb_interrupt_msg(shark->usbdev,
131 usb_sndintpipe(shark->usbdev, SHARK_OUT_EP),
132 shark->transfer_buffer, TB_LEN,
135 v4l2_err(tea->v4l2_dev, "request-read error: %d\n", res);
139 res = usb_interrupt_msg(shark->usbdev,
140 usb_rcvintpipe(shark->usbdev, SHARK_IN_EP),
141 shark->transfer_buffer, TB_LEN,
144 v4l2_err(tea->v4l2_dev, "read error: %d\n", res);
148 for (i = 0; i < 3; i++)
149 reg |= shark->transfer_buffer[i] << (16 - i * 8);
151 v4l2_dbg(1, debug, tea->v4l2_dev, "shark2-read: %02x %02x %02x\n",
152 shark->transfer_buffer[0], shark->transfer_buffer[1],
153 shark->transfer_buffer[2]);
159 static struct radio_tea5777_ops shark_tea_ops = {
160 .write_reg = shark_write_reg,
161 .read_reg = shark_read_reg,
164 static void shark_led_work(struct work_struct *work)
166 struct shark_device *shark =
167 container_of(work, struct shark_device, led_work);
168 int i, res, brightness, actual_len;
170 * We use the v4l2_dev lock and registered bit to ensure the device
171 * does not get unplugged and unreffed while we're running.
173 mutex_lock(&shark->tea.mutex);
174 if (!video_is_registered(&shark->tea.vd))
177 for (i = 0; i < 2; i++) {
178 if (!test_and_clear_bit(i, &shark->brightness_new))
181 brightness = atomic_read(&shark->brightness[i]);
182 memset(shark->transfer_buffer, 0, TB_LEN);
183 shark->transfer_buffer[0] = 0x83 + i;
184 shark->transfer_buffer[1] = brightness;
185 res = usb_interrupt_msg(shark->usbdev,
186 usb_sndintpipe(shark->usbdev,
188 shark->transfer_buffer, TB_LEN,
191 v4l2_err(&shark->v4l2_dev, "set LED %s error: %d\n",
192 shark->led_names[i], res);
195 mutex_unlock(&shark->tea.mutex);
198 static void shark_led_set_blue(struct led_classdev *led_cdev,
199 enum led_brightness value)
201 struct shark_device *shark =
202 container_of(led_cdev, struct shark_device, leds[BLUE_LED]);
204 atomic_set(&shark->brightness[BLUE_LED], value);
205 set_bit(BLUE_LED, &shark->brightness_new);
206 schedule_work(&shark->led_work);
209 static void shark_led_set_red(struct led_classdev *led_cdev,
210 enum led_brightness value)
212 struct shark_device *shark =
213 container_of(led_cdev, struct shark_device, leds[RED_LED]);
215 atomic_set(&shark->brightness[RED_LED], value);
216 set_bit(RED_LED, &shark->brightness_new);
217 schedule_work(&shark->led_work);
220 static void usb_shark_disconnect(struct usb_interface *intf)
222 struct v4l2_device *v4l2_dev = usb_get_intfdata(intf);
223 struct shark_device *shark = v4l2_dev_to_shark(v4l2_dev);
226 mutex_lock(&shark->tea.mutex);
227 v4l2_device_disconnect(&shark->v4l2_dev);
228 radio_tea5777_exit(&shark->tea);
229 mutex_unlock(&shark->tea.mutex);
231 for (i = 0; i < NO_LEDS; i++)
232 led_classdev_unregister(&shark->leds[i]);
234 v4l2_device_put(&shark->v4l2_dev);
237 static void usb_shark_release(struct v4l2_device *v4l2_dev)
239 struct shark_device *shark = v4l2_dev_to_shark(v4l2_dev);
241 cancel_work_sync(&shark->led_work);
242 v4l2_device_unregister(&shark->v4l2_dev);
243 kfree(shark->transfer_buffer);
247 static int usb_shark_probe(struct usb_interface *intf,
248 const struct usb_device_id *id)
250 struct shark_device *shark;
251 int i, retval = -ENOMEM;
253 shark = kzalloc(sizeof(struct shark_device), GFP_KERNEL);
257 shark->transfer_buffer = kmalloc(TB_LEN, GFP_KERNEL);
258 if (!shark->transfer_buffer)
259 goto err_alloc_buffer;
261 shark->v4l2_dev.release = usb_shark_release;
262 v4l2_device_set_name(&shark->v4l2_dev, DRV_NAME, &shark_instance);
263 retval = v4l2_device_register(&intf->dev, &shark->v4l2_dev);
265 v4l2_err(&shark->v4l2_dev, "couldn't register v4l2_device\n");
269 shark->usbdev = interface_to_usbdev(intf);
270 shark->tea.v4l2_dev = &shark->v4l2_dev;
271 shark->tea.private_data = shark;
272 shark->tea.ops = &shark_tea_ops;
273 shark->tea.has_am = true;
274 shark->tea.write_before_read = true;
275 strlcpy(shark->tea.card, "Griffin radioSHARK2",
276 sizeof(shark->tea.card));
277 usb_make_path(shark->usbdev, shark->tea.bus_info,
278 sizeof(shark->tea.bus_info));
280 retval = radio_tea5777_init(&shark->tea, THIS_MODULE);
282 v4l2_err(&shark->v4l2_dev, "couldn't init tea5777\n");
286 INIT_WORK(&shark->led_work, shark_led_work);
287 for (i = 0; i < NO_LEDS; i++) {
288 shark->leds[i] = shark_led_templates[i];
289 snprintf(shark->led_names[i], sizeof(shark->led_names[0]),
290 shark->leds[i].name, shark->v4l2_dev.name);
291 shark->leds[i].name = shark->led_names[i];
293 * We don't fail the probe if we fail to register the leds,
294 * because once we've called radio_tea5777_init, the /dev/radio0
295 * node may be opened from userspace holding a reference to us!
297 * Note we cannot register the leds first instead as
298 * shark_led_work depends on the v4l2 mutex and registered bit.
300 retval = led_classdev_register(&intf->dev, &shark->leds[i]);
302 v4l2_err(&shark->v4l2_dev,
303 "couldn't register led: %s\n",
304 shark->led_names[i]);
310 v4l2_device_unregister(&shark->v4l2_dev);
312 kfree(shark->transfer_buffer);
319 /* Specify the bcdDevice value, as the radioSHARK and radioSHARK2 share ids */
320 static struct usb_device_id usb_shark_device_table[] = {
321 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE_AND_VERSION |
322 USB_DEVICE_ID_MATCH_INT_CLASS,
325 .bcdDevice_lo = 0x0010,
326 .bcdDevice_hi = 0x0010,
327 .bInterfaceClass = 3,
331 MODULE_DEVICE_TABLE(usb, usb_shark_device_table);
333 static struct usb_driver usb_shark_driver = {
335 .probe = usb_shark_probe,
336 .disconnect = usb_shark_disconnect,
337 .id_table = usb_shark_device_table,
339 module_usb_driver(usb_shark_driver);