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