From patchwork Sat Jun 22 23:48:29 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Grzeschik X-Patchwork-Id: 806888 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 02AD34C8A for ; Sat, 22 Jun 2024 23:48:42 +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=1719100126; cv=none; b=dpwSSVIsBqwdFt2/DatmmW/dXS78njmRpqAtD4xdL/fj3ePszE6TbPcUFp7viTi1oZ4i+RhZ7srJacmo3i2huN0A2Q1CtJAoGXUNSgB23yxNYobaIrUs6Tuue8KQh9bplptMpjJzlpi5nw/J3G1bmgQKnDmkCuKqqp68C3szJiM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1719100126; c=relaxed/simple; bh=zvzna6niCULIOsf1krFO6xX5xTvSU+BBkHM8O4pKRro=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=nsPnlRNfVyK5B5O+eVPCC9efNPkB/Tt1pgOGNluef+xT9QxR1vgdmrnGybG/nJQDco5Xzxdej+RxZOL8pw8lVok2NipxOhRN5rAVIeGoMcanG9pie3AG/4w67bOeivhA9n5DrR0N9G6qe5wk8MFfZxYt47uQmFkTXeASACEePQM= 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 1sLATD-0002cf-5F; Sun, 23 Jun 2024 01:48:35 +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 1sLATC-004Hkc-Gv; Sun, 23 Jun 2024 01:48:34 +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 1sLATC-000IWx-35; Sun, 23 Jun 2024 01:48:34 +0200 From: Michael Grzeschik Date: Sun, 23 Jun 2024 01:48:29 +0200 Subject: [PATCH v2 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-v2-1-12690f7a2eff@pengutronix.de> References: <20240403-uvc_request_length_by_interval-v2-0-12690f7a2eff@pengutronix.de> In-Reply-To: <20240403-uvc_request_length_by_interval-v2-0-12690f7a2eff@pengutronix.de> To: Laurent Pinchart , Daniel Scally , Greg Kroah-Hartman , Avichal Rakesh 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=3232; i=m.grzeschik@pengutronix.de; h=from:subject:message-id; bh=zvzna6niCULIOsf1krFO6xX5xTvSU+BBkHM8O4pKRro=; b=owEBbQKS/ZANAwAKAb9pWET5cfSrAcsmYgBmd2LRzov2p04A9MUzQour0vyAA+sYkI5LzSBaW 2zEThFiKvqJAjMEAAEKAB0WIQQV2+2Fpbqd6fvv0Gi/aVhE+XH0qwUCZndi0QAKCRC/aVhE+XH0 q3AID/4oWCCa5ep7GqbbfBiSNExY4njozZKlHwyji4J+P1MtmqLp1B/KNR7HefOKF+0yvVhggqs bfcKpDH9NpTldVJAMAXS4T4a47JgNjhHYUHgBwQ1jhdw0qsMsuEta/SWaLwE5TxOCCGTintLIz+ PxJ60SBNej+m3oxgfQbwX6czlKv75aLk2UQ572IezV2XnubTLhcBwphgkKrG2GSj6BdOfGo5Pzi TC6qYAIHEgMtS5CV2A+cw1Ht+q3W1YNC+xkRKZMPbcc3zQ0yZkmfYG14Zh6P0Dl64X6Y9TPHmhl dDe0f/X+4FLbRht0BNapul1+RfIB/ZsiLNYrblZqPUewvQsMCR6+YoAU0sQgrpckcPeLS4+s0ca KMsd9UvTzRU84bvUE8611PN885B/FCBbrHhA6lgiPDZgTNgvpp2/kEb0e7J2ICk2gRjJDGQHMZQ T0Y2TUNXi1YaECQMbNDA3rfX7HvnDwh3vOGX8hLnmGIv4vaHXEDP2NKXSQ8cq3uW01/PdoWp5uy bYIhLLTiikGqMZl/RKkVwtQJQnaHSAjdZ5mJAEcOpkPZDbvarPPALZ0uamfW5E+G+jwS7+NCS0J vp7jC0kDgt1pWW2dXSBIIP80oTlXUD6C3omCibURyAD9F/YVhCRKLhfEQugOQu2HysVTiXGVfRY +OgLsrgx4yv+8TA== 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 v1 -> v2: - --- 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 Sat Jun 22 23:48:30 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Grzeschik X-Patchwork-Id: 806889 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 02BA112AAD6 for ; Sat, 22 Jun 2024 23:48:42 +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=1719100126; cv=none; b=m5aRZ+SwaEEdWxsU38afPuunuAcYSM1iyKsUIARMQj1V7rFOz9csDUUgJEDcB1jE/cUGExQMHZPAp2cDlHalSsl8YO+5aPUDPFEUV342FU7hWHAKBc8YwppU8r/On2eIMj5ZKYaM4bRp2Aq/7Snkt/XnMOmA1kIsTmvVLtgeQoM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1719100126; c=relaxed/simple; bh=LBZuX0Ga+lPp0ZoDeMqKbgnd7nk7N/KSSAGb3NNcxPs=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=QEl6gZuWkXjla/FwDorgQDbJFnQIlS8etU61M/Mwz7fLkv+JftNKKD+wNe/a6c65AwuirhE56OYwEHs46tQ5zbo+J6nr45BAFMjkGbCm68HrQmozViCxjYlo3QwyxT/+08WBeKmtvJz5DXwCFFhlt5VGb+0WuBvWk44Cqk6loSA= 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 1sLATD-0002ch-5F; Sun, 23 Jun 2024 01:48:35 +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 1sLATC-004Hkd-Hh; Sun, 23 Jun 2024 01:48:34 +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 1sLATC-000IWx-36; Sun, 23 Jun 2024 01:48:34 +0200 From: Michael Grzeschik Date: Sun, 23 Jun 2024 01:48:30 +0200 Subject: [PATCH v2 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-v2-2-12690f7a2eff@pengutronix.de> References: <20240403-uvc_request_length_by_interval-v2-0-12690f7a2eff@pengutronix.de> In-Reply-To: <20240403-uvc_request_length_by_interval-v2-0-12690f7a2eff@pengutronix.de> To: Laurent Pinchart , Daniel Scally , Greg Kroah-Hartman , Avichal Rakesh 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=3294; i=m.grzeschik@pengutronix.de; h=from:subject:message-id; bh=LBZuX0Ga+lPp0ZoDeMqKbgnd7nk7N/KSSAGb3NNcxPs=; b=owEBbQKS/ZANAwAKAb9pWET5cfSrAcsmYgBmd2LRwyaHGG9bQWJsOYA3ksoti8y/7qKzFpIme VuhGLRug6CJAjMEAAEKAB0WIQQV2+2Fpbqd6fvv0Gi/aVhE+XH0qwUCZndi0QAKCRC/aVhE+XH0 q+PaD/0c1flE0NngA6ghPiONXKDLdXKXZyYl9SfjUOsHfkmcQXG06sQHL6fx31YFTmqJ6N1Vi6j QGZuZMGwjn1I9/tG3aT68zkqr6tiD5R/S0Re5jiPS4vsNMmgnfrBe0HVss8r2AILDwr476u9mdu N1NjG6Bir5LEQmghWi5bM/LOatCaak4f8VsWmgGZqveBG5/kObwRMVg8NZ0w7lNt7uEP0HS1+pS xAKGMrZVXeBZLxLQL4heSqxUa9l8nKMBzY+ToVKDXL9uCnKmLnkswCuMFNemK+U2Cs0MvHQVB2a 6jcwkGMyafDcXui+C+MpzPps5aottIX3ScpVzdjU5/zAmIIBN6cu2/7PNFRD4N3oFyeWDwaDDuv oIIjHFU1pZ66oOV+u2kft6+yFB+SREIrgfcfZzQUUWotqVqn6dqSRt3NlBwDmPOaZwXp2I4CmBX ipnNi8uvzG+zaihL0DKqdZ10SoDJ/lHAKmQVnJyBQSk+b28rndZqGqekPv2Qg/IgYRg7CvBYwBQ ie/oLzfMed6TpVVsfkf8w/8c8WA05MFM6pg9Q4T0UkhMMvKV9bgRonV/t7q2nzp1KXrQiZ+fnGD JR2/C+lr0MN4lz5tVBjmneE5sh4Sg7r6iO7NX4W9qA1GgDw0YmU8HXvzjwT7loCtJDWMiXaTEF3 WJQ86Ah/q6O/eUA== 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 --- v1 -> v2: - --- 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, From patchwork Sat Jun 22 23:48:31 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Grzeschik X-Patchwork-Id: 806989 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 02B65129E94 for ; Sat, 22 Jun 2024 23:48:42 +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=1719100126; cv=none; b=p2nmjEvwdsb5qffNm+Ty4+Brn0FqfBmU9ZrD36LRF9dVrz1vcZSvv0StFoe2+HSKRoOxtOa6ukOx+IgF2eyEV5QaSlUE2MHx2oZP3BCjdD28MvlwWXYghfm8+WSp5qB0BWMb6VJV0m9BbUgZnvbNoflz7wFwxZlDIkXn8oW3iBw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1719100126; c=relaxed/simple; bh=G7Bdwm2LgSzcu8hKez+YkvaF4kB0cS8V8sK1sTJqTBo=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=sMs0uicR7+RzKJ1/FHTGUOsggfUtIi97YTfLhv9Q1cR93nVjrLX+Hs+xQU2DL4B4e2MmrgLVjts3Qazvxyu3p9ZOP6EOdXjjxLHr9QqBQLbxXISYXWty8YStqswlFvRW+zkWqHnUkDJqfb3aTWztjpfT6WCcBcAqLVFuLbqkcgs= 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 1sLATD-0002cg-5F; Sun, 23 Jun 2024 01:48:35 +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 1sLATC-004Hke-HQ; Sun, 23 Jun 2024 01:48:34 +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 1sLATC-000IWx-37; Sun, 23 Jun 2024 01:48:34 +0200 From: Michael Grzeschik Date: Sun, 23 Jun 2024 01:48:31 +0200 Subject: [PATCH v2 3/3] usb: gadget: uvc: set req_size and n_requests based on the 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-v2-3-12690f7a2eff@pengutronix.de> References: <20240403-uvc_request_length_by_interval-v2-0-12690f7a2eff@pengutronix.de> In-Reply-To: <20240403-uvc_request_length_by_interval-v2-0-12690f7a2eff@pengutronix.de> To: Laurent Pinchart , Daniel Scally , Greg Kroah-Hartman , Avichal Rakesh 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=3182; i=m.grzeschik@pengutronix.de; h=from:subject:message-id; bh=G7Bdwm2LgSzcu8hKez+YkvaF4kB0cS8V8sK1sTJqTBo=; b=owEBbQKS/ZANAwAKAb9pWET5cfSrAcsmYgBmd2LSvQDIFbhZh1nGSdlJ6iKp1YiKTCaR4Flr/ XaLR/EK/lKJAjMEAAEKAB0WIQQV2+2Fpbqd6fvv0Gi/aVhE+XH0qwUCZndi0gAKCRC/aVhE+XH0 q9YFEADCdz7a9ExUObvtVWFP8ta1exn5yyW8RaWwyUPA0FbvgczUKbvDUSTkzLnSBXi1Sx/Vqgu WLmzqGVZCEzJfrbYKNSduz9Rz0MyM1Pp5hE2zVqdp/4H3mxbHXSmfmfweHymsY8jdj7zSdrNSJr y+j0/8Y7HU1bFyBlDzPtMDCSotGQ42VowD5JyFyxn/xvvTy2nXeJCBsaD0Ykyy5GqcLMfXIxbOR dcrcWVaow/cOumdeQ3orgODjhQd5kE1nCUlrOsE46HfK+TyCBdsiTYUvH7vlg+gthsjFrgNruJZ Z3WE53ZsZNkHy3oQTuiv5FzIaLqZs8bh91hX5Rhm9NIZ3vtbJGDjIgnmbSH8uavFDV1uucnZyzu GziduKL0c37gx7CY0h6uVOh2XGUoyosqtP0M2VbtEyAj7BOeuWjfTDduDsoDYEB4pn0h4zABvLv BNbh0kMg963Kazdl7eqQONd0fdHq/K7/N6mqFjtaPCA5M3FhZPXiNN/DScerhpFaTRchkbUqbdN utQd3mcf8d7SJqNmMi8vF3OS9yKIxuPjcFvbEefwf62dAKKJB23nMNQRD8Q18kLr1yOTajAhYDp FhIkw90X3Eb0JGL+Orgauu9xJeqwdbRtbcJjfxi6LMlWvWe5ZFFa9QN8zw3ZtAOfxBKjDDQmdF0 VqDx5VgtNsypwxg== 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 With the information of the interval frame length it is now possible to calculate the number of usb requests by the frame duration. Based on the request size and the imagesize we calculate the actual size per request. This has calculation has the benefit that the frame data is equally distributed over all allocated requests. We keep the current req_size calculation as a fallback, if the interval callbacks did not set the interval property. Signed-off-by: Michael Grzeschik --- v1 -> v2: - add headersize per request into calculation --- drivers/usb/gadget/function/uvc_queue.c | 30 +++++++++++++++++++++++------- drivers/usb/gadget/function/uvc_video.c | 2 +- 2 files changed, 24 insertions(+), 8 deletions(-) diff --git a/drivers/usb/gadget/function/uvc_queue.c b/drivers/usb/gadget/function/uvc_queue.c index ce51643fc4639..141e52e34c610 100644 --- a/drivers/usb/gadget/function/uvc_queue.c +++ b/drivers/usb/gadget/function/uvc_queue.c @@ -44,7 +44,7 @@ static int uvc_queue_setup(struct vb2_queue *vq, { struct uvc_video_queue *queue = vb2_get_drv_priv(vq); struct uvc_video *video = container_of(queue, struct uvc_video, queue); - unsigned int req_size; + unsigned int req_size, max_req_size, header_size; unsigned int nreq; if (*nbuffers > UVC_MAX_VIDEO_BUFFERS) @@ -54,15 +54,31 @@ static int uvc_queue_setup(struct vb2_queue *vq, sizes[0] = video->imagesize; - req_size = video->ep->maxpacket + nreq = DIV_ROUND_UP(video->interval, video->ep->desc->bInterval * 1250); + + header_size = nreq * UVCG_REQUEST_HEADER_LEN; + + req_size = DIV_ROUND_UP(video->imagesize + header_size, nreq); + + max_req_size = video->ep->maxpacket * max_t(unsigned int, video->ep->maxburst, 1) * (video->ep->mult); - /* We divide by two, to increase the chance to run - * into fewer requests for smaller framesizes. - */ - nreq = DIV_ROUND_UP(DIV_ROUND_UP(sizes[0], 2), req_size); - nreq = clamp(nreq, 4U, 64U); + if (!req_size) { + req_size = max_req_size; + + /* We divide by two, to increase the chance to run + * into fewer requests for smaller framesizes. + */ + nreq = DIV_ROUND_UP(DIV_ROUND_UP(sizes[0], 2), req_size); + nreq = clamp(nreq, 4U, 64U); + } else if (req_size > max_req_size) { + /* The prepared interval length and expected buffer size + * is not possible to stream with the currently configured + * isoc bandwidth + */ + return -EINVAL; + } video->req_size = req_size; video->uvc_num_requests = nreq; diff --git a/drivers/usb/gadget/function/uvc_video.c b/drivers/usb/gadget/function/uvc_video.c index 95bb64e16f3da..d197c46e93fb4 100644 --- a/drivers/usb/gadget/function/uvc_video.c +++ b/drivers/usb/gadget/function/uvc_video.c @@ -304,7 +304,7 @@ static int uvcg_video_usb_req_queue(struct uvc_video *video, */ if (list_empty(&video->req_free) || ureq->last_buf || !(video->req_int_count % - DIV_ROUND_UP(video->uvc_num_requests, 4))) { + clamp(DIV_ROUND_UP(video->uvc_num_requests, 4), 4U, 16U))) { video->req_int_count = 0; req->no_interrupt = 0; } else {