upload tizen1.0 source
[kernel/linux-2.6.36.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 #define PORT_WAKE_BITS  (PORT_WKOC_E|PORT_WKDISC_E|PORT_WKCONN_E)
32
33 #ifdef  CONFIG_PM
34
35 static int ehci_hub_control(
36         struct usb_hcd  *hcd,
37         u16             typeReq,
38         u16             wValue,
39         u16             wIndex,
40         char            *buf,
41         u16             wLength
42 );
43
44 /* After a power loss, ports that were owned by the companion must be
45  * reset so that the companion can still own them.
46  */
47 static void ehci_handover_companion_ports(struct ehci_hcd *ehci)
48 {
49         u32 __iomem     *reg;
50         u32             status;
51         int             port;
52         __le32          buf;
53         struct usb_hcd  *hcd = ehci_to_hcd(ehci);
54
55         if (!ehci->owned_ports)
56                 return;
57
58         /* Give the connections some time to appear */
59         msleep(20);
60
61         port = HCS_N_PORTS(ehci->hcs_params);
62         while (port--) {
63                 if (test_bit(port, &ehci->owned_ports)) {
64                         reg = &ehci->regs->port_status[port];
65                         status = ehci_readl(ehci, reg) & ~PORT_RWC_BITS;
66
67                         /* Port already owned by companion? */
68                         if (status & PORT_OWNER)
69                                 clear_bit(port, &ehci->owned_ports);
70                         else if (test_bit(port, &ehci->companion_ports))
71                                 ehci_writel(ehci, status & ~PORT_PE, reg);
72                         else
73                                 ehci_hub_control(hcd, SetPortFeature,
74                                                 USB_PORT_FEAT_RESET, port + 1,
75                                                 NULL, 0);
76                 }
77         }
78
79         if (!ehci->owned_ports)
80                 return;
81         msleep(90);             /* Wait for resets to complete */
82
83         port = HCS_N_PORTS(ehci->hcs_params);
84         while (port--) {
85                 if (test_bit(port, &ehci->owned_ports)) {
86                         ehci_hub_control(hcd, GetPortStatus,
87                                         0, port + 1,
88                                         (char *) &buf, sizeof(buf));
89
90                         /* The companion should now own the port,
91                          * but if something went wrong the port must not
92                          * remain enabled.
93                          */
94                         reg = &ehci->regs->port_status[port];
95                         status = ehci_readl(ehci, reg) & ~PORT_RWC_BITS;
96                         if (status & PORT_OWNER)
97                                 ehci_writel(ehci, status | PORT_CSC, reg);
98                         else {
99                                 ehci_dbg(ehci, "failed handover port %d: %x\n",
100                                                 port + 1, status);
101                                 ehci_writel(ehci, status & ~PORT_PE, reg);
102                         }
103                 }
104         }
105
106         ehci->owned_ports = 0;
107 }
108
109 static void ehci_adjust_port_wakeup_flags(struct ehci_hcd *ehci,
110                 bool suspending, bool do_wakeup)
111 {
112         int             port;
113         u32             temp;
114         unsigned long   flags;
115
116         /* If remote wakeup is enabled for the root hub but disabled
117          * for the controller, we must adjust all the port wakeup flags
118          * when the controller is suspended or resumed.  In all other
119          * cases they don't need to be changed.
120          */
121         if (!ehci_to_hcd(ehci)->self.root_hub->do_remote_wakeup || do_wakeup)
122                 return;
123
124         spin_lock_irqsave(&ehci->lock, flags);
125
126         /* clear phy low-power mode before changing wakeup flags */
127         if (ehci->has_hostpc) {
128                 port = HCS_N_PORTS(ehci->hcs_params);
129                 while (port--) {
130                         u32 __iomem     *hostpc_reg;
131
132                         hostpc_reg = (u32 __iomem *)((u8 *) ehci->regs
133                                         + HOSTPC0 + 4 * port);
134                         temp = ehci_readl(ehci, hostpc_reg);
135                         ehci_writel(ehci, temp & ~HOSTPC_PHCD, hostpc_reg);
136                 }
137                 spin_unlock_irqrestore(&ehci->lock, flags);
138                 msleep(5);
139                 spin_lock_irqsave(&ehci->lock, flags);
140         }
141
142         port = HCS_N_PORTS(ehci->hcs_params);
143         while (port--) {
144                 u32 __iomem     *reg = &ehci->regs->port_status[port];
145                 u32             t1 = ehci_readl(ehci, reg) & ~PORT_RWC_BITS;
146                 u32             t2 = t1 & ~PORT_WAKE_BITS;
147
148                 /* If we are suspending the controller, clear the flags.
149                  * If we are resuming the controller, set the wakeup flags.
150                  */
151                 if (!suspending) {
152                         if (t1 & PORT_CONNECT)
153                                 t2 |= PORT_WKOC_E | PORT_WKDISC_E;
154                         else
155                                 t2 |= PORT_WKOC_E | PORT_WKCONN_E;
156                 }
157                 ehci_vdbg(ehci, "port %d, %08x -> %08x\n",
158                                 port + 1, t1, t2);
159                 ehci_writel(ehci, t2, reg);
160         }
161
162         /* enter phy low-power mode again */
163         if (ehci->has_hostpc) {
164                 port = HCS_N_PORTS(ehci->hcs_params);
165                 while (port--) {
166                         u32 __iomem     *hostpc_reg;
167
168                         hostpc_reg = (u32 __iomem *)((u8 *) ehci->regs
169                                         + HOSTPC0 + 4 * port);
170                         temp = ehci_readl(ehci, hostpc_reg);
171                         ehci_writel(ehci, temp | HOSTPC_PHCD, hostpc_reg);
172                 }
173         }
174
175         /* Does the root hub have a port wakeup pending? */
176         if (!suspending && (ehci_readl(ehci, &ehci->regs->status) & STS_PCD))
177                 usb_hcd_resume_root_hub(ehci_to_hcd(ehci));
178
179         spin_unlock_irqrestore(&ehci->lock, flags);
180 }
181
182 static int ehci_bus_suspend (struct usb_hcd *hcd)
183 {
184         struct ehci_hcd         *ehci = hcd_to_ehci (hcd);
185         int                     port;
186         int                     mask;
187         int                     changed;
188
189         ehci_dbg(ehci, "suspend root hub\n");
190
191         if (time_before (jiffies, ehci->next_statechange))
192                 msleep(5);
193         del_timer_sync(&ehci->watchdog);
194         del_timer_sync(&ehci->iaa_watchdog);
195
196         spin_lock_irq (&ehci->lock);
197
198         /* Once the controller is stopped, port resumes that are already
199          * in progress won't complete.  Hence if remote wakeup is enabled
200          * for the root hub and any ports are in the middle of a resume or
201          * remote wakeup, we must fail the suspend.
202          */
203         if (hcd->self.root_hub->do_remote_wakeup) {
204                 port = HCS_N_PORTS(ehci->hcs_params);
205                 while (port--) {
206                         if (ehci->reset_done[port] != 0) {
207                                 spin_unlock_irq(&ehci->lock);
208                                 ehci_dbg(ehci, "suspend failed because "
209                                                 "port %d is resuming\n",
210                                                 port + 1);
211                                 return -EBUSY;
212                         }
213                 }
214         }
215
216         /* stop schedules, clean any completed work */
217         if (HC_IS_RUNNING(hcd->state)) {
218                 ehci_quiesce (ehci);
219                 hcd->state = HC_STATE_QUIESCING;
220         }
221         ehci->command = ehci_readl(ehci, &ehci->regs->command);
222         ehci_work(ehci);
223
224         /* Unlike other USB host controller types, EHCI doesn't have
225          * any notion of "global" or bus-wide suspend.  The driver has
226          * to manually suspend all the active unsuspended ports, and
227          * then manually resume them in the bus_resume() routine.
228          */
229         ehci->bus_suspended = 0;
230         ehci->owned_ports = 0;
231         changed = 0;
232         port = HCS_N_PORTS(ehci->hcs_params);
233         while (port--) {
234                 u32 __iomem     *reg = &ehci->regs->port_status [port];
235                 u32             t1 = ehci_readl(ehci, reg) & ~PORT_RWC_BITS;
236                 u32             t2 = t1 & ~PORT_WAKE_BITS;
237
238                 /* keep track of which ports we suspend */
239                 if (t1 & PORT_OWNER)
240                         set_bit(port, &ehci->owned_ports);
241                 else if ((t1 & PORT_PE) && !(t1 & PORT_SUSPEND)) {
242                         t2 |= PORT_SUSPEND;
243                         set_bit(port, &ehci->bus_suspended);
244                 }
245
246                 /* enable remote wakeup on all ports, if told to do so */
247                 if (hcd->self.root_hub->do_remote_wakeup) {
248                         /* only enable appropriate wake bits, otherwise the
249                          * hardware can not go phy low power mode. If a race
250                          * condition happens here(connection change during bits
251                          * set), the port change detection will finally fix it.
252                          */
253                         if (t1 & PORT_CONNECT)
254                                 t2 |= PORT_WKOC_E | PORT_WKDISC_E;
255                         else
256                                 t2 |= PORT_WKOC_E | PORT_WKCONN_E;
257                 }
258
259                 if (t1 != t2) {
260                         ehci_vdbg (ehci, "port %d, %08x -> %08x\n",
261                                 port + 1, t1, t2);
262                         ehci_writel(ehci, t2, reg);
263                         changed = 1;
264                 }
265         }
266
267         if (changed && ehci->has_hostpc) {
268                 spin_unlock_irq(&ehci->lock);
269                 msleep(5);      /* 5 ms for HCD to enter low-power mode */
270                 spin_lock_irq(&ehci->lock);
271
272                 port = HCS_N_PORTS(ehci->hcs_params);
273                 while (port--) {
274                         u32 __iomem     *hostpc_reg;
275                         u32             t3;
276
277                         hostpc_reg = (u32 __iomem *)((u8 *) ehci->regs
278                                         + HOSTPC0 + 4 * port);
279                         t3 = ehci_readl(ehci, hostpc_reg);
280                         ehci_writel(ehci, t3 | HOSTPC_PHCD, hostpc_reg);
281                         t3 = ehci_readl(ehci, hostpc_reg);
282                         ehci_dbg(ehci, "Port %d phy low-power mode %s\n",
283                                         port, (t3 & HOSTPC_PHCD) ?
284                                         "succeeded" : "failed");
285                 }
286         }
287
288         /* Apparently some devices need a >= 1-uframe delay here */
289         if (ehci->bus_suspended)
290                 udelay(150);
291
292         /* turn off now-idle HC */
293         ehci_halt (ehci);
294         hcd->state = HC_STATE_SUSPENDED;
295
296         if (ehci->reclaim)
297                 end_unlink_async(ehci);
298
299         /* allow remote wakeup */
300         mask = INTR_MASK;
301         if (!hcd->self.root_hub->do_remote_wakeup)
302                 mask &= ~STS_PCD;
303         ehci_writel(ehci, mask, &ehci->regs->intr_enable);
304         ehci_readl(ehci, &ehci->regs->intr_enable);
305
306         ehci->next_statechange = jiffies + msecs_to_jiffies(10);
307         spin_unlock_irq (&ehci->lock);
308
309         /* ehci_work() may have re-enabled the watchdog timer, which we do not
310          * want, and so we must delete any pending watchdog timer events.
311          */
312         del_timer_sync(&ehci->watchdog);
313         return 0;
314 }
315
316
317 /* caller has locked the root hub, and should reset/reinit on error */
318 static int ehci_bus_resume (struct usb_hcd *hcd)
319 {
320         struct ehci_hcd         *ehci = hcd_to_ehci (hcd);
321         u32                     temp;
322         u32                     power_okay;
323         int                     i;
324         u8                      resume_needed = 0;
325
326         if (time_before (jiffies, ehci->next_statechange))
327                 msleep(5);
328         spin_lock_irq (&ehci->lock);
329         if (!HCD_HW_ACCESSIBLE(hcd)) {
330                 spin_unlock_irq(&ehci->lock);
331                 return -ESHUTDOWN;
332         }
333
334         if (unlikely(ehci->debug)) {
335                 if (!dbgp_reset_prep())
336                         ehci->debug = NULL;
337                 else
338                         dbgp_external_startup();
339         }
340
341         /* Ideally and we've got a real resume here, and no port's power
342          * was lost.  (For PCI, that means Vaux was maintained.)  But we
343          * could instead be restoring a swsusp snapshot -- so that BIOS was
344          * the last user of the controller, not reset/pm hardware keeping
345          * state we gave to it.
346          */
347         power_okay = ehci_readl(ehci, &ehci->regs->intr_enable);
348         ehci_dbg(ehci, "resume root hub%s\n",
349                         power_okay ? "" : " after power loss");
350
351         /* at least some APM implementations will try to deliver
352          * IRQs right away, so delay them until we're ready.
353          */
354         ehci_writel(ehci, 0, &ehci->regs->intr_enable);
355
356         /* re-init operational registers */
357         ehci_writel(ehci, 0, &ehci->regs->segment);
358         ehci_writel(ehci, ehci->periodic_dma, &ehci->regs->frame_list);
359         ehci_writel(ehci, (u32) ehci->async->qh_dma, &ehci->regs->async_next);
360
361         /* restore CMD_RUN, framelist size, and irq threshold */
362         ehci_writel(ehci, ehci->command, &ehci->regs->command);
363
364         /* Some controller/firmware combinations need a delay during which
365          * they set up the port statuses.  See Bugzilla #8190. */
366         spin_unlock_irq(&ehci->lock);
367         msleep(8);
368         spin_lock_irq(&ehci->lock);
369
370         /* clear phy low-power mode before resume */
371         if (ehci->bus_suspended && ehci->has_hostpc) {
372                 i = HCS_N_PORTS(ehci->hcs_params);
373                 while (i--) {
374                         if (test_bit(i, &ehci->bus_suspended)) {
375                                 u32 __iomem     *hostpc_reg;
376
377                                 hostpc_reg = (u32 __iomem *)((u8 *) ehci->regs
378                                                 + HOSTPC0 + 4 * i);
379                                 temp = ehci_readl(ehci, hostpc_reg);
380                                 ehci_writel(ehci, temp & ~HOSTPC_PHCD,
381                                                 hostpc_reg);
382                         }
383                 }
384                 spin_unlock_irq(&ehci->lock);
385                 msleep(5);
386                 spin_lock_irq(&ehci->lock);
387         }
388
389         /* manually resume the ports we suspended during bus_suspend() */
390         i = HCS_N_PORTS (ehci->hcs_params);
391         while (i--) {
392                 temp = ehci_readl(ehci, &ehci->regs->port_status [i]);
393                 temp &= ~(PORT_RWC_BITS | PORT_WAKE_BITS);
394                 if (test_bit(i, &ehci->bus_suspended) &&
395                                 (temp & PORT_SUSPEND)) {
396                         temp |= PORT_RESUME;
397                         resume_needed = 1;
398                 }
399                 ehci_writel(ehci, temp, &ehci->regs->port_status [i]);
400         }
401
402         /* msleep for 20ms only if code is trying to resume port */
403         if (resume_needed) {
404                 spin_unlock_irq(&ehci->lock);
405                 msleep(20);
406                 spin_lock_irq(&ehci->lock);
407         }
408
409         i = HCS_N_PORTS (ehci->hcs_params);
410         while (i--) {
411                 temp = ehci_readl(ehci, &ehci->regs->port_status [i]);
412                 if (test_bit(i, &ehci->bus_suspended) &&
413                                 (temp & PORT_SUSPEND)) {
414                         temp &= ~(PORT_RWC_BITS | PORT_RESUME);
415                         ehci_writel(ehci, temp, &ehci->regs->port_status [i]);
416                         ehci_vdbg (ehci, "resumed port %d\n", i + 1);
417                 }
418         }
419         (void) ehci_readl(ehci, &ehci->regs->command);
420
421         /* maybe re-activate the schedule(s) */
422         temp = 0;
423         if (ehci->async->qh_next.qh)
424                 temp |= CMD_ASE;
425         if (ehci->periodic_sched)
426                 temp |= CMD_PSE;
427         if (temp) {
428                 ehci->command |= temp;
429                 ehci_writel(ehci, ehci->command, &ehci->regs->command);
430         }
431
432         ehci->next_statechange = jiffies + msecs_to_jiffies(5);
433         hcd->state = HC_STATE_RUNNING;
434
435         /* Now we can safely re-enable irqs */
436         ehci_writel(ehci, INTR_MASK, &ehci->regs->intr_enable);
437
438         spin_unlock_irq (&ehci->lock);
439         ehci_handover_companion_ports(ehci);
440         return 0;
441 }
442
443 #else
444
445 #define ehci_bus_suspend        NULL
446 #define ehci_bus_resume         NULL
447
448 #endif  /* CONFIG_PM */
449
450 /*-------------------------------------------------------------------------*/
451
452 /* Display the ports dedicated to the companion controller */
453 static ssize_t show_companion(struct device *dev,
454                               struct device_attribute *attr,
455                               char *buf)
456 {
457         struct ehci_hcd         *ehci;
458         int                     nports, index, n;
459         int                     count = PAGE_SIZE;
460         char                    *ptr = buf;
461
462         ehci = hcd_to_ehci(bus_to_hcd(dev_get_drvdata(dev)));
463         nports = HCS_N_PORTS(ehci->hcs_params);
464
465         for (index = 0; index < nports; ++index) {
466                 if (test_bit(index, &ehci->companion_ports)) {
467                         n = scnprintf(ptr, count, "%d\n", index + 1);
468                         ptr += n;
469                         count -= n;
470                 }
471         }
472         return ptr - buf;
473 }
474
475 /*
476  * Sets the owner of a port
477  */
478 static void set_owner(struct ehci_hcd *ehci, int portnum, int new_owner)
479 {
480         u32 __iomem             *status_reg;
481         u32                     port_status;
482         int                     try;
483
484         status_reg = &ehci->regs->port_status[portnum];
485
486         /*
487          * The controller won't set the OWNER bit if the port is
488          * enabled, so this loop will sometimes require at least two
489          * iterations: one to disable the port and one to set OWNER.
490          */
491         for (try = 4; try > 0; --try) {
492                 spin_lock_irq(&ehci->lock);
493                 port_status = ehci_readl(ehci, status_reg);
494                 if ((port_status & PORT_OWNER) == new_owner
495                                 || (port_status & (PORT_OWNER | PORT_CONNECT))
496                                         == 0)
497                         try = 0;
498                 else {
499                         port_status ^= PORT_OWNER;
500                         port_status &= ~(PORT_PE | PORT_RWC_BITS);
501                         ehci_writel(ehci, port_status, status_reg);
502                 }
503                 spin_unlock_irq(&ehci->lock);
504                 if (try > 1)
505                         msleep(5);
506         }
507 }
508
509 /*
510  * Dedicate or undedicate a port to the companion controller.
511  * Syntax is "[-]portnum", where a leading '-' sign means
512  * return control of the port to the EHCI controller.
513  */
514 static ssize_t store_companion(struct device *dev,
515                                struct device_attribute *attr,
516                                const char *buf, size_t count)
517 {
518         struct ehci_hcd         *ehci;
519         int                     portnum, new_owner;
520
521         ehci = hcd_to_ehci(bus_to_hcd(dev_get_drvdata(dev)));
522         new_owner = PORT_OWNER;         /* Owned by companion */
523         if (sscanf(buf, "%d", &portnum) != 1)
524                 return -EINVAL;
525         if (portnum < 0) {
526                 portnum = - portnum;
527                 new_owner = 0;          /* Owned by EHCI */
528         }
529         if (portnum <= 0 || portnum > HCS_N_PORTS(ehci->hcs_params))
530                 return -ENOENT;
531         portnum--;
532         if (new_owner)
533                 set_bit(portnum, &ehci->companion_ports);
534         else
535                 clear_bit(portnum, &ehci->companion_ports);
536         set_owner(ehci, portnum, new_owner);
537         return count;
538 }
539 static DEVICE_ATTR(companion, 0644, show_companion, store_companion);
540
541 static inline void create_companion_file(struct ehci_hcd *ehci)
542 {
543         int     i;
544
545         /* with integrated TT there is no companion! */
546         if (!ehci_is_TDI(ehci))
547                 i = device_create_file(ehci_to_hcd(ehci)->self.controller,
548                                        &dev_attr_companion);
549 }
550
551 static inline void remove_companion_file(struct ehci_hcd *ehci)
552 {
553         /* with integrated TT there is no companion! */
554         if (!ehci_is_TDI(ehci))
555                 device_remove_file(ehci_to_hcd(ehci)->self.controller,
556                                    &dev_attr_companion);
557 }
558
559
560 /*-------------------------------------------------------------------------*/
561
562 static int check_reset_complete (
563         struct ehci_hcd *ehci,
564         int             index,
565         u32 __iomem     *status_reg,
566         int             port_status
567 ) {
568         if (!(port_status & PORT_CONNECT))
569                 return port_status;
570
571         /* if reset finished and it's still not enabled -- handoff */
572         if (!(port_status & PORT_PE)) {
573
574                 /* with integrated TT, there's nobody to hand it to! */
575                 if (ehci_is_TDI(ehci)) {
576                         ehci_dbg (ehci,
577                                 "Failed to enable port %d on root hub TT\n",
578                                 index+1);
579                         return port_status;
580                 }
581
582                 ehci_dbg (ehci, "port %d full speed --> companion\n",
583                         index + 1);
584
585                 // what happens if HCS_N_CC(params) == 0 ?
586                 port_status |= PORT_OWNER;
587                 port_status &= ~PORT_RWC_BITS;
588                 ehci_writel(ehci, port_status, status_reg);
589
590                 /* ensure 440EPX ohci controller state is operational */
591                 if (ehci->has_amcc_usb23)
592                         set_ohci_hcfs(ehci, 1);
593         } else {
594                 ehci_dbg (ehci, "port %d high speed\n", index + 1);
595                 /* ensure 440EPx ohci controller state is suspended */
596                 if (ehci->has_amcc_usb23)
597                         set_ohci_hcfs(ehci, 0);
598         }
599
600         return port_status;
601 }
602
603 /*-------------------------------------------------------------------------*/
604
605
606 /* build "status change" packet (one or two bytes) from HC registers */
607
608 static int
609 ehci_hub_status_data (struct usb_hcd *hcd, char *buf)
610 {
611         struct ehci_hcd *ehci = hcd_to_ehci (hcd);
612         u32             temp, status = 0;
613         u32             mask;
614         int             ports, i, retval = 1;
615         unsigned long   flags;
616         u32             ppcd = 0;
617
618         /* if !USB_SUSPEND, root hub timers won't get shut down ... */
619         if (!HC_IS_RUNNING(hcd->state))
620                 return 0;
621
622         /* init status to no-changes */
623         buf [0] = 0;
624         ports = HCS_N_PORTS (ehci->hcs_params);
625         if (ports > 7) {
626                 buf [1] = 0;
627                 retval++;
628         }
629
630         /* Some boards (mostly VIA?) report bogus overcurrent indications,
631          * causing massive log spam unless we completely ignore them.  It
632          * may be relevant that VIA VT8235 controllers, where PORT_POWER is
633          * always set, seem to clear PORT_OCC and PORT_CSC when writing to
634          * PORT_POWER; that's surprising, but maybe within-spec.
635          */
636         if (!ignore_oc)
637                 mask = PORT_CSC | PORT_PEC | PORT_OCC;
638         else
639                 mask = PORT_CSC | PORT_PEC;
640         // PORT_RESUME from hardware ~= PORT_STAT_C_SUSPEND
641
642         /* no hub change reports (bit 0) for now (power, ...) */
643
644         /* port N changes (bit N)? */
645         spin_lock_irqsave (&ehci->lock, flags);
646
647         /* get per-port change detect bits */
648         if (ehci->has_ppcd)
649                 ppcd = ehci_readl(ehci, &ehci->regs->status) >> 16;
650
651         for (i = 0; i < ports; i++) {
652                 /* leverage per-port change bits feature */
653                 if (ehci->has_ppcd && !(ppcd & (1 << i)))
654                         continue;
655                 temp = ehci_readl(ehci, &ehci->regs->port_status [i]);
656
657                 /*
658                  * Return status information even for ports with OWNER set.
659                  * Otherwise khubd wouldn't see the disconnect event when a
660                  * high-speed device is switched over to the companion
661                  * controller by the user.
662                  */
663
664                 if ((temp & mask) != 0 || test_bit(i, &ehci->port_c_suspend)
665                                 || (ehci->reset_done[i] && time_after_eq(
666                                         jiffies, ehci->reset_done[i]))) {
667                         if (i < 7)
668                             buf [0] |= 1 << (i + 1);
669                         else
670                             buf [1] |= 1 << (i - 7);
671                         status = STS_PCD;
672                 }
673         }
674         /* FIXME autosuspend idle root hubs */
675         spin_unlock_irqrestore (&ehci->lock, flags);
676         return status ? retval : 0;
677 }
678
679 /*-------------------------------------------------------------------------*/
680
681 static void
682 ehci_hub_descriptor (
683         struct ehci_hcd                 *ehci,
684         struct usb_hub_descriptor       *desc
685 ) {
686         int             ports = HCS_N_PORTS (ehci->hcs_params);
687         u16             temp;
688
689         desc->bDescriptorType = 0x29;
690         desc->bPwrOn2PwrGood = 10;      /* ehci 1.0, 2.3.9 says 20ms max */
691         desc->bHubContrCurrent = 0;
692
693         desc->bNbrPorts = ports;
694         temp = 1 + (ports / 8);
695         desc->bDescLength = 7 + 2 * temp;
696
697         /* two bitmaps:  ports removable, and usb 1.0 legacy PortPwrCtrlMask */
698         memset (&desc->bitmap [0], 0, temp);
699         memset (&desc->bitmap [temp], 0xff, temp);
700
701         temp = 0x0008;                  /* per-port overcurrent reporting */
702         if (HCS_PPC (ehci->hcs_params))
703                 temp |= 0x0001;         /* per-port power control */
704         else
705                 temp |= 0x0002;         /* no power switching */
706 #if 0
707 // re-enable when we support USB_PORT_FEAT_INDICATOR below.
708         if (HCS_INDICATOR (ehci->hcs_params))
709                 temp |= 0x0080;         /* per-port indicators (LEDs) */
710 #endif
711         desc->wHubCharacteristics = cpu_to_le16(temp);
712 }
713
714 /*-------------------------------------------------------------------------*/
715 #ifdef CONFIG_HOST_COMPLIANT_TEST
716 struct api_context {
717         struct completion       done;
718         int                     status;
719 };
720 #if 0
721 static void ehci_test_api_blocking_completion(struct urb *urb)
722 {
723         struct api_context *ctx = urb->context;
724         printk("ehci_test_api_blocking_completion\n");
725         ctx->status = urb->status;
726         complete(&ctx->done);
727 }
728 #endif
729
730 static int single_step_get_descriptor( struct usb_hcd *hcd, u8 port)
731 {
732         struct ehci_hcd         *ehci = hcd_to_ehci (hcd);
733         struct list_head        qtd_list;
734         struct list_head        test_list;
735         struct usb_device       *udev;
736         struct ehci_qtd         *qtd;
737         struct urb              *urb;
738         struct usb_ctrlrequest  setup_packet;
739         char    data_buffer[USB_DT_DEVICE_SIZE];
740         int             retval = 0;
741
742         ehci_info (ehci, "Testing SINGLE_STEP_GET_DEV_DESC\n");
743
744         udev = hcd->self.root_hub;
745         if (udev == NULL) {
746                 ehci_err (ehci, "EHSET: root_hub pointer is NULL\n");
747                 retval = -EPIPE;
748                 goto error;
749         }
750
751         if (udev->children[port] != NULL) {
752                 udev = udev->children[port];
753         }
754
755         urb = usb_alloc_urb(0, GFP_ATOMIC);
756
757         if (!urb) {
758                 retval = -ENOMEM;
759                 goto error;
760         }
761
762         setup_packet.bRequestType = USB_DIR_IN;
763         setup_packet.bRequest = USB_REQ_GET_DESCRIPTOR;
764         setup_packet.wValue = (USB_DT_DEVICE << 8);
765         setup_packet.wIndex = 0;
766         setup_packet.wLength = USB_DT_DEVICE_SIZE;
767
768         INIT_LIST_HEAD (&qtd_list);
769         INIT_LIST_HEAD (&test_list);
770
771         urb->dev = udev;
772         urb->pipe = usb_rcvctrlpipe(udev, 0);
773         urb->hcpriv= udev->ep0.hcpriv;
774         urb->setup_packet = (char *)&setup_packet;
775         urb->transfer_buffer = data_buffer;
776         urb->transfer_flags = URB_HCD_DRIVER_TEST;
777         urb->ep = udev->ep_in[usb_pipeendpoint(urb->pipe)];
778         if (!urb->ep) {
779                 retval = -ENOENT;
780                 goto error;
781         }
782
783         urb->setup_dma = dma_map_single(
784                         hcd->self.controller,
785                         urb->setup_packet,
786                         sizeof(struct usb_ctrlrequest),
787                         DMA_TO_DEVICE);
788
789         urb->transfer_dma = dma_map_single (
790                         hcd->self.controller,
791                         urb->transfer_buffer,
792                         urb->transfer_buffer_length,
793                         DMA_TO_DEVICE);
794
795         if (!urb->setup_dma || !urb->transfer_dma) {
796                 ehci_err (ehci, "dma_map_single Failed"
797                                 "\n");
798                 retval = -EBUSY;
799                 goto error;
800         }
801
802         if (!qh_urb_transaction (ehci, urb, &qtd_list,
803                         GFP_ATOMIC)) {
804                 ehci_err (ehci, "qh_urb_transaction "
805                                 "Failed\n");
806                 retval = -EBUSY;
807                 goto error;
808         }
809
810         qtd =  container_of (qtd_list.next,
811                         struct ehci_qtd, qtd_list);
812         list_del_init (&qtd->qtd_list);
813         list_add (&qtd->qtd_list, &test_list);
814         qtd =  container_of (qtd_list.next,
815                         struct ehci_qtd, qtd_list);
816         list_del_init (&qtd->qtd_list);
817         ehci_qtd_free (ehci, qtd);
818
819         set_current_state(TASK_UNINTERRUPTIBLE);
820         schedule_timeout(msecs_to_jiffies(15000));
821
822         ehci_info (ehci, "Sending SETUP PHASE\n");
823         if (submit_async (ehci,  urb,
824                         &test_list, GFP_ATOMIC)) {
825                 ehci_err (ehci, "Failed to queue up "
826                                 "qtds\n");
827                 retval = -EBUSY;
828                 goto error;
829         }
830 error:
831         return retval;
832 }
833
834 static int single_step_set_feature( struct usb_hcd *hcd, u8 port)
835 {
836         struct ehci_hcd         *ehci = hcd_to_ehci (hcd);
837         struct usb_device       *udev;
838         struct list_head        qtd_list;
839         struct list_head        setup_list;
840         struct list_head        data_list;
841         struct ehci_qtd         *qtd;
842         struct urb              *urb;
843         struct usb_ctrlrequest  setup_packet;
844         char                    data_buffer[USB_DT_DEVICE_SIZE];
845         int             retval = 0;
846
847         ehci_info (ehci, "Testing SINGLE_STEP_SET_FEATURE\n");
848
849         udev = hcd->self.root_hub;
850         if (udev == NULL) {
851                 ehci_err (ehci, "EHSET: root_hub pointer is NULL\n");
852                 retval = -EPIPE;
853                 goto error;
854         }
855
856         if (udev->children[port] != NULL) {
857                 udev = udev->children[port];
858         }
859
860         urb = usb_alloc_urb(0, GFP_ATOMIC);
861         if (!urb) {
862                 retval = -ENOMEM;
863                 goto error;
864         }
865         setup_packet.bRequestType = USB_DIR_IN;
866         setup_packet.bRequest = USB_REQ_GET_DESCRIPTOR;
867         setup_packet.wValue = (USB_DT_DEVICE << 8);
868         setup_packet.wIndex = 0;
869         setup_packet.wLength = USB_DT_DEVICE_SIZE;
870
871         INIT_LIST_HEAD (&qtd_list);
872         INIT_LIST_HEAD (&setup_list);
873         INIT_LIST_HEAD (&data_list);
874
875         urb->transfer_buffer_length = USB_DT_DEVICE_SIZE;
876         urb->dev = udev;
877         urb->pipe = usb_rcvctrlpipe(udev, 0);
878         urb->hcpriv = udev->ep0.hcpriv;
879         urb->setup_packet = (char *)&setup_packet;
880         urb->transfer_buffer = data_buffer;
881         urb->transfer_flags = URB_HCD_DRIVER_TEST;
882         urb->ep = udev->ep_in[usb_pipeendpoint(urb->pipe)];
883         if (!urb->ep) {
884                 retval = -ENOENT;
885                 goto error;
886         }
887
888         urb->setup_dma = dma_map_single( hcd->self.controller,
889                         urb->setup_packet,
890                         sizeof (struct usb_ctrlrequest),
891                         DMA_TO_DEVICE);
892
893         urb->transfer_dma = dma_map_single (hcd->self.controller,
894                         urb->transfer_buffer,
895                         sizeof (struct usb_ctrlrequest),
896                         DMA_TO_DEVICE);
897
898         if (!urb->setup_dma || !urb->transfer_dma) {
899                 ehci_err (ehci, "dma_map_single Failed\n");
900                 retval = -EBUSY;
901                 goto error;
902         }
903
904         if (!qh_urb_transaction (ehci, urb, &qtd_list, GFP_ATOMIC)) {
905                 ehci_err (ehci, "qh_urb_transaction Failed\n");
906                 retval = -EBUSY;
907                 goto error;
908         }
909
910         qtd =  container_of (qtd_list.next, struct ehci_qtd, qtd_list);
911         list_del_init (&qtd->qtd_list);
912         list_add (&qtd->qtd_list, &setup_list);
913         qtd =  container_of (qtd_list.next, struct ehci_qtd, qtd_list);
914         list_del_init (&qtd->qtd_list);
915         list_add (&qtd->qtd_list, &data_list);
916         qtd =  container_of (qtd_list.next, struct ehci_qtd, qtd_list);
917         list_del_init (&qtd->qtd_list);
918         ehci_qtd_free (ehci, qtd);
919
920         ehci_info (ehci, "Sending SETUP PHASE\n");
921         if (submit_async (ehci, urb, &setup_list, GFP_ATOMIC)) {
922                 ehci_err (ehci, "Failed to queue up qtds\n");
923                 retval = -EBUSY;
924                 goto error;
925         }
926
927         set_current_state(TASK_UNINTERRUPTIBLE);
928         schedule_timeout(msecs_to_jiffies(15000));
929         urb->status = 0;
930         urb->actual_length = 0;
931
932         ehci_info (ehci, "Sending DATA PHASE\n");
933         if (submit_async (ehci, urb, &data_list, GFP_ATOMIC))
934         {
935                 ehci_err (ehci, "Failed to queue up qtds\n");
936                 retval = -EBUSY;
937                 goto error;
938         }
939 error:
940         return retval;
941 }
942
943 static int ehci_port_test(struct usb_hcd *hcd, u8 selector, u8 port,
944                                                         unsigned long flags)
945 {
946         struct ehci_hcd *ehci = hcd_to_ehci (hcd);
947         u32 temp;
948         u32 __iomem     *status_reg = &ehci->regs->port_status[port];
949         int             retval = 0;
950
951         temp = ehci_readl(ehci, status_reg);
952
953         ehci_info (ehci, "status_reg BEFORE write regs = 0x%x\n",temp);
954         switch (selector) {
955                 case USB_PORT_TEST_J:
956                         spin_unlock_irqrestore (&ehci->lock, flags);
957                         ehci_info (ehci, "Testing J State\n");
958                         ehci_quiesce(ehci);
959                         if(hcd->driver->bus_suspend)
960                                 hcd->driver->bus_suspend(hcd);
961                         ehci_halt(ehci);
962                         spin_lock_irqsave (&ehci->lock, flags);
963                         ehci_writel(ehci, temp|PORT_TEST_J,status_reg);
964                         break;
965
966                 case USB_PORT_TEST_K:
967                         spin_unlock_irqrestore (&ehci->lock, flags);
968                         ehci_info (ehci, "Testing K State\n");
969                         ehci_quiesce(ehci);
970                         if(hcd->driver->bus_suspend)
971                                 hcd->driver->bus_suspend(hcd);
972                         ehci_halt(ehci);
973                         spin_lock_irqsave (&ehci->lock, flags);
974                         ehci_writel(ehci, temp|PORT_TEST_K,status_reg);
975                         break;
976
977                 case USB_PORT_TEST_SE0_NAK:
978                         spin_unlock_irqrestore (&ehci->lock, flags);
979                         ehci_info (ehci, "Testing SE0_NAK\n");
980                         ehci_quiesce(ehci);
981                         if(hcd->driver->bus_suspend)
982                                 hcd->driver->bus_suspend(hcd);
983                         ehci_halt(ehci);
984                         spin_lock_irqsave (&ehci->lock, flags);
985                         ehci_writel(ehci, temp|PORT_TEST_SE0_NAK,status_reg);
986                         break;
987
988                 case USB_PORT_TEST_PACKET:
989                         ehci_info (ehci, "Sending Test Packets\n");
990                         ehci_writel(ehci, temp|PORT_TEST_PACKET,status_reg);
991                         break;
992
993                 case USB_PORT_TEST_FORCE_ENABLE:
994                         ehci_info (ehci, "Testing FORCE_ENABLE\n");
995                         ehci_writel(ehci, temp|PORT_TEST_FORCE_ENABLE,status_reg);
996                         break;
997
998                 case (EHSET_HS_HOST_PORT_SUSPEND_RESUME & 0xFF):
999                         spin_unlock_irqrestore (&ehci->lock, flags);
1000                         ehci_info (ehci, "Testing SUSPEND RESUME\n");
1001                         set_current_state(TASK_UNINTERRUPTIBLE);
1002                         schedule_timeout(msecs_to_jiffies(15000));
1003                         ehci_info (ehci, "Suspend Root Hub\n");
1004                         temp = ehci_readl(ehci, status_reg);
1005                         ehci_info(ehci, "[Before -> Suspend Status Reg : 0x%x\n",temp);
1006                         if(hcd->driver->bus_suspend)
1007                                 hcd->driver->bus_suspend(hcd);
1008                         temp = ehci_readl(ehci, status_reg);
1009                         ehci_info(ehci, "[After -> Suspend Status Reg : 0x%x\n",temp);
1010                         set_current_state(TASK_UNINTERRUPTIBLE);
1011                         schedule_timeout(msecs_to_jiffies(15000));
1012                         ehci_info (ehci, "Resume Root Hub\n");
1013                         if(hcd->driver->bus_resume)
1014                                 hcd->driver->bus_resume(hcd);
1015
1016                         spin_lock_irqsave (&ehci->lock, flags);
1017                         break;
1018
1019                 case (EHSET_SINGLE_STEP_GET_DEV_DESC&0xFF):
1020                         spin_unlock_irqrestore (&ehci->lock, flags);
1021                         retval = single_step_get_descriptor(hcd, port);
1022                         if (retval < 0) {
1023                                 ehci_err (ehci, "EHSET: get descriptor test fail\n");
1024                                 spin_lock_irqsave (&ehci->lock, flags);
1025                                 goto error;
1026                         }
1027                         spin_lock_irqsave (&ehci->lock, flags);
1028                         break;
1029
1030                 case (EHSET_SINGLE_STEP_SET_FEATURE & 0xFF):
1031                         spin_unlock_irqrestore (&ehci->lock, flags);
1032                         retval = single_step_set_feature(hcd, port);
1033                         if (retval < 0) {
1034                                 ehci_err (ehci, "EHSET: set feature test fail\n");
1035                                 spin_lock_irqsave (&ehci->lock, flags);
1036                                 goto error;
1037                         }
1038                         spin_lock_irqsave (&ehci->lock, flags);
1039                         break;
1040                 default:
1041                         ehci_err (ehci, "EHSET: Unknown test %x\n",
1042                                                         (selector));
1043                         goto error;
1044         }
1045
1046         temp = ehci_readl(ehci, status_reg);
1047         ehci_info (ehci, "status_reg AFTER write regs = 0x%x\n",temp);
1048         ehci_err(ehci, "EHSET test done. retval = 0x%x\n", retval);
1049         return retval;
1050
1051 error:
1052         ehci_err (ehci, "EHSET test error. retval = 0x%x\n",retval);
1053         return retval;
1054
1055 }
1056
1057 #endif /* CONFIG_HOST_COMPLIANT_TEST */
1058
1059 static int ehci_hub_control (
1060         struct usb_hcd  *hcd,
1061         u16             typeReq,
1062         u16             wValue,
1063         u16             wIndex,
1064         char            *buf,
1065         u16             wLength
1066 ) {
1067         struct ehci_hcd *ehci = hcd_to_ehci (hcd);
1068         int             ports = HCS_N_PORTS (ehci->hcs_params);
1069         u32 __iomem     *status_reg = &ehci->regs->port_status[
1070                                 (wIndex & 0xff) - 1];
1071         u32 __iomem     *hostpc_reg = NULL;
1072         u32             temp, temp1, status;
1073         unsigned long   flags;
1074         int             retval = 0;
1075         unsigned        selector;
1076
1077         /*
1078          * FIXME:  support SetPortFeatures USB_PORT_FEAT_INDICATOR.
1079          * HCS_INDICATOR may say we can change LEDs to off/amber/green.
1080          * (track current state ourselves) ... blink for diagnostics,
1081          * power, "this is the one", etc.  EHCI spec supports this.
1082          */
1083
1084         if (ehci->has_hostpc)
1085                 hostpc_reg = (u32 __iomem *)((u8 *)ehci->regs
1086                                 + HOSTPC0 + 4 * ((wIndex & 0xff) - 1));
1087         spin_lock_irqsave (&ehci->lock, flags);
1088         switch (typeReq) {
1089         case ClearHubFeature:
1090                 switch (wValue) {
1091                 case C_HUB_LOCAL_POWER:
1092                 case C_HUB_OVER_CURRENT:
1093                         /* no hub-wide feature/status flags */
1094                         break;
1095                 default:
1096                         goto error;
1097                 }
1098                 break;
1099         case ClearPortFeature:
1100                 if (!wIndex || wIndex > ports)
1101                         goto error;
1102                 wIndex--;
1103                 temp = ehci_readl(ehci, status_reg);
1104
1105                 /*
1106                  * Even if OWNER is set, so the port is owned by the
1107                  * companion controller, khubd needs to be able to clear
1108                  * the port-change status bits (especially
1109                  * USB_PORT_STAT_C_CONNECTION).
1110                  */
1111
1112                 switch (wValue) {
1113                 case USB_PORT_FEAT_ENABLE:
1114                         ehci_writel(ehci, temp & ~PORT_PE, status_reg);
1115                         break;
1116                 case USB_PORT_FEAT_C_ENABLE:
1117                         ehci_writel(ehci, (temp & ~PORT_RWC_BITS) | PORT_PEC,
1118                                         status_reg);
1119                         break;
1120                 case USB_PORT_FEAT_SUSPEND:
1121                         if (temp & PORT_RESET)
1122                                 goto error;
1123                         if (ehci->no_selective_suspend)
1124                                 break;
1125                         if (!(temp & PORT_SUSPEND))
1126                                 break;
1127                         if ((temp & PORT_PE) == 0)
1128                                 goto error;
1129
1130                         /* clear phy low-power mode before resume */
1131                         if (hostpc_reg) {
1132                                 temp1 = ehci_readl(ehci, hostpc_reg);
1133                                 ehci_writel(ehci, temp1 & ~HOSTPC_PHCD,
1134                                                 hostpc_reg);
1135                                 spin_unlock_irqrestore(&ehci->lock, flags);
1136                                 msleep(5);/* wait to leave low-power mode */
1137                                 spin_lock_irqsave(&ehci->lock, flags);
1138                         }
1139                         /* resume signaling for 20 msec */
1140                         temp &= ~(PORT_RWC_BITS | PORT_WAKE_BITS);
1141                         ehci_writel(ehci, temp | PORT_RESUME, status_reg);
1142                         ehci->reset_done[wIndex] = jiffies
1143                                         + msecs_to_jiffies(20);
1144                         break;
1145                 case USB_PORT_FEAT_C_SUSPEND:
1146                         clear_bit(wIndex, &ehci->port_c_suspend);
1147                         break;
1148                 case USB_PORT_FEAT_POWER:
1149                         if (HCS_PPC (ehci->hcs_params))
1150                                 ehci_writel(ehci,
1151                                           temp & ~(PORT_RWC_BITS | PORT_POWER),
1152                                           status_reg);
1153                         break;
1154                 case USB_PORT_FEAT_C_CONNECTION:
1155                         if (ehci->has_lpm) {
1156                                 /* clear PORTSC bits on disconnect */
1157                                 temp &= ~PORT_LPM;
1158                                 temp &= ~PORT_DEV_ADDR;
1159                         }
1160                         ehci_writel(ehci, (temp & ~PORT_RWC_BITS) | PORT_CSC,
1161                                         status_reg);
1162                         break;
1163                 case USB_PORT_FEAT_C_OVER_CURRENT:
1164                         ehci_writel(ehci, (temp & ~PORT_RWC_BITS) | PORT_OCC,
1165                                         status_reg);
1166                         break;
1167                 case USB_PORT_FEAT_C_RESET:
1168                         /* GetPortStatus clears reset */
1169                         break;
1170                 default:
1171                         goto error;
1172                 }
1173                 ehci_readl(ehci, &ehci->regs->command); /* unblock posted write */
1174                 break;
1175         case GetHubDescriptor:
1176                 ehci_hub_descriptor (ehci, (struct usb_hub_descriptor *)
1177                         buf);
1178                 break;
1179         case GetHubStatus:
1180                 /* no hub-wide feature/status flags */
1181                 memset (buf, 0, 4);
1182                 //cpu_to_le32s ((u32 *) buf);
1183                 break;
1184         case GetPortStatus:
1185                 if (!wIndex || wIndex > ports)
1186                         goto error;
1187                 wIndex--;
1188                 status = 0;
1189                 temp = ehci_readl(ehci, status_reg);
1190
1191                 // wPortChange bits
1192                 if (temp & PORT_CSC)
1193                         status |= USB_PORT_STAT_C_CONNECTION << 16;
1194                 if (temp & PORT_PEC)
1195                         status |= USB_PORT_STAT_C_ENABLE << 16;
1196
1197                 if ((temp & PORT_OCC) && !ignore_oc){
1198                         status |= USB_PORT_STAT_C_OVERCURRENT << 16;
1199
1200                         /*
1201                          * Hubs should disable port power on over-current.
1202                          * However, not all EHCI implementations do this
1203                          * automatically, even if they _do_ support per-port
1204                          * power switching; they're allowed to just limit the
1205                          * current.  khubd will turn the power back on.
1206                          */
1207                         if (HCS_PPC (ehci->hcs_params)){
1208                                 ehci_writel(ehci,
1209                                         temp & ~(PORT_RWC_BITS | PORT_POWER),
1210                                         status_reg);
1211                         }
1212                 }
1213
1214                 /* whoever resumes must GetPortStatus to complete it!! */
1215                 if (temp & PORT_RESUME) {
1216
1217                         /* Remote Wakeup received? */
1218                         if (!ehci->reset_done[wIndex]) {
1219                                 /* resume signaling for 20 msec */
1220                                 ehci->reset_done[wIndex] = jiffies
1221                                                 + msecs_to_jiffies(20);
1222                                 /* check the port again */
1223                                 mod_timer(&ehci_to_hcd(ehci)->rh_timer,
1224                                                 ehci->reset_done[wIndex]);
1225                         }
1226
1227                         /* resume completed? */
1228                         else if (time_after_eq(jiffies,
1229                                         ehci->reset_done[wIndex])) {
1230                                 clear_bit(wIndex, &ehci->suspended_ports);
1231                                 set_bit(wIndex, &ehci->port_c_suspend);
1232                                 ehci->reset_done[wIndex] = 0;
1233
1234                                 /* stop resume signaling */
1235                                 temp = ehci_readl(ehci, status_reg);
1236                                 ehci_writel(ehci,
1237                                         temp & ~(PORT_RWC_BITS | PORT_RESUME),
1238                                         status_reg);
1239                                 retval = handshake(ehci, status_reg,
1240                                            PORT_RESUME, 0, 2000 /* 2msec */);
1241                                 if (retval != 0) {
1242                                         ehci_err(ehci,
1243                                                 "port %d resume error %d\n",
1244                                                 wIndex + 1, retval);
1245                                         goto error;
1246                                 }
1247                                 temp &= ~(PORT_SUSPEND|PORT_RESUME|(3<<10));
1248                         }
1249                 }
1250
1251                 /* whoever resets must GetPortStatus to complete it!! */
1252                 if ((temp & PORT_RESET)
1253                                 && time_after_eq(jiffies,
1254                                         ehci->reset_done[wIndex])) {
1255                         status |= USB_PORT_STAT_C_RESET << 16;
1256                         ehci->reset_done [wIndex] = 0;
1257
1258                         /* force reset to complete */
1259                         ehci_writel(ehci, temp & ~(PORT_RWC_BITS | PORT_RESET),
1260                                         status_reg);
1261                         /* REVISIT:  some hardware needs 550+ usec to clear
1262                          * this bit; seems too long to spin routinely...
1263                          */
1264                         retval = handshake(ehci, status_reg,
1265                                         PORT_RESET, 0, 1000);
1266                         if (retval != 0) {
1267                                 ehci_err (ehci, "port %d reset error %d\n",
1268                                         wIndex + 1, retval);
1269                                 goto error;
1270                         }
1271
1272                         /* see what we found out */
1273                         temp = check_reset_complete (ehci, wIndex, status_reg,
1274                                         ehci_readl(ehci, status_reg));
1275                 }
1276
1277                 if (!(temp & (PORT_RESUME|PORT_RESET)))
1278                         ehci->reset_done[wIndex] = 0;
1279
1280                 /* transfer dedicated ports to the companion hc */
1281                 if ((temp & PORT_CONNECT) &&
1282                                 test_bit(wIndex, &ehci->companion_ports)) {
1283                         temp &= ~PORT_RWC_BITS;
1284                         temp |= PORT_OWNER;
1285                         ehci_writel(ehci, temp, status_reg);
1286                         ehci_dbg(ehci, "port %d --> companion\n", wIndex + 1);
1287                         temp = ehci_readl(ehci, status_reg);
1288                 }
1289
1290                 /*
1291                  * Even if OWNER is set, there's no harm letting khubd
1292                  * see the wPortStatus values (they should all be 0 except
1293                  * for PORT_POWER anyway).
1294                  */
1295
1296                 if (temp & PORT_CONNECT) {
1297                         status |= USB_PORT_STAT_CONNECTION;
1298                         // status may be from integrated TT
1299                         if (ehci->has_hostpc) {
1300                                 temp1 = ehci_readl(ehci, hostpc_reg);
1301                                 status |= ehci_port_speed(ehci, temp1);
1302                         } else
1303                                 status |= ehci_port_speed(ehci, temp);
1304                 }
1305                 if (temp & PORT_PE)
1306                         status |= USB_PORT_STAT_ENABLE;
1307
1308                 /* maybe the port was unsuspended without our knowledge */
1309                 if (temp & (PORT_SUSPEND|PORT_RESUME)) {
1310                         status |= USB_PORT_STAT_SUSPEND;
1311                 } else if (test_bit(wIndex, &ehci->suspended_ports)) {
1312                         clear_bit(wIndex, &ehci->suspended_ports);
1313                         ehci->reset_done[wIndex] = 0;
1314                         if (temp & PORT_PE)
1315                                 set_bit(wIndex, &ehci->port_c_suspend);
1316                 }
1317
1318                 if (temp & PORT_OC)
1319                         status |= USB_PORT_STAT_OVERCURRENT;
1320                 if (temp & PORT_RESET)
1321                         status |= USB_PORT_STAT_RESET;
1322                 if (temp & PORT_POWER)
1323                         status |= USB_PORT_STAT_POWER;
1324                 if (test_bit(wIndex, &ehci->port_c_suspend))
1325                         status |= USB_PORT_STAT_C_SUSPEND << 16;
1326
1327 #ifndef VERBOSE_DEBUG
1328         if (status & ~0xffff)   /* only if wPortChange is interesting */
1329 #endif
1330                 dbg_port (ehci, "GetStatus", wIndex + 1, temp);
1331                 put_unaligned_le32(status, buf);
1332                 break;
1333         case SetHubFeature:
1334                 switch (wValue) {
1335                 case C_HUB_LOCAL_POWER:
1336                 case C_HUB_OVER_CURRENT:
1337                         /* no hub-wide feature/status flags */
1338                         break;
1339                 default:
1340                         goto error;
1341                 }
1342                 break;
1343         case SetPortFeature:
1344                 selector = wIndex >> 8;
1345                 wIndex &= 0xff;
1346                 if (unlikely(ehci->debug)) {
1347                         /* If the debug port is active any port
1348                          * feature requests should get denied */
1349                         if (wIndex == HCS_DEBUG_PORT(ehci->hcs_params) &&
1350                             (readl(&ehci->debug->control) & DBGP_ENABLED)) {
1351                                 retval = -ENODEV;
1352                                 goto error_exit;
1353                         }
1354                 }
1355                 if (!wIndex || wIndex > ports)
1356                         goto error;
1357                 wIndex--;
1358                 temp = ehci_readl(ehci, status_reg);
1359                 if (temp & PORT_OWNER)
1360                         break;
1361
1362                 temp &= ~PORT_RWC_BITS;
1363                 switch (wValue) {
1364                 case USB_PORT_FEAT_SUSPEND:
1365                         if (ehci->no_selective_suspend)
1366                                 break;
1367                         if ((temp & PORT_PE) == 0
1368                                         || (temp & PORT_RESET) != 0)
1369                                 goto error;
1370
1371                         /* After above check the port must be connected.
1372                          * Set appropriate bit thus could put phy into low power
1373                          * mode if we have hostpc feature
1374                          */
1375                         temp &= ~PORT_WKCONN_E;
1376                         temp |= PORT_WKDISC_E | PORT_WKOC_E;
1377                         ehci_writel(ehci, temp | PORT_SUSPEND, status_reg);
1378                         if (hostpc_reg) {
1379                                 spin_unlock_irqrestore(&ehci->lock, flags);
1380                                 msleep(5);/* 5ms for HCD enter low pwr mode */
1381                                 spin_lock_irqsave(&ehci->lock, flags);
1382                                 temp1 = ehci_readl(ehci, hostpc_reg);
1383                                 ehci_writel(ehci, temp1 | HOSTPC_PHCD,
1384                                         hostpc_reg);
1385                                 temp1 = ehci_readl(ehci, hostpc_reg);
1386                                 ehci_dbg(ehci, "Port%d phy low pwr mode %s\n",
1387                                         wIndex, (temp1 & HOSTPC_PHCD) ?
1388                                         "succeeded" : "failed");
1389                         }
1390                         set_bit(wIndex, &ehci->suspended_ports);
1391                         break;
1392                 case USB_PORT_FEAT_POWER:
1393                         if (HCS_PPC (ehci->hcs_params))
1394                                 ehci_writel(ehci, temp | PORT_POWER,
1395                                                 status_reg);
1396                         break;
1397                 case USB_PORT_FEAT_RESET:
1398                         if (temp & PORT_RESUME)
1399                                 goto error;
1400                         /* line status bits may report this as low speed,
1401                          * which can be fine if this root hub has a
1402                          * transaction translator built in.
1403                          */
1404                         if ((temp & (PORT_PE|PORT_CONNECT)) == PORT_CONNECT
1405                                         && !ehci_is_TDI(ehci)
1406                                         && PORT_USB11 (temp)) {
1407                                 ehci_dbg (ehci,
1408                                         "port %d low speed --> companion\n",
1409                                         wIndex + 1);
1410                                 temp |= PORT_OWNER;
1411                         } else {
1412                                 ehci_vdbg (ehci, "port %d reset\n", wIndex + 1);
1413                                 temp |= PORT_RESET;
1414                                 temp &= ~PORT_PE;
1415
1416                                 /*
1417                                  * caller must wait, then call GetPortStatus
1418                                  * usb 2.0 spec says 50 ms resets on root
1419                                  */
1420                                 ehci->reset_done [wIndex] = jiffies
1421                                                 + msecs_to_jiffies (50);
1422                         }
1423                         ehci_writel(ehci, temp, status_reg);
1424                         break;
1425
1426                 /* For downstream facing ports (these):  one hub port is put
1427                  * into test mode according to USB2 11.24.2.13, then the hub
1428                  * must be reset (which for root hub now means rmmod+modprobe,
1429                  * or else system reboot).  See EHCI 2.3.9 and 4.14 for info
1430                  * about the EHCI-specific stuff.
1431                  */
1432 #ifdef CONFIG_HOST_COMPLIANT_TEST
1433                 case USB_PORT_FEAT_TEST:
1434                         ehci_info (ehci, "TEST MODE !!!!!!!!  selector == 0x%x \n",selector);
1435
1436                         ehci_info (ehci, "running EHCI test %x on port %x\n",
1437                                         selector, wIndex);
1438
1439                         retval = ehci_port_test(hcd, selector & 0xFF, wIndex, flags);
1440                         if (retval < 0) {
1441                                 ehci_info (ehci, "EHCI test Fail!!\n");
1442                                 goto error;
1443                         }
1444                         break;
1445 #endif
1446                         ehci_quiesce(ehci);
1447                         ehci_halt(ehci);
1448                         temp |= selector << 16;
1449                         ehci_writel(ehci, temp, status_reg);
1450                         break;
1451
1452                 default:
1453                         goto error;
1454                 }
1455                 ehci_readl(ehci, &ehci->regs->command); /* unblock posted writes */
1456                 break;
1457
1458         default:
1459 error:
1460                 /* "stall" on error */
1461                 retval = -EPIPE;
1462         }
1463 error_exit:
1464         spin_unlock_irqrestore (&ehci->lock, flags);
1465         return retval;
1466 }
1467
1468 static void ehci_relinquish_port(struct usb_hcd *hcd, int portnum)
1469 {
1470         struct ehci_hcd         *ehci = hcd_to_ehci(hcd);
1471
1472         if (ehci_is_TDI(ehci))
1473                 return;
1474         set_owner(ehci, --portnum, PORT_OWNER);
1475 }
1476
1477 static int ehci_port_handed_over(struct usb_hcd *hcd, int portnum)
1478 {
1479         struct ehci_hcd         *ehci = hcd_to_ehci(hcd);
1480         u32 __iomem             *reg;
1481
1482         if (ehci_is_TDI(ehci))
1483                 return 0;
1484         reg = &ehci->regs->port_status[portnum - 1];
1485         return ehci_readl(ehci, reg) & PORT_OWNER;
1486 }