dsbr100.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /* A driver for the D-Link DSB-R100 USB radio and Gemtek USB Radio 21.
  3. * The device plugs into both the USB and an analog audio input, so this thing
  4. * only deals with initialisation and frequency setting, the
  5. * audio data has to be handled by a sound driver.
  6. *
  7. * Major issue: I can't find out where the device reports the signal
  8. * strength, and indeed the windows software appearantly just looks
  9. * at the stereo indicator as well. So, scanning will only find
  10. * stereo stations. Sad, but I can't help it.
  11. *
  12. * Also, the windows program sends oodles of messages over to the
  13. * device, and I couldn't figure out their meaning. My suspicion
  14. * is that they don't have any:-)
  15. *
  16. * You might find some interesting stuff about this module at
  17. * http://unimut.fsk.uni-heidelberg.de/unimut/demi/dsbr
  18. *
  19. * Fully tested with the Keene USB FM Transmitter and the v4l2-compliance tool.
  20. *
  21. * Copyright (c) 2000 Markus Demleitner <[email protected]>
  22. */
  23. #include <linux/kernel.h>
  24. #include <linux/module.h>
  25. #include <linux/init.h>
  26. #include <linux/slab.h>
  27. #include <linux/input.h>
  28. #include <linux/videodev2.h>
  29. #include <linux/usb.h>
  30. #include <media/v4l2-device.h>
  31. #include <media/v4l2-ioctl.h>
  32. #include <media/v4l2-ctrls.h>
  33. #include <media/v4l2-event.h>
  34. /*
  35. * Version Information
  36. */
  37. MODULE_AUTHOR("Markus Demleitner <[email protected]>");
  38. MODULE_DESCRIPTION("D-Link DSB-R100 USB FM radio driver");
  39. MODULE_LICENSE("GPL");
  40. MODULE_VERSION("1.1.0");
  41. #define DSB100_VENDOR 0x04b4
  42. #define DSB100_PRODUCT 0x1002
  43. /* Commands the device appears to understand */
  44. #define DSB100_TUNE 1
  45. #define DSB100_ONOFF 2
  46. #define TB_LEN 16
  47. /* Frequency limits in MHz -- these are European values. For Japanese
  48. devices, that would be 76 and 91. */
  49. #define FREQ_MIN 87.5
  50. #define FREQ_MAX 108.0
  51. #define FREQ_MUL 16000
  52. #define v4l2_dev_to_radio(d) container_of(d, struct dsbr100_device, v4l2_dev)
  53. static int radio_nr = -1;
  54. module_param(radio_nr, int, 0);
  55. /* Data for one (physical) device */
  56. struct dsbr100_device {
  57. struct usb_device *usbdev;
  58. struct video_device videodev;
  59. struct v4l2_device v4l2_dev;
  60. struct v4l2_ctrl_handler hdl;
  61. u8 *transfer_buffer;
  62. struct mutex v4l2_lock;
  63. int curfreq;
  64. bool stereo;
  65. bool muted;
  66. };
  67. /* Low-level device interface begins here */
  68. /* set a frequency, freq is defined by v4l's TUNER_LOW, i.e. 1/16th kHz */
  69. static int dsbr100_setfreq(struct dsbr100_device *radio, unsigned freq)
  70. {
  71. unsigned f = (freq / 16 * 80) / 1000 + 856;
  72. int retval = 0;
  73. if (!radio->muted) {
  74. retval = usb_control_msg(radio->usbdev,
  75. usb_rcvctrlpipe(radio->usbdev, 0),
  76. DSB100_TUNE,
  77. USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN,
  78. (f >> 8) & 0x00ff, f & 0xff,
  79. radio->transfer_buffer, 8, 300);
  80. if (retval >= 0)
  81. mdelay(1);
  82. }
  83. if (retval >= 0) {
  84. radio->curfreq = freq;
  85. return 0;
  86. }
  87. dev_err(&radio->usbdev->dev,
  88. "%s - usb_control_msg returned %i, request %i\n",
  89. __func__, retval, DSB100_TUNE);
  90. return retval;
  91. }
  92. /* switch on radio */
  93. static int dsbr100_start(struct dsbr100_device *radio)
  94. {
  95. int retval = usb_control_msg(radio->usbdev,
  96. usb_rcvctrlpipe(radio->usbdev, 0),
  97. DSB100_ONOFF,
  98. USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN,
  99. 0x01, 0x00, radio->transfer_buffer, 8, 300);
  100. if (retval >= 0)
  101. return dsbr100_setfreq(radio, radio->curfreq);
  102. dev_err(&radio->usbdev->dev,
  103. "%s - usb_control_msg returned %i, request %i\n",
  104. __func__, retval, DSB100_ONOFF);
  105. return retval;
  106. }
  107. /* switch off radio */
  108. static int dsbr100_stop(struct dsbr100_device *radio)
  109. {
  110. int retval = usb_control_msg(radio->usbdev,
  111. usb_rcvctrlpipe(radio->usbdev, 0),
  112. DSB100_ONOFF,
  113. USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN,
  114. 0x00, 0x00, radio->transfer_buffer, 8, 300);
  115. if (retval >= 0)
  116. return 0;
  117. dev_err(&radio->usbdev->dev,
  118. "%s - usb_control_msg returned %i, request %i\n",
  119. __func__, retval, DSB100_ONOFF);
  120. return retval;
  121. }
  122. /* return the device status. This is, in effect, just whether it
  123. sees a stereo signal or not. Pity. */
  124. static void dsbr100_getstat(struct dsbr100_device *radio)
  125. {
  126. int retval = usb_control_msg(radio->usbdev,
  127. usb_rcvctrlpipe(radio->usbdev, 0),
  128. USB_REQ_GET_STATUS,
  129. USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN,
  130. 0x00, 0x24, radio->transfer_buffer, 8, 300);
  131. if (retval < 0) {
  132. radio->stereo = false;
  133. dev_err(&radio->usbdev->dev,
  134. "%s - usb_control_msg returned %i, request %i\n",
  135. __func__, retval, USB_REQ_GET_STATUS);
  136. } else {
  137. radio->stereo = !(radio->transfer_buffer[0] & 0x01);
  138. }
  139. }
  140. static int vidioc_querycap(struct file *file, void *priv,
  141. struct v4l2_capability *v)
  142. {
  143. struct dsbr100_device *radio = video_drvdata(file);
  144. strscpy(v->driver, "dsbr100", sizeof(v->driver));
  145. strscpy(v->card, "D-Link R-100 USB FM Radio", sizeof(v->card));
  146. usb_make_path(radio->usbdev, v->bus_info, sizeof(v->bus_info));
  147. return 0;
  148. }
  149. static int vidioc_g_tuner(struct file *file, void *priv,
  150. struct v4l2_tuner *v)
  151. {
  152. struct dsbr100_device *radio = video_drvdata(file);
  153. if (v->index > 0)
  154. return -EINVAL;
  155. dsbr100_getstat(radio);
  156. strscpy(v->name, "FM", sizeof(v->name));
  157. v->type = V4L2_TUNER_RADIO;
  158. v->rangelow = FREQ_MIN * FREQ_MUL;
  159. v->rangehigh = FREQ_MAX * FREQ_MUL;
  160. v->rxsubchans = radio->stereo ? V4L2_TUNER_SUB_STEREO :
  161. V4L2_TUNER_SUB_MONO;
  162. v->capability = V4L2_TUNER_CAP_LOW | V4L2_TUNER_CAP_STEREO;
  163. v->audmode = V4L2_TUNER_MODE_STEREO;
  164. v->signal = radio->stereo ? 0xffff : 0; /* We can't get the signal strength */
  165. return 0;
  166. }
  167. static int vidioc_s_tuner(struct file *file, void *priv,
  168. const struct v4l2_tuner *v)
  169. {
  170. return v->index ? -EINVAL : 0;
  171. }
  172. static int vidioc_s_frequency(struct file *file, void *priv,
  173. const struct v4l2_frequency *f)
  174. {
  175. struct dsbr100_device *radio = video_drvdata(file);
  176. if (f->tuner != 0 || f->type != V4L2_TUNER_RADIO)
  177. return -EINVAL;
  178. return dsbr100_setfreq(radio, clamp_t(unsigned, f->frequency,
  179. FREQ_MIN * FREQ_MUL, FREQ_MAX * FREQ_MUL));
  180. }
  181. static int vidioc_g_frequency(struct file *file, void *priv,
  182. struct v4l2_frequency *f)
  183. {
  184. struct dsbr100_device *radio = video_drvdata(file);
  185. if (f->tuner)
  186. return -EINVAL;
  187. f->type = V4L2_TUNER_RADIO;
  188. f->frequency = radio->curfreq;
  189. return 0;
  190. }
  191. static int usb_dsbr100_s_ctrl(struct v4l2_ctrl *ctrl)
  192. {
  193. struct dsbr100_device *radio =
  194. container_of(ctrl->handler, struct dsbr100_device, hdl);
  195. switch (ctrl->id) {
  196. case V4L2_CID_AUDIO_MUTE:
  197. radio->muted = ctrl->val;
  198. return radio->muted ? dsbr100_stop(radio) : dsbr100_start(radio);
  199. }
  200. return -EINVAL;
  201. }
  202. /* USB subsystem interface begins here */
  203. /*
  204. * Handle unplugging of the device.
  205. * We call video_unregister_device in any case.
  206. * The last function called in this procedure is
  207. * usb_dsbr100_video_device_release
  208. */
  209. static void usb_dsbr100_disconnect(struct usb_interface *intf)
  210. {
  211. struct dsbr100_device *radio = usb_get_intfdata(intf);
  212. mutex_lock(&radio->v4l2_lock);
  213. /*
  214. * Disconnect is also called on unload, and in that case we need to
  215. * mute the device. This call will silently fail if it is called
  216. * after a physical disconnect.
  217. */
  218. usb_control_msg(radio->usbdev,
  219. usb_rcvctrlpipe(radio->usbdev, 0),
  220. DSB100_ONOFF,
  221. USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN,
  222. 0x00, 0x00, radio->transfer_buffer, 8, 300);
  223. usb_set_intfdata(intf, NULL);
  224. video_unregister_device(&radio->videodev);
  225. v4l2_device_disconnect(&radio->v4l2_dev);
  226. mutex_unlock(&radio->v4l2_lock);
  227. v4l2_device_put(&radio->v4l2_dev);
  228. }
  229. /* Suspend device - stop device. */
  230. static int usb_dsbr100_suspend(struct usb_interface *intf, pm_message_t message)
  231. {
  232. struct dsbr100_device *radio = usb_get_intfdata(intf);
  233. mutex_lock(&radio->v4l2_lock);
  234. if (!radio->muted && dsbr100_stop(radio) < 0)
  235. dev_warn(&intf->dev, "dsbr100_stop failed\n");
  236. mutex_unlock(&radio->v4l2_lock);
  237. dev_info(&intf->dev, "going into suspend..\n");
  238. return 0;
  239. }
  240. /* Resume device - start device. */
  241. static int usb_dsbr100_resume(struct usb_interface *intf)
  242. {
  243. struct dsbr100_device *radio = usb_get_intfdata(intf);
  244. mutex_lock(&radio->v4l2_lock);
  245. if (!radio->muted && dsbr100_start(radio) < 0)
  246. dev_warn(&intf->dev, "dsbr100_start failed\n");
  247. mutex_unlock(&radio->v4l2_lock);
  248. dev_info(&intf->dev, "coming out of suspend..\n");
  249. return 0;
  250. }
  251. /* free data structures */
  252. static void usb_dsbr100_release(struct v4l2_device *v4l2_dev)
  253. {
  254. struct dsbr100_device *radio = v4l2_dev_to_radio(v4l2_dev);
  255. v4l2_ctrl_handler_free(&radio->hdl);
  256. v4l2_device_unregister(&radio->v4l2_dev);
  257. kfree(radio->transfer_buffer);
  258. kfree(radio);
  259. }
  260. static const struct v4l2_ctrl_ops usb_dsbr100_ctrl_ops = {
  261. .s_ctrl = usb_dsbr100_s_ctrl,
  262. };
  263. /* File system interface */
  264. static const struct v4l2_file_operations usb_dsbr100_fops = {
  265. .owner = THIS_MODULE,
  266. .unlocked_ioctl = video_ioctl2,
  267. .open = v4l2_fh_open,
  268. .release = v4l2_fh_release,
  269. .poll = v4l2_ctrl_poll,
  270. };
  271. static const struct v4l2_ioctl_ops usb_dsbr100_ioctl_ops = {
  272. .vidioc_querycap = vidioc_querycap,
  273. .vidioc_g_tuner = vidioc_g_tuner,
  274. .vidioc_s_tuner = vidioc_s_tuner,
  275. .vidioc_g_frequency = vidioc_g_frequency,
  276. .vidioc_s_frequency = vidioc_s_frequency,
  277. .vidioc_log_status = v4l2_ctrl_log_status,
  278. .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
  279. .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
  280. };
  281. /* check if the device is present and register with v4l and usb if it is */
  282. static int usb_dsbr100_probe(struct usb_interface *intf,
  283. const struct usb_device_id *id)
  284. {
  285. struct dsbr100_device *radio;
  286. struct v4l2_device *v4l2_dev;
  287. int retval;
  288. radio = kzalloc(sizeof(struct dsbr100_device), GFP_KERNEL);
  289. if (!radio)
  290. return -ENOMEM;
  291. radio->transfer_buffer = kmalloc(TB_LEN, GFP_KERNEL);
  292. if (!(radio->transfer_buffer)) {
  293. kfree(radio);
  294. return -ENOMEM;
  295. }
  296. v4l2_dev = &radio->v4l2_dev;
  297. v4l2_dev->release = usb_dsbr100_release;
  298. retval = v4l2_device_register(&intf->dev, v4l2_dev);
  299. if (retval < 0) {
  300. v4l2_err(v4l2_dev, "couldn't register v4l2_device\n");
  301. goto err_reg_dev;
  302. }
  303. v4l2_ctrl_handler_init(&radio->hdl, 1);
  304. v4l2_ctrl_new_std(&radio->hdl, &usb_dsbr100_ctrl_ops,
  305. V4L2_CID_AUDIO_MUTE, 0, 1, 1, 1);
  306. if (radio->hdl.error) {
  307. retval = radio->hdl.error;
  308. v4l2_err(v4l2_dev, "couldn't register control\n");
  309. goto err_reg_ctrl;
  310. }
  311. mutex_init(&radio->v4l2_lock);
  312. strscpy(radio->videodev.name, v4l2_dev->name,
  313. sizeof(radio->videodev.name));
  314. radio->videodev.v4l2_dev = v4l2_dev;
  315. radio->videodev.fops = &usb_dsbr100_fops;
  316. radio->videodev.ioctl_ops = &usb_dsbr100_ioctl_ops;
  317. radio->videodev.release = video_device_release_empty;
  318. radio->videodev.lock = &radio->v4l2_lock;
  319. radio->videodev.ctrl_handler = &radio->hdl;
  320. radio->videodev.device_caps = V4L2_CAP_RADIO | V4L2_CAP_TUNER;
  321. radio->usbdev = interface_to_usbdev(intf);
  322. radio->curfreq = FREQ_MIN * FREQ_MUL;
  323. radio->muted = true;
  324. video_set_drvdata(&radio->videodev, radio);
  325. usb_set_intfdata(intf, radio);
  326. retval = video_register_device(&radio->videodev, VFL_TYPE_RADIO, radio_nr);
  327. if (retval == 0)
  328. return 0;
  329. v4l2_err(v4l2_dev, "couldn't register video device\n");
  330. err_reg_ctrl:
  331. v4l2_ctrl_handler_free(&radio->hdl);
  332. v4l2_device_unregister(v4l2_dev);
  333. err_reg_dev:
  334. kfree(radio->transfer_buffer);
  335. kfree(radio);
  336. return retval;
  337. }
  338. static const struct usb_device_id usb_dsbr100_device_table[] = {
  339. { USB_DEVICE(DSB100_VENDOR, DSB100_PRODUCT) },
  340. { } /* Terminating entry */
  341. };
  342. MODULE_DEVICE_TABLE(usb, usb_dsbr100_device_table);
  343. /* USB subsystem interface */
  344. static struct usb_driver usb_dsbr100_driver = {
  345. .name = "dsbr100",
  346. .probe = usb_dsbr100_probe,
  347. .disconnect = usb_dsbr100_disconnect,
  348. .id_table = usb_dsbr100_device_table,
  349. .suspend = usb_dsbr100_suspend,
  350. .resume = usb_dsbr100_resume,
  351. .reset_resume = usb_dsbr100_resume,
  352. };
  353. module_usb_driver(usb_dsbr100_driver);