radio-sf16fmi.c 8.2 KB

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