blob: 84914fb267bebbcc88d66d9834b670f6eaebdd2a [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* A driver for the D-Link DSB-R100 USB radio. The R100 plugs
2 into both the USB and an analog audio input, so this thing
3 only deals with initialisation and frequency setting, the
4 audio data has to be handled by a sound driver.
5
6 Major issue: I can't find out where the device reports the signal
7 strength, and indeed the windows software appearantly just looks
8 at the stereo indicator as well. So, scanning will only find
9 stereo stations. Sad, but I can't help it.
10
11 Also, the windows program sends oodles of messages over to the
12 device, and I couldn't figure out their meaning. My suspicion
13 is that they don't have any:-)
14
15 You might find some interesting stuff about this module at
16 http://unimut.fsk.uni-heidelberg.de/unimut/demi/dsbr
17
18 Copyright (c) 2000 Markus Demleitner <msdemlei@cl.uni-heidelberg.de>
19
20 This program is free software; you can redistribute it and/or modify
21 it under the terms of the GNU General Public License as published by
22 the Free Software Foundation; either version 2 of the License, or
23 (at your option) any later version.
24
25 This program is distributed in the hope that it will be useful,
26 but WITHOUT ANY WARRANTY; without even the implied warranty of
27 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
28 GNU General Public License for more details.
29
30 You should have received a copy of the GNU General Public License
31 along with this program; if not, write to the Free Software
32 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
33
34 History:
35
Oliver Neukum863c86d2007-12-03 06:48:43 -030036 Version 0.43:
37 Oliver Neukum: avoided DMA coherency issue
38
Douglas Landgraf7002a4f2007-05-07 16:43:01 -030039 Version 0.42:
40 Converted dsbr100 to use video_ioctl2
41 by Douglas Landgraf <dougsland@gmail.com>
42
Alan Cox5aff3082006-08-08 15:47:50 -030043 Version 0.41-ac1:
44 Alan Cox: Some cleanups and fixes
45
46 Version 0.41:
47 Converted to V4L2 API by Mauro Carvalho Chehab <mchehab@infradead.org>
48
Linus Torvalds1da177e2005-04-16 15:20:36 -070049 Version 0.40:
Alan Cox5aff3082006-08-08 15:47:50 -030050 Markus: Updates for 2.6.x kernels, code layout changes, name sanitizing
Linus Torvalds1da177e2005-04-16 15:20:36 -070051
52 Version 0.30:
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -030053 Markus: Updates for 2.5.x kernel and more ISO compliant source
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
55 Version 0.25:
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -030056 PSL and Markus: Cleanup, radio now doesn't stop on device close
Linus Torvalds1da177e2005-04-16 15:20:36 -070057
58 Version 0.24:
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -030059 Markus: Hope I got these silly VIDEO_TUNER_LOW issues finally
Linus Torvalds1da177e2005-04-16 15:20:36 -070060 right. Some minor cleanup, improved standalone compilation
61
62 Version 0.23:
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -030063 Markus: Sign extension bug fixed by declaring transfer_buffer unsigned
Linus Torvalds1da177e2005-04-16 15:20:36 -070064
65 Version 0.22:
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -030066 Markus: Some (brown bag) cleanup in what VIDIOCSTUNER returns,
Linus Torvalds1da177e2005-04-16 15:20:36 -070067 thanks to Mike Cox for pointing the problem out.
68
69 Version 0.21:
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -030070 Markus: Minor cleanup, warnings if something goes wrong, lame attempt
Linus Torvalds1da177e2005-04-16 15:20:36 -070071 to adhere to Documentation/CodingStyle
72
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -030073 Version 0.2:
74 Brad Hards <bradh@dynamite.com.au>: Fixes to make it work as non-module
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 Markus: Copyright clarification
76
77 Version 0.01: Markus: initial release
78
79*/
80
Linus Torvalds1da177e2005-04-16 15:20:36 -070081#include <linux/kernel.h>
82#include <linux/module.h>
83#include <linux/init.h>
84#include <linux/slab.h>
85#include <linux/input.h>
Alan Cox5aff3082006-08-08 15:47:50 -030086#include <linux/videodev2.h>
Mauro Carvalho Chehab5e87efa2006-06-05 10:26:32 -030087#include <media/v4l2-common.h>
Hans Verkuil35ea11f2008-07-20 08:12:02 -030088#include <media/v4l2-ioctl.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070089#include <linux/usb.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070090
91/*
92 * Version Information
93 */
Alan Cox5aff3082006-08-08 15:47:50 -030094#include <linux/version.h> /* for KERNEL_VERSION MACRO */
95
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
175
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176static struct usb_device_id usb_dsbr100_device_table [] = {
177 { USB_DEVICE(DSB100_VENDOR, DSB100_PRODUCT) },
178 { } /* Terminating entry */
179};
180
181MODULE_DEVICE_TABLE (usb, usb_dsbr100_device_table);
182
183/* USB subsystem interface */
184static struct usb_driver usb_dsbr100_driver = {
Alexey Klimov04e0ffb2008-11-08 00:40:46 -0300185 .name = "dsbr100",
186 .probe = usb_dsbr100_probe,
187 .disconnect = usb_dsbr100_disconnect,
188 .id_table = usb_dsbr100_device_table,
189 .suspend = usb_dsbr100_suspend,
190 .resume = usb_dsbr100_resume,
191 .reset_resume = usb_dsbr100_resume,
192 .supports_autosuspend = 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193};
194
195/* Low-level device interface begins here */
196
197/* switch on radio */
Alan Cox5aff3082006-08-08 15:47:50 -0300198static int dsbr100_start(struct dsbr100_device *radio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199{
Alexey Klimov3a0efc32008-12-27 21:32:49 -0300200 mutex_lock(&radio->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 if (usb_control_msg(radio->usbdev, usb_rcvctrlpipe(radio->usbdev, 0),
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300202 USB_REQ_GET_STATUS,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN,
Alexey Klimov223377e2008-10-19 23:50:27 -0300204 0x00, 0xC7, radio->transfer_buffer, 8, 300) < 0 ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 usb_control_msg(radio->usbdev, usb_rcvctrlpipe(radio->usbdev, 0),
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300206 DSB100_ONOFF,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN,
Alexey Klimov3a0efc32008-12-27 21:32:49 -0300208 0x01, 0x00, radio->transfer_buffer, 8, 300) < 0) {
209 mutex_unlock(&radio->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 return -1;
Alexey Klimov3a0efc32008-12-27 21:32:49 -0300211 }
212
Alan Cox5aff3082006-08-08 15:47:50 -0300213 radio->muted=0;
Alexey Klimov3a0efc32008-12-27 21:32:49 -0300214 mutex_unlock(&radio->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 return (radio->transfer_buffer)[0];
216}
217
218
219/* switch off radio */
Alan Cox5aff3082006-08-08 15:47:50 -0300220static int dsbr100_stop(struct dsbr100_device *radio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221{
Alexey Klimov3a0efc32008-12-27 21:32:49 -0300222 mutex_lock(&radio->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 if (usb_control_msg(radio->usbdev, usb_rcvctrlpipe(radio->usbdev, 0),
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300224 USB_REQ_GET_STATUS,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN,
Alexey Klimov223377e2008-10-19 23:50:27 -0300226 0x16, 0x1C, radio->transfer_buffer, 8, 300) < 0 ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 usb_control_msg(radio->usbdev, usb_rcvctrlpipe(radio->usbdev, 0),
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300228 DSB100_ONOFF,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN,
Alexey Klimov3a0efc32008-12-27 21:32:49 -0300230 0x00, 0x00, radio->transfer_buffer, 8, 300) < 0) {
231 mutex_unlock(&radio->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 return -1;
Alexey Klimov3a0efc32008-12-27 21:32:49 -0300233 }
234
Alan Cox5aff3082006-08-08 15:47:50 -0300235 radio->muted=1;
Alexey Klimov3a0efc32008-12-27 21:32:49 -0300236 mutex_unlock(&radio->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 return (radio->transfer_buffer)[0];
238}
239
240/* set a frequency, freq is defined by v4l's TUNER_LOW, i.e. 1/16th kHz */
Alan Cox5aff3082006-08-08 15:47:50 -0300241static int dsbr100_setfreq(struct dsbr100_device *radio, int freq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242{
Alexey Klimov223377e2008-10-19 23:50:27 -0300243 freq = (freq / 16 * 80) / 1000 + 856;
Alexey Klimov3a0efc32008-12-27 21:32:49 -0300244 mutex_lock(&radio->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 if (usb_control_msg(radio->usbdev, usb_rcvctrlpipe(radio->usbdev, 0),
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300246 DSB100_TUNE,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN,
Alexey Klimov223377e2008-10-19 23:50:27 -0300248 (freq >> 8) & 0x00ff, freq & 0xff,
249 radio->transfer_buffer, 8, 300) < 0 ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 usb_control_msg(radio->usbdev, usb_rcvctrlpipe(radio->usbdev, 0),
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300251 USB_REQ_GET_STATUS,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN,
Alexey Klimov223377e2008-10-19 23:50:27 -0300253 0x96, 0xB7, radio->transfer_buffer, 8, 300) < 0 ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 usb_control_msg(radio->usbdev, usb_rcvctrlpipe(radio->usbdev, 0),
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300255 USB_REQ_GET_STATUS,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN,
Alexey Klimov223377e2008-10-19 23:50:27 -0300257 0x00, 0x24, radio->transfer_buffer, 8, 300) < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 radio->stereo = -1;
Alexey Klimov3a0efc32008-12-27 21:32:49 -0300259 mutex_unlock(&radio->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 return -1;
261 }
Alexey Klimov3a0efc32008-12-27 21:32:49 -0300262
Alexey Klimov223377e2008-10-19 23:50:27 -0300263 radio->stereo = !((radio->transfer_buffer)[0] & 0x01);
Alexey Klimov3a0efc32008-12-27 21:32:49 -0300264 mutex_unlock(&radio->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 return (radio->transfer_buffer)[0];
266}
267
268/* return the device status. This is, in effect, just whether it
269sees a stereo signal or not. Pity. */
Alan Cox5aff3082006-08-08 15:47:50 -0300270static void dsbr100_getstat(struct dsbr100_device *radio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271{
Alexey Klimov3a0efc32008-12-27 21:32:49 -0300272 mutex_lock(&radio->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 if (usb_control_msg(radio->usbdev, usb_rcvctrlpipe(radio->usbdev, 0),
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300274 USB_REQ_GET_STATUS,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN,
Alexey Klimov223377e2008-10-19 23:50:27 -0300276 0x00 , 0x24, radio->transfer_buffer, 8, 300) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 radio->stereo = -1;
278 else
Alexey Klimov223377e2008-10-19 23:50:27 -0300279 radio->stereo = !(radio->transfer_buffer[0] & 0x01);
Alexey Klimov3a0efc32008-12-27 21:32:49 -0300280 mutex_unlock(&radio->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281}
282
283
284/* USB subsystem interface begins here */
285
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286/* handle unplugging of the device, release data structures
287if nothing keeps us from doing it. If something is still
288keeping us busy, the release callback of v4l will take care
Oliver Neukum863c86d2007-12-03 06:48:43 -0300289of releasing it. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290static void usb_dsbr100_disconnect(struct usb_interface *intf)
291{
Alan Cox5aff3082006-08-08 15:47:50 -0300292 struct dsbr100_device *radio = usb_get_intfdata(intf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293
294 usb_set_intfdata (intf, NULL);
Alexey Klimov3a0efc32008-12-27 21:32:49 -0300295
296 mutex_lock(&radio->lock);
297 radio->removed = 1;
298 mutex_unlock(&radio->lock);
299
300 video_unregister_device(&radio->videodev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301}
302
303
Douglas Landgraf7002a4f2007-05-07 16:43:01 -0300304static int vidioc_querycap(struct file *file, void *priv,
305 struct v4l2_capability *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306{
Douglas Landgraf7002a4f2007-05-07 16:43:01 -0300307 strlcpy(v->driver, "dsbr100", sizeof(v->driver));
308 strlcpy(v->card, "D-Link R-100 USB FM Radio", sizeof(v->card));
Alexey Klimov448441c2008-10-19 23:20:47 -0300309 sprintf(v->bus_info, "USB");
Douglas Landgraf7002a4f2007-05-07 16:43:01 -0300310 v->version = RADIO_VERSION;
311 v->capabilities = V4L2_CAP_TUNER;
312 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313}
314
Douglas Landgraf7002a4f2007-05-07 16:43:01 -0300315static int vidioc_g_tuner(struct file *file, void *priv,
316 struct v4l2_tuner *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317{
Hans Verkuilc170ecf2008-08-23 08:32:09 -0300318 struct dsbr100_device *radio = video_drvdata(file);
Douglas Landgraf7002a4f2007-05-07 16:43:01 -0300319
Alexey Klimov3a0efc32008-12-27 21:32:49 -0300320 /* safety check */
321 if (radio->removed)
322 return -EIO;
323
Douglas Landgraf7002a4f2007-05-07 16:43:01 -0300324 if (v->index > 0)
325 return -EINVAL;
326
327 dsbr100_getstat(radio);
328 strcpy(v->name, "FM");
329 v->type = V4L2_TUNER_RADIO;
Alexey Klimov223377e2008-10-19 23:50:27 -0300330 v->rangelow = FREQ_MIN * FREQ_MUL;
331 v->rangehigh = FREQ_MAX * FREQ_MUL;
332 v->rxsubchans = V4L2_TUNER_SUB_MONO | V4L2_TUNER_SUB_STEREO;
Douglas Landgraf7002a4f2007-05-07 16:43:01 -0300333 v->capability = V4L2_TUNER_CAP_LOW;
334 if(radio->stereo)
335 v->audmode = V4L2_TUNER_MODE_STEREO;
336 else
337 v->audmode = V4L2_TUNER_MODE_MONO;
338 v->signal = 0xffff; /* We can't get the signal strength */
339 return 0;
340}
341
342static int vidioc_s_tuner(struct file *file, void *priv,
343 struct v4l2_tuner *v)
344{
Alexey Klimov3a0efc32008-12-27 21:32:49 -0300345 struct dsbr100_device *radio = video_drvdata(file);
346
347 /* safety check */
348 if (radio->removed)
349 return -EIO;
350
Douglas Landgraf7002a4f2007-05-07 16:43:01 -0300351 if (v->index > 0)
352 return -EINVAL;
353
354 return 0;
355}
356
357static int vidioc_s_frequency(struct file *file, void *priv,
358 struct v4l2_frequency *f)
359{
Hans Verkuilc170ecf2008-08-23 08:32:09 -0300360 struct dsbr100_device *radio = video_drvdata(file);
Douglas Landgraf7002a4f2007-05-07 16:43:01 -0300361
Alexey Klimov3a0efc32008-12-27 21:32:49 -0300362 /* safety check */
363 if (radio->removed)
364 return -EIO;
365
Douglas Landgraf7002a4f2007-05-07 16:43:01 -0300366 radio->curfreq = f->frequency;
Alexey Klimov223377e2008-10-19 23:50:27 -0300367 if (dsbr100_setfreq(radio, radio->curfreq) == -1)
Greg Kroah-Hartmanaa826612008-08-14 09:37:34 -0700368 dev_warn(&radio->usbdev->dev, "Set frequency failed\n");
Douglas Landgraf7002a4f2007-05-07 16:43:01 -0300369 return 0;
370}
371
372static int vidioc_g_frequency(struct file *file, void *priv,
373 struct v4l2_frequency *f)
374{
Hans Verkuilc170ecf2008-08-23 08:32:09 -0300375 struct dsbr100_device *radio = video_drvdata(file);
Douglas Landgraf7002a4f2007-05-07 16:43:01 -0300376
Alexey Klimov3a0efc32008-12-27 21:32:49 -0300377 /* safety check */
378 if (radio->removed)
379 return -EIO;
380
Douglas Landgraf7002a4f2007-05-07 16:43:01 -0300381 f->type = V4L2_TUNER_RADIO;
382 f->frequency = radio->curfreq;
383 return 0;
384}
385
386static int vidioc_queryctrl(struct file *file, void *priv,
387 struct v4l2_queryctrl *qc)
388{
389 int i;
390
391 for (i = 0; i < ARRAY_SIZE(radio_qctrl); i++) {
392 if (qc->id && qc->id == radio_qctrl[i].id) {
Alexey Klimov223377e2008-10-19 23:50:27 -0300393 memcpy(qc, &(radio_qctrl[i]), sizeof(*qc));
Douglas Landgraf7002a4f2007-05-07 16:43:01 -0300394 return 0;
395 }
396 }
397 return -EINVAL;
398}
399
400static int vidioc_g_ctrl(struct file *file, void *priv,
401 struct v4l2_control *ctrl)
402{
Hans Verkuilc170ecf2008-08-23 08:32:09 -0300403 struct dsbr100_device *radio = video_drvdata(file);
Douglas Landgraf7002a4f2007-05-07 16:43:01 -0300404
Alexey Klimov3a0efc32008-12-27 21:32:49 -0300405 /* safety check */
406 if (radio->removed)
407 return -EIO;
408
Douglas Landgraf7002a4f2007-05-07 16:43:01 -0300409 switch (ctrl->id) {
410 case V4L2_CID_AUDIO_MUTE:
411 ctrl->value = radio->muted;
412 return 0;
413 }
414 return -EINVAL;
415}
416
417static int vidioc_s_ctrl(struct file *file, void *priv,
418 struct v4l2_control *ctrl)
419{
Hans Verkuilc170ecf2008-08-23 08:32:09 -0300420 struct dsbr100_device *radio = video_drvdata(file);
Douglas Landgraf7002a4f2007-05-07 16:43:01 -0300421
Alexey Klimov3a0efc32008-12-27 21:32:49 -0300422 /* safety check */
423 if (radio->removed)
424 return -EIO;
425
Douglas Landgraf7002a4f2007-05-07 16:43:01 -0300426 switch (ctrl->id) {
427 case V4L2_CID_AUDIO_MUTE:
428 if (ctrl->value) {
Alexey Klimov90b698d2008-10-09 13:42:32 -0300429 if (dsbr100_stop(radio) == -1) {
Greg Kroah-Hartmanaa826612008-08-14 09:37:34 -0700430 dev_warn(&radio->usbdev->dev,
431 "Radio did not respond properly\n");
Alexey Klimov90b698d2008-10-09 13:42:32 -0300432 return -EBUSY;
433 }
Douglas Landgraf7002a4f2007-05-07 16:43:01 -0300434 } else {
Alexey Klimov90b698d2008-10-09 13:42:32 -0300435 if (dsbr100_start(radio) == -1) {
Greg Kroah-Hartmanaa826612008-08-14 09:37:34 -0700436 dev_warn(&radio->usbdev->dev,
437 "Radio did not respond properly\n");
Alexey Klimov90b698d2008-10-09 13:42:32 -0300438 return -EBUSY;
439 }
Douglas Landgraf7002a4f2007-05-07 16:43:01 -0300440 }
441 return 0;
442 }
443 return -EINVAL;
444}
445
446static int vidioc_g_audio(struct file *file, void *priv,
447 struct v4l2_audio *a)
448{
449 if (a->index > 1)
450 return -EINVAL;
451
452 strcpy(a->name, "Radio");
453 a->capability = V4L2_AUDCAP_STEREO;
454 return 0;
455}
456
457static int vidioc_g_input(struct file *filp, void *priv, unsigned int *i)
458{
459 *i = 0;
460 return 0;
461}
462
463static int vidioc_s_input(struct file *filp, void *priv, unsigned int i)
464{
465 if (i != 0)
466 return -EINVAL;
467 return 0;
468}
469
470static int vidioc_s_audio(struct file *file, void *priv,
471 struct v4l2_audio *a)
472{
473 if (a->index != 0)
474 return -EINVAL;
475 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476}
477
478static int usb_dsbr100_open(struct inode *inode, struct file *file)
479{
Hans Verkuilc170ecf2008-08-23 08:32:09 -0300480 struct dsbr100_device *radio = video_drvdata(file);
Alexey Klimovb9f35732008-10-20 00:00:03 -0300481 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482
Hans Verkuild56dc612008-07-30 08:43:36 -0300483 lock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 radio->users = 1;
Alan Cox5aff3082006-08-08 15:47:50 -0300485 radio->muted = 1;
486
Alexey Klimov223377e2008-10-19 23:50:27 -0300487 if (dsbr100_start(radio) < 0) {
Greg Kroah-Hartmanaa826612008-08-14 09:37:34 -0700488 dev_warn(&radio->usbdev->dev,
489 "Radio did not start up properly\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 radio->users = 0;
Hans Verkuild56dc612008-07-30 08:43:36 -0300491 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 return -EIO;
493 }
Alexey Klimovb9f35732008-10-20 00:00:03 -0300494
495 retval = dsbr100_setfreq(radio, radio->curfreq);
496
497 if (retval == -1)
498 printk(KERN_WARNING KBUILD_MODNAME ": Set frequency failed\n");
499
Hans Verkuild56dc612008-07-30 08:43:36 -0300500 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 return 0;
502}
503
504static int usb_dsbr100_close(struct inode *inode, struct file *file)
505{
Hans Verkuilc170ecf2008-08-23 08:32:09 -0300506 struct dsbr100_device *radio = video_drvdata(file);
Alexey Klimov3a0efc32008-12-27 21:32:49 -0300507 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508
509 if (!radio)
510 return -ENODEV;
Alexey Klimov3a0efc32008-12-27 21:32:49 -0300511
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 radio->users = 0;
Alexey Klimov3a0efc32008-12-27 21:32:49 -0300513 if (!radio->removed) {
514 retval = dsbr100_stop(radio);
515 if (retval == -1) {
516 dev_warn(&radio->usbdev->dev,
517 "dsbr100_stop failed\n");
518 }
519
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 }
521 return 0;
522}
523
Alexey Klimov04e0ffb2008-11-08 00:40:46 -0300524/* Suspend device - stop device. */
525static int usb_dsbr100_suspend(struct usb_interface *intf, pm_message_t message)
526{
527 struct dsbr100_device *radio = usb_get_intfdata(intf);
528 int retval;
529
530 retval = dsbr100_stop(radio);
531 if (retval == -1)
532 dev_warn(&intf->dev, "dsbr100_stop failed\n");
533
534 dev_info(&intf->dev, "going into suspend..\n");
535
536 return 0;
537}
538
539/* Resume device - start device. */
540static int usb_dsbr100_resume(struct usb_interface *intf)
541{
542 struct dsbr100_device *radio = usb_get_intfdata(intf);
543 int retval;
544
545 retval = dsbr100_start(radio);
546 if (retval == -1)
547 dev_warn(&intf->dev, "dsbr100_start failed\n");
548
549 dev_info(&intf->dev, "coming out of suspend..\n");
550
551 return 0;
552}
553
Alexey Klimov3a0efc32008-12-27 21:32:49 -0300554static void usb_dsbr100_video_device_release(struct video_device *videodev)
555{
556 struct dsbr100_device *radio = videodev_to_radio(videodev);
557
558 kfree(radio->transfer_buffer);
559 kfree(radio);
560}
561
Douglas Landgraf7002a4f2007-05-07 16:43:01 -0300562/* File system interface */
563static const struct file_operations usb_dsbr100_fops = {
564 .owner = THIS_MODULE,
565 .open = usb_dsbr100_open,
566 .release = usb_dsbr100_close,
567 .ioctl = video_ioctl2,
Douglas Schilling Landgraf078ff792008-04-22 14:46:11 -0300568#ifdef CONFIG_COMPAT
Douglas Landgraf7002a4f2007-05-07 16:43:01 -0300569 .compat_ioctl = v4l_compat_ioctl32,
Douglas Schilling Landgraf078ff792008-04-22 14:46:11 -0300570#endif
Douglas Landgraf7002a4f2007-05-07 16:43:01 -0300571 .llseek = no_llseek,
572};
573
Hans Verkuila3998102008-07-21 02:57:38 -0300574static const struct v4l2_ioctl_ops usb_dsbr100_ioctl_ops = {
Douglas Landgraf7002a4f2007-05-07 16:43:01 -0300575 .vidioc_querycap = vidioc_querycap,
576 .vidioc_g_tuner = vidioc_g_tuner,
577 .vidioc_s_tuner = vidioc_s_tuner,
578 .vidioc_g_frequency = vidioc_g_frequency,
579 .vidioc_s_frequency = vidioc_s_frequency,
580 .vidioc_queryctrl = vidioc_queryctrl,
581 .vidioc_g_ctrl = vidioc_g_ctrl,
582 .vidioc_s_ctrl = vidioc_s_ctrl,
583 .vidioc_g_audio = vidioc_g_audio,
584 .vidioc_s_audio = vidioc_s_audio,
585 .vidioc_g_input = vidioc_g_input,
586 .vidioc_s_input = vidioc_s_input,
587};
588
Hans Verkuila3998102008-07-21 02:57:38 -0300589/* V4L2 interface */
Alexey Klimov3a0efc32008-12-27 21:32:49 -0300590static struct video_device dsbr100_videodev_data = {
Hans Verkuila3998102008-07-21 02:57:38 -0300591 .name = "D-Link DSB-R 100",
Hans Verkuila3998102008-07-21 02:57:38 -0300592 .fops = &usb_dsbr100_fops,
593 .ioctl_ops = &usb_dsbr100_ioctl_ops,
Alexey Klimov3a0efc32008-12-27 21:32:49 -0300594 .release = usb_dsbr100_video_device_release,
Hans Verkuila3998102008-07-21 02:57:38 -0300595};
596
Douglas Landgraf7002a4f2007-05-07 16:43:01 -0300597/* check if the device is present and register with v4l and
598usb if it is */
599static int usb_dsbr100_probe(struct usb_interface *intf,
600 const struct usb_device_id *id)
601{
602 struct dsbr100_device *radio;
603
Alexey Klimov223377e2008-10-19 23:50:27 -0300604 radio = kmalloc(sizeof(struct dsbr100_device), GFP_KERNEL);
605
606 if (!radio)
Douglas Landgraf7002a4f2007-05-07 16:43:01 -0300607 return -ENOMEM;
Alexey Klimov223377e2008-10-19 23:50:27 -0300608
609 radio->transfer_buffer = kmalloc(TB_LEN, GFP_KERNEL);
610
611 if (!(radio->transfer_buffer)) {
Oliver Neukum863c86d2007-12-03 06:48:43 -0300612 kfree(radio);
613 return -ENOMEM;
614 }
Alexey Klimov223377e2008-10-19 23:50:27 -0300615
Alexey Klimov3a0efc32008-12-27 21:32:49 -0300616 mutex_init(&radio->lock);
617 radio->videodev = dsbr100_videodev_data;
618
Douglas Landgraf7002a4f2007-05-07 16:43:01 -0300619 radio->removed = 0;
620 radio->users = 0;
621 radio->usbdev = interface_to_usbdev(intf);
Alexey Klimov223377e2008-10-19 23:50:27 -0300622 radio->curfreq = FREQ_MIN * FREQ_MUL;
Alexey Klimov3a0efc32008-12-27 21:32:49 -0300623 video_set_drvdata(&radio->videodev, radio);
624 if (video_register_device(&radio->videodev, VFL_TYPE_RADIO, radio_nr) < 0) {
Greg Kroah-Hartmanaa826612008-08-14 09:37:34 -0700625 dev_warn(&intf->dev, "Could not register video device\n");
Oliver Neukum863c86d2007-12-03 06:48:43 -0300626 kfree(radio->transfer_buffer);
Douglas Landgraf7002a4f2007-05-07 16:43:01 -0300627 kfree(radio);
628 return -EIO;
629 }
630 usb_set_intfdata(intf, radio);
631 return 0;
632}
633
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634static int __init dsbr100_init(void)
635{
636 int retval = usb_register(&usb_dsbr100_driver);
Greg Kroah-Hartmana482f322008-10-10 05:08:23 -0300637 printk(KERN_INFO KBUILD_MODNAME ": " DRIVER_VERSION ":"
638 DRIVER_DESC "\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 return retval;
640}
641
642static void __exit dsbr100_exit(void)
643{
644 usb_deregister(&usb_dsbr100_driver);
645}
646
647module_init (dsbr100_init);
648module_exit (dsbr100_exit);
649
650MODULE_AUTHOR( DRIVER_AUTHOR );
651MODULE_DESCRIPTION( DRIVER_DESC );
652MODULE_LICENSE("GPL");