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