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 * If the streams API is not enabled, remove V4L2_SUBDEV_CAP_STREAMS.
507 * Remove this when the API is no longer experimental.
509 if (!v4l2_subdev_enable_streams_api)
510 streams_subdev = false;
513 case VIDIOC_SUBDEV_QUERYCAP: {
514 struct v4l2_subdev_capability *cap = arg;
516 memset(cap->reserved, 0, sizeof(cap->reserved));
517 cap->version = LINUX_VERSION_CODE;
519 (ro_subdev ? V4L2_SUBDEV_CAP_RO_SUBDEV : 0) |
520 (streams_subdev ? V4L2_SUBDEV_CAP_STREAMS : 0);
525 case VIDIOC_QUERYCTRL:
527 * TODO: this really should be folded into v4l2_queryctrl (this
528 * currently returns -EINVAL for NULL control handlers).
529 * However, v4l2_queryctrl() is still called directly by
530 * drivers as well and until that has been addressed I believe
531 * it is safer to do the check here. The same is true for the
532 * other control ioctls below.
534 if (!vfh->ctrl_handler)
536 return v4l2_queryctrl(vfh->ctrl_handler, arg);
538 case VIDIOC_QUERY_EXT_CTRL:
539 if (!vfh->ctrl_handler)
541 return v4l2_query_ext_ctrl(vfh->ctrl_handler, arg);
543 case VIDIOC_QUERYMENU:
544 if (!vfh->ctrl_handler)
546 return v4l2_querymenu(vfh->ctrl_handler, arg);
549 if (!vfh->ctrl_handler)
551 return v4l2_g_ctrl(vfh->ctrl_handler, arg);
554 if (!vfh->ctrl_handler)
556 return v4l2_s_ctrl(vfh, vfh->ctrl_handler, arg);
558 case VIDIOC_G_EXT_CTRLS:
559 if (!vfh->ctrl_handler)
561 return v4l2_g_ext_ctrls(vfh->ctrl_handler,
562 vdev, sd->v4l2_dev->mdev, arg);
564 case VIDIOC_S_EXT_CTRLS:
565 if (!vfh->ctrl_handler)
567 return v4l2_s_ext_ctrls(vfh, vfh->ctrl_handler,
568 vdev, sd->v4l2_dev->mdev, arg);
570 case VIDIOC_TRY_EXT_CTRLS:
571 if (!vfh->ctrl_handler)
573 return v4l2_try_ext_ctrls(vfh->ctrl_handler,
574 vdev, sd->v4l2_dev->mdev, arg);
577 if (!(sd->flags & V4L2_SUBDEV_FL_HAS_EVENTS))
580 return v4l2_event_dequeue(vfh, arg, file->f_flags & O_NONBLOCK);
582 case VIDIOC_SUBSCRIBE_EVENT:
583 return v4l2_subdev_call(sd, core, subscribe_event, vfh, arg);
585 case VIDIOC_UNSUBSCRIBE_EVENT:
586 return v4l2_subdev_call(sd, core, unsubscribe_event, vfh, arg);
588 #ifdef CONFIG_VIDEO_ADV_DEBUG
589 case VIDIOC_DBG_G_REGISTER:
591 struct v4l2_dbg_register *p = arg;
593 if (!capable(CAP_SYS_ADMIN))
595 return v4l2_subdev_call(sd, core, g_register, p);
597 case VIDIOC_DBG_S_REGISTER:
599 struct v4l2_dbg_register *p = arg;
601 if (!capable(CAP_SYS_ADMIN))
603 return v4l2_subdev_call(sd, core, s_register, p);
605 case VIDIOC_DBG_G_CHIP_INFO:
607 struct v4l2_dbg_chip_info *p = arg;
609 if (p->match.type != V4L2_CHIP_MATCH_SUBDEV || p->match.addr)
611 if (sd->ops->core && sd->ops->core->s_register)
612 p->flags |= V4L2_CHIP_FL_WRITABLE;
613 if (sd->ops->core && sd->ops->core->g_register)
614 p->flags |= V4L2_CHIP_FL_READABLE;
615 strscpy(p->name, sd->name, sizeof(p->name));
620 case VIDIOC_LOG_STATUS: {
623 pr_info("%s: ================= START STATUS =================\n",
625 ret = v4l2_subdev_call(sd, core, log_status);
626 pr_info("%s: ================== END STATUS ==================\n",
631 case VIDIOC_SUBDEV_G_FMT: {
632 struct v4l2_subdev_format *format = arg;
634 if (!client_supports_streams)
637 memset(format->reserved, 0, sizeof(format->reserved));
638 memset(format->format.reserved, 0, sizeof(format->format.reserved));
639 return v4l2_subdev_call(sd, pad, get_fmt, state, format);
642 case VIDIOC_SUBDEV_S_FMT: {
643 struct v4l2_subdev_format *format = arg;
645 if (format->which != V4L2_SUBDEV_FORMAT_TRY && ro_subdev)
648 if (!client_supports_streams)
651 memset(format->reserved, 0, sizeof(format->reserved));
652 memset(format->format.reserved, 0, sizeof(format->format.reserved));
653 return v4l2_subdev_call(sd, pad, set_fmt, state, format);
656 case VIDIOC_SUBDEV_G_CROP: {
657 struct v4l2_subdev_crop *crop = arg;
658 struct v4l2_subdev_selection sel;
660 if (!client_supports_streams)
663 memset(crop->reserved, 0, sizeof(crop->reserved));
664 memset(&sel, 0, sizeof(sel));
665 sel.which = crop->which;
667 sel.target = V4L2_SEL_TGT_CROP;
669 rval = v4l2_subdev_call(
670 sd, pad, get_selection, state, &sel);
677 case VIDIOC_SUBDEV_S_CROP: {
678 struct v4l2_subdev_crop *crop = arg;
679 struct v4l2_subdev_selection sel;
681 if (crop->which != V4L2_SUBDEV_FORMAT_TRY && ro_subdev)
684 if (!client_supports_streams)
687 memset(crop->reserved, 0, sizeof(crop->reserved));
688 memset(&sel, 0, sizeof(sel));
689 sel.which = crop->which;
691 sel.target = V4L2_SEL_TGT_CROP;
694 rval = v4l2_subdev_call(
695 sd, pad, set_selection, state, &sel);
702 case VIDIOC_SUBDEV_ENUM_MBUS_CODE: {
703 struct v4l2_subdev_mbus_code_enum *code = arg;
705 if (!client_supports_streams)
708 memset(code->reserved, 0, sizeof(code->reserved));
709 return v4l2_subdev_call(sd, pad, enum_mbus_code, state,
713 case VIDIOC_SUBDEV_ENUM_FRAME_SIZE: {
714 struct v4l2_subdev_frame_size_enum *fse = arg;
716 if (!client_supports_streams)
719 memset(fse->reserved, 0, sizeof(fse->reserved));
720 return v4l2_subdev_call(sd, pad, enum_frame_size, state,
724 case VIDIOC_SUBDEV_G_FRAME_INTERVAL: {
725 struct v4l2_subdev_frame_interval *fi = arg;
727 if (!client_supports_streams)
730 memset(fi->reserved, 0, sizeof(fi->reserved));
731 return v4l2_subdev_call(sd, video, g_frame_interval, arg);
734 case VIDIOC_SUBDEV_S_FRAME_INTERVAL: {
735 struct v4l2_subdev_frame_interval *fi = arg;
740 if (!client_supports_streams)
743 memset(fi->reserved, 0, sizeof(fi->reserved));
744 return v4l2_subdev_call(sd, video, s_frame_interval, arg);
747 case VIDIOC_SUBDEV_ENUM_FRAME_INTERVAL: {
748 struct v4l2_subdev_frame_interval_enum *fie = arg;
750 if (!client_supports_streams)
753 memset(fie->reserved, 0, sizeof(fie->reserved));
754 return v4l2_subdev_call(sd, pad, enum_frame_interval, state,
758 case VIDIOC_SUBDEV_G_SELECTION: {
759 struct v4l2_subdev_selection *sel = arg;
761 if (!client_supports_streams)
764 memset(sel->reserved, 0, sizeof(sel->reserved));
765 return v4l2_subdev_call(
766 sd, pad, get_selection, state, sel);
769 case VIDIOC_SUBDEV_S_SELECTION: {
770 struct v4l2_subdev_selection *sel = arg;
772 if (sel->which != V4L2_SUBDEV_FORMAT_TRY && ro_subdev)
775 if (!client_supports_streams)
778 memset(sel->reserved, 0, sizeof(sel->reserved));
779 return v4l2_subdev_call(
780 sd, pad, set_selection, state, sel);
783 case VIDIOC_G_EDID: {
784 struct v4l2_subdev_edid *edid = arg;
786 return v4l2_subdev_call(sd, pad, get_edid, edid);
789 case VIDIOC_S_EDID: {
790 struct v4l2_subdev_edid *edid = arg;
792 return v4l2_subdev_call(sd, pad, set_edid, edid);
795 case VIDIOC_SUBDEV_DV_TIMINGS_CAP: {
796 struct v4l2_dv_timings_cap *cap = arg;
798 return v4l2_subdev_call(sd, pad, dv_timings_cap, cap);
801 case VIDIOC_SUBDEV_ENUM_DV_TIMINGS: {
802 struct v4l2_enum_dv_timings *dvt = arg;
804 return v4l2_subdev_call(sd, pad, enum_dv_timings, dvt);
807 case VIDIOC_SUBDEV_QUERY_DV_TIMINGS:
808 return v4l2_subdev_call(sd, video, query_dv_timings, arg);
810 case VIDIOC_SUBDEV_G_DV_TIMINGS:
811 return v4l2_subdev_call(sd, video, g_dv_timings, arg);
813 case VIDIOC_SUBDEV_S_DV_TIMINGS:
817 return v4l2_subdev_call(sd, video, s_dv_timings, arg);
819 case VIDIOC_SUBDEV_G_STD:
820 return v4l2_subdev_call(sd, video, g_std, arg);
822 case VIDIOC_SUBDEV_S_STD: {
823 v4l2_std_id *std = arg;
828 return v4l2_subdev_call(sd, video, s_std, *std);
831 case VIDIOC_SUBDEV_ENUMSTD: {
832 struct v4l2_standard *p = arg;
835 if (v4l2_subdev_call(sd, video, g_tvnorms, &id))
838 return v4l_video_std_enumstd(p, id);
841 case VIDIOC_SUBDEV_QUERYSTD:
842 return v4l2_subdev_call(sd, video, querystd, arg);
844 case VIDIOC_SUBDEV_G_ROUTING: {
845 struct v4l2_subdev_routing *routing = arg;
846 struct v4l2_subdev_krouting *krouting;
848 if (!v4l2_subdev_enable_streams_api)
851 if (!(sd->flags & V4L2_SUBDEV_FL_STREAMS))
854 memset(routing->reserved, 0, sizeof(routing->reserved));
856 krouting = &state->routing;
858 if (routing->num_routes < krouting->num_routes) {
859 routing->num_routes = krouting->num_routes;
863 memcpy((struct v4l2_subdev_route *)(uintptr_t)routing->routes,
865 krouting->num_routes * sizeof(*krouting->routes));
866 routing->num_routes = krouting->num_routes;
871 case VIDIOC_SUBDEV_S_ROUTING: {
872 struct v4l2_subdev_routing *routing = arg;
873 struct v4l2_subdev_route *routes =
874 (struct v4l2_subdev_route *)(uintptr_t)routing->routes;
875 struct v4l2_subdev_krouting krouting = {};
878 if (!v4l2_subdev_enable_streams_api)
881 if (!(sd->flags & V4L2_SUBDEV_FL_STREAMS))
884 if (routing->which != V4L2_SUBDEV_FORMAT_TRY && ro_subdev)
887 memset(routing->reserved, 0, sizeof(routing->reserved));
889 for (i = 0; i < routing->num_routes; ++i) {
890 const struct v4l2_subdev_route *route = &routes[i];
891 const struct media_pad *pads = sd->entity.pads;
893 if (route->sink_stream > V4L2_SUBDEV_MAX_STREAM_ID ||
894 route->source_stream > V4L2_SUBDEV_MAX_STREAM_ID)
897 if (route->sink_pad >= sd->entity.num_pads)
900 if (!(pads[route->sink_pad].flags &
904 if (route->source_pad >= sd->entity.num_pads)
907 if (!(pads[route->source_pad].flags &
908 MEDIA_PAD_FL_SOURCE))
912 krouting.num_routes = routing->num_routes;
913 krouting.routes = routes;
915 return v4l2_subdev_call(sd, pad, set_routing, state,
916 routing->which, &krouting);
919 case VIDIOC_SUBDEV_G_CLIENT_CAP: {
920 struct v4l2_subdev_client_capability *client_cap = arg;
922 client_cap->capabilities = subdev_fh->client_caps;
927 case VIDIOC_SUBDEV_S_CLIENT_CAP: {
928 struct v4l2_subdev_client_capability *client_cap = arg;
931 * Clear V4L2_SUBDEV_CLIENT_CAP_STREAMS if streams API is not
932 * enabled. Remove this when streams API is no longer
935 if (!v4l2_subdev_enable_streams_api)
936 client_cap->capabilities &= ~V4L2_SUBDEV_CLIENT_CAP_STREAMS;
938 /* Filter out unsupported capabilities */
939 client_cap->capabilities &= V4L2_SUBDEV_CLIENT_CAP_STREAMS;
941 subdev_fh->client_caps = client_cap->capabilities;
947 return v4l2_subdev_call(sd, core, ioctl, cmd, arg);
953 static long subdev_do_ioctl_lock(struct file *file, unsigned int cmd, void *arg)
955 struct video_device *vdev = video_devdata(file);
956 struct mutex *lock = vdev->lock;
959 if (lock && mutex_lock_interruptible(lock))
962 if (video_is_registered(vdev)) {
963 struct v4l2_subdev *sd = vdev_to_v4l2_subdev(vdev);
964 struct v4l2_fh *vfh = file->private_data;
965 struct v4l2_subdev_fh *subdev_fh = to_v4l2_subdev_fh(vfh);
966 struct v4l2_subdev_state *state;
968 state = subdev_ioctl_get_state(sd, subdev_fh, cmd, arg);
971 v4l2_subdev_lock_state(state);
973 ret = subdev_do_ioctl(file, cmd, arg, state);
976 v4l2_subdev_unlock_state(state);
984 static long subdev_ioctl(struct file *file, unsigned int cmd,
987 return video_usercopy(file, cmd, arg, subdev_do_ioctl_lock);
991 static long subdev_compat_ioctl32(struct file *file, unsigned int cmd,
994 struct video_device *vdev = video_devdata(file);
995 struct v4l2_subdev *sd = vdev_to_v4l2_subdev(vdev);
997 return v4l2_subdev_call(sd, core, compat_ioctl32, cmd, arg);
1001 #else /* CONFIG_VIDEO_V4L2_SUBDEV_API */
1002 static long subdev_ioctl(struct file *file, unsigned int cmd,
1008 #ifdef CONFIG_COMPAT
1009 static long subdev_compat_ioctl32(struct file *file, unsigned int cmd,
1015 #endif /* CONFIG_VIDEO_V4L2_SUBDEV_API */
1017 static __poll_t subdev_poll(struct file *file, poll_table *wait)
1019 struct video_device *vdev = video_devdata(file);
1020 struct v4l2_subdev *sd = vdev_to_v4l2_subdev(vdev);
1021 struct v4l2_fh *fh = file->private_data;
1023 if (!(sd->flags & V4L2_SUBDEV_FL_HAS_EVENTS))
1026 poll_wait(file, &fh->wait, wait);
1028 if (v4l2_event_pending(fh))
1034 const struct v4l2_file_operations v4l2_subdev_fops = {
1035 .owner = THIS_MODULE,
1036 .open = subdev_open,
1037 .unlocked_ioctl = subdev_ioctl,
1038 #ifdef CONFIG_COMPAT
1039 .compat_ioctl32 = subdev_compat_ioctl32,
1041 .release = subdev_close,
1042 .poll = subdev_poll,
1045 #ifdef CONFIG_MEDIA_CONTROLLER
1047 int v4l2_subdev_get_fwnode_pad_1_to_1(struct media_entity *entity,
1048 struct fwnode_endpoint *endpoint)
1050 struct fwnode_handle *fwnode;
1051 struct v4l2_subdev *sd;
1053 if (!is_media_entity_v4l2_subdev(entity))
1056 sd = media_entity_to_v4l2_subdev(entity);
1058 fwnode = fwnode_graph_get_port_parent(endpoint->local_fwnode);
1059 fwnode_handle_put(fwnode);
1061 if (device_match_fwnode(sd->dev, fwnode))
1062 return endpoint->port;
1066 EXPORT_SYMBOL_GPL(v4l2_subdev_get_fwnode_pad_1_to_1);
1068 int v4l2_subdev_link_validate_default(struct v4l2_subdev *sd,
1069 struct media_link *link,
1070 struct v4l2_subdev_format *source_fmt,
1071 struct v4l2_subdev_format *sink_fmt)
1075 /* The width, height and code must match. */
1076 if (source_fmt->format.width != sink_fmt->format.width) {
1077 dev_dbg(sd->entity.graph_obj.mdev->dev,
1078 "%s: width does not match (source %u, sink %u)\n",
1080 source_fmt->format.width, sink_fmt->format.width);
1084 if (source_fmt->format.height != sink_fmt->format.height) {
1085 dev_dbg(sd->entity.graph_obj.mdev->dev,
1086 "%s: height does not match (source %u, sink %u)\n",
1088 source_fmt->format.height, sink_fmt->format.height);
1092 if (source_fmt->format.code != sink_fmt->format.code) {
1093 dev_dbg(sd->entity.graph_obj.mdev->dev,
1094 "%s: media bus code does not match (source 0x%8.8x, sink 0x%8.8x)\n",
1096 source_fmt->format.code, sink_fmt->format.code);
1100 /* The field order must match, or the sink field order must be NONE
1101 * to support interlaced hardware connected to bridges that support
1102 * progressive formats only.
1104 if (source_fmt->format.field != sink_fmt->format.field &&
1105 sink_fmt->format.field != V4L2_FIELD_NONE) {
1106 dev_dbg(sd->entity.graph_obj.mdev->dev,
1107 "%s: field does not match (source %u, sink %u)\n",
1109 source_fmt->format.field, sink_fmt->format.field);
1116 dev_dbg(sd->entity.graph_obj.mdev->dev,
1117 "%s: link was \"%s\":%u -> \"%s\":%u\n", __func__,
1118 link->source->entity->name, link->source->index,
1119 link->sink->entity->name, link->sink->index);
1123 EXPORT_SYMBOL_GPL(v4l2_subdev_link_validate_default);
1126 v4l2_subdev_link_validate_get_format(struct media_pad *pad, u32 stream,
1127 struct v4l2_subdev_format *fmt,
1130 struct v4l2_subdev_state *state;
1131 struct v4l2_subdev *sd;
1134 if (!is_media_entity_v4l2_subdev(pad->entity)) {
1135 WARN(pad->entity->function != MEDIA_ENT_F_IO_V4L,
1136 "Driver bug! Wrong media entity type 0x%08x, entity %s\n",
1137 pad->entity->function, pad->entity->name);
1142 sd = media_entity_to_v4l2_subdev(pad->entity);
1144 fmt->which = V4L2_SUBDEV_FORMAT_ACTIVE;
1145 fmt->pad = pad->index;
1146 fmt->stream = stream;
1149 state = v4l2_subdev_get_locked_active_state(sd);
1151 state = v4l2_subdev_lock_and_get_active_state(sd);
1153 ret = v4l2_subdev_call(sd, pad, get_fmt, state, fmt);
1155 if (!states_locked && state)
1156 v4l2_subdev_unlock_state(state);
1161 #if defined(CONFIG_VIDEO_V4L2_SUBDEV_API)
1163 static void __v4l2_link_validate_get_streams(struct media_pad *pad,
1167 struct v4l2_subdev_route *route;
1168 struct v4l2_subdev_state *state;
1169 struct v4l2_subdev *subdev;
1171 subdev = media_entity_to_v4l2_subdev(pad->entity);
1176 state = v4l2_subdev_get_locked_active_state(subdev);
1178 state = v4l2_subdev_lock_and_get_active_state(subdev);
1180 if (WARN_ON(!state))
1183 for_each_active_route(&state->routing, route) {
1187 if (pad->flags & MEDIA_PAD_FL_SOURCE) {
1188 route_pad = route->source_pad;
1189 route_stream = route->source_stream;
1191 route_pad = route->sink_pad;
1192 route_stream = route->sink_stream;
1195 if (route_pad != pad->index)
1198 *streams_mask |= BIT_ULL(route_stream);
1202 v4l2_subdev_unlock_state(state);
1205 #endif /* CONFIG_VIDEO_V4L2_SUBDEV_API */
1207 static void v4l2_link_validate_get_streams(struct media_pad *pad,
1211 struct v4l2_subdev *subdev = media_entity_to_v4l2_subdev(pad->entity);
1213 if (!(subdev->flags & V4L2_SUBDEV_FL_STREAMS)) {
1214 /* Non-streams subdevs have an implicit stream 0 */
1215 *streams_mask = BIT_ULL(0);
1219 #if defined(CONFIG_VIDEO_V4L2_SUBDEV_API)
1220 __v4l2_link_validate_get_streams(pad, streams_mask, states_locked);
1222 /* This shouldn't happen */
1227 static int v4l2_subdev_link_validate_locked(struct media_link *link, bool states_locked)
1229 struct v4l2_subdev *sink_subdev =
1230 media_entity_to_v4l2_subdev(link->sink->entity);
1231 struct device *dev = sink_subdev->entity.graph_obj.mdev->dev;
1232 u64 source_streams_mask;
1233 u64 sink_streams_mask;
1234 u64 dangling_sink_streams;
1238 dev_dbg(dev, "validating link \"%s\":%u -> \"%s\":%u\n",
1239 link->source->entity->name, link->source->index,
1240 link->sink->entity->name, link->sink->index);
1242 v4l2_link_validate_get_streams(link->source, &source_streams_mask, states_locked);
1243 v4l2_link_validate_get_streams(link->sink, &sink_streams_mask, states_locked);
1246 * It is ok to have more source streams than sink streams as extra
1247 * source streams can just be ignored by the receiver, but having extra
1248 * sink streams is an error as streams must have a source.
1250 dangling_sink_streams = (source_streams_mask ^ sink_streams_mask) &
1252 if (dangling_sink_streams) {
1253 dev_err(dev, "Dangling sink streams: mask %#llx\n",
1254 dangling_sink_streams);
1258 /* Validate source and sink stream formats */
1260 for (stream = 0; stream < sizeof(sink_streams_mask) * 8; ++stream) {
1261 struct v4l2_subdev_format sink_fmt, source_fmt;
1263 if (!(sink_streams_mask & BIT_ULL(stream)))
1266 dev_dbg(dev, "validating stream \"%s\":%u:%u -> \"%s\":%u:%u\n",
1267 link->source->entity->name, link->source->index, stream,
1268 link->sink->entity->name, link->sink->index, stream);
1270 ret = v4l2_subdev_link_validate_get_format(link->source, stream,
1271 &source_fmt, states_locked);
1274 "Failed to get format for \"%s\":%u:%u (but that's ok)\n",
1275 link->source->entity->name, link->source->index,
1280 ret = v4l2_subdev_link_validate_get_format(link->sink, stream,
1281 &sink_fmt, states_locked);
1284 "Failed to get format for \"%s\":%u:%u (but that's ok)\n",
1285 link->sink->entity->name, link->sink->index,
1290 /* TODO: add stream number to link_validate() */
1291 ret = v4l2_subdev_call(sink_subdev, pad, link_validate, link,
1292 &source_fmt, &sink_fmt);
1296 if (ret != -ENOIOCTLCMD)
1299 ret = v4l2_subdev_link_validate_default(sink_subdev, link,
1300 &source_fmt, &sink_fmt);
1309 int v4l2_subdev_link_validate(struct media_link *link)
1311 struct v4l2_subdev *source_sd, *sink_sd;
1312 struct v4l2_subdev_state *source_state, *sink_state;
1316 if (!is_media_entity_v4l2_subdev(link->sink->entity) ||
1317 !is_media_entity_v4l2_subdev(link->source->entity)) {
1318 pr_warn_once("%s of link '%s':%u->'%s':%u is not a V4L2 sub-device, driver bug!\n",
1319 !is_media_entity_v4l2_subdev(link->sink->entity) ?
1321 link->source->entity->name, link->source->index,
1322 link->sink->entity->name, link->sink->index);
1326 sink_sd = media_entity_to_v4l2_subdev(link->sink->entity);
1327 source_sd = media_entity_to_v4l2_subdev(link->source->entity);
1329 sink_state = v4l2_subdev_get_unlocked_active_state(sink_sd);
1330 source_state = v4l2_subdev_get_unlocked_active_state(source_sd);
1332 states_locked = sink_state && source_state;
1334 if (states_locked) {
1335 v4l2_subdev_lock_state(sink_state);
1336 v4l2_subdev_lock_state(source_state);
1339 ret = v4l2_subdev_link_validate_locked(link, states_locked);
1341 if (states_locked) {
1342 v4l2_subdev_unlock_state(sink_state);
1343 v4l2_subdev_unlock_state(source_state);
1348 EXPORT_SYMBOL_GPL(v4l2_subdev_link_validate);
1350 bool v4l2_subdev_has_pad_interdep(struct media_entity *entity,
1351 unsigned int pad0, unsigned int pad1)
1353 struct v4l2_subdev *sd = media_entity_to_v4l2_subdev(entity);
1354 struct v4l2_subdev_krouting *routing;
1355 struct v4l2_subdev_state *state;
1358 state = v4l2_subdev_lock_and_get_active_state(sd);
1360 routing = &state->routing;
1362 for (i = 0; i < routing->num_routes; ++i) {
1363 struct v4l2_subdev_route *route = &routing->routes[i];
1365 if (!(route->flags & V4L2_SUBDEV_ROUTE_FL_ACTIVE))
1368 if ((route->sink_pad == pad0 && route->source_pad == pad1) ||
1369 (route->source_pad == pad0 && route->sink_pad == pad1)) {
1370 v4l2_subdev_unlock_state(state);
1375 v4l2_subdev_unlock_state(state);
1379 EXPORT_SYMBOL_GPL(v4l2_subdev_has_pad_interdep);
1381 struct v4l2_subdev_state *
1382 __v4l2_subdev_state_alloc(struct v4l2_subdev *sd, const char *lock_name,
1383 struct lock_class_key *lock_key)
1385 struct v4l2_subdev_state *state;
1388 state = kzalloc(sizeof(*state), GFP_KERNEL);
1390 return ERR_PTR(-ENOMEM);
1392 __mutex_init(&state->_lock, lock_name, lock_key);
1394 state->lock = sd->state_lock;
1396 state->lock = &state->_lock;
1398 /* Drivers that support streams do not need the legacy pad config */
1399 if (!(sd->flags & V4L2_SUBDEV_FL_STREAMS) && sd->entity.num_pads) {
1400 state->pads = kvcalloc(sd->entity.num_pads,
1401 sizeof(*state->pads), GFP_KERNEL);
1409 * There can be no race at this point, but we lock the state anyway to
1410 * satisfy lockdep checks.
1412 v4l2_subdev_lock_state(state);
1413 ret = v4l2_subdev_call(sd, pad, init_cfg, state);
1414 v4l2_subdev_unlock_state(state);
1416 if (ret < 0 && ret != -ENOIOCTLCMD)
1422 if (state && state->pads)
1423 kvfree(state->pads);
1427 return ERR_PTR(ret);
1429 EXPORT_SYMBOL_GPL(__v4l2_subdev_state_alloc);
1431 void __v4l2_subdev_state_free(struct v4l2_subdev_state *state)
1436 mutex_destroy(&state->_lock);
1438 kfree(state->routing.routes);
1439 kvfree(state->stream_configs.configs);
1440 kvfree(state->pads);
1443 EXPORT_SYMBOL_GPL(__v4l2_subdev_state_free);
1445 int __v4l2_subdev_init_finalize(struct v4l2_subdev *sd, const char *name,
1446 struct lock_class_key *key)
1448 struct v4l2_subdev_state *state;
1450 state = __v4l2_subdev_state_alloc(sd, name, key);
1452 return PTR_ERR(state);
1454 sd->active_state = state;
1458 EXPORT_SYMBOL_GPL(__v4l2_subdev_init_finalize);
1460 void v4l2_subdev_cleanup(struct v4l2_subdev *sd)
1462 struct v4l2_async_subdev_endpoint *ase, *ase_tmp;
1464 __v4l2_subdev_state_free(sd->active_state);
1465 sd->active_state = NULL;
1467 if (list_empty(&sd->async_subdev_endpoint_list))
1470 list_for_each_entry_safe(ase, ase_tmp, &sd->async_subdev_endpoint_list,
1471 async_subdev_endpoint_entry) {
1472 list_del(&ase->async_subdev_endpoint_entry);
1477 EXPORT_SYMBOL_GPL(v4l2_subdev_cleanup);
1479 #if defined(CONFIG_VIDEO_V4L2_SUBDEV_API)
1482 v4l2_subdev_init_stream_configs(struct v4l2_subdev_stream_configs *stream_configs,
1483 const struct v4l2_subdev_krouting *routing)
1485 struct v4l2_subdev_stream_configs new_configs = { 0 };
1486 struct v4l2_subdev_route *route;
1489 /* Count number of formats needed */
1490 for_each_active_route(routing, route) {
1492 * Each route needs a format on both ends of the route.
1494 new_configs.num_configs += 2;
1497 if (new_configs.num_configs) {
1498 new_configs.configs = kvcalloc(new_configs.num_configs,
1499 sizeof(*new_configs.configs),
1502 if (!new_configs.configs)
1507 * Fill in the 'pad' and stream' value for each item in the array from
1512 for_each_active_route(routing, route) {
1513 new_configs.configs[idx].pad = route->sink_pad;
1514 new_configs.configs[idx].stream = route->sink_stream;
1518 new_configs.configs[idx].pad = route->source_pad;
1519 new_configs.configs[idx].stream = route->source_stream;
1524 kvfree(stream_configs->configs);
1525 *stream_configs = new_configs;
1530 int v4l2_subdev_get_fmt(struct v4l2_subdev *sd, struct v4l2_subdev_state *state,
1531 struct v4l2_subdev_format *format)
1533 struct v4l2_mbus_framefmt *fmt;
1535 if (sd->flags & V4L2_SUBDEV_FL_STREAMS)
1536 fmt = v4l2_subdev_state_get_stream_format(state, format->pad,
1538 else if (format->pad < sd->entity.num_pads && format->stream == 0)
1539 fmt = v4l2_subdev_get_pad_format(sd, state, format->pad);
1546 format->format = *fmt;
1550 EXPORT_SYMBOL_GPL(v4l2_subdev_get_fmt);
1552 int v4l2_subdev_set_routing(struct v4l2_subdev *sd,
1553 struct v4l2_subdev_state *state,
1554 const struct v4l2_subdev_krouting *routing)
1556 struct v4l2_subdev_krouting *dst = &state->routing;
1557 const struct v4l2_subdev_krouting *src = routing;
1558 struct v4l2_subdev_krouting new_routing = { 0 };
1562 if (unlikely(check_mul_overflow((size_t)src->num_routes,
1563 sizeof(*src->routes), &bytes)))
1566 lockdep_assert_held(state->lock);
1568 if (src->num_routes > 0) {
1569 new_routing.routes = kmemdup(src->routes, bytes, GFP_KERNEL);
1570 if (!new_routing.routes)
1574 new_routing.num_routes = src->num_routes;
1576 r = v4l2_subdev_init_stream_configs(&state->stream_configs,
1579 kfree(new_routing.routes);
1588 EXPORT_SYMBOL_GPL(v4l2_subdev_set_routing);
1590 struct v4l2_subdev_route *
1591 __v4l2_subdev_next_active_route(const struct v4l2_subdev_krouting *routing,
1592 struct v4l2_subdev_route *route)
1597 route = &routing->routes[0];
1599 for (; route < routing->routes + routing->num_routes; ++route) {
1600 if (!(route->flags & V4L2_SUBDEV_ROUTE_FL_ACTIVE))
1608 EXPORT_SYMBOL_GPL(__v4l2_subdev_next_active_route);
1610 int v4l2_subdev_set_routing_with_fmt(struct v4l2_subdev *sd,
1611 struct v4l2_subdev_state *state,
1612 const struct v4l2_subdev_krouting *routing,
1613 const struct v4l2_mbus_framefmt *fmt)
1615 struct v4l2_subdev_stream_configs *stream_configs;
1619 ret = v4l2_subdev_set_routing(sd, state, routing);
1623 stream_configs = &state->stream_configs;
1625 for (i = 0; i < stream_configs->num_configs; ++i)
1626 stream_configs->configs[i].fmt = *fmt;
1630 EXPORT_SYMBOL_GPL(v4l2_subdev_set_routing_with_fmt);
1632 struct v4l2_mbus_framefmt *
1633 v4l2_subdev_state_get_stream_format(struct v4l2_subdev_state *state,
1634 unsigned int pad, u32 stream)
1636 struct v4l2_subdev_stream_configs *stream_configs;
1639 lockdep_assert_held(state->lock);
1641 stream_configs = &state->stream_configs;
1643 for (i = 0; i < stream_configs->num_configs; ++i) {
1644 if (stream_configs->configs[i].pad == pad &&
1645 stream_configs->configs[i].stream == stream)
1646 return &stream_configs->configs[i].fmt;
1651 EXPORT_SYMBOL_GPL(v4l2_subdev_state_get_stream_format);
1654 v4l2_subdev_state_get_stream_crop(struct v4l2_subdev_state *state,
1655 unsigned int pad, u32 stream)
1657 struct v4l2_subdev_stream_configs *stream_configs;
1660 lockdep_assert_held(state->lock);
1662 stream_configs = &state->stream_configs;
1664 for (i = 0; i < stream_configs->num_configs; ++i) {
1665 if (stream_configs->configs[i].pad == pad &&
1666 stream_configs->configs[i].stream == stream)
1667 return &stream_configs->configs[i].crop;
1672 EXPORT_SYMBOL_GPL(v4l2_subdev_state_get_stream_crop);
1675 v4l2_subdev_state_get_stream_compose(struct v4l2_subdev_state *state,
1676 unsigned int pad, u32 stream)
1678 struct v4l2_subdev_stream_configs *stream_configs;
1681 lockdep_assert_held(state->lock);
1683 stream_configs = &state->stream_configs;
1685 for (i = 0; i < stream_configs->num_configs; ++i) {
1686 if (stream_configs->configs[i].pad == pad &&
1687 stream_configs->configs[i].stream == stream)
1688 return &stream_configs->configs[i].compose;
1693 EXPORT_SYMBOL_GPL(v4l2_subdev_state_get_stream_compose);
1695 int v4l2_subdev_routing_find_opposite_end(const struct v4l2_subdev_krouting *routing,
1696 u32 pad, u32 stream, u32 *other_pad,
1701 for (i = 0; i < routing->num_routes; ++i) {
1702 struct v4l2_subdev_route *route = &routing->routes[i];
1704 if (route->source_pad == pad &&
1705 route->source_stream == stream) {
1707 *other_pad = route->sink_pad;
1709 *other_stream = route->sink_stream;
1713 if (route->sink_pad == pad && route->sink_stream == stream) {
1715 *other_pad = route->source_pad;
1717 *other_stream = route->source_stream;
1724 EXPORT_SYMBOL_GPL(v4l2_subdev_routing_find_opposite_end);
1726 struct v4l2_mbus_framefmt *
1727 v4l2_subdev_state_get_opposite_stream_format(struct v4l2_subdev_state *state,
1728 u32 pad, u32 stream)
1730 u32 other_pad, other_stream;
1733 ret = v4l2_subdev_routing_find_opposite_end(&state->routing,
1735 &other_pad, &other_stream);
1739 return v4l2_subdev_state_get_stream_format(state, other_pad,
1742 EXPORT_SYMBOL_GPL(v4l2_subdev_state_get_opposite_stream_format);
1744 u64 v4l2_subdev_state_xlate_streams(const struct v4l2_subdev_state *state,
1745 u32 pad0, u32 pad1, u64 *streams)
1747 const struct v4l2_subdev_krouting *routing = &state->routing;
1748 struct v4l2_subdev_route *route;
1752 for_each_active_route(routing, route) {
1753 if (route->sink_pad == pad0 && route->source_pad == pad1 &&
1754 (*streams & BIT_ULL(route->sink_stream))) {
1755 streams0 |= BIT_ULL(route->sink_stream);
1756 streams1 |= BIT_ULL(route->source_stream);
1758 if (route->source_pad == pad0 && route->sink_pad == pad1 &&
1759 (*streams & BIT_ULL(route->source_stream))) {
1760 streams0 |= BIT_ULL(route->source_stream);
1761 streams1 |= BIT_ULL(route->sink_stream);
1765 *streams = streams0;
1768 EXPORT_SYMBOL_GPL(v4l2_subdev_state_xlate_streams);
1770 int v4l2_subdev_routing_validate(struct v4l2_subdev *sd,
1771 const struct v4l2_subdev_krouting *routing,
1772 enum v4l2_subdev_routing_restriction disallow)
1774 u32 *remote_pads = NULL;
1778 if (disallow & (V4L2_SUBDEV_ROUTING_NO_STREAM_MIX |
1779 V4L2_SUBDEV_ROUTING_NO_MULTIPLEXING)) {
1780 remote_pads = kcalloc(sd->entity.num_pads, sizeof(*remote_pads),
1785 for (i = 0; i < sd->entity.num_pads; ++i)
1786 remote_pads[i] = U32_MAX;
1789 for (i = 0; i < routing->num_routes; ++i) {
1790 const struct v4l2_subdev_route *route = &routing->routes[i];
1792 /* Validate the sink and source pad numbers. */
1793 if (route->sink_pad >= sd->entity.num_pads ||
1794 !(sd->entity.pads[route->sink_pad].flags & MEDIA_PAD_FL_SINK)) {
1795 dev_dbg(sd->dev, "route %u sink (%u) is not a sink pad\n",
1796 i, route->sink_pad);
1800 if (route->source_pad >= sd->entity.num_pads ||
1801 !(sd->entity.pads[route->source_pad].flags & MEDIA_PAD_FL_SOURCE)) {
1802 dev_dbg(sd->dev, "route %u source (%u) is not a source pad\n",
1803 i, route->source_pad);
1808 * V4L2_SUBDEV_ROUTING_NO_SINK_STREAM_MIX: all streams from a
1809 * sink pad must be routed to a single source pad.
1811 if (disallow & V4L2_SUBDEV_ROUTING_NO_SINK_STREAM_MIX) {
1812 if (remote_pads[route->sink_pad] != U32_MAX &&
1813 remote_pads[route->sink_pad] != route->source_pad) {
1815 "route %u attempts to mix %s streams\n",
1822 * V4L2_SUBDEV_ROUTING_NO_SOURCE_STREAM_MIX: all streams on a
1823 * source pad must originate from a single sink pad.
1825 if (disallow & V4L2_SUBDEV_ROUTING_NO_SOURCE_STREAM_MIX) {
1826 if (remote_pads[route->source_pad] != U32_MAX &&
1827 remote_pads[route->source_pad] != route->sink_pad) {
1829 "route %u attempts to mix %s streams\n",
1836 * V4L2_SUBDEV_ROUTING_NO_SINK_MULTIPLEXING: Pads on the sink
1837 * side can not do stream multiplexing, i.e. there can be only
1838 * a single stream in a sink pad.
1840 if (disallow & V4L2_SUBDEV_ROUTING_NO_SINK_MULTIPLEXING) {
1841 if (remote_pads[route->sink_pad] != U32_MAX) {
1843 "route %u attempts to multiplex on %s pad %u\n",
1844 i, "sink", route->sink_pad);
1850 * V4L2_SUBDEV_ROUTING_NO_SOURCE_MULTIPLEXING: Pads on the
1851 * source side can not do stream multiplexing, i.e. there can
1852 * be only a single stream in a source pad.
1854 if (disallow & V4L2_SUBDEV_ROUTING_NO_SOURCE_MULTIPLEXING) {
1855 if (remote_pads[route->source_pad] != U32_MAX) {
1857 "route %u attempts to multiplex on %s pad %u\n",
1858 i, "source", route->source_pad);
1864 remote_pads[route->sink_pad] = route->source_pad;
1865 remote_pads[route->source_pad] = route->sink_pad;
1868 for (j = i + 1; j < routing->num_routes; ++j) {
1869 const struct v4l2_subdev_route *r = &routing->routes[j];
1872 * V4L2_SUBDEV_ROUTING_NO_1_TO_N: No two routes can
1873 * originate from the same (sink) stream.
1875 if ((disallow & V4L2_SUBDEV_ROUTING_NO_1_TO_N) &&
1876 route->sink_pad == r->sink_pad &&
1877 route->sink_stream == r->sink_stream) {
1879 "routes %u and %u originate from same sink (%u/%u)\n",
1880 i, j, route->sink_pad,
1881 route->sink_stream);
1886 * V4L2_SUBDEV_ROUTING_NO_N_TO_1: No two routes can end
1887 * at the same (source) stream.
1889 if ((disallow & V4L2_SUBDEV_ROUTING_NO_N_TO_1) &&
1890 route->source_pad == r->source_pad &&
1891 route->source_stream == r->source_stream) {
1893 "routes %u and %u end at same source (%u/%u)\n",
1894 i, j, route->source_pad,
1895 route->source_stream);
1907 EXPORT_SYMBOL_GPL(v4l2_subdev_routing_validate);
1909 static int v4l2_subdev_enable_streams_fallback(struct v4l2_subdev *sd, u32 pad,
1912 struct device *dev = sd->entity.graph_obj.mdev->dev;
1917 * The subdev doesn't implement pad-based stream enable, fall back
1918 * on the .s_stream() operation. This can only be done for subdevs that
1919 * have a single source pad, as sd->enabled_streams is global to the
1922 if (!(sd->entity.pads[pad].flags & MEDIA_PAD_FL_SOURCE))
1925 for (i = 0; i < sd->entity.num_pads; ++i) {
1926 if (i != pad && sd->entity.pads[i].flags & MEDIA_PAD_FL_SOURCE)
1930 if (sd->enabled_streams & streams_mask) {
1931 dev_dbg(dev, "set of streams %#llx already enabled on %s:%u\n",
1932 streams_mask, sd->entity.name, pad);
1936 /* Start streaming when the first streams are enabled. */
1937 if (!sd->enabled_streams) {
1938 ret = v4l2_subdev_call(sd, video, s_stream, 1);
1943 sd->enabled_streams |= streams_mask;
1948 int v4l2_subdev_enable_streams(struct v4l2_subdev *sd, u32 pad,
1951 struct device *dev = sd->entity.graph_obj.mdev->dev;
1952 struct v4l2_subdev_state *state;
1953 u64 found_streams = 0;
1957 /* A few basic sanity checks first. */
1958 if (pad >= sd->entity.num_pads)
1964 /* Fallback on .s_stream() if .enable_streams() isn't available. */
1965 if (!sd->ops->pad || !sd->ops->pad->enable_streams)
1966 return v4l2_subdev_enable_streams_fallback(sd, pad,
1969 state = v4l2_subdev_lock_and_get_active_state(sd);
1972 * Verify that the requested streams exist and that they are not
1975 for (i = 0; i < state->stream_configs.num_configs; ++i) {
1976 struct v4l2_subdev_stream_config *cfg =
1977 &state->stream_configs.configs[i];
1979 if (cfg->pad != pad || !(streams_mask & BIT_ULL(cfg->stream)))
1982 found_streams |= BIT_ULL(cfg->stream);
1985 dev_dbg(dev, "stream %u already enabled on %s:%u\n",
1986 cfg->stream, sd->entity.name, pad);
1992 if (found_streams != streams_mask) {
1993 dev_dbg(dev, "streams 0x%llx not found on %s:%u\n",
1994 streams_mask & ~found_streams, sd->entity.name, pad);
1999 dev_dbg(dev, "enable streams %u:%#llx\n", pad, streams_mask);
2001 /* Call the .enable_streams() operation. */
2002 ret = v4l2_subdev_call(sd, pad, enable_streams, state, pad,
2005 dev_dbg(dev, "enable streams %u:%#llx failed: %d\n", pad,
2010 /* Mark the streams as enabled. */
2011 for (i = 0; i < state->stream_configs.num_configs; ++i) {
2012 struct v4l2_subdev_stream_config *cfg =
2013 &state->stream_configs.configs[i];
2015 if (cfg->pad == pad && (streams_mask & BIT_ULL(cfg->stream)))
2016 cfg->enabled = true;
2020 v4l2_subdev_unlock_state(state);
2024 EXPORT_SYMBOL_GPL(v4l2_subdev_enable_streams);
2026 static int v4l2_subdev_disable_streams_fallback(struct v4l2_subdev *sd, u32 pad,
2029 struct device *dev = sd->entity.graph_obj.mdev->dev;
2034 * If the subdev doesn't implement pad-based stream enable, fall back
2035 * on the .s_stream() operation. This can only be done for subdevs that
2036 * have a single source pad, as sd->enabled_streams is global to the
2039 if (!(sd->entity.pads[pad].flags & MEDIA_PAD_FL_SOURCE))
2042 for (i = 0; i < sd->entity.num_pads; ++i) {
2043 if (i != pad && sd->entity.pads[i].flags & MEDIA_PAD_FL_SOURCE)
2047 if ((sd->enabled_streams & streams_mask) != streams_mask) {
2048 dev_dbg(dev, "set of streams %#llx already disabled on %s:%u\n",
2049 streams_mask, sd->entity.name, pad);
2053 /* Stop streaming when the last streams are disabled. */
2054 if (!(sd->enabled_streams & ~streams_mask)) {
2055 ret = v4l2_subdev_call(sd, video, s_stream, 0);
2060 sd->enabled_streams &= ~streams_mask;
2065 int v4l2_subdev_disable_streams(struct v4l2_subdev *sd, u32 pad,
2068 struct device *dev = sd->entity.graph_obj.mdev->dev;
2069 struct v4l2_subdev_state *state;
2070 u64 found_streams = 0;
2074 /* A few basic sanity checks first. */
2075 if (pad >= sd->entity.num_pads)
2081 /* Fallback on .s_stream() if .disable_streams() isn't available. */
2082 if (!sd->ops->pad || !sd->ops->pad->disable_streams)
2083 return v4l2_subdev_disable_streams_fallback(sd, pad,
2086 state = v4l2_subdev_lock_and_get_active_state(sd);
2089 * Verify that the requested streams exist and that they are not
2092 for (i = 0; i < state->stream_configs.num_configs; ++i) {
2093 struct v4l2_subdev_stream_config *cfg =
2094 &state->stream_configs.configs[i];
2096 if (cfg->pad != pad || !(streams_mask & BIT_ULL(cfg->stream)))
2099 found_streams |= BIT_ULL(cfg->stream);
2101 if (!cfg->enabled) {
2102 dev_dbg(dev, "stream %u already disabled on %s:%u\n",
2103 cfg->stream, sd->entity.name, pad);
2109 if (found_streams != streams_mask) {
2110 dev_dbg(dev, "streams 0x%llx not found on %s:%u\n",
2111 streams_mask & ~found_streams, sd->entity.name, pad);
2116 dev_dbg(dev, "disable streams %u:%#llx\n", pad, streams_mask);
2118 /* Call the .disable_streams() operation. */
2119 ret = v4l2_subdev_call(sd, pad, disable_streams, state, pad,
2122 dev_dbg(dev, "disable streams %u:%#llx failed: %d\n", pad,
2127 /* Mark the streams as disabled. */
2128 for (i = 0; i < state->stream_configs.num_configs; ++i) {
2129 struct v4l2_subdev_stream_config *cfg =
2130 &state->stream_configs.configs[i];
2132 if (cfg->pad == pad && (streams_mask & BIT_ULL(cfg->stream)))
2133 cfg->enabled = false;
2137 v4l2_subdev_unlock_state(state);
2141 EXPORT_SYMBOL_GPL(v4l2_subdev_disable_streams);
2143 int v4l2_subdev_s_stream_helper(struct v4l2_subdev *sd, int enable)
2145 struct v4l2_subdev_state *state;
2146 struct v4l2_subdev_route *route;
2147 struct media_pad *pad;
2148 u64 source_mask = 0;
2152 * Find the source pad. This helper is meant for subdevs that have a
2153 * single source pad, so failures shouldn't happen, but catch them
2154 * loudly nonetheless as they indicate a driver bug.
2156 media_entity_for_each_pad(&sd->entity, pad) {
2157 if (pad->flags & MEDIA_PAD_FL_SOURCE) {
2158 pad_index = pad->index;
2163 if (WARN_ON(pad_index == -1))
2167 * As there's a single source pad, just collect all the source streams.
2169 state = v4l2_subdev_lock_and_get_active_state(sd);
2171 for_each_active_route(&state->routing, route)
2172 source_mask |= BIT_ULL(route->source_stream);
2174 v4l2_subdev_unlock_state(state);
2177 return v4l2_subdev_enable_streams(sd, pad_index, source_mask);
2179 return v4l2_subdev_disable_streams(sd, pad_index, source_mask);
2181 EXPORT_SYMBOL_GPL(v4l2_subdev_s_stream_helper);
2183 #endif /* CONFIG_VIDEO_V4L2_SUBDEV_API */
2185 #endif /* CONFIG_MEDIA_CONTROLLER */
2187 void v4l2_subdev_init(struct v4l2_subdev *sd, const struct v4l2_subdev_ops *ops)
2189 INIT_LIST_HEAD(&sd->list);
2192 sd->v4l2_dev = NULL;
2196 sd->dev_priv = NULL;
2197 sd->host_priv = NULL;
2198 sd->privacy_led = NULL;
2199 INIT_LIST_HEAD(&sd->async_subdev_endpoint_list);
2200 #if defined(CONFIG_MEDIA_CONTROLLER)
2201 sd->entity.name = sd->name;
2202 sd->entity.obj_type = MEDIA_ENTITY_TYPE_V4L2_SUBDEV;
2203 sd->entity.function = MEDIA_ENT_F_V4L2_SUBDEV_UNKNOWN;
2206 EXPORT_SYMBOL(v4l2_subdev_init);
2208 void v4l2_subdev_notify_event(struct v4l2_subdev *sd,
2209 const struct v4l2_event *ev)
2211 v4l2_event_queue(sd->devnode, ev);
2212 v4l2_subdev_notify(sd, V4L2_DEVICE_NOTIFY_EVENT, (void *)ev);
2214 EXPORT_SYMBOL_GPL(v4l2_subdev_notify_event);
2216 int v4l2_subdev_get_privacy_led(struct v4l2_subdev *sd)
2218 #if IS_REACHABLE(CONFIG_LEDS_CLASS)
2219 sd->privacy_led = led_get(sd->dev, "privacy-led");
2220 if (IS_ERR(sd->privacy_led) && PTR_ERR(sd->privacy_led) != -ENOENT)
2221 return dev_err_probe(sd->dev, PTR_ERR(sd->privacy_led),
2222 "getting privacy LED\n");
2224 if (!IS_ERR_OR_NULL(sd->privacy_led)) {
2225 mutex_lock(&sd->privacy_led->led_access);
2226 led_sysfs_disable(sd->privacy_led);
2227 led_trigger_remove(sd->privacy_led);
2228 led_set_brightness(sd->privacy_led, 0);
2229 mutex_unlock(&sd->privacy_led->led_access);
2234 EXPORT_SYMBOL_GPL(v4l2_subdev_get_privacy_led);
2236 void v4l2_subdev_put_privacy_led(struct v4l2_subdev *sd)
2238 #if IS_REACHABLE(CONFIG_LEDS_CLASS)
2239 if (!IS_ERR_OR_NULL(sd->privacy_led)) {
2240 mutex_lock(&sd->privacy_led->led_access);
2241 led_sysfs_enable(sd->privacy_led);
2242 mutex_unlock(&sd->privacy_led->led_access);
2243 led_put(sd->privacy_led);
2247 EXPORT_SYMBOL_GPL(v4l2_subdev_put_privacy_led);