Merge branch 'drm-patches' of git://git.kernel.org/pub/scm/linux/kernel/git/airlied...
[pandora-kernel.git] / drivers / usb / core / hub.c
index f6e6921..2651c2e 100644 (file)
@@ -22,6 +22,7 @@
 #include <linux/usbdevice_fs.h>
 #include <linux/kthread.h>
 #include <linux/mutex.h>
+#include <linux/freezer.h>
 
 #include <asm/semaphore.h>
 #include <asm/uaccess.h>
@@ -65,11 +66,10 @@ struct usb_hub {
        unsigned                limited_power:1;
        unsigned                quiescing:1;
        unsigned                activating:1;
-       unsigned                resume_root_hub:1;
 
        unsigned                has_indicators:1;
        u8                      indicator[USB_MAXCHILDREN];
-       struct work_struct      leds;
+       struct delayed_work     leds;
 };
 
 
@@ -219,9 +219,10 @@ static void set_port_led(
 
 #define        LED_CYCLE_PERIOD        ((2*HZ)/3)
 
-static void led_work (void *__hub)
+static void led_work (struct work_struct *work)
 {
-       struct usb_hub          *hub = __hub;
+       struct usb_hub          *hub =
+               container_of(work, struct usb_hub, leds.work);
        struct usb_device       *hdev = hub->hdev;
        unsigned                i;
        unsigned                changed = 0;
@@ -328,6 +329,9 @@ static void kick_khubd(struct usb_hub *hub)
 {
        unsigned long   flags;
 
+       /* Suppress autosuspend until khubd runs */
+       to_usb_interface(hub->intfdev)->pm_usage_cnt = 1;
+
        spin_lock_irqsave(&hub_event_lock, flags);
        if (list_empty(&hub->event_list)) {
                list_add_tail(&hub->event_list, &hub_event_list);
@@ -403,9 +407,10 @@ hub_clear_tt_buffer (struct usb_device *hdev, u16 devinfo, u16 tt)
  * talking to TTs must queue control transfers (not just bulk and iso), so
  * both can talk to the same hub concurrently.
  */
-static void hub_tt_kevent (void *arg)
+static void hub_tt_kevent (struct work_struct *work)
 {
-       struct usb_hub          *hub = arg;
+       struct usb_hub          *hub =
+               container_of(work, struct usb_hub, tt.kevent);
        unsigned long           flags;
 
        spin_lock_irqsave (&hub->tt.lock, flags);
@@ -456,7 +461,7 @@ void usb_hub_tt_clear_buffer (struct usb_device *udev, int pipe)
         * since each TT has "at least two" buffers that can need it (and
         * there can be many TTs per hub).  even if they're uncommon.
         */
-       if ((clear = kmalloc (sizeof *clear, SLAB_ATOMIC)) == NULL) {
+       if ((clear = kmalloc (sizeof *clear, GFP_ATOMIC)) == NULL) {
                dev_err (&udev->dev, "can't save CLEAR_TT_BUFFER state\n");
                /* FIXME recover somehow ... RESET_TT? */
                return;
@@ -509,7 +514,6 @@ static void hub_quiesce(struct usb_hub *hub)
        /* (nonblocking) khubd and related activity won't re-trigger */
        hub->quiescing = 1;
        hub->activating = 0;
-       hub->resume_root_hub = 0;
 
        /* (blocking) stop khubd and related activity */
        usb_kill_urb(hub->urb);
@@ -525,7 +529,7 @@ static void hub_activate(struct usb_hub *hub)
 
        hub->quiescing = 0;
        hub->activating = 1;
-       hub->resume_root_hub = 0;
+
        status = usb_submit_urb(hub->urb, GFP_NOIO);
        if (status < 0)
                dev_err(hub->intfdev, "activate --> %d\n", status);
@@ -693,7 +697,7 @@ static int hub_configure(struct usb_hub *hub,
 
        spin_lock_init (&hub->tt.lock);
        INIT_LIST_HEAD (&hub->tt.clear_list);
-       INIT_WORK (&hub->tt.kevent, hub_tt_kevent, hub);
+       INIT_WORK (&hub->tt.kevent, hub_tt_kevent);
        switch (hdev->descriptor.bDeviceProtocol) {
                case 0:
                        break;
@@ -937,9 +941,10 @@ descriptor_error:
        INIT_LIST_HEAD(&hub->event_list);
        hub->intfdev = &intf->dev;
        hub->hdev = hdev;
-       INIT_WORK(&hub->leds, led_work, hub);
+       INIT_DELAYED_WORK(&hub->leds, led_work);
 
        usb_set_intfdata (intf, hub);
+       intf->needs_remote_wakeup = 1;
 
        if (hdev->speed == USB_SPEED_HIGH)
                highspeed_hubs++;
@@ -1037,6 +1042,8 @@ static void recursively_mark_NOTATTACHED(struct usb_device *udev)
                if (udev->children[i])
                        recursively_mark_NOTATTACHED(udev->children[i]);
        }
+       if (udev->state == USB_STATE_SUSPENDED)
+               udev->discon_suspended = 1;
        udev->state = USB_STATE_NOTATTACHED;
 }
 
@@ -1226,6 +1233,14 @@ void usb_disconnect(struct usb_device **pdev)
        *pdev = NULL;
        spin_unlock_irq(&device_state_lock);
 
+       /* Decrement the parent's count of unsuspended children */
+       if (udev->parent) {
+               usb_pm_lock(udev);
+               if (!udev->discon_suspended)
+                       usb_autosuspend_device(udev->parent);
+               usb_pm_unlock(udev);
+       }
+
        put_device(&udev->dev);
 }
 
@@ -1354,6 +1369,10 @@ static int __usb_new_device(void *void_data)
                goto fail;
        }
 
+       /* Increment the parent's count of unsuspended children */
+       if (udev->parent)
+               usb_autoresume_device(udev->parent);
+
 exit:
        module_put(THIS_MODULE);
        return err;
@@ -1407,10 +1426,12 @@ static int hub_port_status(struct usb_hub *hub, int port1,
        int ret;
 
        ret = get_port_status(hub->hdev, port1, &hub->status->port);
-       if (ret < 0)
+       if (ret < 4) {
                dev_err (hub->intfdev,
                        "%s failed (err = %d)\n", __FUNCTION__, ret);
-       else {
+               if (ret >= 0)
+                       ret = -EIO;
+       } else {
                *status = le16_to_cpu(hub->status->port.wPortStatus);
                *change = le16_to_cpu(hub->status->port.wPortChange); 
                ret = 0;
@@ -1758,6 +1779,12 @@ static int
 hub_port_resume(struct usb_hub *hub, int port1, struct usb_device *udev)
 {
        int     status;
+       u16     portchange, portstatus;
+
+       /* Skip the initial Clear-Suspend step for a remote wakeup */
+       status = hub_port_status(hub, port1, &portstatus, &portchange);
+       if (status == 0 && !(portstatus & USB_PORT_STAT_SUSPEND))
+               goto SuspendCleared;
 
        // dev_dbg(hub->intfdev, "resume port %d\n", port1);
 
@@ -1771,9 +1798,6 @@ hub_port_resume(struct usb_hub *hub, int port1, struct usb_device *udev)
                        "can't resume port %d, status %d\n",
                        port1, status);
        } else {
-               u16             devstatus;
-               u16             portchange;
-
                /* drive resume for at least 20 msec */
                if (udev)
                        dev_dbg(&udev->dev, "usb %sresume\n",
@@ -1788,16 +1812,15 @@ hub_port_resume(struct usb_hub *hub, int port1, struct usb_device *udev)
                 * stop resume signaling.  Then finish the resume
                 * sequence.
                 */
-               devstatus = portchange = 0;
-               status = hub_port_status(hub, port1,
-                               &devstatus, &portchange);
+               status = hub_port_status(hub, port1, &portstatus, &portchange);
+SuspendCleared:
                if (status < 0
-                               || (devstatus & LIVE_FLAGS) != LIVE_FLAGS
-                               || (devstatus & USB_PORT_STAT_SUSPEND) != 0
+                               || (portstatus & LIVE_FLAGS) != LIVE_FLAGS
+                               || (portstatus & USB_PORT_STAT_SUSPEND) != 0
                                ) {
                        dev_dbg(hub->intfdev,
                                "port %d status %04x.%04x after resume, %d\n",
-                               port1, portchange, devstatus, status);
+                               port1, portchange, portstatus, status);
                        if (status >= 0)
                                status = -ENODEV;
                } else {
@@ -1858,23 +1881,16 @@ static int remote_wakeup(struct usb_device *udev)
 {
        int     status = 0;
 
-       /* All this just to avoid sending a port-resume message
-        * to the parent hub! */
-
        usb_lock_device(udev);
-       usb_pm_lock(udev);
        if (udev->state == USB_STATE_SUSPENDED) {
                dev_dbg(&udev->dev, "usb %sresume\n", "wakeup-");
-               /* TRSMRCY = 10 msec */
-               msleep(10);
-               status = finish_port_resume(udev);
+               status = usb_autoresume_device(udev);
+
+               /* Give the interface drivers a chance to do something,
+                * then autosuspend the device again. */
                if (status == 0)
-                       udev->dev.power.power_state.event = PM_EVENT_ON;
+                       usb_autosuspend_device(udev);
        }
-       usb_pm_unlock(udev);
-
-       if (status == 0)
-               usb_autoresume_device(udev, 0);
        usb_unlock_device(udev);
        return status;
 }
@@ -1938,6 +1954,8 @@ static int hub_suspend(struct usb_interface *intf, pm_message_t msg)
                }
        }
 
+       dev_dbg(&intf->dev, "%s\n", __FUNCTION__);
+
        /* "global suspend" of the downstream HC-to-USB interface */
        if (!hdev->parent) {
                struct usb_bus  *bus = hdev->bus;
@@ -1960,10 +1978,12 @@ static int hub_suspend(struct usb_interface *intf, pm_message_t msg)
 
 static int hub_resume(struct usb_interface *intf)
 {
-       struct usb_device       *hdev = interface_to_usbdev(intf);
        struct usb_hub          *hub = usb_get_intfdata (intf);
+       struct usb_device       *hdev = hub->hdev;
        int                     status;
 
+       dev_dbg(&intf->dev, "%s\n", __FUNCTION__);
+
        /* "global resume" of the downstream HC-to-USB interface */
        if (!hdev->parent) {
                struct usb_bus  *bus = hdev->bus;
@@ -2002,7 +2022,6 @@ void usb_resume_root_hub(struct usb_device *hdev)
 {
        struct usb_hub *hub = hdev_to_hub(hdev);
 
-       hub->resume_root_hub = 1;
        kick_khubd(hub);
 }
 
@@ -2353,7 +2372,7 @@ check_highspeed (struct usb_hub *hub, struct usb_device *udev, int port1)
        struct usb_qualifier_descriptor *qual;
        int                             status;
 
-       qual = kmalloc (sizeof *qual, SLAB_KERNEL);
+       qual = kmalloc (sizeof *qual, GFP_KERNEL);
        if (qual == NULL)
                return;
 
@@ -2365,7 +2384,7 @@ check_highspeed (struct usb_hub *hub, struct usb_device *udev, int port1)
                /* hub LEDs are probably harder to miss than syslog */
                if (hub->has_indicators) {
                        hub->indicator[port1-1] = INDICATOR_GREEN_BLINK;
-                       schedule_work (&hub->leds);
+                       schedule_delayed_work (&hub->leds, 0);
                }
        }
        kfree(qual);
@@ -2539,7 +2558,7 @@ static void hub_port_connect_change(struct usb_hub *hub, int port1,
                                if (hub->has_indicators) {
                                        hub->indicator[port1-1] =
                                                INDICATOR_AMBER_BLINK;
-                                       schedule_work (&hub->leds);
+                                       schedule_delayed_work (&hub->leds, 0);
                                }
                                status = -ENOTCONN;     /* Don't retry */
                                goto loop_disable;
@@ -2639,16 +2658,13 @@ static void hub_events(void)
                intf = to_usb_interface(hub->intfdev);
                hub_dev = &intf->dev;
 
-               i = hub->resume_root_hub;
-
-               dev_dbg(hub_dev, "state %d ports %d chg %04x evt %04x%s\n",
+               dev_dbg(hub_dev, "state %d ports %d chg %04x evt %04x\n",
                                hdev->state, hub->descriptor
                                        ? hub->descriptor->bNbrPorts
                                        : 0,
                                /* NOTE: expects max 15 ports... */
                                (u16) hub->change_bits[0],
-                               (u16) hub->event_bits[0],
-                               i ? ", resume root" : "");
+                               (u16) hub->event_bits[0]);
 
                usb_get_intf(intf);
                spin_unlock_irq(&hub_event_lock);
@@ -2669,16 +2685,16 @@ static void hub_events(void)
                        goto loop;
                }
 
-               /* Is this is a root hub wanting to reactivate the downstream
-                * ports?  If so, be sure the interface resumes even if its
-                * stub "device" node was never suspended.
-                */
-               if (i)
-                       usb_autoresume_device(hdev, 0);
+               /* Autoresume */
+               ret = usb_autopm_get_interface(intf);
+               if (ret) {
+                       dev_dbg(hub_dev, "Can't autoresume: %d\n", ret);
+                       goto loop;
+               }
 
-               /* If this is an inactive or suspended hub, do nothing */
+               /* If this is an inactive hub, do nothing */
                if (hub->quiescing)
-                       goto loop;
+                       goto loop_autopm;
 
                if (hub->error) {
                        dev_dbg (hub_dev, "resetting for error %d\n",
@@ -2688,7 +2704,7 @@ static void hub_events(void)
                        if (ret) {
                                dev_dbg (hub_dev,
                                        "error resetting hub: %d\n", ret);
-                               goto loop;
+                               goto loop_autopm;
                        }
 
                        hub->nerrors = 0;
@@ -2816,6 +2832,10 @@ static void hub_events(void)
                if (!hdev->parent && !hub->busy_bits[0])
                        usb_enable_root_hub_irq(hdev->bus);
 
+loop_autopm:
+               /* Allow autosuspend if we're not going to run again */
+               if (list_empty(&hub->event_list))
+                       usb_autopm_enable(intf);
 loop:
                usb_unlock_device(hdev);
                usb_put_intf(intf);
@@ -2857,6 +2877,7 @@ static struct usb_driver hub_driver = {
        .post_reset =   hub_post_reset,
        .ioctl =        hub_ioctl,
        .id_table =     hub_id_table,
+       .supports_autosuspend = 1,
 };
 
 int usb_hub_init(void)
@@ -2902,7 +2923,7 @@ static int config_descriptors_changed(struct usb_device *udev)
                if (len < le16_to_cpu(udev->config[index].desc.wTotalLength))
                        len = le16_to_cpu(udev->config[index].desc.wTotalLength);
        }
-       buf = kmalloc (len, SLAB_KERNEL);
+       buf = kmalloc (len, GFP_KERNEL);
        if (buf == NULL) {
                dev_err(&udev->dev, "no mem to re-read configs after reset\n");
                /* assume the worst */
@@ -3081,7 +3102,7 @@ int usb_reset_composite_device(struct usb_device *udev,
        }
 
        /* Prevent autosuspend during the reset */
-       usb_autoresume_device(udev, 1);
+       usb_autoresume_device(udev);
 
        if (iface && iface->condition != USB_INTERFACE_BINDING)
                iface = NULL;
@@ -3124,7 +3145,7 @@ int usb_reset_composite_device(struct usb_device *udev,
                }
        }
 
-       usb_autosuspend_device(udev, 1);
+       usb_autosuspend_device(udev);
        return ret;
 }
 EXPORT_SYMBOL(usb_reset_composite_device);