blob: ed9cd7ad06042c630ec147e6a3b830a5a2e3904f [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 Klimovf1ca0ad2009-06-11 00:22:42 -030036 Version 0.46:
37 Removed usb_dsbr100_open/close calls and radio->users counter. Also,
38 radio->muted changed to radio->status and suspend/resume calls updated.
39
Alexey Klimov406827c2009-04-03 18:45:17 -030040 Version 0.45:
41 Converted to v4l2_device.
42
Alexey Klimov6076dbf2008-12-27 22:35:21 -030043 Version 0.44:
44 Add suspend/resume functions, fix unplug of device,
45 a lot of cleanups and fixes by Alexey Klimov <klimov.linux@gmail.com>
46
Oliver Neukum863c86d2007-12-03 06:48:43 -030047 Version 0.43:
48 Oliver Neukum: avoided DMA coherency issue
49
Douglas Landgraf7002a4f2007-05-07 16:43:01 -030050 Version 0.42:
51 Converted dsbr100 to use video_ioctl2
52 by Douglas Landgraf <dougsland@gmail.com>
53
Alan Cox5aff3082006-08-08 15:47:50 -030054 Version 0.41-ac1:
55 Alan Cox: Some cleanups and fixes
56
57 Version 0.41:
58 Converted to V4L2 API by Mauro Carvalho Chehab <mchehab@infradead.org>
59
Linus Torvalds1da177e2005-04-16 15:20:36 -070060 Version 0.40:
Alan Cox5aff3082006-08-08 15:47:50 -030061 Markus: Updates for 2.6.x kernels, code layout changes, name sanitizing
Linus Torvalds1da177e2005-04-16 15:20:36 -070062
63 Version 0.30:
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -030064 Markus: Updates for 2.5.x kernel and more ISO compliant source
Linus Torvalds1da177e2005-04-16 15:20:36 -070065
66 Version 0.25:
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -030067 PSL and Markus: Cleanup, radio now doesn't stop on device close
Linus Torvalds1da177e2005-04-16 15:20:36 -070068
69 Version 0.24:
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -030070 Markus: Hope I got these silly VIDEO_TUNER_LOW issues finally
Linus Torvalds1da177e2005-04-16 15:20:36 -070071 right. Some minor cleanup, improved standalone compilation
72
73 Version 0.23:
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -030074 Markus: Sign extension bug fixed by declaring transfer_buffer unsigned
Linus Torvalds1da177e2005-04-16 15:20:36 -070075
76 Version 0.22:
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -030077 Markus: Some (brown bag) cleanup in what VIDIOCSTUNER returns,
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 thanks to Mike Cox for pointing the problem out.
79
80 Version 0.21:
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -030081 Markus: Minor cleanup, warnings if something goes wrong, lame attempt
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 to adhere to Documentation/CodingStyle
83
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -030084 Version 0.2:
85 Brad Hards <bradh@dynamite.com.au>: Fixes to make it work as non-module
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 Markus: Copyright clarification
87
88 Version 0.01: Markus: initial release
89
90*/
91
Linus Torvalds1da177e2005-04-16 15:20:36 -070092#include <linux/kernel.h>
93#include <linux/module.h>
94#include <linux/init.h>
95#include <linux/slab.h>
96#include <linux/input.h>
Alan Cox5aff3082006-08-08 15:47:50 -030097#include <linux/videodev2.h>
Alexey Klimov406827c2009-04-03 18:45:17 -030098#include <media/v4l2-device.h>
Hans Verkuil35ea11f2008-07-20 08:12:02 -030099#include <media/v4l2-ioctl.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100#include <linux/usb.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101
102/*
103 * Version Information
104 */
Alan Cox5aff3082006-08-08 15:47:50 -0300105#include <linux/version.h> /* for KERNEL_VERSION MACRO */
106
Alexey Klimovf1ca0ad2009-06-11 00:22:42 -0300107#define DRIVER_VERSION "v0.46"
108#define RADIO_VERSION KERNEL_VERSION(0, 4, 6)
Alan Cox5aff3082006-08-08 15:47:50 -0300109
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
Alexey Klimovf1ca0ad2009-06-11 00:22:42 -0300128/* defines for radio->status */
129#define STARTED 0
130#define STOPPED 1
131
Alexey Klimov3a0efc32008-12-27 21:32:49 -0300132#define videodev_to_radio(d) container_of(d, struct dsbr100_device, videodev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133
134static int usb_dsbr100_probe(struct usb_interface *intf,
135 const struct usb_device_id *id);
136static void usb_dsbr100_disconnect(struct usb_interface *intf);
Alexey Klimov04e0ffb2008-11-08 00:40:46 -0300137static int usb_dsbr100_suspend(struct usb_interface *intf,
138 pm_message_t message);
139static int usb_dsbr100_resume(struct usb_interface *intf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140
141static int radio_nr = -1;
142module_param(radio_nr, int, 0);
143
144/* Data for one (physical) device */
Alan Cox5aff3082006-08-08 15:47:50 -0300145struct dsbr100_device {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 struct usb_device *usbdev;
Alexey Klimov3a0efc32008-12-27 21:32:49 -0300147 struct video_device videodev;
Alexey Klimov406827c2009-04-03 18:45:17 -0300148 struct v4l2_device v4l2_dev;
149
Oliver Neukum863c86d2007-12-03 06:48:43 -0300150 u8 *transfer_buffer;
Alexey Klimov3a0efc32008-12-27 21:32:49 -0300151 struct mutex lock; /* buffer locking */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 int curfreq;
153 int stereo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 int removed;
Alexey Klimovf1ca0ad2009-06-11 00:22:42 -0300155 int status;
Alan Cox5aff3082006-08-08 15:47:50 -0300156};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158static struct usb_device_id usb_dsbr100_device_table [] = {
159 { USB_DEVICE(DSB100_VENDOR, DSB100_PRODUCT) },
160 { } /* Terminating entry */
161};
162
163MODULE_DEVICE_TABLE (usb, usb_dsbr100_device_table);
164
165/* USB subsystem interface */
166static struct usb_driver usb_dsbr100_driver = {
Alexey Klimov04e0ffb2008-11-08 00:40:46 -0300167 .name = "dsbr100",
168 .probe = usb_dsbr100_probe,
169 .disconnect = usb_dsbr100_disconnect,
170 .id_table = usb_dsbr100_device_table,
171 .suspend = usb_dsbr100_suspend,
172 .resume = usb_dsbr100_resume,
173 .reset_resume = usb_dsbr100_resume,
174 .supports_autosuspend = 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175};
176
177/* Low-level device interface begins here */
178
179/* switch on radio */
Alan Cox5aff3082006-08-08 15:47:50 -0300180static int dsbr100_start(struct dsbr100_device *radio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181{
Alexey Klimov417b7952008-12-27 22:30:29 -0300182 int retval;
183 int request;
184
Alexey Klimov3a0efc32008-12-27 21:32:49 -0300185 mutex_lock(&radio->lock);
Alexey Klimov417b7952008-12-27 22:30:29 -0300186
187 retval = usb_control_msg(radio->usbdev,
188 usb_rcvctrlpipe(radio->usbdev, 0),
189 USB_REQ_GET_STATUS,
190 USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN,
191 0x00, 0xC7, radio->transfer_buffer, 8, 300);
192
193 if (retval < 0) {
194 request = USB_REQ_GET_STATUS;
195 goto usb_control_msg_failed;
Alexey Klimov3a0efc32008-12-27 21:32:49 -0300196 }
197
Alexey Klimov417b7952008-12-27 22:30:29 -0300198 retval = usb_control_msg(radio->usbdev,
199 usb_rcvctrlpipe(radio->usbdev, 0),
200 DSB100_ONOFF,
201 USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN,
202 0x01, 0x00, radio->transfer_buffer, 8, 300);
203
204 if (retval < 0) {
205 request = DSB100_ONOFF;
206 goto usb_control_msg_failed;
207 }
208
Alexey Klimovf1ca0ad2009-06-11 00:22:42 -0300209 radio->status = STARTED;
Alexey Klimov3a0efc32008-12-27 21:32:49 -0300210 mutex_unlock(&radio->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 return (radio->transfer_buffer)[0];
Alexey Klimov417b7952008-12-27 22:30:29 -0300212
213usb_control_msg_failed:
214 mutex_unlock(&radio->lock);
215 dev_err(&radio->usbdev->dev,
216 "%s - usb_control_msg returned %i, request %i\n",
217 __func__, retval, request);
Alexey Klimovd25cb642008-12-27 22:40:57 -0300218 return retval;
Alexey Klimov417b7952008-12-27 22:30:29 -0300219
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220}
221
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222/* switch off radio */
Alan Cox5aff3082006-08-08 15:47:50 -0300223static int dsbr100_stop(struct dsbr100_device *radio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224{
Alexey Klimov417b7952008-12-27 22:30:29 -0300225 int retval;
226 int request;
227
Alexey Klimov3a0efc32008-12-27 21:32:49 -0300228 mutex_lock(&radio->lock);
Alexey Klimov417b7952008-12-27 22:30:29 -0300229
230 retval = usb_control_msg(radio->usbdev,
231 usb_rcvctrlpipe(radio->usbdev, 0),
232 USB_REQ_GET_STATUS,
233 USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN,
234 0x16, 0x1C, radio->transfer_buffer, 8, 300);
235
236 if (retval < 0) {
237 request = USB_REQ_GET_STATUS;
238 goto usb_control_msg_failed;
Alexey Klimov3a0efc32008-12-27 21:32:49 -0300239 }
240
Alexey Klimov417b7952008-12-27 22:30:29 -0300241 retval = usb_control_msg(radio->usbdev,
242 usb_rcvctrlpipe(radio->usbdev, 0),
243 DSB100_ONOFF,
244 USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN,
245 0x00, 0x00, radio->transfer_buffer, 8, 300);
246
247 if (retval < 0) {
248 request = DSB100_ONOFF;
249 goto usb_control_msg_failed;
250 }
251
Alexey Klimovf1ca0ad2009-06-11 00:22:42 -0300252 radio->status = STOPPED;
Alexey Klimov3a0efc32008-12-27 21:32:49 -0300253 mutex_unlock(&radio->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 return (radio->transfer_buffer)[0];
Alexey Klimov417b7952008-12-27 22:30:29 -0300255
256usb_control_msg_failed:
257 mutex_unlock(&radio->lock);
258 dev_err(&radio->usbdev->dev,
259 "%s - usb_control_msg returned %i, request %i\n",
260 __func__, retval, request);
Alexey Klimovd25cb642008-12-27 22:40:57 -0300261 return retval;
Alexey Klimov417b7952008-12-27 22:30:29 -0300262
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263}
264
265/* set a frequency, freq is defined by v4l's TUNER_LOW, i.e. 1/16th kHz */
Alexey Klimov917fab42009-06-11 00:19:49 -0300266static int dsbr100_setfreq(struct dsbr100_device *radio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267{
Alexey Klimov417b7952008-12-27 22:30:29 -0300268 int retval;
269 int request;
Alexey Klimov917fab42009-06-11 00:19:49 -0300270 int freq = (radio->curfreq / 16 * 80) / 1000 + 856;
Alexey Klimov417b7952008-12-27 22:30:29 -0300271
Alexey Klimov3a0efc32008-12-27 21:32:49 -0300272 mutex_lock(&radio->lock);
Alexey Klimov417b7952008-12-27 22:30:29 -0300273
274 retval = usb_control_msg(radio->usbdev,
275 usb_rcvctrlpipe(radio->usbdev, 0),
276 DSB100_TUNE,
277 USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN,
278 (freq >> 8) & 0x00ff, freq & 0xff,
279 radio->transfer_buffer, 8, 300);
280
281 if (retval < 0) {
282 request = DSB100_TUNE;
283 goto usb_control_msg_failed;
284 }
285
286 retval = usb_control_msg(radio->usbdev,
287 usb_rcvctrlpipe(radio->usbdev, 0),
288 USB_REQ_GET_STATUS,
289 USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN,
290 0x96, 0xB7, radio->transfer_buffer, 8, 300);
291
292 if (retval < 0) {
293 request = USB_REQ_GET_STATUS;
294 goto usb_control_msg_failed;
295 }
296
297 retval = usb_control_msg(radio->usbdev,
298 usb_rcvctrlpipe(radio->usbdev, 0),
299 USB_REQ_GET_STATUS,
300 USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN,
301 0x00, 0x24, radio->transfer_buffer, 8, 300);
302
303 if (retval < 0) {
304 request = USB_REQ_GET_STATUS;
305 goto usb_control_msg_failed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 }
Alexey Klimov3a0efc32008-12-27 21:32:49 -0300307
Alexey Klimov223377e2008-10-19 23:50:27 -0300308 radio->stereo = !((radio->transfer_buffer)[0] & 0x01);
Alexey Klimov3a0efc32008-12-27 21:32:49 -0300309 mutex_unlock(&radio->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 return (radio->transfer_buffer)[0];
Alexey Klimov417b7952008-12-27 22:30:29 -0300311
312usb_control_msg_failed:
313 radio->stereo = -1;
314 mutex_unlock(&radio->lock);
315 dev_err(&radio->usbdev->dev,
316 "%s - usb_control_msg returned %i, request %i\n",
317 __func__, retval, request);
Alexey Klimovd25cb642008-12-27 22:40:57 -0300318 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319}
320
321/* return the device status. This is, in effect, just whether it
322sees a stereo signal or not. Pity. */
Alan Cox5aff3082006-08-08 15:47:50 -0300323static void dsbr100_getstat(struct dsbr100_device *radio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324{
Alexey Klimov417b7952008-12-27 22:30:29 -0300325 int retval;
326
Alexey Klimov3a0efc32008-12-27 21:32:49 -0300327 mutex_lock(&radio->lock);
Alexey Klimov417b7952008-12-27 22:30:29 -0300328
329 retval = usb_control_msg(radio->usbdev,
330 usb_rcvctrlpipe(radio->usbdev, 0),
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300331 USB_REQ_GET_STATUS,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN,
Alexey Klimov417b7952008-12-27 22:30:29 -0300333 0x00 , 0x24, radio->transfer_buffer, 8, 300);
334
335 if (retval < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 radio->stereo = -1;
Alexey Klimov417b7952008-12-27 22:30:29 -0300337 dev_err(&radio->usbdev->dev,
338 "%s - usb_control_msg returned %i, request %i\n",
339 __func__, retval, USB_REQ_GET_STATUS);
340 } else {
Alexey Klimov223377e2008-10-19 23:50:27 -0300341 radio->stereo = !(radio->transfer_buffer[0] & 0x01);
Alexey Klimov417b7952008-12-27 22:30:29 -0300342 }
343
Alexey Klimov3a0efc32008-12-27 21:32:49 -0300344 mutex_unlock(&radio->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345}
346
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347/* USB subsystem interface begins here */
348
Alexey Klimovfc55bcb2008-12-27 22:33:54 -0300349/*
350 * Handle unplugging of the device.
351 * We call video_unregister_device in any case.
352 * The last function called in this procedure is
353 * usb_dsbr100_video_device_release
354 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355static void usb_dsbr100_disconnect(struct usb_interface *intf)
356{
Alan Cox5aff3082006-08-08 15:47:50 -0300357 struct dsbr100_device *radio = usb_get_intfdata(intf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358
359 usb_set_intfdata (intf, NULL);
Alexey Klimov3a0efc32008-12-27 21:32:49 -0300360
361 mutex_lock(&radio->lock);
362 radio->removed = 1;
363 mutex_unlock(&radio->lock);
364
365 video_unregister_device(&radio->videodev);
Alexey Klimov406827c2009-04-03 18:45:17 -0300366 v4l2_device_disconnect(&radio->v4l2_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367}
368
369
Douglas Landgraf7002a4f2007-05-07 16:43:01 -0300370static int vidioc_querycap(struct file *file, void *priv,
371 struct v4l2_capability *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372{
Alexey Klimovc7181cf2009-01-25 20:05:58 -0300373 struct dsbr100_device *radio = video_drvdata(file);
374
Douglas Landgraf7002a4f2007-05-07 16:43:01 -0300375 strlcpy(v->driver, "dsbr100", sizeof(v->driver));
376 strlcpy(v->card, "D-Link R-100 USB FM Radio", sizeof(v->card));
Alexey Klimovc7181cf2009-01-25 20:05:58 -0300377 usb_make_path(radio->usbdev, v->bus_info, sizeof(v->bus_info));
Douglas Landgraf7002a4f2007-05-07 16:43:01 -0300378 v->version = RADIO_VERSION;
379 v->capabilities = V4L2_CAP_TUNER;
380 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381}
382
Douglas Landgraf7002a4f2007-05-07 16:43:01 -0300383static int vidioc_g_tuner(struct file *file, void *priv,
384 struct v4l2_tuner *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385{
Hans Verkuilc170ecf2008-08-23 08:32:09 -0300386 struct dsbr100_device *radio = video_drvdata(file);
Douglas Landgraf7002a4f2007-05-07 16:43:01 -0300387
Alexey Klimov3a0efc32008-12-27 21:32:49 -0300388 /* safety check */
389 if (radio->removed)
390 return -EIO;
391
Douglas Landgraf7002a4f2007-05-07 16:43:01 -0300392 if (v->index > 0)
393 return -EINVAL;
394
395 dsbr100_getstat(radio);
396 strcpy(v->name, "FM");
397 v->type = V4L2_TUNER_RADIO;
Alexey Klimov223377e2008-10-19 23:50:27 -0300398 v->rangelow = FREQ_MIN * FREQ_MUL;
399 v->rangehigh = FREQ_MAX * FREQ_MUL;
400 v->rxsubchans = V4L2_TUNER_SUB_MONO | V4L2_TUNER_SUB_STEREO;
Douglas Landgraf7002a4f2007-05-07 16:43:01 -0300401 v->capability = V4L2_TUNER_CAP_LOW;
402 if(radio->stereo)
403 v->audmode = V4L2_TUNER_MODE_STEREO;
404 else
405 v->audmode = V4L2_TUNER_MODE_MONO;
406 v->signal = 0xffff; /* We can't get the signal strength */
407 return 0;
408}
409
410static int vidioc_s_tuner(struct file *file, void *priv,
411 struct v4l2_tuner *v)
412{
Alexey Klimov3a0efc32008-12-27 21:32:49 -0300413 struct dsbr100_device *radio = video_drvdata(file);
414
415 /* safety check */
416 if (radio->removed)
417 return -EIO;
418
Douglas Landgraf7002a4f2007-05-07 16:43:01 -0300419 if (v->index > 0)
420 return -EINVAL;
421
422 return 0;
423}
424
425static int vidioc_s_frequency(struct file *file, void *priv,
426 struct v4l2_frequency *f)
427{
Hans Verkuilc170ecf2008-08-23 08:32:09 -0300428 struct dsbr100_device *radio = video_drvdata(file);
Alexey Klimov417b7952008-12-27 22:30:29 -0300429 int retval;
Douglas Landgraf7002a4f2007-05-07 16:43:01 -0300430
Alexey Klimov3a0efc32008-12-27 21:32:49 -0300431 /* safety check */
432 if (radio->removed)
433 return -EIO;
434
Alexey Klimovfdf9c992009-02-08 02:00:14 -0300435 mutex_lock(&radio->lock);
Douglas Landgraf7002a4f2007-05-07 16:43:01 -0300436 radio->curfreq = f->frequency;
Alexey Klimovfdf9c992009-02-08 02:00:14 -0300437 mutex_unlock(&radio->lock);
438
Alexey Klimov917fab42009-06-11 00:19:49 -0300439 retval = dsbr100_setfreq(radio);
Alexey Klimovd25cb642008-12-27 22:40:57 -0300440 if (retval < 0)
Greg Kroah-Hartmanaa826612008-08-14 09:37:34 -0700441 dev_warn(&radio->usbdev->dev, "Set frequency failed\n");
Douglas Landgraf7002a4f2007-05-07 16:43:01 -0300442 return 0;
443}
444
445static int vidioc_g_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);
Douglas Landgraf7002a4f2007-05-07 16:43:01 -0300449
Alexey Klimov3a0efc32008-12-27 21:32:49 -0300450 /* safety check */
451 if (radio->removed)
452 return -EIO;
453
Douglas Landgraf7002a4f2007-05-07 16:43:01 -0300454 f->type = V4L2_TUNER_RADIO;
455 f->frequency = radio->curfreq;
456 return 0;
457}
458
459static int vidioc_queryctrl(struct file *file, void *priv,
460 struct v4l2_queryctrl *qc)
461{
Alexey Klimov406827c2009-04-03 18:45:17 -0300462 switch (qc->id) {
463 case V4L2_CID_AUDIO_MUTE:
464 return v4l2_ctrl_query_fill(qc, 0, 1, 1, 1);
Douglas Landgraf7002a4f2007-05-07 16:43:01 -0300465 }
Alexey Klimov406827c2009-04-03 18:45:17 -0300466
Douglas Landgraf7002a4f2007-05-07 16:43:01 -0300467 return -EINVAL;
468}
469
470static int vidioc_g_ctrl(struct file *file, void *priv,
471 struct v4l2_control *ctrl)
472{
Hans Verkuilc170ecf2008-08-23 08:32:09 -0300473 struct dsbr100_device *radio = video_drvdata(file);
Douglas Landgraf7002a4f2007-05-07 16:43:01 -0300474
Alexey Klimov3a0efc32008-12-27 21:32:49 -0300475 /* safety check */
476 if (radio->removed)
477 return -EIO;
478
Douglas Landgraf7002a4f2007-05-07 16:43:01 -0300479 switch (ctrl->id) {
480 case V4L2_CID_AUDIO_MUTE:
Alexey Klimovf1ca0ad2009-06-11 00:22:42 -0300481 ctrl->value = radio->status;
Douglas Landgraf7002a4f2007-05-07 16:43:01 -0300482 return 0;
483 }
484 return -EINVAL;
485}
486
487static int vidioc_s_ctrl(struct file *file, void *priv,
488 struct v4l2_control *ctrl)
489{
Hans Verkuilc170ecf2008-08-23 08:32:09 -0300490 struct dsbr100_device *radio = video_drvdata(file);
Alexey Klimov417b7952008-12-27 22:30:29 -0300491 int retval;
Douglas Landgraf7002a4f2007-05-07 16:43:01 -0300492
Alexey Klimov3a0efc32008-12-27 21:32:49 -0300493 /* safety check */
494 if (radio->removed)
495 return -EIO;
496
Douglas Landgraf7002a4f2007-05-07 16:43:01 -0300497 switch (ctrl->id) {
498 case V4L2_CID_AUDIO_MUTE:
499 if (ctrl->value) {
Alexey Klimov417b7952008-12-27 22:30:29 -0300500 retval = dsbr100_stop(radio);
Alexey Klimovd25cb642008-12-27 22:40:57 -0300501 if (retval < 0) {
Greg Kroah-Hartmanaa826612008-08-14 09:37:34 -0700502 dev_warn(&radio->usbdev->dev,
503 "Radio did not respond properly\n");
Alexey Klimov90b698d2008-10-09 13:42:32 -0300504 return -EBUSY;
505 }
Douglas Landgraf7002a4f2007-05-07 16:43:01 -0300506 } else {
Alexey Klimov417b7952008-12-27 22:30:29 -0300507 retval = dsbr100_start(radio);
Alexey Klimovd25cb642008-12-27 22:40:57 -0300508 if (retval < 0) {
Greg Kroah-Hartmanaa826612008-08-14 09:37:34 -0700509 dev_warn(&radio->usbdev->dev,
510 "Radio did not respond properly\n");
Alexey Klimov90b698d2008-10-09 13:42:32 -0300511 return -EBUSY;
512 }
Douglas Landgraf7002a4f2007-05-07 16:43:01 -0300513 }
514 return 0;
515 }
516 return -EINVAL;
517}
518
519static int vidioc_g_audio(struct file *file, void *priv,
520 struct v4l2_audio *a)
521{
522 if (a->index > 1)
523 return -EINVAL;
524
525 strcpy(a->name, "Radio");
526 a->capability = V4L2_AUDCAP_STEREO;
527 return 0;
528}
529
530static int vidioc_g_input(struct file *filp, void *priv, unsigned int *i)
531{
532 *i = 0;
533 return 0;
534}
535
536static int vidioc_s_input(struct file *filp, void *priv, unsigned int i)
537{
538 if (i != 0)
539 return -EINVAL;
540 return 0;
541}
542
543static int vidioc_s_audio(struct file *file, void *priv,
544 struct v4l2_audio *a)
545{
546 if (a->index != 0)
547 return -EINVAL;
548 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549}
550
Alexey Klimov04e0ffb2008-11-08 00:40:46 -0300551/* Suspend device - stop device. */
552static int usb_dsbr100_suspend(struct usb_interface *intf, pm_message_t message)
553{
554 struct dsbr100_device *radio = usb_get_intfdata(intf);
555 int retval;
556
Alexey Klimovf1ca0ad2009-06-11 00:22:42 -0300557 if (radio->status == STARTED) {
558 retval = dsbr100_stop(radio);
559 if (retval < 0)
560 dev_warn(&intf->dev, "dsbr100_stop failed\n");
561
562 /* After dsbr100_stop() status set to STOPPED.
563 * If we want driver to start radio on resume
564 * we set status equal to STARTED.
565 * On resume we will check status and run radio if needed.
566 */
567
568 mutex_lock(&radio->lock);
569 radio->status = STARTED;
570 mutex_unlock(&radio->lock);
571 }
Alexey Klimov04e0ffb2008-11-08 00:40:46 -0300572
573 dev_info(&intf->dev, "going into suspend..\n");
574
575 return 0;
576}
577
578/* Resume device - start device. */
579static int usb_dsbr100_resume(struct usb_interface *intf)
580{
581 struct dsbr100_device *radio = usb_get_intfdata(intf);
582 int retval;
583
Alexey Klimovf1ca0ad2009-06-11 00:22:42 -0300584 if (radio->status == STARTED) {
585 retval = dsbr100_start(radio);
586 if (retval < 0)
587 dev_warn(&intf->dev, "dsbr100_start failed\n");
588 }
Alexey Klimov04e0ffb2008-11-08 00:40:46 -0300589
590 dev_info(&intf->dev, "coming out of suspend..\n");
591
592 return 0;
593}
594
Alexey Klimovfc55bcb2008-12-27 22:33:54 -0300595/* free data structures */
Alexey Klimov3a0efc32008-12-27 21:32:49 -0300596static void usb_dsbr100_video_device_release(struct video_device *videodev)
597{
598 struct dsbr100_device *radio = videodev_to_radio(videodev);
599
Alexey Klimov406827c2009-04-03 18:45:17 -0300600 v4l2_device_unregister(&radio->v4l2_dev);
Alexey Klimov3a0efc32008-12-27 21:32:49 -0300601 kfree(radio->transfer_buffer);
602 kfree(radio);
603}
604
Douglas Landgraf7002a4f2007-05-07 16:43:01 -0300605/* File system interface */
Hans Verkuilbec43662008-12-30 06:58:20 -0300606static const struct v4l2_file_operations usb_dsbr100_fops = {
Douglas Landgraf7002a4f2007-05-07 16:43:01 -0300607 .owner = THIS_MODULE,
Douglas Landgraf7002a4f2007-05-07 16:43:01 -0300608 .ioctl = video_ioctl2,
Douglas Landgraf7002a4f2007-05-07 16:43:01 -0300609};
610
Hans Verkuila3998102008-07-21 02:57:38 -0300611static const struct v4l2_ioctl_ops usb_dsbr100_ioctl_ops = {
Douglas Landgraf7002a4f2007-05-07 16:43:01 -0300612 .vidioc_querycap = vidioc_querycap,
613 .vidioc_g_tuner = vidioc_g_tuner,
614 .vidioc_s_tuner = vidioc_s_tuner,
615 .vidioc_g_frequency = vidioc_g_frequency,
616 .vidioc_s_frequency = vidioc_s_frequency,
617 .vidioc_queryctrl = vidioc_queryctrl,
618 .vidioc_g_ctrl = vidioc_g_ctrl,
619 .vidioc_s_ctrl = vidioc_s_ctrl,
620 .vidioc_g_audio = vidioc_g_audio,
621 .vidioc_s_audio = vidioc_s_audio,
622 .vidioc_g_input = vidioc_g_input,
623 .vidioc_s_input = vidioc_s_input,
624};
625
Alexey Klimovfc55bcb2008-12-27 22:33:54 -0300626/* check if the device is present and register with v4l and usb if it is */
Douglas Landgraf7002a4f2007-05-07 16:43:01 -0300627static int usb_dsbr100_probe(struct usb_interface *intf,
628 const struct usb_device_id *id)
629{
630 struct dsbr100_device *radio;
Alexey Klimov406827c2009-04-03 18:45:17 -0300631 struct v4l2_device *v4l2_dev;
Alexey Klimov417b7952008-12-27 22:30:29 -0300632 int retval;
Douglas Landgraf7002a4f2007-05-07 16:43:01 -0300633
Alexey Klimov406827c2009-04-03 18:45:17 -0300634 radio = kzalloc(sizeof(struct dsbr100_device), GFP_KERNEL);
Alexey Klimov223377e2008-10-19 23:50:27 -0300635
636 if (!radio)
Douglas Landgraf7002a4f2007-05-07 16:43:01 -0300637 return -ENOMEM;
Alexey Klimov223377e2008-10-19 23:50:27 -0300638
639 radio->transfer_buffer = kmalloc(TB_LEN, GFP_KERNEL);
640
641 if (!(radio->transfer_buffer)) {
Oliver Neukum863c86d2007-12-03 06:48:43 -0300642 kfree(radio);
643 return -ENOMEM;
644 }
Alexey Klimov223377e2008-10-19 23:50:27 -0300645
Alexey Klimov406827c2009-04-03 18:45:17 -0300646 v4l2_dev = &radio->v4l2_dev;
647
648 retval = v4l2_device_register(&intf->dev, v4l2_dev);
649 if (retval < 0) {
650 v4l2_err(v4l2_dev, "couldn't register v4l2_device\n");
651 kfree(radio->transfer_buffer);
652 kfree(radio);
653 return retval;
654 }
655
656 strlcpy(radio->videodev.name, v4l2_dev->name, sizeof(radio->videodev.name));
657 radio->videodev.v4l2_dev = v4l2_dev;
658 radio->videodev.fops = &usb_dsbr100_fops;
659 radio->videodev.ioctl_ops = &usb_dsbr100_ioctl_ops;
660 radio->videodev.release = usb_dsbr100_video_device_release;
661
Alexey Klimov3a0efc32008-12-27 21:32:49 -0300662 mutex_init(&radio->lock);
Alexey Klimov3a0efc32008-12-27 21:32:49 -0300663
Douglas Landgraf7002a4f2007-05-07 16:43:01 -0300664 radio->removed = 0;
Douglas Landgraf7002a4f2007-05-07 16:43:01 -0300665 radio->usbdev = interface_to_usbdev(intf);
Alexey Klimov223377e2008-10-19 23:50:27 -0300666 radio->curfreq = FREQ_MIN * FREQ_MUL;
Alexey Klimovf1ca0ad2009-06-11 00:22:42 -0300667 radio->status = STOPPED;
Alexey Klimov406827c2009-04-03 18:45:17 -0300668
Alexey Klimov3a0efc32008-12-27 21:32:49 -0300669 video_set_drvdata(&radio->videodev, radio);
Alexey Klimov406827c2009-04-03 18:45:17 -0300670
Alexey Klimov417b7952008-12-27 22:30:29 -0300671 retval = video_register_device(&radio->videodev, VFL_TYPE_RADIO, radio_nr);
672 if (retval < 0) {
Alexey Klimov406827c2009-04-03 18:45:17 -0300673 v4l2_err(v4l2_dev, "couldn't register video device\n");
674 v4l2_device_unregister(v4l2_dev);
Oliver Neukum863c86d2007-12-03 06:48:43 -0300675 kfree(radio->transfer_buffer);
Douglas Landgraf7002a4f2007-05-07 16:43:01 -0300676 kfree(radio);
677 return -EIO;
678 }
679 usb_set_intfdata(intf, radio);
680 return 0;
681}
682
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683static int __init dsbr100_init(void)
684{
685 int retval = usb_register(&usb_dsbr100_driver);
Greg Kroah-Hartmana482f322008-10-10 05:08:23 -0300686 printk(KERN_INFO KBUILD_MODNAME ": " DRIVER_VERSION ":"
687 DRIVER_DESC "\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 return retval;
689}
690
691static void __exit dsbr100_exit(void)
692{
693 usb_deregister(&usb_dsbr100_driver);
694}
695
696module_init (dsbr100_init);
697module_exit (dsbr100_exit);
698
699MODULE_AUTHOR( DRIVER_AUTHOR );
700MODULE_DESCRIPTION( DRIVER_DESC );
701MODULE_LICENSE("GPL");