blob: ecd5d6a55ca3652da0ce9ff81b87714269ce552e [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
David Brownelld49d4312005-05-07 13:21:50 -07002 * Copyright (C) 2001-2004 by David Brownell
David Brownell53bd6a62006-08-30 14:50:06 -07003 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License as published by the
6 * Free Software Foundation; either version 2 of the License, or (at your
7 * option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
11 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software Foundation,
16 * Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17 */
18
19/* this file is part of ehci-hcd.c */
20
21/*-------------------------------------------------------------------------*/
22
23/*
24 * EHCI Root Hub ... the nonsharable stuff
25 *
26 * Registers don't need cpu_to_le32, that happens transparently
27 */
28
29/*-------------------------------------------------------------------------*/
Anatolij Gustschin83722bc2011-04-18 22:02:00 +020030#include <linux/usb/otg.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031
Alan Stern58a97ff2008-04-14 12:17:10 -040032#define PORT_WAKE_BITS (PORT_WKOC_E|PORT_WKDISC_E|PORT_WKCONN_E)
33
Alan Sternaff6d182008-04-18 11:11:26 -040034#ifdef CONFIG_PM
35
Julius Werner9b790912013-05-17 12:08:51 -070036static int persist_enabled_on_companion(struct usb_device *udev, void *unused)
37{
38 return !udev->maxchild && udev->persist_enabled &&
39 udev->bus->root_hub->speed < USB_SPEED_HIGH;
40}
41
Alan Stern383975d2007-05-04 11:52:40 -040042/* After a power loss, ports that were owned by the companion must be
43 * reset so that the companion can still own them.
44 */
45static void ehci_handover_companion_ports(struct ehci_hcd *ehci)
46{
47 u32 __iomem *reg;
48 u32 status;
49 int port;
50 __le32 buf;
51 struct usb_hcd *hcd = ehci_to_hcd(ehci);
52
53 if (!ehci->owned_ports)
54 return;
55
Julius Werner9b790912013-05-17 12:08:51 -070056 /*
57 * USB 1.1 devices are mostly HIDs, which don't need to persist across
58 * suspends. If we ensure that none of our companion's devices have
59 * persist_enabled (by looking through all USB 1.1 buses in the system),
60 * we can skip this and avoid slowing resume down. Devices without
61 * persist will just get reenumerated shortly after resume anyway.
62 */
63 if (!usb_for_each_dev(NULL, persist_enabled_on_companion))
64 return;
65
Alan Sternc73cee72012-10-31 13:21:06 -040066 /* Make sure the ports are powered */
67 port = HCS_N_PORTS(ehci->hcs_params);
68 while (port--) {
69 if (test_bit(port, &ehci->owned_ports)) {
70 reg = &ehci->regs->port_status[port];
71 status = ehci_readl(ehci, reg) & ~PORT_RWC_BITS;
72 if (!(status & PORT_POWER)) {
73 status |= PORT_POWER;
74 ehci_writel(ehci, status, reg);
75 }
76 }
77 }
78
Alan Stern383975d2007-05-04 11:52:40 -040079 /* Give the connections some time to appear */
80 msleep(20);
81
Alan Sternc4f34762012-07-11 11:23:10 -040082 spin_lock_irq(&ehci->lock);
Alan Stern383975d2007-05-04 11:52:40 -040083 port = HCS_N_PORTS(ehci->hcs_params);
84 while (port--) {
85 if (test_bit(port, &ehci->owned_ports)) {
86 reg = &ehci->regs->port_status[port];
Alan Stern3c519b82007-05-04 11:55:31 -040087 status = ehci_readl(ehci, reg) & ~PORT_RWC_BITS;
Alan Stern383975d2007-05-04 11:52:40 -040088
89 /* Port already owned by companion? */
90 if (status & PORT_OWNER)
91 clear_bit(port, &ehci->owned_ports);
Alan Stern3c519b82007-05-04 11:55:31 -040092 else if (test_bit(port, &ehci->companion_ports))
93 ehci_writel(ehci, status & ~PORT_PE, reg);
Alan Sternc4f34762012-07-11 11:23:10 -040094 else {
95 spin_unlock_irq(&ehci->lock);
Alan Stern383975d2007-05-04 11:52:40 -040096 ehci_hub_control(hcd, SetPortFeature,
97 USB_PORT_FEAT_RESET, port + 1,
98 NULL, 0);
Alan Sternc4f34762012-07-11 11:23:10 -040099 spin_lock_irq(&ehci->lock);
100 }
Alan Stern383975d2007-05-04 11:52:40 -0400101 }
102 }
Alan Sternc4f34762012-07-11 11:23:10 -0400103 spin_unlock_irq(&ehci->lock);
Alan Stern383975d2007-05-04 11:52:40 -0400104
105 if (!ehci->owned_ports)
106 return;
107 msleep(90); /* Wait for resets to complete */
108
Alan Sternc4f34762012-07-11 11:23:10 -0400109 spin_lock_irq(&ehci->lock);
Alan Stern383975d2007-05-04 11:52:40 -0400110 port = HCS_N_PORTS(ehci->hcs_params);
111 while (port--) {
112 if (test_bit(port, &ehci->owned_ports)) {
Alan Sternc4f34762012-07-11 11:23:10 -0400113 spin_unlock_irq(&ehci->lock);
Alan Stern383975d2007-05-04 11:52:40 -0400114 ehci_hub_control(hcd, GetPortStatus,
115 0, port + 1,
116 (char *) &buf, sizeof(buf));
Alan Sternc4f34762012-07-11 11:23:10 -0400117 spin_lock_irq(&ehci->lock);
Alan Stern383975d2007-05-04 11:52:40 -0400118
119 /* The companion should now own the port,
120 * but if something went wrong the port must not
121 * remain enabled.
122 */
123 reg = &ehci->regs->port_status[port];
124 status = ehci_readl(ehci, reg) & ~PORT_RWC_BITS;
125 if (status & PORT_OWNER)
126 ehci_writel(ehci, status | PORT_CSC, reg);
127 else {
128 ehci_dbg(ehci, "failed handover port %d: %x\n",
129 port + 1, status);
130 ehci_writel(ehci, status & ~PORT_PE, reg);
131 }
132 }
133 }
134
135 ehci->owned_ports = 0;
Alan Sternc4f34762012-07-11 11:23:10 -0400136 spin_unlock_irq(&ehci->lock);
Alan Stern383975d2007-05-04 11:52:40 -0400137}
138
Alan Sternc5cf9212012-06-28 11:19:02 -0400139static int ehci_port_change(struct ehci_hcd *ehci)
Matthew Garrett294d95f2011-01-11 12:26:48 -0500140{
141 int i = HCS_N_PORTS(ehci->hcs_params);
142
143 /* First check if the controller indicates a change event */
144
145 if (ehci_readl(ehci, &ehci->regs->status) & STS_PCD)
146 return 1;
147
148 /*
149 * Not all controllers appear to update this while going from D3 to D0,
150 * so check the individual port status registers as well
151 */
152
153 while (i--)
154 if (ehci_readl(ehci, &ehci->regs->port_status[i]) & PORT_CSC)
155 return 1;
156
157 return 0;
158}
159
Alan Sternc5cf9212012-06-28 11:19:02 -0400160static void ehci_adjust_port_wakeup_flags(struct ehci_hcd *ehci,
Alan Stern41472002010-06-25 14:02:14 -0400161 bool suspending, bool do_wakeup)
Alan Stern16032c42010-05-12 18:21:35 -0400162{
163 int port;
164 u32 temp;
165
166 /* If remote wakeup is enabled for the root hub but disabled
167 * for the controller, we must adjust all the port wakeup flags
168 * when the controller is suspended or resumed. In all other
169 * cases they don't need to be changed.
170 */
Alan Stern41472002010-06-25 14:02:14 -0400171 if (!ehci_to_hcd(ehci)->self.root_hub->do_remote_wakeup || do_wakeup)
Alan Stern16032c42010-05-12 18:21:35 -0400172 return;
173
Alan Sternc4f34762012-07-11 11:23:10 -0400174 spin_lock_irq(&ehci->lock);
Yin Kangkai148fc552011-01-28 12:04:35 +0800175
Alan Stern16032c42010-05-12 18:21:35 -0400176 /* clear phy low-power mode before changing wakeup flags */
Tuomas Tynkkynen2cdcec42013-08-12 16:06:49 +0300177 if (ehci->has_tdi_phy_lpm) {
Alan Stern16032c42010-05-12 18:21:35 -0400178 port = HCS_N_PORTS(ehci->hcs_params);
179 while (port--) {
Alan Sterna46af4e2012-06-25 12:19:03 -0400180 u32 __iomem *hostpc_reg = &ehci->regs->hostpc[port];
Alan Stern16032c42010-05-12 18:21:35 -0400181
Alan Stern16032c42010-05-12 18:21:35 -0400182 temp = ehci_readl(ehci, hostpc_reg);
183 ehci_writel(ehci, temp & ~HOSTPC_PHCD, hostpc_reg);
184 }
Alan Sternc4f34762012-07-11 11:23:10 -0400185 spin_unlock_irq(&ehci->lock);
Alan Stern16032c42010-05-12 18:21:35 -0400186 msleep(5);
Alan Sternc4f34762012-07-11 11:23:10 -0400187 spin_lock_irq(&ehci->lock);
Alan Stern16032c42010-05-12 18:21:35 -0400188 }
189
190 port = HCS_N_PORTS(ehci->hcs_params);
191 while (port--) {
192 u32 __iomem *reg = &ehci->regs->port_status[port];
193 u32 t1 = ehci_readl(ehci, reg) & ~PORT_RWC_BITS;
194 u32 t2 = t1 & ~PORT_WAKE_BITS;
195
196 /* If we are suspending the controller, clear the flags.
197 * If we are resuming the controller, set the wakeup flags.
198 */
199 if (!suspending) {
200 if (t1 & PORT_CONNECT)
201 t2 |= PORT_WKOC_E | PORT_WKDISC_E;
202 else
203 t2 |= PORT_WKOC_E | PORT_WKCONN_E;
204 }
Alan Stern16032c42010-05-12 18:21:35 -0400205 ehci_writel(ehci, t2, reg);
206 }
207
208 /* enter phy low-power mode again */
Tuomas Tynkkynen2cdcec42013-08-12 16:06:49 +0300209 if (ehci->has_tdi_phy_lpm) {
Alan Stern16032c42010-05-12 18:21:35 -0400210 port = HCS_N_PORTS(ehci->hcs_params);
211 while (port--) {
Alan Sterna46af4e2012-06-25 12:19:03 -0400212 u32 __iomem *hostpc_reg = &ehci->regs->hostpc[port];
Alan Stern16032c42010-05-12 18:21:35 -0400213
Alan Stern16032c42010-05-12 18:21:35 -0400214 temp = ehci_readl(ehci, hostpc_reg);
215 ehci_writel(ehci, temp | HOSTPC_PHCD, hostpc_reg);
216 }
217 }
Alan Sternee0b9be2010-06-25 14:02:24 -0400218
219 /* Does the root hub have a port wakeup pending? */
Matthew Garrett294d95f2011-01-11 12:26:48 -0500220 if (!suspending && ehci_port_change(ehci))
Alan Sternee0b9be2010-06-25 14:02:24 -0400221 usb_hcd_resume_root_hub(ehci_to_hcd(ehci));
Yin Kangkai148fc552011-01-28 12:04:35 +0800222
Alan Sternc4f34762012-07-11 11:23:10 -0400223 spin_unlock_irq(&ehci->lock);
Alan Stern16032c42010-05-12 18:21:35 -0400224}
225
Alan Stern0c0382e2005-10-13 17:08:02 -0400226static int ehci_bus_suspend (struct usb_hcd *hcd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227{
228 struct ehci_hcd *ehci = hcd_to_ehci (hcd);
229 int port;
Alan Stern8c033562006-11-09 14:42:16 -0500230 int mask;
Alan Stern16032c42010-05-12 18:21:35 -0400231 int changed;
Alan Stern3e8d6d82014-02-13 15:49:17 -0500232 bool fs_idle_delay;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233
Alan Stern8c774fe2007-02-01 16:09:59 -0500234 ehci_dbg(ehci, "suspend root hub\n");
235
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 if (time_before (jiffies, ehci->next_statechange))
237 msleep(5);
238
Alan Sternc4f34762012-07-11 11:23:10 -0400239 /* stop the schedules */
240 ehci_quiesce(ehci);
241
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 spin_lock_irq (&ehci->lock);
Alan Stern43fe3a92012-07-11 11:23:16 -0400243 if (ehci->rh_state < EHCI_RH_RUNNING)
244 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245
Alan Sterncec3a532010-01-08 11:18:20 -0500246 /* Once the controller is stopped, port resumes that are already
247 * in progress won't complete. Hence if remote wakeup is enabled
248 * for the root hub and any ports are in the middle of a resume or
249 * remote wakeup, we must fail the suspend.
250 */
251 if (hcd->self.root_hub->do_remote_wakeup) {
Alan Sterna448e4d2012-04-03 15:24:30 -0400252 if (ehci->resuming_ports) {
253 spin_unlock_irq(&ehci->lock);
254 ehci_dbg(ehci, "suspend failed because a port is resuming\n");
255 return -EBUSY;
Alan Sterncec3a532010-01-08 11:18:20 -0500256 }
257 }
258
Alan Stern8c033562006-11-09 14:42:16 -0500259 /* Unlike other USB host controller types, EHCI doesn't have
260 * any notion of "global" or bus-wide suspend. The driver has
261 * to manually suspend all the active unsuspended ports, and
262 * then manually resume them in the bus_resume() routine.
263 */
264 ehci->bus_suspended = 0;
Alan Stern383975d2007-05-04 11:52:40 -0400265 ehci->owned_ports = 0;
Alan Stern16032c42010-05-12 18:21:35 -0400266 changed = 0;
Alan Stern3e8d6d82014-02-13 15:49:17 -0500267 fs_idle_delay = false;
Alan Sterncec3a532010-01-08 11:18:20 -0500268 port = HCS_N_PORTS(ehci->hcs_params);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 while (port--) {
270 u32 __iomem *reg = &ehci->regs->port_status [port];
Benjamin Herrenschmidt083522d2006-12-15 06:54:08 +1100271 u32 t1 = ehci_readl(ehci, reg) & ~PORT_RWC_BITS;
Alan Stern16032c42010-05-12 18:21:35 -0400272 u32 t2 = t1 & ~PORT_WAKE_BITS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273
Alan Stern8c033562006-11-09 14:42:16 -0500274 /* keep track of which ports we suspend */
Alan Stern383975d2007-05-04 11:52:40 -0400275 if (t1 & PORT_OWNER)
276 set_bit(port, &ehci->owned_ports);
277 else if ((t1 & PORT_PE) && !(t1 & PORT_SUSPEND)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 t2 |= PORT_SUSPEND;
Alan Stern8c033562006-11-09 14:42:16 -0500279 set_bit(port, &ehci->bus_suspended);
280 }
281
Alan Stern16032c42010-05-12 18:21:35 -0400282 /* enable remote wakeup on all ports, if told to do so */
Alek Du331ac6b2009-07-13 12:41:20 +0800283 if (hcd->self.root_hub->do_remote_wakeup) {
284 /* only enable appropriate wake bits, otherwise the
285 * hardware can not go phy low power mode. If a race
286 * condition happens here(connection change during bits
287 * set), the port change detection will finally fix it.
288 */
Alan Stern16032c42010-05-12 18:21:35 -0400289 if (t1 & PORT_CONNECT)
Alek Du331ac6b2009-07-13 12:41:20 +0800290 t2 |= PORT_WKOC_E | PORT_WKDISC_E;
Alan Stern16032c42010-05-12 18:21:35 -0400291 else
Alek Du331ac6b2009-07-13 12:41:20 +0800292 t2 |= PORT_WKOC_E | PORT_WKCONN_E;
Alan Stern16032c42010-05-12 18:21:35 -0400293 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294
295 if (t1 != t2) {
Alan Stern3e8d6d82014-02-13 15:49:17 -0500296 /*
297 * On some controllers, Wake-On-Disconnect will
298 * generate false wakeup signals until the bus
299 * switches over to full-speed idle. For their
300 * sake, add a delay if we need one.
301 */
302 if ((t2 & PORT_WKDISC_E) &&
303 ehci_port_speed(ehci, t2) ==
304 USB_PORT_STAT_HIGH_SPEED)
305 fs_idle_delay = true;
Benjamin Herrenschmidt083522d2006-12-15 06:54:08 +1100306 ehci_writel(ehci, t2, reg);
Alan Stern16032c42010-05-12 18:21:35 -0400307 changed = 1;
308 }
309 }
Alan Stern3e8d6d82014-02-13 15:49:17 -0500310 spin_unlock_irq(&ehci->lock);
311
312 if ((changed && ehci->has_tdi_phy_lpm) || fs_idle_delay) {
313 /*
314 * Wait for HCD to enter low-power mode or for the bus
315 * to switch to full-speed idle.
316 */
317 usleep_range(5000, 5500);
318 }
Alek Du331ac6b2009-07-13 12:41:20 +0800319
Tuomas Tynkkynen2cdcec42013-08-12 16:06:49 +0300320 if (changed && ehci->has_tdi_phy_lpm) {
Alan Stern16032c42010-05-12 18:21:35 -0400321 spin_lock_irq(&ehci->lock);
Alan Stern16032c42010-05-12 18:21:35 -0400322 port = HCS_N_PORTS(ehci->hcs_params);
323 while (port--) {
Alan Sterna46af4e2012-06-25 12:19:03 -0400324 u32 __iomem *hostpc_reg = &ehci->regs->hostpc[port];
Alan Stern16032c42010-05-12 18:21:35 -0400325 u32 t3;
326
Alan Stern16032c42010-05-12 18:21:35 -0400327 t3 = ehci_readl(ehci, hostpc_reg);
328 ehci_writel(ehci, t3 | HOSTPC_PHCD, hostpc_reg);
329 t3 = ehci_readl(ehci, hostpc_reg);
330 ehci_dbg(ehci, "Port %d phy low-power mode %s\n",
Alek Du331ac6b2009-07-13 12:41:20 +0800331 port, (t3 & HOSTPC_PHCD) ?
332 "succeeded" : "failed");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 }
Alan Stern3e8d6d82014-02-13 15:49:17 -0500334 spin_unlock_irq(&ehci->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 }
336
Alan Sterncd930c92008-01-10 11:14:53 -0500337 /* Apparently some devices need a >= 1-uframe delay here */
338 if (ehci->bus_suspended)
339 udelay(150);
340
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 /* turn off now-idle HC */
342 ehci_halt (ehci);
Alan Sternc4f34762012-07-11 11:23:10 -0400343
344 spin_lock_irq(&ehci->lock);
Alan Stern43fe3a92012-07-11 11:23:16 -0400345 if (ehci->enabled_hrtimer_events & BIT(EHCI_HRTIMER_POLL_DEAD))
346 ehci_handle_controller_death(ehci);
347 if (ehci->rh_state != EHCI_RH_RUNNING)
348 goto done;
Alan Sterne8799902011-08-18 16:31:30 -0400349 ehci->rh_state = EHCI_RH_SUSPENDED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350
Alan Stern3c273a02012-07-11 11:22:49 -0400351 end_unlink_async(ehci);
Alan Stern2a40f322013-03-15 14:40:26 -0400352 unlink_empty_async_suspended(ehci);
Ming Lei9118f9e2013-07-03 22:53:10 +0800353 ehci_handle_start_intr_unlinks(ehci);
Alan Sterndf202252012-07-11 11:22:26 -0400354 ehci_handle_intr_unlinks(ehci);
Alan Stern55934eb2012-07-11 11:22:35 -0400355 end_free_itds(ehci);
David Brownellcdc647a2008-04-02 13:40:20 -0700356
Alan Stern8c033562006-11-09 14:42:16 -0500357 /* allow remote wakeup */
358 mask = INTR_MASK;
Alan Stern58a97ff2008-04-14 12:17:10 -0400359 if (!hcd->self.root_hub->do_remote_wakeup)
Alan Stern8c033562006-11-09 14:42:16 -0500360 mask &= ~STS_PCD;
Benjamin Herrenschmidt083522d2006-12-15 06:54:08 +1100361 ehci_writel(ehci, mask, &ehci->regs->intr_enable);
362 ehci_readl(ehci, &ehci->regs->intr_enable);
Alan Stern8c033562006-11-09 14:42:16 -0500363
Alan Stern43fe3a92012-07-11 11:23:16 -0400364 done:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 ehci->next_statechange = jiffies + msecs_to_jiffies(10);
Alan Sternd58b4bc2012-07-11 11:21:54 -0400366 ehci->enabled_hrtimer_events = 0;
367 ehci->next_hrtimer_event = EHCI_HRTIMER_NO_EVENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 spin_unlock_irq (&ehci->lock);
Jon Hunter015798b2009-08-12 11:57:59 -0400369
Alan Sternd58b4bc2012-07-11 11:21:54 -0400370 hrtimer_cancel(&ehci->hrtimer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 return 0;
372}
373
374
375/* caller has locked the root hub, and should reset/reinit on error */
Alan Stern0c0382e2005-10-13 17:08:02 -0400376static int ehci_bus_resume (struct usb_hcd *hcd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377{
378 struct ehci_hcd *ehci = hcd_to_ehci (hcd);
379 u32 temp;
Alan Stern383975d2007-05-04 11:52:40 -0400380 u32 power_okay;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 int i;
Wang Zhid0f2fb22011-08-17 10:39:31 +0800382 unsigned long resume_needed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383
384 if (time_before (jiffies, ehci->next_statechange))
385 msleep(5);
386 spin_lock_irq (&ehci->lock);
Alan Stern43fe3a92012-07-11 11:23:16 -0400387 if (!HCD_HW_ACCESSIBLE(hcd) || ehci->shutdown)
388 goto shutdown;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389
Jason Wesselad45f1d2009-08-20 15:39:58 -0500390 if (unlikely(ehci->debug)) {
Jan Beulich9fa57802012-09-18 12:23:02 +0100391 if (!dbgp_reset_prep(hcd))
Jason Wesselad45f1d2009-08-20 15:39:58 -0500392 ehci->debug = NULL;
393 else
Jan Beulich9fa57802012-09-18 12:23:02 +0100394 dbgp_external_startup(hcd);
Jason Wesselad45f1d2009-08-20 15:39:58 -0500395 }
396
David Brownellf03c17f2005-11-23 15:45:28 -0800397 /* Ideally and we've got a real resume here, and no port's power
398 * was lost. (For PCI, that means Vaux was maintained.) But we
399 * could instead be restoring a swsusp snapshot -- so that BIOS was
400 * the last user of the controller, not reset/pm hardware keeping
401 * state we gave to it.
402 */
Alan Stern383975d2007-05-04 11:52:40 -0400403 power_okay = ehci_readl(ehci, &ehci->regs->intr_enable);
404 ehci_dbg(ehci, "resume root hub%s\n",
405 power_okay ? "" : " after power loss");
David Brownellf03c17f2005-11-23 15:45:28 -0800406
Alan Stern8c033562006-11-09 14:42:16 -0500407 /* at least some APM implementations will try to deliver
408 * IRQs right away, so delay them until we're ready.
409 */
Benjamin Herrenschmidt083522d2006-12-15 06:54:08 +1100410 ehci_writel(ehci, 0, &ehci->regs->intr_enable);
Alan Stern8c033562006-11-09 14:42:16 -0500411
412 /* re-init operational registers */
Benjamin Herrenschmidt083522d2006-12-15 06:54:08 +1100413 ehci_writel(ehci, 0, &ehci->regs->segment);
414 ehci_writel(ehci, ehci->periodic_dma, &ehci->regs->frame_list);
415 ehci_writel(ehci, (u32) ehci->async->qh_dma, &ehci->regs->async_next);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416
417 /* restore CMD_RUN, framelist size, and irq threshold */
Alan Stern3d9545c2012-04-23 13:54:36 -0400418 ehci->command |= CMD_RUN;
Benjamin Herrenschmidt083522d2006-12-15 06:54:08 +1100419 ehci_writel(ehci, ehci->command, &ehci->regs->command);
Alan Sterne8799902011-08-18 16:31:30 -0400420 ehci->rh_state = EHCI_RH_RUNNING;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421
Peter Chen6273f182012-10-18 12:24:43 +0800422 /*
423 * According to Bugzilla #8190, the port status for some controllers
424 * will be wrong without a delay. At their wrong status, the port
425 * is enabled, but not suspended neither resumed.
426 */
427 i = HCS_N_PORTS(ehci->hcs_params);
428 while (i--) {
429 temp = ehci_readl(ehci, &ehci->regs->port_status[i]);
430 if ((temp & PORT_PE) &&
431 !(temp & (PORT_SUSPEND | PORT_RESUME))) {
432 ehci_dbg(ehci, "Port status(0x%x) is wrong\n", temp);
433 spin_unlock_irq(&ehci->lock);
434 msleep(8);
435 spin_lock_irq(&ehci->lock);
436 break;
437 }
438 }
439
Alan Stern43fe3a92012-07-11 11:23:16 -0400440 if (ehci->shutdown)
441 goto shutdown;
Alan Sterne198a312007-03-15 15:54:30 -0400442
Alan Stern16032c42010-05-12 18:21:35 -0400443 /* clear phy low-power mode before resume */
Tuomas Tynkkynen2cdcec42013-08-12 16:06:49 +0300444 if (ehci->bus_suspended && ehci->has_tdi_phy_lpm) {
Alan Stern16032c42010-05-12 18:21:35 -0400445 i = HCS_N_PORTS(ehci->hcs_params);
446 while (i--) {
447 if (test_bit(i, &ehci->bus_suspended)) {
Alan Sterna46af4e2012-06-25 12:19:03 -0400448 u32 __iomem *hostpc_reg =
449 &ehci->regs->hostpc[i];
Alan Stern16032c42010-05-12 18:21:35 -0400450
Alan Stern16032c42010-05-12 18:21:35 -0400451 temp = ehci_readl(ehci, hostpc_reg);
452 ehci_writel(ehci, temp & ~HOSTPC_PHCD,
453 hostpc_reg);
454 }
455 }
456 spin_unlock_irq(&ehci->lock);
457 msleep(5);
458 spin_lock_irq(&ehci->lock);
Alan Stern43fe3a92012-07-11 11:23:16 -0400459 if (ehci->shutdown)
460 goto shutdown;
Alan Stern16032c42010-05-12 18:21:35 -0400461 }
462
Alan Stern8c033562006-11-09 14:42:16 -0500463 /* manually resume the ports we suspended during bus_suspend() */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 i = HCS_N_PORTS (ehci->hcs_params);
465 while (i--) {
Benjamin Herrenschmidt083522d2006-12-15 06:54:08 +1100466 temp = ehci_readl(ehci, &ehci->regs->port_status [i]);
Alan Stern58a97ff2008-04-14 12:17:10 -0400467 temp &= ~(PORT_RWC_BITS | PORT_WAKE_BITS);
Alan Stern8c033562006-11-09 14:42:16 -0500468 if (test_bit(i, &ehci->bus_suspended) &&
Vikram Pandita3a4e72c2008-10-24 23:41:30 +0530469 (temp & PORT_SUSPEND)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 temp |= PORT_RESUME;
Wang Zhid0f2fb22011-08-17 10:39:31 +0800471 set_bit(i, &resume_needed);
Vikram Pandita3a4e72c2008-10-24 23:41:30 +0530472 }
Benjamin Herrenschmidt083522d2006-12-15 06:54:08 +1100473 ehci_writel(ehci, temp, &ehci->regs->port_status [i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 }
Vikram Pandita3a4e72c2008-10-24 23:41:30 +0530475
Felipe Balbi4062a4e2015-02-13 14:42:25 -0600476 /*
477 * msleep for USB_RESUME_TIMEOUT ms only if code is trying to resume
478 * port
479 */
Vikram Pandita3a4e72c2008-10-24 23:41:30 +0530480 if (resume_needed) {
481 spin_unlock_irq(&ehci->lock);
Felipe Balbi4062a4e2015-02-13 14:42:25 -0600482 msleep(USB_RESUME_TIMEOUT);
Vikram Pandita3a4e72c2008-10-24 23:41:30 +0530483 spin_lock_irq(&ehci->lock);
Alan Stern43fe3a92012-07-11 11:23:16 -0400484 if (ehci->shutdown)
485 goto shutdown;
Vikram Pandita3a4e72c2008-10-24 23:41:30 +0530486 }
487
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 i = HCS_N_PORTS (ehci->hcs_params);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 while (i--) {
Benjamin Herrenschmidt083522d2006-12-15 06:54:08 +1100490 temp = ehci_readl(ehci, &ehci->regs->port_status [i]);
Wang Zhid0f2fb22011-08-17 10:39:31 +0800491 if (test_bit(i, &resume_needed)) {
Alan Stern24b90812013-03-18 12:05:42 -0400492 temp &= ~(PORT_RWC_BITS | PORT_SUSPEND | PORT_RESUME);
Benjamin Herrenschmidt083522d2006-12-15 06:54:08 +1100493 ehci_writel(ehci, temp, &ehci->regs->port_status [i]);
Alan Stern8c033562006-11-09 14:42:16 -0500494 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496
497 ehci->next_statechange = jiffies + msecs_to_jiffies(5);
Alan Sternc4f34762012-07-11 11:23:10 -0400498 spin_unlock_irq(&ehci->lock);
499
500 ehci_handover_companion_ports(ehci);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501
502 /* Now we can safely re-enable irqs */
Alan Stern43fe3a92012-07-11 11:23:16 -0400503 spin_lock_irq(&ehci->lock);
504 if (ehci->shutdown)
505 goto shutdown;
Benjamin Herrenschmidt083522d2006-12-15 06:54:08 +1100506 ehci_writel(ehci, INTR_MASK, &ehci->regs->intr_enable);
Alan Stern15be1052012-07-11 11:21:17 -0400507 (void) ehci_readl(ehci, &ehci->regs->intr_enable);
Alan Stern43fe3a92012-07-11 11:23:16 -0400508 spin_unlock_irq(&ehci->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 return 0;
Alan Stern43fe3a92012-07-11 11:23:16 -0400511
512 shutdown:
513 spin_unlock_irq(&ehci->lock);
514 return -ESHUTDOWN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515}
516
517#else
518
Alan Stern0c0382e2005-10-13 17:08:02 -0400519#define ehci_bus_suspend NULL
520#define ehci_bus_resume NULL
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521
522#endif /* CONFIG_PM */
523
524/*-------------------------------------------------------------------------*/
525
Alan Stern57e06c12007-01-16 11:59:45 -0500526/*
Balaji Rao90da0962007-11-22 01:58:14 +0530527 * Sets the owner of a port
Alan Stern57e06c12007-01-16 11:59:45 -0500528 */
Balaji Rao90da0962007-11-22 01:58:14 +0530529static void set_owner(struct ehci_hcd *ehci, int portnum, int new_owner)
Alan Stern57e06c12007-01-16 11:59:45 -0500530{
Alan Stern57e06c12007-01-16 11:59:45 -0500531 u32 __iomem *status_reg;
532 u32 port_status;
Balaji Rao90da0962007-11-22 01:58:14 +0530533 int try;
Alan Stern57e06c12007-01-16 11:59:45 -0500534
Balaji Rao90da0962007-11-22 01:58:14 +0530535 status_reg = &ehci->regs->port_status[portnum];
Alan Stern57e06c12007-01-16 11:59:45 -0500536
537 /*
538 * The controller won't set the OWNER bit if the port is
539 * enabled, so this loop will sometimes require at least two
540 * iterations: one to disable the port and one to set OWNER.
541 */
Alan Stern57e06c12007-01-16 11:59:45 -0500542 for (try = 4; try > 0; --try) {
543 spin_lock_irq(&ehci->lock);
544 port_status = ehci_readl(ehci, status_reg);
545 if ((port_status & PORT_OWNER) == new_owner
546 || (port_status & (PORT_OWNER | PORT_CONNECT))
547 == 0)
548 try = 0;
549 else {
550 port_status ^= PORT_OWNER;
551 port_status &= ~(PORT_PE | PORT_RWC_BITS);
552 ehci_writel(ehci, port_status, status_reg);
553 }
554 spin_unlock_irq(&ehci->lock);
555 if (try > 1)
556 msleep(5);
557 }
Balaji Rao90da0962007-11-22 01:58:14 +0530558}
559
Alan Stern57e06c12007-01-16 11:59:45 -0500560/*-------------------------------------------------------------------------*/
561
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562static int check_reset_complete (
563 struct ehci_hcd *ehci,
564 int index,
Alan Sterne6316562007-01-16 11:58:00 -0500565 u32 __iomem *status_reg,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 int port_status
567) {
David Brownellcd4cdc92008-01-24 12:39:43 -0800568 if (!(port_status & PORT_CONNECT))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 return port_status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570
571 /* if reset finished and it's still not enabled -- handoff */
572 if (!(port_status & PORT_PE)) {
573
574 /* with integrated TT, there's nobody to hand it to! */
575 if (ehci_is_TDI(ehci)) {
576 ehci_dbg (ehci,
577 "Failed to enable port %d on root hub TT\n",
578 index+1);
579 return port_status;
580 }
581
582 ehci_dbg (ehci, "port %d full speed --> companion\n",
583 index + 1);
584
585 // what happens if HCS_N_CC(params) == 0 ?
586 port_status |= PORT_OWNER;
David Brownell10f65242005-08-31 10:55:38 -0700587 port_status &= ~PORT_RWC_BITS;
Alan Sterne6316562007-01-16 11:58:00 -0500588 ehci_writel(ehci, port_status, status_reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589
Vitaly Bordug796bcae2008-11-09 19:43:30 +0100590 /* ensure 440EPX ohci controller state is operational */
591 if (ehci->has_amcc_usb23)
592 set_ohci_hcfs(ehci, 1);
593 } else {
Peter Chen0ca7eb22012-03-29 09:50:06 +0800594 ehci_dbg(ehci, "port %d reset complete, port enabled\n",
595 index + 1);
Vitaly Bordug796bcae2008-11-09 19:43:30 +0100596 /* ensure 440EPx ohci controller state is suspended */
597 if (ehci->has_amcc_usb23)
598 set_ohci_hcfs(ehci, 0);
599 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600
601 return port_status;
602}
603
604/*-------------------------------------------------------------------------*/
605
606
607/* build "status change" packet (one or two bytes) from HC registers */
608
609static int
610ehci_hub_status_data (struct usb_hcd *hcd, char *buf)
611{
612 struct ehci_hcd *ehci = hcd_to_ehci (hcd);
Alan Sterna448e4d2012-04-03 15:24:30 -0400613 u32 temp, status;
David Brownell93f1a472006-11-16 23:34:58 -0800614 u32 mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 int ports, i, retval = 1;
616 unsigned long flags;
Alan Stern4dd405a2013-03-18 12:05:08 -0400617 u32 ppcd = ~0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 /* init status to no-changes */
620 buf [0] = 0;
621 ports = HCS_N_PORTS (ehci->hcs_params);
622 if (ports > 7) {
623 buf [1] = 0;
624 retval++;
625 }
David Brownell53bd6a62006-08-30 14:50:06 -0700626
Alan Sterna448e4d2012-04-03 15:24:30 -0400627 /* Inform the core about resumes-in-progress by returning
628 * a non-zero value even if there are no status changes.
629 */
630 status = ehci->resuming_ports;
631
David Brownell93f1a472006-11-16 23:34:58 -0800632 /* Some boards (mostly VIA?) report bogus overcurrent indications,
633 * causing massive log spam unless we completely ignore them. It
Robert P. J. Day3a4fa0a2007-10-19 23:10:43 +0200634 * may be relevant that VIA VT8235 controllers, where PORT_POWER is
David Brownell93f1a472006-11-16 23:34:58 -0800635 * always set, seem to clear PORT_OCC and PORT_CSC when writing to
636 * PORT_POWER; that's surprising, but maybe within-spec.
637 */
638 if (!ignore_oc)
639 mask = PORT_CSC | PORT_PEC | PORT_OCC;
640 else
641 mask = PORT_CSC | PORT_PEC;
642 // PORT_RESUME from hardware ~= PORT_STAT_C_SUSPEND
643
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 /* no hub change reports (bit 0) for now (power, ...) */
645
646 /* port N changes (bit N)? */
647 spin_lock_irqsave (&ehci->lock, flags);
Alek Du5a9cdf32010-06-04 15:47:56 +0800648
649 /* get per-port change detect bits */
650 if (ehci->has_ppcd)
651 ppcd = ehci_readl(ehci, &ehci->regs->status) >> 16;
652
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 for (i = 0; i < ports; i++) {
Alek Du5a9cdf32010-06-04 15:47:56 +0800654 /* leverage per-port change bits feature */
Alan Stern4dd405a2013-03-18 12:05:08 -0400655 if (ppcd & (1 << i))
656 temp = ehci_readl(ehci, &ehci->regs->port_status[i]);
657 else
658 temp = 0;
Alan Stern625b5c92007-01-16 11:58:47 -0500659
660 /*
661 * Return status information even for ports with OWNER set.
Petr Mladek37ebb542014-09-19 17:32:23 +0200662 * Otherwise hub_wq wouldn't see the disconnect event when a
Alan Stern625b5c92007-01-16 11:58:47 -0500663 * high-speed device is switched over to the companion
664 * controller by the user.
665 */
666
Alan Sterneafe5b92008-10-06 11:25:53 -0400667 if ((temp & mask) != 0 || test_bit(i, &ehci->port_c_suspend)
668 || (ehci->reset_done[i] && time_after_eq(
669 jiffies, ehci->reset_done[i]))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 if (i < 7)
671 buf [0] |= 1 << (i + 1);
672 else
673 buf [1] |= 1 << (i - 7);
674 status = STS_PCD;
675 }
676 }
Alan Sternee742902013-01-25 17:17:43 -0500677
678 /* If a resume is in progress, make sure it can finish */
679 if (ehci->resuming_ports)
680 mod_timer(&hcd->rh_timer, jiffies + msecs_to_jiffies(25));
681
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 spin_unlock_irqrestore (&ehci->lock, flags);
683 return status ? retval : 0;
684}
685
686/*-------------------------------------------------------------------------*/
687
688static void
689ehci_hub_descriptor (
690 struct ehci_hcd *ehci,
691 struct usb_hub_descriptor *desc
692) {
693 int ports = HCS_N_PORTS (ehci->hcs_params);
694 u16 temp;
695
696 desc->bDescriptorType = 0x29;
697 desc->bPwrOn2PwrGood = 10; /* ehci 1.0, 2.3.9 says 20ms max */
698 desc->bHubContrCurrent = 0;
699
700 desc->bNbrPorts = ports;
701 temp = 1 + (ports / 8);
702 desc->bDescLength = 7 + 2 * temp;
703
704 /* two bitmaps: ports removable, and usb 1.0 legacy PortPwrCtrlMask */
John Youndbe79bb2001-09-17 00:00:00 -0700705 memset(&desc->u.hs.DeviceRemovable[0], 0, temp);
706 memset(&desc->u.hs.DeviceRemovable[temp], 0xff, temp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707
708 temp = 0x0008; /* per-port overcurrent reporting */
709 if (HCS_PPC (ehci->hcs_params))
710 temp |= 0x0001; /* per-port power control */
David Brownell56c1e262005-04-09 09:00:29 -0700711 else
712 temp |= 0x0002; /* no power switching */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713#if 0
714// re-enable when we support USB_PORT_FEAT_INDICATOR below.
715 if (HCS_INDICATOR (ehci->hcs_params))
716 temp |= 0x0080; /* per-port indicators (LEDs) */
717#endif
Al Virofd05e722008-04-28 07:00:16 +0100718 desc->wHubCharacteristics = cpu_to_le16(temp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719}
720
721/*-------------------------------------------------------------------------*/
Jack Pham726a85c2013-08-13 13:40:13 -0700722#ifdef CONFIG_USB_HCD_TEST_MODE
723
Manu Gautam9841f372013-08-08 16:49:24 -0700724#define EHSET_TEST_SINGLE_STEP_SET_FEATURE 0x06
725
726static void usb_ehset_completion(struct urb *urb)
727{
728 struct completion *done = urb->context;
729
730 complete(done);
731}
732static int submit_single_step_set_feature(
733 struct usb_hcd *hcd,
734 struct urb *urb,
735 int is_setup
736);
737
738/*
739 * Allocate and initialize a control URB. This request will be used by the
740 * EHSET SINGLE_STEP_SET_FEATURE test in which the DATA and STATUS stages
741 * of the GetDescriptor request are sent 15 seconds after the SETUP stage.
742 * Return NULL if failed.
743 */
744static struct urb *request_single_step_set_feature_urb(
745 struct usb_device *udev,
746 void *dr,
747 void *buf,
748 struct completion *done
749) {
750 struct urb *urb;
751 struct usb_hcd *hcd = bus_to_hcd(udev->bus);
752 struct usb_host_endpoint *ep;
753
754 urb = usb_alloc_urb(0, GFP_KERNEL);
755 if (!urb)
756 return NULL;
757
758 urb->pipe = usb_rcvctrlpipe(udev, 0);
759 ep = (usb_pipein(urb->pipe) ? udev->ep_in : udev->ep_out)
760 [usb_pipeendpoint(urb->pipe)];
761 if (!ep) {
762 usb_free_urb(urb);
763 return NULL;
764 }
765
766 urb->ep = ep;
767 urb->dev = udev;
768 urb->setup_packet = (void *)dr;
769 urb->transfer_buffer = buf;
770 urb->transfer_buffer_length = USB_DT_DEVICE_SIZE;
771 urb->complete = usb_ehset_completion;
772 urb->status = -EINPROGRESS;
773 urb->actual_length = 0;
774 urb->transfer_flags = URB_DIR_IN;
775 usb_get_urb(urb);
776 atomic_inc(&urb->use_count);
777 atomic_inc(&urb->dev->urbnum);
778 urb->setup_dma = dma_map_single(
779 hcd->self.controller,
780 urb->setup_packet,
781 sizeof(struct usb_ctrlrequest),
782 DMA_TO_DEVICE);
783 urb->transfer_dma = dma_map_single(
784 hcd->self.controller,
785 urb->transfer_buffer,
786 urb->transfer_buffer_length,
787 DMA_FROM_DEVICE);
788 urb->context = done;
789 return urb;
790}
791
792static int ehset_single_step_set_feature(struct usb_hcd *hcd, int port)
793{
794 int retval = -ENOMEM;
795 struct usb_ctrlrequest *dr;
796 struct urb *urb;
797 struct usb_device *udev;
798 struct ehci_hcd *ehci = hcd_to_ehci(hcd);
799 struct usb_device_descriptor *buf;
800 DECLARE_COMPLETION_ONSTACK(done);
801
802 /* Obtain udev of the rhub's child port */
803 udev = usb_hub_find_child(hcd->self.root_hub, port);
804 if (!udev) {
805 ehci_err(ehci, "No device attached to the RootHub\n");
806 return -ENODEV;
807 }
808 buf = kmalloc(USB_DT_DEVICE_SIZE, GFP_KERNEL);
809 if (!buf)
810 return -ENOMEM;
811
812 dr = kmalloc(sizeof(struct usb_ctrlrequest), GFP_KERNEL);
813 if (!dr) {
814 kfree(buf);
815 return -ENOMEM;
816 }
817
818 /* Fill Setup packet for GetDescriptor */
819 dr->bRequestType = USB_DIR_IN;
820 dr->bRequest = USB_REQ_GET_DESCRIPTOR;
821 dr->wValue = cpu_to_le16(USB_DT_DEVICE << 8);
822 dr->wIndex = 0;
823 dr->wLength = cpu_to_le16(USB_DT_DEVICE_SIZE);
824 urb = request_single_step_set_feature_urb(udev, dr, buf, &done);
825 if (!urb)
826 goto cleanup;
827
828 /* Submit just the SETUP stage */
829 retval = submit_single_step_set_feature(hcd, urb, 1);
830 if (retval)
831 goto out1;
832 if (!wait_for_completion_timeout(&done, msecs_to_jiffies(2000))) {
833 usb_kill_urb(urb);
834 retval = -ETIMEDOUT;
835 ehci_err(ehci, "%s SETUP stage timed out on ep0\n", __func__);
836 goto out1;
837 }
838 msleep(15 * 1000);
839
840 /* Complete remaining DATA and STATUS stages using the same URB */
841 urb->status = -EINPROGRESS;
842 usb_get_urb(urb);
843 atomic_inc(&urb->use_count);
844 atomic_inc(&urb->dev->urbnum);
845 retval = submit_single_step_set_feature(hcd, urb, 0);
846 if (!retval && !wait_for_completion_timeout(&done,
847 msecs_to_jiffies(2000))) {
848 usb_kill_urb(urb);
849 retval = -ETIMEDOUT;
850 ehci_err(ehci, "%s IN stage timed out on ep0\n", __func__);
851 }
852out1:
853 usb_free_urb(urb);
854cleanup:
855 kfree(dr);
856 kfree(buf);
857 return retval;
858}
Jack Pham726a85c2013-08-13 13:40:13 -0700859#endif /* CONFIG_USB_HCD_TEST_MODE */
Manu Gautam9841f372013-08-08 16:49:24 -0700860/*-------------------------------------------------------------------------*/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861
Laurent Pinchart37769932014-04-16 18:00:10 +0200862int ehci_hub_control(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 struct usb_hcd *hcd,
864 u16 typeReq,
865 u16 wValue,
866 u16 wIndex,
867 char *buf,
868 u16 wLength
869) {
870 struct ehci_hcd *ehci = hcd_to_ehci (hcd);
871 int ports = HCS_N_PORTS (ehci->hcs_params);
Alan Stern383975d2007-05-04 11:52:40 -0400872 u32 __iomem *status_reg = &ehci->regs->port_status[
873 (wIndex & 0xff) - 1];
Alan Sterna46af4e2012-06-25 12:19:03 -0400874 u32 __iomem *hostpc_reg = &ehci->regs->hostpc[(wIndex & 0xff) - 1];
Alek Du331ac6b2009-07-13 12:41:20 +0800875 u32 temp, temp1, status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 unsigned long flags;
877 int retval = 0;
David Brownellf0d7f272006-11-16 23:56:15 -0800878 unsigned selector;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879
880 /*
881 * FIXME: support SetPortFeatures USB_PORT_FEAT_INDICATOR.
882 * HCS_INDICATOR may say we can change LEDs to off/amber/green.
883 * (track current state ourselves) ... blink for diagnostics,
884 * power, "this is the one", etc. EHCI spec supports this.
885 */
886
887 spin_lock_irqsave (&ehci->lock, flags);
888 switch (typeReq) {
889 case ClearHubFeature:
890 switch (wValue) {
891 case C_HUB_LOCAL_POWER:
892 case C_HUB_OVER_CURRENT:
893 /* no hub-wide feature/status flags */
894 break;
895 default:
896 goto error;
897 }
898 break;
899 case ClearPortFeature:
900 if (!wIndex || wIndex > ports)
901 goto error;
902 wIndex--;
Alan Sterne6316562007-01-16 11:58:00 -0500903 temp = ehci_readl(ehci, status_reg);
Stephen Warren6d5f89c2012-04-18 15:32:46 -0600904 temp &= ~PORT_RWC_BITS;
Alan Stern625b5c92007-01-16 11:58:47 -0500905
906 /*
907 * Even if OWNER is set, so the port is owned by the
Petr Mladek37ebb542014-09-19 17:32:23 +0200908 * companion controller, hub_wq needs to be able to clear
Alan Stern625b5c92007-01-16 11:58:47 -0500909 * the port-change status bits (especially
Alan Stern749da5f2010-03-04 17:05:08 -0500910 * USB_PORT_STAT_C_CONNECTION).
Alan Stern625b5c92007-01-16 11:58:47 -0500911 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912
913 switch (wValue) {
914 case USB_PORT_FEAT_ENABLE:
Alan Sterne6316562007-01-16 11:58:00 -0500915 ehci_writel(ehci, temp & ~PORT_PE, status_reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 break;
917 case USB_PORT_FEAT_C_ENABLE:
Stephen Warren6d5f89c2012-04-18 15:32:46 -0600918 ehci_writel(ehci, temp | PORT_PEC, status_reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919 break;
920 case USB_PORT_FEAT_SUSPEND:
921 if (temp & PORT_RESET)
922 goto error;
David Brownellf8aeb3b2006-01-20 13:55:14 -0800923 if (ehci->no_selective_suspend)
924 break;
Anatolij Gustschin83722bc2011-04-18 22:02:00 +0200925#ifdef CONFIG_USB_OTG
926 if ((hcd->self.otg_port == (wIndex + 1))
927 && hcd->self.b_hnp_enable) {
Antoine Tenart3d46e732014-09-24 23:05:50 +0400928 otg_start_hnp(hcd->usb_phy->otg);
Anatolij Gustschin83722bc2011-04-18 22:02:00 +0200929 break;
930 }
931#endif
Alan Stern16032c42010-05-12 18:21:35 -0400932 if (!(temp & PORT_SUSPEND))
933 break;
934 if ((temp & PORT_PE) == 0)
935 goto error;
936
937 /* clear phy low-power mode before resume */
Tuomas Tynkkynen2cdcec42013-08-12 16:06:49 +0300938 if (ehci->has_tdi_phy_lpm) {
Alan Stern16032c42010-05-12 18:21:35 -0400939 temp1 = ehci_readl(ehci, hostpc_reg);
940 ehci_writel(ehci, temp1 & ~HOSTPC_PHCD,
Alek Dueab80de2010-05-10 11:17:49 +0800941 hostpc_reg);
Alan Stern16032c42010-05-12 18:21:35 -0400942 spin_unlock_irqrestore(&ehci->lock, flags);
943 msleep(5);/* wait to leave low-power mode */
944 spin_lock_irqsave(&ehci->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 }
Alan Stern16032c42010-05-12 18:21:35 -0400946 /* resume signaling for 20 msec */
Stephen Warren6d5f89c2012-04-18 15:32:46 -0600947 temp &= ~PORT_WAKE_BITS;
Alan Stern16032c42010-05-12 18:21:35 -0400948 ehci_writel(ehci, temp | PORT_RESUME, status_reg);
949 ehci->reset_done[wIndex] = jiffies
Felipe Balbi4062a4e2015-02-13 14:42:25 -0600950 + msecs_to_jiffies(USB_RESUME_TIMEOUT);
Alan Stern3a204462013-08-01 17:09:23 -0400951 set_bit(wIndex, &ehci->resuming_ports);
952 usb_hcd_start_port_resume(&hcd->self, wIndex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953 break;
954 case USB_PORT_FEAT_C_SUSPEND:
Alan Sternd1f114d2008-05-20 16:58:58 -0400955 clear_bit(wIndex, &ehci->port_c_suspend);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 break;
957 case USB_PORT_FEAT_POWER:
958 if (HCS_PPC (ehci->hcs_params))
Stephen Warren6d5f89c2012-04-18 15:32:46 -0600959 ehci_writel(ehci, temp & ~PORT_POWER,
960 status_reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 break;
962 case USB_PORT_FEAT_C_CONNECTION:
Stephen Warren6d5f89c2012-04-18 15:32:46 -0600963 ehci_writel(ehci, temp | PORT_CSC, status_reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964 break;
965 case USB_PORT_FEAT_C_OVER_CURRENT:
Stephen Warren6d5f89c2012-04-18 15:32:46 -0600966 ehci_writel(ehci, temp | PORT_OCC, status_reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 break;
968 case USB_PORT_FEAT_C_RESET:
969 /* GetPortStatus clears reset */
970 break;
971 default:
972 goto error;
973 }
Benjamin Herrenschmidt083522d2006-12-15 06:54:08 +1100974 ehci_readl(ehci, &ehci->regs->command); /* unblock posted write */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 break;
976 case GetHubDescriptor:
977 ehci_hub_descriptor (ehci, (struct usb_hub_descriptor *)
978 buf);
979 break;
980 case GetHubStatus:
981 /* no hub-wide feature/status flags */
982 memset (buf, 0, 4);
983 //cpu_to_le32s ((u32 *) buf);
984 break;
985 case GetPortStatus:
986 if (!wIndex || wIndex > ports)
987 goto error;
988 wIndex--;
989 status = 0;
Alan Sterne6316562007-01-16 11:58:00 -0500990 temp = ehci_readl(ehci, status_reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991
992 // wPortChange bits
993 if (temp & PORT_CSC)
Alan Stern749da5f2010-03-04 17:05:08 -0500994 status |= USB_PORT_STAT_C_CONNECTION << 16;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995 if (temp & PORT_PEC)
Alan Stern749da5f2010-03-04 17:05:08 -0500996 status |= USB_PORT_STAT_C_ENABLE << 16;
Christian Engelmayer756aa6b2007-05-30 11:04:48 -0700997
998 if ((temp & PORT_OCC) && !ignore_oc){
Alan Stern749da5f2010-03-04 17:05:08 -0500999 status |= USB_PORT_STAT_C_OVERCURRENT << 16;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000
Christian Engelmayer756aa6b2007-05-30 11:04:48 -07001001 /*
1002 * Hubs should disable port power on over-current.
1003 * However, not all EHCI implementations do this
1004 * automatically, even if they _do_ support per-port
1005 * power switching; they're allowed to just limit the
Petr Mladek37ebb542014-09-19 17:32:23 +02001006 * current. hub_wq will turn the power back on.
Christian Engelmayer756aa6b2007-05-30 11:04:48 -07001007 */
Christian Engelmayere6604a72013-04-03 12:18:51 +02001008 if (((temp & PORT_OC) || (ehci->need_oc_pp_cycle))
1009 && HCS_PPC(ehci->hcs_params)) {
Christian Engelmayer756aa6b2007-05-30 11:04:48 -07001010 ehci_writel(ehci,
1011 temp & ~(PORT_RWC_BITS | PORT_POWER),
1012 status_reg);
Sergei Shtylyov81463c12011-07-06 23:19:38 +04001013 temp = ehci_readl(ehci, status_reg);
Christian Engelmayer756aa6b2007-05-30 11:04:48 -07001014 }
1015 }
1016
Alan Stern6753f4c2013-08-01 17:09:28 -04001017 /* no reset or resume pending */
1018 if (!ehci->reset_done[wIndex]) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019
Alan Stern629e4422007-01-22 16:08:53 -05001020 /* Remote Wakeup received? */
Alan Stern6753f4c2013-08-01 17:09:28 -04001021 if (temp & PORT_RESUME) {
Alan Stern629e4422007-01-22 16:08:53 -05001022 /* resume signaling for 20 msec */
1023 ehci->reset_done[wIndex] = jiffies
1024 + msecs_to_jiffies(20);
Alan Sternf292e7f2013-01-25 17:09:49 -05001025 usb_hcd_start_port_resume(&hcd->self, wIndex);
Roger Quadros47a64a12013-07-09 17:03:50 +03001026 set_bit(wIndex, &ehci->resuming_ports);
Alan Stern629e4422007-01-22 16:08:53 -05001027 /* check the port again */
1028 mod_timer(&ehci_to_hcd(ehci)->rh_timer,
1029 ehci->reset_done[wIndex]);
1030 }
1031
Alan Stern6753f4c2013-08-01 17:09:28 -04001032 /* reset or resume not yet complete */
1033 } else if (!time_after_eq(jiffies, ehci->reset_done[wIndex])) {
1034 ; /* wait until it is complete */
Alan Stern629e4422007-01-22 16:08:53 -05001035
Alan Stern6753f4c2013-08-01 17:09:28 -04001036 /* resume completed */
1037 } else if (test_bit(wIndex, &ehci->resuming_ports)) {
1038 clear_bit(wIndex, &ehci->suspended_ports);
1039 set_bit(wIndex, &ehci->port_c_suspend);
1040 ehci->reset_done[wIndex] = 0;
1041 usb_hcd_end_port_resume(&hcd->self, wIndex);
1042
1043 /* stop resume signaling */
1044 temp &= ~(PORT_RWC_BITS | PORT_SUSPEND | PORT_RESUME);
1045 ehci_writel(ehci, temp, status_reg);
1046 clear_bit(wIndex, &ehci->resuming_ports);
1047 retval = ehci_handshake(ehci, status_reg,
1048 PORT_RESUME, 0, 2000 /* 2msec */);
1049 if (retval != 0) {
1050 ehci_err(ehci, "port %d resume error %d\n",
Alan Stern629e4422007-01-22 16:08:53 -05001051 wIndex + 1, retval);
Alan Stern6753f4c2013-08-01 17:09:28 -04001052 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053 }
Alan Stern6753f4c2013-08-01 17:09:28 -04001054 temp = ehci_readl(ehci, status_reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055
1056 /* whoever resets must GetPortStatus to complete it!! */
Alan Stern6753f4c2013-08-01 17:09:28 -04001057 } else {
Alan Stern749da5f2010-03-04 17:05:08 -05001058 status |= USB_PORT_STAT_C_RESET << 16;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059 ehci->reset_done [wIndex] = 0;
1060
1061 /* force reset to complete */
Benjamin Herrenschmidt083522d2006-12-15 06:54:08 +11001062 ehci_writel(ehci, temp & ~(PORT_RWC_BITS | PORT_RESET),
Alan Sterne6316562007-01-16 11:58:00 -05001063 status_reg);
David Brownellc22fa3a2005-06-13 07:15:28 -07001064 /* REVISIT: some hardware needs 550+ usec to clear
1065 * this bit; seems too long to spin routinely...
1066 */
Manjunath Goudar2f3a6b82013-06-13 11:24:09 -06001067 retval = ehci_handshake(ehci, status_reg,
Dinh Nguyen6307e092010-04-13 11:13:15 -05001068 PORT_RESET, 0, 1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 if (retval != 0) {
1070 ehci_err (ehci, "port %d reset error %d\n",
1071 wIndex + 1, retval);
1072 goto error;
1073 }
1074
1075 /* see what we found out */
Alan Sterne6316562007-01-16 11:58:00 -05001076 temp = check_reset_complete (ehci, wIndex, status_reg,
1077 ehci_readl(ehci, status_reg));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078 }
1079
Alan Stern57e06c12007-01-16 11:59:45 -05001080 /* transfer dedicated ports to the companion hc */
1081 if ((temp & PORT_CONNECT) &&
1082 test_bit(wIndex, &ehci->companion_ports)) {
1083 temp &= ~PORT_RWC_BITS;
1084 temp |= PORT_OWNER;
1085 ehci_writel(ehci, temp, status_reg);
1086 ehci_dbg(ehci, "port %d --> companion\n", wIndex + 1);
1087 temp = ehci_readl(ehci, status_reg);
1088 }
1089
Alan Stern625b5c92007-01-16 11:58:47 -05001090 /*
Petr Mladek37ebb542014-09-19 17:32:23 +02001091 * Even if OWNER is set, there's no harm letting hub_wq
Alan Stern625b5c92007-01-16 11:58:47 -05001092 * see the wPortStatus values (they should all be 0 except
1093 * for PORT_POWER anyway).
1094 */
1095
1096 if (temp & PORT_CONNECT) {
Alan Stern749da5f2010-03-04 17:05:08 -05001097 status |= USB_PORT_STAT_CONNECTION;
Alan Stern625b5c92007-01-16 11:58:47 -05001098 // status may be from integrated TT
Alek Du331ac6b2009-07-13 12:41:20 +08001099 if (ehci->has_hostpc) {
1100 temp1 = ehci_readl(ehci, hostpc_reg);
1101 status |= ehci_port_speed(ehci, temp1);
1102 } else
1103 status |= ehci_port_speed(ehci, temp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104 }
Alan Stern625b5c92007-01-16 11:58:47 -05001105 if (temp & PORT_PE)
Alan Stern749da5f2010-03-04 17:05:08 -05001106 status |= USB_PORT_STAT_ENABLE;
Alan Sterneafe5b92008-10-06 11:25:53 -04001107
1108 /* maybe the port was unsuspended without our knowledge */
1109 if (temp & (PORT_SUSPEND|PORT_RESUME)) {
Alan Stern749da5f2010-03-04 17:05:08 -05001110 status |= USB_PORT_STAT_SUSPEND;
Alan Sterneafe5b92008-10-06 11:25:53 -04001111 } else if (test_bit(wIndex, &ehci->suspended_ports)) {
1112 clear_bit(wIndex, &ehci->suspended_ports);
Alan Sterna448e4d2012-04-03 15:24:30 -04001113 clear_bit(wIndex, &ehci->resuming_ports);
Alan Sterneafe5b92008-10-06 11:25:53 -04001114 ehci->reset_done[wIndex] = 0;
1115 if (temp & PORT_PE)
1116 set_bit(wIndex, &ehci->port_c_suspend);
Alan Sternf292e7f2013-01-25 17:09:49 -05001117 usb_hcd_end_port_resume(&hcd->self, wIndex);
Alan Sterneafe5b92008-10-06 11:25:53 -04001118 }
1119
Alan Stern625b5c92007-01-16 11:58:47 -05001120 if (temp & PORT_OC)
Alan Stern749da5f2010-03-04 17:05:08 -05001121 status |= USB_PORT_STAT_OVERCURRENT;
Alan Stern625b5c92007-01-16 11:58:47 -05001122 if (temp & PORT_RESET)
Alan Stern749da5f2010-03-04 17:05:08 -05001123 status |= USB_PORT_STAT_RESET;
Alan Stern625b5c92007-01-16 11:58:47 -05001124 if (temp & PORT_POWER)
Alan Stern749da5f2010-03-04 17:05:08 -05001125 status |= USB_PORT_STAT_POWER;
Alan Sternd1f114d2008-05-20 16:58:58 -04001126 if (test_bit(wIndex, &ehci->port_c_suspend))
Alan Stern749da5f2010-03-04 17:05:08 -05001127 status |= USB_PORT_STAT_C_SUSPEND << 16;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128
Oliver Neukumbbcd5ca2013-11-18 13:23:01 +01001129 if (status & ~0xffff) /* only if wPortChange is interesting */
1130 dbg_port(ehci, "GetStatus", wIndex + 1, temp);
Harvey Harrisona5abdea2008-04-29 01:03:40 -07001131 put_unaligned_le32(status, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132 break;
1133 case SetHubFeature:
1134 switch (wValue) {
1135 case C_HUB_LOCAL_POWER:
1136 case C_HUB_OVER_CURRENT:
1137 /* no hub-wide feature/status flags */
1138 break;
1139 default:
1140 goto error;
1141 }
1142 break;
1143 case SetPortFeature:
David Brownellf0d7f272006-11-16 23:56:15 -08001144 selector = wIndex >> 8;
1145 wIndex &= 0xff;
Jason Wessel8d053c72009-08-20 15:39:54 -05001146 if (unlikely(ehci->debug)) {
1147 /* If the debug port is active any port
1148 * feature requests should get denied */
1149 if (wIndex == HCS_DEBUG_PORT(ehci->hcs_params) &&
1150 (readl(&ehci->debug->control) & DBGP_ENABLED)) {
1151 retval = -ENODEV;
1152 goto error_exit;
1153 }
1154 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155 if (!wIndex || wIndex > ports)
1156 goto error;
1157 wIndex--;
Alan Sterne6316562007-01-16 11:58:00 -05001158 temp = ehci_readl(ehci, status_reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159 if (temp & PORT_OWNER)
1160 break;
1161
David Brownell10f65242005-08-31 10:55:38 -07001162 temp &= ~PORT_RWC_BITS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163 switch (wValue) {
1164 case USB_PORT_FEAT_SUSPEND:
David Brownellf8aeb3b2006-01-20 13:55:14 -08001165 if (ehci->no_selective_suspend)
1166 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167 if ((temp & PORT_PE) == 0
1168 || (temp & PORT_RESET) != 0)
1169 goto error;
Alek Dub9df79422010-01-19 16:31:31 +08001170
Alek Du331ac6b2009-07-13 12:41:20 +08001171 /* After above check the port must be connected.
1172 * Set appropriate bit thus could put phy into low power
Tuomas Tynkkynen2cdcec42013-08-12 16:06:49 +03001173 * mode if we have tdi_phy_lpm feature
Alek Du331ac6b2009-07-13 12:41:20 +08001174 */
Alek Dub9df79422010-01-19 16:31:31 +08001175 temp &= ~PORT_WKCONN_E;
1176 temp |= PORT_WKDISC_E | PORT_WKOC_E;
1177 ehci_writel(ehci, temp | PORT_SUSPEND, status_reg);
Tuomas Tynkkynen2cdcec42013-08-12 16:06:49 +03001178 if (ehci->has_tdi_phy_lpm) {
Alek Dub9df79422010-01-19 16:31:31 +08001179 spin_unlock_irqrestore(&ehci->lock, flags);
Alek Du331ac6b2009-07-13 12:41:20 +08001180 msleep(5);/* 5ms for HCD enter low pwr mode */
Alek Dub9df79422010-01-19 16:31:31 +08001181 spin_lock_irqsave(&ehci->lock, flags);
Alek Du331ac6b2009-07-13 12:41:20 +08001182 temp1 = ehci_readl(ehci, hostpc_reg);
1183 ehci_writel(ehci, temp1 | HOSTPC_PHCD,
1184 hostpc_reg);
1185 temp1 = ehci_readl(ehci, hostpc_reg);
1186 ehci_dbg(ehci, "Port%d phy low pwr mode %s\n",
1187 wIndex, (temp1 & HOSTPC_PHCD) ?
1188 "succeeded" : "failed");
1189 }
Alan Sterneafe5b92008-10-06 11:25:53 -04001190 set_bit(wIndex, &ehci->suspended_ports);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191 break;
1192 case USB_PORT_FEAT_POWER:
1193 if (HCS_PPC (ehci->hcs_params))
Benjamin Herrenschmidt083522d2006-12-15 06:54:08 +11001194 ehci_writel(ehci, temp | PORT_POWER,
Alan Sterne6316562007-01-16 11:58:00 -05001195 status_reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196 break;
1197 case USB_PORT_FEAT_RESET:
Alan Stern6753f4c2013-08-01 17:09:28 -04001198 if (temp & (PORT_SUSPEND|PORT_RESUME))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199 goto error;
1200 /* line status bits may report this as low speed,
1201 * which can be fine if this root hub has a
1202 * transaction translator built in.
1203 */
1204 if ((temp & (PORT_PE|PORT_CONNECT)) == PORT_CONNECT
1205 && !ehci_is_TDI(ehci)
1206 && PORT_USB11 (temp)) {
1207 ehci_dbg (ehci,
1208 "port %d low speed --> companion\n",
1209 wIndex + 1);
1210 temp |= PORT_OWNER;
1211 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212 temp |= PORT_RESET;
1213 temp &= ~PORT_PE;
1214
1215 /*
1216 * caller must wait, then call GetPortStatus
1217 * usb 2.0 spec says 50 ms resets on root
1218 */
1219 ehci->reset_done [wIndex] = jiffies
1220 + msecs_to_jiffies (50);
1221 }
Alan Sterne6316562007-01-16 11:58:00 -05001222 ehci_writel(ehci, temp, status_reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223 break;
David Brownellf0d7f272006-11-16 23:56:15 -08001224
1225 /* For downstream facing ports (these): one hub port is put
1226 * into test mode according to USB2 11.24.2.13, then the hub
1227 * must be reset (which for root hub now means rmmod+modprobe,
1228 * or else system reboot). See EHCI 2.3.9 and 4.14 for info
1229 * about the EHCI-specific stuff.
1230 */
1231 case USB_PORT_FEAT_TEST:
Jack Pham726a85c2013-08-13 13:40:13 -07001232#ifdef CONFIG_USB_HCD_TEST_MODE
Manu Gautam9841f372013-08-08 16:49:24 -07001233 if (selector == EHSET_TEST_SINGLE_STEP_SET_FEATURE) {
1234 spin_unlock_irqrestore(&ehci->lock, flags);
1235 retval = ehset_single_step_set_feature(hcd,
Peter Chen5cbcc352014-08-05 08:28:19 +08001236 wIndex + 1);
Manu Gautam9841f372013-08-08 16:49:24 -07001237 spin_lock_irqsave(&ehci->lock, flags);
1238 break;
Jack Pham726a85c2013-08-13 13:40:13 -07001239 }
1240#endif
1241 if (!selector || selector > 5)
David Brownellf0d7f272006-11-16 23:56:15 -08001242 goto error;
Alan Sternc4f34762012-07-11 11:23:10 -04001243 spin_unlock_irqrestore(&ehci->lock, flags);
David Brownellf0d7f272006-11-16 23:56:15 -08001244 ehci_quiesce(ehci);
Alan Sternc4f34762012-07-11 11:23:10 -04001245 spin_lock_irqsave(&ehci->lock, flags);
Boris Todorov77636c82011-07-11 12:03:33 +03001246
1247 /* Put all enabled ports into suspend */
1248 while (ports--) {
1249 u32 __iomem *sreg =
1250 &ehci->regs->port_status[ports];
1251
1252 temp = ehci_readl(ehci, sreg) & ~PORT_RWC_BITS;
1253 if (temp & PORT_PE)
1254 ehci_writel(ehci, temp | PORT_SUSPEND,
1255 sreg);
1256 }
Alan Sternc4f34762012-07-11 11:23:10 -04001257
1258 spin_unlock_irqrestore(&ehci->lock, flags);
David Brownellf0d7f272006-11-16 23:56:15 -08001259 ehci_halt(ehci);
Alan Sternc4f34762012-07-11 11:23:10 -04001260 spin_lock_irqsave(&ehci->lock, flags);
1261
Boris Todorov77636c82011-07-11 12:03:33 +03001262 temp = ehci_readl(ehci, status_reg);
David Brownellf0d7f272006-11-16 23:56:15 -08001263 temp |= selector << 16;
Alan Sterne6316562007-01-16 11:58:00 -05001264 ehci_writel(ehci, temp, status_reg);
David Brownellf0d7f272006-11-16 23:56:15 -08001265 break;
1266
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267 default:
1268 goto error;
1269 }
Benjamin Herrenschmidt083522d2006-12-15 06:54:08 +11001270 ehci_readl(ehci, &ehci->regs->command); /* unblock posted writes */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271 break;
1272
1273 default:
1274error:
1275 /* "stall" on error */
1276 retval = -EPIPE;
1277 }
Jason Wessel8d053c72009-08-20 15:39:54 -05001278error_exit:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279 spin_unlock_irqrestore (&ehci->lock, flags);
1280 return retval;
1281}
Laurent Pinchart37769932014-04-16 18:00:10 +02001282EXPORT_SYMBOL_GPL(ehci_hub_control);
Balaji Rao90da0962007-11-22 01:58:14 +05301283
Alan Stern3e023202012-11-01 11:12:58 -04001284static void ehci_relinquish_port(struct usb_hcd *hcd, int portnum)
Balaji Rao90da0962007-11-22 01:58:14 +05301285{
1286 struct ehci_hcd *ehci = hcd_to_ehci(hcd);
1287
1288 if (ehci_is_TDI(ehci))
1289 return;
1290 set_owner(ehci, --portnum, PORT_OWNER);
1291}
1292
Alan Stern3e023202012-11-01 11:12:58 -04001293static int ehci_port_handed_over(struct usb_hcd *hcd, int portnum)
Alan Stern3a311552008-05-20 16:58:29 -04001294{
1295 struct ehci_hcd *ehci = hcd_to_ehci(hcd);
1296 u32 __iomem *reg;
1297
1298 if (ehci_is_TDI(ehci))
1299 return 0;
1300 reg = &ehci->regs->port_status[portnum - 1];
1301 return ehci_readl(ehci, reg) & PORT_OWNER;
1302}