radio-keene.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  1. /*
  2. * Copyright (c) 2012 Hans Verkuil <hverkuil@xs4all.nl>
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 2 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. */
  14. /* kernel includes */
  15. #include <linux/kernel.h>
  16. #include <linux/module.h>
  17. #include <linux/init.h>
  18. #include <linux/slab.h>
  19. #include <linux/input.h>
  20. #include <linux/videodev2.h>
  21. #include <media/v4l2-device.h>
  22. #include <media/v4l2-ioctl.h>
  23. #include <media/v4l2-ctrls.h>
  24. #include <media/v4l2-event.h>
  25. #include <linux/usb.h>
  26. #include <linux/mutex.h>
  27. /* driver and module definitions */
  28. MODULE_AUTHOR("Hans Verkuil <hverkuil@xs4all.nl>");
  29. MODULE_DESCRIPTION("Keene FM Transmitter driver");
  30. MODULE_LICENSE("GPL");
  31. /* Actually, it advertises itself as a Logitech */
  32. #define USB_KEENE_VENDOR 0x046d
  33. #define USB_KEENE_PRODUCT 0x0a0e
  34. /* Probably USB_TIMEOUT should be modified in module parameter */
  35. #define BUFFER_LENGTH 8
  36. #define USB_TIMEOUT 500
  37. /* Frequency limits in MHz */
  38. #define FREQ_MIN 76U
  39. #define FREQ_MAX 108U
  40. #define FREQ_MUL 16000U
  41. /* USB Device ID List */
  42. static const struct usb_device_id usb_keene_device_table[] = {
  43. {USB_DEVICE_AND_INTERFACE_INFO(USB_KEENE_VENDOR, USB_KEENE_PRODUCT,
  44. USB_CLASS_HID, 0, 0) },
  45. { } /* Terminating entry */
  46. };
  47. MODULE_DEVICE_TABLE(usb, usb_keene_device_table);
  48. struct keene_device {
  49. struct usb_device *usbdev;
  50. struct usb_interface *intf;
  51. struct video_device vdev;
  52. struct v4l2_device v4l2_dev;
  53. struct v4l2_ctrl_handler hdl;
  54. struct mutex lock;
  55. u8 *buffer;
  56. unsigned curfreq;
  57. u8 tx;
  58. u8 pa;
  59. bool stereo;
  60. bool muted;
  61. bool preemph_75_us;
  62. };
  63. static inline struct keene_device *to_keene_dev(struct v4l2_device *v4l2_dev)
  64. {
  65. return container_of(v4l2_dev, struct keene_device, v4l2_dev);
  66. }
  67. /* Set frequency (if non-0), PA, mute and turn on/off the FM transmitter. */
  68. static int keene_cmd_main(struct keene_device *radio, unsigned freq, bool play)
  69. {
  70. unsigned short freq_send = freq ? (freq - 76 * 16000) / 800 : 0;
  71. int ret;
  72. radio->buffer[0] = 0x00;
  73. radio->buffer[1] = 0x50;
  74. radio->buffer[2] = (freq_send >> 8) & 0xff;
  75. radio->buffer[3] = freq_send & 0xff;
  76. radio->buffer[4] = radio->pa;
  77. /* If bit 4 is set, then tune to the frequency.
  78. If bit 3 is set, then unmute; if bit 2 is set, then mute.
  79. If bit 1 is set, then enter idle mode; if bit 0 is set,
  80. then enter transmit mode.
  81. */
  82. radio->buffer[5] = (radio->muted ? 4 : 8) | (play ? 1 : 2) |
  83. (freq ? 0x10 : 0);
  84. radio->buffer[6] = 0x00;
  85. radio->buffer[7] = 0x00;
  86. ret = usb_control_msg(radio->usbdev, usb_sndctrlpipe(radio->usbdev, 0),
  87. 9, 0x21, 0x200, 2, radio->buffer, BUFFER_LENGTH, USB_TIMEOUT);
  88. if (ret < 0) {
  89. dev_warn(&radio->vdev.dev, "%s failed (%d)\n", __func__, ret);
  90. return ret;
  91. }
  92. if (freq)
  93. radio->curfreq = freq;
  94. return 0;
  95. }
  96. /* Set TX, stereo and preemphasis mode (50 us vs 75 us). */
  97. static int keene_cmd_set(struct keene_device *radio)
  98. {
  99. int ret;
  100. radio->buffer[0] = 0x00;
  101. radio->buffer[1] = 0x51;
  102. radio->buffer[2] = radio->tx;
  103. /* If bit 0 is set, then transmit mono, otherwise stereo.
  104. If bit 2 is set, then enable 75 us preemphasis, otherwise
  105. it is 50 us. */
  106. radio->buffer[3] = (radio->stereo ? 0 : 1) | (radio->preemph_75_us ? 4 : 0);
  107. radio->buffer[4] = 0x00;
  108. radio->buffer[5] = 0x00;
  109. radio->buffer[6] = 0x00;
  110. radio->buffer[7] = 0x00;
  111. ret = usb_control_msg(radio->usbdev, usb_sndctrlpipe(radio->usbdev, 0),
  112. 9, 0x21, 0x200, 2, radio->buffer, BUFFER_LENGTH, USB_TIMEOUT);
  113. if (ret < 0) {
  114. dev_warn(&radio->vdev.dev, "%s failed (%d)\n", __func__, ret);
  115. return ret;
  116. }
  117. return 0;
  118. }
  119. /* Handle unplugging the device.
  120. * We call video_unregister_device in any case.
  121. * The last function called in this procedure is
  122. * usb_keene_device_release.
  123. */
  124. static void usb_keene_disconnect(struct usb_interface *intf)
  125. {
  126. struct keene_device *radio = to_keene_dev(usb_get_intfdata(intf));
  127. mutex_lock(&radio->lock);
  128. usb_set_intfdata(intf, NULL);
  129. video_unregister_device(&radio->vdev);
  130. v4l2_device_disconnect(&radio->v4l2_dev);
  131. mutex_unlock(&radio->lock);
  132. v4l2_device_put(&radio->v4l2_dev);
  133. }
  134. static int usb_keene_suspend(struct usb_interface *intf, pm_message_t message)
  135. {
  136. struct keene_device *radio = to_keene_dev(usb_get_intfdata(intf));
  137. return keene_cmd_main(radio, 0, false);
  138. }
  139. static int usb_keene_resume(struct usb_interface *intf)
  140. {
  141. struct keene_device *radio = to_keene_dev(usb_get_intfdata(intf));
  142. mdelay(50);
  143. keene_cmd_set(radio);
  144. keene_cmd_main(radio, radio->curfreq, true);
  145. return 0;
  146. }
  147. static int vidioc_querycap(struct file *file, void *priv,
  148. struct v4l2_capability *v)
  149. {
  150. struct keene_device *radio = video_drvdata(file);
  151. strlcpy(v->driver, "radio-keene", sizeof(v->driver));
  152. strlcpy(v->card, "Keene FM Transmitter", sizeof(v->card));
  153. usb_make_path(radio->usbdev, v->bus_info, sizeof(v->bus_info));
  154. v->device_caps = V4L2_CAP_RADIO | V4L2_CAP_MODULATOR;
  155. v->capabilities = v->device_caps | V4L2_CAP_DEVICE_CAPS;
  156. return 0;
  157. }
  158. static int vidioc_g_modulator(struct file *file, void *priv,
  159. struct v4l2_modulator *v)
  160. {
  161. struct keene_device *radio = video_drvdata(file);
  162. if (v->index > 0)
  163. return -EINVAL;
  164. strlcpy(v->name, "FM", sizeof(v->name));
  165. v->rangelow = FREQ_MIN * FREQ_MUL;
  166. v->rangehigh = FREQ_MAX * FREQ_MUL;
  167. v->txsubchans = radio->stereo ? V4L2_TUNER_SUB_STEREO : V4L2_TUNER_SUB_MONO;
  168. v->capability = V4L2_TUNER_CAP_LOW | V4L2_TUNER_CAP_STEREO;
  169. return 0;
  170. }
  171. static int vidioc_s_modulator(struct file *file, void *priv,
  172. const struct v4l2_modulator *v)
  173. {
  174. struct keene_device *radio = video_drvdata(file);
  175. if (v->index > 0)
  176. return -EINVAL;
  177. radio->stereo = (v->txsubchans == V4L2_TUNER_SUB_STEREO);
  178. return keene_cmd_set(radio);
  179. }
  180. static int vidioc_s_frequency(struct file *file, void *priv,
  181. const struct v4l2_frequency *f)
  182. {
  183. struct keene_device *radio = video_drvdata(file);
  184. unsigned freq = f->frequency;
  185. if (f->tuner != 0 || f->type != V4L2_TUNER_RADIO)
  186. return -EINVAL;
  187. freq = clamp(freq, FREQ_MIN * FREQ_MUL, FREQ_MAX * FREQ_MUL);
  188. return keene_cmd_main(radio, freq, true);
  189. }
  190. static int vidioc_g_frequency(struct file *file, void *priv,
  191. struct v4l2_frequency *f)
  192. {
  193. struct keene_device *radio = video_drvdata(file);
  194. if (f->tuner != 0)
  195. return -EINVAL;
  196. f->type = V4L2_TUNER_RADIO;
  197. f->frequency = radio->curfreq;
  198. return 0;
  199. }
  200. static int keene_s_ctrl(struct v4l2_ctrl *ctrl)
  201. {
  202. static const u8 db2tx[] = {
  203. /* -15, -12, -9, -6, -3, 0 dB */
  204. 0x03, 0x13, 0x02, 0x12, 0x22, 0x32,
  205. /* 3, 6, 9, 12, 15, 18 dB */
  206. 0x21, 0x31, 0x20, 0x30, 0x40, 0x50
  207. };
  208. struct keene_device *radio =
  209. container_of(ctrl->handler, struct keene_device, hdl);
  210. switch (ctrl->id) {
  211. case V4L2_CID_AUDIO_MUTE:
  212. radio->muted = ctrl->val;
  213. return keene_cmd_main(radio, 0, true);
  214. case V4L2_CID_TUNE_POWER_LEVEL:
  215. /* To go from dBuV to the register value we apply the
  216. following formula: */
  217. radio->pa = (ctrl->val - 71) * 100 / 62;
  218. return keene_cmd_main(radio, 0, true);
  219. case V4L2_CID_TUNE_PREEMPHASIS:
  220. radio->preemph_75_us = ctrl->val == V4L2_PREEMPHASIS_75_uS;
  221. return keene_cmd_set(radio);
  222. case V4L2_CID_AUDIO_COMPRESSION_GAIN:
  223. radio->tx = db2tx[(ctrl->val - (s32)ctrl->minimum) / (s32)ctrl->step];
  224. return keene_cmd_set(radio);
  225. }
  226. return -EINVAL;
  227. }
  228. /* File system interface */
  229. static const struct v4l2_file_operations usb_keene_fops = {
  230. .owner = THIS_MODULE,
  231. .open = v4l2_fh_open,
  232. .release = v4l2_fh_release,
  233. .poll = v4l2_ctrl_poll,
  234. .unlocked_ioctl = video_ioctl2,
  235. };
  236. static const struct v4l2_ctrl_ops keene_ctrl_ops = {
  237. .s_ctrl = keene_s_ctrl,
  238. };
  239. static const struct v4l2_ioctl_ops usb_keene_ioctl_ops = {
  240. .vidioc_querycap = vidioc_querycap,
  241. .vidioc_g_modulator = vidioc_g_modulator,
  242. .vidioc_s_modulator = vidioc_s_modulator,
  243. .vidioc_g_frequency = vidioc_g_frequency,
  244. .vidioc_s_frequency = vidioc_s_frequency,
  245. .vidioc_log_status = v4l2_ctrl_log_status,
  246. .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
  247. .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
  248. };
  249. static void usb_keene_video_device_release(struct v4l2_device *v4l2_dev)
  250. {
  251. struct keene_device *radio = to_keene_dev(v4l2_dev);
  252. /* free rest memory */
  253. v4l2_ctrl_handler_free(&radio->hdl);
  254. kfree(radio->buffer);
  255. kfree(radio);
  256. }
  257. /* check if the device is present and register with v4l and usb if it is */
  258. static int usb_keene_probe(struct usb_interface *intf,
  259. const struct usb_device_id *id)
  260. {
  261. struct usb_device *dev = interface_to_usbdev(intf);
  262. struct keene_device *radio;
  263. struct v4l2_ctrl_handler *hdl;
  264. int retval = 0;
  265. /*
  266. * The Keene FM transmitter USB device has the same USB ID as
  267. * the Logitech AudioHub Speaker, but it should ignore the hid.
  268. * Check if the name is that of the Keene device.
  269. * If not, then someone connected the AudioHub and we shouldn't
  270. * attempt to handle this driver.
  271. * For reference: the product name of the AudioHub is
  272. * "AudioHub Speaker".
  273. */
  274. if (dev->product && strcmp(dev->product, "B-LINK USB Audio "))
  275. return -ENODEV;
  276. radio = kzalloc(sizeof(struct keene_device), GFP_KERNEL);
  277. if (radio)
  278. radio->buffer = kmalloc(BUFFER_LENGTH, GFP_KERNEL);
  279. if (!radio || !radio->buffer) {
  280. dev_err(&intf->dev, "kmalloc for keene_device failed\n");
  281. kfree(radio);
  282. retval = -ENOMEM;
  283. goto err;
  284. }
  285. hdl = &radio->hdl;
  286. v4l2_ctrl_handler_init(hdl, 4);
  287. v4l2_ctrl_new_std(hdl, &keene_ctrl_ops, V4L2_CID_AUDIO_MUTE,
  288. 0, 1, 1, 0);
  289. v4l2_ctrl_new_std_menu(hdl, &keene_ctrl_ops, V4L2_CID_TUNE_PREEMPHASIS,
  290. V4L2_PREEMPHASIS_75_uS, 1, V4L2_PREEMPHASIS_50_uS);
  291. v4l2_ctrl_new_std(hdl, &keene_ctrl_ops, V4L2_CID_TUNE_POWER_LEVEL,
  292. 84, 118, 1, 118);
  293. v4l2_ctrl_new_std(hdl, &keene_ctrl_ops, V4L2_CID_AUDIO_COMPRESSION_GAIN,
  294. -15, 18, 3, 0);
  295. radio->pa = 118;
  296. radio->tx = 0x32;
  297. radio->stereo = true;
  298. if (hdl->error) {
  299. retval = hdl->error;
  300. v4l2_ctrl_handler_free(hdl);
  301. goto err_v4l2;
  302. }
  303. retval = v4l2_device_register(&intf->dev, &radio->v4l2_dev);
  304. if (retval < 0) {
  305. dev_err(&intf->dev, "couldn't register v4l2_device\n");
  306. goto err_v4l2;
  307. }
  308. mutex_init(&radio->lock);
  309. radio->v4l2_dev.ctrl_handler = hdl;
  310. radio->v4l2_dev.release = usb_keene_video_device_release;
  311. strlcpy(radio->vdev.name, radio->v4l2_dev.name,
  312. sizeof(radio->vdev.name));
  313. radio->vdev.v4l2_dev = &radio->v4l2_dev;
  314. radio->vdev.fops = &usb_keene_fops;
  315. radio->vdev.ioctl_ops = &usb_keene_ioctl_ops;
  316. radio->vdev.lock = &radio->lock;
  317. radio->vdev.release = video_device_release_empty;
  318. radio->vdev.vfl_dir = VFL_DIR_TX;
  319. radio->usbdev = interface_to_usbdev(intf);
  320. radio->intf = intf;
  321. usb_set_intfdata(intf, &radio->v4l2_dev);
  322. video_set_drvdata(&radio->vdev, radio);
  323. /* at least 11ms is needed in order to settle hardware */
  324. msleep(20);
  325. keene_cmd_main(radio, 95.16 * FREQ_MUL, false);
  326. retval = video_register_device(&radio->vdev, VFL_TYPE_RADIO, -1);
  327. if (retval < 0) {
  328. dev_err(&intf->dev, "could not register video device\n");
  329. goto err_vdev;
  330. }
  331. v4l2_ctrl_handler_setup(hdl);
  332. dev_info(&intf->dev, "V4L2 device registered as %s\n",
  333. video_device_node_name(&radio->vdev));
  334. return 0;
  335. err_vdev:
  336. v4l2_device_unregister(&radio->v4l2_dev);
  337. err_v4l2:
  338. kfree(radio->buffer);
  339. kfree(radio);
  340. err:
  341. return retval;
  342. }
  343. /* USB subsystem interface */
  344. static struct usb_driver usb_keene_driver = {
  345. .name = "radio-keene",
  346. .probe = usb_keene_probe,
  347. .disconnect = usb_keene_disconnect,
  348. .id_table = usb_keene_device_table,
  349. .suspend = usb_keene_suspend,
  350. .resume = usb_keene_resume,
  351. .reset_resume = usb_keene_resume,
  352. };
  353. module_usb_driver(usb_keene_driver);