[media] gspca: Fix locking issues related to suspend/resume
There are two bugs here: first the calls to stop0 (in gspca_suspend) and
gspca_init_transfer (in gspca_resume) need to be called with the usb_lock held.
That's true for the other places they are called and it is what subdrivers
expect. Quite a few will unlock the usb_lock in stop0 while waiting for a
worker thread to finish, and if usb_lock isn't held then that can cause a
kernel oops.
The other problem is that a worker thread needs to detect that it has to
halt due to a suspend. Otherwise it will just go on looping. So add tests
against gspca_dev->frozen in the worker threads that need it.
Hdg, 2 minor changes:
1) The finepix device is ok with stopping reading a frame halfway through,
so add frozen checks in all places where we also check if we're still
streaming
2) Use gspca_dev->dev instead of gspca_dev->present to check for disconnect
in all touched drivers. I plan to do this everywhere in the future, and
most relevant lines in the touched drivers are already modified by this
patch.
Signed-off-by: Hans Verkuil <hans.verkuil@cisco.com>
Signed-off-by: Hans de Goede <hdegoede@redhat.com>
Signed-off-by: Mauro Carvalho Chehab <mchehab@redhat.com>
diff --git a/drivers/media/video/gspca/gspca.c b/drivers/media/video/gspca/gspca.c
index 7669f27..a14c8f7 100644
--- a/drivers/media/video/gspca/gspca.c
+++ b/drivers/media/video/gspca/gspca.c
@@ -2499,8 +2499,11 @@
destroy_urbs(gspca_dev);
gspca_input_destroy_urb(gspca_dev);
gspca_set_alt0(gspca_dev);
- if (gspca_dev->sd_desc->stop0)
+ if (gspca_dev->sd_desc->stop0) {
+ mutex_lock(&gspca_dev->usb_lock);
gspca_dev->sd_desc->stop0(gspca_dev);
+ mutex_unlock(&gspca_dev->usb_lock);
+ }
return 0;
}
EXPORT_SYMBOL(gspca_suspend);
@@ -2508,7 +2511,7 @@
int gspca_resume(struct usb_interface *intf)
{
struct gspca_dev *gspca_dev = usb_get_intfdata(intf);
- int streaming;
+ int streaming, ret = 0;
gspca_dev->frozen = 0;
gspca_dev->sd_desc->init(gspca_dev);
@@ -2521,9 +2524,12 @@
streaming = gspca_dev->streaming;
gspca_dev->streaming = 0;
v4l2_ctrl_handler_setup(gspca_dev->vdev.ctrl_handler);
- if (streaming)
- return gspca_init_transfer(gspca_dev);
- return 0;
+ if (streaming) {
+ mutex_lock(&gspca_dev->queue_lock);
+ ret = gspca_init_transfer(gspca_dev);
+ mutex_unlock(&gspca_dev->queue_lock);
+ }
+ return ret;
}
EXPORT_SYMBOL(gspca_resume);
#endif