Received: by 2002:a05:7412:798b:b0:fc:a2b0:25d7 with SMTP id fb11csp3160rdb; Wed, 21 Feb 2024 14:52:07 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCUrrWRDWu3twLyiGu7mEatG/be6CzOUDaYNXRkaCpOxrN9QpyPGNvPZoyyk0yEIrfi/lgO7UQG522zos9myxl3t9T8cgLO9l4HlOvAyeg== X-Google-Smtp-Source: AGHT+IG58js0FNcHLARSvbRkqhJdtmEyVYhaZFuysos6I1lat/orsW5lDnfhYlOHkCcQ/Y++Fomo X-Received: by 2002:a05:622a:190:b0:42d:b3bd:2222 with SMTP id s16-20020a05622a019000b0042db3bd2222mr23168615qtw.24.1708555927147; Wed, 21 Feb 2024 14:52:07 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1708555927; cv=pass; d=google.com; s=arc-20160816; b=FhFfUQqmLA7mBsXuzAQc9f9Ttld74THyERB0NNx9Ag//Yma2tIIRh8iu6TExez6xIl 5Cgn2+NX3+OSPmos+ZK+oTiD5hAeIznC8YRESxOYOjv7x/hUeSED2dEh0/6BFlvOePPd LfadUIwTGre3zk2dXwQdipwTJzIjavPfCBb+PSKb7kg8uW97JKl51zi2j01YLB+xtrK0 Nev3ektirc5pqwamI2Sp31esZapXcbobXYQow51bB5jvWmsxLeB+xYkOWmBYQU17TP/x 5CifHjPZuYMUZWXheR8hql6e+WB1g344mNoyRfb/SYv4JOr7bEKKUgU1p8hyofvMX+t+ zA9w== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=cc:to:message-id:content-transfer-encoding:mime-version :list-unsubscribe:list-subscribe:list-id:precedence:subject:date :from; bh=xQ+YnFjOlNt+8fCBtVb0Df9cZXO0hsYXV3G0LLdV2JE=; fh=5Kv30HdDedIeQn1hli3jxyQe7yiYjyyW8R3deSEaxQ8=; b=i5lU74OQfz50G64Lt/4jwEBSVF/HhkvoEwNrjdTnctaFAloU40rtBAgD0slgC+Gz8M JoBuZW6bRtxIjsvTwr+nt0ZJ0gFvnXbI+UrQm+HBfjoQ9cgTVr3so9KGlPSoKADTRwkK uZPB9+J8nYm5TxUzkcMltzL4ZfgJkVXdfSeBr17Ng9RxQWs689UmuCSPpmLTlPMtq+Qa 9Frd/pfnWzg8FZovRU89gMaGbCC066ft8gLPP98LtDboNke6VYizHVbfn60e14YqWoXS 23Eh3uwK3SI0Uc/VtfK7oBi890c7vZfS24KS/EGrhGppcMaIclyCltuB6SWqRiKqwtLD QQfg==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; arc=pass (i=1 spf=pass spfdomain=pengutronix.de); spf=pass (google.com: domain of linux-kernel+bounces-75618-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-75618-linux.lists.archive=gmail.com@vger.kernel.org" Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [2604:1380:45d1:ec00::1]) by mx.google.com with ESMTPS id d10-20020ac8544a000000b0042c28da72e2si417776qtq.341.2024.02.21.14.52.07 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 21 Feb 2024 14:52:07 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-75618-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) client-ip=2604:1380:45d1:ec00::1; Authentication-Results: mx.google.com; arc=pass (i=1 spf=pass spfdomain=pengutronix.de); spf=pass (google.com: domain of linux-kernel+bounces-75618-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-75618-linux.lists.archive=gmail.com@vger.kernel.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 D30021C22B57 for ; Wed, 21 Feb 2024 22:52:06 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 2116B811E7; Wed, 21 Feb 2024 22:52:02 +0000 (UTC) Received: from metis.whiteo.stw.pengutronix.de (metis.whiteo.stw.pengutronix.de [185.203.201.7]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id D0D5E3A29E for ; Wed, 21 Feb 2024 22:51:58 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=185.203.201.7 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708555921; cv=none; b=QB/wGwqOzL3BS9/aEL+Mwt2cjYuk/E0+OijUcY82l/NY8yEXO85HwFtFyMSJP6qcvru8/Wv0rBHamGiqH8ud3YxQoueS05HAoRwGYN0RnduZLaHYq6z6zOrZasSuywZg0NP85UVBsLfG64YSYRHhbFzhK6d7LKEaVhKgMh4r4sA= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708555921; c=relaxed/simple; bh=R/JHQ1lrEpJPhxyrIeQctCznqCSCvFwGCgMiq98FyQo=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:To:Cc; b=Ij9y5m/g4MDdLCbhOKpkQFCuyy4nZs1MaHNWnltYOwvQgNTMS++LZQPX3Jw55ABVN8rglVetLnb1NGNlexTRWnLN6QtYN9fjxkm48/jiMFNVvk4RfD7gE5C+89rw+++iIox932ejaOfEv8oGEZJn5JcmaoCBudPzm3BrtT2FuFc= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=pengutronix.de; spf=pass smtp.mailfrom=pengutronix.de; arc=none smtp.client-ip=185.203.201.7 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=pengutronix.de Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=pengutronix.de Received: from drehscheibe.grey.stw.pengutronix.de ([2a0a:edc0:0:c01:1d::a2]) by metis.whiteo.stw.pengutronix.de with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1rcvRU-0006yg-FQ; Wed, 21 Feb 2024 23:51:56 +0100 Received: from [2a0a:edc0:0:1101:1d::ac] (helo=dude04.red.stw.pengutronix.de) by drehscheibe.grey.stw.pengutronix.de with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1rcvRU-0027V8-0C; Wed, 21 Feb 2024 23:51:56 +0100 Received: from localhost ([::1] helo=dude04.red.stw.pengutronix.de) by dude04.red.stw.pengutronix.de with esmtp (Exim 4.96) (envelope-from ) id 1rcvRT-00A5aE-33; Wed, 21 Feb 2024 23:51:55 +0100 From: Michael Grzeschik Date: Wed, 21 Feb 2024 23:51:49 +0100 Subject: [PATCH] uvc_video: check for fid change early in decode_start and avoid wrong error counting 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: <20240221-uvc-host-video-decode-start-v1-1-228995925c70@pengutronix.de> X-B4-Tracking: v=1; b=H4sIAIR+1mUC/x2NMQrDMAwAvxI0V5C4ydB+pXRwJLkWBLtYjgmE/ L2m491wd4JJUTF4DicUaWqaU4fpNgBFnz6Cyp3BjW4enZtwb4QxW8WmLBlZKLOgVV8q+rDcOSw PNxNDL6zeBNfiE8XeSPu2dfktEvT4L1/v6/oBGDngMoIAAAA= To: Laurent Pinchart , Mauro Carvalho Chehab Cc: linux-media@vger.kernel.org, linux-kernel@vger.kernel.org, kernel@pengutronix.de, Michael Grzeschik X-Mailer: b4 0.12.0 X-Developer-Signature: v=1; a=openpgp-sha256; l=4304; i=m.grzeschik@pengutronix.de; h=from:subject:message-id; bh=R/JHQ1lrEpJPhxyrIeQctCznqCSCvFwGCgMiq98FyQo=; b=owEBbQKS/ZANAwAKAb9pWET5cfSrAcsmYgBl1n6F23BMm2PAm3HqB/7FdHZGPkL2gcZvuQgF9 YzxBri35zKJAjMEAAEKAB0WIQQV2+2Fpbqd6fvv0Gi/aVhE+XH0qwUCZdZ+hQAKCRC/aVhE+XH0 qxjwEACBbnUOZymdfOIo6PGQrjBiQ2M35j16DH6zwUC4TEjun5YIjpH8obnvZqP/L+fSbibfk5g xL8pJeY2y7a51tzZYcOFfF4tIHOfeA7jEJ9OjrkZHrvLXUx4lUUzZozwrFBBwayTDYrogjBcRUs PMv8QXCgYQqF/MntFTFCZL6Ob8jCsc5lhX8FoTDaSj1xwrORab+pgR5YWbsgJiDwJosnDH0xsrf v14WDS2lVv5Uxa0cIkJcvu+ZKgfHbNQTL/zWjNyLFZ9O+L04WKasT8tiIhyYn/68DYKlMZrPuto UaHw0lvpaZVVHp1sd1OSWdM5ISDHRw/Nt3rgC0ApcS7tn+Ez99msqv/Zyw5CbULO6zADeRlnSb8 cNSc2+QLwdDnD4YwgDERlr7JXzlqd9iq0FtY+JGoLQXMoOL0mYJW2WJBynOA5oucqOJoep02ETH VPY3U5rLXlhuU5/0Fh8AfkdfYWJ+8PX/IVLGMYw4ZWQNCvxRjhEpUbq+YOX2fx8pBcoBaPPq0m4 Azw2hTk8cH+xZYkYQAskAqSKnTyA2cBuv9kL0rT9z53/T3BAiSTmh5w3dxO+lNkn8Yot+0UUqQS QVZTWpfUIgzXmjE8sT2Ka5Sox/gUuEBiwdbg/g+BekxHUscIMKmNxDoMfeyFq7/AyPeyLklW0Wo UzU5zFsTswTNi7Q== X-Developer-Key: i=m.grzeschik@pengutronix.de; a=openpgp; fpr=957BC452CE953D7EA60CF4FC0BE9E3157A1E2C64 X-SA-Exim-Connect-IP: 2a0a:edc0:0:c01:1d::a2 X-SA-Exim-Mail-From: m.grzeschik@pengutronix.de X-SA-Exim-Scanned: No (on metis.whiteo.stw.pengutronix.de); SAEximRunCond expanded to false X-PTX-Original-Recipient: linux-kernel@vger.kernel.org When the uvc request will get parsed by uvc_video_decode_start it will leave the function with -EAGAIN to be restarted on the next frame. While the first wrong parse the statistics will already be updated with uvc_video_stats_decode. One value e.g. is the error_count, which therefor will be incremented twice in case the fid has changed on the way. This patch fixes the unnecessary extra parsing by returning early from the function when the fid has changed. Signed-off-by: Michael Grzeschik --- drivers/media/usb/uvc/uvc_video.c | 64 +++++++++++++++++++-------------------- 1 file changed, 32 insertions(+), 32 deletions(-) diff --git a/drivers/media/usb/uvc/uvc_video.c b/drivers/media/usb/uvc/uvc_video.c index 7cbf4692bd875..fce5349b5f9fa 100644 --- a/drivers/media/usb/uvc/uvc_video.c +++ b/drivers/media/usb/uvc/uvc_video.c @@ -1068,11 +1068,43 @@ static int uvc_video_decode_start(struct uvc_streaming *stream, fid = data[1] & UVC_STREAM_FID; + /* + * Store the payload FID bit and return immediately when the buffer is + * NULL. + */ + if (buf == NULL) { + stream->last_fid = fid; + return -ENODATA; + } + /* * Increase the sequence number regardless of any buffer states, so * that discontinuous sequence numbers always indicate lost frames. */ if (stream->last_fid != fid) { + /* + * Mark the buffer as done if we're at the beginning of a new frame. + * End of frame detection is better implemented by checking the EOF + * bit (FID bit toggling is delayed by one frame compared to the EOF + * bit), but some devices don't set the bit at end of frame (and the + * last payload can be lost anyway). We thus must check if the FID has + * been toggled. + * + * stream->last_fid is initialized to -1, so the first isochronous + * frame will never trigger an end of frame detection. + * + * Empty buffers (bytesused == 0) don't trigger end of frame detection + * as it doesn't make sense to return an empty buffer. This also + * avoids detecting end of frame conditions at FID toggling if the + * previous payload had the EOF bit set. + */ + if (buf->bytesused) { + uvc_dbg(stream->dev, FRAME, + "Frame complete (FID bit toggled)\n"); + buf->state = UVC_BUF_STATE_READY; + return -EAGAIN; + } + stream->sequence++; if (stream->sequence) uvc_video_stats_update(stream); @@ -1081,15 +1113,6 @@ static int uvc_video_decode_start(struct uvc_streaming *stream, uvc_video_clock_decode(stream, buf, data, len); uvc_video_stats_decode(stream, data, len); - /* - * Store the payload FID bit and return immediately when the buffer is - * NULL. - */ - if (buf == NULL) { - stream->last_fid = fid; - return -ENODATA; - } - /* Mark the buffer as bad if the error bit is set. */ if (data[1] & UVC_STREAM_ERR) { uvc_dbg(stream->dev, FRAME, @@ -1124,29 +1147,6 @@ static int uvc_video_decode_start(struct uvc_streaming *stream, buf->state = UVC_BUF_STATE_ACTIVE; } - /* - * Mark the buffer as done if we're at the beginning of a new frame. - * End of frame detection is better implemented by checking the EOF - * bit (FID bit toggling is delayed by one frame compared to the EOF - * bit), but some devices don't set the bit at end of frame (and the - * last payload can be lost anyway). We thus must check if the FID has - * been toggled. - * - * stream->last_fid is initialized to -1, so the first isochronous - * frame will never trigger an end of frame detection. - * - * Empty buffers (bytesused == 0) don't trigger end of frame detection - * as it doesn't make sense to return an empty buffer. This also - * avoids detecting end of frame conditions at FID toggling if the - * previous payload had the EOF bit set. - */ - if (fid != stream->last_fid && buf->bytesused != 0) { - uvc_dbg(stream->dev, FRAME, - "Frame complete (FID bit toggled)\n"); - buf->state = UVC_BUF_STATE_READY; - return -EAGAIN; - } - stream->last_fid = fid; return data[0]; --- base-commit: 3bf0514dc6f36f81ee11b1becd977cb87b4c90c6 change-id: 20240221-uvc-host-video-decode-start-af53df5924cd Best regards, -- Michael Grzeschik