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
37 Converted to v4l2_device.
40 Add suspend/resume functions, fix unplug of device,
41 a lot of cleanups and fixes by Alexey Klimov <klimov.linux@gmail.com>
44 Oliver Neukum: avoided DMA coherency issue
47 Converted dsbr100 to use video_ioctl2
48 by Douglas Landgraf <dougsland@gmail.com>
51 Alan Cox: Some cleanups and fixes
54 Converted to V4L2 API by Mauro Carvalho Chehab <mchehab@infradead.org>
57 Markus: Updates for 2.6.x kernels, code layout changes, name sanitizing
60 Markus: Updates for 2.5.x kernel and more ISO compliant source
63 PSL and Markus: Cleanup, radio now doesn't stop on device close
66 Markus: Hope I got these silly VIDEO_TUNER_LOW issues finally
67 right. Some minor cleanup, improved standalone compilation
70 Markus: Sign extension bug fixed by declaring transfer_buffer unsigned
73 Markus: Some (brown bag) cleanup in what VIDIOCSTUNER returns,
74 thanks to Mike Cox for pointing the problem out.
77 Markus: Minor cleanup, warnings if something goes wrong, lame attempt
78 to adhere to Documentation/CodingStyle
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>
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
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;
145 struct mutex lock; /* buffer locking */
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 = {
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)
180 mutex_lock(&radio->lock);
182 retval = usb_control_msg(radio->usbdev,
183 usb_rcvctrlpipe(radio->usbdev, 0),
185 USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN,
186 0x00, 0xC7, radio->transfer_buffer, 8, 300);
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),
196 USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN,
197 0x01, 0x00, radio->transfer_buffer, 8, 300);
200 request = DSB100_ONOFF;
201 goto usb_control_msg_failed;
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);
217 /* switch off radio */
218 static int dsbr100_stop(struct dsbr100_device *radio)
223 mutex_lock(&radio->lock);
225 retval = usb_control_msg(radio->usbdev,
226 usb_rcvctrlpipe(radio->usbdev, 0),
228 USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN,
229 0x16, 0x1C, radio->transfer_buffer, 8, 300);
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),
239 USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN,
240 0x00, 0x00, radio->transfer_buffer, 8, 300);
243 request = DSB100_ONOFF;
244 goto usb_control_msg_failed;
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);
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)
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),
272 USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN,
273 (freq >> 8) & 0x00ff, freq & 0xff,
274 radio->transfer_buffer, 8, 300);
277 request = DSB100_TUNE;
278 goto usb_control_msg_failed;
281 retval = usb_control_msg(radio->usbdev,
282 usb_rcvctrlpipe(radio->usbdev, 0),
284 USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN,
285 0x96, 0xB7, radio->transfer_buffer, 8, 300);
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),
295 USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN,
296 0x00, 0x24, radio->transfer_buffer, 8, 300);
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:
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);
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)
322 mutex_lock(&radio->lock);
324 retval = usb_control_msg(radio->usbdev,
325 usb_rcvctrlpipe(radio->usbdev, 0),
327 USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN,
328 0x00 , 0x24, radio->transfer_buffer, 8, 300);
332 dev_err(&radio->usbdev->dev,
333 "%s - usb_control_msg returned %i, request %i\n",
334 __func__, retval, USB_REQ_GET_STATUS);
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);
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;
378 static int vidioc_g_tuner(struct file *file, void *priv,
379 struct v4l2_tuner *v)
381 struct dsbr100_device *radio = video_drvdata(file);
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;
398 v->audmode = V4L2_TUNER_MODE_STEREO;
400 v->audmode = V4L2_TUNER_MODE_MONO;
401 v->signal = 0xffff; /* We can't get the signal strength */
405 static int vidioc_s_tuner(struct file *file, void *priv,
406 struct v4l2_tuner *v)
408 struct dsbr100_device *radio = video_drvdata(file);
420 static int vidioc_s_frequency(struct file *file, void *priv,
421 struct v4l2_frequency *f)
423 struct dsbr100_device *radio = video_drvdata(file);
430 mutex_lock(&radio->lock);
431 radio->curfreq = f->frequency;
432 mutex_unlock(&radio->lock);
434 retval = dsbr100_setfreq(radio, radio->curfreq);
436 dev_warn(&radio->usbdev->dev, "Set frequency failed\n");
440 static int vidioc_g_frequency(struct file *file, void *priv,
441 struct v4l2_frequency *f)
443 struct dsbr100_device *radio = video_drvdata(file);
449 f->type = V4L2_TUNER_RADIO;
450 f->frequency = radio->curfreq;
454 static int vidioc_queryctrl(struct file *file, void *priv,
455 struct v4l2_queryctrl *qc)
458 case V4L2_CID_AUDIO_MUTE:
459 return v4l2_ctrl_query_fill(qc, 0, 1, 1, 1);
465 static int vidioc_g_ctrl(struct file *file, void *priv,
466 struct v4l2_control *ctrl)
468 struct dsbr100_device *radio = video_drvdata(file);
475 case V4L2_CID_AUDIO_MUTE:
476 ctrl->value = radio->muted;
482 static int vidioc_s_ctrl(struct file *file, void *priv,
483 struct v4l2_control *ctrl)
485 struct dsbr100_device *radio = video_drvdata(file);
493 case V4L2_CID_AUDIO_MUTE:
495 retval = dsbr100_stop(radio);
497 dev_warn(&radio->usbdev->dev,
498 "Radio did not respond properly\n");
502 retval = dsbr100_start(radio);
504 dev_warn(&radio->usbdev->dev,
505 "Radio did not respond properly\n");
514 static int vidioc_g_audio(struct file *file, void *priv,
515 struct v4l2_audio *a)
520 strcpy(a->name, "Radio");
521 a->capability = V4L2_AUDCAP_STEREO;
525 static int vidioc_g_input(struct file *filp, void *priv, unsigned int *i)
531 static int vidioc_s_input(struct file *filp, void *priv, unsigned int i)
538 static int vidioc_s_audio(struct file *file, void *priv,
539 struct v4l2_audio *a)
546 static int usb_dsbr100_open(struct file *file)
548 struct dsbr100_device *radio = video_drvdata(file);
555 retval = dsbr100_start(radio);
557 dev_warn(&radio->usbdev->dev,
558 "Radio did not start up properly\n");
564 retval = dsbr100_setfreq(radio, radio->curfreq);
566 dev_warn(&radio->usbdev->dev,
567 "set frequency failed\n");
573 static int usb_dsbr100_close(struct file *file)
575 struct dsbr100_device *radio = video_drvdata(file);
581 mutex_lock(&radio->lock);
583 mutex_unlock(&radio->lock);
585 if (!radio->removed) {
586 retval = dsbr100_stop(radio);
588 dev_warn(&radio->usbdev->dev,
589 "dsbr100_stop failed\n");
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);
602 retval = dsbr100_stop(radio);
604 dev_warn(&intf->dev, "dsbr100_stop failed\n");
606 dev_info(&intf->dev, "going into suspend..\n");
611 /* Resume device - start device. */
612 static int usb_dsbr100_resume(struct usb_interface *intf)
614 struct dsbr100_device *radio = usb_get_intfdata(intf);
617 retval = dsbr100_start(radio);
619 dev_warn(&intf->dev, "dsbr100_start failed\n");
621 dev_info(&intf->dev, "coming out of suspend..\n");
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);
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;
667 radio = kzalloc(sizeof(struct dsbr100_device), GFP_KERNEL);
672 radio->transfer_buffer = kmalloc(TB_LEN, GFP_KERNEL);
674 if (!(radio->transfer_buffer)) {
679 v4l2_dev = &radio->v4l2_dev;
681 retval = v4l2_device_register(&intf->dev, v4l2_dev);
683 v4l2_err(v4l2_dev, "couldn't register v4l2_device\n");
684 kfree(radio->transfer_buffer);
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);
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);
706 v4l2_err(v4l2_dev, "couldn't register video device\n");
707 v4l2_device_unregister(v4l2_dev);
708 kfree(radio->transfer_buffer);
712 usb_set_intfdata(intf, radio);
716 static int __init dsbr100_init(void)
718 int retval = usb_register(&usb_dsbr100_driver);
719 printk(KERN_INFO KBUILD_MODNAME ": " DRIVER_VERSION ":"
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");