Re: [PATCH v2 1/7] media: v4l2-event: Allow drivers to provide the event timestamp

From: Gjorgji Rosikopulos (Consultant)

Date: Wed Nov 25 2026 - 07:29:35 EST


Hello,

On 11/25/2026 12:19 PM, Loic Poulain wrote:
> The timestamp reported to userspace for a V4L2 event is taken when the
> event is queued, which for an event signalling a hardware occurrence is
> only as good as the latency between the two. A CSI decoder able to latch
> the start-of-frame against the system counter knows that time to within a
> few microseconds, whereas the interrupt handler that gets to queue the
> event can be hundreds of microseconds late and is not consistent about
> it.
>
> Take the timestamp from the event when the driver filled it in, falling
> back to the current time when the field is cleared. Drivers have had no
> reason to set it so far, and every in-tree caller either uses a
> designated initialiser or memsets the event, so the field reads as zero
> everywhere today.
>
> Signed-off-by: Loic Poulain <loic.poulain@xxxxxxxxxxxxxxxx>
> ---
> drivers/media/v4l2-core/v4l2-event.c | 20 ++++++++++++++++++--
> include/media/v4l2-event.h | 9 ++++++++-
> 2 files changed, 26 insertions(+), 3 deletions(-)
>
> diff --git a/drivers/media/v4l2-core/v4l2-event.c b/drivers/media/v4l2-core/v4l2-event.c
> index 9dd2aaa95a67406bda3a257325442888895ed088..a35646c3a5c89febeb072d691ffd7853d9af714a 100644
> --- a/drivers/media/v4l2-core/v4l2-event.c
> +++ b/drivers/media/v4l2-core/v4l2-event.c
> @@ -150,6 +150,22 @@ static void __v4l2_event_queue_fh(struct v4l2_fh *fh,
> wake_up_all(&fh->wait);
> }
>
> +/*
> + * Hardware able to timestamp the event itself reports that time in the event,
> + * which is more accurate than the moment the driver gets to queue it. Fall
> + * back to the current time for the drivers that leave the field cleared.
> + */
> +static u64 v4l2_event_ts(const struct v4l2_event *ev)
> +{
> + struct timespec64 ts = {
> + .tv_sec = ev->timestamp.tv_sec,
> + .tv_nsec = ev->timestamp.tv_nsec,
> + };
> + u64 ns = timespec64_to_ns(&ts);
> +
> + return ns ? ns : ktime_get_ns();
> +}

I think it makes sense to add an additional field for hardware-based timestamps.
Converting the timestamp to the monotonic time domain introduces precision loss,
so it's better to expose both to userspace and let userspace decide what to do with
them.

~Gjorgji

> +
> void v4l2_event_queue(struct video_device *vdev, const struct v4l2_event *ev)
> {
> struct v4l2_fh *fh;
> @@ -159,7 +175,7 @@ void v4l2_event_queue(struct video_device *vdev, const struct v4l2_event *ev)
> if (vdev == NULL)
> return;
>
> - ts = ktime_get_ns();
> + ts = v4l2_event_ts(ev);
>
> spin_lock_irqsave(&vdev->fh_lock, flags);
>
> @@ -173,7 +189,7 @@ EXPORT_SYMBOL_GPL(v4l2_event_queue);
> void v4l2_event_queue_fh(struct v4l2_fh *fh, const struct v4l2_event *ev)
> {
> unsigned long flags;
> - u64 ts = ktime_get_ns();
> + u64 ts = v4l2_event_ts(ev);
>
> spin_lock_irqsave(&fh->vdev->fh_lock, flags);
> __v4l2_event_queue_fh(fh, ev, ts);
> diff --git a/include/media/v4l2-event.h b/include/media/v4l2-event.h
> index 3a0e2588361cf451b3dbfc63dfab99550d13cf6a..076f788e0469ae4666c60344e5ea73a698352134 100644
> --- a/include/media/v4l2-event.h
> +++ b/include/media/v4l2-event.h
> @@ -102,6 +102,11 @@ int v4l2_event_dequeue(struct v4l2_fh *fh, struct v4l2_event *event,
> * .. note::
> * The driver's only responsibility is to fill in the type and the data
> * fields. The other fields will be filled in by V4L2.
> + *
> + * Drivers whose hardware timestamps the event more accurately than the
> + * moment the driver gets to handle it may also fill in the timestamp
> + * field, as a CLOCK_MONOTONIC time. It is left alone if non-zero, so such
> + * drivers must zero-initialize the whole &struct v4l2_event.
> */
> void v4l2_event_queue(struct video_device *vdev, const struct v4l2_event *ev);
>
> @@ -116,7 +121,9 @@ void v4l2_event_queue(struct video_device *vdev, const struct v4l2_event *ev);
> *
> * .. note::
> * The driver's only responsibility is to fill in the type and the data
> - * fields. The other fields will be filled in by V4L2.
> + * fields. The other fields will be filled in by V4L2. As for
> + * v4l2_event_queue(), a non-zero timestamp field is taken as the
> + * CLOCK_MONOTONIC time of the event.
> */
> void v4l2_event_queue_fh(struct v4l2_fh *fh, const struct v4l2_event *ev);
>
>