18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Copyright 2013 Cisco Systems, Inc. and/or its affiliates. All rights reserved.
48c2ecf20Sopenharmony_ci */
58c2ecf20Sopenharmony_ci
68c2ecf20Sopenharmony_ci#include <linux/kernel.h>
78c2ecf20Sopenharmony_ci#include <linux/module.h>
88c2ecf20Sopenharmony_ci#include <linux/init.h>
98c2ecf20Sopenharmony_ci#include <linux/slab.h>
108c2ecf20Sopenharmony_ci#include <linux/input.h>
118c2ecf20Sopenharmony_ci#include <linux/usb.h>
128c2ecf20Sopenharmony_ci#include <linux/hid.h>
138c2ecf20Sopenharmony_ci#include <linux/mutex.h>
148c2ecf20Sopenharmony_ci#include <linux/videodev2.h>
158c2ecf20Sopenharmony_ci#include <asm/unaligned.h>
168c2ecf20Sopenharmony_ci#include <media/v4l2-device.h>
178c2ecf20Sopenharmony_ci#include <media/v4l2-ioctl.h>
188c2ecf20Sopenharmony_ci#include <media/v4l2-ctrls.h>
198c2ecf20Sopenharmony_ci#include <media/v4l2-event.h>
208c2ecf20Sopenharmony_ci
218c2ecf20Sopenharmony_ci/*
228c2ecf20Sopenharmony_ci * 'Thanko's Raremono' is a Japanese si4734-based AM/FM/SW USB receiver:
238c2ecf20Sopenharmony_ci *
248c2ecf20Sopenharmony_ci * http://www.raremono.jp/product/484.html/
258c2ecf20Sopenharmony_ci *
268c2ecf20Sopenharmony_ci * The USB protocol has been reversed engineered using wireshark, initially
278c2ecf20Sopenharmony_ci * by Dinesh Ram <dinesh.ram@cern.ch> and finished by Hans Verkuil
288c2ecf20Sopenharmony_ci * <hverkuil@xs4all.nl>.
298c2ecf20Sopenharmony_ci *
308c2ecf20Sopenharmony_ci * Sadly the firmware used in this product hides lots of goodies since the
318c2ecf20Sopenharmony_ci * si4734 has more features than are supported by the firmware. Oh well...
328c2ecf20Sopenharmony_ci */
338c2ecf20Sopenharmony_ci
348c2ecf20Sopenharmony_ci/* driver and module definitions */
358c2ecf20Sopenharmony_ciMODULE_AUTHOR("Hans Verkuil <hverkuil@xs4all.nl>");
368c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("Thanko's Raremono AM/FM/SW Receiver USB driver");
378c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL v2");
388c2ecf20Sopenharmony_ci
398c2ecf20Sopenharmony_ci/*
408c2ecf20Sopenharmony_ci * The Device announces itself as Cygnal Integrated Products, Inc.
418c2ecf20Sopenharmony_ci *
428c2ecf20Sopenharmony_ci * The vendor and product IDs (and in fact all other lsusb information as
438c2ecf20Sopenharmony_ci * well) are identical to the si470x Silicon Labs USB FM Radio Reference
448c2ecf20Sopenharmony_ci * Design board, even though this card has a si4734 device. Clearly the
458c2ecf20Sopenharmony_ci * designer of this product never bothered to change the USB IDs.
468c2ecf20Sopenharmony_ci */
478c2ecf20Sopenharmony_ci
488c2ecf20Sopenharmony_ci/* USB Device ID List */
498c2ecf20Sopenharmony_cistatic const struct usb_device_id usb_raremono_device_table[] = {
508c2ecf20Sopenharmony_ci	{USB_DEVICE_AND_INTERFACE_INFO(0x10c4, 0x818a, USB_CLASS_HID, 0, 0) },
518c2ecf20Sopenharmony_ci	{ }						/* Terminating entry */
528c2ecf20Sopenharmony_ci};
538c2ecf20Sopenharmony_ci
548c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(usb, usb_raremono_device_table);
558c2ecf20Sopenharmony_ci
568c2ecf20Sopenharmony_ci#define BUFFER_LENGTH 64
578c2ecf20Sopenharmony_ci
588c2ecf20Sopenharmony_ci/* Timeout is set to a high value, could probably be reduced. Need more tests */
598c2ecf20Sopenharmony_ci#define USB_TIMEOUT 10000
608c2ecf20Sopenharmony_ci
618c2ecf20Sopenharmony_ci/* Frequency limits in KHz */
628c2ecf20Sopenharmony_ci#define FM_FREQ_RANGE_LOW	64000
638c2ecf20Sopenharmony_ci#define FM_FREQ_RANGE_HIGH	108000
648c2ecf20Sopenharmony_ci
658c2ecf20Sopenharmony_ci#define AM_FREQ_RANGE_LOW	520
668c2ecf20Sopenharmony_ci#define AM_FREQ_RANGE_HIGH	1710
678c2ecf20Sopenharmony_ci
688c2ecf20Sopenharmony_ci#define SW_FREQ_RANGE_LOW	2300
698c2ecf20Sopenharmony_ci#define SW_FREQ_RANGE_HIGH	26100
708c2ecf20Sopenharmony_ci
718c2ecf20Sopenharmony_cienum { BAND_FM, BAND_AM, BAND_SW };
728c2ecf20Sopenharmony_ci
738c2ecf20Sopenharmony_cistatic const struct v4l2_frequency_band bands[] = {
748c2ecf20Sopenharmony_ci	/* Band FM */
758c2ecf20Sopenharmony_ci	{
768c2ecf20Sopenharmony_ci		.type = V4L2_TUNER_RADIO,
778c2ecf20Sopenharmony_ci		.index = 0,
788c2ecf20Sopenharmony_ci		.capability = V4L2_TUNER_CAP_LOW | V4L2_TUNER_CAP_STEREO |
798c2ecf20Sopenharmony_ci			      V4L2_TUNER_CAP_FREQ_BANDS,
808c2ecf20Sopenharmony_ci		.rangelow   = FM_FREQ_RANGE_LOW * 16,
818c2ecf20Sopenharmony_ci		.rangehigh  = FM_FREQ_RANGE_HIGH * 16,
828c2ecf20Sopenharmony_ci		.modulation = V4L2_BAND_MODULATION_FM,
838c2ecf20Sopenharmony_ci	},
848c2ecf20Sopenharmony_ci	/* Band AM */
858c2ecf20Sopenharmony_ci	{
868c2ecf20Sopenharmony_ci		.type = V4L2_TUNER_RADIO,
878c2ecf20Sopenharmony_ci		.index = 1,
888c2ecf20Sopenharmony_ci		.capability = V4L2_TUNER_CAP_LOW | V4L2_TUNER_CAP_FREQ_BANDS,
898c2ecf20Sopenharmony_ci		.rangelow   = AM_FREQ_RANGE_LOW * 16,
908c2ecf20Sopenharmony_ci		.rangehigh  = AM_FREQ_RANGE_HIGH * 16,
918c2ecf20Sopenharmony_ci		.modulation = V4L2_BAND_MODULATION_AM,
928c2ecf20Sopenharmony_ci	},
938c2ecf20Sopenharmony_ci	/* Band SW */
948c2ecf20Sopenharmony_ci	{
958c2ecf20Sopenharmony_ci		.type = V4L2_TUNER_RADIO,
968c2ecf20Sopenharmony_ci		.index = 2,
978c2ecf20Sopenharmony_ci		.capability = V4L2_TUNER_CAP_LOW | V4L2_TUNER_CAP_FREQ_BANDS,
988c2ecf20Sopenharmony_ci		.rangelow   = SW_FREQ_RANGE_LOW * 16,
998c2ecf20Sopenharmony_ci		.rangehigh  = SW_FREQ_RANGE_HIGH * 16,
1008c2ecf20Sopenharmony_ci		.modulation = V4L2_BAND_MODULATION_AM,
1018c2ecf20Sopenharmony_ci	},
1028c2ecf20Sopenharmony_ci};
1038c2ecf20Sopenharmony_ci
1048c2ecf20Sopenharmony_cistruct raremono_device {
1058c2ecf20Sopenharmony_ci	struct usb_device *usbdev;
1068c2ecf20Sopenharmony_ci	struct usb_interface *intf;
1078c2ecf20Sopenharmony_ci	struct video_device vdev;
1088c2ecf20Sopenharmony_ci	struct v4l2_device v4l2_dev;
1098c2ecf20Sopenharmony_ci	struct mutex lock;
1108c2ecf20Sopenharmony_ci
1118c2ecf20Sopenharmony_ci	u8 *buffer;
1128c2ecf20Sopenharmony_ci	u32 band;
1138c2ecf20Sopenharmony_ci	unsigned curfreq;
1148c2ecf20Sopenharmony_ci};
1158c2ecf20Sopenharmony_ci
1168c2ecf20Sopenharmony_cistatic inline struct raremono_device *to_raremono_dev(struct v4l2_device *v4l2_dev)
1178c2ecf20Sopenharmony_ci{
1188c2ecf20Sopenharmony_ci	return container_of(v4l2_dev, struct raremono_device, v4l2_dev);
1198c2ecf20Sopenharmony_ci}
1208c2ecf20Sopenharmony_ci
1218c2ecf20Sopenharmony_ci/* Set frequency. */
1228c2ecf20Sopenharmony_cistatic int raremono_cmd_main(struct raremono_device *radio, unsigned band, unsigned freq)
1238c2ecf20Sopenharmony_ci{
1248c2ecf20Sopenharmony_ci	unsigned band_offset;
1258c2ecf20Sopenharmony_ci	int ret;
1268c2ecf20Sopenharmony_ci
1278c2ecf20Sopenharmony_ci	switch (band) {
1288c2ecf20Sopenharmony_ci	case BAND_FM:
1298c2ecf20Sopenharmony_ci		band_offset = 1;
1308c2ecf20Sopenharmony_ci		freq /= 10;
1318c2ecf20Sopenharmony_ci		break;
1328c2ecf20Sopenharmony_ci	case BAND_AM:
1338c2ecf20Sopenharmony_ci		band_offset = 0;
1348c2ecf20Sopenharmony_ci		break;
1358c2ecf20Sopenharmony_ci	default:
1368c2ecf20Sopenharmony_ci		band_offset = 2;
1378c2ecf20Sopenharmony_ci		break;
1388c2ecf20Sopenharmony_ci	}
1398c2ecf20Sopenharmony_ci	radio->buffer[0] = 0x04 + band_offset;
1408c2ecf20Sopenharmony_ci	radio->buffer[1] = freq >> 8;
1418c2ecf20Sopenharmony_ci	radio->buffer[2] = freq & 0xff;
1428c2ecf20Sopenharmony_ci
1438c2ecf20Sopenharmony_ci	ret = usb_control_msg(radio->usbdev, usb_sndctrlpipe(radio->usbdev, 0),
1448c2ecf20Sopenharmony_ci			HID_REQ_SET_REPORT,
1458c2ecf20Sopenharmony_ci			USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_OUT,
1468c2ecf20Sopenharmony_ci			0x0300 + radio->buffer[0], 2,
1478c2ecf20Sopenharmony_ci			radio->buffer, 3, USB_TIMEOUT);
1488c2ecf20Sopenharmony_ci
1498c2ecf20Sopenharmony_ci	if (ret < 0) {
1508c2ecf20Sopenharmony_ci		dev_warn(radio->v4l2_dev.dev, "%s failed (%d)\n", __func__, ret);
1518c2ecf20Sopenharmony_ci		return ret;
1528c2ecf20Sopenharmony_ci	}
1538c2ecf20Sopenharmony_ci	radio->curfreq = (band == BAND_FM) ? freq * 10 : freq;
1548c2ecf20Sopenharmony_ci	return 0;
1558c2ecf20Sopenharmony_ci}
1568c2ecf20Sopenharmony_ci
1578c2ecf20Sopenharmony_ci/* Handle unplugging the device.
1588c2ecf20Sopenharmony_ci * We call video_unregister_device in any case.
1598c2ecf20Sopenharmony_ci * The last function called in this procedure is
1608c2ecf20Sopenharmony_ci * usb_raremono_device_release.
1618c2ecf20Sopenharmony_ci */
1628c2ecf20Sopenharmony_cistatic void usb_raremono_disconnect(struct usb_interface *intf)
1638c2ecf20Sopenharmony_ci{
1648c2ecf20Sopenharmony_ci	struct raremono_device *radio = to_raremono_dev(usb_get_intfdata(intf));
1658c2ecf20Sopenharmony_ci
1668c2ecf20Sopenharmony_ci	dev_info(&intf->dev, "Thanko's Raremono disconnected\n");
1678c2ecf20Sopenharmony_ci
1688c2ecf20Sopenharmony_ci	mutex_lock(&radio->lock);
1698c2ecf20Sopenharmony_ci	usb_set_intfdata(intf, NULL);
1708c2ecf20Sopenharmony_ci	video_unregister_device(&radio->vdev);
1718c2ecf20Sopenharmony_ci	v4l2_device_disconnect(&radio->v4l2_dev);
1728c2ecf20Sopenharmony_ci	mutex_unlock(&radio->lock);
1738c2ecf20Sopenharmony_ci	v4l2_device_put(&radio->v4l2_dev);
1748c2ecf20Sopenharmony_ci}
1758c2ecf20Sopenharmony_ci
1768c2ecf20Sopenharmony_ci/*
1778c2ecf20Sopenharmony_ci * Linux Video interface
1788c2ecf20Sopenharmony_ci */
1798c2ecf20Sopenharmony_cistatic int vidioc_querycap(struct file *file, void *priv,
1808c2ecf20Sopenharmony_ci					struct v4l2_capability *v)
1818c2ecf20Sopenharmony_ci{
1828c2ecf20Sopenharmony_ci	struct raremono_device *radio = video_drvdata(file);
1838c2ecf20Sopenharmony_ci
1848c2ecf20Sopenharmony_ci	strscpy(v->driver, "radio-raremono", sizeof(v->driver));
1858c2ecf20Sopenharmony_ci	strscpy(v->card, "Thanko's Raremono", sizeof(v->card));
1868c2ecf20Sopenharmony_ci	usb_make_path(radio->usbdev, v->bus_info, sizeof(v->bus_info));
1878c2ecf20Sopenharmony_ci	return 0;
1888c2ecf20Sopenharmony_ci}
1898c2ecf20Sopenharmony_ci
1908c2ecf20Sopenharmony_cistatic int vidioc_enum_freq_bands(struct file *file, void *priv,
1918c2ecf20Sopenharmony_ci		struct v4l2_frequency_band *band)
1928c2ecf20Sopenharmony_ci{
1938c2ecf20Sopenharmony_ci	if (band->tuner != 0)
1948c2ecf20Sopenharmony_ci		return -EINVAL;
1958c2ecf20Sopenharmony_ci
1968c2ecf20Sopenharmony_ci	if (band->index >= ARRAY_SIZE(bands))
1978c2ecf20Sopenharmony_ci		return -EINVAL;
1988c2ecf20Sopenharmony_ci
1998c2ecf20Sopenharmony_ci	*band = bands[band->index];
2008c2ecf20Sopenharmony_ci
2018c2ecf20Sopenharmony_ci	return 0;
2028c2ecf20Sopenharmony_ci}
2038c2ecf20Sopenharmony_ci
2048c2ecf20Sopenharmony_cistatic int vidioc_g_tuner(struct file *file, void *priv,
2058c2ecf20Sopenharmony_ci		struct v4l2_tuner *v)
2068c2ecf20Sopenharmony_ci{
2078c2ecf20Sopenharmony_ci	struct raremono_device *radio = video_drvdata(file);
2088c2ecf20Sopenharmony_ci	int ret;
2098c2ecf20Sopenharmony_ci
2108c2ecf20Sopenharmony_ci	if (v->index > 0)
2118c2ecf20Sopenharmony_ci		return -EINVAL;
2128c2ecf20Sopenharmony_ci
2138c2ecf20Sopenharmony_ci	strscpy(v->name, "AM/FM/SW", sizeof(v->name));
2148c2ecf20Sopenharmony_ci	v->capability = V4L2_TUNER_CAP_LOW | V4L2_TUNER_CAP_STEREO |
2158c2ecf20Sopenharmony_ci		V4L2_TUNER_CAP_FREQ_BANDS;
2168c2ecf20Sopenharmony_ci	v->rangelow = AM_FREQ_RANGE_LOW * 16;
2178c2ecf20Sopenharmony_ci	v->rangehigh = FM_FREQ_RANGE_HIGH * 16;
2188c2ecf20Sopenharmony_ci	v->rxsubchans = V4L2_TUNER_SUB_STEREO | V4L2_TUNER_SUB_MONO;
2198c2ecf20Sopenharmony_ci	v->audmode = (radio->curfreq < FM_FREQ_RANGE_LOW) ?
2208c2ecf20Sopenharmony_ci		V4L2_TUNER_MODE_MONO : V4L2_TUNER_MODE_STEREO;
2218c2ecf20Sopenharmony_ci	memset(radio->buffer, 1, BUFFER_LENGTH);
2228c2ecf20Sopenharmony_ci	ret = usb_control_msg(radio->usbdev, usb_rcvctrlpipe(radio->usbdev, 0),
2238c2ecf20Sopenharmony_ci			1, 0xa1, 0x030d, 2, radio->buffer, BUFFER_LENGTH, USB_TIMEOUT);
2248c2ecf20Sopenharmony_ci
2258c2ecf20Sopenharmony_ci	if (ret < 0) {
2268c2ecf20Sopenharmony_ci		dev_warn(radio->v4l2_dev.dev, "%s failed (%d)\n", __func__, ret);
2278c2ecf20Sopenharmony_ci		return ret;
2288c2ecf20Sopenharmony_ci	}
2298c2ecf20Sopenharmony_ci	v->signal = ((radio->buffer[1] & 0xf) << 8 | radio->buffer[2]) << 4;
2308c2ecf20Sopenharmony_ci	return 0;
2318c2ecf20Sopenharmony_ci}
2328c2ecf20Sopenharmony_ci
2338c2ecf20Sopenharmony_cistatic int vidioc_s_tuner(struct file *file, void *priv,
2348c2ecf20Sopenharmony_ci					const struct v4l2_tuner *v)
2358c2ecf20Sopenharmony_ci{
2368c2ecf20Sopenharmony_ci	return v->index ? -EINVAL : 0;
2378c2ecf20Sopenharmony_ci}
2388c2ecf20Sopenharmony_ci
2398c2ecf20Sopenharmony_cistatic int vidioc_s_frequency(struct file *file, void *priv,
2408c2ecf20Sopenharmony_ci				const struct v4l2_frequency *f)
2418c2ecf20Sopenharmony_ci{
2428c2ecf20Sopenharmony_ci	struct raremono_device *radio = video_drvdata(file);
2438c2ecf20Sopenharmony_ci	u32 freq;
2448c2ecf20Sopenharmony_ci	unsigned band;
2458c2ecf20Sopenharmony_ci
2468c2ecf20Sopenharmony_ci	if (f->tuner != 0 || f->type != V4L2_TUNER_RADIO)
2478c2ecf20Sopenharmony_ci		return -EINVAL;
2488c2ecf20Sopenharmony_ci
2498c2ecf20Sopenharmony_ci	if (f->frequency >= (FM_FREQ_RANGE_LOW + SW_FREQ_RANGE_HIGH) * 8)
2508c2ecf20Sopenharmony_ci		band = BAND_FM;
2518c2ecf20Sopenharmony_ci	else if (f->frequency <= (AM_FREQ_RANGE_HIGH + SW_FREQ_RANGE_LOW) * 8)
2528c2ecf20Sopenharmony_ci		band = BAND_AM;
2538c2ecf20Sopenharmony_ci	else
2548c2ecf20Sopenharmony_ci		band = BAND_SW;
2558c2ecf20Sopenharmony_ci
2568c2ecf20Sopenharmony_ci	freq = clamp_t(u32, f->frequency, bands[band].rangelow, bands[band].rangehigh);
2578c2ecf20Sopenharmony_ci	return raremono_cmd_main(radio, band, freq / 16);
2588c2ecf20Sopenharmony_ci}
2598c2ecf20Sopenharmony_ci
2608c2ecf20Sopenharmony_cistatic int vidioc_g_frequency(struct file *file, void *priv,
2618c2ecf20Sopenharmony_ci				struct v4l2_frequency *f)
2628c2ecf20Sopenharmony_ci{
2638c2ecf20Sopenharmony_ci	struct raremono_device *radio = video_drvdata(file);
2648c2ecf20Sopenharmony_ci
2658c2ecf20Sopenharmony_ci	if (f->tuner != 0)
2668c2ecf20Sopenharmony_ci		return -EINVAL;
2678c2ecf20Sopenharmony_ci	f->type = V4L2_TUNER_RADIO;
2688c2ecf20Sopenharmony_ci	f->frequency = radio->curfreq * 16;
2698c2ecf20Sopenharmony_ci	return 0;
2708c2ecf20Sopenharmony_ci}
2718c2ecf20Sopenharmony_ci
2728c2ecf20Sopenharmony_cistatic void raremono_device_release(struct v4l2_device *v4l2_dev)
2738c2ecf20Sopenharmony_ci{
2748c2ecf20Sopenharmony_ci	struct raremono_device *radio = to_raremono_dev(v4l2_dev);
2758c2ecf20Sopenharmony_ci
2768c2ecf20Sopenharmony_ci	kfree(radio->buffer);
2778c2ecf20Sopenharmony_ci	kfree(radio);
2788c2ecf20Sopenharmony_ci}
2798c2ecf20Sopenharmony_ci
2808c2ecf20Sopenharmony_ci/* File system interface */
2818c2ecf20Sopenharmony_cistatic const struct v4l2_file_operations usb_raremono_fops = {
2828c2ecf20Sopenharmony_ci	.owner		= THIS_MODULE,
2838c2ecf20Sopenharmony_ci	.open           = v4l2_fh_open,
2848c2ecf20Sopenharmony_ci	.release        = v4l2_fh_release,
2858c2ecf20Sopenharmony_ci	.unlocked_ioctl	= video_ioctl2,
2868c2ecf20Sopenharmony_ci};
2878c2ecf20Sopenharmony_ci
2888c2ecf20Sopenharmony_cistatic const struct v4l2_ioctl_ops usb_raremono_ioctl_ops = {
2898c2ecf20Sopenharmony_ci	.vidioc_querycap = vidioc_querycap,
2908c2ecf20Sopenharmony_ci	.vidioc_g_tuner = vidioc_g_tuner,
2918c2ecf20Sopenharmony_ci	.vidioc_s_tuner = vidioc_s_tuner,
2928c2ecf20Sopenharmony_ci	.vidioc_g_frequency = vidioc_g_frequency,
2938c2ecf20Sopenharmony_ci	.vidioc_s_frequency = vidioc_s_frequency,
2948c2ecf20Sopenharmony_ci	.vidioc_enum_freq_bands = vidioc_enum_freq_bands,
2958c2ecf20Sopenharmony_ci};
2968c2ecf20Sopenharmony_ci
2978c2ecf20Sopenharmony_ci/* check if the device is present and register with v4l and usb if it is */
2988c2ecf20Sopenharmony_cistatic int usb_raremono_probe(struct usb_interface *intf,
2998c2ecf20Sopenharmony_ci				const struct usb_device_id *id)
3008c2ecf20Sopenharmony_ci{
3018c2ecf20Sopenharmony_ci	struct raremono_device *radio;
3028c2ecf20Sopenharmony_ci	int retval = 0;
3038c2ecf20Sopenharmony_ci
3048c2ecf20Sopenharmony_ci	radio = kzalloc(sizeof(*radio), GFP_KERNEL);
3058c2ecf20Sopenharmony_ci	if (!radio)
3068c2ecf20Sopenharmony_ci		return -ENOMEM;
3078c2ecf20Sopenharmony_ci	radio->buffer = kmalloc(BUFFER_LENGTH, GFP_KERNEL);
3088c2ecf20Sopenharmony_ci	if (!radio->buffer) {
3098c2ecf20Sopenharmony_ci		kfree(radio);
3108c2ecf20Sopenharmony_ci		return -ENOMEM;
3118c2ecf20Sopenharmony_ci	}
3128c2ecf20Sopenharmony_ci
3138c2ecf20Sopenharmony_ci	radio->usbdev = interface_to_usbdev(intf);
3148c2ecf20Sopenharmony_ci	radio->intf = intf;
3158c2ecf20Sopenharmony_ci
3168c2ecf20Sopenharmony_ci	/*
3178c2ecf20Sopenharmony_ci	 * This device uses the same USB IDs as the si470x SiLabs reference
3188c2ecf20Sopenharmony_ci	 * design. So do an additional check: attempt to read the device ID
3198c2ecf20Sopenharmony_ci	 * from the si470x: the lower 12 bits are 0x0242 for the si470x. The
3208c2ecf20Sopenharmony_ci	 * Raremono always returns 0x0800 (the meaning of that is unknown, but
3218c2ecf20Sopenharmony_ci	 * at least it works).
3228c2ecf20Sopenharmony_ci	 *
3238c2ecf20Sopenharmony_ci	 * We use this check to determine which device we are dealing with.
3248c2ecf20Sopenharmony_ci	 */
3258c2ecf20Sopenharmony_ci	msleep(20);
3268c2ecf20Sopenharmony_ci	retval = usb_control_msg(radio->usbdev,
3278c2ecf20Sopenharmony_ci		usb_rcvctrlpipe(radio->usbdev, 0),
3288c2ecf20Sopenharmony_ci		HID_REQ_GET_REPORT,
3298c2ecf20Sopenharmony_ci		USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_IN,
3308c2ecf20Sopenharmony_ci		1, 2,
3318c2ecf20Sopenharmony_ci		radio->buffer, 3, 500);
3328c2ecf20Sopenharmony_ci	if (retval != 3 ||
3338c2ecf20Sopenharmony_ci	    (get_unaligned_be16(&radio->buffer[1]) & 0xfff) == 0x0242) {
3348c2ecf20Sopenharmony_ci		dev_info(&intf->dev, "this is not Thanko's Raremono.\n");
3358c2ecf20Sopenharmony_ci		retval = -ENODEV;
3368c2ecf20Sopenharmony_ci		goto free_mem;
3378c2ecf20Sopenharmony_ci	}
3388c2ecf20Sopenharmony_ci
3398c2ecf20Sopenharmony_ci	dev_info(&intf->dev, "Thanko's Raremono connected: (%04X:%04X)\n",
3408c2ecf20Sopenharmony_ci			id->idVendor, id->idProduct);
3418c2ecf20Sopenharmony_ci
3428c2ecf20Sopenharmony_ci	retval = v4l2_device_register(&intf->dev, &radio->v4l2_dev);
3438c2ecf20Sopenharmony_ci	if (retval < 0) {
3448c2ecf20Sopenharmony_ci		dev_err(&intf->dev, "couldn't register v4l2_device\n");
3458c2ecf20Sopenharmony_ci		goto free_mem;
3468c2ecf20Sopenharmony_ci	}
3478c2ecf20Sopenharmony_ci
3488c2ecf20Sopenharmony_ci	mutex_init(&radio->lock);
3498c2ecf20Sopenharmony_ci
3508c2ecf20Sopenharmony_ci	strscpy(radio->vdev.name, radio->v4l2_dev.name,
3518c2ecf20Sopenharmony_ci		sizeof(radio->vdev.name));
3528c2ecf20Sopenharmony_ci	radio->vdev.v4l2_dev = &radio->v4l2_dev;
3538c2ecf20Sopenharmony_ci	radio->vdev.fops = &usb_raremono_fops;
3548c2ecf20Sopenharmony_ci	radio->vdev.ioctl_ops = &usb_raremono_ioctl_ops;
3558c2ecf20Sopenharmony_ci	radio->vdev.lock = &radio->lock;
3568c2ecf20Sopenharmony_ci	radio->vdev.release = video_device_release_empty;
3578c2ecf20Sopenharmony_ci	radio->vdev.device_caps = V4L2_CAP_TUNER | V4L2_CAP_RADIO;
3588c2ecf20Sopenharmony_ci	radio->v4l2_dev.release = raremono_device_release;
3598c2ecf20Sopenharmony_ci
3608c2ecf20Sopenharmony_ci	usb_set_intfdata(intf, &radio->v4l2_dev);
3618c2ecf20Sopenharmony_ci
3628c2ecf20Sopenharmony_ci	video_set_drvdata(&radio->vdev, radio);
3638c2ecf20Sopenharmony_ci
3648c2ecf20Sopenharmony_ci	raremono_cmd_main(radio, BAND_FM, 95160);
3658c2ecf20Sopenharmony_ci
3668c2ecf20Sopenharmony_ci	retval = video_register_device(&radio->vdev, VFL_TYPE_RADIO, -1);
3678c2ecf20Sopenharmony_ci	if (retval == 0) {
3688c2ecf20Sopenharmony_ci		dev_info(&intf->dev, "V4L2 device registered as %s\n",
3698c2ecf20Sopenharmony_ci				video_device_node_name(&radio->vdev));
3708c2ecf20Sopenharmony_ci		return 0;
3718c2ecf20Sopenharmony_ci	}
3728c2ecf20Sopenharmony_ci	dev_err(&intf->dev, "could not register video device\n");
3738c2ecf20Sopenharmony_ci	v4l2_device_unregister(&radio->v4l2_dev);
3748c2ecf20Sopenharmony_ci
3758c2ecf20Sopenharmony_cifree_mem:
3768c2ecf20Sopenharmony_ci	kfree(radio->buffer);
3778c2ecf20Sopenharmony_ci	kfree(radio);
3788c2ecf20Sopenharmony_ci	return retval;
3798c2ecf20Sopenharmony_ci}
3808c2ecf20Sopenharmony_ci
3818c2ecf20Sopenharmony_ci/* USB subsystem interface */
3828c2ecf20Sopenharmony_cistatic struct usb_driver usb_raremono_driver = {
3838c2ecf20Sopenharmony_ci	.name			= "radio-raremono",
3848c2ecf20Sopenharmony_ci	.probe			= usb_raremono_probe,
3858c2ecf20Sopenharmony_ci	.disconnect		= usb_raremono_disconnect,
3868c2ecf20Sopenharmony_ci	.id_table		= usb_raremono_device_table,
3878c2ecf20Sopenharmony_ci};
3888c2ecf20Sopenharmony_ci
3898c2ecf20Sopenharmony_cimodule_usb_driver(usb_raremono_driver);
390