Merge tag 'csky-for-linus-4.20-fixup-dtb' of https://github.com/c-sky/csky-linux
[linux-block.git] / drivers / media / radio / radio-raremono.c
CommitLineData
ddddfa78 1// SPDX-License-Identifier: GPL-2.0-only
21326c46
HV
2/*
3 * Copyright 2013 Cisco Systems, Inc. and/or its affiliates. All rights reserved.
21326c46
HV
4 */
5
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>
20
21/*
22 * 'Thanko's Raremono' is a Japanese si4734-based AM/FM/SW USB receiver:
23 *
24 * http://www.raremono.jp/product/484.html/
25 *
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>.
29 *
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...
32 */
33
34/* driver and module definitions */
35MODULE_AUTHOR("Hans Verkuil <hverkuil@xs4all.nl>");
36MODULE_DESCRIPTION("Thanko's Raremono AM/FM/SW Receiver USB driver");
37MODULE_LICENSE("GPL v2");
38
39/*
40 * The Device announces itself as Cygnal Integrated Products, Inc.
41 *
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.
46 */
47
48/* USB Device ID List */
1ab2234e 49static const struct usb_device_id usb_raremono_device_table[] = {
21326c46
HV
50 {USB_DEVICE_AND_INTERFACE_INFO(0x10c4, 0x818a, USB_CLASS_HID, 0, 0) },
51 { } /* Terminating entry */
52};
53
54MODULE_DEVICE_TABLE(usb, usb_raremono_device_table);
55
56#define BUFFER_LENGTH 64
57
58/* Timeout is set to a high value, could probably be reduced. Need more tests */
59#define USB_TIMEOUT 10000
60
61/* Frequency limits in KHz */
62#define FM_FREQ_RANGE_LOW 64000
63#define FM_FREQ_RANGE_HIGH 108000
64
65#define AM_FREQ_RANGE_LOW 520
66#define AM_FREQ_RANGE_HIGH 1710
67
68#define SW_FREQ_RANGE_LOW 2300
69#define SW_FREQ_RANGE_HIGH 26100
70
71enum { BAND_FM, BAND_AM, BAND_SW };
72
73static const struct v4l2_frequency_band bands[] = {
74 /* Band FM */
75 {
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,
83 },
84 /* Band AM */
85 {
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,
92 },
93 /* Band SW */
94 {
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,
101 },
102};
103
104struct 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;
110
111 u8 *buffer;
112 u32 band;
113 unsigned curfreq;
114};
115
116static inline struct raremono_device *to_raremono_dev(struct v4l2_device *v4l2_dev)
117{
118 return container_of(v4l2_dev, struct raremono_device, v4l2_dev);
119}
120
121/* Set frequency. */
122static int raremono_cmd_main(struct raremono_device *radio, unsigned band, unsigned freq)
123{
124 unsigned band_offset;
125 int ret;
126
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;
138 }
139 radio->buffer[0] = 0x04 + band_offset;
140 radio->buffer[1] = freq >> 8;
141 radio->buffer[2] = freq & 0xff;
142
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);
148
149 if (ret < 0) {
150 dev_warn(radio->v4l2_dev.dev, "%s failed (%d)\n", __func__, ret);
151 return ret;
152 }
153 radio->curfreq = (band == BAND_FM) ? freq * 10 : freq;
154 return 0;
155}
156
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.
161 */
162static void usb_raremono_disconnect(struct usb_interface *intf)
163{
164 struct raremono_device *radio = to_raremono_dev(usb_get_intfdata(intf));
165
166 dev_info(&intf->dev, "Thanko's Raremono disconnected\n");
167
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);
174}
175
176/*
177 * Linux Video interface
178 */
179static int vidioc_querycap(struct file *file, void *priv,
180 struct v4l2_capability *v)
181{
182 struct raremono_device *radio = video_drvdata(file);
183
c0decac1
MCC
184 strscpy(v->driver, "radio-raremono", sizeof(v->driver));
185 strscpy(v->card, "Thanko's Raremono", sizeof(v->card));
21326c46
HV
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;
190}
191
192static int vidioc_enum_freq_bands(struct file *file, void *priv,
193 struct v4l2_frequency_band *band)
194{
195 if (band->tuner != 0)
196 return -EINVAL;
197
198 if (band->index >= ARRAY_SIZE(bands))
199 return -EINVAL;
200
201 *band = bands[band->index];
202
203 return 0;
204}
205
206static int vidioc_g_tuner(struct file *file, void *priv,
207 struct v4l2_tuner *v)
208{
209 struct raremono_device *radio = video_drvdata(file);
210 int ret;
211
212 if (v->index > 0)
213 return -EINVAL;
214
c0decac1 215 strscpy(v->name, "AM/FM/SW", sizeof(v->name));
21326c46
HV
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);
226
227 if (ret < 0) {
228 dev_warn(radio->v4l2_dev.dev, "%s failed (%d)\n", __func__, ret);
229 return ret;
230 }
231 v->signal = ((radio->buffer[1] & 0xf) << 8 | radio->buffer[2]) << 4;
232 return 0;
233}
234
235static int vidioc_s_tuner(struct file *file, void *priv,
236 const struct v4l2_tuner *v)
237{
238 return v->index ? -EINVAL : 0;
239}
240
241static int vidioc_s_frequency(struct file *file, void *priv,
242 const struct v4l2_frequency *f)
243{
244 struct raremono_device *radio = video_drvdata(file);
66b72cf1 245 u32 freq;
21326c46
HV
246 unsigned band;
247
248 if (f->tuner != 0 || f->type != V4L2_TUNER_RADIO)
249 return -EINVAL;
250
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;
257
258 freq = clamp_t(u32, f->frequency, bands[band].rangelow, bands[band].rangehigh);
259 return raremono_cmd_main(radio, band, freq / 16);
260}
261
262static int vidioc_g_frequency(struct file *file, void *priv,
263 struct v4l2_frequency *f)
264{
265 struct raremono_device *radio = video_drvdata(file);
266
267 if (f->tuner != 0)
268 return -EINVAL;
269 f->type = V4L2_TUNER_RADIO;
270 f->frequency = radio->curfreq * 16;
271 return 0;
272}
273
274/* File system interface */
275static const struct v4l2_file_operations usb_raremono_fops = {
276 .owner = THIS_MODULE,
277 .open = v4l2_fh_open,
278 .release = v4l2_fh_release,
279 .unlocked_ioctl = video_ioctl2,
280};
281
282static const struct v4l2_ioctl_ops usb_raremono_ioctl_ops = {
283 .vidioc_querycap = vidioc_querycap,
284 .vidioc_g_tuner = vidioc_g_tuner,
285 .vidioc_s_tuner = vidioc_s_tuner,
286 .vidioc_g_frequency = vidioc_g_frequency,
287 .vidioc_s_frequency = vidioc_s_frequency,
288 .vidioc_enum_freq_bands = vidioc_enum_freq_bands,
289};
290
291/* check if the device is present and register with v4l and usb if it is */
292static int usb_raremono_probe(struct usb_interface *intf,
293 const struct usb_device_id *id)
294{
295 struct raremono_device *radio;
296 int retval = 0;
297
298 radio = devm_kzalloc(&intf->dev, sizeof(struct raremono_device), GFP_KERNEL);
299 if (radio)
300 radio->buffer = devm_kmalloc(&intf->dev, BUFFER_LENGTH, GFP_KERNEL);
301
302 if (!radio || !radio->buffer)
303 return -ENOMEM;
304
305 radio->usbdev = interface_to_usbdev(intf);
306 radio->intf = intf;
307
308 /*
309 * This device uses the same USB IDs as the si470x SiLabs reference
310 * design. So do an additional check: attempt to read the device ID
311 * from the si470x: the lower 12 bits are 0x0242 for the si470x. The
312 * Raremono always returns 0x0800 (the meaning of that is unknown, but
313 * at least it works).
314 *
315 * We use this check to determine which device we are dealing with.
316 */
317 msleep(20);
318 retval = usb_control_msg(radio->usbdev,
319 usb_rcvctrlpipe(radio->usbdev, 0),
320 HID_REQ_GET_REPORT,
321 USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_IN,
322 1, 2,
323 radio->buffer, 3, 500);
324 if (retval != 3 ||
325 (get_unaligned_be16(&radio->buffer[1]) & 0xfff) == 0x0242) {
326 dev_info(&intf->dev, "this is not Thanko's Raremono.\n");
327 return -ENODEV;
328 }
329
330 dev_info(&intf->dev, "Thanko's Raremono connected: (%04X:%04X)\n",
331 id->idVendor, id->idProduct);
332
333 retval = v4l2_device_register(&intf->dev, &radio->v4l2_dev);
334 if (retval < 0) {
335 dev_err(&intf->dev, "couldn't register v4l2_device\n");
336 return retval;
337 }
338
339 mutex_init(&radio->lock);
340
c0decac1 341 strscpy(radio->vdev.name, radio->v4l2_dev.name,
21326c46
HV
342 sizeof(radio->vdev.name));
343 radio->vdev.v4l2_dev = &radio->v4l2_dev;
344 radio->vdev.fops = &usb_raremono_fops;
345 radio->vdev.ioctl_ops = &usb_raremono_ioctl_ops;
346 radio->vdev.lock = &radio->lock;
347 radio->vdev.release = video_device_release_empty;
348
349 usb_set_intfdata(intf, &radio->v4l2_dev);
350
351 video_set_drvdata(&radio->vdev, radio);
21326c46
HV
352
353 raremono_cmd_main(radio, BAND_FM, 95160);
354
355 retval = video_register_device(&radio->vdev, VFL_TYPE_RADIO, -1);
356 if (retval == 0) {
357 dev_info(&intf->dev, "V4L2 device registered as %s\n",
358 video_device_node_name(&radio->vdev));
359 return 0;
360 }
361 dev_err(&intf->dev, "could not register video device\n");
362 v4l2_device_unregister(&radio->v4l2_dev);
363 return retval;
364}
365
366/* USB subsystem interface */
367static struct usb_driver usb_raremono_driver = {
368 .name = "radio-raremono",
369 .probe = usb_raremono_probe,
370 .disconnect = usb_raremono_disconnect,
371 .id_table = usb_raremono_device_table,
372};
373
374module_usb_driver(usb_raremono_driver);