1 /* 2 * Video capture interface for Linux version 2 3 * 4 * A generic video device interface for the LINUX operating system 5 * using a set of device structures/vectors for low level operations. 6 * 7 * This program is free software; you can redistribute it and/or 8 * modify it under the terms of the GNU General Public License 9 * as published by the Free Software Foundation; either version 10 * 2 of the License, or (at your option) any later version. 11 * 12 * Authors: Alan Cox, <alan@lxorguk.ukuu.org.uk> (version 1) 13 * Mauro Carvalho Chehab <mchehab@infradead.org> (version 2) 14 * 15 * Fixes: 20000516 Claudio Matsuoka <claudio@conectiva.com> 16 * - Added procfs support 17 */ 18 19 #include <linux/module.h> 20 #include <linux/types.h> 21 #include <linux/kernel.h> 22 #include <linux/mm.h> 23 #include <linux/string.h> 24 #include <linux/errno.h> 25 #include <linux/init.h> 26 #include <linux/kmod.h> 27 #include <linux/slab.h> 28 #include <linux/uaccess.h> 29 30 #include <media/v4l2-common.h> 31 #include <media/v4l2-device.h> 32 #include <media/v4l2-ioctl.h> 33 34 #define VIDEO_NUM_DEVICES 256 35 #define VIDEO_NAME "video4linux" 36 37 /* 38 * sysfs stuff 39 */ 40 41 static ssize_t index_show(struct device *cd, 42 struct device_attribute *attr, char *buf) 43 { 44 struct video_device *vdev = to_video_device(cd); 45 46 return sprintf(buf, "%i\n", vdev->index); 47 } 48 static DEVICE_ATTR_RO(index); 49 50 static ssize_t dev_debug_show(struct device *cd, 51 struct device_attribute *attr, char *buf) 52 { 53 struct video_device *vdev = to_video_device(cd); 54 55 return sprintf(buf, "%i\n", vdev->dev_debug); 56 } 57 58 static ssize_t dev_debug_store(struct device *cd, struct device_attribute *attr, 59 const char *buf, size_t len) 60 { 61 struct video_device *vdev = to_video_device(cd); 62 int res = 0; 63 u16 value; 64 65 res = kstrtou16(buf, 0, &value); 66 if (res) 67 return res; 68 69 vdev->dev_debug = value; 70 return len; 71 } 72 static DEVICE_ATTR_RW(dev_debug); 73 74 static ssize_t name_show(struct device *cd, 75 struct device_attribute *attr, char *buf) 76 { 77 struct video_device *vdev = to_video_device(cd); 78 79 return sprintf(buf, "%.*s\n", (int)sizeof(vdev->name), vdev->name); 80 } 81 static DEVICE_ATTR_RO(name); 82 83 static struct attribute *video_device_attrs[] = { 84 &dev_attr_name.attr, 85 &dev_attr_dev_debug.attr, 86 &dev_attr_index.attr, 87 NULL, 88 }; 89 ATTRIBUTE_GROUPS(video_device); 90 91 /* 92 * Active devices 93 */ 94 static struct video_device *video_device[VIDEO_NUM_DEVICES]; 95 static DEFINE_MUTEX(videodev_lock); 96 static DECLARE_BITMAP(devnode_nums[VFL_TYPE_MAX], VIDEO_NUM_DEVICES); 97 98 /* Device node utility functions */ 99 100 /* Note: these utility functions all assume that vfl_type is in the range 101 [0, VFL_TYPE_MAX-1]. */ 102 103 #ifdef CONFIG_VIDEO_FIXED_MINOR_RANGES 104 /* Return the bitmap corresponding to vfl_type. */ 105 static inline unsigned long *devnode_bits(enum vfl_devnode_type vfl_type) 106 { 107 /* Any types not assigned to fixed minor ranges must be mapped to 108 one single bitmap for the purposes of finding a free node number 109 since all those unassigned types use the same minor range. */ 110 int idx = (vfl_type > VFL_TYPE_RADIO) ? VFL_TYPE_MAX - 1 : vfl_type; 111 112 return devnode_nums[idx]; 113 } 114 #else 115 /* Return the bitmap corresponding to vfl_type. */ 116 static inline unsigned long *devnode_bits(enum vfl_devnode_type vfl_type) 117 { 118 return devnode_nums[vfl_type]; 119 } 120 #endif 121 122 /* Mark device node number vdev->num as used */ 123 static inline void devnode_set(struct video_device *vdev) 124 { 125 set_bit(vdev->num, devnode_bits(vdev->vfl_type)); 126 } 127 128 /* Mark device node number vdev->num as unused */ 129 static inline void devnode_clear(struct video_device *vdev) 130 { 131 clear_bit(vdev->num, devnode_bits(vdev->vfl_type)); 132 } 133 134 /* Try to find a free device node number in the range [from, to> */ 135 static inline int devnode_find(struct video_device *vdev, int from, int to) 136 { 137 return find_next_zero_bit(devnode_bits(vdev->vfl_type), to, from); 138 } 139 140 struct video_device *video_device_alloc(void) 141 { 142 return kzalloc(sizeof(struct video_device), GFP_KERNEL); 143 } 144 EXPORT_SYMBOL(video_device_alloc); 145 146 void video_device_release(struct video_device *vdev) 147 { 148 kfree(vdev); 149 } 150 EXPORT_SYMBOL(video_device_release); 151 152 void video_device_release_empty(struct video_device *vdev) 153 { 154 /* Do nothing */ 155 /* Only valid when the video_device struct is a static. */ 156 } 157 EXPORT_SYMBOL(video_device_release_empty); 158 159 static inline void video_get(struct video_device *vdev) 160 { 161 get_device(&vdev->dev); 162 } 163 164 static inline void video_put(struct video_device *vdev) 165 { 166 put_device(&vdev->dev); 167 } 168 169 /* Called when the last user of the video device exits. */ 170 static void v4l2_device_release(struct device *cd) 171 { 172 struct video_device *vdev = to_video_device(cd); 173 struct v4l2_device *v4l2_dev = vdev->v4l2_dev; 174 175 mutex_lock(&videodev_lock); 176 if (WARN_ON(video_device[vdev->minor] != vdev)) { 177 /* should not happen */ 178 mutex_unlock(&videodev_lock); 179 return; 180 } 181 182 /* Free up this device for reuse */ 183 video_device[vdev->minor] = NULL; 184 185 /* Delete the cdev on this minor as well */ 186 cdev_del(vdev->cdev); 187 /* Just in case some driver tries to access this from 188 the release() callback. */ 189 vdev->cdev = NULL; 190 191 /* Mark device node number as free */ 192 devnode_clear(vdev); 193 194 mutex_unlock(&videodev_lock); 195 196 #if defined(CONFIG_MEDIA_CONTROLLER) 197 if (v4l2_dev->mdev) { 198 /* Remove interfaces and interface links */ 199 media_devnode_remove(vdev->intf_devnode); 200 if (vdev->entity.function != MEDIA_ENT_F_UNKNOWN) 201 media_device_unregister_entity(&vdev->entity); 202 } 203 #endif 204 205 /* Do not call v4l2_device_put if there is no release callback set. 206 * Drivers that have no v4l2_device release callback might free the 207 * v4l2_dev instance in the video_device release callback below, so we 208 * must perform this check here. 209 * 210 * TODO: In the long run all drivers that use v4l2_device should use the 211 * v4l2_device release callback. This check will then be unnecessary. 212 */ 213 if (v4l2_dev->release == NULL) 214 v4l2_dev = NULL; 215 216 /* Release video_device and perform other 217 cleanups as needed. */ 218 vdev->release(vdev); 219 220 /* Decrease v4l2_device refcount */ 221 if (v4l2_dev) 222 v4l2_device_put(v4l2_dev); 223 } 224 225 static struct class video_class = { 226 .name = VIDEO_NAME, 227 .dev_groups = video_device_groups, 228 }; 229 230 struct video_device *video_devdata(struct file *file) 231 { 232 return video_device[iminor(file_inode(file))]; 233 } 234 EXPORT_SYMBOL(video_devdata); 235 236 237 /* Priority handling */ 238 239 static inline bool prio_is_valid(enum v4l2_priority prio) 240 { 241 return prio == V4L2_PRIORITY_BACKGROUND || 242 prio == V4L2_PRIORITY_INTERACTIVE || 243 prio == V4L2_PRIORITY_RECORD; 244 } 245 246 void v4l2_prio_init(struct v4l2_prio_state *global) 247 { 248 memset(global, 0, sizeof(*global)); 249 } 250 EXPORT_SYMBOL(v4l2_prio_init); 251 252 int v4l2_prio_change(struct v4l2_prio_state *global, enum v4l2_priority *local, 253 enum v4l2_priority new) 254 { 255 if (!prio_is_valid(new)) 256 return -EINVAL; 257 if (*local == new) 258 return 0; 259 260 atomic_inc(&global->prios[new]); 261 if (prio_is_valid(*local)) 262 atomic_dec(&global->prios[*local]); 263 *local = new; 264 return 0; 265 } 266 EXPORT_SYMBOL(v4l2_prio_change); 267 268 void v4l2_prio_open(struct v4l2_prio_state *global, enum v4l2_priority *local) 269 { 270 v4l2_prio_change(global, local, V4L2_PRIORITY_DEFAULT); 271 } 272 EXPORT_SYMBOL(v4l2_prio_open); 273 274 void v4l2_prio_close(struct v4l2_prio_state *global, enum v4l2_priority local) 275 { 276 if (prio_is_valid(local)) 277 atomic_dec(&global->prios[local]); 278 } 279 EXPORT_SYMBOL(v4l2_prio_close); 280 281 enum v4l2_priority v4l2_prio_max(struct v4l2_prio_state *global) 282 { 283 if (atomic_read(&global->prios[V4L2_PRIORITY_RECORD]) > 0) 284 return V4L2_PRIORITY_RECORD; 285 if (atomic_read(&global->prios[V4L2_PRIORITY_INTERACTIVE]) > 0) 286 return V4L2_PRIORITY_INTERACTIVE; 287 if (atomic_read(&global->prios[V4L2_PRIORITY_BACKGROUND]) > 0) 288 return V4L2_PRIORITY_BACKGROUND; 289 return V4L2_PRIORITY_UNSET; 290 } 291 EXPORT_SYMBOL(v4l2_prio_max); 292 293 int v4l2_prio_check(struct v4l2_prio_state *global, enum v4l2_priority local) 294 { 295 return (local < v4l2_prio_max(global)) ? -EBUSY : 0; 296 } 297 EXPORT_SYMBOL(v4l2_prio_check); 298 299 300 static ssize_t v4l2_read(struct file *filp, char __user *buf, 301 size_t sz, loff_t *off) 302 { 303 struct video_device *vdev = video_devdata(filp); 304 int ret = -ENODEV; 305 306 if (!vdev->fops->read) 307 return -EINVAL; 308 if (video_is_registered(vdev)) 309 ret = vdev->fops->read(filp, buf, sz, off); 310 if ((vdev->dev_debug & V4L2_DEV_DEBUG_FOP) && 311 (vdev->dev_debug & V4L2_DEV_DEBUG_STREAMING)) 312 printk(KERN_DEBUG "%s: read: %zd (%d)\n", 313 video_device_node_name(vdev), sz, ret); 314 return ret; 315 } 316 317 static ssize_t v4l2_write(struct file *filp, const char __user *buf, 318 size_t sz, loff_t *off) 319 { 320 struct video_device *vdev = video_devdata(filp); 321 int ret = -ENODEV; 322 323 if (!vdev->fops->write) 324 return -EINVAL; 325 if (video_is_registered(vdev)) 326 ret = vdev->fops->write(filp, buf, sz, off); 327 if ((vdev->dev_debug & V4L2_DEV_DEBUG_FOP) && 328 (vdev->dev_debug & V4L2_DEV_DEBUG_STREAMING)) 329 printk(KERN_DEBUG "%s: write: %zd (%d)\n", 330 video_device_node_name(vdev), sz, ret); 331 return ret; 332 } 333 334 static __poll_t v4l2_poll(struct file *filp, struct poll_table_struct *poll) 335 { 336 struct video_device *vdev = video_devdata(filp); 337 __poll_t res = EPOLLERR | EPOLLHUP; 338 339 if (!vdev->fops->poll) 340 return DEFAULT_POLLMASK; 341 if (video_is_registered(vdev)) 342 res = vdev->fops->poll(filp, poll); 343 if (vdev->dev_debug & V4L2_DEV_DEBUG_POLL) 344 printk(KERN_DEBUG "%s: poll: %08x\n", 345 video_device_node_name(vdev), res); 346 return res; 347 } 348 349 static long v4l2_ioctl(struct file *filp, unsigned int cmd, unsigned long arg) 350 { 351 struct video_device *vdev = video_devdata(filp); 352 int ret = -ENODEV; 353 354 if (vdev->fops->unlocked_ioctl) { 355 struct mutex *lock = v4l2_ioctl_get_lock(vdev, cmd); 356 357 if (lock && mutex_lock_interruptible(lock)) 358 return -ERESTARTSYS; 359 if (video_is_registered(vdev)) 360 ret = vdev->fops->unlocked_ioctl(filp, cmd, arg); 361 if (lock) 362 mutex_unlock(lock); 363 } else 364 ret = -ENOTTY; 365 366 return ret; 367 } 368 369 #ifdef CONFIG_MMU 370 #define v4l2_get_unmapped_area NULL 371 #else 372 static unsigned long v4l2_get_unmapped_area(struct file *filp, 373 unsigned long addr, unsigned long len, unsigned long pgoff, 374 unsigned long flags) 375 { 376 struct video_device *vdev = video_devdata(filp); 377 int ret; 378 379 if (!vdev->fops->get_unmapped_area) 380 return -ENOSYS; 381 if (!video_is_registered(vdev)) 382 return -ENODEV; 383 ret = vdev->fops->get_unmapped_area(filp, addr, len, pgoff, flags); 384 if (vdev->dev_debug & V4L2_DEV_DEBUG_FOP) 385 printk(KERN_DEBUG "%s: get_unmapped_area (%d)\n", 386 video_device_node_name(vdev), ret); 387 return ret; 388 } 389 #endif 390 391 static int v4l2_mmap(struct file *filp, struct vm_area_struct *vm) 392 { 393 struct video_device *vdev = video_devdata(filp); 394 int ret = -ENODEV; 395 396 if (!vdev->fops->mmap) 397 return -ENODEV; 398 if (video_is_registered(vdev)) 399 ret = vdev->fops->mmap(filp, vm); 400 if (vdev->dev_debug & V4L2_DEV_DEBUG_FOP) 401 printk(KERN_DEBUG "%s: mmap (%d)\n", 402 video_device_node_name(vdev), ret); 403 return ret; 404 } 405 406 /* Override for the open function */ 407 static int v4l2_open(struct inode *inode, struct file *filp) 408 { 409 struct video_device *vdev; 410 int ret = 0; 411 412 /* Check if the video device is available */ 413 mutex_lock(&videodev_lock); 414 vdev = video_devdata(filp); 415 /* return ENODEV if the video device has already been removed. */ 416 if (vdev == NULL || !video_is_registered(vdev)) { 417 mutex_unlock(&videodev_lock); 418 return -ENODEV; 419 } 420 /* and increase the device refcount */ 421 video_get(vdev); 422 mutex_unlock(&videodev_lock); 423 if (vdev->fops->open) { 424 if (video_is_registered(vdev)) 425 ret = vdev->fops->open(filp); 426 else 427 ret = -ENODEV; 428 } 429 430 if (vdev->dev_debug & V4L2_DEV_DEBUG_FOP) 431 printk(KERN_DEBUG "%s: open (%d)\n", 432 video_device_node_name(vdev), ret); 433 /* decrease the refcount in case of an error */ 434 if (ret) 435 video_put(vdev); 436 return ret; 437 } 438 439 /* Override for the release function */ 440 static int v4l2_release(struct inode *inode, struct file *filp) 441 { 442 struct video_device *vdev = video_devdata(filp); 443 int ret = 0; 444 445 if (vdev->fops->release) 446 ret = vdev->fops->release(filp); 447 if (vdev->dev_debug & V4L2_DEV_DEBUG_FOP) 448 printk(KERN_DEBUG "%s: release\n", 449 video_device_node_name(vdev)); 450 451 /* decrease the refcount unconditionally since the release() 452 return value is ignored. */ 453 video_put(vdev); 454 return ret; 455 } 456 457 static const struct file_operations v4l2_fops = { 458 .owner = THIS_MODULE, 459 .read = v4l2_read, 460 .write = v4l2_write, 461 .open = v4l2_open, 462 .get_unmapped_area = v4l2_get_unmapped_area, 463 .mmap = v4l2_mmap, 464 .unlocked_ioctl = v4l2_ioctl, 465 #ifdef CONFIG_COMPAT 466 .compat_ioctl = v4l2_compat_ioctl32, 467 #endif 468 .release = v4l2_release, 469 .poll = v4l2_poll, 470 .llseek = no_llseek, 471 }; 472 473 /** 474 * get_index - assign stream index number based on v4l2_dev 475 * @vdev: video_device to assign index number to, vdev->v4l2_dev should be assigned 476 * 477 * Note that when this is called the new device has not yet been registered 478 * in the video_device array, but it was able to obtain a minor number. 479 * 480 * This means that we can always obtain a free stream index number since 481 * the worst case scenario is that there are VIDEO_NUM_DEVICES - 1 slots in 482 * use of the video_device array. 483 * 484 * Returns a free index number. 485 */ 486 static int get_index(struct video_device *vdev) 487 { 488 /* This can be static since this function is called with the global 489 videodev_lock held. */ 490 static DECLARE_BITMAP(used, VIDEO_NUM_DEVICES); 491 int i; 492 493 bitmap_zero(used, VIDEO_NUM_DEVICES); 494 495 for (i = 0; i < VIDEO_NUM_DEVICES; i++) { 496 if (video_device[i] != NULL && 497 video_device[i]->v4l2_dev == vdev->v4l2_dev) { 498 set_bit(video_device[i]->index, used); 499 } 500 } 501 502 return find_first_zero_bit(used, VIDEO_NUM_DEVICES); 503 } 504 505 #define SET_VALID_IOCTL(ops, cmd, op) \ 506 if (ops->op) \ 507 set_bit(_IOC_NR(cmd), valid_ioctls) 508 509 /* This determines which ioctls are actually implemented in the driver. 510 It's a one-time thing which simplifies video_ioctl2 as it can just do 511 a bit test. 512 513 Note that drivers can override this by setting bits to 1 in 514 vdev->valid_ioctls. If an ioctl is marked as 1 when this function is 515 called, then that ioctl will actually be marked as unimplemented. 516 517 It does that by first setting up the local valid_ioctls bitmap, and 518 at the end do a: 519 520 vdev->valid_ioctls = valid_ioctls & ~(vdev->valid_ioctls) 521 */ 522 static void determine_valid_ioctls(struct video_device *vdev) 523 { 524 DECLARE_BITMAP(valid_ioctls, BASE_VIDIOC_PRIVATE); 525 const struct v4l2_ioctl_ops *ops = vdev->ioctl_ops; 526 bool is_vid = vdev->vfl_type == VFL_TYPE_GRABBER; 527 bool is_vbi = vdev->vfl_type == VFL_TYPE_VBI; 528 bool is_radio = vdev->vfl_type == VFL_TYPE_RADIO; 529 bool is_sdr = vdev->vfl_type == VFL_TYPE_SDR; 530 bool is_tch = vdev->vfl_type == VFL_TYPE_TOUCH; 531 bool is_rx = vdev->vfl_dir != VFL_DIR_TX; 532 bool is_tx = vdev->vfl_dir != VFL_DIR_RX; 533 534 bitmap_zero(valid_ioctls, BASE_VIDIOC_PRIVATE); 535 536 /* vfl_type and vfl_dir independent ioctls */ 537 538 SET_VALID_IOCTL(ops, VIDIOC_QUERYCAP, vidioc_querycap); 539 set_bit(_IOC_NR(VIDIOC_G_PRIORITY), valid_ioctls); 540 set_bit(_IOC_NR(VIDIOC_S_PRIORITY), valid_ioctls); 541 542 /* Note: the control handler can also be passed through the filehandle, 543 and that can't be tested here. If the bit for these control ioctls 544 is set, then the ioctl is valid. But if it is 0, then it can still 545 be valid if the filehandle passed the control handler. */ 546 if (vdev->ctrl_handler || ops->vidioc_queryctrl) 547 set_bit(_IOC_NR(VIDIOC_QUERYCTRL), valid_ioctls); 548 if (vdev->ctrl_handler || ops->vidioc_query_ext_ctrl) 549 set_bit(_IOC_NR(VIDIOC_QUERY_EXT_CTRL), valid_ioctls); 550 if (vdev->ctrl_handler || ops->vidioc_g_ctrl || ops->vidioc_g_ext_ctrls) 551 set_bit(_IOC_NR(VIDIOC_G_CTRL), valid_ioctls); 552 if (vdev->ctrl_handler || ops->vidioc_s_ctrl || ops->vidioc_s_ext_ctrls) 553 set_bit(_IOC_NR(VIDIOC_S_CTRL), valid_ioctls); 554 if (vdev->ctrl_handler || ops->vidioc_g_ext_ctrls) 555 set_bit(_IOC_NR(VIDIOC_G_EXT_CTRLS), valid_ioctls); 556 if (vdev->ctrl_handler || ops->vidioc_s_ext_ctrls) 557 set_bit(_IOC_NR(VIDIOC_S_EXT_CTRLS), valid_ioctls); 558 if (vdev->ctrl_handler || ops->vidioc_try_ext_ctrls) 559 set_bit(_IOC_NR(VIDIOC_TRY_EXT_CTRLS), valid_ioctls); 560 if (vdev->ctrl_handler || ops->vidioc_querymenu) 561 set_bit(_IOC_NR(VIDIOC_QUERYMENU), valid_ioctls); 562 SET_VALID_IOCTL(ops, VIDIOC_G_FREQUENCY, vidioc_g_frequency); 563 SET_VALID_IOCTL(ops, VIDIOC_S_FREQUENCY, vidioc_s_frequency); 564 SET_VALID_IOCTL(ops, VIDIOC_LOG_STATUS, vidioc_log_status); 565 #ifdef CONFIG_VIDEO_ADV_DEBUG 566 set_bit(_IOC_NR(VIDIOC_DBG_G_CHIP_INFO), valid_ioctls); 567 set_bit(_IOC_NR(VIDIOC_DBG_G_REGISTER), valid_ioctls); 568 set_bit(_IOC_NR(VIDIOC_DBG_S_REGISTER), valid_ioctls); 569 #endif 570 /* yes, really vidioc_subscribe_event */ 571 SET_VALID_IOCTL(ops, VIDIOC_DQEVENT, vidioc_subscribe_event); 572 SET_VALID_IOCTL(ops, VIDIOC_SUBSCRIBE_EVENT, vidioc_subscribe_event); 573 SET_VALID_IOCTL(ops, VIDIOC_UNSUBSCRIBE_EVENT, vidioc_unsubscribe_event); 574 if (ops->vidioc_enum_freq_bands || ops->vidioc_g_tuner || ops->vidioc_g_modulator) 575 set_bit(_IOC_NR(VIDIOC_ENUM_FREQ_BANDS), valid_ioctls); 576 577 if (is_vid || is_tch) { 578 /* video and metadata specific ioctls */ 579 if ((is_rx && (ops->vidioc_enum_fmt_vid_cap || 580 ops->vidioc_enum_fmt_vid_cap_mplane || 581 ops->vidioc_enum_fmt_vid_overlay || 582 ops->vidioc_enum_fmt_meta_cap)) || 583 (is_tx && (ops->vidioc_enum_fmt_vid_out || 584 ops->vidioc_enum_fmt_vid_out_mplane))) 585 set_bit(_IOC_NR(VIDIOC_ENUM_FMT), valid_ioctls); 586 if ((is_rx && (ops->vidioc_g_fmt_vid_cap || 587 ops->vidioc_g_fmt_vid_cap_mplane || 588 ops->vidioc_g_fmt_vid_overlay || 589 ops->vidioc_g_fmt_meta_cap)) || 590 (is_tx && (ops->vidioc_g_fmt_vid_out || 591 ops->vidioc_g_fmt_vid_out_mplane || 592 ops->vidioc_g_fmt_vid_out_overlay))) 593 set_bit(_IOC_NR(VIDIOC_G_FMT), valid_ioctls); 594 if ((is_rx && (ops->vidioc_s_fmt_vid_cap || 595 ops->vidioc_s_fmt_vid_cap_mplane || 596 ops->vidioc_s_fmt_vid_overlay || 597 ops->vidioc_s_fmt_meta_cap)) || 598 (is_tx && (ops->vidioc_s_fmt_vid_out || 599 ops->vidioc_s_fmt_vid_out_mplane || 600 ops->vidioc_s_fmt_vid_out_overlay))) 601 set_bit(_IOC_NR(VIDIOC_S_FMT), valid_ioctls); 602 if ((is_rx && (ops->vidioc_try_fmt_vid_cap || 603 ops->vidioc_try_fmt_vid_cap_mplane || 604 ops->vidioc_try_fmt_vid_overlay || 605 ops->vidioc_try_fmt_meta_cap)) || 606 (is_tx && (ops->vidioc_try_fmt_vid_out || 607 ops->vidioc_try_fmt_vid_out_mplane || 608 ops->vidioc_try_fmt_vid_out_overlay))) 609 set_bit(_IOC_NR(VIDIOC_TRY_FMT), valid_ioctls); 610 SET_VALID_IOCTL(ops, VIDIOC_OVERLAY, vidioc_overlay); 611 SET_VALID_IOCTL(ops, VIDIOC_G_FBUF, vidioc_g_fbuf); 612 SET_VALID_IOCTL(ops, VIDIOC_S_FBUF, vidioc_s_fbuf); 613 SET_VALID_IOCTL(ops, VIDIOC_G_JPEGCOMP, vidioc_g_jpegcomp); 614 SET_VALID_IOCTL(ops, VIDIOC_S_JPEGCOMP, vidioc_s_jpegcomp); 615 SET_VALID_IOCTL(ops, VIDIOC_G_ENC_INDEX, vidioc_g_enc_index); 616 SET_VALID_IOCTL(ops, VIDIOC_ENCODER_CMD, vidioc_encoder_cmd); 617 SET_VALID_IOCTL(ops, VIDIOC_TRY_ENCODER_CMD, vidioc_try_encoder_cmd); 618 SET_VALID_IOCTL(ops, VIDIOC_DECODER_CMD, vidioc_decoder_cmd); 619 SET_VALID_IOCTL(ops, VIDIOC_TRY_DECODER_CMD, vidioc_try_decoder_cmd); 620 SET_VALID_IOCTL(ops, VIDIOC_ENUM_FRAMESIZES, vidioc_enum_framesizes); 621 SET_VALID_IOCTL(ops, VIDIOC_ENUM_FRAMEINTERVALS, vidioc_enum_frameintervals); 622 if (ops->vidioc_g_crop || ops->vidioc_g_selection) 623 set_bit(_IOC_NR(VIDIOC_G_CROP), valid_ioctls); 624 if (ops->vidioc_s_crop || ops->vidioc_s_selection) 625 set_bit(_IOC_NR(VIDIOC_S_CROP), valid_ioctls); 626 SET_VALID_IOCTL(ops, VIDIOC_G_SELECTION, vidioc_g_selection); 627 SET_VALID_IOCTL(ops, VIDIOC_S_SELECTION, vidioc_s_selection); 628 if (ops->vidioc_cropcap || ops->vidioc_g_selection) 629 set_bit(_IOC_NR(VIDIOC_CROPCAP), valid_ioctls); 630 } else if (is_vbi) { 631 /* vbi specific ioctls */ 632 if ((is_rx && (ops->vidioc_g_fmt_vbi_cap || 633 ops->vidioc_g_fmt_sliced_vbi_cap)) || 634 (is_tx && (ops->vidioc_g_fmt_vbi_out || 635 ops->vidioc_g_fmt_sliced_vbi_out))) 636 set_bit(_IOC_NR(VIDIOC_G_FMT), valid_ioctls); 637 if ((is_rx && (ops->vidioc_s_fmt_vbi_cap || 638 ops->vidioc_s_fmt_sliced_vbi_cap)) || 639 (is_tx && (ops->vidioc_s_fmt_vbi_out || 640 ops->vidioc_s_fmt_sliced_vbi_out))) 641 set_bit(_IOC_NR(VIDIOC_S_FMT), valid_ioctls); 642 if ((is_rx && (ops->vidioc_try_fmt_vbi_cap || 643 ops->vidioc_try_fmt_sliced_vbi_cap)) || 644 (is_tx && (ops->vidioc_try_fmt_vbi_out || 645 ops->vidioc_try_fmt_sliced_vbi_out))) 646 set_bit(_IOC_NR(VIDIOC_TRY_FMT), valid_ioctls); 647 SET_VALID_IOCTL(ops, VIDIOC_G_SLICED_VBI_CAP, vidioc_g_sliced_vbi_cap); 648 } else if (is_sdr && is_rx) { 649 /* SDR receiver specific ioctls */ 650 if (ops->vidioc_enum_fmt_sdr_cap) 651 set_bit(_IOC_NR(VIDIOC_ENUM_FMT), valid_ioctls); 652 if (ops->vidioc_g_fmt_sdr_cap) 653 set_bit(_IOC_NR(VIDIOC_G_FMT), valid_ioctls); 654 if (ops->vidioc_s_fmt_sdr_cap) 655 set_bit(_IOC_NR(VIDIOC_S_FMT), valid_ioctls); 656 if (ops->vidioc_try_fmt_sdr_cap) 657 set_bit(_IOC_NR(VIDIOC_TRY_FMT), valid_ioctls); 658 } else if (is_sdr && is_tx) { 659 /* SDR transmitter specific ioctls */ 660 if (ops->vidioc_enum_fmt_sdr_out) 661 set_bit(_IOC_NR(VIDIOC_ENUM_FMT), valid_ioctls); 662 if (ops->vidioc_g_fmt_sdr_out) 663 set_bit(_IOC_NR(VIDIOC_G_FMT), valid_ioctls); 664 if (ops->vidioc_s_fmt_sdr_out) 665 set_bit(_IOC_NR(VIDIOC_S_FMT), valid_ioctls); 666 if (ops->vidioc_try_fmt_sdr_out) 667 set_bit(_IOC_NR(VIDIOC_TRY_FMT), valid_ioctls); 668 } 669 670 if (is_vid || is_vbi || is_sdr || is_tch) { 671 /* ioctls valid for video, metadata, vbi or sdr */ 672 SET_VALID_IOCTL(ops, VIDIOC_REQBUFS, vidioc_reqbufs); 673 SET_VALID_IOCTL(ops, VIDIOC_QUERYBUF, vidioc_querybuf); 674 SET_VALID_IOCTL(ops, VIDIOC_QBUF, vidioc_qbuf); 675 SET_VALID_IOCTL(ops, VIDIOC_EXPBUF, vidioc_expbuf); 676 SET_VALID_IOCTL(ops, VIDIOC_DQBUF, vidioc_dqbuf); 677 SET_VALID_IOCTL(ops, VIDIOC_CREATE_BUFS, vidioc_create_bufs); 678 SET_VALID_IOCTL(ops, VIDIOC_PREPARE_BUF, vidioc_prepare_buf); 679 SET_VALID_IOCTL(ops, VIDIOC_STREAMON, vidioc_streamon); 680 SET_VALID_IOCTL(ops, VIDIOC_STREAMOFF, vidioc_streamoff); 681 } 682 683 if (is_vid || is_vbi || is_tch) { 684 /* ioctls valid for video or vbi */ 685 if (ops->vidioc_s_std) 686 set_bit(_IOC_NR(VIDIOC_ENUMSTD), valid_ioctls); 687 SET_VALID_IOCTL(ops, VIDIOC_S_STD, vidioc_s_std); 688 SET_VALID_IOCTL(ops, VIDIOC_G_STD, vidioc_g_std); 689 if (is_rx) { 690 SET_VALID_IOCTL(ops, VIDIOC_QUERYSTD, vidioc_querystd); 691 SET_VALID_IOCTL(ops, VIDIOC_ENUMINPUT, vidioc_enum_input); 692 SET_VALID_IOCTL(ops, VIDIOC_G_INPUT, vidioc_g_input); 693 SET_VALID_IOCTL(ops, VIDIOC_S_INPUT, vidioc_s_input); 694 SET_VALID_IOCTL(ops, VIDIOC_ENUMAUDIO, vidioc_enumaudio); 695 SET_VALID_IOCTL(ops, VIDIOC_G_AUDIO, vidioc_g_audio); 696 SET_VALID_IOCTL(ops, VIDIOC_S_AUDIO, vidioc_s_audio); 697 SET_VALID_IOCTL(ops, VIDIOC_QUERY_DV_TIMINGS, vidioc_query_dv_timings); 698 SET_VALID_IOCTL(ops, VIDIOC_S_EDID, vidioc_s_edid); 699 } 700 if (is_tx) { 701 SET_VALID_IOCTL(ops, VIDIOC_ENUMOUTPUT, vidioc_enum_output); 702 SET_VALID_IOCTL(ops, VIDIOC_G_OUTPUT, vidioc_g_output); 703 SET_VALID_IOCTL(ops, VIDIOC_S_OUTPUT, vidioc_s_output); 704 SET_VALID_IOCTL(ops, VIDIOC_ENUMAUDOUT, vidioc_enumaudout); 705 SET_VALID_IOCTL(ops, VIDIOC_G_AUDOUT, vidioc_g_audout); 706 SET_VALID_IOCTL(ops, VIDIOC_S_AUDOUT, vidioc_s_audout); 707 } 708 if (ops->vidioc_g_parm || (vdev->vfl_type == VFL_TYPE_GRABBER && 709 ops->vidioc_g_std)) 710 set_bit(_IOC_NR(VIDIOC_G_PARM), valid_ioctls); 711 SET_VALID_IOCTL(ops, VIDIOC_S_PARM, vidioc_s_parm); 712 SET_VALID_IOCTL(ops, VIDIOC_S_DV_TIMINGS, vidioc_s_dv_timings); 713 SET_VALID_IOCTL(ops, VIDIOC_G_DV_TIMINGS, vidioc_g_dv_timings); 714 SET_VALID_IOCTL(ops, VIDIOC_ENUM_DV_TIMINGS, vidioc_enum_dv_timings); 715 SET_VALID_IOCTL(ops, VIDIOC_DV_TIMINGS_CAP, vidioc_dv_timings_cap); 716 SET_VALID_IOCTL(ops, VIDIOC_G_EDID, vidioc_g_edid); 717 } 718 if (is_tx && (is_radio || is_sdr)) { 719 /* radio transmitter only ioctls */ 720 SET_VALID_IOCTL(ops, VIDIOC_G_MODULATOR, vidioc_g_modulator); 721 SET_VALID_IOCTL(ops, VIDIOC_S_MODULATOR, vidioc_s_modulator); 722 } 723 if (is_rx) { 724 /* receiver only ioctls */ 725 SET_VALID_IOCTL(ops, VIDIOC_G_TUNER, vidioc_g_tuner); 726 SET_VALID_IOCTL(ops, VIDIOC_S_TUNER, vidioc_s_tuner); 727 SET_VALID_IOCTL(ops, VIDIOC_S_HW_FREQ_SEEK, vidioc_s_hw_freq_seek); 728 } 729 730 bitmap_andnot(vdev->valid_ioctls, valid_ioctls, vdev->valid_ioctls, 731 BASE_VIDIOC_PRIVATE); 732 } 733 734 static int video_register_media_controller(struct video_device *vdev, int type) 735 { 736 #if defined(CONFIG_MEDIA_CONTROLLER) 737 u32 intf_type; 738 int ret; 739 740 if (!vdev->v4l2_dev->mdev) 741 return 0; 742 743 vdev->entity.obj_type = MEDIA_ENTITY_TYPE_VIDEO_DEVICE; 744 vdev->entity.function = MEDIA_ENT_F_UNKNOWN; 745 746 switch (type) { 747 case VFL_TYPE_GRABBER: 748 intf_type = MEDIA_INTF_T_V4L_VIDEO; 749 vdev->entity.function = MEDIA_ENT_F_IO_V4L; 750 break; 751 case VFL_TYPE_VBI: 752 intf_type = MEDIA_INTF_T_V4L_VBI; 753 vdev->entity.function = MEDIA_ENT_F_IO_VBI; 754 break; 755 case VFL_TYPE_SDR: 756 intf_type = MEDIA_INTF_T_V4L_SWRADIO; 757 vdev->entity.function = MEDIA_ENT_F_IO_SWRADIO; 758 break; 759 case VFL_TYPE_TOUCH: 760 intf_type = MEDIA_INTF_T_V4L_TOUCH; 761 vdev->entity.function = MEDIA_ENT_F_IO_V4L; 762 break; 763 case VFL_TYPE_RADIO: 764 intf_type = MEDIA_INTF_T_V4L_RADIO; 765 /* 766 * Radio doesn't have an entity at the V4L2 side to represent 767 * radio input or output. Instead, the audio input/output goes 768 * via either physical wires or ALSA. 769 */ 770 break; 771 case VFL_TYPE_SUBDEV: 772 intf_type = MEDIA_INTF_T_V4L_SUBDEV; 773 /* Entity will be created via v4l2_device_register_subdev() */ 774 break; 775 default: 776 return 0; 777 } 778 779 if (vdev->entity.function != MEDIA_ENT_F_UNKNOWN) { 780 vdev->entity.name = vdev->name; 781 782 /* Needed just for backward compatibility with legacy MC API */ 783 vdev->entity.info.dev.major = VIDEO_MAJOR; 784 vdev->entity.info.dev.minor = vdev->minor; 785 786 ret = media_device_register_entity(vdev->v4l2_dev->mdev, 787 &vdev->entity); 788 if (ret < 0) { 789 printk(KERN_WARNING 790 "%s: media_device_register_entity failed\n", 791 __func__); 792 return ret; 793 } 794 } 795 796 vdev->intf_devnode = media_devnode_create(vdev->v4l2_dev->mdev, 797 intf_type, 798 0, VIDEO_MAJOR, 799 vdev->minor); 800 if (!vdev->intf_devnode) { 801 media_device_unregister_entity(&vdev->entity); 802 return -ENOMEM; 803 } 804 805 if (vdev->entity.function != MEDIA_ENT_F_UNKNOWN) { 806 struct media_link *link; 807 808 link = media_create_intf_link(&vdev->entity, 809 &vdev->intf_devnode->intf, 810 MEDIA_LNK_FL_ENABLED); 811 if (!link) { 812 media_devnode_remove(vdev->intf_devnode); 813 media_device_unregister_entity(&vdev->entity); 814 return -ENOMEM; 815 } 816 } 817 818 /* FIXME: how to create the other interface links? */ 819 820 #endif 821 return 0; 822 } 823 824 int __video_register_device(struct video_device *vdev, 825 enum vfl_devnode_type type, 826 int nr, int warn_if_nr_in_use, 827 struct module *owner) 828 { 829 int i = 0; 830 int ret; 831 int minor_offset = 0; 832 int minor_cnt = VIDEO_NUM_DEVICES; 833 const char *name_base; 834 835 /* A minor value of -1 marks this video device as never 836 having been registered */ 837 vdev->minor = -1; 838 839 /* the release callback MUST be present */ 840 if (WARN_ON(!vdev->release)) 841 return -EINVAL; 842 /* the v4l2_dev pointer MUST be present */ 843 if (WARN_ON(!vdev->v4l2_dev)) 844 return -EINVAL; 845 846 /* v4l2_fh support */ 847 spin_lock_init(&vdev->fh_lock); 848 INIT_LIST_HEAD(&vdev->fh_list); 849 850 /* Part 1: check device type */ 851 switch (type) { 852 case VFL_TYPE_GRABBER: 853 name_base = "video"; 854 break; 855 case VFL_TYPE_VBI: 856 name_base = "vbi"; 857 break; 858 case VFL_TYPE_RADIO: 859 name_base = "radio"; 860 break; 861 case VFL_TYPE_SUBDEV: 862 name_base = "v4l-subdev"; 863 break; 864 case VFL_TYPE_SDR: 865 /* Use device name 'swradio' because 'sdr' was already taken. */ 866 name_base = "swradio"; 867 break; 868 case VFL_TYPE_TOUCH: 869 name_base = "v4l-touch"; 870 break; 871 default: 872 printk(KERN_ERR "%s called with unknown type: %d\n", 873 __func__, type); 874 return -EINVAL; 875 } 876 877 vdev->vfl_type = type; 878 vdev->cdev = NULL; 879 if (vdev->dev_parent == NULL) 880 vdev->dev_parent = vdev->v4l2_dev->dev; 881 if (vdev->ctrl_handler == NULL) 882 vdev->ctrl_handler = vdev->v4l2_dev->ctrl_handler; 883 /* If the prio state pointer is NULL, then use the v4l2_device 884 prio state. */ 885 if (vdev->prio == NULL) 886 vdev->prio = &vdev->v4l2_dev->prio; 887 888 /* Part 2: find a free minor, device node number and device index. */ 889 #ifdef CONFIG_VIDEO_FIXED_MINOR_RANGES 890 /* Keep the ranges for the first four types for historical 891 * reasons. 892 * Newer devices (not yet in place) should use the range 893 * of 128-191 and just pick the first free minor there 894 * (new style). */ 895 switch (type) { 896 case VFL_TYPE_GRABBER: 897 minor_offset = 0; 898 minor_cnt = 64; 899 break; 900 case VFL_TYPE_RADIO: 901 minor_offset = 64; 902 minor_cnt = 64; 903 break; 904 case VFL_TYPE_VBI: 905 minor_offset = 224; 906 minor_cnt = 32; 907 break; 908 default: 909 minor_offset = 128; 910 minor_cnt = 64; 911 break; 912 } 913 #endif 914 915 /* Pick a device node number */ 916 mutex_lock(&videodev_lock); 917 nr = devnode_find(vdev, nr == -1 ? 0 : nr, minor_cnt); 918 if (nr == minor_cnt) 919 nr = devnode_find(vdev, 0, minor_cnt); 920 if (nr == minor_cnt) { 921 printk(KERN_ERR "could not get a free device node number\n"); 922 mutex_unlock(&videodev_lock); 923 return -ENFILE; 924 } 925 #ifdef CONFIG_VIDEO_FIXED_MINOR_RANGES 926 /* 1-on-1 mapping of device node number to minor number */ 927 i = nr; 928 #else 929 /* The device node number and minor numbers are independent, so 930 we just find the first free minor number. */ 931 for (i = 0; i < VIDEO_NUM_DEVICES; i++) 932 if (video_device[i] == NULL) 933 break; 934 if (i == VIDEO_NUM_DEVICES) { 935 mutex_unlock(&videodev_lock); 936 printk(KERN_ERR "could not get a free minor\n"); 937 return -ENFILE; 938 } 939 #endif 940 vdev->minor = i + minor_offset; 941 vdev->num = nr; 942 devnode_set(vdev); 943 944 /* Should not happen since we thought this minor was free */ 945 WARN_ON(video_device[vdev->minor] != NULL); 946 vdev->index = get_index(vdev); 947 video_device[vdev->minor] = vdev; 948 mutex_unlock(&videodev_lock); 949 950 if (vdev->ioctl_ops) 951 determine_valid_ioctls(vdev); 952 953 /* Part 3: Initialize the character device */ 954 vdev->cdev = cdev_alloc(); 955 if (vdev->cdev == NULL) { 956 ret = -ENOMEM; 957 goto cleanup; 958 } 959 vdev->cdev->ops = &v4l2_fops; 960 vdev->cdev->owner = owner; 961 ret = cdev_add(vdev->cdev, MKDEV(VIDEO_MAJOR, vdev->minor), 1); 962 if (ret < 0) { 963 printk(KERN_ERR "%s: cdev_add failed\n", __func__); 964 kfree(vdev->cdev); 965 vdev->cdev = NULL; 966 goto cleanup; 967 } 968 969 /* Part 4: register the device with sysfs */ 970 vdev->dev.class = &video_class; 971 vdev->dev.devt = MKDEV(VIDEO_MAJOR, vdev->minor); 972 vdev->dev.parent = vdev->dev_parent; 973 dev_set_name(&vdev->dev, "%s%d", name_base, vdev->num); 974 ret = device_register(&vdev->dev); 975 if (ret < 0) { 976 printk(KERN_ERR "%s: device_register failed\n", __func__); 977 goto cleanup; 978 } 979 /* Register the release callback that will be called when the last 980 reference to the device goes away. */ 981 vdev->dev.release = v4l2_device_release; 982 983 if (nr != -1 && nr != vdev->num && warn_if_nr_in_use) 984 printk(KERN_WARNING "%s: requested %s%d, got %s\n", __func__, 985 name_base, nr, video_device_node_name(vdev)); 986 987 /* Increase v4l2_device refcount */ 988 v4l2_device_get(vdev->v4l2_dev); 989 990 /* Part 5: Register the entity. */ 991 ret = video_register_media_controller(vdev, type); 992 993 /* Part 6: Activate this minor. The char device can now be used. */ 994 set_bit(V4L2_FL_REGISTERED, &vdev->flags); 995 996 return 0; 997 998 cleanup: 999 mutex_lock(&videodev_lock); 1000 if (vdev->cdev) 1001 cdev_del(vdev->cdev); 1002 video_device[vdev->minor] = NULL; 1003 devnode_clear(vdev); 1004 mutex_unlock(&videodev_lock); 1005 /* Mark this video device as never having been registered. */ 1006 vdev->minor = -1; 1007 return ret; 1008 } 1009 EXPORT_SYMBOL(__video_register_device); 1010 1011 /** 1012 * video_unregister_device - unregister a video4linux device 1013 * @vdev: the device to unregister 1014 * 1015 * This unregisters the passed device. Future open calls will 1016 * be met with errors. 1017 */ 1018 void video_unregister_device(struct video_device *vdev) 1019 { 1020 /* Check if vdev was ever registered at all */ 1021 if (!vdev || !video_is_registered(vdev)) 1022 return; 1023 1024 mutex_lock(&videodev_lock); 1025 /* This must be in a critical section to prevent a race with v4l2_open. 1026 * Once this bit has been cleared video_get may never be called again. 1027 */ 1028 clear_bit(V4L2_FL_REGISTERED, &vdev->flags); 1029 mutex_unlock(&videodev_lock); 1030 device_unregister(&vdev->dev); 1031 } 1032 EXPORT_SYMBOL(video_unregister_device); 1033 1034 /* 1035 * Initialise video for linux 1036 */ 1037 static int __init videodev_init(void) 1038 { 1039 dev_t dev = MKDEV(VIDEO_MAJOR, 0); 1040 int ret; 1041 1042 printk(KERN_INFO "Linux video capture interface: v2.00\n"); 1043 ret = register_chrdev_region(dev, VIDEO_NUM_DEVICES, VIDEO_NAME); 1044 if (ret < 0) { 1045 printk(KERN_WARNING "videodev: unable to get major %d\n", 1046 VIDEO_MAJOR); 1047 return ret; 1048 } 1049 1050 ret = class_register(&video_class); 1051 if (ret < 0) { 1052 unregister_chrdev_region(dev, VIDEO_NUM_DEVICES); 1053 printk(KERN_WARNING "video_dev: class_register failed\n"); 1054 return -EIO; 1055 } 1056 1057 return 0; 1058 } 1059 1060 static void __exit videodev_exit(void) 1061 { 1062 dev_t dev = MKDEV(VIDEO_MAJOR, 0); 1063 1064 class_unregister(&video_class); 1065 unregister_chrdev_region(dev, VIDEO_NUM_DEVICES); 1066 } 1067 1068 subsys_initcall(videodev_init); 1069 module_exit(videodev_exit) 1070 1071 MODULE_AUTHOR("Alan Cox, Mauro Carvalho Chehab <mchehab@infradead.org>"); 1072 MODULE_DESCRIPTION("Device registrar for Video4Linux drivers v2"); 1073 MODULE_LICENSE("GPL"); 1074 MODULE_ALIAS_CHARDEV_MAJOR(VIDEO_MAJOR); 1075