PM / sleep: Asynchronous threads for suspend_noirq
[linux/fpc-iii.git] / drivers / media / radio / radio-shark2.c
blob8654e0dc5c95376aa7140498e10af342b955d15a
1 /*
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
9 * Domain.
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
41 #endif
43 MODULE_AUTHOR("Hans de Goede <hdegoede@redhat.com>");
44 MODULE_DESCRIPTION("Griffin radioSHARK2, USB radio receiver driver");
45 MODULE_LICENSE("GPL");
47 static int debug;
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
54 #define TB_LEN 7
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 };
61 struct shark_device {
62 struct usb_device *usbdev;
63 struct v4l2_device v4l2_dev;
64 struct radio_tea5777 tea;
66 #ifdef SHARK_USE_LEDS
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;
72 #endif
74 u8 *transfer_buffer;
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, "shark2-write: %*ph\n",
90 7, shark->transfer_buffer);
92 res = usb_interrupt_msg(shark->usbdev,
93 usb_sndintpipe(shark->usbdev, SHARK_OUT_EP),
94 shark->transfer_buffer, TB_LEN,
95 &actual_len, 1000);
96 if (res < 0) {
97 v4l2_err(tea->v4l2_dev, "write error: %d\n", res);
98 return res;
101 return 0;
104 static int shark_read_reg(struct radio_tea5777 *tea, u32 *reg_ret)
106 struct shark_device *shark = tea->private_data;
107 int i, res, actual_len;
108 u32 reg = 0;
110 memset(shark->transfer_buffer, 0, TB_LEN);
111 shark->transfer_buffer[0] = 0x82;
112 res = usb_interrupt_msg(shark->usbdev,
113 usb_sndintpipe(shark->usbdev, SHARK_OUT_EP),
114 shark->transfer_buffer, TB_LEN,
115 &actual_len, 1000);
116 if (res < 0) {
117 v4l2_err(tea->v4l2_dev, "request-read error: %d\n", res);
118 return res;
121 res = usb_interrupt_msg(shark->usbdev,
122 usb_rcvintpipe(shark->usbdev, SHARK_IN_EP),
123 shark->transfer_buffer, TB_LEN,
124 &actual_len, 1000);
125 if (res < 0) {
126 v4l2_err(tea->v4l2_dev, "read error: %d\n", res);
127 return res;
130 for (i = 0; i < 3; i++)
131 reg |= shark->transfer_buffer[i] << (16 - i * 8);
133 v4l2_dbg(1, debug, tea->v4l2_dev, "shark2-read: %*ph\n",
134 3, shark->transfer_buffer);
136 *reg_ret = reg;
137 return 0;
140 static struct radio_tea5777_ops shark_tea_ops = {
141 .write_reg = shark_write_reg,
142 .read_reg = shark_read_reg,
145 #ifdef SHARK_USE_LEDS
146 static void shark_led_work(struct work_struct *work)
148 struct shark_device *shark =
149 container_of(work, struct shark_device, led_work);
150 int i, res, brightness, actual_len;
152 for (i = 0; i < 2; i++) {
153 if (!test_and_clear_bit(i, &shark->brightness_new))
154 continue;
156 brightness = atomic_read(&shark->brightness[i]);
157 memset(shark->transfer_buffer, 0, TB_LEN);
158 shark->transfer_buffer[0] = 0x83 + i;
159 shark->transfer_buffer[1] = brightness;
160 res = usb_interrupt_msg(shark->usbdev,
161 usb_sndintpipe(shark->usbdev,
162 SHARK_OUT_EP),
163 shark->transfer_buffer, TB_LEN,
164 &actual_len, 1000);
165 if (res < 0)
166 v4l2_err(&shark->v4l2_dev, "set LED %s error: %d\n",
167 shark->led_names[i], res);
171 static void shark_led_set_blue(struct led_classdev *led_cdev,
172 enum led_brightness value)
174 struct shark_device *shark =
175 container_of(led_cdev, struct shark_device, leds[BLUE_LED]);
177 atomic_set(&shark->brightness[BLUE_LED], value);
178 set_bit(BLUE_LED, &shark->brightness_new);
179 schedule_work(&shark->led_work);
182 static void shark_led_set_red(struct led_classdev *led_cdev,
183 enum led_brightness value)
185 struct shark_device *shark =
186 container_of(led_cdev, struct shark_device, leds[RED_LED]);
188 atomic_set(&shark->brightness[RED_LED], value);
189 set_bit(RED_LED, &shark->brightness_new);
190 schedule_work(&shark->led_work);
193 static const struct led_classdev shark_led_templates[NO_LEDS] = {
194 [BLUE_LED] = {
195 .name = "%s:blue:",
196 .brightness = LED_OFF,
197 .max_brightness = 127,
198 .brightness_set = shark_led_set_blue,
200 [RED_LED] = {
201 .name = "%s:red:",
202 .brightness = LED_OFF,
203 .max_brightness = 1,
204 .brightness_set = shark_led_set_red,
208 static int shark_register_leds(struct shark_device *shark, struct device *dev)
210 int i, retval;
212 atomic_set(&shark->brightness[BLUE_LED], 127);
213 INIT_WORK(&shark->led_work, shark_led_work);
214 for (i = 0; i < NO_LEDS; i++) {
215 shark->leds[i] = shark_led_templates[i];
216 snprintf(shark->led_names[i], sizeof(shark->led_names[0]),
217 shark->leds[i].name, shark->v4l2_dev.name);
218 shark->leds[i].name = shark->led_names[i];
219 retval = led_classdev_register(dev, &shark->leds[i]);
220 if (retval) {
221 v4l2_err(&shark->v4l2_dev,
222 "couldn't register led: %s\n",
223 shark->led_names[i]);
224 return retval;
227 return 0;
230 static void shark_unregister_leds(struct shark_device *shark)
232 int i;
234 for (i = 0; i < NO_LEDS; i++)
235 led_classdev_unregister(&shark->leds[i]);
237 cancel_work_sync(&shark->led_work);
240 static inline void shark_resume_leds(struct shark_device *shark)
242 int i;
244 for (i = 0; i < NO_LEDS; i++)
245 set_bit(i, &shark->brightness_new);
247 schedule_work(&shark->led_work);
249 #else
250 static int shark_register_leds(struct shark_device *shark, struct device *dev)
252 v4l2_warn(&shark->v4l2_dev,
253 "CONFIG_LEDS_CLASS not enabled, LED support disabled\n");
254 return 0;
256 static inline void shark_unregister_leds(struct shark_device *shark) { }
257 static inline void shark_resume_leds(struct shark_device *shark) { }
258 #endif
260 static void usb_shark_disconnect(struct usb_interface *intf)
262 struct v4l2_device *v4l2_dev = usb_get_intfdata(intf);
263 struct shark_device *shark = v4l2_dev_to_shark(v4l2_dev);
265 mutex_lock(&shark->tea.mutex);
266 v4l2_device_disconnect(&shark->v4l2_dev);
267 radio_tea5777_exit(&shark->tea);
268 mutex_unlock(&shark->tea.mutex);
270 shark_unregister_leds(shark);
272 v4l2_device_put(&shark->v4l2_dev);
275 static void usb_shark_release(struct v4l2_device *v4l2_dev)
277 struct shark_device *shark = v4l2_dev_to_shark(v4l2_dev);
279 v4l2_device_unregister(&shark->v4l2_dev);
280 kfree(shark->transfer_buffer);
281 kfree(shark);
284 static int usb_shark_probe(struct usb_interface *intf,
285 const struct usb_device_id *id)
287 struct shark_device *shark;
288 int retval = -ENOMEM;
290 shark = kzalloc(sizeof(struct shark_device), GFP_KERNEL);
291 if (!shark)
292 return retval;
294 shark->transfer_buffer = kmalloc(TB_LEN, GFP_KERNEL);
295 if (!shark->transfer_buffer)
296 goto err_alloc_buffer;
298 v4l2_device_set_name(&shark->v4l2_dev, DRV_NAME, &shark_instance);
300 retval = shark_register_leds(shark, &intf->dev);
301 if (retval)
302 goto err_reg_leds;
304 shark->v4l2_dev.release = usb_shark_release;
305 retval = v4l2_device_register(&intf->dev, &shark->v4l2_dev);
306 if (retval) {
307 v4l2_err(&shark->v4l2_dev, "couldn't register v4l2_device\n");
308 goto err_reg_dev;
311 shark->usbdev = interface_to_usbdev(intf);
312 shark->tea.v4l2_dev = &shark->v4l2_dev;
313 shark->tea.private_data = shark;
314 shark->tea.ops = &shark_tea_ops;
315 shark->tea.has_am = true;
316 shark->tea.write_before_read = true;
317 strlcpy(shark->tea.card, "Griffin radioSHARK2",
318 sizeof(shark->tea.card));
319 usb_make_path(shark->usbdev, shark->tea.bus_info,
320 sizeof(shark->tea.bus_info));
322 retval = radio_tea5777_init(&shark->tea, THIS_MODULE);
323 if (retval) {
324 v4l2_err(&shark->v4l2_dev, "couldn't init tea5777\n");
325 goto err_init_tea;
328 return 0;
330 err_init_tea:
331 v4l2_device_unregister(&shark->v4l2_dev);
332 err_reg_dev:
333 shark_unregister_leds(shark);
334 err_reg_leds:
335 kfree(shark->transfer_buffer);
336 err_alloc_buffer:
337 kfree(shark);
339 return retval;
342 #ifdef CONFIG_PM
343 static int usb_shark_suspend(struct usb_interface *intf, pm_message_t message)
345 return 0;
348 static int usb_shark_resume(struct usb_interface *intf)
350 struct v4l2_device *v4l2_dev = usb_get_intfdata(intf);
351 struct shark_device *shark = v4l2_dev_to_shark(v4l2_dev);
352 int ret;
354 mutex_lock(&shark->tea.mutex);
355 ret = radio_tea5777_set_freq(&shark->tea);
356 mutex_unlock(&shark->tea.mutex);
358 shark_resume_leds(shark);
360 return ret;
362 #endif
364 /* Specify the bcdDevice value, as the radioSHARK and radioSHARK2 share ids */
365 static struct usb_device_id usb_shark_device_table[] = {
366 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE_AND_VERSION |
367 USB_DEVICE_ID_MATCH_INT_CLASS,
368 .idVendor = 0x077d,
369 .idProduct = 0x627a,
370 .bcdDevice_lo = 0x0010,
371 .bcdDevice_hi = 0x0010,
372 .bInterfaceClass = 3,
376 MODULE_DEVICE_TABLE(usb, usb_shark_device_table);
378 static struct usb_driver usb_shark_driver = {
379 .name = DRV_NAME,
380 .probe = usb_shark_probe,
381 .disconnect = usb_shark_disconnect,
382 .id_table = usb_shark_device_table,
383 #ifdef CONFIG_PM
384 .suspend = usb_shark_suspend,
385 .resume = usb_shark_resume,
386 .reset_resume = usb_shark_resume,
387 #endif
389 module_usb_driver(usb_shark_driver);