radio-keene.c 12 KB

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