xref: /linux/drivers/media/v4l2-core/v4l2-subdev.c (revision 0cdee263bc5e7b20f657ea09f9272f50c568f35b)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * V4L2 sub-device
4  *
5  * Copyright (C) 2010 Nokia Corporation
6  *
7  * Contact: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
8  *	    Sakari Ailus <sakari.ailus@iki.fi>
9  */
10 
11 #include <linux/export.h>
12 #include <linux/ioctl.h>
13 #include <linux/leds.h>
14 #include <linux/mm.h>
15 #include <linux/module.h>
16 #include <linux/overflow.h>
17 #include <linux/slab.h>
18 #include <linux/string.h>
19 #include <linux/types.h>
20 #include <linux/version.h>
21 #include <linux/videodev2.h>
22 
23 #include <media/v4l2-ctrls.h>
24 #include <media/v4l2-device.h>
25 #include <media/v4l2-event.h>
26 #include <media/v4l2-fh.h>
27 #include <media/v4l2-ioctl.h>
28 
29 #if defined(CONFIG_VIDEO_V4L2_SUBDEV_API)
30 /*
31  * The Streams API is an experimental feature. To use the Streams API, set
32  * 'v4l2_subdev_enable_streams_api' to 1 below.
33  */
34 
35 static bool v4l2_subdev_enable_streams_api;
36 #endif
37 
38 /*
39  * Maximum stream ID is 63 for now, as we use u64 bitmask to represent a set
40  * of streams.
41  *
42  * Note that V4L2_FRAME_DESC_ENTRY_MAX is related: V4L2_FRAME_DESC_ENTRY_MAX
43  * restricts the total number of streams in a pad, although the stream ID is
44  * not restricted.
45  */
46 #define V4L2_SUBDEV_MAX_STREAM_ID 63
47 
48 #include "v4l2-subdev-priv.h"
49 
50 #if defined(CONFIG_VIDEO_V4L2_SUBDEV_API)
subdev_fh_init(struct v4l2_subdev_fh * fh,struct v4l2_subdev * sd)51 static int subdev_fh_init(struct v4l2_subdev_fh *fh, struct v4l2_subdev *sd)
52 {
53 	struct v4l2_subdev_state *state;
54 	static struct lock_class_key key;
55 
56 	state = __v4l2_subdev_state_alloc(sd, "fh->state->lock", &key);
57 	if (IS_ERR(state))
58 		return PTR_ERR(state);
59 
60 	fh->state = state;
61 
62 	return 0;
63 }
64 
subdev_fh_free(struct v4l2_subdev_fh * fh)65 static void subdev_fh_free(struct v4l2_subdev_fh *fh)
66 {
67 	__v4l2_subdev_state_free(fh->state);
68 	fh->state = NULL;
69 }
70 
subdev_open(struct file * file)71 static int subdev_open(struct file *file)
72 {
73 	struct video_device *vdev = video_devdata(file);
74 	struct v4l2_subdev *sd = vdev_to_v4l2_subdev(vdev);
75 	struct v4l2_subdev_fh *subdev_fh;
76 	int ret;
77 
78 	subdev_fh = kzalloc(sizeof(*subdev_fh), GFP_KERNEL);
79 	if (subdev_fh == NULL)
80 		return -ENOMEM;
81 
82 	ret = subdev_fh_init(subdev_fh, sd);
83 	if (ret) {
84 		kfree(subdev_fh);
85 		return ret;
86 	}
87 
88 	v4l2_fh_init(&subdev_fh->vfh, vdev);
89 	v4l2_fh_add(&subdev_fh->vfh);
90 	file->private_data = &subdev_fh->vfh;
91 
92 	if (sd->v4l2_dev->mdev && sd->entity.graph_obj.mdev->dev) {
93 		struct module *owner;
94 
95 		owner = sd->entity.graph_obj.mdev->dev->driver->owner;
96 		if (!try_module_get(owner)) {
97 			ret = -EBUSY;
98 			goto err;
99 		}
100 		subdev_fh->owner = owner;
101 	}
102 
103 	if (sd->internal_ops && sd->internal_ops->open) {
104 		ret = sd->internal_ops->open(sd, subdev_fh);
105 		if (ret < 0)
106 			goto err;
107 	}
108 
109 	return 0;
110 
111 err:
112 	module_put(subdev_fh->owner);
113 	v4l2_fh_del(&subdev_fh->vfh);
114 	v4l2_fh_exit(&subdev_fh->vfh);
115 	subdev_fh_free(subdev_fh);
116 	kfree(subdev_fh);
117 
118 	return ret;
119 }
120 
subdev_close(struct file * file)121 static int subdev_close(struct file *file)
122 {
123 	struct video_device *vdev = video_devdata(file);
124 	struct v4l2_subdev *sd = vdev_to_v4l2_subdev(vdev);
125 	struct v4l2_fh *vfh = file->private_data;
126 	struct v4l2_subdev_fh *subdev_fh = to_v4l2_subdev_fh(vfh);
127 
128 	if (sd->internal_ops && sd->internal_ops->close)
129 		sd->internal_ops->close(sd, subdev_fh);
130 	module_put(subdev_fh->owner);
131 	v4l2_fh_del(vfh);
132 	v4l2_fh_exit(vfh);
133 	subdev_fh_free(subdev_fh);
134 	kfree(subdev_fh);
135 	file->private_data = NULL;
136 
137 	return 0;
138 }
139 #else /* CONFIG_VIDEO_V4L2_SUBDEV_API */
subdev_open(struct file * file)140 static int subdev_open(struct file *file)
141 {
142 	return -ENODEV;
143 }
144 
subdev_close(struct file * file)145 static int subdev_close(struct file *file)
146 {
147 	return -ENODEV;
148 }
149 #endif /* CONFIG_VIDEO_V4L2_SUBDEV_API */
150 
v4l2_subdev_enable_privacy_led(struct v4l2_subdev * sd)151 static void v4l2_subdev_enable_privacy_led(struct v4l2_subdev *sd)
152 {
153 #if IS_REACHABLE(CONFIG_LEDS_CLASS)
154 	if (!IS_ERR_OR_NULL(sd->privacy_led))
155 		led_set_brightness(sd->privacy_led,
156 				   sd->privacy_led->max_brightness);
157 #endif
158 }
159 
v4l2_subdev_disable_privacy_led(struct v4l2_subdev * sd)160 static void v4l2_subdev_disable_privacy_led(struct v4l2_subdev *sd)
161 {
162 #if IS_REACHABLE(CONFIG_LEDS_CLASS)
163 	if (!IS_ERR_OR_NULL(sd->privacy_led))
164 		led_set_brightness(sd->privacy_led, 0);
165 #endif
166 }
167 
check_which(u32 which)168 static inline int check_which(u32 which)
169 {
170 	if (which != V4L2_SUBDEV_FORMAT_TRY &&
171 	    which != V4L2_SUBDEV_FORMAT_ACTIVE)
172 		return -EINVAL;
173 
174 	return 0;
175 }
176 
check_pad(struct v4l2_subdev * sd,u32 pad)177 static inline int check_pad(struct v4l2_subdev *sd, u32 pad)
178 {
179 #if defined(CONFIG_MEDIA_CONTROLLER)
180 	if (sd->entity.num_pads) {
181 		if (pad >= sd->entity.num_pads)
182 			return -EINVAL;
183 		return 0;
184 	}
185 #endif
186 	/* allow pad 0 on subdevices not registered as media entities */
187 	if (pad > 0)
188 		return -EINVAL;
189 	return 0;
190 }
191 
check_state(struct v4l2_subdev * sd,struct v4l2_subdev_state * state,u32 which,u32 pad,u32 stream)192 static int check_state(struct v4l2_subdev *sd, struct v4l2_subdev_state *state,
193 		       u32 which, u32 pad, u32 stream)
194 {
195 	if (sd->flags & V4L2_SUBDEV_FL_STREAMS) {
196 #if defined(CONFIG_VIDEO_V4L2_SUBDEV_API)
197 		if (!v4l2_subdev_state_get_format(state, pad, stream))
198 			return -EINVAL;
199 		return 0;
200 #else
201 		return -EINVAL;
202 #endif
203 	}
204 
205 	if (stream != 0)
206 		return -EINVAL;
207 
208 	if (which == V4L2_SUBDEV_FORMAT_TRY && (!state || !state->pads))
209 		return -EINVAL;
210 
211 	return 0;
212 }
213 
check_format(struct v4l2_subdev * sd,struct v4l2_subdev_state * state,struct v4l2_subdev_format * format)214 static inline int check_format(struct v4l2_subdev *sd,
215 			       struct v4l2_subdev_state *state,
216 			       struct v4l2_subdev_format *format)
217 {
218 	if (!format)
219 		return -EINVAL;
220 
221 	return check_which(format->which) ? : check_pad(sd, format->pad) ? :
222 	       check_state(sd, state, format->which, format->pad, format->stream);
223 }
224 
call_get_fmt(struct v4l2_subdev * sd,struct v4l2_subdev_state * state,struct v4l2_subdev_format * format)225 static int call_get_fmt(struct v4l2_subdev *sd,
226 			struct v4l2_subdev_state *state,
227 			struct v4l2_subdev_format *format)
228 {
229 	return check_format(sd, state, format) ? :
230 	       sd->ops->pad->get_fmt(sd, state, format);
231 }
232 
call_set_fmt(struct v4l2_subdev * sd,struct v4l2_subdev_state * state,struct v4l2_subdev_format * format)233 static int call_set_fmt(struct v4l2_subdev *sd,
234 			struct v4l2_subdev_state *state,
235 			struct v4l2_subdev_format *format)
236 {
237 	return check_format(sd, state, format) ? :
238 	       sd->ops->pad->set_fmt(sd, state, format);
239 }
240 
call_enum_mbus_code(struct v4l2_subdev * sd,struct v4l2_subdev_state * state,struct v4l2_subdev_mbus_code_enum * code)241 static int call_enum_mbus_code(struct v4l2_subdev *sd,
242 			       struct v4l2_subdev_state *state,
243 			       struct v4l2_subdev_mbus_code_enum *code)
244 {
245 	if (!code)
246 		return -EINVAL;
247 
248 	return check_which(code->which) ? : check_pad(sd, code->pad) ? :
249 	       check_state(sd, state, code->which, code->pad, code->stream) ? :
250 	       sd->ops->pad->enum_mbus_code(sd, state, code);
251 }
252 
call_enum_frame_size(struct v4l2_subdev * sd,struct v4l2_subdev_state * state,struct v4l2_subdev_frame_size_enum * fse)253 static int call_enum_frame_size(struct v4l2_subdev *sd,
254 				struct v4l2_subdev_state *state,
255 				struct v4l2_subdev_frame_size_enum *fse)
256 {
257 	if (!fse)
258 		return -EINVAL;
259 
260 	return check_which(fse->which) ? : check_pad(sd, fse->pad) ? :
261 	       check_state(sd, state, fse->which, fse->pad, fse->stream) ? :
262 	       sd->ops->pad->enum_frame_size(sd, state, fse);
263 }
264 
call_enum_frame_interval(struct v4l2_subdev * sd,struct v4l2_subdev_state * state,struct v4l2_subdev_frame_interval_enum * fie)265 static int call_enum_frame_interval(struct v4l2_subdev *sd,
266 				    struct v4l2_subdev_state *state,
267 				    struct v4l2_subdev_frame_interval_enum *fie)
268 {
269 	if (!fie)
270 		return -EINVAL;
271 
272 	return check_which(fie->which) ? : check_pad(sd, fie->pad) ? :
273 	       check_state(sd, state, fie->which, fie->pad, fie->stream) ? :
274 	       sd->ops->pad->enum_frame_interval(sd, state, fie);
275 }
276 
check_selection(struct v4l2_subdev * sd,struct v4l2_subdev_state * state,struct v4l2_subdev_selection * sel)277 static inline int check_selection(struct v4l2_subdev *sd,
278 				  struct v4l2_subdev_state *state,
279 				  struct v4l2_subdev_selection *sel)
280 {
281 	if (!sel)
282 		return -EINVAL;
283 
284 	return check_which(sel->which) ? : check_pad(sd, sel->pad) ? :
285 	       check_state(sd, state, sel->which, sel->pad, sel->stream);
286 }
287 
call_get_selection(struct v4l2_subdev * sd,struct v4l2_subdev_state * state,struct v4l2_subdev_selection * sel)288 static int call_get_selection(struct v4l2_subdev *sd,
289 			      struct v4l2_subdev_state *state,
290 			      struct v4l2_subdev_selection *sel)
291 {
292 	return check_selection(sd, state, sel) ? :
293 	       sd->ops->pad->get_selection(sd, state, sel);
294 }
295 
call_set_selection(struct v4l2_subdev * sd,struct v4l2_subdev_state * state,struct v4l2_subdev_selection * sel)296 static int call_set_selection(struct v4l2_subdev *sd,
297 			      struct v4l2_subdev_state *state,
298 			      struct v4l2_subdev_selection *sel)
299 {
300 	return check_selection(sd, state, sel) ? :
301 	       sd->ops->pad->set_selection(sd, state, sel);
302 }
303 
check_frame_interval(struct v4l2_subdev * sd,struct v4l2_subdev_state * state,struct v4l2_subdev_frame_interval * fi)304 static inline int check_frame_interval(struct v4l2_subdev *sd,
305 				       struct v4l2_subdev_state *state,
306 				       struct v4l2_subdev_frame_interval *fi)
307 {
308 	if (!fi)
309 		return -EINVAL;
310 
311 	return check_which(fi->which) ? : check_pad(sd, fi->pad) ? :
312 	       check_state(sd, state, fi->which, fi->pad, fi->stream);
313 }
314 
call_get_frame_interval(struct v4l2_subdev * sd,struct v4l2_subdev_state * state,struct v4l2_subdev_frame_interval * fi)315 static int call_get_frame_interval(struct v4l2_subdev *sd,
316 				   struct v4l2_subdev_state *state,
317 				   struct v4l2_subdev_frame_interval *fi)
318 {
319 	return check_frame_interval(sd, state, fi) ? :
320 	       sd->ops->pad->get_frame_interval(sd, state, fi);
321 }
322 
call_set_frame_interval(struct v4l2_subdev * sd,struct v4l2_subdev_state * state,struct v4l2_subdev_frame_interval * fi)323 static int call_set_frame_interval(struct v4l2_subdev *sd,
324 				   struct v4l2_subdev_state *state,
325 				   struct v4l2_subdev_frame_interval *fi)
326 {
327 	return check_frame_interval(sd, state, fi) ? :
328 	       sd->ops->pad->set_frame_interval(sd, state, fi);
329 }
330 
call_get_frame_desc(struct v4l2_subdev * sd,unsigned int pad,struct v4l2_mbus_frame_desc * fd)331 static int call_get_frame_desc(struct v4l2_subdev *sd, unsigned int pad,
332 			       struct v4l2_mbus_frame_desc *fd)
333 {
334 	unsigned int i;
335 	int ret;
336 
337 #if defined(CONFIG_MEDIA_CONTROLLER)
338 	if (!(sd->entity.pads[pad].flags & MEDIA_PAD_FL_SOURCE))
339 		return -EOPNOTSUPP;
340 #endif
341 
342 	memset(fd, 0, sizeof(*fd));
343 
344 	ret = sd->ops->pad->get_frame_desc(sd, pad, fd);
345 	if (ret)
346 		return ret;
347 
348 	dev_dbg(sd->dev, "Frame descriptor on pad %u, type %s\n", pad,
349 		fd->type == V4L2_MBUS_FRAME_DESC_TYPE_PARALLEL ? "parallel" :
350 		fd->type == V4L2_MBUS_FRAME_DESC_TYPE_CSI2 ? "CSI-2" :
351 		"unknown");
352 
353 	for (i = 0; i < fd->num_entries; i++) {
354 		struct v4l2_mbus_frame_desc_entry *entry = &fd->entry[i];
355 		char buf[20] = "";
356 
357 		if (fd->type == V4L2_MBUS_FRAME_DESC_TYPE_CSI2)
358 			WARN_ON(snprintf(buf, sizeof(buf),
359 					 ", vc %u, dt 0x%02x",
360 					 entry->bus.csi2.vc,
361 					 entry->bus.csi2.dt) >= sizeof(buf));
362 
363 		dev_dbg(sd->dev,
364 			"\tstream %u, code 0x%04x, length %u, flags 0x%04x%s\n",
365 			entry->stream, entry->pixelcode, entry->length,
366 			entry->flags, buf);
367 	}
368 
369 	return 0;
370 }
371 
check_edid(struct v4l2_subdev * sd,struct v4l2_subdev_edid * edid)372 static inline int check_edid(struct v4l2_subdev *sd,
373 			     struct v4l2_subdev_edid *edid)
374 {
375 	if (!edid)
376 		return -EINVAL;
377 
378 	if (edid->blocks && edid->edid == NULL)
379 		return -EINVAL;
380 
381 	return check_pad(sd, edid->pad);
382 }
383 
call_get_edid(struct v4l2_subdev * sd,struct v4l2_subdev_edid * edid)384 static int call_get_edid(struct v4l2_subdev *sd, struct v4l2_subdev_edid *edid)
385 {
386 	return check_edid(sd, edid) ? : sd->ops->pad->get_edid(sd, edid);
387 }
388 
call_set_edid(struct v4l2_subdev * sd,struct v4l2_subdev_edid * edid)389 static int call_set_edid(struct v4l2_subdev *sd, struct v4l2_subdev_edid *edid)
390 {
391 	return check_edid(sd, edid) ? : sd->ops->pad->set_edid(sd, edid);
392 }
393 
call_s_dv_timings(struct v4l2_subdev * sd,unsigned int pad,struct v4l2_dv_timings * timings)394 static int call_s_dv_timings(struct v4l2_subdev *sd, unsigned int pad,
395 			     struct v4l2_dv_timings *timings)
396 {
397 	if (!timings)
398 		return -EINVAL;
399 
400 	return check_pad(sd, pad) ? :
401 	       sd->ops->pad->s_dv_timings(sd, pad, timings);
402 }
403 
call_g_dv_timings(struct v4l2_subdev * sd,unsigned int pad,struct v4l2_dv_timings * timings)404 static int call_g_dv_timings(struct v4l2_subdev *sd, unsigned int pad,
405 			     struct v4l2_dv_timings *timings)
406 {
407 	if (!timings)
408 		return -EINVAL;
409 
410 	return check_pad(sd, pad) ? :
411 	       sd->ops->pad->g_dv_timings(sd, pad, timings);
412 }
413 
call_query_dv_timings(struct v4l2_subdev * sd,unsigned int pad,struct v4l2_dv_timings * timings)414 static int call_query_dv_timings(struct v4l2_subdev *sd, unsigned int pad,
415 				 struct v4l2_dv_timings *timings)
416 {
417 	if (!timings)
418 		return -EINVAL;
419 
420 	return check_pad(sd, pad) ? :
421 	       sd->ops->pad->query_dv_timings(sd, pad, timings);
422 }
423 
call_dv_timings_cap(struct v4l2_subdev * sd,struct v4l2_dv_timings_cap * cap)424 static int call_dv_timings_cap(struct v4l2_subdev *sd,
425 			       struct v4l2_dv_timings_cap *cap)
426 {
427 	if (!cap)
428 		return -EINVAL;
429 
430 	return check_pad(sd, cap->pad) ? :
431 	       sd->ops->pad->dv_timings_cap(sd, cap);
432 }
433 
call_enum_dv_timings(struct v4l2_subdev * sd,struct v4l2_enum_dv_timings * dvt)434 static int call_enum_dv_timings(struct v4l2_subdev *sd,
435 				struct v4l2_enum_dv_timings *dvt)
436 {
437 	if (!dvt)
438 		return -EINVAL;
439 
440 	return check_pad(sd, dvt->pad) ? :
441 	       sd->ops->pad->enum_dv_timings(sd, dvt);
442 }
443 
call_get_mbus_config(struct v4l2_subdev * sd,unsigned int pad,struct v4l2_mbus_config * config)444 static int call_get_mbus_config(struct v4l2_subdev *sd, unsigned int pad,
445 				struct v4l2_mbus_config *config)
446 {
447 	memset(config, 0, sizeof(*config));
448 
449 	return check_pad(sd, pad) ? :
450 	       sd->ops->pad->get_mbus_config(sd, pad, config);
451 }
452 
call_s_stream(struct v4l2_subdev * sd,int enable)453 static int call_s_stream(struct v4l2_subdev *sd, int enable)
454 {
455 	int ret;
456 
457 	/*
458 	 * The .s_stream() operation must never be called to start or stop an
459 	 * already started or stopped subdev. Catch offenders but don't return
460 	 * an error yet to avoid regressions.
461 	 */
462 	if (WARN_ON(sd->s_stream_enabled == !!enable))
463 		return 0;
464 
465 	ret = sd->ops->video->s_stream(sd, enable);
466 
467 	if (!enable && ret < 0) {
468 		dev_warn(sd->dev, "disabling streaming failed (%d)\n", ret);
469 		ret = 0;
470 	}
471 
472 	if (!ret) {
473 		sd->s_stream_enabled = enable;
474 
475 		if (enable)
476 			v4l2_subdev_enable_privacy_led(sd);
477 		else
478 			v4l2_subdev_disable_privacy_led(sd);
479 	}
480 
481 	return ret;
482 }
483 
484 #ifdef CONFIG_MEDIA_CONTROLLER
485 /*
486  * Create state-management wrapper for pad ops dealing with subdev state. The
487  * wrapper handles the case where the caller does not provide the called
488  * subdev's state. This should be removed when all the callers are fixed.
489  */
490 #define DEFINE_STATE_WRAPPER(f, arg_type)                                  \
491 	static int call_##f##_state(struct v4l2_subdev *sd,                \
492 				    struct v4l2_subdev_state *_state,      \
493 				    arg_type *arg)                         \
494 	{                                                                  \
495 		struct v4l2_subdev_state *state = _state;                  \
496 		int ret;                                                   \
497 		if (!_state)                                               \
498 			state = v4l2_subdev_lock_and_get_active_state(sd); \
499 		ret = call_##f(sd, state, arg);                            \
500 		if (!_state && state)                                      \
501 			v4l2_subdev_unlock_state(state);                   \
502 		return ret;                                                \
503 	}
504 
505 #else /* CONFIG_MEDIA_CONTROLLER */
506 
507 #define DEFINE_STATE_WRAPPER(f, arg_type)                            \
508 	static int call_##f##_state(struct v4l2_subdev *sd,          \
509 				    struct v4l2_subdev_state *state, \
510 				    arg_type *arg)                   \
511 	{                                                            \
512 		return call_##f(sd, state, arg);                     \
513 	}
514 
515 #endif /* CONFIG_MEDIA_CONTROLLER */
516 
517 DEFINE_STATE_WRAPPER(get_fmt, struct v4l2_subdev_format);
518 DEFINE_STATE_WRAPPER(set_fmt, struct v4l2_subdev_format);
519 DEFINE_STATE_WRAPPER(enum_mbus_code, struct v4l2_subdev_mbus_code_enum);
520 DEFINE_STATE_WRAPPER(enum_frame_size, struct v4l2_subdev_frame_size_enum);
521 DEFINE_STATE_WRAPPER(enum_frame_interval, struct v4l2_subdev_frame_interval_enum);
522 DEFINE_STATE_WRAPPER(get_selection, struct v4l2_subdev_selection);
523 DEFINE_STATE_WRAPPER(set_selection, struct v4l2_subdev_selection);
524 
525 static const struct v4l2_subdev_pad_ops v4l2_subdev_call_pad_wrappers = {
526 	.get_fmt		= call_get_fmt_state,
527 	.set_fmt		= call_set_fmt_state,
528 	.enum_mbus_code		= call_enum_mbus_code_state,
529 	.enum_frame_size	= call_enum_frame_size_state,
530 	.enum_frame_interval	= call_enum_frame_interval_state,
531 	.get_selection		= call_get_selection_state,
532 	.set_selection		= call_set_selection_state,
533 	.get_frame_interval	= call_get_frame_interval,
534 	.set_frame_interval	= call_set_frame_interval,
535 	.get_edid		= call_get_edid,
536 	.set_edid		= call_set_edid,
537 	.s_dv_timings		= call_s_dv_timings,
538 	.g_dv_timings		= call_g_dv_timings,
539 	.query_dv_timings	= call_query_dv_timings,
540 	.dv_timings_cap		= call_dv_timings_cap,
541 	.enum_dv_timings	= call_enum_dv_timings,
542 	.get_frame_desc		= call_get_frame_desc,
543 	.get_mbus_config	= call_get_mbus_config,
544 };
545 
546 static const struct v4l2_subdev_video_ops v4l2_subdev_call_video_wrappers = {
547 	.s_stream		= call_s_stream,
548 };
549 
550 const struct v4l2_subdev_ops v4l2_subdev_call_wrappers = {
551 	.pad	= &v4l2_subdev_call_pad_wrappers,
552 	.video	= &v4l2_subdev_call_video_wrappers,
553 };
554 EXPORT_SYMBOL(v4l2_subdev_call_wrappers);
555 
556 #if defined(CONFIG_VIDEO_V4L2_SUBDEV_API)
557 
558 static struct v4l2_subdev_state *
subdev_ioctl_get_state(struct v4l2_subdev * sd,struct v4l2_subdev_fh * subdev_fh,unsigned int cmd,void * arg)559 subdev_ioctl_get_state(struct v4l2_subdev *sd, struct v4l2_subdev_fh *subdev_fh,
560 		       unsigned int cmd, void *arg)
561 {
562 	u32 which;
563 
564 	switch (cmd) {
565 	default:
566 		return NULL;
567 	case VIDIOC_SUBDEV_G_FMT:
568 	case VIDIOC_SUBDEV_S_FMT:
569 		which = ((struct v4l2_subdev_format *)arg)->which;
570 		break;
571 	case VIDIOC_SUBDEV_G_CROP:
572 	case VIDIOC_SUBDEV_S_CROP:
573 		which = ((struct v4l2_subdev_crop *)arg)->which;
574 		break;
575 	case VIDIOC_SUBDEV_ENUM_MBUS_CODE:
576 		which = ((struct v4l2_subdev_mbus_code_enum *)arg)->which;
577 		break;
578 	case VIDIOC_SUBDEV_ENUM_FRAME_SIZE:
579 		which = ((struct v4l2_subdev_frame_size_enum *)arg)->which;
580 		break;
581 	case VIDIOC_SUBDEV_ENUM_FRAME_INTERVAL:
582 		which = ((struct v4l2_subdev_frame_interval_enum *)arg)->which;
583 		break;
584 	case VIDIOC_SUBDEV_G_SELECTION:
585 	case VIDIOC_SUBDEV_S_SELECTION:
586 		which = ((struct v4l2_subdev_selection *)arg)->which;
587 		break;
588 	case VIDIOC_SUBDEV_G_FRAME_INTERVAL:
589 	case VIDIOC_SUBDEV_S_FRAME_INTERVAL: {
590 		struct v4l2_subdev_frame_interval *fi = arg;
591 
592 		if (!(subdev_fh->client_caps &
593 		      V4L2_SUBDEV_CLIENT_CAP_INTERVAL_USES_WHICH))
594 			fi->which = V4L2_SUBDEV_FORMAT_ACTIVE;
595 
596 		which = fi->which;
597 		break;
598 	}
599 	case VIDIOC_SUBDEV_G_ROUTING:
600 	case VIDIOC_SUBDEV_S_ROUTING:
601 		which = ((struct v4l2_subdev_routing *)arg)->which;
602 		break;
603 	}
604 
605 	return which == V4L2_SUBDEV_FORMAT_TRY ?
606 			     subdev_fh->state :
607 			     v4l2_subdev_get_unlocked_active_state(sd);
608 }
609 
subdev_do_ioctl(struct file * file,unsigned int cmd,void * arg,struct v4l2_subdev_state * state)610 static long subdev_do_ioctl(struct file *file, unsigned int cmd, void *arg,
611 			    struct v4l2_subdev_state *state)
612 {
613 	struct video_device *vdev = video_devdata(file);
614 	struct v4l2_subdev *sd = vdev_to_v4l2_subdev(vdev);
615 	struct v4l2_fh *vfh = file->private_data;
616 	struct v4l2_subdev_fh *subdev_fh = to_v4l2_subdev_fh(vfh);
617 	bool ro_subdev = test_bit(V4L2_FL_SUBDEV_RO_DEVNODE, &vdev->flags);
618 	bool streams_subdev = sd->flags & V4L2_SUBDEV_FL_STREAMS;
619 	bool client_supports_streams = subdev_fh->client_caps &
620 				       V4L2_SUBDEV_CLIENT_CAP_STREAMS;
621 	int rval;
622 
623 	/*
624 	 * If the streams API is not enabled, remove V4L2_SUBDEV_CAP_STREAMS.
625 	 * Remove this when the API is no longer experimental.
626 	 */
627 	if (!v4l2_subdev_enable_streams_api)
628 		streams_subdev = false;
629 
630 	switch (cmd) {
631 	case VIDIOC_SUBDEV_QUERYCAP: {
632 		struct v4l2_subdev_capability *cap = arg;
633 
634 		memset(cap->reserved, 0, sizeof(cap->reserved));
635 		cap->version = LINUX_VERSION_CODE;
636 		cap->capabilities =
637 			(ro_subdev ? V4L2_SUBDEV_CAP_RO_SUBDEV : 0) |
638 			(streams_subdev ? V4L2_SUBDEV_CAP_STREAMS : 0);
639 
640 		return 0;
641 	}
642 
643 	case VIDIOC_QUERYCTRL:
644 		/*
645 		 * TODO: this really should be folded into v4l2_queryctrl (this
646 		 * currently returns -EINVAL for NULL control handlers).
647 		 * However, v4l2_queryctrl() is still called directly by
648 		 * drivers as well and until that has been addressed I believe
649 		 * it is safer to do the check here. The same is true for the
650 		 * other control ioctls below.
651 		 */
652 		if (!vfh->ctrl_handler)
653 			return -ENOTTY;
654 		return v4l2_queryctrl(vfh->ctrl_handler, arg);
655 
656 	case VIDIOC_QUERY_EXT_CTRL:
657 		if (!vfh->ctrl_handler)
658 			return -ENOTTY;
659 		return v4l2_query_ext_ctrl(vfh->ctrl_handler, arg);
660 
661 	case VIDIOC_QUERYMENU:
662 		if (!vfh->ctrl_handler)
663 			return -ENOTTY;
664 		return v4l2_querymenu(vfh->ctrl_handler, arg);
665 
666 	case VIDIOC_G_CTRL:
667 		if (!vfh->ctrl_handler)
668 			return -ENOTTY;
669 		return v4l2_g_ctrl(vfh->ctrl_handler, arg);
670 
671 	case VIDIOC_S_CTRL:
672 		if (!vfh->ctrl_handler)
673 			return -ENOTTY;
674 		return v4l2_s_ctrl(vfh, vfh->ctrl_handler, arg);
675 
676 	case VIDIOC_G_EXT_CTRLS:
677 		if (!vfh->ctrl_handler)
678 			return -ENOTTY;
679 		return v4l2_g_ext_ctrls(vfh->ctrl_handler,
680 					vdev, sd->v4l2_dev->mdev, arg);
681 
682 	case VIDIOC_S_EXT_CTRLS:
683 		if (!vfh->ctrl_handler)
684 			return -ENOTTY;
685 		return v4l2_s_ext_ctrls(vfh, vfh->ctrl_handler,
686 					vdev, sd->v4l2_dev->mdev, arg);
687 
688 	case VIDIOC_TRY_EXT_CTRLS:
689 		if (!vfh->ctrl_handler)
690 			return -ENOTTY;
691 		return v4l2_try_ext_ctrls(vfh->ctrl_handler,
692 					  vdev, sd->v4l2_dev->mdev, arg);
693 
694 	case VIDIOC_DQEVENT:
695 		if (!(sd->flags & V4L2_SUBDEV_FL_HAS_EVENTS))
696 			return -ENOIOCTLCMD;
697 
698 		return v4l2_event_dequeue(vfh, arg, file->f_flags & O_NONBLOCK);
699 
700 	case VIDIOC_SUBSCRIBE_EVENT:
701 		if (v4l2_subdev_has_op(sd, core, subscribe_event))
702 			return v4l2_subdev_call(sd, core, subscribe_event,
703 						vfh, arg);
704 
705 		if ((sd->flags & V4L2_SUBDEV_FL_HAS_EVENTS) &&
706 		    vfh->ctrl_handler)
707 			return v4l2_ctrl_subdev_subscribe_event(sd, vfh, arg);
708 
709 		return -ENOIOCTLCMD;
710 
711 	case VIDIOC_UNSUBSCRIBE_EVENT:
712 		if (v4l2_subdev_has_op(sd, core, unsubscribe_event))
713 			return v4l2_subdev_call(sd, core, unsubscribe_event,
714 						vfh, arg);
715 
716 		if (sd->flags & V4L2_SUBDEV_FL_HAS_EVENTS)
717 			return v4l2_event_subdev_unsubscribe(sd, vfh, arg);
718 
719 		return -ENOIOCTLCMD;
720 
721 #ifdef CONFIG_VIDEO_ADV_DEBUG
722 	case VIDIOC_DBG_G_REGISTER:
723 	{
724 		struct v4l2_dbg_register *p = arg;
725 
726 		if (!capable(CAP_SYS_ADMIN))
727 			return -EPERM;
728 		return v4l2_subdev_call(sd, core, g_register, p);
729 	}
730 	case VIDIOC_DBG_S_REGISTER:
731 	{
732 		struct v4l2_dbg_register *p = arg;
733 
734 		if (!capable(CAP_SYS_ADMIN))
735 			return -EPERM;
736 		return v4l2_subdev_call(sd, core, s_register, p);
737 	}
738 	case VIDIOC_DBG_G_CHIP_INFO:
739 	{
740 		struct v4l2_dbg_chip_info *p = arg;
741 
742 		if (p->match.type != V4L2_CHIP_MATCH_SUBDEV || p->match.addr)
743 			return -EINVAL;
744 		if (sd->ops->core && sd->ops->core->s_register)
745 			p->flags |= V4L2_CHIP_FL_WRITABLE;
746 		if (sd->ops->core && sd->ops->core->g_register)
747 			p->flags |= V4L2_CHIP_FL_READABLE;
748 		strscpy(p->name, sd->name, sizeof(p->name));
749 		return 0;
750 	}
751 #endif
752 
753 	case VIDIOC_LOG_STATUS: {
754 		int ret;
755 
756 		pr_info("%s: =================  START STATUS  =================\n",
757 			sd->name);
758 		ret = v4l2_subdev_call(sd, core, log_status);
759 		pr_info("%s: ==================  END STATUS  ==================\n",
760 			sd->name);
761 		return ret;
762 	}
763 
764 	case VIDIOC_SUBDEV_G_FMT: {
765 		struct v4l2_subdev_format *format = arg;
766 
767 		if (!client_supports_streams)
768 			format->stream = 0;
769 
770 		memset(format->reserved, 0, sizeof(format->reserved));
771 		memset(format->format.reserved, 0, sizeof(format->format.reserved));
772 		return v4l2_subdev_call(sd, pad, get_fmt, state, format);
773 	}
774 
775 	case VIDIOC_SUBDEV_S_FMT: {
776 		struct v4l2_subdev_format *format = arg;
777 
778 		if (format->which != V4L2_SUBDEV_FORMAT_TRY && ro_subdev)
779 			return -EPERM;
780 
781 		if (!client_supports_streams)
782 			format->stream = 0;
783 
784 		memset(format->reserved, 0, sizeof(format->reserved));
785 		memset(format->format.reserved, 0, sizeof(format->format.reserved));
786 		return v4l2_subdev_call(sd, pad, set_fmt, state, format);
787 	}
788 
789 	case VIDIOC_SUBDEV_G_CROP: {
790 		struct v4l2_subdev_crop *crop = arg;
791 		struct v4l2_subdev_selection sel;
792 
793 		if (!client_supports_streams)
794 			crop->stream = 0;
795 
796 		memset(crop->reserved, 0, sizeof(crop->reserved));
797 		memset(&sel, 0, sizeof(sel));
798 		sel.which = crop->which;
799 		sel.pad = crop->pad;
800 		sel.stream = crop->stream;
801 		sel.target = V4L2_SEL_TGT_CROP;
802 
803 		rval = v4l2_subdev_call(
804 			sd, pad, get_selection, state, &sel);
805 
806 		crop->rect = sel.r;
807 
808 		return rval;
809 	}
810 
811 	case VIDIOC_SUBDEV_S_CROP: {
812 		struct v4l2_subdev_crop *crop = arg;
813 		struct v4l2_subdev_selection sel;
814 
815 		if (crop->which != V4L2_SUBDEV_FORMAT_TRY && ro_subdev)
816 			return -EPERM;
817 
818 		if (!client_supports_streams)
819 			crop->stream = 0;
820 
821 		memset(crop->reserved, 0, sizeof(crop->reserved));
822 		memset(&sel, 0, sizeof(sel));
823 		sel.which = crop->which;
824 		sel.pad = crop->pad;
825 		sel.stream = crop->stream;
826 		sel.target = V4L2_SEL_TGT_CROP;
827 		sel.r = crop->rect;
828 
829 		rval = v4l2_subdev_call(
830 			sd, pad, set_selection, state, &sel);
831 
832 		crop->rect = sel.r;
833 
834 		return rval;
835 	}
836 
837 	case VIDIOC_SUBDEV_ENUM_MBUS_CODE: {
838 		struct v4l2_subdev_mbus_code_enum *code = arg;
839 
840 		if (!client_supports_streams)
841 			code->stream = 0;
842 
843 		memset(code->reserved, 0, sizeof(code->reserved));
844 		return v4l2_subdev_call(sd, pad, enum_mbus_code, state,
845 					code);
846 	}
847 
848 	case VIDIOC_SUBDEV_ENUM_FRAME_SIZE: {
849 		struct v4l2_subdev_frame_size_enum *fse = arg;
850 
851 		if (!client_supports_streams)
852 			fse->stream = 0;
853 
854 		memset(fse->reserved, 0, sizeof(fse->reserved));
855 		return v4l2_subdev_call(sd, pad, enum_frame_size, state,
856 					fse);
857 	}
858 
859 	case VIDIOC_SUBDEV_G_FRAME_INTERVAL: {
860 		struct v4l2_subdev_frame_interval *fi = arg;
861 
862 		if (!client_supports_streams)
863 			fi->stream = 0;
864 
865 		memset(fi->reserved, 0, sizeof(fi->reserved));
866 		return v4l2_subdev_call(sd, pad, get_frame_interval, state, fi);
867 	}
868 
869 	case VIDIOC_SUBDEV_S_FRAME_INTERVAL: {
870 		struct v4l2_subdev_frame_interval *fi = arg;
871 
872 		if (!client_supports_streams)
873 			fi->stream = 0;
874 
875 		if (fi->which != V4L2_SUBDEV_FORMAT_TRY && ro_subdev)
876 			return -EPERM;
877 
878 		memset(fi->reserved, 0, sizeof(fi->reserved));
879 		return v4l2_subdev_call(sd, pad, set_frame_interval, state, fi);
880 	}
881 
882 	case VIDIOC_SUBDEV_ENUM_FRAME_INTERVAL: {
883 		struct v4l2_subdev_frame_interval_enum *fie = arg;
884 
885 		if (!client_supports_streams)
886 			fie->stream = 0;
887 
888 		memset(fie->reserved, 0, sizeof(fie->reserved));
889 		return v4l2_subdev_call(sd, pad, enum_frame_interval, state,
890 					fie);
891 	}
892 
893 	case VIDIOC_SUBDEV_G_SELECTION: {
894 		struct v4l2_subdev_selection *sel = arg;
895 
896 		if (!client_supports_streams)
897 			sel->stream = 0;
898 
899 		memset(sel->reserved, 0, sizeof(sel->reserved));
900 		return v4l2_subdev_call(
901 			sd, pad, get_selection, state, sel);
902 	}
903 
904 	case VIDIOC_SUBDEV_S_SELECTION: {
905 		struct v4l2_subdev_selection *sel = arg;
906 
907 		if (sel->which != V4L2_SUBDEV_FORMAT_TRY && ro_subdev)
908 			return -EPERM;
909 
910 		if (!client_supports_streams)
911 			sel->stream = 0;
912 
913 		memset(sel->reserved, 0, sizeof(sel->reserved));
914 		return v4l2_subdev_call(
915 			sd, pad, set_selection, state, sel);
916 	}
917 
918 	case VIDIOC_G_EDID: {
919 		struct v4l2_subdev_edid *edid = arg;
920 
921 		return v4l2_subdev_call(sd, pad, get_edid, edid);
922 	}
923 
924 	case VIDIOC_S_EDID: {
925 		struct v4l2_subdev_edid *edid = arg;
926 
927 		return v4l2_subdev_call(sd, pad, set_edid, edid);
928 	}
929 
930 	case VIDIOC_SUBDEV_DV_TIMINGS_CAP: {
931 		struct v4l2_dv_timings_cap *cap = arg;
932 
933 		return v4l2_subdev_call(sd, pad, dv_timings_cap, cap);
934 	}
935 
936 	case VIDIOC_SUBDEV_ENUM_DV_TIMINGS: {
937 		struct v4l2_enum_dv_timings *dvt = arg;
938 
939 		return v4l2_subdev_call(sd, pad, enum_dv_timings, dvt);
940 	}
941 
942 	case VIDIOC_SUBDEV_QUERY_DV_TIMINGS:
943 		return v4l2_subdev_call(sd, pad, query_dv_timings, 0, arg);
944 
945 	case VIDIOC_SUBDEV_G_DV_TIMINGS:
946 		return v4l2_subdev_call(sd, pad, g_dv_timings, 0, arg);
947 
948 	case VIDIOC_SUBDEV_S_DV_TIMINGS:
949 		if (ro_subdev)
950 			return -EPERM;
951 
952 		return v4l2_subdev_call(sd, pad, s_dv_timings, 0, arg);
953 
954 	case VIDIOC_SUBDEV_G_STD:
955 		return v4l2_subdev_call(sd, video, g_std, arg);
956 
957 	case VIDIOC_SUBDEV_S_STD: {
958 		v4l2_std_id *std = arg;
959 
960 		if (ro_subdev)
961 			return -EPERM;
962 
963 		return v4l2_subdev_call(sd, video, s_std, *std);
964 	}
965 
966 	case VIDIOC_SUBDEV_ENUMSTD: {
967 		struct v4l2_standard *p = arg;
968 		v4l2_std_id id;
969 
970 		if (v4l2_subdev_call(sd, video, g_tvnorms, &id))
971 			return -EINVAL;
972 
973 		return v4l_video_std_enumstd(p, id);
974 	}
975 
976 	case VIDIOC_SUBDEV_QUERYSTD:
977 		return v4l2_subdev_call(sd, video, querystd, arg);
978 
979 	case VIDIOC_SUBDEV_G_ROUTING: {
980 		struct v4l2_subdev_routing *routing = arg;
981 		struct v4l2_subdev_krouting *krouting;
982 
983 		if (!v4l2_subdev_enable_streams_api)
984 			return -ENOIOCTLCMD;
985 
986 		if (!(sd->flags & V4L2_SUBDEV_FL_STREAMS))
987 			return -ENOIOCTLCMD;
988 
989 		memset(routing->reserved, 0, sizeof(routing->reserved));
990 
991 		krouting = &state->routing;
992 
993 		memcpy((struct v4l2_subdev_route *)(uintptr_t)routing->routes,
994 		       krouting->routes,
995 		       min(krouting->num_routes, routing->len_routes) *
996 		       sizeof(*krouting->routes));
997 		routing->num_routes = krouting->num_routes;
998 
999 		return 0;
1000 	}
1001 
1002 	case VIDIOC_SUBDEV_S_ROUTING: {
1003 		struct v4l2_subdev_routing *routing = arg;
1004 		struct v4l2_subdev_route *routes =
1005 			(struct v4l2_subdev_route *)(uintptr_t)routing->routes;
1006 		struct v4l2_subdev_krouting krouting = {};
1007 		unsigned int num_active_routes = 0;
1008 		unsigned int i;
1009 
1010 		if (!v4l2_subdev_enable_streams_api)
1011 			return -ENOIOCTLCMD;
1012 
1013 		if (!(sd->flags & V4L2_SUBDEV_FL_STREAMS))
1014 			return -ENOIOCTLCMD;
1015 
1016 		if (routing->which != V4L2_SUBDEV_FORMAT_TRY && ro_subdev)
1017 			return -EPERM;
1018 
1019 		if (routing->num_routes > routing->len_routes)
1020 			return -EINVAL;
1021 
1022 		memset(routing->reserved, 0, sizeof(routing->reserved));
1023 
1024 		for (i = 0; i < routing->num_routes; ++i) {
1025 			const struct v4l2_subdev_route *route = &routes[i];
1026 			const struct media_pad *pads = sd->entity.pads;
1027 
1028 			if (route->sink_stream > V4L2_SUBDEV_MAX_STREAM_ID ||
1029 			    route->source_stream > V4L2_SUBDEV_MAX_STREAM_ID)
1030 				return -EINVAL;
1031 
1032 			if (route->sink_pad >= sd->entity.num_pads)
1033 				return -EINVAL;
1034 
1035 			if (!(pads[route->sink_pad].flags &
1036 			      MEDIA_PAD_FL_SINK))
1037 				return -EINVAL;
1038 
1039 			if (route->source_pad >= sd->entity.num_pads)
1040 				return -EINVAL;
1041 
1042 			if (!(pads[route->source_pad].flags &
1043 			      MEDIA_PAD_FL_SOURCE))
1044 				return -EINVAL;
1045 
1046 			if (route->flags & V4L2_SUBDEV_ROUTE_FL_ACTIVE)
1047 				num_active_routes++;
1048 		}
1049 
1050 		/*
1051 		 * Drivers that implement routing need to report a frame
1052 		 * descriptor accordingly, with up to one entry per route. Until
1053 		 * the frame descriptors entries get allocated dynamically,
1054 		 * limit the number of active routes to
1055 		 * V4L2_FRAME_DESC_ENTRY_MAX.
1056 		 */
1057 		if (num_active_routes > V4L2_FRAME_DESC_ENTRY_MAX)
1058 			return -E2BIG;
1059 
1060 		/*
1061 		 * If the driver doesn't support setting routing, just return
1062 		 * the routing table.
1063 		 */
1064 		if (!v4l2_subdev_has_op(sd, pad, set_routing)) {
1065 			memcpy((struct v4l2_subdev_route *)(uintptr_t)routing->routes,
1066 			       state->routing.routes,
1067 			       min(state->routing.num_routes, routing->len_routes) *
1068 			       sizeof(*state->routing.routes));
1069 			routing->num_routes = state->routing.num_routes;
1070 
1071 			return 0;
1072 		}
1073 
1074 		krouting.num_routes = routing->num_routes;
1075 		krouting.len_routes = routing->len_routes;
1076 		krouting.routes = routes;
1077 
1078 		rval = v4l2_subdev_call(sd, pad, set_routing, state,
1079 					routing->which, &krouting);
1080 		if (rval < 0)
1081 			return rval;
1082 
1083 		memcpy((struct v4l2_subdev_route *)(uintptr_t)routing->routes,
1084 		       state->routing.routes,
1085 		       min(state->routing.num_routes, routing->len_routes) *
1086 		       sizeof(*state->routing.routes));
1087 		routing->num_routes = state->routing.num_routes;
1088 
1089 		return 0;
1090 	}
1091 
1092 	case VIDIOC_SUBDEV_G_CLIENT_CAP: {
1093 		struct v4l2_subdev_client_capability *client_cap = arg;
1094 
1095 		client_cap->capabilities = subdev_fh->client_caps;
1096 
1097 		return 0;
1098 	}
1099 
1100 	case VIDIOC_SUBDEV_S_CLIENT_CAP: {
1101 		struct v4l2_subdev_client_capability *client_cap = arg;
1102 
1103 		/*
1104 		 * Clear V4L2_SUBDEV_CLIENT_CAP_STREAMS if streams API is not
1105 		 * enabled. Remove this when streams API is no longer
1106 		 * experimental.
1107 		 */
1108 		if (!v4l2_subdev_enable_streams_api)
1109 			client_cap->capabilities &= ~V4L2_SUBDEV_CLIENT_CAP_STREAMS;
1110 
1111 		/* Filter out unsupported capabilities */
1112 		client_cap->capabilities &= (V4L2_SUBDEV_CLIENT_CAP_STREAMS |
1113 					     V4L2_SUBDEV_CLIENT_CAP_INTERVAL_USES_WHICH);
1114 
1115 		subdev_fh->client_caps = client_cap->capabilities;
1116 
1117 		return 0;
1118 	}
1119 
1120 	default:
1121 		return v4l2_subdev_call(sd, core, ioctl, cmd, arg);
1122 	}
1123 
1124 	return 0;
1125 }
1126 
subdev_do_ioctl_lock(struct file * file,unsigned int cmd,void * arg)1127 static long subdev_do_ioctl_lock(struct file *file, unsigned int cmd, void *arg)
1128 {
1129 	struct video_device *vdev = video_devdata(file);
1130 	struct mutex *lock = vdev->lock;
1131 	long ret = -ENODEV;
1132 
1133 	if (lock && mutex_lock_interruptible(lock))
1134 		return -ERESTARTSYS;
1135 
1136 	if (video_is_registered(vdev)) {
1137 		struct v4l2_subdev *sd = vdev_to_v4l2_subdev(vdev);
1138 		struct v4l2_fh *vfh = file->private_data;
1139 		struct v4l2_subdev_fh *subdev_fh = to_v4l2_subdev_fh(vfh);
1140 		struct v4l2_subdev_state *state;
1141 
1142 		state = subdev_ioctl_get_state(sd, subdev_fh, cmd, arg);
1143 
1144 		if (state)
1145 			v4l2_subdev_lock_state(state);
1146 
1147 		ret = subdev_do_ioctl(file, cmd, arg, state);
1148 
1149 		if (state)
1150 			v4l2_subdev_unlock_state(state);
1151 	}
1152 
1153 	if (lock)
1154 		mutex_unlock(lock);
1155 	return ret;
1156 }
1157 
subdev_ioctl(struct file * file,unsigned int cmd,unsigned long arg)1158 static long subdev_ioctl(struct file *file, unsigned int cmd,
1159 	unsigned long arg)
1160 {
1161 	return video_usercopy(file, cmd, arg, subdev_do_ioctl_lock);
1162 }
1163 
1164 #ifdef CONFIG_COMPAT
subdev_compat_ioctl32(struct file * file,unsigned int cmd,unsigned long arg)1165 static long subdev_compat_ioctl32(struct file *file, unsigned int cmd,
1166 	unsigned long arg)
1167 {
1168 	struct video_device *vdev = video_devdata(file);
1169 	struct v4l2_subdev *sd = vdev_to_v4l2_subdev(vdev);
1170 
1171 	return v4l2_subdev_call(sd, core, compat_ioctl32, cmd, arg);
1172 }
1173 #endif
1174 
1175 #else /* CONFIG_VIDEO_V4L2_SUBDEV_API */
subdev_ioctl(struct file * file,unsigned int cmd,unsigned long arg)1176 static long subdev_ioctl(struct file *file, unsigned int cmd,
1177 			 unsigned long arg)
1178 {
1179 	return -ENODEV;
1180 }
1181 
1182 #ifdef CONFIG_COMPAT
subdev_compat_ioctl32(struct file * file,unsigned int cmd,unsigned long arg)1183 static long subdev_compat_ioctl32(struct file *file, unsigned int cmd,
1184 				  unsigned long arg)
1185 {
1186 	return -ENODEV;
1187 }
1188 #endif
1189 #endif /* CONFIG_VIDEO_V4L2_SUBDEV_API */
1190 
subdev_poll(struct file * file,poll_table * wait)1191 static __poll_t subdev_poll(struct file *file, poll_table *wait)
1192 {
1193 	struct video_device *vdev = video_devdata(file);
1194 	struct v4l2_subdev *sd = vdev_to_v4l2_subdev(vdev);
1195 	struct v4l2_fh *fh = file->private_data;
1196 
1197 	if (!(sd->flags & V4L2_SUBDEV_FL_HAS_EVENTS))
1198 		return EPOLLERR;
1199 
1200 	poll_wait(file, &fh->wait, wait);
1201 
1202 	if (v4l2_event_pending(fh))
1203 		return EPOLLPRI;
1204 
1205 	return 0;
1206 }
1207 
1208 const struct v4l2_file_operations v4l2_subdev_fops = {
1209 	.owner = THIS_MODULE,
1210 	.open = subdev_open,
1211 	.unlocked_ioctl = subdev_ioctl,
1212 #ifdef CONFIG_COMPAT
1213 	.compat_ioctl32 = subdev_compat_ioctl32,
1214 #endif
1215 	.release = subdev_close,
1216 	.poll = subdev_poll,
1217 };
1218 
1219 #ifdef CONFIG_MEDIA_CONTROLLER
1220 
v4l2_subdev_get_fwnode_pad_1_to_1(struct media_entity * entity,struct fwnode_endpoint * endpoint)1221 int v4l2_subdev_get_fwnode_pad_1_to_1(struct media_entity *entity,
1222 				      struct fwnode_endpoint *endpoint)
1223 {
1224 	struct fwnode_handle *fwnode;
1225 	struct v4l2_subdev *sd;
1226 
1227 	if (!is_media_entity_v4l2_subdev(entity))
1228 		return -EINVAL;
1229 
1230 	sd = media_entity_to_v4l2_subdev(entity);
1231 
1232 	fwnode = fwnode_graph_get_port_parent(endpoint->local_fwnode);
1233 	fwnode_handle_put(fwnode);
1234 
1235 	if (device_match_fwnode(sd->dev, fwnode))
1236 		return endpoint->port;
1237 
1238 	return -ENXIO;
1239 }
1240 EXPORT_SYMBOL_GPL(v4l2_subdev_get_fwnode_pad_1_to_1);
1241 
v4l2_subdev_link_validate_default(struct v4l2_subdev * sd,struct media_link * link,struct v4l2_subdev_format * source_fmt,struct v4l2_subdev_format * sink_fmt)1242 int v4l2_subdev_link_validate_default(struct v4l2_subdev *sd,
1243 				      struct media_link *link,
1244 				      struct v4l2_subdev_format *source_fmt,
1245 				      struct v4l2_subdev_format *sink_fmt)
1246 {
1247 	bool pass = true;
1248 
1249 	/* The width, height and code must match. */
1250 	if (source_fmt->format.width != sink_fmt->format.width) {
1251 		dev_dbg(sd->entity.graph_obj.mdev->dev,
1252 			"%s: width does not match (source %u, sink %u)\n",
1253 			__func__,
1254 			source_fmt->format.width, sink_fmt->format.width);
1255 		pass = false;
1256 	}
1257 
1258 	if (source_fmt->format.height != sink_fmt->format.height) {
1259 		dev_dbg(sd->entity.graph_obj.mdev->dev,
1260 			"%s: height does not match (source %u, sink %u)\n",
1261 			__func__,
1262 			source_fmt->format.height, sink_fmt->format.height);
1263 		pass = false;
1264 	}
1265 
1266 	if (source_fmt->format.code != sink_fmt->format.code) {
1267 		dev_dbg(sd->entity.graph_obj.mdev->dev,
1268 			"%s: media bus code does not match (source 0x%8.8x, sink 0x%8.8x)\n",
1269 			__func__,
1270 			source_fmt->format.code, sink_fmt->format.code);
1271 		pass = false;
1272 	}
1273 
1274 	/* The field order must match, or the sink field order must be NONE
1275 	 * to support interlaced hardware connected to bridges that support
1276 	 * progressive formats only.
1277 	 */
1278 	if (source_fmt->format.field != sink_fmt->format.field &&
1279 	    sink_fmt->format.field != V4L2_FIELD_NONE) {
1280 		dev_dbg(sd->entity.graph_obj.mdev->dev,
1281 			"%s: field does not match (source %u, sink %u)\n",
1282 			__func__,
1283 			source_fmt->format.field, sink_fmt->format.field);
1284 		pass = false;
1285 	}
1286 
1287 	if (pass)
1288 		return 0;
1289 
1290 	dev_dbg(sd->entity.graph_obj.mdev->dev,
1291 		"%s: link was \"%s\":%u -> \"%s\":%u\n", __func__,
1292 		link->source->entity->name, link->source->index,
1293 		link->sink->entity->name, link->sink->index);
1294 
1295 	return -EPIPE;
1296 }
1297 EXPORT_SYMBOL_GPL(v4l2_subdev_link_validate_default);
1298 
1299 static int
v4l2_subdev_link_validate_get_format(struct media_pad * pad,u32 stream,struct v4l2_subdev_format * fmt,bool states_locked)1300 v4l2_subdev_link_validate_get_format(struct media_pad *pad, u32 stream,
1301 				     struct v4l2_subdev_format *fmt,
1302 				     bool states_locked)
1303 {
1304 	struct v4l2_subdev_state *state;
1305 	struct v4l2_subdev *sd;
1306 	int ret;
1307 
1308 	sd = media_entity_to_v4l2_subdev(pad->entity);
1309 
1310 	fmt->which = V4L2_SUBDEV_FORMAT_ACTIVE;
1311 	fmt->pad = pad->index;
1312 	fmt->stream = stream;
1313 
1314 	if (states_locked)
1315 		state = v4l2_subdev_get_locked_active_state(sd);
1316 	else
1317 		state = v4l2_subdev_lock_and_get_active_state(sd);
1318 
1319 	ret = v4l2_subdev_call(sd, pad, get_fmt, state, fmt);
1320 
1321 	if (!states_locked && state)
1322 		v4l2_subdev_unlock_state(state);
1323 
1324 	return ret;
1325 }
1326 
1327 #if defined(CONFIG_VIDEO_V4L2_SUBDEV_API)
1328 
__v4l2_link_validate_get_streams(struct media_pad * pad,u64 * streams_mask,bool states_locked)1329 static void __v4l2_link_validate_get_streams(struct media_pad *pad,
1330 					     u64 *streams_mask,
1331 					     bool states_locked)
1332 {
1333 	struct v4l2_subdev_route *route;
1334 	struct v4l2_subdev_state *state;
1335 	struct v4l2_subdev *subdev;
1336 
1337 	subdev = media_entity_to_v4l2_subdev(pad->entity);
1338 
1339 	*streams_mask = 0;
1340 
1341 	if (states_locked)
1342 		state = v4l2_subdev_get_locked_active_state(subdev);
1343 	else
1344 		state = v4l2_subdev_lock_and_get_active_state(subdev);
1345 
1346 	if (WARN_ON(!state))
1347 		return;
1348 
1349 	for_each_active_route(&state->routing, route) {
1350 		u32 route_pad;
1351 		u32 route_stream;
1352 
1353 		if (pad->flags & MEDIA_PAD_FL_SOURCE) {
1354 			route_pad = route->source_pad;
1355 			route_stream = route->source_stream;
1356 		} else {
1357 			route_pad = route->sink_pad;
1358 			route_stream = route->sink_stream;
1359 		}
1360 
1361 		if (route_pad != pad->index)
1362 			continue;
1363 
1364 		*streams_mask |= BIT_ULL(route_stream);
1365 	}
1366 
1367 	if (!states_locked)
1368 		v4l2_subdev_unlock_state(state);
1369 }
1370 
1371 #endif /* CONFIG_VIDEO_V4L2_SUBDEV_API */
1372 
v4l2_link_validate_get_streams(struct media_pad * pad,u64 * streams_mask,bool states_locked)1373 static void v4l2_link_validate_get_streams(struct media_pad *pad,
1374 					   u64 *streams_mask,
1375 					   bool states_locked)
1376 {
1377 	struct v4l2_subdev *subdev = media_entity_to_v4l2_subdev(pad->entity);
1378 
1379 	if (!(subdev->flags & V4L2_SUBDEV_FL_STREAMS)) {
1380 		/* Non-streams subdevs have an implicit stream 0 */
1381 		*streams_mask = BIT_ULL(0);
1382 		return;
1383 	}
1384 
1385 #if defined(CONFIG_VIDEO_V4L2_SUBDEV_API)
1386 	__v4l2_link_validate_get_streams(pad, streams_mask, states_locked);
1387 #else
1388 	/* This shouldn't happen */
1389 	*streams_mask = 0;
1390 #endif
1391 }
1392 
v4l2_subdev_link_validate_locked(struct media_link * link,bool states_locked)1393 static int v4l2_subdev_link_validate_locked(struct media_link *link, bool states_locked)
1394 {
1395 	struct v4l2_subdev *sink_subdev =
1396 		media_entity_to_v4l2_subdev(link->sink->entity);
1397 	struct device *dev = sink_subdev->entity.graph_obj.mdev->dev;
1398 	u64 source_streams_mask;
1399 	u64 sink_streams_mask;
1400 	u64 dangling_sink_streams;
1401 	u32 stream;
1402 	int ret;
1403 
1404 	dev_dbg(dev, "validating link \"%s\":%u -> \"%s\":%u\n",
1405 		link->source->entity->name, link->source->index,
1406 		link->sink->entity->name, link->sink->index);
1407 
1408 	v4l2_link_validate_get_streams(link->source, &source_streams_mask, states_locked);
1409 	v4l2_link_validate_get_streams(link->sink, &sink_streams_mask, states_locked);
1410 
1411 	/*
1412 	 * It is ok to have more source streams than sink streams as extra
1413 	 * source streams can just be ignored by the receiver, but having extra
1414 	 * sink streams is an error as streams must have a source.
1415 	 */
1416 	dangling_sink_streams = (source_streams_mask ^ sink_streams_mask) &
1417 				sink_streams_mask;
1418 	if (dangling_sink_streams) {
1419 		dev_err(dev, "Dangling sink streams: mask %#llx\n",
1420 			dangling_sink_streams);
1421 		return -EINVAL;
1422 	}
1423 
1424 	/* Validate source and sink stream formats */
1425 
1426 	for (stream = 0; stream < sizeof(sink_streams_mask) * 8; ++stream) {
1427 		struct v4l2_subdev_format sink_fmt, source_fmt;
1428 
1429 		if (!(sink_streams_mask & BIT_ULL(stream)))
1430 			continue;
1431 
1432 		dev_dbg(dev, "validating stream \"%s\":%u:%u -> \"%s\":%u:%u\n",
1433 			link->source->entity->name, link->source->index, stream,
1434 			link->sink->entity->name, link->sink->index, stream);
1435 
1436 		ret = v4l2_subdev_link_validate_get_format(link->source, stream,
1437 							   &source_fmt, states_locked);
1438 		if (ret < 0) {
1439 			dev_dbg(dev,
1440 				"Failed to get format for \"%s\":%u:%u (but that's ok)\n",
1441 				link->source->entity->name, link->source->index,
1442 				stream);
1443 			continue;
1444 		}
1445 
1446 		ret = v4l2_subdev_link_validate_get_format(link->sink, stream,
1447 							   &sink_fmt, states_locked);
1448 		if (ret < 0) {
1449 			dev_dbg(dev,
1450 				"Failed to get format for \"%s\":%u:%u (but that's ok)\n",
1451 				link->sink->entity->name, link->sink->index,
1452 				stream);
1453 			continue;
1454 		}
1455 
1456 		/* TODO: add stream number to link_validate() */
1457 		ret = v4l2_subdev_call(sink_subdev, pad, link_validate, link,
1458 				       &source_fmt, &sink_fmt);
1459 		if (!ret)
1460 			continue;
1461 
1462 		if (ret != -ENOIOCTLCMD)
1463 			return ret;
1464 
1465 		ret = v4l2_subdev_link_validate_default(sink_subdev, link,
1466 							&source_fmt, &sink_fmt);
1467 
1468 		if (ret)
1469 			return ret;
1470 	}
1471 
1472 	return 0;
1473 }
1474 
v4l2_subdev_link_validate(struct media_link * link)1475 int v4l2_subdev_link_validate(struct media_link *link)
1476 {
1477 	struct v4l2_subdev *source_sd, *sink_sd;
1478 	struct v4l2_subdev_state *source_state, *sink_state;
1479 	bool states_locked;
1480 	int ret;
1481 
1482 	/*
1483 	 * Links are validated in the context of the sink entity. Usage of this
1484 	 * helper on a sink that is not a subdev is a clear driver bug.
1485 	 */
1486 	if (WARN_ON_ONCE(!is_media_entity_v4l2_subdev(link->sink->entity)))
1487 		return -EINVAL;
1488 
1489 	/*
1490 	 * If the source is a video device, delegate link validation to it. This
1491 	 * allows usage of this helper for subdev connected to a video output
1492 	 * device, provided that the driver implement the video output device's
1493 	 * .link_validate() operation.
1494 	 */
1495 	if (is_media_entity_v4l2_video_device(link->source->entity)) {
1496 		struct media_entity *source = link->source->entity;
1497 
1498 		if (!source->ops || !source->ops->link_validate) {
1499 			/*
1500 			 * Many existing drivers do not implement the required
1501 			 * .link_validate() operation for their video devices.
1502 			 * Print a warning to get the drivers fixed, and return
1503 			 * 0 to avoid breaking userspace. This should
1504 			 * eventually be turned into a WARN_ON() when all
1505 			 * drivers will have been fixed.
1506 			 */
1507 			pr_warn_once("video device '%s' does not implement .link_validate(), driver bug!\n",
1508 				     source->name);
1509 			return 0;
1510 		}
1511 
1512 		/*
1513 		 * Avoid infinite loops in case a video device incorrectly uses
1514 		 * this helper function as its .link_validate() handler.
1515 		 */
1516 		if (WARN_ON(source->ops->link_validate == v4l2_subdev_link_validate))
1517 			return -EINVAL;
1518 
1519 		return source->ops->link_validate(link);
1520 	}
1521 
1522 	/*
1523 	 * If the source is still not a subdev, usage of this helper is a clear
1524 	 * driver bug.
1525 	 */
1526 	if (WARN_ON(!is_media_entity_v4l2_subdev(link->source->entity)))
1527 		return -EINVAL;
1528 
1529 	sink_sd = media_entity_to_v4l2_subdev(link->sink->entity);
1530 	source_sd = media_entity_to_v4l2_subdev(link->source->entity);
1531 
1532 	sink_state = v4l2_subdev_get_unlocked_active_state(sink_sd);
1533 	source_state = v4l2_subdev_get_unlocked_active_state(source_sd);
1534 
1535 	states_locked = sink_state && source_state;
1536 
1537 	if (states_locked)
1538 		v4l2_subdev_lock_states(sink_state, source_state);
1539 
1540 	ret = v4l2_subdev_link_validate_locked(link, states_locked);
1541 
1542 	if (states_locked)
1543 		v4l2_subdev_unlock_states(sink_state, source_state);
1544 
1545 	return ret;
1546 }
1547 EXPORT_SYMBOL_GPL(v4l2_subdev_link_validate);
1548 
v4l2_subdev_has_pad_interdep(struct media_entity * entity,unsigned int pad0,unsigned int pad1)1549 bool v4l2_subdev_has_pad_interdep(struct media_entity *entity,
1550 				  unsigned int pad0, unsigned int pad1)
1551 {
1552 	struct v4l2_subdev *sd = media_entity_to_v4l2_subdev(entity);
1553 	struct v4l2_subdev_krouting *routing;
1554 	struct v4l2_subdev_state *state;
1555 	unsigned int i;
1556 
1557 	state = v4l2_subdev_lock_and_get_active_state(sd);
1558 
1559 	routing = &state->routing;
1560 
1561 	for (i = 0; i < routing->num_routes; ++i) {
1562 		struct v4l2_subdev_route *route = &routing->routes[i];
1563 
1564 		if (!(route->flags & V4L2_SUBDEV_ROUTE_FL_ACTIVE))
1565 			continue;
1566 
1567 		if ((route->sink_pad == pad0 && route->source_pad == pad1) ||
1568 		    (route->source_pad == pad0 && route->sink_pad == pad1)) {
1569 			v4l2_subdev_unlock_state(state);
1570 			return true;
1571 		}
1572 	}
1573 
1574 	v4l2_subdev_unlock_state(state);
1575 
1576 	return false;
1577 }
1578 EXPORT_SYMBOL_GPL(v4l2_subdev_has_pad_interdep);
1579 
1580 struct v4l2_subdev_state *
__v4l2_subdev_state_alloc(struct v4l2_subdev * sd,const char * lock_name,struct lock_class_key * lock_key)1581 __v4l2_subdev_state_alloc(struct v4l2_subdev *sd, const char *lock_name,
1582 			  struct lock_class_key *lock_key)
1583 {
1584 	struct v4l2_subdev_state *state;
1585 	int ret;
1586 
1587 	state = kzalloc(sizeof(*state), GFP_KERNEL);
1588 	if (!state)
1589 		return ERR_PTR(-ENOMEM);
1590 
1591 	__mutex_init(&state->_lock, lock_name, lock_key);
1592 	if (sd->state_lock)
1593 		state->lock = sd->state_lock;
1594 	else
1595 		state->lock = &state->_lock;
1596 
1597 	state->sd = sd;
1598 
1599 	/* Drivers that support streams do not need the legacy pad config */
1600 	if (!(sd->flags & V4L2_SUBDEV_FL_STREAMS) && sd->entity.num_pads) {
1601 		state->pads = kvcalloc(sd->entity.num_pads,
1602 				       sizeof(*state->pads), GFP_KERNEL);
1603 		if (!state->pads) {
1604 			ret = -ENOMEM;
1605 			goto err;
1606 		}
1607 	}
1608 
1609 	if (sd->internal_ops && sd->internal_ops->init_state) {
1610 		/*
1611 		 * There can be no race at this point, but we lock the state
1612 		 * anyway to satisfy lockdep checks.
1613 		 */
1614 		v4l2_subdev_lock_state(state);
1615 		ret = sd->internal_ops->init_state(sd, state);
1616 		v4l2_subdev_unlock_state(state);
1617 
1618 		if (ret)
1619 			goto err;
1620 	}
1621 
1622 	return state;
1623 
1624 err:
1625 	if (state && state->pads)
1626 		kvfree(state->pads);
1627 
1628 	kfree(state);
1629 
1630 	return ERR_PTR(ret);
1631 }
1632 EXPORT_SYMBOL_GPL(__v4l2_subdev_state_alloc);
1633 
__v4l2_subdev_state_free(struct v4l2_subdev_state * state)1634 void __v4l2_subdev_state_free(struct v4l2_subdev_state *state)
1635 {
1636 	if (!state)
1637 		return;
1638 
1639 	mutex_destroy(&state->_lock);
1640 
1641 	kfree(state->routing.routes);
1642 	kvfree(state->stream_configs.configs);
1643 	kvfree(state->pads);
1644 	kfree(state);
1645 }
1646 EXPORT_SYMBOL_GPL(__v4l2_subdev_state_free);
1647 
__v4l2_subdev_init_finalize(struct v4l2_subdev * sd,const char * name,struct lock_class_key * key)1648 int __v4l2_subdev_init_finalize(struct v4l2_subdev *sd, const char *name,
1649 				struct lock_class_key *key)
1650 {
1651 	struct v4l2_subdev_state *state;
1652 	struct device *dev = sd->dev;
1653 	bool has_disable_streams;
1654 	bool has_enable_streams;
1655 	bool has_s_stream;
1656 
1657 	/* Check that the subdevice implements the required features */
1658 
1659 	has_s_stream = v4l2_subdev_has_op(sd, video, s_stream);
1660 	has_enable_streams = v4l2_subdev_has_op(sd, pad, enable_streams);
1661 	has_disable_streams = v4l2_subdev_has_op(sd, pad, disable_streams);
1662 
1663 	if (has_enable_streams != has_disable_streams) {
1664 		dev_err(dev,
1665 			"subdev '%s' must implement both or neither of .enable_streams() and .disable_streams()\n",
1666 			sd->name);
1667 		return -EINVAL;
1668 	}
1669 
1670 	if (sd->flags & V4L2_SUBDEV_FL_STREAMS) {
1671 		if (has_s_stream && !has_enable_streams) {
1672 			dev_err(dev,
1673 				"subdev '%s' must implement .enable/disable_streams()\n",
1674 				sd->name);
1675 
1676 			return -EINVAL;
1677 		}
1678 	}
1679 
1680 	if (sd->ctrl_handler)
1681 		sd->flags |= V4L2_SUBDEV_FL_HAS_EVENTS;
1682 
1683 	state = __v4l2_subdev_state_alloc(sd, name, key);
1684 	if (IS_ERR(state))
1685 		return PTR_ERR(state);
1686 
1687 	sd->active_state = state;
1688 
1689 	return 0;
1690 }
1691 EXPORT_SYMBOL_GPL(__v4l2_subdev_init_finalize);
1692 
v4l2_subdev_cleanup(struct v4l2_subdev * sd)1693 void v4l2_subdev_cleanup(struct v4l2_subdev *sd)
1694 {
1695 	struct v4l2_async_subdev_endpoint *ase, *ase_tmp;
1696 
1697 	__v4l2_subdev_state_free(sd->active_state);
1698 	sd->active_state = NULL;
1699 
1700 	/* Uninitialised sub-device, bail out here. */
1701 	if (!sd->async_subdev_endpoint_list.next)
1702 		return;
1703 
1704 	list_for_each_entry_safe(ase, ase_tmp, &sd->async_subdev_endpoint_list,
1705 				 async_subdev_endpoint_entry) {
1706 		list_del(&ase->async_subdev_endpoint_entry);
1707 
1708 		kfree(ase);
1709 	}
1710 }
1711 EXPORT_SYMBOL_GPL(v4l2_subdev_cleanup);
1712 
1713 struct v4l2_mbus_framefmt *
__v4l2_subdev_state_get_format(struct v4l2_subdev_state * state,unsigned int pad,u32 stream)1714 __v4l2_subdev_state_get_format(struct v4l2_subdev_state *state,
1715 			       unsigned int pad, u32 stream)
1716 {
1717 	struct v4l2_subdev_stream_configs *stream_configs;
1718 	unsigned int i;
1719 
1720 	if (WARN_ON_ONCE(!state))
1721 		return NULL;
1722 
1723 	if (state->pads) {
1724 		if (stream)
1725 			return NULL;
1726 
1727 		if (pad >= state->sd->entity.num_pads)
1728 			return NULL;
1729 
1730 		return &state->pads[pad].format;
1731 	}
1732 
1733 	lockdep_assert_held(state->lock);
1734 
1735 	stream_configs = &state->stream_configs;
1736 
1737 	for (i = 0; i < stream_configs->num_configs; ++i) {
1738 		if (stream_configs->configs[i].pad == pad &&
1739 		    stream_configs->configs[i].stream == stream)
1740 			return &stream_configs->configs[i].fmt;
1741 	}
1742 
1743 	return NULL;
1744 }
1745 EXPORT_SYMBOL_GPL(__v4l2_subdev_state_get_format);
1746 
1747 struct v4l2_rect *
__v4l2_subdev_state_get_crop(struct v4l2_subdev_state * state,unsigned int pad,u32 stream)1748 __v4l2_subdev_state_get_crop(struct v4l2_subdev_state *state, unsigned int pad,
1749 			     u32 stream)
1750 {
1751 	struct v4l2_subdev_stream_configs *stream_configs;
1752 	unsigned int i;
1753 
1754 	if (WARN_ON_ONCE(!state))
1755 		return NULL;
1756 
1757 	if (state->pads) {
1758 		if (stream)
1759 			return NULL;
1760 
1761 		if (pad >= state->sd->entity.num_pads)
1762 			return NULL;
1763 
1764 		return &state->pads[pad].crop;
1765 	}
1766 
1767 	lockdep_assert_held(state->lock);
1768 
1769 	stream_configs = &state->stream_configs;
1770 
1771 	for (i = 0; i < stream_configs->num_configs; ++i) {
1772 		if (stream_configs->configs[i].pad == pad &&
1773 		    stream_configs->configs[i].stream == stream)
1774 			return &stream_configs->configs[i].crop;
1775 	}
1776 
1777 	return NULL;
1778 }
1779 EXPORT_SYMBOL_GPL(__v4l2_subdev_state_get_crop);
1780 
1781 struct v4l2_rect *
__v4l2_subdev_state_get_compose(struct v4l2_subdev_state * state,unsigned int pad,u32 stream)1782 __v4l2_subdev_state_get_compose(struct v4l2_subdev_state *state,
1783 				unsigned int pad, u32 stream)
1784 {
1785 	struct v4l2_subdev_stream_configs *stream_configs;
1786 	unsigned int i;
1787 
1788 	if (WARN_ON_ONCE(!state))
1789 		return NULL;
1790 
1791 	if (state->pads) {
1792 		if (stream)
1793 			return NULL;
1794 
1795 		if (pad >= state->sd->entity.num_pads)
1796 			return NULL;
1797 
1798 		return &state->pads[pad].compose;
1799 	}
1800 
1801 	lockdep_assert_held(state->lock);
1802 
1803 	stream_configs = &state->stream_configs;
1804 
1805 	for (i = 0; i < stream_configs->num_configs; ++i) {
1806 		if (stream_configs->configs[i].pad == pad &&
1807 		    stream_configs->configs[i].stream == stream)
1808 			return &stream_configs->configs[i].compose;
1809 	}
1810 
1811 	return NULL;
1812 }
1813 EXPORT_SYMBOL_GPL(__v4l2_subdev_state_get_compose);
1814 
1815 struct v4l2_fract *
__v4l2_subdev_state_get_interval(struct v4l2_subdev_state * state,unsigned int pad,u32 stream)1816 __v4l2_subdev_state_get_interval(struct v4l2_subdev_state *state,
1817 				 unsigned int pad, u32 stream)
1818 {
1819 	struct v4l2_subdev_stream_configs *stream_configs;
1820 	unsigned int i;
1821 
1822 	if (WARN_ON(!state))
1823 		return NULL;
1824 
1825 	lockdep_assert_held(state->lock);
1826 
1827 	if (state->pads) {
1828 		if (stream)
1829 			return NULL;
1830 
1831 		if (pad >= state->sd->entity.num_pads)
1832 			return NULL;
1833 
1834 		return &state->pads[pad].interval;
1835 	}
1836 
1837 	lockdep_assert_held(state->lock);
1838 
1839 	stream_configs = &state->stream_configs;
1840 
1841 	for (i = 0; i < stream_configs->num_configs; ++i) {
1842 		if (stream_configs->configs[i].pad == pad &&
1843 		    stream_configs->configs[i].stream == stream)
1844 			return &stream_configs->configs[i].interval;
1845 	}
1846 
1847 	return NULL;
1848 }
1849 EXPORT_SYMBOL_GPL(__v4l2_subdev_state_get_interval);
1850 
1851 #if defined(CONFIG_VIDEO_V4L2_SUBDEV_API)
1852 
1853 static int
v4l2_subdev_init_stream_configs(struct v4l2_subdev_stream_configs * stream_configs,const struct v4l2_subdev_krouting * routing)1854 v4l2_subdev_init_stream_configs(struct v4l2_subdev_stream_configs *stream_configs,
1855 				const struct v4l2_subdev_krouting *routing)
1856 {
1857 	struct v4l2_subdev_stream_configs new_configs = { 0 };
1858 	struct v4l2_subdev_route *route;
1859 	u32 idx;
1860 
1861 	/* Count number of formats needed */
1862 	for_each_active_route(routing, route) {
1863 		/*
1864 		 * Each route needs a format on both ends of the route.
1865 		 */
1866 		new_configs.num_configs += 2;
1867 	}
1868 
1869 	if (new_configs.num_configs) {
1870 		new_configs.configs = kvcalloc(new_configs.num_configs,
1871 					       sizeof(*new_configs.configs),
1872 					       GFP_KERNEL);
1873 
1874 		if (!new_configs.configs)
1875 			return -ENOMEM;
1876 	}
1877 
1878 	/*
1879 	 * Fill in the 'pad' and stream' value for each item in the array from
1880 	 * the routing table
1881 	 */
1882 	idx = 0;
1883 
1884 	for_each_active_route(routing, route) {
1885 		new_configs.configs[idx].pad = route->sink_pad;
1886 		new_configs.configs[idx].stream = route->sink_stream;
1887 
1888 		idx++;
1889 
1890 		new_configs.configs[idx].pad = route->source_pad;
1891 		new_configs.configs[idx].stream = route->source_stream;
1892 
1893 		idx++;
1894 	}
1895 
1896 	kvfree(stream_configs->configs);
1897 	*stream_configs = new_configs;
1898 
1899 	return 0;
1900 }
1901 
v4l2_subdev_get_fmt(struct v4l2_subdev * sd,struct v4l2_subdev_state * state,struct v4l2_subdev_format * format)1902 int v4l2_subdev_get_fmt(struct v4l2_subdev *sd, struct v4l2_subdev_state *state,
1903 			struct v4l2_subdev_format *format)
1904 {
1905 	struct v4l2_mbus_framefmt *fmt;
1906 
1907 	fmt = v4l2_subdev_state_get_format(state, format->pad, format->stream);
1908 	if (!fmt)
1909 		return -EINVAL;
1910 
1911 	format->format = *fmt;
1912 
1913 	return 0;
1914 }
1915 EXPORT_SYMBOL_GPL(v4l2_subdev_get_fmt);
1916 
v4l2_subdev_get_frame_interval(struct v4l2_subdev * sd,struct v4l2_subdev_state * state,struct v4l2_subdev_frame_interval * fi)1917 int v4l2_subdev_get_frame_interval(struct v4l2_subdev *sd,
1918 				   struct v4l2_subdev_state *state,
1919 				   struct v4l2_subdev_frame_interval *fi)
1920 {
1921 	struct v4l2_fract *interval;
1922 
1923 	interval = v4l2_subdev_state_get_interval(state, fi->pad, fi->stream);
1924 	if (!interval)
1925 		return -EINVAL;
1926 
1927 	fi->interval = *interval;
1928 
1929 	return 0;
1930 }
1931 EXPORT_SYMBOL_GPL(v4l2_subdev_get_frame_interval);
1932 
v4l2_subdev_set_routing(struct v4l2_subdev * sd,struct v4l2_subdev_state * state,const struct v4l2_subdev_krouting * routing)1933 int v4l2_subdev_set_routing(struct v4l2_subdev *sd,
1934 			    struct v4l2_subdev_state *state,
1935 			    const struct v4l2_subdev_krouting *routing)
1936 {
1937 	struct v4l2_subdev_krouting *dst = &state->routing;
1938 	const struct v4l2_subdev_krouting *src = routing;
1939 	struct v4l2_subdev_krouting new_routing = { 0 };
1940 	size_t bytes;
1941 	int r;
1942 
1943 	if (unlikely(check_mul_overflow((size_t)src->num_routes,
1944 					sizeof(*src->routes), &bytes)))
1945 		return -EOVERFLOW;
1946 
1947 	lockdep_assert_held(state->lock);
1948 
1949 	if (src->num_routes > 0) {
1950 		new_routing.routes = kmemdup(src->routes, bytes, GFP_KERNEL);
1951 		if (!new_routing.routes)
1952 			return -ENOMEM;
1953 	}
1954 
1955 	new_routing.num_routes = src->num_routes;
1956 
1957 	r = v4l2_subdev_init_stream_configs(&state->stream_configs,
1958 					    &new_routing);
1959 	if (r) {
1960 		kfree(new_routing.routes);
1961 		return r;
1962 	}
1963 
1964 	kfree(dst->routes);
1965 	*dst = new_routing;
1966 
1967 	return 0;
1968 }
1969 EXPORT_SYMBOL_GPL(v4l2_subdev_set_routing);
1970 
1971 struct v4l2_subdev_route *
__v4l2_subdev_next_active_route(const struct v4l2_subdev_krouting * routing,struct v4l2_subdev_route * route)1972 __v4l2_subdev_next_active_route(const struct v4l2_subdev_krouting *routing,
1973 				struct v4l2_subdev_route *route)
1974 {
1975 	if (route)
1976 		++route;
1977 	else
1978 		route = &routing->routes[0];
1979 
1980 	for (; route < routing->routes + routing->num_routes; ++route) {
1981 		if (!(route->flags & V4L2_SUBDEV_ROUTE_FL_ACTIVE))
1982 			continue;
1983 
1984 		return route;
1985 	}
1986 
1987 	return NULL;
1988 }
1989 EXPORT_SYMBOL_GPL(__v4l2_subdev_next_active_route);
1990 
v4l2_subdev_set_routing_with_fmt(struct v4l2_subdev * sd,struct v4l2_subdev_state * state,const struct v4l2_subdev_krouting * routing,const struct v4l2_mbus_framefmt * fmt)1991 int v4l2_subdev_set_routing_with_fmt(struct v4l2_subdev *sd,
1992 				     struct v4l2_subdev_state *state,
1993 				     const struct v4l2_subdev_krouting *routing,
1994 				     const struct v4l2_mbus_framefmt *fmt)
1995 {
1996 	struct v4l2_subdev_stream_configs *stream_configs;
1997 	unsigned int i;
1998 	int ret;
1999 
2000 	ret = v4l2_subdev_set_routing(sd, state, routing);
2001 	if (ret)
2002 		return ret;
2003 
2004 	stream_configs = &state->stream_configs;
2005 
2006 	for (i = 0; i < stream_configs->num_configs; ++i)
2007 		stream_configs->configs[i].fmt = *fmt;
2008 
2009 	return 0;
2010 }
2011 EXPORT_SYMBOL_GPL(v4l2_subdev_set_routing_with_fmt);
2012 
v4l2_subdev_routing_find_opposite_end(const struct v4l2_subdev_krouting * routing,u32 pad,u32 stream,u32 * other_pad,u32 * other_stream)2013 int v4l2_subdev_routing_find_opposite_end(const struct v4l2_subdev_krouting *routing,
2014 					  u32 pad, u32 stream, u32 *other_pad,
2015 					  u32 *other_stream)
2016 {
2017 	unsigned int i;
2018 
2019 	for (i = 0; i < routing->num_routes; ++i) {
2020 		struct v4l2_subdev_route *route = &routing->routes[i];
2021 
2022 		if (route->source_pad == pad &&
2023 		    route->source_stream == stream) {
2024 			if (other_pad)
2025 				*other_pad = route->sink_pad;
2026 			if (other_stream)
2027 				*other_stream = route->sink_stream;
2028 			return 0;
2029 		}
2030 
2031 		if (route->sink_pad == pad && route->sink_stream == stream) {
2032 			if (other_pad)
2033 				*other_pad = route->source_pad;
2034 			if (other_stream)
2035 				*other_stream = route->source_stream;
2036 			return 0;
2037 		}
2038 	}
2039 
2040 	return -EINVAL;
2041 }
2042 EXPORT_SYMBOL_GPL(v4l2_subdev_routing_find_opposite_end);
2043 
2044 struct v4l2_mbus_framefmt *
v4l2_subdev_state_get_opposite_stream_format(struct v4l2_subdev_state * state,u32 pad,u32 stream)2045 v4l2_subdev_state_get_opposite_stream_format(struct v4l2_subdev_state *state,
2046 					     u32 pad, u32 stream)
2047 {
2048 	u32 other_pad, other_stream;
2049 	int ret;
2050 
2051 	ret = v4l2_subdev_routing_find_opposite_end(&state->routing,
2052 						    pad, stream,
2053 						    &other_pad, &other_stream);
2054 	if (ret)
2055 		return NULL;
2056 
2057 	return v4l2_subdev_state_get_format(state, other_pad, other_stream);
2058 }
2059 EXPORT_SYMBOL_GPL(v4l2_subdev_state_get_opposite_stream_format);
2060 
v4l2_subdev_state_xlate_streams(const struct v4l2_subdev_state * state,u32 pad0,u32 pad1,u64 * streams)2061 u64 v4l2_subdev_state_xlate_streams(const struct v4l2_subdev_state *state,
2062 				    u32 pad0, u32 pad1, u64 *streams)
2063 {
2064 	const struct v4l2_subdev_krouting *routing = &state->routing;
2065 	struct v4l2_subdev_route *route;
2066 	u64 streams0 = 0;
2067 	u64 streams1 = 0;
2068 
2069 	for_each_active_route(routing, route) {
2070 		if (route->sink_pad == pad0 && route->source_pad == pad1 &&
2071 		    (*streams & BIT_ULL(route->sink_stream))) {
2072 			streams0 |= BIT_ULL(route->sink_stream);
2073 			streams1 |= BIT_ULL(route->source_stream);
2074 		}
2075 		if (route->source_pad == pad0 && route->sink_pad == pad1 &&
2076 		    (*streams & BIT_ULL(route->source_stream))) {
2077 			streams0 |= BIT_ULL(route->source_stream);
2078 			streams1 |= BIT_ULL(route->sink_stream);
2079 		}
2080 	}
2081 
2082 	*streams = streams0;
2083 	return streams1;
2084 }
2085 EXPORT_SYMBOL_GPL(v4l2_subdev_state_xlate_streams);
2086 
v4l2_subdev_routing_validate(struct v4l2_subdev * sd,const struct v4l2_subdev_krouting * routing,enum v4l2_subdev_routing_restriction disallow)2087 int v4l2_subdev_routing_validate(struct v4l2_subdev *sd,
2088 				 const struct v4l2_subdev_krouting *routing,
2089 				 enum v4l2_subdev_routing_restriction disallow)
2090 {
2091 	u32 *remote_pads = NULL;
2092 	unsigned int i, j;
2093 	int ret = -EINVAL;
2094 
2095 	if (disallow & (V4L2_SUBDEV_ROUTING_NO_STREAM_MIX |
2096 			V4L2_SUBDEV_ROUTING_NO_MULTIPLEXING)) {
2097 		remote_pads = kcalloc(sd->entity.num_pads, sizeof(*remote_pads),
2098 				      GFP_KERNEL);
2099 		if (!remote_pads)
2100 			return -ENOMEM;
2101 
2102 		for (i = 0; i < sd->entity.num_pads; ++i)
2103 			remote_pads[i] = U32_MAX;
2104 	}
2105 
2106 	for (i = 0; i < routing->num_routes; ++i) {
2107 		const struct v4l2_subdev_route *route = &routing->routes[i];
2108 
2109 		/* Validate the sink and source pad numbers. */
2110 		if (route->sink_pad >= sd->entity.num_pads ||
2111 		    !(sd->entity.pads[route->sink_pad].flags & MEDIA_PAD_FL_SINK)) {
2112 			dev_dbg(sd->dev, "route %u sink (%u) is not a sink pad\n",
2113 				i, route->sink_pad);
2114 			goto out;
2115 		}
2116 
2117 		if (route->source_pad >= sd->entity.num_pads ||
2118 		    !(sd->entity.pads[route->source_pad].flags & MEDIA_PAD_FL_SOURCE)) {
2119 			dev_dbg(sd->dev, "route %u source (%u) is not a source pad\n",
2120 				i, route->source_pad);
2121 			goto out;
2122 		}
2123 
2124 		/*
2125 		 * V4L2_SUBDEV_ROUTING_NO_SINK_STREAM_MIX: all streams from a
2126 		 * sink pad must be routed to a single source pad.
2127 		 */
2128 		if (disallow & V4L2_SUBDEV_ROUTING_NO_SINK_STREAM_MIX) {
2129 			if (remote_pads[route->sink_pad] != U32_MAX &&
2130 			    remote_pads[route->sink_pad] != route->source_pad) {
2131 				dev_dbg(sd->dev,
2132 					"route %u attempts to mix %s streams\n",
2133 					i, "sink");
2134 				goto out;
2135 			}
2136 		}
2137 
2138 		/*
2139 		 * V4L2_SUBDEV_ROUTING_NO_SOURCE_STREAM_MIX: all streams on a
2140 		 * source pad must originate from a single sink pad.
2141 		 */
2142 		if (disallow & V4L2_SUBDEV_ROUTING_NO_SOURCE_STREAM_MIX) {
2143 			if (remote_pads[route->source_pad] != U32_MAX &&
2144 			    remote_pads[route->source_pad] != route->sink_pad) {
2145 				dev_dbg(sd->dev,
2146 					"route %u attempts to mix %s streams\n",
2147 					i, "source");
2148 				goto out;
2149 			}
2150 		}
2151 
2152 		/*
2153 		 * V4L2_SUBDEV_ROUTING_NO_SINK_MULTIPLEXING: Pads on the sink
2154 		 * side can not do stream multiplexing, i.e. there can be only
2155 		 * a single stream in a sink pad.
2156 		 */
2157 		if (disallow & V4L2_SUBDEV_ROUTING_NO_SINK_MULTIPLEXING) {
2158 			if (remote_pads[route->sink_pad] != U32_MAX) {
2159 				dev_dbg(sd->dev,
2160 					"route %u attempts to multiplex on %s pad %u\n",
2161 					i, "sink", route->sink_pad);
2162 				goto out;
2163 			}
2164 		}
2165 
2166 		/*
2167 		 * V4L2_SUBDEV_ROUTING_NO_SOURCE_MULTIPLEXING: Pads on the
2168 		 * source side can not do stream multiplexing, i.e. there can
2169 		 * be only a single stream in a source pad.
2170 		 */
2171 		if (disallow & V4L2_SUBDEV_ROUTING_NO_SOURCE_MULTIPLEXING) {
2172 			if (remote_pads[route->source_pad] != U32_MAX) {
2173 				dev_dbg(sd->dev,
2174 					"route %u attempts to multiplex on %s pad %u\n",
2175 					i, "source", route->source_pad);
2176 				goto out;
2177 			}
2178 		}
2179 
2180 		if (remote_pads) {
2181 			remote_pads[route->sink_pad] = route->source_pad;
2182 			remote_pads[route->source_pad] = route->sink_pad;
2183 		}
2184 
2185 		for (j = i + 1; j < routing->num_routes; ++j) {
2186 			const struct v4l2_subdev_route *r = &routing->routes[j];
2187 
2188 			/*
2189 			 * V4L2_SUBDEV_ROUTING_NO_1_TO_N: No two routes can
2190 			 * originate from the same (sink) stream.
2191 			 */
2192 			if ((disallow & V4L2_SUBDEV_ROUTING_NO_1_TO_N) &&
2193 			    route->sink_pad == r->sink_pad &&
2194 			    route->sink_stream == r->sink_stream) {
2195 				dev_dbg(sd->dev,
2196 					"routes %u and %u originate from same sink (%u/%u)\n",
2197 					i, j, route->sink_pad,
2198 					route->sink_stream);
2199 				goto out;
2200 			}
2201 
2202 			/*
2203 			 * V4L2_SUBDEV_ROUTING_NO_N_TO_1: No two routes can end
2204 			 * at the same (source) stream.
2205 			 */
2206 			if ((disallow & V4L2_SUBDEV_ROUTING_NO_N_TO_1) &&
2207 			    route->source_pad == r->source_pad &&
2208 			    route->source_stream == r->source_stream) {
2209 				dev_dbg(sd->dev,
2210 					"routes %u and %u end at same source (%u/%u)\n",
2211 					i, j, route->source_pad,
2212 					route->source_stream);
2213 				goto out;
2214 			}
2215 		}
2216 	}
2217 
2218 	ret = 0;
2219 
2220 out:
2221 	kfree(remote_pads);
2222 	return ret;
2223 }
2224 EXPORT_SYMBOL_GPL(v4l2_subdev_routing_validate);
2225 
v4l2_subdev_collect_streams(struct v4l2_subdev * sd,struct v4l2_subdev_state * state,u32 pad,u64 streams_mask,u64 * found_streams,u64 * enabled_streams)2226 static void v4l2_subdev_collect_streams(struct v4l2_subdev *sd,
2227 					struct v4l2_subdev_state *state,
2228 					u32 pad, u64 streams_mask,
2229 					u64 *found_streams,
2230 					u64 *enabled_streams)
2231 {
2232 	if (!(sd->flags & V4L2_SUBDEV_FL_STREAMS)) {
2233 		*found_streams = BIT_ULL(0);
2234 		*enabled_streams =
2235 			(sd->enabled_pads & BIT_ULL(pad)) ? BIT_ULL(0) : 0;
2236 		dev_dbg(sd->dev,
2237 			"collect_streams: sub-device \"%s\" does not support streams\n",
2238 			sd->entity.name);
2239 		return;
2240 	}
2241 
2242 	*found_streams = 0;
2243 	*enabled_streams = 0;
2244 
2245 	for (unsigned int i = 0; i < state->stream_configs.num_configs; ++i) {
2246 		const struct v4l2_subdev_stream_config *cfg =
2247 			&state->stream_configs.configs[i];
2248 
2249 		if (cfg->pad != pad || !(streams_mask & BIT_ULL(cfg->stream)))
2250 			continue;
2251 
2252 		*found_streams |= BIT_ULL(cfg->stream);
2253 		if (cfg->enabled)
2254 			*enabled_streams |= BIT_ULL(cfg->stream);
2255 	}
2256 
2257 	dev_dbg(sd->dev,
2258 		"collect_streams: \"%s\":%u: found %#llx enabled %#llx\n",
2259 		sd->entity.name, pad, *found_streams, *enabled_streams);
2260 }
2261 
v4l2_subdev_set_streams_enabled(struct v4l2_subdev * sd,struct v4l2_subdev_state * state,u32 pad,u64 streams_mask,bool enabled)2262 static void v4l2_subdev_set_streams_enabled(struct v4l2_subdev *sd,
2263 					    struct v4l2_subdev_state *state,
2264 					    u32 pad, u64 streams_mask,
2265 					    bool enabled)
2266 {
2267 	if (!(sd->flags & V4L2_SUBDEV_FL_STREAMS)) {
2268 		if (enabled)
2269 			sd->enabled_pads |= BIT_ULL(pad);
2270 		else
2271 			sd->enabled_pads &= ~BIT_ULL(pad);
2272 		return;
2273 	}
2274 
2275 	for (unsigned int i = 0; i < state->stream_configs.num_configs; ++i) {
2276 		struct v4l2_subdev_stream_config *cfg =
2277 			&state->stream_configs.configs[i];
2278 
2279 		if (cfg->pad == pad && (streams_mask & BIT_ULL(cfg->stream)))
2280 			cfg->enabled = enabled;
2281 	}
2282 }
2283 
v4l2_subdev_enable_streams(struct v4l2_subdev * sd,u32 pad,u64 streams_mask)2284 int v4l2_subdev_enable_streams(struct v4l2_subdev *sd, u32 pad,
2285 			       u64 streams_mask)
2286 {
2287 	struct device *dev = sd->entity.graph_obj.mdev->dev;
2288 	struct v4l2_subdev_state *state;
2289 	bool already_streaming;
2290 	u64 enabled_streams;
2291 	u64 found_streams;
2292 	bool use_s_stream;
2293 	int ret;
2294 
2295 	dev_dbg(dev, "enable streams \"%s\":%u/%#llx\n", sd->entity.name, pad,
2296 		streams_mask);
2297 
2298 	/* A few basic sanity checks first. */
2299 	if (pad >= sd->entity.num_pads)
2300 		return -EINVAL;
2301 
2302 	if (!(sd->entity.pads[pad].flags & MEDIA_PAD_FL_SOURCE))
2303 		return -EOPNOTSUPP;
2304 
2305 	/*
2306 	 * We use a 64-bit bitmask for tracking enabled pads, so only subdevices
2307 	 * with 64 pads or less can be supported.
2308 	 */
2309 	if (pad >= sizeof(sd->enabled_pads) * BITS_PER_BYTE)
2310 		return -EOPNOTSUPP;
2311 
2312 	if (!streams_mask)
2313 		return 0;
2314 
2315 	/* Fallback on .s_stream() if .enable_streams() isn't available. */
2316 	use_s_stream = !v4l2_subdev_has_op(sd, pad, enable_streams);
2317 
2318 	if (!use_s_stream)
2319 		state = v4l2_subdev_lock_and_get_active_state(sd);
2320 	else
2321 		state = NULL;
2322 
2323 	/*
2324 	 * Verify that the requested streams exist and that they are not
2325 	 * already enabled.
2326 	 */
2327 
2328 	v4l2_subdev_collect_streams(sd, state, pad, streams_mask,
2329 				    &found_streams, &enabled_streams);
2330 
2331 	if (found_streams != streams_mask) {
2332 		dev_dbg(dev, "streams 0x%llx not found on %s:%u\n",
2333 			streams_mask & ~found_streams, sd->entity.name, pad);
2334 		ret = -EINVAL;
2335 		goto done;
2336 	}
2337 
2338 	if (enabled_streams) {
2339 		dev_dbg(dev, "streams 0x%llx already enabled on %s:%u\n",
2340 			enabled_streams, sd->entity.name, pad);
2341 		ret = -EALREADY;
2342 		goto done;
2343 	}
2344 
2345 	already_streaming = v4l2_subdev_is_streaming(sd);
2346 
2347 	if (!use_s_stream) {
2348 		/* Call the .enable_streams() operation. */
2349 		ret = v4l2_subdev_call(sd, pad, enable_streams, state, pad,
2350 				       streams_mask);
2351 	} else {
2352 		/* Start streaming when the first pad is enabled. */
2353 		if (!already_streaming)
2354 			ret = v4l2_subdev_call(sd, video, s_stream, 1);
2355 		else
2356 			ret = 0;
2357 	}
2358 
2359 	if (ret) {
2360 		dev_dbg(dev, "enable streams %u:%#llx failed: %d\n", pad,
2361 			streams_mask, ret);
2362 		goto done;
2363 	}
2364 
2365 	/* Mark the streams as enabled. */
2366 	v4l2_subdev_set_streams_enabled(sd, state, pad, streams_mask, true);
2367 
2368 	/*
2369 	 * TODO: When all the drivers have been changed to use
2370 	 * v4l2_subdev_enable_streams() and v4l2_subdev_disable_streams(),
2371 	 * instead of calling .s_stream() operation directly, we can remove
2372 	 * the privacy LED handling from call_s_stream() and do it here
2373 	 * for all cases.
2374 	 */
2375 	if (!use_s_stream && !already_streaming)
2376 		v4l2_subdev_enable_privacy_led(sd);
2377 
2378 done:
2379 	if (!use_s_stream)
2380 		v4l2_subdev_unlock_state(state);
2381 
2382 	return ret;
2383 }
2384 EXPORT_SYMBOL_GPL(v4l2_subdev_enable_streams);
2385 
v4l2_subdev_disable_streams(struct v4l2_subdev * sd,u32 pad,u64 streams_mask)2386 int v4l2_subdev_disable_streams(struct v4l2_subdev *sd, u32 pad,
2387 				u64 streams_mask)
2388 {
2389 	struct device *dev = sd->entity.graph_obj.mdev->dev;
2390 	struct v4l2_subdev_state *state;
2391 	u64 enabled_streams;
2392 	u64 found_streams;
2393 	bool use_s_stream;
2394 	int ret;
2395 
2396 	dev_dbg(dev, "disable streams \"%s\":%u/%#llx\n", sd->entity.name, pad,
2397 		streams_mask);
2398 
2399 	/* A few basic sanity checks first. */
2400 	if (pad >= sd->entity.num_pads)
2401 		return -EINVAL;
2402 
2403 	if (!(sd->entity.pads[pad].flags & MEDIA_PAD_FL_SOURCE))
2404 		return -EOPNOTSUPP;
2405 
2406 	/*
2407 	 * We use a 64-bit bitmask for tracking enabled pads, so only subdevices
2408 	 * with 64 pads or less can be supported.
2409 	 */
2410 	if (pad >= sizeof(sd->enabled_pads) * BITS_PER_BYTE)
2411 		return -EOPNOTSUPP;
2412 
2413 	if (!streams_mask)
2414 		return 0;
2415 
2416 	/* Fallback on .s_stream() if .disable_streams() isn't available. */
2417 	use_s_stream = !v4l2_subdev_has_op(sd, pad, disable_streams);
2418 
2419 	if (!use_s_stream)
2420 		state = v4l2_subdev_lock_and_get_active_state(sd);
2421 	else
2422 		state = NULL;
2423 
2424 	/*
2425 	 * Verify that the requested streams exist and that they are not
2426 	 * already disabled.
2427 	 */
2428 
2429 	v4l2_subdev_collect_streams(sd, state, pad, streams_mask,
2430 				    &found_streams, &enabled_streams);
2431 
2432 	if (found_streams != streams_mask) {
2433 		dev_dbg(dev, "streams 0x%llx not found on %s:%u\n",
2434 			streams_mask & ~found_streams, sd->entity.name, pad);
2435 		ret = -EINVAL;
2436 		goto done;
2437 	}
2438 
2439 	if (enabled_streams != streams_mask) {
2440 		dev_dbg(dev, "streams 0x%llx already disabled on %s:%u\n",
2441 			streams_mask & ~enabled_streams, sd->entity.name, pad);
2442 		ret = -EALREADY;
2443 		goto done;
2444 	}
2445 
2446 	if (!use_s_stream) {
2447 		/* Call the .disable_streams() operation. */
2448 		ret = v4l2_subdev_call(sd, pad, disable_streams, state, pad,
2449 				       streams_mask);
2450 	} else {
2451 		/* Stop streaming when the last streams are disabled. */
2452 
2453 		if (!(sd->enabled_pads & ~BIT_ULL(pad)))
2454 			ret = v4l2_subdev_call(sd, video, s_stream, 0);
2455 		else
2456 			ret = 0;
2457 	}
2458 
2459 	if (ret) {
2460 		dev_dbg(dev, "disable streams %u:%#llx failed: %d\n", pad,
2461 			streams_mask, ret);
2462 		goto done;
2463 	}
2464 
2465 	v4l2_subdev_set_streams_enabled(sd, state, pad, streams_mask, false);
2466 
2467 done:
2468 	if (!use_s_stream) {
2469 		if (!v4l2_subdev_is_streaming(sd))
2470 			v4l2_subdev_disable_privacy_led(sd);
2471 
2472 		v4l2_subdev_unlock_state(state);
2473 	}
2474 
2475 	return ret;
2476 }
2477 EXPORT_SYMBOL_GPL(v4l2_subdev_disable_streams);
2478 
v4l2_subdev_s_stream_helper(struct v4l2_subdev * sd,int enable)2479 int v4l2_subdev_s_stream_helper(struct v4l2_subdev *sd, int enable)
2480 {
2481 	struct v4l2_subdev_state *state;
2482 	struct v4l2_subdev_route *route;
2483 	struct media_pad *pad;
2484 	u64 source_mask = 0;
2485 	int pad_index = -1;
2486 
2487 	/*
2488 	 * Find the source pad. This helper is meant for subdevs that have a
2489 	 * single source pad, so failures shouldn't happen, but catch them
2490 	 * loudly nonetheless as they indicate a driver bug.
2491 	 */
2492 	media_entity_for_each_pad(&sd->entity, pad) {
2493 		if (pad->flags & MEDIA_PAD_FL_SOURCE) {
2494 			pad_index = pad->index;
2495 			break;
2496 		}
2497 	}
2498 
2499 	if (WARN_ON(pad_index == -1))
2500 		return -EINVAL;
2501 
2502 	if (sd->flags & V4L2_SUBDEV_FL_STREAMS) {
2503 		/*
2504 		 * As there's a single source pad, just collect all the source
2505 		 * streams.
2506 		 */
2507 		state = v4l2_subdev_lock_and_get_active_state(sd);
2508 
2509 		for_each_active_route(&state->routing, route)
2510 			source_mask |= BIT_ULL(route->source_stream);
2511 
2512 		v4l2_subdev_unlock_state(state);
2513 	} else {
2514 		/*
2515 		 * For non-streams subdevices, there's a single implicit stream
2516 		 * per pad.
2517 		 */
2518 		source_mask = BIT_ULL(0);
2519 	}
2520 
2521 	if (enable)
2522 		return v4l2_subdev_enable_streams(sd, pad_index, source_mask);
2523 	else
2524 		return v4l2_subdev_disable_streams(sd, pad_index, source_mask);
2525 }
2526 EXPORT_SYMBOL_GPL(v4l2_subdev_s_stream_helper);
2527 
2528 #endif /* CONFIG_VIDEO_V4L2_SUBDEV_API */
2529 
2530 #endif /* CONFIG_MEDIA_CONTROLLER */
2531 
v4l2_subdev_init(struct v4l2_subdev * sd,const struct v4l2_subdev_ops * ops)2532 void v4l2_subdev_init(struct v4l2_subdev *sd, const struct v4l2_subdev_ops *ops)
2533 {
2534 	INIT_LIST_HEAD(&sd->list);
2535 	BUG_ON(!ops);
2536 	sd->ops = ops;
2537 	sd->v4l2_dev = NULL;
2538 	sd->flags = 0;
2539 	sd->name[0] = '\0';
2540 	sd->grp_id = 0;
2541 	sd->dev_priv = NULL;
2542 	sd->host_priv = NULL;
2543 	sd->privacy_led = NULL;
2544 	INIT_LIST_HEAD(&sd->async_subdev_endpoint_list);
2545 #if defined(CONFIG_MEDIA_CONTROLLER)
2546 	sd->entity.name = sd->name;
2547 	sd->entity.obj_type = MEDIA_ENTITY_TYPE_V4L2_SUBDEV;
2548 	sd->entity.function = MEDIA_ENT_F_V4L2_SUBDEV_UNKNOWN;
2549 #endif
2550 }
2551 EXPORT_SYMBOL(v4l2_subdev_init);
2552 
v4l2_subdev_notify_event(struct v4l2_subdev * sd,const struct v4l2_event * ev)2553 void v4l2_subdev_notify_event(struct v4l2_subdev *sd,
2554 			      const struct v4l2_event *ev)
2555 {
2556 	v4l2_event_queue(sd->devnode, ev);
2557 	v4l2_subdev_notify(sd, V4L2_DEVICE_NOTIFY_EVENT, (void *)ev);
2558 }
2559 EXPORT_SYMBOL_GPL(v4l2_subdev_notify_event);
2560 
v4l2_subdev_is_streaming(struct v4l2_subdev * sd)2561 bool v4l2_subdev_is_streaming(struct v4l2_subdev *sd)
2562 {
2563 	struct v4l2_subdev_state *state;
2564 
2565 	if (!v4l2_subdev_has_op(sd, pad, enable_streams))
2566 		return sd->s_stream_enabled;
2567 
2568 	if (!(sd->flags & V4L2_SUBDEV_FL_STREAMS))
2569 		return !!sd->enabled_pads;
2570 
2571 	state = v4l2_subdev_get_locked_active_state(sd);
2572 
2573 	for (unsigned int i = 0; i < state->stream_configs.num_configs; ++i) {
2574 		const struct v4l2_subdev_stream_config *cfg;
2575 
2576 		cfg = &state->stream_configs.configs[i];
2577 
2578 		if (cfg->enabled)
2579 			return true;
2580 	}
2581 
2582 	return false;
2583 }
2584 EXPORT_SYMBOL_GPL(v4l2_subdev_is_streaming);
2585 
v4l2_subdev_get_privacy_led(struct v4l2_subdev * sd)2586 int v4l2_subdev_get_privacy_led(struct v4l2_subdev *sd)
2587 {
2588 #if IS_REACHABLE(CONFIG_LEDS_CLASS)
2589 	sd->privacy_led = led_get(sd->dev, "privacy-led");
2590 	if (IS_ERR(sd->privacy_led) && PTR_ERR(sd->privacy_led) != -ENOENT)
2591 		return dev_err_probe(sd->dev, PTR_ERR(sd->privacy_led),
2592 				     "getting privacy LED\n");
2593 
2594 	if (!IS_ERR_OR_NULL(sd->privacy_led)) {
2595 		mutex_lock(&sd->privacy_led->led_access);
2596 		led_sysfs_disable(sd->privacy_led);
2597 		led_trigger_remove(sd->privacy_led);
2598 		led_set_brightness(sd->privacy_led, 0);
2599 		mutex_unlock(&sd->privacy_led->led_access);
2600 	}
2601 #endif
2602 	return 0;
2603 }
2604 EXPORT_SYMBOL_GPL(v4l2_subdev_get_privacy_led);
2605 
v4l2_subdev_put_privacy_led(struct v4l2_subdev * sd)2606 void v4l2_subdev_put_privacy_led(struct v4l2_subdev *sd)
2607 {
2608 #if IS_REACHABLE(CONFIG_LEDS_CLASS)
2609 	if (!IS_ERR_OR_NULL(sd->privacy_led)) {
2610 		mutex_lock(&sd->privacy_led->led_access);
2611 		led_sysfs_enable(sd->privacy_led);
2612 		mutex_unlock(&sd->privacy_led->led_access);
2613 		led_put(sd->privacy_led);
2614 	}
2615 #endif
2616 }
2617 EXPORT_SYMBOL_GPL(v4l2_subdev_put_privacy_led);
2618