Greg Kroah-Hartman | 5fd54ac | 2017-11-03 11:28:30 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Sarah Sharp | 0f2a793 | 2009-04-27 19:57:12 -0700 | [diff] [blame] | 2 | /* |
| 3 | * xHCI host controller driver |
| 4 | * |
| 5 | * Copyright (C) 2008 Intel Corp. |
| 6 | * |
| 7 | * Author: Sarah Sharp |
| 8 | * Some code borrowed from the Linux EHCI driver. |
Sarah Sharp | 0f2a793 | 2009-04-27 19:57:12 -0700 | [diff] [blame] | 9 | */ |
| 10 | |
Mathias Nyman | ddba5cd | 2014-05-08 19:26:00 +0300 | [diff] [blame] | 11 | |
| 12 | #include <linux/slab.h> |
Sarah Sharp | 0f2a793 | 2009-04-27 19:57:12 -0700 | [diff] [blame] | 13 | #include <asm/unaligned.h> |
| 14 | |
| 15 | #include "xhci.h" |
Xenia Ragiadakou | 4bdfe4c | 2013-08-06 07:52:45 +0300 | [diff] [blame] | 16 | #include "xhci-trace.h" |
Sarah Sharp | 0f2a793 | 2009-04-27 19:57:12 -0700 | [diff] [blame] | 17 | |
Andiry Xu | 9777e3c | 2010-10-14 07:23:03 -0700 | [diff] [blame] | 18 | #define PORT_WAKE_BITS (PORT_WKOC_E | PORT_WKDISC_E | PORT_WKCONN_E) |
| 19 | #define PORT_RWC_BITS (PORT_CSC | PORT_PEC | PORT_WRC | PORT_OCC | \ |
| 20 | PORT_RC | PORT_PLC | PORT_PE) |
| 21 | |
Mathias Nyman | 5693e0b | 2015-10-01 18:40:35 +0300 | [diff] [blame] | 22 | /* USB 3 BOS descriptor and a capability descriptors, combined. |
| 23 | * Fields will be adjusted and added later in xhci_create_usb3_bos_desc() |
| 24 | */ |
Sarah Sharp | 48e8236 | 2011-10-06 11:54:23 -0700 | [diff] [blame] | 25 | static u8 usb_bos_descriptor [] = { |
| 26 | USB_DT_BOS_SIZE, /* __u8 bLength, 5 bytes */ |
| 27 | USB_DT_BOS, /* __u8 bDescriptorType */ |
| 28 | 0x0F, 0x00, /* __le16 wTotalLength, 15 bytes */ |
| 29 | 0x1, /* __u8 bNumDeviceCaps */ |
Mathias Nyman | 5693e0b | 2015-10-01 18:40:35 +0300 | [diff] [blame] | 30 | /* First device capability, SuperSpeed */ |
Sarah Sharp | 48e8236 | 2011-10-06 11:54:23 -0700 | [diff] [blame] | 31 | USB_DT_USB_SS_CAP_SIZE, /* __u8 bLength, 10 bytes */ |
| 32 | USB_DT_DEVICE_CAPABILITY, /* Device Capability */ |
| 33 | USB_SS_CAP_TYPE, /* bDevCapabilityType, SUPERSPEED_USB */ |
| 34 | 0x00, /* bmAttributes, LTM off by default */ |
| 35 | USB_5GBPS_OPERATION, 0x00, /* wSpeedsSupported, 5Gbps only */ |
| 36 | 0x03, /* bFunctionalitySupport, |
| 37 | USB 3.0 speed only */ |
| 38 | 0x00, /* bU1DevExitLat, set later. */ |
Mathias Nyman | 5693e0b | 2015-10-01 18:40:35 +0300 | [diff] [blame] | 39 | 0x00, 0x00, /* __le16 bU2DevExitLat, set later. */ |
| 40 | /* Second device capability, SuperSpeedPlus */ |
Mathias Nyman | 5da665f | 2016-01-25 15:30:46 +0200 | [diff] [blame] | 41 | 0x1c, /* bLength 28, will be adjusted later */ |
Mathias Nyman | 5693e0b | 2015-10-01 18:40:35 +0300 | [diff] [blame] | 42 | USB_DT_DEVICE_CAPABILITY, /* Device Capability */ |
| 43 | USB_SSP_CAP_TYPE, /* bDevCapabilityType SUPERSPEED_PLUS */ |
| 44 | 0x00, /* bReserved 0 */ |
Mathias Nyman | 5da665f | 2016-01-25 15:30:46 +0200 | [diff] [blame] | 45 | 0x23, 0x00, 0x00, 0x00, /* bmAttributes, SSAC=3 SSIC=1 */ |
| 46 | 0x01, 0x00, /* wFunctionalitySupport */ |
Mathias Nyman | 5693e0b | 2015-10-01 18:40:35 +0300 | [diff] [blame] | 47 | 0x00, 0x00, /* wReserved 0 */ |
Mathias Nyman | 5da665f | 2016-01-25 15:30:46 +0200 | [diff] [blame] | 48 | /* Default Sublink Speed Attributes, overwrite if custom PSI exists */ |
| 49 | 0x34, 0x00, 0x05, 0x00, /* 5Gbps, symmetric, rx, ID = 4 */ |
| 50 | 0xb4, 0x00, 0x05, 0x00, /* 5Gbps, symmetric, tx, ID = 4 */ |
| 51 | 0x35, 0x40, 0x0a, 0x00, /* 10Gbps, SSP, symmetric, rx, ID = 5 */ |
| 52 | 0xb5, 0x40, 0x0a, 0x00, /* 10Gbps, SSP, symmetric, tx, ID = 5 */ |
Sarah Sharp | 48e8236 | 2011-10-06 11:54:23 -0700 | [diff] [blame] | 53 | }; |
| 54 | |
Mathias Nyman | 5693e0b | 2015-10-01 18:40:35 +0300 | [diff] [blame] | 55 | static int xhci_create_usb3_bos_desc(struct xhci_hcd *xhci, char *buf, |
| 56 | u16 wLength) |
| 57 | { |
| 58 | int i, ssa_count; |
| 59 | u32 temp; |
| 60 | u16 desc_size, ssp_cap_size, ssa_size = 0; |
| 61 | bool usb3_1 = false; |
| 62 | |
| 63 | desc_size = USB_DT_BOS_SIZE + USB_DT_USB_SS_CAP_SIZE; |
| 64 | ssp_cap_size = sizeof(usb_bos_descriptor) - desc_size; |
| 65 | |
| 66 | /* does xhci support USB 3.1 Enhanced SuperSpeed */ |
Mathias Nyman | 5da665f | 2016-01-25 15:30:46 +0200 | [diff] [blame] | 67 | if (xhci->usb3_rhub.min_rev >= 0x01) { |
| 68 | /* does xhci provide a PSI table for SSA speed attributes? */ |
| 69 | if (xhci->usb3_rhub.psi_count) { |
| 70 | /* two SSA entries for each unique PSI ID, RX and TX */ |
| 71 | ssa_count = xhci->usb3_rhub.psi_uid_count * 2; |
| 72 | ssa_size = ssa_count * sizeof(u32); |
| 73 | ssp_cap_size -= 16; /* skip copying the default SSA */ |
| 74 | } |
Mathias Nyman | 5693e0b | 2015-10-01 18:40:35 +0300 | [diff] [blame] | 75 | desc_size += ssp_cap_size; |
| 76 | usb3_1 = true; |
| 77 | } |
| 78 | memcpy(buf, &usb_bos_descriptor, min(desc_size, wLength)); |
| 79 | |
| 80 | if (usb3_1) { |
| 81 | /* modify bos descriptor bNumDeviceCaps and wTotalLength */ |
| 82 | buf[4] += 1; |
| 83 | put_unaligned_le16(desc_size + ssa_size, &buf[2]); |
| 84 | } |
| 85 | |
| 86 | if (wLength < USB_DT_BOS_SIZE + USB_DT_USB_SS_CAP_SIZE) |
| 87 | return wLength; |
| 88 | |
| 89 | /* Indicate whether the host has LTM support. */ |
| 90 | temp = readl(&xhci->cap_regs->hcc_params); |
| 91 | if (HCC_LTC(temp)) |
| 92 | buf[8] |= USB_LTM_SUPPORT; |
| 93 | |
| 94 | /* Set the U1 and U2 exit latencies. */ |
| 95 | if ((xhci->quirks & XHCI_LPM_SUPPORT)) { |
| 96 | temp = readl(&xhci->cap_regs->hcs_params3); |
| 97 | buf[12] = HCS_U1_LATENCY(temp); |
| 98 | put_unaligned_le16(HCS_U2_LATENCY(temp), &buf[13]); |
| 99 | } |
| 100 | |
Mathias Nyman | 5da665f | 2016-01-25 15:30:46 +0200 | [diff] [blame] | 101 | /* If PSI table exists, add the custom speed attributes from it */ |
| 102 | if (usb3_1 && xhci->usb3_rhub.psi_count) { |
Mathias Nyman | 7bea22b | 2017-09-18 17:39:18 +0300 | [diff] [blame] | 103 | u32 ssp_cap_base, bm_attrib, psi, psi_mant, psi_exp; |
Mathias Nyman | 5693e0b | 2015-10-01 18:40:35 +0300 | [diff] [blame] | 104 | int offset; |
| 105 | |
| 106 | ssp_cap_base = USB_DT_BOS_SIZE + USB_DT_USB_SS_CAP_SIZE; |
| 107 | |
| 108 | if (wLength < desc_size) |
| 109 | return wLength; |
| 110 | buf[ssp_cap_base] = ssp_cap_size + ssa_size; |
| 111 | |
| 112 | /* attribute count SSAC bits 4:0 and ID count SSIC bits 8:5 */ |
| 113 | bm_attrib = (ssa_count - 1) & 0x1f; |
| 114 | bm_attrib |= (xhci->usb3_rhub.psi_uid_count - 1) << 5; |
| 115 | put_unaligned_le32(bm_attrib, &buf[ssp_cap_base + 4]); |
| 116 | |
| 117 | if (wLength < desc_size + ssa_size) |
| 118 | return wLength; |
| 119 | /* |
| 120 | * Create the Sublink Speed Attributes (SSA) array. |
| 121 | * The xhci PSI field and USB 3.1 SSA fields are very similar, |
| 122 | * but link type bits 7:6 differ for values 01b and 10b. |
| 123 | * xhci has also only one PSI entry for a symmetric link when |
| 124 | * USB 3.1 requires two SSA entries (RX and TX) for every link |
| 125 | */ |
| 126 | offset = desc_size; |
| 127 | for (i = 0; i < xhci->usb3_rhub.psi_count; i++) { |
| 128 | psi = xhci->usb3_rhub.psi[i]; |
| 129 | psi &= ~USB_SSP_SUBLINK_SPEED_RSVD; |
Mathias Nyman | 7bea22b | 2017-09-18 17:39:18 +0300 | [diff] [blame] | 130 | psi_exp = XHCI_EXT_PORT_PSIE(psi); |
| 131 | psi_mant = XHCI_EXT_PORT_PSIM(psi); |
| 132 | |
| 133 | /* Shift to Gbps and set SSP Link BIT(14) if 10Gpbs */ |
| 134 | for (; psi_exp < 3; psi_exp++) |
| 135 | psi_mant /= 1000; |
| 136 | if (psi_mant >= 10) |
| 137 | psi |= BIT(14); |
| 138 | |
Mathias Nyman | 5693e0b | 2015-10-01 18:40:35 +0300 | [diff] [blame] | 139 | if ((psi & PLT_MASK) == PLT_SYM) { |
| 140 | /* Symmetric, create SSA RX and TX from one PSI entry */ |
| 141 | put_unaligned_le32(psi, &buf[offset]); |
| 142 | psi |= 1 << 7; /* turn entry to TX */ |
| 143 | offset += 4; |
| 144 | if (offset >= desc_size + ssa_size) |
| 145 | return desc_size + ssa_size; |
| 146 | } else if ((psi & PLT_MASK) == PLT_ASYM_RX) { |
| 147 | /* Asymetric RX, flip bits 7:6 for SSA */ |
| 148 | psi ^= PLT_MASK; |
| 149 | } |
| 150 | put_unaligned_le32(psi, &buf[offset]); |
| 151 | offset += 4; |
| 152 | if (offset >= desc_size + ssa_size) |
| 153 | return desc_size + ssa_size; |
| 154 | } |
| 155 | } |
| 156 | /* ssa_size is 0 for other than usb 3.1 hosts */ |
| 157 | return desc_size + ssa_size; |
| 158 | } |
Sarah Sharp | 48e8236 | 2011-10-06 11:54:23 -0700 | [diff] [blame] | 159 | |
Sarah Sharp | 4bbb0ac | 2010-11-29 16:14:37 -0800 | [diff] [blame] | 160 | static void xhci_common_hub_descriptor(struct xhci_hcd *xhci, |
| 161 | struct usb_hub_descriptor *desc, int ports) |
Sarah Sharp | 0f2a793 | 2009-04-27 19:57:12 -0700 | [diff] [blame] | 162 | { |
Sarah Sharp | 0f2a793 | 2009-04-27 19:57:12 -0700 | [diff] [blame] | 163 | u16 temp; |
| 164 | |
Sarah Sharp | 0f2a793 | 2009-04-27 19:57:12 -0700 | [diff] [blame] | 165 | desc->bPwrOn2PwrGood = 10; /* xhci section 5.4.9 says 20ms max */ |
| 166 | desc->bHubContrCurrent = 0; |
| 167 | |
| 168 | desc->bNbrPorts = ports; |
Sarah Sharp | 0f2a793 | 2009-04-27 19:57:12 -0700 | [diff] [blame] | 169 | temp = 0; |
Aman Deep | c842114 | 2011-11-22 19:33:36 +0530 | [diff] [blame] | 170 | /* Bits 1:0 - support per-port power switching, or power always on */ |
Sarah Sharp | 0f2a793 | 2009-04-27 19:57:12 -0700 | [diff] [blame] | 171 | if (HCC_PPC(xhci->hcc_params)) |
Aman Deep | c842114 | 2011-11-22 19:33:36 +0530 | [diff] [blame] | 172 | temp |= HUB_CHAR_INDV_PORT_LPSM; |
Sarah Sharp | 0f2a793 | 2009-04-27 19:57:12 -0700 | [diff] [blame] | 173 | else |
Aman Deep | c842114 | 2011-11-22 19:33:36 +0530 | [diff] [blame] | 174 | temp |= HUB_CHAR_NO_LPSM; |
Sarah Sharp | 0f2a793 | 2009-04-27 19:57:12 -0700 | [diff] [blame] | 175 | /* Bit 2 - root hubs are not part of a compound device */ |
| 176 | /* Bits 4:3 - individual port over current protection */ |
Aman Deep | c842114 | 2011-11-22 19:33:36 +0530 | [diff] [blame] | 177 | temp |= HUB_CHAR_INDV_PORT_OCPM; |
Sarah Sharp | 0f2a793 | 2009-04-27 19:57:12 -0700 | [diff] [blame] | 178 | /* Bits 6:5 - no TTs in root ports */ |
| 179 | /* Bit 7 - no port indicators */ |
Matt Evans | 28ccd29 | 2011-03-29 13:40:46 +1100 | [diff] [blame] | 180 | desc->wHubCharacteristics = cpu_to_le16(temp); |
Sarah Sharp | 0f2a793 | 2009-04-27 19:57:12 -0700 | [diff] [blame] | 181 | } |
| 182 | |
Sarah Sharp | 4bbb0ac | 2010-11-29 16:14:37 -0800 | [diff] [blame] | 183 | /* Fill in the USB 2.0 roothub descriptor */ |
| 184 | static void xhci_usb2_hub_descriptor(struct usb_hcd *hcd, struct xhci_hcd *xhci, |
| 185 | struct usb_hub_descriptor *desc) |
| 186 | { |
| 187 | int ports; |
| 188 | u16 temp; |
| 189 | __u8 port_removable[(USB_MAXCHILDREN + 1 + 7) / 8]; |
| 190 | u32 portsc; |
| 191 | unsigned int i; |
Mathias Nyman | e740b01 | 2018-05-21 16:39:55 +0300 | [diff] [blame] | 192 | struct xhci_hub *rhub; |
Sarah Sharp | 4bbb0ac | 2010-11-29 16:14:37 -0800 | [diff] [blame] | 193 | |
Mathias Nyman | e740b01 | 2018-05-21 16:39:55 +0300 | [diff] [blame] | 194 | rhub = &xhci->usb2_rhub; |
| 195 | ports = rhub->num_ports; |
Sarah Sharp | 4bbb0ac | 2010-11-29 16:14:37 -0800 | [diff] [blame] | 196 | xhci_common_hub_descriptor(xhci, desc, ports); |
Aman Deep | c842114 | 2011-11-22 19:33:36 +0530 | [diff] [blame] | 197 | desc->bDescriptorType = USB_DT_HUB; |
Sarah Sharp | 4bbb0ac | 2010-11-29 16:14:37 -0800 | [diff] [blame] | 198 | temp = 1 + (ports / 8); |
Aman Deep | c842114 | 2011-11-22 19:33:36 +0530 | [diff] [blame] | 199 | desc->bDescLength = USB_DT_HUB_NONVAR_SIZE + 2 * temp; |
Sarah Sharp | 4bbb0ac | 2010-11-29 16:14:37 -0800 | [diff] [blame] | 200 | |
| 201 | /* The Device Removable bits are reported on a byte granularity. |
| 202 | * If the port doesn't exist within that byte, the bit is set to 0. |
| 203 | */ |
| 204 | memset(port_removable, 0, sizeof(port_removable)); |
| 205 | for (i = 0; i < ports; i++) { |
Mathias Nyman | e740b01 | 2018-05-21 16:39:55 +0300 | [diff] [blame] | 206 | portsc = readl(rhub->ports[i]->addr); |
Sarah Sharp | 4bbb0ac | 2010-11-29 16:14:37 -0800 | [diff] [blame] | 207 | /* If a device is removable, PORTSC reports a 0, same as in the |
| 208 | * hub descriptor DeviceRemovable bits. |
| 209 | */ |
| 210 | if (portsc & PORT_DEV_REMOVE) |
| 211 | /* This math is hairy because bit 0 of DeviceRemovable |
| 212 | * is reserved, and bit 1 is for port 1, etc. |
| 213 | */ |
| 214 | port_removable[(i + 1) / 8] |= 1 << ((i + 1) % 8); |
| 215 | } |
| 216 | |
| 217 | /* ch11.h defines a hub descriptor that has room for USB_MAXCHILDREN |
| 218 | * ports on it. The USB 2.0 specification says that there are two |
| 219 | * variable length fields at the end of the hub descriptor: |
| 220 | * DeviceRemovable and PortPwrCtrlMask. But since we can have less than |
| 221 | * USB_MAXCHILDREN ports, we may need to use the DeviceRemovable array |
| 222 | * to set PortPwrCtrlMask bits. PortPwrCtrlMask must always be set to |
| 223 | * 0xFF, so we initialize the both arrays (DeviceRemovable and |
| 224 | * PortPwrCtrlMask) to 0xFF. Then we set the DeviceRemovable for each |
| 225 | * set of ports that actually exist. |
| 226 | */ |
| 227 | memset(desc->u.hs.DeviceRemovable, 0xff, |
| 228 | sizeof(desc->u.hs.DeviceRemovable)); |
| 229 | memset(desc->u.hs.PortPwrCtrlMask, 0xff, |
| 230 | sizeof(desc->u.hs.PortPwrCtrlMask)); |
| 231 | |
| 232 | for (i = 0; i < (ports + 1 + 7) / 8; i++) |
| 233 | memset(&desc->u.hs.DeviceRemovable[i], port_removable[i], |
| 234 | sizeof(__u8)); |
| 235 | } |
| 236 | |
| 237 | /* Fill in the USB 3.0 roothub descriptor */ |
| 238 | static void xhci_usb3_hub_descriptor(struct usb_hcd *hcd, struct xhci_hcd *xhci, |
| 239 | struct usb_hub_descriptor *desc) |
| 240 | { |
| 241 | int ports; |
| 242 | u16 port_removable; |
| 243 | u32 portsc; |
| 244 | unsigned int i; |
Mathias Nyman | e740b01 | 2018-05-21 16:39:55 +0300 | [diff] [blame] | 245 | struct xhci_hub *rhub; |
Sarah Sharp | 4bbb0ac | 2010-11-29 16:14:37 -0800 | [diff] [blame] | 246 | |
Mathias Nyman | e740b01 | 2018-05-21 16:39:55 +0300 | [diff] [blame] | 247 | rhub = &xhci->usb3_rhub; |
| 248 | ports = rhub->num_ports; |
Sarah Sharp | 4bbb0ac | 2010-11-29 16:14:37 -0800 | [diff] [blame] | 249 | xhci_common_hub_descriptor(xhci, desc, ports); |
Aman Deep | c842114 | 2011-11-22 19:33:36 +0530 | [diff] [blame] | 250 | desc->bDescriptorType = USB_DT_SS_HUB; |
| 251 | desc->bDescLength = USB_DT_SS_HUB_SIZE; |
Sarah Sharp | 4bbb0ac | 2010-11-29 16:14:37 -0800 | [diff] [blame] | 252 | |
| 253 | /* header decode latency should be zero for roothubs, |
| 254 | * see section 4.23.5.2. |
| 255 | */ |
| 256 | desc->u.ss.bHubHdrDecLat = 0; |
| 257 | desc->u.ss.wHubDelay = 0; |
| 258 | |
| 259 | port_removable = 0; |
| 260 | /* bit 0 is reserved, bit 1 is for port 1, etc. */ |
| 261 | for (i = 0; i < ports; i++) { |
Mathias Nyman | e740b01 | 2018-05-21 16:39:55 +0300 | [diff] [blame] | 262 | portsc = readl(rhub->ports[i]->addr); |
Sarah Sharp | 4bbb0ac | 2010-11-29 16:14:37 -0800 | [diff] [blame] | 263 | if (portsc & PORT_DEV_REMOVE) |
| 264 | port_removable |= 1 << (i + 1); |
| 265 | } |
Lan Tianyu | 27c411c | 2012-10-15 15:38:35 +0800 | [diff] [blame] | 266 | |
| 267 | desc->u.ss.DeviceRemovable = cpu_to_le16(port_removable); |
Sarah Sharp | 4bbb0ac | 2010-11-29 16:14:37 -0800 | [diff] [blame] | 268 | } |
| 269 | |
| 270 | static void xhci_hub_descriptor(struct usb_hcd *hcd, struct xhci_hcd *xhci, |
| 271 | struct usb_hub_descriptor *desc) |
| 272 | { |
| 273 | |
Mathias Nyman | b50107b | 2015-10-01 18:40:38 +0300 | [diff] [blame] | 274 | if (hcd->speed >= HCD_USB3) |
Sarah Sharp | 4bbb0ac | 2010-11-29 16:14:37 -0800 | [diff] [blame] | 275 | xhci_usb3_hub_descriptor(hcd, xhci, desc); |
| 276 | else |
| 277 | xhci_usb2_hub_descriptor(hcd, xhci, desc); |
| 278 | |
| 279 | } |
| 280 | |
Sarah Sharp | 0f2a793 | 2009-04-27 19:57:12 -0700 | [diff] [blame] | 281 | static unsigned int xhci_port_speed(unsigned int port_status) |
| 282 | { |
| 283 | if (DEV_LOWSPEED(port_status)) |
Alan Stern | 288ead4 | 2010-03-04 11:32:30 -0500 | [diff] [blame] | 284 | return USB_PORT_STAT_LOW_SPEED; |
Sarah Sharp | 0f2a793 | 2009-04-27 19:57:12 -0700 | [diff] [blame] | 285 | if (DEV_HIGHSPEED(port_status)) |
Alan Stern | 288ead4 | 2010-03-04 11:32:30 -0500 | [diff] [blame] | 286 | return USB_PORT_STAT_HIGH_SPEED; |
Sarah Sharp | 0f2a793 | 2009-04-27 19:57:12 -0700 | [diff] [blame] | 287 | /* |
| 288 | * FIXME: Yes, we should check for full speed, but the core uses that as |
| 289 | * a default in portspeed() in usb/core/hub.c (which is the only place |
Alan Stern | 288ead4 | 2010-03-04 11:32:30 -0500 | [diff] [blame] | 290 | * USB_PORT_STAT_*_SPEED is used). |
Sarah Sharp | 0f2a793 | 2009-04-27 19:57:12 -0700 | [diff] [blame] | 291 | */ |
| 292 | return 0; |
| 293 | } |
| 294 | |
| 295 | /* |
| 296 | * These bits are Read Only (RO) and should be saved and written to the |
| 297 | * registers: 0, 3, 10:13, 30 |
| 298 | * connect status, over-current status, port speed, and device removable. |
| 299 | * connect status and port speed are also sticky - meaning they're in |
| 300 | * the AUX well and they aren't changed by a hot, warm, or cold reset. |
| 301 | */ |
| 302 | #define XHCI_PORT_RO ((1<<0) | (1<<3) | (0xf<<10) | (1<<30)) |
| 303 | /* |
| 304 | * These bits are RW; writing a 0 clears the bit, writing a 1 sets the bit: |
| 305 | * bits 5:8, 9, 14:15, 25:27 |
| 306 | * link state, port power, port indicator state, "wake on" enable state |
| 307 | */ |
| 308 | #define XHCI_PORT_RWS ((0xf<<5) | (1<<9) | (0x3<<14) | (0x7<<25)) |
| 309 | /* |
| 310 | * These bits are RW; writing a 1 sets the bit, writing a 0 has no effect: |
| 311 | * bit 4 (port reset) |
| 312 | */ |
| 313 | #define XHCI_PORT_RW1S ((1<<4)) |
| 314 | /* |
| 315 | * These bits are RW; writing a 1 clears the bit, writing a 0 has no effect: |
| 316 | * bits 1, 17, 18, 19, 20, 21, 22, 23 |
| 317 | * port enable/disable, and |
| 318 | * change bits: connect, PED, warm port reset changed (reserved zero for USB 2.0 ports), |
| 319 | * over-current, reset, link state, and L1 change |
| 320 | */ |
| 321 | #define XHCI_PORT_RW1CS ((1<<1) | (0x7f<<17)) |
| 322 | /* |
| 323 | * Bit 16 is RW, and writing a '1' to it causes the link state control to be |
| 324 | * latched in |
| 325 | */ |
| 326 | #define XHCI_PORT_RW ((1<<16)) |
| 327 | /* |
| 328 | * These bits are Reserved Zero (RsvdZ) and zero should be written to them: |
| 329 | * bits 2, 24, 28:31 |
| 330 | */ |
| 331 | #define XHCI_PORT_RZ ((1<<2) | (1<<24) | (0xf<<28)) |
| 332 | |
| 333 | /* |
| 334 | * Given a port state, this function returns a value that would result in the |
| 335 | * port being in the same state, if the value was written to the port status |
| 336 | * control register. |
| 337 | * Save Read Only (RO) bits and save read/write bits where |
| 338 | * writing a 0 clears the bit and writing a 1 sets the bit (RWS). |
| 339 | * For all other types (RW1S, RW1CS, RW, and RZ), writing a '0' has no effect. |
| 340 | */ |
Andiry Xu | 5619253 | 2010-10-14 07:23:00 -0700 | [diff] [blame] | 341 | u32 xhci_port_state_to_neutral(u32 state) |
Sarah Sharp | 0f2a793 | 2009-04-27 19:57:12 -0700 | [diff] [blame] | 342 | { |
| 343 | /* Save read-only status and port state */ |
| 344 | return (state & XHCI_PORT_RO) | (state & XHCI_PORT_RWS); |
| 345 | } |
| 346 | |
Andiry Xu | be88fe4 | 2010-10-14 07:22:57 -0700 | [diff] [blame] | 347 | /* |
| 348 | * find slot id based on port number. |
Sarah Sharp | f6ff0ac | 2010-12-16 11:21:10 -0800 | [diff] [blame] | 349 | * @port: The one-based port number from one of the two split roothubs. |
Andiry Xu | be88fe4 | 2010-10-14 07:22:57 -0700 | [diff] [blame] | 350 | */ |
Sarah Sharp | 5233630 | 2010-12-16 10:49:09 -0800 | [diff] [blame] | 351 | int xhci_find_slot_id_by_port(struct usb_hcd *hcd, struct xhci_hcd *xhci, |
| 352 | u16 port) |
Andiry Xu | be88fe4 | 2010-10-14 07:22:57 -0700 | [diff] [blame] | 353 | { |
| 354 | int slot_id; |
| 355 | int i; |
Sarah Sharp | f6ff0ac | 2010-12-16 11:21:10 -0800 | [diff] [blame] | 356 | enum usb_device_speed speed; |
Andiry Xu | be88fe4 | 2010-10-14 07:22:57 -0700 | [diff] [blame] | 357 | |
| 358 | slot_id = 0; |
| 359 | for (i = 0; i < MAX_HC_SLOTS; i++) { |
Mathias Nyman | 2278446 | 2018-05-14 11:57:23 +0300 | [diff] [blame] | 360 | if (!xhci->devs[i] || !xhci->devs[i]->udev) |
Andiry Xu | be88fe4 | 2010-10-14 07:22:57 -0700 | [diff] [blame] | 361 | continue; |
Sarah Sharp | f6ff0ac | 2010-12-16 11:21:10 -0800 | [diff] [blame] | 362 | speed = xhci->devs[i]->udev->speed; |
Mathias Nyman | b50107b | 2015-10-01 18:40:38 +0300 | [diff] [blame] | 363 | if (((speed >= USB_SPEED_SUPER) == (hcd->speed >= HCD_USB3)) |
Sarah Sharp | fe30182 | 2011-09-02 11:05:41 -0700 | [diff] [blame] | 364 | && xhci->devs[i]->fake_port == port) { |
Andiry Xu | be88fe4 | 2010-10-14 07:22:57 -0700 | [diff] [blame] | 365 | slot_id = i; |
| 366 | break; |
| 367 | } |
| 368 | } |
| 369 | |
| 370 | return slot_id; |
| 371 | } |
| 372 | |
| 373 | /* |
| 374 | * Stop device |
| 375 | * It issues stop endpoint command for EP 0 to 30. And wait the last command |
| 376 | * to complete. |
| 377 | * suspend will set to 1, if suspend bit need to set in command. |
| 378 | */ |
| 379 | static int xhci_stop_device(struct xhci_hcd *xhci, int slot_id, int suspend) |
| 380 | { |
| 381 | struct xhci_virt_device *virt_dev; |
| 382 | struct xhci_command *cmd; |
| 383 | unsigned long flags; |
Andiry Xu | be88fe4 | 2010-10-14 07:22:57 -0700 | [diff] [blame] | 384 | int ret; |
| 385 | int i; |
| 386 | |
| 387 | ret = 0; |
| 388 | virt_dev = xhci->devs[slot_id]; |
Jim Lin | 88716a9 | 2016-08-16 10:18:05 +0300 | [diff] [blame] | 389 | if (!virt_dev) |
| 390 | return -ENODEV; |
| 391 | |
Felipe Balbi | a711ede | 2017-01-23 14:20:23 +0200 | [diff] [blame] | 392 | trace_xhci_stop_device(virt_dev); |
| 393 | |
Mathias Nyman | 103afda | 2017-12-08 17:59:08 +0200 | [diff] [blame] | 394 | cmd = xhci_alloc_command(xhci, true, GFP_NOIO); |
Lu Baolu | 74e0b56 | 2017-04-07 17:57:05 +0300 | [diff] [blame] | 395 | if (!cmd) |
Andiry Xu | be88fe4 | 2010-10-14 07:22:57 -0700 | [diff] [blame] | 396 | return -ENOMEM; |
Andiry Xu | be88fe4 | 2010-10-14 07:22:57 -0700 | [diff] [blame] | 397 | |
| 398 | spin_lock_irqsave(&xhci->lock, flags); |
| 399 | for (i = LAST_EP_INDEX; i > 0; i--) { |
Mathias Nyman | ddba5cd | 2014-05-08 19:26:00 +0300 | [diff] [blame] | 400 | if (virt_dev->eps[i].ring && virt_dev->eps[i].ring->dequeue) { |
Shyam Sundar S K | 28a2369 | 2017-07-20 14:48:28 +0300 | [diff] [blame] | 401 | struct xhci_ep_ctx *ep_ctx; |
Mathias Nyman | ddba5cd | 2014-05-08 19:26:00 +0300 | [diff] [blame] | 402 | struct xhci_command *command; |
Shyam Sundar S K | 28a2369 | 2017-07-20 14:48:28 +0300 | [diff] [blame] | 403 | |
| 404 | ep_ctx = xhci_get_ep_ctx(xhci, virt_dev->out_ctx, i); |
| 405 | |
| 406 | /* Check ep is running, required by AMD SNPS 3.1 xHC */ |
| 407 | if (GET_EP_CTX_STATE(ep_ctx) != EP_STATE_RUNNING) |
| 408 | continue; |
| 409 | |
Mathias Nyman | 103afda | 2017-12-08 17:59:08 +0200 | [diff] [blame] | 410 | command = xhci_alloc_command(xhci, false, GFP_NOWAIT); |
Mathias Nyman | ddba5cd | 2014-05-08 19:26:00 +0300 | [diff] [blame] | 411 | if (!command) { |
| 412 | spin_unlock_irqrestore(&xhci->lock, flags); |
Mayank Rana | b3207c6 | 2017-10-06 17:45:30 +0300 | [diff] [blame] | 413 | ret = -ENOMEM; |
| 414 | goto cmd_cleanup; |
Mathias Nyman | ddba5cd | 2014-05-08 19:26:00 +0300 | [diff] [blame] | 415 | } |
Mayank Rana | b3207c6 | 2017-10-06 17:45:30 +0300 | [diff] [blame] | 416 | |
| 417 | ret = xhci_queue_stop_endpoint(xhci, command, slot_id, |
| 418 | i, suspend); |
| 419 | if (ret) { |
| 420 | spin_unlock_irqrestore(&xhci->lock, flags); |
| 421 | xhci_free_command(xhci, command); |
| 422 | goto cmd_cleanup; |
| 423 | } |
Mathias Nyman | ddba5cd | 2014-05-08 19:26:00 +0300 | [diff] [blame] | 424 | } |
Andiry Xu | be88fe4 | 2010-10-14 07:22:57 -0700 | [diff] [blame] | 425 | } |
Mayank Rana | b3207c6 | 2017-10-06 17:45:30 +0300 | [diff] [blame] | 426 | ret = xhci_queue_stop_endpoint(xhci, cmd, slot_id, 0, suspend); |
| 427 | if (ret) { |
| 428 | spin_unlock_irqrestore(&xhci->lock, flags); |
| 429 | goto cmd_cleanup; |
| 430 | } |
| 431 | |
Andiry Xu | be88fe4 | 2010-10-14 07:22:57 -0700 | [diff] [blame] | 432 | xhci_ring_cmd_db(xhci); |
| 433 | spin_unlock_irqrestore(&xhci->lock, flags); |
| 434 | |
| 435 | /* Wait for last stop endpoint command to finish */ |
Mathias Nyman | c311e39 | 2014-05-08 19:26:03 +0300 | [diff] [blame] | 436 | wait_for_completion(cmd->completion); |
| 437 | |
Felipe Balbi | 0b7c105 | 2017-01-23 14:20:06 +0200 | [diff] [blame] | 438 | if (cmd->status == COMP_COMMAND_ABORTED || |
Mathias Nyman | 604d02a | 2017-05-17 18:32:05 +0300 | [diff] [blame] | 439 | cmd->status == COMP_COMMAND_RING_STOPPED) { |
Mathias Nyman | c311e39 | 2014-05-08 19:26:03 +0300 | [diff] [blame] | 440 | xhci_warn(xhci, "Timeout while waiting for stop endpoint command\n"); |
Andiry Xu | be88fe4 | 2010-10-14 07:22:57 -0700 | [diff] [blame] | 441 | ret = -ETIME; |
Andiry Xu | be88fe4 | 2010-10-14 07:22:57 -0700 | [diff] [blame] | 442 | } |
Mayank Rana | b3207c6 | 2017-10-06 17:45:30 +0300 | [diff] [blame] | 443 | |
| 444 | cmd_cleanup: |
Andiry Xu | be88fe4 | 2010-10-14 07:22:57 -0700 | [diff] [blame] | 445 | xhci_free_command(xhci, cmd); |
| 446 | return ret; |
| 447 | } |
| 448 | |
| 449 | /* |
| 450 | * Ring device, it rings the all doorbells unconditionally. |
| 451 | */ |
Andiry Xu | 5619253 | 2010-10-14 07:23:00 -0700 | [diff] [blame] | 452 | void xhci_ring_device(struct xhci_hcd *xhci, int slot_id) |
Andiry Xu | be88fe4 | 2010-10-14 07:22:57 -0700 | [diff] [blame] | 453 | { |
Hans de Goede | b7f9696 | 2014-08-20 16:41:56 +0300 | [diff] [blame] | 454 | int i, s; |
| 455 | struct xhci_virt_ep *ep; |
Andiry Xu | be88fe4 | 2010-10-14 07:22:57 -0700 | [diff] [blame] | 456 | |
Hans de Goede | b7f9696 | 2014-08-20 16:41:56 +0300 | [diff] [blame] | 457 | for (i = 0; i < LAST_EP_INDEX + 1; i++) { |
| 458 | ep = &xhci->devs[slot_id]->eps[i]; |
| 459 | |
| 460 | if (ep->ep_state & EP_HAS_STREAMS) { |
| 461 | for (s = 1; s < ep->stream_info->num_streams; s++) |
| 462 | xhci_ring_ep_doorbell(xhci, slot_id, i, s); |
| 463 | } else if (ep->ring && ep->ring->dequeue) { |
Andiry Xu | be88fe4 | 2010-10-14 07:22:57 -0700 | [diff] [blame] | 464 | xhci_ring_ep_doorbell(xhci, slot_id, i, 0); |
Hans de Goede | b7f9696 | 2014-08-20 16:41:56 +0300 | [diff] [blame] | 465 | } |
| 466 | } |
Andiry Xu | be88fe4 | 2010-10-14 07:22:57 -0700 | [diff] [blame] | 467 | |
| 468 | return; |
| 469 | } |
| 470 | |
Sarah Sharp | f6ff0ac | 2010-12-16 11:21:10 -0800 | [diff] [blame] | 471 | static void xhci_disable_port(struct usb_hcd *hcd, struct xhci_hcd *xhci, |
Matt Evans | 28ccd29 | 2011-03-29 13:40:46 +1100 | [diff] [blame] | 472 | u16 wIndex, __le32 __iomem *addr, u32 port_status) |
Sarah Sharp | 6219c047 | 2009-12-09 15:59:11 -0800 | [diff] [blame] | 473 | { |
Sarah Sharp | 6dd0a3a7 | 2010-11-16 15:58:52 -0800 | [diff] [blame] | 474 | /* Don't allow the USB core to disable SuperSpeed ports. */ |
Mathias Nyman | b50107b | 2015-10-01 18:40:38 +0300 | [diff] [blame] | 475 | if (hcd->speed >= HCD_USB3) { |
Sarah Sharp | 6dd0a3a7 | 2010-11-16 15:58:52 -0800 | [diff] [blame] | 476 | xhci_dbg(xhci, "Ignoring request to disable " |
| 477 | "SuperSpeed port.\n"); |
| 478 | return; |
| 479 | } |
| 480 | |
Felipe Balbi | 41135de | 2017-01-23 14:19:58 +0200 | [diff] [blame] | 481 | if (xhci->quirks & XHCI_BROKEN_PORT_PED) { |
| 482 | xhci_dbg(xhci, |
| 483 | "Broken Port Enabled/Disabled, ignoring port disable request.\n"); |
| 484 | return; |
| 485 | } |
| 486 | |
Sarah Sharp | 6219c047 | 2009-12-09 15:59:11 -0800 | [diff] [blame] | 487 | /* Write 1 to disable the port */ |
Xenia Ragiadakou | 204b779 | 2013-11-15 05:34:07 +0200 | [diff] [blame] | 488 | writel(port_status | PORT_PE, addr); |
Xenia Ragiadakou | b0ba972 | 2013-11-15 05:34:06 +0200 | [diff] [blame] | 489 | port_status = readl(addr); |
Mathias Nyman | d70d5a8 | 2019-04-26 16:23:30 +0300 | [diff] [blame] | 490 | xhci_dbg(xhci, "disable port %d-%d, portsc: 0x%x\n", |
| 491 | hcd->self.busnum, wIndex + 1, port_status); |
Sarah Sharp | 6219c047 | 2009-12-09 15:59:11 -0800 | [diff] [blame] | 492 | } |
| 493 | |
Sarah Sharp | 34fb562 | 2009-12-09 15:59:08 -0800 | [diff] [blame] | 494 | static void xhci_clear_port_change_bit(struct xhci_hcd *xhci, u16 wValue, |
Matt Evans | 28ccd29 | 2011-03-29 13:40:46 +1100 | [diff] [blame] | 495 | u16 wIndex, __le32 __iomem *addr, u32 port_status) |
Sarah Sharp | 34fb562 | 2009-12-09 15:59:08 -0800 | [diff] [blame] | 496 | { |
| 497 | char *port_change_bit; |
| 498 | u32 status; |
| 499 | |
| 500 | switch (wValue) { |
| 501 | case USB_PORT_FEAT_C_RESET: |
| 502 | status = PORT_RC; |
| 503 | port_change_bit = "reset"; |
| 504 | break; |
Andiry Xu | a11496e | 2011-04-27 18:07:29 +0800 | [diff] [blame] | 505 | case USB_PORT_FEAT_C_BH_PORT_RESET: |
| 506 | status = PORT_WRC; |
| 507 | port_change_bit = "warm(BH) reset"; |
| 508 | break; |
Sarah Sharp | 34fb562 | 2009-12-09 15:59:08 -0800 | [diff] [blame] | 509 | case USB_PORT_FEAT_C_CONNECTION: |
| 510 | status = PORT_CSC; |
| 511 | port_change_bit = "connect"; |
| 512 | break; |
| 513 | case USB_PORT_FEAT_C_OVER_CURRENT: |
| 514 | status = PORT_OCC; |
| 515 | port_change_bit = "over-current"; |
| 516 | break; |
Sarah Sharp | 6219c047 | 2009-12-09 15:59:11 -0800 | [diff] [blame] | 517 | case USB_PORT_FEAT_C_ENABLE: |
| 518 | status = PORT_PEC; |
| 519 | port_change_bit = "enable/disable"; |
| 520 | break; |
Andiry Xu | be88fe4 | 2010-10-14 07:22:57 -0700 | [diff] [blame] | 521 | case USB_PORT_FEAT_C_SUSPEND: |
| 522 | status = PORT_PLC; |
| 523 | port_change_bit = "suspend/resume"; |
| 524 | break; |
Andiry Xu | 85387c0 | 2011-04-27 18:07:35 +0800 | [diff] [blame] | 525 | case USB_PORT_FEAT_C_PORT_LINK_STATE: |
| 526 | status = PORT_PLC; |
| 527 | port_change_bit = "link state"; |
| 528 | break; |
Lu Baolu | 9425183 | 2015-03-23 18:27:41 +0200 | [diff] [blame] | 529 | case USB_PORT_FEAT_C_PORT_CONFIG_ERROR: |
| 530 | status = PORT_CEC; |
| 531 | port_change_bit = "config error"; |
| 532 | break; |
Sarah Sharp | 34fb562 | 2009-12-09 15:59:08 -0800 | [diff] [blame] | 533 | default: |
| 534 | /* Should never happen */ |
| 535 | return; |
| 536 | } |
| 537 | /* Change bits are all write 1 to clear */ |
Xenia Ragiadakou | 204b779 | 2013-11-15 05:34:07 +0200 | [diff] [blame] | 538 | writel(port_status | status, addr); |
Xenia Ragiadakou | b0ba972 | 2013-11-15 05:34:06 +0200 | [diff] [blame] | 539 | port_status = readl(addr); |
Mathias Nyman | d70d5a8 | 2019-04-26 16:23:30 +0300 | [diff] [blame] | 540 | |
| 541 | xhci_dbg(xhci, "clear port%d %s change, portsc: 0x%x\n", |
| 542 | wIndex + 1, port_change_bit, port_status); |
Sarah Sharp | 34fb562 | 2009-12-09 15:59:08 -0800 | [diff] [blame] | 543 | } |
| 544 | |
Mathias Nyman | ffd4b4f | 2018-05-21 16:39:54 +0300 | [diff] [blame] | 545 | struct xhci_hub *xhci_get_rhub(struct usb_hcd *hcd) |
| 546 | { |
| 547 | struct xhci_hcd *xhci = hcd_to_xhci(hcd); |
| 548 | |
| 549 | if (hcd->speed >= HCD_USB3) |
| 550 | return &xhci->usb3_rhub; |
| 551 | return &xhci->usb2_rhub; |
| 552 | } |
| 553 | |
Guoqing Zhang | a6ff6cb | 2017-04-07 17:56:51 +0300 | [diff] [blame] | 554 | /* |
| 555 | * xhci_set_port_power() must be called with xhci->lock held. |
| 556 | * It will release and re-aquire the lock while calling ACPI |
| 557 | * method. |
| 558 | */ |
| 559 | static void xhci_set_port_power(struct xhci_hcd *xhci, struct usb_hcd *hcd, |
Mathias Nyman | ec1dafe | 2017-04-13 14:01:04 +0300 | [diff] [blame] | 560 | u16 index, bool on, unsigned long *flags) |
Jules Irenge | dce174e0 | 2020-02-14 20:47:33 +0000 | [diff] [blame^] | 561 | __must_hold(&xhci->lock) |
Guoqing Zhang | a6ff6cb | 2017-04-07 17:56:51 +0300 | [diff] [blame] | 562 | { |
Mathias Nyman | e740b01 | 2018-05-21 16:39:55 +0300 | [diff] [blame] | 563 | struct xhci_hub *rhub; |
| 564 | struct xhci_port *port; |
Guoqing Zhang | a6ff6cb | 2017-04-07 17:56:51 +0300 | [diff] [blame] | 565 | u32 temp; |
Guoqing Zhang | a6ff6cb | 2017-04-07 17:56:51 +0300 | [diff] [blame] | 566 | |
Mathias Nyman | e740b01 | 2018-05-21 16:39:55 +0300 | [diff] [blame] | 567 | rhub = xhci_get_rhub(hcd); |
| 568 | port = rhub->ports[index]; |
| 569 | temp = readl(port->addr); |
Mathias Nyman | d70d5a8 | 2019-04-26 16:23:30 +0300 | [diff] [blame] | 570 | |
| 571 | xhci_dbg(xhci, "set port power %d-%d %s, portsc: 0x%x\n", |
| 572 | hcd->self.busnum, index + 1, on ? "ON" : "OFF", temp); |
| 573 | |
Guoqing Zhang | a6ff6cb | 2017-04-07 17:56:51 +0300 | [diff] [blame] | 574 | temp = xhci_port_state_to_neutral(temp); |
Mathias Nyman | d70d5a8 | 2019-04-26 16:23:30 +0300 | [diff] [blame] | 575 | |
Guoqing Zhang | a6ff6cb | 2017-04-07 17:56:51 +0300 | [diff] [blame] | 576 | if (on) { |
| 577 | /* Power on */ |
Mathias Nyman | e740b01 | 2018-05-21 16:39:55 +0300 | [diff] [blame] | 578 | writel(temp | PORT_POWER, port->addr); |
Mathias Nyman | d70d5a8 | 2019-04-26 16:23:30 +0300 | [diff] [blame] | 579 | readl(port->addr); |
Guoqing Zhang | a6ff6cb | 2017-04-07 17:56:51 +0300 | [diff] [blame] | 580 | } else { |
| 581 | /* Power off */ |
Mathias Nyman | e740b01 | 2018-05-21 16:39:55 +0300 | [diff] [blame] | 582 | writel(temp & ~PORT_POWER, port->addr); |
Guoqing Zhang | a6ff6cb | 2017-04-07 17:56:51 +0300 | [diff] [blame] | 583 | } |
| 584 | |
Mathias Nyman | ec1dafe | 2017-04-13 14:01:04 +0300 | [diff] [blame] | 585 | spin_unlock_irqrestore(&xhci->lock, *flags); |
Guoqing Zhang | a6ff6cb | 2017-04-07 17:56:51 +0300 | [diff] [blame] | 586 | temp = usb_acpi_power_manageable(hcd->self.root_hub, |
| 587 | index); |
| 588 | if (temp) |
| 589 | usb_acpi_set_power_state(hcd->self.root_hub, |
| 590 | index, on); |
Mathias Nyman | ec1dafe | 2017-04-13 14:01:04 +0300 | [diff] [blame] | 591 | spin_lock_irqsave(&xhci->lock, *flags); |
Guoqing Zhang | a6ff6cb | 2017-04-07 17:56:51 +0300 | [diff] [blame] | 592 | } |
| 593 | |
Guoqing Zhang | 0f1d832 | 2017-04-07 17:56:54 +0300 | [diff] [blame] | 594 | static void xhci_port_set_test_mode(struct xhci_hcd *xhci, |
| 595 | u16 test_mode, u16 wIndex) |
| 596 | { |
| 597 | u32 temp; |
Mathias Nyman | e740b01 | 2018-05-21 16:39:55 +0300 | [diff] [blame] | 598 | struct xhci_port *port; |
Guoqing Zhang | 0f1d832 | 2017-04-07 17:56:54 +0300 | [diff] [blame] | 599 | |
Mathias Nyman | e740b01 | 2018-05-21 16:39:55 +0300 | [diff] [blame] | 600 | /* xhci only supports test mode for usb2 ports */ |
| 601 | port = xhci->usb2_rhub.ports[wIndex]; |
| 602 | temp = readl(port->addr + PORTPMSC); |
Guoqing Zhang | 0f1d832 | 2017-04-07 17:56:54 +0300 | [diff] [blame] | 603 | temp |= test_mode << PORT_TEST_MODE_SHIFT; |
Mathias Nyman | e740b01 | 2018-05-21 16:39:55 +0300 | [diff] [blame] | 604 | writel(temp, port->addr + PORTPMSC); |
Guoqing Zhang | 0f1d832 | 2017-04-07 17:56:54 +0300 | [diff] [blame] | 605 | xhci->test_mode = test_mode; |
| 606 | if (test_mode == TEST_FORCE_EN) |
| 607 | xhci_start(xhci); |
| 608 | } |
| 609 | |
| 610 | static int xhci_enter_test_mode(struct xhci_hcd *xhci, |
Mathias Nyman | ec1dafe | 2017-04-13 14:01:04 +0300 | [diff] [blame] | 611 | u16 test_mode, u16 wIndex, unsigned long *flags) |
Guoqing Zhang | 0f1d832 | 2017-04-07 17:56:54 +0300 | [diff] [blame] | 612 | { |
| 613 | int i, retval; |
| 614 | |
| 615 | /* Disable all Device Slots */ |
| 616 | xhci_dbg(xhci, "Disable all slots\n"); |
Peter Chen | 576d554 | 2017-07-20 14:48:30 +0300 | [diff] [blame] | 617 | spin_unlock_irqrestore(&xhci->lock, *flags); |
Guoqing Zhang | 0f1d832 | 2017-04-07 17:56:54 +0300 | [diff] [blame] | 618 | for (i = 1; i <= HCS_MAX_SLOTS(xhci->hcs_params1); i++) { |
Lu Baolu | b64149c | 2017-10-05 11:21:40 +0300 | [diff] [blame] | 619 | if (!xhci->devs[i]) |
| 620 | continue; |
| 621 | |
Lu Baolu | cd3f179 | 2017-10-05 11:21:41 +0300 | [diff] [blame] | 622 | retval = xhci_disable_slot(xhci, i); |
Guoqing Zhang | 0f1d832 | 2017-04-07 17:56:54 +0300 | [diff] [blame] | 623 | if (retval) |
| 624 | xhci_err(xhci, "Failed to disable slot %d, %d. Enter test mode anyway\n", |
| 625 | i, retval); |
| 626 | } |
Peter Chen | 576d554 | 2017-07-20 14:48:30 +0300 | [diff] [blame] | 627 | spin_lock_irqsave(&xhci->lock, *flags); |
Guoqing Zhang | 0f1d832 | 2017-04-07 17:56:54 +0300 | [diff] [blame] | 628 | /* Put all ports to the Disable state by clear PP */ |
| 629 | xhci_dbg(xhci, "Disable all port (PP = 0)\n"); |
| 630 | /* Power off USB3 ports*/ |
Mathias Nyman | e740b01 | 2018-05-21 16:39:55 +0300 | [diff] [blame] | 631 | for (i = 0; i < xhci->usb3_rhub.num_ports; i++) |
Mathias Nyman | ec1dafe | 2017-04-13 14:01:04 +0300 | [diff] [blame] | 632 | xhci_set_port_power(xhci, xhci->shared_hcd, i, false, flags); |
Guoqing Zhang | 0f1d832 | 2017-04-07 17:56:54 +0300 | [diff] [blame] | 633 | /* Power off USB2 ports*/ |
Mathias Nyman | e740b01 | 2018-05-21 16:39:55 +0300 | [diff] [blame] | 634 | for (i = 0; i < xhci->usb2_rhub.num_ports; i++) |
Mathias Nyman | ec1dafe | 2017-04-13 14:01:04 +0300 | [diff] [blame] | 635 | xhci_set_port_power(xhci, xhci->main_hcd, i, false, flags); |
Guoqing Zhang | 0f1d832 | 2017-04-07 17:56:54 +0300 | [diff] [blame] | 636 | /* Stop the controller */ |
| 637 | xhci_dbg(xhci, "Stop controller\n"); |
| 638 | retval = xhci_halt(xhci); |
| 639 | if (retval) |
| 640 | return retval; |
| 641 | /* Disable runtime PM for test mode */ |
| 642 | pm_runtime_forbid(xhci_to_hcd(xhci)->self.controller); |
| 643 | /* Set PORTPMSC.PTC field to enter selected test mode */ |
| 644 | /* Port is selected by wIndex. port_id = wIndex + 1 */ |
| 645 | xhci_dbg(xhci, "Enter Test Mode: %d, Port_id=%d\n", |
| 646 | test_mode, wIndex + 1); |
| 647 | xhci_port_set_test_mode(xhci, test_mode, wIndex); |
| 648 | return retval; |
| 649 | } |
| 650 | |
| 651 | static int xhci_exit_test_mode(struct xhci_hcd *xhci) |
| 652 | { |
| 653 | int retval; |
| 654 | |
| 655 | if (!xhci->test_mode) { |
| 656 | xhci_err(xhci, "Not in test mode, do nothing.\n"); |
| 657 | return 0; |
| 658 | } |
| 659 | if (xhci->test_mode == TEST_FORCE_EN && |
| 660 | !(xhci->xhc_state & XHCI_STATE_HALTED)) { |
| 661 | retval = xhci_halt(xhci); |
| 662 | if (retval) |
| 663 | return retval; |
| 664 | } |
| 665 | pm_runtime_allow(xhci_to_hcd(xhci)->self.controller); |
| 666 | xhci->test_mode = 0; |
| 667 | return xhci_reset(xhci); |
| 668 | } |
| 669 | |
Mathias Nyman | 6b7f40f | 2018-05-21 16:39:59 +0300 | [diff] [blame] | 670 | void xhci_set_link_state(struct xhci_hcd *xhci, struct xhci_port *port, |
| 671 | u32 link_state) |
Andiry Xu | c9682df | 2011-09-23 14:19:48 -0700 | [diff] [blame] | 672 | { |
| 673 | u32 temp; |
Mathias Nyman | d70d5a8 | 2019-04-26 16:23:30 +0300 | [diff] [blame] | 674 | u32 portsc; |
Andiry Xu | c9682df | 2011-09-23 14:19:48 -0700 | [diff] [blame] | 675 | |
Mathias Nyman | d70d5a8 | 2019-04-26 16:23:30 +0300 | [diff] [blame] | 676 | portsc = readl(port->addr); |
| 677 | temp = xhci_port_state_to_neutral(portsc); |
Andiry Xu | c9682df | 2011-09-23 14:19:48 -0700 | [diff] [blame] | 678 | temp &= ~PORT_PLS_MASK; |
| 679 | temp |= PORT_LINK_STROBE | link_state; |
Mathias Nyman | 6b7f40f | 2018-05-21 16:39:59 +0300 | [diff] [blame] | 680 | writel(temp, port->addr); |
Mathias Nyman | d70d5a8 | 2019-04-26 16:23:30 +0300 | [diff] [blame] | 681 | |
| 682 | xhci_dbg(xhci, "Set port %d-%d link state, portsc: 0x%x, write 0x%x", |
| 683 | port->rhub->hcd->self.busnum, port->hcd_portnum + 1, |
| 684 | portsc, temp); |
Andiry Xu | c9682df | 2011-09-23 14:19:48 -0700 | [diff] [blame] | 685 | } |
| 686 | |
Felipe Balbi | ed384bd | 2012-08-07 14:10:03 +0300 | [diff] [blame] | 687 | static void xhci_set_remote_wake_mask(struct xhci_hcd *xhci, |
Mathias Nyman | fdcf74ff | 2018-05-21 16:39:56 +0300 | [diff] [blame] | 688 | struct xhci_port *port, u16 wake_mask) |
Sarah Sharp | 4296c70a | 2012-01-06 10:34:31 -0800 | [diff] [blame] | 689 | { |
| 690 | u32 temp; |
| 691 | |
Mathias Nyman | fdcf74ff | 2018-05-21 16:39:56 +0300 | [diff] [blame] | 692 | temp = readl(port->addr); |
Sarah Sharp | 4296c70a | 2012-01-06 10:34:31 -0800 | [diff] [blame] | 693 | temp = xhci_port_state_to_neutral(temp); |
| 694 | |
| 695 | if (wake_mask & USB_PORT_FEAT_REMOTE_WAKE_CONNECT) |
| 696 | temp |= PORT_WKCONN_E; |
| 697 | else |
| 698 | temp &= ~PORT_WKCONN_E; |
| 699 | |
| 700 | if (wake_mask & USB_PORT_FEAT_REMOTE_WAKE_DISCONNECT) |
| 701 | temp |= PORT_WKDISC_E; |
| 702 | else |
| 703 | temp &= ~PORT_WKDISC_E; |
| 704 | |
| 705 | if (wake_mask & USB_PORT_FEAT_REMOTE_WAKE_OVER_CURRENT) |
| 706 | temp |= PORT_WKOC_E; |
| 707 | else |
| 708 | temp &= ~PORT_WKOC_E; |
| 709 | |
Mathias Nyman | fdcf74ff | 2018-05-21 16:39:56 +0300 | [diff] [blame] | 710 | writel(temp, port->addr); |
Sarah Sharp | 4296c70a | 2012-01-06 10:34:31 -0800 | [diff] [blame] | 711 | } |
| 712 | |
Andiry Xu | d2f52c9 | 2011-09-23 14:19:49 -0700 | [diff] [blame] | 713 | /* Test and clear port RWC bit */ |
Mathias Nyman | eaefcf2 | 2018-05-21 16:40:00 +0300 | [diff] [blame] | 714 | void xhci_test_and_clear_bit(struct xhci_hcd *xhci, struct xhci_port *port, |
| 715 | u32 port_bit) |
Andiry Xu | d2f52c9 | 2011-09-23 14:19:49 -0700 | [diff] [blame] | 716 | { |
| 717 | u32 temp; |
| 718 | |
Mathias Nyman | eaefcf2 | 2018-05-21 16:40:00 +0300 | [diff] [blame] | 719 | temp = readl(port->addr); |
Andiry Xu | d2f52c9 | 2011-09-23 14:19:49 -0700 | [diff] [blame] | 720 | if (temp & port_bit) { |
| 721 | temp = xhci_port_state_to_neutral(temp); |
| 722 | temp |= port_bit; |
Mathias Nyman | eaefcf2 | 2018-05-21 16:40:00 +0300 | [diff] [blame] | 723 | writel(temp, port->addr); |
Andiry Xu | d2f52c9 | 2011-09-23 14:19:49 -0700 | [diff] [blame] | 724 | } |
| 725 | } |
| 726 | |
Stanislaw Ledwon | 8bea2bd | 2012-06-18 15:20:00 +0200 | [diff] [blame] | 727 | /* Updates Link Status for super Speed port */ |
Felipe Balbi | 9690858 | 2014-08-27 16:38:04 -0500 | [diff] [blame] | 728 | static void xhci_hub_report_usb3_link_state(struct xhci_hcd *xhci, |
| 729 | u32 *status, u32 status_reg) |
Stanislaw Ledwon | 8bea2bd | 2012-06-18 15:20:00 +0200 | [diff] [blame] | 730 | { |
| 731 | u32 pls = status_reg & PORT_PLS_MASK; |
| 732 | |
| 733 | /* resume state is a xHCI internal state. |
Zhuang Jin Can | 243292a | 2015-07-21 17:20:29 +0300 | [diff] [blame] | 734 | * Do not report it to usb core, instead, pretend to be U3, |
| 735 | * thus usb core knows it's not ready for transfer |
Stanislaw Ledwon | 8bea2bd | 2012-06-18 15:20:00 +0200 | [diff] [blame] | 736 | */ |
Zhuang Jin Can | 243292a | 2015-07-21 17:20:29 +0300 | [diff] [blame] | 737 | if (pls == XDEV_RESUME) { |
| 738 | *status |= USB_SS_PORT_LS_U3; |
Stanislaw Ledwon | 8bea2bd | 2012-06-18 15:20:00 +0200 | [diff] [blame] | 739 | return; |
Zhuang Jin Can | 243292a | 2015-07-21 17:20:29 +0300 | [diff] [blame] | 740 | } |
Stanislaw Ledwon | 8bea2bd | 2012-06-18 15:20:00 +0200 | [diff] [blame] | 741 | |
| 742 | /* When the CAS bit is set then warm reset |
| 743 | * should be performed on port |
| 744 | */ |
| 745 | if (status_reg & PORT_CAS) { |
| 746 | /* The CAS bit can be set while the port is |
| 747 | * in any link state. |
| 748 | * Only roothubs have CAS bit, so we |
| 749 | * pretend to be in compliance mode |
| 750 | * unless we're already in compliance |
| 751 | * or the inactive state. |
| 752 | */ |
| 753 | if (pls != USB_SS_PORT_LS_COMP_MOD && |
| 754 | pls != USB_SS_PORT_LS_SS_INACTIVE) { |
| 755 | pls = USB_SS_PORT_LS_COMP_MOD; |
| 756 | } |
| 757 | /* Return also connection bit - |
| 758 | * hub state machine resets port |
| 759 | * when this bit is set. |
| 760 | */ |
| 761 | pls |= USB_PORT_STAT_CONNECTION; |
Alexis R. Cortes | 71c731a | 2012-08-03 14:00:27 -0500 | [diff] [blame] | 762 | } else { |
| 763 | /* |
| 764 | * If CAS bit isn't set but the Port is already at |
| 765 | * Compliance Mode, fake a connection so the USB core |
| 766 | * notices the Compliance state and resets the port. |
| 767 | * This resolves an issue generated by the SN65LVPE502CP |
| 768 | * in which sometimes the port enters compliance mode |
| 769 | * caused by a delay on the host-device negotiation. |
| 770 | */ |
Felipe Balbi | 9690858 | 2014-08-27 16:38:04 -0500 | [diff] [blame] | 771 | if ((xhci->quirks & XHCI_COMP_MODE_QUIRK) && |
| 772 | (pls == USB_SS_PORT_LS_COMP_MOD)) |
Alexis R. Cortes | 71c731a | 2012-08-03 14:00:27 -0500 | [diff] [blame] | 773 | pls |= USB_PORT_STAT_CONNECTION; |
Stanislaw Ledwon | 8bea2bd | 2012-06-18 15:20:00 +0200 | [diff] [blame] | 774 | } |
Alexis R. Cortes | 71c731a | 2012-08-03 14:00:27 -0500 | [diff] [blame] | 775 | |
Stanislaw Ledwon | 8bea2bd | 2012-06-18 15:20:00 +0200 | [diff] [blame] | 776 | /* update status field */ |
| 777 | *status |= pls; |
| 778 | } |
| 779 | |
Alexis R. Cortes | 71c731a | 2012-08-03 14:00:27 -0500 | [diff] [blame] | 780 | /* |
| 781 | * Function for Compliance Mode Quirk. |
| 782 | * |
| 783 | * This Function verifies if all xhc USB3 ports have entered U0, if so, |
| 784 | * the compliance mode timer is deleted. A port won't enter |
| 785 | * compliance mode if it has previously entered U0. |
| 786 | */ |
Sachin Kamat | 5f20cf1 | 2013-09-16 12:01:34 +0530 | [diff] [blame] | 787 | static void xhci_del_comp_mod_timer(struct xhci_hcd *xhci, u32 status, |
| 788 | u16 wIndex) |
Alexis R. Cortes | 71c731a | 2012-08-03 14:00:27 -0500 | [diff] [blame] | 789 | { |
Mathias Nyman | e740b01 | 2018-05-21 16:39:55 +0300 | [diff] [blame] | 790 | u32 all_ports_seen_u0 = ((1 << xhci->usb3_rhub.num_ports) - 1); |
Alexis R. Cortes | 71c731a | 2012-08-03 14:00:27 -0500 | [diff] [blame] | 791 | bool port_in_u0 = ((status & PORT_PLS_MASK) == XDEV_U0); |
| 792 | |
| 793 | if (!(xhci->quirks & XHCI_COMP_MODE_QUIRK)) |
| 794 | return; |
| 795 | |
| 796 | if ((xhci->port_status_u0 != all_ports_seen_u0) && port_in_u0) { |
| 797 | xhci->port_status_u0 |= 1 << wIndex; |
| 798 | if (xhci->port_status_u0 == all_ports_seen_u0) { |
| 799 | del_timer_sync(&xhci->comp_mode_recovery_timer); |
Xenia Ragiadakou | 4bdfe4c | 2013-08-06 07:52:45 +0300 | [diff] [blame] | 800 | xhci_dbg_trace(xhci, trace_xhci_dbg_quirks, |
| 801 | "All USB3 ports have entered U0 already!"); |
| 802 | xhci_dbg_trace(xhci, trace_xhci_dbg_quirks, |
| 803 | "Compliance Mode Recovery Timer Deleted."); |
Alexis R. Cortes | 71c731a | 2012-08-03 14:00:27 -0500 | [diff] [blame] | 804 | } |
| 805 | } |
| 806 | } |
| 807 | |
Mathias Nyman | e67ebf1 | 2018-12-07 16:19:36 +0200 | [diff] [blame] | 808 | static int xhci_handle_usb2_port_link_resume(struct xhci_port *port, |
| 809 | u32 *status, u32 portsc, |
Mathias Nyman | bd82873 | 2019-12-11 16:20:07 +0200 | [diff] [blame] | 810 | unsigned long *flags) |
Mathias Nyman | e67ebf1 | 2018-12-07 16:19:36 +0200 | [diff] [blame] | 811 | { |
| 812 | struct xhci_bus_state *bus_state; |
| 813 | struct xhci_hcd *xhci; |
| 814 | struct usb_hcd *hcd; |
| 815 | int slot_id; |
| 816 | u32 wIndex; |
| 817 | |
| 818 | hcd = port->rhub->hcd; |
| 819 | bus_state = &port->rhub->bus_state; |
| 820 | xhci = hcd_to_xhci(hcd); |
| 821 | wIndex = port->hcd_portnum; |
| 822 | |
| 823 | if ((portsc & PORT_RESET) || !(portsc & PORT_PE)) { |
| 824 | *status = 0xffffffff; |
| 825 | return -EINVAL; |
| 826 | } |
| 827 | /* did port event handler already start resume timing? */ |
| 828 | if (!bus_state->resume_done[wIndex]) { |
| 829 | /* If not, maybe we are in a host initated resume? */ |
| 830 | if (test_bit(wIndex, &bus_state->resuming_ports)) { |
| 831 | /* Host initated resume doesn't time the resume |
| 832 | * signalling using resume_done[]. |
| 833 | * It manually sets RESUME state, sleeps 20ms |
| 834 | * and sets U0 state. This should probably be |
| 835 | * changed, but not right now. |
| 836 | */ |
| 837 | } else { |
| 838 | /* port resume was discovered now and here, |
| 839 | * start resume timing |
| 840 | */ |
| 841 | unsigned long timeout = jiffies + |
| 842 | msecs_to_jiffies(USB_RESUME_TIMEOUT); |
| 843 | |
| 844 | set_bit(wIndex, &bus_state->resuming_ports); |
| 845 | bus_state->resume_done[wIndex] = timeout; |
| 846 | mod_timer(&hcd->rh_timer, timeout); |
| 847 | usb_hcd_start_port_resume(&hcd->self, wIndex); |
| 848 | } |
| 849 | /* Has resume been signalled for USB_RESUME_TIME yet? */ |
| 850 | } else if (time_after_eq(jiffies, bus_state->resume_done[wIndex])) { |
| 851 | int time_left; |
| 852 | |
Mathias Nyman | d70d5a8 | 2019-04-26 16:23:30 +0300 | [diff] [blame] | 853 | xhci_dbg(xhci, "resume USB2 port %d-%d\n", |
| 854 | hcd->self.busnum, wIndex + 1); |
| 855 | |
Mathias Nyman | e67ebf1 | 2018-12-07 16:19:36 +0200 | [diff] [blame] | 856 | bus_state->resume_done[wIndex] = 0; |
| 857 | clear_bit(wIndex, &bus_state->resuming_ports); |
| 858 | |
| 859 | set_bit(wIndex, &bus_state->rexit_ports); |
| 860 | |
| 861 | xhci_test_and_clear_bit(xhci, port, PORT_PLC); |
| 862 | xhci_set_link_state(xhci, port, XDEV_U0); |
| 863 | |
Mathias Nyman | bd82873 | 2019-12-11 16:20:07 +0200 | [diff] [blame] | 864 | spin_unlock_irqrestore(&xhci->lock, *flags); |
Mathias Nyman | e67ebf1 | 2018-12-07 16:19:36 +0200 | [diff] [blame] | 865 | time_left = wait_for_completion_timeout( |
| 866 | &bus_state->rexit_done[wIndex], |
| 867 | msecs_to_jiffies(XHCI_MAX_REXIT_TIMEOUT_MS)); |
Mathias Nyman | bd82873 | 2019-12-11 16:20:07 +0200 | [diff] [blame] | 868 | spin_lock_irqsave(&xhci->lock, *flags); |
Mathias Nyman | e67ebf1 | 2018-12-07 16:19:36 +0200 | [diff] [blame] | 869 | |
| 870 | if (time_left) { |
| 871 | slot_id = xhci_find_slot_id_by_port(hcd, xhci, |
| 872 | wIndex + 1); |
| 873 | if (!slot_id) { |
| 874 | xhci_dbg(xhci, "slot_id is zero\n"); |
| 875 | *status = 0xffffffff; |
| 876 | return -ENODEV; |
| 877 | } |
| 878 | xhci_ring_device(xhci, slot_id); |
| 879 | } else { |
| 880 | int port_status = readl(port->addr); |
| 881 | |
Mathias Nyman | d70d5a8 | 2019-04-26 16:23:30 +0300 | [diff] [blame] | 882 | xhci_warn(xhci, "Port resume timed out, port %d-%d: 0x%x\n", |
| 883 | hcd->self.busnum, wIndex + 1, port_status); |
Mathias Nyman | e67ebf1 | 2018-12-07 16:19:36 +0200 | [diff] [blame] | 884 | *status |= USB_PORT_STAT_SUSPEND; |
| 885 | clear_bit(wIndex, &bus_state->rexit_ports); |
| 886 | } |
| 887 | |
| 888 | usb_hcd_end_port_resume(&hcd->self, wIndex); |
| 889 | bus_state->port_c_suspend |= 1 << wIndex; |
| 890 | bus_state->suspended_ports &= ~(1 << wIndex); |
| 891 | } else { |
| 892 | /* |
| 893 | * The resume has been signaling for less than |
| 894 | * USB_RESUME_TIME. Report the port status as SUSPEND, |
| 895 | * let the usbcore check port status again and clear |
| 896 | * resume signaling later. |
| 897 | */ |
| 898 | *status |= USB_PORT_STAT_SUSPEND; |
| 899 | } |
| 900 | return 0; |
| 901 | } |
| 902 | |
Mathias Nyman | 395f540 | 2015-10-01 18:40:39 +0300 | [diff] [blame] | 903 | static u32 xhci_get_ext_port_status(u32 raw_port_status, u32 port_li) |
| 904 | { |
| 905 | u32 ext_stat = 0; |
| 906 | int speed_id; |
| 907 | |
| 908 | /* only support rx and tx lane counts of 1 in usb3.1 spec */ |
| 909 | speed_id = DEV_PORT_SPEED(raw_port_status); |
| 910 | ext_stat |= speed_id; /* bits 3:0, RX speed id */ |
| 911 | ext_stat |= speed_id << 4; /* bits 7:4, TX speed id */ |
| 912 | |
| 913 | ext_stat |= PORT_RX_LANES(port_li) << 8; /* bits 11:8 Rx lane count */ |
| 914 | ext_stat |= PORT_TX_LANES(port_li) << 12; /* bits 15:12 Tx lane count */ |
| 915 | |
| 916 | return ext_stat; |
| 917 | } |
| 918 | |
Mathias Nyman | 5f78a54 | 2018-12-07 16:19:32 +0200 | [diff] [blame] | 919 | static void xhci_get_usb3_port_status(struct xhci_port *port, u32 *status, |
| 920 | u32 portsc) |
| 921 | { |
Mathias Nyman | a231ec4 | 2018-12-07 16:19:35 +0200 | [diff] [blame] | 922 | struct xhci_bus_state *bus_state; |
Mathias Nyman | 5f78a54 | 2018-12-07 16:19:32 +0200 | [diff] [blame] | 923 | struct xhci_hcd *xhci; |
Mathias Nyman | 057d476 | 2019-12-11 16:20:03 +0200 | [diff] [blame] | 924 | struct usb_hcd *hcd; |
Mathias Nyman | 5f78a54 | 2018-12-07 16:19:32 +0200 | [diff] [blame] | 925 | u32 link_state; |
| 926 | u32 portnum; |
| 927 | |
Mathias Nyman | a231ec4 | 2018-12-07 16:19:35 +0200 | [diff] [blame] | 928 | bus_state = &port->rhub->bus_state; |
Mathias Nyman | 5f78a54 | 2018-12-07 16:19:32 +0200 | [diff] [blame] | 929 | xhci = hcd_to_xhci(port->rhub->hcd); |
Mathias Nyman | 057d476 | 2019-12-11 16:20:03 +0200 | [diff] [blame] | 930 | hcd = port->rhub->hcd; |
Mathias Nyman | 5f78a54 | 2018-12-07 16:19:32 +0200 | [diff] [blame] | 931 | link_state = portsc & PORT_PLS_MASK; |
| 932 | portnum = port->hcd_portnum; |
| 933 | |
| 934 | /* USB3 specific wPortChange bits |
| 935 | * |
| 936 | * Port link change with port in resume state should not be |
| 937 | * reported to usbcore, as this is an internal state to be |
| 938 | * handled by xhci driver. Reporting PLC to usbcore may |
| 939 | * cause usbcore clearing PLC first and port change event |
| 940 | * irq won't be generated. |
| 941 | */ |
| 942 | |
| 943 | if (portsc & PORT_PLC && (link_state != XDEV_RESUME)) |
| 944 | *status |= USB_PORT_STAT_C_LINK_STATE << 16; |
| 945 | if (portsc & PORT_WRC) |
| 946 | *status |= USB_PORT_STAT_C_BH_RESET << 16; |
| 947 | if (portsc & PORT_CEC) |
| 948 | *status |= USB_PORT_STAT_C_CONFIG_ERROR << 16; |
| 949 | |
| 950 | /* USB3 specific wPortStatus bits */ |
Mathias Nyman | a231ec4 | 2018-12-07 16:19:35 +0200 | [diff] [blame] | 951 | if (portsc & PORT_POWER) { |
Mathias Nyman | 5f78a54 | 2018-12-07 16:19:32 +0200 | [diff] [blame] | 952 | *status |= USB_SS_PORT_STAT_POWER; |
Mathias Nyman | a231ec4 | 2018-12-07 16:19:35 +0200 | [diff] [blame] | 953 | /* link state handling */ |
| 954 | if (link_state == XDEV_U0) |
| 955 | bus_state->suspended_ports &= ~(1 << portnum); |
| 956 | } |
Mathias Nyman | 5f78a54 | 2018-12-07 16:19:32 +0200 | [diff] [blame] | 957 | |
Mathias Nyman | 057d476 | 2019-12-11 16:20:03 +0200 | [diff] [blame] | 958 | /* remote wake resume signaling complete */ |
| 959 | if (bus_state->port_remote_wakeup & (1 << portnum) && |
| 960 | link_state != XDEV_RESUME && |
| 961 | link_state != XDEV_RECOVERY) { |
| 962 | bus_state->port_remote_wakeup &= ~(1 << portnum); |
| 963 | usb_hcd_end_port_resume(&hcd->self, portnum); |
| 964 | } |
| 965 | |
Mathias Nyman | 5f78a54 | 2018-12-07 16:19:32 +0200 | [diff] [blame] | 966 | xhci_hub_report_usb3_link_state(xhci, status, portsc); |
| 967 | xhci_del_comp_mod_timer(xhci, portsc, portnum); |
| 968 | } |
| 969 | |
Mathias Nyman | 70e9b53 | 2018-12-07 16:19:33 +0200 | [diff] [blame] | 970 | static void xhci_get_usb2_port_status(struct xhci_port *port, u32 *status, |
Mathias Nyman | bd82873 | 2019-12-11 16:20:07 +0200 | [diff] [blame] | 971 | u32 portsc, unsigned long *flags) |
Mathias Nyman | 70e9b53 | 2018-12-07 16:19:33 +0200 | [diff] [blame] | 972 | { |
Mathias Nyman | a231ec4 | 2018-12-07 16:19:35 +0200 | [diff] [blame] | 973 | struct xhci_bus_state *bus_state; |
Mathias Nyman | 70e9b53 | 2018-12-07 16:19:33 +0200 | [diff] [blame] | 974 | u32 link_state; |
Mathias Nyman | a231ec4 | 2018-12-07 16:19:35 +0200 | [diff] [blame] | 975 | u32 portnum; |
Mathias Nyman | e67ebf1 | 2018-12-07 16:19:36 +0200 | [diff] [blame] | 976 | int ret; |
Mathias Nyman | 70e9b53 | 2018-12-07 16:19:33 +0200 | [diff] [blame] | 977 | |
Mathias Nyman | a231ec4 | 2018-12-07 16:19:35 +0200 | [diff] [blame] | 978 | bus_state = &port->rhub->bus_state; |
Mathias Nyman | 70e9b53 | 2018-12-07 16:19:33 +0200 | [diff] [blame] | 979 | link_state = portsc & PORT_PLS_MASK; |
Mathias Nyman | a231ec4 | 2018-12-07 16:19:35 +0200 | [diff] [blame] | 980 | portnum = port->hcd_portnum; |
Mathias Nyman | 70e9b53 | 2018-12-07 16:19:33 +0200 | [diff] [blame] | 981 | |
| 982 | /* USB2 wPortStatus bits */ |
| 983 | if (portsc & PORT_POWER) { |
| 984 | *status |= USB_PORT_STAT_POWER; |
| 985 | |
| 986 | /* link state is only valid if port is powered */ |
| 987 | if (link_state == XDEV_U3) |
| 988 | *status |= USB_PORT_STAT_SUSPEND; |
| 989 | if (link_state == XDEV_U2) |
| 990 | *status |= USB_PORT_STAT_L1; |
Mathias Nyman | a231ec4 | 2018-12-07 16:19:35 +0200 | [diff] [blame] | 991 | if (link_state == XDEV_U0) { |
| 992 | bus_state->resume_done[portnum] = 0; |
| 993 | clear_bit(portnum, &bus_state->resuming_ports); |
| 994 | if (bus_state->suspended_ports & (1 << portnum)) { |
| 995 | bus_state->suspended_ports &= ~(1 << portnum); |
| 996 | bus_state->port_c_suspend |= 1 << portnum; |
| 997 | } |
| 998 | } |
Mathias Nyman | e67ebf1 | 2018-12-07 16:19:36 +0200 | [diff] [blame] | 999 | if (link_state == XDEV_RESUME) { |
| 1000 | ret = xhci_handle_usb2_port_link_resume(port, status, |
| 1001 | portsc, flags); |
| 1002 | if (ret) |
| 1003 | return; |
| 1004 | } |
Mathias Nyman | 70e9b53 | 2018-12-07 16:19:33 +0200 | [diff] [blame] | 1005 | } |
| 1006 | } |
| 1007 | |
Sarah Sharp | eae5b17 | 2013-04-02 08:42:20 -0700 | [diff] [blame] | 1008 | /* |
| 1009 | * Converts a raw xHCI port status into the format that external USB 2.0 or USB |
| 1010 | * 3.0 hubs use. |
| 1011 | * |
| 1012 | * Possible side effects: |
| 1013 | * - Mark a port as being done with device resume, |
| 1014 | * and ring the endpoint doorbells. |
| 1015 | * - Stop the Synopsys redriver Compliance Mode polling. |
Sarah Sharp | 8b3d457 | 2013-08-20 08:12:12 -0700 | [diff] [blame] | 1016 | * - Drop and reacquire the xHCI lock, in order to wait for port resume. |
Sarah Sharp | eae5b17 | 2013-04-02 08:42:20 -0700 | [diff] [blame] | 1017 | */ |
| 1018 | static u32 xhci_get_port_status(struct usb_hcd *hcd, |
| 1019 | struct xhci_bus_state *bus_state, |
Mathias Nyman | eaefcf2 | 2018-05-21 16:40:00 +0300 | [diff] [blame] | 1020 | u16 wIndex, u32 raw_port_status, |
Mathias Nyman | bd82873 | 2019-12-11 16:20:07 +0200 | [diff] [blame] | 1021 | unsigned long *flags) |
Sarah Sharp | 8b3d457 | 2013-08-20 08:12:12 -0700 | [diff] [blame] | 1022 | __releases(&xhci->lock) |
| 1023 | __acquires(&xhci->lock) |
Sarah Sharp | eae5b17 | 2013-04-02 08:42:20 -0700 | [diff] [blame] | 1024 | { |
Sarah Sharp | eae5b17 | 2013-04-02 08:42:20 -0700 | [diff] [blame] | 1025 | u32 status = 0; |
Mathias Nyman | e740b01 | 2018-05-21 16:39:55 +0300 | [diff] [blame] | 1026 | struct xhci_hub *rhub; |
| 1027 | struct xhci_port *port; |
| 1028 | |
| 1029 | rhub = xhci_get_rhub(hcd); |
| 1030 | port = rhub->ports[wIndex]; |
Sarah Sharp | eae5b17 | 2013-04-02 08:42:20 -0700 | [diff] [blame] | 1031 | |
Mathias Nyman | 3c2ddb4 | 2018-12-07 16:19:34 +0200 | [diff] [blame] | 1032 | /* common wPortChange bits */ |
Sarah Sharp | eae5b17 | 2013-04-02 08:42:20 -0700 | [diff] [blame] | 1033 | if (raw_port_status & PORT_CSC) |
| 1034 | status |= USB_PORT_STAT_C_CONNECTION << 16; |
| 1035 | if (raw_port_status & PORT_PEC) |
| 1036 | status |= USB_PORT_STAT_C_ENABLE << 16; |
| 1037 | if ((raw_port_status & PORT_OCC)) |
| 1038 | status |= USB_PORT_STAT_C_OVERCURRENT << 16; |
| 1039 | if ((raw_port_status & PORT_RC)) |
| 1040 | status |= USB_PORT_STAT_C_RESET << 16; |
Mathias Nyman | 70e9b53 | 2018-12-07 16:19:33 +0200 | [diff] [blame] | 1041 | |
Mathias Nyman | 3c2ddb4 | 2018-12-07 16:19:34 +0200 | [diff] [blame] | 1042 | /* common wPortStatus bits */ |
| 1043 | if (raw_port_status & PORT_CONNECT) { |
| 1044 | status |= USB_PORT_STAT_CONNECTION; |
| 1045 | status |= xhci_port_speed(raw_port_status); |
| 1046 | } |
| 1047 | if (raw_port_status & PORT_PE) |
| 1048 | status |= USB_PORT_STAT_ENABLE; |
| 1049 | if (raw_port_status & PORT_OC) |
| 1050 | status |= USB_PORT_STAT_OVERCURRENT; |
| 1051 | if (raw_port_status & PORT_RESET) |
| 1052 | status |= USB_PORT_STAT_RESET; |
| 1053 | |
| 1054 | /* USB2 and USB3 specific bits, including Port Link State */ |
Mathias Nyman | 5f78a54 | 2018-12-07 16:19:32 +0200 | [diff] [blame] | 1055 | if (hcd->speed >= HCD_USB3) |
| 1056 | xhci_get_usb3_port_status(port, &status, raw_port_status); |
Mathias Nyman | 70e9b53 | 2018-12-07 16:19:33 +0200 | [diff] [blame] | 1057 | else |
Mathias Nyman | e67ebf1 | 2018-12-07 16:19:36 +0200 | [diff] [blame] | 1058 | xhci_get_usb2_port_status(port, &status, raw_port_status, |
| 1059 | flags); |
Mathias Nyman | f69115f | 2015-12-11 14:38:06 +0200 | [diff] [blame] | 1060 | /* |
| 1061 | * Clear stale usb2 resume signalling variables in case port changed |
| 1062 | * state during resume signalling. For example on error |
| 1063 | */ |
| 1064 | if ((bus_state->resume_done[wIndex] || |
| 1065 | test_bit(wIndex, &bus_state->resuming_ports)) && |
| 1066 | (raw_port_status & PORT_PLS_MASK) != XDEV_U3 && |
| 1067 | (raw_port_status & PORT_PLS_MASK) != XDEV_RESUME) { |
| 1068 | bus_state->resume_done[wIndex] = 0; |
| 1069 | clear_bit(wIndex, &bus_state->resuming_ports); |
Anshuman Gupta | 330e2d6 | 2018-09-20 19:13:40 +0300 | [diff] [blame] | 1070 | usb_hcd_end_port_resume(&hcd->self, wIndex); |
Mathias Nyman | f69115f | 2015-12-11 14:38:06 +0200 | [diff] [blame] | 1071 | } |
| 1072 | |
Sarah Sharp | eae5b17 | 2013-04-02 08:42:20 -0700 | [diff] [blame] | 1073 | if (bus_state->port_c_suspend & (1 << wIndex)) |
Mathias Nyman | 5e6389f | 2015-11-24 13:09:46 +0200 | [diff] [blame] | 1074 | status |= USB_PORT_STAT_C_SUSPEND << 16; |
Sarah Sharp | eae5b17 | 2013-04-02 08:42:20 -0700 | [diff] [blame] | 1075 | |
| 1076 | return status; |
| 1077 | } |
| 1078 | |
Sarah Sharp | 0f2a793 | 2009-04-27 19:57:12 -0700 | [diff] [blame] | 1079 | int xhci_hub_control(struct usb_hcd *hcd, u16 typeReq, u16 wValue, |
| 1080 | u16 wIndex, char *buf, u16 wLength) |
| 1081 | { |
| 1082 | struct xhci_hcd *xhci = hcd_to_xhci(hcd); |
huajun li | a088592 | 2011-05-03 21:11:00 +0800 | [diff] [blame] | 1083 | int max_ports; |
Sarah Sharp | 0f2a793 | 2009-04-27 19:57:12 -0700 | [diff] [blame] | 1084 | unsigned long flags; |
Andiry Xu | c9682df | 2011-09-23 14:19:48 -0700 | [diff] [blame] | 1085 | u32 temp, status; |
Sarah Sharp | 0f2a793 | 2009-04-27 19:57:12 -0700 | [diff] [blame] | 1086 | int retval = 0; |
Andiry Xu | be88fe4 | 2010-10-14 07:22:57 -0700 | [diff] [blame] | 1087 | int slot_id; |
Sarah Sharp | 20b67cf | 2010-12-15 12:47:14 -0800 | [diff] [blame] | 1088 | struct xhci_bus_state *bus_state; |
Andiry Xu | 2c44178 | 2011-04-27 18:07:39 +0800 | [diff] [blame] | 1089 | u16 link_state = 0; |
Sarah Sharp | 4296c70a | 2012-01-06 10:34:31 -0800 | [diff] [blame] | 1090 | u16 wake_mask = 0; |
Sarah Sharp | 797b0ca | 2011-11-10 16:02:13 -0800 | [diff] [blame] | 1091 | u16 timeout = 0; |
Guoqing Zhang | 0f1d832 | 2017-04-07 17:56:54 +0300 | [diff] [blame] | 1092 | u16 test_mode = 0; |
Mathias Nyman | e740b01 | 2018-05-21 16:39:55 +0300 | [diff] [blame] | 1093 | struct xhci_hub *rhub; |
| 1094 | struct xhci_port **ports; |
Sarah Sharp | 0f2a793 | 2009-04-27 19:57:12 -0700 | [diff] [blame] | 1095 | |
Mathias Nyman | e740b01 | 2018-05-21 16:39:55 +0300 | [diff] [blame] | 1096 | rhub = xhci_get_rhub(hcd); |
| 1097 | ports = rhub->ports; |
Mathias Nyman | 925f349 | 2018-05-21 16:40:02 +0300 | [diff] [blame] | 1098 | max_ports = rhub->num_ports; |
Mathias Nyman | f6187f4 | 2018-12-07 16:19:30 +0200 | [diff] [blame] | 1099 | bus_state = &rhub->bus_state; |
Sarah Sharp | 0f2a793 | 2009-04-27 19:57:12 -0700 | [diff] [blame] | 1100 | |
| 1101 | spin_lock_irqsave(&xhci->lock, flags); |
| 1102 | switch (typeReq) { |
| 1103 | case GetHubStatus: |
| 1104 | /* No power source, over-current reported per port */ |
| 1105 | memset(buf, 0, 4); |
| 1106 | break; |
| 1107 | case GetHubDescriptor: |
Sarah Sharp | 4bbb0ac | 2010-11-29 16:14:37 -0800 | [diff] [blame] | 1108 | /* Check to make sure userspace is asking for the USB 3.0 hub |
| 1109 | * descriptor for the USB 3.0 roothub. If not, we stall the |
| 1110 | * endpoint, like external hubs do. |
| 1111 | */ |
Mathias Nyman | b50107b | 2015-10-01 18:40:38 +0300 | [diff] [blame] | 1112 | if (hcd->speed >= HCD_USB3 && |
Sarah Sharp | 4bbb0ac | 2010-11-29 16:14:37 -0800 | [diff] [blame] | 1113 | (wLength < USB_DT_SS_HUB_SIZE || |
| 1114 | wValue != (USB_DT_SS_HUB << 8))) { |
| 1115 | xhci_dbg(xhci, "Wrong hub descriptor type for " |
| 1116 | "USB 3.0 roothub.\n"); |
| 1117 | goto error; |
| 1118 | } |
Sarah Sharp | f6ff0ac | 2010-12-16 11:21:10 -0800 | [diff] [blame] | 1119 | xhci_hub_descriptor(hcd, xhci, |
| 1120 | (struct usb_hub_descriptor *) buf); |
Sarah Sharp | 0f2a793 | 2009-04-27 19:57:12 -0700 | [diff] [blame] | 1121 | break; |
Sarah Sharp | 48e8236 | 2011-10-06 11:54:23 -0700 | [diff] [blame] | 1122 | case DeviceRequest | USB_REQ_GET_DESCRIPTOR: |
| 1123 | if ((wValue & 0xff00) != (USB_DT_BOS << 8)) |
| 1124 | goto error; |
| 1125 | |
Mathias Nyman | 5693e0b | 2015-10-01 18:40:35 +0300 | [diff] [blame] | 1126 | if (hcd->speed < HCD_USB3) |
Sarah Sharp | 48e8236 | 2011-10-06 11:54:23 -0700 | [diff] [blame] | 1127 | goto error; |
| 1128 | |
Mathias Nyman | 5693e0b | 2015-10-01 18:40:35 +0300 | [diff] [blame] | 1129 | retval = xhci_create_usb3_bos_desc(xhci, buf, wLength); |
Sarah Sharp | 48e8236 | 2011-10-06 11:54:23 -0700 | [diff] [blame] | 1130 | spin_unlock_irqrestore(&xhci->lock, flags); |
Mathias Nyman | 5693e0b | 2015-10-01 18:40:35 +0300 | [diff] [blame] | 1131 | return retval; |
Sarah Sharp | 0f2a793 | 2009-04-27 19:57:12 -0700 | [diff] [blame] | 1132 | case GetPortStatus: |
huajun li | a088592 | 2011-05-03 21:11:00 +0800 | [diff] [blame] | 1133 | if (!wIndex || wIndex > max_ports) |
Sarah Sharp | 0f2a793 | 2009-04-27 19:57:12 -0700 | [diff] [blame] | 1134 | goto error; |
| 1135 | wIndex--; |
Mathias Nyman | e740b01 | 2018-05-21 16:39:55 +0300 | [diff] [blame] | 1136 | temp = readl(ports[wIndex]->addr); |
Mathias Nyman | d9f11ba | 2017-04-07 17:57:01 +0300 | [diff] [blame] | 1137 | if (temp == ~(u32)0) { |
| 1138 | xhci_hc_died(xhci); |
Sarah Sharp | f9de815 | 2010-10-29 14:37:23 -0700 | [diff] [blame] | 1139 | retval = -ENODEV; |
| 1140 | break; |
| 1141 | } |
Mathias Nyman | 28c06e5 | 2017-12-08 17:59:14 +0200 | [diff] [blame] | 1142 | trace_xhci_get_port_status(wIndex, temp); |
Mathias Nyman | eaefcf2 | 2018-05-21 16:40:00 +0300 | [diff] [blame] | 1143 | status = xhci_get_port_status(hcd, bus_state, wIndex, temp, |
Mathias Nyman | bd82873 | 2019-12-11 16:20:07 +0200 | [diff] [blame] | 1144 | &flags); |
Sarah Sharp | eae5b17 | 2013-04-02 08:42:20 -0700 | [diff] [blame] | 1145 | if (status == 0xffffffff) |
| 1146 | goto error; |
Sarah Sharp | 0f2a793 | 2009-04-27 19:57:12 -0700 | [diff] [blame] | 1147 | |
Mathias Nyman | d70d5a8 | 2019-04-26 16:23:30 +0300 | [diff] [blame] | 1148 | xhci_dbg(xhci, "Get port status %d-%d read: 0x%x, return 0x%x", |
| 1149 | hcd->self.busnum, wIndex + 1, temp, status); |
Sarah Sharp | eae5b17 | 2013-04-02 08:42:20 -0700 | [diff] [blame] | 1150 | |
Sarah Sharp | 0f2a793 | 2009-04-27 19:57:12 -0700 | [diff] [blame] | 1151 | put_unaligned(cpu_to_le32(status), (__le32 *) buf); |
Mathias Nyman | 395f540 | 2015-10-01 18:40:39 +0300 | [diff] [blame] | 1152 | /* if USB 3.1 extended port status return additional 4 bytes */ |
| 1153 | if (wValue == 0x02) { |
| 1154 | u32 port_li; |
| 1155 | |
| 1156 | if (hcd->speed < HCD_USB31 || wLength != 8) { |
| 1157 | xhci_err(xhci, "get ext port status invalid parameter\n"); |
| 1158 | retval = -EINVAL; |
| 1159 | break; |
| 1160 | } |
Mathias Nyman | e740b01 | 2018-05-21 16:39:55 +0300 | [diff] [blame] | 1161 | port_li = readl(ports[wIndex]->addr + PORTLI); |
Mathias Nyman | 395f540 | 2015-10-01 18:40:39 +0300 | [diff] [blame] | 1162 | status = xhci_get_ext_port_status(temp, port_li); |
Ruslan Bilovol | 6269e4c | 2019-07-07 15:17:19 +0300 | [diff] [blame] | 1163 | put_unaligned_le32(status, &buf[4]); |
Mathias Nyman | 395f540 | 2015-10-01 18:40:39 +0300 | [diff] [blame] | 1164 | } |
Sarah Sharp | 0f2a793 | 2009-04-27 19:57:12 -0700 | [diff] [blame] | 1165 | break; |
| 1166 | case SetPortFeature: |
Andiry Xu | 2c44178 | 2011-04-27 18:07:39 +0800 | [diff] [blame] | 1167 | if (wValue == USB_PORT_FEAT_LINK_STATE) |
| 1168 | link_state = (wIndex & 0xff00) >> 3; |
Sarah Sharp | 4296c70a | 2012-01-06 10:34:31 -0800 | [diff] [blame] | 1169 | if (wValue == USB_PORT_FEAT_REMOTE_WAKE_MASK) |
| 1170 | wake_mask = wIndex & 0xff00; |
Guoqing Zhang | 0f1d832 | 2017-04-07 17:56:54 +0300 | [diff] [blame] | 1171 | if (wValue == USB_PORT_FEAT_TEST) |
| 1172 | test_mode = (wIndex & 0xff00) >> 8; |
Sarah Sharp | 797b0ca | 2011-11-10 16:02:13 -0800 | [diff] [blame] | 1173 | /* The MSB of wIndex is the U1/U2 timeout */ |
| 1174 | timeout = (wIndex & 0xff00) >> 8; |
Sarah Sharp | 0f2a793 | 2009-04-27 19:57:12 -0700 | [diff] [blame] | 1175 | wIndex &= 0xff; |
huajun li | a088592 | 2011-05-03 21:11:00 +0800 | [diff] [blame] | 1176 | if (!wIndex || wIndex > max_ports) |
Sarah Sharp | 0f2a793 | 2009-04-27 19:57:12 -0700 | [diff] [blame] | 1177 | goto error; |
| 1178 | wIndex--; |
Mathias Nyman | e740b01 | 2018-05-21 16:39:55 +0300 | [diff] [blame] | 1179 | temp = readl(ports[wIndex]->addr); |
Mathias Nyman | d9f11ba | 2017-04-07 17:57:01 +0300 | [diff] [blame] | 1180 | if (temp == ~(u32)0) { |
| 1181 | xhci_hc_died(xhci); |
Sarah Sharp | f9de815 | 2010-10-29 14:37:23 -0700 | [diff] [blame] | 1182 | retval = -ENODEV; |
| 1183 | break; |
| 1184 | } |
Sarah Sharp | 0f2a793 | 2009-04-27 19:57:12 -0700 | [diff] [blame] | 1185 | temp = xhci_port_state_to_neutral(temp); |
Sarah Sharp | 4bbb0ac | 2010-11-29 16:14:37 -0800 | [diff] [blame] | 1186 | /* FIXME: What new port features do we need to support? */ |
Sarah Sharp | 0f2a793 | 2009-04-27 19:57:12 -0700 | [diff] [blame] | 1187 | switch (wValue) { |
Andiry Xu | be88fe4 | 2010-10-14 07:22:57 -0700 | [diff] [blame] | 1188 | case USB_PORT_FEAT_SUSPEND: |
Mathias Nyman | e740b01 | 2018-05-21 16:39:55 +0300 | [diff] [blame] | 1189 | temp = readl(ports[wIndex]->addr); |
Andiry Xu | 65580b43 | 2011-09-23 14:19:52 -0700 | [diff] [blame] | 1190 | if ((temp & PORT_PLS_MASK) != XDEV_U0) { |
| 1191 | /* Resume the port to U0 first */ |
Mathias Nyman | 6b7f40f | 2018-05-21 16:39:59 +0300 | [diff] [blame] | 1192 | xhci_set_link_state(xhci, ports[wIndex], |
Andiry Xu | 65580b43 | 2011-09-23 14:19:52 -0700 | [diff] [blame] | 1193 | XDEV_U0); |
| 1194 | spin_unlock_irqrestore(&xhci->lock, flags); |
| 1195 | msleep(10); |
| 1196 | spin_lock_irqsave(&xhci->lock, flags); |
| 1197 | } |
Andiry Xu | be88fe4 | 2010-10-14 07:22:57 -0700 | [diff] [blame] | 1198 | /* In spec software should not attempt to suspend |
| 1199 | * a port unless the port reports that it is in the |
| 1200 | * enabled (PED = ‘1’,PLS < ‘3’) state. |
| 1201 | */ |
Mathias Nyman | e740b01 | 2018-05-21 16:39:55 +0300 | [diff] [blame] | 1202 | temp = readl(ports[wIndex]->addr); |
Andiry Xu | be88fe4 | 2010-10-14 07:22:57 -0700 | [diff] [blame] | 1203 | if ((temp & PORT_PE) == 0 || (temp & PORT_RESET) |
| 1204 | || (temp & PORT_PLS_MASK) >= XDEV_U3) { |
Mathias Nyman | d70d5a8 | 2019-04-26 16:23:30 +0300 | [diff] [blame] | 1205 | xhci_warn(xhci, "USB core suspending port %d-%d not in U0/U1/U2\n", |
| 1206 | hcd->self.busnum, wIndex + 1); |
Andiry Xu | be88fe4 | 2010-10-14 07:22:57 -0700 | [diff] [blame] | 1207 | goto error; |
| 1208 | } |
| 1209 | |
Sarah Sharp | 5233630 | 2010-12-16 10:49:09 -0800 | [diff] [blame] | 1210 | slot_id = xhci_find_slot_id_by_port(hcd, xhci, |
| 1211 | wIndex + 1); |
Andiry Xu | be88fe4 | 2010-10-14 07:22:57 -0700 | [diff] [blame] | 1212 | if (!slot_id) { |
| 1213 | xhci_warn(xhci, "slot_id is zero\n"); |
| 1214 | goto error; |
| 1215 | } |
| 1216 | /* unlock to execute stop endpoint commands */ |
| 1217 | spin_unlock_irqrestore(&xhci->lock, flags); |
| 1218 | xhci_stop_device(xhci, slot_id, 1); |
| 1219 | spin_lock_irqsave(&xhci->lock, flags); |
| 1220 | |
Mathias Nyman | 6b7f40f | 2018-05-21 16:39:59 +0300 | [diff] [blame] | 1221 | xhci_set_link_state(xhci, ports[wIndex], XDEV_U3); |
Andiry Xu | be88fe4 | 2010-10-14 07:22:57 -0700 | [diff] [blame] | 1222 | |
| 1223 | spin_unlock_irqrestore(&xhci->lock, flags); |
| 1224 | msleep(10); /* wait device to enter */ |
| 1225 | spin_lock_irqsave(&xhci->lock, flags); |
| 1226 | |
Mathias Nyman | e740b01 | 2018-05-21 16:39:55 +0300 | [diff] [blame] | 1227 | temp = readl(ports[wIndex]->addr); |
Sarah Sharp | 20b67cf | 2010-12-15 12:47:14 -0800 | [diff] [blame] | 1228 | bus_state->suspended_ports |= 1 << wIndex; |
Andiry Xu | be88fe4 | 2010-10-14 07:22:57 -0700 | [diff] [blame] | 1229 | break; |
Andiry Xu | 2c44178 | 2011-04-27 18:07:39 +0800 | [diff] [blame] | 1230 | case USB_PORT_FEAT_LINK_STATE: |
Mathias Nyman | e740b01 | 2018-05-21 16:39:55 +0300 | [diff] [blame] | 1231 | temp = readl(ports[wIndex]->addr); |
Sarah Sharp | 41e7e05 | 2012-11-14 16:42:32 -0800 | [diff] [blame] | 1232 | /* Disable port */ |
| 1233 | if (link_state == USB_SS_PORT_LS_SS_DISABLED) { |
| 1234 | xhci_dbg(xhci, "Disable port %d\n", wIndex); |
| 1235 | temp = xhci_port_state_to_neutral(temp); |
| 1236 | /* |
| 1237 | * Clear all change bits, so that we get a new |
| 1238 | * connection event. |
| 1239 | */ |
| 1240 | temp |= PORT_CSC | PORT_PEC | PORT_WRC | |
| 1241 | PORT_OCC | PORT_RC | PORT_PLC | |
| 1242 | PORT_CEC; |
Mathias Nyman | e740b01 | 2018-05-21 16:39:55 +0300 | [diff] [blame] | 1243 | writel(temp | PORT_PE, ports[wIndex]->addr); |
| 1244 | temp = readl(ports[wIndex]->addr); |
Sarah Sharp | 41e7e05 | 2012-11-14 16:42:32 -0800 | [diff] [blame] | 1245 | break; |
| 1246 | } |
| 1247 | |
| 1248 | /* Put link in RxDetect (enable port) */ |
| 1249 | if (link_state == USB_SS_PORT_LS_RX_DETECT) { |
| 1250 | xhci_dbg(xhci, "Enable port %d\n", wIndex); |
Mathias Nyman | 6b7f40f | 2018-05-21 16:39:59 +0300 | [diff] [blame] | 1251 | xhci_set_link_state(xhci, ports[wIndex], |
| 1252 | link_state); |
Mathias Nyman | e740b01 | 2018-05-21 16:39:55 +0300 | [diff] [blame] | 1253 | temp = readl(ports[wIndex]->addr); |
Sarah Sharp | 41e7e05 | 2012-11-14 16:42:32 -0800 | [diff] [blame] | 1254 | break; |
| 1255 | } |
| 1256 | |
Jack Pham | 4b562bd | 2017-08-23 00:35:29 -0700 | [diff] [blame] | 1257 | /* |
| 1258 | * For xHCI 1.1 according to section 4.19.1.2.4.1 a |
| 1259 | * root hub port's transition to compliance mode upon |
| 1260 | * detecting LFPS timeout may be controlled by an |
| 1261 | * Compliance Transition Enabled (CTE) flag (not |
| 1262 | * software visible). This flag is set by writing 0xA |
| 1263 | * to PORTSC PLS field which will allow transition to |
| 1264 | * compliance mode the next time LFPS timeout is |
| 1265 | * encountered. A warm reset will clear it. |
| 1266 | * |
| 1267 | * The CTE flag is only supported if the HCCPARAMS2 CTC |
| 1268 | * flag is set, otherwise, the compliance substate is |
| 1269 | * automatically entered as on 1.0 and prior. |
| 1270 | */ |
| 1271 | if (link_state == USB_SS_PORT_LS_COMP_MOD) { |
| 1272 | if (!HCC2_CTC(xhci->hcc_params2)) { |
| 1273 | xhci_dbg(xhci, "CTC flag is 0, port already supports entering compliance mode\n"); |
| 1274 | break; |
| 1275 | } |
| 1276 | |
| 1277 | if ((temp & PORT_CONNECT)) { |
| 1278 | xhci_warn(xhci, "Can't set compliance mode when port is connected\n"); |
| 1279 | goto error; |
| 1280 | } |
| 1281 | |
| 1282 | xhci_dbg(xhci, "Enable compliance mode transition for port %d\n", |
| 1283 | wIndex); |
Mathias Nyman | 6b7f40f | 2018-05-21 16:39:59 +0300 | [diff] [blame] | 1284 | xhci_set_link_state(xhci, ports[wIndex], |
Jack Pham | 4b562bd | 2017-08-23 00:35:29 -0700 | [diff] [blame] | 1285 | link_state); |
Mathias Nyman | 6b7f40f | 2018-05-21 16:39:59 +0300 | [diff] [blame] | 1286 | |
Mathias Nyman | e740b01 | 2018-05-21 16:39:55 +0300 | [diff] [blame] | 1287 | temp = readl(ports[wIndex]->addr); |
Jack Pham | 4b562bd | 2017-08-23 00:35:29 -0700 | [diff] [blame] | 1288 | break; |
| 1289 | } |
Mathias Nyman | 1208d8a | 2018-02-12 14:24:47 +0200 | [diff] [blame] | 1290 | /* Port must be enabled */ |
| 1291 | if (!(temp & PORT_PE)) { |
| 1292 | retval = -ENODEV; |
| 1293 | break; |
| 1294 | } |
| 1295 | /* Can't set port link state above '3' (U3) */ |
| 1296 | if (link_state > USB_SS_PORT_LS_U3) { |
| 1297 | xhci_warn(xhci, "Cannot set port %d link state %d\n", |
| 1298 | wIndex, link_state); |
Andiry Xu | 2c44178 | 2011-04-27 18:07:39 +0800 | [diff] [blame] | 1299 | goto error; |
| 1300 | } |
Andiry Xu | 2c44178 | 2011-04-27 18:07:39 +0800 | [diff] [blame] | 1301 | if (link_state == USB_SS_PORT_LS_U3) { |
| 1302 | slot_id = xhci_find_slot_id_by_port(hcd, xhci, |
| 1303 | wIndex + 1); |
| 1304 | if (slot_id) { |
| 1305 | /* unlock to execute stop endpoint |
| 1306 | * commands */ |
| 1307 | spin_unlock_irqrestore(&xhci->lock, |
| 1308 | flags); |
| 1309 | xhci_stop_device(xhci, slot_id, 1); |
| 1310 | spin_lock_irqsave(&xhci->lock, flags); |
| 1311 | } |
| 1312 | } |
| 1313 | |
Mathias Nyman | 6b7f40f | 2018-05-21 16:39:59 +0300 | [diff] [blame] | 1314 | xhci_set_link_state(xhci, ports[wIndex], link_state); |
Andiry Xu | 2c44178 | 2011-04-27 18:07:39 +0800 | [diff] [blame] | 1315 | |
| 1316 | spin_unlock_irqrestore(&xhci->lock, flags); |
| 1317 | msleep(20); /* wait device to enter */ |
| 1318 | spin_lock_irqsave(&xhci->lock, flags); |
| 1319 | |
Mathias Nyman | e740b01 | 2018-05-21 16:39:55 +0300 | [diff] [blame] | 1320 | temp = readl(ports[wIndex]->addr); |
Andiry Xu | 2c44178 | 2011-04-27 18:07:39 +0800 | [diff] [blame] | 1321 | if (link_state == USB_SS_PORT_LS_U3) |
| 1322 | bus_state->suspended_ports |= 1 << wIndex; |
| 1323 | break; |
Sarah Sharp | 0f2a793 | 2009-04-27 19:57:12 -0700 | [diff] [blame] | 1324 | case USB_PORT_FEAT_POWER: |
| 1325 | /* |
| 1326 | * Turn on ports, even if there isn't per-port switching. |
| 1327 | * HC will report connect events even before this is set. |
Petr Mladek | 37ebb54 | 2014-09-19 17:32:23 +0200 | [diff] [blame] | 1328 | * However, hub_wq will ignore the roothub events until |
Sarah Sharp | 0f2a793 | 2009-04-27 19:57:12 -0700 | [diff] [blame] | 1329 | * the roothub is registered. |
| 1330 | */ |
Mathias Nyman | ec1dafe | 2017-04-13 14:01:04 +0300 | [diff] [blame] | 1331 | xhci_set_port_power(xhci, hcd, wIndex, true, &flags); |
Sarah Sharp | 0f2a793 | 2009-04-27 19:57:12 -0700 | [diff] [blame] | 1332 | break; |
| 1333 | case USB_PORT_FEAT_RESET: |
| 1334 | temp = (temp | PORT_RESET); |
Mathias Nyman | e740b01 | 2018-05-21 16:39:55 +0300 | [diff] [blame] | 1335 | writel(temp, ports[wIndex]->addr); |
Sarah Sharp | 0f2a793 | 2009-04-27 19:57:12 -0700 | [diff] [blame] | 1336 | |
Mathias Nyman | e740b01 | 2018-05-21 16:39:55 +0300 | [diff] [blame] | 1337 | temp = readl(ports[wIndex]->addr); |
Sarah Sharp | 0f2a793 | 2009-04-27 19:57:12 -0700 | [diff] [blame] | 1338 | xhci_dbg(xhci, "set port reset, actual port %d status = 0x%x\n", wIndex, temp); |
| 1339 | break; |
Sarah Sharp | 4296c70a | 2012-01-06 10:34:31 -0800 | [diff] [blame] | 1340 | case USB_PORT_FEAT_REMOTE_WAKE_MASK: |
Mathias Nyman | fdcf74ff | 2018-05-21 16:39:56 +0300 | [diff] [blame] | 1341 | xhci_set_remote_wake_mask(xhci, ports[wIndex], |
| 1342 | wake_mask); |
Mathias Nyman | e740b01 | 2018-05-21 16:39:55 +0300 | [diff] [blame] | 1343 | temp = readl(ports[wIndex]->addr); |
Sarah Sharp | 4296c70a | 2012-01-06 10:34:31 -0800 | [diff] [blame] | 1344 | xhci_dbg(xhci, "set port remote wake mask, " |
| 1345 | "actual port %d status = 0x%x\n", |
| 1346 | wIndex, temp); |
| 1347 | break; |
Andiry Xu | a11496e | 2011-04-27 18:07:29 +0800 | [diff] [blame] | 1348 | case USB_PORT_FEAT_BH_PORT_RESET: |
| 1349 | temp |= PORT_WR; |
Mathias Nyman | e740b01 | 2018-05-21 16:39:55 +0300 | [diff] [blame] | 1350 | writel(temp, ports[wIndex]->addr); |
| 1351 | temp = readl(ports[wIndex]->addr); |
Andiry Xu | a11496e | 2011-04-27 18:07:29 +0800 | [diff] [blame] | 1352 | break; |
Sarah Sharp | 797b0ca | 2011-11-10 16:02:13 -0800 | [diff] [blame] | 1353 | case USB_PORT_FEAT_U1_TIMEOUT: |
Mathias Nyman | b50107b | 2015-10-01 18:40:38 +0300 | [diff] [blame] | 1354 | if (hcd->speed < HCD_USB3) |
Sarah Sharp | 797b0ca | 2011-11-10 16:02:13 -0800 | [diff] [blame] | 1355 | goto error; |
Mathias Nyman | e740b01 | 2018-05-21 16:39:55 +0300 | [diff] [blame] | 1356 | temp = readl(ports[wIndex]->addr + PORTPMSC); |
Sarah Sharp | 797b0ca | 2011-11-10 16:02:13 -0800 | [diff] [blame] | 1357 | temp &= ~PORT_U1_TIMEOUT_MASK; |
| 1358 | temp |= PORT_U1_TIMEOUT(timeout); |
Mathias Nyman | e740b01 | 2018-05-21 16:39:55 +0300 | [diff] [blame] | 1359 | writel(temp, ports[wIndex]->addr + PORTPMSC); |
Sarah Sharp | 797b0ca | 2011-11-10 16:02:13 -0800 | [diff] [blame] | 1360 | break; |
| 1361 | case USB_PORT_FEAT_U2_TIMEOUT: |
Mathias Nyman | b50107b | 2015-10-01 18:40:38 +0300 | [diff] [blame] | 1362 | if (hcd->speed < HCD_USB3) |
Sarah Sharp | 797b0ca | 2011-11-10 16:02:13 -0800 | [diff] [blame] | 1363 | goto error; |
Mathias Nyman | e740b01 | 2018-05-21 16:39:55 +0300 | [diff] [blame] | 1364 | temp = readl(ports[wIndex]->addr + PORTPMSC); |
Sarah Sharp | 797b0ca | 2011-11-10 16:02:13 -0800 | [diff] [blame] | 1365 | temp &= ~PORT_U2_TIMEOUT_MASK; |
| 1366 | temp |= PORT_U2_TIMEOUT(timeout); |
Mathias Nyman | e740b01 | 2018-05-21 16:39:55 +0300 | [diff] [blame] | 1367 | writel(temp, ports[wIndex]->addr + PORTPMSC); |
Sarah Sharp | 797b0ca | 2011-11-10 16:02:13 -0800 | [diff] [blame] | 1368 | break; |
Guoqing Zhang | 0f1d832 | 2017-04-07 17:56:54 +0300 | [diff] [blame] | 1369 | case USB_PORT_FEAT_TEST: |
| 1370 | /* 4.19.6 Port Test Modes (USB2 Test Mode) */ |
| 1371 | if (hcd->speed != HCD_USB2) |
| 1372 | goto error; |
| 1373 | if (test_mode > TEST_FORCE_EN || test_mode < TEST_J) |
| 1374 | goto error; |
Mathias Nyman | ec1dafe | 2017-04-13 14:01:04 +0300 | [diff] [blame] | 1375 | retval = xhci_enter_test_mode(xhci, test_mode, wIndex, |
| 1376 | &flags); |
Guoqing Zhang | 0f1d832 | 2017-04-07 17:56:54 +0300 | [diff] [blame] | 1377 | break; |
Sarah Sharp | 0f2a793 | 2009-04-27 19:57:12 -0700 | [diff] [blame] | 1378 | default: |
| 1379 | goto error; |
| 1380 | } |
Sarah Sharp | 5308a91 | 2010-12-01 11:34:59 -0800 | [diff] [blame] | 1381 | /* unblock any posted writes */ |
Mathias Nyman | e740b01 | 2018-05-21 16:39:55 +0300 | [diff] [blame] | 1382 | temp = readl(ports[wIndex]->addr); |
Sarah Sharp | 0f2a793 | 2009-04-27 19:57:12 -0700 | [diff] [blame] | 1383 | break; |
| 1384 | case ClearPortFeature: |
huajun li | a088592 | 2011-05-03 21:11:00 +0800 | [diff] [blame] | 1385 | if (!wIndex || wIndex > max_ports) |
Sarah Sharp | 0f2a793 | 2009-04-27 19:57:12 -0700 | [diff] [blame] | 1386 | goto error; |
| 1387 | wIndex--; |
Mathias Nyman | e740b01 | 2018-05-21 16:39:55 +0300 | [diff] [blame] | 1388 | temp = readl(ports[wIndex]->addr); |
Mathias Nyman | d9f11ba | 2017-04-07 17:57:01 +0300 | [diff] [blame] | 1389 | if (temp == ~(u32)0) { |
| 1390 | xhci_hc_died(xhci); |
Sarah Sharp | f9de815 | 2010-10-29 14:37:23 -0700 | [diff] [blame] | 1391 | retval = -ENODEV; |
| 1392 | break; |
| 1393 | } |
Sarah Sharp | 4bbb0ac | 2010-11-29 16:14:37 -0800 | [diff] [blame] | 1394 | /* FIXME: What new port features do we need to support? */ |
Sarah Sharp | 0f2a793 | 2009-04-27 19:57:12 -0700 | [diff] [blame] | 1395 | temp = xhci_port_state_to_neutral(temp); |
| 1396 | switch (wValue) { |
Andiry Xu | be88fe4 | 2010-10-14 07:22:57 -0700 | [diff] [blame] | 1397 | case USB_PORT_FEAT_SUSPEND: |
Mathias Nyman | e740b01 | 2018-05-21 16:39:55 +0300 | [diff] [blame] | 1398 | temp = readl(ports[wIndex]->addr); |
Andiry Xu | be88fe4 | 2010-10-14 07:22:57 -0700 | [diff] [blame] | 1399 | xhci_dbg(xhci, "clear USB_PORT_FEAT_SUSPEND\n"); |
| 1400 | xhci_dbg(xhci, "PORTSC %04x\n", temp); |
| 1401 | if (temp & PORT_RESET) |
| 1402 | goto error; |
Andiry Xu | 5ac04bf | 2011-08-03 16:46:48 +0800 | [diff] [blame] | 1403 | if ((temp & PORT_PLS_MASK) == XDEV_U3) { |
Andiry Xu | be88fe4 | 2010-10-14 07:22:57 -0700 | [diff] [blame] | 1404 | if ((temp & PORT_PE) == 0) |
| 1405 | goto error; |
Andiry Xu | be88fe4 | 2010-10-14 07:22:57 -0700 | [diff] [blame] | 1406 | |
Mathias Nyman | f69115f | 2015-12-11 14:38:06 +0200 | [diff] [blame] | 1407 | set_bit(wIndex, &bus_state->resuming_ports); |
Anshuman Gupta | 330e2d6 | 2018-09-20 19:13:40 +0300 | [diff] [blame] | 1408 | usb_hcd_start_port_resume(&hcd->self, wIndex); |
Mathias Nyman | 6b7f40f | 2018-05-21 16:39:59 +0300 | [diff] [blame] | 1409 | xhci_set_link_state(xhci, ports[wIndex], |
| 1410 | XDEV_RESUME); |
Andiry Xu | c9682df | 2011-09-23 14:19:48 -0700 | [diff] [blame] | 1411 | spin_unlock_irqrestore(&xhci->lock, flags); |
Mathias Nyman | 7d3b016 | 2016-10-20 18:09:20 +0300 | [diff] [blame] | 1412 | msleep(USB_RESUME_TIMEOUT); |
Andiry Xu | a711423 | 2011-04-27 18:07:50 +0800 | [diff] [blame] | 1413 | spin_lock_irqsave(&xhci->lock, flags); |
Mathias Nyman | 6b7f40f | 2018-05-21 16:39:59 +0300 | [diff] [blame] | 1414 | xhci_set_link_state(xhci, ports[wIndex], |
Andiry Xu | c9682df | 2011-09-23 14:19:48 -0700 | [diff] [blame] | 1415 | XDEV_U0); |
Mathias Nyman | f69115f | 2015-12-11 14:38:06 +0200 | [diff] [blame] | 1416 | clear_bit(wIndex, &bus_state->resuming_ports); |
Anshuman Gupta | 330e2d6 | 2018-09-20 19:13:40 +0300 | [diff] [blame] | 1417 | usb_hcd_end_port_resume(&hcd->self, wIndex); |
Andiry Xu | be88fe4 | 2010-10-14 07:22:57 -0700 | [diff] [blame] | 1418 | } |
Andiry Xu | a711423 | 2011-04-27 18:07:50 +0800 | [diff] [blame] | 1419 | bus_state->port_c_suspend |= 1 << wIndex; |
Andiry Xu | be88fe4 | 2010-10-14 07:22:57 -0700 | [diff] [blame] | 1420 | |
Sarah Sharp | 5233630 | 2010-12-16 10:49:09 -0800 | [diff] [blame] | 1421 | slot_id = xhci_find_slot_id_by_port(hcd, xhci, |
| 1422 | wIndex + 1); |
Andiry Xu | be88fe4 | 2010-10-14 07:22:57 -0700 | [diff] [blame] | 1423 | if (!slot_id) { |
| 1424 | xhci_dbg(xhci, "slot_id is zero\n"); |
| 1425 | goto error; |
| 1426 | } |
| 1427 | xhci_ring_device(xhci, slot_id); |
| 1428 | break; |
| 1429 | case USB_PORT_FEAT_C_SUSPEND: |
Sarah Sharp | 20b67cf | 2010-12-15 12:47:14 -0800 | [diff] [blame] | 1430 | bus_state->port_c_suspend &= ~(1 << wIndex); |
Gustavo A. R. Silva | ff504f5 | 2017-10-25 13:49:38 -0500 | [diff] [blame] | 1431 | /* fall through */ |
Sarah Sharp | 0f2a793 | 2009-04-27 19:57:12 -0700 | [diff] [blame] | 1432 | case USB_PORT_FEAT_C_RESET: |
Andiry Xu | a11496e | 2011-04-27 18:07:29 +0800 | [diff] [blame] | 1433 | case USB_PORT_FEAT_C_BH_PORT_RESET: |
Sarah Sharp | 0f2a793 | 2009-04-27 19:57:12 -0700 | [diff] [blame] | 1434 | case USB_PORT_FEAT_C_CONNECTION: |
Sarah Sharp | 0f2a793 | 2009-04-27 19:57:12 -0700 | [diff] [blame] | 1435 | case USB_PORT_FEAT_C_OVER_CURRENT: |
Sarah Sharp | 6219c047 | 2009-12-09 15:59:11 -0800 | [diff] [blame] | 1436 | case USB_PORT_FEAT_C_ENABLE: |
Andiry Xu | 85387c0 | 2011-04-27 18:07:35 +0800 | [diff] [blame] | 1437 | case USB_PORT_FEAT_C_PORT_LINK_STATE: |
Lu Baolu | 9425183 | 2015-03-23 18:27:41 +0200 | [diff] [blame] | 1438 | case USB_PORT_FEAT_C_PORT_CONFIG_ERROR: |
Sarah Sharp | 34fb562 | 2009-12-09 15:59:08 -0800 | [diff] [blame] | 1439 | xhci_clear_port_change_bit(xhci, wValue, wIndex, |
Mathias Nyman | e740b01 | 2018-05-21 16:39:55 +0300 | [diff] [blame] | 1440 | ports[wIndex]->addr, temp); |
Sarah Sharp | 0f2a793 | 2009-04-27 19:57:12 -0700 | [diff] [blame] | 1441 | break; |
Sarah Sharp | 6219c047 | 2009-12-09 15:59:11 -0800 | [diff] [blame] | 1442 | case USB_PORT_FEAT_ENABLE: |
Sarah Sharp | f6ff0ac | 2010-12-16 11:21:10 -0800 | [diff] [blame] | 1443 | xhci_disable_port(hcd, xhci, wIndex, |
Mathias Nyman | e740b01 | 2018-05-21 16:39:55 +0300 | [diff] [blame] | 1444 | ports[wIndex]->addr, temp); |
Sarah Sharp | 6219c047 | 2009-12-09 15:59:11 -0800 | [diff] [blame] | 1445 | break; |
Lan Tianyu | 693d8eb | 2012-09-05 13:44:35 +0800 | [diff] [blame] | 1446 | case USB_PORT_FEAT_POWER: |
Mathias Nyman | ec1dafe | 2017-04-13 14:01:04 +0300 | [diff] [blame] | 1447 | xhci_set_port_power(xhci, hcd, wIndex, false, &flags); |
Lan Tianyu | 693d8eb | 2012-09-05 13:44:35 +0800 | [diff] [blame] | 1448 | break; |
Guoqing Zhang | 0f1d832 | 2017-04-07 17:56:54 +0300 | [diff] [blame] | 1449 | case USB_PORT_FEAT_TEST: |
| 1450 | retval = xhci_exit_test_mode(xhci); |
| 1451 | break; |
Sarah Sharp | 0f2a793 | 2009-04-27 19:57:12 -0700 | [diff] [blame] | 1452 | default: |
| 1453 | goto error; |
| 1454 | } |
Sarah Sharp | 0f2a793 | 2009-04-27 19:57:12 -0700 | [diff] [blame] | 1455 | break; |
| 1456 | default: |
| 1457 | error: |
| 1458 | /* "stall" on error */ |
| 1459 | retval = -EPIPE; |
| 1460 | } |
| 1461 | spin_unlock_irqrestore(&xhci->lock, flags); |
| 1462 | return retval; |
| 1463 | } |
| 1464 | |
| 1465 | /* |
| 1466 | * Returns 0 if the status hasn't changed, or the number of bytes in buf. |
| 1467 | * Ports are 0-indexed from the HCD point of view, |
| 1468 | * and 1-indexed from the USB core pointer of view. |
Sarah Sharp | 0f2a793 | 2009-04-27 19:57:12 -0700 | [diff] [blame] | 1469 | * |
| 1470 | * Note that the status change bits will be cleared as soon as a port status |
| 1471 | * change event is generated, so we use the saved status from that event. |
| 1472 | */ |
| 1473 | int xhci_hub_status_data(struct usb_hcd *hcd, char *buf) |
| 1474 | { |
| 1475 | unsigned long flags; |
| 1476 | u32 temp, status; |
Andiry Xu | 5619253 | 2010-10-14 07:23:00 -0700 | [diff] [blame] | 1477 | u32 mask; |
Sarah Sharp | 0f2a793 | 2009-04-27 19:57:12 -0700 | [diff] [blame] | 1478 | int i, retval; |
| 1479 | struct xhci_hcd *xhci = hcd_to_xhci(hcd); |
huajun li | a088592 | 2011-05-03 21:11:00 +0800 | [diff] [blame] | 1480 | int max_ports; |
Sarah Sharp | 20b67cf | 2010-12-15 12:47:14 -0800 | [diff] [blame] | 1481 | struct xhci_bus_state *bus_state; |
Sarah Sharp | c52804a | 2012-11-27 12:30:23 -0800 | [diff] [blame] | 1482 | bool reset_change = false; |
Mathias Nyman | e740b01 | 2018-05-21 16:39:55 +0300 | [diff] [blame] | 1483 | struct xhci_hub *rhub; |
| 1484 | struct xhci_port **ports; |
Sarah Sharp | 0f2a793 | 2009-04-27 19:57:12 -0700 | [diff] [blame] | 1485 | |
Mathias Nyman | e740b01 | 2018-05-21 16:39:55 +0300 | [diff] [blame] | 1486 | rhub = xhci_get_rhub(hcd); |
| 1487 | ports = rhub->ports; |
Mathias Nyman | 925f349 | 2018-05-21 16:40:02 +0300 | [diff] [blame] | 1488 | max_ports = rhub->num_ports; |
Mathias Nyman | f6187f4 | 2018-12-07 16:19:30 +0200 | [diff] [blame] | 1489 | bus_state = &rhub->bus_state; |
Sarah Sharp | 0f2a793 | 2009-04-27 19:57:12 -0700 | [diff] [blame] | 1490 | |
| 1491 | /* Initial status is no changes */ |
huajun li | a088592 | 2011-05-03 21:11:00 +0800 | [diff] [blame] | 1492 | retval = (max_ports + 8) / 8; |
William Gulland | 419a8e81 | 2010-05-12 10:20:34 -0700 | [diff] [blame] | 1493 | memset(buf, 0, retval); |
Andiry Xu | f370b99 | 2012-04-14 02:54:30 +0800 | [diff] [blame] | 1494 | |
| 1495 | /* |
| 1496 | * Inform the usbcore about resume-in-progress by returning |
| 1497 | * a non-zero value even if there are no status changes. |
| 1498 | */ |
| 1499 | status = bus_state->resuming_ports; |
Sarah Sharp | 0f2a793 | 2009-04-27 19:57:12 -0700 | [diff] [blame] | 1500 | |
Lu Baolu | 9425183 | 2015-03-23 18:27:41 +0200 | [diff] [blame] | 1501 | mask = PORT_CSC | PORT_PEC | PORT_OCC | PORT_PLC | PORT_WRC | PORT_CEC; |
Andiry Xu | 5619253 | 2010-10-14 07:23:00 -0700 | [diff] [blame] | 1502 | |
Sarah Sharp | 0f2a793 | 2009-04-27 19:57:12 -0700 | [diff] [blame] | 1503 | spin_lock_irqsave(&xhci->lock, flags); |
| 1504 | /* For each port, did anything change? If so, set that bit in buf. */ |
huajun li | a088592 | 2011-05-03 21:11:00 +0800 | [diff] [blame] | 1505 | for (i = 0; i < max_ports; i++) { |
Mathias Nyman | e740b01 | 2018-05-21 16:39:55 +0300 | [diff] [blame] | 1506 | temp = readl(ports[i]->addr); |
Mathias Nyman | d9f11ba | 2017-04-07 17:57:01 +0300 | [diff] [blame] | 1507 | if (temp == ~(u32)0) { |
| 1508 | xhci_hc_died(xhci); |
Sarah Sharp | f9de815 | 2010-10-29 14:37:23 -0700 | [diff] [blame] | 1509 | retval = -ENODEV; |
| 1510 | break; |
| 1511 | } |
Mathias Nyman | 3f8499a | 2017-12-08 17:59:15 +0200 | [diff] [blame] | 1512 | trace_xhci_hub_status_data(i, temp); |
| 1513 | |
Andiry Xu | 5619253 | 2010-10-14 07:23:00 -0700 | [diff] [blame] | 1514 | if ((temp & mask) != 0 || |
Sarah Sharp | 20b67cf | 2010-12-15 12:47:14 -0800 | [diff] [blame] | 1515 | (bus_state->port_c_suspend & 1 << i) || |
| 1516 | (bus_state->resume_done[i] && time_after_eq( |
| 1517 | jiffies, bus_state->resume_done[i]))) { |
William Gulland | 419a8e81 | 2010-05-12 10:20:34 -0700 | [diff] [blame] | 1518 | buf[(i + 1) / 8] |= 1 << (i + 1) % 8; |
Sarah Sharp | 0f2a793 | 2009-04-27 19:57:12 -0700 | [diff] [blame] | 1519 | status = 1; |
| 1520 | } |
Sarah Sharp | c52804a | 2012-11-27 12:30:23 -0800 | [diff] [blame] | 1521 | if ((temp & PORT_RC)) |
| 1522 | reset_change = true; |
| 1523 | } |
| 1524 | if (!status && !reset_change) { |
| 1525 | xhci_dbg(xhci, "%s: stopping port polling.\n", __func__); |
| 1526 | clear_bit(HCD_FLAG_POLL_RH, &hcd->flags); |
Sarah Sharp | 0f2a793 | 2009-04-27 19:57:12 -0700 | [diff] [blame] | 1527 | } |
| 1528 | spin_unlock_irqrestore(&xhci->lock, flags); |
| 1529 | return status ? retval : 0; |
| 1530 | } |
Andiry Xu | 9777e3c | 2010-10-14 07:23:03 -0700 | [diff] [blame] | 1531 | |
| 1532 | #ifdef CONFIG_PM |
| 1533 | |
| 1534 | int xhci_bus_suspend(struct usb_hcd *hcd) |
| 1535 | { |
| 1536 | struct xhci_hcd *xhci = hcd_to_xhci(hcd); |
Sarah Sharp | 518e848 | 2010-12-15 11:56:29 -0800 | [diff] [blame] | 1537 | int max_ports, port_index; |
Sarah Sharp | 20b67cf | 2010-12-15 12:47:14 -0800 | [diff] [blame] | 1538 | struct xhci_bus_state *bus_state; |
Andiry Xu | 9777e3c | 2010-10-14 07:23:03 -0700 | [diff] [blame] | 1539 | unsigned long flags; |
Mathias Nyman | e740b01 | 2018-05-21 16:39:55 +0300 | [diff] [blame] | 1540 | struct xhci_hub *rhub; |
| 1541 | struct xhci_port **ports; |
Mathias Nyman | 2f31a67 | 2018-11-15 11:38:41 +0200 | [diff] [blame] | 1542 | u32 portsc_buf[USB_MAXCHILDREN]; |
| 1543 | bool wake_enabled; |
Andiry Xu | 9777e3c | 2010-10-14 07:23:03 -0700 | [diff] [blame] | 1544 | |
Mathias Nyman | e740b01 | 2018-05-21 16:39:55 +0300 | [diff] [blame] | 1545 | rhub = xhci_get_rhub(hcd); |
| 1546 | ports = rhub->ports; |
Mathias Nyman | 925f349 | 2018-05-21 16:40:02 +0300 | [diff] [blame] | 1547 | max_ports = rhub->num_ports; |
Mathias Nyman | f6187f4 | 2018-12-07 16:19:30 +0200 | [diff] [blame] | 1548 | bus_state = &rhub->bus_state; |
Mathias Nyman | 2f31a67 | 2018-11-15 11:38:41 +0200 | [diff] [blame] | 1549 | wake_enabled = hcd->self.root_hub->do_remote_wakeup; |
Andiry Xu | 9777e3c | 2010-10-14 07:23:03 -0700 | [diff] [blame] | 1550 | |
| 1551 | spin_lock_irqsave(&xhci->lock, flags); |
| 1552 | |
Mathias Nyman | 2f31a67 | 2018-11-15 11:38:41 +0200 | [diff] [blame] | 1553 | if (wake_enabled) { |
Zhuang Jin Can | fac4271 | 2015-07-21 17:20:30 +0300 | [diff] [blame] | 1554 | if (bus_state->resuming_ports || /* USB2 */ |
| 1555 | bus_state->port_remote_wakeup) { /* USB3 */ |
Andiry Xu | f370b99 | 2012-04-14 02:54:30 +0800 | [diff] [blame] | 1556 | spin_unlock_irqrestore(&xhci->lock, flags); |
Zhuang Jin Can | fac4271 | 2015-07-21 17:20:30 +0300 | [diff] [blame] | 1557 | xhci_dbg(xhci, "suspend failed because a port is resuming\n"); |
Andiry Xu | f370b99 | 2012-04-14 02:54:30 +0800 | [diff] [blame] | 1558 | return -EBUSY; |
Andiry Xu | 9777e3c | 2010-10-14 07:23:03 -0700 | [diff] [blame] | 1559 | } |
| 1560 | } |
Mathias Nyman | 2f31a67 | 2018-11-15 11:38:41 +0200 | [diff] [blame] | 1561 | /* |
| 1562 | * Prepare ports for suspend, but don't write anything before all ports |
| 1563 | * are checked and we know bus suspend can proceed |
| 1564 | */ |
Sarah Sharp | 20b67cf | 2010-12-15 12:47:14 -0800 | [diff] [blame] | 1565 | bus_state->bus_suspended = 0; |
Mathias Nyman | 2f31a67 | 2018-11-15 11:38:41 +0200 | [diff] [blame] | 1566 | port_index = max_ports; |
Sarah Sharp | 518e848 | 2010-12-15 11:56:29 -0800 | [diff] [blame] | 1567 | while (port_index--) { |
Andiry Xu | 9777e3c | 2010-10-14 07:23:03 -0700 | [diff] [blame] | 1568 | u32 t1, t2; |
Mathias Nyman | d92f2c5 | 2019-03-22 17:50:17 +0200 | [diff] [blame] | 1569 | int retries = 10; |
| 1570 | retry: |
Mathias Nyman | e740b01 | 2018-05-21 16:39:55 +0300 | [diff] [blame] | 1571 | t1 = readl(ports[port_index]->addr); |
Andiry Xu | 9777e3c | 2010-10-14 07:23:03 -0700 | [diff] [blame] | 1572 | t2 = xhci_port_state_to_neutral(t1); |
Mathias Nyman | 2f31a67 | 2018-11-15 11:38:41 +0200 | [diff] [blame] | 1573 | portsc_buf[port_index] = 0; |
Andiry Xu | 9777e3c | 2010-10-14 07:23:03 -0700 | [diff] [blame] | 1574 | |
Mathias Nyman | d92f2c5 | 2019-03-22 17:50:17 +0200 | [diff] [blame] | 1575 | /* |
| 1576 | * Give a USB3 port in link training time to finish, but don't |
| 1577 | * prevent suspend as port might be stuck |
| 1578 | */ |
| 1579 | if ((hcd->speed >= HCD_USB3) && retries-- && |
Mathias Nyman | 45f750c | 2018-12-14 10:54:43 +0200 | [diff] [blame] | 1580 | (t1 & PORT_PLS_MASK) == XDEV_POLLING) { |
Mathias Nyman | 2f31a67 | 2018-11-15 11:38:41 +0200 | [diff] [blame] | 1581 | spin_unlock_irqrestore(&xhci->lock, flags); |
Mathias Nyman | d92f2c5 | 2019-03-22 17:50:17 +0200 | [diff] [blame] | 1582 | msleep(XHCI_PORT_POLLING_LFPS_TIME); |
| 1583 | spin_lock_irqsave(&xhci->lock, flags); |
| 1584 | xhci_dbg(xhci, "port %d polling in bus suspend, waiting\n", |
| 1585 | port_index); |
| 1586 | goto retry; |
Mathias Nyman | 2f31a67 | 2018-11-15 11:38:41 +0200 | [diff] [blame] | 1587 | } |
Mathias Nyman | 2f31a67 | 2018-11-15 11:38:41 +0200 | [diff] [blame] | 1588 | /* suspend ports in U0, or bail out for new connect changes */ |
| 1589 | if ((t1 & PORT_PE) && (t1 & PORT_PLS_MASK) == XDEV_U0) { |
| 1590 | if ((t1 & PORT_CSC) && wake_enabled) { |
| 1591 | bus_state->bus_suspended = 0; |
Andiry Xu | 9777e3c | 2010-10-14 07:23:03 -0700 | [diff] [blame] | 1592 | spin_unlock_irqrestore(&xhci->lock, flags); |
Mathias Nyman | 2f31a67 | 2018-11-15 11:38:41 +0200 | [diff] [blame] | 1593 | xhci_dbg(xhci, "Bus suspend bailout, port connect change\n"); |
| 1594 | return -EBUSY; |
Andiry Xu | 9777e3c | 2010-10-14 07:23:03 -0700 | [diff] [blame] | 1595 | } |
Mathias Nyman | 2f31a67 | 2018-11-15 11:38:41 +0200 | [diff] [blame] | 1596 | xhci_dbg(xhci, "port %d not suspended\n", port_index); |
Andiry Xu | 9777e3c | 2010-10-14 07:23:03 -0700 | [diff] [blame] | 1597 | t2 &= ~PORT_PLS_MASK; |
| 1598 | t2 |= PORT_LINK_STROBE | XDEV_U3; |
Sarah Sharp | 20b67cf | 2010-12-15 12:47:14 -0800 | [diff] [blame] | 1599 | set_bit(port_index, &bus_state->bus_suspended); |
Andiry Xu | 9777e3c | 2010-10-14 07:23:03 -0700 | [diff] [blame] | 1600 | } |
Sarah Sharp | 4296c70a | 2012-01-06 10:34:31 -0800 | [diff] [blame] | 1601 | /* USB core sets remote wake mask for USB 3.0 hubs, |
Rafael J. Wysocki | ceb6c9c | 2014-11-29 23:47:05 +0100 | [diff] [blame] | 1602 | * including the USB 3.0 roothub, but only if CONFIG_PM |
Sarah Sharp | 4296c70a | 2012-01-06 10:34:31 -0800 | [diff] [blame] | 1603 | * is enabled, so also enable remote wake here. |
| 1604 | */ |
Mathias Nyman | 2f31a67 | 2018-11-15 11:38:41 +0200 | [diff] [blame] | 1605 | if (wake_enabled) { |
Andiry Xu | 9777e3c | 2010-10-14 07:23:03 -0700 | [diff] [blame] | 1606 | if (t1 & PORT_CONNECT) { |
| 1607 | t2 |= PORT_WKOC_E | PORT_WKDISC_E; |
| 1608 | t2 &= ~PORT_WKCONN_E; |
| 1609 | } else { |
| 1610 | t2 |= PORT_WKOC_E | PORT_WKCONN_E; |
| 1611 | t2 &= ~PORT_WKDISC_E; |
| 1612 | } |
Joe Lee | bde0716 | 2018-02-12 14:24:46 +0200 | [diff] [blame] | 1613 | |
| 1614 | if ((xhci->quirks & XHCI_U2_DISABLE_WAKE) && |
| 1615 | (hcd->speed < HCD_USB3)) { |
| 1616 | if (usb_amd_pt_check_port(hcd->self.controller, |
| 1617 | port_index)) |
| 1618 | t2 &= ~PORT_WAKE_BITS; |
| 1619 | } |
Andiry Xu | 9777e3c | 2010-10-14 07:23:03 -0700 | [diff] [blame] | 1620 | } else |
| 1621 | t2 &= ~PORT_WAKE_BITS; |
| 1622 | |
| 1623 | t1 = xhci_port_state_to_neutral(t1); |
| 1624 | if (t1 != t2) |
Mathias Nyman | 2f31a67 | 2018-11-15 11:38:41 +0200 | [diff] [blame] | 1625 | portsc_buf[port_index] = t2; |
| 1626 | } |
| 1627 | |
| 1628 | /* write port settings, stopping and suspending ports if needed */ |
| 1629 | port_index = max_ports; |
| 1630 | while (port_index--) { |
| 1631 | if (!portsc_buf[port_index]) |
| 1632 | continue; |
| 1633 | if (test_bit(port_index, &bus_state->bus_suspended)) { |
| 1634 | int slot_id; |
| 1635 | |
| 1636 | slot_id = xhci_find_slot_id_by_port(hcd, xhci, |
| 1637 | port_index + 1); |
| 1638 | if (slot_id) { |
| 1639 | spin_unlock_irqrestore(&xhci->lock, flags); |
| 1640 | xhci_stop_device(xhci, slot_id, 1); |
| 1641 | spin_lock_irqsave(&xhci->lock, flags); |
| 1642 | } |
| 1643 | } |
| 1644 | writel(portsc_buf[port_index], ports[port_index]->addr); |
Andiry Xu | 9777e3c | 2010-10-14 07:23:03 -0700 | [diff] [blame] | 1645 | } |
| 1646 | hcd->state = HC_STATE_SUSPENDED; |
Sarah Sharp | 20b67cf | 2010-12-15 12:47:14 -0800 | [diff] [blame] | 1647 | bus_state->next_statechange = jiffies + msecs_to_jiffies(10); |
Andiry Xu | 9777e3c | 2010-10-14 07:23:03 -0700 | [diff] [blame] | 1648 | spin_unlock_irqrestore(&xhci->lock, flags); |
| 1649 | return 0; |
| 1650 | } |
| 1651 | |
Mathias Nyman | 346e9973 | 2016-10-20 18:09:19 +0300 | [diff] [blame] | 1652 | /* |
| 1653 | * Workaround for missing Cold Attach Status (CAS) if device re-plugged in S3. |
| 1654 | * warm reset a USB3 device stuck in polling or compliance mode after resume. |
| 1655 | * See Intel 100/c230 series PCH specification update Doc #332692-006 Errata #8 |
| 1656 | */ |
Mathias Nyman | fdcf74ff | 2018-05-21 16:39:56 +0300 | [diff] [blame] | 1657 | static bool xhci_port_missing_cas_quirk(struct xhci_port *port) |
Mathias Nyman | 346e9973 | 2016-10-20 18:09:19 +0300 | [diff] [blame] | 1658 | { |
| 1659 | u32 portsc; |
| 1660 | |
Mathias Nyman | fdcf74ff | 2018-05-21 16:39:56 +0300 | [diff] [blame] | 1661 | portsc = readl(port->addr); |
Mathias Nyman | 346e9973 | 2016-10-20 18:09:19 +0300 | [diff] [blame] | 1662 | |
| 1663 | /* if any of these are set we are not stuck */ |
| 1664 | if (portsc & (PORT_CONNECT | PORT_CAS)) |
| 1665 | return false; |
| 1666 | |
| 1667 | if (((portsc & PORT_PLS_MASK) != XDEV_POLLING) && |
| 1668 | ((portsc & PORT_PLS_MASK) != XDEV_COMP_MODE)) |
| 1669 | return false; |
| 1670 | |
| 1671 | /* clear wakeup/change bits, and do a warm port reset */ |
| 1672 | portsc &= ~(PORT_RWC_BITS | PORT_CEC | PORT_WAKE_BITS); |
| 1673 | portsc |= PORT_WR; |
Mathias Nyman | fdcf74ff | 2018-05-21 16:39:56 +0300 | [diff] [blame] | 1674 | writel(portsc, port->addr); |
Mathias Nyman | 346e9973 | 2016-10-20 18:09:19 +0300 | [diff] [blame] | 1675 | /* flush write */ |
Mathias Nyman | fdcf74ff | 2018-05-21 16:39:56 +0300 | [diff] [blame] | 1676 | readl(port->addr); |
Mathias Nyman | 346e9973 | 2016-10-20 18:09:19 +0300 | [diff] [blame] | 1677 | return true; |
| 1678 | } |
| 1679 | |
Andiry Xu | 9777e3c | 2010-10-14 07:23:03 -0700 | [diff] [blame] | 1680 | int xhci_bus_resume(struct usb_hcd *hcd) |
| 1681 | { |
| 1682 | struct xhci_hcd *xhci = hcd_to_xhci(hcd); |
Sarah Sharp | 20b67cf | 2010-12-15 12:47:14 -0800 | [diff] [blame] | 1683 | struct xhci_bus_state *bus_state; |
Andiry Xu | 9777e3c | 2010-10-14 07:23:03 -0700 | [diff] [blame] | 1684 | unsigned long flags; |
Mathias Nyman | a85c0f8 | 2017-08-16 14:23:26 +0300 | [diff] [blame] | 1685 | int max_ports, port_index; |
Mathias Nyman | 41485a9 | 2015-05-29 17:01:51 +0300 | [diff] [blame] | 1686 | int slot_id; |
| 1687 | int sret; |
Mathias Nyman | a85c0f8 | 2017-08-16 14:23:26 +0300 | [diff] [blame] | 1688 | u32 next_state; |
| 1689 | u32 temp, portsc; |
Mathias Nyman | e740b01 | 2018-05-21 16:39:55 +0300 | [diff] [blame] | 1690 | struct xhci_hub *rhub; |
| 1691 | struct xhci_port **ports; |
Andiry Xu | 9777e3c | 2010-10-14 07:23:03 -0700 | [diff] [blame] | 1692 | |
Mathias Nyman | e740b01 | 2018-05-21 16:39:55 +0300 | [diff] [blame] | 1693 | rhub = xhci_get_rhub(hcd); |
| 1694 | ports = rhub->ports; |
Mathias Nyman | 925f349 | 2018-05-21 16:40:02 +0300 | [diff] [blame] | 1695 | max_ports = rhub->num_ports; |
Mathias Nyman | f6187f4 | 2018-12-07 16:19:30 +0200 | [diff] [blame] | 1696 | bus_state = &rhub->bus_state; |
Andiry Xu | 9777e3c | 2010-10-14 07:23:03 -0700 | [diff] [blame] | 1697 | |
Sarah Sharp | 20b67cf | 2010-12-15 12:47:14 -0800 | [diff] [blame] | 1698 | if (time_before(jiffies, bus_state->next_statechange)) |
Andiry Xu | 9777e3c | 2010-10-14 07:23:03 -0700 | [diff] [blame] | 1699 | msleep(5); |
| 1700 | |
| 1701 | spin_lock_irqsave(&xhci->lock, flags); |
| 1702 | if (!HCD_HW_ACCESSIBLE(hcd)) { |
| 1703 | spin_unlock_irqrestore(&xhci->lock, flags); |
| 1704 | return -ESHUTDOWN; |
| 1705 | } |
| 1706 | |
| 1707 | /* delay the irqs */ |
Xenia Ragiadakou | b0ba972 | 2013-11-15 05:34:06 +0200 | [diff] [blame] | 1708 | temp = readl(&xhci->op_regs->command); |
Andiry Xu | 9777e3c | 2010-10-14 07:23:03 -0700 | [diff] [blame] | 1709 | temp &= ~CMD_EIE; |
Xenia Ragiadakou | 204b779 | 2013-11-15 05:34:07 +0200 | [diff] [blame] | 1710 | writel(temp, &xhci->op_regs->command); |
Andiry Xu | 9777e3c | 2010-10-14 07:23:03 -0700 | [diff] [blame] | 1711 | |
Mathias Nyman | a85c0f8 | 2017-08-16 14:23:26 +0300 | [diff] [blame] | 1712 | /* bus specific resume for ports we suspended at bus_suspend */ |
| 1713 | if (hcd->speed >= HCD_USB3) |
| 1714 | next_state = XDEV_U0; |
| 1715 | else |
| 1716 | next_state = XDEV_RESUME; |
| 1717 | |
Sarah Sharp | 518e848 | 2010-12-15 11:56:29 -0800 | [diff] [blame] | 1718 | port_index = max_ports; |
| 1719 | while (port_index--) { |
Mathias Nyman | e740b01 | 2018-05-21 16:39:55 +0300 | [diff] [blame] | 1720 | portsc = readl(ports[port_index]->addr); |
Mathias Nyman | 346e9973 | 2016-10-20 18:09:19 +0300 | [diff] [blame] | 1721 | |
| 1722 | /* warm reset CAS limited ports stuck in polling/compliance */ |
| 1723 | if ((xhci->quirks & XHCI_MISSING_CAS) && |
| 1724 | (hcd->speed >= HCD_USB3) && |
Mathias Nyman | fdcf74ff | 2018-05-21 16:39:56 +0300 | [diff] [blame] | 1725 | xhci_port_missing_cas_quirk(ports[port_index])) { |
Mathias Nyman | 346e9973 | 2016-10-20 18:09:19 +0300 | [diff] [blame] | 1726 | xhci_dbg(xhci, "reset stuck port %d\n", port_index); |
Mathias Nyman | a85c0f8 | 2017-08-16 14:23:26 +0300 | [diff] [blame] | 1727 | clear_bit(port_index, &bus_state->bus_suspended); |
Mathias Nyman | 346e9973 | 2016-10-20 18:09:19 +0300 | [diff] [blame] | 1728 | continue; |
| 1729 | } |
Mathias Nyman | a85c0f8 | 2017-08-16 14:23:26 +0300 | [diff] [blame] | 1730 | /* resume if we suspended the link, and it is still suspended */ |
| 1731 | if (test_bit(port_index, &bus_state->bus_suspended)) |
| 1732 | switch (portsc & PORT_PLS_MASK) { |
| 1733 | case XDEV_U3: |
| 1734 | portsc = xhci_port_state_to_neutral(portsc); |
| 1735 | portsc &= ~PORT_PLS_MASK; |
| 1736 | portsc |= PORT_LINK_STROBE | next_state; |
| 1737 | break; |
| 1738 | case XDEV_RESUME: |
| 1739 | /* resume already initiated */ |
| 1740 | break; |
| 1741 | default: |
| 1742 | /* not in a resumeable state, ignore it */ |
| 1743 | clear_bit(port_index, |
| 1744 | &bus_state->bus_suspended); |
| 1745 | break; |
Andiry Xu | 9777e3c | 2010-10-14 07:23:03 -0700 | [diff] [blame] | 1746 | } |
Mathias Nyman | a85c0f8 | 2017-08-16 14:23:26 +0300 | [diff] [blame] | 1747 | /* disable wake for all ports, write new link state if needed */ |
| 1748 | portsc &= ~(PORT_RWC_BITS | PORT_CEC | PORT_WAKE_BITS); |
Mathias Nyman | e740b01 | 2018-05-21 16:39:55 +0300 | [diff] [blame] | 1749 | writel(portsc, ports[port_index]->addr); |
Andiry Xu | 9777e3c | 2010-10-14 07:23:03 -0700 | [diff] [blame] | 1750 | } |
| 1751 | |
Mathias Nyman | a85c0f8 | 2017-08-16 14:23:26 +0300 | [diff] [blame] | 1752 | /* USB2 specific resume signaling delay and U0 link state transition */ |
| 1753 | if (hcd->speed < HCD_USB3) { |
| 1754 | if (bus_state->bus_suspended) { |
| 1755 | spin_unlock_irqrestore(&xhci->lock, flags); |
| 1756 | msleep(USB_RESUME_TIMEOUT); |
| 1757 | spin_lock_irqsave(&xhci->lock, flags); |
| 1758 | } |
| 1759 | for_each_set_bit(port_index, &bus_state->bus_suspended, |
| 1760 | BITS_PER_LONG) { |
| 1761 | /* Clear PLC to poll it later for U0 transition */ |
Mathias Nyman | eaefcf2 | 2018-05-21 16:40:00 +0300 | [diff] [blame] | 1762 | xhci_test_and_clear_bit(xhci, ports[port_index], |
Mathias Nyman | a85c0f8 | 2017-08-16 14:23:26 +0300 | [diff] [blame] | 1763 | PORT_PLC); |
Mathias Nyman | 6b7f40f | 2018-05-21 16:39:59 +0300 | [diff] [blame] | 1764 | xhci_set_link_state(xhci, ports[port_index], XDEV_U0); |
Mathias Nyman | a85c0f8 | 2017-08-16 14:23:26 +0300 | [diff] [blame] | 1765 | } |
Mathias Nyman | 41485a9 | 2015-05-29 17:01:51 +0300 | [diff] [blame] | 1766 | } |
| 1767 | |
Mathias Nyman | a85c0f8 | 2017-08-16 14:23:26 +0300 | [diff] [blame] | 1768 | /* poll for U0 link state complete, both USB2 and USB3 */ |
| 1769 | for_each_set_bit(port_index, &bus_state->bus_suspended, BITS_PER_LONG) { |
Mathias Nyman | e740b01 | 2018-05-21 16:39:55 +0300 | [diff] [blame] | 1770 | sret = xhci_handshake(ports[port_index]->addr, PORT_PLC, |
Mathias Nyman | 41485a9 | 2015-05-29 17:01:51 +0300 | [diff] [blame] | 1771 | PORT_PLC, 10 * 1000); |
Mathias Nyman | a85c0f8 | 2017-08-16 14:23:26 +0300 | [diff] [blame] | 1772 | if (sret) { |
Mathias Nyman | 41485a9 | 2015-05-29 17:01:51 +0300 | [diff] [blame] | 1773 | xhci_warn(xhci, "port %d resume PLC timeout\n", |
| 1774 | port_index); |
Mathias Nyman | a85c0f8 | 2017-08-16 14:23:26 +0300 | [diff] [blame] | 1775 | continue; |
| 1776 | } |
Mathias Nyman | eaefcf2 | 2018-05-21 16:40:00 +0300 | [diff] [blame] | 1777 | xhci_test_and_clear_bit(xhci, ports[port_index], PORT_PLC); |
Mathias Nyman | 41485a9 | 2015-05-29 17:01:51 +0300 | [diff] [blame] | 1778 | slot_id = xhci_find_slot_id_by_port(hcd, xhci, port_index + 1); |
| 1779 | if (slot_id) |
| 1780 | xhci_ring_device(xhci, slot_id); |
| 1781 | } |
Xenia Ragiadakou | b0ba972 | 2013-11-15 05:34:06 +0200 | [diff] [blame] | 1782 | (void) readl(&xhci->op_regs->command); |
Andiry Xu | 9777e3c | 2010-10-14 07:23:03 -0700 | [diff] [blame] | 1783 | |
Sarah Sharp | 20b67cf | 2010-12-15 12:47:14 -0800 | [diff] [blame] | 1784 | bus_state->next_statechange = jiffies + msecs_to_jiffies(5); |
Andiry Xu | 9777e3c | 2010-10-14 07:23:03 -0700 | [diff] [blame] | 1785 | /* re-enable irqs */ |
Xenia Ragiadakou | b0ba972 | 2013-11-15 05:34:06 +0200 | [diff] [blame] | 1786 | temp = readl(&xhci->op_regs->command); |
Andiry Xu | 9777e3c | 2010-10-14 07:23:03 -0700 | [diff] [blame] | 1787 | temp |= CMD_EIE; |
Xenia Ragiadakou | 204b779 | 2013-11-15 05:34:07 +0200 | [diff] [blame] | 1788 | writel(temp, &xhci->op_regs->command); |
Xenia Ragiadakou | b0ba972 | 2013-11-15 05:34:06 +0200 | [diff] [blame] | 1789 | temp = readl(&xhci->op_regs->command); |
Andiry Xu | 9777e3c | 2010-10-14 07:23:03 -0700 | [diff] [blame] | 1790 | |
| 1791 | spin_unlock_irqrestore(&xhci->lock, flags); |
| 1792 | return 0; |
| 1793 | } |
| 1794 | |
Alan Stern | 8f9cc83c | 2018-06-08 16:59:57 -0400 | [diff] [blame] | 1795 | unsigned long xhci_get_resuming_ports(struct usb_hcd *hcd) |
| 1796 | { |
Mathias Nyman | f6187f4 | 2018-12-07 16:19:30 +0200 | [diff] [blame] | 1797 | struct xhci_hub *rhub = xhci_get_rhub(hcd); |
Alan Stern | 8f9cc83c | 2018-06-08 16:59:57 -0400 | [diff] [blame] | 1798 | |
| 1799 | /* USB3 port wakeups are reported via usb_wakeup_notification() */ |
Mathias Nyman | f6187f4 | 2018-12-07 16:19:30 +0200 | [diff] [blame] | 1800 | return rhub->bus_state.resuming_ports; /* USB2 ports only */ |
Alan Stern | 8f9cc83c | 2018-06-08 16:59:57 -0400 | [diff] [blame] | 1801 | } |
| 1802 | |
Sarah Sharp | 436a389 | 2010-10-15 14:59:15 -0700 | [diff] [blame] | 1803 | #endif /* CONFIG_PM */ |