radio-keene.c 12 KB

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