xref: /linux/drivers/media/radio/radio-mr800.c (revision 4bd9ff19708ce8d88926121dda98423e36191c58)
12aa72f3bSAlexey Klimov /*
22aa72f3bSAlexey Klimov  * A driver for the AverMedia MR 800 USB FM radio. This device plugs
32aa72f3bSAlexey Klimov  * into both the USB and an analog audio input, so this thing
42aa72f3bSAlexey Klimov  * only deals with initialization and frequency setting, the
52aa72f3bSAlexey Klimov  * audio data has to be handled by a sound driver.
62aa72f3bSAlexey Klimov  *
72aa72f3bSAlexey Klimov  * Copyright (c) 2008 Alexey Klimov <klimov.linux@gmail.com>
82aa72f3bSAlexey Klimov  *
92aa72f3bSAlexey Klimov  * This program is free software; you can redistribute it and/or modify
102aa72f3bSAlexey Klimov  * it under the terms of the GNU General Public License as published by
112aa72f3bSAlexey Klimov  * the Free Software Foundation; either version 2 of the License, or
122aa72f3bSAlexey Klimov  * (at your option) any later version.
132aa72f3bSAlexey Klimov  *
142aa72f3bSAlexey Klimov  * This program is distributed in the hope that it will be useful,
152aa72f3bSAlexey Klimov  * but WITHOUT ANY WARRANTY; without even the implied warranty of
162aa72f3bSAlexey Klimov  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
172aa72f3bSAlexey Klimov  * GNU General Public License for more details.
182aa72f3bSAlexey Klimov  *
192aa72f3bSAlexey Klimov  * You should have received a copy of the GNU General Public License
202aa72f3bSAlexey Klimov  * along with this program; if not, write to the Free Software
212aa72f3bSAlexey Klimov  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
222aa72f3bSAlexey Klimov  */
232aa72f3bSAlexey Klimov 
242aa72f3bSAlexey Klimov /*
2571f07d94SAlexey Klimov  * Big thanks to authors and contributors of dsbr100.c and radio-si470x.c
262aa72f3bSAlexey Klimov  *
272aa72f3bSAlexey Klimov  * When work was looked pretty good, i discover this:
282aa72f3bSAlexey Klimov  * http://av-usbradio.sourceforge.net/index.php
292aa72f3bSAlexey Klimov  * http://sourceforge.net/projects/av-usbradio/
302aa72f3bSAlexey Klimov  * Latest release of theirs project was in 2005.
31af901ca1SAndré Goddard Rosa  * Probably, this driver could be improved through using their
322aa72f3bSAlexey Klimov  * achievements (specifications given).
3371f07d94SAlexey Klimov  * Also, Faidon Liambotis <paravoid@debian.org> wrote nice driver for this radio
3471f07d94SAlexey Klimov  * in 2007. He allowed to use his driver to improve current mr800 radio driver.
3571f07d94SAlexey Klimov  * http://kerneltrap.org/mailarchive/linux-usb-devel/2007/10/11/342492
362aa72f3bSAlexey Klimov  *
372aa72f3bSAlexey Klimov  * Version 0.01:	First working version.
382aa72f3bSAlexey Klimov  * 			It's required to blacklist AverMedia USB Radio
392aa72f3bSAlexey Klimov  * 			in usbhid/hid-quirks.c
4071f07d94SAlexey Klimov  * Version 0.10:	A lot of cleanups and fixes: unpluging the device,
4171f07d94SAlexey Klimov  * 			few mutex locks were added, codinstyle issues, etc.
4271f07d94SAlexey Klimov  * 			Added stereo support. Thanks to
4371f07d94SAlexey Klimov  * 			Douglas Schilling Landgraf <dougsland@gmail.com> and
4471f07d94SAlexey Klimov  * 			David Ellingsworth <david@identd.dyndns.org>
4571f07d94SAlexey Klimov  * 			for discussion, help and support.
46b466248dSAlexey Klimov  * Version 0.11:	Converted to v4l2_device.
472aa72f3bSAlexey Klimov  *
482aa72f3bSAlexey Klimov  * Many things to do:
493dbda77eSUwe Kleine-Koenig  * 	- Correct power management of device (suspend & resume)
502aa72f3bSAlexey Klimov  * 	- Add code for scanning and smooth tuning
512aa72f3bSAlexey Klimov  * 	- Add code for sensitivity value
522aa72f3bSAlexey Klimov  * 	- Correct mistakes
532aa72f3bSAlexey Klimov  * 	- In Japan another FREQ_MIN and FREQ_MAX
542aa72f3bSAlexey Klimov  */
552aa72f3bSAlexey Klimov 
562aa72f3bSAlexey Klimov /* kernel includes */
572aa72f3bSAlexey Klimov #include <linux/kernel.h>
582aa72f3bSAlexey Klimov #include <linux/module.h>
592aa72f3bSAlexey Klimov #include <linux/init.h>
602aa72f3bSAlexey Klimov #include <linux/slab.h>
612aa72f3bSAlexey Klimov #include <linux/input.h>
622aa72f3bSAlexey Klimov #include <linux/videodev2.h>
63b466248dSAlexey Klimov #include <media/v4l2-device.h>
642aa72f3bSAlexey Klimov #include <media/v4l2-ioctl.h>
6585578b0fSHans Verkuil #include <media/v4l2-ctrls.h>
6685578b0fSHans Verkuil #include <media/v4l2-event.h>
672aa72f3bSAlexey Klimov #include <linux/usb.h>
6824c44d85SAlessio Igor Bogani #include <linux/mutex.h>
692aa72f3bSAlexey Klimov 
702aa72f3bSAlexey Klimov /* driver and module definitions */
712aa72f3bSAlexey Klimov #define DRIVER_AUTHOR "Alexey Klimov <klimov.linux@gmail.com>"
722aa72f3bSAlexey Klimov #define DRIVER_DESC "AverMedia MR 800 USB FM radio driver"
7329834c1aSMauro Carvalho Chehab #define DRIVER_VERSION "0.1.2"
742aa72f3bSAlexey Klimov 
752aa72f3bSAlexey Klimov MODULE_AUTHOR(DRIVER_AUTHOR);
762aa72f3bSAlexey Klimov MODULE_DESCRIPTION(DRIVER_DESC);
772aa72f3bSAlexey Klimov MODULE_LICENSE("GPL");
7829834c1aSMauro Carvalho Chehab MODULE_VERSION(DRIVER_VERSION);
792aa72f3bSAlexey Klimov 
802aa72f3bSAlexey Klimov #define USB_AMRADIO_VENDOR 0x07ca
812aa72f3bSAlexey Klimov #define USB_AMRADIO_PRODUCT 0xb800
822aa72f3bSAlexey Klimov 
83e60b022eSAlexey Klimov /* dev_warn macro with driver name */
84e60b022eSAlexey Klimov #define MR800_DRIVER_NAME "radio-mr800"
85e60b022eSAlexey Klimov #define amradio_dev_warn(dev, fmt, arg...)				\
86e60b022eSAlexey Klimov 		dev_warn(dev, MR800_DRIVER_NAME " - " fmt, ##arg)
87e60b022eSAlexey Klimov 
88502d5016SDavid Ellingsworth #define amradio_dev_err(dev, fmt, arg...) \
89502d5016SDavid Ellingsworth 		dev_err(dev, MR800_DRIVER_NAME " - " fmt, ##arg)
90502d5016SDavid Ellingsworth 
912aa72f3bSAlexey Klimov /* Probably USB_TIMEOUT should be modified in module parameter */
922aa72f3bSAlexey Klimov #define BUFFER_LENGTH 8
932aa72f3bSAlexey Klimov #define USB_TIMEOUT 500
942aa72f3bSAlexey Klimov 
952aa72f3bSAlexey Klimov /* Frequency limits in MHz -- these are European values.  For Japanese
962aa72f3bSAlexey Klimov devices, that would be 76 and 91.  */
972aa72f3bSAlexey Klimov #define FREQ_MIN  87.5
982aa72f3bSAlexey Klimov #define FREQ_MAX 108.0
992aa72f3bSAlexey Klimov #define FREQ_MUL 16000
1002aa72f3bSAlexey Klimov 
101db821804SAlexey Klimov /*
102db821804SAlexey Klimov  * Commands that device should understand
10325985edcSLucas De Marchi  * List isn't full and will be updated with implementation of new functions
104db821804SAlexey Klimov  */
105f7c1a380SAlexey Klimov #define AMRADIO_SET_FREQ	0xa4
106*4bd9ff19SHans Verkuil #define AMRADIO_GET_READY_FLAG	0xa5
10777cf3934SHans Verkuil #define AMRADIO_GET_SIGNAL	0xa7
108*4bd9ff19SHans Verkuil #define AMRADIO_GET_FREQ	0xa8
109*4bd9ff19SHans Verkuil #define AMRADIO_SET_SEARCH_UP	0xa9
110*4bd9ff19SHans Verkuil #define AMRADIO_SET_SEARCH_DOWN	0xaa
111db821804SAlexey Klimov #define AMRADIO_SET_MUTE	0xab
112*4bd9ff19SHans Verkuil #define AMRADIO_SET_RIGHT_MUTE	0xac
113*4bd9ff19SHans Verkuil #define AMRADIO_SET_LEFT_MUTE	0xad
1141bb16d71SAlexey Klimov #define AMRADIO_SET_MONO	0xae
115*4bd9ff19SHans Verkuil #define AMRADIO_SET_SEARCH_LVL	0xb0
116*4bd9ff19SHans Verkuil #define AMRADIO_STOP_SEARCH	0xb1
117db821804SAlexey Klimov 
1181bb16d71SAlexey Klimov /* Comfortable defines for amradio_set_stereo */
1191bb16d71SAlexey Klimov #define WANT_STEREO		0x00
1201bb16d71SAlexey Klimov #define WANT_MONO		0x01
1211bb16d71SAlexey Klimov 
1222aa72f3bSAlexey Klimov /* module parameter */
1232aa72f3bSAlexey Klimov static int radio_nr = -1;
1242aa72f3bSAlexey Klimov module_param(radio_nr, int, 0);
1252aa72f3bSAlexey Klimov MODULE_PARM_DESC(radio_nr, "Radio Nr");
1262aa72f3bSAlexey Klimov 
1272aa72f3bSAlexey Klimov /* Data for one (physical) device */
1282aa72f3bSAlexey Klimov struct amradio_device {
1292aa72f3bSAlexey Klimov 	/* reference to USB and video device */
1302aa72f3bSAlexey Klimov 	struct usb_device *usbdev;
131d89ce0d9SOliver Neukum 	struct usb_interface *intf;
13285578b0fSHans Verkuil 	struct video_device vdev;
133b466248dSAlexey Klimov 	struct v4l2_device v4l2_dev;
13485578b0fSHans Verkuil 	struct v4l2_ctrl_handler hdl;
1352aa72f3bSAlexey Klimov 
136*4bd9ff19SHans Verkuil 	u8 *buffer;
1372aa72f3bSAlexey Klimov 	struct mutex lock;	/* buffer locking */
1382aa72f3bSAlexey Klimov 	int curfreq;
1392aa72f3bSAlexey Klimov 	int stereo;
1402aa72f3bSAlexey Klimov 	int muted;
1412aa72f3bSAlexey Klimov };
1422aa72f3bSAlexey Klimov 
143a2bdc5e6SHans Verkuil static inline struct amradio_device *to_amradio_dev(struct v4l2_device *v4l2_dev)
144a2bdc5e6SHans Verkuil {
145a2bdc5e6SHans Verkuil 	return container_of(v4l2_dev, struct amradio_device, v4l2_dev);
146a2bdc5e6SHans Verkuil }
147ceb99e1bSDavid Ellingsworth 
148*4bd9ff19SHans Verkuil static int amradio_send_cmd(struct amradio_device *radio, u8 cmd, u8 arg,
149*4bd9ff19SHans Verkuil 		u8 *extra, u8 extralen, bool reply)
1502aa72f3bSAlexey Klimov {
1512aa72f3bSAlexey Klimov 	int retval;
1522aa72f3bSAlexey Klimov 	int size;
1532aa72f3bSAlexey Klimov 
1542aa72f3bSAlexey Klimov 	radio->buffer[0] = 0x00;
1552aa72f3bSAlexey Klimov 	radio->buffer[1] = 0x55;
1562aa72f3bSAlexey Klimov 	radio->buffer[2] = 0xaa;
157*4bd9ff19SHans Verkuil 	radio->buffer[3] = extralen;
158*4bd9ff19SHans Verkuil 	radio->buffer[4] = cmd;
159*4bd9ff19SHans Verkuil 	radio->buffer[5] = arg;
1602aa72f3bSAlexey Klimov 	radio->buffer[6] = 0x00;
161*4bd9ff19SHans Verkuil 	radio->buffer[7] = extra || reply ? 8 : 0;
1622aa72f3bSAlexey Klimov 
1632aa72f3bSAlexey Klimov 	retval = usb_bulk_msg(radio->usbdev, usb_sndintpipe(radio->usbdev, 2),
164*4bd9ff19SHans Verkuil 		radio->buffer, BUFFER_LENGTH, &size, USB_TIMEOUT);
1652aa72f3bSAlexey Klimov 
166e57458dcSAlexey Klimov 	if (retval < 0 || size != BUFFER_LENGTH) {
167*4bd9ff19SHans Verkuil 		if (video_is_registered(&radio->vdev))
168*4bd9ff19SHans Verkuil 			amradio_dev_warn(&radio->vdev.dev,
169*4bd9ff19SHans Verkuil 					"cmd %02x failed\n", cmd);
170*4bd9ff19SHans Verkuil 		return retval ? retval : -EIO;
1712aa72f3bSAlexey Klimov 	}
172*4bd9ff19SHans Verkuil 	if (!extra && !reply)
17385578b0fSHans Verkuil 		return 0;
174*4bd9ff19SHans Verkuil 
175*4bd9ff19SHans Verkuil 	if (extra) {
176*4bd9ff19SHans Verkuil 		memcpy(radio->buffer, extra, extralen);
177*4bd9ff19SHans Verkuil 		memset(radio->buffer + extralen, 0, 8 - extralen);
178*4bd9ff19SHans Verkuil 		retval = usb_bulk_msg(radio->usbdev, usb_sndintpipe(radio->usbdev, 2),
179*4bd9ff19SHans Verkuil 			radio->buffer, BUFFER_LENGTH, &size, USB_TIMEOUT);
180*4bd9ff19SHans Verkuil 	} else {
181*4bd9ff19SHans Verkuil 		memset(radio->buffer, 0, 8);
182*4bd9ff19SHans Verkuil 		retval = usb_bulk_msg(radio->usbdev, usb_rcvbulkpipe(radio->usbdev, 0x81),
183*4bd9ff19SHans Verkuil 			radio->buffer, BUFFER_LENGTH, &size, USB_TIMEOUT);
184*4bd9ff19SHans Verkuil 	}
185*4bd9ff19SHans Verkuil 	if (retval == 0 && size == BUFFER_LENGTH)
186*4bd9ff19SHans Verkuil 		return 0;
187*4bd9ff19SHans Verkuil 	if (video_is_registered(&radio->vdev) && cmd != AMRADIO_GET_READY_FLAG)
188*4bd9ff19SHans Verkuil 		amradio_dev_warn(&radio->vdev.dev, "follow-up to cmd %02x failed\n", cmd);
189*4bd9ff19SHans Verkuil 	return retval ? retval : -EIO;
190*4bd9ff19SHans Verkuil }
191*4bd9ff19SHans Verkuil 
192*4bd9ff19SHans Verkuil /* switch on/off the radio. Send 8 bytes to device */
193*4bd9ff19SHans Verkuil static int amradio_set_mute(struct amradio_device *radio, bool mute)
194*4bd9ff19SHans Verkuil {
195*4bd9ff19SHans Verkuil 	int ret = amradio_send_cmd(radio,
196*4bd9ff19SHans Verkuil 			AMRADIO_SET_MUTE, mute, NULL, 0, false);
197*4bd9ff19SHans Verkuil 
198*4bd9ff19SHans Verkuil 	if (!ret)
199*4bd9ff19SHans Verkuil 		radio->muted = mute;
200*4bd9ff19SHans Verkuil 	return ret;
2012aa72f3bSAlexey Klimov }
2022aa72f3bSAlexey Klimov 
2032aa72f3bSAlexey Klimov /* set a frequency, freq is defined by v4l's TUNER_LOW, i.e. 1/16th kHz */
204*4bd9ff19SHans Verkuil static int amradio_set_freq(struct amradio_device *radio, int freq)
2052aa72f3bSAlexey Klimov {
20685578b0fSHans Verkuil 	unsigned short freq_send = 0x10 + (freq >> 3) / 25;
207*4bd9ff19SHans Verkuil 	u8 buf[3];
2082aa72f3bSAlexey Klimov 	int retval;
2092aa72f3bSAlexey Klimov 
2102aa72f3bSAlexey Klimov 	/* frequency is calculated from freq_send and placed in first 2 bytes */
211*4bd9ff19SHans Verkuil 	buf[0] = (freq_send >> 8) & 0xff;
212*4bd9ff19SHans Verkuil 	buf[1] = freq_send & 0xff;
213*4bd9ff19SHans Verkuil 	buf[2] = 0x01;
2142aa72f3bSAlexey Klimov 
215*4bd9ff19SHans Verkuil 	retval = amradio_send_cmd(radio, AMRADIO_SET_FREQ, 0, buf, 3, false);
216*4bd9ff19SHans Verkuil 	if (retval)
217*4bd9ff19SHans Verkuil 		return retval;
218798166dbSDavid Ellingsworth 	radio->curfreq = freq;
219*4bd9ff19SHans Verkuil 	msleep(40);
22085578b0fSHans Verkuil 	return 0;
22185578b0fSHans Verkuil }
2221bb16d71SAlexey Klimov 
223*4bd9ff19SHans Verkuil static int amradio_set_stereo(struct amradio_device *radio, bool stereo)
2241bb16d71SAlexey Klimov {
225*4bd9ff19SHans Verkuil 	int ret = amradio_send_cmd(radio,
226*4bd9ff19SHans Verkuil 			AMRADIO_SET_MONO, !stereo, NULL, 0, false);
2271bb16d71SAlexey Klimov 
228*4bd9ff19SHans Verkuil 	if (!ret)
229*4bd9ff19SHans Verkuil 		radio->stereo = stereo;
230*4bd9ff19SHans Verkuil 	return ret;
2312aa72f3bSAlexey Klimov }
2322aa72f3bSAlexey Klimov 
23377cf3934SHans Verkuil static int amradio_get_stat(struct amradio_device *radio, bool *is_stereo, u32 *signal)
23477cf3934SHans Verkuil {
235*4bd9ff19SHans Verkuil 	int ret = amradio_send_cmd(radio,
236*4bd9ff19SHans Verkuil 			AMRADIO_GET_SIGNAL, 0, NULL, 0, true);
23777cf3934SHans Verkuil 
238*4bd9ff19SHans Verkuil 	if (ret)
239*4bd9ff19SHans Verkuil 		return ret;
24077cf3934SHans Verkuil 	*is_stereo = radio->buffer[2] >> 7;
24177cf3934SHans Verkuil 	*signal = (radio->buffer[3] & 0xf0) << 8;
24277cf3934SHans Verkuil 	return 0;
24377cf3934SHans Verkuil }
24477cf3934SHans Verkuil 
24571f07d94SAlexey Klimov /* Handle unplugging the device.
24671f07d94SAlexey Klimov  * We call video_unregister_device in any case.
24771f07d94SAlexey Klimov  * The last function called in this procedure is
24871f07d94SAlexey Klimov  * usb_amradio_device_release.
24971f07d94SAlexey Klimov  */
2502aa72f3bSAlexey Klimov static void usb_amradio_disconnect(struct usb_interface *intf)
2512aa72f3bSAlexey Klimov {
252a2bdc5e6SHans Verkuil 	struct amradio_device *radio = to_amradio_dev(usb_get_intfdata(intf));
2532aa72f3bSAlexey Klimov 
254f4e9043eSAlexey Klimov 	mutex_lock(&radio->lock);
25585578b0fSHans Verkuil 	video_unregister_device(&radio->vdev);
256*4bd9ff19SHans Verkuil 	amradio_set_mute(radio, true);
257*4bd9ff19SHans Verkuil 	usb_set_intfdata(intf, NULL);
258b466248dSAlexey Klimov 	v4l2_device_disconnect(&radio->v4l2_dev);
259a682d4cbSHans Verkuil 	mutex_unlock(&radio->lock);
26085578b0fSHans Verkuil 	v4l2_device_put(&radio->v4l2_dev);
2612aa72f3bSAlexey Klimov }
2622aa72f3bSAlexey Klimov 
2632aa72f3bSAlexey Klimov /* vidioc_querycap - query device capabilities */
2642aa72f3bSAlexey Klimov static int vidioc_querycap(struct file *file, void *priv,
2652aa72f3bSAlexey Klimov 					struct v4l2_capability *v)
2662aa72f3bSAlexey Klimov {
26785578b0fSHans Verkuil 	struct amradio_device *radio = video_drvdata(file);
268c7181cfaSAlexey Klimov 
2692aa72f3bSAlexey Klimov 	strlcpy(v->driver, "radio-mr800", sizeof(v->driver));
2702aa72f3bSAlexey Klimov 	strlcpy(v->card, "AverMedia MR 800 USB FM Radio", sizeof(v->card));
271c7181cfaSAlexey Klimov 	usb_make_path(radio->usbdev, v->bus_info, sizeof(v->bus_info));
272*4bd9ff19SHans Verkuil 	v->device_caps = V4L2_CAP_RADIO | V4L2_CAP_TUNER |
273*4bd9ff19SHans Verkuil 					V4L2_CAP_HW_FREQ_SEEK;
27485578b0fSHans Verkuil 	v->capabilities = v->device_caps | V4L2_CAP_DEVICE_CAPS;
2752aa72f3bSAlexey Klimov 	return 0;
2762aa72f3bSAlexey Klimov }
2772aa72f3bSAlexey Klimov 
2782aa72f3bSAlexey Klimov /* vidioc_g_tuner - get tuner attributes */
2792aa72f3bSAlexey Klimov static int vidioc_g_tuner(struct file *file, void *priv,
2802aa72f3bSAlexey Klimov 				struct v4l2_tuner *v)
2812aa72f3bSAlexey Klimov {
28285578b0fSHans Verkuil 	struct amradio_device *radio = video_drvdata(file);
28377cf3934SHans Verkuil 	bool is_stereo = false;
28477cf3934SHans Verkuil 	int retval;
2852aa72f3bSAlexey Klimov 
2862aa72f3bSAlexey Klimov 	if (v->index > 0)
2872aa72f3bSAlexey Klimov 		return -EINVAL;
2882aa72f3bSAlexey Klimov 
28977cf3934SHans Verkuil 	v->signal = 0;
29077cf3934SHans Verkuil 	retval = amradio_get_stat(radio, &is_stereo, &v->signal);
29177cf3934SHans Verkuil 	if (retval)
29277cf3934SHans Verkuil 		return retval;
29377cf3934SHans Verkuil 
2942aa72f3bSAlexey Klimov 	strcpy(v->name, "FM");
2952aa72f3bSAlexey Klimov 	v->type = V4L2_TUNER_RADIO;
2962aa72f3bSAlexey Klimov 	v->rangelow = FREQ_MIN * FREQ_MUL;
2972aa72f3bSAlexey Klimov 	v->rangehigh = FREQ_MAX * FREQ_MUL;
29885578b0fSHans Verkuil 	v->capability = V4L2_TUNER_CAP_LOW | V4L2_TUNER_CAP_STEREO;
29977cf3934SHans Verkuil 	v->rxsubchans = is_stereo ? V4L2_TUNER_SUB_STEREO : V4L2_TUNER_SUB_MONO;
30085578b0fSHans Verkuil 	v->audmode = radio->stereo ?
30185578b0fSHans Verkuil 		V4L2_TUNER_MODE_STEREO : V4L2_TUNER_MODE_MONO;
30285578b0fSHans Verkuil 	return 0;
3032aa72f3bSAlexey Klimov }
3042aa72f3bSAlexey Klimov 
3052aa72f3bSAlexey Klimov /* vidioc_s_tuner - set tuner attributes */
3062aa72f3bSAlexey Klimov static int vidioc_s_tuner(struct file *file, void *priv,
3072aa72f3bSAlexey Klimov 				struct v4l2_tuner *v)
3082aa72f3bSAlexey Klimov {
30985578b0fSHans Verkuil 	struct amradio_device *radio = video_drvdata(file);
3103480130aSAlexey Klimov 
3112aa72f3bSAlexey Klimov 	if (v->index > 0)
3122aa72f3bSAlexey Klimov 		return -EINVAL;
3131bb16d71SAlexey Klimov 
3141bb16d71SAlexey Klimov 	/* mono/stereo selector */
3151bb16d71SAlexey Klimov 	switch (v->audmode) {
3161bb16d71SAlexey Klimov 	case V4L2_TUNER_MODE_MONO:
31785578b0fSHans Verkuil 		return amradio_set_stereo(radio, WANT_MONO);
31885578b0fSHans Verkuil 	default:
31985578b0fSHans Verkuil 		return amradio_set_stereo(radio, WANT_STEREO);
3201bb16d71SAlexey Klimov 	}
3212aa72f3bSAlexey Klimov }
3222aa72f3bSAlexey Klimov 
3232aa72f3bSAlexey Klimov /* vidioc_s_frequency - set tuner radio frequency */
3242aa72f3bSAlexey Klimov static int vidioc_s_frequency(struct file *file, void *priv,
3252aa72f3bSAlexey Klimov 				struct v4l2_frequency *f)
3262aa72f3bSAlexey Klimov {
32785578b0fSHans Verkuil 	struct amradio_device *radio = video_drvdata(file);
3282aa72f3bSAlexey Klimov 
32985578b0fSHans Verkuil 	if (f->tuner != 0)
330a3a9e287SHans Verkuil 		return -EINVAL;
331*4bd9ff19SHans Verkuil 	return amradio_set_freq(radio, clamp_t(unsigned, f->frequency,
33285578b0fSHans Verkuil 				FREQ_MIN * FREQ_MUL, FREQ_MAX * FREQ_MUL));
3332aa72f3bSAlexey Klimov }
3342aa72f3bSAlexey Klimov 
3352aa72f3bSAlexey Klimov /* vidioc_g_frequency - get tuner radio frequency */
3362aa72f3bSAlexey Klimov static int vidioc_g_frequency(struct file *file, void *priv,
3372aa72f3bSAlexey Klimov 				struct v4l2_frequency *f)
3382aa72f3bSAlexey Klimov {
33985578b0fSHans Verkuil 	struct amradio_device *radio = video_drvdata(file);
3403480130aSAlexey Klimov 
34185578b0fSHans Verkuil 	if (f->tuner != 0 || f->type != V4L2_TUNER_RADIO)
342a3a9e287SHans Verkuil 		return -EINVAL;
3432aa72f3bSAlexey Klimov 	f->type = V4L2_TUNER_RADIO;
3442aa72f3bSAlexey Klimov 	f->frequency = radio->curfreq;
3454aebc289SDavid Ellingsworth 
3462aa72f3bSAlexey Klimov 	return 0;
3472aa72f3bSAlexey Klimov }
3482aa72f3bSAlexey Klimov 
349*4bd9ff19SHans Verkuil static int vidioc_s_hw_freq_seek(struct file *file, void *priv,
350*4bd9ff19SHans Verkuil 		struct v4l2_hw_freq_seek *seek)
351*4bd9ff19SHans Verkuil {
352*4bd9ff19SHans Verkuil 	static u8 buf[8] = {
353*4bd9ff19SHans Verkuil 		0x3d, 0x32, 0x0f, 0x08, 0x3d, 0x32, 0x0f, 0x08
354*4bd9ff19SHans Verkuil 	};
355*4bd9ff19SHans Verkuil 	struct amradio_device *radio = video_drvdata(file);
356*4bd9ff19SHans Verkuil 	unsigned long timeout;
357*4bd9ff19SHans Verkuil 	int retval;
358*4bd9ff19SHans Verkuil 
359*4bd9ff19SHans Verkuil 	if (seek->tuner != 0 || !seek->wrap_around)
360*4bd9ff19SHans Verkuil 		return -EINVAL;
361*4bd9ff19SHans Verkuil 
362*4bd9ff19SHans Verkuil 	retval = amradio_send_cmd(radio,
363*4bd9ff19SHans Verkuil 			AMRADIO_SET_SEARCH_LVL, 0, buf, 8, false);
364*4bd9ff19SHans Verkuil 	if (retval)
365*4bd9ff19SHans Verkuil 		return retval;
366*4bd9ff19SHans Verkuil 	amradio_set_freq(radio, radio->curfreq);
367*4bd9ff19SHans Verkuil 	retval = amradio_send_cmd(radio,
368*4bd9ff19SHans Verkuil 		seek->seek_upward ? AMRADIO_SET_SEARCH_UP : AMRADIO_SET_SEARCH_DOWN,
369*4bd9ff19SHans Verkuil 		0, NULL, 0, false);
370*4bd9ff19SHans Verkuil 	if (retval)
371*4bd9ff19SHans Verkuil 		return retval;
372*4bd9ff19SHans Verkuil 	timeout = jiffies + msecs_to_jiffies(30000);
373*4bd9ff19SHans Verkuil 	for (;;) {
374*4bd9ff19SHans Verkuil 		if (time_after(jiffies, timeout)) {
375*4bd9ff19SHans Verkuil 			retval = -EAGAIN;
376*4bd9ff19SHans Verkuil 			break;
377*4bd9ff19SHans Verkuil 		}
378*4bd9ff19SHans Verkuil 		if (schedule_timeout_interruptible(msecs_to_jiffies(10))) {
379*4bd9ff19SHans Verkuil 			retval = -ERESTARTSYS;
380*4bd9ff19SHans Verkuil 			break;
381*4bd9ff19SHans Verkuil 		}
382*4bd9ff19SHans Verkuil 		retval = amradio_send_cmd(radio, AMRADIO_GET_READY_FLAG,
383*4bd9ff19SHans Verkuil 				0, NULL, 0, true);
384*4bd9ff19SHans Verkuil 		if (retval)
385*4bd9ff19SHans Verkuil 			continue;
386*4bd9ff19SHans Verkuil 		amradio_send_cmd(radio, AMRADIO_GET_FREQ, 0, NULL, 0, true);
387*4bd9ff19SHans Verkuil 		if (radio->buffer[1] || radio->buffer[2]) {
388*4bd9ff19SHans Verkuil 			radio->curfreq = (radio->buffer[1] << 8) | radio->buffer[2];
389*4bd9ff19SHans Verkuil 			radio->curfreq = (radio->curfreq - 0x10) * 200;
390*4bd9ff19SHans Verkuil 			amradio_send_cmd(radio, AMRADIO_STOP_SEARCH,
391*4bd9ff19SHans Verkuil 					0, NULL, 0, false);
392*4bd9ff19SHans Verkuil 			amradio_set_freq(radio, radio->curfreq);
393*4bd9ff19SHans Verkuil 			retval = 0;
394*4bd9ff19SHans Verkuil 			break;
395*4bd9ff19SHans Verkuil 		}
396*4bd9ff19SHans Verkuil 	}
397*4bd9ff19SHans Verkuil 	amradio_send_cmd(radio, AMRADIO_STOP_SEARCH, 0, NULL, 0, false);
398*4bd9ff19SHans Verkuil 	amradio_set_freq(radio, radio->curfreq);
399*4bd9ff19SHans Verkuil 	return retval;
400*4bd9ff19SHans Verkuil }
401*4bd9ff19SHans Verkuil 
40285578b0fSHans Verkuil static int usb_amradio_s_ctrl(struct v4l2_ctrl *ctrl)
4032aa72f3bSAlexey Klimov {
40485578b0fSHans Verkuil 	struct amradio_device *radio =
40585578b0fSHans Verkuil 		container_of(ctrl->handler, struct amradio_device, hdl);
4063480130aSAlexey Klimov 
4072aa72f3bSAlexey Klimov 	switch (ctrl->id) {
4082aa72f3bSAlexey Klimov 	case V4L2_CID_AUDIO_MUTE:
409*4bd9ff19SHans Verkuil 		return amradio_set_mute(radio, ctrl->val);
4102aa72f3bSAlexey Klimov 	}
4114aebc289SDavid Ellingsworth 
4122aa72f3bSAlexey Klimov 	return -EINVAL;
4132aa72f3bSAlexey Klimov }
4142aa72f3bSAlexey Klimov 
415502d5016SDavid Ellingsworth static int usb_amradio_init(struct amradio_device *radio)
416502d5016SDavid Ellingsworth {
417502d5016SDavid Ellingsworth 	int retval;
418502d5016SDavid Ellingsworth 
419*4bd9ff19SHans Verkuil 	retval = amradio_set_mute(radio, true);
42016a72f41SDavid Ellingsworth 	if (retval)
421502d5016SDavid Ellingsworth 		goto out_err;
422*4bd9ff19SHans Verkuil 	retval = amradio_set_stereo(radio, true);
42316a72f41SDavid Ellingsworth 	if (retval)
424502d5016SDavid Ellingsworth 		goto out_err;
425*4bd9ff19SHans Verkuil 	retval = amradio_set_freq(radio, radio->curfreq);
426*4bd9ff19SHans Verkuil 	if (retval)
427*4bd9ff19SHans Verkuil 		goto out_err;
428*4bd9ff19SHans Verkuil 	return 0;
429502d5016SDavid Ellingsworth 
430502d5016SDavid Ellingsworth out_err:
43185578b0fSHans Verkuil 	amradio_dev_err(&radio->vdev.dev, "initialization failed\n");
432502d5016SDavid Ellingsworth 	return retval;
433502d5016SDavid Ellingsworth }
434502d5016SDavid Ellingsworth 
4352aa72f3bSAlexey Klimov /* Suspend device - stop device. Need to be checked and fixed */
4362aa72f3bSAlexey Klimov static int usb_amradio_suspend(struct usb_interface *intf, pm_message_t message)
4372aa72f3bSAlexey Klimov {
438a2bdc5e6SHans Verkuil 	struct amradio_device *radio = to_amradio_dev(usb_get_intfdata(intf));
4392aa72f3bSAlexey Klimov 
440a682d4cbSHans Verkuil 	mutex_lock(&radio->lock);
44185578b0fSHans Verkuil 	if (!radio->muted) {
442*4bd9ff19SHans Verkuil 		amradio_set_mute(radio, true);
443*4bd9ff19SHans Verkuil 		radio->muted = false;
444d8970e5fSDavid Ellingsworth 	}
445a682d4cbSHans Verkuil 	mutex_unlock(&radio->lock);
4462aa72f3bSAlexey Klimov 
447e60b022eSAlexey Klimov 	dev_info(&intf->dev, "going into suspend..\n");
4482aa72f3bSAlexey Klimov 	return 0;
4492aa72f3bSAlexey Klimov }
4502aa72f3bSAlexey Klimov 
4512aa72f3bSAlexey Klimov /* Resume device - start device. Need to be checked and fixed */
4522aa72f3bSAlexey Klimov static int usb_amradio_resume(struct usb_interface *intf)
4532aa72f3bSAlexey Klimov {
454a2bdc5e6SHans Verkuil 	struct amradio_device *radio = to_amradio_dev(usb_get_intfdata(intf));
4552aa72f3bSAlexey Klimov 
456a682d4cbSHans Verkuil 	mutex_lock(&radio->lock);
457*4bd9ff19SHans Verkuil 	amradio_set_stereo(radio, radio->stereo);
458*4bd9ff19SHans Verkuil 	amradio_set_freq(radio, radio->curfreq);
459d8970e5fSDavid Ellingsworth 
46016a72f41SDavid Ellingsworth 	if (!radio->muted)
461*4bd9ff19SHans Verkuil 		amradio_set_mute(radio, false);
4622aa72f3bSAlexey Klimov 
463a682d4cbSHans Verkuil 	mutex_unlock(&radio->lock);
464a682d4cbSHans Verkuil 
465e60b022eSAlexey Klimov 	dev_info(&intf->dev, "coming out of suspend..\n");
4662aa72f3bSAlexey Klimov 	return 0;
4672aa72f3bSAlexey Klimov }
4682aa72f3bSAlexey Klimov 
46985578b0fSHans Verkuil static const struct v4l2_ctrl_ops usb_amradio_ctrl_ops = {
47085578b0fSHans Verkuil 	.s_ctrl = usb_amradio_s_ctrl,
47185578b0fSHans Verkuil };
47285578b0fSHans Verkuil 
4732aa72f3bSAlexey Klimov /* File system interface */
474bec43661SHans Verkuil static const struct v4l2_file_operations usb_amradio_fops = {
4752aa72f3bSAlexey Klimov 	.owner		= THIS_MODULE,
47685578b0fSHans Verkuil 	.open		= v4l2_fh_open,
47785578b0fSHans Verkuil 	.release	= v4l2_fh_release,
47885578b0fSHans Verkuil 	.poll		= v4l2_ctrl_poll,
479361ae54fSHans Verkuil 	.unlocked_ioctl	= video_ioctl2,
4802aa72f3bSAlexey Klimov };
4812aa72f3bSAlexey Klimov 
4822aa72f3bSAlexey Klimov static const struct v4l2_ioctl_ops usb_amradio_ioctl_ops = {
4832aa72f3bSAlexey Klimov 	.vidioc_querycap    = vidioc_querycap,
4842aa72f3bSAlexey Klimov 	.vidioc_g_tuner     = vidioc_g_tuner,
4852aa72f3bSAlexey Klimov 	.vidioc_s_tuner     = vidioc_s_tuner,
4862aa72f3bSAlexey Klimov 	.vidioc_g_frequency = vidioc_g_frequency,
4872aa72f3bSAlexey Klimov 	.vidioc_s_frequency = vidioc_s_frequency,
488*4bd9ff19SHans Verkuil 	.vidioc_s_hw_freq_seek = vidioc_s_hw_freq_seek,
48985578b0fSHans Verkuil 	.vidioc_log_status  = v4l2_ctrl_log_status,
49085578b0fSHans Verkuil 	.vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
49185578b0fSHans Verkuil 	.vidioc_unsubscribe_event = v4l2_event_unsubscribe,
4922aa72f3bSAlexey Klimov };
4932aa72f3bSAlexey Klimov 
49485578b0fSHans Verkuil static void usb_amradio_release(struct v4l2_device *v4l2_dev)
495f4e9043eSAlexey Klimov {
49685578b0fSHans Verkuil 	struct amradio_device *radio = to_amradio_dev(v4l2_dev);
497b466248dSAlexey Klimov 
498f4e9043eSAlexey Klimov 	/* free rest memory */
49985578b0fSHans Verkuil 	v4l2_ctrl_handler_free(&radio->hdl);
50085578b0fSHans Verkuil 	v4l2_device_unregister(&radio->v4l2_dev);
501f4e9043eSAlexey Klimov 	kfree(radio->buffer);
502f4e9043eSAlexey Klimov 	kfree(radio);
503f4e9043eSAlexey Klimov }
504f4e9043eSAlexey Klimov 
505a5d69475SAlexey Klimov /* check if the device is present and register with v4l and usb if it is */
5062aa72f3bSAlexey Klimov static int usb_amradio_probe(struct usb_interface *intf,
5072aa72f3bSAlexey Klimov 				const struct usb_device_id *id)
5082aa72f3bSAlexey Klimov {
5092aa72f3bSAlexey Klimov 	struct amradio_device *radio;
5101b8bbb3cSDavid Ellingsworth 	int retval = 0;
5112aa72f3bSAlexey Klimov 
512b466248dSAlexey Klimov 	radio = kzalloc(sizeof(struct amradio_device), GFP_KERNEL);
5132aa72f3bSAlexey Klimov 
5148edafcc6SAlexey Klimov 	if (!radio) {
5158edafcc6SAlexey Klimov 		dev_err(&intf->dev, "kmalloc for amradio_device failed\n");
5161b8bbb3cSDavid Ellingsworth 		retval = -ENOMEM;
5171b8bbb3cSDavid Ellingsworth 		goto err;
5188edafcc6SAlexey Klimov 	}
5192aa72f3bSAlexey Klimov 
5202aa72f3bSAlexey Klimov 	radio->buffer = kmalloc(BUFFER_LENGTH, GFP_KERNEL);
5212aa72f3bSAlexey Klimov 
5228edafcc6SAlexey Klimov 	if (!radio->buffer) {
5238edafcc6SAlexey Klimov 		dev_err(&intf->dev, "kmalloc for radio->buffer failed\n");
5241b8bbb3cSDavid Ellingsworth 		retval = -ENOMEM;
5251b8bbb3cSDavid Ellingsworth 		goto err_nobuf;
5262aa72f3bSAlexey Klimov 	}
5272aa72f3bSAlexey Klimov 
528d1939e4cSDavid Ellingsworth 	retval = v4l2_device_register(&intf->dev, &radio->v4l2_dev);
529b466248dSAlexey Klimov 	if (retval < 0) {
530b466248dSAlexey Klimov 		dev_err(&intf->dev, "couldn't register v4l2_device\n");
5311b8bbb3cSDavid Ellingsworth 		goto err_v4l2;
532b466248dSAlexey Klimov 	}
533b466248dSAlexey Klimov 
53485578b0fSHans Verkuil 	v4l2_ctrl_handler_init(&radio->hdl, 1);
53585578b0fSHans Verkuil 	v4l2_ctrl_new_std(&radio->hdl, &usb_amradio_ctrl_ops,
53685578b0fSHans Verkuil 			  V4L2_CID_AUDIO_MUTE, 0, 1, 1, 1);
53785578b0fSHans Verkuil 	if (radio->hdl.error) {
53885578b0fSHans Verkuil 		retval = radio->hdl.error;
53985578b0fSHans Verkuil 		dev_err(&intf->dev, "couldn't register control\n");
54085578b0fSHans Verkuil 		goto err_ctrl;
54185578b0fSHans Verkuil 	}
542361ae54fSHans Verkuil 	mutex_init(&radio->lock);
543361ae54fSHans Verkuil 
54485578b0fSHans Verkuil 	radio->v4l2_dev.ctrl_handler = &radio->hdl;
54585578b0fSHans Verkuil 	radio->v4l2_dev.release = usb_amradio_release;
54685578b0fSHans Verkuil 	strlcpy(radio->vdev.name, radio->v4l2_dev.name,
54785578b0fSHans Verkuil 		sizeof(radio->vdev.name));
54885578b0fSHans Verkuil 	radio->vdev.v4l2_dev = &radio->v4l2_dev;
54985578b0fSHans Verkuil 	radio->vdev.fops = &usb_amradio_fops;
55085578b0fSHans Verkuil 	radio->vdev.ioctl_ops = &usb_amradio_ioctl_ops;
55185578b0fSHans Verkuil 	radio->vdev.release = video_device_release_empty;
55285578b0fSHans Verkuil 	radio->vdev.lock = &radio->lock;
55385578b0fSHans Verkuil 	set_bit(V4L2_FL_USE_FH_PRIO, &radio->vdev.flags);
5542aa72f3bSAlexey Klimov 
5552aa72f3bSAlexey Klimov 	radio->usbdev = interface_to_usbdev(intf);
556d89ce0d9SOliver Neukum 	radio->intf = intf;
55785578b0fSHans Verkuil 	usb_set_intfdata(intf, &radio->v4l2_dev);
5582aa72f3bSAlexey Klimov 	radio->curfreq = 95.16 * FREQ_MUL;
5592aa72f3bSAlexey Klimov 
56085578b0fSHans Verkuil 	video_set_drvdata(&radio->vdev, radio);
56185578b0fSHans Verkuil 	retval = usb_amradio_init(radio);
56285578b0fSHans Verkuil 	if (retval)
56385578b0fSHans Verkuil 		goto err_vdev;
564b466248dSAlexey Klimov 
56585578b0fSHans Verkuil 	retval = video_register_device(&radio->vdev, VFL_TYPE_RADIO,
5667a7d92e0SDavid Ellingsworth 					radio_nr);
567a5d69475SAlexey Klimov 	if (retval < 0) {
56865c51dc9SAlexey Klimov 		dev_err(&intf->dev, "could not register video device\n");
5691b8bbb3cSDavid Ellingsworth 		goto err_vdev;
5702aa72f3bSAlexey Klimov 	}
5712aa72f3bSAlexey Klimov 
5722aa72f3bSAlexey Klimov 	return 0;
5731b8bbb3cSDavid Ellingsworth 
5741b8bbb3cSDavid Ellingsworth err_vdev:
57585578b0fSHans Verkuil 	v4l2_ctrl_handler_free(&radio->hdl);
57685578b0fSHans Verkuil err_ctrl:
577d1939e4cSDavid Ellingsworth 	v4l2_device_unregister(&radio->v4l2_dev);
5781b8bbb3cSDavid Ellingsworth err_v4l2:
5791b8bbb3cSDavid Ellingsworth 	kfree(radio->buffer);
5801b8bbb3cSDavid Ellingsworth err_nobuf:
5811b8bbb3cSDavid Ellingsworth 	kfree(radio);
5821b8bbb3cSDavid Ellingsworth err:
5831b8bbb3cSDavid Ellingsworth 	return retval;
5842aa72f3bSAlexey Klimov }
5852aa72f3bSAlexey Klimov 
58685578b0fSHans Verkuil /* USB Device ID List */
58785578b0fSHans Verkuil static struct usb_device_id usb_amradio_device_table[] = {
58885578b0fSHans Verkuil 	{ USB_DEVICE_AND_INTERFACE_INFO(USB_AMRADIO_VENDOR, USB_AMRADIO_PRODUCT,
58985578b0fSHans Verkuil 							USB_CLASS_HID, 0, 0) },
59085578b0fSHans Verkuil 	{ }						/* Terminating entry */
59185578b0fSHans Verkuil };
59285578b0fSHans Verkuil 
59385578b0fSHans Verkuil MODULE_DEVICE_TABLE(usb, usb_amradio_device_table);
59485578b0fSHans Verkuil 
59585578b0fSHans Verkuil /* USB subsystem interface */
59685578b0fSHans Verkuil static struct usb_driver usb_amradio_driver = {
59785578b0fSHans Verkuil 	.name			= MR800_DRIVER_NAME,
59885578b0fSHans Verkuil 	.probe			= usb_amradio_probe,
59985578b0fSHans Verkuil 	.disconnect		= usb_amradio_disconnect,
60085578b0fSHans Verkuil 	.suspend		= usb_amradio_suspend,
60185578b0fSHans Verkuil 	.resume			= usb_amradio_resume,
60285578b0fSHans Verkuil 	.reset_resume		= usb_amradio_resume,
60385578b0fSHans Verkuil 	.id_table		= usb_amradio_device_table,
60485578b0fSHans Verkuil };
60585578b0fSHans Verkuil 
606ecb3b2b3SGreg Kroah-Hartman module_usb_driver(usb_amradio_driver);
607