From patchwork Thu Mar 9 12:55:15 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ricardo Ribalda X-Patchwork-Id: 66841 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:5915:0:0:0:0:0 with SMTP id v21csp278499wrd; Thu, 9 Mar 2023 05:07:49 -0800 (PST) X-Google-Smtp-Source: AK7set+4z/09MEwVOYByfCCvRjV9AnGI/V6VHcvFDfrgOPIVxEI/VCkRCggPIVqA4O/0wsm4XLkz X-Received: by 2002:a05:6e02:1c4d:b0:313:c32b:de26 with SMTP id d13-20020a056e021c4d00b00313c32bde26mr22278512ilg.9.1678367269270; Thu, 09 Mar 2023 05:07:49 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1678367269; cv=none; d=google.com; s=arc-20160816; b=iRSRMe8zJ+8zdSN8MpSNoRENh+aaT7cTn6oQ7c4NC1jBBF33wq6q1QDuBijV0cdlg0 H2RI/AxNRWKV5NqjZhMHs4Ic0aOdbsW7LspGArn8ULntU5lNIBS+S6tv+wuOZmhCaftk nDucbkOhz9XZgsoG3FQHhrVZwGC6z6laWE5YCAkP81nNVT9EJj/cZdPZW7+TasEJ9qeh DMTtb/odwMDaK/ni3tYOii/prw9gNPabl+4wpsE7ILJNW/w72KvUnsIPktemKSsRrSui cuVNq0CkjR9foLTcvTaKTR2NKLyfwtp3S5xAkawFSy12SODYChzB4NNbt8Ais8mtkl/1 5nnA== 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=eniktNp5LyeH77bXTVN24xHTzfOMCc87vcXnyNAQqNE=; b=riA2MYBRfl6AATegzW7Rb5KVYCQ9zV6q+XYFikIdrcuoXUh00Rye5JQr7kNPN4LGZ6 yl/8/TeDyJntxxe5AQFsCsx1WyXdxescZpIRIrf4iA9nClbe3dLhGXSw9cfZ07mZ/8gm 86r1cSbZOMdIICUT/pODDQm2lSiGlymX6csYfNuQy1Vv+9vRtgzIjxhrjL3j6/vDHwUs Y70+riG/vs1/BgLFXscVNqLRYcsuZI5DeP2NiMYothw1qOxk6DWeyO9H+kOfwrASfcPb HoJ+93Z4OUvs9N5KzSfdbdI3iGnzn99hGq1IFGzZkQasjD+wcaHuibkdLOHj5oLfC62c fqIA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=ig2UFTmh; 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 a1-20020a029981000000b003c4ebee591bsi17346611jal.106.2023.03.09.05.07.34; Thu, 09 Mar 2023 05:07:49 -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=ig2UFTmh; 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 S231216AbjCIM5s (ORCPT + 99 others); Thu, 9 Mar 2023 07:57:48 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59926 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230035AbjCIM5V (ORCPT ); Thu, 9 Mar 2023 07:57:21 -0500 Received: from mail-ed1-x52f.google.com (mail-ed1-x52f.google.com [IPv6:2a00:1450:4864:20::52f]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id BEF3CF16A4 for ; Thu, 9 Mar 2023 04:56:37 -0800 (PST) Received: by mail-ed1-x52f.google.com with SMTP id o12so6527928edb.9 for ; Thu, 09 Mar 2023 04:56:37 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1678366566; 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=eniktNp5LyeH77bXTVN24xHTzfOMCc87vcXnyNAQqNE=; b=ig2UFTmhgKy5IkdvxdBJJdPqppgIeg87xUrwvJaM9qJE4Mba/3iZ0mse4/MuPGqOiY AnX8UhWDkUQVs2R6ADhuC5zfV4o/esaGfqjNwZX5Rgf5odzDaowpptM0M/NiikhmFWhj R8atwSCw3bqjgobxfnV7EdTp34ebcQXdBNKWo= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1678366566; 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=eniktNp5LyeH77bXTVN24xHTzfOMCc87vcXnyNAQqNE=; b=w2GL7ybq3T8+wHQvDkN6yUbTk+fEAcKHcXfDTouX329PSwAC/G2KM5ffHAKj8H4CqK UiHesyN5dT6XuXgcdkaRnMa8XV+UfyT5bZWr38x/JDBzITsjDEK9Pm3vHbblW+LtoIZ1 NSRg9ERAB07FnhE8SLvt2AcB+mVIWdRyy1XypcxOLhblK3L6fqslIOYa3yB4uXUGIYp7 Tg5ZCNe3nSFIAydWS4IobsJKmHaKwSVkRfZryPe93Kj/2CN8i56Sa5PQk/ykGzR1K7Je MlWy766fJxzqLr0yd780tXne4knA0UNcWdhpIBY2oHOX7a9s+FS65l24zhiRM3QGlHn6 Nv2g== X-Gm-Message-State: AO0yUKX433G8R98TRu5IBY7kN8wtKjqc7cpxZDjMX5UXQQmj6e9QbmJb WHMiKXueHbhCPDFY6+rnu2yiO6hAI27d/YHltJ2HPA== X-Received: by 2002:a17:906:858c:b0:88c:3502:56d4 with SMTP id v12-20020a170906858c00b0088c350256d4mr21483985ejx.46.1678366566451; Thu, 09 Mar 2023 04:56:06 -0800 (PST) Received: from alco.roam.corp.google.com ([2620:0:1059:10:1645:7aa4:a765:1966]) by smtp.gmail.com with ESMTPSA id hy26-20020a1709068a7a00b008d92897cc29sm8799523ejc.37.2023.03.09.04.56.05 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 09 Mar 2023 04:56:06 -0800 (PST) From: Ricardo Ribalda Date: Thu, 09 Mar 2023 13:55:15 +0100 Subject: [PATCH v6 1/5] media: uvcvideo: Ignore empty TS packets MIME-Version: 1.0 Message-Id: <20220920-resend-hwtimestamp-v6-1-c7a99299ec35@chromium.org> References: <20220920-resend-hwtimestamp-v6-0-c7a99299ec35@chromium.org> In-Reply-To: <20220920-resend-hwtimestamp-v6-0-c7a99299ec35@chromium.org> To: Mauro Carvalho Chehab , Laurent Pinchart Cc: linux-media@vger.kernel.org, Ricardo Ribalda , Sergey Senozhatsky , linux-kernel@vger.kernel.org, "hn.chen" X-Mailer: b4 0.11.0-dev-696ae X-Developer-Signature: v=1; a=openpgp-sha256; l=3862; i=ribalda@chromium.org; h=from:subject:message-id; bh=ndFlDl0AdhbyYkioWAtq/V2eWm/wcOyReOlolbI+09Y=; b=owEBbQKS/ZANAwAKAdE30T7POsSIAcsmYgBkCddbVxx19Gt5zrBF61atbNz73Y5XtkY79IimR1tc 5tbWG8+JAjMEAAEKAB0WIQREDzjr+/4oCDLSsx7RN9E+zzrEiAUCZAnXWwAKCRDRN9E+zzrEiEL4D/ 9U9Nci+Hzs2osRy76LEiE/MqEDFa1evlsASjlukfYUkm421vFEMgo71FnBNoVwK157NJGUQuNiu8cp dR4SLDhLkpM8iQ/FMlYwZ2WNwLtCERbKgA3TSvCMubk/5bHH0vYmXJdIAprjNFqtcP7itZwUNvhDl6 Xdy6RUNyY35b5FyaEfJoA8aJiI7fG0yPpqrdGxCE1WKT1z9MwfHxI9oqPOxyM+iqRKwIBSydERqPDM s7cRpBvwro3HWp3GoGsB016fegsNOsbQf0gx2EHq3uBrrB92VmNGpDOWgrG0SWVIteuLB3uVmhuVmn N8aeiHct4iaj5Rpo3bkkdI/OAKe4H4xbeIpWZjjZwwhVGKuPe3LzkKAaNOHvLtYB0aEmTLDgIzWn+7 MOMIi6pCXYR2zGv+3OaL6phtmFQcJb+hmtg0sm8w6fdwnWF3rB7nzd/AJm6fw0sclcebsDWw3Q5sz7 VfBnVPL7/d+PL70i3/ZterJ5HPt/uVoc0H70uTx92WfCbnFQq5+fCqzIPELJMdPqpkbbEY2lXcRT51 kc1dgNu3k762fZyk39EIPac9glluIEBVkVHiSzKbSJs4axnsbB1ttfY2bnR4zQ+YZi+1v5Qo+Pi0bo 5GuPOa6k52sIET4KjtfcRp8pJFQc/NohVmjGoqosA4mz1yLwrVpO/slrBa/g== 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?1759895637735224638?= X-GMAIL-MSGID: =?utf-8?q?1759895637735224638?= 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 Reviewed-by: Sergey Senozhatsky --- 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 d4b023d4de7c..fc7f8b6fda85 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 Thu Mar 9 12:55: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: 66842 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:5915:0:0:0:0:0 with SMTP id v21csp279043wrd; Thu, 9 Mar 2023 05:08:51 -0800 (PST) X-Google-Smtp-Source: AK7set8NViTDv6aaUYccGXQkWBrReQOQy/L0IbsimX8wN8dYehgNVm8ELXHU2SeWJ1u/9SqDlKk9 X-Received: by 2002:a17:902:e88e:b0:19c:df17:7c8e with SMTP id w14-20020a170902e88e00b0019cdf177c8emr26988509plg.68.1678367331471; Thu, 09 Mar 2023 05:08:51 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1678367331; cv=none; d=google.com; s=arc-20160816; b=KirmygASP41Ke3Pp22+jQYKEuMCvBSPcXNR0abrz8mLgOMtwe1Bizi5A8gDGL9PPo+ ufxpfqtwcu+AelZZynu/0svpM2Kl4w0rppc9iEeazvHSaBkL7Yf6hkLzCI7F5h9ydE+w gxN79JadEOYWwKjFY7RCjAhwbYArDzC+6xwhD5bF4xsljZwPvzBOszMvUH9LDsS/0D+D ROJJAstSg6T+EjzkvjdzX4HANBF1BBrgvnHxLvmocxTlCe8H96TRRlqZQYLof8V8upG2 3UsOjxv+uLnsD38f0o5LR/0RXUnFxcphId4IC0vL4bSIt7bjPv97i+rXzFkOWdPxP8Tr R9cA== 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=7BPyhUJo2IJ440ycAVWGg+bKDSXaxq1Jdyu36gyWJDY=; b=qgzGG0D8rTsrbry97EejU1nAGlgXVg4jxig+9wKN+AGQLGOQ9TogMkPVew0k0goHXI Cq1M/YG/5+NHTyOe5VnKe+Sf2EAaKqGpCGluAa5yB92LKFg3RaWkK2Fj0KKZtn6QHF/G NLEuvRN7d7pgtr7RuCCDyl0JmOKL6v9UEr6YPP25IBGDzBWRijKAepC/31hiOhkgJiWb OrCasa15H7uZooAVng4ET+B8hs2EPfaDpM3EUnnQMXPPP+zL73M7JwBC0HptxUBmexIZ LCiqHsk2N8pbE4D7NZJnwpLC1+9qFz13ugPMpFNJl/j/uV4gH/CgRABoMktE4MFww8jU MUag== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=YZLhQdWq; 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 b2-20020a170902ed0200b0019a9c0e9adasi17034997pld.278.2023.03.09.05.08.36; Thu, 09 Mar 2023 05:08:51 -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=YZLhQdWq; 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 S231319AbjCIM5v (ORCPT + 99 others); Thu, 9 Mar 2023 07:57:51 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59556 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230104AbjCIM5X (ORCPT ); Thu, 9 Mar 2023 07:57:23 -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 AA167EE75E for ; Thu, 9 Mar 2023 04:56:44 -0800 (PST) Received: by mail-ed1-x531.google.com with SMTP id da10so6625230edb.3 for ; Thu, 09 Mar 2023 04:56:44 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1678366567; 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=7BPyhUJo2IJ440ycAVWGg+bKDSXaxq1Jdyu36gyWJDY=; b=YZLhQdWqvwOgjs2qhMAhjeIEdYmdTlly6E2NEFhNCqQGfVxSZJb6jyvQ9yHWVcz7RK u/HbhcBpT2tu2AReoXK/zQ3s10jZUTTHqbwkZIOSdLJzd15cI2YGYBTOz1hKPSNH/cKD Jrjp3qErHgGDC2UUB5T+F7QORkstD1JRuzk0Q= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1678366567; 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=7BPyhUJo2IJ440ycAVWGg+bKDSXaxq1Jdyu36gyWJDY=; b=Lh8U7RzZ9CVnYSe0AbY1s9Nz6WE75B4tRjACzLXgbMmkoeqv6T3O/3tmjWGFwYCMRv LdQM7coXxa/tLEhZFmNQs+p5J4Q7mzqMOzscU9I09b7LpbE1BuVZhwCva0E3a5JyFk7h 6dBF5Gv7HzVwggJZoZYw2xCGm/Va3ut8aKOw87S2F0MZhSAI8dG+osC0AnkkIs3VR4mj YRQQUUWfXDXmoh4G7T5gKQBz27coTy4MZs35pB4pw75ra9QHQoRi9cz9dMr3sieqxsEO 2nCTwlcjCiDc0B2lXnEuvGMtuGFAPfDaLHnm8mVqc/gf9kM0jttzivUSwDZKdNdUiLFw CExQ== X-Gm-Message-State: AO0yUKWyRk1MUnzjFtKuiKKbYwuBk48mvhrdq5O+LnpdOGBAheNBRtt9 7kO4qEYvhKv8ELgsrHn47ahT49bMHYdJ2mc1dwtiZg== X-Received: by 2002:a17:906:4953:b0:877:a2d1:7560 with SMTP id f19-20020a170906495300b00877a2d17560mr21050758ejt.27.1678366567425; Thu, 09 Mar 2023 04:56:07 -0800 (PST) Received: from alco.roam.corp.google.com ([2620:0:1059:10:1645:7aa4:a765:1966]) by smtp.gmail.com with ESMTPSA id hy26-20020a1709068a7a00b008d92897cc29sm8799523ejc.37.2023.03.09.04.56.06 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 09 Mar 2023 04:56:07 -0800 (PST) From: Ricardo Ribalda Date: Thu, 09 Mar 2023 13:55:16 +0100 Subject: [PATCH v6 2/5] media: uvcvideo: Quirk for invalid dev_sof in Logitech C922 MIME-Version: 1.0 Message-Id: <20220920-resend-hwtimestamp-v6-2-c7a99299ec35@chromium.org> References: <20220920-resend-hwtimestamp-v6-0-c7a99299ec35@chromium.org> In-Reply-To: <20220920-resend-hwtimestamp-v6-0-c7a99299ec35@chromium.org> To: Mauro Carvalho Chehab , Laurent Pinchart Cc: linux-media@vger.kernel.org, Ricardo Ribalda , Sergey Senozhatsky , linux-kernel@vger.kernel.org, "hn.chen" X-Mailer: b4 0.11.0-dev-696ae X-Developer-Signature: v=1; a=openpgp-sha256; l=5174; i=ribalda@chromium.org; h=from:subject:message-id; bh=fGx/5pJOYEPLRnhXYtWOAWi9O15PTHo6OOgt4cGK7yM=; b=owEBbQKS/ZANAwAKAdE30T7POsSIAcsmYgBkCddddQCIr5C7jKmLOkFpRpHdWv8bbDzhCJSv2M/g PKKCSlaJAjMEAAEKAB0WIQREDzjr+/4oCDLSsx7RN9E+zzrEiAUCZAnXXQAKCRDRN9E+zzrEiP9mD/ 9zlYoO8h4dC+jcmYihHBzBS9RdW7yKbgTaKf9nrlZS1DSO0u4J1vZYdtVH/UOWdyE/q2HK5+FDLvKQ xUx/4ovpYJrrISliy9NjXK/Wb5jlJbMPmFiYscjtgO52I1MxdhZL4iD5QRiakdLMd2ydbHirsbdpdc urJA0+Y/h7yBiBOEyjQQZzEWVnFC+NYbd810uVGOWFpLvH2mLxdYDDEtnEOKZzVgIFQ12G819H2QYH tTC9cwoUNj86Z7fK+bvTtLu9rVj+xlOCd62/vxjCM5Qf9ziksIFGYakwE1tMWmxoWESWhZFUdYkMos 3dlEh4sT1MGu8rVuOu8mycMhZidlukrf4crG4KUtF5AZkTCGvRerO5bMdJpaLT6U8K5q+AXggIWNlL sS5k5ZOO150sLuJUqAlbHB1vGFUc+AxruemjodnS22jYzG8uBFMTnxR2zMRSfCrGuRMRy85V+iyJuI Uc+NbdYQ2nAmVEvBOwxHxKYMDyoyIsf7oZEOVO5spjffSMdTTCarMx4S1dl1tOaQ+UonOdcsXjB05Q 0uH6oU2LZv11I+GshDtvXzWix1nKc4ovd02QxqxQSfNu6y/4LDC0d/WaL/nfJwBx9NI0975CgpEz6+ C2NwpayhDMnMLIe2cjluso2zFt2tIzwSX4U5xLyRYIQCc7I0zmlkVN9RJUXQ== 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?1759895703125753130?= X-GMAIL-MSGID: =?utf-8?q?1759895703125753130?= 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 Reviewed-by: Sergey Senozhatsky --- 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[] = { .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 fc7f8b6fda85..e363a71dd190 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, 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 = host_sof; + time = uvc_video_get_time(); /* diff --git a/drivers/media/usb/uvc/uvcvideo.h b/drivers/media/usb/uvc/uvcvideo.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 /* Format flags */ #define UVC_FMT_FLAG_COMPRESSED 0x00000001 From patchwork Thu Mar 9 12:55: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: 66846 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:5915:0:0:0:0:0 with SMTP id v21csp285358wrd; Thu, 9 Mar 2023 05:21:29 -0800 (PST) X-Google-Smtp-Source: AK7set/iEI1CpvgigtFilU8ACJcjGYdygQUijrCfcAudI+XeinnKIPqpDL5sCoAJ0Wpy1yogv5Po X-Received: by 2002:a17:902:720b:b0:19a:7758:e5e6 with SMTP id ba11-20020a170902720b00b0019a7758e5e6mr18320238plb.48.1678368088890; Thu, 09 Mar 2023 05:21:28 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1678368088; cv=none; d=google.com; s=arc-20160816; b=Sv+1CthQCr6mXwhv26ygN344tBM2BgWUAVxb5URKoTCLq3mgE06JHF+FCYCpXBVCSh zrOpvdSwwdzs4XrGNR0fQ1FgH96vd7b4xO4RwIX0kdM/7LUaS0gO/X4LzXFmILOiAu5f tjJFzj8e73VFYiBJr8fuzyDeaWobKeCtVDhuKDd+J3onLz0RLUIiUnearQH9gvrRceDL pPdL2OiZb0LEcU8I4zNNm5udXSTS5qQW/+cjpdk7JkSZVScI4U/tdrQGQ/CyIRMkGq/K q1NwOLTnWM5Hmftx2iBDJL14baM3PqSSMQkpNMkhtaCZa352IY7rpjuyRb/iICFM8T7l JHGw== 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=oihws/oJVUk4/qcYiN1BQhjDRl1MZi40xXWGpNQ+ONY=; b=PtOEggusK3tTUlcfvjeS55es9YBZP1LlQl7Xe6J1p+Ptegfg26r/iphNjAFtZVE9ce BxAQvvn5feKKgiaZlq0od4kbfABdqDq6T2SOr+xWDhmIOFj/NfaSb+EMZ+yrEQXjubjQ HElEMKt1qJ/XSRH+jfsDyQQIml3AsN2e3sV++lFEENRZfxZ0wL8Twj8SUpiB0ZPDFHNK TYewKe1jVy48WTPnhZN6zEJ0gQ5Tz+tQvMG+sm0FEM0MzkQueUBxRRU6msdQDQRZiCny wfBWXBRfOTHGON4qxhcU6CTSQicEtkU0IABnkMbikt5alDUjPZ35UmC+MjHZSP+/qA5k jlbg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=m6K1pinQ; 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 lg14-20020a170902fb8e00b00199051ac8f7si16628630plb.188.2023.03.09.05.21.13; Thu, 09 Mar 2023 05:21:28 -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=m6K1pinQ; 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 S231341AbjCIM5y (ORCPT + 99 others); Thu, 9 Mar 2023 07:57:54 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58006 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231134AbjCIM53 (ORCPT ); Thu, 9 Mar 2023 07:57:29 -0500 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 B533CF168F for ; Thu, 9 Mar 2023 04:56:49 -0800 (PST) Received: by mail-ed1-x529.google.com with SMTP id cw28so6582728edb.5 for ; Thu, 09 Mar 2023 04:56:49 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1678366568; 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=oihws/oJVUk4/qcYiN1BQhjDRl1MZi40xXWGpNQ+ONY=; b=m6K1pinQpMXJ7zNjBGgM9palA2IOSgd1NQPJ0OqhN4sVXkjbA0bvTPp+WtAD2Fw+Ka GM6aZumoy9jelme0wMZr5QrTMQFiI4Dd68/wwGDbznCPspB25fjxZTtTot6PkHnzBrCI 760PSlrpWXDpE292ciStrqHYub1wuZZbevkDU= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1678366568; 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=oihws/oJVUk4/qcYiN1BQhjDRl1MZi40xXWGpNQ+ONY=; b=sRoRxNK2wx5Z/NUwsag0lJ7DlAqD33KnDnHHOKK/Ew6pdS8h2AS2KWw6iCi0mbuZy+ 13FeTVeX8sGADQsiWyhwu6iaU+gvhyflGwAlb5SjwdjOwocrAJ5/M8gx+LQwSiZuo5MB OB1JImhWS6mIP729KQ/f5VrEo0p96MHCsjk/z9R6MazzoQmzCxPGZ7ro2Gb3ePrM6C/q ykC6/+vsGBEyFrHZmuni3m1BOX4iFM/NEpuicY47J6NEY4gdtD7rNJlodMww1goZbnyd A1A18trk6FWO34+DiKH3twrJ/uaZphwp+C6VFcWDIfvY6GWUviGqa+UEo9sSZL2uCSFu RjIQ== X-Gm-Message-State: AO0yUKVQ11b1yad+EiPQR7kYrqtFEnjRzOBod/+ZhcsxrCnBogBQl0sC VNkkXiNNHAreL5VvISAavN21Proj2SOujpVAol+wVA== X-Received: by 2002:aa7:d305:0:b0:4c0:4912:2006 with SMTP id p5-20020aa7d305000000b004c049122006mr17708532edq.11.1678366568228; Thu, 09 Mar 2023 04:56:08 -0800 (PST) Received: from alco.roam.corp.google.com ([2620:0:1059:10:1645:7aa4:a765:1966]) by smtp.gmail.com with ESMTPSA id hy26-20020a1709068a7a00b008d92897cc29sm8799523ejc.37.2023.03.09.04.56.07 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 09 Mar 2023 04:56:08 -0800 (PST) From: Ricardo Ribalda Date: Thu, 09 Mar 2023 13:55:17 +0100 Subject: [PATCH v6 3/5] media: uvcvideo: Allow hw clock updates with buffers not full MIME-Version: 1.0 Message-Id: <20220920-resend-hwtimestamp-v6-3-c7a99299ec35@chromium.org> References: <20220920-resend-hwtimestamp-v6-0-c7a99299ec35@chromium.org> In-Reply-To: <20220920-resend-hwtimestamp-v6-0-c7a99299ec35@chromium.org> To: Mauro Carvalho Chehab , Laurent Pinchart Cc: linux-media@vger.kernel.org, Ricardo Ribalda , Sergey Senozhatsky , linux-kernel@vger.kernel.org, "hn.chen" X-Mailer: b4 0.11.0-dev-696ae X-Developer-Signature: v=1; a=openpgp-sha256; l=1846; i=ribalda@chromium.org; h=from:subject:message-id; bh=GlVqUU92g3WKEyOK44VrS63EV0hOXZ0v4iXYGJQ5aOU=; b=owEBbQKS/ZANAwAKAdE30T7POsSIAcsmYgBkCddfTZKqE6aW7SvZIz5O9S+ai73EnKknqwCmNy6A J+lGGCiJAjMEAAEKAB0WIQREDzjr+/4oCDLSsx7RN9E+zzrEiAUCZAnXXwAKCRDRN9E+zzrEiP1fD/ 0XJLBl4y7+8jrIpqo6Dv+jQ23AUpjjv+62ILj8Ow8eNOCgvNd912V2bfQQpxDHIleMKurAA5n5Rio1 //G2Wc2KLghDUNpMaiQ/zFKH7fktJYVS/3NSlPApRU40rzyQNz04efItH6EZTc7l1/jS/MAODRMFb0 7cUR5EBvFFGTeeyHCNojHTff/xf15WJcX/T23z2YFmsJ/z/mhVSKw2nmwHWN0mR5W3e3WbGTyJ40uP pOX91r5lWUJ0G4VffrgnCBeED09KQmceScN6v7f8bo7lAtqc7FBdcYw0nvkQfzZGIB4xmYava0qaa0 O5y/Q/amNkwtbZQtQrC8iECRE/pJEnW0+nmdIprWApia6kM4D7fHrGg2Jllt5wXpGtTojwpVr2ddC5 nhpmVUBiMY6pMR8G6P3QlDSDhMBHHwPKI1xbGxF/TlEmo6cZHoPJ3WdQsnoUVEq0+ZBmgSGta+lV7G 75g+/45/lwhaPEBETfMDg4q2dFbQLLZstxQaX9fEfjun1N6QUdrMyv8qVnm+2S5GTqSJCuaUUH7Edu kE3nNQTPpSrL2BWoYeDcgvrOifQPlNtvFkVOKU1NGVBdc6FdV+i/1p9p6SJZzTWxzoII45wHn1qWhq kqEBNN5zFIjs4jlwflslNg5R5tcQDh5hJyfHSUPb4GX0hoxTuvzxYWk+yaJg== 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?1759896497064531643?= X-GMAIL-MSGID: =?utf-8?q?1759896497064531643?= 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 e363a71dd190..d5fb0c1339a3 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 *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. */ @@ -772,6 +772,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 Thu Mar 9 12:55:18 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ricardo Ribalda X-Patchwork-Id: 66845 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:5915:0:0:0:0:0 with SMTP id v21csp285356wrd; Thu, 9 Mar 2023 05:21:28 -0800 (PST) X-Google-Smtp-Source: AK7set/4zGi4XqIONP/4EWhjReOlJNYHPvAuv0UPsRgM+cozclHFq9K1s5113PmRIM6Ni8CRkXfZ X-Received: by 2002:a17:902:6941:b0:19e:664d:8a28 with SMTP id k1-20020a170902694100b0019e664d8a28mr18169177plt.15.1678368088581; Thu, 09 Mar 2023 05:21:28 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1678368088; cv=none; d=google.com; s=arc-20160816; b=FyL2pHVpQruxL1pKNNeKs6VVklkYEDU76ny77ex5y+rWHKCD3Xacp58pLs1F+PWVtA JliphIrJLxuNrJ5CeWdWMS2zfguuPjFY25QB60alGFUDAYMjOqb5+sCoiOw0qGemykpT h+u8X5gT48gKPRgdlAkUEj26lHIzF/SARMsTRc0wabgqkkgTN8bTtSzY7F5HieM40jp2 WnUvb5qbWocIY+O0T+wTkEyE1JsGgwms5YZW8tGI+HE+hSjDQVDrvu/0eJEPMRxiaXqs Wn6a4bstBIo8aFsiZnwgYl8sVS+32abfN18lAvRaxZ9L10/2BMsmW4oncusKx/cnIIZE Y9pA== 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=Ru/q2AVdUwJAWeSXRp9GMn60NOVlvm+wcgoK1aArSdM=; b=dpMt1TVQIFWp529pK2a0uTjjuqTMYARnaVYH2Wlt9kPOK+P6rkUumPB2GCQAdeFc1V L2i8BwYwR8sshQlw9Am697ndAXbVGaxerAfs0D0trj0L3xECLd8oDZJuQikA5CgdyUmC endEnT2G7wn98XzXGkDjz+6P8Bcl8Yv58mZVQ3O3QIFanKUCMDBFUGxkIImauri84408 hhih1uq2ceocpU67xgJXd0yiogP0XTvl46k8WEY2i/iVBpJVkDleKMMAc+PgTxnI3/8W tRW5i/OhB2ahWegGobWWlod8cLTbmW/2IQrX9LawCl0ukx5bYFfU3XId9hptPTF6x5bs pkiw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=dn4K4WvW; 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 lm5-20020a170903298500b0019c9b10377esi17387899plb.40.2023.03.09.05.21.13; Thu, 09 Mar 2023 05:21:28 -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=dn4K4WvW; 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 S231261AbjCIM6K (ORCPT + 99 others); Thu, 9 Mar 2023 07:58:10 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58014 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231166AbjCIM5c (ORCPT ); Thu, 9 Mar 2023 07:57:32 -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 48BE6F2084 for ; Thu, 9 Mar 2023 04:56:52 -0800 (PST) Received: by mail-ed1-x530.google.com with SMTP id da10so6625470edb.3 for ; Thu, 09 Mar 2023 04:56:52 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1678366569; 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=Ru/q2AVdUwJAWeSXRp9GMn60NOVlvm+wcgoK1aArSdM=; b=dn4K4WvWlQpEGoFB4tuo7UDZZextIzf4f1tM0Qj7lJfEln/yKEcJJAAOAIsFGYtoE5 9fgQQ3/ZzmDrFop7nXEDpEZDNPvHtIodBr9JYfWt202FmW3o8SzYF4iwqXqDpEsU6omJ ePiX1kJ2jM4+0uqAALF4bUVzGC6Be1J6isxpw= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1678366569; 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=Ru/q2AVdUwJAWeSXRp9GMn60NOVlvm+wcgoK1aArSdM=; b=Uq5yNUG4fbkEGZebI4e4GdgEQaTky2y5H/ChxAqJ2wHBHEL0BOIvtNCtQ1yWgR02ve gHpeQXpPIajle3PxhjzoAtodkkYhsw2wNtlx4bbFRh6fO8cb/JFn8QW0rK6LPdcj8Xnt D7VaIwnaF6IZWyC4Rhdy2fSAvbeJM62lNZZ2FQ0+SLUrftNLYN/kysQN9tbUiK1lSyCh v52Em62Kc+C0mvolYZx5DOqszwZzl3WmNpm5lWmBoiJWApD61fwNmaGZ/pcEAo1mfwtL 1tAidZPjunPlu4R3RB2wvPXwA135zHWxVzMY3cDUGhUJUmPpB3hWggoitvLJZpDSv1xz tHPA== X-Gm-Message-State: AO0yUKWI7KKJ9g/QXITrKNi3GHnSvj3J1QMIF/Wz/jd7fNcMa6x2GdUK aJzET+9KLi1qnkqtBbvI7rYcDpav0uSfZmsshW2llg== X-Received: by 2002:a17:906:db05:b0:882:cdd4:14d9 with SMTP id xj5-20020a170906db0500b00882cdd414d9mr26386036ejb.46.1678366568858; Thu, 09 Mar 2023 04:56:08 -0800 (PST) Received: from alco.roam.corp.google.com ([2620:0:1059:10:1645:7aa4:a765:1966]) by smtp.gmail.com with ESMTPSA id hy26-20020a1709068a7a00b008d92897cc29sm8799523ejc.37.2023.03.09.04.56.08 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 09 Mar 2023 04:56:08 -0800 (PST) From: Ricardo Ribalda Date: Thu, 09 Mar 2023 13:55:18 +0100 Subject: [PATCH v6 4/5] media: uvcvideo: Refactor clock circular buffer MIME-Version: 1.0 Message-Id: <20220920-resend-hwtimestamp-v6-4-c7a99299ec35@chromium.org> References: <20220920-resend-hwtimestamp-v6-0-c7a99299ec35@chromium.org> In-Reply-To: <20220920-resend-hwtimestamp-v6-0-c7a99299ec35@chromium.org> To: Mauro Carvalho Chehab , Laurent Pinchart Cc: linux-media@vger.kernel.org, Ricardo Ribalda , Sergey Senozhatsky , linux-kernel@vger.kernel.org, "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=CNOqiM4ezF7ILeG8xiZdi/AwLna/49D4dSt5xbUhfs8=; b=owEBbQKS/ZANAwAKAdE30T7POsSIAcsmYgBkCddgr2N61y/Br9Tw6mHj8xKEfrK+/7urrcvSZZSH 4ojtVwKJAjMEAAEKAB0WIQREDzjr+/4oCDLSsx7RN9E+zzrEiAUCZAnXYAAKCRDRN9E+zzrEiOyGD/ 9oGFQhpCsXjASjZ8Zx2yor8eO0UpMNgb3Tw4OeuOFAWQC9LGr8G/e+LHLjdo+mgwoWx8R+dE/TgUGG nvkZFvJzLkwWO0qUf+G11IA38sE1lAlqw+QhcCCKl9qHz0ihyYGe8vV323NESoSkZzHjIioN+hLmcM xcoe9+NH5PdT9iO5vAGOHBfm1HadqMNu+9hL5zWIF/mHM34iWXtF1TVGKWVgXbj3HMNv0nYWd0sP+j S/3HznFWhEf9QbVH3QPZblck3ipC7AtXhPnPJFgEgaJoJGQsZLu2IfYHoSoU7xJPCrnyIPQyMAN4Nu KSgk+LpKNBpFLOvFZKPk/6uLq8v4HTaLNVK5iijEjVtadh7AZby/9T70dDRcIRnOEYwcVlS+fDyMiS CWM3FK8ut2dwOorfNvy3mvRXKQhHFKi294gU1LBREPrqsK8rONzwuz4QzxB0uSTRAbqmJuVG3lrJKe 4esfAhxuu2+Ej7qyXlFAMkrawBNW+MsFy0jFHE0cSM0sIbk4CI9huLCv2cRdcXyStuakAY9CdElVgj 9ozD8Cdchv4yc3qO314Jto2PAgK+SrXjpQjQg/uGFK+PsJ5ljPVaRYp3XrerKujDQM1vQt/xctP5nz R1u1HLyrTh+U4USfM3zcfjVUK80owt6ze52QgEcQky8vD1OOWvkZvA1Jk2VA== 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?1759896496486065713?= X-GMAIL-MSGID: =?utf-8?q?1759896496486065713?= 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 --- 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 d5fb0c1339a3..0bb862e8c803 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]); /* * 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 == 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; - 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; } /* @@ -2106,7 +2098,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; @@ -2255,7 +2247,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; @@ -2273,7 +2265,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; } @@ -2301,5 +2293,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 Thu Mar 9 12:55:19 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ricardo Ribalda X-Patchwork-Id: 66847 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:5915:0:0:0:0:0 with SMTP id v21csp285485wrd; Thu, 9 Mar 2023 05:21:47 -0800 (PST) X-Google-Smtp-Source: AK7set/8iICz2H10tsMuKGLYYOobAcunGyUcH0ROW+8DamWNRa4E9PuKJbV2d1JvmKW1fH+zTn2+ X-Received: by 2002:a17:90b:3e81:b0:233:f354:e7f6 with SMTP id rj1-20020a17090b3e8100b00233f354e7f6mr21302468pjb.46.1678368106879; Thu, 09 Mar 2023 05:21:46 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1678368106; cv=none; d=google.com; s=arc-20160816; b=ULAEvFJ7cAChz9Q0cnQAecxLqr+mFzUYiTVB5dpun0y3Ghwj2XO8tR6FjnSL3mZAzF 2l+/SMDvMtuiWCLt9pI1i+SRLaHE2eC8ZFwmtBaJnUqvd/MEXtS9bp+v1utPbRL1ZO2a l2VuxcmWief/08EiWv+xd16AyCuX3IEOmN2+VNNTJzZOa29DaZ9R+beBIABQWGN1Fohn Pph4A/GhrW76FOdawiHiUdhnHpCi2jzgEQcCGRoZ2s8Rspxe3ReT7uH9ARhRUNa2540z +z2gcdt3c+tgt0EeD2j2LtC5kQPAlpptf+jnwITS2sGZrjJriJQHQsed/GGJlYaka7IM tvdQ== 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=iMAXkHtzZVXUIYSgEhnqVkH5iil6F/pIxroVXvOBotI=; b=IDqzVccmSV+wgKWa61qForz78Y0lMP3FeAvERkeY1WeldaMYR0skPcTaFva9NrlV5j Kh8muqaBmTi0OZVxqO9rONZCBvs6/jLF61rjz+2ALFulCXVBocGUJsDy0aXmyxlxxMk2 IKmMnvm8JvIw7gEeuR089iQZEzowigyKz/vItORrW4HgLkr7LHrIEVcUNmHQvztl28dG jsHxGGPYNiKLqfALrnrZjToixh78lfLXbShm/7DtxerqH/WKZ07xOdijkwUOWB9TGoqB ouodAjR9Fw8CMpttCJy30dmxDMcHAThrcNBbLZS48CDTr15nz1dV7b3Cd0a7mYJVCC6V SKRg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=NsOgtDKW; 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 u191-20020a6385c8000000b00502d75eb815si16771630pgd.757.2023.03.09.05.21.31; Thu, 09 Mar 2023 05:21:46 -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=NsOgtDKW; 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 S231249AbjCIM6N (ORCPT + 99 others); Thu, 9 Mar 2023 07:58:13 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58964 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231229AbjCIM5e (ORCPT ); Thu, 9 Mar 2023 07:57:34 -0500 Received: from mail-ed1-x535.google.com (mail-ed1-x535.google.com [IPv6:2a00:1450:4864:20::535]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5DA6CF2093 for ; Thu, 9 Mar 2023 04:56:55 -0800 (PST) Received: by mail-ed1-x535.google.com with SMTP id da10so6625579edb.3 for ; Thu, 09 Mar 2023 04:56:55 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1678366569; 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=iMAXkHtzZVXUIYSgEhnqVkH5iil6F/pIxroVXvOBotI=; b=NsOgtDKW0v1J0PerqA4rmY1XfO0Y8aYIdvklcqGgVCvR83ZRI+T1ckbqPde7SpjItx gZBAl/dC9Zp70nsZFHjiQVXQV31jLl2CZih4ew9OnpLk4msTY5L08R6EbSweVigzk4/x zBv0W9g7zPyO52SzdBC3UUZIkoKolTMa/huaE= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1678366569; 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=iMAXkHtzZVXUIYSgEhnqVkH5iil6F/pIxroVXvOBotI=; b=RTZ9tnEhxQr9qR+UOF43VIzsKBOAz8X8RKATuoQJc9lj26Ew4ATvPrNmbuP4kmDwvx +UNsKLxujc5cojxqEGQtokK27mQA6CU19b3I3eYA2lpQtd7hppdZ46ePPXRGNfwaFsCu fx2nuPgr5QEqODljI2X+hGHxp7893GcN4i95L4A/TBvrkw+gHomMXSY5rMtgGPVoVfbJ FaFRvrm+7onHVv9Gcu/UDzK022j29Khx2IoxE3zvg/hUHxFU8vaj16SMzDQTz8OB5T6i ZTykhC1w3d3S9VP6zvISamQ7JKNClfiKfIFu6mG2w8AC//iLdHmoW2SbroIVQA8ZCfeX RxSA== X-Gm-Message-State: AO0yUKWTRKUFxQXdwJpy4oiGNAC1Lu/nr9QcHHWwQV3/uhoieH9eCYXO cPciESmVTf4jixfvsQQWw0YdfarsNREwdy2txHygPQ== X-Received: by 2002:aa7:d357:0:b0:4af:6a7e:9218 with SMTP id m23-20020aa7d357000000b004af6a7e9218mr21053463edr.42.1678366569494; Thu, 09 Mar 2023 04:56:09 -0800 (PST) Received: from alco.roam.corp.google.com ([2620:0:1059:10:1645:7aa4:a765:1966]) by smtp.gmail.com with ESMTPSA id hy26-20020a1709068a7a00b008d92897cc29sm8799523ejc.37.2023.03.09.04.56.08 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 09 Mar 2023 04:56:09 -0800 (PST) From: Ricardo Ribalda Date: Thu, 09 Mar 2023 13:55:19 +0100 Subject: [PATCH v6 5/5] media: uvcvideo: Fix hw timestamp handling for slow FPS MIME-Version: 1.0 Message-Id: <20220920-resend-hwtimestamp-v6-5-c7a99299ec35@chromium.org> References: <20220920-resend-hwtimestamp-v6-0-c7a99299ec35@chromium.org> In-Reply-To: <20220920-resend-hwtimestamp-v6-0-c7a99299ec35@chromium.org> To: Mauro Carvalho Chehab , Laurent Pinchart Cc: linux-media@vger.kernel.org, Ricardo Ribalda , Sergey Senozhatsky , linux-kernel@vger.kernel.org, "hn.chen" X-Mailer: b4 0.11.0-dev-696ae X-Developer-Signature: v=1; a=openpgp-sha256; l=2432; i=ribalda@chromium.org; h=from:subject:message-id; bh=BkEYNPgQFikUx3ehYbVZ7ucDEzERBb5OFmbVLs51fjc=; b=owEBbQKS/ZANAwAKAdE30T7POsSIAcsmYgBkCddiI0AjI40kE9YpN74xp7ZzsdAfpfwQMB18nkKv Rnn7RxOJAjMEAAEKAB0WIQREDzjr+/4oCDLSsx7RN9E+zzrEiAUCZAnXYgAKCRDRN9E+zzrEiJLqD/ 9Sln6eswFHpBkcgQ7zVvZmsAAMSt6u2TvjDhNj+uKGB3T7NgPECGoScjtN6mQ/ALrk9TjEQ5cA/zpy syX5uFtWEb00ZX0Jz1E4F5Vg/tUXDRrhqxiyf+p50JolqVhmronpTAnxq7loh8HDgEVGaMyHSSXx2S urhUhVwW8ztFtlSvK1Nfn3FXjg/tunhga7KHWkvPBLQdIPBtgnx3fnO8TnYGx/WDn9C2ra/kbJ9sU2 8ByWavtyo3znu8CE22D6RQ+NHo120lJO3A4ctKUFfqUb5hgWpPbOzRzci76B5FExqpe9RJsEzQLD04 /X+6M0be/U0DS+zfTFFcEraXCaclrhPakiNTEcwQOat/Z5APbkXtlnnRtLjulysbS6fQdoDiIdMk8Y jNpi1djE7ExfSaZsHCKkI1fO29wWF2dkvqk43GFLyaxuTGdShzgaorgDjK/pawjFbNrqDuXG56eOYl RK8vNpdGgP+TOk1uIOKGCNvFuxUqy7zAXWuZr07tIGdncn49OAdtqPRKitBTJsGKOJBhaqojBYRK5Y tGLnz05oQplnH+gVAL/hCqxWUMrjxqpcM8+3ng/LqZUqs7M8noVEVMQBBjooqIwyMNN2lZJmawR5wZ ewqPjGqziCT25lsatxFJRGbL2ABYf/x/jwQ7mw8VTHGAI0KjjtCcx3zq2KzQ== 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?1759896515893038377?= X-GMAIL-MSGID: =?utf-8?q?1759896515893038377?= 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 0bb862e8c803..6f2bb0dc991c 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); @@ -605,6 +619,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;