From patchwork Thu Nov 3 13:40:49 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ricardo Ribalda X-Patchwork-Id: 621600 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 5962FC4332F for ; Thu, 3 Nov 2022 13:41:31 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229964AbiKCNl2 (ORCPT ); Thu, 3 Nov 2022 09:41:28 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42222 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231372AbiKCNlP (ORCPT ); Thu, 3 Nov 2022 09:41:15 -0400 Received: from mail-ed1-x532.google.com (mail-ed1-x532.google.com [IPv6:2a00:1450:4864:20::532]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C9085BE3E for ; Thu, 3 Nov 2022 06:41:14 -0700 (PDT) Received: by mail-ed1-x532.google.com with SMTP id z18so3032589edb.9 for ; Thu, 03 Nov 2022 06:41:14 -0700 (PDT) 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=QBTXj0Mo20+lqPJvJbV07DHgoZv0TgJ6xnkWV3WDOVo=; b=kpJ+DF6PjVbaFJFClIwC2LT+GMwpgkrYik0GCWpQWnEDyK5RHeaEmgpN1H1m4pbPO7 iukhRLpcj8n97CqE4Q7s2Ho/Kzw1UDSm6WdoUl9u7ERHkMtxySQqCpCXMFwVduVwX5Qj ucny/W6XGe89qBRpKfMowg/k6LTpMzF00Z1Mw= 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=QBTXj0Mo20+lqPJvJbV07DHgoZv0TgJ6xnkWV3WDOVo=; b=3pbPL0QfCwlHLip9C7Knv5F14uFEGLGpqyCVI8g7RtcLlL/6QUSGwI2jMLygZyu+rv XKf/WXtUrwkuunGI+ReVktdLFDSff2Au4XQ9V+SVg2GNbZe6589yg7wsv7bIE6yIVDIT sBWjhmXBbYq0y97jW0PFWb9uSkD0NEBeS6v7ifYznzYUdtXeuzLqP59XFciPUd6/U2ey jd4q3XqqwEd0UAbbB20QMgKkxtPMqbifP66qiGWKqGBLF8fi7fwyGDKub/hRVZ43Wdt3 OP1O3QIztnJbg2vryaSThLl7Nf1Fzc5HNAwkczUaTx5sjQNqV9FAWT315/Bea1C2ON8h ov1A== X-Gm-Message-State: ACrzQf0B1NjMm6A2S+fiosc545s3JeXg4zLlTk6rBJqCUtm1TNWGihOS wJAfibesSFwte4z3fCfmSj0iiw== X-Google-Smtp-Source: AMsMyM6cDOmJqgaysevnZNDkDtF8b0cTaF3lEiUh8+ouWSe3DB0h3ADd+SGhgAkOfYJYGKJR+pqsWg== X-Received: by 2002:a05:6402:274c:b0:461:9921:60cf with SMTP id z12-20020a056402274c00b00461992160cfmr31079740edd.353.1667482873385; Thu, 03 Nov 2022 06:41:13 -0700 (PDT) Received: from alco.roam.corp.google.com ([2620:0:1059:10:c1a3:5bc:2068:8f2c]) by smtp.gmail.com with ESMTPSA id j1-20020a17090623e100b0078d46aa3b82sm521948ejg.21.2022.11.03.06.41.12 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 03 Nov 2022 06:41:13 -0700 (PDT) From: Ricardo Ribalda Date: Thu, 03 Nov 2022 14:40:49 +0100 Subject: [PATCH v2 8/8] media: uvcvideo: Fix hw timestampt handling for slow FPS MIME-Version: 1.0 Message-Id: <20220920-resend-hwtimestamp-v2-8-d8d0616bb612@chromium.org> References: <20220920-resend-hwtimestamp-v2-0-d8d0616bb612@chromium.org> In-Reply-To: <20220920-resend-hwtimestamp-v2-0-d8d0616bb612@chromium.org> To: Laurent Pinchart , Mauro Carvalho Chehab Cc: HungNien Chen , linux-kernel@vger.kernel.org, linux-media@vger.kernel.org, Ricardo Ribalda X-Mailer: b4 0.11.0-dev-d93f8 X-Developer-Signature: v=1; a=openpgp-sha256; l=1969; i=ribalda@chromium.org; h=from:subject:message-id; bh=pNerfrtBJbE7z0e3sYuUA+5U9LQzvOcTp5yM8ujyuVs=; b=owEBbQKS/ZANAwAKAdE30T7POsSIAcsmYgBjY8TwN3zXisLOmlBMSPM91NI1ZKp/+UQ2K81uRsDG O4RuhOeJAjMEAAEKAB0WIQREDzjr+/4oCDLSsx7RN9E+zzrEiAUCY2PE8AAKCRDRN9E+zzrEiDeXD/ 9II30l2x2EBLHN1QVJa5o/1HeUqhrKrd81xF7n/aHdzfnYDRZnyNZ477pPsKiLmvjWhf/9hzCl5Z21 NCgJXb6jHEaegZPKkVuIFjidXdx2NW1UPAYBNfk/fPH35+FxX8cVnTKFfna10nIb5OjAQ5133ksSKn jMgF5tvLQzL1/+9XMRTBOru1sXl9QF1sP1QrPRyCc5+KueXH0cNelcomv8L9PDQLpSCXy1WRVbJgzm Z/KJEqsnvW/ERAJpT46Rx8MJP97Xi2lokEtvGGGhA0a9tDP0Zn/IXm8k99kMniK02qt3pZ3OVmi05F aadaxwSOBus9ZD8BTYmrVqX1ruw+grTT+KuZ0dP+zWMk4juMkbqoWoxqkYz2sjAdN6wKG74ZT/hdnI MrTFEs5BQl5P90SI3SBdEa5iV3uZ/IfLqzF31qpegSxzDWqERNjNgT7j7RFAQl5eaWSZtJK9s7Bz2U 7YXwt+i0h4NItmLSorZUqn0M7bpT3cWPkCvcw+0c4j41M8FysjckCgHnlBdraFpQawANbSmsX1l0Pj iLsc4FMZQq66GQKq87BODBJ3KDMs8kj1YHT9pTCs3KJifowILFBmA2GS/N8cOHLO6O3DqJnC+6WsUm kwujlSEBPKCnr1uIcVzzZLXgCum1ZErNMqG/JF+O9YS9XLsyNJ5LfdXMN+wA== X-Developer-Key: i=ribalda@chromium.org; a=openpgp; fpr=9EC3BB66E2FC129A6F90B39556A0D81F9F782DA9 Precedence: bulk List-ID: X-Mailing-List: linux-media@vger.kernel.org In UVC 1.5, when working with FPS under 32, there is a chance that the circular buffer contains two dev_sof overflows, but the clock interpolator is only capable of handle a single overflow. Remove all the samples from the circular buffer that are two overflows old. Tested-by: HungNien Chen Signed-off-by: Ricardo Ribalda diff --git a/drivers/media/usb/uvc/uvc_video.c b/drivers/media/usb/uvc/uvc_video.c index c81a8362d582..6b6bd521d6c2 100644 --- a/drivers/media/usb/uvc/uvc_video.c +++ b/drivers/media/usb/uvc/uvc_video.c @@ -471,6 +471,20 @@ static void uvc_video_clock_add_sample(struct uvc_clock *clock, spin_lock_irqsave(&clock->lock, flags); + /* Delete last overflows */ + if (clock->head == clock->last_sof_overflow) + clock->last_sof_overflow = -1; + + /* Handle overflows */ + if (clock->count > 0 && clock->last_sof > sample->dev_sof) { + /* Remove data from the last^2 overflows */ + if (clock->last_sof_overflow != -1) + clock->count = (clock->head - clock->last_sof_overflow) + % clock->count; + clock->last_sof_overflow = clock->head; + } + + /* Add sample */ memcpy(&clock->samples[clock->head], sample, sizeof(*sample)); clock->last_sof = sample->dev_sof; clock->head = (clock->head + 1) % clock->size; @@ -594,6 +608,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 14daa7111953..d8c520ce5a86 100644 --- a/drivers/media/usb/uvc/uvcvideo.h +++ b/drivers/media/usb/uvc/uvcvideo.h @@ -647,6 +647,7 @@ struct uvc_streaming { unsigned int head; unsigned int count; unsigned int size; + unsigned int last_sof_overflow; u16 last_sof; u16 sof_offset;