[PATCH] USB: gadget-serial: fix a deadlock when closing the serial device
[safe/jmp/linux-2.6] / drivers / usb / gadget / dummy_hcd.c
index c605aae..42ce41d 100644 (file)
@@ -49,8 +49,7 @@
 #include <linux/timer.h>
 #include <linux/list.h>
 #include <linux/interrupt.h>
-#include <linux/version.h>
-
+#include <linux/platform_device.h>
 #include <linux/usb.h>
 #include <linux/usb_gadget.h>
 
@@ -139,7 +138,7 @@ static const char *const ep_name [] = {
        /* or like sa1100: two fixed function endpoints */
        "ep1out-bulk", "ep2in-bulk",
 };
-#define DUMMY_ENDPOINTS        (sizeof(ep_name)/sizeof(char *))
+#define DUMMY_ENDPOINTS        ARRAY_SIZE(ep_name)
 
 /*-------------------------------------------------------------------------*/
 
@@ -479,10 +478,9 @@ dummy_alloc_request (struct usb_ep *_ep, gfp_t mem_flags)
                return NULL;
        ep = usb_ep_to_dummy_ep (_ep);
 
-       req = kmalloc (sizeof *req, mem_flags);
+       req = kzalloc(sizeof(*req), mem_flags);
        if (!req)
                return NULL;
-       memset (req, 0, sizeof *req);
        INIT_LIST_HEAD (&req->queue);
        return &req->req;
 }
@@ -897,7 +895,7 @@ dummy_gadget_release (struct device *dev)
 #endif
 }
 
-static int dummy_udc_probe (struct device *dev)
+static int dummy_udc_probe (struct platform_device *pdev)
 {
        struct dummy    *dum = the_controller;
        int             rc;
@@ -910,7 +908,7 @@ static int dummy_udc_probe (struct device *dev)
        dum->gadget.is_otg = (dummy_to_hcd(dum)->self.otg_port != 0);
 
        strcpy (dum->gadget.dev.bus_id, "gadget");
-       dum->gadget.dev.parent = dev;
+       dum->gadget.dev.parent = &pdev->dev;
        dum->gadget.dev.release = dummy_gadget_release;
        rc = device_register (&dum->gadget.dev);
        if (rc < 0)
@@ -920,58 +918,60 @@ static int dummy_udc_probe (struct device *dev)
        usb_bus_get (&dummy_to_hcd (dum)->self);
 #endif
 
-       dev_set_drvdata (dev, dum);
+       platform_set_drvdata (pdev, dum);
        device_create_file (&dum->gadget.dev, &dev_attr_function);
        return rc;
 }
 
-static int dummy_udc_remove (struct device *dev)
+static int dummy_udc_remove (struct platform_device *pdev)
 {
-       struct dummy    *dum = dev_get_drvdata (dev);
+       struct dummy    *dum = platform_get_drvdata (pdev);
 
-       dev_set_drvdata (dev, NULL);
+       platform_set_drvdata (pdev, NULL);
        device_remove_file (&dum->gadget.dev, &dev_attr_function);
        device_unregister (&dum->gadget.dev);
        return 0;
 }
 
-static int dummy_udc_suspend (struct device *dev, pm_message_t state)
+static int dummy_udc_suspend (struct platform_device *pdev, pm_message_t state)
 {
-       struct dummy    *dum = dev_get_drvdata(dev);
+       struct dummy    *dum = platform_get_drvdata(pdev);
 
-       dev_dbg (dev, "%s\n", __FUNCTION__);
+       dev_dbg (&pdev->dev, "%s\n", __FUNCTION__);
        spin_lock_irq (&dum->lock);
        dum->udc_suspended = 1;
        set_link_state (dum);
        spin_unlock_irq (&dum->lock);
 
-       dev->power.power_state = state;
+       pdev->dev.power.power_state = state;
        usb_hcd_poll_rh_status (dummy_to_hcd (dum));
        return 0;
 }
 
