From patchwork Tue Apr 9 21:24:57 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Grzeschik X-Patchwork-Id: 787406 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 3C324156F4F for ; Tue, 9 Apr 2024 21:25:02 +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=1712697905; cv=none; b=L+t25Dfp6VKxXlphX2kW0n9FqXZd93bPn14q7keGAIMSMJe/2PihYZe2Wkcgi1zv0yfPOQB8a5Qlp06tat/iOHlDjbQr7iqmhrG2fJk6WLishzBXB17680njLrLz5drViIFE3ZEyhR9kFTj1H6inIifCPw1pdXf/09avhl02rCM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712697905; c=relaxed/simple; bh=2S0XiBQEhafQJvyQNhvCXNW0prPzuhGVKt39M7Kdgt8=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=cMPTCcMbnHaY5Z77sOce2gdnXW8XZBb2clNJACd5dW84cFOk6fFdp+i3GepwvHR3v0pOH3Ci8NgQcF9ZXQOsyu1xYwTpD+Z0Q6oSJapL2L58j6Dx329eyuoNVbUCARzQiHzZFwkfdb5diywRhmfeGC0yJpPuOWmGsw5ar8jrTLs= 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 1ruIxf-00081Z-FR; Tue, 09 Apr 2024 23:24:59 +0200 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 1ruIxe-00BNAe-Ql; Tue, 09 Apr 2024 23:24:58 +0200 Received: from localhost ([::1] helo=dude04.red.stw.pengutronix.de) by dude04.red.stw.pengutronix.de with esmtp (Exim 4.96) (envelope-from ) id 1ruIxe-00FQKu-2S; Tue, 09 Apr 2024 23:24:58 +0200 From: Michael Grzeschik Date: Tue, 09 Apr 2024 23:24:57 +0200 Subject: [PATCH 1/3] usb: gadget: function: uvc: set req_size once when the vb2 queue is calculated Precedence: bulk X-Mailing-List: linux-usb@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Message-Id: <20240403-uvc_request_length_by_interval-v1-1-9436c4716233@pengutronix.de> References: <20240403-uvc_request_length_by_interval-v1-0-9436c4716233@pengutronix.de> In-Reply-To: <20240403-uvc_request_length_by_interval-v1-0-9436c4716233@pengutronix.de> To: Laurent Pinchart , Daniel Scally , Greg Kroah-Hartman Cc: linux-usb@vger.kernel.org, linux-kernel@vger.kernel.org, Michael Grzeschik X-Mailer: b4 0.12.0 X-Developer-Signature: v=1; a=openpgp-sha256; l=3217; i=m.grzeschik@pengutronix.de; h=from:subject:message-id; bh=2S0XiBQEhafQJvyQNhvCXNW0prPzuhGVKt39M7Kdgt8=; b=owEBbQKS/ZANAwAKAb9pWET5cfSrAcsmYgBmFbIpOc5dtFS8oO3jYgMNeAwhpXTaXzy+azwUR 0LEiPF+Ip+JAjMEAAEKAB0WIQQV2+2Fpbqd6fvv0Gi/aVhE+XH0qwUCZhWyKQAKCRC/aVhE+XH0 q34aEACSSxF5mlMrKFC0Mz4nzFDFlYAVM6+IaAJ32qK+4JnAwjE70xb/gK4nV8XIKOVDtqJxQzW vY71kj+3DFa7l1++eaBv6mj29MEsyPwQo4+QicvGNeZ2EbfYrbGFo4Gk97XafiH8Mj052G3zjhl O7Gc5f/hZLcYvXME6T/Pp7tattQtwEUyXdRUMghzx+cxzV+1F4+MrunGi4uSsItNLXS+SyjKdk/ 0AQ7LLwsY5VsH8RhVoU5abOuIkO5YfS6rWx7nMk/kivqQzQYUOQqNY96qBwv8sBV4Eg3mpv9Ecq TRpxYI4y70iQJDDsr16vR81baiLreRRdhB+4z1Pw8W+/p/D/oq6P7YorWkINy7CPm6BSXNVGxUa JojR1SAs7haxChThgtUpVeHa0cf5gJ1RuQdB+NdLA/KwSE7jeR0ul9t+4Yb8rbWDZZ4rvQB34GS SBnakJiM/v8W2IompOADngh748KUok4jxsaLbDGKL0Ruz3ExVFelQ0eowWZqJocIjJk7iE88XFH CmPlWTef4Q5XHOqXrNsp7zTkre/TFdBVlAI+cmYBF4pcm4mSIPUX01ELVlrtoyPcFU7qAZPRTEI gYzP2qFpbwpWE7N6Wgz0cvd/4ihedOt+ri+yEWE5ntyvQYqWMqMZIrifsNxK9Fq0tv+fi0OYI9b WoYiLKSCsIv5HFg== 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-usb@vger.kernel.org The uvc gadget driver is calculating the req_size on every video_enable/alloc_request and is based on the fixed configfs parameters maxpacket, maxburst and mult. As those parameters can not be changed once the gadget is started and the same calculation is done already early on the vb2_streamon/queue_setup path its save to remove one extra calculation and reuse the calculation from uvc_queue_setup for the allocation step. Signed-off-by: Michael Grzeschik --- Link to v1: https://lore.kernel.org/r/20240404-uvc_request_calc_once-v1-1-fea7fd8f0496@pengutronix.de --- drivers/usb/gadget/function/uvc_queue.c | 2 ++ drivers/usb/gadget/function/uvc_video.c | 15 ++------------- 2 files changed, 4 insertions(+), 13 deletions(-) diff --git a/drivers/usb/gadget/function/uvc_queue.c b/drivers/usb/gadget/function/uvc_queue.c index 0aa3d7e1f3cc3..ce51643fc4639 100644 --- a/drivers/usb/gadget/function/uvc_queue.c +++ b/drivers/usb/gadget/function/uvc_queue.c @@ -63,6 +63,8 @@ static int uvc_queue_setup(struct vb2_queue *vq, */ nreq = DIV_ROUND_UP(DIV_ROUND_UP(sizes[0], 2), req_size); nreq = clamp(nreq, 4U, 64U); + + video->req_size = req_size; video->uvc_num_requests = nreq; return 0; diff --git a/drivers/usb/gadget/function/uvc_video.c b/drivers/usb/gadget/function/uvc_video.c index d41f5f31dadd5..95bb64e16f3da 100644 --- a/drivers/usb/gadget/function/uvc_video.c +++ b/drivers/usb/gadget/function/uvc_video.c @@ -496,7 +496,6 @@ uvc_video_free_requests(struct uvc_video *video) INIT_LIST_HEAD(&video->ureqs); INIT_LIST_HEAD(&video->req_free); INIT_LIST_HEAD(&video->req_ready); - video->req_size = 0; return 0; } @@ -504,16 +503,9 @@ static int uvc_video_alloc_requests(struct uvc_video *video) { struct uvc_request *ureq; - unsigned int req_size; unsigned int i; int ret = -ENOMEM; - BUG_ON(video->req_size); - - req_size = video->ep->maxpacket - * max_t(unsigned int, video->ep->maxburst, 1) - * (video->ep->mult); - for (i = 0; i < video->uvc_num_requests; i++) { ureq = kzalloc(sizeof(struct uvc_request), GFP_KERNEL); if (ureq == NULL) @@ -523,7 +515,7 @@ uvc_video_alloc_requests(struct uvc_video *video) list_add_tail(&ureq->list, &video->ureqs); - ureq->req_buffer = kmalloc(req_size, GFP_KERNEL); + ureq->req_buffer = kmalloc(video->req_size, GFP_KERNEL); if (ureq->req_buffer == NULL) goto error; @@ -541,12 +533,10 @@ uvc_video_alloc_requests(struct uvc_video *video) list_add_tail(&ureq->req->list, &video->req_free); /* req_size/PAGE_SIZE + 1 for overruns and + 1 for header */ sg_alloc_table(&ureq->sgt, - DIV_ROUND_UP(req_size - UVCG_REQUEST_HEADER_LEN, + DIV_ROUND_UP(video->req_size - UVCG_REQUEST_HEADER_LEN, PAGE_SIZE) + 2, GFP_KERNEL); } - video->req_size = req_size; - return 0; error: @@ -699,7 +689,6 @@ uvcg_video_disable(struct uvc_video *video) INIT_LIST_HEAD(&video->ureqs); INIT_LIST_HEAD(&video->req_free); INIT_LIST_HEAD(&video->req_ready); - video->req_size = 0; spin_unlock_irqrestore(&video->req_lock, flags); /* From patchwork Tue Apr 9 21:24:58 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Grzeschik X-Patchwork-Id: 787405 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 3C3E7158878 for ; Tue, 9 Apr 2024 21:25:02 +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=1712697906; cv=none; b=geFQgKl3uJcqVfQYJ5Dqqk4pcMlrWTA/uxgHncWqEvunzeZNyO1k6zIDmBEFSprlXj476i2ZG/cSUr9RTpVEmgXkUqaN5WusKjysgXk9vgIjd03qxqi/No52XP0tSm+qsXLXXZy8qEusYOO1hWn3UouNk6wAEAaduH75GC233pM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712697906; c=relaxed/simple; bh=HiHvbLiNKh+4kFHZN7IDQemdpmrFlcoc0ZTzObRXs3M=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=qNvREF2aQW7ltOwmUof16ZREKVgc17003uur82YZiauu6HibgVcOE4WFzTEWuIvMWTUv0HpNUHexRPirrx6GL/ObYYynm2Tr33eJ++Nbl3Y/i3e5cj7x1SE2FqLUkBJ2lB1YLFUvyGw9B0xC7EJt04iQVkwqfyOybYvZo8WjsLI= 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 1ruIxf-00081Y-FR; Tue, 09 Apr 2024 23:24:59 +0200 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 1ruIxe-00BNAd-Qr; Tue, 09 Apr 2024 23:24:58 +0200 Received: from localhost ([::1] helo=dude04.red.stw.pengutronix.de) by dude04.red.stw.pengutronix.de with esmtp (Exim 4.96) (envelope-from ) id 1ruIxe-00FQKu-2T; Tue, 09 Apr 2024 23:24:58 +0200 From: Michael Grzeschik Date: Tue, 09 Apr 2024 23:24:58 +0200 Subject: [PATCH 2/3] usb: gadget: uvc: add g_parm and s_parm for frame interval Precedence: bulk X-Mailing-List: linux-usb@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Message-Id: <20240403-uvc_request_length_by_interval-v1-2-9436c4716233@pengutronix.de> References: <20240403-uvc_request_length_by_interval-v1-0-9436c4716233@pengutronix.de> In-Reply-To: <20240403-uvc_request_length_by_interval-v1-0-9436c4716233@pengutronix.de> To: Laurent Pinchart , Daniel Scally , Greg Kroah-Hartman Cc: linux-usb@vger.kernel.org, linux-kernel@vger.kernel.org, Michael Grzeschik X-Mailer: b4 0.12.0 X-Developer-Signature: v=1; a=openpgp-sha256; l=3274; i=m.grzeschik@pengutronix.de; h=from:subject:message-id; bh=HiHvbLiNKh+4kFHZN7IDQemdpmrFlcoc0ZTzObRXs3M=; b=owEBbQKS/ZANAwAKAb9pWET5cfSrAcsmYgBmFbIqyMzAMSvamjZxvQctnqi69TxA/R68dMTpZ Ob1D/TLwtWJAjMEAAEKAB0WIQQV2+2Fpbqd6fvv0Gi/aVhE+XH0qwUCZhWyKgAKCRC/aVhE+XH0 q2nDD/92L9Hl0OnAyJCoyDXNFLh4JqhW2wTkHzR89YirJ43XbbiQXMNl91Vby7KOYECzreFtGLv VvcvQBYyNtmbyh6SkV65oczfEdY0W429frz+tl+geW6t+i3XEt8ECS/u8pWDzn/mrRQbi0ho/5q mDG8buvfo+cdi/qGLpVvBuBn8wf2BuObvLCRGJt4n+K4c53VZtUB+2WGRLo30iAxJg9hh98Z59Q jveU+gv7p7qmQh0h9ja6CX1gXlS6JW8AuGtooGAGNrCL68WsIWUYJwmh85EPe9T+n/O8ErQRIPv p82ZWBpLQ0EjXSaQt0iYwV9MoJ2ef+8lPAPdoIQm4LQupGUnsdFYPvbKE83pF6tB6JwmhLgi+Bb L2mafVXl2jkrKuWy4lFfcg9WAx/ZrA0xs4HerHDj+vSYMnw2mb/u1C49Xf4DPX6tM7IsDxfNsXv Gek14AMU9310+G9AbVtT1NdJ+0FbkUBZ+L0Mj5rPQVR3NrAYZdNVUkXcZkHniADCUobpwBKR65S /LLDAJYz9n9SkQdDfUeZQwW6vJO9unC020J7Ssnh4Qf9K7sGWE5RJsHQNedjrSAjSDPkPuzUDef YvMNdZImMXKmZOuIIWhC2ERKKS0eunMrF8s6AQASln9C7D9Mw0opUit3XpFv9w1FaBIgBMNQVtv /fIDcNTL+1MCU6w== 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-usb@vger.kernel.org The uvc gadget driver is lacking the information which frame interval was set by the host. We add this information by implementing the g_parm and s_parm callbacks. Signed-off-by: Michael Grzeschik --- drivers/usb/gadget/function/uvc.h | 1 + drivers/usb/gadget/function/uvc_v4l2.c | 52 ++++++++++++++++++++++++++++++++++ 2 files changed, 53 insertions(+) diff --git a/drivers/usb/gadget/function/uvc.h b/drivers/usb/gadget/function/uvc.h index cb35687b11e7e..d153bd9e35e31 100644 --- a/drivers/usb/gadget/function/uvc.h +++ b/drivers/usb/gadget/function/uvc.h @@ -97,6 +97,7 @@ struct uvc_video { unsigned int width; unsigned int height; unsigned int imagesize; + unsigned int interval; struct mutex mutex; /* protects frame parameters */ unsigned int uvc_num_requests; diff --git a/drivers/usb/gadget/function/uvc_v4l2.c b/drivers/usb/gadget/function/uvc_v4l2.c index a024aecb76dc3..5b579ec1f5040 100644 --- a/drivers/usb/gadget/function/uvc_v4l2.c +++ b/drivers/usb/gadget/function/uvc_v4l2.c @@ -307,6 +307,56 @@ uvc_v4l2_set_format(struct file *file, void *fh, struct v4l2_format *fmt) return ret; } +static int uvc_v4l2_g_parm(struct file *file, void *fh, + struct v4l2_streamparm *parm) +{ + struct video_device *vdev = video_devdata(file); + struct uvc_device *uvc = video_get_drvdata(vdev); + struct uvc_video *video = &uvc->video; + struct v4l2_fract timeperframe; + + if (parm->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) + return -EINVAL; + + /* Return the actual frame period. */ + timeperframe.numerator = video->interval; + timeperframe.denominator = 10000000; + v4l2_simplify_fraction(&timeperframe.numerator, + &timeperframe.denominator, 8, 333); + + uvcg_dbg(&uvc->func, "Getting frame interval of %u/%u (%u)\n", + timeperframe.numerator, timeperframe.denominator, + video->interval); + + parm->parm.output.timeperframe = timeperframe; + parm->parm.output.capability = V4L2_CAP_TIMEPERFRAME; + + return 0; +} + +static int uvc_v4l2_s_parm(struct file *file, void *fh, + struct v4l2_streamparm *parm) +{ + struct video_device *vdev = video_devdata(file); + struct uvc_device *uvc = video_get_drvdata(vdev); + struct uvc_video *video = &uvc->video; + struct v4l2_fract timeperframe; + + if (parm->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) + return -EINVAL; + + timeperframe = parm->parm.output.timeperframe; + + video->interval = v4l2_fraction_to_interval(timeperframe.numerator, + timeperframe.denominator); + + uvcg_dbg(&uvc->func, "Setting frame interval to %u/%u (%u)\n", + timeperframe.numerator, timeperframe.denominator, + video->interval); + + return 0; +} + static int uvc_v4l2_enum_frameintervals(struct file *file, void *fh, struct v4l2_frmivalenum *fival) @@ -577,6 +627,8 @@ const struct v4l2_ioctl_ops uvc_v4l2_ioctl_ops = { .vidioc_dqbuf = uvc_v4l2_dqbuf, .vidioc_streamon = uvc_v4l2_streamon, .vidioc_streamoff = uvc_v4l2_streamoff, + .vidioc_s_parm = uvc_v4l2_s_parm, + .vidioc_g_parm = uvc_v4l2_g_parm, .vidioc_subscribe_event = uvc_v4l2_subscribe_event, .vidioc_unsubscribe_event = uvc_v4l2_unsubscribe_event, .vidioc_default = uvc_v4l2_ioctl_default,