radio-isa.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  1. /*
  2. * Framework for ISA radio drivers.
  3. * This takes care of all the V4L2 scaffolding, allowing the ISA drivers
  4. * to concentrate on the actual hardware operation.
  5. *
  6. * Copyright (C) 2012 Hans Verkuil <hans.verkuil@cisco.com>
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License
  10. * version 2 as published by the Free Software Foundation.
  11. *
  12. * This program is distributed in the hope that it will be useful, but
  13. * WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * General Public License for more details.
  16. */
  17. #include <linux/module.h>
  18. #include <linux/init.h>
  19. #include <linux/ioport.h>
  20. #include <linux/delay.h>
  21. #include <linux/videodev2.h>
  22. #include <linux/io.h>
  23. #include <linux/slab.h>
  24. #include <media/v4l2-device.h>
  25. #include <media/v4l2-ioctl.h>
  26. #include <media/v4l2-fh.h>
  27. #include <media/v4l2-ctrls.h>
  28. #include <media/v4l2-event.h>
  29. #include "radio-isa.h"
  30. MODULE_AUTHOR("Hans Verkuil");
  31. MODULE_DESCRIPTION("A framework for ISA radio drivers.");
  32. MODULE_LICENSE("GPL");
  33. #define FREQ_LOW (87U * 16000U)
  34. #define FREQ_HIGH (108U * 16000U)
  35. static int radio_isa_querycap(struct file *file, void *priv,
  36. struct v4l2_capability *v)
  37. {
  38. struct radio_isa_card *isa = video_drvdata(file);
  39. strlcpy(v->driver, isa->drv->driver.driver.name, sizeof(v->driver));
  40. strlcpy(v->card, isa->drv->card, sizeof(v->card));
  41. snprintf(v->bus_info, sizeof(v->bus_info), "ISA:%s", isa->v4l2_dev.name);
  42. v->device_caps = V4L2_CAP_TUNER | V4L2_CAP_RADIO;
  43. v->capabilities = v->device_caps | V4L2_CAP_DEVICE_CAPS;
  44. return 0;
  45. }
  46. static int radio_isa_g_tuner(struct file *file, void *priv,
  47. struct v4l2_tuner *v)
  48. {
  49. struct radio_isa_card *isa = video_drvdata(file);
  50. const struct radio_isa_ops *ops = isa->drv->ops;
  51. if (v->index > 0)
  52. return -EINVAL;
  53. strlcpy(v->name, "FM", sizeof(v->name));
  54. v->type = V4L2_TUNER_RADIO;
  55. v->rangelow = FREQ_LOW;
  56. v->rangehigh = FREQ_HIGH;
  57. v->capability = V4L2_TUNER_CAP_LOW;
  58. if (isa->drv->has_stereo)
  59. v->capability |= V4L2_TUNER_CAP_STEREO;
  60. if (ops->g_rxsubchans)
  61. v->rxsubchans = ops->g_rxsubchans(isa);
  62. else
  63. v->rxsubchans = V4L2_TUNER_SUB_MONO | V4L2_TUNER_SUB_STEREO;
  64. v->audmode = isa->stereo ? V4L2_TUNER_MODE_STEREO : V4L2_TUNER_MODE_MONO;
  65. if (ops->g_signal)
  66. v->signal = ops->g_signal(isa);
  67. else
  68. v->signal = (v->rxsubchans & V4L2_TUNER_SUB_STEREO) ?
  69. 0xffff : 0;
  70. return 0;
  71. }
  72. static int radio_isa_s_tuner(struct file *file, void *priv,
  73. const struct v4l2_tuner *v)
  74. {
  75. struct radio_isa_card *isa = video_drvdata(file);
  76. const struct radio_isa_ops *ops = isa->drv->ops;
  77. if (v->index)
  78. return -EINVAL;
  79. if (ops->s_stereo) {
  80. isa->stereo = (v->audmode == V4L2_TUNER_MODE_STEREO);
  81. return ops->s_stereo(isa, isa->stereo);
  82. }
  83. return 0;
  84. }
  85. static int radio_isa_s_frequency(struct file *file, void *priv,
  86. const struct v4l2_frequency *f)
  87. {
  88. struct radio_isa_card *isa = video_drvdata(file);
  89. u32 freq = f->frequency;
  90. int res;
  91. if (f->tuner != 0 || f->type != V4L2_TUNER_RADIO)
  92. return -EINVAL;
  93. freq = clamp(freq, FREQ_LOW, FREQ_HIGH);
  94. res = isa->drv->ops->s_frequency(isa, freq);
  95. if (res == 0)
  96. isa->freq = freq;
  97. return res;
  98. }
  99. static int radio_isa_g_frequency(struct file *file, void *priv,
  100. struct v4l2_frequency *f)
  101. {
  102. struct radio_isa_card *isa = video_drvdata(file);
  103. if (f->tuner != 0)
  104. return -EINVAL;
  105. f->type = V4L2_TUNER_RADIO;
  106. f->frequency = isa->freq;
  107. return 0;
  108. }
  109. static int radio_isa_s_ctrl(struct v4l2_ctrl *ctrl)
  110. {
  111. struct radio_isa_card *isa =
  112. container_of(ctrl->handler, struct radio_isa_card, hdl);
  113. switch (ctrl->id) {
  114. case V4L2_CID_AUDIO_MUTE:
  115. return isa->drv->ops->s_mute_volume(isa, ctrl->val,
  116. isa->volume ? isa->volume->val : 0);
  117. }
  118. return -EINVAL;
  119. }
  120. static int radio_isa_log_status(struct file *file, void *priv)
  121. {
  122. struct radio_isa_card *isa = video_drvdata(file);
  123. v4l2_info(&isa->v4l2_dev, "I/O Port = 0x%03x\n", isa->io);
  124. v4l2_ctrl_handler_log_status(&isa->hdl, isa->v4l2_dev.name);
  125. return 0;
  126. }
  127. static const struct v4l2_ctrl_ops radio_isa_ctrl_ops = {
  128. .s_ctrl = radio_isa_s_ctrl,
  129. };
  130. static const struct v4l2_file_operations radio_isa_fops = {
  131. .owner = THIS_MODULE,
  132. .open = v4l2_fh_open,
  133. .release = v4l2_fh_release,
  134. .poll = v4l2_ctrl_poll,
  135. .unlocked_ioctl = video_ioctl2,
  136. };
  137. static const struct v4l2_ioctl_ops radio_isa_ioctl_ops = {
  138. .vidioc_querycap = radio_isa_querycap,
  139. .vidioc_g_tuner = radio_isa_g_tuner,
  140. .vidioc_s_tuner = radio_isa_s_tuner,
  141. .vidioc_g_frequency = radio_isa_g_frequency,
  142. .vidioc_s_frequency = radio_isa_s_frequency,
  143. .vidioc_log_status = radio_isa_log_status,
  144. .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
  145. .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
  146. };
  147. int radio_isa_match(struct device *pdev, unsigned int dev)
  148. {
  149. struct radio_isa_driver *drv = pdev->platform_data;
  150. return drv->probe || drv->io_params[dev] >= 0;
  151. }
  152. EXPORT_SYMBOL_GPL(radio_isa_match);
  153. static bool radio_isa_valid_io(const struct radio_isa_driver *drv, int io)
  154. {
  155. int i;
  156. for (i = 0; i < drv->num_of_io_ports; i++)
  157. if (drv->io_ports[i] == io)
  158. return true;
  159. return false;
  160. }
  161. static struct radio_isa_card *radio_isa_alloc(struct radio_isa_driver *drv,
  162. struct device *pdev)
  163. {
  164. struct v4l2_device *v4l2_dev;
  165. struct radio_isa_card *isa = drv->ops->alloc();
  166. if (!isa)
  167. return NULL;
  168. dev_set_drvdata(pdev, isa);
  169. isa->drv = drv;
  170. v4l2_dev = &isa->v4l2_dev;
  171. strlcpy(v4l2_dev->name, dev_name(pdev), sizeof(v4l2_dev->name));
  172. return isa;
  173. }
  174. static int radio_isa_common_probe(struct radio_isa_card *isa,
  175. struct device *pdev,
  176. int radio_nr, unsigned region_size)
  177. {
  178. const struct radio_isa_driver *drv = isa->drv;
  179. const struct radio_isa_ops *ops = drv->ops;
  180. struct v4l2_device *v4l2_dev = &isa->v4l2_dev;
  181. int res;
  182. if (!request_region(isa->io, region_size, v4l2_dev->name)) {
  183. v4l2_err(v4l2_dev, "port 0x%x already in use\n", isa->io);
  184. kfree(isa);
  185. return -EBUSY;
  186. }
  187. res = v4l2_device_register(pdev, v4l2_dev);
  188. if (res < 0) {
  189. v4l2_err(v4l2_dev, "Could not register v4l2_device\n");
  190. goto err_dev_reg;
  191. }
  192. v4l2_ctrl_handler_init(&isa->hdl, 1);
  193. isa->mute = v4l2_ctrl_new_std(&isa->hdl, &radio_isa_ctrl_ops,
  194. V4L2_CID_AUDIO_MUTE, 0, 1, 1, 1);
  195. if (drv->max_volume)
  196. isa->volume = v4l2_ctrl_new_std(&isa->hdl, &radio_isa_ctrl_ops,
  197. V4L2_CID_AUDIO_VOLUME, 0, drv->max_volume, 1,
  198. drv->max_volume);
  199. v4l2_dev->ctrl_handler = &isa->hdl;
  200. if (isa->hdl.error) {
  201. res = isa->hdl.error;
  202. v4l2_err(v4l2_dev, "Could not register controls\n");
  203. goto err_hdl;
  204. }
  205. if (drv->max_volume)
  206. v4l2_ctrl_cluster(2, &isa->mute);
  207. v4l2_dev->ctrl_handler = &isa->hdl;
  208. mutex_init(&isa->lock);
  209. isa->vdev.lock = &isa->lock;
  210. strlcpy(isa->vdev.name, v4l2_dev->name, sizeof(isa->vdev.name));
  211. isa->vdev.v4l2_dev = v4l2_dev;
  212. isa->vdev.fops = &radio_isa_fops;
  213. isa->vdev.ioctl_ops = &radio_isa_ioctl_ops;
  214. isa->vdev.release = video_device_release_empty;
  215. video_set_drvdata(&isa->vdev, isa);
  216. isa->freq = FREQ_LOW;
  217. isa->stereo = drv->has_stereo;
  218. if (ops->init)
  219. res = ops->init(isa);
  220. if (!res)
  221. res = v4l2_ctrl_handler_setup(&isa->hdl);
  222. if (!res)
  223. res = ops->s_frequency(isa, isa->freq);
  224. if (!res && ops->s_stereo)
  225. res = ops->s_stereo(isa, isa->stereo);
  226. if (res < 0) {
  227. v4l2_err(v4l2_dev, "Could not setup card\n");
  228. goto err_hdl;
  229. }
  230. res = video_register_device(&isa->vdev, VFL_TYPE_RADIO, radio_nr);
  231. if (res < 0) {
  232. v4l2_err(v4l2_dev, "Could not register device node\n");
  233. goto err_hdl;
  234. }
  235. v4l2_info(v4l2_dev, "Initialized radio card %s on port 0x%03x\n",
  236. drv->card, isa->io);
  237. return 0;
  238. err_hdl:
  239. v4l2_ctrl_handler_free(&isa->hdl);
  240. err_dev_reg:
  241. release_region(isa->io, region_size);
  242. kfree(isa);
  243. return res;
  244. }
  245. static int radio_isa_common_remove(struct radio_isa_card *isa,
  246. unsigned region_size)
  247. {
  248. const struct radio_isa_ops *ops = isa->drv->ops;
  249. ops->s_mute_volume(isa, true, isa->volume ? isa->volume->cur.val : 0);
  250. video_unregister_device(&isa->vdev);
  251. v4l2_ctrl_handler_free(&isa->hdl);
  252. v4l2_device_unregister(&isa->v4l2_dev);
  253. release_region(isa->io, region_size);
  254. v4l2_info(&isa->v4l2_dev, "Removed radio card %s\n", isa->drv->card);
  255. kfree(isa);
  256. return 0;
  257. }
  258. int radio_isa_probe(struct device *pdev, unsigned int dev)
  259. {
  260. struct radio_isa_driver *drv = pdev->platform_data;
  261. const struct radio_isa_ops *ops = drv->ops;
  262. struct v4l2_device *v4l2_dev;
  263. struct radio_isa_card *isa;
  264. isa = radio_isa_alloc(drv, pdev);
  265. if (!isa)
  266. return -ENOMEM;
  267. isa->io = drv->io_params[dev];
  268. v4l2_dev = &isa->v4l2_dev;
  269. if (drv->probe && ops->probe) {
  270. int i;
  271. for (i = 0; i < drv->num_of_io_ports; ++i) {
  272. int io = drv->io_ports[i];
  273. if (request_region(io, drv->region_size, v4l2_dev->name)) {
  274. bool found = ops->probe(isa, io);
  275. release_region(io, drv->region_size);
  276. if (found) {
  277. isa->io = io;
  278. break;
  279. }
  280. }
  281. }
  282. }
  283. if (!radio_isa_valid_io(drv, isa->io)) {
  284. int i;
  285. if (isa->io < 0)
  286. return -ENODEV;
  287. v4l2_err(v4l2_dev, "you must set an I/O address with io=0x%03x",
  288. drv->io_ports[0]);
  289. for (i = 1; i < drv->num_of_io_ports; i++)
  290. printk(KERN_CONT "/0x%03x", drv->io_ports[i]);
  291. printk(KERN_CONT ".\n");
  292. kfree(isa);
  293. return -EINVAL;
  294. }
  295. return radio_isa_common_probe(isa, pdev, drv->radio_nr_params[dev],
  296. drv->region_size);
  297. }
  298. EXPORT_SYMBOL_GPL(radio_isa_probe);
  299. int radio_isa_remove(struct device *pdev, unsigned int dev)
  300. {
  301. struct radio_isa_card *isa = dev_get_drvdata(pdev);
  302. return radio_isa_common_remove(isa, isa->drv->region_size);
  303. }
  304. EXPORT_SYMBOL_GPL(radio_isa_remove);
  305. #ifdef CONFIG_PNP
  306. int radio_isa_pnp_probe(struct pnp_dev *dev, const struct pnp_device_id *dev_id)
  307. {
  308. struct pnp_driver *pnp_drv = to_pnp_driver(dev->dev.driver);
  309. struct radio_isa_driver *drv = container_of(pnp_drv,
  310. struct radio_isa_driver, pnp_driver);
  311. struct radio_isa_card *isa;
  312. if (!pnp_port_valid(dev, 0))
  313. return -ENODEV;
  314. isa = radio_isa_alloc(drv, &dev->dev);
  315. if (!isa)
  316. return -ENOMEM;
  317. isa->io = pnp_port_start(dev, 0);
  318. return radio_isa_common_probe(isa, &dev->dev, drv->radio_nr_params[0],
  319. pnp_port_len(dev, 0));
  320. }
  321. EXPORT_SYMBOL_GPL(radio_isa_pnp_probe);
  322. void radio_isa_pnp_remove(struct pnp_dev *dev)
  323. {
  324. struct radio_isa_card *isa = dev_get_drvdata(&dev->dev);
  325. radio_isa_common_remove(isa, pnp_port_len(dev, 0));
  326. }
  327. EXPORT_SYMBOL_GPL(radio_isa_pnp_remove);
  328. #endif