xref: /linux/drivers/usb/host/ohci-hub.c (revision 03ab8e6297acd1bc0eedaa050e2a1635c576fd11)
15fd54aceSGreg Kroah-Hartman // SPDX-License-Identifier: GPL-1.0+
21da177e4SLinus Torvalds /*
31da177e4SLinus Torvalds  * OHCI HCD (Host Controller Driver) for USB.
41da177e4SLinus Torvalds  *
51da177e4SLinus Torvalds  * (C) Copyright 1999 Roman Weissgaerber <weissg@vienna.at>
61da177e4SLinus Torvalds  * (C) Copyright 2000-2004 David Brownell <dbrownell@users.sourceforge.net>
71da177e4SLinus Torvalds  *
81da177e4SLinus Torvalds  * This file is licenced under GPL
91da177e4SLinus Torvalds  */
101da177e4SLinus Torvalds 
111da177e4SLinus Torvalds /*-------------------------------------------------------------------------*/
121da177e4SLinus Torvalds 
131da177e4SLinus Torvalds /*
141da177e4SLinus Torvalds  * OHCI Root Hub ... the nonsharable stuff
151da177e4SLinus Torvalds  */
161da177e4SLinus Torvalds 
171da177e4SLinus Torvalds #define dbg_port(hc,label,num,value) \
181da177e4SLinus Torvalds 	ohci_dbg (hc, \
191da177e4SLinus Torvalds 		"%s roothub.portstatus [%d] " \
201da177e4SLinus Torvalds 		"= 0x%08x%s%s%s%s%s%s%s%s%s%s%s%s\n", \
21baa42a35SJelle Martijn Kok 		label, num, value, \
22baa42a35SJelle Martijn Kok 		(value & RH_PS_PRSC) ? " PRSC" : "", \
23baa42a35SJelle Martijn Kok 		(value & RH_PS_OCIC) ? " OCIC" : "", \
24baa42a35SJelle Martijn Kok 		(value & RH_PS_PSSC) ? " PSSC" : "", \
25baa42a35SJelle Martijn Kok 		(value & RH_PS_PESC) ? " PESC" : "", \
26baa42a35SJelle Martijn Kok 		(value & RH_PS_CSC) ? " CSC" : "", \
271da177e4SLinus Torvalds 		\
28baa42a35SJelle Martijn Kok 		(value & RH_PS_LSDA) ? " LSDA" : "", \
29baa42a35SJelle Martijn Kok 		(value & RH_PS_PPS) ? " PPS" : "", \
30baa42a35SJelle Martijn Kok 		(value & RH_PS_PRS) ? " PRS" : "", \
31baa42a35SJelle Martijn Kok 		(value & RH_PS_POCI) ? " POCI" : "", \
32baa42a35SJelle Martijn Kok 		(value & RH_PS_PSS) ? " PSS" : "", \
331da177e4SLinus Torvalds 		\
34baa42a35SJelle Martijn Kok 		(value & RH_PS_PES) ? " PES" : "", \
35baa42a35SJelle Martijn Kok 		(value & RH_PS_CCS) ? " CCS" : "" \
361da177e4SLinus Torvalds 		);
371da177e4SLinus Torvalds 
381da177e4SLinus Torvalds /*-------------------------------------------------------------------------*/
391da177e4SLinus Torvalds 
401da177e4SLinus Torvalds #define OHCI_SCHED_ENABLES \
411da177e4SLinus Torvalds 	(OHCI_CTRL_CLE|OHCI_CTRL_BLE|OHCI_CTRL_PLE|OHCI_CTRL_IE)
421da177e4SLinus Torvalds 
43c6fcb85eSAlan Stern static void update_done_list(struct ohci_hcd *);
44cdb4dd15SAlan Stern static void ohci_work(struct ohci_hcd *);
451da177e4SLinus Torvalds 
46b1878440SAlan Stern #ifdef	CONFIG_PM
ohci_rh_suspend(struct ohci_hcd * ohci,int autostop)478d1a243bSAlan Stern static int ohci_rh_suspend (struct ohci_hcd *ohci, int autostop)
488d1a243bSAlan Stern __releases(ohci->lock)
498d1a243bSAlan Stern __acquires(ohci->lock)
501da177e4SLinus Torvalds {
511da177e4SLinus Torvalds 	int			status = 0;
528de98402SBenjamin Herrenschmidt 
531da177e4SLinus Torvalds 	ohci->hc_control = ohci_readl (ohci, &ohci->regs->control);
541da177e4SLinus Torvalds 	switch (ohci->hc_control & OHCI_CTRL_HCFS) {
551da177e4SLinus Torvalds 	case OHCI_USB_RESUME:
561da177e4SLinus Torvalds 		ohci_dbg (ohci, "resume/suspend?\n");
571da177e4SLinus Torvalds 		ohci->hc_control &= ~OHCI_CTRL_HCFS;
581da177e4SLinus Torvalds 		ohci->hc_control |= OHCI_USB_RESET;
591da177e4SLinus Torvalds 		ohci_writel (ohci, ohci->hc_control, &ohci->regs->control);
601da177e4SLinus Torvalds 		(void) ohci_readl (ohci, &ohci->regs->control);
61e288fc98SGustavo A. R. Silva 		fallthrough;
621da177e4SLinus Torvalds 	case OHCI_USB_RESET:
631da177e4SLinus Torvalds 		status = -EBUSY;
641da177e4SLinus Torvalds 		ohci_dbg (ohci, "needs reinit!\n");
651da177e4SLinus Torvalds 		goto done;
661da177e4SLinus Torvalds 	case OHCI_USB_SUSPEND:
678d1a243bSAlan Stern 		if (!ohci->autostop) {
681da177e4SLinus Torvalds 			ohci_dbg (ohci, "already suspended\n");
691da177e4SLinus Torvalds 			goto done;
701da177e4SLinus Torvalds 		}
718d1a243bSAlan Stern 	}
728d1a243bSAlan Stern 	ohci_dbg (ohci, "%s root hub\n",
738d1a243bSAlan Stern 			autostop ? "auto-stop" : "suspend");
741da177e4SLinus Torvalds 
751da177e4SLinus Torvalds 	/* First stop any processing */
768d1a243bSAlan Stern 	if (!autostop && (ohci->hc_control & OHCI_SCHED_ENABLES)) {
771da177e4SLinus Torvalds 		ohci->hc_control &= ~OHCI_SCHED_ENABLES;
781da177e4SLinus Torvalds 		ohci_writel (ohci, ohci->hc_control, &ohci->regs->control);
791da177e4SLinus Torvalds 		ohci->hc_control = ohci_readl (ohci, &ohci->regs->control);
801da177e4SLinus Torvalds 		ohci_writel (ohci, OHCI_INTR_SF, &ohci->regs->intrstatus);
811da177e4SLinus Torvalds 
821da177e4SLinus Torvalds 		/* sched disables take effect on the next frame,
831da177e4SLinus Torvalds 		 * then the last WDH could take 6+ msec
841da177e4SLinus Torvalds 		 */
851da177e4SLinus Torvalds 		ohci_dbg (ohci, "stopping schedules ...\n");
868d1a243bSAlan Stern 		ohci->autostop = 0;
878d1a243bSAlan Stern 		spin_unlock_irq (&ohci->lock);
888d1a243bSAlan Stern 		msleep (8);
898d1a243bSAlan Stern 		spin_lock_irq (&ohci->lock);
901da177e4SLinus Torvalds 	}
91c6fcb85eSAlan Stern 	update_done_list(ohci);
92cdb4dd15SAlan Stern 	ohci_work(ohci);
931da177e4SLinus Torvalds 
94*20f588acSYinbo Zhu 	/* All ED unlinks should be finished, no need for SOF interrupts */
95*20f588acSYinbo Zhu 	ohci_writel(ohci, OHCI_INTR_SF, &ohci->regs->intrdisable);
96*20f588acSYinbo Zhu 
97c1db30a2SAlan Stern 	/*
98c1db30a2SAlan Stern 	 * Some controllers don't handle "global" suspend properly if
99c1db30a2SAlan Stern 	 * there are unsuspended ports.  For these controllers, put all
100c1db30a2SAlan Stern 	 * the enabled ports into suspend before suspending the root hub.
101c1db30a2SAlan Stern 	 */
102c1db30a2SAlan Stern 	if (ohci->flags & OHCI_QUIRK_GLOBAL_SUSPEND) {
103c1db30a2SAlan Stern 		__hc32 __iomem	*portstat = ohci->regs->roothub.portstatus;
104c1db30a2SAlan Stern 		int		i;
105c1db30a2SAlan Stern 		unsigned	temp;
106c1db30a2SAlan Stern 
107c1db30a2SAlan Stern 		for (i = 0; i < ohci->num_ports; (++i, ++portstat)) {
108c1db30a2SAlan Stern 			temp = ohci_readl(ohci, portstat);
109c1db30a2SAlan Stern 			if ((temp & (RH_PS_PES | RH_PS_PSS)) ==
110c1db30a2SAlan Stern 					RH_PS_PES)
111c1db30a2SAlan Stern 				ohci_writel(ohci, RH_PS_PSS, portstat);
112c1db30a2SAlan Stern 		}
113c1db30a2SAlan Stern 	}
114c1db30a2SAlan Stern 
1151da177e4SLinus Torvalds 	/* maybe resume can wake root hub */
11658a97ffeSAlan Stern 	if (ohci_to_hcd(ohci)->self.root_hub->do_remote_wakeup || autostop) {
1171da177e4SLinus Torvalds 		ohci->hc_control |= OHCI_CTRL_RWE;
11858a97ffeSAlan Stern 	} else {
1195f47493cSAlan Stern 		ohci_writel(ohci, OHCI_INTR_RHSC | OHCI_INTR_RD,
1205f47493cSAlan Stern 				&ohci->regs->intrdisable);
1211da177e4SLinus Torvalds 		ohci->hc_control &= ~OHCI_CTRL_RWE;
1221f7e1a3bSAlan Stern 	}
1231da177e4SLinus Torvalds 
124f197b2c5SDavid Brownell 	/* Suspend hub ... this is the "global (to this bus) suspend" mode,
125f197b2c5SDavid Brownell 	 * which doesn't imply ports will first be individually suspended.
126f197b2c5SDavid Brownell 	 */
1271da177e4SLinus Torvalds 	ohci->hc_control &= ~OHCI_CTRL_HCFS;
1281da177e4SLinus Torvalds 	ohci->hc_control |= OHCI_USB_SUSPEND;
1291da177e4SLinus Torvalds 	ohci_writel (ohci, ohci->hc_control, &ohci->regs->control);
1301da177e4SLinus Torvalds 	(void) ohci_readl (ohci, &ohci->regs->control);
1311da177e4SLinus Torvalds 
1321da177e4SLinus Torvalds 	/* no resumes until devices finish suspending */
1338d1a243bSAlan Stern 	if (!autostop) {
1341da177e4SLinus Torvalds 		ohci->next_statechange = jiffies + msecs_to_jiffies (5);
1358d1a243bSAlan Stern 		ohci->autostop = 0;
136b7463c71SAlan Stern 		ohci->rh_state = OHCI_RH_SUSPENDED;
1378d1a243bSAlan Stern 	}
138d413984aSDavid Brownell 
1391da177e4SLinus Torvalds done:
1401da177e4SLinus Torvalds 	return status;
1411da177e4SLinus Torvalds }
1421da177e4SLinus Torvalds 
find_head(struct ed * ed)1431da177e4SLinus Torvalds static inline struct ed *find_head (struct ed *ed)
1441da177e4SLinus Torvalds {
1451da177e4SLinus Torvalds 	/* for bulk and control lists */
1461da177e4SLinus Torvalds 	while (ed->ed_prev)
1471da177e4SLinus Torvalds 		ed = ed->ed_prev;
1481da177e4SLinus Torvalds 	return ed;
1491da177e4SLinus Torvalds }
1501da177e4SLinus Torvalds 
1511da177e4SLinus Torvalds /* caller has locked the root hub */
ohci_rh_resume(struct ohci_hcd * ohci)1528d1a243bSAlan Stern static int ohci_rh_resume (struct ohci_hcd *ohci)
1538d1a243bSAlan Stern __releases(ohci->lock)
1548d1a243bSAlan Stern __acquires(ohci->lock)
1551da177e4SLinus Torvalds {
1568d1a243bSAlan Stern 	struct usb_hcd		*hcd = ohci_to_hcd (ohci);
1571da177e4SLinus Torvalds 	u32			temp, enables;
1581da177e4SLinus Torvalds 	int			status = -EINPROGRESS;
1598d1a243bSAlan Stern 	int			autostopped = ohci->autostop;
1601da177e4SLinus Torvalds 
1618d1a243bSAlan Stern 	ohci->autostop = 0;
1621da177e4SLinus Torvalds 	ohci->hc_control = ohci_readl (ohci, &ohci->regs->control);
1631da177e4SLinus Torvalds 
1641da177e4SLinus Torvalds 	if (ohci->hc_control & (OHCI_CTRL_IR | OHCI_SCHED_ENABLES)) {
165f197b2c5SDavid Brownell 		/* this can happen after resuming a swsusp snapshot */
166b7463c71SAlan Stern 		if (ohci->rh_state != OHCI_RH_RUNNING) {
1671da177e4SLinus Torvalds 			ohci_dbg (ohci, "BIOS/SMM active, control %03x\n",
1681da177e4SLinus Torvalds 					ohci->hc_control);
1691da177e4SLinus Torvalds 			status = -EBUSY;
1701da177e4SLinus Torvalds 		/* this happens when pmcore resumes HC then root */
1711da177e4SLinus Torvalds 		} else {
1721da177e4SLinus Torvalds 			ohci_dbg (ohci, "duplicate resume\n");
1731da177e4SLinus Torvalds 			status = 0;
1741da177e4SLinus Torvalds 		}
1751da177e4SLinus Torvalds 	} else switch (ohci->hc_control & OHCI_CTRL_HCFS) {
1761da177e4SLinus Torvalds 	case OHCI_USB_SUSPEND:
1771da177e4SLinus Torvalds 		ohci->hc_control &= ~(OHCI_CTRL_HCFS|OHCI_SCHED_ENABLES);
1781da177e4SLinus Torvalds 		ohci->hc_control |= OHCI_USB_RESUME;
1791da177e4SLinus Torvalds 		ohci_writel (ohci, ohci->hc_control, &ohci->regs->control);
1801da177e4SLinus Torvalds 		(void) ohci_readl (ohci, &ohci->regs->control);
1818d1a243bSAlan Stern 		ohci_dbg (ohci, "%s root hub\n",
1828d1a243bSAlan Stern 				autostopped ? "auto-start" : "resume");
1831da177e4SLinus Torvalds 		break;
1841da177e4SLinus Torvalds 	case OHCI_USB_RESUME:
1851da177e4SLinus Torvalds 		/* HCFS changes sometime after INTR_RD */
1861b7be3c0SAlan Stern 		ohci_dbg(ohci, "%swakeup root hub\n",
187583ceadaSAlan Stern 				autostopped ? "auto-" : "");
1881da177e4SLinus Torvalds 		break;
1891da177e4SLinus Torvalds 	case OHCI_USB_OPER:
190f197b2c5SDavid Brownell 		/* this can happen after resuming a swsusp snapshot */
191f197b2c5SDavid Brownell 		ohci_dbg (ohci, "snapshot resume? reinit\n");
192f197b2c5SDavid Brownell 		status = -EBUSY;
1931da177e4SLinus Torvalds 		break;
1941da177e4SLinus Torvalds 	default:		/* RESET, we lost power */
195f197b2c5SDavid Brownell 		ohci_dbg (ohci, "lost power\n");
1961da177e4SLinus Torvalds 		status = -EBUSY;
1971da177e4SLinus Torvalds 	}
1981da177e4SLinus Torvalds 	if (status == -EBUSY) {
1998d1a243bSAlan Stern 		if (!autostopped) {
2008d1a243bSAlan Stern 			spin_unlock_irq (&ohci->lock);
2018d1a243bSAlan Stern 			status = ohci_restart (ohci);
202d576bb9fSMichael Hanselmann 
203d576bb9fSMichael Hanselmann 			usb_root_hub_lost_power(hcd->self.root_hub);
204d576bb9fSMichael Hanselmann 
2058d1a243bSAlan Stern 			spin_lock_irq (&ohci->lock);
2061da177e4SLinus Torvalds 		}
2078d1a243bSAlan Stern 		return status;
2088d1a243bSAlan Stern 	}
2091da177e4SLinus Torvalds 	if (status != -EINPROGRESS)
2101da177e4SLinus Torvalds 		return status;
2118d1a243bSAlan Stern 	if (autostopped)
2128d1a243bSAlan Stern 		goto skip_resume;
2138d1a243bSAlan Stern 	spin_unlock_irq (&ohci->lock);
2141da177e4SLinus Torvalds 
2151da177e4SLinus Torvalds 	/* Some controllers (lucent erratum) need extra-long delays */
216f197b2c5SDavid Brownell 	msleep (20 /* usb 11.5.1.10 */ + 12 /* 32 msec counter */ + 1);
2171da177e4SLinus Torvalds 
2181da177e4SLinus Torvalds 	temp = ohci_readl (ohci, &ohci->regs->control);
2191da177e4SLinus Torvalds 	temp &= OHCI_CTRL_HCFS;
2201da177e4SLinus Torvalds 	if (temp != OHCI_USB_RESUME) {
2211da177e4SLinus Torvalds 		ohci_err (ohci, "controller won't resume\n");
222565402baSAlan Stern 		spin_lock_irq(&ohci->lock);
2231da177e4SLinus Torvalds 		return -EBUSY;
2241da177e4SLinus Torvalds 	}
2251da177e4SLinus Torvalds 
2261da177e4SLinus Torvalds 	/* disable old schedule state, reinit from scratch */
2271da177e4SLinus Torvalds 	ohci_writel (ohci, 0, &ohci->regs->ed_controlhead);
2281da177e4SLinus Torvalds 	ohci_writel (ohci, 0, &ohci->regs->ed_controlcurrent);
2291da177e4SLinus Torvalds 	ohci_writel (ohci, 0, &ohci->regs->ed_bulkhead);
2301da177e4SLinus Torvalds 	ohci_writel (ohci, 0, &ohci->regs->ed_bulkcurrent);
2311da177e4SLinus Torvalds 	ohci_writel (ohci, 0, &ohci->regs->ed_periodcurrent);
2321da177e4SLinus Torvalds 	ohci_writel (ohci, (u32) ohci->hcca_dma, &ohci->regs->hcca);
2331da177e4SLinus Torvalds 
2341da177e4SLinus Torvalds 	/* Sometimes PCI D3 suspend trashes frame timings ... */
2351da177e4SLinus Torvalds 	periodic_reinit (ohci);
2361da177e4SLinus Torvalds 
2377c71c6e6SAlan Stern 	/*
2387c71c6e6SAlan Stern 	 * The following code is executed with ohci->lock held and
2397c71c6e6SAlan Stern 	 * irqs disabled if and only if autostopped is true.  This
2407c71c6e6SAlan Stern 	 * will cause sparse to warn about a "context imbalance".
2418d1a243bSAlan Stern 	 */
2428d1a243bSAlan Stern skip_resume:
2431da177e4SLinus Torvalds 	/* interrupts might have been disabled */
2441da177e4SLinus Torvalds 	ohci_writel (ohci, OHCI_INTR_INIT, &ohci->regs->intrenable);
2451da177e4SLinus Torvalds 	if (ohci->ed_rm_list)
2461da177e4SLinus Torvalds 		ohci_writel (ohci, OHCI_INTR_SF, &ohci->regs->intrenable);
2471da177e4SLinus Torvalds 
2481da177e4SLinus Torvalds 	/* Then re-enable operations */
2491da177e4SLinus Torvalds 	ohci_writel (ohci, OHCI_USB_OPER, &ohci->regs->control);
2501da177e4SLinus Torvalds 	(void) ohci_readl (ohci, &ohci->regs->control);
2518d1a243bSAlan Stern 	if (!autostopped)
2521da177e4SLinus Torvalds 		msleep (3);
2531da177e4SLinus Torvalds 
2546a9062f3SDavid Brownell 	temp = ohci->hc_control;
2556a9062f3SDavid Brownell 	temp &= OHCI_CTRL_RWC;
2566a9062f3SDavid Brownell 	temp |= OHCI_CONTROL_INIT | OHCI_USB_OPER;
2571da177e4SLinus Torvalds 	ohci->hc_control = temp;
2581da177e4SLinus Torvalds 	ohci_writel (ohci, temp, &ohci->regs->control);
2591da177e4SLinus Torvalds 	(void) ohci_readl (ohci, &ohci->regs->control);
2601da177e4SLinus Torvalds 
2611da177e4SLinus Torvalds 	/* TRSMRCY */
2628d1a243bSAlan Stern 	if (!autostopped) {
2631da177e4SLinus Torvalds 		msleep (10);
2648d1a243bSAlan Stern 		spin_lock_irq (&ohci->lock);
2658d1a243bSAlan Stern 	}
2668d1a243bSAlan Stern 	/* now ohci->lock is always held and irqs are always disabled */
2671da177e4SLinus Torvalds 
268d413984aSDavid Brownell 	/* keep it alive for more than ~5x suspend + resume costs */
269d413984aSDavid Brownell 	ohci->next_statechange = jiffies + STATECHANGE_DELAY;
2701da177e4SLinus Torvalds 
2711da177e4SLinus Torvalds 	/* maybe turn schedules back on */
2721da177e4SLinus Torvalds 	enables = 0;
2731da177e4SLinus Torvalds 	temp = 0;
2741da177e4SLinus Torvalds 	if (!ohci->ed_rm_list) {
2751da177e4SLinus Torvalds 		if (ohci->ed_controltail) {
2761da177e4SLinus Torvalds 			ohci_writel (ohci,
2771da177e4SLinus Torvalds 					find_head (ohci->ed_controltail)->dma,
2781da177e4SLinus Torvalds 					&ohci->regs->ed_controlhead);
2791da177e4SLinus Torvalds 			enables |= OHCI_CTRL_CLE;
2801da177e4SLinus Torvalds 			temp |= OHCI_CLF;
2811da177e4SLinus Torvalds 		}
2821da177e4SLinus Torvalds 		if (ohci->ed_bulktail) {
2831da177e4SLinus Torvalds 			ohci_writel (ohci, find_head (ohci->ed_bulktail)->dma,
2841da177e4SLinus Torvalds 				&ohci->regs->ed_bulkhead);
2851da177e4SLinus Torvalds 			enables |= OHCI_CTRL_BLE;
2861da177e4SLinus Torvalds 			temp |= OHCI_BLF;
2871da177e4SLinus Torvalds 		}
2881da177e4SLinus Torvalds 	}
2891da177e4SLinus Torvalds 	if (hcd->self.bandwidth_isoc_reqs || hcd->self.bandwidth_int_reqs)
2901da177e4SLinus Torvalds 		enables |= OHCI_CTRL_PLE|OHCI_CTRL_IE;
2911da177e4SLinus Torvalds 	if (enables) {
2921da177e4SLinus Torvalds 		ohci_dbg (ohci, "restarting schedules ... %08x\n", enables);
2931da177e4SLinus Torvalds 		ohci->hc_control |= enables;
2941da177e4SLinus Torvalds 		ohci_writel (ohci, ohci->hc_control, &ohci->regs->control);
2951da177e4SLinus Torvalds 		if (temp)
2961da177e4SLinus Torvalds 			ohci_writel (ohci, temp, &ohci->regs->cmdstatus);
2971da177e4SLinus Torvalds 		(void) ohci_readl (ohci, &ohci->regs->control);
2981da177e4SLinus Torvalds 	}
2991da177e4SLinus Torvalds 
300b7463c71SAlan Stern 	ohci->rh_state = OHCI_RH_RUNNING;
3011da177e4SLinus Torvalds 	return 0;
3021da177e4SLinus Torvalds }
3031da177e4SLinus Torvalds 
ohci_bus_suspend(struct usb_hcd * hcd)3048d1a243bSAlan Stern static int ohci_bus_suspend (struct usb_hcd *hcd)
3058d1a243bSAlan Stern {
3068d1a243bSAlan Stern 	struct ohci_hcd		*ohci = hcd_to_ohci (hcd);
3078d1a243bSAlan Stern 	int			rc;
3088d1a243bSAlan Stern 
3098d1a243bSAlan Stern 	spin_lock_irq (&ohci->lock);
3108d1a243bSAlan Stern 
311541c7d43SAlan Stern 	if (unlikely(!HCD_HW_ACCESSIBLE(hcd)))
3128d1a243bSAlan Stern 		rc = -ESHUTDOWN;
3138d1a243bSAlan Stern 	else
3148d1a243bSAlan Stern 		rc = ohci_rh_suspend (ohci, 0);
3158d1a243bSAlan Stern 	spin_unlock_irq (&ohci->lock);
31681e38333SAlan Stern 
317b2685bdaSShigeru Yoshida 	if (rc == 0) {
31881e38333SAlan Stern 		del_timer_sync(&ohci->io_watchdog);
319b2685bdaSShigeru Yoshida 		ohci->prev_frame_no = IO_WATCHDOG_OFF;
320b2685bdaSShigeru Yoshida 	}
3218d1a243bSAlan Stern 	return rc;
3228d1a243bSAlan Stern }
3238d1a243bSAlan Stern 
ohci_bus_resume(struct usb_hcd * hcd)3248d1a243bSAlan Stern static int ohci_bus_resume (struct usb_hcd *hcd)
3258d1a243bSAlan Stern {
3268d1a243bSAlan Stern 	struct ohci_hcd		*ohci = hcd_to_ohci (hcd);
3278d1a243bSAlan Stern 	int			rc;
3288d1a243bSAlan Stern 
3298d1a243bSAlan Stern 	if (time_before (jiffies, ohci->next_statechange))
3308d1a243bSAlan Stern 		msleep(5);
3318d1a243bSAlan Stern 
3328d1a243bSAlan Stern 	spin_lock_irq (&ohci->lock);
3338d1a243bSAlan Stern 
334541c7d43SAlan Stern 	if (unlikely(!HCD_HW_ACCESSIBLE(hcd)))
3358d1a243bSAlan Stern 		rc = -ESHUTDOWN;
3368d1a243bSAlan Stern 	else
3378d1a243bSAlan Stern 		rc = ohci_rh_resume (ohci);
3388d1a243bSAlan Stern 	spin_unlock_irq (&ohci->lock);
3398d1a243bSAlan Stern 
3408d1a243bSAlan Stern 	/* poll until we know a device is connected or we autostop */
3418d1a243bSAlan Stern 	if (rc == 0)
3428d1a243bSAlan Stern 		usb_hcd_poll_rh_status(hcd);
3438d1a243bSAlan Stern 	return rc;
3448d1a243bSAlan Stern }
3458d1a243bSAlan Stern 
3461f9fc882SAlan Stern /* Carry out polling-, autostop-, and autoresume-related state changes */
ohci_root_hub_state_changes(struct ohci_hcd * ohci,int changed,int any_connected,int rhsc_status)3471f9fc882SAlan Stern static int ohci_root_hub_state_changes(struct ohci_hcd *ohci, int changed,
34871b7497cSAlan Stern 		int any_connected, int rhsc_status)
3491f9fc882SAlan Stern {
3501f9fc882SAlan Stern 	int	poll_rh = 1;
35171b7497cSAlan Stern 	int	rhsc_enable;
3521f9fc882SAlan Stern 
353ee0b9be8SAlan Stern 	/* Some broken controllers never turn off RHSC in the interrupt
3544a511bc3SAlan Stern 	 * status register.  For their sake we won't re-enable RHSC
3554a511bc3SAlan Stern 	 * interrupts if the interrupt bit is already active.
356b5fb454fSAlan Stern 	 */
3574a511bc3SAlan Stern 	rhsc_enable = ohci_readl(ohci, &ohci->regs->intrenable) &
3584a511bc3SAlan Stern 			OHCI_INTR_RHSC;
3594a511bc3SAlan Stern 
3604a511bc3SAlan Stern 	switch (ohci->hc_control & OHCI_CTRL_HCFS) {
3614a511bc3SAlan Stern 	case OHCI_USB_OPER:
3624a511bc3SAlan Stern 		/* If no status changes are pending, enable RHSC interrupts. */
3634a511bc3SAlan Stern 		if (!rhsc_enable && !rhsc_status && !changed) {
3644a511bc3SAlan Stern 			rhsc_enable = OHCI_INTR_RHSC;
3654a511bc3SAlan Stern 			ohci_writel(ohci, rhsc_enable, &ohci->regs->intrenable);
366b5fb454fSAlan Stern 		}
367b5fb454fSAlan Stern 
368b5fb454fSAlan Stern 		/* Keep on polling until we know a device is connected
369b5fb454fSAlan Stern 		 * and RHSC is enabled, or until we autostop.
370b5fb454fSAlan Stern 		 */
3711f9fc882SAlan Stern 		if (!ohci->autostop) {
3721f9fc882SAlan Stern 			if (any_connected ||
3731f9fc882SAlan Stern 					!device_may_wakeup(&ohci_to_hcd(ohci)
3741f9fc882SAlan Stern 						->self.root_hub->dev)) {
3754a511bc3SAlan Stern 				if (rhsc_enable)
3761f9fc882SAlan Stern 					poll_rh = 0;
3771f9fc882SAlan Stern 			} else {
3781f9fc882SAlan Stern 				ohci->autostop = 1;
3791f9fc882SAlan Stern 				ohci->next_statechange = jiffies + HZ;
3801f9fc882SAlan Stern 			}
3811f9fc882SAlan Stern 
3821f9fc882SAlan Stern 		/* if no devices have been attached for one second, autostop */
3831f9fc882SAlan Stern 		} else {
3841f9fc882SAlan Stern 			if (changed || any_connected) {
3851f9fc882SAlan Stern 				ohci->autostop = 0;
3861f9fc882SAlan Stern 				ohci->next_statechange = jiffies +
3871f9fc882SAlan Stern 						STATECHANGE_DELAY;
3884a511bc3SAlan Stern 			} else if (time_after_eq(jiffies,
3891f9fc882SAlan Stern 						ohci->next_statechange)
3901f9fc882SAlan Stern 					&& !ohci->ed_rm_list
3911f9fc882SAlan Stern 					&& !(ohci->hc_control &
3921f9fc882SAlan Stern 						OHCI_SCHED_ENABLES)) {
3931f9fc882SAlan Stern 				ohci_rh_suspend(ohci, 1);
3944a511bc3SAlan Stern 				if (rhsc_enable)
395b5fb454fSAlan Stern 					poll_rh = 0;
3961f9fc882SAlan Stern 			}
3971f9fc882SAlan Stern 		}
3981f9fc882SAlan Stern 		break;
3991f9fc882SAlan Stern 
4001f9fc882SAlan Stern 	case OHCI_USB_SUSPEND:
4011f9fc882SAlan Stern 	case OHCI_USB_RESUME:
4024a511bc3SAlan Stern 		/* if there is a port change, autostart or ask to be resumed */
4031f9fc882SAlan Stern 		if (changed) {
4041f9fc882SAlan Stern 			if (ohci->autostop)
4051f9fc882SAlan Stern 				ohci_rh_resume(ohci);
4061f9fc882SAlan Stern 			else
4071f9fc882SAlan Stern 				usb_hcd_resume_root_hub(ohci_to_hcd(ohci));
40871b7497cSAlan Stern 
40971b7497cSAlan Stern 		/* If remote wakeup is disabled, stop polling */
41071b7497cSAlan Stern 		} else if (!ohci->autostop &&
41171b7497cSAlan Stern 				!ohci_to_hcd(ohci)->self.root_hub->
41271b7497cSAlan Stern 					do_remote_wakeup) {
41371b7497cSAlan Stern 			poll_rh = 0;
41471b7497cSAlan Stern 
4151f9fc882SAlan Stern 		} else {
41671b7497cSAlan Stern 			/* If no status changes are pending,
41771b7497cSAlan Stern 			 * enable RHSC interrupts
41871b7497cSAlan Stern 			 */
41971b7497cSAlan Stern 			if (!rhsc_enable && !rhsc_status) {
4204a511bc3SAlan Stern 				rhsc_enable = OHCI_INTR_RHSC;
4214a511bc3SAlan Stern 				ohci_writel(ohci, rhsc_enable,
422b5fb454fSAlan Stern 						&ohci->regs->intrenable);
4234a511bc3SAlan Stern 			}
42471b7497cSAlan Stern 			/* Keep polling until RHSC is enabled */
4254a511bc3SAlan Stern 			if (rhsc_enable)
4261f9fc882SAlan Stern 				poll_rh = 0;
4271f9fc882SAlan Stern 		}
4281f9fc882SAlan Stern 		break;
4291f9fc882SAlan Stern 	}
4301f9fc882SAlan Stern 	return poll_rh;
4311f9fc882SAlan Stern }
4321f9fc882SAlan Stern 
4331f9fc882SAlan Stern #else	/* CONFIG_PM */
4341f9fc882SAlan Stern 
ohci_rh_resume(struct ohci_hcd * ohci)4351f9fc882SAlan Stern static inline int ohci_rh_resume(struct ohci_hcd *ohci)
4361f9fc882SAlan Stern {
4371f9fc882SAlan Stern 	return 0;
4381f9fc882SAlan Stern }
4391f9fc882SAlan Stern 
4401f9fc882SAlan Stern /* Carry out polling-related state changes.
4411f9fc882SAlan Stern  * autostop isn't used when CONFIG_PM is turned off.
4421f9fc882SAlan Stern  */
ohci_root_hub_state_changes(struct ohci_hcd * ohci,int changed,int any_connected,int rhsc_status)4431f9fc882SAlan Stern static int ohci_root_hub_state_changes(struct ohci_hcd *ohci, int changed,
44471b7497cSAlan Stern 		int any_connected, int rhsc_status)
4451f9fc882SAlan Stern {
446b5fb454fSAlan Stern 	/* If RHSC is enabled, don't poll */
44709ca8adbSLinus Torvalds 	if (ohci_readl(ohci, &ohci->regs->intrenable) & OHCI_INTR_RHSC)
448b5fb454fSAlan Stern 		return 0;
449b5fb454fSAlan Stern 
45071b7497cSAlan Stern 	/* If status changes are pending, continue polling.
45171b7497cSAlan Stern 	 * Conversely, if no status changes are pending but the RHSC
45271b7497cSAlan Stern 	 * status bit was set, then RHSC may be broken so continue polling.
45371b7497cSAlan Stern 	 */
45471b7497cSAlan Stern 	if (changed || rhsc_status)
45571b7497cSAlan Stern 		return 1;
45671b7497cSAlan Stern 
45771b7497cSAlan Stern 	/* It's safe to re-enable RHSC interrupts */
458b5fb454fSAlan Stern 	ohci_writel(ohci, OHCI_INTR_RHSC, &ohci->regs->intrenable);
459b5fb454fSAlan Stern 	return 0;
460b5fb454fSAlan Stern }
4611f9fc882SAlan Stern 
4628ad7fe16SDavid Brownell #endif	/* CONFIG_PM */
4631da177e4SLinus Torvalds 
4641da177e4SLinus Torvalds /*-------------------------------------------------------------------------*/
4651da177e4SLinus Torvalds 
4661da177e4SLinus Torvalds /* build "status change" packet (one or two bytes) from HC registers */
4671da177e4SLinus Torvalds 
ohci_hub_status_data(struct usb_hcd * hcd,char * buf)46842b59ebaSLaurent Pinchart int ohci_hub_status_data(struct usb_hcd *hcd, char *buf)
4691da177e4SLinus Torvalds {
4701da177e4SLinus Torvalds 	struct ohci_hcd	*ohci = hcd_to_ohci (hcd);
471fdd13b36SDavid Brownell 	int		i, changed = 0, length = 1;
472052ac01aSAlan Stern 	int		any_connected = 0;
47371b7497cSAlan Stern 	int		rhsc_status;
4741da177e4SLinus Torvalds 	unsigned long	flags;
4751da177e4SLinus Torvalds 
4761da177e4SLinus Torvalds 	spin_lock_irqsave (&ohci->lock, flags);
477541c7d43SAlan Stern 	if (!HCD_HW_ACCESSIBLE(hcd))
4786fd75b19SAlan Stern 		goto done;
4791da177e4SLinus Torvalds 
480fdd13b36SDavid Brownell 	/* undocumented erratum seen on at least rev D */
481fdd13b36SDavid Brownell 	if ((ohci->flags & OHCI_QUIRK_AMD756)
482fdd13b36SDavid Brownell 			&& (roothub_a (ohci) & RH_A_NDP) > MAX_ROOT_PORTS) {
483fdd13b36SDavid Brownell 		ohci_warn (ohci, "bogus NDP, rereads as NDP=%d\n",
4841da177e4SLinus Torvalds 			  ohci_readl (ohci, &ohci->regs->roothub.a) & RH_A_NDP);
4851da177e4SLinus Torvalds 		/* retry later; "should not happen" */
4861da177e4SLinus Torvalds 		goto done;
4871da177e4SLinus Torvalds 	}
4881da177e4SLinus Torvalds 
4891da177e4SLinus Torvalds 	/* init status */
4901da177e4SLinus Torvalds 	if (roothub_status (ohci) & (RH_HS_LPSC | RH_HS_OCIC))
4911da177e4SLinus Torvalds 		buf [0] = changed = 1;
4921da177e4SLinus Torvalds 	else
4931da177e4SLinus Torvalds 		buf [0] = 0;
494fdd13b36SDavid Brownell 	if (ohci->num_ports > 7) {
4951da177e4SLinus Torvalds 		buf [1] = 0;
4961da177e4SLinus Torvalds 		length++;
4971da177e4SLinus Torvalds 	}
4981da177e4SLinus Torvalds 
49971b7497cSAlan Stern 	/* Clear the RHSC status flag before reading the port statuses */
50071b7497cSAlan Stern 	ohci_writel(ohci, OHCI_INTR_RHSC, &ohci->regs->intrstatus);
50171b7497cSAlan Stern 	rhsc_status = ohci_readl(ohci, &ohci->regs->intrstatus) &
50271b7497cSAlan Stern 			OHCI_INTR_RHSC;
50371b7497cSAlan Stern 
5041da177e4SLinus Torvalds 	/* look at each port */
505fdd13b36SDavid Brownell 	for (i = 0; i < ohci->num_ports; i++) {
5061da177e4SLinus Torvalds 		u32	status = roothub_portstatus (ohci, i);
5071da177e4SLinus Torvalds 
5088d1a243bSAlan Stern 		/* can't autostop if ports are connected */
5098d1a243bSAlan Stern 		any_connected |= (status & RH_PS_CCS);
5108d1a243bSAlan Stern 
5111da177e4SLinus Torvalds 		if (status & (RH_PS_CSC | RH_PS_PESC | RH_PS_PSSC
5121da177e4SLinus Torvalds 				| RH_PS_OCIC | RH_PS_PRSC)) {
5131da177e4SLinus Torvalds 			changed = 1;
5141da177e4SLinus Torvalds 			if (i < 7)
5151da177e4SLinus Torvalds 			    buf [0] |= 1 << (i + 1);
5161da177e4SLinus Torvalds 			else
5171da177e4SLinus Torvalds 			    buf [1] |= 1 << (i - 7);
5181da177e4SLinus Torvalds 		}
5191da177e4SLinus Torvalds 	}
520d413984aSDavid Brownell 
521541c7d43SAlan Stern 	if (ohci_root_hub_state_changes(ohci, changed,
522541c7d43SAlan Stern 			any_connected, rhsc_status))
523541c7d43SAlan Stern 		set_bit(HCD_FLAG_POLL_RH, &hcd->flags);
524541c7d43SAlan Stern 	else
525541c7d43SAlan Stern 		clear_bit(HCD_FLAG_POLL_RH, &hcd->flags);
526541c7d43SAlan Stern 
527d413984aSDavid Brownell 
5281da177e4SLinus Torvalds done:
5291da177e4SLinus Torvalds 	spin_unlock_irqrestore (&ohci->lock, flags);
5301da177e4SLinus Torvalds 
5311da177e4SLinus Torvalds 	return changed ? length : 0;
5321da177e4SLinus Torvalds }
53342b59ebaSLaurent Pinchart EXPORT_SYMBOL_GPL(ohci_hub_status_data);
5341da177e4SLinus Torvalds 
5351da177e4SLinus Torvalds /*-------------------------------------------------------------------------*/
5361da177e4SLinus Torvalds 
5371da177e4SLinus Torvalds static void
ohci_hub_descriptor(struct ohci_hcd * ohci,struct usb_hub_descriptor * desc)5381da177e4SLinus Torvalds ohci_hub_descriptor (
5391da177e4SLinus Torvalds 	struct ohci_hcd			*ohci,
5401da177e4SLinus Torvalds 	struct usb_hub_descriptor	*desc
5411da177e4SLinus Torvalds ) {
5421da177e4SLinus Torvalds 	u32		rh = roothub_a (ohci);
5431da177e4SLinus Torvalds 	u16		temp;
5441da177e4SLinus Torvalds 
5457a2d2672SSergei Shtylyov 	desc->bDescriptorType = USB_DT_HUB;
5461da177e4SLinus Torvalds 	desc->bPwrOn2PwrGood = (rh & RH_A_POTPGT) >> 24;
5471da177e4SLinus Torvalds 	desc->bHubContrCurrent = 0;
5481da177e4SLinus Torvalds 
549fdd13b36SDavid Brownell 	desc->bNbrPorts = ohci->num_ports;
550fdd13b36SDavid Brownell 	temp = 1 + (ohci->num_ports / 8);
5511da177e4SLinus Torvalds 	desc->bDescLength = 7 + 2 * temp;
5521da177e4SLinus Torvalds 
553db42a4c9SSergei Shtylyov 	temp = HUB_CHAR_COMMON_LPSM | HUB_CHAR_COMMON_OCPM;
5541da177e4SLinus Torvalds 	if (rh & RH_A_NPS)		/* no power switching? */
555db42a4c9SSergei Shtylyov 		temp |= HUB_CHAR_NO_LPSM;
5561da177e4SLinus Torvalds 	if (rh & RH_A_PSM)		/* per-port power switching? */
557db42a4c9SSergei Shtylyov 		temp |= HUB_CHAR_INDV_PORT_LPSM;
5581da177e4SLinus Torvalds 	if (rh & RH_A_NOCP)		/* no overcurrent reporting? */
559db42a4c9SSergei Shtylyov 		temp |= HUB_CHAR_NO_OCPM;
5601da177e4SLinus Torvalds 	else if (rh & RH_A_OCPM)	/* per-port overcurrent reporting? */
561db42a4c9SSergei Shtylyov 		temp |= HUB_CHAR_INDV_PORT_OCPM;
5627c71c6e6SAlan Stern 	desc->wHubCharacteristics = cpu_to_le16(temp);
5631da177e4SLinus Torvalds 
564da13051cSSarah Sharp 	/* ports removable, and usb 1.0 legacy PortPwrCtrlMask */
5651da177e4SLinus Torvalds 	rh = roothub_b (ohci);
566dbe79bbeSJohn Youn 	memset(desc->u.hs.DeviceRemovable, 0xff,
567dbe79bbeSJohn Youn 			sizeof(desc->u.hs.DeviceRemovable));
568dbe79bbeSJohn Youn 	desc->u.hs.DeviceRemovable[0] = rh & RH_B_DR;
569fdd13b36SDavid Brownell 	if (ohci->num_ports > 7) {
570dbe79bbeSJohn Youn 		desc->u.hs.DeviceRemovable[1] = (rh & RH_B_DR) >> 8;
571dbe79bbeSJohn Youn 		desc->u.hs.DeviceRemovable[2] = 0xff;
5721da177e4SLinus Torvalds 	} else
573dbe79bbeSJohn Youn 		desc->u.hs.DeviceRemovable[1] = 0xff;
5741da177e4SLinus Torvalds }
5751da177e4SLinus Torvalds 
5761da177e4SLinus Torvalds /*-------------------------------------------------------------------------*/
5771da177e4SLinus Torvalds 
5781da177e4SLinus Torvalds #ifdef	CONFIG_USB_OTG
5791da177e4SLinus Torvalds 
ohci_start_port_reset(struct usb_hcd * hcd,unsigned port)5801da177e4SLinus Torvalds static int ohci_start_port_reset (struct usb_hcd *hcd, unsigned port)
5811da177e4SLinus Torvalds {
5821da177e4SLinus Torvalds 	struct ohci_hcd	*ohci = hcd_to_ohci (hcd);
5831da177e4SLinus Torvalds 	u32			status;
5841da177e4SLinus Torvalds 
5851da177e4SLinus Torvalds 	if (!port)
5861da177e4SLinus Torvalds 		return -EINVAL;
5871da177e4SLinus Torvalds 	port--;
5881da177e4SLinus Torvalds 
5891da177e4SLinus Torvalds 	/* start port reset before HNP protocol times out */
5901da177e4SLinus Torvalds 	status = ohci_readl(ohci, &ohci->regs->roothub.portstatus [port]);
5911da177e4SLinus Torvalds 	if (!(status & RH_PS_CCS))
5921da177e4SLinus Torvalds 		return -ENODEV;
5931da177e4SLinus Torvalds 
59437ebb549SPetr Mladek 	/* hub_wq will finish the reset later */
5951da177e4SLinus Torvalds 	ohci_writel(ohci, RH_PS_PRS, &ohci->regs->roothub.portstatus [port]);
5961da177e4SLinus Torvalds 	return 0;
5971da177e4SLinus Torvalds }
5981da177e4SLinus Torvalds 
5991da177e4SLinus Torvalds #else
6001da177e4SLinus Torvalds 
6011da177e4SLinus Torvalds #define	ohci_start_port_reset		NULL
6021da177e4SLinus Torvalds 
6031da177e4SLinus Torvalds #endif
6041da177e4SLinus Torvalds 
6051da177e4SLinus Torvalds /*-------------------------------------------------------------------------*/
6061da177e4SLinus Torvalds 
6071da177e4SLinus Torvalds 
6081da177e4SLinus Torvalds /* See usb 7.1.7.5:  root hubs must issue at least 50 msec reset signaling,
6091da177e4SLinus Torvalds  * not necessarily continuous ... to guard against resume signaling.
6101da177e4SLinus Torvalds  */
6111da177e4SLinus Torvalds #define	PORT_RESET_MSEC		50
6121da177e4SLinus Torvalds 
6131da177e4SLinus Torvalds /* this timer value might be vendor-specific ... */
6141da177e4SLinus Torvalds #define	PORT_RESET_HW_MSEC	10
6151da177e4SLinus Torvalds 
6161da177e4SLinus Torvalds /* wrap-aware logic morphed from <linux/jiffies.h> */
6171da177e4SLinus Torvalds #define tick_before(t1,t2) ((s16)(((s16)(t1))-((s16)(t2))) < 0)
6181da177e4SLinus Torvalds 
61937ebb549SPetr Mladek /* called from some task, normally hub_wq */
root_port_reset(struct ohci_hcd * ohci,unsigned port)62023d10a9eSTakamasa Ohtake static inline int root_port_reset (struct ohci_hcd *ohci, unsigned port)
6211da177e4SLinus Torvalds {
6221da177e4SLinus Torvalds 	__hc32 __iomem *portstat = &ohci->regs->roothub.portstatus [port];
6231b7b61c5SAlan Stern 	u32	temp = 0;
6241da177e4SLinus Torvalds 	u16	now = ohci_readl(ohci, &ohci->regs->fmnumber);
6251da177e4SLinus Torvalds 	u16	reset_done = now + PORT_RESET_MSEC;
626e01e7fe3SDavid Brownell 	int	limit_1 = DIV_ROUND_UP(PORT_RESET_MSEC, PORT_RESET_HW_MSEC);
6271da177e4SLinus Torvalds 
6281da177e4SLinus Torvalds 	/* build a "continuous enough" reset signal, with up to
6291da177e4SLinus Torvalds 	 * 3msec gap between pulses.  scheduler HZ==100 must work;
6301da177e4SLinus Torvalds 	 * this might need to be deadline-scheduled.
6311da177e4SLinus Torvalds 	 */
6321da177e4SLinus Torvalds 	do {
633e01e7fe3SDavid Brownell 		int limit_2;
634e01e7fe3SDavid Brownell 
6351da177e4SLinus Torvalds 		/* spin until any current reset finishes */
636e01e7fe3SDavid Brownell 		limit_2 = PORT_RESET_HW_MSEC * 2;
637e01e7fe3SDavid Brownell 		while (--limit_2 >= 0) {
6381da177e4SLinus Torvalds 			temp = ohci_readl (ohci, portstat);
63923d10a9eSTakamasa Ohtake 			/* handle e.g. CardBus eject */
64023d10a9eSTakamasa Ohtake 			if (temp == ~(u32)0)
64123d10a9eSTakamasa Ohtake 				return -ESHUTDOWN;
6421da177e4SLinus Torvalds 			if (!(temp & RH_PS_PRS))
6431da177e4SLinus Torvalds 				break;
6441da177e4SLinus Torvalds 			udelay (500);
6451da177e4SLinus Torvalds 		}
6461da177e4SLinus Torvalds 
647e01e7fe3SDavid Brownell 		/* timeout (a hardware error) has been observed when
648e01e7fe3SDavid Brownell 		 * EHCI sets CF while this driver is resetting a port;
649e01e7fe3SDavid Brownell 		 * presumably other disconnect paths might do it too.
650e01e7fe3SDavid Brownell 		 */
651e01e7fe3SDavid Brownell 		if (limit_2 < 0) {
652e01e7fe3SDavid Brownell 			ohci_dbg(ohci,
653e01e7fe3SDavid Brownell 				"port[%d] reset timeout, stat %08x\n",
654e01e7fe3SDavid Brownell 				port, temp);
655e01e7fe3SDavid Brownell 			break;
656e01e7fe3SDavid Brownell 		}
657e01e7fe3SDavid Brownell 
6581da177e4SLinus Torvalds 		if (!(temp & RH_PS_CCS))
6591da177e4SLinus Torvalds 			break;
6601da177e4SLinus Torvalds 		if (temp & RH_PS_PRSC)
6611da177e4SLinus Torvalds 			ohci_writel (ohci, RH_PS_PRSC, portstat);
6621da177e4SLinus Torvalds 
6631da177e4SLinus Torvalds 		/* start the next reset, sleep till it's probably done */
6641da177e4SLinus Torvalds 		ohci_writel (ohci, RH_PS_PRS, portstat);
6651da177e4SLinus Torvalds 		msleep(PORT_RESET_HW_MSEC);
6661da177e4SLinus Torvalds 		now = ohci_readl(ohci, &ohci->regs->fmnumber);
667e01e7fe3SDavid Brownell 	} while (tick_before(now, reset_done) && --limit_1 >= 0);
668e01e7fe3SDavid Brownell 
669e01e7fe3SDavid Brownell 	/* caller synchronizes using PRSC ... and handles PRS
670e01e7fe3SDavid Brownell 	 * still being set when this returns.
671e01e7fe3SDavid Brownell 	 */
67223d10a9eSTakamasa Ohtake 
67323d10a9eSTakamasa Ohtake 	return 0;
6741da177e4SLinus Torvalds }
6751da177e4SLinus Torvalds 
ohci_hub_control(struct usb_hcd * hcd,u16 typeReq,u16 wValue,u16 wIndex,char * buf,u16 wLength)67642b59ebaSLaurent Pinchart int ohci_hub_control(
6771da177e4SLinus Torvalds 	struct usb_hcd	*hcd,
6781da177e4SLinus Torvalds 	u16		typeReq,
6791da177e4SLinus Torvalds 	u16		wValue,
6801da177e4SLinus Torvalds 	u16		wIndex,
6811da177e4SLinus Torvalds 	char		*buf,
6821da177e4SLinus Torvalds 	u16		wLength
6831da177e4SLinus Torvalds ) {
6841da177e4SLinus Torvalds 	struct ohci_hcd	*ohci = hcd_to_ohci (hcd);
685fcf7d214SAlan Stern 	int		ports = ohci->num_ports;
6861da177e4SLinus Torvalds 	u32		temp;
6871da177e4SLinus Torvalds 	int		retval = 0;
6881da177e4SLinus Torvalds 
689541c7d43SAlan Stern 	if (unlikely(!HCD_HW_ACCESSIBLE(hcd)))
6908de98402SBenjamin Herrenschmidt 		return -ESHUTDOWN;
6918de98402SBenjamin Herrenschmidt 
6921da177e4SLinus Torvalds 	switch (typeReq) {
6931da177e4SLinus Torvalds 	case ClearHubFeature:
6941da177e4SLinus Torvalds 		switch (wValue) {
6951da177e4SLinus Torvalds 		case C_HUB_OVER_CURRENT:
6961da177e4SLinus Torvalds 			ohci_writel (ohci, RH_HS_OCIC,
6971da177e4SLinus Torvalds 					&ohci->regs->roothub.status);
6981d6903a6SNick Desaulniers 			break;
6991da177e4SLinus Torvalds 		case C_HUB_LOCAL_POWER:
7001da177e4SLinus Torvalds 			break;
7011da177e4SLinus Torvalds 		default:
7021da177e4SLinus Torvalds 			goto error;
7031da177e4SLinus Torvalds 		}
7041da177e4SLinus Torvalds 		break;
7051da177e4SLinus Torvalds 	case ClearPortFeature:
7061da177e4SLinus Torvalds 		if (!wIndex || wIndex > ports)
7071da177e4SLinus Torvalds 			goto error;
7081da177e4SLinus Torvalds 		wIndex--;
7091da177e4SLinus Torvalds 
7101da177e4SLinus Torvalds 		switch (wValue) {
7111da177e4SLinus Torvalds 		case USB_PORT_FEAT_ENABLE:
7121da177e4SLinus Torvalds 			temp = RH_PS_CCS;
7131da177e4SLinus Torvalds 			break;
7141da177e4SLinus Torvalds 		case USB_PORT_FEAT_C_ENABLE:
7151da177e4SLinus Torvalds 			temp = RH_PS_PESC;
7161da177e4SLinus Torvalds 			break;
7171da177e4SLinus Torvalds 		case USB_PORT_FEAT_SUSPEND:
7181da177e4SLinus Torvalds 			temp = RH_PS_POCI;
7191da177e4SLinus Torvalds 			break;
7201da177e4SLinus Torvalds 		case USB_PORT_FEAT_C_SUSPEND:
7211da177e4SLinus Torvalds 			temp = RH_PS_PSSC;
7221da177e4SLinus Torvalds 			break;
7231da177e4SLinus Torvalds 		case USB_PORT_FEAT_POWER:
7241da177e4SLinus Torvalds 			temp = RH_PS_LSDA;
7251da177e4SLinus Torvalds 			break;
7261da177e4SLinus Torvalds 		case USB_PORT_FEAT_C_CONNECTION:
7271da177e4SLinus Torvalds 			temp = RH_PS_CSC;
7281da177e4SLinus Torvalds 			break;
7291da177e4SLinus Torvalds 		case USB_PORT_FEAT_C_OVER_CURRENT:
7301da177e4SLinus Torvalds 			temp = RH_PS_OCIC;
7311da177e4SLinus Torvalds 			break;
7321da177e4SLinus Torvalds 		case USB_PORT_FEAT_C_RESET:
7331da177e4SLinus Torvalds 			temp = RH_PS_PRSC;
7341da177e4SLinus Torvalds 			break;
7351da177e4SLinus Torvalds 		default:
7361da177e4SLinus Torvalds 			goto error;
7371da177e4SLinus Torvalds 		}
7381da177e4SLinus Torvalds 		ohci_writel (ohci, temp,
7391da177e4SLinus Torvalds 				&ohci->regs->roothub.portstatus [wIndex]);
7401da177e4SLinus Torvalds 		// ohci_readl (ohci, &ohci->regs->roothub.portstatus [wIndex]);
7411da177e4SLinus Torvalds 		break;
7421da177e4SLinus Torvalds 	case GetHubDescriptor:
7431da177e4SLinus Torvalds 		ohci_hub_descriptor (ohci, (struct usb_hub_descriptor *) buf);
7441da177e4SLinus Torvalds 		break;
7451da177e4SLinus Torvalds 	case GetHubStatus:
7461da177e4SLinus Torvalds 		temp = roothub_status (ohci) & ~(RH_HS_CRWE | RH_HS_DRWE);
747a5abdeafSHarvey Harrison 		put_unaligned_le32(temp, buf);
7481da177e4SLinus Torvalds 		break;
7491da177e4SLinus Torvalds 	case GetPortStatus:
7501da177e4SLinus Torvalds 		if (!wIndex || wIndex > ports)
7511da177e4SLinus Torvalds 			goto error;
7521da177e4SLinus Torvalds 		wIndex--;
7531da177e4SLinus Torvalds 		temp = roothub_portstatus (ohci, wIndex);
754a5abdeafSHarvey Harrison 		put_unaligned_le32(temp, buf);
7551da177e4SLinus Torvalds 
7561da177e4SLinus Torvalds 		if (*(u16*)(buf+2))	/* only if wPortChange is interesting */
7571da177e4SLinus Torvalds 			dbg_port(ohci, "GetStatus", wIndex, temp);
7581da177e4SLinus Torvalds 		break;
7591da177e4SLinus Torvalds 	case SetHubFeature:
7601da177e4SLinus Torvalds 		switch (wValue) {
7611da177e4SLinus Torvalds 		case C_HUB_OVER_CURRENT:
7621da177e4SLinus Torvalds 			// FIXME:  this can be cleared, yes?
7631da177e4SLinus Torvalds 		case C_HUB_LOCAL_POWER:
7641da177e4SLinus Torvalds 			break;
7651da177e4SLinus Torvalds 		default:
7661da177e4SLinus Torvalds 			goto error;
7671da177e4SLinus Torvalds 		}
7681da177e4SLinus Torvalds 		break;
7691da177e4SLinus Torvalds 	case SetPortFeature:
7701da177e4SLinus Torvalds 		if (!wIndex || wIndex > ports)
7711da177e4SLinus Torvalds 			goto error;
7721da177e4SLinus Torvalds 		wIndex--;
7731da177e4SLinus Torvalds 		switch (wValue) {
7741da177e4SLinus Torvalds 		case USB_PORT_FEAT_SUSPEND:
7751da177e4SLinus Torvalds #ifdef	CONFIG_USB_OTG
7761da177e4SLinus Torvalds 			if (hcd->self.otg_port == (wIndex + 1)
7771da177e4SLinus Torvalds 					&& hcd->self.b_hnp_enable)
778e8b24450SDmitry Baryshkov 				ohci->start_hnp(ohci);
7791da177e4SLinus Torvalds 			else
7801da177e4SLinus Torvalds #endif
7811da177e4SLinus Torvalds 			ohci_writel (ohci, RH_PS_PSS,
7821da177e4SLinus Torvalds 				&ohci->regs->roothub.portstatus [wIndex]);
7831da177e4SLinus Torvalds 			break;
7841da177e4SLinus Torvalds 		case USB_PORT_FEAT_POWER:
7851da177e4SLinus Torvalds 			ohci_writel (ohci, RH_PS_PPS,
7861da177e4SLinus Torvalds 				&ohci->regs->roothub.portstatus [wIndex]);
7871da177e4SLinus Torvalds 			break;
7881da177e4SLinus Torvalds 		case USB_PORT_FEAT_RESET:
78923d10a9eSTakamasa Ohtake 			retval = root_port_reset (ohci, wIndex);
7901da177e4SLinus Torvalds 			break;
7911da177e4SLinus Torvalds 		default:
7921da177e4SLinus Torvalds 			goto error;
7931da177e4SLinus Torvalds 		}
7941da177e4SLinus Torvalds 		break;
7951da177e4SLinus Torvalds 
7961da177e4SLinus Torvalds 	default:
7971da177e4SLinus Torvalds error:
7981da177e4SLinus Torvalds 		/* "protocol stall" on error */
7991da177e4SLinus Torvalds 		retval = -EPIPE;
8001da177e4SLinus Torvalds 	}
8011da177e4SLinus Torvalds 	return retval;
8021da177e4SLinus Torvalds }
80342b59ebaSLaurent Pinchart EXPORT_SYMBOL_GPL(ohci_hub_control);
804