radio-sf16fmi.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  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. u32 curfreq; /* freq in kHz */
  49. struct mutex lock;
  50. };
  51. static struct fmi fmi_card;
  52. static struct pnp_dev *dev;
  53. static 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 void fmi_set_freq(struct fmi *fmi)
  104. {
  105. fmi->curfreq = clamp(fmi->curfreq, RSF16_MINFREQ, RSF16_MAXFREQ);
  106. /* rounding in steps of 800 to match the freq
  107. that will be used */
  108. lm7000_set_freq((fmi->curfreq / 800) * 800, fmi, fmi_set_pins);
  109. }
  110. static int vidioc_querycap(struct file *file, void *priv,
  111. struct v4l2_capability *v)
  112. {
  113. strlcpy(v->driver, "radio-sf16fmi", sizeof(v->driver));
  114. strlcpy(v->card, "SF16-FMI/FMP/FMD radio", sizeof(v->card));
  115. strlcpy(v->bus_info, "ISA:radio-sf16fmi", sizeof(v->bus_info));
  116. v->device_caps = V4L2_CAP_TUNER | V4L2_CAP_RADIO;
  117. v->capabilities = v->device_caps | V4L2_CAP_DEVICE_CAPS;
  118. return 0;
  119. }
  120. static int vidioc_g_tuner(struct file *file, void *priv,
  121. struct v4l2_tuner *v)
  122. {
  123. struct fmi *fmi = video_drvdata(file);
  124. if (v->index > 0)
  125. return -EINVAL;
  126. strlcpy(v->name, "FM", sizeof(v->name));
  127. v->type = V4L2_TUNER_RADIO;
  128. v->rangelow = RSF16_MINFREQ;
  129. v->rangehigh = RSF16_MAXFREQ;
  130. v->rxsubchans = V4L2_TUNER_SUB_MONO | V4L2_TUNER_SUB_STEREO;
  131. v->capability = V4L2_TUNER_CAP_STEREO | V4L2_TUNER_CAP_LOW;
  132. v->audmode = V4L2_TUNER_MODE_STEREO;
  133. v->signal = fmi_getsigstr(fmi);
  134. return 0;
  135. }
  136. static int vidioc_s_tuner(struct file *file, void *priv,
  137. const struct v4l2_tuner *v)
  138. {
  139. return v->index ? -EINVAL : 0;
  140. }
  141. static int vidioc_s_frequency(struct file *file, void *priv,
  142. const struct v4l2_frequency *f)
  143. {
  144. struct fmi *fmi = video_drvdata(file);
  145. if (f->tuner != 0 || f->type != V4L2_TUNER_RADIO)
  146. return -EINVAL;
  147. fmi->curfreq = f->frequency;
  148. fmi_set_freq(fmi);
  149. return 0;
  150. }
  151. static int vidioc_g_frequency(struct file *file, void *priv,
  152. struct v4l2_frequency *f)
  153. {
  154. struct fmi *fmi = video_drvdata(file);
  155. if (f->tuner != 0)
  156. return -EINVAL;
  157. f->type = V4L2_TUNER_RADIO;
  158. f->frequency = fmi->curfreq;
  159. return 0;
  160. }
  161. static int fmi_s_ctrl(struct v4l2_ctrl *ctrl)
  162. {
  163. struct fmi *fmi = container_of(ctrl->handler, struct fmi, hdl);
  164. switch (ctrl->id) {
  165. case V4L2_CID_AUDIO_MUTE:
  166. if (ctrl->val)
  167. fmi_mute(fmi);
  168. else
  169. fmi_unmute(fmi);
  170. fmi->mute = ctrl->val;
  171. return 0;
  172. }
  173. return -EINVAL;
  174. }
  175. static const struct v4l2_ctrl_ops fmi_ctrl_ops = {
  176. .s_ctrl = fmi_s_ctrl,
  177. };
  178. static const struct v4l2_file_operations fmi_fops = {
  179. .owner = THIS_MODULE,
  180. .open = v4l2_fh_open,
  181. .release = v4l2_fh_release,
  182. .poll = v4l2_ctrl_poll,
  183. .unlocked_ioctl = video_ioctl2,
  184. };
  185. static const struct v4l2_ioctl_ops fmi_ioctl_ops = {
  186. .vidioc_querycap = vidioc_querycap,
  187. .vidioc_g_tuner = vidioc_g_tuner,
  188. .vidioc_s_tuner = vidioc_s_tuner,
  189. .vidioc_g_frequency = vidioc_g_frequency,
  190. .vidioc_s_frequency = vidioc_s_frequency,
  191. .vidioc_log_status = v4l2_ctrl_log_status,
  192. .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
  193. .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
  194. };
  195. /* ladis: this is my card. does any other types exist? */
  196. static struct isapnp_device_id id_table[] = {
  197. /* SF16-FMI */
  198. { ISAPNP_ANY_ID, ISAPNP_ANY_ID,
  199. ISAPNP_VENDOR('M','F','R'), ISAPNP_FUNCTION(0xad10), 0},
  200. /* SF16-FMD */
  201. { ISAPNP_ANY_ID, ISAPNP_ANY_ID,
  202. ISAPNP_VENDOR('M','F','R'), ISAPNP_FUNCTION(0xad12), 0},
  203. { ISAPNP_CARD_END, },
  204. };
  205. MODULE_DEVICE_TABLE(isapnp, id_table);
  206. static int __init isapnp_fmi_probe(void)
  207. {
  208. int i = 0;
  209. while (id_table[i].card_vendor != 0 && dev == NULL) {
  210. dev = pnp_find_dev(NULL, id_table[i].vendor,
  211. id_table[i].function, NULL);
  212. i++;
  213. }
  214. if (!dev)
  215. return -ENODEV;
  216. if (pnp_device_attach(dev) < 0)
  217. return -EAGAIN;
  218. if (pnp_activate_dev(dev) < 0) {
  219. printk(KERN_ERR "radio-sf16fmi: PnP configure failed (out of resources?)\n");
  220. pnp_device_detach(dev);
  221. return -ENOMEM;
  222. }
  223. if (!pnp_port_valid(dev, 0)) {
  224. pnp_device_detach(dev);
  225. return -ENODEV;
  226. }
  227. i = pnp_port_start(dev, 0);
  228. printk(KERN_INFO "radio-sf16fmi: PnP reports card at %#x\n", i);
  229. return i;
  230. }
  231. static int __init fmi_init(void)
  232. {
  233. struct fmi *fmi = &fmi_card;
  234. struct v4l2_device *v4l2_dev = &fmi->v4l2_dev;
  235. struct v4l2_ctrl_handler *hdl = &fmi->hdl;
  236. int res, i;
  237. int probe_ports[] = { 0, 0x284, 0x384 };
  238. if (io < 0) {
  239. for (i = 0; i < ARRAY_SIZE(probe_ports); i++) {
  240. io = probe_ports[i];
  241. if (io == 0) {
  242. io = isapnp_fmi_probe();
  243. if (io < 0)
  244. continue;
  245. pnp_attached = true;
  246. }
  247. if (!request_region(io, 2, "radio-sf16fmi")) {
  248. if (pnp_attached)
  249. pnp_device_detach(dev);
  250. io = -1;
  251. continue;
  252. }
  253. if (pnp_attached ||
  254. ((inb(io) & 0xf9) == 0xf9 && (inb(io) & 0x4) == 0))
  255. break;
  256. release_region(io, 2);
  257. io = -1;
  258. }
  259. } else {
  260. if (!request_region(io, 2, "radio-sf16fmi")) {
  261. printk(KERN_ERR "radio-sf16fmi: port %#x already in use\n", io);
  262. return -EBUSY;
  263. }
  264. if (inb(io) == 0xff) {
  265. printk(KERN_ERR "radio-sf16fmi: card not present at %#x\n", io);
  266. release_region(io, 2);
  267. return -ENODEV;
  268. }
  269. }
  270. if (io < 0) {
  271. printk(KERN_ERR "radio-sf16fmi: no cards found\n");
  272. return -ENODEV;
  273. }
  274. strlcpy(v4l2_dev->name, "sf16fmi", sizeof(v4l2_dev->name));
  275. fmi->io = io;
  276. res = v4l2_device_register(NULL, v4l2_dev);
  277. if (res < 0) {
  278. release_region(fmi->io, 2);
  279. if (pnp_attached)
  280. pnp_device_detach(dev);
  281. v4l2_err(v4l2_dev, "Could not register v4l2_device\n");
  282. return res;
  283. }
  284. v4l2_ctrl_handler_init(hdl, 1);
  285. v4l2_ctrl_new_std(hdl, &fmi_ctrl_ops,
  286. V4L2_CID_AUDIO_MUTE, 0, 1, 1, 1);
  287. v4l2_dev->ctrl_handler = hdl;
  288. if (hdl->error) {
  289. res = hdl->error;
  290. v4l2_err(v4l2_dev, "Could not register controls\n");
  291. v4l2_ctrl_handler_free(hdl);
  292. v4l2_device_unregister(v4l2_dev);
  293. return res;
  294. }
  295. strlcpy(fmi->vdev.name, v4l2_dev->name, sizeof(fmi->vdev.name));
  296. fmi->vdev.v4l2_dev = v4l2_dev;
  297. fmi->vdev.fops = &fmi_fops;
  298. fmi->vdev.ioctl_ops = &fmi_ioctl_ops;
  299. fmi->vdev.release = video_device_release_empty;
  300. video_set_drvdata(&fmi->vdev, fmi);
  301. mutex_init(&fmi->lock);
  302. /* mute card and set default frequency */
  303. fmi->mute = true;
  304. fmi->curfreq = RSF16_MINFREQ;
  305. fmi_set_freq(fmi);
  306. if (video_register_device(&fmi->vdev, VFL_TYPE_RADIO, radio_nr) < 0) {
  307. v4l2_ctrl_handler_free(hdl);
  308. v4l2_device_unregister(v4l2_dev);
  309. release_region(fmi->io, 2);
  310. if (pnp_attached)
  311. pnp_device_detach(dev);
  312. return -EINVAL;
  313. }
  314. v4l2_info(v4l2_dev, "card driver at 0x%x\n", fmi->io);
  315. return 0;
  316. }
  317. static void __exit fmi_exit(void)
  318. {
  319. struct fmi *fmi = &fmi_card;
  320. v4l2_ctrl_handler_free(&fmi->hdl);
  321. video_unregister_device(&fmi->vdev);
  322. v4l2_device_unregister(&fmi->v4l2_dev);
  323. release_region(fmi->io, 2);
  324. if (dev && pnp_attached)
  325. pnp_device_detach(dev);
  326. }
  327. module_init(fmi_init);
  328. module_exit(fmi_exit);