dsbr100.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  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.40:
  28. Markus: Updates for 2.6.x kernels, code layout changes, name sanitizing
  29. Version 0.30:
  30. Markus: Updates for 2.5.x kernel and more ISO compliant source
  31. Version 0.25:
  32. PSL and Markus: Cleanup, radio now doesn't stop on device close
  33. Version 0.24:
  34. Markus: Hope I got these silly VIDEO_TUNER_LOW issues finally
  35. right. Some minor cleanup, improved standalone compilation
  36. Version 0.23:
  37. Markus: Sign extension bug fixed by declaring transfer_buffer unsigned
  38. Version 0.22:
  39. Markus: Some (brown bag) cleanup in what VIDIOCSTUNER returns,
  40. thanks to Mike Cox for pointing the problem out.
  41. Version 0.21:
  42. Markus: Minor cleanup, warnings if something goes wrong, lame attempt
  43. to adhere to Documentation/CodingStyle
  44. Version 0.2:
  45. Brad Hards <bradh@dynamite.com.au>: Fixes to make it work as non-module
  46. Markus: Copyright clarification
  47. Version 0.01: Markus: initial release
  48. */
  49. #include <linux/kernel.h>
  50. #include <linux/module.h>
  51. #include <linux/init.h>
  52. #include <linux/slab.h>
  53. #include <linux/input.h>
  54. #include <linux/videodev.h>
  55. #include <linux/usb.h>
  56. #include <linux/smp_lock.h>
  57. /*
  58. * Version Information
  59. */
  60. #define DRIVER_VERSION "v0.40"
  61. #define DRIVER_AUTHOR "Markus Demleitner <msdemlei@tucana.harvard.edu>"
  62. #define DRIVER_DESC "D-Link DSB-R100 USB FM radio driver"
  63. #define DSB100_VENDOR 0x04b4
  64. #define DSB100_PRODUCT 0x1002
  65. /* Commands the device appears to understand */
  66. #define DSB100_TUNE 1
  67. #define DSB100_ONOFF 2
  68. #define TB_LEN 16
  69. /* Frequency limits in MHz -- these are European values. For Japanese
  70. devices, that would be 76 and 91. */
  71. #define FREQ_MIN 87.5
  72. #define FREQ_MAX 108.0
  73. #define FREQ_MUL 16000
  74. static int usb_dsbr100_probe(struct usb_interface *intf,
  75. const struct usb_device_id *id);
  76. static void usb_dsbr100_disconnect(struct usb_interface *intf);
  77. static int usb_dsbr100_ioctl(struct inode *inode, struct file *file,
  78. unsigned int cmd, unsigned long arg);
  79. static int usb_dsbr100_open(struct inode *inode, struct file *file);
  80. static int usb_dsbr100_close(struct inode *inode, struct file *file);
  81. static int radio_nr = -1;
  82. module_param(radio_nr, int, 0);
  83. /* Data for one (physical) device */
  84. typedef struct {
  85. struct usb_device *usbdev;
  86. struct video_device *videodev;
  87. unsigned char transfer_buffer[TB_LEN];
  88. int curfreq;
  89. int stereo;
  90. int users;
  91. int removed;
  92. } dsbr100_device;
  93. /* File system interface */
  94. static struct file_operations usb_dsbr100_fops = {
  95. .owner = THIS_MODULE,
  96. .open = usb_dsbr100_open,
  97. .release = usb_dsbr100_close,
  98. .ioctl = usb_dsbr100_ioctl,
  99. .llseek = no_llseek,
  100. };
  101. /* V4L interface */
  102. static struct video_device dsbr100_videodev_template=
  103. {
  104. .owner = THIS_MODULE,
  105. .name = "D-Link DSB-R 100",
  106. .type = VID_TYPE_TUNER,
  107. .hardware = VID_HARDWARE_AZTECH,
  108. .fops = &usb_dsbr100_fops,
  109. .release = video_device_release,
  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. .owner = THIS_MODULE,
  119. .name = "dsbr100",
  120. .probe = usb_dsbr100_probe,
  121. .disconnect = usb_dsbr100_disconnect,
  122. .id_table = usb_dsbr100_device_table,
  123. };
  124. /* Low-level device interface begins here */
  125. /* switch on radio */
  126. static int dsbr100_start(dsbr100_device *radio)
  127. {
  128. if (usb_control_msg(radio->usbdev, usb_rcvctrlpipe(radio->usbdev, 0),
  129. USB_REQ_GET_STATUS,
  130. USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN,
  131. 0x00, 0xC7, radio->transfer_buffer, 8, 300)<0 ||
  132. usb_control_msg(radio->usbdev, usb_rcvctrlpipe(radio->usbdev, 0),
  133. DSB100_ONOFF,
  134. USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN,
  135. 0x01, 0x00, radio->transfer_buffer, 8, 300)<0)
  136. return -1;
  137. return (radio->transfer_buffer)[0];
  138. }
  139. /* switch off radio */
  140. static int dsbr100_stop(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. return (radio->transfer_buffer)[0];
  152. }
  153. /* set a frequency, freq is defined by v4l's TUNER_LOW, i.e. 1/16th kHz */
  154. static int dsbr100_setfreq(dsbr100_device *radio, int freq)
  155. {
  156. freq = (freq/16*80)/1000+856;
  157. if (usb_control_msg(radio->usbdev, usb_rcvctrlpipe(radio->usbdev, 0),
  158. DSB100_TUNE,
  159. USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN,
  160. (freq>>8)&0x00ff, freq&0xff,
  161. radio->transfer_buffer, 8, 300)<0 ||
  162. usb_control_msg(radio->usbdev, usb_rcvctrlpipe(radio->usbdev, 0),
  163. USB_REQ_GET_STATUS,
  164. USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN,
  165. 0x96, 0xB7, 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. 0x00, 0x24, radio->transfer_buffer, 8, 300)<0) {
  170. radio->stereo = -1;
  171. return -1;
  172. }
  173. radio->stereo = ! ((radio->transfer_buffer)[0]&0x01);
  174. return (radio->transfer_buffer)[0];
  175. }
  176. /* return the device status. This is, in effect, just whether it
  177. sees a stereo signal or not. Pity. */
  178. static void dsbr100_getstat(dsbr100_device *radio)
  179. {
  180. if (usb_control_msg(radio->usbdev, usb_rcvctrlpipe(radio->usbdev, 0),
  181. USB_REQ_GET_STATUS,
  182. USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN,
  183. 0x00 , 0x24, radio->transfer_buffer, 8, 300)<0)
  184. radio->stereo = -1;
  185. else
  186. radio->stereo = ! (radio->transfer_buffer[0]&0x01);
  187. }
  188. /* USB subsystem interface begins here */
  189. /* check if the device is present and register with v4l and
  190. usb if it is */
  191. static int usb_dsbr100_probe(struct usb_interface *intf,
  192. const struct usb_device_id *id)
  193. {
  194. dsbr100_device *radio;
  195. if (!(radio = kmalloc(sizeof(dsbr100_device), GFP_KERNEL)))
  196. return -ENOMEM;
  197. if (!(radio->videodev = video_device_alloc())) {
  198. kfree(radio);
  199. return -ENOMEM;
  200. }
  201. memcpy(radio->videodev, &dsbr100_videodev_template,
  202. sizeof(dsbr100_videodev_template));
  203. radio->removed = 0;
  204. radio->users = 0;
  205. radio->usbdev = interface_to_usbdev(intf);
  206. radio->curfreq = FREQ_MIN*FREQ_MUL;
  207. video_set_drvdata(radio->videodev, radio);
  208. if (video_register_device(radio->videodev, VFL_TYPE_RADIO,
  209. radio_nr)) {
  210. warn("Could not register video device");
  211. video_device_release(radio->videodev);
  212. kfree(radio);
  213. return -EIO;
  214. }
  215. usb_set_intfdata(intf, radio);
  216. return 0;
  217. }
  218. /* handle unplugging of the device, release data structures
  219. if nothing keeps us from doing it. If something is still
  220. keeping us busy, the release callback of v4l will take care
  221. of releasing it. stv680.c does not relase its private
  222. data, so I don't do this here either. Checking out the
  223. code I'd expect I better did that, but if there's a memory
  224. leak here it's tiny (~50 bytes per disconnect) */
  225. static void usb_dsbr100_disconnect(struct usb_interface *intf)
  226. {
  227. dsbr100_device *radio = usb_get_intfdata(intf);
  228. usb_set_intfdata (intf, NULL);
  229. if (radio) {
  230. video_unregister_device(radio->videodev);
  231. radio->videodev = NULL;
  232. if (radio->users) {
  233. kfree(radio);
  234. } else {
  235. radio->removed = 1;
  236. }
  237. }
  238. }
  239. /* Video for Linux interface */
  240. static int usb_dsbr100_do_ioctl(struct inode *inode, struct file *file,
  241. unsigned int cmd, void *arg)
  242. {
  243. dsbr100_device *radio=video_get_drvdata(video_devdata(file));
  244. if (!radio)
  245. return -EIO;
  246. switch(cmd) {
  247. case VIDIOCGCAP: {
  248. struct video_capability *v = arg;
  249. memset(v, 0, sizeof(*v));
  250. v->type = VID_TYPE_TUNER;
  251. v->channels = 1;
  252. v->audios = 1;
  253. strcpy(v->name, "D-Link R-100 USB FM Radio");
  254. return 0;
  255. }
  256. case VIDIOCGTUNER: {
  257. struct video_tuner *v = arg;
  258. dsbr100_getstat(radio);
  259. if(v->tuner) /* Only 1 tuner */
  260. return -EINVAL;
  261. v->rangelow = FREQ_MIN*FREQ_MUL;
  262. v->rangehigh = FREQ_MAX*FREQ_MUL;
  263. v->flags = VIDEO_TUNER_LOW;
  264. v->mode = VIDEO_MODE_AUTO;
  265. v->signal = radio->stereo*0x7000;
  266. /* Don't know how to get signal strength */
  267. v->flags |= VIDEO_TUNER_STEREO_ON*radio->stereo;
  268. strcpy(v->name, "DSB R-100");
  269. return 0;
  270. }
  271. case VIDIOCSTUNER: {
  272. struct video_tuner *v = arg;
  273. if(v->tuner!=0)
  274. return -EINVAL;
  275. /* Only 1 tuner so no setting needed ! */
  276. return 0;
  277. }
  278. case VIDIOCGFREQ: {
  279. int *freq = arg;
  280. if (radio->curfreq==-1)
  281. return -EINVAL;
  282. *freq = radio->curfreq;
  283. return 0;
  284. }
  285. case VIDIOCSFREQ: {
  286. int *freq = arg;
  287. radio->curfreq = *freq;
  288. if (dsbr100_setfreq(radio, radio->curfreq)==-1)
  289. warn("Set frequency failed");
  290. return 0;
  291. }
  292. case VIDIOCGAUDIO: {
  293. struct video_audio *v = arg;
  294. memset(v, 0, sizeof(*v));
  295. v->flags |= VIDEO_AUDIO_MUTABLE;
  296. v->mode = VIDEO_SOUND_STEREO;
  297. v->volume = 1;
  298. v->step = 1;
  299. strcpy(v->name, "Radio");
  300. return 0;
  301. }
  302. case VIDIOCSAUDIO: {
  303. struct video_audio *v = arg;
  304. if (v->audio)
  305. return -EINVAL;
  306. if (v->flags&VIDEO_AUDIO_MUTE) {
  307. if (dsbr100_stop(radio)==-1)
  308. warn("Radio did not respond properly");
  309. }
  310. else
  311. if (dsbr100_start(radio)==-1)
  312. warn("Radio did not respond properly");
  313. return 0;
  314. }
  315. default:
  316. return -ENOIOCTLCMD;
  317. }
  318. }
  319. static int usb_dsbr100_ioctl(struct inode *inode, struct file *file,
  320. unsigned int cmd, unsigned long arg)
  321. {
  322. return video_usercopy(inode, file, cmd, arg, usb_dsbr100_do_ioctl);
  323. }
  324. static int usb_dsbr100_open(struct inode *inode, struct file *file)
  325. {
  326. dsbr100_device *radio=video_get_drvdata(video_devdata(file));
  327. radio->users = 1;
  328. if (dsbr100_start(radio)<0) {
  329. warn("Radio did not start up properly");
  330. radio->users = 0;
  331. return -EIO;
  332. }
  333. dsbr100_setfreq(radio, radio->curfreq);
  334. return 0;
  335. }
  336. static int usb_dsbr100_close(struct inode *inode, struct file *file)
  337. {
  338. dsbr100_device *radio=video_get_drvdata(video_devdata(file));
  339. if (!radio)
  340. return -ENODEV;
  341. radio->users = 0;
  342. if (radio->removed) {
  343. kfree(radio);
  344. }
  345. return 0;
  346. }
  347. static int __init dsbr100_init(void)
  348. {
  349. int retval = usb_register(&usb_dsbr100_driver);
  350. info(DRIVER_VERSION ":" DRIVER_DESC);
  351. return retval;
  352. }
  353. static void __exit dsbr100_exit(void)
  354. {
  355. usb_deregister(&usb_dsbr100_driver);
  356. }
  357. module_init (dsbr100_init);
  358. module_exit (dsbr100_exit);
  359. MODULE_AUTHOR( DRIVER_AUTHOR );
  360. MODULE_DESCRIPTION( DRIVER_DESC );
  361. MODULE_LICENSE("GPL");