Commit | Line | Data |
---|---|---|
8e2ce73e HG |
1 | /* |
2 | * Linux V4L2 radio driver for the Griffin radioSHARK USB radio receiver | |
3 | * | |
4 | * Note the radioSHARK offers the audio through a regular USB audio device, | |
5 | * this driver only handles the tuning. | |
6 | * | |
7 | * The info necessary to drive the shark was taken from the small userspace | |
8 | * shark.c program by Michael Rolig, 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 <sound/tea575x-tuner.h> | |
37 | ||
3e3b92ca HG |
38 | #if defined(CONFIG_LEDS_CLASS) || \ |
39 | (defined(CONFIG_LEDS_CLASS_MODULE) && defined(CONFIG_RADIO_SHARK_MODULE)) | |
40 | #define SHARK_USE_LEDS 1 | |
41 | #endif | |
42 | ||
8e2ce73e HG |
43 | /* |
44 | * Version Information | |
45 | */ | |
46 | MODULE_AUTHOR("Hans de Goede <hdegoede@redhat.com>"); | |
47 | MODULE_DESCRIPTION("Griffin radioSHARK, USB radio receiver driver"); | |
48 | MODULE_LICENSE("GPL"); | |
49 | ||
50 | #define SHARK_IN_EP 0x83 | |
51 | #define SHARK_OUT_EP 0x05 | |
52 | ||
53 | #define TEA575X_BIT_MONO (1<<22) /* 0 = stereo, 1 = mono */ | |
54 | #define TEA575X_BIT_BAND_MASK (3<<20) | |
55 | #define TEA575X_BIT_BAND_FM (0<<20) | |
56 | ||
57 | #define TB_LEN 6 | |
58 | #define DRV_NAME "radioshark" | |
59 | ||
60 | #define v4l2_dev_to_shark(d) container_of(d, struct shark_device, v4l2_dev) | |
61 | ||
62 | enum { BLUE_LED, BLUE_PULSE_LED, RED_LED, NO_LEDS }; | |
63 | ||
8e2ce73e HG |
64 | struct shark_device { |
65 | struct usb_device *usbdev; | |
66 | struct v4l2_device v4l2_dev; | |
67 | struct snd_tea575x tea; | |
68 | ||
3e3b92ca | 69 | #ifdef SHARK_USE_LEDS |
8e2ce73e HG |
70 | struct work_struct led_work; |
71 | struct led_classdev leds[NO_LEDS]; | |
72 | char led_names[NO_LEDS][32]; | |
73 | atomic_t brightness[NO_LEDS]; | |
74 | unsigned long brightness_new; | |
3e3b92ca | 75 | #endif |
8e2ce73e HG |
76 | |
77 | u8 *transfer_buffer; | |
78 | u32 last_val; | |
79 | }; | |
80 | ||
81 | static atomic_t shark_instance = ATOMIC_INIT(0); | |
82 | ||
83 | static void shark_write_val(struct snd_tea575x *tea, u32 val) | |
84 | { | |
85 | struct shark_device *shark = tea->private_data; | |
86 | int i, res, actual_len; | |
87 | ||
88 | /* Avoid unnecessary (slow) USB transfers */ | |
89 | if (shark->last_val == val) | |
90 | return; | |
91 | ||
92 | memset(shark->transfer_buffer, 0, TB_LEN); | |
93 | shark->transfer_buffer[0] = 0xc0; /* Write shift register command */ | |
94 | for (i = 0; i < 4; i++) | |
95 | shark->transfer_buffer[i] |= (val >> (24 - i * 8)) & 0xff; | |
96 | ||
97 | res = usb_interrupt_msg(shark->usbdev, | |
98 | usb_sndintpipe(shark->usbdev, SHARK_OUT_EP), | |
99 | shark->transfer_buffer, TB_LEN, | |
100 | &actual_len, 1000); | |
101 | if (res >= 0) | |
102 | shark->last_val = val; | |
103 | else | |
104 | v4l2_err(&shark->v4l2_dev, "set-freq error: %d\n", res); | |
105 | } | |
106 | ||
107 | static u32 shark_read_val(struct snd_tea575x *tea) | |
108 | { | |
109 | struct shark_device *shark = tea->private_data; | |
110 | int i, res, actual_len; | |
111 | u32 val = 0; | |
112 | ||
113 | memset(shark->transfer_buffer, 0, TB_LEN); | |
114 | shark->transfer_buffer[0] = 0x80; | |
115 | res = usb_interrupt_msg(shark->usbdev, | |
116 | usb_sndintpipe(shark->usbdev, SHARK_OUT_EP), | |
117 | shark->transfer_buffer, TB_LEN, | |
118 | &actual_len, 1000); | |
119 | if (res < 0) { | |
120 | v4l2_err(&shark->v4l2_dev, "request-status error: %d\n", res); | |
121 | return shark->last_val; | |
122 | } | |
123 | ||
124 | res = usb_interrupt_msg(shark->usbdev, | |
125 | usb_rcvintpipe(shark->usbdev, SHARK_IN_EP), | |
126 | shark->transfer_buffer, TB_LEN, | |
127 | &actual_len, 1000); | |
128 | if (res < 0) { | |
129 | v4l2_err(&shark->v4l2_dev, "get-status error: %d\n", res); | |
130 | return shark->last_val; | |
131 | } | |
132 | ||
133 | for (i = 0; i < 4; i++) | |
134 | val |= shark->transfer_buffer[i] << (24 - i * 8); | |
135 | ||
136 | shark->last_val = val; | |
137 | ||
138 | /* | |
139 | * The shark does not allow actually reading the stereo / mono pin :( | |
140 | * So assume that when we're tuned to an FM station and mono has not | |
141 | * been requested, that we're receiving stereo. | |
142 | */ | |
143 | if (((val & TEA575X_BIT_BAND_MASK) == TEA575X_BIT_BAND_FM) && | |
144 | !(val & TEA575X_BIT_MONO)) | |
145 | shark->tea.stereo = true; | |
146 | else | |
147 | shark->tea.stereo = false; | |
148 | ||
149 | return val; | |
150 | } | |
151 | ||
152 | static struct snd_tea575x_ops shark_tea_ops = { | |
153 | .write_val = shark_write_val, | |
154 | .read_val = shark_read_val, | |
155 | }; | |
156 | ||
3e3b92ca | 157 | #ifdef SHARK_USE_LEDS |
8e2ce73e HG |
158 | static void shark_led_work(struct work_struct *work) |
159 | { | |
160 | struct shark_device *shark = | |
161 | container_of(work, struct shark_device, led_work); | |
162 | int i, res, brightness, actual_len; | |
163 | ||
8e2ce73e HG |
164 | for (i = 0; i < 3; i++) { |
165 | if (!test_and_clear_bit(i, &shark->brightness_new)) | |
166 | continue; | |
167 | ||
168 | brightness = atomic_read(&shark->brightness[i]); | |
169 | memset(shark->transfer_buffer, 0, TB_LEN); | |
170 | if (i != RED_LED) { | |
171 | shark->transfer_buffer[0] = 0xA0 + i; | |
172 | shark->transfer_buffer[1] = brightness; | |
173 | } else | |
174 | shark->transfer_buffer[0] = brightness ? 0xA9 : 0xA8; | |
175 | res = usb_interrupt_msg(shark->usbdev, | |
176 | usb_sndintpipe(shark->usbdev, 0x05), | |
177 | shark->transfer_buffer, TB_LEN, | |
178 | &actual_len, 1000); | |
179 | if (res < 0) | |
180 | v4l2_err(&shark->v4l2_dev, "set LED %s error: %d\n", | |
181 | shark->led_names[i], res); | |
182 | } | |
8e2ce73e HG |
183 | } |
184 | ||
185 | static void shark_led_set_blue(struct led_classdev *led_cdev, | |
186 | enum led_brightness value) | |
187 | { | |
188 | struct shark_device *shark = | |
189 | container_of(led_cdev, struct shark_device, leds[BLUE_LED]); | |
190 | ||
191 | atomic_set(&shark->brightness[BLUE_LED], value); | |
192 | set_bit(BLUE_LED, &shark->brightness_new); | |
193 | schedule_work(&shark->led_work); | |
194 | } | |
195 | ||
196 | static void shark_led_set_blue_pulse(struct led_classdev *led_cdev, | |
197 | enum led_brightness value) | |
198 | { | |
199 | struct shark_device *shark = container_of(led_cdev, | |
200 | struct shark_device, leds[BLUE_PULSE_LED]); | |
201 | ||
202 | atomic_set(&shark->brightness[BLUE_PULSE_LED], 256 - value); | |
203 | set_bit(BLUE_PULSE_LED, &shark->brightness_new); | |
204 | schedule_work(&shark->led_work); | |
205 | } | |
206 | ||
207 | static void shark_led_set_red(struct led_classdev *led_cdev, | |
208 | enum led_brightness value) | |
209 | { | |
210 | struct shark_device *shark = | |
211 | container_of(led_cdev, struct shark_device, leds[RED_LED]); | |
212 | ||
213 | atomic_set(&shark->brightness[RED_LED], value); | |
214 | set_bit(RED_LED, &shark->brightness_new); | |
215 | schedule_work(&shark->led_work); | |
216 | } | |
217 | ||
3e3b92ca HG |
218 | static const struct led_classdev shark_led_templates[NO_LEDS] = { |
219 | [BLUE_LED] = { | |
220 | .name = "%s:blue:", | |
221 | .brightness = LED_OFF, | |
222 | .max_brightness = 127, | |
223 | .brightness_set = shark_led_set_blue, | |
224 | }, | |
225 | [BLUE_PULSE_LED] = { | |
226 | .name = "%s:blue-pulse:", | |
227 | .brightness = LED_OFF, | |
228 | .max_brightness = 255, | |
229 | .brightness_set = shark_led_set_blue_pulse, | |
230 | }, | |
231 | [RED_LED] = { | |
232 | .name = "%s:red:", | |
233 | .brightness = LED_OFF, | |
234 | .max_brightness = 1, | |
235 | .brightness_set = shark_led_set_red, | |
236 | }, | |
237 | }; | |
238 | ||
239 | static int shark_register_leds(struct shark_device *shark, struct device *dev) | |
240 | { | |
241 | int i, retval; | |
242 | ||
243 | INIT_WORK(&shark->led_work, shark_led_work); | |
244 | for (i = 0; i < NO_LEDS; i++) { | |
245 | shark->leds[i] = shark_led_templates[i]; | |
246 | snprintf(shark->led_names[i], sizeof(shark->led_names[0]), | |
247 | shark->leds[i].name, shark->v4l2_dev.name); | |
248 | shark->leds[i].name = shark->led_names[i]; | |
249 | retval = led_classdev_register(dev, &shark->leds[i]); | |
250 | if (retval) { | |
251 | v4l2_err(&shark->v4l2_dev, | |
252 | "couldn't register led: %s\n", | |
253 | shark->led_names[i]); | |
254 | return retval; | |
255 | } | |
256 | } | |
257 | return 0; | |
258 | } | |
259 | ||
260 | static void shark_unregister_leds(struct shark_device *shark) | |
261 | { | |
262 | int i; | |
263 | ||
264 | for (i = 0; i < NO_LEDS; i++) | |
265 | led_classdev_unregister(&shark->leds[i]); | |
266 | ||
267 | cancel_work_sync(&shark->led_work); | |
268 | } | |
269 | #else | |
270 | static int shark_register_leds(struct shark_device *shark, struct device *dev) | |
271 | { | |
272 | v4l2_warn(&shark->v4l2_dev, | |
273 | "CONFIG_LED_CLASS not enabled, LED support disabled\n"); | |
274 | return 0; | |
275 | } | |
276 | static inline void shark_unregister_leds(struct shark_device *shark) { } | |
277 | #endif | |
278 | ||
8e2ce73e HG |
279 | static void usb_shark_disconnect(struct usb_interface *intf) |
280 | { | |
281 | struct v4l2_device *v4l2_dev = usb_get_intfdata(intf); | |
282 | struct shark_device *shark = v4l2_dev_to_shark(v4l2_dev); | |
8e2ce73e HG |
283 | |
284 | mutex_lock(&shark->tea.mutex); | |
285 | v4l2_device_disconnect(&shark->v4l2_dev); | |
286 | snd_tea575x_exit(&shark->tea); | |
287 | mutex_unlock(&shark->tea.mutex); | |
288 | ||
3e3b92ca | 289 | shark_unregister_leds(shark); |
cfc1b2a0 | 290 | |
8e2ce73e HG |
291 | v4l2_device_put(&shark->v4l2_dev); |
292 | } | |
293 | ||
294 | static void usb_shark_release(struct v4l2_device *v4l2_dev) | |
295 | { | |
296 | struct shark_device *shark = v4l2_dev_to_shark(v4l2_dev); | |
297 | ||
8e2ce73e HG |
298 | v4l2_device_unregister(&shark->v4l2_dev); |
299 | kfree(shark->transfer_buffer); | |
300 | kfree(shark); | |
301 | } | |
302 | ||
303 | static int usb_shark_probe(struct usb_interface *intf, | |
304 | const struct usb_device_id *id) | |
305 | { | |
306 | struct shark_device *shark; | |
3e3b92ca | 307 | int retval = -ENOMEM; |
8e2ce73e HG |
308 | |
309 | shark = kzalloc(sizeof(struct shark_device), GFP_KERNEL); | |
310 | if (!shark) | |
311 | return retval; | |
312 | ||
313 | shark->transfer_buffer = kmalloc(TB_LEN, GFP_KERNEL); | |
314 | if (!shark->transfer_buffer) | |
315 | goto err_alloc_buffer; | |
316 | ||
8e2ce73e | 317 | v4l2_device_set_name(&shark->v4l2_dev, DRV_NAME, &shark_instance); |
3e3b92ca HG |
318 | |
319 | retval = shark_register_leds(shark, &intf->dev); | |
320 | if (retval) | |
321 | goto err_reg_leds; | |
322 | ||
323 | shark->v4l2_dev.release = usb_shark_release; | |
8e2ce73e HG |
324 | retval = v4l2_device_register(&intf->dev, &shark->v4l2_dev); |
325 | if (retval) { | |
326 | v4l2_err(&shark->v4l2_dev, "couldn't register v4l2_device\n"); | |
327 | goto err_reg_dev; | |
328 | } | |
329 | ||
330 | shark->usbdev = interface_to_usbdev(intf); | |
331 | shark->tea.v4l2_dev = &shark->v4l2_dev; | |
332 | shark->tea.private_data = shark; | |
333 | shark->tea.radio_nr = -1; | |
334 | shark->tea.ops = &shark_tea_ops; | |
335 | shark->tea.cannot_mute = true; | |
fc488517 | 336 | shark->tea.has_am = true; |
8e2ce73e HG |
337 | strlcpy(shark->tea.card, "Griffin radioSHARK", |
338 | sizeof(shark->tea.card)); | |
339 | usb_make_path(shark->usbdev, shark->tea.bus_info, | |
340 | sizeof(shark->tea.bus_info)); | |
341 | ||
342 | retval = snd_tea575x_init(&shark->tea, THIS_MODULE); | |
343 | if (retval) { | |
344 | v4l2_err(&shark->v4l2_dev, "couldn't init tea5757\n"); | |
345 | goto err_init_tea; | |
346 | } | |
347 | ||
8e2ce73e HG |
348 | return 0; |
349 | ||
350 | err_init_tea: | |
351 | v4l2_device_unregister(&shark->v4l2_dev); | |
352 | err_reg_dev: | |
3e3b92ca HG |
353 | shark_unregister_leds(shark); |
354 | err_reg_leds: | |
8e2ce73e HG |
355 | kfree(shark->transfer_buffer); |
356 | err_alloc_buffer: | |
357 | kfree(shark); | |
358 | ||
359 | return retval; | |
360 | } | |
361 | ||
362 | /* Specify the bcdDevice value, as the radioSHARK and radioSHARK2 share ids */ | |
363 | static struct usb_device_id usb_shark_device_table[] = { | |
364 | { .match_flags = USB_DEVICE_ID_MATCH_DEVICE_AND_VERSION | | |
365 | USB_DEVICE_ID_MATCH_INT_CLASS, | |
366 | .idVendor = 0x077d, | |
367 | .idProduct = 0x627a, | |
368 | .bcdDevice_lo = 0x0001, | |
369 | .bcdDevice_hi = 0x0001, | |
370 | .bInterfaceClass = 3, | |
371 | }, | |
372 | { } | |
373 | }; | |
374 | MODULE_DEVICE_TABLE(usb, usb_shark_device_table); | |
375 | ||
376 | static struct usb_driver usb_shark_driver = { | |
377 | .name = DRV_NAME, | |
378 | .probe = usb_shark_probe, | |
379 | .disconnect = usb_shark_disconnect, | |
380 | .id_table = usb_shark_device_table, | |
381 | }; | |
382 | module_usb_driver(usb_shark_driver); |