Merge tag 'trace-printf-v6.13' of git://git.kernel.org/pub/scm/linux/kernel/git/trace...
[drm/drm-misc.git] / drivers / media / v4l2-core / v4l2-subdev.c
blobcde1774c9098dd0bf2f1232bd3e929f4c9026612
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * V4L2 sub-device
5 * Copyright (C) 2010 Nokia Corporation
7 * Contact: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
8 * Sakari Ailus <sakari.ailus@iki.fi>
9 */
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>
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>
29 #if defined(CONFIG_VIDEO_V4L2_SUBDEV_API)
31 * The Streams API is an experimental feature. To use the Streams API, set
32 * 'v4l2_subdev_enable_streams_api' to 1 below.
35 static bool v4l2_subdev_enable_streams_api;
36 #endif
39 * Maximum stream ID is 63 for now, as we use u64 bitmask to represent a set
40 * of streams.
42 * Note that V4L2_FRAME_DESC_ENTRY_MAX is related: V4L2_FRAME_DESC_ENTRY_MAX
43 * restricts the total number of streams in a pad, although the stream ID is
44 * not restricted.
46 #define V4L2_SUBDEV_MAX_STREAM_ID 63
48 #include "v4l2-subdev-priv.h"
50 #if defined(CONFIG_VIDEO_V4L2_SUBDEV_API)
51 static int subdev_fh_init(struct v4l2_subdev_fh *fh, struct v4l2_subdev *sd)
53 struct v4l2_subdev_state *state;
54 static struct lock_class_key key;
56 state = __v4l2_subdev_state_alloc(sd, "fh->state->lock", &key);
57 if (IS_ERR(state))
58 return PTR_ERR(state);
60 fh->state = state;
62 return 0;
65 static void subdev_fh_free(struct v4l2_subdev_fh *fh)
67 __v4l2_subdev_state_free(fh->state);
68 fh->state = NULL;
71 static int subdev_open(struct file *file)
73 struct video_device *vdev = video_devdata(file);
74 struct v4l2_subdev *sd = vdev_to_v4l2_subdev(vdev);
75 struct v4l2_subdev_fh *subdev_fh;
76 int ret;
78 subdev_fh = kzalloc(sizeof(*subdev_fh), GFP_KERNEL);
79 if (subdev_fh == NULL)
80 return -ENOMEM;
82 ret = subdev_fh_init(subdev_fh, sd);
83 if (ret) {
84 kfree(subdev_fh);
85 return ret;
88 v4l2_fh_init(&subdev_fh->vfh, vdev);
89 v4l2_fh_add(&subdev_fh->vfh);
90 file->private_data = &subdev_fh->vfh;
92 if (sd->v4l2_dev->mdev && sd->entity.graph_obj.mdev->dev) {
93 struct module *owner;
95 owner = sd->entity.graph_obj.mdev->dev->driver->owner;
96 if (!try_module_get(owner)) {
97 ret = -EBUSY;
98 goto err;
100 subdev_fh->owner = owner;
103 if (sd->internal_ops && sd->internal_ops->open) {
104 ret = sd->internal_ops->open(sd, subdev_fh);
105 if (ret < 0)
106 goto err;
109 return 0;
111 err:
112 module_put(subdev_fh->owner);
113 v4l2_fh_del(&subdev_fh->vfh);
114 v4l2_fh_exit(&subdev_fh->vfh);
115 subdev_fh_free(subdev_fh);
116 kfree(subdev_fh);
118 return ret;
121 static int subdev_close(struct file *file)
123 struct video_device *vdev = video_devdata(file);
124 struct v4l2_subdev *sd = vdev_to_v4l2_subdev(vdev);
125 struct v4l2_fh *vfh = file->private_data;
126 struct v4l2_subdev_fh *subdev_fh = to_v4l2_subdev_fh(vfh);
128 if (sd->internal_ops && sd->internal_ops->close)
129 sd->internal_ops->close(sd, subdev_fh);
130 module_put(subdev_fh->owner);
131 v4l2_fh_del(vfh);
132 v4l2_fh_exit(vfh);
133 subdev_fh_free(subdev_fh);
134 kfree(subdev_fh);
135 file->private_data = NULL;
137 return 0;
139 #else /* CONFIG_VIDEO_V4L2_SUBDEV_API */
140 static int subdev_open(struct file *file)
142 return -ENODEV;
145 static int subdev_close(struct file *file)
147 return -ENODEV;
149 #endif /* CONFIG_VIDEO_V4L2_SUBDEV_API */
151 static void v4l2_subdev_enable_privacy_led(struct v4l2_subdev *sd)
153 #if IS_REACHABLE(CONFIG_LEDS_CLASS)
154 if (!IS_ERR_OR_NULL(sd->privacy_led))
155 led_set_brightness(sd->privacy_led,
156 sd->privacy_led->max_brightness);
157 #endif
160 static void v4l2_subdev_disable_privacy_led(struct v4l2_subdev *sd)
162 #if IS_REACHABLE(CONFIG_LEDS_CLASS)
163 if (!IS_ERR_OR_NULL(sd->privacy_led))
164 led_set_brightness(sd->privacy_led, 0);
165 #endif
168 static inline int check_which(u32 which)
170 if (which != V4L2_SUBDEV_FORMAT_TRY &&
171 which != V4L2_SUBDEV_FORMAT_ACTIVE)
172 return -EINVAL;
174 return 0;
177 static inline int check_pad(struct v4l2_subdev *sd, u32 pad)
179 #if defined(CONFIG_MEDIA_CONTROLLER)
180 if (sd->entity.num_pads) {
181 if (pad >= sd->entity.num_pads)
182 return -EINVAL;
183 return 0;
185 #endif
186 /* allow pad 0 on subdevices not registered as media entities */
187 if (pad > 0)
188 return -EINVAL;
189 return 0;
192 static int check_state(struct v4l2_subdev *sd, struct v4l2_subdev_state *state,
193 u32 which, u32 pad, u32 stream)
195 if (sd->flags & V4L2_SUBDEV_FL_STREAMS) {
196 #if defined(CONFIG_VIDEO_V4L2_SUBDEV_API)
197 if (!v4l2_subdev_state_get_format(state, pad, stream))
198 return -EINVAL;
199 return 0;
200 #else
201 return -EINVAL;
202 #endif
205 if (stream != 0)
206 return -EINVAL;
208 if (which == V4L2_SUBDEV_FORMAT_TRY && (!state || !state->pads))
209 return -EINVAL;
211 return 0;
214 static inline int check_format(struct v4l2_subdev *sd,
215 struct v4l2_subdev_state *state,
216 struct v4l2_subdev_format *format)
218 if (!format)
219 return -EINVAL;
221 return check_which(format->which) ? : check_pad(sd, format->pad) ? :
222 check_state(sd, state, format->which, format->pad, format->stream);
225 static int call_get_fmt(struct v4l2_subdev *sd,
226 struct v4l2_subdev_state *state,
227 struct v4l2_subdev_format *format)
229 return check_format(sd, state, format) ? :
230 sd->ops->pad->get_fmt(sd, state, format);
233 static int call_set_fmt(struct v4l2_subdev *sd,
234 struct v4l2_subdev_state *state,
235 struct v4l2_subdev_format *format)
237 return check_format(sd, state, format) ? :
238 sd->ops->pad->set_fmt(sd, state, format);
241 static int call_enum_mbus_code(struct v4l2_subdev *sd,
242 struct v4l2_subdev_state *state,
243 struct v4l2_subdev_mbus_code_enum *code)
245 if (!code)
246 return -EINVAL;
248 return check_which(code->which) ? : check_pad(sd, code->pad) ? :
249 check_state(sd, state, code->which, code->pad, code->stream) ? :
250 sd->ops->pad->enum_mbus_code(sd, state, code);
253 static int call_enum_frame_size(struct v4l2_subdev *sd,
254 struct v4l2_subdev_state *state,
255 struct v4l2_subdev_frame_size_enum *fse)
257 if (!fse)
258 return -EINVAL;
260 return check_which(fse->which) ? : check_pad(sd, fse->pad) ? :
261 check_state(sd, state, fse->which, fse->pad, fse->stream) ? :
262 sd->ops->pad->enum_frame_size(sd, state, fse);
265 static int call_enum_frame_interval(struct v4l2_subdev *sd,
266 struct v4l2_subdev_state *state,
267 struct v4l2_subdev_frame_interval_enum *fie)
269 if (!fie)
270 return -EINVAL;
272 return check_which(fie->which) ? : check_pad(sd, fie->pad) ? :
273 check_state(sd, state, fie->which, fie->pad, fie->stream) ? :
274 sd->ops->pad->enum_frame_interval(sd, state, fie);
277 static inline int check_selection(struct v4l2_subdev *sd,
278 struct v4l2_subdev_state *state,
279 struct v4l2_subdev_selection *sel)
281 if (!sel)
282 return -EINVAL;
284 return check_which(sel->which) ? : check_pad(sd, sel->pad) ? :
285 check_state(sd, state, sel->which, sel->pad, sel->stream);
288 static int call_get_selection(struct v4l2_subdev *sd,
289 struct v4l2_subdev_state *state,
290 struct v4l2_subdev_selection *sel)
292 return check_selection(sd, state, sel) ? :
293 sd->ops->pad->get_selection(sd, state, sel);
296 static int call_set_selection(struct v4l2_subdev *sd,
297 struct v4l2_subdev_state *state,
298 struct v4l2_subdev_selection *sel)
300 return check_selection(sd, state, sel) ? :
301 sd->ops->pad->set_selection(sd, state, sel);
304 static inline int check_frame_interval(struct v4l2_subdev *sd,
305 struct v4l2_subdev_state *state,
306 struct v4l2_subdev_frame_interval *fi)
308 if (!fi)
309 return -EINVAL;
311 return check_which(fi->which) ? : check_pad(sd, fi->pad) ? :
312 check_state(sd, state, fi->which, fi->pad, fi->stream);
315 static int call_get_frame_interval(struct v4l2_subdev *sd,
316 struct v4l2_subdev_state *state,
317 struct v4l2_subdev_frame_interval *fi)
319 return check_frame_interval(sd, state, fi) ? :
320 sd->ops->pad->get_frame_interval(sd, state, fi);
323 static int call_set_frame_interval(struct v4l2_subdev *sd,
324 struct v4l2_subdev_state *state,
325 struct v4l2_subdev_frame_interval *fi)
327 return check_frame_interval(sd, state, fi) ? :
328 sd->ops->pad->set_frame_interval(sd, state, fi);
331 static int call_get_frame_desc(struct v4l2_subdev *sd, unsigned int pad,
332 struct v4l2_mbus_frame_desc *fd)
334 unsigned int i;
335 int ret;
337 #if defined(CONFIG_MEDIA_CONTROLLER)
338 if (!(sd->entity.pads[pad].flags & MEDIA_PAD_FL_SOURCE))
339 return -EOPNOTSUPP;
340 #endif
342 memset(fd, 0, sizeof(*fd));
344 ret = sd->ops->pad->get_frame_desc(sd, pad, fd);
345 if (ret)
346 return ret;
348 dev_dbg(sd->dev, "Frame descriptor on pad %u, type %s\n", pad,
349 fd->type == V4L2_MBUS_FRAME_DESC_TYPE_PARALLEL ? "parallel" :
350 fd->type == V4L2_MBUS_FRAME_DESC_TYPE_CSI2 ? "CSI-2" :
351 "unknown");
353 for (i = 0; i < fd->num_entries; i++) {
354 struct v4l2_mbus_frame_desc_entry *entry = &fd->entry[i];
355 char buf[20] = "";
357 if (fd->type == V4L2_MBUS_FRAME_DESC_TYPE_CSI2)
358 WARN_ON(snprintf(buf, sizeof(buf),
359 ", vc %u, dt 0x%02x",
360 entry->bus.csi2.vc,
361 entry->bus.csi2.dt) >= sizeof(buf));
363 dev_dbg(sd->dev,
364 "\tstream %u, code 0x%04x, length %u, flags 0x%04x%s\n",
365 entry->stream, entry->pixelcode, entry->length,
366 entry->flags, buf);
369 return 0;
372 static inline int check_edid(struct v4l2_subdev *sd,
373 struct v4l2_subdev_edid *edid)
375 if (!edid)
376 return -EINVAL;
378 if (edid->blocks && edid->edid == NULL)
379 return -EINVAL;
381 return check_pad(sd, edid->pad);
384 static int call_get_edid(struct v4l2_subdev *sd, struct v4l2_subdev_edid *edid)
386 return check_edid(sd, edid) ? : sd->ops->pad->get_edid(sd, edid);
389 static int call_set_edid(struct v4l2_subdev *sd, struct v4l2_subdev_edid *edid)
391 return check_edid(sd, edid) ? : sd->ops->pad->set_edid(sd, edid);
394 static int call_s_dv_timings(struct v4l2_subdev *sd, unsigned int pad,
395 struct v4l2_dv_timings *timings)
397 if (!timings)
398 return -EINVAL;
400 return check_pad(sd, pad) ? :
401 sd->ops->pad->s_dv_timings(sd, pad, timings);
404 static int call_g_dv_timings(struct v4l2_subdev *sd, unsigned int pad,
405 struct v4l2_dv_timings *timings)
407 if (!timings)
408 return -EINVAL;
410 return check_pad(sd, pad) ? :
411 sd->ops->pad->g_dv_timings(sd, pad, timings);
414 static int call_query_dv_timings(struct v4l2_subdev *sd, unsigned int pad,
415 struct v4l2_dv_timings *timings)
417 if (!timings)
418 return -EINVAL;
420 return check_pad(sd, pad) ? :
421 sd->ops->pad->query_dv_timings(sd, pad, timings);
424 static int call_dv_timings_cap(struct v4l2_subdev *sd,
425 struct v4l2_dv_timings_cap *cap)
427 if (!cap)
428 return -EINVAL;
430 return check_pad(sd, cap->pad) ? :
431 sd->ops->pad->dv_timings_cap(sd, cap);
434 static int call_enum_dv_timings(struct v4l2_subdev *sd,
435 struct v4l2_enum_dv_timings *dvt)
437 if (!dvt)
438 return -EINVAL;
440 return check_pad(sd, dvt->pad) ? :
441 sd->ops->pad->enum_dv_timings(sd, dvt);
444 static int call_get_mbus_config(struct v4l2_subdev *sd, unsigned int pad,
445 struct v4l2_mbus_config *config)
447 return check_pad(sd, pad) ? :
448 sd->ops->pad->get_mbus_config(sd, pad, config);
451 static int call_s_stream(struct v4l2_subdev *sd, int enable)
453 int ret;
456 * The .s_stream() operation must never be called to start or stop an
457 * already started or stopped subdev. Catch offenders but don't return
458 * an error yet to avoid regressions.
460 if (WARN_ON(sd->s_stream_enabled == !!enable))
461 return 0;
463 ret = sd->ops->video->s_stream(sd, enable);
465 if (!enable && ret < 0) {
466 dev_warn(sd->dev, "disabling streaming failed (%d)\n", ret);
467 ret = 0;
470 if (!ret) {
471 sd->s_stream_enabled = enable;
473 if (enable)
474 v4l2_subdev_enable_privacy_led(sd);
475 else
476 v4l2_subdev_disable_privacy_led(sd);
479 return ret;
482 #ifdef CONFIG_MEDIA_CONTROLLER
484 * Create state-management wrapper for pad ops dealing with subdev state. The
485 * wrapper handles the case where the caller does not provide the called
486 * subdev's state. This should be removed when all the callers are fixed.
488 #define DEFINE_STATE_WRAPPER(f, arg_type) \
489 static int call_##f##_state(struct v4l2_subdev *sd, \
490 struct v4l2_subdev_state *_state, \
491 arg_type *arg) \
493 struct v4l2_subdev_state *state = _state; \
494 int ret; \
495 if (!_state) \
496 state = v4l2_subdev_lock_and_get_active_state(sd); \
497 ret = call_##f(sd, state, arg); \
498 if (!_state && state) \
499 v4l2_subdev_unlock_state(state); \
500 return ret; \
503 #else /* CONFIG_MEDIA_CONTROLLER */
505 #define DEFINE_STATE_WRAPPER(f, arg_type) \
506 static int call_##f##_state(struct v4l2_subdev *sd, \
507 struct v4l2_subdev_state *state, \
508 arg_type *arg) \
510 return call_##f(sd, state, arg); \
513 #endif /* CONFIG_MEDIA_CONTROLLER */
515 DEFINE_STATE_WRAPPER(get_fmt, struct v4l2_subdev_format);
516 DEFINE_STATE_WRAPPER(set_fmt, struct v4l2_subdev_format);
517 DEFINE_STATE_WRAPPER(enum_mbus_code, struct v4l2_subdev_mbus_code_enum);
518 DEFINE_STATE_WRAPPER(enum_frame_size, struct v4l2_subdev_frame_size_enum);
519 DEFINE_STATE_WRAPPER(enum_frame_interval, struct v4l2_subdev_frame_interval_enum);
520 DEFINE_STATE_WRAPPER(get_selection, struct v4l2_subdev_selection);
521 DEFINE_STATE_WRAPPER(set_selection, struct v4l2_subdev_selection);
523 static const struct v4l2_subdev_pad_ops v4l2_subdev_call_pad_wrappers = {
524 .get_fmt = call_get_fmt_state,
525 .set_fmt = call_set_fmt_state,
526 .enum_mbus_code = call_enum_mbus_code_state,
527 .enum_frame_size = call_enum_frame_size_state,
528 .enum_frame_interval = call_enum_frame_interval_state,
529 .get_selection = call_get_selection_state,
530 .set_selection = call_set_selection_state,
531 .get_frame_interval = call_get_frame_interval,
532 .set_frame_interval = call_set_frame_interval,
533 .get_edid = call_get_edid,
534 .set_edid = call_set_edid,
535 .s_dv_timings = call_s_dv_timings,
536 .g_dv_timings = call_g_dv_timings,
537 .query_dv_timings = call_query_dv_timings,
538 .dv_timings_cap = call_dv_timings_cap,
539 .enum_dv_timings = call_enum_dv_timings,
540 .get_frame_desc = call_get_frame_desc,
541 .get_mbus_config = call_get_mbus_config,
544 static const struct v4l2_subdev_video_ops v4l2_subdev_call_video_wrappers = {
545 .s_stream = call_s_stream,
548 const struct v4l2_subdev_ops v4l2_subdev_call_wrappers = {
549 .pad = &v4l2_subdev_call_pad_wrappers,
550 .video = &v4l2_subdev_call_video_wrappers,
552 EXPORT_SYMBOL(v4l2_subdev_call_wrappers);
554 #if defined(CONFIG_VIDEO_V4L2_SUBDEV_API)
556 static struct v4l2_subdev_state *
557 subdev_ioctl_get_state(struct v4l2_subdev *sd, struct v4l2_subdev_fh *subdev_fh,
558 unsigned int cmd, void *arg)
560 u32 which;
562 switch (cmd) {
563 default:
564 return NULL;
565 case VIDIOC_SUBDEV_G_FMT:
566 case VIDIOC_SUBDEV_S_FMT:
567 which = ((struct v4l2_subdev_format *)arg)->which;
568 break;
569 case VIDIOC_SUBDEV_G_CROP:
570 case VIDIOC_SUBDEV_S_CROP:
571 which = ((struct v4l2_subdev_crop *)arg)->which;
572 break;
573 case VIDIOC_SUBDEV_ENUM_MBUS_CODE:
574 which = ((struct v4l2_subdev_mbus_code_enum *)arg)->which;
575 break;
576 case VIDIOC_SUBDEV_ENUM_FRAME_SIZE:
577 which = ((struct v4l2_subdev_frame_size_enum *)arg)->which;
578 break;
579 case VIDIOC_SUBDEV_ENUM_FRAME_INTERVAL:
580 which = ((struct v4l2_subdev_frame_interval_enum *)arg)->which;
581 break;
582 case VIDIOC_SUBDEV_G_SELECTION:
583 case VIDIOC_SUBDEV_S_SELECTION:
584 which = ((struct v4l2_subdev_selection *)arg)->which;
585 break;
586 case VIDIOC_SUBDEV_G_FRAME_INTERVAL:
587 case VIDIOC_SUBDEV_S_FRAME_INTERVAL: {
588 struct v4l2_subdev_frame_interval *fi = arg;
590 if (!(subdev_fh->client_caps &
591 V4L2_SUBDEV_CLIENT_CAP_INTERVAL_USES_WHICH))
592 fi->which = V4L2_SUBDEV_FORMAT_ACTIVE;
594 which = fi->which;
595 break;
597 case VIDIOC_SUBDEV_G_ROUTING:
598 case VIDIOC_SUBDEV_S_ROUTING:
599 which = ((struct v4l2_subdev_routing *)arg)->which;
600 break;
603 return which == V4L2_SUBDEV_FORMAT_TRY ?
604 subdev_fh->state :
605 v4l2_subdev_get_unlocked_active_state(sd);
608 static long subdev_do_ioctl(struct file *file, unsigned int cmd, void *arg,
609 struct v4l2_subdev_state *state)
611 struct video_device *vdev = video_devdata(file);
612 struct v4l2_subdev *sd = vdev_to_v4l2_subdev(vdev);
613 struct v4l2_fh *vfh = file->private_data;
614 struct v4l2_subdev_fh *subdev_fh = to_v4l2_subdev_fh(vfh);
615 bool ro_subdev = test_bit(V4L2_FL_SUBDEV_RO_DEVNODE, &vdev->flags);
616 bool streams_subdev = sd->flags & V4L2_SUBDEV_FL_STREAMS;
617 bool client_supports_streams = subdev_fh->client_caps &
618 V4L2_SUBDEV_CLIENT_CAP_STREAMS;
619 int rval;
622 * If the streams API is not enabled, remove V4L2_SUBDEV_CAP_STREAMS.
623 * Remove this when the API is no longer experimental.
625 if (!v4l2_subdev_enable_streams_api)
626 streams_subdev = false;
628 switch (cmd) {
629 case VIDIOC_SUBDEV_QUERYCAP: {
630 struct v4l2_subdev_capability *cap = arg;
632 memset(cap->reserved, 0, sizeof(cap->reserved));
633 cap->version = LINUX_VERSION_CODE;
634 cap->capabilities =
635 (ro_subdev ? V4L2_SUBDEV_CAP_RO_SUBDEV : 0) |
636 (streams_subdev ? V4L2_SUBDEV_CAP_STREAMS : 0);
638 return 0;
641 case VIDIOC_QUERYCTRL:
643 * TODO: this really should be folded into v4l2_queryctrl (this
644 * currently returns -EINVAL for NULL control handlers).
645 * However, v4l2_queryctrl() is still called directly by
646 * drivers as well and until that has been addressed I believe
647 * it is safer to do the check here. The same is true for the
648 * other control ioctls below.
650 if (!vfh->ctrl_handler)
651 return -ENOTTY;
652 return v4l2_queryctrl(vfh->ctrl_handler, arg);
654 case VIDIOC_QUERY_EXT_CTRL:
655 if (!vfh->ctrl_handler)
656 return -ENOTTY;
657 return v4l2_query_ext_ctrl(vfh->ctrl_handler, arg);
659 case VIDIOC_QUERYMENU:
660 if (!vfh->ctrl_handler)
661 return -ENOTTY;
662 return v4l2_querymenu(vfh->ctrl_handler, arg);
664 case VIDIOC_G_CTRL:
665 if (!vfh->ctrl_handler)
666 return -ENOTTY;
667 return v4l2_g_ctrl(vfh->ctrl_handler, arg);
669 case VIDIOC_S_CTRL:
670 if (!vfh->ctrl_handler)
671 return -ENOTTY;
672 return v4l2_s_ctrl(vfh, vfh->ctrl_handler, arg);
674 case VIDIOC_G_EXT_CTRLS:
675 if (!vfh->ctrl_handler)
676 return -ENOTTY;
677 return v4l2_g_ext_ctrls(vfh->ctrl_handler,
678 vdev, sd->v4l2_dev->mdev, arg);
680 case VIDIOC_S_EXT_CTRLS:
681 if (!vfh->ctrl_handler)
682 return -ENOTTY;
683 return v4l2_s_ext_ctrls(vfh, vfh->ctrl_handler,
684 vdev, sd->v4l2_dev->mdev, arg);
686 case VIDIOC_TRY_EXT_CTRLS:
687 if (!vfh->ctrl_handler)
688 return -ENOTTY;
689 return v4l2_try_ext_ctrls(vfh->ctrl_handler,
690 vdev, sd->v4l2_dev->mdev, arg);
692 case VIDIOC_DQEVENT:
693 if (!(sd->flags & V4L2_SUBDEV_FL_HAS_EVENTS))
694 return -ENOIOCTLCMD;
696 return v4l2_event_dequeue(vfh, arg, file->f_flags & O_NONBLOCK);
698 case VIDIOC_SUBSCRIBE_EVENT:
699 if (v4l2_subdev_has_op(sd, core, subscribe_event))
700 return v4l2_subdev_call(sd, core, subscribe_event,
701 vfh, arg);
703 if ((sd->flags & V4L2_SUBDEV_FL_HAS_EVENTS) &&
704 vfh->ctrl_handler)
705 return v4l2_ctrl_subdev_subscribe_event(sd, vfh, arg);
707 return -ENOIOCTLCMD;
709 case VIDIOC_UNSUBSCRIBE_EVENT:
710 if (v4l2_subdev_has_op(sd, core, unsubscribe_event))
711 return v4l2_subdev_call(sd, core, unsubscribe_event,
712 vfh, arg);
714 if (sd->flags & V4L2_SUBDEV_FL_HAS_EVENTS)
715 return v4l2_event_subdev_unsubscribe(sd, vfh, arg);
717 return -ENOIOCTLCMD;
719 #ifdef CONFIG_VIDEO_ADV_DEBUG
720 case VIDIOC_DBG_G_REGISTER:
722 struct v4l2_dbg_register *p = arg;
724 if (!capable(CAP_SYS_ADMIN))
725 return -EPERM;
726 return v4l2_subdev_call(sd, core, g_register, p);
728 case VIDIOC_DBG_S_REGISTER:
730 struct v4l2_dbg_register *p = arg;
732 if (!capable(CAP_SYS_ADMIN))
733 return -EPERM;
734 return v4l2_subdev_call(sd, core, s_register, p);
736 case VIDIOC_DBG_G_CHIP_INFO:
738 struct v4l2_dbg_chip_info *p = arg;
740 if (p->match.type != V4L2_CHIP_MATCH_SUBDEV || p->match.addr)
741 return -EINVAL;
742 if (sd->ops->core && sd->ops->core->s_register)
743 p->flags |= V4L2_CHIP_FL_WRITABLE;
744 if (sd->ops->core && sd->ops->core->g_register)
745 p->flags |= V4L2_CHIP_FL_READABLE;
746 strscpy(p->name, sd->name, sizeof(p->name));
747 return 0;
749 #endif
751 case VIDIOC_LOG_STATUS: {
752 int ret;
754 pr_info("%s: ================= START STATUS =================\n",
755 sd->name);
756 ret = v4l2_subdev_call(sd, core, log_status);
757 pr_info("%s: ================== END STATUS ==================\n",
758 sd->name);
759 return ret;
762 case VIDIOC_SUBDEV_G_FMT: {
763 struct v4l2_subdev_format *format = arg;
765 if (!client_supports_streams)
766 format->stream = 0;
768 memset(format->reserved, 0, sizeof(format->reserved));
769 memset(format->format.reserved, 0, sizeof(format->format.reserved));
770 return v4l2_subdev_call(sd, pad, get_fmt, state, format);
773 case VIDIOC_SUBDEV_S_FMT: {
774 struct v4l2_subdev_format *format = arg;
776 if (format->which != V4L2_SUBDEV_FORMAT_TRY && ro_subdev)
777 return -EPERM;
779 if (!client_supports_streams)
780 format->stream = 0;
782 memset(format->reserved, 0, sizeof(format->reserved));
783 memset(format->format.reserved, 0, sizeof(format->format.reserved));
784 return v4l2_subdev_call(sd, pad, set_fmt, state, format);
787 case VIDIOC_SUBDEV_G_CROP: {
788 struct v4l2_subdev_crop *crop = arg;
789 struct v4l2_subdev_selection sel;
791 if (!client_supports_streams)
792 crop->stream = 0;
794 memset(crop->reserved, 0, sizeof(crop->reserved));
795 memset(&sel, 0, sizeof(sel));
796 sel.which = crop->which;
797 sel.pad = crop->pad;
798 sel.stream = crop->stream;
799 sel.target = V4L2_SEL_TGT_CROP;
801 rval = v4l2_subdev_call(
802 sd, pad, get_selection, state, &sel);
804 crop->rect = sel.r;
806 return rval;
809 case VIDIOC_SUBDEV_S_CROP: {
810 struct v4l2_subdev_crop *crop = arg;
811 struct v4l2_subdev_selection sel;
813 if (crop->which != V4L2_SUBDEV_FORMAT_TRY && ro_subdev)
814 return -EPERM;
816 if (!client_supports_streams)
817 crop->stream = 0;
819 memset(crop->reserved, 0, sizeof(crop->reserved));
820 memset(&sel, 0, sizeof(sel));
821 sel.which = crop->which;
822 sel.pad = crop->pad;
823 sel.stream = crop->stream;
824 sel.target = V4L2_SEL_TGT_CROP;
825 sel.r = crop->rect;
827 rval = v4l2_subdev_call(
828 sd, pad, set_selection, state, &sel);
830 crop->rect = sel.r;
832 return rval;
835 case VIDIOC_SUBDEV_ENUM_MBUS_CODE: {
836 struct v4l2_subdev_mbus_code_enum *code = arg;
838 if (!client_supports_streams)
839 code->stream = 0;
841 memset(code->reserved, 0, sizeof(code->reserved));
842 return v4l2_subdev_call(sd, pad, enum_mbus_code, state,
843 code);
846 case VIDIOC_SUBDEV_ENUM_FRAME_SIZE: {
847 struct v4l2_subdev_frame_size_enum *fse = arg;
849 if (!client_supports_streams)
850 fse->stream = 0;
852 memset(fse->reserved, 0, sizeof(fse->reserved));
853 return v4l2_subdev_call(sd, pad, enum_frame_size, state,
854 fse);
857 case VIDIOC_SUBDEV_G_FRAME_INTERVAL: {
858 struct v4l2_subdev_frame_interval *fi = arg;
860 if (!client_supports_streams)
861 fi->stream = 0;
863 memset(fi->reserved, 0, sizeof(fi->reserved));
864 return v4l2_subdev_call(sd, pad, get_frame_interval, state, fi);
867 case VIDIOC_SUBDEV_S_FRAME_INTERVAL: {
868 struct v4l2_subdev_frame_interval *fi = arg;
870 if (!client_supports_streams)
871 fi->stream = 0;
873 if (fi->which != V4L2_SUBDEV_FORMAT_TRY && ro_subdev)
874 return -EPERM;
876 memset(fi->reserved, 0, sizeof(fi->reserved));
877 return v4l2_subdev_call(sd, pad, set_frame_interval, state, fi);
880 case VIDIOC_SUBDEV_ENUM_FRAME_INTERVAL: {
881 struct v4l2_subdev_frame_interval_enum *fie = arg;
883 if (!client_supports_streams)
884 fie->stream = 0;
886 memset(fie->reserved, 0, sizeof(fie->reserved));
887 return v4l2_subdev_call(sd, pad, enum_frame_interval, state,
888 fie);
891 case VIDIOC_SUBDEV_G_SELECTION: {
892 struct v4l2_subdev_selection *sel = arg;
894 if (!client_supports_streams)
895 sel->stream = 0;
897 memset(sel->reserved, 0, sizeof(sel->reserved));
898 return v4l2_subdev_call(
899 sd, pad, get_selection, state, sel);
902 case VIDIOC_SUBDEV_S_SELECTION: {
903 struct v4l2_subdev_selection *sel = arg;
905 if (sel->which != V4L2_SUBDEV_FORMAT_TRY && ro_subdev)
906 return -EPERM;
908 if (!client_supports_streams)
909 sel->stream = 0;
911 memset(sel->reserved, 0, sizeof(sel->reserved));
912 return v4l2_subdev_call(
913 sd, pad, set_selection, state, sel);
916 case VIDIOC_G_EDID: {
917 struct v4l2_subdev_edid *edid = arg;
919 return v4l2_subdev_call(sd, pad, get_edid, edid);
922 case VIDIOC_S_EDID: {
923 struct v4l2_subdev_edid *edid = arg;
925 return v4l2_subdev_call(sd, pad, set_edid, edid);
928 case VIDIOC_SUBDEV_DV_TIMINGS_CAP: {
929 struct v4l2_dv_timings_cap *cap = arg;
931 return v4l2_subdev_call(sd, pad, dv_timings_cap, cap);
934 case VIDIOC_SUBDEV_ENUM_DV_TIMINGS: {
935 struct v4l2_enum_dv_timings *dvt = arg;
937 return v4l2_subdev_call(sd, pad, enum_dv_timings, dvt);
940 case VIDIOC_SUBDEV_QUERY_DV_TIMINGS:
941 return v4l2_subdev_call(sd, pad, query_dv_timings, 0, arg);
943 case VIDIOC_SUBDEV_G_DV_TIMINGS:
944 return v4l2_subdev_call(sd, pad, g_dv_timings, 0, arg);
946 case VIDIOC_SUBDEV_S_DV_TIMINGS:
947 if (ro_subdev)
948 return -EPERM;
950 return v4l2_subdev_call(sd, pad, s_dv_timings, 0, arg);
952 case VIDIOC_SUBDEV_G_STD:
953 return v4l2_subdev_call(sd, video, g_std, arg);
955 case VIDIOC_SUBDEV_S_STD: {
956 v4l2_std_id *std = arg;
958 if (ro_subdev)
959 return -EPERM;
961 return v4l2_subdev_call(sd, video, s_std, *std);
964 case VIDIOC_SUBDEV_ENUMSTD: {
965 struct v4l2_standard *p = arg;
966 v4l2_std_id id;
968 if (v4l2_subdev_call(sd, video, g_tvnorms, &id))
969 return -EINVAL;
971 return v4l_video_std_enumstd(p, id);
974 case VIDIOC_SUBDEV_QUERYSTD:
975 return v4l2_subdev_call(sd, video, querystd, arg);
977 case VIDIOC_SUBDEV_G_ROUTING: {
978 struct v4l2_subdev_routing *routing = arg;
979 struct v4l2_subdev_krouting *krouting;
981 if (!v4l2_subdev_enable_streams_api)
982 return -ENOIOCTLCMD;
984 if (!(sd->flags & V4L2_SUBDEV_FL_STREAMS))
985 return -ENOIOCTLCMD;
987 memset(routing->reserved, 0, sizeof(routing->reserved));
989 krouting = &state->routing;
991 memcpy((struct v4l2_subdev_route *)(uintptr_t)routing->routes,
992 krouting->routes,
993 min(krouting->num_routes, routing->len_routes) *
994 sizeof(*krouting->routes));
995 routing->num_routes = krouting->num_routes;
997 return 0;
1000 case VIDIOC_SUBDEV_S_ROUTING: {
1001 struct v4l2_subdev_routing *routing = arg;
1002 struct v4l2_subdev_route *routes =
1003 (struct v4l2_subdev_route *)(uintptr_t)routing->routes;
1004 struct v4l2_subdev_krouting krouting = {};
1005 unsigned int i;
1007 if (!v4l2_subdev_enable_streams_api)
1008 return -ENOIOCTLCMD;
1010 if (!(sd->flags & V4L2_SUBDEV_FL_STREAMS))
1011 return -ENOIOCTLCMD;
1013 if (routing->which != V4L2_SUBDEV_FORMAT_TRY && ro_subdev)
1014 return -EPERM;
1016 if (routing->num_routes > routing->len_routes)
1017 return -EINVAL;
1019 memset(routing->reserved, 0, sizeof(routing->reserved));
1021 for (i = 0; i < routing->num_routes; ++i) {
1022 const struct v4l2_subdev_route *route = &routes[i];
1023 const struct media_pad *pads = sd->entity.pads;
1025 if (route->sink_stream > V4L2_SUBDEV_MAX_STREAM_ID ||
1026 route->source_stream > V4L2_SUBDEV_MAX_STREAM_ID)
1027 return -EINVAL;
1029 if (route->sink_pad >= sd->entity.num_pads)
1030 return -EINVAL;
1032 if (!(pads[route->sink_pad].flags &
1033 MEDIA_PAD_FL_SINK))
1034 return -EINVAL;
1036 if (route->source_pad >= sd->entity.num_pads)
1037 return -EINVAL;
1039 if (!(pads[route->source_pad].flags &
1040 MEDIA_PAD_FL_SOURCE))
1041 return -EINVAL;
1045 * If the driver doesn't support setting routing, just return
1046 * the routing table.
1048 if (!v4l2_subdev_has_op(sd, pad, set_routing)) {
1049 memcpy((struct v4l2_subdev_route *)(uintptr_t)routing->routes,
1050 state->routing.routes,
1051 min(state->routing.num_routes, routing->len_routes) *
1052 sizeof(*state->routing.routes));
1053 routing->num_routes = state->routing.num_routes;
1055 return 0;
1058 krouting.num_routes = routing->num_routes;
1059 krouting.len_routes = routing->len_routes;
1060 krouting.routes = routes;
1062 rval = v4l2_subdev_call(sd, pad, set_routing, state,
1063 routing->which, &krouting);
1064 if (rval < 0)
1065 return rval;
1067 memcpy((struct v4l2_subdev_route *)(uintptr_t)routing->routes,
1068 state->routing.routes,
1069 min(state->routing.num_routes, routing->len_routes) *
1070 sizeof(*state->routing.routes));
1071 routing->num_routes = state->routing.num_routes;
1073 return 0;
1076 case VIDIOC_SUBDEV_G_CLIENT_CAP: {
1077 struct v4l2_subdev_client_capability *client_cap = arg;
1079 client_cap->capabilities = subdev_fh->client_caps;
1081 return 0;
1084 case VIDIOC_SUBDEV_S_CLIENT_CAP: {
1085 struct v4l2_subdev_client_capability *client_cap = arg;
1088 * Clear V4L2_SUBDEV_CLIENT_CAP_STREAMS if streams API is not
1089 * enabled. Remove this when streams API is no longer
1090 * experimental.
1092 if (!v4l2_subdev_enable_streams_api)
1093 client_cap->capabilities &= ~V4L2_SUBDEV_CLIENT_CAP_STREAMS;
1095 /* Filter out unsupported capabilities */
1096 client_cap->capabilities &= (V4L2_SUBDEV_CLIENT_CAP_STREAMS |
1097 V4L2_SUBDEV_CLIENT_CAP_INTERVAL_USES_WHICH);
1099 subdev_fh->client_caps = client_cap->capabilities;
1101 return 0;
1104 default:
1105 return v4l2_subdev_call(sd, core, ioctl, cmd, arg);
1108 return 0;
1111 static long subdev_do_ioctl_lock(struct file *file, unsigned int cmd, void *arg)
1113 struct video_device *vdev = video_devdata(file);
1114 struct mutex *lock = vdev->lock;
1115 long ret = -ENODEV;
1117 if (lock && mutex_lock_interruptible(lock))
1118 return -ERESTARTSYS;
1120 if (video_is_registered(vdev)) {
1121 struct v4l2_subdev *sd = vdev_to_v4l2_subdev(vdev);
1122 struct v4l2_fh *vfh = file->private_data;
1123 struct v4l2_subdev_fh *subdev_fh = to_v4l2_subdev_fh(vfh);
1124 struct v4l2_subdev_state *state;
1126 state = subdev_ioctl_get_state(sd, subdev_fh, cmd, arg);
1128 if (state)
1129 v4l2_subdev_lock_state(state);
1131 ret = subdev_do_ioctl(file, cmd, arg, state);
1133 if (state)
1134 v4l2_subdev_unlock_state(state);
1137 if (lock)
1138 mutex_unlock(lock);
1139 return ret;
1142 static long subdev_ioctl(struct file *file, unsigned int cmd,
1143 unsigned long arg)
1145 return video_usercopy(file, cmd, arg, subdev_do_ioctl_lock);
1148 #ifdef CONFIG_COMPAT
1149 static long subdev_compat_ioctl32(struct file *file, unsigned int cmd,
1150 unsigned long arg)
1152 struct video_device *vdev = video_devdata(file);
1153 struct v4l2_subdev *sd = vdev_to_v4l2_subdev(vdev);
1155 return v4l2_subdev_call(sd, core, compat_ioctl32, cmd, arg);
1157 #endif
1159 #else /* CONFIG_VIDEO_V4L2_SUBDEV_API */
1160 static long subdev_ioctl(struct file *file, unsigned int cmd,
1161 unsigned long arg)
1163 return -ENODEV;
1166 #ifdef CONFIG_COMPAT
1167 static long subdev_compat_ioctl32(struct file *file, unsigned int cmd,
1168 unsigned long arg)
1170 return -ENODEV;
1172 #endif
1173 #endif /* CONFIG_VIDEO_V4L2_SUBDEV_API */
1175 static __poll_t subdev_poll(struct file *file, poll_table *wait)
1177 struct video_device *vdev = video_devdata(file);
1178 struct v4l2_subdev *sd = vdev_to_v4l2_subdev(vdev);
1179 struct v4l2_fh *fh = file->private_data;
1181 if (!(sd->flags & V4L2_SUBDEV_FL_HAS_EVENTS))
1182 return EPOLLERR;
1184 poll_wait(file, &fh->wait, wait);
1186 if (v4l2_event_pending(fh))
1187 return EPOLLPRI;
1189 return 0;
1192 const struct v4l2_file_operations v4l2_subdev_fops = {
1193 .owner = THIS_MODULE,
1194 .open = subdev_open,
1195 .unlocked_ioctl = subdev_ioctl,
1196 #ifdef CONFIG_COMPAT
1197 .compat_ioctl32 = subdev_compat_ioctl32,
1198 #endif
1199 .release = subdev_close,
1200 .poll = subdev_poll,
1203 #ifdef CONFIG_MEDIA_CONTROLLER
1205 int v4l2_subdev_get_fwnode_pad_1_to_1(struct media_entity *entity,
1206 struct fwnode_endpoint *endpoint)
1208 struct fwnode_handle *fwnode;
1209 struct v4l2_subdev *sd;
1211 if (!is_media_entity_v4l2_subdev(entity))
1212 return -EINVAL;
1214 sd = media_entity_to_v4l2_subdev(entity);
1216 fwnode = fwnode_graph_get_port_parent(endpoint->local_fwnode);
1217 fwnode_handle_put(fwnode);
1219 if (device_match_fwnode(sd->dev, fwnode))
1220 return endpoint->port;
1222 return -ENXIO;
1224 EXPORT_SYMBOL_GPL(v4l2_subdev_get_fwnode_pad_1_to_1);
1226 int v4l2_subdev_link_validate_default(struct v4l2_subdev *sd,
1227 struct media_link *link,
1228 struct v4l2_subdev_format *source_fmt,
1229 struct v4l2_subdev_format *sink_fmt)
1231 bool pass = true;
1233 /* The width, height and code must match. */
1234 if (source_fmt->format.width != sink_fmt->format.width) {
1235 dev_dbg(sd->entity.graph_obj.mdev->dev,
1236 "%s: width does not match (source %u, sink %u)\n",
1237 __func__,
1238 source_fmt->format.width, sink_fmt->format.width);
1239 pass = false;
1242 if (source_fmt->format.height != sink_fmt->format.height) {
1243 dev_dbg(sd->entity.graph_obj.mdev->dev,
1244 "%s: height does not match (source %u, sink %u)\n",
1245 __func__,
1246 source_fmt->format.height, sink_fmt->format.height);
1247 pass = false;
1250 if (source_fmt->format.code != sink_fmt->format.code) {
1251 dev_dbg(sd->entity.graph_obj.mdev->dev,
1252 "%s: media bus code does not match (source 0x%8.8x, sink 0x%8.8x)\n",
1253 __func__,
1254 source_fmt->format.code, sink_fmt->format.code);
1255 pass = false;
1258 /* The field order must match, or the sink field order must be NONE
1259 * to support interlaced hardware connected to bridges that support
1260 * progressive formats only.
1262 if (source_fmt->format.field != sink_fmt->format.field &&
1263 sink_fmt->format.field != V4L2_FIELD_NONE) {
1264 dev_dbg(sd->entity.graph_obj.mdev->dev,
1265 "%s: field does not match (source %u, sink %u)\n",
1266 __func__,
1267 source_fmt->format.field, sink_fmt->format.field);
1268 pass = false;
1271 if (pass)
1272 return 0;
1274 dev_dbg(sd->entity.graph_obj.mdev->dev,
1275 "%s: link was \"%s\":%u -> \"%s\":%u\n", __func__,
1276 link->source->entity->name, link->source->index,
1277 link->sink->entity->name, link->sink->index);
1279 return -EPIPE;
1281 EXPORT_SYMBOL_GPL(v4l2_subdev_link_validate_default);
1283 static int
1284 v4l2_subdev_link_validate_get_format(struct media_pad *pad, u32 stream,
1285 struct v4l2_subdev_format *fmt,
1286 bool states_locked)
1288 struct v4l2_subdev_state *state;
1289 struct v4l2_subdev *sd;
1290 int ret;
1292 sd = media_entity_to_v4l2_subdev(pad->entity);
1294 fmt->which = V4L2_SUBDEV_FORMAT_ACTIVE;
1295 fmt->pad = pad->index;
1296 fmt->stream = stream;
1298 if (states_locked)
1299 state = v4l2_subdev_get_locked_active_state(sd);
1300 else
1301 state = v4l2_subdev_lock_and_get_active_state(sd);
1303 ret = v4l2_subdev_call(sd, pad, get_fmt, state, fmt);
1305 if (!states_locked && state)
1306 v4l2_subdev_unlock_state(state);
1308 return ret;
1311 #if defined(CONFIG_VIDEO_V4L2_SUBDEV_API)
1313 static void __v4l2_link_validate_get_streams(struct media_pad *pad,
1314 u64 *streams_mask,
1315 bool states_locked)
1317 struct v4l2_subdev_route *route;
1318 struct v4l2_subdev_state *state;
1319 struct v4l2_subdev *subdev;
1321 subdev = media_entity_to_v4l2_subdev(pad->entity);
1323 *streams_mask = 0;
1325 if (states_locked)
1326 state = v4l2_subdev_get_locked_active_state(subdev);
1327 else
1328 state = v4l2_subdev_lock_and_get_active_state(subdev);
1330 if (WARN_ON(!state))
1331 return;
1333 for_each_active_route(&state->routing, route) {
1334 u32 route_pad;
1335 u32 route_stream;
1337 if (pad->flags & MEDIA_PAD_FL_SOURCE) {
1338 route_pad = route->source_pad;
1339 route_stream = route->source_stream;
1340 } else {
1341 route_pad = route->sink_pad;
1342 route_stream = route->sink_stream;
1345 if (route_pad != pad->index)
1346 continue;
1348 *streams_mask |= BIT_ULL(route_stream);
1351 if (!states_locked)
1352 v4l2_subdev_unlock_state(state);
1355 #endif /* CONFIG_VIDEO_V4L2_SUBDEV_API */
1357 static void v4l2_link_validate_get_streams(struct media_pad *pad,
1358 u64 *streams_mask,
1359 bool states_locked)
1361 struct v4l2_subdev *subdev = media_entity_to_v4l2_subdev(pad->entity);
1363 if (!(subdev->flags & V4L2_SUBDEV_FL_STREAMS)) {
1364 /* Non-streams subdevs have an implicit stream 0 */
1365 *streams_mask = BIT_ULL(0);
1366 return;
1369 #if defined(CONFIG_VIDEO_V4L2_SUBDEV_API)
1370 __v4l2_link_validate_get_streams(pad, streams_mask, states_locked);
1371 #else
1372 /* This shouldn't happen */
1373 *streams_mask = 0;
1374 #endif
1377 static int v4l2_subdev_link_validate_locked(struct media_link *link, bool states_locked)
1379 struct v4l2_subdev *sink_subdev =
1380 media_entity_to_v4l2_subdev(link->sink->entity);
1381 struct device *dev = sink_subdev->entity.graph_obj.mdev->dev;
1382 u64 source_streams_mask;
1383 u64 sink_streams_mask;
1384 u64 dangling_sink_streams;
1385 u32 stream;
1386 int ret;
1388 dev_dbg(dev, "validating link \"%s\":%u -> \"%s\":%u\n",
1389 link->source->entity->name, link->source->index,
1390 link->sink->entity->name, link->sink->index);
1392 v4l2_link_validate_get_streams(link->source, &source_streams_mask, states_locked);
1393 v4l2_link_validate_get_streams(link->sink, &sink_streams_mask, states_locked);
1396 * It is ok to have more source streams than sink streams as extra
1397 * source streams can just be ignored by the receiver, but having extra
1398 * sink streams is an error as streams must have a source.
1400 dangling_sink_streams = (source_streams_mask ^ sink_streams_mask) &
1401 sink_streams_mask;
1402 if (dangling_sink_streams) {
1403 dev_err(dev, "Dangling sink streams: mask %#llx\n",
1404 dangling_sink_streams);
1405 return -EINVAL;
1408 /* Validate source and sink stream formats */
1410 for (stream = 0; stream < sizeof(sink_streams_mask) * 8; ++stream) {
1411 struct v4l2_subdev_format sink_fmt, source_fmt;
1413 if (!(sink_streams_mask & BIT_ULL(stream)))
1414 continue;
1416 dev_dbg(dev, "validating stream \"%s\":%u:%u -> \"%s\":%u:%u\n",
1417 link->source->entity->name, link->source->index, stream,
1418 link->sink->entity->name, link->sink->index, stream);
1420 ret = v4l2_subdev_link_validate_get_format(link->source, stream,
1421 &source_fmt, states_locked);
1422 if (ret < 0) {
1423 dev_dbg(dev,
1424 "Failed to get format for \"%s\":%u:%u (but that's ok)\n",
1425 link->source->entity->name, link->source->index,
1426 stream);
1427 continue;
1430 ret = v4l2_subdev_link_validate_get_format(link->sink, stream,
1431 &sink_fmt, states_locked);
1432 if (ret < 0) {
1433 dev_dbg(dev,
1434 "Failed to get format for \"%s\":%u:%u (but that's ok)\n",
1435 link->sink->entity->name, link->sink->index,
1436 stream);
1437 continue;
1440 /* TODO: add stream number to link_validate() */
1441 ret = v4l2_subdev_call(sink_subdev, pad, link_validate, link,
1442 &source_fmt, &sink_fmt);
1443 if (!ret)
1444 continue;
1446 if (ret != -ENOIOCTLCMD)
1447 return ret;
1449 ret = v4l2_subdev_link_validate_default(sink_subdev, link,
1450 &source_fmt, &sink_fmt);
1452 if (ret)
1453 return ret;
1456 return 0;
1459 int v4l2_subdev_link_validate(struct media_link *link)
1461 struct v4l2_subdev *source_sd, *sink_sd;
1462 struct v4l2_subdev_state *source_state, *sink_state;
1463 bool states_locked;
1464 int ret;
1467 * Links are validated in the context of the sink entity. Usage of this
1468 * helper on a sink that is not a subdev is a clear driver bug.
1470 if (WARN_ON_ONCE(!is_media_entity_v4l2_subdev(link->sink->entity)))
1471 return -EINVAL;
1474 * If the source is a video device, delegate link validation to it. This
1475 * allows usage of this helper for subdev connected to a video output
1476 * device, provided that the driver implement the video output device's
1477 * .link_validate() operation.
1479 if (is_media_entity_v4l2_video_device(link->source->entity)) {
1480 struct media_entity *source = link->source->entity;
1482 if (!source->ops || !source->ops->link_validate) {
1484 * Many existing drivers do not implement the required
1485 * .link_validate() operation for their video devices.
1486 * Print a warning to get the drivers fixed, and return
1487 * 0 to avoid breaking userspace. This should
1488 * eventually be turned into a WARN_ON() when all
1489 * drivers will have been fixed.
1491 pr_warn_once("video device '%s' does not implement .link_validate(), driver bug!\n",
1492 source->name);
1493 return 0;
1497 * Avoid infinite loops in case a video device incorrectly uses
1498 * this helper function as its .link_validate() handler.
1500 if (WARN_ON(source->ops->link_validate == v4l2_subdev_link_validate))
1501 return -EINVAL;
1503 return source->ops->link_validate(link);
1507 * If the source is still not a subdev, usage of this helper is a clear
1508 * driver bug.
1510 if (WARN_ON(!is_media_entity_v4l2_subdev(link->source->entity)))
1511 return -EINVAL;
1513 sink_sd = media_entity_to_v4l2_subdev(link->sink->entity);
1514 source_sd = media_entity_to_v4l2_subdev(link->source->entity);
1516 sink_state = v4l2_subdev_get_unlocked_active_state(sink_sd);
1517 source_state = v4l2_subdev_get_unlocked_active_state(source_sd);
1519 states_locked = sink_state && source_state;
1521 if (states_locked)
1522 v4l2_subdev_lock_states(sink_state, source_state);
1524 ret = v4l2_subdev_link_validate_locked(link, states_locked);
1526 if (states_locked)
1527 v4l2_subdev_unlock_states(sink_state, source_state);
1529 return ret;
1531 EXPORT_SYMBOL_GPL(v4l2_subdev_link_validate);
1533 bool v4l2_subdev_has_pad_interdep(struct media_entity *entity,
1534 unsigned int pad0, unsigned int pad1)
1536 struct v4l2_subdev *sd = media_entity_to_v4l2_subdev(entity);
1537 struct v4l2_subdev_krouting *routing;
1538 struct v4l2_subdev_state *state;
1539 unsigned int i;
1541 state = v4l2_subdev_lock_and_get_active_state(sd);
1543 routing = &state->routing;
1545 for (i = 0; i < routing->num_routes; ++i) {
1546 struct v4l2_subdev_route *route = &routing->routes[i];
1548 if (!(route->flags & V4L2_SUBDEV_ROUTE_FL_ACTIVE))
1549 continue;
1551 if ((route->sink_pad == pad0 && route->source_pad == pad1) ||
1552 (route->source_pad == pad0 && route->sink_pad == pad1)) {
1553 v4l2_subdev_unlock_state(state);
1554 return true;
1558 v4l2_subdev_unlock_state(state);
1560 return false;
1562 EXPORT_SYMBOL_GPL(v4l2_subdev_has_pad_interdep);
1564 struct v4l2_subdev_state *
1565 __v4l2_subdev_state_alloc(struct v4l2_subdev *sd, const char *lock_name,
1566 struct lock_class_key *lock_key)
1568 struct v4l2_subdev_state *state;
1569 int ret;
1571 state = kzalloc(sizeof(*state), GFP_KERNEL);
1572 if (!state)
1573 return ERR_PTR(-ENOMEM);
1575 __mutex_init(&state->_lock, lock_name, lock_key);
1576 if (sd->state_lock)
1577 state->lock = sd->state_lock;
1578 else
1579 state->lock = &state->_lock;
1581 state->sd = sd;
1583 /* Drivers that support streams do not need the legacy pad config */
1584 if (!(sd->flags & V4L2_SUBDEV_FL_STREAMS) && sd->entity.num_pads) {
1585 state->pads = kvcalloc(sd->entity.num_pads,
1586 sizeof(*state->pads), GFP_KERNEL);
1587 if (!state->pads) {
1588 ret = -ENOMEM;
1589 goto err;
1593 if (sd->internal_ops && sd->internal_ops->init_state) {
1595 * There can be no race at this point, but we lock the state
1596 * anyway to satisfy lockdep checks.
1598 v4l2_subdev_lock_state(state);
1599 ret = sd->internal_ops->init_state(sd, state);
1600 v4l2_subdev_unlock_state(state);
1602 if (ret)
1603 goto err;
1606 return state;
1608 err:
1609 if (state && state->pads)
1610 kvfree(state->pads);
1612 kfree(state);
1614 return ERR_PTR(ret);
1616 EXPORT_SYMBOL_GPL(__v4l2_subdev_state_alloc);
1618 void __v4l2_subdev_state_free(struct v4l2_subdev_state *state)
1620 if (!state)
1621 return;
1623 mutex_destroy(&state->_lock);
1625 kfree(state->routing.routes);
1626 kvfree(state->stream_configs.configs);
1627 kvfree(state->pads);
1628 kfree(state);
1630 EXPORT_SYMBOL_GPL(__v4l2_subdev_state_free);
1632 int __v4l2_subdev_init_finalize(struct v4l2_subdev *sd, const char *name,
1633 struct lock_class_key *key)
1635 struct v4l2_subdev_state *state;
1636 struct device *dev = sd->dev;
1637 bool has_disable_streams;
1638 bool has_enable_streams;
1639 bool has_s_stream;
1641 /* Check that the subdevice implements the required features */
1643 has_s_stream = v4l2_subdev_has_op(sd, video, s_stream);
1644 has_enable_streams = v4l2_subdev_has_op(sd, pad, enable_streams);
1645 has_disable_streams = v4l2_subdev_has_op(sd, pad, disable_streams);
1647 if (has_enable_streams != has_disable_streams) {
1648 dev_err(dev,
1649 "subdev '%s' must implement both or neither of .enable_streams() and .disable_streams()\n",
1650 sd->name);
1651 return -EINVAL;
1654 if (sd->flags & V4L2_SUBDEV_FL_STREAMS) {
1655 if (has_s_stream && !has_enable_streams) {
1656 dev_err(dev,
1657 "subdev '%s' must implement .enable/disable_streams()\n",
1658 sd->name);
1660 return -EINVAL;
1664 if (sd->ctrl_handler)
1665 sd->flags |= V4L2_SUBDEV_FL_HAS_EVENTS;
1667 state = __v4l2_subdev_state_alloc(sd, name, key);
1668 if (IS_ERR(state))
1669 return PTR_ERR(state);
1671 sd->active_state = state;
1673 return 0;
1675 EXPORT_SYMBOL_GPL(__v4l2_subdev_init_finalize);
1677 void v4l2_subdev_cleanup(struct v4l2_subdev *sd)
1679 struct v4l2_async_subdev_endpoint *ase, *ase_tmp;
1681 __v4l2_subdev_state_free(sd->active_state);
1682 sd->active_state = NULL;
1684 /* Uninitialised sub-device, bail out here. */
1685 if (!sd->async_subdev_endpoint_list.next)
1686 return;
1688 list_for_each_entry_safe(ase, ase_tmp, &sd->async_subdev_endpoint_list,
1689 async_subdev_endpoint_entry) {
1690 list_del(&ase->async_subdev_endpoint_entry);
1692 kfree(ase);
1695 EXPORT_SYMBOL_GPL(v4l2_subdev_cleanup);
1697 struct v4l2_mbus_framefmt *
1698 __v4l2_subdev_state_get_format(struct v4l2_subdev_state *state,
1699 unsigned int pad, u32 stream)
1701 struct v4l2_subdev_stream_configs *stream_configs;
1702 unsigned int i;
1704 if (WARN_ON_ONCE(!state))
1705 return NULL;
1707 if (state->pads) {
1708 if (stream)
1709 return NULL;
1711 if (pad >= state->sd->entity.num_pads)
1712 return NULL;
1714 return &state->pads[pad].format;
1717 lockdep_assert_held(state->lock);
1719 stream_configs = &state->stream_configs;
1721 for (i = 0; i < stream_configs->num_configs; ++i) {
1722 if (stream_configs->configs[i].pad == pad &&
1723 stream_configs->configs[i].stream == stream)
1724 return &stream_configs->configs[i].fmt;
1727 return NULL;
1729 EXPORT_SYMBOL_GPL(__v4l2_subdev_state_get_format);
1731 struct v4l2_rect *
1732 __v4l2_subdev_state_get_crop(struct v4l2_subdev_state *state, unsigned int pad,
1733 u32 stream)
1735 struct v4l2_subdev_stream_configs *stream_configs;
1736 unsigned int i;
1738 if (WARN_ON_ONCE(!state))
1739 return NULL;
1741 if (state->pads) {
1742 if (stream)
1743 return NULL;
1745 if (pad >= state->sd->entity.num_pads)
1746 return NULL;
1748 return &state->pads[pad].crop;
1751 lockdep_assert_held(state->lock);
1753 stream_configs = &state->stream_configs;
1755 for (i = 0; i < stream_configs->num_configs; ++i) {
1756 if (stream_configs->configs[i].pad == pad &&
1757 stream_configs->configs[i].stream == stream)
1758 return &stream_configs->configs[i].crop;
1761 return NULL;
1763 EXPORT_SYMBOL_GPL(__v4l2_subdev_state_get_crop);
1765 struct v4l2_rect *
1766 __v4l2_subdev_state_get_compose(struct v4l2_subdev_state *state,
1767 unsigned int pad, u32 stream)
1769 struct v4l2_subdev_stream_configs *stream_configs;
1770 unsigned int i;
1772 if (WARN_ON_ONCE(!state))
1773 return NULL;
1775 if (state->pads) {
1776 if (stream)
1777 return NULL;
1779 if (pad >= state->sd->entity.num_pads)
1780 return NULL;
1782 return &state->pads[pad].compose;
1785 lockdep_assert_held(state->lock);
1787 stream_configs = &state->stream_configs;
1789 for (i = 0; i < stream_configs->num_configs; ++i) {
1790 if (stream_configs->configs[i].pad == pad &&
1791 stream_configs->configs[i].stream == stream)
1792 return &stream_configs->configs[i].compose;
1795 return NULL;
1797 EXPORT_SYMBOL_GPL(__v4l2_subdev_state_get_compose);
1799 struct v4l2_fract *
1800 __v4l2_subdev_state_get_interval(struct v4l2_subdev_state *state,
1801 unsigned int pad, u32 stream)
1803 struct v4l2_subdev_stream_configs *stream_configs;
1804 unsigned int i;
1806 if (WARN_ON(!state))
1807 return NULL;
1809 lockdep_assert_held(state->lock);
1811 if (state->pads) {
1812 if (stream)
1813 return NULL;
1815 if (pad >= state->sd->entity.num_pads)
1816 return NULL;
1818 return &state->pads[pad].interval;
1821 lockdep_assert_held(state->lock);
1823 stream_configs = &state->stream_configs;
1825 for (i = 0; i < stream_configs->num_configs; ++i) {
1826 if (stream_configs->configs[i].pad == pad &&
1827 stream_configs->configs[i].stream == stream)
1828 return &stream_configs->configs[i].interval;
1831 return NULL;
1833 EXPORT_SYMBOL_GPL(__v4l2_subdev_state_get_interval);
1835 #if defined(CONFIG_VIDEO_V4L2_SUBDEV_API)
1837 static int
1838 v4l2_subdev_init_stream_configs(struct v4l2_subdev_stream_configs *stream_configs,
1839 const struct v4l2_subdev_krouting *routing)
1841 struct v4l2_subdev_stream_configs new_configs = { 0 };
1842 struct v4l2_subdev_route *route;
1843 u32 idx;
1845 /* Count number of formats needed */
1846 for_each_active_route(routing, route) {
1848 * Each route needs a format on both ends of the route.
1850 new_configs.num_configs += 2;
1853 if (new_configs.num_configs) {
1854 new_configs.configs = kvcalloc(new_configs.num_configs,
1855 sizeof(*new_configs.configs),
1856 GFP_KERNEL);
1858 if (!new_configs.configs)
1859 return -ENOMEM;
1863 * Fill in the 'pad' and stream' value for each item in the array from
1864 * the routing table
1866 idx = 0;
1868 for_each_active_route(routing, route) {
1869 new_configs.configs[idx].pad = route->sink_pad;
1870 new_configs.configs[idx].stream = route->sink_stream;
1872 idx++;
1874 new_configs.configs[idx].pad = route->source_pad;
1875 new_configs.configs[idx].stream = route->source_stream;
1877 idx++;
1880 kvfree(stream_configs->configs);
1881 *stream_configs = new_configs;
1883 return 0;
1886 int v4l2_subdev_get_fmt(struct v4l2_subdev *sd, struct v4l2_subdev_state *state,
1887 struct v4l2_subdev_format *format)
1889 struct v4l2_mbus_framefmt *fmt;
1891 fmt = v4l2_subdev_state_get_format(state, format->pad, format->stream);
1892 if (!fmt)
1893 return -EINVAL;
1895 format->format = *fmt;
1897 return 0;
1899 EXPORT_SYMBOL_GPL(v4l2_subdev_get_fmt);
1901 int v4l2_subdev_get_frame_interval(struct v4l2_subdev *sd,
1902 struct v4l2_subdev_state *state,
1903 struct v4l2_subdev_frame_interval *fi)
1905 struct v4l2_fract *interval;
1907 interval = v4l2_subdev_state_get_interval(state, fi->pad, fi->stream);
1908 if (!interval)
1909 return -EINVAL;
1911 fi->interval = *interval;
1913 return 0;
1915 EXPORT_SYMBOL_GPL(v4l2_subdev_get_frame_interval);
1917 int v4l2_subdev_set_routing(struct v4l2_subdev *sd,
1918 struct v4l2_subdev_state *state,
1919 const struct v4l2_subdev_krouting *routing)
1921 struct v4l2_subdev_krouting *dst = &state->routing;
1922 const struct v4l2_subdev_krouting *src = routing;
1923 struct v4l2_subdev_krouting new_routing = { 0 };
1924 size_t bytes;
1925 int r;
1927 if (unlikely(check_mul_overflow((size_t)src->num_routes,
1928 sizeof(*src->routes), &bytes)))
1929 return -EOVERFLOW;
1931 lockdep_assert_held(state->lock);
1933 if (src->num_routes > 0) {
1934 new_routing.routes = kmemdup(src->routes, bytes, GFP_KERNEL);
1935 if (!new_routing.routes)
1936 return -ENOMEM;
1939 new_routing.num_routes = src->num_routes;
1941 r = v4l2_subdev_init_stream_configs(&state->stream_configs,
1942 &new_routing);
1943 if (r) {
1944 kfree(new_routing.routes);
1945 return r;
1948 kfree(dst->routes);
1949 *dst = new_routing;
1951 return 0;
1953 EXPORT_SYMBOL_GPL(v4l2_subdev_set_routing);
1955 struct v4l2_subdev_route *
1956 __v4l2_subdev_next_active_route(const struct v4l2_subdev_krouting *routing,
1957 struct v4l2_subdev_route *route)
1959 if (route)
1960 ++route;
1961 else
1962 route = &routing->routes[0];
1964 for (; route < routing->routes + routing->num_routes; ++route) {
1965 if (!(route->flags & V4L2_SUBDEV_ROUTE_FL_ACTIVE))
1966 continue;
1968 return route;
1971 return NULL;
1973 EXPORT_SYMBOL_GPL(__v4l2_subdev_next_active_route);
1975 int v4l2_subdev_set_routing_with_fmt(struct v4l2_subdev *sd,
1976 struct v4l2_subdev_state *state,
1977 const struct v4l2_subdev_krouting *routing,
1978 const struct v4l2_mbus_framefmt *fmt)
1980 struct v4l2_subdev_stream_configs *stream_configs;
1981 unsigned int i;
1982 int ret;
1984 ret = v4l2_subdev_set_routing(sd, state, routing);
1985 if (ret)
1986 return ret;
1988 stream_configs = &state->stream_configs;
1990 for (i = 0; i < stream_configs->num_configs; ++i)
1991 stream_configs->configs[i].fmt = *fmt;
1993 return 0;
1995 EXPORT_SYMBOL_GPL(v4l2_subdev_set_routing_with_fmt);
1997 int v4l2_subdev_routing_find_opposite_end(const struct v4l2_subdev_krouting *routing,
1998 u32 pad, u32 stream, u32 *other_pad,
1999 u32 *other_stream)
2001 unsigned int i;
2003 for (i = 0; i < routing->num_routes; ++i) {
2004 struct v4l2_subdev_route *route = &routing->routes[i];
2006 if (route->source_pad == pad &&
2007 route->source_stream == stream) {
2008 if (other_pad)
2009 *other_pad = route->sink_pad;
2010 if (other_stream)
2011 *other_stream = route->sink_stream;
2012 return 0;
2015 if (route->sink_pad == pad && route->sink_stream == stream) {
2016 if (other_pad)
2017 *other_pad = route->source_pad;
2018 if (other_stream)
2019 *other_stream = route->source_stream;
2020 return 0;
2024 return -EINVAL;
2026 EXPORT_SYMBOL_GPL(v4l2_subdev_routing_find_opposite_end);
2028 struct v4l2_mbus_framefmt *
2029 v4l2_subdev_state_get_opposite_stream_format(struct v4l2_subdev_state *state,
2030 u32 pad, u32 stream)
2032 u32 other_pad, other_stream;
2033 int ret;
2035 ret = v4l2_subdev_routing_find_opposite_end(&state->routing,
2036 pad, stream,
2037 &other_pad, &other_stream);
2038 if (ret)
2039 return NULL;
2041 return v4l2_subdev_state_get_format(state, other_pad, other_stream);
2043 EXPORT_SYMBOL_GPL(v4l2_subdev_state_get_opposite_stream_format);
2045 u64 v4l2_subdev_state_xlate_streams(const struct v4l2_subdev_state *state,
2046 u32 pad0, u32 pad1, u64 *streams)
2048 const struct v4l2_subdev_krouting *routing = &state->routing;
2049 struct v4l2_subdev_route *route;
2050 u64 streams0 = 0;
2051 u64 streams1 = 0;
2053 for_each_active_route(routing, route) {
2054 if (route->sink_pad == pad0 && route->source_pad == pad1 &&
2055 (*streams & BIT_ULL(route->sink_stream))) {
2056 streams0 |= BIT_ULL(route->sink_stream);
2057 streams1 |= BIT_ULL(route->source_stream);
2059 if (route->source_pad == pad0 && route->sink_pad == pad1 &&
2060 (*streams & BIT_ULL(route->source_stream))) {
2061 streams0 |= BIT_ULL(route->source_stream);
2062 streams1 |= BIT_ULL(route->sink_stream);
2066 *streams = streams0;
2067 return streams1;
2069 EXPORT_SYMBOL_GPL(v4l2_subdev_state_xlate_streams);
2071 int v4l2_subdev_routing_validate(struct v4l2_subdev *sd,
2072 const struct v4l2_subdev_krouting *routing,
2073 enum v4l2_subdev_routing_restriction disallow)
2075 u32 *remote_pads = NULL;
2076 unsigned int i, j;
2077 int ret = -EINVAL;
2079 if (disallow & (V4L2_SUBDEV_ROUTING_NO_STREAM_MIX |
2080 V4L2_SUBDEV_ROUTING_NO_MULTIPLEXING)) {
2081 remote_pads = kcalloc(sd->entity.num_pads, sizeof(*remote_pads),
2082 GFP_KERNEL);
2083 if (!remote_pads)
2084 return -ENOMEM;
2086 for (i = 0; i < sd->entity.num_pads; ++i)
2087 remote_pads[i] = U32_MAX;
2090 for (i = 0; i < routing->num_routes; ++i) {
2091 const struct v4l2_subdev_route *route = &routing->routes[i];
2093 /* Validate the sink and source pad numbers. */
2094 if (route->sink_pad >= sd->entity.num_pads ||
2095 !(sd->entity.pads[route->sink_pad].flags & MEDIA_PAD_FL_SINK)) {
2096 dev_dbg(sd->dev, "route %u sink (%u) is not a sink pad\n",
2097 i, route->sink_pad);
2098 goto out;
2101 if (route->source_pad >= sd->entity.num_pads ||
2102 !(sd->entity.pads[route->source_pad].flags & MEDIA_PAD_FL_SOURCE)) {
2103 dev_dbg(sd->dev, "route %u source (%u) is not a source pad\n",
2104 i, route->source_pad);
2105 goto out;
2109 * V4L2_SUBDEV_ROUTING_NO_SINK_STREAM_MIX: all streams from a
2110 * sink pad must be routed to a single source pad.
2112 if (disallow & V4L2_SUBDEV_ROUTING_NO_SINK_STREAM_MIX) {
2113 if (remote_pads[route->sink_pad] != U32_MAX &&
2114 remote_pads[route->sink_pad] != route->source_pad) {
2115 dev_dbg(sd->dev,
2116 "route %u attempts to mix %s streams\n",
2117 i, "sink");
2118 goto out;
2123 * V4L2_SUBDEV_ROUTING_NO_SOURCE_STREAM_MIX: all streams on a
2124 * source pad must originate from a single sink pad.
2126 if (disallow & V4L2_SUBDEV_ROUTING_NO_SOURCE_STREAM_MIX) {
2127 if (remote_pads[route->source_pad] != U32_MAX &&
2128 remote_pads[route->source_pad] != route->sink_pad) {
2129 dev_dbg(sd->dev,
2130 "route %u attempts to mix %s streams\n",
2131 i, "source");
2132 goto out;
2137 * V4L2_SUBDEV_ROUTING_NO_SINK_MULTIPLEXING: Pads on the sink
2138 * side can not do stream multiplexing, i.e. there can be only
2139 * a single stream in a sink pad.
2141 if (disallow & V4L2_SUBDEV_ROUTING_NO_SINK_MULTIPLEXING) {
2142 if (remote_pads[route->sink_pad] != U32_MAX) {
2143 dev_dbg(sd->dev,
2144 "route %u attempts to multiplex on %s pad %u\n",
2145 i, "sink", route->sink_pad);
2146 goto out;
2151 * V4L2_SUBDEV_ROUTING_NO_SOURCE_MULTIPLEXING: Pads on the
2152 * source side can not do stream multiplexing, i.e. there can
2153 * be only a single stream in a source pad.
2155 if (disallow & V4L2_SUBDEV_ROUTING_NO_SOURCE_MULTIPLEXING) {
2156 if (remote_pads[route->source_pad] != U32_MAX) {
2157 dev_dbg(sd->dev,
2158 "route %u attempts to multiplex on %s pad %u\n",
2159 i, "source", route->source_pad);
2160 goto out;
2164 if (remote_pads) {
2165 remote_pads[route->sink_pad] = route->source_pad;
2166 remote_pads[route->source_pad] = route->sink_pad;
2169 for (j = i + 1; j < routing->num_routes; ++j) {
2170 const struct v4l2_subdev_route *r = &routing->routes[j];
2173 * V4L2_SUBDEV_ROUTING_NO_1_TO_N: No two routes can
2174 * originate from the same (sink) stream.
2176 if ((disallow & V4L2_SUBDEV_ROUTING_NO_1_TO_N) &&
2177 route->sink_pad == r->sink_pad &&
2178 route->sink_stream == r->sink_stream) {
2179 dev_dbg(sd->dev,
2180 "routes %u and %u originate from same sink (%u/%u)\n",
2181 i, j, route->sink_pad,
2182 route->sink_stream);
2183 goto out;
2187 * V4L2_SUBDEV_ROUTING_NO_N_TO_1: No two routes can end
2188 * at the same (source) stream.
2190 if ((disallow & V4L2_SUBDEV_ROUTING_NO_N_TO_1) &&
2191 route->source_pad == r->source_pad &&
2192 route->source_stream == r->source_stream) {
2193 dev_dbg(sd->dev,
2194 "routes %u and %u end at same source (%u/%u)\n",
2195 i, j, route->source_pad,
2196 route->source_stream);
2197 goto out;
2202 ret = 0;
2204 out:
2205 kfree(remote_pads);
2206 return ret;
2208 EXPORT_SYMBOL_GPL(v4l2_subdev_routing_validate);
2210 static void v4l2_subdev_collect_streams(struct v4l2_subdev *sd,
2211 struct v4l2_subdev_state *state,
2212 u32 pad, u64 streams_mask,
2213 u64 *found_streams,
2214 u64 *enabled_streams)
2216 if (!(sd->flags & V4L2_SUBDEV_FL_STREAMS)) {
2217 *found_streams = BIT_ULL(0);
2218 *enabled_streams =
2219 (sd->enabled_pads & BIT_ULL(pad)) ? BIT_ULL(0) : 0;
2220 return;
2223 *found_streams = 0;
2224 *enabled_streams = 0;
2226 for (unsigned int i = 0; i < state->stream_configs.num_configs; ++i) {
2227 const struct v4l2_subdev_stream_config *cfg =
2228 &state->stream_configs.configs[i];
2230 if (cfg->pad != pad || !(streams_mask & BIT_ULL(cfg->stream)))
2231 continue;
2233 *found_streams |= BIT_ULL(cfg->stream);
2234 if (cfg->enabled)
2235 *enabled_streams |= BIT_ULL(cfg->stream);
2239 static void v4l2_subdev_set_streams_enabled(struct v4l2_subdev *sd,
2240 struct v4l2_subdev_state *state,
2241 u32 pad, u64 streams_mask,
2242 bool enabled)
2244 if (!(sd->flags & V4L2_SUBDEV_FL_STREAMS)) {
2245 if (enabled)
2246 sd->enabled_pads |= BIT_ULL(pad);
2247 else
2248 sd->enabled_pads &= ~BIT_ULL(pad);
2249 return;
2252 for (unsigned int i = 0; i < state->stream_configs.num_configs; ++i) {
2253 struct v4l2_subdev_stream_config *cfg =
2254 &state->stream_configs.configs[i];
2256 if (cfg->pad == pad && (streams_mask & BIT_ULL(cfg->stream)))
2257 cfg->enabled = enabled;
2261 int v4l2_subdev_enable_streams(struct v4l2_subdev *sd, u32 pad,
2262 u64 streams_mask)
2264 struct device *dev = sd->entity.graph_obj.mdev->dev;
2265 struct v4l2_subdev_state *state;
2266 bool already_streaming;
2267 u64 enabled_streams;
2268 u64 found_streams;
2269 bool use_s_stream;
2270 int ret;
2272 /* A few basic sanity checks first. */
2273 if (pad >= sd->entity.num_pads)
2274 return -EINVAL;
2276 if (!(sd->entity.pads[pad].flags & MEDIA_PAD_FL_SOURCE))
2277 return -EOPNOTSUPP;
2280 * We use a 64-bit bitmask for tracking enabled pads, so only subdevices
2281 * with 64 pads or less can be supported.
2283 if (pad >= sizeof(sd->enabled_pads) * BITS_PER_BYTE)
2284 return -EOPNOTSUPP;
2286 if (!streams_mask)
2287 return 0;
2289 /* Fallback on .s_stream() if .enable_streams() isn't available. */
2290 use_s_stream = !v4l2_subdev_has_op(sd, pad, enable_streams);
2292 if (!use_s_stream)
2293 state = v4l2_subdev_lock_and_get_active_state(sd);
2294 else
2295 state = NULL;
2298 * Verify that the requested streams exist and that they are not
2299 * already enabled.
2302 v4l2_subdev_collect_streams(sd, state, pad, streams_mask,
2303 &found_streams, &enabled_streams);
2305 if (found_streams != streams_mask) {
2306 dev_dbg(dev, "streams 0x%llx not found on %s:%u\n",
2307 streams_mask & ~found_streams, sd->entity.name, pad);
2308 ret = -EINVAL;
2309 goto done;
2312 if (enabled_streams) {
2313 dev_dbg(dev, "streams 0x%llx already enabled on %s:%u\n",
2314 enabled_streams, sd->entity.name, pad);
2315 ret = -EALREADY;
2316 goto done;
2319 dev_dbg(dev, "enable streams %u:%#llx\n", pad, streams_mask);
2321 already_streaming = v4l2_subdev_is_streaming(sd);
2323 if (!use_s_stream) {
2324 /* Call the .enable_streams() operation. */
2325 ret = v4l2_subdev_call(sd, pad, enable_streams, state, pad,
2326 streams_mask);
2327 } else {
2328 /* Start streaming when the first pad is enabled. */
2329 if (!already_streaming)
2330 ret = v4l2_subdev_call(sd, video, s_stream, 1);
2331 else
2332 ret = 0;
2335 if (ret) {
2336 dev_dbg(dev, "enable streams %u:%#llx failed: %d\n", pad,
2337 streams_mask, ret);
2338 goto done;
2341 /* Mark the streams as enabled. */
2342 v4l2_subdev_set_streams_enabled(sd, state, pad, streams_mask, true);
2345 * TODO: When all the drivers have been changed to use
2346 * v4l2_subdev_enable_streams() and v4l2_subdev_disable_streams(),
2347 * instead of calling .s_stream() operation directly, we can remove
2348 * the privacy LED handling from call_s_stream() and do it here
2349 * for all cases.
2351 if (!use_s_stream && !already_streaming)
2352 v4l2_subdev_enable_privacy_led(sd);
2354 done:
2355 if (!use_s_stream)
2356 v4l2_subdev_unlock_state(state);
2358 return ret;
2360 EXPORT_SYMBOL_GPL(v4l2_subdev_enable_streams);
2362 int v4l2_subdev_disable_streams(struct v4l2_subdev *sd, u32 pad,
2363 u64 streams_mask)
2365 struct device *dev = sd->entity.graph_obj.mdev->dev;
2366 struct v4l2_subdev_state *state;
2367 u64 enabled_streams;
2368 u64 found_streams;
2369 bool use_s_stream;
2370 int ret;
2372 /* A few basic sanity checks first. */
2373 if (pad >= sd->entity.num_pads)
2374 return -EINVAL;
2376 if (!(sd->entity.pads[pad].flags & MEDIA_PAD_FL_SOURCE))
2377 return -EOPNOTSUPP;
2380 * We use a 64-bit bitmask for tracking enabled pads, so only subdevices
2381 * with 64 pads or less can be supported.
2383 if (pad >= sizeof(sd->enabled_pads) * BITS_PER_BYTE)
2384 return -EOPNOTSUPP;
2386 if (!streams_mask)
2387 return 0;
2389 /* Fallback on .s_stream() if .disable_streams() isn't available. */
2390 use_s_stream = !v4l2_subdev_has_op(sd, pad, disable_streams);
2392 if (!use_s_stream)
2393 state = v4l2_subdev_lock_and_get_active_state(sd);
2394 else
2395 state = NULL;
2398 * Verify that the requested streams exist and that they are not
2399 * already disabled.
2402 v4l2_subdev_collect_streams(sd, state, pad, streams_mask,
2403 &found_streams, &enabled_streams);
2405 if (found_streams != streams_mask) {
2406 dev_dbg(dev, "streams 0x%llx not found on %s:%u\n",
2407 streams_mask & ~found_streams, sd->entity.name, pad);
2408 ret = -EINVAL;
2409 goto done;
2412 if (enabled_streams != streams_mask) {
2413 dev_dbg(dev, "streams 0x%llx already disabled on %s:%u\n",
2414 streams_mask & ~enabled_streams, sd->entity.name, pad);
2415 ret = -EALREADY;
2416 goto done;
2419 dev_dbg(dev, "disable streams %u:%#llx\n", pad, streams_mask);
2421 if (!use_s_stream) {
2422 /* Call the .disable_streams() operation. */
2423 ret = v4l2_subdev_call(sd, pad, disable_streams, state, pad,
2424 streams_mask);
2425 } else {
2426 /* Stop streaming when the last streams are disabled. */
2428 if (!(sd->enabled_pads & ~BIT_ULL(pad)))
2429 ret = v4l2_subdev_call(sd, video, s_stream, 0);
2430 else
2431 ret = 0;
2434 if (ret) {
2435 dev_dbg(dev, "disable streams %u:%#llx failed: %d\n", pad,
2436 streams_mask, ret);
2437 goto done;
2440 v4l2_subdev_set_streams_enabled(sd, state, pad, streams_mask, false);
2442 done:
2443 if (!use_s_stream) {
2444 if (!v4l2_subdev_is_streaming(sd))
2445 v4l2_subdev_disable_privacy_led(sd);
2447 v4l2_subdev_unlock_state(state);
2450 return ret;
2452 EXPORT_SYMBOL_GPL(v4l2_subdev_disable_streams);
2454 int v4l2_subdev_s_stream_helper(struct v4l2_subdev *sd, int enable)
2456 struct v4l2_subdev_state *state;
2457 struct v4l2_subdev_route *route;
2458 struct media_pad *pad;
2459 u64 source_mask = 0;
2460 int pad_index = -1;
2463 * Find the source pad. This helper is meant for subdevs that have a
2464 * single source pad, so failures shouldn't happen, but catch them
2465 * loudly nonetheless as they indicate a driver bug.
2467 media_entity_for_each_pad(&sd->entity, pad) {
2468 if (pad->flags & MEDIA_PAD_FL_SOURCE) {
2469 pad_index = pad->index;
2470 break;
2474 if (WARN_ON(pad_index == -1))
2475 return -EINVAL;
2477 if (sd->flags & V4L2_SUBDEV_FL_STREAMS) {
2479 * As there's a single source pad, just collect all the source
2480 * streams.
2482 state = v4l2_subdev_lock_and_get_active_state(sd);
2484 for_each_active_route(&state->routing, route)
2485 source_mask |= BIT_ULL(route->source_stream);
2487 v4l2_subdev_unlock_state(state);
2488 } else {
2490 * For non-streams subdevices, there's a single implicit stream
2491 * per pad.
2493 source_mask = BIT_ULL(0);
2496 if (enable)
2497 return v4l2_subdev_enable_streams(sd, pad_index, source_mask);
2498 else
2499 return v4l2_subdev_disable_streams(sd, pad_index, source_mask);
2501 EXPORT_SYMBOL_GPL(v4l2_subdev_s_stream_helper);
2503 #endif /* CONFIG_VIDEO_V4L2_SUBDEV_API */
2505 #endif /* CONFIG_MEDIA_CONTROLLER */
2507 void v4l2_subdev_init(struct v4l2_subdev *sd, const struct v4l2_subdev_ops *ops)
2509 INIT_LIST_HEAD(&sd->list);
2510 BUG_ON(!ops);
2511 sd->ops = ops;
2512 sd->v4l2_dev = NULL;
2513 sd->flags = 0;
2514 sd->name[0] = '\0';
2515 sd->grp_id = 0;
2516 sd->dev_priv = NULL;
2517 sd->host_priv = NULL;
2518 sd->privacy_led = NULL;
2519 INIT_LIST_HEAD(&sd->async_subdev_endpoint_list);
2520 #if defined(CONFIG_MEDIA_CONTROLLER)
2521 sd->entity.name = sd->name;
2522 sd->entity.obj_type = MEDIA_ENTITY_TYPE_V4L2_SUBDEV;
2523 sd->entity.function = MEDIA_ENT_F_V4L2_SUBDEV_UNKNOWN;
2524 #endif
2526 EXPORT_SYMBOL(v4l2_subdev_init);
2528 void v4l2_subdev_notify_event(struct v4l2_subdev *sd,
2529 const struct v4l2_event *ev)
2531 v4l2_event_queue(sd->devnode, ev);
2532 v4l2_subdev_notify(sd, V4L2_DEVICE_NOTIFY_EVENT, (void *)ev);
2534 EXPORT_SYMBOL_GPL(v4l2_subdev_notify_event);
2536 bool v4l2_subdev_is_streaming(struct v4l2_subdev *sd)
2538 struct v4l2_subdev_state *state;
2540 if (!v4l2_subdev_has_op(sd, pad, enable_streams))
2541 return sd->s_stream_enabled;
2543 if (!(sd->flags & V4L2_SUBDEV_FL_STREAMS))
2544 return !!sd->enabled_pads;
2546 state = v4l2_subdev_get_locked_active_state(sd);
2548 for (unsigned int i = 0; i < state->stream_configs.num_configs; ++i) {
2549 const struct v4l2_subdev_stream_config *cfg;
2551 cfg = &state->stream_configs.configs[i];
2553 if (cfg->enabled)
2554 return true;
2557 return false;
2559 EXPORT_SYMBOL_GPL(v4l2_subdev_is_streaming);
2561 int v4l2_subdev_get_privacy_led(struct v4l2_subdev *sd)
2563 #if IS_REACHABLE(CONFIG_LEDS_CLASS)
2564 sd->privacy_led = led_get(sd->dev, "privacy-led");
2565 if (IS_ERR(sd->privacy_led) && PTR_ERR(sd->privacy_led) != -ENOENT)
2566 return dev_err_probe(sd->dev, PTR_ERR(sd->privacy_led),
2567 "getting privacy LED\n");
2569 if (!IS_ERR_OR_NULL(sd->privacy_led)) {
2570 mutex_lock(&sd->privacy_led->led_access);
2571 led_sysfs_disable(sd->privacy_led);
2572 led_trigger_remove(sd->privacy_led);
2573 led_set_brightness(sd->privacy_led, 0);
2574 mutex_unlock(&sd->privacy_led->led_access);
2576 #endif
2577 return 0;
2579 EXPORT_SYMBOL_GPL(v4l2_subdev_get_privacy_led);
2581 void v4l2_subdev_put_privacy_led(struct v4l2_subdev *sd)
2583 #if IS_REACHABLE(CONFIG_LEDS_CLASS)
2584 if (!IS_ERR_OR_NULL(sd->privacy_led)) {
2585 mutex_lock(&sd->privacy_led->led_access);
2586 led_sysfs_enable(sd->privacy_led);
2587 mutex_unlock(&sd->privacy_led->led_access);
2588 led_put(sd->privacy_led);
2590 #endif
2592 EXPORT_SYMBOL_GPL(v4l2_subdev_put_privacy_led);