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/string.h>
19 #include <linux/types.h>
20 #include <linux/version.h>
21 #include <linux/videodev2.h>
23 #include <media/v4l2-ctrls.h>
24 #include <media/v4l2-device.h>
25 #include <media/v4l2-event.h>
26 #include <media/v4l2-fh.h>
27 #include <media/v4l2-ioctl.h>
29 #if defined(CONFIG_VIDEO_V4L2_SUBDEV_API)
31 * The Streams API is an experimental feature. To use the Streams API, set
32 * 'v4l2_subdev_enable_streams_api' to 1 below.
35 static bool v4l2_subdev_enable_streams_api
;
39 * Maximum stream ID is 63 for now, as we use u64 bitmask to represent a set
42 * Note that V4L2_FRAME_DESC_ENTRY_MAX is related: V4L2_FRAME_DESC_ENTRY_MAX
43 * restricts the total number of streams in a pad, although the stream ID is
46 #define V4L2_SUBDEV_MAX_STREAM_ID 63
48 #include "v4l2-subdev-priv.h"
50 #if defined(CONFIG_VIDEO_V4L2_SUBDEV_API)
51 static int subdev_fh_init(struct v4l2_subdev_fh
*fh
, struct v4l2_subdev
*sd
)
53 struct v4l2_subdev_state
*state
;
54 static struct lock_class_key key
;
56 state
= __v4l2_subdev_state_alloc(sd
, "fh->state->lock", &key
);
58 return PTR_ERR(state
);
65 static void subdev_fh_free(struct v4l2_subdev_fh
*fh
)
67 __v4l2_subdev_state_free(fh
->state
);
71 static int subdev_open(struct file
*file
)
73 struct video_device
*vdev
= video_devdata(file
);
74 struct v4l2_subdev
*sd
= vdev_to_v4l2_subdev(vdev
);
75 struct v4l2_subdev_fh
*subdev_fh
;
78 subdev_fh
= kzalloc(sizeof(*subdev_fh
), GFP_KERNEL
);
79 if (subdev_fh
== NULL
)
82 ret
= subdev_fh_init(subdev_fh
, sd
);
88 v4l2_fh_init(&subdev_fh
->vfh
, vdev
);
89 v4l2_fh_add(&subdev_fh
->vfh
);
90 file
->private_data
= &subdev_fh
->vfh
;
92 if (sd
->v4l2_dev
->mdev
&& sd
->entity
.graph_obj
.mdev
->dev
) {
95 owner
= sd
->entity
.graph_obj
.mdev
->dev
->driver
->owner
;
96 if (!try_module_get(owner
)) {
100 subdev_fh
->owner
= owner
;
103 if (sd
->internal_ops
&& sd
->internal_ops
->open
) {
104 ret
= sd
->internal_ops
->open(sd
, subdev_fh
);
112 module_put(subdev_fh
->owner
);
113 v4l2_fh_del(&subdev_fh
->vfh
);
114 v4l2_fh_exit(&subdev_fh
->vfh
);
115 subdev_fh_free(subdev_fh
);
121 static int subdev_close(struct file
*file
)
123 struct video_device
*vdev
= video_devdata(file
);
124 struct v4l2_subdev
*sd
= vdev_to_v4l2_subdev(vdev
);
125 struct v4l2_fh
*vfh
= file
->private_data
;
126 struct v4l2_subdev_fh
*subdev_fh
= to_v4l2_subdev_fh(vfh
);
128 if (sd
->internal_ops
&& sd
->internal_ops
->close
)
129 sd
->internal_ops
->close(sd
, subdev_fh
);
130 module_put(subdev_fh
->owner
);
133 subdev_fh_free(subdev_fh
);
135 file
->private_data
= NULL
;
139 #else /* CONFIG_VIDEO_V4L2_SUBDEV_API */
140 static int subdev_open(struct file
*file
)
145 static int subdev_close(struct file
*file
)
149 #endif /* CONFIG_VIDEO_V4L2_SUBDEV_API */
151 static void v4l2_subdev_enable_privacy_led(struct v4l2_subdev
*sd
)
153 #if IS_REACHABLE(CONFIG_LEDS_CLASS)
154 if (!IS_ERR_OR_NULL(sd
->privacy_led
))
155 led_set_brightness(sd
->privacy_led
,
156 sd
->privacy_led
->max_brightness
);
160 static void v4l2_subdev_disable_privacy_led(struct v4l2_subdev
*sd
)
162 #if IS_REACHABLE(CONFIG_LEDS_CLASS)
163 if (!IS_ERR_OR_NULL(sd
->privacy_led
))
164 led_set_brightness(sd
->privacy_led
, 0);
168 static inline int check_which(u32 which
)
170 if (which
!= V4L2_SUBDEV_FORMAT_TRY
&&
171 which
!= V4L2_SUBDEV_FORMAT_ACTIVE
)
177 static inline int check_pad(struct v4l2_subdev
*sd
, u32 pad
)
179 #if defined(CONFIG_MEDIA_CONTROLLER)
180 if (sd
->entity
.num_pads
) {
181 if (pad
>= sd
->entity
.num_pads
)
186 /* allow pad 0 on subdevices not registered as media entities */
192 static int check_state(struct v4l2_subdev
*sd
, struct v4l2_subdev_state
*state
,
193 u32 which
, u32 pad
, u32 stream
)
195 if (sd
->flags
& V4L2_SUBDEV_FL_STREAMS
) {
196 #if defined(CONFIG_VIDEO_V4L2_SUBDEV_API)
197 if (!v4l2_subdev_state_get_format(state
, pad
, stream
))
208 if (which
== V4L2_SUBDEV_FORMAT_TRY
&& (!state
|| !state
->pads
))
214 static inline int check_format(struct v4l2_subdev
*sd
,
215 struct v4l2_subdev_state
*state
,
216 struct v4l2_subdev_format
*format
)
221 return check_which(format
->which
) ? : check_pad(sd
, format
->pad
) ? :
222 check_state(sd
, state
, format
->which
, format
->pad
, format
->stream
);
225 static int call_get_fmt(struct v4l2_subdev
*sd
,
226 struct v4l2_subdev_state
*state
,
227 struct v4l2_subdev_format
*format
)
229 return check_format(sd
, state
, format
) ? :
230 sd
->ops
->pad
->get_fmt(sd
, state
, format
);
233 static int call_set_fmt(struct v4l2_subdev
*sd
,
234 struct v4l2_subdev_state
*state
,
235 struct v4l2_subdev_format
*format
)
237 return check_format(sd
, state
, format
) ? :
238 sd
->ops
->pad
->set_fmt(sd
, state
, format
);
241 static int call_enum_mbus_code(struct v4l2_subdev
*sd
,
242 struct v4l2_subdev_state
*state
,
243 struct v4l2_subdev_mbus_code_enum
*code
)
248 return check_which(code
->which
) ? : check_pad(sd
, code
->pad
) ? :
249 check_state(sd
, state
, code
->which
, code
->pad
, code
->stream
) ? :
250 sd
->ops
->pad
->enum_mbus_code(sd
, state
, code
);
253 static int call_enum_frame_size(struct v4l2_subdev
*sd
,
254 struct v4l2_subdev_state
*state
,
255 struct v4l2_subdev_frame_size_enum
*fse
)
260 return check_which(fse
->which
) ? : check_pad(sd
, fse
->pad
) ? :
261 check_state(sd
, state
, fse
->which
, fse
->pad
, fse
->stream
) ? :
262 sd
->ops
->pad
->enum_frame_size(sd
, state
, fse
);
265 static int call_enum_frame_interval(struct v4l2_subdev
*sd
,
266 struct v4l2_subdev_state
*state
,
267 struct v4l2_subdev_frame_interval_enum
*fie
)
272 return check_which(fie
->which
) ? : check_pad(sd
, fie
->pad
) ? :
273 check_state(sd
, state
, fie
->which
, fie
->pad
, fie
->stream
) ? :
274 sd
->ops
->pad
->enum_frame_interval(sd
, state
, fie
);
277 static inline int check_selection(struct v4l2_subdev
*sd
,
278 struct v4l2_subdev_state
*state
,
279 struct v4l2_subdev_selection
*sel
)
284 return check_which(sel
->which
) ? : check_pad(sd
, sel
->pad
) ? :
285 check_state(sd
, state
, sel
->which
, sel
->pad
, sel
->stream
);
288 static int call_get_selection(struct v4l2_subdev
*sd
,
289 struct v4l2_subdev_state
*state
,
290 struct v4l2_subdev_selection
*sel
)
292 return check_selection(sd
, state
, sel
) ? :
293 sd
->ops
->pad
->get_selection(sd
, state
, sel
);
296 static int call_set_selection(struct v4l2_subdev
*sd
,
297 struct v4l2_subdev_state
*state
,
298 struct v4l2_subdev_selection
*sel
)
300 return check_selection(sd
, state
, sel
) ? :
301 sd
->ops
->pad
->set_selection(sd
, state
, sel
);
304 static inline int check_frame_interval(struct v4l2_subdev
*sd
,
305 struct v4l2_subdev_state
*state
,
306 struct v4l2_subdev_frame_interval
*fi
)
311 return check_which(fi
->which
) ? : check_pad(sd
, fi
->pad
) ? :
312 check_state(sd
, state
, fi
->which
, fi
->pad
, fi
->stream
);
315 static int call_get_frame_interval(struct v4l2_subdev
*sd
,
316 struct v4l2_subdev_state
*state
,
317 struct v4l2_subdev_frame_interval
*fi
)
319 return check_frame_interval(sd
, state
, fi
) ? :
320 sd
->ops
->pad
->get_frame_interval(sd
, state
, fi
);
323 static int call_set_frame_interval(struct v4l2_subdev
*sd
,
324 struct v4l2_subdev_state
*state
,
325 struct v4l2_subdev_frame_interval
*fi
)
327 return check_frame_interval(sd
, state
, fi
) ? :
328 sd
->ops
->pad
->set_frame_interval(sd
, state
, fi
);
331 static int call_get_frame_desc(struct v4l2_subdev
*sd
, unsigned int pad
,
332 struct v4l2_mbus_frame_desc
*fd
)
337 #if defined(CONFIG_MEDIA_CONTROLLER)
338 if (!(sd
->entity
.pads
[pad
].flags
& MEDIA_PAD_FL_SOURCE
))
342 memset(fd
, 0, sizeof(*fd
));
344 ret
= sd
->ops
->pad
->get_frame_desc(sd
, pad
, fd
);
348 dev_dbg(sd
->dev
, "Frame descriptor on pad %u, type %s\n", pad
,
349 fd
->type
== V4L2_MBUS_FRAME_DESC_TYPE_PARALLEL
? "parallel" :
350 fd
->type
== V4L2_MBUS_FRAME_DESC_TYPE_CSI2
? "CSI-2" :
353 for (i
= 0; i
< fd
->num_entries
; i
++) {
354 struct v4l2_mbus_frame_desc_entry
*entry
= &fd
->entry
[i
];
357 if (fd
->type
== V4L2_MBUS_FRAME_DESC_TYPE_CSI2
)
358 WARN_ON(snprintf(buf
, sizeof(buf
),
359 ", vc %u, dt 0x%02x",
361 entry
->bus
.csi2
.dt
) >= sizeof(buf
));
364 "\tstream %u, code 0x%04x, length %u, flags 0x%04x%s\n",
365 entry
->stream
, entry
->pixelcode
, entry
->length
,
372 static inline int check_edid(struct v4l2_subdev
*sd
,
373 struct v4l2_subdev_edid
*edid
)
378 if (edid
->blocks
&& edid
->edid
== NULL
)
381 return check_pad(sd
, edid
->pad
);
384 static int call_get_edid(struct v4l2_subdev
*sd
, struct v4l2_subdev_edid
*edid
)
386 return check_edid(sd
, edid
) ? : sd
->ops
->pad
->get_edid(sd
, edid
);
389 static int call_set_edid(struct v4l2_subdev
*sd
, struct v4l2_subdev_edid
*edid
)
391 return check_edid(sd
, edid
) ? : sd
->ops
->pad
->set_edid(sd
, edid
);
394 static int call_s_dv_timings(struct v4l2_subdev
*sd
, unsigned int pad
,
395 struct v4l2_dv_timings
*timings
)
400 return check_pad(sd
, pad
) ? :
401 sd
->ops
->pad
->s_dv_timings(sd
, pad
, timings
);
404 static int call_g_dv_timings(struct v4l2_subdev
*sd
, unsigned int pad
,
405 struct v4l2_dv_timings
*timings
)
410 return check_pad(sd
, pad
) ? :
411 sd
->ops
->pad
->g_dv_timings(sd
, pad
, timings
);
414 static int call_query_dv_timings(struct v4l2_subdev
*sd
, unsigned int pad
,
415 struct v4l2_dv_timings
*timings
)
420 return check_pad(sd
, pad
) ? :
421 sd
->ops
->pad
->query_dv_timings(sd
, pad
, timings
);
424 static int call_dv_timings_cap(struct v4l2_subdev
*sd
,
425 struct v4l2_dv_timings_cap
*cap
)
430 return check_pad(sd
, cap
->pad
) ? :
431 sd
->ops
->pad
->dv_timings_cap(sd
, cap
);
434 static int call_enum_dv_timings(struct v4l2_subdev
*sd
,
435 struct v4l2_enum_dv_timings
*dvt
)
440 return check_pad(sd
, dvt
->pad
) ? :
441 sd
->ops
->pad
->enum_dv_timings(sd
, dvt
);
444 static int call_get_mbus_config(struct v4l2_subdev
*sd
, unsigned int pad
,
445 struct v4l2_mbus_config
*config
)
447 return check_pad(sd
, pad
) ? :
448 sd
->ops
->pad
->get_mbus_config(sd
, pad
, config
);
451 static int call_s_stream(struct v4l2_subdev
*sd
, int enable
)
456 * The .s_stream() operation must never be called to start or stop an
457 * already started or stopped subdev. Catch offenders but don't return
458 * an error yet to avoid regressions.
460 if (WARN_ON(sd
->s_stream_enabled
== !!enable
))
463 ret
= sd
->ops
->video
->s_stream(sd
, enable
);
465 if (!enable
&& ret
< 0) {
466 dev_warn(sd
->dev
, "disabling streaming failed (%d)\n", ret
);
471 sd
->s_stream_enabled
= enable
;
474 v4l2_subdev_enable_privacy_led(sd
);
476 v4l2_subdev_disable_privacy_led(sd
);
482 #ifdef CONFIG_MEDIA_CONTROLLER
484 * Create state-management wrapper for pad ops dealing with subdev state. The
485 * wrapper handles the case where the caller does not provide the called
486 * subdev's state. This should be removed when all the callers are fixed.
488 #define DEFINE_STATE_WRAPPER(f, arg_type) \
489 static int call_##f##_state(struct v4l2_subdev *sd, \
490 struct v4l2_subdev_state *_state, \
493 struct v4l2_subdev_state *state = _state; \
496 state = v4l2_subdev_lock_and_get_active_state(sd); \
497 ret = call_##f(sd, state, arg); \
498 if (!_state && state) \
499 v4l2_subdev_unlock_state(state); \
503 #else /* CONFIG_MEDIA_CONTROLLER */
505 #define DEFINE_STATE_WRAPPER(f, arg_type) \
506 static int call_##f##_state(struct v4l2_subdev *sd, \
507 struct v4l2_subdev_state *state, \
510 return call_##f(sd, state, arg); \
513 #endif /* CONFIG_MEDIA_CONTROLLER */
515 DEFINE_STATE_WRAPPER(get_fmt
, struct v4l2_subdev_format
);
516 DEFINE_STATE_WRAPPER(set_fmt
, struct v4l2_subdev_format
);
517 DEFINE_STATE_WRAPPER(enum_mbus_code
, struct v4l2_subdev_mbus_code_enum
);
518 DEFINE_STATE_WRAPPER(enum_frame_size
, struct v4l2_subdev_frame_size_enum
);
519 DEFINE_STATE_WRAPPER(enum_frame_interval
, struct v4l2_subdev_frame_interval_enum
);
520 DEFINE_STATE_WRAPPER(get_selection
, struct v4l2_subdev_selection
);
521 DEFINE_STATE_WRAPPER(set_selection
, struct v4l2_subdev_selection
);
523 static const struct v4l2_subdev_pad_ops v4l2_subdev_call_pad_wrappers
= {
524 .get_fmt
= call_get_fmt_state
,
525 .set_fmt
= call_set_fmt_state
,
526 .enum_mbus_code
= call_enum_mbus_code_state
,
527 .enum_frame_size
= call_enum_frame_size_state
,
528 .enum_frame_interval
= call_enum_frame_interval_state
,
529 .get_selection
= call_get_selection_state
,
530 .set_selection
= call_set_selection_state
,
531 .get_frame_interval
= call_get_frame_interval
,
532 .set_frame_interval
= call_set_frame_interval
,
533 .get_edid
= call_get_edid
,
534 .set_edid
= call_set_edid
,
535 .s_dv_timings
= call_s_dv_timings
,
536 .g_dv_timings
= call_g_dv_timings
,
537 .query_dv_timings
= call_query_dv_timings
,
538 .dv_timings_cap
= call_dv_timings_cap
,
539 .enum_dv_timings
= call_enum_dv_timings
,
540 .get_frame_desc
= call_get_frame_desc
,
541 .get_mbus_config
= call_get_mbus_config
,
544 static const struct v4l2_subdev_video_ops v4l2_subdev_call_video_wrappers
= {
545 .s_stream
= call_s_stream
,
548 const struct v4l2_subdev_ops v4l2_subdev_call_wrappers
= {
549 .pad
= &v4l2_subdev_call_pad_wrappers
,
550 .video
= &v4l2_subdev_call_video_wrappers
,
552 EXPORT_SYMBOL(v4l2_subdev_call_wrappers
);
554 #if defined(CONFIG_VIDEO_V4L2_SUBDEV_API)
556 static struct v4l2_subdev_state
*
557 subdev_ioctl_get_state(struct v4l2_subdev
*sd
, struct v4l2_subdev_fh
*subdev_fh
,
558 unsigned int cmd
, void *arg
)
565 case VIDIOC_SUBDEV_G_FMT
:
566 case VIDIOC_SUBDEV_S_FMT
:
567 which
= ((struct v4l2_subdev_format
*)arg
)->which
;
569 case VIDIOC_SUBDEV_G_CROP
:
570 case VIDIOC_SUBDEV_S_CROP
:
571 which
= ((struct v4l2_subdev_crop
*)arg
)->which
;
573 case VIDIOC_SUBDEV_ENUM_MBUS_CODE
:
574 which
= ((struct v4l2_subdev_mbus_code_enum
*)arg
)->which
;
576 case VIDIOC_SUBDEV_ENUM_FRAME_SIZE
:
577 which
= ((struct v4l2_subdev_frame_size_enum
*)arg
)->which
;
579 case VIDIOC_SUBDEV_ENUM_FRAME_INTERVAL
:
580 which
= ((struct v4l2_subdev_frame_interval_enum
*)arg
)->which
;
582 case VIDIOC_SUBDEV_G_SELECTION
:
583 case VIDIOC_SUBDEV_S_SELECTION
:
584 which
= ((struct v4l2_subdev_selection
*)arg
)->which
;
586 case VIDIOC_SUBDEV_G_FRAME_INTERVAL
:
587 case VIDIOC_SUBDEV_S_FRAME_INTERVAL
: {
588 struct v4l2_subdev_frame_interval
*fi
= arg
;
590 if (!(subdev_fh
->client_caps
&
591 V4L2_SUBDEV_CLIENT_CAP_INTERVAL_USES_WHICH
))
592 fi
->which
= V4L2_SUBDEV_FORMAT_ACTIVE
;
597 case VIDIOC_SUBDEV_G_ROUTING
:
598 case VIDIOC_SUBDEV_S_ROUTING
:
599 which
= ((struct v4l2_subdev_routing
*)arg
)->which
;
603 return which
== V4L2_SUBDEV_FORMAT_TRY
?
605 v4l2_subdev_get_unlocked_active_state(sd
);
608 static long subdev_do_ioctl(struct file
*file
, unsigned int cmd
, void *arg
,
609 struct v4l2_subdev_state
*state
)
611 struct video_device
*vdev
= video_devdata(file
);
612 struct v4l2_subdev
*sd
= vdev_to_v4l2_subdev(vdev
);
613 struct v4l2_fh
*vfh
= file
->private_data
;
614 struct v4l2_subdev_fh
*subdev_fh
= to_v4l2_subdev_fh(vfh
);
615 bool ro_subdev
= test_bit(V4L2_FL_SUBDEV_RO_DEVNODE
, &vdev
->flags
);
616 bool streams_subdev
= sd
->flags
& V4L2_SUBDEV_FL_STREAMS
;
617 bool client_supports_streams
= subdev_fh
->client_caps
&
618 V4L2_SUBDEV_CLIENT_CAP_STREAMS
;
622 * If the streams API is not enabled, remove V4L2_SUBDEV_CAP_STREAMS.
623 * Remove this when the API is no longer experimental.
625 if (!v4l2_subdev_enable_streams_api
)
626 streams_subdev
= false;
629 case VIDIOC_SUBDEV_QUERYCAP
: {
630 struct v4l2_subdev_capability
*cap
= arg
;
632 memset(cap
->reserved
, 0, sizeof(cap
->reserved
));
633 cap
->version
= LINUX_VERSION_CODE
;
635 (ro_subdev
? V4L2_SUBDEV_CAP_RO_SUBDEV
: 0) |
636 (streams_subdev
? V4L2_SUBDEV_CAP_STREAMS
: 0);
641 case VIDIOC_QUERYCTRL
:
643 * TODO: this really should be folded into v4l2_queryctrl (this
644 * currently returns -EINVAL for NULL control handlers).
645 * However, v4l2_queryctrl() is still called directly by
646 * drivers as well and until that has been addressed I believe
647 * it is safer to do the check here. The same is true for the
648 * other control ioctls below.
650 if (!vfh
->ctrl_handler
)
652 return v4l2_queryctrl(vfh
->ctrl_handler
, arg
);
654 case VIDIOC_QUERY_EXT_CTRL
:
655 if (!vfh
->ctrl_handler
)
657 return v4l2_query_ext_ctrl(vfh
->ctrl_handler
, arg
);
659 case VIDIOC_QUERYMENU
:
660 if (!vfh
->ctrl_handler
)
662 return v4l2_querymenu(vfh
->ctrl_handler
, arg
);
665 if (!vfh
->ctrl_handler
)
667 return v4l2_g_ctrl(vfh
->ctrl_handler
, arg
);
670 if (!vfh
->ctrl_handler
)
672 return v4l2_s_ctrl(vfh
, vfh
->ctrl_handler
, arg
);
674 case VIDIOC_G_EXT_CTRLS
:
675 if (!vfh
->ctrl_handler
)
677 return v4l2_g_ext_ctrls(vfh
->ctrl_handler
,
678 vdev
, sd
->v4l2_dev
->mdev
, arg
);
680 case VIDIOC_S_EXT_CTRLS
:
681 if (!vfh
->ctrl_handler
)
683 return v4l2_s_ext_ctrls(vfh
, vfh
->ctrl_handler
,
684 vdev
, sd
->v4l2_dev
->mdev
, arg
);
686 case VIDIOC_TRY_EXT_CTRLS
:
687 if (!vfh
->ctrl_handler
)
689 return v4l2_try_ext_ctrls(vfh
->ctrl_handler
,
690 vdev
, sd
->v4l2_dev
->mdev
, arg
);
693 if (!(sd
->flags
& V4L2_SUBDEV_FL_HAS_EVENTS
))
696 return v4l2_event_dequeue(vfh
, arg
, file
->f_flags
& O_NONBLOCK
);
698 case VIDIOC_SUBSCRIBE_EVENT
:
699 if (v4l2_subdev_has_op(sd
, core
, subscribe_event
))
700 return v4l2_subdev_call(sd
, core
, subscribe_event
,
703 if ((sd
->flags
& V4L2_SUBDEV_FL_HAS_EVENTS
) &&
705 return v4l2_ctrl_subdev_subscribe_event(sd
, vfh
, arg
);
709 case VIDIOC_UNSUBSCRIBE_EVENT
:
710 if (v4l2_subdev_has_op(sd
, core
, unsubscribe_event
))
711 return v4l2_subdev_call(sd
, core
, unsubscribe_event
,
714 if (sd
->flags
& V4L2_SUBDEV_FL_HAS_EVENTS
)
715 return v4l2_event_subdev_unsubscribe(sd
, vfh
, arg
);
719 #ifdef CONFIG_VIDEO_ADV_DEBUG
720 case VIDIOC_DBG_G_REGISTER
:
722 struct v4l2_dbg_register
*p
= arg
;
724 if (!capable(CAP_SYS_ADMIN
))
726 return v4l2_subdev_call(sd
, core
, g_register
, p
);
728 case VIDIOC_DBG_S_REGISTER
:
730 struct v4l2_dbg_register
*p
= arg
;
732 if (!capable(CAP_SYS_ADMIN
))
734 return v4l2_subdev_call(sd
, core
, s_register
, p
);
736 case VIDIOC_DBG_G_CHIP_INFO
:
738 struct v4l2_dbg_chip_info
*p
= arg
;
740 if (p
->match
.type
!= V4L2_CHIP_MATCH_SUBDEV
|| p
->match
.addr
)
742 if (sd
->ops
->core
&& sd
->ops
->core
->s_register
)
743 p
->flags
|= V4L2_CHIP_FL_WRITABLE
;
744 if (sd
->ops
->core
&& sd
->ops
->core
->g_register
)
745 p
->flags
|= V4L2_CHIP_FL_READABLE
;
746 strscpy(p
->name
, sd
->name
, sizeof(p
->name
));
751 case VIDIOC_LOG_STATUS
: {
754 pr_info("%s: ================= START STATUS =================\n",
756 ret
= v4l2_subdev_call(sd
, core
, log_status
);
757 pr_info("%s: ================== END STATUS ==================\n",
762 case VIDIOC_SUBDEV_G_FMT
: {
763 struct v4l2_subdev_format
*format
= arg
;
765 if (!client_supports_streams
)
768 memset(format
->reserved
, 0, sizeof(format
->reserved
));
769 memset(format
->format
.reserved
, 0, sizeof(format
->format
.reserved
));
770 return v4l2_subdev_call(sd
, pad
, get_fmt
, state
, format
);
773 case VIDIOC_SUBDEV_S_FMT
: {
774 struct v4l2_subdev_format
*format
= arg
;
776 if (format
->which
!= V4L2_SUBDEV_FORMAT_TRY
&& ro_subdev
)
779 if (!client_supports_streams
)
782 memset(format
->reserved
, 0, sizeof(format
->reserved
));
783 memset(format
->format
.reserved
, 0, sizeof(format
->format
.reserved
));
784 return v4l2_subdev_call(sd
, pad
, set_fmt
, state
, format
);
787 case VIDIOC_SUBDEV_G_CROP
: {
788 struct v4l2_subdev_crop
*crop
= arg
;
789 struct v4l2_subdev_selection sel
;
791 if (!client_supports_streams
)
794 memset(crop
->reserved
, 0, sizeof(crop
->reserved
));
795 memset(&sel
, 0, sizeof(sel
));
796 sel
.which
= crop
->which
;
798 sel
.stream
= crop
->stream
;
799 sel
.target
= V4L2_SEL_TGT_CROP
;
801 rval
= v4l2_subdev_call(
802 sd
, pad
, get_selection
, state
, &sel
);
809 case VIDIOC_SUBDEV_S_CROP
: {
810 struct v4l2_subdev_crop
*crop
= arg
;
811 struct v4l2_subdev_selection sel
;
813 if (crop
->which
!= V4L2_SUBDEV_FORMAT_TRY
&& ro_subdev
)
816 if (!client_supports_streams
)
819 memset(crop
->reserved
, 0, sizeof(crop
->reserved
));
820 memset(&sel
, 0, sizeof(sel
));
821 sel
.which
= crop
->which
;
823 sel
.stream
= crop
->stream
;
824 sel
.target
= V4L2_SEL_TGT_CROP
;
827 rval
= v4l2_subdev_call(
828 sd
, pad
, set_selection
, state
, &sel
);
835 case VIDIOC_SUBDEV_ENUM_MBUS_CODE
: {
836 struct v4l2_subdev_mbus_code_enum
*code
= arg
;
838 if (!client_supports_streams
)
841 memset(code
->reserved
, 0, sizeof(code
->reserved
));
842 return v4l2_subdev_call(sd
, pad
, enum_mbus_code
, state
,
846 case VIDIOC_SUBDEV_ENUM_FRAME_SIZE
: {
847 struct v4l2_subdev_frame_size_enum
*fse
= arg
;
849 if (!client_supports_streams
)
852 memset(fse
->reserved
, 0, sizeof(fse
->reserved
));
853 return v4l2_subdev_call(sd
, pad
, enum_frame_size
, state
,
857 case VIDIOC_SUBDEV_G_FRAME_INTERVAL
: {
858 struct v4l2_subdev_frame_interval
*fi
= arg
;
860 if (!client_supports_streams
)
863 memset(fi
->reserved
, 0, sizeof(fi
->reserved
));
864 return v4l2_subdev_call(sd
, pad
, get_frame_interval
, state
, fi
);
867 case VIDIOC_SUBDEV_S_FRAME_INTERVAL
: {
868 struct v4l2_subdev_frame_interval
*fi
= arg
;
870 if (!client_supports_streams
)
873 if (fi
->which
!= V4L2_SUBDEV_FORMAT_TRY
&& ro_subdev
)
876 memset(fi
->reserved
, 0, sizeof(fi
->reserved
));
877 return v4l2_subdev_call(sd
, pad
, set_frame_interval
, state
, fi
);
880 case VIDIOC_SUBDEV_ENUM_FRAME_INTERVAL
: {
881 struct v4l2_subdev_frame_interval_enum
*fie
= arg
;
883 if (!client_supports_streams
)
886 memset(fie
->reserved
, 0, sizeof(fie
->reserved
));
887 return v4l2_subdev_call(sd
, pad
, enum_frame_interval
, state
,
891 case VIDIOC_SUBDEV_G_SELECTION
: {
892 struct v4l2_subdev_selection
*sel
= arg
;
894 if (!client_supports_streams
)
897 memset(sel
->reserved
, 0, sizeof(sel
->reserved
));
898 return v4l2_subdev_call(
899 sd
, pad
, get_selection
, state
, sel
);
902 case VIDIOC_SUBDEV_S_SELECTION
: {
903 struct v4l2_subdev_selection
*sel
= arg
;
905 if (sel
->which
!= V4L2_SUBDEV_FORMAT_TRY
&& ro_subdev
)
908 if (!client_supports_streams
)
911 memset(sel
->reserved
, 0, sizeof(sel
->reserved
));
912 return v4l2_subdev_call(
913 sd
, pad
, set_selection
, state
, sel
);
916 case VIDIOC_G_EDID
: {
917 struct v4l2_subdev_edid
*edid
= arg
;
919 return v4l2_subdev_call(sd
, pad
, get_edid
, edid
);
922 case VIDIOC_S_EDID
: {
923 struct v4l2_subdev_edid
*edid
= arg
;
925 return v4l2_subdev_call(sd
, pad
, set_edid
, edid
);
928 case VIDIOC_SUBDEV_DV_TIMINGS_CAP
: {
929 struct v4l2_dv_timings_cap
*cap
= arg
;
931 return v4l2_subdev_call(sd
, pad
, dv_timings_cap
, cap
);
934 case VIDIOC_SUBDEV_ENUM_DV_TIMINGS
: {
935 struct v4l2_enum_dv_timings
*dvt
= arg
;
937 return v4l2_subdev_call(sd
, pad
, enum_dv_timings
, dvt
);
940 case VIDIOC_SUBDEV_QUERY_DV_TIMINGS
:
941 return v4l2_subdev_call(sd
, pad
, query_dv_timings
, 0, arg
);
943 case VIDIOC_SUBDEV_G_DV_TIMINGS
:
944 return v4l2_subdev_call(sd
, pad
, g_dv_timings
, 0, arg
);
946 case VIDIOC_SUBDEV_S_DV_TIMINGS
:
950 return v4l2_subdev_call(sd
, pad
, s_dv_timings
, 0, arg
);
952 case VIDIOC_SUBDEV_G_STD
:
953 return v4l2_subdev_call(sd
, video
, g_std
, arg
);
955 case VIDIOC_SUBDEV_S_STD
: {
956 v4l2_std_id
*std
= arg
;
961 return v4l2_subdev_call(sd
, video
, s_std
, *std
);
964 case VIDIOC_SUBDEV_ENUMSTD
: {
965 struct v4l2_standard
*p
= arg
;
968 if (v4l2_subdev_call(sd
, video
, g_tvnorms
, &id
))
971 return v4l_video_std_enumstd(p
, id
);
974 case VIDIOC_SUBDEV_QUERYSTD
:
975 return v4l2_subdev_call(sd
, video
, querystd
, arg
);
977 case VIDIOC_SUBDEV_G_ROUTING
: {
978 struct v4l2_subdev_routing
*routing
= arg
;
979 struct v4l2_subdev_krouting
*krouting
;
981 if (!v4l2_subdev_enable_streams_api
)
984 if (!(sd
->flags
& V4L2_SUBDEV_FL_STREAMS
))
987 memset(routing
->reserved
, 0, sizeof(routing
->reserved
));
989 krouting
= &state
->routing
;
991 memcpy((struct v4l2_subdev_route
*)(uintptr_t)routing
->routes
,
993 min(krouting
->num_routes
, routing
->len_routes
) *
994 sizeof(*krouting
->routes
));
995 routing
->num_routes
= krouting
->num_routes
;
1000 case VIDIOC_SUBDEV_S_ROUTING
: {
1001 struct v4l2_subdev_routing
*routing
= arg
;
1002 struct v4l2_subdev_route
*routes
=
1003 (struct v4l2_subdev_route
*)(uintptr_t)routing
->routes
;
1004 struct v4l2_subdev_krouting krouting
= {};
1007 if (!v4l2_subdev_enable_streams_api
)
1008 return -ENOIOCTLCMD
;
1010 if (!(sd
->flags
& V4L2_SUBDEV_FL_STREAMS
))
1011 return -ENOIOCTLCMD
;
1013 if (routing
->which
!= V4L2_SUBDEV_FORMAT_TRY
&& ro_subdev
)
1016 if (routing
->num_routes
> routing
->len_routes
)
1019 memset(routing
->reserved
, 0, sizeof(routing
->reserved
));
1021 for (i
= 0; i
< routing
->num_routes
; ++i
) {
1022 const struct v4l2_subdev_route
*route
= &routes
[i
];
1023 const struct media_pad
*pads
= sd
->entity
.pads
;
1025 if (route
->sink_stream
> V4L2_SUBDEV_MAX_STREAM_ID
||
1026 route
->source_stream
> V4L2_SUBDEV_MAX_STREAM_ID
)
1029 if (route
->sink_pad
>= sd
->entity
.num_pads
)
1032 if (!(pads
[route
->sink_pad
].flags
&
1036 if (route
->source_pad
>= sd
->entity
.num_pads
)
1039 if (!(pads
[route
->source_pad
].flags
&
1040 MEDIA_PAD_FL_SOURCE
))
1045 * If the driver doesn't support setting routing, just return
1046 * the routing table.
1048 if (!v4l2_subdev_has_op(sd
, pad
, set_routing
)) {
1049 memcpy((struct v4l2_subdev_route
*)(uintptr_t)routing
->routes
,
1050 state
->routing
.routes
,
1051 min(state
->routing
.num_routes
, routing
->len_routes
) *
1052 sizeof(*state
->routing
.routes
));
1053 routing
->num_routes
= state
->routing
.num_routes
;
1058 krouting
.num_routes
= routing
->num_routes
;
1059 krouting
.len_routes
= routing
->len_routes
;
1060 krouting
.routes
= routes
;
1062 rval
= v4l2_subdev_call(sd
, pad
, set_routing
, state
,
1063 routing
->which
, &krouting
);
1067 memcpy((struct v4l2_subdev_route
*)(uintptr_t)routing
->routes
,
1068 state
->routing
.routes
,
1069 min(state
->routing
.num_routes
, routing
->len_routes
) *
1070 sizeof(*state
->routing
.routes
));
1071 routing
->num_routes
= state
->routing
.num_routes
;
1076 case VIDIOC_SUBDEV_G_CLIENT_CAP
: {
1077 struct v4l2_subdev_client_capability
*client_cap
= arg
;
1079 client_cap
->capabilities
= subdev_fh
->client_caps
;
1084 case VIDIOC_SUBDEV_S_CLIENT_CAP
: {
1085 struct v4l2_subdev_client_capability
*client_cap
= arg
;
1088 * Clear V4L2_SUBDEV_CLIENT_CAP_STREAMS if streams API is not
1089 * enabled. Remove this when streams API is no longer
1092 if (!v4l2_subdev_enable_streams_api
)
1093 client_cap
->capabilities
&= ~V4L2_SUBDEV_CLIENT_CAP_STREAMS
;
1095 /* Filter out unsupported capabilities */
1096 client_cap
->capabilities
&= (V4L2_SUBDEV_CLIENT_CAP_STREAMS
|
1097 V4L2_SUBDEV_CLIENT_CAP_INTERVAL_USES_WHICH
);
1099 subdev_fh
->client_caps
= client_cap
->capabilities
;
1105 return v4l2_subdev_call(sd
, core
, ioctl
, cmd
, arg
);
1111 static long subdev_do_ioctl_lock(struct file
*file
, unsigned int cmd
, void *arg
)
1113 struct video_device
*vdev
= video_devdata(file
);
1114 struct mutex
*lock
= vdev
->lock
;
1117 if (lock
&& mutex_lock_interruptible(lock
))
1118 return -ERESTARTSYS
;
1120 if (video_is_registered(vdev
)) {
1121 struct v4l2_subdev
*sd
= vdev_to_v4l2_subdev(vdev
);
1122 struct v4l2_fh
*vfh
= file
->private_data
;
1123 struct v4l2_subdev_fh
*subdev_fh
= to_v4l2_subdev_fh(vfh
);
1124 struct v4l2_subdev_state
*state
;
1126 state
= subdev_ioctl_get_state(sd
, subdev_fh
, cmd
, arg
);
1129 v4l2_subdev_lock_state(state
);
1131 ret
= subdev_do_ioctl(file
, cmd
, arg
, state
);
1134 v4l2_subdev_unlock_state(state
);
1142 static long subdev_ioctl(struct file
*file
, unsigned int cmd
,
1145 return video_usercopy(file
, cmd
, arg
, subdev_do_ioctl_lock
);
1148 #ifdef CONFIG_COMPAT
1149 static long subdev_compat_ioctl32(struct file
*file
, unsigned int cmd
,
1152 struct video_device
*vdev
= video_devdata(file
);
1153 struct v4l2_subdev
*sd
= vdev_to_v4l2_subdev(vdev
);
1155 return v4l2_subdev_call(sd
, core
, compat_ioctl32
, cmd
, arg
);
1159 #else /* CONFIG_VIDEO_V4L2_SUBDEV_API */
1160 static long subdev_ioctl(struct file
*file
, unsigned int cmd
,
1166 #ifdef CONFIG_COMPAT
1167 static long subdev_compat_ioctl32(struct file
*file
, unsigned int cmd
,
1173 #endif /* CONFIG_VIDEO_V4L2_SUBDEV_API */
1175 static __poll_t
subdev_poll(struct file
*file
, poll_table
*wait
)
1177 struct video_device
*vdev
= video_devdata(file
);
1178 struct v4l2_subdev
*sd
= vdev_to_v4l2_subdev(vdev
);
1179 struct v4l2_fh
*fh
= file
->private_data
;
1181 if (!(sd
->flags
& V4L2_SUBDEV_FL_HAS_EVENTS
))
1184 poll_wait(file
, &fh
->wait
, wait
);
1186 if (v4l2_event_pending(fh
))
1192 const struct v4l2_file_operations v4l2_subdev_fops
= {
1193 .owner
= THIS_MODULE
,
1194 .open
= subdev_open
,
1195 .unlocked_ioctl
= subdev_ioctl
,
1196 #ifdef CONFIG_COMPAT
1197 .compat_ioctl32
= subdev_compat_ioctl32
,
1199 .release
= subdev_close
,
1200 .poll
= subdev_poll
,
1203 #ifdef CONFIG_MEDIA_CONTROLLER
1205 int v4l2_subdev_get_fwnode_pad_1_to_1(struct media_entity
*entity
,
1206 struct fwnode_endpoint
*endpoint
)
1208 struct fwnode_handle
*fwnode
;
1209 struct v4l2_subdev
*sd
;
1211 if (!is_media_entity_v4l2_subdev(entity
))
1214 sd
= media_entity_to_v4l2_subdev(entity
);
1216 fwnode
= fwnode_graph_get_port_parent(endpoint
->local_fwnode
);
1217 fwnode_handle_put(fwnode
);
1219 if (device_match_fwnode(sd
->dev
, fwnode
))
1220 return endpoint
->port
;
1224 EXPORT_SYMBOL_GPL(v4l2_subdev_get_fwnode_pad_1_to_1
);
1226 int v4l2_subdev_link_validate_default(struct v4l2_subdev
*sd
,
1227 struct media_link
*link
,
1228 struct v4l2_subdev_format
*source_fmt
,
1229 struct v4l2_subdev_format
*sink_fmt
)
1233 /* The width, height and code must match. */
1234 if (source_fmt
->format
.width
!= sink_fmt
->format
.width
) {
1235 dev_dbg(sd
->entity
.graph_obj
.mdev
->dev
,
1236 "%s: width does not match (source %u, sink %u)\n",
1238 source_fmt
->format
.width
, sink_fmt
->format
.width
);
1242 if (source_fmt
->format
.height
!= sink_fmt
->format
.height
) {
1243 dev_dbg(sd
->entity
.graph_obj
.mdev
->dev
,
1244 "%s: height does not match (source %u, sink %u)\n",
1246 source_fmt
->format
.height
, sink_fmt
->format
.height
);
1250 if (source_fmt
->format
.code
!= sink_fmt
->format
.code
) {
1251 dev_dbg(sd
->entity
.graph_obj
.mdev
->dev
,
1252 "%s: media bus code does not match (source 0x%8.8x, sink 0x%8.8x)\n",
1254 source_fmt
->format
.code
, sink_fmt
->format
.code
);
1258 /* The field order must match, or the sink field order must be NONE
1259 * to support interlaced hardware connected to bridges that support
1260 * progressive formats only.
1262 if (source_fmt
->format
.field
!= sink_fmt
->format
.field
&&
1263 sink_fmt
->format
.field
!= V4L2_FIELD_NONE
) {
1264 dev_dbg(sd
->entity
.graph_obj
.mdev
->dev
,
1265 "%s: field does not match (source %u, sink %u)\n",
1267 source_fmt
->format
.field
, sink_fmt
->format
.field
);
1274 dev_dbg(sd
->entity
.graph_obj
.mdev
->dev
,
1275 "%s: link was \"%s\":%u -> \"%s\":%u\n", __func__
,
1276 link
->source
->entity
->name
, link
->source
->index
,
1277 link
->sink
->entity
->name
, link
->sink
->index
);
1281 EXPORT_SYMBOL_GPL(v4l2_subdev_link_validate_default
);
1284 v4l2_subdev_link_validate_get_format(struct media_pad
*pad
, u32 stream
,
1285 struct v4l2_subdev_format
*fmt
,
1288 struct v4l2_subdev_state
*state
;
1289 struct v4l2_subdev
*sd
;
1292 sd
= media_entity_to_v4l2_subdev(pad
->entity
);
1294 fmt
->which
= V4L2_SUBDEV_FORMAT_ACTIVE
;
1295 fmt
->pad
= pad
->index
;
1296 fmt
->stream
= stream
;
1299 state
= v4l2_subdev_get_locked_active_state(sd
);
1301 state
= v4l2_subdev_lock_and_get_active_state(sd
);
1303 ret
= v4l2_subdev_call(sd
, pad
, get_fmt
, state
, fmt
);
1305 if (!states_locked
&& state
)
1306 v4l2_subdev_unlock_state(state
);
1311 #if defined(CONFIG_VIDEO_V4L2_SUBDEV_API)
1313 static void __v4l2_link_validate_get_streams(struct media_pad
*pad
,
1317 struct v4l2_subdev_route
*route
;
1318 struct v4l2_subdev_state
*state
;
1319 struct v4l2_subdev
*subdev
;
1321 subdev
= media_entity_to_v4l2_subdev(pad
->entity
);
1326 state
= v4l2_subdev_get_locked_active_state(subdev
);
1328 state
= v4l2_subdev_lock_and_get_active_state(subdev
);
1330 if (WARN_ON(!state
))
1333 for_each_active_route(&state
->routing
, route
) {
1337 if (pad
->flags
& MEDIA_PAD_FL_SOURCE
) {
1338 route_pad
= route
->source_pad
;
1339 route_stream
= route
->source_stream
;
1341 route_pad
= route
->sink_pad
;
1342 route_stream
= route
->sink_stream
;
1345 if (route_pad
!= pad
->index
)
1348 *streams_mask
|= BIT_ULL(route_stream
);
1352 v4l2_subdev_unlock_state(state
);
1355 #endif /* CONFIG_VIDEO_V4L2_SUBDEV_API */
1357 static void v4l2_link_validate_get_streams(struct media_pad
*pad
,
1361 struct v4l2_subdev
*subdev
= media_entity_to_v4l2_subdev(pad
->entity
);
1363 if (!(subdev
->flags
& V4L2_SUBDEV_FL_STREAMS
)) {
1364 /* Non-streams subdevs have an implicit stream 0 */
1365 *streams_mask
= BIT_ULL(0);
1369 #if defined(CONFIG_VIDEO_V4L2_SUBDEV_API)
1370 __v4l2_link_validate_get_streams(pad
, streams_mask
, states_locked
);
1372 /* This shouldn't happen */
1377 static int v4l2_subdev_link_validate_locked(struct media_link
*link
, bool states_locked
)
1379 struct v4l2_subdev
*sink_subdev
=
1380 media_entity_to_v4l2_subdev(link
->sink
->entity
);
1381 struct device
*dev
= sink_subdev
->entity
.graph_obj
.mdev
->dev
;
1382 u64 source_streams_mask
;
1383 u64 sink_streams_mask
;
1384 u64 dangling_sink_streams
;
1388 dev_dbg(dev
, "validating link \"%s\":%u -> \"%s\":%u\n",
1389 link
->source
->entity
->name
, link
->source
->index
,
1390 link
->sink
->entity
->name
, link
->sink
->index
);
1392 v4l2_link_validate_get_streams(link
->source
, &source_streams_mask
, states_locked
);
1393 v4l2_link_validate_get_streams(link
->sink
, &sink_streams_mask
, states_locked
);
1396 * It is ok to have more source streams than sink streams as extra
1397 * source streams can just be ignored by the receiver, but having extra
1398 * sink streams is an error as streams must have a source.
1400 dangling_sink_streams
= (source_streams_mask
^ sink_streams_mask
) &
1402 if (dangling_sink_streams
) {
1403 dev_err(dev
, "Dangling sink streams: mask %#llx\n",
1404 dangling_sink_streams
);
1408 /* Validate source and sink stream formats */
1410 for (stream
= 0; stream
< sizeof(sink_streams_mask
) * 8; ++stream
) {
1411 struct v4l2_subdev_format sink_fmt
, source_fmt
;
1413 if (!(sink_streams_mask
& BIT_ULL(stream
)))
1416 dev_dbg(dev
, "validating stream \"%s\":%u:%u -> \"%s\":%u:%u\n",
1417 link
->source
->entity
->name
, link
->source
->index
, stream
,
1418 link
->sink
->entity
->name
, link
->sink
->index
, stream
);
1420 ret
= v4l2_subdev_link_validate_get_format(link
->source
, stream
,
1421 &source_fmt
, states_locked
);
1424 "Failed to get format for \"%s\":%u:%u (but that's ok)\n",
1425 link
->source
->entity
->name
, link
->source
->index
,
1430 ret
= v4l2_subdev_link_validate_get_format(link
->sink
, stream
,
1431 &sink_fmt
, states_locked
);
1434 "Failed to get format for \"%s\":%u:%u (but that's ok)\n",
1435 link
->sink
->entity
->name
, link
->sink
->index
,
1440 /* TODO: add stream number to link_validate() */
1441 ret
= v4l2_subdev_call(sink_subdev
, pad
, link_validate
, link
,
1442 &source_fmt
, &sink_fmt
);
1446 if (ret
!= -ENOIOCTLCMD
)
1449 ret
= v4l2_subdev_link_validate_default(sink_subdev
, link
,
1450 &source_fmt
, &sink_fmt
);
1459 int v4l2_subdev_link_validate(struct media_link
*link
)
1461 struct v4l2_subdev
*source_sd
, *sink_sd
;
1462 struct v4l2_subdev_state
*source_state
, *sink_state
;
1467 * Links are validated in the context of the sink entity. Usage of this
1468 * helper on a sink that is not a subdev is a clear driver bug.
1470 if (WARN_ON_ONCE(!is_media_entity_v4l2_subdev(link
->sink
->entity
)))
1474 * If the source is a video device, delegate link validation to it. This
1475 * allows usage of this helper for subdev connected to a video output
1476 * device, provided that the driver implement the video output device's
1477 * .link_validate() operation.
1479 if (is_media_entity_v4l2_video_device(link
->source
->entity
)) {
1480 struct media_entity
*source
= link
->source
->entity
;
1482 if (!source
->ops
|| !source
->ops
->link_validate
) {
1484 * Many existing drivers do not implement the required
1485 * .link_validate() operation for their video devices.
1486 * Print a warning to get the drivers fixed, and return
1487 * 0 to avoid breaking userspace. This should
1488 * eventually be turned into a WARN_ON() when all
1489 * drivers will have been fixed.
1491 pr_warn_once("video device '%s' does not implement .link_validate(), driver bug!\n",
1497 * Avoid infinite loops in case a video device incorrectly uses
1498 * this helper function as its .link_validate() handler.
1500 if (WARN_ON(source
->ops
->link_validate
== v4l2_subdev_link_validate
))
1503 return source
->ops
->link_validate(link
);
1507 * If the source is still not a subdev, usage of this helper is a clear
1510 if (WARN_ON(!is_media_entity_v4l2_subdev(link
->source
->entity
)))
1513 sink_sd
= media_entity_to_v4l2_subdev(link
->sink
->entity
);
1514 source_sd
= media_entity_to_v4l2_subdev(link
->source
->entity
);
1516 sink_state
= v4l2_subdev_get_unlocked_active_state(sink_sd
);
1517 source_state
= v4l2_subdev_get_unlocked_active_state(source_sd
);
1519 states_locked
= sink_state
&& source_state
;
1522 v4l2_subdev_lock_states(sink_state
, source_state
);
1524 ret
= v4l2_subdev_link_validate_locked(link
, states_locked
);
1527 v4l2_subdev_unlock_states(sink_state
, source_state
);
1531 EXPORT_SYMBOL_GPL(v4l2_subdev_link_validate
);
1533 bool v4l2_subdev_has_pad_interdep(struct media_entity
*entity
,
1534 unsigned int pad0
, unsigned int pad1
)
1536 struct v4l2_subdev
*sd
= media_entity_to_v4l2_subdev(entity
);
1537 struct v4l2_subdev_krouting
*routing
;
1538 struct v4l2_subdev_state
*state
;
1541 state
= v4l2_subdev_lock_and_get_active_state(sd
);
1543 routing
= &state
->routing
;
1545 for (i
= 0; i
< routing
->num_routes
; ++i
) {
1546 struct v4l2_subdev_route
*route
= &routing
->routes
[i
];
1548 if (!(route
->flags
& V4L2_SUBDEV_ROUTE_FL_ACTIVE
))
1551 if ((route
->sink_pad
== pad0
&& route
->source_pad
== pad1
) ||
1552 (route
->source_pad
== pad0
&& route
->sink_pad
== pad1
)) {
1553 v4l2_subdev_unlock_state(state
);
1558 v4l2_subdev_unlock_state(state
);
1562 EXPORT_SYMBOL_GPL(v4l2_subdev_has_pad_interdep
);
1564 struct v4l2_subdev_state
*
1565 __v4l2_subdev_state_alloc(struct v4l2_subdev
*sd
, const char *lock_name
,
1566 struct lock_class_key
*lock_key
)
1568 struct v4l2_subdev_state
*state
;
1571 state
= kzalloc(sizeof(*state
), GFP_KERNEL
);
1573 return ERR_PTR(-ENOMEM
);
1575 __mutex_init(&state
->_lock
, lock_name
, lock_key
);
1577 state
->lock
= sd
->state_lock
;
1579 state
->lock
= &state
->_lock
;
1583 /* Drivers that support streams do not need the legacy pad config */
1584 if (!(sd
->flags
& V4L2_SUBDEV_FL_STREAMS
) && sd
->entity
.num_pads
) {
1585 state
->pads
= kvcalloc(sd
->entity
.num_pads
,
1586 sizeof(*state
->pads
), GFP_KERNEL
);
1593 if (sd
->internal_ops
&& sd
->internal_ops
->init_state
) {
1595 * There can be no race at this point, but we lock the state
1596 * anyway to satisfy lockdep checks.
1598 v4l2_subdev_lock_state(state
);
1599 ret
= sd
->internal_ops
->init_state(sd
, state
);
1600 v4l2_subdev_unlock_state(state
);
1609 if (state
&& state
->pads
)
1610 kvfree(state
->pads
);
1614 return ERR_PTR(ret
);
1616 EXPORT_SYMBOL_GPL(__v4l2_subdev_state_alloc
);
1618 void __v4l2_subdev_state_free(struct v4l2_subdev_state
*state
)
1623 mutex_destroy(&state
->_lock
);
1625 kfree(state
->routing
.routes
);
1626 kvfree(state
->stream_configs
.configs
);
1627 kvfree(state
->pads
);
1630 EXPORT_SYMBOL_GPL(__v4l2_subdev_state_free
);
1632 int __v4l2_subdev_init_finalize(struct v4l2_subdev
*sd
, const char *name
,
1633 struct lock_class_key
*key
)
1635 struct v4l2_subdev_state
*state
;
1636 struct device
*dev
= sd
->dev
;
1637 bool has_disable_streams
;
1638 bool has_enable_streams
;
1641 /* Check that the subdevice implements the required features */
1643 has_s_stream
= v4l2_subdev_has_op(sd
, video
, s_stream
);
1644 has_enable_streams
= v4l2_subdev_has_op(sd
, pad
, enable_streams
);
1645 has_disable_streams
= v4l2_subdev_has_op(sd
, pad
, disable_streams
);
1647 if (has_enable_streams
!= has_disable_streams
) {
1649 "subdev '%s' must implement both or neither of .enable_streams() and .disable_streams()\n",
1654 if (sd
->flags
& V4L2_SUBDEV_FL_STREAMS
) {
1655 if (has_s_stream
&& !has_enable_streams
) {
1657 "subdev '%s' must implement .enable/disable_streams()\n",
1664 if (sd
->ctrl_handler
)
1665 sd
->flags
|= V4L2_SUBDEV_FL_HAS_EVENTS
;
1667 state
= __v4l2_subdev_state_alloc(sd
, name
, key
);
1669 return PTR_ERR(state
);
1671 sd
->active_state
= state
;
1675 EXPORT_SYMBOL_GPL(__v4l2_subdev_init_finalize
);
1677 void v4l2_subdev_cleanup(struct v4l2_subdev
*sd
)
1679 struct v4l2_async_subdev_endpoint
*ase
, *ase_tmp
;
1681 __v4l2_subdev_state_free(sd
->active_state
);
1682 sd
->active_state
= NULL
;
1684 /* Uninitialised sub-device, bail out here. */
1685 if (!sd
->async_subdev_endpoint_list
.next
)
1688 list_for_each_entry_safe(ase
, ase_tmp
, &sd
->async_subdev_endpoint_list
,
1689 async_subdev_endpoint_entry
) {
1690 list_del(&ase
->async_subdev_endpoint_entry
);
1695 EXPORT_SYMBOL_GPL(v4l2_subdev_cleanup
);
1697 struct v4l2_mbus_framefmt
*
1698 __v4l2_subdev_state_get_format(struct v4l2_subdev_state
*state
,
1699 unsigned int pad
, u32 stream
)
1701 struct v4l2_subdev_stream_configs
*stream_configs
;
1704 if (WARN_ON_ONCE(!state
))
1711 if (pad
>= state
->sd
->entity
.num_pads
)
1714 return &state
->pads
[pad
].format
;
1717 lockdep_assert_held(state
->lock
);
1719 stream_configs
= &state
->stream_configs
;
1721 for (i
= 0; i
< stream_configs
->num_configs
; ++i
) {
1722 if (stream_configs
->configs
[i
].pad
== pad
&&
1723 stream_configs
->configs
[i
].stream
== stream
)
1724 return &stream_configs
->configs
[i
].fmt
;
1729 EXPORT_SYMBOL_GPL(__v4l2_subdev_state_get_format
);
1732 __v4l2_subdev_state_get_crop(struct v4l2_subdev_state
*state
, unsigned int pad
,
1735 struct v4l2_subdev_stream_configs
*stream_configs
;
1738 if (WARN_ON_ONCE(!state
))
1745 if (pad
>= state
->sd
->entity
.num_pads
)
1748 return &state
->pads
[pad
].crop
;
1751 lockdep_assert_held(state
->lock
);
1753 stream_configs
= &state
->stream_configs
;
1755 for (i
= 0; i
< stream_configs
->num_configs
; ++i
) {
1756 if (stream_configs
->configs
[i
].pad
== pad
&&
1757 stream_configs
->configs
[i
].stream
== stream
)
1758 return &stream_configs
->configs
[i
].crop
;
1763 EXPORT_SYMBOL_GPL(__v4l2_subdev_state_get_crop
);
1766 __v4l2_subdev_state_get_compose(struct v4l2_subdev_state
*state
,
1767 unsigned int pad
, u32 stream
)
1769 struct v4l2_subdev_stream_configs
*stream_configs
;
1772 if (WARN_ON_ONCE(!state
))
1779 if (pad
>= state
->sd
->entity
.num_pads
)
1782 return &state
->pads
[pad
].compose
;
1785 lockdep_assert_held(state
->lock
);
1787 stream_configs
= &state
->stream_configs
;
1789 for (i
= 0; i
< stream_configs
->num_configs
; ++i
) {
1790 if (stream_configs
->configs
[i
].pad
== pad
&&
1791 stream_configs
->configs
[i
].stream
== stream
)
1792 return &stream_configs
->configs
[i
].compose
;
1797 EXPORT_SYMBOL_GPL(__v4l2_subdev_state_get_compose
);
1800 __v4l2_subdev_state_get_interval(struct v4l2_subdev_state
*state
,
1801 unsigned int pad
, u32 stream
)
1803 struct v4l2_subdev_stream_configs
*stream_configs
;
1806 if (WARN_ON(!state
))
1809 lockdep_assert_held(state
->lock
);
1815 if (pad
>= state
->sd
->entity
.num_pads
)
1818 return &state
->pads
[pad
].interval
;
1821 lockdep_assert_held(state
->lock
);
1823 stream_configs
= &state
->stream_configs
;
1825 for (i
= 0; i
< stream_configs
->num_configs
; ++i
) {
1826 if (stream_configs
->configs
[i
].pad
== pad
&&
1827 stream_configs
->configs
[i
].stream
== stream
)
1828 return &stream_configs
->configs
[i
].interval
;
1833 EXPORT_SYMBOL_GPL(__v4l2_subdev_state_get_interval
);
1835 #if defined(CONFIG_VIDEO_V4L2_SUBDEV_API)
1838 v4l2_subdev_init_stream_configs(struct v4l2_subdev_stream_configs
*stream_configs
,
1839 const struct v4l2_subdev_krouting
*routing
)
1841 struct v4l2_subdev_stream_configs new_configs
= { 0 };
1842 struct v4l2_subdev_route
*route
;
1845 /* Count number of formats needed */
1846 for_each_active_route(routing
, route
) {
1848 * Each route needs a format on both ends of the route.
1850 new_configs
.num_configs
+= 2;
1853 if (new_configs
.num_configs
) {
1854 new_configs
.configs
= kvcalloc(new_configs
.num_configs
,
1855 sizeof(*new_configs
.configs
),
1858 if (!new_configs
.configs
)
1863 * Fill in the 'pad' and stream' value for each item in the array from
1868 for_each_active_route(routing
, route
) {
1869 new_configs
.configs
[idx
].pad
= route
->sink_pad
;
1870 new_configs
.configs
[idx
].stream
= route
->sink_stream
;
1874 new_configs
.configs
[idx
].pad
= route
->source_pad
;
1875 new_configs
.configs
[idx
].stream
= route
->source_stream
;
1880 kvfree(stream_configs
->configs
);
1881 *stream_configs
= new_configs
;
1886 int v4l2_subdev_get_fmt(struct v4l2_subdev
*sd
, struct v4l2_subdev_state
*state
,
1887 struct v4l2_subdev_format
*format
)
1889 struct v4l2_mbus_framefmt
*fmt
;
1891 fmt
= v4l2_subdev_state_get_format(state
, format
->pad
, format
->stream
);
1895 format
->format
= *fmt
;
1899 EXPORT_SYMBOL_GPL(v4l2_subdev_get_fmt
);
1901 int v4l2_subdev_get_frame_interval(struct v4l2_subdev
*sd
,
1902 struct v4l2_subdev_state
*state
,
1903 struct v4l2_subdev_frame_interval
*fi
)
1905 struct v4l2_fract
*interval
;
1907 interval
= v4l2_subdev_state_get_interval(state
, fi
->pad
, fi
->stream
);
1911 fi
->interval
= *interval
;
1915 EXPORT_SYMBOL_GPL(v4l2_subdev_get_frame_interval
);
1917 int v4l2_subdev_set_routing(struct v4l2_subdev
*sd
,
1918 struct v4l2_subdev_state
*state
,
1919 const struct v4l2_subdev_krouting
*routing
)
1921 struct v4l2_subdev_krouting
*dst
= &state
->routing
;
1922 const struct v4l2_subdev_krouting
*src
= routing
;
1923 struct v4l2_subdev_krouting new_routing
= { 0 };
1927 if (unlikely(check_mul_overflow((size_t)src
->num_routes
,
1928 sizeof(*src
->routes
), &bytes
)))
1931 lockdep_assert_held(state
->lock
);
1933 if (src
->num_routes
> 0) {
1934 new_routing
.routes
= kmemdup(src
->routes
, bytes
, GFP_KERNEL
);
1935 if (!new_routing
.routes
)
1939 new_routing
.num_routes
= src
->num_routes
;
1941 r
= v4l2_subdev_init_stream_configs(&state
->stream_configs
,
1944 kfree(new_routing
.routes
);
1953 EXPORT_SYMBOL_GPL(v4l2_subdev_set_routing
);
1955 struct v4l2_subdev_route
*
1956 __v4l2_subdev_next_active_route(const struct v4l2_subdev_krouting
*routing
,
1957 struct v4l2_subdev_route
*route
)
1962 route
= &routing
->routes
[0];
1964 for (; route
< routing
->routes
+ routing
->num_routes
; ++route
) {
1965 if (!(route
->flags
& V4L2_SUBDEV_ROUTE_FL_ACTIVE
))
1973 EXPORT_SYMBOL_GPL(__v4l2_subdev_next_active_route
);
1975 int v4l2_subdev_set_routing_with_fmt(struct v4l2_subdev
*sd
,
1976 struct v4l2_subdev_state
*state
,
1977 const struct v4l2_subdev_krouting
*routing
,
1978 const struct v4l2_mbus_framefmt
*fmt
)
1980 struct v4l2_subdev_stream_configs
*stream_configs
;
1984 ret
= v4l2_subdev_set_routing(sd
, state
, routing
);
1988 stream_configs
= &state
->stream_configs
;
1990 for (i
= 0; i
< stream_configs
->num_configs
; ++i
)
1991 stream_configs
->configs
[i
].fmt
= *fmt
;
1995 EXPORT_SYMBOL_GPL(v4l2_subdev_set_routing_with_fmt
);
1997 int v4l2_subdev_routing_find_opposite_end(const struct v4l2_subdev_krouting
*routing
,
1998 u32 pad
, u32 stream
, u32
*other_pad
,
2003 for (i
= 0; i
< routing
->num_routes
; ++i
) {
2004 struct v4l2_subdev_route
*route
= &routing
->routes
[i
];
2006 if (route
->source_pad
== pad
&&
2007 route
->source_stream
== stream
) {
2009 *other_pad
= route
->sink_pad
;
2011 *other_stream
= route
->sink_stream
;
2015 if (route
->sink_pad
== pad
&& route
->sink_stream
== stream
) {
2017 *other_pad
= route
->source_pad
;
2019 *other_stream
= route
->source_stream
;
2026 EXPORT_SYMBOL_GPL(v4l2_subdev_routing_find_opposite_end
);
2028 struct v4l2_mbus_framefmt
*
2029 v4l2_subdev_state_get_opposite_stream_format(struct v4l2_subdev_state
*state
,
2030 u32 pad
, u32 stream
)
2032 u32 other_pad
, other_stream
;
2035 ret
= v4l2_subdev_routing_find_opposite_end(&state
->routing
,
2037 &other_pad
, &other_stream
);
2041 return v4l2_subdev_state_get_format(state
, other_pad
, other_stream
);
2043 EXPORT_SYMBOL_GPL(v4l2_subdev_state_get_opposite_stream_format
);
2045 u64
v4l2_subdev_state_xlate_streams(const struct v4l2_subdev_state
*state
,
2046 u32 pad0
, u32 pad1
, u64
*streams
)
2048 const struct v4l2_subdev_krouting
*routing
= &state
->routing
;
2049 struct v4l2_subdev_route
*route
;
2053 for_each_active_route(routing
, route
) {
2054 if (route
->sink_pad
== pad0
&& route
->source_pad
== pad1
&&
2055 (*streams
& BIT_ULL(route
->sink_stream
))) {
2056 streams0
|= BIT_ULL(route
->sink_stream
);
2057 streams1
|= BIT_ULL(route
->source_stream
);
2059 if (route
->source_pad
== pad0
&& route
->sink_pad
== pad1
&&
2060 (*streams
& BIT_ULL(route
->source_stream
))) {
2061 streams0
|= BIT_ULL(route
->source_stream
);
2062 streams1
|= BIT_ULL(route
->sink_stream
);
2066 *streams
= streams0
;
2069 EXPORT_SYMBOL_GPL(v4l2_subdev_state_xlate_streams
);
2071 int v4l2_subdev_routing_validate(struct v4l2_subdev
*sd
,
2072 const struct v4l2_subdev_krouting
*routing
,
2073 enum v4l2_subdev_routing_restriction disallow
)
2075 u32
*remote_pads
= NULL
;
2079 if (disallow
& (V4L2_SUBDEV_ROUTING_NO_STREAM_MIX
|
2080 V4L2_SUBDEV_ROUTING_NO_MULTIPLEXING
)) {
2081 remote_pads
= kcalloc(sd
->entity
.num_pads
, sizeof(*remote_pads
),
2086 for (i
= 0; i
< sd
->entity
.num_pads
; ++i
)
2087 remote_pads
[i
] = U32_MAX
;
2090 for (i
= 0; i
< routing
->num_routes
; ++i
) {
2091 const struct v4l2_subdev_route
*route
= &routing
->routes
[i
];
2093 /* Validate the sink and source pad numbers. */
2094 if (route
->sink_pad
>= sd
->entity
.num_pads
||
2095 !(sd
->entity
.pads
[route
->sink_pad
].flags
& MEDIA_PAD_FL_SINK
)) {
2096 dev_dbg(sd
->dev
, "route %u sink (%u) is not a sink pad\n",
2097 i
, route
->sink_pad
);
2101 if (route
->source_pad
>= sd
->entity
.num_pads
||
2102 !(sd
->entity
.pads
[route
->source_pad
].flags
& MEDIA_PAD_FL_SOURCE
)) {
2103 dev_dbg(sd
->dev
, "route %u source (%u) is not a source pad\n",
2104 i
, route
->source_pad
);
2109 * V4L2_SUBDEV_ROUTING_NO_SINK_STREAM_MIX: all streams from a
2110 * sink pad must be routed to a single source pad.
2112 if (disallow
& V4L2_SUBDEV_ROUTING_NO_SINK_STREAM_MIX
) {
2113 if (remote_pads
[route
->sink_pad
] != U32_MAX
&&
2114 remote_pads
[route
->sink_pad
] != route
->source_pad
) {
2116 "route %u attempts to mix %s streams\n",
2123 * V4L2_SUBDEV_ROUTING_NO_SOURCE_STREAM_MIX: all streams on a
2124 * source pad must originate from a single sink pad.
2126 if (disallow
& V4L2_SUBDEV_ROUTING_NO_SOURCE_STREAM_MIX
) {
2127 if (remote_pads
[route
->source_pad
] != U32_MAX
&&
2128 remote_pads
[route
->source_pad
] != route
->sink_pad
) {
2130 "route %u attempts to mix %s streams\n",
2137 * V4L2_SUBDEV_ROUTING_NO_SINK_MULTIPLEXING: Pads on the sink
2138 * side can not do stream multiplexing, i.e. there can be only
2139 * a single stream in a sink pad.
2141 if (disallow
& V4L2_SUBDEV_ROUTING_NO_SINK_MULTIPLEXING
) {
2142 if (remote_pads
[route
->sink_pad
] != U32_MAX
) {
2144 "route %u attempts to multiplex on %s pad %u\n",
2145 i
, "sink", route
->sink_pad
);
2151 * V4L2_SUBDEV_ROUTING_NO_SOURCE_MULTIPLEXING: Pads on the
2152 * source side can not do stream multiplexing, i.e. there can
2153 * be only a single stream in a source pad.
2155 if (disallow
& V4L2_SUBDEV_ROUTING_NO_SOURCE_MULTIPLEXING
) {
2156 if (remote_pads
[route
->source_pad
] != U32_MAX
) {
2158 "route %u attempts to multiplex on %s pad %u\n",
2159 i
, "source", route
->source_pad
);
2165 remote_pads
[route
->sink_pad
] = route
->source_pad
;
2166 remote_pads
[route
->source_pad
] = route
->sink_pad
;
2169 for (j
= i
+ 1; j
< routing
->num_routes
; ++j
) {
2170 const struct v4l2_subdev_route
*r
= &routing
->routes
[j
];
2173 * V4L2_SUBDEV_ROUTING_NO_1_TO_N: No two routes can
2174 * originate from the same (sink) stream.
2176 if ((disallow
& V4L2_SUBDEV_ROUTING_NO_1_TO_N
) &&
2177 route
->sink_pad
== r
->sink_pad
&&
2178 route
->sink_stream
== r
->sink_stream
) {
2180 "routes %u and %u originate from same sink (%u/%u)\n",
2181 i
, j
, route
->sink_pad
,
2182 route
->sink_stream
);
2187 * V4L2_SUBDEV_ROUTING_NO_N_TO_1: No two routes can end
2188 * at the same (source) stream.
2190 if ((disallow
& V4L2_SUBDEV_ROUTING_NO_N_TO_1
) &&
2191 route
->source_pad
== r
->source_pad
&&
2192 route
->source_stream
== r
->source_stream
) {
2194 "routes %u and %u end at same source (%u/%u)\n",
2195 i
, j
, route
->source_pad
,
2196 route
->source_stream
);
2208 EXPORT_SYMBOL_GPL(v4l2_subdev_routing_validate
);
2210 static void v4l2_subdev_collect_streams(struct v4l2_subdev
*sd
,
2211 struct v4l2_subdev_state
*state
,
2212 u32 pad
, u64 streams_mask
,
2214 u64
*enabled_streams
)
2216 if (!(sd
->flags
& V4L2_SUBDEV_FL_STREAMS
)) {
2217 *found_streams
= BIT_ULL(0);
2219 (sd
->enabled_pads
& BIT_ULL(pad
)) ? BIT_ULL(0) : 0;
2224 *enabled_streams
= 0;
2226 for (unsigned int i
= 0; i
< state
->stream_configs
.num_configs
; ++i
) {
2227 const struct v4l2_subdev_stream_config
*cfg
=
2228 &state
->stream_configs
.configs
[i
];
2230 if (cfg
->pad
!= pad
|| !(streams_mask
& BIT_ULL(cfg
->stream
)))
2233 *found_streams
|= BIT_ULL(cfg
->stream
);
2235 *enabled_streams
|= BIT_ULL(cfg
->stream
);
2239 static void v4l2_subdev_set_streams_enabled(struct v4l2_subdev
*sd
,
2240 struct v4l2_subdev_state
*state
,
2241 u32 pad
, u64 streams_mask
,
2244 if (!(sd
->flags
& V4L2_SUBDEV_FL_STREAMS
)) {
2246 sd
->enabled_pads
|= BIT_ULL(pad
);
2248 sd
->enabled_pads
&= ~BIT_ULL(pad
);
2252 for (unsigned int i
= 0; i
< state
->stream_configs
.num_configs
; ++i
) {
2253 struct v4l2_subdev_stream_config
*cfg
=
2254 &state
->stream_configs
.configs
[i
];
2256 if (cfg
->pad
== pad
&& (streams_mask
& BIT_ULL(cfg
->stream
)))
2257 cfg
->enabled
= enabled
;
2261 int v4l2_subdev_enable_streams(struct v4l2_subdev
*sd
, u32 pad
,
2264 struct device
*dev
= sd
->entity
.graph_obj
.mdev
->dev
;
2265 struct v4l2_subdev_state
*state
;
2266 bool already_streaming
;
2267 u64 enabled_streams
;
2272 /* A few basic sanity checks first. */
2273 if (pad
>= sd
->entity
.num_pads
)
2276 if (!(sd
->entity
.pads
[pad
].flags
& MEDIA_PAD_FL_SOURCE
))
2280 * We use a 64-bit bitmask for tracking enabled pads, so only subdevices
2281 * with 64 pads or less can be supported.
2283 if (pad
>= sizeof(sd
->enabled_pads
) * BITS_PER_BYTE
)
2289 /* Fallback on .s_stream() if .enable_streams() isn't available. */
2290 use_s_stream
= !v4l2_subdev_has_op(sd
, pad
, enable_streams
);
2293 state
= v4l2_subdev_lock_and_get_active_state(sd
);
2298 * Verify that the requested streams exist and that they are not
2302 v4l2_subdev_collect_streams(sd
, state
, pad
, streams_mask
,
2303 &found_streams
, &enabled_streams
);
2305 if (found_streams
!= streams_mask
) {
2306 dev_dbg(dev
, "streams 0x%llx not found on %s:%u\n",
2307 streams_mask
& ~found_streams
, sd
->entity
.name
, pad
);
2312 if (enabled_streams
) {
2313 dev_dbg(dev
, "streams 0x%llx already enabled on %s:%u\n",
2314 enabled_streams
, sd
->entity
.name
, pad
);
2319 dev_dbg(dev
, "enable streams %u:%#llx\n", pad
, streams_mask
);
2321 already_streaming
= v4l2_subdev_is_streaming(sd
);
2323 if (!use_s_stream
) {
2324 /* Call the .enable_streams() operation. */
2325 ret
= v4l2_subdev_call(sd
, pad
, enable_streams
, state
, pad
,
2328 /* Start streaming when the first pad is enabled. */
2329 if (!already_streaming
)
2330 ret
= v4l2_subdev_call(sd
, video
, s_stream
, 1);
2336 dev_dbg(dev
, "enable streams %u:%#llx failed: %d\n", pad
,
2341 /* Mark the streams as enabled. */
2342 v4l2_subdev_set_streams_enabled(sd
, state
, pad
, streams_mask
, true);
2345 * TODO: When all the drivers have been changed to use
2346 * v4l2_subdev_enable_streams() and v4l2_subdev_disable_streams(),
2347 * instead of calling .s_stream() operation directly, we can remove
2348 * the privacy LED handling from call_s_stream() and do it here
2351 if (!use_s_stream
&& !already_streaming
)
2352 v4l2_subdev_enable_privacy_led(sd
);
2356 v4l2_subdev_unlock_state(state
);
2360 EXPORT_SYMBOL_GPL(v4l2_subdev_enable_streams
);
2362 int v4l2_subdev_disable_streams(struct v4l2_subdev
*sd
, u32 pad
,
2365 struct device
*dev
= sd
->entity
.graph_obj
.mdev
->dev
;
2366 struct v4l2_subdev_state
*state
;
2367 u64 enabled_streams
;
2372 /* A few basic sanity checks first. */
2373 if (pad
>= sd
->entity
.num_pads
)
2376 if (!(sd
->entity
.pads
[pad
].flags
& MEDIA_PAD_FL_SOURCE
))
2380 * We use a 64-bit bitmask for tracking enabled pads, so only subdevices
2381 * with 64 pads or less can be supported.
2383 if (pad
>= sizeof(sd
->enabled_pads
) * BITS_PER_BYTE
)
2389 /* Fallback on .s_stream() if .disable_streams() isn't available. */
2390 use_s_stream
= !v4l2_subdev_has_op(sd
, pad
, disable_streams
);
2393 state
= v4l2_subdev_lock_and_get_active_state(sd
);
2398 * Verify that the requested streams exist and that they are not
2402 v4l2_subdev_collect_streams(sd
, state
, pad
, streams_mask
,
2403 &found_streams
, &enabled_streams
);
2405 if (found_streams
!= streams_mask
) {
2406 dev_dbg(dev
, "streams 0x%llx not found on %s:%u\n",
2407 streams_mask
& ~found_streams
, sd
->entity
.name
, pad
);
2412 if (enabled_streams
!= streams_mask
) {
2413 dev_dbg(dev
, "streams 0x%llx already disabled on %s:%u\n",
2414 streams_mask
& ~enabled_streams
, sd
->entity
.name
, pad
);
2419 dev_dbg(dev
, "disable streams %u:%#llx\n", pad
, streams_mask
);
2421 if (!use_s_stream
) {
2422 /* Call the .disable_streams() operation. */
2423 ret
= v4l2_subdev_call(sd
, pad
, disable_streams
, state
, pad
,
2426 /* Stop streaming when the last streams are disabled. */
2428 if (!(sd
->enabled_pads
& ~BIT_ULL(pad
)))
2429 ret
= v4l2_subdev_call(sd
, video
, s_stream
, 0);
2435 dev_dbg(dev
, "disable streams %u:%#llx failed: %d\n", pad
,
2440 v4l2_subdev_set_streams_enabled(sd
, state
, pad
, streams_mask
, false);
2443 if (!use_s_stream
) {
2444 if (!v4l2_subdev_is_streaming(sd
))
2445 v4l2_subdev_disable_privacy_led(sd
);
2447 v4l2_subdev_unlock_state(state
);
2452 EXPORT_SYMBOL_GPL(v4l2_subdev_disable_streams
);
2454 int v4l2_subdev_s_stream_helper(struct v4l2_subdev
*sd
, int enable
)
2456 struct v4l2_subdev_state
*state
;
2457 struct v4l2_subdev_route
*route
;
2458 struct media_pad
*pad
;
2459 u64 source_mask
= 0;
2463 * Find the source pad. This helper is meant for subdevs that have a
2464 * single source pad, so failures shouldn't happen, but catch them
2465 * loudly nonetheless as they indicate a driver bug.
2467 media_entity_for_each_pad(&sd
->entity
, pad
) {
2468 if (pad
->flags
& MEDIA_PAD_FL_SOURCE
) {
2469 pad_index
= pad
->index
;
2474 if (WARN_ON(pad_index
== -1))
2477 if (sd
->flags
& V4L2_SUBDEV_FL_STREAMS
) {
2479 * As there's a single source pad, just collect all the source
2482 state
= v4l2_subdev_lock_and_get_active_state(sd
);
2484 for_each_active_route(&state
->routing
, route
)
2485 source_mask
|= BIT_ULL(route
->source_stream
);
2487 v4l2_subdev_unlock_state(state
);
2490 * For non-streams subdevices, there's a single implicit stream
2493 source_mask
= BIT_ULL(0);
2497 return v4l2_subdev_enable_streams(sd
, pad_index
, source_mask
);
2499 return v4l2_subdev_disable_streams(sd
, pad_index
, source_mask
);
2501 EXPORT_SYMBOL_GPL(v4l2_subdev_s_stream_helper
);
2503 #endif /* CONFIG_VIDEO_V4L2_SUBDEV_API */
2505 #endif /* CONFIG_MEDIA_CONTROLLER */
2507 void v4l2_subdev_init(struct v4l2_subdev
*sd
, const struct v4l2_subdev_ops
*ops
)
2509 INIT_LIST_HEAD(&sd
->list
);
2512 sd
->v4l2_dev
= NULL
;
2516 sd
->dev_priv
= NULL
;
2517 sd
->host_priv
= NULL
;
2518 sd
->privacy_led
= NULL
;
2519 INIT_LIST_HEAD(&sd
->async_subdev_endpoint_list
);
2520 #if defined(CONFIG_MEDIA_CONTROLLER)
2521 sd
->entity
.name
= sd
->name
;
2522 sd
->entity
.obj_type
= MEDIA_ENTITY_TYPE_V4L2_SUBDEV
;
2523 sd
->entity
.function
= MEDIA_ENT_F_V4L2_SUBDEV_UNKNOWN
;
2526 EXPORT_SYMBOL(v4l2_subdev_init
);
2528 void v4l2_subdev_notify_event(struct v4l2_subdev
*sd
,
2529 const struct v4l2_event
*ev
)
2531 v4l2_event_queue(sd
->devnode
, ev
);
2532 v4l2_subdev_notify(sd
, V4L2_DEVICE_NOTIFY_EVENT
, (void *)ev
);
2534 EXPORT_SYMBOL_GPL(v4l2_subdev_notify_event
);
2536 bool v4l2_subdev_is_streaming(struct v4l2_subdev
*sd
)
2538 struct v4l2_subdev_state
*state
;
2540 if (!v4l2_subdev_has_op(sd
, pad
, enable_streams
))
2541 return sd
->s_stream_enabled
;
2543 if (!(sd
->flags
& V4L2_SUBDEV_FL_STREAMS
))
2544 return !!sd
->enabled_pads
;
2546 state
= v4l2_subdev_get_locked_active_state(sd
);
2548 for (unsigned int i
= 0; i
< state
->stream_configs
.num_configs
; ++i
) {
2549 const struct v4l2_subdev_stream_config
*cfg
;
2551 cfg
= &state
->stream_configs
.configs
[i
];
2559 EXPORT_SYMBOL_GPL(v4l2_subdev_is_streaming
);
2561 int v4l2_subdev_get_privacy_led(struct v4l2_subdev
*sd
)
2563 #if IS_REACHABLE(CONFIG_LEDS_CLASS)
2564 sd
->privacy_led
= led_get(sd
->dev
, "privacy-led");
2565 if (IS_ERR(sd
->privacy_led
) && PTR_ERR(sd
->privacy_led
) != -ENOENT
)
2566 return dev_err_probe(sd
->dev
, PTR_ERR(sd
->privacy_led
),
2567 "getting privacy LED\n");
2569 if (!IS_ERR_OR_NULL(sd
->privacy_led
)) {
2570 mutex_lock(&sd
->privacy_led
->led_access
);
2571 led_sysfs_disable(sd
->privacy_led
);
2572 led_trigger_remove(sd
->privacy_led
);
2573 led_set_brightness(sd
->privacy_led
, 0);
2574 mutex_unlock(&sd
->privacy_led
->led_access
);
2579 EXPORT_SYMBOL_GPL(v4l2_subdev_get_privacy_led
);
2581 void v4l2_subdev_put_privacy_led(struct v4l2_subdev
*sd
)
2583 #if IS_REACHABLE(CONFIG_LEDS_CLASS)
2584 if (!IS_ERR_OR_NULL(sd
->privacy_led
)) {
2585 mutex_lock(&sd
->privacy_led
->led_access
);
2586 led_sysfs_enable(sd
->privacy_led
);
2587 mutex_unlock(&sd
->privacy_led
->led_access
);
2588 led_put(sd
->privacy_led
);
2592 EXPORT_SYMBOL_GPL(v4l2_subdev_put_privacy_led
);