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