Greg KH wrote:
On Mon, Dec 15, 2003 at 01:17:53PM -0800, David Brownell wrote:

We've had some open issues in the usb_reset_device() paths for
some time, and this starts to resolve them.


This looks nice, thanks.


So far as I can tell it's ready for merging right now (unless
Greg wants any small chunks split out).


Please, I would feel much more comfortable accepting this if it was
split out into logical chunks.

Well, here's the first such chunk, against the latest 2.6 BK. It accounts for about 1/3 of the larger patch, and most of the non-core file changes. Please merge it, and I'll split out a few more patches (touching the more ticklish code) soonish.

- Dave



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.
--- 1.75/drivers/usb/core/hcd.c Sun Dec  7 04:29:05 2003
+++ edited/drivers/usb/core/hcd.c       Tue Dec 30 11:34:26 2003
@@ -735,7 +735,7 @@
  *
  * 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)
@@ -743,14 +743,14 @@
        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);
--- 1.34/drivers/usb/core/hcd.h Fri Aug 29 11:06:37 2003
+++ edited/drivers/usb/core/hcd.h       Tue Dec 30 11:34:27 2003
@@ -241,7 +241,9 @@
 /* -------------------------------------------------------------------------- */
 
 /* 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 **);
 
--- 1.84/drivers/usb/core/hub.c Mon Dec 29 16:38:38 2003
+++ edited/drivers/usb/core/hub.c       Tue Dec 30 11:34:27 2003
@@ -929,11 +929,9 @@
        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");
@@ -961,38 +959,18 @@
                        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;
                }
--- 1.149/drivers/usb/core/usb.c        Mon Dec 29 16:38:38 2003
+++ edited/drivers/usb/core/usb.c       Tue Dec 30 11:34:27 2003
@@ -672,17 +672,19 @@
 }
 
 /**
- * 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;
 
@@ -699,11 +701,42 @@
        }
 
        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);
@@ -992,32 +1025,12 @@
  */
 #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.
--- 1.64/drivers/usb/host/ehci-hcd.c    Sun Dec 28 21:09:07 2003
+++ edited/drivers/usb/host/ehci-hcd.c  Tue Dec 30 11:34:27 2003
@@ -473,7 +473,7 @@
 
        /* 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);
--- 1.6/drivers/usb/host/hc_sl811_rh.c  Fri Jul 25 06:48:06 2003
+++ edited/drivers/usb/host/hc_sl811_rh.c       Tue Dec 30 11:34:27 2003
@@ -559,7 +559,7 @@
        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;
 
--- 1.52/drivers/usb/host/ohci-hcd.c    Mon Dec 29 16:38:39 2003
+++ edited/drivers/usb/host/ohci-hcd.c  Tue Dec 30 11:34:27 2003
@@ -519,7 +519,7 @@
  
        /* 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);
--- 1.48/drivers/usb/host/uhci-hcd.c    Mon Dec 29 10:20:27 2003
+++ edited/drivers/usb/host/uhci-hcd.c  Tue Dec 30 11:34:27 2003
@@ -2306,7 +2306,7 @@
 
        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;
--- 1.92/include/linux/usb.h    Mon Dec  8 09:39:26 2003
+++ edited/include/linux/usb.h  Tue Dec 30 11:34:27 2003
@@ -274,7 +274,6 @@
 };
 #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);
 

Reply via email to