usb: gadget: composite: conditionally dequeue os_desc and setup requests
authorFelipe Balbi <balbi@ti.com>
Thu, 18 Sep 2014 15:01:55 +0000 (10:01 -0500)
committerFelipe Balbi <balbi@ti.com>
Mon, 3 Nov 2014 16:00:55 +0000 (10:00 -0600)
In case we unload a gadget driver while any of
os_desc_req or req are still pending, we need
to make sure to dequeue them.

By using our setup_pending and os_desc_pending
flags we achieve that in a way that doesn't
cause any regressions because we won't dequeue
a request which was already completed.

The original idea came from Li Jun's commit
f2267089ea17fa97b796b1b4247e3f8957655df3
(usb: gadget: composite: dequeue cdev->req
before free it in composite_dev_cleanup) which,
unfortunately, caused two regressions (kfree()
being called before usb_ep_dequeue() and calling
usb_ep_dequeue() when the request was already
completed). That commit also didn't take care
of os_desc_req which can fall into the same
situation so we must care for that one too.

Note that in order to make code slightly easier
to read, we introduce composite_ep_queue() which
hides details about how to fiddle with our pending
flags.

Signed-off-by: Felipe Balbi <balbi@ti.com>
drivers/usb/gadget/composite.c

index 09602dc902059d9f84c7a841e3adebb2033070a8..e071d580346ec340deb59e815fcdd740a5dd1bf9 100644 (file)
@@ -1246,10 +1246,49 @@ EXPORT_SYMBOL_GPL(usb_string_ids_n);
 
 static void composite_setup_complete(struct usb_ep *ep, struct usb_request *req)
 {
+       struct usb_composite_dev *cdev;
+
        if (req->status || req->actual != req->length)
                DBG((struct usb_composite_dev *) ep->driver_data,
                                "setup complete --> %d, %d/%d\n",
                                req->status, req->actual, req->length);
+
+       /*
+        * REVIST The same ep0 requests are shared with function drivers
+        * so they don't have to maintain the same ->complete() stubs.
+        *
+        * Because of that, we need to check for the validity of ->context
+        * here, even though we know we've set it to something useful.
+        */
+       if (!req->context)
+               return;
+
+       cdev = req->context;
+
+       if (cdev->req == req)
+               cdev->setup_pending = false;
+       else if (cdev->os_desc_req == req)
+               cdev->os_desc_pending = false;
+       else
+               WARN(1, "unknown request %p\n", req);
+}
+
+static int composite_ep0_queue(struct usb_composite_dev *cdev,
+               struct usb_request *req, gfp_t gfp_flags)
+{
+       int ret;
+
+       ret = usb_ep_queue(cdev->gadget->ep0, req, gfp_flags);
+       if (ret == 0) {
+               if (cdev->req == req)
+                       cdev->setup_pending = true;
+               else if (cdev->os_desc_req == req)
+                       cdev->os_desc_pending = true;
+               else
+                       WARN(1, "unknown request %p\n", req);
+       }
+
+       return ret;
 }
 
 static int count_ext_compat(struct usb_configuration *c)
@@ -1690,7 +1729,7 @@ unknown:
                        req->length = value;
                        req->context = cdev;
                        req->zero = value < w_length;
-                       value = usb_ep_queue(gadget->ep0, req, GFP_ATOMIC);
+                       value = composite_ep0_queue(cdev, req, GFP_ATOMIC);
                        if (value < 0) {
                                DBG(cdev, "ep_queue --> %d\n", value);
                                req->status = 0;
@@ -1762,7 +1801,7 @@ unknown:
                req->length = value;
                req->context = cdev;
                req->zero = value < w_length;
-               value = usb_ep_queue(gadget->ep0, req, GFP_ATOMIC);
+               value = composite_ep0_queue(cdev, req, GFP_ATOMIC);
                if (value < 0) {
                        DBG(cdev, "ep_queue --> %d\n", value);
                        req->status = 0;
@@ -1957,10 +1996,16 @@ void composite_dev_cleanup(struct usb_composite_dev *cdev)
                kfree(uc);
        }
        if (cdev->os_desc_req) {
+               if (cdev->os_desc_pending)
+                       usb_ep_dequeue(cdev->gadget->ep0, cdev->os_desc_req);
+
                kfree(cdev->os_desc_req->buf);
                usb_ep_free_request(cdev->gadget->ep0, cdev->os_desc_req);
        }
        if (cdev->req) {
+               if (cdev->setup_pending)
+                       usb_ep_dequeue(cdev->gadget->ep0, cdev->req);
+
                kfree(cdev->req->buf);
                usb_ep_free_request(cdev->gadget->ep0, cdev->req);
        }
@@ -2165,7 +2210,7 @@ void usb_composite_setup_continue(struct usb_composite_dev *cdev)
                DBG(cdev, "%s: Completing delayed status\n", __func__);
                req->length = 0;
                req->context = cdev;
-               value = usb_ep_queue(cdev->gadget->ep0, req, GFP_ATOMIC);
+               value = composite_ep0_queue(cdev, req, GFP_ATOMIC);
                if (value < 0) {
                        DBG(cdev, "ep_queue --> %d\n", value);
                        req->status = 0;