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