[PATCH v3 3/4] usb: gadget: uvc: disable stream when disconnected

From: Paul Elder
Date: Wed Jan 09 2019 - 02:11:22 EST


If the streamon ioctl is issued while the stream is already on, then the
kernel BUGs. This happens at the BUG_ON in uvc_video_alloc_requests
within the call stack from the ioctl handler for VIDIOC_STREAMON.

This can also be triggered by starting the stream and then physically
disconnecting usb. To fix this, do the streamoff procedures on usb
disconnect. Since uvcg_video_enable is not interrupt-safe, add an
interrupt-safe version uvcg_video_cancel, and use that.

Signed-off-by: Paul Elder <paul.elder@xxxxxxxxxxxxxxxx>
v2 Reviewed-by: Kieran Bingham <kieran.bingham@xxxxxxxxxxxxxxxx>
---
Changes in v3:

- added interrupt-safe uvcg_video_cancel and used instead of the
non-interrupt-save uvcg_video_enable 0

Changes in v2: Nothing

drivers/usb/gadget/function/f_uvc.c | 3 +++
drivers/usb/gadget/function/uvc_video.c | 13 +++++++++++++
drivers/usb/gadget/function/uvc_video.h | 2 ++
3 files changed, 18 insertions(+)

diff --git a/drivers/usb/gadget/function/f_uvc.c b/drivers/usb/gadget/function/f_uvc.c
index b407b10a95ed..4134117b5f81 100644
--- a/drivers/usb/gadget/function/f_uvc.c
+++ b/drivers/usb/gadget/function/f_uvc.c
@@ -369,9 +369,12 @@ uvc_function_disable(struct usb_function *f)
{
struct uvc_device *uvc = to_uvc(f);
struct v4l2_event v4l2_event;
+ struct uvc_video *video = &uvc->video;

uvcg_info(f, "%s()\n", __func__);

+ uvcg_video_cancel(video, 1);
+
memset(&v4l2_event, 0, sizeof(v4l2_event));
v4l2_event.type = UVC_EVENT_DISCONNECT;
v4l2_event_queue(&uvc->vdev, &v4l2_event);
diff --git a/drivers/usb/gadget/function/uvc_video.c b/drivers/usb/gadget/function/uvc_video.c
index 5c042f380708..5f3ed9e0b5ad 100644
--- a/drivers/usb/gadget/function/uvc_video.c
+++ b/drivers/usb/gadget/function/uvc_video.c
@@ -348,6 +348,19 @@ int uvcg_video_pump(struct uvc_video *video)
return 0;
}

+int uvcg_video_cancel(struct uvc_video *video, int disconnect)
+{
+ unsigned int i;
+
+ for (i = 0; i < UVC_NUM_REQUESTS; ++i)
+ if (video->req[i])
+ usb_ep_dequeue(video->ep, video->req[i]);
+
+ uvc_video_free_requests(video);
+ uvcg_queue_cancel(&video->queue, disconnect);
+ return 0;
+}
+
/*
* Enable or disable the video stream.
*/
diff --git a/drivers/usb/gadget/function/uvc_video.h b/drivers/usb/gadget/function/uvc_video.h
index 278dc52c7604..1f310fa58ce5 100644
--- a/drivers/usb/gadget/function/uvc_video.h
+++ b/drivers/usb/gadget/function/uvc_video.h
@@ -16,6 +16,8 @@ struct uvc_video;

int uvcg_video_pump(struct uvc_video *video);

+int uvcg_video_cancel(struct uvc_video *video, int disconnect);
+
int uvcg_video_enable(struct uvc_video *video, int enable);

int uvcg_video_init(struct uvc_video *video, struct uvc_device *uvc);
--
2.20.1