radio-raremono.c 11 KB

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