1 // SPDX-License-Identifier: GPL-2.0-only
5 * Copyright (C) 2010 Nokia Corporation
7 * Contact: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
8 * Sakari Ailus <sakari.ailus@iki.fi>
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/types.h>
19 #include <linux/version.h>
20 #include <linux/videodev2.h>
22 #include <media/v4l2-ctrls.h>
23 #include <media/v4l2-device.h>
24 #include <media/v4l2-event.h>
25 #include <media/v4l2-fh.h>
26 #include <media/v4l2-ioctl.h>
28 #if defined(CONFIG_VIDEO_V4L2_SUBDEV_API)
30 * The Streams API is an experimental feature. To use the Streams API, set
31 * 'v4l2_subdev_enable_streams_api' to 1 below.
34 static bool v4l2_subdev_enable_streams_api;
38 * Maximum stream ID is 63 for now, as we use u64 bitmask to represent a set
41 * Note that V4L2_FRAME_DESC_ENTRY_MAX is related: V4L2_FRAME_DESC_ENTRY_MAX
42 * restricts the total number of streams in a pad, although the stream ID is
45 #define V4L2_SUBDEV_MAX_STREAM_ID 63
47 #include "v4l2-subdev-priv.h"
49 #if defined(CONFIG_VIDEO_V4L2_SUBDEV_API)
50 static int subdev_fh_init(struct v4l2_subdev_fh *fh, struct v4l2_subdev *sd)
52 struct v4l2_subdev_state *state;
53 static struct lock_class_key key;
55 state = __v4l2_subdev_state_alloc(sd, "fh->state->lock", &key);
57 return PTR_ERR(state);
64 static void subdev_fh_free(struct v4l2_subdev_fh *fh)
66 __v4l2_subdev_state_free(fh->state);
70 static int subdev_open(struct file *file)
72 struct video_device *vdev = video_devdata(file);
73 struct v4l2_subdev *sd = vdev_to_v4l2_subdev(vdev);
74 struct v4l2_subdev_fh *subdev_fh;
77 subdev_fh = kzalloc(sizeof(*subdev_fh), GFP_KERNEL);
78 if (subdev_fh == NULL)
81 ret = subdev_fh_init(subdev_fh, sd);
87 v4l2_fh_init(&subdev_fh->vfh, vdev);
88 v4l2_fh_add(&subdev_fh->vfh);
89 file->private_data = &subdev_fh->vfh;
91 if (sd->v4l2_dev->mdev && sd->entity.graph_obj.mdev->dev) {
94 owner = sd->entity.graph_obj.mdev->dev->driver->owner;
95 if (!try_module_get(owner)) {
99 subdev_fh->owner = owner;
102 if (sd->internal_ops && sd->internal_ops->open) {
103 ret = sd->internal_ops->open(sd, subdev_fh);
111 module_put(subdev_fh->owner);
112 v4l2_fh_del(&subdev_fh->vfh);
113 v4l2_fh_exit(&subdev_fh->vfh);
114 subdev_fh_free(subdev_fh);
120 static int subdev_close(struct file *file)
122 struct video_device *vdev = video_devdata(file);
123 struct v4l2_subdev *sd = vdev_to_v4l2_subdev(vdev);
124 struct v4l2_fh *vfh = file->private_data;
125 struct v4l2_subdev_fh *subdev_fh = to_v4l2_subdev_fh(vfh);
127 if (sd->internal_ops && sd->internal_ops->close)
128 sd->internal_ops->close(sd, subdev_fh);
129 module_put(subdev_fh->owner);
132 subdev_fh_free(subdev_fh);
134 file->private_data = NULL;
138 #else /* CONFIG_VIDEO_V4L2_SUBDEV_API */
139 static int subdev_open(struct file *file)
144 static int subdev_close(struct file *file)
148 #endif /* CONFIG_VIDEO_V4L2_SUBDEV_API */
150 static inline int check_which(u32 which)
152 if (which != V4L2_SUBDEV_FORMAT_TRY &&
153 which != V4L2_SUBDEV_FORMAT_ACTIVE)
159 static inline int check_pad(struct v4l2_subdev *sd, u32 pad)
161 #if defined(CONFIG_MEDIA_CONTROLLER)
162 if (sd->entity.num_pads) {
163 if (pad >= sd->entity.num_pads)
168 /* allow pad 0 on subdevices not registered as media entities */
174 static int check_state(struct v4l2_subdev *sd, struct v4l2_subdev_state *state,
175 u32 which, u32 pad, u32 stream)
177 if (sd->flags & V4L2_SUBDEV_FL_STREAMS) {
178 #if defined(CONFIG_VIDEO_V4L2_SUBDEV_API)
179 if (!v4l2_subdev_state_get_stream_format(state, pad, stream))
190 if (which == V4L2_SUBDEV_FORMAT_TRY && (!state || !state->pads))
196 static inline int check_format(struct v4l2_subdev *sd,
197 struct v4l2_subdev_state *state,
198 struct v4l2_subdev_format *format)
203 return check_which(format->which) ? : check_pad(sd, format->pad) ? :
204 check_state(sd, state, format->which, format->pad, format->stream);
207 static int call_get_fmt(struct v4l2_subdev *sd,
208 struct v4l2_subdev_state *state,
209 struct v4l2_subdev_format *format)
211 return check_format(sd, state, format) ? :
212 sd->ops->pad->get_fmt(sd, state, format);
215 static int call_set_fmt(struct v4l2_subdev *sd,
216 struct v4l2_subdev_state *state,
217 struct v4l2_subdev_format *format)
219 return check_format(sd, state, format) ? :
220 sd->ops->pad->set_fmt(sd, state, format);
223 static int call_enum_mbus_code(struct v4l2_subdev *sd,
224 struct v4l2_subdev_state *state,
225 struct v4l2_subdev_mbus_code_enum *code)
230 return check_which(code->which) ? : check_pad(sd, code->pad) ? :
231 check_state(sd, state, code->which, code->pad, code->stream) ? :
232 sd->ops->pad->enum_mbus_code(sd, state, code);
235 static int call_enum_frame_size(struct v4l2_subdev *sd,
236 struct v4l2_subdev_state *state,
237 struct v4l2_subdev_frame_size_enum *fse)
242 return check_which(fse->which) ? : check_pad(sd, fse->pad) ? :
243 check_state(sd, state, fse->which, fse->pad, fse->stream) ? :
244 sd->ops->pad->enum_frame_size(sd, state, fse);
247 static inline int check_frame_interval(struct v4l2_subdev *sd,
248 struct v4l2_subdev_frame_interval *fi)
253 return check_pad(sd, fi->pad);
256 static int call_g_frame_interval(struct v4l2_subdev *sd,
257 struct v4l2_subdev_frame_interval *fi)
259 return check_frame_interval(sd, fi) ? :
260 sd->ops->video->g_frame_interval(sd, fi);
263 static int call_s_frame_interval(struct v4l2_subdev *sd,
264 struct v4l2_subdev_frame_interval *fi)
266 return check_frame_interval(sd, fi) ? :
267 sd->ops->video->s_frame_interval(sd, fi);
270 static int call_enum_frame_interval(struct v4l2_subdev *sd,
271 struct v4l2_subdev_state *state,
272 struct v4l2_subdev_frame_interval_enum *fie)
277 return check_which(fie->which) ? : check_pad(sd, fie->pad) ? :
278 check_state(sd, state, fie->which, fie->pad, fie->stream) ? :
279 sd->ops->pad->enum_frame_interval(sd, state, fie);
282 static inline int check_selection(struct v4l2_subdev *sd,
283 struct v4l2_subdev_state *state,
284 struct v4l2_subdev_selection *sel)
289 return check_which(sel->which) ? : check_pad(sd, sel->pad) ? :
290 check_state(sd, state, sel->which, sel->pad, sel->stream);
293 static int call_get_selection(struct v4l2_subdev *sd,
294 struct v4l2_subdev_state *state,
295 struct v4l2_subdev_selection *sel)
297 return check_selection(sd, state, sel) ? :
298 sd->ops->pad->get_selection(sd, state, sel);
301 static int call_set_selection(struct v4l2_subdev *sd,
302 struct v4l2_subdev_state *state,
303 struct v4l2_subdev_selection *sel)
305 return check_selection(sd, state, sel) ? :
306 sd->ops->pad->set_selection(sd, state, sel);
309 static inline int check_edid(struct v4l2_subdev *sd,
310 struct v4l2_subdev_edid *edid)
315 if (edid->blocks && edid->edid == NULL)
318 return check_pad(sd, edid->pad);
321 static int call_get_edid(struct v4l2_subdev *sd, struct v4l2_subdev_edid *edid)
323 return check_edid(sd, edid) ? : sd->ops->pad->get_edid(sd, edid);
326 static int call_set_edid(struct v4l2_subdev *sd, struct v4l2_subdev_edid *edid)
328 return check_edid(sd, edid) ? : sd->ops->pad->set_edid(sd, edid);
331 static int call_dv_timings_cap(struct v4l2_subdev *sd,
332 struct v4l2_dv_timings_cap *cap)
337 return check_pad(sd, cap->pad) ? :
338 sd->ops->pad->dv_timings_cap(sd, cap);
341 static int call_enum_dv_timings(struct v4l2_subdev *sd,
342 struct v4l2_enum_dv_timings *dvt)
347 return check_pad(sd, dvt->pad) ? :
348 sd->ops->pad->enum_dv_timings(sd, dvt);
351 static int call_get_mbus_config(struct v4l2_subdev *sd, unsigned int pad,
352 struct v4l2_mbus_config *config)
354 return check_pad(sd, pad) ? :
355 sd->ops->pad->get_mbus_config(sd, pad, config);
358 static int call_s_stream(struct v4l2_subdev *sd, int enable)
362 #if IS_REACHABLE(CONFIG_LEDS_CLASS)
363 if (!IS_ERR_OR_NULL(sd->privacy_led)) {
365 led_set_brightness(sd->privacy_led,
366 sd->privacy_led->max_brightness);
368 led_set_brightness(sd->privacy_led, 0);
371 ret = sd->ops->video->s_stream(sd, enable);
373 if (!enable && ret < 0) {
374 dev_warn(sd->dev, "disabling streaming failed (%d)\n", ret);
381 #ifdef CONFIG_MEDIA_CONTROLLER
383 * Create state-management wrapper for pad ops dealing with subdev state. The
384 * wrapper handles the case where the caller does not provide the called
385 * subdev's state. This should be removed when all the callers are fixed.
387 #define DEFINE_STATE_WRAPPER(f, arg_type) \
388 static int call_##f##_state(struct v4l2_subdev *sd, \
389 struct v4l2_subdev_state *_state, \
392 struct v4l2_subdev_state *state = _state; \
395 state = v4l2_subdev_lock_and_get_active_state(sd); \
396 ret = call_##f(sd, state, arg); \
397 if (!_state && state) \
398 v4l2_subdev_unlock_state(state); \
402 #else /* CONFIG_MEDIA_CONTROLLER */
404 #define DEFINE_STATE_WRAPPER(f, arg_type) \
405 static int call_##f##_state(struct v4l2_subdev *sd, \
406 struct v4l2_subdev_state *state, \
409 return call_##f(sd, state, arg); \
412 #endif /* CONFIG_MEDIA_CONTROLLER */
414 DEFINE_STATE_WRAPPER(get_fmt, struct v4l2_subdev_format);
415 DEFINE_STATE_WRAPPER(set_fmt, struct v4l2_subdev_format);
416 DEFINE_STATE_WRAPPER(enum_mbus_code, struct v4l2_subdev_mbus_code_enum);
417 DEFINE_STATE_WRAPPER(enum_frame_size, struct v4l2_subdev_frame_size_enum);
418 DEFINE_STATE_WRAPPER(enum_frame_interval, struct v4l2_subdev_frame_interval_enum);
419 DEFINE_STATE_WRAPPER(get_selection, struct v4l2_subdev_selection);
420 DEFINE_STATE_WRAPPER(set_selection, struct v4l2_subdev_selection);
422 static const struct v4l2_subdev_pad_ops v4l2_subdev_call_pad_wrappers = {
423 .get_fmt = call_get_fmt_state,
424 .set_fmt = call_set_fmt_state,
425 .enum_mbus_code = call_enum_mbus_code_state,
426 .enum_frame_size = call_enum_frame_size_state,
427 .enum_frame_interval = call_enum_frame_interval_state,
428 .get_selection = call_get_selection_state,
429 .set_selection = call_set_selection_state,
430 .get_edid = call_get_edid,
431 .set_edid = call_set_edid,
432 .dv_timings_cap = call_dv_timings_cap,
433 .enum_dv_timings = call_enum_dv_timings,
434 .get_mbus_config = call_get_mbus_config,
437 static const struct v4l2_subdev_video_ops v4l2_subdev_call_video_wrappers = {
438 .g_frame_interval = call_g_frame_interval,
439 .s_frame_interval = call_s_frame_interval,
440 .s_stream = call_s_stream,
443 const struct v4l2_subdev_ops v4l2_subdev_call_wrappers = {
444 .pad = &v4l2_subdev_call_pad_wrappers,
445 .video = &v4l2_subdev_call_video_wrappers,
447 EXPORT_SYMBOL(v4l2_subdev_call_wrappers);
449 #if defined(CONFIG_VIDEO_V4L2_SUBDEV_API)
451 static struct v4l2_subdev_state *
452 subdev_ioctl_get_state(struct v4l2_subdev *sd, struct v4l2_subdev_fh *subdev_fh,
453 unsigned int cmd, void *arg)
460 case VIDIOC_SUBDEV_G_FMT:
461 case VIDIOC_SUBDEV_S_FMT:
462 which = ((struct v4l2_subdev_format *)arg)->which;
464 case VIDIOC_SUBDEV_G_CROP:
465 case VIDIOC_SUBDEV_S_CROP:
466 which = ((struct v4l2_subdev_crop *)arg)->which;
468 case VIDIOC_SUBDEV_ENUM_MBUS_CODE:
469 which = ((struct v4l2_subdev_mbus_code_enum *)arg)->which;
471 case VIDIOC_SUBDEV_ENUM_FRAME_SIZE:
472 which = ((struct v4l2_subdev_frame_size_enum *)arg)->which;
474 case VIDIOC_SUBDEV_ENUM_FRAME_INTERVAL:
475 which = ((struct v4l2_subdev_frame_interval_enum *)arg)->which;
477 case VIDIOC_SUBDEV_G_SELECTION:
478 case VIDIOC_SUBDEV_S_SELECTION:
479 which = ((struct v4l2_subdev_selection *)arg)->which;
481 case VIDIOC_SUBDEV_G_ROUTING:
482 case VIDIOC_SUBDEV_S_ROUTING:
483 which = ((struct v4l2_subdev_routing *)arg)->which;
487 return which == V4L2_SUBDEV_FORMAT_TRY ?
489 v4l2_subdev_get_unlocked_active_state(sd);
492 static long subdev_do_ioctl(struct file *file, unsigned int cmd, void *arg,
493 struct v4l2_subdev_state *state)
495 struct video_device *vdev = video_devdata(file);
496 struct v4l2_subdev *sd = vdev_to_v4l2_subdev(vdev);
497 struct v4l2_fh *vfh = file->private_data;
498 struct v4l2_subdev_fh *subdev_fh = to_v4l2_subdev_fh(vfh);
499 bool ro_subdev = test_bit(V4L2_FL_SUBDEV_RO_DEVNODE, &vdev->flags);
500 bool streams_subdev = sd->flags & V4L2_SUBDEV_FL_STREAMS;
501 bool client_supports_streams = subdev_fh->client_caps &
502 V4L2_SUBDEV_CLIENT_CAP_STREAMS;
506 case VIDIOC_SUBDEV_QUERYCAP: {
507 struct v4l2_subdev_capability *cap = arg;
509 memset(cap->reserved, 0, sizeof(cap->reserved));
510 cap->version = LINUX_VERSION_CODE;
512 (ro_subdev ? V4L2_SUBDEV_CAP_RO_SUBDEV : 0) |
513 (streams_subdev ? V4L2_SUBDEV_CAP_STREAMS : 0);
518 case VIDIOC_QUERYCTRL:
520 * TODO: this really should be folded into v4l2_queryctrl (this
521 * currently returns -EINVAL for NULL control handlers).
522 * However, v4l2_queryctrl() is still called directly by
523 * drivers as well and until that has been addressed I believe
524 * it is safer to do the check here. The same is true for the
525 * other control ioctls below.
527 if (!vfh->ctrl_handler)
529 return v4l2_queryctrl(vfh->ctrl_handler, arg);
531 case VIDIOC_QUERY_EXT_CTRL:
532 if (!vfh->ctrl_handler)
534 return v4l2_query_ext_ctrl(vfh->ctrl_handler, arg);
536 case VIDIOC_QUERYMENU:
537 if (!vfh->ctrl_handler)
539 return v4l2_querymenu(vfh->ctrl_handler, arg);
542 if (!vfh->ctrl_handler)
544 return v4l2_g_ctrl(vfh->ctrl_handler, arg);
547 if (!vfh->ctrl_handler)
549 return v4l2_s_ctrl(vfh, vfh->ctrl_handler, arg);
551 case VIDIOC_G_EXT_CTRLS:
552 if (!vfh->ctrl_handler)
554 return v4l2_g_ext_ctrls(vfh->ctrl_handler,
555 vdev, sd->v4l2_dev->mdev, arg);
557 case VIDIOC_S_EXT_CTRLS:
558 if (!vfh->ctrl_handler)
560 return v4l2_s_ext_ctrls(vfh, vfh->ctrl_handler,
561 vdev, sd->v4l2_dev->mdev, arg);
563 case VIDIOC_TRY_EXT_CTRLS:
564 if (!vfh->ctrl_handler)
566 return v4l2_try_ext_ctrls(vfh->ctrl_handler,
567 vdev, sd->v4l2_dev->mdev, arg);
570 if (!(sd->flags & V4L2_SUBDEV_FL_HAS_EVENTS))
573 return v4l2_event_dequeue(vfh, arg, file->f_flags & O_NONBLOCK);
575 case VIDIOC_SUBSCRIBE_EVENT:
576 return v4l2_subdev_call(sd, core, subscribe_event, vfh, arg);
578 case VIDIOC_UNSUBSCRIBE_EVENT:
579 return v4l2_subdev_call(sd, core, unsubscribe_event, vfh, arg);
581 #ifdef CONFIG_VIDEO_ADV_DEBUG
582 case VIDIOC_DBG_G_REGISTER:
584 struct v4l2_dbg_register *p = arg;
586 if (!capable(CAP_SYS_ADMIN))
588 return v4l2_subdev_call(sd, core, g_register, p);
590 case VIDIOC_DBG_S_REGISTER:
592 struct v4l2_dbg_register *p = arg;
594 if (!capable(CAP_SYS_ADMIN))
596 return v4l2_subdev_call(sd, core, s_register, p);
598 case VIDIOC_DBG_G_CHIP_INFO:
600 struct v4l2_dbg_chip_info *p = arg;
602 if (p->match.type != V4L2_CHIP_MATCH_SUBDEV || p->match.addr)
604 if (sd->ops->core && sd->ops->core->s_register)
605 p->flags |= V4L2_CHIP_FL_WRITABLE;
606 if (sd->ops->core && sd->ops->core->g_register)
607 p->flags |= V4L2_CHIP_FL_READABLE;
608 strscpy(p->name, sd->name, sizeof(p->name));
613 case VIDIOC_LOG_STATUS: {
616 pr_info("%s: ================= START STATUS =================\n",
618 ret = v4l2_subdev_call(sd, core, log_status);
619 pr_info("%s: ================== END STATUS ==================\n",
624 case VIDIOC_SUBDEV_G_FMT: {
625 struct v4l2_subdev_format *format = arg;
627 if (!client_supports_streams)
630 memset(format->reserved, 0, sizeof(format->reserved));
631 memset(format->format.reserved, 0, sizeof(format->format.reserved));
632 return v4l2_subdev_call(sd, pad, get_fmt, state, format);
635 case VIDIOC_SUBDEV_S_FMT: {
636 struct v4l2_subdev_format *format = arg;
638 if (format->which != V4L2_SUBDEV_FORMAT_TRY && ro_subdev)
641 if (!client_supports_streams)
644 memset(format->reserved, 0, sizeof(format->reserved));
645 memset(format->format.reserved, 0, sizeof(format->format.reserved));
646 return v4l2_subdev_call(sd, pad, set_fmt, state, format);
649 case VIDIOC_SUBDEV_G_CROP: {
650 struct v4l2_subdev_crop *crop = arg;
651 struct v4l2_subdev_selection sel;
653 if (!client_supports_streams)
656 memset(crop->reserved, 0, sizeof(crop->reserved));
657 memset(&sel, 0, sizeof(sel));
658 sel.which = crop->which;
660 sel.target = V4L2_SEL_TGT_CROP;
662 rval = v4l2_subdev_call(
663 sd, pad, get_selection, state, &sel);
670 case VIDIOC_SUBDEV_S_CROP: {
671 struct v4l2_subdev_crop *crop = arg;
672 struct v4l2_subdev_selection sel;
674 if (crop->which != V4L2_SUBDEV_FORMAT_TRY && ro_subdev)
677 if (!client_supports_streams)
680 memset(crop->reserved, 0, sizeof(crop->reserved));
681 memset(&sel, 0, sizeof(sel));
682 sel.which = crop->which;
684 sel.target = V4L2_SEL_TGT_CROP;
687 rval = v4l2_subdev_call(
688 sd, pad, set_selection, state, &sel);
695 case VIDIOC_SUBDEV_ENUM_MBUS_CODE: {
696 struct v4l2_subdev_mbus_code_enum *code = arg;
698 if (!client_supports_streams)
701 memset(code->reserved, 0, sizeof(code->reserved));
702 return v4l2_subdev_call(sd, pad, enum_mbus_code, state,
706 case VIDIOC_SUBDEV_ENUM_FRAME_SIZE: {
707 struct v4l2_subdev_frame_size_enum *fse = arg;
709 if (!client_supports_streams)
712 memset(fse->reserved, 0, sizeof(fse->reserved));
713 return v4l2_subdev_call(sd, pad, enum_frame_size, state,
717 case VIDIOC_SUBDEV_G_FRAME_INTERVAL: {
718 struct v4l2_subdev_frame_interval *fi = arg;
720 if (!client_supports_streams)
723 memset(fi->reserved, 0, sizeof(fi->reserved));
724 return v4l2_subdev_call(sd, video, g_frame_interval, arg);
727 case VIDIOC_SUBDEV_S_FRAME_INTERVAL: {
728 struct v4l2_subdev_frame_interval *fi = arg;
733 if (!client_supports_streams)
736 memset(fi->reserved, 0, sizeof(fi->reserved));
737 return v4l2_subdev_call(sd, video, s_frame_interval, arg);
740 case VIDIOC_SUBDEV_ENUM_FRAME_INTERVAL: {
741 struct v4l2_subdev_frame_interval_enum *fie = arg;
743 if (!client_supports_streams)
746 memset(fie->reserved, 0, sizeof(fie->reserved));
747 return v4l2_subdev_call(sd, pad, enum_frame_interval, state,
751 case VIDIOC_SUBDEV_G_SELECTION: {
752 struct v4l2_subdev_selection *sel = arg;
754 if (!client_supports_streams)
757 memset(sel->reserved, 0, sizeof(sel->reserved));
758 return v4l2_subdev_call(
759 sd, pad, get_selection, state, sel);
762 case VIDIOC_SUBDEV_S_SELECTION: {
763 struct v4l2_subdev_selection *sel = arg;
765 if (sel->which != V4L2_SUBDEV_FORMAT_TRY && ro_subdev)
768 if (!client_supports_streams)
771 memset(sel->reserved, 0, sizeof(sel->reserved));
772 return v4l2_subdev_call(
773 sd, pad, set_selection, state, sel);
776 case VIDIOC_G_EDID: {
777 struct v4l2_subdev_edid *edid = arg;
779 return v4l2_subdev_call(sd, pad, get_edid, edid);
782 case VIDIOC_S_EDID: {
783 struct v4l2_subdev_edid *edid = arg;
785 return v4l2_subdev_call(sd, pad, set_edid, edid);
788 case VIDIOC_SUBDEV_DV_TIMINGS_CAP: {
789 struct v4l2_dv_timings_cap *cap = arg;
791 return v4l2_subdev_call(sd, pad, dv_timings_cap, cap);
794 case VIDIOC_SUBDEV_ENUM_DV_TIMINGS: {
795 struct v4l2_enum_dv_timings *dvt = arg;
797 return v4l2_subdev_call(sd, pad, enum_dv_timings, dvt);
800 case VIDIOC_SUBDEV_QUERY_DV_TIMINGS:
801 return v4l2_subdev_call(sd, video, query_dv_timings, arg);
803 case VIDIOC_SUBDEV_G_DV_TIMINGS:
804 return v4l2_subdev_call(sd, video, g_dv_timings, arg);
806 case VIDIOC_SUBDEV_S_DV_TIMINGS:
810 return v4l2_subdev_call(sd, video, s_dv_timings, arg);
812 case VIDIOC_SUBDEV_G_STD:
813 return v4l2_subdev_call(sd, video, g_std, arg);
815 case VIDIOC_SUBDEV_S_STD: {
816 v4l2_std_id *std = arg;
821 return v4l2_subdev_call(sd, video, s_std, *std);
824 case VIDIOC_SUBDEV_ENUMSTD: {
825 struct v4l2_standard *p = arg;
828 if (v4l2_subdev_call(sd, video, g_tvnorms, &id))
831 return v4l_video_std_enumstd(p, id);
834 case VIDIOC_SUBDEV_QUERYSTD:
835 return v4l2_subdev_call(sd, video, querystd, arg);
837 case VIDIOC_SUBDEV_G_ROUTING: {
838 struct v4l2_subdev_routing *routing = arg;
839 struct v4l2_subdev_krouting *krouting;
841 if (!v4l2_subdev_enable_streams_api)
844 if (!(sd->flags & V4L2_SUBDEV_FL_STREAMS))
847 memset(routing->reserved, 0, sizeof(routing->reserved));
849 krouting = &state->routing;
851 if (routing->num_routes < krouting->num_routes) {
852 routing->num_routes = krouting->num_routes;
856 memcpy((struct v4l2_subdev_route *)(uintptr_t)routing->routes,
858 krouting->num_routes * sizeof(*krouting->routes));
859 routing->num_routes = krouting->num_routes;
864 case VIDIOC_SUBDEV_S_ROUTING: {
865 struct v4l2_subdev_routing *routing = arg;
866 struct v4l2_subdev_route *routes =
867 (struct v4l2_subdev_route *)(uintptr_t)routing->routes;
868 struct v4l2_subdev_krouting krouting = {};
871 if (!v4l2_subdev_enable_streams_api)
874 if (!(sd->flags & V4L2_SUBDEV_FL_STREAMS))
877 if (routing->which != V4L2_SUBDEV_FORMAT_TRY && ro_subdev)
880 memset(routing->reserved, 0, sizeof(routing->reserved));
882 for (i = 0; i < routing->num_routes; ++i) {
883 const struct v4l2_subdev_route *route = &routes[i];
884 const struct media_pad *pads = sd->entity.pads;
886 if (route->sink_stream > V4L2_SUBDEV_MAX_STREAM_ID ||
887 route->source_stream > V4L2_SUBDEV_MAX_STREAM_ID)
890 if (route->sink_pad >= sd->entity.num_pads)
893 if (!(pads[route->sink_pad].flags &
897 if (route->source_pad >= sd->entity.num_pads)
900 if (!(pads[route->source_pad].flags &
901 MEDIA_PAD_FL_SOURCE))
905 krouting.num_routes = routing->num_routes;
906 krouting.routes = routes;
908 return v4l2_subdev_call(sd, pad, set_routing, state,
909 routing->which, &krouting);
912 case VIDIOC_SUBDEV_G_CLIENT_CAP: {
913 struct v4l2_subdev_client_capability *client_cap = arg;
915 client_cap->capabilities = subdev_fh->client_caps;
920 case VIDIOC_SUBDEV_S_CLIENT_CAP: {
921 struct v4l2_subdev_client_capability *client_cap = arg;
924 * Clear V4L2_SUBDEV_CLIENT_CAP_STREAMS if streams API is not
925 * enabled. Remove this when streams API is no longer
928 if (!v4l2_subdev_enable_streams_api)
929 client_cap->capabilities &= ~V4L2_SUBDEV_CLIENT_CAP_STREAMS;
931 /* Filter out unsupported capabilities */
932 client_cap->capabilities &= V4L2_SUBDEV_CLIENT_CAP_STREAMS;
934 subdev_fh->client_caps = client_cap->capabilities;
940 return v4l2_subdev_call(sd, core, ioctl, cmd, arg);
946 static long subdev_do_ioctl_lock(struct file *file, unsigned int cmd, void *arg)
948 struct video_device *vdev = video_devdata(file);
949 struct mutex *lock = vdev->lock;
952 if (lock && mutex_lock_interruptible(lock))
955 if (video_is_registered(vdev)) {
956 struct v4l2_subdev *sd = vdev_to_v4l2_subdev(vdev);
957 struct v4l2_fh *vfh = file->private_data;
958 struct v4l2_subdev_fh *subdev_fh = to_v4l2_subdev_fh(vfh);
959 struct v4l2_subdev_state *state;
961 state = subdev_ioctl_get_state(sd, subdev_fh, cmd, arg);
964 v4l2_subdev_lock_state(state);
966 ret = subdev_do_ioctl(file, cmd, arg, state);
969 v4l2_subdev_unlock_state(state);
977 static long subdev_ioctl(struct file *file, unsigned int cmd,
980 return video_usercopy(file, cmd, arg, subdev_do_ioctl_lock);
984 static long subdev_compat_ioctl32(struct file *file, unsigned int cmd,
987 struct video_device *vdev = video_devdata(file);
988 struct v4l2_subdev *sd = vdev_to_v4l2_subdev(vdev);
990 return v4l2_subdev_call(sd, core, compat_ioctl32, cmd, arg);
994 #else /* CONFIG_VIDEO_V4L2_SUBDEV_API */
995 static long subdev_ioctl(struct file *file, unsigned int cmd,
1001 #ifdef CONFIG_COMPAT
1002 static long subdev_compat_ioctl32(struct file *file, unsigned int cmd,
1008 #endif /* CONFIG_VIDEO_V4L2_SUBDEV_API */
1010 static __poll_t subdev_poll(struct file *file, poll_table *wait)
1012 struct video_device *vdev = video_devdata(file);
1013 struct v4l2_subdev *sd = vdev_to_v4l2_subdev(vdev);
1014 struct v4l2_fh *fh = file->private_data;
1016 if (!(sd->flags & V4L2_SUBDEV_FL_HAS_EVENTS))
1019 poll_wait(file, &fh->wait, wait);
1021 if (v4l2_event_pending(fh))
1027 const struct v4l2_file_operations v4l2_subdev_fops = {
1028 .owner = THIS_MODULE,
1029 .open = subdev_open,
1030 .unlocked_ioctl = subdev_ioctl,
1031 #ifdef CONFIG_COMPAT
1032 .compat_ioctl32 = subdev_compat_ioctl32,
1034 .release = subdev_close,
1035 .poll = subdev_poll,
1038 #ifdef CONFIG_MEDIA_CONTROLLER
1040 int v4l2_subdev_get_fwnode_pad_1_to_1(struct media_entity *entity,
1041 struct fwnode_endpoint *endpoint)
1043 struct fwnode_handle *fwnode;
1044 struct v4l2_subdev *sd;
1046 if (!is_media_entity_v4l2_subdev(entity))
1049 sd = media_entity_to_v4l2_subdev(entity);
1051 fwnode = fwnode_graph_get_port_parent(endpoint->local_fwnode);
1052 fwnode_handle_put(fwnode);
1054 if (device_match_fwnode(sd->dev, fwnode))
1055 return endpoint->port;
1059 EXPORT_SYMBOL_GPL(v4l2_subdev_get_fwnode_pad_1_to_1);
1061 int v4l2_subdev_link_validate_default(struct v4l2_subdev *sd,
1062 struct media_link *link,
1063 struct v4l2_subdev_format *source_fmt,
1064 struct v4l2_subdev_format *sink_fmt)
1068 /* The width, height and code must match. */
1069 if (source_fmt->format.width != sink_fmt->format.width) {
1070 dev_dbg(sd->entity.graph_obj.mdev->dev,
1071 "%s: width does not match (source %u, sink %u)\n",
1073 source_fmt->format.width, sink_fmt->format.width);
1077 if (source_fmt->format.height != sink_fmt->format.height) {
1078 dev_dbg(sd->entity.graph_obj.mdev->dev,
1079 "%s: height does not match (source %u, sink %u)\n",
1081 source_fmt->format.height, sink_fmt->format.height);
1085 if (source_fmt->format.code != sink_fmt->format.code) {
1086 dev_dbg(sd->entity.graph_obj.mdev->dev,
1087 "%s: media bus code does not match (source 0x%8.8x, sink 0x%8.8x)\n",
1089 source_fmt->format.code, sink_fmt->format.code);
1093 /* The field order must match, or the sink field order must be NONE
1094 * to support interlaced hardware connected to bridges that support
1095 * progressive formats only.
1097 if (source_fmt->format.field != sink_fmt->format.field &&
1098 sink_fmt->format.field != V4L2_FIELD_NONE) {
1099 dev_dbg(sd->entity.graph_obj.mdev->dev,
1100 "%s: field does not match (source %u, sink %u)\n",
1102 source_fmt->format.field, sink_fmt->format.field);
1109 dev_dbg(sd->entity.graph_obj.mdev->dev,
1110 "%s: link was \"%s\":%u -> \"%s\":%u\n", __func__,
1111 link->source->entity->name, link->source->index,
1112 link->sink->entity->name, link->sink->index);
1116 EXPORT_SYMBOL_GPL(v4l2_subdev_link_validate_default);
1119 v4l2_subdev_link_validate_get_format(struct media_pad *pad, u32 stream,
1120 struct v4l2_subdev_format *fmt,
1123 struct v4l2_subdev_state *state;
1124 struct v4l2_subdev *sd;
1127 if (!is_media_entity_v4l2_subdev(pad->entity)) {
1128 WARN(pad->entity->function != MEDIA_ENT_F_IO_V4L,
1129 "Driver bug! Wrong media entity type 0x%08x, entity %s\n",
1130 pad->entity->function, pad->entity->name);
1135 sd = media_entity_to_v4l2_subdev(pad->entity);
1137 fmt->which = V4L2_SUBDEV_FORMAT_ACTIVE;
1138 fmt->pad = pad->index;
1139 fmt->stream = stream;
1142 state = v4l2_subdev_get_locked_active_state(sd);
1144 state = v4l2_subdev_lock_and_get_active_state(sd);
1146 ret = v4l2_subdev_call(sd, pad, get_fmt, state, fmt);
1148 if (!states_locked && state)
1149 v4l2_subdev_unlock_state(state);
1154 #if defined(CONFIG_VIDEO_V4L2_SUBDEV_API)
1156 static void __v4l2_link_validate_get_streams(struct media_pad *pad,
1160 struct v4l2_subdev_route *route;
1161 struct v4l2_subdev_state *state;
1162 struct v4l2_subdev *subdev;
1164 subdev = media_entity_to_v4l2_subdev(pad->entity);
1169 state = v4l2_subdev_get_locked_active_state(subdev);
1171 state = v4l2_subdev_lock_and_get_active_state(subdev);
1173 if (WARN_ON(!state))
1176 for_each_active_route(&state->routing, route) {
1180 if (pad->flags & MEDIA_PAD_FL_SOURCE) {
1181 route_pad = route->source_pad;
1182 route_stream = route->source_stream;
1184 route_pad = route->sink_pad;
1185 route_stream = route->sink_stream;
1188 if (route_pad != pad->index)
1191 *streams_mask |= BIT_ULL(route_stream);
1195 v4l2_subdev_unlock_state(state);
1198 #endif /* CONFIG_VIDEO_V4L2_SUBDEV_API */
1200 static void v4l2_link_validate_get_streams(struct media_pad *pad,
1204 struct v4l2_subdev *subdev = media_entity_to_v4l2_subdev(pad->entity);
1206 if (!(subdev->flags & V4L2_SUBDEV_FL_STREAMS)) {
1207 /* Non-streams subdevs have an implicit stream 0 */
1208 *streams_mask = BIT_ULL(0);
1212 #if defined(CONFIG_VIDEO_V4L2_SUBDEV_API)
1213 __v4l2_link_validate_get_streams(pad, streams_mask, states_locked);
1215 /* This shouldn't happen */
1220 static int v4l2_subdev_link_validate_locked(struct media_link *link, bool states_locked)
1222 struct v4l2_subdev *sink_subdev =
1223 media_entity_to_v4l2_subdev(link->sink->entity);
1224 struct device *dev = sink_subdev->entity.graph_obj.mdev->dev;
1225 u64 source_streams_mask;
1226 u64 sink_streams_mask;
1227 u64 dangling_sink_streams;
1231 dev_dbg(dev, "validating link \"%s\":%u -> \"%s\":%u\n",
1232 link->source->entity->name, link->source->index,
1233 link->sink->entity->name, link->sink->index);
1235 v4l2_link_validate_get_streams(link->source, &source_streams_mask, states_locked);
1236 v4l2_link_validate_get_streams(link->sink, &sink_streams_mask, states_locked);
1239 * It is ok to have more source streams than sink streams as extra
1240 * source streams can just be ignored by the receiver, but having extra
1241 * sink streams is an error as streams must have a source.
1243 dangling_sink_streams = (source_streams_mask ^ sink_streams_mask) &
1245 if (dangling_sink_streams) {
1246 dev_err(dev, "Dangling sink streams: mask %#llx\n",
1247 dangling_sink_streams);
1251 /* Validate source and sink stream formats */
1253 for (stream = 0; stream < sizeof(sink_streams_mask) * 8; ++stream) {
1254 struct v4l2_subdev_format sink_fmt, source_fmt;
1256 if (!(sink_streams_mask & BIT_ULL(stream)))
1259 dev_dbg(dev, "validating stream \"%s\":%u:%u -> \"%s\":%u:%u\n",
1260 link->source->entity->name, link->source->index, stream,
1261 link->sink->entity->name, link->sink->index, stream);
1263 ret = v4l2_subdev_link_validate_get_format(link->source, stream,
1264 &source_fmt, states_locked);
1267 "Failed to get format for \"%s\":%u:%u (but that's ok)\n",
1268 link->source->entity->name, link->source->index,
1273 ret = v4l2_subdev_link_validate_get_format(link->sink, stream,
1274 &sink_fmt, states_locked);
1277 "Failed to get format for \"%s\":%u:%u (but that's ok)\n",
1278 link->sink->entity->name, link->sink->index,
1283 /* TODO: add stream number to link_validate() */
1284 ret = v4l2_subdev_call(sink_subdev, pad, link_validate, link,
1285 &source_fmt, &sink_fmt);
1289 if (ret != -ENOIOCTLCMD)
1292 ret = v4l2_subdev_link_validate_default(sink_subdev, link,
1293 &source_fmt, &sink_fmt);
1302 int v4l2_subdev_link_validate(struct media_link *link)
1304 struct v4l2_subdev *source_sd, *sink_sd;
1305 struct v4l2_subdev_state *source_state, *sink_state;
1309 if (!is_media_entity_v4l2_subdev(link->sink->entity) ||
1310 !is_media_entity_v4l2_subdev(link->source->entity)) {
1311 pr_warn_once("%s of link '%s':%u->'%s':%u is not a V4L2 sub-device, driver bug!\n",
1312 !is_media_entity_v4l2_subdev(link->sink->entity) ?
1314 link->source->entity->name, link->source->index,
1315 link->sink->entity->name, link->sink->index);
1319 sink_sd = media_entity_to_v4l2_subdev(link->sink->entity);
1320 source_sd = media_entity_to_v4l2_subdev(link->source->entity);
1322 sink_state = v4l2_subdev_get_unlocked_active_state(sink_sd);
1323 source_state = v4l2_subdev_get_unlocked_active_state(source_sd);
1325 states_locked = sink_state && source_state;
1327 if (states_locked) {
1328 v4l2_subdev_lock_state(sink_state);
1329 v4l2_subdev_lock_state(source_state);
1332 ret = v4l2_subdev_link_validate_locked(link, states_locked);
1334 if (states_locked) {
1335 v4l2_subdev_unlock_state(sink_state);
1336 v4l2_subdev_unlock_state(source_state);
1341 EXPORT_SYMBOL_GPL(v4l2_subdev_link_validate);
1343 bool v4l2_subdev_has_pad_interdep(struct media_entity *entity,
1344 unsigned int pad0, unsigned int pad1)
1346 struct v4l2_subdev *sd = media_entity_to_v4l2_subdev(entity);
1347 struct v4l2_subdev_krouting *routing;
1348 struct v4l2_subdev_state *state;
1351 state = v4l2_subdev_lock_and_get_active_state(sd);
1353 routing = &state->routing;
1355 for (i = 0; i < routing->num_routes; ++i) {
1356 struct v4l2_subdev_route *route = &routing->routes[i];
1358 if (!(route->flags & V4L2_SUBDEV_ROUTE_FL_ACTIVE))
1361 if ((route->sink_pad == pad0 && route->source_pad == pad1) ||
1362 (route->source_pad == pad0 && route->sink_pad == pad1)) {
1363 v4l2_subdev_unlock_state(state);
1368 v4l2_subdev_unlock_state(state);
1372 EXPORT_SYMBOL_GPL(v4l2_subdev_has_pad_interdep);
1374 struct v4l2_subdev_state *
1375 __v4l2_subdev_state_alloc(struct v4l2_subdev *sd, const char *lock_name,
1376 struct lock_class_key *lock_key)
1378 struct v4l2_subdev_state *state;
1381 state = kzalloc(sizeof(*state), GFP_KERNEL);
1383 return ERR_PTR(-ENOMEM);
1385 __mutex_init(&state->_lock, lock_name, lock_key);
1387 state->lock = sd->state_lock;
1389 state->lock = &state->_lock;
1391 /* Drivers that support streams do not need the legacy pad config */
1392 if (!(sd->flags & V4L2_SUBDEV_FL_STREAMS) && sd->entity.num_pads) {
1393 state->pads = kvcalloc(sd->entity.num_pads,
1394 sizeof(*state->pads), GFP_KERNEL);
1402 * There can be no race at this point, but we lock the state anyway to
1403 * satisfy lockdep checks.
1405 v4l2_subdev_lock_state(state);
1406 ret = v4l2_subdev_call(sd, pad, init_cfg, state);
1407 v4l2_subdev_unlock_state(state);
1409 if (ret < 0 && ret != -ENOIOCTLCMD)
1415 if (state && state->pads)
1416 kvfree(state->pads);
1420 return ERR_PTR(ret);
1422 EXPORT_SYMBOL_GPL(__v4l2_subdev_state_alloc);
1424 void __v4l2_subdev_state_free(struct v4l2_subdev_state *state)
1429 mutex_destroy(&state->_lock);
1431 kfree(state->routing.routes);
1432 kvfree(state->stream_configs.configs);
1433 kvfree(state->pads);
1436 EXPORT_SYMBOL_GPL(__v4l2_subdev_state_free);
1438 int __v4l2_subdev_init_finalize(struct v4l2_subdev *sd, const char *name,
1439 struct lock_class_key *key)
1441 struct v4l2_subdev_state *state;
1443 state = __v4l2_subdev_state_alloc(sd, name, key);
1445 return PTR_ERR(state);
1447 sd->active_state = state;
1451 EXPORT_SYMBOL_GPL(__v4l2_subdev_init_finalize);
1453 void v4l2_subdev_cleanup(struct v4l2_subdev *sd)
1455 struct v4l2_async_subdev_endpoint *ase, *ase_tmp;
1457 __v4l2_subdev_state_free(sd->active_state);
1458 sd->active_state = NULL;
1460 if (list_empty(&sd->async_subdev_endpoint_list))
1463 list_for_each_entry_safe(ase, ase_tmp, &sd->async_subdev_endpoint_list,
1464 async_subdev_endpoint_entry) {
1465 list_del(&ase->async_subdev_endpoint_entry);
1470 EXPORT_SYMBOL_GPL(v4l2_subdev_cleanup);
1472 #if defined(CONFIG_VIDEO_V4L2_SUBDEV_API)
1475 v4l2_subdev_init_stream_configs(struct v4l2_subdev_stream_configs *stream_configs,
1476 const struct v4l2_subdev_krouting *routing)
1478 struct v4l2_subdev_stream_configs new_configs = { 0 };
1479 struct v4l2_subdev_route *route;
1482 /* Count number of formats needed */
1483 for_each_active_route(routing, route) {
1485 * Each route needs a format on both ends of the route.
1487 new_configs.num_configs += 2;
1490 if (new_configs.num_configs) {
1491 new_configs.configs = kvcalloc(new_configs.num_configs,
1492 sizeof(*new_configs.configs),
1495 if (!new_configs.configs)
1500 * Fill in the 'pad' and stream' value for each item in the array from
1505 for_each_active_route(routing, route) {
1506 new_configs.configs[idx].pad = route->sink_pad;
1507 new_configs.configs[idx].stream = route->sink_stream;
1511 new_configs.configs[idx].pad = route->source_pad;
1512 new_configs.configs[idx].stream = route->source_stream;
1517 kvfree(stream_configs->configs);
1518 *stream_configs = new_configs;
1523 int v4l2_subdev_get_fmt(struct v4l2_subdev *sd, struct v4l2_subdev_state *state,
1524 struct v4l2_subdev_format *format)
1526 struct v4l2_mbus_framefmt *fmt;
1528 if (sd->flags & V4L2_SUBDEV_FL_STREAMS)
1529 fmt = v4l2_subdev_state_get_stream_format(state, format->pad,
1531 else if (format->pad < sd->entity.num_pads && format->stream == 0)
1532 fmt = v4l2_subdev_get_pad_format(sd, state, format->pad);
1539 format->format = *fmt;
1543 EXPORT_SYMBOL_GPL(v4l2_subdev_get_fmt);
1545 int v4l2_subdev_set_routing(struct v4l2_subdev *sd,
1546 struct v4l2_subdev_state *state,
1547 const struct v4l2_subdev_krouting *routing)
1549 struct v4l2_subdev_krouting *dst = &state->routing;
1550 const struct v4l2_subdev_krouting *src = routing;
1551 struct v4l2_subdev_krouting new_routing = { 0 };
1555 if (unlikely(check_mul_overflow((size_t)src->num_routes,
1556 sizeof(*src->routes), &bytes)))
1559 lockdep_assert_held(state->lock);
1561 if (src->num_routes > 0) {
1562 new_routing.routes = kmemdup(src->routes, bytes, GFP_KERNEL);
1563 if (!new_routing.routes)
1567 new_routing.num_routes = src->num_routes;
1569 r = v4l2_subdev_init_stream_configs(&state->stream_configs,
1572 kfree(new_routing.routes);
1581 EXPORT_SYMBOL_GPL(v4l2_subdev_set_routing);
1583 struct v4l2_subdev_route *
1584 __v4l2_subdev_next_active_route(const struct v4l2_subdev_krouting *routing,
1585 struct v4l2_subdev_route *route)
1590 route = &routing->routes[0];
1592 for (; route < routing->routes + routing->num_routes; ++route) {
1593 if (!(route->flags & V4L2_SUBDEV_ROUTE_FL_ACTIVE))
1601 EXPORT_SYMBOL_GPL(__v4l2_subdev_next_active_route);
1603 int v4l2_subdev_set_routing_with_fmt(struct v4l2_subdev *sd,
1604 struct v4l2_subdev_state *state,
1605 const struct v4l2_subdev_krouting *routing,
1606 const struct v4l2_mbus_framefmt *fmt)
1608 struct v4l2_subdev_stream_configs *stream_configs;
1612 ret = v4l2_subdev_set_routing(sd, state, routing);
1616 stream_configs = &state->stream_configs;
1618 for (i = 0; i < stream_configs->num_configs; ++i)
1619 stream_configs->configs[i].fmt = *fmt;
1623 EXPORT_SYMBOL_GPL(v4l2_subdev_set_routing_with_fmt);
1625 struct v4l2_mbus_framefmt *
1626 v4l2_subdev_state_get_stream_format(struct v4l2_subdev_state *state,
1627 unsigned int pad, u32 stream)
1629 struct v4l2_subdev_stream_configs *stream_configs;
1632 lockdep_assert_held(state->lock);
1634 stream_configs = &state->stream_configs;
1636 for (i = 0; i < stream_configs->num_configs; ++i) {
1637 if (stream_configs->configs[i].pad == pad &&
1638 stream_configs->configs[i].stream == stream)
1639 return &stream_configs->configs[i].fmt;
1644 EXPORT_SYMBOL_GPL(v4l2_subdev_state_get_stream_format);
1647 v4l2_subdev_state_get_stream_crop(struct v4l2_subdev_state *state,
1648 unsigned int pad, u32 stream)
1650 struct v4l2_subdev_stream_configs *stream_configs;
1653 lockdep_assert_held(state->lock);
1655 stream_configs = &state->stream_configs;
1657 for (i = 0; i < stream_configs->num_configs; ++i) {
1658 if (stream_configs->configs[i].pad == pad &&
1659 stream_configs->configs[i].stream == stream)
1660 return &stream_configs->configs[i].crop;
1665 EXPORT_SYMBOL_GPL(v4l2_subdev_state_get_stream_crop);
1668 v4l2_subdev_state_get_stream_compose(struct v4l2_subdev_state *state,
1669 unsigned int pad, u32 stream)
1671 struct v4l2_subdev_stream_configs *stream_configs;
1674 lockdep_assert_held(state->lock);
1676 stream_configs = &state->stream_configs;
1678 for (i = 0; i < stream_configs->num_configs; ++i) {
1679 if (stream_configs->configs[i].pad == pad &&
1680 stream_configs->configs[i].stream == stream)
1681 return &stream_configs->configs[i].compose;
1686 EXPORT_SYMBOL_GPL(v4l2_subdev_state_get_stream_compose);
1688 int v4l2_subdev_routing_find_opposite_end(const struct v4l2_subdev_krouting *routing,
1689 u32 pad, u32 stream, u32 *other_pad,
1694 for (i = 0; i < routing->num_routes; ++i) {
1695 struct v4l2_subdev_route *route = &routing->routes[i];
1697 if (route->source_pad == pad &&
1698 route->source_stream == stream) {
1700 *other_pad = route->sink_pad;
1702 *other_stream = route->sink_stream;
1706 if (route->sink_pad == pad && route->sink_stream == stream) {
1708 *other_pad = route->source_pad;
1710 *other_stream = route->source_stream;
1717 EXPORT_SYMBOL_GPL(v4l2_subdev_routing_find_opposite_end);
1719 struct v4l2_mbus_framefmt *
1720 v4l2_subdev_state_get_opposite_stream_format(struct v4l2_subdev_state *state,
1721 u32 pad, u32 stream)
1723 u32 other_pad, other_stream;
1726 ret = v4l2_subdev_routing_find_opposite_end(&state->routing,
1728 &other_pad, &other_stream);
1732 return v4l2_subdev_state_get_stream_format(state, other_pad,
1735 EXPORT_SYMBOL_GPL(v4l2_subdev_state_get_opposite_stream_format);
1737 u64 v4l2_subdev_state_xlate_streams(const struct v4l2_subdev_state *state,
1738 u32 pad0, u32 pad1, u64 *streams)
1740 const struct v4l2_subdev_krouting *routing = &state->routing;
1741 struct v4l2_subdev_route *route;
1745 for_each_active_route(routing, route) {
1746 if (route->sink_pad == pad0 && route->source_pad == pad1 &&
1747 (*streams & BIT_ULL(route->sink_stream))) {
1748 streams0 |= BIT_ULL(route->sink_stream);
1749 streams1 |= BIT_ULL(route->source_stream);
1751 if (route->source_pad == pad0 && route->sink_pad == pad1 &&
1752 (*streams & BIT_ULL(route->source_stream))) {
1753 streams0 |= BIT_ULL(route->source_stream);
1754 streams1 |= BIT_ULL(route->sink_stream);
1758 *streams = streams0;
1761 EXPORT_SYMBOL_GPL(v4l2_subdev_state_xlate_streams);
1763 int v4l2_subdev_routing_validate(struct v4l2_subdev *sd,
1764 const struct v4l2_subdev_krouting *routing,
1765 enum v4l2_subdev_routing_restriction disallow)
1767 u32 *remote_pads = NULL;
1771 if (disallow & (V4L2_SUBDEV_ROUTING_NO_STREAM_MIX |
1772 V4L2_SUBDEV_ROUTING_NO_MULTIPLEXING)) {
1773 remote_pads = kcalloc(sd->entity.num_pads, sizeof(*remote_pads),
1778 for (i = 0; i < sd->entity.num_pads; ++i)
1779 remote_pads[i] = U32_MAX;
1782 for (i = 0; i < routing->num_routes; ++i) {
1783 const struct v4l2_subdev_route *route = &routing->routes[i];
1785 /* Validate the sink and source pad numbers. */
1786 if (route->sink_pad >= sd->entity.num_pads ||
1787 !(sd->entity.pads[route->sink_pad].flags & MEDIA_PAD_FL_SINK)) {
1788 dev_dbg(sd->dev, "route %u sink (%u) is not a sink pad\n",
1789 i, route->sink_pad);
1793 if (route->source_pad >= sd->entity.num_pads ||
1794 !(sd->entity.pads[route->source_pad].flags & MEDIA_PAD_FL_SOURCE)) {
1795 dev_dbg(sd->dev, "route %u source (%u) is not a source pad\n",
1796 i, route->source_pad);
1801 * V4L2_SUBDEV_ROUTING_NO_SINK_STREAM_MIX: all streams from a
1802 * sink pad must be routed to a single source pad.
1804 if (disallow & V4L2_SUBDEV_ROUTING_NO_SINK_STREAM_MIX) {
1805 if (remote_pads[route->sink_pad] != U32_MAX &&
1806 remote_pads[route->sink_pad] != route->source_pad) {
1808 "route %u attempts to mix %s streams\n",
1815 * V4L2_SUBDEV_ROUTING_NO_SOURCE_STREAM_MIX: all streams on a
1816 * source pad must originate from a single sink pad.
1818 if (disallow & V4L2_SUBDEV_ROUTING_NO_SOURCE_STREAM_MIX) {
1819 if (remote_pads[route->source_pad] != U32_MAX &&
1820 remote_pads[route->source_pad] != route->sink_pad) {
1822 "route %u attempts to mix %s streams\n",
1829 * V4L2_SUBDEV_ROUTING_NO_SINK_MULTIPLEXING: Pads on the sink
1830 * side can not do stream multiplexing, i.e. there can be only
1831 * a single stream in a sink pad.
1833 if (disallow & V4L2_SUBDEV_ROUTING_NO_SINK_MULTIPLEXING) {
1834 if (remote_pads[route->sink_pad] != U32_MAX) {
1836 "route %u attempts to multiplex on %s pad %u\n",
1837 i, "sink", route->sink_pad);
1843 * V4L2_SUBDEV_ROUTING_NO_SOURCE_MULTIPLEXING: Pads on the
1844 * source side can not do stream multiplexing, i.e. there can
1845 * be only a single stream in a source pad.
1847 if (disallow & V4L2_SUBDEV_ROUTING_NO_SOURCE_MULTIPLEXING) {
1848 if (remote_pads[route->source_pad] != U32_MAX) {
1850 "route %u attempts to multiplex on %s pad %u\n",
1851 i, "source", route->source_pad);
1857 remote_pads[route->sink_pad] = route->source_pad;
1858 remote_pads[route->source_pad] = route->sink_pad;
1861 for (j = i + 1; j < routing->num_routes; ++j) {
1862 const struct v4l2_subdev_route *r = &routing->routes[j];
1865 * V4L2_SUBDEV_ROUTING_NO_1_TO_N: No two routes can
1866 * originate from the same (sink) stream.
1868 if ((disallow & V4L2_SUBDEV_ROUTING_NO_1_TO_N) &&
1869 route->sink_pad == r->sink_pad &&
1870 route->sink_stream == r->sink_stream) {
1872 "routes %u and %u originate from same sink (%u/%u)\n",
1873 i, j, route->sink_pad,
1874 route->sink_stream);
1879 * V4L2_SUBDEV_ROUTING_NO_N_TO_1: No two routes can end
1880 * at the same (source) stream.
1882 if ((disallow & V4L2_SUBDEV_ROUTING_NO_N_TO_1) &&
1883 route->source_pad == r->source_pad &&
1884 route->source_stream == r->source_stream) {
1886 "routes %u and %u end at same source (%u/%u)\n",
1887 i, j, route->source_pad,
1888 route->source_stream);
1900 EXPORT_SYMBOL_GPL(v4l2_subdev_routing_validate);
1902 static int v4l2_subdev_enable_streams_fallback(struct v4l2_subdev *sd, u32 pad,
1905 struct device *dev = sd->entity.graph_obj.mdev->dev;
1910 * The subdev doesn't implement pad-based stream enable, fall back
1911 * on the .s_stream() operation. This can only be done for subdevs that
1912 * have a single source pad, as sd->enabled_streams is global to the
1915 if (!(sd->entity.pads[pad].flags & MEDIA_PAD_FL_SOURCE))
1918 for (i = 0; i < sd->entity.num_pads; ++i) {
1919 if (i != pad && sd->entity.pads[i].flags & MEDIA_PAD_FL_SOURCE)
1923 if (sd->enabled_streams & streams_mask) {
1924 dev_dbg(dev, "set of streams %#llx already enabled on %s:%u\n",
1925 streams_mask, sd->entity.name, pad);
1929 /* Start streaming when the first streams are enabled. */
1930 if (!sd->enabled_streams) {
1931 ret = v4l2_subdev_call(sd, video, s_stream, 1);
1936 sd->enabled_streams |= streams_mask;
1941 int v4l2_subdev_enable_streams(struct v4l2_subdev *sd, u32 pad,
1944 struct device *dev = sd->entity.graph_obj.mdev->dev;
1945 struct v4l2_subdev_state *state;
1946 u64 found_streams = 0;
1950 /* A few basic sanity checks first. */
1951 if (pad >= sd->entity.num_pads)
1957 /* Fallback on .s_stream() if .enable_streams() isn't available. */
1958 if (!sd->ops->pad || !sd->ops->pad->enable_streams)
1959 return v4l2_subdev_enable_streams_fallback(sd, pad,
1962 state = v4l2_subdev_lock_and_get_active_state(sd);
1965 * Verify that the requested streams exist and that they are not
1968 for (i = 0; i < state->stream_configs.num_configs; ++i) {
1969 struct v4l2_subdev_stream_config *cfg =
1970 &state->stream_configs.configs[i];
1972 if (cfg->pad != pad || !(streams_mask & BIT_ULL(cfg->stream)))
1975 found_streams |= BIT_ULL(cfg->stream);
1978 dev_dbg(dev, "stream %u already enabled on %s:%u\n",
1979 cfg->stream, sd->entity.name, pad);
1985 if (found_streams != streams_mask) {
1986 dev_dbg(dev, "streams 0x%llx not found on %s:%u\n",
1987 streams_mask & ~found_streams, sd->entity.name, pad);
1992 dev_dbg(dev, "enable streams %u:%#llx\n", pad, streams_mask);
1994 /* Call the .enable_streams() operation. */
1995 ret = v4l2_subdev_call(sd, pad, enable_streams, state, pad,
1998 dev_dbg(dev, "enable streams %u:%#llx failed: %d\n", pad,
2003 /* Mark the streams as enabled. */
2004 for (i = 0; i < state->stream_configs.num_configs; ++i) {
2005 struct v4l2_subdev_stream_config *cfg =
2006 &state->stream_configs.configs[i];
2008 if (cfg->pad == pad && (streams_mask & BIT_ULL(cfg->stream)))
2009 cfg->enabled = true;
2013 v4l2_subdev_unlock_state(state);
2017 EXPORT_SYMBOL_GPL(v4l2_subdev_enable_streams);
2019 static int v4l2_subdev_disable_streams_fallback(struct v4l2_subdev *sd, u32 pad,
2022 struct device *dev = sd->entity.graph_obj.mdev->dev;
2027 * If the subdev doesn't implement pad-based stream enable, fall back
2028 * on the .s_stream() operation. This can only be done for subdevs that
2029 * have a single source pad, as sd->enabled_streams is global to the
2032 if (!(sd->entity.pads[pad].flags & MEDIA_PAD_FL_SOURCE))
2035 for (i = 0; i < sd->entity.num_pads; ++i) {
2036 if (i != pad && sd->entity.pads[i].flags & MEDIA_PAD_FL_SOURCE)
2040 if ((sd->enabled_streams & streams_mask) != streams_mask) {
2041 dev_dbg(dev, "set of streams %#llx already disabled on %s:%u\n",
2042 streams_mask, sd->entity.name, pad);
2046 /* Stop streaming when the last streams are disabled. */
2047 if (!(sd->enabled_streams & ~streams_mask)) {
2048 ret = v4l2_subdev_call(sd, video, s_stream, 0);
2053 sd->enabled_streams &= ~streams_mask;
2058 int v4l2_subdev_disable_streams(struct v4l2_subdev *sd, u32 pad,
2061 struct device *dev = sd->entity.graph_obj.mdev->dev;
2062 struct v4l2_subdev_state *state;
2063 u64 found_streams = 0;
2067 /* A few basic sanity checks first. */
2068 if (pad >= sd->entity.num_pads)
2074 /* Fallback on .s_stream() if .disable_streams() isn't available. */
2075 if (!sd->ops->pad || !sd->ops->pad->disable_streams)
2076 return v4l2_subdev_disable_streams_fallback(sd, pad,
2079 state = v4l2_subdev_lock_and_get_active_state(sd);
2082 * Verify that the requested streams exist and that they are not
2085 for (i = 0; i < state->stream_configs.num_configs; ++i) {
2086 struct v4l2_subdev_stream_config *cfg =
2087 &state->stream_configs.configs[i];
2089 if (cfg->pad != pad || !(streams_mask & BIT_ULL(cfg->stream)))
2092 found_streams |= BIT_ULL(cfg->stream);
2094 if (!cfg->enabled) {
2095 dev_dbg(dev, "stream %u already disabled on %s:%u\n",
2096 cfg->stream, sd->entity.name, pad);
2102 if (found_streams != streams_mask) {
2103 dev_dbg(dev, "streams 0x%llx not found on %s:%u\n",
2104 streams_mask & ~found_streams, sd->entity.name, pad);
2109 dev_dbg(dev, "disable streams %u:%#llx\n", pad, streams_mask);
2111 /* Call the .disable_streams() operation. */
2112 ret = v4l2_subdev_call(sd, pad, disable_streams, state, pad,
2115 dev_dbg(dev, "disable streams %u:%#llx failed: %d\n", pad,
2120 /* Mark the streams as disabled. */
2121 for (i = 0; i < state->stream_configs.num_configs; ++i) {
2122 struct v4l2_subdev_stream_config *cfg =
2123 &state->stream_configs.configs[i];
2125 if (cfg->pad == pad && (streams_mask & BIT_ULL(cfg->stream)))
2126 cfg->enabled = false;
2130 v4l2_subdev_unlock_state(state);
2134 EXPORT_SYMBOL_GPL(v4l2_subdev_disable_streams);
2136 int v4l2_subdev_s_stream_helper(struct v4l2_subdev *sd, int enable)
2138 struct v4l2_subdev_state *state;
2139 struct v4l2_subdev_route *route;
2140 struct media_pad *pad;
2141 u64 source_mask = 0;
2145 * Find the source pad. This helper is meant for subdevs that have a
2146 * single source pad, so failures shouldn't happen, but catch them
2147 * loudly nonetheless as they indicate a driver bug.
2149 media_entity_for_each_pad(&sd->entity, pad) {
2150 if (pad->flags & MEDIA_PAD_FL_SOURCE) {
2151 pad_index = pad->index;
2156 if (WARN_ON(pad_index == -1))
2160 * As there's a single source pad, just collect all the source streams.
2162 state = v4l2_subdev_lock_and_get_active_state(sd);
2164 for_each_active_route(&state->routing, route)
2165 source_mask |= BIT_ULL(route->source_stream);
2167 v4l2_subdev_unlock_state(state);
2170 return v4l2_subdev_enable_streams(sd, pad_index, source_mask);
2172 return v4l2_subdev_disable_streams(sd, pad_index, source_mask);
2174 EXPORT_SYMBOL_GPL(v4l2_subdev_s_stream_helper);
2176 #endif /* CONFIG_VIDEO_V4L2_SUBDEV_API */
2178 #endif /* CONFIG_MEDIA_CONTROLLER */
2180 void v4l2_subdev_init(struct v4l2_subdev *sd, const struct v4l2_subdev_ops *ops)
2182 INIT_LIST_HEAD(&sd->list);
2185 sd->v4l2_dev = NULL;
2189 sd->dev_priv = NULL;
2190 sd->host_priv = NULL;
2191 sd->privacy_led = NULL;
2192 INIT_LIST_HEAD(&sd->async_subdev_endpoint_list);
2193 #if defined(CONFIG_MEDIA_CONTROLLER)
2194 sd->entity.name = sd->name;
2195 sd->entity.obj_type = MEDIA_ENTITY_TYPE_V4L2_SUBDEV;
2196 sd->entity.function = MEDIA_ENT_F_V4L2_SUBDEV_UNKNOWN;
2199 EXPORT_SYMBOL(v4l2_subdev_init);
2201 void v4l2_subdev_notify_event(struct v4l2_subdev *sd,
2202 const struct v4l2_event *ev)
2204 v4l2_event_queue(sd->devnode, ev);
2205 v4l2_subdev_notify(sd, V4L2_DEVICE_NOTIFY_EVENT, (void *)ev);
2207 EXPORT_SYMBOL_GPL(v4l2_subdev_notify_event);
2209 int v4l2_subdev_get_privacy_led(struct v4l2_subdev *sd)
2211 #if IS_REACHABLE(CONFIG_LEDS_CLASS)
2212 sd->privacy_led = led_get(sd->dev, "privacy-led");
2213 if (IS_ERR(sd->privacy_led) && PTR_ERR(sd->privacy_led) != -ENOENT)
2214 return dev_err_probe(sd->dev, PTR_ERR(sd->privacy_led),
2215 "getting privacy LED\n");
2217 if (!IS_ERR_OR_NULL(sd->privacy_led)) {
2218 mutex_lock(&sd->privacy_led->led_access);
2219 led_sysfs_disable(sd->privacy_led);
2220 led_trigger_remove(sd->privacy_led);
2221 led_set_brightness(sd->privacy_led, 0);
2222 mutex_unlock(&sd->privacy_led->led_access);
2227 EXPORT_SYMBOL_GPL(v4l2_subdev_get_privacy_led);
2229 void v4l2_subdev_put_privacy_led(struct v4l2_subdev *sd)
2231 #if IS_REACHABLE(CONFIG_LEDS_CLASS)
2232 if (!IS_ERR_OR_NULL(sd->privacy_led)) {
2233 mutex_lock(&sd->privacy_led->led_access);
2234 led_sysfs_enable(sd->privacy_led);
2235 mutex_unlock(&sd->privacy_led->led_access);
2236 led_put(sd->privacy_led);
2240 EXPORT_SYMBOL_GPL(v4l2_subdev_put_privacy_led);