radio-raremono.c 10 KB

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