Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

usb: xhci: change enumeration scheme to 'new scheme' by default

Change the default enumeration scheme for xhci attached non-SuperSpeed
devices from:

Reset
SetAddress [xhci address-device BSR = 0]
GetDescriptor(8)
GetDescriptor(18)

...to:

Reset
[xhci address-device BSR = 1]
GetDescriptor(64)
Reset
SetAddress [xhci address-device BSR = 0]
GetDescriptor(18)

...as some devices misbehave when encountering a SetAddress command
prior to GetDescriptor. There are known legacy devices that require
this scheme, but testing has found at least one USB3 device that fails
enumeration when presented with this ordering. For now, follow the ehci
case and enable 'new scheme' by default for non-SuperSpeed devices.

To support this enumeration scheme on xhci the AddressDevice operation
needs to be performed twice. The first instance of the command enables
the HC's device and slot context info for the device, but omits sending
the device a SetAddress command (BSR == block set address request).
Then, after GetDescriptor completes, follow up with the full
AddressDevice+SetAddress operation.

As mentioned before, this ordering of events with USB3 devices causes an
extra state transition to be exposed to xhci. Previously USB3 devices
would transition directly from 'enabled' to 'addressed' and never need
to underrun responses to 'get descriptor'. We do see the 64-byte
descriptor fetch the correct data, but the following 18-byte descriptor
read after the reset gets:

bLength = 0
bDescriptorType = 0
bcdUSB = 0
bDeviceClass = 0
bDeviceSubClass = 0
bDeviceProtocol = 0
bMaxPacketSize0 = 9

instead of:

bLength = 12
bDescriptorType = 1
bcdUSB = 300
bDeviceClass = 0
bDeviceSubClass = 0
bDeviceProtocol = 0
bMaxPacketSize0 = 9

which results in the discovery process looping until falling back to
'old scheme' enumeration.

Acked-by: Alan Stern <stern@rowland.harvard.edu>
Reported-by: David Moore <david.moore@gmail.com>
Suggested-by: Sarah Sharp <sarah.a.sharp@linux.intel.com>
Reported-by: Dan Carpenter <dan.carpenter@oracle.com>
Signed-off-by: Dan Williams <dan.j.williams@intel.com>
Signed-off-by: Sarah Sharp <sarah.a.sharp@linux.intel.com>

authored by

Dan Williams and committed by
Sarah Sharp
48fc7dbd 40fcd88b

