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 #include <linux/usb/otg.h> 31 32 #define PORT_WAKE_BITS (PORT_WKOC_E|PORT_WKDISC_E|PORT_WKCONN_E) 33 34 #ifdef CONFIG_PM 35 36 static int ehci_hub_control( 37 struct usb_hcd *hcd, 38 u16 typeReq, 39 u16 wValue, 40 u16 wIndex, 41 char *buf, 42 u16 wLength 43 ); 44 45 /* After a power loss, ports that were owned by the companion must be 46 * reset so that the companion can still own them. 47 */ 48 static void ehci_handover_companion_ports(struct ehci_hcd *ehci) 49 { 50 u32 __iomem *reg; 51 u32 status; 52 int port; 53 __le32 buf; 54 struct usb_hcd *hcd = ehci_to_hcd(ehci); 55 56 if (!ehci->owned_ports) 57 return; 58 59 /* Give the connections some time to appear */ 60 msleep(20); 61 62 spin_lock_irq(&ehci->lock); 63 port = HCS_N_PORTS(ehci->hcs_params); 64 while (port--) { 65 if (test_bit(port, &ehci->owned_ports)) { 66 reg = &ehci->regs->port_status[port]; 67 status = ehci_readl(ehci, reg) & ~PORT_RWC_BITS; 68 69 /* Port already owned by companion? */ 70 if (status & PORT_OWNER) 71 clear_bit(port, &ehci->owned_ports); 72 else if (test_bit(port, &ehci->companion_ports)) 73 ehci_writel(ehci, status & ~PORT_PE, reg); 74 else { 75 spin_unlock_irq(&ehci->lock); 76 ehci_hub_control(hcd, SetPortFeature, 77 USB_PORT_FEAT_RESET, port + 1, 78 NULL, 0); 79 spin_lock_irq(&ehci->lock); 80 } 81 } 82 } 83 spin_unlock_irq(&ehci->lock); 84 85 if (!ehci->owned_ports) 86 return; 87 msleep(90); /* Wait for resets to complete */ 88 89 spin_lock_irq(&ehci->lock); 90 port = HCS_N_PORTS(ehci->hcs_params); 91 while (port--) { 92 if (test_bit(port, &ehci->owned_ports)) { 93 spin_unlock_irq(&ehci->lock); 94 ehci_hub_control(hcd, GetPortStatus, 95 0, port + 1, 96 (char *) &buf, sizeof(buf)); 97 spin_lock_irq(&ehci->lock); 98 99 /* The companion should now own the port, 100 * but if something went wrong the port must not 101 * remain enabled. 102 */ 103 reg = &ehci->regs->port_status[port]; 104 status = ehci_readl(ehci, reg) & ~PORT_RWC_BITS; 105 if (status & PORT_OWNER) 106 ehci_writel(ehci, status | PORT_CSC, reg); 107 else { 108 ehci_dbg(ehci, "failed handover port %d: %x\n", 109 port + 1, status); 110 ehci_writel(ehci, status & ~PORT_PE, reg); 111 } 112 } 113 } 114 115 ehci->owned_ports = 0; 116 spin_unlock_irq(&ehci->lock); 117 } 118 119 static int ehci_port_change(struct ehci_hcd *ehci) 120 { 121 int i = HCS_N_PORTS(ehci->hcs_params); 122 123 /* First check if the controller indicates a change event */ 124 125 if (ehci_readl(ehci, &ehci->regs->status) & STS_PCD) 126 return 1; 127 128 /* 129 * Not all controllers appear to update this while going from D3 to D0, 130 * so check the individual port status registers as well 131 */ 132 133 while (i--) 134 if (ehci_readl(ehci, &ehci->regs->port_status[i]) & PORT_CSC) 135 return 1; 136 137 return 0; 138 } 139 140 static void ehci_adjust_port_wakeup_flags(struct ehci_hcd *ehci, 141 bool suspending, bool do_wakeup) 142 { 143 int port; 144 u32 temp; 145 146 /* If remote wakeup is enabled for the root hub but disabled 147 * for the controller, we must adjust all the port wakeup flags 148 * when the controller is suspended or resumed. In all other 149 * cases they don't need to be changed. 150 */ 151 if (!ehci_to_hcd(ehci)->self.root_hub->do_remote_wakeup || do_wakeup) 152 return; 153 154 spin_lock_irq(&ehci->lock); 155 156 /* clear phy low-power mode before changing wakeup flags */ 157 if (ehci->has_hostpc) { 158 port = HCS_N_PORTS(ehci->hcs_params); 159 while (port--) { 160 u32 __iomem *hostpc_reg = &ehci->regs->hostpc[port]; 161 162 temp = ehci_readl(ehci, hostpc_reg); 163 ehci_writel(ehci, temp & ~HOSTPC_PHCD, hostpc_reg); 164 } 165 spin_unlock_irq(&ehci->lock); 166 msleep(5); 167 spin_lock_irq(&ehci->lock); 168 } 169 170 port = HCS_N_PORTS(ehci->hcs_params); 171 while (port--) { 172 u32 __iomem *reg = &ehci->regs->port_status[port]; 173 u32 t1 = ehci_readl(ehci, reg) & ~PORT_RWC_BITS; 174 u32 t2 = t1 & ~PORT_WAKE_BITS; 175 176 /* If we are suspending the controller, clear the flags. 177 * If we are resuming the controller, set the wakeup flags. 178 */ 179 if (!suspending) { 180 if (t1 & PORT_CONNECT) 181 t2 |= PORT_WKOC_E | PORT_WKDISC_E; 182 else 183 t2 |= PORT_WKOC_E | PORT_WKCONN_E; 184 } 185 ehci_vdbg(ehci, "port %d, %08x -> %08x\n", 186 port + 1, t1, t2); 187 ehci_writel(ehci, t2, reg); 188 } 189 190 /* enter phy low-power mode again */ 191 if (ehci->has_hostpc) { 192 port = HCS_N_PORTS(ehci->hcs_params); 193 while (port--) { 194 u32 __iomem *hostpc_reg = &ehci->regs->hostpc[port]; 195 196 temp = ehci_readl(ehci, hostpc_reg); 197 ehci_writel(ehci, temp | HOSTPC_PHCD, hostpc_reg); 198 } 199 } 200 201 /* Does the root hub have a port wakeup pending? */ 202 if (!suspending && ehci_port_change(ehci)) 203 usb_hcd_resume_root_hub(ehci_to_hcd(ehci)); 204 205 spin_unlock_irq(&ehci->lock); 206 } 207 208 static int ehci_bus_suspend (struct usb_hcd *hcd) 209 { 210 struct ehci_hcd *ehci = hcd_to_ehci (hcd); 211 int port; 212 int mask; 213 int changed; 214 215 ehci_dbg(ehci, "suspend root hub\n"); 216 217 if (time_before (jiffies, ehci->next_statechange)) 218 msleep(5); 219 220 /* stop the schedules */ 221 ehci_quiesce(ehci); 222 223 spin_lock_irq (&ehci->lock); 224 if (ehci->rh_state < EHCI_RH_RUNNING) 225 goto done; 226 227 /* Once the controller is stopped, port resumes that are already 228 * in progress won't complete. Hence if remote wakeup is enabled 229 * for the root hub and any ports are in the middle of a resume or 230 * remote wakeup, we must fail the suspend. 231 */ 232 if (hcd->self.root_hub->do_remote_wakeup) { 233 if (ehci->resuming_ports) { 234 spin_unlock_irq(&ehci->lock); 235 ehci_dbg(ehci, "suspend failed because a port is resuming\n"); 236 return -EBUSY; 237 } 238 } 239 240 /* Unlike other USB host controller types, EHCI doesn't have 241 * any notion of "global" or bus-wide suspend. The driver has 242 * to manually suspend all the active unsuspended ports, and 243 * then manually resume them in the bus_resume() routine. 244 */ 245 ehci->bus_suspended = 0; 246 ehci->owned_ports = 0; 247 changed = 0; 248 port = HCS_N_PORTS(ehci->hcs_params); 249 while (port--) { 250 u32 __iomem *reg = &ehci->regs->port_status [port]; 251 u32 t1 = ehci_readl(ehci, reg) & ~PORT_RWC_BITS; 252 u32 t2 = t1 & ~PORT_WAKE_BITS; 253 254 /* keep track of which ports we suspend */ 255 if (t1 & PORT_OWNER) 256 set_bit(port, &ehci->owned_ports); 257 else if ((t1 & PORT_PE) && !(t1 & PORT_SUSPEND)) { 258 t2 |= PORT_SUSPEND; 259 set_bit(port, &ehci->bus_suspended); 260 } 261 262 /* enable remote wakeup on all ports, if told to do so */ 263 if (hcd->self.root_hub->do_remote_wakeup) { 264 /* only enable appropriate wake bits, otherwise the 265 * hardware can not go phy low power mode. If a race 266 * condition happens here(connection change during bits 267 * set), the port change detection will finally fix it. 268 */ 269 if (t1 & PORT_CONNECT) 270 t2 |= PORT_WKOC_E | PORT_WKDISC_E; 271 else 272 t2 |= PORT_WKOC_E | PORT_WKCONN_E; 273 } 274 275 if (t1 != t2) { 276 ehci_vdbg (ehci, "port %d, %08x -> %08x\n", 277 port + 1, t1, t2); 278 ehci_writel(ehci, t2, reg); 279 changed = 1; 280 } 281 } 282 283 if (changed && ehci->has_hostpc) { 284 spin_unlock_irq(&ehci->lock); 285 msleep(5); /* 5 ms for HCD to enter low-power mode */ 286 spin_lock_irq(&ehci->lock); 287 288 port = HCS_N_PORTS(ehci->hcs_params); 289 while (port--) { 290 u32 __iomem *hostpc_reg = &ehci->regs->hostpc[port]; 291 u32 t3; 292 293 t3 = ehci_readl(ehci, hostpc_reg); 294 ehci_writel(ehci, t3 | HOSTPC_PHCD, hostpc_reg); 295 t3 = ehci_readl(ehci, hostpc_reg); 296 ehci_dbg(ehci, "Port %d phy low-power mode %s\n", 297 port, (t3 & HOSTPC_PHCD) ? 298 "succeeded" : "failed"); 299 } 300 } 301 spin_unlock_irq(&ehci->lock); 302 303 /* Apparently some devices need a >= 1-uframe delay here */ 304 if (ehci->bus_suspended) 305 udelay(150); 306 307 /* turn off now-idle HC */ 308 ehci_halt (ehci); 309 310 spin_lock_irq(&ehci->lock); 311 if (ehci->enabled_hrtimer_events & BIT(EHCI_HRTIMER_POLL_DEAD)) 312 ehci_handle_controller_death(ehci); 313 if (ehci->rh_state != EHCI_RH_RUNNING) 314 goto done; 315 ehci->rh_state = EHCI_RH_SUSPENDED; 316 317 end_unlink_async(ehci); 318 unlink_empty_async(ehci); 319 ehci_handle_intr_unlinks(ehci); 320 end_free_itds(ehci); 321 322 /* allow remote wakeup */ 323 mask = INTR_MASK; 324 if (!hcd->self.root_hub->do_remote_wakeup) 325 mask &= ~STS_PCD; 326 ehci_writel(ehci, mask, &ehci->regs->intr_enable); 327 ehci_readl(ehci, &ehci->regs->intr_enable); 328 329 done: 330 ehci->next_statechange = jiffies + msecs_to_jiffies(10); 331 ehci->enabled_hrtimer_events = 0; 332 ehci->next_hrtimer_event = EHCI_HRTIMER_NO_EVENT; 333 spin_unlock_irq (&ehci->lock); 334 335 hrtimer_cancel(&ehci->hrtimer); 336 return 0; 337 } 338 339 340 /* caller has locked the root hub, and should reset/reinit on error */ 341 static int ehci_bus_resume (struct usb_hcd *hcd) 342 { 343 struct ehci_hcd *ehci = hcd_to_ehci (hcd); 344 u32 temp; 345 u32 power_okay; 346 int i; 347 unsigned long resume_needed = 0; 348 349 if (time_before (jiffies, ehci->next_statechange)) 350 msleep(5); 351 spin_lock_irq (&ehci->lock); 352 if (!HCD_HW_ACCESSIBLE(hcd) || ehci->shutdown) 353 goto shutdown; 354 355 if (unlikely(ehci->debug)) { 356 if (!dbgp_reset_prep(hcd)) 357 ehci->debug = NULL; 358 else 359 dbgp_external_startup(hcd); 360 } 361 362 /* Ideally and we've got a real resume here, and no port's power 363 * was lost. (For PCI, that means Vaux was maintained.) But we 364 * could instead be restoring a swsusp snapshot -- so that BIOS was 365 * the last user of the controller, not reset/pm hardware keeping 366 * state we gave to it. 367 */ 368 power_okay = ehci_readl(ehci, &ehci->regs->intr_enable); 369 ehci_dbg(ehci, "resume root hub%s\n", 370 power_okay ? "" : " after power loss"); 371 372 /* at least some APM implementations will try to deliver 373 * IRQs right away, so delay them until we're ready. 374 */ 375 ehci_writel(ehci, 0, &ehci->regs->intr_enable); 376 377 /* re-init operational registers */ 378 ehci_writel(ehci, 0, &ehci->regs->segment); 379 ehci_writel(ehci, ehci->periodic_dma, &ehci->regs->frame_list); 380 ehci_writel(ehci, (u32) ehci->async->qh_dma, &ehci->regs->async_next); 381 382 /* restore CMD_RUN, framelist size, and irq threshold */ 383 ehci->command |= CMD_RUN; 384 ehci_writel(ehci, ehci->command, &ehci->regs->command); 385 ehci->rh_state = EHCI_RH_RUNNING; 386 387 /* Some controller/firmware combinations need a delay during which 388 * they set up the port statuses. See Bugzilla #8190. */ 389 spin_unlock_irq(&ehci->lock); 390 msleep(8); 391 spin_lock_irq(&ehci->lock); 392 if (ehci->shutdown) 393 goto shutdown; 394 395 /* clear phy low-power mode before resume */ 396 if (ehci->bus_suspended && ehci->has_hostpc) { 397 i = HCS_N_PORTS(ehci->hcs_params); 398 while (i--) { 399 if (test_bit(i, &ehci->bus_suspended)) { 400 u32 __iomem *hostpc_reg = 401 &ehci->regs->hostpc[i]; 402 403 temp = ehci_readl(ehci, hostpc_reg); 404 ehci_writel(ehci, temp & ~HOSTPC_PHCD, 405 hostpc_reg); 406 } 407 } 408 spin_unlock_irq(&ehci->lock); 409 msleep(5); 410 spin_lock_irq(&ehci->lock); 411 if (ehci->shutdown) 412 goto shutdown; 413 } 414 415 /* manually resume the ports we suspended during bus_suspend() */ 416 i = HCS_N_PORTS (ehci->hcs_params); 417 while (i--) { 418 temp = ehci_readl(ehci, &ehci->regs->port_status [i]); 419 temp &= ~(PORT_RWC_BITS | PORT_WAKE_BITS); 420 if (test_bit(i, &ehci->bus_suspended) && 421 (temp & PORT_SUSPEND)) { 422 temp |= PORT_RESUME; 423 set_bit(i, &resume_needed); 424 } 425 ehci_writel(ehci, temp, &ehci->regs->port_status [i]); 426 } 427 428 /* msleep for 20ms only if code is trying to resume port */ 429 if (resume_needed) { 430 spin_unlock_irq(&ehci->lock); 431 msleep(20); 432 spin_lock_irq(&ehci->lock); 433 if (ehci->shutdown) 434 goto shutdown; 435 } 436 437 i = HCS_N_PORTS (ehci->hcs_params); 438 while (i--) { 439 temp = ehci_readl(ehci, &ehci->regs->port_status [i]); 440 if (test_bit(i, &resume_needed)) { 441 temp &= ~(PORT_RWC_BITS | PORT_RESUME); 442 ehci_writel(ehci, temp, &ehci->regs->port_status [i]); 443 ehci_vdbg (ehci, "resumed port %d\n", i + 1); 444 } 445 } 446 447 ehci->next_statechange = jiffies + msecs_to_jiffies(5); 448 spin_unlock_irq(&ehci->lock); 449 450 ehci_handover_companion_ports(ehci); 451 452 /* Now we can safely re-enable irqs */ 453 spin_lock_irq(&ehci->lock); 454 if (ehci->shutdown) 455 goto shutdown; 456 ehci_writel(ehci, INTR_MASK, &ehci->regs->intr_enable); 457 (void) ehci_readl(ehci, &ehci->regs->intr_enable); 458 spin_unlock_irq(&ehci->lock); 459 460 return 0; 461 462 shutdown: 463 spin_unlock_irq(&ehci->lock); 464 return -ESHUTDOWN; 465 } 466 467 #else 468 469 #define ehci_bus_suspend NULL 470 #define ehci_bus_resume NULL 471 472 #endif /* CONFIG_PM */ 473 474 /*-------------------------------------------------------------------------*/ 475 476 /* 477 * Sets the owner of a port 478 */ 479 static void set_owner(struct ehci_hcd *ehci, int portnum, int new_owner) 480 { 481 u32 __iomem *status_reg; 482 u32 port_status; 483 int try; 484 485 status_reg = &ehci->regs->port_status[portnum]; 486 487 /* 488 * The controller won't set the OWNER bit if the port is 489 * enabled, so this loop will sometimes require at least two 490 * iterations: one to disable the port and one to set OWNER. 491 */ 492 for (try = 4; try > 0; --try) { 493 spin_lock_irq(&ehci->lock); 494 port_status = ehci_readl(ehci, status_reg); 495 if ((port_status & PORT_OWNER) == new_owner 496 || (port_status & (PORT_OWNER | PORT_CONNECT)) 497 == 0) 498 try = 0; 499 else { 500 port_status ^= PORT_OWNER; 501 port_status &= ~(PORT_PE | PORT_RWC_BITS); 502 ehci_writel(ehci, port_status, status_reg); 503 } 504 spin_unlock_irq(&ehci->lock); 505 if (try > 1) 506 msleep(5); 507 } 508 } 509 510 /*-------------------------------------------------------------------------*/ 511 512 static int check_reset_complete ( 513 struct ehci_hcd *ehci, 514 int index, 515 u32 __iomem *status_reg, 516 int port_status 517 ) { 518 if (!(port_status & PORT_CONNECT)) 519 return port_status; 520 521 /* if reset finished and it's still not enabled -- handoff */ 522 if (!(port_status & PORT_PE)) { 523 524 /* with integrated TT, there's nobody to hand it to! */ 525 if (ehci_is_TDI(ehci)) { 526 ehci_dbg (ehci, 527 "Failed to enable port %d on root hub TT\n", 528 index+1); 529 return port_status; 530 } 531 532 ehci_dbg (ehci, "port %d full speed --> companion\n", 533 index + 1); 534 535 // what happens if HCS_N_CC(params) == 0 ? 536 port_status |= PORT_OWNER; 537 port_status &= ~PORT_RWC_BITS; 538 ehci_writel(ehci, port_status, status_reg); 539 540 /* ensure 440EPX ohci controller state is operational */ 541 if (ehci->has_amcc_usb23) 542 set_ohci_hcfs(ehci, 1); 543 } else { 544 ehci_dbg(ehci, "port %d reset complete, port enabled\n", 545 index + 1); 546 /* ensure 440EPx ohci controller state is suspended */ 547 if (ehci->has_amcc_usb23) 548 set_ohci_hcfs(ehci, 0); 549 } 550 551 return port_status; 552 } 553 554 /*-------------------------------------------------------------------------*/ 555 556 557 /* build "status change" packet (one or two bytes) from HC registers */ 558 559 static int 560 ehci_hub_status_data (struct usb_hcd *hcd, char *buf) 561 { 562 struct ehci_hcd *ehci = hcd_to_ehci (hcd); 563 u32 temp, status; 564 u32 mask; 565 int ports, i, retval = 1; 566 unsigned long flags; 567 u32 ppcd = 0; 568 569 /* init status to no-changes */ 570 buf [0] = 0; 571 ports = HCS_N_PORTS (ehci->hcs_params); 572 if (ports > 7) { 573 buf [1] = 0; 574 retval++; 575 } 576 577 /* Inform the core about resumes-in-progress by returning 578 * a non-zero value even if there are no status changes. 579 */ 580 status = ehci->resuming_ports; 581 582 /* Some boards (mostly VIA?) report bogus overcurrent indications, 583 * causing massive log spam unless we completely ignore them. It 584 * may be relevant that VIA VT8235 controllers, where PORT_POWER is 585 * always set, seem to clear PORT_OCC and PORT_CSC when writing to 586 * PORT_POWER; that's surprising, but maybe within-spec. 587 */ 588 if (!ignore_oc) 589 mask = PORT_CSC | PORT_PEC | PORT_OCC; 590 else 591 mask = PORT_CSC | PORT_PEC; 592 // PORT_RESUME from hardware ~= PORT_STAT_C_SUSPEND 593 594 /* no hub change reports (bit 0) for now (power, ...) */ 595 596 /* port N changes (bit N)? */ 597 spin_lock_irqsave (&ehci->lock, flags); 598 599 /* get per-port change detect bits */ 600 if (ehci->has_ppcd) 601 ppcd = ehci_readl(ehci, &ehci->regs->status) >> 16; 602 603 for (i = 0; i < ports; i++) { 604 /* leverage per-port change bits feature */ 605 if (ehci->has_ppcd && !(ppcd & (1 << i))) 606 continue; 607 temp = ehci_readl(ehci, &ehci->regs->port_status [i]); 608 609 /* 610 * Return status information even for ports with OWNER set. 611 * Otherwise khubd wouldn't see the disconnect event when a 612 * high-speed device is switched over to the companion 613 * controller by the user. 614 */ 615 616 if ((temp & mask) != 0 || test_bit(i, &ehci->port_c_suspend) 617 || (ehci->reset_done[i] && time_after_eq( 618 jiffies, ehci->reset_done[i]))) { 619 if (i < 7) 620 buf [0] |= 1 << (i + 1); 621 else 622 buf [1] |= 1 << (i - 7); 623 status = STS_PCD; 624 } 625 } 626 /* FIXME autosuspend idle root hubs */ 627 spin_unlock_irqrestore (&ehci->lock, flags); 628 return status ? retval : 0; 629 } 630 631 /*-------------------------------------------------------------------------*/ 632 633 static void 634 ehci_hub_descriptor ( 635 struct ehci_hcd *ehci, 636 struct usb_hub_descriptor *desc 637 ) { 638 int ports = HCS_N_PORTS (ehci->hcs_params); 639 u16 temp; 640 641 desc->bDescriptorType = 0x29; 642 desc->bPwrOn2PwrGood = 10; /* ehci 1.0, 2.3.9 says 20ms max */ 643 desc->bHubContrCurrent = 0; 644 645 desc->bNbrPorts = ports; 646 temp = 1 + (ports / 8); 647 desc->bDescLength = 7 + 2 * temp; 648 649 /* two bitmaps: ports removable, and usb 1.0 legacy PortPwrCtrlMask */ 650 memset(&desc->u.hs.DeviceRemovable[0], 0, temp); 651 memset(&desc->u.hs.DeviceRemovable[temp], 0xff, temp); 652 653 temp = 0x0008; /* per-port overcurrent reporting */ 654 if (HCS_PPC (ehci->hcs_params)) 655 temp |= 0x0001; /* per-port power control */ 656 else 657 temp |= 0x0002; /* no power switching */ 658 #if 0 659 // re-enable when we support USB_PORT_FEAT_INDICATOR below. 660 if (HCS_INDICATOR (ehci->hcs_params)) 661 temp |= 0x0080; /* per-port indicators (LEDs) */ 662 #endif 663 desc->wHubCharacteristics = cpu_to_le16(temp); 664 } 665 666 /*-------------------------------------------------------------------------*/ 667 668 static int ehci_hub_control ( 669 struct usb_hcd *hcd, 670 u16 typeReq, 671 u16 wValue, 672 u16 wIndex, 673 char *buf, 674 u16 wLength 675 ) { 676 struct ehci_hcd *ehci = hcd_to_ehci (hcd); 677 int ports = HCS_N_PORTS (ehci->hcs_params); 678 u32 __iomem *status_reg = &ehci->regs->port_status[ 679 (wIndex & 0xff) - 1]; 680 u32 __iomem *hostpc_reg = &ehci->regs->hostpc[(wIndex & 0xff) - 1]; 681 u32 temp, temp1, status; 682 unsigned long flags; 683 int retval = 0; 684 unsigned selector; 685 686 /* 687 * FIXME: support SetPortFeatures USB_PORT_FEAT_INDICATOR. 688 * HCS_INDICATOR may say we can change LEDs to off/amber/green. 689 * (track current state ourselves) ... blink for diagnostics, 690 * power, "this is the one", etc. EHCI spec supports this. 691 */ 692 693 spin_lock_irqsave (&ehci->lock, flags); 694 switch (typeReq) { 695 case ClearHubFeature: 696 switch (wValue) { 697 case C_HUB_LOCAL_POWER: 698 case C_HUB_OVER_CURRENT: 699 /* no hub-wide feature/status flags */ 700 break; 701 default: 702 goto error; 703 } 704 break; 705 case ClearPortFeature: 706 if (!wIndex || wIndex > ports) 707 goto error; 708 wIndex--; 709 temp = ehci_readl(ehci, status_reg); 710 temp &= ~PORT_RWC_BITS; 711 712 /* 713 * Even if OWNER is set, so the port is owned by the 714 * companion controller, khubd needs to be able to clear 715 * the port-change status bits (especially 716 * USB_PORT_STAT_C_CONNECTION). 717 */ 718 719 switch (wValue) { 720 case USB_PORT_FEAT_ENABLE: 721 ehci_writel(ehci, temp & ~PORT_PE, status_reg); 722 break; 723 case USB_PORT_FEAT_C_ENABLE: 724 ehci_writel(ehci, temp | PORT_PEC, status_reg); 725 break; 726 case USB_PORT_FEAT_SUSPEND: 727 if (temp & PORT_RESET) 728 goto error; 729 if (ehci->no_selective_suspend) 730 break; 731 #ifdef CONFIG_USB_OTG 732 if ((hcd->self.otg_port == (wIndex + 1)) 733 && hcd->self.b_hnp_enable) { 734 otg_start_hnp(hcd->phy->otg); 735 break; 736 } 737 #endif 738 if (!(temp & PORT_SUSPEND)) 739 break; 740 if ((temp & PORT_PE) == 0) 741 goto error; 742 743 /* clear phy low-power mode before resume */ 744 if (ehci->has_hostpc) { 745 temp1 = ehci_readl(ehci, hostpc_reg); 746 ehci_writel(ehci, temp1 & ~HOSTPC_PHCD, 747 hostpc_reg); 748 spin_unlock_irqrestore(&ehci->lock, flags); 749 msleep(5);/* wait to leave low-power mode */ 750 spin_lock_irqsave(&ehci->lock, flags); 751 } 752 /* resume signaling for 20 msec */ 753 temp &= ~PORT_WAKE_BITS; 754 ehci_writel(ehci, temp | PORT_RESUME, status_reg); 755 ehci->reset_done[wIndex] = jiffies 756 + msecs_to_jiffies(20); 757 break; 758 case USB_PORT_FEAT_C_SUSPEND: 759 clear_bit(wIndex, &ehci->port_c_suspend); 760 break; 761 case USB_PORT_FEAT_POWER: 762 if (HCS_PPC (ehci->hcs_params)) 763 ehci_writel(ehci, temp & ~PORT_POWER, 764 status_reg); 765 break; 766 case USB_PORT_FEAT_C_CONNECTION: 767 if (ehci->has_lpm) { 768 /* clear PORTSC bits on disconnect */ 769 temp &= ~PORT_LPM; 770 temp &= ~PORT_DEV_ADDR; 771 } 772 ehci_writel(ehci, temp | PORT_CSC, status_reg); 773 break; 774 case USB_PORT_FEAT_C_OVER_CURRENT: 775 ehci_writel(ehci, temp | PORT_OCC, status_reg); 776 break; 777 case USB_PORT_FEAT_C_RESET: 778 /* GetPortStatus clears reset */ 779 break; 780 default: 781 goto error; 782 } 783 ehci_readl(ehci, &ehci->regs->command); /* unblock posted write */ 784 break; 785 case GetHubDescriptor: 786 ehci_hub_descriptor (ehci, (struct usb_hub_descriptor *) 787 buf); 788 break; 789 case GetHubStatus: 790 /* no hub-wide feature/status flags */ 791 memset (buf, 0, 4); 792 //cpu_to_le32s ((u32 *) buf); 793 break; 794 case GetPortStatus: 795 if (!wIndex || wIndex > ports) 796 goto error; 797 wIndex--; 798 status = 0; 799 temp = ehci_readl(ehci, status_reg); 800 801 // wPortChange bits 802 if (temp & PORT_CSC) 803 status |= USB_PORT_STAT_C_CONNECTION << 16; 804 if (temp & PORT_PEC) 805 status |= USB_PORT_STAT_C_ENABLE << 16; 806 807 if ((temp & PORT_OCC) && !ignore_oc){ 808 status |= USB_PORT_STAT_C_OVERCURRENT << 16; 809 810 /* 811 * Hubs should disable port power on over-current. 812 * However, not all EHCI implementations do this 813 * automatically, even if they _do_ support per-port 814 * power switching; they're allowed to just limit the 815 * current. khubd will turn the power back on. 816 */ 817 if ((temp & PORT_OC) && HCS_PPC(ehci->hcs_params)) { 818 ehci_writel(ehci, 819 temp & ~(PORT_RWC_BITS | PORT_POWER), 820 status_reg); 821 temp = ehci_readl(ehci, status_reg); 822 } 823 } 824 825 /* whoever resumes must GetPortStatus to complete it!! */ 826 if (temp & PORT_RESUME) { 827 828 /* Remote Wakeup received? */ 829 if (!ehci->reset_done[wIndex]) { 830 /* resume signaling for 20 msec */ 831 ehci->reset_done[wIndex] = jiffies 832 + msecs_to_jiffies(20); 833 /* check the port again */ 834 mod_timer(&ehci_to_hcd(ehci)->rh_timer, 835 ehci->reset_done[wIndex]); 836 } 837 838 /* resume completed? */ 839 else if (time_after_eq(jiffies, 840 ehci->reset_done[wIndex])) { 841 clear_bit(wIndex, &ehci->suspended_ports); 842 set_bit(wIndex, &ehci->port_c_suspend); 843 ehci->reset_done[wIndex] = 0; 844 845 /* stop resume signaling */ 846 temp = ehci_readl(ehci, status_reg); 847 ehci_writel(ehci, 848 temp & ~(PORT_RWC_BITS | PORT_RESUME), 849 status_reg); 850 clear_bit(wIndex, &ehci->resuming_ports); 851 retval = handshake(ehci, status_reg, 852 PORT_RESUME, 0, 2000 /* 2msec */); 853 if (retval != 0) { 854 ehci_err(ehci, 855 "port %d resume error %d\n", 856 wIndex + 1, retval); 857 goto error; 858 } 859 temp &= ~(PORT_SUSPEND|PORT_RESUME|(3<<10)); 860 } 861 } 862 863 /* whoever resets must GetPortStatus to complete it!! */ 864 if ((temp & PORT_RESET) 865 && time_after_eq(jiffies, 866 ehci->reset_done[wIndex])) { 867 status |= USB_PORT_STAT_C_RESET << 16; 868 ehci->reset_done [wIndex] = 0; 869 clear_bit(wIndex, &ehci->resuming_ports); 870 871 /* force reset to complete */ 872 ehci_writel(ehci, temp & ~(PORT_RWC_BITS | PORT_RESET), 873 status_reg); 874 /* REVISIT: some hardware needs 550+ usec to clear 875 * this bit; seems too long to spin routinely... 876 */ 877 retval = handshake(ehci, status_reg, 878 PORT_RESET, 0, 1000); 879 if (retval != 0) { 880 ehci_err (ehci, "port %d reset error %d\n", 881 wIndex + 1, retval); 882 goto error; 883 } 884 885 /* see what we found out */ 886 temp = check_reset_complete (ehci, wIndex, status_reg, 887 ehci_readl(ehci, status_reg)); 888 } 889 890 if (!(temp & (PORT_RESUME|PORT_RESET))) { 891 ehci->reset_done[wIndex] = 0; 892 clear_bit(wIndex, &ehci->resuming_ports); 893 } 894 895 /* transfer dedicated ports to the companion hc */ 896 if ((temp & PORT_CONNECT) && 897 test_bit(wIndex, &ehci->companion_ports)) { 898 temp &= ~PORT_RWC_BITS; 899 temp |= PORT_OWNER; 900 ehci_writel(ehci, temp, status_reg); 901 ehci_dbg(ehci, "port %d --> companion\n", wIndex + 1); 902 temp = ehci_readl(ehci, status_reg); 903 } 904 905 /* 906 * Even if OWNER is set, there's no harm letting khubd 907 * see the wPortStatus values (they should all be 0 except 908 * for PORT_POWER anyway). 909 */ 910 911 if (temp & PORT_CONNECT) { 912 status |= USB_PORT_STAT_CONNECTION; 913 // status may be from integrated TT 914 if (ehci->has_hostpc) { 915 temp1 = ehci_readl(ehci, hostpc_reg); 916 status |= ehci_port_speed(ehci, temp1); 917 } else 918 status |= ehci_port_speed(ehci, temp); 919 } 920 if (temp & PORT_PE) 921 status |= USB_PORT_STAT_ENABLE; 922 923 /* maybe the port was unsuspended without our knowledge */ 924 if (temp & (PORT_SUSPEND|PORT_RESUME)) { 925 status |= USB_PORT_STAT_SUSPEND; 926 } else if (test_bit(wIndex, &ehci->suspended_ports)) { 927 clear_bit(wIndex, &ehci->suspended_ports); 928 clear_bit(wIndex, &ehci->resuming_ports); 929 ehci->reset_done[wIndex] = 0; 930 if (temp & PORT_PE) 931 set_bit(wIndex, &ehci->port_c_suspend); 932 } 933 934 if (temp & PORT_OC) 935 status |= USB_PORT_STAT_OVERCURRENT; 936 if (temp & PORT_RESET) 937 status |= USB_PORT_STAT_RESET; 938 if (temp & PORT_POWER) 939 status |= USB_PORT_STAT_POWER; 940 if (test_bit(wIndex, &ehci->port_c_suspend)) 941 status |= USB_PORT_STAT_C_SUSPEND << 16; 942 943 #ifndef VERBOSE_DEBUG 944 if (status & ~0xffff) /* only if wPortChange is interesting */ 945 #endif 946 dbg_port (ehci, "GetStatus", wIndex + 1, temp); 947 put_unaligned_le32(status, buf); 948 break; 949 case SetHubFeature: 950 switch (wValue) { 951 case C_HUB_LOCAL_POWER: 952 case C_HUB_OVER_CURRENT: 953 /* no hub-wide feature/status flags */ 954 break; 955 default: 956 goto error; 957 } 958 break; 959 case SetPortFeature: 960 selector = wIndex >> 8; 961 wIndex &= 0xff; 962 if (unlikely(ehci->debug)) { 963 /* If the debug port is active any port 964 * feature requests should get denied */ 965 if (wIndex == HCS_DEBUG_PORT(ehci->hcs_params) && 966 (readl(&ehci->debug->control) & DBGP_ENABLED)) { 967 retval = -ENODEV; 968 goto error_exit; 969 } 970 } 971 if (!wIndex || wIndex > ports) 972 goto error; 973 wIndex--; 974 temp = ehci_readl(ehci, status_reg); 975 if (temp & PORT_OWNER) 976 break; 977 978 temp &= ~PORT_RWC_BITS; 979 switch (wValue) { 980 case USB_PORT_FEAT_SUSPEND: 981 if (ehci->no_selective_suspend) 982 break; 983 if ((temp & PORT_PE) == 0 984 || (temp & PORT_RESET) != 0) 985 goto error; 986 987 /* After above check the port must be connected. 988 * Set appropriate bit thus could put phy into low power 989 * mode if we have hostpc feature 990 */ 991 temp &= ~PORT_WKCONN_E; 992 temp |= PORT_WKDISC_E | PORT_WKOC_E; 993 ehci_writel(ehci, temp | PORT_SUSPEND, status_reg); 994 if (ehci->has_hostpc) { 995 spin_unlock_irqrestore(&ehci->lock, flags); 996 msleep(5);/* 5ms for HCD enter low pwr mode */ 997 spin_lock_irqsave(&ehci->lock, flags); 998 temp1 = ehci_readl(ehci, hostpc_reg); 999 ehci_writel(ehci, temp1 | HOSTPC_PHCD, 1000 hostpc_reg); 1001 temp1 = ehci_readl(ehci, hostpc_reg); 1002 ehci_dbg(ehci, "Port%d phy low pwr mode %s\n", 1003 wIndex, (temp1 & HOSTPC_PHCD) ? 1004 "succeeded" : "failed"); 1005 } 1006 set_bit(wIndex, &ehci->suspended_ports); 1007 break; 1008 case USB_PORT_FEAT_POWER: 1009 if (HCS_PPC (ehci->hcs_params)) 1010 ehci_writel(ehci, temp | PORT_POWER, 1011 status_reg); 1012 break; 1013 case USB_PORT_FEAT_RESET: 1014 if (temp & PORT_RESUME) 1015 goto error; 1016 /* line status bits may report this as low speed, 1017 * which can be fine if this root hub has a 1018 * transaction translator built in. 1019 */ 1020 if ((temp & (PORT_PE|PORT_CONNECT)) == PORT_CONNECT 1021 && !ehci_is_TDI(ehci) 1022 && PORT_USB11 (temp)) { 1023 ehci_dbg (ehci, 1024 "port %d low speed --> companion\n", 1025 wIndex + 1); 1026 temp |= PORT_OWNER; 1027 } else { 1028 ehci_vdbg (ehci, "port %d reset\n", wIndex + 1); 1029 temp |= PORT_RESET; 1030 temp &= ~PORT_PE; 1031 1032 /* 1033 * caller must wait, then call GetPortStatus 1034 * usb 2.0 spec says 50 ms resets on root 1035 */ 1036 ehci->reset_done [wIndex] = jiffies 1037 + msecs_to_jiffies (50); 1038 } 1039 ehci_writel(ehci, temp, status_reg); 1040 break; 1041 1042 /* For downstream facing ports (these): one hub port is put 1043 * into test mode according to USB2 11.24.2.13, then the hub 1044 * must be reset (which for root hub now means rmmod+modprobe, 1045 * or else system reboot). See EHCI 2.3.9 and 4.14 for info 1046 * about the EHCI-specific stuff. 1047 */ 1048 case USB_PORT_FEAT_TEST: 1049 if (!selector || selector > 5) 1050 goto error; 1051 spin_unlock_irqrestore(&ehci->lock, flags); 1052 ehci_quiesce(ehci); 1053 spin_lock_irqsave(&ehci->lock, flags); 1054 1055 /* Put all enabled ports into suspend */ 1056 while (ports--) { 1057 u32 __iomem *sreg = 1058 &ehci->regs->port_status[ports]; 1059 1060 temp = ehci_readl(ehci, sreg) & ~PORT_RWC_BITS; 1061 if (temp & PORT_PE) 1062 ehci_writel(ehci, temp | PORT_SUSPEND, 1063 sreg); 1064 } 1065 1066 spin_unlock_irqrestore(&ehci->lock, flags); 1067 ehci_halt(ehci); 1068 spin_lock_irqsave(&ehci->lock, flags); 1069 1070 temp = ehci_readl(ehci, status_reg); 1071 temp |= selector << 16; 1072 ehci_writel(ehci, temp, status_reg); 1073 break; 1074 1075 default: 1076 goto error; 1077 } 1078 ehci_readl(ehci, &ehci->regs->command); /* unblock posted writes */ 1079 break; 1080 1081 default: 1082 error: 1083 /* "stall" on error */ 1084 retval = -EPIPE; 1085 } 1086 error_exit: 1087 spin_unlock_irqrestore (&ehci->lock, flags); 1088 return retval; 1089 } 1090 1091 static void __maybe_unused ehci_relinquish_port(struct usb_hcd *hcd, 1092 int portnum) 1093 { 1094 struct ehci_hcd *ehci = hcd_to_ehci(hcd); 1095 1096 if (ehci_is_TDI(ehci)) 1097 return; 1098 set_owner(ehci, --portnum, PORT_OWNER); 1099 } 1100 1101 static int __maybe_unused ehci_port_handed_over(struct usb_hcd *hcd, 1102 int portnum) 1103 { 1104 struct ehci_hcd *ehci = hcd_to_ehci(hcd); 1105 u32 __iomem *reg; 1106 1107 if (ehci_is_TDI(ehci)) 1108 return 0; 1109 reg = &ehci->regs->port_status[portnum - 1]; 1110 return ehci_readl(ehci, reg) & PORT_OWNER; 1111 } 1112