xref: /linux/drivers/media/radio/radio-shark.c (revision c5b0b3cb)
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 <media/tea575x.h>
37 
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 
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 /* Note BLUE_IS_PULSE comes after NO_LEDS as it is a status bit, not a LED */
63 enum { BLUE_LED, BLUE_PULSE_LED, RED_LED, NO_LEDS, BLUE_IS_PULSE };
64 
65 struct shark_device {
66 	struct usb_device *usbdev;
67 	struct v4l2_device v4l2_dev;
68 	struct snd_tea575x tea;
69 
70 #ifdef SHARK_USE_LEDS
71 	struct work_struct led_work;
72 	struct led_classdev leds[NO_LEDS];
73 	char led_names[NO_LEDS][32];
74 	atomic_t brightness[NO_LEDS];
75 	unsigned long brightness_new;
76 #endif
77 
78 	u8 *transfer_buffer;
79 	u32 last_val;
80 };
81 
82 static atomic_t shark_instance = ATOMIC_INIT(0);
83 
84 static void shark_write_val(struct snd_tea575x *tea, u32 val)
85 {
86 	struct shark_device *shark = tea->private_data;
87 	int i, res, actual_len;
88 
89 	/* Avoid unnecessary (slow) USB transfers */
90 	if (shark->last_val == val)
91 		return;
92 
93 	memset(shark->transfer_buffer, 0, TB_LEN);
94 	shark->transfer_buffer[0] = 0xc0; /* Write shift register command */
95 	for (i = 0; i < 4; i++)
96 		shark->transfer_buffer[i] |= (val >> (24 - i * 8)) & 0xff;
97 
98 	res = usb_interrupt_msg(shark->usbdev,
99 				usb_sndintpipe(shark->usbdev, SHARK_OUT_EP),
100 				shark->transfer_buffer, TB_LEN,
101 				&actual_len, 1000);
102 	if (res >= 0)
103 		shark->last_val = val;
104 	else
105 		v4l2_err(&shark->v4l2_dev, "set-freq error: %d\n", res);
106 }
107 
108 static u32 shark_read_val(struct snd_tea575x *tea)
109 {
110 	struct shark_device *shark = tea->private_data;
111 	int i, res, actual_len;
112 	u32 val = 0;
113 
114 	memset(shark->transfer_buffer, 0, TB_LEN);
115 	shark->transfer_buffer[0] = 0x80;
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(&shark->v4l2_dev, "request-status error: %d\n", res);
122 		return shark->last_val;
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(&shark->v4l2_dev, "get-status error: %d\n", res);
131 		return shark->last_val;
132 	}
133 
134 	for (i = 0; i < 4; i++)
135 		val |= shark->transfer_buffer[i] << (24 - i * 8);
136 
137 	shark->last_val = val;
138 
139 	/*
140 	 * The shark does not allow actually reading the stereo / mono pin :(
141 	 * So assume that when we're tuned to an FM station and mono has not
142 	 * been requested, that we're receiving stereo.
143 	 */
144 	if (((val & TEA575X_BIT_BAND_MASK) == TEA575X_BIT_BAND_FM) &&
145 	    !(val & TEA575X_BIT_MONO))
146 		shark->tea.stereo = true;
147 	else
148 		shark->tea.stereo = false;
149 
150 	return val;
151 }
152 
153 static struct snd_tea575x_ops shark_tea_ops = {
154 	.write_val = shark_write_val,
155 	.read_val  = shark_read_val,
156 };
157 
158 #ifdef SHARK_USE_LEDS
159 static void shark_led_work(struct work_struct *work)
160 {
161 	struct shark_device *shark =
162 		container_of(work, struct shark_device, led_work);
163 	int i, res, brightness, actual_len;
164 
165 	for (i = 0; i < 3; i++) {
166 		if (!test_and_clear_bit(i, &shark->brightness_new))
167 			continue;
168 
169 		brightness = atomic_read(&shark->brightness[i]);
170 		memset(shark->transfer_buffer, 0, TB_LEN);
171 		if (i != RED_LED) {
172 			shark->transfer_buffer[0] = 0xA0 + i;
173 			shark->transfer_buffer[1] = brightness;
174 		} else
175 			shark->transfer_buffer[0] = brightness ? 0xA9 : 0xA8;
176 		res = usb_interrupt_msg(shark->usbdev,
177 					usb_sndintpipe(shark->usbdev, 0x05),
178 					shark->transfer_buffer, TB_LEN,
179 					&actual_len, 1000);
180 		if (res < 0)
181 			v4l2_err(&shark->v4l2_dev, "set LED %s error: %d\n",
182 				 shark->led_names[i], res);
183 	}
184 }
185 
186 static void shark_led_set_blue(struct led_classdev *led_cdev,
187 			       enum led_brightness value)
188 {
189 	struct shark_device *shark =
190 		container_of(led_cdev, struct shark_device, leds[BLUE_LED]);
191 
192 	atomic_set(&shark->brightness[BLUE_LED], value);
193 	set_bit(BLUE_LED, &shark->brightness_new);
194 	clear_bit(BLUE_IS_PULSE, &shark->brightness_new);
195 	schedule_work(&shark->led_work);
196 }
197 
198 static void shark_led_set_blue_pulse(struct led_classdev *led_cdev,
199 				     enum led_brightness value)
200 {
201 	struct shark_device *shark = container_of(led_cdev,
202 				struct shark_device, leds[BLUE_PULSE_LED]);
203 
204 	atomic_set(&shark->brightness[BLUE_PULSE_LED], 256 - value);
205 	set_bit(BLUE_PULSE_LED, &shark->brightness_new);
206 	set_bit(BLUE_IS_PULSE, &shark->brightness_new);
207 	schedule_work(&shark->led_work);
208 }
209 
210 static void shark_led_set_red(struct led_classdev *led_cdev,
211 			      enum led_brightness value)
212 {
213 	struct shark_device *shark =
214 		container_of(led_cdev, struct shark_device, leds[RED_LED]);
215 
216 	atomic_set(&shark->brightness[RED_LED], value);
217 	set_bit(RED_LED, &shark->brightness_new);
218 	schedule_work(&shark->led_work);
219 }
220 
221 static const struct led_classdev shark_led_templates[NO_LEDS] = {
222 	[BLUE_LED] = {
223 		.name		= "%s:blue:",
224 		.brightness	= LED_OFF,
225 		.max_brightness = 127,
226 		.brightness_set = shark_led_set_blue,
227 	},
228 	[BLUE_PULSE_LED] = {
229 		.name		= "%s:blue-pulse:",
230 		.brightness	= LED_OFF,
231 		.max_brightness = 255,
232 		.brightness_set = shark_led_set_blue_pulse,
233 	},
234 	[RED_LED] = {
235 		.name		= "%s:red:",
236 		.brightness	= LED_OFF,
237 		.max_brightness = 1,
238 		.brightness_set = shark_led_set_red,
239 	},
240 };
241 
242 static int shark_register_leds(struct shark_device *shark, struct device *dev)
243 {
244 	int i, retval;
245 
246 	atomic_set(&shark->brightness[BLUE_LED], 127);
247 	INIT_WORK(&shark->led_work, shark_led_work);
248 	for (i = 0; i < NO_LEDS; i++) {
249 		shark->leds[i] = shark_led_templates[i];
250 		snprintf(shark->led_names[i], sizeof(shark->led_names[0]),
251 			 shark->leds[i].name, shark->v4l2_dev.name);
252 		shark->leds[i].name = shark->led_names[i];
253 		retval = led_classdev_register(dev, &shark->leds[i]);
254 		if (retval) {
255 			v4l2_err(&shark->v4l2_dev,
256 				 "couldn't register led: %s\n",
257 				 shark->led_names[i]);
258 			return retval;
259 		}
260 	}
261 	return 0;
262 }
263 
264 static void shark_unregister_leds(struct shark_device *shark)
265 {
266 	int i;
267 
268 	for (i = 0; i < NO_LEDS; i++)
269 		led_classdev_unregister(&shark->leds[i]);
270 
271 	cancel_work_sync(&shark->led_work);
272 }
273 
274 #ifdef CONFIG_PM
275 static void shark_resume_leds(struct shark_device *shark)
276 {
277 	if (test_bit(BLUE_IS_PULSE, &shark->brightness_new))
278 		set_bit(BLUE_PULSE_LED, &shark->brightness_new);
279 	else
280 		set_bit(BLUE_LED, &shark->brightness_new);
281 	set_bit(RED_LED, &shark->brightness_new);
282 	schedule_work(&shark->led_work);
283 }
284 #endif
285 #else
286 static int shark_register_leds(struct shark_device *shark, struct device *dev)
287 {
288 	v4l2_warn(&shark->v4l2_dev,
289 		  "CONFIG_LEDS_CLASS not enabled, LED support disabled\n");
290 	return 0;
291 }
292 static inline void shark_unregister_leds(struct shark_device *shark) { }
293 static inline void shark_resume_leds(struct shark_device *shark) { }
294 #endif
295 
296 static void usb_shark_disconnect(struct usb_interface *intf)
297 {
298 	struct v4l2_device *v4l2_dev = usb_get_intfdata(intf);
299 	struct shark_device *shark = v4l2_dev_to_shark(v4l2_dev);
300 
301 	mutex_lock(&shark->tea.mutex);
302 	v4l2_device_disconnect(&shark->v4l2_dev);
303 	snd_tea575x_exit(&shark->tea);
304 	mutex_unlock(&shark->tea.mutex);
305 
306 	shark_unregister_leds(shark);
307 
308 	v4l2_device_put(&shark->v4l2_dev);
309 }
310 
311 static void usb_shark_release(struct v4l2_device *v4l2_dev)
312 {
313 	struct shark_device *shark = v4l2_dev_to_shark(v4l2_dev);
314 
315 	v4l2_device_unregister(&shark->v4l2_dev);
316 	kfree(shark->transfer_buffer);
317 	kfree(shark);
318 }
319 
320 static int usb_shark_probe(struct usb_interface *intf,
321 			   const struct usb_device_id *id)
322 {
323 	struct shark_device *shark;
324 	int retval = -ENOMEM;
325 
326 	shark = kzalloc(sizeof(struct shark_device), GFP_KERNEL);
327 	if (!shark)
328 		return retval;
329 
330 	shark->transfer_buffer = kmalloc(TB_LEN, GFP_KERNEL);
331 	if (!shark->transfer_buffer)
332 		goto err_alloc_buffer;
333 
334 	v4l2_device_set_name(&shark->v4l2_dev, DRV_NAME, &shark_instance);
335 
336 	retval = shark_register_leds(shark, &intf->dev);
337 	if (retval)
338 		goto err_reg_leds;
339 
340 	shark->v4l2_dev.release = usb_shark_release;
341 	retval = v4l2_device_register(&intf->dev, &shark->v4l2_dev);
342 	if (retval) {
343 		v4l2_err(&shark->v4l2_dev, "couldn't register v4l2_device\n");
344 		goto err_reg_dev;
345 	}
346 
347 	shark->usbdev = interface_to_usbdev(intf);
348 	shark->tea.v4l2_dev = &shark->v4l2_dev;
349 	shark->tea.private_data = shark;
350 	shark->tea.radio_nr = -1;
351 	shark->tea.ops = &shark_tea_ops;
352 	shark->tea.cannot_mute = true;
353 	shark->tea.has_am = true;
354 	strlcpy(shark->tea.card, "Griffin radioSHARK",
355 		sizeof(shark->tea.card));
356 	usb_make_path(shark->usbdev, shark->tea.bus_info,
357 		sizeof(shark->tea.bus_info));
358 
359 	retval = snd_tea575x_init(&shark->tea, THIS_MODULE);
360 	if (retval) {
361 		v4l2_err(&shark->v4l2_dev, "couldn't init tea5757\n");
362 		goto err_init_tea;
363 	}
364 
365 	return 0;
366 
367 err_init_tea:
368 	v4l2_device_unregister(&shark->v4l2_dev);
369 err_reg_dev:
370 	shark_unregister_leds(shark);
371 err_reg_leds:
372 	kfree(shark->transfer_buffer);
373 err_alloc_buffer:
374 	kfree(shark);
375 
376 	return retval;
377 }
378 
379 #ifdef CONFIG_PM
380 static int usb_shark_suspend(struct usb_interface *intf, pm_message_t message)
381 {
382 	return 0;
383 }
384 
385 static int usb_shark_resume(struct usb_interface *intf)
386 {
387 	struct v4l2_device *v4l2_dev = usb_get_intfdata(intf);
388 	struct shark_device *shark = v4l2_dev_to_shark(v4l2_dev);
389 
390 	mutex_lock(&shark->tea.mutex);
391 	snd_tea575x_set_freq(&shark->tea);
392 	mutex_unlock(&shark->tea.mutex);
393 
394 	shark_resume_leds(shark);
395 
396 	return 0;
397 }
398 #endif
399 
400 /* Specify the bcdDevice value, as the radioSHARK and radioSHARK2 share ids */
401 static struct usb_device_id usb_shark_device_table[] = {
402 	{ .match_flags = USB_DEVICE_ID_MATCH_DEVICE_AND_VERSION |
403 			 USB_DEVICE_ID_MATCH_INT_CLASS,
404 	  .idVendor     = 0x077d,
405 	  .idProduct    = 0x627a,
406 	  .bcdDevice_lo = 0x0001,
407 	  .bcdDevice_hi = 0x0001,
408 	  .bInterfaceClass = 3,
409 	},
410 	{ }
411 };
412 MODULE_DEVICE_TABLE(usb, usb_shark_device_table);
413 
414 static struct usb_driver usb_shark_driver = {
415 	.name			= DRV_NAME,
416 	.probe			= usb_shark_probe,
417 	.disconnect		= usb_shark_disconnect,
418 	.id_table		= usb_shark_device_table,
419 #ifdef CONFIG_PM
420 	.suspend		= usb_shark_suspend,
421 	.resume			= usb_shark_resume,
422 	.reset_resume		= usb_shark_resume,
423 #endif
424 };
425 module_usb_driver(usb_shark_driver);
426