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 Add suspend/resume functions, fix unplug of device,
38 a lot of cleanups and fixes by Alexey Klimov <klimov.linux@gmail.com>
41 Oliver Neukum: avoided DMA coherency issue
44 Converted dsbr100 to use video_ioctl2
45 by Douglas Landgraf <dougsland@gmail.com>
48 Alan Cox: Some cleanups and fixes
51 Converted to V4L2 API by Mauro Carvalho Chehab <mchehab@infradead.org>
54 Markus: Updates for 2.6.x kernels, code layout changes, name sanitizing
57 Markus: Updates for 2.5.x kernel and more ISO compliant source
60 PSL and Markus: Cleanup, radio now doesn't stop on device close
63 Markus: Hope I got these silly VIDEO_TUNER_LOW issues finally
64 right. Some minor cleanup, improved standalone compilation
67 Markus: Sign extension bug fixed by declaring transfer_buffer unsigned
70 Markus: Some (brown bag) cleanup in what VIDIOCSTUNER returns,
71 thanks to Mike Cox for pointing the problem out.
74 Markus: Minor cleanup, warnings if something goes wrong, lame attempt
75 to adhere to Documentation/CodingStyle
78 Brad Hards <bradh@dynamite.com.au>: Fixes to make it work as non-module
79 Markus: Copyright clarification
81 Version 0.01: Markus: initial release
85 #include <linux/kernel.h>
86 #include <linux/module.h>
87 #include <linux/init.h>
88 #include <linux/slab.h>
89 #include <linux/input.h>
90 #include <linux/videodev2.h>
91 #include <media/v4l2-common.h>
92 #include <media/v4l2-ioctl.h>
93 #include <linux/usb.h>
98 #include <linux/version.h> /* for KERNEL_VERSION MACRO */
100 #define DRIVER_VERSION "v0.44"
101 #define RADIO_VERSION KERNEL_VERSION(0, 4, 4)
103 static struct v4l2_queryctrl radio_qctrl
[] = {
105 .id
= V4L2_CID_AUDIO_MUTE
,
110 .type
= V4L2_CTRL_TYPE_BOOLEAN
,
112 /* HINT: the disabled controls are only here to satify kradio and such apps */
113 { .id
= V4L2_CID_AUDIO_VOLUME
,
114 .flags
= V4L2_CTRL_FLAG_DISABLED
,
117 .id
= V4L2_CID_AUDIO_BALANCE
,
118 .flags
= V4L2_CTRL_FLAG_DISABLED
,
121 .id
= V4L2_CID_AUDIO_BASS
,
122 .flags
= V4L2_CTRL_FLAG_DISABLED
,
125 .id
= V4L2_CID_AUDIO_TREBLE
,
126 .flags
= V4L2_CTRL_FLAG_DISABLED
,
129 .id
= V4L2_CID_AUDIO_LOUDNESS
,
130 .flags
= V4L2_CTRL_FLAG_DISABLED
,
134 #define DRIVER_AUTHOR "Markus Demleitner <msdemlei@tucana.harvard.edu>"
135 #define DRIVER_DESC "D-Link DSB-R100 USB FM radio driver"
137 #define DSB100_VENDOR 0x04b4
138 #define DSB100_PRODUCT 0x1002
140 /* Commands the device appears to understand */
141 #define DSB100_TUNE 1
142 #define DSB100_ONOFF 2
146 /* Frequency limits in MHz -- these are European values. For Japanese
147 devices, that would be 76 and 91. */
148 #define FREQ_MIN 87.5
149 #define FREQ_MAX 108.0
150 #define FREQ_MUL 16000
152 #define videodev_to_radio(d) container_of(d, struct dsbr100_device, videodev)
154 static int usb_dsbr100_probe(struct usb_interface
*intf
,
155 const struct usb_device_id
*id
);
156 static void usb_dsbr100_disconnect(struct usb_interface
*intf
);
157 static int usb_dsbr100_open(struct file
*file
);
158 static int usb_dsbr100_close(struct file
*file
);
159 static int usb_dsbr100_suspend(struct usb_interface
*intf
,
160 pm_message_t message
);
161 static int usb_dsbr100_resume(struct usb_interface
*intf
);
163 static int radio_nr
= -1;
164 module_param(radio_nr
, int, 0);
166 /* Data for one (physical) device */
167 struct dsbr100_device
{
168 struct usb_device
*usbdev
;
169 struct video_device videodev
;
171 struct mutex lock
; /* buffer locking */
179 static struct usb_device_id usb_dsbr100_device_table
[] = {
180 { USB_DEVICE(DSB100_VENDOR
, DSB100_PRODUCT
) },
181 { } /* Terminating entry */
184 MODULE_DEVICE_TABLE (usb
, usb_dsbr100_device_table
);
186 /* USB subsystem interface */
187 static struct usb_driver usb_dsbr100_driver
= {
189 .probe
= usb_dsbr100_probe
,
190 .disconnect
= usb_dsbr100_disconnect
,
191 .id_table
= usb_dsbr100_device_table
,
192 .suspend
= usb_dsbr100_suspend
,
193 .resume
= usb_dsbr100_resume
,
194 .reset_resume
= usb_dsbr100_resume
,
195 .supports_autosuspend
= 0,
198 /* Low-level device interface begins here */
200 /* switch on radio */
201 static int dsbr100_start(struct dsbr100_device
*radio
)
206 mutex_lock(&radio
->lock
);
208 retval
= usb_control_msg(radio
->usbdev
,
209 usb_rcvctrlpipe(radio
->usbdev
, 0),
211 USB_TYPE_VENDOR
| USB_RECIP_DEVICE
| USB_DIR_IN
,
212 0x00, 0xC7, radio
->transfer_buffer
, 8, 300);
215 request
= USB_REQ_GET_STATUS
;
216 goto usb_control_msg_failed
;
219 retval
= usb_control_msg(radio
->usbdev
,
220 usb_rcvctrlpipe(radio
->usbdev
, 0),
222 USB_TYPE_VENDOR
| USB_RECIP_DEVICE
| USB_DIR_IN
,
223 0x01, 0x00, radio
->transfer_buffer
, 8, 300);
226 request
= DSB100_ONOFF
;
227 goto usb_control_msg_failed
;
231 mutex_unlock(&radio
->lock
);
232 return (radio
->transfer_buffer
)[0];
234 usb_control_msg_failed
:
235 mutex_unlock(&radio
->lock
);
236 dev_err(&radio
->usbdev
->dev
,
237 "%s - usb_control_msg returned %i, request %i\n",
238 __func__
, retval
, request
);
243 /* switch off radio */
244 static int dsbr100_stop(struct dsbr100_device
*radio
)
249 mutex_lock(&radio
->lock
);
251 retval
= usb_control_msg(radio
->usbdev
,
252 usb_rcvctrlpipe(radio
->usbdev
, 0),
254 USB_TYPE_VENDOR
| USB_RECIP_DEVICE
| USB_DIR_IN
,
255 0x16, 0x1C, radio
->transfer_buffer
, 8, 300);
258 request
= USB_REQ_GET_STATUS
;
259 goto usb_control_msg_failed
;
262 retval
= usb_control_msg(radio
->usbdev
,
263 usb_rcvctrlpipe(radio
->usbdev
, 0),
265 USB_TYPE_VENDOR
| USB_RECIP_DEVICE
| USB_DIR_IN
,
266 0x00, 0x00, radio
->transfer_buffer
, 8, 300);
269 request
= DSB100_ONOFF
;
270 goto usb_control_msg_failed
;
274 mutex_unlock(&radio
->lock
);
275 return (radio
->transfer_buffer
)[0];
277 usb_control_msg_failed
:
278 mutex_unlock(&radio
->lock
);
279 dev_err(&radio
->usbdev
->dev
,
280 "%s - usb_control_msg returned %i, request %i\n",
281 __func__
, retval
, request
);
286 /* set a frequency, freq is defined by v4l's TUNER_LOW, i.e. 1/16th kHz */
287 static int dsbr100_setfreq(struct dsbr100_device
*radio
, int freq
)
292 freq
= (freq
/ 16 * 80) / 1000 + 856;
293 mutex_lock(&radio
->lock
);
295 retval
= usb_control_msg(radio
->usbdev
,
296 usb_rcvctrlpipe(radio
->usbdev
, 0),
298 USB_TYPE_VENDOR
| USB_RECIP_DEVICE
| USB_DIR_IN
,
299 (freq
>> 8) & 0x00ff, freq
& 0xff,
300 radio
->transfer_buffer
, 8, 300);
303 request
= DSB100_TUNE
;
304 goto usb_control_msg_failed
;
307 retval
= usb_control_msg(radio
->usbdev
,
308 usb_rcvctrlpipe(radio
->usbdev
, 0),
310 USB_TYPE_VENDOR
| USB_RECIP_DEVICE
| USB_DIR_IN
,
311 0x96, 0xB7, radio
->transfer_buffer
, 8, 300);
314 request
= USB_REQ_GET_STATUS
;
315 goto usb_control_msg_failed
;
318 retval
= usb_control_msg(radio
->usbdev
,
319 usb_rcvctrlpipe(radio
->usbdev
, 0),
321 USB_TYPE_VENDOR
| USB_RECIP_DEVICE
| USB_DIR_IN
,
322 0x00, 0x24, radio
->transfer_buffer
, 8, 300);
325 request
= USB_REQ_GET_STATUS
;
326 goto usb_control_msg_failed
;
329 radio
->stereo
= !((radio
->transfer_buffer
)[0] & 0x01);
330 mutex_unlock(&radio
->lock
);
331 return (radio
->transfer_buffer
)[0];
333 usb_control_msg_failed
:
335 mutex_unlock(&radio
->lock
);
336 dev_err(&radio
->usbdev
->dev
,
337 "%s - usb_control_msg returned %i, request %i\n",
338 __func__
, retval
, request
);
342 /* return the device status. This is, in effect, just whether it
343 sees a stereo signal or not. Pity. */
344 static void dsbr100_getstat(struct dsbr100_device
*radio
)
348 mutex_lock(&radio
->lock
);
350 retval
= usb_control_msg(radio
->usbdev
,
351 usb_rcvctrlpipe(radio
->usbdev
, 0),
353 USB_TYPE_VENDOR
| USB_RECIP_DEVICE
| USB_DIR_IN
,
354 0x00 , 0x24, radio
->transfer_buffer
, 8, 300);
358 dev_err(&radio
->usbdev
->dev
,
359 "%s - usb_control_msg returned %i, request %i\n",
360 __func__
, retval
, USB_REQ_GET_STATUS
);
362 radio
->stereo
= !(radio
->transfer_buffer
[0] & 0x01);
365 mutex_unlock(&radio
->lock
);
368 /* USB subsystem interface begins here */
371 * Handle unplugging of the device.
372 * We call video_unregister_device in any case.
373 * The last function called in this procedure is
374 * usb_dsbr100_video_device_release
376 static void usb_dsbr100_disconnect(struct usb_interface
*intf
)
378 struct dsbr100_device
*radio
= usb_get_intfdata(intf
);
380 usb_set_intfdata (intf
, NULL
);
382 mutex_lock(&radio
->lock
);
384 mutex_unlock(&radio
->lock
);
386 video_unregister_device(&radio
->videodev
);
390 static int vidioc_querycap(struct file
*file
, void *priv
,
391 struct v4l2_capability
*v
)
393 strlcpy(v
->driver
, "dsbr100", sizeof(v
->driver
));
394 strlcpy(v
->card
, "D-Link R-100 USB FM Radio", sizeof(v
->card
));
395 sprintf(v
->bus_info
, "USB");
396 v
->version
= RADIO_VERSION
;
397 v
->capabilities
= V4L2_CAP_TUNER
;
401 static int vidioc_g_tuner(struct file
*file
, void *priv
,
402 struct v4l2_tuner
*v
)
404 struct dsbr100_device
*radio
= video_drvdata(file
);
413 dsbr100_getstat(radio
);
414 strcpy(v
->name
, "FM");
415 v
->type
= V4L2_TUNER_RADIO
;
416 v
->rangelow
= FREQ_MIN
* FREQ_MUL
;
417 v
->rangehigh
= FREQ_MAX
* FREQ_MUL
;
418 v
->rxsubchans
= V4L2_TUNER_SUB_MONO
| V4L2_TUNER_SUB_STEREO
;
419 v
->capability
= V4L2_TUNER_CAP_LOW
;
421 v
->audmode
= V4L2_TUNER_MODE_STEREO
;
423 v
->audmode
= V4L2_TUNER_MODE_MONO
;
424 v
->signal
= 0xffff; /* We can't get the signal strength */
428 static int vidioc_s_tuner(struct file
*file
, void *priv
,
429 struct v4l2_tuner
*v
)
431 struct dsbr100_device
*radio
= video_drvdata(file
);
443 static int vidioc_s_frequency(struct file
*file
, void *priv
,
444 struct v4l2_frequency
*f
)
446 struct dsbr100_device
*radio
= video_drvdata(file
);
453 radio
->curfreq
= f
->frequency
;
454 retval
= dsbr100_setfreq(radio
, radio
->curfreq
);
456 dev_warn(&radio
->usbdev
->dev
, "Set frequency failed\n");
460 static int vidioc_g_frequency(struct file
*file
, void *priv
,
461 struct v4l2_frequency
*f
)
463 struct dsbr100_device
*radio
= video_drvdata(file
);
469 f
->type
= V4L2_TUNER_RADIO
;
470 f
->frequency
= radio
->curfreq
;
474 static int vidioc_queryctrl(struct file
*file
, void *priv
,
475 struct v4l2_queryctrl
*qc
)
479 for (i
= 0; i
< ARRAY_SIZE(radio_qctrl
); i
++) {
480 if (qc
->id
&& qc
->id
== radio_qctrl
[i
].id
) {
481 memcpy(qc
, &(radio_qctrl
[i
]), sizeof(*qc
));
488 static int vidioc_g_ctrl(struct file
*file
, void *priv
,
489 struct v4l2_control
*ctrl
)
491 struct dsbr100_device
*radio
= video_drvdata(file
);
498 case V4L2_CID_AUDIO_MUTE
:
499 ctrl
->value
= radio
->muted
;
505 static int vidioc_s_ctrl(struct file
*file
, void *priv
,
506 struct v4l2_control
*ctrl
)
508 struct dsbr100_device
*radio
= video_drvdata(file
);
516 case V4L2_CID_AUDIO_MUTE
:
518 retval
= dsbr100_stop(radio
);
520 dev_warn(&radio
->usbdev
->dev
,
521 "Radio did not respond properly\n");
525 retval
= dsbr100_start(radio
);
527 dev_warn(&radio
->usbdev
->dev
,
528 "Radio did not respond properly\n");
537 static int vidioc_g_audio(struct file
*file
, void *priv
,
538 struct v4l2_audio
*a
)
543 strcpy(a
->name
, "Radio");
544 a
->capability
= V4L2_AUDCAP_STEREO
;
548 static int vidioc_g_input(struct file
*filp
, void *priv
, unsigned int *i
)
554 static int vidioc_s_input(struct file
*filp
, void *priv
, unsigned int i
)
561 static int vidioc_s_audio(struct file
*file
, void *priv
,
562 struct v4l2_audio
*a
)
569 static int usb_dsbr100_open(struct file
*file
)
571 struct dsbr100_device
*radio
= video_drvdata(file
);
578 retval
= dsbr100_start(radio
);
580 dev_warn(&radio
->usbdev
->dev
,
581 "Radio did not start up properly\n");
587 retval
= dsbr100_setfreq(radio
, radio
->curfreq
);
589 dev_warn(&radio
->usbdev
->dev
,
590 "set frequency failed\n");
596 static int usb_dsbr100_close(struct file
*file
)
598 struct dsbr100_device
*radio
= video_drvdata(file
);
605 if (!radio
->removed
) {
606 retval
= dsbr100_stop(radio
);
608 dev_warn(&radio
->usbdev
->dev
,
609 "dsbr100_stop failed\n");
616 /* Suspend device - stop device. */
617 static int usb_dsbr100_suspend(struct usb_interface
*intf
, pm_message_t message
)
619 struct dsbr100_device
*radio
= usb_get_intfdata(intf
);
622 retval
= dsbr100_stop(radio
);
624 dev_warn(&intf
->dev
, "dsbr100_stop failed\n");
626 dev_info(&intf
->dev
, "going into suspend..\n");
631 /* Resume device - start device. */
632 static int usb_dsbr100_resume(struct usb_interface
*intf
)
634 struct dsbr100_device
*radio
= usb_get_intfdata(intf
);
637 retval
= dsbr100_start(radio
);
639 dev_warn(&intf
->dev
, "dsbr100_start failed\n");
641 dev_info(&intf
->dev
, "coming out of suspend..\n");
646 /* free data structures */
647 static void usb_dsbr100_video_device_release(struct video_device
*videodev
)
649 struct dsbr100_device
*radio
= videodev_to_radio(videodev
);
651 kfree(radio
->transfer_buffer
);
655 /* File system interface */
656 static const struct v4l2_file_operations usb_dsbr100_fops
= {
657 .owner
= THIS_MODULE
,
658 .open
= usb_dsbr100_open
,
659 .release
= usb_dsbr100_close
,
660 .ioctl
= video_ioctl2
,
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");