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
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)
36 static int ehci_hub_control(
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)
60 struct usb_hcd *hcd = ehci_to_hcd(ehci);
62 if (!ehci->owned_ports)
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))
75 /* Make sure the ports are powered */
76 port = HCS_N_PORTS(ehci->hcs_params);
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)) {
83 ehci_writel(ehci, status, reg);
88 /* Give the connections some time to appear */
91 spin_lock_irq(&ehci->lock);
92 port = HCS_N_PORTS(ehci->hcs_params);
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);
104 spin_unlock_irq(&ehci->lock);
105 ehci_hub_control(hcd, SetPortFeature,
106 USB_PORT_FEAT_RESET, port + 1,
108 spin_lock_irq(&ehci->lock);
112 spin_unlock_irq(&ehci->lock);
114 if (!ehci->owned_ports)
116 msleep(90); /* Wait for resets to complete */
118 spin_lock_irq(&ehci->lock);
119 port = HCS_N_PORTS(ehci->hcs_params);
121 if (test_bit(port, &ehci->owned_ports)) {
122 spin_unlock_irq(&ehci->lock);
123 ehci_hub_control(hcd, GetPortStatus,
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
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);
137 ehci_dbg(ehci, "failed handover port %d: %x\n",
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)
158 * Not all controllers appear to update this while going from D3 to D0,
159 * so check the individual port status registers as well
163 if (ehci_readl(ehci, &ehci->regs->port_status[i]) & PORT_CSC)
169 static void ehci_adjust_port_wakeup_flags(struct ehci_hcd *ehci,
170 bool suspending, bool do_wakeup)
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)
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);
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);
196 spin_lock_irq(&ehci->lock);
199 port = HCS_N_PORTS(ehci->hcs_params);
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.
209 if (t1 & PORT_CONNECT)
210 t2 |= PORT_WKOC_E | PORT_WKDISC_E;
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);
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);
242 ehci_dbg(ehci, "suspend root hub\n");
244 if (time_before (jiffies, ehci->next_statechange))
247 /* stop the schedules */
250 spin_lock_irq (&ehci->lock);
251 if (ehci->rh_state < EHCI_RH_RUNNING)
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");
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;
275 port = HCS_N_PORTS(ehci->hcs_params);
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 */
283 set_bit(port, &ehci->owned_ports);
284 else if ((t1 & PORT_PE) && !(t1 & 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;
299 t2 |= PORT_WKOC_E | PORT_WKCONN_E;
303 ehci_writel(ehci, t2, reg);
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);
315 u32 __iomem *hostpc_reg = &ehci->regs->hostpc[port];
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)
332 /* turn off now-idle HC */
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)
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);
348 /* allow remote wakeup */
350 if (!hcd->self.root_hub->do_remote_wakeup)
352 ehci_writel(ehci, mask, &ehci->regs->intr_enable);
353 ehci_readl(ehci, &ehci->regs->intr_enable);
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);
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);
373 unsigned long resume_needed = 0;
375 if (time_before (jiffies, ehci->next_statechange))
377 spin_lock_irq (&ehci->lock);
378 if (!HCD_HW_ACCESSIBLE(hcd) || ehci->shutdown)
381 if (unlikely(ehci->debug)) {
382 if (!dbgp_reset_prep(hcd))
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);
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);
426 spin_lock_irq(&ehci->lock);
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);
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,
447 spin_unlock_irq(&ehci->lock);
449 spin_lock_irq(&ehci->lock);
454 /* manually resume the ports we suspended during bus_suspend() */
455 i = HCS_N_PORTS (ehci->hcs_params);
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)) {
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 */
469 spin_unlock_irq(&ehci->lock);
471 spin_lock_irq(&ehci->lock);
476 i = HCS_N_PORTS (ehci->hcs_params);
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);
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);
501 spin_unlock_irq(&ehci->lock);
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;
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))
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);
548 /*-------------------------------------------------------------------------*/
550 static int check_reset_complete (
551 struct ehci_hcd *ehci,
553 u32 __iomem *status_reg,
556 if (!(port_status & PORT_CONNECT))
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)) {
565 "Failed to enable port %d on root hub TT\n",
570 ehci_dbg (ehci, "port %d full speed --> companion\n",
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);
582 ehci_dbg(ehci, "port %d reset complete, port enabled\n",
584 /* ensure 440EPx ohci controller state is suspended */
585 if (ehci->has_amcc_usb23)
586 set_ohci_hcfs(ehci, 0);
592 /*-------------------------------------------------------------------------*/
595 /* build "status change" packet (one or two bytes) from HC registers */
598 ehci_hub_status_data (struct usb_hcd *hcd, char *buf)
600 struct ehci_hcd *ehci = hcd_to_ehci (hcd);
603 int ports, i, retval = 1;
607 /* init status to no-changes */
609 ports = HCS_N_PORTS (ehci->hcs_params);
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.
627 mask = PORT_CSC | PORT_PEC | PORT_OCC;
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 */
639 ppcd = ehci_readl(ehci, &ehci->regs->status) >> 16;
641 for (i = 0; i < ports; i++) {
642 /* leverage per-port change bits feature */
644 temp = ehci_readl(ehci, &ehci->regs->port_status[i]);
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]))) {
659 buf [0] |= 1 << (i + 1);
661 buf [1] |= 1 << (i - 7);
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 /*-------------------------------------------------------------------------*/
677 ehci_hub_descriptor (
678 struct ehci_hcd *ehci,
679 struct usb_hub_descriptor *desc
681 int ports = HCS_N_PORTS (ehci->hcs_params);
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 */
700 temp |= 0x0002; /* no power switching */
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) */
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;
720 static int submit_single_step_set_feature(
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,
736 struct completion *done
739 struct usb_hcd *hcd = bus_to_hcd(udev->bus);
740 struct usb_host_endpoint *ep;
742 urb = usb_alloc_urb(0, GFP_KERNEL);
746 urb->pipe = usb_rcvctrlpipe(udev, 0);
747 ep = (usb_pipein(urb->pipe) ? udev->ep_in : udev->ep_out)
748 [usb_pipeendpoint(urb->pipe)];
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;
764 atomic_inc(&urb->use_count);
765 atomic_inc(&urb->dev->urbnum);
766 urb->setup_dma = dma_map_single(
767 hcd->self.controller,
769 sizeof(struct usb_ctrlrequest),
771 urb->transfer_dma = dma_map_single(
772 hcd->self.controller,
773 urb->transfer_buffer,
774 urb->transfer_buffer_length,
780 static int ehset_single_step_set_feature(struct usb_hcd *hcd, int port)
782 int retval = -ENOMEM;
783 struct usb_ctrlrequest *dr;
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);
793 ehci_err(ehci, "No device attached to the RootHub\n");
796 buf = kmalloc(USB_DT_DEVICE_SIZE, GFP_KERNEL);
800 dr = kmalloc(sizeof(struct usb_ctrlrequest), GFP_KERNEL);
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);
811 dr->wLength = cpu_to_le16(USB_DT_DEVICE_SIZE);
812 urb = request_single_step_set_feature_urb(udev, dr, buf, &done);
816 /* Submit just the SETUP stage */
817 retval = submit_single_step_set_feature(hcd, urb, 1);
820 if (!wait_for_completion_timeout(&done, msecs_to_jiffies(2000))) {
823 ehci_err(ehci, "%s SETUP stage timed out on ep0\n", __func__);
828 /* Complete remaining DATA and STATUS stages using the same URB */
829 urb->status = -EINPROGRESS;
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))) {
838 ehci_err(ehci, "%s IN stage timed out on ep0\n", __func__);
847 #endif /* CONFIG_USB_HCD_TEST_MODE */
848 /*-------------------------------------------------------------------------*/
850 static int ehci_hub_control (
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;
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);
877 case ClearHubFeature:
879 case C_HUB_LOCAL_POWER:
880 case C_HUB_OVER_CURRENT:
881 /* no hub-wide feature/status flags */
887 case ClearPortFeature:
888 if (!wIndex || wIndex > ports)
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).
902 case USB_PORT_FEAT_ENABLE:
903 ehci_writel(ehci, temp & ~PORT_PE, status_reg);
905 case USB_PORT_FEAT_C_ENABLE:
906 ehci_writel(ehci, temp | PORT_PEC, status_reg);
908 case USB_PORT_FEAT_SUSPEND:
909 if (temp & PORT_RESET)
911 if (ehci->no_selective_suspend)
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);
920 if (!(temp & PORT_SUSPEND))
922 if ((temp & PORT_PE) == 0)
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,
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);
942 case USB_PORT_FEAT_C_SUSPEND:
943 clear_bit(wIndex, &ehci->port_c_suspend);
945 case USB_PORT_FEAT_POWER:
946 if (HCS_PPC (ehci->hcs_params))
947 ehci_writel(ehci, temp & ~PORT_POWER,
950 case USB_PORT_FEAT_C_CONNECTION:
951 ehci_writel(ehci, temp | PORT_CSC, status_reg);
953 case USB_PORT_FEAT_C_OVER_CURRENT:
954 ehci_writel(ehci, temp | PORT_OCC, status_reg);
956 case USB_PORT_FEAT_C_RESET:
957 /* GetPortStatus clears reset */
962 ehci_readl(ehci, &ehci->regs->command); /* unblock posted write */
964 case GetHubDescriptor:
965 ehci_hub_descriptor (ehci, (struct usb_hub_descriptor *)
969 /* no hub-wide feature/status flags */
971 //cpu_to_le32s ((u32 *) buf);
974 if (!wIndex || wIndex > ports)
978 temp = ehci_readl(ehci, status_reg);
982 status |= USB_PORT_STAT_C_CONNECTION << 16;
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)) {
999 temp & ~(PORT_RWC_BITS | PORT_POWER),
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 */);
1038 ehci_err(ehci, "port %d resume error %d\n",
1039 wIndex + 1, retval);
1042 temp = ehci_readl(ehci, status_reg);
1044 /* whoever resets must GetPortStatus to complete it!! */
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),
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);
1058 ehci_err (ehci, "port %d reset error %d\n",
1059 wIndex + 1, retval);
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;
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);
1091 status |= ehci_port_speed(ehci, temp);
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;
1104 set_bit(wIndex, &ehci->port_c_suspend);
1105 usb_hcd_end_port_resume(&hcd->self, wIndex);
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 if (status & ~0xffff) /* only if wPortChange is interesting */
1118 dbg_port(ehci, "GetStatus", wIndex + 1, temp);
1119 put_unaligned_le32(status, buf);
1123 case C_HUB_LOCAL_POWER:
1124 case C_HUB_OVER_CURRENT:
1125 /* no hub-wide feature/status flags */
1131 case SetPortFeature:
1132 selector = wIndex >> 8;
1134 if (unlikely(ehci->debug)) {
1135 /* If the debug port is active any port
1136 * feature requests should get denied */
1137 if (wIndex == HCS_DEBUG_PORT(ehci->hcs_params) &&
1138 (readl(&ehci->debug->control) & DBGP_ENABLED)) {
1143 if (!wIndex || wIndex > ports)
1146 temp = ehci_readl(ehci, status_reg);
1147 if (temp & PORT_OWNER)
1150 temp &= ~PORT_RWC_BITS;
1152 case USB_PORT_FEAT_SUSPEND:
1153 if (ehci->no_selective_suspend)
1155 if ((temp & PORT_PE) == 0
1156 || (temp & PORT_RESET) != 0)
1159 /* After above check the port must be connected.
1160 * Set appropriate bit thus could put phy into low power
1161 * mode if we have tdi_phy_lpm feature
1163 temp &= ~PORT_WKCONN_E;
1164 temp |= PORT_WKDISC_E | PORT_WKOC_E;
1165 ehci_writel(ehci, temp | PORT_SUSPEND, status_reg);
1166 if (ehci->has_tdi_phy_lpm) {
1167 spin_unlock_irqrestore(&ehci->lock, flags);
1168 msleep(5);/* 5ms for HCD enter low pwr mode */
1169 spin_lock_irqsave(&ehci->lock, flags);
1170 temp1 = ehci_readl(ehci, hostpc_reg);
1171 ehci_writel(ehci, temp1 | HOSTPC_PHCD,
1173 temp1 = ehci_readl(ehci, hostpc_reg);
1174 ehci_dbg(ehci, "Port%d phy low pwr mode %s\n",
1175 wIndex, (temp1 & HOSTPC_PHCD) ?
1176 "succeeded" : "failed");
1178 set_bit(wIndex, &ehci->suspended_ports);
1180 case USB_PORT_FEAT_POWER:
1181 if (HCS_PPC (ehci->hcs_params))
1182 ehci_writel(ehci, temp | PORT_POWER,
1185 case USB_PORT_FEAT_RESET:
1186 if (temp & (PORT_SUSPEND|PORT_RESUME))
1188 /* line status bits may report this as low speed,
1189 * which can be fine if this root hub has a
1190 * transaction translator built in.
1192 if ((temp & (PORT_PE|PORT_CONNECT)) == PORT_CONNECT
1193 && !ehci_is_TDI(ehci)
1194 && PORT_USB11 (temp)) {
1196 "port %d low speed --> companion\n",
1204 * caller must wait, then call GetPortStatus
1205 * usb 2.0 spec says 50 ms resets on root
1207 ehci->reset_done [wIndex] = jiffies
1208 + msecs_to_jiffies (50);
1210 ehci_writel(ehci, temp, status_reg);
1213 /* For downstream facing ports (these): one hub port is put
1214 * into test mode according to USB2 11.24.2.13, then the hub
1215 * must be reset (which for root hub now means rmmod+modprobe,
1216 * or else system reboot). See EHCI 2.3.9 and 4.14 for info
1217 * about the EHCI-specific stuff.
1219 case USB_PORT_FEAT_TEST:
1220 #ifdef CONFIG_USB_HCD_TEST_MODE
1221 if (selector == EHSET_TEST_SINGLE_STEP_SET_FEATURE) {
1222 spin_unlock_irqrestore(&ehci->lock, flags);
1223 retval = ehset_single_step_set_feature(hcd,
1225 spin_lock_irqsave(&ehci->lock, flags);
1229 if (!selector || selector > 5)
1231 spin_unlock_irqrestore(&ehci->lock, flags);
1233 spin_lock_irqsave(&ehci->lock, flags);
1235 /* Put all enabled ports into suspend */
1238 &ehci->regs->port_status[ports];
1240 temp = ehci_readl(ehci, sreg) & ~PORT_RWC_BITS;
1242 ehci_writel(ehci, temp | PORT_SUSPEND,
1246 spin_unlock_irqrestore(&ehci->lock, flags);
1248 spin_lock_irqsave(&ehci->lock, flags);
1250 temp = ehci_readl(ehci, status_reg);
1251 temp |= selector << 16;
1252 ehci_writel(ehci, temp, status_reg);
1258 ehci_readl(ehci, &ehci->regs->command); /* unblock posted writes */
1263 /* "stall" on error */
1267 spin_unlock_irqrestore (&ehci->lock, flags);
1271 static void ehci_relinquish_port(struct usb_hcd *hcd, int portnum)
1273 struct ehci_hcd *ehci = hcd_to_ehci(hcd);
1275 if (ehci_is_TDI(ehci))
1277 set_owner(ehci, --portnum, PORT_OWNER);
1280 static int ehci_port_handed_over(struct usb_hcd *hcd, int portnum)
1282 struct ehci_hcd *ehci = hcd_to_ehci(hcd);
1285 if (ehci_is_TDI(ehci))
1287 reg = &ehci->regs->port_status[portnum - 1];
1288 return ehci_readl(ehci, reg) & PORT_OWNER;