dsbr100.c 12 KB

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