[PATCH] USB: EHCI updates split init/reinit logic for resume

Moving the PCI-specific parts of the EHCI driver into their own file
created a few issues ... notably on resume paths which (like swsusp)
require re-initializing the controller. This patch:

- Splits the EHCI startup code into run-once HCD setup code and
separate "init the hardware" reinit code. (That reinit code is
a superset of the "early usb handoff" code.)

- Then it makes the PCI init code run both, and the resume code only
run the reinit code.

- It also removes needless pci wrappers around EHCI start/stop methods.

- Removes a byteswap issue that would be seen on big-endian hardware.

The HCD glue still doesn't actually provide a good way to do all this
run-one init stuff in one place though.

Signed-off-by: David Brownell <dbrownell@users.sourceforge.net>
Signed-off-by: Greg Kroah-Hartman <gregkh@suse.de>
Signed-off-by: Linus Torvalds <torvalds@osdl.org>

authored by David Brownell and committed by Linus Torvalds 18807521 abcc9448

+161 -160
+82 -73
drivers/usb/host/ehci-hcd.c
··· 411 411 dbg_status (ehci, "ehci_stop completed", readl (&ehci->regs->status)); 412 412 } 413 413 414 - static int ehci_run (struct usb_hcd *hcd) 414 + /* one-time init, only for memory state */ 415 + static int ehci_init(struct usb_hcd *hcd) 415 416 { 416 - struct ehci_hcd *ehci = hcd_to_ehci (hcd); 417 + struct ehci_hcd *ehci = hcd_to_ehci(hcd); 417 418 u32 temp; 418 419 int retval; 419 420 u32 hcc_params; 420 - int first; 421 421 422 - /* skip some things on restart paths */ 423 - first = (ehci->watchdog.data == 0); 424 - if (first) { 425 - init_timer (&ehci->watchdog); 426 - ehci->watchdog.function = ehci_watchdog; 427 - ehci->watchdog.data = (unsigned long) ehci; 428 - } 422 + spin_lock_init(&ehci->lock); 423 + 424 + init_timer(&ehci->watchdog); 425 + ehci->watchdog.function = ehci_watchdog; 426 + ehci->watchdog.data = (unsigned long) ehci; 429 427 430 428 /* 431 429 * hw default: 1K periodic list heads, one per frame. 432 430 * periodic_size can shrink by USBCMD update if hcc_params allows. 433 431 */ 434 432 ehci->periodic_size = DEFAULT_I_TDPS; 435 - if (first && (retval = ehci_mem_init (ehci, GFP_KERNEL)) < 0) 433 + if ((retval = ehci_mem_init(ehci, GFP_KERNEL)) < 0) 436 434 return retval; 437 435 438 436 /* controllers may cache some of the periodic schedule ... */ 439 - hcc_params = readl (&ehci->caps->hcc_params); 440 - if (HCC_ISOC_CACHE (hcc_params)) // full frame cache 437 + hcc_params = readl(&ehci->caps->hcc_params); 438 + if (HCC_ISOC_CACHE(hcc_params)) // full frame cache 441 439 ehci->i_thresh = 8; 442 440 else // N microframes cached 443 - ehci->i_thresh = 2 + HCC_ISOC_THRES (hcc_params); 441 + ehci->i_thresh = 2 + HCC_ISOC_THRES(hcc_params); 444 442 445 443 ehci->reclaim = NULL; 446 444 ehci->reclaim_ready = 0; 447 445 ehci->next_uframe = -1; 448 - 449 - /* controller state: unknown --> reset */ 450 - 451 - /* EHCI spec section 4.1 */ 452 - if ((retval = ehci_reset (ehci)) != 0) { 453 - ehci_mem_cleanup (ehci); 454 - return retval; 455 - } 456 - writel (ehci->periodic_dma, &ehci->regs->frame_list); 457 446 458 447 /* 459 448 * dedicate a qh for the async ring head, since we couldn't unlink ··· 451 462 * its dummy is used in hw_alt_next of many tds, to prevent the qh 452 463 * from automatically advancing to the next td after short reads. 453 464 */ 454 - if (first) { 455 - ehci->async->qh_next.qh = NULL; 456 - ehci->async->hw_next = QH_NEXT (ehci->async->qh_dma); 457 - ehci->async->hw_info1 = cpu_to_le32 (QH_HEAD); 458 - ehci->async->hw_token = cpu_to_le32 (QTD_STS_HALT); 459 - ehci->async->hw_qtd_next = EHCI_LIST_END; 460 - ehci->async->qh_state = QH_STATE_LINKED; 461 - ehci->async->hw_alt_next = QTD_NEXT (ehci->async->dummy->qtd_dma); 462 - } 463 - writel ((u32)ehci->async->qh_dma, &ehci->regs->async_next); 464 - 465 - /* 466 - * hcc_params controls whether ehci->regs->segment must (!!!) 467 - * be used; it constrains QH/ITD/SITD and QTD locations. 468 - * pci_pool consistent memory always uses segment zero. 469 - * streaming mappings for I/O buffers, like pci_map_single(), 470 - * can return segments above 4GB, if the device allows. 471 - * 472 - * NOTE: the dma mask is visible through dma_supported(), so 473 - * drivers can pass this info along ... like NETIF_F_HIGHDMA, 474 - * Scsi_Host.highmem_io, and so forth. It's readonly to all 475 - * host side drivers though. 476 - */ 477 - if (HCC_64BIT_ADDR (hcc_params)) { 478 - writel (0, &ehci->regs->segment); 479 - #if 0 480 - // this is deeply broken on almost all architectures 481 - if (!dma_set_mask (hcd->self.controller, DMA_64BIT_MASK)) 482 - ehci_info (ehci, "enabled 64bit DMA\n"); 483 - #endif 484 - } 465 + ehci->async->qh_next.qh = NULL; 466 + ehci->async->hw_next = QH_NEXT(ehci->async->qh_dma); 467 + ehci->async->hw_info1 = cpu_to_le32(QH_HEAD); 468 + ehci->async->hw_token = cpu_to_le32(QTD_STS_HALT); 469 + ehci->async->hw_qtd_next = EHCI_LIST_END; 470 + ehci->async->qh_state = QH_STATE_LINKED; 471 + ehci->async->hw_alt_next = QTD_NEXT(ehci->async->dummy->qtd_dma); 485 472 486 473 /* clear interrupt enables, set irq latency */ 487 474 if (log2_irq_thresh < 0 || log2_irq_thresh > 6) ··· 472 507 * make problems: throughput reduction (!), data errors... 473 508 */ 474 509 if (park) { 475 - park = min (park, (unsigned) 3); 510 + park = min(park, (unsigned) 3); 476 511 temp |= CMD_PARK; 477 512 temp |= park << 8; 478 513 } 479 - ehci_info (ehci, "park %d\n", park); 514 + ehci_dbg(ehci, "park %d\n", park); 480 515 } 481 - if (HCC_PGM_FRAMELISTLEN (hcc_params)) { 516 + if (HCC_PGM_FRAMELISTLEN(hcc_params)) { 482 517 /* periodic schedule size can be smaller than default */ 483 518 temp &= ~(3 << 2); 484 519 temp |= (EHCI_TUNE_FLS << 2); ··· 486 521 case 0: ehci->periodic_size = 1024; break; 487 522 case 1: ehci->periodic_size = 512; break; 488 523 case 2: ehci->periodic_size = 256; break; 489 - default: BUG (); 524 + default: BUG(); 490 525 } 491 526 } 527 + ehci->command = temp; 528 + 529 + ehci->reboot_notifier.notifier_call = ehci_reboot; 530 + register_reboot_notifier(&ehci->reboot_notifier); 531 + 532 + return 0; 533 + } 534 + 535 + /* start HC running; it's halted, ehci_init() has been run (once) */ 536 + static int ehci_run (struct usb_hcd *hcd) 537 + { 538 + struct ehci_hcd *ehci = hcd_to_ehci (hcd); 539 + int retval; 540 + u32 temp; 541 + u32 hcc_params; 542 + 543 + /* EHCI spec section 4.1 */ 544 + if ((retval = ehci_reset(ehci)) != 0) { 545 + unregister_reboot_notifier(&ehci->reboot_notifier); 546 + ehci_mem_cleanup(ehci); 547 + return retval; 548 + } 549 + writel(ehci->periodic_dma, &ehci->regs->frame_list); 550 + writel((u32)ehci->async->qh_dma, &ehci->regs->async_next); 551 + 552 + /* 553 + * hcc_params controls whether ehci->regs->segment must (!!!) 554 + * be used; it constrains QH/ITD/SITD and QTD locations. 555 + * pci_pool consistent memory always uses segment zero. 556 + * streaming mappings for I/O buffers, like pci_map_single(), 557 + * can return segments above 4GB, if the device allows. 558 + * 559 + * NOTE: the dma mask is visible through dma_supported(), so 560 + * drivers can pass this info along ... like NETIF_F_HIGHDMA, 561 + * Scsi_Host.highmem_io, and so forth. It's readonly to all 562 + * host side drivers though. 563 + */ 564 + hcc_params = readl(&ehci->caps->hcc_params); 565 + if (HCC_64BIT_ADDR(hcc_params)) { 566 + writel(0, &ehci->regs->segment); 567 + #if 0 568 + // this is deeply broken on almost all architectures 569 + if (!dma_set_mask(hcd->self.controller, DMA_64BIT_MASK)) 570 + ehci_info(ehci, "enabled 64bit DMA\n"); 571 + #endif 572 + } 573 + 574 + 492 575 // Philips, Intel, and maybe others need CMD_RUN before the 493 576 // root hub will detect new devices (why?); NEC doesn't 494 - temp |= CMD_RUN; 495 - writel (temp, &ehci->regs->command); 496 - dbg_cmd (ehci, "init", temp); 497 - 498 - /* set async sleep time = 10 us ... ? */ 577 + ehci->command &= ~(CMD_LRESET|CMD_IAAD|CMD_PSE|CMD_ASE|CMD_RESET); 578 + ehci->command |= CMD_RUN; 579 + writel (ehci->command, &ehci->regs->command); 580 + dbg_cmd (ehci, "init", ehci->command); 499 581 500 582 /* 501 583 * Start, enabling full USB 2.0 functionality ... usb 1.1 devices ··· 550 538 * involved with the root hub. (Except where one is integrated, 551 539 * and there's no companion controller unless maybe for USB OTG.) 552 540 */ 553 - if (first) { 554 - ehci->reboot_notifier.notifier_call = ehci_reboot; 555 - register_reboot_notifier (&ehci->reboot_notifier); 556 - } 557 - 558 541 hcd->state = HC_STATE_RUNNING; 559 542 writel (FLAG_CF, &ehci->regs->configured_flag); 560 - readl (&ehci->regs->command); /* unblock posted write */ 543 + readl (&ehci->regs->command); /* unblock posted writes */ 561 544 562 545 temp = HC_VERSION(readl (&ehci->caps->hc_capbase)); 563 546 ehci_info (ehci, 564 - "USB %x.%x %s, EHCI %x.%02x, driver %s\n", 547 + "USB %x.%x started, EHCI %x.%02x, driver %s\n", 565 548 ((ehci->sbrn & 0xf0)>>4), (ehci->sbrn & 0x0f), 566 - first ? "initialized" : "restarted", 567 549 temp >> 8, temp & 0xff, DRIVER_VERSION); 568 550 569 551 writel (INTR_MASK, &ehci->regs->intr_enable); /* Turn On Interrupts */ 570 552 571 - if (first) 572 - create_debug_files (ehci); 553 + /* GRR this is run-once init(), being done every time the HC starts. 554 + * So long as they're part of class devices, we can't do it init() 555 + * since the class device isn't created that early. 556 + */ 557 + create_debug_files(ehci); 573 558 574 559 return 0; 575 560 }
+79 -87
drivers/usb/host/ehci-pci.c
··· 58 58 return 0; 59 59 } 60 60 61 - /* called by khubd or root hub init threads */ 61 + /* called after powerup, by probe or system-pm "wakeup" */ 62 + static int ehci_pci_reinit(struct ehci_hcd *ehci, struct pci_dev *pdev) 63 + { 64 + u32 temp; 65 + int retval; 66 + unsigned count = 256/4; 67 + 68 + /* optional debug port, normally in the first BAR */ 69 + temp = pci_find_capability(pdev, 0x0a); 70 + if (temp) { 71 + pci_read_config_dword(pdev, temp, &temp); 72 + temp >>= 16; 73 + if ((temp & (3 << 13)) == (1 << 13)) { 74 + temp &= 0x1fff; 75 + ehci->debug = ehci_to_hcd(ehci)->regs + temp; 76 + temp = readl(&ehci->debug->control); 77 + ehci_info(ehci, "debug port %d%s\n", 78 + HCS_DEBUG_PORT(ehci->hcs_params), 79 + (temp & DBGP_ENABLED) 80 + ? " IN USE" 81 + : ""); 82 + if (!(temp & DBGP_ENABLED)) 83 + ehci->debug = NULL; 84 + } 85 + } 86 + 87 + temp = HCC_EXT_CAPS(readl(&ehci->caps->hcc_params)); 88 + 89 + /* EHCI 0.96 and later may have "extended capabilities" */ 90 + while (temp && count--) { 91 + u32 cap; 92 + 93 + pci_read_config_dword(pdev, temp, &cap); 94 + ehci_dbg(ehci, "capability %04x at %02x\n", cap, temp); 95 + switch (cap & 0xff) { 96 + case 1: /* BIOS/SMM/... handoff */ 97 + if (bios_handoff(ehci, temp, cap) != 0) 98 + return -EOPNOTSUPP; 99 + break; 100 + case 0: /* illegal reserved capability */ 101 + ehci_dbg(ehci, "illegal capability!\n"); 102 + cap = 0; 103 + /* FALLTHROUGH */ 104 + default: /* unknown */ 105 + break; 106 + } 107 + temp = (cap >> 8) & 0xff; 108 + } 109 + if (!count) { 110 + ehci_err(ehci, "bogus capabilities ... PCI problems!\n"); 111 + return -EIO; 112 + } 113 + 114 + /* PCI Memory-Write-Invalidate cycle support is optional (uncommon) */ 115 + retval = pci_set_mwi(pdev); 116 + if (!retval) 117 + ehci_dbg(ehci, "MWI active\n"); 118 + 119 + ehci_port_power(ehci, 0); 120 + 121 + return 0; 122 + } 123 + 124 + /* called by khubd or root hub (re)init threads; leaves HC in halt state */ 62 125 static int ehci_pci_reset(struct usb_hcd *hcd) 63 126 { 64 127 struct ehci_hcd *ehci = hcd_to_ehci(hcd); 65 128 struct pci_dev *pdev = to_pci_dev(hcd->self.controller); 66 129 u32 temp; 67 - unsigned count = 256/4; 68 - 69 - spin_lock_init (&ehci->lock); 130 + int retval; 70 131 71 132 ehci->caps = hcd->regs; 72 133 ehci->regs = hcd->regs + HC_LENGTH(readl(&ehci->caps->hc_capbase)); ··· 136 75 137 76 /* cache this readonly data; minimize chip reads */ 138 77 ehci->hcs_params = readl(&ehci->caps->hcs_params); 78 + 79 + retval = ehci_halt(ehci); 80 + if (retval) 81 + return retval; 139 82 140 83 /* NOTE: only the parts below this line are PCI-specific */ 141 84 ··· 176 111 break; 177 112 } 178 113 179 - /* optional debug port, normally in the first BAR */ 180 - temp = pci_find_capability(pdev, 0x0a); 181 - if (temp) { 182 - pci_read_config_dword(pdev, temp, &temp); 183 - temp >>= 16; 184 - if ((temp & (3 << 13)) == (1 << 13)) { 185 - temp &= 0x1fff; 186 - ehci->debug = hcd->regs + temp; 187 - temp = readl(&ehci->debug->control); 188 - ehci_info(ehci, "debug port %d%s\n", 189 - HCS_DEBUG_PORT(ehci->hcs_params), 190 - (temp & DBGP_ENABLED) 191 - ? " IN USE" 192 - : ""); 193 - if (!(temp & DBGP_ENABLED)) 194 - ehci->debug = NULL; 195 - } 196 - } 197 - 198 - temp = HCC_EXT_CAPS(readl(&ehci->caps->hcc_params)); 199 - 200 - /* EHCI 0.96 and later may have "extended capabilities" */ 201 - while (temp && count--) { 202 - u32 cap; 203 - 204 - pci_read_config_dword(to_pci_dev(hcd->self.controller), 205 - temp, &cap); 206 - ehci_dbg(ehci, "capability %04x at %02x\n", cap, temp); 207 - switch (cap & 0xff) { 208 - case 1: /* BIOS/SMM/... handoff */ 209 - if (bios_handoff(ehci, temp, cap) != 0) 210 - return -EOPNOTSUPP; 211 - break; 212 - case 0: /* illegal reserved capability */ 213 - ehci_warn(ehci, "illegal capability!\n"); 214 - cap = 0; 215 - /* FALLTHROUGH */ 216 - default: /* unknown */ 217 - break; 218 - } 219 - temp = (cap >> 8) & 0xff; 220 - } 221 - if (!count) { 222 - ehci_err(ehci, "bogus capabilities ... PCI problems!\n"); 223 - return -EIO; 224 - } 225 114 if (ehci_is_TDI(ehci)) 226 115 ehci_reset(ehci); 227 - 228 - ehci_port_power(ehci, 0); 229 116 230 117 /* at least the Genesys GL880S needs fixup here */ 231 118 temp = HCS_N_CC(ehci->hcs_params) * HCS_N_PCC(ehci->hcs_params); ··· 201 184 } 202 185 } 203 186 204 - /* force HC to halt state */ 205 - return ehci_halt(ehci); 206 - } 207 - 208 - static int ehci_pci_start(struct usb_hcd *hcd) 209 - { 210 - struct ehci_hcd *ehci = hcd_to_ehci(hcd); 211 - int result = 0; 212 - struct pci_dev *pdev; 213 - u16 port_wake; 214 - 215 - pdev = to_pci_dev(hcd->self.controller); 216 - 217 187 /* Serial Bus Release Number is at PCI 0x60 offset */ 218 188 pci_read_config_byte(pdev, 0x60, &ehci->sbrn); 219 189 220 - /* port wake capability, reported by boot firmware */ 221 - pci_read_config_word(pdev, 0x62, &port_wake); 222 - hcd->can_wakeup = (port_wake & 1) != 0; 190 + /* REVISIT: per-port wake capability (PCI 0x62) currently unused */ 223 191 224 - /* PCI Memory-Write-Invalidate cycle support is optional (uncommon) */ 225 - result = pci_set_mwi(pdev); 226 - if (!result) 227 - ehci_dbg(ehci, "MWI active\n"); 192 + retval = ehci_pci_reinit(ehci, pdev); 228 193 229 - return ehci_run(hcd); 230 - } 231 - 232 - /* always called by thread; normally rmmod */ 233 - 234 - static void ehci_pci_stop(struct usb_hcd *hcd) 235 - { 236 - ehci_stop(hcd); 194 + /* finish init */ 195 + return ehci_init(hcd); 237 196 } 238 197 239 198 /*-------------------------------------------------------------------------*/ ··· 243 250 struct ehci_hcd *ehci = hcd_to_ehci(hcd); 244 251 unsigned port; 245 252 struct usb_device *root = hcd->self.root_hub; 253 + struct pci_dev *pdev = to_pci_dev(hcd->self.controller); 246 254 int retval = -EINVAL; 247 255 248 256 // maybe restore FLADJ ··· 252 258 msleep(100); 253 259 254 260 /* If CF is clear, we lost PCI Vaux power and need to restart. */ 255 - if (readl(&ehci->regs->configured_flag) != cpu_to_le32(FLAG_CF)) 261 + if (readl(&ehci->regs->configured_flag) != FLAG_CF) 256 262 goto restart; 257 263 258 264 /* If any port is suspended (or owned by the companion), ··· 286 292 */ 287 293 (void) ehci_halt(ehci); 288 294 (void) ehci_reset(ehci); 289 - (void) ehci_pci_reset(hcd); 295 + (void) ehci_pci_reinit(ehci, pdev); 290 296 291 297 /* emptying the schedule aborts any urbs */ 292 298 spin_lock_irq(&ehci->lock); ··· 298 304 /* restart; khubd will disconnect devices */ 299 305 retval = ehci_run(hcd); 300 306 301 - /* here we "know" root ports should always stay powered; 302 - * but some controllers may lose all power. 303 - */ 307 + /* here we "know" root ports should always stay powered */ 304 308 ehci_port_power(ehci, 1); 305 309 306 310 return retval; ··· 320 328 * basic lifecycle operations 321 329 */ 322 330 .reset = ehci_pci_reset, 323 - .start = ehci_pci_start, 331 + .start = ehci_run, 324 332 #ifdef CONFIG_PM 325 333 .suspend = ehci_pci_suspend, 326 334 .resume = ehci_pci_resume, 327 335 #endif 328 - .stop = ehci_pci_stop, 336 + .stop = ehci_stop, 329 337 330 338 /* 331 339 * managing i/o requests and associated device resources