Received: by 2002:ab2:6857:0:b0:1ef:ffd0:ce49 with SMTP id l23csp1659318lqp; Sat, 23 Mar 2024 03:49:24 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCUHM+RMGaumDS8aaXCJMREd4+wNpj3wzpdpnp0sQwRvImoHOsvqp+a0SQf+BPXnTu3Ao6KajfNxulRq6ry7pDFeJQi6bH8F8oNxgpy/1Q== X-Google-Smtp-Source: AGHT+IEGZlym1RLxOFNbdWni8jQdaowDK5I/9Qf+RgAxp1oc1jKOc8O96diE14UPfOQBhtW4XnOU X-Received: by 2002:ad4:4eae:0:b0:696:4643:ba9f with SMTP id ed14-20020ad44eae000000b006964643ba9fmr2175478qvb.4.1711190964034; Sat, 23 Mar 2024 03:49:24 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1711190964; cv=pass; d=google.com; s=arc-20160816; b=ilfV6AyAFXcAWIddInwuVb0z4W9Gy0hP5al3/UnHAuYauvRjJHoRA3Vw8a273by5ml 1Do+fOwmSFneZndc5lR3l92iAb3JSmEwJUV+3gaeGnaXePMQLlyAWXGfnXXxWbNCI8Sx bp1j0hy29s40U8st8nxcG3z9eAJqEi0HLju9EPiICg32YiERKjHV/JRO0EVVhdmDcCYa KKwZ2Ux08tuFPrDtxH5gjvEsbfWvjYJJfXhzov/ajzpsGzUCO1US7suK7X2wab0+BaPD ZfoXgfV1zf63jyz0RYjqI3NRe06HKUuJbID8uGpKaKRuAqhygp4crzfD2B0X0T/oN13R +O+w== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:list-unsubscribe:list-subscribe:list-id:precedence :subject:date:from:dkim-signature; bh=JKe4RtJiEbXRw4y6dExCMpbk+o5eY5P3K0ygKdrnicA=; fh=LSK79HILKBvjzU0c3mbaw7dTC/AynZipemDbGvHZJsA=; b=uqovXyb7xbMKYyl7KIPXxvy46e07LwG2AXwX8oG33j0rMZAjhuuHm3KnpJbn6pnLNm zfbR8OzsIlujI9CPLiGbr45kEJWVyoQ1QBGS0Zz3W1zSojVORBer69qvLRcE5zQnhZD+ jRwuVh28LoODJbHb3wYGFz9gs1wDBsuEbcAexPWiR3Q7wTc7qwD4aTNFP1YyYPxZvP6S 9N6+qJUr5C7D1ii56JcbZlFJagzmeWSM7hxJTNULZfTcRScHGvuGzrjHIeVTrLqRvW1q F1MN/7r3nfLtKZEurwPopWFBeipvrYJpLAdGSkhgxQJnkIS3XUGOr3huqbDZXYQYbFV5 KuVw==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=WBUQZ4dc; arc=pass (i=1 spf=pass spfdomain=chromium.org dkim=pass dkdomain=chromium.org dmarc=pass fromdomain=chromium.org); spf=pass (google.com: domain of linux-kernel+bounces-112304-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-112304-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [147.75.199.223]) by mx.google.com with ESMTPS id qm19-20020a056214569300b006901ae3295bsi3751090qvb.299.2024.03.23.03.49.23 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 23 Mar 2024 03:49:24 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-112304-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) client-ip=147.75.199.223; Authentication-Results: mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=WBUQZ4dc; arc=pass (i=1 spf=pass spfdomain=chromium.org dkim=pass dkdomain=chromium.org dmarc=pass fromdomain=chromium.org); spf=pass (google.com: domain of linux-kernel+bounces-112304-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-112304-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ny.mirrors.kernel.org (Postfix) with ESMTPS id B40E21C20DCF for ; Sat, 23 Mar 2024 10:49:23 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id A62C6405C7; Sat, 23 Mar 2024 10:48:12 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=chromium.org header.i=@chromium.org header.b="WBUQZ4dc" Received: from mail-qv1-f52.google.com (mail-qv1-f52.google.com [209.85.219.52]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id A60B620B27 for ; Sat, 23 Mar 2024 10:48:09 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.219.52 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711190891; cv=none; b=u+eH5qIFNl4+7POuuFRylcmjjcpi3LntiJekxVuMZh+CbiiFYOnXJQB1zeBA8YBy/kZtqBBnF/N01eQ/EYvo4N/cJJd4iD5ILtqrw4mktDyIN9M+d+gskOWR6mKWk32Y4eN5cG4Oo1Qt0pMPoee7jb2VWhYcqx0UQEsVQIBWJOk= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711190891; c=relaxed/simple; bh=YfJmZoxIib8Yn8Py8fuuc15hVMILzEM8oygxd3mpLn8=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=f6y7tIzk5pmd3wcn/pUNAsejZmUDBMlZUPhXDVVYfuVdj+1yVNVMAtjR1r0IJzIjV3RxL1DnoLzHvsZqz4WCZ1bCPY50V09EdGlsVhaH4dBGEySXOuXx/Tt6QwTzd/ukmac6LgDoFt/N+2YbbCFWLwaW4V8MaycWUvW1VPibUBY= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=chromium.org; spf=pass smtp.mailfrom=chromium.org; dkim=pass (1024-bit key) header.d=chromium.org header.i=@chromium.org header.b=WBUQZ4dc; arc=none smtp.client-ip=209.85.219.52 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=chromium.org Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=chromium.org Received: by mail-qv1-f52.google.com with SMTP id 6a1803df08f44-696147fb5a7so27793966d6.0 for ; Sat, 23 Mar 2024 03:48:09 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1711190888; x=1711795688; darn=vger.kernel.org; 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=JKe4RtJiEbXRw4y6dExCMpbk+o5eY5P3K0ygKdrnicA=; b=WBUQZ4dcJfdwDBRS/2oIVJGwLR5kJnBU+u9JyIxhe3T32CmU9B3FXrqzD14Upx8GEt jM6Y3Zb1CgN5rQdtVPwOYnVAisjZooDmH9Aa2jV3C6VEfIqu9AeGjJ0NuqSJC3ON2agj LddDtqmMAiBq5Q590/SKJo2U2aQ7n/aTIgzII= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1711190888; x=1711795688; 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=JKe4RtJiEbXRw4y6dExCMpbk+o5eY5P3K0ygKdrnicA=; b=GdduBC4z0ze5OGARKAWWi4Ke2jY6d9MshuJkMH52CLUm/TX+jyBAx7C8ELUm90Haz9 UOEBAcV86QGMrSeCxJnMxMl6ZwIX2avRaNfhXzTjZzjYNZJHr09x0l+Ea5Kw2SZTy0BF 7QOk2cAO7C7IKClp4ReCNRSudlS25cqFu08C1/t3IivpSIYuhyQ9QA1RLFNi1R7byps+ AZLzO0MHK+KSkBAIcerBA1E0HYN10A8EeipEXOxOeBS4Fdkvczqr9PcuVNbY19VXPY5c nnKO51tX6kAP0qMr7q0SNoqzaFXheT7NDOL8DM0Sf4rxb80pd7oMbMZK76yddo4Ahvpm IDoQ== X-Forwarded-Encrypted: i=1; AJvYcCVRliE+8wnKlFXCEsu2xb3h7zlDNdg/0qVfWsxfOQGshw/5cnhpObNWZ+9SDWToCxFDDgZ4ekZ9q3n6GSaUwJVceA83SJe8+o5WqWYF X-Gm-Message-State: AOJu0YxX231XebyjOqmmc13cX6MVZ7OjJtQaXtkgq4OyjVJMhSXiWcu6 9F85SwF9yDMHmUeJ6Bs0dQPfYexYG4Te76OdMu0h4o78puQlS3qUxquHnP4yYQ== X-Received: by 2002:a05:6214:cac:b0:690:b45c:19ca with SMTP id s12-20020a0562140cac00b00690b45c19camr2124993qvs.55.1711190888621; Sat, 23 Mar 2024 03:48:08 -0700 (PDT) Received: from denia.c.googlers.com (188.173.86.34.bc.googleusercontent.com. [34.86.173.188]) by smtp.gmail.com with ESMTPSA id gd14-20020a05622a5c0e00b0042f21fe66f7sm697213qtb.73.2024.03.23.03.48.07 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 23 Mar 2024 03:48:07 -0700 (PDT) From: Ricardo Ribalda Date: Sat, 23 Mar 2024 10:48:06 +0000 Subject: [PATCH v10 5/6] media: uvcvideo: Refactor clock circular buffer Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit Message-Id: <20240323-resend-hwtimestamp-v10-5-b08e590d97c7@chromium.org> References: <20240323-resend-hwtimestamp-v10-0-b08e590d97c7@chromium.org> In-Reply-To: <20240323-resend-hwtimestamp-v10-0-b08e590d97c7@chromium.org> To: Laurent Pinchart , Mauro Carvalho Chehab Cc: linux-media@vger.kernel.org, linux-kernel@vger.kernel.org, "hn.chen" , Hans Verkuil , Ricardo Ribalda , Sergey Senozhatsky X-Mailer: b4 0.12.4 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 Reviewed-by: Laurent Pinchart Signed-off-by: Ricardo Ribalda --- drivers/media/usb/uvc/uvc_video.c | 83 +++++++++++++++++---------------------- 1 file changed, 36 insertions(+), 47 deletions(-) diff --git a/drivers/media/usb/uvc/uvc_video.c b/drivers/media/usb/uvc/uvc_video.c index af25b9f1b53fe..5df8f61d39cd1 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); + + clock->samples[clock->head] = *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 @@ -552,12 +561,10 @@ uvc_video_clock_decode(struct uvc_streaming *stream, struct uvc_buffer *buf, * suffer from this condition. */ 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; - - 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 @@ -567,9 +574,9 @@ uvc_video_clock_decode(struct uvc_streaming *stream, struct uvc_buffer *buf, * host, but the exact reason hasn't been fully determined. */ 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 @@ -592,46 +599,28 @@ 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; - - if (stream->clock.count < stream->clock.size) - stream->clock.count++; - - spin_unlock_irqrestore(&stream->clock.lock, flags); + sample.dev_sof = (sample.dev_sof + stream->clock.sof_offset) & 2047; + uvc_video_clock_add_sample(&stream->clock, &sample); + stream->clock.last_sof = sample.dev_sof; } -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; @@ -640,15 +629,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; } /* @@ -2123,7 +2112,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; @@ -2272,7 +2261,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; @@ -2290,7 +2279,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; } @@ -2318,5 +2307,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); } -- 2.44.0.396.g6e790dbe36-goog