]> git.hungrycats.org Git - linux/commitdiff
[PATCH] USB: re-factor enumeration/reset paths
authorDavid Brownell <david-b@pacbell.net>
Mon, 9 Feb 2004 04:16:07 +0000 (20:16 -0800)
committerGreg Kroah-Hartman <greg@kroah.com>
Mon, 9 Feb 2004 04:16:07 +0000 (20:16 -0800)
This patch starts dis-entangling some of the enumeration logic by
moving initialization code into the usb_alloc_dev() constructor.
Some call signatures changed; a usbcore-internal declaration was
moved in <linux/usb.h> to a more appropriate location.

With the driver model init now more centralized, it's safer to
use driver model calls (including dev_info) a lot earlier, so
the "new device at address N" message now does that.  It also
reports the device speed, which may not be evident otherwise.

drivers/usb/core/hcd.c
drivers/usb/core/hcd.h
drivers/usb/core/hub.c
drivers/usb/core/usb.c
drivers/usb/host/ehci-hcd.c
drivers/usb/host/hc_sl811_rh.c
drivers/usb/host/ohci-hcd.c
drivers/usb/host/uhci-hcd.c
include/linux/usb.h

index 8ee641df6b9cd530bcd7f86519f034df65d050fb..e76cb4c1cf4d088c74c4a1ee3da7baf61a4ba37c 100644 (file)
@@ -738,7 +738,7 @@ EXPORT_SYMBOL (usb_deregister_bus);
  *
  * The USB host controller calls this function to register the root hub
  * properly with the USB subsystem.  It sets up the device properly in
- * the driverfs tree, and then calls usb_new_device() to register the
+ * the device model tree, and then calls usb_new_device() to register the
  * usb device.  It also assigns the root hub's USB address (always 1).
  */
 int usb_register_root_hub (struct usb_device *usb_dev, struct device *parent_dev)
