blob: 6135762022940ec4095538bfa446803fb2be6373 [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 Klimov406827c2009-04-03 18:45:17 -030036 Version 0.45:
37 Converted to v4l2_device.
38
Alexey Klimov6076dbf2008-12-27 22:35:21 -030039 Version 0.44:
40 Add suspend/resume functions, fix unplug of device,
41 a lot of cleanups and fixes by Alexey Klimov <klimov.linux@gmail.com>
42
Oliver Neukum863c86d2007-12-03 06:48:43 -030043 Version 0.43:
44 Oliver Neukum: avoided DMA coherency issue
45
Douglas Landgraf7002a4f2007-05-07 16:43:01 -030046 Version 0.42:
47 Converted dsbr100 to use video_ioctl2
48 by Douglas Landgraf <dougsland@gmail.com>
49
Alan Cox5aff3082006-08-08 15:47:50 -030050 Version 0.41-ac1:
51 Alan Cox: Some cleanups and fixes
52
53 Version 0.41:
54 Converted to V4L2 API by Mauro Carvalho Chehab <mchehab@infradead.org>
55
Linus Torvalds1da177e2005-04-16 15:20:36 -070056 Version 0.40:
Alan Cox5aff3082006-08-08 15:47:50 -030057 Markus: Updates for 2.6.x kernels, code layout changes, name sanitizing
Linus Torvalds1da177e2005-04-16 15:20:36 -070058
59 Version 0.30:
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -030060 Markus: Updates for 2.5.x kernel and more ISO compliant source
Linus Torvalds1da177e2005-04-16 15:20:36 -070061
62 Version 0.25:
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -030063 PSL and Markus: Cleanup, radio now doesn't stop on device close
Linus Torvalds1da177e2005-04-16 15:20:36 -070064
65 Version 0.24:
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -030066 Markus: Hope I got these silly VIDEO_TUNER_LOW issues finally
Linus Torvalds1da177e2005-04-16 15:20:36 -070067 right. Some minor cleanup, improved standalone compilation
68
69 Version 0.23:
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -030070 Markus: Sign extension bug fixed by declaring transfer_buffer unsigned
Linus Torvalds1da177e2005-04-16 15:20:36 -070071
72 Version 0.22:
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -030073 Markus: Some (brown bag) cleanup in what VIDIOCSTUNER returns,
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 thanks to Mike Cox for pointing the problem out.
75
76 Version 0.21:
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -030077 Markus: Minor cleanup, warnings if something goes wrong, lame attempt
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 to adhere to Documentation/CodingStyle
79
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -030080 Version 0.2:
81 Brad Hards <bradh@dynamite.com.au>: Fixes to make it work as non-module
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 Markus: Copyright clarification
83
84 Version 0.01: Markus: initial release
85
86*/
87
Linus Torvalds1da177e2005-04-16 15:20:36 -070088#include <linux/kernel.h>
89#include <linux/module.h>
90#include <linux/init.h>
91#include <linux/slab.h>
92#include <linux/input.h>
Alan Cox5aff3082006-08-08 15:47:50 -030093#include <linux/videodev2.h>
Alexey Klimov406827c2009-04-03 18:45:17 -030094#include <media/v4l2-device.h>
Hans Verkuil35ea11f2008-07-20 08:12:02 -030095#include <media/v4l2-ioctl.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070096#include <linux/usb.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070097
98/*
99 * Version Information
100 */
Alan Cox5aff3082006-08-08 15:47:50 -0300101#include <linux/version.h> /* for KERNEL_VERSION MACRO */
102
Alexey Klimov406827c2009-04-03 18:45:17 -0300103#define DRIVER_VERSION "v0.45"
104#define RADIO_VERSION KERNEL_VERSION(0, 4, 5)
Alan Cox5aff3082006-08-08 15:47:50 -0300105
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106#define DRIVER_AUTHOR "Markus Demleitner <msdemlei@tucana.harvard.edu>"
107#define DRIVER_DESC "D-Link DSB-R100 USB FM radio driver"
108
109#define DSB100_VENDOR 0x04b4
110#define DSB100_PRODUCT 0x1002
111
112/* Commands the device appears to understand */
113#define DSB100_TUNE 1
114#define DSB100_ONOFF 2
115
116#define TB_LEN 16
117
118/* Frequency limits in MHz -- these are European values. For Japanese
119devices, that would be 76 and 91. */
120#define FREQ_MIN 87.5
121#define FREQ_MAX 108.0
122#define FREQ_MUL 16000
123
Alexey Klimov3a0efc32008-12-27 21:32:49 -0300124#define videodev_to_radio(d) container_of(d, struct dsbr100_device, videodev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125
126static int usb_dsbr100_probe(struct usb_interface *intf,
127 const struct usb_device_id *id);
128static void usb_dsbr100_disconnect(struct usb_interface *intf);
Hans Verkuilbec43662008-12-30 06:58:20 -0300129static int usb_dsbr100_open(struct file *file);
130static int usb_dsbr100_close(struct file *file);
Alexey Klimov04e0ffb2008-11-08 00:40:46 -0300131static int usb_dsbr100_suspend(struct usb_interface *intf,
132 pm_message_t message);
133static int usb_dsbr100_resume(struct usb_interface *intf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134
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;
Alexey Klimov3a0efc32008-12-27 21:32:49 -0300141 struct video_device videodev;
Alexey Klimov406827c2009-04-03 18:45:17 -0300142 struct v4l2_device v4l2_dev;
143
Oliver Neukum863c86d2007-12-03 06:48:43 -0300144 u8 *transfer_buffer;
Alexey Klimov3a0efc32008-12-27 21:32:49 -0300145 struct mutex lock; /* buffer locking */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 int curfreq;
147 int stereo;
148 int users;
149 int removed;
Alan Cox5aff3082006-08-08 15:47:50 -0300150 int muted;
151};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153static struct usb_device_id usb_dsbr100_device_table [] = {
154 { USB_DEVICE(DSB100_VENDOR, DSB100_PRODUCT) },
155 { } /* Terminating entry */
156};
157
158MODULE_DEVICE_TABLE (usb, usb_dsbr100_device_table);
159
160/* USB subsystem interface */
161static struct usb_driver usb_dsbr100_driver = {
Alexey Klimov04e0ffb2008-11-08 00:40:46 -0300162 .name = "dsbr100",
163 .probe = usb_dsbr100_probe,
164 .disconnect = usb_dsbr100_disconnect,
165 .id_table = usb_dsbr100_device_table,
166 .suspend = usb_dsbr100_suspend,
167 .resume = usb_dsbr100_resume,
168 .reset_resume = usb_dsbr100_resume,
169 .supports_autosuspend = 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170};
171
172/* Low-level device interface begins here */
173
174/* switch on radio */
Alan Cox5aff3082006-08-08 15:47:50 -0300175static int dsbr100_start(struct dsbr100_device *radio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176{
Alexey Klimov417b7952008-12-27 22:30:29 -0300177 int retval;
178 int request;
179
Alexey Klimov3a0efc32008-12-27 21:32:49 -0300180 mutex_lock(&radio->lock);
Alexey Klimov417b7952008-12-27 22:30:29 -0300181
182 retval = usb_control_msg(radio->usbdev,
183 usb_rcvctrlpipe(radio->usbdev, 0),
184 USB_REQ_GET_STATUS,
185 USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN,
186 0x00, 0xC7, radio->transfer_buffer, 8, 300);
187
188 if (retval < 0) {
189 request = USB_REQ_GET_STATUS;
190 goto usb_control_msg_failed;
Alexey Klimov3a0efc32008-12-27 21:32:49 -0300191 }
192
Alexey Klimov417b7952008-12-27 22:30:29 -0300193 retval = usb_control_msg(radio->usbdev,
194 usb_rcvctrlpipe(radio->usbdev, 0),
195 DSB100_ONOFF,
196 USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN,
197 0x01, 0x00, radio->transfer_buffer, 8, 300);
198
199 if (retval < 0) {
200 request = DSB100_ONOFF;
201 goto usb_control_msg_failed;
202 }
203
204 radio->muted = 0;
Alexey Klimov3a0efc32008-12-27 21:32:49 -0300205 mutex_unlock(&radio->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 return (radio->transfer_buffer)[0];
Alexey Klimov417b7952008-12-27 22:30:29 -0300207
208usb_control_msg_failed:
209 mutex_unlock(&radio->lock);
210 dev_err(&radio->usbdev->dev,
211 "%s - usb_control_msg returned %i, request %i\n",
212 __func__, retval, request);
Alexey Klimovd25cb642008-12-27 22:40:57 -0300213 return retval;
Alexey Klimov417b7952008-12-27 22:30:29 -0300214
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215}
216
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217/* switch off radio */
Alan Cox5aff3082006-08-08 15:47:50 -0300218static int dsbr100_stop(struct dsbr100_device *radio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219{
Alexey Klimov417b7952008-12-27 22:30:29 -0300220 int retval;
221 int request;
222
Alexey Klimov3a0efc32008-12-27 21:32:49 -0300223 mutex_lock(&radio->lock);
Alexey Klimov417b7952008-12-27 22:30:29 -0300224
225 retval = usb_control_msg(radio->usbdev,
226 usb_rcvctrlpipe(radio->usbdev, 0),
227 USB_REQ_GET_STATUS,
228 USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN,
229 0x16, 0x1C, radio->transfer_buffer, 8, 300);
230
231 if (retval < 0) {
232 request = USB_REQ_GET_STATUS;
233 goto usb_control_msg_failed;
Alexey Klimov3a0efc32008-12-27 21:32:49 -0300234 }
235
Alexey Klimov417b7952008-12-27 22:30:29 -0300236 retval = usb_control_msg(radio->usbdev,
237 usb_rcvctrlpipe(radio->usbdev, 0),
238 DSB100_ONOFF,
239 USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN,
240 0x00, 0x00, radio->transfer_buffer, 8, 300);
241
242 if (retval < 0) {
243 request = DSB100_ONOFF;
244 goto usb_control_msg_failed;
245 }
246
247 radio->muted = 1;
Alexey Klimov3a0efc32008-12-27 21:32:49 -0300248 mutex_unlock(&radio->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 return (radio->transfer_buffer)[0];
Alexey Klimov417b7952008-12-27 22:30:29 -0300250
251usb_control_msg_failed:
252 mutex_unlock(&radio->lock);
253 dev_err(&radio->usbdev->dev,
254 "%s - usb_control_msg returned %i, request %i\n",
255 __func__, retval, request);
Alexey Klimovd25cb642008-12-27 22:40:57 -0300256 return retval;
Alexey Klimov417b7952008-12-27 22:30:29 -0300257
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258}
259
260/* set a frequency, freq is defined by v4l's TUNER_LOW, i.e. 1/16th kHz */
Alan Cox5aff3082006-08-08 15:47:50 -0300261static int dsbr100_setfreq(struct dsbr100_device *radio, int freq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262{
Alexey Klimov417b7952008-12-27 22:30:29 -0300263 int retval;
264 int request;
265
Alexey Klimov223377e2008-10-19 23:50:27 -0300266 freq = (freq / 16 * 80) / 1000 + 856;
Alexey Klimov3a0efc32008-12-27 21:32:49 -0300267 mutex_lock(&radio->lock);
Alexey Klimov417b7952008-12-27 22:30:29 -0300268
269 retval = usb_control_msg(radio->usbdev,
270 usb_rcvctrlpipe(radio->usbdev, 0),
271 DSB100_TUNE,
272 USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN,
273 (freq >> 8) & 0x00ff, freq & 0xff,
274 radio->transfer_buffer, 8, 300);
275
276 if (retval < 0) {
277 request = DSB100_TUNE;
278 goto usb_control_msg_failed;
279 }
280
281 retval = usb_control_msg(radio->usbdev,
282 usb_rcvctrlpipe(radio->usbdev, 0),
283 USB_REQ_GET_STATUS,
284 USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN,
285 0x96, 0xB7, radio->transfer_buffer, 8, 300);
286
287 if (retval < 0) {
288 request = USB_REQ_GET_STATUS;
289 goto usb_control_msg_failed;
290 }
291
292 retval = usb_control_msg(radio->usbdev,
293 usb_rcvctrlpipe(radio->usbdev, 0),
294 USB_REQ_GET_STATUS,
295 USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN,
296 0x00, 0x24, radio->transfer_buffer, 8, 300);
297
298 if (retval < 0) {
299 request = USB_REQ_GET_STATUS;
300 goto usb_control_msg_failed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 }
Alexey Klimov3a0efc32008-12-27 21:32:49 -0300302
Alexey Klimov223377e2008-10-19 23:50:27 -0300303 radio->stereo = !((radio->transfer_buffer)[0] & 0x01);
Alexey Klimov3a0efc32008-12-27 21:32:49 -0300304 mutex_unlock(&radio->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 return (radio->transfer_buffer)[0];
Alexey Klimov417b7952008-12-27 22:30:29 -0300306
307usb_control_msg_failed:
308 radio->stereo = -1;
309 mutex_unlock(&radio->lock);
310 dev_err(&radio->usbdev->dev,
311 "%s - usb_control_msg returned %i, request %i\n",
312 __func__, retval, request);
Alexey Klimovd25cb642008-12-27 22:40:57 -0300313 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314}
315
316/* return the device status. This is, in effect, just whether it
317sees a stereo signal or not. Pity. */
Alan Cox5aff3082006-08-08 15:47:50 -0300318static void dsbr100_getstat(struct dsbr100_device *radio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319{
Alexey Klimov417b7952008-12-27 22:30:29 -0300320 int retval;
321
Alexey Klimov3a0efc32008-12-27 21:32:49 -0300322 mutex_lock(&radio->lock);
Alexey Klimov417b7952008-12-27 22:30:29 -0300323
324 retval = usb_control_msg(radio->usbdev,
325 usb_rcvctrlpipe(radio->usbdev, 0),
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300326 USB_REQ_GET_STATUS,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN,
Alexey Klimov417b7952008-12-27 22:30:29 -0300328 0x00 , 0x24, radio->transfer_buffer, 8, 300);
329
330 if (retval < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 radio->stereo = -1;
Alexey Klimov417b7952008-12-27 22:30:29 -0300332 dev_err(&radio->usbdev->dev,
333 "%s - usb_control_msg returned %i, request %i\n",
334 __func__, retval, USB_REQ_GET_STATUS);
335 } else {
Alexey Klimov223377e2008-10-19 23:50:27 -0300336 radio->stereo = !(radio->transfer_buffer[0] & 0x01);
Alexey Klimov417b7952008-12-27 22:30:29 -0300337 }
338
Alexey Klimov3a0efc32008-12-27 21:32:49 -0300339 mutex_unlock(&radio->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340}
341
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342/* USB subsystem interface begins here */
343
Alexey Klimovfc55bcb2008-12-27 22:33:54 -0300344/*
345 * Handle unplugging of the device.
346 * We call video_unregister_device in any case.
347 * The last function called in this procedure is
348 * usb_dsbr100_video_device_release
349 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350static void usb_dsbr100_disconnect(struct usb_interface *intf)
351{
Alan Cox5aff3082006-08-08 15:47:50 -0300352 struct dsbr100_device *radio = usb_get_intfdata(intf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353
354 usb_set_intfdata (intf, NULL);
Alexey Klimov3a0efc32008-12-27 21:32:49 -0300355
356 mutex_lock(&radio->lock);
357 radio->removed = 1;
358 mutex_unlock(&radio->lock);
359
360 video_unregister_device(&radio->videodev);
Alexey Klimov406827c2009-04-03 18:45:17 -0300361 v4l2_device_disconnect(&radio->v4l2_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362}
363
364
Douglas Landgraf7002a4f2007-05-07 16:43:01 -0300365static int vidioc_querycap(struct file *file, void *priv,
366 struct v4l2_capability *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367{
Alexey Klimovc7181cf2009-01-25 20:05:58 -0300368 struct dsbr100_device *radio = video_drvdata(file);
369
Douglas Landgraf7002a4f2007-05-07 16:43:01 -0300370 strlcpy(v->driver, "dsbr100", sizeof(v->driver));
371 strlcpy(v->card, "D-Link R-100 USB FM Radio", sizeof(v->card));
Alexey Klimovc7181cf2009-01-25 20:05:58 -0300372 usb_make_path(radio->usbdev, v->bus_info, sizeof(v->bus_info));
Douglas Landgraf7002a4f2007-05-07 16:43:01 -0300373 v->version = RADIO_VERSION;
374 v->capabilities = V4L2_CAP_TUNER;
375 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376}
377
Douglas Landgraf7002a4f2007-05-07 16:43:01 -0300378static int vidioc_g_tuner(struct file *file, void *priv,
379 struct v4l2_tuner *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380{
Hans Verkuilc170ecf2008-08-23 08:32:09 -0300381 struct dsbr100_device *radio = video_drvdata(file);
Douglas Landgraf7002a4f2007-05-07 16:43:01 -0300382
Alexey Klimov3a0efc32008-12-27 21:32:49 -0300383 /* safety check */
384 if (radio->removed)
385 return -EIO;
386
Douglas Landgraf7002a4f2007-05-07 16:43:01 -0300387 if (v->index > 0)
388 return -EINVAL;
389
390 dsbr100_getstat(radio);
391 strcpy(v->name, "FM");
392 v->type = V4L2_TUNER_RADIO;
Alexey Klimov223377e2008-10-19 23:50:27 -0300393 v->rangelow = FREQ_MIN * FREQ_MUL;
394 v->rangehigh = FREQ_MAX * FREQ_MUL;
395 v->rxsubchans = V4L2_TUNER_SUB_MONO | V4L2_TUNER_SUB_STEREO;
Douglas Landgraf7002a4f2007-05-07 16:43:01 -0300396 v->capability = V4L2_TUNER_CAP_LOW;
397 if(radio->stereo)
398 v->audmode = V4L2_TUNER_MODE_STEREO;
399 else
400 v->audmode = V4L2_TUNER_MODE_MONO;
401 v->signal = 0xffff; /* We can't get the signal strength */
402 return 0;
403}
404
405static int vidioc_s_tuner(struct file *file, void *priv,
406 struct v4l2_tuner *v)
407{
Alexey Klimov3a0efc32008-12-27 21:32:49 -0300408 struct dsbr100_device *radio = video_drvdata(file);
409
410 /* safety check */
411 if (radio->removed)
412 return -EIO;
413
Douglas Landgraf7002a4f2007-05-07 16:43:01 -0300414 if (v->index > 0)
415 return -EINVAL;
416
417 return 0;
418}
419
420static int vidioc_s_frequency(struct file *file, void *priv,
421 struct v4l2_frequency *f)
422{
Hans Verkuilc170ecf2008-08-23 08:32:09 -0300423 struct dsbr100_device *radio = video_drvdata(file);
Alexey Klimov417b7952008-12-27 22:30:29 -0300424 int retval;
Douglas Landgraf7002a4f2007-05-07 16:43:01 -0300425
Alexey Klimov3a0efc32008-12-27 21:32:49 -0300426 /* safety check */
427 if (radio->removed)
428 return -EIO;
429
Alexey Klimovfdf9c992009-02-08 02:00:14 -0300430 mutex_lock(&radio->lock);
Douglas Landgraf7002a4f2007-05-07 16:43:01 -0300431 radio->curfreq = f->frequency;
Alexey Klimovfdf9c992009-02-08 02:00:14 -0300432 mutex_unlock(&radio->lock);
433
Alexey Klimov417b7952008-12-27 22:30:29 -0300434 retval = dsbr100_setfreq(radio, radio->curfreq);
Alexey Klimovd25cb642008-12-27 22:40:57 -0300435 if (retval < 0)
Greg Kroah-Hartmanaa826612008-08-14 09:37:34 -0700436 dev_warn(&radio->usbdev->dev, "Set frequency failed\n");
Douglas Landgraf7002a4f2007-05-07 16:43:01 -0300437 return 0;
438}
439
440static int vidioc_g_frequency(struct file *file, void *priv,
441 struct v4l2_frequency *f)
442{
Hans Verkuilc170ecf2008-08-23 08:32:09 -0300443 struct dsbr100_device *radio = video_drvdata(file);
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 f->type = V4L2_TUNER_RADIO;
450 f->frequency = radio->curfreq;
451 return 0;
452}
453
454static int vidioc_queryctrl(struct file *file, void *priv,
455 struct v4l2_queryctrl *qc)
456{
Alexey Klimov406827c2009-04-03 18:45:17 -0300457 switch (qc->id) {
458 case V4L2_CID_AUDIO_MUTE:
459 return v4l2_ctrl_query_fill(qc, 0, 1, 1, 1);
Douglas Landgraf7002a4f2007-05-07 16:43:01 -0300460 }
Alexey Klimov406827c2009-04-03 18:45:17 -0300461
Douglas Landgraf7002a4f2007-05-07 16:43:01 -0300462 return -EINVAL;
463}
464
465static int vidioc_g_ctrl(struct file *file, void *priv,
466 struct v4l2_control *ctrl)
467{
Hans Verkuilc170ecf2008-08-23 08:32:09 -0300468 struct dsbr100_device *radio = video_drvdata(file);
Douglas Landgraf7002a4f2007-05-07 16:43:01 -0300469
Alexey Klimov3a0efc32008-12-27 21:32:49 -0300470 /* safety check */
471 if (radio->removed)
472 return -EIO;
473
Douglas Landgraf7002a4f2007-05-07 16:43:01 -0300474 switch (ctrl->id) {
475 case V4L2_CID_AUDIO_MUTE:
476 ctrl->value = radio->muted;
477 return 0;
478 }
479 return -EINVAL;
480}
481
482static int vidioc_s_ctrl(struct file *file, void *priv,
483 struct v4l2_control *ctrl)
484{
Hans Verkuilc170ecf2008-08-23 08:32:09 -0300485 struct dsbr100_device *radio = video_drvdata(file);
Alexey Klimov417b7952008-12-27 22:30:29 -0300486 int retval;
Douglas Landgraf7002a4f2007-05-07 16:43:01 -0300487
Alexey Klimov3a0efc32008-12-27 21:32:49 -0300488 /* safety check */
489 if (radio->removed)
490 return -EIO;
491
Douglas Landgraf7002a4f2007-05-07 16:43:01 -0300492 switch (ctrl->id) {
493 case V4L2_CID_AUDIO_MUTE:
494 if (ctrl->value) {
Alexey Klimov417b7952008-12-27 22:30:29 -0300495 retval = dsbr100_stop(radio);
Alexey Klimovd25cb642008-12-27 22:40:57 -0300496 if (retval < 0) {
Greg Kroah-Hartmanaa826612008-08-14 09:37:34 -0700497 dev_warn(&radio->usbdev->dev,
498 "Radio did not respond properly\n");
Alexey Klimov90b698d2008-10-09 13:42:32 -0300499 return -EBUSY;
500 }
Douglas Landgraf7002a4f2007-05-07 16:43:01 -0300501 } else {
Alexey Klimov417b7952008-12-27 22:30:29 -0300502 retval = dsbr100_start(radio);
Alexey Klimovd25cb642008-12-27 22:40:57 -0300503 if (retval < 0) {
Greg Kroah-Hartmanaa826612008-08-14 09:37:34 -0700504 dev_warn(&radio->usbdev->dev,
505 "Radio did not respond properly\n");
Alexey Klimov90b698d2008-10-09 13:42:32 -0300506 return -EBUSY;
507 }
Douglas Landgraf7002a4f2007-05-07 16:43:01 -0300508 }
509 return 0;
510 }
511 return -EINVAL;
512}
513
514static int vidioc_g_audio(struct file *file, void *priv,
515 struct v4l2_audio *a)
516{
517 if (a->index > 1)
518 return -EINVAL;
519
520 strcpy(a->name, "Radio");
521 a->capability = V4L2_AUDCAP_STEREO;
522 return 0;
523}
524
525static int vidioc_g_input(struct file *filp, void *priv, unsigned int *i)
526{
527 *i = 0;
528 return 0;
529}
530
531static int vidioc_s_input(struct file *filp, void *priv, unsigned int i)
532{
533 if (i != 0)
534 return -EINVAL;
535 return 0;
536}
537
538static int vidioc_s_audio(struct file *file, void *priv,
539 struct v4l2_audio *a)
540{
541 if (a->index != 0)
542 return -EINVAL;
543 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544}
545
Hans Verkuilbec43662008-12-30 06:58:20 -0300546static int usb_dsbr100_open(struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547{
Hans Verkuilc170ecf2008-08-23 08:32:09 -0300548 struct dsbr100_device *radio = video_drvdata(file);
Alexey Klimovb9f35732008-10-20 00:00:03 -0300549 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550
Hans Verkuild56dc612008-07-30 08:43:36 -0300551 lock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 radio->users = 1;
Alan Cox5aff3082006-08-08 15:47:50 -0300553 radio->muted = 1;
554
Alexey Klimov417b7952008-12-27 22:30:29 -0300555 retval = dsbr100_start(radio);
556 if (retval < 0) {
Greg Kroah-Hartmanaa826612008-08-14 09:37:34 -0700557 dev_warn(&radio->usbdev->dev,
558 "Radio did not start up properly\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 radio->users = 0;
Hans Verkuild56dc612008-07-30 08:43:36 -0300560 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 return -EIO;
562 }
Alexey Klimovb9f35732008-10-20 00:00:03 -0300563
564 retval = dsbr100_setfreq(radio, radio->curfreq);
Alexey Klimovd25cb642008-12-27 22:40:57 -0300565 if (retval < 0)
Alexey Klimov290588e2008-12-27 21:42:39 -0300566 dev_warn(&radio->usbdev->dev,
567 "set frequency failed\n");
Alexey Klimovb9f35732008-10-20 00:00:03 -0300568
Hans Verkuild56dc612008-07-30 08:43:36 -0300569 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 return 0;
571}
572
Hans Verkuilbec43662008-12-30 06:58:20 -0300573static int usb_dsbr100_close(struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574{
Hans Verkuilc170ecf2008-08-23 08:32:09 -0300575 struct dsbr100_device *radio = video_drvdata(file);
Alexey Klimov3a0efc32008-12-27 21:32:49 -0300576 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577
578 if (!radio)
579 return -ENODEV;
Alexey Klimov3a0efc32008-12-27 21:32:49 -0300580
Alexey Klimovfdf9c992009-02-08 02:00:14 -0300581 mutex_lock(&radio->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 radio->users = 0;
Alexey Klimovfdf9c992009-02-08 02:00:14 -0300583 mutex_unlock(&radio->lock);
584
Alexey Klimov3a0efc32008-12-27 21:32:49 -0300585 if (!radio->removed) {
586 retval = dsbr100_stop(radio);
Alexey Klimovd25cb642008-12-27 22:40:57 -0300587 if (retval < 0) {
Alexey Klimov3a0efc32008-12-27 21:32:49 -0300588 dev_warn(&radio->usbdev->dev,
589 "dsbr100_stop failed\n");
590 }
591
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 }
593 return 0;
594}
595
Alexey Klimov04e0ffb2008-11-08 00:40:46 -0300596/* Suspend device - stop device. */
597static int usb_dsbr100_suspend(struct usb_interface *intf, pm_message_t message)
598{
599 struct dsbr100_device *radio = usb_get_intfdata(intf);
600 int retval;
601
602 retval = dsbr100_stop(radio);
Alexey Klimovd25cb642008-12-27 22:40:57 -0300603 if (retval < 0)
Alexey Klimov04e0ffb2008-11-08 00:40:46 -0300604 dev_warn(&intf->dev, "dsbr100_stop failed\n");
605
606 dev_info(&intf->dev, "going into suspend..\n");
607
608 return 0;
609}
610
611/* Resume device - start device. */
612static int usb_dsbr100_resume(struct usb_interface *intf)
613{
614 struct dsbr100_device *radio = usb_get_intfdata(intf);
615 int retval;
616
617 retval = dsbr100_start(radio);
Alexey Klimovd25cb642008-12-27 22:40:57 -0300618 if (retval < 0)
Alexey Klimov04e0ffb2008-11-08 00:40:46 -0300619 dev_warn(&intf->dev, "dsbr100_start failed\n");
620
621 dev_info(&intf->dev, "coming out of suspend..\n");
622
623 return 0;
624}
625
Alexey Klimovfc55bcb2008-12-27 22:33:54 -0300626/* free data structures */
Alexey Klimov3a0efc32008-12-27 21:32:49 -0300627static void usb_dsbr100_video_device_release(struct video_device *videodev)
628{
629 struct dsbr100_device *radio = videodev_to_radio(videodev);
630
Alexey Klimov406827c2009-04-03 18:45:17 -0300631 v4l2_device_unregister(&radio->v4l2_dev);
Alexey Klimov3a0efc32008-12-27 21:32:49 -0300632 kfree(radio->transfer_buffer);
633 kfree(radio);
634}
635
Douglas Landgraf7002a4f2007-05-07 16:43:01 -0300636/* File system interface */
Hans Verkuilbec43662008-12-30 06:58:20 -0300637static const struct v4l2_file_operations usb_dsbr100_fops = {
Douglas Landgraf7002a4f2007-05-07 16:43:01 -0300638 .owner = THIS_MODULE,
639 .open = usb_dsbr100_open,
640 .release = usb_dsbr100_close,
641 .ioctl = video_ioctl2,
Douglas Landgraf7002a4f2007-05-07 16:43:01 -0300642};
643
Hans Verkuila3998102008-07-21 02:57:38 -0300644static const struct v4l2_ioctl_ops usb_dsbr100_ioctl_ops = {
Douglas Landgraf7002a4f2007-05-07 16:43:01 -0300645 .vidioc_querycap = vidioc_querycap,
646 .vidioc_g_tuner = vidioc_g_tuner,
647 .vidioc_s_tuner = vidioc_s_tuner,
648 .vidioc_g_frequency = vidioc_g_frequency,
649 .vidioc_s_frequency = vidioc_s_frequency,
650 .vidioc_queryctrl = vidioc_queryctrl,
651 .vidioc_g_ctrl = vidioc_g_ctrl,
652 .vidioc_s_ctrl = vidioc_s_ctrl,
653 .vidioc_g_audio = vidioc_g_audio,
654 .vidioc_s_audio = vidioc_s_audio,
655 .vidioc_g_input = vidioc_g_input,
656 .vidioc_s_input = vidioc_s_input,
657};
658
Alexey Klimovfc55bcb2008-12-27 22:33:54 -0300659/* check if the device is present and register with v4l and usb if it is */
Douglas Landgraf7002a4f2007-05-07 16:43:01 -0300660static int usb_dsbr100_probe(struct usb_interface *intf,
661 const struct usb_device_id *id)
662{
663 struct dsbr100_device *radio;
Alexey Klimov406827c2009-04-03 18:45:17 -0300664 struct v4l2_device *v4l2_dev;
Alexey Klimov417b7952008-12-27 22:30:29 -0300665 int retval;
Douglas Landgraf7002a4f2007-05-07 16:43:01 -0300666
Alexey Klimov406827c2009-04-03 18:45:17 -0300667 radio = kzalloc(sizeof(struct dsbr100_device), GFP_KERNEL);
Alexey Klimov223377e2008-10-19 23:50:27 -0300668
669 if (!radio)
Douglas Landgraf7002a4f2007-05-07 16:43:01 -0300670 return -ENOMEM;
Alexey Klimov223377e2008-10-19 23:50:27 -0300671
672 radio->transfer_buffer = kmalloc(TB_LEN, GFP_KERNEL);
673
674 if (!(radio->transfer_buffer)) {
Oliver Neukum863c86d2007-12-03 06:48:43 -0300675 kfree(radio);
676 return -ENOMEM;
677 }
Alexey Klimov223377e2008-10-19 23:50:27 -0300678
Alexey Klimov406827c2009-04-03 18:45:17 -0300679 v4l2_dev = &radio->v4l2_dev;
680
681 retval = v4l2_device_register(&intf->dev, v4l2_dev);
682 if (retval < 0) {
683 v4l2_err(v4l2_dev, "couldn't register v4l2_device\n");
684 kfree(radio->transfer_buffer);
685 kfree(radio);
686 return retval;
687 }
688
689 strlcpy(radio->videodev.name, v4l2_dev->name, sizeof(radio->videodev.name));
690 radio->videodev.v4l2_dev = v4l2_dev;
691 radio->videodev.fops = &usb_dsbr100_fops;
692 radio->videodev.ioctl_ops = &usb_dsbr100_ioctl_ops;
693 radio->videodev.release = usb_dsbr100_video_device_release;
694
Alexey Klimov3a0efc32008-12-27 21:32:49 -0300695 mutex_init(&radio->lock);
Alexey Klimov3a0efc32008-12-27 21:32:49 -0300696
Douglas Landgraf7002a4f2007-05-07 16:43:01 -0300697 radio->removed = 0;
698 radio->users = 0;
699 radio->usbdev = interface_to_usbdev(intf);
Alexey Klimov223377e2008-10-19 23:50:27 -0300700 radio->curfreq = FREQ_MIN * FREQ_MUL;
Alexey Klimov406827c2009-04-03 18:45:17 -0300701
Alexey Klimov3a0efc32008-12-27 21:32:49 -0300702 video_set_drvdata(&radio->videodev, radio);
Alexey Klimov406827c2009-04-03 18:45:17 -0300703
Alexey Klimov417b7952008-12-27 22:30:29 -0300704 retval = video_register_device(&radio->videodev, VFL_TYPE_RADIO, radio_nr);
705 if (retval < 0) {
Alexey Klimov406827c2009-04-03 18:45:17 -0300706 v4l2_err(v4l2_dev, "couldn't register video device\n");
707 v4l2_device_unregister(v4l2_dev);
Oliver Neukum863c86d2007-12-03 06:48:43 -0300708 kfree(radio->transfer_buffer);
Douglas Landgraf7002a4f2007-05-07 16:43:01 -0300709 kfree(radio);
710 return -EIO;
711 }
712 usb_set_intfdata(intf, radio);
713 return 0;
714}
715
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716static int __init dsbr100_init(void)
717{
718 int retval = usb_register(&usb_dsbr100_driver);
Greg Kroah-Hartmana482f322008-10-10 05:08:23 -0300719 printk(KERN_INFO KBUILD_MODNAME ": " DRIVER_VERSION ":"
720 DRIVER_DESC "\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 return retval;
722}
723
724static void __exit dsbr100_exit(void)
725{
726 usb_deregister(&usb_dsbr100_driver);
727}
728
729module_init (dsbr100_init);
730module_exit (dsbr100_exit);
731
732MODULE_AUTHOR( DRIVER_AUTHOR );
733MODULE_DESCRIPTION( DRIVER_DESC );
734MODULE_LICENSE("GPL");