Message ID | 20240403-uvc_request_length_by_interval-v2-3-12690f7a2eff@pengutronix.de (mailing list archive) |
---|---|
State | New, archived |
Headers | show |
Series | usb: gadget: uvc: allocate requests based on frame interval length and buffersize | expand |
On 6/22/24 4:48 PM, Michael Grzeschik wrote: > 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 <m.grzeschik@pengutronix.de> > > --- > 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); This seems problematic? I am not very well versed in the different USB speeds, but IIRC fullspeed and highspeed enpoints have different bus intervals, and treat bInterval in different units (in frames for fs and in microframes for hs). We likely need some speed specific logic when calculating nreq. Assuming this logic is for >= hs, this allocates the exact number of usb_requests needed to stream a frame over to the host in one video frame interval. With the zero length backpressure still in place, this would mean that the actual video frame is sent over a period longer than on video frame interval. I will try these patches locally, but if you haven't already, please do check if you run into the problem you brought up in https://lore.kernel.org/all/ZiWga5Kqno1ICv97@pengutronix.de/. My guess is that the problem will show up here as well. > + > + 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 { >
Hi Avichal, On Wed, Jun 26, 2024 at 11:57:42AM -0700, Avichal Rakesh wrote: > > >On 6/22/24 4:48 PM, Michael Grzeschik wrote: >> 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 <m.grzeschik@pengutronix.de> >> >> --- >> 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); > >This seems problematic? I am not very well versed in the different USB speeds, >but IIRC fullspeed and highspeed enpoints have different bus intervals, and >treat bInterval in different units (in frames for fs and in microframes for hs). > >We likely need some speed specific logic when calculating nreq. Fair point! I did not think about that yet and will fix it in v3. >Assuming this logic is for >= hs, this allocates the exact number of >usb_requests needed to stream a frame over to the host in one video >frame interval. With the zero length backpressure still in place, this >would mean that the actual video frame is sent over a period longer than >on video frame interval. I will try these patches locally, but if you >haven't already, please do check if you run into the problem you >brought up in https://lore.kernel.org/all/ZiWga5Kqno1ICv97@pengutronix.de/. >My guess is that the problem will show up here as well. Yes. With this current patchset there is not enough requests to keep enqueueing requests fast enough since the interrupt handler will have to wait for ready requests to show up while it uses the finishing requests to fill zero length requests instead of giving them back to the free pool. So just having the exact amount of requests for one frame interval available is way to less. I fixed that by creating at least four times the amount of available requests. Just the way you already suggested in an earlier mail :) . I also added an threshold that will only enqueue zero length requests if the currently enqueued amount of requests is undercut. However this is not enough to fulfill the requirements for the dwc3 gadget driver. We also have to ensure that the interrupt handler is not running too long. To solve this I made additional changes. I sort them this week and send a next version of it. I hope you could review and test them soon. Regards, Michael >> + >> + 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 { >> >
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 {
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 <m.grzeschik@pengutronix.de> --- 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(-)