blob: 8608ac513fb764e4c8069cf870ecd1e12e068d1d [file] [log] [blame]
Greg Kroah-Hartman5fd54ac2017-11-03 11:28:30 +01001// SPDX-License-Identifier: GPL-2.0+
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
Alan Stern578333a2011-06-15 16:32:46 -04003 * Enhanced Host Controller Interface (EHCI) driver for USB.
4 *
5 * Maintainer: Alan Stern <stern@rowland.harvard.edu>
6 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 * Copyright (c) 2000-2004 by David Brownell
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 */
9
Linus Torvalds1da177e2005-04-16 15:20:36 -070010#include <linux/module.h>
11#include <linux/pci.h>
12#include <linux/dmapool.h>
13#include <linux/kernel.h>
14#include <linux/delay.h>
15#include <linux/ioport.h>
16#include <linux/sched.h>
Ming Lei3c04e202008-09-18 23:06:21 +080017#include <linux/vmalloc.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include <linux/errno.h>
19#include <linux/init.h>
Alan Sternd58b4bc2012-07-11 11:21:54 -040020#include <linux/hrtimer.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include <linux/list.h>
22#include <linux/interrupt.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <linux/usb.h>
Eric Lescouet27729aa2010-04-24 23:21:52 +020024#include <linux/usb/hcd.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include <linux/moduleparam.h>
26#include <linux/dma-mapping.h>
Tony Jones694cc202007-09-11 14:07:31 -070027#include <linux/debugfs.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090028#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include <asm/byteorder.h>
31#include <asm/io.h>
32#include <asm/irq.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include <asm/unaligned.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070034
Geoff Levanddf7c1ca2011-11-30 16:40:57 -080035#if defined(CONFIG_PPC_PS3)
36#include <asm/firmware.h>
37#endif
38
Linus Torvalds1da177e2005-04-16 15:20:36 -070039/*-------------------------------------------------------------------------*/
40
41/*
42 * EHCI hc_driver implementation ... experimental, incomplete.
43 * Based on the final 1.0 register interface specification.
44 *
45 * USB 2.0 shows up in upcoming www.pcmcia.org technology.
46 * First was PCMCIA, like ISA; then CardBus, which is PCI.
47 * Next comes "CardBay", using USB 2.0 signals.
48 *
49 * Contains additional contributions by Brad Hards, Rory Bolt, and others.
50 * Special thanks to Intel and VIA for providing host controllers to
51 * test this driver on, and Cypress (including In-System Design) for
52 * providing early devices for those host controllers to talk to!
Linus Torvalds1da177e2005-04-16 15:20:36 -070053 */
54
Linus Torvalds1da177e2005-04-16 15:20:36 -070055#define DRIVER_AUTHOR "David Brownell"
56#define DRIVER_DESC "USB 2.0 'Enhanced' Host Controller (EHCI) Driver"
57
58static const char hcd_name [] = "ehci_hcd";
59
60
Linus Torvalds1da177e2005-04-16 15:20:36 -070061#undef EHCI_URB_TRACE
62
Linus Torvalds1da177e2005-04-16 15:20:36 -070063/* magic numbers that can affect system performance */
64#define EHCI_TUNE_CERR 3 /* 0-3 qtd retries; 0 == don't stop */
65#define EHCI_TUNE_RL_HS 4 /* nak throttle; see 4.9 */
66#define EHCI_TUNE_RL_TT 0
67#define EHCI_TUNE_MULT_HS 1 /* 1-3 transactions/uframe; 4.10.3 */
68#define EHCI_TUNE_MULT_TT 1
Alan Sternffda0802010-07-14 11:03:46 -040069/*
70 * Some drivers think it's safe to schedule isochronous transfers more than
71 * 256 ms into the future (partly as a result of an old bug in the scheduling
72 * code). In an attempt to avoid trouble, we will use a minimum scheduling
73 * length of 512 frames instead of 256.
74 */
75#define EHCI_TUNE_FLS 1 /* (medium) 512-frame schedule */
Linus Torvalds1da177e2005-04-16 15:20:36 -070076
Linus Torvalds1da177e2005-04-16 15:20:36 -070077/* Initial IRQ latency: faster than hw default */
78static int log2_irq_thresh = 0; // 0 to 6
79module_param (log2_irq_thresh, int, S_IRUGO);
80MODULE_PARM_DESC (log2_irq_thresh, "log2 IRQ latency, 1-64 microframes");
81
82/* initial park setting: slower than hw default */
83static unsigned park = 0;
84module_param (park, uint, S_IRUGO);
85MODULE_PARM_DESC (park, "park setting; 1-3 back-to-back async packets");
86
David Brownell93f1a472006-11-16 23:34:58 -080087/* for flakey hardware, ignore overcurrent indicators */
Geyslan G. Bem900937c2015-12-02 20:25:23 -030088static bool ignore_oc;
David Brownell93f1a472006-11-16 23:34:58 -080089module_param (ignore_oc, bool, S_IRUGO);
90MODULE_PARM_DESC (ignore_oc, "ignore bogus hardware overcurrent indications");
91
Linus Torvalds1da177e2005-04-16 15:20:36 -070092#define INTR_MASK (STS_IAA | STS_FATAL | STS_PCD | STS_ERR | STS_INT)
93
94/*-------------------------------------------------------------------------*/
95
96#include "ehci.h"
Andiry Xuad935622011-03-01 14:57:05 +080097#include "pci-quirks.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070098
Alan Sternb35c5002013-10-11 22:16:21 -040099static void compute_tt_budget(u8 budget_table[EHCI_BANDWIDTH_SIZE],
100 struct ehci_tt *tt);
101
Alan Sternacc08502012-10-10 15:07:39 -0400102/*
103 * The MosChip MCS9990 controller updates its microframe counter
104 * a little before the frame counter, and occasionally we will read
105 * the invalid intermediate value. Avoid problems by checking the
106 * microframe number (the low-order 3 bits); if they are 0 then
107 * re-read the register to get the correct value.
108 */
109static unsigned ehci_moschip_read_frame_index(struct ehci_hcd *ehci)
110{
111 unsigned uf;
112
113 uf = ehci_readl(ehci, &ehci->regs->frame_index);
114 if (unlikely((uf & 7) == 0))
115 uf = ehci_readl(ehci, &ehci->regs->frame_index);
116 return uf;
117}
118
119static inline unsigned ehci_read_frame_index(struct ehci_hcd *ehci)
120{
121 if (ehci->frame_index_bug)
122 return ehci_moschip_read_frame_index(ehci);
123 return ehci_readl(ehci, &ehci->regs->frame_index);
124}
125
126#include "ehci-dbg.c"
127
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128/*-------------------------------------------------------------------------*/
129
130/*
Manjunath Goudar2f3a6b82013-06-13 11:24:09 -0600131 * ehci_handshake - spin reading hc until handshake completes or fails
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 * @ptr: address of hc register to be read
133 * @mask: bits to look at in result of read
134 * @done: value of those bits when handshake succeeds
135 * @usec: timeout in microseconds
136 *
137 * Returns negative errno, or zero on success
138 *
139 * Success happens when the "mask" bits have the specified value (hardware
140 * handshake done). There are two failure modes: "usec" have passed (major
141 * hardware flakeout), or the register reads as all-ones (hardware removed).
142 *
143 * That last failure should_only happen in cases like physical cardbus eject
144 * before driver shutdown. But it also seems to be caused by bugs in cardbus
145 * bridge shutdown: shutting down the bridge before the devices using it.
146 */
Manjunath Goudar2f3a6b82013-06-13 11:24:09 -0600147int ehci_handshake(struct ehci_hcd *ehci, void __iomem *ptr,
148 u32 mask, u32 done, int usec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149{
150 u32 result;
151
152 do {
Benjamin Herrenschmidt083522d2006-12-15 06:54:08 +1100153 result = ehci_readl(ehci, ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 if (result == ~(u32)0) /* card removed */
155 return -ENODEV;
156 result &= mask;
157 if (result == done)
158 return 0;
159 udelay (1);
160 usec--;
161 } while (usec > 0);
162 return -ETIMEDOUT;
163}
Manjunath Goudar2f3a6b82013-06-13 11:24:09 -0600164EXPORT_SYMBOL_GPL(ehci_handshake);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165
Matthieu CASTET65fd4272010-09-06 18:26:56 +0200166/* check TDI/ARC silicon is in host mode */
167static int tdi_in_host_mode (struct ehci_hcd *ehci)
168{
Matthieu CASTET65fd4272010-09-06 18:26:56 +0200169 u32 tmp;
170
Alan Sterna46af4e2012-06-25 12:19:03 -0400171 tmp = ehci_readl(ehci, &ehci->regs->usbmode);
Matthieu CASTET65fd4272010-09-06 18:26:56 +0200172 return (tmp & 3) == USBMODE_CM_HC;
173}
174
Alan Sternc4f34762012-07-11 11:23:10 -0400175/*
176 * Force HC to halt state from unknown (EHCI spec section 2.3).
177 * Must be called with interrupts enabled and the lock not held.
178 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179static int ehci_halt (struct ehci_hcd *ehci)
180{
Alan Sternc4f34762012-07-11 11:23:10 -0400181 u32 temp;
182
183 spin_lock_irq(&ehci->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184
David Brownell72f30b62005-09-27 10:19:39 -0700185 /* disable any irqs left enabled by previous code */
Benjamin Herrenschmidt083522d2006-12-15 06:54:08 +1100186 ehci_writel(ehci, 0, &ehci->regs->intr_enable);
David Brownell72f30b62005-09-27 10:19:39 -0700187
Alan Sternc4f34762012-07-11 11:23:10 -0400188 if (ehci_is_TDI(ehci) && !tdi_in_host_mode(ehci)) {
189 spin_unlock_irq(&ehci->lock);
Matthieu CASTET65fd4272010-09-06 18:26:56 +0200190 return 0;
191 }
192
Alan Stern3d9545c2012-04-23 13:54:36 -0400193 /*
194 * This routine gets called during probe before ehci->command
195 * has been initialized, so we can't rely on its value.
196 */
197 ehci->command &= ~CMD_RUN;
Benjamin Herrenschmidt083522d2006-12-15 06:54:08 +1100198 temp = ehci_readl(ehci, &ehci->regs->command);
Alan Stern3d9545c2012-04-23 13:54:36 -0400199 temp &= ~(CMD_RUN | CMD_IAAD);
Benjamin Herrenschmidt083522d2006-12-15 06:54:08 +1100200 ehci_writel(ehci, temp, &ehci->regs->command);
Alan Sternc4f34762012-07-11 11:23:10 -0400201
202 spin_unlock_irq(&ehci->lock);
203 synchronize_irq(ehci_to_hcd(ehci)->irq);
204
Manjunath Goudar2f3a6b82013-06-13 11:24:09 -0600205 return ehci_handshake(ehci, &ehci->regs->status,
Benjamin Herrenschmidt083522d2006-12-15 06:54:08 +1100206 STS_HALT, STS_HALT, 16 * 125);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207}
208
209/* put TDI/ARC silicon into EHCI mode */
210static void tdi_reset (struct ehci_hcd *ehci)
211{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 u32 tmp;
213
Alan Sterna46af4e2012-06-25 12:19:03 -0400214 tmp = ehci_readl(ehci, &ehci->regs->usbmode);
Vladimir Barinovd23a1372007-05-23 20:07:48 +0400215 tmp |= USBMODE_CM_HC;
216 /* The default byte access to MMR space is LE after
217 * controller reset. Set the required endian mode
218 * for transfer buffers to match the host microprocessor
219 */
220 if (ehci_big_endian_mmio(ehci))
221 tmp |= USBMODE_BE;
Alan Sterna46af4e2012-06-25 12:19:03 -0400222 ehci_writel(ehci, tmp, &ehci->regs->usbmode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223}
224
Alan Sternc4f34762012-07-11 11:23:10 -0400225/*
226 * Reset a non-running (STS_HALT == 1) controller.
227 * Must be called with interrupts enabled and the lock not held.
228 */
Ramneek Mehresh74db22c2015-05-29 11:28:30 +0530229int ehci_reset(struct ehci_hcd *ehci)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230{
231 int retval;
Benjamin Herrenschmidt083522d2006-12-15 06:54:08 +1100232 u32 command = ehci_readl(ehci, &ehci->regs->command);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233
Jason Wessel8d053c72009-08-20 15:39:54 -0500234 /* If the EHCI debug controller is active, special care must be
235 * taken before and after a host controller reset */
Jan Beulich9fa57802012-09-18 12:23:02 +0100236 if (ehci->debug && !dbgp_reset_prep(ehci_to_hcd(ehci)))
Jason Wessel8d053c72009-08-20 15:39:54 -0500237 ehci->debug = NULL;
238
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 command |= CMD_RESET;
240 dbg_cmd (ehci, "reset", command);
Benjamin Herrenschmidt083522d2006-12-15 06:54:08 +1100241 ehci_writel(ehci, command, &ehci->regs->command);
Alan Sterne8799902011-08-18 16:31:30 -0400242 ehci->rh_state = EHCI_RH_HALTED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 ehci->next_statechange = jiffies;
Manjunath Goudar2f3a6b82013-06-13 11:24:09 -0600244 retval = ehci_handshake(ehci, &ehci->regs->command,
Benjamin Herrenschmidt083522d2006-12-15 06:54:08 +1100245 CMD_RESET, 0, 250 * 1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246
Alek Du331ac6b2009-07-13 12:41:20 +0800247 if (ehci->has_hostpc) {
248 ehci_writel(ehci, USBMODE_EX_HC | USBMODE_EX_VBPS,
Alan Sterna46af4e2012-06-25 12:19:03 -0400249 &ehci->regs->usbmode_ex);
250 ehci_writel(ehci, TXFIFO_DEFAULT, &ehci->regs->txfill_tuning);
Alek Du331ac6b2009-07-13 12:41:20 +0800251 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 if (retval)
253 return retval;
254
255 if (ehci_is_TDI(ehci))
256 tdi_reset (ehci);
257
Jason Wessel8d053c72009-08-20 15:39:54 -0500258 if (ehci->debug)
Jan Beulich9fa57802012-09-18 12:23:02 +0100259 dbgp_external_startup(ehci_to_hcd(ehci));
Jason Wessel8d053c72009-08-20 15:39:54 -0500260
Alan Sterna448e4d2012-04-03 15:24:30 -0400261 ehci->port_c_suspend = ehci->suspended_ports =
262 ehci->resuming_ports = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 return retval;
264}
Ramneek Mehresh74db22c2015-05-29 11:28:30 +0530265EXPORT_SYMBOL_GPL(ehci_reset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266
Alan Sternc4f34762012-07-11 11:23:10 -0400267/*
268 * Idle the controller (turn off the schedules).
269 * Must be called with interrupts enabled and the lock not held.
270 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271static void ehci_quiesce (struct ehci_hcd *ehci)
272{
273 u32 temp;
274
Alan Sterne8799902011-08-18 16:31:30 -0400275 if (ehci->rh_state != EHCI_RH_RUNNING)
Alan Sternc0c53db2012-07-11 11:21:48 -0400276 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277
278 /* wait for any schedule enables/disables to take effect */
Alan Stern3d9545c2012-04-23 13:54:36 -0400279 temp = (ehci->command << 10) & (STS_ASS | STS_PSS);
Manjunath Goudar2f3a6b82013-06-13 11:24:09 -0600280 ehci_handshake(ehci, &ehci->regs->status, STS_ASS | STS_PSS, temp,
281 16 * 125);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282
283 /* then disable anything that's still active */
Alan Sternc4f34762012-07-11 11:23:10 -0400284 spin_lock_irq(&ehci->lock);
Alan Stern3d9545c2012-04-23 13:54:36 -0400285 ehci->command &= ~(CMD_ASE | CMD_PSE);
286 ehci_writel(ehci, ehci->command, &ehci->regs->command);
Alan Sternc4f34762012-07-11 11:23:10 -0400287 spin_unlock_irq(&ehci->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288
289 /* hardware can take 16 microframes to turn off ... */
Manjunath Goudar2f3a6b82013-06-13 11:24:09 -0600290 ehci_handshake(ehci, &ehci->regs->status, STS_ASS | STS_PSS, 0,
291 16 * 125);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292}
293
294/*-------------------------------------------------------------------------*/
295
Alan Sternf96fba02016-01-25 15:44:16 -0500296static void end_iaa_cycle(struct ehci_hcd *ehci);
Alan Stern07d29b62007-12-11 16:05:30 -0500297static void end_unlink_async(struct ehci_hcd *ehci);
Alan Stern32830f22012-07-11 11:22:53 -0400298static void unlink_empty_async(struct ehci_hcd *ehci);
David Howells7d12e782006-10-05 14:55:46 +0100299static void ehci_work(struct ehci_hcd *ehci);
Alan Sterndf202252012-07-11 11:22:26 -0400300static void start_unlink_intr(struct ehci_hcd *ehci, struct ehci_qh *qh);
301static void end_unlink_intr(struct ehci_hcd *ehci, struct ehci_qh *qh);
Michael Grzeschik11a7e592014-10-13 09:53:03 +0800302static int ehci_port_power(struct ehci_hcd *ehci, int portnum, bool enable);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303
Alan Sternd58b4bc2012-07-11 11:21:54 -0400304#include "ehci-timer.c"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305#include "ehci-hub.c"
306#include "ehci-mem.c"
307#include "ehci-q.c"
308#include "ehci-sched.c"
Kirill Smelkov4c670452011-07-03 20:36:56 +0400309#include "ehci-sysfs.c"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310
311/*-------------------------------------------------------------------------*/
312
Alan Stern89037952007-02-13 14:55:27 -0500313/* On some systems, leaving remote wakeup enabled prevents system shutdown.
314 * The firmware seems to think that powering off is a wakeup event!
315 * This routine turns off remote wakeup and everything else, on all ports.
316 */
317static void ehci_turn_off_all_ports(struct ehci_hcd *ehci)
318{
319 int port = HCS_N_PORTS(ehci->hcs_params);
320
Michael Grzeschik11a7e592014-10-13 09:53:03 +0800321 while (port--) {
Michael Grzeschik11a7e592014-10-13 09:53:03 +0800322 spin_unlock_irq(&ehci->lock);
323 ehci_port_power(ehci, port, false);
324 spin_lock_irq(&ehci->lock);
Marc Ohlfbc337b52016-08-03 11:51:54 +0200325 ehci_writel(ehci, PORT_RWC_BITS,
326 &ehci->regs->port_status[port]);
Michael Grzeschik11a7e592014-10-13 09:53:03 +0800327 }
Alan Stern89037952007-02-13 14:55:27 -0500328}
329
Sarah Sharp21da84a2008-04-08 14:30:18 -0700330/*
331 * Halt HC, turn off all ports, and let the BIOS use the companion controllers.
Alan Sternc4f34762012-07-11 11:23:10 -0400332 * Must be called with interrupts enabled and the lock not held.
David Brownell72f30b62005-09-27 10:19:39 -0700333 */
Sarah Sharp21da84a2008-04-08 14:30:18 -0700334static void ehci_silence_controller(struct ehci_hcd *ehci)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335{
Sarah Sharp21da84a2008-04-08 14:30:18 -0700336 ehci_halt(ehci);
Alan Sternc4f34762012-07-11 11:23:10 -0400337
338 spin_lock_irq(&ehci->lock);
339 ehci->rh_state = EHCI_RH_HALTED;
Alan Stern89037952007-02-13 14:55:27 -0500340 ehci_turn_off_all_ports(ehci);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341
342 /* make BIOS/etc use companion controller during reboot */
Benjamin Herrenschmidt083522d2006-12-15 06:54:08 +1100343 ehci_writel(ehci, 0, &ehci->regs->configured_flag);
Alan Stern89037952007-02-13 14:55:27 -0500344
345 /* unblock posted writes */
346 ehci_readl(ehci, &ehci->regs->configured_flag);
Alan Sternc4f34762012-07-11 11:23:10 -0400347 spin_unlock_irq(&ehci->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348}
349
Sarah Sharp21da84a2008-04-08 14:30:18 -0700350/* ehci_shutdown kick in for silicon on any bus (not just pci, etc).
351 * This forcibly disables dma and IRQs, helping kexec and other cases
352 * where the next system software may expect clean state.
353 */
354static void ehci_shutdown(struct usb_hcd *hcd)
355{
356 struct ehci_hcd *ehci = hcd_to_ehci(hcd);
357
Srinivas Kandagatla11c011a2016-05-19 11:12:56 +0100358 /**
359 * Protect the system from crashing at system shutdown in cases where
360 * usb host is not added yet from OTG controller driver.
361 * As ehci_setup() not done yet, so stop accessing registers or
362 * variables initialized in ehci_setup()
363 */
364 if (!ehci->sbrn)
365 return;
366
Sarah Sharp21da84a2008-04-08 14:30:18 -0700367 spin_lock_irq(&ehci->lock);
Alan Stern43fe3a92012-07-11 11:23:16 -0400368 ehci->shutdown = true;
Alan Sternc0c53db2012-07-11 11:21:48 -0400369 ehci->rh_state = EHCI_RH_STOPPING;
Alan Sternd58b4bc2012-07-11 11:21:54 -0400370 ehci->enabled_hrtimer_events = 0;
Sarah Sharp21da84a2008-04-08 14:30:18 -0700371 spin_unlock_irq(&ehci->lock);
Alan Sternd58b4bc2012-07-11 11:21:54 -0400372
Alan Sternc4f34762012-07-11 11:23:10 -0400373 ehci_silence_controller(ehci);
374
Alan Sternd58b4bc2012-07-11 11:21:54 -0400375 hrtimer_cancel(&ehci->hrtimer);
Sarah Sharp21da84a2008-04-08 14:30:18 -0700376}
377
Matt Porter7ff71d62005-09-22 22:31:15 -0700378/*-------------------------------------------------------------------------*/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379
Matt Porter7ff71d62005-09-22 22:31:15 -0700380/*
381 * ehci_work is called from some interrupts, timers, and so on.
382 * it calls driver completion functions, after dropping ehci->lock.
383 */
David Howells7d12e782006-10-05 14:55:46 +0100384static void ehci_work (struct ehci_hcd *ehci)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385{
Matt Porter7ff71d62005-09-22 22:31:15 -0700386 /* another CPU may drop ehci->lock during a schedule scan while
387 * it reports urb completions. this flag guards against bogus
388 * attempts at re-entrant schedule scanning.
389 */
Alan Stern361aabf2012-07-11 11:22:57 -0400390 if (ehci->scanning) {
391 ehci->need_rescan = true;
Matt Porter7ff71d62005-09-22 22:31:15 -0700392 return;
Alan Stern361aabf2012-07-11 11:22:57 -0400393 }
394 ehci->scanning = true;
395
396 rescan:
397 ehci->need_rescan = false;
Alan Stern31446612012-07-11 11:22:21 -0400398 if (ehci->async_count)
399 scan_async(ehci);
Alan Stern569b3942012-07-11 11:23:00 -0400400 if (ehci->intr_count > 0)
401 scan_intr(ehci);
402 if (ehci->isoc_count > 0)
403 scan_isoc(ehci);
Alan Stern361aabf2012-07-11 11:22:57 -0400404 if (ehci->need_rescan)
405 goto rescan;
406 ehci->scanning = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407
Matt Porter7ff71d62005-09-22 22:31:15 -0700408 /* the IO watchdog guards against hardware or driver bugs that
409 * misplace IRQs, and should let us run completely without IRQs.
410 * such lossage has been observed on both VT6202 and VT8235.
411 */
Alan Stern18aafe62012-07-11 11:23:04 -0400412 turn_on_io_watchdog(ehci);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413}
414
Sarah Sharp21da84a2008-04-08 14:30:18 -0700415/*
416 * Called when the ehci_hcd module is removed.
417 */
Matt Porter7ff71d62005-09-22 22:31:15 -0700418static void ehci_stop (struct usb_hcd *hcd)
419{
420 struct ehci_hcd *ehci = hcd_to_ehci (hcd);
421
422 ehci_dbg (ehci, "stop\n");
423
Matt Porter7ff71d62005-09-22 22:31:15 -0700424 /* no more interrupts ... */
Matt Porter7ff71d62005-09-22 22:31:15 -0700425
426 spin_lock_irq(&ehci->lock);
Alan Sternd58b4bc2012-07-11 11:21:54 -0400427 ehci->enabled_hrtimer_events = 0;
Matt Porter7ff71d62005-09-22 22:31:15 -0700428 spin_unlock_irq(&ehci->lock);
429
Alan Sternc4f34762012-07-11 11:23:10 -0400430 ehci_quiesce(ehci);
Matt Porter7ff71d62005-09-22 22:31:15 -0700431 ehci_silence_controller(ehci);
432 ehci_reset (ehci);
Matt Porter7ff71d62005-09-22 22:31:15 -0700433
Alan Sternd58b4bc2012-07-11 11:21:54 -0400434 hrtimer_cancel(&ehci->hrtimer);
Kirill Smelkov4c670452011-07-03 20:36:56 +0400435 remove_sysfs_files(ehci);
Matt Porter7ff71d62005-09-22 22:31:15 -0700436 remove_debug_files (ehci);
437
438 /* root hub is shut down separately (first, when possible) */
439 spin_lock_irq (&ehci->lock);
Alan Stern55934eb2012-07-11 11:22:35 -0400440 end_free_itds(ehci);
Matt Porter7ff71d62005-09-22 22:31:15 -0700441 spin_unlock_irq (&ehci->lock);
442 ehci_mem_cleanup (ehci);
443
Andiry Xuad935622011-03-01 14:57:05 +0800444 if (ehci->amd_pll_fix == 1)
445 usb_amd_dev_put();
Alex He05570292010-12-07 10:10:08 +0800446
Benjamin Herrenschmidt083522d2006-12-15 06:54:08 +1100447 dbg_status (ehci, "ehci_stop completed",
448 ehci_readl(ehci, &ehci->regs->status));
Matt Porter7ff71d62005-09-22 22:31:15 -0700449}
450
David Brownell18807522005-11-23 15:45:37 -0800451/* one-time init, only for memory state */
452static int ehci_init(struct usb_hcd *hcd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453{
David Brownell18807522005-11-23 15:45:37 -0800454 struct ehci_hcd *ehci = hcd_to_ehci(hcd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 u32 temp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 int retval;
457 u32 hcc_params;
Alek Du3807e262009-07-14 07:23:29 +0800458 struct ehci_qh_hw *hw;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459
David Brownell18807522005-11-23 15:45:37 -0800460 spin_lock_init(&ehci->lock);
461
Alek Du403dbd32009-07-13 17:30:41 +0800462 /*
463 * keep io watchdog by default, those good HCDs could turn off it later
464 */
465 ehci->need_io_watchdog = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466
Alan Sternd58b4bc2012-07-11 11:21:54 -0400467 hrtimer_init(&ehci->hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
468 ehci->hrtimer.function = ehci_hrtimer_func;
469 ehci->next_hrtimer_event = EHCI_HRTIMER_NO_EVENT;
Alan Stern07d29b62007-12-11 16:05:30 -0500470
Alan Sternf75593c2011-01-06 10:17:09 -0500471 hcc_params = ehci_readl(ehci, &ehci->caps->hcc_params);
472
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 /*
Kirill Smelkovcc62a7e2011-07-03 20:36:57 +0400474 * by default set standard 80% (== 100 usec/uframe) max periodic
475 * bandwidth as required by USB 2.0
476 */
477 ehci->uframe_periodic_max = 100;
478
479 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 * hw default: 1K periodic list heads, one per frame.
481 * periodic_size can shrink by USBCMD update if hcc_params allows.
482 */
483 ehci->periodic_size = DEFAULT_I_TDPS;
Alan Stern6e018752013-03-22 13:31:45 -0400484 INIT_LIST_HEAD(&ehci->async_unlink);
Alan Stern214ac7a2013-03-22 13:31:58 -0400485 INIT_LIST_HEAD(&ehci->async_idle);
Ming Lei9118f9e2013-07-03 22:53:10 +0800486 INIT_LIST_HEAD(&ehci->intr_unlink_wait);
Alan Stern6e018752013-03-22 13:31:45 -0400487 INIT_LIST_HEAD(&ehci->intr_unlink);
Alan Stern569b3942012-07-11 11:23:00 -0400488 INIT_LIST_HEAD(&ehci->intr_qh_list);
Karsten Wiese9aa09d22009-02-08 16:07:58 -0800489 INIT_LIST_HEAD(&ehci->cached_itd_list);
Alan Stern0e5f2312010-04-08 16:56:37 -0400490 INIT_LIST_HEAD(&ehci->cached_sitd_list);
Alan Sternb35c5002013-10-11 22:16:21 -0400491 INIT_LIST_HEAD(&ehci->tt_list);
Alan Sternf75593c2011-01-06 10:17:09 -0500492
Greg Kroah-Hartman8e192912012-05-21 08:54:43 -0700493 if (HCC_PGM_FRAMELISTLEN(hcc_params)) {
Alan Sternf75593c2011-01-06 10:17:09 -0500494 /* periodic schedule size can be smaller than default */
495 switch (EHCI_TUNE_FLS) {
496 case 0: ehci->periodic_size = 1024; break;
497 case 1: ehci->periodic_size = 512; break;
498 case 2: ehci->periodic_size = 256; break;
499 default: BUG();
500 }
501 }
David Brownell18807522005-11-23 15:45:37 -0800502 if ((retval = ehci_mem_init(ehci, GFP_KERNEL)) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 return retval;
504
505 /* controllers may cache some of the periodic schedule ... */
David Brownell53bd6a62006-08-30 14:50:06 -0700506 if (HCC_ISOC_CACHE(hcc_params)) // full frame cache
Alan Stern98cae422012-09-28 16:01:34 -0400507 ehci->i_thresh = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 else // N microframes cached
David Brownell18807522005-11-23 15:45:37 -0800509 ehci->i_thresh = 2 + HCC_ISOC_THRES(hcc_params);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 /*
512 * dedicate a qh for the async ring head, since we couldn't unlink
513 * a 'real' qh without stopping the async schedule [4.8]. use it
514 * as the 'reclamation list head' too.
515 * its dummy is used in hw_alt_next of many tds, to prevent the qh
516 * from automatically advancing to the next td after short reads.
517 */
David Brownell18807522005-11-23 15:45:37 -0800518 ehci->async->qh_next.qh = NULL;
Alek Du3807e262009-07-14 07:23:29 +0800519 hw = ehci->async->hw;
520 hw->hw_next = QH_NEXT(ehci, ehci->async->qh_dma);
521 hw->hw_info1 = cpu_to_hc32(ehci, QH_HEAD);
Ricardo Martins4f7a67e2012-05-22 18:02:03 +0100522#if defined(CONFIG_PPC_PS3)
Alan Stern4c53de72012-07-11 11:21:32 -0400523 hw->hw_info1 |= cpu_to_hc32(ehci, QH_INACTIVATE);
Ricardo Martins4f7a67e2012-05-22 18:02:03 +0100524#endif
Alek Du3807e262009-07-14 07:23:29 +0800525 hw->hw_token = cpu_to_hc32(ehci, QTD_STS_HALT);
526 hw->hw_qtd_next = EHCI_LIST_END(ehci);
David Brownell18807522005-11-23 15:45:37 -0800527 ehci->async->qh_state = QH_STATE_LINKED;
Alek Du3807e262009-07-14 07:23:29 +0800528 hw->hw_alt_next = QTD_NEXT(ehci, ehci->async->dummy->qtd_dma);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529
530 /* clear interrupt enables, set irq latency */
531 if (log2_irq_thresh < 0 || log2_irq_thresh > 6)
532 log2_irq_thresh = 0;
533 temp = 1 << (16 + log2_irq_thresh);
Alek Du5a9cdf32010-06-04 15:47:56 +0800534 if (HCC_PER_PORT_CHANGE_EVENT(hcc_params)) {
535 ehci->has_ppcd = 1;
536 ehci_dbg(ehci, "enable per-port change event\n");
537 temp |= CMD_PPCEE;
538 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 if (HCC_CANPARK(hcc_params)) {
540 /* HW default park == 3, on hardware that supports it (like
541 * NVidia and ALI silicon), maximizes throughput on the async
542 * schedule by avoiding QH fetches between transfers.
543 *
544 * With fast usb storage devices and NForce2, "park" seems to
545 * make problems: throughput reduction (!), data errors...
546 */
547 if (park) {
David Brownell18807522005-11-23 15:45:37 -0800548 park = min(park, (unsigned) 3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 temp |= CMD_PARK;
550 temp |= park << 8;
551 }
David Brownell18807522005-11-23 15:45:37 -0800552 ehci_dbg(ehci, "park %d\n", park);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 }
David Brownell18807522005-11-23 15:45:37 -0800554 if (HCC_PGM_FRAMELISTLEN(hcc_params)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 /* periodic schedule size can be smaller than default */
556 temp &= ~(3 << 2);
557 temp |= (EHCI_TUNE_FLS << 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 }
David Brownell18807522005-11-23 15:45:37 -0800559 ehci->command = temp;
560
Alan Stern40f8db82009-11-06 12:29:40 -0500561 /* Accept arbitrarily long scatter-gather lists */
Andrea Righi4307a282010-06-28 16:56:45 +0200562 if (!(hcd->driver->flags & HCD_LOCAL_MEM))
563 hcd->self.sg_tablesize = ~0;
Alan Stern87d61912016-01-25 15:45:25 -0500564
565 /* Prepare for unlinking active QHs */
566 ehci->old_current = ~0;
David Brownell18807522005-11-23 15:45:37 -0800567 return 0;
568}
569
570/* start HC running; it's halted, ehci_init() has been run (once) */
571static int ehci_run (struct usb_hcd *hcd)
572{
573 struct ehci_hcd *ehci = hcd_to_ehci (hcd);
David Brownell18807522005-11-23 15:45:37 -0800574 u32 temp;
575 u32 hcc_params;
576
Marcelo Tosatti1d619f12007-01-21 19:45:59 -0200577 hcd->uses_new_polling = 1;
Marcelo Tosatti1d619f12007-01-21 19:45:59 -0200578
David Brownell18807522005-11-23 15:45:37 -0800579 /* EHCI spec section 4.1 */
Geoff Levand876e0df2011-11-22 18:04:45 -0800580
Benjamin Herrenschmidt083522d2006-12-15 06:54:08 +1100581 ehci_writel(ehci, ehci->periodic_dma, &ehci->regs->frame_list);
582 ehci_writel(ehci, (u32)ehci->async->qh_dma, &ehci->regs->async_next);
David Brownell18807522005-11-23 15:45:37 -0800583
584 /*
585 * hcc_params controls whether ehci->regs->segment must (!!!)
586 * be used; it constrains QH/ITD/SITD and QTD locations.
Romain Perier324c54f2017-03-08 17:19:56 +0100587 * dma_pool consistent memory always uses segment zero.
David Brownell18807522005-11-23 15:45:37 -0800588 * streaming mappings for I/O buffers, like pci_map_single(),
589 * can return segments above 4GB, if the device allows.
590 *
Christoph Hellwig0e77ace2015-11-09 14:58:18 -0800591 * NOTE: the dma mask is visible through dev->dma_mask, so
David Brownell18807522005-11-23 15:45:37 -0800592 * drivers can pass this info along ... like NETIF_F_HIGHDMA,
593 * Scsi_Host.highmem_io, and so forth. It's readonly to all
594 * host side drivers though.
595 */
Benjamin Herrenschmidt083522d2006-12-15 06:54:08 +1100596 hcc_params = ehci_readl(ehci, &ehci->caps->hcc_params);
David Brownell18807522005-11-23 15:45:37 -0800597 if (HCC_64BIT_ADDR(hcc_params)) {
Benjamin Herrenschmidt083522d2006-12-15 06:54:08 +1100598 ehci_writel(ehci, 0, &ehci->regs->segment);
David Brownell18807522005-11-23 15:45:37 -0800599#if 0
600// this is deeply broken on almost all architectures
Yang Hongyang6a355282009-04-06 19:01:13 -0700601 if (!dma_set_mask(hcd->self.controller, DMA_BIT_MASK(64)))
David Brownell18807522005-11-23 15:45:37 -0800602 ehci_info(ehci, "enabled 64bit DMA\n");
603#endif
604 }
605
606
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 // Philips, Intel, and maybe others need CMD_RUN before the
608 // root hub will detect new devices (why?); NEC doesn't
David Brownell18807522005-11-23 15:45:37 -0800609 ehci->command &= ~(CMD_LRESET|CMD_IAAD|CMD_PSE|CMD_ASE|CMD_RESET);
610 ehci->command |= CMD_RUN;
Benjamin Herrenschmidt083522d2006-12-15 06:54:08 +1100611 ehci_writel(ehci, ehci->command, &ehci->regs->command);
David Brownell18807522005-11-23 15:45:37 -0800612 dbg_cmd (ehci, "init", ehci->command);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 /*
615 * Start, enabling full USB 2.0 functionality ... usb 1.1 devices
616 * are explicitly handed to companion controller(s), so no TT is
617 * involved with the root hub. (Except where one is integrated,
618 * and there's no companion controller unless maybe for USB OTG.)
Alan Stern32fe0192007-10-10 16:27:07 -0400619 *
620 * Turning on the CF flag will transfer ownership of all ports
621 * from the companions to the EHCI controller. If any of the
622 * companions are in the middle of a port reset at the time, it
623 * could cause trouble. Write-locking ehci_cf_port_reset_rwsem
David Brownell1cb52652007-11-13 16:22:30 -0800624 * guarantees that no resets are in progress. After we set CF,
625 * a short delay lets the hardware catch up; new resets shouldn't
626 * be started before the port switching actions could complete.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 */
Alan Stern32fe0192007-10-10 16:27:07 -0400628 down_write(&ehci_cf_port_reset_rwsem);
Alan Sterne8799902011-08-18 16:31:30 -0400629 ehci->rh_state = EHCI_RH_RUNNING;
Benjamin Herrenschmidt083522d2006-12-15 06:54:08 +1100630 ehci_writel(ehci, FLAG_CF, &ehci->regs->configured_flag);
631 ehci_readl(ehci, &ehci->regs->command); /* unblock posted writes */
David Brownell1cb52652007-11-13 16:22:30 -0800632 msleep(5);
Alan Stern32fe0192007-10-10 16:27:07 -0400633 up_write(&ehci_cf_port_reset_rwsem);
Oliver Neukumee4ecb82009-11-27 15:17:59 +0100634 ehci->last_periodic_enable = ktime_get_real();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635
Jan Anderssonc4301312011-05-03 20:11:57 +0200636 temp = HC_VERSION(ehci, ehci_readl(ehci, &ehci->caps->hc_capbase));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 ehci_info (ehci,
Alan Stern2b70f072008-10-02 11:47:15 -0400638 "USB %x.%x started, EHCI %x.%02x%s\n",
Matt Porter7ff71d62005-09-22 22:31:15 -0700639 ((ehci->sbrn & 0xf0)>>4), (ehci->sbrn & 0x0f),
Alan Stern2b70f072008-10-02 11:47:15 -0400640 temp >> 8, temp & 0xff,
David Brownell93f1a472006-11-16 23:34:58 -0800641 ignore_oc ? ", overcurrent ignored" : "");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642
Benjamin Herrenschmidt083522d2006-12-15 06:54:08 +1100643 ehci_writel(ehci, INTR_MASK,
644 &ehci->regs->intr_enable); /* Turn On Interrupts */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645
David Brownell18807522005-11-23 15:45:37 -0800646 /* GRR this is run-once init(), being done every time the HC starts.
647 * So long as they're part of class devices, we can't do it init()
648 * since the class device isn't created that early.
649 */
650 create_debug_files(ehci);
Kirill Smelkov4c670452011-07-03 20:36:56 +0400651 create_sysfs_files(ehci);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652
653 return 0;
654}
655
Alan Stern3e023202012-11-01 11:12:58 -0400656int ehci_setup(struct usb_hcd *hcd)
Matthieu CASTET2093c6b2011-07-02 19:47:33 +0200657{
658 struct ehci_hcd *ehci = hcd_to_ehci(hcd);
659 int retval;
660
661 ehci->regs = (void __iomem *)ehci->caps +
662 HC_LENGTH(ehci, ehci_readl(ehci, &ehci->caps->hc_capbase));
663 dbg_hcs_params(ehci, "reset");
664 dbg_hcc_params(ehci, "reset");
665
666 /* cache this readonly data; minimize chip reads */
667 ehci->hcs_params = ehci_readl(ehci, &ehci->caps->hcs_params);
668
669 ehci->sbrn = HCD_USB2;
670
Matthieu CASTET2093c6b2011-07-02 19:47:33 +0200671 /* data structure init */
672 retval = ehci_init(hcd);
673 if (retval)
674 return retval;
675
Alan Stern631fe9d2012-07-11 11:21:09 -0400676 retval = ehci_halt(ehci);
Jia-Ju Baid8ff4632016-01-04 16:14:43 +0800677 if (retval) {
678 ehci_mem_cleanup(ehci);
Matthieu CASTET2093c6b2011-07-02 19:47:33 +0200679 return retval;
Jia-Ju Baid8ff4632016-01-04 16:14:43 +0800680 }
Matthieu CASTET2093c6b2011-07-02 19:47:33 +0200681
682 ehci_reset(ehci);
683
684 return 0;
685}
Alan Stern3e023202012-11-01 11:12:58 -0400686EXPORT_SYMBOL_GPL(ehci_setup);
Matthieu CASTET2093c6b2011-07-02 19:47:33 +0200687
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688/*-------------------------------------------------------------------------*/
689
David Howells7d12e782006-10-05 14:55:46 +0100690static irqreturn_t ehci_irq (struct usb_hcd *hcd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691{
692 struct ehci_hcd *ehci = hcd_to_ehci (hcd);
Alan Stern67b2e022008-11-12 17:04:53 -0500693 u32 status, masked_status, pcd_status = 0, cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 int bh;
Stanislaw Gruszkaa1227f32014-02-19 10:29:01 +0100695 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696
Stanislaw Gruszkaa1227f32014-02-19 10:29:01 +0100697 /*
698 * For threadirqs option we use spin_lock_irqsave() variant to prevent
699 * deadlock with ehci hrtimer callback, because hrtimer callbacks run
700 * in interrupt context even when threadirqs is specified. We can go
701 * back to spin_lock() variant when hrtimer callbacks become threaded.
702 */
703 spin_lock_irqsave(&ehci->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704
Benjamin Herrenschmidt083522d2006-12-15 06:54:08 +1100705 status = ehci_readl(ehci, &ehci->regs->status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706
707 /* e.g. cardbus physical eject */
708 if (status == ~(u32) 0) {
709 ehci_dbg (ehci, "device removed\n");
710 goto dead;
711 }
712
Alan Stern2fbe2bf2012-04-18 11:33:00 -0400713 /*
714 * We don't use STS_FLR, but some controllers don't like it to
715 * remain on, so mask it out along with the other status bits.
716 */
717 masked_status = status & (INTR_MASK | STS_FLR);
718
Alan Stern69fff592011-05-17 17:27:12 -0400719 /* Shared IRQ? */
Alan Sterne8799902011-08-18 16:31:30 -0400720 if (!masked_status || unlikely(ehci->rh_state == EHCI_RH_HALTED)) {
Stanislaw Gruszkaa1227f32014-02-19 10:29:01 +0100721 spin_unlock_irqrestore(&ehci->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 return IRQ_NONE;
723 }
724
725 /* clear (just) interrupts */
Alan Stern67b2e022008-11-12 17:04:53 -0500726 ehci_writel(ehci, masked_status, &ehci->regs->status);
David Brownelle82cc122008-03-07 13:49:42 -0800727 cmd = ehci_readl(ehci, &ehci->regs->command);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 bh = 0;
729
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 /* normal [4.15.1.2] or error [4.15.1.1] completion */
731 if (likely ((status & (STS_INT|STS_ERR)) != 0)) {
732 if (likely ((status & STS_ERR) == 0))
733 COUNT (ehci->stats.normal);
734 else
735 COUNT (ehci->stats.error);
736 bh = 1;
737 }
738
739 /* complete the unlinking of some qh [4.15.2.3] */
740 if (status & STS_IAA) {
Alan Stern9d938742012-07-11 11:22:44 -0400741
742 /* Turn off the IAA watchdog */
743 ehci->enabled_hrtimer_events &= ~BIT(EHCI_HRTIMER_IAA_WATCHDOG);
744
745 /*
746 * Mild optimization: Allow another IAAD to reset the
747 * hrtimer, if one occurs before the next expiration.
748 * In theory we could always cancel the hrtimer, but
749 * tests show that about half the time it will be reset
750 * for some other event anyway.
751 */
752 if (ehci->next_hrtimer_event == EHCI_HRTIMER_IAA_WATCHDOG)
753 ++ehci->next_hrtimer_event;
754
David Brownelle82cc122008-03-07 13:49:42 -0800755 /* guard against (alleged) silicon errata */
Alan Stern6feff1b2012-04-17 15:23:25 -0400756 if (cmd & CMD_IAAD)
David Brownelle82cc122008-03-07 13:49:42 -0800757 ehci_dbg(ehci, "IAA with IAAD still set?\n");
Alan Stern214ac7a2013-03-22 13:31:58 -0400758 if (ehci->iaa_in_progress)
Alan Stern99ac5b12012-07-11 11:21:38 -0400759 COUNT(ehci->stats.iaa);
Alan Sternf96fba02016-01-25 15:44:16 -0500760 end_iaa_cycle(ehci);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 }
762
763 /* remote wakeup [4.3.1] */
David Brownelld97cc2f2005-12-22 17:05:18 -0800764 if (status & STS_PCD) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 unsigned i = HCS_N_PORTS (ehci->hcs_params);
Alan Stern4dd405a2013-03-18 12:05:08 -0400766 u32 ppcd = ~0;
David Brownelld1b18422008-03-05 23:37:52 -0800767
768 /* kick root hub later */
Marcelo Tosatti1d619f12007-01-21 19:45:59 -0200769 pcd_status = status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770
771 /* resume root hub? */
Alan Sterndc75ce92012-04-17 15:24:15 -0400772 if (ehci->rh_state == EHCI_RH_SUSPENDED)
Alan Stern8c033562006-11-09 14:42:16 -0500773 usb_hcd_resume_root_hub(hcd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774
Alek Du5a9cdf32010-06-04 15:47:56 +0800775 /* get per-port change detect bits */
776 if (ehci->has_ppcd)
777 ppcd = status >> 16;
778
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 while (i--) {
Alek Du5a9cdf32010-06-04 15:47:56 +0800780 int pstatus;
781
782 /* leverage per-port change bits feature */
Alan Stern4dd405a2013-03-18 12:05:08 -0400783 if (!(ppcd & (1 << i)))
Alek Du5a9cdf32010-06-04 15:47:56 +0800784 continue;
785 pstatus = ehci_readl(ehci,
786 &ehci->regs->port_status[i]);
David Brownellb972b682006-06-30 02:34:42 -0700787
788 if (pstatus & PORT_OWNER)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 continue;
Alan Sterneafe5b92008-10-06 11:25:53 -0400790 if (!(test_bit(i, &ehci->suspended_ports) &&
791 ((pstatus & PORT_RESUME) ||
792 !(pstatus & PORT_SUSPEND)) &&
793 (pstatus & PORT_PE) &&
794 ehci->reset_done[i] == 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 continue;
796
Felipe Balbiea163282015-02-13 14:42:25 -0600797 /* start USB_RESUME_TIMEOUT msec resume signaling from
798 * this port, and make hub_wq collect
799 * PORT_STAT_C_SUSPEND to stop that signaling.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 */
Felipe Balbiea163282015-02-13 14:42:25 -0600801 ehci->reset_done[i] = jiffies +
802 msecs_to_jiffies(USB_RESUME_TIMEOUT);
Alan Sterna448e4d2012-04-03 15:24:30 -0400803 set_bit(i, &ehci->resuming_ports);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 ehci_dbg (ehci, "port %d remote wakeup\n", i + 1);
Alan Sternf292e7f2013-01-25 17:09:49 -0500805 usb_hcd_start_port_resume(&hcd->self, i);
Alan Stern61e8b852007-04-09 11:52:31 -0400806 mod_timer(&hcd->rh_timer, ehci->reset_done[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 }
808 }
809
810 /* PCI errors [4.15.2.4] */
811 if (unlikely ((status & STS_FATAL) != 0)) {
Alan Stern67b2e022008-11-12 17:04:53 -0500812 ehci_err(ehci, "fatal error\n");
Alan Sterneafe5b92008-10-06 11:25:53 -0400813 dbg_cmd(ehci, "fatal", cmd);
814 dbg_status(ehci, "fatal", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815dead:
Alan Stern69fff592011-05-17 17:27:12 -0400816 usb_hc_died(hcd);
Alan Sternbf6387b2012-07-11 11:22:31 -0400817
818 /* Don't let the controller do anything more */
Alan Stern43fe3a92012-07-11 11:23:16 -0400819 ehci->shutdown = true;
Alan Sternbf6387b2012-07-11 11:22:31 -0400820 ehci->rh_state = EHCI_RH_STOPPING;
821 ehci->command &= ~(CMD_RUN | CMD_ASE | CMD_PSE);
822 ehci_writel(ehci, ehci->command, &ehci->regs->command);
823 ehci_writel(ehci, 0, &ehci->regs->intr_enable);
824 ehci_handle_controller_death(ehci);
825
826 /* Handle completions when the controller stops */
827 bh = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 }
829
830 if (bh)
David Howells7d12e782006-10-05 14:55:46 +0100831 ehci_work (ehci);
Stanislaw Gruszkaa1227f32014-02-19 10:29:01 +0100832 spin_unlock_irqrestore(&ehci->lock, flags);
David Brownelld1b18422008-03-05 23:37:52 -0800833 if (pcd_status)
Marcelo Tosatti1d619f12007-01-21 19:45:59 -0200834 usb_hcd_poll_rh_status(hcd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 return IRQ_HANDLED;
836}
837
838/*-------------------------------------------------------------------------*/
839
840/*
841 * non-error returns are a promise to giveback() the urb later
842 * we drop ownership so next owner (or urb unlink) can get it
843 *
844 * urb + dev is in hcd.self.controller.urb_list
845 * we're queueing TDs onto software and hardware lists
846 *
847 * hcd-specific init for hcpriv hasn't been done yet
848 *
849 * NOTE: control, bulk, and interrupt share the same code to append TDs
850 * to a (possibly active) QH, and the same QH scanning code.
851 */
852static int ehci_urb_enqueue (
853 struct usb_hcd *hcd,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 struct urb *urb,
Al Viro55016f12005-10-21 03:21:58 -0400855 gfp_t mem_flags
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856) {
857 struct ehci_hcd *ehci = hcd_to_ehci (hcd);
858 struct list_head qtd_list;
859
860 INIT_LIST_HEAD (&qtd_list);
861
862 switch (usb_pipetype (urb->pipe)) {
David Brownell25b70a82008-03-04 15:11:07 -0800863 case PIPE_CONTROL:
864 /* qh_completions() code doesn't handle all the fault cases
865 * in multi-TD control transfers. Even 1KB is rare anyway.
866 */
867 if (urb->transfer_buffer_length > (16 * 1024))
868 return -EMSGSIZE;
869 /* FALLTHROUGH */
870 /* case PIPE_BULK: */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871 default:
872 if (!qh_urb_transaction (ehci, urb, &qtd_list, mem_flags))
873 return -ENOMEM;
Alan Sterne9df41c2007-08-08 11:48:02 -0400874 return submit_async(ehci, urb, &qtd_list, mem_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875
876 case PIPE_INTERRUPT:
877 if (!qh_urb_transaction (ehci, urb, &qtd_list, mem_flags))
878 return -ENOMEM;
Alan Sterne9df41c2007-08-08 11:48:02 -0400879 return intr_submit(ehci, urb, &qtd_list, mem_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880
881 case PIPE_ISOCHRONOUS:
882 if (urb->dev->speed == USB_SPEED_HIGH)
883 return itd_submit (ehci, urb, mem_flags);
884 else
885 return sitd_submit (ehci, urb, mem_flags);
886 }
887}
888
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889/* remove from hardware lists
890 * completions normally happen asynchronously
891 */
892
Alan Sterne9df41c2007-08-08 11:48:02 -0400893static int ehci_urb_dequeue(struct usb_hcd *hcd, struct urb *urb, int status)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894{
895 struct ehci_hcd *ehci = hcd_to_ehci (hcd);
896 struct ehci_qh *qh;
897 unsigned long flags;
Alan Sterne9df41c2007-08-08 11:48:02 -0400898 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899
900 spin_lock_irqsave (&ehci->lock, flags);
Alan Sterne9df41c2007-08-08 11:48:02 -0400901 rc = usb_hcd_check_unlink_urb(hcd, urb, status);
902 if (rc)
903 goto done;
904
Alan Stern7655e312013-03-22 13:31:29 -0400905 if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS) {
906 /*
907 * We don't expedite dequeue for isochronous URBs.
908 * Just wait until they complete normally or their
909 * time slot expires.
910 */
911 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 qh = (struct ehci_qh *) urb->hcpriv;
Alan Sternfcc51842016-01-25 15:42:04 -0500913 qh->unlink_reason |= QH_UNLINK_REQUESTED;
Alan Stern07d29b62007-12-11 16:05:30 -0500914 switch (qh->qh_state) {
915 case QH_STATE_LINKED:
Alan Stern7655e312013-03-22 13:31:29 -0400916 if (usb_pipetype(urb->pipe) == PIPE_INTERRUPT)
917 start_unlink_intr(ehci, qh);
918 else
919 start_unlink_async(ehci, qh);
Alan Stern07d29b62007-12-11 16:05:30 -0500920 break;
Alan Stern7bc782d2013-03-22 13:31:11 -0400921 case QH_STATE_COMPLETING:
922 qh->dequeue_during_giveback = 1;
923 break;
Alan Stern07d29b62007-12-11 16:05:30 -0500924 case QH_STATE_UNLINK:
925 case QH_STATE_UNLINK_WAIT:
926 /* already started */
927 break;
928 case QH_STATE_IDLE:
Alan Stern7a0f0d92009-07-31 10:40:22 -0400929 /* QH might be waiting for a Clear-TT-Buffer */
930 qh_completions(ehci, qh);
Alan Stern07d29b62007-12-11 16:05:30 -0500931 break;
932 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 }
934done:
935 spin_unlock_irqrestore (&ehci->lock, flags);
Alan Sterne9df41c2007-08-08 11:48:02 -0400936 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937}
938
939/*-------------------------------------------------------------------------*/
940
941// bulk qh holds the data toggle
942
943static void
944ehci_endpoint_disable (struct usb_hcd *hcd, struct usb_host_endpoint *ep)
945{
946 struct ehci_hcd *ehci = hcd_to_ehci (hcd);
947 unsigned long flags;
Ming Lei35371e42013-07-03 22:53:09 +0800948 struct ehci_qh *qh;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949
950 /* ASSERT: any requests/urbs are being unlinked */
951 /* ASSERT: nobody can be submitting urbs for this any more */
952
953rescan:
954 spin_lock_irqsave (&ehci->lock, flags);
955 qh = ep->hcpriv;
956 if (!qh)
957 goto done;
958
959 /* endpoints can be iso streams. for now, we don't
960 * accelerate iso completions ... so spin a while.
961 */
Clemens Ladisch1082f572010-03-01 17:18:56 +0100962 if (qh->hw == NULL) {
Alan Stern8c5bf7b2012-07-11 11:22:39 -0400963 struct ehci_iso_stream *stream = ep->hcpriv;
964
965 if (!list_empty(&stream->td_list))
966 goto idle_timeout;
967
968 /* BUG_ON(!list_empty(&stream->free_list)); */
Alan Sternd0ce5c62013-10-11 11:29:13 -0400969 reserve_release_iso_bandwidth(ehci, stream, -1);
Alan Stern8c5bf7b2012-07-11 11:22:39 -0400970 kfree(stream);
971 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 }
973
Alan Sternfcc51842016-01-25 15:42:04 -0500974 qh->unlink_reason |= QH_UNLINK_REQUESTED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 switch (qh->qh_state) {
976 case QH_STATE_LINKED:
Alan Sternfcc51842016-01-25 15:42:04 -0500977 if (list_empty(&qh->qtd_list))
978 qh->unlink_reason |= QH_UNLINK_QUEUE_EMPTY;
979 else
980 WARN_ON(1);
Ming Lei35371e42013-07-03 22:53:09 +0800981 if (usb_endpoint_type(&ep->desc) != USB_ENDPOINT_XFER_INT)
Alan Stern3c273a02012-07-11 11:22:49 -0400982 start_unlink_async(ehci, qh);
Ming Lei35371e42013-07-03 22:53:09 +0800983 else
984 start_unlink_intr(ehci, qh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 /* FALL THROUGH */
Ming Lei35371e42013-07-03 22:53:09 +0800986 case QH_STATE_COMPLETING: /* already in unlinking */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 case QH_STATE_UNLINK: /* wait for hw to finish? */
Alan Stern07d29b62007-12-11 16:05:30 -0500988 case QH_STATE_UNLINK_WAIT:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989idle_timeout:
990 spin_unlock_irqrestore (&ehci->lock, flags);
Nishanth Aravamudan22c43862005-08-15 11:30:11 -0700991 schedule_timeout_uninterruptible(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992 goto rescan;
993 case QH_STATE_IDLE: /* fully unlinked */
Alan Stern914b7012009-06-29 10:47:30 -0400994 if (qh->clearing_tt)
995 goto idle_timeout;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996 if (list_empty (&qh->qtd_list)) {
Alan Sternd0ce5c62013-10-11 11:29:13 -0400997 if (qh->ps.bw_uperiod)
998 reserve_release_intr_bandwidth(ehci, qh, -1);
Alan Sternc83e1a92012-07-11 11:21:25 -0400999 qh_destroy(ehci, qh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000 break;
1001 }
Gustavo A. R. Silva6ecbf2e2017-10-25 13:49:21 -05001002 /* fall through */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 /* caller was supposed to have unlinked any requests;
1005 * that's not our job. just leak this memory.
1006 */
1007 ehci_err (ehci, "qh %p (#%02x) state %d%s\n",
1008 qh, ep->desc.bEndpointAddress, qh->qh_state,
1009 list_empty (&qh->qtd_list) ? "" : "(has tds)");
1010 break;
1011 }
Alan Stern8c5bf7b2012-07-11 11:22:39 -04001012 done:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 ep->hcpriv = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014 spin_unlock_irqrestore (&ehci->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015}
1016
Alan Sternb18ffd42009-05-27 18:21:56 -04001017static void
1018ehci_endpoint_reset(struct usb_hcd *hcd, struct usb_host_endpoint *ep)
1019{
1020 struct ehci_hcd *ehci = hcd_to_ehci(hcd);
1021 struct ehci_qh *qh;
1022 int eptype = usb_endpoint_type(&ep->desc);
Alan Sterna455212d2009-06-11 14:56:22 -04001023 int epnum = usb_endpoint_num(&ep->desc);
1024 int is_out = usb_endpoint_dir_out(&ep->desc);
1025 unsigned long flags;
Alan Sternb18ffd42009-05-27 18:21:56 -04001026
1027 if (eptype != USB_ENDPOINT_XFER_BULK && eptype != USB_ENDPOINT_XFER_INT)
1028 return;
1029
Alan Sterna455212d2009-06-11 14:56:22 -04001030 spin_lock_irqsave(&ehci->lock, flags);
Alan Sternb18ffd42009-05-27 18:21:56 -04001031 qh = ep->hcpriv;
1032
1033 /* For Bulk and Interrupt endpoints we maintain the toggle state
1034 * in the hardware; the toggle bits in udev aren't used at all.
1035 * When an endpoint is reset by usb_clear_halt() we must reset
1036 * the toggle bit in the QH.
1037 */
1038 if (qh) {
1039 if (!list_empty(&qh->qtd_list)) {
1040 WARN_ONCE(1, "clear_halt for a busy endpoint\n");
Alan Stern7bc782d2013-03-22 13:31:11 -04001041 } else {
Alan Sterna455212d2009-06-11 14:56:22 -04001042 /* The toggle value in the QH can't be updated
1043 * while the QH is active. Unlink it now;
1044 * re-linking will call qh_refresh().
Alan Sternb18ffd42009-05-27 18:21:56 -04001045 */
Alan Sternffa02482013-10-11 11:29:03 -04001046 usb_settoggle(qh->ps.udev, epnum, is_out, 0);
Alan Sternfcc51842016-01-25 15:42:04 -05001047 qh->unlink_reason |= QH_UNLINK_REQUESTED;
Alan Sterna448c9d2009-08-19 12:22:44 -04001048 if (eptype == USB_ENDPOINT_XFER_BULK)
Alan Stern3c273a02012-07-11 11:22:49 -04001049 start_unlink_async(ehci, qh);
Alan Sterna448c9d2009-08-19 12:22:44 -04001050 else
Alan Sterndf202252012-07-11 11:22:26 -04001051 start_unlink_intr(ehci, qh);
Alan Sternb18ffd42009-05-27 18:21:56 -04001052 }
1053 }
Alan Sterna455212d2009-06-11 14:56:22 -04001054 spin_unlock_irqrestore(&ehci->lock, flags);
Alan Sternb18ffd42009-05-27 18:21:56 -04001055}
1056
Matt Porter7ff71d62005-09-22 22:31:15 -07001057static int ehci_get_frame (struct usb_hcd *hcd)
1058{
1059 struct ehci_hcd *ehci = hcd_to_ehci (hcd);
Alan Stern68aa95d2011-10-12 10:39:14 -04001060 return (ehci_read_frame_index(ehci) >> 3) % ehci->periodic_size;
Matt Porter7ff71d62005-09-22 22:31:15 -07001061}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062
1063/*-------------------------------------------------------------------------*/
Alan Sternc5cf9212012-06-28 11:19:02 -04001064
Alan Sternb35c5002013-10-11 22:16:21 -04001065/* Device addition and removal */
1066
1067static void ehci_remove_device(struct usb_hcd *hcd, struct usb_device *udev)
1068{
1069 struct ehci_hcd *ehci = hcd_to_ehci(hcd);
1070
1071 spin_lock_irq(&ehci->lock);
1072 drop_tt(udev);
1073 spin_unlock_irq(&ehci->lock);
1074}
1075
1076/*-------------------------------------------------------------------------*/
1077
Alan Sternc5cf9212012-06-28 11:19:02 -04001078#ifdef CONFIG_PM
1079
1080/* suspend/resume, section 4.3 */
1081
1082/* These routines handle the generic parts of controller suspend/resume */
1083
Alan Stern3e023202012-11-01 11:12:58 -04001084int ehci_suspend(struct usb_hcd *hcd, bool do_wakeup)
Alan Sternc5cf9212012-06-28 11:19:02 -04001085{
1086 struct ehci_hcd *ehci = hcd_to_ehci(hcd);
1087
1088 if (time_before(jiffies, ehci->next_statechange))
1089 msleep(10);
1090
1091 /*
1092 * Root hub was already suspended. Disable IRQ emission and
1093 * mark HW unaccessible. The PM and USB cores make sure that
1094 * the root hub is either suspended or stopped.
1095 */
1096 ehci_prepare_ports_for_controller_suspend(ehci, do_wakeup);
1097
1098 spin_lock_irq(&ehci->lock);
1099 ehci_writel(ehci, 0, &ehci->regs->intr_enable);
1100 (void) ehci_readl(ehci, &ehci->regs->intr_enable);
1101
1102 clear_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
1103 spin_unlock_irq(&ehci->lock);
1104
Alan Sternb8efdaf2013-10-18 11:18:21 -04001105 synchronize_irq(hcd->irq);
1106
1107 /* Check for race with a wakeup request */
1108 if (do_wakeup && HCD_WAKEUP_PENDING(hcd)) {
1109 ehci_resume(hcd, false);
1110 return -EBUSY;
1111 }
1112
Alan Sternc5cf9212012-06-28 11:19:02 -04001113 return 0;
1114}
Alan Stern3e023202012-11-01 11:12:58 -04001115EXPORT_SYMBOL_GPL(ehci_suspend);
Alan Sternc5cf9212012-06-28 11:19:02 -04001116
1117/* Returns 0 if power was preserved, 1 if power was lost */
Wu Liang feng314b41b2014-12-24 18:22:19 +08001118int ehci_resume(struct usb_hcd *hcd, bool force_reset)
Alan Sternc5cf9212012-06-28 11:19:02 -04001119{
1120 struct ehci_hcd *ehci = hcd_to_ehci(hcd);
1121
1122 if (time_before(jiffies, ehci->next_statechange))
1123 msleep(100);
1124
1125 /* Mark hardware accessible again as we are back to full power by now */
1126 set_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
1127
Alan Stern43fe3a92012-07-11 11:23:16 -04001128 if (ehci->shutdown)
1129 return 0; /* Controller is dead */
1130
Alan Sternc5cf9212012-06-28 11:19:02 -04001131 /*
Wu Liang feng314b41b2014-12-24 18:22:19 +08001132 * If CF is still set and reset isn't forced
Alan Sternc5cf9212012-06-28 11:19:02 -04001133 * then we maintained suspend power.
1134 * Just undo the effect of ehci_suspend().
1135 */
1136 if (ehci_readl(ehci, &ehci->regs->configured_flag) == FLAG_CF &&
Wu Liang feng314b41b2014-12-24 18:22:19 +08001137 !force_reset) {
Alan Sternc5cf9212012-06-28 11:19:02 -04001138 int mask = INTR_MASK;
1139
1140 ehci_prepare_ports_for_controller_resume(ehci);
Alan Stern43fe3a92012-07-11 11:23:16 -04001141
1142 spin_lock_irq(&ehci->lock);
1143 if (ehci->shutdown)
1144 goto skip;
1145
Alan Sternc5cf9212012-06-28 11:19:02 -04001146 if (!hcd->self.root_hub->do_remote_wakeup)
1147 mask &= ~STS_PCD;
1148 ehci_writel(ehci, mask, &ehci->regs->intr_enable);
1149 ehci_readl(ehci, &ehci->regs->intr_enable);
Alan Stern43fe3a92012-07-11 11:23:16 -04001150 skip:
1151 spin_unlock_irq(&ehci->lock);
Alan Sternc5cf9212012-06-28 11:19:02 -04001152 return 0;
1153 }
1154
1155 /*
1156 * Else reset, to cope with power loss or resume from hibernation
1157 * having let the firmware kick in during reboot.
1158 */
1159 usb_root_hub_lost_power(hcd->self.root_hub);
1160 (void) ehci_halt(ehci);
1161 (void) ehci_reset(ehci);
1162
Alan Stern43fe3a92012-07-11 11:23:16 -04001163 spin_lock_irq(&ehci->lock);
1164 if (ehci->shutdown)
1165 goto skip;
1166
Alan Sternc5cf9212012-06-28 11:19:02 -04001167 ehci_writel(ehci, ehci->command, &ehci->regs->command);
1168 ehci_writel(ehci, FLAG_CF, &ehci->regs->configured_flag);
1169 ehci_readl(ehci, &ehci->regs->command); /* unblock posted writes */
1170
Alan Stern43fe3a92012-07-11 11:23:16 -04001171 ehci->rh_state = EHCI_RH_SUSPENDED;
1172 spin_unlock_irq(&ehci->lock);
1173
Alan Sternc5cf9212012-06-28 11:19:02 -04001174 return 1;
1175}
Alan Stern3e023202012-11-01 11:12:58 -04001176EXPORT_SYMBOL_GPL(ehci_resume);
Alan Sternc5cf9212012-06-28 11:19:02 -04001177
1178#endif
1179
1180/*-------------------------------------------------------------------------*/
1181
Alexander Shishkineb70e5a2012-05-11 17:25:54 +03001182/*
Alan Stern3e023202012-11-01 11:12:58 -04001183 * Generic structure: This gets copied for platform drivers so that
1184 * individual entries can be overridden as needed.
1185 */
1186
1187static const struct hc_driver ehci_hc_driver = {
1188 .description = hcd_name,
1189 .product_desc = "EHCI Host Controller",
1190 .hcd_priv_size = sizeof(struct ehci_hcd),
1191
1192 /*
1193 * generic hardware linkage
1194 */
1195 .irq = ehci_irq,
Greg Kroah-Hartmanc04ee4b2013-09-23 13:32:51 -07001196 .flags = HCD_MEMORY | HCD_USB2 | HCD_BH,
Alan Stern3e023202012-11-01 11:12:58 -04001197
1198 /*
1199 * basic lifecycle operations
1200 */
1201 .reset = ehci_setup,
1202 .start = ehci_run,
1203 .stop = ehci_stop,
1204 .shutdown = ehci_shutdown,
1205
1206 /*
1207 * managing i/o requests and associated device resources
1208 */
1209 .urb_enqueue = ehci_urb_enqueue,
1210 .urb_dequeue = ehci_urb_dequeue,
1211 .endpoint_disable = ehci_endpoint_disable,
1212 .endpoint_reset = ehci_endpoint_reset,
1213 .clear_tt_buffer_complete = ehci_clear_tt_buffer_complete,
1214
1215 /*
1216 * scheduling support
1217 */
1218 .get_frame_number = ehci_get_frame,
1219
1220 /*
1221 * root hub support
1222 */
1223 .hub_status_data = ehci_hub_status_data,
1224 .hub_control = ehci_hub_control,
1225 .bus_suspend = ehci_bus_suspend,
1226 .bus_resume = ehci_bus_resume,
1227 .relinquish_port = ehci_relinquish_port,
1228 .port_handed_over = ehci_port_handed_over,
Alan Stern00d423c2018-06-08 16:59:50 -04001229 .get_resuming_ports = ehci_get_resuming_ports,
Alan Sternb35c5002013-10-11 22:16:21 -04001230
1231 /*
1232 * device support
1233 */
1234 .free_dev = ehci_remove_device,
Alan Stern3e023202012-11-01 11:12:58 -04001235};
1236
1237void ehci_init_driver(struct hc_driver *drv,
1238 const struct ehci_driver_overrides *over)
1239{
1240 /* Copy the generic table to drv and then apply the overrides */
1241 *drv = ehci_hc_driver;
1242
Alan Stern1b368102012-11-07 16:12:47 -05001243 if (over) {
1244 drv->hcd_priv_size += over->extra_priv_size;
1245 if (over->reset)
1246 drv->reset = over->reset;
Michael Grzeschik11a7e592014-10-13 09:53:03 +08001247 if (over->port_power)
1248 drv->port_power = over->port_power;
Alan Stern1b368102012-11-07 16:12:47 -05001249 }
Alan Stern3e023202012-11-01 11:12:58 -04001250}
1251EXPORT_SYMBOL_GPL(ehci_init_driver);
1252
1253/*-------------------------------------------------------------------------*/
1254
Alan Stern2b70f072008-10-02 11:47:15 -04001255MODULE_DESCRIPTION(DRIVER_DESC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256MODULE_AUTHOR (DRIVER_AUTHOR);
1257MODULE_LICENSE ("GPL");
1258
Yoshihiro Shimoda60b0bf02011-03-01 16:58:37 +09001259#ifdef CONFIG_USB_EHCI_SH
Paul Mundt63c84552010-11-01 17:03:27 -04001260#include "ehci-sh.c"
1261#define PLATFORM_DRIVER ehci_hcd_sh_driver
1262#endif
1263
Geoff Levandad75a412007-01-15 20:11:47 -08001264#ifdef CONFIG_PPC_PS3
1265#include "ehci-ps3.c"
Geoff Levand7a4eb7f2007-06-05 20:04:35 -07001266#define PS3_SYSTEM_BUS_DRIVER ps3_ehci_driver
Geoff Levandad75a412007-01-15 20:11:47 -08001267#endif
1268
Valentine Barshakda0e8fb2007-12-30 15:28:50 -08001269#ifdef CONFIG_USB_EHCI_HCD_PPC_OF
1270#include "ehci-ppc-of.c"
1271#define OF_PLATFORM_DRIVER ehci_hcd_ppc_of_driver
1272#endif
1273
Julie Zhu08d3c182009-09-21 16:08:19 -06001274#ifdef CONFIG_XPS_USB_HCD_XILINX
1275#include "ehci-xilinx-of.c"
Grant Likely1f23b2d2010-06-02 13:53:17 -06001276#define XILINX_OF_PLATFORM_DRIVER ehci_hcd_xilinx_of_driver
Julie Zhu08d3c182009-09-21 16:08:19 -06001277#endif
1278
Anoop22ced6872011-02-24 19:26:28 +05301279#ifdef CONFIG_USB_EHCI_HCD_PMC_MSP
1280#include "ehci-pmcmsp.c"
1281#define PLATFORM_DRIVER ehci_hcd_msp_driver
1282#endif
1283
Jan Andersson9be03922011-05-03 20:11:58 +02001284#ifdef CONFIG_SPARC_LEON
1285#include "ehci-grlib.c"
1286#define PLATFORM_DRIVER ehci_grlib_driver
1287#endif
1288
Neil Zhang3a082ec2011-12-20 13:20:23 +08001289#ifdef CONFIG_USB_EHCI_MV
1290#include "ehci-mv.c"
1291#define PLATFORM_DRIVER ehci_mv_driver
1292#endif
1293
Kumar Gala01cced22006-04-11 10:07:16 -05001294static int __init ehci_hcd_init(void)
1295{
1296 int retval = 0;
1297
Alan Stern2b70f072008-10-02 11:47:15 -04001298 if (usb_disabled())
1299 return -ENODEV;
1300
1301 printk(KERN_INFO "%s: " DRIVER_DESC "\n", hcd_name);
Alan Stern9beeee62008-10-02 11:48:13 -04001302 set_bit(USB_EHCI_LOADED, &usb_hcds_loaded);
1303 if (test_bit(USB_UHCI_LOADED, &usb_hcds_loaded) ||
1304 test_bit(USB_OHCI_LOADED, &usb_hcds_loaded))
1305 printk(KERN_WARNING "Warning! ehci_hcd should always be loaded"
1306 " before uhci_hcd and ohci_hcd, not after\n");
1307
Alexey Dobriyan5b5e0922017-02-27 14:30:02 -08001308 pr_debug("%s: block sizes: qh %zd qtd %zd itd %zd sitd %zd\n",
Kumar Gala01cced22006-04-11 10:07:16 -05001309 hcd_name,
1310 sizeof(struct ehci_qh), sizeof(struct ehci_qtd),
1311 sizeof(struct ehci_itd), sizeof(struct ehci_sitd));
1312
Oliver Neukum1c201632013-11-18 13:23:16 +01001313#ifdef CONFIG_DYNAMIC_DEBUG
Greg Kroah-Hartman08f4e582009-04-24 15:13:18 -07001314 ehci_debug_root = debugfs_create_dir("ehci", usb_debug_root);
Tony Jones694cc202007-09-11 14:07:31 -07001315#endif
1316
Kumar Gala01cced22006-04-11 10:07:16 -05001317#ifdef PLATFORM_DRIVER
1318 retval = platform_driver_register(&PLATFORM_DRIVER);
Valentine Barshakda0e8fb2007-12-30 15:28:50 -08001319 if (retval < 0)
1320 goto clean0;
Kumar Gala01cced22006-04-11 10:07:16 -05001321#endif
1322
Geoff Levandad75a412007-01-15 20:11:47 -08001323#ifdef PS3_SYSTEM_BUS_DRIVER
Geoff Levand7a4eb7f2007-06-05 20:04:35 -07001324 retval = ps3_ehci_driver_register(&PS3_SYSTEM_BUS_DRIVER);
Valentine Barshakda0e8fb2007-12-30 15:28:50 -08001325 if (retval < 0)
1326 goto clean2;
Kumar Gala01cced22006-04-11 10:07:16 -05001327#endif
1328
Valentine Barshakda0e8fb2007-12-30 15:28:50 -08001329#ifdef OF_PLATFORM_DRIVER
Grant Likelyd35fb642011-02-22 21:08:34 -07001330 retval = platform_driver_register(&OF_PLATFORM_DRIVER);
Valentine Barshakda0e8fb2007-12-30 15:28:50 -08001331 if (retval < 0)
1332 goto clean3;
1333#endif
Grant Likely1f23b2d2010-06-02 13:53:17 -06001334
1335#ifdef XILINX_OF_PLATFORM_DRIVER
Grant Likelyd35fb642011-02-22 21:08:34 -07001336 retval = platform_driver_register(&XILINX_OF_PLATFORM_DRIVER);
Grant Likely1f23b2d2010-06-02 13:53:17 -06001337 if (retval < 0)
1338 goto clean4;
1339#endif
Valentine Barshakda0e8fb2007-12-30 15:28:50 -08001340 return retval;
1341
Grant Likely1f23b2d2010-06-02 13:53:17 -06001342#ifdef XILINX_OF_PLATFORM_DRIVER
Grant Likelyd35fb642011-02-22 21:08:34 -07001343 /* platform_driver_unregister(&XILINX_OF_PLATFORM_DRIVER); */
Grant Likely1f23b2d2010-06-02 13:53:17 -06001344clean4:
1345#endif
Valentine Barshakda0e8fb2007-12-30 15:28:50 -08001346#ifdef OF_PLATFORM_DRIVER
Grant Likelyd35fb642011-02-22 21:08:34 -07001347 platform_driver_unregister(&OF_PLATFORM_DRIVER);
Valentine Barshakda0e8fb2007-12-30 15:28:50 -08001348clean3:
1349#endif
1350#ifdef PS3_SYSTEM_BUS_DRIVER
1351 ps3_ehci_driver_unregister(&PS3_SYSTEM_BUS_DRIVER);
1352clean2:
1353#endif
Valentine Barshakda0e8fb2007-12-30 15:28:50 -08001354#ifdef PLATFORM_DRIVER
1355 platform_driver_unregister(&PLATFORM_DRIVER);
1356clean0:
1357#endif
Oliver Neukum1c201632013-11-18 13:23:16 +01001358#ifdef CONFIG_DYNAMIC_DEBUG
Valentine Barshakda0e8fb2007-12-30 15:28:50 -08001359 debugfs_remove(ehci_debug_root);
1360 ehci_debug_root = NULL;
Linus Torvaldsa9b61482008-10-20 14:23:29 -07001361#endif
Alan Stern9beeee62008-10-02 11:48:13 -04001362 clear_bit(USB_EHCI_LOADED, &usb_hcds_loaded);
Kumar Gala01cced22006-04-11 10:07:16 -05001363 return retval;
1364}
1365module_init(ehci_hcd_init);
1366
1367static void __exit ehci_hcd_cleanup(void)
1368{
Grant Likely1f23b2d2010-06-02 13:53:17 -06001369#ifdef XILINX_OF_PLATFORM_DRIVER
Grant Likelyd35fb642011-02-22 21:08:34 -07001370 platform_driver_unregister(&XILINX_OF_PLATFORM_DRIVER);
Grant Likely1f23b2d2010-06-02 13:53:17 -06001371#endif
Valentine Barshakda0e8fb2007-12-30 15:28:50 -08001372#ifdef OF_PLATFORM_DRIVER
Grant Likelyd35fb642011-02-22 21:08:34 -07001373 platform_driver_unregister(&OF_PLATFORM_DRIVER);
Valentine Barshakda0e8fb2007-12-30 15:28:50 -08001374#endif
Kumar Gala01cced22006-04-11 10:07:16 -05001375#ifdef PLATFORM_DRIVER
1376 platform_driver_unregister(&PLATFORM_DRIVER);
1377#endif
Geoff Levandad75a412007-01-15 20:11:47 -08001378#ifdef PS3_SYSTEM_BUS_DRIVER
Geoff Levand7a4eb7f2007-06-05 20:04:35 -07001379 ps3_ehci_driver_unregister(&PS3_SYSTEM_BUS_DRIVER);
Geoff Levandad75a412007-01-15 20:11:47 -08001380#endif
Oliver Neukum1c201632013-11-18 13:23:16 +01001381#ifdef CONFIG_DYNAMIC_DEBUG
Tony Jones694cc202007-09-11 14:07:31 -07001382 debugfs_remove(ehci_debug_root);
1383#endif
Alan Stern9beeee62008-10-02 11:48:13 -04001384 clear_bit(USB_EHCI_LOADED, &usb_hcds_loaded);
Kumar Gala01cced22006-04-11 10:07:16 -05001385}
1386module_exit(ehci_hcd_cleanup);