dsbr100.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512
  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.42:
  28. Converted dsbr100 to use video_ioctl2
  29. by Douglas Landgraf <dougsland@gmail.com>
  30. Version 0.41-ac1:
  31. Alan Cox: Some cleanups and fixes
  32. Version 0.41:
  33. Converted to V4L2 API by Mauro Carvalho Chehab <mchehab@infradead.org>
  34. Version 0.40:
  35. Markus: Updates for 2.6.x kernels, code layout changes, name sanitizing
  36. Version 0.30:
  37. Markus: Updates for 2.5.x kernel and more ISO compliant source
  38. Version 0.25:
  39. PSL and Markus: Cleanup, radio now doesn't stop on device close
  40. Version 0.24:
  41. Markus: Hope I got these silly VIDEO_TUNER_LOW issues finally
  42. right. Some minor cleanup, improved standalone compilation
  43. Version 0.23:
  44. Markus: Sign extension bug fixed by declaring transfer_buffer unsigned
  45. Version 0.22:
  46. Markus: Some (brown bag) cleanup in what VIDIOCSTUNER returns,
  47. thanks to Mike Cox for pointing the problem out.
  48. Version 0.21:
  49. Markus: Minor cleanup, warnings if something goes wrong, lame attempt
  50. to adhere to Documentation/CodingStyle
  51. Version 0.2:
  52. Brad Hards <bradh@dynamite.com.au>: Fixes to make it work as non-module
  53. Markus: Copyright clarification
  54. Version 0.01: Markus: initial release
  55. */
  56. #include <linux/kernel.h>
  57. #include <linux/module.h>
  58. #include <linux/init.h>
  59. #include <linux/slab.h>
  60. #include <linux/input.h>
  61. #include <linux/videodev2.h>
  62. #include <media/v4l2-common.h>
  63. #include <linux/usb.h>
  64. /*
  65. * Version Information
  66. */
  67. #include <linux/version.h> /* for KERNEL_VERSION MACRO */
  68. #define DRIVER_VERSION "v0.41"
  69. #define RADIO_VERSION KERNEL_VERSION(0,4,1)
  70. static struct v4l2_queryctrl radio_qctrl[] = {
  71. {
  72. .id = V4L2_CID_AUDIO_MUTE,
  73. .name = "Mute",
  74. .minimum = 0,
  75. .maximum = 1,
  76. .default_value = 1,
  77. .type = V4L2_CTRL_TYPE_BOOLEAN,
  78. }
  79. };
  80. #define DRIVER_AUTHOR "Markus Demleitner <msdemlei@tucana.harvard.edu>"
  81. #define DRIVER_DESC "D-Link DSB-R100 USB FM radio driver"
  82. #define DSB100_VENDOR 0x04b4
  83. #define DSB100_PRODUCT 0x1002
  84. /* Commands the device appears to understand */
  85. #define DSB100_TUNE 1
  86. #define DSB100_ONOFF 2
  87. #define TB_LEN 16
  88. /* Frequency limits in MHz -- these are European values. For Japanese
  89. devices, that would be 76 and 91. */
  90. #define FREQ_MIN 87.5
  91. #define FREQ_MAX 108.0
  92. #define FREQ_MUL 16000
  93. static int usb_dsbr100_probe(struct usb_interface *intf,
  94. const struct usb_device_id *id);
  95. static void usb_dsbr100_disconnect(struct usb_interface *intf);
  96. static int usb_dsbr100_open(struct inode *inode, struct file *file);
  97. static int usb_dsbr100_close(struct inode *inode, struct file *file);
  98. static int radio_nr = -1;
  99. module_param(radio_nr, int, 0);
  100. /* Data for one (physical) device */
  101. struct dsbr100_device {
  102. struct usb_device *usbdev;
  103. struct video_device *videodev;
  104. unsigned char transfer_buffer[TB_LEN];
  105. int curfreq;
  106. int stereo;
  107. int users;
  108. int removed;
  109. int muted;
  110. };
  111. static struct usb_device_id usb_dsbr100_device_table [] = {
  112. { USB_DEVICE(DSB100_VENDOR, DSB100_PRODUCT) },
  113. { } /* Terminating entry */
  114. };
  115. MODULE_DEVICE_TABLE (usb, usb_dsbr100_device_table);
  116. /* USB subsystem interface */
  117. static struct usb_driver usb_dsbr100_driver = {
  118. .name = "dsbr100",
  119. .probe = usb_dsbr100_probe,
  120. .disconnect = usb_dsbr100_disconnect,
  121. .id_table = usb_dsbr100_device_table,
  122. };
  123. /* Low-level device interface begins here */
  124. /* switch on radio */
  125. static int dsbr100_start(struct dsbr100_device *radio)
  126. {
  127. if (usb_control_msg(radio->usbdev, usb_rcvctrlpipe(radio->usbdev, 0),
  128. USB_REQ_GET_STATUS,
  129. USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN,
  130. 0x00, 0xC7, radio->transfer_buffer, 8, 300)<0 ||
  131. usb_control_msg(radio->usbdev, usb_rcvctrlpipe(radio->usbdev, 0),
  132. DSB100_ONOFF,
  133. USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN,
  134. 0x01, 0x00, radio->transfer_buffer, 8, 300)<0)
  135. return -1;
  136. radio->muted=0;
  137. return (radio->transfer_buffer)[0];
  138. }
  139. /* switch off radio */
  140. static int dsbr100_stop(struct dsbr100_device *radio)
  141. {
  142. if (usb_control_msg(radio->usbdev, usb_rcvctrlpipe(radio->usbdev, 0),
  143. USB_REQ_GET_STATUS,
  144. USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN,
  145. 0x16, 0x1C, radio->transfer_buffer, 8, 300)<0 ||
  146. usb_control_msg(radio->usbdev, usb_rcvctrlpipe(radio->usbdev, 0),
  147. DSB100_ONOFF,
  148. USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN,
  149. 0x00, 0x00, radio->transfer_buffer, 8, 300)<0)
  150. return -1;
  151. radio->muted=1;
  152. return (radio->transfer_buffer)[0];
  153. }
  154. /* set a frequency, freq is defined by v4l's TUNER_LOW, i.e. 1/16th kHz */
  155. static int dsbr100_setfreq(struct dsbr100_device *radio, int freq)
  156. {
  157. freq = (freq/16*80)/1000+856;
  158. if (usb_control_msg(radio->usbdev, usb_rcvctrlpipe(radio->usbdev, 0),
  159. DSB100_TUNE,
  160. USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN,
  161. (freq>>8)&0x00ff, freq&0xff,
  162. radio->transfer_buffer, 8, 300)<0 ||
  163. usb_control_msg(radio->usbdev, usb_rcvctrlpipe(radio->usbdev, 0),
  164. USB_REQ_GET_STATUS,
  165. USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN,
  166. 0x96, 0xB7, radio->transfer_buffer, 8, 300)<0 ||
  167. usb_control_msg(radio->usbdev, usb_rcvctrlpipe(radio->usbdev, 0),
  168. USB_REQ_GET_STATUS,
  169. USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN,
  170. 0x00, 0x24, radio->transfer_buffer, 8, 300)<0) {
  171. radio->stereo = -1;
  172. return -1;
  173. }
  174. radio->stereo = ! ((radio->transfer_buffer)[0]&0x01);
  175. return (radio->transfer_buffer)[0];
  176. }
  177. /* return the device status. This is, in effect, just whether it
  178. sees a stereo signal or not. Pity. */
  179. static void dsbr100_getstat(struct dsbr100_device *radio)
  180. {
  181. if (usb_control_msg(radio->usbdev, usb_rcvctrlpipe(radio->usbdev, 0),
  182. USB_REQ_GET_STATUS,
  183. USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN,
  184. 0x00 , 0x24, radio->transfer_buffer, 8, 300)<0)
  185. radio->stereo = -1;
  186. else
  187. radio->stereo = ! (radio->transfer_buffer[0]&0x01);
  188. }
  189. /* USB subsystem interface begins here */
  190. /* handle unplugging of the device, release data structures
  191. if nothing keeps us from doing it. If something is still
  192. keeping us busy, the release callback of v4l will take care
  193. of releasing it. stv680.c does not relase its private
  194. data, so I don't do this here either. Checking out the
  195. code I'd expect I better did that, but if there's a memory
  196. leak here it's tiny (~50 bytes per disconnect) */
  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);
  206. } else {
  207. radio->removed = 1;
  208. }
  209. }
  210. }
  211. static int vidioc_querycap(struct file *file, void *priv,
  212. struct v4l2_capability *v)
  213. {
  214. strlcpy(v->driver, "dsbr100", sizeof(v->driver));
  215. strlcpy(v->card, "D-Link R-100 USB FM Radio", sizeof(v->card));
  216. sprintf(v->bus_info, "ISA");
  217. v->version = RADIO_VERSION;
  218. v->capabilities = V4L2_CAP_TUNER;
  219. return 0;
  220. }
  221. static int vidioc_g_tuner(struct file *file, void *priv,
  222. struct v4l2_tuner *v)
  223. {
  224. struct dsbr100_device *radio = video_get_drvdata(video_devdata(file));
  225. if (v->index > 0)
  226. return -EINVAL;
  227. dsbr100_getstat(radio);
  228. strcpy(v->name, "FM");
  229. v->type = V4L2_TUNER_RADIO;
  230. v->rangelow = FREQ_MIN*FREQ_MUL;
  231. v->rangehigh = FREQ_MAX*FREQ_MUL;
  232. v->rxsubchans = V4L2_TUNER_SUB_MONO|V4L2_TUNER_SUB_STEREO;
  233. v->capability = V4L2_TUNER_CAP_LOW;
  234. if(radio->stereo)
  235. v->audmode = V4L2_TUNER_MODE_STEREO;
  236. else
  237. v->audmode = V4L2_TUNER_MODE_MONO;
  238. v->signal = 0xffff; /* We can't get the signal strength */
  239. return 0;
  240. }
  241. static int vidioc_s_tuner(struct file *file, void *priv,
  242. struct v4l2_tuner *v)
  243. {
  244. if (v->index > 0)
  245. return -EINVAL;
  246. return 0;
  247. }
  248. static int vidioc_s_frequency(struct file *file, void *priv,
  249. struct v4l2_frequency *f)
  250. {
  251. struct dsbr100_device *radio = video_get_drvdata(video_devdata(file));
  252. radio->curfreq = f->frequency;
  253. if (dsbr100_setfreq(radio, radio->curfreq)==-1)
  254. warn("Set frequency failed");
  255. return 0;
  256. }
  257. static int vidioc_g_frequency(struct file *file, void *priv,
  258. struct v4l2_frequency *f)
  259. {
  260. struct dsbr100_device *radio = video_get_drvdata(video_devdata(file));
  261. f->type = V4L2_TUNER_RADIO;
  262. f->frequency = radio->curfreq;
  263. return 0;
  264. }
  265. static int vidioc_queryctrl(struct file *file, void *priv,
  266. struct v4l2_queryctrl *qc)
  267. {
  268. int i;
  269. for (i = 0; i < ARRAY_SIZE(radio_qctrl); i++) {
  270. if (qc->id && qc->id == radio_qctrl[i].id) {
  271. memcpy(qc, &(radio_qctrl[i]),
  272. 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_get_drvdata(video_devdata(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_get_drvdata(video_devdata(file));
  293. switch (ctrl->id) {
  294. case V4L2_CID_AUDIO_MUTE:
  295. if (ctrl->value) {
  296. if (dsbr100_stop(radio)==-1)
  297. warn("Radio did not respond properly");
  298. } else {
  299. if (dsbr100_start(radio)==-1)
  300. warn("Radio did not respond properly");
  301. }
  302. return 0;
  303. }
  304. return -EINVAL;
  305. }
  306. static int vidioc_g_audio(struct file *file, void *priv,
  307. struct v4l2_audio *a)
  308. {
  309. if (a->index > 1)
  310. return -EINVAL;
  311. strcpy(a->name, "Radio");
  312. a->capability = V4L2_AUDCAP_STEREO;
  313. return 0;
  314. }
  315. static int vidioc_g_input(struct file *filp, void *priv, unsigned int *i)
  316. {
  317. *i = 0;
  318. return 0;
  319. }
  320. static int vidioc_s_input(struct file *filp, void *priv, unsigned int i)
  321. {
  322. if (i != 0)
  323. return -EINVAL;
  324. return 0;
  325. }
  326. static int vidioc_s_audio(struct file *file, void *priv,
  327. struct v4l2_audio *a)
  328. {
  329. if (a->index != 0)
  330. return -EINVAL;
  331. return 0;
  332. }
  333. static int usb_dsbr100_open(struct inode *inode, struct file *file)
  334. {
  335. struct dsbr100_device *radio=video_get_drvdata(video_devdata(file));
  336. radio->users = 1;
  337. radio->muted = 1;
  338. if (dsbr100_start(radio)<0) {
  339. warn("Radio did not start up properly");
  340. radio->users = 0;
  341. return -EIO;
  342. }
  343. dsbr100_setfreq(radio, radio->curfreq);
  344. return 0;
  345. }
  346. static int usb_dsbr100_close(struct inode *inode, struct file *file)
  347. {
  348. struct dsbr100_device *radio=video_get_drvdata(video_devdata(file));
  349. if (!radio)
  350. return -ENODEV;
  351. radio->users = 0;
  352. if (radio->removed) {
  353. kfree(radio);
  354. }
  355. return 0;
  356. }
  357. /* File system interface */
  358. static const struct file_operations usb_dsbr100_fops = {
  359. .owner = THIS_MODULE,
  360. .open = usb_dsbr100_open,
  361. .release = usb_dsbr100_close,
  362. .ioctl = video_ioctl2,
  363. .compat_ioctl = v4l_compat_ioctl32,
  364. .llseek = no_llseek,
  365. };
  366. /* V4L2 interface */
  367. static struct video_device dsbr100_videodev_template =
  368. {
  369. .owner = THIS_MODULE,
  370. .name = "D-Link DSB-R 100",
  371. .type = VID_TYPE_TUNER,
  372. .fops = &usb_dsbr100_fops,
  373. .release = video_device_release,
  374. .vidioc_querycap = vidioc_querycap,
  375. .vidioc_g_tuner = vidioc_g_tuner,
  376. .vidioc_s_tuner = vidioc_s_tuner,
  377. .vidioc_g_frequency = vidioc_g_frequency,
  378. .vidioc_s_frequency = vidioc_s_frequency,
  379. .vidioc_queryctrl = vidioc_queryctrl,
  380. .vidioc_g_ctrl = vidioc_g_ctrl,
  381. .vidioc_s_ctrl = vidioc_s_ctrl,
  382. .vidioc_g_audio = vidioc_g_audio,
  383. .vidioc_s_audio = vidioc_s_audio,
  384. .vidioc_g_input = vidioc_g_input,
  385. .vidioc_s_input = vidioc_s_input,
  386. };
  387. /* check if the device is present and register with v4l and
  388. usb if it is */
  389. static int usb_dsbr100_probe(struct usb_interface *intf,
  390. const struct usb_device_id *id)
  391. {
  392. struct dsbr100_device *radio;
  393. if (!(radio = kmalloc(sizeof(struct dsbr100_device), GFP_KERNEL)))
  394. return -ENOMEM;
  395. if (!(radio->videodev = video_device_alloc())) {
  396. kfree(radio);
  397. return -ENOMEM;
  398. }
  399. memcpy(radio->videodev, &dsbr100_videodev_template,
  400. sizeof(dsbr100_videodev_template));
  401. radio->removed = 0;
  402. radio->users = 0;
  403. radio->usbdev = interface_to_usbdev(intf);
  404. radio->curfreq = FREQ_MIN*FREQ_MUL;
  405. video_set_drvdata(radio->videodev, radio);
  406. if (video_register_device(radio->videodev, VFL_TYPE_RADIO,radio_nr)) {
  407. warn("Could not register video device");
  408. video_device_release(radio->videodev);
  409. kfree(radio);
  410. return -EIO;
  411. }
  412. usb_set_intfdata(intf, radio);
  413. return 0;
  414. }
  415. static int __init dsbr100_init(void)
  416. {
  417. int retval = usb_register(&usb_dsbr100_driver);
  418. info(DRIVER_VERSION ":" DRIVER_DESC);
  419. return retval;
  420. }
  421. static void __exit dsbr100_exit(void)
  422. {
  423. usb_deregister(&usb_dsbr100_driver);
  424. }
  425. module_init (dsbr100_init);
  426. module_exit (dsbr100_exit);
  427. MODULE_AUTHOR( DRIVER_AUTHOR );
  428. MODULE_DESCRIPTION( DRIVER_DESC );
  429. MODULE_LICENSE("GPL");