dsbr100.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547
  1. /* A driver for the D-Link DSB-R100 USB radio. The R100 plugs
  2. 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. Major issue: I can't find out where the device reports the signal
  6. strength, and indeed the windows software appearantly just looks
  7. at the stereo indicator as well. So, scanning will only find
  8. stereo stations. Sad, but I can't help it.
  9. Also, the windows program sends oodles of messages over to the
  10. device, and I couldn't figure out their meaning. My suspicion
  11. is that they don't have any:-)
  12. You might find some interesting stuff about this module at
  13. http://unimut.fsk.uni-heidelberg.de/unimut/demi/dsbr
  14. Copyright (c) 2000 Markus Demleitner <msdemlei@cl.uni-heidelberg.de>
  15. This program is free software; you can redistribute it and/or modify
  16. it under the terms of the GNU General Public License as published by
  17. the Free Software Foundation; either version 2 of the License, or
  18. (at your option) any later version.
  19. This program is distributed in the hope that it will be useful,
  20. but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  22. GNU General Public License for more details.
  23. You should have received a copy of the GNU General Public License
  24. along with this program; if not, write to the Free Software
  25. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  26. History:
  27. Version 0.43:
  28. Oliver Neukum: avoided DMA coherency issue
  29. Version 0.42:
  30. Converted dsbr100 to use video_ioctl2
  31. by Douglas Landgraf <dougsland@gmail.com>
  32. Version 0.41-ac1:
  33. Alan Cox: Some cleanups and fixes
  34. Version 0.41:
  35. Converted to V4L2 API by Mauro Carvalho Chehab <mchehab@infradead.org>
  36. Version 0.40:
  37. Markus: Updates for 2.6.x kernels, code layout changes, name sanitizing
  38. Version 0.30:
  39. Markus: Updates for 2.5.x kernel and more ISO compliant source
  40. Version 0.25:
  41. PSL and Markus: Cleanup, radio now doesn't stop on device close
  42. Version 0.24:
  43. Markus: Hope I got these silly VIDEO_TUNER_LOW issues finally
  44. right. Some minor cleanup, improved standalone compilation
  45. Version 0.23:
  46. Markus: Sign extension bug fixed by declaring transfer_buffer unsigned
  47. Version 0.22:
  48. Markus: Some (brown bag) cleanup in what VIDIOCSTUNER returns,
  49. thanks to Mike Cox for pointing the problem out.
  50. Version 0.21:
  51. Markus: Minor cleanup, warnings if something goes wrong, lame attempt
  52. to adhere to Documentation/CodingStyle
  53. Version 0.2:
  54. Brad Hards <bradh@dynamite.com.au>: Fixes to make it work as non-module
  55. Markus: Copyright clarification
  56. Version 0.01: Markus: initial release
  57. */
  58. #include <linux/kernel.h>
  59. #include <linux/module.h>
  60. #include <linux/init.h>
  61. #include <linux/slab.h>
  62. #include <linux/input.h>
  63. #include <linux/videodev2.h>
  64. #include <media/v4l2-common.h>
  65. #include <media/v4l2-ioctl.h>
  66. #include <linux/usb.h>
  67. /*
  68. * Version Information
  69. */
  70. #include <linux/version.h> /* for KERNEL_VERSION MACRO */
  71. #define DRIVER_VERSION "v0.41"
  72. #define RADIO_VERSION KERNEL_VERSION(0,4,1)
  73. static struct v4l2_queryctrl radio_qctrl[] = {
  74. {
  75. .id = V4L2_CID_AUDIO_MUTE,
  76. .name = "Mute",
  77. .minimum = 0,
  78. .maximum = 1,
  79. .default_value = 1,
  80. .type = V4L2_CTRL_TYPE_BOOLEAN,
  81. }
  82. };
  83. #define DRIVER_AUTHOR "Markus Demleitner <msdemlei@tucana.harvard.edu>"
  84. #define DRIVER_DESC "D-Link DSB-R100 USB FM radio driver"
  85. #define DSB100_VENDOR 0x04b4
  86. #define DSB100_PRODUCT 0x1002
  87. /* Commands the device appears to understand */
  88. #define DSB100_TUNE 1
  89. #define DSB100_ONOFF 2
  90. #define TB_LEN 16
  91. /* Frequency limits in MHz -- these are European values. For Japanese
  92. devices, that would be 76 and 91. */
  93. #define FREQ_MIN 87.5
  94. #define FREQ_MAX 108.0
  95. #define FREQ_MUL 16000
  96. static int usb_dsbr100_probe(struct usb_interface *intf,
  97. const struct usb_device_id *id);
  98. static void usb_dsbr100_disconnect(struct usb_interface *intf);
  99. static int usb_dsbr100_open(struct inode *inode, struct file *file);
  100. static int usb_dsbr100_close(struct inode *inode, struct file *file);
  101. static int radio_nr = -1;
  102. module_param(radio_nr, int, 0);
  103. /* Data for one (physical) device */
  104. struct dsbr100_device {
  105. struct usb_device *usbdev;
  106. struct video_device *videodev;
  107. u8 *transfer_buffer;
  108. int curfreq;
  109. int stereo;
  110. int users;
  111. int removed;
  112. int muted;
  113. };
  114. static struct usb_device_id usb_dsbr100_device_table [] = {
  115. { USB_DEVICE(DSB100_VENDOR, DSB100_PRODUCT) },
  116. { } /* Terminating entry */
  117. };
  118. MODULE_DEVICE_TABLE (usb, usb_dsbr100_device_table);
  119. /* USB subsystem interface */
  120. static struct usb_driver usb_dsbr100_driver = {
  121. .name = "dsbr100",
  122. .probe = usb_dsbr100_probe,
  123. .disconnect = usb_dsbr100_disconnect,
  124. .id_table = usb_dsbr100_device_table,
  125. };
  126. /* Low-level device interface begins here */
  127. /* switch on radio */
  128. static int dsbr100_start(struct dsbr100_device *radio)
  129. {
  130. if (usb_control_msg(radio->usbdev, usb_rcvctrlpipe(radio->usbdev, 0),
  131. USB_REQ_GET_STATUS,
  132. USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN,
  133. 0x00, 0xC7, radio->transfer_buffer, 8, 300) < 0 ||
  134. usb_control_msg(radio->usbdev, usb_rcvctrlpipe(radio->usbdev, 0),
  135. DSB100_ONOFF,
  136. USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN,
  137. 0x01, 0x00, radio->transfer_buffer, 8, 300) < 0)
  138. return -1;
  139. radio->muted=0;
  140. return (radio->transfer_buffer)[0];
  141. }
  142. /* switch off radio */
  143. static int dsbr100_stop(struct dsbr100_device *radio)
  144. {
  145. if (usb_control_msg(radio->usbdev, usb_rcvctrlpipe(radio->usbdev, 0),
  146. USB_REQ_GET_STATUS,
  147. USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN,
  148. 0x16, 0x1C, radio->transfer_buffer, 8, 300) < 0 ||
  149. usb_control_msg(radio->usbdev, usb_rcvctrlpipe(radio->usbdev, 0),
  150. DSB100_ONOFF,
  151. USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN,
  152. 0x00, 0x00, radio->transfer_buffer, 8, 300) < 0)
  153. return -1;
  154. radio->muted=1;
  155. return (radio->transfer_buffer)[0];
  156. }
  157. /* set a frequency, freq is defined by v4l's TUNER_LOW, i.e. 1/16th kHz */
  158. static int dsbr100_setfreq(struct dsbr100_device *radio, int freq)
  159. {
  160. freq = (freq / 16 * 80) / 1000 + 856;
  161. if (usb_control_msg(radio->usbdev, usb_rcvctrlpipe(radio->usbdev, 0),
  162. DSB100_TUNE,
  163. USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN,
  164. (freq >> 8) & 0x00ff, freq & 0xff,
  165. radio->transfer_buffer, 8, 300) < 0 ||
  166. usb_control_msg(radio->usbdev, usb_rcvctrlpipe(radio->usbdev, 0),
  167. USB_REQ_GET_STATUS,
  168. USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN,
  169. 0x96, 0xB7, radio->transfer_buffer, 8, 300) < 0 ||
  170. usb_control_msg(radio->usbdev, usb_rcvctrlpipe(radio->usbdev, 0),
  171. USB_REQ_GET_STATUS,
  172. USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN,
  173. 0x00, 0x24, radio->transfer_buffer, 8, 300) < 0) {
  174. radio->stereo = -1;
  175. return -1;
  176. }
  177. radio->stereo = !((radio->transfer_buffer)[0] & 0x01);
  178. return (radio->transfer_buffer)[0];
  179. }
  180. /* return the device status. This is, in effect, just whether it
  181. sees a stereo signal or not. Pity. */
  182. static void dsbr100_getstat(struct dsbr100_device *radio)
  183. {
  184. if (usb_control_msg(radio->usbdev, usb_rcvctrlpipe(radio->usbdev, 0),
  185. USB_REQ_GET_STATUS,
  186. USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN,
  187. 0x00 , 0x24, radio->transfer_buffer, 8, 300) < 0)
  188. radio->stereo = -1;
  189. else
  190. radio->stereo = !(radio->transfer_buffer[0] & 0x01);
  191. }
  192. /* USB subsystem interface begins here */
  193. /* handle unplugging of the device, release data structures
  194. if nothing keeps us from doing it. If something is still
  195. keeping us busy, the release callback of v4l will take care
  196. of releasing it. */
  197. static void usb_dsbr100_disconnect(struct usb_interface *intf)
  198. {
  199. struct dsbr100_device *radio = usb_get_intfdata(intf);
  200. usb_set_intfdata (intf, NULL);
  201. if (radio) {
  202. video_unregister_device(radio->videodev);
  203. radio->videodev = NULL;
  204. if (radio->users) {
  205. kfree(radio->transfer_buffer);
  206. kfree(radio);
  207. } else {
  208. radio->removed = 1;
  209. }
  210. }
  211. }
  212. static int vidioc_querycap(struct file *file, void *priv,
  213. struct v4l2_capability *v)
  214. {
  215. strlcpy(v->driver, "dsbr100", sizeof(v->driver));
  216. strlcpy(v->card, "D-Link R-100 USB FM Radio", sizeof(v->card));
  217. sprintf(v->bus_info, "USB");
  218. v->version = RADIO_VERSION;
  219. v->capabilities = V4L2_CAP_TUNER;
  220. return 0;
  221. }
  222. static int vidioc_g_tuner(struct file *file, void *priv,
  223. struct v4l2_tuner *v)
  224. {
  225. struct dsbr100_device *radio = video_drvdata(file);
  226. if (v->index > 0)
  227. return -EINVAL;
  228. dsbr100_getstat(radio);
  229. strcpy(v->name, "FM");
  230. v->type = V4L2_TUNER_RADIO;
  231. v->rangelow = FREQ_MIN * FREQ_MUL;
  232. v->rangehigh = FREQ_MAX * FREQ_MUL;
  233. v->rxsubchans = V4L2_TUNER_SUB_MONO | V4L2_TUNER_SUB_STEREO;
  234. v->capability = V4L2_TUNER_CAP_LOW;
  235. if(radio->stereo)
  236. v->audmode = V4L2_TUNER_MODE_STEREO;
  237. else
  238. v->audmode = V4L2_TUNER_MODE_MONO;
  239. v->signal = 0xffff; /* We can't get the signal strength */
  240. return 0;
  241. }
  242. static int vidioc_s_tuner(struct file *file, void *priv,
  243. struct v4l2_tuner *v)
  244. {
  245. if (v->index > 0)
  246. return -EINVAL;
  247. return 0;
  248. }
  249. static int vidioc_s_frequency(struct file *file, void *priv,
  250. struct v4l2_frequency *f)
  251. {
  252. struct dsbr100_device *radio = video_drvdata(file);
  253. radio->curfreq = f->frequency;
  254. if (dsbr100_setfreq(radio, radio->curfreq) == -1)
  255. dev_warn(&radio->usbdev->dev, "Set frequency failed\n");
  256. return 0;
  257. }
  258. static int vidioc_g_frequency(struct file *file, void *priv,
  259. struct v4l2_frequency *f)
  260. {
  261. struct dsbr100_device *radio = video_drvdata(file);
  262. f->type = V4L2_TUNER_RADIO;
  263. f->frequency = radio->curfreq;
  264. return 0;
  265. }
  266. static int vidioc_queryctrl(struct file *file, void *priv,
  267. struct v4l2_queryctrl *qc)
  268. {
  269. int i;
  270. for (i = 0; i < ARRAY_SIZE(radio_qctrl); i++) {
  271. if (qc->id && qc->id == radio_qctrl[i].id) {
  272. memcpy(qc, &(radio_qctrl[i]), sizeof(*qc));
  273. return 0;
  274. }
  275. }
  276. return -EINVAL;
  277. }
  278. static int vidioc_g_ctrl(struct file *file, void *priv,
  279. struct v4l2_control *ctrl)
  280. {
  281. struct dsbr100_device *radio = video_drvdata(file);
  282. switch (ctrl->id) {
  283. case V4L2_CID_AUDIO_MUTE:
  284. ctrl->value = radio->muted;
  285. return 0;
  286. }
  287. return -EINVAL;
  288. }
  289. static int vidioc_s_ctrl(struct file *file, void *priv,
  290. struct v4l2_control *ctrl)
  291. {
  292. struct dsbr100_device *radio = video_drvdata(file);
  293. switch (ctrl->id) {
  294. case V4L2_CID_AUDIO_MUTE:
  295. if (ctrl->value) {
  296. if (dsbr100_stop(radio) == -1) {
  297. dev_warn(&radio->usbdev->dev,
  298. "Radio did not respond properly\n");
  299. return -EBUSY;
  300. }
  301. } else {
  302. if (dsbr100_start(radio) == -1) {
  303. dev_warn(&radio->usbdev->dev,
  304. "Radio did not respond properly\n");
  305. return -EBUSY;
  306. }
  307. }
  308. return 0;
  309. }
  310. return -EINVAL;
  311. }
  312. static int vidioc_g_audio(struct file *file, void *priv,
  313. struct v4l2_audio *a)
  314. {
  315. if (a->index > 1)
  316. return -EINVAL;
  317. strcpy(a->name, "Radio");
  318. a->capability = V4L2_AUDCAP_STEREO;
  319. return 0;
  320. }
  321. static int vidioc_g_input(struct file *filp, void *priv, unsigned int *i)
  322. {
  323. *i = 0;
  324. return 0;
  325. }
  326. static int vidioc_s_input(struct file *filp, void *priv, unsigned int i)
  327. {
  328. if (i != 0)
  329. return -EINVAL;
  330. return 0;
  331. }
  332. static int vidioc_s_audio(struct file *file, void *priv,
  333. struct v4l2_audio *a)
  334. {
  335. if (a->index != 0)
  336. return -EINVAL;
  337. return 0;
  338. }
  339. static int usb_dsbr100_open(struct inode *inode, struct file *file)
  340. {
  341. struct dsbr100_device *radio = video_drvdata(file);
  342. int retval;
  343. lock_kernel();
  344. radio->users = 1;
  345. radio->muted = 1;
  346. if (dsbr100_start(radio) < 0) {
  347. dev_warn(&radio->usbdev->dev,
  348. "Radio did not start up properly\n");
  349. radio->users = 0;
  350. unlock_kernel();
  351. return -EIO;
  352. }
  353. retval = dsbr100_setfreq(radio, radio->curfreq);
  354. if (retval == -1)
  355. printk(KERN_WARNING KBUILD_MODNAME ": Set frequency failed\n");
  356. unlock_kernel();
  357. return 0;
  358. }
  359. static int usb_dsbr100_close(struct inode *inode, struct file *file)
  360. {
  361. struct dsbr100_device *radio = video_drvdata(file);
  362. if (!radio)
  363. return -ENODEV;
  364. radio->users = 0;
  365. if (radio->removed) {
  366. kfree(radio->transfer_buffer);
  367. kfree(radio);
  368. }
  369. return 0;
  370. }
  371. /* File system interface */
  372. static const struct file_operations usb_dsbr100_fops = {
  373. .owner = THIS_MODULE,
  374. .open = usb_dsbr100_open,
  375. .release = usb_dsbr100_close,
  376. .ioctl = video_ioctl2,
  377. #ifdef CONFIG_COMPAT
  378. .compat_ioctl = v4l_compat_ioctl32,
  379. #endif
  380. .llseek = no_llseek,
  381. };
  382. static const struct v4l2_ioctl_ops usb_dsbr100_ioctl_ops = {
  383. .vidioc_querycap = vidioc_querycap,
  384. .vidioc_g_tuner = vidioc_g_tuner,
  385. .vidioc_s_tuner = vidioc_s_tuner,
  386. .vidioc_g_frequency = vidioc_g_frequency,
  387. .vidioc_s_frequency = vidioc_s_frequency,
  388. .vidioc_queryctrl = vidioc_queryctrl,
  389. .vidioc_g_ctrl = vidioc_g_ctrl,
  390. .vidioc_s_ctrl = vidioc_s_ctrl,
  391. .vidioc_g_audio = vidioc_g_audio,
  392. .vidioc_s_audio = vidioc_s_audio,
  393. .vidioc_g_input = vidioc_g_input,
  394. .vidioc_s_input = vidioc_s_input,
  395. };
  396. /* V4L2 interface */
  397. static struct video_device dsbr100_videodev_template = {
  398. .name = "D-Link DSB-R 100",
  399. .fops = &usb_dsbr100_fops,
  400. .ioctl_ops = &usb_dsbr100_ioctl_ops,
  401. .release = video_device_release,
  402. };
  403. /* check if the device is present and register with v4l and
  404. usb if it is */
  405. static int usb_dsbr100_probe(struct usb_interface *intf,
  406. const struct usb_device_id *id)
  407. {
  408. struct dsbr100_device *radio;
  409. radio = kmalloc(sizeof(struct dsbr100_device), GFP_KERNEL);
  410. if (!radio)
  411. return -ENOMEM;
  412. radio->transfer_buffer = kmalloc(TB_LEN, GFP_KERNEL);
  413. if (!(radio->transfer_buffer)) {
  414. kfree(radio);
  415. return -ENOMEM;
  416. }
  417. radio->videodev = video_device_alloc();
  418. if (!(radio->videodev)) {
  419. kfree(radio->transfer_buffer);
  420. kfree(radio);
  421. return -ENOMEM;
  422. }
  423. memcpy(radio->videodev, &dsbr100_videodev_template,
  424. sizeof(dsbr100_videodev_template));
  425. radio->removed = 0;
  426. radio->users = 0;
  427. radio->usbdev = interface_to_usbdev(intf);
  428. radio->curfreq = FREQ_MIN * FREQ_MUL;
  429. video_set_drvdata(radio->videodev, radio);
  430. if (video_register_device(radio->videodev, VFL_TYPE_RADIO, radio_nr) < 0) {
  431. dev_warn(&intf->dev, "Could not register video device\n");
  432. video_device_release(radio->videodev);
  433. kfree(radio->transfer_buffer);
  434. kfree(radio);
  435. return -EIO;
  436. }
  437. usb_set_intfdata(intf, radio);
  438. return 0;
  439. }
  440. static int __init dsbr100_init(void)
  441. {
  442. int retval = usb_register(&usb_dsbr100_driver);
  443. printk(KERN_INFO KBUILD_MODNAME ": " DRIVER_VERSION ":"
  444. DRIVER_DESC "\n");
  445. return retval;
  446. }
  447. static void __exit dsbr100_exit(void)
  448. {
  449. usb_deregister(&usb_dsbr100_driver);
  450. }
  451. module_init (dsbr100_init);
  452. module_exit (dsbr100_exit);
  453. MODULE_AUTHOR( DRIVER_AUTHOR );
  454. MODULE_DESCRIPTION( DRIVER_DESC );
  455. MODULE_LICENSE("GPL");