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