Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * v4l2-fh.c
0004  *
0005  * V4L2 file handles.
0006  *
0007  * Copyright (C) 2009--2010 Nokia Corporation.
0008  *
0009  * Contact: Sakari Ailus <sakari.ailus@iki.fi>
0010  */
0011 
0012 #include <linux/bitops.h>
0013 #include <linux/slab.h>
0014 #include <linux/export.h>
0015 #include <media/v4l2-dev.h>
0016 #include <media/v4l2-fh.h>
0017 #include <media/v4l2-event.h>
0018 #include <media/v4l2-ioctl.h>
0019 #include <media/v4l2-mc.h>
0020 
0021 void v4l2_fh_init(struct v4l2_fh *fh, struct video_device *vdev)
0022 {
0023     fh->vdev = vdev;
0024     /* Inherit from video_device. May be overridden by the driver. */
0025     fh->ctrl_handler = vdev->ctrl_handler;
0026     INIT_LIST_HEAD(&fh->list);
0027     set_bit(V4L2_FL_USES_V4L2_FH, &fh->vdev->flags);
0028     /*
0029      * determine_valid_ioctls() does not know if struct v4l2_fh
0030      * is used by this driver, but here we do. So enable the
0031      * prio ioctls here.
0032      */
0033     set_bit(_IOC_NR(VIDIOC_G_PRIORITY), vdev->valid_ioctls);
0034     set_bit(_IOC_NR(VIDIOC_S_PRIORITY), vdev->valid_ioctls);
0035     fh->prio = V4L2_PRIORITY_UNSET;
0036     init_waitqueue_head(&fh->wait);
0037     INIT_LIST_HEAD(&fh->available);
0038     INIT_LIST_HEAD(&fh->subscribed);
0039     fh->sequence = -1;
0040     mutex_init(&fh->subscribe_lock);
0041 }
0042 EXPORT_SYMBOL_GPL(v4l2_fh_init);
0043 
0044 void v4l2_fh_add(struct v4l2_fh *fh)
0045 {
0046     unsigned long flags;
0047 
0048     v4l2_prio_open(fh->vdev->prio, &fh->prio);
0049     spin_lock_irqsave(&fh->vdev->fh_lock, flags);
0050     list_add(&fh->list, &fh->vdev->fh_list);
0051     spin_unlock_irqrestore(&fh->vdev->fh_lock, flags);
0052 }
0053 EXPORT_SYMBOL_GPL(v4l2_fh_add);
0054 
0055 int v4l2_fh_open(struct file *filp)
0056 {
0057     struct video_device *vdev = video_devdata(filp);
0058     struct v4l2_fh *fh = kzalloc(sizeof(*fh), GFP_KERNEL);
0059 
0060     filp->private_data = fh;
0061     if (fh == NULL)
0062         return -ENOMEM;
0063     v4l2_fh_init(fh, vdev);
0064     v4l2_fh_add(fh);
0065     return 0;
0066 }
0067 EXPORT_SYMBOL_GPL(v4l2_fh_open);
0068 
0069 void v4l2_fh_del(struct v4l2_fh *fh)
0070 {
0071     unsigned long flags;
0072 
0073     spin_lock_irqsave(&fh->vdev->fh_lock, flags);
0074     list_del_init(&fh->list);
0075     spin_unlock_irqrestore(&fh->vdev->fh_lock, flags);
0076     v4l2_prio_close(fh->vdev->prio, fh->prio);
0077 }
0078 EXPORT_SYMBOL_GPL(v4l2_fh_del);
0079 
0080 void v4l2_fh_exit(struct v4l2_fh *fh)
0081 {
0082     if (fh->vdev == NULL)
0083         return;
0084     v4l_disable_media_source(fh->vdev);
0085     v4l2_event_unsubscribe_all(fh);
0086     mutex_destroy(&fh->subscribe_lock);
0087     fh->vdev = NULL;
0088 }
0089 EXPORT_SYMBOL_GPL(v4l2_fh_exit);
0090 
0091 int v4l2_fh_release(struct file *filp)
0092 {
0093     struct v4l2_fh *fh = filp->private_data;
0094 
0095     if (fh) {
0096         v4l2_fh_del(fh);
0097         v4l2_fh_exit(fh);
0098         kfree(fh);
0099         filp->private_data = NULL;
0100     }
0101     return 0;
0102 }
0103 EXPORT_SYMBOL_GPL(v4l2_fh_release);
0104 
0105 int v4l2_fh_is_singular(struct v4l2_fh *fh)
0106 {
0107     unsigned long flags;
0108     int is_singular;
0109 
0110     if (fh == NULL || fh->vdev == NULL)
0111         return 0;
0112     spin_lock_irqsave(&fh->vdev->fh_lock, flags);
0113     is_singular = list_is_singular(&fh->list);
0114     spin_unlock_irqrestore(&fh->vdev->fh_lock, flags);
0115     return is_singular;
0116 }
0117 EXPORT_SYMBOL_GPL(v4l2_fh_is_singular);