+75 -11
+43 -3
drivers/usb/core/hub.c
··· 2498 2498 #define HUB_LONG_RESET_TIME 200 2499 2499 #define HUB_RESET_TIMEOUT 800 2500 2500 2501 + /* 2502 + * "New scheme" enumeration causes an extra state transition to be 2503 + * exposed to an xhci host and causes USB3 devices to receive control 2504 + * commands in the default state. This has been seen to cause 2505 + * enumeration failures, so disable this enumeration scheme for USB3 2506 + * devices. 2507 + */ 2508 + static bool use_new_scheme(struct usb_device *udev, int retry) 2509 + { 2510 + if (udev->speed == USB_SPEED_SUPER) 2511 + return false; 2512 + 2513 + return USE_NEW_SCHEME(retry); 2514 + } 2515 + 2501 2516 static int hub_port_reset(struct usb_hub *hub, int port1, 2502 2517 struct usb_device *udev, unsigned int delay, bool warm); 2503 2518 ··· 3971 3956 } 3972 3957 } 3973 3958 3959 + static int hub_enable_device(struct usb_device *udev) 3960 + { 3961 + struct usb_hcd *hcd = bus_to_hcd(udev->bus); 3962 + 3963 + if (!hcd->driver->enable_device) 3964 + return 0; 3965 + if (udev->state == USB_STATE_ADDRESS) 3966 + return 0; 3967 + if (udev->state != USB_STATE_DEFAULT) 3968 + return -EINVAL; 3969 + 3970 + return hcd->driver->enable_device(hcd, udev); 3971 + } 3972 + 3974 3973 /* Reset device, (re)assign address, get device descriptor. 3975 3974 * Device connection must be stable, no more debouncing needed. 3976 3975 * Returns device in USB_STATE_ADDRESS, except on error. ··· 4097 4068 * this area, and this is how Linux has done it for ages. 4098 4069 * Change it cautiously. 4099 4070 * 4100 - * NOTE: If USE_NEW_SCHEME() is true we will start by issuing 4071 + * NOTE: If use_new_scheme() is true we will start by issuing 4101 4072 * a 64-byte GET_DESCRIPTOR request. This is what Windows does, 4102 4073 * so it may help with some non-standards-compliant devices. 4103 4074 * Otherwise we start with SET_ADDRESS and then try to read the ··· 4105 4076 * value. 4106 4077 */ 4107 4078 for (i = 0; i < GET_DESCRIPTOR_TRIES; (++i, msleep(100))) { 4108 - if (USE_NEW_SCHEME(retry_counter) && !(hcd->driver->flags & HCD_USB3)) { 4079 + bool did_new_scheme = false; 4080 + 4081 + if (use_new_scheme(udev, retry_counter)) { 4109 4082 struct usb_device_descriptor *buf; 4110 4083 int r = 0; 4084 + 4085 + did_new_scheme = true; 4086 + retval = hub_enable_device(udev); 4087 + if (retval < 0) 4088 + goto fail; 4111 4089 4112 4090 #define GET_DESCRIPTOR_BUFSIZE 64 4113 4091 buf = kmalloc(GET_DESCRIPTOR_BUFSIZE, GFP_NOIO); ··· 4204 4168 * - read ep0 maxpacket even for high and low speed, 4205 4169 */ 4206 4170 msleep(10); 4207 - if (USE_NEW_SCHEME(retry_counter) && !(hcd->driver->flags & HCD_USB3)) 4171 + /* use_new_scheme() checks the speed which may have 4172 + * changed since the initial look so we cache the result 4173 + * in did_new_scheme 4174 + */ 4175 + if (did_new_scheme) 4208 4176 break; 4209 4177 } 4210 4178
+1
drivers/usb/host/xhci-pci.c
··· 331 331 .check_bandwidth = xhci_check_bandwidth, 332 332 .reset_bandwidth = xhci_reset_bandwidth, 333 333 .address_device = xhci_address_device, 334 + .enable_device = xhci_enable_device, 334 335 .update_hub_device = xhci_update_hub_device, 335 336 .reset_device = xhci_discover_or_reset_device, 336 337
+1
drivers/usb/host/xhci-plat.c
··· 69 69 .check_bandwidth = xhci_check_bandwidth, 70 70 .reset_bandwidth = xhci_reset_bandwidth, 71 71 .address_device = xhci_address_device, 72 + .enable_device = xhci_enable_device, 72 73 .update_hub_device = xhci_update_hub_device, 73 74 .reset_device = xhci_discover_or_reset_device, 74 75
+3 -3
drivers/usb/host/xhci-ring.c
··· 4004 4004 4005 4005 /* Queue an address device command TRB */ 4006 4006 int xhci_queue_address_device(struct xhci_hcd *xhci, dma_addr_t in_ctx_ptr, 4007 - u32 slot_id) 4007 + u32 slot_id, enum xhci_setup_dev setup) 4008 4008 { 4009 4009 return queue_command(xhci, lower_32_bits(in_ctx_ptr), 4010 4010 upper_32_bits(in_ctx_ptr), 0, 4011 - TRB_TYPE(TRB_ADDR_DEV) | SLOT_ID_FOR_TRB(slot_id), 4012 - false); 4011 + TRB_TYPE(TRB_ADDR_DEV) | SLOT_ID_FOR_TRB(slot_id) 4012 + | (setup == SETUP_CONTEXT_ONLY ? TRB_BSR : 0), false); 4013 4013 } 4014 4014 4015 4015 int xhci_queue_vendor_command(struct xhci_hcd *xhci,
+15 -4
drivers/usb/host/xhci.c
··· 3709 3709 } 3710 3710 3711 3711 /* 3712 - * Issue an Address Device command (which will issue a SetAddress request to 3713 - * the device). 3712 + * Issue an Address Device command and optionally send a corresponding 3713 + * SetAddress request to the device. 3714 3714 * We should be protected by the usb_address0_mutex in khubd's hub_port_init, so 3715 3715 * we should only issue and wait on one address command at the same time. 3716 3716 */ 3717 - int xhci_address_device(struct usb_hcd *hcd, struct usb_device *udev) 3717 + static int xhci_setup_device(struct usb_hcd *hcd, struct usb_device *udev, 3718 + enum xhci_setup_dev setup) 3718 3719 { 3719 3720 unsigned long flags; 3720 3721 int timeleft; ··· 3774 3773 spin_lock_irqsave(&xhci->lock, flags); 3775 3774 cmd_trb = xhci_find_next_enqueue(xhci->cmd_ring); 3776 3775 ret = xhci_queue_address_device(xhci, virt_dev->in_ctx->dma, 3777 - udev->slot_id); 3776 + udev->slot_id, setup); 3778 3777 if (ret) { 3779 3778 spin_unlock_irqrestore(&xhci->lock, flags); 3780 3779 xhci_dbg_trace(xhci, trace_xhci_dbg_address, ··· 3867 3866 le32_to_cpu(slot_ctx->dev_state) & DEV_ADDR_MASK); 3868 3867 3869 3868 return 0; 3869 + } 3870 + 3871 + int xhci_address_device(struct usb_hcd *hcd, struct usb_device *udev) 3872 + { 3873 + return xhci_setup_device(hcd, udev, SETUP_CONTEXT_ADDRESS); 3874 + } 3875 + 3876 + int xhci_enable_device(struct usb_hcd *hcd, struct usb_device *udev) 3877 + { 3878 + return xhci_setup_device(hcd, udev, SETUP_CONTEXT_ONLY); 3870 3879 } 3871 3880 3872 3881 /*
+10 -1
drivers/usb/host/xhci.h
··· 1108 1108 }; 1109 1109 1110 1110 /* flags bitmasks */ 1111 + 1112 + /* Address device - disable SetAddress */ 1113 + #define TRB_BSR (1<<9) 1114 + enum xhci_setup_dev { 1115 + SETUP_CONTEXT_ONLY, 1116 + SETUP_CONTEXT_ADDRESS, 1117 + }; 1118 + 1111 1119 /* bits 16:23 are the virtual function ID */ 1112 1120 /* bits 24:31 are the slot ID */ 1113 1121 #define TRB_TO_SLOT_ID(p) (((p) & (0xff<<24)) >> 24) ··· 1768 1760 struct usb_host_endpoint **eps, unsigned int num_eps, 1769 1761 gfp_t mem_flags); 1770 1762 int xhci_address_device(struct usb_hcd *hcd, struct usb_device *udev); 1763 + int xhci_enable_device(struct usb_hcd *hcd, struct usb_device *udev); 1771 1764 int xhci_update_device(struct usb_hcd *hcd, struct usb_device *udev); 1772 1765 int xhci_set_usb2_hardware_lpm(struct usb_hcd *hcd, 1773 1766 struct usb_device *udev, int enable); ··· 1792 1783 void xhci_ring_cmd_db(struct xhci_hcd *xhci); 1793 1784 int xhci_queue_slot_control(struct xhci_hcd *xhci, u32 trb_type, u32 slot_id); 1794 1785 int xhci_queue_address_device(struct xhci_hcd *xhci, dma_addr_t in_ctx_ptr, 1795 - u32 slot_id); 1786 + u32 slot_id, enum xhci_setup_dev); 1796 1787 int xhci_queue_vendor_command(struct xhci_hcd *xhci, 1797 1788 u32 field1, u32 field2, u32 field3, u32 field4); 1798 1789 int xhci_queue_stop_endpoint(struct xhci_hcd *xhci, int slot_id,
+2
include/linux/usb/hcd.h
··· 353 353 void (*reset_bandwidth)(struct usb_hcd *, struct usb_device *); 354 354 /* Returns the hardware-chosen device address */ 355 355 int (*address_device)(struct usb_hcd *, struct usb_device *udev); 356 + /* prepares the hardware to send commands to the device */ 357 + int (*enable_device)(struct usb_hcd *, struct usb_device *udev); 356 358 /* Notifies the HCD after a hub descriptor is fetched. 357 359 * Will block. 358 360 */