radio-sf16fmi.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  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 vidioc_querycap(struct file *file, void *priv,
  117. struct v4l2_capability *v)
  118. {
  119. strlcpy(v->driver, "radio-sf16fmi", sizeof(v->driver));
  120. strlcpy(v->card, "SF16-FMx radio", sizeof(v->card));
  121. sprintf(v->bus_info, "ISA");
  122. v->version = RADIO_VERSION;
  123. v->capabilities = V4L2_CAP_TUNER;
  124. return 0;
  125. }
  126. static int vidioc_g_tuner(struct file *file, void *priv,
  127. struct v4l2_tuner *v)
  128. {
  129. int mult;
  130. struct video_device *dev = video_devdata(file);
  131. struct fmi_device *fmi = dev->priv;
  132. if (v->index > 0)
  133. return -EINVAL;
  134. strcpy(v->name, "FM");
  135. v->type = V4L2_TUNER_RADIO;
  136. mult = (fmi->flags & V4L2_TUNER_CAP_LOW) ? 1 : 1000;
  137. v->rangelow = RSF16_MINFREQ/mult;
  138. v->rangehigh = RSF16_MAXFREQ/mult;
  139. v->rxsubchans = V4L2_TUNER_SUB_MONO | V4L2_TUNER_MODE_STEREO;
  140. v->capability = fmi->flags&V4L2_TUNER_CAP_LOW;
  141. v->audmode = V4L2_TUNER_MODE_STEREO;
  142. v->signal = fmi_getsigstr(fmi);
  143. return 0;
  144. }
  145. static int vidioc_s_tuner(struct file *file, void *priv,
  146. struct v4l2_tuner *v)
  147. {
  148. if (v->index > 0)
  149. return -EINVAL;
  150. return 0;
  151. }
  152. static int vidioc_s_frequency(struct file *file, void *priv,
  153. struct v4l2_frequency *f)
  154. {
  155. struct video_device *dev = video_devdata(file);
  156. struct fmi_device *fmi = dev->priv;
  157. if (!(fmi->flags & V4L2_TUNER_CAP_LOW))
  158. f->frequency *= 1000;
  159. if (f->frequency < RSF16_MINFREQ ||
  160. f->frequency > RSF16_MAXFREQ )
  161. return -EINVAL;
  162. /*rounding in steps of 800 to match th freq
  163. that will be used */
  164. fmi->curfreq = (f->frequency/800)*800;
  165. fmi_setfreq(fmi);
  166. return 0;
  167. }
  168. static int vidioc_g_frequency(struct file *file, void *priv,
  169. struct v4l2_frequency *f)
  170. {
  171. struct video_device *dev = video_devdata(file);
  172. struct fmi_device *fmi = dev->priv;
  173. f->type = V4L2_TUNER_RADIO;
  174. f->frequency = fmi->curfreq;
  175. if (!(fmi->flags & V4L2_TUNER_CAP_LOW))
  176. f->frequency /= 1000;
  177. return 0;
  178. }
  179. static int vidioc_queryctrl(struct file *file, void *priv,
  180. struct v4l2_queryctrl *qc)
  181. {
  182. int i;
  183. for (i = 0; i < ARRAY_SIZE(radio_qctrl); i++) {
  184. if (qc->id && qc->id == radio_qctrl[i].id) {
  185. memcpy(qc, &(radio_qctrl[i]),
  186. sizeof(*qc));
  187. return 0;
  188. }
  189. }
  190. return -EINVAL;
  191. }
  192. static int vidioc_g_ctrl(struct file *file, void *priv,
  193. struct v4l2_control *ctrl)
  194. {
  195. struct video_device *dev = video_devdata(file);
  196. struct fmi_device *fmi = dev->priv;
  197. switch (ctrl->id) {
  198. case V4L2_CID_AUDIO_MUTE:
  199. ctrl->value = fmi->curvol;
  200. return 0;
  201. }
  202. return -EINVAL;
  203. }
  204. static int vidioc_s_ctrl(struct file *file, void *priv,
  205. struct v4l2_control *ctrl)
  206. {
  207. struct video_device *dev = video_devdata(file);
  208. struct fmi_device *fmi = dev->priv;
  209. switch (ctrl->id) {
  210. case V4L2_CID_AUDIO_MUTE:
  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. return -EINVAL;
  219. }
  220. static int vidioc_g_audio(struct file *file, void *priv,
  221. struct v4l2_audio *a)
  222. {
  223. if (a->index > 1)
  224. return -EINVAL;
  225. strcpy(a->name, "Radio");
  226. a->capability = V4L2_AUDCAP_STEREO;
  227. return 0;
  228. }
  229. static int vidioc_g_input(struct file *filp, void *priv, unsigned int *i)
  230. {
  231. *i = 0;
  232. return 0;
  233. }
  234. static int vidioc_s_input(struct file *filp, void *priv, unsigned int i)
  235. {
  236. if (i != 0)
  237. return -EINVAL;
  238. return 0;
  239. }
  240. static int vidioc_s_audio(struct file *file, void *priv,
  241. struct v4l2_audio *a)
  242. {
  243. if (a->index != 0)
  244. return -EINVAL;
  245. return 0;
  246. }
  247. static struct fmi_device fmi_unit;
  248. static const struct file_operations fmi_fops = {
  249. .owner = THIS_MODULE,
  250. .open = video_exclusive_open,
  251. .release = video_exclusive_release,
  252. .ioctl = video_ioctl2,
  253. .compat_ioctl = v4l_compat_ioctl32,
  254. .llseek = no_llseek,
  255. };
  256. static struct video_device fmi_radio=
  257. {
  258. .owner = THIS_MODULE,
  259. .name = "SF16FMx radio",
  260. .type = VID_TYPE_TUNER,
  261. .fops = &fmi_fops,
  262. .vidioc_querycap = vidioc_querycap,
  263. .vidioc_g_tuner = vidioc_g_tuner,
  264. .vidioc_s_tuner = vidioc_s_tuner,
  265. .vidioc_g_audio = vidioc_g_audio,
  266. .vidioc_s_audio = vidioc_s_audio,
  267. .vidioc_g_input = vidioc_g_input,
  268. .vidioc_s_input = vidioc_s_input,
  269. .vidioc_g_frequency = vidioc_g_frequency,
  270. .vidioc_s_frequency = vidioc_s_frequency,
  271. .vidioc_queryctrl = vidioc_queryctrl,
  272. .vidioc_g_ctrl = vidioc_g_ctrl,
  273. .vidioc_s_ctrl = vidioc_s_ctrl,
  274. };
  275. /* ladis: this is my card. does any other types exist? */
  276. static struct isapnp_device_id id_table[] __devinitdata = {
  277. { ISAPNP_ANY_ID, ISAPNP_ANY_ID,
  278. ISAPNP_VENDOR('M','F','R'), ISAPNP_FUNCTION(0xad10), 0},
  279. { ISAPNP_CARD_END, },
  280. };
  281. MODULE_DEVICE_TABLE(isapnp, id_table);
  282. static int isapnp_fmi_probe(void)
  283. {
  284. int i = 0;
  285. while (id_table[i].card_vendor != 0 && dev == NULL) {
  286. dev = pnp_find_dev(NULL, id_table[i].vendor,
  287. id_table[i].function, NULL);
  288. i++;
  289. }
  290. if (!dev)
  291. return -ENODEV;
  292. if (pnp_device_attach(dev) < 0)
  293. return -EAGAIN;
  294. if (pnp_activate_dev(dev) < 0) {
  295. printk ("radio-sf16fmi: PnP configure failed (out of resources?)\n");
  296. pnp_device_detach(dev);
  297. return -ENOMEM;
  298. }
  299. if (!pnp_port_valid(dev, 0)) {
  300. pnp_device_detach(dev);
  301. return -ENODEV;
  302. }
  303. i = pnp_port_start(dev, 0);
  304. printk ("radio-sf16fmi: PnP reports card at %#x\n", i);
  305. return i;
  306. }
  307. static int __init fmi_init(void)
  308. {
  309. if (io < 0)
  310. io = isapnp_fmi_probe();
  311. if (io < 0) {
  312. printk(KERN_ERR "radio-sf16fmi: No PnP card found.\n");
  313. return io;
  314. }
  315. if (!request_region(io, 2, "radio-sf16fmi")) {
  316. printk(KERN_ERR "radio-sf16fmi: port 0x%x already in use\n", io);
  317. return -EBUSY;
  318. }
  319. fmi_unit.port = io;
  320. fmi_unit.curvol = 0;
  321. fmi_unit.curfreq = 0;
  322. fmi_unit.flags = V4L2_TUNER_CAP_LOW;
  323. fmi_radio.priv = &fmi_unit;
  324. mutex_init(&lock);
  325. if (video_register_device(&fmi_radio, VFL_TYPE_RADIO, radio_nr) == -1) {
  326. release_region(io, 2);
  327. return -EINVAL;
  328. }
  329. printk(KERN_INFO "SF16FMx radio card driver at 0x%x\n", io);
  330. /* mute card - prevents noisy bootups */
  331. fmi_mute(io);
  332. return 0;
  333. }
  334. MODULE_AUTHOR("Petr Vandrovec, vandrove@vc.cvut.cz and M. Kirkwood");
  335. MODULE_DESCRIPTION("A driver for the SF16MI radio.");
  336. MODULE_LICENSE("GPL");
  337. module_param(io, int, 0);
  338. MODULE_PARM_DESC(io, "I/O address of the SF16MI card (0x284 or 0x384)");
  339. module_param(radio_nr, int, 0);
  340. static void __exit fmi_cleanup_module(void)
  341. {
  342. video_unregister_device(&fmi_radio);
  343. release_region(io, 2);
  344. if (dev)
  345. pnp_device_detach(dev);
  346. }
  347. module_init(fmi_init);
  348. module_exit(fmi_cleanup_module);