VServer 1.9.2 (patch-2.6.8.1-vs1.9.2.diff)
[linux-2.6.git] / drivers / usb / host / ohci-hcd.c
1 /*
2  * OHCI HCD (Host Controller Driver) for USB.
3  *
4  * (C) Copyright 1999 Roman Weissgaerber <weissg@vienna.at>
5  * (C) Copyright 2000-2002 David Brownell <dbrownell@users.sourceforge.net>
6  * 
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]
11  * 
12  * 
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.
17  *
18  * History:
19  * 
20  * 2004/03/24 LH7A404 support (Durgesh Pattamatta & Marc Singer)
21  * 2004/02/04 use generic dma_* functions instead of pci_* (dsaxena@plexity.net)
22  * 2003/02/24 show registers in sysfs (Kevin Brosius)
23  *
24  * 2002/09/03 get rid of ed hashtables, rework periodic scheduling and
25  *      bandwidth accounting; if debugging, show schedules in driverfs
26  * 2002/07/19 fixes to management of ED and schedule state.
27  * 2002/06/09 SA-1111 support (Christopher Hoover)
28  * 2002/06/01 remember frame when HC won't see EDs any more; use that info
29  *      to fix urb unlink races caused by interrupt latency assumptions;
30  *      minor ED field and function naming updates
31  * 2002/01/18 package as a patch for 2.5.3; this should match the
32  *      2.4.17 kernel modulo some bugs being fixed.
33  *
34  * 2001/10/18 merge pmac cleanup (Benjamin Herrenschmidt) and bugfixes
35  *      from post-2.4.5 patches.
36  * 2001/09/20 URB_ZERO_PACKET support; hcca_dma portability, OPTi warning
37  * 2001/09/07 match PCI PM changes, errnos from Linus' tree
38  * 2001/05/05 fork 2.4.5 version into "hcd" framework, cleanup, simplify;
39  *      pbook pci quirks gone (please fix pbook pci sw!) (db)
40  *
41  * 2001/04/08 Identify version on module load (gb)
42  * 2001/03/24 td/ed hashing to remove bus_to_virt (Steve Longerbeam);
43         pci_map_single (db)
44  * 2001/03/21 td and dev/ed allocation uses new pci_pool API (db)
45  * 2001/03/07 hcca allocation uses pci_alloc_consistent (Steve Longerbeam)
46  *
47  * 2000/09/26 fixed races in removing the private portion of the urb
48  * 2000/09/07 disable bulk and control lists when unlinking the last
49  *      endpoint descriptor in order to avoid unrecoverable errors on
50  *      the Lucent chips. (rwc@sgi)
51  * 2000/08/29 use bandwidth claiming hooks (thanks Randy!), fix some
52  *      urb unlink probs, indentation fixes
53  * 2000/08/11 various oops fixes mostly affecting iso and cleanup from
54  *      device unplugs.
55  * 2000/06/28 use PCI hotplug framework, for better power management
56  *      and for Cardbus support (David Brownell)
57  * 2000/earlier:  fixes for NEC/Lucent chips; suspend/resume handling
58  *      when the controller loses power; handle UE; cleanup; ...
59  *
60  * v5.2 1999/12/07 URB 3rd preview, 
61  * v5.1 1999/11/30 URB 2nd preview, cpia, (usb-scsi)
62  * v5.0 1999/11/22 URB Technical preview, Paul Mackerras powerbook susp/resume 
63  *      i386: HUB, Keyboard, Mouse, Printer 
64  *
65  * v4.3 1999/10/27 multiple HCs, bulk_request
66  * v4.2 1999/09/05 ISO API alpha, new dev alloc, neg Error-codes
67  * v4.1 1999/08/27 Randy Dunlap's - ISO API first impl.
68  * v4.0 1999/08/18 
69  * v3.0 1999/06/25 
70  * v2.1 1999/05/09  code clean up
71  * v2.0 1999/05/04 
72  * v1.0 1999/04/27 initial release
73  *
74  * This file is licenced under the GPL.
75  */
76  
77 #include <linux/config.h>
78
79 #ifdef CONFIG_USB_DEBUG
80 #       define DEBUG
81 #else
82 #       undef DEBUG
83 #endif
84
85 #include <linux/module.h>
86 #include <linux/moduleparam.h>
87 #include <linux/pci.h>
88 #include <linux/kernel.h>
89 #include <linux/delay.h>
90 #include <linux/ioport.h>
91 #include <linux/sched.h>
92 #include <linux/slab.h>
93 #include <linux/smp_lock.h>
94 #include <linux/errno.h>
95 #include <linux/init.h>
96 #include <linux/timer.h>
97 #include <linux/list.h>
98 #include <linux/interrupt.h>  /* for in_interrupt () */
99 #include <linux/usb.h>
100 #include "../core/hcd.h"
101 #include <linux/dma-mapping.h> 
102 #include <linux/dmapool.h>    /* needed by ohci-mem.c when no PCI */
103
104 #include <asm/io.h>
105 #include <asm/irq.h>
106 #include <asm/system.h>
107 #include <asm/unaligned.h>
108 #include <asm/byteorder.h>
109
110
111 #define DRIVER_VERSION "2004 Feb 02"
112 #define DRIVER_AUTHOR "Roman Weissgaerber, David Brownell"
113 #define DRIVER_DESC "USB 1.1 'Open' Host Controller (OHCI) Driver"
114
115 /*-------------------------------------------------------------------------*/
116
117 // #define OHCI_VERBOSE_DEBUG   /* not always helpful */
118
119 /* For initializing controller (mask in an HCFS mode too) */
120 #define OHCI_CONTROL_INIT       OHCI_CTRL_CBSR
121 #define OHCI_INTR_INIT \
122         (OHCI_INTR_MIE | OHCI_INTR_UE | OHCI_INTR_RD | OHCI_INTR_WDH)
123
124 /*-------------------------------------------------------------------------*/
125
126 static const char       hcd_name [] = "ohci_hcd";
127
128 #include "ohci.h"
129
130 #include "ohci-hub.c"
131 #include "ohci-dbg.c"
132 #include "ohci-mem.c"
133 #include "ohci-q.c"
134
135
136 /* Some boards don't support per-port power switching */
137 static int power_switching = 0;
138 module_param (power_switching, bool, 0);
139 MODULE_PARM_DESC (power_switching, "true (not default) to switch port power");
140
141 /*-------------------------------------------------------------------------*/
142
143 /*
144  * queue up an urb for anything except the root hub
145  */
146 static int ohci_urb_enqueue (
147         struct usb_hcd  *hcd,
148         struct urb      *urb,
149         int             mem_flags
150 ) {
151         struct ohci_hcd *ohci = hcd_to_ohci (hcd);
152         struct ed       *ed;
153         urb_priv_t      *urb_priv;
154         unsigned int    pipe = urb->pipe;
155         int             i, size = 0;
156         unsigned long   flags;
157         int             retval = 0;
158         
159 #ifdef OHCI_VERBOSE_DEBUG
160         urb_print (urb, "SUB", usb_pipein (pipe));
161 #endif
162         
163         /* every endpoint has a ed, locate and maybe (re)initialize it */
164         if (! (ed = ed_get (ohci, urb->dev, pipe, urb->interval)))
165                 return -ENOMEM;
166
167         /* for the private part of the URB we need the number of TDs (size) */
168         switch (ed->type) {
169                 case PIPE_CONTROL:
170                         /* td_submit_urb() doesn't yet handle these */
171                         if (urb->transfer_buffer_length > 4096)
172                                 return -EMSGSIZE;
173
174                         /* 1 TD for setup, 1 for ACK, plus ... */
175                         size = 2;
176                         /* FALLTHROUGH */
177                 // case PIPE_INTERRUPT:
178                 // case PIPE_BULK:
179                 default:
180                         /* one TD for every 4096 Bytes (can be upto 8K) */
181                         size += urb->transfer_buffer_length / 4096;
182                         /* ... and for any remaining bytes ... */
183                         if ((urb->transfer_buffer_length % 4096) != 0)
184                                 size++;
185                         /* ... and maybe a zero length packet to wrap it up */
186                         if (size == 0)
187                                 size++;
188                         else if ((urb->transfer_flags & URB_ZERO_PACKET) != 0
189                                 && (urb->transfer_buffer_length
190                                         % usb_maxpacket (urb->dev, pipe,
191                                                 usb_pipeout (pipe))) == 0)
192                                 size++;
193                         break;
194                 case PIPE_ISOCHRONOUS: /* number of packets from URB */
195                         size = urb->number_of_packets;
196                         break;
197         }
198
199         /* allocate the private part of the URB */
200         urb_priv = kmalloc (sizeof (urb_priv_t) + size * sizeof (struct td *),
201                         mem_flags);
202         if (!urb_priv)
203                 return -ENOMEM;
204         memset (urb_priv, 0, sizeof (urb_priv_t) + size * sizeof (struct td *));
205         INIT_LIST_HEAD (&urb_priv->pending);
206         urb_priv->length = size;
207         urb_priv->ed = ed;      
208
209         /* allocate the TDs (deferring hash chain updates) */
210         for (i = 0; i < size; i++) {
211                 urb_priv->td [i] = td_alloc (ohci, mem_flags);
212                 if (!urb_priv->td [i]) {
213                         urb_priv->length = i;
214                         urb_free_priv (ohci, urb_priv);
215                         return -ENOMEM;
216                 }
217         }       
218
219         spin_lock_irqsave (&ohci->lock, flags);
220
221         /* don't submit to a dead HC */
222         if (!HCD_IS_RUNNING(ohci->hcd.state)) {
223                 retval = -ENODEV;
224                 goto fail;
225         }
226
227         /* in case of unlink-during-submit */
228         spin_lock (&urb->lock);
229         if (urb->status != -EINPROGRESS) {
230                 spin_unlock (&urb->lock);
231                 urb->hcpriv = urb_priv;
232                 finish_urb (ohci, urb, NULL);
233                 retval = 0;
234                 goto fail;
235         }
236
237         /* schedule the ed if needed */
238         if (ed->state == ED_IDLE) {
239                 retval = ed_schedule (ohci, ed);
240                 if (retval < 0)
241                         goto fail0;
242                 if (ed->type == PIPE_ISOCHRONOUS) {
243                         u16     frame = OHCI_FRAME_NO(ohci->hcca);
244
245                         /* delay a few frames before the first TD */
246                         frame += max_t (u16, 8, ed->interval);
247                         frame &= ~(ed->interval - 1);
248                         frame |= ed->branch;
249                         urb->start_frame = frame;
250
251                         /* yes, only URB_ISO_ASAP is supported, and
252                          * urb->start_frame is never used as input.
253                          */
254                 }
255         } else if (ed->type == PIPE_ISOCHRONOUS)
256                 urb->start_frame = ed->last_iso + ed->interval;
257
258         /* fill the TDs and link them to the ed; and
259          * enable that part of the schedule, if needed
260          * and update count of queued periodic urbs
261          */
262         urb->hcpriv = urb_priv;
263         td_submit_urb (ohci, urb);
264
265 fail0:
266         spin_unlock (&urb->lock);
267 fail:
268         if (retval)
269                 urb_free_priv (ohci, urb_priv);
270         spin_unlock_irqrestore (&ohci->lock, flags);
271         return retval;
272 }
273
274 /*
275  * decouple the URB from the HC queues (TDs, urb_priv); it's
276  * already marked using urb->status.  reporting is always done
277  * asynchronously, and we might be dealing with an urb that's
278  * partially transferred, or an ED with other urbs being unlinked.
279  */
280 static int ohci_urb_dequeue (struct usb_hcd *hcd, struct urb *urb)
281 {
282         struct ohci_hcd         *ohci = hcd_to_ohci (hcd);
283         unsigned long           flags;
284         
285 #ifdef OHCI_VERBOSE_DEBUG
286         urb_print (urb, "UNLINK", 1);
287 #endif            
288
289         spin_lock_irqsave (&ohci->lock, flags);
290         if (HCD_IS_RUNNING(ohci->hcd.state)) {
291                 urb_priv_t  *urb_priv;
292
293                 /* Unless an IRQ completed the unlink while it was being
294                  * handed to us, flag it for unlink and giveback, and force
295                  * some upcoming INTR_SF to call finish_unlinks()
296                  */
297                 urb_priv = urb->hcpriv;
298                 if (urb_priv) {
299                         if (urb_priv->ed->state == ED_OPER)
300                                 start_ed_unlink (ohci, urb_priv->ed);
301                 }
302         } else {
303                 /*
304                  * with HC dead, we won't respect hc queue pointers
305                  * any more ... just clean up every urb's memory.
306                  */
307                 if (urb->hcpriv)
308                         finish_urb (ohci, urb, NULL);
309         }
310         spin_unlock_irqrestore (&ohci->lock, flags);
311         return 0;
312 }
313
314 /*-------------------------------------------------------------------------*/
315
316 /* frees config/altsetting state for endpoints,
317  * including ED memory, dummy TD, and bulk/intr data toggle
318  */
319
320 static void
321 ohci_endpoint_disable (struct usb_hcd *hcd, struct hcd_dev *dev, int ep)
322 {
323         struct ohci_hcd         *ohci = hcd_to_ohci (hcd);
324         int                     epnum = ep & USB_ENDPOINT_NUMBER_MASK;
325         unsigned long           flags;
326         struct ed               *ed;
327         unsigned                limit = 1000;
328
329         /* ASSERT:  any requests/urbs are being unlinked */
330         /* ASSERT:  nobody can be submitting urbs for this any more */
331
332         epnum <<= 1;
333         if (epnum != 0 && !(ep & USB_DIR_IN))
334                 epnum |= 1;
335
336 rescan:
337         spin_lock_irqsave (&ohci->lock, flags);
338         ed = dev->ep [epnum];
339         if (!ed)
340                 goto done;
341
342         if (!HCD_IS_RUNNING (ohci->hcd.state)) {
343 sanitize:
344                 ed->state = ED_IDLE;
345                 finish_unlinks (ohci, 0, NULL);
346         }
347
348         switch (ed->state) {
349         case ED_UNLINK:         /* wait for hw to finish? */
350                 /* major IRQ delivery trouble loses INTR_SF too... */
351                 if (limit-- == 0) {
352                         ohci_warn (ohci, "IRQ INTR_SF lossage\n");
353                         goto sanitize;
354                 }
355                 spin_unlock_irqrestore (&ohci->lock, flags);
356                 set_current_state (TASK_UNINTERRUPTIBLE);
357                 schedule_timeout (1);
358                 goto rescan;
359         case ED_IDLE:           /* fully unlinked */
360                 if (list_empty (&ed->td_list)) {
361                         td_free (ohci, ed->dummy);
362                         ed_free (ohci, ed);
363                         break;
364                 }
365                 /* else FALL THROUGH */
366         default:
367                 /* caller was supposed to have unlinked any requests;
368                  * that's not our job.  can't recover; must leak ed.
369                  */
370                 ohci_err (ohci, "leak ed %p (#%d) state %d%s\n",
371                         ed, epnum, ed->state,
372                         list_empty (&ed->td_list) ? "" : " (has tds)");
373                 td_free (ohci, ed->dummy);
374                 break;
375         }
376         dev->ep [epnum] = NULL;
377 done:
378         spin_unlock_irqrestore (&ohci->lock, flags);
379         return;
380 }
381
382 static int ohci_get_frame (struct usb_hcd *hcd)
383 {
384         struct ohci_hcd         *ohci = hcd_to_ohci (hcd);
385
386         return OHCI_FRAME_NO(ohci->hcca);
387 }
388
389 /*-------------------------------------------------------------------------*
390  * HC functions
391  *-------------------------------------------------------------------------*/
392
393 /* reset the HC and BUS */
394
395 static int hc_reset (struct ohci_hcd *ohci)
396 {
397         u32 temp;
398
399         /* boot firmware should have set this up (5.1.1.3.1) */
400         if (!ohci->fminterval) {
401                 temp = ohci_readl (&ohci->regs->fminterval);
402                 if (temp & 0x3fff0000)
403                         ohci->fminterval = temp;
404                 else
405                         ohci->fminterval = DEFAULT_FMINTERVAL;
406                 /* also: power/overcurrent flags in roothub.a */
407         }
408
409         /* SMM owns the HC?  not for long!
410          * On PA-RISC, PDC can leave IR set incorrectly; ignore it there.
411          */
412 #ifndef __hppa__
413         if (ohci_readl (&ohci->regs->control) & OHCI_CTRL_IR) {
414                 ohci_dbg (ohci, "USB HC TakeOver from BIOS/SMM\n");
415
416                 /* this timeout is arbitrary.  we make it long, so systems
417                  * depending on usb keyboards may be usable even if the
418                  * BIOS/SMM code seems pretty broken.
419                  */
420                 temp = 500;     /* arbitrary: five seconds */
421
422                 writel (OHCI_INTR_OC, &ohci->regs->intrenable);
423                 writel (OHCI_OCR, &ohci->regs->cmdstatus);
424                 while (ohci_readl (&ohci->regs->control) & OHCI_CTRL_IR) {
425                         msleep (10);
426                         if (--temp == 0) {
427                                 ohci_err (ohci, "USB HC TakeOver failed!\n");
428                                 return -1;
429                         }
430                 }
431         }
432 #endif
433
434         /* Disable HC interrupts */
435         writel (OHCI_INTR_MIE, &ohci->regs->intrdisable);
436
437         ohci_dbg (ohci, "reset, control = 0x%x\n",
438                   ohci_readl (&ohci->regs->control));
439
440         /* Reset USB (needed by some controllers); RemoteWakeupConnected
441          * saved if boot firmware (BIOS/SMM/...) told us it's connected
442          * (for OHCI integrated on mainboard, it normally is)
443          */
444         ohci->hc_control = ohci_readl (&ohci->regs->control);
445         ohci->hc_control &= OHCI_CTRL_RWC;      /* hcfs 0 = RESET */
446         if (ohci->hc_control)
447                 ohci->hcd.can_wakeup = 1;
448         writel (ohci->hc_control, &ohci->regs->control);
449         if (power_switching) {
450                 unsigned ports = roothub_a (ohci) & RH_A_NDP; 
451
452                 /* power down each port */
453                 for (temp = 0; temp < ports; temp++)
454                         writel (RH_PS_LSDA,
455                                 &ohci->regs->roothub.portstatus [temp]);
456         }
457         // flush those pci writes
458         (void) ohci_readl (&ohci->regs->control);
459         msleep (50);
460
461         /* HC Reset requires max 10 us delay */
462         writel (OHCI_HCR,  &ohci->regs->cmdstatus);
463         temp = 30;      /* ... allow extra time */
464         while ((ohci_readl (&ohci->regs->cmdstatus) & OHCI_HCR) != 0) {
465                 if (--temp == 0) {
466                         ohci_err (ohci, "USB HC reset timed out!\n");
467                         return -1;
468                 }
469                 udelay (1);
470         }
471
472         /* now we're in the SUSPEND state ... must go OPERATIONAL
473          * within 2msec else HC enters RESUME
474          *
475          * ... but some hardware won't init fmInterval "by the book"
476          * (SiS, OPTi ...), so reset again instead.  SiS doesn't need
477          * this if we write fmInterval after we're OPERATIONAL.
478          */
479         writel (ohci->hc_control, &ohci->regs->control);
480         // flush those pci writes
481         (void) ohci_readl (&ohci->regs->control);
482
483         return 0;
484 }
485
486 /*-------------------------------------------------------------------------*/
487
488 /* Start an OHCI controller, set the BUS operational
489  * enable interrupts 
490  * connect the virtual root hub
491  */
492 static int hc_start (struct ohci_hcd *ohci)
493 {
494         u32                     mask, tmp;
495         struct usb_device       *udev;
496         struct usb_bus          *bus;
497
498         disable (ohci);
499
500         /* Tell the controller where the control and bulk lists are
501          * The lists are empty now. */
502         writel (0, &ohci->regs->ed_controlhead);
503         writel (0, &ohci->regs->ed_bulkhead);
504
505         /* a reset clears this */
506         writel ((u32) ohci->hcca_dma, &ohci->regs->hcca);
507
508         periodic_reinit (ohci);
509
510         /* some OHCI implementations are finicky about how they init.
511          * bogus values here mean not even enumeration could work.
512          */
513         if ((ohci_readl (&ohci->regs->fminterval) & 0x3fff0000) == 0
514                         || !ohci_readl (&ohci->regs->periodicstart)) {
515                 ohci_err (ohci, "init err\n");
516                 return -EOVERFLOW;
517         }
518
519         /* start controller operations */
520         ohci->hc_control &= OHCI_CTRL_RWC;
521         ohci->hc_control |= OHCI_CONTROL_INIT | OHCI_USB_OPER;
522         writel (ohci->hc_control, &ohci->regs->control);
523         ohci->hcd.state = USB_STATE_RUNNING;
524
525         /* wake on ConnectStatusChange, matching external hubs */
526         writel (RH_HS_DRWE, &ohci->regs->roothub.status);
527
528         /* Choose the interrupts we care about now, others later on demand */
529         mask = OHCI_INTR_INIT;
530         writel (mask, &ohci->regs->intrstatus);
531         writel (mask, &ohci->regs->intrenable);
532
533         /* handle root hub init quirks ... */
534         tmp = roothub_a (ohci);
535         tmp &= ~(RH_A_PSM | RH_A_OCPM);
536         if (ohci->flags & OHCI_QUIRK_SUPERIO) {
537                 /* NSC 87560 and maybe others */
538                 tmp |= RH_A_NOCP;
539                 tmp &= ~(RH_A_POTPGT | RH_A_NPS);
540         } else if (power_switching) {
541                 /* act like most external hubs:  use per-port power
542                  * switching and overcurrent reporting.
543                  */
544                 tmp &= ~(RH_A_NPS | RH_A_NOCP);
545                 tmp |= RH_A_PSM | RH_A_OCPM;
546         } else {
547                 /* hub power always on; required for AMD-756 and some
548                  * Mac platforms.  ganged overcurrent reporting, if any.
549                  */
550                 tmp |= RH_A_NPS;
551         }
552         writel (tmp, &ohci->regs->roothub.a);
553         writel (RH_HS_LPSC, &ohci->regs->roothub.status);
554         writel (power_switching ? RH_B_PPCM : 0, &ohci->regs->roothub.b);
555         // flush those pci writes
556         (void) ohci_readl (&ohci->regs->control);
557
558         // POTPGT delay is bits 24-31, in 2 ms units.
559         mdelay ((roothub_a (ohci) >> 23) & 0x1fe);
560         bus = hcd_to_bus (&ohci->hcd);
561
562         if (bus->root_hub) {
563                 ohci->hcd.state = USB_STATE_RUNNING;
564                 return 0;
565         }
566  
567         /* connect the virtual root hub */
568         udev = usb_alloc_dev (NULL, bus, 0);
569         ohci->hcd.state = USB_STATE_RUNNING;
570         if (!udev) {
571                 disable (ohci);
572                 ohci->hc_control &= ~OHCI_CTRL_HCFS;
573                 writel (ohci->hc_control, &ohci->regs->control);
574                 return -ENOMEM;
575         }
576
577         udev->speed = USB_SPEED_FULL;
578         if (hcd_register_root (udev, &ohci->hcd) != 0) {
579                 usb_put_dev (udev);
580                 disable (ohci);
581                 ohci->hc_control &= ~OHCI_CTRL_HCFS;
582                 writel (ohci->hc_control, &ohci->regs->control);
583                 return -ENODEV;
584         }
585
586         return 0;
587 }
588
589 /*-------------------------------------------------------------------------*/
590
591 /* an interrupt happens */
592
593 static irqreturn_t ohci_irq (struct usb_hcd *hcd, struct pt_regs *ptregs)
594 {
595         struct ohci_hcd         *ohci = hcd_to_ohci (hcd);
596         struct ohci_regs        *regs = ohci->regs;
597         int                     ints; 
598
599         /* we can eliminate a (slow) ohci_readl()
600            if _only_ WDH caused this irq */
601         if ((ohci->hcca->done_head != 0)
602                         && ! (le32_to_cpup (&ohci->hcca->done_head) & 0x01)) {
603                 ints =  OHCI_INTR_WDH;
604
605         /* cardbus/... hardware gone before remove() */
606         } else if ((ints = ohci_readl (&regs->intrstatus)) == ~(u32)0) {
607                 disable (ohci);
608                 ohci_dbg (ohci, "device removed!\n");
609                 return IRQ_HANDLED;
610
611         /* interrupt for some other device? */
612         } else if ((ints &= ohci_readl (&regs->intrenable)) == 0) {
613                 return IRQ_NONE;
614         } 
615
616         if (ints & OHCI_INTR_UE) {
617                 disable (ohci);
618                 ohci_err (ohci, "OHCI Unrecoverable Error, disabled\n");
619                 // e.g. due to PCI Master/Target Abort
620
621                 ohci_dump (ohci, 1);
622                 hc_reset (ohci);
623         }
624
625         if (ints & OHCI_INTR_RD) {
626                 ohci_vdbg (ohci, "resume detect\n");
627                 schedule_work(&ohci->rh_resume);
628         }
629
630         if (ints & OHCI_INTR_WDH) {
631                 if (HCD_IS_RUNNING(hcd->state))
632                         writel (OHCI_INTR_WDH, &regs->intrdisable);     
633                 spin_lock (&ohci->lock);
634                 dl_done_list (ohci, ptregs);
635                 spin_unlock (&ohci->lock);
636                 if (HCD_IS_RUNNING(hcd->state))
637                         writel (OHCI_INTR_WDH, &regs->intrenable); 
638         }
639   
640         /* could track INTR_SO to reduce available PCI/... bandwidth */
641
642         /* handle any pending URB/ED unlinks, leaving INTR_SF enabled
643          * when there's still unlinking to be done (next frame).
644          */
645         spin_lock (&ohci->lock);
646         if (ohci->ed_rm_list)
647                 finish_unlinks (ohci, OHCI_FRAME_NO(ohci->hcca),
648                                 ptregs);
649         if ((ints & OHCI_INTR_SF) != 0 && !ohci->ed_rm_list
650                         && HCD_IS_RUNNING(ohci->hcd.state))
651                 writel (OHCI_INTR_SF, &regs->intrdisable);      
652         spin_unlock (&ohci->lock);
653
654         if (HCD_IS_RUNNING(ohci->hcd.state)) {
655                 writel (ints, &regs->intrstatus);
656                 writel (OHCI_INTR_MIE, &regs->intrenable);      
657                 // flush those pci writes
658                 (void) ohci_readl (&ohci->regs->control);
659         }
660
661         return IRQ_HANDLED;
662 }
663
664 /*-------------------------------------------------------------------------*/
665
666 static void ohci_stop (struct usb_hcd *hcd)
667 {       
668         struct ohci_hcd         *ohci = hcd_to_ohci (hcd);
669
670         ohci_dbg (ohci, "stop %s controller (state 0x%02x)\n",
671                 hcfs2string (ohci->hc_control & OHCI_CTRL_HCFS),
672                 ohci->hcd.state);
673         ohci_dump (ohci, 1);
674
675         flush_scheduled_work();
676         if (HCD_IS_RUNNING(ohci->hcd.state))
677                 hc_reset (ohci);
678         else
679                 writel (OHCI_INTR_MIE, &ohci->regs->intrdisable);
680         
681         remove_debug_files (ohci);
682         ohci_mem_cleanup (ohci);
683         if (ohci->hcca) {
684                 dma_free_coherent (ohci->hcd.self.controller, 
685                                 sizeof *ohci->hcca, 
686                                 ohci->hcca, ohci->hcca_dma);
687                 ohci->hcca = NULL;
688                 ohci->hcca_dma = 0;
689         }
690 }
691
692 /*-------------------------------------------------------------------------*/
693
694 /* must not be called from interrupt context */
695
696 #ifdef CONFIG_PM
697
698 static void mark_children_gone (struct usb_device *dev)
699 {
700         unsigned i;
701
702         for (i = 0; i < dev->maxchild; i++) {
703                 if (dev->children [i] == 0)
704                         continue;
705                 dev->children [i]->state = USB_STATE_NOTATTACHED;
706                 mark_children_gone (dev->children [i]);
707         }
708 }
709
710 static int hc_restart (struct ohci_hcd *ohci)
711 {
712         int temp;
713         int i;
714         struct urb_priv *priv;
715
716         /* mark any devices gone, so they do nothing till khubd disconnects.
717          * recycle any "live" eds/tds (and urbs) right away.
718          * later, khubd disconnect processing will recycle the other state,
719          * (either as disconnect/reconnect, or maybe someday as a reset).
720          */ 
721         spin_lock_irq(&ohci->lock);
722         disable (ohci);
723         mark_children_gone (ohci->hcd.self.root_hub);
724         if (!list_empty (&ohci->pending))
725                 ohci_dbg(ohci, "abort schedule...\n");
726         list_for_each_entry (priv, &ohci->pending, pending) {
727                 struct urb      *urb = priv->td[0]->urb;
728                 struct ed       *ed = priv->ed;
729
730                 switch (ed->state) {
731                 case ED_OPER:
732                         ed->state = ED_UNLINK;
733                         ed->hwINFO |= ED_DEQUEUE;
734                         ed_deschedule (ohci, ed);
735
736                         ed->ed_next = ohci->ed_rm_list;
737                         ed->ed_prev = NULL;
738                         ohci->ed_rm_list = ed;
739                         /* FALLTHROUGH */
740                 case ED_UNLINK:
741                         break;
742                 default:
743                         ohci_dbg(ohci, "bogus ed %p state %d\n",
744                                         ed, ed->state);
745                 }
746
747                 spin_lock (&urb->lock);
748                 urb->status = -ESHUTDOWN;
749                 spin_unlock (&urb->lock);
750         }
751         finish_unlinks (ohci, 0, NULL);
752         spin_unlock_irq(&ohci->lock);
753
754         /* paranoia, in case that didn't work: */
755
756         /* empty the interrupt branches */
757         for (i = 0; i < NUM_INTS; i++) ohci->load [i] = 0;
758         for (i = 0; i < NUM_INTS; i++) ohci->hcca->int_table [i] = 0;
759         
760         /* no EDs to remove */
761         ohci->ed_rm_list = NULL;
762
763         /* empty control and bulk lists */       
764         ohci->ed_controltail = NULL;
765         ohci->ed_bulktail    = NULL;
766
767         if ((temp = hc_reset (ohci)) < 0 || (temp = hc_start (ohci)) < 0) {
768                 ohci_err (ohci, "can't restart, %d\n", temp);
769                 return temp;
770         } else {
771                 /* here we "know" root ports should always stay powered,
772                  * and that if we try to turn them back on the root hub
773                  * will respond to CSC processing.
774                  */
775                 i = roothub_a (ohci) & RH_A_NDP;
776                 while (i--)
777                         writel (RH_PS_PSS,
778                                 &ohci->regs->roothub.portstatus [temp]);
779                 ohci->hcd.self.root_hub->dev.power.power_state = 0;
780                 ohci->hcd.state = USB_STATE_RUNNING;
781                 ohci_dbg (ohci, "restart complete\n");
782                 ohci_dump (ohci, 1);
783         }
784         return 0;
785 }
786 #endif
787
788 /*-------------------------------------------------------------------------*/
789
790 #define DRIVER_INFO DRIVER_VERSION " " DRIVER_DESC
791
792 MODULE_AUTHOR (DRIVER_AUTHOR);
793 MODULE_DESCRIPTION (DRIVER_INFO);
794 MODULE_LICENSE ("GPL");
795
796 #ifdef CONFIG_PCI
797 #include "ohci-pci.c"
798 #endif
799
800 #ifdef CONFIG_SA1111
801 #include "ohci-sa1111.c"
802 #endif
803
804 #ifdef CONFIG_ARCH_OMAP
805 #include "ohci-omap.c"
806 #endif
807
808 #ifdef CONFIG_ARCH_LH7A404
809 #include "ohci-lh7a404.c"
810 #endif
811
812 #if !(defined(CONFIG_PCI) \
813       || defined(CONFIG_SA1111) \
814       || defined(CONFIG_ARCH_OMAP) \
815       || defined (CONFIG_ARCH_LH7A404) \
816         )
817 #error "missing bus glue for ohci-hcd"
818 #endif