From patchwork Wed Mar 15 13:30:13 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ricardo Ribalda X-Patchwork-Id: 663857 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 6B45BC61DA4 for ; Wed, 15 Mar 2023 13:30:39 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232152AbjCONah (ORCPT ); Wed, 15 Mar 2023 09:30:37 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34598 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232069AbjCONae (ORCPT ); Wed, 15 Mar 2023 09:30:34 -0400 Received: from mail-ed1-x529.google.com (mail-ed1-x529.google.com [IPv6:2a00:1450:4864:20::529]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 74A087DF9A for ; Wed, 15 Mar 2023 06:30:28 -0700 (PDT) Received: by mail-ed1-x529.google.com with SMTP id cn21so45398491edb.0 for ; Wed, 15 Mar 2023 06:30:28 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1678887027; 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=gHfCVCL+KtwV3EGazhIeiRvmQifinDdtF04C+2pUjQdV1/8/ES84XLYEjUdkzB4QsU TSStCHlPV1aC8PvSJQYXYl/EScKnV2JYi3AISC6OUA392cGv0mylaw+FIcpg1OunvKbT RAC2B//QzWfaTxB8+Dc/EtX9u3Bg0yHMmvz/Y= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1678887027; 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=FAC0Ygk9Ld/5/MvCEwMQ2+sZZLNIykk/1mm49JKiVfjEF9bElI/3t7dz7Xs5jMM27V oF2qlgqsVrWK67oqMVrv/V4iOlI5qtUhVXYJFWJrek6irVVPvbQovz5V2EssJG5HF3xF KmRjEfpujG3TJj+tXhqi0p6Bwfl4NGyWrR1NOtjfsVXRoifChyrP6gzfVimS61QJkBr4 Sf8Bx11Mu9hqi2xxfDEuod4tZ7/c/27JbebGbDwvEc+tVgSl9IVduECWRMivEikc+5vi +bcEUbFoG5LifvXShAICpAIJYvJOpuuvQhjrQMXJRDwoUIYPZ5AOKLMsIC0GqYmSxdzC vY5Q== X-Gm-Message-State: AO0yUKWb0JBhU4jBexj8IXqpWn3H5Jblut6xENOk4mFJSNDqDrne2jHp ZAVSW52PpY3xgZ7A7TSBzo6kQg== X-Google-Smtp-Source: AK7set8REglz9uHlenBcNYXEDNU7f8XywG6UdqAnZ4tGl829rZg4MXLJsMz1sNqBii07HK3EkEKmyw== X-Received: by 2002:a17:906:c1d0:b0:879:ab3:93cd with SMTP id bw16-20020a170906c1d000b008790ab393cdmr7663387ejb.46.1678887026905; Wed, 15 Mar 2023 06:30:26 -0700 (PDT) Received: from alco.roam.corp.google.com ([2620:0:1059:10:491c:f627:27f0:1c4]) by smtp.gmail.com with ESMTPSA id ha11-20020a170906a88b00b0092043ae1a8asm2534638ejb.92.2023.03.15.06.30.26 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 15 Mar 2023 06:30:26 -0700 (PDT) From: Ricardo Ribalda Date: Wed, 15 Mar 2023 14:30:13 +0100 Subject: [PATCH v9 2/6] media: uvcvideo: Ignore empty TS packets MIME-Version: 1.0 Message-Id: <20220920-resend-hwtimestamp-v9-2-55a89f46f6be@chromium.org> References: <20220920-resend-hwtimestamp-v9-0-55a89f46f6be@chromium.org> In-Reply-To: <20220920-resend-hwtimestamp-v9-0-55a89f46f6be@chromium.org> To: Laurent Pinchart , Mauro Carvalho Chehab Cc: Sergey Senozhatsky , Ricardo Ribalda , linux-kernel@vger.kernel.org, "hn.chen" , linux-media@vger.kernel.org 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/ZANAwAKAdE30T7POsSIAcsmYgBkEchpR2vAg7sBI1dBwEaleUoYD6Drm9+mb10eFF4d hd04yCeJAjMEAAEKAB0WIQREDzjr+/4oCDLSsx7RN9E+zzrEiAUCZBHIaQAKCRDRN9E+zzrEiKCRD/ 94LdXzVScvrVV29sQ8EK2AVywlk82cd6hn4nTtlVg4uXUKQYiHC5na7f44u2FI9CSFXKwiwghgAZwd 5/dDjYFYPND+npCycYyq+jz4WVsrxsOoB2VyqkuOh6ydpq5d/wbKaI/FIXkx4LRexFe2/BcT7kdtN8 QUFA8K6zLGDPpcFUspOX4Rn3kVY9V7IkklSZPOSy2z0UzR43xsNiVtAA61/D7Bw/H31K8x5PmrumE2 z7eTr5YpcGunNBaTTFUm3jgDFSSxY4l8v6kIK7NqeJqxWtCOGqYdQLw4yt9lVlTBclitt1YQCWybjd s4MWoYXtL3xRZTTC7o3GbNPQr42+/E71beG2E9KpaCXvl0r2QVPMlnb4bUO6QDfE6tgJBSik4Rlm/o 5XHtOUvsS6FfUPZ3RlQegjRSCjM5msSzhiStAwMPt1OquSz0QsfDQxmA+CLZpy6DWOJYTUkU5tzR01 hVGOUhF7QRK/O0NRHZxJkGSnDzZS/TmMzayHKVBGNDqBDykRPXsHJramc5e5mHuZl/s3uIsFbm+BJZ lZC75yL/VkAdYCiuYo4AWNv4SjSKYkEaMHrNr6Iy2v7HAXVoc+RhCITlGX7ik52DvxRvQH5Q6j83Aa MGuJWJeZ6HvhDgr11Mo9RByVmOa4Ccq8oWpF6OYIDOlnK+ziPoSQ4G1IrV8g== X-Developer-Key: i=ribalda@chromium.org; a=openpgp; fpr=9EC3BB66E2FC129A6F90B39556A0D81F9F782DA9 Precedence: bulk List-ID: X-Mailing-List: linux-media@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: Laurent Pinchart --- 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, struct uvc_buffer *buf, ktime_t time; u16 host_sof; u16 dev_sof; + u32 dev_stc; 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, struct uvc_buffer *buf, if (dev_sof == stream->clock.last_sof) return; + dev_stc = 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 == 0 && len == header_size && + dev_stc == 0 && dev_sof == 0) + return; + stream->clock.last_sof = dev_sof; host_sof = usb_get_current_frame_number(stream->dev->udev); @@ -564,7 +582,7 @@ uvc_video_clock_decode(struct uvc_streaming *stream, struct uvc_buffer *buf, spin_lock_irqsave(&stream->clock.lock, flags); sample = &stream->clock.samples[stream->clock.head]; - sample->dev_stc = get_unaligned_le32(&data[header_size - 6]); + sample->dev_stc = dev_stc; sample->dev_sof = dev_sof; sample->host_sof = host_sof; sample->host_time = time; From patchwork Wed Mar 15 13:30:16 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ricardo Ribalda X-Patchwork-Id: 663856 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 EA27DC6FD1D for ; Wed, 15 Mar 2023 13:30:42 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232214AbjCONal (ORCPT ); Wed, 15 Mar 2023 09:30:41 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34702 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231993AbjCONag (ORCPT ); Wed, 15 Mar 2023 09:30:36 -0400 Received: from mail-ed1-x52a.google.com (mail-ed1-x52a.google.com [IPv6:2a00:1450:4864:20::52a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1C8B391B79 for ; Wed, 15 Mar 2023 06:30:31 -0700 (PDT) Received: by mail-ed1-x52a.google.com with SMTP id x3so75497852edb.10 for ; Wed, 15 Mar 2023 06:30:31 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1678887029; 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=nfrIME0wJIYRWxUiJsRB8/dUZqSdjz1B4rSK467QV2U=; b=KHaT5VylGWcmvubhEggVy1vnsnxEkDSBrt3DeXHUsFx0R4eN1KDdZ3ymOH3PxVeKpX pNHSUjoqcZN1nHEA1MDBDH64GV5eJ2F1F8kcKCYV0jRuSvEYp/mYAHlqB+bD3ClW6Ebw vh2DGGYroFbQWIYp4S5befUpvtQkeYtj1r+dQ= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1678887029; 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=nfrIME0wJIYRWxUiJsRB8/dUZqSdjz1B4rSK467QV2U=; b=V3KZVKKpuq+b0wXU/xql03VyTpPLFaFozHPoFoimAA4u39ZgRuIzQJnxQU069nb4fj FeZD68lHzowNTdEtZxp9y6S0Zy88w47FinAsDjKs0j2tqkBrsVCPEVXPQmagZ1bJ1iIT HQTPeGj4SOQxoow3wdLOEwrnWNoYzfVJQxlfGkColdj5KfhibHnn46yJfCr0H7Y6gkhH 3ov81yb7RYeo+flLTRSeWWREev4mH+ykQiNysNKo92nfBjTlm4YefKrzbiJJ+kNQieo4 tXgBG6q3OXgDVgVIRL+nZicsVJAd229CEYpPnK9iUNUr6pxYGpBoUvYfux3/Y+1HvxaA 228A== X-Gm-Message-State: AO0yUKXBitnr3Dx5UvTY8D3ZIRTQpYaxVyQbRy0MIp9sBz5rr3h28kog ahJRExn++llYqOjoCoDQWOHJnw== X-Google-Smtp-Source: AK7set9/wL4JhG2jrBM6XmJsChWwIxp6GU1HruVLKmf4ERJUrScxkUoUv1YDYsMv5ya3e3wO+kZrIQ== X-Received: by 2002:a17:906:4e92:b0:878:72d0:2817 with SMTP id v18-20020a1709064e9200b0087872d02817mr6062723eju.29.1678887029512; Wed, 15 Mar 2023 06:30:29 -0700 (PDT) Received: from alco.roam.corp.google.com ([2620:0:1059:10:491c:f627:27f0:1c4]) by smtp.gmail.com with ESMTPSA id ha11-20020a170906a88b00b0092043ae1a8asm2534638ejb.92.2023.03.15.06.30.28 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 15 Mar 2023 06:30:29 -0700 (PDT) From: Ricardo Ribalda Date: Wed, 15 Mar 2023 14:30:16 +0100 Subject: [PATCH v9 5/6] media: uvcvideo: Refactor clock circular buffer MIME-Version: 1.0 Message-Id: <20220920-resend-hwtimestamp-v9-5-55a89f46f6be@chromium.org> References: <20220920-resend-hwtimestamp-v9-0-55a89f46f6be@chromium.org> In-Reply-To: <20220920-resend-hwtimestamp-v9-0-55a89f46f6be@chromium.org> To: Laurent Pinchart , Mauro Carvalho Chehab Cc: Sergey Senozhatsky , Ricardo Ribalda , linux-kernel@vger.kernel.org, "hn.chen" , linux-media@vger.kernel.org X-Mailer: b4 0.11.0-dev-696ae X-Developer-Signature: v=1; a=openpgp-sha256; l=6682; i=ribalda@chromium.org; h=from:subject:message-id; bh=1XOlapgZ7o2YnyPsHBz+Zu3te118klpRecTLUAUw/1Q=; b=owEBbQKS/ZANAwAKAdE30T7POsSIAcsmYgBkEchskmhPNyQcOP0px7P1LeWbWNLniHOx4a86Kzhj 1fGkW0aJAjMEAAEKAB0WIQREDzjr+/4oCDLSsx7RN9E+zzrEiAUCZBHIbAAKCRDRN9E+zzrEiPCuD/ 9aGs1INStszDd9kwQmNc+7X/FvhIisrcqqhFYKTs8n4vMBpepipuRtuPtX925dXnuLgHgjYZP1Pae/ /nni4S6OpCb/5Q3cqO4e5PuofG9f8c3mjwRHUYyu3sLFFgIT882ICgLKDDk4xU+tNWwnwI97FpdHFl x8pWWR1qnRqdci0mT+jUMpOFUvlLN2UWpdR1QwKrxGoplQQIT6pU1TGMRuxy52d1Il6iYFBS9lWBQa LYyNATj+nHh5iPocEq/Vx/M5WvsIJfmDLMmB3pMr93vP2T057BfFy+AgYrZxHlo8CM/qmbbYK4GtL1 46IhXLf69ohhBo0a0n36DSfr3LSDaYG7zZfYr49ExMBOS2sP00++hIHcgDgnNOmZzzKsGH6Qdy8dai dsg5FimQbfWl6Ygb1IOsMXpgMwgRIpEEDtpxcvM2IUIaZPNLxpgv+/WDFWUrC6/zmGNysGn9C8KY5Z KmFyEOCux16IE2SLgjF+kKGyTqEY6B7RuCCROWNeArhEz6hthfvF8f0TCeirJM6DvsQnqzOXvzqZJz NmGX4rrsjWfNdevApQHFjE6VsgBaFDJ8zRuKXl9fmG7R8izf6r6ATNr5i5EUSmNlYbwh6gDw8mLaJV 3y2DjuupdjjCDk4QXx5uvrvzkxyPvTZAkhsava5DdpqxyO4vWt0fDbwnUKaQ== X-Developer-Key: i=ribalda@chromium.org; a=openpgp; fpr=9EC3BB66E2FC129A6F90B39556A0D81F9F782DA9 Precedence: bulk List-ID: X-Mailing-List: linux-media@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: Laurent Pinchart --- drivers/media/usb/uvc/uvc_video.c | 84 ++++++++++++++++++--------------------- 1 file changed, 38 insertions(+), 46 deletions(-) diff --git a/drivers/media/usb/uvc/uvc_video.c b/drivers/media/usb/uvc/uvc_video.c index 6d0243ea0e07..1db0d1bc80e6 100644 --- a/drivers/media/usb/uvc/uvc_video.c +++ b/drivers/media/usb/uvc/uvc_video.c @@ -466,19 +466,30 @@ static inline ktime_t uvc_video_get_time(void) return ktime_get_real(); } +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 = (clock->head + 1) % clock->size; + clock->count = min(clock->count + 1, clock->size); + + spin_unlock_irqrestore(&clock->lock, flags); + + clock->last_sof = sample->dev_sof; +} + static void uvc_video_clock_decode(struct uvc_streaming *stream, struct uvc_buffer *buf, const u8 *data, int len) { - struct uvc_clock_sample *sample; + struct uvc_clock_sample sample; unsigned int header_size; bool has_pts = false; bool has_scr = false; - unsigned long flags; - ktime_t time; - u16 host_sof; - u16 dev_sof; - u32 dev_stc; switch (data[1] & (UVC_STREAM_PTS | UVC_STREAM_SCR)) { case UVC_STREAM_PTS | UVC_STREAM_SCR: @@ -523,11 +534,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 = get_unaligned_le16(&data[header_size - 2]); - if (dev_sof == stream->clock.last_sof) + sample.dev_sof = get_unaligned_le16(&data[header_size - 2]); + if (sample.dev_sof == stream->clock.last_sof) return; - dev_stc = get_unaligned_le32(&data[header_size - 6]); + sample.dev_stc = get_unaligned_le32(&data[header_size - 6]); /* * STC (Source Time Clock) is the clock used by the camera. The UVC 1.5 @@ -541,21 +552,19 @@ uvc_video_clock_decode(struct uvc_streaming *stream, struct uvc_buffer *buf, * and stc and sof are zero. */ if (buf && buf->bytesused == 0 && len == header_size && - dev_stc == 0 && dev_sof == 0) + sample.dev_stc == 0 && sample.dev_sof == 0) return; - stream->clock.last_sof = dev_sof; - - host_sof = usb_get_current_frame_number(stream->dev->udev); + sample.host_sof = 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 = host_sof; + sample.dev_sof = sample.host_sof; - time = uvc_video_get_time(); + sample.host_time = uvc_video_get_time(); /* * 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 == (u16)-1) { - u16 delta_sof = (host_sof - dev_sof) & 255; + u16 delta_sof = (sample.host_sof - sample.dev_sof) & 255; if (delta_sof >= 10) stream->clock.sof_offset = delta_sof; else stream->clock.sof_offset = 0; } - dev_sof = (dev_sof + stream->clock.sof_offset) & 2047; - - spin_lock_irqsave(&stream->clock.lock, flags); - - sample = &stream->clock.samples[stream->clock.head]; - sample->dev_stc = dev_stc; - sample->dev_sof = dev_sof; - sample->host_sof = host_sof; - sample->host_time = time; - - /* Update the sliding window head and count. */ - stream->clock.head = (stream->clock.head + 1) % stream->clock.size; + sample.dev_sof = (sample.dev_sof + stream->clock.sof_offset) & 2047; + sample.dev_stc = get_unaligned_le32(&data[header_size - 6]); - 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); } -static void uvc_video_clock_reset(struct uvc_streaming *stream) +static void uvc_video_clock_reset(struct uvc_clock *clock) { - struct uvc_clock *clock = &stream->clock; - clock->head = 0; clock->count = 0; clock->last_sof = -1; clock->sof_offset = -1; } -static int uvc_video_clock_init(struct uvc_streaming *stream) +static int uvc_video_clock_init(struct uvc_clock *clock) { - struct uvc_clock *clock = &stream->clock; - spin_lock_init(&clock->lock); clock->size = 32; @@ -626,15 +618,15 @@ static int uvc_video_clock_init(struct uvc_streaming *stream) if (clock->samples == NULL) return -ENOMEM; - uvc_video_clock_reset(stream); + uvc_video_clock_reset(clock); return 0; } -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 = NULL; + kfree(clock->samples); + clock->samples = NULL; } /* @@ -2108,7 +2100,7 @@ int uvc_video_resume(struct uvc_streaming *stream, int reset) stream->frozen = 0; - uvc_video_clock_reset(stream); + uvc_video_clock_reset(&stream->clock); if (!uvc_queue_streaming(&stream->queue)) return 0; @@ -2257,7 +2249,7 @@ int uvc_video_start_streaming(struct uvc_streaming *stream) { int ret; - ret = uvc_video_clock_init(stream); + ret = uvc_video_clock_init(&stream->clock); if (ret < 0) return ret; @@ -2275,7 +2267,7 @@ int uvc_video_start_streaming(struct uvc_streaming *stream) error_video: usb_set_interface(stream->dev->udev, stream->intfnum, 0); error_commit: - uvc_video_clock_cleanup(stream); + uvc_video_clock_cleanup(&stream->clock); return ret; } @@ -2303,5 +2295,5 @@ void uvc_video_stop_streaming(struct uvc_streaming *stream) usb_clear_halt(stream->dev->udev, pipe); } - uvc_video_clock_cleanup(stream); + uvc_video_clock_cleanup(&stream->clock); } From patchwork Wed Mar 15 13:30:17 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ricardo Ribalda X-Patchwork-Id: 663855 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 ADFE7C61DA4 for ; Wed, 15 Mar 2023 13:30:50 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232238AbjCONat (ORCPT ); Wed, 15 Mar 2023 09:30:49 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34758 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232154AbjCONah (ORCPT ); Wed, 15 Mar 2023 09:30:37 -0400 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 3AA73132FA for ; Wed, 15 Mar 2023 06:30:32 -0700 (PDT) Received: by mail-ed1-x52b.google.com with SMTP id h8so31781016ede.8 for ; Wed, 15 Mar 2023 06:30:32 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1678887030; 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=i5B0qoy4xzYyHKiW419Q3keWH8TLfCy2QqjxB+rQKmY=; b=eDuZrMTZQRkFCCKAVBZ1yDVVPQuq+yVVR1dwL33dDy6kjAY5yuDtq1vMJ17exZTVvT nPgfXww6zGjvgRadPsNBxLbs2IglY2laD0zpXV5nP6i9Kvbv4VIZ1ZO4IlprXACuUA5z ZNVVurZs3N6YgnTj9oKH08G7QK+blLduri9Mw= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1678887030; 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=i5B0qoy4xzYyHKiW419Q3keWH8TLfCy2QqjxB+rQKmY=; b=r4BnRLeQlHoTP45ULLjmNpHtnIVAtimEgYec5eC8DL6F10lXJk10Yh3VL09+oTWFll 98xeebQtmMsvpeiwJ5h62AXAF+4jTcXgawRaY1shqP8L6gWAFiwoA7ppTYqyUB/nl1kA gR/Iht4V9g05D/FMmE1LZCbPeWAJMRV/u6vfHWQcUHm7f4LCeXgV8u2p0IWLDYCaG1tj DHn3dnHyafiR3B2ZsrdIxITIcXwd/15unMLxRQixfoaRWFcLBpclGwRHOaYr2wzEcMXQ ThI14d+13bW20ZSulrtzPHP/uoAEfqSqZqV1Nx3NGlhAaXyp0xW6jBoIPArylV8kkMsw ICpg== X-Gm-Message-State: AO0yUKWPWq4N7k+9UfNDJESA/MZWITgNVowcLvTMEWGsh/E2BRhYhaHA WgPNuWn4+LJWJTSWF4eo4D/+Ag== X-Google-Smtp-Source: AK7set/0Gqcp0PUhCw2srN5+7w90EXouNyEdBKyHEctHCAQW2NuynKrzEXN6u63V1fR3kTO2uwlp5A== X-Received: by 2002:a17:906:bcc7:b0:88c:4f0d:85af with SMTP id lw7-20020a170906bcc700b0088c4f0d85afmr8035808ejb.75.1678887030229; Wed, 15 Mar 2023 06:30:30 -0700 (PDT) Received: from alco.roam.corp.google.com ([2620:0:1059:10:491c:f627:27f0:1c4]) by smtp.gmail.com with ESMTPSA id ha11-20020a170906a88b00b0092043ae1a8asm2534638ejb.92.2023.03.15.06.30.29 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 15 Mar 2023 06:30:30 -0700 (PDT) From: Ricardo Ribalda Date: Wed, 15 Mar 2023 14:30:17 +0100 Subject: [PATCH v9 6/6] media: uvcvideo: Fix hw timestamp handling for slow FPS MIME-Version: 1.0 Message-Id: <20220920-resend-hwtimestamp-v9-6-55a89f46f6be@chromium.org> References: <20220920-resend-hwtimestamp-v9-0-55a89f46f6be@chromium.org> In-Reply-To: <20220920-resend-hwtimestamp-v9-0-55a89f46f6be@chromium.org> To: Laurent Pinchart , Mauro Carvalho Chehab Cc: Sergey Senozhatsky , Ricardo Ribalda , linux-kernel@vger.kernel.org, "hn.chen" , linux-media@vger.kernel.org X-Mailer: b4 0.11.0-dev-696ae X-Developer-Signature: v=1; a=openpgp-sha256; l=2751; i=ribalda@chromium.org; h=from:subject:message-id; bh=fCG+oG/6p/tn1lzkSxpwZk3Rj+eiISLy+hRedLVdGsQ=; b=owEBbQKS/ZANAwAKAdE30T7POsSIAcsmYgBkEchugQqeZNyZ6DJj1IZ/0buGF2GeBZkLFQVks2eb gBmN6LiJAjMEAAEKAB0WIQREDzjr+/4oCDLSsx7RN9E+zzrEiAUCZBHIbgAKCRDRN9E+zzrEiPkZD/ 4omsHCqeQYnAUSpfL3GnIT2BJqCAunmwFKnHdD0FB9IzhfTUorQnctbqToZlfjD6XHniSX+wkRddXm 7HhDjMAnSLYLWCNkRv/9Qpw2590qvZ6cE2OYWwOM4ZDbDlbH4NjFkCSusH1urP6Y9sN1Y8hh6g2fsv WKfqMTCMUdD13JdTyUBpO/FeOFnkZe9UKRsIzP+0kpCyIKJti4RrRthD+qyO8ll5eIoNjxwDVLfMYS j0i3/Io3XK64BdHkkWrROqhHfZ8kqVVExPA6p7cBz6NEmby2WiOY+5ad+Dl8McF3KUBF9H8gtChv9g eGayxIFoJefeJy7u0ZnB1L3otr5j0h9311axpx2XIEYtWHgAKIWjp436t5A9FYDiBYNvx7j0vS6WSM WDY5MWHUl2m2OyjbA8lXlKTaO3Ga7vJUCdcSsW0SAiKWzkt+6ORAVvcYk3Mt1d7w83J6N3Uc1CYWxG ppn6CjkgloIawA+YIsuWFYaSmQCUfQl89ttEgDYvbUVZEySSjHfyPnIPR8NWCCy0GykTlsT28LCnMk vyqfBS5waUf+xBW5vZJZVqWhDsnhmrPhCz84TtIUhZDyD+oJh6hPZHQTO90FmdLo6/kKFLkzgjOX9a V8DtBU+G4AS1FeNsqdOpCogLNXRwuoNJ5ntpOlm3MgZYsvwo59bCNmRqtrqg== X-Developer-Key: i=ribalda@chromium.org; a=openpgp; fpr=9EC3BB66E2FC129A6F90B39556A0D81F9F782DA9 Precedence: bulk List-ID: X-Mailing-List: linux-media@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 Senozhatsky --- drivers/media/usb/uvc/uvc_video.c | 24 ++++++++++++++++++++++++ drivers/media/usb/uvc/uvcvideo.h | 1 + 2 files changed, 25 insertions(+) diff --git a/drivers/media/usb/uvc/uvc_video.c b/drivers/media/usb/uvc/uvc_video.c index 1db0d1bc80e6..c58b51207be6 100644 --- a/drivers/media/usb/uvc/uvc_video.c +++ b/drivers/media/usb/uvc/uvc_video.c @@ -471,8 +471,31 @@ static void uvc_video_clock_add_sample(struct uvc_clock *clock, { unsigned long flags; + /* + * If we write new data on the position where we had the last + * overflow, remove the overflow pointer. There is no overflow + * on the whole circular buffer. + */ + if (clock->head == clock->last_sof_overflow) + clock->last_sof_overflow = -1; + spin_lock_irqsave(&clock->lock, flags); + /* Handle overflows */ + if (clock->count > 0 && clock->last_sof > sample->dev_sof) { + /* + * Remove data from the circular buffer that is older than the + * last overflow. We only support one overflow per circular + * buffer. + */ + if (clock->last_sof_overflow != -1) { + clock->count = (clock->head - clock->last_sof_overflow + + clock->count) % clock->count; + } + clock->last_sof_overflow = clock->head; + } + + /* Add sample */ memcpy(&clock->samples[clock->head], sample, sizeof(*sample)); clock->head = (clock->head + 1) % clock->size; clock->count = min(clock->count + 1, clock->size); @@ -605,6 +628,7 @@ static void uvc_video_clock_reset(struct uvc_clock *clock) clock->head = 0; clock->count = 0; clock->last_sof = -1; + clock->last_sof_overflow = -1; clock->sof_offset = -1; } diff --git a/drivers/media/usb/uvc/uvcvideo.h b/drivers/media/usb/uvc/uvcvideo.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; u16 last_sof; u16 sof_offset;