]> git.ipfire.org Git - thirdparty/linux.git/commitdiff
media: v4l2-dev: Make open and release file operations mandatory
authorLaurent Pinchart <laurent.pinchart+renesas@ideasonboard.com>
Sun, 10 Aug 2025 01:30:13 +0000 (04:30 +0300)
committerHans Verkuil <hverkuil+cisco@kernel.org>
Wed, 13 Aug 2025 06:33:45 +0000 (08:33 +0200)
All V4L2 drivers implement the open and release file operations. As all
new drivers will need to use v4l2_fh, this situation won't change. Make
those two file operation mandatory at registration time. This allows
simplifying v4l2_open() and v4l2_release().

Signed-off-by: Laurent Pinchart <laurent.pinchart+renesas@ideasonboard.com>
Signed-off-by: Hans Verkuil <hverkuil+cisco@kernel.org>
drivers/media/v4l2-core/v4l2-dev.c

index c369235113d98ae26c30a1aa386e7d60d541a66e..1a4184b94838c8c95f3169778c1300c8dfb11dd7 100644 (file)
@@ -411,7 +411,7 @@ static int v4l2_mmap(struct file *filp, struct vm_area_struct *vm)
 static int v4l2_open(struct inode *inode, struct file *filp)
 {
        struct video_device *vdev;
-       int ret = 0;
+       int ret;
 
        /* Check if the video device is available */
        mutex_lock(&videodev_lock);
@@ -424,12 +424,11 @@ static int v4l2_open(struct inode *inode, struct file *filp)
        /* and increase the device refcount */
        video_get(vdev);
        mutex_unlock(&videodev_lock);
-       if (vdev->fops->open) {
-               if (video_is_registered(vdev))
-                       ret = vdev->fops->open(filp);
-               else
-                       ret = -ENODEV;
-       }
+
+       if (video_is_registered(vdev))
+               ret = vdev->fops->open(filp);
+       else
+               ret = -ENODEV;
 
        if (vdev->dev_debug & V4L2_DEV_DEBUG_FOP)
                dprintk("%s: open (%d)\n",
@@ -444,7 +443,7 @@ static int v4l2_open(struct inode *inode, struct file *filp)
 static int v4l2_release(struct inode *inode, struct file *filp)
 {
        struct video_device *vdev = video_devdata(filp);
-       int ret = 0;
+       int ret;
 
        /*
         * We need to serialize the release() with queueing new requests.
@@ -452,14 +451,12 @@ static int v4l2_release(struct inode *inode, struct file *filp)
         * operation, and that should not be mixed with queueing a new
         * request at the same time.
         */
-       if (vdev->fops->release) {
-               if (v4l2_device_supports_requests(vdev->v4l2_dev)) {
-                       mutex_lock(&vdev->v4l2_dev->mdev->req_queue_mutex);
-                       ret = vdev->fops->release(filp);
-                       mutex_unlock(&vdev->v4l2_dev->mdev->req_queue_mutex);
-               } else {
-                       ret = vdev->fops->release(filp);
-               }
+       if (v4l2_device_supports_requests(vdev->v4l2_dev)) {
+               mutex_lock(&vdev->v4l2_dev->mdev->req_queue_mutex);
+               ret = vdev->fops->release(filp);
+               mutex_unlock(&vdev->v4l2_dev->mdev->req_queue_mutex);
+       } else {
+               ret = vdev->fops->release(filp);
        }
 
        if (vdev->dev_debug & V4L2_DEV_DEBUG_FOP)
@@ -922,6 +919,9 @@ int __video_register_device(struct video_device *vdev,
        /* the device_caps field MUST be set for all but subdevs */
        if (WARN_ON(type != VFL_TYPE_SUBDEV && !vdev->device_caps))
                return -EINVAL;
+       /* the open and release file operations are mandatory */
+       if (WARN_ON(!vdev->fops || !vdev->fops->open || !vdev->fops->release))
+               return -EINVAL;
 
        /* v4l2_fh support */
        spin_lock_init(&vdev->fh_lock);