90cee192e96db5e968bc6889748035554c464699
[releases.git] / ohci-hub.c
1 // SPDX-License-Identifier: GPL-1.0+
2 /*
3  * OHCI HCD (Host Controller Driver) for USB.
4  *
5  * (C) Copyright 1999 Roman Weissgaerber <weissg@vienna.at>
6  * (C) Copyright 2000-2004 David Brownell <dbrownell@users.sourceforge.net>
7  *
8  * This file is licenced under GPL
9  */
10
11 /*-------------------------------------------------------------------------*/
12
13 /*
14  * OHCI Root Hub ... the nonsharable stuff
15  */
16
17 #define dbg_port(hc,label,num,value) \
18         ohci_dbg (hc, \
19                 "%s roothub.portstatus [%d] " \
20                 "= 0x%08x%s%s%s%s%s%s%s%s%s%s%s%s\n", \
21                 label, num, value, \
22                 (value & RH_PS_PRSC) ? " PRSC" : "", \
23                 (value & RH_PS_OCIC) ? " OCIC" : "", \
24                 (value & RH_PS_PSSC) ? " PSSC" : "", \
25                 (value & RH_PS_PESC) ? " PESC" : "", \
26                 (value & RH_PS_CSC) ? " CSC" : "", \
27                 \
28                 (value & RH_PS_LSDA) ? " LSDA" : "", \
29                 (value & RH_PS_PPS) ? " PPS" : "", \
30                 (value & RH_PS_PRS) ? " PRS" : "", \
31                 (value & RH_PS_POCI) ? " POCI" : "", \
32                 (value & RH_PS_PSS) ? " PSS" : "", \
33                 \
34                 (value & RH_PS_PES) ? " PES" : "", \
35                 (value & RH_PS_CCS) ? " CCS" : "" \
36                 );
37
38 /*-------------------------------------------------------------------------*/
39
40 #define OHCI_SCHED_ENABLES \
41         (OHCI_CTRL_CLE|OHCI_CTRL_BLE|OHCI_CTRL_PLE|OHCI_CTRL_IE)
42
43 static void update_done_list(struct ohci_hcd *);
44 static void ohci_work(struct ohci_hcd *);
45
46 #ifdef  CONFIG_PM
47 static int ohci_rh_suspend (struct ohci_hcd *ohci, int autostop)
48 __releases(ohci->lock)
49 __acquires(ohci->lock)
50 {
51         int                     status = 0;
52
53         ohci->hc_control = ohci_readl (ohci, &ohci->regs->control);
54         switch (ohci->hc_control & OHCI_CTRL_HCFS) {
55         case OHCI_USB_RESUME:
56                 ohci_dbg (ohci, "resume/suspend?\n");
57                 ohci->hc_control &= ~OHCI_CTRL_HCFS;
58                 ohci->hc_control |= OHCI_USB_RESET;
59                 ohci_writel (ohci, ohci->hc_control, &ohci->regs->control);
60                 (void) ohci_readl (ohci, &ohci->regs->control);
61                 fallthrough;
62         case OHCI_USB_RESET:
63                 status = -EBUSY;
64                 ohci_dbg (ohci, "needs reinit!\n");
65                 goto done;
66         case OHCI_USB_SUSPEND:
67                 if (!ohci->autostop) {
68                         ohci_dbg (ohci, "already suspended\n");
69                         goto done;
70                 }
71         }
72         ohci_dbg (ohci, "%s root hub\n",
73                         autostop ? "auto-stop" : "suspend");
74
75         /* First stop any processing */
76         if (!autostop && (ohci->hc_control & OHCI_SCHED_ENABLES)) {
77                 ohci->hc_control &= ~OHCI_SCHED_ENABLES;
78                 ohci_writel (ohci, ohci->hc_control, &ohci->regs->control);
79                 ohci->hc_control = ohci_readl (ohci, &ohci->regs->control);
80                 ohci_writel (ohci, OHCI_INTR_SF, &ohci->regs->intrstatus);
81
82                 /* sched disables take effect on the next frame,
83                  * then the last WDH could take 6+ msec
84                  */
85                 ohci_dbg (ohci, "stopping schedules ...\n");
86                 ohci->autostop = 0;
87                 spin_unlock_irq (&ohci->lock);
88                 msleep (8);
89                 spin_lock_irq (&ohci->lock);
90         }
91         update_done_list(ohci);
92         ohci_work(ohci);
93
94         /* All ED unlinks should be finished, no need for SOF interrupts */
95         ohci_writel(ohci, OHCI_INTR_SF, &ohci->regs->intrdisable);
96
97         /*
98          * Some controllers don't handle "global" suspend properly if
99          * there are unsuspended ports.  For these controllers, put all
100          * the enabled ports into suspend before suspending the root hub.
101          */
102         if (ohci->flags & OHCI_QUIRK_GLOBAL_SUSPEND) {
103                 __hc32 __iomem  *portstat = ohci->regs->roothub.portstatus;
104                 int             i;
105                 unsigned        temp;
106
107                 for (i = 0; i < ohci->num_ports; (++i, ++portstat)) {
108                         temp = ohci_readl(ohci, portstat);
109                         if ((temp & (RH_PS_PES | RH_PS_PSS)) ==
110                                         RH_PS_PES)
111                                 ohci_writel(ohci, RH_PS_PSS, portstat);
112                 }
113         }
114
115         /* maybe resume can wake root hub */
116         if (ohci_to_hcd(ohci)->self.root_hub->do_remote_wakeup || autostop) {
117                 ohci->hc_control |= OHCI_CTRL_RWE;
118         } else {
119                 ohci_writel(ohci, OHCI_INTR_RHSC | OHCI_INTR_RD,
120                                 &ohci->regs->intrdisable);
121                 ohci->hc_control &= ~OHCI_CTRL_RWE;
122         }
123
124         /* Suspend hub ... this is the "global (to this bus) suspend" mode,
125          * which doesn't imply ports will first be individually suspended.
126          */
127         ohci->hc_control &= ~OHCI_CTRL_HCFS;
128         ohci->hc_control |= OHCI_USB_SUSPEND;
129         ohci_writel (ohci, ohci->hc_control, &ohci->regs->control);
130         (void) ohci_readl (ohci, &ohci->regs->control);
131
132         /* no resumes until devices finish suspending */
133         if (!autostop) {
134                 ohci->next_statechange = jiffies + msecs_to_jiffies (5);
135                 ohci->autostop = 0;
136                 ohci->rh_state = OHCI_RH_SUSPENDED;
137         }
138
139 done:
140         return status;
141 }
142
143 static inline struct ed *find_head (struct ed *ed)
144 {
145         /* for bulk and control lists */
146         while (ed->ed_prev)
147                 ed = ed->ed_prev;
148         return ed;
149 }
150
151 /* caller has locked the root hub */
152 static int ohci_rh_resume (struct ohci_hcd *ohci)
153 __releases(ohci->lock)
154 __acquires(ohci->lock)
155 {
156         struct usb_hcd          *hcd = ohci_to_hcd (ohci);
157         u32                     temp, enables;
158         int                     status = -EINPROGRESS;
159         int                     autostopped = ohci->autostop;
160
161         ohci->autostop = 0;
162         ohci->hc_control = ohci_readl (ohci, &ohci->regs->control);
163
164         if (ohci->hc_control & (OHCI_CTRL_IR | OHCI_SCHED_ENABLES)) {
165                 /* this can happen after resuming a swsusp snapshot */
166                 if (ohci->rh_state != OHCI_RH_RUNNING) {
167                         ohci_dbg (ohci, "BIOS/SMM active, control %03x\n",
168                                         ohci->hc_control);
169                         status = -EBUSY;
170                 /* this happens when pmcore resumes HC then root */
171                 } else {
172                         ohci_dbg (ohci, "duplicate resume\n");
173                         status = 0;
174                 }
175         } else switch (ohci->hc_control & OHCI_CTRL_HCFS) {
176         case OHCI_USB_SUSPEND:
177                 ohci->hc_control &= ~(OHCI_CTRL_HCFS|OHCI_SCHED_ENABLES);
178                 ohci->hc_control |= OHCI_USB_RESUME;
179                 ohci_writel (ohci, ohci->hc_control, &ohci->regs->control);
180                 (void) ohci_readl (ohci, &ohci->regs->control);
181                 ohci_dbg (ohci, "%s root hub\n",
182                                 autostopped ? "auto-start" : "resume");
183                 break;
184         case OHCI_USB_RESUME:
185                 /* HCFS changes sometime after INTR_RD */
186                 ohci_dbg(ohci, "%swakeup root hub\n",
187                                 autostopped ? "auto-" : "");
188                 break;
189         case OHCI_USB_OPER:
190                 /* this can happen after resuming a swsusp snapshot */
191                 ohci_dbg (ohci, "snapshot resume? reinit\n");
192                 status = -EBUSY;
193                 break;
194         default:                /* RESET, we lost power */
195                 ohci_dbg (ohci, "lost power\n");
196                 status = -EBUSY;
197         }
198         if (status == -EBUSY) {
199                 if (!autostopped) {
200                         spin_unlock_irq (&ohci->lock);
201                         status = ohci_restart (ohci);
202
203                         usb_root_hub_lost_power(hcd->self.root_hub);
204
205                         spin_lock_irq (&ohci->lock);
206                 }
207                 return status;
208         }
209         if (status != -EINPROGRESS)
210                 return status;
211         if (autostopped)
212                 goto skip_resume;
213         spin_unlock_irq (&ohci->lock);
214
215         /* Some controllers (lucent erratum) need extra-long delays */
216         msleep (20 /* usb 11.5.1.10 */ + 12 /* 32 msec counter */ + 1);
217
218         temp = ohci_readl (ohci, &ohci->regs->control);
219         temp &= OHCI_CTRL_HCFS;
220         if (temp != OHCI_USB_RESUME) {
221                 ohci_err (ohci, "controller won't resume\n");
222                 spin_lock_irq(&ohci->lock);
223                 return -EBUSY;
224         }
225
226         /* disable old schedule state, reinit from scratch */
227         ohci_writel (ohci, 0, &ohci->regs->ed_controlhead);
228         ohci_writel (ohci, 0, &ohci->regs->ed_controlcurrent);
229         ohci_writel (ohci, 0, &ohci->regs->ed_bulkhead);
230         ohci_writel (ohci, 0, &ohci->regs->ed_bulkcurrent);
231         ohci_writel (ohci, 0, &ohci->regs->ed_periodcurrent);
232         ohci_writel (ohci, (u32) ohci->hcca_dma, &ohci->regs->hcca);
233
234         /* Sometimes PCI D3 suspend trashes frame timings ... */
235         periodic_reinit (ohci);
236
237         /*
238          * The following code is executed with ohci->lock held and
239          * irqs disabled if and only if autostopped is true.  This
240          * will cause sparse to warn about a "context imbalance".
241          */
242 skip_resume:
243         /* interrupts might have been disabled */
244         ohci_writel (ohci, OHCI_INTR_INIT, &ohci->regs->intrenable);
245         if (ohci->ed_rm_list)
246                 ohci_writel (ohci, OHCI_INTR_SF, &ohci->regs->intrenable);
247
248         /* Then re-enable operations */
249         ohci_writel (ohci, OHCI_USB_OPER, &ohci->regs->control);
250         (void) ohci_readl (ohci, &ohci->regs->control);
251         if (!autostopped)
252                 msleep (3);
253
254         temp = ohci->hc_control;
255         temp &= OHCI_CTRL_RWC;
256         temp |= OHCI_CONTROL_INIT | OHCI_USB_OPER;
257         ohci->hc_control = temp;
258         ohci_writel (ohci, temp, &ohci->regs->control);
259         (void) ohci_readl (ohci, &ohci->regs->control);
260
261         /* TRSMRCY */
262         if (!autostopped) {
263                 msleep (10);
264                 spin_lock_irq (&ohci->lock);
265         }
266         /* now ohci->lock is always held and irqs are always disabled */
267
268         /* keep it alive for more than ~5x suspend + resume costs */
269         ohci->next_statechange = jiffies + STATECHANGE_DELAY;
270
271         /* maybe turn schedules back on */
272         enables = 0;
273         temp = 0;
274         if (!ohci->ed_rm_list) {
275                 if (ohci->ed_controltail) {
276                         ohci_writel (ohci,
277                                         find_head (ohci->ed_controltail)->dma,
278                                         &ohci->regs->ed_controlhead);
279                         enables |= OHCI_CTRL_CLE;
280                         temp |= OHCI_CLF;
281                 }
282                 if (ohci->ed_bulktail) {
283                         ohci_writel (ohci, find_head (ohci->ed_bulktail)->dma,
284                                 &ohci->regs->ed_bulkhead);
285                         enables |= OHCI_CTRL_BLE;
286                         temp |= OHCI_BLF;
287                 }
288         }
289         if (hcd->self.bandwidth_isoc_reqs || hcd->self.bandwidth_int_reqs)
290                 enables |= OHCI_CTRL_PLE|OHCI_CTRL_IE;
291         if (enables) {
292                 ohci_dbg (ohci, "restarting schedules ... %08x\n", enables);
293                 ohci->hc_control |= enables;
294                 ohci_writel (ohci, ohci->hc_control, &ohci->regs->control);
295                 if (temp)
296                         ohci_writel (ohci, temp, &ohci->regs->cmdstatus);
297                 (void) ohci_readl (ohci, &ohci->regs->control);
298         }
299
300         ohci->rh_state = OHCI_RH_RUNNING;
301         return 0;
302 }
303
304 static int ohci_bus_suspend (struct usb_hcd *hcd)
305 {
306         struct ohci_hcd         *ohci = hcd_to_ohci (hcd);
307         int                     rc;
308
309         spin_lock_irq (&ohci->lock);
310
311         if (unlikely(!HCD_HW_ACCESSIBLE(hcd)))
312                 rc = -ESHUTDOWN;
313         else
314                 rc = ohci_rh_suspend (ohci, 0);
315         spin_unlock_irq (&ohci->lock);
316
317         if (rc == 0) {
318                 del_timer_sync(&ohci->io_watchdog);
319                 ohci->prev_frame_no = IO_WATCHDOG_OFF;
320         }
321         return rc;
322 }
323
324 static int ohci_bus_resume (struct usb_hcd *hcd)
325 {
326         struct ohci_hcd         *ohci = hcd_to_ohci (hcd);
327         int                     rc;
328
329         if (time_before (jiffies, ohci->next_statechange))
330                 msleep(5);
331
332         spin_lock_irq (&ohci->lock);
333
334         if (unlikely(!HCD_HW_ACCESSIBLE(hcd)))
335                 rc = -ESHUTDOWN;
336         else
337                 rc = ohci_rh_resume (ohci);
338         spin_unlock_irq (&ohci->lock);
339
340         /* poll until we know a device is connected or we autostop */
341         if (rc == 0)
342                 usb_hcd_poll_rh_status(hcd);
343         return rc;
344 }
345
346 /* Carry out polling-, autostop-, and autoresume-related state changes */
347 static int ohci_root_hub_state_changes(struct ohci_hcd *ohci, int changed,
348                 int any_connected, int rhsc_status)
349 {
350         int     poll_rh = 1;
351         int     rhsc_enable;
352
353         /* Some broken controllers never turn off RHSC in the interrupt
354          * status register.  For their sake we won't re-enable RHSC
355          * interrupts if the interrupt bit is already active.
356          */
357         rhsc_enable = ohci_readl(ohci, &ohci->regs->intrenable) &
358                         OHCI_INTR_RHSC;
359
360         switch (ohci->hc_control & OHCI_CTRL_HCFS) {
361         case OHCI_USB_OPER:
362                 /* If no status changes are pending, enable RHSC interrupts. */
363                 if (!rhsc_enable && !rhsc_status && !changed) {
364                         rhsc_enable = OHCI_INTR_RHSC;
365                         ohci_writel(ohci, rhsc_enable, &ohci->regs->intrenable);
366                 }
367
368                 /* Keep on polling until we know a device is connected
369                  * and RHSC is enabled, or until we autostop.
370                  */
371                 if (!ohci->autostop) {
372                         if (any_connected ||
373                                         !device_may_wakeup(&ohci_to_hcd(ohci)
374                                                 ->self.root_hub->dev)) {
375                                 if (rhsc_enable)
376                                         poll_rh = 0;
377                         } else {
378                                 ohci->autostop = 1;
379                                 ohci->next_statechange = jiffies + HZ;
380                         }
381
382                 /* if no devices have been attached for one second, autostop */
383                 } else {
384                         if (changed || any_connected) {
385                                 ohci->autostop = 0;
386                                 ohci->next_statechange = jiffies +
387                                                 STATECHANGE_DELAY;
388                         } else if (time_after_eq(jiffies,
389                                                 ohci->next_statechange)
390                                         && !ohci->ed_rm_list
391                                         && !(ohci->hc_control &
392                                                 OHCI_SCHED_ENABLES)) {
393                                 ohci_rh_suspend(ohci, 1);
394                                 if (rhsc_enable)
395                                         poll_rh = 0;
396                         }
397                 }
398                 break;
399
400         case OHCI_USB_SUSPEND:
401         case OHCI_USB_RESUME:
402                 /* if there is a port change, autostart or ask to be resumed */
403                 if (changed) {
404                         if (ohci->autostop)
405                                 ohci_rh_resume(ohci);
406                         else
407                                 usb_hcd_resume_root_hub(ohci_to_hcd(ohci));
408
409                 /* If remote wakeup is disabled, stop polling */
410                 } else if (!ohci->autostop &&
411                                 !ohci_to_hcd(ohci)->self.root_hub->
412                                         do_remote_wakeup) {
413                         poll_rh = 0;
414
415                 } else {
416                         /* If no status changes are pending,
417                          * enable RHSC interrupts
418                          */
419                         if (!rhsc_enable && !rhsc_status) {
420                                 rhsc_enable = OHCI_INTR_RHSC;
421                                 ohci_writel(ohci, rhsc_enable,
422                                                 &ohci->regs->intrenable);
423                         }
424                         /* Keep polling until RHSC is enabled */
425                         if (rhsc_enable)
426                                 poll_rh = 0;
427                 }
428                 break;
429         }
430         return poll_rh;
431 }
432
433 #else   /* CONFIG_PM */
434
435 static inline int ohci_rh_resume(struct ohci_hcd *ohci)
436 {
437         return 0;
438 }
439
440 /* Carry out polling-related state changes.
441  * autostop isn't used when CONFIG_PM is turned off.
442  */
443 static int ohci_root_hub_state_changes(struct ohci_hcd *ohci, int changed,
444                 int any_connected, int rhsc_status)
445 {
446         /* If RHSC is enabled, don't poll */
447         if (ohci_readl(ohci, &ohci->regs->intrenable) & OHCI_INTR_RHSC)
448                 return 0;
449
450         /* If status changes are pending, continue polling.
451          * Conversely, if no status changes are pending but the RHSC
452          * status bit was set, then RHSC may be broken so continue polling.
453          */
454         if (changed || rhsc_status)
455                 return 1;
456
457         /* It's safe to re-enable RHSC interrupts */
458         ohci_writel(ohci, OHCI_INTR_RHSC, &ohci->regs->intrenable);
459         return 0;
460 }
461
462 #endif  /* CONFIG_PM */
463
464 /*-------------------------------------------------------------------------*/
465
466 /* build "status change" packet (one or two bytes) from HC registers */
467
468 int ohci_hub_status_data(struct usb_hcd *hcd, char *buf)
469 {
470         struct ohci_hcd *ohci = hcd_to_ohci (hcd);
471         int             i, changed = 0, length = 1;
472         int             any_connected = 0;
473         int             rhsc_status;
474         unsigned long   flags;
475
476         spin_lock_irqsave (&ohci->lock, flags);
477         if (!HCD_HW_ACCESSIBLE(hcd))
478                 goto done;
479
480         /* undocumented erratum seen on at least rev D */
481         if ((ohci->flags & OHCI_QUIRK_AMD756)
482                         && (roothub_a (ohci) & RH_A_NDP) > MAX_ROOT_PORTS) {
483                 ohci_warn (ohci, "bogus NDP, rereads as NDP=%d\n",
484                           ohci_readl (ohci, &ohci->regs->roothub.a) & RH_A_NDP);
485                 /* retry later; "should not happen" */
486                 goto done;
487         }
488
489         /* init status */
490         if (roothub_status (ohci) & (RH_HS_LPSC | RH_HS_OCIC))
491                 buf [0] = changed = 1;
492         else
493                 buf [0] = 0;
494         if (ohci->num_ports > 7) {
495                 buf [1] = 0;
496                 length++;
497         }
498
499         /* Clear the RHSC status flag before reading the port statuses */
500         ohci_writel(ohci, OHCI_INTR_RHSC, &ohci->regs->intrstatus);
501         rhsc_status = ohci_readl(ohci, &ohci->regs->intrstatus) &
502                         OHCI_INTR_RHSC;
503
504         /* look at each port */
505         for (i = 0; i < ohci->num_ports; i++) {
506                 u32     status = roothub_portstatus (ohci, i);
507
508                 /* can't autostop if ports are connected */
509                 any_connected |= (status & RH_PS_CCS);
510
511                 if (status & (RH_PS_CSC | RH_PS_PESC | RH_PS_PSSC
512                                 | RH_PS_OCIC | RH_PS_PRSC)) {
513                         changed = 1;
514                         if (i < 7)
515                             buf [0] |= 1 << (i + 1);
516                         else
517                             buf [1] |= 1 << (i - 7);
518                 }
519         }
520
521         if (ohci_root_hub_state_changes(ohci, changed,
522                         any_connected, rhsc_status))
523                 set_bit(HCD_FLAG_POLL_RH, &hcd->flags);
524         else
525                 clear_bit(HCD_FLAG_POLL_RH, &hcd->flags);
526
527
528 done:
529         spin_unlock_irqrestore (&ohci->lock, flags);
530
531         return changed ? length : 0;
532 }
533 EXPORT_SYMBOL_GPL(ohci_hub_status_data);
534
535 /*-------------------------------------------------------------------------*/
536
537 static void
538 ohci_hub_descriptor (
539         struct ohci_hcd                 *ohci,
540         struct usb_hub_descriptor       *desc
541 ) {
542         u32             rh = roothub_a (ohci);
543         u16             temp;
544
545         desc->bDescriptorType = USB_DT_HUB;
546         desc->bPwrOn2PwrGood = (rh & RH_A_POTPGT) >> 24;
547         desc->bHubContrCurrent = 0;
548
549         desc->bNbrPorts = ohci->num_ports;
550         temp = 1 + (ohci->num_ports / 8);
551         desc->bDescLength = 7 + 2 * temp;
552
553         temp = HUB_CHAR_COMMON_LPSM | HUB_CHAR_COMMON_OCPM;
554         if (rh & RH_A_NPS)              /* no power switching? */
555                 temp |= HUB_CHAR_NO_LPSM;
556         if (rh & RH_A_PSM)              /* per-port power switching? */
557                 temp |= HUB_CHAR_INDV_PORT_LPSM;
558         if (rh & RH_A_NOCP)             /* no overcurrent reporting? */
559                 temp |= HUB_CHAR_NO_OCPM;
560         else if (rh & RH_A_OCPM)        /* per-port overcurrent reporting? */
561                 temp |= HUB_CHAR_INDV_PORT_OCPM;
562         desc->wHubCharacteristics = cpu_to_le16(temp);
563
564         /* ports removable, and usb 1.0 legacy PortPwrCtrlMask */
565         rh = roothub_b (ohci);
566         memset(desc->u.hs.DeviceRemovable, 0xff,
567                         sizeof(desc->u.hs.DeviceRemovable));
568         desc->u.hs.DeviceRemovable[0] = rh & RH_B_DR;
569         if (ohci->num_ports > 7) {
570                 desc->u.hs.DeviceRemovable[1] = (rh & RH_B_DR) >> 8;
571                 desc->u.hs.DeviceRemovable[2] = 0xff;
572         } else
573                 desc->u.hs.DeviceRemovable[1] = 0xff;
574 }
575
576 /*-------------------------------------------------------------------------*/
577
578 #ifdef  CONFIG_USB_OTG
579
580 static int ohci_start_port_reset (struct usb_hcd *hcd, unsigned port)
581 {
582         struct ohci_hcd *ohci = hcd_to_ohci (hcd);
583         u32                     status;
584
585         if (!port)
586                 return -EINVAL;
587         port--;
588
589         /* start port reset before HNP protocol times out */
590         status = ohci_readl(ohci, &ohci->regs->roothub.portstatus [port]);
591         if (!(status & RH_PS_CCS))
592                 return -ENODEV;
593
594         /* hub_wq will finish the reset later */
595         ohci_writel(ohci, RH_PS_PRS, &ohci->regs->roothub.portstatus [port]);
596         return 0;
597 }
598
599 #else
600
601 #define ohci_start_port_reset           NULL
602
603 #endif
604
605 /*-------------------------------------------------------------------------*/
606
607
608 /* See usb 7.1.7.5:  root hubs must issue at least 50 msec reset signaling,
609  * not necessarily continuous ... to guard against resume signaling.
610  */
611 #define PORT_RESET_MSEC         50
612
613 /* this timer value might be vendor-specific ... */
614 #define PORT_RESET_HW_MSEC      10
615
616 /* wrap-aware logic morphed from <linux/jiffies.h> */
617 #define tick_before(t1,t2) ((s16)(((s16)(t1))-((s16)(t2))) < 0)
618
619 /* called from some task, normally hub_wq */
620 static inline int root_port_reset (struct ohci_hcd *ohci, unsigned port)
621 {
622         __hc32 __iomem *portstat = &ohci->regs->roothub.portstatus [port];
623         u32     temp = 0;
624         u16     now = ohci_readl(ohci, &ohci->regs->fmnumber);
625         u16     reset_done = now + PORT_RESET_MSEC;
626         int     limit_1 = DIV_ROUND_UP(PORT_RESET_MSEC, PORT_RESET_HW_MSEC);
627
628         /* build a "continuous enough" reset signal, with up to
629          * 3msec gap between pulses.  scheduler HZ==100 must work;
630          * this might need to be deadline-scheduled.
631          */
632         do {
633                 int limit_2;
634
635                 /* spin until any current reset finishes */
636                 limit_2 = PORT_RESET_HW_MSEC * 2;
637                 while (--limit_2 >= 0) {
638                         temp = ohci_readl (ohci, portstat);
639                         /* handle e.g. CardBus eject */
640                         if (temp == ~(u32)0)
641                                 return -ESHUTDOWN;
642                         if (!(temp & RH_PS_PRS))
643                                 break;
644                         udelay (500);
645                 }
646
647                 /* timeout (a hardware error) has been observed when
648                  * EHCI sets CF while this driver is resetting a port;
649                  * presumably other disconnect paths might do it too.
650                  */
651                 if (limit_2 < 0) {
652                         ohci_dbg(ohci,
653                                 "port[%d] reset timeout, stat %08x\n",
654                                 port, temp);
655                         break;
656                 }
657
658                 if (!(temp & RH_PS_CCS))
659                         break;
660                 if (temp & RH_PS_PRSC)
661                         ohci_writel (ohci, RH_PS_PRSC, portstat);
662
663                 /* start the next reset, sleep till it's probably done */
664                 ohci_writel (ohci, RH_PS_PRS, portstat);
665                 msleep(PORT_RESET_HW_MSEC);
666                 now = ohci_readl(ohci, &ohci->regs->fmnumber);
667         } while (tick_before(now, reset_done) && --limit_1 >= 0);
668
669         /* caller synchronizes using PRSC ... and handles PRS
670          * still being set when this returns.
671          */
672
673         return 0;
674 }
675
676 int ohci_hub_control(
677         struct usb_hcd  *hcd,
678         u16             typeReq,
679         u16             wValue,
680         u16             wIndex,
681         char            *buf,
682         u16             wLength
683 ) {
684         struct ohci_hcd *ohci = hcd_to_ohci (hcd);
685         int             ports = ohci->num_ports;
686         u32             temp;
687         int             retval = 0;
688
689         if (unlikely(!HCD_HW_ACCESSIBLE(hcd)))
690                 return -ESHUTDOWN;
691
692         switch (typeReq) {
693         case ClearHubFeature:
694                 switch (wValue) {
695                 case C_HUB_OVER_CURRENT:
696                         ohci_writel (ohci, RH_HS_OCIC,
697                                         &ohci->regs->roothub.status);
698                         break;
699                 case C_HUB_LOCAL_POWER:
700                         break;
701                 default:
702                         goto error;
703                 }
704                 break;
705         case ClearPortFeature:
706                 if (!wIndex || wIndex > ports)
707                         goto error;
708                 wIndex--;
709
710                 switch (wValue) {
711                 case USB_PORT_FEAT_ENABLE:
712                         temp = RH_PS_CCS;
713                         break;
714                 case USB_PORT_FEAT_C_ENABLE:
715                         temp = RH_PS_PESC;
716                         break;
717                 case USB_PORT_FEAT_SUSPEND:
718                         temp = RH_PS_POCI;
719                         break;
720                 case USB_PORT_FEAT_C_SUSPEND:
721                         temp = RH_PS_PSSC;
722                         break;
723                 case USB_PORT_FEAT_POWER:
724                         temp = RH_PS_LSDA;
725                         break;
726                 case USB_PORT_FEAT_C_CONNECTION:
727                         temp = RH_PS_CSC;
728                         break;
729                 case USB_PORT_FEAT_C_OVER_CURRENT:
730                         temp = RH_PS_OCIC;
731                         break;
732                 case USB_PORT_FEAT_C_RESET:
733                         temp = RH_PS_PRSC;
734                         break;
735                 default:
736                         goto error;
737                 }
738                 ohci_writel (ohci, temp,
739                                 &ohci->regs->roothub.portstatus [wIndex]);
740                 // ohci_readl (ohci, &ohci->regs->roothub.portstatus [wIndex]);
741                 break;
742         case GetHubDescriptor:
743                 ohci_hub_descriptor (ohci, (struct usb_hub_descriptor *) buf);
744                 break;
745         case GetHubStatus:
746                 temp = roothub_status (ohci) & ~(RH_HS_CRWE | RH_HS_DRWE);
747                 put_unaligned_le32(temp, buf);
748                 break;
749         case GetPortStatus:
750                 if (!wIndex || wIndex > ports)
751                         goto error;
752                 wIndex--;
753                 temp = roothub_portstatus (ohci, wIndex);
754                 put_unaligned_le32(temp, buf);
755
756                 if (*(u16*)(buf+2))     /* only if wPortChange is interesting */
757                         dbg_port(ohci, "GetStatus", wIndex, temp);
758                 break;
759         case SetHubFeature:
760                 switch (wValue) {
761                 case C_HUB_OVER_CURRENT:
762                         // FIXME:  this can be cleared, yes?
763                 case C_HUB_LOCAL_POWER:
764                         break;
765                 default:
766                         goto error;
767                 }
768                 break;
769         case SetPortFeature:
770                 if (!wIndex || wIndex > ports)
771                         goto error;
772                 wIndex--;
773                 switch (wValue) {
774                 case USB_PORT_FEAT_SUSPEND:
775 #ifdef  CONFIG_USB_OTG
776                         if (hcd->self.otg_port == (wIndex + 1)
777                                         && hcd->self.b_hnp_enable)
778                                 ohci->start_hnp(ohci);
779                         else
780 #endif
781                         ohci_writel (ohci, RH_PS_PSS,
782                                 &ohci->regs->roothub.portstatus [wIndex]);
783                         break;
784                 case USB_PORT_FEAT_POWER:
785                         ohci_writel (ohci, RH_PS_PPS,
786                                 &ohci->regs->roothub.portstatus [wIndex]);
787                         break;
788                 case USB_PORT_FEAT_RESET:
789                         retval = root_port_reset (ohci, wIndex);
790                         break;
791                 default:
792                         goto error;
793                 }
794                 break;
795
796         default:
797 error:
798                 /* "protocol stall" on error */
799                 retval = -EPIPE;
800         }
801         return retval;
802 }
803 EXPORT_SYMBOL_GPL(ohci_hub_control);