📄 hub.c
字号:
/* 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 must already 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 (and any of its children) will fail immediately. * this quiesces everyting except pending urbs. */ 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); usb_lock_device(udev); } else 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 * so that the hardware is now fully quiesced. */ 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); if (!udev->parent) { usb_unlock_device(udev); up(&usb_bus_list_lock); } else up(&udev->serialize); 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. * MSFT needs this to be the first config; never use * it as the default unless Linux has host-side RNDIS. * A second config would ideally be CDC-Ethernet, but * may instead be the "vendor specific" CDC subset * long used by ARM Linux for sa1100 or pxa255. */ if (desc->bInterfaceClass == USB_CLASS_COMM && desc->bInterfaceSubClass == 2 && desc->bInterfaceProtocol == 0xff) { c = udev->config[1].desc.bConfigurationValue; 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 DEBUGstatic 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);}#elsestatic inline void show_string(struct usb_device *udev, char *id, int index){}#endif#ifdef CONFIG_USB_OTG#include "otg_whitelist.h"#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);#ifdef CONFIG_USB_OTG /* * OTG-aware devices on OTG-capable root hubs may be able to use SRP, * to wake us after we've powered off VBUS; and HNP, switching roles * "host" to "peripheral". The OTG descriptor helps figure this out. */ if (!udev->bus->is_b_host && udev->config && udev->parent == udev->bus->root_hub) { struct usb_otg_descriptor *desc = 0; struct usb_bus *bus = udev->bus; /* descriptor may appear anywhere in config */ if (__usb_get_extra_descriptor (udev->rawdescriptors[0], le16_to_cpu(udev->config[0].desc.wTotalLength), USB_DT_OTG, (void **) &desc) == 0) { if (desc->bmAttributes & USB_OTG_HNP) { unsigned port1; struct usb_device *root = udev->parent; for (port1 = 1; port1 <= root->maxchild; port1++) { if (root->children[port1-1] == udev) break; } dev_info(&udev->dev, "Dual-Role OTG device on %sHNP port\n", (port1 == bus->otg_port) ? "" : "non-"); /* enable HNP before suspend, it's simpler */ if (port1 == bus->otg_port) bus->b_hnp_enable = 1; err = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), USB_REQ_SET_FEATURE, 0, bus->b_hnp_enable ? USB_DEVICE_B_HNP_ENABLE : USB_DEVICE_A_ALT_HNP_SUPPORT, 0, NULL, 0, HZ * USB_CTRL_SET_TIMEOUT); if (err < 0) { /* OTG MESSAGE: report errors here, * customize to match your product. */ dev_info(&udev->dev, "can't set HNP mode; %d\n", err); bus->b_hnp_enable = 0; } } } } if (!is_targeted(udev)) { /* Maybe it can talk to us, though we can't talk to it. * (Includes HNP test device.) */ if (udev->bus->b_hnp_enable || udev->bus->is_b_host) { static int __usb_suspend_device (struct usb_device *, int port1, u32 state); err = __usb_suspend_device(udev, udev->bus->otg_port, PM_SUSPEND_MEM); if (err < 0) dev_dbg(&udev->dev, "HNP fail, %d\n", err); } err = -ENODEV; goto fail; }#endif /* 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;}static int hub_port_status(struct usb_hub *hub, int port1, u16 *status, u16 *change){ int ret; ret = get_port_status(hub->hdev, port1, &hub->status->port); if (ret < 0) dev_err (hub->intfdev, "%s failed (err = %d)\n", __FUNCTION__, ret); else { *status = le16_to_cpu(hub->status->port.wPortStatus); *change = le16_to_cpu(hub->status->port.wPortChange); ret = 0; } return ret;}#define PORT_RESET_TRIES 5#define SET_ADDRESS_TRIES 2#define GET_DESCRIPTOR_TRIES 2#define SET_CONFIG_TRIES (2 * (use_both_schemes + 1))#define USE_NEW_SCHEME(i) ((i) / 2 == old_scheme_first)#define HUB_ROOT_RESET_TIME 50 /* times are in msec */#define HUB_SHORT_RESET_TIME 10#define HUB_LONG_RESET_TIME 200#define HUB_RESET_TIMEOUT 500static int hub_port_wait_reset(struct usb_hub *hub, int port1, struct usb_device *udev, unsigned int delay){ int delay_time, ret; u16 portstatus; u16 portchange; for (delay_time = 0; delay_time < HUB_RESET_TIMEOUT; delay_time += delay) { /* wait to give the device a chance to reset */ msleep(delay); /* read and decode port status */ ret = hub_port_status(hub, port1, &portstatus, &portchange); if (ret < 0) return ret; /* Device went away? */ if (!(portstatus & USB_PORT_STAT_CONNECTION)) return -ENOTCONN; /* bomb out completely if something weird happened */ if ((portchange & USB_PORT_STAT_C_CONNECTION)) return -EINVAL; /* if we`ve finished resetting, then break out of the loop */ if (!(portstatus & USB_PORT_STAT_RESET) && (portstatus & USB_PORT_STAT_ENABLE)) { if (portstatus & USB_PORT_STAT_HIGH_SPEED) udev->speed = USB_SPEED_HIGH; else if (portstatus & USB_PORT_STAT_LOW_SPEED) udev->speed = USB_SPEED_LOW; else udev->speed = USB_SPEED_FULL; return 0; } /* switch to the long delay after two short delay failures */ if (delay_time >= 2 * HUB_SHORT_RESET_TIME) delay = HUB_LONG_RESET_TIME; dev_dbg (hub->intfdev, "port %d not reset yet, waiting %dms\n", port1, delay); } return -EBUSY;}static int hub_port_reset(struct usb_hub *hub, int port1, struct usb_device *udev, unsigned int delay){ int i, status; /* Reset the port */ for (i = 0; i < PORT_RESET_TRIES; i++) { status = set_port_feature(hub->hdev, port1, USB_PORT_FEAT_RESET); if (status) dev_err(hub->intfdev, "cannot reset port %d (err = %d)\n", port1, status); else status = hub_port_wait_reset(hub, port1, udev, delay); /* return on disconnect or reset */ switch (status) { case 0: /* TRSTRCY = 10 ms */ msleep(10); /* FALL THROUGH */ case -ENOTCONN: case -ENODEV: clear_port_feature(hub->hdev, port1, USB_PORT_FEAT_C_RESET); /* FIXME need disconnect() for NOTATTACHED device */ usb_set_device_state(udev, status ? USB_STATE_NOTATTACHED : USB_STATE_DEFAULT); return status; } dev_dbg (hub->intfdev, "port %d not enabled, trying reset again...\n", port1); delay = HUB_LONG_RESET_TIME; } dev_err (hub->intfdev, "Cannot enable port %i. Maybe the USB cable is bad?\n", port1); return status;}static int hub_port_disable(struct usb_hub *hub, int port1, int set_state){ struct usb_device *hdev = hub->hdev; int ret; if (hdev->children[port1-1] && set_state) { usb_set_device_state(hdev->children[port1-1], USB_STATE_NOTATTACHED); } ret = clear_port_feature(hdev, port1, USB_PORT_FEAT_ENABLE); if (ret) dev_err(hub->intfdev, "cannot disable port %d (err = %d)\n", port1, ret); return ret;}/* * Disable a port and mark a logical connnect-change event, so that some * time later khubd will disconnect() any existing usb_device on the port * and will re-enumerate if there actually is a device attached. */static void hub_port_logical_disconnect(struct usb_hub *hub, int port1){ dev_dbg(hub->intfdev, "logical disconnect on port %d\n", port1); hub_port_disable(hub, port1, 1); /* FIXME let caller ask to power down the port: * - some devices won't enumerate without a VBUS power cycle * - SRP saves power that way * - usb_suspend_device(dev,PM_SUSPEND_DISK) * That's easy if this hub can switch power per-port, and * khubd reactivates the port later (timer, SRP, etc). * Powerdown must be optional, because of reset/DFU. */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -