Skip to contentSkip to conversation

usb: gadget: uvc: effectively fill the udc isoc pipeline with available video buffers and fixes

PATCH New Michael Grzeschik 1 participant 10 messages Oct 16, 2024

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

v7 cover usb: gadget: uvc: effectively fill the udc isoc pipeline with available video buffers and fixes

This patch series is improving the size calculation and allocation of the uvc requests. Using the selected frame duration of the stream it is possible to calculate the number of requests based on the interval length.

It also precalculates the request length based on the actual per frame size for compressed formats.

For this calculations to work it was needed to rework the request queueing by moving the encoding to one extra thread (in this case we chose the qbuf) context.

Next it was needed to move the actual request enqueueing to one extra thread which is kept busy to fill the isoc queue in the udc.

---
Changes in v7:
- fixed build of tracing in uvc_trace
- Link to v6: https://lore.kernel.org/r/20240403-uvc_request_length_by_interval-v6-0-08c05522e1f5@pengutronix.de

Changes in v6:
- fixes in: ("usb: gadget: uvc: add trace of enqueued and completed requests")
- Link to v5: https://lore.kernel.org/r/20240403-uvc_request_length_by_interval-v5-0-2de78794365c@pengutronix.de

Changes in v5:
- dropped: ('usb: gadget: uvc: remove pump worker and enqueue all buffers per frame in qbuf')
- squashed ('usb: gadget: uvc: rework to enqueue in pump worker from encoded queue') and ('usb: gadget: uvc: remove uvc_video_ep_queue_initial_requests'))
- squashed ('usb: gadget: uvc: set req_size once when the vb2 queue is calculated') and ('usb: gadget: uvc: set req_size and n_requests based on the frame interval')
- replaced ('usb: gadget: uvc: add min g_ctrl vidioc and set min buffs to 4') with ('usb: gadget: uvc: set nbuffers to minimum STREAMING_MIN_BUFFERS in uvc_queue_setup')
- added ('usb: gadget: uvc: wake pump everytime we update the free list')
- added ('usb: gadget: uvc: add trace of enqueued and completed requests')
- added ('usb: gadget: uvc: dont call usb_composite_setup_continue when not streamin')
- some patch reordering
- Link to v4: https://lore.kernel.org/r/20240403-uvc_request_length_by_interval-v4-0-ca22f334226e@pengutronix.de

Changes in v4:
- fixed exit path in uvc_enqueue_buffer on loop break
- Link to v3: https://lore.kernel.org/r/20240403-uvc_request_length_by_interval-v3-0-4da7033dd488@pengutronix.de

Changes in v3:
- Added more patches necessary to properly rework the request queueing
- Link to v2: https://lore.kernel.org/r/20240403-uvc_request_length_by_interval-v2-0-12690f7a2eff@pengutronix.de

Changes in v2:
- added header size into calculation of request size
- Link to v1: https://lore.kernel.org/r/20240403-uvc_request_length_by_interval-v1-0-9436c4716233@pengutronix.de

---
Michael Grzeschik (9):
usb: gadget: uvc: wake pump everytime we update the free list
usb: gadget: uvc: only enqueue zero length requests in potential underrun
usb: gadget: uvc: rework to enqueue in pump worker from encoded queue
usb: gadget: uvc: add g_parm and s_parm for frame interval
usb: gadget: uvc: set req_size and n_requests based on the frame interval
usb: gadget: uvc: set req_length based on payload by nreqs instead of req_size
usb: gadget: uvc: set nbuffers to minimum STREAMING_MIN_BUFFERS in uvc_queue_setup
usb: gadget: uvc: add trace of enqueued and completed requests
usb: gadget: uvc: dont call usb_composite_setup_continue when not streaming

 drivers/usb/gadget/function/Makefile    |   4 +
 drivers/usb/gadget/function/f_uvc.c     |   2 +
 drivers/usb/gadget/function/uvc.h       |  13 ++
 drivers/usb/gadget/function/uvc_queue.c |  26 ++--
 drivers/usb/gadget/function/uvc_queue.h |   2 +
 drivers/usb/gadget/function/uvc_trace.c |  11 ++
 drivers/usb/gadget/function/uvc_trace.h |  60 ++++++++
 drivers/usb/gadget/function/uvc_v4l2.c  |  55 +++++++
 drivers/usb/gadget/function/uvc_video.c | 264 +++++++++++++++++++-------------
 9 files changed, 317 insertions(+), 120 deletions(-)

