public inbox for stable@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH 0/4] media: uvcvideo: Fixes for hw timestamping
@ 2026-03-23 13:10 Ricardo Ribalda
  2026-03-23 13:10 ` [PATCH 1/4] media: uvcvideo: Fix dev_sof filtering in hw timestamp Ricardo Ribalda
                   ` (3 more replies)
  0 siblings, 4 replies; 5+ messages in thread
From: Ricardo Ribalda @ 2026-03-23 13:10 UTC (permalink / raw)
  To: Laurent Pinchart, Hans de Goede, Mauro Carvalho Chehab,
	Tomasz Figa, Sergey Senozhatsky
  Cc: Yunke Cao, linux-media, linux-kernel, Ricardo Ribalda, stable

This series introduces fixes for the hardware timestamp calculations.

Signed-off-by: Ricardo Ribalda <ribalda@chromium.org>
---
Ricardo Ribalda (4):
      media: uvcvideo: Fix dev_sof filtering in hw timestamp
      media: uvcvideo: Use hw timestaming if the clock buffer is full
      media: uvcvideo: Relax the constrains for interpolating the hw clock
      media: uvcvideo: Do not add clock samples with small sof delta

 drivers/media/usb/uvc/uvc_video.c | 51 +++++++++++++++++++++++++++------------
 1 file changed, 35 insertions(+), 16 deletions(-)
---
base-commit: a7da7fb57f2a787412da1a62292a17fa00fbfbdf
change-id: 20260309-uvc-hwtimestamp-f25dc27f5711

Best regards,
-- 
Ricardo Ribalda <ribalda@chromium.org>


^ permalink raw reply	[flat|nested] 5+ messages in thread

* [PATCH 1/4] media: uvcvideo: Fix dev_sof filtering in hw timestamp
  2026-03-23 13:10 [PATCH 0/4] media: uvcvideo: Fixes for hw timestamping Ricardo Ribalda
@ 2026-03-23 13:10 ` Ricardo Ribalda
  2026-03-23 13:10 ` [PATCH 2/4] media: uvcvideo: Use hw timestaming if the clock buffer is full Ricardo Ribalda
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 5+ messages in thread
From: Ricardo Ribalda @ 2026-03-23 13:10 UTC (permalink / raw)
  To: Laurent Pinchart, Hans de Goede, Mauro Carvalho Chehab,
	Tomasz Figa, Sergey Senozhatsky
  Cc: Yunke Cao, linux-media, linux-kernel, Ricardo Ribalda, stable

To avoid filling the clock circular buffer with duplicated data we only
add it if the new value sof is different than the last added sof.

The issue is that we compare the unprocess sof with the processed sof.
If there is a sof_offset, or UVC_QUIRK_INVALID_DEVICE_SOF is enabled,
the comparison will not work as expected.

This patch moves the comparison to the right place.

Fixes: 141270bd95d4 ("media: uvcvideo: Refactor clock circular buffer")
Cc: stable@vger.kernel.org
Signed-off-by: Ricardo Ribalda <ribalda@chromium.org>
---
 drivers/media/usb/uvc/uvc_video.c | 19 ++++++++++---------
 1 file changed, 10 insertions(+), 9 deletions(-)

