+
+static void recursively_mark_NOTATTACHED(struct usb_device *udev)
+{
+ int i;
+
+ for (i = 0; i < udev->maxchild; ++i) {
+ if (udev->children[i])
+ recursively_mark_NOTATTACHED(udev->children[i]);
+ }
+ udev->state = USB_STATE_NOTATTACHED;
+}
+
+/**
+ * usb_set_device_state - change a device's current state (usbcore-internal)
+ * @udev: pointer to device whose state should be changed
+ * @new_state: new state value to be stored
+ *
+ * udev->state is _not_ protected by the udev->serialize semaphore. This
+ * is so that devices can be marked as disconnected as soon as possible,
+ * without having to wait for the semaphore to be released. Instead,
+ * changes to the state must be protected by the device_state_lock spinlock.
+ *
+ * Once a device has been added to the device tree, all changes to its state
+ * should be made using this routine. The state should _not_ be set directly.
+ *
+ * If udev->state is already USB_STATE_NOTATTACHED then no change is made.
+ * Otherwise udev->state is set to new_state, and if new_state is
+ * USB_STATE_NOTATTACHED then all of udev's descendant's states are also set
+ * to USB_STATE_NOTATTACHED.
+ */
+void usb_set_device_state(struct usb_device *udev,
+ enum usb_device_state new_state)
+{
+ unsigned long flags;
+
+ spin_lock_irqsave(&device_state_lock, flags);
+ if (udev->state == USB_STATE_NOTATTACHED)
+ ; /* do nothing */
+ else if (new_state != USB_STATE_NOTATTACHED)
+ udev->state = new_state;
+ else
+ recursively_mark_NOTATTACHED(udev);
+ spin_unlock_irqrestore(&device_state_lock, flags);
+}
+
+
+static void choose_address(struct usb_device *udev)
+{
+ int devnum;
+ struct usb_bus *bus = udev->bus;
+
+ /* If khubd ever becomes multithreaded, this will need a lock */
+
+ /* Try to allocate the next devnum beginning at bus->devnum_next. */
+ devnum = find_next_zero_bit(bus->devmap.devicemap, 128,
+ bus->devnum_next);
+ if (devnum >= 128)
+ devnum = find_next_zero_bit(bus->devmap.devicemap, 128, 1);
+
+ bus->devnum_next = ( devnum >= 127 ? 1 : devnum + 1);
+
+ if (devnum < 128) {
+ set_bit(devnum, bus->devmap.devicemap);
+ udev->devnum = devnum;
+ }
+}
+
+static void release_address(struct usb_device *udev)
+{
+ if (udev->devnum > 0) {
+ clear_bit(udev->devnum, udev->bus->devmap.devicemap);
+ udev->devnum = -1;
+ }
+}
+
+/**
+ * usb_disconnect - disconnect a device (usbcore-internal)
+ * @pdev: pointer to device being disconnected
+ * Context: !in_interrupt ()
+ *
+ * Something got disconnected. Get rid of it, and all of its children.
+ * If *pdev is a normal device then the parent hub should be locked.
+ * If *pdev is a root hub then this routine will acquire the
+ * usb_bus_list_lock on behalf of the caller.
+ *
+ * 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.
+ */
+void usb_disconnect(struct usb_device **pdev)
+{
+ struct usb_device *udev = *pdev;
+ int i;
+
+ if (!udev) {
+ pr_debug ("%s nodev\n", __FUNCTION__);
+ return;
+ }
+
+ /* mark the device as inactive, so any further urb submissions for
+ * this device will fail.
+ */
+ usb_set_device_state(udev, USB_STATE_NOTATTACHED);
+
+ /* lock the bus list on behalf of HCDs unregistering their root hubs */
+ if (!udev->parent)
+ down(&usb_bus_list_lock);
+ down(&udev->serialize);
+
+ dev_info (&udev->dev, "USB disconnect, address %d\n", udev->devnum);
+
+ /* Free up all the children before we remove this device */
+ for (i = 0; i < USB_MAXCHILDREN; i++) {
+ if (udev->children[i])
+ usb_disconnect(&udev->children[i]);
+ }
+
+ /* deallocate hcd/hardware state ... nuking all pending urbs and
+ * cleaning up all state associated with the current configuration
+ */
+ usb_disable_device(udev, 0);
+
+ /* 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);
+ usb_remove_sysfs_dev_files(udev);
+
+ /* Avoid races with recursively_mark_NOTATTACHED() */
+ spin_lock_irq(&device_state_lock);
+ *pdev = NULL;
+ spin_unlock_irq(&device_state_lock);
+
+ up(&udev->serialize);
+ if (!udev->parent)
+ up(&usb_bus_list_lock);
+
+ device_unregister(&udev->dev);
+}
+
+static int choose_configuration(struct usb_device *udev)
+{
+ int c, i;
+
+ /* NOTE: this should interact with hub power budgeting */
+
+ c = udev->config[0].desc.bConfigurationValue;
+ if (udev->descriptor.bNumConfigurations != 1) {
+ for (i = 0; i < udev->descriptor.bNumConfigurations; i++) {
+ struct usb_interface_descriptor *desc;
+
+ /* heuristic: Linux is more likely to have class
+ * drivers, so avoid vendor-specific interfaces.
+ */
+ desc = &udev->config[i].intf_cache[0]
+ ->altsetting->desc;
+ if (desc->bInterfaceClass == USB_CLASS_VENDOR_SPEC)
+ continue;
+ /* COMM/2/all is CDC ACM, except 0xff is MSFT RNDIS */
+ if (desc->bInterfaceClass == USB_CLASS_COMM
+ && desc->bInterfaceSubClass == 2
+ && desc->bInterfaceProtocol == 0xff)
+ continue;
+ c = udev->config[i].desc.bConfigurationValue;
+ break;
+ }
+ dev_info(&udev->dev,
+ "configuration #%d chosen from %d choices\n",
+ c, udev->descriptor.bNumConfigurations);
+ }
+ return c;
+}
+
+#ifdef DEBUG
+static void show_string(struct usb_device *udev, char *id, int index)
+{
+ char *buf;
+
+ if (!index)
+ return;
+ if (!(buf = kmalloc(256, GFP_KERNEL)))
+ return;
+ if (usb_string(udev, index, buf, 256) > 0)
+ dev_printk(KERN_INFO, &udev->dev, "%s: %s\n", id, buf);
+ kfree(buf);
+}
+
+#else
+static inline void show_string(struct usb_device *udev, char *id, int index)
+{}
+#endif
+
+/**
+ * usb_new_device - perform initial device setup (usbcore-internal)
+ * @udev: newly addressed device (in ADDRESS state)
+ *
+ * This is called with devices which have been enumerated, but not yet
+ * configured. The device descriptor is available, but not descriptors
+ * for any device configuration. The caller must have locked udev and
+ * either the parent hub (if udev is a normal device) or else the
+ * usb_bus_list_lock (if udev is a root hub). The parent's pointer to
+ * udev has already been installed, but udev is not yet visible through
+ * sysfs or other filesystem code.
+ *
+ * Returns 0 for success (device is configured and listed, with its
+ * interfaces, in sysfs); else a negative errno value.
+ *
+ * This call is synchronous, and may not be used in an interrupt context.
+ *
+ * Only the hub driver should ever call this; root hub registration
+ * uses it indirectly.
+ */
+int usb_new_device(struct usb_device *udev)
+{
+ int err;
+ int c;
+
+ err = usb_get_configuration(udev);
+ if (err < 0) {
+ dev_err(&udev->dev, "can't read configurations, error %d\n",
+ err);
+ goto fail;
+ }
+
+ /* Tell the world! */
+ dev_dbg(&udev->dev, "new device strings: Mfr=%d, Product=%d, "
+ "SerialNumber=%d\n",
+ udev->descriptor.iManufacturer,
+ udev->descriptor.iProduct,
+ udev->descriptor.iSerialNumber);
+
+ if (udev->descriptor.iProduct)
+ show_string(udev, "Product",
+ udev->descriptor.iProduct);
+ if (udev->descriptor.iManufacturer)
+ show_string(udev, "Manufacturer",
+ udev->descriptor.iManufacturer);
+ if (udev->descriptor.iSerialNumber)
+ show_string(udev, "SerialNumber",
+ udev->descriptor.iSerialNumber);
+
+ /* put device-specific files into sysfs */
+ err = device_add (&udev->dev);
+ if (err) {
+ dev_err(&udev->dev, "can't device_add, error %d\n", err);
+ goto fail;
+ }
+ usb_create_sysfs_dev_files (udev);
+
+ /* choose and set the configuration. that registers the interfaces
+ * with the driver core, and lets usb device drivers bind to them.
+ */
+ c = choose_configuration(udev);
+ if (c < 0)
+ dev_warn(&udev->dev,
+ "can't choose an initial configuration\n");
+ else {
+ err = usb_set_configuration(udev, c);
+ if (err) {
+ dev_err(&udev->dev, "can't set config #%d, error %d\n",
+ c, err);
+ usb_remove_sysfs_dev_files(udev);
+ device_del(&udev->dev);
+ goto fail;
+ }
+ }
+
+ /* USB device state == configured ... usable */
+
+ /* add a /proc/bus/usb entry */
+ usbfs_add_device(udev);
+ return 0;
+
+fail:
+ usb_set_device_state(udev, USB_STATE_NOTATTACHED);
+ return err;
+}
+
+