From patchwork Fri Mar 10 09:01:28 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ricardo Ribalda X-Patchwork-Id: 67288 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:5915:0:0:0:0:0 with SMTP id v21csp766323wrd; Fri, 10 Mar 2023 01:15:47 -0800 (PST) X-Google-Smtp-Source: AK7set+ys8BG9wKg+Y0eSYNqPsfFM/Hr9VTxHB2PWX13XuA4SVYZwFr/P+bqmrDyht61ou81uoo/ X-Received: by 2002:a17:90b:3c49:b0:237:5a3c:e86c with SMTP id pm9-20020a17090b3c4900b002375a3ce86cmr24881794pjb.24.1678439746865; Fri, 10 Mar 2023 01:15:46 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1678439746; cv=none; d=google.com; s=arc-20160816; b=Nor+WI/8spqhHGh5FwPG5Q/GrvAqynBrD1e76bSAzr4b2aRPKWKk/YAsCUHSo7gcla 1/azwf4vvNAvGRpVAGG1kf22hvebRuZ6ysmy2JKvTdg7ANE5a8CgNXcjYb82TgjFg7Ex QrsSPCvJGb7t/KSet4DqjswZxxbUtWmE4cnNLmqDnuWq3zLGY7FYXovAcqPwXkBVozu+ DQrCxmmibdMlGpt+V19NdHUQ94mrAgvtiVMRG83t0qDAuVqFp8a6bQVNsHVePmsnKQch 3T6K4NG3D/3kkerDYhINbwbCurDCRShGFEXS3OKQVDGEOi8eRo4nx9mbc+hwcmYp/NpB kd4Q== 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=sM9VHBcEDRumQi7DFlwhwQNZqP1X9niavDP72C2S7Eg=; b=q7ifbQBiCKXBz+kFLWTVl6g9v6dQk9KjkfXDEtrs2a/SlHZHg6ZUjaZLYNrByIzNWN v2ShFInT5OmsuU8C5oMTxiytmGu1ppE+sp84BYut3Z9OGvwyYGNJ2WHdef8k3yg7xuQH Rdn2U8Cml+0dMEaFnxjvnBusYjaR/wIWEM9ZFKvF+M5v8kdHlvGP2BDAVIbHSAyb07UT 0zGxfvrgA5u6aWIQfpFysU8YdVBPAyHhPbz+iCaI4PleSuGk5PDwsNjzJPFUliKKUZYZ /aXuQerP3Lv9JWqayHAiUJATpY2Cc3dTRID3BN8IcSZLIoIk2knFiNokJZlTheXcM9Cn S6mg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=TVlNXxSG; 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 u12-20020a6540cc000000b00502efeb73e1si1396396pgp.717.2023.03.10.01.15.34; Fri, 10 Mar 2023 01:15: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=TVlNXxSG; 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 S230488AbjCJJG6 (ORCPT + 99 others); Fri, 10 Mar 2023 04:06:58 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41110 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229995AbjCJJGZ (ORCPT ); Fri, 10 Mar 2023 04:06:25 -0500 Received: from mail-ed1-x530.google.com (mail-ed1-x530.google.com [IPv6:2a00:1450:4864:20::530]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1DE6C1091E7 for ; Fri, 10 Mar 2023 01:02:20 -0800 (PST) Received: by mail-ed1-x530.google.com with SMTP id k10so17533501edk.13 for ; Fri, 10 Mar 2023 01:02:20 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1678438931; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:from:to:cc:subject:date:message-id :reply-to; bh=sM9VHBcEDRumQi7DFlwhwQNZqP1X9niavDP72C2S7Eg=; b=TVlNXxSG8XHLd9IHtxlECmOJzD6guBdiBHWFuA2oRcTnWkiHEwH/DhlRlB4u3twMm/ VQNL6Y3YXytpHhizGRely/Kkn9Kf29KV/ot84gRy6fd8cB8jZDrMTSLcCTEVbA2oEHLv n/TYbZ407vY6GEaEWyIUP+70D2BeyIk9wNii0= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1678438931; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=sM9VHBcEDRumQi7DFlwhwQNZqP1X9niavDP72C2S7Eg=; b=UbKixM9MjKzE8YlhuofSPhDhuxme44+vG79C+uWqMmv2vwwp7Tc6IpK48a0VyicTmZ 0cmlp7H3nrj34fo0U4JICVyc/0pS01fZflFYDGuai7gk/H2WyJhfJqmER1BiT4X4oPS9 82bQ52taIhlFvtGHTzGkPZyDYVVRsS1Q89sw8+uXKMt1t2+1XwP0vKz8fpCWlhtRuioy HOLqQnHoc+8Ziboaj/NwOt2r32rUgyzfKqoVqYmoQ8dFFsNlPPMk0R+kABiRQj8qU/n3 SwWM1BGhFBhgVxS3J5MAgR4FoE7ldgUXOk287tqAx/nEN67NP/hHr+jCy2+T2qnPe9PR eX3A== X-Gm-Message-State: AO0yUKWL/4Z4BPEN1Sxe1RvJ6xOw9Wk0zHaV8z3Vlzmf4bf15uXZBfzy 00NJxbQO4+H7eGPfGsYsLsH8sA== X-Received: by 2002:a17:906:dfca:b0:878:7471:6da7 with SMTP id jt10-20020a170906dfca00b0087874716da7mr26254638ejc.66.1678438931628; Fri, 10 Mar 2023 01:02:11 -0800 (PST) Received: from alco.roam.corp.google.com ([2620:0:1059:10:ef69:7ab6:87ac:99f]) by smtp.gmail.com with ESMTPSA id s5-20020a170906454500b008b23b22b062sm692931ejq.114.2023.03.10.01.02.10 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 10 Mar 2023 01:02:11 -0800 (PST) From: Ricardo Ribalda Date: Fri, 10 Mar 2023 10:01:28 +0100 Subject: [PATCH v7 1/6] media: uvcvideo: Fix underflow addressing on hw timestamp MIME-Version: 1.0 Message-Id: <20220920-resend-hwtimestamp-v7-1-cf1d78bb8821@chromium.org> References: <20220920-resend-hwtimestamp-v7-0-cf1d78bb8821@chromium.org> In-Reply-To: <20220920-resend-hwtimestamp-v7-0-cf1d78bb8821@chromium.org> To: Laurent Pinchart , Mauro Carvalho Chehab Cc: Ricardo Ribalda , linux-kernel@vger.kernel.org, linux-media@vger.kernel.org, Sergey Senozhatsky , "hn.chen" X-Mailer: b4 0.11.0-dev-696ae X-Developer-Signature: v=1; a=openpgp-sha256; l=950; i=ribalda@chromium.org; h=from:subject:message-id; bh=Z9vWlYOlXeYS2ei5qnSybP3k1rYf5ivkf/cMeBsEyPU=; b=owEBbQKS/ZANAwAKAdE30T7POsSIAcsmYgBkCvIILFEZ+h9JBJEGf1HkEqI1CrPZiSpVEZYgh7Vw cvf169uJAjMEAAEKAB0WIQREDzjr+/4oCDLSsx7RN9E+zzrEiAUCZAryCAAKCRDRN9E+zzrEiBKlD/ 4kmB9H1te84VZ66FA71pBUtqzKWkX+f/iYsSMJBJ4Dk52qd2zBZNoj18Ybe8SlaSivaHRmWeYCAZsV 7ZEg6ClLwZnTrjaFSAbdniaVoyRSEx/03c90MUSiEPjiR+q2h2RWm7zNuNP4BU4SmMlapZaGTBdkWi hVISXkDwG9+cFGP6EW73PNBgYPiKiqBRtJIxzU6pmm3N0QNzjma9ysmQgP6Ac2iABDbBSrNz9R08iG qSmObMdGjjF9qoX+6xCZn9q7lfEIzBLX6NGaKqIyr0geKk2VmZHzhFSTSxyvz65Z64yHPr5Mqyg7fj W6jlRoBEWAJWjPiHbAfvw/dunprnh4BOoMToYz18AbBjdzYDtVnQ57qJn3lf7i2rjpKb8J+iEMyxCs a0ynjFPW89A5JycwNZzRNl5RQykgT/rFyQzz5lSjZIRIve3YioWPthxoIIf7Wo3cSg33NXGbq9VMNB HFc/JNx6GhIygLvVNOj9DSWs981QsOjomlkODEyzinkpOgEVNz1vn687axiilzriSeKOREHIgR6nf8 Io58irzLBEyzkm7AMDTaYRxe7SBWlTvzy9qwAoP/YmNYOiOYMrLGRgyGLEIysMEPWJChVdIJOHo42W qgqTkFExayDJBFCcIxsuy32KWL1oRgfaCel4l/kyr5oA43llpNHc6aQoEsjA== X-Developer-Key: i=ribalda@chromium.org; a=openpgp; fpr=9EC3BB66E2FC129A6F90B39556A0D81F9F782DA9 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,URIBL_BLOCKED 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?1759971635920692265?= X-GMAIL-MSGID: =?utf-8?q?1759971635920692265?= If head is 0, we will be addressing clock->samples[-1], which will result in undefined behaviour. Fixes: 66847ef013cc ("[media] uvcvideo: Add UVC timestamps support") Signed-off-by: Ricardo Ribalda --- drivers/media/usb/uvc/uvc_video.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/drivers/media/usb/uvc/uvc_video.c b/drivers/media/usb/uvc/uvc_video.c index d4b023d4de7c..4ff4ab4471fe 100644 --- a/drivers/media/usb/uvc/uvc_video.c +++ b/drivers/media/usb/uvc/uvc_video.c @@ -732,7 +732,7 @@ void uvc_video_clock_update(struct uvc_streaming *stream, goto done; first = &clock->samples[clock->head]; - last = &clock->samples[(clock->head - 1) % clock->size]; + last = &clock->samples[(clock->head - 1 + clock->size) % clock->size]; /* First step, PTS to SOF conversion. */ delta_stc = buf->pts - (1UL << 31); From patchwork Fri Mar 10 09:01:29 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ricardo Ribalda X-Patchwork-Id: 67290 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:5915:0:0:0:0:0 with SMTP id v21csp766585wrd; Fri, 10 Mar 2023 01:16:29 -0800 (PST) X-Google-Smtp-Source: AK7set/s5gdbL2fYBjZntT+85idjjaXLk75xeWKuzcltzGpqJtR2Hl+ISNBJAZ9FpnsJrj2NIV0L X-Received: by 2002:a05:6a00:2887:b0:5a8:aa5e:4bc3 with SMTP id ch7-20020a056a00288700b005a8aa5e4bc3mr1294549pfb.1.1678439789664; Fri, 10 Mar 2023 01:16:29 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1678439789; cv=none; d=google.com; s=arc-20160816; b=K1Eo5XDHVYtabtjZQD+5Dxz9y2alXCniek4WBUf5LDk0KMn2R04mBMr9R6OZ9POUSs x9nGH9Rgl/XiEPDCVwv9xSvfFtWBJl9WGecrunM8Rk80cCqphGQd4OOP2rUaH9dLLVxZ cIcoIzOmL2extW4BObIZNQWnoj9B4B0kJ9lZc36736QWmqTDLivt9O1/CmRKScxYKRDR ZwBxTPKPv8wok8SNF+KFX5JgKS1ccOIKauI0iU1me48ClpSg1rFKK58eNbLrWwG1WzUb gjOzn6CclqVOHh6pxKSLg1U1hUR0tbooJjYlfsegsa2Vw/WkgtMuomg/EiX/Xz1eNgDL MXvQ== 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=Ks+I0iL9S/eJzdcQJQgtcbFNoYKHK8Hr97Z4+vCos3E=; b=pkO56bdSxIQobY6oGiEVcHQDQnk+Y9VUD3TYNrBeHJNh00N9ESg1qzvqMDiR/pGDas WRRQVB7CAOKNQNVjT8rckHVsu3OW5MBjg3v9uV+jGrefJIlYB1wM/HRZD+0dHtUA5N9I NyXXfzGb2QFFy5iQrJUjmQXyUrB/XPQQsszGaO6V1bvVu+D+trGQdbcgCG8x/ug/VPGJ T+zUMWfkCBlcleP17MOagUqUfwSJ7qQx9QAc3tZjkfPruRQtT4FT3KWJ5itWvAVSgrX/ OlEVWMBpRu7ei3FOXbXJcq7BT20K6RL6EbJ8EcGHOsA2l9VFWHC4+UWv0nj0iIScb7ZG +3Jw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=UpQqQOkT; 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 i190-20020a6387c7000000b00502d75eb815si1501049pge.757.2023.03.10.01.16.17; Fri, 10 Mar 2023 01:16: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=UpQqQOkT; 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 S231517AbjCJJHD (ORCPT + 99 others); Fri, 10 Mar 2023 04:07:03 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45230 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230519AbjCJJG3 (ORCPT ); Fri, 10 Mar 2023 04:06:29 -0500 Received: from mail-ed1-x52e.google.com (mail-ed1-x52e.google.com [IPv6:2a00:1450:4864:20::52e]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id CEE9710822B for ; Fri, 10 Mar 2023 01:02:23 -0800 (PST) Received: by mail-ed1-x52e.google.com with SMTP id j11so17665143edq.4 for ; Fri, 10 Mar 2023 01:02:23 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1678438932; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:from:to:cc:subject:date:message-id :reply-to; bh=Ks+I0iL9S/eJzdcQJQgtcbFNoYKHK8Hr97Z4+vCos3E=; b=UpQqQOkT1NsmAXYZfYiPuqaLhTysktOoQDcEi4v3ki6rszPwB7IOUMArrRQnua/XVs ABo1zDYVHqt4o/GGxMUj/GrtYrdGgYG/HA+ET6vNHiXF6UNuVyGoO0En6h1CaZvMs8Ln Ed8LbCjkL1OkI5PSh8mIO52PUAo9wFWDYkw2E= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1678438932; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=Ks+I0iL9S/eJzdcQJQgtcbFNoYKHK8Hr97Z4+vCos3E=; b=tcfvduu7BJ/LGxqy4QE29jzqHGKrZJ2nACB2ZIznYfjrSnx8taumloIIh0G/zGMi1Q 37Ng3r0oQifxpUxzaKUoRya3DF330pP5/vKMOpH76Vzt60auHCVSXbZZBiL9ryztvlCf I0Jc+VkW0kgoiEKrojTsoEw980bFg0iY99ddL01/TJ/XsZKTiy/tebqWlEEiGrQlEWP7 WuKf51roLVTjwtnR9zgamu4DZbZPa5WzmR8GUVCWlTMkMw4knr9xAb2eqkrntlcOQp4t 5gSUyW1bfxPWPoChacezstH4EphX/R4OSteI1tLw1sIoTtwz4MpvL4iAlwvMOUgUEkEs lawQ== X-Gm-Message-State: AO0yUKW0e6KXSBhna+nlpDoYvT8l1MtFX1zuRZlY+JvmeVzXx7G/EcSJ tFIAdjQRhM2a/CpRq0hYlyeKWQ== X-Received: by 2002:a17:907:9d17:b0:888:5d34:dc79 with SMTP id kt23-20020a1709079d1700b008885d34dc79mr28576821ejc.40.1678438932476; Fri, 10 Mar 2023 01:02:12 -0800 (PST) Received: from alco.roam.corp.google.com ([2620:0:1059:10:ef69:7ab6:87ac:99f]) by smtp.gmail.com with ESMTPSA id s5-20020a170906454500b008b23b22b062sm692931ejq.114.2023.03.10.01.02.11 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 10 Mar 2023 01:02:12 -0800 (PST) From: Ricardo Ribalda Date: Fri, 10 Mar 2023 10:01:29 +0100 Subject: [PATCH v7 2/6] media: uvcvideo: Ignore empty TS packets MIME-Version: 1.0 Message-Id: <20220920-resend-hwtimestamp-v7-2-cf1d78bb8821@chromium.org> References: <20220920-resend-hwtimestamp-v7-0-cf1d78bb8821@chromium.org> In-Reply-To: <20220920-resend-hwtimestamp-v7-0-cf1d78bb8821@chromium.org> To: Laurent Pinchart , Mauro Carvalho Chehab Cc: Ricardo Ribalda , linux-kernel@vger.kernel.org, linux-media@vger.kernel.org, Sergey Senozhatsky , "hn.chen" X-Mailer: b4 0.11.0-dev-696ae X-Developer-Signature: v=1; a=openpgp-sha256; l=3922; i=ribalda@chromium.org; h=from:subject:message-id; bh=+e1rJhTRZYFtyKcR4/N8/nQKunH8/Dqv1jSdJGbbaL4=; b=owEBbQKS/ZANAwAKAdE30T7POsSIAcsmYgBkCvIJyM5E8SM1e6cOc0A6OINFuwFO6Mmq+lkzoSNt bNYna4WJAjMEAAEKAB0WIQREDzjr+/4oCDLSsx7RN9E+zzrEiAUCZAryCQAKCRDRN9E+zzrEiAEuD/ 9H0447j0gDhLx/5bRMVTMgJhFKnQ/L1rvPm37RR9ps7x9qgCO29kQj9kWsX56YMhVDnhkNkMTOhH5e 8uFtimI7mIw7XcFXvnRr4tvq0awvT8yACmY83rr9wdUVRhXPJSbxHDEJAG++weCFNfG0J4svFjLzC2 Uq84n9YMPtgP+Ht8BgASNe/2iFzHbn3p55EIsinmpGU+qfbbr+n3lL5pwAh2X0IYsw6jwZlGoKCWnN okJWFw+t77X0O3dvC/AQDkkN0Gcx13tuzssOiq031+IewyXMls5VBdiVjqXZ1iWqNzQD4Mu+3wNb6C ETqNy1ZRnmUzhjnA49QvCnP4CM5y9Hw5IEjvQQUP4M18+lnpJ1zFo9e69Z1wf/v4kZx1JPRW5BBpfv uh2b0lowHnSJzUGspUnf9noxAl0PghWhq6EU/MrJBEe4lcD1GnwrbjKsKpRVr6wRKwK4gJVpXLuCK1 Uvv6FErORhehJJNGYlWGWYPemfCRHsiF9AWr/AEGiWa9xbKeWzAlbGjr8/Er+IU/Cb8YMiFVYnPkeM syTM0zAeeagjMrUYRlGgQzChcUxRi2KnHFq2KM8dfhE2gD5S2YZGvEQkEz4jc2QefCdN0N93LKi30g z4yTE8B6SeabUihtJoyxVRUHM4QYj/rAin7/ZVDaXyKfNIy3ufvGGUT2lUJw== X-Developer-Key: i=ribalda@chromium.org; a=openpgp; fpr=9EC3BB66E2FC129A6F90B39556A0D81F9F782DA9 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,URIBL_BLOCKED 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?1759971680804160874?= X-GMAIL-MSGID: =?utf-8?q?1759971680804160874?= Some SunplusIT cameras took a borderline interpretation of the UVC 1.5 standard, and fill the PTS and SCR fields with invalid data if the package does not contain data. "STC must be captured when the first video data of a video frame is put on the USB bus." Eg: buffer: 0xa7755c00 len 000012 header:0x8c stc 00000000 sof 0000 pts 00000000 buffer: 0xa7755c00 len 000012 header:0x8c stc 00000000 sof 0000 pts 00000000 buffer: 0xa7755c00 len 000668 header:0x8c stc 73779dba sof 070c pts 7376d37a This borderline/buggy interpretation has been implemented in a variety of devices, from directly SunplusIT and from other OEMs that rebrand SunplusIT products. So quirking based on VID:PID will be problematic. All the affected modules have the following extension unit: VideoControl Interface Descriptor: guidExtensionCode {82066163-7050-ab49-b8cc-b3855e8d221d} But the vendor plans to use that GUID in the future and fix the bug, this means that we should use heuristic to figure out the broken packets. This patch takes care of this. lsusb of one of the affected cameras: Bus 001 Device 003: ID 1bcf:2a01 Sunplus Innovation Technology Inc. Device Descriptor: bLength 18 bDescriptorType 1 bcdUSB 2.01 bDeviceClass 239 Miscellaneous Device bDeviceSubClass 2 ? bDeviceProtocol 1 Interface Association bMaxPacketSize0 64 idVendor 0x1bcf Sunplus Innovation Technology Inc. idProduct 0x2a01 bcdDevice 0.02 iManufacturer 1 SunplusIT Inc iProduct 2 HanChen Wise Camera iSerial 3 01.00.00 bNumConfigurations 1 Tested-by: HungNien Chen Reviewed-by: Sergey Senozhatsky Signed-off-by: Ricardo Ribalda --- drivers/media/usb/uvc/uvc_video.c | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/drivers/media/usb/uvc/uvc_video.c b/drivers/media/usb/uvc/uvc_video.c index 4ff4ab4471fe..1f416c494acc 100644 --- a/drivers/media/usb/uvc/uvc_video.c +++ b/drivers/media/usb/uvc/uvc_video.c @@ -478,6 +478,7 @@ uvc_video_clock_decode(struct uvc_streaming *stream, struct uvc_buffer *buf, ktime_t time; u16 host_sof; u16 dev_sof; + u32 dev_stc; switch (data[1] & (UVC_STREAM_PTS | UVC_STREAM_SCR)) { case UVC_STREAM_PTS | UVC_STREAM_SCR: @@ -526,6 +527,23 @@ uvc_video_clock_decode(struct uvc_streaming *stream, struct uvc_buffer *buf, if (dev_sof == stream->clock.last_sof) return; + dev_stc = get_unaligned_le32(&data[header_size - 6]); + + /* + * STC (Source Time Clock) is the clock used by the camera. The UVC 1.5 + * standard states that it "must be captured when the first video data + * of a video frame is put on the USB bus". + * Most of the vendors, clear the `UVC_STREAM_SCR` bit when the data is + * not valid, other vendors always set the `UVC_STREAM_SCR` bit and + * expect that the driver only samples the stc if there is data on the + * packet. + * Ignore all the hardware timestamp information if there is no data + * and stc and sof are zero. + */ + if (buf && buf->bytesused == 0 && len == header_size && + dev_stc == 0 && dev_sof == 0) + return; + stream->clock.last_sof = dev_sof; host_sof = usb_get_current_frame_number(stream->dev->udev); @@ -564,7 +582,7 @@ uvc_video_clock_decode(struct uvc_streaming *stream, struct uvc_buffer *buf, spin_lock_irqsave(&stream->clock.lock, flags); sample = &stream->clock.samples[stream->clock.head]; - sample->dev_stc = get_unaligned_le32(&data[header_size - 6]); + sample->dev_stc = dev_stc; sample->dev_sof = dev_sof; sample->host_sof = host_sof; sample->host_time = time; From patchwork Fri Mar 10 09:01:30 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ricardo Ribalda X-Patchwork-Id: 67287 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:5915:0:0:0:0:0 with SMTP id v21csp766318wrd; Fri, 10 Mar 2023 01:15:45 -0800 (PST) X-Google-Smtp-Source: AK7set+8Vv72Md+iUIWoaKHOCQGuxqs9XoI+NbqUJfvGVJlVbVWPPaZNkbEtTcyCwPyZR20tY8z3 X-Received: by 2002:a17:903:120b:b0:19c:bcb7:a3f4 with SMTP id l11-20020a170903120b00b0019cbcb7a3f4mr28596810plh.54.1678439745180; Fri, 10 Mar 2023 01:15:45 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1678439745; cv=none; d=google.com; s=arc-20160816; b=Dg1DQnBdGfcKECCPzUX1/85xMOLlfzUuWCOvHhYnKmh6MhTJTXKZt/L+m7ffs1OAPv XzdjyWiFKfxHlLxx4n51WxZDvGI+qq/mNKM47NBZrTDRPEhcQURUA0uYYViOqmwk9jlG yR3AlydmkxILKEVycG7sEV4G8H8F55UfTQ1hHFQzANkZ7SYwe7Z4z225dwULlOEeZiTb d5z7/l8zherXGo9QF40Pdv6pajHT+VfktsKww9eTC/K3a/0+F1uKABlD0IXCUawR7f3+ ipjQeKk8H3NsjXv09IJ4EGLfI2iKmbwZMiYznjKMGtYgIDv8dpQ+WSzs2iwrDK6g5ryz oT7A== 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=a5a+6HYmuNF0NcA/SU/mW3fBSurTLFx+qA7YB8EkD48=; b=FfSI087ALsDsve3cJD1vzi7GscXJk/y6nYqwmN/97OUxns0IdWDvof4a0Q9o7xkR+U iV77sJBTPiPz88/JkCJ3sbYFWbw1NZKqvONjFteRaweb2LSvJzGrwNp3osvsTK0166U0 1/HE5EitYffjE71jQHpUFG6a7qpLcVX7YiGkNZXsWp/qIS7G93hyP27/IEA+0Hlo7DJH pPVWYQLZ+pzMCcIhpa+1ZFTErGwLdqhcJWZYizkIktsdfAfCmzpIj66QxsQCKu61mbQK Vv0tQYemqXYDKYtxinlWw0zbpSFfwo67y4sqbTrLH7gRryH/DXxWDRXjCuHaesP/hCnu G/XA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=SQ1tcv8B; 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 bf3-20020a170902b90300b0019ae58cae8esi1679032plb.120.2023.03.10.01.15.31; Fri, 10 Mar 2023 01:15:45 -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=SQ1tcv8B; 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 S231529AbjCJJHY (ORCPT + 99 others); Fri, 10 Mar 2023 04:07:24 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42132 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231438AbjCJJGh (ORCPT ); Fri, 10 Mar 2023 04:06:37 -0500 Received: from mail-ed1-x531.google.com (mail-ed1-x531.google.com [IPv6:2a00:1450:4864:20::531]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 3BBB0112581 for ; Fri, 10 Mar 2023 01:02:33 -0800 (PST) Received: by mail-ed1-x531.google.com with SMTP id ec29so17630377edb.6 for ; Fri, 10 Mar 2023 01:02:33 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1678438933; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:from:to:cc:subject:date:message-id :reply-to; bh=a5a+6HYmuNF0NcA/SU/mW3fBSurTLFx+qA7YB8EkD48=; b=SQ1tcv8Bj4AQO0mGVa6QGvn2qt/K+uVY9E1vSWP5QhD9LY+6LcKGm49RSuz69L7GUf nA1dM3ZL0w7NL8NPkPLt+rc54BLdvrUddhb8LUdDrSgLc/p2PJt1RV+y4yWb0adhD1YD AvtdRHoFjzyiAsSUnbg1qaVZBv+c3j9hRiVAA= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1678438933; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=a5a+6HYmuNF0NcA/SU/mW3fBSurTLFx+qA7YB8EkD48=; b=L6YC0FmAdyIKgvXHjaeMpEOivAT7+cGo8hX9JnPc05tL1FzzylEC5VouhQ4lJM6Dck 6Q7NyvwOgKIrjunES2tmVT0osGeGJkOb4/ELwM5R+v91JLndwvvEZq0h/l1TqudlOi6z Gyz8IPovsTTupmuLZ8FT0hEybRpLCtgG0JTPK7at/UuPRgZE7o3vXGWVccKHGjNd9XRZ GWJzlpzgCF79pH8kqXeCM81avfcQo0TKx/WoM1utkMHpsKI2BwFcpCRHMCqhrnvJWw3e 0xg+9vyq4v9xxuMMV6k6HdzpUmaOS1Gss+Xv1J8TzN66cxM/6ODD7YAld4vSqTKjT4v+ vlIg== X-Gm-Message-State: AO0yUKVZSMcIHk0A4xWkQvn3CSEinniQYVIbDQoKrUTiqaQNZH+5tlrk ozgRJPxWzXFlSD99Q3kl2Kaelob+aXpTZPsgyvw= X-Received: by 2002:a17:907:c282:b0:884:930:b017 with SMTP id tk2-20020a170907c28200b008840930b017mr30309066ejc.60.1678438933400; Fri, 10 Mar 2023 01:02:13 -0800 (PST) Received: from alco.roam.corp.google.com ([2620:0:1059:10:ef69:7ab6:87ac:99f]) by smtp.gmail.com with ESMTPSA id s5-20020a170906454500b008b23b22b062sm692931ejq.114.2023.03.10.01.02.12 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 10 Mar 2023 01:02:13 -0800 (PST) From: Ricardo Ribalda Date: Fri, 10 Mar 2023 10:01:30 +0100 Subject: [PATCH v7 3/6] media: uvcvideo: Quirk for invalid dev_sof in Logitech C922 MIME-Version: 1.0 Message-Id: <20220920-resend-hwtimestamp-v7-3-cf1d78bb8821@chromium.org> References: <20220920-resend-hwtimestamp-v7-0-cf1d78bb8821@chromium.org> In-Reply-To: <20220920-resend-hwtimestamp-v7-0-cf1d78bb8821@chromium.org> To: Laurent Pinchart , Mauro Carvalho Chehab Cc: Ricardo Ribalda , linux-kernel@vger.kernel.org, linux-media@vger.kernel.org, Sergey Senozhatsky , "hn.chen" X-Mailer: b4 0.11.0-dev-696ae X-Developer-Signature: v=1; a=openpgp-sha256; l=5234; i=ribalda@chromium.org; h=from:subject:message-id; bh=5jcOY5wQypEMPaIWclRrjn20dOHGC/hJF48i1+E0AwQ=; b=owEBbQKS/ZANAwAKAdE30T7POsSIAcsmYgBkCvIKmv5rljVl3OMQWkOujH04PqNlN8jGCzwBSCZQ iAh73ACJAjMEAAEKAB0WIQREDzjr+/4oCDLSsx7RN9E+zzrEiAUCZAryCgAKCRDRN9E+zzrEiBTxD/ 9QAx238IgygZp7TSwJ2ULiXyuQrtrOWICfy1mFA5zA/pwm++GKNo22ihXa5glEcicoxkwbuOoqeUMg 0ot+/kWuOeIEG2f2BTOyJ9F7Bc21hcMW+ci5H2MLE9SI8uS7SHVz6jLWsF00hzDQA30UN2c6mErgLG YrJxhaM3FomaxBqJ/xn6tAgnhA0da0DiG82zXbNhGF8zUSamBNhLNFbTE/LR7P5alg2Sr5eilBpbgG UeAb7b5WRs67S4vlaVkZMxdEM2qt3TyLYVh2MOv7XMJGgTuzFbp+fP2YFHPt3akjO82ItDEeiaVdDS tAHcxJbw04e7FbJFdZrd0BH0GTR2scdjwPLdupgceRP7ZJqEQvI3uJteeLUti35eoKKxh96HNf3q/T I3eJzxvM8SvMFZC1Vx6/PZqYg9IM2PHLCRSXbYNeIpXTyVO2tuu7FfVJHWPzvSVmWisMC/mwSszc0J 6Y7It2BhVlQDRpWuzvyuwEfbe6nQ4Blt5vEbt+9vwjiY5AQYKPKBURKSSZl8fqMAedWLFL2bzc2eX7 lJHgEoodhYk9KHmax4xaie4RXPJHVW88uhLQOxgGvTxmQ77D/8Nd9YsFm8qohRfa7FQg19pemPZi+k 17n+D5Gu4a5H4Vq2Z8h4zV/kxzM2dYpfueT0ZgG2z5lpMQ6gQKpW92qdsUCA== X-Developer-Key: i=ribalda@chromium.org; a=openpgp; fpr=9EC3BB66E2FC129A6F90B39556A0D81F9F782DA9 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,URIBL_BLOCKED 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?1759971634588698779?= X-GMAIL-MSGID: =?utf-8?q?1759971634588698779?= Logitech C922 internal SOF does not increases at a stable rate of 1kHz. This causes that the device_sof and the host_sof run at different rates, breaking the clock domain conversion algorithm. Eg: 30 (6) [-] none 30 614400 B 21.245557 21.395214 34.133 fps ts mono/SoE 31 (7) [-] none 31 614400 B 21.275327 21.427246 33.591 fps ts mono/SoE 32 (0) [-] none 32 614400 B 21.304739 21.459256 34.000 fps ts mono/SoE 33 (1) [-] none 33 614400 B 21.334324 21.495274 33.801 fps ts mono/SoE * 34 (2) [-] none 34 614400 B 21.529237 21.527297 5.130 fps ts mono/SoE * 35 (3) [-] none 35 614400 B 21.649416 21.559306 8.321 fps ts mono/SoE 36 (4) [-] none 36 614400 B 21.678789 21.595320 34.045 fps ts mono/SoE ... 99 (3) [-] none 99 614400 B 23.542226 23.696352 33.541 fps ts mono/SoE 100 (4) [-] none 100 614400 B 23.571578 23.728404 34.069 fps ts mono/SoE 101 (5) [-] none 101 614400 B 23.601425 23.760420 33.504 fps ts mono/SoE * 102 (6) [-] none 102 614400 B 23.798324 23.796428 5.079 fps ts mono/SoE * 103 (7) [-] none 103 614400 B 23.916271 23.828450 8.478 fps ts mono/SoE 104 (0) [-] none 104 614400 B 23.945720 23.860479 33.957 fps ts mono/SoE Instead of disabling completely the hardware timestamping for such hardware we take the assumption that the packet handling jitter is under 2ms and use the host_sof as dev_sof. We can think of the UVC hardware clock as a system with a coarse clock (the SOF) and a fine clock (the PTS). The coarse clock can be replaced with a clock on the same frequency, if the jitter of such clock is smaller than its sampling rate. That way we can save some of the precision of the fine clock. To probe this point we have run three experiments on the Logitech C922. On that experiment we run the camera at 33fps and we analyse the difference in msec between a frame and its predecessor. If we display the histogram of that value, a thinner histogram will mean a better meassurement. The results for: - original hw timestamp: https://ibb.co/D1HJJ4x - pure software timestamp: https://ibb.co/QC9MgVK - modified hw timestamp: https://ibb.co/8s9dBdk This bug in the camera firmware has been confirmed by the vendor. lsusb -v Bus 001 Device 044: ID 046d:085c Logitech, Inc. C922 Pro Stream Webcam Device Descriptor: bLength 18 bDescriptorType 1 bcdUSB 2.00 bDeviceClass 239 Miscellaneous Device bDeviceSubClass 2 bDeviceProtocol 1 Interface Association bMaxPacketSize0 64 idVendor 0x046d Logitech, Inc. idProduct 0x085c C922 Pro Stream Webcam bcdDevice 0.16 iManufacturer 0 iProduct 2 C922 Pro Stream Webcam iSerial 1 80B912DF bNumConfigurations 1 Reviewed-by: Sergey Senozhatsky Signed-off-by: Ricardo Ribalda --- 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 1f416c494acc..4d566edb73e7 100644 --- a/drivers/media/usb/uvc/uvc_video.c +++ b/drivers/media/usb/uvc/uvc_video.c @@ -547,6 +547,14 @@ uvc_video_clock_decode(struct uvc_streaming *stream, 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 Fri Mar 10 09:01:31 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ricardo Ribalda X-Patchwork-Id: 67285 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:5915:0:0:0:0:0 with SMTP id v21csp763605wrd; Fri, 10 Mar 2023 01:08:24 -0800 (PST) X-Google-Smtp-Source: AK7set/mwmUeBbPG/h7m9FU4+JxZj2Bx2yDXebC9gNqnnw+w6EWamNDDxczhqo8aMhukgZGPHNd/ X-Received: by 2002:a17:90b:1b10:b0:237:40a5:7acf with SMTP id nu16-20020a17090b1b1000b0023740a57acfmr26218640pjb.33.1678439304145; Fri, 10 Mar 2023 01:08:24 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1678439304; cv=none; d=google.com; s=arc-20160816; b=gFhDdAVFGIdjRzR/e8WIgMb7SMX1P64sNV/w+MXBO4BAPUe+dk2ySNGhc55Nn2r+V8 9EQ3pbNsQekr5XWc+j21B5Iasal4xWKIdB7LDD9kFeje9DrlMMsMpbaMg918+BfvnTVl flWOuJX99WSE/9k3Z0Fnz6t/gamPl1PrWioUm61kn2LFkpxw8ZuHoW4ADGhrp1sNLbf0 CKOtccgpG+wdKdKTDrasASQ2m7avEnxu5BOHTb85nPwOrQoRhRNpMWDHELeRLwxcVeWo RDbb8xvNSO/5SOOejPMhYL81vYSIbS0HIr3YV8KQWz8cYlzEgAMBy3tpyeSxsRmD+PPw f+ZA== 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=RN9dk0TN64Mo7WHVhpVGbABaRx7Q/q6Pwknupu3adDk=; b=GIJ4NYsOVV8RZeylhHPPojbsvAFmmoT5ppiz3o3U3kxO00C1cXjFGrr7rM6/9URmFG JQcy5uYR5ouddgtBC+QdqD34rXf34zFwKM2av+W3KCsXpHsjiiDD3rjAzJkdn8y9E0IW YiXUMqc7LUA9r7aRV2l5Ug47zPPJ2uZ6Oy9CA50sTA0s7S1/EKACPF1ZIL5P2EsZK9Rw LFIDWzj8aT6eENWbgDFev+sIsiHO8PIwsWzKUmwaFwiSfU4wTJS1Rdx618SighrUBMzH 1s6TCbFiDvYSZp/IjctSrTfOLn1Ehp4lrJpW6uWNQc2/oQ9CR/8+VqSUN0L8O4Q2y6zS 7z1g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=auahc3+p; 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 v2-20020a17090a898200b00230ca3efcf3si1436228pjn.158.2023.03.10.01.08.10; Fri, 10 Mar 2023 01:08:24 -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=auahc3+p; 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 S231538AbjCJJHg (ORCPT + 99 others); Fri, 10 Mar 2023 04:07:36 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40048 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230098AbjCJJGk (ORCPT ); Fri, 10 Mar 2023 04:06:40 -0500 Received: from mail-ed1-x52b.google.com (mail-ed1-x52b.google.com [IPv6:2a00:1450:4864:20::52b]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 3AF0E7DF81 for ; Fri, 10 Mar 2023 01:02:35 -0800 (PST) Received: by mail-ed1-x52b.google.com with SMTP id ec29so17630540edb.6 for ; Fri, 10 Mar 2023 01:02:35 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1678438934; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:from:to:cc:subject:date:message-id :reply-to; bh=RN9dk0TN64Mo7WHVhpVGbABaRx7Q/q6Pwknupu3adDk=; b=auahc3+pJeuaf50PijN97Rc1bShULdg8glH6J/F2ecGmAk6HjZ2P6FY++By0j9Wzpy nzGKYmfnG32/bfAFFgwu0GVMYVf50UTIlIs6CrBFKzbRse2c1VFHSkaL9ewdJ0O6xAc+ ws4F4we3XAVtEiiqgXTiluJxDHoCiZQwoYrGc= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1678438934; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=RN9dk0TN64Mo7WHVhpVGbABaRx7Q/q6Pwknupu3adDk=; b=w+asR4fAr4gSvuZMVGlOR5D/2OWNxZxqKKeH3bfzU/XullmdQSNN/iPEwDlLNVai3s TJbztDiGIVWwSw12NP+tOkDNrHGGo1y5dH7Bz9FQB25iKll+SCIYJ5/cAF8YYfXd2NNq iF6g3KTt16Oi5dZplWHBQXWZk5F1z0jRPp8JjMt392llJBa+G5OcdwT/cRdKFqDXjcmV p68HTvJRgpQKkxMDXpSaZs7C3GElJRIQVH1663z9kUTmOFGiL6WVxmp1sETqQo3XA6aI 0G+IA6PHBH6VIbGniEmRgECaH6H83LJ3JuxiNPo292rTylM1Y+2oN45x7vbCFXO0NWZV AvRg== X-Gm-Message-State: AO0yUKXk1kRUQS9On1p/1CWWrdppd+nV+iDgsQeorjXXXVrO8cCvrh3n 1aX4Mb7Pi8qtZRgVoV1QO9V2MQ== X-Received: by 2002:a17:907:a808:b0:8aa:c35b:a34b with SMTP id vo8-20020a170907a80800b008aac35ba34bmr25771623ejc.20.1678438934536; Fri, 10 Mar 2023 01:02:14 -0800 (PST) Received: from alco.roam.corp.google.com ([2620:0:1059:10:ef69:7ab6:87ac:99f]) by smtp.gmail.com with ESMTPSA id s5-20020a170906454500b008b23b22b062sm692931ejq.114.2023.03.10.01.02.13 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 10 Mar 2023 01:02:14 -0800 (PST) From: Ricardo Ribalda Date: Fri, 10 Mar 2023 10:01:31 +0100 Subject: [PATCH v7 4/6] media: uvcvideo: Allow hw clock updates with buffers not full MIME-Version: 1.0 Message-Id: <20220920-resend-hwtimestamp-v7-4-cf1d78bb8821@chromium.org> References: <20220920-resend-hwtimestamp-v7-0-cf1d78bb8821@chromium.org> In-Reply-To: <20220920-resend-hwtimestamp-v7-0-cf1d78bb8821@chromium.org> To: Laurent Pinchart , Mauro Carvalho Chehab Cc: Ricardo Ribalda , linux-kernel@vger.kernel.org, linux-media@vger.kernel.org, Sergey Senozhatsky , "hn.chen" X-Mailer: b4 0.11.0-dev-696ae X-Developer-Signature: v=1; a=openpgp-sha256; l=1966; i=ribalda@chromium.org; h=from:subject:message-id; bh=Ka10ItjemOVlrqrWVbbdhzTBAJcpymfvLrHEyyU17oM=; b=owEBbQKS/ZANAwAKAdE30T7POsSIAcsmYgBkCvIMxQ6NquFZoQQOCgrRDaCRelepwRKDHcV6hDD7 +ALRcGmJAjMEAAEKAB0WIQREDzjr+/4oCDLSsx7RN9E+zzrEiAUCZAryDAAKCRDRN9E+zzrEiBQUEA CJkPTC56XQ5o/bYlLjZwg+MwUglsKtjo3UStrY7DZ82nwL3w0vF6A8FBKOODmvShxR17gikEdhhgi+ 4E5NUybANa4TrbHQ5T+dUmZFRvvk0kRLUkGcl6s4VDDNmD7a9FDmMhgOjkuK6VV6/f+XjhZVj2Dx3V x+8Gb5HcGdpUT3rYIzJPW8ASyVC6o9tUz+VQiJt8EyO0RAKNCeJVwxXsdvZ7NxyX5qEQNVZ/IO27bV FDxPm7w/5eAF5gd7CbJRZ/y7TCWRse6xU0bxM53OfJnXbBFOSLe8aYaXoyYFo828PTIYnSD+SH4NyV SSVVQ+dTW2k0918lsL8Fm6OvFCpeM1hQALewDDWE5jG70NWY2yrb0H+6FNhX7Zu5T2sVQztFCZKSRx j+c0QMLZ7EnSrHsDTKvLn8kP8kDDPTZw5s30PPoxfQ+qA29/83RUbueaAryTJF06kMPYz9DuFiqFBc yqdxdFYvj7yWgiogpjWZJzcYzYcsZxHkSrnQx8ZLahAQmbKb1vU0ArOeE2S+wxFs4sNT0a4a+pvGaP ItfdV7jSgdGo6mIGf3mFvL+FUXsTgrRPF768VzNpdXp3WDc4+7Sn2oVI0sfC+lgYFBZrf9hg9IylZ0 I2t8DzZMMnzLKQ6UWbazGtMJLPwE2xnE4txXjYaLfqrSqVI/NuDGzCS5heZg== X-Developer-Key: i=ribalda@chromium.org; a=openpgp; fpr=9EC3BB66E2FC129A6F90B39556A0D81F9F782DA9 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,URIBL_BLOCKED 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?1759971171724612492?= X-GMAIL-MSGID: =?utf-8?q?1759971171724612492?= 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 | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/drivers/media/usb/uvc/uvc_video.c b/drivers/media/usb/uvc/uvc_video.c index 4d566edb73e7..6d0243ea0e07 100644 --- a/drivers/media/usb/uvc/uvc_video.c +++ b/drivers/media/usb/uvc/uvc_video.c @@ -754,10 +754,10 @@ void uvc_video_clock_update(struct uvc_streaming *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) % clock->size]; last = &clock->samples[(clock->head - 1 + clock->size) % clock->size]; /* First step, PTS to SOF conversion. */ @@ -772,6 +772,17 @@ 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. + * Dev_sof runs at 1KHz, and we have a fixed point precision of + * 16 bits. + */ + if ((y2 - y1) < ( (1000 / 4) << 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 Mar 10 09:01:32 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ricardo Ribalda X-Patchwork-Id: 67292 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:5915:0:0:0:0:0 with SMTP id v21csp766817wrd; Fri, 10 Mar 2023 01:17:17 -0800 (PST) X-Google-Smtp-Source: AK7set83CIWhwBq87/ifvkFrAc3IH4r9IJVFke+U7yazLfzDn+BORjn6ApAzKYrjcjeDFjqnApDU X-Received: by 2002:a05:6a20:c126:b0:cd:74aa:df55 with SMTP id bh38-20020a056a20c12600b000cd74aadf55mr20321512pzb.25.1678439837271; Fri, 10 Mar 2023 01:17:17 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1678439837; cv=none; d=google.com; s=arc-20160816; b=e8bEeMoI2E9qlgp/R5iH/EPwK5V80fpmoThKQMcZrGWZfdjG8BTWBs79MULqsGGFRQ fXoG3JCOfOQ655CE/PGjM0ERF/auOUoJ5lUaNMVdUUKHzLuoYQDiZjz7ijNhxaehF5gN sPBD+0/RVtT36zAw4VQv4eKWQw8zRMNDSTqq2vZ9apAMla1XCmVBvsuNmkfbBG0Zi0sq vSMSe7lVEpMA0LHGL21/jLBv97G9/2Okg2MXYTs6KPg7SRpeIzf8rF/bRnHKz0d/5t9S Gjq4mj8wSTQloZ0HfOWaZELjKiyLIv6buisSJNkq86D9HtFQtpe4xEoYWyEiLoUSJ2tx Iz1g== 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=1nm96Wrt6gyEIi1trIE8ggK/zsqJmxHOVJaDLG8Q4K4=; b=nc36lJP2W2F+av9OOk1hC5zQFmoZU65ueiXIfqzUSXKX+RaTz16mCi1aHpannQWA/0 VDD/VyPAeX3A+Yq0cy2xIBGbUDF8zeFgFh1cE6RcpKa4pA/jwGR9k0gAU02cO5dgi1NP lxVuSSA26JN8YP/BHO9LCzxhU+sJgX1DbDscb38QSUWZYm4devBlejT/oipOGPg+Dteb 5IQz4FKvE/BspZc4k4yBQ+NzeIfXD3OK6XfbRK6xlNlry2rz7QovBAquun9eP/OFLeM5 hkuHlas3/3oL9HkgegeOneMdskqZ5K5/+OPhY/2U03KAagx6Snkx7Aa+hhPGoJtkcH4Z W9pQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=dfDNrC0Z; 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 j188-20020a6255c5000000b005a910206938si1487036pfb.29.2023.03.10.01.17.04; Fri, 10 Mar 2023 01:17:17 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=dfDNrC0Z; 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 S231546AbjCJJHl (ORCPT + 99 others); Fri, 10 Mar 2023 04:07:41 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40206 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229683AbjCJJGn (ORCPT ); Fri, 10 Mar 2023 04:06:43 -0500 Received: from mail-ed1-x530.google.com (mail-ed1-x530.google.com [IPv6:2a00:1450:4864:20::530]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 4F1A911258F for ; Fri, 10 Mar 2023 01:02:40 -0800 (PST) Received: by mail-ed1-x530.google.com with SMTP id i34so17611708eda.7 for ; Fri, 10 Mar 2023 01:02:40 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1678438935; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:from:to:cc:subject:date:message-id :reply-to; bh=1nm96Wrt6gyEIi1trIE8ggK/zsqJmxHOVJaDLG8Q4K4=; b=dfDNrC0ZLZtziQjxrQ827AzIfRrdg85nRDa5OZ3OAtO+bStH7Izi+nC02g8UG+RUd+ OYTDfM0WOemGp3oVfnjmrLoF/rXcjz6wT7qOZY9AkUr2B6ze7MYeA/xSBXUdN+JC2d9w bNHExn7H8B8MzdFqEipTSicJQMh1tgprtlhSA= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1678438935; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=1nm96Wrt6gyEIi1trIE8ggK/zsqJmxHOVJaDLG8Q4K4=; b=BhSiapVcfTH1oGZAGZqXfk6G3l6QAsIYRZYngyN0fJqZ2yXZD2uXnA/A4WvsX945IU tH5Sd3iZsK3GOBDG7xbAut0rpY4Y+Ou5/Kese+pOOhRVEeuqT2N8sLz0Rhe82B5JIgMc V+CmgVtZx33ioE/9Mzx4l/H7O6CJaYuLECS+obIA79b8PF/imqGjuNaZ1as/VGlqqiiY EJZlQ3BC8GjlA78eBqvu2YD88tg4r/pskVMMpI6ips8sd5lEJl8+MyXLqu3dPw88iKjO m8TC4cOjmd4BxmxRSgNqQRhfVCmtxt8P2bwHpGtczB76LLtKtZq8UzK8VJd7OhMMSa81 B20Q== X-Gm-Message-State: AO0yUKUj950kgnssd8p3lW0DdWDVSC0baDCZEtpXVFzQcauYcBnlVpvv 5r8QMpmglJgO2pA2/qppyqDfDA== X-Received: by 2002:a17:906:66cd:b0:8e8:602f:847a with SMTP id k13-20020a17090666cd00b008e8602f847amr23072180ejp.24.1678438935445; Fri, 10 Mar 2023 01:02:15 -0800 (PST) Received: from alco.roam.corp.google.com ([2620:0:1059:10:ef69:7ab6:87ac:99f]) by smtp.gmail.com with ESMTPSA id s5-20020a170906454500b008b23b22b062sm692931ejq.114.2023.03.10.01.02.14 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 10 Mar 2023 01:02:15 -0800 (PST) From: Ricardo Ribalda Date: Fri, 10 Mar 2023 10:01:32 +0100 Subject: [PATCH v7 5/6] media: uvcvideo: Refactor clock circular buffer MIME-Version: 1.0 Message-Id: <20220920-resend-hwtimestamp-v7-5-cf1d78bb8821@chromium.org> References: <20220920-resend-hwtimestamp-v7-0-cf1d78bb8821@chromium.org> In-Reply-To: <20220920-resend-hwtimestamp-v7-0-cf1d78bb8821@chromium.org> To: Laurent Pinchart , Mauro Carvalho Chehab Cc: Ricardo Ribalda , linux-kernel@vger.kernel.org, linux-media@vger.kernel.org, Sergey Senozhatsky , "hn.chen" X-Mailer: b4 0.11.0-dev-696ae X-Developer-Signature: v=1; a=openpgp-sha256; l=6685; i=ribalda@chromium.org; h=from:subject:message-id; bh=K1XWiZZVgbVikxQUf2acjH8pNqXvxOd5rL4C6h3qVQI=; b=owEBbQKS/ZANAwAKAdE30T7POsSIAcsmYgBkCvINr+ZwSCAqRUItGprR4yFuX8Jm0ErN3ZjNeUt+ YTHQm4SJAjMEAAEKAB0WIQREDzjr+/4oCDLSsx7RN9E+zzrEiAUCZAryDQAKCRDRN9E+zzrEiGh+D/ 4n3tTAxb4vRF+tYvF0MSi562VBo4IhUak2Jrl591uTCzcc2xTtxsLF7VRE3WPdLIfvD1SiJ7l5oMX8 vmN3UwtjS9smDKz310p1dROTT3s2xmTGxDq21q06O/6h4YsV9oRsLX+9+ikoDND4XfBAYEco9VFxhc kfO6fjOAbUA2Jdp8EZL+Uw38RuommvxaWmG99Ejeh0q4PqLsiT0N1/5WDrjfXuPDuSmfsTwT+kyWSM CVakUrqH4MWt8smN24MzoVuHu9eZvslCmCsdvwICDf1aEDgMxxIT4MtYlo/1dAskZf67qBX/TOZtnM zLjtg3/dQkWvom2DFqJIZDfEM2dY44Z4M60oQ7kGwW+65wg0vpXpDtxToVfEIfF90cQF3gFKF2gqK7 xhN0tpuADROLSG1+Mx7L1ZIC1SEb/33ux8ZJLwEAlikI2d33Cnt5NQPBo0mPnBDF1ipvf1elupsOaN GngxtY1+UscwnBxqoMNXH+N0rmxnLaH/D1VMCTb+70uJP41HP3FmiLleENwG4GKXKHS9f76yK68Kal WrMdx3pMGXyjytwuO3cziB4I3ZxEG5+LWXktCvlx+TZt2HQdpo8KDmPE/wmr5D96i8277us3+yfTTh /8nWkhFB/TObJccEmiFoACpCCRAMnqQcz45869ijeO09QKQXmQ6KOL3TJR0g== X-Developer-Key: i=ribalda@chromium.org; a=openpgp; fpr=9EC3BB66E2FC129A6F90B39556A0D81F9F782DA9 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,URIBL_BLOCKED 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?1759971731236343740?= X-GMAIL-MSGID: =?utf-8?q?1759971731236343740?= 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 6d0243ea0e07..8156efcf48ac 100644 --- a/drivers/media/usb/uvc/uvc_video.c +++ b/drivers/media/usb/uvc/uvc_video.c @@ -466,19 +466,28 @@ static inline ktime_t uvc_video_get_time(void) return ktime_get_real(); } +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; } /* @@ -2108,7 +2100,7 @@ int uvc_video_resume(struct uvc_streaming *stream, int reset) stream->frozen = 0; - uvc_video_clock_reset(stream); + uvc_video_clock_reset(&stream->clock); if (!uvc_queue_streaming(&stream->queue)) return 0; @@ -2257,7 +2249,7 @@ int uvc_video_start_streaming(struct uvc_streaming *stream) { int ret; - ret = uvc_video_clock_init(stream); + ret = uvc_video_clock_init(&stream->clock); if (ret < 0) return ret; @@ -2275,7 +2267,7 @@ int uvc_video_start_streaming(struct uvc_streaming *stream) error_video: usb_set_interface(stream->dev->udev, stream->intfnum, 0); error_commit: - uvc_video_clock_cleanup(stream); + uvc_video_clock_cleanup(&stream->clock); return ret; } @@ -2303,5 +2295,5 @@ void uvc_video_stop_streaming(struct uvc_streaming *stream) usb_clear_halt(stream->dev->udev, pipe); } - uvc_video_clock_cleanup(stream); + uvc_video_clock_cleanup(&stream->clock); } From patchwork Fri Mar 10 09:01:33 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ricardo Ribalda X-Patchwork-Id: 67300 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:5915:0:0:0:0:0 with SMTP id v21csp770216wrd; Fri, 10 Mar 2023 01:27:32 -0800 (PST) X-Google-Smtp-Source: AK7set/mspuz56mogPbtXtC9XyhD66jj2RIUcMRjsQT6GmS983jxub5W3br4j2a0FXyMvFXrfiw4 X-Received: by 2002:a17:903:18a:b0:19c:df6e:6aec with SMTP id z10-20020a170903018a00b0019cdf6e6aecmr1496695plg.7.1678440452178; Fri, 10 Mar 2023 01:27:32 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1678440452; cv=none; d=google.com; s=arc-20160816; b=c7ePvmWSvtgkZtwpyhJVbBN9G4656YDPMWpLtSYb6nBIhK/IIdpv14H3VVpsAEz5rB oT3in7/Y/f3tN+9M1i6F8XIlWvORjO6W08ZXlEy8EABAzQVXQh4HLrjgUsSy7zy+IvxK XlUZNa4s/MS7FOScjUnts+akOF52C/ueFQr6b3myENJ07/p9l3K7xw5LT8TjHE8UKbBt Es/PEwgIPZuYtDKP5+TfCQxlQXMkBxQMhdAHYXGaTVMeeoOjtuXL8v7ZCem7Dap2XpQ2 bZgXeOSjSvqWge3KRUWTsmmx1yvVUEPYLEFRW2jeyOHQEoHF9JVjPdAugeRSLrBUeE8Q jXGQ== 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=Xnm0YfgvCV+Ipm6xs9Mtjl0Q+VTBw28sf5U+arx5bb8=; b=Cg0pGQsQf4Ox3oFUrmjAJQs6D3SGN9r3cN1VlieFwljgGVQlAOP3RrsDwFtBHx+vk+ TFdnMcZ7QgSXpaKIMNwo4aJ29M6RpCjEEu2jkQZ5m9Usm7jgwmZoUUQRih+dk3lmfZI5 axfp1umJnkxuVhhmE0RteUm9tDOaBA2CTlNKx2aK0RNEqC5JpuyyBt2csdkj0UHJVjF3 HjohCs64BIqEAtItZetInuNPwYdPHPb2F5tSrq5cNU/PJHJNSKLvX5y7DhOM7LfJhQCv FZXpjZi7zIcT6HyGWbbySPonUlsBF5cd81oyiXtS4lTUx+6lvp/tHZyKoBD2A5qSJa20 e2eA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=MZri77WS; 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 w11-20020a63490b000000b0050327dc5ff4si1584427pga.67.2023.03.10.01.27.19; Fri, 10 Mar 2023 01:27:32 -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=MZri77WS; 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 S229845AbjCJJHp (ORCPT + 99 others); Fri, 10 Mar 2023 04:07:45 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40220 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230379AbjCJJGn (ORCPT ); Fri, 10 Mar 2023 04:06:43 -0500 Received: from mail-ed1-x536.google.com (mail-ed1-x536.google.com [IPv6:2a00:1450:4864:20::536]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 9880F112593 for ; Fri, 10 Mar 2023 01:02:40 -0800 (PST) Received: by mail-ed1-x536.google.com with SMTP id x3so17575252edb.10 for ; Fri, 10 Mar 2023 01:02:40 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1678438936; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:from:to:cc:subject:date:message-id :reply-to; bh=Xnm0YfgvCV+Ipm6xs9Mtjl0Q+VTBw28sf5U+arx5bb8=; b=MZri77WSkYEyxC5HlP1IFuaW3AYi3chr9mQhghCiMLqfSj8Op8753JyQ+sP6jCVbBX X6WYRTE7y5kJ5xuHCV/CMqVloSssG0qaTpCBjWmwXRK98gc6FwdVjWIF0SHnydheijRA IPXW2HPRgLVpdx9k8ktWDKm3hDi2bmGFYaf/k= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1678438936; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=Xnm0YfgvCV+Ipm6xs9Mtjl0Q+VTBw28sf5U+arx5bb8=; b=b5ewiCi82BC8bgOzi8F7jnboA7PmtM2q2U9PHNp2n3hTaORZORpKmtHF9HmUHUQLvb sDOZH4RRWjpEji3PiszD2fo47cxOm0lbdvFdpFzkLrWTXXVEywJ2fXue5UH3eWBpyd+C n0uWQBGp2+2gE2aJCrbCTLKN1p0JLyBjZZZExg9xkLvA3Ya8tUm/3X14ziURgcemg1bv TJ5FumVLmHs98vLiX3HUeZt/7rOVgqWuLUaJVuSXkDwBzDmcUZDpKZuLyuxAzOBZd7D+ YsKPLKYrqe6m7U7Mz+WbkP5LASKdb/dBjTP+0vznxkgGyhJH2sEiG7Eiv/ZgqcteYd9R wvjA== X-Gm-Message-State: AO0yUKV1o9AOQDATJCrUUCgDjxAl9Xn3lhYBkhfqareV0zLoN7psKep1 JjrH8+RdgAjpz5ystXOJ/OGP/A== X-Received: by 2002:a17:907:c0c:b0:8af:2cf7:dd2b with SMTP id ga12-20020a1709070c0c00b008af2cf7dd2bmr31570396ejc.13.1678438936082; Fri, 10 Mar 2023 01:02:16 -0800 (PST) Received: from alco.roam.corp.google.com ([2620:0:1059:10:ef69:7ab6:87ac:99f]) by smtp.gmail.com with ESMTPSA id s5-20020a170906454500b008b23b22b062sm692931ejq.114.2023.03.10.01.02.15 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 10 Mar 2023 01:02:15 -0800 (PST) From: Ricardo Ribalda Date: Fri, 10 Mar 2023 10:01:33 +0100 Subject: [PATCH v7 6/6] media: uvcvideo: Fix hw timestamp handling for slow FPS MIME-Version: 1.0 Message-Id: <20220920-resend-hwtimestamp-v7-6-cf1d78bb8821@chromium.org> References: <20220920-resend-hwtimestamp-v7-0-cf1d78bb8821@chromium.org> In-Reply-To: <20220920-resend-hwtimestamp-v7-0-cf1d78bb8821@chromium.org> To: Laurent Pinchart , Mauro Carvalho Chehab Cc: Ricardo Ribalda , linux-kernel@vger.kernel.org, linux-media@vger.kernel.org, Sergey Senozhatsky , "hn.chen" X-Mailer: b4 0.11.0-dev-696ae X-Developer-Signature: v=1; a=openpgp-sha256; l=2453; i=ribalda@chromium.org; h=from:subject:message-id; bh=QSOtDi8Lpp1WwCcscneU3GgnDWNqfm9La5ACGKDE/1s=; b=owEBbQKS/ZANAwAKAdE30T7POsSIAcsmYgBkCvIO/4kYmLvH1iiWCJT+hFKLcOK8Wi79IHBtmT/M xtdiMKmJAjMEAAEKAB0WIQREDzjr+/4oCDLSsx7RN9E+zzrEiAUCZAryDgAKCRDRN9E+zzrEiLBTD/ 0Ryz/FeiC6LVOp07enO0v9qf4Yw+/Lu6WjBWFsRm1oOxcANF9lL/mkE2Qd6SqcXJf1QXdBfy7udIUe iwVaULNUu/VVWWskPMDTqswdyiqbHHf4+B5BHzWTteEvlmipjYbHcmsRxh7i8IJBBMDiE7vIJ9WXd4 uoD4xc05qskdOKx0lYis65D7w3y6LY5jL+GmXcJdYtu9dYYXYcakIfS9XoMGnMyip4yiBTgsRvXTGM NH49PIao1aOkOoX8vDCo6R650JNDAq0B4Pgz0hiwgg8r/CpVp3vGAKD1nItbVV9XFcrUPXwK5ecxjv eQpK2zsvMm+11YYPEWQFsO/u08HcQhw9t6fg7/zGUUrfzfilL+ajc7P48vHpY5LNBSBh9B6am/JOCc e390J6WA5FMP07H5LlchX1ssgjSAh4u7bIGcXprxx2OzOwGgad8y3feYSZ5GSGhmRE/3cU2+DFky1F 499RTkiyJQ/C5QEDoxJtslOqo9yxOa7hMpBEWJ2+cDqdW/E31+AxPizo0RqLYZWgmrVpk3PIG12AOd 4jg90tUWWXxTde+4qr08qyWBE8L8doLVWrZOgc9AAqwPptrURcfjm1ckTSPDYbj/UOTTxGpnehQFYR bnbq0uhBZnz/Gkqxge8yvbDbZpfmISWGF1eslE0oy8a3E1TvPH4ipZYIV8CQ== X-Developer-Key: i=ribalda@chromium.org; a=openpgp; fpr=9EC3BB66E2FC129A6F90B39556A0D81F9F782DA9 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,URIBL_BLOCKED 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?1759972375832472069?= X-GMAIL-MSGID: =?utf-8?q?1759972375832472069?= 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 | 16 ++++++++++++++++ drivers/media/usb/uvc/uvcvideo.h | 1 + 2 files changed, 17 insertions(+) diff --git a/drivers/media/usb/uvc/uvc_video.c b/drivers/media/usb/uvc/uvc_video.c index 8156efcf48ac..eb1165366fa0 100644 --- a/drivers/media/usb/uvc/uvc_video.c +++ b/drivers/media/usb/uvc/uvc_video.c @@ -473,6 +473,21 @@ static void uvc_video_clock_add_sample(struct uvc_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->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 +620,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;