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 v4l2_dev_to_radio(d) container_of(d, struct dsbr100_device, v4l2_dev)
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 v4l2_lock
;
157 static struct usb_device_id usb_dsbr100_device_table
[] = {
158 { USB_DEVICE(DSB100_VENDOR
, DSB100_PRODUCT
) },
159 { } /* Terminating entry */
162 MODULE_DEVICE_TABLE (usb
, usb_dsbr100_device_table
);
164 /* USB subsystem interface */
165 static struct usb_driver usb_dsbr100_driver
= {
167 .probe
= usb_dsbr100_probe
,
168 .disconnect
= usb_dsbr100_disconnect
,
169 .id_table
= usb_dsbr100_device_table
,
170 .suspend
= usb_dsbr100_suspend
,
171 .resume
= usb_dsbr100_resume
,
172 .reset_resume
= usb_dsbr100_resume
,
173 .supports_autosuspend
= 0,
176 /* Low-level device interface begins here */
178 /* switch on radio */
179 static int dsbr100_start(struct dsbr100_device
*radio
)
184 retval
= usb_control_msg(radio
->usbdev
,
185 usb_rcvctrlpipe(radio
->usbdev
, 0),
187 USB_TYPE_VENDOR
| USB_RECIP_DEVICE
| USB_DIR_IN
,
188 0x00, 0xC7, radio
->transfer_buffer
, 8, 300);
191 request
= USB_REQ_GET_STATUS
;
192 goto usb_control_msg_failed
;
195 retval
= usb_control_msg(radio
->usbdev
,
196 usb_rcvctrlpipe(radio
->usbdev
, 0),
198 USB_TYPE_VENDOR
| USB_RECIP_DEVICE
| USB_DIR_IN
,
199 0x01, 0x00, radio
->transfer_buffer
, 8, 300);
202 request
= DSB100_ONOFF
;
203 goto usb_control_msg_failed
;
206 radio
->status
= STARTED
;
207 return (radio
->transfer_buffer
)[0];
209 usb_control_msg_failed
:
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 retval
= usb_control_msg(radio
->usbdev
,
224 usb_rcvctrlpipe(radio
->usbdev
, 0),
226 USB_TYPE_VENDOR
| USB_RECIP_DEVICE
| USB_DIR_IN
,
227 0x16, 0x1C, radio
->transfer_buffer
, 8, 300);
230 request
= USB_REQ_GET_STATUS
;
231 goto usb_control_msg_failed
;
234 retval
= usb_control_msg(radio
->usbdev
,
235 usb_rcvctrlpipe(radio
->usbdev
, 0),
237 USB_TYPE_VENDOR
| USB_RECIP_DEVICE
| USB_DIR_IN
,
238 0x00, 0x00, radio
->transfer_buffer
, 8, 300);
241 request
= DSB100_ONOFF
;
242 goto usb_control_msg_failed
;
245 radio
->status
= STOPPED
;
246 return (radio
->transfer_buffer
)[0];
248 usb_control_msg_failed
:
249 dev_err(&radio
->usbdev
->dev
,
250 "%s - usb_control_msg returned %i, request %i\n",
251 __func__
, retval
, request
);
256 /* set a frequency, freq is defined by v4l's TUNER_LOW, i.e. 1/16th kHz */
257 static int dsbr100_setfreq(struct dsbr100_device
*radio
)
261 int freq
= (radio
->curfreq
/ 16 * 80) / 1000 + 856;
263 retval
= usb_control_msg(radio
->usbdev
,
264 usb_rcvctrlpipe(radio
->usbdev
, 0),
266 USB_TYPE_VENDOR
| USB_RECIP_DEVICE
| USB_DIR_IN
,
267 (freq
>> 8) & 0x00ff, freq
& 0xff,
268 radio
->transfer_buffer
, 8, 300);
271 request
= DSB100_TUNE
;
272 goto usb_control_msg_failed
;
275 retval
= usb_control_msg(radio
->usbdev
,
276 usb_rcvctrlpipe(radio
->usbdev
, 0),
278 USB_TYPE_VENDOR
| USB_RECIP_DEVICE
| USB_DIR_IN
,
279 0x96, 0xB7, radio
->transfer_buffer
, 8, 300);
282 request
= USB_REQ_GET_STATUS
;
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 0x00, 0x24, radio
->transfer_buffer
, 8, 300);
293 request
= USB_REQ_GET_STATUS
;
294 goto usb_control_msg_failed
;
297 radio
->stereo
= !((radio
->transfer_buffer
)[0] & 0x01);
298 return (radio
->transfer_buffer
)[0];
300 usb_control_msg_failed
:
302 dev_err(&radio
->usbdev
->dev
,
303 "%s - usb_control_msg returned %i, request %i\n",
304 __func__
, retval
, request
);
308 /* return the device status. This is, in effect, just whether it
309 sees a stereo signal or not. Pity. */
310 static void dsbr100_getstat(struct dsbr100_device
*radio
)
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);
322 dev_err(&radio
->usbdev
->dev
,
323 "%s - usb_control_msg returned %i, request %i\n",
324 __func__
, retval
, USB_REQ_GET_STATUS
);
326 radio
->stereo
= !(radio
->transfer_buffer
[0] & 0x01);
330 static int vidioc_querycap(struct file
*file
, void *priv
,
331 struct v4l2_capability
*v
)
333 struct dsbr100_device
*radio
= video_drvdata(file
);
335 strlcpy(v
->driver
, "dsbr100", sizeof(v
->driver
));
336 strlcpy(v
->card
, "D-Link R-100 USB FM Radio", sizeof(v
->card
));
337 usb_make_path(radio
->usbdev
, v
->bus_info
, sizeof(v
->bus_info
));
338 v
->version
= RADIO_VERSION
;
339 v
->capabilities
= V4L2_CAP_TUNER
;
343 static int vidioc_g_tuner(struct file
*file
, void *priv
,
344 struct v4l2_tuner
*v
)
346 struct dsbr100_device
*radio
= video_drvdata(file
);
351 dsbr100_getstat(radio
);
352 strcpy(v
->name
, "FM");
353 v
->type
= V4L2_TUNER_RADIO
;
354 v
->rangelow
= FREQ_MIN
* FREQ_MUL
;
355 v
->rangehigh
= FREQ_MAX
* FREQ_MUL
;
356 v
->rxsubchans
= V4L2_TUNER_SUB_MONO
| V4L2_TUNER_SUB_STEREO
;
357 v
->capability
= V4L2_TUNER_CAP_LOW
;
359 v
->audmode
= V4L2_TUNER_MODE_STEREO
;
361 v
->audmode
= V4L2_TUNER_MODE_MONO
;
362 v
->signal
= 0xffff; /* We can't get the signal strength */
366 static int vidioc_s_tuner(struct file
*file
, void *priv
,
367 struct v4l2_tuner
*v
)
369 return v
->index
? -EINVAL
: 0;
372 static int vidioc_s_frequency(struct file
*file
, void *priv
,
373 struct v4l2_frequency
*f
)
375 struct dsbr100_device
*radio
= video_drvdata(file
);
378 radio
->curfreq
= f
->frequency
;
380 retval
= dsbr100_setfreq(radio
);
382 dev_warn(&radio
->usbdev
->dev
, "Set frequency failed\n");
386 static int vidioc_g_frequency(struct file
*file
, void *priv
,
387 struct v4l2_frequency
*f
)
389 struct dsbr100_device
*radio
= video_drvdata(file
);
391 f
->type
= V4L2_TUNER_RADIO
;
392 f
->frequency
= radio
->curfreq
;
396 static int vidioc_queryctrl(struct file
*file
, void *priv
,
397 struct v4l2_queryctrl
*qc
)
400 case V4L2_CID_AUDIO_MUTE
:
401 return v4l2_ctrl_query_fill(qc
, 0, 1, 1, 1);
407 static int vidioc_g_ctrl(struct file
*file
, void *priv
,
408 struct v4l2_control
*ctrl
)
410 struct dsbr100_device
*radio
= video_drvdata(file
);
413 case V4L2_CID_AUDIO_MUTE
:
414 ctrl
->value
= radio
->status
;
420 static int vidioc_s_ctrl(struct file
*file
, void *priv
,
421 struct v4l2_control
*ctrl
)
423 struct dsbr100_device
*radio
= video_drvdata(file
);
427 case V4L2_CID_AUDIO_MUTE
:
429 retval
= dsbr100_stop(radio
);
431 dev_warn(&radio
->usbdev
->dev
,
432 "Radio did not respond properly\n");
436 retval
= dsbr100_start(radio
);
438 dev_warn(&radio
->usbdev
->dev
,
439 "Radio did not respond properly\n");
448 static int vidioc_g_audio(struct file
*file
, void *priv
,
449 struct v4l2_audio
*a
)
454 strcpy(a
->name
, "Radio");
455 a
->capability
= V4L2_AUDCAP_STEREO
;
459 static int vidioc_g_input(struct file
*filp
, void *priv
, unsigned int *i
)
465 static int vidioc_s_input(struct file
*filp
, void *priv
, unsigned int i
)
467 return i
? -EINVAL
: 0;
470 static int vidioc_s_audio(struct file
*file
, void *priv
,
471 struct v4l2_audio
*a
)
473 return a
->index
? -EINVAL
: 0;
476 /* USB subsystem interface begins here */
479 * Handle unplugging of the device.
480 * We call video_unregister_device in any case.
481 * The last function called in this procedure is
482 * usb_dsbr100_video_device_release
484 static void usb_dsbr100_disconnect(struct usb_interface
*intf
)
486 struct dsbr100_device
*radio
= usb_get_intfdata(intf
);
488 v4l2_device_get(&radio
->v4l2_dev
);
489 mutex_lock(&radio
->v4l2_lock
);
490 usb_set_intfdata(intf
, NULL
);
491 video_unregister_device(&radio
->videodev
);
492 v4l2_device_disconnect(&radio
->v4l2_dev
);
493 mutex_unlock(&radio
->v4l2_lock
);
494 v4l2_device_put(&radio
->v4l2_dev
);
498 /* Suspend device - stop device. */
499 static int usb_dsbr100_suspend(struct usb_interface
*intf
, pm_message_t message
)
501 struct dsbr100_device
*radio
= usb_get_intfdata(intf
);
504 mutex_lock(&radio
->v4l2_lock
);
505 if (radio
->status
== STARTED
) {
506 retval
= dsbr100_stop(radio
);
508 dev_warn(&intf
->dev
, "dsbr100_stop failed\n");
510 /* After dsbr100_stop() status set to STOPPED.
511 * If we want driver to start radio on resume
512 * we set status equal to STARTED.
513 * On resume we will check status and run radio if needed.
515 radio
->status
= STARTED
;
517 mutex_unlock(&radio
->v4l2_lock
);
519 dev_info(&intf
->dev
, "going into suspend..\n");
524 /* Resume device - start device. */
525 static int usb_dsbr100_resume(struct usb_interface
*intf
)
527 struct dsbr100_device
*radio
= usb_get_intfdata(intf
);
530 mutex_lock(&radio
->v4l2_lock
);
531 if (radio
->status
== STARTED
) {
532 retval
= dsbr100_start(radio
);
534 dev_warn(&intf
->dev
, "dsbr100_start failed\n");
536 mutex_unlock(&radio
->v4l2_lock
);
538 dev_info(&intf
->dev
, "coming out of suspend..\n");
543 /* free data structures */
544 static void usb_dsbr100_release(struct v4l2_device
*v4l2_dev
)
546 struct dsbr100_device
*radio
= v4l2_dev_to_radio(v4l2_dev
);
548 v4l2_device_unregister(&radio
->v4l2_dev
);
549 kfree(radio
->transfer_buffer
);
553 /* File system interface */
554 static const struct v4l2_file_operations usb_dsbr100_fops
= {
555 .owner
= THIS_MODULE
,
556 .unlocked_ioctl
= video_ioctl2
,
559 static const struct v4l2_ioctl_ops usb_dsbr100_ioctl_ops
= {
560 .vidioc_querycap
= vidioc_querycap
,
561 .vidioc_g_tuner
= vidioc_g_tuner
,
562 .vidioc_s_tuner
= vidioc_s_tuner
,
563 .vidioc_g_frequency
= vidioc_g_frequency
,
564 .vidioc_s_frequency
= vidioc_s_frequency
,
565 .vidioc_queryctrl
= vidioc_queryctrl
,
566 .vidioc_g_ctrl
= vidioc_g_ctrl
,
567 .vidioc_s_ctrl
= vidioc_s_ctrl
,
568 .vidioc_g_audio
= vidioc_g_audio
,
569 .vidioc_s_audio
= vidioc_s_audio
,
570 .vidioc_g_input
= vidioc_g_input
,
571 .vidioc_s_input
= vidioc_s_input
,
574 /* check if the device is present and register with v4l and usb if it is */
575 static int usb_dsbr100_probe(struct usb_interface
*intf
,
576 const struct usb_device_id
*id
)
578 struct dsbr100_device
*radio
;
579 struct v4l2_device
*v4l2_dev
;
582 radio
= kzalloc(sizeof(struct dsbr100_device
), GFP_KERNEL
);
587 radio
->transfer_buffer
= kmalloc(TB_LEN
, GFP_KERNEL
);
589 if (!(radio
->transfer_buffer
)) {
594 v4l2_dev
= &radio
->v4l2_dev
;
595 v4l2_dev
->release
= usb_dsbr100_release
;
597 retval
= v4l2_device_register(&intf
->dev
, v4l2_dev
);
599 v4l2_err(v4l2_dev
, "couldn't register v4l2_device\n");
600 kfree(radio
->transfer_buffer
);
605 mutex_init(&radio
->v4l2_lock
);
606 strlcpy(radio
->videodev
.name
, v4l2_dev
->name
, sizeof(radio
->videodev
.name
));
607 radio
->videodev
.v4l2_dev
= v4l2_dev
;
608 radio
->videodev
.fops
= &usb_dsbr100_fops
;
609 radio
->videodev
.ioctl_ops
= &usb_dsbr100_ioctl_ops
;
610 radio
->videodev
.release
= video_device_release_empty
;
611 radio
->videodev
.lock
= &radio
->v4l2_lock
;
613 radio
->usbdev
= interface_to_usbdev(intf
);
614 radio
->curfreq
= FREQ_MIN
* FREQ_MUL
;
615 radio
->status
= STOPPED
;
617 video_set_drvdata(&radio
->videodev
, radio
);
619 retval
= video_register_device(&radio
->videodev
, VFL_TYPE_RADIO
, radio_nr
);
621 v4l2_err(v4l2_dev
, "couldn't register video device\n");
622 v4l2_device_unregister(v4l2_dev
);
623 kfree(radio
->transfer_buffer
);
627 usb_set_intfdata(intf
, radio
);
631 static int __init
dsbr100_init(void)
633 int retval
= usb_register(&usb_dsbr100_driver
);
634 printk(KERN_INFO KBUILD_MODNAME
": " DRIVER_VERSION
":"
639 static void __exit
dsbr100_exit(void)
641 usb_deregister(&usb_dsbr100_driver
);
644 module_init (dsbr100_init
);
645 module_exit (dsbr100_exit
);
647 MODULE_AUTHOR( DRIVER_AUTHOR
);
648 MODULE_DESCRIPTION( DRIVER_DESC
);
649 MODULE_LICENSE("GPL");