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