V4L/DVB (11937): vino: replace dma_sync_single with dma_sync_single_for_cpu
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / media / radio / dsbr100.c
blob6135762022940ec4095538bfa446803fb2be6373
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.
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.
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:-)
15 You might find some interesting stuff about this module at
16 http://unimut.fsk.uni-heidelberg.de/unimut/demi/dsbr
18 Copyright (c) 2000 Markus Demleitner <msdemlei@cl.uni-heidelberg.de>
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.
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.
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
34 History:
36 Version 0.45:
37 Converted to v4l2_device.
39 Version 0.44:
40 Add suspend/resume functions, fix unplug of device,
41 a lot of cleanups and fixes by Alexey Klimov <klimov.linux@gmail.com>
43 Version 0.43:
44 Oliver Neukum: avoided DMA coherency issue
46 Version 0.42:
47 Converted dsbr100 to use video_ioctl2
48 by Douglas Landgraf <dougsland@gmail.com>
50 Version 0.41-ac1:
51 Alan Cox: Some cleanups and fixes
53 Version 0.41:
54 Converted to V4L2 API by Mauro Carvalho Chehab <mchehab@infradead.org>
56 Version 0.40:
57 Markus: Updates for 2.6.x kernels, code layout changes, name sanitizing
59 Version 0.30:
60 Markus: Updates for 2.5.x kernel and more ISO compliant source
62 Version 0.25:
63 PSL and Markus: Cleanup, radio now doesn't stop on device close
65 Version 0.24:
66 Markus: Hope I got these silly VIDEO_TUNER_LOW issues finally
67 right. Some minor cleanup, improved standalone compilation
69 Version 0.23:
70 Markus: Sign extension bug fixed by declaring transfer_buffer unsigned
72 Version 0.22:
73 Markus: Some (brown bag) cleanup in what VIDIOCSTUNER returns,
74 thanks to Mike Cox for pointing the problem out.
76 Version 0.21:
77 Markus: Minor cleanup, warnings if something goes wrong, lame attempt
78 to adhere to Documentation/CodingStyle
80 Version 0.2:
81 Brad Hards <bradh@dynamite.com.au>: Fixes to make it work as non-module
82 Markus: Copyright clarification
84 Version 0.01: Markus: initial release
88 #include <linux/kernel.h>
89 #include <linux/module.h>
90 #include <linux/init.h>
91 #include <linux/slab.h>
92 #include <linux/input.h>
93 #include <linux/videodev2.h>
94 #include <media/v4l2-device.h>
95 #include <media/v4l2-ioctl.h>
96 #include <linux/usb.h>
99 * Version Information
101 #include <linux/version.h> /* for KERNEL_VERSION MACRO */
103 #define DRIVER_VERSION "v0.45"
104 #define RADIO_VERSION KERNEL_VERSION(0, 4, 5)
106 #define DRIVER_AUTHOR "Markus Demleitner <msdemlei@tucana.harvard.edu>"
107 #define DRIVER_DESC "D-Link DSB-R100 USB FM radio driver"
109 #define DSB100_VENDOR 0x04b4
110 #define DSB100_PRODUCT 0x1002
112 /* Commands the device appears to understand */
113 #define DSB100_TUNE 1
114 #define DSB100_ONOFF 2
116 #define TB_LEN 16
118 /* Frequency limits in MHz -- these are European values. For Japanese
119 devices, that would be 76 and 91. */
120 #define FREQ_MIN 87.5
121 #define FREQ_MAX 108.0
122 #define FREQ_MUL 16000
124 #define videodev_to_radio(d) container_of(d, struct dsbr100_device, videodev)
126 static int usb_dsbr100_probe(struct usb_interface *intf,
127 const struct usb_device_id *id);
128 static void usb_dsbr100_disconnect(struct usb_interface *intf);
129 static int usb_dsbr100_open(struct file *file);
130 static int usb_dsbr100_close(struct file *file);
131 static int usb_dsbr100_suspend(struct usb_interface *intf,
132 pm_message_t message);
133 static int usb_dsbr100_resume(struct usb_interface *intf);
135 static int radio_nr = -1;
136 module_param(radio_nr, int, 0);
138 /* Data for one (physical) device */
139 struct dsbr100_device {
140 struct usb_device *usbdev;
141 struct video_device videodev;
142 struct v4l2_device v4l2_dev;
144 u8 *transfer_buffer;
145 struct mutex lock; /* buffer locking */
146 int curfreq;
147 int stereo;
148 int users;
149 int removed;
150 int muted;
153 static struct usb_device_id usb_dsbr100_device_table [] = {
154 { USB_DEVICE(DSB100_VENDOR, DSB100_PRODUCT) },
155 { } /* Terminating entry */
158 MODULE_DEVICE_TABLE (usb, usb_dsbr100_device_table);
160 /* USB subsystem interface */
161 static struct usb_driver usb_dsbr100_driver = {
162 .name = "dsbr100",
163 .probe = usb_dsbr100_probe,
164 .disconnect = usb_dsbr100_disconnect,
165 .id_table = usb_dsbr100_device_table,
166 .suspend = usb_dsbr100_suspend,
167 .resume = usb_dsbr100_resume,
168 .reset_resume = usb_dsbr100_resume,
169 .supports_autosuspend = 0,
172 /* Low-level device interface begins here */
174 /* switch on radio */
175 static int dsbr100_start(struct dsbr100_device *radio)
177 int retval;
178 int request;
180 mutex_lock(&radio->lock);
182 retval = usb_control_msg(radio->usbdev,
183 usb_rcvctrlpipe(radio->usbdev, 0),
184 USB_REQ_GET_STATUS,
185 USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN,
186 0x00, 0xC7, radio->transfer_buffer, 8, 300);
188 if (retval < 0) {
189 request = USB_REQ_GET_STATUS;
190 goto usb_control_msg_failed;
193 retval = usb_control_msg(radio->usbdev,
194 usb_rcvctrlpipe(radio->usbdev, 0),
195 DSB100_ONOFF,
196 USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN,
197 0x01, 0x00, radio->transfer_buffer, 8, 300);
199 if (retval < 0) {
200 request = DSB100_ONOFF;
201 goto usb_control_msg_failed;
204 radio->muted = 0;
205 mutex_unlock(&radio->lock);
206 return (radio->transfer_buffer)[0];
208 usb_control_msg_failed:
209 mutex_unlock(&radio->lock);
210 dev_err(&radio->usbdev->dev,
211 "%s - usb_control_msg returned %i, request %i\n",
212 __func__, retval, request);
213 return retval;
217 /* switch off radio */
218 static int dsbr100_stop(struct dsbr100_device *radio)
220 int retval;
221 int request;
223 mutex_lock(&radio->lock);
225 retval = usb_control_msg(radio->usbdev,
226 usb_rcvctrlpipe(radio->usbdev, 0),
227 USB_REQ_GET_STATUS,
228 USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN,
229 0x16, 0x1C, radio->transfer_buffer, 8, 300);
231 if (retval < 0) {
232 request = USB_REQ_GET_STATUS;
233 goto usb_control_msg_failed;
236 retval = usb_control_msg(radio->usbdev,
237 usb_rcvctrlpipe(radio->usbdev, 0),
238 DSB100_ONOFF,
239 USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN,
240 0x00, 0x00, radio->transfer_buffer, 8, 300);
242 if (retval < 0) {
243 request = DSB100_ONOFF;
244 goto usb_control_msg_failed;
247 radio->muted = 1;
248 mutex_unlock(&radio->lock);
249 return (radio->transfer_buffer)[0];
251 usb_control_msg_failed:
252 mutex_unlock(&radio->lock);
253 dev_err(&radio->usbdev->dev,
254 "%s - usb_control_msg returned %i, request %i\n",
255 __func__, retval, request);
256 return retval;
260 /* set a frequency, freq is defined by v4l's TUNER_LOW, i.e. 1/16th kHz */
261 static int dsbr100_setfreq(struct dsbr100_device *radio, int freq)
263 int retval;
264 int request;
266 freq = (freq / 16 * 80) / 1000 + 856;
267 mutex_lock(&radio->lock);
269 retval = usb_control_msg(radio->usbdev,
270 usb_rcvctrlpipe(radio->usbdev, 0),
271 DSB100_TUNE,
272 USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN,
273 (freq >> 8) & 0x00ff, freq & 0xff,
274 radio->transfer_buffer, 8, 300);
276 if (retval < 0) {
277 request = DSB100_TUNE;
278 goto usb_control_msg_failed;
281 retval = usb_control_msg(radio->usbdev,
282 usb_rcvctrlpipe(radio->usbdev, 0),
283 USB_REQ_GET_STATUS,
284 USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN,
285 0x96, 0xB7, radio->transfer_buffer, 8, 300);
287 if (retval < 0) {
288 request = USB_REQ_GET_STATUS;
289 goto usb_control_msg_failed;
292 retval = usb_control_msg(radio->usbdev,
293 usb_rcvctrlpipe(radio->usbdev, 0),
294 USB_REQ_GET_STATUS,
295 USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN,
296 0x00, 0x24, radio->transfer_buffer, 8, 300);
298 if (retval < 0) {
299 request = USB_REQ_GET_STATUS;
300 goto usb_control_msg_failed;
303 radio->stereo = !((radio->transfer_buffer)[0] & 0x01);
304 mutex_unlock(&radio->lock);
305 return (radio->transfer_buffer)[0];
307 usb_control_msg_failed:
308 radio->stereo = -1;
309 mutex_unlock(&radio->lock);
310 dev_err(&radio->usbdev->dev,
311 "%s - usb_control_msg returned %i, request %i\n",
312 __func__, retval, request);
313 return retval;
316 /* return the device status. This is, in effect, just whether it
317 sees a stereo signal or not. Pity. */
318 static void dsbr100_getstat(struct dsbr100_device *radio)
320 int retval;
322 mutex_lock(&radio->lock);
324 retval = usb_control_msg(radio->usbdev,
325 usb_rcvctrlpipe(radio->usbdev, 0),
326 USB_REQ_GET_STATUS,
327 USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN,
328 0x00 , 0x24, radio->transfer_buffer, 8, 300);
330 if (retval < 0) {
331 radio->stereo = -1;
332 dev_err(&radio->usbdev->dev,
333 "%s - usb_control_msg returned %i, request %i\n",
334 __func__, retval, USB_REQ_GET_STATUS);
335 } else {
336 radio->stereo = !(radio->transfer_buffer[0] & 0x01);
339 mutex_unlock(&radio->lock);
342 /* USB subsystem interface begins here */
345 * Handle unplugging of the device.
346 * We call video_unregister_device in any case.
347 * The last function called in this procedure is
348 * usb_dsbr100_video_device_release
350 static void usb_dsbr100_disconnect(struct usb_interface *intf)
352 struct dsbr100_device *radio = usb_get_intfdata(intf);
354 usb_set_intfdata (intf, NULL);
356 mutex_lock(&radio->lock);
357 radio->removed = 1;
358 mutex_unlock(&radio->lock);
360 video_unregister_device(&radio->videodev);
361 v4l2_device_disconnect(&radio->v4l2_dev);
365 static int vidioc_querycap(struct file *file, void *priv,
366 struct v4l2_capability *v)
368 struct dsbr100_device *radio = video_drvdata(file);
370 strlcpy(v->driver, "dsbr100", sizeof(v->driver));
371 strlcpy(v->card, "D-Link R-100 USB FM Radio", sizeof(v->card));
372 usb_make_path(radio->usbdev, v->bus_info, sizeof(v->bus_info));
373 v->version = RADIO_VERSION;
374 v->capabilities = V4L2_CAP_TUNER;
375 return 0;
378 static int vidioc_g_tuner(struct file *file, void *priv,
379 struct v4l2_tuner *v)
381 struct dsbr100_device *radio = video_drvdata(file);
383 /* safety check */
384 if (radio->removed)
385 return -EIO;
387 if (v->index > 0)
388 return -EINVAL;
390 dsbr100_getstat(radio);
391 strcpy(v->name, "FM");
392 v->type = V4L2_TUNER_RADIO;
393 v->rangelow = FREQ_MIN * FREQ_MUL;
394 v->rangehigh = FREQ_MAX * FREQ_MUL;
395 v->rxsubchans = V4L2_TUNER_SUB_MONO | V4L2_TUNER_SUB_STEREO;
396 v->capability = V4L2_TUNER_CAP_LOW;
397 if(radio->stereo)
398 v->audmode = V4L2_TUNER_MODE_STEREO;
399 else
400 v->audmode = V4L2_TUNER_MODE_MONO;
401 v->signal = 0xffff; /* We can't get the signal strength */
402 return 0;
405 static int vidioc_s_tuner(struct file *file, void *priv,
406 struct v4l2_tuner *v)
408 struct dsbr100_device *radio = video_drvdata(file);
410 /* safety check */
411 if (radio->removed)
412 return -EIO;
414 if (v->index > 0)
415 return -EINVAL;
417 return 0;
420 static int vidioc_s_frequency(struct file *file, void *priv,
421 struct v4l2_frequency *f)
423 struct dsbr100_device *radio = video_drvdata(file);
424 int retval;
426 /* safety check */
427 if (radio->removed)
428 return -EIO;
430 mutex_lock(&radio->lock);
431 radio->curfreq = f->frequency;
432 mutex_unlock(&radio->lock);
434 retval = dsbr100_setfreq(radio, radio->curfreq);
435 if (retval < 0)
436 dev_warn(&radio->usbdev->dev, "Set frequency failed\n");
437 return 0;
440 static int vidioc_g_frequency(struct file *file, void *priv,
441 struct v4l2_frequency *f)
443 struct dsbr100_device *radio = video_drvdata(file);
445 /* safety check */
446 if (radio->removed)
447 return -EIO;
449 f->type = V4L2_TUNER_RADIO;
450 f->frequency = radio->curfreq;
451 return 0;
454 static int vidioc_queryctrl(struct file *file, void *priv,
455 struct v4l2_queryctrl *qc)
457 switch (qc->id) {
458 case V4L2_CID_AUDIO_MUTE:
459 return v4l2_ctrl_query_fill(qc, 0, 1, 1, 1);
462 return -EINVAL;
465 static int vidioc_g_ctrl(struct file *file, void *priv,
466 struct v4l2_control *ctrl)
468 struct dsbr100_device *radio = video_drvdata(file);
470 /* safety check */
471 if (radio->removed)
472 return -EIO;
474 switch (ctrl->id) {
475 case V4L2_CID_AUDIO_MUTE:
476 ctrl->value = radio->muted;
477 return 0;
479 return -EINVAL;
482 static int vidioc_s_ctrl(struct file *file, void *priv,
483 struct v4l2_control *ctrl)
485 struct dsbr100_device *radio = video_drvdata(file);
486 int retval;
488 /* safety check */
489 if (radio->removed)
490 return -EIO;
492 switch (ctrl->id) {
493 case V4L2_CID_AUDIO_MUTE:
494 if (ctrl->value) {
495 retval = dsbr100_stop(radio);
496 if (retval < 0) {
497 dev_warn(&radio->usbdev->dev,
498 "Radio did not respond properly\n");
499 return -EBUSY;
501 } else {
502 retval = dsbr100_start(radio);
503 if (retval < 0) {
504 dev_warn(&radio->usbdev->dev,
505 "Radio did not respond properly\n");
506 return -EBUSY;
509 return 0;
511 return -EINVAL;
514 static int vidioc_g_audio(struct file *file, void *priv,
515 struct v4l2_audio *a)
517 if (a->index > 1)
518 return -EINVAL;
520 strcpy(a->name, "Radio");
521 a->capability = V4L2_AUDCAP_STEREO;
522 return 0;
525 static int vidioc_g_input(struct file *filp, void *priv, unsigned int *i)
527 *i = 0;
528 return 0;
531 static int vidioc_s_input(struct file *filp, void *priv, unsigned int i)
533 if (i != 0)
534 return -EINVAL;
535 return 0;
538 static int vidioc_s_audio(struct file *file, void *priv,
539 struct v4l2_audio *a)
541 if (a->index != 0)
542 return -EINVAL;
543 return 0;
546 static int usb_dsbr100_open(struct file *file)
548 struct dsbr100_device *radio = video_drvdata(file);
549 int retval;
551 lock_kernel();
552 radio->users = 1;
553 radio->muted = 1;
555 retval = dsbr100_start(radio);
556 if (retval < 0) {
557 dev_warn(&radio->usbdev->dev,
558 "Radio did not start up properly\n");
559 radio->users = 0;
560 unlock_kernel();
561 return -EIO;
564 retval = dsbr100_setfreq(radio, radio->curfreq);
565 if (retval < 0)
566 dev_warn(&radio->usbdev->dev,
567 "set frequency failed\n");
569 unlock_kernel();
570 return 0;
573 static int usb_dsbr100_close(struct file *file)
575 struct dsbr100_device *radio = video_drvdata(file);
576 int retval;
578 if (!radio)
579 return -ENODEV;
581 mutex_lock(&radio->lock);
582 radio->users = 0;
583 mutex_unlock(&radio->lock);
585 if (!radio->removed) {
586 retval = dsbr100_stop(radio);
587 if (retval < 0) {
588 dev_warn(&radio->usbdev->dev,
589 "dsbr100_stop failed\n");
593 return 0;
596 /* Suspend device - stop device. */
597 static int usb_dsbr100_suspend(struct usb_interface *intf, pm_message_t message)
599 struct dsbr100_device *radio = usb_get_intfdata(intf);
600 int retval;
602 retval = dsbr100_stop(radio);
603 if (retval < 0)
604 dev_warn(&intf->dev, "dsbr100_stop failed\n");
606 dev_info(&intf->dev, "going into suspend..\n");
608 return 0;
611 /* Resume device - start device. */
612 static int usb_dsbr100_resume(struct usb_interface *intf)
614 struct dsbr100_device *radio = usb_get_intfdata(intf);
615 int retval;
617 retval = dsbr100_start(radio);
618 if (retval < 0)
619 dev_warn(&intf->dev, "dsbr100_start failed\n");
621 dev_info(&intf->dev, "coming out of suspend..\n");
623 return 0;
626 /* free data structures */
627 static void usb_dsbr100_video_device_release(struct video_device *videodev)
629 struct dsbr100_device *radio = videodev_to_radio(videodev);
631 v4l2_device_unregister(&radio->v4l2_dev);
632 kfree(radio->transfer_buffer);
633 kfree(radio);
636 /* File system interface */
637 static const struct v4l2_file_operations usb_dsbr100_fops = {
638 .owner = THIS_MODULE,
639 .open = usb_dsbr100_open,
640 .release = usb_dsbr100_close,
641 .ioctl = video_ioctl2,
644 static const struct v4l2_ioctl_ops usb_dsbr100_ioctl_ops = {
645 .vidioc_querycap = vidioc_querycap,
646 .vidioc_g_tuner = vidioc_g_tuner,
647 .vidioc_s_tuner = vidioc_s_tuner,
648 .vidioc_g_frequency = vidioc_g_frequency,
649 .vidioc_s_frequency = vidioc_s_frequency,
650 .vidioc_queryctrl = vidioc_queryctrl,
651 .vidioc_g_ctrl = vidioc_g_ctrl,
652 .vidioc_s_ctrl = vidioc_s_ctrl,
653 .vidioc_g_audio = vidioc_g_audio,
654 .vidioc_s_audio = vidioc_s_audio,
655 .vidioc_g_input = vidioc_g_input,
656 .vidioc_s_input = vidioc_s_input,
659 /* check if the device is present and register with v4l and usb if it is */
660 static int usb_dsbr100_probe(struct usb_interface *intf,
661 const struct usb_device_id *id)
663 struct dsbr100_device *radio;
664 struct v4l2_device *v4l2_dev;
665 int retval;
667 radio = kzalloc(sizeof(struct dsbr100_device), GFP_KERNEL);
669 if (!radio)
670 return -ENOMEM;
672 radio->transfer_buffer = kmalloc(TB_LEN, GFP_KERNEL);
674 if (!(radio->transfer_buffer)) {
675 kfree(radio);
676 return -ENOMEM;
679 v4l2_dev = &radio->v4l2_dev;
681 retval = v4l2_device_register(&intf->dev, v4l2_dev);
682 if (retval < 0) {
683 v4l2_err(v4l2_dev, "couldn't register v4l2_device\n");
684 kfree(radio->transfer_buffer);
685 kfree(radio);
686 return retval;
689 strlcpy(radio->videodev.name, v4l2_dev->name, sizeof(radio->videodev.name));
690 radio->videodev.v4l2_dev = v4l2_dev;
691 radio->videodev.fops = &usb_dsbr100_fops;
692 radio->videodev.ioctl_ops = &usb_dsbr100_ioctl_ops;
693 radio->videodev.release = usb_dsbr100_video_device_release;
695 mutex_init(&radio->lock);
697 radio->removed = 0;
698 radio->users = 0;
699 radio->usbdev = interface_to_usbdev(intf);
700 radio->curfreq = FREQ_MIN * FREQ_MUL;
702 video_set_drvdata(&radio->videodev, radio);
704 retval = video_register_device(&radio->videodev, VFL_TYPE_RADIO, radio_nr);
705 if (retval < 0) {
706 v4l2_err(v4l2_dev, "couldn't register video device\n");
707 v4l2_device_unregister(v4l2_dev);
708 kfree(radio->transfer_buffer);
709 kfree(radio);
710 return -EIO;
712 usb_set_intfdata(intf, radio);
713 return 0;
716 static int __init dsbr100_init(void)
718 int retval = usb_register(&usb_dsbr100_driver);
719 printk(KERN_INFO KBUILD_MODNAME ": " DRIVER_VERSION ":"
720 DRIVER_DESC "\n");
721 return retval;
724 static void __exit dsbr100_exit(void)
726 usb_deregister(&usb_dsbr100_driver);
729 module_init (dsbr100_init);
730 module_exit (dsbr100_exit);
732 MODULE_AUTHOR( DRIVER_AUTHOR );
733 MODULE_DESCRIPTION( DRIVER_DESC );
734 MODULE_LICENSE("GPL");