radio-sf16fmi.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. /* SF16FMI radio driver for Linux radio support
  2. * heavily based on rtrack driver...
  3. * (c) 1997 M. Kirkwood
  4. * (c) 1998 Petr Vandrovec, vandrove@vc.cvut.cz
  5. *
  6. * Fitted to new interface by Alan Cox <alan.cox@linux.org>
  7. * Made working and cleaned up functions <mikael.hedin@irf.se>
  8. * Support for ISAPnP by Ladislav Michl <ladis@psi.cz>
  9. *
  10. * Notes on the hardware
  11. *
  12. * Frequency control is done digitally -- ie out(port,encodefreq(95.8));
  13. * No volume control - only mute/unmute - you have to use line volume
  14. * control on SB-part of SF16FMI
  15. *
  16. */
  17. #include <linux/kernel.h> /* __setup */
  18. #include <linux/module.h> /* Modules */
  19. #include <linux/init.h> /* Initdata */
  20. #include <linux/ioport.h> /* request_region */
  21. #include <linux/delay.h> /* udelay */
  22. #include <linux/videodev.h> /* kernel radio structs */
  23. #include <linux/isapnp.h>
  24. #include <asm/io.h> /* outb, outb_p */
  25. #include <asm/uaccess.h> /* copy to/from user */
  26. #include <asm/semaphore.h>
  27. struct fmi_device
  28. {
  29. int port;
  30. int curvol; /* 1 or 0 */
  31. unsigned long curfreq; /* freq in kHz */
  32. __u32 flags;
  33. };
  34. static int io = -1;
  35. static int radio_nr = -1;
  36. static struct pnp_dev *dev = NULL;
  37. static struct semaphore lock;
  38. /* freq is in 1/16 kHz to internal number, hw precision is 50 kHz */
  39. /* It is only useful to give freq in intervall of 800 (=0.05Mhz),
  40. * other bits will be truncated, e.g 92.7400016 -> 92.7, but
  41. * 92.7400017 -> 92.75
  42. */
  43. #define RSF16_ENCODE(x) ((x)/800+214)
  44. #define RSF16_MINFREQ 87*16000
  45. #define RSF16_MAXFREQ 108*16000
  46. static void outbits(int bits, unsigned int data, int port)
  47. {
  48. while(bits--) {
  49. if(data & 1) {
  50. outb(5, port);
  51. udelay(6);
  52. outb(7, port);
  53. udelay(6);
  54. } else {
  55. outb(1, port);
  56. udelay(6);
  57. outb(3, port);
  58. udelay(6);
  59. }
  60. data>>=1;
  61. }
  62. }
  63. static inline void fmi_mute(int port)
  64. {
  65. down(&lock);
  66. outb(0x00, port);
  67. up(&lock);
  68. }
  69. static inline void fmi_unmute(int port)
  70. {
  71. down(&lock);
  72. outb(0x08, port);
  73. up(&lock);
  74. }
  75. static inline int fmi_setfreq(struct fmi_device *dev)
  76. {
  77. int myport = dev->port;
  78. unsigned long freq = dev->curfreq;
  79. down(&lock);
  80. outbits(16, RSF16_ENCODE(freq), myport);
  81. outbits(8, 0xC0, myport);
  82. msleep(143); /* was schedule_timeout(HZ/7) */
  83. up(&lock);
  84. if (dev->curvol) fmi_unmute(myport);
  85. return 0;
  86. }
  87. static inline int fmi_getsigstr(struct fmi_device *dev)
  88. {
  89. int val;
  90. int res;
  91. int myport = dev->port;
  92. down(&lock);
  93. val = dev->curvol ? 0x08 : 0x00; /* unmute/mute */
  94. outb(val, myport);
  95. outb(val | 0x10, myport);
  96. msleep(143); /* was schedule_timeout(HZ/7) */
  97. res = (int)inb(myport+1);
  98. outb(val, myport);
  99. up(&lock);
  100. return (res & 2) ? 0 : 0xFFFF;
  101. }
  102. static int fmi_do_ioctl(struct inode *inode, struct file *file,
  103. unsigned int cmd, void *arg)
  104. {
  105. struct video_device *dev = video_devdata(file);
  106. struct fmi_device *fmi=dev->priv;
  107. switch(cmd)
  108. {
  109. case VIDIOCGCAP:
  110. {
  111. struct video_capability *v = arg;
  112. memset(v,0,sizeof(*v));
  113. strcpy(v->name, "SF16-FMx radio");
  114. v->type=VID_TYPE_TUNER;
  115. v->channels=1;
  116. v->audios=1;
  117. return 0;
  118. }
  119. case VIDIOCGTUNER:
  120. {
  121. struct video_tuner *v = arg;
  122. int mult;
  123. if(v->tuner) /* Only 1 tuner */
  124. return -EINVAL;
  125. strcpy(v->name, "FM");
  126. mult = (fmi->flags & VIDEO_TUNER_LOW) ? 1 : 1000;
  127. v->rangelow = RSF16_MINFREQ/mult;
  128. v->rangehigh = RSF16_MAXFREQ/mult;
  129. v->flags=fmi->flags;
  130. v->mode=VIDEO_MODE_AUTO;
  131. v->signal = fmi_getsigstr(fmi);
  132. return 0;
  133. }
  134. case VIDIOCSTUNER:
  135. {
  136. struct video_tuner *v = arg;
  137. if(v->tuner!=0)
  138. return -EINVAL;
  139. fmi->flags = v->flags & VIDEO_TUNER_LOW;
  140. /* Only 1 tuner so no setting needed ! */
  141. return 0;
  142. }
  143. case VIDIOCGFREQ:
  144. {
  145. unsigned long *freq = arg;
  146. *freq = fmi->curfreq;
  147. if (!(fmi->flags & VIDEO_TUNER_LOW))
  148. *freq /= 1000;
  149. return 0;
  150. }
  151. case VIDIOCSFREQ:
  152. {
  153. unsigned long *freq = arg;
  154. if (!(fmi->flags & VIDEO_TUNER_LOW))
  155. *freq *= 1000;
  156. if (*freq < RSF16_MINFREQ || *freq > RSF16_MAXFREQ )
  157. return -EINVAL;
  158. /*rounding in steps of 800 to match th freq
  159. that will be used */
  160. fmi->curfreq = (*freq/800)*800;
  161. fmi_setfreq(fmi);
  162. return 0;
  163. }
  164. case VIDIOCGAUDIO:
  165. {
  166. struct video_audio *v = arg;
  167. memset(v,0,sizeof(*v));
  168. v->flags=( (!fmi->curvol)*VIDEO_AUDIO_MUTE | VIDEO_AUDIO_MUTABLE);
  169. strcpy(v->name, "Radio");
  170. v->mode=VIDEO_SOUND_STEREO;
  171. return 0;
  172. }
  173. case VIDIOCSAUDIO:
  174. {
  175. struct video_audio *v = arg;
  176. if(v->audio)
  177. return -EINVAL;
  178. fmi->curvol= v->flags&VIDEO_AUDIO_MUTE ? 0 : 1;
  179. fmi->curvol ?
  180. fmi_unmute(fmi->port) : fmi_mute(fmi->port);
  181. return 0;
  182. }
  183. case VIDIOCGUNIT:
  184. {
  185. struct video_unit *v = arg;
  186. v->video=VIDEO_NO_UNIT;
  187. v->vbi=VIDEO_NO_UNIT;
  188. v->radio=dev->minor;
  189. v->audio=0; /* How do we find out this??? */
  190. v->teletext=VIDEO_NO_UNIT;
  191. return 0;
  192. }
  193. default:
  194. return -ENOIOCTLCMD;
  195. }
  196. }
  197. static int fmi_ioctl(struct inode *inode, struct file *file,
  198. unsigned int cmd, unsigned long arg)
  199. {
  200. return video_usercopy(inode, file, cmd, arg, fmi_do_ioctl);
  201. }
  202. static struct fmi_device fmi_unit;
  203. static struct file_operations fmi_fops = {
  204. .owner = THIS_MODULE,
  205. .open = video_exclusive_open,
  206. .release = video_exclusive_release,
  207. .ioctl = fmi_ioctl,
  208. .llseek = no_llseek,
  209. };
  210. static struct video_device fmi_radio=
  211. {
  212. .owner = THIS_MODULE,
  213. .name = "SF16FMx radio",
  214. .type = VID_TYPE_TUNER,
  215. .hardware = VID_HARDWARE_SF16MI,
  216. .fops = &fmi_fops,
  217. };
  218. /* ladis: this is my card. does any other types exist? */
  219. static struct isapnp_device_id id_table[] __devinitdata = {
  220. { ISAPNP_ANY_ID, ISAPNP_ANY_ID,
  221. ISAPNP_VENDOR('M','F','R'), ISAPNP_FUNCTION(0xad10), 0},
  222. { ISAPNP_CARD_END, },
  223. };
  224. MODULE_DEVICE_TABLE(isapnp, id_table);
  225. static int isapnp_fmi_probe(void)
  226. {
  227. int i = 0;
  228. while (id_table[i].card_vendor != 0 && dev == NULL) {
  229. dev = pnp_find_dev(NULL, id_table[i].vendor,
  230. id_table[i].function, NULL);
  231. i++;
  232. }
  233. if (!dev)
  234. return -ENODEV;
  235. if (pnp_device_attach(dev) < 0)
  236. return -EAGAIN;
  237. if (pnp_activate_dev(dev) < 0) {
  238. printk ("radio-sf16fmi: PnP configure failed (out of resources?)\n");
  239. pnp_device_detach(dev);
  240. return -ENOMEM;
  241. }
  242. if (!pnp_port_valid(dev, 0)) {
  243. pnp_device_detach(dev);
  244. return -ENODEV;
  245. }
  246. i = pnp_port_start(dev, 0);
  247. printk ("radio-sf16fmi: PnP reports card at %#x\n", i);
  248. return i;
  249. }
  250. static int __init fmi_init(void)
  251. {
  252. if (io < 0)
  253. io = isapnp_fmi_probe();
  254. if (io < 0) {
  255. printk(KERN_ERR "radio-sf16fmi: No PnP card found.\n");
  256. return io;
  257. }
  258. if (!request_region(io, 2, "radio-sf16fmi")) {
  259. printk(KERN_ERR "radio-sf16fmi: port 0x%x already in use\n", io);
  260. return -EBUSY;
  261. }
  262. fmi_unit.port = io;
  263. fmi_unit.curvol = 0;
  264. fmi_unit.curfreq = 0;
  265. fmi_unit.flags = VIDEO_TUNER_LOW;
  266. fmi_radio.priv = &fmi_unit;
  267. init_MUTEX(&lock);
  268. if (video_register_device(&fmi_radio, VFL_TYPE_RADIO, radio_nr) == -1) {
  269. release_region(io, 2);
  270. return -EINVAL;
  271. }
  272. printk(KERN_INFO "SF16FMx radio card driver at 0x%x\n", io);
  273. /* mute card - prevents noisy bootups */
  274. fmi_mute(io);
  275. return 0;
  276. }
  277. MODULE_AUTHOR("Petr Vandrovec, vandrove@vc.cvut.cz and M. Kirkwood");
  278. MODULE_DESCRIPTION("A driver for the SF16MI radio.");
  279. MODULE_LICENSE("GPL");
  280. module_param(io, int, 0);
  281. MODULE_PARM_DESC(io, "I/O address of the SF16MI card (0x284 or 0x384)");
  282. module_param(radio_nr, int, 0);
  283. static void __exit fmi_cleanup_module(void)
  284. {
  285. video_unregister_device(&fmi_radio);
  286. release_region(io, 2);
  287. if (dev)
  288. pnp_device_detach(dev);
  289. }
  290. module_init(fmi_init);
  291. module_exit(fmi_cleanup_module);