1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * A driver for the AverMedia MR 800 USB FM radio. This device plugs
4 * into both the USB and an analog audio input, so this thing
5 * only deals with initialization and frequency setting, the
6 * audio data has to be handled by a sound driver.
8 * Copyright (c) 2008 Alexey Klimov <klimov.linux@gmail.com>
12 * Big thanks to authors and contributors of dsbr100.c and radio-si470x.c
14 * When work was looked pretty good, i discover this:
15 * http://av-usbradio.sourceforge.net/index.php
16 * http://sourceforge.net/projects/av-usbradio/
17 * Latest release of theirs project was in 2005.
18 * Probably, this driver could be improved through using their
19 * achievements (specifications given).
20 * Also, Faidon Liambotis <paravoid@debian.org> wrote nice driver for this radio
21 * in 2007. He allowed to use his driver to improve current mr800 radio driver.
22 * http://www.spinics.net/lists/linux-usb-devel/msg10109.html
24 * Version 0.01: First working version.
25 * It's required to blacklist AverMedia USB Radio
26 * in usbhid/hid-quirks.c
27 * Version 0.10: A lot of cleanups and fixes: unpluging the device,
28 * few mutex locks were added, codinstyle issues, etc.
29 * Added stereo support. Thanks to
30 * Douglas Schilling Landgraf <dougsland@gmail.com> and
31 * David Ellingsworth <david@identd.dyndns.org>
32 * for discussion, help and support.
33 * Version 0.11: Converted to v4l2_device.
36 * - Correct power management of device (suspend & resume)
37 * - Add code for scanning and smooth tuning
38 * - Add code for sensitivity value
40 * - In Japan another FREQ_MIN and FREQ_MAX
44 #include <linux/kernel.h>
45 #include <linux/module.h>
46 #include <linux/init.h>
47 #include <linux/slab.h>
48 #include <linux/input.h>
49 #include <linux/videodev2.h>
50 #include <media/v4l2-device.h>
51 #include <media/v4l2-ioctl.h>
52 #include <media/v4l2-ctrls.h>
53 #include <media/v4l2-event.h>
54 #include <linux/usb.h>
55 #include <linux/mutex.h>
57 /* driver and module definitions */
58 #define DRIVER_AUTHOR "Alexey Klimov <klimov.linux@gmail.com>"
59 #define DRIVER_DESC "AverMedia MR 800 USB FM radio driver"
60 #define DRIVER_VERSION "0.1.2"
62 MODULE_AUTHOR(DRIVER_AUTHOR
);
63 MODULE_DESCRIPTION(DRIVER_DESC
);
64 MODULE_LICENSE("GPL");
65 MODULE_VERSION(DRIVER_VERSION
);
67 #define USB_AMRADIO_VENDOR 0x07ca
68 #define USB_AMRADIO_PRODUCT 0xb800
70 /* dev_warn macro with driver name */
71 #define MR800_DRIVER_NAME "radio-mr800"
72 #define amradio_dev_warn(dev, fmt, arg...) \
73 dev_warn(dev, MR800_DRIVER_NAME " - " fmt, ##arg)
75 #define amradio_dev_err(dev, fmt, arg...) \
76 dev_err(dev, MR800_DRIVER_NAME " - " fmt, ##arg)
78 /* Probably USB_TIMEOUT should be modified in module parameter */
79 #define BUFFER_LENGTH 8
80 #define USB_TIMEOUT 500
82 /* Frequency limits in MHz -- these are European values. For Japanese
83 devices, that would be 76 and 91. */
85 #define FREQ_MAX 108.0
86 #define FREQ_MUL 16000
89 * Commands that device should understand
90 * List isn't full and will be updated with implementation of new functions
92 #define AMRADIO_SET_FREQ 0xa4
93 #define AMRADIO_GET_READY_FLAG 0xa5
94 #define AMRADIO_GET_SIGNAL 0xa7
95 #define AMRADIO_GET_FREQ 0xa8
96 #define AMRADIO_SET_SEARCH_UP 0xa9
97 #define AMRADIO_SET_SEARCH_DOWN 0xaa
98 #define AMRADIO_SET_MUTE 0xab
99 #define AMRADIO_SET_RIGHT_MUTE 0xac
100 #define AMRADIO_SET_LEFT_MUTE 0xad
101 #define AMRADIO_SET_MONO 0xae
102 #define AMRADIO_SET_SEARCH_LVL 0xb0
103 #define AMRADIO_STOP_SEARCH 0xb1
105 /* Comfortable defines for amradio_set_stereo */
106 #define WANT_STEREO 0x00
107 #define WANT_MONO 0x01
109 /* module parameter */
110 static int radio_nr
= -1;
111 module_param(radio_nr
, int, 0);
112 MODULE_PARM_DESC(radio_nr
, "Radio Nr");
114 /* Data for one (physical) device */
115 struct amradio_device
{
116 /* reference to USB and video device */
117 struct usb_device
*usbdev
;
118 struct usb_interface
*intf
;
119 struct video_device vdev
;
120 struct v4l2_device v4l2_dev
;
121 struct v4l2_ctrl_handler hdl
;
124 struct mutex lock
; /* buffer locking */
130 static inline struct amradio_device
*to_amradio_dev(struct v4l2_device
*v4l2_dev
)
132 return container_of(v4l2_dev
, struct amradio_device
, v4l2_dev
);
135 static int amradio_send_cmd(struct amradio_device
*radio
, u8 cmd
, u8 arg
,
136 u8
*extra
, u8 extralen
, bool reply
)
141 radio
->buffer
[0] = 0x00;
142 radio
->buffer
[1] = 0x55;
143 radio
->buffer
[2] = 0xaa;
144 radio
->buffer
[3] = extralen
;
145 radio
->buffer
[4] = cmd
;
146 radio
->buffer
[5] = arg
;
147 radio
->buffer
[6] = 0x00;
148 radio
->buffer
[7] = extra
|| reply
? 8 : 0;
150 retval
= usb_bulk_msg(radio
->usbdev
, usb_sndintpipe(radio
->usbdev
, 2),
151 radio
->buffer
, BUFFER_LENGTH
, &size
, USB_TIMEOUT
);
153 if (retval
< 0 || size
!= BUFFER_LENGTH
) {
154 if (video_is_registered(&radio
->vdev
))
155 amradio_dev_warn(&radio
->vdev
.dev
,
156 "cmd %02x failed\n", cmd
);
157 return retval
? retval
: -EIO
;
159 if (!extra
&& !reply
)
163 memcpy(radio
->buffer
, extra
, extralen
);
164 memset(radio
->buffer
+ extralen
, 0, 8 - extralen
);
165 retval
= usb_bulk_msg(radio
->usbdev
, usb_sndintpipe(radio
->usbdev
, 2),
166 radio
->buffer
, BUFFER_LENGTH
, &size
, USB_TIMEOUT
);
168 memset(radio
->buffer
, 0, 8);
169 retval
= usb_bulk_msg(radio
->usbdev
, usb_rcvbulkpipe(radio
->usbdev
, 0x81),
170 radio
->buffer
, BUFFER_LENGTH
, &size
, USB_TIMEOUT
);
172 if (retval
== 0 && size
== BUFFER_LENGTH
)
174 if (video_is_registered(&radio
->vdev
) && cmd
!= AMRADIO_GET_READY_FLAG
)
175 amradio_dev_warn(&radio
->vdev
.dev
, "follow-up to cmd %02x failed\n", cmd
);
176 return retval
? retval
: -EIO
;
179 /* switch on/off the radio. Send 8 bytes to device */
180 static int amradio_set_mute(struct amradio_device
*radio
, bool mute
)
182 int ret
= amradio_send_cmd(radio
,
183 AMRADIO_SET_MUTE
, mute
, NULL
, 0, false);
190 /* set a frequency, freq is defined by v4l's TUNER_LOW, i.e. 1/16th kHz */
191 static int amradio_set_freq(struct amradio_device
*radio
, int freq
)
193 unsigned short freq_send
;
197 /* we need to be sure that frequency isn't out of range */
198 freq
= clamp_t(unsigned, freq
, FREQ_MIN
* FREQ_MUL
, FREQ_MAX
* FREQ_MUL
);
199 freq_send
= 0x10 + (freq
>> 3) / 25;
201 /* frequency is calculated from freq_send and placed in first 2 bytes */
202 buf
[0] = (freq_send
>> 8) & 0xff;
203 buf
[1] = freq_send
& 0xff;
206 retval
= amradio_send_cmd(radio
, AMRADIO_SET_FREQ
, 0, buf
, 3, false);
209 radio
->curfreq
= freq
;
214 static int amradio_set_stereo(struct amradio_device
*radio
, bool stereo
)
216 int ret
= amradio_send_cmd(radio
,
217 AMRADIO_SET_MONO
, !stereo
, NULL
, 0, false);
220 radio
->stereo
= stereo
;
224 static int amradio_get_stat(struct amradio_device
*radio
, bool *is_stereo
, u32
*signal
)
226 int ret
= amradio_send_cmd(radio
,
227 AMRADIO_GET_SIGNAL
, 0, NULL
, 0, true);
231 *is_stereo
= radio
->buffer
[2] >> 7;
232 *signal
= (radio
->buffer
[3] & 0xf0) << 8;
236 /* Handle unplugging the device.
237 * We call video_unregister_device in any case.
238 * The last function called in this procedure is
239 * usb_amradio_device_release.
241 static void usb_amradio_disconnect(struct usb_interface
*intf
)
243 struct amradio_device
*radio
= to_amradio_dev(usb_get_intfdata(intf
));
245 mutex_lock(&radio
->lock
);
246 video_unregister_device(&radio
->vdev
);
247 amradio_set_mute(radio
, true);
248 usb_set_intfdata(intf
, NULL
);
249 v4l2_device_disconnect(&radio
->v4l2_dev
);
250 mutex_unlock(&radio
->lock
);
251 v4l2_device_put(&radio
->v4l2_dev
);
254 /* vidioc_querycap - query device capabilities */
255 static int vidioc_querycap(struct file
*file
, void *priv
,
256 struct v4l2_capability
*v
)
258 struct amradio_device
*radio
= video_drvdata(file
);
260 strscpy(v
->driver
, "radio-mr800", sizeof(v
->driver
));
261 strscpy(v
->card
, "AverMedia MR 800 USB FM Radio", sizeof(v
->card
));
262 usb_make_path(radio
->usbdev
, v
->bus_info
, sizeof(v
->bus_info
));
266 /* vidioc_g_tuner - get tuner attributes */
267 static int vidioc_g_tuner(struct file
*file
, void *priv
,
268 struct v4l2_tuner
*v
)
270 struct amradio_device
*radio
= video_drvdata(file
);
271 bool is_stereo
= false;
278 retval
= amradio_get_stat(radio
, &is_stereo
, &v
->signal
);
282 strscpy(v
->name
, "FM", sizeof(v
->name
));
283 v
->type
= V4L2_TUNER_RADIO
;
284 v
->rangelow
= FREQ_MIN
* FREQ_MUL
;
285 v
->rangehigh
= FREQ_MAX
* FREQ_MUL
;
286 v
->capability
= V4L2_TUNER_CAP_LOW
| V4L2_TUNER_CAP_STEREO
|
287 V4L2_TUNER_CAP_HWSEEK_WRAP
;
288 v
->rxsubchans
= is_stereo
? V4L2_TUNER_SUB_STEREO
: V4L2_TUNER_SUB_MONO
;
289 v
->audmode
= radio
->stereo
?
290 V4L2_TUNER_MODE_STEREO
: V4L2_TUNER_MODE_MONO
;
294 /* vidioc_s_tuner - set tuner attributes */
295 static int vidioc_s_tuner(struct file
*file
, void *priv
,
296 const struct v4l2_tuner
*v
)
298 struct amradio_device
*radio
= video_drvdata(file
);
303 /* mono/stereo selector */
304 switch (v
->audmode
) {
305 case V4L2_TUNER_MODE_MONO
:
306 return amradio_set_stereo(radio
, WANT_MONO
);
308 return amradio_set_stereo(radio
, WANT_STEREO
);
312 /* vidioc_s_frequency - set tuner radio frequency */
313 static int vidioc_s_frequency(struct file
*file
, void *priv
,
314 const struct v4l2_frequency
*f
)
316 struct amradio_device
*radio
= video_drvdata(file
);
320 return amradio_set_freq(radio
, f
->frequency
);
323 /* vidioc_g_frequency - get tuner radio frequency */
324 static int vidioc_g_frequency(struct file
*file
, void *priv
,
325 struct v4l2_frequency
*f
)
327 struct amradio_device
*radio
= video_drvdata(file
);
329 if (f
->tuner
!= 0 || f
->type
!= V4L2_TUNER_RADIO
)
331 f
->type
= V4L2_TUNER_RADIO
;
332 f
->frequency
= radio
->curfreq
;
337 static int vidioc_s_hw_freq_seek(struct file
*file
, void *priv
,
338 const struct v4l2_hw_freq_seek
*seek
)
341 0x3d, 0x32, 0x0f, 0x08, 0x3d, 0x32, 0x0f, 0x08
343 struct amradio_device
*radio
= video_drvdata(file
);
344 unsigned long timeout
;
347 if (seek
->tuner
!= 0 || !seek
->wrap_around
)
350 if (file
->f_flags
& O_NONBLOCK
)
353 retval
= amradio_send_cmd(radio
,
354 AMRADIO_SET_SEARCH_LVL
, 0, buf
, 8, false);
357 amradio_set_freq(radio
, radio
->curfreq
);
358 retval
= amradio_send_cmd(radio
,
359 seek
->seek_upward
? AMRADIO_SET_SEARCH_UP
: AMRADIO_SET_SEARCH_DOWN
,
363 timeout
= jiffies
+ msecs_to_jiffies(30000);
365 if (time_after(jiffies
, timeout
)) {
369 if (schedule_timeout_interruptible(msecs_to_jiffies(10))) {
370 retval
= -ERESTARTSYS
;
373 retval
= amradio_send_cmd(radio
, AMRADIO_GET_READY_FLAG
,
377 amradio_send_cmd(radio
, AMRADIO_GET_FREQ
, 0, NULL
, 0, true);
378 if (radio
->buffer
[1] || radio
->buffer
[2]) {
379 /* To check: sometimes radio->curfreq is set to out of range value */
380 radio
->curfreq
= (radio
->buffer
[1] << 8) | radio
->buffer
[2];
381 radio
->curfreq
= (radio
->curfreq
- 0x10) * 200;
382 amradio_send_cmd(radio
, AMRADIO_STOP_SEARCH
,
384 amradio_set_freq(radio
, radio
->curfreq
);
389 amradio_send_cmd(radio
, AMRADIO_STOP_SEARCH
, 0, NULL
, 0, false);
390 amradio_set_freq(radio
, radio
->curfreq
);
394 static int usb_amradio_s_ctrl(struct v4l2_ctrl
*ctrl
)
396 struct amradio_device
*radio
=
397 container_of(ctrl
->handler
, struct amradio_device
, hdl
);
400 case V4L2_CID_AUDIO_MUTE
:
401 return amradio_set_mute(radio
, ctrl
->val
);
407 static int usb_amradio_init(struct amradio_device
*radio
)
411 retval
= amradio_set_mute(radio
, true);
414 retval
= amradio_set_stereo(radio
, true);
417 retval
= amradio_set_freq(radio
, radio
->curfreq
);
423 amradio_dev_err(&radio
->vdev
.dev
, "initialization failed\n");
427 /* Suspend device - stop device. Need to be checked and fixed */
428 static int usb_amradio_suspend(struct usb_interface
*intf
, pm_message_t message
)
430 struct amradio_device
*radio
= to_amradio_dev(usb_get_intfdata(intf
));
432 mutex_lock(&radio
->lock
);
434 amradio_set_mute(radio
, true);
435 radio
->muted
= false;
437 mutex_unlock(&radio
->lock
);
439 dev_info(&intf
->dev
, "going into suspend..\n");
443 /* Resume device - start device. Need to be checked and fixed */
444 static int usb_amradio_resume(struct usb_interface
*intf
)
446 struct amradio_device
*radio
= to_amradio_dev(usb_get_intfdata(intf
));
448 mutex_lock(&radio
->lock
);
449 amradio_set_stereo(radio
, radio
->stereo
);
450 amradio_set_freq(radio
, radio
->curfreq
);
453 amradio_set_mute(radio
, false);
455 mutex_unlock(&radio
->lock
);
457 dev_info(&intf
->dev
, "coming out of suspend..\n");
461 static const struct v4l2_ctrl_ops usb_amradio_ctrl_ops
= {
462 .s_ctrl
= usb_amradio_s_ctrl
,
465 /* File system interface */
466 static const struct v4l2_file_operations usb_amradio_fops
= {
467 .owner
= THIS_MODULE
,
468 .open
= v4l2_fh_open
,
469 .release
= v4l2_fh_release
,
470 .poll
= v4l2_ctrl_poll
,
471 .unlocked_ioctl
= video_ioctl2
,
474 static const struct v4l2_ioctl_ops usb_amradio_ioctl_ops
= {
475 .vidioc_querycap
= vidioc_querycap
,
476 .vidioc_g_tuner
= vidioc_g_tuner
,
477 .vidioc_s_tuner
= vidioc_s_tuner
,
478 .vidioc_g_frequency
= vidioc_g_frequency
,
479 .vidioc_s_frequency
= vidioc_s_frequency
,
480 .vidioc_s_hw_freq_seek
= vidioc_s_hw_freq_seek
,
481 .vidioc_log_status
= v4l2_ctrl_log_status
,
482 .vidioc_subscribe_event
= v4l2_ctrl_subscribe_event
,
483 .vidioc_unsubscribe_event
= v4l2_event_unsubscribe
,
486 static void usb_amradio_release(struct v4l2_device
*v4l2_dev
)
488 struct amradio_device
*radio
= to_amradio_dev(v4l2_dev
);
490 /* free rest memory */
491 v4l2_ctrl_handler_free(&radio
->hdl
);
492 v4l2_device_unregister(&radio
->v4l2_dev
);
493 kfree(radio
->buffer
);
497 /* check if the device is present and register with v4l and usb if it is */
498 static int usb_amradio_probe(struct usb_interface
*intf
,
499 const struct usb_device_id
*id
)
501 struct amradio_device
*radio
;
504 radio
= kzalloc(sizeof(struct amradio_device
), GFP_KERNEL
);
507 dev_err(&intf
->dev
, "kmalloc for amradio_device failed\n");
512 radio
->buffer
= kmalloc(BUFFER_LENGTH
, GFP_KERNEL
);
514 if (!radio
->buffer
) {
515 dev_err(&intf
->dev
, "kmalloc for radio->buffer failed\n");
520 retval
= v4l2_device_register(&intf
->dev
, &radio
->v4l2_dev
);
522 dev_err(&intf
->dev
, "couldn't register v4l2_device\n");
526 v4l2_ctrl_handler_init(&radio
->hdl
, 1);
527 v4l2_ctrl_new_std(&radio
->hdl
, &usb_amradio_ctrl_ops
,
528 V4L2_CID_AUDIO_MUTE
, 0, 1, 1, 1);
529 if (radio
->hdl
.error
) {
530 retval
= radio
->hdl
.error
;
531 dev_err(&intf
->dev
, "couldn't register control\n");
534 mutex_init(&radio
->lock
);
536 radio
->v4l2_dev
.ctrl_handler
= &radio
->hdl
;
537 radio
->v4l2_dev
.release
= usb_amradio_release
;
538 strscpy(radio
->vdev
.name
, radio
->v4l2_dev
.name
,
539 sizeof(radio
->vdev
.name
));
540 radio
->vdev
.v4l2_dev
= &radio
->v4l2_dev
;
541 radio
->vdev
.fops
= &usb_amradio_fops
;
542 radio
->vdev
.ioctl_ops
= &usb_amradio_ioctl_ops
;
543 radio
->vdev
.release
= video_device_release_empty
;
544 radio
->vdev
.lock
= &radio
->lock
;
545 radio
->vdev
.device_caps
= V4L2_CAP_RADIO
| V4L2_CAP_TUNER
|
546 V4L2_CAP_HW_FREQ_SEEK
;
548 radio
->usbdev
= interface_to_usbdev(intf
);
550 usb_set_intfdata(intf
, &radio
->v4l2_dev
);
551 radio
->curfreq
= 95.16 * FREQ_MUL
;
553 video_set_drvdata(&radio
->vdev
, radio
);
554 retval
= usb_amradio_init(radio
);
558 retval
= video_register_device(&radio
->vdev
, VFL_TYPE_RADIO
,
561 dev_err(&intf
->dev
, "could not register video device\n");
568 v4l2_ctrl_handler_free(&radio
->hdl
);
570 v4l2_device_unregister(&radio
->v4l2_dev
);
572 kfree(radio
->buffer
);
579 /* USB Device ID List */
580 static const struct usb_device_id usb_amradio_device_table
[] = {
581 { USB_DEVICE_AND_INTERFACE_INFO(USB_AMRADIO_VENDOR
, USB_AMRADIO_PRODUCT
,
582 USB_CLASS_HID
, 0, 0) },
583 { } /* Terminating entry */
586 MODULE_DEVICE_TABLE(usb
, usb_amradio_device_table
);
588 /* USB subsystem interface */
589 static struct usb_driver usb_amradio_driver
= {
590 .name
= MR800_DRIVER_NAME
,
591 .probe
= usb_amradio_probe
,
592 .disconnect
= usb_amradio_disconnect
,
593 .suspend
= usb_amradio_suspend
,
594 .resume
= usb_amradio_resume
,
595 .reset_resume
= usb_amradio_resume
,
596 .id_table
= usb_amradio_device_table
,
599 module_usb_driver(usb_amradio_driver
);