xref: /linux/drivers/usb/host/ehci-hcd.c (revision 0e5f231bc16ff9910882fa5b9d64d80e7691cfab)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  * Copyright (c) 2000-2004 by David Brownell
31da177e4SLinus Torvalds  *
41da177e4SLinus Torvalds  * This program is free software; you can redistribute it and/or modify it
51da177e4SLinus Torvalds  * under the terms of the GNU General Public License as published by the
61da177e4SLinus Torvalds  * Free Software Foundation; either version 2 of the License, or (at your
71da177e4SLinus Torvalds  * option) any later version.
81da177e4SLinus Torvalds  *
91da177e4SLinus Torvalds  * This program is distributed in the hope that it will be useful, but
101da177e4SLinus Torvalds  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
111da177e4SLinus Torvalds  * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
121da177e4SLinus Torvalds  * for more details.
131da177e4SLinus Torvalds  *
141da177e4SLinus Torvalds  * You should have received a copy of the GNU General Public License
151da177e4SLinus Torvalds  * along with this program; if not, write to the Free Software Foundation,
161da177e4SLinus Torvalds  * Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
171da177e4SLinus Torvalds  */
181da177e4SLinus Torvalds 
191da177e4SLinus Torvalds #include <linux/module.h>
201da177e4SLinus Torvalds #include <linux/pci.h>
211da177e4SLinus Torvalds #include <linux/dmapool.h>
221da177e4SLinus Torvalds #include <linux/kernel.h>
231da177e4SLinus Torvalds #include <linux/delay.h>
241da177e4SLinus Torvalds #include <linux/ioport.h>
251da177e4SLinus Torvalds #include <linux/sched.h>
263c04e20eSMing Lei #include <linux/vmalloc.h>
271da177e4SLinus Torvalds #include <linux/errno.h>
281da177e4SLinus Torvalds #include <linux/init.h>
291da177e4SLinus Torvalds #include <linux/timer.h>
30ee4ecb8aSOliver Neukum #include <linux/ktime.h>
311da177e4SLinus Torvalds #include <linux/list.h>
321da177e4SLinus Torvalds #include <linux/interrupt.h>
331da177e4SLinus Torvalds #include <linux/usb.h>
341da177e4SLinus Torvalds #include <linux/moduleparam.h>
351da177e4SLinus Torvalds #include <linux/dma-mapping.h>
36694cc208STony Jones #include <linux/debugfs.h>
375a0e3ad6STejun Heo #include <linux/slab.h>
381da177e4SLinus Torvalds 
391da177e4SLinus Torvalds #include "../core/hcd.h"
401da177e4SLinus Torvalds 
411da177e4SLinus Torvalds #include <asm/byteorder.h>
421da177e4SLinus Torvalds #include <asm/io.h>
431da177e4SLinus Torvalds #include <asm/irq.h>
441da177e4SLinus Torvalds #include <asm/system.h>
451da177e4SLinus Torvalds #include <asm/unaligned.h>
461da177e4SLinus Torvalds 
471da177e4SLinus Torvalds /*-------------------------------------------------------------------------*/
481da177e4SLinus Torvalds 
491da177e4SLinus Torvalds /*
501da177e4SLinus Torvalds  * EHCI hc_driver implementation ... experimental, incomplete.
511da177e4SLinus Torvalds  * Based on the final 1.0 register interface specification.
521da177e4SLinus Torvalds  *
531da177e4SLinus Torvalds  * USB 2.0 shows up in upcoming www.pcmcia.org technology.
541da177e4SLinus Torvalds  * First was PCMCIA, like ISA; then CardBus, which is PCI.
551da177e4SLinus Torvalds  * Next comes "CardBay", using USB 2.0 signals.
561da177e4SLinus Torvalds  *
571da177e4SLinus Torvalds  * Contains additional contributions by Brad Hards, Rory Bolt, and others.
581da177e4SLinus Torvalds  * Special thanks to Intel and VIA for providing host controllers to
591da177e4SLinus Torvalds  * test this driver on, and Cypress (including In-System Design) for
601da177e4SLinus Torvalds  * providing early devices for those host controllers to talk to!
611da177e4SLinus Torvalds  */
621da177e4SLinus Torvalds 
631da177e4SLinus Torvalds #define DRIVER_AUTHOR "David Brownell"
641da177e4SLinus Torvalds #define DRIVER_DESC "USB 2.0 'Enhanced' Host Controller (EHCI) Driver"
651da177e4SLinus Torvalds 
661da177e4SLinus Torvalds static const char	hcd_name [] = "ehci_hcd";
671da177e4SLinus Torvalds 
681da177e4SLinus Torvalds 
699776afc8SDavid Brownell #undef VERBOSE_DEBUG
701da177e4SLinus Torvalds #undef EHCI_URB_TRACE
711da177e4SLinus Torvalds 
721da177e4SLinus Torvalds #ifdef DEBUG
731da177e4SLinus Torvalds #define EHCI_STATS
741da177e4SLinus Torvalds #endif
751da177e4SLinus Torvalds 
761da177e4SLinus Torvalds /* magic numbers that can affect system performance */
771da177e4SLinus Torvalds #define	EHCI_TUNE_CERR		3	/* 0-3 qtd retries; 0 == don't stop */
781da177e4SLinus Torvalds #define	EHCI_TUNE_RL_HS		4	/* nak throttle; see 4.9 */
791da177e4SLinus Torvalds #define	EHCI_TUNE_RL_TT		0
801da177e4SLinus Torvalds #define	EHCI_TUNE_MULT_HS	1	/* 1-3 transactions/uframe; 4.10.3 */
811da177e4SLinus Torvalds #define	EHCI_TUNE_MULT_TT	1
821da177e4SLinus Torvalds #define	EHCI_TUNE_FLS		2	/* (small) 256 frame schedule */
831da177e4SLinus Torvalds 
8407d29b63SAlan Stern #define EHCI_IAA_MSECS		10		/* arbitrary */
851da177e4SLinus Torvalds #define EHCI_IO_JIFFIES		(HZ/10)		/* io watchdog > irq_thresh */
861da177e4SLinus Torvalds #define EHCI_ASYNC_JIFFIES	(HZ/20)		/* async idle timeout */
87b9638011SDavid Brownell #define EHCI_SHRINK_FRAMES	5		/* async qh unlink delay */
881da177e4SLinus Torvalds 
891da177e4SLinus Torvalds /* Initial IRQ latency:  faster than hw default */
901da177e4SLinus Torvalds static int log2_irq_thresh = 0;		// 0 to 6
911da177e4SLinus Torvalds module_param (log2_irq_thresh, int, S_IRUGO);
921da177e4SLinus Torvalds MODULE_PARM_DESC (log2_irq_thresh, "log2 IRQ latency, 1-64 microframes");
931da177e4SLinus Torvalds 
941da177e4SLinus Torvalds /* initial park setting:  slower than hw default */
951da177e4SLinus Torvalds static unsigned park = 0;
961da177e4SLinus Torvalds module_param (park, uint, S_IRUGO);
971da177e4SLinus Torvalds MODULE_PARM_DESC (park, "park setting; 1-3 back-to-back async packets");
981da177e4SLinus Torvalds 
9993f1a47cSDavid Brownell /* for flakey hardware, ignore overcurrent indicators */
10093f1a47cSDavid Brownell static int ignore_oc = 0;
10193f1a47cSDavid Brownell module_param (ignore_oc, bool, S_IRUGO);
10293f1a47cSDavid Brownell MODULE_PARM_DESC (ignore_oc, "ignore bogus hardware overcurrent indications");
10393f1a47cSDavid Brownell 
1041da177e4SLinus Torvalds #define	INTR_MASK (STS_IAA | STS_FATAL | STS_PCD | STS_ERR | STS_INT)
1051da177e4SLinus Torvalds 
1061da177e4SLinus Torvalds /*-------------------------------------------------------------------------*/
1071da177e4SLinus Torvalds 
1081da177e4SLinus Torvalds #include "ehci.h"
1091da177e4SLinus Torvalds #include "ehci-dbg.c"
1101da177e4SLinus Torvalds 
1111da177e4SLinus Torvalds /*-------------------------------------------------------------------------*/
1121da177e4SLinus Torvalds 
113bc29847eSAlan Stern static void
114bc29847eSAlan Stern timer_action(struct ehci_hcd *ehci, enum ehci_timer_action action)
115bc29847eSAlan Stern {
116bc29847eSAlan Stern 	/* Don't override timeouts which shrink or (later) disable
117bc29847eSAlan Stern 	 * the async ring; just the I/O watchdog.  Note that if a
118bc29847eSAlan Stern 	 * SHRINK were pending, OFF would never be requested.
119bc29847eSAlan Stern 	 */
120bc29847eSAlan Stern 	if (timer_pending(&ehci->watchdog)
121bc29847eSAlan Stern 			&& ((BIT(TIMER_ASYNC_SHRINK) | BIT(TIMER_ASYNC_OFF))
122bc29847eSAlan Stern 				& ehci->actions))
123bc29847eSAlan Stern 		return;
124bc29847eSAlan Stern 
125bc29847eSAlan Stern 	if (!test_and_set_bit(action, &ehci->actions)) {
126bc29847eSAlan Stern 		unsigned long t;
127bc29847eSAlan Stern 
128bc29847eSAlan Stern 		switch (action) {
129bc29847eSAlan Stern 		case TIMER_IO_WATCHDOG:
130403dbd36SAlek Du 			if (!ehci->need_io_watchdog)
131403dbd36SAlek Du 				return;
132bc29847eSAlan Stern 			t = EHCI_IO_JIFFIES;
133bc29847eSAlan Stern 			break;
134bc29847eSAlan Stern 		case TIMER_ASYNC_OFF:
135bc29847eSAlan Stern 			t = EHCI_ASYNC_JIFFIES;
136bc29847eSAlan Stern 			break;
137bc29847eSAlan Stern 		/* case TIMER_ASYNC_SHRINK: */
138bc29847eSAlan Stern 		default:
139bc29847eSAlan Stern 			/* add a jiffie since we synch against the
140bc29847eSAlan Stern 			 * 8 KHz uframe counter.
141bc29847eSAlan Stern 			 */
142bc29847eSAlan Stern 			t = DIV_ROUND_UP(EHCI_SHRINK_FRAMES * HZ, 1000) + 1;
143bc29847eSAlan Stern 			break;
144bc29847eSAlan Stern 		}
145bc29847eSAlan Stern 		mod_timer(&ehci->watchdog, t + jiffies);
146bc29847eSAlan Stern 	}
147bc29847eSAlan Stern }
148bc29847eSAlan Stern 
149bc29847eSAlan Stern /*-------------------------------------------------------------------------*/
150bc29847eSAlan Stern 
1511da177e4SLinus Torvalds /*
1521da177e4SLinus Torvalds  * handshake - spin reading hc until handshake completes or fails
1531da177e4SLinus Torvalds  * @ptr: address of hc register to be read
1541da177e4SLinus Torvalds  * @mask: bits to look at in result of read
1551da177e4SLinus Torvalds  * @done: value of those bits when handshake succeeds
1561da177e4SLinus Torvalds  * @usec: timeout in microseconds
1571da177e4SLinus Torvalds  *
1581da177e4SLinus Torvalds  * Returns negative errno, or zero on success
1591da177e4SLinus Torvalds  *
1601da177e4SLinus Torvalds  * Success happens when the "mask" bits have the specified value (hardware
1611da177e4SLinus Torvalds  * handshake done).  There are two failure modes:  "usec" have passed (major
1621da177e4SLinus Torvalds  * hardware flakeout), or the register reads as all-ones (hardware removed).
1631da177e4SLinus Torvalds  *
1641da177e4SLinus Torvalds  * That last failure should_only happen in cases like physical cardbus eject
1651da177e4SLinus Torvalds  * before driver shutdown. But it also seems to be caused by bugs in cardbus
1661da177e4SLinus Torvalds  * bridge shutdown:  shutting down the bridge before the devices using it.
1671da177e4SLinus Torvalds  */
168083522d7SBenjamin Herrenschmidt static int handshake (struct ehci_hcd *ehci, void __iomem *ptr,
169083522d7SBenjamin Herrenschmidt 		      u32 mask, u32 done, int usec)
1701da177e4SLinus Torvalds {
1711da177e4SLinus Torvalds 	u32	result;
1721da177e4SLinus Torvalds 
1731da177e4SLinus Torvalds 	do {
174083522d7SBenjamin Herrenschmidt 		result = ehci_readl(ehci, ptr);
1751da177e4SLinus Torvalds 		if (result == ~(u32)0)		/* card removed */
1761da177e4SLinus Torvalds 			return -ENODEV;
1771da177e4SLinus Torvalds 		result &= mask;
1781da177e4SLinus Torvalds 		if (result == done)
1791da177e4SLinus Torvalds 			return 0;
1801da177e4SLinus Torvalds 		udelay (1);
1811da177e4SLinus Torvalds 		usec--;
1821da177e4SLinus Torvalds 	} while (usec > 0);
1831da177e4SLinus Torvalds 	return -ETIMEDOUT;
1841da177e4SLinus Torvalds }
1851da177e4SLinus Torvalds 
1861da177e4SLinus Torvalds /* force HC to halt state from unknown (EHCI spec section 2.3) */
1871da177e4SLinus Torvalds static int ehci_halt (struct ehci_hcd *ehci)
1881da177e4SLinus Torvalds {
189083522d7SBenjamin Herrenschmidt 	u32	temp = ehci_readl(ehci, &ehci->regs->status);
1901da177e4SLinus Torvalds 
19172f30b6fSDavid Brownell 	/* disable any irqs left enabled by previous code */
192083522d7SBenjamin Herrenschmidt 	ehci_writel(ehci, 0, &ehci->regs->intr_enable);
19372f30b6fSDavid Brownell 
1941da177e4SLinus Torvalds 	if ((temp & STS_HALT) != 0)
1951da177e4SLinus Torvalds 		return 0;
1961da177e4SLinus Torvalds 
197083522d7SBenjamin Herrenschmidt 	temp = ehci_readl(ehci, &ehci->regs->command);
1981da177e4SLinus Torvalds 	temp &= ~CMD_RUN;
199083522d7SBenjamin Herrenschmidt 	ehci_writel(ehci, temp, &ehci->regs->command);
200083522d7SBenjamin Herrenschmidt 	return handshake (ehci, &ehci->regs->status,
201083522d7SBenjamin Herrenschmidt 			  STS_HALT, STS_HALT, 16 * 125);
2021da177e4SLinus Torvalds }
2031da177e4SLinus Torvalds 
2040bcfeb3eSDavid Brownell static int handshake_on_error_set_halt(struct ehci_hcd *ehci, void __iomem *ptr,
2050bcfeb3eSDavid Brownell 				       u32 mask, u32 done, int usec)
2060bcfeb3eSDavid Brownell {
2070bcfeb3eSDavid Brownell 	int error;
2080bcfeb3eSDavid Brownell 
2090bcfeb3eSDavid Brownell 	error = handshake(ehci, ptr, mask, done, usec);
2100bcfeb3eSDavid Brownell 	if (error) {
2110bcfeb3eSDavid Brownell 		ehci_halt(ehci);
2120bcfeb3eSDavid Brownell 		ehci_to_hcd(ehci)->state = HC_STATE_HALT;
21365cb76baSOzan Çağlayan 		ehci_err(ehci, "force halt; handshake %p %08x %08x -> %d\n",
2140bcfeb3eSDavid Brownell 			ptr, mask, done, error);
2150bcfeb3eSDavid Brownell 	}
2160bcfeb3eSDavid Brownell 
2170bcfeb3eSDavid Brownell 	return error;
2180bcfeb3eSDavid Brownell }
2190bcfeb3eSDavid Brownell 
2201da177e4SLinus Torvalds /* put TDI/ARC silicon into EHCI mode */
2211da177e4SLinus Torvalds static void tdi_reset (struct ehci_hcd *ehci)
2221da177e4SLinus Torvalds {
2231da177e4SLinus Torvalds 	u32 __iomem	*reg_ptr;
2241da177e4SLinus Torvalds 	u32		tmp;
2251da177e4SLinus Torvalds 
226d23a1377SVladimir Barinov 	reg_ptr = (u32 __iomem *)(((u8 __iomem *)ehci->regs) + USBMODE);
227083522d7SBenjamin Herrenschmidt 	tmp = ehci_readl(ehci, reg_ptr);
228d23a1377SVladimir Barinov 	tmp |= USBMODE_CM_HC;
229d23a1377SVladimir Barinov 	/* The default byte access to MMR space is LE after
230d23a1377SVladimir Barinov 	 * controller reset. Set the required endian mode
231d23a1377SVladimir Barinov 	 * for transfer buffers to match the host microprocessor
232d23a1377SVladimir Barinov 	 */
233d23a1377SVladimir Barinov 	if (ehci_big_endian_mmio(ehci))
234d23a1377SVladimir Barinov 		tmp |= USBMODE_BE;
235083522d7SBenjamin Herrenschmidt 	ehci_writel(ehci, tmp, reg_ptr);
2361da177e4SLinus Torvalds }
2371da177e4SLinus Torvalds 
2381da177e4SLinus Torvalds /* reset a non-running (STS_HALT == 1) controller */
2391da177e4SLinus Torvalds static int ehci_reset (struct ehci_hcd *ehci)
2401da177e4SLinus Torvalds {
2411da177e4SLinus Torvalds 	int	retval;
242083522d7SBenjamin Herrenschmidt 	u32	command = ehci_readl(ehci, &ehci->regs->command);
2431da177e4SLinus Torvalds 
2448d053c79SJason Wessel 	/* If the EHCI debug controller is active, special care must be
2458d053c79SJason Wessel 	 * taken before and after a host controller reset */
2468d053c79SJason Wessel 	if (ehci->debug && !dbgp_reset_prep())
2478d053c79SJason Wessel 		ehci->debug = NULL;
2488d053c79SJason Wessel 
2491da177e4SLinus Torvalds 	command |= CMD_RESET;
2501da177e4SLinus Torvalds 	dbg_cmd (ehci, "reset", command);
251083522d7SBenjamin Herrenschmidt 	ehci_writel(ehci, command, &ehci->regs->command);
2521da177e4SLinus Torvalds 	ehci_to_hcd(ehci)->state = HC_STATE_HALT;
2531da177e4SLinus Torvalds 	ehci->next_statechange = jiffies;
254083522d7SBenjamin Herrenschmidt 	retval = handshake (ehci, &ehci->regs->command,
255083522d7SBenjamin Herrenschmidt 			    CMD_RESET, 0, 250 * 1000);
2561da177e4SLinus Torvalds 
257331ac6b2SAlek Du 	if (ehci->has_hostpc) {
258331ac6b2SAlek Du 		ehci_writel(ehci, USBMODE_EX_HC | USBMODE_EX_VBPS,
259331ac6b2SAlek Du 			(u32 __iomem *)(((u8 *)ehci->regs) + USBMODE_EX));
260331ac6b2SAlek Du 		ehci_writel(ehci, TXFIFO_DEFAULT,
261331ac6b2SAlek Du 			(u32 __iomem *)(((u8 *)ehci->regs) + TXFILLTUNING));
262331ac6b2SAlek Du 	}
2631da177e4SLinus Torvalds 	if (retval)
2641da177e4SLinus Torvalds 		return retval;
2651da177e4SLinus Torvalds 
2661da177e4SLinus Torvalds 	if (ehci_is_TDI(ehci))
2671da177e4SLinus Torvalds 		tdi_reset (ehci);
2681da177e4SLinus Torvalds 
2698d053c79SJason Wessel 	if (ehci->debug)
2708d053c79SJason Wessel 		dbgp_external_startup();
2718d053c79SJason Wessel 
2721da177e4SLinus Torvalds 	return retval;
2731da177e4SLinus Torvalds }
2741da177e4SLinus Torvalds 
2751da177e4SLinus Torvalds /* idle the controller (from running) */
2761da177e4SLinus Torvalds static void ehci_quiesce (struct ehci_hcd *ehci)
2771da177e4SLinus Torvalds {
2781da177e4SLinus Torvalds 	u32	temp;
2791da177e4SLinus Torvalds 
2801da177e4SLinus Torvalds #ifdef DEBUG
2811da177e4SLinus Torvalds 	if (!HC_IS_RUNNING (ehci_to_hcd(ehci)->state))
2821da177e4SLinus Torvalds 		BUG ();
2831da177e4SLinus Torvalds #endif
2841da177e4SLinus Torvalds 
2851da177e4SLinus Torvalds 	/* wait for any schedule enables/disables to take effect */
286083522d7SBenjamin Herrenschmidt 	temp = ehci_readl(ehci, &ehci->regs->command) << 10;
2871da177e4SLinus Torvalds 	temp &= STS_ASS | STS_PSS;
288c765d4caSKarsten Wiese 	if (handshake_on_error_set_halt(ehci, &ehci->regs->status,
289c765d4caSKarsten Wiese 					STS_ASS | STS_PSS, temp, 16 * 125))
2901da177e4SLinus Torvalds 		return;
2911da177e4SLinus Torvalds 
2921da177e4SLinus Torvalds 	/* then disable anything that's still active */
293083522d7SBenjamin Herrenschmidt 	temp = ehci_readl(ehci, &ehci->regs->command);
2941da177e4SLinus Torvalds 	temp &= ~(CMD_ASE | CMD_IAAD | CMD_PSE);
295083522d7SBenjamin Herrenschmidt 	ehci_writel(ehci, temp, &ehci->regs->command);
2961da177e4SLinus Torvalds 
2971da177e4SLinus Torvalds 	/* hardware can take 16 microframes to turn off ... */
298c765d4caSKarsten Wiese 	handshake_on_error_set_halt(ehci, &ehci->regs->status,
299c765d4caSKarsten Wiese 				    STS_ASS | STS_PSS, 0, 16 * 125);
3001da177e4SLinus Torvalds }
3011da177e4SLinus Torvalds 
3021da177e4SLinus Torvalds /*-------------------------------------------------------------------------*/
3031da177e4SLinus Torvalds 
30407d29b63SAlan Stern static void end_unlink_async(struct ehci_hcd *ehci);
3057d12e780SDavid Howells static void ehci_work(struct ehci_hcd *ehci);
3061da177e4SLinus Torvalds 
3071da177e4SLinus Torvalds #include "ehci-hub.c"
3081da177e4SLinus Torvalds #include "ehci-mem.c"
3091da177e4SLinus Torvalds #include "ehci-q.c"
3101da177e4SLinus Torvalds #include "ehci-sched.c"
3111da177e4SLinus Torvalds 
3121da177e4SLinus Torvalds /*-------------------------------------------------------------------------*/
3131da177e4SLinus Torvalds 
31407d29b63SAlan Stern static void ehci_iaa_watchdog(unsigned long param)
31507d29b63SAlan Stern {
31607d29b63SAlan Stern 	struct ehci_hcd		*ehci = (struct ehci_hcd *) param;
31707d29b63SAlan Stern 	unsigned long		flags;
31807d29b63SAlan Stern 
31907d29b63SAlan Stern 	spin_lock_irqsave (&ehci->lock, flags);
32007d29b63SAlan Stern 
321e82cc128SDavid Brownell 	/* Lost IAA irqs wedge things badly; seen first with a vt8235.
322e82cc128SDavid Brownell 	 * So we need this watchdog, but must protect it against both
323e82cc128SDavid Brownell 	 * (a) SMP races against real IAA firing and retriggering, and
324e82cc128SDavid Brownell 	 * (b) clean HC shutdown, when IAA watchdog was pending.
325e82cc128SDavid Brownell 	 */
326e82cc128SDavid Brownell 	if (ehci->reclaim
327e82cc128SDavid Brownell 			&& !timer_pending(&ehci->iaa_watchdog)
328e82cc128SDavid Brownell 			&& HC_IS_RUNNING(ehci_to_hcd(ehci)->state)) {
329e82cc128SDavid Brownell 		u32 cmd, status;
330e82cc128SDavid Brownell 
331e82cc128SDavid Brownell 		/* If we get here, IAA is *REALLY* late.  It's barely
332e82cc128SDavid Brownell 		 * conceivable that the system is so busy that CMD_IAAD
333e82cc128SDavid Brownell 		 * is still legitimately set, so let's be sure it's
334e82cc128SDavid Brownell 		 * clear before we read STS_IAA.  (The HC should clear
335e82cc128SDavid Brownell 		 * CMD_IAAD when it sets STS_IAA.)
336e82cc128SDavid Brownell 		 */
33707d29b63SAlan Stern 		cmd = ehci_readl(ehci, &ehci->regs->command);
338e82cc128SDavid Brownell 		if (cmd & CMD_IAAD)
339e82cc128SDavid Brownell 			ehci_writel(ehci, cmd & ~CMD_IAAD,
340e82cc128SDavid Brownell 					&ehci->regs->command);
34107d29b63SAlan Stern 
342e82cc128SDavid Brownell 		/* If IAA is set here it either legitimately triggered
343e82cc128SDavid Brownell 		 * before we cleared IAAD above (but _way_ late, so we'll
344e82cc128SDavid Brownell 		 * still count it as lost) ... or a silicon erratum:
345e82cc128SDavid Brownell 		 * - VIA seems to set IAA without triggering the IRQ;
346e82cc128SDavid Brownell 		 * - IAAD potentially cleared without setting IAA.
347e82cc128SDavid Brownell 		 */
348e82cc128SDavid Brownell 		status = ehci_readl(ehci, &ehci->regs->status);
349e82cc128SDavid Brownell 		if ((status & STS_IAA) || !(cmd & CMD_IAAD)) {
35007d29b63SAlan Stern 			COUNT (ehci->stats.lost_iaa);
35107d29b63SAlan Stern 			ehci_writel(ehci, STS_IAA, &ehci->regs->status);
35207d29b63SAlan Stern 		}
353e82cc128SDavid Brownell 
354e82cc128SDavid Brownell 		ehci_vdbg(ehci, "IAA watchdog: status %x cmd %x\n",
355e82cc128SDavid Brownell 				status, cmd);
35607d29b63SAlan Stern 		end_unlink_async(ehci);
35707d29b63SAlan Stern 	}
35807d29b63SAlan Stern 
35907d29b63SAlan Stern 	spin_unlock_irqrestore(&ehci->lock, flags);
36007d29b63SAlan Stern }
36107d29b63SAlan Stern 
3621da177e4SLinus Torvalds static void ehci_watchdog(unsigned long param)
3631da177e4SLinus Torvalds {
3641da177e4SLinus Torvalds 	struct ehci_hcd		*ehci = (struct ehci_hcd *) param;
3651da177e4SLinus Torvalds 	unsigned long		flags;
3661da177e4SLinus Torvalds 
3671da177e4SLinus Torvalds 	spin_lock_irqsave(&ehci->lock, flags);
3681da177e4SLinus Torvalds 
3691da177e4SLinus Torvalds 	/* stop async processing after it's idled a bit */
3701da177e4SLinus Torvalds 	if (test_bit (TIMER_ASYNC_OFF, &ehci->actions))
3711da177e4SLinus Torvalds 		start_unlink_async (ehci, ehci->async);
3721da177e4SLinus Torvalds 
3731da177e4SLinus Torvalds 	/* ehci could run by timer, without IRQs ... */
3747d12e780SDavid Howells 	ehci_work (ehci);
3751da177e4SLinus Torvalds 
3761da177e4SLinus Torvalds 	spin_unlock_irqrestore (&ehci->lock, flags);
3771da177e4SLinus Torvalds }
3781da177e4SLinus Torvalds 
3798903795aSAlan Stern /* On some systems, leaving remote wakeup enabled prevents system shutdown.
3808903795aSAlan Stern  * The firmware seems to think that powering off is a wakeup event!
3818903795aSAlan Stern  * This routine turns off remote wakeup and everything else, on all ports.
3828903795aSAlan Stern  */
3838903795aSAlan Stern static void ehci_turn_off_all_ports(struct ehci_hcd *ehci)
3848903795aSAlan Stern {
3858903795aSAlan Stern 	int	port = HCS_N_PORTS(ehci->hcs_params);
3868903795aSAlan Stern 
3878903795aSAlan Stern 	while (port--)
3888903795aSAlan Stern 		ehci_writel(ehci, PORT_RWC_BITS,
3898903795aSAlan Stern 				&ehci->regs->port_status[port]);
3908903795aSAlan Stern }
3918903795aSAlan Stern 
39221da84a8SSarah Sharp /*
39321da84a8SSarah Sharp  * Halt HC, turn off all ports, and let the BIOS use the companion controllers.
39421da84a8SSarah Sharp  * Should be called with ehci->lock held.
39572f30b6fSDavid Brownell  */
39621da84a8SSarah Sharp static void ehci_silence_controller(struct ehci_hcd *ehci)
3971da177e4SLinus Torvalds {
39821da84a8SSarah Sharp 	ehci_halt(ehci);
3998903795aSAlan Stern 	ehci_turn_off_all_ports(ehci);
4001da177e4SLinus Torvalds 
4011da177e4SLinus Torvalds 	/* make BIOS/etc use companion controller during reboot */
402083522d7SBenjamin Herrenschmidt 	ehci_writel(ehci, 0, &ehci->regs->configured_flag);
4038903795aSAlan Stern 
4048903795aSAlan Stern 	/* unblock posted writes */
4058903795aSAlan Stern 	ehci_readl(ehci, &ehci->regs->configured_flag);
4061da177e4SLinus Torvalds }
4071da177e4SLinus Torvalds 
40821da84a8SSarah Sharp /* ehci_shutdown kick in for silicon on any bus (not just pci, etc).
40921da84a8SSarah Sharp  * This forcibly disables dma and IRQs, helping kexec and other cases
41021da84a8SSarah Sharp  * where the next system software may expect clean state.
41121da84a8SSarah Sharp  */
41221da84a8SSarah Sharp static void ehci_shutdown(struct usb_hcd *hcd)
41321da84a8SSarah Sharp {
41421da84a8SSarah Sharp 	struct ehci_hcd	*ehci = hcd_to_ehci(hcd);
41521da84a8SSarah Sharp 
41621da84a8SSarah Sharp 	del_timer_sync(&ehci->watchdog);
41721da84a8SSarah Sharp 	del_timer_sync(&ehci->iaa_watchdog);
41821da84a8SSarah Sharp 
41921da84a8SSarah Sharp 	spin_lock_irq(&ehci->lock);
42021da84a8SSarah Sharp 	ehci_silence_controller(ehci);
42121da84a8SSarah Sharp 	spin_unlock_irq(&ehci->lock);
42221da84a8SSarah Sharp }
42321da84a8SSarah Sharp 
42456c1e26dSDavid Brownell static void ehci_port_power (struct ehci_hcd *ehci, int is_on)
42556c1e26dSDavid Brownell {
42656c1e26dSDavid Brownell 	unsigned port;
42756c1e26dSDavid Brownell 
42856c1e26dSDavid Brownell 	if (!HCS_PPC (ehci->hcs_params))
42956c1e26dSDavid Brownell 		return;
43056c1e26dSDavid Brownell 
43156c1e26dSDavid Brownell 	ehci_dbg (ehci, "...power%s ports...\n", is_on ? "up" : "down");
43256c1e26dSDavid Brownell 	for (port = HCS_N_PORTS (ehci->hcs_params); port > 0; )
43356c1e26dSDavid Brownell 		(void) ehci_hub_control(ehci_to_hcd(ehci),
43456c1e26dSDavid Brownell 				is_on ? SetPortFeature : ClearPortFeature,
43556c1e26dSDavid Brownell 				USB_PORT_FEAT_POWER,
43656c1e26dSDavid Brownell 				port--, NULL, 0);
437383975d7SAlan Stern 	/* Flush those writes */
438383975d7SAlan Stern 	ehci_readl(ehci, &ehci->regs->command);
43956c1e26dSDavid Brownell 	msleep(20);
44056c1e26dSDavid Brownell }
44156c1e26dSDavid Brownell 
4427ff71d6aSMatt Porter /*-------------------------------------------------------------------------*/
4431da177e4SLinus Torvalds 
4447ff71d6aSMatt Porter /*
4457ff71d6aSMatt Porter  * ehci_work is called from some interrupts, timers, and so on.
4467ff71d6aSMatt Porter  * it calls driver completion functions, after dropping ehci->lock.
4477ff71d6aSMatt Porter  */
4487d12e780SDavid Howells static void ehci_work (struct ehci_hcd *ehci)
4497ff71d6aSMatt Porter {
4507ff71d6aSMatt Porter 	timer_action_done (ehci, TIMER_IO_WATCHDOG);
4511da177e4SLinus Torvalds 
4527ff71d6aSMatt Porter 	/* another CPU may drop ehci->lock during a schedule scan while
4537ff71d6aSMatt Porter 	 * it reports urb completions.  this flag guards against bogus
4547ff71d6aSMatt Porter 	 * attempts at re-entrant schedule scanning.
4557ff71d6aSMatt Porter 	 */
4567ff71d6aSMatt Porter 	if (ehci->scanning)
4577ff71d6aSMatt Porter 		return;
4587ff71d6aSMatt Porter 	ehci->scanning = 1;
4597d12e780SDavid Howells 	scan_async (ehci);
4607ff71d6aSMatt Porter 	if (ehci->next_uframe != -1)
4617d12e780SDavid Howells 		scan_periodic (ehci);
4627ff71d6aSMatt Porter 	ehci->scanning = 0;
4637ff71d6aSMatt Porter 
4647ff71d6aSMatt Porter 	/* the IO watchdog guards against hardware or driver bugs that
4657ff71d6aSMatt Porter 	 * misplace IRQs, and should let us run completely without IRQs.
4667ff71d6aSMatt Porter 	 * such lossage has been observed on both VT6202 and VT8235.
4677ff71d6aSMatt Porter 	 */
4687ff71d6aSMatt Porter 	if (HC_IS_RUNNING (ehci_to_hcd(ehci)->state) &&
4697ff71d6aSMatt Porter 			(ehci->async->qh_next.ptr != NULL ||
4707ff71d6aSMatt Porter 			 ehci->periodic_sched != 0))
4717ff71d6aSMatt Porter 		timer_action (ehci, TIMER_IO_WATCHDOG);
4727ff71d6aSMatt Porter }
4737ff71d6aSMatt Porter 
47421da84a8SSarah Sharp /*
47521da84a8SSarah Sharp  * Called when the ehci_hcd module is removed.
47621da84a8SSarah Sharp  */
4777ff71d6aSMatt Porter static void ehci_stop (struct usb_hcd *hcd)
4781da177e4SLinus Torvalds {
4791da177e4SLinus Torvalds 	struct ehci_hcd		*ehci = hcd_to_ehci (hcd);
4801da177e4SLinus Torvalds 
4817ff71d6aSMatt Porter 	ehci_dbg (ehci, "stop\n");
4821da177e4SLinus Torvalds 
4837ff71d6aSMatt Porter 	/* no more interrupts ... */
4847ff71d6aSMatt Porter 	del_timer_sync (&ehci->watchdog);
48507d29b63SAlan Stern 	del_timer_sync(&ehci->iaa_watchdog);
4861da177e4SLinus Torvalds 
4877ff71d6aSMatt Porter 	spin_lock_irq(&ehci->lock);
4887ff71d6aSMatt Porter 	if (HC_IS_RUNNING (hcd->state))
4897ff71d6aSMatt Porter 		ehci_quiesce (ehci);
4901da177e4SLinus Torvalds 
49121da84a8SSarah Sharp 	ehci_silence_controller(ehci);
4927ff71d6aSMatt Porter 	ehci_reset (ehci);
4937ff71d6aSMatt Porter 	spin_unlock_irq(&ehci->lock);
4947ff71d6aSMatt Porter 
49557e06c11SAlan Stern 	remove_companion_file(ehci);
4967ff71d6aSMatt Porter 	remove_debug_files (ehci);
4977ff71d6aSMatt Porter 
4987ff71d6aSMatt Porter 	/* root hub is shut down separately (first, when possible) */
4997ff71d6aSMatt Porter 	spin_lock_irq (&ehci->lock);
5007ff71d6aSMatt Porter 	if (ehci->async)
5017d12e780SDavid Howells 		ehci_work (ehci);
5027ff71d6aSMatt Porter 	spin_unlock_irq (&ehci->lock);
5037ff71d6aSMatt Porter 	ehci_mem_cleanup (ehci);
5047ff71d6aSMatt Porter 
5057ff71d6aSMatt Porter #ifdef	EHCI_STATS
5067ff71d6aSMatt Porter 	ehci_dbg (ehci, "irq normal %ld err %ld reclaim %ld (lost %ld)\n",
5077ff71d6aSMatt Porter 		ehci->stats.normal, ehci->stats.error, ehci->stats.reclaim,
5087ff71d6aSMatt Porter 		ehci->stats.lost_iaa);
5097ff71d6aSMatt Porter 	ehci_dbg (ehci, "complete %ld unlink %ld\n",
5107ff71d6aSMatt Porter 		ehci->stats.complete, ehci->stats.unlink);
5111da177e4SLinus Torvalds #endif
5127ff71d6aSMatt Porter 
513083522d7SBenjamin Herrenschmidt 	dbg_status (ehci, "ehci_stop completed",
514083522d7SBenjamin Herrenschmidt 		    ehci_readl(ehci, &ehci->regs->status));
5151da177e4SLinus Torvalds }
5161da177e4SLinus Torvalds 
51718807521SDavid Brownell /* one-time init, only for memory state */
51818807521SDavid Brownell static int ehci_init(struct usb_hcd *hcd)
5191da177e4SLinus Torvalds {
5201da177e4SLinus Torvalds 	struct ehci_hcd		*ehci = hcd_to_ehci(hcd);
5211da177e4SLinus Torvalds 	u32			temp;
5221da177e4SLinus Torvalds 	int			retval;
5231da177e4SLinus Torvalds 	u32			hcc_params;
5243807e26dSAlek Du 	struct ehci_qh_hw	*hw;
5251da177e4SLinus Torvalds 
52618807521SDavid Brownell 	spin_lock_init(&ehci->lock);
52718807521SDavid Brownell 
528403dbd36SAlek Du 	/*
529403dbd36SAlek Du 	 * keep io watchdog by default, those good HCDs could turn off it later
530403dbd36SAlek Du 	 */
531403dbd36SAlek Du 	ehci->need_io_watchdog = 1;
5321da177e4SLinus Torvalds 	init_timer(&ehci->watchdog);
5331da177e4SLinus Torvalds 	ehci->watchdog.function = ehci_watchdog;
5341da177e4SLinus Torvalds 	ehci->watchdog.data = (unsigned long) ehci;
5351da177e4SLinus Torvalds 
53607d29b63SAlan Stern 	init_timer(&ehci->iaa_watchdog);
53707d29b63SAlan Stern 	ehci->iaa_watchdog.function = ehci_iaa_watchdog;
53807d29b63SAlan Stern 	ehci->iaa_watchdog.data = (unsigned long) ehci;
53907d29b63SAlan Stern 
5401da177e4SLinus Torvalds 	/*
5411da177e4SLinus Torvalds 	 * hw default: 1K periodic list heads, one per frame.
5421da177e4SLinus Torvalds 	 * periodic_size can shrink by USBCMD update if hcc_params allows.
5431da177e4SLinus Torvalds 	 */
5441da177e4SLinus Torvalds 	ehci->periodic_size = DEFAULT_I_TDPS;
5459aa09d2fSKarsten Wiese 	INIT_LIST_HEAD(&ehci->cached_itd_list);
546*0e5f231bSAlan Stern 	INIT_LIST_HEAD(&ehci->cached_sitd_list);
54718807521SDavid Brownell 	if ((retval = ehci_mem_init(ehci, GFP_KERNEL)) < 0)
5481da177e4SLinus Torvalds 		return retval;
5491da177e4SLinus Torvalds 
5501da177e4SLinus Torvalds 	/* controllers may cache some of the periodic schedule ... */
551083522d7SBenjamin Herrenschmidt 	hcc_params = ehci_readl(ehci, &ehci->caps->hcc_params);
5521da177e4SLinus Torvalds 	if (HCC_ISOC_CACHE(hcc_params))		// full frame cache
553dccd574cSSarah Sharp 		ehci->i_thresh = 2 + 8;
5541da177e4SLinus Torvalds 	else					// N microframes cached
5551da177e4SLinus Torvalds 		ehci->i_thresh = 2 + HCC_ISOC_THRES(hcc_params);
5561da177e4SLinus Torvalds 
5571da177e4SLinus Torvalds 	ehci->reclaim = NULL;
5581da177e4SLinus Torvalds 	ehci->next_uframe = -1;
5599aa09d2fSKarsten Wiese 	ehci->clock_frame = -1;
5601da177e4SLinus Torvalds 
5611da177e4SLinus Torvalds 	/*
5621da177e4SLinus Torvalds 	 * dedicate a qh for the async ring head, since we couldn't unlink
5631da177e4SLinus Torvalds 	 * a 'real' qh without stopping the async schedule [4.8].  use it
5641da177e4SLinus Torvalds 	 * as the 'reclamation list head' too.
5651da177e4SLinus Torvalds 	 * its dummy is used in hw_alt_next of many tds, to prevent the qh
5661da177e4SLinus Torvalds 	 * from automatically advancing to the next td after short reads.
5671da177e4SLinus Torvalds 	 */
5681da177e4SLinus Torvalds 	ehci->async->qh_next.qh = NULL;
5693807e26dSAlek Du 	hw = ehci->async->hw;
5703807e26dSAlek Du 	hw->hw_next = QH_NEXT(ehci, ehci->async->qh_dma);
5713807e26dSAlek Du 	hw->hw_info1 = cpu_to_hc32(ehci, QH_HEAD);
5723807e26dSAlek Du 	hw->hw_token = cpu_to_hc32(ehci, QTD_STS_HALT);
5733807e26dSAlek Du 	hw->hw_qtd_next = EHCI_LIST_END(ehci);
5741da177e4SLinus Torvalds 	ehci->async->qh_state = QH_STATE_LINKED;
5753807e26dSAlek Du 	hw->hw_alt_next = QTD_NEXT(ehci, ehci->async->dummy->qtd_dma);
5761da177e4SLinus Torvalds 
5771da177e4SLinus Torvalds 	/* clear interrupt enables, set irq latency */
5781da177e4SLinus Torvalds 	if (log2_irq_thresh < 0 || log2_irq_thresh > 6)
5791da177e4SLinus Torvalds 		log2_irq_thresh = 0;
5801da177e4SLinus Torvalds 	temp = 1 << (16 + log2_irq_thresh);
5811da177e4SLinus Torvalds 	if (HCC_CANPARK(hcc_params)) {
5821da177e4SLinus Torvalds 		/* HW default park == 3, on hardware that supports it (like
5831da177e4SLinus Torvalds 		 * NVidia and ALI silicon), maximizes throughput on the async
5841da177e4SLinus Torvalds 		 * schedule by avoiding QH fetches between transfers.
5851da177e4SLinus Torvalds 		 *
5861da177e4SLinus Torvalds 		 * With fast usb storage devices and NForce2, "park" seems to
5871da177e4SLinus Torvalds 		 * make problems:  throughput reduction (!), data errors...
5881da177e4SLinus Torvalds 		 */
5891da177e4SLinus Torvalds 		if (park) {
5901da177e4SLinus Torvalds 			park = min(park, (unsigned) 3);
5911da177e4SLinus Torvalds 			temp |= CMD_PARK;
5921da177e4SLinus Torvalds 			temp |= park << 8;
5931da177e4SLinus Torvalds 		}
59418807521SDavid Brownell 		ehci_dbg(ehci, "park %d\n", park);
5951da177e4SLinus Torvalds 	}
5961da177e4SLinus Torvalds 	if (HCC_PGM_FRAMELISTLEN(hcc_params)) {
5971da177e4SLinus Torvalds 		/* periodic schedule size can be smaller than default */
5981da177e4SLinus Torvalds 		temp &= ~(3 << 2);
5991da177e4SLinus Torvalds 		temp |= (EHCI_TUNE_FLS << 2);
6001da177e4SLinus Torvalds 		switch (EHCI_TUNE_FLS) {
6011da177e4SLinus Torvalds 		case 0: ehci->periodic_size = 1024; break;
6021da177e4SLinus Torvalds 		case 1: ehci->periodic_size = 512; break;
6031da177e4SLinus Torvalds 		case 2: ehci->periodic_size = 256; break;
6041da177e4SLinus Torvalds 		default:	BUG();
6051da177e4SLinus Torvalds 		}
6061da177e4SLinus Torvalds 	}
60718807521SDavid Brownell 	ehci->command = temp;
60818807521SDavid Brownell 
60940f8db8fSAlan Stern 	/* Accept arbitrarily long scatter-gather lists */
61040f8db8fSAlan Stern 	hcd->self.sg_tablesize = ~0;
61118807521SDavid Brownell 	return 0;
61218807521SDavid Brownell }
61318807521SDavid Brownell 
61418807521SDavid Brownell /* start HC running; it's halted, ehci_init() has been run (once) */
61518807521SDavid Brownell static int ehci_run (struct usb_hcd *hcd)
61618807521SDavid Brownell {
61718807521SDavid Brownell 	struct ehci_hcd		*ehci = hcd_to_ehci (hcd);
61818807521SDavid Brownell 	int			retval;
61918807521SDavid Brownell 	u32			temp;
62018807521SDavid Brownell 	u32			hcc_params;
62118807521SDavid Brownell 
6221d619f12SMarcelo Tosatti 	hcd->uses_new_polling = 1;
6231d619f12SMarcelo Tosatti 	hcd->poll_rh = 0;
6241d619f12SMarcelo Tosatti 
62518807521SDavid Brownell 	/* EHCI spec section 4.1 */
62618807521SDavid Brownell 	if ((retval = ehci_reset(ehci)) != 0) {
62718807521SDavid Brownell 		ehci_mem_cleanup(ehci);
62818807521SDavid Brownell 		return retval;
62918807521SDavid Brownell 	}
630083522d7SBenjamin Herrenschmidt 	ehci_writel(ehci, ehci->periodic_dma, &ehci->regs->frame_list);
631083522d7SBenjamin Herrenschmidt 	ehci_writel(ehci, (u32)ehci->async->qh_dma, &ehci->regs->async_next);
63218807521SDavid Brownell 
63318807521SDavid Brownell 	/*
63418807521SDavid Brownell 	 * hcc_params controls whether ehci->regs->segment must (!!!)
63518807521SDavid Brownell 	 * be used; it constrains QH/ITD/SITD and QTD locations.
63618807521SDavid Brownell 	 * pci_pool consistent memory always uses segment zero.
63718807521SDavid Brownell 	 * streaming mappings for I/O buffers, like pci_map_single(),
63818807521SDavid Brownell 	 * can return segments above 4GB, if the device allows.
63918807521SDavid Brownell 	 *
64018807521SDavid Brownell 	 * NOTE:  the dma mask is visible through dma_supported(), so
64118807521SDavid Brownell 	 * drivers can pass this info along ... like NETIF_F_HIGHDMA,
64218807521SDavid Brownell 	 * Scsi_Host.highmem_io, and so forth.  It's readonly to all
64318807521SDavid Brownell 	 * host side drivers though.
64418807521SDavid Brownell 	 */
645083522d7SBenjamin Herrenschmidt 	hcc_params = ehci_readl(ehci, &ehci->caps->hcc_params);
64618807521SDavid Brownell 	if (HCC_64BIT_ADDR(hcc_params)) {
647083522d7SBenjamin Herrenschmidt 		ehci_writel(ehci, 0, &ehci->regs->segment);
64818807521SDavid Brownell #if 0
64918807521SDavid Brownell // this is deeply broken on almost all architectures
6506a35528aSYang Hongyang 		if (!dma_set_mask(hcd->self.controller, DMA_BIT_MASK(64)))
65118807521SDavid Brownell 			ehci_info(ehci, "enabled 64bit DMA\n");
65218807521SDavid Brownell #endif
65318807521SDavid Brownell 	}
65418807521SDavid Brownell 
65518807521SDavid Brownell 
6561da177e4SLinus Torvalds 	// Philips, Intel, and maybe others need CMD_RUN before the
6571da177e4SLinus Torvalds 	// root hub will detect new devices (why?); NEC doesn't
65818807521SDavid Brownell 	ehci->command &= ~(CMD_LRESET|CMD_IAAD|CMD_PSE|CMD_ASE|CMD_RESET);
65918807521SDavid Brownell 	ehci->command |= CMD_RUN;
660083522d7SBenjamin Herrenschmidt 	ehci_writel(ehci, ehci->command, &ehci->regs->command);
66118807521SDavid Brownell 	dbg_cmd (ehci, "init", ehci->command);
6621da177e4SLinus Torvalds 
6631da177e4SLinus Torvalds 	/*
6641da177e4SLinus Torvalds 	 * Start, enabling full USB 2.0 functionality ... usb 1.1 devices
6651da177e4SLinus Torvalds 	 * are explicitly handed to companion controller(s), so no TT is
6661da177e4SLinus Torvalds 	 * involved with the root hub.  (Except where one is integrated,
6671da177e4SLinus Torvalds 	 * and there's no companion controller unless maybe for USB OTG.)
66832fe0198SAlan Stern 	 *
66932fe0198SAlan Stern 	 * Turning on the CF flag will transfer ownership of all ports
67032fe0198SAlan Stern 	 * from the companions to the EHCI controller.  If any of the
67132fe0198SAlan Stern 	 * companions are in the middle of a port reset at the time, it
67232fe0198SAlan Stern 	 * could cause trouble.  Write-locking ehci_cf_port_reset_rwsem
6731cb52658SDavid Brownell 	 * guarantees that no resets are in progress.  After we set CF,
6741cb52658SDavid Brownell 	 * a short delay lets the hardware catch up; new resets shouldn't
6751cb52658SDavid Brownell 	 * be started before the port switching actions could complete.
6761da177e4SLinus Torvalds 	 */
67732fe0198SAlan Stern 	down_write(&ehci_cf_port_reset_rwsem);
6781da177e4SLinus Torvalds 	hcd->state = HC_STATE_RUNNING;
679083522d7SBenjamin Herrenschmidt 	ehci_writel(ehci, FLAG_CF, &ehci->regs->configured_flag);
680083522d7SBenjamin Herrenschmidt 	ehci_readl(ehci, &ehci->regs->command);	/* unblock posted writes */
6811cb52658SDavid Brownell 	msleep(5);
68232fe0198SAlan Stern 	up_write(&ehci_cf_port_reset_rwsem);
683ee4ecb8aSOliver Neukum 	ehci->last_periodic_enable = ktime_get_real();
6841da177e4SLinus Torvalds 
685083522d7SBenjamin Herrenschmidt 	temp = HC_VERSION(ehci_readl(ehci, &ehci->caps->hc_capbase));
6861da177e4SLinus Torvalds 	ehci_info (ehci,
6872b70f073SAlan Stern 		"USB %x.%x started, EHCI %x.%02x%s\n",
6887ff71d6aSMatt Porter 		((ehci->sbrn & 0xf0)>>4), (ehci->sbrn & 0x0f),
6892b70f073SAlan Stern 		temp >> 8, temp & 0xff,
69093f1a47cSDavid Brownell 		ignore_oc ? ", overcurrent ignored" : "");
6911da177e4SLinus Torvalds 
692083522d7SBenjamin Herrenschmidt 	ehci_writel(ehci, INTR_MASK,
693083522d7SBenjamin Herrenschmidt 		    &ehci->regs->intr_enable); /* Turn On Interrupts */
6941da177e4SLinus Torvalds 
69518807521SDavid Brownell 	/* GRR this is run-once init(), being done every time the HC starts.
69618807521SDavid Brownell 	 * So long as they're part of class devices, we can't do it init()
69718807521SDavid Brownell 	 * since the class device isn't created that early.
69818807521SDavid Brownell 	 */
6991da177e4SLinus Torvalds 	create_debug_files(ehci);
70057e06c11SAlan Stern 	create_companion_file(ehci);
7011da177e4SLinus Torvalds 
7021da177e4SLinus Torvalds 	return 0;
7031da177e4SLinus Torvalds }
7041da177e4SLinus Torvalds 
7051da177e4SLinus Torvalds /*-------------------------------------------------------------------------*/
7061da177e4SLinus Torvalds 
7077d12e780SDavid Howells static irqreturn_t ehci_irq (struct usb_hcd *hcd)
7081da177e4SLinus Torvalds {
7091da177e4SLinus Torvalds 	struct ehci_hcd		*ehci = hcd_to_ehci (hcd);
71067b2e029SAlan Stern 	u32			status, masked_status, pcd_status = 0, cmd;
7111da177e4SLinus Torvalds 	int			bh;
7121da177e4SLinus Torvalds 
7131da177e4SLinus Torvalds 	spin_lock (&ehci->lock);
7141da177e4SLinus Torvalds 
715083522d7SBenjamin Herrenschmidt 	status = ehci_readl(ehci, &ehci->regs->status);
7161da177e4SLinus Torvalds 
7171da177e4SLinus Torvalds 	/* e.g. cardbus physical eject */
7181da177e4SLinus Torvalds 	if (status == ~(u32) 0) {
7191da177e4SLinus Torvalds 		ehci_dbg (ehci, "device removed\n");
7201da177e4SLinus Torvalds 		goto dead;
7211da177e4SLinus Torvalds 	}
7221da177e4SLinus Torvalds 
72367b2e029SAlan Stern 	masked_status = status & INTR_MASK;
72467b2e029SAlan Stern 	if (!masked_status) {		/* irq sharing? */
7251da177e4SLinus Torvalds 		spin_unlock(&ehci->lock);
7261da177e4SLinus Torvalds 		return IRQ_NONE;
7271da177e4SLinus Torvalds 	}
7281da177e4SLinus Torvalds 
7291da177e4SLinus Torvalds 	/* clear (just) interrupts */
73067b2e029SAlan Stern 	ehci_writel(ehci, masked_status, &ehci->regs->status);
731e82cc128SDavid Brownell 	cmd = ehci_readl(ehci, &ehci->regs->command);
7321da177e4SLinus Torvalds 	bh = 0;
7331da177e4SLinus Torvalds 
7349776afc8SDavid Brownell #ifdef	VERBOSE_DEBUG
7351da177e4SLinus Torvalds 	/* unrequested/ignored: Frame List Rollover */
7361da177e4SLinus Torvalds 	dbg_status (ehci, "irq", status);
7371da177e4SLinus Torvalds #endif
7381da177e4SLinus Torvalds 
7391da177e4SLinus Torvalds 	/* INT, ERR, and IAA interrupt rates can be throttled */
7401da177e4SLinus Torvalds 
7411da177e4SLinus Torvalds 	/* normal [4.15.1.2] or error [4.15.1.1] completion */
7421da177e4SLinus Torvalds 	if (likely ((status & (STS_INT|STS_ERR)) != 0)) {
7431da177e4SLinus Torvalds 		if (likely ((status & STS_ERR) == 0))
7441da177e4SLinus Torvalds 			COUNT (ehci->stats.normal);
7451da177e4SLinus Torvalds 		else
7461da177e4SLinus Torvalds 			COUNT (ehci->stats.error);
7471da177e4SLinus Torvalds 		bh = 1;
7481da177e4SLinus Torvalds 	}
7491da177e4SLinus Torvalds 
7501da177e4SLinus Torvalds 	/* complete the unlinking of some qh [4.15.2.3] */
7511da177e4SLinus Torvalds 	if (status & STS_IAA) {
752e82cc128SDavid Brownell 		/* guard against (alleged) silicon errata */
753e82cc128SDavid Brownell 		if (cmd & CMD_IAAD) {
754e82cc128SDavid Brownell 			ehci_writel(ehci, cmd & ~CMD_IAAD,
755e82cc128SDavid Brownell 					&ehci->regs->command);
756e82cc128SDavid Brownell 			ehci_dbg(ehci, "IAA with IAAD still set?\n");
757e82cc128SDavid Brownell 		}
758e82cc128SDavid Brownell 		if (ehci->reclaim) {
7591da177e4SLinus Torvalds 			COUNT(ehci->stats.reclaim);
76007d29b63SAlan Stern 			end_unlink_async(ehci);
761e82cc128SDavid Brownell 		} else
762e82cc128SDavid Brownell 			ehci_dbg(ehci, "IAA with nothing to reclaim?\n");
7631da177e4SLinus Torvalds 	}
7641da177e4SLinus Torvalds 
7651da177e4SLinus Torvalds 	/* remote wakeup [4.3.1] */
766d97cc2f2SDavid Brownell 	if (status & STS_PCD) {
7671da177e4SLinus Torvalds 		unsigned	i = HCS_N_PORTS (ehci->hcs_params);
768d1b1842cSDavid Brownell 
769d1b1842cSDavid Brownell 		/* kick root hub later */
7701d619f12SMarcelo Tosatti 		pcd_status = status;
7711da177e4SLinus Torvalds 
7721da177e4SLinus Torvalds 		/* resume root hub? */
773eafe5b99SAlan Stern 		if (!(cmd & CMD_RUN))
7748c03356aSAlan Stern 			usb_hcd_resume_root_hub(hcd);
7751da177e4SLinus Torvalds 
7761da177e4SLinus Torvalds 		while (i--) {
777083522d7SBenjamin Herrenschmidt 			int pstatus = ehci_readl(ehci,
778083522d7SBenjamin Herrenschmidt 						 &ehci->regs->port_status [i]);
779b972b68cSDavid Brownell 
780b972b68cSDavid Brownell 			if (pstatus & PORT_OWNER)
7811da177e4SLinus Torvalds 				continue;
782eafe5b99SAlan Stern 			if (!(test_bit(i, &ehci->suspended_ports) &&
783eafe5b99SAlan Stern 					((pstatus & PORT_RESUME) ||
784eafe5b99SAlan Stern 						!(pstatus & PORT_SUSPEND)) &&
785eafe5b99SAlan Stern 					(pstatus & PORT_PE) &&
786eafe5b99SAlan Stern 					ehci->reset_done[i] == 0))
7871da177e4SLinus Torvalds 				continue;
7881da177e4SLinus Torvalds 
7891da177e4SLinus Torvalds 			/* start 20 msec resume signaling from this port,
7901da177e4SLinus Torvalds 			 * and make khubd collect PORT_STAT_C_SUSPEND to
79149d0f078SAlan Stern 			 * stop that signaling.  Use 5 ms extra for safety,
79249d0f078SAlan Stern 			 * like usb_port_resume() does.
7931da177e4SLinus Torvalds 			 */
79449d0f078SAlan Stern 			ehci->reset_done[i] = jiffies + msecs_to_jiffies(25);
7951da177e4SLinus Torvalds 			ehci_dbg (ehci, "port %d remote wakeup\n", i + 1);
79661e8b858SAlan Stern 			mod_timer(&hcd->rh_timer, ehci->reset_done[i]);
7971da177e4SLinus Torvalds 		}
7981da177e4SLinus Torvalds 	}
7991da177e4SLinus Torvalds 
8001da177e4SLinus Torvalds 	/* PCI errors [4.15.2.4] */
8011da177e4SLinus Torvalds 	if (unlikely ((status & STS_FATAL) != 0)) {
80267b2e029SAlan Stern 		ehci_err(ehci, "fatal error\n");
803eafe5b99SAlan Stern 		dbg_cmd(ehci, "fatal", cmd);
8041da177e4SLinus Torvalds 		dbg_status(ehci, "fatal", status);
80567b2e029SAlan Stern 		ehci_halt(ehci);
8061da177e4SLinus Torvalds dead:
8071da177e4SLinus Torvalds 		ehci_reset(ehci);
808083522d7SBenjamin Herrenschmidt 		ehci_writel(ehci, 0, &ehci->regs->configured_flag);
8091da177e4SLinus Torvalds 		/* generic layer kills/unlinks all urbs, then
8101da177e4SLinus Torvalds 		 * uses ehci_stop to clean up the rest
8111da177e4SLinus Torvalds 		 */
8121da177e4SLinus Torvalds 		bh = 1;
8131da177e4SLinus Torvalds 	}
8141da177e4SLinus Torvalds 
8151da177e4SLinus Torvalds 	if (bh)
8167d12e780SDavid Howells 		ehci_work (ehci);
8171da177e4SLinus Torvalds 	spin_unlock (&ehci->lock);
818d1b1842cSDavid Brownell 	if (pcd_status)
8191d619f12SMarcelo Tosatti 		usb_hcd_poll_rh_status(hcd);
8201da177e4SLinus Torvalds 	return IRQ_HANDLED;
8211da177e4SLinus Torvalds }
8221da177e4SLinus Torvalds 
8231da177e4SLinus Torvalds /*-------------------------------------------------------------------------*/
8241da177e4SLinus Torvalds 
8251da177e4SLinus Torvalds /*
8261da177e4SLinus Torvalds  * non-error returns are a promise to giveback() the urb later
8271da177e4SLinus Torvalds  * we drop ownership so next owner (or urb unlink) can get it
8281da177e4SLinus Torvalds  *
8291da177e4SLinus Torvalds  * urb + dev is in hcd.self.controller.urb_list
8301da177e4SLinus Torvalds  * we're queueing TDs onto software and hardware lists
8311da177e4SLinus Torvalds  *
8321da177e4SLinus Torvalds  * hcd-specific init for hcpriv hasn't been done yet
8331da177e4SLinus Torvalds  *
8341da177e4SLinus Torvalds  * NOTE:  control, bulk, and interrupt share the same code to append TDs
8351da177e4SLinus Torvalds  * to a (possibly active) QH, and the same QH scanning code.
8361da177e4SLinus Torvalds  */
8371da177e4SLinus Torvalds static int ehci_urb_enqueue (
8381da177e4SLinus Torvalds 	struct usb_hcd	*hcd,
8391da177e4SLinus Torvalds 	struct urb	*urb,
84055016f10SAl Viro 	gfp_t		mem_flags
8411da177e4SLinus Torvalds ) {
8421da177e4SLinus Torvalds 	struct ehci_hcd		*ehci = hcd_to_ehci (hcd);
8431da177e4SLinus Torvalds 	struct list_head	qtd_list;
8441da177e4SLinus Torvalds 
8451da177e4SLinus Torvalds 	INIT_LIST_HEAD (&qtd_list);
8461da177e4SLinus Torvalds 
8471da177e4SLinus Torvalds 	switch (usb_pipetype (urb->pipe)) {
84825b70a86SDavid Brownell 	case PIPE_CONTROL:
84925b70a86SDavid Brownell 		/* qh_completions() code doesn't handle all the fault cases
85025b70a86SDavid Brownell 		 * in multi-TD control transfers.  Even 1KB is rare anyway.
85125b70a86SDavid Brownell 		 */
85225b70a86SDavid Brownell 		if (urb->transfer_buffer_length > (16 * 1024))
85325b70a86SDavid Brownell 			return -EMSGSIZE;
85425b70a86SDavid Brownell 		/* FALLTHROUGH */
85525b70a86SDavid Brownell 	/* case PIPE_BULK: */
8561da177e4SLinus Torvalds 	default:
8571da177e4SLinus Torvalds 		if (!qh_urb_transaction (ehci, urb, &qtd_list, mem_flags))
8581da177e4SLinus Torvalds 			return -ENOMEM;
859e9df41c5SAlan Stern 		return submit_async(ehci, urb, &qtd_list, mem_flags);
8601da177e4SLinus Torvalds 
8611da177e4SLinus Torvalds 	case PIPE_INTERRUPT:
8621da177e4SLinus Torvalds 		if (!qh_urb_transaction (ehci, urb, &qtd_list, mem_flags))
8631da177e4SLinus Torvalds 			return -ENOMEM;
864e9df41c5SAlan Stern 		return intr_submit(ehci, urb, &qtd_list, mem_flags);
8651da177e4SLinus Torvalds 
8661da177e4SLinus Torvalds 	case PIPE_ISOCHRONOUS:
8671da177e4SLinus Torvalds 		if (urb->dev->speed == USB_SPEED_HIGH)
8681da177e4SLinus Torvalds 			return itd_submit (ehci, urb, mem_flags);
8691da177e4SLinus Torvalds 		else
8701da177e4SLinus Torvalds 			return sitd_submit (ehci, urb, mem_flags);
8711da177e4SLinus Torvalds 	}
8721da177e4SLinus Torvalds }
8731da177e4SLinus Torvalds 
8741da177e4SLinus Torvalds static void unlink_async (struct ehci_hcd *ehci, struct ehci_qh *qh)
8751da177e4SLinus Torvalds {
87607d29b63SAlan Stern 	/* failfast */
877e82cc128SDavid Brownell 	if (!HC_IS_RUNNING(ehci_to_hcd(ehci)->state) && ehci->reclaim)
87807d29b63SAlan Stern 		end_unlink_async(ehci);
87907d29b63SAlan Stern 
8803a44494eSAlan Stern 	/* If the QH isn't linked then there's nothing we can do
8813a44494eSAlan Stern 	 * unless we were called during a giveback, in which case
8823a44494eSAlan Stern 	 * qh_completions() has to deal with it.
8833a44494eSAlan Stern 	 */
8843a44494eSAlan Stern 	if (qh->qh_state != QH_STATE_LINKED) {
8853a44494eSAlan Stern 		if (qh->qh_state == QH_STATE_COMPLETING)
8863a44494eSAlan Stern 			qh->needs_rescan = 1;
8873a44494eSAlan Stern 		return;
8883a44494eSAlan Stern 	}
88907d29b63SAlan Stern 
89007d29b63SAlan Stern 	/* defer till later if busy */
8913a44494eSAlan Stern 	if (ehci->reclaim) {
8921da177e4SLinus Torvalds 		struct ehci_qh		*last;
8931da177e4SLinus Torvalds 
8941da177e4SLinus Torvalds 		for (last = ehci->reclaim;
8951da177e4SLinus Torvalds 				last->reclaim;
8961da177e4SLinus Torvalds 				last = last->reclaim)
8971da177e4SLinus Torvalds 			continue;
8981da177e4SLinus Torvalds 		qh->qh_state = QH_STATE_UNLINK_WAIT;
8991da177e4SLinus Torvalds 		last->reclaim = qh;
9001da177e4SLinus Torvalds 
90107d29b63SAlan Stern 	/* start IAA cycle */
90207d29b63SAlan Stern 	} else
9031da177e4SLinus Torvalds 		start_unlink_async (ehci, qh);
9041da177e4SLinus Torvalds }
9051da177e4SLinus Torvalds 
9061da177e4SLinus Torvalds /* remove from hardware lists
9071da177e4SLinus Torvalds  * completions normally happen asynchronously
9081da177e4SLinus Torvalds  */
9091da177e4SLinus Torvalds 
910e9df41c5SAlan Stern static int ehci_urb_dequeue(struct usb_hcd *hcd, struct urb *urb, int status)
9111da177e4SLinus Torvalds {
9121da177e4SLinus Torvalds 	struct ehci_hcd		*ehci = hcd_to_ehci (hcd);
9131da177e4SLinus Torvalds 	struct ehci_qh		*qh;
9141da177e4SLinus Torvalds 	unsigned long		flags;
915e9df41c5SAlan Stern 	int			rc;
9161da177e4SLinus Torvalds 
9171da177e4SLinus Torvalds 	spin_lock_irqsave (&ehci->lock, flags);
918e9df41c5SAlan Stern 	rc = usb_hcd_check_unlink_urb(hcd, urb, status);
919e9df41c5SAlan Stern 	if (rc)
920e9df41c5SAlan Stern 		goto done;
921e9df41c5SAlan Stern 
9221da177e4SLinus Torvalds 	switch (usb_pipetype (urb->pipe)) {
9231da177e4SLinus Torvalds 	// case PIPE_CONTROL:
9241da177e4SLinus Torvalds 	// case PIPE_BULK:
9251da177e4SLinus Torvalds 	default:
9261da177e4SLinus Torvalds 		qh = (struct ehci_qh *) urb->hcpriv;
9271da177e4SLinus Torvalds 		if (!qh)
9281da177e4SLinus Torvalds 			break;
92907d29b63SAlan Stern 		switch (qh->qh_state) {
93007d29b63SAlan Stern 		case QH_STATE_LINKED:
93107d29b63SAlan Stern 		case QH_STATE_COMPLETING:
9321da177e4SLinus Torvalds 			unlink_async(ehci, qh);
9331da177e4SLinus Torvalds 			break;
93407d29b63SAlan Stern 		case QH_STATE_UNLINK:
93507d29b63SAlan Stern 		case QH_STATE_UNLINK_WAIT:
93607d29b63SAlan Stern 			/* already started */
93707d29b63SAlan Stern 			break;
93807d29b63SAlan Stern 		case QH_STATE_IDLE:
9397a0f0d95SAlan Stern 			/* QH might be waiting for a Clear-TT-Buffer */
9407a0f0d95SAlan Stern 			qh_completions(ehci, qh);
94107d29b63SAlan Stern 			break;
94207d29b63SAlan Stern 		}
94307d29b63SAlan Stern 		break;
9441da177e4SLinus Torvalds 
9451da177e4SLinus Torvalds 	case PIPE_INTERRUPT:
9461da177e4SLinus Torvalds 		qh = (struct ehci_qh *) urb->hcpriv;
9471da177e4SLinus Torvalds 		if (!qh)
9481da177e4SLinus Torvalds 			break;
9491da177e4SLinus Torvalds 		switch (qh->qh_state) {
9501da177e4SLinus Torvalds 		case QH_STATE_LINKED:
951a448c9d8SAlan Stern 		case QH_STATE_COMPLETING:
9521da177e4SLinus Torvalds 			intr_deschedule (ehci, qh);
953a448c9d8SAlan Stern 			break;
9541da177e4SLinus Torvalds 		case QH_STATE_IDLE:
9557d12e780SDavid Howells 			qh_completions (ehci, qh);
9561da177e4SLinus Torvalds 			break;
9571da177e4SLinus Torvalds 		default:
9581da177e4SLinus Torvalds 			ehci_dbg (ehci, "bogus qh %p state %d\n",
9591da177e4SLinus Torvalds 					qh, qh->qh_state);
9601da177e4SLinus Torvalds 			goto done;
9611da177e4SLinus Torvalds 		}
9621da177e4SLinus Torvalds 		break;
9631da177e4SLinus Torvalds 
9641da177e4SLinus Torvalds 	case PIPE_ISOCHRONOUS:
9651da177e4SLinus Torvalds 		// itd or sitd ...
9661da177e4SLinus Torvalds 
9671da177e4SLinus Torvalds 		// wait till next completion, do it then.
9681da177e4SLinus Torvalds 		// completion irqs can wait up to 1024 msec,
9691da177e4SLinus Torvalds 		break;
9701da177e4SLinus Torvalds 	}
9711da177e4SLinus Torvalds done:
9721da177e4SLinus Torvalds 	spin_unlock_irqrestore (&ehci->lock, flags);
973e9df41c5SAlan Stern 	return rc;
9741da177e4SLinus Torvalds }
9751da177e4SLinus Torvalds 
9761da177e4SLinus Torvalds /*-------------------------------------------------------------------------*/
9771da177e4SLinus Torvalds 
9781da177e4SLinus Torvalds // bulk qh holds the data toggle
9791da177e4SLinus Torvalds 
9801da177e4SLinus Torvalds static void
9811da177e4SLinus Torvalds ehci_endpoint_disable (struct usb_hcd *hcd, struct usb_host_endpoint *ep)
9821da177e4SLinus Torvalds {
9831da177e4SLinus Torvalds 	struct ehci_hcd		*ehci = hcd_to_ehci (hcd);
9841da177e4SLinus Torvalds 	unsigned long		flags;
9851da177e4SLinus Torvalds 	struct ehci_qh		*qh, *tmp;
9861da177e4SLinus Torvalds 
9871da177e4SLinus Torvalds 	/* ASSERT:  any requests/urbs are being unlinked */
9881da177e4SLinus Torvalds 	/* ASSERT:  nobody can be submitting urbs for this any more */
9891da177e4SLinus Torvalds 
9901da177e4SLinus Torvalds rescan:
9911da177e4SLinus Torvalds 	spin_lock_irqsave (&ehci->lock, flags);
9921da177e4SLinus Torvalds 	qh = ep->hcpriv;
9931da177e4SLinus Torvalds 	if (!qh)
9941da177e4SLinus Torvalds 		goto done;
9951da177e4SLinus Torvalds 
9961da177e4SLinus Torvalds 	/* endpoints can be iso streams.  for now, we don't
9971da177e4SLinus Torvalds 	 * accelerate iso completions ... so spin a while.
9981da177e4SLinus Torvalds 	 */
9991082f57aSClemens Ladisch 	if (qh->hw == NULL) {
10001da177e4SLinus Torvalds 		ehci_vdbg (ehci, "iso delay\n");
10011da177e4SLinus Torvalds 		goto idle_timeout;
10021da177e4SLinus Torvalds 	}
10031da177e4SLinus Torvalds 
10041da177e4SLinus Torvalds 	if (!HC_IS_RUNNING (hcd->state))
10051da177e4SLinus Torvalds 		qh->qh_state = QH_STATE_IDLE;
10061da177e4SLinus Torvalds 	switch (qh->qh_state) {
10071da177e4SLinus Torvalds 	case QH_STATE_LINKED:
10083a44494eSAlan Stern 	case QH_STATE_COMPLETING:
10091da177e4SLinus Torvalds 		for (tmp = ehci->async->qh_next.qh;
10101da177e4SLinus Torvalds 				tmp && tmp != qh;
10111da177e4SLinus Torvalds 				tmp = tmp->qh_next.qh)
10121da177e4SLinus Torvalds 			continue;
10131da177e4SLinus Torvalds 		/* periodic qh self-unlinks on empty */
10141da177e4SLinus Torvalds 		if (!tmp)
10151da177e4SLinus Torvalds 			goto nogood;
10161da177e4SLinus Torvalds 		unlink_async (ehci, qh);
10171da177e4SLinus Torvalds 		/* FALL THROUGH */
10181da177e4SLinus Torvalds 	case QH_STATE_UNLINK:		/* wait for hw to finish? */
101907d29b63SAlan Stern 	case QH_STATE_UNLINK_WAIT:
10201da177e4SLinus Torvalds idle_timeout:
10211da177e4SLinus Torvalds 		spin_unlock_irqrestore (&ehci->lock, flags);
102222c43863SNishanth Aravamudan 		schedule_timeout_uninterruptible(1);
10231da177e4SLinus Torvalds 		goto rescan;
10241da177e4SLinus Torvalds 	case QH_STATE_IDLE:		/* fully unlinked */
1025914b7012SAlan Stern 		if (qh->clearing_tt)
1026914b7012SAlan Stern 			goto idle_timeout;
10271da177e4SLinus Torvalds 		if (list_empty (&qh->qtd_list)) {
10281da177e4SLinus Torvalds 			qh_put (qh);
10291da177e4SLinus Torvalds 			break;
10301da177e4SLinus Torvalds 		}
10311da177e4SLinus Torvalds 		/* else FALL THROUGH */
10321da177e4SLinus Torvalds 	default:
10331da177e4SLinus Torvalds nogood:
10341da177e4SLinus Torvalds 		/* caller was supposed to have unlinked any requests;
10351da177e4SLinus Torvalds 		 * that's not our job.  just leak this memory.
10361da177e4SLinus Torvalds 		 */
10371da177e4SLinus Torvalds 		ehci_err (ehci, "qh %p (#%02x) state %d%s\n",
10381da177e4SLinus Torvalds 			qh, ep->desc.bEndpointAddress, qh->qh_state,
10391da177e4SLinus Torvalds 			list_empty (&qh->qtd_list) ? "" : "(has tds)");
10401da177e4SLinus Torvalds 		break;
10411da177e4SLinus Torvalds 	}
10421da177e4SLinus Torvalds 	ep->hcpriv = NULL;
10431da177e4SLinus Torvalds done:
10441da177e4SLinus Torvalds 	spin_unlock_irqrestore (&ehci->lock, flags);
10451da177e4SLinus Torvalds 	return;
10461da177e4SLinus Torvalds }
10471da177e4SLinus Torvalds 
1048b18ffd49SAlan Stern static void
1049b18ffd49SAlan Stern ehci_endpoint_reset(struct usb_hcd *hcd, struct usb_host_endpoint *ep)
1050b18ffd49SAlan Stern {
1051b18ffd49SAlan Stern 	struct ehci_hcd		*ehci = hcd_to_ehci(hcd);
1052b18ffd49SAlan Stern 	struct ehci_qh		*qh;
1053b18ffd49SAlan Stern 	int			eptype = usb_endpoint_type(&ep->desc);
1054a455212dSAlan Stern 	int			epnum = usb_endpoint_num(&ep->desc);
1055a455212dSAlan Stern 	int			is_out = usb_endpoint_dir_out(&ep->desc);
1056a455212dSAlan Stern 	unsigned long		flags;
1057b18ffd49SAlan Stern 
1058b18ffd49SAlan Stern 	if (eptype != USB_ENDPOINT_XFER_BULK && eptype != USB_ENDPOINT_XFER_INT)
1059b18ffd49SAlan Stern 		return;
1060b18ffd49SAlan Stern 
1061a455212dSAlan Stern 	spin_lock_irqsave(&ehci->lock, flags);
1062b18ffd49SAlan Stern 	qh = ep->hcpriv;
1063b18ffd49SAlan Stern 
1064b18ffd49SAlan Stern 	/* For Bulk and Interrupt endpoints we maintain the toggle state
1065b18ffd49SAlan Stern 	 * in the hardware; the toggle bits in udev aren't used at all.
1066b18ffd49SAlan Stern 	 * When an endpoint is reset by usb_clear_halt() we must reset
1067b18ffd49SAlan Stern 	 * the toggle bit in the QH.
1068b18ffd49SAlan Stern 	 */
1069b18ffd49SAlan Stern 	if (qh) {
1070a455212dSAlan Stern 		usb_settoggle(qh->dev, epnum, is_out, 0);
1071b18ffd49SAlan Stern 		if (!list_empty(&qh->qtd_list)) {
1072b18ffd49SAlan Stern 			WARN_ONCE(1, "clear_halt for a busy endpoint\n");
10733a44494eSAlan Stern 		} else if (qh->qh_state == QH_STATE_LINKED ||
10743a44494eSAlan Stern 				qh->qh_state == QH_STATE_COMPLETING) {
1075a455212dSAlan Stern 
1076a455212dSAlan Stern 			/* The toggle value in the QH can't be updated
1077a455212dSAlan Stern 			 * while the QH is active.  Unlink it now;
1078a455212dSAlan Stern 			 * re-linking will call qh_refresh().
1079b18ffd49SAlan Stern 			 */
1080a448c9d8SAlan Stern 			if (eptype == USB_ENDPOINT_XFER_BULK)
1081b18ffd49SAlan Stern 				unlink_async(ehci, qh);
1082a448c9d8SAlan Stern 			else
1083b18ffd49SAlan Stern 				intr_deschedule(ehci, qh);
1084b18ffd49SAlan Stern 		}
1085b18ffd49SAlan Stern 	}
1086a455212dSAlan Stern 	spin_unlock_irqrestore(&ehci->lock, flags);
1087b18ffd49SAlan Stern }
1088b18ffd49SAlan Stern 
10897ff71d6aSMatt Porter static int ehci_get_frame (struct usb_hcd *hcd)
10907ff71d6aSMatt Porter {
10917ff71d6aSMatt Porter 	struct ehci_hcd		*ehci = hcd_to_ehci (hcd);
1092083522d7SBenjamin Herrenschmidt 	return (ehci_readl(ehci, &ehci->regs->frame_index) >> 3) %
1093083522d7SBenjamin Herrenschmidt 		ehci->periodic_size;
10947ff71d6aSMatt Porter }
10951da177e4SLinus Torvalds 
10961da177e4SLinus Torvalds /*-------------------------------------------------------------------------*/
10971da177e4SLinus Torvalds 
10982b70f073SAlan Stern MODULE_DESCRIPTION(DRIVER_DESC);
10991da177e4SLinus Torvalds MODULE_AUTHOR (DRIVER_AUTHOR);
11001da177e4SLinus Torvalds MODULE_LICENSE ("GPL");
11011da177e4SLinus Torvalds 
11027ff71d6aSMatt Porter #ifdef CONFIG_PCI
11037ff71d6aSMatt Porter #include "ehci-pci.c"
110401cced25SKumar Gala #define	PCI_DRIVER		ehci_pci_driver
11057ff71d6aSMatt Porter #endif
11061da177e4SLinus Torvalds 
1107ba02978aSLi Yang #ifdef CONFIG_USB_EHCI_FSL
110880cb9aeeSRandy Vinson #include "ehci-fsl.c"
110901cced25SKumar Gala #define	PLATFORM_DRIVER		ehci_fsl_driver
111080cb9aeeSRandy Vinson #endif
111180cb9aeeSRandy Vinson 
11127e8d5cd9SDaniel Mack #ifdef CONFIG_USB_EHCI_MXC
11137e8d5cd9SDaniel Mack #include "ehci-mxc.c"
11147e8d5cd9SDaniel Mack #define PLATFORM_DRIVER		ehci_mxc_driver
11157e8d5cd9SDaniel Mack #endif
11167e8d5cd9SDaniel Mack 
1117dfbaa7d8SRalf Baechle #ifdef CONFIG_SOC_AU1200
111876fa9a24SJordan Crouse #include "ehci-au1xxx.c"
111901cced25SKumar Gala #define	PLATFORM_DRIVER		ehci_hcd_au1xxx_driver
112076fa9a24SJordan Crouse #endif
112176fa9a24SJordan Crouse 
1122a8eb7ca0STony Lindgren #ifdef CONFIG_ARCH_OMAP3
112354ab2b02SFelipe Balbi #include "ehci-omap.c"
112454ab2b02SFelipe Balbi #define        PLATFORM_DRIVER         ehci_hcd_omap_driver
112554ab2b02SFelipe Balbi #endif
112654ab2b02SFelipe Balbi 
1127ad75a410SGeoff Levand #ifdef CONFIG_PPC_PS3
1128ad75a410SGeoff Levand #include "ehci-ps3.c"
11297a4eb7fdSGeoff Levand #define	PS3_SYSTEM_BUS_DRIVER	ps3_ehci_driver
1130ad75a410SGeoff Levand #endif
1131ad75a410SGeoff Levand 
1132da0e8fb0SValentine Barshak #ifdef CONFIG_USB_EHCI_HCD_PPC_OF
1133da0e8fb0SValentine Barshak #include "ehci-ppc-of.c"
1134da0e8fb0SValentine Barshak #define OF_PLATFORM_DRIVER	ehci_hcd_ppc_of_driver
1135da0e8fb0SValentine Barshak #endif
1136da0e8fb0SValentine Barshak 
113708d3c18eSJulie Zhu #ifdef CONFIG_XPS_USB_HCD_XILINX
113808d3c18eSJulie Zhu #include "ehci-xilinx-of.c"
113908d3c18eSJulie Zhu #define OF_PLATFORM_DRIVER	ehci_hcd_xilinx_of_driver
114008d3c18eSJulie Zhu #endif
114108d3c18eSJulie Zhu 
1142705a7521SLennert Buytenhek #ifdef CONFIG_PLAT_ORION
1143e96ffe2fSTzachi Perelstein #include "ehci-orion.c"
1144e96ffe2fSTzachi Perelstein #define	PLATFORM_DRIVER		ehci_orion_driver
1145e96ffe2fSTzachi Perelstein #endif
1146e96ffe2fSTzachi Perelstein 
114791bc4d31SVladimir Barinov #ifdef CONFIG_ARCH_IXP4XX
114891bc4d31SVladimir Barinov #include "ehci-ixp4xx.c"
114991bc4d31SVladimir Barinov #define	PLATFORM_DRIVER		ixp4xx_ehci_driver
115091bc4d31SVladimir Barinov #endif
115191bc4d31SVladimir Barinov 
1152586dfc8cSWan ZongShun #ifdef CONFIG_USB_W90X900_EHCI
1153586dfc8cSWan ZongShun #include "ehci-w90x900.c"
1154586dfc8cSWan ZongShun #define	PLATFORM_DRIVER		ehci_hcd_w90x900_driver
1155586dfc8cSWan ZongShun #endif
1156586dfc8cSWan ZongShun 
1157501c9c08SNicolas Ferre #ifdef CONFIG_ARCH_AT91
1158501c9c08SNicolas Ferre #include "ehci-atmel.c"
1159501c9c08SNicolas Ferre #define	PLATFORM_DRIVER		ehci_atmel_driver
1160501c9c08SNicolas Ferre #endif
1161501c9c08SNicolas Ferre 
1162ad75a410SGeoff Levand #if !defined(PCI_DRIVER) && !defined(PLATFORM_DRIVER) && \
1163c6dd2e61SAnton Vorontsov     !defined(PS3_SYSTEM_BUS_DRIVER) && !defined(OF_PLATFORM_DRIVER)
11647ff71d6aSMatt Porter #error "missing bus glue for ehci-hcd"
11657ff71d6aSMatt Porter #endif
116601cced25SKumar Gala 
116701cced25SKumar Gala static int __init ehci_hcd_init(void)
116801cced25SKumar Gala {
116901cced25SKumar Gala 	int retval = 0;
117001cced25SKumar Gala 
11712b70f073SAlan Stern 	if (usb_disabled())
11722b70f073SAlan Stern 		return -ENODEV;
11732b70f073SAlan Stern 
11742b70f073SAlan Stern 	printk(KERN_INFO "%s: " DRIVER_DESC "\n", hcd_name);
11759beeee65SAlan Stern 	set_bit(USB_EHCI_LOADED, &usb_hcds_loaded);
11769beeee65SAlan Stern 	if (test_bit(USB_UHCI_LOADED, &usb_hcds_loaded) ||
11779beeee65SAlan Stern 			test_bit(USB_OHCI_LOADED, &usb_hcds_loaded))
11789beeee65SAlan Stern 		printk(KERN_WARNING "Warning! ehci_hcd should always be loaded"
11799beeee65SAlan Stern 				" before uhci_hcd and ohci_hcd, not after\n");
11809beeee65SAlan Stern 
118101cced25SKumar Gala 	pr_debug("%s: block sizes: qh %Zd qtd %Zd itd %Zd sitd %Zd\n",
118201cced25SKumar Gala 		 hcd_name,
118301cced25SKumar Gala 		 sizeof(struct ehci_qh), sizeof(struct ehci_qtd),
118401cced25SKumar Gala 		 sizeof(struct ehci_itd), sizeof(struct ehci_sitd));
118501cced25SKumar Gala 
1186694cc208STony Jones #ifdef DEBUG
118708f4e586SGreg Kroah-Hartman 	ehci_debug_root = debugfs_create_dir("ehci", usb_debug_root);
11889beeee65SAlan Stern 	if (!ehci_debug_root) {
11899beeee65SAlan Stern 		retval = -ENOENT;
11909beeee65SAlan Stern 		goto err_debug;
11919beeee65SAlan Stern 	}
1192694cc208STony Jones #endif
1193694cc208STony Jones 
119401cced25SKumar Gala #ifdef PLATFORM_DRIVER
119501cced25SKumar Gala 	retval = platform_driver_register(&PLATFORM_DRIVER);
1196da0e8fb0SValentine Barshak 	if (retval < 0)
1197da0e8fb0SValentine Barshak 		goto clean0;
119801cced25SKumar Gala #endif
119901cced25SKumar Gala 
120001cced25SKumar Gala #ifdef PCI_DRIVER
120101cced25SKumar Gala 	retval = pci_register_driver(&PCI_DRIVER);
1202da0e8fb0SValentine Barshak 	if (retval < 0)
1203da0e8fb0SValentine Barshak 		goto clean1;
1204ad75a410SGeoff Levand #endif
1205ad75a410SGeoff Levand 
1206ad75a410SGeoff Levand #ifdef PS3_SYSTEM_BUS_DRIVER
12077a4eb7fdSGeoff Levand 	retval = ps3_ehci_driver_register(&PS3_SYSTEM_BUS_DRIVER);
1208da0e8fb0SValentine Barshak 	if (retval < 0)
1209da0e8fb0SValentine Barshak 		goto clean2;
1210da0e8fb0SValentine Barshak #endif
1211da0e8fb0SValentine Barshak 
1212da0e8fb0SValentine Barshak #ifdef OF_PLATFORM_DRIVER
1213da0e8fb0SValentine Barshak 	retval = of_register_platform_driver(&OF_PLATFORM_DRIVER);
1214da0e8fb0SValentine Barshak 	if (retval < 0)
1215da0e8fb0SValentine Barshak 		goto clean3;
1216da0e8fb0SValentine Barshak #endif
1217da0e8fb0SValentine Barshak 	return retval;
1218da0e8fb0SValentine Barshak 
1219da0e8fb0SValentine Barshak #ifdef OF_PLATFORM_DRIVER
1220da0e8fb0SValentine Barshak 	/* of_unregister_platform_driver(&OF_PLATFORM_DRIVER); */
1221da0e8fb0SValentine Barshak clean3:
1222da0e8fb0SValentine Barshak #endif
1223da0e8fb0SValentine Barshak #ifdef PS3_SYSTEM_BUS_DRIVER
1224da0e8fb0SValentine Barshak 	ps3_ehci_driver_unregister(&PS3_SYSTEM_BUS_DRIVER);
1225da0e8fb0SValentine Barshak clean2:
1226da0e8fb0SValentine Barshak #endif
1227da0e8fb0SValentine Barshak #ifdef PCI_DRIVER
1228da0e8fb0SValentine Barshak 	pci_unregister_driver(&PCI_DRIVER);
1229da0e8fb0SValentine Barshak clean1:
1230da0e8fb0SValentine Barshak #endif
1231da0e8fb0SValentine Barshak #ifdef PLATFORM_DRIVER
1232da0e8fb0SValentine Barshak 	platform_driver_unregister(&PLATFORM_DRIVER);
1233da0e8fb0SValentine Barshak clean0:
1234da0e8fb0SValentine Barshak #endif
1235694cc208STony Jones #ifdef DEBUG
1236694cc208STony Jones 	debugfs_remove(ehci_debug_root);
1237694cc208STony Jones 	ehci_debug_root = NULL;
12389beeee65SAlan Stern err_debug:
1239a9b6148dSLinus Torvalds #endif
12409beeee65SAlan Stern 	clear_bit(USB_EHCI_LOADED, &usb_hcds_loaded);
124101cced25SKumar Gala 	return retval;
124201cced25SKumar Gala }
124301cced25SKumar Gala module_init(ehci_hcd_init);
124401cced25SKumar Gala 
124501cced25SKumar Gala static void __exit ehci_hcd_cleanup(void)
124601cced25SKumar Gala {
1247da0e8fb0SValentine Barshak #ifdef OF_PLATFORM_DRIVER
1248da0e8fb0SValentine Barshak 	of_unregister_platform_driver(&OF_PLATFORM_DRIVER);
1249da0e8fb0SValentine Barshak #endif
125001cced25SKumar Gala #ifdef PLATFORM_DRIVER
125101cced25SKumar Gala 	platform_driver_unregister(&PLATFORM_DRIVER);
125201cced25SKumar Gala #endif
125301cced25SKumar Gala #ifdef PCI_DRIVER
125401cced25SKumar Gala 	pci_unregister_driver(&PCI_DRIVER);
125501cced25SKumar Gala #endif
1256ad75a410SGeoff Levand #ifdef PS3_SYSTEM_BUS_DRIVER
12577a4eb7fdSGeoff Levand 	ps3_ehci_driver_unregister(&PS3_SYSTEM_BUS_DRIVER);
1258ad75a410SGeoff Levand #endif
1259694cc208STony Jones #ifdef DEBUG
1260694cc208STony Jones 	debugfs_remove(ehci_debug_root);
1261694cc208STony Jones #endif
12629beeee65SAlan Stern 	clear_bit(USB_EHCI_LOADED, &usb_hcds_loaded);
126301cced25SKumar Gala }
126401cced25SKumar Gala module_exit(ehci_hcd_cleanup);
126501cced25SKumar Gala 
1266