Skip to contentSkip to conversation

media: uvcvideo: handle urb completion in a tasklet

PATCH New Mian Yousaf Kaukab 1 participant 1 message Sep 23, 2015

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

PATCH v2 1/1 media: uvcvideo: handle urb completion in a tasklet

urb completion callback is executed in either host controllers interrupt context or a tasklet (if hcd has set HCD_BH flag).

If hcd is calling urb->complete from interrupt context, to keep preempt disable time short, add urbs to a list on completion and schedule a tasklet to process the list.

Moreover, save timestamp and sof number in the urb completion callback to avoid any delays.

---
[Rename from media: uvcvideo: handle urb completion in a work queue]

History:
v2:
- Change to use tasklet instead of workqueue
- Don't use local tasklet if hcd is aleady using tasklet for completion callback
v1:
- Use global work queue instead of creating ordered queue.
- Add completed urbs to a list and process all on the list when work is scheduled
- Save timestamp and sof number in urb completion callback and use in uvc_video_clock_decode() and uvc_video_decode_start()
- Fix race between usb_submit_urb() from uvc_video_complete() and usb_kill_urb() from uvc_uninit_video()

 drivers/media/usb/uvc/uvc_driver.c |   8 +++
 drivers/media/usb/uvc/uvc_isight.c |   3 +-
 drivers/media/usb/uvc/uvc_video.c  | 144 ++++++++++++++++++++++++++++---------
 drivers/media/usb/uvc/uvcvideo.h   |  18 ++++-
 4 files changed, 134 insertions(+), 39 deletions(-)
drivers/media/usb/uvc/uvc_driver.c +8 -0
index 4b5b3e8..218d4f5 100644
@@ -21,6 +21,7 @@
 #include <linux/vmalloc.h>
 #include <linux/wait.h>
 #include <linux/version.h>
+#include <linux/usb/hcd.h>
 #include <asm/unaligned.h>
 
 #include <media/v4l2-common.h>
@@ -1945,6 +1946,13 @@ static int uvc_probe(struct usb_interface *intf,
 			"supported.\n", ret);
 	}
 
+	/*
+	 * This shouldn't be here. But since not all hcd are using tasklet for
+	 * urb completion callback, Check this from hcd and only use tasklet
+	 * for handling urb completion if hcd is not already using it.
+	 */
+	dev->hcd_uses_bh = hcd_giveback_urb_in_bh(bus_to_hcd(udev->bus));
+
 	uvc_trace(UVC_TRACE_PROBE, "UVC device initialized.\n");
 	usb_enable_autosuspend(udev);
 	return 0;
drivers/media/usb/uvc/uvc_isight.c +2 -1
index 8510e725..7f93d09 100644
@@ -99,9 +99,10 @@ static int isight_decode(struct uvc_video_queue *queue, struct uvc_buffer *buf,
 	return 0;
 }
 
-void uvc_video_decode_isight(struct urb *urb, struct uvc_streaming *stream,
+void uvc_video_decode_isight(struct uvc_urb *uu, struct uvc_streaming *stream,
 		struct uvc_buffer *buf)
 {
+	struct urb *urb = uu->urb;
 	int ret, i;
 
 	for (i = 0; i < urb->number_of_packets; ++i) {
drivers/media/usb/uvc/uvc_video.c +109 -35

300 lines not on this page · raw message

drivers/media/usb/uvc/uvcvideo.h +15 -3
index 816dd1a..0633d8a 100644
@@ -440,6 +440,13 @@ struct uvc_stats_stream {
 	unsigned int max_sof;		/* Maximum STC.SOF value */
 };
 
+struct uvc_urb {
+	struct urb *urb;
+	struct uvc_streaming *stream;
+	struct timespec ts;
+	u16 sof;
+};
+
 struct uvc_streaming {
 	struct list_head list;
 	struct uvc_device *dev;
@@ -470,7 +477,7 @@ struct uvc_streaming {
 	/* Buffers queue. */
 	unsigned int frozen : 1;
 	struct uvc_video_queue queue;
-	void (*decode) (struct urb *urb, struct uvc_streaming *video,
+	void (*decode)(struct uvc_urb *uu, struct uvc_streaming *video,
 			struct uvc_buffer *buf);
 
 	/* Context data used by the bulk completion handler. */
@@ -482,7 +489,11 @@ struct uvc_streaming {
 		__u32 max_payload_size;
 	} bulk;
 
-	struct urb *urb[UVC_URBS];
+	bool stop;
+	spinlock_t urb_list_lock;
+	struct list_head urb_complete_list;
+	struct tasklet_struct urb_tasklet;
+	struct uvc_urb uvc_urb[UVC_URBS];
 	char *urb_buffer[UVC_URBS];
 	dma_addr_t urb_dma[UVC_URBS];
 	unsigned int urb_size;
@@ -550,6 +561,7 @@ struct uvc_device {
 	__u8 *status;
 	struct input_dev *input;
 	char input_phys[64];
+	bool hcd_uses_bh;
 };
 
 enum uvc_handle_state {
@@ -728,7 +740,7 @@ extern struct usb_host_endpoint *uvc_find_endpoint(
 		struct usb_host_interface *alts, __u8 epaddr);
 
 /* Quirks support */
-void uvc_video_decode_isight(struct urb *urb, struct uvc_streaming *stream,
+void uvc_video_decode_isight(struct uvc_urb *uu, struct uvc_streaming *stream,
 		struct uvc_buffer *buf);
 
 /* debugfs and statistics */
Signature · 1 line
2.3.3