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