From patchwork Mon Jan 9 10:00:51 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ricardo Ribalda X-Patchwork-Id: 40771 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:4e01:0:0:0:0:0 with SMTP id p1csp2068760wrt; Mon, 9 Jan 2023 02:04:21 -0800 (PST) X-Google-Smtp-Source: AMrXdXvLkpsXytO8TpDcuHYSSswizACmvJI8yHV+WZ9nJ59sovG9D2n7+wTe7oX6yQ/PMFEE5CoO X-Received: by 2002:a17:90a:7503:b0:226:c196:db55 with SMTP id q3-20020a17090a750300b00226c196db55mr17653974pjk.18.1673258660949; Mon, 09 Jan 2023 02:04:20 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1673258660; cv=none; d=google.com; s=arc-20160816; b=vBjHH7YtHLbLbu4pia+IjhyqFfhrTievqFtLCFcLASTNGnZ6CofdUhW9njdDUOQIFj IYUuiikl6xH0+UO4oszBz1hgRrgJe4/d4SIEo6exW1u3rlOl2eN1Nd5agRMvNTe7VXQw KKMk8SMtbqSZJvCklm3zhnw2FQeN9Ic9SvzFLIFqQx3LMqXfrIxsjKR72zxUl3nZa3S0 ECyaHW308yw20W0Hm0ncPdn3eUiei1pg5f68lDhDyoPTQpvd8cZh7TP1O2RuIMIkY6n+ f5VBjdkvs7rGEkDrWs/vBtMQ4c3Cm7wWX/oOr8M+/P2r5LDnuceSghFCHNu8ZjM02pPh A9qQ== 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=IYmriAjuARTqoa8gks/FWwbBbDlG7rw+znorcPY4O/c=; b=JK7QrzgU3cY2JmOJrIulOjSfO0KWl3f3Zv7VRZaTcTZubueR7u9pbqKOJM3k8PzcV5 PpcOTx+JJA1iVlWJjSuAZR7H6fGHGjfyEx4bX6buazPN28V3DSmwSieRlRWKPUn4AD3W oyOdJ1NTKY2R78J2rzXAtLoEKrUaCyUEU7bh4NDOR12DTlkPI83AkdiWPef1g5ULYtTy PVYPeYkKjsNyddj93IYEw7mxI3OWH+HqpQg7lehP9gkhhdbTb1uYCfKGa/e+DkaBfary oOOAIBAijIaa4Q2qInRW06AFUSMojo011A4c8W4eIlOwNS9JABHqdFhvGyRvv5oEkXSO UGzg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=e8d7HA3B; 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 n64-20020a632743000000b00489a4cba34fsi9253005pgn.21.2023.01.09.02.04.07; Mon, 09 Jan 2023 02:04:20 -0800 (PST) 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=e8d7HA3B; 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 S236536AbjAIKCq (ORCPT + 99 others); Mon, 9 Jan 2023 05:02:46 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51358 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S237068AbjAIKBZ (ORCPT ); Mon, 9 Jan 2023 05:01:25 -0500 Received: from mail-ej1-x633.google.com (mail-ej1-x633.google.com [IPv6:2a00:1450:4864:20::633]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 7995612770 for ; Mon, 9 Jan 2023 02:01:17 -0800 (PST) Received: by mail-ej1-x633.google.com with SMTP id jo4so18692836ejb.7 for ; Mon, 09 Jan 2023 02:01:17 -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=IYmriAjuARTqoa8gks/FWwbBbDlG7rw+znorcPY4O/c=; b=e8d7HA3Bqkout7TJIcxnPsRrw7QTdH390nDfdq/xs+BxeMotE3+kv01NE7PaKD3JPL 124LEJofpGeAyqm7P6z1F8/XhF4XT82iWbtdoGqXnPoDy8C6Zy8K14/Q1N/0ZSqhiRHT eHJU1SNcNVYHCZVX/cX3fRh0wGISFPVWiRu04= 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=IYmriAjuARTqoa8gks/FWwbBbDlG7rw+znorcPY4O/c=; b=AhoiWy74L25PtlGmbgXFjmC06asj38j2wdcWgP2i3fekMI/OH0bYcJL9HyXlsRLuIk iiSmxkzr6Zl/wowxeoSUPLOaOWxr1JD70XlNo72fsilGXcfsER0FM9tUT9EgcFMqW+B4 ISiIhoiXOpP/uQ9Av7+3cF6QjYg4TiNlxUPkgvl6iYtRm2V6RIuISBbh4CyRvMN57dnu 7GkMkBMQzNHDMVz6R4rIbq2gkFJsE0qyM9uiLoEVxBSdTUaqi8G9ozO7/DNDmj7h1Dpo p/lxH71kKK7uEBtaqGRtlAAcZrxwdFjCW5lU0WxXpxIPGp0Unl54Va21cVHy5MkvXfyZ qwnw== X-Gm-Message-State: AFqh2kqhJPK0aCzpTL5epTQFI5PtbMAHSDJjKLbgM1tQdZQoxSD3nHtv /tPY0Ly781pMlYDypqzPZFqn2w== X-Received: by 2002:a17:906:a842:b0:816:ef2a:631a with SMTP id dx2-20020a170906a84200b00816ef2a631amr54776548ejb.31.1673258476257; Mon, 09 Jan 2023 02:01:16 -0800 (PST) Received: from alco.roam.corp.google.com ([2620:0:1059:10:cd22:9dd:6d54:8488]) by smtp.gmail.com with ESMTPSA id bg14-20020a170906a04e00b007c094d31f35sm3557548ejb.76.2023.01.09.02.01.15 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 09 Jan 2023 02:01:15 -0800 (PST) From: Ricardo Ribalda Date: Mon, 09 Jan 2023 11:00:51 +0100 Subject: [PATCH v4 4/5] media: uvcvideo: Refactor clock circular buffer MIME-Version: 1.0 Message-Id: <20220920-resend-hwtimestamp-v4-4-a8ddc1358a29@chromium.org> References: <20220920-resend-hwtimestamp-v4-0-a8ddc1358a29@chromium.org> In-Reply-To: <20220920-resend-hwtimestamp-v4-0-a8ddc1358a29@chromium.org> To: Mauro Carvalho Chehab , Laurent Pinchart Cc: Ricardo Ribalda , "hn.chen" , linux-kernel@vger.kernel.org, linux-media@vger.kernel.org X-Mailer: b4 0.11.0-dev-696ae X-Developer-Signature: v=1; a=openpgp-sha256; l=6607; i=ribalda@chromium.org; h=from:subject:message-id; bh=gd7LIKTyyDLtaDNUSPifHorCJvktQIQVilx4JFUvgoI=; b=owEBbQKS/ZANAwAKAdE30T7POsSIAcsmYgBju+XlwWqnAmWHCIg0P35z3vMJsHj5JcVP1XuXTd8i djmoZDuJAjMEAAEKAB0WIQREDzjr+/4oCDLSsx7RN9E+zzrEiAUCY7vl5QAKCRDRN9E+zzrEiF56EA CakfahkFXDSi7hY6hh0eX/zeV1DmgRo1xbW5YdJ7GOnUEYCTc87Fj5ywfuOr2vnsFjBJSgjb1+ZqEE HgVyFmBlBldXB2D3LG4YI96HV5WlKXqPJERLQuVc/SxAPciGCV+y08R8Fgu1yCsBpIbrFQwQliAPNt SJEYGcSXW+1hv3c8dpJx7UTaMjhObGxVbDfHxbHQkEezqiKqHOXWnwLY98MYDEXszyRRdXvv7OICDH 7YDed1kGLy6CHCoRevemj2AL8bnAeeRvmxcTWCLfNUlS0n2jmNur4cTYRQoXi6P65ARN/KJHfcR8Kj BXw4KxR25PEWJltsPTKHOUeSCqaudmLGZN//VQWDHS6LUYqJAXIcjPhIgF4O7IeylnczRu6CZk7v7b QHIwrSDOWZpZV1nLUp50lyuumaeAEq9LfZU5aXSC7Pcf/bYsbPMKkAW1sY/1G722Bx++zAu4yEU5PD +22oF/CVc6OkXZIH7SWjUf3VmLqvpwJP070OU6MDNTCfdefVGIaR1Gz1xZuWzCnP4DN1ws/f+OWwIQ PqYzUwVE08X5tII+kFUvnskbB5Av4YvlfSelhGsFoHbOcGqMniLaxokRtpKGO/2iHETSpIWgix5LbR XZLw5W49hENMNfM/G9Ysi1ft/MaXiME8WdSxWiwPN5CXEJwgAkPGqhNzrcbQ== 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 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?1754538874032037187?= X-GMAIL-MSGID: =?utf-8?q?1754538874032037187?= 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 | 84 ++++++++++++++++++--------------------- 1 file changed, 39 insertions(+), 45 deletions(-) diff --git a/drivers/media/usb/uvc/uvc_video.c b/drivers/media/usb/uvc/uvc_video.c index ee0f40b9eae3..02831cf0a00f 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]); /* * Some devices make a borderline interpretation of the UVC 1.5 standard @@ -536,19 +545,21 @@ uvc_video_clock_decode(struct uvc_streaming *stream, struct uvc_buffer *buf, * algorithm. */ if (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; /* * 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 = usb_get_current_frame_number(stream->dev->udev); + if (stream->dev->quirks & UVC_QUIRK_INVALID_DEVICE_SOF) { + sample.dev_sof = + usb_get_current_frame_number(stream->dev->udev); + } - time = uvc_video_get_time(); + sample.host_time = uvc_video_get_time(); /* * The UVC specification allows device implementations that can't obtain @@ -571,46 +582,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; @@ -619,15 +613,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; } /* @@ -2099,7 +2093,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; @@ -2248,7 +2242,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; @@ -2266,7 +2260,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; } @@ -2294,5 +2288,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); }