RT-AC66 3.0.0.4.374.130 core
[tomato.git] / release / src-rt-6.x / linux / linux-2.6 / drivers / usb / host / ohci-hcd.c
blob67592c28d3deeadfb3f284f944f052cdb9a2a584
1 /*
2 * OHCI HCD (Host Controller Driver) for USB.
4 * (C) Copyright 1999 Roman Weissgaerber <weissg@vienna.at>
5 * (C) Copyright 2000-2004 David Brownell <dbrownell@users.sourceforge.net>
7 * [ Initialisation is based on Linus' ]
8 * [ uhci code and gregs ohci fragments ]
9 * [ (C) Copyright 1999 Linus Torvalds ]
10 * [ (C) Copyright 1999 Gregory P. Smith]
13 * OHCI is the main "non-Intel/VIA" standard for USB 1.1 host controller
14 * interfaces (though some non-x86 Intel chips use it). It supports
15 * smarter hardware than UHCI. A download link for the spec available
16 * through the http://www.usb.org website.
18 * This file is licenced under the GPL.
21 #include <linux/module.h>
22 #include <linux/moduleparam.h>
23 #include <linux/pci.h>
24 #include <linux/kernel.h>
25 #include <linux/delay.h>
26 #include <linux/ioport.h>
27 #include <linux/sched.h>
28 #include <linux/slab.h>
29 #include <linux/errno.h>
30 #include <linux/init.h>
31 #include <linux/timer.h>
32 #include <linux/list.h>
33 #include <linux/usb.h>
34 #include <linux/usb/otg.h>
35 #include <linux/dma-mapping.h>
36 #include <linux/dmapool.h>
37 #include <linux/reboot.h>
38 #include <linux/workqueue.h>
39 #include <linux/debugfs.h>
41 #include <asm/io.h>
42 #include <asm/irq.h>
43 #include <asm/system.h>
44 #include <asm/unaligned.h>
45 #include <asm/byteorder.h>
47 #include "../core/hcd.h"
49 #define DRIVER_AUTHOR "Roman Weissgaerber, David Brownell"
50 #define DRIVER_DESC "USB 1.1 'Open' Host Controller (OHCI) Driver"
52 /*-------------------------------------------------------------------------*/
54 #undef OHCI_VERBOSE_DEBUG /* not always helpful */
56 /* For initializing controller (mask in an HCFS mode too) */
57 #define OHCI_CONTROL_INIT OHCI_CTRL_CBSR
58 #define OHCI_INTR_INIT \
59 (OHCI_INTR_MIE | OHCI_INTR_RHSC | OHCI_INTR_UE \
60 | OHCI_INTR_RD | OHCI_INTR_WDH)
62 #ifdef __hppa__
63 /* On PA-RISC, PDC can leave IR set incorrectly; ignore it there. */
64 #define IR_DISABLE
65 #endif
67 #ifdef CONFIG_ARCH_OMAP
68 /* OMAP doesn't support IR (no SMM; not needed) */
69 #define IR_DISABLE
70 #endif
72 /*-------------------------------------------------------------------------*/
74 static const char hcd_name [] = "ohci_hcd";
76 #define STATECHANGE_DELAY msecs_to_jiffies(300)
78 #include "ohci.h"
80 static void ohci_dump (struct ohci_hcd *ohci, int verbose);
81 static int ohci_init (struct ohci_hcd *ohci);
82 static void ohci_stop (struct usb_hcd *hcd);
84 #if defined(CONFIG_PM) || defined(CONFIG_PCI)
85 static int ohci_restart (struct ohci_hcd *ohci);
86 #endif
88 #include "ohci-hub.c"
89 #include "ohci-dbg.c"
90 #include "ohci-mem.c"
91 #include "ohci-q.c"
95 * On architectures with edge-triggered interrupts we must never return
96 * IRQ_NONE.
98 #if defined(CONFIG_SA1111) /* ... or other edge-triggered systems */
99 #define IRQ_NOTMINE IRQ_HANDLED
100 #else
101 #define IRQ_NOTMINE IRQ_NONE
102 #endif
105 /* Some boards misreport power switching/overcurrent */
106 static int distrust_firmware = 1;
107 module_param (distrust_firmware, bool, 0);
108 MODULE_PARM_DESC (distrust_firmware,
109 "true to distrust firmware power/overcurrent setup");
111 /* Some boards leave IR set wrongly, since they fail BIOS/SMM handshakes */
112 static int no_handshake = 0;
113 module_param (no_handshake, bool, 0);
114 MODULE_PARM_DESC (no_handshake, "true (not default) disables BIOS handshake");
116 /*-------------------------------------------------------------------------*/
119 * queue up an urb for anything except the root hub
121 static int ohci_urb_enqueue (
122 struct usb_hcd *hcd,
123 struct urb *urb,
124 gfp_t mem_flags
126 struct ohci_hcd *ohci = hcd_to_ohci (hcd);
127 struct ed *ed;
128 urb_priv_t *urb_priv;
129 unsigned int pipe = urb->pipe;
130 int i, size = 0;
131 unsigned long flags;
132 int retval = 0;
134 #ifdef OHCI_VERBOSE_DEBUG
135 urb_print(urb, "SUB", usb_pipein(pipe), -EINPROGRESS);
136 #endif
138 /* every endpoint has a ed, locate and maybe (re)initialize it */
139 if (! (ed = ed_get (ohci, urb->ep, urb->dev, pipe, urb->interval)))
140 return -ENOMEM;
142 /* for the private part of the URB we need the number of TDs (size) */
143 switch (ed->type) {
144 case PIPE_CONTROL:
145 /* td_submit_urb() doesn't yet handle these */
146 if (urb->transfer_buffer_length > 4096)
147 return -EMSGSIZE;
149 /* 1 TD for setup, 1 for ACK, plus ... */
150 size = 2;
151 /* FALLTHROUGH */
152 // case PIPE_INTERRUPT:
153 // case PIPE_BULK:
154 default:
155 /* one TD for every 4096 Bytes (can be upto 8K) */
156 size += urb->transfer_buffer_length / 4096;
157 /* ... and for any remaining bytes ... */
158 if ((urb->transfer_buffer_length % 4096) != 0)
159 size++;
160 /* ... and maybe a zero length packet to wrap it up */
161 if (size == 0)
162 size++;
163 else if ((urb->transfer_flags & URB_ZERO_PACKET) != 0
164 && (urb->transfer_buffer_length
165 % usb_maxpacket (urb->dev, pipe,
166 usb_pipeout (pipe))) == 0)
167 size++;
168 break;
169 case PIPE_ISOCHRONOUS: /* number of packets from URB */
170 size = urb->number_of_packets;
171 break;
174 /* allocate the private part of the URB */
175 urb_priv = kzalloc (sizeof (urb_priv_t) + size * sizeof (struct td *),
176 mem_flags);
177 if (!urb_priv)
178 return -ENOMEM;
179 INIT_LIST_HEAD (&urb_priv->pending);
180 urb_priv->length = size;
181 urb_priv->ed = ed;
183 /* allocate the TDs (deferring hash chain updates) */
184 for (i = 0; i < size; i++) {
185 urb_priv->td [i] = td_alloc (ohci, mem_flags);
186 if (!urb_priv->td [i]) {
187 urb_priv->length = i;
188 urb_free_priv (ohci, urb_priv);
189 return -ENOMEM;
193 spin_lock_irqsave (&ohci->lock, flags);
195 /* don't submit to a dead HC */
196 if (!test_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags)) {
197 retval = -ENODEV;
198 goto fail;
200 if (!HC_IS_RUNNING(hcd->state)) {
201 retval = -ENODEV;
202 goto fail;
204 retval = usb_hcd_link_urb_to_ep(hcd, urb);
205 if (retval)
206 goto fail;
208 /* schedule the ed if needed */
209 if (ed->state == ED_IDLE) {
210 retval = ed_schedule (ohci, ed);
211 if (retval < 0) {
212 usb_hcd_unlink_urb_from_ep(hcd, urb);
213 goto fail;
215 if (ed->type == PIPE_ISOCHRONOUS) {
216 u16 frame = ohci_frame_no(ohci);
218 /* delay a few frames before the first TD */
219 frame += max_t (u16, 8, ed->interval);
220 frame &= ~(ed->interval - 1);
221 frame |= ed->branch;
222 urb->start_frame = frame;
224 /* yes, only URB_ISO_ASAP is supported, and
225 * urb->start_frame is never used as input.
228 } else if (ed->type == PIPE_ISOCHRONOUS)
229 urb->start_frame = ed->last_iso + ed->interval;
231 /* fill the TDs and link them to the ed; and
232 * enable that part of the schedule, if needed
233 * and update count of queued periodic urbs
235 urb->hcpriv = urb_priv;
236 td_submit_urb (ohci, urb);
238 fail:
239 if (retval)
240 urb_free_priv (ohci, urb_priv);
241 spin_unlock_irqrestore (&ohci->lock, flags);
242 return retval;
246 * decouple the URB from the HC queues (TDs, urb_priv).
247 * reporting is always done
248 * asynchronously, and we might be dealing with an urb that's
249 * partially transferred, or an ED with other urbs being unlinked.
251 static int ohci_urb_dequeue(struct usb_hcd *hcd, struct urb *urb, int status)
253 struct ohci_hcd *ohci = hcd_to_ohci (hcd);
254 unsigned long flags;
255 int rc;
257 #ifdef OHCI_VERBOSE_DEBUG
258 urb_print(urb, "UNLINK", 1, status);
259 #endif
261 spin_lock_irqsave (&ohci->lock, flags);
262 rc = usb_hcd_check_unlink_urb(hcd, urb, status);
263 if (rc) {
264 ; /* Do nothing */
265 } else if (HC_IS_RUNNING(hcd->state)) {
266 urb_priv_t *urb_priv;
268 /* Unless an IRQ completed the unlink while it was being
269 * handed to us, flag it for unlink and giveback, and force
270 * some upcoming INTR_SF to call finish_unlinks()
272 urb_priv = urb->hcpriv;
273 if (urb_priv) {
274 if (urb_priv->ed->state == ED_OPER)
275 start_ed_unlink (ohci, urb_priv->ed);
277 } else {
279 * with HC dead, we won't respect hc queue pointers
280 * any more ... just clean up every urb's memory.
282 if (urb->hcpriv)
283 finish_urb(ohci, urb, status);
285 spin_unlock_irqrestore (&ohci->lock, flags);
286 return rc;
289 /*-------------------------------------------------------------------------*/
291 /* frees config/altsetting state for endpoints,
292 * including ED memory, dummy TD, and bulk/intr data toggle
295 static void
296 ohci_endpoint_disable (struct usb_hcd *hcd, struct usb_host_endpoint *ep)
298 struct ohci_hcd *ohci = hcd_to_ohci (hcd);
299 unsigned long flags;
300 struct ed *ed = ep->hcpriv;
301 unsigned limit = 1000;
303 /* ASSERT: any requests/urbs are being unlinked */
304 /* ASSERT: nobody can be submitting urbs for this any more */
306 if (!ed)
307 return;
309 rescan:
310 spin_lock_irqsave (&ohci->lock, flags);
312 if (!HC_IS_RUNNING (hcd->state)) {
313 sanitize:
314 ed->state = ED_IDLE;
315 if (quirk_zfmicro(ohci) && ed->type == PIPE_INTERRUPT)
316 ohci->eds_scheduled--;
317 finish_unlinks (ohci, 0);
320 switch (ed->state) {
321 case ED_UNLINK: /* wait for hw to finish? */
322 /* major IRQ delivery trouble loses INTR_SF too... */
323 if (limit-- == 0) {
324 ohci_warn(ohci, "ED unlink timeout\n");
325 if (quirk_zfmicro(ohci)) {
326 ohci_warn(ohci, "Attempting ZF TD recovery\n");
327 ohci->ed_to_check = ed;
328 ohci->zf_delay = 2;
330 goto sanitize;
332 spin_unlock_irqrestore (&ohci->lock, flags);
333 schedule_timeout_uninterruptible(1);
334 goto rescan;
335 case ED_IDLE: /* fully unlinked */
336 if (list_empty (&ed->td_list)) {
337 td_free (ohci, ed->dummy);
338 ed_free (ohci, ed);
339 break;
341 /* else FALL THROUGH */
342 default:
343 /* caller was supposed to have unlinked any requests;
344 * that's not our job. can't recover; must leak ed.
346 ohci_err (ohci, "leak ed %p (#%02x) state %d%s\n",
347 ed, ep->desc.bEndpointAddress, ed->state,
348 list_empty (&ed->td_list) ? "" : " (has tds)");
349 td_free (ohci, ed->dummy);
350 break;
352 ep->hcpriv = NULL;
353 spin_unlock_irqrestore (&ohci->lock, flags);
354 return;
357 static int ohci_get_frame (struct usb_hcd *hcd)
359 struct ohci_hcd *ohci = hcd_to_ohci (hcd);
361 return ohci_frame_no(ohci);
364 static void ohci_usb_reset (struct ohci_hcd *ohci)
366 ohci->hc_control = ohci_readl (ohci, &ohci->regs->control);
367 ohci->hc_control &= OHCI_CTRL_RWC;
368 ohci_writel (ohci, ohci->hc_control, &ohci->regs->control);
371 /* ohci_shutdown forcibly disables IRQs and DMA, helping kexec and
372 * other cases where the next software may expect clean state from the
373 * "firmware". this is bus-neutral, unlike shutdown() methods.
375 static void
376 ohci_shutdown (struct usb_hcd *hcd)
378 struct ohci_hcd *ohci;
380 ohci = hcd_to_ohci (hcd);
381 ohci_writel (ohci, OHCI_INTR_MIE, &ohci->regs->intrdisable);
382 ohci_usb_reset (ohci);
383 /* flush the writes */
384 (void) ohci_readl (ohci, &ohci->regs->control);
387 static int check_ed(struct ohci_hcd *ohci, struct ed *ed)
389 return (hc32_to_cpu(ohci, ed->hwINFO) & ED_IN) != 0
390 && (hc32_to_cpu(ohci, ed->hwHeadP) & TD_MASK)
391 == (hc32_to_cpu(ohci, ed->hwTailP) & TD_MASK)
392 && !list_empty(&ed->td_list);
395 /* ZF Micro watchdog timer callback. The ZF Micro chipset sometimes completes
396 * an interrupt TD but neglects to add it to the donelist. On systems with
397 * this chipset, we need to periodically check the state of the queues to look
398 * for such "lost" TDs.
400 static void unlink_watchdog_func(unsigned long _ohci)
402 unsigned long flags;
403 unsigned max;
404 unsigned seen_count = 0;
405 unsigned i;
406 struct ed **seen = NULL;
407 struct ohci_hcd *ohci = (struct ohci_hcd *) _ohci;
409 spin_lock_irqsave(&ohci->lock, flags);
410 max = ohci->eds_scheduled;
411 if (!max)
412 goto done;
414 if (ohci->ed_to_check)
415 goto out;
417 seen = kcalloc(max, sizeof *seen, GFP_ATOMIC);
418 if (!seen)
419 goto out;
421 for (i = 0; i < NUM_INTS; i++) {
422 struct ed *ed = ohci->periodic[i];
424 while (ed) {
425 unsigned temp;
427 /* scan this branch of the periodic schedule tree */
428 for (temp = 0; temp < seen_count; temp++) {
429 if (seen[temp] == ed) {
430 /* we've checked it and what's after */
431 ed = NULL;
432 break;
435 if (!ed)
436 break;
437 seen[seen_count++] = ed;
438 if (!check_ed(ohci, ed)) {
439 ed = ed->ed_next;
440 continue;
443 /* HC's TD list is empty, but HCD sees at least one
444 * TD that's not been sent through the donelist.
446 ohci->ed_to_check = ed;
447 ohci->zf_delay = 2;
449 /* The HC may wait until the next frame to report the
450 * TD as done through the donelist and INTR_WDH. (We
451 * just *assume* it's not a multi-TD interrupt URB;
452 * those could defer the IRQ more than one frame, using
453 * DI...) Check again after the next INTR_SF.
455 ohci_writel(ohci, OHCI_INTR_SF,
456 &ohci->regs->intrstatus);
457 ohci_writel(ohci, OHCI_INTR_SF,
458 &ohci->regs->intrenable);
460 /* flush those writes */
461 (void) ohci_readl(ohci, &ohci->regs->control);
463 goto out;
466 out:
467 kfree(seen);
468 if (ohci->eds_scheduled)
469 mod_timer(&ohci->unlink_watchdog, round_jiffies(jiffies + HZ));
470 done:
471 spin_unlock_irqrestore(&ohci->lock, flags);
474 /*-------------------------------------------------------------------------*
475 * HC functions
476 *-------------------------------------------------------------------------*/
478 /* init memory, and kick BIOS/SMM off */
480 static int ohci_init (struct ohci_hcd *ohci)
482 int ret;
483 struct usb_hcd *hcd = ohci_to_hcd(ohci);
485 disable (ohci);
486 ohci->regs = hcd->regs;
488 /* REVISIT this BIOS handshake is now moved into PCI "quirks", and
489 * was never needed for most non-PCI systems ... remove the code?
492 #ifndef IR_DISABLE
493 /* SMM owns the HC? not for long! */
494 if (!no_handshake && ohci_readl (ohci,
495 &ohci->regs->control) & OHCI_CTRL_IR) {
496 u32 temp;
498 ohci_dbg (ohci, "USB HC TakeOver from BIOS/SMM\n");
500 /* this timeout is arbitrary. we make it long, so systems
501 * depending on usb keyboards may be usable even if the
502 * BIOS/SMM code seems pretty broken.
504 temp = 500; /* arbitrary: five seconds */
506 ohci_writel (ohci, OHCI_INTR_OC, &ohci->regs->intrenable);
507 ohci_writel (ohci, OHCI_OCR, &ohci->regs->cmdstatus);
508 while (ohci_readl (ohci, &ohci->regs->control) & OHCI_CTRL_IR) {
509 msleep (10);
510 if (--temp == 0) {
511 ohci_err (ohci, "USB HC takeover failed!"
512 " (BIOS/SMM bug)\n");
513 return -EBUSY;
516 ohci_usb_reset (ohci);
518 #endif
520 /* Disable HC interrupts */
521 ohci_writel (ohci, OHCI_INTR_MIE, &ohci->regs->intrdisable);
523 /* flush the writes, and save key bits like RWC */
524 if (ohci_readl (ohci, &ohci->regs->control) & OHCI_CTRL_RWC)
525 ohci->hc_control |= OHCI_CTRL_RWC;
527 /* Read the number of ports unless overridden */
528 if (ohci->num_ports == 0)
529 ohci->num_ports = roothub_a(ohci) & RH_A_NDP;
531 if (ohci->hcca)
532 return 0;
534 ohci->hcca = dma_alloc_coherent (hcd->self.controller,
535 sizeof *ohci->hcca, &ohci->hcca_dma, 0);
536 if (!ohci->hcca)
537 return -ENOMEM;
539 if ((ret = ohci_mem_init (ohci)) < 0)
540 ohci_stop (hcd);
541 else {
542 create_debug_files (ohci);
545 return ret;
548 /*-------------------------------------------------------------------------*/
550 /* Start an OHCI controller, set the BUS operational
551 * resets USB and controller
552 * enable interrupts
554 static int ohci_run (struct ohci_hcd *ohci)
556 u32 mask, temp;
557 int first = ohci->fminterval == 0;
558 struct usb_hcd *hcd = ohci_to_hcd(ohci);
560 disable (ohci);
562 /* boot firmware should have set this up (5.1.1.3.1) */
563 if (first) {
565 temp = ohci_readl (ohci, &ohci->regs->fminterval);
566 ohci->fminterval = temp & 0x3fff;
567 if (ohci->fminterval != FI)
568 ohci_dbg (ohci, "fminterval delta %d\n",
569 ohci->fminterval - FI);
570 ohci->fminterval |= FSMP (ohci->fminterval) << 16;
571 /* also: power/overcurrent flags in roothub.a */
574 /* Reset USB nearly "by the book". RemoteWakeupConnected was
575 * saved if boot firmware (BIOS/SMM/...) told us it's connected,
576 * or if bus glue did the same (e.g. for PCI add-in cards with
577 * PCI PM support).
579 if ((ohci->hc_control & OHCI_CTRL_RWC) != 0
580 && !device_may_wakeup(hcd->self.controller))
581 device_init_wakeup(hcd->self.controller, 1);
583 switch (ohci->hc_control & OHCI_CTRL_HCFS) {
584 case OHCI_USB_OPER:
585 temp = 0;
586 break;
587 case OHCI_USB_SUSPEND:
588 case OHCI_USB_RESUME:
589 ohci->hc_control &= OHCI_CTRL_RWC;
590 ohci->hc_control |= OHCI_USB_RESUME;
591 temp = 10 /* msec wait */;
592 break;
593 // case OHCI_USB_RESET:
594 default:
595 ohci->hc_control &= OHCI_CTRL_RWC;
596 ohci->hc_control |= OHCI_USB_RESET;
597 temp = 50 /* msec wait */;
598 break;
600 ohci_writel (ohci, ohci->hc_control, &ohci->regs->control);
601 // flush the writes
602 (void) ohci_readl (ohci, &ohci->regs->control);
603 msleep(temp);
605 memset (ohci->hcca, 0, sizeof (struct ohci_hcca));
607 /* 2msec timelimit here means no irqs/preempt */
608 spin_lock_irq (&ohci->lock);
610 retry:
611 /* HC Reset requires max 10 us delay */
612 ohci_writel (ohci, OHCI_HCR, &ohci->regs->cmdstatus);
613 temp = 30; /* ... allow extra time */
614 while ((ohci_readl (ohci, &ohci->regs->cmdstatus) & OHCI_HCR) != 0) {
615 if (--temp == 0) {
616 spin_unlock_irq (&ohci->lock);
617 ohci_err (ohci, "USB HC reset timed out!\n");
618 return -1;
620 udelay (1);
623 /* now we're in the SUSPEND state ... must go OPERATIONAL
624 * within 2msec else HC enters RESUME
626 * ... but some hardware won't init fmInterval "by the book"
627 * (SiS, OPTi ...), so reset again instead. SiS doesn't need
628 * this if we write fmInterval after we're OPERATIONAL.
629 * Unclear about ALi, ServerWorks, and others ... this could
630 * easily be a longstanding bug in chip init on Linux.
632 if (ohci->flags & OHCI_QUIRK_INITRESET) {
633 ohci_writel (ohci, ohci->hc_control, &ohci->regs->control);
634 // flush those writes
635 (void) ohci_readl (ohci, &ohci->regs->control);
638 /* Tell the controller where the control and bulk lists are
639 * The lists are empty now. */
640 ohci_writel (ohci, 0, &ohci->regs->ed_controlhead);
641 ohci_writel (ohci, 0, &ohci->regs->ed_bulkhead);
643 /* a reset clears this */
644 ohci_writel (ohci, (u32) ohci->hcca_dma, &ohci->regs->hcca);
646 periodic_reinit (ohci);
648 /* some OHCI implementations are finicky about how they init.
649 * bogus values here mean not even enumeration could work.
651 if ((ohci_readl (ohci, &ohci->regs->fminterval) & 0x3fff0000) == 0
652 || !ohci_readl (ohci, &ohci->regs->periodicstart)) {
653 if (!(ohci->flags & OHCI_QUIRK_INITRESET)) {
654 ohci->flags |= OHCI_QUIRK_INITRESET;
655 ohci_dbg (ohci, "enabling initreset quirk\n");
656 goto retry;
658 spin_unlock_irq (&ohci->lock);
659 ohci_err (ohci, "init err (%08x %04x)\n",
660 ohci_readl (ohci, &ohci->regs->fminterval),
661 ohci_readl (ohci, &ohci->regs->periodicstart));
662 return -EOVERFLOW;
665 /* use rhsc irqs after khubd is fully initialized */
666 hcd->poll_rh = 1;
667 hcd->uses_new_polling = 1;
669 /* start controller operations */
670 ohci->hc_control &= OHCI_CTRL_RWC;
671 ohci->hc_control |= OHCI_CONTROL_INIT | OHCI_USB_OPER;
672 ohci_writel (ohci, ohci->hc_control, &ohci->regs->control);
673 hcd->state = HC_STATE_RUNNING;
675 /* wake on ConnectStatusChange, matching external hubs */
676 ohci_writel (ohci, RH_HS_DRWE, &ohci->regs->roothub.status);
678 /* Choose the interrupts we care about now, others later on demand */
679 mask = OHCI_INTR_INIT;
680 ohci_writel (ohci, ~0, &ohci->regs->intrstatus);
681 ohci_writel (ohci, mask, &ohci->regs->intrenable);
683 /* handle root hub init quirks ... */
684 temp = roothub_a (ohci);
685 temp &= ~(RH_A_PSM | RH_A_OCPM);
686 if (ohci->flags & OHCI_QUIRK_SUPERIO) {
687 /* NSC 87560 and maybe others */
688 temp |= RH_A_NOCP;
689 temp &= ~(RH_A_POTPGT | RH_A_NPS);
690 ohci_writel (ohci, temp, &ohci->regs->roothub.a);
691 } else if ((ohci->flags & OHCI_QUIRK_AMD756) || distrust_firmware) {
692 /* hub power always on; required for AMD-756 and some
693 * Mac platforms. ganged overcurrent reporting, if any.
695 temp |= RH_A_NPS;
696 ohci_writel (ohci, temp, &ohci->regs->roothub.a);
698 ohci_writel (ohci, RH_HS_LPSC, &ohci->regs->roothub.status);
699 ohci_writel (ohci, (temp & RH_A_NPS) ? 0 : RH_B_PPCM,
700 &ohci->regs->roothub.b);
701 // flush those writes
702 (void) ohci_readl (ohci, &ohci->regs->control);
704 ohci->next_statechange = jiffies + STATECHANGE_DELAY;
705 spin_unlock_irq (&ohci->lock);
707 // POTPGT delay is bits 24-31, in 2 ms units.
708 mdelay ((temp >> 23) & 0x1fe);
709 hcd->state = HC_STATE_RUNNING;
711 if (quirk_zfmicro(ohci)) {
712 /* Create timer to watch for bad queue state on ZF Micro */
713 setup_timer(&ohci->unlink_watchdog, unlink_watchdog_func,
714 (unsigned long) ohci);
716 ohci->eds_scheduled = 0;
717 ohci->ed_to_check = NULL;
720 ohci_dump (ohci, 1);
722 return 0;
725 /*-------------------------------------------------------------------------*/
727 /* an interrupt happens */
729 static irqreturn_t ohci_irq (struct usb_hcd *hcd)
731 struct ohci_hcd *ohci = hcd_to_ohci (hcd);
732 struct ohci_regs __iomem *regs = ohci->regs;
733 int ints;
735 /* Read interrupt status (and flush pending writes). We ignore the
736 * optimization of checking the LSB of hcca->done_head; it doesn't
737 * work on all systems (edge triggering for OHCI can be a factor).
739 ints = ohci_readl(ohci, &regs->intrstatus);
741 /* Check for an all 1's result which is a typical consequence
742 * of dead, unclocked, or unplugged (CardBus...) devices
744 if (ints == ~(u32)0) {
745 disable (ohci);
746 ohci_dbg (ohci, "device removed!\n");
747 return IRQ_HANDLED;
750 /* We only care about interrupts that are enabled */
751 ints &= ohci_readl(ohci, &regs->intrenable);
753 /* interrupt for some other device? */
754 if (ints == 0)
755 return IRQ_NOTMINE;
757 if (ints & OHCI_INTR_UE) {
758 // e.g. due to PCI Master/Target Abort
759 if (quirk_nec(ohci)) {
760 /* Workaround for a silicon bug in some NEC chips used
761 * in Apple's PowerBooks. Adapted from Darwin code.
763 ohci_err (ohci, "OHCI Unrecoverable Error, scheduling NEC chip restart\n");
765 ohci_writel (ohci, OHCI_INTR_UE, &regs->intrdisable);
767 schedule_work (&ohci->nec_work);
768 } else {
769 disable (ohci);
770 ohci_err (ohci, "OHCI Unrecoverable Error, disabled\n");
773 ohci_dump (ohci, 1);
774 ohci_usb_reset (ohci);
777 if (ints & OHCI_INTR_RHSC) {
778 ohci_vdbg(ohci, "rhsc\n");
779 ohci->next_statechange = jiffies + STATECHANGE_DELAY;
780 ohci_writel(ohci, OHCI_INTR_RD | OHCI_INTR_RHSC,
781 &regs->intrstatus);
783 /* NOTE: Vendors didn't always make the same implementation
784 * choices for RHSC. Many followed the spec; RHSC triggers
785 * on an edge, like setting and maybe clearing a port status
786 * change bit. With others it's level-triggered, active
787 * until khubd clears all the port status change bits. We'll
788 * always disable it here and rely on polling until khubd
789 * re-enables it.
791 ohci_writel(ohci, OHCI_INTR_RHSC, &regs->intrdisable);
792 usb_hcd_poll_rh_status(hcd);
795 /* For connect and disconnect events, we expect the controller
796 * to turn on RHSC along with RD. But for remote wakeup events
797 * this might not happen.
799 else if (ints & OHCI_INTR_RD) {
800 ohci_vdbg(ohci, "resume detect\n");
801 ohci_writel(ohci, OHCI_INTR_RD, &regs->intrstatus);
802 hcd->poll_rh = 1;
803 if (ohci->autostop) {
804 spin_lock (&ohci->lock);
805 ohci_rh_resume (ohci);
806 spin_unlock (&ohci->lock);
807 } else
808 usb_hcd_resume_root_hub(hcd);
811 /* Some controllers violate the OHCI spec by setting OHCI_INTR_WDH
812 * before writing ohci->hcca->done_head. They don't generate an
813 * early interrupt, but if the IRQ line is shared then we might see
814 * the status bit prematurely.
816 if (ints & OHCI_INTR_WDH) {
817 if (ohci->hcca->done_head == 0) {
818 ints &= ~OHCI_INTR_WDH;
819 } else {
820 spin_lock(&ohci->lock);
821 dl_done_list(ohci);
822 spin_unlock(&ohci->lock);
826 if (quirk_zfmicro(ohci) && (ints & OHCI_INTR_SF)) {
827 spin_lock(&ohci->lock);
828 if (ohci->ed_to_check) {
829 struct ed *ed = ohci->ed_to_check;
831 if (check_ed(ohci, ed)) {
832 /* HC thinks the TD list is empty; HCD knows
833 * at least one TD is outstanding
835 if (--ohci->zf_delay == 0) {
836 struct td *td = list_entry(
837 ed->td_list.next,
838 struct td, td_list);
839 ohci_warn(ohci,
840 "Reclaiming orphan TD %p\n",
841 td);
842 takeback_td(ohci, td);
843 ohci->ed_to_check = NULL;
845 } else
846 ohci->ed_to_check = NULL;
848 spin_unlock(&ohci->lock);
851 /* could track INTR_SO to reduce available PCI/... bandwidth */
853 /* handle any pending URB/ED unlinks, leaving INTR_SF enabled
854 * when there's still unlinking to be done (next frame).
856 spin_lock (&ohci->lock);
857 if (ohci->ed_rm_list)
858 finish_unlinks (ohci, ohci_frame_no(ohci));
859 if ((ints & OHCI_INTR_SF) != 0
860 && !ohci->ed_rm_list
861 && !ohci->ed_to_check
862 && HC_IS_RUNNING(hcd->state))
863 ohci_writel (ohci, OHCI_INTR_SF, &regs->intrdisable);
864 spin_unlock (&ohci->lock);
866 if (HC_IS_RUNNING(hcd->state)) {
867 ohci_writel (ohci, ints, &regs->intrstatus);
868 ohci_writel (ohci, OHCI_INTR_MIE, &regs->intrenable);
869 // flush those writes
870 (void) ohci_readl (ohci, &ohci->regs->control);
873 return IRQ_HANDLED;
876 /*-------------------------------------------------------------------------*/
878 static void ohci_stop (struct usb_hcd *hcd)
880 struct ohci_hcd *ohci = hcd_to_ohci (hcd);
882 ohci_dump (ohci, 1);
884 flush_scheduled_work();
886 ohci_usb_reset (ohci);
887 ohci_writel (ohci, OHCI_INTR_MIE, &ohci->regs->intrdisable);
888 free_irq(hcd->irq, hcd);
889 hcd->irq = -1;
891 if (quirk_zfmicro(ohci))
892 del_timer(&ohci->unlink_watchdog);
894 remove_debug_files (ohci);
895 ohci_mem_cleanup (ohci);
896 if (ohci->hcca) {
897 dma_free_coherent (hcd->self.controller,
898 sizeof *ohci->hcca,
899 ohci->hcca, ohci->hcca_dma);
900 ohci->hcca = NULL;
901 ohci->hcca_dma = 0;
905 /*-------------------------------------------------------------------------*/
907 #if defined(CONFIG_PM) || defined(CONFIG_PCI)
909 /* must not be called from interrupt context */
910 static int ohci_restart (struct ohci_hcd *ohci)
912 int temp;
913 int i;
914 struct urb_priv *priv;
916 spin_lock_irq(&ohci->lock);
917 disable (ohci);
919 /* Recycle any "live" eds/tds (and urbs). */
920 if (!list_empty (&ohci->pending))
921 ohci_dbg(ohci, "abort schedule...\n");
922 list_for_each_entry (priv, &ohci->pending, pending) {
923 struct urb *urb = priv->td[0]->urb;
924 struct ed *ed = priv->ed;
926 switch (ed->state) {
927 case ED_OPER:
928 ed->state = ED_UNLINK;
929 ed->hwINFO |= cpu_to_hc32(ohci, ED_DEQUEUE);
930 ed_deschedule (ohci, ed);
932 ed->ed_next = ohci->ed_rm_list;
933 ed->ed_prev = NULL;
934 ohci->ed_rm_list = ed;
935 /* FALLTHROUGH */
936 case ED_UNLINK:
937 break;
938 default:
939 ohci_dbg(ohci, "bogus ed %p state %d\n",
940 ed, ed->state);
943 if (!urb->unlinked)
944 urb->unlinked = -ESHUTDOWN;
946 finish_unlinks (ohci, 0);
947 spin_unlock_irq(&ohci->lock);
949 /* paranoia, in case that didn't work: */
951 /* empty the interrupt branches */
952 for (i = 0; i < NUM_INTS; i++) ohci->load [i] = 0;
953 for (i = 0; i < NUM_INTS; i++) ohci->hcca->int_table [i] = 0;
955 /* no EDs to remove */
956 ohci->ed_rm_list = NULL;
958 /* empty control and bulk lists */
959 ohci->ed_controltail = NULL;
960 ohci->ed_bulktail = NULL;
962 if ((temp = ohci_run (ohci)) < 0) {
963 ohci_err (ohci, "can't restart, %d\n", temp);
964 return temp;
966 ohci_dbg(ohci, "restart complete\n");
967 return 0;
970 #endif
972 /*-------------------------------------------------------------------------*/
974 MODULE_AUTHOR (DRIVER_AUTHOR);
975 MODULE_DESCRIPTION(DRIVER_DESC);
976 MODULE_LICENSE ("GPL");
978 #ifdef CONFIG_PCI
979 #include "ohci-pci.c"
980 #define PCI_DRIVER ohci_pci_driver
981 #endif
983 #ifdef CONFIG_SA1111
984 #include "ohci-sa1111.c"
985 #define SA1111_DRIVER ohci_hcd_sa1111_driver
986 #endif
988 #ifdef CONFIG_ARCH_S3C2410
989 #include "ohci-s3c2410.c"
990 #define PLATFORM_DRIVER ohci_hcd_s3c2410_driver
991 #endif
993 #ifdef CONFIG_ARCH_OMAP
994 #include "ohci-omap.c"
995 #define PLATFORM_DRIVER ohci_hcd_omap_driver
996 #endif
998 #ifdef CONFIG_ARCH_LH7A404
999 #include "ohci-lh7a404.c"
1000 #define PLATFORM_DRIVER ohci_hcd_lh7a404_driver
1001 #endif
1003 #ifdef CONFIG_PXA27x
1004 #include "ohci-pxa27x.c"
1005 #define PLATFORM_DRIVER ohci_hcd_pxa27x_driver
1006 #endif
1008 #ifdef CONFIG_ARCH_EP93XX
1009 #include "ohci-ep93xx.c"
1010 #define PLATFORM_DRIVER ohci_hcd_ep93xx_driver
1011 #endif
1013 #ifdef CONFIG_SOC_AU1X00
1014 #include "ohci-au1xxx.c"
1015 #define PLATFORM_DRIVER ohci_hcd_au1xxx_driver
1016 #endif
1018 #ifdef CONFIG_PNX8550
1019 #include "ohci-pnx8550.c"
1020 #define PLATFORM_DRIVER ohci_hcd_pnx8550_driver
1021 #endif
1023 #ifdef CONFIG_USB_OHCI_HCD_PPC_SOC
1024 #include "ohci-ppc-soc.c"
1025 #define PLATFORM_DRIVER ohci_hcd_ppc_soc_driver
1026 #endif
1028 #ifdef CONFIG_ARCH_AT91
1029 #include "ohci-at91.c"
1030 #define PLATFORM_DRIVER ohci_hcd_at91_driver
1031 #endif
1033 #ifdef CONFIG_ARCH_PNX4008
1034 #include "ohci-pnx4008.c"
1035 #define PLATFORM_DRIVER usb_hcd_pnx4008_driver
1036 #endif
1039 #ifdef CONFIG_USB_OHCI_HCD_PPC_OF
1040 #include "ohci-ppc-of.c"
1041 #define OF_PLATFORM_DRIVER ohci_hcd_ppc_of_driver
1042 #endif
1044 #ifdef CONFIG_PPC_PS3
1045 #include "ohci-ps3.c"
1046 #define PS3_SYSTEM_BUS_DRIVER ps3_ohci_driver
1047 #endif
1049 #if !defined(PCI_DRIVER) && \
1050 !defined(PLATFORM_DRIVER) && \
1051 !defined(OF_PLATFORM_DRIVER) && \
1052 !defined(SA1111_DRIVER) && \
1053 !defined(PS3_SYSTEM_BUS_DRIVER)
1054 #error "missing bus glue for ohci-hcd"
1055 #endif
1057 static int __init ohci_hcd_mod_init(void)
1059 int retval = 0;
1061 if (usb_disabled())
1062 return -ENODEV;
1064 printk(KERN_INFO "%s: " DRIVER_DESC "\n", hcd_name);
1065 pr_debug ("%s: block sizes: ed %Zd td %Zd\n", hcd_name,
1066 sizeof (struct ed), sizeof (struct td));
1067 set_bit(USB_OHCI_LOADED, &usb_hcds_loaded);
1069 #ifdef DEBUG
1070 ohci_debug_root = debugfs_create_dir("ohci", NULL);
1071 if (!ohci_debug_root) {
1072 retval = -ENOENT;
1073 goto error_debug;
1075 #endif
1077 #ifdef PS3_SYSTEM_BUS_DRIVER
1078 retval = ps3_ohci_driver_register(&PS3_SYSTEM_BUS_DRIVER);
1079 if (retval < 0)
1080 goto error_ps3;
1081 #endif
1083 #ifdef PLATFORM_DRIVER
1084 retval = platform_driver_register(&PLATFORM_DRIVER);
1085 if (retval < 0)
1086 goto error_platform;
1087 #endif
1089 #ifdef OF_PLATFORM_DRIVER
1090 retval = of_register_platform_driver(&OF_PLATFORM_DRIVER);
1091 if (retval < 0)
1092 goto error_of_platform;
1093 #endif
1095 #ifdef SA1111_DRIVER
1096 retval = sa1111_driver_register(&SA1111_DRIVER);
1097 if (retval < 0)
1098 goto error_sa1111;
1099 #endif
1101 #ifdef PCI_DRIVER
1102 retval = pci_register_driver(&PCI_DRIVER);
1103 if (retval < 0)
1104 goto error_pci;
1105 #endif
1107 return retval;
1109 /* Error path */
1110 #ifdef PCI_DRIVER
1111 error_pci:
1112 #endif
1113 #ifdef SA1111_DRIVER
1114 sa1111_driver_unregister(&SA1111_DRIVER);
1115 error_sa1111:
1116 #endif
1117 #ifdef OF_PLATFORM_DRIVER
1118 of_unregister_platform_driver(&OF_PLATFORM_DRIVER);
1119 error_of_platform:
1120 #endif
1121 #ifdef PLATFORM_DRIVER
1122 platform_driver_unregister(&PLATFORM_DRIVER);
1123 error_platform:
1124 #endif
1125 #ifdef PS3_SYSTEM_BUS_DRIVER
1126 ps3_ohci_driver_unregister(&PS3_SYSTEM_BUS_DRIVER);
1127 error_ps3:
1128 #endif
1129 #ifdef DEBUG
1130 debugfs_remove(ohci_debug_root);
1131 ohci_debug_root = NULL;
1132 error_debug:
1133 #endif
1135 clear_bit(USB_OHCI_LOADED, &usb_hcds_loaded);
1136 return retval;
1138 module_init(ohci_hcd_mod_init);
1140 static void __exit ohci_hcd_mod_exit(void)
1142 #ifdef PCI_DRIVER
1143 pci_unregister_driver(&PCI_DRIVER);
1144 #endif
1145 #ifdef SA1111_DRIVER
1146 sa1111_driver_unregister(&SA1111_DRIVER);
1147 #endif
1148 #ifdef OF_PLATFORM_DRIVER
1149 of_unregister_platform_driver(&OF_PLATFORM_DRIVER);
1150 #endif
1151 #ifdef PLATFORM_DRIVER
1152 platform_driver_unregister(&PLATFORM_DRIVER);
1153 #endif
1154 #ifdef PS3_SYSTEM_BUS_DRIVER
1155 ps3_ohci_driver_unregister(&PS3_SYSTEM_BUS_DRIVER);
1156 #endif
1157 #ifdef DEBUG
1158 debugfs_remove(ohci_debug_root);
1159 #endif
1160 clear_bit(USB_OHCI_LOADED, &usb_hcds_loaded);
1162 module_exit(ohci_hcd_mod_exit);