radio-isa.c 11 KB

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