]>
Commit | Line | Data |
---|---|---|
fc55bcb0 | 1 | /* A driver for the D-Link DSB-R100 USB radio and Gemtek USB Radio 21. |
5d778648 HV |
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. | |
5 | * | |
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. | |
10 | * | |
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:-) | |
14 | * | |
15 | * You might find some interesting stuff about this module at | |
16 | * http://unimut.fsk.uni-heidelberg.de/unimut/demi/dsbr | |
17 | * | |
18 | * Fully tested with the Keene USB FM Transmitter and the v4l2-compliance tool. | |
19 | * | |
20 | * Copyright (c) 2000 Markus Demleitner <msdemlei@cl.uni-heidelberg.de> | |
21 | * | |
22 | * This program is free software; you can redistribute it and/or modify | |
23 | * it under the terms of the GNU General Public License as published by | |
24 | * the Free Software Foundation; either version 2 of the License, or | |
25 | * (at your option) any later version. | |
26 | * | |
27 | * This program is distributed in the hope that it will be useful, | |
28 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
29 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
30 | * GNU General Public License for more details. | |
1da177e4 LT |
31 | */ |
32 | ||
1da177e4 LT |
33 | #include <linux/kernel.h> |
34 | #include <linux/module.h> | |
35 | #include <linux/init.h> | |
36 | #include <linux/slab.h> | |
37 | #include <linux/input.h> | |
5aff308c | 38 | #include <linux/videodev2.h> |
5d778648 | 39 | #include <linux/usb.h> |
406827cc | 40 | #include <media/v4l2-device.h> |
35ea11ff | 41 | #include <media/v4l2-ioctl.h> |
5d778648 HV |
42 | #include <media/v4l2-ctrls.h> |
43 | #include <media/v4l2-event.h> | |
1da177e4 LT |
44 | |
45 | /* | |
46 | * Version Information | |
47 | */ | |
5d778648 HV |
48 | MODULE_AUTHOR("Markus Demleitner <msdemlei@tucana.harvard.edu>"); |
49 | MODULE_DESCRIPTION("D-Link DSB-R100 USB FM radio driver"); | |
50 | MODULE_LICENSE("GPL"); | |
51 | MODULE_VERSION("1.1.0"); | |
1da177e4 LT |
52 | |
53 | #define DSB100_VENDOR 0x04b4 | |
54 | #define DSB100_PRODUCT 0x1002 | |
55 | ||
56 | /* Commands the device appears to understand */ | |
57 | #define DSB100_TUNE 1 | |
58 | #define DSB100_ONOFF 2 | |
59 | ||
60 | #define TB_LEN 16 | |
61 | ||
62 | /* Frequency limits in MHz -- these are European values. For Japanese | |
63 | devices, that would be 76 and 91. */ | |
64 | #define FREQ_MIN 87.5 | |
65 | #define FREQ_MAX 108.0 | |
66 | #define FREQ_MUL 16000 | |
67 | ||
13099294 | 68 | #define v4l2_dev_to_radio(d) container_of(d, struct dsbr100_device, v4l2_dev) |
1da177e4 | 69 | |
1da177e4 LT |
70 | static int radio_nr = -1; |
71 | module_param(radio_nr, int, 0); | |
72 | ||
73 | /* Data for one (physical) device */ | |
5aff308c | 74 | struct dsbr100_device { |
1da177e4 | 75 | struct usb_device *usbdev; |
3a0efc32 | 76 | struct video_device videodev; |
406827cc | 77 | struct v4l2_device v4l2_dev; |
5d778648 | 78 | struct v4l2_ctrl_handler hdl; |
406827cc | 79 | |
863c86dd | 80 | u8 *transfer_buffer; |
e64d07c9 | 81 | struct mutex v4l2_lock; |
1da177e4 | 82 | int curfreq; |
5d778648 HV |
83 | bool stereo; |
84 | bool muted; | |
1da177e4 LT |
85 | }; |
86 | ||
87 | /* Low-level device interface begins here */ | |
88 | ||
5d778648 HV |
89 | /* set a frequency, freq is defined by v4l's TUNER_LOW, i.e. 1/16th kHz */ |
90 | static int dsbr100_setfreq(struct dsbr100_device *radio, unsigned freq) | |
1da177e4 | 91 | { |
5d778648 HV |
92 | unsigned f = (freq / 16 * 80) / 1000 + 856; |
93 | int retval = 0; | |
94 | ||
95 | if (!radio->muted) { | |
96 | retval = usb_control_msg(radio->usbdev, | |
97 | usb_rcvctrlpipe(radio->usbdev, 0), | |
98 | DSB100_TUNE, | |
99 | USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN, | |
100 | (f >> 8) & 0x00ff, f & 0xff, | |
101 | radio->transfer_buffer, 8, 300); | |
102 | if (retval >= 0) | |
103 | mdelay(1); | |
3a0efc32 AK |
104 | } |
105 | ||
5d778648 HV |
106 | if (retval >= 0) { |
107 | radio->curfreq = freq; | |
108 | return 0; | |
417b7953 | 109 | } |
417b7953 AK |
110 | dev_err(&radio->usbdev->dev, |
111 | "%s - usb_control_msg returned %i, request %i\n", | |
5d778648 | 112 | __func__, retval, DSB100_TUNE); |
d25cb646 | 113 | return retval; |
1da177e4 LT |
114 | } |
115 | ||
5d778648 HV |
116 | /* switch on radio */ |
117 | static int dsbr100_start(struct dsbr100_device *radio) | |
1da177e4 | 118 | { |
5d778648 | 119 | int retval = usb_control_msg(radio->usbdev, |
417b7953 AK |
120 | usb_rcvctrlpipe(radio->usbdev, 0), |
121 | DSB100_ONOFF, | |
122 | USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN, | |
5d778648 | 123 | 0x01, 0x00, radio->transfer_buffer, 8, 300); |
417b7953 | 124 | |
5d778648 HV |
125 | if (retval >= 0) |
126 | return dsbr100_setfreq(radio, radio->curfreq); | |
417b7953 AK |
127 | dev_err(&radio->usbdev->dev, |
128 | "%s - usb_control_msg returned %i, request %i\n", | |
5d778648 | 129 | __func__, retval, DSB100_ONOFF); |
d25cb646 | 130 | return retval; |
417b7953 | 131 | |
1da177e4 LT |
132 | } |
133 | ||
5d778648 HV |
134 | /* switch off radio */ |
135 | static int dsbr100_stop(struct dsbr100_device *radio) | |
1da177e4 | 136 | { |
5d778648 | 137 | int retval = usb_control_msg(radio->usbdev, |
417b7953 | 138 | usb_rcvctrlpipe(radio->usbdev, 0), |
5d778648 | 139 | DSB100_ONOFF, |
417b7953 | 140 | USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN, |
5d778648 | 141 | 0x00, 0x00, radio->transfer_buffer, 8, 300); |
417b7953 | 142 | |
5d778648 HV |
143 | if (retval >= 0) |
144 | return 0; | |
417b7953 AK |
145 | dev_err(&radio->usbdev->dev, |
146 | "%s - usb_control_msg returned %i, request %i\n", | |
5d778648 | 147 | __func__, retval, DSB100_ONOFF); |
d25cb646 | 148 | return retval; |
5d778648 | 149 | |
1da177e4 LT |
150 | } |
151 | ||
152 | /* return the device status. This is, in effect, just whether it | |
153 | sees a stereo signal or not. Pity. */ | |
5aff308c | 154 | static void dsbr100_getstat(struct dsbr100_device *radio) |
1da177e4 | 155 | { |
5d778648 | 156 | int retval = usb_control_msg(radio->usbdev, |
417b7953 | 157 | usb_rcvctrlpipe(radio->usbdev, 0), |
d56410e0 | 158 | USB_REQ_GET_STATUS, |
1da177e4 | 159 | USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN, |
5d778648 | 160 | 0x00, 0x24, radio->transfer_buffer, 8, 300); |
417b7953 AK |
161 | |
162 | if (retval < 0) { | |
5d778648 | 163 | radio->stereo = false; |
417b7953 AK |
164 | dev_err(&radio->usbdev->dev, |
165 | "%s - usb_control_msg returned %i, request %i\n", | |
166 | __func__, retval, USB_REQ_GET_STATUS); | |
167 | } else { | |
223377e7 | 168 | radio->stereo = !(radio->transfer_buffer[0] & 0x01); |
417b7953 | 169 | } |
1da177e4 LT |
170 | } |
171 | ||
7002a4f3 DL |
172 | static int vidioc_querycap(struct file *file, void *priv, |
173 | struct v4l2_capability *v) | |
174 | { | |
c7181cfa AK |
175 | struct dsbr100_device *radio = video_drvdata(file); |
176 | ||
7002a4f3 DL |
177 | strlcpy(v->driver, "dsbr100", sizeof(v->driver)); |
178 | strlcpy(v->card, "D-Link R-100 USB FM Radio", sizeof(v->card)); | |
c7181cfa | 179 | usb_make_path(radio->usbdev, v->bus_info, sizeof(v->bus_info)); |
5d778648 HV |
180 | v->device_caps = V4L2_CAP_RADIO | V4L2_CAP_TUNER; |
181 | v->capabilities = v->device_caps | V4L2_CAP_DEVICE_CAPS; | |
7002a4f3 DL |
182 | return 0; |
183 | } | |
1da177e4 | 184 | |
7002a4f3 DL |
185 | static int vidioc_g_tuner(struct file *file, void *priv, |
186 | struct v4l2_tuner *v) | |
1da177e4 | 187 | { |
c170ecf4 | 188 | struct dsbr100_device *radio = video_drvdata(file); |
7002a4f3 DL |
189 | |
190 | if (v->index > 0) | |
191 | return -EINVAL; | |
192 | ||
193 | dsbr100_getstat(radio); | |
194 | strcpy(v->name, "FM"); | |
195 | v->type = V4L2_TUNER_RADIO; | |
223377e7 AK |
196 | v->rangelow = FREQ_MIN * FREQ_MUL; |
197 | v->rangehigh = FREQ_MAX * FREQ_MUL; | |
5d778648 HV |
198 | v->rxsubchans = radio->stereo ? V4L2_TUNER_SUB_STEREO : |
199 | V4L2_TUNER_SUB_MONO; | |
200 | v->capability = V4L2_TUNER_CAP_LOW | V4L2_TUNER_CAP_STEREO; | |
201 | v->audmode = V4L2_TUNER_MODE_STEREO; | |
202 | v->signal = radio->stereo ? 0xffff : 0; /* We can't get the signal strength */ | |
7002a4f3 DL |
203 | return 0; |
204 | } | |
1da177e4 | 205 | |
7002a4f3 | 206 | static int vidioc_s_tuner(struct file *file, void *priv, |
2f73c7c5 | 207 | const struct v4l2_tuner *v) |
7002a4f3 | 208 | { |
13099294 | 209 | return v->index ? -EINVAL : 0; |
7002a4f3 | 210 | } |
5aff308c | 211 | |
7002a4f3 | 212 | static int vidioc_s_frequency(struct file *file, void *priv, |
b530a447 | 213 | const struct v4l2_frequency *f) |
7002a4f3 | 214 | { |
c170ecf4 | 215 | struct dsbr100_device *radio = video_drvdata(file); |
1da177e4 | 216 | |
5d778648 HV |
217 | if (f->tuner != 0 || f->type != V4L2_TUNER_RADIO) |
218 | return -EINVAL; | |
fdf9c997 | 219 | |
5d778648 HV |
220 | return dsbr100_setfreq(radio, clamp_t(unsigned, f->frequency, |
221 | FREQ_MIN * FREQ_MUL, FREQ_MAX * FREQ_MUL)); | |
7002a4f3 | 222 | } |
5aff308c | 223 | |
7002a4f3 DL |
224 | static int vidioc_g_frequency(struct file *file, void *priv, |
225 | struct v4l2_frequency *f) | |
226 | { | |
c170ecf4 | 227 | struct dsbr100_device *radio = video_drvdata(file); |
5aff308c | 228 | |
5d778648 HV |
229 | if (f->tuner) |
230 | return -EINVAL; | |
7002a4f3 DL |
231 | f->type = V4L2_TUNER_RADIO; |
232 | f->frequency = radio->curfreq; | |
233 | return 0; | |
234 | } | |
5aff308c | 235 | |
5d778648 | 236 | static int usb_dsbr100_s_ctrl(struct v4l2_ctrl *ctrl) |
7002a4f3 | 237 | { |
5d778648 HV |
238 | struct dsbr100_device *radio = |
239 | container_of(ctrl->handler, struct dsbr100_device, hdl); | |
1da177e4 | 240 | |
7002a4f3 DL |
241 | switch (ctrl->id) { |
242 | case V4L2_CID_AUDIO_MUTE: | |
5d778648 HV |
243 | radio->muted = ctrl->val; |
244 | return radio->muted ? dsbr100_stop(radio) : dsbr100_start(radio); | |
7002a4f3 DL |
245 | } |
246 | return -EINVAL; | |
247 | } | |
5aff308c | 248 | |
1da177e4 | 249 | |
e64d07c9 HV |
250 | /* USB subsystem interface begins here */ |
251 | ||
252 | /* | |
253 | * Handle unplugging of the device. | |
254 | * We call video_unregister_device in any case. | |
255 | * The last function called in this procedure is | |
256 | * usb_dsbr100_video_device_release | |
257 | */ | |
258 | static void usb_dsbr100_disconnect(struct usb_interface *intf) | |
259 | { | |
260 | struct dsbr100_device *radio = usb_get_intfdata(intf); | |
261 | ||
e64d07c9 | 262 | mutex_lock(&radio->v4l2_lock); |
5d778648 HV |
263 | /* |
264 | * Disconnect is also called on unload, and in that case we need to | |
265 | * mute the device. This call will silently fail if it is called | |
266 | * after a physical disconnect. | |
267 | */ | |
268 | usb_control_msg(radio->usbdev, | |
269 | usb_rcvctrlpipe(radio->usbdev, 0), | |
270 | DSB100_ONOFF, | |
271 | USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN, | |
272 | 0x00, 0x00, radio->transfer_buffer, 8, 300); | |
13099294 | 273 | usb_set_intfdata(intf, NULL); |
e64d07c9 HV |
274 | video_unregister_device(&radio->videodev); |
275 | v4l2_device_disconnect(&radio->v4l2_dev); | |
13099294 HV |
276 | mutex_unlock(&radio->v4l2_lock); |
277 | v4l2_device_put(&radio->v4l2_dev); | |
e64d07c9 HV |
278 | } |
279 | ||
280 | ||
04e0ffbb AK |
281 | /* Suspend device - stop device. */ |
282 | static int usb_dsbr100_suspend(struct usb_interface *intf, pm_message_t message) | |
283 | { | |
284 | struct dsbr100_device *radio = usb_get_intfdata(intf); | |
04e0ffbb | 285 | |
e64d07c9 | 286 | mutex_lock(&radio->v4l2_lock); |
5d778648 HV |
287 | if (!radio->muted && dsbr100_stop(radio) < 0) |
288 | dev_warn(&intf->dev, "dsbr100_stop failed\n"); | |
e64d07c9 | 289 | mutex_unlock(&radio->v4l2_lock); |
04e0ffbb AK |
290 | |
291 | dev_info(&intf->dev, "going into suspend..\n"); | |
04e0ffbb AK |
292 | return 0; |
293 | } | |
294 | ||
295 | /* Resume device - start device. */ | |
296 | static int usb_dsbr100_resume(struct usb_interface *intf) | |
297 | { | |
298 | struct dsbr100_device *radio = usb_get_intfdata(intf); | |
04e0ffbb | 299 | |
e64d07c9 | 300 | mutex_lock(&radio->v4l2_lock); |
5d778648 HV |
301 | if (!radio->muted && dsbr100_start(radio) < 0) |
302 | dev_warn(&intf->dev, "dsbr100_start failed\n"); | |
e64d07c9 | 303 | mutex_unlock(&radio->v4l2_lock); |
04e0ffbb AK |
304 | |
305 | dev_info(&intf->dev, "coming out of suspend..\n"); | |
04e0ffbb AK |
306 | return 0; |
307 | } | |
308 | ||
fc55bcb0 | 309 | /* free data structures */ |
13099294 | 310 | static void usb_dsbr100_release(struct v4l2_device *v4l2_dev) |
3a0efc32 | 311 | { |
13099294 | 312 | struct dsbr100_device *radio = v4l2_dev_to_radio(v4l2_dev); |
3a0efc32 | 313 | |
5d778648 | 314 | v4l2_ctrl_handler_free(&radio->hdl); |
406827cc | 315 | v4l2_device_unregister(&radio->v4l2_dev); |
3a0efc32 AK |
316 | kfree(radio->transfer_buffer); |
317 | kfree(radio); | |
318 | } | |
319 | ||
5d778648 HV |
320 | static const struct v4l2_ctrl_ops usb_dsbr100_ctrl_ops = { |
321 | .s_ctrl = usb_dsbr100_s_ctrl, | |
322 | }; | |
323 | ||
7002a4f3 | 324 | /* File system interface */ |
bec43661 | 325 | static const struct v4l2_file_operations usb_dsbr100_fops = { |
7002a4f3 | 326 | .owner = THIS_MODULE, |
e64d07c9 | 327 | .unlocked_ioctl = video_ioctl2, |
5d778648 HV |
328 | .open = v4l2_fh_open, |
329 | .release = v4l2_fh_release, | |
330 | .poll = v4l2_ctrl_poll, | |
7002a4f3 DL |
331 | }; |
332 | ||
a399810c | 333 | static const struct v4l2_ioctl_ops usb_dsbr100_ioctl_ops = { |
7002a4f3 DL |
334 | .vidioc_querycap = vidioc_querycap, |
335 | .vidioc_g_tuner = vidioc_g_tuner, | |
336 | .vidioc_s_tuner = vidioc_s_tuner, | |
337 | .vidioc_g_frequency = vidioc_g_frequency, | |
338 | .vidioc_s_frequency = vidioc_s_frequency, | |
5d778648 HV |
339 | .vidioc_log_status = v4l2_ctrl_log_status, |
340 | .vidioc_subscribe_event = v4l2_ctrl_subscribe_event, | |
341 | .vidioc_unsubscribe_event = v4l2_event_unsubscribe, | |
7002a4f3 DL |
342 | }; |
343 | ||
fc55bcb0 | 344 | /* check if the device is present and register with v4l and usb if it is */ |
7002a4f3 DL |
345 | static int usb_dsbr100_probe(struct usb_interface *intf, |
346 | const struct usb_device_id *id) | |
347 | { | |
348 | struct dsbr100_device *radio; | |
406827cc | 349 | struct v4l2_device *v4l2_dev; |
417b7953 | 350 | int retval; |
7002a4f3 | 351 | |
406827cc | 352 | radio = kzalloc(sizeof(struct dsbr100_device), GFP_KERNEL); |
223377e7 AK |
353 | |
354 | if (!radio) | |
7002a4f3 | 355 | return -ENOMEM; |
223377e7 AK |
356 | |
357 | radio->transfer_buffer = kmalloc(TB_LEN, GFP_KERNEL); | |
358 | ||
359 | if (!(radio->transfer_buffer)) { | |
863c86dd ON |
360 | kfree(radio); |
361 | return -ENOMEM; | |
362 | } | |
223377e7 | 363 | |
406827cc | 364 | v4l2_dev = &radio->v4l2_dev; |
13099294 | 365 | v4l2_dev->release = usb_dsbr100_release; |
406827cc AK |
366 | |
367 | retval = v4l2_device_register(&intf->dev, v4l2_dev); | |
368 | if (retval < 0) { | |
369 | v4l2_err(v4l2_dev, "couldn't register v4l2_device\n"); | |
5d778648 | 370 | goto err_reg_dev; |
406827cc AK |
371 | } |
372 | ||
5d778648 HV |
373 | v4l2_ctrl_handler_init(&radio->hdl, 1); |
374 | v4l2_ctrl_new_std(&radio->hdl, &usb_dsbr100_ctrl_ops, | |
375 | V4L2_CID_AUDIO_MUTE, 0, 1, 1, 1); | |
376 | if (radio->hdl.error) { | |
377 | retval = radio->hdl.error; | |
378 | v4l2_err(v4l2_dev, "couldn't register control\n"); | |
379 | goto err_reg_ctrl; | |
380 | } | |
e64d07c9 | 381 | mutex_init(&radio->v4l2_lock); |
406827cc AK |
382 | strlcpy(radio->videodev.name, v4l2_dev->name, sizeof(radio->videodev.name)); |
383 | radio->videodev.v4l2_dev = v4l2_dev; | |
384 | radio->videodev.fops = &usb_dsbr100_fops; | |
385 | radio->videodev.ioctl_ops = &usb_dsbr100_ioctl_ops; | |
13099294 | 386 | radio->videodev.release = video_device_release_empty; |
e64d07c9 | 387 | radio->videodev.lock = &radio->v4l2_lock; |
5d778648 | 388 | radio->videodev.ctrl_handler = &radio->hdl; |
3a0efc32 | 389 | |
7002a4f3 | 390 | radio->usbdev = interface_to_usbdev(intf); |
223377e7 | 391 | radio->curfreq = FREQ_MIN * FREQ_MUL; |
5d778648 | 392 | radio->muted = true; |
406827cc | 393 | |
3a0efc32 | 394 | video_set_drvdata(&radio->videodev, radio); |
5d778648 | 395 | usb_set_intfdata(intf, radio); |
406827cc | 396 | |
417b7953 | 397 | retval = video_register_device(&radio->videodev, VFL_TYPE_RADIO, radio_nr); |
5d778648 HV |
398 | if (retval == 0) |
399 | return 0; | |
400 | v4l2_err(v4l2_dev, "couldn't register video device\n"); | |
401 | ||
402 | err_reg_ctrl: | |
403 | v4l2_ctrl_handler_free(&radio->hdl); | |
404 | v4l2_device_unregister(v4l2_dev); | |
405 | err_reg_dev: | |
406 | kfree(radio->transfer_buffer); | |
407 | kfree(radio); | |
408 | return retval; | |
7002a4f3 DL |
409 | } |
410 | ||
5d778648 HV |
411 | static struct usb_device_id usb_dsbr100_device_table[] = { |
412 | { USB_DEVICE(DSB100_VENDOR, DSB100_PRODUCT) }, | |
413 | { } /* Terminating entry */ | |
414 | }; | |
1da177e4 | 415 | |
5d778648 HV |
416 | MODULE_DEVICE_TABLE(usb, usb_dsbr100_device_table); |
417 | ||
418 | /* USB subsystem interface */ | |
419 | static struct usb_driver usb_dsbr100_driver = { | |
420 | .name = "dsbr100", | |
421 | .probe = usb_dsbr100_probe, | |
422 | .disconnect = usb_dsbr100_disconnect, | |
423 | .id_table = usb_dsbr100_device_table, | |
424 | .suspend = usb_dsbr100_suspend, | |
425 | .resume = usb_dsbr100_resume, | |
426 | .reset_resume = usb_dsbr100_resume, | |
427 | }; | |
428 | ||
429 | module_usb_driver(usb_dsbr100_driver); |