radio-sf16fmi.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  1. /* SF16-FMI, SF16-FMP and SF16-FMD 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 SF16-FMI/SF16-FMP/SF16-FMD
  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/isapnp.h>
  24. #include <linux/mutex.h>
  25. #include <linux/videodev2.h> /* kernel radio structs */
  26. #include <linux/io.h> /* outb, outb_p */
  27. #include <media/v4l2-device.h>
  28. #include <media/v4l2-ioctl.h>
  29. #include <media/v4l2-ctrls.h>
  30. #include <media/v4l2-event.h>
  31. #include "lm7000.h"
  32. MODULE_AUTHOR("Petr Vandrovec, vandrove@vc.cvut.cz and M. Kirkwood");
  33. MODULE_DESCRIPTION("A driver for the SF16-FMI, SF16-FMP and SF16-FMD radio.");
  34. MODULE_LICENSE("GPL");
  35. MODULE_VERSION("0.0.3");
  36. static int io = -1;
  37. static int radio_nr = -1;
  38. module_param(io, int, 0);
  39. MODULE_PARM_DESC(io, "I/O address of the SF16-FMI/SF16-FMP/SF16-FMD card (0x284 or 0x384)");
  40. module_param(radio_nr, int, 0);
  41. struct fmi
  42. {
  43. struct v4l2_device v4l2_dev;
  44. struct v4l2_ctrl_handler hdl;
  45. struct video_device vdev;
  46. int io;
  47. bool mute;
  48. unsigned long curfreq; /* freq in kHz */
  49. struct mutex lock;
  50. };
  51. static struct fmi fmi_card;
  52. static struct pnp_dev *dev;
  53. bool pnp_attached;
  54. #define RSF16_MINFREQ (87U * 16000)
  55. #define RSF16_MAXFREQ (108U * 16000)
  56. #define FMI_BIT_TUN_CE (1 << 0)
  57. #define FMI_BIT_TUN_CLK (1 << 1)
  58. #define FMI_BIT_TUN_DATA (1 << 2)
  59. #define FMI_BIT_VOL_SW (1 << 3)
  60. #define FMI_BIT_TUN_STRQ (1 << 4)
  61. static void fmi_set_pins(void *handle, u8 pins)
  62. {
  63. struct fmi *fmi = handle;
  64. u8 bits = FMI_BIT_TUN_STRQ;
  65. if (!fmi->mute)
  66. bits |= FMI_BIT_VOL_SW;
  67. if (pins & LM7000_DATA)
  68. bits |= FMI_BIT_TUN_DATA;
  69. if (pins & LM7000_CLK)
  70. bits |= FMI_BIT_TUN_CLK;
  71. if (pins & LM7000_CE)
  72. bits |= FMI_BIT_TUN_CE;
  73. mutex_lock(&fmi->lock);
  74. outb_p(bits, fmi->io);
  75. mutex_unlock(&fmi->lock);
  76. }
  77. static inline void fmi_mute(struct fmi *fmi)
  78. {
  79. mutex_lock(&fmi->lock);
  80. outb(0x00, fmi->io);
  81. mutex_unlock(&fmi->lock);
  82. }
  83. static inline void fmi_unmute(struct fmi *fmi)
  84. {
  85. mutex_lock(&fmi->lock);
  86. outb(0x08, fmi->io);
  87. mutex_unlock(&fmi->lock);
  88. }
  89. static inline int fmi_getsigstr(struct fmi *fmi)
  90. {
  91. int val;
  92. int res;
  93. mutex_lock(&fmi->lock);
  94. val = fmi->mute ? 0x00 : 0x08; /* mute/unmute */
  95. outb(val, fmi->io);
  96. outb(val | 0x10, fmi->io);
  97. msleep(143); /* was schedule_timeout(HZ/7) */
  98. res = (int)inb(fmi->io + 1);
  99. outb(val, fmi->io);
  100. mutex_unlock(&fmi->lock);
  101. return (res & 2) ? 0 : 0xFFFF;
  102. }
  103. static int vidioc_querycap(struct file *file, void *priv,
  104. struct v4l2_capability *v)
  105. {
  106. strlcpy(v->driver, "radio-sf16fmi", sizeof(v->driver));
  107. strlcpy(v->card, "SF16-FMI/FMP/FMD radio", sizeof(v->card));
  108. strlcpy(v->bus_info, "ISA:radio-sf16fmi", sizeof(v->bus_info));
  109. v->device_caps = V4L2_CAP_TUNER | V4L2_CAP_RADIO;
  110. v->capabilities = v->device_caps | V4L2_CAP_DEVICE_CAPS;
  111. return 0;
  112. }
  113. static int vidioc_g_tuner(struct file *file, void *priv,
  114. struct v4l2_tuner *v)
  115. {
  116. struct fmi *fmi = video_drvdata(file);
  117. if (v->index > 0)
  118. return -EINVAL;
  119. strlcpy(v->name, "FM", sizeof(v->name));
  120. v->type = V4L2_TUNER_RADIO;
  121. v->rangelow = RSF16_MINFREQ;
  122. v->rangehigh = RSF16_MAXFREQ;
  123. v->rxsubchans = V4L2_TUNER_SUB_MONO | V4L2_TUNER_SUB_STEREO;
  124. v->capability = V4L2_TUNER_CAP_STEREO | V4L2_TUNER_CAP_LOW;
  125. v->audmode = V4L2_TUNER_MODE_STEREO;
  126. v->signal = fmi_getsigstr(fmi);
  127. return 0;
  128. }
  129. static int vidioc_s_tuner(struct file *file, void *priv,
  130. const struct v4l2_tuner *v)
  131. {
  132. return v->index ? -EINVAL : 0;
  133. }
  134. static int vidioc_s_frequency(struct file *file, void *priv,
  135. const struct v4l2_frequency *f)
  136. {
  137. struct fmi *fmi = video_drvdata(file);
  138. unsigned freq = f->frequency;
  139. if (f->tuner != 0 || f->type != V4L2_TUNER_RADIO)
  140. return -EINVAL;
  141. clamp(freq, RSF16_MINFREQ, RSF16_MAXFREQ);
  142. /* rounding in steps of 800 to match the freq
  143. that will be used */
  144. lm7000_set_freq((freq / 800) * 800, fmi, fmi_set_pins);
  145. return 0;
  146. }
  147. static int vidioc_g_frequency(struct file *file, void *priv,
  148. struct v4l2_frequency *f)
  149. {
  150. struct fmi *fmi = video_drvdata(file);
  151. if (f->tuner != 0)
  152. return -EINVAL;
  153. f->type = V4L2_TUNER_RADIO;
  154. f->frequency = fmi->curfreq;
  155. return 0;
  156. }
  157. static int fmi_s_ctrl(struct v4l2_ctrl *ctrl)
  158. {
  159. struct fmi *fmi = container_of(ctrl->handler, struct fmi, hdl);
  160. switch (ctrl->id) {
  161. case V4L2_CID_AUDIO_MUTE:
  162. if (ctrl->val)
  163. fmi_mute(fmi);
  164. else
  165. fmi_unmute(fmi);
  166. fmi->mute = ctrl->val;
  167. return 0;
  168. }
  169. return -EINVAL;
  170. }
  171. static const struct v4l2_ctrl_ops fmi_ctrl_ops = {
  172. .s_ctrl = fmi_s_ctrl,
  173. };
  174. static const struct v4l2_file_operations fmi_fops = {
  175. .owner = THIS_MODULE,
  176. .open = v4l2_fh_open,
  177. .release = v4l2_fh_release,
  178. .poll = v4l2_ctrl_poll,
  179. .unlocked_ioctl = video_ioctl2,
  180. };
  181. static const struct v4l2_ioctl_ops fmi_ioctl_ops = {
  182. .vidioc_querycap = vidioc_querycap,
  183. .vidioc_g_tuner = vidioc_g_tuner,
  184. .vidioc_s_tuner = vidioc_s_tuner,
  185. .vidioc_g_frequency = vidioc_g_frequency,
  186. .vidioc_s_frequency = vidioc_s_frequency,
  187. .vidioc_log_status = v4l2_ctrl_log_status,
  188. .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
  189. .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
  190. };
  191. /* ladis: this is my card. does any other types exist? */
  192. static struct isapnp_device_id id_table[] = {
  193. /* SF16-FMI */
  194. { ISAPNP_ANY_ID, ISAPNP_ANY_ID,
  195. ISAPNP_VENDOR('M','F','R'), ISAPNP_FUNCTION(0xad10), 0},
  196. /* SF16-FMD */
  197. { ISAPNP_ANY_ID, ISAPNP_ANY_ID,
  198. ISAPNP_VENDOR('M','F','R'), ISAPNP_FUNCTION(0xad12), 0},
  199. { ISAPNP_CARD_END, },
  200. };
  201. MODULE_DEVICE_TABLE(isapnp, id_table);
  202. static int __init isapnp_fmi_probe(void)
  203. {
  204. int i = 0;
  205. while (id_table[i].card_vendor != 0 && dev == NULL) {
  206. dev = pnp_find_dev(NULL, id_table[i].vendor,
  207. id_table[i].function, NULL);
  208. i++;
  209. }
  210. if (!dev)
  211. return -ENODEV;
  212. if (pnp_device_attach(dev) < 0)
  213. return -EAGAIN;
  214. if (pnp_activate_dev(dev) < 0) {
  215. printk(KERN_ERR "radio-sf16fmi: PnP configure failed (out of resources?)\n");
  216. pnp_device_detach(dev);
  217. return -ENOMEM;
  218. }
  219. if (!pnp_port_valid(dev, 0)) {
  220. pnp_device_detach(dev);
  221. return -ENODEV;
  222. }
  223. i = pnp_port_start(dev, 0);
  224. printk(KERN_INFO "radio-sf16fmi: PnP reports card at %#x\n", i);
  225. return i;
  226. }
  227. static int __init fmi_init(void)
  228. {
  229. struct fmi *fmi = &fmi_card;
  230. struct v4l2_device *v4l2_dev = &fmi->v4l2_dev;
  231. struct v4l2_ctrl_handler *hdl = &fmi->hdl;
  232. int res, i;
  233. int probe_ports[] = { 0, 0x284, 0x384 };
  234. if (io < 0) {
  235. for (i = 0; i < ARRAY_SIZE(probe_ports); i++) {
  236. io = probe_ports[i];
  237. if (io == 0) {
  238. io = isapnp_fmi_probe();
  239. if (io < 0)
  240. continue;
  241. pnp_attached = 1;
  242. }
  243. if (!request_region(io, 2, "radio-sf16fmi")) {
  244. if (pnp_attached)
  245. pnp_device_detach(dev);
  246. io = -1;
  247. continue;
  248. }
  249. if (pnp_attached ||
  250. ((inb(io) & 0xf9) == 0xf9 && (inb(io) & 0x4) == 0))
  251. break;
  252. release_region(io, 2);
  253. io = -1;
  254. }
  255. } else {
  256. if (!request_region(io, 2, "radio-sf16fmi")) {
  257. printk(KERN_ERR "radio-sf16fmi: port %#x already in use\n", io);
  258. return -EBUSY;
  259. }
  260. if (inb(io) == 0xff) {
  261. printk(KERN_ERR "radio-sf16fmi: card not present at %#x\n", io);
  262. release_region(io, 2);
  263. return -ENODEV;
  264. }
  265. }
  266. if (io < 0) {
  267. printk(KERN_ERR "radio-sf16fmi: no cards found\n");
  268. return -ENODEV;
  269. }
  270. strlcpy(v4l2_dev->name, "sf16fmi", sizeof(v4l2_dev->name));
  271. fmi->io = io;
  272. res = v4l2_device_register(NULL, v4l2_dev);
  273. if (res < 0) {
  274. release_region(fmi->io, 2);
  275. if (pnp_attached)
  276. pnp_device_detach(dev);
  277. v4l2_err(v4l2_dev, "Could not register v4l2_device\n");
  278. return res;
  279. }
  280. v4l2_ctrl_handler_init(hdl, 1);
  281. v4l2_ctrl_new_std(hdl, &fmi_ctrl_ops,
  282. V4L2_CID_AUDIO_MUTE, 0, 1, 1, 1);
  283. v4l2_dev->ctrl_handler = hdl;
  284. if (hdl->error) {
  285. res = hdl->error;
  286. v4l2_err(v4l2_dev, "Could not register controls\n");
  287. v4l2_ctrl_handler_free(hdl);
  288. v4l2_device_unregister(v4l2_dev);
  289. return res;
  290. }
  291. strlcpy(fmi->vdev.name, v4l2_dev->name, sizeof(fmi->vdev.name));
  292. fmi->vdev.v4l2_dev = v4l2_dev;
  293. fmi->vdev.fops = &fmi_fops;
  294. fmi->vdev.ioctl_ops = &fmi_ioctl_ops;
  295. fmi->vdev.release = video_device_release_empty;
  296. set_bit(V4L2_FL_USE_FH_PRIO, &fmi->vdev.flags);
  297. video_set_drvdata(&fmi->vdev, fmi);
  298. mutex_init(&fmi->lock);
  299. /* mute card - prevents noisy bootups */
  300. fmi_mute(fmi);
  301. if (video_register_device(&fmi->vdev, VFL_TYPE_RADIO, radio_nr) < 0) {
  302. v4l2_ctrl_handler_free(hdl);
  303. v4l2_device_unregister(v4l2_dev);
  304. release_region(fmi->io, 2);
  305. if (pnp_attached)
  306. pnp_device_detach(dev);
  307. return -EINVAL;
  308. }
  309. v4l2_info(v4l2_dev, "card driver at 0x%x\n", fmi->io);
  310. return 0;
  311. }
  312. static void __exit fmi_exit(void)
  313. {
  314. struct fmi *fmi = &fmi_card;
  315. v4l2_ctrl_handler_free(&fmi->hdl);
  316. video_unregister_device(&fmi->vdev);
  317. v4l2_device_unregister(&fmi->v4l2_dev);
  318. release_region(fmi->io, 2);
  319. if (dev && pnp_attached)
  320. pnp_device_detach(dev);
  321. }
  322. module_init(fmi_init);
  323. module_exit(fmi_exit);