blob: 448e5a42b4689eaa800ffb70ac70509369aa117b [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
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
Alexey Klimov54b7b0d2008-11-08 00:31:50 -030096#define DRIVER_VERSION "v0.43"
97#define RADIO_VERSION KERNEL_VERSION(0, 4, 3)
Alan Cox5aff3082006-08-08 15:47:50 -030098
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,
Alexey Klimov54b7b0d2008-11-08 00:31:50 -0300107 },
108/* HINT: the disabled controls are only here to satify kradio and such apps */
109 { .id = V4L2_CID_AUDIO_VOLUME,
110 .flags = V4L2_CTRL_FLAG_DISABLED,
111 },
112 {
113 .id = V4L2_CID_AUDIO_BALANCE,
114 .flags = V4L2_CTRL_FLAG_DISABLED,
115 },
116 {
117 .id = V4L2_CID_AUDIO_BASS,
118 .flags = V4L2_CTRL_FLAG_DISABLED,
119 },
120 {
121 .id = V4L2_CID_AUDIO_TREBLE,
122 .flags = V4L2_CTRL_FLAG_DISABLED,
123 },
124 {
125 .id = V4L2_CID_AUDIO_LOUDNESS,
126 .flags = V4L2_CTRL_FLAG_DISABLED,
127 },
Alan Cox5aff3082006-08-08 15:47:50 -0300128};
129
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130#define DRIVER_AUTHOR "Markus Demleitner <msdemlei@tucana.harvard.edu>"
131#define DRIVER_DESC "D-Link DSB-R100 USB FM radio driver"
132
133#define DSB100_VENDOR 0x04b4
134#define DSB100_PRODUCT 0x1002
135
136/* Commands the device appears to understand */
137#define DSB100_TUNE 1
138#define DSB100_ONOFF 2
139
140#define TB_LEN 16
141
142/* Frequency limits in MHz -- these are European values. For Japanese
143devices, that would be 76 and 91. */
144#define FREQ_MIN 87.5
145#define FREQ_MAX 108.0
146#define FREQ_MUL 16000
147
Alexey Klimov3a0efc32008-12-27 21:32:49 -0300148#define videodev_to_radio(d) container_of(d, struct dsbr100_device, videodev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149
150static int usb_dsbr100_probe(struct usb_interface *intf,
151 const struct usb_device_id *id);
152static void usb_dsbr100_disconnect(struct usb_interface *intf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153static int usb_dsbr100_open(struct inode *inode, struct file *file);
154static int usb_dsbr100_close(struct inode *inode, struct file *file);
Alexey Klimov04e0ffb2008-11-08 00:40:46 -0300155static int usb_dsbr100_suspend(struct usb_interface *intf,
156 pm_message_t message);
157static int usb_dsbr100_resume(struct usb_interface *intf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158
159static int radio_nr = -1;
160module_param(radio_nr, int, 0);
161
162/* Data for one (physical) device */
Alan Cox5aff3082006-08-08 15:47:50 -0300163struct dsbr100_device {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 struct usb_device *usbdev;
Alexey Klimov3a0efc32008-12-27 21:32:49 -0300165 struct video_device videodev;
Oliver Neukum863c86d2007-12-03 06:48:43 -0300166 u8 *transfer_buffer;
Alexey Klimov3a0efc32008-12-27 21:32:49 -0300167 struct mutex lock; /* buffer locking */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 int curfreq;
169 int stereo;
170 int users;
171 int removed;
Alan Cox5aff3082006-08-08 15:47:50 -0300172 int muted;
173};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175static struct usb_device_id usb_dsbr100_device_table [] = {
176 { USB_DEVICE(DSB100_VENDOR, DSB100_PRODUCT) },
177 { } /* Terminating entry */
178};
179
180MODULE_DEVICE_TABLE (usb, usb_dsbr100_device_table);
181
182/* USB subsystem interface */
183static struct usb_driver usb_dsbr100_driver = {
Alexey Klimov04e0ffb2008-11-08 00:40:46 -0300184 .name = "dsbr100",
185 .probe = usb_dsbr100_probe,
186 .disconnect = usb_dsbr100_disconnect,
187 .id_table = usb_dsbr100_device_table,
188 .suspend = usb_dsbr100_suspend,
189 .resume = usb_dsbr100_resume,
190 .reset_resume = usb_dsbr100_resume,
191 .supports_autosuspend = 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192};
193
194/* Low-level device interface begins here */
195
196/* switch on radio */
Alan Cox5aff3082006-08-08 15:47:50 -0300197static int dsbr100_start(struct dsbr100_device *radio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198{
Alexey Klimov417b7952008-12-27 22:30:29 -0300199 int retval;
200 int request;
201
Alexey Klimov3a0efc32008-12-27 21:32:49 -0300202 mutex_lock(&radio->lock);
Alexey Klimov417b7952008-12-27 22:30:29 -0300203
204 retval = usb_control_msg(radio->usbdev,
205 usb_rcvctrlpipe(radio->usbdev, 0),
206 USB_REQ_GET_STATUS,
207 USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN,
208 0x00, 0xC7, radio->transfer_buffer, 8, 300);
209
210 if (retval < 0) {
211 request = USB_REQ_GET_STATUS;
212 goto usb_control_msg_failed;
Alexey Klimov3a0efc32008-12-27 21:32:49 -0300213 }
214
Alexey Klimov417b7952008-12-27 22:30:29 -0300215 retval = usb_control_msg(radio->usbdev,
216 usb_rcvctrlpipe(radio->usbdev, 0),
217 DSB100_ONOFF,
218 USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN,
219 0x01, 0x00, radio->transfer_buffer, 8, 300);
220
221 if (retval < 0) {
222 request = DSB100_ONOFF;
223 goto usb_control_msg_failed;
224 }
225
226 radio->muted = 0;
Alexey Klimov3a0efc32008-12-27 21:32:49 -0300227 mutex_unlock(&radio->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 return (radio->transfer_buffer)[0];
Alexey Klimov417b7952008-12-27 22:30:29 -0300229
230usb_control_msg_failed:
231 mutex_unlock(&radio->lock);
232 dev_err(&radio->usbdev->dev,
233 "%s - usb_control_msg returned %i, request %i\n",
234 __func__, retval, request);
235 return -1;
236
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237}
238
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239/* switch off radio */
Alan Cox5aff3082006-08-08 15:47:50 -0300240static int dsbr100_stop(struct dsbr100_device *radio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241{
Alexey Klimov417b7952008-12-27 22:30:29 -0300242 int retval;
243 int request;
244
Alexey Klimov3a0efc32008-12-27 21:32:49 -0300245 mutex_lock(&radio->lock);
Alexey Klimov417b7952008-12-27 22:30:29 -0300246
247 retval = usb_control_msg(radio->usbdev,
248 usb_rcvctrlpipe(radio->usbdev, 0),
249 USB_REQ_GET_STATUS,
250 USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN,
251 0x16, 0x1C, radio->transfer_buffer, 8, 300);
252
253 if (retval < 0) {
254 request = USB_REQ_GET_STATUS;
255 goto usb_control_msg_failed;
Alexey Klimov3a0efc32008-12-27 21:32:49 -0300256 }
257
Alexey Klimov417b7952008-12-27 22:30:29 -0300258 retval = usb_control_msg(radio->usbdev,
259 usb_rcvctrlpipe(radio->usbdev, 0),
260 DSB100_ONOFF,
261 USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN,
262 0x00, 0x00, radio->transfer_buffer, 8, 300);
263
264 if (retval < 0) {
265 request = DSB100_ONOFF;
266 goto usb_control_msg_failed;
267 }
268
269 radio->muted = 1;
Alexey Klimov3a0efc32008-12-27 21:32:49 -0300270 mutex_unlock(&radio->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 return (radio->transfer_buffer)[0];
Alexey Klimov417b7952008-12-27 22:30:29 -0300272
273usb_control_msg_failed:
274 mutex_unlock(&radio->lock);
275 dev_err(&radio->usbdev->dev,
276 "%s - usb_control_msg returned %i, request %i\n",
277 __func__, retval, request);
278 return -1;
279
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280}
281
282/* set a frequency, freq is defined by v4l's TUNER_LOW, i.e. 1/16th kHz */
Alan Cox5aff3082006-08-08 15:47:50 -0300283static int dsbr100_setfreq(struct dsbr100_device *radio, int freq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284{
Alexey Klimov417b7952008-12-27 22:30:29 -0300285 int retval;
286 int request;
287
Alexey Klimov223377e2008-10-19 23:50:27 -0300288 freq = (freq / 16 * 80) / 1000 + 856;
Alexey Klimov3a0efc32008-12-27 21:32:49 -0300289 mutex_lock(&radio->lock);
Alexey Klimov417b7952008-12-27 22:30:29 -0300290
291 retval = usb_control_msg(radio->usbdev,
292 usb_rcvctrlpipe(radio->usbdev, 0),
293 DSB100_TUNE,
294 USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN,
295 (freq >> 8) & 0x00ff, freq & 0xff,
296 radio->transfer_buffer, 8, 300);
297
298 if (retval < 0) {
299 request = DSB100_TUNE;
300 goto usb_control_msg_failed;
301 }
302
303 retval = usb_control_msg(radio->usbdev,
304 usb_rcvctrlpipe(radio->usbdev, 0),
305 USB_REQ_GET_STATUS,
306 USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN,
307 0x96, 0xB7, radio->transfer_buffer, 8, 300);
308
309 if (retval < 0) {
310 request = USB_REQ_GET_STATUS;
311 goto usb_control_msg_failed;
312 }
313
314 retval = usb_control_msg(radio->usbdev,
315 usb_rcvctrlpipe(radio->usbdev, 0),
316 USB_REQ_GET_STATUS,
317 USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN,
318 0x00, 0x24, radio->transfer_buffer, 8, 300);
319
320 if (retval < 0) {
321 request = USB_REQ_GET_STATUS;
322 goto usb_control_msg_failed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 }
Alexey Klimov3a0efc32008-12-27 21:32:49 -0300324
Alexey Klimov223377e2008-10-19 23:50:27 -0300325 radio->stereo = !((radio->transfer_buffer)[0] & 0x01);
Alexey Klimov3a0efc32008-12-27 21:32:49 -0300326 mutex_unlock(&radio->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 return (radio->transfer_buffer)[0];
Alexey Klimov417b7952008-12-27 22:30:29 -0300328
329usb_control_msg_failed:
330 radio->stereo = -1;
331 mutex_unlock(&radio->lock);
332 dev_err(&radio->usbdev->dev,
333 "%s - usb_control_msg returned %i, request %i\n",
334 __func__, retval, request);
335 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336}
337
338/* return the device status. This is, in effect, just whether it
339sees a stereo signal or not. Pity. */
Alan Cox5aff3082006-08-08 15:47:50 -0300340static void dsbr100_getstat(struct dsbr100_device *radio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341{
Alexey Klimov417b7952008-12-27 22:30:29 -0300342 int retval;
343
Alexey Klimov3a0efc32008-12-27 21:32:49 -0300344 mutex_lock(&radio->lock);
Alexey Klimov417b7952008-12-27 22:30:29 -0300345
346 retval = usb_control_msg(radio->usbdev,
347 usb_rcvctrlpipe(radio->usbdev, 0),
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300348 USB_REQ_GET_STATUS,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN,
Alexey Klimov417b7952008-12-27 22:30:29 -0300350 0x00 , 0x24, radio->transfer_buffer, 8, 300);
351
352 if (retval < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 radio->stereo = -1;
Alexey Klimov417b7952008-12-27 22:30:29 -0300354 dev_err(&radio->usbdev->dev,
355 "%s - usb_control_msg returned %i, request %i\n",
356 __func__, retval, USB_REQ_GET_STATUS);
357 } else {
Alexey Klimov223377e2008-10-19 23:50:27 -0300358 radio->stereo = !(radio->transfer_buffer[0] & 0x01);
Alexey Klimov417b7952008-12-27 22:30:29 -0300359 }
360
Alexey Klimov3a0efc32008-12-27 21:32:49 -0300361 mutex_unlock(&radio->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362}
363
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364/* USB subsystem interface begins here */
365
Alexey Klimovfc55bcb2008-12-27 22:33:54 -0300366/*
367 * Handle unplugging of the device.
368 * We call video_unregister_device in any case.
369 * The last function called in this procedure is
370 * usb_dsbr100_video_device_release
371 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372static void usb_dsbr100_disconnect(struct usb_interface *intf)
373{
Alan Cox5aff3082006-08-08 15:47:50 -0300374 struct dsbr100_device *radio = usb_get_intfdata(intf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375
376 usb_set_intfdata (intf, NULL);
Alexey Klimov3a0efc32008-12-27 21:32:49 -0300377
378 mutex_lock(&radio->lock);
379 radio->removed = 1;
380 mutex_unlock(&radio->lock);
381
382 video_unregister_device(&radio->videodev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383}
384
385
Douglas Landgraf7002a4f2007-05-07 16:43:01 -0300386static int vidioc_querycap(struct file *file, void *priv,
387 struct v4l2_capability *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388{
Douglas Landgraf7002a4f2007-05-07 16:43:01 -0300389 strlcpy(v->driver, "dsbr100", sizeof(v->driver));
390 strlcpy(v->card, "D-Link R-100 USB FM Radio", sizeof(v->card));
Alexey Klimov448441c2008-10-19 23:20:47 -0300391 sprintf(v->bus_info, "USB");
Douglas Landgraf7002a4f2007-05-07 16:43:01 -0300392 v->version = RADIO_VERSION;
393 v->capabilities = V4L2_CAP_TUNER;
394 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395}
396
Douglas Landgraf7002a4f2007-05-07 16:43:01 -0300397static int vidioc_g_tuner(struct file *file, void *priv,
398 struct v4l2_tuner *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399{
Hans Verkuilc170ecf2008-08-23 08:32:09 -0300400 struct dsbr100_device *radio = video_drvdata(file);
Douglas Landgraf7002a4f2007-05-07 16:43:01 -0300401
Alexey Klimov3a0efc32008-12-27 21:32:49 -0300402 /* safety check */
403 if (radio->removed)
404 return -EIO;
405
Douglas Landgraf7002a4f2007-05-07 16:43:01 -0300406 if (v->index > 0)
407 return -EINVAL;
408
409 dsbr100_getstat(radio);
410 strcpy(v->name, "FM");
411 v->type = V4L2_TUNER_RADIO;
Alexey Klimov223377e2008-10-19 23:50:27 -0300412 v->rangelow = FREQ_MIN * FREQ_MUL;
413 v->rangehigh = FREQ_MAX * FREQ_MUL;
414 v->rxsubchans = V4L2_TUNER_SUB_MONO | V4L2_TUNER_SUB_STEREO;
Douglas Landgraf7002a4f2007-05-07 16:43:01 -0300415 v->capability = V4L2_TUNER_CAP_LOW;
416 if(radio->stereo)
417 v->audmode = V4L2_TUNER_MODE_STEREO;
418 else
419 v->audmode = V4L2_TUNER_MODE_MONO;
420 v->signal = 0xffff; /* We can't get the signal strength */
421 return 0;
422}
423
424static int vidioc_s_tuner(struct file *file, void *priv,
425 struct v4l2_tuner *v)
426{
Alexey Klimov3a0efc32008-12-27 21:32:49 -0300427 struct dsbr100_device *radio = video_drvdata(file);
428
429 /* safety check */
430 if (radio->removed)
431 return -EIO;
432
Douglas Landgraf7002a4f2007-05-07 16:43:01 -0300433 if (v->index > 0)
434 return -EINVAL;
435
436 return 0;
437}
438
439static int vidioc_s_frequency(struct file *file, void *priv,
440 struct v4l2_frequency *f)
441{
Hans Verkuilc170ecf2008-08-23 08:32:09 -0300442 struct dsbr100_device *radio = video_drvdata(file);
Alexey Klimov417b7952008-12-27 22:30:29 -0300443 int retval;
Douglas Landgraf7002a4f2007-05-07 16:43:01 -0300444
Alexey Klimov3a0efc32008-12-27 21:32:49 -0300445 /* safety check */
446 if (radio->removed)
447 return -EIO;
448
Douglas Landgraf7002a4f2007-05-07 16:43:01 -0300449 radio->curfreq = f->frequency;
Alexey Klimov417b7952008-12-27 22:30:29 -0300450 retval = dsbr100_setfreq(radio, radio->curfreq);
451 if (retval == -1)
Greg Kroah-Hartmanaa826612008-08-14 09:37:34 -0700452 dev_warn(&radio->usbdev->dev, "Set frequency failed\n");
Douglas Landgraf7002a4f2007-05-07 16:43:01 -0300453 return 0;
454}
455
456static int vidioc_g_frequency(struct file *file, void *priv,
457 struct v4l2_frequency *f)
458{
Hans Verkuilc170ecf2008-08-23 08:32:09 -0300459 struct dsbr100_device *radio = video_drvdata(file);
Douglas Landgraf7002a4f2007-05-07 16:43:01 -0300460
Alexey Klimov3a0efc32008-12-27 21:32:49 -0300461 /* safety check */
462 if (radio->removed)
463 return -EIO;
464
Douglas Landgraf7002a4f2007-05-07 16:43:01 -0300465 f->type = V4L2_TUNER_RADIO;
466 f->frequency = radio->curfreq;
467 return 0;
468}
469
470static int vidioc_queryctrl(struct file *file, void *priv,
471 struct v4l2_queryctrl *qc)
472{
473 int i;
474
475 for (i = 0; i < ARRAY_SIZE(radio_qctrl); i++) {
476 if (qc->id && qc->id == radio_qctrl[i].id) {
Alexey Klimov223377e2008-10-19 23:50:27 -0300477 memcpy(qc, &(radio_qctrl[i]), sizeof(*qc));
Douglas Landgraf7002a4f2007-05-07 16:43:01 -0300478 return 0;
479 }
480 }
481 return -EINVAL;
482}
483
484static int vidioc_g_ctrl(struct file *file, void *priv,
485 struct v4l2_control *ctrl)
486{
Hans Verkuilc170ecf2008-08-23 08:32:09 -0300487 struct dsbr100_device *radio = video_drvdata(file);
Douglas Landgraf7002a4f2007-05-07 16:43:01 -0300488
Alexey Klimov3a0efc32008-12-27 21:32:49 -0300489 /* safety check */
490 if (radio->removed)
491 return -EIO;
492
Douglas Landgraf7002a4f2007-05-07 16:43:01 -0300493 switch (ctrl->id) {
494 case V4L2_CID_AUDIO_MUTE:
495 ctrl->value = radio->muted;
496 return 0;
497 }
498 return -EINVAL;
499}
500
501static int vidioc_s_ctrl(struct file *file, void *priv,
502 struct v4l2_control *ctrl)
503{
Hans Verkuilc170ecf2008-08-23 08:32:09 -0300504 struct dsbr100_device *radio = video_drvdata(file);
Alexey Klimov417b7952008-12-27 22:30:29 -0300505 int retval;
Douglas Landgraf7002a4f2007-05-07 16:43:01 -0300506
Alexey Klimov3a0efc32008-12-27 21:32:49 -0300507 /* safety check */
508 if (radio->removed)
509 return -EIO;
510
Douglas Landgraf7002a4f2007-05-07 16:43:01 -0300511 switch (ctrl->id) {
512 case V4L2_CID_AUDIO_MUTE:
513 if (ctrl->value) {
Alexey Klimov417b7952008-12-27 22:30:29 -0300514 retval = dsbr100_stop(radio);
515 if (retval == -1) {
Greg Kroah-Hartmanaa826612008-08-14 09:37:34 -0700516 dev_warn(&radio->usbdev->dev,
517 "Radio did not respond properly\n");
Alexey Klimov90b698d2008-10-09 13:42:32 -0300518 return -EBUSY;
519 }
Douglas Landgraf7002a4f2007-05-07 16:43:01 -0300520 } else {
Alexey Klimov417b7952008-12-27 22:30:29 -0300521 retval = dsbr100_start(radio);
522 if (retval == -1) {
Greg Kroah-Hartmanaa826612008-08-14 09:37:34 -0700523 dev_warn(&radio->usbdev->dev,
524 "Radio did not respond properly\n");
Alexey Klimov90b698d2008-10-09 13:42:32 -0300525 return -EBUSY;
526 }
Douglas Landgraf7002a4f2007-05-07 16:43:01 -0300527 }
528 return 0;
529 }
530 return -EINVAL;
531}
532
533static int vidioc_g_audio(struct file *file, void *priv,
534 struct v4l2_audio *a)
535{
536 if (a->index > 1)
537 return -EINVAL;
538
539 strcpy(a->name, "Radio");
540 a->capability = V4L2_AUDCAP_STEREO;
541 return 0;
542}
543
544static int vidioc_g_input(struct file *filp, void *priv, unsigned int *i)
545{
546 *i = 0;
547 return 0;
548}
549
550static int vidioc_s_input(struct file *filp, void *priv, unsigned int i)
551{
552 if (i != 0)
553 return -EINVAL;
554 return 0;
555}
556
557static int vidioc_s_audio(struct file *file, void *priv,
558 struct v4l2_audio *a)
559{
560 if (a->index != 0)
561 return -EINVAL;
562 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563}
564
565static int usb_dsbr100_open(struct inode *inode, struct file *file)
566{
Hans Verkuilc170ecf2008-08-23 08:32:09 -0300567 struct dsbr100_device *radio = video_drvdata(file);
Alexey Klimovb9f35732008-10-20 00:00:03 -0300568 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569
Hans Verkuild56dc612008-07-30 08:43:36 -0300570 lock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 radio->users = 1;
Alan Cox5aff3082006-08-08 15:47:50 -0300572 radio->muted = 1;
573
Alexey Klimov417b7952008-12-27 22:30:29 -0300574 retval = dsbr100_start(radio);
575 if (retval < 0) {
Greg Kroah-Hartmanaa826612008-08-14 09:37:34 -0700576 dev_warn(&radio->usbdev->dev,
577 "Radio did not start up properly\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 radio->users = 0;
Hans Verkuild56dc612008-07-30 08:43:36 -0300579 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 return -EIO;
581 }
Alexey Klimovb9f35732008-10-20 00:00:03 -0300582
583 retval = dsbr100_setfreq(radio, radio->curfreq);
Alexey Klimovb9f35732008-10-20 00:00:03 -0300584 if (retval == -1)
Alexey Klimov290588e2008-12-27 21:42:39 -0300585 dev_warn(&radio->usbdev->dev,
586 "set frequency failed\n");
Alexey Klimovb9f35732008-10-20 00:00:03 -0300587
Hans Verkuild56dc612008-07-30 08:43:36 -0300588 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 return 0;
590}
591
592static int usb_dsbr100_close(struct inode *inode, struct file *file)
593{
Hans Verkuilc170ecf2008-08-23 08:32:09 -0300594 struct dsbr100_device *radio = video_drvdata(file);
Alexey Klimov3a0efc32008-12-27 21:32:49 -0300595 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596
597 if (!radio)
598 return -ENODEV;
Alexey Klimov3a0efc32008-12-27 21:32:49 -0300599
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 radio->users = 0;
Alexey Klimov3a0efc32008-12-27 21:32:49 -0300601 if (!radio->removed) {
602 retval = dsbr100_stop(radio);
603 if (retval == -1) {
604 dev_warn(&radio->usbdev->dev,
605 "dsbr100_stop failed\n");
606 }
607
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 }
609 return 0;
610}
611
Alexey Klimov04e0ffb2008-11-08 00:40:46 -0300612/* Suspend device - stop device. */
613static int usb_dsbr100_suspend(struct usb_interface *intf, pm_message_t message)
614{
615 struct dsbr100_device *radio = usb_get_intfdata(intf);
616 int retval;
617
618 retval = dsbr100_stop(radio);
619 if (retval == -1)
620 dev_warn(&intf->dev, "dsbr100_stop failed\n");
621
622 dev_info(&intf->dev, "going into suspend..\n");
623
624 return 0;
625}
626
627/* Resume device - start device. */
628static int usb_dsbr100_resume(struct usb_interface *intf)
629{
630 struct dsbr100_device *radio = usb_get_intfdata(intf);
631 int retval;
632
633 retval = dsbr100_start(radio);
634 if (retval == -1)
635 dev_warn(&intf->dev, "dsbr100_start failed\n");
636
637 dev_info(&intf->dev, "coming out of suspend..\n");
638
639 return 0;
640}
641
Alexey Klimovfc55bcb2008-12-27 22:33:54 -0300642/* free data structures */
Alexey Klimov3a0efc32008-12-27 21:32:49 -0300643static void usb_dsbr100_video_device_release(struct video_device *videodev)
644{
645 struct dsbr100_device *radio = videodev_to_radio(videodev);
646
647 kfree(radio->transfer_buffer);
648 kfree(radio);
649}
650
Douglas Landgraf7002a4f2007-05-07 16:43:01 -0300651/* File system interface */
652static const struct file_operations usb_dsbr100_fops = {
653 .owner = THIS_MODULE,
654 .open = usb_dsbr100_open,
655 .release = usb_dsbr100_close,
656 .ioctl = video_ioctl2,
Douglas Schilling Landgraf078ff792008-04-22 14:46:11 -0300657#ifdef CONFIG_COMPAT
Douglas Landgraf7002a4f2007-05-07 16:43:01 -0300658 .compat_ioctl = v4l_compat_ioctl32,
Douglas Schilling Landgraf078ff792008-04-22 14:46:11 -0300659#endif
Douglas Landgraf7002a4f2007-05-07 16:43:01 -0300660 .llseek = no_llseek,
661};
662
Hans Verkuila3998102008-07-21 02:57:38 -0300663static const struct v4l2_ioctl_ops usb_dsbr100_ioctl_ops = {
Douglas Landgraf7002a4f2007-05-07 16:43:01 -0300664 .vidioc_querycap = vidioc_querycap,
665 .vidioc_g_tuner = vidioc_g_tuner,
666 .vidioc_s_tuner = vidioc_s_tuner,
667 .vidioc_g_frequency = vidioc_g_frequency,
668 .vidioc_s_frequency = vidioc_s_frequency,
669 .vidioc_queryctrl = vidioc_queryctrl,
670 .vidioc_g_ctrl = vidioc_g_ctrl,
671 .vidioc_s_ctrl = vidioc_s_ctrl,
672 .vidioc_g_audio = vidioc_g_audio,
673 .vidioc_s_audio = vidioc_s_audio,
674 .vidioc_g_input = vidioc_g_input,
675 .vidioc_s_input = vidioc_s_input,
676};
677
Hans Verkuila3998102008-07-21 02:57:38 -0300678/* V4L2 interface */
Alexey Klimov3a0efc32008-12-27 21:32:49 -0300679static struct video_device dsbr100_videodev_data = {
Hans Verkuila3998102008-07-21 02:57:38 -0300680 .name = "D-Link DSB-R 100",
Hans Verkuila3998102008-07-21 02:57:38 -0300681 .fops = &usb_dsbr100_fops,
682 .ioctl_ops = &usb_dsbr100_ioctl_ops,
Alexey Klimov3a0efc32008-12-27 21:32:49 -0300683 .release = usb_dsbr100_video_device_release,
Hans Verkuila3998102008-07-21 02:57:38 -0300684};
685
Alexey Klimovfc55bcb2008-12-27 22:33:54 -0300686/* check if the device is present and register with v4l and usb if it is */
Douglas Landgraf7002a4f2007-05-07 16:43:01 -0300687static int usb_dsbr100_probe(struct usb_interface *intf,
688 const struct usb_device_id *id)
689{
690 struct dsbr100_device *radio;
Alexey Klimov417b7952008-12-27 22:30:29 -0300691 int retval;
Douglas Landgraf7002a4f2007-05-07 16:43:01 -0300692
Alexey Klimov223377e2008-10-19 23:50:27 -0300693 radio = kmalloc(sizeof(struct dsbr100_device), GFP_KERNEL);
694
695 if (!radio)
Douglas Landgraf7002a4f2007-05-07 16:43:01 -0300696 return -ENOMEM;
Alexey Klimov223377e2008-10-19 23:50:27 -0300697
698 radio->transfer_buffer = kmalloc(TB_LEN, GFP_KERNEL);
699
700 if (!(radio->transfer_buffer)) {
Oliver Neukum863c86d2007-12-03 06:48:43 -0300701 kfree(radio);
702 return -ENOMEM;
703 }
Alexey Klimov223377e2008-10-19 23:50:27 -0300704
Alexey Klimov3a0efc32008-12-27 21:32:49 -0300705 mutex_init(&radio->lock);
706 radio->videodev = dsbr100_videodev_data;
707
Douglas Landgraf7002a4f2007-05-07 16:43:01 -0300708 radio->removed = 0;
709 radio->users = 0;
710 radio->usbdev = interface_to_usbdev(intf);
Alexey Klimov223377e2008-10-19 23:50:27 -0300711 radio->curfreq = FREQ_MIN * FREQ_MUL;
Alexey Klimov3a0efc32008-12-27 21:32:49 -0300712 video_set_drvdata(&radio->videodev, radio);
Alexey Klimov417b7952008-12-27 22:30:29 -0300713 retval = video_register_device(&radio->videodev, VFL_TYPE_RADIO, radio_nr);
714 if (retval < 0) {
Alexey Klimov7e1ca842008-12-27 22:31:52 -0300715 dev_err(&intf->dev, "couldn't register video device\n");
Oliver Neukum863c86d2007-12-03 06:48:43 -0300716 kfree(radio->transfer_buffer);
Douglas Landgraf7002a4f2007-05-07 16:43:01 -0300717 kfree(radio);
718 return -EIO;
719 }
720 usb_set_intfdata(intf, radio);
721 return 0;
722}
723
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724static int __init dsbr100_init(void)
725{
726 int retval = usb_register(&usb_dsbr100_driver);
Greg Kroah-Hartmana482f322008-10-10 05:08:23 -0300727 printk(KERN_INFO KBUILD_MODNAME ": " DRIVER_VERSION ":"
728 DRIVER_DESC "\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 return retval;
730}
731
732static void __exit dsbr100_exit(void)
733{
734 usb_deregister(&usb_dsbr100_driver);
735}
736
737module_init (dsbr100_init);
738module_exit (dsbr100_exit);
739
740MODULE_AUTHOR( DRIVER_AUTHOR );
741MODULE_DESCRIPTION( DRIVER_DESC );
742MODULE_LICENSE("GPL");