[PATCH] USB: remove .owner field from struct usb_driver
[linux-2.6.git] / drivers / usb / core / hub.c
index 3c8d8d1..40c6c50 100644 (file)
@@ -9,11 +9,6 @@
  */
 
 #include <linux/config.h>
-#ifdef CONFIG_USB_DEBUG
-       #define DEBUG
-#else
-       #undef DEBUG
-#endif
 #include <linux/kernel.h>
 #include <linux/errno.h>
 #include <linux/module.h>
@@ -436,9 +431,10 @@ static void hub_power_on(struct usb_hub *hub)
 {
        int port1;
        unsigned pgood_delay = hub->descriptor->bPwrOn2PwrGood * 2;
+       u16 wHubCharacteristics = le16_to_cpu(hub->descriptor->wHubCharacteristics);
 
        /* if hub supports power switching, enable power on each port */
-       if ((hub->descriptor->wHubCharacteristics & HUB_CHAR_LPSM) < 2) {
+       if ((wHubCharacteristics & HUB_CHAR_LPSM) < 2) {
                dev_dbg(hub->intfdev, "enabling power on all ports\n");
                for (port1 = 1; port1 <= hub->descriptor->bNbrPorts; port1++)
                        set_port_feature(hub->hdev, port1,
@@ -525,6 +521,7 @@ static int hub_configure(struct usb_hub *hub,
        struct usb_device *hdev = hub->hdev;
        struct device *hub_dev = hub->intfdev;
        u16 hubstatus, hubchange;
+       u16 wHubCharacteristics;
        unsigned int pipe;
        int maxp, ret;
        char *message;
@@ -570,9 +567,9 @@ static int hub_configure(struct usb_hub *hub,
        dev_info (hub_dev, "%d port%s detected\n", hdev->maxchild,
                (hdev->maxchild == 1) ? "" : "s");
 
-       le16_to_cpus(&hub->descriptor->wHubCharacteristics);
+       wHubCharacteristics = le16_to_cpu(hub->descriptor->wHubCharacteristics);
 
-       if (hub->descriptor->wHubCharacteristics & HUB_CHAR_COMPOUND) {
+       if (wHubCharacteristics & HUB_CHAR_COMPOUND) {
                int     i;
                char    portstr [USB_MAXCHILDREN + 1];
 
@@ -585,7 +582,7 @@ static int hub_configure(struct usb_hub *hub,
        } else
                dev_dbg(hub_dev, "standalone hub\n");
 
-       switch (hub->descriptor->wHubCharacteristics & HUB_CHAR_LPSM) {
+       switch (wHubCharacteristics & HUB_CHAR_LPSM) {
                case 0x00:
                        dev_dbg(hub_dev, "ganged power switching\n");
                        break;
@@ -598,7 +595,7 @@ static int hub_configure(struct usb_hub *hub,
                        break;
        }
 
-       switch (hub->descriptor->wHubCharacteristics & HUB_CHAR_OCPM) {
+       switch (wHubCharacteristics & HUB_CHAR_OCPM) {
                case 0x00:
                        dev_dbg(hub_dev, "global over-current protection\n");
                        break;
@@ -638,7 +635,7 @@ static int hub_configure(struct usb_hub *hub,
        }
 
        /* Note 8 FS bit times == (8 bits / 12000000 bps) ~= 666ns */
-       switch (hub->descriptor->wHubCharacteristics & HUB_CHAR_TTTT) {
+       switch (wHubCharacteristics & HUB_CHAR_TTTT) {
                case HUB_TTTT_8_BITS:
                        if (hdev->descriptor.bDeviceProtocol != 0) {
                                hub->tt.think_time = 666;
@@ -668,7 +665,7 @@ static int hub_configure(struct usb_hub *hub,
        }
 
        /* probe() zeroes hub->indicator[] */
-       if (hub->descriptor->wHubCharacteristics & HUB_CHAR_PORTIND) {
+       if (wHubCharacteristics & HUB_CHAR_PORTIND) {
                hub->has_indicators = 1;
                dev_dbg(hub_dev, "Port indicators are supported\n");
        }
@@ -713,7 +710,7 @@ static int hub_configure(struct usb_hub *hub,
                        (hubstatus & HUB_STATUS_LOCAL_POWER)
                        ? "lost (inactive)" : "good");
 
-       if ((hub->descriptor->wHubCharacteristics & HUB_CHAR_OCPM) == 0)
+       if ((wHubCharacteristics & HUB_CHAR_OCPM) == 0)
                dev_dbg(hub_dev, "%sover-current condition exists\n",
                        (hubstatus & HUB_STATUS_OVERCURRENT) ? "" : "no ");
 
@@ -863,14 +860,12 @@ descriptor_error:
        /* We found a hub */
        dev_info (&intf->dev, "USB hub found\n");
 
-       hub = kmalloc(sizeof(*hub), GFP_KERNEL);
+       hub = kzalloc(sizeof(*hub), GFP_KERNEL);
        if (!hub) {
                dev_dbg (&intf->dev, "couldn't kmalloc hub struct\n");
                return -ENOMEM;
        }
 
-       memset(hub, 0, sizeof(*hub));
-
        INIT_LIST_HEAD(&hub->event_list);
        hub->intfdev = &intf->dev;
        hub->hdev = hdev;
@@ -1044,6 +1039,39 @@ void usb_set_device_state(struct usb_device *udev,
 EXPORT_SYMBOL(usb_set_device_state);
 
 
+#ifdef CONFIG_PM
+
+/**
+ * usb_root_hub_lost_power - called by HCD if the root hub lost Vbus power
+ * @rhdev: struct usb_device for the root hub
+ *
+ * The USB host controller driver calls this function when its root hub
+ * is resumed and Vbus power has been interrupted or the controller
+ * has been reset.  The routine marks all the children of the root hub
+ * as NOTATTACHED and marks logical connect-change events on their ports.
+ */
+void usb_root_hub_lost_power(struct usb_device *rhdev)
+{
+       struct usb_hub *hub;
+       int port1;
+       unsigned long flags;
+
+       dev_warn(&rhdev->dev, "root hub lost power or was reset\n");
+       spin_lock_irqsave(&device_state_lock, flags);
+       hub = hdev_to_hub(rhdev);
+       for (port1 = 1; port1 <= rhdev->maxchild; ++port1) {
+               if (rhdev->children[port1 - 1]) {
+                       recursively_mark_NOTATTACHED(
+                                       rhdev->children[port1 - 1]);
+                       set_bit(port1, hub->change_bits);
+               }
+       }
+       spin_unlock_irqrestore(&device_state_lock, flags);
+}
+EXPORT_SYMBOL_GPL(usb_root_hub_lost_power);
+
+#endif
+
 static void choose_address(struct usb_device *udev)
 {
        int             devnum;
@@ -1126,14 +1154,14 @@ void usb_disconnect(struct usb_device **pdev)
         */
        usb_disable_device(udev, 0);
 
+       usb_notify_remove_device(udev);
+
        /* Free the device number, remove the /proc/bus/usb entry and
         * the sysfs attributes, and delete the parent's children[]
         * (or root_hub) pointer.
         */
        dev_dbg (&udev->dev, "unregistering device\n");
        release_address(udev);
-       usbfs_remove_device(udev);
-       usbdev_remove(udev);
        usb_remove_sysfs_dev_files(udev);
 
        /* Avoid races with recursively_mark_NOTATTACHED() */
@@ -1204,21 +1232,6 @@ static inline void show_string(struct usb_device *udev, char *id, char *string)
 {}
 #endif
 
-static void get_string(struct usb_device *udev, char **string, int index)
-{
-       char *buf;
-
-       if (!index)
-               return;
-       buf = kmalloc(256, GFP_KERNEL);
-       if (!buf)
-               return;
-       if (usb_string(udev, index, buf, 256) > 0)
-               *string = buf;
-       else
-               kfree(buf);
-}
-
 
 #ifdef CONFIG_USB_OTG
 #include "otg_whitelist.h"
@@ -1257,9 +1270,10 @@ int usb_new_device(struct usb_device *udev)
        }
 
        /* read the standard strings and cache them if present */
-       get_string(udev, &udev->product, udev->descriptor.iProduct);
-       get_string(udev, &udev->manufacturer, udev->descriptor.iManufacturer);
-       get_string(udev, &udev->serial, udev->descriptor.iSerialNumber);
+       udev->product = usb_cache_string(udev, udev->descriptor.iProduct);
+       udev->manufacturer = usb_cache_string(udev,
+                       udev->descriptor.iManufacturer);
+       udev->serial = usb_cache_string(udev, udev->descriptor.iSerialNumber);
 
        /* Tell the world! */
        dev_dbg(&udev->dev, "new device strings: Mfr=%d, Product=%d, "
@@ -1369,10 +1383,8 @@ int usb_new_device(struct usb_device *udev)
        }
 
        /* USB device state == configured ... usable */
+       usb_notify_add_device(udev);
 
-       /* add a /proc/bus/usb entry */
-       usbdev_add(udev);
-       usbfs_add_device(udev);
        return 0;
 
 fail:
@@ -1612,7 +1624,7 @@ static int hub_port_suspend(struct usb_hub *hub, int port1,
  */
 static int __usb_suspend_device (struct usb_device *udev, int port1)
 {
-       int     status;
+       int     status = 0;
 
        /* caller owns the udev device lock */
        if (port1 < 0)
@@ -1638,21 +1650,10 @@ static int __usb_suspend_device (struct usb_device *udev, int port1)
                }
        }
 
-       /* "global suspend" of the HC-to-USB interface (root hub), or
-        * "selective suspend" of just one hub-device link.
+       /* we only change a device's upstream USB link.
+        * root hubs have no upstream USB link.
         */
-       if (!udev->parent) {
-               struct usb_bus  *bus = udev->bus;
-               if (bus && bus->op->hub_suspend) {
-                       status = bus->op->hub_suspend (bus);
-                       if (status == 0) {
-                               dev_dbg(&udev->dev, "usb suspend\n");
-                               usb_set_device_state(udev,
-                                               USB_STATE_SUSPENDED);
-                       }
-               } else
-                       status = -EOPNOTSUPP;
-       } else
+       if (udev->parent)
                status = hub_port_suspend(hdev_to_hub(udev->parent), port1,
                                udev);
 
@@ -1661,10 +1662,12 @@ static int __usb_suspend_device (struct usb_device *udev, int port1)
        return status;
 }
 
-/**
+#endif
+
+/*
  * usb_suspend_device - suspend a usb device
  * @udev: device that's no longer in active use
- * Context: must be able to sleep; device not locked
+ * Context: must be able to sleep; device not locked; pm locks held
  *
  * Suspends a USB device that isn't in active use, conserving power.
  * Devices may wake out of a suspend, if anything important happens,
@@ -1683,6 +1686,7 @@ static int __usb_suspend_device (struct usb_device *udev, int port1)
  */
 int usb_suspend_device(struct usb_device *udev)
 {
+#ifdef CONFIG_USB_SUSPEND
        int     port1, status;
 
        port1 = locktree(udev);
@@ -1692,6 +1696,11 @@ int usb_suspend_device(struct usb_device *udev)
        status = __usb_suspend_device(udev, port1);
        usb_unlock_device(udev);
        return status;
+#else
+       /* NOTE:  udev->state unchanged, it's not lying ... */
+       udev->dev.power.power_state = PMSG_SUSPEND;
+       return 0;
+#endif
 }
 
 /*
@@ -1702,13 +1711,13 @@ int usb_suspend_device(struct usb_device *udev)
  * resume (by host) or remote wakeup (by device) ... now see what changed
  * in the tree that's rooted at this device.
  */
-static int finish_port_resume(struct usb_device *udev)
+static int finish_device_resume(struct usb_device *udev)
 {
        int     status;
        u16     devstatus;
 
        /* caller owns the udev device lock */
-       dev_dbg(&udev->dev, "usb resume\n");
+       dev_dbg(&udev->dev, "finish resume\n");
 
        /* usb ch9 identifies four variants of SUSPENDED, based on what
         * state the device resumes to.  Linux currently won't see the
@@ -1718,7 +1727,6 @@ static int finish_port_resume(struct usb_device *udev)
        usb_set_device_state(udev, udev->actconfig
                        ? USB_STATE_CONFIGURED
                        : USB_STATE_ADDRESS);
-       udev->dev.power.power_state = PMSG_ON;
 
        /* 10.5.4.5 says be sure devices in the tree are still there.
         * For now let's assume the device didn't go crazy on resume,
@@ -1734,7 +1742,8 @@ static int finish_port_resume(struct usb_device *udev)
                int             (*resume)(struct device *);
 
                le16_to_cpus(&devstatus);
-               if (devstatus & (1 << USB_DEVICE_REMOTE_WAKEUP)) {
+               if (devstatus & (1 << USB_DEVICE_REMOTE_WAKEUP)
+                               && udev->parent) {
                        status = usb_control_msg(udev,
                                        usb_sndctrlpipe(udev, 0),
                                        USB_REQ_CLEAR_FEATURE,
@@ -1764,6 +1773,8 @@ static int finish_port_resume(struct usb_device *udev)
        return status;
 }
 
+#ifdef CONFIG_USB_SUSPEND
+
 static int
 hub_port_resume(struct usb_hub *hub, int port1, struct usb_device *udev)
 {
@@ -1809,7 +1820,7 @@ hub_port_resume(struct usb_hub *hub, int port1, struct usb_device *udev)
                        /* TRSMRCY = 10 msec */
                        msleep(10);
                        if (udev)
-                               status = finish_port_resume(udev);
+                               status = finish_device_resume(udev);
                }
        }
        if (status < 0)
@@ -1818,12 +1829,12 @@ hub_port_resume(struct usb_hub *hub, int port1, struct usb_device *udev)
        return status;
 }
 
-static int hub_resume (struct usb_interface *intf);
+#endif
 
-/**
+/*
  * usb_resume_device - re-activate a suspended usb device
  * @udev: device to re-activate
- * Context: must be able to sleep; device not locked
+ * Context: must be able to sleep; device not locked; pm locks held
  *
  * This will re-activate the suspended device, increasing power usage
  * while letting drivers communicate again with its endpoints.
@@ -1841,35 +1852,22 @@ int usb_resume_device(struct usb_device *udev)
        if (port1 < 0)
                return port1;
 
-       /* "global resume" of the HC-to-USB interface (root hub), or
-        * selective resume of one hub-to-device port
-        */
-       if (!udev->parent) {
-               struct usb_bus  *bus = udev->bus;
-               if (bus && bus->op->hub_resume) {
-                       status = bus->op->hub_resume (bus);
+#ifdef CONFIG_USB_SUSPEND
+       /* selective resume of one downstream hub-to-device port */
+       if (udev->parent) {
+               if (udev->state == USB_STATE_SUSPENDED) {
+                       // NOTE swsusp may bork us, device state being wrong...
+                       // NOTE this fails if parent is also suspended...
+                       status = hub_port_resume(hdev_to_hub(udev->parent),
+                                       port1, udev);
                } else
-                       status = -EOPNOTSUPP;
-               if (status == 0) {
-                       dev_dbg(&udev->dev, "usb resume\n");
-                       /* TRSMRCY = 10 msec */
-                       msleep(10);
-                       usb_set_device_state (udev, USB_STATE_CONFIGURED);
-                       udev->dev.power.power_state = PMSG_ON;
-                       status = hub_resume (udev
-                                       ->actconfig->interface[0]);
-               }
-       } else if (udev->state == USB_STATE_SUSPENDED) {
-               // NOTE this fails if parent is also suspended...
-               status = hub_port_resume(hdev_to_hub(udev->parent),
-                               port1, udev);
-       } else {
-               status = 0;
-       }
-       if (status < 0) {
+                       status = 0;
+       } else
+#endif
+               status = finish_device_resume(udev);
+       if (status < 0)
                dev_dbg(&udev->dev, "can't resume, status %d\n",
                        status);
-       }
 
        usb_unlock_device(udev);
 
@@ -1886,6 +1884,8 @@ static int remote_wakeup(struct usb_device *udev)
 {
        int     status = 0;
 
+#ifdef CONFIG_USB_SUSPEND
+
        /* don't repeat RESUME sequence if this device
         * was already woken up by some other task
         */
@@ -1894,9 +1894,10 @@ static int remote_wakeup(struct usb_device *udev)
                dev_dbg(&udev->dev, "RESUME (wakeup)\n");
                /* TRSMRCY = 10 msec */
                msleep(10);
-               status = finish_port_resume(udev);
+               status = finish_device_resume(udev);
        }
        up(&udev->serialize);
+#endif
        return status;
 }
 
@@ -1911,12 +1912,32 @@ static int hub_suspend(struct usb_interface *intf, pm_message_t msg)
                struct usb_device       *udev;
 
                udev = hdev->children [port1-1];
-               if (udev && udev->state != USB_STATE_SUSPENDED) {
+               if (udev && (udev->dev.power.power_state.event
+                                       == PM_EVENT_ON
+#ifdef CONFIG_USB_SUSPEND
+                               || udev->state != USB_STATE_SUSPENDED
+#endif
+                               )) {
                        dev_dbg(&intf->dev, "port %d nyet suspended\n", port1);
                        return -EBUSY;
                }
        }
 
+       /* "global suspend" of the downstream HC-to-USB interface */
+       if (!hdev->parent) {
+               struct usb_bus  *bus = hdev->bus;
+               if (bus) {
+                       int     status = hcd_bus_suspend (bus);
+
+                       if (status != 0) {
+                               dev_dbg(&hdev->dev, "'global' suspend %d\n",
+                                       status);
+                               return status;
+                       }
+               } else
+                       return -EOPNOTSUPP;
+       }
+
        /* stop khubd and related activity */
        hub_quiesce(hub);
        return 0;
@@ -1926,9 +1947,36 @@ static int hub_resume(struct usb_interface *intf)
 {
        struct usb_device       *hdev = interface_to_usbdev(intf);
        struct usb_hub          *hub = usb_get_intfdata (intf);
-       unsigned                port1;
        int                     status;
 
+       /* "global resume" of the downstream HC-to-USB interface */
+       if (!hdev->parent) {
+               struct usb_bus  *bus = hdev->bus;
+               if (bus) {
+                       status = hcd_bus_resume (bus);
+                       if (status) {
+                               dev_dbg(&intf->dev, "'global' resume %d\n",
+                                       status);
+                               return status;
+                       }
+               } else
+                       return -EOPNOTSUPP;
+               if (status == 0) {
+                       /* TRSMRCY = 10 msec */
+                       msleep(10);
+               }
+       }
+
+       hub_activate(hub);
+
+       /* REVISIT:  this recursion probably shouldn't exist.  Remove
+        * this code sometime, after retesting with different root and
+        * external hubs.
+        */
+#ifdef CONFIG_USB_SUSPEND
+       {
+       unsigned                port1;
+
        for (port1 = 1; port1 <= hdev->maxchild; port1++) {
                struct usb_device       *udev;
                u16                     portstat, portchange;
@@ -1953,7 +2001,7 @@ static int hub_resume(struct usb_interface *intf)
                if (portstat & USB_PORT_STAT_SUSPEND)
                        status = hub_port_resume(hub, port1, udev);
                else {
-                       status = finish_port_resume(udev);
+                       status = finish_device_resume(udev);
                        if (status < 0) {
                                dev_dbg(&intf->dev, "resume port %d --> %d\n",
                                        port1, status);
@@ -1962,8 +2010,8 @@ static int hub_resume(struct usb_interface *intf)
                }
                up(&udev->serialize);
        }
-       hub->resume_root_hub = 0;
-       hub_activate(hub);
+       }
+#endif
        return 0;
 }
 
@@ -1987,30 +2035,6 @@ void usb_resume_root_hub(struct usb_device *hdev)
        kick_khubd(hub);
 }
 
-#else  /* !CONFIG_USB_SUSPEND */
-
-int usb_suspend_device(struct usb_device *udev)
-{
-       /* state does NOT lie by saying it's USB_STATE_SUSPENDED! */
-       return 0;
-}
-
-int usb_resume_device(struct usb_device *udev)
-{
-       udev->dev.power.power_state.event = PM_EVENT_ON;
-       return 0;
-}
-
-#define        hub_suspend             NULL
-#define        hub_resume              NULL
-#define        remote_wakeup(x)        0
-
-#endif /* CONFIG_USB_SUSPEND */
-
-EXPORT_SYMBOL(usb_suspend_device);
-EXPORT_SYMBOL(usb_resume_device);
-
-
 
 /* USB 2.0 spec, 7.1.7.3 / fig 7-29:
  *
@@ -2419,6 +2443,7 @@ static void hub_port_connect_change(struct usb_hub *hub, int port1,
 {
        struct usb_device *hdev = hub->hdev;
        struct device *hub_dev = hub->intfdev;
+       u16 wHubCharacteristics = le16_to_cpu(hub->descriptor->wHubCharacteristics);
        int status, i;
  
        dev_dbg (hub_dev,
@@ -2456,8 +2481,7 @@ static void hub_port_connect_change(struct usb_hub *hub, int port1,
        if (!(portstatus & USB_PORT_STAT_CONNECTION)) {
 
                /* maybe switch power back on (e.g. root hub was reset) */
-               if ((hub->descriptor->wHubCharacteristics
-                                       & HUB_CHAR_LPSM) < 2
+               if ((wHubCharacteristics & HUB_CHAR_LPSM) < 2
                                && !(portstatus & (1 << USB_PORT_FEAT_POWER)))
                        set_port_feature(hdev, port1, USB_PORT_FEAT_POWER);
  
@@ -2655,8 +2679,6 @@ static void hub_events(void)
                 * stub "device" node was never suspended.
                 */
                if (i) {
-                       extern void dpm_runtime_resume(struct device *);
-
                        dpm_runtime_resume(&hdev->dev);
                        dpm_runtime_resume(&intf->dev);
                }
@@ -2843,7 +2865,6 @@ static struct usb_device_id hub_id_table [] = {
 MODULE_DEVICE_TABLE (usb, hub_id_table);
 
 static struct usb_driver hub_driver = {
-       .owner =        THIS_MODULE,
        .name =         "hub",
        .probe =        hub_probe,
        .disconnect =   hub_disconnect,