Commit | Line | Data |
---|---|---|
4faba767 HG |
1 | /* |
2 | * Linux V4L2 radio driver for the Griffin radioSHARK2 USB radio receiver | |
3 | * | |
4 | * Note the radioSHARK2 offers the audio through a regular USB audio device, | |
5 | * this driver only handles the tuning. | |
6 | * | |
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 | |
9 | * Domain. | |
10 | * | |
11 | * Copyright (c) 2012 Hans de Goede <hdegoede@redhat.com> | |
12 | * | |
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. | |
17 | * | |
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. | |
22 | * | |
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 | |
26 | */ | |
27 | ||
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" | |
37 | ||
be0c44fb HG |
38 | #if defined(CONFIG_LEDS_CLASS) || \ |
39 | (defined(CONFIG_LEDS_CLASS_MODULE) && defined(CONFIG_RADIO_SHARK2_MODULE)) | |
40 | #define SHARK_USE_LEDS 1 | |
41 | #endif | |
42 | ||
4faba767 HG |
43 | MODULE_AUTHOR("Hans de Goede <hdegoede@redhat.com>"); |
44 | MODULE_DESCRIPTION("Griffin radioSHARK2, USB radio receiver driver"); | |
45 | MODULE_LICENSE("GPL"); | |
46 | ||
47 | static int debug; | |
48 | module_param(debug, int, 0); | |
49 | MODULE_PARM_DESC(debug, "Debug level (0-1)"); | |
50 | ||
4faba767 HG |
51 | #define SHARK_IN_EP 0x83 |
52 | #define SHARK_OUT_EP 0x05 | |
53 | ||
54 | #define TB_LEN 7 | |
55 | #define DRV_NAME "radioshark2" | |
56 | ||
57 | #define v4l2_dev_to_shark(d) container_of(d, struct shark_device, v4l2_dev) | |
58 | ||
59 | enum { BLUE_LED, RED_LED, NO_LEDS }; | |
60 | ||
4faba767 HG |
61 | struct shark_device { |
62 | struct usb_device *usbdev; | |
63 | struct v4l2_device v4l2_dev; | |
64 | struct radio_tea5777 tea; | |
65 | ||
be0c44fb | 66 | #ifdef SHARK_USE_LEDS |
4faba767 HG |
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; | |
be0c44fb | 72 | #endif |
4faba767 HG |
73 | |
74 | u8 *transfer_buffer; | |
75 | }; | |
76 | ||
77 | static atomic_t shark_instance = ATOMIC_INIT(0); | |
78 | ||
79 | static int shark_write_reg(struct radio_tea5777 *tea, u64 reg) | |
80 | { | |
81 | struct shark_device *shark = tea->private_data; | |
82 | int i, res, actual_len; | |
83 | ||
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; | |
88 | ||
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]); | |
95 | ||
96 | res = usb_interrupt_msg(shark->usbdev, | |
97 | usb_sndintpipe(shark->usbdev, SHARK_OUT_EP), | |
98 | shark->transfer_buffer, TB_LEN, | |
99 | &actual_len, 1000); | |
100 | if (res < 0) { | |
101 | v4l2_err(tea->v4l2_dev, "write error: %d\n", res); | |
102 | return res; | |
103 | } | |
104 | ||
105 | return 0; | |
106 | } | |
107 | ||
108 | static int shark_read_reg(struct radio_tea5777 *tea, u32 *reg_ret) | |
109 | { | |
110 | struct shark_device *shark = tea->private_data; | |
111 | int i, res, actual_len; | |
112 | u32 reg = 0; | |
113 | ||
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, | |
119 | &actual_len, 1000); | |
120 | if (res < 0) { | |
121 | v4l2_err(tea->v4l2_dev, "request-read error: %d\n", res); | |
122 | return res; | |
123 | } | |
124 | ||
125 | res = usb_interrupt_msg(shark->usbdev, | |
126 | usb_rcvintpipe(shark->usbdev, SHARK_IN_EP), | |
127 | shark->transfer_buffer, TB_LEN, | |
128 | &actual_len, 1000); | |
129 | if (res < 0) { | |
130 | v4l2_err(tea->v4l2_dev, "read error: %d\n", res); | |
131 | return res; | |
132 | } | |
133 | ||
134 | for (i = 0; i < 3; i++) | |
135 | reg |= shark->transfer_buffer[i] << (16 - i * 8); | |
136 | ||
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]); | |
140 | ||
141 | *reg_ret = reg; | |
142 | return 0; | |
143 | } | |
144 | ||
145 | static struct radio_tea5777_ops shark_tea_ops = { | |
146 | .write_reg = shark_write_reg, | |
147 | .read_reg = shark_read_reg, | |
148 | }; | |
149 | ||
be0c44fb | 150 | #ifdef SHARK_USE_LEDS |
4faba767 HG |
151 | static void shark_led_work(struct work_struct *work) |
152 | { | |
153 | struct shark_device *shark = | |
154 | container_of(work, struct shark_device, led_work); | |
155 | int i, res, brightness, actual_len; | |
4faba767 HG |
156 | |
157 | for (i = 0; i < 2; i++) { | |
158 | if (!test_and_clear_bit(i, &shark->brightness_new)) | |
159 | continue; | |
160 | ||
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, | |
167 | SHARK_OUT_EP), | |
168 | shark->transfer_buffer, TB_LEN, | |
169 | &actual_len, 1000); | |
170 | if (res < 0) | |
171 | v4l2_err(&shark->v4l2_dev, "set LED %s error: %d\n", | |
172 | shark->led_names[i], res); | |
173 | } | |
4faba767 HG |
174 | } |
175 | ||
176 | static void shark_led_set_blue(struct led_classdev *led_cdev, | |
177 | enum led_brightness value) | |
178 | { | |
179 | struct shark_device *shark = | |
180 | container_of(led_cdev, struct shark_device, leds[BLUE_LED]); | |
181 | ||
182 | atomic_set(&shark->brightness[BLUE_LED], value); | |
183 | set_bit(BLUE_LED, &shark->brightness_new); | |
184 | schedule_work(&shark->led_work); | |
185 | } | |
186 | ||
187 | static void shark_led_set_red(struct led_classdev *led_cdev, | |
188 | enum led_brightness value) | |
189 | { | |
190 | struct shark_device *shark = | |
191 | container_of(led_cdev, struct shark_device, leds[RED_LED]); | |
192 | ||
193 | atomic_set(&shark->brightness[RED_LED], value); | |
194 | set_bit(RED_LED, &shark->brightness_new); | |
195 | schedule_work(&shark->led_work); | |
196 | } | |
197 | ||
be0c44fb HG |
198 | static const struct led_classdev shark_led_templates[NO_LEDS] = { |
199 | [BLUE_LED] = { | |
200 | .name = "%s:blue:", | |
201 | .brightness = LED_OFF, | |
202 | .max_brightness = 127, | |
203 | .brightness_set = shark_led_set_blue, | |
204 | }, | |
205 | [RED_LED] = { | |
206 | .name = "%s:red:", | |
207 | .brightness = LED_OFF, | |
208 | .max_brightness = 1, | |
209 | .brightness_set = shark_led_set_red, | |
210 | }, | |
211 | }; | |
212 | ||
213 | static int shark_register_leds(struct shark_device *shark, struct device *dev) | |
214 | { | |
215 | int i, retval; | |
216 | ||
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]); | |
224 | if (retval) { | |
225 | v4l2_err(&shark->v4l2_dev, | |
226 | "couldn't register led: %s\n", | |
227 | shark->led_names[i]); | |
228 | return retval; | |
229 | } | |
230 | } | |
231 | return 0; | |
232 | } | |
233 | ||
234 | static void shark_unregister_leds(struct shark_device *shark) | |
235 | { | |
236 | int i; | |
237 | ||
238 | for (i = 0; i < NO_LEDS; i++) | |
239 | led_classdev_unregister(&shark->leds[i]); | |
240 | ||
241 | cancel_work_sync(&shark->led_work); | |
242 | } | |
243 | #else | |
244 | static int shark_register_leds(struct shark_device *shark, struct device *dev) | |
245 | { | |
246 | v4l2_warn(&shark->v4l2_dev, | |
247 | "CONFIG_LED_CLASS not enabled, LED support disabled\n"); | |
248 | return 0; | |
249 | } | |
250 | static inline void shark_unregister_leds(struct shark_device *shark) { } | |
251 | #endif | |
252 | ||
4faba767 HG |
253 | static void usb_shark_disconnect(struct usb_interface *intf) |
254 | { | |
255 | struct v4l2_device *v4l2_dev = usb_get_intfdata(intf); | |
256 | struct shark_device *shark = v4l2_dev_to_shark(v4l2_dev); | |
4faba767 HG |
257 | |
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); | |
262 | ||
be0c44fb | 263 | shark_unregister_leds(shark); |
cfc1b2a0 | 264 | |
4faba767 HG |
265 | v4l2_device_put(&shark->v4l2_dev); |
266 | } | |
267 | ||
268 | static void usb_shark_release(struct v4l2_device *v4l2_dev) | |
269 | { | |
270 | struct shark_device *shark = v4l2_dev_to_shark(v4l2_dev); | |
271 | ||
4faba767 HG |
272 | v4l2_device_unregister(&shark->v4l2_dev); |
273 | kfree(shark->transfer_buffer); | |
274 | kfree(shark); | |
275 | } | |
276 | ||
277 | static int usb_shark_probe(struct usb_interface *intf, | |
278 | const struct usb_device_id *id) | |
279 | { | |
280 | struct shark_device *shark; | |
be0c44fb | 281 | int retval = -ENOMEM; |
4faba767 HG |
282 | |
283 | shark = kzalloc(sizeof(struct shark_device), GFP_KERNEL); | |
284 | if (!shark) | |
285 | return retval; | |
286 | ||
287 | shark->transfer_buffer = kmalloc(TB_LEN, GFP_KERNEL); | |
288 | if (!shark->transfer_buffer) | |
289 | goto err_alloc_buffer; | |
290 | ||
4faba767 | 291 | v4l2_device_set_name(&shark->v4l2_dev, DRV_NAME, &shark_instance); |
be0c44fb HG |
292 | |
293 | retval = shark_register_leds(shark, &intf->dev); | |
294 | if (retval) | |
295 | goto err_reg_leds; | |
296 | ||
297 | shark->v4l2_dev.release = usb_shark_release; | |
4faba767 HG |
298 | retval = v4l2_device_register(&intf->dev, &shark->v4l2_dev); |
299 | if (retval) { | |
300 | v4l2_err(&shark->v4l2_dev, "couldn't register v4l2_device\n"); | |
301 | goto err_reg_dev; | |
302 | } | |
303 | ||
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)); | |
314 | ||
315 | retval = radio_tea5777_init(&shark->tea, THIS_MODULE); | |
316 | if (retval) { | |
317 | v4l2_err(&shark->v4l2_dev, "couldn't init tea5777\n"); | |
318 | goto err_init_tea; | |
319 | } | |
320 | ||
4faba767 HG |
321 | return 0; |
322 | ||
323 | err_init_tea: | |
324 | v4l2_device_unregister(&shark->v4l2_dev); | |
325 | err_reg_dev: | |
be0c44fb HG |
326 | shark_unregister_leds(shark); |
327 | err_reg_leds: | |
4faba767 HG |
328 | kfree(shark->transfer_buffer); |
329 | err_alloc_buffer: | |
330 | kfree(shark); | |
331 | ||
332 | return retval; | |
333 | } | |
334 | ||
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, | |
339 | .idVendor = 0x077d, | |
340 | .idProduct = 0x627a, | |
341 | .bcdDevice_lo = 0x0010, | |
342 | .bcdDevice_hi = 0x0010, | |
343 | .bInterfaceClass = 3, | |
344 | }, | |
345 | { } | |
346 | }; | |
347 | MODULE_DEVICE_TABLE(usb, usb_shark_device_table); | |
348 | ||
349 | static struct usb_driver usb_shark_driver = { | |
350 | .name = DRV_NAME, | |
351 | .probe = usb_shark_probe, | |
352 | .disconnect = usb_shark_disconnect, | |
353 | .id_table = usb_shark_device_table, | |
354 | }; | |
355 | module_usb_driver(usb_shark_driver); |