blob: 0931f5a7dec4c288043fd310b40a327a69197001 [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/*-------------------------------------------------------------------------*/
30
Alan Stern58a97ff2008-04-14 12:17:10 -040031#define PORT_WAKE_BITS (PORT_WKOC_E|PORT_WKDISC_E|PORT_WKCONN_E)
32
Alan Sternaff6d182008-04-18 11:11:26 -040033#ifdef CONFIG_PM
34
Alan Stern383975d2007-05-04 11:52:40 -040035static int ehci_hub_control(
36 struct usb_hcd *hcd,
37 u16 typeReq,
38 u16 wValue,
39 u16 wIndex,
40 char *buf,
41 u16 wLength
42);
43
44/* After a power loss, ports that were owned by the companion must be
45 * reset so that the companion can still own them.
46 */
47static void ehci_handover_companion_ports(struct ehci_hcd *ehci)
48{
49 u32 __iomem *reg;
50 u32 status;
51 int port;
52 __le32 buf;
53 struct usb_hcd *hcd = ehci_to_hcd(ehci);
54
55 if (!ehci->owned_ports)
56 return;
57
58 /* Give the connections some time to appear */
59 msleep(20);
60
61 port = HCS_N_PORTS(ehci->hcs_params);
62 while (port--) {
63 if (test_bit(port, &ehci->owned_ports)) {
64 reg = &ehci->regs->port_status[port];
Alan Stern3c519b82007-05-04 11:55:31 -040065 status = ehci_readl(ehci, reg) & ~PORT_RWC_BITS;
Alan Stern383975d2007-05-04 11:52:40 -040066
67 /* Port already owned by companion? */
68 if (status & PORT_OWNER)
69 clear_bit(port, &ehci->owned_ports);
Alan Stern3c519b82007-05-04 11:55:31 -040070 else if (test_bit(port, &ehci->companion_ports))
71 ehci_writel(ehci, status & ~PORT_PE, reg);
Alan Stern383975d2007-05-04 11:52:40 -040072 else
73 ehci_hub_control(hcd, SetPortFeature,
74 USB_PORT_FEAT_RESET, port + 1,
75 NULL, 0);
76 }
77 }
78
79 if (!ehci->owned_ports)
80 return;
81 msleep(90); /* Wait for resets to complete */
82
83 port = HCS_N_PORTS(ehci->hcs_params);
84 while (port--) {
85 if (test_bit(port, &ehci->owned_ports)) {
86 ehci_hub_control(hcd, GetPortStatus,
87 0, port + 1,
88 (char *) &buf, sizeof(buf));
89
90 /* The companion should now own the port,
91 * but if something went wrong the port must not
92 * remain enabled.
93 */
94 reg = &ehci->regs->port_status[port];
95 status = ehci_readl(ehci, reg) & ~PORT_RWC_BITS;
96 if (status & PORT_OWNER)
97 ehci_writel(ehci, status | PORT_CSC, reg);
98 else {
99 ehci_dbg(ehci, "failed handover port %d: %x\n",
100 port + 1, status);
101 ehci_writel(ehci, status & ~PORT_PE, reg);
102 }
103 }
104 }
105
106 ehci->owned_ports = 0;
107}
108
Alan Stern16032c42010-05-12 18:21:35 -0400109static void ehci_adjust_port_wakeup_flags(struct ehci_hcd *ehci,
110 bool suspending)
111{
112 int port;
113 u32 temp;
114
115 /* If remote wakeup is enabled for the root hub but disabled
116 * for the controller, we must adjust all the port wakeup flags
117 * when the controller is suspended or resumed. In all other
118 * cases they don't need to be changed.
119 */
120 if (!ehci_to_hcd(ehci)->self.root_hub->do_remote_wakeup ||
121 device_may_wakeup(ehci_to_hcd(ehci)->self.controller))
122 return;
123
124 /* clear phy low-power mode before changing wakeup flags */
125 if (ehci->has_hostpc) {
126 port = HCS_N_PORTS(ehci->hcs_params);
127 while (port--) {
128 u32 __iomem *hostpc_reg;
129
130 hostpc_reg = (u32 __iomem *)((u8 *) ehci->regs
131 + HOSTPC0 + 4 * port);
132 temp = ehci_readl(ehci, hostpc_reg);
133 ehci_writel(ehci, temp & ~HOSTPC_PHCD, hostpc_reg);
134 }
135 msleep(5);
136 }
137
138 port = HCS_N_PORTS(ehci->hcs_params);
139 while (port--) {
140 u32 __iomem *reg = &ehci->regs->port_status[port];
141 u32 t1 = ehci_readl(ehci, reg) & ~PORT_RWC_BITS;
142 u32 t2 = t1 & ~PORT_WAKE_BITS;
143
144 /* If we are suspending the controller, clear the flags.
145 * If we are resuming the controller, set the wakeup flags.
146 */
147 if (!suspending) {
148 if (t1 & PORT_CONNECT)
149 t2 |= PORT_WKOC_E | PORT_WKDISC_E;
150 else
151 t2 |= PORT_WKOC_E | PORT_WKCONN_E;
152 }
153 ehci_vdbg(ehci, "port %d, %08x -> %08x\n",
154 port + 1, t1, t2);
155 ehci_writel(ehci, t2, reg);
156 }
157
158 /* enter phy low-power mode again */
159 if (ehci->has_hostpc) {
160 port = HCS_N_PORTS(ehci->hcs_params);
161 while (port--) {
162 u32 __iomem *hostpc_reg;
163
164 hostpc_reg = (u32 __iomem *)((u8 *) ehci->regs
165 + HOSTPC0 + 4 * port);
166 temp = ehci_readl(ehci, hostpc_reg);
167 ehci_writel(ehci, temp | HOSTPC_PHCD, hostpc_reg);
168 }
169 }
170}
171
Alan Stern0c0382e2005-10-13 17:08:02 -0400172static int ehci_bus_suspend (struct usb_hcd *hcd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173{
174 struct ehci_hcd *ehci = hcd_to_ehci (hcd);
175 int port;
Alan Stern8c033562006-11-09 14:42:16 -0500176 int mask;
Alan Stern16032c42010-05-12 18:21:35 -0400177 int changed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178
Alan Stern8c774fe2007-02-01 16:09:59 -0500179 ehci_dbg(ehci, "suspend root hub\n");
180
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 if (time_before (jiffies, ehci->next_statechange))
182 msleep(5);
Alan Sternf8fa7572008-01-10 16:43:15 -0500183 del_timer_sync(&ehci->watchdog);
184 del_timer_sync(&ehci->iaa_watchdog);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 spin_lock_irq (&ehci->lock);
187
Alan Sterncec3a532010-01-08 11:18:20 -0500188 /* Once the controller is stopped, port resumes that are already
189 * in progress won't complete. Hence if remote wakeup is enabled
190 * for the root hub and any ports are in the middle of a resume or
191 * remote wakeup, we must fail the suspend.
192 */
193 if (hcd->self.root_hub->do_remote_wakeup) {
194 port = HCS_N_PORTS(ehci->hcs_params);
195 while (port--) {
196 if (ehci->reset_done[port] != 0) {
197 spin_unlock_irq(&ehci->lock);
198 ehci_dbg(ehci, "suspend failed because "
199 "port %d is resuming\n",
200 port + 1);
201 return -EBUSY;
202 }
203 }
204 }
205
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 /* stop schedules, clean any completed work */
207 if (HC_IS_RUNNING(hcd->state)) {
208 ehci_quiesce (ehci);
209 hcd->state = HC_STATE_QUIESCING;
210 }
Benjamin Herrenschmidt083522d2006-12-15 06:54:08 +1100211 ehci->command = ehci_readl(ehci, &ehci->regs->command);
David Howells7d12e782006-10-05 14:55:46 +0100212 ehci_work(ehci);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213
Alan Stern8c033562006-11-09 14:42:16 -0500214 /* Unlike other USB host controller types, EHCI doesn't have
215 * any notion of "global" or bus-wide suspend. The driver has
216 * to manually suspend all the active unsuspended ports, and
217 * then manually resume them in the bus_resume() routine.
218 */
219 ehci->bus_suspended = 0;
Alan Stern383975d2007-05-04 11:52:40 -0400220 ehci->owned_ports = 0;
Alan Stern16032c42010-05-12 18:21:35 -0400221 changed = 0;
Alan Sterncec3a532010-01-08 11:18:20 -0500222 port = HCS_N_PORTS(ehci->hcs_params);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 while (port--) {
224 u32 __iomem *reg = &ehci->regs->port_status [port];
Benjamin Herrenschmidt083522d2006-12-15 06:54:08 +1100225 u32 t1 = ehci_readl(ehci, reg) & ~PORT_RWC_BITS;
Alan Stern16032c42010-05-12 18:21:35 -0400226 u32 t2 = t1 & ~PORT_WAKE_BITS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227
Alan Stern8c033562006-11-09 14:42:16 -0500228 /* keep track of which ports we suspend */
Alan Stern383975d2007-05-04 11:52:40 -0400229 if (t1 & PORT_OWNER)
230 set_bit(port, &ehci->owned_ports);
231 else if ((t1 & PORT_PE) && !(t1 & PORT_SUSPEND)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 t2 |= PORT_SUSPEND;
Alan Stern8c033562006-11-09 14:42:16 -0500233 set_bit(port, &ehci->bus_suspended);
234 }
235
Alan Stern16032c42010-05-12 18:21:35 -0400236 /* enable remote wakeup on all ports, if told to do so */
Alek Du331ac6b2009-07-13 12:41:20 +0800237 if (hcd->self.root_hub->do_remote_wakeup) {
238 /* only enable appropriate wake bits, otherwise the
239 * hardware can not go phy low power mode. If a race
240 * condition happens here(connection change during bits
241 * set), the port change detection will finally fix it.
242 */
Alan Stern16032c42010-05-12 18:21:35 -0400243 if (t1 & PORT_CONNECT)
Alek Du331ac6b2009-07-13 12:41:20 +0800244 t2 |= PORT_WKOC_E | PORT_WKDISC_E;
Alan Stern16032c42010-05-12 18:21:35 -0400245 else
Alek Du331ac6b2009-07-13 12:41:20 +0800246 t2 |= PORT_WKOC_E | PORT_WKCONN_E;
Alan Stern16032c42010-05-12 18:21:35 -0400247 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248
249 if (t1 != t2) {
250 ehci_vdbg (ehci, "port %d, %08x -> %08x\n",
251 port + 1, t1, t2);
Benjamin Herrenschmidt083522d2006-12-15 06:54:08 +1100252 ehci_writel(ehci, t2, reg);
Alan Stern16032c42010-05-12 18:21:35 -0400253 changed = 1;
254 }
255 }
Alek Du331ac6b2009-07-13 12:41:20 +0800256
Alan Stern16032c42010-05-12 18:21:35 -0400257 if (changed && ehci->has_hostpc) {
258 spin_unlock_irq(&ehci->lock);
259 msleep(5); /* 5 ms for HCD to enter low-power mode */
260 spin_lock_irq(&ehci->lock);
261
262 port = HCS_N_PORTS(ehci->hcs_params);
263 while (port--) {
264 u32 __iomem *hostpc_reg;
265 u32 t3;
266
267 hostpc_reg = (u32 __iomem *)((u8 *) ehci->regs
268 + HOSTPC0 + 4 * port);
269 t3 = ehci_readl(ehci, hostpc_reg);
270 ehci_writel(ehci, t3 | HOSTPC_PHCD, hostpc_reg);
271 t3 = ehci_readl(ehci, hostpc_reg);
272 ehci_dbg(ehci, "Port %d phy low-power mode %s\n",
Alek Du331ac6b2009-07-13 12:41:20 +0800273 port, (t3 & HOSTPC_PHCD) ?
274 "succeeded" : "failed");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 }
276 }
277
Alan Sterncd930c92008-01-10 11:14:53 -0500278 /* Apparently some devices need a >= 1-uframe delay here */
279 if (ehci->bus_suspended)
280 udelay(150);
281
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 /* turn off now-idle HC */
283 ehci_halt (ehci);
284 hcd->state = HC_STATE_SUSPENDED;
285
David Brownellcdc647a2008-04-02 13:40:20 -0700286 if (ehci->reclaim)
287 end_unlink_async(ehci);
288
Alan Stern8c033562006-11-09 14:42:16 -0500289 /* allow remote wakeup */
290 mask = INTR_MASK;
Alan Stern58a97ff2008-04-14 12:17:10 -0400291 if (!hcd->self.root_hub->do_remote_wakeup)
Alan Stern8c033562006-11-09 14:42:16 -0500292 mask &= ~STS_PCD;
Benjamin Herrenschmidt083522d2006-12-15 06:54:08 +1100293 ehci_writel(ehci, mask, &ehci->regs->intr_enable);
294 ehci_readl(ehci, &ehci->regs->intr_enable);
Alan Stern8c033562006-11-09 14:42:16 -0500295
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 ehci->next_statechange = jiffies + msecs_to_jiffies(10);
297 spin_unlock_irq (&ehci->lock);
Jon Hunter015798b2009-08-12 11:57:59 -0400298
299 /* ehci_work() may have re-enabled the watchdog timer, which we do not
300 * want, and so we must delete any pending watchdog timer events.
301 */
302 del_timer_sync(&ehci->watchdog);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 return 0;
304}
305
306
307/* caller has locked the root hub, and should reset/reinit on error */
Alan Stern0c0382e2005-10-13 17:08:02 -0400308static int ehci_bus_resume (struct usb_hcd *hcd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309{
310 struct ehci_hcd *ehci = hcd_to_ehci (hcd);
311 u32 temp;
Alan Stern383975d2007-05-04 11:52:40 -0400312 u32 power_okay;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 int i;
Vikram Pandita3a4e72c2008-10-24 23:41:30 +0530314 u8 resume_needed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315
316 if (time_before (jiffies, ehci->next_statechange))
317 msleep(5);
318 spin_lock_irq (&ehci->lock);
Alan Stern541c7d42010-06-22 16:39:10 -0400319 if (!HCD_HW_ACCESSIBLE(hcd)) {
Alan Sterncfa59da2007-06-21 16:25:35 -0400320 spin_unlock_irq(&ehci->lock);
321 return -ESHUTDOWN;
322 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323
Jason Wesselad45f1d2009-08-20 15:39:58 -0500324 if (unlikely(ehci->debug)) {
Jason Wessel872d3592009-09-23 18:32:44 -0500325 if (!dbgp_reset_prep())
Jason Wesselad45f1d2009-08-20 15:39:58 -0500326 ehci->debug = NULL;
327 else
328 dbgp_external_startup();
329 }
330
David Brownellf03c17f2005-11-23 15:45:28 -0800331 /* Ideally and we've got a real resume here, and no port's power
332 * was lost. (For PCI, that means Vaux was maintained.) But we
333 * could instead be restoring a swsusp snapshot -- so that BIOS was
334 * the last user of the controller, not reset/pm hardware keeping
335 * state we gave to it.
336 */
Alan Stern383975d2007-05-04 11:52:40 -0400337 power_okay = ehci_readl(ehci, &ehci->regs->intr_enable);
338 ehci_dbg(ehci, "resume root hub%s\n",
339 power_okay ? "" : " after power loss");
David Brownellf03c17f2005-11-23 15:45:28 -0800340
Alan Stern8c033562006-11-09 14:42:16 -0500341 /* at least some APM implementations will try to deliver
342 * IRQs right away, so delay them until we're ready.
343 */
Benjamin Herrenschmidt083522d2006-12-15 06:54:08 +1100344 ehci_writel(ehci, 0, &ehci->regs->intr_enable);
Alan Stern8c033562006-11-09 14:42:16 -0500345
346 /* re-init operational registers */
Benjamin Herrenschmidt083522d2006-12-15 06:54:08 +1100347 ehci_writel(ehci, 0, &ehci->regs->segment);
348 ehci_writel(ehci, ehci->periodic_dma, &ehci->regs->frame_list);
349 ehci_writel(ehci, (u32) ehci->async->qh_dma, &ehci->regs->async_next);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350
351 /* restore CMD_RUN, framelist size, and irq threshold */
Benjamin Herrenschmidt083522d2006-12-15 06:54:08 +1100352 ehci_writel(ehci, ehci->command, &ehci->regs->command);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353
Alan Sterne198a312007-03-15 15:54:30 -0400354 /* Some controller/firmware combinations need a delay during which
355 * they set up the port statuses. See Bugzilla #8190. */
Vikram Pandita3a4e72c2008-10-24 23:41:30 +0530356 spin_unlock_irq(&ehci->lock);
357 msleep(8);
358 spin_lock_irq(&ehci->lock);
Alan Sterne198a312007-03-15 15:54:30 -0400359
Alan Stern16032c42010-05-12 18:21:35 -0400360 /* clear phy low-power mode before resume */
361 if (ehci->bus_suspended && ehci->has_hostpc) {
362 i = HCS_N_PORTS(ehci->hcs_params);
363 while (i--) {
364 if (test_bit(i, &ehci->bus_suspended)) {
365 u32 __iomem *hostpc_reg;
366
367 hostpc_reg = (u32 __iomem *)((u8 *) ehci->regs
368 + HOSTPC0 + 4 * i);
369 temp = ehci_readl(ehci, hostpc_reg);
370 ehci_writel(ehci, temp & ~HOSTPC_PHCD,
371 hostpc_reg);
372 }
373 }
374 spin_unlock_irq(&ehci->lock);
375 msleep(5);
376 spin_lock_irq(&ehci->lock);
377 }
378
Alan Stern8c033562006-11-09 14:42:16 -0500379 /* manually resume the ports we suspended during bus_suspend() */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 i = HCS_N_PORTS (ehci->hcs_params);
381 while (i--) {
Benjamin Herrenschmidt083522d2006-12-15 06:54:08 +1100382 temp = ehci_readl(ehci, &ehci->regs->port_status [i]);
Alan Stern58a97ff2008-04-14 12:17:10 -0400383 temp &= ~(PORT_RWC_BITS | PORT_WAKE_BITS);
Alan Stern8c033562006-11-09 14:42:16 -0500384 if (test_bit(i, &ehci->bus_suspended) &&
Vikram Pandita3a4e72c2008-10-24 23:41:30 +0530385 (temp & PORT_SUSPEND)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 temp |= PORT_RESUME;
Vikram Pandita3a4e72c2008-10-24 23:41:30 +0530387 resume_needed = 1;
388 }
Benjamin Herrenschmidt083522d2006-12-15 06:54:08 +1100389 ehci_writel(ehci, temp, &ehci->regs->port_status [i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 }
Vikram Pandita3a4e72c2008-10-24 23:41:30 +0530391
392 /* msleep for 20ms only if code is trying to resume port */
393 if (resume_needed) {
394 spin_unlock_irq(&ehci->lock);
395 msleep(20);
396 spin_lock_irq(&ehci->lock);
397 }
398
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 i = HCS_N_PORTS (ehci->hcs_params);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 while (i--) {
Benjamin Herrenschmidt083522d2006-12-15 06:54:08 +1100401 temp = ehci_readl(ehci, &ehci->regs->port_status [i]);
Alan Stern8c033562006-11-09 14:42:16 -0500402 if (test_bit(i, &ehci->bus_suspended) &&
403 (temp & PORT_SUSPEND)) {
404 temp &= ~(PORT_RWC_BITS | PORT_RESUME);
Benjamin Herrenschmidt083522d2006-12-15 06:54:08 +1100405 ehci_writel(ehci, temp, &ehci->regs->port_status [i]);
Alan Stern8c033562006-11-09 14:42:16 -0500406 ehci_vdbg (ehci, "resumed port %d\n", i + 1);
407 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 }
Benjamin Herrenschmidt083522d2006-12-15 06:54:08 +1100409 (void) ehci_readl(ehci, &ehci->regs->command);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410
411 /* maybe re-activate the schedule(s) */
412 temp = 0;
413 if (ehci->async->qh_next.qh)
414 temp |= CMD_ASE;
415 if (ehci->periodic_sched)
416 temp |= CMD_PSE;
417 if (temp) {
418 ehci->command |= temp;
Benjamin Herrenschmidt083522d2006-12-15 06:54:08 +1100419 ehci_writel(ehci, ehci->command, &ehci->regs->command);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 }
421
422 ehci->next_statechange = jiffies + msecs_to_jiffies(5);
423 hcd->state = HC_STATE_RUNNING;
424
425 /* Now we can safely re-enable irqs */
Benjamin Herrenschmidt083522d2006-12-15 06:54:08 +1100426 ehci_writel(ehci, INTR_MASK, &ehci->regs->intr_enable);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427
428 spin_unlock_irq (&ehci->lock);
Alan Stern3bb1af52008-03-03 15:15:36 -0500429 ehci_handover_companion_ports(ehci);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 return 0;
431}
432
433#else
434
Alan Stern0c0382e2005-10-13 17:08:02 -0400435#define ehci_bus_suspend NULL
436#define ehci_bus_resume NULL
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437
438#endif /* CONFIG_PM */
439
440/*-------------------------------------------------------------------------*/
441
Alan Stern57e06c12007-01-16 11:59:45 -0500442/* Display the ports dedicated to the companion controller */
Tony Jones5a3201b2007-09-11 14:07:31 -0700443static ssize_t show_companion(struct device *dev,
444 struct device_attribute *attr,
445 char *buf)
Alan Stern57e06c12007-01-16 11:59:45 -0500446{
447 struct ehci_hcd *ehci;
448 int nports, index, n;
449 int count = PAGE_SIZE;
450 char *ptr = buf;
451
Tony Jones5a3201b2007-09-11 14:07:31 -0700452 ehci = hcd_to_ehci(bus_to_hcd(dev_get_drvdata(dev)));
Alan Stern57e06c12007-01-16 11:59:45 -0500453 nports = HCS_N_PORTS(ehci->hcs_params);
454
455 for (index = 0; index < nports; ++index) {
456 if (test_bit(index, &ehci->companion_ports)) {
457 n = scnprintf(ptr, count, "%d\n", index + 1);
458 ptr += n;
459 count -= n;
460 }
461 }
462 return ptr - buf;
463}
464
465/*
Balaji Rao90da0962007-11-22 01:58:14 +0530466 * Sets the owner of a port
Alan Stern57e06c12007-01-16 11:59:45 -0500467 */
Balaji Rao90da0962007-11-22 01:58:14 +0530468static void set_owner(struct ehci_hcd *ehci, int portnum, int new_owner)
Alan Stern57e06c12007-01-16 11:59:45 -0500469{
Alan Stern57e06c12007-01-16 11:59:45 -0500470 u32 __iomem *status_reg;
471 u32 port_status;
Balaji Rao90da0962007-11-22 01:58:14 +0530472 int try;
Alan Stern57e06c12007-01-16 11:59:45 -0500473
Balaji Rao90da0962007-11-22 01:58:14 +0530474 status_reg = &ehci->regs->port_status[portnum];
Alan Stern57e06c12007-01-16 11:59:45 -0500475
476 /*
477 * The controller won't set the OWNER bit if the port is
478 * enabled, so this loop will sometimes require at least two
479 * iterations: one to disable the port and one to set OWNER.
480 */
Alan Stern57e06c12007-01-16 11:59:45 -0500481 for (try = 4; try > 0; --try) {
482 spin_lock_irq(&ehci->lock);
483 port_status = ehci_readl(ehci, status_reg);
484 if ((port_status & PORT_OWNER) == new_owner
485 || (port_status & (PORT_OWNER | PORT_CONNECT))
486 == 0)
487 try = 0;
488 else {
489 port_status ^= PORT_OWNER;
490 port_status &= ~(PORT_PE | PORT_RWC_BITS);
491 ehci_writel(ehci, port_status, status_reg);
492 }
493 spin_unlock_irq(&ehci->lock);
494 if (try > 1)
495 msleep(5);
496 }
Balaji Rao90da0962007-11-22 01:58:14 +0530497}
498
499/*
500 * Dedicate or undedicate a port to the companion controller.
501 * Syntax is "[-]portnum", where a leading '-' sign means
502 * return control of the port to the EHCI controller.
503 */
504static ssize_t store_companion(struct device *dev,
505 struct device_attribute *attr,
506 const char *buf, size_t count)
507{
508 struct ehci_hcd *ehci;
509 int portnum, new_owner;
510
511 ehci = hcd_to_ehci(bus_to_hcd(dev_get_drvdata(dev)));
512 new_owner = PORT_OWNER; /* Owned by companion */
513 if (sscanf(buf, "%d", &portnum) != 1)
514 return -EINVAL;
515 if (portnum < 0) {
516 portnum = - portnum;
517 new_owner = 0; /* Owned by EHCI */
518 }
519 if (portnum <= 0 || portnum > HCS_N_PORTS(ehci->hcs_params))
520 return -ENOENT;
521 portnum--;
522 if (new_owner)
523 set_bit(portnum, &ehci->companion_ports);
524 else
525 clear_bit(portnum, &ehci->companion_ports);
526 set_owner(ehci, portnum, new_owner);
Alan Stern57e06c12007-01-16 11:59:45 -0500527 return count;
528}
Tony Jones5a3201b2007-09-11 14:07:31 -0700529static DEVICE_ATTR(companion, 0644, show_companion, store_companion);
Alan Stern57e06c12007-01-16 11:59:45 -0500530
531static inline void create_companion_file(struct ehci_hcd *ehci)
532{
533 int i;
534
535 /* with integrated TT there is no companion! */
536 if (!ehci_is_TDI(ehci))
Greg Kroah-Hartmaned14f032009-04-27 13:15:38 -0700537 i = device_create_file(ehci_to_hcd(ehci)->self.controller,
Tony Jones5a3201b2007-09-11 14:07:31 -0700538 &dev_attr_companion);
Alan Stern57e06c12007-01-16 11:59:45 -0500539}
540
541static inline void remove_companion_file(struct ehci_hcd *ehci)
542{
543 /* with integrated TT there is no companion! */
544 if (!ehci_is_TDI(ehci))
Greg Kroah-Hartmaned14f032009-04-27 13:15:38 -0700545 device_remove_file(ehci_to_hcd(ehci)->self.controller,
Tony Jones5a3201b2007-09-11 14:07:31 -0700546 &dev_attr_companion);
Alan Stern57e06c12007-01-16 11:59:45 -0500547}
548
549
550/*-------------------------------------------------------------------------*/
551
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552static int check_reset_complete (
553 struct ehci_hcd *ehci,
554 int index,
Alan Sterne6316562007-01-16 11:58:00 -0500555 u32 __iomem *status_reg,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 int port_status
557) {
David Brownellcd4cdc92008-01-24 12:39:43 -0800558 if (!(port_status & PORT_CONNECT))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 return port_status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560
561 /* if reset finished and it's still not enabled -- handoff */
562 if (!(port_status & PORT_PE)) {
563
564 /* with integrated TT, there's nobody to hand it to! */
565 if (ehci_is_TDI(ehci)) {
566 ehci_dbg (ehci,
567 "Failed to enable port %d on root hub TT\n",
568 index+1);
569 return port_status;
570 }
571
572 ehci_dbg (ehci, "port %d full speed --> companion\n",
573 index + 1);
574
575 // what happens if HCS_N_CC(params) == 0 ?
576 port_status |= PORT_OWNER;
David Brownell10f65242005-08-31 10:55:38 -0700577 port_status &= ~PORT_RWC_BITS;
Alan Sterne6316562007-01-16 11:58:00 -0500578 ehci_writel(ehci, port_status, status_reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579
Vitaly Bordug796bcae2008-11-09 19:43:30 +0100580 /* ensure 440EPX ohci controller state is operational */
581 if (ehci->has_amcc_usb23)
582 set_ohci_hcfs(ehci, 1);
583 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 ehci_dbg (ehci, "port %d high speed\n", index + 1);
Vitaly Bordug796bcae2008-11-09 19:43:30 +0100585 /* ensure 440EPx ohci controller state is suspended */
586 if (ehci->has_amcc_usb23)
587 set_ohci_hcfs(ehci, 0);
588 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589
590 return port_status;
591}
592
593/*-------------------------------------------------------------------------*/
594
595
596/* build "status change" packet (one or two bytes) from HC registers */
597
598static int
599ehci_hub_status_data (struct usb_hcd *hcd, char *buf)
600{
601 struct ehci_hcd *ehci = hcd_to_ehci (hcd);
602 u32 temp, status = 0;
David Brownell93f1a472006-11-16 23:34:58 -0800603 u32 mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 int ports, i, retval = 1;
605 unsigned long flags;
Alek Du5a9cdf32010-06-04 15:47:56 +0800606 u32 ppcd = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607
608 /* if !USB_SUSPEND, root hub timers won't get shut down ... */
609 if (!HC_IS_RUNNING(hcd->state))
610 return 0;
611
612 /* init status to no-changes */
613 buf [0] = 0;
614 ports = HCS_N_PORTS (ehci->hcs_params);
615 if (ports > 7) {
616 buf [1] = 0;
617 retval++;
618 }
David Brownell53bd6a62006-08-30 14:50:06 -0700619
David Brownell93f1a472006-11-16 23:34:58 -0800620 /* Some boards (mostly VIA?) report bogus overcurrent indications,
621 * causing massive log spam unless we completely ignore them. It
Robert P. J. Day3a4fa0a2007-10-19 23:10:43 +0200622 * may be relevant that VIA VT8235 controllers, where PORT_POWER is
David Brownell93f1a472006-11-16 23:34:58 -0800623 * always set, seem to clear PORT_OCC and PORT_CSC when writing to
624 * PORT_POWER; that's surprising, but maybe within-spec.
625 */
626 if (!ignore_oc)
627 mask = PORT_CSC | PORT_PEC | PORT_OCC;
628 else
629 mask = PORT_CSC | PORT_PEC;
630 // PORT_RESUME from hardware ~= PORT_STAT_C_SUSPEND
631
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 /* no hub change reports (bit 0) for now (power, ...) */
633
634 /* port N changes (bit N)? */
635 spin_lock_irqsave (&ehci->lock, flags);
Alek Du5a9cdf32010-06-04 15:47:56 +0800636
637 /* get per-port change detect bits */
638 if (ehci->has_ppcd)
639 ppcd = ehci_readl(ehci, &ehci->regs->status) >> 16;
640
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 for (i = 0; i < ports; i++) {
Alek Du5a9cdf32010-06-04 15:47:56 +0800642 /* leverage per-port change bits feature */
643 if (ehci->has_ppcd && !(ppcd & (1 << i)))
644 continue;
Benjamin Herrenschmidt083522d2006-12-15 06:54:08 +1100645 temp = ehci_readl(ehci, &ehci->regs->port_status [i]);
Alan Stern625b5c92007-01-16 11:58:47 -0500646
647 /*
648 * Return status information even for ports with OWNER set.
649 * Otherwise khubd wouldn't see the disconnect event when a
650 * high-speed device is switched over to the companion
651 * controller by the user.
652 */
653
Alan Sterneafe5b92008-10-06 11:25:53 -0400654 if ((temp & mask) != 0 || test_bit(i, &ehci->port_c_suspend)
655 || (ehci->reset_done[i] && time_after_eq(
656 jiffies, ehci->reset_done[i]))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 if (i < 7)
658 buf [0] |= 1 << (i + 1);
659 else
660 buf [1] |= 1 << (i - 7);
661 status = STS_PCD;
662 }
663 }
664 /* FIXME autosuspend idle root hubs */
665 spin_unlock_irqrestore (&ehci->lock, flags);
666 return status ? retval : 0;
667}
668
669/*-------------------------------------------------------------------------*/
670
671static void
672ehci_hub_descriptor (
673 struct ehci_hcd *ehci,
674 struct usb_hub_descriptor *desc
675) {
676 int ports = HCS_N_PORTS (ehci->hcs_params);
677 u16 temp;
678
679 desc->bDescriptorType = 0x29;
680 desc->bPwrOn2PwrGood = 10; /* ehci 1.0, 2.3.9 says 20ms max */
681 desc->bHubContrCurrent = 0;
682
683 desc->bNbrPorts = ports;
684 temp = 1 + (ports / 8);
685 desc->bDescLength = 7 + 2 * temp;
686
687 /* two bitmaps: ports removable, and usb 1.0 legacy PortPwrCtrlMask */
688 memset (&desc->bitmap [0], 0, temp);
689 memset (&desc->bitmap [temp], 0xff, temp);
690
691 temp = 0x0008; /* per-port overcurrent reporting */
692 if (HCS_PPC (ehci->hcs_params))
693 temp |= 0x0001; /* per-port power control */
David Brownell56c1e262005-04-09 09:00:29 -0700694 else
695 temp |= 0x0002; /* no power switching */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696#if 0
697// re-enable when we support USB_PORT_FEAT_INDICATOR below.
698 if (HCS_INDICATOR (ehci->hcs_params))
699 temp |= 0x0080; /* per-port indicators (LEDs) */
700#endif
Al Virofd05e722008-04-28 07:00:16 +0100701 desc->wHubCharacteristics = cpu_to_le16(temp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702}
703
704/*-------------------------------------------------------------------------*/
705
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706static int ehci_hub_control (
707 struct usb_hcd *hcd,
708 u16 typeReq,
709 u16 wValue,
710 u16 wIndex,
711 char *buf,
712 u16 wLength
713) {
714 struct ehci_hcd *ehci = hcd_to_ehci (hcd);
715 int ports = HCS_N_PORTS (ehci->hcs_params);
Alan Stern383975d2007-05-04 11:52:40 -0400716 u32 __iomem *status_reg = &ehci->regs->port_status[
717 (wIndex & 0xff) - 1];
Alek Du331ac6b2009-07-13 12:41:20 +0800718 u32 __iomem *hostpc_reg = NULL;
719 u32 temp, temp1, status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 unsigned long flags;
721 int retval = 0;
David Brownellf0d7f272006-11-16 23:56:15 -0800722 unsigned selector;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723
724 /*
725 * FIXME: support SetPortFeatures USB_PORT_FEAT_INDICATOR.
726 * HCS_INDICATOR may say we can change LEDs to off/amber/green.
727 * (track current state ourselves) ... blink for diagnostics,
728 * power, "this is the one", etc. EHCI spec supports this.
729 */
730
Alek Du331ac6b2009-07-13 12:41:20 +0800731 if (ehci->has_hostpc)
732 hostpc_reg = (u32 __iomem *)((u8 *)ehci->regs
733 + HOSTPC0 + 4 * ((wIndex & 0xff) - 1));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 spin_lock_irqsave (&ehci->lock, flags);
735 switch (typeReq) {
736 case ClearHubFeature:
737 switch (wValue) {
738 case C_HUB_LOCAL_POWER:
739 case C_HUB_OVER_CURRENT:
740 /* no hub-wide feature/status flags */
741 break;
742 default:
743 goto error;
744 }
745 break;
746 case ClearPortFeature:
747 if (!wIndex || wIndex > ports)
748 goto error;
749 wIndex--;
Alan Sterne6316562007-01-16 11:58:00 -0500750 temp = ehci_readl(ehci, status_reg);
Alan Stern625b5c92007-01-16 11:58:47 -0500751
752 /*
753 * Even if OWNER is set, so the port is owned by the
754 * companion controller, khubd needs to be able to clear
755 * the port-change status bits (especially
Alan Stern749da5f2010-03-04 17:05:08 -0500756 * USB_PORT_STAT_C_CONNECTION).
Alan Stern625b5c92007-01-16 11:58:47 -0500757 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758
759 switch (wValue) {
760 case USB_PORT_FEAT_ENABLE:
Alan Sterne6316562007-01-16 11:58:00 -0500761 ehci_writel(ehci, temp & ~PORT_PE, status_reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 break;
763 case USB_PORT_FEAT_C_ENABLE:
Benjamin Herrenschmidt083522d2006-12-15 06:54:08 +1100764 ehci_writel(ehci, (temp & ~PORT_RWC_BITS) | PORT_PEC,
Alan Sterne6316562007-01-16 11:58:00 -0500765 status_reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 break;
767 case USB_PORT_FEAT_SUSPEND:
768 if (temp & PORT_RESET)
769 goto error;
David Brownellf8aeb3b2006-01-20 13:55:14 -0800770 if (ehci->no_selective_suspend)
771 break;
Alan Stern16032c42010-05-12 18:21:35 -0400772 if (!(temp & PORT_SUSPEND))
773 break;
774 if ((temp & PORT_PE) == 0)
775 goto error;
776
777 /* clear phy low-power mode before resume */
778 if (hostpc_reg) {
779 temp1 = ehci_readl(ehci, hostpc_reg);
780 ehci_writel(ehci, temp1 & ~HOSTPC_PHCD,
Alek Dueab80de2010-05-10 11:17:49 +0800781 hostpc_reg);
Alan Stern16032c42010-05-12 18:21:35 -0400782 spin_unlock_irqrestore(&ehci->lock, flags);
783 msleep(5);/* wait to leave low-power mode */
784 spin_lock_irqsave(&ehci->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 }
Alan Stern16032c42010-05-12 18:21:35 -0400786 /* resume signaling for 20 msec */
787 temp &= ~(PORT_RWC_BITS | PORT_WAKE_BITS);
788 ehci_writel(ehci, temp | PORT_RESUME, status_reg);
789 ehci->reset_done[wIndex] = jiffies
790 + msecs_to_jiffies(20);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 break;
792 case USB_PORT_FEAT_C_SUSPEND:
Alan Sternd1f114d2008-05-20 16:58:58 -0400793 clear_bit(wIndex, &ehci->port_c_suspend);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 break;
795 case USB_PORT_FEAT_POWER:
796 if (HCS_PPC (ehci->hcs_params))
Benjamin Herrenschmidt083522d2006-12-15 06:54:08 +1100797 ehci_writel(ehci,
798 temp & ~(PORT_RWC_BITS | PORT_POWER),
Alan Sterne6316562007-01-16 11:58:00 -0500799 status_reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 break;
801 case USB_PORT_FEAT_C_CONNECTION:
Alek Du48f24972010-06-04 15:47:55 +0800802 if (ehci->has_lpm) {
803 /* clear PORTSC bits on disconnect */
804 temp &= ~PORT_LPM;
805 temp &= ~PORT_DEV_ADDR;
806 }
Benjamin Herrenschmidt083522d2006-12-15 06:54:08 +1100807 ehci_writel(ehci, (temp & ~PORT_RWC_BITS) | PORT_CSC,
Alan Sterne6316562007-01-16 11:58:00 -0500808 status_reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 break;
810 case USB_PORT_FEAT_C_OVER_CURRENT:
Benjamin Herrenschmidt083522d2006-12-15 06:54:08 +1100811 ehci_writel(ehci, (temp & ~PORT_RWC_BITS) | PORT_OCC,
Alan Sterne6316562007-01-16 11:58:00 -0500812 status_reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 break;
814 case USB_PORT_FEAT_C_RESET:
815 /* GetPortStatus clears reset */
816 break;
817 default:
818 goto error;
819 }
Benjamin Herrenschmidt083522d2006-12-15 06:54:08 +1100820 ehci_readl(ehci, &ehci->regs->command); /* unblock posted write */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 break;
822 case GetHubDescriptor:
823 ehci_hub_descriptor (ehci, (struct usb_hub_descriptor *)
824 buf);
825 break;
826 case GetHubStatus:
827 /* no hub-wide feature/status flags */
828 memset (buf, 0, 4);
829 //cpu_to_le32s ((u32 *) buf);
830 break;
831 case GetPortStatus:
832 if (!wIndex || wIndex > ports)
833 goto error;
834 wIndex--;
835 status = 0;
Alan Sterne6316562007-01-16 11:58:00 -0500836 temp = ehci_readl(ehci, status_reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837
838 // wPortChange bits
839 if (temp & PORT_CSC)
Alan Stern749da5f2010-03-04 17:05:08 -0500840 status |= USB_PORT_STAT_C_CONNECTION << 16;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 if (temp & PORT_PEC)
Alan Stern749da5f2010-03-04 17:05:08 -0500842 status |= USB_PORT_STAT_C_ENABLE << 16;
Christian Engelmayer756aa6b2007-05-30 11:04:48 -0700843
844 if ((temp & PORT_OCC) && !ignore_oc){
Alan Stern749da5f2010-03-04 17:05:08 -0500845 status |= USB_PORT_STAT_C_OVERCURRENT << 16;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846
Christian Engelmayer756aa6b2007-05-30 11:04:48 -0700847 /*
848 * Hubs should disable port power on over-current.
849 * However, not all EHCI implementations do this
850 * automatically, even if they _do_ support per-port
851 * power switching; they're allowed to just limit the
852 * current. khubd will turn the power back on.
853 */
854 if (HCS_PPC (ehci->hcs_params)){
855 ehci_writel(ehci,
856 temp & ~(PORT_RWC_BITS | PORT_POWER),
857 status_reg);
858 }
859 }
860
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 /* whoever resumes must GetPortStatus to complete it!! */
Alan Stern629e4422007-01-22 16:08:53 -0500862 if (temp & PORT_RESUME) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863
Alan Stern629e4422007-01-22 16:08:53 -0500864 /* Remote Wakeup received? */
865 if (!ehci->reset_done[wIndex]) {
866 /* resume signaling for 20 msec */
867 ehci->reset_done[wIndex] = jiffies
868 + msecs_to_jiffies(20);
869 /* check the port again */
870 mod_timer(&ehci_to_hcd(ehci)->rh_timer,
871 ehci->reset_done[wIndex]);
872 }
873
874 /* resume completed? */
875 else if (time_after_eq(jiffies,
876 ehci->reset_done[wIndex])) {
Alan Sterneafe5b92008-10-06 11:25:53 -0400877 clear_bit(wIndex, &ehci->suspended_ports);
Alan Sternd1f114d2008-05-20 16:58:58 -0400878 set_bit(wIndex, &ehci->port_c_suspend);
Alan Stern629e4422007-01-22 16:08:53 -0500879 ehci->reset_done[wIndex] = 0;
880
881 /* stop resume signaling */
882 temp = ehci_readl(ehci, status_reg);
883 ehci_writel(ehci,
Alan Sterne6316562007-01-16 11:58:00 -0500884 temp & ~(PORT_RWC_BITS | PORT_RESUME),
885 status_reg);
Alan Stern629e4422007-01-22 16:08:53 -0500886 retval = handshake(ehci, status_reg,
Benjamin Herrenschmidt083522d2006-12-15 06:54:08 +1100887 PORT_RESUME, 0, 2000 /* 2msec */);
Alan Stern629e4422007-01-22 16:08:53 -0500888 if (retval != 0) {
889 ehci_err(ehci,
890 "port %d resume error %d\n",
891 wIndex + 1, retval);
892 goto error;
893 }
894 temp &= ~(PORT_SUSPEND|PORT_RESUME|(3<<10));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896 }
897
898 /* whoever resets must GetPortStatus to complete it!! */
899 if ((temp & PORT_RESET)
Alan Stern629e4422007-01-22 16:08:53 -0500900 && time_after_eq(jiffies,
901 ehci->reset_done[wIndex])) {
Alan Stern749da5f2010-03-04 17:05:08 -0500902 status |= USB_PORT_STAT_C_RESET << 16;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903 ehci->reset_done [wIndex] = 0;
904
905 /* force reset to complete */
Benjamin Herrenschmidt083522d2006-12-15 06:54:08 +1100906 ehci_writel(ehci, temp & ~(PORT_RWC_BITS | PORT_RESET),
Alan Sterne6316562007-01-16 11:58:00 -0500907 status_reg);
David Brownellc22fa3a2005-06-13 07:15:28 -0700908 /* REVISIT: some hardware needs 550+ usec to clear
909 * this bit; seems too long to spin routinely...
910 */
Alan Sterne6316562007-01-16 11:58:00 -0500911 retval = handshake(ehci, status_reg,
Dinh Nguyen6307e092010-04-13 11:13:15 -0500912 PORT_RESET, 0, 1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 if (retval != 0) {
914 ehci_err (ehci, "port %d reset error %d\n",
915 wIndex + 1, retval);
916 goto error;
917 }
918
919 /* see what we found out */
Alan Sterne6316562007-01-16 11:58:00 -0500920 temp = check_reset_complete (ehci, wIndex, status_reg,
921 ehci_readl(ehci, status_reg));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922 }
923
Alan Sterneafe5b92008-10-06 11:25:53 -0400924 if (!(temp & (PORT_RESUME|PORT_RESET)))
925 ehci->reset_done[wIndex] = 0;
926
Alan Stern57e06c12007-01-16 11:59:45 -0500927 /* transfer dedicated ports to the companion hc */
928 if ((temp & PORT_CONNECT) &&
929 test_bit(wIndex, &ehci->companion_ports)) {
930 temp &= ~PORT_RWC_BITS;
931 temp |= PORT_OWNER;
932 ehci_writel(ehci, temp, status_reg);
933 ehci_dbg(ehci, "port %d --> companion\n", wIndex + 1);
934 temp = ehci_readl(ehci, status_reg);
935 }
936
Alan Stern625b5c92007-01-16 11:58:47 -0500937 /*
938 * Even if OWNER is set, there's no harm letting khubd
939 * see the wPortStatus values (they should all be 0 except
940 * for PORT_POWER anyway).
941 */
942
943 if (temp & PORT_CONNECT) {
Alan Stern749da5f2010-03-04 17:05:08 -0500944 status |= USB_PORT_STAT_CONNECTION;
Alan Stern625b5c92007-01-16 11:58:47 -0500945 // status may be from integrated TT
Alek Du331ac6b2009-07-13 12:41:20 +0800946 if (ehci->has_hostpc) {
947 temp1 = ehci_readl(ehci, hostpc_reg);
948 status |= ehci_port_speed(ehci, temp1);
949 } else
950 status |= ehci_port_speed(ehci, temp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951 }
Alan Stern625b5c92007-01-16 11:58:47 -0500952 if (temp & PORT_PE)
Alan Stern749da5f2010-03-04 17:05:08 -0500953 status |= USB_PORT_STAT_ENABLE;
Alan Sterneafe5b92008-10-06 11:25:53 -0400954
955 /* maybe the port was unsuspended without our knowledge */
956 if (temp & (PORT_SUSPEND|PORT_RESUME)) {
Alan Stern749da5f2010-03-04 17:05:08 -0500957 status |= USB_PORT_STAT_SUSPEND;
Alan Sterneafe5b92008-10-06 11:25:53 -0400958 } else if (test_bit(wIndex, &ehci->suspended_ports)) {
959 clear_bit(wIndex, &ehci->suspended_ports);
960 ehci->reset_done[wIndex] = 0;
961 if (temp & PORT_PE)
962 set_bit(wIndex, &ehci->port_c_suspend);
963 }
964
Alan Stern625b5c92007-01-16 11:58:47 -0500965 if (temp & PORT_OC)
Alan Stern749da5f2010-03-04 17:05:08 -0500966 status |= USB_PORT_STAT_OVERCURRENT;
Alan Stern625b5c92007-01-16 11:58:47 -0500967 if (temp & PORT_RESET)
Alan Stern749da5f2010-03-04 17:05:08 -0500968 status |= USB_PORT_STAT_RESET;
Alan Stern625b5c92007-01-16 11:58:47 -0500969 if (temp & PORT_POWER)
Alan Stern749da5f2010-03-04 17:05:08 -0500970 status |= USB_PORT_STAT_POWER;
Alan Sternd1f114d2008-05-20 16:58:58 -0400971 if (test_bit(wIndex, &ehci->port_c_suspend))
Alan Stern749da5f2010-03-04 17:05:08 -0500972 status |= USB_PORT_STAT_C_SUSPEND << 16;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973
David Brownell9776afc2008-02-01 11:42:05 -0800974#ifndef VERBOSE_DEBUG
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 if (status & ~0xffff) /* only if wPortChange is interesting */
976#endif
977 dbg_port (ehci, "GetStatus", wIndex + 1, temp);
Harvey Harrisona5abdea2008-04-29 01:03:40 -0700978 put_unaligned_le32(status, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979 break;
980 case SetHubFeature:
981 switch (wValue) {
982 case C_HUB_LOCAL_POWER:
983 case C_HUB_OVER_CURRENT:
984 /* no hub-wide feature/status flags */
985 break;
986 default:
987 goto error;
988 }
989 break;
990 case SetPortFeature:
David Brownellf0d7f272006-11-16 23:56:15 -0800991 selector = wIndex >> 8;
992 wIndex &= 0xff;
Jason Wessel8d053c72009-08-20 15:39:54 -0500993 if (unlikely(ehci->debug)) {
994 /* If the debug port is active any port
995 * feature requests should get denied */
996 if (wIndex == HCS_DEBUG_PORT(ehci->hcs_params) &&
997 (readl(&ehci->debug->control) & DBGP_ENABLED)) {
998 retval = -ENODEV;
999 goto error_exit;
1000 }
1001 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002 if (!wIndex || wIndex > ports)
1003 goto error;
1004 wIndex--;
Alan Sterne6316562007-01-16 11:58:00 -05001005 temp = ehci_readl(ehci, status_reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006 if (temp & PORT_OWNER)
1007 break;
1008
David Brownell10f65242005-08-31 10:55:38 -07001009 temp &= ~PORT_RWC_BITS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010 switch (wValue) {
1011 case USB_PORT_FEAT_SUSPEND:
David Brownellf8aeb3b2006-01-20 13:55:14 -08001012 if (ehci->no_selective_suspend)
1013 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014 if ((temp & PORT_PE) == 0
1015 || (temp & PORT_RESET) != 0)
1016 goto error;
Alek Dub9df79422010-01-19 16:31:31 +08001017
Alek Du331ac6b2009-07-13 12:41:20 +08001018 /* After above check the port must be connected.
1019 * Set appropriate bit thus could put phy into low power
1020 * mode if we have hostpc feature
1021 */
Alek Dub9df79422010-01-19 16:31:31 +08001022 temp &= ~PORT_WKCONN_E;
1023 temp |= PORT_WKDISC_E | PORT_WKOC_E;
1024 ehci_writel(ehci, temp | PORT_SUSPEND, status_reg);
Alek Du331ac6b2009-07-13 12:41:20 +08001025 if (hostpc_reg) {
Alek Dub9df79422010-01-19 16:31:31 +08001026 spin_unlock_irqrestore(&ehci->lock, flags);
Alek Du331ac6b2009-07-13 12:41:20 +08001027 msleep(5);/* 5ms for HCD enter low pwr mode */
Alek Dub9df79422010-01-19 16:31:31 +08001028 spin_lock_irqsave(&ehci->lock, flags);
Alek Du331ac6b2009-07-13 12:41:20 +08001029 temp1 = ehci_readl(ehci, hostpc_reg);
1030 ehci_writel(ehci, temp1 | HOSTPC_PHCD,
1031 hostpc_reg);
1032 temp1 = ehci_readl(ehci, hostpc_reg);
1033 ehci_dbg(ehci, "Port%d phy low pwr mode %s\n",
1034 wIndex, (temp1 & HOSTPC_PHCD) ?
1035 "succeeded" : "failed");
1036 }
Alan Sterneafe5b92008-10-06 11:25:53 -04001037 set_bit(wIndex, &ehci->suspended_ports);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038 break;
1039 case USB_PORT_FEAT_POWER:
1040 if (HCS_PPC (ehci->hcs_params))
Benjamin Herrenschmidt083522d2006-12-15 06:54:08 +11001041 ehci_writel(ehci, temp | PORT_POWER,
Alan Sterne6316562007-01-16 11:58:00 -05001042 status_reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 break;
1044 case USB_PORT_FEAT_RESET:
1045 if (temp & PORT_RESUME)
1046 goto error;
1047 /* line status bits may report this as low speed,
1048 * which can be fine if this root hub has a
1049 * transaction translator built in.
1050 */
1051 if ((temp & (PORT_PE|PORT_CONNECT)) == PORT_CONNECT
1052 && !ehci_is_TDI(ehci)
1053 && PORT_USB11 (temp)) {
1054 ehci_dbg (ehci,
1055 "port %d low speed --> companion\n",
1056 wIndex + 1);
1057 temp |= PORT_OWNER;
1058 } else {
1059 ehci_vdbg (ehci, "port %d reset\n", wIndex + 1);
1060 temp |= PORT_RESET;
1061 temp &= ~PORT_PE;
1062
1063 /*
1064 * caller must wait, then call GetPortStatus
1065 * usb 2.0 spec says 50 ms resets on root
1066 */
1067 ehci->reset_done [wIndex] = jiffies
1068 + msecs_to_jiffies (50);
1069 }
Alan Sterne6316562007-01-16 11:58:00 -05001070 ehci_writel(ehci, temp, status_reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071 break;
David Brownellf0d7f272006-11-16 23:56:15 -08001072
1073 /* For downstream facing ports (these): one hub port is put
1074 * into test mode according to USB2 11.24.2.13, then the hub
1075 * must be reset (which for root hub now means rmmod+modprobe,
1076 * or else system reboot). See EHCI 2.3.9 and 4.14 for info
1077 * about the EHCI-specific stuff.
1078 */
1079 case USB_PORT_FEAT_TEST:
1080 if (!selector || selector > 5)
1081 goto error;
1082 ehci_quiesce(ehci);
1083 ehci_halt(ehci);
1084 temp |= selector << 16;
Alan Sterne6316562007-01-16 11:58:00 -05001085 ehci_writel(ehci, temp, status_reg);
David Brownellf0d7f272006-11-16 23:56:15 -08001086 break;
1087
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088 default:
1089 goto error;
1090 }
Benjamin Herrenschmidt083522d2006-12-15 06:54:08 +11001091 ehci_readl(ehci, &ehci->regs->command); /* unblock posted writes */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092 break;
1093
1094 default:
1095error:
1096 /* "stall" on error */
1097 retval = -EPIPE;
1098 }
Jason Wessel8d053c72009-08-20 15:39:54 -05001099error_exit:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100 spin_unlock_irqrestore (&ehci->lock, flags);
1101 return retval;
1102}
Balaji Rao90da0962007-11-22 01:58:14 +05301103
1104static void ehci_relinquish_port(struct usb_hcd *hcd, int portnum)
1105{
1106 struct ehci_hcd *ehci = hcd_to_ehci(hcd);
1107
1108 if (ehci_is_TDI(ehci))
1109 return;
1110 set_owner(ehci, --portnum, PORT_OWNER);
1111}
1112
Alan Stern3a311552008-05-20 16:58:29 -04001113static int ehci_port_handed_over(struct usb_hcd *hcd, int portnum)
1114{
1115 struct ehci_hcd *ehci = hcd_to_ehci(hcd);
1116 u32 __iomem *reg;
1117
1118 if (ehci_is_TDI(ehci))
1119 return 0;
1120 reg = &ehci->regs->port_status[portnum - 1];
1121 return ehci_readl(ehci, reg) & PORT_OWNER;
1122}