---
base-commit: 64f3b5a6bc49adf77d58eddd72a4bfccd492fa24
change-id: 20240403-uvc_request_length_by_interval-a7efd587d963

Best regards,

Signature · 1 line
Michael Grzeschik <[email protected]>
#1 lore raw

PATCH v7 1/9 usb: gadget: uvc: wake pump everytime we update the free list

Since the req_free list will updated if enqueuing one request was not possible it will be added back to the free list. With every available free request in the queue it is a valid case for the pump worker to use it and continue the pending bufferdata into requests for the req_ready list.

Fixes 6acba0345b68 ("usb:gadget:uvc Do not use worker thread to pump isoc usb requests")

---
v5 -> v7: -
v1 -> v5: - new patch

---
 drivers/usb/gadget/function/uvc_video.c | 4 ++++
 1 file changed, 4 insertions(+)
drivers/usb/gadget/function/uvc_video.c +4 -0
index 57a851151225d..002bf724d8025 100644
@@ -480,6 +480,10 @@ uvc_video_complete(struct usb_ep *ep, struct usb_request *req)
 		 * up later.
 		 */
 		list_add_tail(&to_queue->list, &video->req_free);
+		/*
+		 * There is a new free request - wake up the pump.
+		 */
+		queue_work(video->async_wq, &video->pump);
 	}
 
 	spin_unlock_irqrestore(&video->req_lock, flags);
Signature · 1 line
2.39.5
#2 lore raw

PATCH v7 2/9 usb: gadget: uvc: only enqueue zero length requests in potential underrun

In reply to Michael Grzeschik

The complete handler will at least be called after 16 requests have completed, but will still handle all finisher requests. Since we have to maintain a costant filling in the isoc queue we ensure this by adding zero length requests.

By counting the amount enqueued requests we can ensure that the queue is never underrun and only need to get active if the queue is running critical. This patch is setting 32 as the critical level, which is twice the request amount that is needed to create interrupts.

To properly solve the amount of zero length requests that needs to be held in the hardware after one interrupt needs to be measured and depends on the runtime of the first enqueue run after the interrupt triggered. For now we just use twice the amount of requests between an interrupt.

---
v5 -> v7: -
v4 -> v5:
- using min to limit for UVCG_REQ_MAX_INT_COUNT as the interrupt boundary
- using atomic_inc, atomic_dec on one variable to avoid rollover
- reordered this patch in the series
- added UVCG_REQ_MAX_ZERO_COUNT to set the threshold of zero length requests in the hw
- added UVCG_REQ_MAX_INT_COUNT as quantifier for the
highest amount of request between an interrupt
v1 -> v4: -

---
 drivers/usb/gadget/function/uvc.h       |  5 +++++
 drivers/usb/gadget/function/uvc_video.c | 17 ++++++++++++++++-
 2 files changed, 21 insertions(+), 1 deletion(-)
drivers/usb/gadget/function/uvc.h +5 -0
index cb35687b11e7e..55d796f5f5e8d 100644
@@ -71,6 +71,9 @@ extern unsigned int uvc_gadget_trace_param;
 
 #define UVCG_REQUEST_HEADER_LEN			12
 
+#define UVCG_REQ_MAX_INT_COUNT			16
+#define UVCG_REQ_MAX_ZERO_COUNT			(2 * UVCG_REQ_MAX_INT_COUNT)
+
 /* ------------------------------------------------------------------------
  * Structures
  */
@@ -91,6 +94,8 @@ struct uvc_video {
 	struct work_struct pump;
 	struct workqueue_struct *async_wq;
 
+	atomic_t queued;
+
 	/* Frame parameters */
 	u8 bpp;
 	u32 fcc;
drivers/usb/gadget/function/uvc_video.c +16 -1
index 002bf724d8025..c041873cf8560 100644
@@ -269,6 +269,8 @@ static int uvcg_video_ep_queue(struct uvc_video *video, struct usb_request *req)
 		}
 	}
 
+	atomic_inc(&video->queued);
+
 	return ret;
 }
 
