1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /* A driver for the D-Link DSB-R100 USB radio and Gemtek USB Radio 21.
3 * The device plugs into both the USB and an analog audio input, so this thing
4 * only deals with initialisation and frequency setting, the
5 * audio data has to be handled by a sound driver.
7 * Major issue: I can't find out where the device reports the signal
8 * strength, and indeed the windows software appearantly just looks
9 * at the stereo indicator as well. So, scanning will only find
10 * stereo stations. Sad, but I can't help it.
12 * Also, the windows program sends oodles of messages over to the
13 * device, and I couldn't figure out their meaning. My suspicion
14 * is that they don't have any:-)
16 * You might find some interesting stuff about this module at
17 * http://unimut.fsk.uni-heidelberg.de/unimut/demi/dsbr
19 * Fully tested with the Keene USB FM Transmitter and the v4l2-compliance tool.
21 * Copyright (c) 2000 Markus Demleitner <msdemlei@cl.uni-heidelberg.de>
24 #include <linux/kernel.h>
25 #include <linux/module.h>
26 #include <linux/init.h>
27 #include <linux/slab.h>
28 #include <linux/input.h>
29 #include <linux/videodev2.h>
30 #include <linux/usb.h>
31 #include <media/v4l2-device.h>
32 #include <media/v4l2-ioctl.h>
33 #include <media/v4l2-ctrls.h>
34 #include <media/v4l2-event.h>
39 MODULE_AUTHOR("Markus Demleitner <msdemlei@tucana.harvard.edu>");
40 MODULE_DESCRIPTION("D-Link DSB-R100 USB FM radio driver");
41 MODULE_LICENSE("GPL");
42 MODULE_VERSION("1.1.0");
44 #define DSB100_VENDOR 0x04b4
45 #define DSB100_PRODUCT 0x1002
47 /* Commands the device appears to understand */
49 #define DSB100_ONOFF 2
53 /* Frequency limits in MHz -- these are European values. For Japanese
54 devices, that would be 76 and 91. */
56 #define FREQ_MAX 108.0
57 #define FREQ_MUL 16000
59 #define v4l2_dev_to_radio(d) container_of(d, struct dsbr100_device, v4l2_dev)
61 static int radio_nr
= -1;
62 module_param(radio_nr
, int, 0);
64 /* Data for one (physical) device */
65 struct dsbr100_device
{
66 struct usb_device
*usbdev
;
67 struct video_device videodev
;
68 struct v4l2_device v4l2_dev
;
69 struct v4l2_ctrl_handler hdl
;
72 struct mutex v4l2_lock
;
78 /* Low-level device interface begins here */
80 /* set a frequency, freq is defined by v4l's TUNER_LOW, i.e. 1/16th kHz */
81 static int dsbr100_setfreq(struct dsbr100_device
*radio
, unsigned freq
)
83 unsigned f
= (freq
/ 16 * 80) / 1000 + 856;
87 retval
= usb_control_msg(radio
->usbdev
,
88 usb_rcvctrlpipe(radio
->usbdev
, 0),
90 USB_TYPE_VENDOR
| USB_RECIP_DEVICE
| USB_DIR_IN
,
91 (f
>> 8) & 0x00ff, f
& 0xff,
92 radio
->transfer_buffer
, 8, 300);
98 radio
->curfreq
= freq
;
101 dev_err(&radio
->usbdev
->dev
,
102 "%s - usb_control_msg returned %i, request %i\n",
103 __func__
, retval
, DSB100_TUNE
);
107 /* switch on radio */
108 static int dsbr100_start(struct dsbr100_device
*radio
)
110 int retval
= usb_control_msg(radio
->usbdev
,
111 usb_rcvctrlpipe(radio
->usbdev
, 0),
113 USB_TYPE_VENDOR
| USB_RECIP_DEVICE
| USB_DIR_IN
,
114 0x01, 0x00, radio
->transfer_buffer
, 8, 300);
117 return dsbr100_setfreq(radio
, radio
->curfreq
);
118 dev_err(&radio
->usbdev
->dev
,
119 "%s - usb_control_msg returned %i, request %i\n",
120 __func__
, retval
, DSB100_ONOFF
);
125 /* switch off radio */
126 static int dsbr100_stop(struct dsbr100_device
*radio
)
128 int retval
= usb_control_msg(radio
->usbdev
,
129 usb_rcvctrlpipe(radio
->usbdev
, 0),
131 USB_TYPE_VENDOR
| USB_RECIP_DEVICE
| USB_DIR_IN
,
132 0x00, 0x00, radio
->transfer_buffer
, 8, 300);
136 dev_err(&radio
->usbdev
->dev
,
137 "%s - usb_control_msg returned %i, request %i\n",
138 __func__
, retval
, DSB100_ONOFF
);
143 /* return the device status. This is, in effect, just whether it
144 sees a stereo signal or not. Pity. */
145 static void dsbr100_getstat(struct dsbr100_device
*radio
)
147 int retval
= usb_control_msg(radio
->usbdev
,
148 usb_rcvctrlpipe(radio
->usbdev
, 0),
150 USB_TYPE_VENDOR
| USB_RECIP_DEVICE
| USB_DIR_IN
,
151 0x00, 0x24, radio
->transfer_buffer
, 8, 300);
154 radio
->stereo
= false;
155 dev_err(&radio
->usbdev
->dev
,
156 "%s - usb_control_msg returned %i, request %i\n",
157 __func__
, retval
, USB_REQ_GET_STATUS
);
159 radio
->stereo
= !(radio
->transfer_buffer
[0] & 0x01);
163 static int vidioc_querycap(struct file
*file
, void *priv
,
164 struct v4l2_capability
*v
)
166 struct dsbr100_device
*radio
= video_drvdata(file
);
168 strscpy(v
->driver
, "dsbr100", sizeof(v
->driver
));
169 strscpy(v
->card
, "D-Link R-100 USB FM Radio", sizeof(v
->card
));
170 usb_make_path(radio
->usbdev
, v
->bus_info
, sizeof(v
->bus_info
));
171 v
->device_caps
= V4L2_CAP_RADIO
| V4L2_CAP_TUNER
;
172 v
->capabilities
= v
->device_caps
| V4L2_CAP_DEVICE_CAPS
;
176 static int vidioc_g_tuner(struct file
*file
, void *priv
,
177 struct v4l2_tuner
*v
)
179 struct dsbr100_device
*radio
= video_drvdata(file
);
184 dsbr100_getstat(radio
);
185 strscpy(v
->name
, "FM", sizeof(v
->name
));
186 v
->type
= V4L2_TUNER_RADIO
;
187 v
->rangelow
= FREQ_MIN
* FREQ_MUL
;
188 v
->rangehigh
= FREQ_MAX
* FREQ_MUL
;
189 v
->rxsubchans
= radio
->stereo
? V4L2_TUNER_SUB_STEREO
:
191 v
->capability
= V4L2_TUNER_CAP_LOW
| V4L2_TUNER_CAP_STEREO
;
192 v
->audmode
= V4L2_TUNER_MODE_STEREO
;
193 v
->signal
= radio
->stereo
? 0xffff : 0; /* We can't get the signal strength */
197 static int vidioc_s_tuner(struct file
*file
, void *priv
,
198 const struct v4l2_tuner
*v
)
200 return v
->index
? -EINVAL
: 0;
203 static int vidioc_s_frequency(struct file
*file
, void *priv
,
204 const struct v4l2_frequency
*f
)
206 struct dsbr100_device
*radio
= video_drvdata(file
);
208 if (f
->tuner
!= 0 || f
->type
!= V4L2_TUNER_RADIO
)
211 return dsbr100_setfreq(radio
, clamp_t(unsigned, f
->frequency
,
212 FREQ_MIN
* FREQ_MUL
, FREQ_MAX
* FREQ_MUL
));
215 static int vidioc_g_frequency(struct file
*file
, void *priv
,
216 struct v4l2_frequency
*f
)
218 struct dsbr100_device
*radio
= video_drvdata(file
);
222 f
->type
= V4L2_TUNER_RADIO
;
223 f
->frequency
= radio
->curfreq
;
227 static int usb_dsbr100_s_ctrl(struct v4l2_ctrl
*ctrl
)
229 struct dsbr100_device
*radio
=
230 container_of(ctrl
->handler
, struct dsbr100_device
, hdl
);
233 case V4L2_CID_AUDIO_MUTE
:
234 radio
->muted
= ctrl
->val
;
235 return radio
->muted
? dsbr100_stop(radio
) : dsbr100_start(radio
);
241 /* USB subsystem interface begins here */
244 * Handle unplugging of the device.
245 * We call video_unregister_device in any case.
246 * The last function called in this procedure is
247 * usb_dsbr100_video_device_release
249 static void usb_dsbr100_disconnect(struct usb_interface
*intf
)
251 struct dsbr100_device
*radio
= usb_get_intfdata(intf
);
253 mutex_lock(&radio
->v4l2_lock
);
255 * Disconnect is also called on unload, and in that case we need to
256 * mute the device. This call will silently fail if it is called
257 * after a physical disconnect.
259 usb_control_msg(radio
->usbdev
,
260 usb_rcvctrlpipe(radio
->usbdev
, 0),
262 USB_TYPE_VENDOR
| USB_RECIP_DEVICE
| USB_DIR_IN
,
263 0x00, 0x00, radio
->transfer_buffer
, 8, 300);
264 usb_set_intfdata(intf
, NULL
);
265 video_unregister_device(&radio
->videodev
);
266 v4l2_device_disconnect(&radio
->v4l2_dev
);
267 mutex_unlock(&radio
->v4l2_lock
);
268 v4l2_device_put(&radio
->v4l2_dev
);
272 /* Suspend device - stop device. */
273 static int usb_dsbr100_suspend(struct usb_interface
*intf
, pm_message_t message
)
275 struct dsbr100_device
*radio
= usb_get_intfdata(intf
);
277 mutex_lock(&radio
->v4l2_lock
);
278 if (!radio
->muted
&& dsbr100_stop(radio
) < 0)
279 dev_warn(&intf
->dev
, "dsbr100_stop failed\n");
280 mutex_unlock(&radio
->v4l2_lock
);
282 dev_info(&intf
->dev
, "going into suspend..\n");
286 /* Resume device - start device. */
287 static int usb_dsbr100_resume(struct usb_interface
*intf
)
289 struct dsbr100_device
*radio
= usb_get_intfdata(intf
);
291 mutex_lock(&radio
->v4l2_lock
);
292 if (!radio
->muted
&& dsbr100_start(radio
) < 0)
293 dev_warn(&intf
->dev
, "dsbr100_start failed\n");
294 mutex_unlock(&radio
->v4l2_lock
);
296 dev_info(&intf
->dev
, "coming out of suspend..\n");
300 /* free data structures */
301 static void usb_dsbr100_release(struct v4l2_device
*v4l2_dev
)
303 struct dsbr100_device
*radio
= v4l2_dev_to_radio(v4l2_dev
);
305 v4l2_ctrl_handler_free(&radio
->hdl
);
306 v4l2_device_unregister(&radio
->v4l2_dev
);
307 kfree(radio
->transfer_buffer
);
311 static const struct v4l2_ctrl_ops usb_dsbr100_ctrl_ops
= {
312 .s_ctrl
= usb_dsbr100_s_ctrl
,
315 /* File system interface */
316 static const struct v4l2_file_operations usb_dsbr100_fops
= {
317 .owner
= THIS_MODULE
,
318 .unlocked_ioctl
= video_ioctl2
,
319 .open
= v4l2_fh_open
,
320 .release
= v4l2_fh_release
,
321 .poll
= v4l2_ctrl_poll
,
324 static const struct v4l2_ioctl_ops usb_dsbr100_ioctl_ops
= {
325 .vidioc_querycap
= vidioc_querycap
,
326 .vidioc_g_tuner
= vidioc_g_tuner
,
327 .vidioc_s_tuner
= vidioc_s_tuner
,
328 .vidioc_g_frequency
= vidioc_g_frequency
,
329 .vidioc_s_frequency
= vidioc_s_frequency
,
330 .vidioc_log_status
= v4l2_ctrl_log_status
,
331 .vidioc_subscribe_event
= v4l2_ctrl_subscribe_event
,
332 .vidioc_unsubscribe_event
= v4l2_event_unsubscribe
,
335 /* check if the device is present and register with v4l and usb if it is */
336 static int usb_dsbr100_probe(struct usb_interface
*intf
,
337 const struct usb_device_id
*id
)
339 struct dsbr100_device
*radio
;
340 struct v4l2_device
*v4l2_dev
;
343 radio
= kzalloc(sizeof(struct dsbr100_device
), GFP_KERNEL
);
348 radio
->transfer_buffer
= kmalloc(TB_LEN
, GFP_KERNEL
);
350 if (!(radio
->transfer_buffer
)) {
355 v4l2_dev
= &radio
->v4l2_dev
;
356 v4l2_dev
->release
= usb_dsbr100_release
;
358 retval
= v4l2_device_register(&intf
->dev
, v4l2_dev
);
360 v4l2_err(v4l2_dev
, "couldn't register v4l2_device\n");
364 v4l2_ctrl_handler_init(&radio
->hdl
, 1);
365 v4l2_ctrl_new_std(&radio
->hdl
, &usb_dsbr100_ctrl_ops
,
366 V4L2_CID_AUDIO_MUTE
, 0, 1, 1, 1);
367 if (radio
->hdl
.error
) {
368 retval
= radio
->hdl
.error
;
369 v4l2_err(v4l2_dev
, "couldn't register control\n");
372 mutex_init(&radio
->v4l2_lock
);
373 strscpy(radio
->videodev
.name
, v4l2_dev
->name
,
374 sizeof(radio
->videodev
.name
));
375 radio
->videodev
.v4l2_dev
= v4l2_dev
;
376 radio
->videodev
.fops
= &usb_dsbr100_fops
;
377 radio
->videodev
.ioctl_ops
= &usb_dsbr100_ioctl_ops
;
378 radio
->videodev
.release
= video_device_release_empty
;
379 radio
->videodev
.lock
= &radio
->v4l2_lock
;
380 radio
->videodev
.ctrl_handler
= &radio
->hdl
;
382 radio
->usbdev
= interface_to_usbdev(intf
);
383 radio
->curfreq
= FREQ_MIN
* FREQ_MUL
;
386 video_set_drvdata(&radio
->videodev
, radio
);
387 usb_set_intfdata(intf
, radio
);
389 retval
= video_register_device(&radio
->videodev
, VFL_TYPE_RADIO
, radio_nr
);
392 v4l2_err(v4l2_dev
, "couldn't register video device\n");
395 v4l2_ctrl_handler_free(&radio
->hdl
);
396 v4l2_device_unregister(v4l2_dev
);
398 kfree(radio
->transfer_buffer
);
403 static const struct usb_device_id usb_dsbr100_device_table
[] = {
404 { USB_DEVICE(DSB100_VENDOR
, DSB100_PRODUCT
) },
405 { } /* Terminating entry */
408 MODULE_DEVICE_TABLE(usb
, usb_dsbr100_device_table
);
410 /* USB subsystem interface */
411 static struct usb_driver usb_dsbr100_driver
= {
413 .probe
= usb_dsbr100_probe
,
414 .disconnect
= usb_dsbr100_disconnect
,
415 .id_table
= usb_dsbr100_device_table
,
416 .suspend
= usb_dsbr100_suspend
,
417 .resume
= usb_dsbr100_resume
,
418 .reset_resume
= usb_dsbr100_resume
,
421 module_usb_driver(usb_dsbr100_driver
);