xref: /linux/drivers/usb/host/ohci-hub.c (revision da13051cc756756f10b2da8ea97b05bdf84bd7bb)
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 
391da177e4SLinus Torvalds #define OHCI_SCHED_ENABLES \
401da177e4SLinus Torvalds 	(OHCI_CTRL_CLE|OHCI_CTRL_BLE|OHCI_CTRL_PLE|OHCI_CTRL_IE)
411da177e4SLinus Torvalds 
427d12e780SDavid Howells static void dl_done_list (struct ohci_hcd *);
437d12e780SDavid Howells static void finish_unlinks (struct ohci_hcd *, u16);
441da177e4SLinus Torvalds 
45b1878440SAlan Stern #ifdef	CONFIG_PM
468d1a243bSAlan Stern static int ohci_rh_suspend (struct ohci_hcd *ohci, int autostop)
478d1a243bSAlan Stern __releases(ohci->lock)
488d1a243bSAlan Stern __acquires(ohci->lock)
491da177e4SLinus Torvalds {
501da177e4SLinus Torvalds 	int			status = 0;
518de98402SBenjamin Herrenschmidt 
521da177e4SLinus Torvalds 	ohci->hc_control = ohci_readl (ohci, &ohci->regs->control);
531da177e4SLinus Torvalds 	switch (ohci->hc_control & OHCI_CTRL_HCFS) {
541da177e4SLinus Torvalds 	case OHCI_USB_RESUME:
551da177e4SLinus Torvalds 		ohci_dbg (ohci, "resume/suspend?\n");
561da177e4SLinus Torvalds 		ohci->hc_control &= ~OHCI_CTRL_HCFS;
571da177e4SLinus Torvalds 		ohci->hc_control |= OHCI_USB_RESET;
581da177e4SLinus Torvalds 		ohci_writel (ohci, ohci->hc_control, &ohci->regs->control);
591da177e4SLinus Torvalds 		(void) ohci_readl (ohci, &ohci->regs->control);
601da177e4SLinus Torvalds 		/* FALL THROUGH */
611da177e4SLinus Torvalds 	case OHCI_USB_RESET:
621da177e4SLinus Torvalds 		status = -EBUSY;
631da177e4SLinus Torvalds 		ohci_dbg (ohci, "needs reinit!\n");
641da177e4SLinus Torvalds 		goto done;
651da177e4SLinus Torvalds 	case OHCI_USB_SUSPEND:
668d1a243bSAlan Stern 		if (!ohci->autostop) {
671da177e4SLinus Torvalds 			ohci_dbg (ohci, "already suspended\n");
681da177e4SLinus Torvalds 			goto done;
691da177e4SLinus Torvalds 		}
708d1a243bSAlan Stern 	}
718d1a243bSAlan Stern 	ohci_dbg (ohci, "%s root hub\n",
728d1a243bSAlan Stern 			autostop ? "auto-stop" : "suspend");
731da177e4SLinus Torvalds 
741da177e4SLinus Torvalds 	/* First stop any processing */
758d1a243bSAlan Stern 	if (!autostop && (ohci->hc_control & OHCI_SCHED_ENABLES)) {
761da177e4SLinus Torvalds 		ohci->hc_control &= ~OHCI_SCHED_ENABLES;
771da177e4SLinus Torvalds 		ohci_writel (ohci, ohci->hc_control, &ohci->regs->control);
781da177e4SLinus Torvalds 		ohci->hc_control = ohci_readl (ohci, &ohci->regs->control);
791da177e4SLinus Torvalds 		ohci_writel (ohci, OHCI_INTR_SF, &ohci->regs->intrstatus);
801da177e4SLinus Torvalds 
811da177e4SLinus Torvalds 		/* sched disables take effect on the next frame,
821da177e4SLinus Torvalds 		 * then the last WDH could take 6+ msec
831da177e4SLinus Torvalds 		 */
841da177e4SLinus Torvalds 		ohci_dbg (ohci, "stopping schedules ...\n");
858d1a243bSAlan Stern 		ohci->autostop = 0;
868d1a243bSAlan Stern 		spin_unlock_irq (&ohci->lock);
878d1a243bSAlan Stern 		msleep (8);
888d1a243bSAlan Stern 		spin_lock_irq (&ohci->lock);
891da177e4SLinus Torvalds 	}
907d12e780SDavid Howells 	dl_done_list (ohci);
917d12e780SDavid Howells 	finish_unlinks (ohci, ohci_frame_no(ohci));
921da177e4SLinus Torvalds 
931da177e4SLinus Torvalds 	/* maybe resume can wake root hub */
9458a97ffeSAlan Stern 	if (ohci_to_hcd(ohci)->self.root_hub->do_remote_wakeup || autostop) {
951da177e4SLinus Torvalds 		ohci->hc_control |= OHCI_CTRL_RWE;
9658a97ffeSAlan Stern 	} else {
975f47493cSAlan Stern 		ohci_writel(ohci, OHCI_INTR_RHSC | OHCI_INTR_RD,
985f47493cSAlan Stern 				&ohci->regs->intrdisable);
991da177e4SLinus Torvalds 		ohci->hc_control &= ~OHCI_CTRL_RWE;
1001f7e1a3bSAlan Stern 	}
1011da177e4SLinus Torvalds 
102f197b2c5SDavid Brownell 	/* Suspend hub ... this is the "global (to this bus) suspend" mode,
103f197b2c5SDavid Brownell 	 * which doesn't imply ports will first be individually suspended.
104f197b2c5SDavid Brownell 	 */
1051da177e4SLinus Torvalds 	ohci->hc_control &= ~OHCI_CTRL_HCFS;
1061da177e4SLinus Torvalds 	ohci->hc_control |= OHCI_USB_SUSPEND;
1071da177e4SLinus Torvalds 	ohci_writel (ohci, ohci->hc_control, &ohci->regs->control);
1081da177e4SLinus Torvalds 	(void) ohci_readl (ohci, &ohci->regs->control);
1091da177e4SLinus Torvalds 
1101da177e4SLinus Torvalds 	/* no resumes until devices finish suspending */
1118d1a243bSAlan Stern 	if (!autostop) {
1121da177e4SLinus Torvalds 		ohci->next_statechange = jiffies + msecs_to_jiffies (5);
1138d1a243bSAlan Stern 		ohci->autostop = 0;
1148d1a243bSAlan Stern 	}
115d413984aSDavid Brownell 
1161da177e4SLinus Torvalds done:
1171da177e4SLinus Torvalds 	return status;
1181da177e4SLinus Torvalds }
1191da177e4SLinus Torvalds 
1201da177e4SLinus Torvalds static inline struct ed *find_head (struct ed *ed)
1211da177e4SLinus Torvalds {
1221da177e4SLinus Torvalds 	/* for bulk and control lists */
1231da177e4SLinus Torvalds 	while (ed->ed_prev)
1241da177e4SLinus Torvalds 		ed = ed->ed_prev;
1251da177e4SLinus Torvalds 	return ed;
1261da177e4SLinus Torvalds }
1271da177e4SLinus Torvalds 
1281da177e4SLinus Torvalds /* caller has locked the root hub */
1298d1a243bSAlan Stern static int ohci_rh_resume (struct ohci_hcd *ohci)
1308d1a243bSAlan Stern __releases(ohci->lock)
1318d1a243bSAlan Stern __acquires(ohci->lock)
1321da177e4SLinus Torvalds {
1338d1a243bSAlan Stern 	struct usb_hcd		*hcd = ohci_to_hcd (ohci);
1341da177e4SLinus Torvalds 	u32			temp, enables;
1351da177e4SLinus Torvalds 	int			status = -EINPROGRESS;
1368d1a243bSAlan Stern 	int			autostopped = ohci->autostop;
1371da177e4SLinus Torvalds 
1388d1a243bSAlan Stern 	ohci->autostop = 0;
1391da177e4SLinus Torvalds 	ohci->hc_control = ohci_readl (ohci, &ohci->regs->control);
1401da177e4SLinus Torvalds 
1411da177e4SLinus Torvalds 	if (ohci->hc_control & (OHCI_CTRL_IR | OHCI_SCHED_ENABLES)) {
142f197b2c5SDavid Brownell 		/* this can happen after resuming a swsusp snapshot */
1431da177e4SLinus Torvalds 		if (hcd->state == HC_STATE_RESUMING) {
1441da177e4SLinus Torvalds 			ohci_dbg (ohci, "BIOS/SMM active, control %03x\n",
1451da177e4SLinus Torvalds 					ohci->hc_control);
1461da177e4SLinus Torvalds 			status = -EBUSY;
1471da177e4SLinus Torvalds 		/* this happens when pmcore resumes HC then root */
1481da177e4SLinus Torvalds 		} else {
1491da177e4SLinus Torvalds 			ohci_dbg (ohci, "duplicate resume\n");
1501da177e4SLinus Torvalds 			status = 0;
1511da177e4SLinus Torvalds 		}
1521da177e4SLinus Torvalds 	} else switch (ohci->hc_control & OHCI_CTRL_HCFS) {
1531da177e4SLinus Torvalds 	case OHCI_USB_SUSPEND:
1541da177e4SLinus Torvalds 		ohci->hc_control &= ~(OHCI_CTRL_HCFS|OHCI_SCHED_ENABLES);
1551da177e4SLinus Torvalds 		ohci->hc_control |= OHCI_USB_RESUME;
1561da177e4SLinus Torvalds 		ohci_writel (ohci, ohci->hc_control, &ohci->regs->control);
1571da177e4SLinus Torvalds 		(void) ohci_readl (ohci, &ohci->regs->control);
1588d1a243bSAlan Stern 		ohci_dbg (ohci, "%s root hub\n",
1598d1a243bSAlan Stern 				autostopped ? "auto-start" : "resume");
1601da177e4SLinus Torvalds 		break;
1611da177e4SLinus Torvalds 	case OHCI_USB_RESUME:
1621da177e4SLinus Torvalds 		/* HCFS changes sometime after INTR_RD */
1631b7be3c0SAlan Stern 		ohci_dbg(ohci, "%swakeup root hub\n",
164583ceadaSAlan Stern 				autostopped ? "auto-" : "");
1651da177e4SLinus Torvalds 		break;
1661da177e4SLinus Torvalds 	case OHCI_USB_OPER:
167f197b2c5SDavid Brownell 		/* this can happen after resuming a swsusp snapshot */
168f197b2c5SDavid Brownell 		ohci_dbg (ohci, "snapshot resume? reinit\n");
169f197b2c5SDavid Brownell 		status = -EBUSY;
1701da177e4SLinus Torvalds 		break;
1711da177e4SLinus Torvalds 	default:		/* RESET, we lost power */
172f197b2c5SDavid Brownell 		ohci_dbg (ohci, "lost power\n");
1731da177e4SLinus Torvalds 		status = -EBUSY;
1741da177e4SLinus Torvalds 	}
1751da177e4SLinus Torvalds 	if (status == -EBUSY) {
1768d1a243bSAlan Stern 		if (!autostopped) {
1778d1a243bSAlan Stern 			spin_unlock_irq (&ohci->lock);
1781da177e4SLinus Torvalds 			(void) ohci_init (ohci);
1798d1a243bSAlan Stern 			status = ohci_restart (ohci);
180d576bb9fSMichael Hanselmann 
181d576bb9fSMichael Hanselmann 			usb_root_hub_lost_power(hcd->self.root_hub);
182d576bb9fSMichael Hanselmann 
1838d1a243bSAlan Stern 			spin_lock_irq (&ohci->lock);
1841da177e4SLinus Torvalds 		}
1858d1a243bSAlan Stern 		return status;
1868d1a243bSAlan Stern 	}
1871da177e4SLinus Torvalds 	if (status != -EINPROGRESS)
1881da177e4SLinus Torvalds 		return status;
1898d1a243bSAlan Stern 	if (autostopped)
1908d1a243bSAlan Stern 		goto skip_resume;
1918d1a243bSAlan Stern 	spin_unlock_irq (&ohci->lock);
1921da177e4SLinus Torvalds 
1931da177e4SLinus Torvalds 	/* Some controllers (lucent erratum) need extra-long delays */
194f197b2c5SDavid Brownell 	msleep (20 /* usb 11.5.1.10 */ + 12 /* 32 msec counter */ + 1);
1951da177e4SLinus Torvalds 
1961da177e4SLinus Torvalds 	temp = ohci_readl (ohci, &ohci->regs->control);
1971da177e4SLinus Torvalds 	temp &= OHCI_CTRL_HCFS;
1981da177e4SLinus Torvalds 	if (temp != OHCI_USB_RESUME) {
1991da177e4SLinus Torvalds 		ohci_err (ohci, "controller won't resume\n");
200565402baSAlan Stern 		spin_lock_irq(&ohci->lock);
2011da177e4SLinus Torvalds 		return -EBUSY;
2021da177e4SLinus Torvalds 	}
2031da177e4SLinus Torvalds 
2041da177e4SLinus Torvalds 	/* disable old schedule state, reinit from scratch */
2051da177e4SLinus Torvalds 	ohci_writel (ohci, 0, &ohci->regs->ed_controlhead);
2061da177e4SLinus Torvalds 	ohci_writel (ohci, 0, &ohci->regs->ed_controlcurrent);
2071da177e4SLinus Torvalds 	ohci_writel (ohci, 0, &ohci->regs->ed_bulkhead);
2081da177e4SLinus Torvalds 	ohci_writel (ohci, 0, &ohci->regs->ed_bulkcurrent);
2091da177e4SLinus Torvalds 	ohci_writel (ohci, 0, &ohci->regs->ed_periodcurrent);
2101da177e4SLinus Torvalds 	ohci_writel (ohci, (u32) ohci->hcca_dma, &ohci->regs->hcca);
2111da177e4SLinus Torvalds 
2121da177e4SLinus Torvalds 	/* Sometimes PCI D3 suspend trashes frame timings ... */
2131da177e4SLinus Torvalds 	periodic_reinit (ohci);
2141da177e4SLinus Torvalds 
2158d1a243bSAlan Stern 	/* the following code is executed with ohci->lock held and
2168d1a243bSAlan Stern 	 * irqs disabled if and only if autostopped is true
2178d1a243bSAlan Stern 	 */
2188d1a243bSAlan Stern 
2198d1a243bSAlan Stern skip_resume:
2201da177e4SLinus Torvalds 	/* interrupts might have been disabled */
2211da177e4SLinus Torvalds 	ohci_writel (ohci, OHCI_INTR_INIT, &ohci->regs->intrenable);
2221da177e4SLinus Torvalds 	if (ohci->ed_rm_list)
2231da177e4SLinus Torvalds 		ohci_writel (ohci, OHCI_INTR_SF, &ohci->regs->intrenable);
2241da177e4SLinus Torvalds 
2251da177e4SLinus Torvalds 	/* Then re-enable operations */
2261da177e4SLinus Torvalds 	ohci_writel (ohci, OHCI_USB_OPER, &ohci->regs->control);
2271da177e4SLinus Torvalds 	(void) ohci_readl (ohci, &ohci->regs->control);
2288d1a243bSAlan Stern 	if (!autostopped)
2291da177e4SLinus Torvalds 		msleep (3);
2301da177e4SLinus Torvalds 
2316a9062f3SDavid Brownell 	temp = ohci->hc_control;
2326a9062f3SDavid Brownell 	temp &= OHCI_CTRL_RWC;
2336a9062f3SDavid Brownell 	temp |= OHCI_CONTROL_INIT | OHCI_USB_OPER;
2341da177e4SLinus Torvalds 	ohci->hc_control = temp;
2351da177e4SLinus Torvalds 	ohci_writel (ohci, temp, &ohci->regs->control);
2361da177e4SLinus Torvalds 	(void) ohci_readl (ohci, &ohci->regs->control);
2371da177e4SLinus Torvalds 
2381da177e4SLinus Torvalds 	/* TRSMRCY */
2398d1a243bSAlan Stern 	if (!autostopped) {
2401da177e4SLinus Torvalds 		msleep (10);
2418d1a243bSAlan Stern 		spin_lock_irq (&ohci->lock);
2428d1a243bSAlan Stern 	}
2438d1a243bSAlan Stern 	/* now ohci->lock is always held and irqs are always disabled */
2441da177e4SLinus Torvalds 
245d413984aSDavid Brownell 	/* keep it alive for more than ~5x suspend + resume costs */
246d413984aSDavid Brownell 	ohci->next_statechange = jiffies + STATECHANGE_DELAY;
2471da177e4SLinus Torvalds 
2481da177e4SLinus Torvalds 	/* maybe turn schedules back on */
2491da177e4SLinus Torvalds 	enables = 0;
2501da177e4SLinus Torvalds 	temp = 0;
2511da177e4SLinus Torvalds 	if (!ohci->ed_rm_list) {
2521da177e4SLinus Torvalds 		if (ohci->ed_controltail) {
2531da177e4SLinus Torvalds 			ohci_writel (ohci,
2541da177e4SLinus Torvalds 					find_head (ohci->ed_controltail)->dma,
2551da177e4SLinus Torvalds 					&ohci->regs->ed_controlhead);
2561da177e4SLinus Torvalds 			enables |= OHCI_CTRL_CLE;
2571da177e4SLinus Torvalds 			temp |= OHCI_CLF;
2581da177e4SLinus Torvalds 		}
2591da177e4SLinus Torvalds 		if (ohci->ed_bulktail) {
2601da177e4SLinus Torvalds 			ohci_writel (ohci, find_head (ohci->ed_bulktail)->dma,
2611da177e4SLinus Torvalds 				&ohci->regs->ed_bulkhead);
2621da177e4SLinus Torvalds 			enables |= OHCI_CTRL_BLE;
2631da177e4SLinus Torvalds 			temp |= OHCI_BLF;
2641da177e4SLinus Torvalds 		}
2651da177e4SLinus Torvalds 	}
2661da177e4SLinus Torvalds 	if (hcd->self.bandwidth_isoc_reqs || hcd->self.bandwidth_int_reqs)
2671da177e4SLinus Torvalds 		enables |= OHCI_CTRL_PLE|OHCI_CTRL_IE;
2681da177e4SLinus Torvalds 	if (enables) {
2691da177e4SLinus Torvalds 		ohci_dbg (ohci, "restarting schedules ... %08x\n", enables);
2701da177e4SLinus Torvalds 		ohci->hc_control |= enables;
2711da177e4SLinus Torvalds 		ohci_writel (ohci, ohci->hc_control, &ohci->regs->control);
2721da177e4SLinus Torvalds 		if (temp)
2731da177e4SLinus Torvalds 			ohci_writel (ohci, temp, &ohci->regs->cmdstatus);
2741da177e4SLinus Torvalds 		(void) ohci_readl (ohci, &ohci->regs->control);
2751da177e4SLinus Torvalds 	}
2761da177e4SLinus Torvalds 
2771da177e4SLinus Torvalds 	return 0;
2781da177e4SLinus Torvalds }
2791da177e4SLinus Torvalds 
2808d1a243bSAlan Stern static int ohci_bus_suspend (struct usb_hcd *hcd)
2818d1a243bSAlan Stern {
2828d1a243bSAlan Stern 	struct ohci_hcd		*ohci = hcd_to_ohci (hcd);
2838d1a243bSAlan Stern 	int			rc;
2848d1a243bSAlan Stern 
2858d1a243bSAlan Stern 	spin_lock_irq (&ohci->lock);
2868d1a243bSAlan Stern 
287541c7d43SAlan Stern 	if (unlikely(!HCD_HW_ACCESSIBLE(hcd)))
2888d1a243bSAlan Stern 		rc = -ESHUTDOWN;
2898d1a243bSAlan Stern 	else
2908d1a243bSAlan Stern 		rc = ohci_rh_suspend (ohci, 0);
2918d1a243bSAlan Stern 	spin_unlock_irq (&ohci->lock);
2928d1a243bSAlan Stern 	return rc;
2938d1a243bSAlan Stern }
2948d1a243bSAlan Stern 
2958d1a243bSAlan Stern static int ohci_bus_resume (struct usb_hcd *hcd)
2968d1a243bSAlan Stern {
2978d1a243bSAlan Stern 	struct ohci_hcd		*ohci = hcd_to_ohci (hcd);
2988d1a243bSAlan Stern 	int			rc;
2998d1a243bSAlan Stern 
3008d1a243bSAlan Stern 	if (time_before (jiffies, ohci->next_statechange))
3018d1a243bSAlan Stern 		msleep(5);
3028d1a243bSAlan Stern 
3038d1a243bSAlan Stern 	spin_lock_irq (&ohci->lock);
3048d1a243bSAlan Stern 
305541c7d43SAlan Stern 	if (unlikely(!HCD_HW_ACCESSIBLE(hcd)))
3068d1a243bSAlan Stern 		rc = -ESHUTDOWN;
3078d1a243bSAlan Stern 	else
3088d1a243bSAlan Stern 		rc = ohci_rh_resume (ohci);
3098d1a243bSAlan Stern 	spin_unlock_irq (&ohci->lock);
3108d1a243bSAlan Stern 
3118d1a243bSAlan Stern 	/* poll until we know a device is connected or we autostop */
3128d1a243bSAlan Stern 	if (rc == 0)
3138d1a243bSAlan Stern 		usb_hcd_poll_rh_status(hcd);
3148d1a243bSAlan Stern 	return rc;
3158d1a243bSAlan Stern }
3168d1a243bSAlan Stern 
31743bbb7e0SAlan Stern /* Carry out the final steps of resuming the controller device */
31843bbb7e0SAlan Stern static void ohci_finish_controller_resume(struct usb_hcd *hcd)
31943bbb7e0SAlan Stern {
32043bbb7e0SAlan Stern 	struct ohci_hcd		*ohci = hcd_to_ohci(hcd);
32143bbb7e0SAlan Stern 	int			port;
32243bbb7e0SAlan Stern 	bool			need_reinit = false;
32343bbb7e0SAlan Stern 
32443bbb7e0SAlan Stern 	/* See if the controller is already running or has been reset */
32543bbb7e0SAlan Stern 	ohci->hc_control = ohci_readl(ohci, &ohci->regs->control);
32643bbb7e0SAlan Stern 	if (ohci->hc_control & (OHCI_CTRL_IR | OHCI_SCHED_ENABLES)) {
32743bbb7e0SAlan Stern 		need_reinit = true;
32843bbb7e0SAlan Stern 	} else {
32943bbb7e0SAlan Stern 		switch (ohci->hc_control & OHCI_CTRL_HCFS) {
33043bbb7e0SAlan Stern 		case OHCI_USB_OPER:
33143bbb7e0SAlan Stern 		case OHCI_USB_RESET:
33243bbb7e0SAlan Stern 			need_reinit = true;
33343bbb7e0SAlan Stern 		}
33443bbb7e0SAlan Stern 	}
33543bbb7e0SAlan Stern 
33643bbb7e0SAlan Stern 	/* If needed, reinitialize and suspend the root hub */
33743bbb7e0SAlan Stern 	if (need_reinit) {
33843bbb7e0SAlan Stern 		spin_lock_irq(&ohci->lock);
33943bbb7e0SAlan Stern 		hcd->state = HC_STATE_RESUMING;
34043bbb7e0SAlan Stern 		ohci_rh_resume(ohci);
34143bbb7e0SAlan Stern 		hcd->state = HC_STATE_QUIESCING;
34243bbb7e0SAlan Stern 		ohci_rh_suspend(ohci, 0);
34343bbb7e0SAlan Stern 		hcd->state = HC_STATE_SUSPENDED;
34443bbb7e0SAlan Stern 		spin_unlock_irq(&ohci->lock);
34543bbb7e0SAlan Stern 	}
34643bbb7e0SAlan Stern 
34743bbb7e0SAlan Stern 	/* Normally just turn on port power and enable interrupts */
34843bbb7e0SAlan Stern 	else {
34943bbb7e0SAlan Stern 		ohci_dbg(ohci, "powerup ports\n");
35043bbb7e0SAlan Stern 		for (port = 0; port < ohci->num_ports; port++)
35143bbb7e0SAlan Stern 			ohci_writel(ohci, RH_PS_PPS,
35243bbb7e0SAlan Stern 					&ohci->regs->roothub.portstatus[port]);
35343bbb7e0SAlan Stern 
35443bbb7e0SAlan Stern 		ohci_writel(ohci, OHCI_INTR_MIE, &ohci->regs->intrenable);
35543bbb7e0SAlan Stern 		ohci_readl(ohci, &ohci->regs->intrenable);
35643bbb7e0SAlan Stern 		msleep(20);
35743bbb7e0SAlan Stern 	}
358ee0b9be8SAlan Stern 
359ee0b9be8SAlan Stern 	/* Does the root hub have a port wakeup pending? */
360ee0b9be8SAlan Stern 	if (ohci_readl(ohci, &ohci->regs->intrstatus) &
361ee0b9be8SAlan Stern 			(OHCI_INTR_RD | OHCI_INTR_RHSC))
362ee0b9be8SAlan Stern 		usb_hcd_resume_root_hub(hcd);
36343bbb7e0SAlan Stern }
36443bbb7e0SAlan Stern 
3651f9fc882SAlan Stern /* Carry out polling-, autostop-, and autoresume-related state changes */
3661f9fc882SAlan Stern static int ohci_root_hub_state_changes(struct ohci_hcd *ohci, int changed,
36771b7497cSAlan Stern 		int any_connected, int rhsc_status)
3681f9fc882SAlan Stern {
3691f9fc882SAlan Stern 	int	poll_rh = 1;
37071b7497cSAlan Stern 	int	rhsc_enable;
3711f9fc882SAlan Stern 
372ee0b9be8SAlan Stern 	/* Some broken controllers never turn off RHSC in the interrupt
3734a511bc3SAlan Stern 	 * status register.  For their sake we won't re-enable RHSC
3744a511bc3SAlan Stern 	 * interrupts if the interrupt bit is already active.
375b5fb454fSAlan Stern 	 */
3764a511bc3SAlan Stern 	rhsc_enable = ohci_readl(ohci, &ohci->regs->intrenable) &
3774a511bc3SAlan Stern 			OHCI_INTR_RHSC;
3784a511bc3SAlan Stern 
3794a511bc3SAlan Stern 	switch (ohci->hc_control & OHCI_CTRL_HCFS) {
3804a511bc3SAlan Stern 	case OHCI_USB_OPER:
3814a511bc3SAlan Stern 		/* If no status changes are pending, enable RHSC interrupts. */
3824a511bc3SAlan Stern 		if (!rhsc_enable && !rhsc_status && !changed) {
3834a511bc3SAlan Stern 			rhsc_enable = OHCI_INTR_RHSC;
3844a511bc3SAlan Stern 			ohci_writel(ohci, rhsc_enable, &ohci->regs->intrenable);
385b5fb454fSAlan Stern 		}
386b5fb454fSAlan Stern 
387b5fb454fSAlan Stern 		/* Keep on polling until we know a device is connected
388b5fb454fSAlan Stern 		 * and RHSC is enabled, or until we autostop.
389b5fb454fSAlan Stern 		 */
3901f9fc882SAlan Stern 		if (!ohci->autostop) {
3911f9fc882SAlan Stern 			if (any_connected ||
3921f9fc882SAlan Stern 					!device_may_wakeup(&ohci_to_hcd(ohci)
3931f9fc882SAlan Stern 						->self.root_hub->dev)) {
3944a511bc3SAlan Stern 				if (rhsc_enable)
3951f9fc882SAlan Stern 					poll_rh = 0;
3961f9fc882SAlan Stern 			} else {
3971f9fc882SAlan Stern 				ohci->autostop = 1;
3981f9fc882SAlan Stern 				ohci->next_statechange = jiffies + HZ;
3991f9fc882SAlan Stern 			}
4001f9fc882SAlan Stern 
4011f9fc882SAlan Stern 		/* if no devices have been attached for one second, autostop */
4021f9fc882SAlan Stern 		} else {
4031f9fc882SAlan Stern 			if (changed || any_connected) {
4041f9fc882SAlan Stern 				ohci->autostop = 0;
4051f9fc882SAlan Stern 				ohci->next_statechange = jiffies +
4061f9fc882SAlan Stern 						STATECHANGE_DELAY;
4074a511bc3SAlan Stern 			} else if (time_after_eq(jiffies,
4081f9fc882SAlan Stern 						ohci->next_statechange)
4091f9fc882SAlan Stern 					&& !ohci->ed_rm_list
4101f9fc882SAlan Stern 					&& !(ohci->hc_control &
4111f9fc882SAlan Stern 						OHCI_SCHED_ENABLES)) {
4121f9fc882SAlan Stern 				ohci_rh_suspend(ohci, 1);
4134a511bc3SAlan Stern 				if (rhsc_enable)
414b5fb454fSAlan Stern 					poll_rh = 0;
4151f9fc882SAlan Stern 			}
4161f9fc882SAlan Stern 		}
4171f9fc882SAlan Stern 		break;
4181f9fc882SAlan Stern 
4191f9fc882SAlan Stern 	case OHCI_USB_SUSPEND:
4201f9fc882SAlan Stern 	case OHCI_USB_RESUME:
4214a511bc3SAlan Stern 		/* if there is a port change, autostart or ask to be resumed */
4221f9fc882SAlan Stern 		if (changed) {
4231f9fc882SAlan Stern 			if (ohci->autostop)
4241f9fc882SAlan Stern 				ohci_rh_resume(ohci);
4251f9fc882SAlan Stern 			else
4261f9fc882SAlan Stern 				usb_hcd_resume_root_hub(ohci_to_hcd(ohci));
42771b7497cSAlan Stern 
42871b7497cSAlan Stern 		/* If remote wakeup is disabled, stop polling */
42971b7497cSAlan Stern 		} else if (!ohci->autostop &&
43071b7497cSAlan Stern 				!ohci_to_hcd(ohci)->self.root_hub->
43171b7497cSAlan Stern 					do_remote_wakeup) {
43271b7497cSAlan Stern 			poll_rh = 0;
43371b7497cSAlan Stern 
4341f9fc882SAlan Stern 		} else {
43571b7497cSAlan Stern 			/* If no status changes are pending,
43671b7497cSAlan Stern 			 * enable RHSC interrupts
43771b7497cSAlan Stern 			 */
43871b7497cSAlan Stern 			if (!rhsc_enable && !rhsc_status) {
4394a511bc3SAlan Stern 				rhsc_enable = OHCI_INTR_RHSC;
4404a511bc3SAlan Stern 				ohci_writel(ohci, rhsc_enable,
441b5fb454fSAlan Stern 						&ohci->regs->intrenable);
4424a511bc3SAlan Stern 			}
44371b7497cSAlan Stern 			/* Keep polling until RHSC is enabled */
4444a511bc3SAlan Stern 			if (rhsc_enable)
4451f9fc882SAlan Stern 				poll_rh = 0;
4461f9fc882SAlan Stern 		}
4471f9fc882SAlan Stern 		break;
4481f9fc882SAlan Stern 	}
4491f9fc882SAlan Stern 	return poll_rh;
4501f9fc882SAlan Stern }
4511f9fc882SAlan Stern 
4521f9fc882SAlan Stern #else	/* CONFIG_PM */
4531f9fc882SAlan Stern 
4541f9fc882SAlan Stern static inline int ohci_rh_resume(struct ohci_hcd *ohci)
4551f9fc882SAlan Stern {
4561f9fc882SAlan Stern 	return 0;
4571f9fc882SAlan Stern }
4581f9fc882SAlan Stern 
4591f9fc882SAlan Stern /* Carry out polling-related state changes.
4601f9fc882SAlan Stern  * autostop isn't used when CONFIG_PM is turned off.
4611f9fc882SAlan Stern  */
4621f9fc882SAlan Stern static int ohci_root_hub_state_changes(struct ohci_hcd *ohci, int changed,
46371b7497cSAlan Stern 		int any_connected, int rhsc_status)
4641f9fc882SAlan Stern {
465b5fb454fSAlan Stern 	/* If RHSC is enabled, don't poll */
46609ca8adbSLinus Torvalds 	if (ohci_readl(ohci, &ohci->regs->intrenable) & OHCI_INTR_RHSC)
467b5fb454fSAlan Stern 		return 0;
468b5fb454fSAlan Stern 
46971b7497cSAlan Stern 	/* If status changes are pending, continue polling.
47071b7497cSAlan Stern 	 * Conversely, if no status changes are pending but the RHSC
47171b7497cSAlan Stern 	 * status bit was set, then RHSC may be broken so continue polling.
47271b7497cSAlan Stern 	 */
47371b7497cSAlan Stern 	if (changed || rhsc_status)
47471b7497cSAlan Stern 		return 1;
47571b7497cSAlan Stern 
47671b7497cSAlan Stern 	/* It's safe to re-enable RHSC interrupts */
477b5fb454fSAlan Stern 	ohci_writel(ohci, OHCI_INTR_RHSC, &ohci->regs->intrenable);
478b5fb454fSAlan Stern 	return 0;
479b5fb454fSAlan Stern }
4801f9fc882SAlan Stern 
4818ad7fe16SDavid Brownell #endif	/* CONFIG_PM */
4821da177e4SLinus Torvalds 
4831da177e4SLinus Torvalds /*-------------------------------------------------------------------------*/
4841da177e4SLinus Torvalds 
4851da177e4SLinus Torvalds /* build "status change" packet (one or two bytes) from HC registers */
4861da177e4SLinus Torvalds 
4871da177e4SLinus Torvalds static int
4881da177e4SLinus Torvalds ohci_hub_status_data (struct usb_hcd *hcd, char *buf)
4891da177e4SLinus Torvalds {
4901da177e4SLinus Torvalds 	struct ohci_hcd	*ohci = hcd_to_ohci (hcd);
491fdd13b36SDavid Brownell 	int		i, changed = 0, length = 1;
492052ac01aSAlan Stern 	int		any_connected = 0;
49371b7497cSAlan Stern 	int		rhsc_status;
4941da177e4SLinus Torvalds 	unsigned long	flags;
4951da177e4SLinus Torvalds 
4961da177e4SLinus Torvalds 	spin_lock_irqsave (&ohci->lock, flags);
497541c7d43SAlan Stern 	if (!HCD_HW_ACCESSIBLE(hcd))
4986fd75b19SAlan Stern 		goto done;
4991da177e4SLinus Torvalds 
500fdd13b36SDavid Brownell 	/* undocumented erratum seen on at least rev D */
501fdd13b36SDavid Brownell 	if ((ohci->flags & OHCI_QUIRK_AMD756)
502fdd13b36SDavid Brownell 			&& (roothub_a (ohci) & RH_A_NDP) > MAX_ROOT_PORTS) {
503fdd13b36SDavid Brownell 		ohci_warn (ohci, "bogus NDP, rereads as NDP=%d\n",
5041da177e4SLinus Torvalds 			  ohci_readl (ohci, &ohci->regs->roothub.a) & RH_A_NDP);
5051da177e4SLinus Torvalds 		/* retry later; "should not happen" */
5061da177e4SLinus Torvalds 		goto done;
5071da177e4SLinus Torvalds 	}
5081da177e4SLinus Torvalds 
5091da177e4SLinus Torvalds 	/* init status */
5101da177e4SLinus Torvalds 	if (roothub_status (ohci) & (RH_HS_LPSC | RH_HS_OCIC))
5111da177e4SLinus Torvalds 		buf [0] = changed = 1;
5121da177e4SLinus Torvalds 	else
5131da177e4SLinus Torvalds 		buf [0] = 0;
514fdd13b36SDavid Brownell 	if (ohci->num_ports > 7) {
5151da177e4SLinus Torvalds 		buf [1] = 0;
5161da177e4SLinus Torvalds 		length++;
5171da177e4SLinus Torvalds 	}
5181da177e4SLinus Torvalds 
51971b7497cSAlan Stern 	/* Clear the RHSC status flag before reading the port statuses */
52071b7497cSAlan Stern 	ohci_writel(ohci, OHCI_INTR_RHSC, &ohci->regs->intrstatus);
52171b7497cSAlan Stern 	rhsc_status = ohci_readl(ohci, &ohci->regs->intrstatus) &
52271b7497cSAlan Stern 			OHCI_INTR_RHSC;
52371b7497cSAlan Stern 
5241da177e4SLinus Torvalds 	/* look at each port */
525fdd13b36SDavid Brownell 	for (i = 0; i < ohci->num_ports; i++) {
5261da177e4SLinus Torvalds 		u32	status = roothub_portstatus (ohci, i);
5271da177e4SLinus Torvalds 
5288d1a243bSAlan Stern 		/* can't autostop if ports are connected */
5298d1a243bSAlan Stern 		any_connected |= (status & RH_PS_CCS);
5308d1a243bSAlan Stern 
5311da177e4SLinus Torvalds 		if (status & (RH_PS_CSC | RH_PS_PESC | RH_PS_PSSC
5321da177e4SLinus Torvalds 				| RH_PS_OCIC | RH_PS_PRSC)) {
5331da177e4SLinus Torvalds 			changed = 1;
5341da177e4SLinus Torvalds 			if (i < 7)
5351da177e4SLinus Torvalds 			    buf [0] |= 1 << (i + 1);
5361da177e4SLinus Torvalds 			else
5371da177e4SLinus Torvalds 			    buf [1] |= 1 << (i - 7);
5381da177e4SLinus Torvalds 		}
5391da177e4SLinus Torvalds 	}
540d413984aSDavid Brownell 
541541c7d43SAlan Stern 	if (ohci_root_hub_state_changes(ohci, changed,
542541c7d43SAlan Stern 			any_connected, rhsc_status))
543541c7d43SAlan Stern 		set_bit(HCD_FLAG_POLL_RH, &hcd->flags);
544541c7d43SAlan Stern 	else
545541c7d43SAlan Stern 		clear_bit(HCD_FLAG_POLL_RH, &hcd->flags);
546541c7d43SAlan Stern 
547d413984aSDavid Brownell 
5481da177e4SLinus Torvalds done:
5491da177e4SLinus Torvalds 	spin_unlock_irqrestore (&ohci->lock, flags);
5501da177e4SLinus Torvalds 
5511da177e4SLinus Torvalds 	return changed ? length : 0;
5521da177e4SLinus Torvalds }
5531da177e4SLinus Torvalds 
5541da177e4SLinus Torvalds /*-------------------------------------------------------------------------*/
5551da177e4SLinus Torvalds 
5561da177e4SLinus Torvalds static void
5571da177e4SLinus Torvalds ohci_hub_descriptor (
5581da177e4SLinus Torvalds 	struct ohci_hcd			*ohci,
5591da177e4SLinus Torvalds 	struct usb_hub_descriptor	*desc
5601da177e4SLinus Torvalds ) {
5611da177e4SLinus Torvalds 	u32		rh = roothub_a (ohci);
5621da177e4SLinus Torvalds 	u16		temp;
5631da177e4SLinus Torvalds 
5641da177e4SLinus Torvalds 	desc->bDescriptorType = 0x29;
5651da177e4SLinus Torvalds 	desc->bPwrOn2PwrGood = (rh & RH_A_POTPGT) >> 24;
5661da177e4SLinus Torvalds 	desc->bHubContrCurrent = 0;
5671da177e4SLinus Torvalds 
568fdd13b36SDavid Brownell 	desc->bNbrPorts = ohci->num_ports;
569fdd13b36SDavid Brownell 	temp = 1 + (ohci->num_ports / 8);
5701da177e4SLinus Torvalds 	desc->bDescLength = 7 + 2 * temp;
5711da177e4SLinus Torvalds 
5721da177e4SLinus Torvalds 	temp = 0;
5731da177e4SLinus Torvalds 	if (rh & RH_A_NPS)		/* no power switching? */
5741da177e4SLinus Torvalds 	    temp |= 0x0002;
5751da177e4SLinus Torvalds 	if (rh & RH_A_PSM)		/* per-port power switching? */
5761da177e4SLinus Torvalds 	    temp |= 0x0001;
5771da177e4SLinus Torvalds 	if (rh & RH_A_NOCP)		/* no overcurrent reporting? */
5781da177e4SLinus Torvalds 	    temp |= 0x0010;
5791da177e4SLinus Torvalds 	else if (rh & RH_A_OCPM)	/* per-port overcurrent reporting? */
5801da177e4SLinus Torvalds 	    temp |= 0x0008;
5811da177e4SLinus Torvalds 	desc->wHubCharacteristics = (__force __u16)cpu_to_hc16(ohci, temp);
5821da177e4SLinus Torvalds 
583*da13051cSSarah Sharp 	/* ports removable, and usb 1.0 legacy PortPwrCtrlMask */
5841da177e4SLinus Torvalds 	rh = roothub_b (ohci);
585*da13051cSSarah Sharp 	memset(desc->DeviceRemovable, 0xff, sizeof(desc->DeviceRemovable));
586*da13051cSSarah Sharp 	desc->DeviceRemovable[0] = rh & RH_B_DR;
587fdd13b36SDavid Brownell 	if (ohci->num_ports > 7) {
588*da13051cSSarah Sharp 		desc->DeviceRemovable[1] = (rh & RH_B_DR) >> 8;
589*da13051cSSarah Sharp 		desc->DeviceRemovable[2] = 0xff;
5901da177e4SLinus Torvalds 	} else
591*da13051cSSarah Sharp 		desc->DeviceRemovable[1] = 0xff;
5921da177e4SLinus Torvalds }
5931da177e4SLinus Torvalds 
5941da177e4SLinus Torvalds /*-------------------------------------------------------------------------*/
5951da177e4SLinus Torvalds 
5961da177e4SLinus Torvalds #ifdef	CONFIG_USB_OTG
5971da177e4SLinus Torvalds 
5981da177e4SLinus Torvalds static int ohci_start_port_reset (struct usb_hcd *hcd, unsigned port)
5991da177e4SLinus Torvalds {
6001da177e4SLinus Torvalds 	struct ohci_hcd	*ohci = hcd_to_ohci (hcd);
6011da177e4SLinus Torvalds 	u32			status;
6021da177e4SLinus Torvalds 
6031da177e4SLinus Torvalds 	if (!port)
6041da177e4SLinus Torvalds 		return -EINVAL;
6051da177e4SLinus Torvalds 	port--;
6061da177e4SLinus Torvalds 
6071da177e4SLinus Torvalds 	/* start port reset before HNP protocol times out */
6081da177e4SLinus Torvalds 	status = ohci_readl(ohci, &ohci->regs->roothub.portstatus [port]);
6091da177e4SLinus Torvalds 	if (!(status & RH_PS_CCS))
6101da177e4SLinus Torvalds 		return -ENODEV;
6111da177e4SLinus Torvalds 
6121da177e4SLinus Torvalds 	/* khubd will finish the reset later */
6131da177e4SLinus Torvalds 	ohci_writel(ohci, RH_PS_PRS, &ohci->regs->roothub.portstatus [port]);
6141da177e4SLinus Torvalds 	return 0;
6151da177e4SLinus Torvalds }
6161da177e4SLinus Torvalds 
6171da177e4SLinus Torvalds #else
6181da177e4SLinus Torvalds 
6191da177e4SLinus Torvalds #define	ohci_start_port_reset		NULL
6201da177e4SLinus Torvalds 
6211da177e4SLinus Torvalds #endif
6221da177e4SLinus Torvalds 
6231da177e4SLinus Torvalds /*-------------------------------------------------------------------------*/
6241da177e4SLinus Torvalds 
6251da177e4SLinus Torvalds 
6261da177e4SLinus Torvalds /* See usb 7.1.7.5:  root hubs must issue at least 50 msec reset signaling,
6271da177e4SLinus Torvalds  * not necessarily continuous ... to guard against resume signaling.
6281da177e4SLinus Torvalds  * The short timeout is safe for non-root hubs, and is backward-compatible
6291da177e4SLinus Torvalds  * with earlier Linux hosts.
6301da177e4SLinus Torvalds  */
6311da177e4SLinus Torvalds #ifdef	CONFIG_USB_SUSPEND
6321da177e4SLinus Torvalds #define	PORT_RESET_MSEC		50
6331da177e4SLinus Torvalds #else
6341da177e4SLinus Torvalds #define	PORT_RESET_MSEC		10
6351da177e4SLinus Torvalds #endif
6361da177e4SLinus Torvalds 
6371da177e4SLinus Torvalds /* this timer value might be vendor-specific ... */
6381da177e4SLinus Torvalds #define	PORT_RESET_HW_MSEC	10
6391da177e4SLinus Torvalds 
6401da177e4SLinus Torvalds /* wrap-aware logic morphed from <linux/jiffies.h> */
6411da177e4SLinus Torvalds #define tick_before(t1,t2) ((s16)(((s16)(t1))-((s16)(t2))) < 0)
6421da177e4SLinus Torvalds 
6431da177e4SLinus Torvalds /* called from some task, normally khubd */
64423d10a9eSTakamasa Ohtake static inline int root_port_reset (struct ohci_hcd *ohci, unsigned port)
6451da177e4SLinus Torvalds {
6461da177e4SLinus Torvalds 	__hc32 __iomem *portstat = &ohci->regs->roothub.portstatus [port];
6471b7b61c5SAlan Stern 	u32	temp = 0;
6481da177e4SLinus Torvalds 	u16	now = ohci_readl(ohci, &ohci->regs->fmnumber);
6491da177e4SLinus Torvalds 	u16	reset_done = now + PORT_RESET_MSEC;
650e01e7fe3SDavid Brownell 	int	limit_1 = DIV_ROUND_UP(PORT_RESET_MSEC, PORT_RESET_HW_MSEC);
6511da177e4SLinus Torvalds 
6521da177e4SLinus Torvalds 	/* build a "continuous enough" reset signal, with up to
6531da177e4SLinus Torvalds 	 * 3msec gap between pulses.  scheduler HZ==100 must work;
6541da177e4SLinus Torvalds 	 * this might need to be deadline-scheduled.
6551da177e4SLinus Torvalds 	 */
6561da177e4SLinus Torvalds 	do {
657e01e7fe3SDavid Brownell 		int limit_2;
658e01e7fe3SDavid Brownell 
6591da177e4SLinus Torvalds 		/* spin until any current reset finishes */
660e01e7fe3SDavid Brownell 		limit_2 = PORT_RESET_HW_MSEC * 2;
661e01e7fe3SDavid Brownell 		while (--limit_2 >= 0) {
6621da177e4SLinus Torvalds 			temp = ohci_readl (ohci, portstat);
66323d10a9eSTakamasa Ohtake 			/* handle e.g. CardBus eject */
66423d10a9eSTakamasa Ohtake 			if (temp == ~(u32)0)
66523d10a9eSTakamasa Ohtake 				return -ESHUTDOWN;
6661da177e4SLinus Torvalds 			if (!(temp & RH_PS_PRS))
6671da177e4SLinus Torvalds 				break;
6681da177e4SLinus Torvalds 			udelay (500);
6691da177e4SLinus Torvalds 		}
6701da177e4SLinus Torvalds 
671e01e7fe3SDavid Brownell 		/* timeout (a hardware error) has been observed when
672e01e7fe3SDavid Brownell 		 * EHCI sets CF while this driver is resetting a port;
673e01e7fe3SDavid Brownell 		 * presumably other disconnect paths might do it too.
674e01e7fe3SDavid Brownell 		 */
675e01e7fe3SDavid Brownell 		if (limit_2 < 0) {
676e01e7fe3SDavid Brownell 			ohci_dbg(ohci,
677e01e7fe3SDavid Brownell 				"port[%d] reset timeout, stat %08x\n",
678e01e7fe3SDavid Brownell 				port, temp);
679e01e7fe3SDavid Brownell 			break;
680e01e7fe3SDavid Brownell 		}
681e01e7fe3SDavid Brownell 
6821da177e4SLinus Torvalds 		if (!(temp & RH_PS_CCS))
6831da177e4SLinus Torvalds 			break;
6841da177e4SLinus Torvalds 		if (temp & RH_PS_PRSC)
6851da177e4SLinus Torvalds 			ohci_writel (ohci, RH_PS_PRSC, portstat);
6861da177e4SLinus Torvalds 
6871da177e4SLinus Torvalds 		/* start the next reset, sleep till it's probably done */
6881da177e4SLinus Torvalds 		ohci_writel (ohci, RH_PS_PRS, portstat);
6891da177e4SLinus Torvalds 		msleep(PORT_RESET_HW_MSEC);
6901da177e4SLinus Torvalds 		now = ohci_readl(ohci, &ohci->regs->fmnumber);
691e01e7fe3SDavid Brownell 	} while (tick_before(now, reset_done) && --limit_1 >= 0);
692e01e7fe3SDavid Brownell 
693e01e7fe3SDavid Brownell 	/* caller synchronizes using PRSC ... and handles PRS
694e01e7fe3SDavid Brownell 	 * still being set when this returns.
695e01e7fe3SDavid Brownell 	 */
69623d10a9eSTakamasa Ohtake 
69723d10a9eSTakamasa Ohtake 	return 0;
6981da177e4SLinus Torvalds }
6991da177e4SLinus Torvalds 
7001da177e4SLinus Torvalds static int ohci_hub_control (
7011da177e4SLinus Torvalds 	struct usb_hcd	*hcd,
7021da177e4SLinus Torvalds 	u16		typeReq,
7031da177e4SLinus Torvalds 	u16		wValue,
7041da177e4SLinus Torvalds 	u16		wIndex,
7051da177e4SLinus Torvalds 	char		*buf,
7061da177e4SLinus Torvalds 	u16		wLength
7071da177e4SLinus Torvalds ) {
7081da177e4SLinus Torvalds 	struct ohci_hcd	*ohci = hcd_to_ohci (hcd);
709fcf7d214SAlan Stern 	int		ports = ohci->num_ports;
7101da177e4SLinus Torvalds 	u32		temp;
7111da177e4SLinus Torvalds 	int		retval = 0;
7121da177e4SLinus Torvalds 
713541c7d43SAlan Stern 	if (unlikely(!HCD_HW_ACCESSIBLE(hcd)))
7148de98402SBenjamin Herrenschmidt 		return -ESHUTDOWN;
7158de98402SBenjamin Herrenschmidt 
7161da177e4SLinus Torvalds 	switch (typeReq) {
7171da177e4SLinus Torvalds 	case ClearHubFeature:
7181da177e4SLinus Torvalds 		switch (wValue) {
7191da177e4SLinus Torvalds 		case C_HUB_OVER_CURRENT:
7201da177e4SLinus Torvalds 			ohci_writel (ohci, RH_HS_OCIC,
7211da177e4SLinus Torvalds 					&ohci->regs->roothub.status);
7221da177e4SLinus Torvalds 		case C_HUB_LOCAL_POWER:
7231da177e4SLinus Torvalds 			break;
7241da177e4SLinus Torvalds 		default:
7251da177e4SLinus Torvalds 			goto error;
7261da177e4SLinus Torvalds 		}
7271da177e4SLinus Torvalds 		break;
7281da177e4SLinus Torvalds 	case ClearPortFeature:
7291da177e4SLinus Torvalds 		if (!wIndex || wIndex > ports)
7301da177e4SLinus Torvalds 			goto error;
7311da177e4SLinus Torvalds 		wIndex--;
7321da177e4SLinus Torvalds 
7331da177e4SLinus Torvalds 		switch (wValue) {
7341da177e4SLinus Torvalds 		case USB_PORT_FEAT_ENABLE:
7351da177e4SLinus Torvalds 			temp = RH_PS_CCS;
7361da177e4SLinus Torvalds 			break;
7371da177e4SLinus Torvalds 		case USB_PORT_FEAT_C_ENABLE:
7381da177e4SLinus Torvalds 			temp = RH_PS_PESC;
7391da177e4SLinus Torvalds 			break;
7401da177e4SLinus Torvalds 		case USB_PORT_FEAT_SUSPEND:
7411da177e4SLinus Torvalds 			temp = RH_PS_POCI;
7421da177e4SLinus Torvalds 			break;
7431da177e4SLinus Torvalds 		case USB_PORT_FEAT_C_SUSPEND:
7441da177e4SLinus Torvalds 			temp = RH_PS_PSSC;
7451da177e4SLinus Torvalds 			break;
7461da177e4SLinus Torvalds 		case USB_PORT_FEAT_POWER:
7471da177e4SLinus Torvalds 			temp = RH_PS_LSDA;
7481da177e4SLinus Torvalds 			break;
7491da177e4SLinus Torvalds 		case USB_PORT_FEAT_C_CONNECTION:
7501da177e4SLinus Torvalds 			temp = RH_PS_CSC;
7511da177e4SLinus Torvalds 			break;
7521da177e4SLinus Torvalds 		case USB_PORT_FEAT_C_OVER_CURRENT:
7531da177e4SLinus Torvalds 			temp = RH_PS_OCIC;
7541da177e4SLinus Torvalds 			break;
7551da177e4SLinus Torvalds 		case USB_PORT_FEAT_C_RESET:
7561da177e4SLinus Torvalds 			temp = RH_PS_PRSC;
7571da177e4SLinus Torvalds 			break;
7581da177e4SLinus Torvalds 		default:
7591da177e4SLinus Torvalds 			goto error;
7601da177e4SLinus Torvalds 		}
7611da177e4SLinus Torvalds 		ohci_writel (ohci, temp,
7621da177e4SLinus Torvalds 				&ohci->regs->roothub.portstatus [wIndex]);
7631da177e4SLinus Torvalds 		// ohci_readl (ohci, &ohci->regs->roothub.portstatus [wIndex]);
7641da177e4SLinus Torvalds 		break;
7651da177e4SLinus Torvalds 	case GetHubDescriptor:
7661da177e4SLinus Torvalds 		ohci_hub_descriptor (ohci, (struct usb_hub_descriptor *) buf);
7671da177e4SLinus Torvalds 		break;
7681da177e4SLinus Torvalds 	case GetHubStatus:
7691da177e4SLinus Torvalds 		temp = roothub_status (ohci) & ~(RH_HS_CRWE | RH_HS_DRWE);
770a5abdeafSHarvey Harrison 		put_unaligned_le32(temp, buf);
7711da177e4SLinus Torvalds 		break;
7721da177e4SLinus Torvalds 	case GetPortStatus:
7731da177e4SLinus Torvalds 		if (!wIndex || wIndex > ports)
7741da177e4SLinus Torvalds 			goto error;
7751da177e4SLinus Torvalds 		wIndex--;
7761da177e4SLinus Torvalds 		temp = roothub_portstatus (ohci, wIndex);
777a5abdeafSHarvey Harrison 		put_unaligned_le32(temp, buf);
7781da177e4SLinus Torvalds 
7791da177e4SLinus Torvalds #ifndef	OHCI_VERBOSE_DEBUG
7801da177e4SLinus Torvalds 	if (*(u16*)(buf+2))	/* only if wPortChange is interesting */
7811da177e4SLinus Torvalds #endif
7821da177e4SLinus Torvalds 		dbg_port (ohci, "GetStatus", wIndex, temp);
7831da177e4SLinus Torvalds 		break;
7841da177e4SLinus Torvalds 	case SetHubFeature:
7851da177e4SLinus Torvalds 		switch (wValue) {
7861da177e4SLinus Torvalds 		case C_HUB_OVER_CURRENT:
7871da177e4SLinus Torvalds 			// FIXME:  this can be cleared, yes?
7881da177e4SLinus Torvalds 		case C_HUB_LOCAL_POWER:
7891da177e4SLinus Torvalds 			break;
7901da177e4SLinus Torvalds 		default:
7911da177e4SLinus Torvalds 			goto error;
7921da177e4SLinus Torvalds 		}
7931da177e4SLinus Torvalds 		break;
7941da177e4SLinus Torvalds 	case SetPortFeature:
7951da177e4SLinus Torvalds 		if (!wIndex || wIndex > ports)
7961da177e4SLinus Torvalds 			goto error;
7971da177e4SLinus Torvalds 		wIndex--;
7981da177e4SLinus Torvalds 		switch (wValue) {
7991da177e4SLinus Torvalds 		case USB_PORT_FEAT_SUSPEND:
8001da177e4SLinus Torvalds #ifdef	CONFIG_USB_OTG
8011da177e4SLinus Torvalds 			if (hcd->self.otg_port == (wIndex + 1)
8021da177e4SLinus Torvalds 					&& hcd->self.b_hnp_enable)
803e8b24450SDmitry Baryshkov 				ohci->start_hnp(ohci);
8041da177e4SLinus Torvalds 			else
8051da177e4SLinus Torvalds #endif
8061da177e4SLinus Torvalds 			ohci_writel (ohci, RH_PS_PSS,
8071da177e4SLinus Torvalds 				&ohci->regs->roothub.portstatus [wIndex]);
8081da177e4SLinus Torvalds 			break;
8091da177e4SLinus Torvalds 		case USB_PORT_FEAT_POWER:
8101da177e4SLinus Torvalds 			ohci_writel (ohci, RH_PS_PPS,
8111da177e4SLinus Torvalds 				&ohci->regs->roothub.portstatus [wIndex]);
8121da177e4SLinus Torvalds 			break;
8131da177e4SLinus Torvalds 		case USB_PORT_FEAT_RESET:
81423d10a9eSTakamasa Ohtake 			retval = root_port_reset (ohci, wIndex);
8151da177e4SLinus Torvalds 			break;
8161da177e4SLinus Torvalds 		default:
8171da177e4SLinus Torvalds 			goto error;
8181da177e4SLinus Torvalds 		}
8191da177e4SLinus Torvalds 		break;
8201da177e4SLinus Torvalds 
8211da177e4SLinus Torvalds 	default:
8221da177e4SLinus Torvalds error:
8231da177e4SLinus Torvalds 		/* "protocol stall" on error */
8241da177e4SLinus Torvalds 		retval = -EPIPE;
8251da177e4SLinus Torvalds 	}
8261da177e4SLinus Torvalds 	return retval;
8271da177e4SLinus Torvalds }
8281da177e4SLinus Torvalds 
829