1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
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.
7  *
8  * Copyright (c) 2008 Alexey Klimov <klimov.linux@gmail.com>
9  */
10 
11 /*
12  * Big thanks to authors and contributors of dsbr100.c and radio-si470x.c
13  *
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
23  *
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.
34  *
35  * Many things to do:
36  *	- Correct power management of device (suspend & resume)
37  *	- Add code for scanning and smooth tuning
38  *	- Add code for sensitivity value
39  *	- Correct mistakes
40  *	- In Japan another FREQ_MIN and FREQ_MAX
41  */
42 
43 /* kernel includes */
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>
56 
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"
61 
62 MODULE_AUTHOR(DRIVER_AUTHOR);
63 MODULE_DESCRIPTION(DRIVER_DESC);
64 MODULE_LICENSE("GPL");
65 MODULE_VERSION(DRIVER_VERSION);
66 
67 #define USB_AMRADIO_VENDOR 0x07ca
68 #define USB_AMRADIO_PRODUCT 0xb800
69 
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)
74 
75 #define amradio_dev_err(dev, fmt, arg...) \
76 		dev_err(dev, MR800_DRIVER_NAME " - " fmt, ##arg)
77 
78 /* Probably USB_TIMEOUT should be modified in module parameter */
79 #define BUFFER_LENGTH 8
80 #define USB_TIMEOUT 500
81 
82 /* Frequency limits in MHz -- these are European values.  For Japanese
83 devices, that would be 76 and 91.  */
84 #define FREQ_MIN  87.5
85 #define FREQ_MAX 108.0
86 #define FREQ_MUL 16000
87 
88 /*
89  * Commands that device should understand
90  * List isn't full and will be updated with implementation of new functions
91  */
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
104 
105 /* Comfortable defines for amradio_set_stereo */
106 #define WANT_STEREO		0x00
107 #define WANT_MONO		0x01
108 
109 /* module parameter */
110 static int radio_nr = -1;
111 module_param(radio_nr, int, 0);
112 MODULE_PARM_DESC(radio_nr, "Radio Nr");
113 
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;
122 
123 	u8 *buffer;
124 	struct mutex lock;	/* buffer locking */
125 	int curfreq;
126 	int stereo;
127 	int muted;
128 };
129 
130 static inline struct amradio_device *to_amradio_dev(struct v4l2_device *v4l2_dev)
131 {
132 	return container_of(v4l2_dev, struct amradio_device, v4l2_dev);
133 }
134 
135 static int amradio_send_cmd(struct amradio_device *radio, u8 cmd, u8 arg,
136 		u8 *extra, u8 extralen, bool reply)
137 {
138 	int retval;
139 	int size;
140 
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;
149 
150 	retval = usb_bulk_msg(radio->usbdev, usb_sndintpipe(radio->usbdev, 2),
151 		radio->buffer, BUFFER_LENGTH, &size, USB_TIMEOUT);
152 
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;
158 	}
159 	if (!extra && !reply)
160 		return 0;
161 
162 	if (extra) {
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);
167 	} else {
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);
171 	}
172 	if (retval == 0 && size == BUFFER_LENGTH)
173 		return 0;
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;
177 }
178 
179 /* switch on/off the radio. Send 8 bytes to device */
180 static int amradio_set_mute(struct amradio_device *radio, bool mute)
181 {
182 	int ret = amradio_send_cmd(radio,
183 			AMRADIO_SET_MUTE, mute, NULL, 0, false);
184 
185 	if (!ret)
186 		radio->muted = mute;
187 	return ret;
188 }
189 
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)
192 {
193 	unsigned short freq_send;
194 	u8 buf[3];
195 	int retval;
196 
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;
200 
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;
204 	buf[2] = 0x01;
205 
206 	retval = amradio_send_cmd(radio, AMRADIO_SET_FREQ, 0, buf, 3, false);
207 	if (retval)
208 		return retval;
209 	radio->curfreq = freq;
210 	msleep(40);
211 	return 0;
212 }
213 
214 static int amradio_set_stereo(struct amradio_device *radio, bool stereo)
215 {
216 	int ret = amradio_send_cmd(radio,
217 			AMRADIO_SET_MONO, !stereo, NULL, 0, false);
218 
219 	if (!ret)
220 		radio->stereo = stereo;
221 	return ret;
222 }
223 
224 static int amradio_get_stat(struct amradio_device *radio, bool *is_stereo, u32 *signal)
225 {
226 	int ret = amradio_send_cmd(radio,
227 			AMRADIO_GET_SIGNAL, 0, NULL, 0, true);
228 
229 	if (ret)
230 		return ret;
231 	*is_stereo = radio->buffer[2] >> 7;
232 	*signal = (radio->buffer[3] & 0xf0) << 8;
233 	return 0;
234 }
235 
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.
240  */
241 static void usb_amradio_disconnect(struct usb_interface *intf)
242 {
243 	struct amradio_device *radio = to_amradio_dev(usb_get_intfdata(intf));
244 
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);
252 }
253 
254 /* vidioc_querycap - query device capabilities */
255 static int vidioc_querycap(struct file *file, void *priv,
256 					struct v4l2_capability *v)
257 {
258 	struct amradio_device *radio = video_drvdata(file);
259 
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));
263 	v->device_caps = V4L2_CAP_RADIO | V4L2_CAP_TUNER |
264 					V4L2_CAP_HW_FREQ_SEEK;
265 	v->capabilities = v->device_caps | V4L2_CAP_DEVICE_CAPS;
266 	return 0;
267 }
268 
269 /* vidioc_g_tuner - get tuner attributes */
270 static int vidioc_g_tuner(struct file *file, void *priv,
271 				struct v4l2_tuner *v)
272 {
273 	struct amradio_device *radio = video_drvdata(file);
274 	bool is_stereo = false;
275 	int retval;
276 
277 	if (v->index > 0)
278 		return -EINVAL;
279 
280 	v->signal = 0;
281 	retval = amradio_get_stat(radio, &is_stereo, &v->signal);
282 	if (retval)
283 		return retval;
284 
285 	strscpy(v->name, "FM", sizeof(v->name));
286 	v->type = V4L2_TUNER_RADIO;
287 	v->rangelow = FREQ_MIN * FREQ_MUL;
288 	v->rangehigh = FREQ_MAX * FREQ_MUL;
289 	v->capability = V4L2_TUNER_CAP_LOW | V4L2_TUNER_CAP_STEREO |
290 		V4L2_TUNER_CAP_HWSEEK_WRAP;
291 	v->rxsubchans = is_stereo ? V4L2_TUNER_SUB_STEREO : V4L2_TUNER_SUB_MONO;
292 	v->audmode = radio->stereo ?
293 		V4L2_TUNER_MODE_STEREO : V4L2_TUNER_MODE_MONO;
294 	return 0;
295 }
296 
297 /* vidioc_s_tuner - set tuner attributes */
298 static int vidioc_s_tuner(struct file *file, void *priv,
299 				const struct v4l2_tuner *v)
300 {
301 	struct amradio_device *radio = video_drvdata(file);
302 
303 	if (v->index > 0)
304 		return -EINVAL;
305 
306 	/* mono/stereo selector */
307 	switch (v->audmode) {
308 	case V4L2_TUNER_MODE_MONO:
309 		return amradio_set_stereo(radio, WANT_MONO);
310 	default:
311 		return amradio_set_stereo(radio, WANT_STEREO);
312 	}
313 }
314 
315 /* vidioc_s_frequency - set tuner radio frequency */
316 static int vidioc_s_frequency(struct file *file, void *priv,
317 				const struct v4l2_frequency *f)
318 {
319 	struct amradio_device *radio = video_drvdata(file);
320 
321 	if (f->tuner != 0)
322 		return -EINVAL;
323 	return amradio_set_freq(radio, f->frequency);
324 }
325 
326 /* vidioc_g_frequency - get tuner radio frequency */
327 static int vidioc_g_frequency(struct file *file, void *priv,
328 				struct v4l2_frequency *f)
329 {
330 	struct amradio_device *radio = video_drvdata(file);
331 
332 	if (f->tuner != 0 || f->type != V4L2_TUNER_RADIO)
333 		return -EINVAL;
334 	f->type = V4L2_TUNER_RADIO;
335 	f->frequency = radio->curfreq;
336 
337 	return 0;
338 }
339 
340 static int vidioc_s_hw_freq_seek(struct file *file, void *priv,
341 		const struct v4l2_hw_freq_seek *seek)
342 {
343 	static u8 buf[8] = {
344 		0x3d, 0x32, 0x0f, 0x08, 0x3d, 0x32, 0x0f, 0x08
345 	};
346 	struct amradio_device *radio = video_drvdata(file);
347 	unsigned long timeout;
348 	int retval;
349 
350 	if (seek->tuner != 0 || !seek->wrap_around)
351 		return -EINVAL;
352 
353 	if (file->f_flags & O_NONBLOCK)
354 		return -EWOULDBLOCK;
355 
356 	retval = amradio_send_cmd(radio,
357 			AMRADIO_SET_SEARCH_LVL, 0, buf, 8, false);
358 	if (retval)
359 		return retval;
360 	amradio_set_freq(radio, radio->curfreq);
361 	retval = amradio_send_cmd(radio,
362 		seek->seek_upward ? AMRADIO_SET_SEARCH_UP : AMRADIO_SET_SEARCH_DOWN,
363 		0, NULL, 0, false);
364 	if (retval)
365 		return retval;
366 	timeout = jiffies + msecs_to_jiffies(30000);
367 	for (;;) {
368 		if (time_after(jiffies, timeout)) {
369 			retval = -ENODATA;
370 			break;
371 		}
372 		if (schedule_timeout_interruptible(msecs_to_jiffies(10))) {
373 			retval = -ERESTARTSYS;
374 			break;
375 		}
376 		retval = amradio_send_cmd(radio, AMRADIO_GET_READY_FLAG,
377 				0, NULL, 0, true);
378 		if (retval)
379 			continue;
380 		amradio_send_cmd(radio, AMRADIO_GET_FREQ, 0, NULL, 0, true);
381 		if (radio->buffer[1] || radio->buffer[2]) {
382 			/* To check: sometimes radio->curfreq is set to out of range value */
383 			radio->curfreq = (radio->buffer[1] << 8) | radio->buffer[2];
384 			radio->curfreq = (radio->curfreq - 0x10) * 200;
385 			amradio_send_cmd(radio, AMRADIO_STOP_SEARCH,
386 					0, NULL, 0, false);
387 			amradio_set_freq(radio, radio->curfreq);
388 			retval = 0;
389 			break;
390 		}
391 	}
392 	amradio_send_cmd(radio, AMRADIO_STOP_SEARCH, 0, NULL, 0, false);
393 	amradio_set_freq(radio, radio->curfreq);
394 	return retval;
395 }
396 
397 static int usb_amradio_s_ctrl(struct v4l2_ctrl *ctrl)
398 {
399 	struct amradio_device *radio =
400 		container_of(ctrl->handler, struct amradio_device, hdl);
401 
402 	switch (ctrl->id) {
403 	case V4L2_CID_AUDIO_MUTE:
404 		return amradio_set_mute(radio, ctrl->val);
405 	}
406 
407 	return -EINVAL;
408 }
409 
410 static int usb_amradio_init(struct amradio_device *radio)
411 {
412 	int retval;
413 
414 	retval = amradio_set_mute(radio, true);
415 	if (retval)
416 		goto out_err;
417 	retval = amradio_set_stereo(radio, true);
418 	if (retval)
419 		goto out_err;
420 	retval = amradio_set_freq(radio, radio->curfreq);
421 	if (retval)
422 		goto out_err;
423 	return 0;
424 
425 out_err:
426 	amradio_dev_err(&radio->vdev.dev, "initialization failed\n");
427 	return retval;
428 }
429 
430 /* Suspend device - stop device. Need to be checked and fixed */
431 static int usb_amradio_suspend(struct usb_interface *intf, pm_message_t message)
432 {
433 	struct amradio_device *radio = to_amradio_dev(usb_get_intfdata(intf));
434 
435 	mutex_lock(&radio->lock);
436 	if (!radio->muted) {
437 		amradio_set_mute(radio, true);
438 		radio->muted = false;
439 	}
440 	mutex_unlock(&radio->lock);
441 
442 	dev_info(&intf->dev, "going into suspend..\n");
443 	return 0;
444 }
445 
446 /* Resume device - start device. Need to be checked and fixed */
447 static int usb_amradio_resume(struct usb_interface *intf)
448 {
449 	struct amradio_device *radio = to_amradio_dev(usb_get_intfdata(intf));
450 
451 	mutex_lock(&radio->lock);
452 	amradio_set_stereo(radio, radio->stereo);
453 	amradio_set_freq(radio, radio->curfreq);
454 
455 	if (!radio->muted)
456 		amradio_set_mute(radio, false);
457 
458 	mutex_unlock(&radio->lock);
459 
460 	dev_info(&intf->dev, "coming out of suspend..\n");
461 	return 0;
462 }
463 
464 static const struct v4l2_ctrl_ops usb_amradio_ctrl_ops = {
465 	.s_ctrl = usb_amradio_s_ctrl,
466 };
467 
468 /* File system interface */
469 static const struct v4l2_file_operations usb_amradio_fops = {
470 	.owner		= THIS_MODULE,
471 	.open		= v4l2_fh_open,
472 	.release	= v4l2_fh_release,
473 	.poll		= v4l2_ctrl_poll,
474 	.unlocked_ioctl	= video_ioctl2,
475 };
476 
477 static const struct v4l2_ioctl_ops usb_amradio_ioctl_ops = {
478 	.vidioc_querycap    = vidioc_querycap,
479 	.vidioc_g_tuner     = vidioc_g_tuner,
480 	.vidioc_s_tuner     = vidioc_s_tuner,
481 	.vidioc_g_frequency = vidioc_g_frequency,
482 	.vidioc_s_frequency = vidioc_s_frequency,
483 	.vidioc_s_hw_freq_seek = vidioc_s_hw_freq_seek,
484 	.vidioc_log_status  = v4l2_ctrl_log_status,
485 	.vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
486 	.vidioc_unsubscribe_event = v4l2_event_unsubscribe,
487 };
488 
489 static void usb_amradio_release(struct v4l2_device *v4l2_dev)
490 {
491 	struct amradio_device *radio = to_amradio_dev(v4l2_dev);
492 
493 	/* free rest memory */
494 	v4l2_ctrl_handler_free(&radio->hdl);
495 	v4l2_device_unregister(&radio->v4l2_dev);
496 	kfree(radio->buffer);
497 	kfree(radio);
498 }
499 
500 /* check if the device is present and register with v4l and usb if it is */
501 static int usb_amradio_probe(struct usb_interface *intf,
502 				const struct usb_device_id *id)
503 {
504 	struct amradio_device *radio;
505 	int retval;
506 
507 	radio = kzalloc(sizeof(struct amradio_device), GFP_KERNEL);
508 
509 	if (!radio) {
510 		dev_err(&intf->dev, "kmalloc for amradio_device failed\n");
511 		retval = -ENOMEM;
512 		goto err;
513 	}
514 
515 	radio->buffer = kmalloc(BUFFER_LENGTH, GFP_KERNEL);
516 
517 	if (!radio->buffer) {
518 		dev_err(&intf->dev, "kmalloc for radio->buffer failed\n");
519 		retval = -ENOMEM;
520 		goto err_nobuf;
521 	}
522 
523 	retval = v4l2_device_register(&intf->dev, &radio->v4l2_dev);
524 	if (retval < 0) {
525 		dev_err(&intf->dev, "couldn't register v4l2_device\n");
526 		goto err_v4l2;
527 	}
528 
529 	v4l2_ctrl_handler_init(&radio->hdl, 1);
530 	v4l2_ctrl_new_std(&radio->hdl, &usb_amradio_ctrl_ops,
531 			  V4L2_CID_AUDIO_MUTE, 0, 1, 1, 1);
532 	if (radio->hdl.error) {
533 		retval = radio->hdl.error;
534 		dev_err(&intf->dev, "couldn't register control\n");
535 		goto err_ctrl;
536 	}
537 	mutex_init(&radio->lock);
538 
539 	radio->v4l2_dev.ctrl_handler = &radio->hdl;
540 	radio->v4l2_dev.release = usb_amradio_release;
541 	strscpy(radio->vdev.name, radio->v4l2_dev.name,
542 		sizeof(radio->vdev.name));
543 	radio->vdev.v4l2_dev = &radio->v4l2_dev;
544 	radio->vdev.fops = &usb_amradio_fops;
545 	radio->vdev.ioctl_ops = &usb_amradio_ioctl_ops;
546 	radio->vdev.release = video_device_release_empty;
547 	radio->vdev.lock = &radio->lock;
548 
549 	radio->usbdev = interface_to_usbdev(intf);
550 	radio->intf = intf;
551 	usb_set_intfdata(intf, &radio->v4l2_dev);
552 	radio->curfreq = 95.16 * FREQ_MUL;
553 
554 	video_set_drvdata(&radio->vdev, radio);
555 	retval = usb_amradio_init(radio);
556 	if (retval)
557 		goto err_vdev;
558 
559 	retval = video_register_device(&radio->vdev, VFL_TYPE_RADIO,
560 					radio_nr);
561 	if (retval < 0) {
562 		dev_err(&intf->dev, "could not register video device\n");
563 		goto err_vdev;
564 	}
565 
566 	return 0;
567 
568 err_vdev:
569 	v4l2_ctrl_handler_free(&radio->hdl);
570 err_ctrl:
571 	v4l2_device_unregister(&radio->v4l2_dev);
572 err_v4l2:
573 	kfree(radio->buffer);
574 err_nobuf:
575 	kfree(radio);
576 err:
577 	return retval;
578 }
579 
580 /* USB Device ID List */
581 static const struct usb_device_id usb_amradio_device_table[] = {
582 	{ USB_DEVICE_AND_INTERFACE_INFO(USB_AMRADIO_VENDOR, USB_AMRADIO_PRODUCT,
583 							USB_CLASS_HID, 0, 0) },
584 	{ }						/* Terminating entry */
585 };
586 
587 MODULE_DEVICE_TABLE(usb, usb_amradio_device_table);
588 
589 /* USB subsystem interface */
590 static struct usb_driver usb_amradio_driver = {
591 	.name			= MR800_DRIVER_NAME,
592 	.probe			= usb_amradio_probe,
593 	.disconnect		= usb_amradio_disconnect,
594 	.suspend		= usb_amradio_suspend,
595 	.resume			= usb_amradio_resume,
596 	.reset_resume		= usb_amradio_resume,
597 	.id_table		= usb_amradio_device_table,
598 };
599 
600 module_usb_driver(usb_amradio_driver);
601