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