radio-isa.c 9.7 KB

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