]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - drivers/usb/host/ehci-hub.c
USB: otg: twl4030-usb.c: mark .init as subsys_initcall_sync
[mirror_ubuntu-artful-kernel.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/*-------------------------------------------------------------------------*/
30
58a97ffe
AS
31#define PORT_WAKE_BITS (PORT_WKOC_E|PORT_WKDISC_E|PORT_WKCONN_E)
32
aff6d18f
AS
33#ifdef CONFIG_PM
34
383975d7
AS
35static 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];
3c519b84 65 status = ehci_readl(ehci, reg) & ~PORT_RWC_BITS;
383975d7
AS
66
67 /* Port already owned by companion? */
68 if (status & PORT_OWNER)
69 clear_bit(port, &ehci->owned_ports);
3c519b84
AS
70 else if (test_bit(port, &ehci->companion_ports))
71 ehci_writel(ehci, status & ~PORT_PE, reg);
383975d7
AS
72 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
0c0382e3 109static int ehci_bus_suspend (struct usb_hcd *hcd)
1da177e4
LT
110{
111 struct ehci_hcd *ehci = hcd_to_ehci (hcd);
112 int port;
8c03356a 113 int mask;
331ac6b2 114 u32 __iomem *hostpc_reg = NULL;
1da177e4 115
8c774fe8
AS
116 ehci_dbg(ehci, "suspend root hub\n");
117
1da177e4
LT
118 if (time_before (jiffies, ehci->next_statechange))
119 msleep(5);
f8fa7571
AS
120 del_timer_sync(&ehci->watchdog);
121 del_timer_sync(&ehci->iaa_watchdog);
1da177e4
LT
122
123 port = HCS_N_PORTS (ehci->hcs_params);
124 spin_lock_irq (&ehci->lock);
125
126 /* stop schedules, clean any completed work */
127 if (HC_IS_RUNNING(hcd->state)) {
128 ehci_quiesce (ehci);
129 hcd->state = HC_STATE_QUIESCING;
130 }
083522d7 131 ehci->command = ehci_readl(ehci, &ehci->regs->command);
7d12e780 132 ehci_work(ehci);
1da177e4 133
8c03356a
AS
134 /* Unlike other USB host controller types, EHCI doesn't have
135 * any notion of "global" or bus-wide suspend. The driver has
136 * to manually suspend all the active unsuspended ports, and
137 * then manually resume them in the bus_resume() routine.
138 */
139 ehci->bus_suspended = 0;
383975d7 140 ehci->owned_ports = 0;
1da177e4
LT
141 while (port--) {
142 u32 __iomem *reg = &ehci->regs->port_status [port];
083522d7 143 u32 t1 = ehci_readl(ehci, reg) & ~PORT_RWC_BITS;
1da177e4
LT
144 u32 t2 = t1;
145
331ac6b2
AD
146 if (ehci->has_hostpc)
147 hostpc_reg = (u32 __iomem *)((u8 *)ehci->regs
148 + HOSTPC0 + 4 * (port & 0xff));
8c03356a 149 /* keep track of which ports we suspend */
383975d7
AS
150 if (t1 & PORT_OWNER)
151 set_bit(port, &ehci->owned_ports);
152 else if ((t1 & PORT_PE) && !(t1 & PORT_SUSPEND)) {
1da177e4 153 t2 |= PORT_SUSPEND;
8c03356a
AS
154 set_bit(port, &ehci->bus_suspended);
155 }
156
157 /* enable remote wakeup on all ports */
331ac6b2
AD
158 if (hcd->self.root_hub->do_remote_wakeup) {
159 /* only enable appropriate wake bits, otherwise the
160 * hardware can not go phy low power mode. If a race
161 * condition happens here(connection change during bits
162 * set), the port change detection will finally fix it.
163 */
164 if (t1 & PORT_CONNECT) {
165 t2 |= PORT_WKOC_E | PORT_WKDISC_E;
166 t2 &= ~PORT_WKCONN_E;
167 } else {
168 t2 |= PORT_WKOC_E | PORT_WKCONN_E;
169 t2 &= ~PORT_WKDISC_E;
170 }
171 } else
58a97ffe 172 t2 &= ~PORT_WAKE_BITS;
1da177e4
LT
173
174 if (t1 != t2) {
175 ehci_vdbg (ehci, "port %d, %08x -> %08x\n",
176 port + 1, t1, t2);
083522d7 177 ehci_writel(ehci, t2, reg);
331ac6b2
AD
178 if (hostpc_reg) {
179 u32 t3;
180
181 msleep(5);/* 5ms for HCD enter low pwr mode */
182 t3 = ehci_readl(ehci, hostpc_reg);
183 ehci_writel(ehci, t3 | HOSTPC_PHCD, hostpc_reg);
184 t3 = ehci_readl(ehci, hostpc_reg);
185 ehci_dbg(ehci, "Port%d phy low pwr mode %s\n",
186 port, (t3 & HOSTPC_PHCD) ?
187 "succeeded" : "failed");
188 }
1da177e4
LT
189 }
190 }
191
cd930c93
AS
192 /* Apparently some devices need a >= 1-uframe delay here */
193 if (ehci->bus_suspended)
194 udelay(150);
195
1da177e4
LT
196 /* turn off now-idle HC */
197 ehci_halt (ehci);
198 hcd->state = HC_STATE_SUSPENDED;
199
cdc647a9
DB
200 if (ehci->reclaim)
201 end_unlink_async(ehci);
202
8c03356a
AS
203 /* allow remote wakeup */
204 mask = INTR_MASK;
58a97ffe 205 if (!hcd->self.root_hub->do_remote_wakeup)
8c03356a 206 mask &= ~STS_PCD;
083522d7
BH
207 ehci_writel(ehci, mask, &ehci->regs->intr_enable);
208 ehci_readl(ehci, &ehci->regs->intr_enable);
8c03356a 209
1da177e4
LT
210 ehci->next_statechange = jiffies + msecs_to_jiffies(10);
211 spin_unlock_irq (&ehci->lock);
212 return 0;
213}
214
215
216/* caller has locked the root hub, and should reset/reinit on error */
0c0382e3 217static int ehci_bus_resume (struct usb_hcd *hcd)
1da177e4
LT
218{
219 struct ehci_hcd *ehci = hcd_to_ehci (hcd);
220 u32 temp;
383975d7 221 u32 power_okay;
1da177e4 222 int i;
3a4e72cb 223 u8 resume_needed = 0;
1da177e4
LT
224
225 if (time_before (jiffies, ehci->next_statechange))
226 msleep(5);
227 spin_lock_irq (&ehci->lock);
cfa59dab
AS
228 if (!test_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags)) {
229 spin_unlock_irq(&ehci->lock);
230 return -ESHUTDOWN;
231 }
1da177e4 232
f03c17fc
DB
233 /* Ideally and we've got a real resume here, and no port's power
234 * was lost. (For PCI, that means Vaux was maintained.) But we
235 * could instead be restoring a swsusp snapshot -- so that BIOS was
236 * the last user of the controller, not reset/pm hardware keeping
237 * state we gave to it.
238 */
383975d7
AS
239 power_okay = ehci_readl(ehci, &ehci->regs->intr_enable);
240 ehci_dbg(ehci, "resume root hub%s\n",
241 power_okay ? "" : " after power loss");
f03c17fc 242
8c03356a
AS
243 /* at least some APM implementations will try to deliver
244 * IRQs right away, so delay them until we're ready.
245 */
083522d7 246 ehci_writel(ehci, 0, &ehci->regs->intr_enable);
8c03356a
AS
247
248 /* re-init operational registers */
083522d7
BH
249 ehci_writel(ehci, 0, &ehci->regs->segment);
250 ehci_writel(ehci, ehci->periodic_dma, &ehci->regs->frame_list);
251 ehci_writel(ehci, (u32) ehci->async->qh_dma, &ehci->regs->async_next);
1da177e4
LT
252
253 /* restore CMD_RUN, framelist size, and irq threshold */
083522d7 254 ehci_writel(ehci, ehci->command, &ehci->regs->command);
1da177e4 255
e198a314
AS
256 /* Some controller/firmware combinations need a delay during which
257 * they set up the port statuses. See Bugzilla #8190. */
3a4e72cb
VP
258 spin_unlock_irq(&ehci->lock);
259 msleep(8);
260 spin_lock_irq(&ehci->lock);
e198a314 261
8c03356a 262 /* manually resume the ports we suspended during bus_suspend() */
1da177e4
LT
263 i = HCS_N_PORTS (ehci->hcs_params);
264 while (i--) {
083522d7 265 temp = ehci_readl(ehci, &ehci->regs->port_status [i]);
58a97ffe 266 temp &= ~(PORT_RWC_BITS | PORT_WAKE_BITS);
8c03356a 267 if (test_bit(i, &ehci->bus_suspended) &&
3a4e72cb 268 (temp & PORT_SUSPEND)) {
1da177e4 269 temp |= PORT_RESUME;
3a4e72cb
VP
270 resume_needed = 1;
271 }
083522d7 272 ehci_writel(ehci, temp, &ehci->regs->port_status [i]);
1da177e4 273 }
3a4e72cb
VP
274
275 /* msleep for 20ms only if code is trying to resume port */
276 if (resume_needed) {
277 spin_unlock_irq(&ehci->lock);
278 msleep(20);
279 spin_lock_irq(&ehci->lock);
280 }
281
1da177e4 282 i = HCS_N_PORTS (ehci->hcs_params);
1da177e4 283 while (i--) {
083522d7 284 temp = ehci_readl(ehci, &ehci->regs->port_status [i]);
8c03356a
AS
285 if (test_bit(i, &ehci->bus_suspended) &&
286 (temp & PORT_SUSPEND)) {
287 temp &= ~(PORT_RWC_BITS | PORT_RESUME);
083522d7 288 ehci_writel(ehci, temp, &ehci->regs->port_status [i]);
8c03356a
AS
289 ehci_vdbg (ehci, "resumed port %d\n", i + 1);
290 }
1da177e4 291 }
083522d7 292 (void) ehci_readl(ehci, &ehci->regs->command);
1da177e4
LT
293
294 /* maybe re-activate the schedule(s) */
295 temp = 0;
296 if (ehci->async->qh_next.qh)
297 temp |= CMD_ASE;
298 if (ehci->periodic_sched)
299 temp |= CMD_PSE;
300 if (temp) {
301 ehci->command |= temp;
083522d7 302 ehci_writel(ehci, ehci->command, &ehci->regs->command);
1da177e4
LT
303 }
304
305 ehci->next_statechange = jiffies + msecs_to_jiffies(5);
306 hcd->state = HC_STATE_RUNNING;
307
308 /* Now we can safely re-enable irqs */
083522d7 309 ehci_writel(ehci, INTR_MASK, &ehci->regs->intr_enable);
1da177e4
LT
310
311 spin_unlock_irq (&ehci->lock);
3bb1af52 312 ehci_handover_companion_ports(ehci);
1da177e4
LT
313 return 0;
314}
315
316#else
317
0c0382e3
AS
318#define ehci_bus_suspend NULL
319#define ehci_bus_resume NULL
1da177e4
LT
320
321#endif /* CONFIG_PM */
322
57e06c11
AS
323/*-------------------------------------------------------------------------*/
324
325/* Display the ports dedicated to the companion controller */
5a3201b2
TJ
326static ssize_t show_companion(struct device *dev,
327 struct device_attribute *attr,
328 char *buf)
57e06c11
AS
329{
330 struct ehci_hcd *ehci;
331 int nports, index, n;
332 int count = PAGE_SIZE;
333 char *ptr = buf;
334
5a3201b2 335 ehci = hcd_to_ehci(bus_to_hcd(dev_get_drvdata(dev)));
57e06c11
AS
336 nports = HCS_N_PORTS(ehci->hcs_params);
337
338 for (index = 0; index < nports; ++index) {
339 if (test_bit(index, &ehci->companion_ports)) {
340 n = scnprintf(ptr, count, "%d\n", index + 1);
341 ptr += n;
342 count -= n;
343 }
344 }
345 return ptr - buf;
346}
347
348/*
90da096e 349 * Sets the owner of a port
57e06c11 350 */
90da096e 351static void set_owner(struct ehci_hcd *ehci, int portnum, int new_owner)
57e06c11 352{
57e06c11
AS
353 u32 __iomem *status_reg;
354 u32 port_status;
90da096e 355 int try;
57e06c11 356
90da096e 357 status_reg = &ehci->regs->port_status[portnum];
57e06c11
AS
358
359 /*
360 * The controller won't set the OWNER bit if the port is
361 * enabled, so this loop will sometimes require at least two
362 * iterations: one to disable the port and one to set OWNER.
363 */
57e06c11
AS
364 for (try = 4; try > 0; --try) {
365 spin_lock_irq(&ehci->lock);
366 port_status = ehci_readl(ehci, status_reg);
367 if ((port_status & PORT_OWNER) == new_owner
368 || (port_status & (PORT_OWNER | PORT_CONNECT))
369 == 0)
370 try = 0;
371 else {
372 port_status ^= PORT_OWNER;
373 port_status &= ~(PORT_PE | PORT_RWC_BITS);
374 ehci_writel(ehci, port_status, status_reg);
375 }
376 spin_unlock_irq(&ehci->lock);
377 if (try > 1)
378 msleep(5);
379 }
90da096e
BR
380}
381
382/*
383 * Dedicate or undedicate a port to the companion controller.
384 * Syntax is "[-]portnum", where a leading '-' sign means
385 * return control of the port to the EHCI controller.
386 */
387static ssize_t store_companion(struct device *dev,
388 struct device_attribute *attr,
389 const char *buf, size_t count)
390{
391 struct ehci_hcd *ehci;
392 int portnum, new_owner;
393
394 ehci = hcd_to_ehci(bus_to_hcd(dev_get_drvdata(dev)));
395 new_owner = PORT_OWNER; /* Owned by companion */
396 if (sscanf(buf, "%d", &portnum) != 1)
397 return -EINVAL;
398 if (portnum < 0) {
399 portnum = - portnum;
400 new_owner = 0; /* Owned by EHCI */
401 }
402 if (portnum <= 0 || portnum > HCS_N_PORTS(ehci->hcs_params))
403 return -ENOENT;
404 portnum--;
405 if (new_owner)
406 set_bit(portnum, &ehci->companion_ports);
407 else
408 clear_bit(portnum, &ehci->companion_ports);
409 set_owner(ehci, portnum, new_owner);
57e06c11
AS
410 return count;
411}
5a3201b2 412static DEVICE_ATTR(companion, 0644, show_companion, store_companion);
57e06c11
AS
413
414static inline void create_companion_file(struct ehci_hcd *ehci)
415{
416 int i;
417
418 /* with integrated TT there is no companion! */
419 if (!ehci_is_TDI(ehci))
ed14f034 420 i = device_create_file(ehci_to_hcd(ehci)->self.controller,
5a3201b2 421 &dev_attr_companion);
57e06c11
AS
422}
423
424static inline void remove_companion_file(struct ehci_hcd *ehci)
425{
426 /* with integrated TT there is no companion! */
427 if (!ehci_is_TDI(ehci))
ed14f034 428 device_remove_file(ehci_to_hcd(ehci)->self.controller,
5a3201b2 429 &dev_attr_companion);
57e06c11
AS
430}
431
432
1da177e4
LT
433/*-------------------------------------------------------------------------*/
434
435static int check_reset_complete (
436 struct ehci_hcd *ehci,
437 int index,
e6316565 438 u32 __iomem *status_reg,
1da177e4
LT
439 int port_status
440) {
cd4cdc93 441 if (!(port_status & PORT_CONNECT))
1da177e4 442 return port_status;
1da177e4
LT
443
444 /* if reset finished and it's still not enabled -- handoff */
445 if (!(port_status & PORT_PE)) {
446
447 /* with integrated TT, there's nobody to hand it to! */
448 if (ehci_is_TDI(ehci)) {
449 ehci_dbg (ehci,
450 "Failed to enable port %d on root hub TT\n",
451 index+1);
452 return port_status;
453 }
454
455 ehci_dbg (ehci, "port %d full speed --> companion\n",
456 index + 1);
457
458 // what happens if HCS_N_CC(params) == 0 ?
459 port_status |= PORT_OWNER;
10f6524a 460 port_status &= ~PORT_RWC_BITS;
e6316565 461 ehci_writel(ehci, port_status, status_reg);
1da177e4 462
796bcae7
VB
463 /* ensure 440EPX ohci controller state is operational */
464 if (ehci->has_amcc_usb23)
465 set_ohci_hcfs(ehci, 1);
466 } else {
1da177e4 467 ehci_dbg (ehci, "port %d high speed\n", index + 1);
796bcae7
VB
468 /* ensure 440EPx ohci controller state is suspended */
469 if (ehci->has_amcc_usb23)
470 set_ohci_hcfs(ehci, 0);
471 }
1da177e4
LT
472
473 return port_status;
474}
475
476/*-------------------------------------------------------------------------*/
477
478
479/* build "status change" packet (one or two bytes) from HC registers */
480
481static int
482ehci_hub_status_data (struct usb_hcd *hcd, char *buf)
483{
484 struct ehci_hcd *ehci = hcd_to_ehci (hcd);
485 u32 temp, status = 0;
93f1a47c 486 u32 mask;
1da177e4
LT
487 int ports, i, retval = 1;
488 unsigned long flags;
489
490 /* if !USB_SUSPEND, root hub timers won't get shut down ... */
491 if (!HC_IS_RUNNING(hcd->state))
492 return 0;
493
494 /* init status to no-changes */
495 buf [0] = 0;
496 ports = HCS_N_PORTS (ehci->hcs_params);
497 if (ports > 7) {
498 buf [1] = 0;
499 retval++;
500 }
53bd6a60 501
93f1a47c
DB
502 /* Some boards (mostly VIA?) report bogus overcurrent indications,
503 * causing massive log spam unless we completely ignore them. It
3a4fa0a2 504 * may be relevant that VIA VT8235 controllers, where PORT_POWER is
93f1a47c
DB
505 * always set, seem to clear PORT_OCC and PORT_CSC when writing to
506 * PORT_POWER; that's surprising, but maybe within-spec.
507 */
508 if (!ignore_oc)
509 mask = PORT_CSC | PORT_PEC | PORT_OCC;
510 else
511 mask = PORT_CSC | PORT_PEC;
512 // PORT_RESUME from hardware ~= PORT_STAT_C_SUSPEND
513
1da177e4
LT
514 /* no hub change reports (bit 0) for now (power, ...) */
515
516 /* port N changes (bit N)? */
517 spin_lock_irqsave (&ehci->lock, flags);
518 for (i = 0; i < ports; i++) {
083522d7 519 temp = ehci_readl(ehci, &ehci->regs->port_status [i]);
625b5c9a
AS
520
521 /*
522 * Return status information even for ports with OWNER set.
523 * Otherwise khubd wouldn't see the disconnect event when a
524 * high-speed device is switched over to the companion
525 * controller by the user.
526 */
527
eafe5b99
AS
528 if ((temp & mask) != 0 || test_bit(i, &ehci->port_c_suspend)
529 || (ehci->reset_done[i] && time_after_eq(
530 jiffies, ehci->reset_done[i]))) {
1da177e4
LT
531 if (i < 7)
532 buf [0] |= 1 << (i + 1);
533 else
534 buf [1] |= 1 << (i - 7);
535 status = STS_PCD;
536 }
537 }
538 /* FIXME autosuspend idle root hubs */
539 spin_unlock_irqrestore (&ehci->lock, flags);
540 return status ? retval : 0;
541}
542
543/*-------------------------------------------------------------------------*/
544
545static void
546ehci_hub_descriptor (
547 struct ehci_hcd *ehci,
548 struct usb_hub_descriptor *desc
549) {
550 int ports = HCS_N_PORTS (ehci->hcs_params);
551 u16 temp;
552
553 desc->bDescriptorType = 0x29;
554 desc->bPwrOn2PwrGood = 10; /* ehci 1.0, 2.3.9 says 20ms max */
555 desc->bHubContrCurrent = 0;
556
557 desc->bNbrPorts = ports;
558 temp = 1 + (ports / 8);
559 desc->bDescLength = 7 + 2 * temp;
560
561 /* two bitmaps: ports removable, and usb 1.0 legacy PortPwrCtrlMask */
562 memset (&desc->bitmap [0], 0, temp);
563 memset (&desc->bitmap [temp], 0xff, temp);
564
565 temp = 0x0008; /* per-port overcurrent reporting */
566 if (HCS_PPC (ehci->hcs_params))
567 temp |= 0x0001; /* per-port power control */
56c1e26d
DB
568 else
569 temp |= 0x0002; /* no power switching */
1da177e4
LT
570#if 0
571// re-enable when we support USB_PORT_FEAT_INDICATOR below.
572 if (HCS_INDICATOR (ehci->hcs_params))
573 temp |= 0x0080; /* per-port indicators (LEDs) */
574#endif
fd05e720 575 desc->wHubCharacteristics = cpu_to_le16(temp);
1da177e4
LT
576}
577
578/*-------------------------------------------------------------------------*/
579
1da177e4
LT
580static int ehci_hub_control (
581 struct usb_hcd *hcd,
582 u16 typeReq,
583 u16 wValue,
584 u16 wIndex,
585 char *buf,
586 u16 wLength
587) {
588 struct ehci_hcd *ehci = hcd_to_ehci (hcd);
589 int ports = HCS_N_PORTS (ehci->hcs_params);
383975d7
AS
590 u32 __iomem *status_reg = &ehci->regs->port_status[
591 (wIndex & 0xff) - 1];
331ac6b2
AD
592 u32 __iomem *hostpc_reg = NULL;
593 u32 temp, temp1, status;
1da177e4
LT
594 unsigned long flags;
595 int retval = 0;
f0d7f273 596 unsigned selector;
1da177e4
LT
597
598 /*
599 * FIXME: support SetPortFeatures USB_PORT_FEAT_INDICATOR.
600 * HCS_INDICATOR may say we can change LEDs to off/amber/green.
601 * (track current state ourselves) ... blink for diagnostics,
602 * power, "this is the one", etc. EHCI spec supports this.
603 */
604
331ac6b2
AD
605 if (ehci->has_hostpc)
606 hostpc_reg = (u32 __iomem *)((u8 *)ehci->regs
607 + HOSTPC0 + 4 * ((wIndex & 0xff) - 1));
1da177e4
LT
608 spin_lock_irqsave (&ehci->lock, flags);
609 switch (typeReq) {
610 case ClearHubFeature:
611 switch (wValue) {
612 case C_HUB_LOCAL_POWER:
613 case C_HUB_OVER_CURRENT:
614 /* no hub-wide feature/status flags */
615 break;
616 default:
617 goto error;
618 }
619 break;
620 case ClearPortFeature:
621 if (!wIndex || wIndex > ports)
622 goto error;
623 wIndex--;
e6316565 624 temp = ehci_readl(ehci, status_reg);
625b5c9a
AS
625
626 /*
627 * Even if OWNER is set, so the port is owned by the
628 * companion controller, khubd needs to be able to clear
629 * the port-change status bits (especially
630 * USB_PORT_FEAT_C_CONNECTION).
631 */
1da177e4
LT
632
633 switch (wValue) {
634 case USB_PORT_FEAT_ENABLE:
e6316565 635 ehci_writel(ehci, temp & ~PORT_PE, status_reg);
1da177e4
LT
636 break;
637 case USB_PORT_FEAT_C_ENABLE:
083522d7 638 ehci_writel(ehci, (temp & ~PORT_RWC_BITS) | PORT_PEC,
e6316565 639 status_reg);
1da177e4
LT
640 break;
641 case USB_PORT_FEAT_SUSPEND:
642 if (temp & PORT_RESET)
643 goto error;
f8aeb3bb
DB
644 if (ehci->no_selective_suspend)
645 break;
1da177e4
LT
646 if (temp & PORT_SUSPEND) {
647 if ((temp & PORT_PE) == 0)
648 goto error;
649 /* resume signaling for 20 msec */
10f6524a 650 temp &= ~(PORT_RWC_BITS | PORT_WAKE_BITS);
083522d7 651 ehci_writel(ehci, temp | PORT_RESUME,
e6316565 652 status_reg);
1da177e4
LT
653 ehci->reset_done [wIndex] = jiffies
654 + msecs_to_jiffies (20);
655 }
656 break;
657 case USB_PORT_FEAT_C_SUSPEND:
d1f114d1 658 clear_bit(wIndex, &ehci->port_c_suspend);
1da177e4
LT
659 break;
660 case USB_PORT_FEAT_POWER:
661 if (HCS_PPC (ehci->hcs_params))
083522d7
BH
662 ehci_writel(ehci,
663 temp & ~(PORT_RWC_BITS | PORT_POWER),
e6316565 664 status_reg);
1da177e4
LT
665 break;
666 case USB_PORT_FEAT_C_CONNECTION:
083522d7 667 ehci_writel(ehci, (temp & ~PORT_RWC_BITS) | PORT_CSC,
e6316565 668 status_reg);
1da177e4
LT
669 break;
670 case USB_PORT_FEAT_C_OVER_CURRENT:
083522d7 671 ehci_writel(ehci, (temp & ~PORT_RWC_BITS) | PORT_OCC,
e6316565 672 status_reg);
1da177e4
LT
673 break;
674 case USB_PORT_FEAT_C_RESET:
675 /* GetPortStatus clears reset */
676 break;
677 default:
678 goto error;
679 }
083522d7 680 ehci_readl(ehci, &ehci->regs->command); /* unblock posted write */
1da177e4
LT
681 break;
682 case GetHubDescriptor:
683 ehci_hub_descriptor (ehci, (struct usb_hub_descriptor *)
684 buf);
685 break;
686 case GetHubStatus:
687 /* no hub-wide feature/status flags */
688 memset (buf, 0, 4);
689 //cpu_to_le32s ((u32 *) buf);
690 break;
691 case GetPortStatus:
692 if (!wIndex || wIndex > ports)
693 goto error;
694 wIndex--;
695 status = 0;
e6316565 696 temp = ehci_readl(ehci, status_reg);
1da177e4
LT
697
698 // wPortChange bits
699 if (temp & PORT_CSC)
700 status |= 1 << USB_PORT_FEAT_C_CONNECTION;
701 if (temp & PORT_PEC)
702 status |= 1 << USB_PORT_FEAT_C_ENABLE;
756aa6b3
CE
703
704 if ((temp & PORT_OCC) && !ignore_oc){
1da177e4
LT
705 status |= 1 << USB_PORT_FEAT_C_OVER_CURRENT;
706
756aa6b3
CE
707 /*
708 * Hubs should disable port power on over-current.
709 * However, not all EHCI implementations do this
710 * automatically, even if they _do_ support per-port
711 * power switching; they're allowed to just limit the
712 * current. khubd will turn the power back on.
713 */
714 if (HCS_PPC (ehci->hcs_params)){
715 ehci_writel(ehci,
716 temp & ~(PORT_RWC_BITS | PORT_POWER),
717 status_reg);
718 }
719 }
720
1da177e4 721 /* whoever resumes must GetPortStatus to complete it!! */
629e4427
AS
722 if (temp & PORT_RESUME) {
723
724 /* Remote Wakeup received? */
725 if (!ehci->reset_done[wIndex]) {
726 /* resume signaling for 20 msec */
727 ehci->reset_done[wIndex] = jiffies
728 + msecs_to_jiffies(20);
729 /* check the port again */
730 mod_timer(&ehci_to_hcd(ehci)->rh_timer,
731 ehci->reset_done[wIndex]);
732 }
1da177e4 733
629e4427
AS
734 /* resume completed? */
735 else if (time_after_eq(jiffies,
736 ehci->reset_done[wIndex])) {
eafe5b99 737 clear_bit(wIndex, &ehci->suspended_ports);
d1f114d1 738 set_bit(wIndex, &ehci->port_c_suspend);
629e4427
AS
739 ehci->reset_done[wIndex] = 0;
740
741 /* stop resume signaling */
742 temp = ehci_readl(ehci, status_reg);
743 ehci_writel(ehci,
e6316565
AS
744 temp & ~(PORT_RWC_BITS | PORT_RESUME),
745 status_reg);
629e4427 746 retval = handshake(ehci, status_reg,
083522d7 747 PORT_RESUME, 0, 2000 /* 2msec */);
629e4427
AS
748 if (retval != 0) {
749 ehci_err(ehci,
750 "port %d resume error %d\n",
751 wIndex + 1, retval);
752 goto error;
753 }
754 temp &= ~(PORT_SUSPEND|PORT_RESUME|(3<<10));
1da177e4 755 }
1da177e4
LT
756 }
757
758 /* whoever resets must GetPortStatus to complete it!! */
759 if ((temp & PORT_RESET)
629e4427
AS
760 && time_after_eq(jiffies,
761 ehci->reset_done[wIndex])) {
1da177e4
LT
762 status |= 1 << USB_PORT_FEAT_C_RESET;
763 ehci->reset_done [wIndex] = 0;
764
765 /* force reset to complete */
083522d7 766 ehci_writel(ehci, temp & ~(PORT_RWC_BITS | PORT_RESET),
e6316565 767 status_reg);
c22fa3ac
DB
768 /* REVISIT: some hardware needs 550+ usec to clear
769 * this bit; seems too long to spin routinely...
770 */
e6316565 771 retval = handshake(ehci, status_reg,
c22fa3ac 772 PORT_RESET, 0, 750);
1da177e4
LT
773 if (retval != 0) {
774 ehci_err (ehci, "port %d reset error %d\n",
775 wIndex + 1, retval);
776 goto error;
777 }
778
779 /* see what we found out */
e6316565
AS
780 temp = check_reset_complete (ehci, wIndex, status_reg,
781 ehci_readl(ehci, status_reg));
1da177e4
LT
782 }
783
eafe5b99
AS
784 if (!(temp & (PORT_RESUME|PORT_RESET)))
785 ehci->reset_done[wIndex] = 0;
786
57e06c11
AS
787 /* transfer dedicated ports to the companion hc */
788 if ((temp & PORT_CONNECT) &&
789 test_bit(wIndex, &ehci->companion_ports)) {
790 temp &= ~PORT_RWC_BITS;
791 temp |= PORT_OWNER;
792 ehci_writel(ehci, temp, status_reg);
793 ehci_dbg(ehci, "port %d --> companion\n", wIndex + 1);
794 temp = ehci_readl(ehci, status_reg);
795 }
796
625b5c9a
AS
797 /*
798 * Even if OWNER is set, there's no harm letting khubd
799 * see the wPortStatus values (they should all be 0 except
800 * for PORT_POWER anyway).
801 */
802
803 if (temp & PORT_CONNECT) {
804 status |= 1 << USB_PORT_FEAT_CONNECTION;
805 // status may be from integrated TT
331ac6b2
AD
806 if (ehci->has_hostpc) {
807 temp1 = ehci_readl(ehci, hostpc_reg);
808 status |= ehci_port_speed(ehci, temp1);
809 } else
810 status |= ehci_port_speed(ehci, temp);
1da177e4 811 }
625b5c9a
AS
812 if (temp & PORT_PE)
813 status |= 1 << USB_PORT_FEAT_ENABLE;
eafe5b99
AS
814
815 /* maybe the port was unsuspended without our knowledge */
816 if (temp & (PORT_SUSPEND|PORT_RESUME)) {
625b5c9a 817 status |= 1 << USB_PORT_FEAT_SUSPEND;
eafe5b99
AS
818 } else if (test_bit(wIndex, &ehci->suspended_ports)) {
819 clear_bit(wIndex, &ehci->suspended_ports);
820 ehci->reset_done[wIndex] = 0;
821 if (temp & PORT_PE)
822 set_bit(wIndex, &ehci->port_c_suspend);
823 }
824
625b5c9a
AS
825 if (temp & PORT_OC)
826 status |= 1 << USB_PORT_FEAT_OVER_CURRENT;
827 if (temp & PORT_RESET)
828 status |= 1 << USB_PORT_FEAT_RESET;
829 if (temp & PORT_POWER)
830 status |= 1 << USB_PORT_FEAT_POWER;
d1f114d1
AS
831 if (test_bit(wIndex, &ehci->port_c_suspend))
832 status |= 1 << USB_PORT_FEAT_C_SUSPEND;
1da177e4 833
9776afc8 834#ifndef VERBOSE_DEBUG
1da177e4
LT
835 if (status & ~0xffff) /* only if wPortChange is interesting */
836#endif
837 dbg_port (ehci, "GetStatus", wIndex + 1, temp);
a5abdeaf 838 put_unaligned_le32(status, buf);
1da177e4
LT
839 break;
840 case SetHubFeature:
841 switch (wValue) {
842 case C_HUB_LOCAL_POWER:
843 case C_HUB_OVER_CURRENT:
844 /* no hub-wide feature/status flags */
845 break;
846 default:
847 goto error;
848 }
849 break;
850 case SetPortFeature:
f0d7f273
DB
851 selector = wIndex >> 8;
852 wIndex &= 0xff;
1da177e4
LT
853 if (!wIndex || wIndex > ports)
854 goto error;
855 wIndex--;
e6316565 856 temp = ehci_readl(ehci, status_reg);
1da177e4
LT
857 if (temp & PORT_OWNER)
858 break;
859
10f6524a 860 temp &= ~PORT_RWC_BITS;
1da177e4
LT
861 switch (wValue) {
862 case USB_PORT_FEAT_SUSPEND:
f8aeb3bb
DB
863 if (ehci->no_selective_suspend)
864 break;
1da177e4
LT
865 if ((temp & PORT_PE) == 0
866 || (temp & PORT_RESET) != 0)
867 goto error;
e6316565 868 ehci_writel(ehci, temp | PORT_SUSPEND, status_reg);
331ac6b2
AD
869 /* After above check the port must be connected.
870 * Set appropriate bit thus could put phy into low power
871 * mode if we have hostpc feature
872 */
873 if (hostpc_reg) {
874 temp &= ~PORT_WKCONN_E;
875 temp |= (PORT_WKDISC_E | PORT_WKOC_E);
876 ehci_writel(ehci, temp | PORT_SUSPEND,
877 status_reg);
878 msleep(5);/* 5ms for HCD enter low pwr mode */
879 temp1 = ehci_readl(ehci, hostpc_reg);
880 ehci_writel(ehci, temp1 | HOSTPC_PHCD,
881 hostpc_reg);
882 temp1 = ehci_readl(ehci, hostpc_reg);
883 ehci_dbg(ehci, "Port%d phy low pwr mode %s\n",
884 wIndex, (temp1 & HOSTPC_PHCD) ?
885 "succeeded" : "failed");
886 }
eafe5b99 887 set_bit(wIndex, &ehci->suspended_ports);
1da177e4
LT
888 break;
889 case USB_PORT_FEAT_POWER:
890 if (HCS_PPC (ehci->hcs_params))
083522d7 891 ehci_writel(ehci, temp | PORT_POWER,
e6316565 892 status_reg);
1da177e4
LT
893 break;
894 case USB_PORT_FEAT_RESET:
895 if (temp & PORT_RESUME)
896 goto error;
897 /* line status bits may report this as low speed,
898 * which can be fine if this root hub has a
899 * transaction translator built in.
900 */
901 if ((temp & (PORT_PE|PORT_CONNECT)) == PORT_CONNECT
902 && !ehci_is_TDI(ehci)
903 && PORT_USB11 (temp)) {
904 ehci_dbg (ehci,
905 "port %d low speed --> companion\n",
906 wIndex + 1);
907 temp |= PORT_OWNER;
908 } else {
909 ehci_vdbg (ehci, "port %d reset\n", wIndex + 1);
910 temp |= PORT_RESET;
911 temp &= ~PORT_PE;
912
913 /*
914 * caller must wait, then call GetPortStatus
915 * usb 2.0 spec says 50 ms resets on root
916 */
917 ehci->reset_done [wIndex] = jiffies
918 + msecs_to_jiffies (50);
919 }
e6316565 920 ehci_writel(ehci, temp, status_reg);
1da177e4 921 break;
f0d7f273
DB
922
923 /* For downstream facing ports (these): one hub port is put
924 * into test mode according to USB2 11.24.2.13, then the hub
925 * must be reset (which for root hub now means rmmod+modprobe,
926 * or else system reboot). See EHCI 2.3.9 and 4.14 for info
927 * about the EHCI-specific stuff.
928 */
929 case USB_PORT_FEAT_TEST:
930 if (!selector || selector > 5)
931 goto error;
932 ehci_quiesce(ehci);
933 ehci_halt(ehci);
934 temp |= selector << 16;
e6316565 935 ehci_writel(ehci, temp, status_reg);
f0d7f273
DB
936 break;
937
1da177e4
LT
938 default:
939 goto error;
940 }
083522d7 941 ehci_readl(ehci, &ehci->regs->command); /* unblock posted writes */
1da177e4
LT
942 break;
943
944 default:
945error:
946 /* "stall" on error */
947 retval = -EPIPE;
948 }
949 spin_unlock_irqrestore (&ehci->lock, flags);
950 return retval;
951}
90da096e
BR
952
953static void ehci_relinquish_port(struct usb_hcd *hcd, int portnum)
954{
955 struct ehci_hcd *ehci = hcd_to_ehci(hcd);
956
957 if (ehci_is_TDI(ehci))
958 return;
959 set_owner(ehci, --portnum, PORT_OWNER);
960}
961
3a31155c
AS
962static int ehci_port_handed_over(struct usb_hcd *hcd, int portnum)
963{
964 struct ehci_hcd *ehci = hcd_to_ehci(hcd);
965 u32 __iomem *reg;
966
967 if (ehci_is_TDI(ehci))
968 return 0;
969 reg = &ehci->regs->port_status[portnum - 1];
970 return ehci_readl(ehci, reg) & PORT_OWNER;
971}