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