From patchwork Fri Jan 13 13:48:47 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ricardo Ribalda X-Patchwork-Id: 43315 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:4e01:0:0:0:0:0 with SMTP id p1csp281976wrt; Fri, 13 Jan 2023 05:58:37 -0800 (PST) X-Google-Smtp-Source: AMrXdXujkn7QQaWGWiYfs7P8lCPPpnIiU9Frzbti3xXlAZqg7wOv50gKqo5PTeWMgMkL4fTNoynP X-Received: by 2002:a17:907:c242:b0:7c1:453f:1aae with SMTP id tj2-20020a170907c24200b007c1453f1aaemr86876819ejc.37.1673618317477; Fri, 13 Jan 2023 05:58:37 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1673618317; cv=none; d=google.com; s=arc-20160816; b=yUvH3wswbyJboVZMNegLBwdIPKzxjSZzxFenXG3gN+MwNdMCMkAc3QI952gehuiPYl cjiQU939oJGa8HuGPnKwIPBSUzptCas7pr17MFcqwDYX+vrIkm0bfgXn8YD498fInGTV hgLVcgAsiRdBVoVvsesW4fSY4YK5AyLD+ZhTrHP2oUdouyzmBtDX2IPdqR/ZVByh1lvj ew0SHu/gMnIkauH8F3OLGhmCAdaGZzAqNoLmT5ssBKtyIo3RnAFud8nM/L3yxv5Z8rk6 MYodPK6Oxsyd4bHctJTolGiDjs5chPu6/1uOwjDYyfUVqDDq3ukiMxOhy0Ri/WpIcsTL h6Kg== 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=i7Gw/Wt0kH+cB2xtvU6KPFDByh+yOvFcci+3eI5EXXQ=; b=xy6P+zrpVce95M/ffLBGKU3/ZYT3ugQ4ogu4aT28x9oiescOcadTO6wZ/1MHfrnJ6X 920+4h9jC1XPL+OXVDJF5GlMzJ2FQSk84V8fb1gAsl2d3RviX+I/CxGYvrW/CZyMXjtM Ofx4/7FKFUT/LbdiR9f8lmnEvEiTAoGnC1HTbkGD+HKvlEnVdkSMJl4JqB2BN0QrrKlC Ar/1xMcUYAi4TBZZ4Wwwe9yajMHpir2g5Tds0rggZdbXEySfXcyjH8LO0RNBRUf0F0Qb SAH/KaH0d9LUVbaPHpmA1V6lVTqIxTUIPDJ690uh1rdzJn+hKnr5W6Ubdc7w9QCoSeBj KjOg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=mhHk1EqH; 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 gb23-20020a170907961700b007bbf9652314si280354ejc.435.2023.01.13.05.58.13; Fri, 13 Jan 2023 05:58:37 -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=mhHk1EqH; 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 S241877AbjAMNwu (ORCPT + 99 others); Fri, 13 Jan 2023 08:52:50 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50282 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233019AbjAMNwT (ORCPT ); Fri, 13 Jan 2023 08:52:19 -0500 Received: from mail-ej1-x62b.google.com (mail-ej1-x62b.google.com [IPv6:2a00:1450:4864:20::62b]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 2AA9565AF7 for ; Fri, 13 Jan 2023 05:49:05 -0800 (PST) Received: by mail-ej1-x62b.google.com with SMTP id ss4so45194816ejb.11 for ; Fri, 13 Jan 2023 05:49:05 -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=i7Gw/Wt0kH+cB2xtvU6KPFDByh+yOvFcci+3eI5EXXQ=; b=mhHk1EqH9xVIqkhKsNdXdmh+OuuxL+KEI0D3zaPzlYzcIRSGiOdglM9YSA1sntKCDV TdcndgS11/KSuipZ9sM0JS35hiZwO3gTXYpyNET0v1E/paQcf6dUjJV93fnsiqPHCtgb AXIzQQKVb3P5RjADuj0UsumKUrSQeC3MXftN8= 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=i7Gw/Wt0kH+cB2xtvU6KPFDByh+yOvFcci+3eI5EXXQ=; b=5VUp+ZB70uVoHP1bJl1yK+dvIb3Ya/GKfVGk03kFjaxLIKZm805xG129Hkxv0+2a8z jMD9djw7HPLNTRIyycNwFbcbTh+GYsB9AlRQ34uOejWMqr+rDeYIZCOZTFlFThfH7kJT 4Q82OEZcm8rGrdmTTRvsz9aJ5HyDIablSQ2Yo6npmnOBJSPRdyrseRBHhq6kEv51YqIe yrCSAS5THgxabWvDgMltn54v+Zs/71ratb5RmTYrG3mtbHpriAQ+SwoipQBEBsIS3Fg3 CGs9M5F2aLwvs79qbftUYu4iFosuCp8LG0fFYOaEFMYl4H565Iz8DV/6uoAV79zjtTeN p5dg== X-Gm-Message-State: AFqh2kpNKNbgwpSzZ/7gcSSXjXMtVq9d4o+m9ZskiH2w3qYGWD1XP4JK kjRyuuVuebCeferfXgAx9EI2VdW0iisbiDUPItw= X-Received: by 2002:a17:907:c208:b0:84c:e73c:d224 with SMTP id ti8-20020a170907c20800b0084ce73cd224mr30979496ejc.10.1673617743591; Fri, 13 Jan 2023 05:49:03 -0800 (PST) Received: from alco.roam.corp.google.com ([2620:0:1059:10:b023:4272:bddc:acf2]) by smtp.gmail.com with ESMTPSA id w23-20020a1709062f9700b0084d3bf44996sm7072888eji.142.2023.01.13.05.49.02 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 13 Jan 2023 05:49:03 -0800 (PST) From: Ricardo Ribalda Date: Fri, 13 Jan 2023 14:48:47 +0100 Subject: [PATCH v5 1/5] media: uvc: Ignore empty TS packets MIME-Version: 1.0 Message-Id: <20220920-resend-hwtimestamp-v5-1-660679c6e148@chromium.org> References: <20220920-resend-hwtimestamp-v5-0-660679c6e148@chromium.org> In-Reply-To: <20220920-resend-hwtimestamp-v5-0-660679c6e148@chromium.org> To: Mauro Carvalho Chehab , Laurent Pinchart Cc: "hn.chen" , linux-kernel@vger.kernel.org, linux-media@vger.kernel.org, Ricardo Ribalda X-Mailer: b4 0.11.0-dev-696ae X-Developer-Signature: v=1; a=openpgp-sha256; l=3557; i=ribalda@chromium.org; h=from:subject:message-id; bh=2f5YmRNpaCruZQpy69ufXYbW1rPh5r91J7dE4mEBR5A=; b=owEBbQKS/ZANAwAKAdE30T7POsSIAcsmYgBjwWFFfFL5GkVzMO4ITDR9HY0Ml5keCj9kLRkwObQR aIM9lUuJAjMEAAEKAB0WIQREDzjr+/4oCDLSsx7RN9E+zzrEiAUCY8FhRQAKCRDRN9E+zzrEiIrQD/ 9rs93+/3QWU+vZSQemr3EkZfXx0Gqyr0rMzn/AVumh4gCMbq9wILo/9I9oJ60p/uE8cst5TNrmkeB1 HF+8Sx3XzFRryL0enPuJprCuliI6UkW4gSI1KYWI36yUpQx9OgY1oo8915eEpukvsWGahHG+JUiWzT dKoMu4PsUpusBKTAMOOvszl+R7SmCe00+CSjoO75R2RVtFT8I3UgEjj0uXOvq4MHbLKmRdGlqXZkvG KCmgGRqJc8+5/oIbwKdbmIAtpsNZF/YDXkoZxVCUAj9w44cyESeyRMZ2KX1cxRsVOqZWV1Bd+VeUGy /tsocLM3YVH7sKfgGWLvuKe9y8LA5gSp5y74thXPl47sfhQtdr2Psb4JPEaej0Sa0HzxZm+jmu9D4d BzQYtYuM0LIDKV/1Idj9ApRdRXkyXR/PoBWxkMXtf0eOvow4HKFO0NLhoX/3kfX1S6qxe83saKZF2k g7Xge2kfkuRByU+ERzYUJqtqI4+2Ddvo309CrfEGbyBPwRNjnjLr2LONPxKeSkiwCI+TNfWV+I1PMk GPby9Awa6R0mMNduKKjDj+dmDYyDFWvVMEVkrhJy+MOR2t4yDmevCD1Pz2MhI3a594kfCgkuCRZR2p yOFf3HwGdJvKgbGPfhgGASWuiFzINWuSXedjFfuvCgcBYiL6lRQHTHjtnX2w== 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?1754916001178362396?= X-GMAIL-MSGID: =?utf-8?q?1754916001178362396?= 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..488c58bc9919 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 && 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 Fri Jan 13 13:48: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: 43316 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:4e01:0:0:0:0:0 with SMTP id p1csp282309wrt; Fri, 13 Jan 2023 05:59:25 -0800 (PST) X-Google-Smtp-Source: AMrXdXu7HMwu5w1oWaEA3Yg8Frl2HE4NsSDpCf1jCnlLRnzX7zNLzT8OLkjVGJvCdpUWVJLbMohz X-Received: by 2002:a17:906:7e0c:b0:86c:df3:4bbc with SMTP id e12-20020a1709067e0c00b0086c0df34bbcmr181377ejr.4.1673618365428; Fri, 13 Jan 2023 05:59:25 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1673618365; cv=none; d=google.com; s=arc-20160816; b=vIuvo2NAI7KKUTKZunxHI70mYjrb6jWUL2UmVxq5YsLrD7xU3Ho9e0rEMsL5Ja2a+X LWoFpBmwP9GsTa/gMS0QccVwDrM78l8tq2O3tvEHH8XwRQaQJA47bIx5fbc0MjrSxdJY JBUQCXEuFDdrK9UogTrNqxht5tIyytMeLSMqpf73yW9Ig192aCYNkXhzlrKOrcWoeOEY O21+2ZOUt9i1nBkBmuajgmeQ78XmvivbX/9ZxoERjDa+cfEkVopFpw9eOxdMWkN45+Db P7Fok5oitkZGecM+RT8TY3zMkhpXqAvckSXw010fywnaRQaAF985rWNF7Cq2enIL+0cx esvg== 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=qJgtpHz+rrTvZ2HTdfCXZ2CyubMLbgrhsqS57oo+fLs=; b=XIejPpDLLuPAqRG5Gt1SNLchzrW6HDBB5H77y61q4/NUt5YVhtvQlXisxl1Yj2Uhqo rqIXapUTBDhuSIqgGTavYwcpg6T4qw5tZiQ6gpyh2siqHI+oUeM5JJL3dpReTM+CwH+Y vzttWM7s/ETm+ipRUByaO2Zy5mnWy0SIX85CYD2q74Vy/Du84sr80jmntudYRpcUbd3k lLahzH54YzzwSfyVxPl8AWP0f0bdY8on+a22w/vJlqP5esXq+i7Pz6uAA3lFTafGAvwk mGCBvxll5CPxcbT7flqd3ZMssJqUy0HyM01KWLyBK3xL6mk11pgGujT/k50xWwialIU/ rGoQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=ND08wZXS; 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 sa18-20020a1709076d1200b007ae4717bf08si24656082ejc.359.2023.01.13.05.59.01; Fri, 13 Jan 2023 05:59:25 -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=ND08wZXS; 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 S241716AbjAMNxC (ORCPT + 99 others); Fri, 13 Jan 2023 08:53:02 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51312 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S241591AbjAMNwV (ORCPT ); Fri, 13 Jan 2023 08:52:21 -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 7C8F9669BB for ; Fri, 13 Jan 2023 05:49:06 -0800 (PST) Received: by mail-ej1-x62d.google.com with SMTP id v6so9445112ejg.6 for ; Fri, 13 Jan 2023 05:49:06 -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=qJgtpHz+rrTvZ2HTdfCXZ2CyubMLbgrhsqS57oo+fLs=; b=ND08wZXSDHaD26Y0TCZhButoS5O8WaoV7uieUsnmBfhZHWPpncmTUCs4PJn3K1qSeg zmUzvLwj9sNFB6KtMVa44DjavpJ2SZ+wLrZyXmBA/dkQDh3FXuqNIA1fi8HIhR1rDCaG dKc0juQpNzQk2G1W7Q8wPK3AVhFEv9G+IVROs= 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=qJgtpHz+rrTvZ2HTdfCXZ2CyubMLbgrhsqS57oo+fLs=; b=AjUUpj19A/0UsvDz6AD6AEA00+gqHpmbwJiauFxr+0sD97HBZygG7uqb8DTp6MRtae BC+Jp2p81e7BSkPWIdbiQTYFMHB4xqZede/+UBxptxXPWw5rMr3HG9U7eR0ko+w0Dllj 6iZAvJU97uHV7hrVzL+6mmZ5AgeAqi6/AvyIS0+NJtUWE3+9a3PQGS5OwhWm+apcRr6U 1Ee3/yqslsrphgBlyIrCwkt0a9iFBEgUdcB7g+nmO609VxId4pM7kUGomoNzZtRosXNB MeNevVRcQStzoM/adiD4WwxBf/d8edLTaW8wmwM5gfQ+4Xq187Hb8wwxndEHXdtwUlEd 7FVQ== X-Gm-Message-State: AFqh2kpcvN3d7gD4DLaOERcF7dv7nzpbd9nI6OJM6yCzEVGMeLzSmEk8 3oxggVAx7+ddbREDPun4FhRIujL8M0kpUGk/c5Y= X-Received: by 2002:a17:907:8a24:b0:846:95f9:5b4d with SMTP id sc36-20020a1709078a2400b0084695f95b4dmr90060532ejc.42.1673617744658; Fri, 13 Jan 2023 05:49:04 -0800 (PST) Received: from alco.roam.corp.google.com ([2620:0:1059:10:b023:4272:bddc:acf2]) by smtp.gmail.com with ESMTPSA id w23-20020a1709062f9700b0084d3bf44996sm7072888eji.142.2023.01.13.05.49.03 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 13 Jan 2023 05:49:04 -0800 (PST) From: Ricardo Ribalda Date: Fri, 13 Jan 2023 14:48:48 +0100 Subject: [PATCH v5 2/5] media: uvcvideo: Quirk for invalid dev_sof in Logitech C922 MIME-Version: 1.0 Message-Id: <20220920-resend-hwtimestamp-v5-2-660679c6e148@chromium.org> References: <20220920-resend-hwtimestamp-v5-0-660679c6e148@chromium.org> In-Reply-To: <20220920-resend-hwtimestamp-v5-0-660679c6e148@chromium.org> To: Mauro Carvalho Chehab , Laurent Pinchart Cc: "hn.chen" , linux-kernel@vger.kernel.org, linux-media@vger.kernel.org, Ricardo Ribalda 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=hnq1PFU+jUes6McsuTh1jrRR6iZpqXb1FFM1WIqCsy0=; b=owEBbQKS/ZANAwAKAdE30T7POsSIAcsmYgBjwWFHk2w4RhlH18GhP3/ZqGa/Lgl662roB7gJW0jX tE+jkN6JAjMEAAEKAB0WIQREDzjr+/4oCDLSsx7RN9E+zzrEiAUCY8FhRwAKCRDRN9E+zzrEiFuHD/ 0T+asPS2gJL5WRZcRw41N5tD5bz49be8iTGWEr5yjsM7e/dLMj2gA6jD6+PZE7rAlvHGcXUItOxY7D Z/aYo8edrIpPc+EqSGRuoUvQILn3xFn85iukIjts/l+76aPKp1M7BbxVNHYl7vsJTSHtctB5Rq6XtH 49Kdnk+HPfngySAvC8GouO6GbDsCD04uSN5fsMbh7xcM93NTqMgoCHqTJvdEPCMW6LirNs1MYmTAGM KANqrIB+8sqZi+5khKW4pmuGXxYRNnUQFgM3TqeJXxYVkY6G9UUhk1btaFvNw7/hcdznHoSuEwwf0j CNm/vsAmRQ2OTCUMHG0pZyA1S4BkvLXDGcYx35i93QZbVhrCgT27ZemDky82lZqU2SpoL4Zg/fMUhP vGTrw6sV+JDqzh9JfdETbo9XogvEu6hzlWQZ5CJnUFD9Wuk7xmSXs1wg7NoVx1RTUVq2DjPQGHQl1i ZSsMWfF9uCc6ymrEMo1WKL8ZiXYpFaSlzmYiTlU0cmdkLRVWreLOLnLr1UCPOQ7wLFSH35+Usql0vB SevSDJTpskAE42jfK8Gd9JLb/qjnWHTN3X7jiFyMazMu69B3ESB5ViZ8oIViK3xLGLlp0PXgfJ3N9O 6iRhtHlsD1mBez4uTqHeyddhTupgsUeWXE0TQp+o+fUbf4PjirUCjKx3ludg== 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?1754916051005757507?= X-GMAIL-MSGID: =?utf-8?q?1754916051005757507?= 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 488c58bc9919..f4470f6df45c 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 Fri Jan 13 13:48: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: 43318 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:4e01:0:0:0:0:0 with SMTP id p1csp283134wrt; Fri, 13 Jan 2023 06:00:50 -0800 (PST) X-Google-Smtp-Source: AMrXdXuDW49HffTq6UdWBweK2BsEj/ebo+ARC36EVMCukkfCGL952qPi3stm9lNJZ7rsyDrW88jx X-Received: by 2002:a17:906:c2d8:b0:812:d53e:11f6 with SMTP id ch24-20020a170906c2d800b00812d53e11f6mr3244113ejb.70.1673618450590; Fri, 13 Jan 2023 06:00:50 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1673618450; cv=none; d=google.com; s=arc-20160816; b=fiwRFyiX+A08e56w0/grvorI7UaQ6xqRlvAR5vfBiFdJqEyvp1WgadGM3uy5VJNZ6R aQuinjDV7ngYvtdYtn8LyuU5kptk/j/mVWg9SFknuAeMWPJCfl9ZK89XY5HaU2jsFyag Ln1HCDLQFDfGzmBD7gdaM/Ez4jTLi/ZHMVHFtZDB0wBjAeouknXLi6pLq4seVGRhNyjo yd8Bff7jjJ0OlkXKpzCE5cmjDmLmbAtK397oIIsjJxsRQCxmsBjj7E12U1Cf5JMLJtnC MrLwS4XTqVal3itle7BiXLfarlpxdrxAsQRsAHhhZvXzOmISh7HIKUTDyCBmTWc5SizQ C84g== 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=BIYSq+SG4WtbilzNOWYagt4DvEBoOwZmBVCbkxWrmdg=; b=sAhWbqD70saqZbrO/yMo1V0g6vsTpFiy1rrSnMeSx54VhNn/9H1+fqbH7/XRkHs+2Y eUZAUNmizXZ9Elx03H92TdimBPSuM6me0dN2tipsJBOmQ07MHROcpmVe9wMdb4OaqZhp SNWyRXfqjbUCHETWU/l/QHFFtlwLwduHpUfEroF8U0lhgyhs4cnwrgCsFoXT3uASipTQ vORpvFmVy4rFO9pGgTnOvRHj8i1H0u9f+I3u9ABhoklqWPzCNUeF/vGZ3PhAXHk5p9D+ h3adPEozFWJgogAG9M/8mcYI2t7GDlRQrkTK6xY9SCTkMRpjlBVNK1F/7nO5ZwQYDqO0 Gl9Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=Psf7tBaJ; 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 y9-20020a056402440900b00489644e8bd0si26484235eda.610.2023.01.13.06.00.25; Fri, 13 Jan 2023 06:00:50 -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=Psf7tBaJ; 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 S241540AbjAMNw4 (ORCPT + 99 others); Fri, 13 Jan 2023 08:52:56 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51048 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S241624AbjAMNwV (ORCPT ); Fri, 13 Jan 2023 08:52:21 -0500 Received: from mail-ej1-x632.google.com (mail-ej1-x632.google.com [IPv6:2a00:1450:4864:20::632]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 582A26951E for ; Fri, 13 Jan 2023 05:49:07 -0800 (PST) Received: by mail-ej1-x632.google.com with SMTP id l22so22742678eja.12 for ; Fri, 13 Jan 2023 05:49:07 -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=BIYSq+SG4WtbilzNOWYagt4DvEBoOwZmBVCbkxWrmdg=; b=Psf7tBaJ4kUzf0xvoPiWTIUp/xnmYxZevQ6ZlF2RYXkljmHGkAyZ+3rU/Ykwng7MeL GFT4jRKvJT/LUY5LDmcTukJ5b68E0ywQG+6EQDR6J4Rw9zbm2ROUc7by6meEkx+aOmTE /q17Fpsz/BPRntTDr6/imThxAf80cbNjjx+hs= 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=BIYSq+SG4WtbilzNOWYagt4DvEBoOwZmBVCbkxWrmdg=; b=FUwpqdkP1r/3CDPLWb726EIyXlwLA/fdiZOT6dLh1QaOFh7V3zTPoi4jceb4sKRevc 1eYjGVCxrYzLINXUT2mThXQK7m9w4P6m1FF4yi6bea8/dQaz9wOrXSeeG2omAdTI/slK OK1ubmgGAepgsH275gIzuXUYsgX/RliRfVK+QGhnqmrdW07UdieBiL24tJ3/+9F9jhBU 8cC+KI57fJJ4nGH8JWiJ7XmB7fl68pGk5ehvBt/uPdcPxCpqXEqC5wctDYZYNQw6fJV5 udXLocAOk7Nmtg5JRnV6QwM+WiELwub0BgKgGdVtFDhHQCADLJ2rDyTKvhTEEnrnDjEg 7b3Q== X-Gm-Message-State: AFqh2krMM7v2NfVrVOBP+q3CGikd8wk7buB8xkp/2Tw+2/wlr9J6gSe5 4JHJ3sxPLtedF/dPQQyQzQLEOpdDs3rEJ8WNJ/o= X-Received: by 2002:a17:907:8b1a:b0:84d:34a4:46fd with SMTP id sz26-20020a1709078b1a00b0084d34a446fdmr3495226ejc.71.1673617745694; Fri, 13 Jan 2023 05:49:05 -0800 (PST) Received: from alco.roam.corp.google.com ([2620:0:1059:10:b023:4272:bddc:acf2]) by smtp.gmail.com with ESMTPSA id w23-20020a1709062f9700b0084d3bf44996sm7072888eji.142.2023.01.13.05.49.04 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 13 Jan 2023 05:49:05 -0800 (PST) From: Ricardo Ribalda Date: Fri, 13 Jan 2023 14:48:49 +0100 Subject: [PATCH v5 3/5] media: uvcvideo: Allow hw clock updates with buffers not full MIME-Version: 1.0 Message-Id: <20220920-resend-hwtimestamp-v5-3-660679c6e148@chromium.org> References: <20220920-resend-hwtimestamp-v5-0-660679c6e148@chromium.org> In-Reply-To: <20220920-resend-hwtimestamp-v5-0-660679c6e148@chromium.org> To: Mauro Carvalho Chehab , Laurent Pinchart Cc: "hn.chen" , linux-kernel@vger.kernel.org, linux-media@vger.kernel.org, Ricardo Ribalda 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=ze5myykY7IWxFl+shu/B7fsc0sNffBzVSl+RqFnttm0=; b=owEBbQKS/ZANAwAKAdE30T7POsSIAcsmYgBjwWFJsRwW22gkHsLnSkRocTuSoJA32f8kqplaGddG N4YwKziJAjMEAAEKAB0WIQREDzjr+/4oCDLSsx7RN9E+zzrEiAUCY8FhSQAKCRDRN9E+zzrEiN9VD/ 0XTWp1wD61yuxVx7AJoIHnpjTRZO9X/JAVF2G2p0qj14/oeLVkL9YnLryvrkdYmxnPAIzJUtkroCiN X/eegUJgccziHt2GXqQVm7oBEcOehw2R5WKkoUT5WY7cXOv5rqfedIya76MxqGcpAQfa4KVkX4eBQm QqUvRqBKcBqKryviL9KXUA1zKfRx6PaMMIV/5/OXU2pztfzXjlFIKlntYu08rLeKVR/8upJlEdN0+1 XrfjPHP/PKM2qCY5bTV2siH7/y/QJJ68VFxFLkylkRWjIwgIZP3k0BAYiiTAStlQAz5Lqu7ia/Xg6p g+tc0xduyHVY1fOTWTf2TYdeaaPiBVFdP0R1m+8ubQ+23SNo56/0TEqywOgDkrgT6cIoZWE4XKyI/Y 0cEkwuMnBbL7rGmTi4HMFsNKapWZrtBZW2YRe9MhUNQEtl0UFt3SpQiCimuu1ECSX3N3hQkofgop8B DqOKbGlbhdoyeNgk/HSx1AX6/4Zn7NHUC4YYoFYw235Xr5NCItFs7aUESZl5U3r8nMPsubCpsWHjCV rZRpLurXrSJISxwTCX7OH32f45YY4ImJQWhKJlAQEBsBugv7ClLGfvi9nQybHr+VNTMZmJVUK5w4HO 1MZIksNsbbQ7l+iYvFa4z2exrsAbDlPWclFqxV0GCwCZI9ysjawGZrsZ3k9w== 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?1754916140725191949?= X-GMAIL-MSGID: =?utf-8?q?1754916140725191949?= 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 f4470f6df45c..21985598ff5b 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 Fri Jan 13 13:48: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: 43319 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:4e01:0:0:0:0:0 with SMTP id p1csp283584wrt; Fri, 13 Jan 2023 06:01:32 -0800 (PST) X-Google-Smtp-Source: AMrXdXsgPs7clgYCqqVUivNr0a2gGqw8wMHOec1Mp6GEzj1L/AIV1JG0AydUURs8FGcDogy5ON1w X-Received: by 2002:aa7:d914:0:b0:49b:6449:1d4c with SMTP id a20-20020aa7d914000000b0049b64491d4cmr5930410edr.23.1673618491616; Fri, 13 Jan 2023 06:01:31 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1673618491; cv=none; d=google.com; s=arc-20160816; b=oUv/dIZhF0SVNeVvaaNWUzTY1awk75T8jHFVThBC67j09u+Dnk2+TV43QxakdgjNRX I3T24GVaBZCKOovl9q5nuYmUX4iV+q8PaDV5euFzNecuitIQ6c3eNwdkTgIWYsaCldv5 gBXOodNzaj+AohuN+yBamv1cB1JMHfKrn3MtWvyh75VhYDhUK2yOJxgV+X+68afU98w9 HpOtbdkquyezF8gUDwLP1eDwm8/x638/L+iXipyA9mwCRG0IsO+BG/ZM6As7bePg9Xd/ cFCz6sKRPi0yVB7AX3DsulZiGhiPnM/UHg/WMPbCP8HyxY6csyf6uP1vQ66q7ERDlaV7 u5oQ== 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=2JdQ79/DG3w/NnYpFjhrKQiF9vf+P0kD+HDc/SwBkDU=; b=icqwhlWLNeDjJ1hMtdsfneB1oqZjk81hZXD5imgFrHlVqNtYOz1o8f7ATXgh5jAf7Q VYwTDQCN5lFk2Wflwfq1E04fORRcoX3nI2Ie/Cllx+Q+EJWM47nSyCAMo310rNaUef3k 1ZWLwh6QyqFrPl/XuO646H5aLiIcftBLumV6lH/1Plf+A8oJj+sUABxNX/cqZpIJrQ/I Rgu8R69qty7y9RbsjHaDCXENY8JpdkuXfeL0aIi3lmGlxnXvxVp+qcrZY0HeYGKy5dJ7 vAfZphAy/GSJ3mV0rzJx0W8CscohkJ9ruAp4rqyFdM9Zlu+rdhDZvX6riSpkAbTv5PHV C+eQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=SPSWEpVl; 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 q20-20020a056402519400b004854e1d2682si25190718edd.249.2023.01.13.06.01.06; Fri, 13 Jan 2023 06:01:31 -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=SPSWEpVl; 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 S241335AbjAMNxM (ORCPT + 99 others); Fri, 13 Jan 2023 08:53:12 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51362 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S241802AbjAMNwW (ORCPT ); Fri, 13 Jan 2023 08:52:22 -0500 Received: from mail-ej1-x62b.google.com (mail-ej1-x62b.google.com [IPv6:2a00:1450:4864:20::62b]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A1E6569B1B for ; Fri, 13 Jan 2023 05:49:08 -0800 (PST) Received: by mail-ej1-x62b.google.com with SMTP id mp20so5845660ejc.7 for ; Fri, 13 Jan 2023 05:49:08 -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=2JdQ79/DG3w/NnYpFjhrKQiF9vf+P0kD+HDc/SwBkDU=; b=SPSWEpVlZTdei5NmTNUgjxxFp8CYsjWO5wHQilWmjHPTTHVb4NWA73Yd/jt8L6iGNg 4OFqL9ivs7qwbnHHenui8Ue13qsjG7/8c2ZczkdyZOUlVYljfqhFuetUCbJg4GEpYyD7 MzD350qHCe99aghjFjFso9blEJvSuuma/OgO4= 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=2JdQ79/DG3w/NnYpFjhrKQiF9vf+P0kD+HDc/SwBkDU=; b=IxiEip02ObQpoOsuAWy/SjKtXdqIj+2mrYKpp/gtI0jnYe1uFwkKSQAJNppwdKAWcD pblKzGsdKT4o24x2lIT91t0ENzNj8l2iSinIRZcuEVDrAKu4XPyR6Uch2cnqJGhYwfe5 ZR81UIW1u2elPKxVE1hkqSnNU8TGdQlBr8gLX1eBLZAWFubNks1j+cupMaXgYJ9P22y3 ZXv3Xeu/6HWylYg1UeUH0OXZJsQlEkdsqDlLem4K6mdTXAF2fzX7lcJt8Zdi5+2rMxje Gw/PHL8qXGBy7AenwvrRcMSmfmaGQsTETbBVXEx1MQ5AujrVKGG2vc4TABLOgFO/zZNu qlTA== X-Gm-Message-State: AFqh2kqFQVLJnxrKdut3VHbj+GGxA3xoJleSW0X7pGiCFoqERzGL4+cv vciadBW7bNVdXg0XToyqxhnAiK3pvEiM3T0rDKs= X-Received: by 2002:a17:907:a485:b0:7c1:709d:fa49 with SMTP id vp5-20020a170907a48500b007c1709dfa49mr87888466ejc.18.1673617746803; Fri, 13 Jan 2023 05:49:06 -0800 (PST) Received: from alco.roam.corp.google.com ([2620:0:1059:10:b023:4272:bddc:acf2]) by smtp.gmail.com with ESMTPSA id w23-20020a1709062f9700b0084d3bf44996sm7072888eji.142.2023.01.13.05.49.05 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 13 Jan 2023 05:49:06 -0800 (PST) From: Ricardo Ribalda Date: Fri, 13 Jan 2023 14:48:50 +0100 Subject: [PATCH v5 4/5] media: uvcvideo: Refactor clock circular buffer MIME-Version: 1.0 Message-Id: <20220920-resend-hwtimestamp-v5-4-660679c6e148@chromium.org> References: <20220920-resend-hwtimestamp-v5-0-660679c6e148@chromium.org> In-Reply-To: <20220920-resend-hwtimestamp-v5-0-660679c6e148@chromium.org> To: Mauro Carvalho Chehab , Laurent Pinchart Cc: "hn.chen" , linux-kernel@vger.kernel.org, linux-media@vger.kernel.org, Ricardo Ribalda X-Mailer: b4 0.11.0-dev-696ae X-Developer-Signature: v=1; a=openpgp-sha256; l=6614; i=ribalda@chromium.org; h=from:subject:message-id; bh=r2Bi8jzAlegYJ34KV7vhsUT4PU3/z0Uyq8nK4oWIpCU=; b=owEBbQKS/ZANAwAKAdE30T7POsSIAcsmYgBjwWFKV4PZvGmcZmD3pUGhHBoq/4lKBwYpLBlCb1Og KVnqGqCJAjMEAAEKAB0WIQREDzjr+/4oCDLSsx7RN9E+zzrEiAUCY8FhSgAKCRDRN9E+zzrEiLPLD/ sHCLfMibRlpo7NDo/NG17fdXhmRU63hrmgnPJ3Z1cbh6sfQyLIMRXi3A5vxJJ8gHRtcma3Vo95HR5H 8MGyiIfblutzocaLdP/PjlraBskXJ4oudncy9SdzJ0dnVKmb3y9Pij+X2onf0q/WPT6oHoDpJiIe2F UZuvqBxRiMMdMhaphG4OQdFN5zEz5uWVX+geqxkZs+Mw9gei0ZlgKSQJdeTJSjdWRgQnQB0IatGP/g fE0T/JBIg6lMxUhNphIdXeCMXdvdLgdjYHvtdmijw/iKYJRMcoD+h6f2tKN8ZxnQyC1CTBmxKfxqc0 9CKpdMwi3TX4nnOi1hqgn3FVxl7nAnhMwYUj6ingAGt08V/9v2sshrOkRz1gqwhyA/kH9salQ84U5M xD0ZbymF1jKzxKsqQX+OtrbmmJi+9CxGVSlm29lBCFsBpeuiqn+ndlYhJ103e3hzlb2fZWEkDbl9Hf VQDlWgZNs+WMuyg80SJJCAttMxn2UPZUfDQBjLpmmTv46l5lkUljBR5wTe4HHXy8b4L1mJdThdYW+/ bs2MSMmVOSgrm10aQcvjQm98BiXT5L6eXuVhtjFde9ieLlhLbxjjP/92Gl8jsdbXqajKYtFe8lw25C vWGazIMErHMVn7MEBJcPU6SsFaW60mGJrH9GLCQkVAPiWUllr7+Aav3THNkQ== 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?1754916183362524017?= X-GMAIL-MSGID: =?utf-8?q?1754916183362524017?= 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 21985598ff5b..22255bfa7db9 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 && 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 Fri Jan 13 13:48: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: 43317 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:4e01:0:0:0:0:0 with SMTP id p1csp282336wrt; Fri, 13 Jan 2023 05:59:29 -0800 (PST) X-Google-Smtp-Source: AMrXdXtmYVFbjA/WuI4r3mLC1UKJO9Yj989mk5QEwPxXEcJeRluHhAM7bv5nkVLiBe30Gn6IYWVk X-Received: by 2002:a17:906:12c1:b0:843:a9fe:f112 with SMTP id l1-20020a17090612c100b00843a9fef112mr59156935ejb.16.1673618369641; Fri, 13 Jan 2023 05:59:29 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1673618369; cv=none; d=google.com; s=arc-20160816; b=RlS8sDNQiJhTDELQIw5unpqBkqaCQa9ZfUvEY92GFSbZSY+QYrj0PbW+3xPd6aU6Dr 3tALiDywtY6i4GiOn4oK/+KQeUM2AHZnq2zaXiBUCxbu8x61y2cbdK+VEnJqhbgN1+OI dvAfHm+T6ZjJoJoRpl3iV+eImq3OC5WnMHiGk1AnMeoa3RhFvAtmFag9kl+0OCud7x1z U/MbKPmioe/heklGQDd1kdi/Q3JcNKhGWdzdKEKyNSlbyWmAgeaNKT9q9/If/ia9gr0L 1reC8GSrqPvX2p0dUlbaS7hwFpkXwQRwcv2Th0VR4BgTha12rON5tzdp06k4exyuUfsM XGrA== 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=dDfjDeTst6OQxRVhR2qypSB7k7XxFsYPOvv4qs9VTxY=; b=I+TXJmnmnIYHspNspApLqNfCh4JOH9gyGFeLhbQ2rr1NZXl57CSgHPgOjuFyLFLGwV HXIZrpnjer7w/xBkZjCitT30qCGVhqNKnkqY7lPBqKvC0cJsfE4W6tBLvoUNqzc/qU6k dadspDTeC3JnCLt80C5sjp3g0HsNtHVTNrp47uTTX8ZForESlWDUicmMBCCfBf/8e35G vU5RFq4sx+iL4eDyjUHbSB8npxYnBTKyhV9STA9/zqpiRLc4UqXO2qpHfty5jmEF0nH4 Jn/8fvOROVwYaiUirMVCWxbZGg9nv4A740ly3XtiXsBFP0hNKSHluVHlM4ZC0dJmDGC8 D0Dg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=knAfm9vH; 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 ds12-20020a170907724c00b007ea638b5670si20196069ejc.485.2023.01.13.05.59.05; Fri, 13 Jan 2023 05:59:29 -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=knAfm9vH; 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 S241189AbjAMNxT (ORCPT + 99 others); Fri, 13 Jan 2023 08:53:19 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51378 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S241324AbjAMNwX (ORCPT ); Fri, 13 Jan 2023 08:52:23 -0500 Received: from mail-ej1-x62b.google.com (mail-ej1-x62b.google.com [IPv6:2a00:1450:4864:20::62b]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 263DD69525 for ; Fri, 13 Jan 2023 05:49:09 -0800 (PST) Received: by mail-ej1-x62b.google.com with SMTP id qk9so52475021ejc.3 for ; Fri, 13 Jan 2023 05:49:09 -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=dDfjDeTst6OQxRVhR2qypSB7k7XxFsYPOvv4qs9VTxY=; b=knAfm9vHU6HiEfv+gWrWbwIGU0C9RXL7SSqRpLkf//zZYBgXkmztEsQYrNOvz6nSuU Mnc9MPPtOhSg+BV/ZLQP05GW1CQRoEIjJYyUZUVyyyT9qaajgokS1qEcINf3xr6eOjoB ekkr2py3iWWrMxt39pNGi55cyzez2HzjOUQXE= 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=dDfjDeTst6OQxRVhR2qypSB7k7XxFsYPOvv4qs9VTxY=; b=lB2N7WdjFDXyaPnz6JR7euRWtsg/uYWGuWw7OzehQr2vapjIjvMy2Q+EYVKRXr1MDo oZZJECmZbcF+pkSYN1IecQ1SeB30FxrvH8ecL0eog4MNvHcY8Yqy3XWS+CjfzmULrFYh LYLBPFRLUTvD6pJKMCexTvnQnYKHS5btYAkqzg+IVaxJF7bj56p4WnHbH8F1iGu2Z4u4 3KG4V4wq7eYK09sBUeraXMsQ647Mtuse1oBLd/hnDTnuiOPWqBiAriu8rVscbsybMWKZ x5uE7wKiNk0ZD/wBmZLSuUZI8djROGgkTbPnKBqqS89avcQnhfLWu3ZCU+eTfu5FWYwj Vicw== X-Gm-Message-State: AFqh2kq6c5RlW5mY7FpBd3PAzq9tz7i1m9dPIFeasSxXxID3OZrUJOgO DxP7nd5+xmrE/V7CekAvYAWF3W0dRxVJSJx07DE= X-Received: by 2002:a17:907:d312:b0:84d:3543:fa7b with SMTP id vg18-20020a170907d31200b0084d3543fa7bmr22367374ejc.54.1673617747573; Fri, 13 Jan 2023 05:49:07 -0800 (PST) Received: from alco.roam.corp.google.com ([2620:0:1059:10:b023:4272:bddc:acf2]) by smtp.gmail.com with ESMTPSA id w23-20020a1709062f9700b0084d3bf44996sm7072888eji.142.2023.01.13.05.49.06 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 13 Jan 2023 05:49:07 -0800 (PST) From: Ricardo Ribalda Date: Fri, 13 Jan 2023 14:48:51 +0100 Subject: [PATCH v5 5/5] media: uvcvideo: Fix hw timestamp handling for slow FPS MIME-Version: 1.0 Message-Id: <20220920-resend-hwtimestamp-v5-5-660679c6e148@chromium.org> References: <20220920-resend-hwtimestamp-v5-0-660679c6e148@chromium.org> In-Reply-To: <20220920-resend-hwtimestamp-v5-0-660679c6e148@chromium.org> To: Mauro Carvalho Chehab , Laurent Pinchart Cc: "hn.chen" , linux-kernel@vger.kernel.org, linux-media@vger.kernel.org, Ricardo Ribalda 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=78AsqBPCn9SSTg96NmP9pgJexa1mmA+uxtg7QUEhcrg=; b=owEBbQKS/ZANAwAKAdE30T7POsSIAcsmYgBjwWFLQMt/hm9dgwBbvtkC6/YYapOI4tQlTEdEbj/n BT8kx8uJAjMEAAEKAB0WIQREDzjr+/4oCDLSsx7RN9E+zzrEiAUCY8FhSwAKCRDRN9E+zzrEiKteD/ 4pR6X/2GxhnuA2bKPD6ZrG9DiHOPaonuT/jZsm6Wj7UCTOJsQ41RCFqfLkdl3DQ4xuERRvjkHcTznj 0CcJ8p6yhO6GGa4NctZGXAj3MBSsRUVkeeHhCd5VCGaYfBcuhLbHJIljtgValjqnoh7IuTqxgJPWKw j172qlY7ZysYOcQGX5Pj/WZMyHA3ZBQTVx3voDEmG0StmdlbCq8d0D/C829plELKJ7DFlsqNapEFQC uKauAa/6tgSpd9QEyeqTJ+eGVGO4YD1CAikeQlTOVSujLFUUeMkOFQwlGyi+m8ROcHm2E9bUMn6TOy xhgIO0F14fClachp79yCkO+uRvhNq51OlcLDZ5uOtlY4CaSN9NfFnI3FSjqJRcDko//WRW4Sru9hBP jwKONpryM3GhC4VUbW5r5gnwB4OPSEZ2sHKxL5i7Fkzoapzi5R7SjA8dyrMBL6u14apAA7I12IAA9J NJzOc781CxMGOZh2gP0WwFZs6Yb7BKY0nbS2BYCPYyFzYy0i8uf26GCB7SmXu4WKhcYEZ9zpHm5yqT QeO1vfzwk4WTbBTs6IpW4kswrKJ2Wc4V/m76Mlh/LH/tccCnBgOY+LL+hMphKwep4Zf1ZUV9debsUB vy0hwGe5pDX/ZNdJRE6Yt0F1pBASpnMVcyur7+vWzz6nvB/G4coLVIaSbqwQ== 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?1754916055783883813?= X-GMAIL-MSGID: =?utf-8?q?1754916055783883813?= 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 22255bfa7db9..ae692122d5dc 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;