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