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 Oliver Neukum: avoided DMA coherency issue
40 Converted dsbr100 to use video_ioctl2
41 by Douglas Landgraf <dougsland@gmail.com>
44 Alan Cox: Some cleanups and fixes
47 Converted to V4L2 API by Mauro Carvalho Chehab <mchehab@infradead.org>
50 Markus: Updates for 2.6.x kernels, code layout changes, name sanitizing
53 Markus: Updates for 2.5.x kernel and more ISO compliant source
56 PSL and Markus: Cleanup, radio now doesn't stop on device close
59 Markus: Hope I got these silly VIDEO_TUNER_LOW issues finally
60 right. Some minor cleanup, improved standalone compilation
63 Markus: Sign extension bug fixed by declaring transfer_buffer unsigned
66 Markus: Some (brown bag) cleanup in what VIDIOCSTUNER returns,
67 thanks to Mike Cox for pointing the problem out.
70 Markus: Minor cleanup, warnings if something goes wrong, lame attempt
71 to adhere to Documentation/CodingStyle
74 Brad Hards <bradh@dynamite.com.au>: Fixes to make it work as non-module
75 Markus: Copyright clarification
77 Version 0.01: Markus: initial release
81 #include <linux/kernel.h>
82 #include <linux/module.h>
83 #include <linux/init.h>
84 #include <linux/slab.h>
85 #include <linux/input.h>
86 #include <linux/videodev2.h>
87 #include <media/v4l2-common.h>
88 #include <media/v4l2-ioctl.h>
89 #include <linux/usb.h>
94 #include <linux/version.h> /* for KERNEL_VERSION MACRO */
96 #define DRIVER_VERSION "v0.43"
97 #define RADIO_VERSION KERNEL_VERSION(0, 4, 3)
99 static struct v4l2_queryctrl radio_qctrl[] = {
101 .id = V4L2_CID_AUDIO_MUTE,
106 .type = V4L2_CTRL_TYPE_BOOLEAN,
108 /* HINT: the disabled controls are only here to satify kradio and such apps */
109 { .id = V4L2_CID_AUDIO_VOLUME,
110 .flags = V4L2_CTRL_FLAG_DISABLED,
113 .id = V4L2_CID_AUDIO_BALANCE,
114 .flags = V4L2_CTRL_FLAG_DISABLED,
117 .id = V4L2_CID_AUDIO_BASS,
118 .flags = V4L2_CTRL_FLAG_DISABLED,
121 .id = V4L2_CID_AUDIO_TREBLE,
122 .flags = V4L2_CTRL_FLAG_DISABLED,
125 .id = V4L2_CID_AUDIO_LOUDNESS,
126 .flags = V4L2_CTRL_FLAG_DISABLED,
130 #define DRIVER_AUTHOR "Markus Demleitner <msdemlei@tucana.harvard.edu>"
131 #define DRIVER_DESC "D-Link DSB-R100 USB FM radio driver"
133 #define DSB100_VENDOR 0x04b4
134 #define DSB100_PRODUCT 0x1002
136 /* Commands the device appears to understand */
137 #define DSB100_TUNE 1
138 #define DSB100_ONOFF 2
142 /* Frequency limits in MHz -- these are European values. For Japanese
143 devices, that would be 76 and 91. */
144 #define FREQ_MIN 87.5
145 #define FREQ_MAX 108.0
146 #define FREQ_MUL 16000
148 #define videodev_to_radio(d) container_of(d, struct dsbr100_device, videodev)
150 static int usb_dsbr100_probe(struct usb_interface *intf,
151 const struct usb_device_id *id);
152 static void usb_dsbr100_disconnect(struct usb_interface *intf);
153 static int usb_dsbr100_open(struct inode *inode, struct file *file);
154 static int usb_dsbr100_close(struct inode *inode, struct file *file);
155 static int usb_dsbr100_suspend(struct usb_interface *intf,
156 pm_message_t message);
157 static int usb_dsbr100_resume(struct usb_interface *intf);
159 static int radio_nr = -1;
160 module_param(radio_nr, int, 0);
162 /* Data for one (physical) device */
163 struct dsbr100_device {
164 struct usb_device *usbdev;
165 struct video_device videodev;
167 struct mutex lock; /* buffer locking */
175 static struct usb_device_id usb_dsbr100_device_table [] = {
176 { USB_DEVICE(DSB100_VENDOR, DSB100_PRODUCT) },
177 { } /* Terminating entry */
180 MODULE_DEVICE_TABLE (usb, usb_dsbr100_device_table);
182 /* USB subsystem interface */
183 static struct usb_driver usb_dsbr100_driver = {
185 .probe = usb_dsbr100_probe,
186 .disconnect = usb_dsbr100_disconnect,
187 .id_table = usb_dsbr100_device_table,
188 .suspend = usb_dsbr100_suspend,
189 .resume = usb_dsbr100_resume,
190 .reset_resume = usb_dsbr100_resume,
191 .supports_autosuspend = 0,
194 /* Low-level device interface begins here */
196 /* switch on radio */
197 static int dsbr100_start(struct dsbr100_device *radio)
202 mutex_lock(&radio->lock);
204 retval = usb_control_msg(radio->usbdev,
205 usb_rcvctrlpipe(radio->usbdev, 0),
207 USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN,
208 0x00, 0xC7, radio->transfer_buffer, 8, 300);
211 request = USB_REQ_GET_STATUS;
212 goto usb_control_msg_failed;
215 retval = usb_control_msg(radio->usbdev,
216 usb_rcvctrlpipe(radio->usbdev, 0),
218 USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN,
219 0x01, 0x00, radio->transfer_buffer, 8, 300);
222 request = DSB100_ONOFF;
223 goto usb_control_msg_failed;
227 mutex_unlock(&radio->lock);
228 return (radio->transfer_buffer)[0];
230 usb_control_msg_failed:
231 mutex_unlock(&radio->lock);
232 dev_err(&radio->usbdev->dev,
233 "%s - usb_control_msg returned %i, request %i\n",
234 __func__, retval, request);
239 /* switch off radio */
240 static int dsbr100_stop(struct dsbr100_device *radio)
245 mutex_lock(&radio->lock);
247 retval = usb_control_msg(radio->usbdev,
248 usb_rcvctrlpipe(radio->usbdev, 0),
250 USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN,
251 0x16, 0x1C, radio->transfer_buffer, 8, 300);
254 request = USB_REQ_GET_STATUS;
255 goto usb_control_msg_failed;
258 retval = usb_control_msg(radio->usbdev,
259 usb_rcvctrlpipe(radio->usbdev, 0),
261 USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN,
262 0x00, 0x00, radio->transfer_buffer, 8, 300);
265 request = DSB100_ONOFF;
266 goto usb_control_msg_failed;
270 mutex_unlock(&radio->lock);
271 return (radio->transfer_buffer)[0];
273 usb_control_msg_failed:
274 mutex_unlock(&radio->lock);
275 dev_err(&radio->usbdev->dev,
276 "%s - usb_control_msg returned %i, request %i\n",
277 __func__, retval, request);
282 /* set a frequency, freq is defined by v4l's TUNER_LOW, i.e. 1/16th kHz */
283 static int dsbr100_setfreq(struct dsbr100_device *radio, int freq)
288 freq = (freq / 16 * 80) / 1000 + 856;
289 mutex_lock(&radio->lock);
291 retval = usb_control_msg(radio->usbdev,
292 usb_rcvctrlpipe(radio->usbdev, 0),
294 USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN,
295 (freq >> 8) & 0x00ff, freq & 0xff,
296 radio->transfer_buffer, 8, 300);
299 request = DSB100_TUNE;
300 goto usb_control_msg_failed;
303 retval = usb_control_msg(radio->usbdev,
304 usb_rcvctrlpipe(radio->usbdev, 0),
306 USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN,
307 0x96, 0xB7, radio->transfer_buffer, 8, 300);
310 request = USB_REQ_GET_STATUS;
311 goto usb_control_msg_failed;
314 retval = usb_control_msg(radio->usbdev,
315 usb_rcvctrlpipe(radio->usbdev, 0),
317 USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN,
318 0x00, 0x24, radio->transfer_buffer, 8, 300);
321 request = USB_REQ_GET_STATUS;
322 goto usb_control_msg_failed;
325 radio->stereo = !((radio->transfer_buffer)[0] & 0x01);
326 mutex_unlock(&radio->lock);
327 return (radio->transfer_buffer)[0];
329 usb_control_msg_failed:
331 mutex_unlock(&radio->lock);
332 dev_err(&radio->usbdev->dev,
333 "%s - usb_control_msg returned %i, request %i\n",
334 __func__, retval, request);
338 /* return the device status. This is, in effect, just whether it
339 sees a stereo signal or not. Pity. */
340 static void dsbr100_getstat(struct dsbr100_device *radio)
344 mutex_lock(&radio->lock);
346 retval = usb_control_msg(radio->usbdev,
347 usb_rcvctrlpipe(radio->usbdev, 0),
349 USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN,
350 0x00 , 0x24, radio->transfer_buffer, 8, 300);
354 dev_err(&radio->usbdev->dev,
355 "%s - usb_control_msg returned %i, request %i\n",
356 __func__, retval, USB_REQ_GET_STATUS);
358 radio->stereo = !(radio->transfer_buffer[0] & 0x01);
361 mutex_unlock(&radio->lock);
364 /* USB subsystem interface begins here */
367 * Handle unplugging of the device.
368 * We call video_unregister_device in any case.
369 * The last function called in this procedure is
370 * usb_dsbr100_video_device_release
372 static void usb_dsbr100_disconnect(struct usb_interface *intf)
374 struct dsbr100_device *radio = usb_get_intfdata(intf);
376 usb_set_intfdata (intf, NULL);
378 mutex_lock(&radio->lock);
380 mutex_unlock(&radio->lock);
382 video_unregister_device(&radio->videodev);
386 static int vidioc_querycap(struct file *file, void *priv,
387 struct v4l2_capability *v)
389 strlcpy(v->driver, "dsbr100", sizeof(v->driver));
390 strlcpy(v->card, "D-Link R-100 USB FM Radio", sizeof(v->card));
391 sprintf(v->bus_info, "USB");
392 v->version = RADIO_VERSION;
393 v->capabilities = V4L2_CAP_TUNER;
397 static int vidioc_g_tuner(struct file *file, void *priv,
398 struct v4l2_tuner *v)
400 struct dsbr100_device *radio = video_drvdata(file);
409 dsbr100_getstat(radio);
410 strcpy(v->name, "FM");
411 v->type = V4L2_TUNER_RADIO;
412 v->rangelow = FREQ_MIN * FREQ_MUL;
413 v->rangehigh = FREQ_MAX * FREQ_MUL;
414 v->rxsubchans = V4L2_TUNER_SUB_MONO | V4L2_TUNER_SUB_STEREO;
415 v->capability = V4L2_TUNER_CAP_LOW;
417 v->audmode = V4L2_TUNER_MODE_STEREO;
419 v->audmode = V4L2_TUNER_MODE_MONO;
420 v->signal = 0xffff; /* We can't get the signal strength */
424 static int vidioc_s_tuner(struct file *file, void *priv,
425 struct v4l2_tuner *v)
427 struct dsbr100_device *radio = video_drvdata(file);
439 static int vidioc_s_frequency(struct file *file, void *priv,
440 struct v4l2_frequency *f)
442 struct dsbr100_device *radio = video_drvdata(file);
449 radio->curfreq = f->frequency;
450 retval = dsbr100_setfreq(radio, radio->curfreq);
452 dev_warn(&radio->usbdev->dev, "Set frequency failed\n");
456 static int vidioc_g_frequency(struct file *file, void *priv,
457 struct v4l2_frequency *f)
459 struct dsbr100_device *radio = video_drvdata(file);
465 f->type = V4L2_TUNER_RADIO;
466 f->frequency = radio->curfreq;
470 static int vidioc_queryctrl(struct file *file, void *priv,
471 struct v4l2_queryctrl *qc)
475 for (i = 0; i < ARRAY_SIZE(radio_qctrl); i++) {
476 if (qc->id && qc->id == radio_qctrl[i].id) {
477 memcpy(qc, &(radio_qctrl[i]), sizeof(*qc));
484 static int vidioc_g_ctrl(struct file *file, void *priv,
485 struct v4l2_control *ctrl)
487 struct dsbr100_device *radio = video_drvdata(file);
494 case V4L2_CID_AUDIO_MUTE:
495 ctrl->value = radio->muted;
501 static int vidioc_s_ctrl(struct file *file, void *priv,
502 struct v4l2_control *ctrl)
504 struct dsbr100_device *radio = video_drvdata(file);
512 case V4L2_CID_AUDIO_MUTE:
514 retval = dsbr100_stop(radio);
516 dev_warn(&radio->usbdev->dev,
517 "Radio did not respond properly\n");
521 retval = dsbr100_start(radio);
523 dev_warn(&radio->usbdev->dev,
524 "Radio did not respond properly\n");
533 static int vidioc_g_audio(struct file *file, void *priv,
534 struct v4l2_audio *a)
539 strcpy(a->name, "Radio");
540 a->capability = V4L2_AUDCAP_STEREO;
544 static int vidioc_g_input(struct file *filp, void *priv, unsigned int *i)
550 static int vidioc_s_input(struct file *filp, void *priv, unsigned int i)
557 static int vidioc_s_audio(struct file *file, void *priv,
558 struct v4l2_audio *a)
565 static int usb_dsbr100_open(struct inode *inode, struct file *file)
567 struct dsbr100_device *radio = video_drvdata(file);
574 retval = dsbr100_start(radio);
576 dev_warn(&radio->usbdev->dev,
577 "Radio did not start up properly\n");
583 retval = dsbr100_setfreq(radio, radio->curfreq);
585 dev_warn(&radio->usbdev->dev,
586 "set frequency failed\n");
592 static int usb_dsbr100_close(struct inode *inode, struct file *file)
594 struct dsbr100_device *radio = video_drvdata(file);
601 if (!radio->removed) {
602 retval = dsbr100_stop(radio);
604 dev_warn(&radio->usbdev->dev,
605 "dsbr100_stop failed\n");
612 /* Suspend device - stop device. */
613 static int usb_dsbr100_suspend(struct usb_interface *intf, pm_message_t message)
615 struct dsbr100_device *radio = usb_get_intfdata(intf);
618 retval = dsbr100_stop(radio);
620 dev_warn(&intf->dev, "dsbr100_stop failed\n");
622 dev_info(&intf->dev, "going into suspend..\n");
627 /* Resume device - start device. */
628 static int usb_dsbr100_resume(struct usb_interface *intf)
630 struct dsbr100_device *radio = usb_get_intfdata(intf);
633 retval = dsbr100_start(radio);
635 dev_warn(&intf->dev, "dsbr100_start failed\n");
637 dev_info(&intf->dev, "coming out of suspend..\n");
642 /* free data structures */
643 static void usb_dsbr100_video_device_release(struct video_device *videodev)
645 struct dsbr100_device *radio = videodev_to_radio(videodev);
647 kfree(radio->transfer_buffer);
651 /* File system interface */
652 static const struct file_operations usb_dsbr100_fops = {
653 .owner = THIS_MODULE,
654 .open = usb_dsbr100_open,
655 .release = usb_dsbr100_close,
656 .ioctl = video_ioctl2,
658 .compat_ioctl = v4l_compat_ioctl32,
663 static const struct v4l2_ioctl_ops usb_dsbr100_ioctl_ops = {
664 .vidioc_querycap = vidioc_querycap,
665 .vidioc_g_tuner = vidioc_g_tuner,
666 .vidioc_s_tuner = vidioc_s_tuner,
667 .vidioc_g_frequency = vidioc_g_frequency,
668 .vidioc_s_frequency = vidioc_s_frequency,
669 .vidioc_queryctrl = vidioc_queryctrl,
670 .vidioc_g_ctrl = vidioc_g_ctrl,
671 .vidioc_s_ctrl = vidioc_s_ctrl,
672 .vidioc_g_audio = vidioc_g_audio,
673 .vidioc_s_audio = vidioc_s_audio,
674 .vidioc_g_input = vidioc_g_input,
675 .vidioc_s_input = vidioc_s_input,
679 static struct video_device dsbr100_videodev_data = {
680 .name = "D-Link DSB-R 100",
681 .fops = &usb_dsbr100_fops,
682 .ioctl_ops = &usb_dsbr100_ioctl_ops,
683 .release = usb_dsbr100_video_device_release,
686 /* check if the device is present and register with v4l and usb if it is */
687 static int usb_dsbr100_probe(struct usb_interface *intf,
688 const struct usb_device_id *id)
690 struct dsbr100_device *radio;
693 radio = kmalloc(sizeof(struct dsbr100_device), GFP_KERNEL);
698 radio->transfer_buffer = kmalloc(TB_LEN, GFP_KERNEL);
700 if (!(radio->transfer_buffer)) {
705 mutex_init(&radio->lock);
706 radio->videodev = dsbr100_videodev_data;
710 radio->usbdev = interface_to_usbdev(intf);
711 radio->curfreq = FREQ_MIN * FREQ_MUL;
712 video_set_drvdata(&radio->videodev, radio);
713 retval = video_register_device(&radio->videodev, VFL_TYPE_RADIO, radio_nr);
715 dev_err(&intf->dev, "couldn't register video device\n");
716 kfree(radio->transfer_buffer);
720 usb_set_intfdata(intf, radio);
724 static int __init dsbr100_init(void)
726 int retval = usb_register(&usb_dsbr100_driver);
727 printk(KERN_INFO KBUILD_MODNAME ": " DRIVER_VERSION ":"
732 static void __exit dsbr100_exit(void)
734 usb_deregister(&usb_dsbr100_driver);
737 module_init (dsbr100_init);
738 module_exit (dsbr100_exit);
740 MODULE_AUTHOR( DRIVER_AUTHOR );
741 MODULE_DESCRIPTION( DRIVER_DESC );
742 MODULE_LICENSE("GPL");