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 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);
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);
168 static inline int check_which(u32 which)
170 if (which != V4L2_SUBDEV_FORMAT_TRY &&
171 which != V4L2_SUBDEV_FORMAT_ACTIVE)
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)
186 /* allow pad 0 on subdevices not registered as media entities */
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))
208 if (which == V4L2_SUBDEV_FORMAT_TRY && (!state || !state->pads))
214 static inline int check_format(struct v4l2_subdev *sd,
215 struct v4l2_subdev_state *state,
216 struct v4l2_subdev_format *format)
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)
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)
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)
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)
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)
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)
337 memset(fd, 0, sizeof(*fd));
339 ret = sd->ops->pad->get_frame_desc(sd, pad, fd);
343 dev_dbg(sd->dev, "Frame descriptor on pad %u, type %s\n", pad,
344 fd->type == V4L2_MBUS_FRAME_DESC_TYPE_PARALLEL ? "parallel" :
345 fd->type == V4L2_MBUS_FRAME_DESC_TYPE_CSI2 ? "CSI-2" :
348 for (i = 0; i < fd->num_entries; i++) {
349 struct v4l2_mbus_frame_desc_entry *entry = &fd->entry[i];
352 if (fd->type == V4L2_MBUS_FRAME_DESC_TYPE_CSI2)
353 WARN_ON(snprintf(buf, sizeof(buf),
354 ", vc %u, dt 0x%02x",
356 entry->bus.csi2.dt) >= sizeof(buf));
359 "\tstream %u, code 0x%04x, length %u, flags 0x%04x%s\n",
360 entry->stream, entry->pixelcode, entry->length,
367 static inline int check_edid(struct v4l2_subdev *sd,
368 struct v4l2_subdev_edid *edid)
373 if (edid->blocks && edid->edid == NULL)
376 return check_pad(sd, edid->pad);
379 static int call_get_edid(struct v4l2_subdev *sd, struct v4l2_subdev_edid *edid)
381 return check_edid(sd, edid) ? : sd->ops->pad->get_edid(sd, edid);
384 static int call_set_edid(struct v4l2_subdev *sd, struct v4l2_subdev_edid *edid)
386 return check_edid(sd, edid) ? : sd->ops->pad->set_edid(sd, edid);
389 static int call_s_dv_timings(struct v4l2_subdev *sd, unsigned int pad,
390 struct v4l2_dv_timings *timings)
395 return check_pad(sd, pad) ? :
396 sd->ops->pad->s_dv_timings(sd, pad, timings);
399 static int call_g_dv_timings(struct v4l2_subdev *sd, unsigned int pad,
400 struct v4l2_dv_timings *timings)
405 return check_pad(sd, pad) ? :
406 sd->ops->pad->g_dv_timings(sd, pad, timings);
409 static int call_query_dv_timings(struct v4l2_subdev *sd, unsigned int pad,
410 struct v4l2_dv_timings *timings)
415 return check_pad(sd, pad) ? :
416 sd->ops->pad->query_dv_timings(sd, pad, timings);
419 static int call_dv_timings_cap(struct v4l2_subdev *sd,
420 struct v4l2_dv_timings_cap *cap)
425 return check_pad(sd, cap->pad) ? :
426 sd->ops->pad->dv_timings_cap(sd, cap);
429 static int call_enum_dv_timings(struct v4l2_subdev *sd,
430 struct v4l2_enum_dv_timings *dvt)
435 return check_pad(sd, dvt->pad) ? :
436 sd->ops->pad->enum_dv_timings(sd, dvt);
439 static int call_get_mbus_config(struct v4l2_subdev *sd, unsigned int pad,
440 struct v4l2_mbus_config *config)
442 return check_pad(sd, pad) ? :
443 sd->ops->pad->get_mbus_config(sd, pad, config);
446 static int call_s_stream(struct v4l2_subdev *sd, int enable)
451 * The .s_stream() operation must never be called to start or stop an
452 * already started or stopped subdev. Catch offenders but don't return
453 * an error yet to avoid regressions.
455 if (WARN_ON(sd->s_stream_enabled == !!enable))
458 ret = sd->ops->video->s_stream(sd, enable);
460 if (!enable && ret < 0) {
461 dev_warn(sd->dev, "disabling streaming failed (%d)\n", ret);
466 sd->s_stream_enabled = enable;
469 v4l2_subdev_enable_privacy_led(sd);
471 v4l2_subdev_disable_privacy_led(sd);
477 #ifdef CONFIG_MEDIA_CONTROLLER
479 * Create state-management wrapper for pad ops dealing with subdev state. The
480 * wrapper handles the case where the caller does not provide the called
481 * subdev's state. This should be removed when all the callers are fixed.
483 #define DEFINE_STATE_WRAPPER(f, arg_type) \
484 static int call_##f##_state(struct v4l2_subdev *sd, \
485 struct v4l2_subdev_state *_state, \
488 struct v4l2_subdev_state *state = _state; \
491 state = v4l2_subdev_lock_and_get_active_state(sd); \
492 ret = call_##f(sd, state, arg); \
493 if (!_state && state) \
494 v4l2_subdev_unlock_state(state); \
498 #else /* CONFIG_MEDIA_CONTROLLER */
500 #define DEFINE_STATE_WRAPPER(f, arg_type) \
501 static int call_##f##_state(struct v4l2_subdev *sd, \
502 struct v4l2_subdev_state *state, \
505 return call_##f(sd, state, arg); \
508 #endif /* CONFIG_MEDIA_CONTROLLER */
510 DEFINE_STATE_WRAPPER(get_fmt, struct v4l2_subdev_format);
511 DEFINE_STATE_WRAPPER(set_fmt, struct v4l2_subdev_format);
512 DEFINE_STATE_WRAPPER(enum_mbus_code, struct v4l2_subdev_mbus_code_enum);
513 DEFINE_STATE_WRAPPER(enum_frame_size, struct v4l2_subdev_frame_size_enum);
514 DEFINE_STATE_WRAPPER(enum_frame_interval, struct v4l2_subdev_frame_interval_enum);
515 DEFINE_STATE_WRAPPER(get_selection, struct v4l2_subdev_selection);
516 DEFINE_STATE_WRAPPER(set_selection, struct v4l2_subdev_selection);
518 static const struct v4l2_subdev_pad_ops v4l2_subdev_call_pad_wrappers = {
519 .get_fmt = call_get_fmt_state,
520 .set_fmt = call_set_fmt_state,
521 .enum_mbus_code = call_enum_mbus_code_state,
522 .enum_frame_size = call_enum_frame_size_state,
523 .enum_frame_interval = call_enum_frame_interval_state,
524 .get_selection = call_get_selection_state,
525 .set_selection = call_set_selection_state,
526 .get_frame_interval = call_get_frame_interval,
527 .set_frame_interval = call_set_frame_interval,
528 .get_edid = call_get_edid,
529 .set_edid = call_set_edid,
530 .s_dv_timings = call_s_dv_timings,
531 .g_dv_timings = call_g_dv_timings,
532 .query_dv_timings = call_query_dv_timings,
533 .dv_timings_cap = call_dv_timings_cap,
534 .enum_dv_timings = call_enum_dv_timings,
535 .get_frame_desc = call_get_frame_desc,
536 .get_mbus_config = call_get_mbus_config,
539 static const struct v4l2_subdev_video_ops v4l2_subdev_call_video_wrappers = {
540 .s_stream = call_s_stream,
543 const struct v4l2_subdev_ops v4l2_subdev_call_wrappers = {
544 .pad = &v4l2_subdev_call_pad_wrappers,
545 .video = &v4l2_subdev_call_video_wrappers,
547 EXPORT_SYMBOL(v4l2_subdev_call_wrappers);
549 #if defined(CONFIG_VIDEO_V4L2_SUBDEV_API)
551 static struct v4l2_subdev_state *
552 subdev_ioctl_get_state(struct v4l2_subdev *sd, struct v4l2_subdev_fh *subdev_fh,
553 unsigned int cmd, void *arg)
560 case VIDIOC_SUBDEV_G_FMT:
561 case VIDIOC_SUBDEV_S_FMT:
562 which = ((struct v4l2_subdev_format *)arg)->which;
564 case VIDIOC_SUBDEV_G_CROP:
565 case VIDIOC_SUBDEV_S_CROP:
566 which = ((struct v4l2_subdev_crop *)arg)->which;
568 case VIDIOC_SUBDEV_ENUM_MBUS_CODE:
569 which = ((struct v4l2_subdev_mbus_code_enum *)arg)->which;
571 case VIDIOC_SUBDEV_ENUM_FRAME_SIZE:
572 which = ((struct v4l2_subdev_frame_size_enum *)arg)->which;
574 case VIDIOC_SUBDEV_ENUM_FRAME_INTERVAL:
575 which = ((struct v4l2_subdev_frame_interval_enum *)arg)->which;
577 case VIDIOC_SUBDEV_G_SELECTION:
578 case VIDIOC_SUBDEV_S_SELECTION:
579 which = ((struct v4l2_subdev_selection *)arg)->which;
581 case VIDIOC_SUBDEV_G_FRAME_INTERVAL:
582 case VIDIOC_SUBDEV_S_FRAME_INTERVAL: {
583 struct v4l2_subdev_frame_interval *fi = arg;
585 if (!(subdev_fh->client_caps &
586 V4L2_SUBDEV_CLIENT_CAP_INTERVAL_USES_WHICH))
587 fi->which = V4L2_SUBDEV_FORMAT_ACTIVE;
592 case VIDIOC_SUBDEV_G_ROUTING:
593 case VIDIOC_SUBDEV_S_ROUTING:
594 which = ((struct v4l2_subdev_routing *)arg)->which;
598 return which == V4L2_SUBDEV_FORMAT_TRY ?
600 v4l2_subdev_get_unlocked_active_state(sd);
603 static long subdev_do_ioctl(struct file *file, unsigned int cmd, void *arg,
604 struct v4l2_subdev_state *state)
606 struct video_device *vdev = video_devdata(file);
607 struct v4l2_subdev *sd = vdev_to_v4l2_subdev(vdev);
608 struct v4l2_fh *vfh = file->private_data;
609 struct v4l2_subdev_fh *subdev_fh = to_v4l2_subdev_fh(vfh);
610 bool ro_subdev = test_bit(V4L2_FL_SUBDEV_RO_DEVNODE, &vdev->flags);
611 bool streams_subdev = sd->flags & V4L2_SUBDEV_FL_STREAMS;
612 bool client_supports_streams = subdev_fh->client_caps &
613 V4L2_SUBDEV_CLIENT_CAP_STREAMS;
617 * If the streams API is not enabled, remove V4L2_SUBDEV_CAP_STREAMS.
618 * Remove this when the API is no longer experimental.
620 if (!v4l2_subdev_enable_streams_api)
621 streams_subdev = false;
624 case VIDIOC_SUBDEV_QUERYCAP: {
625 struct v4l2_subdev_capability *cap = arg;
627 memset(cap->reserved, 0, sizeof(cap->reserved));
628 cap->version = LINUX_VERSION_CODE;
630 (ro_subdev ? V4L2_SUBDEV_CAP_RO_SUBDEV : 0) |
631 (streams_subdev ? V4L2_SUBDEV_CAP_STREAMS : 0);
636 case VIDIOC_QUERYCTRL:
638 * TODO: this really should be folded into v4l2_queryctrl (this
639 * currently returns -EINVAL for NULL control handlers).
640 * However, v4l2_queryctrl() is still called directly by
641 * drivers as well and until that has been addressed I believe
642 * it is safer to do the check here. The same is true for the
643 * other control ioctls below.
645 if (!vfh->ctrl_handler)
647 return v4l2_queryctrl(vfh->ctrl_handler, arg);
649 case VIDIOC_QUERY_EXT_CTRL:
650 if (!vfh->ctrl_handler)
652 return v4l2_query_ext_ctrl(vfh->ctrl_handler, arg);
654 case VIDIOC_QUERYMENU:
655 if (!vfh->ctrl_handler)
657 return v4l2_querymenu(vfh->ctrl_handler, arg);
660 if (!vfh->ctrl_handler)
662 return v4l2_g_ctrl(vfh->ctrl_handler, arg);
665 if (!vfh->ctrl_handler)
667 return v4l2_s_ctrl(vfh, vfh->ctrl_handler, arg);
669 case VIDIOC_G_EXT_CTRLS:
670 if (!vfh->ctrl_handler)
672 return v4l2_g_ext_ctrls(vfh->ctrl_handler,
673 vdev, sd->v4l2_dev->mdev, arg);
675 case VIDIOC_S_EXT_CTRLS:
676 if (!vfh->ctrl_handler)
678 return v4l2_s_ext_ctrls(vfh, vfh->ctrl_handler,
679 vdev, sd->v4l2_dev->mdev, arg);
681 case VIDIOC_TRY_EXT_CTRLS:
682 if (!vfh->ctrl_handler)
684 return v4l2_try_ext_ctrls(vfh->ctrl_handler,
685 vdev, sd->v4l2_dev->mdev, arg);
688 if (!(sd->flags & V4L2_SUBDEV_FL_HAS_EVENTS))
691 return v4l2_event_dequeue(vfh, arg, file->f_flags & O_NONBLOCK);
693 case VIDIOC_SUBSCRIBE_EVENT:
694 return v4l2_subdev_call(sd, core, subscribe_event, vfh, arg);
696 case VIDIOC_UNSUBSCRIBE_EVENT:
697 return v4l2_subdev_call(sd, core, unsubscribe_event, vfh, arg);
699 #ifdef CONFIG_VIDEO_ADV_DEBUG
700 case VIDIOC_DBG_G_REGISTER:
702 struct v4l2_dbg_register *p = arg;
704 if (!capable(CAP_SYS_ADMIN))
706 return v4l2_subdev_call(sd, core, g_register, p);
708 case VIDIOC_DBG_S_REGISTER:
710 struct v4l2_dbg_register *p = arg;
712 if (!capable(CAP_SYS_ADMIN))
714 return v4l2_subdev_call(sd, core, s_register, p);
716 case VIDIOC_DBG_G_CHIP_INFO:
718 struct v4l2_dbg_chip_info *p = arg;
720 if (p->match.type != V4L2_CHIP_MATCH_SUBDEV || p->match.addr)
722 if (sd->ops->core && sd->ops->core->s_register)
723 p->flags |= V4L2_CHIP_FL_WRITABLE;
724 if (sd->ops->core && sd->ops->core->g_register)
725 p->flags |= V4L2_CHIP_FL_READABLE;
726 strscpy(p->name, sd->name, sizeof(p->name));
731 case VIDIOC_LOG_STATUS: {
734 pr_info("%s: ================= START STATUS =================\n",
736 ret = v4l2_subdev_call(sd, core, log_status);
737 pr_info("%s: ================== END STATUS ==================\n",
742 case VIDIOC_SUBDEV_G_FMT: {
743 struct v4l2_subdev_format *format = arg;
745 if (!client_supports_streams)
748 memset(format->reserved, 0, sizeof(format->reserved));
749 memset(format->format.reserved, 0, sizeof(format->format.reserved));
750 return v4l2_subdev_call(sd, pad, get_fmt, state, format);
753 case VIDIOC_SUBDEV_S_FMT: {
754 struct v4l2_subdev_format *format = arg;
756 if (format->which != V4L2_SUBDEV_FORMAT_TRY && ro_subdev)
759 if (!client_supports_streams)
762 memset(format->reserved, 0, sizeof(format->reserved));
763 memset(format->format.reserved, 0, sizeof(format->format.reserved));
764 return v4l2_subdev_call(sd, pad, set_fmt, state, format);
767 case VIDIOC_SUBDEV_G_CROP: {
768 struct v4l2_subdev_crop *crop = arg;
769 struct v4l2_subdev_selection sel;
771 if (!client_supports_streams)
774 memset(crop->reserved, 0, sizeof(crop->reserved));
775 memset(&sel, 0, sizeof(sel));
776 sel.which = crop->which;
778 sel.stream = crop->stream;
779 sel.target = V4L2_SEL_TGT_CROP;
781 rval = v4l2_subdev_call(
782 sd, pad, get_selection, state, &sel);
789 case VIDIOC_SUBDEV_S_CROP: {
790 struct v4l2_subdev_crop *crop = arg;
791 struct v4l2_subdev_selection sel;
793 if (crop->which != V4L2_SUBDEV_FORMAT_TRY && ro_subdev)
796 if (!client_supports_streams)
799 memset(crop->reserved, 0, sizeof(crop->reserved));
800 memset(&sel, 0, sizeof(sel));
801 sel.which = crop->which;
803 sel.stream = crop->stream;
804 sel.target = V4L2_SEL_TGT_CROP;
807 rval = v4l2_subdev_call(
808 sd, pad, set_selection, state, &sel);
815 case VIDIOC_SUBDEV_ENUM_MBUS_CODE: {
816 struct v4l2_subdev_mbus_code_enum *code = arg;
818 if (!client_supports_streams)
821 memset(code->reserved, 0, sizeof(code->reserved));
822 return v4l2_subdev_call(sd, pad, enum_mbus_code, state,
826 case VIDIOC_SUBDEV_ENUM_FRAME_SIZE: {
827 struct v4l2_subdev_frame_size_enum *fse = arg;
829 if (!client_supports_streams)
832 memset(fse->reserved, 0, sizeof(fse->reserved));
833 return v4l2_subdev_call(sd, pad, enum_frame_size, state,
837 case VIDIOC_SUBDEV_G_FRAME_INTERVAL: {
838 struct v4l2_subdev_frame_interval *fi = arg;
840 if (!client_supports_streams)
843 memset(fi->reserved, 0, sizeof(fi->reserved));
844 return v4l2_subdev_call(sd, pad, get_frame_interval, state, fi);
847 case VIDIOC_SUBDEV_S_FRAME_INTERVAL: {
848 struct v4l2_subdev_frame_interval *fi = arg;
850 if (!client_supports_streams)
853 if (fi->which != V4L2_SUBDEV_FORMAT_TRY && ro_subdev)
856 memset(fi->reserved, 0, sizeof(fi->reserved));
857 return v4l2_subdev_call(sd, pad, set_frame_interval, state, fi);
860 case VIDIOC_SUBDEV_ENUM_FRAME_INTERVAL: {
861 struct v4l2_subdev_frame_interval_enum *fie = arg;
863 if (!client_supports_streams)
866 memset(fie->reserved, 0, sizeof(fie->reserved));
867 return v4l2_subdev_call(sd, pad, enum_frame_interval, state,
871 case VIDIOC_SUBDEV_G_SELECTION: {
872 struct v4l2_subdev_selection *sel = arg;
874 if (!client_supports_streams)
877 memset(sel->reserved, 0, sizeof(sel->reserved));
878 return v4l2_subdev_call(
879 sd, pad, get_selection, state, sel);
882 case VIDIOC_SUBDEV_S_SELECTION: {
883 struct v4l2_subdev_selection *sel = arg;
885 if (sel->which != V4L2_SUBDEV_FORMAT_TRY && ro_subdev)
888 if (!client_supports_streams)
891 memset(sel->reserved, 0, sizeof(sel->reserved));
892 return v4l2_subdev_call(
893 sd, pad, set_selection, state, sel);
896 case VIDIOC_G_EDID: {
897 struct v4l2_subdev_edid *edid = arg;
899 return v4l2_subdev_call(sd, pad, get_edid, edid);
902 case VIDIOC_S_EDID: {
903 struct v4l2_subdev_edid *edid = arg;
905 return v4l2_subdev_call(sd, pad, set_edid, edid);
908 case VIDIOC_SUBDEV_DV_TIMINGS_CAP: {
909 struct v4l2_dv_timings_cap *cap = arg;
911 return v4l2_subdev_call(sd, pad, dv_timings_cap, cap);
914 case VIDIOC_SUBDEV_ENUM_DV_TIMINGS: {
915 struct v4l2_enum_dv_timings *dvt = arg;
917 return v4l2_subdev_call(sd, pad, enum_dv_timings, dvt);
920 case VIDIOC_SUBDEV_QUERY_DV_TIMINGS:
921 return v4l2_subdev_call(sd, pad, query_dv_timings, 0, arg);
923 case VIDIOC_SUBDEV_G_DV_TIMINGS:
924 return v4l2_subdev_call(sd, pad, g_dv_timings, 0, arg);
926 case VIDIOC_SUBDEV_S_DV_TIMINGS:
930 return v4l2_subdev_call(sd, pad, s_dv_timings, 0, arg);
932 case VIDIOC_SUBDEV_G_STD:
933 return v4l2_subdev_call(sd, video, g_std, arg);
935 case VIDIOC_SUBDEV_S_STD: {
936 v4l2_std_id *std = arg;
941 return v4l2_subdev_call(sd, video, s_std, *std);
944 case VIDIOC_SUBDEV_ENUMSTD: {
945 struct v4l2_standard *p = arg;
948 if (v4l2_subdev_call(sd, video, g_tvnorms, &id))
951 return v4l_video_std_enumstd(p, id);
954 case VIDIOC_SUBDEV_QUERYSTD:
955 return v4l2_subdev_call(sd, video, querystd, arg);
957 case VIDIOC_SUBDEV_G_ROUTING: {
958 struct v4l2_subdev_routing *routing = arg;
959 struct v4l2_subdev_krouting *krouting;
961 if (!v4l2_subdev_enable_streams_api)
964 if (!(sd->flags & V4L2_SUBDEV_FL_STREAMS))
967 memset(routing->reserved, 0, sizeof(routing->reserved));
969 krouting = &state->routing;
971 memcpy((struct v4l2_subdev_route *)(uintptr_t)routing->routes,
973 min(krouting->num_routes, routing->len_routes) *
974 sizeof(*krouting->routes));
975 routing->num_routes = krouting->num_routes;
980 case VIDIOC_SUBDEV_S_ROUTING: {
981 struct v4l2_subdev_routing *routing = arg;
982 struct v4l2_subdev_route *routes =
983 (struct v4l2_subdev_route *)(uintptr_t)routing->routes;
984 struct v4l2_subdev_krouting krouting = {};
987 if (!v4l2_subdev_enable_streams_api)
990 if (!(sd->flags & V4L2_SUBDEV_FL_STREAMS))
993 if (routing->which != V4L2_SUBDEV_FORMAT_TRY && ro_subdev)
996 if (routing->num_routes > routing->len_routes)
999 memset(routing->reserved, 0, sizeof(routing->reserved));
1001 for (i = 0; i < routing->num_routes; ++i) {
1002 const struct v4l2_subdev_route *route = &routes[i];
1003 const struct media_pad *pads = sd->entity.pads;
1005 if (route->sink_stream > V4L2_SUBDEV_MAX_STREAM_ID ||
1006 route->source_stream > V4L2_SUBDEV_MAX_STREAM_ID)
1009 if (route->sink_pad >= sd->entity.num_pads)
1012 if (!(pads[route->sink_pad].flags &
1016 if (route->source_pad >= sd->entity.num_pads)
1019 if (!(pads[route->source_pad].flags &
1020 MEDIA_PAD_FL_SOURCE))
1025 * If the driver doesn't support setting routing, just return
1026 * the routing table.
1028 if (!v4l2_subdev_has_op(sd, pad, set_routing)) {
1029 memcpy((struct v4l2_subdev_route *)(uintptr_t)routing->routes,
1030 state->routing.routes,
1031 min(state->routing.num_routes, routing->len_routes) *
1032 sizeof(*state->routing.routes));
1033 routing->num_routes = state->routing.num_routes;
1038 krouting.num_routes = routing->num_routes;
1039 krouting.len_routes = routing->len_routes;
1040 krouting.routes = routes;
1042 rval = v4l2_subdev_call(sd, pad, set_routing, state,
1043 routing->which, &krouting);
1047 memcpy((struct v4l2_subdev_route *)(uintptr_t)routing->routes,
1048 state->routing.routes,
1049 min(state->routing.num_routes, routing->len_routes) *
1050 sizeof(*state->routing.routes));
1051 routing->num_routes = state->routing.num_routes;
1056 case VIDIOC_SUBDEV_G_CLIENT_CAP: {
1057 struct v4l2_subdev_client_capability *client_cap = arg;
1059 client_cap->capabilities = subdev_fh->client_caps;
1064 case VIDIOC_SUBDEV_S_CLIENT_CAP: {
1065 struct v4l2_subdev_client_capability *client_cap = arg;
1068 * Clear V4L2_SUBDEV_CLIENT_CAP_STREAMS if streams API is not
1069 * enabled. Remove this when streams API is no longer
1072 if (!v4l2_subdev_enable_streams_api)
1073 client_cap->capabilities &= ~V4L2_SUBDEV_CLIENT_CAP_STREAMS;
1075 /* Filter out unsupported capabilities */
1076 client_cap->capabilities &= (V4L2_SUBDEV_CLIENT_CAP_STREAMS |
1077 V4L2_SUBDEV_CLIENT_CAP_INTERVAL_USES_WHICH);
1079 subdev_fh->client_caps = client_cap->capabilities;
1085 return v4l2_subdev_call(sd, core, ioctl, cmd, arg);
1091 static long subdev_do_ioctl_lock(struct file *file, unsigned int cmd, void *arg)
1093 struct video_device *vdev = video_devdata(file);
1094 struct mutex *lock = vdev->lock;
1097 if (lock && mutex_lock_interruptible(lock))
1098 return -ERESTARTSYS;
1100 if (video_is_registered(vdev)) {
1101 struct v4l2_subdev *sd = vdev_to_v4l2_subdev(vdev);
1102 struct v4l2_fh *vfh = file->private_data;
1103 struct v4l2_subdev_fh *subdev_fh = to_v4l2_subdev_fh(vfh);
1104 struct v4l2_subdev_state *state;
1106 state = subdev_ioctl_get_state(sd, subdev_fh, cmd, arg);
1109 v4l2_subdev_lock_state(state);
1111 ret = subdev_do_ioctl(file, cmd, arg, state);
1114 v4l2_subdev_unlock_state(state);
1122 static long subdev_ioctl(struct file *file, unsigned int cmd,
1125 return video_usercopy(file, cmd, arg, subdev_do_ioctl_lock);
1128 #ifdef CONFIG_COMPAT
1129 static long subdev_compat_ioctl32(struct file *file, unsigned int cmd,
1132 struct video_device *vdev = video_devdata(file);
1133 struct v4l2_subdev *sd = vdev_to_v4l2_subdev(vdev);
1135 return v4l2_subdev_call(sd, core, compat_ioctl32, cmd, arg);
1139 #else /* CONFIG_VIDEO_V4L2_SUBDEV_API */
1140 static long subdev_ioctl(struct file *file, unsigned int cmd,
1146 #ifdef CONFIG_COMPAT
1147 static long subdev_compat_ioctl32(struct file *file, unsigned int cmd,
1153 #endif /* CONFIG_VIDEO_V4L2_SUBDEV_API */
1155 static __poll_t subdev_poll(struct file *file, poll_table *wait)
1157 struct video_device *vdev = video_devdata(file);
1158 struct v4l2_subdev *sd = vdev_to_v4l2_subdev(vdev);
1159 struct v4l2_fh *fh = file->private_data;
1161 if (!(sd->flags & V4L2_SUBDEV_FL_HAS_EVENTS))
1164 poll_wait(file, &fh->wait, wait);
1166 if (v4l2_event_pending(fh))
1172 const struct v4l2_file_operations v4l2_subdev_fops = {
1173 .owner = THIS_MODULE,
1174 .open = subdev_open,
1175 .unlocked_ioctl = subdev_ioctl,
1176 #ifdef CONFIG_COMPAT
1177 .compat_ioctl32 = subdev_compat_ioctl32,
1179 .release = subdev_close,
1180 .poll = subdev_poll,
1183 #ifdef CONFIG_MEDIA_CONTROLLER
1185 int v4l2_subdev_get_fwnode_pad_1_to_1(struct media_entity *entity,
1186 struct fwnode_endpoint *endpoint)
1188 struct fwnode_handle *fwnode;
1189 struct v4l2_subdev *sd;
1191 if (!is_media_entity_v4l2_subdev(entity))
1194 sd = media_entity_to_v4l2_subdev(entity);
1196 fwnode = fwnode_graph_get_port_parent(endpoint->local_fwnode);
1197 fwnode_handle_put(fwnode);
1199 if (device_match_fwnode(sd->dev, fwnode))
1200 return endpoint->port;
1204 EXPORT_SYMBOL_GPL(v4l2_subdev_get_fwnode_pad_1_to_1);
1206 int v4l2_subdev_link_validate_default(struct v4l2_subdev *sd,
1207 struct media_link *link,
1208 struct v4l2_subdev_format *source_fmt,
1209 struct v4l2_subdev_format *sink_fmt)
1213 /* The width, height and code must match. */
1214 if (source_fmt->format.width != sink_fmt->format.width) {
1215 dev_dbg(sd->entity.graph_obj.mdev->dev,
1216 "%s: width does not match (source %u, sink %u)\n",
1218 source_fmt->format.width, sink_fmt->format.width);
1222 if (source_fmt->format.height != sink_fmt->format.height) {
1223 dev_dbg(sd->entity.graph_obj.mdev->dev,
1224 "%s: height does not match (source %u, sink %u)\n",
1226 source_fmt->format.height, sink_fmt->format.height);
1230 if (source_fmt->format.code != sink_fmt->format.code) {
1231 dev_dbg(sd->entity.graph_obj.mdev->dev,
1232 "%s: media bus code does not match (source 0x%8.8x, sink 0x%8.8x)\n",
1234 source_fmt->format.code, sink_fmt->format.code);
1238 /* The field order must match, or the sink field order must be NONE
1239 * to support interlaced hardware connected to bridges that support
1240 * progressive formats only.
1242 if (source_fmt->format.field != sink_fmt->format.field &&
1243 sink_fmt->format.field != V4L2_FIELD_NONE) {
1244 dev_dbg(sd->entity.graph_obj.mdev->dev,
1245 "%s: field does not match (source %u, sink %u)\n",
1247 source_fmt->format.field, sink_fmt->format.field);
1254 dev_dbg(sd->entity.graph_obj.mdev->dev,
1255 "%s: link was \"%s\":%u -> \"%s\":%u\n", __func__,
1256 link->source->entity->name, link->source->index,
1257 link->sink->entity->name, link->sink->index);
1261 EXPORT_SYMBOL_GPL(v4l2_subdev_link_validate_default);
1264 v4l2_subdev_link_validate_get_format(struct media_pad *pad, u32 stream,
1265 struct v4l2_subdev_format *fmt,
1268 struct v4l2_subdev_state *state;
1269 struct v4l2_subdev *sd;
1272 sd = media_entity_to_v4l2_subdev(pad->entity);
1274 fmt->which = V4L2_SUBDEV_FORMAT_ACTIVE;
1275 fmt->pad = pad->index;
1276 fmt->stream = stream;
1279 state = v4l2_subdev_get_locked_active_state(sd);
1281 state = v4l2_subdev_lock_and_get_active_state(sd);
1283 ret = v4l2_subdev_call(sd, pad, get_fmt, state, fmt);
1285 if (!states_locked && state)
1286 v4l2_subdev_unlock_state(state);
1291 #if defined(CONFIG_VIDEO_V4L2_SUBDEV_API)
1293 static void __v4l2_link_validate_get_streams(struct media_pad *pad,
1297 struct v4l2_subdev_route *route;
1298 struct v4l2_subdev_state *state;
1299 struct v4l2_subdev *subdev;
1301 subdev = media_entity_to_v4l2_subdev(pad->entity);
1306 state = v4l2_subdev_get_locked_active_state(subdev);
1308 state = v4l2_subdev_lock_and_get_active_state(subdev);
1310 if (WARN_ON(!state))
1313 for_each_active_route(&state->routing, route) {
1317 if (pad->flags & MEDIA_PAD_FL_SOURCE) {
1318 route_pad = route->source_pad;
1319 route_stream = route->source_stream;
1321 route_pad = route->sink_pad;
1322 route_stream = route->sink_stream;
1325 if (route_pad != pad->index)
1328 *streams_mask |= BIT_ULL(route_stream);
1332 v4l2_subdev_unlock_state(state);
1335 #endif /* CONFIG_VIDEO_V4L2_SUBDEV_API */
1337 static void v4l2_link_validate_get_streams(struct media_pad *pad,
1341 struct v4l2_subdev *subdev = media_entity_to_v4l2_subdev(pad->entity);
1343 if (!(subdev->flags & V4L2_SUBDEV_FL_STREAMS)) {
1344 /* Non-streams subdevs have an implicit stream 0 */
1345 *streams_mask = BIT_ULL(0);
1349 #if defined(CONFIG_VIDEO_V4L2_SUBDEV_API)
1350 __v4l2_link_validate_get_streams(pad, streams_mask, states_locked);
1352 /* This shouldn't happen */
1357 static int v4l2_subdev_link_validate_locked(struct media_link *link, bool states_locked)
1359 struct v4l2_subdev *sink_subdev =
1360 media_entity_to_v4l2_subdev(link->sink->entity);
1361 struct device *dev = sink_subdev->entity.graph_obj.mdev->dev;
1362 u64 source_streams_mask;
1363 u64 sink_streams_mask;
1364 u64 dangling_sink_streams;
1368 dev_dbg(dev, "validating link \"%s\":%u -> \"%s\":%u\n",
1369 link->source->entity->name, link->source->index,
1370 link->sink->entity->name, link->sink->index);
1372 v4l2_link_validate_get_streams(link->source, &source_streams_mask, states_locked);
1373 v4l2_link_validate_get_streams(link->sink, &sink_streams_mask, states_locked);
1376 * It is ok to have more source streams than sink streams as extra
1377 * source streams can just be ignored by the receiver, but having extra
1378 * sink streams is an error as streams must have a source.
1380 dangling_sink_streams = (source_streams_mask ^ sink_streams_mask) &
1382 if (dangling_sink_streams) {
1383 dev_err(dev, "Dangling sink streams: mask %#llx\n",
1384 dangling_sink_streams);
1388 /* Validate source and sink stream formats */
1390 for (stream = 0; stream < sizeof(sink_streams_mask) * 8; ++stream) {
1391 struct v4l2_subdev_format sink_fmt, source_fmt;
1393 if (!(sink_streams_mask & BIT_ULL(stream)))
1396 dev_dbg(dev, "validating stream \"%s\":%u:%u -> \"%s\":%u:%u\n",
1397 link->source->entity->name, link->source->index, stream,
1398 link->sink->entity->name, link->sink->index, stream);
1400 ret = v4l2_subdev_link_validate_get_format(link->source, stream,
1401 &source_fmt, states_locked);
1404 "Failed to get format for \"%s\":%u:%u (but that's ok)\n",
1405 link->source->entity->name, link->source->index,
1410 ret = v4l2_subdev_link_validate_get_format(link->sink, stream,
1411 &sink_fmt, states_locked);
1414 "Failed to get format for \"%s\":%u:%u (but that's ok)\n",
1415 link->sink->entity->name, link->sink->index,
1420 /* TODO: add stream number to link_validate() */
1421 ret = v4l2_subdev_call(sink_subdev, pad, link_validate, link,
1422 &source_fmt, &sink_fmt);
1426 if (ret != -ENOIOCTLCMD)
1429 ret = v4l2_subdev_link_validate_default(sink_subdev, link,
1430 &source_fmt, &sink_fmt);
1439 int v4l2_subdev_link_validate(struct media_link *link)
1441 struct v4l2_subdev *source_sd, *sink_sd;
1442 struct v4l2_subdev_state *source_state, *sink_state;
1447 * Links are validated in the context of the sink entity. Usage of this
1448 * helper on a sink that is not a subdev is a clear driver bug.
1450 if (WARN_ON_ONCE(!is_media_entity_v4l2_subdev(link->sink->entity)))
1454 * If the source is a video device, delegate link validation to it. This
1455 * allows usage of this helper for subdev connected to a video output
1456 * device, provided that the driver implement the video output device's
1457 * .link_validate() operation.
1459 if (is_media_entity_v4l2_video_device(link->source->entity)) {
1460 struct media_entity *source = link->source->entity;
1462 if (!source->ops || !source->ops->link_validate) {
1464 * Many existing drivers do not implement the required
1465 * .link_validate() operation for their video devices.
1466 * Print a warning to get the drivers fixed, and return
1467 * 0 to avoid breaking userspace. This should
1468 * eventually be turned into a WARN_ON() when all
1469 * drivers will have been fixed.
1471 pr_warn_once("video device '%s' does not implement .link_validate(), driver bug!\n",
1477 * Avoid infinite loops in case a video device incorrectly uses
1478 * this helper function as its .link_validate() handler.
1480 if (WARN_ON(source->ops->link_validate == v4l2_subdev_link_validate))
1483 return source->ops->link_validate(link);
1487 * If the source is still not a subdev, usage of this helper is a clear
1490 if (WARN_ON(!is_media_entity_v4l2_subdev(link->source->entity)))
1493 sink_sd = media_entity_to_v4l2_subdev(link->sink->entity);
1494 source_sd = media_entity_to_v4l2_subdev(link->source->entity);
1496 sink_state = v4l2_subdev_get_unlocked_active_state(sink_sd);
1497 source_state = v4l2_subdev_get_unlocked_active_state(source_sd);
1499 states_locked = sink_state && source_state;
1502 v4l2_subdev_lock_states(sink_state, source_state);
1504 ret = v4l2_subdev_link_validate_locked(link, states_locked);
1507 v4l2_subdev_unlock_states(sink_state, source_state);
1511 EXPORT_SYMBOL_GPL(v4l2_subdev_link_validate);
1513 bool v4l2_subdev_has_pad_interdep(struct media_entity *entity,
1514 unsigned int pad0, unsigned int pad1)
1516 struct v4l2_subdev *sd = media_entity_to_v4l2_subdev(entity);
1517 struct v4l2_subdev_krouting *routing;
1518 struct v4l2_subdev_state *state;
1521 state = v4l2_subdev_lock_and_get_active_state(sd);
1523 routing = &state->routing;
1525 for (i = 0; i < routing->num_routes; ++i) {
1526 struct v4l2_subdev_route *route = &routing->routes[i];
1528 if (!(route->flags & V4L2_SUBDEV_ROUTE_FL_ACTIVE))
1531 if ((route->sink_pad == pad0 && route->source_pad == pad1) ||
1532 (route->source_pad == pad0 && route->sink_pad == pad1)) {
1533 v4l2_subdev_unlock_state(state);
1538 v4l2_subdev_unlock_state(state);
1542 EXPORT_SYMBOL_GPL(v4l2_subdev_has_pad_interdep);
1544 struct v4l2_subdev_state *
1545 __v4l2_subdev_state_alloc(struct v4l2_subdev *sd, const char *lock_name,
1546 struct lock_class_key *lock_key)
1548 struct v4l2_subdev_state *state;
1551 state = kzalloc(sizeof(*state), GFP_KERNEL);
1553 return ERR_PTR(-ENOMEM);
1555 __mutex_init(&state->_lock, lock_name, lock_key);
1557 state->lock = sd->state_lock;
1559 state->lock = &state->_lock;
1563 /* Drivers that support streams do not need the legacy pad config */
1564 if (!(sd->flags & V4L2_SUBDEV_FL_STREAMS) && sd->entity.num_pads) {
1565 state->pads = kvcalloc(sd->entity.num_pads,
1566 sizeof(*state->pads), GFP_KERNEL);
1573 if (sd->internal_ops && sd->internal_ops->init_state) {
1575 * There can be no race at this point, but we lock the state
1576 * anyway to satisfy lockdep checks.
1578 v4l2_subdev_lock_state(state);
1579 ret = sd->internal_ops->init_state(sd, state);
1580 v4l2_subdev_unlock_state(state);
1589 if (state && state->pads)
1590 kvfree(state->pads);
1594 return ERR_PTR(ret);
1596 EXPORT_SYMBOL_GPL(__v4l2_subdev_state_alloc);
1598 void __v4l2_subdev_state_free(struct v4l2_subdev_state *state)
1603 mutex_destroy(&state->_lock);
1605 kfree(state->routing.routes);
1606 kvfree(state->stream_configs.configs);
1607 kvfree(state->pads);
1610 EXPORT_SYMBOL_GPL(__v4l2_subdev_state_free);
1612 int __v4l2_subdev_init_finalize(struct v4l2_subdev *sd, const char *name,
1613 struct lock_class_key *key)
1615 struct v4l2_subdev_state *state;
1616 struct device *dev = sd->dev;
1617 bool has_disable_streams;
1618 bool has_enable_streams;
1621 /* Check that the subdevice implements the required features */
1623 has_s_stream = v4l2_subdev_has_op(sd, video, s_stream);
1624 has_enable_streams = v4l2_subdev_has_op(sd, pad, enable_streams);
1625 has_disable_streams = v4l2_subdev_has_op(sd, pad, disable_streams);
1627 if (has_enable_streams != has_disable_streams) {
1629 "subdev '%s' must implement both or neither of .enable_streams() and .disable_streams()\n",
1634 if (sd->flags & V4L2_SUBDEV_FL_STREAMS) {
1635 if (has_s_stream && !has_enable_streams) {
1637 "subdev '%s' must implement .enable/disable_streams()\n",
1644 state = __v4l2_subdev_state_alloc(sd, name, key);
1646 return PTR_ERR(state);
1648 sd->active_state = state;
1652 EXPORT_SYMBOL_GPL(__v4l2_subdev_init_finalize);
1654 void v4l2_subdev_cleanup(struct v4l2_subdev *sd)
1656 struct v4l2_async_subdev_endpoint *ase, *ase_tmp;
1658 __v4l2_subdev_state_free(sd->active_state);
1659 sd->active_state = NULL;
1661 /* Uninitialised sub-device, bail out here. */
1662 if (!sd->async_subdev_endpoint_list.next)
1665 list_for_each_entry_safe(ase, ase_tmp, &sd->async_subdev_endpoint_list,
1666 async_subdev_endpoint_entry) {
1667 list_del(&ase->async_subdev_endpoint_entry);
1672 EXPORT_SYMBOL_GPL(v4l2_subdev_cleanup);
1674 struct v4l2_mbus_framefmt *
1675 __v4l2_subdev_state_get_format(struct v4l2_subdev_state *state,
1676 unsigned int pad, u32 stream)
1678 struct v4l2_subdev_stream_configs *stream_configs;
1681 if (WARN_ON_ONCE(!state))
1688 if (pad >= state->sd->entity.num_pads)
1691 return &state->pads[pad].format;
1694 lockdep_assert_held(state->lock);
1696 stream_configs = &state->stream_configs;
1698 for (i = 0; i < stream_configs->num_configs; ++i) {
1699 if (stream_configs->configs[i].pad == pad &&
1700 stream_configs->configs[i].stream == stream)
1701 return &stream_configs->configs[i].fmt;
1706 EXPORT_SYMBOL_GPL(__v4l2_subdev_state_get_format);
1709 __v4l2_subdev_state_get_crop(struct v4l2_subdev_state *state, unsigned int pad,
1712 struct v4l2_subdev_stream_configs *stream_configs;
1715 if (WARN_ON_ONCE(!state))
1722 if (pad >= state->sd->entity.num_pads)
1725 return &state->pads[pad].crop;
1728 lockdep_assert_held(state->lock);
1730 stream_configs = &state->stream_configs;
1732 for (i = 0; i < stream_configs->num_configs; ++i) {
1733 if (stream_configs->configs[i].pad == pad &&
1734 stream_configs->configs[i].stream == stream)
1735 return &stream_configs->configs[i].crop;
1740 EXPORT_SYMBOL_GPL(__v4l2_subdev_state_get_crop);
1743 __v4l2_subdev_state_get_compose(struct v4l2_subdev_state *state,
1744 unsigned int pad, u32 stream)
1746 struct v4l2_subdev_stream_configs *stream_configs;
1749 if (WARN_ON_ONCE(!state))
1756 if (pad >= state->sd->entity.num_pads)
1759 return &state->pads[pad].compose;
1762 lockdep_assert_held(state->lock);
1764 stream_configs = &state->stream_configs;
1766 for (i = 0; i < stream_configs->num_configs; ++i) {
1767 if (stream_configs->configs[i].pad == pad &&
1768 stream_configs->configs[i].stream == stream)
1769 return &stream_configs->configs[i].compose;
1774 EXPORT_SYMBOL_GPL(__v4l2_subdev_state_get_compose);
1777 __v4l2_subdev_state_get_interval(struct v4l2_subdev_state *state,
1778 unsigned int pad, u32 stream)
1780 struct v4l2_subdev_stream_configs *stream_configs;
1783 if (WARN_ON(!state))
1786 lockdep_assert_held(state->lock);
1792 if (pad >= state->sd->entity.num_pads)
1795 return &state->pads[pad].interval;
1798 lockdep_assert_held(state->lock);
1800 stream_configs = &state->stream_configs;
1802 for (i = 0; i < stream_configs->num_configs; ++i) {
1803 if (stream_configs->configs[i].pad == pad &&
1804 stream_configs->configs[i].stream == stream)
1805 return &stream_configs->configs[i].interval;
1810 EXPORT_SYMBOL_GPL(__v4l2_subdev_state_get_interval);
1812 #if defined(CONFIG_VIDEO_V4L2_SUBDEV_API)
1815 v4l2_subdev_init_stream_configs(struct v4l2_subdev_stream_configs *stream_configs,
1816 const struct v4l2_subdev_krouting *routing)
1818 struct v4l2_subdev_stream_configs new_configs = { 0 };
1819 struct v4l2_subdev_route *route;
1822 /* Count number of formats needed */
1823 for_each_active_route(routing, route) {
1825 * Each route needs a format on both ends of the route.
1827 new_configs.num_configs += 2;
1830 if (new_configs.num_configs) {
1831 new_configs.configs = kvcalloc(new_configs.num_configs,
1832 sizeof(*new_configs.configs),
1835 if (!new_configs.configs)
1840 * Fill in the 'pad' and stream' value for each item in the array from
1845 for_each_active_route(routing, route) {
1846 new_configs.configs[idx].pad = route->sink_pad;
1847 new_configs.configs[idx].stream = route->sink_stream;
1851 new_configs.configs[idx].pad = route->source_pad;
1852 new_configs.configs[idx].stream = route->source_stream;
1857 kvfree(stream_configs->configs);
1858 *stream_configs = new_configs;
1863 int v4l2_subdev_get_fmt(struct v4l2_subdev *sd, struct v4l2_subdev_state *state,
1864 struct v4l2_subdev_format *format)
1866 struct v4l2_mbus_framefmt *fmt;
1868 fmt = v4l2_subdev_state_get_format(state, format->pad, format->stream);
1872 format->format = *fmt;
1876 EXPORT_SYMBOL_GPL(v4l2_subdev_get_fmt);
1878 int v4l2_subdev_get_frame_interval(struct v4l2_subdev *sd,
1879 struct v4l2_subdev_state *state,
1880 struct v4l2_subdev_frame_interval *fi)
1882 struct v4l2_fract *interval;
1884 interval = v4l2_subdev_state_get_interval(state, fi->pad, fi->stream);
1888 fi->interval = *interval;
1892 EXPORT_SYMBOL_GPL(v4l2_subdev_get_frame_interval);
1894 int v4l2_subdev_set_routing(struct v4l2_subdev *sd,
1895 struct v4l2_subdev_state *state,
1896 const struct v4l2_subdev_krouting *routing)
1898 struct v4l2_subdev_krouting *dst = &state->routing;
1899 const struct v4l2_subdev_krouting *src = routing;
1900 struct v4l2_subdev_krouting new_routing = { 0 };
1904 if (unlikely(check_mul_overflow((size_t)src->num_routes,
1905 sizeof(*src->routes), &bytes)))
1908 lockdep_assert_held(state->lock);
1910 if (src->num_routes > 0) {
1911 new_routing.routes = kmemdup(src->routes, bytes, GFP_KERNEL);
1912 if (!new_routing.routes)
1916 new_routing.num_routes = src->num_routes;
1918 r = v4l2_subdev_init_stream_configs(&state->stream_configs,
1921 kfree(new_routing.routes);
1930 EXPORT_SYMBOL_GPL(v4l2_subdev_set_routing);
1932 struct v4l2_subdev_route *
1933 __v4l2_subdev_next_active_route(const struct v4l2_subdev_krouting *routing,
1934 struct v4l2_subdev_route *route)
1939 route = &routing->routes[0];
1941 for (; route < routing->routes + routing->num_routes; ++route) {
1942 if (!(route->flags & V4L2_SUBDEV_ROUTE_FL_ACTIVE))
1950 EXPORT_SYMBOL_GPL(__v4l2_subdev_next_active_route);
1952 int v4l2_subdev_set_routing_with_fmt(struct v4l2_subdev *sd,
1953 struct v4l2_subdev_state *state,
1954 const struct v4l2_subdev_krouting *routing,
1955 const struct v4l2_mbus_framefmt *fmt)
1957 struct v4l2_subdev_stream_configs *stream_configs;
1961 ret = v4l2_subdev_set_routing(sd, state, routing);
1965 stream_configs = &state->stream_configs;
1967 for (i = 0; i < stream_configs->num_configs; ++i)
1968 stream_configs->configs[i].fmt = *fmt;
1972 EXPORT_SYMBOL_GPL(v4l2_subdev_set_routing_with_fmt);
1974 int v4l2_subdev_routing_find_opposite_end(const struct v4l2_subdev_krouting *routing,
1975 u32 pad, u32 stream, u32 *other_pad,
1980 for (i = 0; i < routing->num_routes; ++i) {
1981 struct v4l2_subdev_route *route = &routing->routes[i];
1983 if (route->source_pad == pad &&
1984 route->source_stream == stream) {
1986 *other_pad = route->sink_pad;
1988 *other_stream = route->sink_stream;
1992 if (route->sink_pad == pad && route->sink_stream == stream) {
1994 *other_pad = route->source_pad;
1996 *other_stream = route->source_stream;
2003 EXPORT_SYMBOL_GPL(v4l2_subdev_routing_find_opposite_end);
2005 struct v4l2_mbus_framefmt *
2006 v4l2_subdev_state_get_opposite_stream_format(struct v4l2_subdev_state *state,
2007 u32 pad, u32 stream)
2009 u32 other_pad, other_stream;
2012 ret = v4l2_subdev_routing_find_opposite_end(&state->routing,
2014 &other_pad, &other_stream);
2018 return v4l2_subdev_state_get_format(state, other_pad, other_stream);
2020 EXPORT_SYMBOL_GPL(v4l2_subdev_state_get_opposite_stream_format);
2022 u64 v4l2_subdev_state_xlate_streams(const struct v4l2_subdev_state *state,
2023 u32 pad0, u32 pad1, u64 *streams)
2025 const struct v4l2_subdev_krouting *routing = &state->routing;
2026 struct v4l2_subdev_route *route;
2030 for_each_active_route(routing, route) {
2031 if (route->sink_pad == pad0 && route->source_pad == pad1 &&
2032 (*streams & BIT_ULL(route->sink_stream))) {
2033 streams0 |= BIT_ULL(route->sink_stream);
2034 streams1 |= BIT_ULL(route->source_stream);
2036 if (route->source_pad == pad0 && route->sink_pad == pad1 &&
2037 (*streams & BIT_ULL(route->source_stream))) {
2038 streams0 |= BIT_ULL(route->source_stream);
2039 streams1 |= BIT_ULL(route->sink_stream);
2043 *streams = streams0;
2046 EXPORT_SYMBOL_GPL(v4l2_subdev_state_xlate_streams);
2048 int v4l2_subdev_routing_validate(struct v4l2_subdev *sd,
2049 const struct v4l2_subdev_krouting *routing,
2050 enum v4l2_subdev_routing_restriction disallow)
2052 u32 *remote_pads = NULL;
2056 if (disallow & (V4L2_SUBDEV_ROUTING_NO_STREAM_MIX |
2057 V4L2_SUBDEV_ROUTING_NO_MULTIPLEXING)) {
2058 remote_pads = kcalloc(sd->entity.num_pads, sizeof(*remote_pads),
2063 for (i = 0; i < sd->entity.num_pads; ++i)
2064 remote_pads[i] = U32_MAX;
2067 for (i = 0; i < routing->num_routes; ++i) {
2068 const struct v4l2_subdev_route *route = &routing->routes[i];
2070 /* Validate the sink and source pad numbers. */
2071 if (route->sink_pad >= sd->entity.num_pads ||
2072 !(sd->entity.pads[route->sink_pad].flags & MEDIA_PAD_FL_SINK)) {
2073 dev_dbg(sd->dev, "route %u sink (%u) is not a sink pad\n",
2074 i, route->sink_pad);
2078 if (route->source_pad >= sd->entity.num_pads ||
2079 !(sd->entity.pads[route->source_pad].flags & MEDIA_PAD_FL_SOURCE)) {
2080 dev_dbg(sd->dev, "route %u source (%u) is not a source pad\n",
2081 i, route->source_pad);
2086 * V4L2_SUBDEV_ROUTING_NO_SINK_STREAM_MIX: all streams from a
2087 * sink pad must be routed to a single source pad.
2089 if (disallow & V4L2_SUBDEV_ROUTING_NO_SINK_STREAM_MIX) {
2090 if (remote_pads[route->sink_pad] != U32_MAX &&
2091 remote_pads[route->sink_pad] != route->source_pad) {
2093 "route %u attempts to mix %s streams\n",
2100 * V4L2_SUBDEV_ROUTING_NO_SOURCE_STREAM_MIX: all streams on a
2101 * source pad must originate from a single sink pad.
2103 if (disallow & V4L2_SUBDEV_ROUTING_NO_SOURCE_STREAM_MIX) {
2104 if (remote_pads[route->source_pad] != U32_MAX &&
2105 remote_pads[route->source_pad] != route->sink_pad) {
2107 "route %u attempts to mix %s streams\n",
2114 * V4L2_SUBDEV_ROUTING_NO_SINK_MULTIPLEXING: Pads on the sink
2115 * side can not do stream multiplexing, i.e. there can be only
2116 * a single stream in a sink pad.
2118 if (disallow & V4L2_SUBDEV_ROUTING_NO_SINK_MULTIPLEXING) {
2119 if (remote_pads[route->sink_pad] != U32_MAX) {
2121 "route %u attempts to multiplex on %s pad %u\n",
2122 i, "sink", route->sink_pad);
2128 * V4L2_SUBDEV_ROUTING_NO_SOURCE_MULTIPLEXING: Pads on the
2129 * source side can not do stream multiplexing, i.e. there can
2130 * be only a single stream in a source pad.
2132 if (disallow & V4L2_SUBDEV_ROUTING_NO_SOURCE_MULTIPLEXING) {
2133 if (remote_pads[route->source_pad] != U32_MAX) {
2135 "route %u attempts to multiplex on %s pad %u\n",
2136 i, "source", route->source_pad);
2142 remote_pads[route->sink_pad] = route->source_pad;
2143 remote_pads[route->source_pad] = route->sink_pad;
2146 for (j = i + 1; j < routing->num_routes; ++j) {
2147 const struct v4l2_subdev_route *r = &routing->routes[j];
2150 * V4L2_SUBDEV_ROUTING_NO_1_TO_N: No two routes can
2151 * originate from the same (sink) stream.
2153 if ((disallow & V4L2_SUBDEV_ROUTING_NO_1_TO_N) &&
2154 route->sink_pad == r->sink_pad &&
2155 route->sink_stream == r->sink_stream) {
2157 "routes %u and %u originate from same sink (%u/%u)\n",
2158 i, j, route->sink_pad,
2159 route->sink_stream);
2164 * V4L2_SUBDEV_ROUTING_NO_N_TO_1: No two routes can end
2165 * at the same (source) stream.
2167 if ((disallow & V4L2_SUBDEV_ROUTING_NO_N_TO_1) &&
2168 route->source_pad == r->source_pad &&
2169 route->source_stream == r->source_stream) {
2171 "routes %u and %u end at same source (%u/%u)\n",
2172 i, j, route->source_pad,
2173 route->source_stream);
2185 EXPORT_SYMBOL_GPL(v4l2_subdev_routing_validate);
2187 static void v4l2_subdev_collect_streams(struct v4l2_subdev *sd,
2188 struct v4l2_subdev_state *state,
2189 u32 pad, u64 streams_mask,
2191 u64 *enabled_streams)
2193 if (!(sd->flags & V4L2_SUBDEV_FL_STREAMS)) {
2194 *found_streams = BIT_ULL(0);
2196 (sd->enabled_pads & BIT_ULL(pad)) ? BIT_ULL(0) : 0;
2201 *enabled_streams = 0;
2203 for (unsigned int i = 0; i < state->stream_configs.num_configs; ++i) {
2204 const struct v4l2_subdev_stream_config *cfg =
2205 &state->stream_configs.configs[i];
2207 if (cfg->pad != pad || !(streams_mask & BIT_ULL(cfg->stream)))
2210 *found_streams |= BIT_ULL(cfg->stream);
2212 *enabled_streams |= BIT_ULL(cfg->stream);
2216 static void v4l2_subdev_set_streams_enabled(struct v4l2_subdev *sd,
2217 struct v4l2_subdev_state *state,
2218 u32 pad, u64 streams_mask,
2221 if (!(sd->flags & V4L2_SUBDEV_FL_STREAMS)) {
2223 sd->enabled_pads |= BIT_ULL(pad);
2225 sd->enabled_pads &= ~BIT_ULL(pad);
2229 for (unsigned int i = 0; i < state->stream_configs.num_configs; ++i) {
2230 struct v4l2_subdev_stream_config *cfg =
2231 &state->stream_configs.configs[i];
2233 if (cfg->pad == pad && (streams_mask & BIT_ULL(cfg->stream)))
2234 cfg->enabled = enabled;
2238 int v4l2_subdev_enable_streams(struct v4l2_subdev *sd, u32 pad,
2241 struct device *dev = sd->entity.graph_obj.mdev->dev;
2242 struct v4l2_subdev_state *state;
2243 bool already_streaming;
2244 u64 enabled_streams;
2249 /* A few basic sanity checks first. */
2250 if (pad >= sd->entity.num_pads)
2253 if (!(sd->entity.pads[pad].flags & MEDIA_PAD_FL_SOURCE))
2257 * We use a 64-bit bitmask for tracking enabled pads, so only subdevices
2258 * with 64 pads or less can be supported.
2260 if (pad >= sizeof(sd->enabled_pads) * BITS_PER_BYTE)
2266 /* Fallback on .s_stream() if .enable_streams() isn't available. */
2267 use_s_stream = !v4l2_subdev_has_op(sd, pad, enable_streams);
2270 state = v4l2_subdev_lock_and_get_active_state(sd);
2275 * Verify that the requested streams exist and that they are not
2279 v4l2_subdev_collect_streams(sd, state, pad, streams_mask,
2280 &found_streams, &enabled_streams);
2282 if (found_streams != streams_mask) {
2283 dev_dbg(dev, "streams 0x%llx not found on %s:%u\n",
2284 streams_mask & ~found_streams, sd->entity.name, pad);
2289 if (enabled_streams) {
2290 dev_dbg(dev, "streams 0x%llx already enabled on %s:%u\n",
2291 enabled_streams, sd->entity.name, pad);
2296 dev_dbg(dev, "enable streams %u:%#llx\n", pad, streams_mask);
2298 already_streaming = v4l2_subdev_is_streaming(sd);
2300 if (!use_s_stream) {
2301 /* Call the .enable_streams() operation. */
2302 ret = v4l2_subdev_call(sd, pad, enable_streams, state, pad,
2305 /* Start streaming when the first pad is enabled. */
2306 if (!already_streaming)
2307 ret = v4l2_subdev_call(sd, video, s_stream, 1);
2313 dev_dbg(dev, "enable streams %u:%#llx failed: %d\n", pad,
2318 /* Mark the streams as enabled. */
2319 v4l2_subdev_set_streams_enabled(sd, state, pad, streams_mask, true);
2322 * TODO: When all the drivers have been changed to use
2323 * v4l2_subdev_enable_streams() and v4l2_subdev_disable_streams(),
2324 * instead of calling .s_stream() operation directly, we can remove
2325 * the privacy LED handling from call_s_stream() and do it here
2328 if (!use_s_stream && !already_streaming)
2329 v4l2_subdev_enable_privacy_led(sd);
2333 v4l2_subdev_unlock_state(state);
2337 EXPORT_SYMBOL_GPL(v4l2_subdev_enable_streams);
2339 int v4l2_subdev_disable_streams(struct v4l2_subdev *sd, u32 pad,
2342 struct device *dev = sd->entity.graph_obj.mdev->dev;
2343 struct v4l2_subdev_state *state;
2344 u64 enabled_streams;
2349 /* A few basic sanity checks first. */
2350 if (pad >= sd->entity.num_pads)
2353 if (!(sd->entity.pads[pad].flags & MEDIA_PAD_FL_SOURCE))
2357 * We use a 64-bit bitmask for tracking enabled pads, so only subdevices
2358 * with 64 pads or less can be supported.
2360 if (pad >= sizeof(sd->enabled_pads) * BITS_PER_BYTE)
2366 /* Fallback on .s_stream() if .disable_streams() isn't available. */
2367 use_s_stream = !v4l2_subdev_has_op(sd, pad, disable_streams);
2370 state = v4l2_subdev_lock_and_get_active_state(sd);
2375 * Verify that the requested streams exist and that they are not
2379 v4l2_subdev_collect_streams(sd, state, pad, streams_mask,
2380 &found_streams, &enabled_streams);
2382 if (found_streams != streams_mask) {
2383 dev_dbg(dev, "streams 0x%llx not found on %s:%u\n",
2384 streams_mask & ~found_streams, sd->entity.name, pad);
2389 if (enabled_streams != streams_mask) {
2390 dev_dbg(dev, "streams 0x%llx already disabled on %s:%u\n",
2391 streams_mask & ~enabled_streams, sd->entity.name, pad);
2396 dev_dbg(dev, "disable streams %u:%#llx\n", pad, streams_mask);
2398 if (!use_s_stream) {
2399 /* Call the .disable_streams() operation. */
2400 ret = v4l2_subdev_call(sd, pad, disable_streams, state, pad,
2403 /* Stop streaming when the last streams are disabled. */
2405 if (!(sd->enabled_pads & ~BIT_ULL(pad)))
2406 ret = v4l2_subdev_call(sd, video, s_stream, 0);
2412 dev_dbg(dev, "disable streams %u:%#llx failed: %d\n", pad,
2417 v4l2_subdev_set_streams_enabled(sd, state, pad, streams_mask, false);
2420 if (!use_s_stream) {
2421 if (!v4l2_subdev_is_streaming(sd))
2422 v4l2_subdev_disable_privacy_led(sd);
2424 v4l2_subdev_unlock_state(state);
2429 EXPORT_SYMBOL_GPL(v4l2_subdev_disable_streams);
2431 int v4l2_subdev_s_stream_helper(struct v4l2_subdev *sd, int enable)
2433 struct v4l2_subdev_state *state;
2434 struct v4l2_subdev_route *route;
2435 struct media_pad *pad;
2436 u64 source_mask = 0;
2440 * Find the source pad. This helper is meant for subdevs that have a
2441 * single source pad, so failures shouldn't happen, but catch them
2442 * loudly nonetheless as they indicate a driver bug.
2444 media_entity_for_each_pad(&sd->entity, pad) {
2445 if (pad->flags & MEDIA_PAD_FL_SOURCE) {
2446 pad_index = pad->index;
2451 if (WARN_ON(pad_index == -1))
2454 if (sd->flags & V4L2_SUBDEV_FL_STREAMS) {
2456 * As there's a single source pad, just collect all the source
2459 state = v4l2_subdev_lock_and_get_active_state(sd);
2461 for_each_active_route(&state->routing, route)
2462 source_mask |= BIT_ULL(route->source_stream);
2464 v4l2_subdev_unlock_state(state);
2467 * For non-streams subdevices, there's a single implicit stream
2470 source_mask = BIT_ULL(0);
2474 return v4l2_subdev_enable_streams(sd, pad_index, source_mask);
2476 return v4l2_subdev_disable_streams(sd, pad_index, source_mask);
2478 EXPORT_SYMBOL_GPL(v4l2_subdev_s_stream_helper);
2480 #endif /* CONFIG_VIDEO_V4L2_SUBDEV_API */
2482 #endif /* CONFIG_MEDIA_CONTROLLER */
2484 void v4l2_subdev_init(struct v4l2_subdev *sd, const struct v4l2_subdev_ops *ops)
2486 INIT_LIST_HEAD(&sd->list);
2489 sd->v4l2_dev = NULL;
2493 sd->dev_priv = NULL;
2494 sd->host_priv = NULL;
2495 sd->privacy_led = NULL;
2496 INIT_LIST_HEAD(&sd->async_subdev_endpoint_list);
2497 #if defined(CONFIG_MEDIA_CONTROLLER)
2498 sd->entity.name = sd->name;
2499 sd->entity.obj_type = MEDIA_ENTITY_TYPE_V4L2_SUBDEV;
2500 sd->entity.function = MEDIA_ENT_F_V4L2_SUBDEV_UNKNOWN;
2503 EXPORT_SYMBOL(v4l2_subdev_init);
2505 void v4l2_subdev_notify_event(struct v4l2_subdev *sd,
2506 const struct v4l2_event *ev)
2508 v4l2_event_queue(sd->devnode, ev);
2509 v4l2_subdev_notify(sd, V4L2_DEVICE_NOTIFY_EVENT, (void *)ev);
2511 EXPORT_SYMBOL_GPL(v4l2_subdev_notify_event);
2513 bool v4l2_subdev_is_streaming(struct v4l2_subdev *sd)
2515 struct v4l2_subdev_state *state;
2517 if (!v4l2_subdev_has_op(sd, pad, enable_streams))
2518 return sd->s_stream_enabled;
2520 if (!(sd->flags & V4L2_SUBDEV_FL_STREAMS))
2521 return !!sd->enabled_pads;
2523 state = v4l2_subdev_get_locked_active_state(sd);
2525 for (unsigned int i = 0; i < state->stream_configs.num_configs; ++i) {
2526 const struct v4l2_subdev_stream_config *cfg;
2528 cfg = &state->stream_configs.configs[i];
2536 EXPORT_SYMBOL_GPL(v4l2_subdev_is_streaming);
2538 int v4l2_subdev_get_privacy_led(struct v4l2_subdev *sd)
2540 #if IS_REACHABLE(CONFIG_LEDS_CLASS)
2541 sd->privacy_led = led_get(sd->dev, "privacy-led");
2542 if (IS_ERR(sd->privacy_led) && PTR_ERR(sd->privacy_led) != -ENOENT)
2543 return dev_err_probe(sd->dev, PTR_ERR(sd->privacy_led),
2544 "getting privacy LED\n");
2546 if (!IS_ERR_OR_NULL(sd->privacy_led)) {
2547 mutex_lock(&sd->privacy_led->led_access);
2548 led_sysfs_disable(sd->privacy_led);
2549 led_trigger_remove(sd->privacy_led);
2550 led_set_brightness(sd->privacy_led, 0);
2551 mutex_unlock(&sd->privacy_led->led_access);
2556 EXPORT_SYMBOL_GPL(v4l2_subdev_get_privacy_led);
2558 void v4l2_subdev_put_privacy_led(struct v4l2_subdev *sd)
2560 #if IS_REACHABLE(CONFIG_LEDS_CLASS)
2561 if (!IS_ERR_OR_NULL(sd->privacy_led)) {
2562 mutex_lock(&sd->privacy_led->led_access);
2563 led_sysfs_enable(sd->privacy_led);
2564 mutex_unlock(&sd->privacy_led->led_access);
2565 led_put(sd->privacy_led);
2569 EXPORT_SYMBOL_GPL(v4l2_subdev_put_privacy_led);