public inbox for linux-media@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH] [media] uvcvideo: Retry usb_submit_urb on -EPERM return
@ 2013-04-24  0:42 Shawn Nematbakhsh
  2013-04-29 20:34 ` Laurent Pinchart
  0 siblings, 1 reply; 5+ messages in thread
From: Shawn Nematbakhsh @ 2013-04-24  0:42 UTC (permalink / raw)
  To: Laurent Pinchart; +Cc: Mauro Carvalho Chehab, linux-media, linux-kernel, shawnn

While usb_kill_urb is in progress, calls to usb_submit_urb will fail
with -EPERM (documented in Documentation/usb/URB.txt). The UVC driver
does not correctly handle this case -- there is no synchronization
between uvc_v4l2_open / uvc_status_start and uvc_v4l2_release /
uvc_status_stop.

This patch adds a retry / timeout when uvc_status_open / usb_submit_urb
returns -EPERM. This usually means that usb_kill_urb is in progress, and
we just need to wait a while.

Signed-off-by: Shawn Nematbakhsh <shawnn@chromium.org>
---
 drivers/media/usb/uvc/uvc_v4l2.c | 10 +++++++++-
 drivers/media/usb/uvc/uvcvideo.h |  1 +
 2 files changed, 10 insertions(+), 1 deletion(-)

diff --git a/drivers/media/usb/uvc/uvc_v4l2.c b/drivers/media/usb/uvc/uvc_v4l2.c
index b2dc326..f1498a8 100644
--- a/drivers/media/usb/uvc/uvc_v4l2.c
+++ b/drivers/media/usb/uvc/uvc_v4l2.c
@@ -479,6 +479,7 @@ static int uvc_v4l2_open(struct file *file)
 {
 	struct uvc_streaming *stream;
 	struct uvc_fh *handle;
+	unsigned long timeout;
 	int ret = 0;
 
 	uvc_trace(UVC_TRACE_CALLS, "uvc_v4l2_open\n");
@@ -499,7 +500,14 @@ static int uvc_v4l2_open(struct file *file)
 	}
 
 	if (atomic_inc_return(&stream->dev->users) == 1) {
-		ret = uvc_status_start(stream->dev);
+		timeout = jiffies + msecs_to_jiffies(UVC_STATUS_START_TIMEOUT);
+		/* -EPERM means stop in progress, wait for completion */
+		do {
+			ret = uvc_status_start(stream->dev);
+			if (ret == -EPERM)
+				usleep_range(5000, 6000);
+		} while (ret == -EPERM && time_before(jiffies, timeout));
+
 		if (ret < 0) {
 			atomic_dec(&stream->dev->users);
 			usb_autopm_put_interface(stream->dev->intf);
diff --git a/drivers/media/usb/uvc/uvcvideo.h b/drivers/media/usb/uvc/uvcvideo.h
index af505fd..a47e1d3 100644
--- a/drivers/media/usb/uvc/uvcvideo.h
+++ b/drivers/media/usb/uvc/uvcvideo.h
@@ -122,6 +122,7 @@
 
 #define UVC_CTRL_CONTROL_TIMEOUT	300
 #define UVC_CTRL_STREAMING_TIMEOUT	5000
+#define UVC_STATUS_START_TIMEOUT	100
 
 /* Maximum allowed number of control mappings per device */
 #define UVC_MAX_CONTROL_MAPPINGS	1024
-- 
1.7.12.4


^ permalink raw reply related	[flat|nested] 5+ messages in thread
* [PATCH] [media] uvcvideo: Retry usb_submit_urb on -EPERM return
@ 2013-04-24  0:41 Shawn Nematbakhsh
  0 siblings, 0 replies; 5+ messages in thread
From: Shawn Nematbakhsh @ 2013-04-24  0:41 UTC (permalink / raw)
  To: Laurent Pinchart; +Cc: Mauro Carvalho Chehab, linux-media, linux-kernel, shawnn

From: Shawn Nematbakhsh <shawnn@chromium.org>

While usb_kill_urb is in progress, calls to usb_submit_urb will fail
with -EPERM (documented in Documentation/usb/URB.txt). The UVC driver
does not correctly handle this case -- there is no synchronization
between uvc_v4l2_open / uvc_status_start and uvc_v4l2_release /
uvc_status_stop.

This patch adds a retry / timeout when uvc_status_open / usb_submit_urb
returns -EPERM. This usually means that usb_kill_urb is in progress, and
we just need to wait a while.

Signed-off-by: Shawn Nematbakhsh <shawnn@chromium.org>
---
 drivers/media/usb/uvc/uvc_v4l2.c | 10 +++++++++-
 drivers/media/usb/uvc/uvcvideo.h |  1 +
 2 files changed, 10 insertions(+), 1 deletion(-)

diff --git a/drivers/media/usb/uvc/uvc_v4l2.c b/drivers/media/usb/uvc/uvc_v4l2.c
index b2dc326..f1498a8 100644
--- a/drivers/media/usb/uvc/uvc_v4l2.c
+++ b/drivers/media/usb/uvc/uvc_v4l2.c
@@ -479,6 +479,7 @@ static int uvc_v4l2_open(struct file *file)
 {
 	struct uvc_streaming *stream;
 	struct uvc_fh *handle;
+	unsigned long timeout;
 	int ret = 0;
 
 	uvc_trace(UVC_TRACE_CALLS, "uvc_v4l2_open\n");
@@ -499,7 +500,14 @@ static int uvc_v4l2_open(struct file *file)
 	}
 
 	if (atomic_inc_return(&stream->dev->users) == 1) {
-		ret = uvc_status_start(stream->dev);
+		timeout = jiffies + msecs_to_jiffies(UVC_STATUS_START_TIMEOUT);
+		/* -EPERM means stop in progress, wait for completion */
+		do {
+			ret = uvc_status_start(stream->dev);
+			if (ret == -EPERM)
+				usleep_range(5000, 6000);
+		} while (ret == -EPERM && time_before(jiffies, timeout));
+
 		if (ret < 0) {
 			atomic_dec(&stream->dev->users);
 			usb_autopm_put_interface(stream->dev->intf);
diff --git a/drivers/media/usb/uvc/uvcvideo.h b/drivers/media/usb/uvc/uvcvideo.h
index af505fd..a47e1d3 100644
--- a/drivers/media/usb/uvc/uvcvideo.h
+++ b/drivers/media/usb/uvc/uvcvideo.h
@@ -122,6 +122,7 @@
 
 #define UVC_CTRL_CONTROL_TIMEOUT	300
 #define UVC_CTRL_STREAMING_TIMEOUT	5000
+#define UVC_STATUS_START_TIMEOUT	100
 
 /* Maximum allowed number of control mappings per device */
 #define UVC_MAX_CONTROL_MAPPINGS	1024
-- 
1.7.12.4


^ permalink raw reply related	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2013-05-07 11:14 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2013-04-24  0:42 [PATCH] [media] uvcvideo: Retry usb_submit_urb on -EPERM return Shawn Nematbakhsh
2013-04-29 20:34 ` Laurent Pinchart
2013-05-03 23:00   ` Shawn Nematbakhsh
2013-05-07 11:14     ` Laurent Pinchart
  -- strict thread matches above, loose matches on Subject: below --
2013-04-24  0:41 Shawn Nematbakhsh

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox