[PATCH] USB: Remove USB private semaphore
[linux-2.6/zen-sources.git] / drivers / usb / host / ohci-hub.c
blob4b2226d77b342dd68082733a2aac20e41be25fa9
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 #ifdef 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_bus_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 if (unlikely(!test_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags))) {
57 spin_unlock_irqrestore (&ohci->lock, flags);
58 return -ESHUTDOWN;
61 ohci->hc_control = ohci_readl (ohci, &ohci->regs->control);
62 switch (ohci->hc_control & OHCI_CTRL_HCFS) {
63 case OHCI_USB_RESUME:
64 ohci_dbg (ohci, "resume/suspend?\n");
65 ohci->hc_control &= ~OHCI_CTRL_HCFS;
66 ohci->hc_control |= OHCI_USB_RESET;
67 ohci_writel (ohci, ohci->hc_control, &ohci->regs->control);
68 (void) ohci_readl (ohci, &ohci->regs->control);
69 /* FALL THROUGH */
70 case OHCI_USB_RESET:
71 status = -EBUSY;
72 ohci_dbg (ohci, "needs reinit!\n");
73 goto done;
74 case OHCI_USB_SUSPEND:
75 ohci_dbg (ohci, "already suspended\n");
76 goto done;
78 ohci_dbg (ohci, "suspend root hub\n");
80 /* First stop any processing */
81 if (ohci->hc_control & OHCI_SCHED_ENABLES) {
82 int limit;
84 ohci->hc_control &= ~OHCI_SCHED_ENABLES;
85 ohci_writel (ohci, ohci->hc_control, &ohci->regs->control);
86 ohci->hc_control = ohci_readl (ohci, &ohci->regs->control);
87 ohci_writel (ohci, OHCI_INTR_SF, &ohci->regs->intrstatus);
89 /* sched disables take effect on the next frame,
90 * then the last WDH could take 6+ msec
92 ohci_dbg (ohci, "stopping schedules ...\n");
93 limit = 2000;
94 while (limit > 0) {
95 udelay (250);
96 limit =- 250;
97 if (ohci_readl (ohci, &ohci->regs->intrstatus)
98 & OHCI_INTR_SF)
99 break;
101 dl_done_list (ohci, NULL);
102 mdelay (7);
104 dl_done_list (ohci, NULL);
105 finish_unlinks (ohci, ohci_frame_no(ohci), NULL);
106 ohci_writel (ohci, ohci_readl (ohci, &ohci->regs->intrstatus),
107 &ohci->regs->intrstatus);
109 /* maybe resume can wake root hub */
110 if (hcd->remote_wakeup)
111 ohci->hc_control |= OHCI_CTRL_RWE;
112 else
113 ohci->hc_control &= ~OHCI_CTRL_RWE;
115 /* Suspend hub ... this is the "global (to this bus) suspend" mode,
116 * which doesn't imply ports will first be individually suspended.
118 ohci->hc_control &= ~OHCI_CTRL_HCFS;
119 ohci->hc_control |= OHCI_USB_SUSPEND;
120 ohci_writel (ohci, ohci->hc_control, &ohci->regs->control);
121 (void) ohci_readl (ohci, &ohci->regs->control);
123 /* no resumes until devices finish suspending */
124 ohci->next_statechange = jiffies + msecs_to_jiffies (5);
126 done:
127 /* external suspend vs self autosuspend ... same effect */
128 if (status == 0)
129 usb_hcd_suspend_root_hub(hcd);
130 spin_unlock_irqrestore (&ohci->lock, flags);
131 return status;
134 static inline struct ed *find_head (struct ed *ed)
136 /* for bulk and control lists */
137 while (ed->ed_prev)
138 ed = ed->ed_prev;
139 return ed;
142 /* caller has locked the root hub */
143 static int ohci_bus_resume (struct usb_hcd *hcd)
145 struct ohci_hcd *ohci = hcd_to_ohci (hcd);
146 u32 temp, enables;
147 int status = -EINPROGRESS;
148 unsigned long flags;
150 if (time_before (jiffies, ohci->next_statechange))
151 msleep(5);
153 spin_lock_irqsave (&ohci->lock, flags);
155 if (unlikely(!test_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags))) {
156 spin_unlock_irqrestore (&ohci->lock, flags);
157 return -ESHUTDOWN;
161 ohci->hc_control = ohci_readl (ohci, &ohci->regs->control);
163 if (ohci->hc_control & (OHCI_CTRL_IR | OHCI_SCHED_ENABLES)) {
164 /* this can happen after resuming a swsusp snapshot */
165 if (hcd->state == HC_STATE_RESUMING) {
166 ohci_dbg (ohci, "BIOS/SMM active, control %03x\n",
167 ohci->hc_control);
168 status = -EBUSY;
169 /* this happens when pmcore resumes HC then root */
170 } else {
171 ohci_dbg (ohci, "duplicate resume\n");
172 status = 0;
174 } else switch (ohci->hc_control & OHCI_CTRL_HCFS) {
175 case OHCI_USB_SUSPEND:
176 ohci->hc_control &= ~(OHCI_CTRL_HCFS|OHCI_SCHED_ENABLES);
177 ohci->hc_control |= OHCI_USB_RESUME;
178 ohci_writel (ohci, ohci->hc_control, &ohci->regs->control);
179 (void) ohci_readl (ohci, &ohci->regs->control);
180 ohci_dbg (ohci, "resume root hub\n");
181 break;
182 case OHCI_USB_RESUME:
183 /* HCFS changes sometime after INTR_RD */
184 ohci_info (ohci, "wakeup\n");
185 break;
186 case OHCI_USB_OPER:
187 /* this can happen after resuming a swsusp snapshot */
188 ohci_dbg (ohci, "snapshot resume? reinit\n");
189 status = -EBUSY;
190 break;
191 default: /* RESET, we lost power */
192 ohci_dbg (ohci, "lost power\n");
193 status = -EBUSY;
195 spin_unlock_irqrestore (&ohci->lock, flags);
196 if (status == -EBUSY) {
197 (void) ohci_init (ohci);
198 return ohci_restart (ohci);
200 if (status != -EINPROGRESS)
201 return status;
203 temp = ohci->num_ports;
204 enables = 0;
205 while (temp--) {
206 u32 stat = ohci_readl (ohci,
207 &ohci->regs->roothub.portstatus [temp]);
209 /* force global, not selective, resume */
210 if (!(stat & RH_PS_PSS))
211 continue;
212 ohci_writel (ohci, RH_PS_POCI,
213 &ohci->regs->roothub.portstatus [temp]);
216 /* Some controllers (lucent erratum) need extra-long delays */
217 msleep (20 /* usb 11.5.1.10 */ + 12 /* 32 msec counter */ + 1);
219 temp = ohci_readl (ohci, &ohci->regs->control);
220 temp &= OHCI_CTRL_HCFS;
221 if (temp != OHCI_USB_RESUME) {
222 ohci_err (ohci, "controller won't resume\n");
223 return -EBUSY;
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);
234 /* Sometimes PCI D3 suspend trashes frame timings ... */
235 periodic_reinit (ohci);
237 /* interrupts might have been disabled */
238 ohci_writel (ohci, OHCI_INTR_INIT, &ohci->regs->intrenable);
239 if (ohci->ed_rm_list)
240 ohci_writel (ohci, OHCI_INTR_SF, &ohci->regs->intrenable);
241 ohci_writel (ohci, ohci_readl (ohci, &ohci->regs->intrstatus),
242 &ohci->regs->intrstatus);
244 /* Then re-enable operations */
245 ohci_writel (ohci, OHCI_USB_OPER, &ohci->regs->control);
246 (void) ohci_readl (ohci, &ohci->regs->control);
247 msleep (3);
249 temp = OHCI_CONTROL_INIT | OHCI_USB_OPER;
250 if (hcd->can_wakeup)
251 temp |= OHCI_CTRL_RWC;
252 ohci->hc_control = temp;
253 ohci_writel (ohci, temp, &ohci->regs->control);
254 (void) ohci_readl (ohci, &ohci->regs->control);
256 /* TRSMRCY */
257 msleep (10);
259 /* keep it alive for ~5x suspend + resume costs */
260 ohci->next_statechange = jiffies + msecs_to_jiffies (250);
262 /* maybe turn schedules back on */
263 enables = 0;
264 temp = 0;
265 if (!ohci->ed_rm_list) {
266 if (ohci->ed_controltail) {
267 ohci_writel (ohci,
268 find_head (ohci->ed_controltail)->dma,
269 &ohci->regs->ed_controlhead);
270 enables |= OHCI_CTRL_CLE;
271 temp |= OHCI_CLF;
273 if (ohci->ed_bulktail) {
274 ohci_writel (ohci, find_head (ohci->ed_bulktail)->dma,
275 &ohci->regs->ed_bulkhead);
276 enables |= OHCI_CTRL_BLE;
277 temp |= OHCI_BLF;
280 if (hcd->self.bandwidth_isoc_reqs || hcd->self.bandwidth_int_reqs)
281 enables |= OHCI_CTRL_PLE|OHCI_CTRL_IE;
282 if (enables) {
283 ohci_dbg (ohci, "restarting schedules ... %08x\n", enables);
284 ohci->hc_control |= enables;
285 ohci_writel (ohci, ohci->hc_control, &ohci->regs->control);
286 if (temp)
287 ohci_writel (ohci, temp, &ohci->regs->cmdstatus);
288 (void) ohci_readl (ohci, &ohci->regs->control);
291 return 0;
294 #endif /* CONFIG_PM */
296 /*-------------------------------------------------------------------------*/
298 /* build "status change" packet (one or two bytes) from HC registers */
300 static int
301 ohci_hub_status_data (struct usb_hcd *hcd, char *buf)
303 struct ohci_hcd *ohci = hcd_to_ohci (hcd);
304 int i, changed = 0, length = 1;
305 int can_suspend = hcd->can_wakeup;
306 unsigned long flags;
308 spin_lock_irqsave (&ohci->lock, flags);
310 /* handle autosuspended root: finish resuming before
311 * letting khubd or root hub timer see state changes.
313 if (unlikely((ohci->hc_control & OHCI_CTRL_HCFS) != OHCI_USB_OPER
314 || !HC_IS_RUNNING(hcd->state))) {
315 can_suspend = 0;
316 goto done;
319 /* undocumented erratum seen on at least rev D */
320 if ((ohci->flags & OHCI_QUIRK_AMD756)
321 && (roothub_a (ohci) & RH_A_NDP) > MAX_ROOT_PORTS) {
322 ohci_warn (ohci, "bogus NDP, rereads as NDP=%d\n",
323 ohci_readl (ohci, &ohci->regs->roothub.a) & RH_A_NDP);
324 /* retry later; "should not happen" */
325 goto done;
328 /* init status */
329 if (roothub_status (ohci) & (RH_HS_LPSC | RH_HS_OCIC))
330 buf [0] = changed = 1;
331 else
332 buf [0] = 0;
333 if (ohci->num_ports > 7) {
334 buf [1] = 0;
335 length++;
338 /* look at each port */
339 for (i = 0; i < ohci->num_ports; i++) {
340 u32 status = roothub_portstatus (ohci, i);
342 if (status & (RH_PS_CSC | RH_PS_PESC | RH_PS_PSSC
343 | RH_PS_OCIC | RH_PS_PRSC)) {
344 changed = 1;
345 if (i < 7)
346 buf [0] |= 1 << (i + 1);
347 else
348 buf [1] |= 1 << (i - 7);
349 continue;
352 /* can suspend if no ports are enabled; or if all all
353 * enabled ports are suspended AND remote wakeup is on.
355 if (!(status & RH_PS_CCS))
356 continue;
357 if ((status & RH_PS_PSS) && hcd->remote_wakeup)
358 continue;
359 can_suspend = 0;
361 done:
362 spin_unlock_irqrestore (&ohci->lock, flags);
364 #ifdef CONFIG_PM
365 /* save power by suspending idle root hubs;
366 * INTR_RD wakes us when there's work
368 if (can_suspend
369 && !changed
370 && !ohci->ed_rm_list
371 && ((OHCI_CTRL_HCFS | OHCI_SCHED_ENABLES)
372 & ohci->hc_control)
373 == OHCI_USB_OPER
374 && time_after (jiffies, ohci->next_statechange)
375 && usb_trylock_device (hcd->self.root_hub) == 0
377 ohci_vdbg (ohci, "autosuspend\n");
378 (void) ohci_bus_suspend (hcd);
379 usb_unlock_device (hcd->self.root_hub);
381 #endif
383 return changed ? length : 0;
386 /*-------------------------------------------------------------------------*/
388 static void
389 ohci_hub_descriptor (
390 struct ohci_hcd *ohci,
391 struct usb_hub_descriptor *desc
393 u32 rh = roothub_a (ohci);
394 u16 temp;
396 desc->bDescriptorType = 0x29;
397 desc->bPwrOn2PwrGood = (rh & RH_A_POTPGT) >> 24;
398 desc->bHubContrCurrent = 0;
400 desc->bNbrPorts = ohci->num_ports;
401 temp = 1 + (ohci->num_ports / 8);
402 desc->bDescLength = 7 + 2 * temp;
404 temp = 0;
405 if (rh & RH_A_NPS) /* no power switching? */
406 temp |= 0x0002;
407 if (rh & RH_A_PSM) /* per-port power switching? */
408 temp |= 0x0001;
409 if (rh & RH_A_NOCP) /* no overcurrent reporting? */
410 temp |= 0x0010;
411 else if (rh & RH_A_OCPM) /* per-port overcurrent reporting? */
412 temp |= 0x0008;
413 desc->wHubCharacteristics = (__force __u16)cpu_to_hc16(ohci, temp);
415 /* two bitmaps: ports removable, and usb 1.0 legacy PortPwrCtrlMask */
416 rh = roothub_b (ohci);
417 memset(desc->bitmap, 0xff, sizeof(desc->bitmap));
418 desc->bitmap [0] = rh & RH_B_DR;
419 if (ohci->num_ports > 7) {
420 desc->bitmap [1] = (rh & RH_B_DR) >> 8;
421 desc->bitmap [2] = 0xff;
422 } else
423 desc->bitmap [1] = 0xff;
426 /*-------------------------------------------------------------------------*/
428 #ifdef CONFIG_USB_OTG
430 static int ohci_start_port_reset (struct usb_hcd *hcd, unsigned port)
432 struct ohci_hcd *ohci = hcd_to_ohci (hcd);
433 u32 status;
435 if (!port)
436 return -EINVAL;
437 port--;
439 /* start port reset before HNP protocol times out */
440 status = ohci_readl(ohci, &ohci->regs->roothub.portstatus [port]);
441 if (!(status & RH_PS_CCS))
442 return -ENODEV;
444 /* khubd will finish the reset later */
445 ohci_writel(ohci, RH_PS_PRS, &ohci->regs->roothub.portstatus [port]);
446 return 0;
449 static void start_hnp(struct ohci_hcd *ohci);
451 #else
453 #define ohci_start_port_reset NULL
455 #endif
457 /*-------------------------------------------------------------------------*/
460 /* See usb 7.1.7.5: root hubs must issue at least 50 msec reset signaling,
461 * not necessarily continuous ... to guard against resume signaling.
462 * The short timeout is safe for non-root hubs, and is backward-compatible
463 * with earlier Linux hosts.
465 #ifdef CONFIG_USB_SUSPEND
466 #define PORT_RESET_MSEC 50
467 #else
468 #define PORT_RESET_MSEC 10
469 #endif
471 /* this timer value might be vendor-specific ... */
472 #define PORT_RESET_HW_MSEC 10
474 /* wrap-aware logic morphed from <linux/jiffies.h> */
475 #define tick_before(t1,t2) ((s16)(((s16)(t1))-((s16)(t2))) < 0)
477 /* called from some task, normally khubd */
478 static inline void root_port_reset (struct ohci_hcd *ohci, unsigned port)
480 __hc32 __iomem *portstat = &ohci->regs->roothub.portstatus [port];
481 u32 temp;
482 u16 now = ohci_readl(ohci, &ohci->regs->fmnumber);
483 u16 reset_done = now + PORT_RESET_MSEC;
485 /* build a "continuous enough" reset signal, with up to
486 * 3msec gap between pulses. scheduler HZ==100 must work;
487 * this might need to be deadline-scheduled.
489 do {
490 /* spin until any current reset finishes */
491 for (;;) {
492 temp = ohci_readl (ohci, portstat);
493 if (!(temp & RH_PS_PRS))
494 break;
495 udelay (500);
498 if (!(temp & RH_PS_CCS))
499 break;
500 if (temp & RH_PS_PRSC)
501 ohci_writel (ohci, RH_PS_PRSC, portstat);
503 /* start the next reset, sleep till it's probably done */
504 ohci_writel (ohci, RH_PS_PRS, portstat);
505 msleep(PORT_RESET_HW_MSEC);
506 now = ohci_readl(ohci, &ohci->regs->fmnumber);
507 } while (tick_before(now, reset_done));
508 /* caller synchronizes using PRSC */
511 static int ohci_hub_control (
512 struct usb_hcd *hcd,
513 u16 typeReq,
514 u16 wValue,
515 u16 wIndex,
516 char *buf,
517 u16 wLength
519 struct ohci_hcd *ohci = hcd_to_ohci (hcd);
520 int ports = hcd_to_bus (hcd)->root_hub->maxchild;
521 u32 temp;
522 int retval = 0;
524 if (unlikely(!test_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags)))
525 return -ESHUTDOWN;
527 switch (typeReq) {
528 case ClearHubFeature:
529 switch (wValue) {
530 case C_HUB_OVER_CURRENT:
531 ohci_writel (ohci, RH_HS_OCIC,
532 &ohci->regs->roothub.status);
533 case C_HUB_LOCAL_POWER:
534 break;
535 default:
536 goto error;
538 break;
539 case ClearPortFeature:
540 if (!wIndex || wIndex > ports)
541 goto error;
542 wIndex--;
544 switch (wValue) {
545 case USB_PORT_FEAT_ENABLE:
546 temp = RH_PS_CCS;
547 break;
548 case USB_PORT_FEAT_C_ENABLE:
549 temp = RH_PS_PESC;
550 break;
551 case USB_PORT_FEAT_SUSPEND:
552 temp = RH_PS_POCI;
553 if ((ohci->hc_control & OHCI_CTRL_HCFS)
554 != OHCI_USB_OPER)
555 usb_hcd_resume_root_hub(hcd);
556 break;
557 case USB_PORT_FEAT_C_SUSPEND:
558 temp = RH_PS_PSSC;
559 break;
560 case USB_PORT_FEAT_POWER:
561 temp = RH_PS_LSDA;
562 break;
563 case USB_PORT_FEAT_C_CONNECTION:
564 temp = RH_PS_CSC;
565 break;
566 case USB_PORT_FEAT_C_OVER_CURRENT:
567 temp = RH_PS_OCIC;
568 break;
569 case USB_PORT_FEAT_C_RESET:
570 temp = RH_PS_PRSC;
571 break;
572 default:
573 goto error;
575 ohci_writel (ohci, temp,
576 &ohci->regs->roothub.portstatus [wIndex]);
577 // ohci_readl (ohci, &ohci->regs->roothub.portstatus [wIndex]);
578 break;
579 case GetHubDescriptor:
580 ohci_hub_descriptor (ohci, (struct usb_hub_descriptor *) buf);
581 break;
582 case GetHubStatus:
583 temp = roothub_status (ohci) & ~(RH_HS_CRWE | RH_HS_DRWE);
584 *(__le32 *) buf = cpu_to_le32 (temp);
585 break;
586 case GetPortStatus:
587 if (!wIndex || wIndex > ports)
588 goto error;
589 wIndex--;
590 temp = roothub_portstatus (ohci, wIndex);
591 *(__le32 *) buf = cpu_to_le32 (temp);
593 #ifndef OHCI_VERBOSE_DEBUG
594 if (*(u16*)(buf+2)) /* only if wPortChange is interesting */
595 #endif
596 dbg_port (ohci, "GetStatus", wIndex, temp);
597 break;
598 case SetHubFeature:
599 switch (wValue) {
600 case C_HUB_OVER_CURRENT:
601 // FIXME: this can be cleared, yes?
602 case C_HUB_LOCAL_POWER:
603 break;
604 default:
605 goto error;
607 break;
608 case SetPortFeature:
609 if (!wIndex || wIndex > ports)
610 goto error;
611 wIndex--;
612 switch (wValue) {
613 case USB_PORT_FEAT_SUSPEND:
614 #ifdef CONFIG_USB_OTG
615 if (hcd->self.otg_port == (wIndex + 1)
616 && hcd->self.b_hnp_enable)
617 start_hnp(ohci);
618 else
619 #endif
620 ohci_writel (ohci, RH_PS_PSS,
621 &ohci->regs->roothub.portstatus [wIndex]);
622 break;
623 case USB_PORT_FEAT_POWER:
624 ohci_writel (ohci, RH_PS_PPS,
625 &ohci->regs->roothub.portstatus [wIndex]);
626 break;
627 case USB_PORT_FEAT_RESET:
628 root_port_reset (ohci, wIndex);
629 break;
630 default:
631 goto error;
633 break;
635 default:
636 error:
637 /* "protocol stall" on error */
638 retval = -EPIPE;
640 return retval;