11da177e4SLinus Torvalds /* 21da177e4SLinus Torvalds * OHCI HCD (Host Controller Driver) for USB. 31da177e4SLinus Torvalds * 41da177e4SLinus Torvalds * (C) Copyright 1999 Roman Weissgaerber <weissg@vienna.at> 51da177e4SLinus Torvalds * (C) Copyright 2000-2004 David Brownell <dbrownell@users.sourceforge.net> 61da177e4SLinus Torvalds * 71da177e4SLinus Torvalds * This file is licenced under GPL 81da177e4SLinus Torvalds */ 91da177e4SLinus Torvalds 101da177e4SLinus Torvalds /*-------------------------------------------------------------------------*/ 111da177e4SLinus Torvalds 121da177e4SLinus Torvalds /* 131da177e4SLinus Torvalds * OHCI Root Hub ... the nonsharable stuff 141da177e4SLinus Torvalds */ 151da177e4SLinus Torvalds 161da177e4SLinus Torvalds #define dbg_port(hc,label,num,value) \ 171da177e4SLinus Torvalds ohci_dbg (hc, \ 181da177e4SLinus Torvalds "%s roothub.portstatus [%d] " \ 191da177e4SLinus Torvalds "= 0x%08x%s%s%s%s%s%s%s%s%s%s%s%s\n", \ 201da177e4SLinus Torvalds label, num, temp, \ 211da177e4SLinus Torvalds (temp & RH_PS_PRSC) ? " PRSC" : "", \ 221da177e4SLinus Torvalds (temp & RH_PS_OCIC) ? " OCIC" : "", \ 231da177e4SLinus Torvalds (temp & RH_PS_PSSC) ? " PSSC" : "", \ 241da177e4SLinus Torvalds (temp & RH_PS_PESC) ? " PESC" : "", \ 251da177e4SLinus Torvalds (temp & RH_PS_CSC) ? " CSC" : "", \ 261da177e4SLinus Torvalds \ 271da177e4SLinus Torvalds (temp & RH_PS_LSDA) ? " LSDA" : "", \ 281da177e4SLinus Torvalds (temp & RH_PS_PPS) ? " PPS" : "", \ 291da177e4SLinus Torvalds (temp & RH_PS_PRS) ? " PRS" : "", \ 301da177e4SLinus Torvalds (temp & RH_PS_POCI) ? " POCI" : "", \ 311da177e4SLinus Torvalds (temp & RH_PS_PSS) ? " PSS" : "", \ 321da177e4SLinus Torvalds \ 331da177e4SLinus Torvalds (temp & RH_PS_PES) ? " PES" : "", \ 341da177e4SLinus Torvalds (temp & RH_PS_CCS) ? " CCS" : "" \ 351da177e4SLinus Torvalds ); 361da177e4SLinus Torvalds 371da177e4SLinus Torvalds /*-------------------------------------------------------------------------*/ 381da177e4SLinus Torvalds 39d413984aSDavid Brownell /* hcd->hub_irq_enable() */ 40d413984aSDavid Brownell static void ohci_rhsc_enable (struct usb_hcd *hcd) 41d413984aSDavid Brownell { 42d413984aSDavid Brownell struct ohci_hcd *ohci = hcd_to_ohci (hcd); 43d413984aSDavid Brownell 44d413984aSDavid Brownell ohci_writel (ohci, OHCI_INTR_RHSC, &ohci->regs->intrenable); 45d413984aSDavid Brownell } 46d413984aSDavid Brownell 471da177e4SLinus Torvalds #define OHCI_SCHED_ENABLES \ 481da177e4SLinus Torvalds (OHCI_CTRL_CLE|OHCI_CTRL_BLE|OHCI_CTRL_PLE|OHCI_CTRL_IE) 491da177e4SLinus Torvalds 50*7d12e780SDavid Howells static void dl_done_list (struct ohci_hcd *); 51*7d12e780SDavid Howells static void finish_unlinks (struct ohci_hcd *, u16); 521da177e4SLinus Torvalds 538d1a243bSAlan Stern static int ohci_rh_suspend (struct ohci_hcd *ohci, int autostop) 548d1a243bSAlan Stern __releases(ohci->lock) 558d1a243bSAlan Stern __acquires(ohci->lock) 561da177e4SLinus Torvalds { 571da177e4SLinus Torvalds int status = 0; 588de98402SBenjamin Herrenschmidt 591da177e4SLinus Torvalds ohci->hc_control = ohci_readl (ohci, &ohci->regs->control); 601da177e4SLinus Torvalds switch (ohci->hc_control & OHCI_CTRL_HCFS) { 611da177e4SLinus Torvalds case OHCI_USB_RESUME: 621da177e4SLinus Torvalds ohci_dbg (ohci, "resume/suspend?\n"); 631da177e4SLinus Torvalds ohci->hc_control &= ~OHCI_CTRL_HCFS; 641da177e4SLinus Torvalds ohci->hc_control |= OHCI_USB_RESET; 651da177e4SLinus Torvalds ohci_writel (ohci, ohci->hc_control, &ohci->regs->control); 661da177e4SLinus Torvalds (void) ohci_readl (ohci, &ohci->regs->control); 671da177e4SLinus Torvalds /* FALL THROUGH */ 681da177e4SLinus Torvalds case OHCI_USB_RESET: 691da177e4SLinus Torvalds status = -EBUSY; 701da177e4SLinus Torvalds ohci_dbg (ohci, "needs reinit!\n"); 711da177e4SLinus Torvalds goto done; 721da177e4SLinus Torvalds case OHCI_USB_SUSPEND: 738d1a243bSAlan Stern if (!ohci->autostop) { 741da177e4SLinus Torvalds ohci_dbg (ohci, "already suspended\n"); 751da177e4SLinus Torvalds goto done; 761da177e4SLinus Torvalds } 778d1a243bSAlan Stern } 788d1a243bSAlan Stern ohci_dbg (ohci, "%s root hub\n", 798d1a243bSAlan Stern autostop ? "auto-stop" : "suspend"); 801da177e4SLinus Torvalds 811da177e4SLinus Torvalds /* First stop any processing */ 828d1a243bSAlan Stern if (!autostop && (ohci->hc_control & OHCI_SCHED_ENABLES)) { 831da177e4SLinus Torvalds ohci->hc_control &= ~OHCI_SCHED_ENABLES; 841da177e4SLinus Torvalds ohci_writel (ohci, ohci->hc_control, &ohci->regs->control); 851da177e4SLinus Torvalds ohci->hc_control = ohci_readl (ohci, &ohci->regs->control); 861da177e4SLinus Torvalds ohci_writel (ohci, OHCI_INTR_SF, &ohci->regs->intrstatus); 871da177e4SLinus Torvalds 881da177e4SLinus Torvalds /* sched disables take effect on the next frame, 891da177e4SLinus Torvalds * then the last WDH could take 6+ msec 901da177e4SLinus Torvalds */ 911da177e4SLinus Torvalds ohci_dbg (ohci, "stopping schedules ...\n"); 928d1a243bSAlan Stern ohci->autostop = 0; 938d1a243bSAlan Stern spin_unlock_irq (&ohci->lock); 948d1a243bSAlan Stern msleep (8); 958d1a243bSAlan Stern spin_lock_irq (&ohci->lock); 961da177e4SLinus Torvalds } 97*7d12e780SDavid Howells dl_done_list (ohci); 98*7d12e780SDavid Howells finish_unlinks (ohci, ohci_frame_no(ohci)); 991da177e4SLinus Torvalds 1001da177e4SLinus Torvalds /* maybe resume can wake root hub */ 1018d1a243bSAlan Stern if (device_may_wakeup(&ohci_to_hcd(ohci)->self.root_hub->dev) || 1028d1a243bSAlan Stern autostop) 1031da177e4SLinus Torvalds ohci->hc_control |= OHCI_CTRL_RWE; 1041f7e1a3bSAlan Stern else { 1051f7e1a3bSAlan Stern ohci_writel (ohci, OHCI_INTR_RHSC, &ohci->regs->intrdisable); 1061da177e4SLinus Torvalds ohci->hc_control &= ~OHCI_CTRL_RWE; 1071f7e1a3bSAlan Stern } 1081da177e4SLinus Torvalds 109f197b2c5SDavid Brownell /* Suspend hub ... this is the "global (to this bus) suspend" mode, 110f197b2c5SDavid Brownell * which doesn't imply ports will first be individually suspended. 111f197b2c5SDavid Brownell */ 1121da177e4SLinus Torvalds ohci->hc_control &= ~OHCI_CTRL_HCFS; 1131da177e4SLinus Torvalds ohci->hc_control |= OHCI_USB_SUSPEND; 1141da177e4SLinus Torvalds ohci_writel (ohci, ohci->hc_control, &ohci->regs->control); 1151da177e4SLinus Torvalds (void) ohci_readl (ohci, &ohci->regs->control); 1161da177e4SLinus Torvalds 1171da177e4SLinus Torvalds /* no resumes until devices finish suspending */ 1188d1a243bSAlan Stern if (!autostop) { 1191da177e4SLinus Torvalds ohci->next_statechange = jiffies + msecs_to_jiffies (5); 1208d1a243bSAlan Stern ohci->autostop = 0; 1218d1a243bSAlan Stern } 122d413984aSDavid Brownell 1231da177e4SLinus Torvalds done: 1241da177e4SLinus Torvalds return status; 1251da177e4SLinus Torvalds } 1261da177e4SLinus Torvalds 1271da177e4SLinus Torvalds static inline struct ed *find_head (struct ed *ed) 1281da177e4SLinus Torvalds { 1291da177e4SLinus Torvalds /* for bulk and control lists */ 1301da177e4SLinus Torvalds while (ed->ed_prev) 1311da177e4SLinus Torvalds ed = ed->ed_prev; 1321da177e4SLinus Torvalds return ed; 1331da177e4SLinus Torvalds } 1341da177e4SLinus Torvalds 1356e3ce3aeSGreg Kroah-Hartman static int ohci_restart (struct ohci_hcd *ohci); 1366e3ce3aeSGreg Kroah-Hartman 1371da177e4SLinus Torvalds /* caller has locked the root hub */ 1388d1a243bSAlan Stern static int ohci_rh_resume (struct ohci_hcd *ohci) 1398d1a243bSAlan Stern __releases(ohci->lock) 1408d1a243bSAlan Stern __acquires(ohci->lock) 1411da177e4SLinus Torvalds { 1428d1a243bSAlan Stern struct usb_hcd *hcd = ohci_to_hcd (ohci); 1431da177e4SLinus Torvalds u32 temp, enables; 1441da177e4SLinus Torvalds int status = -EINPROGRESS; 1458d1a243bSAlan Stern int autostopped = ohci->autostop; 1461da177e4SLinus Torvalds 1478d1a243bSAlan Stern ohci->autostop = 0; 1481da177e4SLinus Torvalds ohci->hc_control = ohci_readl (ohci, &ohci->regs->control); 1491da177e4SLinus Torvalds 1501da177e4SLinus Torvalds if (ohci->hc_control & (OHCI_CTRL_IR | OHCI_SCHED_ENABLES)) { 151f197b2c5SDavid Brownell /* this can happen after resuming a swsusp snapshot */ 1521da177e4SLinus Torvalds if (hcd->state == HC_STATE_RESUMING) { 1531da177e4SLinus Torvalds ohci_dbg (ohci, "BIOS/SMM active, control %03x\n", 1541da177e4SLinus Torvalds ohci->hc_control); 1551da177e4SLinus Torvalds status = -EBUSY; 1561da177e4SLinus Torvalds /* this happens when pmcore resumes HC then root */ 1571da177e4SLinus Torvalds } else { 1581da177e4SLinus Torvalds ohci_dbg (ohci, "duplicate resume\n"); 1591da177e4SLinus Torvalds status = 0; 1601da177e4SLinus Torvalds } 1611da177e4SLinus Torvalds } else switch (ohci->hc_control & OHCI_CTRL_HCFS) { 1621da177e4SLinus Torvalds case OHCI_USB_SUSPEND: 1631da177e4SLinus Torvalds ohci->hc_control &= ~(OHCI_CTRL_HCFS|OHCI_SCHED_ENABLES); 1641da177e4SLinus Torvalds ohci->hc_control |= OHCI_USB_RESUME; 1651da177e4SLinus Torvalds ohci_writel (ohci, ohci->hc_control, &ohci->regs->control); 1661da177e4SLinus Torvalds (void) ohci_readl (ohci, &ohci->regs->control); 1678d1a243bSAlan Stern ohci_dbg (ohci, "%s root hub\n", 1688d1a243bSAlan Stern autostopped ? "auto-start" : "resume"); 1691da177e4SLinus Torvalds break; 1701da177e4SLinus Torvalds case OHCI_USB_RESUME: 1711da177e4SLinus Torvalds /* HCFS changes sometime after INTR_RD */ 1721da177e4SLinus Torvalds ohci_info (ohci, "wakeup\n"); 1731da177e4SLinus Torvalds break; 1741da177e4SLinus Torvalds case OHCI_USB_OPER: 175f197b2c5SDavid Brownell /* this can happen after resuming a swsusp snapshot */ 176f197b2c5SDavid Brownell ohci_dbg (ohci, "snapshot resume? reinit\n"); 177f197b2c5SDavid Brownell status = -EBUSY; 1781da177e4SLinus Torvalds break; 1791da177e4SLinus Torvalds default: /* RESET, we lost power */ 180f197b2c5SDavid Brownell ohci_dbg (ohci, "lost power\n"); 1811da177e4SLinus Torvalds status = -EBUSY; 1821da177e4SLinus Torvalds } 1838d1a243bSAlan Stern #ifdef CONFIG_PM 1841da177e4SLinus Torvalds if (status == -EBUSY) { 1858d1a243bSAlan Stern if (!autostopped) { 1868d1a243bSAlan Stern spin_unlock_irq (&ohci->lock); 1871da177e4SLinus Torvalds (void) ohci_init (ohci); 1888d1a243bSAlan Stern status = ohci_restart (ohci); 1898d1a243bSAlan Stern spin_lock_irq (&ohci->lock); 1901da177e4SLinus Torvalds } 1918d1a243bSAlan Stern return status; 1928d1a243bSAlan Stern } 1938d1a243bSAlan Stern #endif 1941da177e4SLinus Torvalds if (status != -EINPROGRESS) 1951da177e4SLinus Torvalds return status; 1968d1a243bSAlan Stern if (autostopped) 1978d1a243bSAlan Stern goto skip_resume; 1988d1a243bSAlan Stern spin_unlock_irq (&ohci->lock); 1991da177e4SLinus Torvalds 200fdd13b36SDavid Brownell temp = ohci->num_ports; 2011da177e4SLinus Torvalds while (temp--) { 2021da177e4SLinus Torvalds u32 stat = ohci_readl (ohci, 2031da177e4SLinus Torvalds &ohci->regs->roothub.portstatus [temp]); 2041da177e4SLinus Torvalds 2051da177e4SLinus Torvalds /* force global, not selective, resume */ 2061da177e4SLinus Torvalds if (!(stat & RH_PS_PSS)) 2071da177e4SLinus Torvalds continue; 2081da177e4SLinus Torvalds ohci_writel (ohci, RH_PS_POCI, 2091da177e4SLinus Torvalds &ohci->regs->roothub.portstatus [temp]); 2101da177e4SLinus Torvalds } 2111da177e4SLinus Torvalds 2121da177e4SLinus Torvalds /* Some controllers (lucent erratum) need extra-long delays */ 213f197b2c5SDavid Brownell msleep (20 /* usb 11.5.1.10 */ + 12 /* 32 msec counter */ + 1); 2141da177e4SLinus Torvalds 2151da177e4SLinus Torvalds temp = ohci_readl (ohci, &ohci->regs->control); 2161da177e4SLinus Torvalds temp &= OHCI_CTRL_HCFS; 2171da177e4SLinus Torvalds if (temp != OHCI_USB_RESUME) { 2181da177e4SLinus Torvalds ohci_err (ohci, "controller won't resume\n"); 2191da177e4SLinus Torvalds return -EBUSY; 2201da177e4SLinus Torvalds } 2211da177e4SLinus Torvalds 2221da177e4SLinus Torvalds /* disable old schedule state, reinit from scratch */ 2231da177e4SLinus Torvalds ohci_writel (ohci, 0, &ohci->regs->ed_controlhead); 2241da177e4SLinus Torvalds ohci_writel (ohci, 0, &ohci->regs->ed_controlcurrent); 2251da177e4SLinus Torvalds ohci_writel (ohci, 0, &ohci->regs->ed_bulkhead); 2261da177e4SLinus Torvalds ohci_writel (ohci, 0, &ohci->regs->ed_bulkcurrent); 2271da177e4SLinus Torvalds ohci_writel (ohci, 0, &ohci->regs->ed_periodcurrent); 2281da177e4SLinus Torvalds ohci_writel (ohci, (u32) ohci->hcca_dma, &ohci->regs->hcca); 2291da177e4SLinus Torvalds 2301da177e4SLinus Torvalds /* Sometimes PCI D3 suspend trashes frame timings ... */ 2311da177e4SLinus Torvalds periodic_reinit (ohci); 2321da177e4SLinus Torvalds 2338d1a243bSAlan Stern /* the following code is executed with ohci->lock held and 2348d1a243bSAlan Stern * irqs disabled if and only if autostopped is true 2358d1a243bSAlan Stern */ 2368d1a243bSAlan Stern 2378d1a243bSAlan Stern skip_resume: 2381da177e4SLinus Torvalds /* interrupts might have been disabled */ 2391da177e4SLinus Torvalds ohci_writel (ohci, OHCI_INTR_INIT, &ohci->regs->intrenable); 2401da177e4SLinus Torvalds if (ohci->ed_rm_list) 2411da177e4SLinus Torvalds ohci_writel (ohci, OHCI_INTR_SF, &ohci->regs->intrenable); 2421da177e4SLinus Torvalds 2431da177e4SLinus Torvalds /* Then re-enable operations */ 2441da177e4SLinus Torvalds ohci_writel (ohci, OHCI_USB_OPER, &ohci->regs->control); 2451da177e4SLinus Torvalds (void) ohci_readl (ohci, &ohci->regs->control); 2468d1a243bSAlan Stern if (!autostopped) 2471da177e4SLinus Torvalds msleep (3); 2481da177e4SLinus Torvalds 2496a9062f3SDavid Brownell temp = ohci->hc_control; 2506a9062f3SDavid Brownell temp &= OHCI_CTRL_RWC; 2516a9062f3SDavid Brownell temp |= OHCI_CONTROL_INIT | OHCI_USB_OPER; 2521da177e4SLinus Torvalds ohci->hc_control = temp; 2531da177e4SLinus Torvalds ohci_writel (ohci, temp, &ohci->regs->control); 2541da177e4SLinus Torvalds (void) ohci_readl (ohci, &ohci->regs->control); 2551da177e4SLinus Torvalds 2561da177e4SLinus Torvalds /* TRSMRCY */ 2578d1a243bSAlan Stern if (!autostopped) { 2581da177e4SLinus Torvalds msleep (10); 2598d1a243bSAlan Stern spin_lock_irq (&ohci->lock); 2608d1a243bSAlan Stern } 2618d1a243bSAlan Stern /* now ohci->lock is always held and irqs are always disabled */ 2621da177e4SLinus Torvalds 263d413984aSDavid Brownell /* keep it alive for more than ~5x suspend + resume costs */ 264d413984aSDavid Brownell ohci->next_statechange = jiffies + STATECHANGE_DELAY; 2651da177e4SLinus Torvalds 2661da177e4SLinus Torvalds /* maybe turn schedules back on */ 2671da177e4SLinus Torvalds enables = 0; 2681da177e4SLinus Torvalds temp = 0; 2691da177e4SLinus Torvalds if (!ohci->ed_rm_list) { 2701da177e4SLinus Torvalds if (ohci->ed_controltail) { 2711da177e4SLinus Torvalds ohci_writel (ohci, 2721da177e4SLinus Torvalds find_head (ohci->ed_controltail)->dma, 2731da177e4SLinus Torvalds &ohci->regs->ed_controlhead); 2741da177e4SLinus Torvalds enables |= OHCI_CTRL_CLE; 2751da177e4SLinus Torvalds temp |= OHCI_CLF; 2761da177e4SLinus Torvalds } 2771da177e4SLinus Torvalds if (ohci->ed_bulktail) { 2781da177e4SLinus Torvalds ohci_writel (ohci, find_head (ohci->ed_bulktail)->dma, 2791da177e4SLinus Torvalds &ohci->regs->ed_bulkhead); 2801da177e4SLinus Torvalds enables |= OHCI_CTRL_BLE; 2811da177e4SLinus Torvalds temp |= OHCI_BLF; 2821da177e4SLinus Torvalds } 2831da177e4SLinus Torvalds } 2841da177e4SLinus Torvalds if (hcd->self.bandwidth_isoc_reqs || hcd->self.bandwidth_int_reqs) 2851da177e4SLinus Torvalds enables |= OHCI_CTRL_PLE|OHCI_CTRL_IE; 2861da177e4SLinus Torvalds if (enables) { 2871da177e4SLinus Torvalds ohci_dbg (ohci, "restarting schedules ... %08x\n", enables); 2881da177e4SLinus Torvalds ohci->hc_control |= enables; 2891da177e4SLinus Torvalds ohci_writel (ohci, ohci->hc_control, &ohci->regs->control); 2901da177e4SLinus Torvalds if (temp) 2911da177e4SLinus Torvalds ohci_writel (ohci, temp, &ohci->regs->cmdstatus); 2921da177e4SLinus Torvalds (void) ohci_readl (ohci, &ohci->regs->control); 2931da177e4SLinus Torvalds } 2941da177e4SLinus Torvalds 2951da177e4SLinus Torvalds return 0; 2961da177e4SLinus Torvalds } 2971da177e4SLinus Torvalds 2988d1a243bSAlan Stern #ifdef CONFIG_PM 2998d1a243bSAlan Stern 3008d1a243bSAlan Stern static int ohci_bus_suspend (struct usb_hcd *hcd) 3018d1a243bSAlan Stern { 3028d1a243bSAlan Stern struct ohci_hcd *ohci = hcd_to_ohci (hcd); 3038d1a243bSAlan Stern int rc; 3048d1a243bSAlan Stern 3058d1a243bSAlan Stern spin_lock_irq (&ohci->lock); 3068d1a243bSAlan Stern 3078d1a243bSAlan Stern if (unlikely(!test_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags))) 3088d1a243bSAlan Stern rc = -ESHUTDOWN; 3098d1a243bSAlan Stern else 3108d1a243bSAlan Stern rc = ohci_rh_suspend (ohci, 0); 3118d1a243bSAlan Stern spin_unlock_irq (&ohci->lock); 3128d1a243bSAlan Stern return rc; 3138d1a243bSAlan Stern } 3148d1a243bSAlan Stern 3158d1a243bSAlan Stern static int ohci_bus_resume (struct usb_hcd *hcd) 3168d1a243bSAlan Stern { 3178d1a243bSAlan Stern struct ohci_hcd *ohci = hcd_to_ohci (hcd); 3188d1a243bSAlan Stern int rc; 3198d1a243bSAlan Stern 3208d1a243bSAlan Stern if (time_before (jiffies, ohci->next_statechange)) 3218d1a243bSAlan Stern msleep(5); 3228d1a243bSAlan Stern 3238d1a243bSAlan Stern spin_lock_irq (&ohci->lock); 3248d1a243bSAlan Stern 3258d1a243bSAlan Stern if (unlikely(!test_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags))) 3268d1a243bSAlan Stern rc = -ESHUTDOWN; 3278d1a243bSAlan Stern else 3288d1a243bSAlan Stern rc = ohci_rh_resume (ohci); 3298d1a243bSAlan Stern spin_unlock_irq (&ohci->lock); 3308d1a243bSAlan Stern 3318d1a243bSAlan Stern /* poll until we know a device is connected or we autostop */ 3328d1a243bSAlan Stern if (rc == 0) 3338d1a243bSAlan Stern usb_hcd_poll_rh_status(hcd); 3348d1a243bSAlan Stern return rc; 3358d1a243bSAlan Stern } 3368d1a243bSAlan Stern 3378ad7fe16SDavid Brownell #endif /* CONFIG_PM */ 3381da177e4SLinus Torvalds 3391da177e4SLinus Torvalds /*-------------------------------------------------------------------------*/ 3401da177e4SLinus Torvalds 3411da177e4SLinus Torvalds /* build "status change" packet (one or two bytes) from HC registers */ 3421da177e4SLinus Torvalds 3431da177e4SLinus Torvalds static int 3441da177e4SLinus Torvalds ohci_hub_status_data (struct usb_hcd *hcd, char *buf) 3451da177e4SLinus Torvalds { 3461da177e4SLinus Torvalds struct ohci_hcd *ohci = hcd_to_ohci (hcd); 347fdd13b36SDavid Brownell int i, changed = 0, length = 1; 3488d1a243bSAlan Stern int any_connected = 0, rhsc_enabled = 1; 3491da177e4SLinus Torvalds unsigned long flags; 3501da177e4SLinus Torvalds 3511da177e4SLinus Torvalds spin_lock_irqsave (&ohci->lock, flags); 3521da177e4SLinus Torvalds 353fdd13b36SDavid Brownell /* undocumented erratum seen on at least rev D */ 354fdd13b36SDavid Brownell if ((ohci->flags & OHCI_QUIRK_AMD756) 355fdd13b36SDavid Brownell && (roothub_a (ohci) & RH_A_NDP) > MAX_ROOT_PORTS) { 356fdd13b36SDavid Brownell ohci_warn (ohci, "bogus NDP, rereads as NDP=%d\n", 3571da177e4SLinus Torvalds ohci_readl (ohci, &ohci->regs->roothub.a) & RH_A_NDP); 3581da177e4SLinus Torvalds /* retry later; "should not happen" */ 3591da177e4SLinus Torvalds goto done; 3601da177e4SLinus Torvalds } 3611da177e4SLinus Torvalds 3621da177e4SLinus Torvalds /* init status */ 3631da177e4SLinus Torvalds if (roothub_status (ohci) & (RH_HS_LPSC | RH_HS_OCIC)) 3641da177e4SLinus Torvalds buf [0] = changed = 1; 3651da177e4SLinus Torvalds else 3661da177e4SLinus Torvalds buf [0] = 0; 367fdd13b36SDavid Brownell if (ohci->num_ports > 7) { 3681da177e4SLinus Torvalds buf [1] = 0; 3691da177e4SLinus Torvalds length++; 3701da177e4SLinus Torvalds } 3711da177e4SLinus Torvalds 3721da177e4SLinus Torvalds /* look at each port */ 373fdd13b36SDavid Brownell for (i = 0; i < ohci->num_ports; i++) { 3741da177e4SLinus Torvalds u32 status = roothub_portstatus (ohci, i); 3751da177e4SLinus Torvalds 3768d1a243bSAlan Stern /* can't autostop if ports are connected */ 3778d1a243bSAlan Stern any_connected |= (status & RH_PS_CCS); 3788d1a243bSAlan Stern 3791da177e4SLinus Torvalds if (status & (RH_PS_CSC | RH_PS_PESC | RH_PS_PSSC 3801da177e4SLinus Torvalds | RH_PS_OCIC | RH_PS_PRSC)) { 3811da177e4SLinus Torvalds changed = 1; 3821da177e4SLinus Torvalds if (i < 7) 3831da177e4SLinus Torvalds buf [0] |= 1 << (i + 1); 3841da177e4SLinus Torvalds else 3851da177e4SLinus Torvalds buf [1] |= 1 << (i - 7); 3861da177e4SLinus Torvalds } 3871da177e4SLinus Torvalds } 388d413984aSDavid Brownell 3898d1a243bSAlan Stern /* NOTE: vendors didn't always make the same implementation 3908d1a243bSAlan Stern * choices for RHSC. Sometimes it triggers on an edge (like 3918d1a243bSAlan Stern * setting and maybe clearing a port status change bit); and 3928d1a243bSAlan Stern * it's level-triggered on other silicon, active until khubd 3938d1a243bSAlan Stern * clears all active port status change bits. If it's still 3948d1a243bSAlan Stern * set (level-triggered) we must disable it and rely on 3958d1a243bSAlan Stern * polling until khubd re-enables it. 396d413984aSDavid Brownell */ 3978d1a243bSAlan Stern if (ohci_readl (ohci, &ohci->regs->intrstatus) & OHCI_INTR_RHSC) { 3988d1a243bSAlan Stern ohci_writel (ohci, OHCI_INTR_RHSC, &ohci->regs->intrdisable); 3998d1a243bSAlan Stern (void) ohci_readl (ohci, &ohci->regs->intrdisable); 4008d1a243bSAlan Stern rhsc_enabled = 0; 4018d1a243bSAlan Stern } 4028d1a243bSAlan Stern hcd->poll_rh = 1; 4038d1a243bSAlan Stern 4048d1a243bSAlan Stern /* carry out appropriate state changes */ 4058d1a243bSAlan Stern switch (ohci->hc_control & OHCI_CTRL_HCFS) { 4068d1a243bSAlan Stern 4078d1a243bSAlan Stern case OHCI_USB_OPER: 4088d1a243bSAlan Stern /* keep on polling until we know a device is connected 4098d1a243bSAlan Stern * and RHSC is enabled */ 4108d1a243bSAlan Stern if (!ohci->autostop) { 4118d1a243bSAlan Stern if (any_connected) { 4128d1a243bSAlan Stern if (rhsc_enabled) 4138d1a243bSAlan Stern hcd->poll_rh = 0; 4148d1a243bSAlan Stern } else { 4158d1a243bSAlan Stern ohci->autostop = 1; 4168d1a243bSAlan Stern ohci->next_statechange = jiffies + HZ; 4178d1a243bSAlan Stern } 4188d1a243bSAlan Stern 4198d1a243bSAlan Stern /* if no devices have been attached for one second, autostop */ 4208d1a243bSAlan Stern } else { 4218d1a243bSAlan Stern if (changed || any_connected) { 4228d1a243bSAlan Stern ohci->autostop = 0; 4238d1a243bSAlan Stern ohci->next_statechange = jiffies + 4248d1a243bSAlan Stern STATECHANGE_DELAY; 4258d1a243bSAlan Stern } else if (time_after_eq (jiffies, 4268d1a243bSAlan Stern ohci->next_statechange) 4278d1a243bSAlan Stern && !ohci->ed_rm_list 4288d1a243bSAlan Stern && !(ohci->hc_control & 4298d1a243bSAlan Stern OHCI_SCHED_ENABLES)) { 4308d1a243bSAlan Stern ohci_rh_suspend (ohci, 1); 4318d1a243bSAlan Stern } 4328d1a243bSAlan Stern } 4338d1a243bSAlan Stern break; 4348d1a243bSAlan Stern 4358d1a243bSAlan Stern /* if there is a port change, autostart or ask to be resumed */ 4368d1a243bSAlan Stern case OHCI_USB_SUSPEND: 4378d1a243bSAlan Stern case OHCI_USB_RESUME: 4388d1a243bSAlan Stern if (changed) { 4398d1a243bSAlan Stern if (ohci->autostop) 4408d1a243bSAlan Stern ohci_rh_resume (ohci); 4418d1a243bSAlan Stern else 4428d1a243bSAlan Stern usb_hcd_resume_root_hub (hcd); 4438d1a243bSAlan Stern } else { 4448d1a243bSAlan Stern /* everything is idle, no need for polling */ 4458d1a243bSAlan Stern hcd->poll_rh = 0; 4468d1a243bSAlan Stern } 4478d1a243bSAlan Stern break; 4488d1a243bSAlan Stern } 449d413984aSDavid Brownell 4501da177e4SLinus Torvalds done: 4511da177e4SLinus Torvalds spin_unlock_irqrestore (&ohci->lock, flags); 4521da177e4SLinus Torvalds 4531da177e4SLinus Torvalds return changed ? length : 0; 4541da177e4SLinus Torvalds } 4551da177e4SLinus Torvalds 4561da177e4SLinus Torvalds /*-------------------------------------------------------------------------*/ 4571da177e4SLinus Torvalds 4581da177e4SLinus Torvalds static void 4591da177e4SLinus Torvalds ohci_hub_descriptor ( 4601da177e4SLinus Torvalds struct ohci_hcd *ohci, 4611da177e4SLinus Torvalds struct usb_hub_descriptor *desc 4621da177e4SLinus Torvalds ) { 4631da177e4SLinus Torvalds u32 rh = roothub_a (ohci); 4641da177e4SLinus Torvalds u16 temp; 4651da177e4SLinus Torvalds 4661da177e4SLinus Torvalds desc->bDescriptorType = 0x29; 4671da177e4SLinus Torvalds desc->bPwrOn2PwrGood = (rh & RH_A_POTPGT) >> 24; 4681da177e4SLinus Torvalds desc->bHubContrCurrent = 0; 4691da177e4SLinus Torvalds 470fdd13b36SDavid Brownell desc->bNbrPorts = ohci->num_ports; 471fdd13b36SDavid Brownell temp = 1 + (ohci->num_ports / 8); 4721da177e4SLinus Torvalds desc->bDescLength = 7 + 2 * temp; 4731da177e4SLinus Torvalds 4741da177e4SLinus Torvalds temp = 0; 4751da177e4SLinus Torvalds if (rh & RH_A_NPS) /* no power switching? */ 4761da177e4SLinus Torvalds temp |= 0x0002; 4771da177e4SLinus Torvalds if (rh & RH_A_PSM) /* per-port power switching? */ 4781da177e4SLinus Torvalds temp |= 0x0001; 4791da177e4SLinus Torvalds if (rh & RH_A_NOCP) /* no overcurrent reporting? */ 4801da177e4SLinus Torvalds temp |= 0x0010; 4811da177e4SLinus Torvalds else if (rh & RH_A_OCPM) /* per-port overcurrent reporting? */ 4821da177e4SLinus Torvalds temp |= 0x0008; 4831da177e4SLinus Torvalds desc->wHubCharacteristics = (__force __u16)cpu_to_hc16(ohci, temp); 4841da177e4SLinus Torvalds 4851da177e4SLinus Torvalds /* two bitmaps: ports removable, and usb 1.0 legacy PortPwrCtrlMask */ 4861da177e4SLinus Torvalds rh = roothub_b (ohci); 487b2134bcdSKAMBAROV, ZAUR memset(desc->bitmap, 0xff, sizeof(desc->bitmap)); 4881da177e4SLinus Torvalds desc->bitmap [0] = rh & RH_B_DR; 489fdd13b36SDavid Brownell if (ohci->num_ports > 7) { 4901da177e4SLinus Torvalds desc->bitmap [1] = (rh & RH_B_DR) >> 8; 491b2134bcdSKAMBAROV, ZAUR desc->bitmap [2] = 0xff; 4921da177e4SLinus Torvalds } else 4931da177e4SLinus Torvalds desc->bitmap [1] = 0xff; 4941da177e4SLinus Torvalds } 4951da177e4SLinus Torvalds 4961da177e4SLinus Torvalds /*-------------------------------------------------------------------------*/ 4971da177e4SLinus Torvalds 4981da177e4SLinus Torvalds #ifdef CONFIG_USB_OTG 4991da177e4SLinus Torvalds 5001da177e4SLinus Torvalds static int ohci_start_port_reset (struct usb_hcd *hcd, unsigned port) 5011da177e4SLinus Torvalds { 5021da177e4SLinus Torvalds struct ohci_hcd *ohci = hcd_to_ohci (hcd); 5031da177e4SLinus Torvalds u32 status; 5041da177e4SLinus Torvalds 5051da177e4SLinus Torvalds if (!port) 5061da177e4SLinus Torvalds return -EINVAL; 5071da177e4SLinus Torvalds port--; 5081da177e4SLinus Torvalds 5091da177e4SLinus Torvalds /* start port reset before HNP protocol times out */ 5101da177e4SLinus Torvalds status = ohci_readl(ohci, &ohci->regs->roothub.portstatus [port]); 5111da177e4SLinus Torvalds if (!(status & RH_PS_CCS)) 5121da177e4SLinus Torvalds return -ENODEV; 5131da177e4SLinus Torvalds 5141da177e4SLinus Torvalds /* khubd will finish the reset later */ 5151da177e4SLinus Torvalds ohci_writel(ohci, RH_PS_PRS, &ohci->regs->roothub.portstatus [port]); 5161da177e4SLinus Torvalds return 0; 5171da177e4SLinus Torvalds } 5181da177e4SLinus Torvalds 5191da177e4SLinus Torvalds static void start_hnp(struct ohci_hcd *ohci); 5201da177e4SLinus Torvalds 5211da177e4SLinus Torvalds #else 5221da177e4SLinus Torvalds 5231da177e4SLinus Torvalds #define ohci_start_port_reset NULL 5241da177e4SLinus Torvalds 5251da177e4SLinus Torvalds #endif 5261da177e4SLinus Torvalds 5271da177e4SLinus Torvalds /*-------------------------------------------------------------------------*/ 5281da177e4SLinus Torvalds 5291da177e4SLinus Torvalds 5301da177e4SLinus Torvalds /* See usb 7.1.7.5: root hubs must issue at least 50 msec reset signaling, 5311da177e4SLinus Torvalds * not necessarily continuous ... to guard against resume signaling. 5321da177e4SLinus Torvalds * The short timeout is safe for non-root hubs, and is backward-compatible 5331da177e4SLinus Torvalds * with earlier Linux hosts. 5341da177e4SLinus Torvalds */ 5351da177e4SLinus Torvalds #ifdef CONFIG_USB_SUSPEND 5361da177e4SLinus Torvalds #define PORT_RESET_MSEC 50 5371da177e4SLinus Torvalds #else 5381da177e4SLinus Torvalds #define PORT_RESET_MSEC 10 5391da177e4SLinus Torvalds #endif 5401da177e4SLinus Torvalds 5411da177e4SLinus Torvalds /* this timer value might be vendor-specific ... */ 5421da177e4SLinus Torvalds #define PORT_RESET_HW_MSEC 10 5431da177e4SLinus Torvalds 5441da177e4SLinus Torvalds /* wrap-aware logic morphed from <linux/jiffies.h> */ 5451da177e4SLinus Torvalds #define tick_before(t1,t2) ((s16)(((s16)(t1))-((s16)(t2))) < 0) 5461da177e4SLinus Torvalds 5471da177e4SLinus Torvalds /* called from some task, normally khubd */ 5481da177e4SLinus Torvalds static inline void root_port_reset (struct ohci_hcd *ohci, unsigned port) 5491da177e4SLinus Torvalds { 5501da177e4SLinus Torvalds __hc32 __iomem *portstat = &ohci->regs->roothub.portstatus [port]; 5511da177e4SLinus Torvalds u32 temp; 5521da177e4SLinus Torvalds u16 now = ohci_readl(ohci, &ohci->regs->fmnumber); 5531da177e4SLinus Torvalds u16 reset_done = now + PORT_RESET_MSEC; 5541da177e4SLinus Torvalds 5551da177e4SLinus Torvalds /* build a "continuous enough" reset signal, with up to 5561da177e4SLinus Torvalds * 3msec gap between pulses. scheduler HZ==100 must work; 5571da177e4SLinus Torvalds * this might need to be deadline-scheduled. 5581da177e4SLinus Torvalds */ 5591da177e4SLinus Torvalds do { 5601da177e4SLinus Torvalds /* spin until any current reset finishes */ 5611da177e4SLinus Torvalds for (;;) { 5621da177e4SLinus Torvalds temp = ohci_readl (ohci, portstat); 5631da177e4SLinus Torvalds if (!(temp & RH_PS_PRS)) 5641da177e4SLinus Torvalds break; 5651da177e4SLinus Torvalds udelay (500); 5661da177e4SLinus Torvalds } 5671da177e4SLinus Torvalds 5681da177e4SLinus Torvalds if (!(temp & RH_PS_CCS)) 5691da177e4SLinus Torvalds break; 5701da177e4SLinus Torvalds if (temp & RH_PS_PRSC) 5711da177e4SLinus Torvalds ohci_writel (ohci, RH_PS_PRSC, portstat); 5721da177e4SLinus Torvalds 5731da177e4SLinus Torvalds /* start the next reset, sleep till it's probably done */ 5741da177e4SLinus Torvalds ohci_writel (ohci, RH_PS_PRS, portstat); 5751da177e4SLinus Torvalds msleep(PORT_RESET_HW_MSEC); 5761da177e4SLinus Torvalds now = ohci_readl(ohci, &ohci->regs->fmnumber); 5771da177e4SLinus Torvalds } while (tick_before(now, reset_done)); 5781da177e4SLinus Torvalds /* caller synchronizes using PRSC */ 5791da177e4SLinus Torvalds } 5801da177e4SLinus Torvalds 5811da177e4SLinus Torvalds static int ohci_hub_control ( 5821da177e4SLinus Torvalds struct usb_hcd *hcd, 5831da177e4SLinus Torvalds u16 typeReq, 5841da177e4SLinus Torvalds u16 wValue, 5851da177e4SLinus Torvalds u16 wIndex, 5861da177e4SLinus Torvalds char *buf, 5871da177e4SLinus Torvalds u16 wLength 5881da177e4SLinus Torvalds ) { 5891da177e4SLinus Torvalds struct ohci_hcd *ohci = hcd_to_ohci (hcd); 5901da177e4SLinus Torvalds int ports = hcd_to_bus (hcd)->root_hub->maxchild; 5911da177e4SLinus Torvalds u32 temp; 5921da177e4SLinus Torvalds int retval = 0; 5931da177e4SLinus Torvalds 5948de98402SBenjamin Herrenschmidt if (unlikely(!test_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags))) 5958de98402SBenjamin Herrenschmidt return -ESHUTDOWN; 5968de98402SBenjamin Herrenschmidt 5971da177e4SLinus Torvalds switch (typeReq) { 5981da177e4SLinus Torvalds case ClearHubFeature: 5991da177e4SLinus Torvalds switch (wValue) { 6001da177e4SLinus Torvalds case C_HUB_OVER_CURRENT: 6011da177e4SLinus Torvalds ohci_writel (ohci, RH_HS_OCIC, 6021da177e4SLinus Torvalds &ohci->regs->roothub.status); 6031da177e4SLinus Torvalds case C_HUB_LOCAL_POWER: 6041da177e4SLinus Torvalds break; 6051da177e4SLinus Torvalds default: 6061da177e4SLinus Torvalds goto error; 6071da177e4SLinus Torvalds } 6081da177e4SLinus Torvalds break; 6091da177e4SLinus Torvalds case ClearPortFeature: 6101da177e4SLinus Torvalds if (!wIndex || wIndex > ports) 6111da177e4SLinus Torvalds goto error; 6121da177e4SLinus Torvalds wIndex--; 6131da177e4SLinus Torvalds 6141da177e4SLinus Torvalds switch (wValue) { 6151da177e4SLinus Torvalds case USB_PORT_FEAT_ENABLE: 6161da177e4SLinus Torvalds temp = RH_PS_CCS; 6171da177e4SLinus Torvalds break; 6181da177e4SLinus Torvalds case USB_PORT_FEAT_C_ENABLE: 6191da177e4SLinus Torvalds temp = RH_PS_PESC; 6201da177e4SLinus Torvalds break; 6211da177e4SLinus Torvalds case USB_PORT_FEAT_SUSPEND: 6221da177e4SLinus Torvalds temp = RH_PS_POCI; 6231da177e4SLinus Torvalds break; 6241da177e4SLinus Torvalds case USB_PORT_FEAT_C_SUSPEND: 6251da177e4SLinus Torvalds temp = RH_PS_PSSC; 6261da177e4SLinus Torvalds break; 6271da177e4SLinus Torvalds case USB_PORT_FEAT_POWER: 6281da177e4SLinus Torvalds temp = RH_PS_LSDA; 6291da177e4SLinus Torvalds break; 6301da177e4SLinus Torvalds case USB_PORT_FEAT_C_CONNECTION: 6311da177e4SLinus Torvalds temp = RH_PS_CSC; 6321da177e4SLinus Torvalds break; 6331da177e4SLinus Torvalds case USB_PORT_FEAT_C_OVER_CURRENT: 6341da177e4SLinus Torvalds temp = RH_PS_OCIC; 6351da177e4SLinus Torvalds break; 6361da177e4SLinus Torvalds case USB_PORT_FEAT_C_RESET: 6371da177e4SLinus Torvalds temp = RH_PS_PRSC; 6381da177e4SLinus Torvalds break; 6391da177e4SLinus Torvalds default: 6401da177e4SLinus Torvalds goto error; 6411da177e4SLinus Torvalds } 6421da177e4SLinus Torvalds ohci_writel (ohci, temp, 6431da177e4SLinus Torvalds &ohci->regs->roothub.portstatus [wIndex]); 6441da177e4SLinus Torvalds // ohci_readl (ohci, &ohci->regs->roothub.portstatus [wIndex]); 6451da177e4SLinus Torvalds break; 6461da177e4SLinus Torvalds case GetHubDescriptor: 6471da177e4SLinus Torvalds ohci_hub_descriptor (ohci, (struct usb_hub_descriptor *) buf); 6481da177e4SLinus Torvalds break; 6491da177e4SLinus Torvalds case GetHubStatus: 6501da177e4SLinus Torvalds temp = roothub_status (ohci) & ~(RH_HS_CRWE | RH_HS_DRWE); 65192164c5dSDavid Miller put_unaligned(cpu_to_le32 (temp), (__le32 *) buf); 6521da177e4SLinus Torvalds break; 6531da177e4SLinus Torvalds case GetPortStatus: 6541da177e4SLinus Torvalds if (!wIndex || wIndex > ports) 6551da177e4SLinus Torvalds goto error; 6561da177e4SLinus Torvalds wIndex--; 6571da177e4SLinus Torvalds temp = roothub_portstatus (ohci, wIndex); 65892164c5dSDavid Miller put_unaligned(cpu_to_le32 (temp), (__le32 *) buf); 6591da177e4SLinus Torvalds 6601da177e4SLinus Torvalds #ifndef OHCI_VERBOSE_DEBUG 6611da177e4SLinus Torvalds if (*(u16*)(buf+2)) /* only if wPortChange is interesting */ 6621da177e4SLinus Torvalds #endif 6631da177e4SLinus Torvalds dbg_port (ohci, "GetStatus", wIndex, temp); 6641da177e4SLinus Torvalds break; 6651da177e4SLinus Torvalds case SetHubFeature: 6661da177e4SLinus Torvalds switch (wValue) { 6671da177e4SLinus Torvalds case C_HUB_OVER_CURRENT: 6681da177e4SLinus Torvalds // FIXME: this can be cleared, yes? 6691da177e4SLinus Torvalds case C_HUB_LOCAL_POWER: 6701da177e4SLinus Torvalds break; 6711da177e4SLinus Torvalds default: 6721da177e4SLinus Torvalds goto error; 6731da177e4SLinus Torvalds } 6741da177e4SLinus Torvalds break; 6751da177e4SLinus Torvalds case SetPortFeature: 6761da177e4SLinus Torvalds if (!wIndex || wIndex > ports) 6771da177e4SLinus Torvalds goto error; 6781da177e4SLinus Torvalds wIndex--; 6791da177e4SLinus Torvalds switch (wValue) { 6801da177e4SLinus Torvalds case USB_PORT_FEAT_SUSPEND: 6811da177e4SLinus Torvalds #ifdef CONFIG_USB_OTG 6821da177e4SLinus Torvalds if (hcd->self.otg_port == (wIndex + 1) 6831da177e4SLinus Torvalds && hcd->self.b_hnp_enable) 6841da177e4SLinus Torvalds start_hnp(ohci); 6851da177e4SLinus Torvalds else 6861da177e4SLinus Torvalds #endif 6871da177e4SLinus Torvalds ohci_writel (ohci, RH_PS_PSS, 6881da177e4SLinus Torvalds &ohci->regs->roothub.portstatus [wIndex]); 6891da177e4SLinus Torvalds break; 6901da177e4SLinus Torvalds case USB_PORT_FEAT_POWER: 6911da177e4SLinus Torvalds ohci_writel (ohci, RH_PS_PPS, 6921da177e4SLinus Torvalds &ohci->regs->roothub.portstatus [wIndex]); 6931da177e4SLinus Torvalds break; 6941da177e4SLinus Torvalds case USB_PORT_FEAT_RESET: 6951da177e4SLinus Torvalds root_port_reset (ohci, wIndex); 6961da177e4SLinus Torvalds break; 6971da177e4SLinus Torvalds default: 6981da177e4SLinus Torvalds goto error; 6991da177e4SLinus Torvalds } 7001da177e4SLinus Torvalds break; 7011da177e4SLinus Torvalds 7021da177e4SLinus Torvalds default: 7031da177e4SLinus Torvalds error: 7041da177e4SLinus Torvalds /* "protocol stall" on error */ 7051da177e4SLinus Torvalds retval = -EPIPE; 7061da177e4SLinus Torvalds } 7071da177e4SLinus Torvalds return retval; 7081da177e4SLinus Torvalds } 7091da177e4SLinus Torvalds 710