1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37#include <linux/kernel.h>
38#include <linux/module.h>
39#include <linux/init.h>
40#include <linux/slab.h>
41#include <linux/input.h>
42#include <linux/videodev2.h>
43#include <linux/usb.h>
44#include <media/v4l2-device.h>
45#include <media/v4l2-ioctl.h>
46#include <media/v4l2-ctrls.h>
47#include <media/v4l2-event.h>
48
49
50
51
52MODULE_AUTHOR("Markus Demleitner <msdemlei@tucana.harvard.edu>");
53MODULE_DESCRIPTION("D-Link DSB-R100 USB FM radio driver");
54MODULE_LICENSE("GPL");
55MODULE_VERSION("1.1.0");
56
57#define DSB100_VENDOR 0x04b4
58#define DSB100_PRODUCT 0x1002
59
60
61#define DSB100_TUNE 1
62#define DSB100_ONOFF 2
63
64#define TB_LEN 16
65
66
67
68#define FREQ_MIN 87.5
69#define FREQ_MAX 108.0
70#define FREQ_MUL 16000
71
72#define v4l2_dev_to_radio(d) container_of(d, struct dsbr100_device, v4l2_dev)
73
74static int radio_nr = -1;
75module_param(radio_nr, int, 0);
76
77
78struct dsbr100_device {
79 struct usb_device *usbdev;
80 struct video_device videodev;
81 struct v4l2_device v4l2_dev;
82 struct v4l2_ctrl_handler hdl;
83
84 u8 *transfer_buffer;
85 struct mutex v4l2_lock;
86 int curfreq;
87 bool stereo;
88 bool muted;
89};
90
91
92
93
94static int dsbr100_setfreq(struct dsbr100_device *radio, unsigned freq)
95{
96 unsigned f = (freq / 16 * 80) / 1000 + 856;
97 int retval = 0;
98
99 if (!radio->muted) {
100 retval = usb_control_msg(radio->usbdev,
101 usb_rcvctrlpipe(radio->usbdev, 0),
102 DSB100_TUNE,
103 USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN,
104 (f >> 8) & 0x00ff, f & 0xff,
105 radio->transfer_buffer, 8, 300);
106 if (retval >= 0)
107 mdelay(1);
108 }
109
110 if (retval >= 0) {
111 radio->curfreq = freq;
112 return 0;
113 }
114 dev_err(&radio->usbdev->dev,
115 "%s - usb_control_msg returned %i, request %i\n",
116 __func__, retval, DSB100_TUNE);
117 return retval;
118}
119
120
121static int dsbr100_start(struct dsbr100_device *radio)
122{
123 int retval = usb_control_msg(radio->usbdev,
124 usb_rcvctrlpipe(radio->usbdev, 0),
125 DSB100_ONOFF,
126 USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN,
127 0x01, 0x00, radio->transfer_buffer, 8, 300);
128
129 if (retval >= 0)
130 return dsbr100_setfreq(radio, radio->curfreq);
131 dev_err(&radio->usbdev->dev,
132 "%s - usb_control_msg returned %i, request %i\n",
133 __func__, retval, DSB100_ONOFF);
134 return retval;
135
136}
137
138
139static int dsbr100_stop(struct dsbr100_device *radio)
140{
141 int retval = usb_control_msg(radio->usbdev,
142 usb_rcvctrlpipe(radio->usbdev, 0),
143 DSB100_ONOFF,
144 USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN,
145 0x00, 0x00, radio->transfer_buffer, 8, 300);
146
147 if (retval >= 0)
148 return 0;
149 dev_err(&radio->usbdev->dev,
150 "%s - usb_control_msg returned %i, request %i\n",
151 __func__, retval, DSB100_ONOFF);
152 return retval;
153
154}
155
156
157
158static void dsbr100_getstat(struct dsbr100_device *radio)
159{
160 int retval = usb_control_msg(radio->usbdev,
161 usb_rcvctrlpipe(radio->usbdev, 0),
162 USB_REQ_GET_STATUS,
163 USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN,
164 0x00, 0x24, radio->transfer_buffer, 8, 300);
165
166 if (retval < 0) {
167 radio->stereo = false;
168 dev_err(&radio->usbdev->dev,
169 "%s - usb_control_msg returned %i, request %i\n",
170 __func__, retval, USB_REQ_GET_STATUS);
171 } else {
172 radio->stereo = !(radio->transfer_buffer[0] & 0x01);
173 }
174}
175
176static int vidioc_querycap(struct file *file, void *priv,
177 struct v4l2_capability *v)
178{
179 struct dsbr100_device *radio = video_drvdata(file);
180
181 strlcpy(v->driver, "dsbr100", sizeof(v->driver));
182 strlcpy(v->card, "D-Link R-100 USB FM Radio", sizeof(v->card));
183 usb_make_path(radio->usbdev, v->bus_info, sizeof(v->bus_info));
184 v->device_caps = V4L2_CAP_RADIO | V4L2_CAP_TUNER;
185 v->capabilities = v->device_caps | V4L2_CAP_DEVICE_CAPS;
186 return 0;
187}
188
189static int vidioc_g_tuner(struct file *file, void *priv,
190 struct v4l2_tuner *v)
191{
192 struct dsbr100_device *radio = video_drvdata(file);
193
194 if (v->index > 0)
195 return -EINVAL;
196
197 dsbr100_getstat(radio);
198 strcpy(v->name, "FM");
199 v->type = V4L2_TUNER_RADIO;
200 v->rangelow = FREQ_MIN * FREQ_MUL;
201 v->rangehigh = FREQ_MAX * FREQ_MUL;
202 v->rxsubchans = radio->stereo ? V4L2_TUNER_SUB_STEREO :
203 V4L2_TUNER_SUB_MONO;
204 v->capability = V4L2_TUNER_CAP_LOW | V4L2_TUNER_CAP_STEREO;
205 v->audmode = V4L2_TUNER_MODE_STEREO;
206 v->signal = radio->stereo ? 0xffff : 0;
207 return 0;
208}
209
210static int vidioc_s_tuner(struct file *file, void *priv,
211 const struct v4l2_tuner *v)
212{
213 return v->index ? -EINVAL : 0;
214}
215
216static int vidioc_s_frequency(struct file *file, void *priv,
217 const struct v4l2_frequency *f)
218{
219 struct dsbr100_device *radio = video_drvdata(file);
220
221 if (f->tuner != 0 || f->type != V4L2_TUNER_RADIO)
222 return -EINVAL;
223
224 return dsbr100_setfreq(radio, clamp_t(unsigned, f->frequency,
225 FREQ_MIN * FREQ_MUL, FREQ_MAX * FREQ_MUL));
226}
227
228static int vidioc_g_frequency(struct file *file, void *priv,
229 struct v4l2_frequency *f)
230{
231 struct dsbr100_device *radio = video_drvdata(file);
232
233 if (f->tuner)
234 return -EINVAL;
235 f->type = V4L2_TUNER_RADIO;
236 f->frequency = radio->curfreq;
237 return 0;
238}
239
240static int usb_dsbr100_s_ctrl(struct v4l2_ctrl *ctrl)
241{
242 struct dsbr100_device *radio =
243 container_of(ctrl->handler, struct dsbr100_device, hdl);
244
245 switch (ctrl->id) {
246 case V4L2_CID_AUDIO_MUTE:
247 radio->muted = ctrl->val;
248 return radio->muted ? dsbr100_stop(radio) : dsbr100_start(radio);
249 }
250 return -EINVAL;
251}
252
253
254
255
256
257
258
259
260
261
262static void usb_dsbr100_disconnect(struct usb_interface *intf)
263{
264 struct dsbr100_device *radio = usb_get_intfdata(intf);
265
266 mutex_lock(&radio->v4l2_lock);
267
268
269
270
271
272 usb_control_msg(radio->usbdev,
273 usb_rcvctrlpipe(radio->usbdev, 0),
274 DSB100_ONOFF,
275 USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN,
276 0x00, 0x00, radio->transfer_buffer, 8, 300);
277 usb_set_intfdata(intf, NULL);
278 video_unregister_device(&radio->videodev);
279 v4l2_device_disconnect(&radio->v4l2_dev);
280 mutex_unlock(&radio->v4l2_lock);
281 v4l2_device_put(&radio->v4l2_dev);
282}
283
284
285
286static int usb_dsbr100_suspend(struct usb_interface *intf, pm_message_t message)
287{
288 struct dsbr100_device *radio = usb_get_intfdata(intf);
289
290 mutex_lock(&radio->v4l2_lock);
291 if (!radio->muted && dsbr100_stop(radio) < 0)
292 dev_warn(&intf->dev, "dsbr100_stop failed\n");
293 mutex_unlock(&radio->v4l2_lock);
294
295 dev_info(&intf->dev, "going into suspend..\n");
296 return 0;
297}
298
299
300static int usb_dsbr100_resume(struct usb_interface *intf)
301{
302 struct dsbr100_device *radio = usb_get_intfdata(intf);
303
304 mutex_lock(&radio->v4l2_lock);
305 if (!radio->muted && dsbr100_start(radio) < 0)
306 dev_warn(&intf->dev, "dsbr100_start failed\n");
307 mutex_unlock(&radio->v4l2_lock);
308
309 dev_info(&intf->dev, "coming out of suspend..\n");
310 return 0;
311}
312
313
314static void usb_dsbr100_release(struct v4l2_device *v4l2_dev)
315{
316 struct dsbr100_device *radio = v4l2_dev_to_radio(v4l2_dev);
317
318 v4l2_ctrl_handler_free(&radio->hdl);
319 v4l2_device_unregister(&radio->v4l2_dev);
320 kfree(radio->transfer_buffer);
321 kfree(radio);
322}
323
324static const struct v4l2_ctrl_ops usb_dsbr100_ctrl_ops = {
325 .s_ctrl = usb_dsbr100_s_ctrl,
326};
327
328
329static const struct v4l2_file_operations usb_dsbr100_fops = {
330 .owner = THIS_MODULE,
331 .unlocked_ioctl = video_ioctl2,
332 .open = v4l2_fh_open,
333 .release = v4l2_fh_release,
334 .poll = v4l2_ctrl_poll,
335};
336
337static const struct v4l2_ioctl_ops usb_dsbr100_ioctl_ops = {
338 .vidioc_querycap = vidioc_querycap,
339 .vidioc_g_tuner = vidioc_g_tuner,
340 .vidioc_s_tuner = vidioc_s_tuner,
341 .vidioc_g_frequency = vidioc_g_frequency,
342 .vidioc_s_frequency = vidioc_s_frequency,
343 .vidioc_log_status = v4l2_ctrl_log_status,
344 .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
345 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
346};
347
348
349static int usb_dsbr100_probe(struct usb_interface *intf,
350 const struct usb_device_id *id)
351{
352 struct dsbr100_device *radio;
353 struct v4l2_device *v4l2_dev;
354 int retval;
355
356 radio = kzalloc(sizeof(struct dsbr100_device), GFP_KERNEL);
357
358 if (!radio)
359 return -ENOMEM;
360
361 radio->transfer_buffer = kmalloc(TB_LEN, GFP_KERNEL);
362
363 if (!(radio->transfer_buffer)) {
364 kfree(radio);
365 return -ENOMEM;
366 }
367
368 v4l2_dev = &radio->v4l2_dev;
369 v4l2_dev->release = usb_dsbr100_release;
370
371 retval = v4l2_device_register(&intf->dev, v4l2_dev);
372 if (retval < 0) {
373 v4l2_err(v4l2_dev, "couldn't register v4l2_device\n");
374 goto err_reg_dev;
375 }
376
377 v4l2_ctrl_handler_init(&radio->hdl, 1);
378 v4l2_ctrl_new_std(&radio->hdl, &usb_dsbr100_ctrl_ops,
379 V4L2_CID_AUDIO_MUTE, 0, 1, 1, 1);
380 if (radio->hdl.error) {
381 retval = radio->hdl.error;
382 v4l2_err(v4l2_dev, "couldn't register control\n");
383 goto err_reg_ctrl;
384 }
385 mutex_init(&radio->v4l2_lock);
386 strlcpy(radio->videodev.name, v4l2_dev->name, sizeof(radio->videodev.name));
387 radio->videodev.v4l2_dev = v4l2_dev;
388 radio->videodev.fops = &usb_dsbr100_fops;
389 radio->videodev.ioctl_ops = &usb_dsbr100_ioctl_ops;
390 radio->videodev.release = video_device_release_empty;
391 radio->videodev.lock = &radio->v4l2_lock;
392 radio->videodev.ctrl_handler = &radio->hdl;
393
394 radio->usbdev = interface_to_usbdev(intf);
395 radio->curfreq = FREQ_MIN * FREQ_MUL;
396 radio->muted = true;
397
398 video_set_drvdata(&radio->videodev, radio);
399 usb_set_intfdata(intf, radio);
400
401 retval = video_register_device(&radio->videodev, VFL_TYPE_RADIO, radio_nr);
402 if (retval == 0)
403 return 0;
404 v4l2_err(v4l2_dev, "couldn't register video device\n");
405
406err_reg_ctrl:
407 v4l2_ctrl_handler_free(&radio->hdl);
408 v4l2_device_unregister(v4l2_dev);
409err_reg_dev:
410 kfree(radio->transfer_buffer);
411 kfree(radio);
412 return retval;
413}
414
415static struct usb_device_id usb_dsbr100_device_table[] = {
416 { USB_DEVICE(DSB100_VENDOR, DSB100_PRODUCT) },
417 { }
418};
419
420MODULE_DEVICE_TABLE(usb, usb_dsbr100_device_table);
421
422
423static struct usb_driver usb_dsbr100_driver = {
424 .name = "dsbr100",
425 .probe = usb_dsbr100_probe,
426 .disconnect = usb_dsbr100_disconnect,
427 .id_table = usb_dsbr100_device_table,
428 .suspend = usb_dsbr100_suspend,
429 .resume = usb_dsbr100_resume,
430 .reset_resume = usb_dsbr100_resume,
431};
432
433module_usb_driver(usb_dsbr100_driver);
434