[media] dsbr100: convert to unlocked_ioctl
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / drivers / media / radio / dsbr100.c
1 /* A driver for the D-Link DSB-R100 USB radio and Gemtek USB Radio 21.
2 The device plugs into both the USB and an analog audio input, so this thing
3 only deals with initialisation and frequency setting, the
4 audio data has to be handled by a sound driver.
5
6 Major issue: I can't find out where the device reports the signal
7 strength, and indeed the windows software appearantly just looks
8 at the stereo indicator as well. So, scanning will only find
9 stereo stations. Sad, but I can't help it.
10
11 Also, the windows program sends oodles of messages over to the
12 device, and I couldn't figure out their meaning. My suspicion
13 is that they don't have any:-)
14
15 You might find some interesting stuff about this module at
16 http://unimut.fsk.uni-heidelberg.de/unimut/demi/dsbr
17
18 Copyright (c) 2000 Markus Demleitner <msdemlei@cl.uni-heidelberg.de>
19
20 This program is free software; you can redistribute it and/or modify
21 it under the terms of the GNU General Public License as published by
22 the Free Software Foundation; either version 2 of the License, or
23 (at your option) any later version.
24
25 This program is distributed in the hope that it will be useful,
26 but WITHOUT ANY WARRANTY; without even the implied warranty of
27 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
28 GNU General Public License for more details.
29
30 You should have received a copy of the GNU General Public License
31 along with this program; if not, write to the Free Software
32 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
33
34 History:
35
36 Version 0.46:
37 Removed usb_dsbr100_open/close calls and radio->users counter. Also,
38 radio->muted changed to radio->status and suspend/resume calls updated.
39
40 Version 0.45:
41 Converted to v4l2_device.
42
43 Version 0.44:
44 Add suspend/resume functions, fix unplug of device,
45 a lot of cleanups and fixes by Alexey Klimov <klimov.linux@gmail.com>
46
47 Version 0.43:
48 Oliver Neukum: avoided DMA coherency issue
49
50 Version 0.42:
51 Converted dsbr100 to use video_ioctl2
52 by Douglas Landgraf <dougsland@gmail.com>
53
54 Version 0.41-ac1:
55 Alan Cox: Some cleanups and fixes
56
57 Version 0.41:
58 Converted to V4L2 API by Mauro Carvalho Chehab <mchehab@infradead.org>
59
60 Version 0.40:
61 Markus: Updates for 2.6.x kernels, code layout changes, name sanitizing
62
63 Version 0.30:
64 Markus: Updates for 2.5.x kernel and more ISO compliant source
65
66 Version 0.25:
67 PSL and Markus: Cleanup, radio now doesn't stop on device close
68
69 Version 0.24:
70 Markus: Hope I got these silly VIDEO_TUNER_LOW issues finally
71 right. Some minor cleanup, improved standalone compilation
72
73 Version 0.23:
74 Markus: Sign extension bug fixed by declaring transfer_buffer unsigned
75
76 Version 0.22:
77 Markus: Some (brown bag) cleanup in what VIDIOCSTUNER returns,
78 thanks to Mike Cox for pointing the problem out.
79
80 Version 0.21:
81 Markus: Minor cleanup, warnings if something goes wrong, lame attempt
82 to adhere to Documentation/CodingStyle
83
84 Version 0.2:
85 Brad Hards <bradh@dynamite.com.au>: Fixes to make it work as non-module
86 Markus: Copyright clarification
87
88 Version 0.01: Markus: initial release
89
90 */
91
92 #include <linux/kernel.h>
93 #include <linux/module.h>
94 #include <linux/init.h>
95 #include <linux/slab.h>
96 #include <linux/input.h>
97 #include <linux/videodev2.h>
98 #include <media/v4l2-device.h>
99 #include <media/v4l2-ioctl.h>
100 #include <linux/usb.h>
101
102 /*
103 * Version Information
104 */
105 #include <linux/version.h> /* for KERNEL_VERSION MACRO */
106
107 #define DRIVER_VERSION "v0.46"
108 #define RADIO_VERSION KERNEL_VERSION(0, 4, 6)
109
110 #define DRIVER_AUTHOR "Markus Demleitner <msdemlei@tucana.harvard.edu>"
111 #define DRIVER_DESC "D-Link DSB-R100 USB FM radio driver"
112
113 #define DSB100_VENDOR 0x04b4
114 #define DSB100_PRODUCT 0x1002
115
116 /* Commands the device appears to understand */
117 #define DSB100_TUNE 1
118 #define DSB100_ONOFF 2
119
120 #define TB_LEN 16
121
122 /* Frequency limits in MHz -- these are European values. For Japanese
123 devices, that would be 76 and 91. */
124 #define FREQ_MIN 87.5
125 #define FREQ_MAX 108.0
126 #define FREQ_MUL 16000
127
128 /* defines for radio->status */
129 #define STARTED 0
130 #define STOPPED 1
131
132 #define videodev_to_radio(d) container_of(d, struct dsbr100_device, videodev)
133
134 static int usb_dsbr100_probe(struct usb_interface *intf,
135 const struct usb_device_id *id);
136 static void usb_dsbr100_disconnect(struct usb_interface *intf);
137 static int usb_dsbr100_suspend(struct usb_interface *intf,
138 pm_message_t message);
139 static int usb_dsbr100_resume(struct usb_interface *intf);
140
141 static int radio_nr = -1;
142 module_param(radio_nr, int, 0);
143
144 /* Data for one (physical) device */
145 struct dsbr100_device {
146 struct usb_device *usbdev;
147 struct video_device videodev;
148 struct v4l2_device v4l2_dev;
149
150 u8 *transfer_buffer;
151 struct mutex v4l2_lock;
152 int curfreq;
153 int stereo;
154 int removed;
155 int status;
156 };
157
158 static struct usb_device_id usb_dsbr100_device_table [] = {
159 { USB_DEVICE(DSB100_VENDOR, DSB100_PRODUCT) },
160 { } /* Terminating entry */
161 };
162
163 MODULE_DEVICE_TABLE (usb, usb_dsbr100_device_table);
164
165 /* USB subsystem interface */
166 static struct usb_driver usb_dsbr100_driver = {
167 .name = "dsbr100",
168 .probe = usb_dsbr100_probe,
169 .disconnect = usb_dsbr100_disconnect,
170 .id_table = usb_dsbr100_device_table,
171 .suspend = usb_dsbr100_suspend,
172 .resume = usb_dsbr100_resume,
173 .reset_resume = usb_dsbr100_resume,
174 .supports_autosuspend = 0,
175 };
176
177 /* Low-level device interface begins here */
178
179 /* switch on radio */
180 static int dsbr100_start(struct dsbr100_device *radio)
181 {
182 int retval;
183 int request;
184
185 retval = usb_control_msg(radio->usbdev,
186 usb_rcvctrlpipe(radio->usbdev, 0),
187 USB_REQ_GET_STATUS,
188 USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN,
189 0x00, 0xC7, radio->transfer_buffer, 8, 300);
190
191 if (retval < 0) {
192 request = USB_REQ_GET_STATUS;
193 goto usb_control_msg_failed;
194 }
195
196 retval = usb_control_msg(radio->usbdev,
197 usb_rcvctrlpipe(radio->usbdev, 0),
198 DSB100_ONOFF,
199 USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN,
200 0x01, 0x00, radio->transfer_buffer, 8, 300);
201
202 if (retval < 0) {
203 request = DSB100_ONOFF;
204 goto usb_control_msg_failed;
205 }
206
207 radio->status = STARTED;
208 return (radio->transfer_buffer)[0];
209
210 usb_control_msg_failed:
211 dev_err(&radio->usbdev->dev,
212 "%s - usb_control_msg returned %i, request %i\n",
213 __func__, retval, request);
214 return retval;
215
216 }
217
218 /* switch off radio */
219 static int dsbr100_stop(struct dsbr100_device *radio)
220 {
221 int retval;
222 int request;
223
224 retval = usb_control_msg(radio->usbdev,
225 usb_rcvctrlpipe(radio->usbdev, 0),
226 USB_REQ_GET_STATUS,
227 USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN,
228 0x16, 0x1C, radio->transfer_buffer, 8, 300);
229
230 if (retval < 0) {
231 request = USB_REQ_GET_STATUS;
232 goto usb_control_msg_failed;
233 }
234
235 retval = usb_control_msg(radio->usbdev,
236 usb_rcvctrlpipe(radio->usbdev, 0),
237 DSB100_ONOFF,
238 USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN,
239 0x00, 0x00, radio->transfer_buffer, 8, 300);
240
241 if (retval < 0) {
242 request = DSB100_ONOFF;
243 goto usb_control_msg_failed;
244 }
245
246 radio->status = STOPPED;
247 return (radio->transfer_buffer)[0];
248
249 usb_control_msg_failed:
250 dev_err(&radio->usbdev->dev,
251 "%s - usb_control_msg returned %i, request %i\n",
252 __func__, retval, request);
253 return retval;
254
255 }
256
257 /* set a frequency, freq is defined by v4l's TUNER_LOW, i.e. 1/16th kHz */
258 static int dsbr100_setfreq(struct dsbr100_device *radio)
259 {
260 int retval;
261 int request;
262 int freq = (radio->curfreq / 16 * 80) / 1000 + 856;
263
264 retval = usb_control_msg(radio->usbdev,
265 usb_rcvctrlpipe(radio->usbdev, 0),
266 DSB100_TUNE,
267 USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN,
268 (freq >> 8) & 0x00ff, freq & 0xff,
269 radio->transfer_buffer, 8, 300);
270
271 if (retval < 0) {
272 request = DSB100_TUNE;
273 goto usb_control_msg_failed;
274 }
275
276 retval = usb_control_msg(radio->usbdev,
277 usb_rcvctrlpipe(radio->usbdev, 0),
278 USB_REQ_GET_STATUS,
279 USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN,
280 0x96, 0xB7, radio->transfer_buffer, 8, 300);
281
282 if (retval < 0) {
283 request = USB_REQ_GET_STATUS;
284 goto usb_control_msg_failed;
285 }
286
287 retval = usb_control_msg(radio->usbdev,
288 usb_rcvctrlpipe(radio->usbdev, 0),
289 USB_REQ_GET_STATUS,
290 USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN,
291 0x00, 0x24, radio->transfer_buffer, 8, 300);
292
293 if (retval < 0) {
294 request = USB_REQ_GET_STATUS;
295 goto usb_control_msg_failed;
296 }
297
298 radio->stereo = !((radio->transfer_buffer)[0] & 0x01);
299 return (radio->transfer_buffer)[0];
300
301 usb_control_msg_failed:
302 radio->stereo = -1;
303 dev_err(&radio->usbdev->dev,
304 "%s - usb_control_msg returned %i, request %i\n",
305 __func__, retval, request);
306 return retval;
307 }
308
309 /* return the device status. This is, in effect, just whether it
310 sees a stereo signal or not. Pity. */
311 static void dsbr100_getstat(struct dsbr100_device *radio)
312 {
313 int retval;
314
315 retval = usb_control_msg(radio->usbdev,
316 usb_rcvctrlpipe(radio->usbdev, 0),
317 USB_REQ_GET_STATUS,
318 USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN,
319 0x00 , 0x24, radio->transfer_buffer, 8, 300);
320
321 if (retval < 0) {
322 radio->stereo = -1;
323 dev_err(&radio->usbdev->dev,
324 "%s - usb_control_msg returned %i, request %i\n",
325 __func__, retval, USB_REQ_GET_STATUS);
326 } else {
327 radio->stereo = !(radio->transfer_buffer[0] & 0x01);
328 }
329 }
330
331 static int vidioc_querycap(struct file *file, void *priv,
332 struct v4l2_capability *v)
333 {
334 struct dsbr100_device *radio = video_drvdata(file);
335
336 strlcpy(v->driver, "dsbr100", sizeof(v->driver));
337 strlcpy(v->card, "D-Link R-100 USB FM Radio", sizeof(v->card));
338 usb_make_path(radio->usbdev, v->bus_info, sizeof(v->bus_info));
339 v->version = RADIO_VERSION;
340 v->capabilities = V4L2_CAP_TUNER;
341 return 0;
342 }
343
344 static int vidioc_g_tuner(struct file *file, void *priv,
345 struct v4l2_tuner *v)
346 {
347 struct dsbr100_device *radio = video_drvdata(file);
348
349 /* safety check */
350 if (radio->removed)
351 return -EIO;
352
353 if (v->index > 0)
354 return -EINVAL;
355
356 dsbr100_getstat(radio);
357 strcpy(v->name, "FM");
358 v->type = V4L2_TUNER_RADIO;
359 v->rangelow = FREQ_MIN * FREQ_MUL;
360 v->rangehigh = FREQ_MAX * FREQ_MUL;
361 v->rxsubchans = V4L2_TUNER_SUB_MONO | V4L2_TUNER_SUB_STEREO;
362 v->capability = V4L2_TUNER_CAP_LOW;
363 if(radio->stereo)
364 v->audmode = V4L2_TUNER_MODE_STEREO;
365 else
366 v->audmode = V4L2_TUNER_MODE_MONO;
367 v->signal = 0xffff; /* We can't get the signal strength */
368 return 0;
369 }
370
371 static int vidioc_s_tuner(struct file *file, void *priv,
372 struct v4l2_tuner *v)
373 {
374 struct dsbr100_device *radio = video_drvdata(file);
375
376 /* safety check */
377 if (radio->removed)
378 return -EIO;
379
380 if (v->index > 0)
381 return -EINVAL;
382
383 return 0;
384 }
385
386 static int vidioc_s_frequency(struct file *file, void *priv,
387 struct v4l2_frequency *f)
388 {
389 struct dsbr100_device *radio = video_drvdata(file);
390 int retval;
391
392 /* safety check */
393 if (radio->removed)
394 return -EIO;
395
396 radio->curfreq = f->frequency;
397
398 retval = dsbr100_setfreq(radio);
399 if (retval < 0)
400 dev_warn(&radio->usbdev->dev, "Set frequency failed\n");
401 return 0;
402 }
403
404 static int vidioc_g_frequency(struct file *file, void *priv,
405 struct v4l2_frequency *f)
406 {
407 struct dsbr100_device *radio = video_drvdata(file);
408
409 /* safety check */
410 if (radio->removed)
411 return -EIO;
412
413 f->type = V4L2_TUNER_RADIO;
414 f->frequency = radio->curfreq;
415 return 0;
416 }
417
418 static int vidioc_queryctrl(struct file *file, void *priv,
419 struct v4l2_queryctrl *qc)
420 {
421 switch (qc->id) {
422 case V4L2_CID_AUDIO_MUTE:
423 return v4l2_ctrl_query_fill(qc, 0, 1, 1, 1);
424 }
425
426 return -EINVAL;
427 }
428
429 static int vidioc_g_ctrl(struct file *file, void *priv,
430 struct v4l2_control *ctrl)
431 {
432 struct dsbr100_device *radio = video_drvdata(file);
433
434 /* safety check */
435 if (radio->removed)
436 return -EIO;
437
438 switch (ctrl->id) {
439 case V4L2_CID_AUDIO_MUTE:
440 ctrl->value = radio->status;
441 return 0;
442 }
443 return -EINVAL;
444 }
445
446 static int vidioc_s_ctrl(struct file *file, void *priv,
447 struct v4l2_control *ctrl)
448 {
449 struct dsbr100_device *radio = video_drvdata(file);
450 int retval;
451
452 /* safety check */
453 if (radio->removed)
454 return -EIO;
455
456 switch (ctrl->id) {
457 case V4L2_CID_AUDIO_MUTE:
458 if (ctrl->value) {
459 retval = dsbr100_stop(radio);
460 if (retval < 0) {
461 dev_warn(&radio->usbdev->dev,
462 "Radio did not respond properly\n");
463 return -EBUSY;
464 }
465 } else {
466 retval = dsbr100_start(radio);
467 if (retval < 0) {
468 dev_warn(&radio->usbdev->dev,
469 "Radio did not respond properly\n");
470 return -EBUSY;
471 }
472 }
473 return 0;
474 }
475 return -EINVAL;
476 }
477
478 static int vidioc_g_audio(struct file *file, void *priv,
479 struct v4l2_audio *a)
480 {
481 if (a->index > 1)
482 return -EINVAL;
483
484 strcpy(a->name, "Radio");
485 a->capability = V4L2_AUDCAP_STEREO;
486 return 0;
487 }
488
489 static int vidioc_g_input(struct file *filp, void *priv, unsigned int *i)
490 {
491 *i = 0;
492 return 0;
493 }
494
495 static int vidioc_s_input(struct file *filp, void *priv, unsigned int i)
496 {
497 if (i != 0)
498 return -EINVAL;
499 return 0;
500 }
501
502 static int vidioc_s_audio(struct file *file, void *priv,
503 struct v4l2_audio *a)
504 {
505 if (a->index != 0)
506 return -EINVAL;
507 return 0;
508 }
509
510 /* USB subsystem interface begins here */
511
512 /*
513 * Handle unplugging of the device.
514 * We call video_unregister_device in any case.
515 * The last function called in this procedure is
516 * usb_dsbr100_video_device_release
517 */
518 static void usb_dsbr100_disconnect(struct usb_interface *intf)
519 {
520 struct dsbr100_device *radio = usb_get_intfdata(intf);
521
522 usb_set_intfdata(intf, NULL);
523
524 mutex_lock(&radio->v4l2_lock);
525 radio->removed = 1;
526 mutex_unlock(&radio->v4l2_lock);
527
528 video_unregister_device(&radio->videodev);
529 v4l2_device_disconnect(&radio->v4l2_dev);
530 }
531
532
533 /* Suspend device - stop device. */
534 static int usb_dsbr100_suspend(struct usb_interface *intf, pm_message_t message)
535 {
536 struct dsbr100_device *radio = usb_get_intfdata(intf);
537 int retval;
538
539 mutex_lock(&radio->v4l2_lock);
540 if (radio->status == STARTED) {
541 retval = dsbr100_stop(radio);
542 if (retval < 0)
543 dev_warn(&intf->dev, "dsbr100_stop failed\n");
544
545 /* After dsbr100_stop() status set to STOPPED.
546 * If we want driver to start radio on resume
547 * we set status equal to STARTED.
548 * On resume we will check status and run radio if needed.
549 */
550 radio->status = STARTED;
551 }
552 mutex_unlock(&radio->v4l2_lock);
553
554 dev_info(&intf->dev, "going into suspend..\n");
555
556 return 0;
557 }
558
559 /* Resume device - start device. */
560 static int usb_dsbr100_resume(struct usb_interface *intf)
561 {
562 struct dsbr100_device *radio = usb_get_intfdata(intf);
563 int retval;
564
565 mutex_lock(&radio->v4l2_lock);
566 if (radio->status == STARTED) {
567 retval = dsbr100_start(radio);
568 if (retval < 0)
569 dev_warn(&intf->dev, "dsbr100_start failed\n");
570 }
571 mutex_unlock(&radio->v4l2_lock);
572
573 dev_info(&intf->dev, "coming out of suspend..\n");
574
575 return 0;
576 }
577
578 /* free data structures */
579 static void usb_dsbr100_video_device_release(struct video_device *videodev)
580 {
581 struct dsbr100_device *radio = videodev_to_radio(videodev);
582
583 v4l2_device_unregister(&radio->v4l2_dev);
584 kfree(radio->transfer_buffer);
585 kfree(radio);
586 }
587
588 /* File system interface */
589 static const struct v4l2_file_operations usb_dsbr100_fops = {
590 .owner = THIS_MODULE,
591 .unlocked_ioctl = video_ioctl2,
592 };
593
594 static const struct v4l2_ioctl_ops usb_dsbr100_ioctl_ops = {
595 .vidioc_querycap = vidioc_querycap,
596 .vidioc_g_tuner = vidioc_g_tuner,
597 .vidioc_s_tuner = vidioc_s_tuner,
598 .vidioc_g_frequency = vidioc_g_frequency,
599 .vidioc_s_frequency = vidioc_s_frequency,
600 .vidioc_queryctrl = vidioc_queryctrl,
601 .vidioc_g_ctrl = vidioc_g_ctrl,
602 .vidioc_s_ctrl = vidioc_s_ctrl,
603 .vidioc_g_audio = vidioc_g_audio,
604 .vidioc_s_audio = vidioc_s_audio,
605 .vidioc_g_input = vidioc_g_input,
606 .vidioc_s_input = vidioc_s_input,
607 };
608
609 /* check if the device is present and register with v4l and usb if it is */
610 static int usb_dsbr100_probe(struct usb_interface *intf,
611 const struct usb_device_id *id)
612 {
613 struct dsbr100_device *radio;
614 struct v4l2_device *v4l2_dev;
615 int retval;
616
617 radio = kzalloc(sizeof(struct dsbr100_device), GFP_KERNEL);
618
619 if (!radio)
620 return -ENOMEM;
621
622 radio->transfer_buffer = kmalloc(TB_LEN, GFP_KERNEL);
623
624 if (!(radio->transfer_buffer)) {
625 kfree(radio);
626 return -ENOMEM;
627 }
628
629 v4l2_dev = &radio->v4l2_dev;
630
631 retval = v4l2_device_register(&intf->dev, v4l2_dev);
632 if (retval < 0) {
633 v4l2_err(v4l2_dev, "couldn't register v4l2_device\n");
634 kfree(radio->transfer_buffer);
635 kfree(radio);
636 return retval;
637 }
638
639 mutex_init(&radio->v4l2_lock);
640 strlcpy(radio->videodev.name, v4l2_dev->name, sizeof(radio->videodev.name));
641 radio->videodev.v4l2_dev = v4l2_dev;
642 radio->videodev.fops = &usb_dsbr100_fops;
643 radio->videodev.ioctl_ops = &usb_dsbr100_ioctl_ops;
644 radio->videodev.release = usb_dsbr100_video_device_release;
645 radio->videodev.lock = &radio->v4l2_lock;
646
647 radio->removed = 0;
648 radio->usbdev = interface_to_usbdev(intf);
649 radio->curfreq = FREQ_MIN * FREQ_MUL;
650 radio->status = STOPPED;
651
652 video_set_drvdata(&radio->videodev, radio);
653
654 retval = video_register_device(&radio->videodev, VFL_TYPE_RADIO, radio_nr);
655 if (retval < 0) {
656 v4l2_err(v4l2_dev, "couldn't register video device\n");
657 v4l2_device_unregister(v4l2_dev);
658 kfree(radio->transfer_buffer);
659 kfree(radio);
660 return -EIO;
661 }
662 usb_set_intfdata(intf, radio);
663 return 0;
664 }
665
666 static int __init dsbr100_init(void)
667 {
668 int retval = usb_register(&usb_dsbr100_driver);
669 printk(KERN_INFO KBUILD_MODNAME ": " DRIVER_VERSION ":"
670 DRIVER_DESC "\n");
671 return retval;
672 }
673
674 static void __exit dsbr100_exit(void)
675 {
676 usb_deregister(&usb_dsbr100_driver);
677 }
678
679 module_init (dsbr100_init);
680 module_exit (dsbr100_exit);
681
682 MODULE_AUTHOR( DRIVER_AUTHOR );
683 MODULE_DESCRIPTION( DRIVER_DESC );
684 MODULE_LICENSE("GPL");