Merge tag 'gpio-v3.13-3' of git://git.kernel.org/pub/scm/linux/kernel/git/linusw...
[linux-2.6.git] / drivers / usb / host / ehci-hub.c
blob835fc0844a666ba545a966566f4c5fe302548370
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 /*-------------------------------------------------------------------------*/
30 #include <linux/usb/otg.h>
32 #define PORT_WAKE_BITS (PORT_WKOC_E|PORT_WKDISC_E|PORT_WKCONN_E)
34 #ifdef CONFIG_PM
36 static int ehci_hub_control(
37 struct usb_hcd *hcd,
38 u16 typeReq,
39 u16 wValue,
40 u16 wIndex,
41 char *buf,
42 u16 wLength
45 static int persist_enabled_on_companion(struct usb_device *udev, void *unused)
47 return !udev->maxchild && udev->persist_enabled &&
48 udev->bus->root_hub->speed < USB_SPEED_HIGH;
51 /* After a power loss, ports that were owned by the companion must be
52 * reset so that the companion can still own them.
54 static void ehci_handover_companion_ports(struct ehci_hcd *ehci)
56 u32 __iomem *reg;
57 u32 status;
58 int port;
59 __le32 buf;
60 struct usb_hcd *hcd = ehci_to_hcd(ehci);
62 if (!ehci->owned_ports)
63 return;
66 * USB 1.1 devices are mostly HIDs, which don't need to persist across
67 * suspends. If we ensure that none of our companion's devices have
68 * persist_enabled (by looking through all USB 1.1 buses in the system),
69 * we can skip this and avoid slowing resume down. Devices without
70 * persist will just get reenumerated shortly after resume anyway.
72 if (!usb_for_each_dev(NULL, persist_enabled_on_companion))
73 return;
75 /* Make sure the ports are powered */
76 port = HCS_N_PORTS(ehci->hcs_params);
77 while (port--) {
78 if (test_bit(port, &ehci->owned_ports)) {
79 reg = &ehci->regs->port_status[port];
80 status = ehci_readl(ehci, reg) & ~PORT_RWC_BITS;
81 if (!(status & PORT_POWER)) {
82 status |= PORT_POWER;
83 ehci_writel(ehci, status, reg);
88 /* Give the connections some time to appear */
89 msleep(20);
91 spin_lock_irq(&ehci->lock);
92 port = HCS_N_PORTS(ehci->hcs_params);
93 while (port--) {
94 if (test_bit(port, &ehci->owned_ports)) {
95 reg = &ehci->regs->port_status[port];
96 status = ehci_readl(ehci, reg) & ~PORT_RWC_BITS;
98 /* Port already owned by companion? */
99 if (status & PORT_OWNER)
100 clear_bit(port, &ehci->owned_ports);
101 else if (test_bit(port, &ehci->companion_ports))
102 ehci_writel(ehci, status & ~PORT_PE, reg);
103 else {
104 spin_unlock_irq(&ehci->lock);
105 ehci_hub_control(hcd, SetPortFeature,
106 USB_PORT_FEAT_RESET, port + 1,
107 NULL, 0);
108 spin_lock_irq(&ehci->lock);
112 spin_unlock_irq(&ehci->lock);
114 if (!ehci->owned_ports)
115 return;
116 msleep(90); /* Wait for resets to complete */
118 spin_lock_irq(&ehci->lock);
119 port = HCS_N_PORTS(ehci->hcs_params);
120 while (port--) {
121 if (test_bit(port, &ehci->owned_ports)) {
122 spin_unlock_irq(&ehci->lock);
123 ehci_hub_control(hcd, GetPortStatus,
124 0, port + 1,
125 (char *) &buf, sizeof(buf));
126 spin_lock_irq(&ehci->lock);
128 /* The companion should now own the port,
129 * but if something went wrong the port must not
130 * remain enabled.
132 reg = &ehci->regs->port_status[port];
133 status = ehci_readl(ehci, reg) & ~PORT_RWC_BITS;
134 if (status & PORT_OWNER)
135 ehci_writel(ehci, status | PORT_CSC, reg);
136 else {
137 ehci_dbg(ehci, "failed handover port %d: %x\n",
138 port + 1, status);
139 ehci_writel(ehci, status & ~PORT_PE, reg);
144 ehci->owned_ports = 0;
145 spin_unlock_irq(&ehci->lock);
148 static int ehci_port_change(struct ehci_hcd *ehci)
150 int i = HCS_N_PORTS(ehci->hcs_params);
152 /* First check if the controller indicates a change event */
154 if (ehci_readl(ehci, &ehci->regs->status) & STS_PCD)
155 return 1;
158 * Not all controllers appear to update this while going from D3 to D0,
159 * so check the individual port status registers as well
162 while (i--)
163 if (ehci_readl(ehci, &ehci->regs->port_status[i]) & PORT_CSC)
164 return 1;
166 return 0;
169 static void ehci_adjust_port_wakeup_flags(struct ehci_hcd *ehci,
170 bool suspending, bool do_wakeup)
172 int port;
173 u32 temp;
175 /* If remote wakeup is enabled for the root hub but disabled
176 * for the controller, we must adjust all the port wakeup flags
177 * when the controller is suspended or resumed. In all other
178 * cases they don't need to be changed.
180 if (!ehci_to_hcd(ehci)->self.root_hub->do_remote_wakeup || do_wakeup)
181 return;
183 spin_lock_irq(&ehci->lock);
185 /* clear phy low-power mode before changing wakeup flags */
186 if (ehci->has_tdi_phy_lpm) {
187 port = HCS_N_PORTS(ehci->hcs_params);
188 while (port--) {
189 u32 __iomem *hostpc_reg = &ehci->regs->hostpc[port];
191 temp = ehci_readl(ehci, hostpc_reg);
192 ehci_writel(ehci, temp & ~HOSTPC_PHCD, hostpc_reg);
194 spin_unlock_irq(&ehci->lock);
195 msleep(5);
196 spin_lock_irq(&ehci->lock);
199 port = HCS_N_PORTS(ehci->hcs_params);
200 while (port--) {
201 u32 __iomem *reg = &ehci->regs->port_status[port];
202 u32 t1 = ehci_readl(ehci, reg) & ~PORT_RWC_BITS;
203 u32 t2 = t1 & ~PORT_WAKE_BITS;
205 /* If we are suspending the controller, clear the flags.
206 * If we are resuming the controller, set the wakeup flags.
208 if (!suspending) {
209 if (t1 & PORT_CONNECT)
210 t2 |= PORT_WKOC_E | PORT_WKDISC_E;
211 else
212 t2 |= PORT_WKOC_E | PORT_WKCONN_E;
214 ehci_writel(ehci, t2, reg);
217 /* enter phy low-power mode again */
218 if (ehci->has_tdi_phy_lpm) {
219 port = HCS_N_PORTS(ehci->hcs_params);
220 while (port--) {
221 u32 __iomem *hostpc_reg = &ehci->regs->hostpc[port];
223 temp = ehci_readl(ehci, hostpc_reg);
224 ehci_writel(ehci, temp | HOSTPC_PHCD, hostpc_reg);
228 /* Does the root hub have a port wakeup pending? */
229 if (!suspending && ehci_port_change(ehci))
230 usb_hcd_resume_root_hub(ehci_to_hcd(ehci));
232 spin_unlock_irq(&ehci->lock);
235 static int ehci_bus_suspend (struct usb_hcd *hcd)
237 struct ehci_hcd *ehci = hcd_to_ehci (hcd);
238 int port;
239 int mask;
240 int changed;
242 ehci_dbg(ehci, "suspend root hub\n");
244 if (time_before (jiffies, ehci->next_statechange))
245 msleep(5);
247 /* stop the schedules */
248 ehci_quiesce(ehci);
250 spin_lock_irq (&ehci->lock);
251 if (ehci->rh_state < EHCI_RH_RUNNING)
252 goto done;
254 /* Once the controller is stopped, port resumes that are already
255 * in progress won't complete. Hence if remote wakeup is enabled
256 * for the root hub and any ports are in the middle of a resume or
257 * remote wakeup, we must fail the suspend.
259 if (hcd->self.root_hub->do_remote_wakeup) {
260 if (ehci->resuming_ports) {
261 spin_unlock_irq(&ehci->lock);
262 ehci_dbg(ehci, "suspend failed because a port is resuming\n");
263 return -EBUSY;
267 /* Unlike other USB host controller types, EHCI doesn't have
268 * any notion of "global" or bus-wide suspend. The driver has
269 * to manually suspend all the active unsuspended ports, and
270 * then manually resume them in the bus_resume() routine.
272 ehci->bus_suspended = 0;
273 ehci->owned_ports = 0;
274 changed = 0;
275 port = HCS_N_PORTS(ehci->hcs_params);
276 while (port--) {
277 u32 __iomem *reg = &ehci->regs->port_status [port];
278 u32 t1 = ehci_readl(ehci, reg) & ~PORT_RWC_BITS;
279 u32 t2 = t1 & ~PORT_WAKE_BITS;
281 /* keep track of which ports we suspend */
282 if (t1 & PORT_OWNER)
283 set_bit(port, &ehci->owned_ports);
284 else if ((t1 & PORT_PE) && !(t1 & PORT_SUSPEND)) {
285 t2 |= PORT_SUSPEND;
286 set_bit(port, &ehci->bus_suspended);
289 /* enable remote wakeup on all ports, if told to do so */
290 if (hcd->self.root_hub->do_remote_wakeup) {
291 /* only enable appropriate wake bits, otherwise the
292 * hardware can not go phy low power mode. If a race
293 * condition happens here(connection change during bits
294 * set), the port change detection will finally fix it.
296 if (t1 & PORT_CONNECT)
297 t2 |= PORT_WKOC_E | PORT_WKDISC_E;
298 else
299 t2 |= PORT_WKOC_E | PORT_WKCONN_E;
302 if (t1 != t2) {
303 ehci_writel(ehci, t2, reg);
304 changed = 1;
308 if (changed && ehci->has_tdi_phy_lpm) {
309 spin_unlock_irq(&ehci->lock);
310 msleep(5); /* 5 ms for HCD to enter low-power mode */
311 spin_lock_irq(&ehci->lock);
313 port = HCS_N_PORTS(ehci->hcs_params);
314 while (port--) {
315 u32 __iomem *hostpc_reg = &ehci->regs->hostpc[port];
316 u32 t3;
318 t3 = ehci_readl(ehci, hostpc_reg);
319 ehci_writel(ehci, t3 | HOSTPC_PHCD, hostpc_reg);
320 t3 = ehci_readl(ehci, hostpc_reg);
321 ehci_dbg(ehci, "Port %d phy low-power mode %s\n",
322 port, (t3 & HOSTPC_PHCD) ?
323 "succeeded" : "failed");
326 spin_unlock_irq(&ehci->lock);
328 /* Apparently some devices need a >= 1-uframe delay here */
329 if (ehci->bus_suspended)
330 udelay(150);
332 /* turn off now-idle HC */
333 ehci_halt (ehci);
335 spin_lock_irq(&ehci->lock);
336 if (ehci->enabled_hrtimer_events & BIT(EHCI_HRTIMER_POLL_DEAD))
337 ehci_handle_controller_death(ehci);
338 if (ehci->rh_state != EHCI_RH_RUNNING)
339 goto done;
340 ehci->rh_state = EHCI_RH_SUSPENDED;
342 end_unlink_async(ehci);
343 unlink_empty_async_suspended(ehci);
344 ehci_handle_start_intr_unlinks(ehci);
345 ehci_handle_intr_unlinks(ehci);
346 end_free_itds(ehci);
348 /* allow remote wakeup */
349 mask = INTR_MASK;
350 if (!hcd->self.root_hub->do_remote_wakeup)
351 mask &= ~STS_PCD;
352 ehci_writel(ehci, mask, &ehci->regs->intr_enable);
353 ehci_readl(ehci, &ehci->regs->intr_enable);
355 done:
356 ehci->next_statechange = jiffies + msecs_to_jiffies(10);
357 ehci->enabled_hrtimer_events = 0;
358 ehci->next_hrtimer_event = EHCI_HRTIMER_NO_EVENT;
359 spin_unlock_irq (&ehci->lock);
361 hrtimer_cancel(&ehci->hrtimer);
362 return 0;
366 /* caller has locked the root hub, and should reset/reinit on error */
367 static int ehci_bus_resume (struct usb_hcd *hcd)
369 struct ehci_hcd *ehci = hcd_to_ehci (hcd);
370 u32 temp;
371 u32 power_okay;
372 int i;
373 unsigned long resume_needed = 0;
375 if (time_before (jiffies, ehci->next_statechange))
376 msleep(5);
377 spin_lock_irq (&ehci->lock);
378 if (!HCD_HW_ACCESSIBLE(hcd) || ehci->shutdown)
379 goto shutdown;
381 if (unlikely(ehci->debug)) {
382 if (!dbgp_reset_prep(hcd))
383 ehci->debug = NULL;
384 else
385 dbgp_external_startup(hcd);
388 /* Ideally and we've got a real resume here, and no port's power
389 * was lost. (For PCI, that means Vaux was maintained.) But we
390 * could instead be restoring a swsusp snapshot -- so that BIOS was
391 * the last user of the controller, not reset/pm hardware keeping
392 * state we gave to it.
394 power_okay = ehci_readl(ehci, &ehci->regs->intr_enable);
395 ehci_dbg(ehci, "resume root hub%s\n",
396 power_okay ? "" : " after power loss");
398 /* at least some APM implementations will try to deliver
399 * IRQs right away, so delay them until we're ready.
401 ehci_writel(ehci, 0, &ehci->regs->intr_enable);
403 /* re-init operational registers */
404 ehci_writel(ehci, 0, &ehci->regs->segment);
405 ehci_writel(ehci, ehci->periodic_dma, &ehci->regs->frame_list);
406 ehci_writel(ehci, (u32) ehci->async->qh_dma, &ehci->regs->async_next);
408 /* restore CMD_RUN, framelist size, and irq threshold */
409 ehci->command |= CMD_RUN;
410 ehci_writel(ehci, ehci->command, &ehci->regs->command);
411 ehci->rh_state = EHCI_RH_RUNNING;
414 * According to Bugzilla #8190, the port status for some controllers
415 * will be wrong without a delay. At their wrong status, the port
416 * is enabled, but not suspended neither resumed.
418 i = HCS_N_PORTS(ehci->hcs_params);
419 while (i--) {
420 temp = ehci_readl(ehci, &ehci->regs->port_status[i]);
421 if ((temp & PORT_PE) &&
422 !(temp & (PORT_SUSPEND | PORT_RESUME))) {
423 ehci_dbg(ehci, "Port status(0x%x) is wrong\n", temp);
424 spin_unlock_irq(&ehci->lock);
425 msleep(8);
426 spin_lock_irq(&ehci->lock);
427 break;
431 if (ehci->shutdown)
432 goto shutdown;
434 /* clear phy low-power mode before resume */
435 if (ehci->bus_suspended && ehci->has_tdi_phy_lpm) {
436 i = HCS_N_PORTS(ehci->hcs_params);
437 while (i--) {
438 if (test_bit(i, &ehci->bus_suspended)) {
439 u32 __iomem *hostpc_reg =
440 &ehci->regs->hostpc[i];
442 temp = ehci_readl(ehci, hostpc_reg);
443 ehci_writel(ehci, temp & ~HOSTPC_PHCD,
444 hostpc_reg);
447 spin_unlock_irq(&ehci->lock);
448 msleep(5);
449 spin_lock_irq(&ehci->lock);
450 if (ehci->shutdown)
451 goto shutdown;
454 /* manually resume the ports we suspended during bus_suspend() */
455 i = HCS_N_PORTS (ehci->hcs_params);
456 while (i--) {
457 temp = ehci_readl(ehci, &ehci->regs->port_status [i]);
458 temp &= ~(PORT_RWC_BITS | PORT_WAKE_BITS);
459 if (test_bit(i, &ehci->bus_suspended) &&
460 (temp & PORT_SUSPEND)) {
461 temp |= PORT_RESUME;
462 set_bit(i, &resume_needed);
464 ehci_writel(ehci, temp, &ehci->regs->port_status [i]);
467 /* msleep for 20ms only if code is trying to resume port */
468 if (resume_needed) {
469 spin_unlock_irq(&ehci->lock);
470 msleep(20);
471 spin_lock_irq(&ehci->lock);
472 if (ehci->shutdown)
473 goto shutdown;
476 i = HCS_N_PORTS (ehci->hcs_params);
477 while (i--) {
478 temp = ehci_readl(ehci, &ehci->regs->port_status [i]);
479 if (test_bit(i, &resume_needed)) {
480 temp &= ~(PORT_RWC_BITS | PORT_SUSPEND | PORT_RESUME);
481 ehci_writel(ehci, temp, &ehci->regs->port_status [i]);
485 ehci->next_statechange = jiffies + msecs_to_jiffies(5);
486 spin_unlock_irq(&ehci->lock);
488 ehci_handover_companion_ports(ehci);
490 /* Now we can safely re-enable irqs */
491 spin_lock_irq(&ehci->lock);
492 if (ehci->shutdown)
493 goto shutdown;
494 ehci_writel(ehci, INTR_MASK, &ehci->regs->intr_enable);
495 (void) ehci_readl(ehci, &ehci->regs->intr_enable);
496 spin_unlock_irq(&ehci->lock);
498 return 0;
500 shutdown:
501 spin_unlock_irq(&ehci->lock);
502 return -ESHUTDOWN;
505 #else
507 #define ehci_bus_suspend NULL
508 #define ehci_bus_resume NULL
510 #endif /* CONFIG_PM */
512 /*-------------------------------------------------------------------------*/
515 * Sets the owner of a port
517 static void set_owner(struct ehci_hcd *ehci, int portnum, int new_owner)
519 u32 __iomem *status_reg;
520 u32 port_status;
521 int try;
523 status_reg = &ehci->regs->port_status[portnum];
526 * The controller won't set the OWNER bit if the port is
527 * enabled, so this loop will sometimes require at least two
528 * iterations: one to disable the port and one to set OWNER.
530 for (try = 4; try > 0; --try) {
531 spin_lock_irq(&ehci->lock);
532 port_status = ehci_readl(ehci, status_reg);
533 if ((port_status & PORT_OWNER) == new_owner
534 || (port_status & (PORT_OWNER | PORT_CONNECT))
535 == 0)
536 try = 0;
537 else {
538 port_status ^= PORT_OWNER;
539 port_status &= ~(PORT_PE | PORT_RWC_BITS);
540 ehci_writel(ehci, port_status, status_reg);
542 spin_unlock_irq(&ehci->lock);
543 if (try > 1)
544 msleep(5);
548 /*-------------------------------------------------------------------------*/
550 static int check_reset_complete (
551 struct ehci_hcd *ehci,
552 int index,
553 u32 __iomem *status_reg,
554 int port_status
556 if (!(port_status & PORT_CONNECT))
557 return port_status;
559 /* if reset finished and it's still not enabled -- handoff */
560 if (!(port_status & PORT_PE)) {
562 /* with integrated TT, there's nobody to hand it to! */
563 if (ehci_is_TDI(ehci)) {
564 ehci_dbg (ehci,
565 "Failed to enable port %d on root hub TT\n",
566 index+1);
567 return port_status;
570 ehci_dbg (ehci, "port %d full speed --> companion\n",
571 index + 1);
573 // what happens if HCS_N_CC(params) == 0 ?
574 port_status |= PORT_OWNER;
575 port_status &= ~PORT_RWC_BITS;
576 ehci_writel(ehci, port_status, status_reg);
578 /* ensure 440EPX ohci controller state is operational */
579 if (ehci->has_amcc_usb23)
580 set_ohci_hcfs(ehci, 1);
581 } else {
582 ehci_dbg(ehci, "port %d reset complete, port enabled\n",
583 index + 1);
584 /* ensure 440EPx ohci controller state is suspended */
585 if (ehci->has_amcc_usb23)
586 set_ohci_hcfs(ehci, 0);
589 return port_status;
592 /*-------------------------------------------------------------------------*/
595 /* build "status change" packet (one or two bytes) from HC registers */
597 static int
598 ehci_hub_status_data (struct usb_hcd *hcd, char *buf)
600 struct ehci_hcd *ehci = hcd_to_ehci (hcd);
601 u32 temp, status;
602 u32 mask;
603 int ports, i, retval = 1;
604 unsigned long flags;
605 u32 ppcd = ~0;
607 /* init status to no-changes */
608 buf [0] = 0;
609 ports = HCS_N_PORTS (ehci->hcs_params);
610 if (ports > 7) {
611 buf [1] = 0;
612 retval++;
615 /* Inform the core about resumes-in-progress by returning
616 * a non-zero value even if there are no status changes.
618 status = ehci->resuming_ports;
620 /* Some boards (mostly VIA?) report bogus overcurrent indications,
621 * causing massive log spam unless we completely ignore them. It
622 * may be relevant that VIA VT8235 controllers, where PORT_POWER is
623 * always set, seem to clear PORT_OCC and PORT_CSC when writing to
624 * PORT_POWER; that's surprising, but maybe within-spec.
626 if (!ignore_oc)
627 mask = PORT_CSC | PORT_PEC | PORT_OCC;
628 else
629 mask = PORT_CSC | PORT_PEC;
630 // PORT_RESUME from hardware ~= PORT_STAT_C_SUSPEND
632 /* no hub change reports (bit 0) for now (power, ...) */
634 /* port N changes (bit N)? */
635 spin_lock_irqsave (&ehci->lock, flags);
637 /* get per-port change detect bits */
638 if (ehci->has_ppcd)
639 ppcd = ehci_readl(ehci, &ehci->regs->status) >> 16;
641 for (i = 0; i < ports; i++) {
642 /* leverage per-port change bits feature */
643 if (ppcd & (1 << i))
644 temp = ehci_readl(ehci, &ehci->regs->port_status[i]);
645 else
646 temp = 0;
649 * Return status information even for ports with OWNER set.
650 * Otherwise khubd wouldn't see the disconnect event when a
651 * high-speed device is switched over to the companion
652 * controller by the user.
655 if ((temp & mask) != 0 || test_bit(i, &ehci->port_c_suspend)
656 || (ehci->reset_done[i] && time_after_eq(
657 jiffies, ehci->reset_done[i]))) {
658 if (i < 7)
659 buf [0] |= 1 << (i + 1);
660 else
661 buf [1] |= 1 << (i - 7);
662 status = STS_PCD;
666 /* If a resume is in progress, make sure it can finish */
667 if (ehci->resuming_ports)
668 mod_timer(&hcd->rh_timer, jiffies + msecs_to_jiffies(25));
670 spin_unlock_irqrestore (&ehci->lock, flags);
671 return status ? retval : 0;
674 /*-------------------------------------------------------------------------*/
676 static void
677 ehci_hub_descriptor (
678 struct ehci_hcd *ehci,
679 struct usb_hub_descriptor *desc
681 int ports = HCS_N_PORTS (ehci->hcs_params);
682 u16 temp;
684 desc->bDescriptorType = 0x29;
685 desc->bPwrOn2PwrGood = 10; /* ehci 1.0, 2.3.9 says 20ms max */
686 desc->bHubContrCurrent = 0;
688 desc->bNbrPorts = ports;
689 temp = 1 + (ports / 8);
690 desc->bDescLength = 7 + 2 * temp;
692 /* two bitmaps: ports removable, and usb 1.0 legacy PortPwrCtrlMask */
693 memset(&desc->u.hs.DeviceRemovable[0], 0, temp);
694 memset(&desc->u.hs.DeviceRemovable[temp], 0xff, temp);
696 temp = 0x0008; /* per-port overcurrent reporting */
697 if (HCS_PPC (ehci->hcs_params))
698 temp |= 0x0001; /* per-port power control */
699 else
700 temp |= 0x0002; /* no power switching */
701 #if 0
702 // re-enable when we support USB_PORT_FEAT_INDICATOR below.
703 if (HCS_INDICATOR (ehci->hcs_params))
704 temp |= 0x0080; /* per-port indicators (LEDs) */
705 #endif
706 desc->wHubCharacteristics = cpu_to_le16(temp);
709 /*-------------------------------------------------------------------------*/
710 #ifdef CONFIG_USB_HCD_TEST_MODE
712 #define EHSET_TEST_SINGLE_STEP_SET_FEATURE 0x06
714 static void usb_ehset_completion(struct urb *urb)
716 struct completion *done = urb->context;
718 complete(done);
720 static int submit_single_step_set_feature(
721 struct usb_hcd *hcd,
722 struct urb *urb,
723 int is_setup
727 * Allocate and initialize a control URB. This request will be used by the
728 * EHSET SINGLE_STEP_SET_FEATURE test in which the DATA and STATUS stages
729 * of the GetDescriptor request are sent 15 seconds after the SETUP stage.
730 * Return NULL if failed.
732 static struct urb *request_single_step_set_feature_urb(
733 struct usb_device *udev,
734 void *dr,
735 void *buf,
736 struct completion *done
738 struct urb *urb;
739 struct usb_hcd *hcd = bus_to_hcd(udev->bus);
740 struct usb_host_endpoint *ep;
742 urb = usb_alloc_urb(0, GFP_KERNEL);
743 if (!urb)
744 return NULL;
746 urb->pipe = usb_rcvctrlpipe(udev, 0);
747 ep = (usb_pipein(urb->pipe) ? udev->ep_in : udev->ep_out)
748 [usb_pipeendpoint(urb->pipe)];
749 if (!ep) {
750 usb_free_urb(urb);
751 return NULL;
754 urb->ep = ep;
755 urb->dev = udev;
756 urb->setup_packet = (void *)dr;
757 urb->transfer_buffer = buf;
758 urb->transfer_buffer_length = USB_DT_DEVICE_SIZE;
759 urb->complete = usb_ehset_completion;
760 urb->status = -EINPROGRESS;
761 urb->actual_length = 0;
762 urb->transfer_flags = URB_DIR_IN;
763 usb_get_urb(urb);
764 atomic_inc(&urb->use_count);
765 atomic_inc(&urb->dev->urbnum);
766 urb->setup_dma = dma_map_single(
767 hcd->self.controller,
768 urb->setup_packet,
769 sizeof(struct usb_ctrlrequest),
770 DMA_TO_DEVICE);
771 urb->transfer_dma = dma_map_single(
772 hcd->self.controller,
773 urb->transfer_buffer,
774 urb->transfer_buffer_length,
775 DMA_FROM_DEVICE);
776 urb->context = done;
777 return urb;
780 static int ehset_single_step_set_feature(struct usb_hcd *hcd, int port)
782 int retval = -ENOMEM;
783 struct usb_ctrlrequest *dr;
784 struct urb *urb;
785 struct usb_device *udev;
786 struct ehci_hcd *ehci = hcd_to_ehci(hcd);
787 struct usb_device_descriptor *buf;
788 DECLARE_COMPLETION_ONSTACK(done);
790 /* Obtain udev of the rhub's child port */
791 udev = usb_hub_find_child(hcd->self.root_hub, port);
792 if (!udev) {
793 ehci_err(ehci, "No device attached to the RootHub\n");
794 return -ENODEV;
796 buf = kmalloc(USB_DT_DEVICE_SIZE, GFP_KERNEL);
797 if (!buf)
798 return -ENOMEM;
800 dr = kmalloc(sizeof(struct usb_ctrlrequest), GFP_KERNEL);
801 if (!dr) {
802 kfree(buf);
803 return -ENOMEM;
806 /* Fill Setup packet for GetDescriptor */
807 dr->bRequestType = USB_DIR_IN;
808 dr->bRequest = USB_REQ_GET_DESCRIPTOR;
809 dr->wValue = cpu_to_le16(USB_DT_DEVICE << 8);
810 dr->wIndex = 0;
811 dr->wLength = cpu_to_le16(USB_DT_DEVICE_SIZE);
812 urb = request_single_step_set_feature_urb(udev, dr, buf, &done);
813 if (!urb)
814 goto cleanup;
816 /* Submit just the SETUP stage */
817 retval = submit_single_step_set_feature(hcd, urb, 1);
818 if (retval)
819 goto out1;
820 if (!wait_for_completion_timeout(&done, msecs_to_jiffies(2000))) {
821 usb_kill_urb(urb);
822 retval = -ETIMEDOUT;
823 ehci_err(ehci, "%s SETUP stage timed out on ep0\n", __func__);
824 goto out1;
826 msleep(15 * 1000);
828 /* Complete remaining DATA and STATUS stages using the same URB */
829 urb->status = -EINPROGRESS;
830 usb_get_urb(urb);
831 atomic_inc(&urb->use_count);
832 atomic_inc(&urb->dev->urbnum);
833 retval = submit_single_step_set_feature(hcd, urb, 0);
834 if (!retval && !wait_for_completion_timeout(&done,
835 msecs_to_jiffies(2000))) {
836 usb_kill_urb(urb);
837 retval = -ETIMEDOUT;
838 ehci_err(ehci, "%s IN stage timed out on ep0\n", __func__);
840 out1:
841 usb_free_urb(urb);
842 cleanup:
843 kfree(dr);
844 kfree(buf);
845 return retval;
847 #endif /* CONFIG_USB_HCD_TEST_MODE */
848 /*-------------------------------------------------------------------------*/
850 static int ehci_hub_control (
851 struct usb_hcd *hcd,
852 u16 typeReq,
853 u16 wValue,
854 u16 wIndex,
855 char *buf,
856 u16 wLength
858 struct ehci_hcd *ehci = hcd_to_ehci (hcd);
859 int ports = HCS_N_PORTS (ehci->hcs_params);
860 u32 __iomem *status_reg = &ehci->regs->port_status[
861 (wIndex & 0xff) - 1];
862 u32 __iomem *hostpc_reg = &ehci->regs->hostpc[(wIndex & 0xff) - 1];
863 u32 temp, temp1, status;
864 unsigned long flags;
865 int retval = 0;
866 unsigned selector;
869 * FIXME: support SetPortFeatures USB_PORT_FEAT_INDICATOR.
870 * HCS_INDICATOR may say we can change LEDs to off/amber/green.
871 * (track current state ourselves) ... blink for diagnostics,
872 * power, "this is the one", etc. EHCI spec supports this.
875 spin_lock_irqsave (&ehci->lock, flags);
876 switch (typeReq) {
877 case ClearHubFeature:
878 switch (wValue) {
879 case C_HUB_LOCAL_POWER:
880 case C_HUB_OVER_CURRENT:
881 /* no hub-wide feature/status flags */
882 break;
883 default:
884 goto error;
886 break;
887 case ClearPortFeature:
888 if (!wIndex || wIndex > ports)
889 goto error;
890 wIndex--;
891 temp = ehci_readl(ehci, status_reg);
892 temp &= ~PORT_RWC_BITS;
895 * Even if OWNER is set, so the port is owned by the
896 * companion controller, khubd needs to be able to clear
897 * the port-change status bits (especially
898 * USB_PORT_STAT_C_CONNECTION).
901 switch (wValue) {
902 case USB_PORT_FEAT_ENABLE:
903 ehci_writel(ehci, temp & ~PORT_PE, status_reg);
904 break;
905 case USB_PORT_FEAT_C_ENABLE:
906 ehci_writel(ehci, temp | PORT_PEC, status_reg);
907 break;
908 case USB_PORT_FEAT_SUSPEND:
909 if (temp & PORT_RESET)
910 goto error;
911 if (ehci->no_selective_suspend)
912 break;
913 #ifdef CONFIG_USB_OTG
914 if ((hcd->self.otg_port == (wIndex + 1))
915 && hcd->self.b_hnp_enable) {
916 otg_start_hnp(hcd->phy->otg);
917 break;
919 #endif
920 if (!(temp & PORT_SUSPEND))
921 break;
922 if ((temp & PORT_PE) == 0)
923 goto error;
925 /* clear phy low-power mode before resume */
926 if (ehci->has_tdi_phy_lpm) {
927 temp1 = ehci_readl(ehci, hostpc_reg);
928 ehci_writel(ehci, temp1 & ~HOSTPC_PHCD,
929 hostpc_reg);
930 spin_unlock_irqrestore(&ehci->lock, flags);
931 msleep(5);/* wait to leave low-power mode */
932 spin_lock_irqsave(&ehci->lock, flags);
934 /* resume signaling for 20 msec */
935 temp &= ~PORT_WAKE_BITS;
936 ehci_writel(ehci, temp | PORT_RESUME, status_reg);
937 ehci->reset_done[wIndex] = jiffies
938 + msecs_to_jiffies(20);
939 set_bit(wIndex, &ehci->resuming_ports);
940 usb_hcd_start_port_resume(&hcd->self, wIndex);
941 break;
942 case USB_PORT_FEAT_C_SUSPEND:
943 clear_bit(wIndex, &ehci->port_c_suspend);
944 break;
945 case USB_PORT_FEAT_POWER:
946 if (HCS_PPC (ehci->hcs_params))
947 ehci_writel(ehci, temp & ~PORT_POWER,
948 status_reg);
949 break;
950 case USB_PORT_FEAT_C_CONNECTION:
951 ehci_writel(ehci, temp | PORT_CSC, status_reg);
952 break;
953 case USB_PORT_FEAT_C_OVER_CURRENT:
954 ehci_writel(ehci, temp | PORT_OCC, status_reg);
955 break;
956 case USB_PORT_FEAT_C_RESET:
957 /* GetPortStatus clears reset */
958 break;
959 default:
960 goto error;
962 ehci_readl(ehci, &ehci->regs->command); /* unblock posted write */
963 break;
964 case GetHubDescriptor:
965 ehci_hub_descriptor (ehci, (struct usb_hub_descriptor *)
966 buf);
967 break;
968 case GetHubStatus:
969 /* no hub-wide feature/status flags */
970 memset (buf, 0, 4);
971 //cpu_to_le32s ((u32 *) buf);
972 break;
973 case GetPortStatus:
974 if (!wIndex || wIndex > ports)
975 goto error;
976 wIndex--;
977 status = 0;
978 temp = ehci_readl(ehci, status_reg);
980 // wPortChange bits
981 if (temp & PORT_CSC)
982 status |= USB_PORT_STAT_C_CONNECTION << 16;
983 if (temp & PORT_PEC)
984 status |= USB_PORT_STAT_C_ENABLE << 16;
986 if ((temp & PORT_OCC) && !ignore_oc){
987 status |= USB_PORT_STAT_C_OVERCURRENT << 16;
990 * Hubs should disable port power on over-current.
991 * However, not all EHCI implementations do this
992 * automatically, even if they _do_ support per-port
993 * power switching; they're allowed to just limit the
994 * current. khubd will turn the power back on.
996 if (((temp & PORT_OC) || (ehci->need_oc_pp_cycle))
997 && HCS_PPC(ehci->hcs_params)) {
998 ehci_writel(ehci,
999 temp & ~(PORT_RWC_BITS | PORT_POWER),
1000 status_reg);
1001 temp = ehci_readl(ehci, status_reg);
1005 /* no reset or resume pending */
1006 if (!ehci->reset_done[wIndex]) {
1008 /* Remote Wakeup received? */
1009 if (temp & PORT_RESUME) {
1010 /* resume signaling for 20 msec */
1011 ehci->reset_done[wIndex] = jiffies
1012 + msecs_to_jiffies(20);
1013 usb_hcd_start_port_resume(&hcd->self, wIndex);
1014 set_bit(wIndex, &ehci->resuming_ports);
1015 /* check the port again */
1016 mod_timer(&ehci_to_hcd(ehci)->rh_timer,
1017 ehci->reset_done[wIndex]);
1020 /* reset or resume not yet complete */
1021 } else if (!time_after_eq(jiffies, ehci->reset_done[wIndex])) {
1022 ; /* wait until it is complete */
1024 /* resume completed */
1025 } else if (test_bit(wIndex, &ehci->resuming_ports)) {
1026 clear_bit(wIndex, &ehci->suspended_ports);
1027 set_bit(wIndex, &ehci->port_c_suspend);
1028 ehci->reset_done[wIndex] = 0;
1029 usb_hcd_end_port_resume(&hcd->self, wIndex);
1031 /* stop resume signaling */
1032 temp &= ~(PORT_RWC_BITS | PORT_SUSPEND | PORT_RESUME);
1033 ehci_writel(ehci, temp, status_reg);
1034 clear_bit(wIndex, &ehci->resuming_ports);
1035 retval = ehci_handshake(ehci, status_reg,
1036 PORT_RESUME, 0, 2000 /* 2msec */);
1037 if (retval != 0) {
1038 ehci_err(ehci, "port %d resume error %d\n",
1039 wIndex + 1, retval);
1040 goto error;
1042 temp = ehci_readl(ehci, status_reg);
1044 /* whoever resets must GetPortStatus to complete it!! */
1045 } else {
1046 status |= USB_PORT_STAT_C_RESET << 16;
1047 ehci->reset_done [wIndex] = 0;
1049 /* force reset to complete */
1050 ehci_writel(ehci, temp & ~(PORT_RWC_BITS | PORT_RESET),
1051 status_reg);
1052 /* REVISIT: some hardware needs 550+ usec to clear
1053 * this bit; seems too long to spin routinely...
1055 retval = ehci_handshake(ehci, status_reg,
1056 PORT_RESET, 0, 1000);
1057 if (retval != 0) {
1058 ehci_err (ehci, "port %d reset error %d\n",
1059 wIndex + 1, retval);
1060 goto error;
1063 /* see what we found out */
1064 temp = check_reset_complete (ehci, wIndex, status_reg,
1065 ehci_readl(ehci, status_reg));
1068 /* transfer dedicated ports to the companion hc */
1069 if ((temp & PORT_CONNECT) &&
1070 test_bit(wIndex, &ehci->companion_ports)) {
1071 temp &= ~PORT_RWC_BITS;
1072 temp |= PORT_OWNER;
1073 ehci_writel(ehci, temp, status_reg);
1074 ehci_dbg(ehci, "port %d --> companion\n", wIndex + 1);
1075 temp = ehci_readl(ehci, status_reg);
1079 * Even if OWNER is set, there's no harm letting khubd
1080 * see the wPortStatus values (they should all be 0 except
1081 * for PORT_POWER anyway).
1084 if (temp & PORT_CONNECT) {
1085 status |= USB_PORT_STAT_CONNECTION;
1086 // status may be from integrated TT
1087 if (ehci->has_hostpc) {
1088 temp1 = ehci_readl(ehci, hostpc_reg);
1089 status |= ehci_port_speed(ehci, temp1);
1090 } else
1091 status |= ehci_port_speed(ehci, temp);
1093 if (temp & PORT_PE)
1094 status |= USB_PORT_STAT_ENABLE;
1096 /* maybe the port was unsuspended without our knowledge */
1097 if (temp & (PORT_SUSPEND|PORT_RESUME)) {
1098 status |= USB_PORT_STAT_SUSPEND;
1099 } else if (test_bit(wIndex, &ehci->suspended_ports)) {
1100 clear_bit(wIndex, &ehci->suspended_ports);
1101 clear_bit(wIndex, &ehci->resuming_ports);
1102 ehci->reset_done[wIndex] = 0;
1103 if (temp & PORT_PE)
1104 set_bit(wIndex, &ehci->port_c_suspend);
1105 usb_hcd_end_port_resume(&hcd->self, wIndex);
1108 if (temp & PORT_OC)
1109 status |= USB_PORT_STAT_OVERCURRENT;
1110 if (temp & PORT_RESET)
1111 status |= USB_PORT_STAT_RESET;
1112 if (temp & PORT_POWER)
1113 status |= USB_PORT_STAT_POWER;
1114 if (test_bit(wIndex, &ehci->port_c_suspend))
1115 status |= USB_PORT_STAT_C_SUSPEND << 16;
1117 #ifndef VERBOSE_DEBUG
1118 if (status & ~0xffff) /* only if wPortChange is interesting */
1119 #endif
1120 dbg_port (ehci, "GetStatus", wIndex + 1, temp);
1121 put_unaligned_le32(status, buf);
1122 break;
1123 case SetHubFeature:
1124 switch (wValue) {
1125 case C_HUB_LOCAL_POWER:
1126 case C_HUB_OVER_CURRENT:
1127 /* no hub-wide feature/status flags */
1128 break;
1129 default:
1130 goto error;
1132 break;
1133 case SetPortFeature:
1134 selector = wIndex >> 8;
1135 wIndex &= 0xff;
1136 if (unlikely(ehci->debug)) {
1137 /* If the debug port is active any port
1138 * feature requests should get denied */
1139 if (wIndex == HCS_DEBUG_PORT(ehci->hcs_params) &&
1140 (readl(&ehci->debug->control) & DBGP_ENABLED)) {
1141 retval = -ENODEV;
1142 goto error_exit;
1145 if (!wIndex || wIndex > ports)
1146 goto error;
1147 wIndex--;
1148 temp = ehci_readl(ehci, status_reg);
1149 if (temp & PORT_OWNER)
1150 break;
1152 temp &= ~PORT_RWC_BITS;
1153 switch (wValue) {
1154 case USB_PORT_FEAT_SUSPEND:
1155 if (ehci->no_selective_suspend)
1156 break;
1157 if ((temp & PORT_PE) == 0
1158 || (temp & PORT_RESET) != 0)
1159 goto error;
1161 /* After above check the port must be connected.
1162 * Set appropriate bit thus could put phy into low power
1163 * mode if we have tdi_phy_lpm feature
1165 temp &= ~PORT_WKCONN_E;
1166 temp |= PORT_WKDISC_E | PORT_WKOC_E;
1167 ehci_writel(ehci, temp | PORT_SUSPEND, status_reg);
1168 if (ehci->has_tdi_phy_lpm) {
1169 spin_unlock_irqrestore(&ehci->lock, flags);
1170 msleep(5);/* 5ms for HCD enter low pwr mode */
1171 spin_lock_irqsave(&ehci->lock, flags);
1172 temp1 = ehci_readl(ehci, hostpc_reg);
1173 ehci_writel(ehci, temp1 | HOSTPC_PHCD,
1174 hostpc_reg);
1175 temp1 = ehci_readl(ehci, hostpc_reg);
1176 ehci_dbg(ehci, "Port%d phy low pwr mode %s\n",
1177 wIndex, (temp1 & HOSTPC_PHCD) ?
1178 "succeeded" : "failed");
1180 set_bit(wIndex, &ehci->suspended_ports);
1181 break;
1182 case USB_PORT_FEAT_POWER:
1183 if (HCS_PPC (ehci->hcs_params))
1184 ehci_writel(ehci, temp | PORT_POWER,
1185 status_reg);
1186 break;
1187 case USB_PORT_FEAT_RESET:
1188 if (temp & (PORT_SUSPEND|PORT_RESUME))
1189 goto error;
1190 /* line status bits may report this as low speed,
1191 * which can be fine if this root hub has a
1192 * transaction translator built in.
1194 if ((temp & (PORT_PE|PORT_CONNECT)) == PORT_CONNECT
1195 && !ehci_is_TDI(ehci)
1196 && PORT_USB11 (temp)) {
1197 ehci_dbg (ehci,
1198 "port %d low speed --> companion\n",
1199 wIndex + 1);
1200 temp |= PORT_OWNER;
1201 } else {
1202 temp |= PORT_RESET;
1203 temp &= ~PORT_PE;
1206 * caller must wait, then call GetPortStatus
1207 * usb 2.0 spec says 50 ms resets on root
1209 ehci->reset_done [wIndex] = jiffies
1210 + msecs_to_jiffies (50);
1212 ehci_writel(ehci, temp, status_reg);
1213 break;
1215 /* For downstream facing ports (these): one hub port is put
1216 * into test mode according to USB2 11.24.2.13, then the hub
1217 * must be reset (which for root hub now means rmmod+modprobe,
1218 * or else system reboot). See EHCI 2.3.9 and 4.14 for info
1219 * about the EHCI-specific stuff.
1221 case USB_PORT_FEAT_TEST:
1222 #ifdef CONFIG_USB_HCD_TEST_MODE
1223 if (selector == EHSET_TEST_SINGLE_STEP_SET_FEATURE) {
1224 spin_unlock_irqrestore(&ehci->lock, flags);
1225 retval = ehset_single_step_set_feature(hcd,
1226 wIndex);
1227 spin_lock_irqsave(&ehci->lock, flags);
1228 break;
1230 #endif
1231 if (!selector || selector > 5)
1232 goto error;
1233 spin_unlock_irqrestore(&ehci->lock, flags);
1234 ehci_quiesce(ehci);
1235 spin_lock_irqsave(&ehci->lock, flags);
1237 /* Put all enabled ports into suspend */
1238 while (ports--) {
1239 u32 __iomem *sreg =
1240 &ehci->regs->port_status[ports];
1242 temp = ehci_readl(ehci, sreg) & ~PORT_RWC_BITS;
1243 if (temp & PORT_PE)
1244 ehci_writel(ehci, temp | PORT_SUSPEND,
1245 sreg);
1248 spin_unlock_irqrestore(&ehci->lock, flags);
1249 ehci_halt(ehci);
1250 spin_lock_irqsave(&ehci->lock, flags);
1252 temp = ehci_readl(ehci, status_reg);
1253 temp |= selector << 16;
1254 ehci_writel(ehci, temp, status_reg);
1255 break;
1257 default:
1258 goto error;
1260 ehci_readl(ehci, &ehci->regs->command); /* unblock posted writes */
1261 break;
1263 default:
1264 error:
1265 /* "stall" on error */
1266 retval = -EPIPE;
1268 error_exit:
1269 spin_unlock_irqrestore (&ehci->lock, flags);
1270 return retval;
1273 static void ehci_relinquish_port(struct usb_hcd *hcd, int portnum)
1275 struct ehci_hcd *ehci = hcd_to_ehci(hcd);
1277 if (ehci_is_TDI(ehci))
1278 return;
1279 set_owner(ehci, --portnum, PORT_OWNER);
1282 static int ehci_port_handed_over(struct usb_hcd *hcd, int portnum)
1284 struct ehci_hcd *ehci = hcd_to_ehci(hcd);
1285 u32 __iomem *reg;
1287 if (ehci_is_TDI(ehci))
1288 return 0;
1289 reg = &ehci->regs->port_status[portnum - 1];
1290 return ehci_readl(ehci, reg) & PORT_OWNER;