diff --git a/drivers/media/usb/uvc/uvc_video.c b/drivers/media/usb/uvc/uvc_video.c
index 40c76c051da2..6786ca38fe5e 100644
--- a/drivers/media/usb/uvc/uvc_video.c
+++ b/drivers/media/usb/uvc/uvc_video.c
@@ -583,16 +583,7 @@ uvc_video_clock_decode(struct uvc_streaming *stream, struct uvc_buffer *buf,
 	if (!has_scr)
 		return;
 
-	/*
-	 * To limit the amount of data, drop SCRs with an SOF identical to the
-	 * previous one. This filtering is also needed to support UVC 1.5, where
-	 * all the data packets of the same frame contains the same SOF. In that
-	 * case only the first one will match the host_sof.
-	 */
 	sample.dev_sof = get_unaligned_le16(&data[header_size - 2]);
-	if (sample.dev_sof == stream->clock.last_sof)
-		return;
-
 	sample.dev_stc = get_unaligned_le32(&data[header_size - 6]);
 
 	/*
@@ -664,6 +655,16 @@ uvc_video_clock_decode(struct uvc_streaming *stream, struct uvc_buffer *buf,
 	}
 
 	sample.dev_sof = (sample.dev_sof + stream->clock.sof_offset) & 2047;
+
+	/*
+	 * To limit the amount of data, drop SCRs with an SOF identical to the
+	 * previous one. This filtering is also needed to support UVC 1.5, where
+	 * all the data packets of the same frame contains the same SOF. In that
+	 * case only the first one will match the host_sof.
+	 */
+	if (sample.dev_sof == stream->clock.last_sof)
+		return;
+
 	uvc_video_clock_add_sample(&stream->clock, &sample);
 	stream->clock.last_sof = sample.dev_sof;
 }

-- 
2.53.0.959.g497ff81fa9-goog


^ permalink raw reply related	[flat|nested] 5+ messages in thread

* [PATCH 2/4] media: uvcvideo: Use hw timestaming if the clock buffer is full
  2026-03-23 13:10 [PATCH 0/4] media: uvcvideo: Fixes for hw timestamping Ricardo Ribalda
  2026-03-23 13:10 ` [PATCH 1/4] media: uvcvideo: Fix dev_sof filtering in hw timestamp Ricardo Ribalda
@ 2026-03-23 13:10 ` Ricardo Ribalda
  2026-03-23 13:10 ` [PATCH 3/4] media: uvcvideo: Relax the constrains for interpolating the hw clock Ricardo Ribalda
  2026-03-23 13:10 ` [PATCH 4/4] media: uvcvideo: Do not add clock samples with small sof delta Ricardo Ribalda
  3 siblings, 0 replies; 5+ messages in thread
From: Ricardo Ribalda @ 2026-03-23 13:10 UTC (permalink / raw)
  To: Laurent Pinchart, Hans de Goede, Mauro Carvalho Chehab,
	Tomasz Figa, Sergey Senozhatsky
  Cc: Yunke Cao, linux-media, linux-kernel, Ricardo Ribalda, stable

In some situations, even with a full clock buffer, it does not contain
250msec of data. This results in the driver jumping back from software
to hardware timestapsing creating a nasty artifact in the video.

If the clock buffer is full, use it to calculate the timestamp instead
of defaulting to software stamps, the reduced accuracy is less visible
than jumping from one timestamping mechanism to the other.

Fixes: 6243c83be6ee8 ("media: uvcvideo: Allow hw clock updates with buffers not full")
Cc: stable@vger.kernel.org
Signed-off-by: Ricardo Ribalda <ribalda@chromium.org>
---
 drivers/media/usb/uvc/uvc_video.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/drivers/media/usb/uvc/uvc_video.c b/drivers/media/usb/uvc/uvc_video.c
