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