2 * Copyright 2013 Cisco Systems, Inc. and/or its affiliates. All rights reserved.
4 * This program is free software; you may redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; version 2 of the License.
8 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
9 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
10 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
11 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
12 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
13 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
14 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
18 #include <linux/kernel.h>
19 #include <linux/module.h>
20 #include <linux/init.h>
21 #include <linux/slab.h>
22 #include <linux/input.h>
23 #include <linux/usb.h>
24 #include <linux/hid.h>
25 #include <linux/mutex.h>
26 #include <linux/videodev2.h>
27 #include <asm/unaligned.h>
28 #include <media/v4l2-device.h>
29 #include <media/v4l2-ioctl.h>
30 #include <media/v4l2-ctrls.h>
31 #include <media/v4l2-event.h>
34 * 'Thanko's Raremono' is a Japanese si4734-based AM/FM/SW USB receiver:
36 * http://www.raremono.jp/product/484.html/
38 * The USB protocol has been reversed engineered using wireshark, initially
39 * by Dinesh Ram <dinesh.ram@cern.ch> and finished by Hans Verkuil
40 * <hverkuil@xs4all.nl>.
42 * Sadly the firmware used in this product hides lots of goodies since the
43 * si4734 has more features than are supported by the firmware. Oh well...
46 /* driver and module definitions */
47 MODULE_AUTHOR("Hans Verkuil <hverkuil@xs4all.nl>");
48 MODULE_DESCRIPTION("Thanko's Raremono AM/FM/SW Receiver USB driver");
49 MODULE_LICENSE("GPL v2");
52 * The Device announces itself as Cygnal Integrated Products, Inc.
54 * The vendor and product IDs (and in fact all other lsusb information as
55 * well) are identical to the si470x Silicon Labs USB FM Radio Reference
56 * Design board, even though this card has a si4734 device. Clearly the
57 * designer of this product never bothered to change the USB IDs.
60 /* USB Device ID List */
61 static struct usb_device_id usb_raremono_device_table
[] = {
62 {USB_DEVICE_AND_INTERFACE_INFO(0x10c4, 0x818a, USB_CLASS_HID
, 0, 0) },
63 { } /* Terminating entry */
66 MODULE_DEVICE_TABLE(usb
, usb_raremono_device_table
);
68 #define BUFFER_LENGTH 64
70 /* Timeout is set to a high value, could probably be reduced. Need more tests */
71 #define USB_TIMEOUT 10000
73 /* Frequency limits in KHz */
74 #define FM_FREQ_RANGE_LOW 64000
75 #define FM_FREQ_RANGE_HIGH 108000
77 #define AM_FREQ_RANGE_LOW 520
78 #define AM_FREQ_RANGE_HIGH 1710
80 #define SW_FREQ_RANGE_LOW 2300
81 #define SW_FREQ_RANGE_HIGH 26100
83 enum { BAND_FM
, BAND_AM
, BAND_SW
};
85 static const struct v4l2_frequency_band bands
[] = {
88 .type
= V4L2_TUNER_RADIO
,
90 .capability
= V4L2_TUNER_CAP_LOW
| V4L2_TUNER_CAP_STEREO
|
91 V4L2_TUNER_CAP_FREQ_BANDS
,
92 .rangelow
= FM_FREQ_RANGE_LOW
* 16,
93 .rangehigh
= FM_FREQ_RANGE_HIGH
* 16,
94 .modulation
= V4L2_BAND_MODULATION_FM
,
98 .type
= V4L2_TUNER_RADIO
,
100 .capability
= V4L2_TUNER_CAP_LOW
| V4L2_TUNER_CAP_FREQ_BANDS
,
101 .rangelow
= AM_FREQ_RANGE_LOW
* 16,
102 .rangehigh
= AM_FREQ_RANGE_HIGH
* 16,
103 .modulation
= V4L2_BAND_MODULATION_AM
,
107 .type
= V4L2_TUNER_RADIO
,
109 .capability
= V4L2_TUNER_CAP_LOW
| V4L2_TUNER_CAP_FREQ_BANDS
,
110 .rangelow
= SW_FREQ_RANGE_LOW
* 16,
111 .rangehigh
= SW_FREQ_RANGE_HIGH
* 16,
112 .modulation
= V4L2_BAND_MODULATION_AM
,
116 struct raremono_device
{
117 struct usb_device
*usbdev
;
118 struct usb_interface
*intf
;
119 struct video_device vdev
;
120 struct v4l2_device v4l2_dev
;
128 static inline struct raremono_device
*to_raremono_dev(struct v4l2_device
*v4l2_dev
)
130 return container_of(v4l2_dev
, struct raremono_device
, v4l2_dev
);
134 static int raremono_cmd_main(struct raremono_device
*radio
, unsigned band
, unsigned freq
)
136 unsigned band_offset
;
151 radio
->buffer
[0] = 0x04 + band_offset
;
152 radio
->buffer
[1] = freq
>> 8;
153 radio
->buffer
[2] = freq
& 0xff;
155 ret
= usb_control_msg(radio
->usbdev
, usb_sndctrlpipe(radio
->usbdev
, 0),
157 USB_TYPE_CLASS
| USB_RECIP_INTERFACE
| USB_DIR_OUT
,
158 0x0300 + radio
->buffer
[0], 2,
159 radio
->buffer
, 3, USB_TIMEOUT
);
162 dev_warn(radio
->v4l2_dev
.dev
, "%s failed (%d)\n", __func__
, ret
);
165 radio
->curfreq
= (band
== BAND_FM
) ? freq
* 10 : freq
;
169 /* Handle unplugging the device.
170 * We call video_unregister_device in any case.
171 * The last function called in this procedure is
172 * usb_raremono_device_release.
174 static void usb_raremono_disconnect(struct usb_interface
*intf
)
176 struct raremono_device
*radio
= to_raremono_dev(usb_get_intfdata(intf
));
178 dev_info(&intf
->dev
, "Thanko's Raremono disconnected\n");
180 mutex_lock(&radio
->lock
);
181 usb_set_intfdata(intf
, NULL
);
182 video_unregister_device(&radio
->vdev
);
183 v4l2_device_disconnect(&radio
->v4l2_dev
);
184 mutex_unlock(&radio
->lock
);
185 v4l2_device_put(&radio
->v4l2_dev
);
189 * Linux Video interface
191 static int vidioc_querycap(struct file
*file
, void *priv
,
192 struct v4l2_capability
*v
)
194 struct raremono_device
*radio
= video_drvdata(file
);
196 strlcpy(v
->driver
, "radio-raremono", sizeof(v
->driver
));
197 strlcpy(v
->card
, "Thanko's Raremono", sizeof(v
->card
));
198 usb_make_path(radio
->usbdev
, v
->bus_info
, sizeof(v
->bus_info
));
199 v
->device_caps
= V4L2_CAP_TUNER
| V4L2_CAP_RADIO
;
200 v
->capabilities
= v
->device_caps
| V4L2_CAP_DEVICE_CAPS
;
204 static int vidioc_enum_freq_bands(struct file
*file
, void *priv
,
205 struct v4l2_frequency_band
*band
)
207 if (band
->tuner
!= 0)
210 if (band
->index
>= ARRAY_SIZE(bands
))
213 *band
= bands
[band
->index
];
218 static int vidioc_g_tuner(struct file
*file
, void *priv
,
219 struct v4l2_tuner
*v
)
221 struct raremono_device
*radio
= video_drvdata(file
);
227 strlcpy(v
->name
, "AM/FM/SW", sizeof(v
->name
));
228 v
->capability
= V4L2_TUNER_CAP_LOW
| V4L2_TUNER_CAP_STEREO
|
229 V4L2_TUNER_CAP_FREQ_BANDS
;
230 v
->rangelow
= AM_FREQ_RANGE_LOW
* 16;
231 v
->rangehigh
= FM_FREQ_RANGE_HIGH
* 16;
232 v
->rxsubchans
= V4L2_TUNER_SUB_STEREO
| V4L2_TUNER_SUB_MONO
;
233 v
->audmode
= (radio
->curfreq
< FM_FREQ_RANGE_LOW
) ?
234 V4L2_TUNER_MODE_MONO
: V4L2_TUNER_MODE_STEREO
;
235 memset(radio
->buffer
, 1, BUFFER_LENGTH
);
236 ret
= usb_control_msg(radio
->usbdev
, usb_rcvctrlpipe(radio
->usbdev
, 0),
237 1, 0xa1, 0x030d, 2, radio
->buffer
, BUFFER_LENGTH
, USB_TIMEOUT
);
240 dev_warn(radio
->v4l2_dev
.dev
, "%s failed (%d)\n", __func__
, ret
);
243 v
->signal
= ((radio
->buffer
[1] & 0xf) << 8 | radio
->buffer
[2]) << 4;
247 static int vidioc_s_tuner(struct file
*file
, void *priv
,
248 const struct v4l2_tuner
*v
)
250 return v
->index
? -EINVAL
: 0;
253 static int vidioc_s_frequency(struct file
*file
, void *priv
,
254 const struct v4l2_frequency
*f
)
256 struct raremono_device
*radio
= video_drvdata(file
);
257 u32 freq
= f
->frequency
;
260 if (f
->tuner
!= 0 || f
->type
!= V4L2_TUNER_RADIO
)
263 if (f
->frequency
>= (FM_FREQ_RANGE_LOW
+ SW_FREQ_RANGE_HIGH
) * 8)
265 else if (f
->frequency
<= (AM_FREQ_RANGE_HIGH
+ SW_FREQ_RANGE_LOW
) * 8)
270 freq
= clamp_t(u32
, f
->frequency
, bands
[band
].rangelow
, bands
[band
].rangehigh
);
271 return raremono_cmd_main(radio
, band
, freq
/ 16);
274 static int vidioc_g_frequency(struct file
*file
, void *priv
,
275 struct v4l2_frequency
*f
)
277 struct raremono_device
*radio
= video_drvdata(file
);
281 f
->type
= V4L2_TUNER_RADIO
;
282 f
->frequency
= radio
->curfreq
* 16;
286 static void raremono_device_release(struct v4l2_device
*v4l2_dev
)
288 struct raremono_device
*radio
= to_raremono_dev(v4l2_dev
);
290 kfree(radio
->buffer
);
294 /* File system interface */
295 static const struct v4l2_file_operations usb_raremono_fops
= {
296 .owner
= THIS_MODULE
,
297 .open
= v4l2_fh_open
,
298 .release
= v4l2_fh_release
,
299 .unlocked_ioctl
= video_ioctl2
,
302 static const struct v4l2_ioctl_ops usb_raremono_ioctl_ops
= {
303 .vidioc_querycap
= vidioc_querycap
,
304 .vidioc_g_tuner
= vidioc_g_tuner
,
305 .vidioc_s_tuner
= vidioc_s_tuner
,
306 .vidioc_g_frequency
= vidioc_g_frequency
,
307 .vidioc_s_frequency
= vidioc_s_frequency
,
308 .vidioc_enum_freq_bands
= vidioc_enum_freq_bands
,
311 /* check if the device is present and register with v4l and usb if it is */
312 static int usb_raremono_probe(struct usb_interface
*intf
,
313 const struct usb_device_id
*id
)
315 struct raremono_device
*radio
;
318 radio
= kzalloc(sizeof(*radio
), GFP_KERNEL
);
321 radio
->buffer
= kmalloc(BUFFER_LENGTH
, GFP_KERNEL
);
322 if (!radio
->buffer
) {
327 radio
->usbdev
= interface_to_usbdev(intf
);
331 * This device uses the same USB IDs as the si470x SiLabs reference
332 * design. So do an additional check: attempt to read the device ID
333 * from the si470x: the lower 12 bits are 0x0242 for the si470x. The
334 * Raremono always returns 0x0800 (the meaning of that is unknown, but
335 * at least it works).
337 * We use this check to determine which device we are dealing with.
340 retval
= usb_control_msg(radio
->usbdev
,
341 usb_rcvctrlpipe(radio
->usbdev
, 0),
343 USB_TYPE_CLASS
| USB_RECIP_INTERFACE
| USB_DIR_IN
,
345 radio
->buffer
, 3, 500);
347 (get_unaligned_be16(&radio
->buffer
[1]) & 0xfff) == 0x0242) {
348 dev_info(&intf
->dev
, "this is not Thanko's Raremono.\n");
353 dev_info(&intf
->dev
, "Thanko's Raremono connected: (%04X:%04X)\n",
354 id
->idVendor
, id
->idProduct
);
356 retval
= v4l2_device_register(&intf
->dev
, &radio
->v4l2_dev
);
358 dev_err(&intf
->dev
, "couldn't register v4l2_device\n");
362 mutex_init(&radio
->lock
);
364 strlcpy(radio
->vdev
.name
, radio
->v4l2_dev
.name
,
365 sizeof(radio
->vdev
.name
));
366 radio
->vdev
.v4l2_dev
= &radio
->v4l2_dev
;
367 radio
->vdev
.fops
= &usb_raremono_fops
;
368 radio
->vdev
.ioctl_ops
= &usb_raremono_ioctl_ops
;
369 radio
->vdev
.lock
= &radio
->lock
;
370 radio
->vdev
.release
= video_device_release_empty
;
371 radio
->v4l2_dev
.release
= raremono_device_release
;
373 usb_set_intfdata(intf
, &radio
->v4l2_dev
);
375 video_set_drvdata(&radio
->vdev
, radio
);
377 raremono_cmd_main(radio
, BAND_FM
, 95160);
379 retval
= video_register_device(&radio
->vdev
, VFL_TYPE_RADIO
, -1);
381 dev_info(&intf
->dev
, "V4L2 device registered as %s\n",
382 video_device_node_name(&radio
->vdev
));
385 dev_err(&intf
->dev
, "could not register video device\n");
386 v4l2_device_unregister(&radio
->v4l2_dev
);
389 kfree(radio
->buffer
);
394 /* USB subsystem interface */
395 static struct usb_driver usb_raremono_driver
= {
396 .name
= "radio-raremono",
397 .probe
= usb_raremono_probe
,
398 .disconnect
= usb_raremono_disconnect
,
399 .id_table
= usb_raremono_device_table
,
402 module_usb_driver(usb_raremono_driver
);