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