1 // SPDX-License-Identifier: GPL-2.0-only
5 * Copyright (C) 2010 Nokia Corporation
11 #include <linux/export.h>
12 #include <linux/ioctl.h>
13 #include <linux/leds.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;
39 * Maximum stream ID is 63 for now, as we use u64 bitmask to represent a set
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
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);
58 return PTR_ERR(state);
65 static void subdev_fh_free(struct v4l2_subdev_fh *fh)
67 __v4l2_subdev_state_free(fh->state);
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;
78 subdev_fh = kzalloc(sizeof(*subdev_fh), GFP_KERNEL);
79 if (subdev_fh == NULL)
82 ret = subdev_fh_init(subdev_fh, sd);
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) {
95 owner = sd->entity.graph_obj.mdev->dev->driver->owner;
96 if (!try_module_get(owner)) {
100 subdev_fh->owner = owner;
103 if (sd->internal_ops && sd->internal_ops->open) {
104 ret = sd->internal_ops->open(sd, subdev_fh);
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);
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);
133 subdev_fh_free(subdev_fh);
135 file->private_data = NULL;
139 #else /* CONFIG_VIDEO_V4L2_SUBDEV_API */
140 static int subdev_open(struct file *file)
145 static int subdev_close(struct file *file)
149 #endif /* CONFIG_VIDEO_V4L2_SUBDEV_API */
151 static inline int check_which(u32 which)
153 if (which != V4L2_SUBDEV_FORMAT_TRY &&
154 which != V4L2_SUBDEV_FORMAT_ACTIVE)
160 static inline int check_pad(struct v4l2_subdev *sd, u32 pad)
162 #if defined(CONFIG_MEDIA_CONTROLLER)
163 if (sd->entity.num_pads) {
164 if (pad >= sd->entity.num_pads)
169 /* allow pad 0 on subdevices not registered as media entities */
175 static int check_state(struct v4l2_subdev *sd, struct v4l2_subdev_state *state,
176 u32 which, u32 pad, u32 stream)
178 if (sd->flags & V4L2_SUBDEV_FL_STREAMS) {
179 #if defined(CONFIG_VIDEO_V4L2_SUBDEV_API)
180 if (!v4l2_subdev_state_get_stream_format(state, pad, stream))
191 if (which == V4L2_SUBDEV_FORMAT_TRY && (!state || !state->pads))
197 static inline int check_format(struct v4l2_subdev *sd,
198 struct v4l2_subdev_state *state,
199 struct v4l2_subdev_format *format)
204 return check_which(format->which) ? : check_pad(sd, format->pad) ? :
205 check_state(sd, state, format->which, format->pad, format->stream);
208 static int call_get_fmt(struct v4l2_subdev *sd,
209 struct v4l2_subdev_state *state,
210 struct v4l2_subdev_format *format)
212 return check_format(sd, state, format) ? :
213 sd->ops->pad->get_fmt(sd, state, format);
216 static int call_set_fmt(struct v4l2_subdev *sd,
217 struct v4l2_subdev_state *state,
218 struct v4l2_subdev_format *format)
220 return check_format(sd, state, format) ? :
221 sd->ops->pad->set_fmt(sd, state, format);
224 static int call_enum_mbus_code(struct v4l2_subdev *sd,
225 struct v4l2_subdev_state *state,
226 struct v4l2_subdev_mbus_code_enum *code)
231 return check_which(code->which) ? : check_pad(sd, code->pad) ? :
232 check_state(sd, state, code->which, code->pad, code->stream) ? :
233 sd->ops->pad->enum_mbus_code(sd, state, code);
236 static int call_enum_frame_size(struct v4l2_subdev *sd,
237 struct v4l2_subdev_state *state,
238 struct v4l2_subdev_frame_size_enum *fse)
243 return check_which(fse->which) ? : check_pad(sd, fse->pad) ? :
244 check_state(sd, state, fse->which, fse->pad, fse->stream) ? :
245 sd->ops->pad->enum_frame_size(sd, state, fse);
248 static inline int check_frame_interval(struct v4l2_subdev *sd,
249 struct v4l2_subdev_frame_interval *fi)
254 return check_pad(sd, fi->pad);
257 static int call_g_frame_interval(struct v4l2_subdev *sd,
258 struct v4l2_subdev_frame_interval *fi)
260 return check_frame_interval(sd, fi) ? :
261 sd->ops->video->g_frame_interval(sd, fi);
264 static int call_s_frame_interval(struct v4l2_subdev *sd,
265 struct v4l2_subdev_frame_interval *fi)
267 return check_frame_interval(sd, fi) ? :
268 sd->ops->video->s_frame_interval(sd, fi);
271 static int call_enum_frame_interval(struct v4l2_subdev *sd,
272 struct v4l2_subdev_state *state,
273 struct v4l2_subdev_frame_interval_enum *fie)
278 return check_which(fie->which) ? : check_pad(sd, fie->pad) ? :
279 check_state(sd, state, fie->which, fie->pad, fie->stream) ? :
280 sd->ops->pad->enum_frame_interval(sd, state, fie);
283 static inline int check_selection(struct v4l2_subdev *sd,
284 struct v4l2_subdev_state *state,
285 struct v4l2_subdev_selection *sel)
290 return check_which(sel->which) ? : check_pad(sd, sel->pad) ? :
291 check_state(sd, state, sel->which, sel->pad, sel->stream);
294 static int call_get_selection(struct v4l2_subdev *sd,
295 struct v4l2_subdev_state *state,
296 struct v4l2_subdev_selection *sel)
298 return check_selection(sd, state, sel) ? :
299 sd->ops->pad->get_selection(sd, state, sel);
302 static int call_set_selection(struct v4l2_subdev *sd,
303 struct v4l2_subdev_state *state,
304 struct v4l2_subdev_selection *sel)
306 return check_selection(sd, state, sel) ? :
307 sd->ops->pad->set_selection(sd, state, sel);
310 static int call_get_frame_desc(struct v4l2_subdev *sd, unsigned int pad,
311 struct v4l2_mbus_frame_desc *fd)
316 memset(fd, 0, sizeof(*fd));
318 ret = sd->ops->pad->get_frame_desc(sd, pad, fd);
322 dev_dbg(sd->dev, "Frame descriptor on pad %u, type %s\n", pad,
323 fd->type == V4L2_MBUS_FRAME_DESC_TYPE_PARALLEL ? "parallel" :
324 fd->type == V4L2_MBUS_FRAME_DESC_TYPE_CSI2 ? "CSI-2" :
327 for (i = 0; i < fd->num_entries; i++) {
328 struct v4l2_mbus_frame_desc_entry *entry = &fd->entry[i];
331 if (fd->type == V4L2_MBUS_FRAME_DESC_TYPE_CSI2)
332 WARN_ON(snprintf(buf, sizeof(buf),
333 ", vc %u, dt 0x%02x",
335 entry->bus.csi2.dt) >= sizeof(buf));
338 "\tstream %u, code 0x%04x, length %u, flags 0x%04x%s\n",
339 entry->stream, entry->pixelcode, entry->length,
346 static inline int check_edid(struct v4l2_subdev *sd,
347 struct v4l2_subdev_edid *edid)
352 if (edid->blocks && edid->edid == NULL)
355 return check_pad(sd, edid->pad);
358 static int call_get_edid(struct v4l2_subdev *sd, struct v4l2_subdev_edid *edid)
360 return check_edid(sd, edid) ? : sd->ops->pad->get_edid(sd, edid);
363 static int call_set_edid(struct v4l2_subdev *sd, struct v4l2_subdev_edid *edid)
365 return check_edid(sd, edid) ? : sd->ops->pad->set_edid(sd, edid);
368 static int call_dv_timings_cap(struct v4l2_subdev *sd,
369 struct v4l2_dv_timings_cap *cap)
374 return check_pad(sd, cap->pad) ? :
375 sd->ops->pad->dv_timings_cap(sd, cap);
378 static int call_enum_dv_timings(struct v4l2_subdev *sd,
379 struct v4l2_enum_dv_timings *dvt)
384 return check_pad(sd, dvt->pad) ? :
385 sd->ops->pad->enum_dv_timings(sd, dvt);
388 static int call_get_mbus_config(struct v4l2_subdev *sd, unsigned int pad,
389 struct v4l2_mbus_config *config)
391 return check_pad(sd, pad) ? :
392 sd->ops->pad->get_mbus_config(sd, pad, config);
395 static int call_s_stream(struct v4l2_subdev *sd, int enable)
400 * The .s_stream() operation must never be called to start or stop an
401 * already started or stopped subdev. Catch offenders but don't return
402 * an error yet to avoid regressions.
404 * As .s_stream() is mutually exclusive with the .enable_streams() and
405 * .disable_streams() operation, we can use the enabled_streams field
406 * to store the subdev streaming state.
408 if (WARN_ON(!!sd->enabled_streams == !!enable))
411 #if IS_REACHABLE(CONFIG_LEDS_CLASS)
412 if (!IS_ERR_OR_NULL(sd->privacy_led)) {
414 led_set_brightness(sd->privacy_led,
415 sd->privacy_led->max_brightness);
417 led_set_brightness(sd->privacy_led, 0);
420 ret = sd->ops->video->s_stream(sd, enable);
422 if (!enable && ret < 0) {
423 dev_warn(sd->dev, "disabling streaming failed (%d)\n", ret);
428 sd->enabled_streams = enable ? BIT(0) : 0;
433 #ifdef CONFIG_MEDIA_CONTROLLER
435 * Create state-management wrapper for pad ops dealing with subdev state. The
436 * wrapper handles the case where the caller does not provide the called
437 * subdev's state. This should be removed when all the callers are fixed.
439 #define DEFINE_STATE_WRAPPER(f, arg_type) \
440 static int call_##f##_state(struct v4l2_subdev *sd, \
441 struct v4l2_subdev_state *_state, \
444 struct v4l2_subdev_state *state = _state; \
447 state = v4l2_subdev_lock_and_get_active_state(sd); \
448 ret = call_##f(sd, state, arg); \
449 if (!_state && state) \
450 v4l2_subdev_unlock_state(state); \
454 #else /* CONFIG_MEDIA_CONTROLLER */
456 #define DEFINE_STATE_WRAPPER(f, arg_type) \
457 static int call_##f##_state(struct v4l2_subdev *sd, \
458 struct v4l2_subdev_state *state, \
461 return call_##f(sd, state, arg); \
464 #endif /* CONFIG_MEDIA_CONTROLLER */
466 DEFINE_STATE_WRAPPER(get_fmt, struct v4l2_subdev_format);
467 DEFINE_STATE_WRAPPER(set_fmt, struct v4l2_subdev_format);
468 DEFINE_STATE_WRAPPER(enum_mbus_code, struct v4l2_subdev_mbus_code_enum);
469 DEFINE_STATE_WRAPPER(enum_frame_size, struct v4l2_subdev_frame_size_enum);
470 DEFINE_STATE_WRAPPER(enum_frame_interval, struct v4l2_subdev_frame_interval_enum);
471 DEFINE_STATE_WRAPPER(get_selection, struct v4l2_subdev_selection);
472 DEFINE_STATE_WRAPPER(set_selection, struct v4l2_subdev_selection);
474 static const struct v4l2_subdev_pad_ops v4l2_subdev_call_pad_wrappers = {
475 .get_fmt = call_get_fmt_state,
476 .set_fmt = call_set_fmt_state,
477 .enum_mbus_code = call_enum_mbus_code_state,
478 .enum_frame_size = call_enum_frame_size_state,
479 .enum_frame_interval = call_enum_frame_interval_state,
480 .get_selection = call_get_selection_state,
481 .set_selection = call_set_selection_state,
482 .get_edid = call_get_edid,
483 .set_edid = call_set_edid,
484 .dv_timings_cap = call_dv_timings_cap,
485 .enum_dv_timings = call_enum_dv_timings,
486 .get_frame_desc = call_get_frame_desc,
487 .get_mbus_config = call_get_mbus_config,
490 static const struct v4l2_subdev_video_ops v4l2_subdev_call_video_wrappers = {
491 .g_frame_interval = call_g_frame_interval,
492 .s_frame_interval = call_s_frame_interval,
493 .s_stream = call_s_stream,
496 const struct v4l2_subdev_ops v4l2_subdev_call_wrappers = {
497 .pad = &v4l2_subdev_call_pad_wrappers,
498 .video = &v4l2_subdev_call_video_wrappers,
500 EXPORT_SYMBOL(v4l2_subdev_call_wrappers);
502 #if defined(CONFIG_VIDEO_V4L2_SUBDEV_API)
504 static struct v4l2_subdev_state *
505 subdev_ioctl_get_state(struct v4l2_subdev *sd, struct v4l2_subdev_fh *subdev_fh,
506 unsigned int cmd, void *arg)
513 case VIDIOC_SUBDEV_G_FMT:
514 case VIDIOC_SUBDEV_S_FMT:
515 which = ((struct v4l2_subdev_format *)arg)->which;
517 case VIDIOC_SUBDEV_G_CROP:
518 case VIDIOC_SUBDEV_S_CROP:
519 which = ((struct v4l2_subdev_crop *)arg)->which;
521 case VIDIOC_SUBDEV_ENUM_MBUS_CODE:
522 which = ((struct v4l2_subdev_mbus_code_enum *)arg)->which;
524 case VIDIOC_SUBDEV_ENUM_FRAME_SIZE:
525 which = ((struct v4l2_subdev_frame_size_enum *)arg)->which;
527 case VIDIOC_SUBDEV_ENUM_FRAME_INTERVAL:
528 which = ((struct v4l2_subdev_frame_interval_enum *)arg)->which;
530 case VIDIOC_SUBDEV_G_SELECTION:
531 case VIDIOC_SUBDEV_S_SELECTION:
532 which = ((struct v4l2_subdev_selection *)arg)->which;
534 case VIDIOC_SUBDEV_G_ROUTING:
535 case VIDIOC_SUBDEV_S_ROUTING:
536 which = ((struct v4l2_subdev_routing *)arg)->which;
540 return which == V4L2_SUBDEV_FORMAT_TRY ?
542 v4l2_subdev_get_unlocked_active_state(sd);
545 static long subdev_do_ioctl(struct file *file, unsigned int cmd, void *arg,
546 struct v4l2_subdev_state *state)
548 struct video_device *vdev = video_devdata(file);
549 struct v4l2_subdev *sd = vdev_to_v4l2_subdev(vdev);
550 struct v4l2_fh *vfh = file->private_data;
551 struct v4l2_subdev_fh *subdev_fh = to_v4l2_subdev_fh(vfh);
552 bool ro_subdev = test_bit(V4L2_FL_SUBDEV_RO_DEVNODE, &vdev->flags);
553 bool streams_subdev = sd->flags & V4L2_SUBDEV_FL_STREAMS;
554 bool client_supports_streams = subdev_fh->client_caps &
555 V4L2_SUBDEV_CLIENT_CAP_STREAMS;
559 * If the streams API is not enabled, remove V4L2_SUBDEV_CAP_STREAMS.
560 * Remove this when the API is no longer experimental.
562 if (!v4l2_subdev_enable_streams_api)
563 streams_subdev = false;
566 case VIDIOC_SUBDEV_QUERYCAP: {
567 struct v4l2_subdev_capability *cap = arg;
569 memset(cap->reserved, 0, sizeof(cap->reserved));
570 cap->version = LINUX_VERSION_CODE;
572 (ro_subdev ? V4L2_SUBDEV_CAP_RO_SUBDEV : 0) |
573 (streams_subdev ? V4L2_SUBDEV_CAP_STREAMS : 0);
578 case VIDIOC_QUERYCTRL:
580 * TODO: this really should be folded into v4l2_queryctrl (this
581 * currently returns -EINVAL for NULL control handlers).
582 * However, v4l2_queryctrl() is still called directly by
583 * drivers as well and until that has been addressed I believe
584 * it is safer to do the check here. The same is true for the
585 * other control ioctls below.
587 if (!vfh->ctrl_handler)
589 return v4l2_queryctrl(vfh->ctrl_handler, arg);
591 case VIDIOC_QUERY_EXT_CTRL:
592 if (!vfh->ctrl_handler)
594 return v4l2_query_ext_ctrl(vfh->ctrl_handler, arg);
596 case VIDIOC_QUERYMENU:
597 if (!vfh->ctrl_handler)
599 return v4l2_querymenu(vfh->ctrl_handler, arg);
602 if (!vfh->ctrl_handler)
604 return v4l2_g_ctrl(vfh->ctrl_handler, arg);
607 if (!vfh->ctrl_handler)
609 return v4l2_s_ctrl(vfh, vfh->ctrl_handler, arg);
611 case VIDIOC_G_EXT_CTRLS:
612 if (!vfh->ctrl_handler)
614 return v4l2_g_ext_ctrls(vfh->ctrl_handler,
615 vdev, sd->v4l2_dev->mdev, arg);
617 case VIDIOC_S_EXT_CTRLS:
618 if (!vfh->ctrl_handler)
620 return v4l2_s_ext_ctrls(vfh, vfh->ctrl_handler,
621 vdev, sd->v4l2_dev->mdev, arg);
623 case VIDIOC_TRY_EXT_CTRLS:
624 if (!vfh->ctrl_handler)
626 return v4l2_try_ext_ctrls(vfh->ctrl_handler,
627 vdev, sd->v4l2_dev->mdev, arg);
630 if (!(sd->flags & V4L2_SUBDEV_FL_HAS_EVENTS))
633 return v4l2_event_dequeue(vfh, arg, file->f_flags & O_NONBLOCK);
635 case VIDIOC_SUBSCRIBE_EVENT:
636 return v4l2_subdev_call(sd, core, subscribe_event, vfh, arg);
638 case VIDIOC_UNSUBSCRIBE_EVENT:
639 return v4l2_subdev_call(sd, core, unsubscribe_event, vfh, arg);
641 #ifdef CONFIG_VIDEO_ADV_DEBUG
642 case VIDIOC_DBG_G_REGISTER:
644 struct v4l2_dbg_register *p = arg;
646 if (!capable(CAP_SYS_ADMIN))
648 return v4l2_subdev_call(sd, core, g_register, p);
650 case VIDIOC_DBG_S_REGISTER:
652 struct v4l2_dbg_register *p = arg;
654 if (!capable(CAP_SYS_ADMIN))
656 return v4l2_subdev_call(sd, core, s_register, p);
658 case VIDIOC_DBG_G_CHIP_INFO:
660 struct v4l2_dbg_chip_info *p = arg;
662 if (p->match.type != V4L2_CHIP_MATCH_SUBDEV || p->match.addr)
664 if (sd->ops->core && sd->ops->core->s_register)
665 p->flags |= V4L2_CHIP_FL_WRITABLE;
666 if (sd->ops->core && sd->ops->core->g_register)
667 p->flags |= V4L2_CHIP_FL_READABLE;
668 strscpy(p->name, sd->name, sizeof(p->name));
673 case VIDIOC_LOG_STATUS: {
676 pr_info("%s: ================= START STATUS =================\n",
678 ret = v4l2_subdev_call(sd, core, log_status);
679 pr_info("%s: ================== END STATUS ==================\n",
684 case VIDIOC_SUBDEV_G_FMT: {
685 struct v4l2_subdev_format *format = arg;
687 if (!client_supports_streams)
690 memset(format->reserved, 0, sizeof(format->reserved));
691 memset(format->format.reserved, 0, sizeof(format->format.reserved));
692 return v4l2_subdev_call(sd, pad, get_fmt, state, format);
695 case VIDIOC_SUBDEV_S_FMT: {
696 struct v4l2_subdev_format *format = arg;
698 if (format->which != V4L2_SUBDEV_FORMAT_TRY && ro_subdev)
701 if (!client_supports_streams)
704 memset(format->reserved, 0, sizeof(format->reserved));
705 memset(format->format.reserved, 0, sizeof(format->format.reserved));
706 return v4l2_subdev_call(sd, pad, set_fmt, state, format);
709 case VIDIOC_SUBDEV_G_CROP: {
710 struct v4l2_subdev_crop *crop = arg;
711 struct v4l2_subdev_selection sel;
713 if (!client_supports_streams)
716 memset(crop->reserved, 0, sizeof(crop->reserved));
717 memset(&sel, 0, sizeof(sel));
718 sel.which = crop->which;
720 sel.target = V4L2_SEL_TGT_CROP;
722 rval = v4l2_subdev_call(
723 sd, pad, get_selection, state, &sel);
730 case VIDIOC_SUBDEV_S_CROP: {
731 struct v4l2_subdev_crop *crop = arg;
732 struct v4l2_subdev_selection sel;
734 if (crop->which != V4L2_SUBDEV_FORMAT_TRY && ro_subdev)
737 if (!client_supports_streams)
740 memset(crop->reserved, 0, sizeof(crop->reserved));
741 memset(&sel, 0, sizeof(sel));
742 sel.which = crop->which;
744 sel.target = V4L2_SEL_TGT_CROP;
747 rval = v4l2_subdev_call(
748 sd, pad, set_selection, state, &sel);
755 case VIDIOC_SUBDEV_ENUM_MBUS_CODE: {
756 struct v4l2_subdev_mbus_code_enum *code = arg;
758 if (!client_supports_streams)
761 memset(code->reserved, 0, sizeof(code->reserved));
762 return v4l2_subdev_call(sd, pad, enum_mbus_code, state,
766 case VIDIOC_SUBDEV_ENUM_FRAME_SIZE: {
767 struct v4l2_subdev_frame_size_enum *fse = arg;
769 if (!client_supports_streams)
772 memset(fse->reserved, 0, sizeof(fse->reserved));
773 return v4l2_subdev_call(sd, pad, enum_frame_size, state,
777 case VIDIOC_SUBDEV_G_FRAME_INTERVAL: {
778 struct v4l2_subdev_frame_interval *fi = arg;
780 if (!client_supports_streams)
783 memset(fi->reserved, 0, sizeof(fi->reserved));
784 return v4l2_subdev_call(sd, video, g_frame_interval, arg);
787 case VIDIOC_SUBDEV_S_FRAME_INTERVAL: {
788 struct v4l2_subdev_frame_interval *fi = arg;
793 if (!client_supports_streams)
796 memset(fi->reserved, 0, sizeof(fi->reserved));
797 return v4l2_subdev_call(sd, video, s_frame_interval, arg);
800 case VIDIOC_SUBDEV_ENUM_FRAME_INTERVAL: {
801 struct v4l2_subdev_frame_interval_enum *fie = arg;
803 if (!client_supports_streams)
806 memset(fie->reserved, 0, sizeof(fie->reserved));
807 return v4l2_subdev_call(sd, pad, enum_frame_interval, state,
811 case VIDIOC_SUBDEV_G_SELECTION: {
812 struct v4l2_subdev_selection *sel = arg;
814 if (!client_supports_streams)
817 memset(sel->reserved, 0, sizeof(sel->reserved));
818 return v4l2_subdev_call(
819 sd, pad, get_selection, state, sel);
822 case VIDIOC_SUBDEV_S_SELECTION: {
823 struct v4l2_subdev_selection *sel = arg;
825 if (sel->which != V4L2_SUBDEV_FORMAT_TRY && ro_subdev)
828 if (!client_supports_streams)
831 memset(sel->reserved, 0, sizeof(sel->reserved));
832 return v4l2_subdev_call(
833 sd, pad, set_selection, state, sel);
836 case VIDIOC_G_EDID: {
837 struct v4l2_subdev_edid *edid = arg;
839 return v4l2_subdev_call(sd, pad, get_edid, edid);
842 case VIDIOC_S_EDID: {
843 struct v4l2_subdev_edid *edid = arg;
845 return v4l2_subdev_call(sd, pad, set_edid, edid);
848 case VIDIOC_SUBDEV_DV_TIMINGS_CAP: {
849 struct v4l2_dv_timings_cap *cap = arg;
851 return v4l2_subdev_call(sd, pad, dv_timings_cap, cap);
854 case VIDIOC_SUBDEV_ENUM_DV_TIMINGS: {
855 struct v4l2_enum_dv_timings *dvt = arg;
857 return v4l2_subdev_call(sd, pad, enum_dv_timings, dvt);
860 case VIDIOC_SUBDEV_QUERY_DV_TIMINGS:
861 return v4l2_subdev_call(sd, video, query_dv_timings, arg);
863 case VIDIOC_SUBDEV_G_DV_TIMINGS:
864 return v4l2_subdev_call(sd, video, g_dv_timings, arg);
866 case VIDIOC_SUBDEV_S_DV_TIMINGS:
870 return v4l2_subdev_call(sd, video, s_dv_timings, arg);
872 case VIDIOC_SUBDEV_G_STD:
873 return v4l2_subdev_call(sd, video, g_std, arg);
875 case VIDIOC_SUBDEV_S_STD: {
876 v4l2_std_id *std = arg;
881 return v4l2_subdev_call(sd, video, s_std, *std);
884 case VIDIOC_SUBDEV_ENUMSTD: {
885 struct v4l2_standard *p = arg;
888 if (v4l2_subdev_call(sd, video, g_tvnorms, &id))
891 return v4l_video_std_enumstd(p, id);
894 case VIDIOC_SUBDEV_QUERYSTD:
895 return v4l2_subdev_call(sd, video, querystd, arg);
897 case VIDIOC_SUBDEV_G_ROUTING: {
898 struct v4l2_subdev_routing *routing = arg;
899 struct v4l2_subdev_krouting *krouting;
901 if (!v4l2_subdev_enable_streams_api)
904 if (!(sd->flags & V4L2_SUBDEV_FL_STREAMS))
907 memset(routing->reserved, 0, sizeof(routing->reserved));
909 krouting = &state->routing;
911 if (routing->num_routes < krouting->num_routes) {
912 routing->num_routes = krouting->num_routes;
916 memcpy((struct v4l2_subdev_route *)(uintptr_t)routing->routes,
918 krouting->num_routes * sizeof(*krouting->routes));
919 routing->num_routes = krouting->num_routes;
924 case VIDIOC_SUBDEV_S_ROUTING: {
925 struct v4l2_subdev_routing *routing = arg;
926 struct v4l2_subdev_route *routes =
927 (struct v4l2_subdev_route *)(uintptr_t)routing->routes;
928 struct v4l2_subdev_krouting krouting = {};
931 if (!v4l2_subdev_enable_streams_api)
934 if (!(sd->flags & V4L2_SUBDEV_FL_STREAMS))
937 if (routing->which != V4L2_SUBDEV_FORMAT_TRY && ro_subdev)
940 memset(routing->reserved, 0, sizeof(routing->reserved));
942 for (i = 0; i < routing->num_routes; ++i) {
943 const struct v4l2_subdev_route *route = &routes[i];
944 const struct media_pad *pads = sd->entity.pads;
946 if (route->sink_stream > V4L2_SUBDEV_MAX_STREAM_ID ||
947 route->source_stream > V4L2_SUBDEV_MAX_STREAM_ID)
950 if (route->sink_pad >= sd->entity.num_pads)
953 if (!(pads[route->sink_pad].flags &
957 if (route->source_pad >= sd->entity.num_pads)
960 if (!(pads[route->source_pad].flags &
961 MEDIA_PAD_FL_SOURCE))
965 krouting.num_routes = routing->num_routes;
966 krouting.routes = routes;
968 return v4l2_subdev_call(sd, pad, set_routing, state,
969 routing->which, &krouting);
972 case VIDIOC_SUBDEV_G_CLIENT_CAP: {
973 struct v4l2_subdev_client_capability *client_cap = arg;
975 client_cap->capabilities = subdev_fh->client_caps;
980 case VIDIOC_SUBDEV_S_CLIENT_CAP: {
981 struct v4l2_subdev_client_capability *client_cap = arg;
984 * Clear V4L2_SUBDEV_CLIENT_CAP_STREAMS if streams API is not
985 * enabled. Remove this when streams API is no longer
988 if (!v4l2_subdev_enable_streams_api)
989 client_cap->capabilities &= ~V4L2_SUBDEV_CLIENT_CAP_STREAMS;
991 /* Filter out unsupported capabilities */
992 client_cap->capabilities &= V4L2_SUBDEV_CLIENT_CAP_STREAMS;
994 subdev_fh->client_caps = client_cap->capabilities;
1000 return v4l2_subdev_call(sd, core, ioctl, cmd, arg);
1006 static long subdev_do_ioctl_lock(struct file *file, unsigned int cmd, void *arg)
1008 struct video_device *vdev = video_devdata(file);
1009 struct mutex *lock = vdev->lock;
1012 if (lock && mutex_lock_interruptible(lock))
1013 return -ERESTARTSYS;
1015 if (video_is_registered(vdev)) {
1016 struct v4l2_subdev *sd = vdev_to_v4l2_subdev(vdev);
1017 struct v4l2_fh *vfh = file->private_data;
1018 struct v4l2_subdev_fh *subdev_fh = to_v4l2_subdev_fh(vfh);
1019 struct v4l2_subdev_state *state;
1021 state = subdev_ioctl_get_state(sd, subdev_fh, cmd, arg);
1024 v4l2_subdev_lock_state(state);
1026 ret = subdev_do_ioctl(file, cmd, arg, state);
1029 v4l2_subdev_unlock_state(state);
1037 static long subdev_ioctl(struct file *file, unsigned int cmd,
1040 return video_usercopy(file, cmd, arg, subdev_do_ioctl_lock);
1043 #ifdef CONFIG_COMPAT
1044 static long subdev_compat_ioctl32(struct file *file, unsigned int cmd,
1047 struct video_device *vdev = video_devdata(file);
1048 struct v4l2_subdev *sd = vdev_to_v4l2_subdev(vdev);
1050 return v4l2_subdev_call(sd, core, compat_ioctl32, cmd, arg);
1054 #else /* CONFIG_VIDEO_V4L2_SUBDEV_API */
1055 static long subdev_ioctl(struct file *file, unsigned int cmd,
1061 #ifdef CONFIG_COMPAT
1062 static long subdev_compat_ioctl32(struct file *file, unsigned int cmd,
1068 #endif /* CONFIG_VIDEO_V4L2_SUBDEV_API */
1070 static __poll_t subdev_poll(struct file *file, poll_table *wait)
1072 struct video_device *vdev = video_devdata(file);
1073 struct v4l2_subdev *sd = vdev_to_v4l2_subdev(vdev);
1074 struct v4l2_fh *fh = file->private_data;
1076 if (!(sd->flags & V4L2_SUBDEV_FL_HAS_EVENTS))
1079 poll_wait(file, &fh->wait, wait);
1081 if (v4l2_event_pending(fh))
1087 const struct v4l2_file_operations v4l2_subdev_fops = {
1088 .owner = THIS_MODULE,
1089 .open = subdev_open,
1090 .unlocked_ioctl = subdev_ioctl,
1091 #ifdef CONFIG_COMPAT
1092 .compat_ioctl32 = subdev_compat_ioctl32,
1094 .release = subdev_close,
1095 .poll = subdev_poll,
1098 #ifdef CONFIG_MEDIA_CONTROLLER
1100 int v4l2_subdev_get_fwnode_pad_1_to_1(struct media_entity *entity,
1101 struct fwnode_endpoint *endpoint)
1103 struct fwnode_handle *fwnode;
1104 struct v4l2_subdev *sd;
1106 if (!is_media_entity_v4l2_subdev(entity))
1109 sd = media_entity_to_v4l2_subdev(entity);
1111 fwnode = fwnode_graph_get_port_parent(endpoint->local_fwnode);
1112 fwnode_handle_put(fwnode);
1114 if (device_match_fwnode(sd->dev, fwnode))
1115 return endpoint->port;
1119 EXPORT_SYMBOL_GPL(v4l2_subdev_get_fwnode_pad_1_to_1);
1121 int v4l2_subdev_link_validate_default(struct v4l2_subdev *sd,
1122 struct media_link *link,
1123 struct v4l2_subdev_format *source_fmt,
1124 struct v4l2_subdev_format *sink_fmt)
1128 /* The width, height and code must match. */
1129 if (source_fmt->format.width != sink_fmt->format.width) {
1130 dev_dbg(sd->entity.graph_obj.mdev->dev,
1131 "%s: width does not match (source %u, sink %u)\n",
1133 source_fmt->format.width, sink_fmt->format.width);
1137 if (source_fmt->format.height != sink_fmt->format.height) {
1138 dev_dbg(sd->entity.graph_obj.mdev->dev,
1139 "%s: height does not match (source %u, sink %u)\n",
1141 source_fmt->format.height, sink_fmt->format.height);
1145 if (source_fmt->format.code != sink_fmt->format.code) {
1146 dev_dbg(sd->entity.graph_obj.mdev->dev,
1147 "%s: media bus code does not match (source 0x%8.8x, sink 0x%8.8x)\n",
1149 source_fmt->format.code, sink_fmt->format.code);
1153 /* The field order must match, or the sink field order must be NONE
1154 * to support interlaced hardware connected to bridges that support
1155 * progressive formats only.
1157 if (source_fmt->format.field != sink_fmt->format.field &&
1158 sink_fmt->format.field != V4L2_FIELD_NONE) {
1159 dev_dbg(sd->entity.graph_obj.mdev->dev,
1160 "%s: field does not match (source %u, sink %u)\n",
1162 source_fmt->format.field, sink_fmt->format.field);
1169 dev_dbg(sd->entity.graph_obj.mdev->dev,
1170 "%s: link was \"%s\":%u -> \"%s\":%u\n", __func__,
1171 link->source->entity->name, link->source->index,
1172 link->sink->entity->name, link->sink->index);
1176 EXPORT_SYMBOL_GPL(v4l2_subdev_link_validate_default);
1179 v4l2_subdev_link_validate_get_format(struct media_pad *pad, u32 stream,
1180 struct v4l2_subdev_format *fmt,
1183 struct v4l2_subdev_state *state;
1184 struct v4l2_subdev *sd;
1187 if (!is_media_entity_v4l2_subdev(pad->entity)) {
1188 WARN(pad->entity->function != MEDIA_ENT_F_IO_V4L,
1189 "Driver bug! Wrong media entity type 0x%08x, entity %s\n",
1190 pad->entity->function, pad->entity->name);
1195 sd = media_entity_to_v4l2_subdev(pad->entity);
1197 fmt->which = V4L2_SUBDEV_FORMAT_ACTIVE;
1198 fmt->pad = pad->index;
1199 fmt->stream = stream;
1202 state = v4l2_subdev_get_locked_active_state(sd);
1204 state = v4l2_subdev_lock_and_get_active_state(sd);
1206 ret = v4l2_subdev_call(sd, pad, get_fmt, state, fmt);
1208 if (!states_locked && state)
1209 v4l2_subdev_unlock_state(state);
1214 #if defined(CONFIG_VIDEO_V4L2_SUBDEV_API)
1216 static void __v4l2_link_validate_get_streams(struct media_pad *pad,
1220 struct v4l2_subdev_route *route;
1221 struct v4l2_subdev_state *state;
1222 struct v4l2_subdev *subdev;
1224 subdev = media_entity_to_v4l2_subdev(pad->entity);
1229 state = v4l2_subdev_get_locked_active_state(subdev);
1231 state = v4l2_subdev_lock_and_get_active_state(subdev);
1233 if (WARN_ON(!state))
1236 for_each_active_route(&state->routing, route) {
1240 if (pad->flags & MEDIA_PAD_FL_SOURCE) {
1241 route_pad = route->source_pad;
1242 route_stream = route->source_stream;
1244 route_pad = route->sink_pad;
1245 route_stream = route->sink_stream;
1248 if (route_pad != pad->index)
1251 *streams_mask |= BIT_ULL(route_stream);
1255 v4l2_subdev_unlock_state(state);
1258 #endif /* CONFIG_VIDEO_V4L2_SUBDEV_API */
1260 static void v4l2_link_validate_get_streams(struct media_pad *pad,
1264 struct v4l2_subdev *subdev = media_entity_to_v4l2_subdev(pad->entity);
1266 if (!(subdev->flags & V4L2_SUBDEV_FL_STREAMS)) {
1267 /* Non-streams subdevs have an implicit stream 0 */
1268 *streams_mask = BIT_ULL(0);
1272 #if defined(CONFIG_VIDEO_V4L2_SUBDEV_API)
1273 __v4l2_link_validate_get_streams(pad, streams_mask, states_locked);
1275 /* This shouldn't happen */
1280 static int v4l2_subdev_link_validate_locked(struct media_link *link, bool states_locked)
1282 struct v4l2_subdev *sink_subdev =
1283 media_entity_to_v4l2_subdev(link->sink->entity);
1284 struct device *dev = sink_subdev->entity.graph_obj.mdev->dev;
1285 u64 source_streams_mask;
1286 u64 sink_streams_mask;
1287 u64 dangling_sink_streams;
1291 dev_dbg(dev, "validating link \"%s\":%u -> \"%s\":%u\n",
1292 link->source->entity->name, link->source->index,
1293 link->sink->entity->name, link->sink->index);
1295 v4l2_link_validate_get_streams(link->source, &source_streams_mask, states_locked);
1296 v4l2_link_validate_get_streams(link->sink, &sink_streams_mask, states_locked);
1299 * It is ok to have more source streams than sink streams as extra
1300 * source streams can just be ignored by the receiver, but having extra
1301 * sink streams is an error as streams must have a source.
1303 dangling_sink_streams = (source_streams_mask ^ sink_streams_mask) &
1305 if (dangling_sink_streams) {
1306 dev_err(dev, "Dangling sink streams: mask %#llx\n",
1307 dangling_sink_streams);
1311 /* Validate source and sink stream formats */
1313 for (stream = 0; stream < sizeof(sink_streams_mask) * 8; ++stream) {
1314 struct v4l2_subdev_format sink_fmt, source_fmt;
1316 if (!(sink_streams_mask & BIT_ULL(stream)))
1319 dev_dbg(dev, "validating stream \"%s\":%u:%u -> \"%s\":%u:%u\n",
1320 link->source->entity->name, link->source->index, stream,
1321 link->sink->entity->name, link->sink->index, stream);
1323 ret = v4l2_subdev_link_validate_get_format(link->source, stream,
1324 &source_fmt, states_locked);
1327 "Failed to get format for \"%s\":%u:%u (but that's ok)\n",
1328 link->source->entity->name, link->source->index,
1333 ret = v4l2_subdev_link_validate_get_format(link->sink, stream,
1334 &sink_fmt, states_locked);
1337 "Failed to get format for \"%s\":%u:%u (but that's ok)\n",
1338 link->sink->entity->name, link->sink->index,
1343 /* TODO: add stream number to link_validate() */
1344 ret = v4l2_subdev_call(sink_subdev, pad, link_validate, link,
1345 &source_fmt, &sink_fmt);
1349 if (ret != -ENOIOCTLCMD)
1352 ret = v4l2_subdev_link_validate_default(sink_subdev, link,
1353 &source_fmt, &sink_fmt);
1362 int v4l2_subdev_link_validate(struct media_link *link)
1364 struct v4l2_subdev *source_sd, *sink_sd;
1365 struct v4l2_subdev_state *source_state, *sink_state;
1369 if (!is_media_entity_v4l2_subdev(link->sink->entity) ||
1370 !is_media_entity_v4l2_subdev(link->source->entity)) {
1371 pr_warn_once("%s of link '%s':%u->'%s':%u is not a V4L2 sub-device, driver bug!\n",
1372 !is_media_entity_v4l2_subdev(link->sink->entity) ?
1374 link->source->entity->name, link->source->index,
1375 link->sink->entity->name, link->sink->index);
1379 sink_sd = media_entity_to_v4l2_subdev(link->sink->entity);
1380 source_sd = media_entity_to_v4l2_subdev(link->source->entity);
1382 sink_state = v4l2_subdev_get_unlocked_active_state(sink_sd);
1383 source_state = v4l2_subdev_get_unlocked_active_state(source_sd);
1385 states_locked = sink_state && source_state;
1387 if (states_locked) {
1388 v4l2_subdev_lock_state(sink_state);
1389 v4l2_subdev_lock_state(source_state);
1392 ret = v4l2_subdev_link_validate_locked(link, states_locked);
1394 if (states_locked) {
1395 v4l2_subdev_unlock_state(sink_state);
1396 v4l2_subdev_unlock_state(source_state);
1401 EXPORT_SYMBOL_GPL(v4l2_subdev_link_validate);
1403 bool v4l2_subdev_has_pad_interdep(struct media_entity *entity,
1404 unsigned int pad0, unsigned int pad1)
1406 struct v4l2_subdev *sd = media_entity_to_v4l2_subdev(entity);
1407 struct v4l2_subdev_krouting *routing;
1408 struct v4l2_subdev_state *state;
1411 state = v4l2_subdev_lock_and_get_active_state(sd);
1413 routing = &state->routing;
1415 for (i = 0; i < routing->num_routes; ++i) {
1416 struct v4l2_subdev_route *route = &routing->routes[i];
1418 if (!(route->flags & V4L2_SUBDEV_ROUTE_FL_ACTIVE))
1421 if ((route->sink_pad == pad0 && route->source_pad == pad1) ||
1422 (route->source_pad == pad0 && route->sink_pad == pad1)) {
1423 v4l2_subdev_unlock_state(state);
1428 v4l2_subdev_unlock_state(state);
1432 EXPORT_SYMBOL_GPL(v4l2_subdev_has_pad_interdep);
1434 struct v4l2_subdev_state *
1435 __v4l2_subdev_state_alloc(struct v4l2_subdev *sd, const char *lock_name,
1436 struct lock_class_key *lock_key)
1438 struct v4l2_subdev_state *state;
1441 state = kzalloc(sizeof(*state), GFP_KERNEL);
1443 return ERR_PTR(-ENOMEM);
1445 __mutex_init(&state->_lock, lock_name, lock_key);
1447 state->lock = sd->state_lock;
1449 state->lock = &state->_lock;
1451 /* Drivers that support streams do not need the legacy pad config */
1452 if (!(sd->flags & V4L2_SUBDEV_FL_STREAMS) && sd->entity.num_pads) {
1453 state->pads = kvcalloc(sd->entity.num_pads,
1454 sizeof(*state->pads), GFP_KERNEL);
1462 * There can be no race at this point, but we lock the state anyway to
1463 * satisfy lockdep checks.
1465 v4l2_subdev_lock_state(state);
1466 ret = v4l2_subdev_call(sd, pad, init_cfg, state);
1467 v4l2_subdev_unlock_state(state);
1469 if (ret < 0 && ret != -ENOIOCTLCMD)
1475 if (state && state->pads)
1476 kvfree(state->pads);
1480 return ERR_PTR(ret);
1482 EXPORT_SYMBOL_GPL(__v4l2_subdev_state_alloc);
1484 void __v4l2_subdev_state_free(struct v4l2_subdev_state *state)
1489 mutex_destroy(&state->_lock);
1491 kfree(state->routing.routes);
1492 kvfree(state->stream_configs.configs);
1493 kvfree(state->pads);
1496 EXPORT_SYMBOL_GPL(__v4l2_subdev_state_free);
1498 int __v4l2_subdev_init_finalize(struct v4l2_subdev *sd, const char *name,
1499 struct lock_class_key *key)
1501 struct v4l2_subdev_state *state;
1503 state = __v4l2_subdev_state_alloc(sd, name, key);
1505 return PTR_ERR(state);
1507 sd->active_state = state;
1511 EXPORT_SYMBOL_GPL(__v4l2_subdev_init_finalize);
1513 void v4l2_subdev_cleanup(struct v4l2_subdev *sd)
1515 struct v4l2_async_subdev_endpoint *ase, *ase_tmp;
1517 __v4l2_subdev_state_free(sd->active_state);
1518 sd->active_state = NULL;
1520 if (list_empty(&sd->async_subdev_endpoint_list))
1523 list_for_each_entry_safe(ase, ase_tmp, &sd->async_subdev_endpoint_list,
1524 async_subdev_endpoint_entry) {
1525 list_del(&ase->async_subdev_endpoint_entry);
1530 EXPORT_SYMBOL_GPL(v4l2_subdev_cleanup);
1532 #if defined(CONFIG_VIDEO_V4L2_SUBDEV_API)
1535 v4l2_subdev_init_stream_configs(struct v4l2_subdev_stream_configs *stream_configs,
1536 const struct v4l2_subdev_krouting *routing)
1538 struct v4l2_subdev_stream_configs new_configs = { 0 };
1539 struct v4l2_subdev_route *route;
1542 /* Count number of formats needed */
1543 for_each_active_route(routing, route) {
1545 * Each route needs a format on both ends of the route.
1547 new_configs.num_configs += 2;
1550 if (new_configs.num_configs) {
1551 new_configs.configs = kvcalloc(new_configs.num_configs,
1552 sizeof(*new_configs.configs),
1555 if (!new_configs.configs)
1560 * Fill in the 'pad' and stream' value for each item in the array from
1565 for_each_active_route(routing, route) {
1566 new_configs.configs[idx].pad = route->sink_pad;
1567 new_configs.configs[idx].stream = route->sink_stream;
1571 new_configs.configs[idx].pad = route->source_pad;
1572 new_configs.configs[idx].stream = route->source_stream;
1577 kvfree(stream_configs->configs);
1578 *stream_configs = new_configs;
1583 int v4l2_subdev_get_fmt(struct v4l2_subdev *sd, struct v4l2_subdev_state *state,
1584 struct v4l2_subdev_format *format)
1586 struct v4l2_mbus_framefmt *fmt;
1588 if (sd->flags & V4L2_SUBDEV_FL_STREAMS)
1589 fmt = v4l2_subdev_state_get_stream_format(state, format->pad,
1591 else if (format->pad < sd->entity.num_pads && format->stream == 0)
1592 fmt = v4l2_subdev_get_pad_format(sd, state, format->pad);
1599 format->format = *fmt;
1603 EXPORT_SYMBOL_GPL(v4l2_subdev_get_fmt);
1605 int v4l2_subdev_set_routing(struct v4l2_subdev *sd,
1606 struct v4l2_subdev_state *state,
1607 const struct v4l2_subdev_krouting *routing)
1609 struct v4l2_subdev_krouting *dst = &state->routing;
1610 const struct v4l2_subdev_krouting *src = routing;
1611 struct v4l2_subdev_krouting new_routing = { 0 };
1615 if (unlikely(check_mul_overflow((size_t)src->num_routes,
1616 sizeof(*src->routes), &bytes)))
1619 lockdep_assert_held(state->lock);
1621 if (src->num_routes > 0) {
1622 new_routing.routes = kmemdup(src->routes, bytes, GFP_KERNEL);
1623 if (!new_routing.routes)
1627 new_routing.num_routes = src->num_routes;
1629 r = v4l2_subdev_init_stream_configs(&state->stream_configs,
1632 kfree(new_routing.routes);
1641 EXPORT_SYMBOL_GPL(v4l2_subdev_set_routing);
1643 struct v4l2_subdev_route *
1644 __v4l2_subdev_next_active_route(const struct v4l2_subdev_krouting *routing,
1645 struct v4l2_subdev_route *route)
1650 route = &routing->routes[0];
1652 for (; route < routing->routes + routing->num_routes; ++route) {
1653 if (!(route->flags & V4L2_SUBDEV_ROUTE_FL_ACTIVE))
1661 EXPORT_SYMBOL_GPL(__v4l2_subdev_next_active_route);
1663 int v4l2_subdev_set_routing_with_fmt(struct v4l2_subdev *sd,
1664 struct v4l2_subdev_state *state,
1665 const struct v4l2_subdev_krouting *routing,
1666 const struct v4l2_mbus_framefmt *fmt)
1668 struct v4l2_subdev_stream_configs *stream_configs;
1672 ret = v4l2_subdev_set_routing(sd, state, routing);
1676 stream_configs = &state->stream_configs;
1678 for (i = 0; i < stream_configs->num_configs; ++i)
1679 stream_configs->configs[i].fmt = *fmt;
1683 EXPORT_SYMBOL_GPL(v4l2_subdev_set_routing_with_fmt);
1685 struct v4l2_mbus_framefmt *
1686 v4l2_subdev_state_get_stream_format(struct v4l2_subdev_state *state,
1687 unsigned int pad, u32 stream)
1689 struct v4l2_subdev_stream_configs *stream_configs;
1692 lockdep_assert_held(state->lock);
1694 stream_configs = &state->stream_configs;
1696 for (i = 0; i < stream_configs->num_configs; ++i) {
1697 if (stream_configs->configs[i].pad == pad &&
1698 stream_configs->configs[i].stream == stream)
1699 return &stream_configs->configs[i].fmt;
1704 EXPORT_SYMBOL_GPL(v4l2_subdev_state_get_stream_format);
1707 v4l2_subdev_state_get_stream_crop(struct v4l2_subdev_state *state,
1708 unsigned int pad, u32 stream)
1710 struct v4l2_subdev_stream_configs *stream_configs;
1713 lockdep_assert_held(state->lock);
1715 stream_configs = &state->stream_configs;
1717 for (i = 0; i < stream_configs->num_configs; ++i) {
1718 if (stream_configs->configs[i].pad == pad &&
1719 stream_configs->configs[i].stream == stream)
1720 return &stream_configs->configs[i].crop;
1725 EXPORT_SYMBOL_GPL(v4l2_subdev_state_get_stream_crop);
1728 v4l2_subdev_state_get_stream_compose(struct v4l2_subdev_state *state,
1729 unsigned int pad, u32 stream)
1731 struct v4l2_subdev_stream_configs *stream_configs;
1734 lockdep_assert_held(state->lock);
1736 stream_configs = &state->stream_configs;
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;
1746 EXPORT_SYMBOL_GPL(v4l2_subdev_state_get_stream_compose);
1748 int v4l2_subdev_routing_find_opposite_end(const struct v4l2_subdev_krouting *routing,
1749 u32 pad, u32 stream, u32 *other_pad,
1754 for (i = 0; i < routing->num_routes; ++i) {
1755 struct v4l2_subdev_route *route = &routing->routes[i];
1757 if (route->source_pad == pad &&
1758 route->source_stream == stream) {
1760 *other_pad = route->sink_pad;
1762 *other_stream = route->sink_stream;
1766 if (route->sink_pad == pad && route->sink_stream == stream) {
1768 *other_pad = route->source_pad;
1770 *other_stream = route->source_stream;
1777 EXPORT_SYMBOL_GPL(v4l2_subdev_routing_find_opposite_end);
1779 struct v4l2_mbus_framefmt *
1780 v4l2_subdev_state_get_opposite_stream_format(struct v4l2_subdev_state *state,
1781 u32 pad, u32 stream)
1783 u32 other_pad, other_stream;
1786 ret = v4l2_subdev_routing_find_opposite_end(&state->routing,
1788 &other_pad, &other_stream);
1792 return v4l2_subdev_state_get_stream_format(state, other_pad,
1795 EXPORT_SYMBOL_GPL(v4l2_subdev_state_get_opposite_stream_format);
1797 u64 v4l2_subdev_state_xlate_streams(const struct v4l2_subdev_state *state,
1798 u32 pad0, u32 pad1, u64 *streams)
1800 const struct v4l2_subdev_krouting *routing = &state->routing;
1801 struct v4l2_subdev_route *route;
1805 for_each_active_route(routing, route) {
1806 if (route->sink_pad == pad0 && route->source_pad == pad1 &&
1807 (*streams & BIT_ULL(route->sink_stream))) {
1808 streams0 |= BIT_ULL(route->sink_stream);
1809 streams1 |= BIT_ULL(route->source_stream);
1811 if (route->source_pad == pad0 && route->sink_pad == pad1 &&
1812 (*streams & BIT_ULL(route->source_stream))) {
1813 streams0 |= BIT_ULL(route->source_stream);
1814 streams1 |= BIT_ULL(route->sink_stream);
1818 *streams = streams0;
1821 EXPORT_SYMBOL_GPL(v4l2_subdev_state_xlate_streams);
1823 int v4l2_subdev_routing_validate(struct v4l2_subdev *sd,
1824 const struct v4l2_subdev_krouting *routing,
1825 enum v4l2_subdev_routing_restriction disallow)
1827 u32 *remote_pads = NULL;
1831 if (disallow & (V4L2_SUBDEV_ROUTING_NO_STREAM_MIX |
1832 V4L2_SUBDEV_ROUTING_NO_MULTIPLEXING)) {
1833 remote_pads = kcalloc(sd->entity.num_pads, sizeof(*remote_pads),
1838 for (i = 0; i < sd->entity.num_pads; ++i)
1839 remote_pads[i] = U32_MAX;
1842 for (i = 0; i < routing->num_routes; ++i) {
1843 const struct v4l2_subdev_route *route = &routing->routes[i];
1845 /* Validate the sink and source pad numbers. */
1846 if (route->sink_pad >= sd->entity.num_pads ||
1847 !(sd->entity.pads[route->sink_pad].flags & MEDIA_PAD_FL_SINK)) {
1848 dev_dbg(sd->dev, "route %u sink (%u) is not a sink pad\n",
1849 i, route->sink_pad);
1853 if (route->source_pad >= sd->entity.num_pads ||
1854 !(sd->entity.pads[route->source_pad].flags & MEDIA_PAD_FL_SOURCE)) {
1855 dev_dbg(sd->dev, "route %u source (%u) is not a source pad\n",
1856 i, route->source_pad);
1861 * V4L2_SUBDEV_ROUTING_NO_SINK_STREAM_MIX: all streams from a
1862 * sink pad must be routed to a single source pad.
1864 if (disallow & V4L2_SUBDEV_ROUTING_NO_SINK_STREAM_MIX) {
1865 if (remote_pads[route->sink_pad] != U32_MAX &&
1866 remote_pads[route->sink_pad] != route->source_pad) {
1868 "route %u attempts to mix %s streams\n",
1875 * V4L2_SUBDEV_ROUTING_NO_SOURCE_STREAM_MIX: all streams on a
1876 * source pad must originate from a single sink pad.
1878 if (disallow & V4L2_SUBDEV_ROUTING_NO_SOURCE_STREAM_MIX) {
1879 if (remote_pads[route->source_pad] != U32_MAX &&
1880 remote_pads[route->source_pad] != route->sink_pad) {
1882 "route %u attempts to mix %s streams\n",
1889 * V4L2_SUBDEV_ROUTING_NO_SINK_MULTIPLEXING: Pads on the sink
1890 * side can not do stream multiplexing, i.e. there can be only
1891 * a single stream in a sink pad.
1893 if (disallow & V4L2_SUBDEV_ROUTING_NO_SINK_MULTIPLEXING) {
1894 if (remote_pads[route->sink_pad] != U32_MAX) {
1896 "route %u attempts to multiplex on %s pad %u\n",
1897 i, "sink", route->sink_pad);
1903 * V4L2_SUBDEV_ROUTING_NO_SOURCE_MULTIPLEXING: Pads on the
1904 * source side can not do stream multiplexing, i.e. there can
1905 * be only a single stream in a source pad.
1907 if (disallow & V4L2_SUBDEV_ROUTING_NO_SOURCE_MULTIPLEXING) {
1908 if (remote_pads[route->source_pad] != U32_MAX) {
1910 "route %u attempts to multiplex on %s pad %u\n",
1911 i, "source", route->source_pad);
1917 remote_pads[route->sink_pad] = route->source_pad;
1918 remote_pads[route->source_pad] = route->sink_pad;
1921 for (j = i + 1; j < routing->num_routes; ++j) {
1922 const struct v4l2_subdev_route *r = &routing->routes[j];
1925 * V4L2_SUBDEV_ROUTING_NO_1_TO_N: No two routes can
1926 * originate from the same (sink) stream.
1928 if ((disallow & V4L2_SUBDEV_ROUTING_NO_1_TO_N) &&
1929 route->sink_pad == r->sink_pad &&
1930 route->sink_stream == r->sink_stream) {
1932 "routes %u and %u originate from same sink (%u/%u)\n",
1933 i, j, route->sink_pad,
1934 route->sink_stream);
1939 * V4L2_SUBDEV_ROUTING_NO_N_TO_1: No two routes can end
1940 * at the same (source) stream.
1942 if ((disallow & V4L2_SUBDEV_ROUTING_NO_N_TO_1) &&
1943 route->source_pad == r->source_pad &&
1944 route->source_stream == r->source_stream) {
1946 "routes %u and %u end at same source (%u/%u)\n",
1947 i, j, route->source_pad,
1948 route->source_stream);
1960 EXPORT_SYMBOL_GPL(v4l2_subdev_routing_validate);
1962 static int v4l2_subdev_enable_streams_fallback(struct v4l2_subdev *sd, u32 pad,
1965 struct device *dev = sd->entity.graph_obj.mdev->dev;
1970 * The subdev doesn't implement pad-based stream enable, fall back
1971 * on the .s_stream() operation. This can only be done for subdevs that
1972 * have a single source pad, as sd->enabled_streams is global to the
1975 if (!(sd->entity.pads[pad].flags & MEDIA_PAD_FL_SOURCE))
1978 for (i = 0; i < sd->entity.num_pads; ++i) {
1979 if (i != pad && sd->entity.pads[i].flags & MEDIA_PAD_FL_SOURCE)
1983 if (sd->enabled_streams & streams_mask) {
1984 dev_dbg(dev, "set of streams %#llx already enabled on %s:%u\n",
1985 streams_mask, sd->entity.name, pad);
1989 /* Start streaming when the first streams are enabled. */
1990 if (!sd->enabled_streams) {
1991 ret = v4l2_subdev_call(sd, video, s_stream, 1);
1996 sd->enabled_streams |= streams_mask;
2001 int v4l2_subdev_enable_streams(struct v4l2_subdev *sd, u32 pad,
2004 struct device *dev = sd->entity.graph_obj.mdev->dev;
2005 struct v4l2_subdev_state *state;
2006 u64 found_streams = 0;
2010 /* A few basic sanity checks first. */
2011 if (pad >= sd->entity.num_pads)
2017 /* Fallback on .s_stream() if .enable_streams() isn't available. */
2018 if (!sd->ops->pad || !sd->ops->pad->enable_streams)
2019 return v4l2_subdev_enable_streams_fallback(sd, pad,
2022 state = v4l2_subdev_lock_and_get_active_state(sd);
2025 * Verify that the requested streams exist and that they are not
2028 for (i = 0; i < state->stream_configs.num_configs; ++i) {
2029 struct v4l2_subdev_stream_config *cfg =
2030 &state->stream_configs.configs[i];
2032 if (cfg->pad != pad || !(streams_mask & BIT_ULL(cfg->stream)))
2035 found_streams |= BIT_ULL(cfg->stream);
2038 dev_dbg(dev, "stream %u already enabled on %s:%u\n",
2039 cfg->stream, sd->entity.name, pad);
2045 if (found_streams != streams_mask) {
2046 dev_dbg(dev, "streams 0x%llx not found on %s:%u\n",
2047 streams_mask & ~found_streams, sd->entity.name, pad);
2052 dev_dbg(dev, "enable streams %u:%#llx\n", pad, streams_mask);
2054 /* Call the .enable_streams() operation. */
2055 ret = v4l2_subdev_call(sd, pad, enable_streams, state, pad,
2058 dev_dbg(dev, "enable streams %u:%#llx failed: %d\n", pad,
2063 /* Mark the streams as enabled. */
2064 for (i = 0; i < state->stream_configs.num_configs; ++i) {
2065 struct v4l2_subdev_stream_config *cfg =
2066 &state->stream_configs.configs[i];
2068 if (cfg->pad == pad && (streams_mask & BIT_ULL(cfg->stream)))
2069 cfg->enabled = true;
2073 v4l2_subdev_unlock_state(state);
2077 EXPORT_SYMBOL_GPL(v4l2_subdev_enable_streams);
2079 static int v4l2_subdev_disable_streams_fallback(struct v4l2_subdev *sd, u32 pad,
2082 struct device *dev = sd->entity.graph_obj.mdev->dev;
2087 * If the subdev doesn't implement pad-based stream enable, fall back
2088 * on the .s_stream() operation. This can only be done for subdevs that
2089 * have a single source pad, as sd->enabled_streams is global to the
2092 if (!(sd->entity.pads[pad].flags & MEDIA_PAD_FL_SOURCE))
2095 for (i = 0; i < sd->entity.num_pads; ++i) {
2096 if (i != pad && sd->entity.pads[i].flags & MEDIA_PAD_FL_SOURCE)
2100 if ((sd->enabled_streams & streams_mask) != streams_mask) {
2101 dev_dbg(dev, "set of streams %#llx already disabled on %s:%u\n",
2102 streams_mask, sd->entity.name, pad);
2106 /* Stop streaming when the last streams are disabled. */
2107 if (!(sd->enabled_streams & ~streams_mask)) {
2108 ret = v4l2_subdev_call(sd, video, s_stream, 0);
2113 sd->enabled_streams &= ~streams_mask;
2118 int v4l2_subdev_disable_streams(struct v4l2_subdev *sd, u32 pad,
2121 struct device *dev = sd->entity.graph_obj.mdev->dev;
2122 struct v4l2_subdev_state *state;
2123 u64 found_streams = 0;
2127 /* A few basic sanity checks first. */
2128 if (pad >= sd->entity.num_pads)
2134 /* Fallback on .s_stream() if .disable_streams() isn't available. */
2135 if (!sd->ops->pad || !sd->ops->pad->disable_streams)
2136 return v4l2_subdev_disable_streams_fallback(sd, pad,
2139 state = v4l2_subdev_lock_and_get_active_state(sd);
2142 * Verify that the requested streams exist and that they are not
2145 for (i = 0; i < state->stream_configs.num_configs; ++i) {
2146 struct v4l2_subdev_stream_config *cfg =
2147 &state->stream_configs.configs[i];
2149 if (cfg->pad != pad || !(streams_mask & BIT_ULL(cfg->stream)))
2152 found_streams |= BIT_ULL(cfg->stream);
2154 if (!cfg->enabled) {
2155 dev_dbg(dev, "stream %u already disabled on %s:%u\n",
2156 cfg->stream, sd->entity.name, pad);
2162 if (found_streams != streams_mask) {
2163 dev_dbg(dev, "streams 0x%llx not found on %s:%u\n",
2164 streams_mask & ~found_streams, sd->entity.name, pad);
2169 dev_dbg(dev, "disable streams %u:%#llx\n", pad, streams_mask);
2171 /* Call the .disable_streams() operation. */
2172 ret = v4l2_subdev_call(sd, pad, disable_streams, state, pad,
2175 dev_dbg(dev, "disable streams %u:%#llx failed: %d\n", pad,
2180 /* Mark the streams as disabled. */
2181 for (i = 0; i < state->stream_configs.num_configs; ++i) {
2182 struct v4l2_subdev_stream_config *cfg =
2183 &state->stream_configs.configs[i];
2185 if (cfg->pad == pad && (streams_mask & BIT_ULL(cfg->stream)))
2186 cfg->enabled = false;
2190 v4l2_subdev_unlock_state(state);
2194 EXPORT_SYMBOL_GPL(v4l2_subdev_disable_streams);
2196 int v4l2_subdev_s_stream_helper(struct v4l2_subdev *sd, int enable)
2198 struct v4l2_subdev_state *state;
2199 struct v4l2_subdev_route *route;
2200 struct media_pad *pad;
2201 u64 source_mask = 0;
2205 * Find the source pad. This helper is meant for subdevs that have a
2206 * single source pad, so failures shouldn't happen, but catch them
2207 * loudly nonetheless as they indicate a driver bug.
2209 media_entity_for_each_pad(&sd->entity, pad) {
2210 if (pad->flags & MEDIA_PAD_FL_SOURCE) {
2211 pad_index = pad->index;
2216 if (WARN_ON(pad_index == -1))
2220 * As there's a single source pad, just collect all the source streams.
2222 state = v4l2_subdev_lock_and_get_active_state(sd);
2224 for_each_active_route(&state->routing, route)
2225 source_mask |= BIT_ULL(route->source_stream);
2227 v4l2_subdev_unlock_state(state);
2230 return v4l2_subdev_enable_streams(sd, pad_index, source_mask);
2232 return v4l2_subdev_disable_streams(sd, pad_index, source_mask);
2234 EXPORT_SYMBOL_GPL(v4l2_subdev_s_stream_helper);
2236 #endif /* CONFIG_VIDEO_V4L2_SUBDEV_API */
2238 #endif /* CONFIG_MEDIA_CONTROLLER */
2240 void v4l2_subdev_init(struct v4l2_subdev *sd, const struct v4l2_subdev_ops *ops)
2242 INIT_LIST_HEAD(&sd->list);
2245 sd->v4l2_dev = NULL;
2249 sd->dev_priv = NULL;
2250 sd->host_priv = NULL;
2251 sd->privacy_led = NULL;
2252 INIT_LIST_HEAD(&sd->async_subdev_endpoint_list);
2253 #if defined(CONFIG_MEDIA_CONTROLLER)
2254 sd->entity.name = sd->name;
2255 sd->entity.obj_type = MEDIA_ENTITY_TYPE_V4L2_SUBDEV;
2256 sd->entity.function = MEDIA_ENT_F_V4L2_SUBDEV_UNKNOWN;
2259 EXPORT_SYMBOL(v4l2_subdev_init);
2261 void v4l2_subdev_notify_event(struct v4l2_subdev *sd,
2262 const struct v4l2_event *ev)
2264 v4l2_event_queue(sd->devnode, ev);
2265 v4l2_subdev_notify(sd, V4L2_DEVICE_NOTIFY_EVENT, (void *)ev);
2267 EXPORT_SYMBOL_GPL(v4l2_subdev_notify_event);
2269 int v4l2_subdev_get_privacy_led(struct v4l2_subdev *sd)
2271 #if IS_REACHABLE(CONFIG_LEDS_CLASS)
2272 sd->privacy_led = led_get(sd->dev, "privacy-led");
2273 if (IS_ERR(sd->privacy_led) && PTR_ERR(sd->privacy_led) != -ENOENT)
2274 return dev_err_probe(sd->dev, PTR_ERR(sd->privacy_led),
2275 "getting privacy LED\n");
2277 if (!IS_ERR_OR_NULL(sd->privacy_led)) {
2278 mutex_lock(&sd->privacy_led->led_access);
2279 led_sysfs_disable(sd->privacy_led);
2280 led_trigger_remove(sd->privacy_led);
2281 led_set_brightness(sd->privacy_led, 0);
2282 mutex_unlock(&sd->privacy_led->led_access);
2287 EXPORT_SYMBOL_GPL(v4l2_subdev_get_privacy_led);
2289 void v4l2_subdev_put_privacy_led(struct v4l2_subdev *sd)
2291 #if IS_REACHABLE(CONFIG_LEDS_CLASS)
2292 if (!IS_ERR_OR_NULL(sd->privacy_led)) {
2293 mutex_lock(&sd->privacy_led->led_access);
2294 led_sysfs_enable(sd->privacy_led);
2295 mutex_unlock(&sd->privacy_led->led_access);
2296 led_put(sd->privacy_led);
2300 EXPORT_SYMBOL_GPL(v4l2_subdev_put_privacy_led);