USB: ehci: minor cleanups
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / usb / host / ehci-hub.c
blob21ac3781f21abe7bc0d628593e6174c122d87c36
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 #ifdef CONFIG_PM
33 static int ehci_hub_control(
34 struct usb_hcd *hcd,
35 u16 typeReq,
36 u16 wValue,
37 u16 wIndex,
38 char *buf,
39 u16 wLength
42 /* After a power loss, ports that were owned by the companion must be
43 * reset so that the companion can still own them.
45 static void ehci_handover_companion_ports(struct ehci_hcd *ehci)
47 u32 __iomem *reg;
48 u32 status;
49 int port;
50 __le32 buf;
51 struct usb_hcd *hcd = ehci_to_hcd(ehci);
53 if (!ehci->owned_ports)
54 return;
56 /* Give the connections some time to appear */
57 msleep(20);
59 port = HCS_N_PORTS(ehci->hcs_params);
60 while (port--) {
61 if (test_bit(port, &ehci->owned_ports)) {
62 reg = &ehci->regs->port_status[port];
63 status = ehci_readl(ehci, reg) & ~PORT_RWC_BITS;
65 /* Port already owned by companion? */
66 if (status & PORT_OWNER)
67 clear_bit(port, &ehci->owned_ports);
68 else if (test_bit(port, &ehci->companion_ports))
69 ehci_writel(ehci, status & ~PORT_PE, reg);
70 else
71 ehci_hub_control(hcd, SetPortFeature,
72 USB_PORT_FEAT_RESET, port + 1,
73 NULL, 0);
77 if (!ehci->owned_ports)
78 return;
79 msleep(90); /* Wait for resets to complete */
81 port = HCS_N_PORTS(ehci->hcs_params);
82 while (port--) {
83 if (test_bit(port, &ehci->owned_ports)) {
84 ehci_hub_control(hcd, GetPortStatus,
85 0, port + 1,
86 (char *) &buf, sizeof(buf));
88 /* The companion should now own the port,
89 * but if something went wrong the port must not
90 * remain enabled.
92 reg = &ehci->regs->port_status[port];
93 status = ehci_readl(ehci, reg) & ~PORT_RWC_BITS;
94 if (status & PORT_OWNER)
95 ehci_writel(ehci, status | PORT_CSC, reg);
96 else {
97 ehci_dbg(ehci, "failed handover port %d: %x\n",
98 port + 1, status);
99 ehci_writel(ehci, status & ~PORT_PE, reg);
104 ehci->owned_ports = 0;
107 static int ehci_bus_suspend (struct usb_hcd *hcd)
109 struct ehci_hcd *ehci = hcd_to_ehci (hcd);
110 int port;
111 int mask;
113 ehci_dbg(ehci, "suspend root hub\n");
115 if (time_before (jiffies, ehci->next_statechange))
116 msleep(5);
117 del_timer_sync(&ehci->watchdog);
118 del_timer_sync(&ehci->iaa_watchdog);
120 port = HCS_N_PORTS (ehci->hcs_params);
121 spin_lock_irq (&ehci->lock);
123 /* stop schedules, clean any completed work */
124 if (HC_IS_RUNNING(hcd->state)) {
125 ehci_quiesce (ehci);
126 hcd->state = HC_STATE_QUIESCING;
128 ehci->command = ehci_readl(ehci, &ehci->regs->command);
129 ehci_work(ehci);
131 /* Unlike other USB host controller types, EHCI doesn't have
132 * any notion of "global" or bus-wide suspend. The driver has
133 * to manually suspend all the active unsuspended ports, and
134 * then manually resume them in the bus_resume() routine.
136 ehci->bus_suspended = 0;
137 ehci->owned_ports = 0;
138 while (port--) {
139 u32 __iomem *reg = &ehci->regs->port_status [port];
140 u32 t1 = ehci_readl(ehci, reg) & ~PORT_RWC_BITS;
141 u32 t2 = t1;
143 /* keep track of which ports we suspend */
144 if (t1 & PORT_OWNER)
145 set_bit(port, &ehci->owned_ports);
146 else if ((t1 & PORT_PE) && !(t1 & PORT_SUSPEND)) {
147 t2 |= PORT_SUSPEND;
148 set_bit(port, &ehci->bus_suspended);
151 /* enable remote wakeup on all ports */
152 if (device_may_wakeup(&hcd->self.root_hub->dev))
153 t2 |= PORT_WKOC_E|PORT_WKDISC_E|PORT_WKCONN_E;
154 else
155 t2 &= ~(PORT_WKOC_E|PORT_WKDISC_E|PORT_WKCONN_E);
157 if (t1 != t2) {
158 ehci_vdbg (ehci, "port %d, %08x -> %08x\n",
159 port + 1, t1, t2);
160 ehci_writel(ehci, t2, reg);
164 /* Apparently some devices need a >= 1-uframe delay here */
165 if (ehci->bus_suspended)
166 udelay(150);
168 /* turn off now-idle HC */
169 ehci_halt (ehci);
170 hcd->state = HC_STATE_SUSPENDED;
172 if (ehci->reclaim)
173 end_unlink_async(ehci);
175 /* allow remote wakeup */
176 mask = INTR_MASK;
177 if (!device_may_wakeup(&hcd->self.root_hub->dev))
178 mask &= ~STS_PCD;
179 ehci_writel(ehci, mask, &ehci->regs->intr_enable);
180 ehci_readl(ehci, &ehci->regs->intr_enable);
182 ehci->next_statechange = jiffies + msecs_to_jiffies(10);
183 spin_unlock_irq (&ehci->lock);
184 return 0;
188 /* caller has locked the root hub, and should reset/reinit on error */
189 static int ehci_bus_resume (struct usb_hcd *hcd)
191 struct ehci_hcd *ehci = hcd_to_ehci (hcd);
192 u32 temp;
193 u32 power_okay;
194 int i;
196 if (time_before (jiffies, ehci->next_statechange))
197 msleep(5);
198 spin_lock_irq (&ehci->lock);
199 if (!test_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags)) {
200 spin_unlock_irq(&ehci->lock);
201 return -ESHUTDOWN;
204 /* Ideally and we've got a real resume here, and no port's power
205 * was lost. (For PCI, that means Vaux was maintained.) But we
206 * could instead be restoring a swsusp snapshot -- so that BIOS was
207 * the last user of the controller, not reset/pm hardware keeping
208 * state we gave to it.
210 power_okay = ehci_readl(ehci, &ehci->regs->intr_enable);
211 ehci_dbg(ehci, "resume root hub%s\n",
212 power_okay ? "" : " after power loss");
214 /* at least some APM implementations will try to deliver
215 * IRQs right away, so delay them until we're ready.
217 ehci_writel(ehci, 0, &ehci->regs->intr_enable);
219 /* re-init operational registers */
220 ehci_writel(ehci, 0, &ehci->regs->segment);
221 ehci_writel(ehci, ehci->periodic_dma, &ehci->regs->frame_list);
222 ehci_writel(ehci, (u32) ehci->async->qh_dma, &ehci->regs->async_next);
224 /* restore CMD_RUN, framelist size, and irq threshold */
225 ehci_writel(ehci, ehci->command, &ehci->regs->command);
227 /* Some controller/firmware combinations need a delay during which
228 * they set up the port statuses. See Bugzilla #8190. */
229 mdelay(8);
231 /* manually resume the ports we suspended during bus_suspend() */
232 i = HCS_N_PORTS (ehci->hcs_params);
233 while (i--) {
234 temp = ehci_readl(ehci, &ehci->regs->port_status [i]);
235 temp &= ~(PORT_RWC_BITS
236 | PORT_WKOC_E | PORT_WKDISC_E | PORT_WKCONN_E);
237 if (test_bit(i, &ehci->bus_suspended) &&
238 (temp & PORT_SUSPEND)) {
239 ehci->reset_done [i] = jiffies + msecs_to_jiffies (20);
240 temp |= PORT_RESUME;
242 ehci_writel(ehci, temp, &ehci->regs->port_status [i]);
244 i = HCS_N_PORTS (ehci->hcs_params);
245 mdelay (20);
246 while (i--) {
247 temp = ehci_readl(ehci, &ehci->regs->port_status [i]);
248 if (test_bit(i, &ehci->bus_suspended) &&
249 (temp & PORT_SUSPEND)) {
250 temp &= ~(PORT_RWC_BITS | PORT_RESUME);
251 ehci_writel(ehci, temp, &ehci->regs->port_status [i]);
252 ehci_vdbg (ehci, "resumed port %d\n", i + 1);
255 (void) ehci_readl(ehci, &ehci->regs->command);
257 /* maybe re-activate the schedule(s) */
258 temp = 0;
259 if (ehci->async->qh_next.qh)
260 temp |= CMD_ASE;
261 if (ehci->periodic_sched)
262 temp |= CMD_PSE;
263 if (temp) {
264 ehci->command |= temp;
265 ehci_writel(ehci, ehci->command, &ehci->regs->command);
268 ehci->next_statechange = jiffies + msecs_to_jiffies(5);
269 hcd->state = HC_STATE_RUNNING;
271 /* Now we can safely re-enable irqs */
272 ehci_writel(ehci, INTR_MASK, &ehci->regs->intr_enable);
274 spin_unlock_irq (&ehci->lock);
275 ehci_handover_companion_ports(ehci);
276 return 0;
279 #else
281 #define ehci_bus_suspend NULL
282 #define ehci_bus_resume NULL
284 #endif /* CONFIG_PM */
286 /*-------------------------------------------------------------------------*/
288 /* Display the ports dedicated to the companion controller */
289 static ssize_t show_companion(struct device *dev,
290 struct device_attribute *attr,
291 char *buf)
293 struct ehci_hcd *ehci;
294 int nports, index, n;
295 int count = PAGE_SIZE;
296 char *ptr = buf;
298 ehci = hcd_to_ehci(bus_to_hcd(dev_get_drvdata(dev)));
299 nports = HCS_N_PORTS(ehci->hcs_params);
301 for (index = 0; index < nports; ++index) {
302 if (test_bit(index, &ehci->companion_ports)) {
303 n = scnprintf(ptr, count, "%d\n", index + 1);
304 ptr += n;
305 count -= n;
308 return ptr - buf;
312 * Sets the owner of a port
314 static void set_owner(struct ehci_hcd *ehci, int portnum, int new_owner)
316 u32 __iomem *status_reg;
317 u32 port_status;
318 int try;
320 status_reg = &ehci->regs->port_status[portnum];
323 * The controller won't set the OWNER bit if the port is
324 * enabled, so this loop will sometimes require at least two
325 * iterations: one to disable the port and one to set OWNER.
327 for (try = 4; try > 0; --try) {
328 spin_lock_irq(&ehci->lock);
329 port_status = ehci_readl(ehci, status_reg);
330 if ((port_status & PORT_OWNER) == new_owner
331 || (port_status & (PORT_OWNER | PORT_CONNECT))
332 == 0)
333 try = 0;
334 else {
335 port_status ^= PORT_OWNER;
336 port_status &= ~(PORT_PE | PORT_RWC_BITS);
337 ehci_writel(ehci, port_status, status_reg);
339 spin_unlock_irq(&ehci->lock);
340 if (try > 1)
341 msleep(5);
346 * Dedicate or undedicate a port to the companion controller.
347 * Syntax is "[-]portnum", where a leading '-' sign means
348 * return control of the port to the EHCI controller.
350 static ssize_t store_companion(struct device *dev,
351 struct device_attribute *attr,
352 const char *buf, size_t count)
354 struct ehci_hcd *ehci;
355 int portnum, new_owner;
357 ehci = hcd_to_ehci(bus_to_hcd(dev_get_drvdata(dev)));
358 new_owner = PORT_OWNER; /* Owned by companion */
359 if (sscanf(buf, "%d", &portnum) != 1)
360 return -EINVAL;
361 if (portnum < 0) {
362 portnum = - portnum;
363 new_owner = 0; /* Owned by EHCI */
365 if (portnum <= 0 || portnum > HCS_N_PORTS(ehci->hcs_params))
366 return -ENOENT;
367 portnum--;
368 if (new_owner)
369 set_bit(portnum, &ehci->companion_ports);
370 else
371 clear_bit(portnum, &ehci->companion_ports);
372 set_owner(ehci, portnum, new_owner);
373 return count;
375 static DEVICE_ATTR(companion, 0644, show_companion, store_companion);
377 static inline void create_companion_file(struct ehci_hcd *ehci)
379 int i;
381 /* with integrated TT there is no companion! */
382 if (!ehci_is_TDI(ehci))
383 i = device_create_file(ehci_to_hcd(ehci)->self.dev,
384 &dev_attr_companion);
387 static inline void remove_companion_file(struct ehci_hcd *ehci)
389 /* with integrated TT there is no companion! */
390 if (!ehci_is_TDI(ehci))
391 device_remove_file(ehci_to_hcd(ehci)->self.dev,
392 &dev_attr_companion);
396 /*-------------------------------------------------------------------------*/
398 static int check_reset_complete (
399 struct ehci_hcd *ehci,
400 int index,
401 u32 __iomem *status_reg,
402 int port_status
404 if (!(port_status & PORT_CONNECT))
405 return port_status;
407 /* if reset finished and it's still not enabled -- handoff */
408 if (!(port_status & PORT_PE)) {
410 /* with integrated TT, there's nobody to hand it to! */
411 if (ehci_is_TDI(ehci)) {
412 ehci_dbg (ehci,
413 "Failed to enable port %d on root hub TT\n",
414 index+1);
415 return port_status;
418 ehci_dbg (ehci, "port %d full speed --> companion\n",
419 index + 1);
421 // what happens if HCS_N_CC(params) == 0 ?
422 port_status |= PORT_OWNER;
423 port_status &= ~PORT_RWC_BITS;
424 ehci_writel(ehci, port_status, status_reg);
426 } else
427 ehci_dbg (ehci, "port %d high speed\n", index + 1);
429 return port_status;
432 /*-------------------------------------------------------------------------*/
435 /* build "status change" packet (one or two bytes) from HC registers */
437 static int
438 ehci_hub_status_data (struct usb_hcd *hcd, char *buf)
440 struct ehci_hcd *ehci = hcd_to_ehci (hcd);
441 u32 temp, status = 0;
442 u32 mask;
443 int ports, i, retval = 1;
444 unsigned long flags;
446 /* if !USB_SUSPEND, root hub timers won't get shut down ... */
447 if (!HC_IS_RUNNING(hcd->state))
448 return 0;
450 /* init status to no-changes */
451 buf [0] = 0;
452 ports = HCS_N_PORTS (ehci->hcs_params);
453 if (ports > 7) {
454 buf [1] = 0;
455 retval++;
458 /* Some boards (mostly VIA?) report bogus overcurrent indications,
459 * causing massive log spam unless we completely ignore them. It
460 * may be relevant that VIA VT8235 controllers, where PORT_POWER is
461 * always set, seem to clear PORT_OCC and PORT_CSC when writing to
462 * PORT_POWER; that's surprising, but maybe within-spec.
464 if (!ignore_oc)
465 mask = PORT_CSC | PORT_PEC | PORT_OCC;
466 else
467 mask = PORT_CSC | PORT_PEC;
468 // PORT_RESUME from hardware ~= PORT_STAT_C_SUSPEND
470 /* no hub change reports (bit 0) for now (power, ...) */
472 /* port N changes (bit N)? */
473 spin_lock_irqsave (&ehci->lock, flags);
474 for (i = 0; i < ports; i++) {
475 temp = ehci_readl(ehci, &ehci->regs->port_status [i]);
478 * Return status information even for ports with OWNER set.
479 * Otherwise khubd wouldn't see the disconnect event when a
480 * high-speed device is switched over to the companion
481 * controller by the user.
484 if ((temp & mask) != 0
485 || ((temp & PORT_RESUME) != 0
486 && time_after_eq(jiffies,
487 ehci->reset_done[i]))) {
488 if (i < 7)
489 buf [0] |= 1 << (i + 1);
490 else
491 buf [1] |= 1 << (i - 7);
492 status = STS_PCD;
495 /* FIXME autosuspend idle root hubs */
496 spin_unlock_irqrestore (&ehci->lock, flags);
497 return status ? retval : 0;
500 /*-------------------------------------------------------------------------*/
502 static void
503 ehci_hub_descriptor (
504 struct ehci_hcd *ehci,
505 struct usb_hub_descriptor *desc
507 int ports = HCS_N_PORTS (ehci->hcs_params);
508 u16 temp;
510 desc->bDescriptorType = 0x29;
511 desc->bPwrOn2PwrGood = 10; /* ehci 1.0, 2.3.9 says 20ms max */
512 desc->bHubContrCurrent = 0;
514 desc->bNbrPorts = ports;
515 temp = 1 + (ports / 8);
516 desc->bDescLength = 7 + 2 * temp;
518 /* two bitmaps: ports removable, and usb 1.0 legacy PortPwrCtrlMask */
519 memset (&desc->bitmap [0], 0, temp);
520 memset (&desc->bitmap [temp], 0xff, temp);
522 temp = 0x0008; /* per-port overcurrent reporting */
523 if (HCS_PPC (ehci->hcs_params))
524 temp |= 0x0001; /* per-port power control */
525 else
526 temp |= 0x0002; /* no power switching */
527 #if 0
528 // re-enable when we support USB_PORT_FEAT_INDICATOR below.
529 if (HCS_INDICATOR (ehci->hcs_params))
530 temp |= 0x0080; /* per-port indicators (LEDs) */
531 #endif
532 desc->wHubCharacteristics = (__force __u16)cpu_to_le16 (temp);
535 /*-------------------------------------------------------------------------*/
537 #define PORT_WAKE_BITS (PORT_WKOC_E|PORT_WKDISC_E|PORT_WKCONN_E)
539 static int ehci_hub_control (
540 struct usb_hcd *hcd,
541 u16 typeReq,
542 u16 wValue,
543 u16 wIndex,
544 char *buf,
545 u16 wLength
547 struct ehci_hcd *ehci = hcd_to_ehci (hcd);
548 int ports = HCS_N_PORTS (ehci->hcs_params);
549 u32 __iomem *status_reg = &ehci->regs->port_status[
550 (wIndex & 0xff) - 1];
551 u32 temp, status;
552 unsigned long flags;
553 int retval = 0;
554 unsigned selector;
557 * FIXME: support SetPortFeatures USB_PORT_FEAT_INDICATOR.
558 * HCS_INDICATOR may say we can change LEDs to off/amber/green.
559 * (track current state ourselves) ... blink for diagnostics,
560 * power, "this is the one", etc. EHCI spec supports this.
563 spin_lock_irqsave (&ehci->lock, flags);
564 switch (typeReq) {
565 case ClearHubFeature:
566 switch (wValue) {
567 case C_HUB_LOCAL_POWER:
568 case C_HUB_OVER_CURRENT:
569 /* no hub-wide feature/status flags */
570 break;
571 default:
572 goto error;
574 break;
575 case ClearPortFeature:
576 if (!wIndex || wIndex > ports)
577 goto error;
578 wIndex--;
579 temp = ehci_readl(ehci, status_reg);
582 * Even if OWNER is set, so the port is owned by the
583 * companion controller, khubd needs to be able to clear
584 * the port-change status bits (especially
585 * USB_PORT_FEAT_C_CONNECTION).
588 switch (wValue) {
589 case USB_PORT_FEAT_ENABLE:
590 ehci_writel(ehci, temp & ~PORT_PE, status_reg);
591 break;
592 case USB_PORT_FEAT_C_ENABLE:
593 ehci_writel(ehci, (temp & ~PORT_RWC_BITS) | PORT_PEC,
594 status_reg);
595 break;
596 case USB_PORT_FEAT_SUSPEND:
597 if (temp & PORT_RESET)
598 goto error;
599 if (ehci->no_selective_suspend)
600 break;
601 if (temp & PORT_SUSPEND) {
602 if ((temp & PORT_PE) == 0)
603 goto error;
604 /* resume signaling for 20 msec */
605 temp &= ~(PORT_RWC_BITS | PORT_WAKE_BITS);
606 ehci_writel(ehci, temp | PORT_RESUME,
607 status_reg);
608 ehci->reset_done [wIndex] = jiffies
609 + msecs_to_jiffies (20);
611 break;
612 case USB_PORT_FEAT_C_SUSPEND:
613 /* we auto-clear this feature */
614 break;
615 case USB_PORT_FEAT_POWER:
616 if (HCS_PPC (ehci->hcs_params))
617 ehci_writel(ehci,
618 temp & ~(PORT_RWC_BITS | PORT_POWER),
619 status_reg);
620 break;
621 case USB_PORT_FEAT_C_CONNECTION:
622 ehci_writel(ehci, (temp & ~PORT_RWC_BITS) | PORT_CSC,
623 status_reg);
624 break;
625 case USB_PORT_FEAT_C_OVER_CURRENT:
626 ehci_writel(ehci, (temp & ~PORT_RWC_BITS) | PORT_OCC,
627 status_reg);
628 break;
629 case USB_PORT_FEAT_C_RESET:
630 /* GetPortStatus clears reset */
631 break;
632 default:
633 goto error;
635 ehci_readl(ehci, &ehci->regs->command); /* unblock posted write */
636 break;
637 case GetHubDescriptor:
638 ehci_hub_descriptor (ehci, (struct usb_hub_descriptor *)
639 buf);
640 break;
641 case GetHubStatus:
642 /* no hub-wide feature/status flags */
643 memset (buf, 0, 4);
644 //cpu_to_le32s ((u32 *) buf);
645 break;
646 case GetPortStatus:
647 if (!wIndex || wIndex > ports)
648 goto error;
649 wIndex--;
650 status = 0;
651 temp = ehci_readl(ehci, status_reg);
653 // wPortChange bits
654 if (temp & PORT_CSC)
655 status |= 1 << USB_PORT_FEAT_C_CONNECTION;
656 if (temp & PORT_PEC)
657 status |= 1 << USB_PORT_FEAT_C_ENABLE;
659 if ((temp & PORT_OCC) && !ignore_oc){
660 status |= 1 << USB_PORT_FEAT_C_OVER_CURRENT;
663 * Hubs should disable port power on over-current.
664 * However, not all EHCI implementations do this
665 * automatically, even if they _do_ support per-port
666 * power switching; they're allowed to just limit the
667 * current. khubd will turn the power back on.
669 if (HCS_PPC (ehci->hcs_params)){
670 ehci_writel(ehci,
671 temp & ~(PORT_RWC_BITS | PORT_POWER),
672 status_reg);
676 /* whoever resumes must GetPortStatus to complete it!! */
677 if (temp & PORT_RESUME) {
679 /* Remote Wakeup received? */
680 if (!ehci->reset_done[wIndex]) {
681 /* resume signaling for 20 msec */
682 ehci->reset_done[wIndex] = jiffies
683 + msecs_to_jiffies(20);
684 /* check the port again */
685 mod_timer(&ehci_to_hcd(ehci)->rh_timer,
686 ehci->reset_done[wIndex]);
689 /* resume completed? */
690 else if (time_after_eq(jiffies,
691 ehci->reset_done[wIndex])) {
692 status |= 1 << USB_PORT_FEAT_C_SUSPEND;
693 ehci->reset_done[wIndex] = 0;
695 /* stop resume signaling */
696 temp = ehci_readl(ehci, status_reg);
697 ehci_writel(ehci,
698 temp & ~(PORT_RWC_BITS | PORT_RESUME),
699 status_reg);
700 retval = handshake(ehci, status_reg,
701 PORT_RESUME, 0, 2000 /* 2msec */);
702 if (retval != 0) {
703 ehci_err(ehci,
704 "port %d resume error %d\n",
705 wIndex + 1, retval);
706 goto error;
708 temp &= ~(PORT_SUSPEND|PORT_RESUME|(3<<10));
712 /* whoever resets must GetPortStatus to complete it!! */
713 if ((temp & PORT_RESET)
714 && time_after_eq(jiffies,
715 ehci->reset_done[wIndex])) {
716 status |= 1 << USB_PORT_FEAT_C_RESET;
717 ehci->reset_done [wIndex] = 0;
719 /* force reset to complete */
720 ehci_writel(ehci, temp & ~(PORT_RWC_BITS | PORT_RESET),
721 status_reg);
722 /* REVISIT: some hardware needs 550+ usec to clear
723 * this bit; seems too long to spin routinely...
725 retval = handshake(ehci, status_reg,
726 PORT_RESET, 0, 750);
727 if (retval != 0) {
728 ehci_err (ehci, "port %d reset error %d\n",
729 wIndex + 1, retval);
730 goto error;
733 /* see what we found out */
734 temp = check_reset_complete (ehci, wIndex, status_reg,
735 ehci_readl(ehci, status_reg));
738 /* transfer dedicated ports to the companion hc */
739 if ((temp & PORT_CONNECT) &&
740 test_bit(wIndex, &ehci->companion_ports)) {
741 temp &= ~PORT_RWC_BITS;
742 temp |= PORT_OWNER;
743 ehci_writel(ehci, temp, status_reg);
744 ehci_dbg(ehci, "port %d --> companion\n", wIndex + 1);
745 temp = ehci_readl(ehci, status_reg);
749 * Even if OWNER is set, there's no harm letting khubd
750 * see the wPortStatus values (they should all be 0 except
751 * for PORT_POWER anyway).
754 if (temp & PORT_CONNECT) {
755 status |= 1 << USB_PORT_FEAT_CONNECTION;
756 // status may be from integrated TT
757 status |= ehci_port_speed(ehci, temp);
759 if (temp & PORT_PE)
760 status |= 1 << USB_PORT_FEAT_ENABLE;
761 if (temp & (PORT_SUSPEND|PORT_RESUME))
762 status |= 1 << USB_PORT_FEAT_SUSPEND;
763 if (temp & PORT_OC)
764 status |= 1 << USB_PORT_FEAT_OVER_CURRENT;
765 if (temp & PORT_RESET)
766 status |= 1 << USB_PORT_FEAT_RESET;
767 if (temp & PORT_POWER)
768 status |= 1 << USB_PORT_FEAT_POWER;
770 #ifndef VERBOSE_DEBUG
771 if (status & ~0xffff) /* only if wPortChange is interesting */
772 #endif
773 dbg_port (ehci, "GetStatus", wIndex + 1, temp);
774 put_unaligned(cpu_to_le32 (status), (__le32 *) buf);
775 break;
776 case SetHubFeature:
777 switch (wValue) {
778 case C_HUB_LOCAL_POWER:
779 case C_HUB_OVER_CURRENT:
780 /* no hub-wide feature/status flags */
781 break;
782 default:
783 goto error;
785 break;
786 case SetPortFeature:
787 selector = wIndex >> 8;
788 wIndex &= 0xff;
789 if (!wIndex || wIndex > ports)
790 goto error;
791 wIndex--;
792 temp = ehci_readl(ehci, status_reg);
793 if (temp & PORT_OWNER)
794 break;
796 temp &= ~PORT_RWC_BITS;
797 switch (wValue) {
798 case USB_PORT_FEAT_SUSPEND:
799 if (ehci->no_selective_suspend)
800 break;
801 if ((temp & PORT_PE) == 0
802 || (temp & PORT_RESET) != 0)
803 goto error;
804 if (device_may_wakeup(&hcd->self.root_hub->dev))
805 temp |= PORT_WAKE_BITS;
806 ehci_writel(ehci, temp | PORT_SUSPEND, status_reg);
807 break;
808 case USB_PORT_FEAT_POWER:
809 if (HCS_PPC (ehci->hcs_params))
810 ehci_writel(ehci, temp | PORT_POWER,
811 status_reg);
812 break;
813 case USB_PORT_FEAT_RESET:
814 if (temp & PORT_RESUME)
815 goto error;
816 /* line status bits may report this as low speed,
817 * which can be fine if this root hub has a
818 * transaction translator built in.
820 if ((temp & (PORT_PE|PORT_CONNECT)) == PORT_CONNECT
821 && !ehci_is_TDI(ehci)
822 && PORT_USB11 (temp)) {
823 ehci_dbg (ehci,
824 "port %d low speed --> companion\n",
825 wIndex + 1);
826 temp |= PORT_OWNER;
827 } else {
828 ehci_vdbg (ehci, "port %d reset\n", wIndex + 1);
829 temp |= PORT_RESET;
830 temp &= ~PORT_PE;
833 * caller must wait, then call GetPortStatus
834 * usb 2.0 spec says 50 ms resets on root
836 ehci->reset_done [wIndex] = jiffies
837 + msecs_to_jiffies (50);
839 ehci_writel(ehci, temp, status_reg);
840 break;
842 /* For downstream facing ports (these): one hub port is put
843 * into test mode according to USB2 11.24.2.13, then the hub
844 * must be reset (which for root hub now means rmmod+modprobe,
845 * or else system reboot). See EHCI 2.3.9 and 4.14 for info
846 * about the EHCI-specific stuff.
848 case USB_PORT_FEAT_TEST:
849 if (!selector || selector > 5)
850 goto error;
851 ehci_quiesce(ehci);
852 ehci_halt(ehci);
853 temp |= selector << 16;
854 ehci_writel(ehci, temp, status_reg);
855 break;
857 default:
858 goto error;
860 ehci_readl(ehci, &ehci->regs->command); /* unblock posted writes */
861 break;
863 default:
864 error:
865 /* "stall" on error */
866 retval = -EPIPE;
868 spin_unlock_irqrestore (&ehci->lock, flags);
869 return retval;
872 static void ehci_relinquish_port(struct usb_hcd *hcd, int portnum)
874 struct ehci_hcd *ehci = hcd_to_ehci(hcd);
876 if (ehci_is_TDI(ehci))
877 return;
878 set_owner(ehci, --portnum, PORT_OWNER);