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