Linux Media Controller development
 help / color / mirror / Atom feed
From: Loic Poulain <loic.poulain@oss.qualcomm.com>
To: Mauro Carvalho Chehab <mchehab@kernel.org>
Cc: linux-media@vger.kernel.org,
	gjorgji.rosikopulos@oss.qualcomm.com, bryan.odonoghue@linaro.org,
	Loic Poulain <loic.poulain@oss.qualcomm.com>
Subject: [PATCH] media: v4l2-event: Allow drivers to provide the event timestamp
Date: Wed, 02 Dec 2026 18:15:35 +0100	[thread overview]
Message-ID: <20261202-sof-ts-v1-1-5437ce4b3933@oss.qualcomm.com> (raw)

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 unset. 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@oss.qualcomm.com>
---
 drivers/media/v4l2-core/v4l2-event.c | 15 +++++++++++++--
 include/media/v4l2-event.h           |  8 ++++----
 2 files changed, 17 insertions(+), 6 deletions(-)

diff --git a/drivers/media/v4l2-core/v4l2-event.c b/drivers/media/v4l2-core/v4l2-event.c
index 17d3c53c0d93b3f5fe28db6e2243935d3c17ed38..c63891d7d80bca2ab2b278ee447bd6021dbf3db5 100644
--- a/drivers/media/v4l2-core/v4l2-event.c
+++ b/drivers/media/v4l2-core/v4l2-event.c
@@ -153,6 +153,17 @@ static void __v4l2_event_queue_fh(struct v4l2_fh *fh,
 	wake_up_all(&fh->wait);
 }
 
+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();
+}
+
 void v4l2_event_queue(struct video_device *vdev, const struct v4l2_event *ev)
 {
 	struct v4l2_fh *fh;
@@ -162,7 +173,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);
 
@@ -176,7 +187,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..9cf05cc2b72f355908ad4638113d3550286362f5 100644
--- a/include/media/v4l2-event.h
+++ b/include/media/v4l2-event.h
@@ -100,8 +100,8 @@ int v4l2_event_dequeue(struct v4l2_fh *fh, struct v4l2_event *event,
  * The event will be queued for all &struct v4l2_fh file handlers.
  *
  * .. 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.
+ *    The driver's responsibility is to fill in the type, the data fields
+ *    and optionally the timestamp. The other fields will be filled in by V4L2.
  */
 void v4l2_event_queue(struct video_device *vdev, const struct v4l2_event *ev);
 
@@ -115,8 +115,8 @@ void v4l2_event_queue(struct video_device *vdev, const struct v4l2_event *ev);
  * The event will be queued only for the specified &struct v4l2_fh file handler.
  *
  * .. 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.
+ *    The driver's responsibility is to fill in the type, the data fields
+ *    and optionally the timestamp. The other fields will be filled in by V4L2.
  */
 void v4l2_event_queue_fh(struct v4l2_fh *fh, const struct v4l2_event *ev);
 

---
base-commit: 799dd936553182df5770dde7f38d94c8337df75a
change-id: 20261202-sof-ts-1e86823dbb50

Best regards,
-- 
Loic Poulain <loic.poulain@oss.qualcomm.com>


                 reply	other threads:[~2026-12-02 17:15 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261202-sof-ts-v1-1-5437ce4b3933@oss.qualcomm.com \
    --to=loic.poulain@oss.qualcomm.com \
    --cc=bryan.odonoghue@linaro.org \
    --cc=gjorgji.rosikopulos@oss.qualcomm.com \
    --cc=linux-media@vger.kernel.org \
    --cc=mchehab@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox