USB: EHCI: fix up locking
[deliverable/linux.git] / drivers / usb / host / ehci-hub.c
CommitLineData
1da177e4 1/*
d49d4317 2 * Copyright (C) 2001-2004 by David Brownell
53bd6a60 3 *
1da177e4
LT
4 * 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/*-------------------------------------------------------------------------*/
83722bc9 30#include <linux/usb/otg.h>
1da177e4 31
58a97ffe
AS
32#define PORT_WAKE_BITS (PORT_WKOC_E|PORT_WKDISC_E|PORT_WKCONN_E)
33
aff6d18f
AS
34#ifdef CONFIG_PM
35
383975d7
AS
36static int ehci_hub_control(
37 struct usb_hcd *hcd,
38 u16 typeReq,
39 u16 wValue,
40 u16 wIndex,
41 char *buf,
42 u16 wLength
43);
44
45/* After a power loss, ports that were owned by the companion must be
46 * reset so that the companion can still own them.
47 */
48static void ehci_handover_companion_ports(struct ehci_hcd *ehci)
49{
50 u32 __iomem *reg;
51 u32 status;
52 int port;
53 __le32 buf;
54 struct usb_hcd *hcd = ehci_to_hcd(ehci);
55
56 if (!ehci->owned_ports)
57 return;
58
59 /* Give the connections some time to appear */
60 msleep(20);
61
c4f34764 62 spin_lock_irq(&ehci->lock);
383975d7
AS
63 port = HCS_N_PORTS(ehci->hcs_params);
64 while (port--) {
65 if (test_bit(port, &ehci->owned_ports)) {
66 reg = &ehci->regs->port_status[port];
3c519b84 67 status = ehci_readl(ehci, reg) & ~PORT_RWC_BITS;
383975d7
AS
68
69 /* Port already owned by companion? */
70 if (status & PORT_OWNER)
71 clear_bit(port, &ehci->owned_ports);
3c519b84
AS
72 else if (test_bit(port, &ehci->companion_ports))
73 ehci_writel(ehci, status & ~PORT_PE, reg);
c4f34764
AS
74 else {
75 spin_unlock_irq(&ehci->lock);
383975d7
AS
76 ehci_hub_control(hcd, SetPortFeature,
77 USB_PORT_FEAT_RESET, port + 1,
78 NULL, 0);
c4f34764
AS
79 spin_lock_irq(&ehci->lock);
80 }
383975d7
AS
81 }
82 }
c4f34764 83 spin_unlock_irq(&ehci->lock);
383975d7
AS
84
85 if (!ehci->owned_ports)
86 return;
87 msleep(90); /* Wait for resets to complete */
88
c4f34764 89 spin_lock_irq(&ehci->lock);
383975d7
AS
90 port = HCS_N_PORTS(ehci->hcs_params);
91 while (port--) {
92 if (test_bit(port, &ehci->owned_ports)) {
c4f34764 93 spin_unlock_irq(&ehci->lock);
383975d7
AS
94 ehci_hub_control(hcd, GetPortStatus,
95 0, port + 1,
96 (char *) &buf, sizeof(buf));
c4f34764 97 spin_lock_irq(&ehci->lock);
383975d7
AS
98
99 /* The companion should now own the port,
100 * but if something went wrong the port must not
101 * remain enabled.
102 */
103 reg = &ehci->regs->port_status[port];
104 status = ehci_readl(ehci, reg) & ~PORT_RWC_BITS;
105 if (status & PORT_OWNER)
106 ehci_writel(ehci, status | PORT_CSC, reg);
107 else {
108 ehci_dbg(ehci, "failed handover port %d: %x\n",
109 port + 1, status);
110 ehci_writel(ehci, status & ~PORT_PE, reg);
111 }
112 }
113 }
114
115 ehci->owned_ports = 0;
c4f34764 116 spin_unlock_irq(&ehci->lock);
383975d7
AS
117}
118
c5cf9212 119static int ehci_port_change(struct ehci_hcd *ehci)
294d95f2
MG
120{
121 int i = HCS_N_PORTS(ehci->hcs_params);
122
123 /* First check if the controller indicates a change event */
124
125 if (ehci_readl(ehci, &ehci->regs->status) & STS_PCD)
126 return 1;
127
128 /*
129 * Not all controllers appear to update this while going from D3 to D0,
130 * so check the individual port status registers as well
131 */
132
133 while (i--)
134 if (ehci_readl(ehci, &ehci->regs->port_status[i]) & PORT_CSC)
135 return 1;
136
137 return 0;
138}
139
c5cf9212 140static void ehci_adjust_port_wakeup_flags(struct ehci_hcd *ehci,
4147200d 141 bool suspending, bool do_wakeup)
16032c4f
AS
142{
143 int port;
144 u32 temp;
145
146 /* If remote wakeup is enabled for the root hub but disabled
147 * for the controller, we must adjust all the port wakeup flags
148 * when the controller is suspended or resumed. In all other
149 * cases they don't need to be changed.
150 */
4147200d 151 if (!ehci_to_hcd(ehci)->self.root_hub->do_remote_wakeup || do_wakeup)
16032c4f
AS
152 return;
153
c4f34764 154 spin_lock_irq(&ehci->lock);
148fc55f 155
16032c4f
AS
156 /* clear phy low-power mode before changing wakeup flags */
157 if (ehci->has_hostpc) {
158 port = HCS_N_PORTS(ehci->hcs_params);
159 while (port--) {
a46af4eb 160 u32 __iomem *hostpc_reg = &ehci->regs->hostpc[port];
16032c4f 161
16032c4f
AS
162 temp = ehci_readl(ehci, hostpc_reg);
163 ehci_writel(ehci, temp & ~HOSTPC_PHCD, hostpc_reg);
164 }
c4f34764 165 spin_unlock_irq(&ehci->lock);
16032c4f 166 msleep(5);
c4f34764 167 spin_lock_irq(&ehci->lock);
16032c4f
AS
168 }
169
170 port = HCS_N_PORTS(ehci->hcs_params);
171 while (port--) {
172 u32 __iomem *reg = &ehci->regs->port_status[port];
173 u32 t1 = ehci_readl(ehci, reg) & ~PORT_RWC_BITS;
174 u32 t2 = t1 & ~PORT_WAKE_BITS;
175
176 /* If we are suspending the controller, clear the flags.
177 * If we are resuming the controller, set the wakeup flags.
178 */
179 if (!suspending) {
180 if (t1 & PORT_CONNECT)
181 t2 |= PORT_WKOC_E | PORT_WKDISC_E;
182 else
183 t2 |= PORT_WKOC_E | PORT_WKCONN_E;
184 }
185 ehci_vdbg(ehci, "port %d, %08x -> %08x\n",
186 port + 1, t1, t2);
187 ehci_writel(ehci, t2, reg);
188 }
189
190 /* enter phy low-power mode again */
191 if (ehci->has_hostpc) {
192 port = HCS_N_PORTS(ehci->hcs_params);
193 while (port--) {
a46af4eb 194 u32 __iomem *hostpc_reg = &ehci->regs->hostpc[port];
16032c4f 195
16032c4f
AS
196 temp = ehci_readl(ehci, hostpc_reg);
197 ehci_writel(ehci, temp | HOSTPC_PHCD, hostpc_reg);
198 }
199 }
ee0b9be8
AS
200
201 /* Does the root hub have a port wakeup pending? */
294d95f2 202 if (!suspending && ehci_port_change(ehci))
ee0b9be8 203 usb_hcd_resume_root_hub(ehci_to_hcd(ehci));
148fc55f 204
c4f34764 205 spin_unlock_irq(&ehci->lock);
16032c4f
AS
206}
207
0c0382e3 208static int ehci_bus_suspend (struct usb_hcd *hcd)
1da177e4
LT
209{
210 struct ehci_hcd *ehci = hcd_to_ehci (hcd);
211 int port;
8c03356a 212 int mask;
16032c4f 213 int changed;
1da177e4 214
8c774fe8
AS
215 ehci_dbg(ehci, "suspend root hub\n");
216
1da177e4
LT
217 if (time_before (jiffies, ehci->next_statechange))
218 msleep(5);
219
c4f34764
AS
220 /* stop the schedules */
221 ehci_quiesce(ehci);
222
1da177e4
LT
223 spin_lock_irq (&ehci->lock);
224
cec3a53c
AS
225 /* Once the controller is stopped, port resumes that are already
226 * in progress won't complete. Hence if remote wakeup is enabled
227 * for the root hub and any ports are in the middle of a resume or
228 * remote wakeup, we must fail the suspend.
229 */
230 if (hcd->self.root_hub->do_remote_wakeup) {
a448e4dc
AS
231 if (ehci->resuming_ports) {
232 spin_unlock_irq(&ehci->lock);
233 ehci_dbg(ehci, "suspend failed because a port is resuming\n");
234 return -EBUSY;
cec3a53c
AS
235 }
236 }
237
8c03356a
AS
238 /* Unlike other USB host controller types, EHCI doesn't have
239 * any notion of "global" or bus-wide suspend. The driver has
240 * to manually suspend all the active unsuspended ports, and
241 * then manually resume them in the bus_resume() routine.
242 */
243 ehci->bus_suspended = 0;
383975d7 244 ehci->owned_ports = 0;
16032c4f 245 changed = 0;
cec3a53c 246 port = HCS_N_PORTS(ehci->hcs_params);
1da177e4
LT
247 while (port--) {
248 u32 __iomem *reg = &ehci->regs->port_status [port];
083522d7 249 u32 t1 = ehci_readl(ehci, reg) & ~PORT_RWC_BITS;
16032c4f 250 u32 t2 = t1 & ~PORT_WAKE_BITS;
1da177e4 251
8c03356a 252 /* keep track of which ports we suspend */
383975d7
AS
253 if (t1 & PORT_OWNER)
254 set_bit(port, &ehci->owned_ports);
255 else if ((t1 & PORT_PE) && !(t1 & PORT_SUSPEND)) {
1da177e4 256 t2 |= PORT_SUSPEND;
8c03356a
AS
257 set_bit(port, &ehci->bus_suspended);
258 }
259
16032c4f 260 /* enable remote wakeup on all ports, if told to do so */
331ac6b2
AD
261 if (hcd->self.root_hub->do_remote_wakeup) {
262 /* only enable appropriate wake bits, otherwise the
263 * hardware can not go phy low power mode. If a race
264 * condition happens here(connection change during bits
265 * set), the port change detection will finally fix it.
266 */
16032c4f 267 if (t1 & PORT_CONNECT)
331ac6b2 268 t2 |= PORT_WKOC_E | PORT_WKDISC_E;
16032c4f 269 else
331ac6b2 270 t2 |= PORT_WKOC_E | PORT_WKCONN_E;
16032c4f 271 }
1da177e4
LT
272
273 if (t1 != t2) {
274 ehci_vdbg (ehci, "port %d, %08x -> %08x\n",
275 port + 1, t1, t2);
083522d7 276 ehci_writel(ehci, t2, reg);
16032c4f
AS
277 changed = 1;
278 }
279 }
331ac6b2 280
16032c4f
AS
281 if (changed && ehci->has_hostpc) {
282 spin_unlock_irq(&ehci->lock);
283 msleep(5); /* 5 ms for HCD to enter low-power mode */
284 spin_lock_irq(&ehci->lock);
285
286 port = HCS_N_PORTS(ehci->hcs_params);
287 while (port--) {
a46af4eb 288 u32 __iomem *hostpc_reg = &ehci->regs->hostpc[port];
16032c4f
AS
289 u32 t3;
290
16032c4f
AS
291 t3 = ehci_readl(ehci, hostpc_reg);
292 ehci_writel(ehci, t3 | HOSTPC_PHCD, hostpc_reg);
293 t3 = ehci_readl(ehci, hostpc_reg);
294 ehci_dbg(ehci, "Port %d phy low-power mode %s\n",
331ac6b2
AD
295 port, (t3 & HOSTPC_PHCD) ?
296 "succeeded" : "failed");
1da177e4
LT
297 }
298 }
c4f34764 299 spin_unlock_irq(&ehci->lock);
1da177e4 300
cd930c93
AS
301 /* Apparently some devices need a >= 1-uframe delay here */
302 if (ehci->bus_suspended)
303 udelay(150);
304
1da177e4
LT
305 /* turn off now-idle HC */
306 ehci_halt (ehci);
c4f34764
AS
307
308 spin_lock_irq(&ehci->lock);
e8799906 309 ehci->rh_state = EHCI_RH_SUSPENDED;
1da177e4 310
3c273a05 311 end_unlink_async(ehci);
32830f20 312 unlink_empty_async(ehci);
df202255 313 ehci_handle_intr_unlinks(ehci);
55934eb3 314 end_free_itds(ehci);
cdc647a9 315
8c03356a
AS
316 /* allow remote wakeup */
317 mask = INTR_MASK;
58a97ffe 318 if (!hcd->self.root_hub->do_remote_wakeup)
8c03356a 319 mask &= ~STS_PCD;
083522d7
BH
320 ehci_writel(ehci, mask, &ehci->regs->intr_enable);
321 ehci_readl(ehci, &ehci->regs->intr_enable);
8c03356a 322
1da177e4 323 ehci->next_statechange = jiffies + msecs_to_jiffies(10);
d58b4bcc
AS
324 ehci->enabled_hrtimer_events = 0;
325 ehci->next_hrtimer_event = EHCI_HRTIMER_NO_EVENT;
1da177e4 326 spin_unlock_irq (&ehci->lock);
015798b2 327
d58b4bcc 328 hrtimer_cancel(&ehci->hrtimer);
1da177e4
LT
329 return 0;
330}
331
332
333/* caller has locked the root hub, and should reset/reinit on error */
0c0382e3 334static int ehci_bus_resume (struct usb_hcd *hcd)
1da177e4
LT
335{
336 struct ehci_hcd *ehci = hcd_to_ehci (hcd);
337 u32 temp;
383975d7 338 u32 power_okay;
1da177e4 339 int i;
d0f2fb25 340 unsigned long resume_needed = 0;
1da177e4
LT
341
342 if (time_before (jiffies, ehci->next_statechange))
343 msleep(5);
344 spin_lock_irq (&ehci->lock);
541c7d43 345 if (!HCD_HW_ACCESSIBLE(hcd)) {
cfa59dab
AS
346 spin_unlock_irq(&ehci->lock);
347 return -ESHUTDOWN;
348 }
1da177e4 349
ad45f1dc 350 if (unlikely(ehci->debug)) {
872d3599 351 if (!dbgp_reset_prep())
ad45f1dc
JW
352 ehci->debug = NULL;
353 else
354 dbgp_external_startup();
355 }
356
f03c17fc
DB
357 /* Ideally and we've got a real resume here, and no port's power
358 * was lost. (For PCI, that means Vaux was maintained.) But we
359 * could instead be restoring a swsusp snapshot -- so that BIOS was
360 * the last user of the controller, not reset/pm hardware keeping
361 * state we gave to it.
362 */
383975d7
AS
363 power_okay = ehci_readl(ehci, &ehci->regs->intr_enable);
364 ehci_dbg(ehci, "resume root hub%s\n",
365 power_okay ? "" : " after power loss");
f03c17fc 366
8c03356a
AS
367 /* at least some APM implementations will try to deliver
368 * IRQs right away, so delay them until we're ready.
369 */
083522d7 370 ehci_writel(ehci, 0, &ehci->regs->intr_enable);
8c03356a
AS
371
372 /* re-init operational registers */
083522d7
BH
373 ehci_writel(ehci, 0, &ehci->regs->segment);
374 ehci_writel(ehci, ehci->periodic_dma, &ehci->regs->frame_list);
375 ehci_writel(ehci, (u32) ehci->async->qh_dma, &ehci->regs->async_next);
1da177e4
LT
376
377 /* restore CMD_RUN, framelist size, and irq threshold */
3d9545cc 378 ehci->command |= CMD_RUN;
083522d7 379 ehci_writel(ehci, ehci->command, &ehci->regs->command);
e8799906 380 ehci->rh_state = EHCI_RH_RUNNING;
1da177e4 381
e198a314
AS
382 /* Some controller/firmware combinations need a delay during which
383 * they set up the port statuses. See Bugzilla #8190. */
3a4e72cb
VP
384 spin_unlock_irq(&ehci->lock);
385 msleep(8);
386 spin_lock_irq(&ehci->lock);
e198a314 387
16032c4f
AS
388 /* clear phy low-power mode before resume */
389 if (ehci->bus_suspended && ehci->has_hostpc) {
390 i = HCS_N_PORTS(ehci->hcs_params);
391 while (i--) {
392 if (test_bit(i, &ehci->bus_suspended)) {
a46af4eb
AS
393 u32 __iomem *hostpc_reg =
394 &ehci->regs->hostpc[i];
16032c4f 395
16032c4f
AS
396 temp = ehci_readl(ehci, hostpc_reg);
397 ehci_writel(ehci, temp & ~HOSTPC_PHCD,
398 hostpc_reg);
399 }
400 }
401 spin_unlock_irq(&ehci->lock);
402 msleep(5);
403 spin_lock_irq(&ehci->lock);
404 }
405
8c03356a 406 /* manually resume the ports we suspended during bus_suspend() */
1da177e4
LT
407 i = HCS_N_PORTS (ehci->hcs_params);
408 while (i--) {
083522d7 409 temp = ehci_readl(ehci, &ehci->regs->port_status [i]);
58a97ffe 410 temp &= ~(PORT_RWC_BITS | PORT_WAKE_BITS);
8c03356a 411 if (test_bit(i, &ehci->bus_suspended) &&
3a4e72cb 412 (temp & PORT_SUSPEND)) {
1da177e4 413 temp |= PORT_RESUME;
d0f2fb25 414 set_bit(i, &resume_needed);
3a4e72cb 415 }
083522d7 416 ehci_writel(ehci, temp, &ehci->regs->port_status [i]);
1da177e4 417 }
3a4e72cb
VP
418
419 /* msleep for 20ms only if code is trying to resume port */
420 if (resume_needed) {
421 spin_unlock_irq(&ehci->lock);
422 msleep(20);
423 spin_lock_irq(&ehci->lock);
424 }
425
1da177e4 426 i = HCS_N_PORTS (ehci->hcs_params);
1da177e4 427 while (i--) {
083522d7 428 temp = ehci_readl(ehci, &ehci->regs->port_status [i]);
d0f2fb25 429 if (test_bit(i, &resume_needed)) {
8c03356a 430 temp &= ~(PORT_RWC_BITS | PORT_RESUME);
083522d7 431 ehci_writel(ehci, temp, &ehci->regs->port_status [i]);
8c03356a
AS
432 ehci_vdbg (ehci, "resumed port %d\n", i + 1);
433 }
1da177e4 434 }
1da177e4
LT
435
436 ehci->next_statechange = jiffies + msecs_to_jiffies(5);
c4f34764
AS
437 spin_unlock_irq(&ehci->lock);
438
439 ehci_handover_companion_ports(ehci);
1da177e4
LT
440
441 /* Now we can safely re-enable irqs */
083522d7 442 ehci_writel(ehci, INTR_MASK, &ehci->regs->intr_enable);
15be105b 443 (void) ehci_readl(ehci, &ehci->regs->intr_enable);
1da177e4 444
1da177e4
LT
445 return 0;
446}
447
448#else
449
0c0382e3
AS
450#define ehci_bus_suspend NULL
451#define ehci_bus_resume NULL
1da177e4
LT
452
453#endif /* CONFIG_PM */
454
57e06c11
AS
455/*-------------------------------------------------------------------------*/
456
57e06c11 457/*
90da096e 458 * Sets the owner of a port
57e06c11 459 */
90da096e 460static void set_owner(struct ehci_hcd *ehci, int portnum, int new_owner)
57e06c11 461{
57e06c11
AS
462 u32 __iomem *status_reg;
463 u32 port_status;
90da096e 464 int try;
57e06c11 465
90da096e 466 status_reg = &ehci->regs->port_status[portnum];
57e06c11
AS
467
468 /*
469 * The controller won't set the OWNER bit if the port is
470 * enabled, so this loop will sometimes require at least two
471 * iterations: one to disable the port and one to set OWNER.
472 */
57e06c11
AS
473 for (try = 4; try > 0; --try) {
474 spin_lock_irq(&ehci->lock);
475 port_status = ehci_readl(ehci, status_reg);
476 if ((port_status & PORT_OWNER) == new_owner
477 || (port_status & (PORT_OWNER | PORT_CONNECT))
478 == 0)
479 try = 0;
480 else {
481 port_status ^= PORT_OWNER;
482 port_status &= ~(PORT_PE | PORT_RWC_BITS);
483 ehci_writel(ehci, port_status, status_reg);
484 }
485 spin_unlock_irq(&ehci->lock);
486 if (try > 1)
487 msleep(5);
488 }
90da096e
BR
489}
490
1da177e4
LT
491/*-------------------------------------------------------------------------*/
492
493static int check_reset_complete (
494 struct ehci_hcd *ehci,
495 int index,
e6316565 496 u32 __iomem *status_reg,
1da177e4
LT
497 int port_status
498) {
cd4cdc93 499 if (!(port_status & PORT_CONNECT))
1da177e4 500 return port_status;
1da177e4
LT
501
502 /* if reset finished and it's still not enabled -- handoff */
503 if (!(port_status & PORT_PE)) {
504
505 /* with integrated TT, there's nobody to hand it to! */
506 if (ehci_is_TDI(ehci)) {
507 ehci_dbg (ehci,
508 "Failed to enable port %d on root hub TT\n",
509 index+1);
510 return port_status;
511 }
512
513 ehci_dbg (ehci, "port %d full speed --> companion\n",
514 index + 1);
515
516 // what happens if HCS_N_CC(params) == 0 ?
517 port_status |= PORT_OWNER;
10f6524a 518 port_status &= ~PORT_RWC_BITS;
e6316565 519 ehci_writel(ehci, port_status, status_reg);
1da177e4 520
796bcae7
VB
521 /* ensure 440EPX ohci controller state is operational */
522 if (ehci->has_amcc_usb23)
523 set_ohci_hcfs(ehci, 1);
524 } else {
0ca7eb23
PC
525 ehci_dbg(ehci, "port %d reset complete, port enabled\n",
526 index + 1);
796bcae7
VB
527 /* ensure 440EPx ohci controller state is suspended */
528 if (ehci->has_amcc_usb23)
529 set_ohci_hcfs(ehci, 0);
530 }
1da177e4
LT
531
532 return port_status;
533}
534
535/*-------------------------------------------------------------------------*/
536
537
538/* build "status change" packet (one or two bytes) from HC registers */
539
540static int
541ehci_hub_status_data (struct usb_hcd *hcd, char *buf)
542{
543 struct ehci_hcd *ehci = hcd_to_ehci (hcd);
a448e4dc 544 u32 temp, status;
93f1a47c 545 u32 mask;
1da177e4
LT
546 int ports, i, retval = 1;
547 unsigned long flags;
5a9cdf33 548 u32 ppcd = 0;
1da177e4 549
1da177e4
LT
550 /* init status to no-changes */
551 buf [0] = 0;
552 ports = HCS_N_PORTS (ehci->hcs_params);
553 if (ports > 7) {
554 buf [1] = 0;
555 retval++;
556 }
53bd6a60 557
a448e4dc
AS
558 /* Inform the core about resumes-in-progress by returning
559 * a non-zero value even if there are no status changes.
560 */
561 status = ehci->resuming_ports;
562
93f1a47c
DB
563 /* Some boards (mostly VIA?) report bogus overcurrent indications,
564 * causing massive log spam unless we completely ignore them. It
3a4fa0a2 565 * may be relevant that VIA VT8235 controllers, where PORT_POWER is
93f1a47c
DB
566 * always set, seem to clear PORT_OCC and PORT_CSC when writing to
567 * PORT_POWER; that's surprising, but maybe within-spec.
568 */
569 if (!ignore_oc)
570 mask = PORT_CSC | PORT_PEC | PORT_OCC;
571 else
572 mask = PORT_CSC | PORT_PEC;
573 // PORT_RESUME from hardware ~= PORT_STAT_C_SUSPEND
574
1da177e4
LT
575 /* no hub change reports (bit 0) for now (power, ...) */
576
577 /* port N changes (bit N)? */
578 spin_lock_irqsave (&ehci->lock, flags);
5a9cdf33
AD
579
580 /* get per-port change detect bits */
581 if (ehci->has_ppcd)
582 ppcd = ehci_readl(ehci, &ehci->regs->status) >> 16;
583
1da177e4 584 for (i = 0; i < ports; i++) {
5a9cdf33
AD
585 /* leverage per-port change bits feature */
586 if (ehci->has_ppcd && !(ppcd & (1 << i)))
587 continue;
083522d7 588 temp = ehci_readl(ehci, &ehci->regs->port_status [i]);
625b5c9a
AS
589
590 /*
591 * Return status information even for ports with OWNER set.
592 * Otherwise khubd wouldn't see the disconnect event when a
593 * high-speed device is switched over to the companion
594 * controller by the user.
595 */
596
eafe5b99
AS
597 if ((temp & mask) != 0 || test_bit(i, &ehci->port_c_suspend)
598 || (ehci->reset_done[i] && time_after_eq(
599 jiffies, ehci->reset_done[i]))) {
1da177e4
LT
600 if (i < 7)
601 buf [0] |= 1 << (i + 1);
602 else
603 buf [1] |= 1 << (i - 7);
604 status = STS_PCD;
605 }
606 }
607 /* FIXME autosuspend idle root hubs */
608 spin_unlock_irqrestore (&ehci->lock, flags);
609 return status ? retval : 0;
610}
611
612/*-------------------------------------------------------------------------*/
613
614static void
615ehci_hub_descriptor (
616 struct ehci_hcd *ehci,
617 struct usb_hub_descriptor *desc
618) {
619 int ports = HCS_N_PORTS (ehci->hcs_params);
620 u16 temp;
621
622 desc->bDescriptorType = 0x29;
623 desc->bPwrOn2PwrGood = 10; /* ehci 1.0, 2.3.9 says 20ms max */
624 desc->bHubContrCurrent = 0;
625
626 desc->bNbrPorts = ports;
627 temp = 1 + (ports / 8);
628 desc->bDescLength = 7 + 2 * temp;
629
630 /* two bitmaps: ports removable, and usb 1.0 legacy PortPwrCtrlMask */
dbe79bbe
JY
631 memset(&desc->u.hs.DeviceRemovable[0], 0, temp);
632 memset(&desc->u.hs.DeviceRemovable[temp], 0xff, temp);
1da177e4
LT
633
634 temp = 0x0008; /* per-port overcurrent reporting */
635 if (HCS_PPC (ehci->hcs_params))
636 temp |= 0x0001; /* per-port power control */
56c1e26d
DB
637 else
638 temp |= 0x0002; /* no power switching */
1da177e4
LT
639#if 0
640// re-enable when we support USB_PORT_FEAT_INDICATOR below.
641 if (HCS_INDICATOR (ehci->hcs_params))
642 temp |= 0x0080; /* per-port indicators (LEDs) */
643#endif
fd05e720 644 desc->wHubCharacteristics = cpu_to_le16(temp);
1da177e4
LT
645}
646
647/*-------------------------------------------------------------------------*/
648
1da177e4
LT
649static int ehci_hub_control (
650 struct usb_hcd *hcd,
651 u16 typeReq,
652 u16 wValue,
653 u16 wIndex,
654 char *buf,
655 u16 wLength
656) {
657 struct ehci_hcd *ehci = hcd_to_ehci (hcd);
658 int ports = HCS_N_PORTS (ehci->hcs_params);
383975d7
AS
659 u32 __iomem *status_reg = &ehci->regs->port_status[
660 (wIndex & 0xff) - 1];
a46af4eb 661 u32 __iomem *hostpc_reg = &ehci->regs->hostpc[(wIndex & 0xff) - 1];
331ac6b2 662 u32 temp, temp1, status;
1da177e4
LT
663 unsigned long flags;
664 int retval = 0;
f0d7f273 665 unsigned selector;
1da177e4
LT
666
667 /*
668 * FIXME: support SetPortFeatures USB_PORT_FEAT_INDICATOR.
669 * HCS_INDICATOR may say we can change LEDs to off/amber/green.
670 * (track current state ourselves) ... blink for diagnostics,
671 * power, "this is the one", etc. EHCI spec supports this.
672 */
673
674 spin_lock_irqsave (&ehci->lock, flags);
675 switch (typeReq) {
676 case ClearHubFeature:
677 switch (wValue) {
678 case C_HUB_LOCAL_POWER:
679 case C_HUB_OVER_CURRENT:
680 /* no hub-wide feature/status flags */
681 break;
682 default:
683 goto error;
684 }
685 break;
686 case ClearPortFeature:
687 if (!wIndex || wIndex > ports)
688 goto error;
689 wIndex--;
e6316565 690 temp = ehci_readl(ehci, status_reg);
6d5f89c7 691 temp &= ~PORT_RWC_BITS;
625b5c9a
AS
692
693 /*
694 * Even if OWNER is set, so the port is owned by the
695 * companion controller, khubd needs to be able to clear
696 * the port-change status bits (especially
749da5f8 697 * USB_PORT_STAT_C_CONNECTION).
625b5c9a 698 */
1da177e4
LT
699
700 switch (wValue) {
701 case USB_PORT_FEAT_ENABLE:
e6316565 702 ehci_writel(ehci, temp & ~PORT_PE, status_reg);
1da177e4
LT
703 break;
704 case USB_PORT_FEAT_C_ENABLE:
6d5f89c7 705 ehci_writel(ehci, temp | PORT_PEC, status_reg);
1da177e4
LT
706 break;
707 case USB_PORT_FEAT_SUSPEND:
708 if (temp & PORT_RESET)
709 goto error;
f8aeb3bb
DB
710 if (ehci->no_selective_suspend)
711 break;
83722bc9
AG
712#ifdef CONFIG_USB_OTG
713 if ((hcd->self.otg_port == (wIndex + 1))
714 && hcd->self.b_hnp_enable) {
c2e935a7 715 otg_start_hnp(hcd->phy->otg);
83722bc9
AG
716 break;
717 }
718#endif
16032c4f
AS
719 if (!(temp & PORT_SUSPEND))
720 break;
721 if ((temp & PORT_PE) == 0)
722 goto error;
723
724 /* clear phy low-power mode before resume */
a46af4eb 725 if (ehci->has_hostpc) {
16032c4f
AS
726 temp1 = ehci_readl(ehci, hostpc_reg);
727 ehci_writel(ehci, temp1 & ~HOSTPC_PHCD,
eab80de0 728 hostpc_reg);
16032c4f
AS
729 spin_unlock_irqrestore(&ehci->lock, flags);
730 msleep(5);/* wait to leave low-power mode */
731 spin_lock_irqsave(&ehci->lock, flags);
1da177e4 732 }
16032c4f 733 /* resume signaling for 20 msec */
6d5f89c7 734 temp &= ~PORT_WAKE_BITS;
16032c4f
AS
735 ehci_writel(ehci, temp | PORT_RESUME, status_reg);
736 ehci->reset_done[wIndex] = jiffies
737 + msecs_to_jiffies(20);
1da177e4
LT
738 break;
739 case USB_PORT_FEAT_C_SUSPEND:
d1f114d1 740 clear_bit(wIndex, &ehci->port_c_suspend);
1da177e4
LT
741 break;
742 case USB_PORT_FEAT_POWER:
743 if (HCS_PPC (ehci->hcs_params))
6d5f89c7
SW
744 ehci_writel(ehci, temp & ~PORT_POWER,
745 status_reg);
1da177e4
LT
746 break;
747 case USB_PORT_FEAT_C_CONNECTION:
48f24970
AD
748 if (ehci->has_lpm) {
749 /* clear PORTSC bits on disconnect */
750 temp &= ~PORT_LPM;
751 temp &= ~PORT_DEV_ADDR;
752 }
6d5f89c7 753 ehci_writel(ehci, temp | PORT_CSC, status_reg);
1da177e4
LT
754 break;
755 case USB_PORT_FEAT_C_OVER_CURRENT:
6d5f89c7 756 ehci_writel(ehci, temp | PORT_OCC, status_reg);
1da177e4
LT
757 break;
758 case USB_PORT_FEAT_C_RESET:
759 /* GetPortStatus clears reset */
760 break;
761 default:
762 goto error;
763 }
083522d7 764 ehci_readl(ehci, &ehci->regs->command); /* unblock posted write */
1da177e4
LT
765 break;
766 case GetHubDescriptor:
767 ehci_hub_descriptor (ehci, (struct usb_hub_descriptor *)
768 buf);
769 break;
770 case GetHubStatus:
771 /* no hub-wide feature/status flags */
772 memset (buf, 0, 4);
773 //cpu_to_le32s ((u32 *) buf);
774 break;
775 case GetPortStatus:
776 if (!wIndex || wIndex > ports)
777 goto error;
778 wIndex--;
779 status = 0;
e6316565 780 temp = ehci_readl(ehci, status_reg);
1da177e4
LT
781
782 // wPortChange bits
783 if (temp & PORT_CSC)
749da5f8 784 status |= USB_PORT_STAT_C_CONNECTION << 16;
1da177e4 785 if (temp & PORT_PEC)
749da5f8 786 status |= USB_PORT_STAT_C_ENABLE << 16;
756aa6b3
CE
787
788 if ((temp & PORT_OCC) && !ignore_oc){
749da5f8 789 status |= USB_PORT_STAT_C_OVERCURRENT << 16;
1da177e4 790
756aa6b3
CE
791 /*
792 * Hubs should disable port power on over-current.
793 * However, not all EHCI implementations do this
794 * automatically, even if they _do_ support per-port
795 * power switching; they're allowed to just limit the
796 * current. khubd will turn the power back on.
797 */
81463c1d 798 if ((temp & PORT_OC) && HCS_PPC(ehci->hcs_params)) {
756aa6b3
CE
799 ehci_writel(ehci,
800 temp & ~(PORT_RWC_BITS | PORT_POWER),
801 status_reg);
81463c1d 802 temp = ehci_readl(ehci, status_reg);
756aa6b3
CE
803 }
804 }
805
1da177e4 806 /* whoever resumes must GetPortStatus to complete it!! */
629e4427
AS
807 if (temp & PORT_RESUME) {
808
809 /* Remote Wakeup received? */
810 if (!ehci->reset_done[wIndex]) {
811 /* resume signaling for 20 msec */
812 ehci->reset_done[wIndex] = jiffies
813 + msecs_to_jiffies(20);
814 /* check the port again */
815 mod_timer(&ehci_to_hcd(ehci)->rh_timer,
816 ehci->reset_done[wIndex]);
817 }
1da177e4 818
629e4427
AS
819 /* resume completed? */
820 else if (time_after_eq(jiffies,
821 ehci->reset_done[wIndex])) {
eafe5b99 822 clear_bit(wIndex, &ehci->suspended_ports);
d1f114d1 823 set_bit(wIndex, &ehci->port_c_suspend);
629e4427
AS
824 ehci->reset_done[wIndex] = 0;
825
826 /* stop resume signaling */
827 temp = ehci_readl(ehci, status_reg);
828 ehci_writel(ehci,
e6316565
AS
829 temp & ~(PORT_RWC_BITS | PORT_RESUME),
830 status_reg);
a448e4dc 831 clear_bit(wIndex, &ehci->resuming_ports);
629e4427 832 retval = handshake(ehci, status_reg,
083522d7 833 PORT_RESUME, 0, 2000 /* 2msec */);
629e4427
AS
834 if (retval != 0) {
835 ehci_err(ehci,
836 "port %d resume error %d\n",
837 wIndex + 1, retval);
838 goto error;
839 }
840 temp &= ~(PORT_SUSPEND|PORT_RESUME|(3<<10));
1da177e4 841 }
1da177e4
LT
842 }
843
844 /* whoever resets must GetPortStatus to complete it!! */
845 if ((temp & PORT_RESET)
629e4427
AS
846 && time_after_eq(jiffies,
847 ehci->reset_done[wIndex])) {
749da5f8 848 status |= USB_PORT_STAT_C_RESET << 16;
1da177e4 849 ehci->reset_done [wIndex] = 0;
a448e4dc 850 clear_bit(wIndex, &ehci->resuming_ports);
1da177e4
LT
851
852 /* force reset to complete */
083522d7 853 ehci_writel(ehci, temp & ~(PORT_RWC_BITS | PORT_RESET),
e6316565 854 status_reg);
c22fa3ac
DB
855 /* REVISIT: some hardware needs 550+ usec to clear
856 * this bit; seems too long to spin routinely...
857 */
e6316565 858 retval = handshake(ehci, status_reg,
6307e096 859 PORT_RESET, 0, 1000);
1da177e4
LT
860 if (retval != 0) {
861 ehci_err (ehci, "port %d reset error %d\n",
862 wIndex + 1, retval);
863 goto error;
864 }
865
866 /* see what we found out */
e6316565
AS
867 temp = check_reset_complete (ehci, wIndex, status_reg,
868 ehci_readl(ehci, status_reg));
1da177e4
LT
869 }
870
a448e4dc 871 if (!(temp & (PORT_RESUME|PORT_RESET))) {
eafe5b99 872 ehci->reset_done[wIndex] = 0;
a448e4dc
AS
873 clear_bit(wIndex, &ehci->resuming_ports);
874 }
eafe5b99 875
57e06c11
AS
876 /* transfer dedicated ports to the companion hc */
877 if ((temp & PORT_CONNECT) &&
878 test_bit(wIndex, &ehci->companion_ports)) {
879 temp &= ~PORT_RWC_BITS;
880 temp |= PORT_OWNER;
881 ehci_writel(ehci, temp, status_reg);
882 ehci_dbg(ehci, "port %d --> companion\n", wIndex + 1);
883 temp = ehci_readl(ehci, status_reg);
884 }
885
625b5c9a
AS
886 /*
887 * Even if OWNER is set, there's no harm letting khubd
888 * see the wPortStatus values (they should all be 0 except
889 * for PORT_POWER anyway).
890 */
891
892 if (temp & PORT_CONNECT) {
749da5f8 893 status |= USB_PORT_STAT_CONNECTION;
625b5c9a 894 // status may be from integrated TT
331ac6b2
AD
895 if (ehci->has_hostpc) {
896 temp1 = ehci_readl(ehci, hostpc_reg);
897 status |= ehci_port_speed(ehci, temp1);
898 } else
899 status |= ehci_port_speed(ehci, temp);
1da177e4 900 }
625b5c9a 901 if (temp & PORT_PE)
749da5f8 902 status |= USB_PORT_STAT_ENABLE;
eafe5b99
AS
903
904 /* maybe the port was unsuspended without our knowledge */
905 if (temp & (PORT_SUSPEND|PORT_RESUME)) {
749da5f8 906 status |= USB_PORT_STAT_SUSPEND;
eafe5b99
AS
907 } else if (test_bit(wIndex, &ehci->suspended_ports)) {
908 clear_bit(wIndex, &ehci->suspended_ports);
a448e4dc 909 clear_bit(wIndex, &ehci->resuming_ports);
eafe5b99
AS
910 ehci->reset_done[wIndex] = 0;
911 if (temp & PORT_PE)
912 set_bit(wIndex, &ehci->port_c_suspend);
913 }
914
625b5c9a 915 if (temp & PORT_OC)
749da5f8 916 status |= USB_PORT_STAT_OVERCURRENT;
625b5c9a 917 if (temp & PORT_RESET)
749da5f8 918 status |= USB_PORT_STAT_RESET;
625b5c9a 919 if (temp & PORT_POWER)
749da5f8 920 status |= USB_PORT_STAT_POWER;
d1f114d1 921 if (test_bit(wIndex, &ehci->port_c_suspend))
749da5f8 922 status |= USB_PORT_STAT_C_SUSPEND << 16;
1da177e4 923
9776afc8 924#ifndef VERBOSE_DEBUG
1da177e4
LT
925 if (status & ~0xffff) /* only if wPortChange is interesting */
926#endif
927 dbg_port (ehci, "GetStatus", wIndex + 1, temp);
a5abdeaf 928 put_unaligned_le32(status, buf);
1da177e4
LT
929 break;
930 case SetHubFeature:
931 switch (wValue) {
932 case C_HUB_LOCAL_POWER:
933 case C_HUB_OVER_CURRENT:
934 /* no hub-wide feature/status flags */
935 break;
936 default:
937 goto error;
938 }
939 break;
940 case SetPortFeature:
f0d7f273
DB
941 selector = wIndex >> 8;
942 wIndex &= 0xff;
8d053c79
JW
943 if (unlikely(ehci->debug)) {
944 /* If the debug port is active any port
945 * feature requests should get denied */
946 if (wIndex == HCS_DEBUG_PORT(ehci->hcs_params) &&
947 (readl(&ehci->debug->control) & DBGP_ENABLED)) {
948 retval = -ENODEV;
949 goto error_exit;
950 }
951 }
1da177e4
LT
952 if (!wIndex || wIndex > ports)
953 goto error;
954 wIndex--;
e6316565 955 temp = ehci_readl(ehci, status_reg);
1da177e4
LT
956 if (temp & PORT_OWNER)
957 break;
958
10f6524a 959 temp &= ~PORT_RWC_BITS;
1da177e4
LT
960 switch (wValue) {
961 case USB_PORT_FEAT_SUSPEND:
f8aeb3bb
DB
962 if (ehci->no_selective_suspend)
963 break;
1da177e4
LT
964 if ((temp & PORT_PE) == 0
965 || (temp & PORT_RESET) != 0)
966 goto error;
b9df7942 967
331ac6b2
AD
968 /* After above check the port must be connected.
969 * Set appropriate bit thus could put phy into low power
970 * mode if we have hostpc feature
971 */
b9df7942
AD
972 temp &= ~PORT_WKCONN_E;
973 temp |= PORT_WKDISC_E | PORT_WKOC_E;
974 ehci_writel(ehci, temp | PORT_SUSPEND, status_reg);
a46af4eb 975 if (ehci->has_hostpc) {
b9df7942 976 spin_unlock_irqrestore(&ehci->lock, flags);
331ac6b2 977 msleep(5);/* 5ms for HCD enter low pwr mode */
b9df7942 978 spin_lock_irqsave(&ehci->lock, flags);
331ac6b2
AD
979 temp1 = ehci_readl(ehci, hostpc_reg);
980 ehci_writel(ehci, temp1 | HOSTPC_PHCD,
981 hostpc_reg);
982 temp1 = ehci_readl(ehci, hostpc_reg);
983 ehci_dbg(ehci, "Port%d phy low pwr mode %s\n",
984 wIndex, (temp1 & HOSTPC_PHCD) ?
985 "succeeded" : "failed");
986 }
eafe5b99 987 set_bit(wIndex, &ehci->suspended_ports);
1da177e4
LT
988 break;
989 case USB_PORT_FEAT_POWER:
990 if (HCS_PPC (ehci->hcs_params))
083522d7 991 ehci_writel(ehci, temp | PORT_POWER,
e6316565 992 status_reg);
1da177e4
LT
993 break;
994 case USB_PORT_FEAT_RESET:
995 if (temp & PORT_RESUME)
996 goto error;
997 /* line status bits may report this as low speed,
998 * which can be fine if this root hub has a
999 * transaction translator built in.
1000 */
1001 if ((temp & (PORT_PE|PORT_CONNECT)) == PORT_CONNECT
1002 && !ehci_is_TDI(ehci)
1003 && PORT_USB11 (temp)) {
1004 ehci_dbg (ehci,
1005 "port %d low speed --> companion\n",
1006 wIndex + 1);
1007 temp |= PORT_OWNER;
1008 } else {
1009 ehci_vdbg (ehci, "port %d reset\n", wIndex + 1);
1010 temp |= PORT_RESET;
1011 temp &= ~PORT_PE;
1012
1013 /*
1014 * caller must wait, then call GetPortStatus
1015 * usb 2.0 spec says 50 ms resets on root
1016 */
1017 ehci->reset_done [wIndex] = jiffies
1018 + msecs_to_jiffies (50);
1019 }
e6316565 1020 ehci_writel(ehci, temp, status_reg);
1da177e4 1021 break;
f0d7f273
DB
1022
1023 /* For downstream facing ports (these): one hub port is put
1024 * into test mode according to USB2 11.24.2.13, then the hub
1025 * must be reset (which for root hub now means rmmod+modprobe,
1026 * or else system reboot). See EHCI 2.3.9 and 4.14 for info
1027 * about the EHCI-specific stuff.
1028 */
1029 case USB_PORT_FEAT_TEST:
1030 if (!selector || selector > 5)
1031 goto error;
c4f34764 1032 spin_unlock_irqrestore(&ehci->lock, flags);
f0d7f273 1033 ehci_quiesce(ehci);
c4f34764 1034 spin_lock_irqsave(&ehci->lock, flags);
77636c86
BT
1035
1036 /* Put all enabled ports into suspend */
1037 while (ports--) {
1038 u32 __iomem *sreg =
1039 &ehci->regs->port_status[ports];
1040
1041 temp = ehci_readl(ehci, sreg) & ~PORT_RWC_BITS;
1042 if (temp & PORT_PE)
1043 ehci_writel(ehci, temp | PORT_SUSPEND,
1044 sreg);
1045 }
c4f34764
AS
1046
1047 spin_unlock_irqrestore(&ehci->lock, flags);
f0d7f273 1048 ehci_halt(ehci);
c4f34764
AS
1049 spin_lock_irqsave(&ehci->lock, flags);
1050
77636c86 1051 temp = ehci_readl(ehci, status_reg);
f0d7f273 1052 temp |= selector << 16;
e6316565 1053 ehci_writel(ehci, temp, status_reg);
f0d7f273
DB
1054 break;
1055
1da177e4
LT
1056 default:
1057 goto error;
1058 }
083522d7 1059 ehci_readl(ehci, &ehci->regs->command); /* unblock posted writes */
1da177e4
LT
1060 break;
1061
1062 default:
1063error:
1064 /* "stall" on error */
1065 retval = -EPIPE;
1066 }
8d053c79 1067error_exit:
1da177e4
LT
1068 spin_unlock_irqrestore (&ehci->lock, flags);
1069 return retval;
1070}
90da096e 1071
5407a3c3
FB
1072static void __maybe_unused ehci_relinquish_port(struct usb_hcd *hcd,
1073 int portnum)
90da096e
BR
1074{
1075 struct ehci_hcd *ehci = hcd_to_ehci(hcd);
1076
1077 if (ehci_is_TDI(ehci))
1078 return;
1079 set_owner(ehci, --portnum, PORT_OWNER);
1080}
1081
5407a3c3
FB
1082static int __maybe_unused ehci_port_handed_over(struct usb_hcd *hcd,
1083 int portnum)
3a31155c
AS
1084{
1085 struct ehci_hcd *ehci = hcd_to_ehci(hcd);
1086 u32 __iomem *reg;
1087
1088 if (ehci_is_TDI(ehci))
1089 return 0;
1090 reg = &ehci->regs->port_status[portnum - 1];
1091 return ehci_readl(ehci, reg) & PORT_OWNER;
1092}
This page took 0.816656 seconds and 5 git commands to generate.