radio-sf16fmi.c 9.4 KB

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