1c942fddfSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
22aa72f3bSAlexey Klimov /*
32aa72f3bSAlexey Klimov * A driver for the AverMedia MR 800 USB FM radio. This device plugs
42aa72f3bSAlexey Klimov * into both the USB and an analog audio input, so this thing
52aa72f3bSAlexey Klimov * only deals with initialization and frequency setting, the
62aa72f3bSAlexey Klimov * audio data has to be handled by a sound driver.
72aa72f3bSAlexey Klimov *
82aa72f3bSAlexey Klimov * Copyright (c) 2008 Alexey Klimov <klimov.linux@gmail.com>
92aa72f3bSAlexey Klimov */
102aa72f3bSAlexey Klimov
112aa72f3bSAlexey Klimov /*
1271f07d94SAlexey Klimov * Big thanks to authors and contributors of dsbr100.c and radio-si470x.c
132aa72f3bSAlexey Klimov *
142aa72f3bSAlexey Klimov * When work was looked pretty good, i discover this:
152aa72f3bSAlexey Klimov * http://av-usbradio.sourceforge.net/index.php
162aa72f3bSAlexey Klimov * http://sourceforge.net/projects/av-usbradio/
172aa72f3bSAlexey Klimov * Latest release of theirs project was in 2005.
18af901ca1SAndré Goddard Rosa * Probably, this driver could be improved through using their
192aa72f3bSAlexey Klimov * achievements (specifications given).
2071f07d94SAlexey Klimov * Also, Faidon Liambotis <paravoid@debian.org> wrote nice driver for this radio
2171f07d94SAlexey Klimov * in 2007. He allowed to use his driver to improve current mr800 radio driver.
228effbff4SPranith Kumar * http://www.spinics.net/lists/linux-usb-devel/msg10109.html
232aa72f3bSAlexey Klimov *
242aa72f3bSAlexey Klimov * Version 0.01: First working version.
252aa72f3bSAlexey Klimov * It's required to blacklist AverMedia USB Radio
262aa72f3bSAlexey Klimov * in usbhid/hid-quirks.c
2771f07d94SAlexey Klimov * Version 0.10: A lot of cleanups and fixes: unpluging the device,
2871f07d94SAlexey Klimov * few mutex locks were added, codinstyle issues, etc.
2971f07d94SAlexey Klimov * Added stereo support. Thanks to
3071f07d94SAlexey Klimov * Douglas Schilling Landgraf <dougsland@gmail.com> and
3171f07d94SAlexey Klimov * David Ellingsworth <david@identd.dyndns.org>
3271f07d94SAlexey Klimov * for discussion, help and support.
33b466248dSAlexey Klimov * Version 0.11: Converted to v4l2_device.
342aa72f3bSAlexey Klimov *
352aa72f3bSAlexey Klimov * Many things to do:
363dbda77eSUwe Kleine-Koenig * - Correct power management of device (suspend & resume)
372aa72f3bSAlexey Klimov * - Add code for scanning and smooth tuning
382aa72f3bSAlexey Klimov * - Add code for sensitivity value
392aa72f3bSAlexey Klimov * - Correct mistakes
402aa72f3bSAlexey Klimov * - In Japan another FREQ_MIN and FREQ_MAX
412aa72f3bSAlexey Klimov */
422aa72f3bSAlexey Klimov
432aa72f3bSAlexey Klimov /* kernel includes */
442aa72f3bSAlexey Klimov #include <linux/kernel.h>
452aa72f3bSAlexey Klimov #include <linux/module.h>
462aa72f3bSAlexey Klimov #include <linux/init.h>
472aa72f3bSAlexey Klimov #include <linux/slab.h>
482aa72f3bSAlexey Klimov #include <linux/input.h>
492aa72f3bSAlexey Klimov #include <linux/videodev2.h>
50b466248dSAlexey Klimov #include <media/v4l2-device.h>
512aa72f3bSAlexey Klimov #include <media/v4l2-ioctl.h>
5285578b0fSHans Verkuil #include <media/v4l2-ctrls.h>
5385578b0fSHans Verkuil #include <media/v4l2-event.h>
542aa72f3bSAlexey Klimov #include <linux/usb.h>
5524c44d85SAlessio Igor Bogani #include <linux/mutex.h>
562aa72f3bSAlexey Klimov
572aa72f3bSAlexey Klimov /* driver and module definitions */
582aa72f3bSAlexey Klimov #define DRIVER_AUTHOR "Alexey Klimov <klimov.linux@gmail.com>"
592aa72f3bSAlexey Klimov #define DRIVER_DESC "AverMedia MR 800 USB FM radio driver"
6029834c1aSMauro Carvalho Chehab #define DRIVER_VERSION "0.1.2"
612aa72f3bSAlexey Klimov
622aa72f3bSAlexey Klimov MODULE_AUTHOR(DRIVER_AUTHOR);
632aa72f3bSAlexey Klimov MODULE_DESCRIPTION(DRIVER_DESC);
642aa72f3bSAlexey Klimov MODULE_LICENSE("GPL");
6529834c1aSMauro Carvalho Chehab MODULE_VERSION(DRIVER_VERSION);
662aa72f3bSAlexey Klimov
672aa72f3bSAlexey Klimov #define USB_AMRADIO_VENDOR 0x07ca
682aa72f3bSAlexey Klimov #define USB_AMRADIO_PRODUCT 0xb800
692aa72f3bSAlexey Klimov
70e60b022eSAlexey Klimov /* dev_warn macro with driver name */
71e60b022eSAlexey Klimov #define MR800_DRIVER_NAME "radio-mr800"
72e60b022eSAlexey Klimov #define amradio_dev_warn(dev, fmt, arg...) \
73e60b022eSAlexey Klimov dev_warn(dev, MR800_DRIVER_NAME " - " fmt, ##arg)
74e60b022eSAlexey Klimov
75502d5016SDavid Ellingsworth #define amradio_dev_err(dev, fmt, arg...) \
76502d5016SDavid Ellingsworth dev_err(dev, MR800_DRIVER_NAME " - " fmt, ##arg)
77502d5016SDavid Ellingsworth
782aa72f3bSAlexey Klimov /* Probably USB_TIMEOUT should be modified in module parameter */
792aa72f3bSAlexey Klimov #define BUFFER_LENGTH 8
802aa72f3bSAlexey Klimov #define USB_TIMEOUT 500
812aa72f3bSAlexey Klimov
822aa72f3bSAlexey Klimov /* Frequency limits in MHz -- these are European values. For Japanese
832aa72f3bSAlexey Klimov devices, that would be 76 and 91. */
842aa72f3bSAlexey Klimov #define FREQ_MIN 87.5
852aa72f3bSAlexey Klimov #define FREQ_MAX 108.0
862aa72f3bSAlexey Klimov #define FREQ_MUL 16000
872aa72f3bSAlexey Klimov
88db821804SAlexey Klimov /*
89db821804SAlexey Klimov * Commands that device should understand
9025985edcSLucas De Marchi * List isn't full and will be updated with implementation of new functions
91db821804SAlexey Klimov */
92f7c1a380SAlexey Klimov #define AMRADIO_SET_FREQ 0xa4
934bd9ff19SHans Verkuil #define AMRADIO_GET_READY_FLAG 0xa5
9477cf3934SHans Verkuil #define AMRADIO_GET_SIGNAL 0xa7
954bd9ff19SHans Verkuil #define AMRADIO_GET_FREQ 0xa8
964bd9ff19SHans Verkuil #define AMRADIO_SET_SEARCH_UP 0xa9
974bd9ff19SHans Verkuil #define AMRADIO_SET_SEARCH_DOWN 0xaa
98db821804SAlexey Klimov #define AMRADIO_SET_MUTE 0xab
994bd9ff19SHans Verkuil #define AMRADIO_SET_RIGHT_MUTE 0xac
1004bd9ff19SHans Verkuil #define AMRADIO_SET_LEFT_MUTE 0xad
1011bb16d71SAlexey Klimov #define AMRADIO_SET_MONO 0xae
1024bd9ff19SHans Verkuil #define AMRADIO_SET_SEARCH_LVL 0xb0
1034bd9ff19SHans Verkuil #define AMRADIO_STOP_SEARCH 0xb1
104db821804SAlexey Klimov
1051bb16d71SAlexey Klimov /* Comfortable defines for amradio_set_stereo */
1061bb16d71SAlexey Klimov #define WANT_STEREO 0x00
1071bb16d71SAlexey Klimov #define WANT_MONO 0x01
1081bb16d71SAlexey Klimov
1092aa72f3bSAlexey Klimov /* module parameter */
1102aa72f3bSAlexey Klimov static int radio_nr = -1;
1112aa72f3bSAlexey Klimov module_param(radio_nr, int, 0);
1122aa72f3bSAlexey Klimov MODULE_PARM_DESC(radio_nr, "Radio Nr");
1132aa72f3bSAlexey Klimov
1142aa72f3bSAlexey Klimov /* Data for one (physical) device */
1152aa72f3bSAlexey Klimov struct amradio_device {
1162aa72f3bSAlexey Klimov /* reference to USB and video device */
1172aa72f3bSAlexey Klimov struct usb_device *usbdev;
118d89ce0d9SOliver Neukum struct usb_interface *intf;
11985578b0fSHans Verkuil struct video_device vdev;
120b466248dSAlexey Klimov struct v4l2_device v4l2_dev;
12185578b0fSHans Verkuil struct v4l2_ctrl_handler hdl;
1222aa72f3bSAlexey Klimov
1234bd9ff19SHans Verkuil u8 *buffer;
1242aa72f3bSAlexey Klimov struct mutex lock; /* buffer locking */
1252aa72f3bSAlexey Klimov int curfreq;
1262aa72f3bSAlexey Klimov int stereo;
1272aa72f3bSAlexey Klimov int muted;
1282aa72f3bSAlexey Klimov };
1292aa72f3bSAlexey Klimov
to_amradio_dev(struct v4l2_device * v4l2_dev)130a2bdc5e6SHans Verkuil static inline struct amradio_device *to_amradio_dev(struct v4l2_device *v4l2_dev)
131a2bdc5e6SHans Verkuil {
132a2bdc5e6SHans Verkuil return container_of(v4l2_dev, struct amradio_device, v4l2_dev);
133a2bdc5e6SHans Verkuil }
134ceb99e1bSDavid Ellingsworth
amradio_send_cmd(struct amradio_device * radio,u8 cmd,u8 arg,u8 * extra,u8 extralen,bool reply)1354bd9ff19SHans Verkuil static int amradio_send_cmd(struct amradio_device *radio, u8 cmd, u8 arg,
1364bd9ff19SHans Verkuil u8 *extra, u8 extralen, bool reply)
1372aa72f3bSAlexey Klimov {
1382aa72f3bSAlexey Klimov int retval;
1392aa72f3bSAlexey Klimov int size;
1402aa72f3bSAlexey Klimov
1412aa72f3bSAlexey Klimov radio->buffer[0] = 0x00;
1422aa72f3bSAlexey Klimov radio->buffer[1] = 0x55;
1432aa72f3bSAlexey Klimov radio->buffer[2] = 0xaa;
1444bd9ff19SHans Verkuil radio->buffer[3] = extralen;
1454bd9ff19SHans Verkuil radio->buffer[4] = cmd;
1464bd9ff19SHans Verkuil radio->buffer[5] = arg;
1472aa72f3bSAlexey Klimov radio->buffer[6] = 0x00;
1484bd9ff19SHans Verkuil radio->buffer[7] = extra || reply ? 8 : 0;
1492aa72f3bSAlexey Klimov
1502aa72f3bSAlexey Klimov retval = usb_bulk_msg(radio->usbdev, usb_sndintpipe(radio->usbdev, 2),
1514bd9ff19SHans Verkuil radio->buffer, BUFFER_LENGTH, &size, USB_TIMEOUT);
1522aa72f3bSAlexey Klimov
153e57458dcSAlexey Klimov if (retval < 0 || size != BUFFER_LENGTH) {
1544bd9ff19SHans Verkuil if (video_is_registered(&radio->vdev))
1554bd9ff19SHans Verkuil amradio_dev_warn(&radio->vdev.dev,
1564bd9ff19SHans Verkuil "cmd %02x failed\n", cmd);
1574bd9ff19SHans Verkuil return retval ? retval : -EIO;
1582aa72f3bSAlexey Klimov }
1594bd9ff19SHans Verkuil if (!extra && !reply)
16085578b0fSHans Verkuil return 0;
1614bd9ff19SHans Verkuil
1624bd9ff19SHans Verkuil if (extra) {
1634bd9ff19SHans Verkuil memcpy(radio->buffer, extra, extralen);
1644bd9ff19SHans Verkuil memset(radio->buffer + extralen, 0, 8 - extralen);
1654bd9ff19SHans Verkuil retval = usb_bulk_msg(radio->usbdev, usb_sndintpipe(radio->usbdev, 2),
1664bd9ff19SHans Verkuil radio->buffer, BUFFER_LENGTH, &size, USB_TIMEOUT);
1674bd9ff19SHans Verkuil } else {
1684bd9ff19SHans Verkuil memset(radio->buffer, 0, 8);
1694bd9ff19SHans Verkuil retval = usb_bulk_msg(radio->usbdev, usb_rcvbulkpipe(radio->usbdev, 0x81),
1704bd9ff19SHans Verkuil radio->buffer, BUFFER_LENGTH, &size, USB_TIMEOUT);
1714bd9ff19SHans Verkuil }
1724bd9ff19SHans Verkuil if (retval == 0 && size == BUFFER_LENGTH)
1734bd9ff19SHans Verkuil return 0;
1744bd9ff19SHans Verkuil if (video_is_registered(&radio->vdev) && cmd != AMRADIO_GET_READY_FLAG)
1754bd9ff19SHans Verkuil amradio_dev_warn(&radio->vdev.dev, "follow-up to cmd %02x failed\n", cmd);
1764bd9ff19SHans Verkuil return retval ? retval : -EIO;
1774bd9ff19SHans Verkuil }
1784bd9ff19SHans Verkuil
1794bd9ff19SHans Verkuil /* switch on/off the radio. Send 8 bytes to device */
amradio_set_mute(struct amradio_device * radio,bool mute)1804bd9ff19SHans Verkuil static int amradio_set_mute(struct amradio_device *radio, bool mute)
1814bd9ff19SHans Verkuil {
1824bd9ff19SHans Verkuil int ret = amradio_send_cmd(radio,
1834bd9ff19SHans Verkuil AMRADIO_SET_MUTE, mute, NULL, 0, false);
1844bd9ff19SHans Verkuil
1854bd9ff19SHans Verkuil if (!ret)
1864bd9ff19SHans Verkuil radio->muted = mute;
1874bd9ff19SHans Verkuil return ret;
1882aa72f3bSAlexey Klimov }
1892aa72f3bSAlexey Klimov
1902aa72f3bSAlexey Klimov /* set a frequency, freq is defined by v4l's TUNER_LOW, i.e. 1/16th kHz */
amradio_set_freq(struct amradio_device * radio,int freq)1914bd9ff19SHans Verkuil static int amradio_set_freq(struct amradio_device *radio, int freq)
1922aa72f3bSAlexey Klimov {
1934b0cac5eSAlexey Klimov unsigned short freq_send;
1944bd9ff19SHans Verkuil u8 buf[3];
1952aa72f3bSAlexey Klimov int retval;
1962aa72f3bSAlexey Klimov
1974b0cac5eSAlexey Klimov /* we need to be sure that frequency isn't out of range */
1984b0cac5eSAlexey Klimov freq = clamp_t(unsigned, freq, FREQ_MIN * FREQ_MUL, FREQ_MAX * FREQ_MUL);
1994b0cac5eSAlexey Klimov freq_send = 0x10 + (freq >> 3) / 25;
2004b0cac5eSAlexey Klimov
2012aa72f3bSAlexey Klimov /* frequency is calculated from freq_send and placed in first 2 bytes */
2024bd9ff19SHans Verkuil buf[0] = (freq_send >> 8) & 0xff;
2034bd9ff19SHans Verkuil buf[1] = freq_send & 0xff;
2044bd9ff19SHans Verkuil buf[2] = 0x01;
2052aa72f3bSAlexey Klimov
2064bd9ff19SHans Verkuil retval = amradio_send_cmd(radio, AMRADIO_SET_FREQ, 0, buf, 3, false);
2074bd9ff19SHans Verkuil if (retval)
2084bd9ff19SHans Verkuil return retval;
209798166dbSDavid Ellingsworth radio->curfreq = freq;
2104bd9ff19SHans Verkuil msleep(40);
21185578b0fSHans Verkuil return 0;
21285578b0fSHans Verkuil }
2131bb16d71SAlexey Klimov
amradio_set_stereo(struct amradio_device * radio,bool stereo)2144bd9ff19SHans Verkuil static int amradio_set_stereo(struct amradio_device *radio, bool stereo)
2151bb16d71SAlexey Klimov {
2164bd9ff19SHans Verkuil int ret = amradio_send_cmd(radio,
2174bd9ff19SHans Verkuil AMRADIO_SET_MONO, !stereo, NULL, 0, false);
2181bb16d71SAlexey Klimov
2194bd9ff19SHans Verkuil if (!ret)
2204bd9ff19SHans Verkuil radio->stereo = stereo;
2214bd9ff19SHans Verkuil return ret;
2222aa72f3bSAlexey Klimov }
2232aa72f3bSAlexey Klimov
amradio_get_stat(struct amradio_device * radio,bool * is_stereo,u32 * signal)22477cf3934SHans Verkuil static int amradio_get_stat(struct amradio_device *radio, bool *is_stereo, u32 *signal)
22577cf3934SHans Verkuil {
2264bd9ff19SHans Verkuil int ret = amradio_send_cmd(radio,
2274bd9ff19SHans Verkuil AMRADIO_GET_SIGNAL, 0, NULL, 0, true);
22877cf3934SHans Verkuil
2294bd9ff19SHans Verkuil if (ret)
2304bd9ff19SHans Verkuil return ret;
23177cf3934SHans Verkuil *is_stereo = radio->buffer[2] >> 7;
23277cf3934SHans Verkuil *signal = (radio->buffer[3] & 0xf0) << 8;
23377cf3934SHans Verkuil return 0;
23477cf3934SHans Verkuil }
23577cf3934SHans Verkuil
23671f07d94SAlexey Klimov /* Handle unplugging the device.
23771f07d94SAlexey Klimov * We call video_unregister_device in any case.
23871f07d94SAlexey Klimov * The last function called in this procedure is
23971f07d94SAlexey Klimov * usb_amradio_device_release.
24071f07d94SAlexey Klimov */
usb_amradio_disconnect(struct usb_interface * intf)2412aa72f3bSAlexey Klimov static void usb_amradio_disconnect(struct usb_interface *intf)
2422aa72f3bSAlexey Klimov {
243a2bdc5e6SHans Verkuil struct amradio_device *radio = to_amradio_dev(usb_get_intfdata(intf));
2442aa72f3bSAlexey Klimov
245f4e9043eSAlexey Klimov mutex_lock(&radio->lock);
24685578b0fSHans Verkuil video_unregister_device(&radio->vdev);
2474bd9ff19SHans Verkuil amradio_set_mute(radio, true);
2484bd9ff19SHans Verkuil usb_set_intfdata(intf, NULL);
249b466248dSAlexey Klimov v4l2_device_disconnect(&radio->v4l2_dev);
250a682d4cbSHans Verkuil mutex_unlock(&radio->lock);
25185578b0fSHans Verkuil v4l2_device_put(&radio->v4l2_dev);
2522aa72f3bSAlexey Klimov }
2532aa72f3bSAlexey Klimov
2542aa72f3bSAlexey Klimov /* vidioc_querycap - query device capabilities */
vidioc_querycap(struct file * file,void * priv,struct v4l2_capability * v)2552aa72f3bSAlexey Klimov static int vidioc_querycap(struct file *file, void *priv,
2562aa72f3bSAlexey Klimov struct v4l2_capability *v)
2572aa72f3bSAlexey Klimov {
25885578b0fSHans Verkuil struct amradio_device *radio = video_drvdata(file);
259c7181cfaSAlexey Klimov
260c0decac1SMauro Carvalho Chehab strscpy(v->driver, "radio-mr800", sizeof(v->driver));
261c0decac1SMauro Carvalho Chehab strscpy(v->card, "AverMedia MR 800 USB FM Radio", sizeof(v->card));
262c7181cfaSAlexey Klimov usb_make_path(radio->usbdev, v->bus_info, sizeof(v->bus_info));
2632aa72f3bSAlexey Klimov return 0;
2642aa72f3bSAlexey Klimov }
2652aa72f3bSAlexey Klimov
2662aa72f3bSAlexey Klimov /* vidioc_g_tuner - get tuner attributes */
vidioc_g_tuner(struct file * file,void * priv,struct v4l2_tuner * v)2672aa72f3bSAlexey Klimov static int vidioc_g_tuner(struct file *file, void *priv,
2682aa72f3bSAlexey Klimov struct v4l2_tuner *v)
2692aa72f3bSAlexey Klimov {
27085578b0fSHans Verkuil struct amradio_device *radio = video_drvdata(file);
27177cf3934SHans Verkuil bool is_stereo = false;
27277cf3934SHans Verkuil int retval;
2732aa72f3bSAlexey Klimov
2742aa72f3bSAlexey Klimov if (v->index > 0)
2752aa72f3bSAlexey Klimov return -EINVAL;
2762aa72f3bSAlexey Klimov
27777cf3934SHans Verkuil v->signal = 0;
27877cf3934SHans Verkuil retval = amradio_get_stat(radio, &is_stereo, &v->signal);
27977cf3934SHans Verkuil if (retval)
28077cf3934SHans Verkuil return retval;
28177cf3934SHans Verkuil
282cc1e6315SMauro Carvalho Chehab strscpy(v->name, "FM", sizeof(v->name));
2832aa72f3bSAlexey Klimov v->type = V4L2_TUNER_RADIO;
2842aa72f3bSAlexey Klimov v->rangelow = FREQ_MIN * FREQ_MUL;
2852aa72f3bSAlexey Klimov v->rangehigh = FREQ_MAX * FREQ_MUL;
28654f6019bSHans Verkuil v->capability = V4L2_TUNER_CAP_LOW | V4L2_TUNER_CAP_STEREO |
28754f6019bSHans Verkuil V4L2_TUNER_CAP_HWSEEK_WRAP;
28877cf3934SHans Verkuil v->rxsubchans = is_stereo ? V4L2_TUNER_SUB_STEREO : V4L2_TUNER_SUB_MONO;
28985578b0fSHans Verkuil v->audmode = radio->stereo ?
29085578b0fSHans Verkuil V4L2_TUNER_MODE_STEREO : V4L2_TUNER_MODE_MONO;
29185578b0fSHans Verkuil return 0;
2922aa72f3bSAlexey Klimov }
2932aa72f3bSAlexey Klimov
2942aa72f3bSAlexey Klimov /* vidioc_s_tuner - set tuner attributes */
vidioc_s_tuner(struct file * file,void * priv,const struct v4l2_tuner * v)2952aa72f3bSAlexey Klimov static int vidioc_s_tuner(struct file *file, void *priv,
2962f73c7c5SHans Verkuil const struct v4l2_tuner *v)
2972aa72f3bSAlexey Klimov {
29885578b0fSHans Verkuil struct amradio_device *radio = video_drvdata(file);
2993480130aSAlexey Klimov
3002aa72f3bSAlexey Klimov if (v->index > 0)
3012aa72f3bSAlexey Klimov return -EINVAL;
3021bb16d71SAlexey Klimov
3031bb16d71SAlexey Klimov /* mono/stereo selector */
3041bb16d71SAlexey Klimov switch (v->audmode) {
3051bb16d71SAlexey Klimov case V4L2_TUNER_MODE_MONO:
30685578b0fSHans Verkuil return amradio_set_stereo(radio, WANT_MONO);
30785578b0fSHans Verkuil default:
30885578b0fSHans Verkuil return amradio_set_stereo(radio, WANT_STEREO);
3091bb16d71SAlexey Klimov }
3102aa72f3bSAlexey Klimov }
3112aa72f3bSAlexey Klimov
3122aa72f3bSAlexey Klimov /* vidioc_s_frequency - set tuner radio frequency */
vidioc_s_frequency(struct file * file,void * priv,const struct v4l2_frequency * f)3132aa72f3bSAlexey Klimov static int vidioc_s_frequency(struct file *file, void *priv,
314b530a447SHans Verkuil const struct v4l2_frequency *f)
3152aa72f3bSAlexey Klimov {
31685578b0fSHans Verkuil struct amradio_device *radio = video_drvdata(file);
3172aa72f3bSAlexey Klimov
31885578b0fSHans Verkuil if (f->tuner != 0)
319a3a9e287SHans Verkuil return -EINVAL;
3204b0cac5eSAlexey Klimov return amradio_set_freq(radio, f->frequency);
3212aa72f3bSAlexey Klimov }
3222aa72f3bSAlexey Klimov
3232aa72f3bSAlexey Klimov /* vidioc_g_frequency - get tuner radio frequency */
vidioc_g_frequency(struct file * file,void * priv,struct v4l2_frequency * f)3242aa72f3bSAlexey Klimov static int vidioc_g_frequency(struct file *file, void *priv,
3252aa72f3bSAlexey Klimov struct v4l2_frequency *f)
3262aa72f3bSAlexey Klimov {
32785578b0fSHans Verkuil struct amradio_device *radio = video_drvdata(file);
3283480130aSAlexey Klimov
32985578b0fSHans Verkuil if (f->tuner != 0 || f->type != V4L2_TUNER_RADIO)
330a3a9e287SHans Verkuil return -EINVAL;
3312aa72f3bSAlexey Klimov f->type = V4L2_TUNER_RADIO;
3322aa72f3bSAlexey Klimov f->frequency = radio->curfreq;
3334aebc289SDavid Ellingsworth
3342aa72f3bSAlexey Klimov return 0;
3352aa72f3bSAlexey Klimov }
3362aa72f3bSAlexey Klimov
vidioc_s_hw_freq_seek(struct file * file,void * priv,const struct v4l2_hw_freq_seek * seek)3374bd9ff19SHans Verkuil static int vidioc_s_hw_freq_seek(struct file *file, void *priv,
338ec6f4328SHans Verkuil const struct v4l2_hw_freq_seek *seek)
3394bd9ff19SHans Verkuil {
3404bd9ff19SHans Verkuil static u8 buf[8] = {
3414bd9ff19SHans Verkuil 0x3d, 0x32, 0x0f, 0x08, 0x3d, 0x32, 0x0f, 0x08
3424bd9ff19SHans Verkuil };
3434bd9ff19SHans Verkuil struct amradio_device *radio = video_drvdata(file);
3444bd9ff19SHans Verkuil unsigned long timeout;
3454bd9ff19SHans Verkuil int retval;
3464bd9ff19SHans Verkuil
3474bd9ff19SHans Verkuil if (seek->tuner != 0 || !seek->wrap_around)
3484bd9ff19SHans Verkuil return -EINVAL;
3494bd9ff19SHans Verkuil
350617ade61SHans Verkuil if (file->f_flags & O_NONBLOCK)
351617ade61SHans Verkuil return -EWOULDBLOCK;
352617ade61SHans Verkuil
3534bd9ff19SHans Verkuil retval = amradio_send_cmd(radio,
3544bd9ff19SHans Verkuil AMRADIO_SET_SEARCH_LVL, 0, buf, 8, false);
3554bd9ff19SHans Verkuil if (retval)
3564bd9ff19SHans Verkuil return retval;
3574bd9ff19SHans Verkuil amradio_set_freq(radio, radio->curfreq);
3584bd9ff19SHans Verkuil retval = amradio_send_cmd(radio,
3594bd9ff19SHans Verkuil seek->seek_upward ? AMRADIO_SET_SEARCH_UP : AMRADIO_SET_SEARCH_DOWN,
3604bd9ff19SHans Verkuil 0, NULL, 0, false);
3614bd9ff19SHans Verkuil if (retval)
3624bd9ff19SHans Verkuil return retval;
3634bd9ff19SHans Verkuil timeout = jiffies + msecs_to_jiffies(30000);
3644bd9ff19SHans Verkuil for (;;) {
3654bd9ff19SHans Verkuil if (time_after(jiffies, timeout)) {
36654f6019bSHans Verkuil retval = -ENODATA;
3674bd9ff19SHans Verkuil break;
3684bd9ff19SHans Verkuil }
3694bd9ff19SHans Verkuil if (schedule_timeout_interruptible(msecs_to_jiffies(10))) {
3704bd9ff19SHans Verkuil retval = -ERESTARTSYS;
3714bd9ff19SHans Verkuil break;
3724bd9ff19SHans Verkuil }
3734bd9ff19SHans Verkuil retval = amradio_send_cmd(radio, AMRADIO_GET_READY_FLAG,
3744bd9ff19SHans Verkuil 0, NULL, 0, true);
3754bd9ff19SHans Verkuil if (retval)
3764bd9ff19SHans Verkuil continue;
3774bd9ff19SHans Verkuil amradio_send_cmd(radio, AMRADIO_GET_FREQ, 0, NULL, 0, true);
3784bd9ff19SHans Verkuil if (radio->buffer[1] || radio->buffer[2]) {
3794b0cac5eSAlexey Klimov /* To check: sometimes radio->curfreq is set to out of range value */
3804bd9ff19SHans Verkuil radio->curfreq = (radio->buffer[1] << 8) | radio->buffer[2];
3814bd9ff19SHans Verkuil radio->curfreq = (radio->curfreq - 0x10) * 200;
3824bd9ff19SHans Verkuil amradio_send_cmd(radio, AMRADIO_STOP_SEARCH,
3834bd9ff19SHans Verkuil 0, NULL, 0, false);
3844bd9ff19SHans Verkuil amradio_set_freq(radio, radio->curfreq);
3854bd9ff19SHans Verkuil retval = 0;
3864bd9ff19SHans Verkuil break;
3874bd9ff19SHans Verkuil }
3884bd9ff19SHans Verkuil }
3894bd9ff19SHans Verkuil amradio_send_cmd(radio, AMRADIO_STOP_SEARCH, 0, NULL, 0, false);
3904bd9ff19SHans Verkuil amradio_set_freq(radio, radio->curfreq);
3914bd9ff19SHans Verkuil return retval;
3924bd9ff19SHans Verkuil }
3934bd9ff19SHans Verkuil
usb_amradio_s_ctrl(struct v4l2_ctrl * ctrl)39485578b0fSHans Verkuil static int usb_amradio_s_ctrl(struct v4l2_ctrl *ctrl)
3952aa72f3bSAlexey Klimov {
39685578b0fSHans Verkuil struct amradio_device *radio =
39785578b0fSHans Verkuil container_of(ctrl->handler, struct amradio_device, hdl);
3983480130aSAlexey Klimov
3992aa72f3bSAlexey Klimov switch (ctrl->id) {
4002aa72f3bSAlexey Klimov case V4L2_CID_AUDIO_MUTE:
4014bd9ff19SHans Verkuil return amradio_set_mute(radio, ctrl->val);
4022aa72f3bSAlexey Klimov }
4034aebc289SDavid Ellingsworth
4042aa72f3bSAlexey Klimov return -EINVAL;
4052aa72f3bSAlexey Klimov }
4062aa72f3bSAlexey Klimov
usb_amradio_init(struct amradio_device * radio)407502d5016SDavid Ellingsworth static int usb_amradio_init(struct amradio_device *radio)
408502d5016SDavid Ellingsworth {
409502d5016SDavid Ellingsworth int retval;
410502d5016SDavid Ellingsworth
4114bd9ff19SHans Verkuil retval = amradio_set_mute(radio, true);
41216a72f41SDavid Ellingsworth if (retval)
413502d5016SDavid Ellingsworth goto out_err;
4144bd9ff19SHans Verkuil retval = amradio_set_stereo(radio, true);
41516a72f41SDavid Ellingsworth if (retval)
416502d5016SDavid Ellingsworth goto out_err;
4174bd9ff19SHans Verkuil retval = amradio_set_freq(radio, radio->curfreq);
4184bd9ff19SHans Verkuil if (retval)
4194bd9ff19SHans Verkuil goto out_err;
4204bd9ff19SHans Verkuil return 0;
421502d5016SDavid Ellingsworth
422502d5016SDavid Ellingsworth out_err:
42385578b0fSHans Verkuil amradio_dev_err(&radio->vdev.dev, "initialization failed\n");
424502d5016SDavid Ellingsworth return retval;
425502d5016SDavid Ellingsworth }
426502d5016SDavid Ellingsworth
4272aa72f3bSAlexey Klimov /* Suspend device - stop device. Need to be checked and fixed */
usb_amradio_suspend(struct usb_interface * intf,pm_message_t message)4282aa72f3bSAlexey Klimov static int usb_amradio_suspend(struct usb_interface *intf, pm_message_t message)
4292aa72f3bSAlexey Klimov {
430a2bdc5e6SHans Verkuil struct amradio_device *radio = to_amradio_dev(usb_get_intfdata(intf));
4312aa72f3bSAlexey Klimov
432a682d4cbSHans Verkuil mutex_lock(&radio->lock);
43385578b0fSHans Verkuil if (!radio->muted) {
4344bd9ff19SHans Verkuil amradio_set_mute(radio, true);
4354bd9ff19SHans Verkuil radio->muted = false;
436d8970e5fSDavid Ellingsworth }
437a682d4cbSHans Verkuil mutex_unlock(&radio->lock);
4382aa72f3bSAlexey Klimov
439e60b022eSAlexey Klimov dev_info(&intf->dev, "going into suspend..\n");
4402aa72f3bSAlexey Klimov return 0;
4412aa72f3bSAlexey Klimov }
4422aa72f3bSAlexey Klimov
4432aa72f3bSAlexey Klimov /* Resume device - start device. Need to be checked and fixed */
usb_amradio_resume(struct usb_interface * intf)4442aa72f3bSAlexey Klimov static int usb_amradio_resume(struct usb_interface *intf)
4452aa72f3bSAlexey Klimov {
446a2bdc5e6SHans Verkuil struct amradio_device *radio = to_amradio_dev(usb_get_intfdata(intf));
4472aa72f3bSAlexey Klimov
448a682d4cbSHans Verkuil mutex_lock(&radio->lock);
4494bd9ff19SHans Verkuil amradio_set_stereo(radio, radio->stereo);
4504bd9ff19SHans Verkuil amradio_set_freq(radio, radio->curfreq);
451d8970e5fSDavid Ellingsworth
45216a72f41SDavid Ellingsworth if (!radio->muted)
4534bd9ff19SHans Verkuil amradio_set_mute(radio, false);
4542aa72f3bSAlexey Klimov
455a682d4cbSHans Verkuil mutex_unlock(&radio->lock);
456a682d4cbSHans Verkuil
457e60b022eSAlexey Klimov dev_info(&intf->dev, "coming out of suspend..\n");
4582aa72f3bSAlexey Klimov return 0;
4592aa72f3bSAlexey Klimov }
4602aa72f3bSAlexey Klimov
46185578b0fSHans Verkuil static const struct v4l2_ctrl_ops usb_amradio_ctrl_ops = {
46285578b0fSHans Verkuil .s_ctrl = usb_amradio_s_ctrl,
46385578b0fSHans Verkuil };
46485578b0fSHans Verkuil
4652aa72f3bSAlexey Klimov /* File system interface */
466bec43661SHans Verkuil static const struct v4l2_file_operations usb_amradio_fops = {
4672aa72f3bSAlexey Klimov .owner = THIS_MODULE,
46885578b0fSHans Verkuil .open = v4l2_fh_open,
46985578b0fSHans Verkuil .release = v4l2_fh_release,
47085578b0fSHans Verkuil .poll = v4l2_ctrl_poll,
471361ae54fSHans Verkuil .unlocked_ioctl = video_ioctl2,
4722aa72f3bSAlexey Klimov };
4732aa72f3bSAlexey Klimov
4742aa72f3bSAlexey Klimov static const struct v4l2_ioctl_ops usb_amradio_ioctl_ops = {
4752aa72f3bSAlexey Klimov .vidioc_querycap = vidioc_querycap,
4762aa72f3bSAlexey Klimov .vidioc_g_tuner = vidioc_g_tuner,
4772aa72f3bSAlexey Klimov .vidioc_s_tuner = vidioc_s_tuner,
4782aa72f3bSAlexey Klimov .vidioc_g_frequency = vidioc_g_frequency,
4792aa72f3bSAlexey Klimov .vidioc_s_frequency = vidioc_s_frequency,
4804bd9ff19SHans Verkuil .vidioc_s_hw_freq_seek = vidioc_s_hw_freq_seek,
48185578b0fSHans Verkuil .vidioc_log_status = v4l2_ctrl_log_status,
48285578b0fSHans Verkuil .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
48385578b0fSHans Verkuil .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
4842aa72f3bSAlexey Klimov };
4852aa72f3bSAlexey Klimov
usb_amradio_release(struct v4l2_device * v4l2_dev)48685578b0fSHans Verkuil static void usb_amradio_release(struct v4l2_device *v4l2_dev)
487f4e9043eSAlexey Klimov {
48885578b0fSHans Verkuil struct amradio_device *radio = to_amradio_dev(v4l2_dev);
489b466248dSAlexey Klimov
490f4e9043eSAlexey Klimov /* free rest memory */
49185578b0fSHans Verkuil v4l2_ctrl_handler_free(&radio->hdl);
49285578b0fSHans Verkuil v4l2_device_unregister(&radio->v4l2_dev);
493f4e9043eSAlexey Klimov kfree(radio->buffer);
494f4e9043eSAlexey Klimov kfree(radio);
495f4e9043eSAlexey Klimov }
496f4e9043eSAlexey Klimov
497a5d69475SAlexey Klimov /* check if the device is present and register with v4l and usb if it is */
usb_amradio_probe(struct usb_interface * intf,const struct usb_device_id * id)4982aa72f3bSAlexey Klimov static int usb_amradio_probe(struct usb_interface *intf,
4992aa72f3bSAlexey Klimov const struct usb_device_id *id)
5002aa72f3bSAlexey Klimov {
5012aa72f3bSAlexey Klimov struct amradio_device *radio;
5029aa4d4eaSMarkus Elfring int retval;
5032aa72f3bSAlexey Klimov
504b466248dSAlexey Klimov radio = kzalloc(sizeof(struct amradio_device), GFP_KERNEL);
5052aa72f3bSAlexey Klimov
5068edafcc6SAlexey Klimov if (!radio) {
5078edafcc6SAlexey Klimov dev_err(&intf->dev, "kmalloc for amradio_device failed\n");
5081b8bbb3cSDavid Ellingsworth retval = -ENOMEM;
5091b8bbb3cSDavid Ellingsworth goto err;
5108edafcc6SAlexey Klimov }
5112aa72f3bSAlexey Klimov
5122aa72f3bSAlexey Klimov radio->buffer = kmalloc(BUFFER_LENGTH, GFP_KERNEL);
5132aa72f3bSAlexey Klimov
5148edafcc6SAlexey Klimov if (!radio->buffer) {
5158edafcc6SAlexey Klimov dev_err(&intf->dev, "kmalloc for radio->buffer failed\n");
5161b8bbb3cSDavid Ellingsworth retval = -ENOMEM;
5171b8bbb3cSDavid Ellingsworth goto err_nobuf;
5182aa72f3bSAlexey Klimov }
5192aa72f3bSAlexey Klimov
520d1939e4cSDavid Ellingsworth retval = v4l2_device_register(&intf->dev, &radio->v4l2_dev);
521b466248dSAlexey Klimov if (retval < 0) {
522b466248dSAlexey Klimov dev_err(&intf->dev, "couldn't register v4l2_device\n");
5231b8bbb3cSDavid Ellingsworth goto err_v4l2;
524b466248dSAlexey Klimov }
525b466248dSAlexey Klimov
52685578b0fSHans Verkuil v4l2_ctrl_handler_init(&radio->hdl, 1);
52785578b0fSHans Verkuil v4l2_ctrl_new_std(&radio->hdl, &usb_amradio_ctrl_ops,
52885578b0fSHans Verkuil V4L2_CID_AUDIO_MUTE, 0, 1, 1, 1);
52985578b0fSHans Verkuil if (radio->hdl.error) {
53085578b0fSHans Verkuil retval = radio->hdl.error;
53185578b0fSHans Verkuil dev_err(&intf->dev, "couldn't register control\n");
53285578b0fSHans Verkuil goto err_ctrl;
53385578b0fSHans Verkuil }
534361ae54fSHans Verkuil mutex_init(&radio->lock);
535361ae54fSHans Verkuil
53685578b0fSHans Verkuil radio->v4l2_dev.ctrl_handler = &radio->hdl;
53785578b0fSHans Verkuil radio->v4l2_dev.release = usb_amradio_release;
538c0decac1SMauro Carvalho Chehab strscpy(radio->vdev.name, radio->v4l2_dev.name,
53985578b0fSHans Verkuil sizeof(radio->vdev.name));
54085578b0fSHans Verkuil radio->vdev.v4l2_dev = &radio->v4l2_dev;
54185578b0fSHans Verkuil radio->vdev.fops = &usb_amradio_fops;
54285578b0fSHans Verkuil radio->vdev.ioctl_ops = &usb_amradio_ioctl_ops;
54385578b0fSHans Verkuil radio->vdev.release = video_device_release_empty;
54485578b0fSHans Verkuil radio->vdev.lock = &radio->lock;
545*e83ce300SHans Verkuil radio->vdev.device_caps = V4L2_CAP_RADIO | V4L2_CAP_TUNER |
546*e83ce300SHans Verkuil V4L2_CAP_HW_FREQ_SEEK;
5472aa72f3bSAlexey Klimov
5482aa72f3bSAlexey Klimov radio->usbdev = interface_to_usbdev(intf);
549d89ce0d9SOliver Neukum radio->intf = intf;
55085578b0fSHans Verkuil usb_set_intfdata(intf, &radio->v4l2_dev);
5512aa72f3bSAlexey Klimov radio->curfreq = 95.16 * FREQ_MUL;
5522aa72f3bSAlexey Klimov
55385578b0fSHans Verkuil video_set_drvdata(&radio->vdev, radio);
55485578b0fSHans Verkuil retval = usb_amradio_init(radio);
55585578b0fSHans Verkuil if (retval)
55685578b0fSHans Verkuil goto err_vdev;
557b466248dSAlexey Klimov
55885578b0fSHans Verkuil retval = video_register_device(&radio->vdev, VFL_TYPE_RADIO,
5597a7d92e0SDavid Ellingsworth radio_nr);
560a5d69475SAlexey Klimov if (retval < 0) {
56165c51dc9SAlexey Klimov dev_err(&intf->dev, "could not register video device\n");
5621b8bbb3cSDavid Ellingsworth goto err_vdev;
5632aa72f3bSAlexey Klimov }
5642aa72f3bSAlexey Klimov
5652aa72f3bSAlexey Klimov return 0;
5661b8bbb3cSDavid Ellingsworth
5671b8bbb3cSDavid Ellingsworth err_vdev:
56885578b0fSHans Verkuil v4l2_ctrl_handler_free(&radio->hdl);
56985578b0fSHans Verkuil err_ctrl:
570d1939e4cSDavid Ellingsworth v4l2_device_unregister(&radio->v4l2_dev);
5711b8bbb3cSDavid Ellingsworth err_v4l2:
5721b8bbb3cSDavid Ellingsworth kfree(radio->buffer);
5731b8bbb3cSDavid Ellingsworth err_nobuf:
5741b8bbb3cSDavid Ellingsworth kfree(radio);
5751b8bbb3cSDavid Ellingsworth err:
5761b8bbb3cSDavid Ellingsworth return retval;
5772aa72f3bSAlexey Klimov }
5782aa72f3bSAlexey Klimov
57985578b0fSHans Verkuil /* USB Device ID List */
5801ab2234eSArvind Yadav static const struct usb_device_id usb_amradio_device_table[] = {
58185578b0fSHans Verkuil { USB_DEVICE_AND_INTERFACE_INFO(USB_AMRADIO_VENDOR, USB_AMRADIO_PRODUCT,
58285578b0fSHans Verkuil USB_CLASS_HID, 0, 0) },
58385578b0fSHans Verkuil { } /* Terminating entry */
58485578b0fSHans Verkuil };
58585578b0fSHans Verkuil
58685578b0fSHans Verkuil MODULE_DEVICE_TABLE(usb, usb_amradio_device_table);
58785578b0fSHans Verkuil
58885578b0fSHans Verkuil /* USB subsystem interface */
58985578b0fSHans Verkuil static struct usb_driver usb_amradio_driver = {
59085578b0fSHans Verkuil .name = MR800_DRIVER_NAME,
59185578b0fSHans Verkuil .probe = usb_amradio_probe,
59285578b0fSHans Verkuil .disconnect = usb_amradio_disconnect,
59385578b0fSHans Verkuil .suspend = usb_amradio_suspend,
59485578b0fSHans Verkuil .resume = usb_amradio_resume,
59585578b0fSHans Verkuil .reset_resume = usb_amradio_resume,
59685578b0fSHans Verkuil .id_table = usb_amradio_device_table,
59785578b0fSHans Verkuil };
59885578b0fSHans Verkuil
599ecb3b2b3SGreg Kroah-Hartman module_usb_driver(usb_amradio_driver);
600