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 Removed usb_dsbr100_open/close calls and radio->users counter. Also,
38 radio->muted changed to radio->status and suspend/resume calls updated.
41 Converted to v4l2_device.
44 Add suspend/resume functions, fix unplug of device,
45 a lot of cleanups and fixes by Alexey Klimov <klimov.linux@gmail.com>
48 Oliver Neukum: avoided DMA coherency issue
51 Converted dsbr100 to use video_ioctl2
52 by Douglas Landgraf <dougsland@gmail.com>
55 Alan Cox: Some cleanups and fixes
58 Converted to V4L2 API by Mauro Carvalho Chehab <mchehab@infradead.org>
61 Markus: Updates for 2.6.x kernels, code layout changes, name sanitizing
64 Markus: Updates for 2.5.x kernel and more ISO compliant source
67 PSL and Markus: Cleanup, radio now doesn't stop on device close
70 Markus: Hope I got these silly VIDEO_TUNER_LOW issues finally
71 right. Some minor cleanup, improved standalone compilation
74 Markus: Sign extension bug fixed by declaring transfer_buffer unsigned
77 Markus: Some (brown bag) cleanup in what VIDIOCSTUNER returns,
78 thanks to Mike Cox for pointing the problem out.
81 Markus: Minor cleanup, warnings if something goes wrong, lame attempt
82 to adhere to Documentation/CodingStyle
85 Brad Hards <bradh@dynamite.com.au>: Fixes to make it work as non-module
86 Markus: Copyright clarification
88 Version 0.01: Markus: initial release
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>
103 * Version Information
105 #include <linux/version.h> /* for KERNEL_VERSION MACRO */
107 #define DRIVER_VERSION "v0.46"
108 #define RADIO_VERSION KERNEL_VERSION(0, 4, 6)
110 #define DRIVER_AUTHOR "Markus Demleitner <msdemlei@tucana.harvard.edu>"
111 #define DRIVER_DESC "D-Link DSB-R100 USB FM radio driver"
113 #define DSB100_VENDOR 0x04b4
114 #define DSB100_PRODUCT 0x1002
116 /* Commands the device appears to understand */
117 #define DSB100_TUNE 1
118 #define DSB100_ONOFF 2
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
128 /* defines for radio->status */
132 #define videodev_to_radio(d) container_of(d, struct dsbr100_device, videodev)
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);
141 static int radio_nr = -1;
142 module_param(radio_nr, int, 0);
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;
151 struct mutex lock; /* buffer locking */
158 static struct usb_device_id usb_dsbr100_device_table [] = {
159 { USB_DEVICE(DSB100_VENDOR, DSB100_PRODUCT) },
160 { } /* Terminating entry */
163 MODULE_DEVICE_TABLE (usb, usb_dsbr100_device_table);
165 /* USB subsystem interface */
166 static struct usb_driver usb_dsbr100_driver = {
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,
177 /* Low-level device interface begins here */
179 /* switch on radio */
180 static int dsbr100_start(struct dsbr100_device *radio)
185 mutex_lock(&radio->lock);
187 retval = usb_control_msg(radio->usbdev,
188 usb_rcvctrlpipe(radio->usbdev, 0),
190 USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN,
191 0x00, 0xC7, radio->transfer_buffer, 8, 300);
194 request = USB_REQ_GET_STATUS;
195 goto usb_control_msg_failed;
198 retval = usb_control_msg(radio->usbdev,
199 usb_rcvctrlpipe(radio->usbdev, 0),
201 USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN,
202 0x01, 0x00, radio->transfer_buffer, 8, 300);
205 request = DSB100_ONOFF;
206 goto usb_control_msg_failed;
209 radio->status = STARTED;
210 mutex_unlock(&radio->lock);
211 return (radio->transfer_buffer)[0];
213 usb_control_msg_failed:
214 mutex_unlock(&radio->lock);
215 dev_err(&radio->usbdev->dev,
216 "%s - usb_control_msg returned %i, request %i\n",
217 __func__, retval, request);
222 /* switch off radio */
223 static int dsbr100_stop(struct dsbr100_device *radio)
228 mutex_lock(&radio->lock);
230 retval = usb_control_msg(radio->usbdev,
231 usb_rcvctrlpipe(radio->usbdev, 0),
233 USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN,
234 0x16, 0x1C, radio->transfer_buffer, 8, 300);
237 request = USB_REQ_GET_STATUS;
238 goto usb_control_msg_failed;
241 retval = usb_control_msg(radio->usbdev,
242 usb_rcvctrlpipe(radio->usbdev, 0),
244 USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN,
245 0x00, 0x00, radio->transfer_buffer, 8, 300);
248 request = DSB100_ONOFF;
249 goto usb_control_msg_failed;
252 radio->status = STOPPED;
253 mutex_unlock(&radio->lock);
254 return (radio->transfer_buffer)[0];
256 usb_control_msg_failed:
257 mutex_unlock(&radio->lock);
258 dev_err(&radio->usbdev->dev,
259 "%s - usb_control_msg returned %i, request %i\n",
260 __func__, retval, request);
265 /* set a frequency, freq is defined by v4l's TUNER_LOW, i.e. 1/16th kHz */
266 static int dsbr100_setfreq(struct dsbr100_device *radio)
270 int freq = (radio->curfreq / 16 * 80) / 1000 + 856;
272 mutex_lock(&radio->lock);
274 retval = usb_control_msg(radio->usbdev,
275 usb_rcvctrlpipe(radio->usbdev, 0),
277 USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN,
278 (freq >> 8) & 0x00ff, freq & 0xff,
279 radio->transfer_buffer, 8, 300);
282 request = DSB100_TUNE;
283 goto usb_control_msg_failed;
286 retval = usb_control_msg(radio->usbdev,
287 usb_rcvctrlpipe(radio->usbdev, 0),
289 USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN,
290 0x96, 0xB7, radio->transfer_buffer, 8, 300);
293 request = USB_REQ_GET_STATUS;
294 goto usb_control_msg_failed;
297 retval = usb_control_msg(radio->usbdev,
298 usb_rcvctrlpipe(radio->usbdev, 0),
300 USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN,
301 0x00, 0x24, radio->transfer_buffer, 8, 300);
304 request = USB_REQ_GET_STATUS;
305 goto usb_control_msg_failed;
308 radio->stereo = !((radio->transfer_buffer)[0] & 0x01);
309 mutex_unlock(&radio->lock);
310 return (radio->transfer_buffer)[0];
312 usb_control_msg_failed:
314 mutex_unlock(&radio->lock);
315 dev_err(&radio->usbdev->dev,
316 "%s - usb_control_msg returned %i, request %i\n",
317 __func__, retval, request);
321 /* return the device status. This is, in effect, just whether it
322 sees a stereo signal or not. Pity. */
323 static void dsbr100_getstat(struct dsbr100_device *radio)
327 mutex_lock(&radio->lock);
329 retval = usb_control_msg(radio->usbdev,
330 usb_rcvctrlpipe(radio->usbdev, 0),
332 USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN,
333 0x00 , 0x24, radio->transfer_buffer, 8, 300);
337 dev_err(&radio->usbdev->dev,
338 "%s - usb_control_msg returned %i, request %i\n",
339 __func__, retval, USB_REQ_GET_STATUS);
341 radio->stereo = !(radio->transfer_buffer[0] & 0x01);
344 mutex_unlock(&radio->lock);
347 /* USB subsystem interface begins here */
350 * Handle unplugging of the device.
351 * We call video_unregister_device in any case.
352 * The last function called in this procedure is
353 * usb_dsbr100_video_device_release
355 static void usb_dsbr100_disconnect(struct usb_interface *intf)
357 struct dsbr100_device *radio = usb_get_intfdata(intf);
359 usb_set_intfdata (intf, NULL);
361 mutex_lock(&radio->lock);
363 mutex_unlock(&radio->lock);
365 video_unregister_device(&radio->videodev);
366 v4l2_device_disconnect(&radio->v4l2_dev);
370 static int vidioc_querycap(struct file *file, void *priv,
371 struct v4l2_capability *v)
373 struct dsbr100_device *radio = video_drvdata(file);
375 strlcpy(v->driver, "dsbr100", sizeof(v->driver));
376 strlcpy(v->card, "D-Link R-100 USB FM Radio", sizeof(v->card));
377 usb_make_path(radio->usbdev, v->bus_info, sizeof(v->bus_info));
378 v->version = RADIO_VERSION;
379 v->capabilities = V4L2_CAP_TUNER;
383 static int vidioc_g_tuner(struct file *file, void *priv,
384 struct v4l2_tuner *v)
386 struct dsbr100_device *radio = video_drvdata(file);
395 dsbr100_getstat(radio);
396 strcpy(v->name, "FM");
397 v->type = V4L2_TUNER_RADIO;
398 v->rangelow = FREQ_MIN * FREQ_MUL;
399 v->rangehigh = FREQ_MAX * FREQ_MUL;
400 v->rxsubchans = V4L2_TUNER_SUB_MONO | V4L2_TUNER_SUB_STEREO;
401 v->capability = V4L2_TUNER_CAP_LOW;
403 v->audmode = V4L2_TUNER_MODE_STEREO;
405 v->audmode = V4L2_TUNER_MODE_MONO;
406 v->signal = 0xffff; /* We can't get the signal strength */
410 static int vidioc_s_tuner(struct file *file, void *priv,
411 struct v4l2_tuner *v)
413 struct dsbr100_device *radio = video_drvdata(file);
425 static int vidioc_s_frequency(struct file *file, void *priv,
426 struct v4l2_frequency *f)
428 struct dsbr100_device *radio = video_drvdata(file);
435 mutex_lock(&radio->lock);
436 radio->curfreq = f->frequency;
437 mutex_unlock(&radio->lock);
439 retval = dsbr100_setfreq(radio);
441 dev_warn(&radio->usbdev->dev, "Set frequency failed\n");
445 static int vidioc_g_frequency(struct file *file, void *priv,
446 struct v4l2_frequency *f)
448 struct dsbr100_device *radio = video_drvdata(file);
454 f->type = V4L2_TUNER_RADIO;
455 f->frequency = radio->curfreq;
459 static int vidioc_queryctrl(struct file *file, void *priv,
460 struct v4l2_queryctrl *qc)
463 case V4L2_CID_AUDIO_MUTE:
464 return v4l2_ctrl_query_fill(qc, 0, 1, 1, 1);
470 static int vidioc_g_ctrl(struct file *file, void *priv,
471 struct v4l2_control *ctrl)
473 struct dsbr100_device *radio = video_drvdata(file);
480 case V4L2_CID_AUDIO_MUTE:
481 ctrl->value = radio->status;
487 static int vidioc_s_ctrl(struct file *file, void *priv,
488 struct v4l2_control *ctrl)
490 struct dsbr100_device *radio = video_drvdata(file);
498 case V4L2_CID_AUDIO_MUTE:
500 retval = dsbr100_stop(radio);
502 dev_warn(&radio->usbdev->dev,
503 "Radio did not respond properly\n");
507 retval = dsbr100_start(radio);
509 dev_warn(&radio->usbdev->dev,
510 "Radio did not respond properly\n");
519 static int vidioc_g_audio(struct file *file, void *priv,
520 struct v4l2_audio *a)
525 strcpy(a->name, "Radio");
526 a->capability = V4L2_AUDCAP_STEREO;
530 static int vidioc_g_input(struct file *filp, void *priv, unsigned int *i)
536 static int vidioc_s_input(struct file *filp, void *priv, unsigned int i)
543 static int vidioc_s_audio(struct file *file, void *priv,
544 struct v4l2_audio *a)
551 /* Suspend device - stop device. */
552 static int usb_dsbr100_suspend(struct usb_interface *intf, pm_message_t message)
554 struct dsbr100_device *radio = usb_get_intfdata(intf);
557 if (radio->status == STARTED) {
558 retval = dsbr100_stop(radio);
560 dev_warn(&intf->dev, "dsbr100_stop failed\n");
562 /* After dsbr100_stop() status set to STOPPED.
563 * If we want driver to start radio on resume
564 * we set status equal to STARTED.
565 * On resume we will check status and run radio if needed.
568 mutex_lock(&radio->lock);
569 radio->status = STARTED;
570 mutex_unlock(&radio->lock);
573 dev_info(&intf->dev, "going into suspend..\n");
578 /* Resume device - start device. */
579 static int usb_dsbr100_resume(struct usb_interface *intf)
581 struct dsbr100_device *radio = usb_get_intfdata(intf);
584 if (radio->status == STARTED) {
585 retval = dsbr100_start(radio);
587 dev_warn(&intf->dev, "dsbr100_start failed\n");
590 dev_info(&intf->dev, "coming out of suspend..\n");
595 /* free data structures */
596 static void usb_dsbr100_video_device_release(struct video_device *videodev)
598 struct dsbr100_device *radio = videodev_to_radio(videodev);
600 v4l2_device_unregister(&radio->v4l2_dev);
601 kfree(radio->transfer_buffer);
605 /* File system interface */
606 static const struct v4l2_file_operations usb_dsbr100_fops = {
607 .owner = THIS_MODULE,
608 .ioctl = video_ioctl2,
611 static const struct v4l2_ioctl_ops usb_dsbr100_ioctl_ops = {
612 .vidioc_querycap = vidioc_querycap,
613 .vidioc_g_tuner = vidioc_g_tuner,
614 .vidioc_s_tuner = vidioc_s_tuner,
615 .vidioc_g_frequency = vidioc_g_frequency,
616 .vidioc_s_frequency = vidioc_s_frequency,
617 .vidioc_queryctrl = vidioc_queryctrl,
618 .vidioc_g_ctrl = vidioc_g_ctrl,
619 .vidioc_s_ctrl = vidioc_s_ctrl,
620 .vidioc_g_audio = vidioc_g_audio,
621 .vidioc_s_audio = vidioc_s_audio,
622 .vidioc_g_input = vidioc_g_input,
623 .vidioc_s_input = vidioc_s_input,
626 /* check if the device is present and register with v4l and usb if it is */
627 static int usb_dsbr100_probe(struct usb_interface *intf,
628 const struct usb_device_id *id)
630 struct dsbr100_device *radio;
631 struct v4l2_device *v4l2_dev;
634 radio = kzalloc(sizeof(struct dsbr100_device), GFP_KERNEL);
639 radio->transfer_buffer = kmalloc(TB_LEN, GFP_KERNEL);
641 if (!(radio->transfer_buffer)) {
646 v4l2_dev = &radio->v4l2_dev;
648 retval = v4l2_device_register(&intf->dev, v4l2_dev);
650 v4l2_err(v4l2_dev, "couldn't register v4l2_device\n");
651 kfree(radio->transfer_buffer);
656 strlcpy(radio->videodev.name, v4l2_dev->name, sizeof(radio->videodev.name));
657 radio->videodev.v4l2_dev = v4l2_dev;
658 radio->videodev.fops = &usb_dsbr100_fops;
659 radio->videodev.ioctl_ops = &usb_dsbr100_ioctl_ops;
660 radio->videodev.release = usb_dsbr100_video_device_release;
662 mutex_init(&radio->lock);
665 radio->usbdev = interface_to_usbdev(intf);
666 radio->curfreq = FREQ_MIN * FREQ_MUL;
667 radio->status = STOPPED;
669 video_set_drvdata(&radio->videodev, radio);
671 retval = video_register_device(&radio->videodev, VFL_TYPE_RADIO, radio_nr);
673 v4l2_err(v4l2_dev, "couldn't register video device\n");
674 v4l2_device_unregister(v4l2_dev);
675 kfree(radio->transfer_buffer);
679 usb_set_intfdata(intf, radio);
683 static int __init dsbr100_init(void)
685 int retval = usb_register(&usb_dsbr100_driver);
686 printk(KERN_INFO KBUILD_MODNAME ": " DRIVER_VERSION ":"
691 static void __exit dsbr100_exit(void)
693 usb_deregister(&usb_dsbr100_driver);
696 module_init (dsbr100_init);
697 module_exit (dsbr100_exit);
699 MODULE_AUTHOR( DRIVER_AUTHOR );
700 MODULE_DESCRIPTION( DRIVER_DESC );
701 MODULE_LICENSE("GPL");