From patchwork Tue Sep 20 14:08:13 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ricardo Ribalda X-Patchwork-Id: 608145 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 B7304ECAAD8 for ; Tue, 20 Sep 2022 14:08:52 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231528AbiITOIu (ORCPT ); Tue, 20 Sep 2022 10:08:50 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41704 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231529AbiITOIi (ORCPT ); Tue, 20 Sep 2022 10:08:38 -0400 Received: from mail-ej1-x62a.google.com (mail-ej1-x62a.google.com [IPv6:2a00:1450:4864:20::62a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A6D385AA07 for ; Tue, 20 Sep 2022 07:08:35 -0700 (PDT) Received: by mail-ej1-x62a.google.com with SMTP id a26so6485374ejc.4 for ; Tue, 20 Sep 2022 07:08:35 -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; bh=NCc5XnE86A35upfQb7w9Zg49ZtxeysSLRX/7yyxgtR0=; b=BX1eXrPvlIVcwDO3uOoPlFa+ImKnuEQCQV/DWcnsoCT6sqrex4Hu7kkrwCgLMyWJtx UNJcjUQV2YD8jravMv0yw7c/xqBO25g8hMAACLntYpoJoUXKUELs4ZOkieYcfdHHBhFd l/quskDGx3sQyd4FHgPSVZrexFzLbKQLo3caM= 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; bh=NCc5XnE86A35upfQb7w9Zg49ZtxeysSLRX/7yyxgtR0=; b=bFCBUxyBamhmTwRedrAyZuPhpKrSPXaUIe9h8PKlbTn3Ossp/8qkT6QdLUjAZAuBCN xuvXVs7RqgfGLNOQ6spPo5POuVvmJkDSv4hZyDDhGZNKwEhV8l6Zm3ma40I2ZMFhLulk F7oSFSGOLWIknJ/5xuAz/ZVnDP/Wnr3VKHGRd93aRXrcGk557ebSeEFFucvwe4RF1zwk 8h6cSuUrefWAlseD+dT9PoGs9i7KoGgVqwEIAqkI6A6jaleBnBD99oDeVkLP6YGdHKAJ ZDVenYstmoh7kt3lwnC1b2RCstu8ttCO+xh6QQ3F0tSaCv4SaZVwHM7teNk+6oLdfhTK o+aw== X-Gm-Message-State: ACrzQf26bCJWkuycyARBxv4Ax57HTKPzKgkHd2NG0ggccPv3eS6muaeM eLZzoigNR2Dp3ywLPihYs0rchw== X-Google-Smtp-Source: AMsMyM7PE6lyS3+0vtSumMvg+ryWzwkFIVOxy/qOiei/xbbb0im3D3s8lTsiQHWWrV9HXqQr9yDzsQ== X-Received: by 2002:a17:906:cc4a:b0:779:ed37:b5a3 with SMTP id mm10-20020a170906cc4a00b00779ed37b5a3mr16861886ejb.626.1663682914022; Tue, 20 Sep 2022 07:08:34 -0700 (PDT) Received: from alco.roam.corp.google.com ([2620:0:1059:10:935d:52b0:7461:88e1]) by smtp.gmail.com with ESMTPSA id j17-20020a17090623f100b0077016f4c6d4sm936564ejg.55.2022.09.20.07.08.33 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 20 Sep 2022 07:08:33 -0700 (PDT) From: Ricardo Ribalda Date: Tue, 20 Sep 2022 16:08:13 +0200 Subject: [PATCH v1 7/8] media: uvcvideo: Refactor clock circular buffer MIME-Version: 1.0 Message-Id: <20220920-resend-hwtimestamp-v1-7-e9c14b258404@chromium.org> References: <20220920-resend-hwtimestamp-v1-0-e9c14b258404@chromium.org> In-Reply-To: <20220920-resend-hwtimestamp-v1-0-e9c14b258404@chromium.org> To: Laurent Pinchart , Mauro Carvalho Chehab Cc: "hn.chen" , Ricardo Ribalda , linux-kernel@vger.kernel.org, linux-media@vger.kernel.org X-Mailer: b4 0.11.0-dev-d93f8 X-Developer-Signature: v=1; a=openpgp-sha256; l=5683; i=ribalda@chromium.org; h=from:subject:message-id; bh=z/+kzQHc3YxeEXHUqT6czLGAa5vbV9no6M5D99w9Aks=; b=owEBbQKS/ZANAwAKAdE30T7POsSIAcsmYgBjKclW0N/GT6a158BpvExijGgXwYoWPIwxcszDBMi7 C5iAMHCJAjMEAAEKAB0WIQREDzjr+/4oCDLSsx7RN9E+zzrEiAUCYynJVgAKCRDRN9E+zzrEiGC4EA CKtFH0hEXzzHQtBofaYIEP0M3h7dxYuzsCo0upAQgd5jzreQXQPSfq4+hIHvfBqlKEWYO8j1Kl5BVw jcs9KGfYF3H+Oxk2tAlNHpvh/MehKyHnE9HtNbQjkU9OEOcoEQElSleKv9meMo7eKZ0t59K9tlVNRv 0z/MaIVQCFJ8slQ5YxkqP6OGJoUCqcr4w4fVYvGKQf758AFB5AHRdyoFC8zZR7ARU7MnUNnQRnqHu1 4FQ2rxxukpuiuzwZaiyBqT0IWVohLEk/1X16na43YkMdBqhFODfxt6M0W4CtmWXINvx+5pvYf4RKgF tPsK3IFGY8sY2LJ6fRu3pLcRHahGF2lxvxpR6f4C+ZsJSU96txF693nooIQcwqQVuZwbBDIfPDv5iF Wgex9vKVDIeh7Jt44sQXJ6yYFZ/TtaDahxhL1lR+mZcZBVTsHtPxvFpHNphehj/n/m+h1cAesTsJBg 2T1GpsRCbJDPHZCPUvjhJnHpucaRohuofM+LXHcihW40X8TC/ku/Gd7psBf3ytCyKwc1f4zJQCiC5f qJCD6ZOure5118fJouhI0V5F7HVrAbpGMd3eAuDv6UxWfJdU4BipA6TiNoNCwrTUApUlvIVLtGfp8F vNjD/8l2RRUQvxsMubsWoJQyBHKULQNpoNWmGS4a+QCVT3vK791zMsyVcJsQ== X-Developer-Key: i=ribalda@chromium.org; a=openpgp; fpr=9EC3BB66E2FC129A6F90B39556A0D81F9F782DA9 Precedence: bulk List-ID: X-Mailing-List: linux-media@vger.kernel.org Move the addition to the buffer into its own function and remove the stream dependency to all the clock functions. Signed-off-by: Ricardo Ribalda diff --git a/drivers/media/usb/uvc/uvc_video.c b/drivers/media/usb/uvc/uvc_video.c index 7c7fb478aef7..9e7ab7ef8624 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: @@ -530,20 +541,19 @@ uvc_video_clock_decode(struct uvc_streaming *stream, struct uvc_buffer *buf, * 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 @@ -566,46 +576,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; @@ -614,15 +607,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; } /* @@ -2077,7 +2070,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; @@ -2211,7 +2204,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; @@ -2229,7 +2222,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; } @@ -2257,5 +2250,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); }