blob: a5ca176a7b083a80ef5201d2ac57d1a3475bbbbb [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* A driver for the D-Link DSB-R100 USB radio. The R100 plugs
2 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 Copyright (c) 2000 Markus Demleitner <msdemlei@cl.uni-heidelberg.de>
19
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.
24
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.
29
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
33
34 History:
35
Oliver Neukum863c86d2007-12-03 06:48:43 -030036 Version 0.43:
37 Oliver Neukum: avoided DMA coherency issue
38
Douglas Landgraf7002a4f2007-05-07 16:43:01 -030039 Version 0.42:
40 Converted dsbr100 to use video_ioctl2
41 by Douglas Landgraf <dougsland@gmail.com>
42
Alan Cox5aff3082006-08-08 15:47:50 -030043 Version 0.41-ac1:
44 Alan Cox: Some cleanups and fixes
45
46 Version 0.41:
47 Converted to V4L2 API by Mauro Carvalho Chehab <mchehab@infradead.org>
48
Linus Torvalds1da177e2005-04-16 15:20:36 -070049 Version 0.40:
Alan Cox5aff3082006-08-08 15:47:50 -030050 Markus: Updates for 2.6.x kernels, code layout changes, name sanitizing
Linus Torvalds1da177e2005-04-16 15:20:36 -070051
52 Version 0.30:
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -030053 Markus: Updates for 2.5.x kernel and more ISO compliant source
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
55 Version 0.25:
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -030056 PSL and Markus: Cleanup, radio now doesn't stop on device close
Linus Torvalds1da177e2005-04-16 15:20:36 -070057
58 Version 0.24:
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -030059 Markus: Hope I got these silly VIDEO_TUNER_LOW issues finally
Linus Torvalds1da177e2005-04-16 15:20:36 -070060 right. Some minor cleanup, improved standalone compilation
61
62 Version 0.23:
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -030063 Markus: Sign extension bug fixed by declaring transfer_buffer unsigned
Linus Torvalds1da177e2005-04-16 15:20:36 -070064
65 Version 0.22:
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -030066 Markus: Some (brown bag) cleanup in what VIDIOCSTUNER returns,
Linus Torvalds1da177e2005-04-16 15:20:36 -070067 thanks to Mike Cox for pointing the problem out.
68
69 Version 0.21:
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -030070 Markus: Minor cleanup, warnings if something goes wrong, lame attempt
Linus Torvalds1da177e2005-04-16 15:20:36 -070071 to adhere to Documentation/CodingStyle
72
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -030073 Version 0.2:
74 Brad Hards <bradh@dynamite.com.au>: Fixes to make it work as non-module
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 Markus: Copyright clarification
76
77 Version 0.01: Markus: initial release
78
79*/
80
Linus Torvalds1da177e2005-04-16 15:20:36 -070081#include <linux/kernel.h>
82#include <linux/module.h>
83#include <linux/init.h>
84#include <linux/slab.h>
85#include <linux/input.h>
Alan Cox5aff3082006-08-08 15:47:50 -030086#include <linux/videodev2.h>
Mauro Carvalho Chehab5e87efa2006-06-05 10:26:32 -030087#include <media/v4l2-common.h>
Hans Verkuil35ea11f2008-07-20 08:12:02 -030088#include <media/v4l2-ioctl.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070089#include <linux/usb.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070090
91/*
92 * Version Information
93 */
Alan Cox5aff3082006-08-08 15:47:50 -030094#include <linux/version.h> /* for KERNEL_VERSION MACRO */
95
96#define DRIVER_VERSION "v0.41"
97#define RADIO_VERSION KERNEL_VERSION(0,4,1)
98
99static struct v4l2_queryctrl radio_qctrl[] = {
100 {
101 .id = V4L2_CID_AUDIO_MUTE,
102 .name = "Mute",
103 .minimum = 0,
104 .maximum = 1,
105 .default_value = 1,
106 .type = V4L2_CTRL_TYPE_BOOLEAN,
107 }
108};
109
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110#define DRIVER_AUTHOR "Markus Demleitner <msdemlei@tucana.harvard.edu>"
111#define DRIVER_DESC "D-Link DSB-R100 USB FM radio driver"
112
113#define DSB100_VENDOR 0x04b4
114#define DSB100_PRODUCT 0x1002
115
116/* Commands the device appears to understand */
117#define DSB100_TUNE 1
118#define DSB100_ONOFF 2
119
120#define TB_LEN 16
121
122/* Frequency limits in MHz -- these are European values. For Japanese
123devices, that would be 76 and 91. */
124#define FREQ_MIN 87.5
125#define FREQ_MAX 108.0
126#define FREQ_MUL 16000
127
128
129static int usb_dsbr100_probe(struct usb_interface *intf,
130 const struct usb_device_id *id);
131static void usb_dsbr100_disconnect(struct usb_interface *intf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132static int usb_dsbr100_open(struct inode *inode, struct file *file);
133static int usb_dsbr100_close(struct inode *inode, struct file *file);
134
135static int radio_nr = -1;
136module_param(radio_nr, int, 0);
137
138/* Data for one (physical) device */
Alan Cox5aff3082006-08-08 15:47:50 -0300139struct dsbr100_device {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 struct usb_device *usbdev;
141 struct video_device *videodev;
Oliver Neukum863c86d2007-12-03 06:48:43 -0300142 u8 *transfer_buffer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 int curfreq;
144 int stereo;
145 int users;
146 int removed;
Alan Cox5aff3082006-08-08 15:47:50 -0300147 int muted;
148};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149
150
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151static struct usb_device_id usb_dsbr100_device_table [] = {
152 { USB_DEVICE(DSB100_VENDOR, DSB100_PRODUCT) },
153 { } /* Terminating entry */
154};
155
156MODULE_DEVICE_TABLE (usb, usb_dsbr100_device_table);
157
158/* USB subsystem interface */
159static struct usb_driver usb_dsbr100_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 .name = "dsbr100",
161 .probe = usb_dsbr100_probe,
162 .disconnect = usb_dsbr100_disconnect,
163 .id_table = usb_dsbr100_device_table,
164};
165
166/* Low-level device interface begins here */
167
168/* switch on radio */
Alan Cox5aff3082006-08-08 15:47:50 -0300169static int dsbr100_start(struct dsbr100_device *radio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170{
171 if (usb_control_msg(radio->usbdev, usb_rcvctrlpipe(radio->usbdev, 0),
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300172 USB_REQ_GET_STATUS,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN,
Alexey Klimov223377e2008-10-19 23:50:27 -0300174 0x00, 0xC7, radio->transfer_buffer, 8, 300) < 0 ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 usb_control_msg(radio->usbdev, usb_rcvctrlpipe(radio->usbdev, 0),
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300176 DSB100_ONOFF,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN,
Alexey Klimov223377e2008-10-19 23:50:27 -0300178 0x01, 0x00, radio->transfer_buffer, 8, 300) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 return -1;
Alan Cox5aff3082006-08-08 15:47:50 -0300180 radio->muted=0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 return (radio->transfer_buffer)[0];
182}
183
184
185/* switch off radio */
Alan Cox5aff3082006-08-08 15:47:50 -0300186static int dsbr100_stop(struct dsbr100_device *radio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187{
188 if (usb_control_msg(radio->usbdev, usb_rcvctrlpipe(radio->usbdev, 0),
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300189 USB_REQ_GET_STATUS,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN,
Alexey Klimov223377e2008-10-19 23:50:27 -0300191 0x16, 0x1C, radio->transfer_buffer, 8, 300) < 0 ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 usb_control_msg(radio->usbdev, usb_rcvctrlpipe(radio->usbdev, 0),
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300193 DSB100_ONOFF,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN,
Alexey Klimov223377e2008-10-19 23:50:27 -0300195 0x00, 0x00, radio->transfer_buffer, 8, 300) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 return -1;
Alan Cox5aff3082006-08-08 15:47:50 -0300197 radio->muted=1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 return (radio->transfer_buffer)[0];
199}
200
201/* set a frequency, freq is defined by v4l's TUNER_LOW, i.e. 1/16th kHz */
Alan Cox5aff3082006-08-08 15:47:50 -0300202static int dsbr100_setfreq(struct dsbr100_device *radio, int freq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203{
Alexey Klimov223377e2008-10-19 23:50:27 -0300204 freq = (freq / 16 * 80) / 1000 + 856;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 if (usb_control_msg(radio->usbdev, usb_rcvctrlpipe(radio->usbdev, 0),
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300206 DSB100_TUNE,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN,
Alexey Klimov223377e2008-10-19 23:50:27 -0300208 (freq >> 8) & 0x00ff, freq & 0xff,
209 radio->transfer_buffer, 8, 300) < 0 ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 usb_control_msg(radio->usbdev, usb_rcvctrlpipe(radio->usbdev, 0),
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300211 USB_REQ_GET_STATUS,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN,
Alexey Klimov223377e2008-10-19 23:50:27 -0300213 0x96, 0xB7, radio->transfer_buffer, 8, 300) < 0 ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 usb_control_msg(radio->usbdev, usb_rcvctrlpipe(radio->usbdev, 0),
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300215 USB_REQ_GET_STATUS,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN,
Alexey Klimov223377e2008-10-19 23:50:27 -0300217 0x00, 0x24, radio->transfer_buffer, 8, 300) < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 radio->stereo = -1;
219 return -1;
220 }
Alexey Klimov223377e2008-10-19 23:50:27 -0300221 radio->stereo = !((radio->transfer_buffer)[0] & 0x01);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 return (radio->transfer_buffer)[0];
223}
224
225/* return the device status. This is, in effect, just whether it
226sees a stereo signal or not. Pity. */
Alan Cox5aff3082006-08-08 15:47:50 -0300227static void dsbr100_getstat(struct dsbr100_device *radio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228{
229 if (usb_control_msg(radio->usbdev, usb_rcvctrlpipe(radio->usbdev, 0),
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300230 USB_REQ_GET_STATUS,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN,
Alexey Klimov223377e2008-10-19 23:50:27 -0300232 0x00 , 0x24, radio->transfer_buffer, 8, 300) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 radio->stereo = -1;
234 else
Alexey Klimov223377e2008-10-19 23:50:27 -0300235 radio->stereo = !(radio->transfer_buffer[0] & 0x01);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236}
237
238
239/* USB subsystem interface begins here */
240
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241/* handle unplugging of the device, release data structures
242if nothing keeps us from doing it. If something is still
243keeping us busy, the release callback of v4l will take care
Oliver Neukum863c86d2007-12-03 06:48:43 -0300244of releasing it. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245static void usb_dsbr100_disconnect(struct usb_interface *intf)
246{
Alan Cox5aff3082006-08-08 15:47:50 -0300247 struct dsbr100_device *radio = usb_get_intfdata(intf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248
249 usb_set_intfdata (intf, NULL);
250 if (radio) {
251 video_unregister_device(radio->videodev);
252 radio->videodev = NULL;
253 if (radio->users) {
Oliver Neukum863c86d2007-12-03 06:48:43 -0300254 kfree(radio->transfer_buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 kfree(radio);
256 } else {
257 radio->removed = 1;
258 }
259 }
260}
261
262
Douglas Landgraf7002a4f2007-05-07 16:43:01 -0300263static int vidioc_querycap(struct file *file, void *priv,
264 struct v4l2_capability *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265{
Douglas Landgraf7002a4f2007-05-07 16:43:01 -0300266 strlcpy(v->driver, "dsbr100", sizeof(v->driver));
267 strlcpy(v->card, "D-Link R-100 USB FM Radio", sizeof(v->card));
Alexey Klimov448441c2008-10-19 23:20:47 -0300268 sprintf(v->bus_info, "USB");
Douglas Landgraf7002a4f2007-05-07 16:43:01 -0300269 v->version = RADIO_VERSION;
270 v->capabilities = V4L2_CAP_TUNER;
271 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272}
273
Douglas Landgraf7002a4f2007-05-07 16:43:01 -0300274static int vidioc_g_tuner(struct file *file, void *priv,
275 struct v4l2_tuner *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276{
Hans Verkuilc170ecf2008-08-23 08:32:09 -0300277 struct dsbr100_device *radio = video_drvdata(file);
Douglas Landgraf7002a4f2007-05-07 16:43:01 -0300278
279 if (v->index > 0)
280 return -EINVAL;
281
282 dsbr100_getstat(radio);
283 strcpy(v->name, "FM");
284 v->type = V4L2_TUNER_RADIO;
Alexey Klimov223377e2008-10-19 23:50:27 -0300285 v->rangelow = FREQ_MIN * FREQ_MUL;
286 v->rangehigh = FREQ_MAX * FREQ_MUL;
287 v->rxsubchans = V4L2_TUNER_SUB_MONO | V4L2_TUNER_SUB_STEREO;
Douglas Landgraf7002a4f2007-05-07 16:43:01 -0300288 v->capability = V4L2_TUNER_CAP_LOW;
289 if(radio->stereo)
290 v->audmode = V4L2_TUNER_MODE_STEREO;
291 else
292 v->audmode = V4L2_TUNER_MODE_MONO;
293 v->signal = 0xffff; /* We can't get the signal strength */
294 return 0;
295}
296
297static int vidioc_s_tuner(struct file *file, void *priv,
298 struct v4l2_tuner *v)
299{
300 if (v->index > 0)
301 return -EINVAL;
302
303 return 0;
304}
305
306static int vidioc_s_frequency(struct file *file, void *priv,
307 struct v4l2_frequency *f)
308{
Hans Verkuilc170ecf2008-08-23 08:32:09 -0300309 struct dsbr100_device *radio = video_drvdata(file);
Douglas Landgraf7002a4f2007-05-07 16:43:01 -0300310
311 radio->curfreq = f->frequency;
Alexey Klimov223377e2008-10-19 23:50:27 -0300312 if (dsbr100_setfreq(radio, radio->curfreq) == -1)
Greg Kroah-Hartmanaa826612008-08-14 09:37:34 -0700313 dev_warn(&radio->usbdev->dev, "Set frequency failed\n");
Douglas Landgraf7002a4f2007-05-07 16:43:01 -0300314 return 0;
315}
316
317static int vidioc_g_frequency(struct file *file, void *priv,
318 struct v4l2_frequency *f)
319{
Hans Verkuilc170ecf2008-08-23 08:32:09 -0300320 struct dsbr100_device *radio = video_drvdata(file);
Douglas Landgraf7002a4f2007-05-07 16:43:01 -0300321
322 f->type = V4L2_TUNER_RADIO;
323 f->frequency = radio->curfreq;
324 return 0;
325}
326
327static int vidioc_queryctrl(struct file *file, void *priv,
328 struct v4l2_queryctrl *qc)
329{
330 int i;
331
332 for (i = 0; i < ARRAY_SIZE(radio_qctrl); i++) {
333 if (qc->id && qc->id == radio_qctrl[i].id) {
Alexey Klimov223377e2008-10-19 23:50:27 -0300334 memcpy(qc, &(radio_qctrl[i]), sizeof(*qc));
Douglas Landgraf7002a4f2007-05-07 16:43:01 -0300335 return 0;
336 }
337 }
338 return -EINVAL;
339}
340
341static int vidioc_g_ctrl(struct file *file, void *priv,
342 struct v4l2_control *ctrl)
343{
Hans Verkuilc170ecf2008-08-23 08:32:09 -0300344 struct dsbr100_device *radio = video_drvdata(file);
Douglas Landgraf7002a4f2007-05-07 16:43:01 -0300345
346 switch (ctrl->id) {
347 case V4L2_CID_AUDIO_MUTE:
348 ctrl->value = radio->muted;
349 return 0;
350 }
351 return -EINVAL;
352}
353
354static int vidioc_s_ctrl(struct file *file, void *priv,
355 struct v4l2_control *ctrl)
356{
Hans Verkuilc170ecf2008-08-23 08:32:09 -0300357 struct dsbr100_device *radio = video_drvdata(file);
Douglas Landgraf7002a4f2007-05-07 16:43:01 -0300358
359 switch (ctrl->id) {
360 case V4L2_CID_AUDIO_MUTE:
361 if (ctrl->value) {
Alexey Klimov90b698d2008-10-09 13:42:32 -0300362 if (dsbr100_stop(radio) == -1) {
Greg Kroah-Hartmanaa826612008-08-14 09:37:34 -0700363 dev_warn(&radio->usbdev->dev,
364 "Radio did not respond properly\n");
Alexey Klimov90b698d2008-10-09 13:42:32 -0300365 return -EBUSY;
366 }
Douglas Landgraf7002a4f2007-05-07 16:43:01 -0300367 } else {
Alexey Klimov90b698d2008-10-09 13:42:32 -0300368 if (dsbr100_start(radio) == -1) {
Greg Kroah-Hartmanaa826612008-08-14 09:37:34 -0700369 dev_warn(&radio->usbdev->dev,
370 "Radio did not respond properly\n");
Alexey Klimov90b698d2008-10-09 13:42:32 -0300371 return -EBUSY;
372 }
Douglas Landgraf7002a4f2007-05-07 16:43:01 -0300373 }
374 return 0;
375 }
376 return -EINVAL;
377}
378
379static int vidioc_g_audio(struct file *file, void *priv,
380 struct v4l2_audio *a)
381{
382 if (a->index > 1)
383 return -EINVAL;
384
385 strcpy(a->name, "Radio");
386 a->capability = V4L2_AUDCAP_STEREO;
387 return 0;
388}
389
390static int vidioc_g_input(struct file *filp, void *priv, unsigned int *i)
391{
392 *i = 0;
393 return 0;
394}
395
396static int vidioc_s_input(struct file *filp, void *priv, unsigned int i)
397{
398 if (i != 0)
399 return -EINVAL;
400 return 0;
401}
402
403static int vidioc_s_audio(struct file *file, void *priv,
404 struct v4l2_audio *a)
405{
406 if (a->index != 0)
407 return -EINVAL;
408 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409}
410
411static int usb_dsbr100_open(struct inode *inode, struct file *file)
412{
Hans Verkuilc170ecf2008-08-23 08:32:09 -0300413 struct dsbr100_device *radio = video_drvdata(file);
Alexey Klimovb9f35732008-10-20 00:00:03 -0300414 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415
Hans Verkuild56dc612008-07-30 08:43:36 -0300416 lock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 radio->users = 1;
Alan Cox5aff3082006-08-08 15:47:50 -0300418 radio->muted = 1;
419
Alexey Klimov223377e2008-10-19 23:50:27 -0300420 if (dsbr100_start(radio) < 0) {
Greg Kroah-Hartmanaa826612008-08-14 09:37:34 -0700421 dev_warn(&radio->usbdev->dev,
422 "Radio did not start up properly\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 radio->users = 0;
Hans Verkuild56dc612008-07-30 08:43:36 -0300424 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 return -EIO;
426 }
Alexey Klimovb9f35732008-10-20 00:00:03 -0300427
428 retval = dsbr100_setfreq(radio, radio->curfreq);
429
430 if (retval == -1)
431 printk(KERN_WARNING KBUILD_MODNAME ": Set frequency failed\n");
432
Hans Verkuild56dc612008-07-30 08:43:36 -0300433 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 return 0;
435}
436
437static int usb_dsbr100_close(struct inode *inode, struct file *file)
438{
Hans Verkuilc170ecf2008-08-23 08:32:09 -0300439 struct dsbr100_device *radio = video_drvdata(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440
441 if (!radio)
442 return -ENODEV;
443 radio->users = 0;
444 if (radio->removed) {
Oliver Neukum863c86d2007-12-03 06:48:43 -0300445 kfree(radio->transfer_buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 kfree(radio);
447 }
448 return 0;
449}
450
Douglas Landgraf7002a4f2007-05-07 16:43:01 -0300451/* File system interface */
452static const struct file_operations usb_dsbr100_fops = {
453 .owner = THIS_MODULE,
454 .open = usb_dsbr100_open,
455 .release = usb_dsbr100_close,
456 .ioctl = video_ioctl2,
Douglas Schilling Landgraf078ff792008-04-22 14:46:11 -0300457#ifdef CONFIG_COMPAT
Douglas Landgraf7002a4f2007-05-07 16:43:01 -0300458 .compat_ioctl = v4l_compat_ioctl32,
Douglas Schilling Landgraf078ff792008-04-22 14:46:11 -0300459#endif
Douglas Landgraf7002a4f2007-05-07 16:43:01 -0300460 .llseek = no_llseek,
461};
462
Hans Verkuila3998102008-07-21 02:57:38 -0300463static const struct v4l2_ioctl_ops usb_dsbr100_ioctl_ops = {
Douglas Landgraf7002a4f2007-05-07 16:43:01 -0300464 .vidioc_querycap = vidioc_querycap,
465 .vidioc_g_tuner = vidioc_g_tuner,
466 .vidioc_s_tuner = vidioc_s_tuner,
467 .vidioc_g_frequency = vidioc_g_frequency,
468 .vidioc_s_frequency = vidioc_s_frequency,
469 .vidioc_queryctrl = vidioc_queryctrl,
470 .vidioc_g_ctrl = vidioc_g_ctrl,
471 .vidioc_s_ctrl = vidioc_s_ctrl,
472 .vidioc_g_audio = vidioc_g_audio,
473 .vidioc_s_audio = vidioc_s_audio,
474 .vidioc_g_input = vidioc_g_input,
475 .vidioc_s_input = vidioc_s_input,
476};
477
Hans Verkuila3998102008-07-21 02:57:38 -0300478/* V4L2 interface */
479static struct video_device dsbr100_videodev_template = {
Hans Verkuila3998102008-07-21 02:57:38 -0300480 .name = "D-Link DSB-R 100",
Hans Verkuila3998102008-07-21 02:57:38 -0300481 .fops = &usb_dsbr100_fops,
482 .ioctl_ops = &usb_dsbr100_ioctl_ops,
483 .release = video_device_release,
484};
485
Douglas Landgraf7002a4f2007-05-07 16:43:01 -0300486/* check if the device is present and register with v4l and
487usb if it is */
488static int usb_dsbr100_probe(struct usb_interface *intf,
489 const struct usb_device_id *id)
490{
491 struct dsbr100_device *radio;
492
Alexey Klimov223377e2008-10-19 23:50:27 -0300493 radio = kmalloc(sizeof(struct dsbr100_device), GFP_KERNEL);
494
495 if (!radio)
Douglas Landgraf7002a4f2007-05-07 16:43:01 -0300496 return -ENOMEM;
Alexey Klimov223377e2008-10-19 23:50:27 -0300497
498 radio->transfer_buffer = kmalloc(TB_LEN, GFP_KERNEL);
499
500 if (!(radio->transfer_buffer)) {
Oliver Neukum863c86d2007-12-03 06:48:43 -0300501 kfree(radio);
502 return -ENOMEM;
503 }
Alexey Klimov223377e2008-10-19 23:50:27 -0300504 radio->videodev = video_device_alloc();
505
506 if (!(radio->videodev)) {
Oliver Neukum863c86d2007-12-03 06:48:43 -0300507 kfree(radio->transfer_buffer);
Douglas Landgraf7002a4f2007-05-07 16:43:01 -0300508 kfree(radio);
509 return -ENOMEM;
510 }
511 memcpy(radio->videodev, &dsbr100_videodev_template,
512 sizeof(dsbr100_videodev_template));
513 radio->removed = 0;
514 radio->users = 0;
515 radio->usbdev = interface_to_usbdev(intf);
Alexey Klimov223377e2008-10-19 23:50:27 -0300516 radio->curfreq = FREQ_MIN * FREQ_MUL;
Douglas Landgraf7002a4f2007-05-07 16:43:01 -0300517 video_set_drvdata(radio->videodev, radio);
Hans Verkuilcba99ae2008-09-03 17:11:58 -0300518 if (video_register_device(radio->videodev, VFL_TYPE_RADIO, radio_nr) < 0) {
Greg Kroah-Hartmanaa826612008-08-14 09:37:34 -0700519 dev_warn(&intf->dev, "Could not register video device\n");
Douglas Landgraf7002a4f2007-05-07 16:43:01 -0300520 video_device_release(radio->videodev);
Oliver Neukum863c86d2007-12-03 06:48:43 -0300521 kfree(radio->transfer_buffer);
Douglas Landgraf7002a4f2007-05-07 16:43:01 -0300522 kfree(radio);
523 return -EIO;
524 }
525 usb_set_intfdata(intf, radio);
526 return 0;
527}
528
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529static int __init dsbr100_init(void)
530{
531 int retval = usb_register(&usb_dsbr100_driver);
Greg Kroah-Hartmana482f322008-10-10 05:08:23 -0300532 printk(KERN_INFO KBUILD_MODNAME ": " DRIVER_VERSION ":"
533 DRIVER_DESC "\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 return retval;
535}
536
537static void __exit dsbr100_exit(void)
538{
539 usb_deregister(&usb_dsbr100_driver);
540}
541
542module_init (dsbr100_init);
543module_exit (dsbr100_exit);
544
545MODULE_AUTHOR( DRIVER_AUTHOR );
546MODULE_DESCRIPTION( DRIVER_DESC );
547MODULE_LICENSE("GPL");