rjw | 1f88458 | 2022-01-06 17:20:42 +0800 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright (C) 2001-2004 by David Brownell |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify it |
| 5 | * under the terms of the GNU General Public License as published by the |
| 6 | * Free Software Foundation; either version 2 of the License, or (at your |
| 7 | * option) any later version. |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, but |
| 10 | * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY |
| 11 | * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 12 | * for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write to the Free Software Foundation, |
| 16 | * Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
| 17 | */ |
| 18 | |
| 19 | /* this file is part of ehci-hcd.c */ |
| 20 | |
| 21 | /*-------------------------------------------------------------------------*/ |
| 22 | |
| 23 | /* |
| 24 | * EHCI Root Hub ... the nonsharable stuff |
| 25 | * |
| 26 | * Registers don't need cpu_to_le32, that happens transparently |
| 27 | */ |
| 28 | |
| 29 | /*-------------------------------------------------------------------------*/ |
| 30 | |
| 31 | #define PORT_WAKE_BITS (PORT_WKOC_E|PORT_WKDISC_E|PORT_WKCONN_E) |
| 32 | |
| 33 | #ifdef CONFIG_PM |
| 34 | |
| 35 | static void unlink_empty_async_suspended(struct ehci_hcd *ehci); |
| 36 | |
| 37 | static int persist_enabled_on_companion(struct usb_device *udev, void *unused) |
| 38 | { |
| 39 | return !udev->maxchild && udev->persist_enabled && |
| 40 | udev->bus->root_hub->speed < USB_SPEED_HIGH; |
| 41 | } |
| 42 | |
| 43 | /* After a power loss, ports that were owned by the companion must be |
| 44 | * reset so that the companion can still own them. |
| 45 | */ |
| 46 | static void ehci_handover_companion_ports(struct ehci_hcd *ehci) |
| 47 | { |
| 48 | u32 __iomem *reg; |
| 49 | u32 status; |
| 50 | int port; |
| 51 | __le32 buf; |
| 52 | struct usb_hcd *hcd = ehci_to_hcd(ehci); |
| 53 | |
| 54 | if (!ehci->owned_ports) |
| 55 | return; |
| 56 | |
| 57 | /* |
| 58 | * USB 1.1 devices are mostly HIDs, which don't need to persist across |
| 59 | * suspends. If we ensure that none of our companion's devices have |
| 60 | * persist_enabled (by looking through all USB 1.1 buses in the system), |
| 61 | * we can skip this and avoid slowing resume down. Devices without |
| 62 | * persist will just get reenumerated shortly after resume anyway. |
| 63 | */ |
| 64 | if (!usb_for_each_dev(NULL, persist_enabled_on_companion)) |
| 65 | return; |
| 66 | |
| 67 | /* Make sure the ports are powered */ |
| 68 | port = HCS_N_PORTS(ehci->hcs_params); |
| 69 | while (port--) { |
| 70 | if (test_bit(port, &ehci->owned_ports)) { |
| 71 | reg = &ehci->regs->port_status[port]; |
| 72 | status = ehci_readl(ehci, reg) & ~PORT_RWC_BITS; |
| 73 | if (!(status & PORT_POWER)) |
| 74 | ehci_port_power(ehci, port, true); |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | /* Give the connections some time to appear */ |
| 79 | msleep(20); |
| 80 | |
| 81 | spin_lock_irq(&ehci->lock); |
| 82 | port = HCS_N_PORTS(ehci->hcs_params); |
| 83 | while (port--) { |
| 84 | if (test_bit(port, &ehci->owned_ports)) { |
| 85 | reg = &ehci->regs->port_status[port]; |
| 86 | status = ehci_readl(ehci, reg) & ~PORT_RWC_BITS; |
| 87 | |
| 88 | /* Port already owned by companion? */ |
| 89 | if (status & PORT_OWNER) |
| 90 | clear_bit(port, &ehci->owned_ports); |
| 91 | else if (test_bit(port, &ehci->companion_ports)) |
| 92 | ehci_writel(ehci, status & ~PORT_PE, reg); |
| 93 | else { |
| 94 | spin_unlock_irq(&ehci->lock); |
| 95 | ehci_hub_control(hcd, SetPortFeature, |
| 96 | USB_PORT_FEAT_RESET, port + 1, |
| 97 | NULL, 0); |
| 98 | spin_lock_irq(&ehci->lock); |
| 99 | } |
| 100 | } |
| 101 | } |
| 102 | spin_unlock_irq(&ehci->lock); |
| 103 | |
| 104 | if (!ehci->owned_ports) |
| 105 | return; |
| 106 | msleep(90); /* Wait for resets to complete */ |
| 107 | |
| 108 | spin_lock_irq(&ehci->lock); |
| 109 | port = HCS_N_PORTS(ehci->hcs_params); |
| 110 | while (port--) { |
| 111 | if (test_bit(port, &ehci->owned_ports)) { |
| 112 | spin_unlock_irq(&ehci->lock); |
| 113 | ehci_hub_control(hcd, GetPortStatus, |
| 114 | 0, port + 1, |
| 115 | (char *) &buf, sizeof(buf)); |
| 116 | spin_lock_irq(&ehci->lock); |
| 117 | |
| 118 | /* The companion should now own the port, |
| 119 | * but if something went wrong the port must not |
| 120 | * remain enabled. |
| 121 | */ |
| 122 | reg = &ehci->regs->port_status[port]; |
| 123 | status = ehci_readl(ehci, reg) & ~PORT_RWC_BITS; |
| 124 | if (status & PORT_OWNER) |
| 125 | ehci_writel(ehci, status | PORT_CSC, reg); |
| 126 | else { |
| 127 | ehci_dbg(ehci, "failed handover port %d: %x\n", |
| 128 | port + 1, status); |
| 129 | ehci_writel(ehci, status & ~PORT_PE, reg); |
| 130 | } |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | ehci->owned_ports = 0; |
| 135 | spin_unlock_irq(&ehci->lock); |
| 136 | } |
| 137 | |
| 138 | static int ehci_port_change(struct ehci_hcd *ehci) |
| 139 | { |
| 140 | int i = HCS_N_PORTS(ehci->hcs_params); |
| 141 | |
| 142 | /* First check if the controller indicates a change event */ |
| 143 | |
| 144 | if (ehci_readl(ehci, &ehci->regs->status) & STS_PCD) |
| 145 | return 1; |
| 146 | |
| 147 | /* |
| 148 | * Not all controllers appear to update this while going from D3 to D0, |
| 149 | * so check the individual port status registers as well |
| 150 | */ |
| 151 | |
| 152 | while (i--) |
| 153 | if (ehci_readl(ehci, &ehci->regs->port_status[i]) & PORT_CSC) |
| 154 | return 1; |
| 155 | |
| 156 | return 0; |
| 157 | } |
| 158 | |
| 159 | void ehci_adjust_port_wakeup_flags(struct ehci_hcd *ehci, |
| 160 | bool suspending, bool do_wakeup) |
| 161 | { |
| 162 | int port; |
| 163 | u32 temp; |
| 164 | |
| 165 | /* If remote wakeup is enabled for the root hub but disabled |
| 166 | * for the controller, we must adjust all the port wakeup flags |
| 167 | * when the controller is suspended or resumed. In all other |
| 168 | * cases they don't need to be changed. |
| 169 | */ |
| 170 | if (!ehci_to_hcd(ehci)->self.root_hub->do_remote_wakeup || do_wakeup) |
| 171 | return; |
| 172 | |
| 173 | spin_lock_irq(&ehci->lock); |
| 174 | |
| 175 | /* clear phy low-power mode before changing wakeup flags */ |
| 176 | if (ehci->has_tdi_phy_lpm) { |
| 177 | port = HCS_N_PORTS(ehci->hcs_params); |
| 178 | while (port--) { |
| 179 | u32 __iomem *hostpc_reg = &ehci->regs->hostpc[port]; |
| 180 | |
| 181 | temp = ehci_readl(ehci, hostpc_reg); |
| 182 | ehci_writel(ehci, temp & ~HOSTPC_PHCD, hostpc_reg); |
| 183 | } |
| 184 | spin_unlock_irq(&ehci->lock); |
| 185 | msleep(5); |
| 186 | spin_lock_irq(&ehci->lock); |
| 187 | } |
| 188 | |
| 189 | port = HCS_N_PORTS(ehci->hcs_params); |
| 190 | while (port--) { |
| 191 | u32 __iomem *reg = &ehci->regs->port_status[port]; |
| 192 | u32 t1 = ehci_readl(ehci, reg) & ~PORT_RWC_BITS; |
| 193 | u32 t2 = t1 & ~PORT_WAKE_BITS; |
| 194 | |
| 195 | /* If we are suspending the controller, clear the flags. |
| 196 | * If we are resuming the controller, set the wakeup flags. |
| 197 | */ |
| 198 | if (!suspending) { |
| 199 | if (t1 & PORT_CONNECT) |
| 200 | t2 |= PORT_WKOC_E | PORT_WKDISC_E; |
| 201 | else |
| 202 | t2 |= PORT_WKOC_E | PORT_WKCONN_E; |
| 203 | } |
| 204 | ehci_writel(ehci, t2, reg); |
| 205 | } |
| 206 | |
| 207 | /* enter phy low-power mode again */ |
| 208 | if (ehci->has_tdi_phy_lpm) { |
| 209 | port = HCS_N_PORTS(ehci->hcs_params); |
| 210 | while (port--) { |
| 211 | u32 __iomem *hostpc_reg = &ehci->regs->hostpc[port]; |
| 212 | |
| 213 | temp = ehci_readl(ehci, hostpc_reg); |
| 214 | ehci_writel(ehci, temp | HOSTPC_PHCD, hostpc_reg); |
| 215 | } |
| 216 | } |
| 217 | |
| 218 | /* Does the root hub have a port wakeup pending? */ |
| 219 | if (!suspending && ehci_port_change(ehci)) |
| 220 | usb_hcd_resume_root_hub(ehci_to_hcd(ehci)); |
| 221 | |
| 222 | spin_unlock_irq(&ehci->lock); |
| 223 | } |
| 224 | EXPORT_SYMBOL_GPL(ehci_adjust_port_wakeup_flags); |
| 225 | |
| 226 | static int ehci_bus_suspend (struct usb_hcd *hcd) |
| 227 | { |
| 228 | struct ehci_hcd *ehci = hcd_to_ehci (hcd); |
| 229 | int port; |
| 230 | int mask; |
| 231 | int changed; |
| 232 | bool fs_idle_delay; |
| 233 | |
| 234 | ehci_dbg(ehci, "suspend root hub\n"); |
| 235 | |
| 236 | if (time_before (jiffies, ehci->next_statechange)) |
| 237 | msleep(5); |
| 238 | |
| 239 | /* stop the schedules */ |
| 240 | ehci_quiesce(ehci); |
| 241 | |
| 242 | spin_lock_irq (&ehci->lock); |
| 243 | if (ehci->rh_state < EHCI_RH_RUNNING) |
| 244 | goto done; |
| 245 | |
| 246 | /* Once the controller is stopped, port resumes that are already |
| 247 | * in progress won't complete. Hence if remote wakeup is enabled |
| 248 | * for the root hub and any ports are in the middle of a resume or |
| 249 | * remote wakeup, we must fail the suspend. |
| 250 | */ |
| 251 | if (hcd->self.root_hub->do_remote_wakeup) { |
| 252 | if (ehci->resuming_ports) { |
| 253 | spin_unlock_irq(&ehci->lock); |
| 254 | ehci_dbg(ehci, "suspend failed because a port is resuming\n"); |
| 255 | return -EBUSY; |
| 256 | } |
| 257 | } |
| 258 | |
| 259 | /* Unlike other USB host controller types, EHCI doesn't have |
| 260 | * any notion of "global" or bus-wide suspend. The driver has |
| 261 | * to manually suspend all the active unsuspended ports, and |
| 262 | * then manually resume them in the bus_resume() routine. |
| 263 | */ |
| 264 | ehci->bus_suspended = 0; |
| 265 | ehci->owned_ports = 0; |
| 266 | changed = 0; |
| 267 | fs_idle_delay = false; |
| 268 | port = HCS_N_PORTS(ehci->hcs_params); |
| 269 | while (port--) { |
| 270 | u32 __iomem *reg = &ehci->regs->port_status [port]; |
| 271 | u32 t1 = ehci_readl(ehci, reg) & ~PORT_RWC_BITS; |
| 272 | u32 t2 = t1 & ~PORT_WAKE_BITS; |
| 273 | |
| 274 | /* keep track of which ports we suspend */ |
| 275 | if (t1 & PORT_OWNER) |
| 276 | set_bit(port, &ehci->owned_ports); |
| 277 | else if ((t1 & PORT_PE) && !(t1 & PORT_SUSPEND)) { |
| 278 | t2 |= PORT_SUSPEND; |
| 279 | set_bit(port, &ehci->bus_suspended); |
| 280 | } |
| 281 | |
| 282 | /* enable remote wakeup on all ports, if told to do so */ |
| 283 | if (hcd->self.root_hub->do_remote_wakeup) { |
| 284 | /* only enable appropriate wake bits, otherwise the |
| 285 | * hardware can not go phy low power mode. If a race |
| 286 | * condition happens here(connection change during bits |
| 287 | * set), the port change detection will finally fix it. |
| 288 | */ |
| 289 | if (t1 & PORT_CONNECT) |
| 290 | t2 |= PORT_WKOC_E | PORT_WKDISC_E; |
| 291 | else |
| 292 | t2 |= PORT_WKOC_E | PORT_WKCONN_E; |
| 293 | } |
| 294 | |
| 295 | if (t1 != t2) { |
| 296 | /* |
| 297 | * On some controllers, Wake-On-Disconnect will |
| 298 | * generate false wakeup signals until the bus |
| 299 | * switches over to full-speed idle. For their |
| 300 | * sake, add a delay if we need one. |
| 301 | */ |
| 302 | if ((t2 & PORT_WKDISC_E) && |
| 303 | ehci_port_speed(ehci, t2) == |
| 304 | USB_PORT_STAT_HIGH_SPEED) |
| 305 | fs_idle_delay = true; |
| 306 | ehci_writel(ehci, t2, reg); |
| 307 | changed = 1; |
| 308 | } |
| 309 | } |
| 310 | spin_unlock_irq(&ehci->lock); |
| 311 | |
| 312 | if (changed && ehci_has_fsl_susp_errata(ehci)) |
| 313 | /* |
| 314 | * Wait for at least 10 millisecondes to ensure the controller |
| 315 | * enter the suspend status before initiating a port resume |
| 316 | * using the Force Port Resume bit (Not-EHCI compatible). |
| 317 | */ |
| 318 | usleep_range(10000, 20000); |
| 319 | |
| 320 | if ((changed && ehci->has_tdi_phy_lpm) || fs_idle_delay) { |
| 321 | /* |
| 322 | * Wait for HCD to enter low-power mode or for the bus |
| 323 | * to switch to full-speed idle. |
| 324 | */ |
| 325 | usleep_range(5000, 5500); |
| 326 | } |
| 327 | |
| 328 | if (changed && ehci->has_tdi_phy_lpm) { |
| 329 | spin_lock_irq(&ehci->lock); |
| 330 | port = HCS_N_PORTS(ehci->hcs_params); |
| 331 | while (port--) { |
| 332 | u32 __iomem *hostpc_reg = &ehci->regs->hostpc[port]; |
| 333 | u32 t3; |
| 334 | |
| 335 | t3 = ehci_readl(ehci, hostpc_reg); |
| 336 | ehci_writel(ehci, t3 | HOSTPC_PHCD, hostpc_reg); |
| 337 | t3 = ehci_readl(ehci, hostpc_reg); |
| 338 | ehci_dbg(ehci, "Port %d phy low-power mode %s\n", |
| 339 | port, (t3 & HOSTPC_PHCD) ? |
| 340 | "succeeded" : "failed"); |
| 341 | } |
| 342 | spin_unlock_irq(&ehci->lock); |
| 343 | } |
| 344 | |
| 345 | /* Apparently some devices need a >= 1-uframe delay here */ |
| 346 | if (ehci->bus_suspended) |
| 347 | udelay(150); |
| 348 | |
| 349 | /* turn off now-idle HC */ |
| 350 | ehci_halt (ehci); |
| 351 | |
| 352 | spin_lock_irq(&ehci->lock); |
| 353 | if (ehci->enabled_hrtimer_events & BIT(EHCI_HRTIMER_POLL_DEAD)) |
| 354 | ehci_handle_controller_death(ehci); |
| 355 | if (ehci->rh_state != EHCI_RH_RUNNING) |
| 356 | goto done; |
| 357 | ehci->rh_state = EHCI_RH_SUSPENDED; |
| 358 | |
| 359 | unlink_empty_async_suspended(ehci); |
| 360 | |
| 361 | /* Any IAA cycle that started before the suspend is now invalid */ |
| 362 | end_iaa_cycle(ehci); |
| 363 | ehci_handle_start_intr_unlinks(ehci); |
| 364 | ehci_handle_intr_unlinks(ehci); |
| 365 | end_free_itds(ehci); |
| 366 | |
| 367 | /* allow remote wakeup */ |
| 368 | mask = INTR_MASK; |
| 369 | if (!hcd->self.root_hub->do_remote_wakeup) |
| 370 | mask &= ~STS_PCD; |
| 371 | ehci_writel(ehci, mask, &ehci->regs->intr_enable); |
| 372 | ehci_readl(ehci, &ehci->regs->intr_enable); |
| 373 | |
| 374 | done: |
| 375 | ehci->next_statechange = jiffies + msecs_to_jiffies(10); |
| 376 | ehci->enabled_hrtimer_events = 0; |
| 377 | ehci->next_hrtimer_event = EHCI_HRTIMER_NO_EVENT; |
| 378 | spin_unlock_irq (&ehci->lock); |
| 379 | |
| 380 | hrtimer_cancel(&ehci->hrtimer); |
| 381 | return 0; |
| 382 | } |
| 383 | |
| 384 | |
| 385 | /* caller has locked the root hub, and should reset/reinit on error */ |
| 386 | static int ehci_bus_resume (struct usb_hcd *hcd) |
| 387 | { |
| 388 | struct ehci_hcd *ehci = hcd_to_ehci (hcd); |
| 389 | u32 temp; |
| 390 | u32 power_okay; |
| 391 | int i; |
| 392 | unsigned long resume_needed = 0; |
| 393 | |
| 394 | if (time_before (jiffies, ehci->next_statechange)) |
| 395 | msleep(5); |
| 396 | spin_lock_irq (&ehci->lock); |
| 397 | if (!HCD_HW_ACCESSIBLE(hcd) || ehci->shutdown) |
| 398 | goto shutdown; |
| 399 | |
| 400 | if (unlikely(ehci->debug)) { |
| 401 | if (!dbgp_reset_prep(hcd)) |
| 402 | ehci->debug = NULL; |
| 403 | else |
| 404 | dbgp_external_startup(hcd); |
| 405 | } |
| 406 | |
| 407 | /* Ideally and we've got a real resume here, and no port's power |
| 408 | * was lost. (For PCI, that means Vaux was maintained.) But we |
| 409 | * could instead be restoring a swsusp snapshot -- so that BIOS was |
| 410 | * the last user of the controller, not reset/pm hardware keeping |
| 411 | * state we gave to it. |
| 412 | */ |
| 413 | power_okay = ehci_readl(ehci, &ehci->regs->intr_enable); |
| 414 | ehci_dbg(ehci, "resume root hub%s\n", |
| 415 | power_okay ? "" : " after power loss"); |
| 416 | |
| 417 | /* at least some APM implementations will try to deliver |
| 418 | * IRQs right away, so delay them until we're ready. |
| 419 | */ |
| 420 | ehci_writel(ehci, 0, &ehci->regs->intr_enable); |
| 421 | |
| 422 | /* re-init operational registers */ |
| 423 | ehci_writel(ehci, 0, &ehci->regs->segment); |
| 424 | ehci_writel(ehci, ehci->periodic_dma, &ehci->regs->frame_list); |
| 425 | ehci_writel(ehci, (u32) ehci->async->qh_dma, &ehci->regs->async_next); |
| 426 | |
| 427 | /* restore CMD_RUN, framelist size, and irq threshold */ |
| 428 | ehci->command |= CMD_RUN; |
| 429 | ehci_writel(ehci, ehci->command, &ehci->regs->command); |
| 430 | ehci->rh_state = EHCI_RH_RUNNING; |
| 431 | |
| 432 | /* |
| 433 | * According to Bugzilla #8190, the port status for some controllers |
| 434 | * will be wrong without a delay. At their wrong status, the port |
| 435 | * is enabled, but not suspended neither resumed. |
| 436 | */ |
| 437 | i = HCS_N_PORTS(ehci->hcs_params); |
| 438 | while (i--) { |
| 439 | temp = ehci_readl(ehci, &ehci->regs->port_status[i]); |
| 440 | if ((temp & PORT_PE) && |
| 441 | !(temp & (PORT_SUSPEND | PORT_RESUME))) { |
| 442 | ehci_dbg(ehci, "Port status(0x%x) is wrong\n", temp); |
| 443 | spin_unlock_irq(&ehci->lock); |
| 444 | msleep(8); |
| 445 | spin_lock_irq(&ehci->lock); |
| 446 | break; |
| 447 | } |
| 448 | } |
| 449 | |
| 450 | if (ehci->shutdown) |
| 451 | goto shutdown; |
| 452 | |
| 453 | /* clear phy low-power mode before resume */ |
| 454 | if (ehci->bus_suspended && ehci->has_tdi_phy_lpm) { |
| 455 | i = HCS_N_PORTS(ehci->hcs_params); |
| 456 | while (i--) { |
| 457 | if (test_bit(i, &ehci->bus_suspended)) { |
| 458 | u32 __iomem *hostpc_reg = |
| 459 | &ehci->regs->hostpc[i]; |
| 460 | |
| 461 | temp = ehci_readl(ehci, hostpc_reg); |
| 462 | ehci_writel(ehci, temp & ~HOSTPC_PHCD, |
| 463 | hostpc_reg); |
| 464 | } |
| 465 | } |
| 466 | spin_unlock_irq(&ehci->lock); |
| 467 | msleep(5); |
| 468 | spin_lock_irq(&ehci->lock); |
| 469 | if (ehci->shutdown) |
| 470 | goto shutdown; |
| 471 | } |
| 472 | |
| 473 | /* manually resume the ports we suspended during bus_suspend() */ |
| 474 | i = HCS_N_PORTS (ehci->hcs_params); |
| 475 | while (i--) { |
| 476 | temp = ehci_readl(ehci, &ehci->regs->port_status [i]); |
| 477 | temp &= ~(PORT_RWC_BITS | PORT_WAKE_BITS); |
| 478 | if (test_bit(i, &ehci->bus_suspended) && |
| 479 | (temp & PORT_SUSPEND)) { |
| 480 | temp |= PORT_RESUME; |
| 481 | set_bit(i, &resume_needed); |
| 482 | } |
| 483 | ehci_writel(ehci, temp, &ehci->regs->port_status [i]); |
| 484 | } |
| 485 | |
| 486 | /* |
| 487 | * msleep for USB_RESUME_TIMEOUT ms only if code is trying to resume |
| 488 | * port |
| 489 | */ |
| 490 | if (resume_needed) { |
| 491 | spin_unlock_irq(&ehci->lock); |
| 492 | msleep(USB_RESUME_TIMEOUT); |
| 493 | spin_lock_irq(&ehci->lock); |
| 494 | if (ehci->shutdown) |
| 495 | goto shutdown; |
| 496 | } |
| 497 | |
| 498 | i = HCS_N_PORTS (ehci->hcs_params); |
| 499 | while (i--) { |
| 500 | temp = ehci_readl(ehci, &ehci->regs->port_status [i]); |
| 501 | if (test_bit(i, &resume_needed)) { |
| 502 | temp &= ~(PORT_RWC_BITS | PORT_SUSPEND | PORT_RESUME); |
| 503 | ehci_writel(ehci, temp, &ehci->regs->port_status [i]); |
| 504 | } |
| 505 | } |
| 506 | |
| 507 | ehci->next_statechange = jiffies + msecs_to_jiffies(5); |
| 508 | spin_unlock_irq(&ehci->lock); |
| 509 | |
| 510 | ehci_handover_companion_ports(ehci); |
| 511 | |
| 512 | /* Now we can safely re-enable irqs */ |
| 513 | spin_lock_irq(&ehci->lock); |
| 514 | if (ehci->shutdown) |
| 515 | goto shutdown; |
| 516 | ehci_writel(ehci, INTR_MASK, &ehci->regs->intr_enable); |
| 517 | (void) ehci_readl(ehci, &ehci->regs->intr_enable); |
| 518 | spin_unlock_irq(&ehci->lock); |
| 519 | |
| 520 | return 0; |
| 521 | |
| 522 | shutdown: |
| 523 | spin_unlock_irq(&ehci->lock); |
| 524 | return -ESHUTDOWN; |
| 525 | } |
| 526 | |
| 527 | #else |
| 528 | |
| 529 | #define ehci_bus_suspend NULL |
| 530 | #define ehci_bus_resume NULL |
| 531 | |
| 532 | #endif /* CONFIG_PM */ |
| 533 | |
| 534 | /*-------------------------------------------------------------------------*/ |
| 535 | |
| 536 | /* |
| 537 | * Sets the owner of a port |
| 538 | */ |
| 539 | static void set_owner(struct ehci_hcd *ehci, int portnum, int new_owner) |
| 540 | { |
| 541 | u32 __iomem *status_reg; |
| 542 | u32 port_status; |
| 543 | int try; |
| 544 | |
| 545 | status_reg = &ehci->regs->port_status[portnum]; |
| 546 | |
| 547 | /* |
| 548 | * The controller won't set the OWNER bit if the port is |
| 549 | * enabled, so this loop will sometimes require at least two |
| 550 | * iterations: one to disable the port and one to set OWNER. |
| 551 | */ |
| 552 | for (try = 4; try > 0; --try) { |
| 553 | spin_lock_irq(&ehci->lock); |
| 554 | port_status = ehci_readl(ehci, status_reg); |
| 555 | if ((port_status & PORT_OWNER) == new_owner |
| 556 | || (port_status & (PORT_OWNER | PORT_CONNECT)) |
| 557 | == 0) |
| 558 | try = 0; |
| 559 | else { |
| 560 | port_status ^= PORT_OWNER; |
| 561 | port_status &= ~(PORT_PE | PORT_RWC_BITS); |
| 562 | ehci_writel(ehci, port_status, status_reg); |
| 563 | } |
| 564 | spin_unlock_irq(&ehci->lock); |
| 565 | if (try > 1) |
| 566 | msleep(5); |
| 567 | } |
| 568 | } |
| 569 | |
| 570 | /*-------------------------------------------------------------------------*/ |
| 571 | |
| 572 | static int check_reset_complete ( |
| 573 | struct ehci_hcd *ehci, |
| 574 | int index, |
| 575 | u32 __iomem *status_reg, |
| 576 | int port_status |
| 577 | ) { |
| 578 | if (!(port_status & PORT_CONNECT)) |
| 579 | return port_status; |
| 580 | |
| 581 | /* if reset finished and it's still not enabled -- handoff */ |
| 582 | if (!(port_status & PORT_PE)) { |
| 583 | |
| 584 | /* with integrated TT, there's nobody to hand it to! */ |
| 585 | if (ehci_is_TDI(ehci)) { |
| 586 | ehci_dbg (ehci, |
| 587 | "Failed to enable port %d on root hub TT\n", |
| 588 | index+1); |
| 589 | return port_status; |
| 590 | } |
| 591 | |
| 592 | ehci_dbg (ehci, "port %d full speed --> companion\n", |
| 593 | index + 1); |
| 594 | |
| 595 | // what happens if HCS_N_CC(params) == 0 ? |
| 596 | port_status |= PORT_OWNER; |
| 597 | port_status &= ~PORT_RWC_BITS; |
| 598 | ehci_writel(ehci, port_status, status_reg); |
| 599 | |
| 600 | /* ensure 440EPX ohci controller state is operational */ |
| 601 | if (ehci->has_amcc_usb23) |
| 602 | set_ohci_hcfs(ehci, 1); |
| 603 | } else { |
| 604 | ehci_dbg(ehci, "port %d reset complete, port enabled\n", |
| 605 | index + 1); |
| 606 | /* ensure 440EPx ohci controller state is suspended */ |
| 607 | if (ehci->has_amcc_usb23) |
| 608 | set_ohci_hcfs(ehci, 0); |
| 609 | } |
| 610 | |
| 611 | return port_status; |
| 612 | } |
| 613 | |
| 614 | /*-------------------------------------------------------------------------*/ |
| 615 | |
| 616 | |
| 617 | /* build "status change" packet (one or two bytes) from HC registers */ |
| 618 | |
| 619 | static int |
| 620 | ehci_hub_status_data (struct usb_hcd *hcd, char *buf) |
| 621 | { |
| 622 | struct ehci_hcd *ehci = hcd_to_ehci (hcd); |
| 623 | u32 temp, status; |
| 624 | u32 mask; |
| 625 | int ports, i, retval = 1; |
| 626 | unsigned long flags; |
| 627 | u32 ppcd = ~0; |
| 628 | |
| 629 | /* init status to no-changes */ |
| 630 | buf [0] = 0; |
| 631 | ports = HCS_N_PORTS (ehci->hcs_params); |
| 632 | if (ports > 7) { |
| 633 | buf [1] = 0; |
| 634 | retval++; |
| 635 | } |
| 636 | |
| 637 | /* Inform the core about resumes-in-progress by returning |
| 638 | * a non-zero value even if there are no status changes. |
| 639 | */ |
| 640 | status = ehci->resuming_ports; |
| 641 | |
| 642 | /* Some boards (mostly VIA?) report bogus overcurrent indications, |
| 643 | * causing massive log spam unless we completely ignore them. It |
| 644 | * may be relevant that VIA VT8235 controllers, where PORT_POWER is |
| 645 | * always set, seem to clear PORT_OCC and PORT_CSC when writing to |
| 646 | * PORT_POWER; that's surprising, but maybe within-spec. |
| 647 | */ |
| 648 | if (!ignore_oc) |
| 649 | mask = PORT_CSC | PORT_PEC | PORT_OCC; |
| 650 | else |
| 651 | mask = PORT_CSC | PORT_PEC; |
| 652 | // PORT_RESUME from hardware ~= PORT_STAT_C_SUSPEND |
| 653 | |
| 654 | /* no hub change reports (bit 0) for now (power, ...) */ |
| 655 | |
| 656 | /* port N changes (bit N)? */ |
| 657 | spin_lock_irqsave (&ehci->lock, flags); |
| 658 | |
| 659 | /* get per-port change detect bits */ |
| 660 | if (ehci->has_ppcd) |
| 661 | ppcd = ehci_readl(ehci, &ehci->regs->status) >> 16; |
| 662 | |
| 663 | for (i = 0; i < ports; i++) { |
| 664 | /* leverage per-port change bits feature */ |
| 665 | if (ppcd & (1 << i)) |
| 666 | temp = ehci_readl(ehci, &ehci->regs->port_status[i]); |
| 667 | else |
| 668 | temp = 0; |
| 669 | |
| 670 | /* |
| 671 | * Return status information even for ports with OWNER set. |
| 672 | * Otherwise hub_wq wouldn't see the disconnect event when a |
| 673 | * high-speed device is switched over to the companion |
| 674 | * controller by the user. |
| 675 | */ |
| 676 | |
| 677 | if ((temp & mask) != 0 || test_bit(i, &ehci->port_c_suspend) |
| 678 | || (ehci->reset_done[i] && time_after_eq( |
| 679 | jiffies, ehci->reset_done[i]))) { |
| 680 | if (i < 7) |
| 681 | buf [0] |= 1 << (i + 1); |
| 682 | else |
| 683 | buf [1] |= 1 << (i - 7); |
| 684 | status = STS_PCD; |
| 685 | } |
| 686 | } |
| 687 | |
| 688 | /* If a resume is in progress, make sure it can finish */ |
| 689 | if (ehci->resuming_ports) |
| 690 | mod_timer(&hcd->rh_timer, jiffies + msecs_to_jiffies(25)); |
| 691 | |
| 692 | spin_unlock_irqrestore (&ehci->lock, flags); |
| 693 | return status ? retval : 0; |
| 694 | } |
| 695 | |
| 696 | /*-------------------------------------------------------------------------*/ |
| 697 | |
| 698 | static void |
| 699 | ehci_hub_descriptor ( |
| 700 | struct ehci_hcd *ehci, |
| 701 | struct usb_hub_descriptor *desc |
| 702 | ) { |
| 703 | int ports = HCS_N_PORTS (ehci->hcs_params); |
| 704 | u16 temp; |
| 705 | |
| 706 | desc->bDescriptorType = USB_DT_HUB; |
| 707 | desc->bPwrOn2PwrGood = 10; /* ehci 1.0, 2.3.9 says 20ms max */ |
| 708 | desc->bHubContrCurrent = 0; |
| 709 | |
| 710 | desc->bNbrPorts = ports; |
| 711 | temp = 1 + (ports / 8); |
| 712 | desc->bDescLength = 7 + 2 * temp; |
| 713 | |
| 714 | /* two bitmaps: ports removable, and usb 1.0 legacy PortPwrCtrlMask */ |
| 715 | memset(&desc->u.hs.DeviceRemovable[0], 0, temp); |
| 716 | memset(&desc->u.hs.DeviceRemovable[temp], 0xff, temp); |
| 717 | |
| 718 | temp = HUB_CHAR_INDV_PORT_OCPM; /* per-port overcurrent reporting */ |
| 719 | if (HCS_PPC (ehci->hcs_params)) |
| 720 | temp |= HUB_CHAR_INDV_PORT_LPSM; /* per-port power control */ |
| 721 | else |
| 722 | temp |= HUB_CHAR_NO_LPSM; /* no power switching */ |
| 723 | #if 0 |
| 724 | // re-enable when we support USB_PORT_FEAT_INDICATOR below. |
| 725 | if (HCS_INDICATOR (ehci->hcs_params)) |
| 726 | temp |= HUB_CHAR_PORTIND; /* per-port indicators (LEDs) */ |
| 727 | #endif |
| 728 | desc->wHubCharacteristics = cpu_to_le16(temp); |
| 729 | } |
| 730 | |
| 731 | /*-------------------------------------------------------------------------*/ |
| 732 | #ifdef CONFIG_USB_HCD_TEST_MODE |
| 733 | |
| 734 | #define EHSET_TEST_SINGLE_STEP_SET_FEATURE 0x06 |
| 735 | |
| 736 | static void usb_ehset_completion(struct urb *urb) |
| 737 | { |
| 738 | struct completion *done = urb->context; |
| 739 | |
| 740 | complete(done); |
| 741 | } |
| 742 | static int submit_single_step_set_feature( |
| 743 | struct usb_hcd *hcd, |
| 744 | struct urb *urb, |
| 745 | int is_setup |
| 746 | ); |
| 747 | |
| 748 | /* |
| 749 | * Allocate and initialize a control URB. This request will be used by the |
| 750 | * EHSET SINGLE_STEP_SET_FEATURE test in which the DATA and STATUS stages |
| 751 | * of the GetDescriptor request are sent 15 seconds after the SETUP stage. |
| 752 | * Return NULL if failed. |
| 753 | */ |
| 754 | static struct urb *request_single_step_set_feature_urb( |
| 755 | struct usb_device *udev, |
| 756 | void *dr, |
| 757 | void *buf, |
| 758 | struct completion *done |
| 759 | ) { |
| 760 | struct urb *urb; |
| 761 | struct usb_hcd *hcd = bus_to_hcd(udev->bus); |
| 762 | struct usb_host_endpoint *ep; |
| 763 | |
| 764 | urb = usb_alloc_urb(0, GFP_KERNEL); |
| 765 | if (!urb) |
| 766 | return NULL; |
| 767 | |
| 768 | urb->pipe = usb_rcvctrlpipe(udev, 0); |
| 769 | ep = (usb_pipein(urb->pipe) ? udev->ep_in : udev->ep_out) |
| 770 | [usb_pipeendpoint(urb->pipe)]; |
| 771 | if (!ep) { |
| 772 | usb_free_urb(urb); |
| 773 | return NULL; |
| 774 | } |
| 775 | |
| 776 | urb->ep = ep; |
| 777 | urb->dev = udev; |
| 778 | urb->setup_packet = (void *)dr; |
| 779 | urb->transfer_buffer = buf; |
| 780 | urb->transfer_buffer_length = USB_DT_DEVICE_SIZE; |
| 781 | urb->complete = usb_ehset_completion; |
| 782 | urb->status = -EINPROGRESS; |
| 783 | urb->actual_length = 0; |
| 784 | urb->transfer_flags = URB_DIR_IN; |
| 785 | usb_get_urb(urb); |
| 786 | atomic_inc(&urb->use_count); |
| 787 | atomic_inc(&urb->dev->urbnum); |
| 788 | urb->setup_dma = dma_map_single( |
| 789 | hcd->self.sysdev, |
| 790 | urb->setup_packet, |
| 791 | sizeof(struct usb_ctrlrequest), |
| 792 | DMA_TO_DEVICE); |
| 793 | urb->transfer_dma = dma_map_single( |
| 794 | hcd->self.sysdev, |
| 795 | urb->transfer_buffer, |
| 796 | urb->transfer_buffer_length, |
| 797 | DMA_FROM_DEVICE); |
| 798 | urb->context = done; |
| 799 | return urb; |
| 800 | } |
| 801 | |
| 802 | static int ehset_single_step_set_feature(struct usb_hcd *hcd, int port) |
| 803 | { |
| 804 | int retval = -ENOMEM; |
| 805 | struct usb_ctrlrequest *dr; |
| 806 | struct urb *urb; |
| 807 | struct usb_device *udev; |
| 808 | struct ehci_hcd *ehci = hcd_to_ehci(hcd); |
| 809 | struct usb_device_descriptor *buf; |
| 810 | DECLARE_COMPLETION_ONSTACK(done); |
| 811 | |
| 812 | /* Obtain udev of the rhub's child port */ |
| 813 | udev = usb_hub_find_child(hcd->self.root_hub, port); |
| 814 | if (!udev) { |
| 815 | ehci_err(ehci, "No device attached to the RootHub\n"); |
| 816 | return -ENODEV; |
| 817 | } |
| 818 | buf = kmalloc(USB_DT_DEVICE_SIZE, GFP_KERNEL); |
| 819 | if (!buf) |
| 820 | return -ENOMEM; |
| 821 | |
| 822 | dr = kmalloc(sizeof(struct usb_ctrlrequest), GFP_KERNEL); |
| 823 | if (!dr) { |
| 824 | kfree(buf); |
| 825 | return -ENOMEM; |
| 826 | } |
| 827 | |
| 828 | /* Fill Setup packet for GetDescriptor */ |
| 829 | dr->bRequestType = USB_DIR_IN; |
| 830 | dr->bRequest = USB_REQ_GET_DESCRIPTOR; |
| 831 | dr->wValue = cpu_to_le16(USB_DT_DEVICE << 8); |
| 832 | dr->wIndex = 0; |
| 833 | dr->wLength = cpu_to_le16(USB_DT_DEVICE_SIZE); |
| 834 | urb = request_single_step_set_feature_urb(udev, dr, buf, &done); |
| 835 | if (!urb) |
| 836 | goto cleanup; |
| 837 | |
| 838 | /* Submit just the SETUP stage */ |
| 839 | retval = submit_single_step_set_feature(hcd, urb, 1); |
| 840 | if (retval) |
| 841 | goto out1; |
| 842 | if (!wait_for_completion_timeout(&done, msecs_to_jiffies(2000))) { |
| 843 | usb_kill_urb(urb); |
| 844 | retval = -ETIMEDOUT; |
| 845 | ehci_err(ehci, "%s SETUP stage timed out on ep0\n", __func__); |
| 846 | goto out1; |
| 847 | } |
| 848 | msleep(15 * 1000); |
| 849 | |
| 850 | /* Complete remaining DATA and STATUS stages using the same URB */ |
| 851 | urb->status = -EINPROGRESS; |
| 852 | usb_get_urb(urb); |
| 853 | atomic_inc(&urb->use_count); |
| 854 | atomic_inc(&urb->dev->urbnum); |
| 855 | retval = submit_single_step_set_feature(hcd, urb, 0); |
| 856 | if (!retval && !wait_for_completion_timeout(&done, |
| 857 | msecs_to_jiffies(2000))) { |
| 858 | usb_kill_urb(urb); |
| 859 | retval = -ETIMEDOUT; |
| 860 | ehci_err(ehci, "%s IN stage timed out on ep0\n", __func__); |
| 861 | } |
| 862 | out1: |
| 863 | usb_free_urb(urb); |
| 864 | cleanup: |
| 865 | kfree(dr); |
| 866 | kfree(buf); |
| 867 | return retval; |
| 868 | } |
| 869 | #endif /* CONFIG_USB_HCD_TEST_MODE */ |
| 870 | /*-------------------------------------------------------------------------*/ |
| 871 | |
| 872 | int ehci_hub_control( |
| 873 | struct usb_hcd *hcd, |
| 874 | u16 typeReq, |
| 875 | u16 wValue, |
| 876 | u16 wIndex, |
| 877 | char *buf, |
| 878 | u16 wLength |
| 879 | ) { |
| 880 | struct ehci_hcd *ehci = hcd_to_ehci (hcd); |
| 881 | int ports = HCS_N_PORTS (ehci->hcs_params); |
| 882 | u32 __iomem *status_reg, *hostpc_reg; |
| 883 | u32 temp, temp1, status; |
| 884 | unsigned long flags; |
| 885 | int retval = 0; |
| 886 | unsigned selector; |
| 887 | |
| 888 | /* |
| 889 | * Avoid underflow while calculating (wIndex & 0xff) - 1. |
| 890 | * The compiler might deduce that wIndex can never be 0 and then |
| 891 | * optimize away the tests for !wIndex below. |
| 892 | */ |
| 893 | temp = wIndex & 0xff; |
| 894 | temp -= (temp > 0); |
| 895 | status_reg = &ehci->regs->port_status[temp]; |
| 896 | hostpc_reg = &ehci->regs->hostpc[temp]; |
| 897 | |
| 898 | /* |
| 899 | * FIXME: support SetPortFeatures USB_PORT_FEAT_INDICATOR. |
| 900 | * HCS_INDICATOR may say we can change LEDs to off/amber/green. |
| 901 | * (track current state ourselves) ... blink for diagnostics, |
| 902 | * power, "this is the one", etc. EHCI spec supports this. |
| 903 | */ |
| 904 | |
| 905 | spin_lock_irqsave (&ehci->lock, flags); |
| 906 | switch (typeReq) { |
| 907 | case ClearHubFeature: |
| 908 | switch (wValue) { |
| 909 | case C_HUB_LOCAL_POWER: |
| 910 | case C_HUB_OVER_CURRENT: |
| 911 | /* no hub-wide feature/status flags */ |
| 912 | break; |
| 913 | default: |
| 914 | goto error; |
| 915 | } |
| 916 | break; |
| 917 | case ClearPortFeature: |
| 918 | if (!wIndex || wIndex > ports) |
| 919 | goto error; |
| 920 | wIndex--; |
| 921 | temp = ehci_readl(ehci, status_reg); |
| 922 | temp &= ~PORT_RWC_BITS; |
| 923 | |
| 924 | /* |
| 925 | * Even if OWNER is set, so the port is owned by the |
| 926 | * companion controller, hub_wq needs to be able to clear |
| 927 | * the port-change status bits (especially |
| 928 | * USB_PORT_STAT_C_CONNECTION). |
| 929 | */ |
| 930 | |
| 931 | switch (wValue) { |
| 932 | case USB_PORT_FEAT_ENABLE: |
| 933 | ehci_writel(ehci, temp & ~PORT_PE, status_reg); |
| 934 | break; |
| 935 | case USB_PORT_FEAT_C_ENABLE: |
| 936 | ehci_writel(ehci, temp | PORT_PEC, status_reg); |
| 937 | break; |
| 938 | case USB_PORT_FEAT_SUSPEND: |
| 939 | if (temp & PORT_RESET) |
| 940 | goto error; |
| 941 | if (ehci->no_selective_suspend) |
| 942 | break; |
| 943 | #ifdef CONFIG_USB_OTG |
| 944 | if ((hcd->self.otg_port == (wIndex + 1)) |
| 945 | && hcd->self.b_hnp_enable) { |
| 946 | otg_start_hnp(hcd->usb_phy->otg); |
| 947 | break; |
| 948 | } |
| 949 | #endif |
| 950 | if (!(temp & PORT_SUSPEND)) |
| 951 | break; |
| 952 | if ((temp & PORT_PE) == 0) |
| 953 | goto error; |
| 954 | |
| 955 | /* clear phy low-power mode before resume */ |
| 956 | if (ehci->has_tdi_phy_lpm) { |
| 957 | temp1 = ehci_readl(ehci, hostpc_reg); |
| 958 | ehci_writel(ehci, temp1 & ~HOSTPC_PHCD, |
| 959 | hostpc_reg); |
| 960 | spin_unlock_irqrestore(&ehci->lock, flags); |
| 961 | msleep(5);/* wait to leave low-power mode */ |
| 962 | spin_lock_irqsave(&ehci->lock, flags); |
| 963 | } |
| 964 | /* resume signaling for 20 msec */ |
| 965 | temp &= ~PORT_WAKE_BITS; |
| 966 | ehci_writel(ehci, temp | PORT_RESUME, status_reg); |
| 967 | ehci->reset_done[wIndex] = jiffies |
| 968 | + msecs_to_jiffies(USB_RESUME_TIMEOUT); |
| 969 | set_bit(wIndex, &ehci->resuming_ports); |
| 970 | usb_hcd_start_port_resume(&hcd->self, wIndex); |
| 971 | break; |
| 972 | case USB_PORT_FEAT_C_SUSPEND: |
| 973 | clear_bit(wIndex, &ehci->port_c_suspend); |
| 974 | break; |
| 975 | case USB_PORT_FEAT_POWER: |
| 976 | if (HCS_PPC(ehci->hcs_params)) { |
| 977 | spin_unlock_irqrestore(&ehci->lock, flags); |
| 978 | ehci_port_power(ehci, wIndex, false); |
| 979 | spin_lock_irqsave(&ehci->lock, flags); |
| 980 | } |
| 981 | break; |
| 982 | case USB_PORT_FEAT_C_CONNECTION: |
| 983 | ehci_writel(ehci, temp | PORT_CSC, status_reg); |
| 984 | break; |
| 985 | case USB_PORT_FEAT_C_OVER_CURRENT: |
| 986 | ehci_writel(ehci, temp | PORT_OCC, status_reg); |
| 987 | break; |
| 988 | case USB_PORT_FEAT_C_RESET: |
| 989 | /* GetPortStatus clears reset */ |
| 990 | break; |
| 991 | default: |
| 992 | goto error; |
| 993 | } |
| 994 | ehci_readl(ehci, &ehci->regs->command); /* unblock posted write */ |
| 995 | break; |
| 996 | case GetHubDescriptor: |
| 997 | ehci_hub_descriptor (ehci, (struct usb_hub_descriptor *) |
| 998 | buf); |
| 999 | break; |
| 1000 | case GetHubStatus: |
| 1001 | /* no hub-wide feature/status flags */ |
| 1002 | memset (buf, 0, 4); |
| 1003 | //cpu_to_le32s ((u32 *) buf); |
| 1004 | break; |
| 1005 | case GetPortStatus: |
| 1006 | if (!wIndex || wIndex > ports) |
| 1007 | goto error; |
| 1008 | wIndex--; |
| 1009 | status = 0; |
| 1010 | temp = ehci_readl(ehci, status_reg); |
| 1011 | |
| 1012 | // wPortChange bits |
| 1013 | if (temp & PORT_CSC) |
| 1014 | status |= USB_PORT_STAT_C_CONNECTION << 16; |
| 1015 | if (temp & PORT_PEC) |
| 1016 | status |= USB_PORT_STAT_C_ENABLE << 16; |
| 1017 | |
| 1018 | if ((temp & PORT_OCC) && !ignore_oc){ |
| 1019 | status |= USB_PORT_STAT_C_OVERCURRENT << 16; |
| 1020 | |
| 1021 | /* |
| 1022 | * Hubs should disable port power on over-current. |
| 1023 | * However, not all EHCI implementations do this |
| 1024 | * automatically, even if they _do_ support per-port |
| 1025 | * power switching; they're allowed to just limit the |
| 1026 | * current. hub_wq will turn the power back on. |
| 1027 | */ |
| 1028 | if (((temp & PORT_OC) || (ehci->need_oc_pp_cycle)) |
| 1029 | && HCS_PPC(ehci->hcs_params)) { |
| 1030 | spin_unlock_irqrestore(&ehci->lock, flags); |
| 1031 | ehci_port_power(ehci, wIndex, false); |
| 1032 | spin_lock_irqsave(&ehci->lock, flags); |
| 1033 | temp = ehci_readl(ehci, status_reg); |
| 1034 | } |
| 1035 | } |
| 1036 | |
| 1037 | /* no reset or resume pending */ |
| 1038 | if (!ehci->reset_done[wIndex]) { |
| 1039 | |
| 1040 | /* Remote Wakeup received? */ |
| 1041 | if (temp & PORT_RESUME) { |
| 1042 | /* resume signaling for 20 msec */ |
| 1043 | ehci->reset_done[wIndex] = jiffies |
| 1044 | + msecs_to_jiffies(20); |
| 1045 | usb_hcd_start_port_resume(&hcd->self, wIndex); |
| 1046 | set_bit(wIndex, &ehci->resuming_ports); |
| 1047 | /* check the port again */ |
| 1048 | mod_timer(&ehci_to_hcd(ehci)->rh_timer, |
| 1049 | ehci->reset_done[wIndex]); |
| 1050 | } |
| 1051 | |
| 1052 | /* reset or resume not yet complete */ |
| 1053 | } else if (!time_after_eq(jiffies, ehci->reset_done[wIndex])) { |
| 1054 | ; /* wait until it is complete */ |
| 1055 | |
| 1056 | /* resume completed */ |
| 1057 | } else if (test_bit(wIndex, &ehci->resuming_ports)) { |
| 1058 | clear_bit(wIndex, &ehci->suspended_ports); |
| 1059 | set_bit(wIndex, &ehci->port_c_suspend); |
| 1060 | ehci->reset_done[wIndex] = 0; |
| 1061 | usb_hcd_end_port_resume(&hcd->self, wIndex); |
| 1062 | |
| 1063 | /* stop resume signaling */ |
| 1064 | temp &= ~(PORT_RWC_BITS | PORT_SUSPEND | PORT_RESUME); |
| 1065 | ehci_writel(ehci, temp, status_reg); |
| 1066 | clear_bit(wIndex, &ehci->resuming_ports); |
| 1067 | retval = ehci_handshake(ehci, status_reg, |
| 1068 | PORT_RESUME, 0, 2000 /* 2msec */); |
| 1069 | if (retval != 0) { |
| 1070 | ehci_err(ehci, "port %d resume error %d\n", |
| 1071 | wIndex + 1, retval); |
| 1072 | goto error; |
| 1073 | } |
| 1074 | temp = ehci_readl(ehci, status_reg); |
| 1075 | |
| 1076 | /* whoever resets must GetPortStatus to complete it!! */ |
| 1077 | } else { |
| 1078 | status |= USB_PORT_STAT_C_RESET << 16; |
| 1079 | ehci->reset_done [wIndex] = 0; |
| 1080 | |
| 1081 | /* force reset to complete */ |
| 1082 | ehci_writel(ehci, temp & ~(PORT_RWC_BITS | PORT_RESET), |
| 1083 | status_reg); |
| 1084 | /* REVISIT: some hardware needs 550+ usec to clear |
| 1085 | * this bit; seems too long to spin routinely... |
| 1086 | */ |
| 1087 | retval = ehci_handshake(ehci, status_reg, |
| 1088 | PORT_RESET, 0, 1000); |
| 1089 | if (retval != 0) { |
| 1090 | ehci_err (ehci, "port %d reset error %d\n", |
| 1091 | wIndex + 1, retval); |
| 1092 | goto error; |
| 1093 | } |
| 1094 | |
| 1095 | /* see what we found out */ |
| 1096 | temp = check_reset_complete (ehci, wIndex, status_reg, |
| 1097 | ehci_readl(ehci, status_reg)); |
| 1098 | } |
| 1099 | |
| 1100 | /* transfer dedicated ports to the companion hc */ |
| 1101 | if ((temp & PORT_CONNECT) && |
| 1102 | test_bit(wIndex, &ehci->companion_ports)) { |
| 1103 | temp &= ~PORT_RWC_BITS; |
| 1104 | temp |= PORT_OWNER; |
| 1105 | ehci_writel(ehci, temp, status_reg); |
| 1106 | ehci_dbg(ehci, "port %d --> companion\n", wIndex + 1); |
| 1107 | temp = ehci_readl(ehci, status_reg); |
| 1108 | } |
| 1109 | |
| 1110 | /* |
| 1111 | * Even if OWNER is set, there's no harm letting hub_wq |
| 1112 | * see the wPortStatus values (they should all be 0 except |
| 1113 | * for PORT_POWER anyway). |
| 1114 | */ |
| 1115 | |
| 1116 | if (temp & PORT_CONNECT) { |
| 1117 | status |= USB_PORT_STAT_CONNECTION; |
| 1118 | // status may be from integrated TT |
| 1119 | if (ehci->has_hostpc) { |
| 1120 | temp1 = ehci_readl(ehci, hostpc_reg); |
| 1121 | status |= ehci_port_speed(ehci, temp1); |
| 1122 | } else |
| 1123 | status |= ehci_port_speed(ehci, temp); |
| 1124 | } |
| 1125 | if (temp & PORT_PE) |
| 1126 | status |= USB_PORT_STAT_ENABLE; |
| 1127 | |
| 1128 | /* maybe the port was unsuspended without our knowledge */ |
| 1129 | if (temp & (PORT_SUSPEND|PORT_RESUME)) { |
| 1130 | status |= USB_PORT_STAT_SUSPEND; |
| 1131 | } else if (test_bit(wIndex, &ehci->suspended_ports)) { |
| 1132 | clear_bit(wIndex, &ehci->suspended_ports); |
| 1133 | clear_bit(wIndex, &ehci->resuming_ports); |
| 1134 | ehci->reset_done[wIndex] = 0; |
| 1135 | if (temp & PORT_PE) |
| 1136 | set_bit(wIndex, &ehci->port_c_suspend); |
| 1137 | usb_hcd_end_port_resume(&hcd->self, wIndex); |
| 1138 | } |
| 1139 | |
| 1140 | if (temp & PORT_OC) |
| 1141 | status |= USB_PORT_STAT_OVERCURRENT; |
| 1142 | if (temp & PORT_RESET) |
| 1143 | status |= USB_PORT_STAT_RESET; |
| 1144 | if (temp & PORT_POWER) |
| 1145 | status |= USB_PORT_STAT_POWER; |
| 1146 | if (test_bit(wIndex, &ehci->port_c_suspend)) |
| 1147 | status |= USB_PORT_STAT_C_SUSPEND << 16; |
| 1148 | |
| 1149 | if (status & ~0xffff) /* only if wPortChange is interesting */ |
| 1150 | dbg_port(ehci, "GetStatus", wIndex + 1, temp); |
| 1151 | put_unaligned_le32(status, buf); |
| 1152 | break; |
| 1153 | case SetHubFeature: |
| 1154 | switch (wValue) { |
| 1155 | case C_HUB_LOCAL_POWER: |
| 1156 | case C_HUB_OVER_CURRENT: |
| 1157 | /* no hub-wide feature/status flags */ |
| 1158 | break; |
| 1159 | default: |
| 1160 | goto error; |
| 1161 | } |
| 1162 | break; |
| 1163 | case SetPortFeature: |
| 1164 | selector = wIndex >> 8; |
| 1165 | wIndex &= 0xff; |
| 1166 | if (unlikely(ehci->debug)) { |
| 1167 | /* If the debug port is active any port |
| 1168 | * feature requests should get denied */ |
| 1169 | if (wIndex == HCS_DEBUG_PORT(ehci->hcs_params) && |
| 1170 | (readl(&ehci->debug->control) & DBGP_ENABLED)) { |
| 1171 | retval = -ENODEV; |
| 1172 | goto error_exit; |
| 1173 | } |
| 1174 | } |
| 1175 | if (!wIndex || wIndex > ports) |
| 1176 | goto error; |
| 1177 | wIndex--; |
| 1178 | temp = ehci_readl(ehci, status_reg); |
| 1179 | if (temp & PORT_OWNER) |
| 1180 | break; |
| 1181 | |
| 1182 | temp &= ~PORT_RWC_BITS; |
| 1183 | switch (wValue) { |
| 1184 | case USB_PORT_FEAT_SUSPEND: |
| 1185 | if (ehci->no_selective_suspend) |
| 1186 | break; |
| 1187 | if ((temp & PORT_PE) == 0 |
| 1188 | || (temp & PORT_RESET) != 0) |
| 1189 | goto error; |
| 1190 | |
| 1191 | /* After above check the port must be connected. |
| 1192 | * Set appropriate bit thus could put phy into low power |
| 1193 | * mode if we have tdi_phy_lpm feature |
| 1194 | */ |
| 1195 | temp &= ~PORT_WKCONN_E; |
| 1196 | temp |= PORT_WKDISC_E | PORT_WKOC_E; |
| 1197 | ehci_writel(ehci, temp | PORT_SUSPEND, status_reg); |
| 1198 | if (ehci->has_tdi_phy_lpm) { |
| 1199 | spin_unlock_irqrestore(&ehci->lock, flags); |
| 1200 | msleep(5);/* 5ms for HCD enter low pwr mode */ |
| 1201 | spin_lock_irqsave(&ehci->lock, flags); |
| 1202 | temp1 = ehci_readl(ehci, hostpc_reg); |
| 1203 | ehci_writel(ehci, temp1 | HOSTPC_PHCD, |
| 1204 | hostpc_reg); |
| 1205 | temp1 = ehci_readl(ehci, hostpc_reg); |
| 1206 | ehci_dbg(ehci, "Port%d phy low pwr mode %s\n", |
| 1207 | wIndex, (temp1 & HOSTPC_PHCD) ? |
| 1208 | "succeeded" : "failed"); |
| 1209 | } |
| 1210 | if (ehci_has_fsl_susp_errata(ehci)) { |
| 1211 | /* 10ms for HCD enter suspend */ |
| 1212 | spin_unlock_irqrestore(&ehci->lock, flags); |
| 1213 | usleep_range(10000, 20000); |
| 1214 | spin_lock_irqsave(&ehci->lock, flags); |
| 1215 | } |
| 1216 | set_bit(wIndex, &ehci->suspended_ports); |
| 1217 | break; |
| 1218 | case USB_PORT_FEAT_POWER: |
| 1219 | if (HCS_PPC(ehci->hcs_params)) { |
| 1220 | spin_unlock_irqrestore(&ehci->lock, flags); |
| 1221 | ehci_port_power(ehci, wIndex, true); |
| 1222 | spin_lock_irqsave(&ehci->lock, flags); |
| 1223 | } |
| 1224 | break; |
| 1225 | case USB_PORT_FEAT_RESET: |
| 1226 | if (temp & (PORT_SUSPEND|PORT_RESUME)) |
| 1227 | goto error; |
| 1228 | /* line status bits may report this as low speed, |
| 1229 | * which can be fine if this root hub has a |
| 1230 | * transaction translator built in. |
| 1231 | */ |
| 1232 | if ((temp & (PORT_PE|PORT_CONNECT)) == PORT_CONNECT |
| 1233 | && !ehci_is_TDI(ehci) |
| 1234 | && PORT_USB11 (temp)) { |
| 1235 | ehci_dbg (ehci, |
| 1236 | "port %d low speed --> companion\n", |
| 1237 | wIndex + 1); |
| 1238 | temp |= PORT_OWNER; |
| 1239 | } else { |
| 1240 | temp |= PORT_RESET; |
| 1241 | temp &= ~PORT_PE; |
| 1242 | |
| 1243 | /* |
| 1244 | * caller must wait, then call GetPortStatus |
| 1245 | * usb 2.0 spec says 50 ms resets on root |
| 1246 | */ |
| 1247 | ehci->reset_done [wIndex] = jiffies |
| 1248 | + msecs_to_jiffies (50); |
| 1249 | |
| 1250 | /* |
| 1251 | * Force full-speed connect for FSL high-speed |
| 1252 | * erratum; disable HS Chirp by setting PFSC bit |
| 1253 | */ |
| 1254 | if (ehci_has_fsl_hs_errata(ehci)) |
| 1255 | temp |= (1 << PORTSC_FSL_PFSC); |
| 1256 | } |
| 1257 | ehci_writel(ehci, temp, status_reg); |
| 1258 | break; |
| 1259 | |
| 1260 | /* For downstream facing ports (these): one hub port is put |
| 1261 | * into test mode according to USB2 11.24.2.13, then the hub |
| 1262 | * must be reset (which for root hub now means rmmod+modprobe, |
| 1263 | * or else system reboot). See EHCI 2.3.9 and 4.14 for info |
| 1264 | * about the EHCI-specific stuff. |
| 1265 | */ |
| 1266 | case USB_PORT_FEAT_TEST: |
| 1267 | #ifdef CONFIG_USB_HCD_TEST_MODE |
| 1268 | if (selector == EHSET_TEST_SINGLE_STEP_SET_FEATURE) { |
| 1269 | spin_unlock_irqrestore(&ehci->lock, flags); |
| 1270 | retval = ehset_single_step_set_feature(hcd, |
| 1271 | wIndex + 1); |
| 1272 | spin_lock_irqsave(&ehci->lock, flags); |
| 1273 | break; |
| 1274 | } |
| 1275 | #endif |
| 1276 | if (!selector || selector > 5) |
| 1277 | goto error; |
| 1278 | spin_unlock_irqrestore(&ehci->lock, flags); |
| 1279 | ehci_quiesce(ehci); |
| 1280 | spin_lock_irqsave(&ehci->lock, flags); |
| 1281 | |
| 1282 | /* Put all enabled ports into suspend */ |
| 1283 | while (ports--) { |
| 1284 | u32 __iomem *sreg = |
| 1285 | &ehci->regs->port_status[ports]; |
| 1286 | |
| 1287 | temp = ehci_readl(ehci, sreg) & ~PORT_RWC_BITS; |
| 1288 | if (temp & PORT_PE) |
| 1289 | ehci_writel(ehci, temp | PORT_SUSPEND, |
| 1290 | sreg); |
| 1291 | } |
| 1292 | |
| 1293 | spin_unlock_irqrestore(&ehci->lock, flags); |
| 1294 | ehci_halt(ehci); |
| 1295 | spin_lock_irqsave(&ehci->lock, flags); |
| 1296 | |
| 1297 | temp = ehci_readl(ehci, status_reg); |
| 1298 | temp |= selector << 16; |
| 1299 | ehci_writel(ehci, temp, status_reg); |
| 1300 | break; |
| 1301 | |
| 1302 | default: |
| 1303 | goto error; |
| 1304 | } |
| 1305 | ehci_readl(ehci, &ehci->regs->command); /* unblock posted writes */ |
| 1306 | break; |
| 1307 | |
| 1308 | default: |
| 1309 | error: |
| 1310 | /* "stall" on error */ |
| 1311 | retval = -EPIPE; |
| 1312 | } |
| 1313 | error_exit: |
| 1314 | spin_unlock_irqrestore (&ehci->lock, flags); |
| 1315 | return retval; |
| 1316 | } |
| 1317 | EXPORT_SYMBOL_GPL(ehci_hub_control); |
| 1318 | |
| 1319 | static void ehci_relinquish_port(struct usb_hcd *hcd, int portnum) |
| 1320 | { |
| 1321 | struct ehci_hcd *ehci = hcd_to_ehci(hcd); |
| 1322 | |
| 1323 | if (ehci_is_TDI(ehci)) |
| 1324 | return; |
| 1325 | set_owner(ehci, --portnum, PORT_OWNER); |
| 1326 | } |
| 1327 | |
| 1328 | static int ehci_port_handed_over(struct usb_hcd *hcd, int portnum) |
| 1329 | { |
| 1330 | struct ehci_hcd *ehci = hcd_to_ehci(hcd); |
| 1331 | u32 __iomem *reg; |
| 1332 | |
| 1333 | if (ehci_is_TDI(ehci)) |
| 1334 | return 0; |
| 1335 | reg = &ehci->regs->port_status[portnum - 1]; |
| 1336 | return ehci_readl(ehci, reg) & PORT_OWNER; |
| 1337 | } |
| 1338 | |
| 1339 | static int ehci_port_power(struct ehci_hcd *ehci, int portnum, bool enable) |
| 1340 | { |
| 1341 | struct usb_hcd *hcd = ehci_to_hcd(ehci); |
| 1342 | u32 __iomem *status_reg = &ehci->regs->port_status[portnum]; |
| 1343 | u32 temp = ehci_readl(ehci, status_reg) & ~PORT_RWC_BITS; |
| 1344 | |
| 1345 | if (enable) |
| 1346 | ehci_writel(ehci, temp | PORT_POWER, status_reg); |
| 1347 | else |
| 1348 | ehci_writel(ehci, temp & ~PORT_POWER, status_reg); |
| 1349 | |
| 1350 | if (hcd->driver->port_power) |
| 1351 | hcd->driver->port_power(hcd, portnum, enable); |
| 1352 | |
| 1353 | return 0; |
| 1354 | } |