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