Linux-2.6.12-rc2
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / usb / host / ohci-hub.c
blobe2fc4129dfc6e0f3684d3d5aee438ca4a0e85640
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 */
10 /*-------------------------------------------------------------------------*/
13 * OHCI Root Hub ... the nonsharable stuff
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" : "", \
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" : "", \
33 (temp & RH_PS_PES) ? " PES" : "", \
34 (temp & RH_PS_CCS) ? " CCS" : "" \
37 /*-------------------------------------------------------------------------*/
39 #if defined(CONFIG_USB_SUSPEND) || defined(CONFIG_PM)
41 #define OHCI_SCHED_ENABLES \
42 (OHCI_CTRL_CLE|OHCI_CTRL_BLE|OHCI_CTRL_PLE|OHCI_CTRL_IE)
44 static void dl_done_list (struct ohci_hcd *, struct pt_regs *);
45 static void finish_unlinks (struct ohci_hcd *, u16 , struct pt_regs *);
46 static int ohci_restart (struct ohci_hcd *ohci);
48 static int ohci_hub_suspend (struct usb_hcd *hcd)
50 struct ohci_hcd *ohci = hcd_to_ohci (hcd);
51 int status = 0;
52 unsigned long flags;
54 spin_lock_irqsave (&ohci->lock, flags);
56 ohci->hc_control = ohci_readl (ohci, &ohci->regs->control);
57 switch (ohci->hc_control & OHCI_CTRL_HCFS) {
58 case OHCI_USB_RESUME:
59 ohci_dbg (ohci, "resume/suspend?\n");
60 ohci->hc_control &= ~OHCI_CTRL_HCFS;
61 ohci->hc_control |= OHCI_USB_RESET;
62 ohci_writel (ohci, ohci->hc_control, &ohci->regs->control);
63 (void) ohci_readl (ohci, &ohci->regs->control);
64 /* FALL THROUGH */
65 case OHCI_USB_RESET:
66 status = -EBUSY;
67 ohci_dbg (ohci, "needs reinit!\n");
68 goto done;
69 case OHCI_USB_SUSPEND:
70 ohci_dbg (ohci, "already suspended\n");
71 goto done;
73 ohci_dbg (ohci, "suspend root hub\n");
75 /* First stop any processing */
76 hcd->state = HC_STATE_QUIESCING;
77 if (ohci->hc_control & OHCI_SCHED_ENABLES) {
78 int limit;
80 ohci->hc_control &= ~OHCI_SCHED_ENABLES;
81 ohci_writel (ohci, ohci->hc_control, &ohci->regs->control);
82 ohci->hc_control = ohci_readl (ohci, &ohci->regs->control);
83 ohci_writel (ohci, OHCI_INTR_SF, &ohci->regs->intrstatus);
85 /* sched disables take effect on the next frame,
86 * then the last WDH could take 6+ msec
88 ohci_dbg (ohci, "stopping schedules ...\n");
89 limit = 2000;
90 while (limit > 0) {
91 udelay (250);
92 limit =- 250;
93 if (ohci_readl (ohci, &ohci->regs->intrstatus)
94 & OHCI_INTR_SF)
95 break;
97 dl_done_list (ohci, NULL);
98 mdelay (7);
100 dl_done_list (ohci, NULL);
101 finish_unlinks (ohci, ohci_frame_no(ohci), NULL);
102 ohci_writel (ohci, ohci_readl (ohci, &ohci->regs->intrstatus),
103 &ohci->regs->intrstatus);
105 /* maybe resume can wake root hub */
106 if (hcd->remote_wakeup)
107 ohci->hc_control |= OHCI_CTRL_RWE;
108 else
109 ohci->hc_control &= ~OHCI_CTRL_RWE;
111 /* Suspend hub */
112 ohci->hc_control &= ~OHCI_CTRL_HCFS;
113 ohci->hc_control |= OHCI_USB_SUSPEND;
114 ohci_writel (ohci, ohci->hc_control, &ohci->regs->control);
115 (void) ohci_readl (ohci, &ohci->regs->control);
117 /* no resumes until devices finish suspending */
118 ohci->next_statechange = jiffies + msecs_to_jiffies (5);
120 done:
121 if (status == 0)
122 hcd->state = HC_STATE_SUSPENDED;
123 spin_unlock_irqrestore (&ohci->lock, flags);
124 return status;
127 static inline struct ed *find_head (struct ed *ed)
129 /* for bulk and control lists */
130 while (ed->ed_prev)
131 ed = ed->ed_prev;
132 return ed;
135 /* caller has locked the root hub */
136 static int ohci_hub_resume (struct usb_hcd *hcd)
138 struct ohci_hcd *ohci = hcd_to_ohci (hcd);
139 u32 temp, enables;
140 int status = -EINPROGRESS;
142 if (time_before (jiffies, ohci->next_statechange))
143 msleep(5);
145 spin_lock_irq (&ohci->lock);
146 ohci->hc_control = ohci_readl (ohci, &ohci->regs->control);
148 if (ohci->hc_control & (OHCI_CTRL_IR | OHCI_SCHED_ENABLES)) {
149 /* this can happen after suspend-to-disk */
150 if (hcd->state == HC_STATE_RESUMING) {
151 ohci_dbg (ohci, "BIOS/SMM active, control %03x\n",
152 ohci->hc_control);
153 status = -EBUSY;
154 /* this happens when pmcore resumes HC then root */
155 } else {
156 ohci_dbg (ohci, "duplicate resume\n");
157 status = 0;
159 } else switch (ohci->hc_control & OHCI_CTRL_HCFS) {
160 case OHCI_USB_SUSPEND:
161 ohci->hc_control &= ~(OHCI_CTRL_HCFS|OHCI_SCHED_ENABLES);
162 ohci->hc_control |= OHCI_USB_RESUME;
163 ohci_writel (ohci, ohci->hc_control, &ohci->regs->control);
164 (void) ohci_readl (ohci, &ohci->regs->control);
165 ohci_dbg (ohci, "resume root hub\n");
166 break;
167 case OHCI_USB_RESUME:
168 /* HCFS changes sometime after INTR_RD */
169 ohci_info (ohci, "wakeup\n");
170 break;
171 case OHCI_USB_OPER:
172 ohci_dbg (ohci, "already resumed\n");
173 status = 0;
174 break;
175 default: /* RESET, we lost power */
176 ohci_dbg (ohci, "root hub hardware reset\n");
177 status = -EBUSY;
179 spin_unlock_irq (&ohci->lock);
180 if (status == -EBUSY) {
181 (void) ohci_init (ohci);
182 return ohci_restart (ohci);
184 if (status != -EINPROGRESS)
185 return status;
187 temp = roothub_a (ohci) & RH_A_NDP;
188 enables = 0;
189 while (temp--) {
190 u32 stat = ohci_readl (ohci,
191 &ohci->regs->roothub.portstatus [temp]);
193 /* force global, not selective, resume */
194 if (!(stat & RH_PS_PSS))
195 continue;
196 ohci_writel (ohci, RH_PS_POCI,
197 &ohci->regs->roothub.portstatus [temp]);
200 /* Some controllers (lucent erratum) need extra-long delays */
201 hcd->state = HC_STATE_RESUMING;
202 mdelay (20 /* usb 11.5.1.10 */ + 15);
204 temp = ohci_readl (ohci, &ohci->regs->control);
205 temp &= OHCI_CTRL_HCFS;
206 if (temp != OHCI_USB_RESUME) {
207 ohci_err (ohci, "controller won't resume\n");
208 return -EBUSY;
211 /* disable old schedule state, reinit from scratch */
212 ohci_writel (ohci, 0, &ohci->regs->ed_controlhead);
213 ohci_writel (ohci, 0, &ohci->regs->ed_controlcurrent);
214 ohci_writel (ohci, 0, &ohci->regs->ed_bulkhead);
215 ohci_writel (ohci, 0, &ohci->regs->ed_bulkcurrent);
216 ohci_writel (ohci, 0, &ohci->regs->ed_periodcurrent);
217 ohci_writel (ohci, (u32) ohci->hcca_dma, &ohci->regs->hcca);
219 /* Sometimes PCI D3 suspend trashes frame timings ... */
220 periodic_reinit (ohci);
222 /* interrupts might have been disabled */
223 ohci_writel (ohci, OHCI_INTR_INIT, &ohci->regs->intrenable);
224 if (ohci->ed_rm_list)
225 ohci_writel (ohci, OHCI_INTR_SF, &ohci->regs->intrenable);
226 ohci_writel (ohci, ohci_readl (ohci, &ohci->regs->intrstatus),
227 &ohci->regs->intrstatus);
229 /* Then re-enable operations */
230 ohci_writel (ohci, OHCI_USB_OPER, &ohci->regs->control);
231 (void) ohci_readl (ohci, &ohci->regs->control);
232 msleep (3);
234 temp = OHCI_CONTROL_INIT | OHCI_USB_OPER;
235 if (hcd->can_wakeup)
236 temp |= OHCI_CTRL_RWC;
237 ohci->hc_control = temp;
238 ohci_writel (ohci, temp, &ohci->regs->control);
239 (void) ohci_readl (ohci, &ohci->regs->control);
241 /* TRSMRCY */
242 msleep (10);
244 /* keep it alive for ~5x suspend + resume costs */
245 ohci->next_statechange = jiffies + msecs_to_jiffies (250);
247 /* maybe turn schedules back on */
248 enables = 0;
249 temp = 0;
250 if (!ohci->ed_rm_list) {
251 if (ohci->ed_controltail) {
252 ohci_writel (ohci,
253 find_head (ohci->ed_controltail)->dma,
254 &ohci->regs->ed_controlhead);
255 enables |= OHCI_CTRL_CLE;
256 temp |= OHCI_CLF;
258 if (ohci->ed_bulktail) {
259 ohci_writel (ohci, find_head (ohci->ed_bulktail)->dma,
260 &ohci->regs->ed_bulkhead);
261 enables |= OHCI_CTRL_BLE;
262 temp |= OHCI_BLF;
265 if (hcd->self.bandwidth_isoc_reqs || hcd->self.bandwidth_int_reqs)
266 enables |= OHCI_CTRL_PLE|OHCI_CTRL_IE;
267 if (enables) {
268 ohci_dbg (ohci, "restarting schedules ... %08x\n", enables);
269 ohci->hc_control |= enables;
270 ohci_writel (ohci, ohci->hc_control, &ohci->regs->control);
271 if (temp)
272 ohci_writel (ohci, temp, &ohci->regs->cmdstatus);
273 (void) ohci_readl (ohci, &ohci->regs->control);
276 hcd->state = HC_STATE_RUNNING;
277 return 0;
280 static void ohci_rh_resume (void *_hcd)
282 struct usb_hcd *hcd = _hcd;
284 usb_lock_device (hcd->self.root_hub);
285 (void) ohci_hub_resume (hcd);
286 usb_unlock_device (hcd->self.root_hub);
289 #else
291 static void ohci_rh_resume (void *_hcd)
293 struct ohci_hcd *ohci = hcd_to_ohci (_hcd);
294 ohci_dbg(ohci, "rh_resume ??\n");
297 #endif /* CONFIG_USB_SUSPEND || CONFIG_PM */
299 /*-------------------------------------------------------------------------*/
301 /* build "status change" packet (one or two bytes) from HC registers */
303 static int
304 ohci_hub_status_data (struct usb_hcd *hcd, char *buf)
306 struct ohci_hcd *ohci = hcd_to_ohci (hcd);
307 int ports, i, changed = 0, length = 1;
308 int can_suspend = hcd->can_wakeup;
309 unsigned long flags;
311 spin_lock_irqsave (&ohci->lock, flags);
313 /* handle autosuspended root: finish resuming before
314 * letting khubd or root hub timer see state changes.
316 if ((ohci->hc_control & OHCI_CTRL_HCFS) != OHCI_USB_OPER
317 || !HC_IS_RUNNING(hcd->state)) {
318 can_suspend = 0;
319 goto done;
322 ports = roothub_a (ohci) & RH_A_NDP;
323 if (ports > MAX_ROOT_PORTS) {
324 ohci_err (ohci, "bogus NDP=%d, rereads as NDP=%d\n", ports,
325 ohci_readl (ohci, &ohci->regs->roothub.a) & RH_A_NDP);
326 /* retry later; "should not happen" */
327 goto done;
330 /* init status */
331 if (roothub_status (ohci) & (RH_HS_LPSC | RH_HS_OCIC))
332 buf [0] = changed = 1;
333 else
334 buf [0] = 0;
335 if (ports > 7) {
336 buf [1] = 0;
337 length++;
340 /* look at each port */
341 for (i = 0; i < ports; i++) {
342 u32 status = roothub_portstatus (ohci, i);
344 if (status & (RH_PS_CSC | RH_PS_PESC | RH_PS_PSSC
345 | RH_PS_OCIC | RH_PS_PRSC)) {
346 changed = 1;
347 if (i < 7)
348 buf [0] |= 1 << (i + 1);
349 else
350 buf [1] |= 1 << (i - 7);
351 continue;
354 /* can suspend if no ports are enabled; or if all all
355 * enabled ports are suspended AND remote wakeup is on.
357 if (!(status & RH_PS_CCS))
358 continue;
359 if ((status & RH_PS_PSS) && hcd->remote_wakeup)
360 continue;
361 can_suspend = 0;
363 done:
364 spin_unlock_irqrestore (&ohci->lock, flags);
366 #ifdef CONFIG_PM
367 /* save power by suspending idle root hubs;
368 * INTR_RD wakes us when there's work
369 * NOTE: if we can do this, we don't need a root hub timer!
371 if (can_suspend
372 && !changed
373 && !ohci->ed_rm_list
374 && ((OHCI_CTRL_HCFS | OHCI_SCHED_ENABLES)
375 & ohci->hc_control)
376 == OHCI_USB_OPER
377 && time_after (jiffies, ohci->next_statechange)
378 && usb_trylock_device (hcd->self.root_hub)
380 ohci_vdbg (ohci, "autosuspend\n");
381 (void) ohci_hub_suspend (hcd);
382 hcd->state = HC_STATE_RUNNING;
383 usb_unlock_device (hcd->self.root_hub);
385 #endif
387 return changed ? length : 0;
390 /*-------------------------------------------------------------------------*/
392 static void
393 ohci_hub_descriptor (
394 struct ohci_hcd *ohci,
395 struct usb_hub_descriptor *desc
397 u32 rh = roothub_a (ohci);
398 int ports = rh & RH_A_NDP;
399 u16 temp;
401 desc->bDescriptorType = 0x29;
402 desc->bPwrOn2PwrGood = (rh & RH_A_POTPGT) >> 24;
403 desc->bHubContrCurrent = 0;
405 desc->bNbrPorts = ports;
406 temp = 1 + (ports / 8);
407 desc->bDescLength = 7 + 2 * temp;
409 temp = 0;
410 if (rh & RH_A_NPS) /* no power switching? */
411 temp |= 0x0002;
412 if (rh & RH_A_PSM) /* per-port power switching? */
413 temp |= 0x0001;
414 if (rh & RH_A_NOCP) /* no overcurrent reporting? */
415 temp |= 0x0010;
416 else if (rh & RH_A_OCPM) /* per-port overcurrent reporting? */
417 temp |= 0x0008;
418 desc->wHubCharacteristics = (__force __u16)cpu_to_hc16(ohci, temp);
420 /* two bitmaps: ports removable, and usb 1.0 legacy PortPwrCtrlMask */
421 rh = roothub_b (ohci);
422 desc->bitmap [0] = rh & RH_B_DR;
423 if (ports > 7) {
424 desc->bitmap [1] = (rh & RH_B_DR) >> 8;
425 desc->bitmap [2] = desc->bitmap [3] = 0xff;
426 } else
427 desc->bitmap [1] = 0xff;
430 /*-------------------------------------------------------------------------*/
432 #ifdef CONFIG_USB_OTG
434 static int ohci_start_port_reset (struct usb_hcd *hcd, unsigned port)
436 struct ohci_hcd *ohci = hcd_to_ohci (hcd);
437 u32 status;
439 if (!port)
440 return -EINVAL;
441 port--;
443 /* start port reset before HNP protocol times out */
444 status = ohci_readl(ohci, &ohci->regs->roothub.portstatus [port]);
445 if (!(status & RH_PS_CCS))
446 return -ENODEV;
448 /* khubd will finish the reset later */
449 ohci_writel(ohci, RH_PS_PRS, &ohci->regs->roothub.portstatus [port]);
450 return 0;
453 static void start_hnp(struct ohci_hcd *ohci);
455 #else
457 #define ohci_start_port_reset NULL
459 #endif
461 /*-------------------------------------------------------------------------*/
464 /* See usb 7.1.7.5: root hubs must issue at least 50 msec reset signaling,
465 * not necessarily continuous ... to guard against resume signaling.
466 * The short timeout is safe for non-root hubs, and is backward-compatible
467 * with earlier Linux hosts.
469 #ifdef CONFIG_USB_SUSPEND
470 #define PORT_RESET_MSEC 50
471 #else
472 #define PORT_RESET_MSEC 10
473 #endif
475 /* this timer value might be vendor-specific ... */
476 #define PORT_RESET_HW_MSEC 10
478 /* wrap-aware logic morphed from <linux/jiffies.h> */
479 #define tick_before(t1,t2) ((s16)(((s16)(t1))-((s16)(t2))) < 0)
481 /* called from some task, normally khubd */
482 static inline void root_port_reset (struct ohci_hcd *ohci, unsigned port)
484 __hc32 __iomem *portstat = &ohci->regs->roothub.portstatus [port];
485 u32 temp;
486 u16 now = ohci_readl(ohci, &ohci->regs->fmnumber);
487 u16 reset_done = now + PORT_RESET_MSEC;
489 /* build a "continuous enough" reset signal, with up to
490 * 3msec gap between pulses. scheduler HZ==100 must work;
491 * this might need to be deadline-scheduled.
493 do {
494 /* spin until any current reset finishes */
495 for (;;) {
496 temp = ohci_readl (ohci, portstat);
497 if (!(temp & RH_PS_PRS))
498 break;
499 udelay (500);
502 if (!(temp & RH_PS_CCS))
503 break;
504 if (temp & RH_PS_PRSC)
505 ohci_writel (ohci, RH_PS_PRSC, portstat);
507 /* start the next reset, sleep till it's probably done */
508 ohci_writel (ohci, RH_PS_PRS, portstat);
509 msleep(PORT_RESET_HW_MSEC);
510 now = ohci_readl(ohci, &ohci->regs->fmnumber);
511 } while (tick_before(now, reset_done));
512 /* caller synchronizes using PRSC */
515 static int ohci_hub_control (
516 struct usb_hcd *hcd,
517 u16 typeReq,
518 u16 wValue,
519 u16 wIndex,
520 char *buf,
521 u16 wLength
523 struct ohci_hcd *ohci = hcd_to_ohci (hcd);
524 int ports = hcd_to_bus (hcd)->root_hub->maxchild;
525 u32 temp;
526 int retval = 0;
528 switch (typeReq) {
529 case ClearHubFeature:
530 switch (wValue) {
531 case C_HUB_OVER_CURRENT:
532 ohci_writel (ohci, RH_HS_OCIC,
533 &ohci->regs->roothub.status);
534 case C_HUB_LOCAL_POWER:
535 break;
536 default:
537 goto error;
539 break;
540 case ClearPortFeature:
541 if (!wIndex || wIndex > ports)
542 goto error;
543 wIndex--;
545 switch (wValue) {
546 case USB_PORT_FEAT_ENABLE:
547 temp = RH_PS_CCS;
548 break;
549 case USB_PORT_FEAT_C_ENABLE:
550 temp = RH_PS_PESC;
551 break;
552 case USB_PORT_FEAT_SUSPEND:
553 temp = RH_PS_POCI;
554 if ((ohci->hc_control & OHCI_CTRL_HCFS)
555 != OHCI_USB_OPER)
556 schedule_work (&ohci->rh_resume);
557 break;
558 case USB_PORT_FEAT_C_SUSPEND:
559 temp = RH_PS_PSSC;
560 break;
561 case USB_PORT_FEAT_POWER:
562 temp = RH_PS_LSDA;
563 break;
564 case USB_PORT_FEAT_C_CONNECTION:
565 temp = RH_PS_CSC;
566 break;
567 case USB_PORT_FEAT_C_OVER_CURRENT:
568 temp = RH_PS_OCIC;
569 break;
570 case USB_PORT_FEAT_C_RESET:
571 temp = RH_PS_PRSC;
572 break;
573 default:
574 goto error;
576 ohci_writel (ohci, temp,
577 &ohci->regs->roothub.portstatus [wIndex]);
578 // ohci_readl (ohci, &ohci->regs->roothub.portstatus [wIndex]);
579 break;
580 case GetHubDescriptor:
581 ohci_hub_descriptor (ohci, (struct usb_hub_descriptor *) buf);
582 break;
583 case GetHubStatus:
584 temp = roothub_status (ohci) & ~(RH_HS_CRWE | RH_HS_DRWE);
585 *(__le32 *) buf = cpu_to_le32 (temp);
586 break;
587 case GetPortStatus:
588 if (!wIndex || wIndex > ports)
589 goto error;
590 wIndex--;
591 temp = roothub_portstatus (ohci, wIndex);
592 *(__le32 *) buf = cpu_to_le32 (temp);
594 #ifndef OHCI_VERBOSE_DEBUG
595 if (*(u16*)(buf+2)) /* only if wPortChange is interesting */
596 #endif
597 dbg_port (ohci, "GetStatus", wIndex, temp);
598 break;
599 case SetHubFeature:
600 switch (wValue) {
601 case C_HUB_OVER_CURRENT:
602 // FIXME: this can be cleared, yes?
603 case C_HUB_LOCAL_POWER:
604 break;
605 default:
606 goto error;
608 break;
609 case SetPortFeature:
610 if (!wIndex || wIndex > ports)
611 goto error;
612 wIndex--;
613 switch (wValue) {
614 case USB_PORT_FEAT_SUSPEND:
615 #ifdef CONFIG_USB_OTG
616 if (hcd->self.otg_port == (wIndex + 1)
617 && hcd->self.b_hnp_enable)
618 start_hnp(ohci);
619 else
620 #endif
621 ohci_writel (ohci, RH_PS_PSS,
622 &ohci->regs->roothub.portstatus [wIndex]);
623 break;
624 case USB_PORT_FEAT_POWER:
625 ohci_writel (ohci, RH_PS_PPS,
626 &ohci->regs->roothub.portstatus [wIndex]);
627 break;
628 case USB_PORT_FEAT_RESET:
629 root_port_reset (ohci, wIndex);
630 break;
631 default:
632 goto error;
634 break;
636 default:
637 error:
638 /* "protocol stall" on error */
639 retval = -EPIPE;
641 return retval;