From nobody Sat Apr 11 20:29:17 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 00EA9C64EC4 for ; Fri, 10 Mar 2023 09:06:59 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231508AbjCJJG6 (ORCPT ); Fri, 10 Mar 2023 04:06:58 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41110 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229995AbjCJJGZ (ORCPT ); Fri, 10 Mar 2023 04:06:25 -0500 Received: from mail-ed1-x530.google.com (mail-ed1-x530.google.com [IPv6:2a00:1450:4864:20::530]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1DE6C1091E7 for ; Fri, 10 Mar 2023 01:02:20 -0800 (PST) Received: by mail-ed1-x530.google.com with SMTP id k10so17533501edk.13 for ; Fri, 10 Mar 2023 01:02:20 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1678438931; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:from:to:cc:subject:date:message-id :reply-to; bh=sM9VHBcEDRumQi7DFlwhwQNZqP1X9niavDP72C2S7Eg=; b=TVlNXxSG8XHLd9IHtxlECmOJzD6guBdiBHWFuA2oRcTnWkiHEwH/DhlRlB4u3twMm/ VQNL6Y3YXytpHhizGRely/Kkn9Kf29KV/ot84gRy6fd8cB8jZDrMTSLcCTEVbA2oEHLv n/TYbZ407vY6GEaEWyIUP+70D2BeyIk9wNii0= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1678438931; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=sM9VHBcEDRumQi7DFlwhwQNZqP1X9niavDP72C2S7Eg=; b=UbKixM9MjKzE8YlhuofSPhDhuxme44+vG79C+uWqMmv2vwwp7Tc6IpK48a0VyicTmZ 0cmlp7H3nrj34fo0U4JICVyc/0pS01fZflFYDGuai7gk/H2WyJhfJqmER1BiT4X4oPS9 82bQ52taIhlFvtGHTzGkPZyDYVVRsS1Q89sw8+uXKMt1t2+1XwP0vKz8fpCWlhtRuioy HOLqQnHoc+8Ziboaj/NwOt2r32rUgyzfKqoVqYmoQ8dFFsNlPPMk0R+kABiRQj8qU/n3 SwWM1BGhFBhgVxS3J5MAgR4FoE7ldgUXOk287tqAx/nEN67NP/hHr+jCy2+T2qnPe9PR eX3A== X-Gm-Message-State: AO0yUKWL/4Z4BPEN1Sxe1RvJ6xOw9Wk0zHaV8z3Vlzmf4bf15uXZBfzy 00NJxbQO4+H7eGPfGsYsLsH8sA== X-Google-Smtp-Source: AK7set9BK+XjIoADosdNMDa6cAcEn2PS1KMoGbx3MqO43xbv0IEReknN5FIEXyx5jot/PrEojx1TAQ== X-Received: by 2002:a17:906:dfca:b0:878:7471:6da7 with SMTP id jt10-20020a170906dfca00b0087874716da7mr26254638ejc.66.1678438931628; Fri, 10 Mar 2023 01:02:11 -0800 (PST) Received: from alco.roam.corp.google.com ([2620:0:1059:10:ef69:7ab6:87ac:99f]) by smtp.gmail.com with ESMTPSA id s5-20020a170906454500b008b23b22b062sm692931ejq.114.2023.03.10.01.02.10 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 10 Mar 2023 01:02:11 -0800 (PST) From: Ricardo Ribalda Date: Fri, 10 Mar 2023 10:01:28 +0100 Subject: [PATCH v7 1/6] media: uvcvideo: Fix underflow addressing on hw timestamp MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20220920-resend-hwtimestamp-v7-1-cf1d78bb8821@chromium.org> References: <20220920-resend-hwtimestamp-v7-0-cf1d78bb8821@chromium.org> In-Reply-To: <20220920-resend-hwtimestamp-v7-0-cf1d78bb8821@chromium.org> To: Laurent Pinchart , Mauro Carvalho Chehab Cc: Ricardo Ribalda , linux-kernel@vger.kernel.org, linux-media@vger.kernel.org, Sergey Senozhatsky , "hn.chen" X-Mailer: b4 0.11.0-dev-696ae X-Developer-Signature: v=1; a=openpgp-sha256; l=950; i=ribalda@chromium.org; h=from:subject:message-id; bh=Z9vWlYOlXeYS2ei5qnSybP3k1rYf5ivkf/cMeBsEyPU=; b=owEBbQKS/ZANAwAKAdE30T7POsSIAcsmYgBkCvIILFEZ+h9JBJEGf1HkEqI1CrPZiSpVEZYgh7Vw cvf169uJAjMEAAEKAB0WIQREDzjr+/4oCDLSsx7RN9E+zzrEiAUCZAryCAAKCRDRN9E+zzrEiBKlD/ 4kmB9H1te84VZ66FA71pBUtqzKWkX+f/iYsSMJBJ4Dk52qd2zBZNoj18Ybe8SlaSivaHRmWeYCAZsV 7ZEg6ClLwZnTrjaFSAbdniaVoyRSEx/03c90MUSiEPjiR+q2h2RWm7zNuNP4BU4SmMlapZaGTBdkWi hVISXkDwG9+cFGP6EW73PNBgYPiKiqBRtJIxzU6pmm3N0QNzjma9ysmQgP6Ac2iABDbBSrNz9R08iG qSmObMdGjjF9qoX+6xCZn9q7lfEIzBLX6NGaKqIyr0geKk2VmZHzhFSTSxyvz65Z64yHPr5Mqyg7fj W6jlRoBEWAJWjPiHbAfvw/dunprnh4BOoMToYz18AbBjdzYDtVnQ57qJn3lf7i2rjpKb8J+iEMyxCs a0ynjFPW89A5JycwNZzRNl5RQykgT/rFyQzz5lSjZIRIve3YioWPthxoIIf7Wo3cSg33NXGbq9VMNB HFc/JNx6GhIygLvVNOj9DSWs981QsOjomlkODEyzinkpOgEVNz1vn687axiilzriSeKOREHIgR6nf8 Io58irzLBEyzkm7AMDTaYRxe7SBWlTvzy9qwAoP/YmNYOiOYMrLGRgyGLEIysMEPWJChVdIJOHo42W qgqTkFExayDJBFCcIxsuy32KWL1oRgfaCel4l/kyr5oA43llpNHc6aQoEsjA== X-Developer-Key: i=ribalda@chromium.org; a=openpgp; fpr=9EC3BB66E2FC129A6F90B39556A0D81F9F782DA9 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org If head is 0, we will be addressing clock->samples[-1], which will result in undefined behaviour. Fixes: 66847ef013cc ("[media] uvcvideo: Add UVC timestamps support") Signed-off-by: Ricardo Ribalda Reviewed-by: Sergey Tested-by: HungNien Chen Tested-by: Sunplus --- 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 d4b023d4de7c..4ff4ab4471fe 100644 --- a/drivers/media/usb/uvc/uvc_video.c +++ b/drivers/media/usb/uvc/uvc_video.c @@ -732,7 +732,7 @@ void uvc_video_clock_update(struct uvc_streaming *strea= m, goto done; =20 first =3D &clock->samples[clock->head]; - last =3D &clock->samples[(clock->head - 1) % clock->size]; + last =3D &clock->samples[(clock->head - 1 + clock->size) % clock->size]; =20 /* First step, PTS to SOF conversion. */ delta_stc =3D buf->pts - (1UL << 31); --=20 2.40.0.rc1.284.g88254d51c5-goog-b4-0.11.0-dev-696ae From nobody Sat Apr 11 20:29:17 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 2D0D6C64EC4 for ; Fri, 10 Mar 2023 09:07:05 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231519AbjCJJHD (ORCPT ); Fri, 10 Mar 2023 04:07:03 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45230 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230519AbjCJJG3 (ORCPT ); Fri, 10 Mar 2023 04:06:29 -0500 Received: from mail-ed1-x52e.google.com (mail-ed1-x52e.google.com [IPv6:2a00:1450:4864:20::52e]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id CEE9710822B for ; Fri, 10 Mar 2023 01:02:23 -0800 (PST) Received: by mail-ed1-x52e.google.com with SMTP id j11so17665143edq.4 for ; Fri, 10 Mar 2023 01:02:23 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1678438932; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:from:to:cc:subject:date:message-id :reply-to; bh=Ks+I0iL9S/eJzdcQJQgtcbFNoYKHK8Hr97Z4+vCos3E=; b=UpQqQOkT1NsmAXYZfYiPuqaLhTysktOoQDcEi4v3ki6rszPwB7IOUMArrRQnua/XVs ABo1zDYVHqt4o/GGxMUj/GrtYrdGgYG/HA+ET6vNHiXF6UNuVyGoO0En6h1CaZvMs8Ln Ed8LbCjkL1OkI5PSh8mIO52PUAo9wFWDYkw2E= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1678438932; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=Ks+I0iL9S/eJzdcQJQgtcbFNoYKHK8Hr97Z4+vCos3E=; b=tcfvduu7BJ/LGxqy4QE29jzqHGKrZJ2nACB2ZIznYfjrSnx8taumloIIh0G/zGMi1Q 37Ng3r0oQifxpUxzaKUoRya3DF330pP5/vKMOpH76Vzt60auHCVSXbZZBiL9ryztvlCf I0Jc+VkW0kgoiEKrojTsoEw980bFg0iY99ddL01/TJ/XsZKTiy/tebqWlEEiGrQlEWP7 WuKf51roLVTjwtnR9zgamu4DZbZPa5WzmR8GUVCWlTMkMw4knr9xAb2eqkrntlcOQp4t 5gSUyW1bfxPWPoChacezstH4EphX/R4OSteI1tLw1sIoTtwz4MpvL4iAlwvMOUgUEkEs lawQ== X-Gm-Message-State: AO0yUKW0e6KXSBhna+nlpDoYvT8l1MtFX1zuRZlY+JvmeVzXx7G/EcSJ tFIAdjQRhM2a/CpRq0hYlyeKWQ== X-Google-Smtp-Source: AK7set9nmoPqTFfsxxNAx30lnjNZRP2fucdhekwAya9B+lfjNa4UHo4HZM18m6FsGof56Rxctx5fyQ== X-Received: by 2002:a17:907:9d17:b0:888:5d34:dc79 with SMTP id kt23-20020a1709079d1700b008885d34dc79mr28576821ejc.40.1678438932476; Fri, 10 Mar 2023 01:02:12 -0800 (PST) Received: from alco.roam.corp.google.com ([2620:0:1059:10:ef69:7ab6:87ac:99f]) by smtp.gmail.com with ESMTPSA id s5-20020a170906454500b008b23b22b062sm692931ejq.114.2023.03.10.01.02.11 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 10 Mar 2023 01:02:12 -0800 (PST) From: Ricardo Ribalda Date: Fri, 10 Mar 2023 10:01:29 +0100 Subject: [PATCH v7 2/6] media: uvcvideo: Ignore empty TS packets MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20220920-resend-hwtimestamp-v7-2-cf1d78bb8821@chromium.org> References: <20220920-resend-hwtimestamp-v7-0-cf1d78bb8821@chromium.org> In-Reply-To: <20220920-resend-hwtimestamp-v7-0-cf1d78bb8821@chromium.org> To: Laurent Pinchart , Mauro Carvalho Chehab Cc: Ricardo Ribalda , linux-kernel@vger.kernel.org, linux-media@vger.kernel.org, Sergey Senozhatsky , "hn.chen" X-Mailer: b4 0.11.0-dev-696ae X-Developer-Signature: v=1; a=openpgp-sha256; l=3922; i=ribalda@chromium.org; h=from:subject:message-id; bh=+e1rJhTRZYFtyKcR4/N8/nQKunH8/Dqv1jSdJGbbaL4=; b=owEBbQKS/ZANAwAKAdE30T7POsSIAcsmYgBkCvIJyM5E8SM1e6cOc0A6OINFuwFO6Mmq+lkzoSNt bNYna4WJAjMEAAEKAB0WIQREDzjr+/4oCDLSsx7RN9E+zzrEiAUCZAryCQAKCRDRN9E+zzrEiAEuD/ 9H0447j0gDhLx/5bRMVTMgJhFKnQ/L1rvPm37RR9ps7x9qgCO29kQj9kWsX56YMhVDnhkNkMTOhH5e 8uFtimI7mIw7XcFXvnRr4tvq0awvT8yACmY83rr9wdUVRhXPJSbxHDEJAG++weCFNfG0J4svFjLzC2 Uq84n9YMPtgP+Ht8BgASNe/2iFzHbn3p55EIsinmpGU+qfbbr+n3lL5pwAh2X0IYsw6jwZlGoKCWnN okJWFw+t77X0O3dvC/AQDkkN0Gcx13tuzssOiq031+IewyXMls5VBdiVjqXZ1iWqNzQD4Mu+3wNb6C ETqNy1ZRnmUzhjnA49QvCnP4CM5y9Hw5IEjvQQUP4M18+lnpJ1zFo9e69Z1wf/v4kZx1JPRW5BBpfv uh2b0lowHnSJzUGspUnf9noxAl0PghWhq6EU/MrJBEe4lcD1GnwrbjKsKpRVr6wRKwK4gJVpXLuCK1 Uvv6FErORhehJJNGYlWGWYPemfCRHsiF9AWr/AEGiWa9xbKeWzAlbGjr8/Er+IU/Cb8YMiFVYnPkeM syTM0zAeeagjMrUYRlGgQzChcUxRi2KnHFq2KM8dfhE2gD5S2YZGvEQkEz4jc2QefCdN0N93LKi30g z4yTE8B6SeabUihtJoyxVRUHM4QYj/rAin7/ZVDaXyKfNIy3ufvGGUT2lUJw== X-Developer-Key: i=ribalda@chromium.org; a=openpgp; fpr=9EC3BB66E2FC129A6F90B39556A0D81F9F782DA9 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Some SunplusIT cameras took a borderline interpretation of the UVC 1.5 standard, and fill the PTS and SCR fields with invalid data if the package does not contain data. "STC must be captured when the first video data of a video frame is put on the USB bus." Eg: buffer: 0xa7755c00 len 000012 header:0x8c stc 00000000 sof 0000 pts 00000000 buffer: 0xa7755c00 len 000012 header:0x8c stc 00000000 sof 0000 pts 00000000 buffer: 0xa7755c00 len 000668 header:0x8c stc 73779dba sof 070c pts 7376d37a This borderline/buggy interpretation has been implemented in a variety of devices, from directly SunplusIT and from other OEMs that rebrand SunplusIT products. So quirking based on VID:PID will be problematic. All the affected modules have the following extension unit: VideoControl Interface Descriptor: guidExtensionCode {82066163-7050-ab49-b8cc-b3855e8d221d} But the vendor plans to use that GUID in the future and fix the bug, this means that we should use heuristic to figure out the broken packets. This patch takes care of this. lsusb of one of the affected cameras: Bus 001 Device 003: ID 1bcf:2a01 Sunplus Innovation Technology Inc. Device Descriptor: bLength 18 bDescriptorType 1 bcdUSB 2.01 bDeviceClass 239 Miscellaneous Device bDeviceSubClass 2 ? bDeviceProtocol 1 Interface Association bMaxPacketSize0 64 idVendor 0x1bcf Sunplus Innovation Technology Inc. idProduct 0x2a01 bcdDevice 0.02 iManufacturer 1 SunplusIT Inc iProduct 2 HanChen Wise Camera iSerial 3 01.00.00 bNumConfigurations 1 Tested-by: HungNien Chen Reviewed-by: Sergey Senozhatsky Signed-off-by: Ricardo Ribalda Reviewed-by: Sergey Tested-by: Sunplus --- drivers/media/usb/uvc/uvc_video.c | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/drivers/media/usb/uvc/uvc_video.c b/drivers/media/usb/uvc/uvc_= video.c index 4ff4ab4471fe..1f416c494acc 100644 --- a/drivers/media/usb/uvc/uvc_video.c +++ b/drivers/media/usb/uvc/uvc_video.c @@ -478,6 +478,7 @@ uvc_video_clock_decode(struct uvc_streaming *stream, st= ruct uvc_buffer *buf, ktime_t time; u16 host_sof; u16 dev_sof; + u32 dev_stc; =20 switch (data[1] & (UVC_STREAM_PTS | UVC_STREAM_SCR)) { case UVC_STREAM_PTS | UVC_STREAM_SCR: @@ -526,6 +527,23 @@ uvc_video_clock_decode(struct uvc_streaming *stream, s= truct uvc_buffer *buf, if (dev_sof =3D=3D stream->clock.last_sof) return; =20 + dev_stc =3D get_unaligned_le32(&data[header_size - 6]); + + /* + * STC (Source Time Clock) is the clock used by the camera. The UVC 1.5 + * standard states that it "must be captured when the first video data + * of a video frame is put on the USB bus". + * Most of the vendors, clear the `UVC_STREAM_SCR` bit when the data is + * not valid, other vendors always set the `UVC_STREAM_SCR` bit and + * expect that the driver only samples the stc if there is data on the + * packet. + * Ignore all the hardware timestamp information if there is no data + * and stc and sof are zero. + */ + if (buf && buf->bytesused =3D=3D 0 && len =3D=3D header_size && + dev_stc =3D=3D 0 && dev_sof =3D=3D 0) + return; + stream->clock.last_sof =3D dev_sof; =20 host_sof =3D usb_get_current_frame_number(stream->dev->udev); @@ -564,7 +582,7 @@ uvc_video_clock_decode(struct uvc_streaming *stream, st= ruct uvc_buffer *buf, spin_lock_irqsave(&stream->clock.lock, flags); =20 sample =3D &stream->clock.samples[stream->clock.head]; - sample->dev_stc =3D get_unaligned_le32(&data[header_size - 6]); + sample->dev_stc =3D dev_stc; sample->dev_sof =3D dev_sof; sample->host_sof =3D host_sof; sample->host_time =3D time; --=20 2.40.0.rc1.284.g88254d51c5-goog-b4-0.11.0-dev-696ae From nobody Sat Apr 11 20:29:17 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 1DA26C6FD19 for ; Fri, 10 Mar 2023 09:07:27 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231531AbjCJJHZ (ORCPT ); Fri, 10 Mar 2023 04:07:25 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42132 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231438AbjCJJGh (ORCPT ); Fri, 10 Mar 2023 04:06:37 -0500 Received: from mail-ed1-x531.google.com (mail-ed1-x531.google.com [IPv6:2a00:1450:4864:20::531]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 3BBB0112581 for ; Fri, 10 Mar 2023 01:02:33 -0800 (PST) Received: by mail-ed1-x531.google.com with SMTP id ec29so17630377edb.6 for ; Fri, 10 Mar 2023 01:02:33 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1678438933; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:from:to:cc:subject:date:message-id :reply-to; bh=a5a+6HYmuNF0NcA/SU/mW3fBSurTLFx+qA7YB8EkD48=; b=SQ1tcv8Bj4AQO0mGVa6QGvn2qt/K+uVY9E1vSWP5QhD9LY+6LcKGm49RSuz69L7GUf nA1dM3ZL0w7NL8NPkPLt+rc54BLdvrUddhb8LUdDrSgLc/p2PJt1RV+y4yWb0adhD1YD AvtdRHoFjzyiAsSUnbg1qaVZBv+c3j9hRiVAA= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1678438933; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=a5a+6HYmuNF0NcA/SU/mW3fBSurTLFx+qA7YB8EkD48=; b=L6YC0FmAdyIKgvXHjaeMpEOivAT7+cGo8hX9JnPc05tL1FzzylEC5VouhQ4lJM6Dck 6Q7NyvwOgKIrjunES2tmVT0osGeGJkOb4/ELwM5R+v91JLndwvvEZq0h/l1TqudlOi6z Gyz8IPovsTTupmuLZ8FT0hEybRpLCtgG0JTPK7at/UuPRgZE7o3vXGWVccKHGjNd9XRZ GWJzlpzgCF79pH8kqXeCM81avfcQo0TKx/WoM1utkMHpsKI2BwFcpCRHMCqhrnvJWw3e 0xg+9vyq4v9xxuMMV6k6HdzpUmaOS1Gss+Xv1J8TzN66cxM/6ODD7YAld4vSqTKjT4v+ vlIg== X-Gm-Message-State: AO0yUKVZSMcIHk0A4xWkQvn3CSEinniQYVIbDQoKrUTiqaQNZH+5tlrk ozgRJPxWzXFlSD99Q3kl2Kaelob+aXpTZPsgyvw= X-Google-Smtp-Source: AK7set8GtyVQ8FJebMR3Ilm8/Wev2wx0QAMhQLWGBQV7Mp7MNFA3TKHHIgQxtpym/6HzMncPQ9yEFQ== X-Received: by 2002:a17:907:c282:b0:884:930:b017 with SMTP id tk2-20020a170907c28200b008840930b017mr30309066ejc.60.1678438933400; Fri, 10 Mar 2023 01:02:13 -0800 (PST) Received: from alco.roam.corp.google.com ([2620:0:1059:10:ef69:7ab6:87ac:99f]) by smtp.gmail.com with ESMTPSA id s5-20020a170906454500b008b23b22b062sm692931ejq.114.2023.03.10.01.02.12 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 10 Mar 2023 01:02:13 -0800 (PST) From: Ricardo Ribalda Date: Fri, 10 Mar 2023 10:01:30 +0100 Subject: [PATCH v7 3/6] media: uvcvideo: Quirk for invalid dev_sof in Logitech C922 MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20220920-resend-hwtimestamp-v7-3-cf1d78bb8821@chromium.org> References: <20220920-resend-hwtimestamp-v7-0-cf1d78bb8821@chromium.org> In-Reply-To: <20220920-resend-hwtimestamp-v7-0-cf1d78bb8821@chromium.org> To: Laurent Pinchart , Mauro Carvalho Chehab Cc: Ricardo Ribalda , linux-kernel@vger.kernel.org, linux-media@vger.kernel.org, Sergey Senozhatsky , "hn.chen" X-Mailer: b4 0.11.0-dev-696ae X-Developer-Signature: v=1; a=openpgp-sha256; l=5234; i=ribalda@chromium.org; h=from:subject:message-id; bh=5jcOY5wQypEMPaIWclRrjn20dOHGC/hJF48i1+E0AwQ=; b=owEBbQKS/ZANAwAKAdE30T7POsSIAcsmYgBkCvIKmv5rljVl3OMQWkOujH04PqNlN8jGCzwBSCZQ iAh73ACJAjMEAAEKAB0WIQREDzjr+/4oCDLSsx7RN9E+zzrEiAUCZAryCgAKCRDRN9E+zzrEiBTxD/ 9QAx238IgygZp7TSwJ2ULiXyuQrtrOWICfy1mFA5zA/pwm++GKNo22ihXa5glEcicoxkwbuOoqeUMg 0ot+/kWuOeIEG2f2BTOyJ9F7Bc21hcMW+ci5H2MLE9SI8uS7SHVz6jLWsF00hzDQA30UN2c6mErgLG YrJxhaM3FomaxBqJ/xn6tAgnhA0da0DiG82zXbNhGF8zUSamBNhLNFbTE/LR7P5alg2Sr5eilBpbgG UeAb7b5WRs67S4vlaVkZMxdEM2qt3TyLYVh2MOv7XMJGgTuzFbp+fP2YFHPt3akjO82ItDEeiaVdDS tAHcxJbw04e7FbJFdZrd0BH0GTR2scdjwPLdupgceRP7ZJqEQvI3uJteeLUti35eoKKxh96HNf3q/T I3eJzxvM8SvMFZC1Vx6/PZqYg9IM2PHLCRSXbYNeIpXTyVO2tuu7FfVJHWPzvSVmWisMC/mwSszc0J 6Y7It2BhVlQDRpWuzvyuwEfbe6nQ4Blt5vEbt+9vwjiY5AQYKPKBURKSSZl8fqMAedWLFL2bzc2eX7 lJHgEoodhYk9KHmax4xaie4RXPJHVW88uhLQOxgGvTxmQ77D/8Nd9YsFm8qohRfa7FQg19pemPZi+k 17n+D5Gu4a5H4Vq2Z8h4zV/kxzM2dYpfueT0ZgG2z5lpMQ6gQKpW92qdsUCA== X-Developer-Key: i=ribalda@chromium.org; a=openpgp; fpr=9EC3BB66E2FC129A6F90B39556A0D81F9F782DA9 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Logitech C922 internal SOF does not increases at a stable rate of 1kHz. This causes that the device_sof and the host_sof run at different rates, breaking the clock domain conversion algorithm. Eg: 30 (6) [-] none 30 614400 B 21.245557 21.395214 34.133 fps ts mono/SoE 31 (7) [-] none 31 614400 B 21.275327 21.427246 33.591 fps ts mono/SoE 32 (0) [-] none 32 614400 B 21.304739 21.459256 34.000 fps ts mono/SoE 33 (1) [-] none 33 614400 B 21.334324 21.495274 33.801 fps ts mono/SoE * 34 (2) [-] none 34 614400 B 21.529237 21.527297 5.130 fps ts mono/SoE * 35 (3) [-] none 35 614400 B 21.649416 21.559306 8.321 fps ts mono/SoE 36 (4) [-] none 36 614400 B 21.678789 21.595320 34.045 fps ts mono/SoE ... 99 (3) [-] none 99 614400 B 23.542226 23.696352 33.541 fps ts mono/SoE 100 (4) [-] none 100 614400 B 23.571578 23.728404 34.069 fps ts mono/SoE 101 (5) [-] none 101 614400 B 23.601425 23.760420 33.504 fps ts mono/SoE * 102 (6) [-] none 102 614400 B 23.798324 23.796428 5.079 fps ts mono/SoE * 103 (7) [-] none 103 614400 B 23.916271 23.828450 8.478 fps ts mono/SoE 104 (0) [-] none 104 614400 B 23.945720 23.860479 33.957 fps ts mono/SoE Instead of disabling completely the hardware timestamping for such hardware we take the assumption that the packet handling jitter is under 2ms and use the host_sof as dev_sof. We can think of the UVC hardware clock as a system with a coarse clock (the SOF) and a fine clock (the PTS). The coarse clock can be replaced with a clock on the same frequency, if the jitter of such clock is smaller than its sampling rate. That way we can save some of the precision of the fine clock. To probe this point we have run three experiments on the Logitech C922. On that experiment we run the camera at 33fps and we analyse the difference in msec between a frame and its predecessor. If we display the histogram of that value, a thinner histogram will mean a better meassurement. The results for: - original hw timestamp: https://ibb.co/D1HJJ4x - pure software timestamp: https://ibb.co/QC9MgVK - modified hw timestamp: https://ibb.co/8s9dBdk This bug in the camera firmware has been confirmed by the vendor. lsusb -v Bus 001 Device 044: ID 046d:085c Logitech, Inc. C922 Pro Stream Webcam Device Descriptor: bLength 18 bDescriptorType 1 bcdUSB 2.00 bDeviceClass 239 Miscellaneous Device bDeviceSubClass 2 bDeviceProtocol 1 Interface Association bMaxPacketSize0 64 idVendor 0x046d Logitech, Inc. idProduct 0x085c C922 Pro Stream Webcam bcdDevice 0.16 iManufacturer 0 iProduct 2 C922 Pro Stream Webcam iSerial 1 80B912DF bNumConfigurations 1 Reviewed-by: Sergey Senozhatsky Signed-off-by: Ricardo Ribalda Reviewed-by: Sergey Tested-by: HungNien Chen Tested-by: Sunplus --- drivers/media/usb/uvc/uvc_driver.c | 9 +++++++++ drivers/media/usb/uvc/uvc_video.c | 8 ++++++++ drivers/media/usb/uvc/uvcvideo.h | 1 + 3 files changed, 18 insertions(+) diff --git a/drivers/media/usb/uvc/uvc_driver.c b/drivers/media/usb/uvc/uvc= _driver.c index 7aefa76a42b3..678a5736c9df 100644 --- a/drivers/media/usb/uvc/uvc_driver.c +++ b/drivers/media/usb/uvc/uvc_driver.c @@ -2549,6 +2549,15 @@ static const struct usb_device_id uvc_ids[] =3D { .bInterfaceSubClass =3D 1, .bInterfaceProtocol =3D 0, .driver_info =3D UVC_INFO_QUIRK(UVC_QUIRK_RESTORE_CTRLS_ON_INIT) }, + /* Logitech HD Pro Webcam C922 */ + { .match_flags =3D USB_DEVICE_ID_MATCH_DEVICE + | USB_DEVICE_ID_MATCH_INT_INFO, + .idVendor =3D 0x046d, + .idProduct =3D 0x085c, + .bInterfaceClass =3D USB_CLASS_VIDEO, + .bInterfaceSubClass =3D 1, + .bInterfaceProtocol =3D 0, + .driver_info =3D UVC_INFO_QUIRK(UVC_QUIRK_INVALID_DEVICE_SOF) }, /* Chicony CNF7129 (Asus EEE 100HE) */ { .match_flags =3D USB_DEVICE_ID_MATCH_DEVICE | USB_DEVICE_ID_MATCH_INT_INFO, diff --git a/drivers/media/usb/uvc/uvc_video.c b/drivers/media/usb/uvc/uvc_= video.c index 1f416c494acc..4d566edb73e7 100644 --- a/drivers/media/usb/uvc/uvc_video.c +++ b/drivers/media/usb/uvc/uvc_video.c @@ -547,6 +547,14 @@ uvc_video_clock_decode(struct uvc_streaming *stream, s= truct uvc_buffer *buf, stream->clock.last_sof =3D dev_sof; =20 host_sof =3D usb_get_current_frame_number(stream->dev->udev); + + /* + * On some devices, like the Logitech C922, the device SOF does not run + * at a stable rate of 1kHz. For those devices use the host SOF instead. + */ + if (stream->dev->quirks & UVC_QUIRK_INVALID_DEVICE_SOF) + dev_sof =3D host_sof; + time =3D uvc_video_get_time(); =20 /* diff --git a/drivers/media/usb/uvc/uvcvideo.h b/drivers/media/usb/uvc/uvcvi= deo.h index 9a596c8d894a..07b2fdb80adf 100644 --- a/drivers/media/usb/uvc/uvcvideo.h +++ b/drivers/media/usb/uvc/uvcvideo.h @@ -73,6 +73,7 @@ #define UVC_QUIRK_FORCE_Y8 0x00000800 #define UVC_QUIRK_FORCE_BPP 0x00001000 #define UVC_QUIRK_WAKE_AUTOSUSPEND 0x00002000 +#define UVC_QUIRK_INVALID_DEVICE_SOF 0x00004000 =20 /* Format flags */ #define UVC_FMT_FLAG_COMPRESSED 0x00000001 --=20 2.40.0.rc1.284.g88254d51c5-goog-b4-0.11.0-dev-696ae From nobody Sat Apr 11 20:29:17 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 1082AC64EC4 for ; Fri, 10 Mar 2023 09:07:38 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231540AbjCJJHh (ORCPT ); Fri, 10 Mar 2023 04:07:37 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40048 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230098AbjCJJGk (ORCPT ); Fri, 10 Mar 2023 04:06:40 -0500 Received: from mail-ed1-x52b.google.com (mail-ed1-x52b.google.com [IPv6:2a00:1450:4864:20::52b]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 3AF0E7DF81 for ; Fri, 10 Mar 2023 01:02:35 -0800 (PST) Received: by mail-ed1-x52b.google.com with SMTP id ec29so17630540edb.6 for ; Fri, 10 Mar 2023 01:02:35 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1678438934; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:from:to:cc:subject:date:message-id :reply-to; bh=RN9dk0TN64Mo7WHVhpVGbABaRx7Q/q6Pwknupu3adDk=; b=auahc3+pJeuaf50PijN97Rc1bShULdg8glH6J/F2ecGmAk6HjZ2P6FY++By0j9Wzpy nzGKYmfnG32/bfAFFgwu0GVMYVf50UTIlIs6CrBFKzbRse2c1VFHSkaL9ewdJ0O6xAc+ ws4F4we3XAVtEiiqgXTiluJxDHoCiZQwoYrGc= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1678438934; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=RN9dk0TN64Mo7WHVhpVGbABaRx7Q/q6Pwknupu3adDk=; b=w+asR4fAr4gSvuZMVGlOR5D/2OWNxZxqKKeH3bfzU/XullmdQSNN/iPEwDlLNVai3s TJbztDiGIVWwSw12NP+tOkDNrHGGo1y5dH7Bz9FQB25iKll+SCIYJ5/cAF8YYfXd2NNq iF6g3KTt16Oi5dZplWHBQXWZk5F1z0jRPp8JjMt392llJBa+G5OcdwT/cRdKFqDXjcmV p68HTvJRgpQKkxMDXpSaZs7C3GElJRIQVH1663z9kUTmOFGiL6WVxmp1sETqQo3XA6aI 0G+IA6PHBH6VIbGniEmRgECaH6H83LJ3JuxiNPo292rTylM1Y+2oN45x7vbCFXO0NWZV AvRg== X-Gm-Message-State: AO0yUKXk1kRUQS9On1p/1CWWrdppd+nV+iDgsQeorjXXXVrO8cCvrh3n 1aX4Mb7Pi8qtZRgVoV1QO9V2MQ== X-Google-Smtp-Source: AK7set+87Q5e1RSPYfHWblm0WfYpLHROhntrXBZh3/riZlig+Bxp5XRgrD9ZYFOyZDJhWTg/FOJzgg== X-Received: by 2002:a17:907:a808:b0:8aa:c35b:a34b with SMTP id vo8-20020a170907a80800b008aac35ba34bmr25771623ejc.20.1678438934536; Fri, 10 Mar 2023 01:02:14 -0800 (PST) Received: from alco.roam.corp.google.com ([2620:0:1059:10:ef69:7ab6:87ac:99f]) by smtp.gmail.com with ESMTPSA id s5-20020a170906454500b008b23b22b062sm692931ejq.114.2023.03.10.01.02.13 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 10 Mar 2023 01:02:14 -0800 (PST) From: Ricardo Ribalda Date: Fri, 10 Mar 2023 10:01:31 +0100 Subject: [PATCH v7 4/6] media: uvcvideo: Allow hw clock updates with buffers not full MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20220920-resend-hwtimestamp-v7-4-cf1d78bb8821@chromium.org> References: <20220920-resend-hwtimestamp-v7-0-cf1d78bb8821@chromium.org> In-Reply-To: <20220920-resend-hwtimestamp-v7-0-cf1d78bb8821@chromium.org> To: Laurent Pinchart , Mauro Carvalho Chehab Cc: Ricardo Ribalda , linux-kernel@vger.kernel.org, linux-media@vger.kernel.org, Sergey Senozhatsky , "hn.chen" X-Mailer: b4 0.11.0-dev-696ae X-Developer-Signature: v=1; a=openpgp-sha256; l=1966; i=ribalda@chromium.org; h=from:subject:message-id; bh=Ka10ItjemOVlrqrWVbbdhzTBAJcpymfvLrHEyyU17oM=; b=owEBbQKS/ZANAwAKAdE30T7POsSIAcsmYgBkCvIMxQ6NquFZoQQOCgrRDaCRelepwRKDHcV6hDD7 +ALRcGmJAjMEAAEKAB0WIQREDzjr+/4oCDLSsx7RN9E+zzrEiAUCZAryDAAKCRDRN9E+zzrEiBQUEA CJkPTC56XQ5o/bYlLjZwg+MwUglsKtjo3UStrY7DZ82nwL3w0vF6A8FBKOODmvShxR17gikEdhhgi+ 4E5NUybANa4TrbHQ5T+dUmZFRvvk0kRLUkGcl6s4VDDNmD7a9FDmMhgOjkuK6VV6/f+XjhZVj2Dx3V x+8Gb5HcGdpUT3rYIzJPW8ASyVC6o9tUz+VQiJt8EyO0RAKNCeJVwxXsdvZ7NxyX5qEQNVZ/IO27bV FDxPm7w/5eAF5gd7CbJRZ/y7TCWRse6xU0bxM53OfJnXbBFOSLe8aYaXoyYFo828PTIYnSD+SH4NyV SSVVQ+dTW2k0918lsL8Fm6OvFCpeM1hQALewDDWE5jG70NWY2yrb0H+6FNhX7Zu5T2sVQztFCZKSRx j+c0QMLZ7EnSrHsDTKvLn8kP8kDDPTZw5s30PPoxfQ+qA29/83RUbueaAryTJF06kMPYz9DuFiqFBc yqdxdFYvj7yWgiogpjWZJzcYzYcsZxHkSrnQx8ZLahAQmbKb1vU0ArOeE2S+wxFs4sNT0a4a+pvGaP ItfdV7jSgdGo6mIGf3mFvL+FUXsTgrRPF768VzNpdXp3WDc4+7Sn2oVI0sfC+lgYFBZrf9hg9IylZ0 I2t8DzZMMnzLKQ6UWbazGtMJLPwE2xnE4txXjYaLfqrSqVI/NuDGzCS5heZg== X-Developer-Key: i=ribalda@chromium.org; a=openpgp; fpr=9EC3BB66E2FC129A6F90B39556A0D81F9F782DA9 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org With UVC 1.5 we get as little as one clock sample per frame. Which means that it takes 32 frames to move from the software timestamp to the hardware timestamp method. This results in abrupt changes in the timestamping after 32 frames (~1 second), resulting in noticeable artifacts when used for encoding. With this patch we modify the update algorithm to work with whatever amount of values are available. Tested-by: HungNien Chen Signed-off-by: Ricardo Ribalda Reviewed-by: Sergey Tested-by: Sunplus --- drivers/media/usb/uvc/uvc_video.c | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/drivers/media/usb/uvc/uvc_video.c b/drivers/media/usb/uvc/uvc_= video.c index 4d566edb73e7..6d0243ea0e07 100644 --- a/drivers/media/usb/uvc/uvc_video.c +++ b/drivers/media/usb/uvc/uvc_video.c @@ -754,10 +754,10 @@ void uvc_video_clock_update(struct uvc_streaming *str= eam, =20 spin_lock_irqsave(&clock->lock, flags); =20 - if (clock->count < clock->size) + if (clock->count < 2) goto done; =20 - first =3D &clock->samples[clock->head]; + first =3D &clock->samples[(clock->head - clock->count + clock->size) % cl= ock->size]; last =3D &clock->samples[(clock->head - 1 + clock->size) % clock->size]; =20 /* First step, PTS to SOF conversion. */ @@ -772,6 +772,17 @@ void uvc_video_clock_update(struct uvc_streaming *stre= am, if (y2 < y1) y2 +=3D 2048 << 16; =20 + /* + * 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. + * Dev_sof runs at 1KHz, and we have a fixed point precision of + * 16 bits. + */ + if ((y2 - y1) < ( (1000 / 4) << 16)) + goto done; + y =3D (u64)(y2 - y1) * (1ULL << 31) + (u64)y1 * (u64)x2 - (u64)y2 * (u64)x1; y =3D div_u64(y, x2 - x1); --=20 2.40.0.rc1.284.g88254d51c5-goog-b4-0.11.0-dev-696ae From nobody Sat Apr 11 20:29:17 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id A59B0C64EC4 for ; Fri, 10 Mar 2023 09:07:44 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231548AbjCJJHm (ORCPT ); Fri, 10 Mar 2023 04:07:42 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40206 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229683AbjCJJGn (ORCPT ); Fri, 10 Mar 2023 04:06:43 -0500 Received: from mail-ed1-x530.google.com (mail-ed1-x530.google.com [IPv6:2a00:1450:4864:20::530]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 4F1A911258F for ; Fri, 10 Mar 2023 01:02:40 -0800 (PST) Received: by mail-ed1-x530.google.com with SMTP id i34so17611708eda.7 for ; Fri, 10 Mar 2023 01:02:40 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1678438935; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:from:to:cc:subject:date:message-id :reply-to; bh=1nm96Wrt6gyEIi1trIE8ggK/zsqJmxHOVJaDLG8Q4K4=; b=dfDNrC0ZLZtziQjxrQ827AzIfRrdg85nRDa5OZ3OAtO+bStH7Izi+nC02g8UG+RUd+ OYTDfM0WOemGp3oVfnjmrLoF/rXcjz6wT7qOZY9AkUr2B6ze7MYeA/xSBXUdN+JC2d9w bNHExn7H8B8MzdFqEipTSicJQMh1tgprtlhSA= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1678438935; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=1nm96Wrt6gyEIi1trIE8ggK/zsqJmxHOVJaDLG8Q4K4=; b=BhSiapVcfTH1oGZAGZqXfk6G3l6QAsIYRZYngyN0fJqZ2yXZD2uXnA/A4WvsX945IU tH5Sd3iZsK3GOBDG7xbAut0rpY4Y+Ou5/Kese+pOOhRVEeuqT2N8sLz0Rhe82B5JIgMc V+CmgVtZx33ioE/9Mzx4l/H7O6CJaYuLECS+obIA79b8PF/imqGjuNaZ1as/VGlqqiiY EJZlQ3BC8GjlA78eBqvu2YD88tg4r/pskVMMpI6ips8sd5lEJl8+MyXLqu3dPw88iKjO m8TC4cOjmd4BxmxRSgNqQRhfVCmtxt8P2bwHpGtczB76LLtKtZq8UzK8VJd7OhMMSa81 B20Q== X-Gm-Message-State: AO0yUKUj950kgnssd8p3lW0DdWDVSC0baDCZEtpXVFzQcauYcBnlVpvv 5r8QMpmglJgO2pA2/qppyqDfDA== X-Google-Smtp-Source: AK7set8uBJUWEdjTJ8acxq8/GZ0DAAdqZ0/iRxCNqAQ28wKNp1xWI8Xihg28oO14JL6zkHe+c262zA== X-Received: by 2002:a17:906:66cd:b0:8e8:602f:847a with SMTP id k13-20020a17090666cd00b008e8602f847amr23072180ejp.24.1678438935445; Fri, 10 Mar 2023 01:02:15 -0800 (PST) Received: from alco.roam.corp.google.com ([2620:0:1059:10:ef69:7ab6:87ac:99f]) by smtp.gmail.com with ESMTPSA id s5-20020a170906454500b008b23b22b062sm692931ejq.114.2023.03.10.01.02.14 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 10 Mar 2023 01:02:15 -0800 (PST) From: Ricardo Ribalda Date: Fri, 10 Mar 2023 10:01:32 +0100 Subject: [PATCH v7 5/6] media: uvcvideo: Refactor clock circular buffer MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20220920-resend-hwtimestamp-v7-5-cf1d78bb8821@chromium.org> References: <20220920-resend-hwtimestamp-v7-0-cf1d78bb8821@chromium.org> In-Reply-To: <20220920-resend-hwtimestamp-v7-0-cf1d78bb8821@chromium.org> To: Laurent Pinchart , Mauro Carvalho Chehab Cc: Ricardo Ribalda , linux-kernel@vger.kernel.org, linux-media@vger.kernel.org, Sergey Senozhatsky , "hn.chen" X-Mailer: b4 0.11.0-dev-696ae X-Developer-Signature: v=1; a=openpgp-sha256; l=6685; i=ribalda@chromium.org; h=from:subject:message-id; bh=K1XWiZZVgbVikxQUf2acjH8pNqXvxOd5rL4C6h3qVQI=; b=owEBbQKS/ZANAwAKAdE30T7POsSIAcsmYgBkCvINr+ZwSCAqRUItGprR4yFuX8Jm0ErN3ZjNeUt+ YTHQm4SJAjMEAAEKAB0WIQREDzjr+/4oCDLSsx7RN9E+zzrEiAUCZAryDQAKCRDRN9E+zzrEiGh+D/ 4n3tTAxb4vRF+tYvF0MSi562VBo4IhUak2Jrl591uTCzcc2xTtxsLF7VRE3WPdLIfvD1SiJ7l5oMX8 vmN3UwtjS9smDKz310p1dROTT3s2xmTGxDq21q06O/6h4YsV9oRsLX+9+ikoDND4XfBAYEco9VFxhc kfO6fjOAbUA2Jdp8EZL+Uw38RuommvxaWmG99Ejeh0q4PqLsiT0N1/5WDrjfXuPDuSmfsTwT+kyWSM CVakUrqH4MWt8smN24MzoVuHu9eZvslCmCsdvwICDf1aEDgMxxIT4MtYlo/1dAskZf67qBX/TOZtnM zLjtg3/dQkWvom2DFqJIZDfEM2dY44Z4M60oQ7kGwW+65wg0vpXpDtxToVfEIfF90cQF3gFKF2gqK7 xhN0tpuADROLSG1+Mx7L1ZIC1SEb/33ux8ZJLwEAlikI2d33Cnt5NQPBo0mPnBDF1ipvf1elupsOaN GngxtY1+UscwnBxqoMNXH+N0rmxnLaH/D1VMCTb+70uJP41HP3FmiLleENwG4GKXKHS9f76yK68Kal WrMdx3pMGXyjytwuO3cziB4I3ZxEG5+LWXktCvlx+TZt2HQdpo8KDmPE/wmr5D96i8277us3+yfTTh /8nWkhFB/TObJccEmiFoACpCCRAMnqQcz45869ijeO09QKQXmQ6KOL3TJR0g== X-Developer-Key: i=ribalda@chromium.org; a=openpgp; fpr=9EC3BB66E2FC129A6F90B39556A0D81F9F782DA9 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Isolate all the changes related to the clock circular buffer to its own function, that way we can make changes easier to the buffer logic. Also simplify the lock, by removing the circular buffer clock handling from uvc_video_clock_decode(). And now that we are at it, unify the API of the clock functions. Tested-by: HungNien Chen Reviewed-by: Sergey Senozhatsky Signed-off-by: Ricardo Ribalda Reviewed-by: Sergey Tested-by: Sunplus --- drivers/media/usb/uvc/uvc_video.c | 82 ++++++++++++++++++-----------------= ---- 1 file changed, 37 insertions(+), 45 deletions(-) diff --git a/drivers/media/usb/uvc/uvc_video.c b/drivers/media/usb/uvc/uvc_= video.c index 6d0243ea0e07..8156efcf48ac 100644 --- a/drivers/media/usb/uvc/uvc_video.c +++ b/drivers/media/usb/uvc/uvc_video.c @@ -466,19 +466,28 @@ static inline ktime_t uvc_video_get_time(void) return ktime_get_real(); } =20 +static void uvc_video_clock_add_sample(struct uvc_clock *clock, + const struct uvc_clock_sample *sample) +{ + unsigned long flags; + + spin_lock_irqsave(&clock->lock, flags); + + memcpy(&clock->samples[clock->head], sample, sizeof(*sample)); + clock->head =3D (clock->head + 1) % clock->size; + clock->count =3D min(clock->count + 1, clock->size); + + spin_unlock_irqrestore(&clock->lock, flags); +} + static void uvc_video_clock_decode(struct uvc_streaming *stream, struct uvc_buffer *bu= f, const u8 *data, int len) { - struct uvc_clock_sample *sample; + struct uvc_clock_sample sample; unsigned int header_size; bool has_pts =3D false; bool has_scr =3D false; - unsigned long flags; - ktime_t time; - u16 host_sof; - u16 dev_sof; - u32 dev_stc; =20 switch (data[1] & (UVC_STREAM_PTS | UVC_STREAM_SCR)) { case UVC_STREAM_PTS | UVC_STREAM_SCR: @@ -523,11 +532,11 @@ uvc_video_clock_decode(struct uvc_streaming *stream, = struct uvc_buffer *buf, * all the data packets of the same frame contains the same SOF. In that * case only the first one will match the host_sof. */ - dev_sof =3D get_unaligned_le16(&data[header_size - 2]); - if (dev_sof =3D=3D stream->clock.last_sof) + sample.dev_sof =3D get_unaligned_le16(&data[header_size - 2]); + if (sample.dev_sof =3D=3D stream->clock.last_sof) return; =20 - dev_stc =3D get_unaligned_le32(&data[header_size - 6]); + sample.dev_stc =3D get_unaligned_le32(&data[header_size - 6]); =20 /* * STC (Source Time Clock) is the clock used by the camera. The UVC 1.5 @@ -541,21 +550,21 @@ uvc_video_clock_decode(struct uvc_streaming *stream, = struct uvc_buffer *buf, * and stc and sof are zero. */ if (buf && buf->bytesused =3D=3D 0 && len =3D=3D header_size && - dev_stc =3D=3D 0 && dev_sof =3D=3D 0) + sample.dev_stc =3D=3D 0 && sample.dev_sof =3D=3D 0) return; =20 - stream->clock.last_sof =3D dev_sof; + stream->clock.last_sof =3D sample.dev_sof; =20 - host_sof =3D usb_get_current_frame_number(stream->dev->udev); + sample.host_sof =3D usb_get_current_frame_number(stream->dev->udev); =20 /* * On some devices, like the Logitech C922, the device SOF does not run * at a stable rate of 1kHz. For those devices use the host SOF instead. */ if (stream->dev->quirks & UVC_QUIRK_INVALID_DEVICE_SOF) - dev_sof =3D host_sof; + sample.dev_sof =3D sample.host_sof; =20 - time =3D uvc_video_get_time(); + sample.host_time =3D uvc_video_get_time(); =20 /* * The UVC specification allows device implementations that can't obtain @@ -578,46 +587,29 @@ uvc_video_clock_decode(struct uvc_streaming *stream, = struct uvc_buffer *buf, * the 8 LSBs of the delta are kept. */ if (stream->clock.sof_offset =3D=3D (u16)-1) { - u16 delta_sof =3D (host_sof - dev_sof) & 255; + u16 delta_sof =3D (sample.host_sof - sample.dev_sof) & 255; if (delta_sof >=3D 10) stream->clock.sof_offset =3D delta_sof; else stream->clock.sof_offset =3D 0; } =20 - dev_sof =3D (dev_sof + stream->clock.sof_offset) & 2047; - - spin_lock_irqsave(&stream->clock.lock, flags); - - sample =3D &stream->clock.samples[stream->clock.head]; - sample->dev_stc =3D dev_stc; - sample->dev_sof =3D dev_sof; - sample->host_sof =3D host_sof; - sample->host_time =3D time; - - /* Update the sliding window head and count. */ - stream->clock.head =3D (stream->clock.head + 1) % stream->clock.size; + sample.dev_sof =3D (sample.dev_sof + stream->clock.sof_offset) & 2047; + sample.dev_stc =3D get_unaligned_le32(&data[header_size - 6]); =20 - if (stream->clock.count < stream->clock.size) - stream->clock.count++; - - spin_unlock_irqrestore(&stream->clock.lock, flags); + uvc_video_clock_add_sample(&stream->clock, &sample); } =20 -static void uvc_video_clock_reset(struct uvc_streaming *stream) +static void uvc_video_clock_reset(struct uvc_clock *clock) { - struct uvc_clock *clock =3D &stream->clock; - clock->head =3D 0; clock->count =3D 0; clock->last_sof =3D -1; clock->sof_offset =3D -1; } =20 -static int uvc_video_clock_init(struct uvc_streaming *stream) +static int uvc_video_clock_init(struct uvc_clock *clock) { - struct uvc_clock *clock =3D &stream->clock; - spin_lock_init(&clock->lock); clock->size =3D 32; =20 @@ -626,15 +618,15 @@ static int uvc_video_clock_init(struct uvc_streaming = *stream) if (clock->samples =3D=3D NULL) return -ENOMEM; =20 - uvc_video_clock_reset(stream); + uvc_video_clock_reset(clock); =20 return 0; } =20 -static void uvc_video_clock_cleanup(struct uvc_streaming *stream) +static void uvc_video_clock_cleanup(struct uvc_clock *clock) { - kfree(stream->clock.samples); - stream->clock.samples =3D NULL; + kfree(clock->samples); + clock->samples =3D NULL; } =20 /* @@ -2108,7 +2100,7 @@ int uvc_video_resume(struct uvc_streaming *stream, in= t reset) =20 stream->frozen =3D 0; =20 - uvc_video_clock_reset(stream); + uvc_video_clock_reset(&stream->clock); =20 if (!uvc_queue_streaming(&stream->queue)) return 0; @@ -2257,7 +2249,7 @@ int uvc_video_start_streaming(struct uvc_streaming *s= tream) { int ret; =20 - ret =3D uvc_video_clock_init(stream); + ret =3D uvc_video_clock_init(&stream->clock); if (ret < 0) return ret; =20 @@ -2275,7 +2267,7 @@ int uvc_video_start_streaming(struct uvc_streaming *s= tream) error_video: usb_set_interface(stream->dev->udev, stream->intfnum, 0); error_commit: - uvc_video_clock_cleanup(stream); + uvc_video_clock_cleanup(&stream->clock); =20 return ret; } @@ -2303,5 +2295,5 @@ void uvc_video_stop_streaming(struct uvc_streaming *s= tream) usb_clear_halt(stream->dev->udev, pipe); } =20 - uvc_video_clock_cleanup(stream); + uvc_video_clock_cleanup(&stream->clock); } --=20 2.40.0.rc1.284.g88254d51c5-goog-b4-0.11.0-dev-696ae From nobody Sat Apr 11 20:29:17 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id E2BB5C6FD19 for ; Fri, 10 Mar 2023 09:07:49 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231556AbjCJJHs (ORCPT ); Fri, 10 Mar 2023 04:07:48 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40220 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230379AbjCJJGn (ORCPT ); Fri, 10 Mar 2023 04:06:43 -0500 Received: from mail-ed1-x536.google.com (mail-ed1-x536.google.com [IPv6:2a00:1450:4864:20::536]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 9880F112593 for ; Fri, 10 Mar 2023 01:02:40 -0800 (PST) Received: by mail-ed1-x536.google.com with SMTP id x3so17575252edb.10 for ; Fri, 10 Mar 2023 01:02:40 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1678438936; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:from:to:cc:subject:date:message-id :reply-to; bh=Xnm0YfgvCV+Ipm6xs9Mtjl0Q+VTBw28sf5U+arx5bb8=; b=MZri77WSkYEyxC5HlP1IFuaW3AYi3chr9mQhghCiMLqfSj8Op8753JyQ+sP6jCVbBX X6WYRTE7y5kJ5xuHCV/CMqVloSssG0qaTpCBjWmwXRK98gc6FwdVjWIF0SHnydheijRA IPXW2HPRgLVpdx9k8ktWDKm3hDi2bmGFYaf/k= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1678438936; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=Xnm0YfgvCV+Ipm6xs9Mtjl0Q+VTBw28sf5U+arx5bb8=; b=b5ewiCi82BC8bgOzi8F7jnboA7PmtM2q2U9PHNp2n3hTaORZORpKmtHF9HmUHUQLvb sDOZH4RRWjpEji3PiszD2fo47cxOm0lbdvFdpFzkLrWTXXVEywJ2fXue5UH3eWBpyd+C n0uWQBGp2+2gE2aJCrbCTLKN1p0JLyBjZZZExg9xkLvA3Ya8tUm/3X14ziURgcemg1bv TJ5FumVLmHs98vLiX3HUeZt/7rOVgqWuLUaJVuSXkDwBzDmcUZDpKZuLyuxAzOBZd7D+ YsKPLKYrqe6m7U7Mz+WbkP5LASKdb/dBjTP+0vznxkgGyhJH2sEiG7Eiv/ZgqcteYd9R wvjA== X-Gm-Message-State: AO0yUKV1o9AOQDATJCrUUCgDjxAl9Xn3lhYBkhfqareV0zLoN7psKep1 JjrH8+RdgAjpz5ystXOJ/OGP/A== X-Google-Smtp-Source: AK7set+GWN0YNI3hB6snrIv4co9cLgbNHNwz5c1pc74Nkl8Zth62IfdQcLIoOo5krx7RR2duasVxWQ== X-Received: by 2002:a17:907:c0c:b0:8af:2cf7:dd2b with SMTP id ga12-20020a1709070c0c00b008af2cf7dd2bmr31570396ejc.13.1678438936082; Fri, 10 Mar 2023 01:02:16 -0800 (PST) Received: from alco.roam.corp.google.com ([2620:0:1059:10:ef69:7ab6:87ac:99f]) by smtp.gmail.com with ESMTPSA id s5-20020a170906454500b008b23b22b062sm692931ejq.114.2023.03.10.01.02.15 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 10 Mar 2023 01:02:15 -0800 (PST) From: Ricardo Ribalda Date: Fri, 10 Mar 2023 10:01:33 +0100 Subject: [PATCH v7 6/6] media: uvcvideo: Fix hw timestamp handling for slow FPS MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20220920-resend-hwtimestamp-v7-6-cf1d78bb8821@chromium.org> References: <20220920-resend-hwtimestamp-v7-0-cf1d78bb8821@chromium.org> In-Reply-To: <20220920-resend-hwtimestamp-v7-0-cf1d78bb8821@chromium.org> To: Laurent Pinchart , Mauro Carvalho Chehab Cc: Ricardo Ribalda , linux-kernel@vger.kernel.org, linux-media@vger.kernel.org, Sergey Senozhatsky , "hn.chen" X-Mailer: b4 0.11.0-dev-696ae X-Developer-Signature: v=1; a=openpgp-sha256; l=2453; i=ribalda@chromium.org; h=from:subject:message-id; bh=QSOtDi8Lpp1WwCcscneU3GgnDWNqfm9La5ACGKDE/1s=; b=owEBbQKS/ZANAwAKAdE30T7POsSIAcsmYgBkCvIO/4kYmLvH1iiWCJT+hFKLcOK8Wi79IHBtmT/M xtdiMKmJAjMEAAEKAB0WIQREDzjr+/4oCDLSsx7RN9E+zzrEiAUCZAryDgAKCRDRN9E+zzrEiLBTD/ 0Ryz/FeiC6LVOp07enO0v9qf4Yw+/Lu6WjBWFsRm1oOxcANF9lL/mkE2Qd6SqcXJf1QXdBfy7udIUe iwVaULNUu/VVWWskPMDTqswdyiqbHHf4+B5BHzWTteEvlmipjYbHcmsRxh7i8IJBBMDiE7vIJ9WXd4 uoD4xc05qskdOKx0lYis65D7w3y6LY5jL+GmXcJdYtu9dYYXYcakIfS9XoMGnMyip4yiBTgsRvXTGM NH49PIao1aOkOoX8vDCo6R650JNDAq0B4Pgz0hiwgg8r/CpVp3vGAKD1nItbVV9XFcrUPXwK5ecxjv eQpK2zsvMm+11YYPEWQFsO/u08HcQhw9t6fg7/zGUUrfzfilL+ajc7P48vHpY5LNBSBh9B6am/JOCc e390J6WA5FMP07H5LlchX1ssgjSAh4u7bIGcXprxx2OzOwGgad8y3feYSZ5GSGhmRE/3cU2+DFky1F 499RTkiyJQ/C5QEDoxJtslOqo9yxOa7hMpBEWJ2+cDqdW/E31+AxPizo0RqLYZWgmrVpk3PIG12AOd 4jg90tUWWXxTde+4qr08qyWBE8L8doLVWrZOgc9AAqwPptrURcfjm1ckTSPDYbj/UOTTxGpnehQFYR bnbq0uhBZnz/Gkqxge8yvbDbZpfmISWGF1eslE0oy8a3E1TvPH4ipZYIV8CQ== X-Developer-Key: i=ribalda@chromium.org; a=openpgp; fpr=9EC3BB66E2FC129A6F90B39556A0D81F9F782DA9 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org In UVC 1.5 we get a single clock value per frame. With the current buffer size of 32, FPS slowers than 32 might roll-over twice. The current code cannot handle two roll-over and provide invalid timestamps. Revome all the samples from the circular buffer that are more than two rollovers old, so the algorithm always provides good timestamps. Note that we are removing values that are more than one second old, which means that there is enough distance between the two points that we use for the interpolation to provide good values. Tested-by: HungNien Chen Signed-off-by: Ricardo Ribalda Reviewed-by: Sergey Tested-by: Sunplus --- drivers/media/usb/uvc/uvc_video.c | 16 ++++++++++++++++ drivers/media/usb/uvc/uvcvideo.h | 1 + 2 files changed, 17 insertions(+) diff --git a/drivers/media/usb/uvc/uvc_video.c b/drivers/media/usb/uvc/uvc_= video.c index 8156efcf48ac..eb1165366fa0 100644 --- a/drivers/media/usb/uvc/uvc_video.c +++ b/drivers/media/usb/uvc/uvc_video.c @@ -473,6 +473,21 @@ static void uvc_video_clock_add_sample(struct uvc_cloc= k *clock, =20 spin_lock_irqsave(&clock->lock, flags); =20 + /* Delete last overflows */ + if (clock->head =3D=3D clock->last_sof_overflow) + clock->last_sof_overflow =3D -1; + + /* Handle overflows */ + if (clock->count > 0 && clock->last_sof > sample->dev_sof) { + /* Remove data from the last^2 overflows */ + if (clock->last_sof_overflow !=3D -1) { + clock->count =3D (clock->head - clock->last_sof_overflow + + clock->count) % clock->count; + } + clock->last_sof_overflow =3D clock->head; + } + + /* Add sample */ memcpy(&clock->samples[clock->head], sample, sizeof(*sample)); clock->head =3D (clock->head + 1) % clock->size; clock->count =3D min(clock->count + 1, clock->size); @@ -605,6 +620,7 @@ static void uvc_video_clock_reset(struct uvc_clock *clo= ck) clock->head =3D 0; clock->count =3D 0; clock->last_sof =3D -1; + clock->last_sof_overflow =3D -1; clock->sof_offset =3D -1; } =20 diff --git a/drivers/media/usb/uvc/uvcvideo.h b/drivers/media/usb/uvc/uvcvi= deo.h index 07b2fdb80adf..bf9f5162b833 100644 --- a/drivers/media/usb/uvc/uvcvideo.h +++ b/drivers/media/usb/uvc/uvcvideo.h @@ -499,6 +499,7 @@ struct uvc_streaming { unsigned int head; unsigned int count; unsigned int size; + unsigned int last_sof_overflow; =20 u16 last_sof; u16 sof_offset; --=20 2.40.0.rc1.284.g88254d51c5-goog-b4-0.11.0-dev-696ae