2 * A driver for the AverMedia MR 800 USB FM radio. This device plugs
3 * into both the USB and an analog audio input, so this thing
4 * only deals with initialization and frequency setting, the
5 * audio data has to be handled by a sound driver.
7 * Copyright (c) 2008 Alexey Klimov <klimov.linux@gmail.com>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 * Big thanks to authors of dsbr100.c and radio-si470x.c
27 * When work was looked pretty good, i discover this:
28 * http://av-usbradio.sourceforge.net/index.php
29 * http://sourceforge.net/projects/av-usbradio/
30 * Latest release of theirs project was in 2005.
31 * Probably, this driver could be improved trough using their
32 * achievements (specifications given).
33 * So, we have smth to begin with.
36 * Version 0.01: First working version.
37 * It's required to blacklist AverMedia USB Radio
38 * in usbhid/hid-quirks.c
41 * - Correct power managment of device (suspend & resume)
42 * - Make x86 independance (little-endian and big-endian stuff)
43 * - Add code for scanning and smooth tuning
44 * - Checked and add stereo&mono stuff
45 * - Add code for sensitivity value
47 * - In Japan another FREQ_MIN and FREQ_MAX
51 #include <linux/kernel.h>
52 #include <linux/module.h>
53 #include <linux/init.h>
54 #include <linux/slab.h>
55 #include <linux/input.h>
56 #include <linux/videodev2.h>
57 #include <media/v4l2-common.h>
58 #include <media/v4l2-ioctl.h>
59 #include <linux/usb.h>
60 #include <linux/version.h> /* for KERNEL_VERSION MACRO */
62 /* driver and module definitions */
63 #define DRIVER_AUTHOR "Alexey Klimov <klimov.linux@gmail.com>"
64 #define DRIVER_DESC "AverMedia MR 800 USB FM radio driver"
65 #define DRIVER_VERSION "0.01"
66 #define RADIO_VERSION KERNEL_VERSION(0, 0, 1)
68 MODULE_AUTHOR(DRIVER_AUTHOR);
69 MODULE_DESCRIPTION(DRIVER_DESC);
70 MODULE_LICENSE("GPL");
72 #define USB_AMRADIO_VENDOR 0x07ca
73 #define USB_AMRADIO_PRODUCT 0xb800
75 /* dev_warn macro with driver name */
76 #define MR800_DRIVER_NAME "radio-mr800"
77 #define amradio_dev_warn(dev, fmt, arg...) \
78 dev_warn(dev, MR800_DRIVER_NAME " - " fmt, ##arg)
80 /* Probably USB_TIMEOUT should be modified in module parameter */
81 #define BUFFER_LENGTH 8
82 #define USB_TIMEOUT 500
84 /* Frequency limits in MHz -- these are European values. For Japanese
85 devices, that would be 76 and 91. */
87 #define FREQ_MAX 108.0
88 #define FREQ_MUL 16000
90 /* module parameter */
91 static int radio_nr = -1;
92 module_param(radio_nr, int, 0);
93 MODULE_PARM_DESC(radio_nr, "Radio Nr");
95 static struct v4l2_queryctrl radio_qctrl[] = {
97 .id = V4L2_CID_AUDIO_MUTE,
103 .type = V4L2_CTRL_TYPE_BOOLEAN,
105 /* HINT: the disabled controls are only here to satify kradio and such apps */
106 { .id = V4L2_CID_AUDIO_VOLUME,
107 .flags = V4L2_CTRL_FLAG_DISABLED,
110 .id = V4L2_CID_AUDIO_BALANCE,
111 .flags = V4L2_CTRL_FLAG_DISABLED,
114 .id = V4L2_CID_AUDIO_BASS,
115 .flags = V4L2_CTRL_FLAG_DISABLED,
118 .id = V4L2_CID_AUDIO_TREBLE,
119 .flags = V4L2_CTRL_FLAG_DISABLED,
122 .id = V4L2_CID_AUDIO_LOUDNESS,
123 .flags = V4L2_CTRL_FLAG_DISABLED,
127 static int usb_amradio_probe(struct usb_interface *intf,
128 const struct usb_device_id *id);
129 static void usb_amradio_disconnect(struct usb_interface *intf);
130 static int usb_amradio_open(struct inode *inode, struct file *file);
131 static int usb_amradio_close(struct inode *inode, struct file *file);
132 static int usb_amradio_suspend(struct usb_interface *intf,
133 pm_message_t message);
134 static int usb_amradio_resume(struct usb_interface *intf);
136 /* Data for one (physical) device */
137 struct amradio_device {
138 /* reference to USB and video device */
139 struct usb_device *usbdev;
140 struct video_device *videodev;
142 unsigned char *buffer;
143 struct mutex lock; /* buffer locking */
151 /* USB Device ID List */
152 static struct usb_device_id usb_amradio_device_table[] = {
153 {USB_DEVICE_AND_INTERFACE_INFO(USB_AMRADIO_VENDOR, USB_AMRADIO_PRODUCT,
154 USB_CLASS_HID, 0, 0) },
155 { } /* Terminating entry */
158 MODULE_DEVICE_TABLE(usb, usb_amradio_device_table);
160 /* USB subsystem interface */
161 static struct usb_driver usb_amradio_driver = {
162 .name = MR800_DRIVER_NAME,
163 .probe = usb_amradio_probe,
164 .disconnect = usb_amradio_disconnect,
165 .suspend = usb_amradio_suspend,
166 .resume = usb_amradio_resume,
167 .reset_resume = usb_amradio_resume,
168 .id_table = usb_amradio_device_table,
169 .supports_autosuspend = 1,
172 /* switch on radio. Send 8 bytes to device. */
173 static int amradio_start(struct amradio_device *radio)
178 mutex_lock(&radio->lock);
180 radio->buffer[0] = 0x00;
181 radio->buffer[1] = 0x55;
182 radio->buffer[2] = 0xaa;
183 radio->buffer[3] = 0x00;
184 radio->buffer[4] = 0xab;
185 radio->buffer[5] = 0x00;
186 radio->buffer[6] = 0x00;
187 radio->buffer[7] = 0x00;
189 retval = usb_bulk_msg(radio->usbdev, usb_sndintpipe(radio->usbdev, 2),
190 (void *) (radio->buffer), BUFFER_LENGTH, &size, USB_TIMEOUT);
193 mutex_unlock(&radio->lock);
197 mutex_unlock(&radio->lock);
204 /* switch off radio */
205 static int amradio_stop(struct amradio_device *radio)
210 mutex_lock(&radio->lock);
212 radio->buffer[0] = 0x00;
213 radio->buffer[1] = 0x55;
214 radio->buffer[2] = 0xaa;
215 radio->buffer[3] = 0x00;
216 radio->buffer[4] = 0xab;
217 radio->buffer[5] = 0x01;
218 radio->buffer[6] = 0x00;
219 radio->buffer[7] = 0x00;
221 retval = usb_bulk_msg(radio->usbdev, usb_sndintpipe(radio->usbdev, 2),
222 (void *) (radio->buffer), BUFFER_LENGTH, &size, USB_TIMEOUT);
225 mutex_unlock(&radio->lock);
229 mutex_unlock(&radio->lock);
236 /* set a frequency, freq is defined by v4l's TUNER_LOW, i.e. 1/16th kHz */
237 static int amradio_setfreq(struct amradio_device *radio, int freq)
241 unsigned short freq_send = 0x13 + (freq >> 3) / 25;
243 mutex_lock(&radio->lock);
245 radio->buffer[0] = 0x00;
246 radio->buffer[1] = 0x55;
247 radio->buffer[2] = 0xaa;
248 radio->buffer[3] = 0x03;
249 radio->buffer[4] = 0xa4;
250 radio->buffer[5] = 0x00;
251 radio->buffer[6] = 0x00;
252 radio->buffer[7] = 0x08;
254 retval = usb_bulk_msg(radio->usbdev, usb_sndintpipe(radio->usbdev, 2),
255 (void *) (radio->buffer), BUFFER_LENGTH, &size, USB_TIMEOUT);
258 mutex_unlock(&radio->lock);
262 /* frequency is calculated from freq_send and placed in first 2 bytes */
263 radio->buffer[0] = (freq_send >> 8) & 0xff;
264 radio->buffer[1] = freq_send & 0xff;
265 radio->buffer[2] = 0x01;
266 radio->buffer[3] = 0x00;
267 radio->buffer[4] = 0x00;
268 /* 5 and 6 bytes of buffer already = 0x00 */
269 radio->buffer[7] = 0x00;
271 retval = usb_bulk_msg(radio->usbdev, usb_sndintpipe(radio->usbdev, 2),
272 (void *) (radio->buffer), BUFFER_LENGTH, &size, USB_TIMEOUT);
275 mutex_unlock(&radio->lock);
279 mutex_unlock(&radio->lock);
286 /* USB subsystem interface begins here */
288 /* handle unplugging of the device, release data structures
289 if nothing keeps us from doing it. If something is still
290 keeping us busy, the release callback of v4l will take care
292 static void usb_amradio_disconnect(struct usb_interface *intf)
294 struct amradio_device *radio = usb_get_intfdata(intf);
296 usb_set_intfdata(intf, NULL);
299 video_unregister_device(radio->videodev);
300 radio->videodev = NULL;
302 kfree(radio->buffer);
310 /* vidioc_querycap - query device capabilities */
311 static int vidioc_querycap(struct file *file, void *priv,
312 struct v4l2_capability *v)
314 strlcpy(v->driver, "radio-mr800", sizeof(v->driver));
315 strlcpy(v->card, "AverMedia MR 800 USB FM Radio", sizeof(v->card));
316 sprintf(v->bus_info, "USB");
317 v->version = RADIO_VERSION;
318 v->capabilities = V4L2_CAP_TUNER;
322 /* vidioc_g_tuner - get tuner attributes */
323 static int vidioc_g_tuner(struct file *file, void *priv,
324 struct v4l2_tuner *v)
326 struct amradio_device *radio = video_get_drvdata(video_devdata(file));
331 /* TODO: Add function which look is signal stereo or not
332 * amradio_getstat(radio);
335 strcpy(v->name, "FM");
336 v->type = V4L2_TUNER_RADIO;
337 v->rangelow = FREQ_MIN * FREQ_MUL;
338 v->rangehigh = FREQ_MAX * FREQ_MUL;
339 v->rxsubchans = V4L2_TUNER_SUB_MONO | V4L2_TUNER_SUB_STEREO;
340 v->capability = V4L2_TUNER_CAP_LOW;
342 v->audmode = V4L2_TUNER_MODE_STEREO;
344 v->audmode = V4L2_TUNER_MODE_MONO;
345 v->signal = 0xffff; /* Can't get the signal strength, sad.. */
346 v->afc = 0; /* Don't know what is this */
350 /* vidioc_s_tuner - set tuner attributes */
351 static int vidioc_s_tuner(struct file *file, void *priv,
352 struct v4l2_tuner *v)
359 /* vidioc_s_frequency - set tuner radio frequency */
360 static int vidioc_s_frequency(struct file *file, void *priv,
361 struct v4l2_frequency *f)
363 struct amradio_device *radio = video_get_drvdata(video_devdata(file));
365 radio->curfreq = f->frequency;
366 if (amradio_setfreq(radio, radio->curfreq) < 0)
367 amradio_dev_warn(&radio->videodev->dev,
368 "set frequency failed\n");
372 /* vidioc_g_frequency - get tuner radio frequency */
373 static int vidioc_g_frequency(struct file *file, void *priv,
374 struct v4l2_frequency *f)
376 struct amradio_device *radio = video_get_drvdata(video_devdata(file));
378 f->type = V4L2_TUNER_RADIO;
379 f->frequency = radio->curfreq;
383 /* vidioc_queryctrl - enumerate control items */
384 static int vidioc_queryctrl(struct file *file, void *priv,
385 struct v4l2_queryctrl *qc)
389 for (i = 0; i < ARRAY_SIZE(radio_qctrl); i++) {
390 if (qc->id && qc->id == radio_qctrl[i].id) {
391 memcpy(qc, &(radio_qctrl[i]), sizeof(*qc));
398 /* vidioc_g_ctrl - get the value of a control */
399 static int vidioc_g_ctrl(struct file *file, void *priv,
400 struct v4l2_control *ctrl)
402 struct amradio_device *radio = video_get_drvdata(video_devdata(file));
405 case V4L2_CID_AUDIO_MUTE:
406 ctrl->value = radio->muted;
412 /* vidioc_s_ctrl - set the value of a control */
413 static int vidioc_s_ctrl(struct file *file, void *priv,
414 struct v4l2_control *ctrl)
416 struct amradio_device *radio = video_get_drvdata(video_devdata(file));
419 case V4L2_CID_AUDIO_MUTE:
421 if (amradio_stop(radio) < 0) {
422 amradio_dev_warn(&radio->videodev->dev,
423 "amradio_stop failed\n");
427 if (amradio_start(radio) < 0) {
428 amradio_dev_warn(&radio->videodev->dev,
429 "amradio_start failed\n");
438 /* vidioc_g_audio - get audio attributes */
439 static int vidioc_g_audio(struct file *file, void *priv,
440 struct v4l2_audio *a)
445 strcpy(a->name, "Radio");
446 a->capability = V4L2_AUDCAP_STEREO;
450 /* vidioc_s_audio - set audio attributes */
451 static int vidioc_s_audio(struct file *file, void *priv,
452 struct v4l2_audio *a)
459 /* vidioc_g_input - get input */
460 static int vidioc_g_input(struct file *filp, void *priv, unsigned int *i)
466 /* vidioc_s_input - set input */
467 static int vidioc_s_input(struct file *filp, void *priv, unsigned int i)
474 /* open device - amradio_start() and amradio_setfreq() */
475 static int usb_amradio_open(struct inode *inode, struct file *file)
477 struct amradio_device *radio = video_get_drvdata(video_devdata(file));
484 if (amradio_start(radio) < 0) {
485 amradio_dev_warn(&radio->videodev->dev,
486 "radio did not start up properly\n");
491 if (amradio_setfreq(radio, radio->curfreq) < 0)
492 amradio_dev_warn(&radio->videodev->dev,
493 "set frequency failed\n");
499 /*close device - free driver structures */
500 static int usb_amradio_close(struct inode *inode, struct file *file)
502 struct amradio_device *radio = video_get_drvdata(video_devdata(file));
507 if (radio->removed) {
508 kfree(radio->buffer);
514 /* Suspend device - stop device. Need to be checked and fixed */
515 static int usb_amradio_suspend(struct usb_interface *intf, pm_message_t message)
517 struct amradio_device *radio = usb_get_intfdata(intf);
519 if (amradio_stop(radio) < 0)
520 dev_warn(&intf->dev, "amradio_stop failed\n");
522 dev_info(&intf->dev, "going into suspend..\n");
527 /* Resume device - start device. Need to be checked and fixed */
528 static int usb_amradio_resume(struct usb_interface *intf)
530 struct amradio_device *radio = usb_get_intfdata(intf);
532 if (amradio_start(radio) < 0)
533 dev_warn(&intf->dev, "amradio_start failed\n");
535 dev_info(&intf->dev, "coming out of suspend..\n");
540 /* File system interface */
541 static const struct file_operations usb_amradio_fops = {
542 .owner = THIS_MODULE,
543 .open = usb_amradio_open,
544 .release = usb_amradio_close,
545 .ioctl = video_ioctl2,
547 .compat_ioctl = v4l_compat_ioctl32,
552 static const struct v4l2_ioctl_ops usb_amradio_ioctl_ops = {
553 .vidioc_querycap = vidioc_querycap,
554 .vidioc_g_tuner = vidioc_g_tuner,
555 .vidioc_s_tuner = vidioc_s_tuner,
556 .vidioc_g_frequency = vidioc_g_frequency,
557 .vidioc_s_frequency = vidioc_s_frequency,
558 .vidioc_queryctrl = vidioc_queryctrl,
559 .vidioc_g_ctrl = vidioc_g_ctrl,
560 .vidioc_s_ctrl = vidioc_s_ctrl,
561 .vidioc_g_audio = vidioc_g_audio,
562 .vidioc_s_audio = vidioc_s_audio,
563 .vidioc_g_input = vidioc_g_input,
564 .vidioc_s_input = vidioc_s_input,
568 static struct video_device amradio_videodev_template = {
569 .name = "AverMedia MR 800 USB FM Radio",
570 .fops = &usb_amradio_fops,
571 .ioctl_ops = &usb_amradio_ioctl_ops,
572 .release = video_device_release,
575 /* check if the device is present and register with v4l and
577 static int usb_amradio_probe(struct usb_interface *intf,
578 const struct usb_device_id *id)
580 struct amradio_device *radio;
582 radio = kmalloc(sizeof(struct amradio_device), GFP_KERNEL);
587 radio->buffer = kmalloc(BUFFER_LENGTH, GFP_KERNEL);
589 if (!(radio->buffer)) {
594 radio->videodev = video_device_alloc();
596 if (!(radio->videodev)) {
597 kfree(radio->buffer);
602 memcpy(radio->videodev, &amradio_videodev_template,
603 sizeof(amradio_videodev_template));
607 radio->usbdev = interface_to_usbdev(intf);
608 radio->curfreq = 95.16 * FREQ_MUL;
610 mutex_init(&radio->lock);
612 video_set_drvdata(radio->videodev, radio);
613 if (video_register_device(radio->videodev, VFL_TYPE_RADIO, radio_nr)) {
614 dev_warn(&intf->dev, "could not register video device\n");
615 video_device_release(radio->videodev);
616 kfree(radio->buffer);
621 usb_set_intfdata(intf, radio);
625 static int __init amradio_init(void)
627 int retval = usb_register(&usb_amradio_driver);
629 pr_info(KBUILD_MODNAME
630 ": version " DRIVER_VERSION " " DRIVER_DESC "\n");
633 pr_err(KBUILD_MODNAME
634 ": usb_register failed. Error number %d\n", retval);
639 static void __exit amradio_exit(void)
641 usb_deregister(&usb_amradio_driver);
644 module_init(amradio_init);
645 module_exit(amradio_exit);