From patchwork Wed Jan 4 10:45:20 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ricardo Ribalda X-Patchwork-Id: 639177 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 F305BC4332F for ; Wed, 4 Jan 2023 10:46:08 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234789AbjADKqH (ORCPT ); Wed, 4 Jan 2023 05:46:07 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:57282 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234701AbjADKpy (ORCPT ); Wed, 4 Jan 2023 05:45:54 -0500 Received: from mail-ej1-x631.google.com (mail-ej1-x631.google.com [IPv6:2a00:1450:4864:20::631]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5A6E712AE1 for ; Wed, 4 Jan 2023 02:45:53 -0800 (PST) Received: by mail-ej1-x631.google.com with SMTP id qk9so81382850ejc.3 for ; Wed, 04 Jan 2023 02:45:53 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:from:to:cc:subject:date:message-id :reply-to; bh=KyBrI1RwfXQizMxJ8rnnrBo4q45UrKGn2OI35vCZsYY=; b=Uep3phL55FIMMwa37576Z/9XYaU7Cjh/iSkuW19M9vpAiL+74bvM1KKqyQMnfC5vO1 2/ANgpV/Fvb0SiM+laEYOILg+QbE0pSwYEdxLv26eD+7k9FRRQJA23cVPxX1Ou6UfxYj FfZTQ9PbbKeBDGGS3JYlBg8ew6fyphoje1Hok= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=KyBrI1RwfXQizMxJ8rnnrBo4q45UrKGn2OI35vCZsYY=; b=SwjIIwqIRjAl1QxD+/S4zyvklRUZjSS+dXkrLXbmdvDAeZK9cQfYOjlx8Lw/mCbkHV 10rMzf6FsoqJa+rXwQQDnpH0aLGY0T+kg8dh75PWY0jopU5Oew2BL/XjEU20I6Liv7At c4fo1u6ayIAMrWk93T4wHD+GzHRdsMt4PTeb/aWSJIUQ6wpkvXNe0Wglco8yL7AG/ntU HOBDqmhanw0obhEW8uMOwhHLnCIqTMmEvljCFZLRqCnjyvl0fXPhYCsAgHviuYObOJ9r 73KmvaUWUC8I3t3TsCmQoBPVMek3+IjQmrlFFRi38srjKakZggfVrA8+GkDKQpj/HZZE EpGA== X-Gm-Message-State: AFqh2ko+W8cufxF7OBGBL3FuY1J/aHDPyBOlfieUyrW0RGlBLvidNmkh d0dCnnp/IDNpfTAojeHaYxRTT2NCqngLYX/sABg= X-Google-Smtp-Source: AMrXdXt00As5fSHArbEefv7fvImmjhDllCRKY5zJkWdWW9VOhpNt1PeXLA6fuJJKbzPfqo0kgnX+Kw== X-Received: by 2002:a17:906:5619:b0:7c0:faca:52c with SMTP id f25-20020a170906561900b007c0faca052cmr40663884ejq.37.1672829151931; Wed, 04 Jan 2023 02:45:51 -0800 (PST) Received: from alco.roam.corp.google.com ([2620:0:1059:10:6531:9bb0:b3f7:86a8]) by smtp.gmail.com with ESMTPSA id g32-20020a056402322000b0048c85c5ad30sm4754971eda.83.2023.01.04.02.45.51 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 04 Jan 2023 02:45:51 -0800 (PST) From: Ricardo Ribalda Date: Wed, 04 Jan 2023 11:45:20 +0100 Subject: [PATCH v3 2/8] media: uvc: Allow quirking by entity guid MIME-Version: 1.0 Message-Id: <20220920-resend-hwtimestamp-v3-2-db9faee7f47d@chromium.org> References: <20220920-resend-hwtimestamp-v3-0-db9faee7f47d@chromium.org> In-Reply-To: <20220920-resend-hwtimestamp-v3-0-db9faee7f47d@chromium.org> To: Laurent Pinchart , Mauro Carvalho Chehab Cc: linux-kernel@vger.kernel.org, "hn.chen" , Ricardo Ribalda , linux-media@vger.kernel.org X-Mailer: b4 0.11.0-dev-696ae X-Developer-Signature: v=1; a=openpgp-sha256; l=1797; i=ribalda@chromium.org; h=from:subject:message-id; bh=FoOybSKY0S3eTj4qt7v1f6DB//nxdb92ZYxbnB6L0cM=; b=owEBbQKS/ZANAwAKAdE30T7POsSIAcsmYgBjtVjRUGIm+ptCBoYbh0RSttvX3gody0gvY/pWZOUo z25/ILuJAjMEAAEKAB0WIQREDzjr+/4oCDLSsx7RN9E+zzrEiAUCY7VY0QAKCRDRN9E+zzrEiJyED/ 4smqXXbUx0OhCo1Gk2G1q5NSxcv/gZhUPIrEp2CohBfmROuHc6K1uWFI1PSWxodXVZi2w/hOugVSCN ptF8H4UiBgG0CRwz/UMibCVCYQ5/ODjGYOvcqlieVFr9wvog+Pahb2fzfCXzw/Up1qVUUynedr9WXj PZUOovW8NBWkos2NsAIE8qZ6oj8QRFiHck2hdqxpHrJsENA4NbHVrNldoVOFsf/VrGi5RCempnU4aL L7QwrwiA8A3YVK+6tTdRFnkEKD9ZCDD2FEyyHPj0TqG+HxVFDmxlDfo1IJ9uHS0h6Sc/pgjMMUNl4C 20esCE33VIUp+jT4j2mC6EL+AEe8txMbzavnbHbVRL1BshNFl5EgmoNO72p7peoCS8ZfbSTXBXz1Wg gI0xVENtpBIQ0A64LjlAGM/sqOCAj/j0JkgwK/S6rLPeWbLRWgizLqXfnV4kvz9TV7CjWBVO2KcgAu OBZ7V4kB8YvO0zh+VvOc6QbsTjoSm/sLzqu3jPqAPk9PmrOY/Z0fWAqx5UZEbuSUIuoZe3bZeYW5DX OGiv1eBKLAVISJ1toBBYhwbQIHcNF6pqipBUmfpdN6HOPiwLvHZDDrCOaSA9I6RkkKmJfHF+5nog8e N6unuSrLBaSDn2KDvWYkEVIZU5086LK9v44mMuZH1TeJSKe54wgDHAcCeyQg== X-Developer-Key: i=ribalda@chromium.org; a=openpgp; fpr=9EC3BB66E2FC129A6F90B39556A0D81F9F782DA9 Precedence: bulk List-ID: X-Mailing-List: linux-media@vger.kernel.org When an IP is shared by multiple devices its erratas will be shared by all of them. Instead of creating a long list of device quirks, or waiting for the users to report errors in their hardware lets add a routine to add quirks based on the entity guid. Tested-by: HungNien Chen Signed-off-by: Ricardo Ribalda --- drivers/media/usb/uvc/uvc_driver.c | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/drivers/media/usb/uvc/uvc_driver.c b/drivers/media/usb/uvc/uvc_driver.c index bd3716a359b0..5448576a8413 100644 --- a/drivers/media/usb/uvc/uvc_driver.c +++ b/drivers/media/usb/uvc/uvc_driver.c @@ -1195,6 +1195,28 @@ static int uvc_parse_control(struct uvc_device *dev) return 0; } +static const struct uvc_entity_quirk { + u8 guid[16]; + u32 quirks; +} uvc_entity_quirk[] = { +}; + +static void uvc_entity_quirks(struct uvc_device *dev) +{ + struct uvc_entity *entity; + unsigned int i; + + list_for_each_entry(entity, &dev->entities, list) { + for (i = 0; i < ARRAY_SIZE(uvc_entity_quirk); i++) { + if (memcmp(entity->guid, uvc_entity_quirk[i].guid, + sizeof(entity->guid)) == 0) { + dev->quirks |= uvc_entity_quirk[i].quirks; + break; + } + } + } +} + /* ----------------------------------------------------------------------------- * Privacy GPIO */ @@ -2154,6 +2176,9 @@ static int uvc_probe(struct usb_interface *intf, goto error; } + /* Apply entity based quirks */ + uvc_entity_quirks(dev); + dev_info(&dev->udev->dev, "Found UVC %u.%02x device %s (%04x:%04x)\n", dev->uvc_version >> 8, dev->uvc_version & 0xff, udev->product ? udev->product : "", From patchwork Wed Jan 4 10:45:22 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ricardo Ribalda X-Patchwork-Id: 639176 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 02E6BC4708E for ; Wed, 4 Jan 2023 10:46:14 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S239028AbjADKqL (ORCPT ); Wed, 4 Jan 2023 05:46:11 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:57310 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234971AbjADKp4 (ORCPT ); Wed, 4 Jan 2023 05:45:56 -0500 Received: from mail-ed1-x52f.google.com (mail-ed1-x52f.google.com [IPv6:2a00:1450:4864:20::52f]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id DDB371743B for ; Wed, 4 Jan 2023 02:45:54 -0800 (PST) Received: by mail-ed1-x52f.google.com with SMTP id i9so47960353edj.4 for ; Wed, 04 Jan 2023 02:45:54 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:from:to:cc:subject:date:message-id :reply-to; bh=5iccy1mOhkEmE1PLN3U2ExlldVmSVxYcFcoIeCsEwlQ=; b=f2Fztr7JuvNC7senkDZqqw01nLMiialjzNpFdhL3P35iDCjDHMI2FmyvKXBrPFUooa ELqmg5MgZh+7/nlB5Gcp3EpFR1PMKZpp5xxxdnAzm6i5O08c4OFWD9p9MJokT9Zi7EJZ Z/XAjglDNpQVaSXdi9XDNgNYcRM31UEEOB1hI= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=5iccy1mOhkEmE1PLN3U2ExlldVmSVxYcFcoIeCsEwlQ=; b=VAyf6R0B+N/GvuDwMn7/++lFUYZFBa+jIpeWB2KNb0nIhz3dNEAsomTSUe0JQyb6cz D0v+p7LpXbopUCH1YNpgc4G8hhNXsVs0Fy1mJi5kNUA8k9RlHDP+3+SQw/2R8gzRDHrX ctoafZGSydB76gRXH14PfHS80SljkBBvRxP9TjYC5l2xPfXJ+/exdO8ljlFlo6jZIaZj GB72ACmjbuJJToPIxyWnWioBR1eJ6FBfojbPsVjEh1ncJiAljl5+vOyy7wOt54aHQMfF txGicWOi0ZEPUo4k40gC+ZT2fTwXjhvYmziTmmUZ6Fuf6l0WnRF9u1o0si69uHinH7lC 4JgQ== X-Gm-Message-State: AFqh2kobvnVEj3aVcnCkuXotHNx0lL0gk8+mWEDhG9Pk3+Eh1bUoLviI tZK0bM8TTOOsuhN+2hRQhblHLnF8i6akgpe1nZ4= X-Google-Smtp-Source: AMrXdXtWxwOiWbwJeR6bi2nidVcMqzT4zxVVPGs6v+/ng2Sh2/7tB9Eyhyh+jJ6RxmDayctbl5dpcQ== X-Received: by 2002:aa7:d04d:0:b0:47d:b4e6:a631 with SMTP id n13-20020aa7d04d000000b0047db4e6a631mr40858113edo.7.1672829153476; Wed, 04 Jan 2023 02:45:53 -0800 (PST) Received: from alco.roam.corp.google.com ([2620:0:1059:10:6531:9bb0:b3f7:86a8]) by smtp.gmail.com with ESMTPSA id g32-20020a056402322000b0048c85c5ad30sm4754971eda.83.2023.01.04.02.45.52 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 04 Jan 2023 02:45:53 -0800 (PST) From: Ricardo Ribalda Date: Wed, 04 Jan 2023 11:45:22 +0100 Subject: [PATCH v3 4/8] media: uvcvideo: Quirk for invalid dev_sof in Logitech C922 MIME-Version: 1.0 Message-Id: <20220920-resend-hwtimestamp-v3-4-db9faee7f47d@chromium.org> References: <20220920-resend-hwtimestamp-v3-0-db9faee7f47d@chromium.org> In-Reply-To: <20220920-resend-hwtimestamp-v3-0-db9faee7f47d@chromium.org> To: Laurent Pinchart , Mauro Carvalho Chehab Cc: linux-kernel@vger.kernel.org, "hn.chen" , Ricardo Ribalda , linux-media@vger.kernel.org X-Mailer: b4 0.11.0-dev-696ae X-Developer-Signature: v=1; a=openpgp-sha256; l=5538; i=ribalda@chromium.org; h=from:subject:message-id; bh=Wsf1Wp3a/Huhnb++hUSQ1p3B//D9EJZrjWECEqWLm0k=; b=owEBbQKS/ZANAwAKAdE30T7POsSIAcsmYgBjtVjUSO1A2pI3RH/4ObqEKNarKdcbkJdUKk8m9h3T UVYHYxeJAjMEAAEKAB0WIQREDzjr+/4oCDLSsx7RN9E+zzrEiAUCY7VY1AAKCRDRN9E+zzrEiK27D/ 41498SeqsUC94w2LA7j36W6bwiDzLolqi+vDLu9sI5fzW/YDVWnyEQxf0nEEtJdtxQg5qtjBMyt7ik IvQYYLBAc2fWYpvXs8RUtWknNERPgnF2iSnLHMziVxkhYWbYZ0RMD+jEh+gtrAidA/GbBQ2jhDq5HS pey2zMubFZLlcULTvsL57dYcfuYIBahWJ+qI+XN0f4GNBDZh5g8Nw6mMAlDjRarcONkIYHlaI+AbNB mIrIb8/yhBwtanTC2NYqV20jvHTls5EFuzV2Ssgp/gZtnzW9BmsHOPoXK5BFEYSrhitsyRBJhM8GWx 5lF2ml+hzKd1m4moijHq0i0aFfJXpBt0IzCFOr5shOfAS6kGscyVOuX5gsqYx9A1bQYeoBPNm4acXQ rhyPxYGICyo131Z6ggez62DcBAQJ0fwOXCepoZsYZmDHUB6/UBb/kVlzklyUfoxK5ZbUy579Y+8bYm JZBanaR3mKIO2TJS7+YaLDShtRulvvnZssfNtYSwMPRgZczXvoVENKnR/e7xk2dO7W9jUSJcTrUEAd yH7F2Hp+9fZp9ynTW1qWCM0Q4eD/I3YV7Jg0UjMGwY5LQC394Pfv9QbRjhzoQVFzrziwvAMcDtuc4H gQDF3fCwYDjT3lNG40ASuJm23GJE4SWASbqbtqpZyKzFOT0ZcEt50ElAaPEw== 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 Signed-off-by: Ricardo Ribalda --- drivers/media/usb/uvc/uvc_driver.c | 9 +++++++++ drivers/media/usb/uvc/uvc_video.c | 11 +++++++++-- drivers/media/usb/uvc/uvcvideo.h | 1 + 3 files changed, 19 insertions(+), 2 deletions(-) diff --git a/drivers/media/usb/uvc/uvc_driver.c b/drivers/media/usb/uvc/uvc_driver.c index c5ab6e2d24c3..c89a1eebe91c 100644 --- a/drivers/media/usb/uvc/uvc_driver.c +++ b/drivers/media/usb/uvc/uvc_driver.c @@ -2582,6 +2582,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 f469c62bedca..bc75c7c40251 100644 --- a/drivers/media/usb/uvc/uvc_video.c +++ b/drivers/media/usb/uvc/uvc_video.c @@ -530,13 +530,20 @@ 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 (stream->dev->quirks & UVC_QUIRK_INVALID_DEVICE_SOF) + dev_sof = usb_get_current_frame_number(stream->dev->udev); + else + dev_sof = get_unaligned_le16(&data[header_size - 2]); + if (dev_sof == stream->clock.last_sof) return; stream->clock.last_sof = dev_sof; - host_sof = usb_get_current_frame_number(stream->dev->udev); + if (stream->dev->quirks & UVC_QUIRK_INVALID_DEVICE_SOF) + host_sof = dev_sof; + else + host_sof = usb_get_current_frame_number(stream->dev->udev); time = uvc_video_get_time(); /* diff --git a/drivers/media/usb/uvc/uvcvideo.h b/drivers/media/usb/uvc/uvcvideo.h index c3424ae29339..88d1790e6695 100644 --- a/drivers/media/usb/uvc/uvcvideo.h +++ b/drivers/media/usb/uvc/uvcvideo.h @@ -75,6 +75,7 @@ #define UVC_QUIRK_FORCE_Y8 0x00000800 #define UVC_QUIRK_FORCE_BPP 0x00001000 #define UVC_QUIRK_IGNORE_EMPTY_TS 0x00002000 +#define UVC_QUIRK_INVALID_DEVICE_SOF 0x00004000 /* Format flags */ #define UVC_FMT_FLAG_COMPRESSED 0x00000001 From patchwork Wed Jan 4 10:45:24 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ricardo Ribalda X-Patchwork-Id: 639175 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 9A53BC4332F for ; Wed, 4 Jan 2023 10:46:17 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S239069AbjADKqP (ORCPT ); Wed, 4 Jan 2023 05:46:15 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:57270 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230499AbjADKp6 (ORCPT ); Wed, 4 Jan 2023 05:45:58 -0500 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 8DC1F17054 for ; Wed, 4 Jan 2023 02:45:56 -0800 (PST) Received: by mail-ed1-x52d.google.com with SMTP id g1so33763434edj.8 for ; Wed, 04 Jan 2023 02:45:56 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:from:to:cc:subject:date:message-id :reply-to; bh=Fi0DwAZll32eIad2Imhk1W2tOAIqK4+yUvq5hehYizE=; b=XUGMCvUZpPHu5JfUsPHIp5UtuySYQQ3ZfTycNgzh4mj/V20ITnQtNomVDsuiaGJaT1 oyXvqyxXOzPSCjI19RAp1geECA+s+BlCE4fESF9UnhdxUtljbZ6F0XRdnfRq2SLJUfo3 pEh1nr6/VLLy05jXwqcFO3AOWX0vYQpAwjwB0= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=Fi0DwAZll32eIad2Imhk1W2tOAIqK4+yUvq5hehYizE=; b=4p4aAVFkiPGFFZaliYTZy/hw1apSEgLGscmoMVHCmWaoYLrqeXFLnRyZlWRFkzNeJX LVH2HduS+i+1BEUL+XOGE/wYI274UID6laKNS0H7cpumftIwtYJNAIE1mmA+c9ds311+ ZQRAQGCWk3kFBuHjhAuLEPom7P3QbGn6k3xbDMNO2MfzYU9TtbEHfhLa3KddzYcgfDGG 5yPBQNwuai7KkvqLX6XHvv85lA24kctqUbC2tX5QexB50bnfGympI9PPbDYM3J1WIe3q ewTu3qV9SWpefjJ+nvbHbftWvroOA6kOTYrRq0X52+lw2SNaNBd/RQWhgE4YCKInnEPY BqEw== X-Gm-Message-State: AFqh2kqxMfMkvKzHMEePLXypKMh3CActJ2oC4M3sCgWvaW5m+5VkQi6N YT6t3uv6cvKlswPMniCZdnOKag== X-Google-Smtp-Source: AMrXdXvwsKG211BYtshW9rWO2NXmxw73UOy+Sy1aOwSFvVVkQWotsKMhdnScrv560S+ZILSBfnCdAA== X-Received: by 2002:a05:6402:538d:b0:487:2ce6:2b80 with SMTP id ew13-20020a056402538d00b004872ce62b80mr25256957edb.8.1672829155133; Wed, 04 Jan 2023 02:45:55 -0800 (PST) Received: from alco.roam.corp.google.com ([2620:0:1059:10:6531:9bb0:b3f7:86a8]) by smtp.gmail.com with ESMTPSA id g32-20020a056402322000b0048c85c5ad30sm4754971eda.83.2023.01.04.02.45.54 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 04 Jan 2023 02:45:54 -0800 (PST) From: Ricardo Ribalda Date: Wed, 04 Jan 2023 11:45:24 +0100 Subject: [PATCH v3 6/8] media: uvcvideo: Allow hw clock updates with buffers not full MIME-Version: 1.0 Message-Id: <20220920-resend-hwtimestamp-v3-6-db9faee7f47d@chromium.org> References: <20220920-resend-hwtimestamp-v3-0-db9faee7f47d@chromium.org> In-Reply-To: <20220920-resend-hwtimestamp-v3-0-db9faee7f47d@chromium.org> To: Laurent Pinchart , Mauro Carvalho Chehab Cc: linux-kernel@vger.kernel.org, "hn.chen" , Ricardo Ribalda , linux-media@vger.kernel.org X-Mailer: b4 0.11.0-dev-696ae X-Developer-Signature: v=1; a=openpgp-sha256; l=1843; i=ribalda@chromium.org; h=from:subject:message-id; bh=A2mJudG+6/05Ux5SJhclsJyARWI+ebtKxr7o7Rpc9H0=; b=owEBbQKS/ZANAwAKAdE30T7POsSIAcsmYgBjtVjYRSolvHGNsAlK8u96MVXslFe5ioZa0X5tQYeg GorUlXmJAjMEAAEKAB0WIQREDzjr+/4oCDLSsx7RN9E+zzrEiAUCY7VY2AAKCRDRN9E+zzrEiICuD/ sHUGrAeWicUm6+W27wgC2bTF9F1cAqj5AXqQ3hu4pmQK+0BSNSMIMxUWPLfp0VeT6BgeSsTkrf+MeJ THid/fK4hxg9E2JIYOLYRAa1FK9H/bUbLBl5UlJr2/p74Q9AsF+CuPBFbiOMIHXyLug9hKPzQ0FhaB 82T33bbRv/p2zlaZ6GyAWafawsT3d/4xZH/Evd+2oW/u0SKSINCcdIW8RtZQeDkEM/W4/W0CbPNt2R AkjLHTgG2MO/KQx6Yo4/SIiEbTI38VR/l61+hct7UDdf3n+BliR3cLaljBfgili9qjSK33T31FC+HB IX+MjqA/7XMUy+VUpC4xIBu+i4BuHEvsDiPXHIKUOVPi2kZa6iNy8UMdDapJ/eHe2hLlisGfb1illj SSf8Nbmc/7HSDgvKEZYTAfqizaEa+JxN5Uxir1q5FS0vlvnea9164Vw/aqIPbhPwEx7oRYPC3Uk7WZ 3Wy3AbLEucFFFNB6iSDVhbNlBvmCN7jJjzBirgUIUrSY+mt9f1Pk010/hlcI3K2FZWAOPsqXP4jzXR GkOqa1/oQkXwLz3sC45FE/zNQsDSTrU83mbG0ka8f0Aq7tdd5IDfUy7NlrocgKB6A3Ap6YI+pI//fv TFrzNcXbjAmoWDG5RcekvZRgQU+HRRCp2wpkksqvE6cTo8uLc+nWDnjTgnyQ== X-Developer-Key: i=ribalda@chromium.org; a=openpgp; fpr=9EC3BB66E2FC129A6F90B39556A0D81F9F782DA9 Precedence: bulk List-ID: X-Mailing-List: linux-media@vger.kernel.org With UVC 1.5 we get as little as one clock sample per frame. Which means that it takes 32 frames to move from the software timestamp to the hardware timestamp method. This results in abrupt changes in the timestamping after 32 frames (~1 second), resulting in noticeable artifacts when used for encoding. With this patch we modify the update algorithm to work with whatever amount of values are available. Tested-by: HungNien Chen Signed-off-by: Ricardo Ribalda --- drivers/media/usb/uvc/uvc_video.c | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/drivers/media/usb/uvc/uvc_video.c b/drivers/media/usb/uvc/uvc_video.c index fe5c7b465adf..5c37b080e0c6 100644 --- a/drivers/media/usb/uvc/uvc_video.c +++ b/drivers/media/usb/uvc/uvc_video.c @@ -743,10 +743,10 @@ void uvc_video_clock_update(struct uvc_streaming *stream, spin_lock_irqsave(&clock->lock, flags); - if (clock->count < clock->size) + if (clock->count < 2) goto done; - first = &clock->samples[clock->head]; + first = &clock->samples[(clock->head - clock->count) % clock->size]; last = &clock->samples[(clock->head - 1) % clock->size]; /* First step, PTS to SOF conversion. */ @@ -761,6 +761,15 @@ void uvc_video_clock_update(struct uvc_streaming *stream, if (y2 < y1) y2 += 2048 << 16; + /* + * Have at least 1/4 of a second of timestamps before we + * try to do any calculation. Otherwise we do not have enough + * precision. This value was determined by running Android CTS + * on different devices. + */ + if ((y2 - y1) < (256 << 16)) + goto done; + y = (u64)(y2 - y1) * (1ULL << 31) + (u64)y1 * (u64)x2 - (u64)y2 * (u64)x1; y = div_u64(y, x2 - x1); From patchwork Wed Jan 4 10:45:25 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ricardo Ribalda X-Patchwork-Id: 639174 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 6A513C5479D for ; Wed, 4 Jan 2023 10:46:23 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S239094AbjADKqV (ORCPT ); Wed, 4 Jan 2023 05:46:21 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:57342 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S239005AbjADKqA (ORCPT ); Wed, 4 Jan 2023 05:46:00 -0500 Received: from mail-ed1-x535.google.com (mail-ed1-x535.google.com [IPv6:2a00:1450:4864:20::535]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8026713D66 for ; Wed, 4 Jan 2023 02:45:57 -0800 (PST) Received: by mail-ed1-x535.google.com with SMTP id i9so47960452edj.4 for ; Wed, 04 Jan 2023 02:45:57 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:from:to:cc:subject:date:message-id :reply-to; bh=FDgPA71BB6P36oy/yaWrNwP2I0p7AiHxv3mHi9TEYCk=; b=XtrsS/1UZK81LaeViHHnmVT6dYQWNOCIPPq6upkTAESvZJtiphcvlwCMmvfFqNqnCG EjEGBS9DFN8QEhPzyZZJxLrhNGHcocY/twTOBwFSZmg24iJn0g1QGeOEh8SmvhPlxip+ Y8BnVpUJw+IqyVLAoGVia6OIv4gIZhAGjUMB8= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=FDgPA71BB6P36oy/yaWrNwP2I0p7AiHxv3mHi9TEYCk=; b=LGi1jEHTApbOh2Z5U9R22BEqVVHv9/63KR4OiVyoWXIlDouqZ8iIjxjRfiDxkYUlc0 jsyE4s1HTGibKKCkTAXnxGZVSx9JeDFXVfg5y4w1+exJIP816ftCcyAi1ZGBxHGz993h U/NOAJxKGcfBbnWoMWlBLCqCWqUlMrXE+xsFQfQqMiJCMl3/gzLcTxhPDnDs38ubrJWT XRLFhoIOnIqtcGXDAo8sIA8otYPiwZAh50ABTku5cdz3JpQEpcSD/0GE5E4WZagokvh6 PZEvbrofOiKYi24j3AwKJK0MScR5rjfdJWeM0ev1t4L2eXkBqRVIkZ19Gk/f9fXlh4YW wLtA== X-Gm-Message-State: AFqh2krjhvSzHGas92GK9tDKw4YclHEVwmu47NH6HePmAIpgAOdRWKrc INU3Ceki0kD9Pl0LM5ZLGhw8/g== X-Google-Smtp-Source: AMrXdXs++SNoPgWWG2zaTy3gn1kVq0tM3XhxbFmpXW7ZLN76kBv6tsf4Jq3V2sa6w6iKWMXtsZ9U1w== X-Received: by 2002:a05:6402:1f06:b0:46f:a6ea:202 with SMTP id b6-20020a0564021f0600b0046fa6ea0202mr51524811edb.37.1672829156072; Wed, 04 Jan 2023 02:45:56 -0800 (PST) Received: from alco.roam.corp.google.com ([2620:0:1059:10:6531:9bb0:b3f7:86a8]) by smtp.gmail.com with ESMTPSA id g32-20020a056402322000b0048c85c5ad30sm4754971eda.83.2023.01.04.02.45.55 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 04 Jan 2023 02:45:55 -0800 (PST) From: Ricardo Ribalda Date: Wed, 04 Jan 2023 11:45:25 +0100 Subject: [PATCH v3 7/8] media: uvcvideo: Refactor clock circular buffer MIME-Version: 1.0 Message-Id: <20220920-resend-hwtimestamp-v3-7-db9faee7f47d@chromium.org> References: <20220920-resend-hwtimestamp-v3-0-db9faee7f47d@chromium.org> In-Reply-To: <20220920-resend-hwtimestamp-v3-0-db9faee7f47d@chromium.org> To: Laurent Pinchart , Mauro Carvalho Chehab Cc: linux-kernel@vger.kernel.org, "hn.chen" , Ricardo Ribalda , linux-media@vger.kernel.org X-Mailer: b4 0.11.0-dev-696ae X-Developer-Signature: v=1; a=openpgp-sha256; l=6103; i=ribalda@chromium.org; h=from:subject:message-id; bh=Y+RBkriXzItA9F9XcVh0gDzU4cduaikhSaZRnYLxgS8=; b=owEBbQKS/ZANAwAKAdE30T7POsSIAcsmYgBjtVjatQTNxFXCouiEcpNU0Ctlaqn2ejx6qCnliSKl Eo4FayWJAjMEAAEKAB0WIQREDzjr+/4oCDLSsx7RN9E+zzrEiAUCY7VY2gAKCRDRN9E+zzrEiLF5D/ 9EE4FYXMWbulzY9cMNWC8FcsUF07yEeuJ9Xk64XToqjtLg7WinkcTdQgCFaO2N+QLFVb01Q67WHwqZ 8x9aDyjmelxrGLSziyZcoHsnUmWUYCvU7oj6pce8rMZ4DGNIwBuD/LKVRleJQyshig6x8CV1TdrWXW lmT0cU29Hvn9cGGlKXrUnZEgvvUZsNgPLoLBDlJ1jhyISx3h5huo74Xl3/WzK069o2hkkjF1y0eGl5 Y8WegAHOoL2xV9HtMkNXrSfxdHSl+eUeXLoDHCwW6zGnKkC2V53ish2ujMiMVXmRaoD/i5dpCF35AP jBpxKYQ1OEZr2+kJe5BUmxtnxLaJ8vq3umSy+3HdG8hE6cBhU/QyJ095XTg+zS598lXW6T1DNbrZtG OCJneNrly/gZUvGaXGZy6Et8PvG9ND/hAX0F1ADaCunm73Mi9a1ODedunHMSRCHsBR1I6c9y67DSaJ ubDPWiZr4iEdz7sUo4mfUkV/vIYtvvWoIYRORobKkIzV7XddImx3rbondYfwNLvrdcDuXCZodU9hDc k6y6IQkViLd+YmKXYsbpcAEGjfBN3NswDCSS+vRRdOLwWpkkQ5YFFebqdH5CgoKlcnSK/ntavwsems EYDXmlj62UKiKd9DDLqFEpHuaG9AndtoEuEbDBTbYV5YshkI344nHVp0TN9w== 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 Signed-off-by: Ricardo Ribalda --- drivers/media/usb/uvc/uvc_video.c | 81 ++++++++++++++++++--------------------- 1 file changed, 37 insertions(+), 44 deletions(-) diff --git a/drivers/media/usb/uvc/uvc_video.c b/drivers/media/usb/uvc/uvc_video.c index 5c37b080e0c6..2c6da49ff84b 100644 --- a/drivers/media/usb/uvc/uvc_video.c +++ b/drivers/media/usb/uvc/uvc_video.c @@ -464,18 +464,29 @@ 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->last_sof = sample->dev_sof; + 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; switch (data[1] & (UVC_STREAM_PTS | UVC_STREAM_SCR)) { case UVC_STREAM_PTS | UVC_STREAM_SCR: @@ -531,20 +542,19 @@ uvc_video_clock_decode(struct uvc_streaming *stream, struct uvc_buffer *buf, * case only the first one will match the host_sof. */ if (stream->dev->quirks & UVC_QUIRK_INVALID_DEVICE_SOF) - dev_sof = usb_get_current_frame_number(stream->dev->udev); + sample.dev_sof = + usb_get_current_frame_number(stream->dev->udev); else - dev_sof = get_unaligned_le16(&data[header_size - 2]); + sample.dev_sof = get_unaligned_le16(&data[header_size - 2]); - if (dev_sof == stream->clock.last_sof) + if (sample.dev_sof == stream->clock.last_sof) return; - stream->clock.last_sof = dev_sof; - if (stream->dev->quirks & UVC_QUIRK_INVALID_DEVICE_SOF) - host_sof = dev_sof; + sample.host_sof = sample.dev_sof; else - host_sof = usb_get_current_frame_number(stream->dev->udev); - time = uvc_video_get_time(); + sample.host_sof = usb_get_current_frame_number(stream->dev->udev); + sample.host_time = uvc_video_get_time(); /* * The UVC specification allows device implementations that can't obtain @@ -567,46 +577,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 = get_unaligned_le32(&data[header_size - 6]); - 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; @@ -615,15 +608,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; } /* @@ -2092,7 +2085,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; @@ -2226,7 +2219,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; @@ -2244,7 +2237,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; } @@ -2272,5 +2265,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); }