New routers supported
[tomato.git] / release / src-rt / linux / linux-2.6 / drivers / usb / host / ehci-hub.c
blob6feddb25c7de7e6898d729df8ee9d3330343aa8d
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 */
20 static void qh_link_async (struct ehci_hcd *ehci, struct ehci_qh *qh);
21 static struct ehci_qh *qh_make (struct ehci_hcd *ehci, struct urb *urb, gfp_t flags);
22 static inline void ehci_qtd_init (struct ehci_hcd *ehci, struct ehci_qtd *qtd, dma_addr_t dma);
23 static inline void ehci_qtd_free (struct ehci_hcd *ehci, struct ehci_qtd *qtd);
25 /*-------------------------------------------------------------------------*/
28 * EHCI Root Hub ... the nonsharable stuff
30 * Registers don't need cpu_to_le32, that happens transparently
33 /*-------------------------------------------------------------------------*/
35 #define PORT_WAKE_BITS (PORT_WKOC_E|PORT_WKDISC_E|PORT_WKCONN_E)
37 #ifdef CONFIG_PM
39 static int ehci_hub_control(
40 struct usb_hcd *hcd,
41 u16 typeReq,
42 u16 wValue,
43 u16 wIndex,
44 char *buf,
45 u16 wLength
48 /* After a power loss, ports that were owned by the companion must be
49 * reset so that the companion can still own them.
51 static void ehci_handover_companion_ports(struct ehci_hcd *ehci)
53 u32 __iomem *reg;
54 u32 status;
55 int port;
56 __le32 buf;
57 struct usb_hcd *hcd = ehci_to_hcd(ehci);
59 if (!ehci->owned_ports)
60 return;
62 /* Give the connections some time to appear */
63 msleep(20);
65 port = HCS_N_PORTS(ehci->hcs_params);
66 while (port--) {
67 if (test_bit(port, &ehci->owned_ports)) {
68 reg = &ehci->regs->port_status[port];
69 status = ehci_readl(ehci, reg) & ~PORT_RWC_BITS;
71 /* Port already owned by companion? */
72 if (status & PORT_OWNER)
73 clear_bit(port, &ehci->owned_ports);
74 else if (test_bit(port, &ehci->companion_ports))
75 ehci_writel(ehci, status & ~PORT_PE, reg);
76 else
77 ehci_hub_control(hcd, SetPortFeature,
78 USB_PORT_FEAT_RESET, port + 1,
79 NULL, 0);
83 if (!ehci->owned_ports)
84 return;
85 msleep(90); /* Wait for resets to complete */
87 port = HCS_N_PORTS(ehci->hcs_params);
88 while (port--) {
89 if (test_bit(port, &ehci->owned_ports)) {
90 ehci_hub_control(hcd, GetPortStatus,
91 0, port + 1,
92 (char *) &buf, sizeof(buf));
94 /* The companion should now own the port,
95 * but if something went wrong the port must not
96 * remain enabled.
98 reg = &ehci->regs->port_status[port];
99 status = ehci_readl(ehci, reg) & ~PORT_RWC_BITS;
100 if (status & PORT_OWNER)
101 ehci_writel(ehci, status | PORT_CSC, reg);
102 else {
103 ehci_dbg(ehci, "failed handover port %d: %x\n",
104 port + 1, status);
105 ehci_writel(ehci, status & ~PORT_PE, reg);
110 ehci->owned_ports = 0;
113 static int ehci_bus_suspend (struct usb_hcd *hcd)
115 struct ehci_hcd *ehci = hcd_to_ehci (hcd);
116 int port;
117 int mask;
119 ehci_dbg(ehci, "suspend root hub\n");
121 if (time_before (jiffies, ehci->next_statechange))
122 msleep(5);
123 del_timer_sync(&ehci->watchdog);
124 del_timer_sync(&ehci->iaa_watchdog);
126 spin_lock_irq (&ehci->lock);
128 /* Once the controller is stopped, port resumes that are already
129 * in progress won't complete. Hence if remote wakeup is enabled
130 * for the root hub and any ports are in the middle of a resume or
131 * remote wakeup, we must fail the suspend.
133 if (hcd->self.root_hub->do_remote_wakeup) {
134 port = HCS_N_PORTS(ehci->hcs_params);
135 while (port--) {
136 if (ehci->reset_done[port] != 0) {
137 spin_unlock_irq(&ehci->lock);
138 ehci_dbg(ehci, "suspend failed because "
139 "port %d is resuming\n",
140 port + 1);
141 return -EBUSY;
146 /* stop schedules, clean any completed work */
147 if (HC_IS_RUNNING(hcd->state)) {
148 ehci_quiesce (ehci);
149 hcd->state = HC_STATE_QUIESCING;
151 ehci->command = ehci_readl(ehci, &ehci->regs->command);
152 ehci_work(ehci);
154 /* Unlike other USB host controller types, EHCI doesn't have
155 * any notion of "global" or bus-wide suspend. The driver has
156 * to manually suspend all the active unsuspended ports, and
157 * then manually resume them in the bus_resume() routine.
159 ehci->bus_suspended = 0;
160 ehci->owned_ports = 0;
161 port = HCS_N_PORTS(ehci->hcs_params);
162 while (port--) {
163 u32 __iomem *reg = &ehci->regs->port_status [port];
164 u32 t1 = ehci_readl(ehci, reg) & ~PORT_RWC_BITS;
165 u32 t2 = t1;
167 /* keep track of which ports we suspend */
168 if (t1 & PORT_OWNER)
169 set_bit(port, &ehci->owned_ports);
170 else if ((t1 & PORT_PE) && !(t1 & PORT_SUSPEND)) {
171 t2 |= PORT_SUSPEND;
172 set_bit(port, &ehci->bus_suspended);
175 /* enable remote wakeup on all ports */
176 if (hcd->self.root_hub->do_remote_wakeup)
177 t2 |= PORT_WAKE_BITS;
178 else
179 t2 &= ~PORT_WAKE_BITS;
181 if (t1 != t2) {
182 ehci_vdbg (ehci, "port %d, %08x -> %08x\n",
183 port + 1, t1, t2);
184 ehci_writel(ehci, t2, reg);
188 /* Apparently some devices need a >= 1-uframe delay here */
189 if (ehci->bus_suspended)
190 udelay(150);
192 /* turn off now-idle HC */
193 ehci_halt (ehci);
194 hcd->state = HC_STATE_SUSPENDED;
196 if (ehci->reclaim)
197 end_unlink_async(ehci);
199 /* allow remote wakeup */
200 mask = INTR_MASK;
201 if (!hcd->self.root_hub->do_remote_wakeup)
202 mask &= ~STS_PCD;
203 ehci_writel(ehci, mask, &ehci->regs->intr_enable);
204 ehci_readl(ehci, &ehci->regs->intr_enable);
206 ehci->next_statechange = jiffies + msecs_to_jiffies(10);
207 spin_unlock_irq (&ehci->lock);
209 /* ehci_work() may have re-enabled the watchdog timer, which we do not
210 * want, and so we must delete any pending watchdog timer events.
212 del_timer_sync(&ehci->watchdog);
213 return 0;
217 /* caller has locked the root hub, and should reset/reinit on error */
218 static int ehci_bus_resume (struct usb_hcd *hcd)
220 struct ehci_hcd *ehci = hcd_to_ehci (hcd);
221 u32 temp;
222 u32 power_okay;
223 int i;
224 u8 resume_needed = 0;
226 if (time_before (jiffies, ehci->next_statechange))
227 msleep(5);
228 spin_lock_irq (&ehci->lock);
229 if (!test_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags)) {
230 spin_unlock_irq(&ehci->lock);
231 return -ESHUTDOWN;
234 /* Ideally and we've got a real resume here, and no port's power
235 * was lost. (For PCI, that means Vaux was maintained.) But we
236 * could instead be restoring a swsusp snapshot -- so that BIOS was
237 * the last user of the controller, not reset/pm hardware keeping
238 * state we gave to it.
240 power_okay = ehci_readl(ehci, &ehci->regs->intr_enable);
241 ehci_dbg(ehci, "resume root hub%s\n",
242 power_okay ? "" : " after power loss");
244 /* at least some APM implementations will try to deliver
245 * IRQs right away, so delay them until we're ready.
247 ehci_writel(ehci, 0, &ehci->regs->intr_enable);
249 /* re-init operational registers */
250 ehci_writel(ehci, 0, &ehci->regs->segment);
251 ehci_writel(ehci, ehci->periodic_dma, &ehci->regs->frame_list);
252 ehci_writel(ehci, (u32) ehci->async->qh_dma, &ehci->regs->async_next);
254 /* restore CMD_RUN, framelist size, and irq threshold */
255 ehci_writel(ehci, ehci->command, &ehci->regs->command);
257 /* Some controller/firmware combinations need a delay during which
258 * they set up the port statuses. See Bugzilla #8190. */
259 spin_unlock_irq(&ehci->lock);
260 msleep(8);
261 spin_lock_irq(&ehci->lock);
263 /* manually resume the ports we suspended during bus_suspend() */
264 i = HCS_N_PORTS (ehci->hcs_params);
265 while (i--) {
266 temp = ehci_readl(ehci, &ehci->regs->port_status [i]);
267 temp &= ~(PORT_RWC_BITS | PORT_WAKE_BITS);
268 if (test_bit(i, &ehci->bus_suspended) &&
269 (temp & PORT_SUSPEND)) {
270 temp |= PORT_RESUME;
271 resume_needed = 1;
273 ehci_writel(ehci, temp, &ehci->regs->port_status [i]);
276 /* msleep for 20ms only if code is trying to resume port */
277 if (resume_needed) {
278 spin_unlock_irq(&ehci->lock);
279 msleep(20);
280 spin_lock_irq(&ehci->lock);
283 i = HCS_N_PORTS (ehci->hcs_params);
284 while (i--) {
285 temp = ehci_readl(ehci, &ehci->regs->port_status [i]);
286 if (test_bit(i, &ehci->bus_suspended) &&
287 (temp & PORT_SUSPEND)) {
288 temp &= ~(PORT_RWC_BITS | PORT_RESUME);
289 ehci_writel(ehci, temp, &ehci->regs->port_status [i]);
290 ehci_vdbg (ehci, "resumed port %d\n", i + 1);
293 (void) ehci_readl(ehci, &ehci->regs->command);
295 /* maybe re-activate the schedule(s) */
296 temp = 0;
297 if (ehci->async->qh_next.qh)
298 temp |= CMD_ASE;
299 if (ehci->periodic_sched)
300 temp |= CMD_PSE;
301 if (temp) {
302 ehci->command |= temp;
303 ehci_writel(ehci, ehci->command, &ehci->regs->command);
306 ehci->next_statechange = jiffies + msecs_to_jiffies(5);
307 hcd->state = HC_STATE_RUNNING;
309 /* Now we can safely re-enable irqs */
310 ehci_writel(ehci, INTR_MASK, &ehci->regs->intr_enable);
312 spin_unlock_irq (&ehci->lock);
313 ehci_handover_companion_ports(ehci);
314 return 0;
317 #else
319 #define ehci_bus_suspend NULL
320 #define ehci_bus_resume NULL
322 #endif /* CONFIG_PM */
324 /*-------------------------------------------------------------------------*/
326 /* Display the ports dedicated to the companion controller */
327 static ssize_t show_companion(struct device *dev,
328 struct device_attribute *attr,
329 char *buf)
331 struct ehci_hcd *ehci;
332 int nports, index, n;
333 int count = PAGE_SIZE;
334 char *ptr = buf;
336 ehci = hcd_to_ehci(bus_to_hcd(dev_get_drvdata(dev)));
337 nports = HCS_N_PORTS(ehci->hcs_params);
339 for (index = 0; index < nports; ++index) {
340 if (test_bit(index, &ehci->companion_ports)) {
341 n = scnprintf(ptr, count, "%d\n", index + 1);
342 ptr += n;
343 count -= n;
346 return ptr - buf;
350 * Sets the owner of a port
352 static void set_owner(struct ehci_hcd *ehci, int portnum, int new_owner)
354 u32 __iomem *status_reg;
355 u32 port_status;
356 int try;
358 status_reg = &ehci->regs->port_status[portnum];
361 * The controller won't set the OWNER bit if the port is
362 * enabled, so this loop will sometimes require at least two
363 * iterations: one to disable the port and one to set OWNER.
365 for (try = 4; try > 0; --try) {
366 spin_lock_irq(&ehci->lock);
367 port_status = ehci_readl(ehci, status_reg);
368 if ((port_status & PORT_OWNER) == new_owner
369 || (port_status & (PORT_OWNER | PORT_CONNECT))
370 == 0)
371 try = 0;
372 else {
373 port_status ^= PORT_OWNER;
374 port_status &= ~(PORT_PE | PORT_RWC_BITS);
375 ehci_writel(ehci, port_status, status_reg);
377 spin_unlock_irq(&ehci->lock);
378 if (try > 1)
379 msleep(5);
384 * Dedicate or undedicate a port to the companion controller.
385 * Syntax is "[-]portnum", where a leading '-' sign means
386 * return control of the port to the EHCI controller.
388 static ssize_t store_companion(struct device *dev,
389 struct device_attribute *attr,
390 const char *buf, size_t count)
392 struct ehci_hcd *ehci;
393 int portnum, new_owner;
395 ehci = hcd_to_ehci(bus_to_hcd(dev_get_drvdata(dev)));
396 new_owner = PORT_OWNER; /* Owned by companion */
397 if (sscanf(buf, "%d", &portnum) != 1)
398 return -EINVAL;
399 if (portnum < 0) {
400 portnum = - portnum;
401 new_owner = 0; /* Owned by EHCI */
403 if (portnum <= 0 || portnum > HCS_N_PORTS(ehci->hcs_params))
404 return -ENOENT;
405 portnum--;
406 if (new_owner)
407 set_bit(portnum, &ehci->companion_ports);
408 else
409 clear_bit(portnum, &ehci->companion_ports);
410 set_owner(ehci, portnum, new_owner);
411 return count;
413 static DEVICE_ATTR(companion, 0644, show_companion, store_companion);
415 static inline void create_companion_file(struct ehci_hcd *ehci)
417 int i;
419 /* with integrated TT there is no companion! */
420 if (!ehci_is_TDI(ehci))
421 i = device_create_file(ehci_to_hcd(ehci)->self.controller,
422 &dev_attr_companion);
425 static inline void remove_companion_file(struct ehci_hcd *ehci)
427 /* with integrated TT there is no companion! */
428 if (!ehci_is_TDI(ehci))
429 device_remove_file(ehci_to_hcd(ehci)->self.controller,
430 &dev_attr_companion);
434 /*-------------------------------------------------------------------------*/
436 static int check_reset_complete (
437 struct ehci_hcd *ehci,
438 int index,
439 u32 __iomem *status_reg,
440 int port_status
442 if (!(port_status & PORT_CONNECT))
443 return port_status;
445 /* if reset finished and it's still not enabled -- handoff */
446 if (!(port_status & PORT_PE)) {
448 /* with integrated TT, there's nobody to hand it to! */
449 if (ehci_is_TDI(ehci)) {
450 ehci_dbg (ehci,
451 "Failed to enable port %d on root hub TT\n",
452 index+1);
453 return port_status;
456 ehci_dbg (ehci, "port %d full speed --> companion\n",
457 index + 1);
459 // what happens if HCS_N_CC(params) == 0 ?
460 port_status |= PORT_OWNER;
461 port_status &= ~PORT_RWC_BITS;
462 ehci_writel(ehci, port_status, status_reg);
464 } else
465 ehci_dbg (ehci, "port %d high speed\n", index + 1);
467 return port_status;
470 /*-------------------------------------------------------------------------*/
473 /* build "status change" packet (one or two bytes) from HC registers */
475 static int
476 ehci_hub_status_data (struct usb_hcd *hcd, char *buf)
478 struct ehci_hcd *ehci = hcd_to_ehci (hcd);
479 u32 temp, status = 0;
480 u32 mask;
481 int ports, i, retval = 1;
482 unsigned long flags;
484 /* if !USB_SUSPEND, root hub timers won't get shut down ... */
485 if (!HC_IS_RUNNING(hcd->state))
486 return 0;
488 /* init status to no-changes */
489 buf [0] = 0;
490 ports = HCS_N_PORTS (ehci->hcs_params);
491 if (ports > 7) {
492 buf [1] = 0;
493 retval++;
496 /* Some boards (mostly VIA?) report bogus overcurrent indications,
497 * causing massive log spam unless we completely ignore them. It
498 * may be relevant that VIA VT8235 controlers, where PORT_POWER is
499 * always set, seem to clear PORT_OCC and PORT_CSC when writing to
500 * PORT_POWER; that's surprising, but maybe within-spec.
502 if (!ignore_oc)
503 mask = PORT_CSC | PORT_PEC | PORT_OCC;
504 else
505 mask = PORT_CSC | PORT_PEC;
506 // PORT_RESUME from hardware ~= PORT_STAT_C_SUSPEND
508 /* no hub change reports (bit 0) for now (power, ...) */
510 /* port N changes (bit N)? */
511 spin_lock_irqsave (&ehci->lock, flags);
512 for (i = 0; i < ports; i++) {
513 temp = ehci_readl(ehci, &ehci->regs->port_status [i]);
516 * Return status information even for ports with OWNER set.
517 * Otherwise khubd wouldn't see the disconnect event when a
518 * high-speed device is switched over to the companion
519 * controller by the user.
522 if ((temp & mask) != 0 || test_bit(i, &ehci->port_c_suspend)
523 || (ehci->reset_done[i] && time_after_eq(
524 jiffies, ehci->reset_done[i]))) {
525 if (i < 7)
526 buf [0] |= 1 << (i + 1);
527 else
528 buf [1] |= 1 << (i - 7);
529 status = STS_PCD;
532 /* FIXME autosuspend idle root hubs */
533 spin_unlock_irqrestore (&ehci->lock, flags);
534 return status ? retval : 0;
537 /*-------------------------------------------------------------------------*/
539 static void
540 ehci_hub_descriptor (
541 struct ehci_hcd *ehci,
542 struct usb_hub_descriptor *desc
544 int ports = HCS_N_PORTS (ehci->hcs_params);
545 u16 temp;
547 desc->bDescriptorType = 0x29;
548 desc->bPwrOn2PwrGood = 10; /* ehci 1.0, 2.3.9 says 20ms max */
549 desc->bHubContrCurrent = 0;
551 desc->bNbrPorts = ports;
552 temp = 1 + (ports / 8);
553 desc->bDescLength = 7 + 2 * temp;
555 /* two bitmaps: ports removable, and usb 1.0 legacy PortPwrCtrlMask */
556 memset (&desc->bitmap [0], 0, temp);
557 memset (&desc->bitmap [temp], 0xff, temp);
559 temp = 0x0008; /* per-port overcurrent reporting */
560 if (HCS_PPC (ehci->hcs_params))
561 temp |= 0x0001; /* per-port power control */
562 else
563 temp |= 0x0002; /* no power switching */
564 #if 0
565 // re-enable when we support USB_PORT_FEAT_INDICATOR below.
566 if (HCS_INDICATOR (ehci->hcs_params))
567 temp |= 0x0080; /* per-port indicators (LEDs) */
568 #endif
569 desc->wHubCharacteristics = cpu_to_le16(temp);
572 /*-------------------------------------------------------------------------*/
574 static int ehci_hub_control (
575 struct usb_hcd *hcd,
576 u16 typeReq,
577 u16 wValue,
578 u16 wIndex,
579 char *buf,
580 u16 wLength
582 struct ehci_hcd *ehci = hcd_to_ehci (hcd);
583 int ports = HCS_N_PORTS (ehci->hcs_params);
584 u32 __iomem *status_reg = &ehci->regs->port_status[
585 (wIndex & 0xff) - 1];
586 u32 temp, status;
587 u32 hcc_params;
588 unsigned long flags;
589 int retval = 0;
590 unsigned selector;
593 * FIXME: support SetPortFeatures USB_PORT_FEAT_INDICATOR.
594 * HCS_INDICATOR may say we can change LEDs to off/amber/green.
595 * (track current state ourselves) ... blink for diagnostics,
596 * power, "this is the one", etc. EHCI spec supports this.
599 spin_lock_irqsave (&ehci->lock, flags);
600 switch (typeReq) {
601 case ClearHubFeature:
602 switch (wValue) {
603 case C_HUB_LOCAL_POWER:
604 case C_HUB_OVER_CURRENT:
605 /* no hub-wide feature/status flags */
606 break;
607 default:
608 goto error;
610 break;
611 case ClearPortFeature:
612 if (!wIndex || wIndex > ports)
613 goto error;
614 wIndex--;
615 temp = ehci_readl(ehci, status_reg);
618 * Even if OWNER is set, so the port is owned by the
619 * companion controller, khubd needs to be able to clear
620 * the port-change status bits (especially
621 * USB_PORT_FEAT_C_CONNECTION).
624 switch (wValue) {
625 case USB_PORT_FEAT_ENABLE:
626 ehci_writel(ehci, temp & ~PORT_PE, status_reg);
627 break;
628 case USB_PORT_FEAT_C_ENABLE:
629 ehci_writel(ehci, (temp & ~PORT_RWC_BITS) | PORT_PEC,
630 status_reg);
631 break;
632 case USB_PORT_FEAT_SUSPEND:
633 if (temp & PORT_RESET)
634 goto error;
635 if (ehci->no_selective_suspend)
636 break;
637 if (temp & PORT_SUSPEND) {
638 if ((temp & PORT_PE) == 0)
639 goto error;
640 /* resume signaling for 20 msec */
641 temp &= ~(PORT_RWC_BITS | PORT_WAKE_BITS);
642 ehci_writel(ehci, temp | PORT_RESUME,
643 status_reg);
644 ehci->reset_done [wIndex] = jiffies
645 + msecs_to_jiffies (20);
647 break;
648 case USB_PORT_FEAT_C_SUSPEND:
649 clear_bit(wIndex, &ehci->port_c_suspend);
650 break;
651 case USB_PORT_FEAT_POWER:
652 if (HCS_PPC (ehci->hcs_params))
653 ehci_writel(ehci,
654 temp & ~(PORT_RWC_BITS | PORT_POWER),
655 status_reg);
656 break;
657 case USB_PORT_FEAT_C_CONNECTION:
658 ehci_writel(ehci, (temp & ~PORT_RWC_BITS) | PORT_CSC,
659 status_reg);
660 break;
661 case USB_PORT_FEAT_C_OVER_CURRENT:
662 ehci_writel(ehci, (temp & ~PORT_RWC_BITS) | PORT_OCC,
663 status_reg);
664 break;
665 case USB_PORT_FEAT_C_RESET:
666 /* GetPortStatus clears reset */
667 break;
668 default:
669 goto error;
671 ehci_readl(ehci, &ehci->regs->command); /* unblock posted write */
672 break;
673 case GetHubDescriptor:
674 ehci_hub_descriptor (ehci, (struct usb_hub_descriptor *)
675 buf);
676 break;
677 case GetHubStatus:
678 /* no hub-wide feature/status flags */
679 memset (buf, 0, 4);
680 //cpu_to_le32s ((u32 *) buf);
681 break;
682 case GetPortStatus:
683 if (!wIndex || wIndex > ports)
684 goto error;
685 wIndex--;
686 status = 0;
687 temp = ehci_readl(ehci, status_reg);
689 // wPortChange bits
690 if (temp & PORT_CSC)
691 status |= 1 << USB_PORT_FEAT_C_CONNECTION;
692 if (temp & PORT_PEC)
693 status |= 1 << USB_PORT_FEAT_C_ENABLE;
695 if ((temp & PORT_OCC) && !ignore_oc){
696 status |= 1 << USB_PORT_FEAT_C_OVER_CURRENT;
699 * Hubs should disable port power on over-current.
700 * However, not all EHCI implementations do this
701 * automatically, even if they _do_ support per-port
702 * power switching; they're allowed to just limit the
703 * current. khubd will turn the power back on.
705 if (HCS_PPC (ehci->hcs_params)){
706 ehci_writel(ehci,
707 temp & ~(PORT_RWC_BITS | PORT_POWER),
708 status_reg);
712 /* whoever resumes must GetPortStatus to complete it!! */
713 if (temp & PORT_RESUME) {
715 /* Remote Wakeup received? */
716 if (!ehci->reset_done[wIndex]) {
717 /* resume signaling for 20 msec */
718 ehci->reset_done[wIndex] = jiffies
719 + msecs_to_jiffies(20);
720 /* check the port again */
721 mod_timer(&ehci_to_hcd(ehci)->rh_timer,
722 ehci->reset_done[wIndex]);
725 /* resume completed? */
726 else if (time_after_eq(jiffies,
727 ehci->reset_done[wIndex])) {
728 clear_bit(wIndex, &ehci->suspended_ports);
729 set_bit(wIndex, &ehci->port_c_suspend);
730 ehci->reset_done[wIndex] = 0;
732 /* stop resume signaling */
733 temp = ehci_readl(ehci, status_reg);
734 ehci_writel(ehci,
735 temp & ~(PORT_RWC_BITS | PORT_RESUME),
736 status_reg);
737 retval = handshake(ehci, status_reg,
738 PORT_RESUME, 0, 2000 /* 2msec */);
739 if (retval != 0) {
740 ehci_err(ehci,
741 "port %d resume error %d\n",
742 wIndex + 1, retval);
743 goto error;
745 temp &= ~(PORT_SUSPEND|PORT_RESUME|(3<<10));
749 /* whoever resets must GetPortStatus to complete it!! */
750 if ((temp & PORT_RESET)
751 && time_after_eq(jiffies,
752 ehci->reset_done[wIndex])) {
753 status |= 1 << USB_PORT_FEAT_C_RESET;
754 ehci->reset_done [wIndex] = 0;
756 /* force reset to complete */
757 ehci_writel(ehci, temp & ~(PORT_RWC_BITS | PORT_RESET),
758 status_reg);
759 /* REVISIT: some hardware needs 550+ usec to clear
760 * this bit; seems too long to spin routinely...
762 retval = handshake(ehci, status_reg,
763 PORT_RESET, 0, 1000);
764 if (retval != 0) {
765 ehci_err (ehci, "port %d reset error %d\n",
766 wIndex + 1, retval);
767 goto error;
770 /* see what we found out */
771 temp = check_reset_complete (ehci, wIndex, status_reg,
772 ehci_readl(ehci, status_reg));
775 if (!(temp & (PORT_RESUME|PORT_RESET)))
776 ehci->reset_done[wIndex] = 0;
778 /* transfer dedicated ports to the companion hc */
779 if ((temp & PORT_CONNECT) &&
780 test_bit(wIndex, &ehci->companion_ports)) {
781 temp &= ~PORT_RWC_BITS;
782 temp |= PORT_OWNER;
783 ehci_writel(ehci, temp, status_reg);
784 ehci_dbg(ehci, "port %d --> companion\n", wIndex + 1);
785 temp = ehci_readl(ehci, status_reg);
789 * Even if OWNER is set, there's no harm letting khubd
790 * see the wPortStatus values (they should all be 0 except
791 * for PORT_POWER anyway).
794 if (temp & PORT_CONNECT) {
795 status |= 1 << USB_PORT_FEAT_CONNECTION;
796 // status may be from integrated TT
797 status |= ehci_port_speed(ehci, temp);
799 if (temp & PORT_PE)
800 status |= 1 << USB_PORT_FEAT_ENABLE;
802 /* maybe the port was unsuspended without our knowledge */
803 if (temp & (PORT_SUSPEND|PORT_RESUME)) {
804 status |= 1 << USB_PORT_FEAT_SUSPEND;
805 } else if (test_bit(wIndex, &ehci->suspended_ports)) {
806 clear_bit(wIndex, &ehci->suspended_ports);
807 ehci->reset_done[wIndex] = 0;
808 if (temp & PORT_PE)
809 set_bit(wIndex, &ehci->port_c_suspend);
812 if (temp & PORT_OC)
813 status |= 1 << USB_PORT_FEAT_OVER_CURRENT;
814 if (temp & PORT_RESET)
815 status |= 1 << USB_PORT_FEAT_RESET;
816 if (temp & PORT_POWER)
817 status |= 1 << USB_PORT_FEAT_POWER;
818 if (test_bit(wIndex, &ehci->port_c_suspend))
819 status |= 1 << USB_PORT_FEAT_C_SUSPEND;
821 #ifndef VERBOSE_DEBUG
822 if (status & ~0xffff) /* only if wPortChange is interesting */
823 #endif
824 dbg_port (ehci, "GetStatus", wIndex + 1, temp);
825 put_unaligned(cpu_to_le32 (status), (__le32 *) buf);
826 break;
827 case SetHubFeature:
828 switch (wValue) {
829 case C_HUB_LOCAL_POWER:
830 case C_HUB_OVER_CURRENT:
831 /* no hub-wide feature/status flags */
832 break;
833 default:
834 goto error;
836 break;
837 case SetPortFeature:
838 selector = wIndex >> 8;
839 wIndex &= 0xff;
840 if (!wIndex || wIndex > ports)
841 goto error;
842 wIndex--;
843 temp = ehci_readl(ehci, status_reg);
844 if (temp & PORT_OWNER)
845 break;
847 temp &= ~PORT_RWC_BITS;
848 switch (wValue) {
849 case USB_PORT_FEAT_SUSPEND:
850 if (ehci->no_selective_suspend)
851 break;
852 if ((temp & PORT_PE) == 0
853 || (temp & PORT_RESET) != 0)
854 goto error;
855 ehci_writel(ehci, temp | PORT_SUSPEND, status_reg);
856 set_bit(wIndex, &ehci->suspended_ports);
857 break;
858 case USB_PORT_FEAT_POWER:
859 if (HCS_PPC (ehci->hcs_params))
860 ehci_writel(ehci, temp | PORT_POWER,
861 status_reg);
862 break;
863 case USB_PORT_FEAT_RESET:
864 if (temp & PORT_RESUME)
865 goto error;
866 /* line status bits may report this as low speed,
867 * which can be fine if this root hub has a
868 * transaction translator built in.
870 if ((temp & (PORT_PE|PORT_CONNECT)) == PORT_CONNECT
871 && !ehci_is_TDI(ehci)
872 && PORT_USB11 (temp)) {
873 ehci_dbg (ehci,
874 "port %d low speed --> companion\n",
875 wIndex + 1);
876 temp |= PORT_OWNER;
877 } else {
878 ehci_vdbg (ehci, "port %d reset\n", wIndex + 1);
879 temp |= PORT_RESET;
880 temp &= ~PORT_PE;
883 * caller must wait, then call GetPortStatus
884 * usb 2.0 spec says 50 ms resets on root
886 ehci->reset_done [wIndex] = jiffies
887 + msecs_to_jiffies (50);
889 ehci_writel(ehci, temp, status_reg);
890 break;
892 /* For downstream facing ports (these): one hub port is put
893 * into test mode according to USB2 11.24.2.13, then the hub
894 * must be reset (which for root hub now means rmmod+modprobe,
895 * or else system reboot). See EHCI 2.3.9 and 4.14 for info
896 * about the EHCI-specific stuff.
898 case USB_PORT_FEAT_TEST:
899 if (!selector || selector > 5)
900 goto error;
901 ehci_quiesce(ehci);
902 ehci_halt(ehci);
903 temp |= selector << 16;
904 ehci_writel(ehci, temp, status_reg);
905 break;
907 default:
908 goto error;
910 ehci_readl(ehci, &ehci->regs->command); /* unblock posted writes */
911 break;
913 /* EHCI fastpath acceleration */
914 /* Use parameters instead of buffer to avoid core modifications */
915 case EHCI_SET_BYPASS_CB:
916 ehci->ehci_bypass_callback = (void (*)(int, struct ehci_qh *, spinlock_t *lock))(wValue | (wIndex << 16));
917 ehci_info(ehci, "EHCI Fastpath: Got the bypass callback command in EHCI %p\n", ehci->ehci_bypass_callback);
918 break;
919 case EHCI_SET_BYPASS_DEV:
920 hcc_params = ehci_readl(ehci, &ehci->caps->hcc_params);
921 if (HCC_64BIT_ADDR (hcc_params)) {
922 ehci_info(ehci, "EHCI Fastpath: EHCI 64bit addressing mode not supported\n");
923 goto error;
925 ehci->bypass_device = (struct usb_device *)(wValue | (wIndex << 16));
926 ehci_info(ehci, "EHCI Fastpath: Got the bypass device command in EHCI %p\n", ehci->bypass_device);
927 break;
928 case EHCI_SET_EP_BYPASS:
930 struct urb dummy_urb;
931 /* In/out bit mask is 0x10 */
932 int pipe = wValue;
933 int pipeindex = wIndex;
934 struct usb_device *udev = ehci->bypass_device;
935 struct ehci_qh *qh;
936 int usb_pipe;
937 bool pipeout = ((pipe&0x10) == 0);
939 struct usb_host_endpoint *ep;
941 ehci_info(ehci, "EHCI Fastpath: Got the RH bypass command in EHCI for EP #%d %04x\n", pipeindex, pipe);
943 pipe &= 0xf;
944 ep = pipeout ? udev->ep_out[pipe] : udev->ep_in[pipe];
945 usb_pipe = pipeout ? usb_sndbulkpipe(udev, pipe) : usb_rcvbulkpipe(udev, pipe);
947 usb_fill_bulk_urb(&dummy_urb, ehci->bypass_device, usb_pipe, NULL, 0, NULL, NULL);
949 qh = qh_make(ehci, &dummy_urb, GFP_ATOMIC);
951 if (ehci->fastpath_pool != NULL) {
952 dma_addr_t dma;
954 ehci_info(ehci, "EHCI Fastpath: release native dummy qtd and alloc new one\n");
955 ehci_qtd_free (ehci, qh->dummy);
957 qh->dummy = dma_pool_alloc (ehci->fastpath_pool, GFP_ATOMIC, &dma);
958 if (qh->dummy != NULL)
959 ehci_qtd_init (ehci, qh->dummy, dma);
960 else
961 printk("re-alloc dummy qtd from fastpath_pool error !!\n");
964 ep->hcpriv = qh;
965 qh->first_qtd = qh->dummy;
967 ehci_info(ehci, "EHCI Fastpath: EP %p QH %p dummy %p\n", ep, qh, qh->dummy);
969 /* Start the QH */
970 qh_link_async (ehci, qh);
972 /* Detach the QH */
973 qh->qh_state = QH_STATE_DETACHED;
975 /* Save the pipe pointer */
976 ehci->ehci_pipes[pipeindex] = qh;
979 break;
981 case EHCI_DUMP_STATE:
983 int sts = ehci_readl(ehci, &ehci->regs->status);
985 printk("EHCI IRQ status %08x\n", sts);
986 printk("EHCI INT status %08x\n", ehci->regs->intr_enable);
988 break;
990 case EHCI_SET_BYPASS_POOL:
991 ehci->fastpath_pool= (struct dma_pool *)(wValue | (wIndex << 16));
992 ehci_info(ehci, "EHCI Fastpath: Got the bypass fastpath pool command in EHCI %p\n", ehci->fastpath_pool);
993 break;
995 case EHCI_CLR_EP_BYPASS:
997 int idx;
999 /* reset callback func and bapass dev */
1000 ehci->ehci_bypass_callback = NULL;
1001 ehci->bypass_device = NULL;
1003 /* freeing qh and qtd info */
1004 for (idx = 0; idx < 3; idx++) {
1005 printk("%d freeing \n", idx);
1006 if (ehci->ehci_pipes[idx] != NULL) {
1007 struct ehci_qh *rm_qh = ehci->ehci_pipes[idx];
1008 struct ehci_qtd *rm_qtd= ehci->ehci_pipes[idx]->dummy;
1010 dma_pool_free(ehci->fastpath_pool, rm_qtd, rm_qtd->qtd_dma);
1011 dma_pool_free(ehci->qh_pool, rm_qh, rm_qh->qh_dma);
1012 ehci->ehci_pipes[idx] = NULL;
1016 break;
1017 default:
1018 error:
1019 /* "stall" on error */
1020 retval = -EPIPE;
1022 spin_unlock_irqrestore (&ehci->lock, flags);
1023 return retval;
1026 static void ehci_relinquish_port(struct usb_hcd *hcd, int portnum)
1028 struct ehci_hcd *ehci = hcd_to_ehci(hcd);
1030 if (ehci_is_TDI(ehci))
1031 return;
1032 set_owner(ehci, --portnum, PORT_OWNER);
1035 static int ehci_port_handed_over(struct usb_hcd *hcd, int portnum)
1037 struct ehci_hcd *ehci = hcd_to_ehci(hcd);
1038 u32 __iomem *reg;
1040 if (ehci_is_TDI(ehci))
1041 return 0;
1042 reg = &ehci->regs->port_status[portnum - 1];
1043 return ehci_readl(ehci, reg) & PORT_OWNER;