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: 661978 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 670E0C6FD1C for ; Fri, 10 Mar 2023 09:06:56 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231192AbjCJJGz (ORCPT ); Fri, 10 Mar 2023 04:06:55 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43446 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230155AbjCJJGZ (ORCPT ); Fri, 10 Mar 2023 04:06:25 -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 60E387DFBE for ; Fri, 10 Mar 2023 01:02:19 -0800 (PST) Received: by mail-ed1-x52e.google.com with SMTP id u9so17703619edd.2 for ; Fri, 10 Mar 2023 01:02:19 -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=Bteha5wgxf8bkwCebGb8Fyx5dLTSlwocoLaFbDot+Kf2dfPsLBknUx9oL3jV4P3Fuj E3/xIpTqUsQFzS+fVtIUQ117rBy3BmjJ3Ek5n36DP9PK4ESw0Y3KI2sJaMPYGC2zFAUq A0S/dJxHLYT8xMXOp2LYKlqaw5sTn7aUFHBKo4WF8oK/6hTD3ULyUP+4FmzeDmBZ9w08 QOuq7Wr2/6Qz7/e5Y723jbYilhTiH1hA3Ns0P8NdczLiZ01b8y00vE46p+rtWKvpBwJ6 bvChVAYOoyWu2wPzhXe0iWy6VW0QuhqPkpbY9zI4yThkgvyJvr6TbgT76gCzBSGIUD5F UwMQ== X-Gm-Message-State: AO0yUKXx4aTD6M+Jq+fSZ1okDQvslz6ErJy/foyUzTS/G/oc0Xxvkt1E pPpk1QgZ9L3IJtxvccrRarAhWg== X-Google-Smtp-Source: AK7set9BK+XjIoADosdNMDa6cAcEn2PS1KMoGbx3MqO43xbv0IEReknN5FIEXyx5jot/PrEojx1TAQ== 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 Precedence: bulk List-ID: X-Mailing-List: linux-media@vger.kernel.org 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: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: 661977 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 37FFEC6FD19 for ; Fri, 10 Mar 2023 09:07:20 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231375AbjCJJHS (ORCPT ); Fri, 10 Mar 2023 04:07:18 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41862 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231372AbjCJJGd (ORCPT ); Fri, 10 Mar 2023 04:06:33 -0500 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 4F2A0112583 for ; Fri, 10 Mar 2023 01:02:31 -0800 (PST) Received: by mail-ed1-x52a.google.com with SMTP id o12so17617433edb.9 for ; Fri, 10 Mar 2023 01:02:31 -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=OhLRFPzcWyddP69CGy1zajy4fEf870BJuVZYPmtp6zcbyQ3s1B6IoLncxx+aFZrwf9 f9YwVjUvTmvDqCfMmJ8fDRDw0wg1DH+ul6jxfTbVnhKrvMASTUCTmv3KQHoJ2Pqv03dj eHPsgRv98ra0n7uTTMSeBG9djd99qUR0lvYLq/1LSMEVd7yvISzzmcnLM9Qe0noju6zk OCZmtfDQavNWJPpXdHiG91boM2VWC9b+sKnpHjF/OlxqbhVhKgz7LC9PefyRKADQEiHb aeWQaoaXT5eKq+4ZmvBgtH8jallShcbGl5TYYI+tvKaAylDlf0zJ4kFnR2sOa8Q76TPt jugA== X-Gm-Message-State: AO0yUKWRN0w/T+j0ygIPI44RYNocA1D8oKqk4Rh5l0Ri7OXmb0+KcTKJ 4VI/flkxf6YaNScsQ0HBRezN4w== X-Google-Smtp-Source: AK7set8GtyVQ8FJebMR3Ilm8/Wev2wx0QAMhQLWGBQV7Mp7MNFA3TKHHIgQxtpym/6HzMncPQ9yEFQ== 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 Precedence: bulk List-ID: X-Mailing-List: linux-media@vger.kernel.org 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: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: 661976 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 1B5D4C64EC4 for ; Fri, 10 Mar 2023 09:07:41 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230209AbjCJJHj (ORCPT ); Fri, 10 Mar 2023 04:07:39 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40086 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229922AbjCJJGm (ORCPT ); Fri, 10 Mar 2023 04:06:42 -0500 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 3B590112589 for ; Fri, 10 Mar 2023 01:02:40 -0800 (PST) Received: by mail-ed1-x534.google.com with SMTP id u9so17704307edd.2 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=w0BykU0vM8rNoylyeMXInY+34G4364we7Gmvx7qqDDmMTpBtjw31ZiCvzN8NjYjpLi MLE3B4Uo5yh1WvLg7x7NNopqBGEqjstahhSgmh73VsjVCTWBJuY4MPcCn/w9AHo/dQbN x2xO6KGbYYU30wGy1Hxdxtzqa16N3Lt2bk7YIDe4HjAiYgCAd1L3qHBGPyTqYHsh8ZlS tybnRjm9elEoyCKrKQMwqhvJzatvkAWsGDl/1m9nGhPy3BJhGsf3itjsO8dVoQZEoTUU BasJAdFn4o1oMRW3mgg3GZNhBLOIC+hXSeVDHdDEP8a96S9eaihtjlgbMC28cZc/A6IW J0Gg== X-Gm-Message-State: AO0yUKVDPFPuxDKCKlxNMXx7q4OAyTOWXoFpubwBByaTQqZPis9JEzbI i71Nl6ZYqHBJFmO5PbmwUJSr/g== X-Google-Smtp-Source: AK7set8uBJUWEdjTJ8acxq8/GZ0DAAdqZ0/iRxCNqAQ28wKNp1xWI8Xihg28oO14JL6zkHe+c262zA== 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 Precedence: bulk List-ID: X-Mailing-List: linux-media@vger.kernel.org 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); }