-static int dummy_udc_resume (struct device *dev)
+static int dummy_udc_resume (struct platform_device *pdev)
 {
-       struct dummy    *dum = dev_get_drvdata(dev);
+       struct dummy    *dum = platform_get_drvdata(pdev);
 
-       dev_dbg (dev, "%s\n", __FUNCTION__);
+       dev_dbg (&pdev->dev, "%s\n", __FUNCTION__);
        spin_lock_irq (&dum->lock);
        dum->udc_suspended = 0;
        set_link_state (dum);
        spin_unlock_irq (&dum->lock);
 
-       dev->power.power_state = PMSG_ON;
+       pdev->dev.power.power_state = PMSG_ON;
        usb_hcd_poll_rh_status (dummy_to_hcd (dum));
        return 0;
 }
 
-static struct device_driver dummy_udc_driver = {
-       .name           = (char *) gadget_name,
-       .bus            = &platform_bus_type,
+static struct platform_driver dummy_udc_driver = {
        .probe          = dummy_udc_probe,
        .remove         = dummy_udc_remove,
        .suspend        = dummy_udc_suspend,
        .resume         = dummy_udc_resume,
+       .driver         = {
+               .name   = (char *) gadget_name,
+               .owner  = THIS_MODULE,
+       },
 };
 
 /*-------------------------------------------------------------------------*/
@@ -1575,7 +1575,7 @@ static int dummy_hub_status (struct usb_hcd *hcd, char *buf)
        dum = hcd_to_dummy (hcd);
 
        spin_lock_irqsave (&dum->lock, flags);
-       if (hcd->state != HC_STATE_RUNNING)
+       if (!test_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags))
                goto done;
 
        if (dum->resuming && time_after_eq (jiffies, dum->re_timeout)) {
@@ -1622,7 +1622,7 @@ static int dummy_hub_control (
        int             retval = 0;
        unsigned long   flags;
 
-       if (hcd->state != HC_STATE_RUNNING)
+       if (!test_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags))
                return -ETIMEDOUT;
 
        dum = hcd_to_dummy (hcd);
@@ -1755,9 +1755,12 @@ static int dummy_bus_suspend (struct usb_hcd *hcd)
 {
        struct dummy *dum = hcd_to_dummy (hcd);
 
+       dev_dbg (&hcd->self.root_hub->dev, "%s\n", __FUNCTION__);
+
        spin_lock_irq (&dum->lock);
        dum->rh_state = DUMMY_RH_SUSPENDED;
        set_link_state (dum);
+       hcd->state = HC_STATE_SUSPENDED;
        spin_unlock_irq (&dum->lock);
        return 0;
 }
@@ -1765,14 +1768,23 @@ static int dummy_bus_suspend (struct usb_hcd *hcd)
 static int dummy_bus_resume (struct usb_hcd *hcd)
 {
        struct dummy *dum = hcd_to_dummy (hcd);
+       int rc = 0;
+
+       dev_dbg (&hcd->self.root_hub->dev, "%s\n", __FUNCTION__);
 
        spin_lock_irq (&dum->lock);
-       dum->rh_state = DUMMY_RH_RUNNING;
-       set_link_state (dum);
-       if (!list_empty(&dum->urbp_list))
-               mod_timer (&dum->timer, jiffies);
+       if (!test_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags)) {
+               dev_warn (&hcd->self.root_hub->dev, "HC isn't running!\n");
+               rc = -ENODEV;
+       } else {
+               dum->rh_state = DUMMY_RH_RUNNING;
+               set_link_state (dum);
+               if (!list_empty(&dum->urbp_list))
+                       mod_timer (&dum->timer, jiffies);
+               hcd->state = HC_STATE_RUNNING;
+       }
        spin_unlock_irq (&dum->lock);
-       return 0;
+       return rc;
 }
 
 /*-------------------------------------------------------------------------*/
@@ -1898,14 +1910,14 @@ static const struct hc_driver dummy_hcd = {
        .bus_resume =           dummy_bus_resume,
 };
 
-static int dummy_hcd_probe (struct device *dev)
+static int dummy_hcd_probe(struct platform_device *pdev)
 {
        struct usb_hcd          *hcd;
        int                     retval;
 
-       dev_info (dev, "%s, driver " DRIVER_VERSION "\n", driver_desc);
+       dev_info(&pdev->dev, "%s, driver " DRIVER_VERSION "\n", driver_desc);
 
-       hcd = usb_create_hcd (&dummy_hcd, dev, dev->bus_id);
+       hcd = usb_create_hcd(&dummy_hcd, &pdev->dev, pdev->dev.bus_id);
        if (!hcd)
                return -ENOMEM;
        the_controller = hcd_to_dummy (hcd);
@@ -1918,47 +1930,56 @@ static int dummy_hcd_probe (struct device *dev)
        return retval;
 }
 
