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 #if defined(CONFIG_LEDS_CLASS) || \
39 (defined(CONFIG_LEDS_CLASS_MODULE) && defined(CONFIG_RADIO_SHARK2_MODULE))
40 #define SHARK_USE_LEDS 1
43 MODULE_AUTHOR("Hans de Goede <hdegoede@redhat.com>");
44 MODULE_DESCRIPTION("Griffin radioSHARK2, USB radio receiver driver");
45 MODULE_LICENSE("GPL");
48 module_param(debug
, int, 0);
49 MODULE_PARM_DESC(debug
, "Debug level (0-1)");
51 #define SHARK_IN_EP 0x83
52 #define SHARK_OUT_EP 0x05
55 #define DRV_NAME "radioshark2"
57 #define v4l2_dev_to_shark(d) container_of(d, struct shark_device, v4l2_dev)
59 enum { BLUE_LED
, RED_LED
, NO_LEDS
};
62 struct usb_device
*usbdev
;
63 struct v4l2_device v4l2_dev
;
64 struct radio_tea5777 tea
;
67 struct work_struct led_work
;
68 struct led_classdev leds
[NO_LEDS
];
69 char led_names
[NO_LEDS
][32];
70 atomic_t brightness
[NO_LEDS
];
71 unsigned long brightness_new
;
77 static atomic_t shark_instance
= ATOMIC_INIT(0);
79 static int shark_write_reg(struct radio_tea5777
*tea
, u64 reg
)
81 struct shark_device
*shark
= tea
->private_data
;
82 int i
, res
, actual_len
;
84 memset(shark
->transfer_buffer
, 0, TB_LEN
);
85 shark
->transfer_buffer
[0] = 0x81; /* Write register command */
86 for (i
= 0; i
< 6; i
++)
87 shark
->transfer_buffer
[i
+ 1] = (reg
>> (40 - i
* 8)) & 0xff;
89 v4l2_dbg(1, debug
, tea
->v4l2_dev
,
90 "shark2-write: %02x %02x %02x %02x %02x %02x %02x\n",
91 shark
->transfer_buffer
[0], shark
->transfer_buffer
[1],
92 shark
->transfer_buffer
[2], shark
->transfer_buffer
[3],
93 shark
->transfer_buffer
[4], shark
->transfer_buffer
[5],
94 shark
->transfer_buffer
[6]);
96 res
= usb_interrupt_msg(shark
->usbdev
,
97 usb_sndintpipe(shark
->usbdev
, SHARK_OUT_EP
),
98 shark
->transfer_buffer
, TB_LEN
,
101 v4l2_err(tea
->v4l2_dev
, "write error: %d\n", res
);
108 static int shark_read_reg(struct radio_tea5777
*tea
, u32
*reg_ret
)
110 struct shark_device
*shark
= tea
->private_data
;
111 int i
, res
, actual_len
;
114 memset(shark
->transfer_buffer
, 0, TB_LEN
);
115 shark
->transfer_buffer
[0] = 0x82;
116 res
= usb_interrupt_msg(shark
->usbdev
,
117 usb_sndintpipe(shark
->usbdev
, SHARK_OUT_EP
),
118 shark
->transfer_buffer
, TB_LEN
,
121 v4l2_err(tea
->v4l2_dev
, "request-read error: %d\n", res
);
125 res
= usb_interrupt_msg(shark
->usbdev
,
126 usb_rcvintpipe(shark
->usbdev
, SHARK_IN_EP
),
127 shark
->transfer_buffer
, TB_LEN
,
130 v4l2_err(tea
->v4l2_dev
, "read error: %d\n", res
);
134 for (i
= 0; i
< 3; i
++)
135 reg
|= shark
->transfer_buffer
[i
] << (16 - i
* 8);
137 v4l2_dbg(1, debug
, tea
->v4l2_dev
, "shark2-read: %02x %02x %02x\n",
138 shark
->transfer_buffer
[0], shark
->transfer_buffer
[1],
139 shark
->transfer_buffer
[2]);
145 static struct radio_tea5777_ops shark_tea_ops
= {
146 .write_reg
= shark_write_reg
,
147 .read_reg
= shark_read_reg
,
150 #ifdef SHARK_USE_LEDS
151 static void shark_led_work(struct work_struct
*work
)
153 struct shark_device
*shark
=
154 container_of(work
, struct shark_device
, led_work
);
155 int i
, res
, brightness
, actual_len
;
157 for (i
= 0; i
< 2; i
++) {
158 if (!test_and_clear_bit(i
, &shark
->brightness_new
))
161 brightness
= atomic_read(&shark
->brightness
[i
]);
162 memset(shark
->transfer_buffer
, 0, TB_LEN
);
163 shark
->transfer_buffer
[0] = 0x83 + i
;
164 shark
->transfer_buffer
[1] = brightness
;
165 res
= usb_interrupt_msg(shark
->usbdev
,
166 usb_sndintpipe(shark
->usbdev
,
168 shark
->transfer_buffer
, TB_LEN
,
171 v4l2_err(&shark
->v4l2_dev
, "set LED %s error: %d\n",
172 shark
->led_names
[i
], res
);
176 static void shark_led_set_blue(struct led_classdev
*led_cdev
,
177 enum led_brightness value
)
179 struct shark_device
*shark
=
180 container_of(led_cdev
, struct shark_device
, leds
[BLUE_LED
]);
182 atomic_set(&shark
->brightness
[BLUE_LED
], value
);
183 set_bit(BLUE_LED
, &shark
->brightness_new
);
184 schedule_work(&shark
->led_work
);
187 static void shark_led_set_red(struct led_classdev
*led_cdev
,
188 enum led_brightness value
)
190 struct shark_device
*shark
=
191 container_of(led_cdev
, struct shark_device
, leds
[RED_LED
]);
193 atomic_set(&shark
->brightness
[RED_LED
], value
);
194 set_bit(RED_LED
, &shark
->brightness_new
);
195 schedule_work(&shark
->led_work
);
198 static const struct led_classdev shark_led_templates
[NO_LEDS
] = {
201 .brightness
= LED_OFF
,
202 .max_brightness
= 127,
203 .brightness_set
= shark_led_set_blue
,
207 .brightness
= LED_OFF
,
209 .brightness_set
= shark_led_set_red
,
213 static int shark_register_leds(struct shark_device
*shark
, struct device
*dev
)
217 INIT_WORK(&shark
->led_work
, shark_led_work
);
218 for (i
= 0; i
< NO_LEDS
; i
++) {
219 shark
->leds
[i
] = shark_led_templates
[i
];
220 snprintf(shark
->led_names
[i
], sizeof(shark
->led_names
[0]),
221 shark
->leds
[i
].name
, shark
->v4l2_dev
.name
);
222 shark
->leds
[i
].name
= shark
->led_names
[i
];
223 retval
= led_classdev_register(dev
, &shark
->leds
[i
]);
225 v4l2_err(&shark
->v4l2_dev
,
226 "couldn't register led: %s\n",
227 shark
->led_names
[i
]);
234 static void shark_unregister_leds(struct shark_device
*shark
)
238 for (i
= 0; i
< NO_LEDS
; i
++)
239 led_classdev_unregister(&shark
->leds
[i
]);
241 cancel_work_sync(&shark
->led_work
);
244 static int shark_register_leds(struct shark_device
*shark
, struct device
*dev
)
246 v4l2_warn(&shark
->v4l2_dev
,
247 "CONFIG_LED_CLASS not enabled, LED support disabled\n");
250 static inline void shark_unregister_leds(struct shark_device
*shark
) { }
253 static void usb_shark_disconnect(struct usb_interface
*intf
)
255 struct v4l2_device
*v4l2_dev
= usb_get_intfdata(intf
);
256 struct shark_device
*shark
= v4l2_dev_to_shark(v4l2_dev
);
258 mutex_lock(&shark
->tea
.mutex
);
259 v4l2_device_disconnect(&shark
->v4l2_dev
);
260 radio_tea5777_exit(&shark
->tea
);
261 mutex_unlock(&shark
->tea
.mutex
);
263 shark_unregister_leds(shark
);
265 v4l2_device_put(&shark
->v4l2_dev
);
268 static void usb_shark_release(struct v4l2_device
*v4l2_dev
)
270 struct shark_device
*shark
= v4l2_dev_to_shark(v4l2_dev
);
272 v4l2_device_unregister(&shark
->v4l2_dev
);
273 kfree(shark
->transfer_buffer
);
277 static int usb_shark_probe(struct usb_interface
*intf
,
278 const struct usb_device_id
*id
)
280 struct shark_device
*shark
;
281 int retval
= -ENOMEM
;
283 shark
= kzalloc(sizeof(struct shark_device
), GFP_KERNEL
);
287 shark
->transfer_buffer
= kmalloc(TB_LEN
, GFP_KERNEL
);
288 if (!shark
->transfer_buffer
)
289 goto err_alloc_buffer
;
291 v4l2_device_set_name(&shark
->v4l2_dev
, DRV_NAME
, &shark_instance
);
293 retval
= shark_register_leds(shark
, &intf
->dev
);
297 shark
->v4l2_dev
.release
= usb_shark_release
;
298 retval
= v4l2_device_register(&intf
->dev
, &shark
->v4l2_dev
);
300 v4l2_err(&shark
->v4l2_dev
, "couldn't register v4l2_device\n");
304 shark
->usbdev
= interface_to_usbdev(intf
);
305 shark
->tea
.v4l2_dev
= &shark
->v4l2_dev
;
306 shark
->tea
.private_data
= shark
;
307 shark
->tea
.ops
= &shark_tea_ops
;
308 shark
->tea
.has_am
= true;
309 shark
->tea
.write_before_read
= true;
310 strlcpy(shark
->tea
.card
, "Griffin radioSHARK2",
311 sizeof(shark
->tea
.card
));
312 usb_make_path(shark
->usbdev
, shark
->tea
.bus_info
,
313 sizeof(shark
->tea
.bus_info
));
315 retval
= radio_tea5777_init(&shark
->tea
, THIS_MODULE
);
317 v4l2_err(&shark
->v4l2_dev
, "couldn't init tea5777\n");
324 v4l2_device_unregister(&shark
->v4l2_dev
);
326 shark_unregister_leds(shark
);
328 kfree(shark
->transfer_buffer
);
335 /* Specify the bcdDevice value, as the radioSHARK and radioSHARK2 share ids */
336 static struct usb_device_id usb_shark_device_table
[] = {
337 { .match_flags
= USB_DEVICE_ID_MATCH_DEVICE_AND_VERSION
|
338 USB_DEVICE_ID_MATCH_INT_CLASS
,
341 .bcdDevice_lo
= 0x0010,
342 .bcdDevice_hi
= 0x0010,
343 .bInterfaceClass
= 3,
347 MODULE_DEVICE_TABLE(usb
, usb_shark_device_table
);
349 static struct usb_driver usb_shark_driver
= {
351 .probe
= usb_shark_probe
,
352 .disconnect
= usb_shark_disconnect
,
353 .id_table
= usb_shark_device_table
,
355 module_usb_driver(usb_shark_driver
);