6fef1ee7d1010e9cc223290e73054cc0f60ccdc0
[safe/jmp/linux-2.6] / drivers / usb / host / ehci-hub.c
1 /*
2  * Copyright (C) 2001-2004 by David Brownell
3  *
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.
8  *
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.
13  *
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.
17  */
18
19 /* this file is part of ehci-hcd.c */
20
21 /*-------------------------------------------------------------------------*/
22
23 /*
24  * EHCI Root Hub ... the nonsharable stuff
25  *
26  * Registers don't need cpu_to_le32, that happens transparently
27  */
28
29 /*-------------------------------------------------------------------------*/
30
31 #define PORT_WAKE_BITS  (PORT_WKOC_E|PORT_WKDISC_E|PORT_WKCONN_E)
32
33 #ifdef  CONFIG_PM
34
35 static int ehci_hub_control(
36         struct usb_hcd  *hcd,
37         u16             typeReq,
38         u16             wValue,
39         u16             wIndex,
40         char            *buf,
41         u16             wLength
42 );
43
44 /* After a power loss, ports that were owned by the companion must be
45  * reset so that the companion can still own them.
46  */
47 static void ehci_handover_companion_ports(struct ehci_hcd *ehci)
48 {
49         u32 __iomem     *reg;
50         u32             status;
51         int             port;
52         __le32          buf;
53         struct usb_hcd  *hcd = ehci_to_hcd(ehci);
54
55         if (!ehci->owned_ports)
56                 return;
57
58         /* Give the connections some time to appear */
59         msleep(20);
60
61         port = HCS_N_PORTS(ehci->hcs_params);
62         while (port--) {
63                 if (test_bit(port, &ehci->owned_ports)) {
64                         reg = &ehci->regs->port_status[port];
65                         status = ehci_readl(ehci, reg) & ~PORT_RWC_BITS;
66
67                         /* Port already owned by companion? */
68                         if (status & PORT_OWNER)
69                                 clear_bit(port, &ehci->owned_ports);
70                         else if (test_bit(port, &ehci->companion_ports))
71                                 ehci_writel(ehci, status & ~PORT_PE, reg);
72                         else
73                                 ehci_hub_control(hcd, SetPortFeature,
74                                                 USB_PORT_FEAT_RESET, port + 1,
75                                                 NULL, 0);
76                 }
77         }
78
79         if (!ehci->owned_ports)
80                 return;
81         msleep(90);             /* Wait for resets to complete */
82
83         port = HCS_N_PORTS(ehci->hcs_params);
84         while (port--) {
85                 if (test_bit(port, &ehci->owned_ports)) {
86                         ehci_hub_control(hcd, GetPortStatus,
87                                         0, port + 1,
88                                         (char *) &buf, sizeof(buf));
89
90                         /* The companion should now own the port,
91                          * but if something went wrong the port must not
92                          * remain enabled.
93                          */
94                         reg = &ehci->regs->port_status[port];
95                         status = ehci_readl(ehci, reg) & ~PORT_RWC_BITS;
96                         if (status & PORT_OWNER)
97                                 ehci_writel(ehci, status | PORT_CSC, reg);
98                         else {
99                                 ehci_dbg(ehci, "failed handover port %d: %x\n",
100                                                 port + 1, status);
101                                 ehci_writel(ehci, status & ~PORT_PE, reg);
102                         }
103                 }
104         }
105
106         ehci->owned_ports = 0;
107 }
108
109 static int ehci_bus_suspend (struct usb_hcd *hcd)
110 {
111         struct ehci_hcd         *ehci = hcd_to_ehci (hcd);
112         int                     port;
113         int                     mask;
114         u32 __iomem             *hostpc_reg = NULL;
115
116         ehci_dbg(ehci, "suspend root hub\n");
117
118         if (time_before (jiffies, ehci->next_statechange))
119                 msleep(5);
120         del_timer_sync(&ehci->watchdog);
121         del_timer_sync(&ehci->iaa_watchdog);
122
123         port = HCS_N_PORTS (ehci->hcs_params);
124         spin_lock_irq (&ehci->lock);
125
126         /* stop schedules, clean any completed work */
127         if (HC_IS_RUNNING(hcd->state)) {
128                 ehci_quiesce (ehci);
129                 hcd->state = HC_STATE_QUIESCING;
130         }
131         ehci->command = ehci_readl(ehci, &ehci->regs->command);
132         ehci_work(ehci);
133
134         /* Unlike other USB host controller types, EHCI doesn't have
135          * any notion of "global" or bus-wide suspend.  The driver has
136          * to manually suspend all the active unsuspended ports, and
137          * then manually resume them in the bus_resume() routine.
138          */
139         ehci->bus_suspended = 0;
140         ehci->owned_ports = 0;
141         while (port--) {
142                 u32 __iomem     *reg = &ehci->regs->port_status [port];
143                 u32             t1 = ehci_readl(ehci, reg) & ~PORT_RWC_BITS;
144                 u32             t2 = t1;
145
146                 if (ehci->has_hostpc)
147                         hostpc_reg = (u32 __iomem *)((u8 *)ehci->regs
148                                 + HOSTPC0 + 4 * (port & 0xff));
149                 /* keep track of which ports we suspend */
150                 if (t1 & PORT_OWNER)
151                         set_bit(port, &ehci->owned_ports);
152                 else if ((t1 & PORT_PE) && !(t1 & PORT_SUSPEND)) {
153                         t2 |= PORT_SUSPEND;
154                         set_bit(port, &ehci->bus_suspended);
155                 }
156
157                 /* enable remote wakeup on all ports */
158                 if (hcd->self.root_hub->do_remote_wakeup) {
159                         /* only enable appropriate wake bits, otherwise the
160                          * hardware can not go phy low power mode. If a race
161                          * condition happens here(connection change during bits
162                          * set), the port change detection will finally fix it.
163                          */
164                         if (t1 & PORT_CONNECT) {
165                                 t2 |= PORT_WKOC_E | PORT_WKDISC_E;
166                                 t2 &= ~PORT_WKCONN_E;
167                         } else {
168                                 t2 |= PORT_WKOC_E | PORT_WKCONN_E;
169                                 t2 &= ~PORT_WKDISC_E;
170                         }
171                 } else
172                         t2 &= ~PORT_WAKE_BITS;
173
174                 if (t1 != t2) {
175                         ehci_vdbg (ehci, "port %d, %08x -> %08x\n",
176                                 port + 1, t1, t2);
177                         ehci_writel(ehci, t2, reg);
178                         if (hostpc_reg) {
179                                 u32     t3;
180
181                                 msleep(5);/* 5ms for HCD enter low pwr mode */
182                                 t3 = ehci_readl(ehci, hostpc_reg);
183                                 ehci_writel(ehci, t3 | HOSTPC_PHCD, hostpc_reg);
184                                 t3 = ehci_readl(ehci, hostpc_reg);
185                                 ehci_dbg(ehci, "Port%d phy low pwr mode %s\n",
186                                         port, (t3 & HOSTPC_PHCD) ?
187                                         "succeeded" : "failed");
188                         }
189                 }
190         }
191
192         /* Apparently some devices need a >= 1-uframe delay here */
193         if (ehci->bus_suspended)
194                 udelay(150);
195
196         /* turn off now-idle HC */
197         ehci_halt (ehci);
198         hcd->state = HC_STATE_SUSPENDED;
199
200         if (ehci->reclaim)
201                 end_unlink_async(ehci);
202
203         /* allow remote wakeup */
204         mask = INTR_MASK;
205         if (!hcd->self.root_hub->do_remote_wakeup)
206                 mask &= ~STS_PCD;
207         ehci_writel(ehci, mask, &ehci->regs->intr_enable);
208         ehci_readl(ehci, &ehci->regs->intr_enable);
209
210         ehci->next_statechange = jiffies + msecs_to_jiffies(10);
211         spin_unlock_irq (&ehci->lock);
212         return 0;
213 }
214
215
216 /* caller has locked the root hub, and should reset/reinit on error */
217 static int ehci_bus_resume (struct usb_hcd *hcd)
218 {
219         struct ehci_hcd         *ehci = hcd_to_ehci (hcd);
220         u32                     temp;
221         u32                     power_okay;
222         int                     i;
223         u8                      resume_needed = 0;
224
225         if (time_before (jiffies, ehci->next_statechange))
226                 msleep(5);
227         spin_lock_irq (&ehci->lock);
228         if (!test_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags)) {
229                 spin_unlock_irq(&ehci->lock);
230                 return -ESHUTDOWN;
231         }
232
233         /* Ideally and we've got a real resume here, and no port's power
234          * was lost.  (For PCI, that means Vaux was maintained.)  But we
235          * could instead be restoring a swsusp snapshot -- so that BIOS was
236          * the last user of the controller, not reset/pm hardware keeping
237          * state we gave to it.
238          */
239         power_okay = ehci_readl(ehci, &ehci->regs->intr_enable);
240         ehci_dbg(ehci, "resume root hub%s\n",
241                         power_okay ? "" : " after power loss");
242
243         /* at least some APM implementations will try to deliver
244          * IRQs right away, so delay them until we're ready.
245          */
246         ehci_writel(ehci, 0, &ehci->regs->intr_enable);
247
248         /* re-init operational registers */
249         ehci_writel(ehci, 0, &ehci->regs->segment);
250         ehci_writel(ehci, ehci->periodic_dma, &ehci->regs->frame_list);
251         ehci_writel(ehci, (u32) ehci->async->qh_dma, &ehci->regs->async_next);
252
253         /* restore CMD_RUN, framelist size, and irq threshold */
254         ehci_writel(ehci, ehci->command, &ehci->regs->command);
255
256         /* Some controller/firmware combinations need a delay during which
257          * they set up the port statuses.  See Bugzilla #8190. */
258         spin_unlock_irq(&ehci->lock);
259         msleep(8);
260         spin_lock_irq(&ehci->lock);
261
262         /* manually resume the ports we suspended during bus_suspend() */
263         i = HCS_N_PORTS (ehci->hcs_params);
264         while (i--) {
265                 temp = ehci_readl(ehci, &ehci->regs->port_status [i]);
266                 temp &= ~(PORT_RWC_BITS | PORT_WAKE_BITS);
267                 if (test_bit(i, &ehci->bus_suspended) &&
268                                 (temp & PORT_SUSPEND)) {
269                         temp |= PORT_RESUME;
270                         resume_needed = 1;
271                 }
272                 ehci_writel(ehci, temp, &ehci->regs->port_status [i]);
273         }
274
275         /* msleep for 20ms only if code is trying to resume port */
276         if (resume_needed) {
277                 spin_unlock_irq(&ehci->lock);
278                 msleep(20);
279                 spin_lock_irq(&ehci->lock);
280         }
281
282         i = HCS_N_PORTS (ehci->hcs_params);
283         while (i--) {
284                 temp = ehci_readl(ehci, &ehci->regs->port_status [i]);
285                 if (test_bit(i, &ehci->bus_suspended) &&
286                                 (temp & PORT_SUSPEND)) {
287                         temp &= ~(PORT_RWC_BITS | PORT_RESUME);
288                         ehci_writel(ehci, temp, &ehci->regs->port_status [i]);
289                         ehci_vdbg (ehci, "resumed port %d\n", i + 1);
290                 }
291         }
292         (void) ehci_readl(ehci, &ehci->regs->command);
293
294         /* maybe re-activate the schedule(s) */
295         temp = 0;
296         if (ehci->async->qh_next.qh)
297                 temp |= CMD_ASE;
298         if (ehci->periodic_sched)
299                 temp |= CMD_PSE;
300         if (temp) {
301                 ehci->command |= temp;
302                 ehci_writel(ehci, ehci->command, &ehci->regs->command);
303         }
304
305         ehci->next_statechange = jiffies + msecs_to_jiffies(5);
306         hcd->state = HC_STATE_RUNNING;
307
308         /* Now we can safely re-enable irqs */
309         ehci_writel(ehci, INTR_MASK, &ehci->regs->intr_enable);
310
311         spin_unlock_irq (&ehci->lock);
312         ehci_handover_companion_ports(ehci);
313         return 0;
314 }
315
316 #else
317
318 #define ehci_bus_suspend        NULL
319 #define ehci_bus_resume         NULL
320
321 #endif  /* CONFIG_PM */
322
323 /*-------------------------------------------------------------------------*/
324
325 /* Display the ports dedicated to the companion controller */
326 static ssize_t show_companion(struct device *dev,
327                               struct device_attribute *attr,
328                               char *buf)
329 {
330         struct ehci_hcd         *ehci;
331         int                     nports, index, n;
332         int                     count = PAGE_SIZE;
333         char                    *ptr = buf;
334
335         ehci = hcd_to_ehci(bus_to_hcd(dev_get_drvdata(dev)));
336         nports = HCS_N_PORTS(ehci->hcs_params);
337
338         for (index = 0; index < nports; ++index) {
339                 if (test_bit(index, &ehci->companion_ports)) {
340                         n = scnprintf(ptr, count, "%d\n", index + 1);
341                         ptr += n;
342                         count -= n;
343                 }
344         }
345         return ptr - buf;
346 }
347
348 /*
349  * Sets the owner of a port
350  */
351 static void set_owner(struct ehci_hcd *ehci, int portnum, int new_owner)
352 {
353         u32 __iomem             *status_reg;
354         u32                     port_status;
355         int                     try;
356
357         status_reg = &ehci->regs->port_status[portnum];
358
359         /*
360          * The controller won't set the OWNER bit if the port is
361          * enabled, so this loop will sometimes require at least two
362          * iterations: one to disable the port and one to set OWNER.
363          */
364         for (try = 4; try > 0; --try) {
365                 spin_lock_irq(&ehci->lock);
366                 port_status = ehci_readl(ehci, status_reg);
367                 if ((port_status & PORT_OWNER) == new_owner
368                                 || (port_status & (PORT_OWNER | PORT_CONNECT))
369                                         == 0)
370                         try = 0;
371                 else {
372                         port_status ^= PORT_OWNER;
373                         port_status &= ~(PORT_PE | PORT_RWC_BITS);
374                         ehci_writel(ehci, port_status, status_reg);
375                 }
376                 spin_unlock_irq(&ehci->lock);
377                 if (try > 1)
378                         msleep(5);
379         }
380 }
381
382 /*
383  * Dedicate or undedicate a port to the companion controller.
384  * Syntax is "[-]portnum", where a leading '-' sign means
385  * return control of the port to the EHCI controller.
386  */
387 static ssize_t store_companion(struct device *dev,
388                                struct device_attribute *attr,
389                                const char *buf, size_t count)
390 {
391         struct ehci_hcd         *ehci;
392         int                     portnum, new_owner;
393
394         ehci = hcd_to_ehci(bus_to_hcd(dev_get_drvdata(dev)));
395         new_owner = PORT_OWNER;         /* Owned by companion */
396         if (sscanf(buf, "%d", &portnum) != 1)
397                 return -EINVAL;
398         if (portnum < 0) {
399                 portnum = - portnum;
400                 new_owner = 0;          /* Owned by EHCI */
401         }
402         if (portnum <= 0 || portnum > HCS_N_PORTS(ehci->hcs_params))
403                 return -ENOENT;
404         portnum--;
405         if (new_owner)
406                 set_bit(portnum, &ehci->companion_ports);
407         else
408                 clear_bit(portnum, &ehci->companion_ports);
409         set_owner(ehci, portnum, new_owner);
410         return count;
411 }
412 static DEVICE_ATTR(companion, 0644, show_companion, store_companion);
413
414 static inline void create_companion_file(struct ehci_hcd *ehci)
415 {
416         int     i;
417
418         /* with integrated TT there is no companion! */
419         if (!ehci_is_TDI(ehci))
420                 i = device_create_file(ehci_to_hcd(ehci)->self.controller,
421                                        &dev_attr_companion);
422 }
423
424 static inline void remove_companion_file(struct ehci_hcd *ehci)
425 {
426         /* with integrated TT there is no companion! */
427         if (!ehci_is_TDI(ehci))
428                 device_remove_file(ehci_to_hcd(ehci)->self.controller,
429                                    &dev_attr_companion);
430 }
431
432
433 /*-------------------------------------------------------------------------*/
434
435 static int check_reset_complete (
436         struct ehci_hcd *ehci,
437         int             index,
438         u32 __iomem     *status_reg,
439         int             port_status
440 ) {
441         if (!(port_status & PORT_CONNECT))
442                 return port_status;
443
444         /* if reset finished and it's still not enabled -- handoff */
445         if (!(port_status & PORT_PE)) {
446
447                 /* with integrated TT, there's nobody to hand it to! */
448                 if (ehci_is_TDI(ehci)) {
449                         ehci_dbg (ehci,
450                                 "Failed to enable port %d on root hub TT\n",
451                                 index+1);
452                         return port_status;
453                 }
454
455                 ehci_dbg (ehci, "port %d full speed --> companion\n",
456                         index + 1);
457
458                 // what happens if HCS_N_CC(params) == 0 ?
459                 port_status |= PORT_OWNER;
460                 port_status &= ~PORT_RWC_BITS;
461                 ehci_writel(ehci, port_status, status_reg);
462
463                 /* ensure 440EPX ohci controller state is operational */
464                 if (ehci->has_amcc_usb23)
465                         set_ohci_hcfs(ehci, 1);
466         } else {
467                 ehci_dbg (ehci, "port %d high speed\n", index + 1);
468                 /* ensure 440EPx ohci controller state is suspended */
469                 if (ehci->has_amcc_usb23)
470                         set_ohci_hcfs(ehci, 0);
471         }
472
473         return port_status;
474 }
475
476 /*-------------------------------------------------------------------------*/
477
478
479 /* build "status change" packet (one or two bytes) from HC registers */
480
481 static int
482 ehci_hub_status_data (struct usb_hcd *hcd, char *buf)
483 {
484         struct ehci_hcd *ehci = hcd_to_ehci (hcd);
485         u32             temp, status = 0;
486         u32             mask;
487         int             ports, i, retval = 1;
488         unsigned long   flags;
489
490         /* if !USB_SUSPEND, root hub timers won't get shut down ... */
491         if (!HC_IS_RUNNING(hcd->state))
492                 return 0;
493
494         /* init status to no-changes */
495         buf [0] = 0;
496         ports = HCS_N_PORTS (ehci->hcs_params);
497         if (ports > 7) {
498                 buf [1] = 0;
499                 retval++;
500         }
501
502         /* Some boards (mostly VIA?) report bogus overcurrent indications,
503          * causing massive log spam unless we completely ignore them.  It
504          * may be relevant that VIA VT8235 controllers, where PORT_POWER is
505          * always set, seem to clear PORT_OCC and PORT_CSC when writing to
506          * PORT_POWER; that's surprising, but maybe within-spec.
507          */
508         if (!ignore_oc)
509                 mask = PORT_CSC | PORT_PEC | PORT_OCC;
510         else
511                 mask = PORT_CSC | PORT_PEC;
512         // PORT_RESUME from hardware ~= PORT_STAT_C_SUSPEND
513
514         /* no hub change reports (bit 0) for now (power, ...) */
515
516         /* port N changes (bit N)? */
517         spin_lock_irqsave (&ehci->lock, flags);
518         for (i = 0; i < ports; i++) {
519                 temp = ehci_readl(ehci, &ehci->regs->port_status [i]);
520
521                 /*
522                  * Return status information even for ports with OWNER set.
523                  * Otherwise khubd wouldn't see the disconnect event when a
524                  * high-speed device is switched over to the companion
525                  * controller by the user.
526                  */
527
528                 if ((temp & mask) != 0 || test_bit(i, &ehci->port_c_suspend)
529                                 || (ehci->reset_done[i] && time_after_eq(
530                                         jiffies, ehci->reset_done[i]))) {
531                         if (i < 7)
532                             buf [0] |= 1 << (i + 1);
533                         else
534                             buf [1] |= 1 << (i - 7);
535                         status = STS_PCD;
536                 }
537         }
538         /* FIXME autosuspend idle root hubs */
539         spin_unlock_irqrestore (&ehci->lock, flags);
540         return status ? retval : 0;
541 }
542
543 /*-------------------------------------------------------------------------*/
544
545 static void
546 ehci_hub_descriptor (
547         struct ehci_hcd                 *ehci,
548         struct usb_hub_descriptor       *desc
549 ) {
550         int             ports = HCS_N_PORTS (ehci->hcs_params);
551         u16             temp;
552
553         desc->bDescriptorType = 0x29;
554         desc->bPwrOn2PwrGood = 10;      /* ehci 1.0, 2.3.9 says 20ms max */
555         desc->bHubContrCurrent = 0;
556
557         desc->bNbrPorts = ports;
558         temp = 1 + (ports / 8);
559         desc->bDescLength = 7 + 2 * temp;
560
561         /* two bitmaps:  ports removable, and usb 1.0 legacy PortPwrCtrlMask */
562         memset (&desc->bitmap [0], 0, temp);
563         memset (&desc->bitmap [temp], 0xff, temp);
564
565         temp = 0x0008;                  /* per-port overcurrent reporting */
566         if (HCS_PPC (ehci->hcs_params))
567                 temp |= 0x0001;         /* per-port power control */
568         else
569                 temp |= 0x0002;         /* no power switching */
570 #if 0
571 // re-enable when we support USB_PORT_FEAT_INDICATOR below.
572         if (HCS_INDICATOR (ehci->hcs_params))
573                 temp |= 0x0080;         /* per-port indicators (LEDs) */
574 #endif
575         desc->wHubCharacteristics = cpu_to_le16(temp);
576 }
577
578 /*-------------------------------------------------------------------------*/
579
580 static int ehci_hub_control (
581         struct usb_hcd  *hcd,
582         u16             typeReq,
583         u16             wValue,
584         u16             wIndex,
585         char            *buf,
586         u16             wLength
587 ) {
588         struct ehci_hcd *ehci = hcd_to_ehci (hcd);
589         int             ports = HCS_N_PORTS (ehci->hcs_params);
590         u32 __iomem     *status_reg = &ehci->regs->port_status[
591                                 (wIndex & 0xff) - 1];
592         u32 __iomem     *hostpc_reg = NULL;
593         u32             temp, temp1, status;
594         unsigned long   flags;
595         int             retval = 0;
596         unsigned        selector;
597
598         /*
599          * FIXME:  support SetPortFeatures USB_PORT_FEAT_INDICATOR.
600          * HCS_INDICATOR may say we can change LEDs to off/amber/green.
601          * (track current state ourselves) ... blink for diagnostics,
602          * power, "this is the one", etc.  EHCI spec supports this.
603          */
604
605         if (ehci->has_hostpc)
606                 hostpc_reg = (u32 __iomem *)((u8 *)ehci->regs
607                                 + HOSTPC0 + 4 * ((wIndex & 0xff) - 1));
608         spin_lock_irqsave (&ehci->lock, flags);
609         switch (typeReq) {
610         case ClearHubFeature:
611                 switch (wValue) {
612                 case C_HUB_LOCAL_POWER:
613                 case C_HUB_OVER_CURRENT:
614                         /* no hub-wide feature/status flags */
615                         break;
616                 default:
617                         goto error;
618                 }
619                 break;
620         case ClearPortFeature:
621                 if (!wIndex || wIndex > ports)
622                         goto error;
623                 wIndex--;
624                 temp = ehci_readl(ehci, status_reg);
625
626                 /*
627                  * Even if OWNER is set, so the port is owned by the
628                  * companion controller, khubd needs to be able to clear
629                  * the port-change status bits (especially
630                  * USB_PORT_FEAT_C_CONNECTION).
631                  */
632
633                 switch (wValue) {
634                 case USB_PORT_FEAT_ENABLE:
635                         ehci_writel(ehci, temp & ~PORT_PE, status_reg);
636                         break;
637                 case USB_PORT_FEAT_C_ENABLE:
638                         ehci_writel(ehci, (temp & ~PORT_RWC_BITS) | PORT_PEC,
639                                         status_reg);
640                         break;
641                 case USB_PORT_FEAT_SUSPEND:
642                         if (temp & PORT_RESET)
643                                 goto error;
644                         if (ehci->no_selective_suspend)
645                                 break;
646                         if (temp & PORT_SUSPEND) {
647                                 if ((temp & PORT_PE) == 0)
648                                         goto error;
649                                 /* resume signaling for 20 msec */
650                                 temp &= ~(PORT_RWC_BITS | PORT_WAKE_BITS);
651                                 ehci_writel(ehci, temp | PORT_RESUME,
652                                                 status_reg);
653                                 ehci->reset_done [wIndex] = jiffies
654                                                 + msecs_to_jiffies (20);
655                         }
656                         break;
657                 case USB_PORT_FEAT_C_SUSPEND:
658                         clear_bit(wIndex, &ehci->port_c_suspend);
659                         break;
660                 case USB_PORT_FEAT_POWER:
661                         if (HCS_PPC (ehci->hcs_params))
662                                 ehci_writel(ehci,
663                                           temp & ~(PORT_RWC_BITS | PORT_POWER),
664                                           status_reg);
665                         break;
666                 case USB_PORT_FEAT_C_CONNECTION:
667                         ehci_writel(ehci, (temp & ~PORT_RWC_BITS) | PORT_CSC,
668                                         status_reg);
669                         break;
670                 case USB_PORT_FEAT_C_OVER_CURRENT:
671                         ehci_writel(ehci, (temp & ~PORT_RWC_BITS) | PORT_OCC,
672                                         status_reg);
673                         break;
674                 case USB_PORT_FEAT_C_RESET:
675                         /* GetPortStatus clears reset */
676                         break;
677                 default:
678                         goto error;
679                 }
680                 ehci_readl(ehci, &ehci->regs->command); /* unblock posted write */
681                 break;
682         case GetHubDescriptor:
683                 ehci_hub_descriptor (ehci, (struct usb_hub_descriptor *)
684                         buf);
685                 break;
686         case GetHubStatus:
687                 /* no hub-wide feature/status flags */
688                 memset (buf, 0, 4);
689                 //cpu_to_le32s ((u32 *) buf);
690                 break;
691         case GetPortStatus:
692                 if (!wIndex || wIndex > ports)
693                         goto error;
694                 wIndex--;
695                 status = 0;
696                 temp = ehci_readl(ehci, status_reg);
697
698                 // wPortChange bits
699                 if (temp & PORT_CSC)
700                         status |= 1 << USB_PORT_FEAT_C_CONNECTION;
701                 if (temp & PORT_PEC)
702                         status |= 1 << USB_PORT_FEAT_C_ENABLE;
703
704                 if ((temp & PORT_OCC) && !ignore_oc){
705                         status |= 1 << USB_PORT_FEAT_C_OVER_CURRENT;
706
707                         /*
708                          * Hubs should disable port power on over-current.
709                          * However, not all EHCI implementations do this
710                          * automatically, even if they _do_ support per-port
711                          * power switching; they're allowed to just limit the
712                          * current.  khubd will turn the power back on.
713                          */
714                         if (HCS_PPC (ehci->hcs_params)){
715                                 ehci_writel(ehci,
716                                         temp & ~(PORT_RWC_BITS | PORT_POWER),
717                                         status_reg);
718                         }
719                 }
720
721                 /* whoever resumes must GetPortStatus to complete it!! */
722                 if (temp & PORT_RESUME) {
723
724                         /* Remote Wakeup received? */
725                         if (!ehci->reset_done[wIndex]) {
726                                 /* resume signaling for 20 msec */
727                                 ehci->reset_done[wIndex] = jiffies
728                                                 + msecs_to_jiffies(20);
729                                 /* check the port again */
730                                 mod_timer(&ehci_to_hcd(ehci)->rh_timer,
731                                                 ehci->reset_done[wIndex]);
732                         }
733
734                         /* resume completed? */
735                         else if (time_after_eq(jiffies,
736                                         ehci->reset_done[wIndex])) {
737                                 clear_bit(wIndex, &ehci->suspended_ports);
738                                 set_bit(wIndex, &ehci->port_c_suspend);
739                                 ehci->reset_done[wIndex] = 0;
740
741                                 /* stop resume signaling */
742                                 temp = ehci_readl(ehci, status_reg);
743                                 ehci_writel(ehci,
744                                         temp & ~(PORT_RWC_BITS | PORT_RESUME),
745                                         status_reg);
746                                 retval = handshake(ehci, status_reg,
747                                            PORT_RESUME, 0, 2000 /* 2msec */);
748                                 if (retval != 0) {
749                                         ehci_err(ehci,
750                                                 "port %d resume error %d\n",
751                                                 wIndex + 1, retval);
752                                         goto error;
753                                 }
754                                 temp &= ~(PORT_SUSPEND|PORT_RESUME|(3<<10));
755                         }
756                 }
757
758                 /* whoever resets must GetPortStatus to complete it!! */
759                 if ((temp & PORT_RESET)
760                                 && time_after_eq(jiffies,
761                                         ehci->reset_done[wIndex])) {
762                         status |= 1 << USB_PORT_FEAT_C_RESET;
763                         ehci->reset_done [wIndex] = 0;
764
765                         /* force reset to complete */
766                         ehci_writel(ehci, temp & ~(PORT_RWC_BITS | PORT_RESET),
767                                         status_reg);
768                         /* REVISIT:  some hardware needs 550+ usec to clear
769                          * this bit; seems too long to spin routinely...
770                          */
771                         retval = handshake(ehci, status_reg,
772                                         PORT_RESET, 0, 750);
773                         if (retval != 0) {
774                                 ehci_err (ehci, "port %d reset error %d\n",
775                                         wIndex + 1, retval);
776                                 goto error;
777                         }
778
779                         /* see what we found out */
780                         temp = check_reset_complete (ehci, wIndex, status_reg,
781                                         ehci_readl(ehci, status_reg));
782                 }
783
784                 if (!(temp & (PORT_RESUME|PORT_RESET)))
785                         ehci->reset_done[wIndex] = 0;
786
787                 /* transfer dedicated ports to the companion hc */
788                 if ((temp & PORT_CONNECT) &&
789                                 test_bit(wIndex, &ehci->companion_ports)) {
790                         temp &= ~PORT_RWC_BITS;
791                         temp |= PORT_OWNER;
792                         ehci_writel(ehci, temp, status_reg);
793                         ehci_dbg(ehci, "port %d --> companion\n", wIndex + 1);
794                         temp = ehci_readl(ehci, status_reg);
795                 }
796
797                 /*
798                  * Even if OWNER is set, there's no harm letting khubd
799                  * see the wPortStatus values (they should all be 0 except
800                  * for PORT_POWER anyway).
801                  */
802
803                 if (temp & PORT_CONNECT) {
804                         status |= 1 << USB_PORT_FEAT_CONNECTION;
805                         // status may be from integrated TT
806                         if (ehci->has_hostpc) {
807                                 temp1 = ehci_readl(ehci, hostpc_reg);
808                                 status |= ehci_port_speed(ehci, temp1);
809                         } else
810                                 status |= ehci_port_speed(ehci, temp);
811                 }
812                 if (temp & PORT_PE)
813                         status |= 1 << USB_PORT_FEAT_ENABLE;
814
815                 /* maybe the port was unsuspended without our knowledge */
816                 if (temp & (PORT_SUSPEND|PORT_RESUME)) {
817                         status |= 1 << USB_PORT_FEAT_SUSPEND;
818                 } else if (test_bit(wIndex, &ehci->suspended_ports)) {
819                         clear_bit(wIndex, &ehci->suspended_ports);
820                         ehci->reset_done[wIndex] = 0;
821                         if (temp & PORT_PE)
822                                 set_bit(wIndex, &ehci->port_c_suspend);
823                 }
824
825                 if (temp & PORT_OC)
826                         status |= 1 << USB_PORT_FEAT_OVER_CURRENT;
827                 if (temp & PORT_RESET)
828                         status |= 1 << USB_PORT_FEAT_RESET;
829                 if (temp & PORT_POWER)
830                         status |= 1 << USB_PORT_FEAT_POWER;
831                 if (test_bit(wIndex, &ehci->port_c_suspend))
832                         status |= 1 << USB_PORT_FEAT_C_SUSPEND;
833
834 #ifndef VERBOSE_DEBUG
835         if (status & ~0xffff)   /* only if wPortChange is interesting */
836 #endif
837                 dbg_port (ehci, "GetStatus", wIndex + 1, temp);
838                 put_unaligned_le32(status, buf);
839                 break;
840         case SetHubFeature:
841                 switch (wValue) {
842                 case C_HUB_LOCAL_POWER:
843                 case C_HUB_OVER_CURRENT:
844                         /* no hub-wide feature/status flags */
845                         break;
846                 default:
847                         goto error;
848                 }
849                 break;
850         case SetPortFeature:
851                 selector = wIndex >> 8;
852                 wIndex &= 0xff;
853                 if (!wIndex || wIndex > ports)
854                         goto error;
855                 wIndex--;
856                 temp = ehci_readl(ehci, status_reg);
857                 if (temp & PORT_OWNER)
858                         break;
859
860                 temp &= ~PORT_RWC_BITS;
861                 switch (wValue) {
862                 case USB_PORT_FEAT_SUSPEND:
863                         if (ehci->no_selective_suspend)
864                                 break;
865                         if ((temp & PORT_PE) == 0
866                                         || (temp & PORT_RESET) != 0)
867                                 goto error;
868                         ehci_writel(ehci, temp | PORT_SUSPEND, status_reg);
869                         /* After above check the port must be connected.
870                          * Set appropriate bit thus could put phy into low power
871                          * mode if we have hostpc feature
872                          */
873                         if (hostpc_reg) {
874                                 temp &= ~PORT_WKCONN_E;
875                                 temp |= (PORT_WKDISC_E | PORT_WKOC_E);
876                                 ehci_writel(ehci, temp | PORT_SUSPEND,
877                                                         status_reg);
878                                 msleep(5);/* 5ms for HCD enter low pwr mode */
879                                 temp1 = ehci_readl(ehci, hostpc_reg);
880                                 ehci_writel(ehci, temp1 | HOSTPC_PHCD,
881                                         hostpc_reg);
882                                 temp1 = ehci_readl(ehci, hostpc_reg);
883                                 ehci_dbg(ehci, "Port%d phy low pwr mode %s\n",
884                                         wIndex, (temp1 & HOSTPC_PHCD) ?
885                                         "succeeded" : "failed");
886                         }
887                         set_bit(wIndex, &ehci->suspended_ports);
888                         break;
889                 case USB_PORT_FEAT_POWER:
890                         if (HCS_PPC (ehci->hcs_params))
891                                 ehci_writel(ehci, temp | PORT_POWER,
892                                                 status_reg);
893                         break;
894                 case USB_PORT_FEAT_RESET:
895                         if (temp & PORT_RESUME)
896                                 goto error;
897                         /* line status bits may report this as low speed,
898                          * which can be fine if this root hub has a
899                          * transaction translator built in.
900                          */
901                         if ((temp & (PORT_PE|PORT_CONNECT)) == PORT_CONNECT
902                                         && !ehci_is_TDI(ehci)
903                                         && PORT_USB11 (temp)) {
904                                 ehci_dbg (ehci,
905                                         "port %d low speed --> companion\n",
906                                         wIndex + 1);
907                                 temp |= PORT_OWNER;
908                         } else {
909                                 ehci_vdbg (ehci, "port %d reset\n", wIndex + 1);
910                                 temp |= PORT_RESET;
911                                 temp &= ~PORT_PE;
912
913                                 /*
914                                  * caller must wait, then call GetPortStatus
915                                  * usb 2.0 spec says 50 ms resets on root
916                                  */
917                                 ehci->reset_done [wIndex] = jiffies
918                                                 + msecs_to_jiffies (50);
919                         }
920                         ehci_writel(ehci, temp, status_reg);
921                         break;
922
923                 /* For downstream facing ports (these):  one hub port is put
924                  * into test mode according to USB2 11.24.2.13, then the hub
925                  * must be reset (which for root hub now means rmmod+modprobe,
926                  * or else system reboot).  See EHCI 2.3.9 and 4.14 for info
927                  * about the EHCI-specific stuff.
928                  */
929                 case USB_PORT_FEAT_TEST:
930                         if (!selector || selector > 5)
931                                 goto error;
932                         ehci_quiesce(ehci);
933                         ehci_halt(ehci);
934                         temp |= selector << 16;
935                         ehci_writel(ehci, temp, status_reg);
936                         break;
937
938                 default:
939                         goto error;
940                 }
941                 ehci_readl(ehci, &ehci->regs->command); /* unblock posted writes */
942                 break;
943
944         default:
945 error:
946                 /* "stall" on error */
947                 retval = -EPIPE;
948         }
949         spin_unlock_irqrestore (&ehci->lock, flags);
950         return retval;
951 }
952
953 static void ehci_relinquish_port(struct usb_hcd *hcd, int portnum)
954 {
955         struct ehci_hcd         *ehci = hcd_to_ehci(hcd);
956
957         if (ehci_is_TDI(ehci))
958                 return;
959         set_owner(ehci, --portnum, PORT_OWNER);
960 }
961
962 static int ehci_port_handed_over(struct usb_hcd *hcd, int portnum)
963 {
964         struct ehci_hcd         *ehci = hcd_to_ehci(hcd);
965         u32 __iomem             *reg;
966
967         if (ehci_is_TDI(ehci))
968                 return 0;
969         reg = &ehci->regs->port_status[portnum - 1];
970         return ehci_readl(ehci, reg) & PORT_OWNER;
971 }