-static int dummy_hcd_remove (struct device *dev)
+static int dummy_hcd_remove (struct platform_device *pdev)
 {
        struct usb_hcd          *hcd;
 
-       hcd = dev_get_drvdata (dev);
+       hcd = platform_get_drvdata (pdev);
        usb_remove_hcd (hcd);
        usb_put_hcd (hcd);
        the_controller = NULL;
        return 0;
 }
 
-static int dummy_hcd_suspend (struct device *dev, pm_message_t state)
+static int dummy_hcd_suspend (struct platform_device *pdev, pm_message_t state)
 {
        struct usb_hcd          *hcd;
+       struct dummy            *dum;
+       int                     rc = 0;
 
-       dev_dbg (dev, "%s\n", __FUNCTION__);
-       hcd = dev_get_drvdata (dev);
+       dev_dbg (&pdev->dev, "%s\n", __FUNCTION__);
 
-       hcd->state = HC_STATE_SUSPENDED;
-       return 0;
+       hcd = platform_get_drvdata (pdev);
+       dum = hcd_to_dummy (hcd);
+       if (dum->rh_state == DUMMY_RH_RUNNING) {
+               dev_warn(&pdev->dev, "Root hub isn't suspended!\n");
+               rc = -EBUSY;
+       } else
+               clear_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
+       return rc;
 }
 
-static int dummy_hcd_resume (struct device *dev)
+static int dummy_hcd_resume (struct platform_device *pdev)
 {
        struct usb_hcd          *hcd;
 
-       dev_dbg (dev, "%s\n", __FUNCTION__);
-       hcd = dev_get_drvdata (dev);
-       hcd->state = HC_STATE_RUNNING;
+       dev_dbg (&pdev->dev, "%s\n", __FUNCTION__);
 
+       hcd = platform_get_drvdata (pdev);
+       set_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
        usb_hcd_poll_rh_status (hcd);
        return 0;
 }
 
-static struct device_driver dummy_hcd_driver = {
-       .name           = (char *) driver_name,
-       .bus            = &platform_bus_type,
+static struct platform_driver dummy_hcd_driver = {
        .probe          = dummy_hcd_probe,
        .remove         = dummy_hcd_remove,
        .suspend        = dummy_hcd_suspend,
        .resume         = dummy_hcd_resume,
+       .driver         = {
+               .name   = (char *) driver_name,
+               .owner  = THIS_MODULE,
+       },
 };
 
 /*-------------------------------------------------------------------------*/
@@ -1994,11 +2015,11 @@ static int __init init (void)
        if (usb_disabled ())
                return -ENODEV;
 
-       retval = driver_register (&dummy_hcd_driver);
+       retval = platform_driver_register (&dummy_hcd_driver);
        if (retval < 0)
                return retval;
 
-       retval = driver_register (&dummy_udc_driver);
+       retval = platform_driver_register (&dummy_udc_driver);
        if (retval < 0)
                goto err_register_udc_driver;
 
@@ -2014,9 +2035,9 @@ static int __init init (void)
 err_register_udc:
        platform_device_unregister (&the_hcd_pdev);
 err_register_hcd:
-       driver_unregister (&dummy_udc_driver);
+       platform_driver_unregister (&dummy_udc_driver);
 err_register_udc_driver:
-       driver_unregister (&dummy_hcd_driver);
+       platform_driver_unregister (&dummy_hcd_driver);
        return retval;
 }
 module_init (init);
@@ -2025,7 +2046,7 @@ static void __exit cleanup (void)
 {
        platform_device_unregister (&the_udc_pdev);
        platform_device_unregister (&the_hcd_pdev);
-       driver_unregister (&dummy_udc_driver);
-       driver_unregister (&dummy_hcd_driver);
+       platform_driver_unregister (&dummy_udc_driver);
+       platform_driver_unregister (&dummy_hcd_driver);
 }
 module_exit (cleanup);