V4L/DVB (9304): dsbr100: CodingStyle issue
[linux-2.6/mini2440.git] / drivers / media / radio / dsbr100.c
blobe88c58ea1b76eb2575088334d23f89e31243a1c0
1 /* 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.
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.
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:-)
15 You might find some interesting stuff about this module at
16 http://unimut.fsk.uni-heidelberg.de/unimut/demi/dsbr
18 Copyright (c) 2000 Markus Demleitner <msdemlei@cl.uni-heidelberg.de>
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.
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.
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
34 History:
36 Version 0.43:
37 Oliver Neukum: avoided DMA coherency issue
39 Version 0.42:
40 Converted dsbr100 to use video_ioctl2
41 by Douglas Landgraf <dougsland@gmail.com>
43 Version 0.41-ac1:
44 Alan Cox: Some cleanups and fixes
46 Version 0.41:
47 Converted to V4L2 API by Mauro Carvalho Chehab <mchehab@infradead.org>
49 Version 0.40:
50 Markus: Updates for 2.6.x kernels, code layout changes, name sanitizing
52 Version 0.30:
53 Markus: Updates for 2.5.x kernel and more ISO compliant source
55 Version 0.25:
56 PSL and Markus: Cleanup, radio now doesn't stop on device close
58 Version 0.24:
59 Markus: Hope I got these silly VIDEO_TUNER_LOW issues finally
60 right. Some minor cleanup, improved standalone compilation
62 Version 0.23:
63 Markus: Sign extension bug fixed by declaring transfer_buffer unsigned
65 Version 0.22:
66 Markus: Some (brown bag) cleanup in what VIDIOCSTUNER returns,
67 thanks to Mike Cox for pointing the problem out.
69 Version 0.21:
70 Markus: Minor cleanup, warnings if something goes wrong, lame attempt
71 to adhere to Documentation/CodingStyle
73 Version 0.2:
74 Brad Hards <bradh@dynamite.com.au>: Fixes to make it work as non-module
75 Markus: Copyright clarification
77 Version 0.01: Markus: initial release
81 #include <linux/kernel.h>
82 #include <linux/module.h>
83 #include <linux/init.h>
84 #include <linux/slab.h>
85 #include <linux/input.h>
86 #include <linux/videodev2.h>
87 #include <media/v4l2-common.h>
88 #include <media/v4l2-ioctl.h>
89 #include <linux/usb.h>
92 * Version Information
94 #include <linux/version.h> /* for KERNEL_VERSION MACRO */
96 #define DRIVER_VERSION "v0.41"
97 #define RADIO_VERSION KERNEL_VERSION(0,4,1)
99 static struct v4l2_queryctrl radio_qctrl[] = {
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,
110 #define DRIVER_AUTHOR "Markus Demleitner <msdemlei@tucana.harvard.edu>"
111 #define DRIVER_DESC "D-Link DSB-R100 USB FM radio driver"
113 #define DSB100_VENDOR 0x04b4
114 #define DSB100_PRODUCT 0x1002
116 /* Commands the device appears to understand */
117 #define DSB100_TUNE 1
118 #define DSB100_ONOFF 2
120 #define TB_LEN 16
122 /* Frequency limits in MHz -- these are European values. For Japanese
123 devices, that would be 76 and 91. */
124 #define FREQ_MIN 87.5
125 #define FREQ_MAX 108.0
126 #define FREQ_MUL 16000
129 static int usb_dsbr100_probe(struct usb_interface *intf,
130 const struct usb_device_id *id);
131 static void usb_dsbr100_disconnect(struct usb_interface *intf);
132 static int usb_dsbr100_open(struct inode *inode, struct file *file);
133 static int usb_dsbr100_close(struct inode *inode, struct file *file);
135 static int radio_nr = -1;
136 module_param(radio_nr, int, 0);
138 /* Data for one (physical) device */
139 struct dsbr100_device {
140 struct usb_device *usbdev;
141 struct video_device *videodev;
142 u8 *transfer_buffer;
143 int curfreq;
144 int stereo;
145 int users;
146 int removed;
147 int muted;
151 static struct usb_device_id usb_dsbr100_device_table [] = {
152 { USB_DEVICE(DSB100_VENDOR, DSB100_PRODUCT) },
153 { } /* Terminating entry */
156 MODULE_DEVICE_TABLE (usb, usb_dsbr100_device_table);
158 /* USB subsystem interface */
159 static struct usb_driver usb_dsbr100_driver = {
160 .name = "dsbr100",
161 .probe = usb_dsbr100_probe,
162 .disconnect = usb_dsbr100_disconnect,
163 .id_table = usb_dsbr100_device_table,
166 /* Low-level device interface begins here */
168 /* switch on radio */
169 static int dsbr100_start(struct dsbr100_device *radio)
171 if (usb_control_msg(radio->usbdev, usb_rcvctrlpipe(radio->usbdev, 0),
172 USB_REQ_GET_STATUS,
173 USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN,
174 0x00, 0xC7, radio->transfer_buffer, 8, 300) < 0 ||
175 usb_control_msg(radio->usbdev, usb_rcvctrlpipe(radio->usbdev, 0),
176 DSB100_ONOFF,
177 USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN,
178 0x01, 0x00, radio->transfer_buffer, 8, 300) < 0)
179 return -1;
180 radio->muted=0;
181 return (radio->transfer_buffer)[0];
185 /* switch off radio */
186 static int dsbr100_stop(struct dsbr100_device *radio)
188 if (usb_control_msg(radio->usbdev, usb_rcvctrlpipe(radio->usbdev, 0),
189 USB_REQ_GET_STATUS,
190 USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN,
191 0x16, 0x1C, radio->transfer_buffer, 8, 300) < 0 ||
192 usb_control_msg(radio->usbdev, usb_rcvctrlpipe(radio->usbdev, 0),
193 DSB100_ONOFF,
194 USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN,
195 0x00, 0x00, radio->transfer_buffer, 8, 300) < 0)
196 return -1;
197 radio->muted=1;
198 return (radio->transfer_buffer)[0];
201 /* set a frequency, freq is defined by v4l's TUNER_LOW, i.e. 1/16th kHz */
202 static int dsbr100_setfreq(struct dsbr100_device *radio, int freq)
204 freq = (freq / 16 * 80) / 1000 + 856;
205 if (usb_control_msg(radio->usbdev, usb_rcvctrlpipe(radio->usbdev, 0),
206 DSB100_TUNE,
207 USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN,
208 (freq >> 8) & 0x00ff, freq & 0xff,
209 radio->transfer_buffer, 8, 300) < 0 ||
210 usb_control_msg(radio->usbdev, usb_rcvctrlpipe(radio->usbdev, 0),
211 USB_REQ_GET_STATUS,
212 USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN,
213 0x96, 0xB7, radio->transfer_buffer, 8, 300) < 0 ||
214 usb_control_msg(radio->usbdev, usb_rcvctrlpipe(radio->usbdev, 0),
215 USB_REQ_GET_STATUS,
216 USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN,
217 0x00, 0x24, radio->transfer_buffer, 8, 300) < 0) {
218 radio->stereo = -1;
219 return -1;
221 radio->stereo = !((radio->transfer_buffer)[0] & 0x01);
222 return (radio->transfer_buffer)[0];
225 /* return the device status. This is, in effect, just whether it
226 sees a stereo signal or not. Pity. */
227 static void dsbr100_getstat(struct dsbr100_device *radio)
229 if (usb_control_msg(radio->usbdev, usb_rcvctrlpipe(radio->usbdev, 0),
230 USB_REQ_GET_STATUS,
231 USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN,
232 0x00 , 0x24, radio->transfer_buffer, 8, 300) < 0)
233 radio->stereo = -1;
234 else
235 radio->stereo = !(radio->transfer_buffer[0] & 0x01);
239 /* USB subsystem interface begins here */
241 /* handle unplugging of the device, release data structures
242 if nothing keeps us from doing it. If something is still
243 keeping us busy, the release callback of v4l will take care
244 of releasing it. */
245 static void usb_dsbr100_disconnect(struct usb_interface *intf)
247 struct dsbr100_device *radio = usb_get_intfdata(intf);
249 usb_set_intfdata (intf, NULL);
250 if (radio) {
251 video_unregister_device(radio->videodev);
252 radio->videodev = NULL;
253 if (radio->users) {
254 kfree(radio->transfer_buffer);
255 kfree(radio);
256 } else {
257 radio->removed = 1;
263 static int vidioc_querycap(struct file *file, void *priv,
264 struct v4l2_capability *v)
266 strlcpy(v->driver, "dsbr100", sizeof(v->driver));
267 strlcpy(v->card, "D-Link R-100 USB FM Radio", sizeof(v->card));
268 sprintf(v->bus_info, "USB");
269 v->version = RADIO_VERSION;
270 v->capabilities = V4L2_CAP_TUNER;
271 return 0;
274 static int vidioc_g_tuner(struct file *file, void *priv,
275 struct v4l2_tuner *v)
277 struct dsbr100_device *radio = video_drvdata(file);
279 if (v->index > 0)
280 return -EINVAL;
282 dsbr100_getstat(radio);
283 strcpy(v->name, "FM");
284 v->type = V4L2_TUNER_RADIO;
285 v->rangelow = FREQ_MIN * FREQ_MUL;
286 v->rangehigh = FREQ_MAX * FREQ_MUL;
287 v->rxsubchans = V4L2_TUNER_SUB_MONO | V4L2_TUNER_SUB_STEREO;
288 v->capability = V4L2_TUNER_CAP_LOW;
289 if(radio->stereo)
290 v->audmode = V4L2_TUNER_MODE_STEREO;
291 else
292 v->audmode = V4L2_TUNER_MODE_MONO;
293 v->signal = 0xffff; /* We can't get the signal strength */
294 return 0;
297 static int vidioc_s_tuner(struct file *file, void *priv,
298 struct v4l2_tuner *v)
300 if (v->index > 0)
301 return -EINVAL;
303 return 0;
306 static int vidioc_s_frequency(struct file *file, void *priv,
307 struct v4l2_frequency *f)
309 struct dsbr100_device *radio = video_drvdata(file);
311 radio->curfreq = f->frequency;
312 if (dsbr100_setfreq(radio, radio->curfreq) == -1)
313 dev_warn(&radio->usbdev->dev, "Set frequency failed\n");
314 return 0;
317 static int vidioc_g_frequency(struct file *file, void *priv,
318 struct v4l2_frequency *f)
320 struct dsbr100_device *radio = video_drvdata(file);
322 f->type = V4L2_TUNER_RADIO;
323 f->frequency = radio->curfreq;
324 return 0;
327 static int vidioc_queryctrl(struct file *file, void *priv,
328 struct v4l2_queryctrl *qc)
330 int i;
332 for (i = 0; i < ARRAY_SIZE(radio_qctrl); i++) {
333 if (qc->id && qc->id == radio_qctrl[i].id) {
334 memcpy(qc, &(radio_qctrl[i]), sizeof(*qc));
335 return 0;
338 return -EINVAL;
341 static int vidioc_g_ctrl(struct file *file, void *priv,
342 struct v4l2_control *ctrl)
344 struct dsbr100_device *radio = video_drvdata(file);
346 switch (ctrl->id) {
347 case V4L2_CID_AUDIO_MUTE:
348 ctrl->value = radio->muted;
349 return 0;
351 return -EINVAL;
354 static int vidioc_s_ctrl(struct file *file, void *priv,
355 struct v4l2_control *ctrl)
357 struct dsbr100_device *radio = video_drvdata(file);
359 switch (ctrl->id) {
360 case V4L2_CID_AUDIO_MUTE:
361 if (ctrl->value) {
362 if (dsbr100_stop(radio) == -1) {
363 dev_warn(&radio->usbdev->dev,
364 "Radio did not respond properly\n");
365 return -EBUSY;
367 } else {
368 if (dsbr100_start(radio) == -1) {
369 dev_warn(&radio->usbdev->dev,
370 "Radio did not respond properly\n");
371 return -EBUSY;
374 return 0;
376 return -EINVAL;
379 static int vidioc_g_audio(struct file *file, void *priv,
380 struct v4l2_audio *a)
382 if (a->index > 1)
383 return -EINVAL;
385 strcpy(a->name, "Radio");
386 a->capability = V4L2_AUDCAP_STEREO;
387 return 0;
390 static int vidioc_g_input(struct file *filp, void *priv, unsigned int *i)
392 *i = 0;
393 return 0;
396 static int vidioc_s_input(struct file *filp, void *priv, unsigned int i)
398 if (i != 0)
399 return -EINVAL;
400 return 0;
403 static int vidioc_s_audio(struct file *file, void *priv,
404 struct v4l2_audio *a)
406 if (a->index != 0)
407 return -EINVAL;
408 return 0;
411 static int usb_dsbr100_open(struct inode *inode, struct file *file)
413 struct dsbr100_device *radio = video_drvdata(file);
415 lock_kernel();
416 radio->users = 1;
417 radio->muted = 1;
419 if (dsbr100_start(radio) < 0) {
420 dev_warn(&radio->usbdev->dev,
421 "Radio did not start up properly\n");
422 radio->users = 0;
423 unlock_kernel();
424 return -EIO;
426 dsbr100_setfreq(radio, radio->curfreq);
427 unlock_kernel();
428 return 0;
431 static int usb_dsbr100_close(struct inode *inode, struct file *file)
433 struct dsbr100_device *radio = video_drvdata(file);
435 if (!radio)
436 return -ENODEV;
437 radio->users = 0;
438 if (radio->removed) {
439 kfree(radio->transfer_buffer);
440 kfree(radio);
442 return 0;
445 /* File system interface */
446 static const struct file_operations usb_dsbr100_fops = {
447 .owner = THIS_MODULE,
448 .open = usb_dsbr100_open,
449 .release = usb_dsbr100_close,
450 .ioctl = video_ioctl2,
451 #ifdef CONFIG_COMPAT
452 .compat_ioctl = v4l_compat_ioctl32,
453 #endif
454 .llseek = no_llseek,
457 static const struct v4l2_ioctl_ops usb_dsbr100_ioctl_ops = {
458 .vidioc_querycap = vidioc_querycap,
459 .vidioc_g_tuner = vidioc_g_tuner,
460 .vidioc_s_tuner = vidioc_s_tuner,
461 .vidioc_g_frequency = vidioc_g_frequency,
462 .vidioc_s_frequency = vidioc_s_frequency,
463 .vidioc_queryctrl = vidioc_queryctrl,
464 .vidioc_g_ctrl = vidioc_g_ctrl,
465 .vidioc_s_ctrl = vidioc_s_ctrl,
466 .vidioc_g_audio = vidioc_g_audio,
467 .vidioc_s_audio = vidioc_s_audio,
468 .vidioc_g_input = vidioc_g_input,
469 .vidioc_s_input = vidioc_s_input,
472 /* V4L2 interface */
473 static struct video_device dsbr100_videodev_template = {
474 .name = "D-Link DSB-R 100",
475 .fops = &usb_dsbr100_fops,
476 .ioctl_ops = &usb_dsbr100_ioctl_ops,
477 .release = video_device_release,
480 /* check if the device is present and register with v4l and
481 usb if it is */
482 static int usb_dsbr100_probe(struct usb_interface *intf,
483 const struct usb_device_id *id)
485 struct dsbr100_device *radio;
487 radio = kmalloc(sizeof(struct dsbr100_device), GFP_KERNEL);
489 if (!radio)
490 return -ENOMEM;
492 radio->transfer_buffer = kmalloc(TB_LEN, GFP_KERNEL);
494 if (!(radio->transfer_buffer)) {
495 kfree(radio);
496 return -ENOMEM;
498 radio->videodev = video_device_alloc();
500 if (!(radio->videodev)) {
501 kfree(radio->transfer_buffer);
502 kfree(radio);
503 return -ENOMEM;
505 memcpy(radio->videodev, &dsbr100_videodev_template,
506 sizeof(dsbr100_videodev_template));
507 radio->removed = 0;
508 radio->users = 0;
509 radio->usbdev = interface_to_usbdev(intf);
510 radio->curfreq = FREQ_MIN * FREQ_MUL;
511 video_set_drvdata(radio->videodev, radio);
512 if (video_register_device(radio->videodev, VFL_TYPE_RADIO, radio_nr) < 0) {
513 dev_warn(&intf->dev, "Could not register video device\n");
514 video_device_release(radio->videodev);
515 kfree(radio->transfer_buffer);
516 kfree(radio);
517 return -EIO;
519 usb_set_intfdata(intf, radio);
520 return 0;
523 static int __init dsbr100_init(void)
525 int retval = usb_register(&usb_dsbr100_driver);
526 printk(KERN_INFO KBUILD_MODNAME ": " DRIVER_VERSION ":"
527 DRIVER_DESC "\n");
528 return retval;
531 static void __exit dsbr100_exit(void)
533 usb_deregister(&usb_dsbr100_driver);
536 module_init (dsbr100_init);
537 module_exit (dsbr100_exit);
539 MODULE_AUTHOR( DRIVER_AUTHOR );
540 MODULE_DESCRIPTION( DRIVER_DESC );
541 MODULE_LICENSE("GPL");