@@ -304,7 +306,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))) {
+			min(DIV_ROUND_UP(video->uvc_num_requests, 4), UVCG_REQ_MAX_INT_COUNT))) {
 			video->req_int_count = 0;
 			req->no_interrupt = 0;
 		} else {
@@ -379,6 +381,7 @@ uvc_video_complete(struct usb_ep *ep, struct usb_request *req)
 	int ret = 0;
 
 	spin_lock_irqsave(&video->req_lock, flags);
+	atomic_dec(&video->queued);
 	if (!video->is_enabled) {
 		/*
 		 * When is_enabled is false, uvcg_video_disable() ensures
@@ -466,6 +469,16 @@ uvc_video_complete(struct usb_ep *ep, struct usb_request *req)
 		 * happen.
 		 */
 		queue_work(video->async_wq, &video->pump);
+	} else if (atomic_read(&video->queued) > UVCG_REQ_MAX_ZERO_COUNT) {
+		list_add_tail(&to_queue->list, &video->req_free);
+		/*
+		 * There is a new free request - wake up the pump.
+		 */
+		queue_work(video->async_wq, &video->pump);
+
+		spin_unlock_irqrestore(&video->req_lock, flags);
+
+		return;
 	}
 	/*
 	 * Queue to the endpoint. The actual queueing to ep will
@@ -756,6 +769,8 @@ int uvcg_video_enable(struct uvc_video *video)
 
 	video->req_int_count = 0;
 
+	atomic_set(&video->queued, 0);
+
 	uvc_video_ep_queue_initial_requests(video);
 	queue_work(video->async_wq, &video->pump);
 
Signature · 1 line
2.39.5
#3 lore raw

PATCH v7 3/9 usb: gadget: uvc: rework to enqueue in pump worker from encoded queue

In reply to Michael Grzeschik

We install an kthread with pfifo priority that is iterating over all prepared requests and keeps the isoc queue busy. This way it will be scheduled with the same priority as the interrupt handler.

As the kthread is triggered with video_enable it will immediately queue some zero length requests into the hw if there is no buffer data available. It also watches the level of needed zero length requests in the hardware not to fall under the UVCG_REQ_MAX_ZERO_COUNT threshold. This way we can drop the function uvc_video_ep_queue_initial_requests entirely.

By using the kthread to do the actual request handling the interrupt handler will not be running into the time consuming and eventually locking work of actually enqueueing the requests back into its own pipeline. This work can now even be scheduled on another cpu.

---
v5 -> v7: -
v4 -> v5:
- merging patch '(drivers/usb/gadget/function/uvc_queue.h') into this one
- added initial kthread_queue_work(video->kworker, &video->hw_submit); to uvcg_video_enable
- fixed error message in uvcg_video_init
- reordered this patch in the series
- renamed the kworker to hw_submit since the pump thread is used again with the work_queue
v1 -> v4: -

---
 drivers/usb/gadget/function/f_uvc.c     |   2 +
 drivers/usb/gadget/function/uvc.h       |   3 +
 drivers/usb/gadget/function/uvc_video.c | 180 +++++++++++++++-----------------
 3 files changed, 87 insertions(+), 98 deletions(-)
drivers/usb/gadget/function/f_uvc.c +2 -0
index 40187b7112e79..f04376768bc10 100644
@@ -991,6 +991,8 @@ static void uvc_function_unbind(struct usb_configuration *c,
 
 	uvcg_info(f, "%s()\n", __func__);
 
+	kthread_cancel_work_sync(&video->hw_submit);
+
 	if (video->async_wq)
 		destroy_workqueue(video->async_wq);
 
drivers/usb/gadget/function/uvc.h +3 -0
index 55d796f5f5e8d..4f44a607d9f5c 100644
@@ -94,6 +94,9 @@ struct uvc_video {
 	struct work_struct pump;
 	struct workqueue_struct *async_wq;
 
+	struct kthread_worker   *kworker;
+	struct kthread_work     hw_submit;
+
 	atomic_t queued;
 
 	/* Frame parameters */
drivers/usb/gadget/function/uvc_video.c +82 -98

220 lines not on this page · raw message

Signature · 1 line
2.39.5
#4 lore raw

PATCH v7 4/9 usb: gadget: uvc: add g_parm and s_parm for frame interval

In reply to Michael Grzeschik

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.

---
v5 -> v7: -
v4 -> v5:
- using !V4L2_TYPE_IS_OUTPUT to check for type
- initialzing interval to same default as in configfs
- reordered this patch in the series
v1 -> v4: -

---
 drivers/usb/gadget/function/uvc.h       |  1 +
 drivers/usb/gadget/function/uvc_v4l2.c  | 52 +++++++++++++++++++++++++++++++++
 drivers/usb/gadget/function/uvc_video.c |  1 +
 3 files changed, 54 insertions(+)
drivers/usb/gadget/function/uvc.h +1 -0
index 4f44a607d9f5c..099038f1088ef 100644
@@ -105,6 +105,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;
drivers/usb/gadget/function/uvc_v4l2.c +52 -0

64 lines not on this page · raw message

drivers/usb/gadget/function/uvc_video.c +1 -0
index 06055959f7165..ee7326029d6f9 100644
@@ -784,6 +784,7 @@ int uvcg_video_init(struct uvc_video *video, struct uvc_device *uvc)
 	video->width = 320;
 	video->height = 240;
 	video->imagesize = 320 * 240 * 2;
+	video->interval = 666666;
 
 	/* Initialize the video buffers queue. */
 	uvcg_queue_init(&video->queue, uvc->v4l2_dev.dev->parent,
Signature · 1 line
2.39.5
#5 lore raw

PATCH v7 5/9 usb: gadget: uvc: set req_size and n_requests based on the frame interval

In reply to Michael Grzeschik

This patch is removing the initial imprecise and limited calculation of requests needed to be used from the queue_setup callback. It instead introduces the uvc_video_prep_requests function which is called immediately before the request allocation.

With the information of the usb frame interval length it is possible to calculate the number of requests needed during one frame duration.

Based on the calculated number of requests and the imagesize we calculate the actual size per request. This calculation has the benefit that the frame data is equally distributed over all allocated requests.

When the req_size is not in the range for the actually configured max_req_size configured for the overall bandwidth we fallback to use the max_req_size instead.

Since this calculations are only important for isoc transfers we just use max_request_size for bulk and skip it.

As video->req_size will be recalculated on every video_enable resetting it to 0 is not necessary anymore.

---
v5 -> v7: -
v4 -> v5:
- reordered this patch in the series
- merging previous extra patch: ('usb: gadget: uvc: set req_size once when the vb2 queue is calculated')
- moved the overall request size calculation to one seperate function
- only calculating once before enabling the video ep
v3 -> v4: -
v2 -> v3:
- added the frame duration for full-speed devices into calculation
v1 -> v2:
- add headersize per request into calculation

---
 drivers/usb/gadget/function/uvc_queue.c | 13 -------
 drivers/usb/gadget/function/uvc_video.c | 68 +++++++++++++++++++++++++++------
 2 files changed, 56 insertions(+), 25 deletions(-)
drivers/usb/gadget/function/uvc_queue.c +0 -13
index 0aa3d7e1f3cc3..731e3b9d21acc 100644
@@ -44,8 +44,6 @@ 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 nreq;
 
 	if (*nbuffers > UVC_MAX_VIDEO_BUFFERS)
 		*nbuffers = UVC_MAX_VIDEO_BUFFERS;
@@ -54,17 +52,6 @@ static int uvc_queue_setup(struct vb2_queue *vq,
 
 	sizes[0] = video->imagesize;
 
-	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);
-	video->uvc_num_requests = nreq;
-
 	return 0;
 }
 
drivers/usb/gadget/function/uvc_video.c +56 -12

105 lines not on this page · raw message

Signature · 1 line
2.39.5
#6 lore raw

PATCH v7 6/9 usb: gadget: uvc: set req_length based on payload by nreqs instead of req_size

In reply to Michael Grzeschik

Compressed formats generate content depending amount of data that is set in the vb2 buffer by the payload_size. When streaming those formats it is better to scatter that smaller data over all requests. This patch is doing that by introducing the calculated req_payload_size which is updated by each frame. It the uses this amount of data to fill the isoc requests instead of the video->req_size.

For uncompressed formats it will not make a difference since the payload size will be equal to the imagesize. Therefore the code will have no effecta as req_payload_size will be equal to req_size.

---
v5 -> v7: -
v4 -> v5:
- keep using req_size instead of len in encode_isoc_sg to be more explicit
- using new initialized variable reqs_per_frame instead of two calculations
- reordered this patch in the series
v1 -> v4: -

---
 drivers/usb/gadget/function/uvc.h       |  2 ++
 drivers/usb/gadget/function/uvc_queue.c | 10 ++++++++--
 drivers/usb/gadget/function/uvc_queue.h |  2 ++
 drivers/usb/gadget/function/uvc_video.c | 15 ++++++++-------
 4 files changed, 20 insertions(+), 9 deletions(-)
drivers/usb/gadget/function/uvc.h +2 -0
index 099038f1088ef..bedb4ef42864f 100644
@@ -110,6 +110,8 @@ struct uvc_video {
 
 	unsigned int uvc_num_requests;
 
+	unsigned int reqs_per_frame;
+
 	/* Requests */
 	bool is_enabled; /* tracks whether video stream is enabled */
 	unsigned int req_size;
drivers/usb/gadget/function/uvc_queue.c +8 -2
index 731e3b9d21acc..6757a4e25a743 100644
@@ -58,6 +58,7 @@ static int uvc_queue_setup(struct vb2_queue *vq,
 static int uvc_buffer_prepare(struct vb2_buffer *vb)
 {
 	struct uvc_video_queue *queue = vb2_get_drv_priv(vb->vb2_queue);
+	struct uvc_video *video = container_of(queue, struct uvc_video, queue);
 	struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
 	struct uvc_buffer *buf = container_of(vbuf, struct uvc_buffer, buf);
 
@@ -78,10 +79,15 @@ static int uvc_buffer_prepare(struct vb2_buffer *vb)
 		buf->mem = vb2_plane_vaddr(vb, 0);
 	}
 	buf->length = vb2_plane_size(vb, 0);
-	if (vb->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
+	if (vb->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
 		buf->bytesused = 0;
-	else
+	} else {
 		buf->bytesused = vb2_get_plane_payload(vb, 0);
+		buf->req_payload_size =
+			  DIV_ROUND_UP(buf->bytesused +
+				       (video->reqs_per_frame * UVCG_REQUEST_HEADER_LEN),
+				       video->reqs_per_frame);
+	}
 
 	return 0;
 }
drivers/usb/gadget/function/uvc_queue.h +2 -0
index 41f87b917f6bc..b54becc570a38 100644
@@ -39,6 +39,8 @@ struct uvc_buffer {
 	unsigned int offset;
 	unsigned int length;
 	unsigned int bytesused;
+	/* req_payload_size: only used with isoc */
+	unsigned int req_payload_size;
 };
 
 #define UVC_QUEUE_DISCONNECTED		(1 << 0)
drivers/usb/gadget/function/uvc_video.c +8 -7
index ecb32a3e03760..677eaf5b7e4d0 100644
@@ -136,7 +136,7 @@ uvc_video_encode_isoc_sg(struct usb_request *req, struct uvc_video *video,
 	unsigned int pending = buf->bytesused - video->queue.buf_used;
 	struct uvc_request *ureq = req->context;
 	struct scatterlist *sg, *iter;
-	unsigned int len = video->req_size;
+	unsigned int len = buf->req_payload_size;
 	unsigned int sg_left, part = 0;
 	unsigned int i;
 	int header_len;
@@ -146,15 +146,15 @@ uvc_video_encode_isoc_sg(struct usb_request *req, struct uvc_video *video,
 
 	/* Init the header. */
 	header_len = uvc_video_encode_header(video, buf, ureq->header,
-				      video->req_size);
+					     buf->req_payload_size);
 	sg_set_buf(sg, ureq->header, header_len);
 	len -= header_len;
 
 	if (pending <= len)
 		len = pending;
 
-	req->length = (len == pending) ?
-		len + header_len : video->req_size;
+	req->length = (len == pending) ? len + header_len :
+		buf->req_payload_size;
 
 	/* Init the pending sgs with payload */
 	sg = sg_next(sg);
@@ -202,7 +202,7 @@ uvc_video_encode_isoc(struct usb_request *req, struct uvc_video *video,
 {
 	void *mem = req->buf;
 	struct uvc_request *ureq = req->context;
-	int len = video->req_size;
+	int len = buf->req_payload_size;
 	int ret;
 
 	/* Add the header. */
@@ -214,7 +214,7 @@ uvc_video_encode_isoc(struct usb_request *req, struct uvc_video *video,
 	ret = uvc_video_encode_data(video, buf, mem, len);
 	len -= ret;
 
-	req->length = video->req_size - len;
+	req->length = buf->req_payload_size - len;
 
 	if (buf->bytesused == video->queue.buf_used ||
 			video->queue.flags & UVC_QUEUE_DROP_INCOMPLETE) {
@@ -504,7 +504,7 @@ uvc_video_prep_requests(struct uvc_video *video)
 
 	if (!usb_endpoint_xfer_isoc(video->ep->desc)) {
 		video->req_size = max_req_size;
-		video->uvc_num_requests =
+		video->reqs_per_frame = video->uvc_num_requests =
 			DIV_ROUND_UP(video->imagesize, max_req_size);
 
 		return;
@@ -535,6 +535,7 @@ uvc_video_prep_requests(struct uvc_video *video)
 	 * able to fully encode one frame.
 	 */
 	video->uvc_num_requests = nreq + UVCG_REQ_MAX_ZERO_COUNT;
+	video->reqs_per_frame = nreq;
 }
 
 static int
Signature · 1 line
2.39.5
#7 lore raw

PATCH v7 7/9 usb: gadget: uvc: set nbuffers to minimum STREAMING_MIN_BUFFERS in uvc_queue_setup

In reply to Michael Grzeschik

We set the minimum amount of v4l2 buffers that is possibly be pending to UVCG_STREAMING_MIN_BUFFERS which is two. This way the driver will always have at least one frame pending to be encoded while the other is being enqueued in the hardware.

---
v5 -> v7: -
v4 -> v5:
- using STREAMING_MIN_BUFFERS set the min nbuffers
- renamed the patch since the function changed
- removed the g_ctrl function
- reordered this patch in the series
v1 -> v4: -

---
 drivers/usb/gadget/function/uvc.h       | 2 ++
 drivers/usb/gadget/function/uvc_queue.c | 3 +++
 2 files changed, 5 insertions(+)
drivers/usb/gadget/function/uvc.h +2 -0
index bedb4ef42864f..6f44dd7323150 100644
@@ -74,6 +74,8 @@ extern unsigned int uvc_gadget_trace_param;
 #define UVCG_REQ_MAX_INT_COUNT			16
 #define UVCG_REQ_MAX_ZERO_COUNT			(2 * UVCG_REQ_MAX_INT_COUNT)
 
+#define UVCG_STREAMING_MIN_BUFFERS		2
+
 /* ------------------------------------------------------------------------
  * Structures
  */
drivers/usb/gadget/function/uvc_queue.c +3 -0
index 6757a4e25a743..5eaeae3e2441c 100644
@@ -21,6 +21,7 @@
 #include <media/videobuf2-vmalloc.h>
 
 #include "uvc.h"
+#include "uvc_video.h"
 
 /* ------------------------------------------------------------------------
  * Video buffers queue management.
@@ -47,6 +48,8 @@ static int uvc_queue_setup(struct vb2_queue *vq,
 
 	if (*nbuffers > UVC_MAX_VIDEO_BUFFERS)
 		*nbuffers = UVC_MAX_VIDEO_BUFFERS;
+	if (*nbuffers < UVCG_STREAMING_MIN_BUFFERS)
+		*nbuffers = UVCG_STREAMING_MIN_BUFFERS;
 
 	*nplanes = 1;
 
Signature · 1 line
2.39.5
#8 lore raw

PATCH v7 8/9 usb: gadget: uvc: add trace of enqueued and completed requests

In reply to Michael Grzeschik

This patch is adding trace events for each request that is being enqueued into the hw and will be completed. This way it is possible to track the fill status of the gadget hardware and find potential issues.

---
v6 -> v7:
- add missing CFLAGS for uvc_trace.o in Makefile
v5 -> v6:
- fixed compile rule as module
- reordered header files
v1 -> v5:
- new patch

---
 drivers/usb/gadget/function/Makefile    |  4 +++
 drivers/usb/gadget/function/uvc_trace.c | 11 ++++++
 drivers/usb/gadget/function/uvc_trace.h | 60 +++++++++++++++++++++++++++++++++
 drivers/usb/gadget/function/uvc_video.c |  5 +++
 4 files changed, 80 insertions(+)
drivers/usb/gadget/function/Makefile +4 -0
index 87917a7d4a9be..7ce1637276f09 100644
@@ -41,6 +41,10 @@ obj-$(CONFIG_USB_F_UAC1_LEGACY)	+= usb_f_uac1_legacy.o
 usb_f_uac2-y			:= f_uac2.o
 obj-$(CONFIG_USB_F_UAC2)	+= usb_f_uac2.o
 usb_f_uvc-y			:= f_uvc.o uvc_queue.o uvc_v4l2.o uvc_video.o uvc_configfs.o
+ifneq ($(CONFIG_TRACING),)
+    CFLAGS_uvc_trace.o		:= -I$(src)
+    usb_f_uvc-y			+= uvc_trace.o
+endif
 obj-$(CONFIG_USB_F_UVC)		+= usb_f_uvc.o
 usb_f_midi-y			:= f_midi.o
 obj-$(CONFIG_USB_F_MIDI)	+= usb_f_midi.o
drivers/usb/gadget/function/uvc_trace.c new file +11 -0
new file mode 100644
index 0000000000000..d384f6d8221a5
@@ -0,0 +1,11 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * trace.c - USB UVC Gadget Trace Support
+ *
+ * Copyright (C) 2024 Pengutronix e.K.
+ *
+ * Author: Michael Grzeschik <[email protected]>
+ */
+
+#define CREATE_TRACE_POINTS
+#include "uvc_trace.h"
drivers/usb/gadget/function/uvc_trace.h new file +60 -0

60 lines not on this page · raw message

drivers/usb/gadget/function/uvc_video.c +5 -0
index 677eaf5b7e4d0..23fad3bc72c03 100644
@@ -19,6 +19,7 @@
 #include "uvc.h"
 #include "uvc_queue.h"
 #include "uvc_video.h"
+#include "uvc_trace.h"
 
 /* --------------------------------------------------------------------------
  * Video codecs
@@ -271,6 +272,8 @@ static int uvcg_video_ep_queue(struct uvc_video *video, struct usb_request *req)
 
 	atomic_inc(&video->queued);
 
+	trace_uvcg_video_queue(req, atomic_read(&video->queued));
+
 	return ret;
 }
 
@@ -408,6 +411,8 @@ uvc_video_complete(struct usb_ep *ep, struct usb_request *req)
 	 */
 	queue_work(video->async_wq, &video->pump);
 
+	trace_uvcg_video_complete(req, atomic_read(&video->queued));
+
 	spin_unlock_irqrestore(&video->req_lock, flags);
 
 	kthread_queue_work(video->kworker, &video->hw_submit);
Signature · 1 line
2.39.5
#9 lore raw

PATCH v7 9/9 usb: gadget: uvc: dont call usb_composite_setup_continue when not streaming

In reply to Michael Grzeschik

If the streamoff call was triggered by some previous disconnect or userspace application shutdown the uvc_function_setup_continue should not be called and the state should not be overwritten.

For this situation the set_alt(0) was never called and the streaming ep has no USB_GADGET_DELAYED_STATUS pending.

Since the state then was already updated before we also omit the state update.

---
v5 -> v7: -
v1 -> v5: - new patch

---
 drivers/usb/gadget/function/uvc_v4l2.c | 3 +++
 1 file changed, 3 insertions(+)
drivers/usb/gadget/function/uvc_v4l2.c +3 -0
index ab89f1630acb0..3492855f0fb29 100644
@@ -546,6 +546,9 @@ uvc_v4l2_streamoff(struct file *file, void *fh, enum v4l2_buf_type type)
 	if (ret < 0)
 		return ret;
 
+	if (uvc->state != UVC_STATE_STREAMING)
+		return 0;
+
 	uvc->state = UVC_STATE_CONNECTED;
 	uvc_function_setup_continue(uvc, 1);
 	return 0;
Signature · 1 line
2.39.5