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