radio-sf16fmi.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  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. * Converted to V4L2 API by Mauro Carvalho Chehab <mchehab@infradead.org>
  17. */
  18. #include <linux/version.h>
  19. #include <linux/kernel.h> /* __setup */
  20. #include <linux/module.h> /* Modules */
  21. #include <linux/init.h> /* Initdata */
  22. #include <linux/ioport.h> /* request_region */
  23. #include <linux/delay.h> /* udelay */
  24. #include <linux/videodev2.h> /* kernel radio structs */
  25. #include <media/v4l2-common.h>
  26. #include <linux/isapnp.h>
  27. #include <asm/io.h> /* outb, outb_p */
  28. #include <asm/uaccess.h> /* copy to/from user */
  29. #include <linux/mutex.h>
  30. #define RADIO_VERSION KERNEL_VERSION(0,0,2)
  31. static struct v4l2_queryctrl radio_qctrl[] = {
  32. {
  33. .id = V4L2_CID_AUDIO_MUTE,
  34. .name = "Mute",
  35. .minimum = 0,
  36. .maximum = 1,
  37. .default_value = 1,
  38. .type = V4L2_CTRL_TYPE_BOOLEAN,
  39. }
  40. };
  41. struct fmi_device
  42. {
  43. int port;
  44. int curvol; /* 1 or 0 */
  45. unsigned long curfreq; /* freq in kHz */
  46. __u32 flags;
  47. };
  48. static int io = -1;
  49. static int radio_nr = -1;
  50. static struct pnp_dev *dev = NULL;
  51. static struct mutex lock;
  52. /* freq is in 1/16 kHz to internal number, hw precision is 50 kHz */
  53. /* It is only useful to give freq in intervall of 800 (=0.05Mhz),
  54. * other bits will be truncated, e.g 92.7400016 -> 92.7, but
  55. * 92.7400017 -> 92.75
  56. */
  57. #define RSF16_ENCODE(x) ((x)/800+214)
  58. #define RSF16_MINFREQ 87*16000
  59. #define RSF16_MAXFREQ 108*16000
  60. static void outbits(int bits, unsigned int data, int port)
  61. {
  62. while(bits--) {
  63. if(data & 1) {
  64. outb(5, port);
  65. udelay(6);
  66. outb(7, port);
  67. udelay(6);
  68. } else {
  69. outb(1, port);
  70. udelay(6);
  71. outb(3, port);
  72. udelay(6);
  73. }
  74. data>>=1;
  75. }
  76. }
  77. static inline void fmi_mute(int port)
  78. {
  79. mutex_lock(&lock);
  80. outb(0x00, port);
  81. mutex_unlock(&lock);
  82. }
  83. static inline void fmi_unmute(int port)
  84. {
  85. mutex_lock(&lock);
  86. outb(0x08, port);
  87. mutex_unlock(&lock);
  88. }
  89. static inline int fmi_setfreq(struct fmi_device *dev)
  90. {
  91. int myport = dev->port;
  92. unsigned long freq = dev->curfreq;
  93. mutex_lock(&lock);
  94. outbits(16, RSF16_ENCODE(freq), myport);
  95. outbits(8, 0xC0, myport);
  96. msleep(143); /* was schedule_timeout(HZ/7) */
  97. mutex_unlock(&lock);
  98. if (dev->curvol) fmi_unmute(myport);
  99. return 0;
  100. }
  101. static inline int fmi_getsigstr(struct fmi_device *dev)
  102. {
  103. int val;
  104. int res;
  105. int myport = dev->port;
  106. mutex_lock(&lock);
  107. val = dev->curvol ? 0x08 : 0x00; /* unmute/mute */
  108. outb(val, myport);
  109. outb(val | 0x10, myport);
  110. msleep(143); /* was schedule_timeout(HZ/7) */
  111. res = (int)inb(myport+1);
  112. outb(val, myport);
  113. mutex_unlock(&lock);
  114. return (res & 2) ? 0 : 0xFFFF;
  115. }
  116. static int fmi_do_ioctl(struct inode *inode, struct file *file,
  117. unsigned int cmd, void *arg)
  118. {
  119. struct video_device *dev = video_devdata(file);
  120. struct fmi_device *fmi=dev->priv;
  121. switch(cmd)
  122. {
  123. case VIDIOC_QUERYCAP:
  124. {
  125. struct v4l2_capability *v = arg;
  126. memset(v,0,sizeof(*v));
  127. strlcpy(v->driver, "radio-sf16fmi", sizeof (v->driver));
  128. strlcpy(v->card, "SF16-FMx radio", sizeof (v->card));
  129. sprintf(v->bus_info,"ISA");
  130. v->version = RADIO_VERSION;
  131. v->capabilities = V4L2_CAP_TUNER;
  132. return 0;
  133. }
  134. case VIDIOC_G_TUNER:
  135. {
  136. struct v4l2_tuner *v = arg;
  137. int mult;
  138. if (v->index > 0)
  139. return -EINVAL;
  140. memset(v,0,sizeof(*v));
  141. strcpy(v->name, "FM");
  142. v->type = V4L2_TUNER_RADIO;
  143. mult = (fmi->flags & V4L2_TUNER_CAP_LOW) ? 1 : 1000;
  144. v->rangelow = RSF16_MINFREQ/mult;
  145. v->rangehigh = RSF16_MAXFREQ/mult;
  146. v->rxsubchans =V4L2_TUNER_SUB_MONO | V4L2_TUNER_MODE_STEREO;
  147. v->capability=fmi->flags&V4L2_TUNER_CAP_LOW;
  148. v->audmode = V4L2_TUNER_MODE_STEREO;
  149. v->signal = fmi_getsigstr(fmi);
  150. return 0;
  151. }
  152. case VIDIOC_S_TUNER:
  153. {
  154. struct v4l2_tuner *v = arg;
  155. if (v->index > 0)
  156. return -EINVAL;
  157. return 0;
  158. }
  159. case VIDIOC_S_FREQUENCY:
  160. {
  161. struct v4l2_frequency *f = arg;
  162. if (!(fmi->flags & V4L2_TUNER_CAP_LOW))
  163. f->frequency *= 1000;
  164. if (f->frequency < RSF16_MINFREQ ||
  165. f->frequency > RSF16_MAXFREQ )
  166. return -EINVAL;
  167. /*rounding in steps of 800 to match th freq
  168. that will be used */
  169. fmi->curfreq = (f->frequency/800)*800;
  170. fmi_setfreq(fmi);
  171. return 0;
  172. }
  173. case VIDIOC_G_FREQUENCY:
  174. {
  175. struct v4l2_frequency *f = arg;
  176. f->type = V4L2_TUNER_RADIO;
  177. f->frequency = fmi->curfreq;
  178. if (!(fmi->flags & V4L2_TUNER_CAP_LOW))
  179. f->frequency /= 1000;
  180. return 0;
  181. }
  182. case VIDIOC_QUERYCTRL:
  183. {
  184. struct v4l2_queryctrl *qc = arg;
  185. int i;
  186. for (i = 0; i < ARRAY_SIZE(radio_qctrl); i++) {
  187. if (qc->id && qc->id == radio_qctrl[i].id) {
  188. memcpy(qc, &(radio_qctrl[i]),
  189. sizeof(*qc));
  190. return (0);
  191. }
  192. }
  193. return -EINVAL;
  194. }
  195. case VIDIOC_G_CTRL:
  196. {
  197. struct v4l2_control *ctrl= arg;
  198. switch (ctrl->id) {
  199. case V4L2_CID_AUDIO_MUTE:
  200. ctrl->value=fmi->curvol;
  201. return (0);
  202. }
  203. return -EINVAL;
  204. }
  205. case VIDIOC_S_CTRL:
  206. {
  207. struct v4l2_control *ctrl= arg;
  208. switch (ctrl->id) {
  209. case V4L2_CID_AUDIO_MUTE:
  210. {
  211. if (ctrl->value)
  212. fmi_mute(fmi->port);
  213. else
  214. fmi_unmute(fmi->port);
  215. fmi->curvol=ctrl->value;
  216. return (0);
  217. }
  218. }
  219. return -EINVAL;
  220. }
  221. default:
  222. return v4l_compat_translate_ioctl(inode,file,cmd,arg,
  223. fmi_do_ioctl);
  224. }
  225. }
  226. static int fmi_ioctl(struct inode *inode, struct file *file,
  227. unsigned int cmd, unsigned long arg)
  228. {
  229. return video_usercopy(inode, file, cmd, arg, fmi_do_ioctl);
  230. }
  231. static struct fmi_device fmi_unit;
  232. static struct file_operations fmi_fops = {
  233. .owner = THIS_MODULE,
  234. .open = video_exclusive_open,
  235. .release = video_exclusive_release,
  236. .ioctl = fmi_ioctl,
  237. .compat_ioctl = v4l_compat_ioctl32,
  238. .llseek = no_llseek,
  239. };
  240. static struct video_device fmi_radio=
  241. {
  242. .owner = THIS_MODULE,
  243. .name = "SF16FMx radio",
  244. .type = VID_TYPE_TUNER,
  245. .hardware = 0,
  246. .fops = &fmi_fops,
  247. };
  248. /* ladis: this is my card. does any other types exist? */
  249. static struct isapnp_device_id id_table[] __devinitdata = {
  250. { ISAPNP_ANY_ID, ISAPNP_ANY_ID,
  251. ISAPNP_VENDOR('M','F','R'), ISAPNP_FUNCTION(0xad10), 0},
  252. { ISAPNP_CARD_END, },
  253. };
  254. MODULE_DEVICE_TABLE(isapnp, id_table);
  255. static int isapnp_fmi_probe(void)
  256. {
  257. int i = 0;
  258. while (id_table[i].card_vendor != 0 && dev == NULL) {
  259. dev = pnp_find_dev(NULL, id_table[i].vendor,
  260. id_table[i].function, NULL);
  261. i++;
  262. }
  263. if (!dev)
  264. return -ENODEV;
  265. if (pnp_device_attach(dev) < 0)
  266. return -EAGAIN;
  267. if (pnp_activate_dev(dev) < 0) {
  268. printk ("radio-sf16fmi: PnP configure failed (out of resources?)\n");
  269. pnp_device_detach(dev);
  270. return -ENOMEM;
  271. }
  272. if (!pnp_port_valid(dev, 0)) {
  273. pnp_device_detach(dev);
  274. return -ENODEV;
  275. }
  276. i = pnp_port_start(dev, 0);
  277. printk ("radio-sf16fmi: PnP reports card at %#x\n", i);
  278. return i;
  279. }
  280. static int __init fmi_init(void)
  281. {
  282. if (io < 0)
  283. io = isapnp_fmi_probe();
  284. if (io < 0) {
  285. printk(KERN_ERR "radio-sf16fmi: No PnP card found.\n");
  286. return io;
  287. }
  288. if (!request_region(io, 2, "radio-sf16fmi")) {
  289. printk(KERN_ERR "radio-sf16fmi: port 0x%x already in use\n", io);
  290. return -EBUSY;
  291. }
  292. fmi_unit.port = io;
  293. fmi_unit.curvol = 0;
  294. fmi_unit.curfreq = 0;
  295. fmi_unit.flags = V4L2_TUNER_CAP_LOW;
  296. fmi_radio.priv = &fmi_unit;
  297. mutex_init(&lock);
  298. if (video_register_device(&fmi_radio, VFL_TYPE_RADIO, radio_nr) == -1) {
  299. release_region(io, 2);
  300. return -EINVAL;
  301. }
  302. printk(KERN_INFO "SF16FMx radio card driver at 0x%x\n", io);
  303. /* mute card - prevents noisy bootups */
  304. fmi_mute(io);
  305. return 0;
  306. }
  307. MODULE_AUTHOR("Petr Vandrovec, vandrove@vc.cvut.cz and M. Kirkwood");
  308. MODULE_DESCRIPTION("A driver for the SF16MI radio.");
  309. MODULE_LICENSE("GPL");
  310. module_param(io, int, 0);
  311. MODULE_PARM_DESC(io, "I/O address of the SF16MI card (0x284 or 0x384)");
  312. module_param(radio_nr, int, 0);
  313. static void __exit fmi_cleanup_module(void)
  314. {
  315. video_unregister_device(&fmi_radio);
  316. release_region(io, 2);
  317. if (dev)
  318. pnp_device_detach(dev);
  319. }
  320. module_init(fmi_init);
  321. module_exit(fmi_cleanup_module);