radio-keene.c 11 KB

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