Linux 4.19.133
[linux/fpc-iii.git] / drivers / media / radio / radio-raremono.c
blob729600c4a056b824b4fdfcc7e831ae939e951aa2
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Copyright 2013 Cisco Systems, Inc. and/or its affiliates. All rights reserved.
4 */
6 #include <linux/kernel.h>
7 #include <linux/module.h>
8 #include <linux/init.h>
9 #include <linux/slab.h>
10 #include <linux/input.h>
11 #include <linux/usb.h>
12 #include <linux/hid.h>
13 #include <linux/mutex.h>
14 #include <linux/videodev2.h>
15 #include <asm/unaligned.h>
16 #include <media/v4l2-device.h>
17 #include <media/v4l2-ioctl.h>
18 #include <media/v4l2-ctrls.h>
19 #include <media/v4l2-event.h>
22 * 'Thanko's Raremono' is a Japanese si4734-based AM/FM/SW USB receiver:
24 * http://www.raremono.jp/product/484.html/
26 * The USB protocol has been reversed engineered using wireshark, initially
27 * by Dinesh Ram <dinesh.ram@cern.ch> and finished by Hans Verkuil
28 * <hverkuil@xs4all.nl>.
30 * Sadly the firmware used in this product hides lots of goodies since the
31 * si4734 has more features than are supported by the firmware. Oh well...
34 /* driver and module definitions */
35 MODULE_AUTHOR("Hans Verkuil <hverkuil@xs4all.nl>");
36 MODULE_DESCRIPTION("Thanko's Raremono AM/FM/SW Receiver USB driver");
37 MODULE_LICENSE("GPL v2");
40 * The Device announces itself as Cygnal Integrated Products, Inc.
42 * The vendor and product IDs (and in fact all other lsusb information as
43 * well) are identical to the si470x Silicon Labs USB FM Radio Reference
44 * Design board, even though this card has a si4734 device. Clearly the
45 * designer of this product never bothered to change the USB IDs.
48 /* USB Device ID List */
49 static const struct usb_device_id usb_raremono_device_table[] = {
50 {USB_DEVICE_AND_INTERFACE_INFO(0x10c4, 0x818a, USB_CLASS_HID, 0, 0) },
51 { } /* Terminating entry */
54 MODULE_DEVICE_TABLE(usb, usb_raremono_device_table);
56 #define BUFFER_LENGTH 64
58 /* Timeout is set to a high value, could probably be reduced. Need more tests */
59 #define USB_TIMEOUT 10000
61 /* Frequency limits in KHz */
62 #define FM_FREQ_RANGE_LOW 64000
63 #define FM_FREQ_RANGE_HIGH 108000
65 #define AM_FREQ_RANGE_LOW 520
66 #define AM_FREQ_RANGE_HIGH 1710
68 #define SW_FREQ_RANGE_LOW 2300
69 #define SW_FREQ_RANGE_HIGH 26100
71 enum { BAND_FM, BAND_AM, BAND_SW };
73 static const struct v4l2_frequency_band bands[] = {
74 /* Band FM */
76 .type = V4L2_TUNER_RADIO,
77 .index = 0,
78 .capability = V4L2_TUNER_CAP_LOW | V4L2_TUNER_CAP_STEREO |
79 V4L2_TUNER_CAP_FREQ_BANDS,
80 .rangelow = FM_FREQ_RANGE_LOW * 16,
81 .rangehigh = FM_FREQ_RANGE_HIGH * 16,
82 .modulation = V4L2_BAND_MODULATION_FM,
84 /* Band AM */
86 .type = V4L2_TUNER_RADIO,
87 .index = 1,
88 .capability = V4L2_TUNER_CAP_LOW | V4L2_TUNER_CAP_FREQ_BANDS,
89 .rangelow = AM_FREQ_RANGE_LOW * 16,
90 .rangehigh = AM_FREQ_RANGE_HIGH * 16,
91 .modulation = V4L2_BAND_MODULATION_AM,
93 /* Band SW */
95 .type = V4L2_TUNER_RADIO,
96 .index = 2,
97 .capability = V4L2_TUNER_CAP_LOW | V4L2_TUNER_CAP_FREQ_BANDS,
98 .rangelow = SW_FREQ_RANGE_LOW * 16,
99 .rangehigh = SW_FREQ_RANGE_HIGH * 16,
100 .modulation = V4L2_BAND_MODULATION_AM,
104 struct raremono_device {
105 struct usb_device *usbdev;
106 struct usb_interface *intf;
107 struct video_device vdev;
108 struct v4l2_device v4l2_dev;
109 struct mutex lock;
111 u8 *buffer;
112 u32 band;
113 unsigned curfreq;
116 static inline struct raremono_device *to_raremono_dev(struct v4l2_device *v4l2_dev)
118 return container_of(v4l2_dev, struct raremono_device, v4l2_dev);
121 /* Set frequency. */
122 static int raremono_cmd_main(struct raremono_device *radio, unsigned band, unsigned freq)
124 unsigned band_offset;
125 int ret;
127 switch (band) {
128 case BAND_FM:
129 band_offset = 1;
130 freq /= 10;
131 break;
132 case BAND_AM:
133 band_offset = 0;
134 break;
135 default:
136 band_offset = 2;
137 break;
139 radio->buffer[0] = 0x04 + band_offset;
140 radio->buffer[1] = freq >> 8;
141 radio->buffer[2] = freq & 0xff;
143 ret = usb_control_msg(radio->usbdev, usb_sndctrlpipe(radio->usbdev, 0),
144 HID_REQ_SET_REPORT,
145 USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_OUT,
146 0x0300 + radio->buffer[0], 2,
147 radio->buffer, 3, USB_TIMEOUT);
149 if (ret < 0) {
150 dev_warn(radio->v4l2_dev.dev, "%s failed (%d)\n", __func__, ret);
151 return ret;
153 radio->curfreq = (band == BAND_FM) ? freq * 10 : freq;
154 return 0;
157 /* Handle unplugging the device.
158 * We call video_unregister_device in any case.
159 * The last function called in this procedure is
160 * usb_raremono_device_release.
162 static void usb_raremono_disconnect(struct usb_interface *intf)
164 struct raremono_device *radio = to_raremono_dev(usb_get_intfdata(intf));
166 dev_info(&intf->dev, "Thanko's Raremono disconnected\n");
168 mutex_lock(&radio->lock);
169 usb_set_intfdata(intf, NULL);
170 video_unregister_device(&radio->vdev);
171 v4l2_device_disconnect(&radio->v4l2_dev);
172 mutex_unlock(&radio->lock);
173 v4l2_device_put(&radio->v4l2_dev);
177 * Linux Video interface
179 static int vidioc_querycap(struct file *file, void *priv,
180 struct v4l2_capability *v)
182 struct raremono_device *radio = video_drvdata(file);
184 strlcpy(v->driver, "radio-raremono", sizeof(v->driver));
185 strlcpy(v->card, "Thanko's Raremono", sizeof(v->card));
186 usb_make_path(radio->usbdev, v->bus_info, sizeof(v->bus_info));
187 v->device_caps = V4L2_CAP_TUNER | V4L2_CAP_RADIO;
188 v->capabilities = v->device_caps | V4L2_CAP_DEVICE_CAPS;
189 return 0;
192 static int vidioc_enum_freq_bands(struct file *file, void *priv,
193 struct v4l2_frequency_band *band)
195 if (band->tuner != 0)
196 return -EINVAL;
198 if (band->index >= ARRAY_SIZE(bands))
199 return -EINVAL;
201 *band = bands[band->index];
203 return 0;
206 static int vidioc_g_tuner(struct file *file, void *priv,
207 struct v4l2_tuner *v)
209 struct raremono_device *radio = video_drvdata(file);
210 int ret;
212 if (v->index > 0)
213 return -EINVAL;
215 strlcpy(v->name, "AM/FM/SW", sizeof(v->name));
216 v->capability = V4L2_TUNER_CAP_LOW | V4L2_TUNER_CAP_STEREO |
217 V4L2_TUNER_CAP_FREQ_BANDS;
218 v->rangelow = AM_FREQ_RANGE_LOW * 16;
219 v->rangehigh = FM_FREQ_RANGE_HIGH * 16;
220 v->rxsubchans = V4L2_TUNER_SUB_STEREO | V4L2_TUNER_SUB_MONO;
221 v->audmode = (radio->curfreq < FM_FREQ_RANGE_LOW) ?
222 V4L2_TUNER_MODE_MONO : V4L2_TUNER_MODE_STEREO;
223 memset(radio->buffer, 1, BUFFER_LENGTH);
224 ret = usb_control_msg(radio->usbdev, usb_rcvctrlpipe(radio->usbdev, 0),
225 1, 0xa1, 0x030d, 2, radio->buffer, BUFFER_LENGTH, USB_TIMEOUT);
227 if (ret < 0) {
228 dev_warn(radio->v4l2_dev.dev, "%s failed (%d)\n", __func__, ret);
229 return ret;
231 v->signal = ((radio->buffer[1] & 0xf) << 8 | radio->buffer[2]) << 4;
232 return 0;
235 static int vidioc_s_tuner(struct file *file, void *priv,
236 const struct v4l2_tuner *v)
238 return v->index ? -EINVAL : 0;
241 static int vidioc_s_frequency(struct file *file, void *priv,
242 const struct v4l2_frequency *f)
244 struct raremono_device *radio = video_drvdata(file);
245 u32 freq;
246 unsigned band;
248 if (f->tuner != 0 || f->type != V4L2_TUNER_RADIO)
249 return -EINVAL;
251 if (f->frequency >= (FM_FREQ_RANGE_LOW + SW_FREQ_RANGE_HIGH) * 8)
252 band = BAND_FM;
253 else if (f->frequency <= (AM_FREQ_RANGE_HIGH + SW_FREQ_RANGE_LOW) * 8)
254 band = BAND_AM;
255 else
256 band = BAND_SW;
258 freq = clamp_t(u32, f->frequency, bands[band].rangelow, bands[band].rangehigh);
259 return raremono_cmd_main(radio, band, freq / 16);
262 static int vidioc_g_frequency(struct file *file, void *priv,
263 struct v4l2_frequency *f)
265 struct raremono_device *radio = video_drvdata(file);
267 if (f->tuner != 0)
268 return -EINVAL;
269 f->type = V4L2_TUNER_RADIO;
270 f->frequency = radio->curfreq * 16;
271 return 0;
274 static void raremono_device_release(struct v4l2_device *v4l2_dev)
276 struct raremono_device *radio = to_raremono_dev(v4l2_dev);
278 kfree(radio->buffer);
279 kfree(radio);
282 /* File system interface */
283 static const struct v4l2_file_operations usb_raremono_fops = {
284 .owner = THIS_MODULE,
285 .open = v4l2_fh_open,
286 .release = v4l2_fh_release,
287 .unlocked_ioctl = video_ioctl2,
290 static const struct v4l2_ioctl_ops usb_raremono_ioctl_ops = {
291 .vidioc_querycap = vidioc_querycap,
292 .vidioc_g_tuner = vidioc_g_tuner,
293 .vidioc_s_tuner = vidioc_s_tuner,
294 .vidioc_g_frequency = vidioc_g_frequency,
295 .vidioc_s_frequency = vidioc_s_frequency,
296 .vidioc_enum_freq_bands = vidioc_enum_freq_bands,
299 /* check if the device is present and register with v4l and usb if it is */
300 static int usb_raremono_probe(struct usb_interface *intf,
301 const struct usb_device_id *id)
303 struct raremono_device *radio;
304 int retval = 0;
306 radio = kzalloc(sizeof(*radio), GFP_KERNEL);
307 if (!radio)
308 return -ENOMEM;
309 radio->buffer = kmalloc(BUFFER_LENGTH, GFP_KERNEL);
310 if (!radio->buffer) {
311 kfree(radio);
312 return -ENOMEM;
315 radio->usbdev = interface_to_usbdev(intf);
316 radio->intf = intf;
319 * This device uses the same USB IDs as the si470x SiLabs reference
320 * design. So do an additional check: attempt to read the device ID
321 * from the si470x: the lower 12 bits are 0x0242 for the si470x. The
322 * Raremono always returns 0x0800 (the meaning of that is unknown, but
323 * at least it works).
325 * We use this check to determine which device we are dealing with.
327 msleep(20);
328 retval = usb_control_msg(radio->usbdev,
329 usb_rcvctrlpipe(radio->usbdev, 0),
330 HID_REQ_GET_REPORT,
331 USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_IN,
332 1, 2,
333 radio->buffer, 3, 500);
334 if (retval != 3 ||
335 (get_unaligned_be16(&radio->buffer[1]) & 0xfff) == 0x0242) {
336 dev_info(&intf->dev, "this is not Thanko's Raremono.\n");
337 retval = -ENODEV;
338 goto free_mem;
341 dev_info(&intf->dev, "Thanko's Raremono connected: (%04X:%04X)\n",
342 id->idVendor, id->idProduct);
344 retval = v4l2_device_register(&intf->dev, &radio->v4l2_dev);
345 if (retval < 0) {
346 dev_err(&intf->dev, "couldn't register v4l2_device\n");
347 goto free_mem;
350 mutex_init(&radio->lock);
352 strlcpy(radio->vdev.name, radio->v4l2_dev.name,
353 sizeof(radio->vdev.name));
354 radio->vdev.v4l2_dev = &radio->v4l2_dev;
355 radio->vdev.fops = &usb_raremono_fops;
356 radio->vdev.ioctl_ops = &usb_raremono_ioctl_ops;
357 radio->vdev.lock = &radio->lock;
358 radio->vdev.release = video_device_release_empty;
359 radio->v4l2_dev.release = raremono_device_release;
361 usb_set_intfdata(intf, &radio->v4l2_dev);
363 video_set_drvdata(&radio->vdev, radio);
365 raremono_cmd_main(radio, BAND_FM, 95160);
367 retval = video_register_device(&radio->vdev, VFL_TYPE_RADIO, -1);
368 if (retval == 0) {
369 dev_info(&intf->dev, "V4L2 device registered as %s\n",
370 video_device_node_name(&radio->vdev));
371 return 0;
373 dev_err(&intf->dev, "could not register video device\n");
374 v4l2_device_unregister(&radio->v4l2_dev);
376 free_mem:
377 kfree(radio->buffer);
378 kfree(radio);
379 return retval;
382 /* USB subsystem interface */
383 static struct usb_driver usb_raremono_driver = {
384 .name = "radio-raremono",
385 .probe = usb_raremono_probe,
386 .disconnect = usb_raremono_disconnect,
387 .id_table = usb_raremono_device_table,
390 module_usb_driver(usb_raremono_driver);