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