47f2551ec78cc5cb3c797a486d92bbf58394aac4
[openwrt/staging/mans0n.git] /
1 From 90712c1a495c2aa4b10dd8127fdd7f1a0cd9ef00 Mon Sep 17 00:00:00 2001
2 From: Jacopo Mondi <jacopo@jmondi.org>
3 Date: Tue, 7 Apr 2020 17:21:57 +0200
4 Subject: [PATCH] media: v4l2-dev: Add
5 v4l2_device_register_ro_subdev_node()
6
7 Add to the V4L2 core a function to register device nodes for video
8 subdevices in read-only mode.
9
10 Registering a device node in read-only mode is useful to expose to
11 userspace the current sub-device configuration, without allowing
12 application to change it by using the V4L2 subdevice ioctls.
13
14 Acked-by: Sakari Ailus <sakari.ailus@linux.intel.com>
15 Signed-off-by: Jacopo Mondi <jacopo@jmondi.org>
16 ---
17 drivers/media/v4l2-core/v4l2-device.c | 7 ++--
18 drivers/media/v4l2-core/v4l2-subdev.c | 19 ++++++++++
19 include/media/v4l2-dev.h | 7 ++++
20 include/media/v4l2-device.h | 50 ++++++++++++++++++++++++---
21 4 files changed, 77 insertions(+), 6 deletions(-)
22
23 --- a/drivers/media/v4l2-core/v4l2-device.c
24 +++ b/drivers/media/v4l2-core/v4l2-device.c
25 @@ -189,7 +189,8 @@ static void v4l2_device_release_subdev_n
26 kfree(vdev);
27 }
28
29 -int v4l2_device_register_subdev_nodes(struct v4l2_device *v4l2_dev)
30 +int __v4l2_device_register_subdev_nodes(struct v4l2_device *v4l2_dev,
31 + bool read_only)
32 {
33 struct video_device *vdev;
34 struct v4l2_subdev *sd;
35 @@ -218,6 +219,8 @@ int v4l2_device_register_subdev_nodes(st
36 vdev->fops = &v4l2_subdev_fops;
37 vdev->release = v4l2_device_release_subdev_node;
38 vdev->ctrl_handler = sd->ctrl_handler;
39 + if (read_only)
40 + set_bit(V4L2_FL_SUBDEV_RO_DEVNODE, &vdev->flags);
41 err = __video_register_device(vdev, VFL_TYPE_SUBDEV, -1, 1,
42 sd->owner);
43 if (err < 0) {
44 @@ -255,7 +258,7 @@ clean_up:
45
46 return err;
47 }
48 -EXPORT_SYMBOL_GPL(v4l2_device_register_subdev_nodes);
49 +EXPORT_SYMBOL_GPL(__v4l2_device_register_subdev_nodes);
50
51 void v4l2_device_unregister_subdev(struct v4l2_subdev *sd)
52 {
53 --- a/drivers/media/v4l2-core/v4l2-subdev.c
54 +++ b/drivers/media/v4l2-core/v4l2-subdev.c
55 @@ -331,6 +331,7 @@ static long subdev_do_ioctl(struct file
56 struct v4l2_fh *vfh = file->private_data;
57 #if defined(CONFIG_VIDEO_V4L2_SUBDEV_API)
58 struct v4l2_subdev_fh *subdev_fh = to_v4l2_subdev_fh(vfh);
59 + bool ro_subdev = test_bit(V4L2_FL_SUBDEV_RO_DEVNODE, &vdev->flags);
60 int rval;
61 #endif
62
63 @@ -453,6 +454,9 @@ static long subdev_do_ioctl(struct file
64 case VIDIOC_SUBDEV_S_FMT: {
65 struct v4l2_subdev_format *format = arg;
66
67 + if (format->which != V4L2_SUBDEV_FORMAT_TRY && ro_subdev)
68 + return -EPERM;
69 +
70 memset(format->reserved, 0, sizeof(format->reserved));
71 memset(format->format.reserved, 0, sizeof(format->format.reserved));
72 return v4l2_subdev_call(sd, pad, set_fmt, subdev_fh->pad, format);
73 @@ -480,6 +484,9 @@ static long subdev_do_ioctl(struct file
74 struct v4l2_subdev_crop *crop = arg;
75 struct v4l2_subdev_selection sel;
76
77 + if (crop->which != V4L2_SUBDEV_FORMAT_TRY && ro_subdev)
78 + return -EPERM;
79 +
80 memset(crop->reserved, 0, sizeof(crop->reserved));
81 memset(&sel, 0, sizeof(sel));
82 sel.which = crop->which;
83 @@ -521,6 +528,9 @@ static long subdev_do_ioctl(struct file
84 case VIDIOC_SUBDEV_S_FRAME_INTERVAL: {
85 struct v4l2_subdev_frame_interval *fi = arg;
86
87 + if (ro_subdev)
88 + return -EPERM;
89 +
90 memset(fi->reserved, 0, sizeof(fi->reserved));
91 return v4l2_subdev_call(sd, video, s_frame_interval, arg);
92 }
93 @@ -544,6 +554,9 @@ static long subdev_do_ioctl(struct file
94 case VIDIOC_SUBDEV_S_SELECTION: {
95 struct v4l2_subdev_selection *sel = arg;
96
97 + if (sel->which != V4L2_SUBDEV_FORMAT_TRY && ro_subdev)
98 + return -EPERM;
99 +
100 memset(sel->reserved, 0, sizeof(sel->reserved));
101 return v4l2_subdev_call(
102 sd, pad, set_selection, subdev_fh->pad, sel);
103 @@ -580,6 +593,9 @@ static long subdev_do_ioctl(struct file
104 return v4l2_subdev_call(sd, video, g_dv_timings, arg);
105
106 case VIDIOC_SUBDEV_S_DV_TIMINGS:
107 + if (ro_subdev)
108 + return -EPERM;
109 +
110 return v4l2_subdev_call(sd, video, s_dv_timings, arg);
111
112 case VIDIOC_SUBDEV_G_STD:
113 @@ -588,6 +604,9 @@ static long subdev_do_ioctl(struct file
114 case VIDIOC_SUBDEV_S_STD: {
115 v4l2_std_id *std = arg;
116
117 + if (ro_subdev)
118 + return -EPERM;
119 +
120 return v4l2_subdev_call(sd, video, s_std, *std);
121 }
122
123 --- a/include/media/v4l2-dev.h
124 +++ b/include/media/v4l2-dev.h
125 @@ -82,11 +82,18 @@ struct v4l2_ctrl_handler;
126 * but the old crop API will still work as expected in order to preserve
127 * backwards compatibility.
128 * Never set this flag for new drivers.
129 + * @V4L2_FL_SUBDEV_RO_DEVNODE:
130 + * indicates that the video device node is registered in read-only mode.
131 + * The flag only applies to device nodes registered for sub-devices, it is
132 + * set by the core when the sub-devices device nodes are registered with
133 + * v4l2_device_register_ro_subdev_nodes() and used by the sub-device ioctl
134 + * handler to restrict access to some ioctl calls.
135 */
136 enum v4l2_video_device_flags {
137 V4L2_FL_REGISTERED = 0,
138 V4L2_FL_USES_V4L2_FH = 1,
139 V4L2_FL_QUIRK_INVERTED_CROP = 2,
140 + V4L2_FL_SUBDEV_RO_DEVNODE = 3,
141 };
142
143 /* Priority helper functions */
144 --- a/include/media/v4l2-device.h
145 +++ b/include/media/v4l2-device.h
146 @@ -174,14 +174,56 @@ int __must_check v4l2_device_register_su
147 void v4l2_device_unregister_subdev(struct v4l2_subdev *sd);
148
149 /**
150 - * v4l2_device_register_subdev_nodes - Registers device nodes for all subdevs
151 - * of the v4l2 device that are marked with
152 - * the %V4L2_SUBDEV_FL_HAS_DEVNODE flag.
153 + * __v4l2_device_register_ro_subdev_nodes - Registers device nodes for
154 + * all subdevs of the v4l2 device that are marked with the
155 + * %V4L2_SUBDEV_FL_HAS_DEVNODE flag.
156 *
157 * @v4l2_dev: pointer to struct v4l2_device
158 + * @read_only: subdevices read-only flag. True to register the subdevices
159 + * device nodes in read-only mode, false to allow full access to the
160 + * subdevice userspace API.
161 */
162 int __must_check
163 -v4l2_device_register_subdev_nodes(struct v4l2_device *v4l2_dev);
164 +__v4l2_device_register_subdev_nodes(struct v4l2_device *v4l2_dev,
165 + bool read_only);
166 +
167 +/**
168 + * v4l2_device_register_subdev_nodes - Registers subdevices device nodes with
169 + * unrestricted access to the subdevice userspace operations
170 + *
171 + * Internally calls __v4l2_device_register_subdev_nodes(). See its documentation
172 + * for more details.
173 + *
174 + * @v4l2_dev: pointer to struct v4l2_device
175 + */
176 +static inline int __must_check
177 +v4l2_device_register_subdev_nodes(struct v4l2_device *v4l2_dev)
178 +{
179 +#if defined(CONFIG_VIDEO_V4L2_SUBDEV_API)
180 + return __v4l2_device_register_subdev_nodes(v4l2_dev, false);
181 +#else
182 + return 0;
183 +#endif
184 +}
185 +
186 +/**
187 + * v4l2_device_register_ro_subdev_nodes - Registers subdevices device nodes
188 + * in read-only mode
189 + *
190 + * Internally calls __v4l2_device_register_subdev_nodes(). See its documentation
191 + * for more details.
192 + *
193 + * @v4l2_dev: pointer to struct v4l2_device
194 + */
195 +static inline int __must_check
196 +v4l2_device_register_ro_subdev_nodes(struct v4l2_device *v4l2_dev)
197 +{
198 +#if defined(CONFIG_VIDEO_V4L2_SUBDEV_API)
199 + return __v4l2_device_register_subdev_nodes(v4l2_dev, true);
200 +#else
201 + return 0;
202 +#endif
203 +}
204
205 /**
206 * v4l2_subdev_notify - Sends a notification to v4l2_device.