dsbr100.c 12 KB

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