blob: eafa547ca96beeefc74e1fd70de2258fe4b76736 [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
148
149static int usb_dsbr100_probe(struct usb_interface *intf,
150 const struct usb_device_id *id);
151static void usb_dsbr100_disconnect(struct usb_interface *intf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152static int usb_dsbr100_open(struct inode *inode, struct file *file);
153static int usb_dsbr100_close(struct inode *inode, struct file *file);
Alexey Klimov04e0ffb2008-11-08 00:40:46 -0300154static int usb_dsbr100_suspend(struct usb_interface *intf,
155 pm_message_t message);
156static int usb_dsbr100_resume(struct usb_interface *intf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157
158static int radio_nr = -1;
159module_param(radio_nr, int, 0);
160
161/* Data for one (physical) device */
Alan Cox5aff3082006-08-08 15:47:50 -0300162struct dsbr100_device {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 struct usb_device *usbdev;
164 struct video_device *videodev;
Oliver Neukum863c86d2007-12-03 06:48:43 -0300165 u8 *transfer_buffer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 int curfreq;
167 int stereo;
168 int users;
169 int removed;
Alan Cox5aff3082006-08-08 15:47:50 -0300170 int muted;
171};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172
173
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174static struct usb_device_id usb_dsbr100_device_table [] = {
175 { USB_DEVICE(DSB100_VENDOR, DSB100_PRODUCT) },
176 { } /* Terminating entry */
177};
178
179MODULE_DEVICE_TABLE (usb, usb_dsbr100_device_table);
180
181/* USB subsystem interface */
182static struct usb_driver usb_dsbr100_driver = {
Alexey Klimov04e0ffb2008-11-08 00:40:46 -0300183 .name = "dsbr100",
184 .probe = usb_dsbr100_probe,
185 .disconnect = usb_dsbr100_disconnect,
186 .id_table = usb_dsbr100_device_table,
187 .suspend = usb_dsbr100_suspend,
188 .resume = usb_dsbr100_resume,
189 .reset_resume = usb_dsbr100_resume,
190 .supports_autosuspend = 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191};
192
193/* Low-level device interface begins here */
194
195/* switch on radio */
Alan Cox5aff3082006-08-08 15:47:50 -0300196static int dsbr100_start(struct dsbr100_device *radio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197{
198 if (usb_control_msg(radio->usbdev, usb_rcvctrlpipe(radio->usbdev, 0),
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300199 USB_REQ_GET_STATUS,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN,
Alexey Klimov223377e2008-10-19 23:50:27 -0300201 0x00, 0xC7, radio->transfer_buffer, 8, 300) < 0 ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 usb_control_msg(radio->usbdev, usb_rcvctrlpipe(radio->usbdev, 0),
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300203 DSB100_ONOFF,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN,
Alexey Klimov223377e2008-10-19 23:50:27 -0300205 0x01, 0x00, radio->transfer_buffer, 8, 300) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 return -1;
Alan Cox5aff3082006-08-08 15:47:50 -0300207 radio->muted=0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 return (radio->transfer_buffer)[0];
209}
210
211
212/* switch off radio */
Alan Cox5aff3082006-08-08 15:47:50 -0300213static int dsbr100_stop(struct dsbr100_device *radio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214{
215 if (usb_control_msg(radio->usbdev, usb_rcvctrlpipe(radio->usbdev, 0),
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300216 USB_REQ_GET_STATUS,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN,
Alexey Klimov223377e2008-10-19 23:50:27 -0300218 0x16, 0x1C, radio->transfer_buffer, 8, 300) < 0 ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 usb_control_msg(radio->usbdev, usb_rcvctrlpipe(radio->usbdev, 0),
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300220 DSB100_ONOFF,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN,
Alexey Klimov223377e2008-10-19 23:50:27 -0300222 0x00, 0x00, radio->transfer_buffer, 8, 300) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 return -1;
Alan Cox5aff3082006-08-08 15:47:50 -0300224 radio->muted=1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 return (radio->transfer_buffer)[0];
226}
227
228/* set a frequency, freq is defined by v4l's TUNER_LOW, i.e. 1/16th kHz */
Alan Cox5aff3082006-08-08 15:47:50 -0300229static int dsbr100_setfreq(struct dsbr100_device *radio, int freq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230{
Alexey Klimov223377e2008-10-19 23:50:27 -0300231 freq = (freq / 16 * 80) / 1000 + 856;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 if (usb_control_msg(radio->usbdev, usb_rcvctrlpipe(radio->usbdev, 0),
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300233 DSB100_TUNE,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN,
Alexey Klimov223377e2008-10-19 23:50:27 -0300235 (freq >> 8) & 0x00ff, freq & 0xff,
236 radio->transfer_buffer, 8, 300) < 0 ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 usb_control_msg(radio->usbdev, usb_rcvctrlpipe(radio->usbdev, 0),
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300238 USB_REQ_GET_STATUS,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN,
Alexey Klimov223377e2008-10-19 23:50:27 -0300240 0x96, 0xB7, radio->transfer_buffer, 8, 300) < 0 ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 usb_control_msg(radio->usbdev, usb_rcvctrlpipe(radio->usbdev, 0),
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300242 USB_REQ_GET_STATUS,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN,
Alexey Klimov223377e2008-10-19 23:50:27 -0300244 0x00, 0x24, radio->transfer_buffer, 8, 300) < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 radio->stereo = -1;
246 return -1;
247 }
Alexey Klimov223377e2008-10-19 23:50:27 -0300248 radio->stereo = !((radio->transfer_buffer)[0] & 0x01);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 return (radio->transfer_buffer)[0];
250}
251
252/* return the device status. This is, in effect, just whether it
253sees a stereo signal or not. Pity. */
Alan Cox5aff3082006-08-08 15:47:50 -0300254static void dsbr100_getstat(struct dsbr100_device *radio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255{
256 if (usb_control_msg(radio->usbdev, usb_rcvctrlpipe(radio->usbdev, 0),
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300257 USB_REQ_GET_STATUS,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN,
Alexey Klimov223377e2008-10-19 23:50:27 -0300259 0x00 , 0x24, radio->transfer_buffer, 8, 300) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 radio->stereo = -1;
261 else
Alexey Klimov223377e2008-10-19 23:50:27 -0300262 radio->stereo = !(radio->transfer_buffer[0] & 0x01);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263}
264
265
266/* USB subsystem interface begins here */
267
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268/* handle unplugging of the device, release data structures
269if nothing keeps us from doing it. If something is still
270keeping us busy, the release callback of v4l will take care
Oliver Neukum863c86d2007-12-03 06:48:43 -0300271of releasing it. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272static void usb_dsbr100_disconnect(struct usb_interface *intf)
273{
Alan Cox5aff3082006-08-08 15:47:50 -0300274 struct dsbr100_device *radio = usb_get_intfdata(intf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275
276 usb_set_intfdata (intf, NULL);
277 if (radio) {
278 video_unregister_device(radio->videodev);
279 radio->videodev = NULL;
280 if (radio->users) {
Oliver Neukum863c86d2007-12-03 06:48:43 -0300281 kfree(radio->transfer_buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 kfree(radio);
283 } else {
284 radio->removed = 1;
285 }
286 }
287}
288
289
Douglas Landgraf7002a4f2007-05-07 16:43:01 -0300290static int vidioc_querycap(struct file *file, void *priv,
291 struct v4l2_capability *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292{
Douglas Landgraf7002a4f2007-05-07 16:43:01 -0300293 strlcpy(v->driver, "dsbr100", sizeof(v->driver));
294 strlcpy(v->card, "D-Link R-100 USB FM Radio", sizeof(v->card));
Alexey Klimov448441c2008-10-19 23:20:47 -0300295 sprintf(v->bus_info, "USB");
Douglas Landgraf7002a4f2007-05-07 16:43:01 -0300296 v->version = RADIO_VERSION;
297 v->capabilities = V4L2_CAP_TUNER;
298 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299}
300
Douglas Landgraf7002a4f2007-05-07 16:43:01 -0300301static int vidioc_g_tuner(struct file *file, void *priv,
302 struct v4l2_tuner *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303{
Hans Verkuilc170ecf2008-08-23 08:32:09 -0300304 struct dsbr100_device *radio = video_drvdata(file);
Douglas Landgraf7002a4f2007-05-07 16:43:01 -0300305
306 if (v->index > 0)
307 return -EINVAL;
308
309 dsbr100_getstat(radio);
310 strcpy(v->name, "FM");
311 v->type = V4L2_TUNER_RADIO;
Alexey Klimov223377e2008-10-19 23:50:27 -0300312 v->rangelow = FREQ_MIN * FREQ_MUL;
313 v->rangehigh = FREQ_MAX * FREQ_MUL;
314 v->rxsubchans = V4L2_TUNER_SUB_MONO | V4L2_TUNER_SUB_STEREO;
Douglas Landgraf7002a4f2007-05-07 16:43:01 -0300315 v->capability = V4L2_TUNER_CAP_LOW;
316 if(radio->stereo)
317 v->audmode = V4L2_TUNER_MODE_STEREO;
318 else
319 v->audmode = V4L2_TUNER_MODE_MONO;
320 v->signal = 0xffff; /* We can't get the signal strength */
321 return 0;
322}
323
324static int vidioc_s_tuner(struct file *file, void *priv,
325 struct v4l2_tuner *v)
326{
327 if (v->index > 0)
328 return -EINVAL;
329
330 return 0;
331}
332
333static int vidioc_s_frequency(struct file *file, void *priv,
334 struct v4l2_frequency *f)
335{
Hans Verkuilc170ecf2008-08-23 08:32:09 -0300336 struct dsbr100_device *radio = video_drvdata(file);
Douglas Landgraf7002a4f2007-05-07 16:43:01 -0300337
338 radio->curfreq = f->frequency;
Alexey Klimov223377e2008-10-19 23:50:27 -0300339 if (dsbr100_setfreq(radio, radio->curfreq) == -1)
Greg Kroah-Hartmanaa826612008-08-14 09:37:34 -0700340 dev_warn(&radio->usbdev->dev, "Set frequency failed\n");
Douglas Landgraf7002a4f2007-05-07 16:43:01 -0300341 return 0;
342}
343
344static int vidioc_g_frequency(struct file *file, void *priv,
345 struct v4l2_frequency *f)
346{
Hans Verkuilc170ecf2008-08-23 08:32:09 -0300347 struct dsbr100_device *radio = video_drvdata(file);
Douglas Landgraf7002a4f2007-05-07 16:43:01 -0300348
349 f->type = V4L2_TUNER_RADIO;
350 f->frequency = radio->curfreq;
351 return 0;
352}
353
354static int vidioc_queryctrl(struct file *file, void *priv,
355 struct v4l2_queryctrl *qc)
356{
357 int i;
358
359 for (i = 0; i < ARRAY_SIZE(radio_qctrl); i++) {
360 if (qc->id && qc->id == radio_qctrl[i].id) {
Alexey Klimov223377e2008-10-19 23:50:27 -0300361 memcpy(qc, &(radio_qctrl[i]), sizeof(*qc));
Douglas Landgraf7002a4f2007-05-07 16:43:01 -0300362 return 0;
363 }
364 }
365 return -EINVAL;
366}
367
368static int vidioc_g_ctrl(struct file *file, void *priv,
369 struct v4l2_control *ctrl)
370{
Hans Verkuilc170ecf2008-08-23 08:32:09 -0300371 struct dsbr100_device *radio = video_drvdata(file);
Douglas Landgraf7002a4f2007-05-07 16:43:01 -0300372
373 switch (ctrl->id) {
374 case V4L2_CID_AUDIO_MUTE:
375 ctrl->value = radio->muted;
376 return 0;
377 }
378 return -EINVAL;
379}
380
381static int vidioc_s_ctrl(struct file *file, void *priv,
382 struct v4l2_control *ctrl)
383{
Hans Verkuilc170ecf2008-08-23 08:32:09 -0300384 struct dsbr100_device *radio = video_drvdata(file);
Douglas Landgraf7002a4f2007-05-07 16:43:01 -0300385
386 switch (ctrl->id) {
387 case V4L2_CID_AUDIO_MUTE:
388 if (ctrl->value) {
Alexey Klimov90b698d2008-10-09 13:42:32 -0300389 if (dsbr100_stop(radio) == -1) {
Greg Kroah-Hartmanaa826612008-08-14 09:37:34 -0700390 dev_warn(&radio->usbdev->dev,
391 "Radio did not respond properly\n");
Alexey Klimov90b698d2008-10-09 13:42:32 -0300392 return -EBUSY;
393 }
Douglas Landgraf7002a4f2007-05-07 16:43:01 -0300394 } else {
Alexey Klimov90b698d2008-10-09 13:42:32 -0300395 if (dsbr100_start(radio) == -1) {
Greg Kroah-Hartmanaa826612008-08-14 09:37:34 -0700396 dev_warn(&radio->usbdev->dev,
397 "Radio did not respond properly\n");
Alexey Klimov90b698d2008-10-09 13:42:32 -0300398 return -EBUSY;
399 }
Douglas Landgraf7002a4f2007-05-07 16:43:01 -0300400 }
401 return 0;
402 }
403 return -EINVAL;
404}
405
406static int vidioc_g_audio(struct file *file, void *priv,
407 struct v4l2_audio *a)
408{
409 if (a->index > 1)
410 return -EINVAL;
411
412 strcpy(a->name, "Radio");
413 a->capability = V4L2_AUDCAP_STEREO;
414 return 0;
415}
416
417static int vidioc_g_input(struct file *filp, void *priv, unsigned int *i)
418{
419 *i = 0;
420 return 0;
421}
422
423static int vidioc_s_input(struct file *filp, void *priv, unsigned int i)
424{
425 if (i != 0)
426 return -EINVAL;
427 return 0;
428}
429
430static int vidioc_s_audio(struct file *file, void *priv,
431 struct v4l2_audio *a)
432{
433 if (a->index != 0)
434 return -EINVAL;
435 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436}
437
438static int usb_dsbr100_open(struct inode *inode, struct file *file)
439{
Hans Verkuilc170ecf2008-08-23 08:32:09 -0300440 struct dsbr100_device *radio = video_drvdata(file);
Alexey Klimovb9f35732008-10-20 00:00:03 -0300441 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442
Hans Verkuild56dc612008-07-30 08:43:36 -0300443 lock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 radio->users = 1;
Alan Cox5aff3082006-08-08 15:47:50 -0300445 radio->muted = 1;
446
Alexey Klimov223377e2008-10-19 23:50:27 -0300447 if (dsbr100_start(radio) < 0) {
Greg Kroah-Hartmanaa826612008-08-14 09:37:34 -0700448 dev_warn(&radio->usbdev->dev,
449 "Radio did not start up properly\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 radio->users = 0;
Hans Verkuild56dc612008-07-30 08:43:36 -0300451 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 return -EIO;
453 }
Alexey Klimovb9f35732008-10-20 00:00:03 -0300454
455 retval = dsbr100_setfreq(radio, radio->curfreq);
456
457 if (retval == -1)
458 printk(KERN_WARNING KBUILD_MODNAME ": Set frequency failed\n");
459
Hans Verkuild56dc612008-07-30 08:43:36 -0300460 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 return 0;
462}
463
464static int usb_dsbr100_close(struct inode *inode, struct file *file)
465{
Hans Verkuilc170ecf2008-08-23 08:32:09 -0300466 struct dsbr100_device *radio = video_drvdata(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467
468 if (!radio)
469 return -ENODEV;
470 radio->users = 0;
471 if (radio->removed) {
Oliver Neukum863c86d2007-12-03 06:48:43 -0300472 kfree(radio->transfer_buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 kfree(radio);
474 }
475 return 0;
476}
477
Alexey Klimov04e0ffb2008-11-08 00:40:46 -0300478/* Suspend device - stop device. */
479static int usb_dsbr100_suspend(struct usb_interface *intf, pm_message_t message)
480{
481 struct dsbr100_device *radio = usb_get_intfdata(intf);
482 int retval;
483
484 retval = dsbr100_stop(radio);
485 if (retval == -1)
486 dev_warn(&intf->dev, "dsbr100_stop failed\n");
487
488 dev_info(&intf->dev, "going into suspend..\n");
489
490 return 0;
491}
492
493/* Resume device - start device. */
494static int usb_dsbr100_resume(struct usb_interface *intf)
495{
496 struct dsbr100_device *radio = usb_get_intfdata(intf);
497 int retval;
498
499 retval = dsbr100_start(radio);
500 if (retval == -1)
501 dev_warn(&intf->dev, "dsbr100_start failed\n");
502
503 dev_info(&intf->dev, "coming out of suspend..\n");
504
505 return 0;
506}
507
Douglas Landgraf7002a4f2007-05-07 16:43:01 -0300508/* File system interface */
509static const struct file_operations usb_dsbr100_fops = {
510 .owner = THIS_MODULE,
511 .open = usb_dsbr100_open,
512 .release = usb_dsbr100_close,
513 .ioctl = video_ioctl2,
Douglas Schilling Landgraf078ff792008-04-22 14:46:11 -0300514#ifdef CONFIG_COMPAT
Douglas Landgraf7002a4f2007-05-07 16:43:01 -0300515 .compat_ioctl = v4l_compat_ioctl32,
Douglas Schilling Landgraf078ff792008-04-22 14:46:11 -0300516#endif
Douglas Landgraf7002a4f2007-05-07 16:43:01 -0300517 .llseek = no_llseek,
518};
519
Hans Verkuila3998102008-07-21 02:57:38 -0300520static const struct v4l2_ioctl_ops usb_dsbr100_ioctl_ops = {
Douglas Landgraf7002a4f2007-05-07 16:43:01 -0300521 .vidioc_querycap = vidioc_querycap,
522 .vidioc_g_tuner = vidioc_g_tuner,
523 .vidioc_s_tuner = vidioc_s_tuner,
524 .vidioc_g_frequency = vidioc_g_frequency,
525 .vidioc_s_frequency = vidioc_s_frequency,
526 .vidioc_queryctrl = vidioc_queryctrl,
527 .vidioc_g_ctrl = vidioc_g_ctrl,
528 .vidioc_s_ctrl = vidioc_s_ctrl,
529 .vidioc_g_audio = vidioc_g_audio,
530 .vidioc_s_audio = vidioc_s_audio,
531 .vidioc_g_input = vidioc_g_input,
532 .vidioc_s_input = vidioc_s_input,
533};
534
Hans Verkuila3998102008-07-21 02:57:38 -0300535/* V4L2 interface */
536static struct video_device dsbr100_videodev_template = {
Hans Verkuila3998102008-07-21 02:57:38 -0300537 .name = "D-Link DSB-R 100",
Hans Verkuila3998102008-07-21 02:57:38 -0300538 .fops = &usb_dsbr100_fops,
539 .ioctl_ops = &usb_dsbr100_ioctl_ops,
540 .release = video_device_release,
541};
542
Douglas Landgraf7002a4f2007-05-07 16:43:01 -0300543/* check if the device is present and register with v4l and
544usb if it is */
545static int usb_dsbr100_probe(struct usb_interface *intf,
546 const struct usb_device_id *id)
547{
548 struct dsbr100_device *radio;
549
Alexey Klimov223377e2008-10-19 23:50:27 -0300550 radio = kmalloc(sizeof(struct dsbr100_device), GFP_KERNEL);
551
552 if (!radio)
Douglas Landgraf7002a4f2007-05-07 16:43:01 -0300553 return -ENOMEM;
Alexey Klimov223377e2008-10-19 23:50:27 -0300554
555 radio->transfer_buffer = kmalloc(TB_LEN, GFP_KERNEL);
556
557 if (!(radio->transfer_buffer)) {
Oliver Neukum863c86d2007-12-03 06:48:43 -0300558 kfree(radio);
559 return -ENOMEM;
560 }
Alexey Klimov223377e2008-10-19 23:50:27 -0300561 radio->videodev = video_device_alloc();
562
563 if (!(radio->videodev)) {
Oliver Neukum863c86d2007-12-03 06:48:43 -0300564 kfree(radio->transfer_buffer);
Douglas Landgraf7002a4f2007-05-07 16:43:01 -0300565 kfree(radio);
566 return -ENOMEM;
567 }
568 memcpy(radio->videodev, &dsbr100_videodev_template,
569 sizeof(dsbr100_videodev_template));
570 radio->removed = 0;
571 radio->users = 0;
572 radio->usbdev = interface_to_usbdev(intf);
Alexey Klimov223377e2008-10-19 23:50:27 -0300573 radio->curfreq = FREQ_MIN * FREQ_MUL;
Douglas Landgraf7002a4f2007-05-07 16:43:01 -0300574 video_set_drvdata(radio->videodev, radio);
Hans Verkuilcba99ae2008-09-03 17:11:58 -0300575 if (video_register_device(radio->videodev, VFL_TYPE_RADIO, radio_nr) < 0) {
Greg Kroah-Hartmanaa826612008-08-14 09:37:34 -0700576 dev_warn(&intf->dev, "Could not register video device\n");
Douglas Landgraf7002a4f2007-05-07 16:43:01 -0300577 video_device_release(radio->videodev);
Oliver Neukum863c86d2007-12-03 06:48:43 -0300578 kfree(radio->transfer_buffer);
Douglas Landgraf7002a4f2007-05-07 16:43:01 -0300579 kfree(radio);
580 return -EIO;
581 }
582 usb_set_intfdata(intf, radio);
583 return 0;
584}
585
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586static int __init dsbr100_init(void)
587{
588 int retval = usb_register(&usb_dsbr100_driver);
Greg Kroah-Hartmana482f322008-10-10 05:08:23 -0300589 printk(KERN_INFO KBUILD_MODNAME ": " DRIVER_VERSION ":"
590 DRIVER_DESC "\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 return retval;
592}
593
594static void __exit dsbr100_exit(void)
595{
596 usb_deregister(&usb_dsbr100_driver);
597}
598
599module_init (dsbr100_init);
600module_exit (dsbr100_exit);
601
602MODULE_AUTHOR( DRIVER_AUTHOR );
603MODULE_DESCRIPTION( DRIVER_DESC );
604MODULE_LICENSE("GPL");