index 6786ca38fe5e..c7ebedb3450f 100644
--- a/drivers/media/usb/uvc/uvc_video.c
+++ b/drivers/media/usb/uvc/uvc_video.c
@@ -842,7 +842,7 @@ void uvc_video_clock_update(struct uvc_streaming *stream,
 	 * dev_sof runs at 1KHz, and we have a fixed point precision of
 	 * 16 bits.
 	 */
-	if ((y2 - y1) < ((1000 / 4) << 16))
+	if (clock->size != clock->count && (y2 - y1) < ((1000 / 4) << 16))
 		goto done;
 
 	y = (u64)(y2 - y1) * (1ULL << 31) + (u64)y1 * (u64)x2

-- 
2.53.0.959.g497ff81fa9-goog


^ permalink raw reply related	[flat|nested] 5+ messages in thread

* [PATCH 3/4] media: uvcvideo: Relax the constrains for interpolating the hw clock
  2026-03-23 13:10 [PATCH 0/4] media: uvcvideo: Fixes for hw timestamping Ricardo Ribalda
  2026-03-23 13:10 ` [PATCH 1/4] media: uvcvideo: Fix dev_sof filtering in hw timestamp Ricardo Ribalda
  2026-03-23 13:10 ` [PATCH 2/4] media: uvcvideo: Use hw timestaming if the clock buffer is full Ricardo Ribalda
@ 2026-03-23 13:10 ` Ricardo Ribalda
  2026-03-23 13:10 ` [PATCH 4/4] media: uvcvideo: Do not add clock samples with small sof delta Ricardo Ribalda
  3 siblings, 0 replies; 5+ messages in thread
From: Ricardo Ribalda @ 2026-03-23 13:10 UTC (permalink / raw)
  To: Laurent Pinchart, Hans de Goede, Mauro Carvalho Chehab,
	Tomasz Figa, Sergey Senozhatsky
  Cc: Yunke Cao, linux-media, linux-kernel, Ricardo Ribalda, stable

In the initial version we set the min value to 250msec. Looks like
100msec can also provide a good value.

Now that we are at it, refactor a bit the code to make it cleaner.

Fixes: 6243c83be6ee8 ("media: uvcvideo: Allow hw clock updates with buffers not full")
Cc: stable@vger.kernel.org
Signed-off-by: Ricardo Ribalda <ribalda@chromium.org>
---
 drivers/media/usb/uvc/uvc_video.c | 18 +++++++++++-------
 1 file changed, 11 insertions(+), 7 deletions(-)

diff --git a/drivers/media/usb/uvc/uvc_video.c b/drivers/media/usb/uvc/uvc_video.c
index c7ebedb3450f..dcbc0941ffe6 100644
--- a/drivers/media/usb/uvc/uvc_video.c
+++ b/drivers/media/usb/uvc/uvc_video.c
@@ -494,6 +494,13 @@ static int uvc_commit_video(struct uvc_streaming *stream,
  * Clocks and timestamps
  */
 
+/*
+ * The accuracy of the hardware timestamping depends on having enough data to
+ * interpolate between the different clock domains. This value is sof cycles,
+ * this is, milliseconds.
+ */
+#define MIN_HW_TIMESTAMP_DIFF 100
+
 static inline ktime_t uvc_video_get_time(void)
 {
 	if (uvc_clock_param == CLOCK_MONOTONIC)
@@ -834,15 +841,12 @@ void uvc_video_clock_update(struct uvc_streaming *stream,
 		y2 += 2048 << 16;
 
 	/*
-	 * Have at least 1/4 of a second of timestamps before we
-	 * try to do any calculation. Otherwise we do not have enough
-	 * precision. This value was determined by running Android CTS
-	 * on different devices.
+	 * Check that we have enough data to do the interpolation.
 	 *
-	 * dev_sof runs at 1KHz, and we have a fixed point precision of
-	 * 16 bits.
+	 * y1 and y2 are dev_sof with a fixed point precision of 16 bits.
 	 */
-	if (clock->size != clock->count && (y2 - y1) < ((1000 / 4) << 16))
+	if (clock->size != clock->count &&
+	    (y2 - y1) < (MIN_HW_TIMESTAMP_DIFF << 16))
 		goto done;
 
 	y = (u64)(y2 - y1) * (1ULL << 31) + (u64)y1 * (u64)x2

-- 
2.53.0.959.g497ff81fa9-goog


^ permalink raw reply related	[flat|nested] 5+ messages in thread

* [PATCH 4/4] media: uvcvideo: Do not add clock samples with small sof delta
  2026-03-23 13:10 [PATCH 0/4] media: uvcvideo: Fixes for hw timestamping Ricardo Ribalda
                   ` (2 preceding siblings ...)
  2026-03-23 13:10 ` [PATCH 3/4] media: uvcvideo: Relax the constrains for interpolating the hw clock Ricardo Ribalda
@ 2026-03-23 13:10 ` Ricardo Ribalda
  3 siblings, 0 replies; 5+ messages in thread
From: Ricardo Ribalda @ 2026-03-23 13:10 UTC (permalink / raw)
  To: Laurent Pinchart, Hans de Goede, Mauro Carvalho Chehab,
	Tomasz Figa, Sergey Senozhatsky
  Cc: Yunke Cao, linux-media, linux-kernel, Ricardo Ribalda, stable

Some UVC 1.1 cameras running in fast isochronous mode tend to spam the
USB host with a lot of empty packets. These packets contain clock
information and are added to the clock buffer but do not add any
accuracy to the calculation. In fact, it is quite the opposite, in our
calculations, only the first and the last timestamp is used, and we only
have 32 slots.

Ignore the samples that will produce less than MIN_HW_TIMESTAMP_DIFF
data.

Fixes: 141270bd95d4 ("media: uvcvideo: Refactor clock circular buffer")
Cc: stable@vger.kernel.org
Signed-off-by: Ricardo Ribalda <ribalda@chromium.org>
---
 drivers/media/usb/uvc/uvc_video.c | 18 ++++++++++++++++--
 1 file changed, 16 insertions(+), 2 deletions(-)

diff --git a/drivers/media/usb/uvc/uvc_video.c b/drivers/media/usb/uvc/uvc_video.c
index dcbc0941ffe6..e1a4e84d6841 100644
--- a/drivers/media/usb/uvc/uvc_video.c
+++ b/drivers/media/usb/uvc/uvc_video.c
@@ -544,6 +544,19 @@ static void uvc_video_clock_add_sample(struct uvc_clock *clock,
 	spin_unlock_irqrestore(&clock->lock, flags);
 }
 
+static inline u16 sof_diff(u16 a, u16 b)
+{
+	u32 aux;
+
+	a &= 2047;
+	b &= 2047;
+	if (a >= b)
+		return a - b;
+
+	aux = a + 2048;
+	return (u16)(aux - b);
+}
+
 static void
 uvc_video_clock_decode(struct uvc_streaming *stream, struct uvc_buffer *buf,
 		       const u8 *data, int len)
@@ -664,12 +677,13 @@ uvc_video_clock_decode(struct uvc_streaming *stream, struct uvc_buffer *buf,
 	sample.dev_sof = (sample.dev_sof + stream->clock.sof_offset) & 2047;
 
 	/*
-	 * To limit the amount of data, drop SCRs with an SOF identical to the
+	 * To limit the amount of data, drop SCRs with an SOF similar to the
 	 * previous one. This filtering is also needed to support UVC 1.5, where
 	 * all the data packets of the same frame contains the same SOF. In that
 	 * case only the first one will match the host_sof.
 	 */
-	if (sample.dev_sof == stream->clock.last_sof)
+	if (sof_diff(sample.dev_sof, stream->clock.last_sof) <=
+	    (MIN_HW_TIMESTAMP_DIFF / stream->clock.size))
 		return;
 
 	uvc_video_clock_add_sample(&stream->clock, &sample);

-- 
2.53.0.959.g497ff81fa9-goog


^ permalink raw reply related	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2026-03-23 13:10 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-03-23 13:10 [PATCH 0/4] media: uvcvideo: Fixes for hw timestamping Ricardo Ribalda
2026-03-23 13:10 ` [PATCH 1/4] media: uvcvideo: Fix dev_sof filtering in hw timestamp Ricardo Ribalda
2026-03-23 13:10 ` [PATCH 2/4] media: uvcvideo: Use hw timestaming if the clock buffer is full Ricardo Ribalda
2026-03-23 13:10 ` [PATCH 3/4] media: uvcvideo: Relax the constrains for interpolating the hw clock Ricardo Ribalda
2026-03-23 13:10 ` [PATCH 4/4] media: uvcvideo: Do not add clock samples with small sof delta Ricardo Ribalda

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox