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