From patchwork Mon Jan 9 10:00:48 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ricardo Ribalda X-Patchwork-Id: 40770 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:4e01:0:0:0:0:0 with SMTP id p1csp2068736wrt; Mon, 9 Jan 2023 02:04:17 -0800 (PST) X-Google-Smtp-Source: AMrXdXtXYqkI5YO5hqNyi3iPh7OiUpVzKbM2jox7WG2UtGh2OHScMK7Z5CRMaH/bQsAa0xyOTwVg X-Received: by 2002:a17:90a:930f:b0:225:be98:f5b5 with SMTP id p15-20020a17090a930f00b00225be98f5b5mr59397606pjo.23.1673258657341; Mon, 09 Jan 2023 02:04:17 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1673258657; cv=none; d=google.com; s=arc-20160816; b=DOWsUALQEYCHoXgq3CSKmCA1XUyvDlu4Bvu+lYz2ldFuT56qIVBdH+HE6pI5Rblxhq qRVa0m5WqbqozNECWVTOG9GDWJzUX1A2SYvZ3qucpYHeo36K8X7lIeayRLRTyrAGq77O iMALEo9Y+6kQo2J0sGfsT/Ii/zagaxH3dFPBl1CMUgcp0Kdeb4d7OoJ4eNILOk1w67Ms L5JRMXM/LKNCegrWcI7IR47zoO1C61kAUYwiAWFUmK5uDJsOnFuVtxhzkH0djlQsOAxm vPpOPlB5Xl1E+S6BaKkRtD22INcoLEyoMpeoMasurw3vBSmgyUqcBm+UkeTDC1wy8bIq +n8g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:in-reply-to:references:message-id :content-transfer-encoding:mime-version:subject:date:from :dkim-signature; bh=MX1dxoa9iuYVNRE72fLmQcr9R1KY5ieZzlTHXMPxmGU=; b=fyHuysNGZbpjABVgmyKZ1KvG/qtN/56anJ3IkNW2p6Mpt4QIiK1snbn2Ia4W0f3fOB kHnHXknZkwJ/b/H1GPdlNQXWFpS0XCMAMf/jBO9Iud/LL4ZRDMPekFYNqkYD0kzEjHQQ qRxQhnamqyYXpVANRWxSg8jmkHnHGAabPvERed7pGGl8HAs1c4kPM45Q+OGBYrLHhuPw wO+xciSl5eDiOpPU/QbQ6DHhCGHSPTMnHU9YcTm8M+jqEBykULShL33zD0zi6HMLAiiF b+p3ebgQeTChaDGTQQFdkNtNNfdG+unkS1NhDuX04FI08tzuKJwnkdkw21dr0Zq3YH7o BBZA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=Ti4ccvK3; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id lr17-20020a17090b4b9100b00225ddc6a8bcsi13213855pjb.166.2023.01.09.02.04.05; Mon, 09 Jan 2023 02:04:17 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=Ti4ccvK3; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233238AbjAIKCn (ORCPT + 99 others); Mon, 9 Jan 2023 05:02:43 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52030 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S237054AbjAIKBW (ORCPT ); Mon, 9 Jan 2023 05:01:22 -0500 Received: from mail-ej1-x634.google.com (mail-ej1-x634.google.com [IPv6:2a00:1450:4864:20::634]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1B53D10FDF for ; Mon, 9 Jan 2023 02:01:15 -0800 (PST) Received: by mail-ej1-x634.google.com with SMTP id fc4so18633846ejc.12 for ; Mon, 09 Jan 2023 02:01:15 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; 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=MX1dxoa9iuYVNRE72fLmQcr9R1KY5ieZzlTHXMPxmGU=; b=Ti4ccvK3N7Xm22NUnU7Dj9Attzqf0Q+t5UgDYkdVtjVSM8frwdgoMmQZtvqPLbUsOx StW6tFL6QwyBnFQg7E40gvhHb3OcJK0xQZt5rXSMvWk7x59FRe/Rr9HLwDifkwyv8pmf Oq+QmzbeAOAoJO4Nd2tsBLi0DBRk7wrpVI3PI= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; 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=MX1dxoa9iuYVNRE72fLmQcr9R1KY5ieZzlTHXMPxmGU=; b=DyJ5ErECgVNfXNq4P9bc9+a9wv6f08wRNeRY0mA4CD9I8mF/BpfOJx/Yt0y77qvCcJ Whxg13ZEo7F6WPqe0HrbwtYWPBB2XrE4Hnew8w3m8qeqhUvJaGJI8CzCoMhcnPdUkkkT g9W+582TObBdrOGZZcRaQqVlIhb4DeRt8v+yQB4xOCmgIKYfMHyPQQ2pWZRkvNOyK7VU c3FZyhoAv5UtGM0jUB6kfFvXTTer1lCX+AijXYbwrxg5CxULVVzlnhkkk8BOAgUoqxYE fnz1nA1RG1KV5E6wSp9unXNOa8BB0EMzNc8tQT4OYR6WW/3kv/b513jEhnqV+e40AGI/ Lw4Q== X-Gm-Message-State: AFqh2kp6HNNg+YZjve/SRmXfaPyWaxJv+pv0YVlAwo5N3FX7YDma0pQU gNxwxY7i0SA1vQt9ceWsDJwt4A== X-Received: by 2002:a17:906:d18f:b0:7c1:80d7:55f2 with SMTP id c15-20020a170906d18f00b007c180d755f2mr63974330ejz.48.1673258473691; Mon, 09 Jan 2023 02:01:13 -0800 (PST) Received: from alco.roam.corp.google.com ([2620:0:1059:10:cd22:9dd:6d54:8488]) by smtp.gmail.com with ESMTPSA id bg14-20020a170906a04e00b007c094d31f35sm3557548ejb.76.2023.01.09.02.01.13 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 09 Jan 2023 02:01:13 -0800 (PST) From: Ricardo Ribalda Date: Mon, 09 Jan 2023 11:00:48 +0100 Subject: [PATCH v4 1/5] media: uvc: Ignore empty TS packets MIME-Version: 1.0 Message-Id: <20220920-resend-hwtimestamp-v4-1-a8ddc1358a29@chromium.org> References: <20220920-resend-hwtimestamp-v4-0-a8ddc1358a29@chromium.org> In-Reply-To: <20220920-resend-hwtimestamp-v4-0-a8ddc1358a29@chromium.org> To: Mauro Carvalho Chehab , Laurent Pinchart Cc: Ricardo Ribalda , "hn.chen" , linux-kernel@vger.kernel.org, linux-media@vger.kernel.org X-Mailer: b4 0.11.0-dev-696ae X-Developer-Signature: v=1; a=openpgp-sha256; l=3550; i=ribalda@chromium.org; h=from:subject:message-id; bh=/aomtSJPanXG7kRlpjiU1jzPOL/grSL16QkCbPouDCQ=; b=owEBbQKS/ZANAwAKAdE30T7POsSIAcsmYgBju+XghMh2GfOrpQ6sHWLkQ2wcMZP1nMeOhm7hvyt1 1DbNAOiJAjMEAAEKAB0WIQREDzjr+/4oCDLSsx7RN9E+zzrEiAUCY7vl4AAKCRDRN9E+zzrEiIR2EA CL3oS0zc9eDP+MZo3ZBNw0MPVAkbbgpjkWGBh/lb8zKA3AZpiXCQWgjbHAQkACS7IbU23pdKmbjB3O Co+k195+xwrXzTmIP8z0ly35cV9gGco/U6Yor3b5hxeWXZtRrQAkH4v1xSUXr5uG68p8/txrLlnI6z lG2cVlO+c9FCuwAZejAjzTjpANKtpl4024qeuNlkoBdqyOMUNC+UDcFamlA0epd1pcE3pNgFT1P58L LwfmX6vf+zQ8ridaNxj6NB5kakQkX2+5FUSFErc6HRlf6x/iXEEZGMtHUmmb2o60gorlCXFQcD3UO+ u4QpKPfEYmaU0t+nnmmXVQKpubuL45F9u5rKxzP7v/CI2Y7VZgHxjyk2M3o8oEJvJ4pUc+a1rh+iH9 huuG/Z62ISUUojosyOJQ6gXwyD8ltAtK+n+qtgoziaZdy2FWvRm6eJJ2itwufq5yCSldLK/Tq0MUhb lrg/12VpPmNlqc4n+gmlpXYb7eDyzceYkx7yu5+13YIH9few85COLCsdfCQpTFlBXQ7fjd0JOZhV8z Fl6pTWkB1HHJFxJSXfUrzCswrTWkGJE5mH23ySSFvMpIpqs1r0FKeO69kaMC0qDByw31Vjb2lnwOMN ty786YmiutDigG/EEv3JPsZvvQtdZpY4VD0imPcEUYFrhx15pqR9Xn0COrcg== X-Developer-Key: i=ribalda@chromium.org; a=openpgp; fpr=9EC3BB66E2FC129A6F90B39556A0D81F9F782DA9 X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1754538869847634624?= X-GMAIL-MSGID: =?utf-8?q?1754538869847634624?= 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 Signed-off-by: Ricardo Ribalda --- drivers/media/usb/uvc/uvc_video.c | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/drivers/media/usb/uvc/uvc_video.c b/drivers/media/usb/uvc/uvc_video.c index d4b023d4de7c..710165502b5b 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,18 @@ 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]); + + /* + * Some devices make a borderline interpretation of the UVC 1.5 standard + * and sends packets with no data contain undefined timestamps. Ignore + * such packages to avoid interfering with the clock interpolation + * algorithm. + */ + if (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 +577,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 Mon Jan 9 10:00:49 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ricardo Ribalda X-Patchwork-Id: 40767 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:4e01:0:0:0:0:0 with SMTP id p1csp2068594wrt; Mon, 9 Jan 2023 02:03:55 -0800 (PST) X-Google-Smtp-Source: AMrXdXu6xhKymLvqoohzRGcCl6j/bwWy8Boyq+QLaqHLor7BSghIRz7TIb7cbpwb4yr93pi6m8sf X-Received: by 2002:a17:902:a70c:b0:187:1b7a:6930 with SMTP id w12-20020a170902a70c00b001871b7a6930mr64080152plq.6.1673258635325; Mon, 09 Jan 2023 02:03:55 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1673258635; cv=none; d=google.com; s=arc-20160816; b=JUafRYDPV4EpGw1oqhFjX9AIE8kkcb+rjrcomA/ntkOEx2Ajprrtk1msptZ8rSVq/o ZVhxZR84Byi+PoSdma3B3WnIwEGGh9CEk9iUDCtUmZWkhpWkZtNsE1J/DuQbrNukBTW6 vKM4DQ8l/QRCiOJDzuN/XxqtqU27TXxJV0h3/i46lU417elOIiLo3AlZXrJWkjWNMJso 9gCTXITpF3UNcsLm7d1WDSyOeFy2GAdziYXFzkJvvT8xbcvYfmcK8+6o8v6Uq/YE5m4X Wjos/BrvL+Wu+RxeW6KqJgRUrb1AsgukZT/Au6dAHe+sRRuAaweTTFkVY0hQma2dhVFV 2KZQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:in-reply-to:references:message-id :content-transfer-encoding:mime-version:subject:date:from :dkim-signature; bh=xvjHFoIRPinuYhuTUwGZOQFT+hEiktYHj7+SHztYcQc=; b=Jj88kiABwcvSj24s4T3ta8wDGNozkC2wKMqTCEn3XEI7jgBNS7wmiZK7Lb9kuvyJsM d3adUxcs1PEWb8wXx8wrVFK8aABtlRwO9oD9vxSmfUszYKCcKDZEKrSpLlEdWTxedd8o tRS57Q2IvPQ9GTATMX+ySj+RjnzY1769iGA3dePKTJsEfqp8utq/PbPT5M83CA7FAlHN 4zhTsi1rE6bhRrSz8gKHQSa03oDTqlr0bxqvA+5zwunwvgs5VSpS2H9EBGppGZLQqWOt sMO/UhO6bU0MkOnlm5MhQT5qQfSw754ZzJrVLtERHE76JJSj91c3OUC4P0Wq+AGy8Xjo I0Cw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=MNP6VpTz; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id l18-20020a170903245200b001930d8a8352si9590992pls.282.2023.01.09.02.03.40; Mon, 09 Jan 2023 02:03:55 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=MNP6VpTz; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234471AbjAIKCQ (ORCPT + 99 others); Mon, 9 Jan 2023 05:02:16 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51346 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S237061AbjAIKBY (ORCPT ); Mon, 9 Jan 2023 05:01:24 -0500 Received: from mail-ej1-x62f.google.com (mail-ej1-x62f.google.com [IPv6:2a00:1450:4864:20::62f]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 186D711800 for ; Mon, 9 Jan 2023 02:01:16 -0800 (PST) Received: by mail-ej1-x62f.google.com with SMTP id vm8so18704815ejc.2 for ; Mon, 09 Jan 2023 02:01:16 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; 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=xvjHFoIRPinuYhuTUwGZOQFT+hEiktYHj7+SHztYcQc=; b=MNP6VpTztVXmmbuGde6f7sg2tCgkNWgruLxCshIXHGMgr72O+XAxkdM/OHgrDEO0tn /Ugssz5NHL1356jm6jc2UbZMX6aX67JzM2St3ktgyHrSaZExd/Ceim4w8puc6FiyHCB6 /zVeM4QkZ3P/Gs4rNXXJMBbZdViMfN6EowzTw= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; 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=xvjHFoIRPinuYhuTUwGZOQFT+hEiktYHj7+SHztYcQc=; b=UKpxO9jprudx5u3Ar8WEWnyTUjBiUG0acsotmb4EG/a+bt16jx4Mv5tTGNIhk5DmCD 9GQ6XJMfYMPNHBpVPAyfkbM/MRXxfX9+5dIbFK47dnE3JasTr8GZyW7EBq83YEOW4+t4 IXICTGhHXDuvWn4cVVnEEMMmx/V524HqV3jbIZ6n84KJIi9NUBgIvVqlT3If+m1m6W+L OBb/DYzhG1bAuq8CsiiIKKANIExZslbCxCOzZRy7hEEAaWlOqphVBAqjNv1lPQNYI/dt xOSO4gFadq0dF/hT6u9uriFejASCAnBTV662UhZGXztzPtBxzd7yVSnlAciobWaISSp1 soow== X-Gm-Message-State: AFqh2kqLnaf/M7qpsF+1h13psMB7hA6g5lTZk8V3/MI7QCAe/yTpugg3 jQZ+nOnz2ijCB62fgu0o7QvjAw== X-Received: by 2002:a17:906:ca56:b0:84d:363c:888b with SMTP id jx22-20020a170906ca5600b0084d363c888bmr5395629ejb.58.1673258474631; Mon, 09 Jan 2023 02:01:14 -0800 (PST) Received: from alco.roam.corp.google.com ([2620:0:1059:10:cd22:9dd:6d54:8488]) by smtp.gmail.com with ESMTPSA id bg14-20020a170906a04e00b007c094d31f35sm3557548ejb.76.2023.01.09.02.01.13 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 09 Jan 2023 02:01:14 -0800 (PST) From: Ricardo Ribalda Date: Mon, 09 Jan 2023 11:00:49 +0100 Subject: [PATCH v4 2/5] media: uvcvideo: Quirk for invalid dev_sof in Logitech C922 MIME-Version: 1.0 Message-Id: <20220920-resend-hwtimestamp-v4-2-a8ddc1358a29@chromium.org> References: <20220920-resend-hwtimestamp-v4-0-a8ddc1358a29@chromium.org> In-Reply-To: <20220920-resend-hwtimestamp-v4-0-a8ddc1358a29@chromium.org> To: Mauro Carvalho Chehab , Laurent Pinchart Cc: Ricardo Ribalda , "hn.chen" , linux-kernel@vger.kernel.org, linux-media@vger.kernel.org X-Mailer: b4 0.11.0-dev-696ae X-Developer-Signature: v=1; a=openpgp-sha256; l=5225; i=ribalda@chromium.org; h=from:subject:message-id; bh=FQpt+pfRHossLzPpk7WZbwMO2zMVjYkgEDfy6RwkwCA=; b=owEBbQKS/ZANAwAKAdE30T7POsSIAcsmYgBju+Xi9KJLwkoFnzocf5Tv+v23shGAduGgBn6Dp6Dr OFH/mw2JAjMEAAEKAB0WIQREDzjr+/4oCDLSsx7RN9E+zzrEiAUCY7vl4gAKCRDRN9E+zzrEiGb3D/ 9bq6WciES6pVW4X78EZvp5qfPLwUh2eGty8FNFhUYbC2NI1Jz9qU8zax0TPuzH0V03OgxWZE1DMa+B 4m/JOWLudJwtMdmVM40jtSnwVTh8Ie7HCU818AXu+3mqr6ZGQHp5MuhbciQhRzJJ5pjOgU5brFgDBg CnfpX1JvCGBc+Nu20JtJKJb2anDx2E8aF/+dJRsSp4PYvKiI9+eMCUxZaQP3e6tcAVcGN28WTfg8tc zxV/hs889TMfbLSYOSIDQMgOOmHFA9evj46ePcQ244DqGoST4g8ZEHAwueAIBHunBTWlTZeXMIZU7X yhuVQEjIwvQaUIcXTUE2/fPy2KTS9g2XyUiQu6TWuSAKRZyd0DtrBgSmmSmT/AA1h2pSIm6EP4Uof5 2tGY4saqS19UqOYyPYK4xKT5+KZkbKzL9hozjoEd/MFgKYq2Loksg16NSJi6bq8E50ob2PeTs37sa/ Z0fOdq47G9mXv7lzP7zlri6UyjCSSZxviothS7/OjJ0/HrLLbQRKl9h3JgJ3EKfVpnHtF+1qKYe3W3 fi1SOWHfoBGWQH6gbZdAIC9VRjxCJRzoFXgxSVzoB+mTl9cAb+alSHerEDFcTcHA3suQLqGtiMf8C1 C2t/cokh1tzM2dYII0ZdmQWdims303apemmXtm/8M1PNXPmC8cJK68905iJA== X-Developer-Key: i=ribalda@chromium.org; a=openpgp; fpr=9EC3BB66E2FC129A6F90B39556A0D81F9F782DA9 X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, SPF_HELO_NONE,SPF_PASS autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1754538846950295066?= X-GMAIL-MSGID: =?utf-8?q?1754538846950295066?= 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 Signed-off-by: Ricardo Ribalda --- 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, 17 insertions(+), 1 deletion(-) diff --git a/drivers/media/usb/uvc/uvc_driver.c b/drivers/media/usb/uvc/uvc_driver.c index c1e3c1fab976..eae88d0f420e 100644 --- a/drivers/media/usb/uvc/uvc_driver.c +++ b/drivers/media/usb/uvc/uvc_driver.c @@ -2569,6 +2569,15 @@ static const struct usb_device_id uvc_ids[] = { .bInterfaceSubClass = 1, .bInterfaceProtocol = 0, .driver_info = UVC_INFO_QUIRK(UVC_QUIRK_RESTORE_CTRLS_ON_INIT) }, + /* Logitech HD Pro Webcam C922 */ + { .match_flags = USB_DEVICE_ID_MATCH_DEVICE + | USB_DEVICE_ID_MATCH_INT_INFO, + .idVendor = 0x046d, + .idProduct = 0x085c, + .bInterfaceClass = USB_CLASS_VIDEO, + .bInterfaceSubClass = 1, + .bInterfaceProtocol = 0, + .driver_info = UVC_INFO_QUIRK(UVC_QUIRK_INVALID_DEVICE_SOF) }, /* Chicony CNF7129 (Asus EEE 100HE) */ { .match_flags = 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 710165502b5b..ecd3c1292182 100644 --- a/drivers/media/usb/uvc/uvc_video.c +++ b/drivers/media/usb/uvc/uvc_video.c @@ -541,7 +541,13 @@ uvc_video_clock_decode(struct uvc_streaming *stream, struct uvc_buffer *buf, stream->clock.last_sof = dev_sof; - 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 = usb_get_current_frame_number(stream->dev->udev); + time = uvc_video_get_time(); /* diff --git a/drivers/media/usb/uvc/uvcvideo.h b/drivers/media/usb/uvc/uvcvideo.h index 22656755a801..699c33bc24ed 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 /* Format flags */ #define UVC_FMT_FLAG_COMPRESSED 0x00000001 From patchwork Mon Jan 9 10:00:50 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ricardo Ribalda X-Patchwork-Id: 40769 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:4e01:0:0:0:0:0 with SMTP id p1csp2068668wrt; Mon, 9 Jan 2023 02:04:07 -0800 (PST) X-Google-Smtp-Source: AMrXdXsaWtobUGI4TDcvJE73EiMuyDZbmGJVUVhmnk7cFD950JoNxcqC/BAQsAKjFJgCr7b1Gfrd X-Received: by 2002:a17:902:e291:b0:192:fc7b:2bc0 with SMTP id o17-20020a170902e29100b00192fc7b2bc0mr12854200plc.13.1673258647150; Mon, 09 Jan 2023 02:04:07 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1673258647; cv=none; d=google.com; s=arc-20160816; b=wfwptFuoSwUMmvrgWxJlvpwcOaVt9RdjIbmZDmODFb1zRr14L+QLmELXPxFGEdN/Pb T6MAE/pENYszT6yLtw2xkihEluUZkutc1MUqMXxG/kesT9fLC9Rj/aoWgUOKK5cSrTYs o4QLRbi+qayBdmcJeUAfDBGpzlljQuHb5kjIFehlPw9kxJuDKan2rOPVordOICfnixWT i/WOtZCBtjFiyQg86Lt9JV6P4t/EZlCQZUtKEY9cxOypsd5fsyrieLCYCrfA564Tu/fP jl3cs+HZQafiVnDPY9qojH879aV/kdRei7nV8qHFna/rIFdgaY/l+ShPduSLJ1y/3ria 0aFg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:in-reply-to:references:message-id :content-transfer-encoding:mime-version:subject:date:from :dkim-signature; bh=JEk8qL9j9GreB3Z0ydDIFUEO1M43420PznTeKLvJ2ps=; b=qRDSf8K57w3kDiC6p4rgcLXJxQ2uXj6U+IPUwNqXk8KhAIaXagFGLXdFDS+SJm+DZ8 OC1CKspyJYjASXPlShtBpEDoOn1NrdkPySUNjj7Or7B9l62Z1+UE278UYMvptV5beAuE QVCpxsybmlvUnRc2MdL4j2TfRMg4hvS+zn1h0pILYhmHCb1Cqv1K97EDzAI+xx/BY/dv HRLq04y9RuyEYRMFfpGO/pTdpRIzI4dt9LkKt+gJl5sg3ji7JtA9oF33vdFfgmeb0hO+ s47bLjLaSnYMTqa1zlRfp7tClaQt0JME0UE6/aNiULkeYauD0ycUy1MS0b5WOfv0vpNP gtwA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=XJdAfwMn; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id q10-20020a170902daca00b001926464f82esi9964994plx.298.2023.01.09.02.03.54; Mon, 09 Jan 2023 02:04:07 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=XJdAfwMn; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234207AbjAIKC0 (ORCPT + 99 others); Mon, 9 Jan 2023 05:02:26 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51368 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S237074AbjAIKBZ (ORCPT ); Mon, 9 Jan 2023 05:01:25 -0500 Received: from mail-ej1-x62d.google.com (mail-ej1-x62d.google.com [IPv6:2a00:1450:4864:20::62d]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 79738219D for ; Mon, 9 Jan 2023 02:01:16 -0800 (PST) Received: by mail-ej1-x62d.google.com with SMTP id lc27so9402063ejc.1 for ; Mon, 09 Jan 2023 02:01:16 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; 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=JEk8qL9j9GreB3Z0ydDIFUEO1M43420PznTeKLvJ2ps=; b=XJdAfwMn2pdrHDGnck6otnv5zpVNRzetTCVSx3svDns+hEt7NRPRjuZU5/n1/4DEry qYMp/ALcMh4pyjuFAGTMebOItpWF2ck5JPg401/Lj/EmksS/RmU2qTvukRvpUfHR0HTP UpXGSc4G5XhCG8BxGNVwfu4cLxLOfI/C3NwaE= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; 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=JEk8qL9j9GreB3Z0ydDIFUEO1M43420PznTeKLvJ2ps=; b=6UGC0ZQIPic6Va4e6Un+M47zO2Mg8og5H0mlK9OJoTDD0XURCVbgHfLMTC8o0ra0Gn DTY26GByTtMCKZS10ns1IREFKMqkz2RZcjxnQYL7//al+ErHt+bFTvoM9OCQwcRc+gD1 E4vY2s2DY3LQ1wy7hKJDBSzPpVKfsY7C7IlJ1rnInYRqjhiuzsSovdcnGmp9adoxtyg6 krBm9ARdfIxaqL2dK/g21fO1y/opdiMMXvjYNQFrvyo0bSCoXpCNvBpiRdlQ59AVCQcj mGy2XsCp8uaiHuAXlshS2JcT20ou5pqM75W1/bFzujiof/hw+a9mZXMlhNmk0AOq/wB4 mMsg== X-Gm-Message-State: AFqh2kr0IQJGl7SAFu+w1uOCpwoBBAzTCb8jZIlXxopeSXHfVB0jIsjj Q8ql5jliZ8eMk1iOXQYzdP2paeYC6JZHHZPy9Tk= X-Received: by 2002:a17:907:c606:b0:84d:456f:91b7 with SMTP id ud6-20020a170907c60600b0084d456f91b7mr3477815ejc.43.1673258475483; Mon, 09 Jan 2023 02:01:15 -0800 (PST) Received: from alco.roam.corp.google.com ([2620:0:1059:10:cd22:9dd:6d54:8488]) by smtp.gmail.com with ESMTPSA id bg14-20020a170906a04e00b007c094d31f35sm3557548ejb.76.2023.01.09.02.01.14 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 09 Jan 2023 02:01:15 -0800 (PST) From: Ricardo Ribalda Date: Mon, 09 Jan 2023 11:00:50 +0100 Subject: [PATCH v4 3/5] media: uvcvideo: Allow hw clock updates with buffers not full MIME-Version: 1.0 Message-Id: <20220920-resend-hwtimestamp-v4-3-a8ddc1358a29@chromium.org> References: <20220920-resend-hwtimestamp-v4-0-a8ddc1358a29@chromium.org> In-Reply-To: <20220920-resend-hwtimestamp-v4-0-a8ddc1358a29@chromium.org> To: Mauro Carvalho Chehab , Laurent Pinchart Cc: Ricardo Ribalda , "hn.chen" , linux-kernel@vger.kernel.org, linux-media@vger.kernel.org X-Mailer: b4 0.11.0-dev-696ae X-Developer-Signature: v=1; a=openpgp-sha256; l=1843; i=ribalda@chromium.org; h=from:subject:message-id; bh=r85cn+Y4mbCQMerG0X3MvBxTRSX4Epxv+PdQFezSnqU=; b=owEBbQKS/ZANAwAKAdE30T7POsSIAcsmYgBju+XkSYv+U+WUJBpzTWS/iUEB+jp4iL++42VSNaHb F3WjEWKJAjMEAAEKAB0WIQREDzjr+/4oCDLSsx7RN9E+zzrEiAUCY7vl5AAKCRDRN9E+zzrEiOmDD/ sFXcwgZPzie69FGlZ6jzRbqrsZu35Zb2W740oOyD+CPsUTEepeeX90a+7X7cRI6JzTc6ZZ+j8nayJT /dC/3QpQLu4lVU+7PX0iewavI2oDS/v+fOi5EhFhuVFfzwFTr1yjaUNC68cIwOZOYKxWduDJQWm6ww cw5sEORRp2GQTZ3PSi1WAIlRdeoHuSv9ZeCk/Q2CO69kVruldiJfUSdtoBl2Lt+kEvozGmQRecZWN7 SnALGg4C5y+qrOKqsN9fdX+g+hpAqmqZowi3qGAeGllEWYsrhptbff1FvY58C1AVMqgF+nxvoDe7Cy 3DdXwNvpFG9IiDh/cC0gY8+1Qup/qISQaYEp7j7yajxjpUYk8mAx6BKZ6GBLt2SJjc3J6BHnmVdnPz ay3SYd4yq/NR642BGH91b+zvFOe8fw2trbE3GxvQAQ3Wko89zJ+iS5BVzumDarQQouXynqIhF+tTEX XW6exrvXYAAlEizpOc2UazbPvCJV4hyY3Hom+QXkxV5esksW9J/0ARCA0i+olCdkz5KfKCngco6oYW dYL3mSwLDggoIh/32GsH6Pmm7Oz2iptHT9gO9gmek2oizXdGzHJxxik8a8vn88JwE8dw/h5zv881/B E2hJQxZVIZNNzbKupjuKdom3X4zUvzshKQUnjKHjgr/rYcFWwcvEH7R0P8Iw== X-Developer-Key: i=ribalda@chromium.org; a=openpgp; fpr=9EC3BB66E2FC129A6F90B39556A0D81F9F782DA9 X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1754538859240952579?= X-GMAIL-MSGID: =?utf-8?q?1754538859240952579?= 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 --- drivers/media/usb/uvc/uvc_video.c | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/drivers/media/usb/uvc/uvc_video.c b/drivers/media/usb/uvc/uvc_video.c index ecd3c1292182..ee0f40b9eae3 100644 --- a/drivers/media/usb/uvc/uvc_video.c +++ b/drivers/media/usb/uvc/uvc_video.c @@ -747,10 +747,10 @@ void uvc_video_clock_update(struct uvc_streaming *stream, spin_lock_irqsave(&clock->lock, flags); - if (clock->count < clock->size) + if (clock->count < 2) goto done; - first = &clock->samples[clock->head]; + first = &clock->samples[(clock->head - clock->count) % clock->size]; last = &clock->samples[(clock->head - 1) % clock->size]; /* First step, PTS to SOF conversion. */ @@ -765,6 +765,15 @@ void uvc_video_clock_update(struct uvc_streaming *stream, if (y2 < y1) 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. + */ + if ((y2 - y1) < (256 << 16)) + goto done; + y = (u64)(y2 - y1) * (1ULL << 31) + (u64)y1 * (u64)x2 - (u64)y2 * (u64)x1; y = div_u64(y, x2 - x1); From patchwork Mon Jan 9 10:00:51 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ricardo Ribalda X-Patchwork-Id: 40771 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:4e01:0:0:0:0:0 with SMTP id p1csp2068760wrt; Mon, 9 Jan 2023 02:04:21 -0800 (PST) X-Google-Smtp-Source: AMrXdXvLkpsXytO8TpDcuHYSSswizACmvJI8yHV+WZ9nJ59sovG9D2n7+wTe7oX6yQ/PMFEE5CoO X-Received: by 2002:a17:90a:7503:b0:226:c196:db55 with SMTP id q3-20020a17090a750300b00226c196db55mr17653974pjk.18.1673258660949; Mon, 09 Jan 2023 02:04:20 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1673258660; cv=none; d=google.com; s=arc-20160816; b=vBjHH7YtHLbLbu4pia+IjhyqFfhrTievqFtLCFcLASTNGnZ6CofdUhW9njdDUOQIFj IYUuiikl6xH0+UO4oszBz1hgRrgJe4/d4SIEo6exW1u3rlOl2eN1Nd5agRMvNTe7VXQw KKMk8SMtbqSZJvCklm3zhnw2FQeN9Ic9SvzFLIFqQx3LMqXfrIxsjKR72zxUl3nZa3S0 ECyaHW308yw20W0Hm0ncPdn3eUiei1pg5f68lDhDyoPTQpvd8cZh7TP1O2RuIMIkY6n+ f5VBjdkvs7rGEkDrWs/vBtMQ4c3Cm7wWX/oOr8M+/P2r5LDnuceSghFCHNu8ZjM02pPh A9qQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:in-reply-to:references:message-id :content-transfer-encoding:mime-version:subject:date:from :dkim-signature; bh=IYmriAjuARTqoa8gks/FWwbBbDlG7rw+znorcPY4O/c=; b=JK7QrzgU3cY2JmOJrIulOjSfO0KWl3f3Zv7VRZaTcTZubueR7u9pbqKOJM3k8PzcV5 PpcOTx+JJA1iVlWJjSuAZR7H6fGHGjfyEx4bX6buazPN28V3DSmwSieRlRWKPUn4AD3W oyOdJ1NTKY2R78J2rzXAtLoEKrUaCyUEU7bh4NDOR12DTlkPI83AkdiWPef1g5ULYtTy PVYPeYkKjsNyddj93IYEw7mxI3OWH+HqpQg7lehP9gkhhdbTb1uYCfKGa/e+DkaBfary oOOAIBAijIaa4Q2qInRW06AFUSMojo011A4c8W4eIlOwNS9JABHqdFhvGyRvv5oEkXSO UGzg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=e8d7HA3B; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id n64-20020a632743000000b00489a4cba34fsi9253005pgn.21.2023.01.09.02.04.07; Mon, 09 Jan 2023 02:04:20 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=e8d7HA3B; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236536AbjAIKCq (ORCPT + 99 others); Mon, 9 Jan 2023 05:02:46 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51358 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S237068AbjAIKBZ (ORCPT ); Mon, 9 Jan 2023 05:01:25 -0500 Received: from mail-ej1-x633.google.com (mail-ej1-x633.google.com [IPv6:2a00:1450:4864:20::633]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 7995612770 for ; Mon, 9 Jan 2023 02:01:17 -0800 (PST) Received: by mail-ej1-x633.google.com with SMTP id jo4so18692836ejb.7 for ; Mon, 09 Jan 2023 02:01:17 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; 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=IYmriAjuARTqoa8gks/FWwbBbDlG7rw+znorcPY4O/c=; b=e8d7HA3Bqkout7TJIcxnPsRrw7QTdH390nDfdq/xs+BxeMotE3+kv01NE7PaKD3JPL 124LEJofpGeAyqm7P6z1F8/XhF4XT82iWbtdoGqXnPoDy8C6Zy8K14/Q1N/0ZSqhiRHT eHJU1SNcNVYHCZVX/cX3fRh0wGISFPVWiRu04= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; 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=IYmriAjuARTqoa8gks/FWwbBbDlG7rw+znorcPY4O/c=; b=AhoiWy74L25PtlGmbgXFjmC06asj38j2wdcWgP2i3fekMI/OH0bYcJL9HyXlsRLuIk iiSmxkzr6Zl/wowxeoSUPLOaOWxr1JD70XlNo72fsilGXcfsER0FM9tUT9EgcFMqW+B4 ISiIhoiXOpP/uQ9Av7+3cF6QjYg4TiNlxUPkgvl6iYtRm2V6RIuISBbh4CyRvMN57dnu 7GkMkBMQzNHDMVz6R4rIbq2gkFJsE0qyM9uiLoEVxBSdTUaqi8G9ozO7/DNDmj7h1Dpo p/lxH71kKK7uEBtaqGRtlAAcZrxwdFjCW5lU0WxXpxIPGp0Unl54Va21cVHy5MkvXfyZ qwnw== X-Gm-Message-State: AFqh2kqhJPK0aCzpTL5epTQFI5PtbMAHSDJjKLbgM1tQdZQoxSD3nHtv /tPY0Ly781pMlYDypqzPZFqn2w== X-Received: by 2002:a17:906:a842:b0:816:ef2a:631a with SMTP id dx2-20020a170906a84200b00816ef2a631amr54776548ejb.31.1673258476257; Mon, 09 Jan 2023 02:01:16 -0800 (PST) Received: from alco.roam.corp.google.com ([2620:0:1059:10:cd22:9dd:6d54:8488]) by smtp.gmail.com with ESMTPSA id bg14-20020a170906a04e00b007c094d31f35sm3557548ejb.76.2023.01.09.02.01.15 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 09 Jan 2023 02:01:15 -0800 (PST) From: Ricardo Ribalda Date: Mon, 09 Jan 2023 11:00:51 +0100 Subject: [PATCH v4 4/5] media: uvcvideo: Refactor clock circular buffer MIME-Version: 1.0 Message-Id: <20220920-resend-hwtimestamp-v4-4-a8ddc1358a29@chromium.org> References: <20220920-resend-hwtimestamp-v4-0-a8ddc1358a29@chromium.org> In-Reply-To: <20220920-resend-hwtimestamp-v4-0-a8ddc1358a29@chromium.org> To: Mauro Carvalho Chehab , Laurent Pinchart Cc: Ricardo Ribalda , "hn.chen" , linux-kernel@vger.kernel.org, linux-media@vger.kernel.org X-Mailer: b4 0.11.0-dev-696ae X-Developer-Signature: v=1; a=openpgp-sha256; l=6607; i=ribalda@chromium.org; h=from:subject:message-id; bh=gd7LIKTyyDLtaDNUSPifHorCJvktQIQVilx4JFUvgoI=; b=owEBbQKS/ZANAwAKAdE30T7POsSIAcsmYgBju+XlwWqnAmWHCIg0P35z3vMJsHj5JcVP1XuXTd8i djmoZDuJAjMEAAEKAB0WIQREDzjr+/4oCDLSsx7RN9E+zzrEiAUCY7vl5QAKCRDRN9E+zzrEiF56EA CakfahkFXDSi7hY6hh0eX/zeV1DmgRo1xbW5YdJ7GOnUEYCTc87Fj5ywfuOr2vnsFjBJSgjb1+ZqEE HgVyFmBlBldXB2D3LG4YI96HV5WlKXqPJERLQuVc/SxAPciGCV+y08R8Fgu1yCsBpIbrFQwQliAPNt SJEYGcSXW+1hv3c8dpJx7UTaMjhObGxVbDfHxbHQkEezqiKqHOXWnwLY98MYDEXszyRRdXvv7OICDH 7YDed1kGLy6CHCoRevemj2AL8bnAeeRvmxcTWCLfNUlS0n2jmNur4cTYRQoXi6P65ARN/KJHfcR8Kj BXw4KxR25PEWJltsPTKHOUeSCqaudmLGZN//VQWDHS6LUYqJAXIcjPhIgF4O7IeylnczRu6CZk7v7b QHIwrSDOWZpZV1nLUp50lyuumaeAEq9LfZU5aXSC7Pcf/bYsbPMKkAW1sY/1G722Bx++zAu4yEU5PD +22oF/CVc6OkXZIH7SWjUf3VmLqvpwJP070OU6MDNTCfdefVGIaR1Gz1xZuWzCnP4DN1ws/f+OWwIQ PqYzUwVE08X5tII+kFUvnskbB5Av4YvlfSelhGsFoHbOcGqMniLaxokRtpKGO/2iHETSpIWgix5LbR XZLw5W49hENMNfM/G9Ysi1ft/MaXiME8WdSxWiwPN5CXEJwgAkPGqhNzrcbQ== X-Developer-Key: i=ribalda@chromium.org; a=openpgp; fpr=9EC3BB66E2FC129A6F90B39556A0D81F9F782DA9 X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1754538874032037187?= X-GMAIL-MSGID: =?utf-8?q?1754538874032037187?= 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 Signed-off-by: Ricardo Ribalda --- drivers/media/usb/uvc/uvc_video.c | 84 ++++++++++++++++++--------------------- 1 file changed, 39 insertions(+), 45 deletions(-) diff --git a/drivers/media/usb/uvc/uvc_video.c b/drivers/media/usb/uvc/uvc_video.c index ee0f40b9eae3..02831cf0a00f 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(); } +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); +} + 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 +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 = 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]); /* * Some devices make a borderline interpretation of the UVC 1.5 standard @@ -536,19 +545,21 @@ uvc_video_clock_decode(struct uvc_streaming *stream, struct uvc_buffer *buf, * algorithm. */ if (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; + stream->clock.last_sof = sample.dev_sof; /* * 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 = usb_get_current_frame_number(stream->dev->udev); + if (stream->dev->quirks & UVC_QUIRK_INVALID_DEVICE_SOF) { + sample.dev_sof = + usb_get_current_frame_number(stream->dev->udev); + } - time = uvc_video_get_time(); + sample.host_time = uvc_video_get_time(); /* * The UVC specification allows device implementations that can't obtain @@ -571,46 +582,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; @@ -619,15 +613,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; } /* @@ -2099,7 +2093,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; @@ -2248,7 +2242,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; @@ -2266,7 +2260,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; } @@ -2294,5 +2288,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 Mon Jan 9 10:00:52 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ricardo Ribalda X-Patchwork-Id: 40768 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:4e01:0:0:0:0:0 with SMTP id p1csp2068636wrt; Mon, 9 Jan 2023 02:04:01 -0800 (PST) X-Google-Smtp-Source: AMrXdXv76unPivpJuKPMP24mZzMpguH8A5/PCLmyqB3sr6dZ9XJSvDwmeOvuqaOX3hfVfED/WnBl X-Received: by 2002:a17:902:a510:b0:192:48d1:f06c with SMTP id s16-20020a170902a51000b0019248d1f06cmr59144831plq.35.1673258641583; Mon, 09 Jan 2023 02:04:01 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1673258641; cv=none; d=google.com; s=arc-20160816; b=F/nHea4uOoKWC9QR6Crht5yD3iwg4Vc9cA64AD4iR8ZUBQO9tbqWdw/TqGnAHNWTQN q6ltw6etoDTdnxWyMQAR0f+rIGDZFrR0v3W+uqHygG7c85M1MJz160Lf2yO6veMoH6KH 9mdV8r2EILDhbl1vZTfEr9IVTzrni84R5J0PRn4wbwNt8nVQly13uG/uU/OXB+iemK/C x+I18vBkeksw/gFrxhzYOrGDS2MwKgsNBbQY2Ea7px9zxgdHi/qJgKbNfYhG7Fe9oqG7 KCnwkFMhFxVDbUthcrXg2H3kScilvDcPEDEkz7tK3atvN4i9BJCcBQOZZE4jzmpN6hUY kcwg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:in-reply-to:references:message-id :content-transfer-encoding:mime-version:subject:date:from :dkim-signature; bh=IyPGVf1IA+R6W+BsSMF5B8bTb9iyrBrQc1FUkhnDkgs=; b=T0cjbmzxlU0Wk4MCGgvmGyV3+FgXZ2w77bd31544ilN6BmtBSPRYCeceG5E1WTBXAv 7oR5Gpc7goMpeGooCrS6Yy6uS4zwxozAaUTho9s6bGLVtkW0Xb6V5O8dHyZElb2ibTwN aUya4UHvxUDv2CTHrNksfeROiFaaJ0KtczTcrAg+xrSublX9eNlgTGWAR5cX/vYfYXdr 8DafSwM7L+ejX0F/gvmry4RA7dWF5szoccniFc8qm7JxKrsQmmaH1wpKkwa0/9BHqxpK LOvPPRL5i5xCq0WYWWCRG6WfD45ZtUptAfHyftU3CXj8xTY4R7wcjo/Ha3CLrwc8fcaU dFXA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=UjwDJhca; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id z15-20020a170903018f00b00176c891c8a0si8599110plg.6.2023.01.09.02.03.47; Mon, 09 Jan 2023 02:04:01 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=UjwDJhca; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234302AbjAIKCV (ORCPT + 99 others); Mon, 9 Jan 2023 05:02:21 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52074 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S237069AbjAIKBZ (ORCPT ); Mon, 9 Jan 2023 05:01:25 -0500 Received: from mail-ej1-x62a.google.com (mail-ej1-x62a.google.com [IPv6:2a00:1450:4864:20::62a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 79ABD12A89 for ; Mon, 9 Jan 2023 02:01:18 -0800 (PST) Received: by mail-ej1-x62a.google.com with SMTP id gh17so18714552ejb.6 for ; Mon, 09 Jan 2023 02:01:18 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; 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=IyPGVf1IA+R6W+BsSMF5B8bTb9iyrBrQc1FUkhnDkgs=; b=UjwDJhca+rSCU2rIuiVC7dEcBv08I2zcp7ic+9MaK6LlNKz3C70l+p2vosFLBvv6R2 OGFMKB6tPRSyFRPnHXwdkuzowgisl9ZIctjtSQV/Qog49caGMRLbdautE+72tyFQeuOH POBOSfs6K4fxYna9uzn0pyUh08VI/mpyWbGHQ= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; 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=IyPGVf1IA+R6W+BsSMF5B8bTb9iyrBrQc1FUkhnDkgs=; b=Njjhhlz4AT3uFyKsrlDCQjEvYEGX1RdwGH/AuBIm+CzBFx2JPvo159XSKkeJUX9bkE 3DtgVhVg/VTsvNTVfy2ZGI54yC4R0YiN8eVbg9J62x2D5+QiGT44toVIc0MN9GoaX02t JXrGieE/y6B41SAi3W1UZ0rkoZanVNbGDjqqw23y1VLZjZJ/H+avUxKLnHqMZlsmKqB5 ZnrjWHAjC84HwQuIu3rgkUMsQIN0dpv8Ax206rfUb2TFc8uFHJGE8+sz2fgC1d6iQYlS OHIY8Z6202zfCapQ2PaBX2FSrpkp/Pbgil0RXaVa5krJ4IkLfYdDImmXzXIFL7WebNgZ 46FA== X-Gm-Message-State: AFqh2kqcowmivBFzB0ir7UeizATE1RmTtZ5GqZau9AuV8dE8mYSWkDDG l5JdczW/PwoJlIGubrwgvtFUeA== X-Received: by 2002:a17:907:c68a:b0:84c:e9c4:5751 with SMTP id ue10-20020a170907c68a00b0084ce9c45751mr20704462ejc.74.1673258477023; Mon, 09 Jan 2023 02:01:17 -0800 (PST) Received: from alco.roam.corp.google.com ([2620:0:1059:10:cd22:9dd:6d54:8488]) by smtp.gmail.com with ESMTPSA id bg14-20020a170906a04e00b007c094d31f35sm3557548ejb.76.2023.01.09.02.01.16 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 09 Jan 2023 02:01:16 -0800 (PST) From: Ricardo Ribalda Date: Mon, 09 Jan 2023 11:00:52 +0100 Subject: [PATCH v4 5/5] media: uvcvideo: Fix hw timestamp handling for slow FPS MIME-Version: 1.0 Message-Id: <20220920-resend-hwtimestamp-v4-5-a8ddc1358a29@chromium.org> References: <20220920-resend-hwtimestamp-v4-0-a8ddc1358a29@chromium.org> In-Reply-To: <20220920-resend-hwtimestamp-v4-0-a8ddc1358a29@chromium.org> To: Mauro Carvalho Chehab , Laurent Pinchart Cc: Ricardo Ribalda , "hn.chen" , linux-kernel@vger.kernel.org, linux-media@vger.kernel.org X-Mailer: b4 0.11.0-dev-696ae X-Developer-Signature: v=1; a=openpgp-sha256; l=2429; i=ribalda@chromium.org; h=from:subject:message-id; bh=d3/yxcuzrSsjuCAsXjVx8zgkh4Z01muFu99m52Mqt+Q=; b=owEBbQKS/ZANAwAKAdE30T7POsSIAcsmYgBju+Xmus5qwiM+bWh3XmGUFTqq8M16JB31p20MD+uH OlLPKEmJAjMEAAEKAB0WIQREDzjr+/4oCDLSsx7RN9E+zzrEiAUCY7vl5gAKCRDRN9E+zzrEiM7VD/ 9bKbYtjzUsl9za5kNhGVS3+IUFvGg4JO/yvugtLoQfDMSl5vY1dRQGU5BiLNwoF0u6zltN2+7RbLDg zMvxiNuBs1pH55ubB7wgCtvUJmW8WMUMUpddrvhb2P6w6aYqGTthzg07Yji5aDoySqjVHYdmKOm0sw VMNCDpeK2MGCjpw+5nglsWb6TAfMdiz9Q7pPNs2V30GgcqtdZ2f09oIh25/xAXSkXfpKAx+EnZ7+q5 TbuqW6BC5AopWpwdu0Mwlmz40Jz+IUnDuigBl5AUaHV05iJ4Assr+pwo60iquRf4QYd4TYjnTsqhet DQcGz+Kb4VXBtvvxVNoWDzbceEq7teHvT6RRJS0wcYopENhMkYts8nlfzBTlQ84Q+V6lzIbr0jd9kx BEvFZsv6lmFKwiZAG8x7F/ljVB1MJq9whuoowr/JmjQDotkUiLpVyAdbweIiwtamXdKs+NBsKUYcB3 3HRGudm67BzKvZqWlFahr22IgW2x5YN+e5u3hZbHd6afzNRmh9Kwyunw+yWTfesChVAGje3+lxAFXA mXOsA8YLNN+4djBH2QcwIDipQF+9XTM+jZd+9l7hAcNYYKYgA5ofA6/dIMG9lMeDKXBWTI8jVgZEa0 C8HZjtclgV6j8It7KnZkoZ9LI5XKMKueDgJJ9MsSIR6BmJXHRfCU1b1sswJQ== X-Developer-Key: i=ribalda@chromium.org; a=openpgp; fpr=9EC3BB66E2FC129A6F90B39556A0D81F9F782DA9 X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, SPF_HELO_NONE,SPF_PASS autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1754538853221648080?= X-GMAIL-MSGID: =?utf-8?q?1754538853221648080?= 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 --- drivers/media/usb/uvc/uvc_video.c | 15 +++++++++++++++ drivers/media/usb/uvc/uvcvideo.h | 1 + 2 files changed, 16 insertions(+) diff --git a/drivers/media/usb/uvc/uvc_video.c b/drivers/media/usb/uvc/uvc_video.c index 02831cf0a00f..04f452d95fd6 100644 --- a/drivers/media/usb/uvc/uvc_video.c +++ b/drivers/media/usb/uvc/uvc_video.c @@ -473,6 +473,20 @@ static void uvc_video_clock_add_sample(struct uvc_clock *clock, spin_lock_irqsave(&clock->lock, flags); + /* Delete last overflows */ + if (clock->head == clock->last_sof_overflow) + clock->last_sof_overflow = -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 != -1) + clock->count = (clock->head - clock->last_sof_overflow) + % 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); @@ -600,6 +614,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 699c33bc24ed..f2a7e1507492 100644 --- a/drivers/media/usb/uvc/uvcvideo.h +++ b/drivers/media/usb/uvc/uvcvideo.h @@ -501,6 +501,7 @@ struct uvc_streaming { unsigned int head; unsigned int count; unsigned int size; + unsigned int last_sof_overflow; u16 last_sof; u16 sof_offset;