blob: 09988f020c48e61c98ea6061ec84ace7fa077a96 [file] [log] [blame]
Alexey Klimovfc55bcb2008-12-27 22:33:54 -03001/* A driver for the D-Link DSB-R100 USB radio and Gemtek USB Radio 21.
2 The device plugs into both the USB and an analog audio input, so this thing
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 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
Alexey Klimov6076dbf2008-12-27 22:35:21 -030036 Version 0.44:
37 Add suspend/resume functions, fix unplug of device,
38 a lot of cleanups and fixes by Alexey Klimov <klimov.linux@gmail.com>
39
Oliver Neukum863c86d2007-12-03 06:48:43 -030040 Version 0.43:
41 Oliver Neukum: avoided DMA coherency issue
42
Douglas Landgraf7002a4f2007-05-07 16:43:01 -030043 Version 0.42:
44 Converted dsbr100 to use video_ioctl2
45 by Douglas Landgraf <dougsland@gmail.com>
46
Alan Cox5aff3082006-08-08 15:47:50 -030047 Version 0.41-ac1:
48 Alan Cox: Some cleanups and fixes
49
50 Version 0.41:
51 Converted to V4L2 API by Mauro Carvalho Chehab <mchehab@infradead.org>
52
Linus Torvalds1da177e2005-04-16 15:20:36 -070053 Version 0.40:
Alan Cox5aff3082006-08-08 15:47:50 -030054 Markus: Updates for 2.6.x kernels, code layout changes, name sanitizing
Linus Torvalds1da177e2005-04-16 15:20:36 -070055
56 Version 0.30:
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -030057 Markus: Updates for 2.5.x kernel and more ISO compliant source
Linus Torvalds1da177e2005-04-16 15:20:36 -070058
59 Version 0.25:
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -030060 PSL and Markus: Cleanup, radio now doesn't stop on device close
Linus Torvalds1da177e2005-04-16 15:20:36 -070061
62 Version 0.24:
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -030063 Markus: Hope I got these silly VIDEO_TUNER_LOW issues finally
Linus Torvalds1da177e2005-04-16 15:20:36 -070064 right. Some minor cleanup, improved standalone compilation
65
66 Version 0.23:
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -030067 Markus: Sign extension bug fixed by declaring transfer_buffer unsigned
Linus Torvalds1da177e2005-04-16 15:20:36 -070068
69 Version 0.22:
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -030070 Markus: Some (brown bag) cleanup in what VIDIOCSTUNER returns,
Linus Torvalds1da177e2005-04-16 15:20:36 -070071 thanks to Mike Cox for pointing the problem out.
72
73 Version 0.21:
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -030074 Markus: Minor cleanup, warnings if something goes wrong, lame attempt
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 to adhere to Documentation/CodingStyle
76
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -030077 Version 0.2:
78 Brad Hards <bradh@dynamite.com.au>: Fixes to make it work as non-module
Linus Torvalds1da177e2005-04-16 15:20:36 -070079 Markus: Copyright clarification
80
81 Version 0.01: Markus: initial release
82
83*/
84
Linus Torvalds1da177e2005-04-16 15:20:36 -070085#include <linux/kernel.h>
86#include <linux/module.h>
87#include <linux/init.h>
88#include <linux/slab.h>
89#include <linux/input.h>
Alan Cox5aff3082006-08-08 15:47:50 -030090#include <linux/videodev2.h>
Mauro Carvalho Chehab5e87efa2006-06-05 10:26:32 -030091#include <media/v4l2-common.h>
Hans Verkuil35ea11f2008-07-20 08:12:02 -030092#include <media/v4l2-ioctl.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070093#include <linux/usb.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070094
95/*
96 * Version Information
97 */
Alan Cox5aff3082006-08-08 15:47:50 -030098#include <linux/version.h> /* for KERNEL_VERSION MACRO */
99
Alexey Klimov6076dbf2008-12-27 22:35:21 -0300100#define DRIVER_VERSION "v0.44"
101#define RADIO_VERSION KERNEL_VERSION(0, 4, 4)
Alan Cox5aff3082006-08-08 15:47:50 -0300102
103static struct v4l2_queryctrl radio_qctrl[] = {
104 {
105 .id = V4L2_CID_AUDIO_MUTE,
106 .name = "Mute",
107 .minimum = 0,
108 .maximum = 1,
109 .default_value = 1,
110 .type = V4L2_CTRL_TYPE_BOOLEAN,
Alexey Klimov54b7b0d2008-11-08 00:31:50 -0300111 },
112/* HINT: the disabled controls are only here to satify kradio and such apps */
113 { .id = V4L2_CID_AUDIO_VOLUME,
114 .flags = V4L2_CTRL_FLAG_DISABLED,
115 },
116 {
117 .id = V4L2_CID_AUDIO_BALANCE,
118 .flags = V4L2_CTRL_FLAG_DISABLED,
119 },
120 {
121 .id = V4L2_CID_AUDIO_BASS,
122 .flags = V4L2_CTRL_FLAG_DISABLED,
123 },
124 {
125 .id = V4L2_CID_AUDIO_TREBLE,
126 .flags = V4L2_CTRL_FLAG_DISABLED,
127 },
128 {
129 .id = V4L2_CID_AUDIO_LOUDNESS,
130 .flags = V4L2_CTRL_FLAG_DISABLED,
131 },
Alan Cox5aff3082006-08-08 15:47:50 -0300132};
133
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134#define DRIVER_AUTHOR "Markus Demleitner <msdemlei@tucana.harvard.edu>"
135#define DRIVER_DESC "D-Link DSB-R100 USB FM radio driver"
136
137#define DSB100_VENDOR 0x04b4
138#define DSB100_PRODUCT 0x1002
139
140/* Commands the device appears to understand */
141#define DSB100_TUNE 1
142#define DSB100_ONOFF 2
143
144#define TB_LEN 16
145
146/* Frequency limits in MHz -- these are European values. For Japanese
147devices, that would be 76 and 91. */
148#define FREQ_MIN 87.5
149#define FREQ_MAX 108.0
150#define FREQ_MUL 16000
151
Alexey Klimov3a0efc32008-12-27 21:32:49 -0300152#define videodev_to_radio(d) container_of(d, struct dsbr100_device, videodev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153
154static int usb_dsbr100_probe(struct usb_interface *intf,
155 const struct usb_device_id *id);
156static void usb_dsbr100_disconnect(struct usb_interface *intf);
Hans Verkuilbec43662008-12-30 06:58:20 -0300157static int usb_dsbr100_open(struct file *file);
158static int usb_dsbr100_close(struct file *file);
Alexey Klimov04e0ffb2008-11-08 00:40:46 -0300159static int usb_dsbr100_suspend(struct usb_interface *intf,
160 pm_message_t message);
161static int usb_dsbr100_resume(struct usb_interface *intf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162
163static int radio_nr = -1;
164module_param(radio_nr, int, 0);
165
166/* Data for one (physical) device */
Alan Cox5aff3082006-08-08 15:47:50 -0300167struct dsbr100_device {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 struct usb_device *usbdev;
Alexey Klimov3a0efc32008-12-27 21:32:49 -0300169 struct video_device videodev;
Oliver Neukum863c86d2007-12-03 06:48:43 -0300170 u8 *transfer_buffer;
Alexey Klimov3a0efc32008-12-27 21:32:49 -0300171 struct mutex lock; /* buffer locking */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 int curfreq;
173 int stereo;
174 int users;
175 int removed;
Alan Cox5aff3082006-08-08 15:47:50 -0300176 int muted;
177};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179static struct usb_device_id usb_dsbr100_device_table [] = {
180 { USB_DEVICE(DSB100_VENDOR, DSB100_PRODUCT) },
181 { } /* Terminating entry */
182};
183
184MODULE_DEVICE_TABLE (usb, usb_dsbr100_device_table);
185
186/* USB subsystem interface */
187static struct usb_driver usb_dsbr100_driver = {
Alexey Klimov04e0ffb2008-11-08 00:40:46 -0300188 .name = "dsbr100",
189 .probe = usb_dsbr100_probe,
190 .disconnect = usb_dsbr100_disconnect,
191 .id_table = usb_dsbr100_device_table,
192 .suspend = usb_dsbr100_suspend,
193 .resume = usb_dsbr100_resume,
194 .reset_resume = usb_dsbr100_resume,
195 .supports_autosuspend = 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196};
197
198/* Low-level device interface begins here */
199
200/* switch on radio */
Alan Cox5aff3082006-08-08 15:47:50 -0300201static int dsbr100_start(struct dsbr100_device *radio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202{
Alexey Klimov417b7952008-12-27 22:30:29 -0300203 int retval;
204 int request;
205
Alexey Klimov3a0efc32008-12-27 21:32:49 -0300206 mutex_lock(&radio->lock);
Alexey Klimov417b7952008-12-27 22:30:29 -0300207
208 retval = usb_control_msg(radio->usbdev,
209 usb_rcvctrlpipe(radio->usbdev, 0),
210 USB_REQ_GET_STATUS,
211 USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN,
212 0x00, 0xC7, radio->transfer_buffer, 8, 300);
213
214 if (retval < 0) {
215 request = USB_REQ_GET_STATUS;
216 goto usb_control_msg_failed;
Alexey Klimov3a0efc32008-12-27 21:32:49 -0300217 }
218
Alexey Klimov417b7952008-12-27 22:30:29 -0300219 retval = usb_control_msg(radio->usbdev,
220 usb_rcvctrlpipe(radio->usbdev, 0),
221 DSB100_ONOFF,
222 USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN,
223 0x01, 0x00, radio->transfer_buffer, 8, 300);
224
225 if (retval < 0) {
226 request = DSB100_ONOFF;
227 goto usb_control_msg_failed;
228 }
229
230 radio->muted = 0;
Alexey Klimov3a0efc32008-12-27 21:32:49 -0300231 mutex_unlock(&radio->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 return (radio->transfer_buffer)[0];
Alexey Klimov417b7952008-12-27 22:30:29 -0300233
234usb_control_msg_failed:
235 mutex_unlock(&radio->lock);
236 dev_err(&radio->usbdev->dev,
237 "%s - usb_control_msg returned %i, request %i\n",
238 __func__, retval, request);
Alexey Klimovd25cb642008-12-27 22:40:57 -0300239 return retval;
Alexey Klimov417b7952008-12-27 22:30:29 -0300240
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241}
242
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243/* switch off radio */
Alan Cox5aff3082006-08-08 15:47:50 -0300244static int dsbr100_stop(struct dsbr100_device *radio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245{
Alexey Klimov417b7952008-12-27 22:30:29 -0300246 int retval;
247 int request;
248
Alexey Klimov3a0efc32008-12-27 21:32:49 -0300249 mutex_lock(&radio->lock);
Alexey Klimov417b7952008-12-27 22:30:29 -0300250
251 retval = usb_control_msg(radio->usbdev,
252 usb_rcvctrlpipe(radio->usbdev, 0),
253 USB_REQ_GET_STATUS,
254 USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN,
255 0x16, 0x1C, radio->transfer_buffer, 8, 300);
256
257 if (retval < 0) {
258 request = USB_REQ_GET_STATUS;
259 goto usb_control_msg_failed;
Alexey Klimov3a0efc32008-12-27 21:32:49 -0300260 }
261
Alexey Klimov417b7952008-12-27 22:30:29 -0300262 retval = usb_control_msg(radio->usbdev,
263 usb_rcvctrlpipe(radio->usbdev, 0),
264 DSB100_ONOFF,
265 USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN,
266 0x00, 0x00, radio->transfer_buffer, 8, 300);
267
268 if (retval < 0) {
269 request = DSB100_ONOFF;
270 goto usb_control_msg_failed;
271 }
272
273 radio->muted = 1;
Alexey Klimov3a0efc32008-12-27 21:32:49 -0300274 mutex_unlock(&radio->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 return (radio->transfer_buffer)[0];
Alexey Klimov417b7952008-12-27 22:30:29 -0300276
277usb_control_msg_failed:
278 mutex_unlock(&radio->lock);
279 dev_err(&radio->usbdev->dev,
280 "%s - usb_control_msg returned %i, request %i\n",
281 __func__, retval, request);
Alexey Klimovd25cb642008-12-27 22:40:57 -0300282 return retval;
Alexey Klimov417b7952008-12-27 22:30:29 -0300283
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284}
285
286/* set a frequency, freq is defined by v4l's TUNER_LOW, i.e. 1/16th kHz */
Alan Cox5aff3082006-08-08 15:47:50 -0300287static int dsbr100_setfreq(struct dsbr100_device *radio, int freq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288{
Alexey Klimov417b7952008-12-27 22:30:29 -0300289 int retval;
290 int request;
291
Alexey Klimov223377e2008-10-19 23:50:27 -0300292 freq = (freq / 16 * 80) / 1000 + 856;
Alexey Klimov3a0efc32008-12-27 21:32:49 -0300293 mutex_lock(&radio->lock);
Alexey Klimov417b7952008-12-27 22:30:29 -0300294
295 retval = usb_control_msg(radio->usbdev,
296 usb_rcvctrlpipe(radio->usbdev, 0),
297 DSB100_TUNE,
298 USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN,
299 (freq >> 8) & 0x00ff, freq & 0xff,
300 radio->transfer_buffer, 8, 300);
301
302 if (retval < 0) {
303 request = DSB100_TUNE;
304 goto usb_control_msg_failed;
305 }
306
307 retval = usb_control_msg(radio->usbdev,
308 usb_rcvctrlpipe(radio->usbdev, 0),
309 USB_REQ_GET_STATUS,
310 USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN,
311 0x96, 0xB7, radio->transfer_buffer, 8, 300);
312
313 if (retval < 0) {
314 request = USB_REQ_GET_STATUS;
315 goto usb_control_msg_failed;
316 }
317
318 retval = usb_control_msg(radio->usbdev,
319 usb_rcvctrlpipe(radio->usbdev, 0),
320 USB_REQ_GET_STATUS,
321 USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN,
322 0x00, 0x24, radio->transfer_buffer, 8, 300);
323
324 if (retval < 0) {
325 request = USB_REQ_GET_STATUS;
326 goto usb_control_msg_failed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 }
Alexey Klimov3a0efc32008-12-27 21:32:49 -0300328
Alexey Klimov223377e2008-10-19 23:50:27 -0300329 radio->stereo = !((radio->transfer_buffer)[0] & 0x01);
Alexey Klimov3a0efc32008-12-27 21:32:49 -0300330 mutex_unlock(&radio->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 return (radio->transfer_buffer)[0];
Alexey Klimov417b7952008-12-27 22:30:29 -0300332
333usb_control_msg_failed:
334 radio->stereo = -1;
335 mutex_unlock(&radio->lock);
336 dev_err(&radio->usbdev->dev,
337 "%s - usb_control_msg returned %i, request %i\n",
338 __func__, retval, request);
Alexey Klimovd25cb642008-12-27 22:40:57 -0300339 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340}
341
342/* return the device status. This is, in effect, just whether it
343sees a stereo signal or not. Pity. */
Alan Cox5aff3082006-08-08 15:47:50 -0300344static void dsbr100_getstat(struct dsbr100_device *radio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345{
Alexey Klimov417b7952008-12-27 22:30:29 -0300346 int retval;
347
Alexey Klimov3a0efc32008-12-27 21:32:49 -0300348 mutex_lock(&radio->lock);
Alexey Klimov417b7952008-12-27 22:30:29 -0300349
350 retval = usb_control_msg(radio->usbdev,
351 usb_rcvctrlpipe(radio->usbdev, 0),
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300352 USB_REQ_GET_STATUS,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN,
Alexey Klimov417b7952008-12-27 22:30:29 -0300354 0x00 , 0x24, radio->transfer_buffer, 8, 300);
355
356 if (retval < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 radio->stereo = -1;
Alexey Klimov417b7952008-12-27 22:30:29 -0300358 dev_err(&radio->usbdev->dev,
359 "%s - usb_control_msg returned %i, request %i\n",
360 __func__, retval, USB_REQ_GET_STATUS);
361 } else {
Alexey Klimov223377e2008-10-19 23:50:27 -0300362 radio->stereo = !(radio->transfer_buffer[0] & 0x01);
Alexey Klimov417b7952008-12-27 22:30:29 -0300363 }
364
Alexey Klimov3a0efc32008-12-27 21:32:49 -0300365 mutex_unlock(&radio->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366}
367
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368/* USB subsystem interface begins here */
369
Alexey Klimovfc55bcb2008-12-27 22:33:54 -0300370/*
371 * Handle unplugging of the device.
372 * We call video_unregister_device in any case.
373 * The last function called in this procedure is
374 * usb_dsbr100_video_device_release
375 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376static void usb_dsbr100_disconnect(struct usb_interface *intf)
377{
Alan Cox5aff3082006-08-08 15:47:50 -0300378 struct dsbr100_device *radio = usb_get_intfdata(intf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379
380 usb_set_intfdata (intf, NULL);
Alexey Klimov3a0efc32008-12-27 21:32:49 -0300381
382 mutex_lock(&radio->lock);
383 radio->removed = 1;
384 mutex_unlock(&radio->lock);
385
386 video_unregister_device(&radio->videodev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387}
388
389
Douglas Landgraf7002a4f2007-05-07 16:43:01 -0300390static int vidioc_querycap(struct file *file, void *priv,
391 struct v4l2_capability *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392{
Alexey Klimovc7181cf2009-01-25 20:05:58 -0300393 struct dsbr100_device *radio = video_drvdata(file);
394
Douglas Landgraf7002a4f2007-05-07 16:43:01 -0300395 strlcpy(v->driver, "dsbr100", sizeof(v->driver));
396 strlcpy(v->card, "D-Link R-100 USB FM Radio", sizeof(v->card));
Alexey Klimovc7181cf2009-01-25 20:05:58 -0300397 usb_make_path(radio->usbdev, v->bus_info, sizeof(v->bus_info));
Douglas Landgraf7002a4f2007-05-07 16:43:01 -0300398 v->version = RADIO_VERSION;
399 v->capabilities = V4L2_CAP_TUNER;
400 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401}
402
Douglas Landgraf7002a4f2007-05-07 16:43:01 -0300403static int vidioc_g_tuner(struct file *file, void *priv,
404 struct v4l2_tuner *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405{
Hans Verkuilc170ecf2008-08-23 08:32:09 -0300406 struct dsbr100_device *radio = video_drvdata(file);
Douglas Landgraf7002a4f2007-05-07 16:43:01 -0300407
Alexey Klimov3a0efc32008-12-27 21:32:49 -0300408 /* safety check */
409 if (radio->removed)
410 return -EIO;
411
Douglas Landgraf7002a4f2007-05-07 16:43:01 -0300412 if (v->index > 0)
413 return -EINVAL;
414
415 dsbr100_getstat(radio);
416 strcpy(v->name, "FM");
417 v->type = V4L2_TUNER_RADIO;
Alexey Klimov223377e2008-10-19 23:50:27 -0300418 v->rangelow = FREQ_MIN * FREQ_MUL;
419 v->rangehigh = FREQ_MAX * FREQ_MUL;
420 v->rxsubchans = V4L2_TUNER_SUB_MONO | V4L2_TUNER_SUB_STEREO;
Douglas Landgraf7002a4f2007-05-07 16:43:01 -0300421 v->capability = V4L2_TUNER_CAP_LOW;
422 if(radio->stereo)
423 v->audmode = V4L2_TUNER_MODE_STEREO;
424 else
425 v->audmode = V4L2_TUNER_MODE_MONO;
426 v->signal = 0xffff; /* We can't get the signal strength */
427 return 0;
428}
429
430static int vidioc_s_tuner(struct file *file, void *priv,
431 struct v4l2_tuner *v)
432{
Alexey Klimov3a0efc32008-12-27 21:32:49 -0300433 struct dsbr100_device *radio = video_drvdata(file);
434
435 /* safety check */
436 if (radio->removed)
437 return -EIO;
438
Douglas Landgraf7002a4f2007-05-07 16:43:01 -0300439 if (v->index > 0)
440 return -EINVAL;
441
442 return 0;
443}
444
445static int vidioc_s_frequency(struct file *file, void *priv,
446 struct v4l2_frequency *f)
447{
Hans Verkuilc170ecf2008-08-23 08:32:09 -0300448 struct dsbr100_device *radio = video_drvdata(file);
Alexey Klimov417b7952008-12-27 22:30:29 -0300449 int retval;
Douglas Landgraf7002a4f2007-05-07 16:43:01 -0300450
Alexey Klimov3a0efc32008-12-27 21:32:49 -0300451 /* safety check */
452 if (radio->removed)
453 return -EIO;
454
Douglas Landgraf7002a4f2007-05-07 16:43:01 -0300455 radio->curfreq = f->frequency;
Alexey Klimov417b7952008-12-27 22:30:29 -0300456 retval = dsbr100_setfreq(radio, radio->curfreq);
Alexey Klimovd25cb642008-12-27 22:40:57 -0300457 if (retval < 0)
Greg Kroah-Hartmanaa826612008-08-14 09:37:34 -0700458 dev_warn(&radio->usbdev->dev, "Set frequency failed\n");
Douglas Landgraf7002a4f2007-05-07 16:43:01 -0300459 return 0;
460}
461
462static int vidioc_g_frequency(struct file *file, void *priv,
463 struct v4l2_frequency *f)
464{
Hans Verkuilc170ecf2008-08-23 08:32:09 -0300465 struct dsbr100_device *radio = video_drvdata(file);
Douglas Landgraf7002a4f2007-05-07 16:43:01 -0300466
Alexey Klimov3a0efc32008-12-27 21:32:49 -0300467 /* safety check */
468 if (radio->removed)
469 return -EIO;
470
Douglas Landgraf7002a4f2007-05-07 16:43:01 -0300471 f->type = V4L2_TUNER_RADIO;
472 f->frequency = radio->curfreq;
473 return 0;
474}
475
476static int vidioc_queryctrl(struct file *file, void *priv,
477 struct v4l2_queryctrl *qc)
478{
479 int i;
480
481 for (i = 0; i < ARRAY_SIZE(radio_qctrl); i++) {
482 if (qc->id && qc->id == radio_qctrl[i].id) {
Alexey Klimov223377e2008-10-19 23:50:27 -0300483 memcpy(qc, &(radio_qctrl[i]), sizeof(*qc));
Douglas Landgraf7002a4f2007-05-07 16:43:01 -0300484 return 0;
485 }
486 }
487 return -EINVAL;
488}
489
490static int vidioc_g_ctrl(struct file *file, void *priv,
491 struct v4l2_control *ctrl)
492{
Hans Verkuilc170ecf2008-08-23 08:32:09 -0300493 struct dsbr100_device *radio = video_drvdata(file);
Douglas Landgraf7002a4f2007-05-07 16:43:01 -0300494
Alexey Klimov3a0efc32008-12-27 21:32:49 -0300495 /* safety check */
496 if (radio->removed)
497 return -EIO;
498
Douglas Landgraf7002a4f2007-05-07 16:43:01 -0300499 switch (ctrl->id) {
500 case V4L2_CID_AUDIO_MUTE:
501 ctrl->value = radio->muted;
502 return 0;
503 }
504 return -EINVAL;
505}
506
507static int vidioc_s_ctrl(struct file *file, void *priv,
508 struct v4l2_control *ctrl)
509{
Hans Verkuilc170ecf2008-08-23 08:32:09 -0300510 struct dsbr100_device *radio = video_drvdata(file);
Alexey Klimov417b7952008-12-27 22:30:29 -0300511 int retval;
Douglas Landgraf7002a4f2007-05-07 16:43:01 -0300512
Alexey Klimov3a0efc32008-12-27 21:32:49 -0300513 /* safety check */
514 if (radio->removed)
515 return -EIO;
516
Douglas Landgraf7002a4f2007-05-07 16:43:01 -0300517 switch (ctrl->id) {
518 case V4L2_CID_AUDIO_MUTE:
519 if (ctrl->value) {
Alexey Klimov417b7952008-12-27 22:30:29 -0300520 retval = dsbr100_stop(radio);
Alexey Klimovd25cb642008-12-27 22:40:57 -0300521 if (retval < 0) {
Greg Kroah-Hartmanaa826612008-08-14 09:37:34 -0700522 dev_warn(&radio->usbdev->dev,
523 "Radio did not respond properly\n");
Alexey Klimov90b698d2008-10-09 13:42:32 -0300524 return -EBUSY;
525 }
Douglas Landgraf7002a4f2007-05-07 16:43:01 -0300526 } else {
Alexey Klimov417b7952008-12-27 22:30:29 -0300527 retval = dsbr100_start(radio);
Alexey Klimovd25cb642008-12-27 22:40:57 -0300528 if (retval < 0) {
Greg Kroah-Hartmanaa826612008-08-14 09:37:34 -0700529 dev_warn(&radio->usbdev->dev,
530 "Radio did not respond properly\n");
Alexey Klimov90b698d2008-10-09 13:42:32 -0300531 return -EBUSY;
532 }
Douglas Landgraf7002a4f2007-05-07 16:43:01 -0300533 }
534 return 0;
535 }
536 return -EINVAL;
537}
538
539static int vidioc_g_audio(struct file *file, void *priv,
540 struct v4l2_audio *a)
541{
542 if (a->index > 1)
543 return -EINVAL;
544
545 strcpy(a->name, "Radio");
546 a->capability = V4L2_AUDCAP_STEREO;
547 return 0;
548}
549
550static int vidioc_g_input(struct file *filp, void *priv, unsigned int *i)
551{
552 *i = 0;
553 return 0;
554}
555
556static int vidioc_s_input(struct file *filp, void *priv, unsigned int i)
557{
558 if (i != 0)
559 return -EINVAL;
560 return 0;
561}
562
563static int vidioc_s_audio(struct file *file, void *priv,
564 struct v4l2_audio *a)
565{
566 if (a->index != 0)
567 return -EINVAL;
568 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569}
570
Hans Verkuilbec43662008-12-30 06:58:20 -0300571static int usb_dsbr100_open(struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572{
Hans Verkuilc170ecf2008-08-23 08:32:09 -0300573 struct dsbr100_device *radio = video_drvdata(file);
Alexey Klimovb9f35732008-10-20 00:00:03 -0300574 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575
Hans Verkuild56dc612008-07-30 08:43:36 -0300576 lock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 radio->users = 1;
Alan Cox5aff3082006-08-08 15:47:50 -0300578 radio->muted = 1;
579
Alexey Klimov417b7952008-12-27 22:30:29 -0300580 retval = dsbr100_start(radio);
581 if (retval < 0) {
Greg Kroah-Hartmanaa826612008-08-14 09:37:34 -0700582 dev_warn(&radio->usbdev->dev,
583 "Radio did not start up properly\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 radio->users = 0;
Hans Verkuild56dc612008-07-30 08:43:36 -0300585 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 return -EIO;
587 }
Alexey Klimovb9f35732008-10-20 00:00:03 -0300588
589 retval = dsbr100_setfreq(radio, radio->curfreq);
Alexey Klimovd25cb642008-12-27 22:40:57 -0300590 if (retval < 0)
Alexey Klimov290588e2008-12-27 21:42:39 -0300591 dev_warn(&radio->usbdev->dev,
592 "set frequency failed\n");
Alexey Klimovb9f35732008-10-20 00:00:03 -0300593
Hans Verkuild56dc612008-07-30 08:43:36 -0300594 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 return 0;
596}
597
Hans Verkuilbec43662008-12-30 06:58:20 -0300598static int usb_dsbr100_close(struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599{
Hans Verkuilc170ecf2008-08-23 08:32:09 -0300600 struct dsbr100_device *radio = video_drvdata(file);
Alexey Klimov3a0efc32008-12-27 21:32:49 -0300601 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602
603 if (!radio)
604 return -ENODEV;
Alexey Klimov3a0efc32008-12-27 21:32:49 -0300605
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 radio->users = 0;
Alexey Klimov3a0efc32008-12-27 21:32:49 -0300607 if (!radio->removed) {
608 retval = dsbr100_stop(radio);
Alexey Klimovd25cb642008-12-27 22:40:57 -0300609 if (retval < 0) {
Alexey Klimov3a0efc32008-12-27 21:32:49 -0300610 dev_warn(&radio->usbdev->dev,
611 "dsbr100_stop failed\n");
612 }
613
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 }
615 return 0;
616}
617
Alexey Klimov04e0ffb2008-11-08 00:40:46 -0300618/* Suspend device - stop device. */
619static int usb_dsbr100_suspend(struct usb_interface *intf, pm_message_t message)
620{
621 struct dsbr100_device *radio = usb_get_intfdata(intf);
622 int retval;
623
624 retval = dsbr100_stop(radio);
Alexey Klimovd25cb642008-12-27 22:40:57 -0300625 if (retval < 0)
Alexey Klimov04e0ffb2008-11-08 00:40:46 -0300626 dev_warn(&intf->dev, "dsbr100_stop failed\n");
627
628 dev_info(&intf->dev, "going into suspend..\n");
629
630 return 0;
631}
632
633/* Resume device - start device. */
634static int usb_dsbr100_resume(struct usb_interface *intf)
635{
636 struct dsbr100_device *radio = usb_get_intfdata(intf);
637 int retval;
638
639 retval = dsbr100_start(radio);
Alexey Klimovd25cb642008-12-27 22:40:57 -0300640 if (retval < 0)
Alexey Klimov04e0ffb2008-11-08 00:40:46 -0300641 dev_warn(&intf->dev, "dsbr100_start failed\n");
642
643 dev_info(&intf->dev, "coming out of suspend..\n");
644
645 return 0;
646}
647
Alexey Klimovfc55bcb2008-12-27 22:33:54 -0300648/* free data structures */
Alexey Klimov3a0efc32008-12-27 21:32:49 -0300649static void usb_dsbr100_video_device_release(struct video_device *videodev)
650{
651 struct dsbr100_device *radio = videodev_to_radio(videodev);
652
653 kfree(radio->transfer_buffer);
654 kfree(radio);
655}
656
Douglas Landgraf7002a4f2007-05-07 16:43:01 -0300657/* File system interface */
Hans Verkuilbec43662008-12-30 06:58:20 -0300658static const struct v4l2_file_operations usb_dsbr100_fops = {
Douglas Landgraf7002a4f2007-05-07 16:43:01 -0300659 .owner = THIS_MODULE,
660 .open = usb_dsbr100_open,
661 .release = usb_dsbr100_close,
662 .ioctl = video_ioctl2,
Douglas Landgraf7002a4f2007-05-07 16:43:01 -0300663};
664
Hans Verkuila3998102008-07-21 02:57:38 -0300665static const struct v4l2_ioctl_ops usb_dsbr100_ioctl_ops = {
Douglas Landgraf7002a4f2007-05-07 16:43:01 -0300666 .vidioc_querycap = vidioc_querycap,
667 .vidioc_g_tuner = vidioc_g_tuner,
668 .vidioc_s_tuner = vidioc_s_tuner,
669 .vidioc_g_frequency = vidioc_g_frequency,
670 .vidioc_s_frequency = vidioc_s_frequency,
671 .vidioc_queryctrl = vidioc_queryctrl,
672 .vidioc_g_ctrl = vidioc_g_ctrl,
673 .vidioc_s_ctrl = vidioc_s_ctrl,
674 .vidioc_g_audio = vidioc_g_audio,
675 .vidioc_s_audio = vidioc_s_audio,
676 .vidioc_g_input = vidioc_g_input,
677 .vidioc_s_input = vidioc_s_input,
678};
679
Hans Verkuila3998102008-07-21 02:57:38 -0300680/* V4L2 interface */
Alexey Klimov3a0efc32008-12-27 21:32:49 -0300681static struct video_device dsbr100_videodev_data = {
Hans Verkuila3998102008-07-21 02:57:38 -0300682 .name = "D-Link DSB-R 100",
Hans Verkuila3998102008-07-21 02:57:38 -0300683 .fops = &usb_dsbr100_fops,
684 .ioctl_ops = &usb_dsbr100_ioctl_ops,
Alexey Klimov3a0efc32008-12-27 21:32:49 -0300685 .release = usb_dsbr100_video_device_release,
Hans Verkuila3998102008-07-21 02:57:38 -0300686};
687
Alexey Klimovfc55bcb2008-12-27 22:33:54 -0300688/* check if the device is present and register with v4l and usb if it is */
Douglas Landgraf7002a4f2007-05-07 16:43:01 -0300689static int usb_dsbr100_probe(struct usb_interface *intf,
690 const struct usb_device_id *id)
691{
692 struct dsbr100_device *radio;
Alexey Klimov417b7952008-12-27 22:30:29 -0300693 int retval;
Douglas Landgraf7002a4f2007-05-07 16:43:01 -0300694
Alexey Klimov223377e2008-10-19 23:50:27 -0300695 radio = kmalloc(sizeof(struct dsbr100_device), GFP_KERNEL);
696
697 if (!radio)
Douglas Landgraf7002a4f2007-05-07 16:43:01 -0300698 return -ENOMEM;
Alexey Klimov223377e2008-10-19 23:50:27 -0300699
700 radio->transfer_buffer = kmalloc(TB_LEN, GFP_KERNEL);
701
702 if (!(radio->transfer_buffer)) {
Oliver Neukum863c86d2007-12-03 06:48:43 -0300703 kfree(radio);
704 return -ENOMEM;
705 }
Alexey Klimov223377e2008-10-19 23:50:27 -0300706
Alexey Klimov3a0efc32008-12-27 21:32:49 -0300707 mutex_init(&radio->lock);
708 radio->videodev = dsbr100_videodev_data;
709
Douglas Landgraf7002a4f2007-05-07 16:43:01 -0300710 radio->removed = 0;
711 radio->users = 0;
712 radio->usbdev = interface_to_usbdev(intf);
Alexey Klimov223377e2008-10-19 23:50:27 -0300713 radio->curfreq = FREQ_MIN * FREQ_MUL;
Alexey Klimov3a0efc32008-12-27 21:32:49 -0300714 video_set_drvdata(&radio->videodev, radio);
Alexey Klimov417b7952008-12-27 22:30:29 -0300715 retval = video_register_device(&radio->videodev, VFL_TYPE_RADIO, radio_nr);
716 if (retval < 0) {
Alexey Klimov7e1ca842008-12-27 22:31:52 -0300717 dev_err(&intf->dev, "couldn't register video device\n");
Oliver Neukum863c86d2007-12-03 06:48:43 -0300718 kfree(radio->transfer_buffer);
Douglas Landgraf7002a4f2007-05-07 16:43:01 -0300719 kfree(radio);
720 return -EIO;
721 }
722 usb_set_intfdata(intf, radio);
723 return 0;
724}
725
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726static int __init dsbr100_init(void)
727{
728 int retval = usb_register(&usb_dsbr100_driver);
Greg Kroah-Hartmana482f322008-10-10 05:08:23 -0300729 printk(KERN_INFO KBUILD_MODNAME ": " DRIVER_VERSION ":"
730 DRIVER_DESC "\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 return retval;
732}
733
734static void __exit dsbr100_exit(void)
735{
736 usb_deregister(&usb_dsbr100_driver);
737}
738
739module_init (dsbr100_init);
740module_exit (dsbr100_exit);
741
742MODULE_AUTHOR( DRIVER_AUTHOR );
743MODULE_DESCRIPTION( DRIVER_DESC );
744MODULE_LICENSE("GPL");