From patchwork Wed Mar 15 13:30:12 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ricardo Ribalda X-Patchwork-Id: 70227 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:5915:0:0:0:0:0 with SMTP id v21csp2338766wrd; Wed, 15 Mar 2023 06:46:42 -0700 (PDT) X-Google-Smtp-Source: AK7set8GGaCW3u9Y6s1AK8PJJL7ExGm9mM+BKvbJGUVcvcDvlmNHfiIzCUw/ZL6/ma01toVij3lR X-Received: by 2002:a05:6a20:160c:b0:cd:1cce:d892 with SMTP id l12-20020a056a20160c00b000cd1cced892mr46140770pzj.9.1678888002485; Wed, 15 Mar 2023 06:46:42 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1678888002; cv=none; d=google.com; s=arc-20160816; b=RTOW1KSDBvmj7VgMchLuu0OXvvR2/Q4m63ZEKOskqa6dXqpVMgzF96DQkDpiQ6CIMH xlhYaAVvNBsPRkTSnl3MMJJfUzgMgHLfWtya2Ju7Q/JNpMf7gMLTIUDc+m8tQePo4oQq bEMmTpuvPTS7E0ppY2TGSOpUjrnDmPLjYpuYK/nQkxQxmv4JYEGhzltMmuXPOknQEgjY lWTIbA+/yE4poDbI8wKOzzRJ13YrGwTrFOybr4Q3wyoFL3B3yQ7GB2YTnpaHPbpDcdRn hhGbLoBy+nH5RqlnTj66iUB3UtFpRSmJSsUmBRiYONuy1OUgcUI8tEMR6eG54BgK33BF h3fA== 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=jmbQJ7O/6JS/WmDJaNmVYSgRY/QPgP7mF5MHe8za06A=; b=sqPyGdLiQ6KoqHHSPbb8nBzj3vZCn3u2Slv/Y1a3chuUhEzHqwRzf7tvKwJbGtBxYW CIUGJxxuMhwieswJeRn4/NN9xJIgr0oZKgYo1gJVYW5CWIqinLAEhiv/L1foeA9AbNUH S3YAW5RFLQvIWJ1fnoiJZ4ovkzZye4iel4oFEJvsqRWfTbKueAwBRTd6w7qcPjfGQHhO t+QgWLxpapnHKF9Vw+J/WMAfctPKInt933TZ0HdGtepGdyRCcUTda8kGHSFA9uamEXI6 iwjAk92p6Y+z93jC8ZhMBX8EM75Nl3IJO9guHVlH9xW1yI7OyBcrYhSb0/8LmYxbO89C OSBg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=VX9FTX1C; 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 p25-20020a634f59000000b004fc2dfedcd6si5042959pgl.213.2023.03.15.06.46.29; Wed, 15 Mar 2023 06:46:42 -0700 (PDT) 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=VX9FTX1C; 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 S232069AbjCONai (ORCPT + 99 others); Wed, 15 Mar 2023 09:30:38 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34588 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231896AbjCONad (ORCPT ); Wed, 15 Mar 2023 09:30:33 -0400 Received: from mail-ed1-x52a.google.com (mail-ed1-x52a.google.com [IPv6:2a00:1450:4864:20::52a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id ADDA21A955 for ; Wed, 15 Mar 2023 06:30:27 -0700 (PDT) Received: by mail-ed1-x52a.google.com with SMTP id h8so31780150ede.8 for ; Wed, 15 Mar 2023 06:30:27 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1678887026; 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=jmbQJ7O/6JS/WmDJaNmVYSgRY/QPgP7mF5MHe8za06A=; b=VX9FTX1C5SOdJP5ixptJW06u0wqD4ynrX+CaKddF8/mWnQYl9lhvpKvjXAZL2i02jB 3yoo+Lu3eGxvl2gVOngB0stFX8IVaTCLFlnRnWz5WEmBsT1smTXm7k4u2AXeuuNDs/sn 08sUuCpwbeV87KdndzWMAXn3OXRfC0IL9KuVE= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1678887026; 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=jmbQJ7O/6JS/WmDJaNmVYSgRY/QPgP7mF5MHe8za06A=; b=KLXxx7KA1369WTKEXtpq/qH1kPxR2kSCzSnjONiITOWHK0bOrnkXrv5dSDgtVPyJ3d kvo3zay4mPXJqHngw96hc3Y2iM3BCzoZp+bxFppqlykWCYDpfP8EFl9qeqdVgPAGASdU KPoGF7g4CbcLxADyOax5fcjjFCL4Ne88xb9ni1P8SU82LSF2HZPgothacdnd3NzvzW3/ Rc7eWzM+L5+qyAfWJl+D7sIYSklQZgCIPX0W9XBDm/COf6gx5O4ZUwVAXsD3kVR04pf5 rqJWJ61vgcYOAjfEEnc6BgCxoLIqe0N18dGGBP9bcemUgBuvdOwNvIag0frhOOnsd8BM n7ow== X-Gm-Message-State: AO0yUKWseJtrCR5U2on6PKOCj46mkRTVzP2+JLGBUAB8PjZhWM+bozSm C6RqOMf76G2C6JQE4gmBBqFEKA== X-Received: by 2002:a17:906:f85:b0:909:3c55:a1b3 with SMTP id q5-20020a1709060f8500b009093c55a1b3mr6410804ejj.38.1678887026258; Wed, 15 Mar 2023 06:30:26 -0700 (PDT) Received: from alco.roam.corp.google.com ([2620:0:1059:10:491c:f627:27f0:1c4]) by smtp.gmail.com with ESMTPSA id ha11-20020a170906a88b00b0092043ae1a8asm2534638ejb.92.2023.03.15.06.30.25 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 15 Mar 2023 06:30:26 -0700 (PDT) From: Ricardo Ribalda Date: Wed, 15 Mar 2023 14:30:12 +0100 Subject: [PATCH v9 1/6] media: uvcvideo: Fix negative modulus calculation MIME-Version: 1.0 Message-Id: <20220920-resend-hwtimestamp-v9-1-55a89f46f6be@chromium.org> References: <20220920-resend-hwtimestamp-v9-0-55a89f46f6be@chromium.org> In-Reply-To: <20220920-resend-hwtimestamp-v9-0-55a89f46f6be@chromium.org> To: Laurent Pinchart , Mauro Carvalho Chehab Cc: Sergey Senozhatsky , Ricardo Ribalda , linux-kernel@vger.kernel.org, "hn.chen" , linux-media@vger.kernel.org X-Mailer: b4 0.11.0-dev-696ae X-Developer-Signature: v=1; a=openpgp-sha256; l=1078; i=ribalda@chromium.org; h=from:subject:message-id; bh=A9fEA5iTFV9nHf0HSMuMBzuGKXvge+iAmSWSfQdkDBc=; b=owEBbQKS/ZANAwAKAdE30T7POsSIAcsmYgBkEchnkB533tmnBAKDf+UejFGP1U+2lotRHOUIf/Nt jV+F5KaJAjMEAAEKAB0WIQREDzjr+/4oCDLSsx7RN9E+zzrEiAUCZBHIZwAKCRDRN9E+zzrEiDbSD/ 9p34Y29eU+uEnTxl5viO6CIXN1ajPgdEhMo5S6EMhNZb38al/aayV7sOh/ynB7lSWgz8vA3zHQGV1v myTb1fPsHRAWtQpXkznTmnTL3BXJlXEvVIYIQ4IfEpPYLx5sDSKXCrjjIDKTYdb6KfXVzPE72a8mYH WibUfX+RlHaAUPoLwpwsR24BE3xPtuus25vkcT5i6HzAQfuce93rNJFeW8kzxeSzHCQP2gsvKFQP31 ABqqx8708mk2F1lxvKOImrof6ceHXrDfCd+aZSOVafHiAS1eb2PoeF7tCE2NduLb9Q8kd2qtPxO638 dOETkqE6LrXK/SeWPCFx98l4Vyt5pWnyXEh59H+PUEAvOvmy4Jj5P7oIQFH8tpzF8g5j9rkWK3E/He bTOc/WC8mOuYDExo+vATpUWRPWgDEaD+S1PHDx+VHek0EXk/IwV/QO8AajosPw4LiCggsxxwolqk/8 sEoUW0nJg0lEIbphVf4DaWGMPJJsjPkefBB9H+FWrkLN1B+4xsyABcKjyvZU8jG6gcvJWAhDRx6Oto 1cCkBeGw6cRfPEaPrYI6lXJqvqOgJI+2R2rnmmYZhfH5AqeR/qVbQBy4BnYZcIJJ14q+bBna6df4pL DYT+WNMqJNtwcH39JtjaZTsLHtBilTlpchSc+tb5hEu+5N1xxdySAtZ01wDw== 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?1760441666071498074?= X-GMAIL-MSGID: =?utf-8?q?1760441666071498074?= If head is 0, last will be addressing the index 0 instead of clock->size -1. Luckily clock->head is unsiged, otherwise it would be addressing 0xffffffff. Nontheless, this is not the intented behaviour and should be fixed. Fixes: 66847ef013cc ("[media] uvcvideo: Add UVC timestamps support") Signed-off-by: Ricardo Ribalda Reviewed-by: Sergey Senozhatsky --- 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 Wed Mar 15 13:30:13 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ricardo Ribalda X-Patchwork-Id: 70221 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:5915:0:0:0:0:0 with SMTP id v21csp2333143wrd; Wed, 15 Mar 2023 06:35:12 -0700 (PDT) X-Google-Smtp-Source: AK7set9BC5CAmVoWrsczSNnp78I026EUyvS4kMUGwMMoxXILjI3cjeb+wf7/A/uqQ92LQAB3H601 X-Received: by 2002:a17:90a:f3ca:b0:23d:3a3f:950b with SMTP id ha10-20020a17090af3ca00b0023d3a3f950bmr5462595pjb.22.1678887312148; Wed, 15 Mar 2023 06:35:12 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1678887312; cv=none; d=google.com; s=arc-20160816; b=H3cTme1EMhb482jKMhiipBfqGgn2sUSIPoPUgMzIUdNYqWZBw/WxBbdAz9K7SjmnBo yhwSGFbEvKY0zLfn6fseVEvxPitxhZKhCMtu2P/CetlGiZyQwWhireoEwF/oBYhsH/jv hB79ypRtmD/RQQQLz5734sjrOWgERms1qHAsiYts5C1EFBBtmuJ3EqZvEFW4a29P2HBX Zu2V2fqtarXBZilj+MHlUH6mNH7nHubyRVkYYO5t4MujOpVzHX7du8cM+XDNemNToH6D XgCueeK3Nq5iybwF7qUvQNlBV4hVqihyUZQtF0HV05IEYQjv8/LqzfFExqUiGO2/o6tu 5+eg== 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=HtBr0AOu7/2X+Uw/oXOaJaHRHLApYyjgptSUWfDGODqreJOrslLd49wHOyaI+uviXt IJHR22c/bVfKr0XG1L7i3U6c3+JGQjRBDWqbKbjwH0nKlzUxGIDo4HIbGGEa7pdm6wSU pEpqTWedi2pTJFhIHV05OiNv8w3yEutBM1NbfnrOq+tbd2I/JILDnrv9OA3+ihQSPK4K LT6XBmzkNtc3pVQFjwNDEGKerAkkrs6SXei2qJCxciT2/0mApu/ODN+p6gFTue3KYfbj F7LsU+ENZBVyHWtotX1FEOiVWMerXeGnoxDuVyMpaK2wFJtDcv+jYhTJn7YlKmnvh/Zh 2mKA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=gHfCVCL+; 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 m10-20020a638c0a000000b0050be7950cf9si263554pgd.67.2023.03.15.06.34.51; Wed, 15 Mar 2023 06:35:12 -0700 (PDT) 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=gHfCVCL+; 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 S232224AbjCONam (ORCPT + 99 others); Wed, 15 Mar 2023 09:30:42 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34600 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232009AbjCONae (ORCPT ); Wed, 15 Mar 2023 09:30:34 -0400 Received: from mail-ed1-x52d.google.com (mail-ed1-x52d.google.com [IPv6:2a00:1450:4864:20::52d]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 797BB91B47 for ; Wed, 15 Mar 2023 06:30:28 -0700 (PDT) Received: by mail-ed1-x52d.google.com with SMTP id h8so31780277ede.8 for ; Wed, 15 Mar 2023 06:30:28 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1678887027; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:from:to:cc:subject:date:message-id :reply-to; bh=Ks+I0iL9S/eJzdcQJQgtcbFNoYKHK8Hr97Z4+vCos3E=; b=gHfCVCL+KtwV3EGazhIeiRvmQifinDdtF04C+2pUjQdV1/8/ES84XLYEjUdkzB4QsU TSStCHlPV1aC8PvSJQYXYl/EScKnV2JYi3AISC6OUA392cGv0mylaw+FIcpg1OunvKbT RAC2B//QzWfaTxB8+Dc/EtX9u3Bg0yHMmvz/Y= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1678887027; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=Ks+I0iL9S/eJzdcQJQgtcbFNoYKHK8Hr97Z4+vCos3E=; b=S0E0+U9I6TxiaXz2emqHyjCgoptbf6AV7cCm5QoOjCHDVVkkjzXKxJd7TYIfyR6MdZ wAwkSd1bP6lGWHYWJBvfmEEGBZ3duO6Azp3cf1pzjy1sjMBkgFJIglpSeQmx7DvUVU2F 4aoyoDDNyaPK2c8OfYDdv9O1JjBDMzJuXuNqceQ8hDQ/6EZzl0EmTEnHJqtpdRTE6DiS 3XhugM3uckI5v1ivbrU3adquPB4m5UzKhGPtaAVhTCY+9WXg94VuKtTG+sJFkoyzBZRH 1MQRbaHNfHgCVWkRCfqmd2+LLIpwNHVPusr21lWIrguzgwCkgkEeQX1rmqpQumRCII1l lHzw== X-Gm-Message-State: AO0yUKU9fmz2oQrk2lXMTf4wEFxm/dBR6uiWlbqljXuLohYVQsYSvCXP sjv1CW6Y0wl4aN2ZwwKv1PoWyA== X-Received: by 2002:a17:906:c1d0:b0:879:ab3:93cd with SMTP id bw16-20020a170906c1d000b008790ab393cdmr7663387ejb.46.1678887026905; Wed, 15 Mar 2023 06:30:26 -0700 (PDT) Received: from alco.roam.corp.google.com ([2620:0:1059:10:491c:f627:27f0:1c4]) by smtp.gmail.com with ESMTPSA id ha11-20020a170906a88b00b0092043ae1a8asm2534638ejb.92.2023.03.15.06.30.26 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 15 Mar 2023 06:30:26 -0700 (PDT) From: Ricardo Ribalda Date: Wed, 15 Mar 2023 14:30:13 +0100 Subject: [PATCH v9 2/6] media: uvcvideo: Ignore empty TS packets MIME-Version: 1.0 Message-Id: <20220920-resend-hwtimestamp-v9-2-55a89f46f6be@chromium.org> References: <20220920-resend-hwtimestamp-v9-0-55a89f46f6be@chromium.org> In-Reply-To: <20220920-resend-hwtimestamp-v9-0-55a89f46f6be@chromium.org> To: Laurent Pinchart , Mauro Carvalho Chehab Cc: Sergey Senozhatsky , Ricardo Ribalda , linux-kernel@vger.kernel.org, "hn.chen" , linux-media@vger.kernel.org X-Mailer: b4 0.11.0-dev-696ae X-Developer-Signature: v=1; a=openpgp-sha256; l=3922; i=ribalda@chromium.org; h=from:subject:message-id; bh=+e1rJhTRZYFtyKcR4/N8/nQKunH8/Dqv1jSdJGbbaL4=; b=owEBbQKS/ZANAwAKAdE30T7POsSIAcsmYgBkEchpR2vAg7sBI1dBwEaleUoYD6Drm9+mb10eFF4d hd04yCeJAjMEAAEKAB0WIQREDzjr+/4oCDLSsx7RN9E+zzrEiAUCZBHIaQAKCRDRN9E+zzrEiKCRD/ 94LdXzVScvrVV29sQ8EK2AVywlk82cd6hn4nTtlVg4uXUKQYiHC5na7f44u2FI9CSFXKwiwghgAZwd 5/dDjYFYPND+npCycYyq+jz4WVsrxsOoB2VyqkuOh6ydpq5d/wbKaI/FIXkx4LRexFe2/BcT7kdtN8 QUFA8K6zLGDPpcFUspOX4Rn3kVY9V7IkklSZPOSy2z0UzR43xsNiVtAA61/D7Bw/H31K8x5PmrumE2 z7eTr5YpcGunNBaTTFUm3jgDFSSxY4l8v6kIK7NqeJqxWtCOGqYdQLw4yt9lVlTBclitt1YQCWybjd s4MWoYXtL3xRZTTC7o3GbNPQr42+/E71beG2E9KpaCXvl0r2QVPMlnb4bUO6QDfE6tgJBSik4Rlm/o 5XHtOUvsS6FfUPZ3RlQegjRSCjM5msSzhiStAwMPt1OquSz0QsfDQxmA+CLZpy6DWOJYTUkU5tzR01 hVGOUhF7QRK/O0NRHZxJkGSnDzZS/TmMzayHKVBGNDqBDykRPXsHJramc5e5mHuZl/s3uIsFbm+BJZ lZC75yL/VkAdYCiuYo4AWNv4SjSKYkEaMHrNr6Iy2v7HAXVoc+RhCITlGX7ik52DvxRvQH5Q6j83Aa MGuJWJeZ6HvhDgr11Mo9RByVmOa4Ccq8oWpF6OYIDOlnK+ziPoSQ4G1IrV8g== X-Developer-Key: i=ribalda@chromium.org; a=openpgp; fpr=9EC3BB66E2FC129A6F90B39556A0D81F9F782DA9 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?1760440942190175670?= X-GMAIL-MSGID: =?utf-8?q?1760440942190175670?= 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 Wed Mar 15 13:30:14 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ricardo Ribalda X-Patchwork-Id: 70223 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:5915:0:0:0:0:0 with SMTP id v21csp2335079wrd; Wed, 15 Mar 2023 06:39:08 -0700 (PDT) X-Google-Smtp-Source: AK7set84mDwkd97PY7wdv6yrcRxPS4+ucXySw8aH/COULwfvYeUxDfBIM44GcridBV7os+athCaY X-Received: by 2002:a05:6a20:12d0:b0:d6:3208:85bd with SMTP id v16-20020a056a2012d000b000d6320885bdmr2337778pzg.54.1678887548651; Wed, 15 Mar 2023 06:39:08 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1678887548; cv=none; d=google.com; s=arc-20160816; b=eXKrrhDy2y4d/SAonh8y9dU38YnDcyosyh81DXyihBzEBWK+DeWEs1JgGDlfRr3Tyo aAH1Cq7fMOZJvrKzz/KXhChadlXK66v5cRzODjOxJJgv/esKGSc777GYP4IsiG8+Rgqq Zlm4Y0HvCkjXSuvpMryHkhuQlkihUzeIidn8Q9dALDI6zZvZ/bnUAHMvBMZtr6qbMHiZ 5lxxi97rjjvBpzaz1oP5MF3As9jfzAKKMzWJ6XUacZ6wqWKD9aG47GTWM0IOKv91Rzhp LmRfv0dzBC7yOKvrHpdlHhW7SpCa0S5PEI9T5rm0KDpgD/Y947RgT6TJ2Pk7I7FdAOHK vnZA== 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=BCetzbC/t3lLU9FYivD4HFIyzSXjH/vBXeXQggQC2ORs15zVpmngeiA1PjBZr8ascp BHXgBDI5YphpvQjYvBYY468Did+SsTHT6MDpjwU7XIYRG9JPLhVYv7t1NrIxkB8Nzmwp laLT2G1e5yBaa4xk3iy9iwg3kRoDNcKGcko7MKB55xDXEzxjxGRRQGe7/MhBDiaP47vJ lSNVGgSn9SLGaMDCiDkx9/UFJM0I+QEU+hyoa0clAiNtVlZpgmNmg4pvxweNjMxE0JBq n5zK5e9nWoazBv0WDhwRAvdM5zscypc887lf8Cg15pjoxe0S+AzsBQYSdR24MHTgvrlj 4irw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=lkGXUzVf; 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 p6-20020a625b06000000b005d8fd3b36ecsi5047103pfb.228.2023.03.15.06.38.56; Wed, 15 Mar 2023 06:39:08 -0700 (PDT) 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=lkGXUzVf; 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 S232262AbjCONax (ORCPT + 99 others); Wed, 15 Mar 2023 09:30:53 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34688 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232005AbjCONag (ORCPT ); Wed, 15 Mar 2023 09:30:36 -0400 Received: from mail-ed1-x534.google.com (mail-ed1-x534.google.com [IPv6:2a00:1450:4864:20::534]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 9E0F091B5F for ; Wed, 15 Mar 2023 06:30:29 -0700 (PDT) Received: by mail-ed1-x534.google.com with SMTP id cy23so75448901edb.12 for ; Wed, 15 Mar 2023 06:30:29 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1678887027; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:from:to:cc:subject:date:message-id :reply-to; bh=a5a+6HYmuNF0NcA/SU/mW3fBSurTLFx+qA7YB8EkD48=; b=lkGXUzVfMNurFTpaDvb74x5w7jDczuCxdoDbMkv4itF7+EnifGtL5hT2whGNkBD+VZ JVgD5smdZm9pojK8aq6n+UbciMGKM+n4+JYa6HhruKyhL8kcIhTwJ8z2reGaPi59R26M nin4rOhjgVBOIhwgFLlCj8DoAhfkOFTuUzfGM= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1678887027; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=a5a+6HYmuNF0NcA/SU/mW3fBSurTLFx+qA7YB8EkD48=; b=yf2kMTk+vZKIkDATms2wUyjtpiWPijSoEolSTXVt8hG0lZFQgqqDOLAu8XlgGQVXPS FzJMqoqajcETgBi2Tw5SZONvVFD8JV2cIIW+i3eGJO/ZM/RrR40KZo5nBdaJQKrHFLT0 i1QtOLmbMUKqw7vmGYKIvrU1yd3H5m7gnFe1RpcdM5mPEcthofLyWMmHKR/Z4Ve98BU6 p0Dk4BECCqFOG5zxnbe6iwvt1UZWKWgB0+AwNTEw9ZpNhZel8gv2b49cjJKsFJg83xaY rCZx1e5Al5O8Y/TXFzIcW43k9/ElyrBWWevJIs3f7WpNvmseYMHgoEO0HbVMwe45kSgI 2JOg== X-Gm-Message-State: AO0yUKX57MV6N9SIklvmthGC9PUrJHyQzP9/NKKKRUr7rt3XmNB4kiQF 1hzaP9FUwU0bu+xVK+ja9+0r6A== X-Received: by 2002:a17:906:17ca:b0:8b1:76ca:f228 with SMTP id u10-20020a17090617ca00b008b176caf228mr6039405eje.39.1678887027610; Wed, 15 Mar 2023 06:30:27 -0700 (PDT) Received: from alco.roam.corp.google.com ([2620:0:1059:10:491c:f627:27f0:1c4]) by smtp.gmail.com with ESMTPSA id ha11-20020a170906a88b00b0092043ae1a8asm2534638ejb.92.2023.03.15.06.30.27 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 15 Mar 2023 06:30:27 -0700 (PDT) From: Ricardo Ribalda Date: Wed, 15 Mar 2023 14:30:14 +0100 Subject: [PATCH v9 3/6] media: uvcvideo: Quirk for invalid dev_sof in Logitech C922 MIME-Version: 1.0 Message-Id: <20220920-resend-hwtimestamp-v9-3-55a89f46f6be@chromium.org> References: <20220920-resend-hwtimestamp-v9-0-55a89f46f6be@chromium.org> In-Reply-To: <20220920-resend-hwtimestamp-v9-0-55a89f46f6be@chromium.org> To: Laurent Pinchart , Mauro Carvalho Chehab Cc: Sergey Senozhatsky , Ricardo Ribalda , linux-kernel@vger.kernel.org, "hn.chen" , linux-media@vger.kernel.org X-Mailer: b4 0.11.0-dev-696ae X-Developer-Signature: v=1; a=openpgp-sha256; l=5234; i=ribalda@chromium.org; h=from:subject:message-id; bh=5jcOY5wQypEMPaIWclRrjn20dOHGC/hJF48i1+E0AwQ=; b=owEBbQKS/ZANAwAKAdE30T7POsSIAcsmYgBkEchqFy31rdemB9D0huME7IsTEWnrnkF2YuRPmJwl +6uiHROJAjMEAAEKAB0WIQREDzjr+/4oCDLSsx7RN9E+zzrEiAUCZBHIagAKCRDRN9E+zzrEiEQaD/ 4uYq2DW4edlL96kv3i1Hy2R7ZRLYbLh1nDeP6WpArdUMsIr/g/Im81vRErUGYEDEhN+kXAcaNUB99s F4k0gWxZt+en5y1wqzbRuVsKUKuoG3JGIhJGIGOM0eg+apLSPa81ycrrHF3HZhVndNtggQ0v/+YH/a uFfC5ecHJKsUBsjO/+2XfSnnHxqz6IH7zD77H1eXehcTP70PwvAjlF2Q4ckmMz1zueGgpy4+lVd1Zp Ufnmc1bZuCMzYL9ReCUJ827WQgmvflNsL7WQowEKZ13d+O2jp8J7yfFadtSVcJyWPUt+0UHi9cWSxd MSZxWQ+p5tbRjElPtY4olYxLivCt87uYcEoTIxIh4lL/K39w0cUZjw1oVPipzUnTlnbGiKFTsDArgy IEZcKk1D2GekdoZ8ml5tLbsr8Tgqq6cp7auNUm1fId/MCpLhClf/TgGM7kRJxb0mntvhqF8kPYMnX0 HHxaH8OZK8F5/P3NSeNKJNRnQVaZtAldT8f2QAkDEwgPA5P9rJbp0SHJ/KCMTn7NesHZUuYy280TdH qFWVUfAXhVjdAr9zH75cZSdlLWKlftIIkI+MIfw5Ap3CTfP3M/o1rYEXnTenOf2Jrn3F9FFjl8Y60W K9Kse57pCuBprkYYUWG/E1QwDgoogoo3hhvnZvPo37fqUuyHb4QIR+g6F/Aw== 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?1760441190192784366?= X-GMAIL-MSGID: =?utf-8?q?1760441190192784366?= 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 Wed Mar 15 13:30:15 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ricardo Ribalda X-Patchwork-Id: 70226 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:5915:0:0:0:0:0 with SMTP id v21csp2338127wrd; Wed, 15 Mar 2023 06:45:22 -0700 (PDT) X-Google-Smtp-Source: AK7set/uLgWzFio7JVheBEexlLSyZ8NgewMgoNYiZNAoP5zmtsQOUXPGzr+1Qz9vZscPIDLtlPlL X-Received: by 2002:a17:90a:1d1:b0:237:5834:294b with SMTP id 17-20020a17090a01d100b002375834294bmr42684835pjd.41.1678887922180; Wed, 15 Mar 2023 06:45:22 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1678887922; cv=none; d=google.com; s=arc-20160816; b=jo9pLnexVzdg2xTk7FvX8HyuRLcFgJ6PRf3HITBZ+Er0ljp/ye3DDXzryQxhgArGvS FAN6ObxDaSg4SXRi2rbx1enrYS1SLAQc6FGNrLbahNGma2adqFf6j6MHrAluFJDZjdw3 W8IOqWPFCMMYDgtX27OJ1JCSY9w7MKnHNNQfNWGFYBfQF+hZYfevdkqLzEsP8oryjz13 qGVJsE2TZs41RP7oXUhxzpHnwWv8LXZqG6mlA0PxVrzJkHqNHKmPQjN6si4QcJ/0qd4X vjv3+N/cBJeUfG+6KIKPZ6ObVLeGexFb170u1IVVfZjSD5caPVaqPEP9TkU5SfTGC97I SvAA== 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=bFONIGVUPrRZKFG2cOIeHEvcjXK/o+BM+fygcqkVgrKo7eEvahxVTt1uRY+Nmk5qwg 8sls4EeDDb83zOiI2rbtdmnpObiA4D8yqmMqRYIYU+1esv1EM5sAW+KfY5e0hBZrVfKQ NvhnBcSgtOAj3RXDjAVEs309Cdc8ycyXc5ycQRVFkQZZeIUFttR8aGPpMZFuhvB4/Ez4 bnqKI6cqrAhLYqs1qY85UKT6jBJtTEGfgwozSDMTYvb8mEqoe1T1ZwrRtLKo0vdLlMSD YKPiZid1l5Jrvujpk4pn+X7x0uUL08ULIcZF6UeZAVGaPGZ/2ZP2NVfFgT8hSUbmVgPP BLbg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=ToM7VD9X; 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 pc7-20020a17090b3b8700b0023d029be072si1779546pjb.153.2023.03.15.06.45.09; Wed, 15 Mar 2023 06:45:22 -0700 (PDT) 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=ToM7VD9X; 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 S232235AbjCONaq (ORCPT + 99 others); Wed, 15 Mar 2023 09:30:46 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34704 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232130AbjCONag (ORCPT ); Wed, 15 Mar 2023 09:30:36 -0400 Received: from mail-ed1-x533.google.com (mail-ed1-x533.google.com [IPv6:2a00:1450:4864:20::533]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 3DA1A3253F for ; Wed, 15 Mar 2023 06:30:30 -0700 (PDT) Received: by mail-ed1-x533.google.com with SMTP id x13so28232775edd.1 for ; Wed, 15 Mar 2023 06:30:30 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1678887028; 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=ToM7VD9X/aUdz9PzNycBJiRnRHWzb+x6BnG96KWk4BmxIrH4T4KmFE9CHYb8s0VIQb OXJrtXCcNpDCC2BcstyhWQMdvHXz1ec6jeO9iUDX3jRzzCHZ2nkxemOfvvAmRudJiLyN QwCf1/RCkIN4DcBlo5NFxOciPrhEqFsLnP9Eo= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1678887028; 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=SMx+69WyDj58X/8p16J3w1cAjpzql8ACPrLX4idMUfl5l9CGgIDu8JXUMlXuKj//oA N/qhY3+ZQ1VbOGjfPoQqkT4IlRxvydSDViKL5+h+UKDWcb/dhElAgd8/uLvSluDR10CG /No89gRTLKEJcj8mDxQcAqAk9zZWPqE6micLimszdp94EtEzt6CTdTS5PDmnbRYjAzAX swY40yKsE2tbfaDeXuvS/SLUHSAaatbyFaMZwO/VkElZIfRCyQrZ90LHBLsHpGrwOw55 gFohcmOhOcETjh22vo7Y98iyFhmL65SIEPE4YQOatpynDIPO+6WHoQWem1/NQ758BC17 yC5g== X-Gm-Message-State: AO0yUKVMg5k4/zE7P+CC6/KN5c++M/griNSb7Oiw9ritMk7/57sIov/j K2QnYT0P9GXQoKeALRVWJLGkYA== X-Received: by 2002:a17:906:7fc2:b0:8b1:3002:bd6d with SMTP id r2-20020a1709067fc200b008b13002bd6dmr7662768ejs.31.1678887028676; Wed, 15 Mar 2023 06:30:28 -0700 (PDT) Received: from alco.roam.corp.google.com ([2620:0:1059:10:491c:f627:27f0:1c4]) by smtp.gmail.com with ESMTPSA id ha11-20020a170906a88b00b0092043ae1a8asm2534638ejb.92.2023.03.15.06.30.27 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 15 Mar 2023 06:30:28 -0700 (PDT) From: Ricardo Ribalda Date: Wed, 15 Mar 2023 14:30:15 +0100 Subject: [PATCH v9 4/6] media: uvcvideo: Allow hw clock updates with buffers not full MIME-Version: 1.0 Message-Id: <20220920-resend-hwtimestamp-v9-4-55a89f46f6be@chromium.org> References: <20220920-resend-hwtimestamp-v9-0-55a89f46f6be@chromium.org> In-Reply-To: <20220920-resend-hwtimestamp-v9-0-55a89f46f6be@chromium.org> To: Laurent Pinchart , Mauro Carvalho Chehab Cc: Sergey Senozhatsky , Ricardo Ribalda , linux-kernel@vger.kernel.org, "hn.chen" , linux-media@vger.kernel.org X-Mailer: b4 0.11.0-dev-696ae X-Developer-Signature: v=1; a=openpgp-sha256; l=1966; i=ribalda@chromium.org; h=from:subject:message-id; bh=Ka10ItjemOVlrqrWVbbdhzTBAJcpymfvLrHEyyU17oM=; b=owEBbQKS/ZANAwAKAdE30T7POsSIAcsmYgBkEchrnf062mGexzCcK14u+qoHjARmKRwxGuNN3as8 6ad6scuJAjMEAAEKAB0WIQREDzjr+/4oCDLSsx7RN9E+zzrEiAUCZBHIawAKCRDRN9E+zzrEiAKEEA CG9+cG77r2uDQOhqdDXHVs4vEnubrbB26q41MZuaEbFiuy6ZLyB0Gx0uGOFciMs5BEi21K0hMNBCwz ZDbm2aZqPJH3kHIYClkiI90s8hmPQSCHHLCHZZkvrjm7Le8uEjQBSpdP0b7rCghccnwtV8U+nLcY3s q1niE+9G7ga4UY0JYZZO5AQdwpVv+FA0fTnMLEb6ovIo5K8mSg+O2u0OzwYFPEvtbZ9FflragtLgJk M40o7QlRBq5TcweZHWTImH5jw4a3Qjjt5ceK2rnY10y6OGduFSlDREJxuGt4HVA8N0pCMTNDNELPeE ABXNbWzuvNMwbYyRLTnFhSE+SS0efBoi1ibGRP/nXo/T1ou/4i10HKxxi6Zh8MCxBv3A8TtxcYwk9p PyqYlZhLNsrKJ6xOCWz0hMcRerkUjiwEbb5s3opkvBBahno6UlrNttSK/iDvILAo68oPbkMXG6kBmN +ccwfqLVr99EbK4g/+OafbkXQpePOJxwXbbqXLZNZ8O7tYE0n4BQ3ececLYeOuJuaU7JVyA9MSBOa8 QvmO18/mJmNMBHn4gX6AFytKK4KzYTLkmBfSJCulz8MvLsMD14QiPaNwaDiP2h/clJiwcK/TYHYwmc 0mru1B96URZdV3COUAhLsFhCedb0O+ayyf1AUGf/4CsD/q+P7GmIErKutf2A== 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?1760441581538655200?= X-GMAIL-MSGID: =?utf-8?q?1760441581538655200?= 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 Reviewed-by: Sergey Senozhatsky --- 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 Wed Mar 15 13:30:16 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ricardo Ribalda X-Patchwork-Id: 70224 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:5915:0:0:0:0:0 with SMTP id v21csp2335082wrd; Wed, 15 Mar 2023 06:39:09 -0700 (PDT) X-Google-Smtp-Source: AK7set9TqLjtwyf4gzPiGQMMiZlGunYaqi3IkAxyIUlpSb94huAHrYyaAnAeFgCrX+MlKM9XH0+y X-Received: by 2002:a05:6a21:32a2:b0:cb:8d74:112e with SMTP id yt34-20020a056a2132a200b000cb8d74112emr45180075pzb.45.1678887548725; Wed, 15 Mar 2023 06:39:08 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1678887548; cv=none; d=google.com; s=arc-20160816; b=OKohucaIWJieYbPAcfykFwsnG3SZfeB2xYB1bE+AptdZEKpQwHVCFg9lp7PS+Yb/9e OVRkzXAia1+zXFk+VAK00SnX73lE8foQQGCGffJMzidlZhxzG+rG3+d34JQnQmlXKU/o uxs4MkpNiDqumNhbzjngeKjfMpFm2qVi1EFo8bjb5pdOPChZAjauyV7JYBqIViuigMxI XKD7wS2FEzfOYhtqUeiDO7syFLLgEclVgNSlz3FOySFAFCD47tY63bIkWta7aqiobJqC oHhzn36v0QrIa+FEQIFnwFkabiM7pI1FGX1yco56FJOlulu5TvQFsUD1SkZd4gE7wHvL AUzA== 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=nfrIME0wJIYRWxUiJsRB8/dUZqSdjz1B4rSK467QV2U=; b=E3VnBFXPVtB4wqhqdYK5ZTwakyHlOeX4vNvuCt9MVXHPmGg3TMFyd7SnwNzCRh9PKT w5JKtqqpC7Cj7aGCpSeZ0Ldj9Iw01MyZr5FRfo2urDqo+/aqYg3pxj5K4mcUQFvYapwK Yfwjf57PwaCyCkVY7Qlm/YP3PLNMKpB1xhCfhrKl3nxIxS8FCqAWxBqVC/IB/eLdra/n hecqFZ5btb74YxsOV14D8yfn1vgEW7R4TALlVoRXu/BzS5d03rICzgPACMbE6sTANc2q rQ5+Wvj0bklhIkmqzID6cn/9vweK1hQS5Ush7lFOBeBpM8X6nqrq9Mr2nFCO6oDTXOTu 7xGA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=KHaT5Vyl; 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 m13-20020a654c8d000000b005077dda0499si5078920pgt.731.2023.03.15.06.38.56; Wed, 15 Mar 2023 06:39:08 -0700 (PDT) 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=KHaT5Vyl; 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 S232296AbjCONbA (ORCPT + 99 others); Wed, 15 Mar 2023 09:31:00 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34706 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232131AbjCONag (ORCPT ); Wed, 15 Mar 2023 09:30:36 -0400 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 1D8D791B7C for ; Wed, 15 Mar 2023 06:30:31 -0700 (PDT) Received: by mail-ed1-x536.google.com with SMTP id w9so4789864edc.3 for ; Wed, 15 Mar 2023 06:30:31 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1678887029; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:from:to:cc:subject:date:message-id :reply-to; bh=nfrIME0wJIYRWxUiJsRB8/dUZqSdjz1B4rSK467QV2U=; b=KHaT5VylGWcmvubhEggVy1vnsnxEkDSBrt3DeXHUsFx0R4eN1KDdZ3ymOH3PxVeKpX pNHSUjoqcZN1nHEA1MDBDH64GV5eJ2F1F8kcKCYV0jRuSvEYp/mYAHlqB+bD3ClW6Ebw vh2DGGYroFbQWIYp4S5befUpvtQkeYtj1r+dQ= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1678887029; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=nfrIME0wJIYRWxUiJsRB8/dUZqSdjz1B4rSK467QV2U=; b=HY6lD/NWK1QnFb22E4123AGkSc/WdQfrGrRLogLGY29zvzSD3w27MGEA4dmI/p45BB 2IJLyqvLwVRz+u33h9Eg/Szs4MRiZzn2/UQnkNBCwvfeoZLxpCISqOdZn2DItTM6iBjl Ltor9aPhdlWjyGEaVbgPofDrhCv0h0yGJ6mccyEPaHTCOyNB/S8lLYGUMSs4MEKl4hAC VFGlz/kLUplJ317wqgYJsRJ/4S4UM0BjFaP11HxiIMQnxg2oRriaz/1DrUTYaWn64lvF ZINMoL1OvHaQHzQsdw09cOWzySsywZj1UOe/bPHXqrSErkoQvDQlv3jbqXDzjUkQdZC5 386Q== X-Gm-Message-State: AO0yUKVkLJ+SBb7TXb6OaEDZEc9YLtjAd9pbML7Q2yDKR4qLUYhIzWDa hfY43FsHG0QkSQnEYGmQnoh4Fg== X-Received: by 2002:a17:906:4e92:b0:878:72d0:2817 with SMTP id v18-20020a1709064e9200b0087872d02817mr6062723eju.29.1678887029512; Wed, 15 Mar 2023 06:30:29 -0700 (PDT) Received: from alco.roam.corp.google.com ([2620:0:1059:10:491c:f627:27f0:1c4]) by smtp.gmail.com with ESMTPSA id ha11-20020a170906a88b00b0092043ae1a8asm2534638ejb.92.2023.03.15.06.30.28 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 15 Mar 2023 06:30:29 -0700 (PDT) From: Ricardo Ribalda Date: Wed, 15 Mar 2023 14:30:16 +0100 Subject: [PATCH v9 5/6] media: uvcvideo: Refactor clock circular buffer MIME-Version: 1.0 Message-Id: <20220920-resend-hwtimestamp-v9-5-55a89f46f6be@chromium.org> References: <20220920-resend-hwtimestamp-v9-0-55a89f46f6be@chromium.org> In-Reply-To: <20220920-resend-hwtimestamp-v9-0-55a89f46f6be@chromium.org> To: Laurent Pinchart , Mauro Carvalho Chehab Cc: Sergey Senozhatsky , Ricardo Ribalda , linux-kernel@vger.kernel.org, "hn.chen" , linux-media@vger.kernel.org X-Mailer: b4 0.11.0-dev-696ae X-Developer-Signature: v=1; a=openpgp-sha256; l=6682; i=ribalda@chromium.org; h=from:subject:message-id; bh=1XOlapgZ7o2YnyPsHBz+Zu3te118klpRecTLUAUw/1Q=; b=owEBbQKS/ZANAwAKAdE30T7POsSIAcsmYgBkEchskmhPNyQcOP0px7P1LeWbWNLniHOx4a86Kzhj 1fGkW0aJAjMEAAEKAB0WIQREDzjr+/4oCDLSsx7RN9E+zzrEiAUCZBHIbAAKCRDRN9E+zzrEiPCuD/ 9aGs1INStszDd9kwQmNc+7X/FvhIisrcqqhFYKTs8n4vMBpepipuRtuPtX925dXnuLgHgjYZP1Pae/ /nni4S6OpCb/5Q3cqO4e5PuofG9f8c3mjwRHUYyu3sLFFgIT882ICgLKDDk4xU+tNWwnwI97FpdHFl x8pWWR1qnRqdci0mT+jUMpOFUvlLN2UWpdR1QwKrxGoplQQIT6pU1TGMRuxy52d1Il6iYFBS9lWBQa LYyNATj+nHh5iPocEq/Vx/M5WvsIJfmDLMmB3pMr93vP2T057BfFy+AgYrZxHlo8CM/qmbbYK4GtL1 46IhXLf69ohhBo0a0n36DSfr3LSDaYG7zZfYr49ExMBOS2sP00++hIHcgDgnNOmZzzKsGH6Qdy8dai dsg5FimQbfWl6Ygb1IOsMXpgMwgRIpEEDtpxcvM2IUIaZPNLxpgv+/WDFWUrC6/zmGNysGn9C8KY5Z KmFyEOCux16IE2SLgjF+kKGyTqEY6B7RuCCROWNeArhEz6hthfvF8f0TCeirJM6DvsQnqzOXvzqZJz NmGX4rrsjWfNdevApQHFjE6VsgBaFDJ8zRuKXl9fmG7R8izf6r6ATNr5i5EUSmNlYbwh6gDw8mLaJV 3y2DjuupdjjCDk4QXx5uvrvzkxyPvTZAkhsava5DdpqxyO4vWt0fDbwnUKaQ== X-Developer-Key: i=ribalda@chromium.org; a=openpgp; fpr=9EC3BB66E2FC129A6F90B39556A0D81F9F782DA9 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?1760441190164020169?= X-GMAIL-MSGID: =?utf-8?q?1760441190164020169?= 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 | 84 ++++++++++++++++++--------------------- 1 file changed, 38 insertions(+), 46 deletions(-) diff --git a/drivers/media/usb/uvc/uvc_video.c b/drivers/media/usb/uvc/uvc_video.c index 6d0243ea0e07..1db0d1bc80e6 100644 --- a/drivers/media/usb/uvc/uvc_video.c +++ b/drivers/media/usb/uvc/uvc_video.c @@ -466,19 +466,30 @@ static inline ktime_t uvc_video_get_time(void) return ktime_get_real(); } +static void uvc_video_clock_add_sample(struct uvc_clock *clock, + const struct uvc_clock_sample *sample) +{ + unsigned long flags; + + spin_lock_irqsave(&clock->lock, flags); + + memcpy(&clock->samples[clock->head], sample, sizeof(*sample)); + clock->head = (clock->head + 1) % clock->size; + clock->count = min(clock->count + 1, clock->size); + + spin_unlock_irqrestore(&clock->lock, flags); + + clock->last_sof = sample->dev_sof; +} + static void uvc_video_clock_decode(struct uvc_streaming *stream, struct uvc_buffer *buf, const u8 *data, int len) { - struct uvc_clock_sample *sample; + struct uvc_clock_sample sample; unsigned int header_size; bool has_pts = false; bool has_scr = false; - unsigned long flags; - ktime_t time; - u16 host_sof; - u16 dev_sof; - u32 dev_stc; switch (data[1] & (UVC_STREAM_PTS | UVC_STREAM_SCR)) { case UVC_STREAM_PTS | UVC_STREAM_SCR: @@ -523,11 +534,11 @@ uvc_video_clock_decode(struct uvc_streaming *stream, struct uvc_buffer *buf, * all the data packets of the same frame contains the same SOF. In that * case only the first one will match the host_sof. */ - dev_sof = get_unaligned_le16(&data[header_size - 2]); - if (dev_sof == stream->clock.last_sof) + sample.dev_sof = get_unaligned_le16(&data[header_size - 2]); + if (sample.dev_sof == stream->clock.last_sof) return; - dev_stc = get_unaligned_le32(&data[header_size - 6]); + sample.dev_stc = get_unaligned_le32(&data[header_size - 6]); /* * STC (Source Time Clock) is the clock used by the camera. The UVC 1.5 @@ -541,21 +552,19 @@ uvc_video_clock_decode(struct uvc_streaming *stream, struct uvc_buffer *buf, * and stc and sof are zero. */ if (buf && buf->bytesused == 0 && len == header_size && - dev_stc == 0 && dev_sof == 0) + sample.dev_stc == 0 && sample.dev_sof == 0) return; - stream->clock.last_sof = dev_sof; - - host_sof = usb_get_current_frame_number(stream->dev->udev); + sample.host_sof = usb_get_current_frame_number(stream->dev->udev); /* * On some devices, like the Logitech C922, the device SOF does not run * at a stable rate of 1kHz. For those devices use the host SOF instead. */ if (stream->dev->quirks & UVC_QUIRK_INVALID_DEVICE_SOF) - dev_sof = host_sof; + sample.dev_sof = sample.host_sof; - time = uvc_video_get_time(); + sample.host_time = uvc_video_get_time(); /* * The UVC specification allows device implementations that can't obtain @@ -578,46 +587,29 @@ uvc_video_clock_decode(struct uvc_streaming *stream, struct uvc_buffer *buf, * the 8 LSBs of the delta are kept. */ if (stream->clock.sof_offset == (u16)-1) { - u16 delta_sof = (host_sof - dev_sof) & 255; + u16 delta_sof = (sample.host_sof - sample.dev_sof) & 255; if (delta_sof >= 10) stream->clock.sof_offset = delta_sof; else stream->clock.sof_offset = 0; } - dev_sof = (dev_sof + stream->clock.sof_offset) & 2047; - - spin_lock_irqsave(&stream->clock.lock, flags); - - sample = &stream->clock.samples[stream->clock.head]; - sample->dev_stc = dev_stc; - sample->dev_sof = dev_sof; - sample->host_sof = host_sof; - sample->host_time = time; - - /* Update the sliding window head and count. */ - stream->clock.head = (stream->clock.head + 1) % stream->clock.size; + sample.dev_sof = (sample.dev_sof + stream->clock.sof_offset) & 2047; + sample.dev_stc = get_unaligned_le32(&data[header_size - 6]); - if (stream->clock.count < stream->clock.size) - stream->clock.count++; - - spin_unlock_irqrestore(&stream->clock.lock, flags); + uvc_video_clock_add_sample(&stream->clock, &sample); } -static void uvc_video_clock_reset(struct uvc_streaming *stream) +static void uvc_video_clock_reset(struct uvc_clock *clock) { - struct uvc_clock *clock = &stream->clock; - clock->head = 0; clock->count = 0; clock->last_sof = -1; clock->sof_offset = -1; } -static int uvc_video_clock_init(struct uvc_streaming *stream) +static int uvc_video_clock_init(struct uvc_clock *clock) { - struct uvc_clock *clock = &stream->clock; - spin_lock_init(&clock->lock); clock->size = 32; @@ -626,15 +618,15 @@ static int uvc_video_clock_init(struct uvc_streaming *stream) if (clock->samples == NULL) return -ENOMEM; - uvc_video_clock_reset(stream); + uvc_video_clock_reset(clock); return 0; } -static void uvc_video_clock_cleanup(struct uvc_streaming *stream) +static void uvc_video_clock_cleanup(struct uvc_clock *clock) { - kfree(stream->clock.samples); - stream->clock.samples = NULL; + kfree(clock->samples); + clock->samples = NULL; } /* @@ -2108,7 +2100,7 @@ int uvc_video_resume(struct uvc_streaming *stream, int reset) stream->frozen = 0; - uvc_video_clock_reset(stream); + uvc_video_clock_reset(&stream->clock); if (!uvc_queue_streaming(&stream->queue)) return 0; @@ -2257,7 +2249,7 @@ int uvc_video_start_streaming(struct uvc_streaming *stream) { int ret; - ret = uvc_video_clock_init(stream); + ret = uvc_video_clock_init(&stream->clock); if (ret < 0) return ret; @@ -2275,7 +2267,7 @@ int uvc_video_start_streaming(struct uvc_streaming *stream) error_video: usb_set_interface(stream->dev->udev, stream->intfnum, 0); error_commit: - uvc_video_clock_cleanup(stream); + uvc_video_clock_cleanup(&stream->clock); return ret; } @@ -2303,5 +2295,5 @@ void uvc_video_stop_streaming(struct uvc_streaming *stream) usb_clear_halt(stream->dev->udev, pipe); } - uvc_video_clock_cleanup(stream); + uvc_video_clock_cleanup(&stream->clock); } From patchwork Wed Mar 15 13:30:17 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ricardo Ribalda X-Patchwork-Id: 70225 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:5915:0:0:0:0:0 with SMTP id v21csp2336083wrd; Wed, 15 Mar 2023 06:40:58 -0700 (PDT) X-Google-Smtp-Source: AK7set8teF47UdoCI1RwIZCTXicsxGsg8exrJ8eXsks29FhxLN94LpxYtYov4oB96QZ5TsKuwEgA X-Received: by 2002:a17:90b:2245:b0:230:d6a8:a9e9 with SMTP id hk5-20020a17090b224500b00230d6a8a9e9mr40148885pjb.48.1678887658522; Wed, 15 Mar 2023 06:40:58 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1678887658; cv=none; d=google.com; s=arc-20160816; b=TS4Oa8XNLQGXNVjye0Tq0BzWGvMiiGb4n1tZRXw0tTFChAbwpJGoxTIG77loOcmqRQ sOFKTF8OsTHH9CulB4G57uQcqSndP/E6LVczgt+oa5t7XtKlbvZ+lZv29viBMwyCGcje mCQm6FZIJGDal0mtS1Zacz9Ui3mOaliNgOV6eRZ2pYAG0SKlwDGheAtft2AIXwezL/t0 uY/PGhmTJZMp1xNUC8EljsZIq51xIJxko4xgCvAEsKSoZDr95CID60/gSzP0o0ASlQ6A N6SuCZeNB6npVcUkJsZDiaym3FmbCJpdh6kYNVFEjEvtOHuvD9h/+dnFzRIiBY6tnKC8 sg9w== 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=i5B0qoy4xzYyHKiW419Q3keWH8TLfCy2QqjxB+rQKmY=; b=b0/SNUlxuyw3ai5BrzOK0U+GbxmKUcVW8KLshmd7zymyGtocIOvjX84uvEyu5v34Ah yBpWFHnAjftm90p0g3s1+93JWSo5mU/rd6hrhKOQknKeV1YZ6Btr/Vjp4OEtn00cIn1n BcPDoOqnXd4OIneTkMx27SWEWbERpahpDOcQNh9x5IA8KkZmxRkijxZbSWgyoFvq1TN5 r65jpupZY7qmjTF8r1kxOpmiTSeynGZq7mJ89irB+LF3Ny5TyK0c6RmVQMeAyc1iqDJ3 +y1/IQCp+YlSIn2MUorbuhBmnNyIn/xRcvJMRPuT6upHRaOdelPAs9oXdVDPgwFIDcN9 vzFQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=eDuZrMTZ; 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 s198-20020a632ccf000000b005004b2acf6dsi4937414pgs.810.2023.03.15.06.40.41; Wed, 15 Mar 2023 06:40:58 -0700 (PDT) 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=eDuZrMTZ; 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 S232193AbjCONbF (ORCPT + 99 others); Wed, 15 Mar 2023 09:31:05 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34764 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232171AbjCONah (ORCPT ); Wed, 15 Mar 2023 09:30:37 -0400 Received: from mail-ed1-x52b.google.com (mail-ed1-x52b.google.com [IPv6:2a00:1450:4864:20::52b]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 3B17537B58 for ; Wed, 15 Mar 2023 06:30:32 -0700 (PDT) Received: by mail-ed1-x52b.google.com with SMTP id cy23so75449475edb.12 for ; Wed, 15 Mar 2023 06:30:32 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1678887030; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:from:to:cc:subject:date:message-id :reply-to; bh=i5B0qoy4xzYyHKiW419Q3keWH8TLfCy2QqjxB+rQKmY=; b=eDuZrMTZQRkFCCKAVBZ1yDVVPQuq+yVVR1dwL33dDy6kjAY5yuDtq1vMJ17exZTVvT nPgfXww6zGjvgRadPsNBxLbs2IglY2laD0zpXV5nP6i9Kvbv4VIZ1ZO4IlprXACuUA5z ZNVVurZs3N6YgnTj9oKH08G7QK+blLduri9Mw= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1678887030; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=i5B0qoy4xzYyHKiW419Q3keWH8TLfCy2QqjxB+rQKmY=; b=lRnhsRkJV0u7K/4g7SP4rgZOWUpNINjcjJkZZlxzPUHJxksfSN4ak/vfD6VknTMY7y D/mYzspRNK+FsQCJeG5oNNTxw+jQ0Pu+ogait/iVrlToCW38UVwZxG22y5/e7BlmGHrM somJ8B2IyWDEgyE5EPMTMbP0kc6eH0HDn1dCCTEw/0Wnw5gBXpblngDix7YVD3U8XTtN VBvkFvzVoVZbRR/ARBfrnWxo7dEdG36uDFrMOqCoSlbIRCbnIAr0PGMcMtNJYi41tpJz H2CMQZZlHoDir8Soon5PRvmxbJWYnn7z+1azdQebtwBk7RfvJT93ELEYs7h99mHr51TV N20Q== X-Gm-Message-State: AO0yUKX12ip8dYaCunj7z+IWkm+JHLCC1r4pJlm0gnNG+ZEvisAi9GqL wIzyfcoX7D/p1o0Q8iy5rj3iEQ== X-Received: by 2002:a17:906:bcc7:b0:88c:4f0d:85af with SMTP id lw7-20020a170906bcc700b0088c4f0d85afmr8035808ejb.75.1678887030229; Wed, 15 Mar 2023 06:30:30 -0700 (PDT) Received: from alco.roam.corp.google.com ([2620:0:1059:10:491c:f627:27f0:1c4]) by smtp.gmail.com with ESMTPSA id ha11-20020a170906a88b00b0092043ae1a8asm2534638ejb.92.2023.03.15.06.30.29 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 15 Mar 2023 06:30:30 -0700 (PDT) From: Ricardo Ribalda Date: Wed, 15 Mar 2023 14:30:17 +0100 Subject: [PATCH v9 6/6] media: uvcvideo: Fix hw timestamp handling for slow FPS MIME-Version: 1.0 Message-Id: <20220920-resend-hwtimestamp-v9-6-55a89f46f6be@chromium.org> References: <20220920-resend-hwtimestamp-v9-0-55a89f46f6be@chromium.org> In-Reply-To: <20220920-resend-hwtimestamp-v9-0-55a89f46f6be@chromium.org> To: Laurent Pinchart , Mauro Carvalho Chehab Cc: Sergey Senozhatsky , Ricardo Ribalda , linux-kernel@vger.kernel.org, "hn.chen" , linux-media@vger.kernel.org X-Mailer: b4 0.11.0-dev-696ae X-Developer-Signature: v=1; a=openpgp-sha256; l=2751; i=ribalda@chromium.org; h=from:subject:message-id; bh=fCG+oG/6p/tn1lzkSxpwZk3Rj+eiISLy+hRedLVdGsQ=; b=owEBbQKS/ZANAwAKAdE30T7POsSIAcsmYgBkEchugQqeZNyZ6DJj1IZ/0buGF2GeBZkLFQVks2eb gBmN6LiJAjMEAAEKAB0WIQREDzjr+/4oCDLSsx7RN9E+zzrEiAUCZBHIbgAKCRDRN9E+zzrEiPkZD/ 4omsHCqeQYnAUSpfL3GnIT2BJqCAunmwFKnHdD0FB9IzhfTUorQnctbqToZlfjD6XHniSX+wkRddXm 7HhDjMAnSLYLWCNkRv/9Qpw2590qvZ6cE2OYWwOM4ZDbDlbH4NjFkCSusH1urP6Y9sN1Y8hh6g2fsv WKfqMTCMUdD13JdTyUBpO/FeOFnkZe9UKRsIzP+0kpCyIKJti4RrRthD+qyO8ll5eIoNjxwDVLfMYS j0i3/Io3XK64BdHkkWrROqhHfZ8kqVVExPA6p7cBz6NEmby2WiOY+5ad+Dl8McF3KUBF9H8gtChv9g eGayxIFoJefeJy7u0ZnB1L3otr5j0h9311axpx2XIEYtWHgAKIWjp436t5A9FYDiBYNvx7j0vS6WSM WDY5MWHUl2m2OyjbA8lXlKTaO3Ga7vJUCdcSsW0SAiKWzkt+6ORAVvcYk3Mt1d7w83J6N3Uc1CYWxG ppn6CjkgloIawA+YIsuWFYaSmQCUfQl89ttEgDYvbUVZEySSjHfyPnIPR8NWCCy0GykTlsT28LCnMk vyqfBS5waUf+xBW5vZJZVqWhDsnhmrPhCz84TtIUhZDyD+oJh6hPZHQTO90FmdLo6/kKFLkzgjOX9a V8DtBU+G4AS1FeNsqdOpCogLNXRwuoNJ5ntpOlm3MgZYsvwo59bCNmRqtrqg== X-Developer-Key: i=ribalda@chromium.org; a=openpgp; fpr=9EC3BB66E2FC129A6F90B39556A0D81F9F782DA9 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?1760441305829857584?= X-GMAIL-MSGID: =?utf-8?q?1760441305829857584?= In UVC 1.5 we get a single clock value per frame. With the current buffer size of 32, FPS slowers than 32 might roll-over twice. The current code cannot handle two roll-over and provide invalid timestamps. Revome all the samples from the circular buffer that are more than two rollovers old, so the algorithm always provides good timestamps. Note that we are removing values that are more than one second old, which means that there is enough distance between the two points that we use for the interpolation to provide good values. Tested-by: HungNien Chen Signed-off-by: Ricardo Ribalda Reviewed-by: Sergey Senozhatsky --- drivers/media/usb/uvc/uvc_video.c | 24 ++++++++++++++++++++++++ drivers/media/usb/uvc/uvcvideo.h | 1 + 2 files changed, 25 insertions(+) diff --git a/drivers/media/usb/uvc/uvc_video.c b/drivers/media/usb/uvc/uvc_video.c index 1db0d1bc80e6..c58b51207be6 100644 --- a/drivers/media/usb/uvc/uvc_video.c +++ b/drivers/media/usb/uvc/uvc_video.c @@ -471,8 +471,31 @@ static void uvc_video_clock_add_sample(struct uvc_clock *clock, { unsigned long flags; + /* + * If we write new data on the position where we had the last + * overflow, remove the overflow pointer. There is no overflow + * on the whole circular buffer. + */ + if (clock->head == clock->last_sof_overflow) + clock->last_sof_overflow = -1; + spin_lock_irqsave(&clock->lock, flags); + /* Handle overflows */ + if (clock->count > 0 && clock->last_sof > sample->dev_sof) { + /* + * Remove data from the circular buffer that is older than the + * last overflow. We only support one overflow per circular + * buffer. + */ + if (clock->last_sof_overflow != -1) { + clock->count = (clock->head - clock->last_sof_overflow + + clock->count) % clock->count; + } + clock->last_sof_overflow = clock->head; + } + + /* Add sample */ memcpy(&clock->samples[clock->head], sample, sizeof(*sample)); clock->head = (clock->head + 1) % clock->size; clock->count = min(clock->count + 1, clock->size); @@ -605,6 +628,7 @@ static void uvc_video_clock_reset(struct uvc_clock *clock) clock->head = 0; clock->count = 0; clock->last_sof = -1; + clock->last_sof_overflow = -1; clock->sof_offset = -1; } diff --git a/drivers/media/usb/uvc/uvcvideo.h b/drivers/media/usb/uvc/uvcvideo.h index 07b2fdb80adf..bf9f5162b833 100644 --- a/drivers/media/usb/uvc/uvcvideo.h +++ b/drivers/media/usb/uvc/uvcvideo.h @@ -499,6 +499,7 @@ struct uvc_streaming { unsigned int head; unsigned int count; unsigned int size; + unsigned int last_sof_overflow; u16 last_sof; u16 sof_offset;