aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/media/radio/radio-shark2.c
diff options
context:
space:
mode:
Diffstat (limited to 'drivers/media/radio/radio-shark2.c')
-rw-r--r--drivers/media/radio/radio-shark2.c137
1 files changed, 72 insertions, 65 deletions
diff --git a/drivers/media/radio/radio-shark2.c b/drivers/media/radio/radio-shark2.c
index b9575de3e7e8..7b4efdfaae28 100644
--- a/drivers/media/radio/radio-shark2.c
+++ b/drivers/media/radio/radio-shark2.c
@@ -35,6 +35,11 @@
35#include <media/v4l2-device.h> 35#include <media/v4l2-device.h>
36#include "radio-tea5777.h" 36#include "radio-tea5777.h"
37 37
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
38MODULE_AUTHOR("Hans de Goede <hdegoede@redhat.com>"); 43MODULE_AUTHOR("Hans de Goede <hdegoede@redhat.com>");
39MODULE_DESCRIPTION("Griffin radioSHARK2, USB radio receiver driver"); 44MODULE_DESCRIPTION("Griffin radioSHARK2, USB radio receiver driver");
40MODULE_LICENSE("GPL"); 45MODULE_LICENSE("GPL");
@@ -43,7 +48,6 @@ static int debug;
43module_param(debug, int, 0); 48module_param(debug, int, 0);
44MODULE_PARM_DESC(debug, "Debug level (0-1)"); 49MODULE_PARM_DESC(debug, "Debug level (0-1)");
45 50
46
47#define SHARK_IN_EP 0x83 51#define SHARK_IN_EP 0x83
48#define SHARK_OUT_EP 0x05 52#define SHARK_OUT_EP 0x05
49 53
@@ -54,36 +58,18 @@ MODULE_PARM_DESC(debug, "Debug level (0-1)");
54 58
55enum { BLUE_LED, RED_LED, NO_LEDS }; 59enum { BLUE_LED, RED_LED, NO_LEDS };
56 60
57static void shark_led_set_blue(struct led_classdev *led_cdev,
58 enum led_brightness value);
59static void shark_led_set_red(struct led_classdev *led_cdev,
60 enum led_brightness value);
61
62static const struct led_classdev shark_led_templates[NO_LEDS] = {
63 [BLUE_LED] = {
64 .name = "%s:blue:",
65 .brightness = LED_OFF,
66 .max_brightness = 127,
67 .brightness_set = shark_led_set_blue,
68 },
69 [RED_LED] = {
70 .name = "%s:red:",
71 .brightness = LED_OFF,
72 .max_brightness = 1,
73 .brightness_set = shark_led_set_red,
74 },
75};
76
77struct shark_device { 61struct shark_device {
78 struct usb_device *usbdev; 62 struct usb_device *usbdev;
79 struct v4l2_device v4l2_dev; 63 struct v4l2_device v4l2_dev;
80 struct radio_tea5777 tea; 64 struct radio_tea5777 tea;
81 65
66#ifdef SHARK_USE_LEDS
82 struct work_struct led_work; 67 struct work_struct led_work;
83 struct led_classdev leds[NO_LEDS]; 68 struct led_classdev leds[NO_LEDS];
84 char led_names[NO_LEDS][32]; 69 char led_names[NO_LEDS][32];
85 atomic_t brightness[NO_LEDS]; 70 atomic_t brightness[NO_LEDS];
86 unsigned long brightness_new; 71 unsigned long brightness_new;
72#endif
87 73
88 u8 *transfer_buffer; 74 u8 *transfer_buffer;
89}; 75};
@@ -161,18 +147,12 @@ static struct radio_tea5777_ops shark_tea_ops = {
161 .read_reg = shark_read_reg, 147 .read_reg = shark_read_reg,
162}; 148};
163 149
150#ifdef SHARK_USE_LEDS
164static void shark_led_work(struct work_struct *work) 151static void shark_led_work(struct work_struct *work)
165{ 152{
166 struct shark_device *shark = 153 struct shark_device *shark =
167 container_of(work, struct shark_device, led_work); 154 container_of(work, struct shark_device, led_work);
168 int i, res, brightness, actual_len; 155 int i, res, brightness, actual_len;
169 /*
170 * We use the v4l2_dev lock and registered bit to ensure the device
171 * does not get unplugged and unreffed while we're running.
172 */
173 mutex_lock(&shark->tea.mutex);
174 if (!video_is_registered(&shark->tea.vd))
175 goto leave;
176 156
177 for (i = 0; i < 2; i++) { 157 for (i = 0; i < 2; i++) {
178 if (!test_and_clear_bit(i, &shark->brightness_new)) 158 if (!test_and_clear_bit(i, &shark->brightness_new))
@@ -191,8 +171,6 @@ static void shark_led_work(struct work_struct *work)
191 v4l2_err(&shark->v4l2_dev, "set LED %s error: %d\n", 171 v4l2_err(&shark->v4l2_dev, "set LED %s error: %d\n",
192 shark->led_names[i], res); 172 shark->led_names[i], res);
193 } 173 }
194leave:
195 mutex_unlock(&shark->tea.mutex);
196} 174}
197 175
198static void shark_led_set_blue(struct led_classdev *led_cdev, 176static void shark_led_set_blue(struct led_classdev *led_cdev,
@@ -217,19 +195,72 @@ static void shark_led_set_red(struct led_classdev *led_cdev,
217 schedule_work(&shark->led_work); 195 schedule_work(&shark->led_work);
218} 196}
219 197
198static 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
213static 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
234static 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
244static 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}
250static inline void shark_unregister_leds(struct shark_device *shark) { }
251#endif
252
220static void usb_shark_disconnect(struct usb_interface *intf) 253static void usb_shark_disconnect(struct usb_interface *intf)
221{ 254{
222 struct v4l2_device *v4l2_dev = usb_get_intfdata(intf); 255 struct v4l2_device *v4l2_dev = usb_get_intfdata(intf);
223 struct shark_device *shark = v4l2_dev_to_shark(v4l2_dev); 256 struct shark_device *shark = v4l2_dev_to_shark(v4l2_dev);
224 int i;
225 257
226 mutex_lock(&shark->tea.mutex); 258 mutex_lock(&shark->tea.mutex);
227 v4l2_device_disconnect(&shark->v4l2_dev); 259 v4l2_device_disconnect(&shark->v4l2_dev);
228 radio_tea5777_exit(&shark->tea); 260 radio_tea5777_exit(&shark->tea);
229 mutex_unlock(&shark->tea.mutex); 261 mutex_unlock(&shark->tea.mutex);
230 262
231 for (i = 0; i < NO_LEDS; i++) 263 shark_unregister_leds(shark);
232 led_classdev_unregister(&shark->leds[i]);
233 264
234 v4l2_device_put(&shark->v4l2_dev); 265 v4l2_device_put(&shark->v4l2_dev);
235} 266}
@@ -238,7 +269,6 @@ static void usb_shark_release(struct v4l2_device *v4l2_dev)
238{ 269{
239 struct shark_device *shark = v4l2_dev_to_shark(v4l2_dev); 270 struct shark_device *shark = v4l2_dev_to_shark(v4l2_dev);
240 271
241 cancel_work_sync(&shark->led_work);
242 v4l2_device_unregister(&shark->v4l2_dev); 272 v4l2_device_unregister(&shark->v4l2_dev);
243 kfree(shark->transfer_buffer); 273 kfree(shark->transfer_buffer);
244 kfree(shark); 274 kfree(shark);
@@ -248,7 +278,7 @@ static int usb_shark_probe(struct usb_interface *intf,
248 const struct usb_device_id *id) 278 const struct usb_device_id *id)
249{ 279{
250 struct shark_device *shark; 280 struct shark_device *shark;
251 int i, retval = -ENOMEM; 281 int retval = -ENOMEM;
252 282
253 shark = kzalloc(sizeof(struct shark_device), GFP_KERNEL); 283 shark = kzalloc(sizeof(struct shark_device), GFP_KERNEL);
254 if (!shark) 284 if (!shark)
@@ -258,17 +288,13 @@ static int usb_shark_probe(struct usb_interface *intf,
258 if (!shark->transfer_buffer) 288 if (!shark->transfer_buffer)
259 goto err_alloc_buffer; 289 goto err_alloc_buffer;
260 290
261 /* 291 v4l2_device_set_name(&shark->v4l2_dev, DRV_NAME, &shark_instance);
262 * Work around a bug in usbhid/hid-core.c, where it leaves a dangling 292
263 * pointer in intfdata causing v4l2-device.c to not set it. Which 293 retval = shark_register_leds(shark, &intf->dev);
264 * results in usb_shark_disconnect() referencing the dangling pointer 294 if (retval)
265 * 295 goto err_reg_leds;
266 * REMOVE (as soon as the above bug is fixed, patch submitted)
267 */
268 usb_set_intfdata(intf, NULL);
269 296
270 shark->v4l2_dev.release = usb_shark_release; 297 shark->v4l2_dev.release = usb_shark_release;
271 v4l2_device_set_name(&shark->v4l2_dev, DRV_NAME, &shark_instance);
272 retval = v4l2_device_register(&intf->dev, &shark->v4l2_dev); 298 retval = v4l2_device_register(&intf->dev, &shark->v4l2_dev);
273 if (retval) { 299 if (retval) {
274 v4l2_err(&shark->v4l2_dev, "couldn't register v4l2_device\n"); 300 v4l2_err(&shark->v4l2_dev, "couldn't register v4l2_device\n");
@@ -292,32 +318,13 @@ static int usb_shark_probe(struct usb_interface *intf,
292 goto err_init_tea; 318 goto err_init_tea;
293 } 319 }
294 320
295 INIT_WORK(&shark->led_work, shark_led_work);
296 for (i = 0; i < NO_LEDS; i++) {
297 shark->leds[i] = shark_led_templates[i];
298 snprintf(shark->led_names[i], sizeof(shark->led_names[0]),
299 shark->leds[i].name, shark->v4l2_dev.name);
300 shark->leds[i].name = shark->led_names[i];
301 /*
302 * We don't fail the probe if we fail to register the leds,
303 * because once we've called radio_tea5777_init, the /dev/radio0
304 * node may be opened from userspace holding a reference to us!
305 *
306 * Note we cannot register the leds first instead as
307 * shark_led_work depends on the v4l2 mutex and registered bit.
308 */
309 retval = led_classdev_register(&intf->dev, &shark->leds[i]);
310 if (retval)
311 v4l2_err(&shark->v4l2_dev,
312 "couldn't register led: %s\n",
313 shark->led_names[i]);
314 }
315
316 return 0; 321 return 0;
317 322
318err_init_tea: 323err_init_tea:
319 v4l2_device_unregister(&shark->v4l2_dev); 324 v4l2_device_unregister(&shark->v4l2_dev);
320err_reg_dev: 325err_reg_dev:
326 shark_unregister_leds(shark);
327err_reg_leds:
321 kfree(shark->transfer_buffer); 328 kfree(shark->transfer_buffer);
322err_alloc_buffer: 329err_alloc_buffer:
323 kfree(shark); 330 kfree(shark);