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