@@ -746,14 +746,14 @@ int usb_register_root_hub (struct usb_device *usb_dev, struct device *parent_dev
        const int devnum = 1;
        int retval;
 
-       sprintf (&usb_dev->dev.bus_id[0], "usb%d", usb_dev->bus->busnum);
-       usb_dev->state = USB_STATE_DEFAULT;
-
        usb_dev->devnum = devnum;
        usb_dev->bus->devnum_next = devnum + 1;
+       memset (&usb_dev->bus->devmap.devicemap, 0,
+                       sizeof usb_dev->bus->devmap.devicemap);
        set_bit (devnum, usb_dev->bus->devmap.devicemap);
+       usb_dev->state = USB_STATE_ADDRESS;
 
-       retval = usb_new_device (usb_dev, parent_dev);
+       retval = usb_new_device (usb_dev);
        if (retval)
                dev_err (parent_dev, "can't register root hub for %s, %d\n",
                                usb_dev->dev.bus_id, retval);
index 1d0ea74d014837e6c727f3caf19485bde3b2b21c..725a92f29092fd61bacbf1745a77f4ba5f2719ed 100644 (file)
@@ -241,7 +241,9 @@ extern void usb_hc_died (struct usb_hcd *hcd);
 /* -------------------------------------------------------------------------- */
 
 /* Enumeration is only for the hub driver, or HCD virtual root hubs */
-extern int usb_new_device(struct usb_device *dev, struct device *parent);
+extern struct usb_device *usb_alloc_dev(struct usb_device *parent,
+                                       struct usb_bus *, unsigned port);
+extern int usb_new_device(struct usb_device *dev);
 extern void usb_choose_address(struct usb_device *dev);
 extern void usb_disconnect(struct usb_device **);
 
index 6c2976d24562512161c04cd73bf659226f325a05..e9db8522acd7e0767a44aeec0637d38ce7831f18 100644 (file)
@@ -930,11 +930,9 @@ static void hub_port_connect_change(struct usb_hub *hubstate, int port,
        down(&usb_address0_sem);
 
        for (i = 0; i < HUB_PROBE_TRIES; i++) {
-               struct usb_device *pdev;
-               int     len;
 
                /* Allocate a new device struct */
-               dev = usb_alloc_dev(hub, hub->bus);
+               dev = usb_alloc_dev(hub, hub->bus, port);
                if (!dev) {
                        dev_err (&hubstate->intf->dev,
                                "couldn't allocate usb_device\n");
@@ -962,38 +960,18 @@ static void hub_port_connect_change(struct usb_hub *hubstate, int port,
                        dev->ttport = port + 1;
                }
 
-               /* Save readable and stable topology id, distinguishing
-                * devices by location for diagnostics, tools, etc.  The
-                * string is a path along hub ports, from the root.  Each
-                * device's id will be stable until USB is re-cabled, and
-                * hubs are often labeled with these port numbers.
-                *
-                * Initial size: ".NN" times five hubs + NUL = 16 bytes max
-                * (quite rare, since most hubs have 4-6 ports).
-                */
-               pdev = dev->parent;
-               if (pdev->devpath [0] != '0')   /* parent not root? */
-                       len = snprintf (dev->devpath, sizeof dev->devpath,
-                               "%s.%d", pdev->devpath, port + 1);
-               /* root == "0", root port 2 == "2", port 3 that hub "2.3" */
-               else
-                       len = snprintf (dev->devpath, sizeof dev->devpath,
-                               "%d", port + 1);
-               if (len == sizeof dev->devpath)
-                       dev_err (&hubstate->intf->dev,
-                               "devpath size! usb/%03d/%03d path %s\n",
-                               dev->bus->busnum, dev->devnum, dev->devpath);
-               dev_info (&hubstate->intf->dev,
-                       "new USB device on port %d, assigned address %d\n",
-                       port + 1, dev->devnum);
-
-               /* put the device in the global device tree. the hub port
-                * is the "bus_id"; hubs show in hierarchy like bridges
-                */
-               dev->dev.parent = dev->parent->dev.parent->parent;
+               dev_info (&dev->dev,
+                       "new %s speed USB device using address %d\n",
+                       ({ char *speed; switch (dev->speed) {
+                       case USB_SPEED_LOW:     speed = "low";  break;
+                       case USB_SPEED_FULL:    speed = "full"; break;
+                       case USB_SPEED_HIGH:    speed = "high"; break;
+                       default:                speed = "?";    break;
+                       }; speed;}),
+                       dev->devnum);
 
                /* Run it through the hoops (find a driver, etc) */
-               if (!usb_new_device(dev, &hub->dev)) {
+               if (usb_new_device(dev) == 0) {
                        hub->children[port] = dev;
                        goto done;
                }
index 378d51ce2374331c9e7b947154b801b24bf0d0d9..07ddb791beed2b67a8c0c0c4cd69f59dce364c05 100644 (file)
@@ -678,17 +678,19 @@ static void usb_release_dev(struct device *dev)
 }
 
 /**
- * usb_alloc_dev - allocate a usb device structure (usbcore-internal)
- * @parent: hub to which device is connected
+ * usb_alloc_dev - usb device constructor (usbcore-internal)
+ * @parent: hub to which device is connected; null to allocate a root hub
  * @bus: bus used to access the device
+ * @port: zero based index of port; ignored for root hubs
  * Context: !in_interrupt ()
  *
  * Only hub drivers (including virtual root hub drivers for host
  * controllers) should ever call this.
  *
- * This call is synchronous, and may not be used in an interrupt context.
+ * This call may not be used in a non-sleeping context.
  */
-struct usb_device *usb_alloc_dev(struct usb_device *parent, struct usb_bus *bus)
+struct usb_device *
+usb_alloc_dev(struct usb_device *parent, struct usb_bus *bus, unsigned port)
 {
        struct usb_device *dev;
 
@@ -705,11 +707,42 @@ struct usb_device *usb_alloc_dev(struct usb_device *parent, struct usb_bus *bus)
        }
 
        device_initialize(&dev->dev);
+       dev->dev.bus = &usb_bus_type;
+       dev->dev.dma_mask = bus->controller->dma_mask;
+       dev->dev.driver_data = &usb_generic_driver_data;
+       dev->dev.driver = &usb_generic_driver;
        dev->dev.release = usb_release_dev;
        dev->state = USB_STATE_ATTACHED;
 
-       if (!parent)
+       /* Save readable and stable topology id, distinguishing devices
+        * by location for diagnostics, tools, driver model, etc.  The
+        * string is a path along hub ports, from the root.  Each device's
+        * dev->devpath will be stable until USB is re-cabled, and hubs
+        * are often labeled with these port numbers.  The bus_id isn't
+        * as stable:  bus->busnum changes easily from modprobe order,
+        * cardbus or pci hotplugging, and so on.
+        */
+       if (unlikely (!parent)) {
                dev->devpath [0] = '0';
+
+               dev->dev.parent = bus->controller;
+               sprintf (&dev->dev.bus_id[0], "usb%d", bus->busnum);
+       } else {
+               /* match any labeling on the hubs; it's one-based */
+               if (parent->devpath [0] == '0')
+                       snprintf (dev->devpath, sizeof dev->devpath,
+                               "%d", port + 1);
+               else
+                       snprintf (dev->devpath, sizeof dev->devpath,
+                               "%s.%d", parent->devpath, port + 1);
+
+               dev->dev.parent = &parent->dev;
+               sprintf (&dev->dev.bus_id[0], "%d-%s",
+                       bus->busnum, dev->devpath);
+
+               /* hub driver sets up TT records */
+       }
+
        dev->bus = bus;
        dev->parent = parent;
        INIT_LIST_HEAD(&dev->filelist);
@@ -1011,33 +1044,13 @@ static inline void usb_show_string(struct usb_device *dev, char *id, int index)
  */
 #define NEW_DEVICE_RETRYS      2
 #define SET_ADDRESS_RETRYS     2
-int usb_new_device(struct usb_device *dev, struct device *parent)
+int usb_new_device(struct usb_device *dev)
 {
        int err = -EINVAL;
        int i;
        int j;
        int config;
 
-       /*
-        * Set the driver for the usb device to point to the "generic" driver.
-        * This prevents the main usb device from being sent to the usb bus
-        * probe function.  Yes, it's a hack, but a nice one :)
-        *
-        * Do it asap, so more driver model stuff (like the device.h message
-        * utilities) can be used in hcd submit/unlink code paths.
-        */
-       usb_generic_driver.bus = &usb_bus_type;
-       dev->dev.parent = parent;
-       dev->dev.driver = &usb_generic_driver;
-       dev->dev.bus = &usb_bus_type;
-       dev->dev.driver_data = &usb_generic_driver_data;
-       if (dev->dev.bus_id[0] == 0)
-               sprintf (&dev->dev.bus_id[0], "%d-%s",
-                        dev->bus->busnum, dev->devpath);
-
-       /* dma masks come from the controller; readonly, except to hcd */
-       dev->dev.dma_mask = parent->dma_mask;
-
        /* USB 2.0 section 5.5.3 talks about ep0 maxpacket ...
         * it's fixed size except for full speed devices.
         */
index 5c58a16c7693f85dc8d9f575e76839fec9d226cc..0db191fb8ea03a75a12d520d287eca23badff130 100644 (file)
@@ -473,7 +473,7 @@ static int ehci_start (struct usb_hcd *hcd)
 
        /* wire up the root hub */
        bus = hcd_to_bus (hcd);
-       bus->root_hub = udev = usb_alloc_dev (NULL, bus);
+       bus->root_hub = udev = usb_alloc_dev (NULL, bus, 0);
        if (!udev) {
 done2:
                ehci_mem_cleanup (ehci);
index 42ec077b72bff935ee690d449a6c781ef0149eb8..a4dd4628ed66921c0cd4eaf3e53ad6a5588c870a 100644 (file)
@@ -559,7 +559,7 @@ static int rh_connect_rh (hci_t * hci)
        struct usb_device *usb_dev;
 
        hci->rh.devnum = 0;
-       usb_dev = usb_alloc_dev (NULL, hci->bus);
+       usb_dev = usb_alloc_dev (NULL, hci->bus, 0);
        if (!usb_dev)
                return -ENOMEM;
 
index cc2c9c259197bc527fc16e0dcf7499444195e523..53c86a42d4bae0e84ebf205b0ab74ae665d330b4 100644 (file)
@@ -536,7 +536,7 @@ static int hc_start (struct ohci_hcd *ohci)
  
        /* connect the virtual root hub */
        bus = hcd_to_bus (&ohci->hcd);
-       bus->root_hub = udev = usb_alloc_dev (NULL, bus);
+       bus->root_hub = udev = usb_alloc_dev (NULL, bus, 0);
        ohci->hcd.state = USB_STATE_RUNNING;
        if (!udev) {
                disable (ohci);
index 652f85120d75977b1cd9029f890cf28a6d6b1ef3..0678fdf8d7b1aa61d95a8cb98484f105b859fbc7 100644 (file)
@@ -2315,7 +2315,7 @@ static int uhci_start(struct usb_hcd *hcd)
 
        uhci->rh_numports = port;
 
-       hcd->self.root_hub = udev = usb_alloc_dev(NULL, &hcd->self);
+       hcd->self.root_hub = udev = usb_alloc_dev(NULL, &hcd->self, 0);
        if (!udev) {
                err("unable to allocate root hub");
                goto err_alloc_root_hub;
index c16b0125baa0aa4a9e457c14c210f4ae1fec1443..45caca344ca3227d72ab1f67493d1d306d19cec5 100644 (file)
@@ -274,7 +274,6 @@ struct usb_device {
 };
 #define        to_usb_device(d) container_of(d, struct usb_device, dev)
 
-extern struct usb_device *usb_alloc_dev(struct usb_device *parent, struct usb_bus *);
 extern struct usb_device *usb_get_dev(struct usb_device *dev);
 extern void usb_put_dev(struct usb_device *dev);