xref: /linux/drivers/media/v4l2-core/v4l2-fh.c (revision 07fdad3a93756b872da7b53647715c48d0f4a2d0)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * v4l2-fh.c
4  *
5  * V4L2 file handles.
6  *
7  * Copyright (C) 2009--2010 Nokia Corporation.
8  *
9  * Contact: Sakari Ailus <sakari.ailus@iki.fi>
10  */
11 
12 #include <linux/bitops.h>
13 #include <linux/slab.h>
14 #include <linux/export.h>
15 #include <media/v4l2-dev.h>
16 #include <media/v4l2-fh.h>
17 #include <media/v4l2-event.h>
18 #include <media/v4l2-ioctl.h>
19 #include <media/v4l2-mc.h>
20 
21 void v4l2_fh_init(struct v4l2_fh *fh, struct video_device *vdev)
22 {
23 	fh->vdev = vdev;
24 	/* Inherit from video_device. May be overridden by the driver. */
25 	fh->ctrl_handler = vdev->ctrl_handler;
26 	INIT_LIST_HEAD(&fh->list);
27 	set_bit(V4L2_FL_USES_V4L2_FH, &fh->vdev->flags);
28 	/*
29 	 * determine_valid_ioctls() does not know if struct v4l2_fh
30 	 * is used by this driver, but here we do. So enable the
31 	 * prio ioctls here.
32 	 */
33 	set_bit(_IOC_NR(VIDIOC_G_PRIORITY), vdev->valid_ioctls);
34 	set_bit(_IOC_NR(VIDIOC_S_PRIORITY), vdev->valid_ioctls);
35 	fh->prio = V4L2_PRIORITY_UNSET;
36 	init_waitqueue_head(&fh->wait);
37 	INIT_LIST_HEAD(&fh->available);
38 	INIT_LIST_HEAD(&fh->subscribed);
39 	fh->sequence = -1;
40 	mutex_init(&fh->subscribe_lock);
41 }
42 EXPORT_SYMBOL_GPL(v4l2_fh_init);
43 
44 void v4l2_fh_add(struct v4l2_fh *fh, struct file *filp)
45 {
46 	unsigned long flags;
47 
48 	filp->private_data = fh;
49 
50 	v4l2_prio_open(fh->vdev->prio, &fh->prio);
51 	spin_lock_irqsave(&fh->vdev->fh_lock, flags);
52 	list_add(&fh->list, &fh->vdev->fh_list);
53 	spin_unlock_irqrestore(&fh->vdev->fh_lock, flags);
54 }
55 EXPORT_SYMBOL_GPL(v4l2_fh_add);
56 
57 int v4l2_fh_open(struct file *filp)
58 {
59 	struct video_device *vdev = video_devdata(filp);
60 	struct v4l2_fh *fh = kzalloc(sizeof(*fh), GFP_KERNEL);
61 
62 	if (fh == NULL)
63 		return -ENOMEM;
64 	v4l2_fh_init(fh, vdev);
65 	v4l2_fh_add(fh, filp);
66 	return 0;
67 }
68 EXPORT_SYMBOL_GPL(v4l2_fh_open);
69 
70 void v4l2_fh_del(struct v4l2_fh *fh, struct file *filp)
71 {
72 	unsigned long flags;
73 
74 	spin_lock_irqsave(&fh->vdev->fh_lock, flags);
75 	list_del_init(&fh->list);
76 	spin_unlock_irqrestore(&fh->vdev->fh_lock, flags);
77 	v4l2_prio_close(fh->vdev->prio, fh->prio);
78 
79 	filp->private_data = NULL;
80 }
81 EXPORT_SYMBOL_GPL(v4l2_fh_del);
82 
83 void v4l2_fh_exit(struct v4l2_fh *fh)
84 {
85 	if (fh->vdev == NULL)
86 		return;
87 	v4l_disable_media_source(fh->vdev);
88 	v4l2_event_unsubscribe_all(fh);
89 	mutex_destroy(&fh->subscribe_lock);
90 	fh->vdev = NULL;
91 }
92 EXPORT_SYMBOL_GPL(v4l2_fh_exit);
93 
94 int v4l2_fh_release(struct file *filp)
95 {
96 	struct v4l2_fh *fh = file_to_v4l2_fh(filp);
97 
98 	if (fh) {
99 		v4l2_fh_del(fh, filp);
100 		v4l2_fh_exit(fh);
101 		kfree(fh);
102 	}
103 	return 0;
104 }
105 EXPORT_SYMBOL_GPL(v4l2_fh_release);
106 
107 int v4l2_fh_is_singular(struct v4l2_fh *fh)
108 {
109 	unsigned long flags;
110 	int is_singular;
111 
112 	if (fh == NULL || fh->vdev == NULL)
113 		return 0;
114 	spin_lock_irqsave(&fh->vdev->fh_lock, flags);
115 	is_singular = list_is_singular(&fh->list);
116 	spin_unlock_irqrestore(&fh->vdev->fh_lock, flags);
117 	return is_singular;
118 }
119 EXPORT_SYMBOL_GPL(v4l2_fh_is_singular);
120