RT-AC66 3.0.0.4.374.130 core
[tomato.git] / release / src-rt-6.x / linux / linux-2.6 / drivers / usb / host / ehci-hub.c
blobd9b33a241af24ad8ed34b500dbddfb5a8e1d66fc
1 /*
2 * Copyright (C) 2001-2004 by David Brownell
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.
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.
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.
19 /* this file is part of ehci-hcd.c */
21 /*-------------------------------------------------------------------------*/
24 * EHCI Root Hub ... the nonsharable stuff
26 * Registers don't need cpu_to_le32, that happens transparently
29 /*-------------------------------------------------------------------------*/
31 #define PORT_WAKE_BITS (PORT_WKOC_E|PORT_WKDISC_E|PORT_WKCONN_E)
33 #ifdef CONFIG_PM
35 static int ehci_hub_control(
36 struct usb_hcd *hcd,
37 u16 typeReq,
38 u16 wValue,
39 u16 wIndex,
40 char *buf,
41 u16 wLength
44 /* After a power loss, ports that were owned by the companion must be
45 * reset so that the companion can still own them.
47 static void ehci_handover_companion_ports(struct ehci_hcd *ehci)
49 u32 __iomem *reg;
50 u32 status;
51 int port;
52 __le32 buf;
53 struct usb_hcd *hcd = ehci_to_hcd(ehci);
55 if (!ehci->owned_ports)
56 return;
58 /* Give the connections some time to appear */
59 msleep(20);
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];
65 status = ehci_readl(ehci, reg) & ~PORT_RWC_BITS;
67 /* Port already owned by companion? */
68 if (status & PORT_OWNER)
69 clear_bit(port, &ehci->owned_ports);
70 else if (test_bit(port, &ehci->companion_ports))
71 ehci_writel(ehci, status & ~PORT_PE, reg);
72 else
73 ehci_hub_control(hcd, SetPortFeature,
74 USB_PORT_FEAT_RESET, port + 1,
75 NULL, 0);
79 if (!ehci->owned_ports)
80 return;
81 msleep(90); /* Wait for resets to complete */
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));
90 /* The companion should now own the port,
91 * but if something went wrong the port must not
92 * remain enabled.
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);
106 ehci->owned_ports = 0;
109 static int ehci_bus_suspend (struct usb_hcd *hcd)
111 struct ehci_hcd *ehci = hcd_to_ehci (hcd);
112 int port;
113 int mask;
115 ehci_dbg(ehci, "suspend root hub\n");
117 if (time_before (jiffies, ehci->next_statechange))
118 msleep(5);
119 del_timer_sync(&ehci->watchdog);
120 del_timer_sync(&ehci->iaa_watchdog);
122 spin_lock_irq (&ehci->lock);
124 /* Once the controller is stopped, port resumes that are already
125 * in progress won't complete. Hence if remote wakeup is enabled
126 * for the root hub and any ports are in the middle of a resume or
127 * remote wakeup, we must fail the suspend.
129 if (hcd->self.root_hub->do_remote_wakeup) {
130 port = HCS_N_PORTS(ehci->hcs_params);
131 while (port--) {
132 if (ehci->reset_done[port] != 0) {
133 spin_unlock_irq(&ehci->lock);
134 ehci_dbg(ehci, "suspend failed because "
135 "port %d is resuming\n",
136 port + 1);
137 return -EBUSY;
142 /* stop schedules, clean any completed work */
143 if (HC_IS_RUNNING(hcd->state)) {
144 ehci_quiesce (ehci);
145 hcd->state = HC_STATE_QUIESCING;
147 ehci->command = ehci_readl(ehci, &ehci->regs->command);
148 ehci_work(ehci);
150 /* Unlike other USB host controller types, EHCI doesn't have
151 * any notion of "global" or bus-wide suspend. The driver has
152 * to manually suspend all the active unsuspended ports, and
153 * then manually resume them in the bus_resume() routine.
155 ehci->bus_suspended = 0;
156 ehci->owned_ports = 0;
157 port = HCS_N_PORTS(ehci->hcs_params);
158 while (port--) {
159 u32 __iomem *reg = &ehci->regs->port_status [port];
160 u32 t1 = ehci_readl(ehci, reg) & ~PORT_RWC_BITS;
161 u32 t2 = t1;
163 /* keep track of which ports we suspend */
164 if (t1 & PORT_OWNER)
165 set_bit(port, &ehci->owned_ports);
166 else if ((t1 & PORT_PE) && !(t1 & PORT_SUSPEND)) {
167 t2 |= PORT_SUSPEND;
168 set_bit(port, &ehci->bus_suspended);
171 /* enable remote wakeup on all ports */
172 if (hcd->self.root_hub->do_remote_wakeup)
173 t2 |= PORT_WAKE_BITS;
174 else
175 t2 &= ~PORT_WAKE_BITS;
177 if (t1 != t2) {
178 ehci_vdbg (ehci, "port %d, %08x -> %08x\n",
179 port + 1, t1, t2);
180 ehci_writel(ehci, t2, reg);
184 /* Apparently some devices need a >= 1-uframe delay here */
185 if (ehci->bus_suspended)
186 udelay(150);
188 /* turn off now-idle HC */
189 ehci_halt (ehci);
190 hcd->state = HC_STATE_SUSPENDED;
192 if (ehci->reclaim)
193 end_unlink_async(ehci);
195 /* allow remote wakeup */
196 mask = INTR_MASK;
197 if (!hcd->self.root_hub->do_remote_wakeup)
198 mask &= ~STS_PCD;
199 ehci_writel(ehci, mask, &ehci->regs->intr_enable);
200 ehci_readl(ehci, &ehci->regs->intr_enable);
202 ehci->next_statechange = jiffies + msecs_to_jiffies(10);
203 spin_unlock_irq (&ehci->lock);
205 /* ehci_work() may have re-enabled the watchdog timer, which we do not
206 * want, and so we must delete any pending watchdog timer events.
208 del_timer_sync(&ehci->watchdog);
209 return 0;
213 /* caller has locked the root hub, and should reset/reinit on error */
214 static int ehci_bus_resume (struct usb_hcd *hcd)
216 struct ehci_hcd *ehci = hcd_to_ehci (hcd);
217 u32 temp;
218 u32 power_okay;
219 int i;
220 u8 resume_needed = 0;
222 if (time_before (jiffies, ehci->next_statechange))
223 msleep(5);
224 spin_lock_irq (&ehci->lock);
225 if (!test_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags)) {
226 spin_unlock_irq(&ehci->lock);
227 return -ESHUTDOWN;
230 /* Ideally and we've got a real resume here, and no port's power
231 * was lost. (For PCI, that means Vaux was maintained.) But we
232 * could instead be restoring a swsusp snapshot -- so that BIOS was
233 * the last user of the controller, not reset/pm hardware keeping
234 * state we gave to it.
236 power_okay = ehci_readl(ehci, &ehci->regs->intr_enable);
237 ehci_dbg(ehci, "resume root hub%s\n",
238 power_okay ? "" : " after power loss");
240 /* at least some APM implementations will try to deliver
241 * IRQs right away, so delay them until we're ready.
243 ehci_writel(ehci, 0, &ehci->regs->intr_enable);
245 /* re-init operational registers */
246 ehci_writel(ehci, 0, &ehci->regs->segment);
247 ehci_writel(ehci, ehci->periodic_dma, &ehci->regs->frame_list);
248 ehci_writel(ehci, (u32) ehci->async->qh_dma, &ehci->regs->async_next);
250 /* restore CMD_RUN, framelist size, and irq threshold */
251 ehci_writel(ehci, ehci->command, &ehci->regs->command);
253 /* Some controller/firmware combinations need a delay during which
254 * they set up the port statuses. See Bugzilla #8190. */
255 spin_unlock_irq(&ehci->lock);
256 msleep(8);
257 spin_lock_irq(&ehci->lock);
259 /* manually resume the ports we suspended during bus_suspend() */
260 i = HCS_N_PORTS (ehci->hcs_params);
261 while (i--) {
262 temp = ehci_readl(ehci, &ehci->regs->port_status [i]);
263 temp &= ~(PORT_RWC_BITS | PORT_WAKE_BITS);
264 if (test_bit(i, &ehci->bus_suspended) &&
265 (temp & PORT_SUSPEND)) {
266 temp |= PORT_RESUME;
267 resume_needed = 1;
269 ehci_writel(ehci, temp, &ehci->regs->port_status [i]);
272 /* msleep for 20ms only if code is trying to resume port */
273 if (resume_needed) {
274 spin_unlock_irq(&ehci->lock);
275 msleep(20);
276 spin_lock_irq(&ehci->lock);
279 i = HCS_N_PORTS (ehci->hcs_params);
280 while (i--) {
281 temp = ehci_readl(ehci, &ehci->regs->port_status [i]);
282 if (test_bit(i, &ehci->bus_suspended) &&
283 (temp & PORT_SUSPEND)) {
284 temp &= ~(PORT_RWC_BITS | PORT_RESUME);
285 ehci_writel(ehci, temp, &ehci->regs->port_status [i]);
286 ehci_vdbg (ehci, "resumed port %d\n", i + 1);
289 (void) ehci_readl(ehci, &ehci->regs->command);
291 /* maybe re-activate the schedule(s) */
292 temp = 0;
293 if (ehci->async->qh_next.qh)
294 temp |= CMD_ASE;
295 if (ehci->periodic_sched)
296 temp |= CMD_PSE;
297 if (temp) {
298 ehci->command |= temp;
299 ehci_writel(ehci, ehci->command, &ehci->regs->command);
302 ehci->next_statechange = jiffies + msecs_to_jiffies(5);
303 hcd->state = HC_STATE_RUNNING;
305 /* Now we can safely re-enable irqs */
306 ehci_writel(ehci, INTR_MASK, &ehci->regs->intr_enable);
308 spin_unlock_irq (&ehci->lock);
309 ehci_handover_companion_ports(ehci);
310 return 0;
313 #else
315 #define ehci_bus_suspend NULL
316 #define ehci_bus_resume NULL
318 #endif /* CONFIG_PM */
320 /*-------------------------------------------------------------------------*/
322 /* Display the ports dedicated to the companion controller */
323 static ssize_t show_companion(struct device *dev,
324 struct device_attribute *attr,
325 char *buf)
327 struct ehci_hcd *ehci;
328 int nports, index, n;
329 int count = PAGE_SIZE;
330 char *ptr = buf;
332 ehci = hcd_to_ehci(bus_to_hcd(dev_get_drvdata(dev)));
333 nports = HCS_N_PORTS(ehci->hcs_params);
335 for (index = 0; index < nports; ++index) {
336 if (test_bit(index, &ehci->companion_ports)) {
337 n = scnprintf(ptr, count, "%d\n", index + 1);
338 ptr += n;
339 count -= n;
342 return ptr - buf;
346 * Sets the owner of a port
348 static void set_owner(struct ehci_hcd *ehci, int portnum, int new_owner)
350 u32 __iomem *status_reg;
351 u32 port_status;
352 int try;
354 status_reg = &ehci->regs->port_status[portnum];
357 * The controller won't set the OWNER bit if the port is
358 * enabled, so this loop will sometimes require at least two
359 * iterations: one to disable the port and one to set OWNER.
361 for (try = 4; try > 0; --try) {
362 spin_lock_irq(&ehci->lock);
363 port_status = ehci_readl(ehci, status_reg);
364 if ((port_status & PORT_OWNER) == new_owner
365 || (port_status & (PORT_OWNER | PORT_CONNECT))
366 == 0)
367 try = 0;
368 else {
369 port_status ^= PORT_OWNER;
370 port_status &= ~(PORT_PE | PORT_RWC_BITS);
371 ehci_writel(ehci, port_status, status_reg);
373 spin_unlock_irq(&ehci->lock);
374 if (try > 1)
375 msleep(5);
380 * Dedicate or undedicate a port to the companion controller.
381 * Syntax is "[-]portnum", where a leading '-' sign means
382 * return control of the port to the EHCI controller.
384 static ssize_t store_companion(struct device *dev,
385 struct device_attribute *attr,
386 const char *buf, size_t count)
388 struct ehci_hcd *ehci;
389 int portnum, new_owner;
391 ehci = hcd_to_ehci(bus_to_hcd(dev_get_drvdata(dev)));
392 new_owner = PORT_OWNER; /* Owned by companion */
393 if (sscanf(buf, "%d", &portnum) != 1)
394 return -EINVAL;
395 if (portnum < 0) {
396 portnum = - portnum;
397 new_owner = 0; /* Owned by EHCI */
399 if (portnum <= 0 || portnum > HCS_N_PORTS(ehci->hcs_params))
400 return -ENOENT;
401 portnum--;
402 if (new_owner)
403 set_bit(portnum, &ehci->companion_ports);
404 else
405 clear_bit(portnum, &ehci->companion_ports);
406 set_owner(ehci, portnum, new_owner);
407 return count;
409 static DEVICE_ATTR(companion, 0644, show_companion, store_companion);
411 static inline void create_companion_file(struct ehci_hcd *ehci)
413 int i;
415 /* with integrated TT there is no companion! */
416 if (!ehci_is_TDI(ehci))
417 i = device_create_file(ehci_to_hcd(ehci)->self.controller,
418 &dev_attr_companion);
421 static inline void remove_companion_file(struct ehci_hcd *ehci)
423 /* with integrated TT there is no companion! */
424 if (!ehci_is_TDI(ehci))
425 device_remove_file(ehci_to_hcd(ehci)->self.controller,
426 &dev_attr_companion);
430 /*-------------------------------------------------------------------------*/
432 static int check_reset_complete (
433 struct ehci_hcd *ehci,
434 int index,
435 u32 __iomem *status_reg,
436 int port_status
438 if (!(port_status & PORT_CONNECT))
439 return port_status;
441 /* if reset finished and it's still not enabled -- handoff */
442 if (!(port_status & PORT_PE)) {
444 /* with integrated TT, there's nobody to hand it to! */
445 if (ehci_is_TDI(ehci)) {
446 ehci_dbg (ehci,
447 "Failed to enable port %d on root hub TT\n",
448 index+1);
449 return port_status;
452 ehci_dbg (ehci, "port %d full speed --> companion\n",
453 index + 1);
455 // what happens if HCS_N_CC(params) == 0 ?
456 port_status |= PORT_OWNER;
457 port_status &= ~PORT_RWC_BITS;
458 ehci_writel(ehci, port_status, status_reg);
460 } else
461 ehci_dbg (ehci, "port %d high speed\n", index + 1);
463 return port_status;
466 /*-------------------------------------------------------------------------*/
469 /* build "status change" packet (one or two bytes) from HC registers */
471 static int
472 ehci_hub_status_data (struct usb_hcd *hcd, char *buf)
474 struct ehci_hcd *ehci = hcd_to_ehci (hcd);
475 u32 temp, status = 0;
476 u32 mask;
477 int ports, i, retval = 1;
478 unsigned long flags;
480 /* if !USB_SUSPEND, root hub timers won't get shut down ... */
481 if (!HC_IS_RUNNING(hcd->state))
482 return 0;
484 /* init status to no-changes */
485 buf [0] = 0;
486 ports = HCS_N_PORTS (ehci->hcs_params);
487 if (ports > 7) {
488 buf [1] = 0;
489 retval++;
492 /* Some boards (mostly VIA?) report bogus overcurrent indications,
493 * causing massive log spam unless we completely ignore them. It
494 * may be relevant that VIA VT8235 controlers, where PORT_POWER is
495 * always set, seem to clear PORT_OCC and PORT_CSC when writing to
496 * PORT_POWER; that's surprising, but maybe within-spec.
498 if (!ignore_oc)
499 mask = PORT_CSC | PORT_PEC | PORT_OCC;
500 else
501 mask = PORT_CSC | PORT_PEC;
502 // PORT_RESUME from hardware ~= PORT_STAT_C_SUSPEND
504 /* no hub change reports (bit 0) for now (power, ...) */
506 /* port N changes (bit N)? */
507 spin_lock_irqsave (&ehci->lock, flags);
508 for (i = 0; i < ports; i++) {
509 temp = ehci_readl(ehci, &ehci->regs->port_status [i]);
512 * Return status information even for ports with OWNER set.
513 * Otherwise khubd wouldn't see the disconnect event when a
514 * high-speed device is switched over to the companion
515 * controller by the user.
518 if ((temp & mask) != 0 || test_bit(i, &ehci->port_c_suspend)
519 || (ehci->reset_done[i] && time_after_eq(
520 jiffies, ehci->reset_done[i]))) {
521 if (i < 7)
522 buf [0] |= 1 << (i + 1);
523 else
524 buf [1] |= 1 << (i - 7);
525 status = STS_PCD;
528 /* FIXME autosuspend idle root hubs */
529 spin_unlock_irqrestore (&ehci->lock, flags);
530 return status ? retval : 0;
533 /*-------------------------------------------------------------------------*/
535 static void
536 ehci_hub_descriptor (
537 struct ehci_hcd *ehci,
538 struct usb_hub_descriptor *desc
540 int ports = HCS_N_PORTS (ehci->hcs_params);
541 u16 temp;
543 desc->bDescriptorType = 0x29;
544 desc->bPwrOn2PwrGood = 10; /* ehci 1.0, 2.3.9 says 20ms max */
545 desc->bHubContrCurrent = 0;
547 desc->bNbrPorts = ports;
548 temp = 1 + (ports / 8);
549 desc->bDescLength = 7 + 2 * temp;
551 /* two bitmaps: ports removable, and usb 1.0 legacy PortPwrCtrlMask */
552 memset (&desc->bitmap [0], 0, temp);
553 memset (&desc->bitmap [temp], 0xff, temp);
555 temp = 0x0008; /* per-port overcurrent reporting */
556 if (HCS_PPC (ehci->hcs_params))
557 temp |= 0x0001; /* per-port power control */
558 else
559 temp |= 0x0002; /* no power switching */
560 #if 0
561 // re-enable when we support USB_PORT_FEAT_INDICATOR below.
562 if (HCS_INDICATOR (ehci->hcs_params))
563 temp |= 0x0080; /* per-port indicators (LEDs) */
564 #endif
565 desc->wHubCharacteristics = cpu_to_le16(temp);
568 /*-------------------------------------------------------------------------*/
570 static int ehci_hub_control (
571 struct usb_hcd *hcd,
572 u16 typeReq,
573 u16 wValue,
574 u16 wIndex,
575 char *buf,
576 u16 wLength
578 struct ehci_hcd *ehci = hcd_to_ehci (hcd);
579 int ports = HCS_N_PORTS (ehci->hcs_params);
580 u32 __iomem *status_reg = &ehci->regs->port_status[
581 (wIndex & 0xff) - 1];
582 u32 temp, status;
583 unsigned long flags;
584 int retval = 0;
585 unsigned selector;
588 * FIXME: support SetPortFeatures USB_PORT_FEAT_INDICATOR.
589 * HCS_INDICATOR may say we can change LEDs to off/amber/green.
590 * (track current state ourselves) ... blink for diagnostics,
591 * power, "this is the one", etc. EHCI spec supports this.
594 spin_lock_irqsave (&ehci->lock, flags);
595 switch (typeReq) {
596 case ClearHubFeature:
597 switch (wValue) {
598 case C_HUB_LOCAL_POWER:
599 case C_HUB_OVER_CURRENT:
600 /* no hub-wide feature/status flags */
601 break;
602 default:
603 goto error;
605 break;
606 case ClearPortFeature:
607 if (!wIndex || wIndex > ports)
608 goto error;
609 wIndex--;
610 temp = ehci_readl(ehci, status_reg);
613 * Even if OWNER is set, so the port is owned by the
614 * companion controller, khubd needs to be able to clear
615 * the port-change status bits (especially
616 * USB_PORT_FEAT_C_CONNECTION).
619 switch (wValue) {
620 case USB_PORT_FEAT_ENABLE:
621 ehci_writel(ehci, temp & ~PORT_PE, status_reg);
622 break;
623 case USB_PORT_FEAT_C_ENABLE:
624 ehci_writel(ehci, (temp & ~PORT_RWC_BITS) | PORT_PEC,
625 status_reg);
626 break;
627 case USB_PORT_FEAT_SUSPEND:
628 if (temp & PORT_RESET)
629 goto error;
630 if (ehci->no_selective_suspend)
631 break;
632 if (temp & PORT_SUSPEND) {
633 if ((temp & PORT_PE) == 0)
634 goto error;
635 /* resume signaling for 20 msec */
636 temp &= ~(PORT_RWC_BITS | PORT_WAKE_BITS);
637 ehci_writel(ehci, temp | PORT_RESUME,
638 status_reg);
639 ehci->reset_done [wIndex] = jiffies
640 + msecs_to_jiffies (20);
642 break;
643 case USB_PORT_FEAT_C_SUSPEND:
644 clear_bit(wIndex, &ehci->port_c_suspend);
645 break;
646 case USB_PORT_FEAT_POWER:
647 if (HCS_PPC (ehci->hcs_params))
648 ehci_writel(ehci,
649 temp & ~(PORT_RWC_BITS | PORT_POWER),
650 status_reg);
651 break;
652 case USB_PORT_FEAT_C_CONNECTION:
653 ehci_writel(ehci, (temp & ~PORT_RWC_BITS) | PORT_CSC,
654 status_reg);
655 break;
656 case USB_PORT_FEAT_C_OVER_CURRENT:
657 ehci_writel(ehci, (temp & ~PORT_RWC_BITS) | PORT_OCC,
658 status_reg);
659 break;
660 case USB_PORT_FEAT_C_RESET:
661 /* GetPortStatus clears reset */
662 break;
663 default:
664 goto error;
666 ehci_readl(ehci, &ehci->regs->command); /* unblock posted write */
667 break;
668 case GetHubDescriptor:
669 ehci_hub_descriptor (ehci, (struct usb_hub_descriptor *)
670 buf);
671 break;
672 case GetHubStatus:
673 /* no hub-wide feature/status flags */
674 memset (buf, 0, 4);
675 //cpu_to_le32s ((u32 *) buf);
676 break;
677 case GetPortStatus:
678 if (!wIndex || wIndex > ports)
679 goto error;
680 wIndex--;
681 status = 0;
682 temp = ehci_readl(ehci, status_reg);
684 // wPortChange bits
685 if (temp & PORT_CSC)
686 status |= 1 << USB_PORT_FEAT_C_CONNECTION;
687 if (temp & PORT_PEC)
688 status |= 1 << USB_PORT_FEAT_C_ENABLE;
690 if ((temp & PORT_OCC) && !ignore_oc){
691 status |= 1 << USB_PORT_FEAT_C_OVER_CURRENT;
694 * Hubs should disable port power on over-current.
695 * However, not all EHCI implementations do this
696 * automatically, even if they _do_ support per-port
697 * power switching; they're allowed to just limit the
698 * current. khubd will turn the power back on.
700 if (HCS_PPC (ehci->hcs_params)){
701 ehci_writel(ehci,
702 temp & ~(PORT_RWC_BITS | PORT_POWER),
703 status_reg);
707 /* whoever resumes must GetPortStatus to complete it!! */
708 if (temp & PORT_RESUME) {
710 /* Remote Wakeup received? */
711 if (!ehci->reset_done[wIndex]) {
712 /* resume signaling for 20 msec */
713 ehci->reset_done[wIndex] = jiffies
714 + msecs_to_jiffies(20);
715 /* check the port again */
716 mod_timer(&ehci_to_hcd(ehci)->rh_timer,
717 ehci->reset_done[wIndex]);
720 /* resume completed? */
721 else if (time_after_eq(jiffies,
722 ehci->reset_done[wIndex])) {
723 clear_bit(wIndex, &ehci->suspended_ports);
724 set_bit(wIndex, &ehci->port_c_suspend);
725 ehci->reset_done[wIndex] = 0;
727 /* stop resume signaling */
728 temp = ehci_readl(ehci, status_reg);
729 ehci_writel(ehci,
730 temp & ~(PORT_RWC_BITS | PORT_RESUME),
731 status_reg);
732 retval = handshake(ehci, status_reg,
733 PORT_RESUME, 0, 2000 /* 2msec */);
734 if (retval != 0) {
735 ehci_err(ehci,
736 "port %d resume error %d\n",
737 wIndex + 1, retval);
738 goto error;
740 temp &= ~(PORT_SUSPEND|PORT_RESUME|(3<<10));
744 /* whoever resets must GetPortStatus to complete it!! */
745 if ((temp & PORT_RESET)
746 && time_after_eq(jiffies,
747 ehci->reset_done[wIndex])) {
748 status |= 1 << USB_PORT_FEAT_C_RESET;
749 ehci->reset_done [wIndex] = 0;
751 /* force reset to complete */
752 ehci_writel(ehci, temp & ~(PORT_RWC_BITS | PORT_RESET),
753 status_reg);
754 /* REVISIT: some hardware needs 550+ usec to clear
755 * this bit; seems too long to spin routinely...
757 retval = handshake(ehci, status_reg,
758 PORT_RESET, 0, 1000);
759 if (retval != 0) {
760 ehci_err (ehci, "port %d reset error %d\n",
761 wIndex + 1, retval);
762 goto error;
765 /* see what we found out */
766 temp = check_reset_complete (ehci, wIndex, status_reg,
767 ehci_readl(ehci, status_reg));
770 if (!(temp & (PORT_RESUME|PORT_RESET)))
771 ehci->reset_done[wIndex] = 0;
773 /* transfer dedicated ports to the companion hc */
774 if ((temp & PORT_CONNECT) &&
775 test_bit(wIndex, &ehci->companion_ports)) {
776 temp &= ~PORT_RWC_BITS;
777 temp |= PORT_OWNER;
778 ehci_writel(ehci, temp, status_reg);
779 ehci_dbg(ehci, "port %d --> companion\n", wIndex + 1);
780 temp = ehci_readl(ehci, status_reg);
784 * Even if OWNER is set, there's no harm letting khubd
785 * see the wPortStatus values (they should all be 0 except
786 * for PORT_POWER anyway).
789 if (temp & PORT_CONNECT) {
790 status |= 1 << USB_PORT_FEAT_CONNECTION;
791 // status may be from integrated TT
792 status |= ehci_port_speed(ehci, temp);
794 if (temp & PORT_PE)
795 status |= 1 << USB_PORT_FEAT_ENABLE;
797 /* maybe the port was unsuspended without our knowledge */
798 if (temp & (PORT_SUSPEND|PORT_RESUME)) {
799 status |= 1 << USB_PORT_FEAT_SUSPEND;
800 } else if (test_bit(wIndex, &ehci->suspended_ports)) {
801 clear_bit(wIndex, &ehci->suspended_ports);
802 ehci->reset_done[wIndex] = 0;
803 if (temp & PORT_PE)
804 set_bit(wIndex, &ehci->port_c_suspend);
807 if (temp & PORT_OC)
808 status |= 1 << USB_PORT_FEAT_OVER_CURRENT;
809 if (temp & PORT_RESET)
810 status |= 1 << USB_PORT_FEAT_RESET;
811 if (temp & PORT_POWER)
812 status |= 1 << USB_PORT_FEAT_POWER;
813 if (test_bit(wIndex, &ehci->port_c_suspend))
814 status |= 1 << USB_PORT_FEAT_C_SUSPEND;
816 #ifndef VERBOSE_DEBUG
817 if (status & ~0xffff) /* only if wPortChange is interesting */
818 #endif
819 dbg_port (ehci, "GetStatus", wIndex + 1, temp);
820 put_unaligned(cpu_to_le32 (status), (__le32 *) buf);
821 break;
822 case SetHubFeature:
823 switch (wValue) {
824 case C_HUB_LOCAL_POWER:
825 case C_HUB_OVER_CURRENT:
826 /* no hub-wide feature/status flags */
827 break;
828 default:
829 goto error;
831 break;
832 case SetPortFeature:
833 selector = wIndex >> 8;
834 wIndex &= 0xff;
835 if (!wIndex || wIndex > ports)
836 goto error;
837 wIndex--;
838 temp = ehci_readl(ehci, status_reg);
839 if (temp & PORT_OWNER)
840 break;
842 temp &= ~PORT_RWC_BITS;
843 switch (wValue) {
844 case USB_PORT_FEAT_SUSPEND:
845 if (ehci->no_selective_suspend)
846 break;
847 if ((temp & PORT_PE) == 0
848 || (temp & PORT_RESET) != 0)
849 goto error;
850 ehci_writel(ehci, temp | PORT_SUSPEND, status_reg);
851 set_bit(wIndex, &ehci->suspended_ports);
852 break;
853 case USB_PORT_FEAT_POWER:
854 if (HCS_PPC (ehci->hcs_params))
855 ehci_writel(ehci, temp | PORT_POWER,
856 status_reg);
857 break;
858 case USB_PORT_FEAT_RESET:
859 if (temp & PORT_RESUME)
860 goto error;
861 /* line status bits may report this as low speed,
862 * which can be fine if this root hub has a
863 * transaction translator built in.
865 if ((temp & (PORT_PE|PORT_CONNECT)) == PORT_CONNECT
866 && !ehci_is_TDI(ehci)
867 && PORT_USB11 (temp)) {
868 ehci_dbg (ehci,
869 "port %d low speed --> companion\n",
870 wIndex + 1);
871 temp |= PORT_OWNER;
872 } else {
873 ehci_vdbg (ehci, "port %d reset\n", wIndex + 1);
874 temp |= PORT_RESET;
875 temp &= ~PORT_PE;
878 * caller must wait, then call GetPortStatus
879 * usb 2.0 spec says 50 ms resets on root
881 ehci->reset_done [wIndex] = jiffies
882 + msecs_to_jiffies (50);
884 ehci_writel(ehci, temp, status_reg);
885 break;
887 /* For downstream facing ports (these): one hub port is put
888 * into test mode according to USB2 11.24.2.13, then the hub
889 * must be reset (which for root hub now means rmmod+modprobe,
890 * or else system reboot). See EHCI 2.3.9 and 4.14 for info
891 * about the EHCI-specific stuff.
893 case USB_PORT_FEAT_TEST:
894 if (!selector || selector > 5)
895 goto error;
896 ehci_quiesce(ehci);
897 ehci_halt(ehci);
898 temp |= selector << 16;
899 ehci_writel(ehci, temp, status_reg);
900 break;
902 default:
903 goto error;
905 ehci_readl(ehci, &ehci->regs->command); /* unblock posted writes */
906 break;
908 default:
909 error:
910 /* "stall" on error */
911 retval = -EPIPE;
913 spin_unlock_irqrestore (&ehci->lock, flags);
914 return retval;
917 static void ehci_relinquish_port(struct usb_hcd *hcd, int portnum)
919 struct ehci_hcd *ehci = hcd_to_ehci(hcd);
921 if (ehci_is_TDI(ehci))
922 return;
923 set_owner(ehci, --portnum, PORT_OWNER);
926 static int ehci_port_handed_over(struct usb_hcd *hcd, int portnum)
928 struct ehci_hcd *ehci = hcd_to_ehci(hcd);
929 u32 __iomem *reg;
931 if (ehci_is_TDI(ehci))
932 return 0;
933 reg = &ehci->regs->port_status[portnum - 1];
934 return ehci_readl(ehci, reg) & PORT_OWNER;