radio-raremono.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  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 <dinesh.ram@cern.ch> and finished by Hans Verkuil
  26. * <hverkuil@xs4all.nl>.
  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 <hverkuil@xs4all.nl>");
  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. strlcpy(v->driver, "radio-raremono", sizeof(v->driver));
  160. strlcpy(v->card, "Thanko's Raremono", sizeof(v->card));
  161. usb_make_path(radio->usbdev, v->bus_info, sizeof(v->bus_info));
  162. v->device_caps = V4L2_CAP_TUNER | V4L2_CAP_RADIO;
  163. v->capabilities = v->device_caps | V4L2_CAP_DEVICE_CAPS;
  164. return 0;
  165. }
  166. static int vidioc_enum_freq_bands(struct file *file, void *priv,
  167. struct v4l2_frequency_band *band)
  168. {
  169. if (band->tuner != 0)
  170. return -EINVAL;
  171. if (band->index >= ARRAY_SIZE(bands))
  172. return -EINVAL;
  173. *band = bands[band->index];
  174. return 0;
  175. }
  176. static int vidioc_g_tuner(struct file *file, void *priv,
  177. struct v4l2_tuner *v)
  178. {
  179. struct raremono_device *radio = video_drvdata(file);
  180. int ret;
  181. if (v->index > 0)
  182. return -EINVAL;
  183. strlcpy(v->name, "AM/FM/SW", sizeof(v->name));
  184. v->capability = V4L2_TUNER_CAP_LOW | V4L2_TUNER_CAP_STEREO |
  185. V4L2_TUNER_CAP_FREQ_BANDS;
  186. v->rangelow = AM_FREQ_RANGE_LOW * 16;
  187. v->rangehigh = FM_FREQ_RANGE_HIGH * 16;
  188. v->rxsubchans = V4L2_TUNER_SUB_STEREO | V4L2_TUNER_SUB_MONO;
  189. v->audmode = (radio->curfreq < FM_FREQ_RANGE_LOW) ?
  190. V4L2_TUNER_MODE_MONO : V4L2_TUNER_MODE_STEREO;
  191. memset(radio->buffer, 1, BUFFER_LENGTH);
  192. ret = usb_control_msg(radio->usbdev, usb_rcvctrlpipe(radio->usbdev, 0),
  193. 1, 0xa1, 0x030d, 2, radio->buffer, BUFFER_LENGTH, USB_TIMEOUT);
  194. if (ret < 0) {
  195. dev_warn(radio->v4l2_dev.dev, "%s failed (%d)\n", __func__, ret);
  196. return ret;
  197. }
  198. v->signal = ((radio->buffer[1] & 0xf) << 8 | radio->buffer[2]) << 4;
  199. return 0;
  200. }
  201. static int vidioc_s_tuner(struct file *file, void *priv,
  202. const struct v4l2_tuner *v)
  203. {
  204. return v->index ? -EINVAL : 0;
  205. }
  206. static int vidioc_s_frequency(struct file *file, void *priv,
  207. const struct v4l2_frequency *f)
  208. {
  209. struct raremono_device *radio = video_drvdata(file);
  210. u32 freq;
  211. unsigned band;
  212. if (f->tuner != 0 || f->type != V4L2_TUNER_RADIO)
  213. return -EINVAL;
  214. if (f->frequency >= (FM_FREQ_RANGE_LOW + SW_FREQ_RANGE_HIGH) * 8)
  215. band = BAND_FM;
  216. else if (f->frequency <= (AM_FREQ_RANGE_HIGH + SW_FREQ_RANGE_LOW) * 8)
  217. band = BAND_AM;
  218. else
  219. band = BAND_SW;
  220. freq = clamp_t(u32, f->frequency, bands[band].rangelow, bands[band].rangehigh);
  221. return raremono_cmd_main(radio, band, freq / 16);
  222. }
  223. static int vidioc_g_frequency(struct file *file, void *priv,
  224. struct v4l2_frequency *f)
  225. {
  226. struct raremono_device *radio = video_drvdata(file);
  227. if (f->tuner != 0)
  228. return -EINVAL;
  229. f->type = V4L2_TUNER_RADIO;
  230. f->frequency = radio->curfreq * 16;
  231. return 0;
  232. }
  233. static void raremono_device_release(struct v4l2_device *v4l2_dev)
  234. {
  235. struct raremono_device *radio = to_raremono_dev(v4l2_dev);
  236. kfree(radio->buffer);
  237. kfree(radio);
  238. }
  239. /* File system interface */
  240. static const struct v4l2_file_operations usb_raremono_fops = {
  241. .owner = THIS_MODULE,
  242. .open = v4l2_fh_open,
  243. .release = v4l2_fh_release,
  244. .unlocked_ioctl = video_ioctl2,
  245. };
  246. static const struct v4l2_ioctl_ops usb_raremono_ioctl_ops = {
  247. .vidioc_querycap = vidioc_querycap,
  248. .vidioc_g_tuner = vidioc_g_tuner,
  249. .vidioc_s_tuner = vidioc_s_tuner,
  250. .vidioc_g_frequency = vidioc_g_frequency,
  251. .vidioc_s_frequency = vidioc_s_frequency,
  252. .vidioc_enum_freq_bands = vidioc_enum_freq_bands,
  253. };
  254. /* check if the device is present and register with v4l and usb if it is */
  255. static int usb_raremono_probe(struct usb_interface *intf,
  256. const struct usb_device_id *id)
  257. {
  258. struct raremono_device *radio;
  259. int retval = 0;
  260. radio = kzalloc(sizeof(*radio), GFP_KERNEL);
  261. if (!radio)
  262. return -ENOMEM;
  263. radio->buffer = kmalloc(BUFFER_LENGTH, GFP_KERNEL);
  264. if (!radio->buffer) {
  265. kfree(radio);
  266. return -ENOMEM;
  267. }
  268. radio->usbdev = interface_to_usbdev(intf);
  269. radio->intf = intf;
  270. /*
  271. * This device uses the same USB IDs as the si470x SiLabs reference
  272. * design. So do an additional check: attempt to read the device ID
  273. * from the si470x: the lower 12 bits are 0x0242 for the si470x. The
  274. * Raremono always returns 0x0800 (the meaning of that is unknown, but
  275. * at least it works).
  276. *
  277. * We use this check to determine which device we are dealing with.
  278. */
  279. msleep(20);
  280. retval = usb_control_msg(radio->usbdev,
  281. usb_rcvctrlpipe(radio->usbdev, 0),
  282. HID_REQ_GET_REPORT,
  283. USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_IN,
  284. 1, 2,
  285. radio->buffer, 3, 500);
  286. if (retval != 3 ||
  287. (get_unaligned_be16(&radio->buffer[1]) & 0xfff) == 0x0242) {
  288. dev_info(&intf->dev, "this is not Thanko's Raremono.\n");
  289. retval = -ENODEV;
  290. goto free_mem;
  291. }
  292. dev_info(&intf->dev, "Thanko's Raremono connected: (%04X:%04X)\n",
  293. id->idVendor, id->idProduct);
  294. retval = v4l2_device_register(&intf->dev, &radio->v4l2_dev);
  295. if (retval < 0) {
  296. dev_err(&intf->dev, "couldn't register v4l2_device\n");
  297. goto free_mem;
  298. }
  299. mutex_init(&radio->lock);
  300. strlcpy(radio->vdev.name, radio->v4l2_dev.name,
  301. sizeof(radio->vdev.name));
  302. radio->vdev.v4l2_dev = &radio->v4l2_dev;
  303. radio->vdev.fops = &usb_raremono_fops;
  304. radio->vdev.ioctl_ops = &usb_raremono_ioctl_ops;
  305. radio->vdev.lock = &radio->lock;
  306. radio->vdev.release = video_device_release_empty;
  307. radio->v4l2_dev.release = raremono_device_release;
  308. usb_set_intfdata(intf, &radio->v4l2_dev);
  309. video_set_drvdata(&radio->vdev, radio);
  310. raremono_cmd_main(radio, BAND_FM, 95160);
  311. retval = video_register_device(&radio->vdev, VFL_TYPE_RADIO, -1);
  312. if (retval == 0) {
  313. dev_info(&intf->dev, "V4L2 device registered as %s\n",
  314. video_device_node_name(&radio->vdev));
  315. return 0;
  316. }
  317. dev_err(&intf->dev, "could not register video device\n");
  318. v4l2_device_unregister(&radio->v4l2_dev);
  319. free_mem:
  320. kfree(radio->buffer);
  321. kfree(radio);
  322. return retval;
  323. }
  324. /* USB subsystem interface */
  325. static struct usb_driver usb_raremono_driver = {
  326. .name = "radio-raremono",
  327. .probe = usb_raremono_probe,
  328. .disconnect = usb_raremono_disconnect,
  329. .id_table = usb_raremono_device_table,
  330. };
  331. module_usb_driver(usb_raremono_driver);