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