Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * Copyright 2013 Cisco Systems, Inc. and/or its affiliates. All rights reserved.
0004  */
0005 
0006 #include <linux/kernel.h>
0007 #include <linux/module.h>
0008 #include <linux/init.h>
0009 #include <linux/slab.h>
0010 #include <linux/input.h>
0011 #include <linux/usb.h>
0012 #include <linux/hid.h>
0013 #include <linux/mutex.h>
0014 #include <linux/videodev2.h>
0015 #include <asm/unaligned.h>
0016 #include <media/v4l2-device.h>
0017 #include <media/v4l2-ioctl.h>
0018 #include <media/v4l2-ctrls.h>
0019 #include <media/v4l2-event.h>
0020 
0021 /*
0022  * 'Thanko's Raremono' is a Japanese si4734-based AM/FM/SW USB receiver:
0023  *
0024  * http://www.raremono.jp/product/484.html/
0025  *
0026  * The USB protocol has been reversed engineered using wireshark, initially
0027  * by Dinesh Ram <dinesh.ram@cern.ch> and finished by Hans Verkuil
0028  * <hverkuil@xs4all.nl>.
0029  *
0030  * Sadly the firmware used in this product hides lots of goodies since the
0031  * si4734 has more features than are supported by the firmware. Oh well...
0032  */
0033 
0034 /* driver and module definitions */
0035 MODULE_AUTHOR("Hans Verkuil <hverkuil@xs4all.nl>");
0036 MODULE_DESCRIPTION("Thanko's Raremono AM/FM/SW Receiver USB driver");
0037 MODULE_LICENSE("GPL v2");
0038 
0039 /*
0040  * The Device announces itself as Cygnal Integrated Products, Inc.
0041  *
0042  * The vendor and product IDs (and in fact all other lsusb information as
0043  * well) are identical to the si470x Silicon Labs USB FM Radio Reference
0044  * Design board, even though this card has a si4734 device. Clearly the
0045  * designer of this product never bothered to change the USB IDs.
0046  */
0047 
0048 /* USB Device ID List */
0049 static const struct usb_device_id usb_raremono_device_table[] = {
0050     {USB_DEVICE_AND_INTERFACE_INFO(0x10c4, 0x818a, USB_CLASS_HID, 0, 0) },
0051     { }                     /* Terminating entry */
0052 };
0053 
0054 MODULE_DEVICE_TABLE(usb, usb_raremono_device_table);
0055 
0056 #define BUFFER_LENGTH 64
0057 
0058 /* Timeout is set to a high value, could probably be reduced. Need more tests */
0059 #define USB_TIMEOUT 10000
0060 
0061 /* Frequency limits in KHz */
0062 #define FM_FREQ_RANGE_LOW   64000
0063 #define FM_FREQ_RANGE_HIGH  108000
0064 
0065 #define AM_FREQ_RANGE_LOW   520
0066 #define AM_FREQ_RANGE_HIGH  1710
0067 
0068 #define SW_FREQ_RANGE_LOW   2300
0069 #define SW_FREQ_RANGE_HIGH  26100
0070 
0071 enum { BAND_FM, BAND_AM, BAND_SW };
0072 
0073 static const struct v4l2_frequency_band bands[] = {
0074     /* Band FM */
0075     {
0076         .type = V4L2_TUNER_RADIO,
0077         .index = 0,
0078         .capability = V4L2_TUNER_CAP_LOW | V4L2_TUNER_CAP_STEREO |
0079                   V4L2_TUNER_CAP_FREQ_BANDS,
0080         .rangelow   = FM_FREQ_RANGE_LOW * 16,
0081         .rangehigh  = FM_FREQ_RANGE_HIGH * 16,
0082         .modulation = V4L2_BAND_MODULATION_FM,
0083     },
0084     /* Band AM */
0085     {
0086         .type = V4L2_TUNER_RADIO,
0087         .index = 1,
0088         .capability = V4L2_TUNER_CAP_LOW | V4L2_TUNER_CAP_FREQ_BANDS,
0089         .rangelow   = AM_FREQ_RANGE_LOW * 16,
0090         .rangehigh  = AM_FREQ_RANGE_HIGH * 16,
0091         .modulation = V4L2_BAND_MODULATION_AM,
0092     },
0093     /* Band SW */
0094     {
0095         .type = V4L2_TUNER_RADIO,
0096         .index = 2,
0097         .capability = V4L2_TUNER_CAP_LOW | V4L2_TUNER_CAP_FREQ_BANDS,
0098         .rangelow   = SW_FREQ_RANGE_LOW * 16,
0099         .rangehigh  = SW_FREQ_RANGE_HIGH * 16,
0100         .modulation = V4L2_BAND_MODULATION_AM,
0101     },
0102 };
0103 
0104 struct raremono_device {
0105     struct usb_device *usbdev;
0106     struct usb_interface *intf;
0107     struct video_device vdev;
0108     struct v4l2_device v4l2_dev;
0109     struct mutex lock;
0110 
0111     u8 *buffer;
0112     u32 band;
0113     unsigned curfreq;
0114 };
0115 
0116 static inline struct raremono_device *to_raremono_dev(struct v4l2_device *v4l2_dev)
0117 {
0118     return container_of(v4l2_dev, struct raremono_device, v4l2_dev);
0119 }
0120 
0121 /* Set frequency. */
0122 static int raremono_cmd_main(struct raremono_device *radio, unsigned band, unsigned freq)
0123 {
0124     unsigned band_offset;
0125     int ret;
0126 
0127     switch (band) {
0128     case BAND_FM:
0129         band_offset = 1;
0130         freq /= 10;
0131         break;
0132     case BAND_AM:
0133         band_offset = 0;
0134         break;
0135     default:
0136         band_offset = 2;
0137         break;
0138     }
0139     radio->buffer[0] = 0x04 + band_offset;
0140     radio->buffer[1] = freq >> 8;
0141     radio->buffer[2] = freq & 0xff;
0142 
0143     ret = usb_control_msg(radio->usbdev, usb_sndctrlpipe(radio->usbdev, 0),
0144             HID_REQ_SET_REPORT,
0145             USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_OUT,
0146             0x0300 + radio->buffer[0], 2,
0147             radio->buffer, 3, USB_TIMEOUT);
0148 
0149     if (ret < 0) {
0150         dev_warn(radio->v4l2_dev.dev, "%s failed (%d)\n", __func__, ret);
0151         return ret;
0152     }
0153     radio->curfreq = (band == BAND_FM) ? freq * 10 : freq;
0154     return 0;
0155 }
0156 
0157 /* Handle unplugging the device.
0158  * We call video_unregister_device in any case.
0159  * The last function called in this procedure is
0160  * usb_raremono_device_release.
0161  */
0162 static void usb_raremono_disconnect(struct usb_interface *intf)
0163 {
0164     struct raremono_device *radio = to_raremono_dev(usb_get_intfdata(intf));
0165 
0166     dev_info(&intf->dev, "Thanko's Raremono disconnected\n");
0167 
0168     mutex_lock(&radio->lock);
0169     usb_set_intfdata(intf, NULL);
0170     video_unregister_device(&radio->vdev);
0171     v4l2_device_disconnect(&radio->v4l2_dev);
0172     mutex_unlock(&radio->lock);
0173     v4l2_device_put(&radio->v4l2_dev);
0174 }
0175 
0176 /*
0177  * Linux Video interface
0178  */
0179 static int vidioc_querycap(struct file *file, void *priv,
0180                     struct v4l2_capability *v)
0181 {
0182     struct raremono_device *radio = video_drvdata(file);
0183 
0184     strscpy(v->driver, "radio-raremono", sizeof(v->driver));
0185     strscpy(v->card, "Thanko's Raremono", sizeof(v->card));
0186     usb_make_path(radio->usbdev, v->bus_info, sizeof(v->bus_info));
0187     return 0;
0188 }
0189 
0190 static int vidioc_enum_freq_bands(struct file *file, void *priv,
0191         struct v4l2_frequency_band *band)
0192 {
0193     if (band->tuner != 0)
0194         return -EINVAL;
0195 
0196     if (band->index >= ARRAY_SIZE(bands))
0197         return -EINVAL;
0198 
0199     *band = bands[band->index];
0200 
0201     return 0;
0202 }
0203 
0204 static int vidioc_g_tuner(struct file *file, void *priv,
0205         struct v4l2_tuner *v)
0206 {
0207     struct raremono_device *radio = video_drvdata(file);
0208     int ret;
0209 
0210     if (v->index > 0)
0211         return -EINVAL;
0212 
0213     strscpy(v->name, "AM/FM/SW", sizeof(v->name));
0214     v->capability = V4L2_TUNER_CAP_LOW | V4L2_TUNER_CAP_STEREO |
0215         V4L2_TUNER_CAP_FREQ_BANDS;
0216     v->rangelow = AM_FREQ_RANGE_LOW * 16;
0217     v->rangehigh = FM_FREQ_RANGE_HIGH * 16;
0218     v->rxsubchans = V4L2_TUNER_SUB_STEREO | V4L2_TUNER_SUB_MONO;
0219     v->audmode = (radio->curfreq < FM_FREQ_RANGE_LOW) ?
0220         V4L2_TUNER_MODE_MONO : V4L2_TUNER_MODE_STEREO;
0221     memset(radio->buffer, 1, BUFFER_LENGTH);
0222     ret = usb_control_msg(radio->usbdev, usb_rcvctrlpipe(radio->usbdev, 0),
0223             1, 0xa1, 0x030d, 2, radio->buffer, BUFFER_LENGTH, USB_TIMEOUT);
0224 
0225     if (ret < 0) {
0226         dev_warn(radio->v4l2_dev.dev, "%s failed (%d)\n", __func__, ret);
0227         return ret;
0228     }
0229     v->signal = ((radio->buffer[1] & 0xf) << 8 | radio->buffer[2]) << 4;
0230     return 0;
0231 }
0232 
0233 static int vidioc_s_tuner(struct file *file, void *priv,
0234                     const struct v4l2_tuner *v)
0235 {
0236     return v->index ? -EINVAL : 0;
0237 }
0238 
0239 static int vidioc_s_frequency(struct file *file, void *priv,
0240                 const struct v4l2_frequency *f)
0241 {
0242     struct raremono_device *radio = video_drvdata(file);
0243     u32 freq;
0244     unsigned band;
0245 
0246     if (f->tuner != 0 || f->type != V4L2_TUNER_RADIO)
0247         return -EINVAL;
0248 
0249     if (f->frequency >= (FM_FREQ_RANGE_LOW + SW_FREQ_RANGE_HIGH) * 8)
0250         band = BAND_FM;
0251     else if (f->frequency <= (AM_FREQ_RANGE_HIGH + SW_FREQ_RANGE_LOW) * 8)
0252         band = BAND_AM;
0253     else
0254         band = BAND_SW;
0255 
0256     freq = clamp_t(u32, f->frequency, bands[band].rangelow, bands[band].rangehigh);
0257     return raremono_cmd_main(radio, band, freq / 16);
0258 }
0259 
0260 static int vidioc_g_frequency(struct file *file, void *priv,
0261                 struct v4l2_frequency *f)
0262 {
0263     struct raremono_device *radio = video_drvdata(file);
0264 
0265     if (f->tuner != 0)
0266         return -EINVAL;
0267     f->type = V4L2_TUNER_RADIO;
0268     f->frequency = radio->curfreq * 16;
0269     return 0;
0270 }
0271 
0272 static void raremono_device_release(struct v4l2_device *v4l2_dev)
0273 {
0274     struct raremono_device *radio = to_raremono_dev(v4l2_dev);
0275 
0276     kfree(radio->buffer);
0277     kfree(radio);
0278 }
0279 
0280 /* File system interface */
0281 static const struct v4l2_file_operations usb_raremono_fops = {
0282     .owner      = THIS_MODULE,
0283     .open           = v4l2_fh_open,
0284     .release        = v4l2_fh_release,
0285     .unlocked_ioctl = video_ioctl2,
0286 };
0287 
0288 static const struct v4l2_ioctl_ops usb_raremono_ioctl_ops = {
0289     .vidioc_querycap = vidioc_querycap,
0290     .vidioc_g_tuner = vidioc_g_tuner,
0291     .vidioc_s_tuner = vidioc_s_tuner,
0292     .vidioc_g_frequency = vidioc_g_frequency,
0293     .vidioc_s_frequency = vidioc_s_frequency,
0294     .vidioc_enum_freq_bands = vidioc_enum_freq_bands,
0295 };
0296 
0297 /* check if the device is present and register with v4l and usb if it is */
0298 static int usb_raremono_probe(struct usb_interface *intf,
0299                 const struct usb_device_id *id)
0300 {
0301     struct raremono_device *radio;
0302     int retval = 0;
0303 
0304     radio = kzalloc(sizeof(*radio), GFP_KERNEL);
0305     if (!radio)
0306         return -ENOMEM;
0307     radio->buffer = kmalloc(BUFFER_LENGTH, GFP_KERNEL);
0308     if (!radio->buffer) {
0309         kfree(radio);
0310         return -ENOMEM;
0311     }
0312 
0313     radio->usbdev = interface_to_usbdev(intf);
0314     radio->intf = intf;
0315 
0316     /*
0317      * This device uses the same USB IDs as the si470x SiLabs reference
0318      * design. So do an additional check: attempt to read the device ID
0319      * from the si470x: the lower 12 bits are 0x0242 for the si470x. The
0320      * Raremono always returns 0x0800 (the meaning of that is unknown, but
0321      * at least it works).
0322      *
0323      * We use this check to determine which device we are dealing with.
0324      */
0325     msleep(20);
0326     retval = usb_control_msg(radio->usbdev,
0327         usb_rcvctrlpipe(radio->usbdev, 0),
0328         HID_REQ_GET_REPORT,
0329         USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_IN,
0330         1, 2,
0331         radio->buffer, 3, 500);
0332     if (retval != 3 ||
0333         (get_unaligned_be16(&radio->buffer[1]) & 0xfff) == 0x0242) {
0334         dev_info(&intf->dev, "this is not Thanko's Raremono.\n");
0335         retval = -ENODEV;
0336         goto free_mem;
0337     }
0338 
0339     dev_info(&intf->dev, "Thanko's Raremono connected: (%04X:%04X)\n",
0340             id->idVendor, id->idProduct);
0341 
0342     retval = v4l2_device_register(&intf->dev, &radio->v4l2_dev);
0343     if (retval < 0) {
0344         dev_err(&intf->dev, "couldn't register v4l2_device\n");
0345         goto free_mem;
0346     }
0347 
0348     mutex_init(&radio->lock);
0349 
0350     strscpy(radio->vdev.name, radio->v4l2_dev.name,
0351         sizeof(radio->vdev.name));
0352     radio->vdev.v4l2_dev = &radio->v4l2_dev;
0353     radio->vdev.fops = &usb_raremono_fops;
0354     radio->vdev.ioctl_ops = &usb_raremono_ioctl_ops;
0355     radio->vdev.lock = &radio->lock;
0356     radio->vdev.release = video_device_release_empty;
0357     radio->vdev.device_caps = V4L2_CAP_TUNER | V4L2_CAP_RADIO;
0358     radio->v4l2_dev.release = raremono_device_release;
0359 
0360     usb_set_intfdata(intf, &radio->v4l2_dev);
0361 
0362     video_set_drvdata(&radio->vdev, radio);
0363 
0364     raremono_cmd_main(radio, BAND_FM, 95160);
0365 
0366     retval = video_register_device(&radio->vdev, VFL_TYPE_RADIO, -1);
0367     if (retval == 0) {
0368         dev_info(&intf->dev, "V4L2 device registered as %s\n",
0369                 video_device_node_name(&radio->vdev));
0370         return 0;
0371     }
0372     dev_err(&intf->dev, "could not register video device\n");
0373     v4l2_device_unregister(&radio->v4l2_dev);
0374 
0375 free_mem:
0376     kfree(radio->buffer);
0377     kfree(radio);
0378     return retval;
0379 }
0380 
0381 /* USB subsystem interface */
0382 static struct usb_driver usb_raremono_driver = {
0383     .name           = "radio-raremono",
0384     .probe          = usb_raremono_probe,
0385     .disconnect     = usb_raremono_disconnect,
0386     .id_table       = usb_raremono_device_table,
0387 };
0388 
0389 module_usb_driver(usb_raremono_driver);