v2.4.9.9 -> v2.4.9.10
[opensuse:kernel.git] / drivers / usb / usb-ohci.c
1 /*
2  * URB OHCI HCD (Host Controller Driver) for USB.
3  *
4  * (C) Copyright 1999 Roman Weissgaerber <weissg@vienna.at>
5  * (C) Copyright 2000-2001 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  * History:
14  * 
15  * 2001/07/17 power management and pmac cleanup (Benjamin Herrenschmidt)
16  * 2001/03/24 td/ed hashing to remove bus_to_virt (Steve Longerbeam);
17         pci_map_single (db)
18  * 2001/03/21 td and dev/ed allocation uses new pci_pool API (db)
19  * 2001/03/07 hcca allocation uses pci_alloc_consistent (Steve Longerbeam)
20  *
21  * 2000/09/26 fixed races in removing the private portion of the urb
22  * 2000/09/07 disable bulk and control lists when unlinking the last
23  *      endpoint descriptor in order to avoid unrecoverable errors on
24  *      the Lucent chips. (rwc@sgi)
25  * 2000/08/29 use bandwidth claiming hooks (thanks Randy!), fix some
26  *      urb unlink probs, indentation fixes
27  * 2000/08/11 various oops fixes mostly affecting iso and cleanup from
28  *      device unplugs.
29  * 2000/06/28 use PCI hotplug framework, for better power management
30  *      and for Cardbus support (David Brownell)
31  * 2000/earlier:  fixes for NEC/Lucent chips; suspend/resume handling
32  *      when the controller loses power; handle UE; cleanup; ...
33  *
34  * v5.2 1999/12/07 URB 3rd preview, 
35  * v5.1 1999/11/30 URB 2nd preview, cpia, (usb-scsi)
36  * v5.0 1999/11/22 URB Technical preview, Paul Mackerras powerbook susp/resume 
37  *      i386: HUB, Keyboard, Mouse, Printer 
38  *
39  * v4.3 1999/10/27 multiple HCs, bulk_request
40  * v4.2 1999/09/05 ISO API alpha, new dev alloc, neg Error-codes
41  * v4.1 1999/08/27 Randy Dunlap's - ISO API first impl.
42  * v4.0 1999/08/18 
43  * v3.0 1999/06/25 
44  * v2.1 1999/05/09  code clean up
45  * v2.0 1999/05/04 
46  * v1.0 1999/04/27 initial release
47  */
48  
49 #include <linux/config.h>
50 #include <linux/module.h>
51 #include <linux/pci.h>
52 #include <linux/kernel.h>
53 #include <linux/delay.h>
54 #include <linux/ioport.h>
55 #include <linux/sched.h>
56 #include <linux/slab.h>
57 #include <linux/smp_lock.h>
58 #include <linux/errno.h>
59 #include <linux/init.h>
60 #include <linux/timer.h>
61 #include <linux/list.h>
62 #include <linux/interrupt.h>  /* for in_interrupt() */
63 #undef DEBUG
64 #include <linux/usb.h>
65
66 #include <asm/io.h>
67 #include <asm/irq.h>
68 #include <asm/system.h>
69 #include <asm/unaligned.h>
70
71 #define OHCI_USE_NPS            // force NoPowerSwitching mode
72 // #define OHCI_VERBOSE_DEBUG   /* not always helpful */
73
74 #include "usb-ohci.h"
75
76
77 #ifdef CONFIG_PMAC_PBOOK
78 #include <asm/feature.h>
79 #include <asm/pci-bridge.h>
80 #ifndef CONFIG_PM
81 #define CONFIG_PM
82 #endif
83 #endif
84
85
86 /*
87  * Version Information
88  */
89 #define DRIVER_VERSION "v5.3"
90 #define DRIVER_AUTHOR "Roman Weissgaerber <weissg@vienna.at>, David Brownell"
91 #define DRIVER_DESC "USB OHCI Host Controller Driver"
92
93 /* For initializing controller (mask in an HCFS mode too) */
94 #define OHCI_CONTROL_INIT \
95         (OHCI_CTRL_CBSR & 0x3) | OHCI_CTRL_IE | OHCI_CTRL_PLE
96
97 #define OHCI_UNLINK_TIMEOUT     (HZ / 10)
98
99 static LIST_HEAD (ohci_hcd_list);
100 static spinlock_t usb_ed_lock = SPIN_LOCK_UNLOCKED;
101
102
103 /*-------------------------------------------------------------------------*/
104
105 /* AMD-756 (D2 rev) reports corrupt register contents in some cases.
106  * The erratum (#4) description is incorrect.  AMD's workaround waits
107  * till some bits (mostly reserved) are clear; ok for all revs.
108  */
109 #define read_roothub(hc, register, mask) ({ \
110         u32 temp = readl (&hc->regs->roothub.register); \
111         if (hc->flags & OHCI_QUIRK_AMD756) \
112                 while (temp & mask) \
113                         temp = readl (&hc->regs->roothub.register); \
114         temp; })
115
116 static u32 roothub_a (struct ohci *hc)
117         { return read_roothub (hc, a, 0xfc0fe000); }
118 static inline u32 roothub_b (struct ohci *hc)
119         { return readl (&hc->regs->roothub.b); }
120 static inline u32 roothub_status (struct ohci *hc)
121         { return readl (&hc->regs->roothub.status); }
122 static u32 roothub_portstatus (struct ohci *hc, int i)
123         { return read_roothub (hc, portstatus [i], 0xffe0fce0); }
124
125
126 /*-------------------------------------------------------------------------*
127  * URB support functions 
128  *-------------------------------------------------------------------------*/ 
129  
130 /* free HCD-private data associated with this URB */
131
132 static void urb_free_priv (struct ohci *hc, urb_priv_t * urb_priv)
133 {
134         int             i;
135         int             last = urb_priv->length - 1;
136         int             len;
137         int             dir;
138         struct td       *td;
139
140         if (last >= 0) {
141
142                 /* ISOC, BULK, INTR data buffer starts at td 0 
143                  * CTRL setup starts at td 0 */
144                 td = urb_priv->td [0];
145
146                 len = td->urb->transfer_buffer_length,
147                 dir = usb_pipeout (td->urb->pipe)
148                                         ? PCI_DMA_TODEVICE
149                                         : PCI_DMA_FROMDEVICE;
150
151                 /* unmap CTRL URB setup */
152                 if (usb_pipecontrol (td->urb->pipe)) {
153                         pci_unmap_single (hc->ohci_dev, 
154                                         td->data_dma, 8, PCI_DMA_TODEVICE);
155                         
156                         /* CTRL data buffer starts at td 1 if len > 0 */
157                         if (len && last > 0)
158                                 td = urb_priv->td [1];          
159                 } 
160
161                 /* unmap data buffer */
162                 if (len && td->data_dma)
163                         pci_unmap_single (hc->ohci_dev, td->data_dma, len, dir);
164                 
165                 for (i = 0; i <= last; i++) {
166                         td = urb_priv->td [i];
167                         if (td)
168                                 td_free (hc, td);
169                 }
170         }
171
172         kfree (urb_priv);
173 }
174  
175 static void urb_rm_priv_locked (urb_t * urb) 
176 {
177         urb_priv_t * urb_priv = urb->hcpriv;
178         
179         if (urb_priv) {
180                 urb->hcpriv = NULL;
181
182 #ifdef  DO_TIMEOUTS
183                 if (urb->timeout) {
184                         list_del (&urb->urb_list);
185                         urb->timeout -= jiffies;
186                 }
187 #endif
188
189                 /* Release int/iso bandwidth */
190                 if (urb->bandwidth) {
191                         switch (usb_pipetype(urb->pipe)) {
192                         case PIPE_INTERRUPT:
193                                 usb_release_bandwidth (urb->dev, urb, 0);
194                                 break;
195                         case PIPE_ISOCHRONOUS:
196                                 usb_release_bandwidth (urb->dev, urb, 1);
197                                 break;
198                         default:
199                                 break;
200                         }
201                 }
202
203                 urb_free_priv ((struct ohci *)urb->dev->bus->hcpriv, urb_priv);
204                 usb_dec_dev_use (urb->dev);
205                 urb->dev = NULL;
206         }
207 }
208
209 static void urb_rm_priv (urb_t * urb)
210 {
211         unsigned long flags;
212
213         spin_lock_irqsave (&usb_ed_lock, flags);
214         urb_rm_priv_locked (urb);
215         spin_unlock_irqrestore (&usb_ed_lock, flags);
216 }
217
218 /*-------------------------------------------------------------------------*/
219  
220 #ifdef DEBUG
221 static int sohci_get_current_frame_number (struct usb_device * dev);
222
223 /* debug| print the main components of an URB     
224  * small: 0) header + data packets 1) just header */
225  
226 static void urb_print (urb_t * urb, char * str, int small)
227 {
228         unsigned int pipe= urb->pipe;
229         
230         if (!urb->dev || !urb->dev->bus) {
231                 dbg("%s URB: no dev", str);
232                 return;
233         }
234         
235 #ifndef OHCI_VERBOSE_DEBUG
236         if (urb->status != 0)
237 #endif
238         dbg("%s URB:[%4x] dev:%2d,ep:%2d-%c,type:%s,flags:%4x,len:%d/%d,stat:%d(%x)", 
239                         str,
240                         sohci_get_current_frame_number (urb->dev), 
241                         usb_pipedevice (pipe),
242                         usb_pipeendpoint (pipe), 
243                         usb_pipeout (pipe)? 'O': 'I',
244                         usb_pipetype (pipe) < 2? (usb_pipeint (pipe)? "INTR": "ISOC"):
245                                 (usb_pipecontrol (pipe)? "CTRL": "BULK"),
246                         urb->transfer_flags, 
247                         urb->actual_length, 
248                         urb->transfer_buffer_length,
249                         urb->status, urb->status);
250 #ifdef  OHCI_VERBOSE_DEBUG
251         if (!small) {
252                 int i, len;
253
254                 if (usb_pipecontrol (pipe)) {
255                         printk (KERN_DEBUG __FILE__ ": cmd(8):");
256                         for (i = 0; i < 8 ; i++) 
257                                 printk (" %02x", ((__u8 *) urb->setup_packet) [i]);
258                         printk ("\n");
259                 }
260                 if (urb->transfer_buffer_length > 0 && urb->transfer_buffer) {
261                         printk (KERN_DEBUG __FILE__ ": data(%d/%d):", 
262                                 urb->actual_length, 
263                                 urb->transfer_buffer_length);
264                         len = usb_pipeout (pipe)? 
265                                                 urb->transfer_buffer_length: urb->actual_length;
266                         for (i = 0; i < 16 && i < len; i++) 
267                                 printk (" %02x", ((__u8 *) urb->transfer_buffer) [i]);
268                         printk ("%s stat:%d\n", i < len? "...": "", urb->status);
269                 }
270         } 
271 #endif
272 }
273
274 /* just for debugging; prints non-empty branches of the int ed tree inclusive iso eds*/
275 void ep_print_int_eds (ohci_t * ohci, char * str) {
276         int i, j;
277          __u32 * ed_p;
278         for (i= 0; i < 32; i++) {
279                 j = 5;
280                 ed_p = &(ohci->hcca->int_table [i]);
281                 if (*ed_p == 0)
282                     continue;
283                 printk (KERN_DEBUG __FILE__ ": %s branch int %2d(%2x):", str, i, i);
284                 while (*ed_p != 0 && j--) {
285                         ed_t *ed = dma_to_ed (ohci, le32_to_cpup(ed_p));
286                         printk (" ed: %4x;", ed->hwINFO);
287                         ed_p = &ed->hwNextED;
288                 }
289                 printk ("\n");
290         }
291 }
292
293
294 static void ohci_dump_intr_mask (char *label, __u32 mask)
295 {
296         dbg ("%s: 0x%08x%s%s%s%s%s%s%s%s%s",
297                 label,
298                 mask,
299                 (mask & OHCI_INTR_MIE) ? " MIE" : "",
300                 (mask & OHCI_INTR_OC) ? " OC" : "",
301                 (mask & OHCI_INTR_RHSC) ? " RHSC" : "",
302                 (mask & OHCI_INTR_FNO) ? " FNO" : "",
303                 (mask & OHCI_INTR_UE) ? " UE" : "",
304                 (mask & OHCI_INTR_RD) ? " RD" : "",
305                 (mask & OHCI_INTR_SF) ? " SF" : "",
306                 (mask & OHCI_INTR_WDH) ? " WDH" : "",
307                 (mask & OHCI_INTR_SO) ? " SO" : ""
308                 );
309 }
310
311 static void maybe_print_eds (char *label, __u32 value)
312 {
313         if (value)
314                 dbg ("%s %08x", label, value);
315 }
316
317 static char *hcfs2string (int state)
318 {
319         switch (state) {
320                 case OHCI_USB_RESET:    return "reset";
321                 case OHCI_USB_RESUME:   return "resume";
322                 case OHCI_USB_OPER:     return "operational";
323                 case OHCI_USB_SUSPEND:  return "suspend";
324         }
325         return "?";
326 }
327
328 // dump control and status registers
329 static void ohci_dump_status (ohci_t *controller)
330 {
331         struct ohci_regs        *regs = controller->regs;
332         __u32                   temp;
333
334         temp = readl (&regs->revision) & 0xff;
335         if (temp != 0x10)
336                 dbg ("spec %d.%d", (temp >> 4), (temp & 0x0f));
337
338         temp = readl (&regs->control);
339         dbg ("control: 0x%08x%s%s%s HCFS=%s%s%s%s%s CBSR=%d", temp,
340                 (temp & OHCI_CTRL_RWE) ? " RWE" : "",
341                 (temp & OHCI_CTRL_RWC) ? " RWC" : "",
342                 (temp & OHCI_CTRL_IR) ? " IR" : "",
343                 hcfs2string (temp & OHCI_CTRL_HCFS),
344                 (temp & OHCI_CTRL_BLE) ? " BLE" : "",
345                 (temp & OHCI_CTRL_CLE) ? " CLE" : "",
346                 (temp & OHCI_CTRL_IE) ? " IE" : "",
347                 (temp & OHCI_CTRL_PLE) ? " PLE" : "",
348                 temp & OHCI_CTRL_CBSR
349                 );
350
351         temp = readl (&regs->cmdstatus);
352         dbg ("cmdstatus: 0x%08x SOC=%d%s%s%s%s", temp,
353                 (temp & OHCI_SOC) >> 16,
354                 (temp & OHCI_OCR) ? " OCR" : "",
355                 (temp & OHCI_BLF) ? " BLF" : "",
356                 (temp & OHCI_CLF) ? " CLF" : "",
357                 (temp & OHCI_HCR) ? " HCR" : ""
358                 );
359
360         ohci_dump_intr_mask ("intrstatus", readl (&regs->intrstatus));
361         ohci_dump_intr_mask ("intrenable", readl (&regs->intrenable));
362         // intrdisable always same as intrenable
363         // ohci_dump_intr_mask ("intrdisable", readl (&regs->intrdisable));
364
365         maybe_print_eds ("ed_periodcurrent", readl (&regs->ed_periodcurrent));
366
367         maybe_print_eds ("ed_controlhead", readl (&regs->ed_controlhead));
368         maybe_print_eds ("ed_controlcurrent", readl (&regs->ed_controlcurrent));
369
370         maybe_print_eds ("ed_bulkhead", readl (&regs->ed_bulkhead));
371         maybe_print_eds ("ed_bulkcurrent", readl (&regs->ed_bulkcurrent));
372
373         maybe_print_eds ("donehead", readl (&regs->donehead));
374 }
375
376 static void ohci_dump_roothub (ohci_t *controller, int verbose)
377 {
378         __u32                   temp, ndp, i;
379
380         temp = roothub_a (controller);
381         ndp = (temp & RH_A_NDP);
382
383         if (verbose) {
384                 dbg ("roothub.a: %08x POTPGT=%d%s%s%s%s%s NDP=%d", temp,
385                         ((temp & RH_A_POTPGT) >> 24) & 0xff,
386                         (temp & RH_A_NOCP) ? " NOCP" : "",
387                         (temp & RH_A_OCPM) ? " OCPM" : "",
388                         (temp & RH_A_DT) ? " DT" : "",
389                         (temp & RH_A_NPS) ? " NPS" : "",
390                         (temp & RH_A_PSM) ? " PSM" : "",
391                         ndp
392                         );
393                 temp = roothub_b (controller);
394                 dbg ("roothub.b: %08x PPCM=%04x DR=%04x",
395                         temp,
396                         (temp & RH_B_PPCM) >> 16,
397                         (temp & RH_B_DR)
398                         );
399                 temp = roothub_status (controller);
400                 dbg ("roothub.status: %08x%s%s%s%s%s%s",
401                         temp,
402                         (temp & RH_HS_CRWE) ? " CRWE" : "",
403                         (temp & RH_HS_OCIC) ? " OCIC" : "",
404                         (temp & RH_HS_LPSC) ? " LPSC" : "",
405                         (temp & RH_HS_DRWE) ? " DRWE" : "",
406                         (temp & RH_HS_OCI) ? " OCI" : "",
407                         (temp & RH_HS_LPS) ? " LPS" : ""
408                         );
409         }
410         
411         for (i = 0; i < ndp; i++) {
412                 temp = roothub_portstatus (controller, i);
413                 dbg ("roothub.portstatus [%d] = 0x%08x%s%s%s%s%s%s%s%s%s%s%s%s",
414                         i,
415                         temp,
416                         (temp & RH_PS_PRSC) ? " PRSC" : "",
417                         (temp & RH_PS_OCIC) ? " OCIC" : "",
418                         (temp & RH_PS_PSSC) ? " PSSC" : "",
419                         (temp & RH_PS_PESC) ? " PESC" : "",
420                         (temp & RH_PS_CSC) ? " CSC" : "",
421
422                         (temp & RH_PS_LSDA) ? " LSDA" : "",
423                         (temp & RH_PS_PPS) ? " PPS" : "",
424                         (temp & RH_PS_PRS) ? " PRS" : "",
425                         (temp & RH_PS_POCI) ? " POCI" : "",
426                         (temp & RH_PS_PSS) ? " PSS" : "",
427
428                         (temp & RH_PS_PES) ? " PES" : "",
429                         (temp & RH_PS_CCS) ? " CCS" : ""
430                         );
431         }
432 }
433
434 static void ohci_dump (ohci_t *controller, int verbose)
435 {
436         dbg ("OHCI controller usb-%s state", controller->ohci_dev->slot_name);
437
438         // dumps some of the state we know about
439         ohci_dump_status (controller);
440         if (verbose)
441                 ep_print_int_eds (controller, "hcca");
442         dbg ("hcca frame #%04x", controller->hcca->frame_no);
443         ohci_dump_roothub (controller, 1);
444 }
445
446
447 #endif
448
449 /*-------------------------------------------------------------------------*
450  * Interface functions (URB)
451  *-------------------------------------------------------------------------*/
452
453 /* return a request to the completion handler */
454  
455 static int sohci_return_urb (struct ohci *hc, urb_t * urb)
456 {
457         urb_priv_t * urb_priv = urb->hcpriv;
458         urb_t * urbt;
459         unsigned long flags;
460         int i;
461         
462         if (!urb_priv)
463                 return -1; /* urb already unlinked */
464
465         /* just to be sure */
466         if (!urb->complete) {
467                 urb_rm_priv (urb);
468                 return -1;
469         }
470         
471 #ifdef DEBUG
472         urb_print (urb, "RET", usb_pipeout (urb->pipe));
473 #endif
474
475         switch (usb_pipetype (urb->pipe)) {
476                 case PIPE_INTERRUPT:
477                         pci_unmap_single (hc->ohci_dev,
478                                 urb_priv->td [0]->data_dma,
479                                 urb->transfer_buffer_length,
480                                 usb_pipeout (urb->pipe)
481                                         ? PCI_DMA_TODEVICE
482                                         : PCI_DMA_FROMDEVICE);
483                         urb->complete (urb);
484
485                         /* implicitly requeued */
486                         urb->actual_length = 0;
487                         urb->status = USB_ST_URB_PENDING;
488                         if (urb_priv->state != URB_DEL)
489                                 td_submit_urb (urb);
490                         break;
491                         
492                 case PIPE_ISOCHRONOUS:
493                         for (urbt = urb->next; urbt && (urbt != urb); urbt = urbt->next);
494                         if (urbt) { /* send the reply and requeue URB */        
495                                 pci_unmap_single (hc->ohci_dev,
496                                         urb_priv->td [0]->data_dma,
497                                         urb->transfer_buffer_length,
498                                         usb_pipeout (urb->pipe)
499                                                 ? PCI_DMA_TODEVICE
500                                                 : PCI_DMA_FROMDEVICE);
501                                 urb->complete (urb);
502                                 spin_lock_irqsave (&usb_ed_lock, flags);
503                                 urb->actual_length = 0;
504                                 urb->status = USB_ST_URB_PENDING;
505                                 urb->start_frame = urb_priv->ed->last_iso + 1;
506                                 if (urb_priv->state != URB_DEL) {
507                                         for (i = 0; i < urb->number_of_packets; i++) {
508                                                 urb->iso_frame_desc[i].actual_length = 0;
509                                                 urb->iso_frame_desc[i].status = -EXDEV;
510                                         }
511                                         td_submit_urb (urb);
512                                 }
513                                 spin_unlock_irqrestore (&usb_ed_lock, flags);
514                                 
515                         } else { /* unlink URB, call complete */
516                                 urb_rm_priv (urb);
517                                 urb->complete (urb);    
518                         }               
519                         break;
520                                 
521                 case PIPE_BULK:
522                 case PIPE_CONTROL: /* unlink URB, call complete */
523                         urb_rm_priv (urb);
524                         urb->complete (urb);    
525                         break;
526         }
527         return 0;
528 }
529
530 /*-------------------------------------------------------------------------*/
531
532 /* get a transfer request */
533  
534 static int sohci_submit_urb (urb_t * urb)
535 {
536         ohci_t * ohci;
537         ed_t * ed;
538         urb_priv_t * urb_priv;
539         unsigned int pipe = urb->pipe;
540         int i, size = 0;
541         unsigned long flags;
542         int bustime = 0;
543         int mem_flags = ALLOC_FLAGS;
544         
545         if (!urb->dev || !urb->dev->bus)
546                 return -ENODEV;
547         
548         if (urb->hcpriv)                        /* urb already in use */
549                 return -EINVAL;
550
551 //      if(usb_endpoint_halted (urb->dev, usb_pipeendpoint (pipe), usb_pipeout (pipe))) 
552 //              return -EPIPE;
553         
554         usb_inc_dev_use (urb->dev);
555         ohci = (ohci_t *) urb->dev->bus->hcpriv;
556         
557 #ifdef DEBUG
558         urb_print (urb, "SUB", usb_pipein (pipe));
559 #endif
560         
561         /* handle a request to the virtual root hub */
562         if (usb_pipedevice (pipe) == ohci->rh.devnum) 
563                 return rh_submit_urb (urb);
564         
565         /* when controller's hung, permit only roothub cleanup attempts
566          * such as powering down ports */
567         if (ohci->disabled) {
568                 usb_dec_dev_use (urb->dev);     
569                 return -ESHUTDOWN;
570         }
571
572         /* every endpoint has a ed, locate and fill it */
573         if (!(ed = ep_add_ed (urb->dev, pipe, urb->interval, 1, mem_flags))) {
574                 usb_dec_dev_use (urb->dev);     
575                 return -ENOMEM;
576         }
577
578         /* for the private part of the URB we need the number of TDs (size) */
579         switch (usb_pipetype (pipe)) {
580                 case PIPE_BULK: /* one TD for every 4096 Byte */
581                         size = (urb->transfer_buffer_length - 1) / 4096 + 1;
582                         break;
583                 case PIPE_ISOCHRONOUS: /* number of packets from URB */
584                         size = urb->number_of_packets;
585                         if (size <= 0) {
586                                 usb_dec_dev_use (urb->dev);     
587                                 return -EINVAL;
588                         }
589                         for (i = 0; i < urb->number_of_packets; i++) {
590                                 urb->iso_frame_desc[i].actual_length = 0;
591                                 urb->iso_frame_desc[i].status = -EXDEV;
592                         }
593                         break;
594                 case PIPE_CONTROL: /* 1 TD for setup, 1 for ACK and 1 for every 4096 B */
595                         size = (urb->transfer_buffer_length == 0)? 2: 
596                                                 (urb->transfer_buffer_length - 1) / 4096 + 3;
597                         break;
598                 case PIPE_INTERRUPT: /* one TD */
599                         size = 1;
600                         break;
601         }
602
603         /* allocate the private part of the URB */
604         urb_priv = kmalloc (sizeof (urb_priv_t) + size * sizeof (td_t *), 
605                                                         in_interrupt() ? GFP_ATOMIC : GFP_KERNEL);
606         if (!urb_priv) {
607                 usb_dec_dev_use (urb->dev);     
608                 return -ENOMEM;
609         }
610         memset (urb_priv, 0, sizeof (urb_priv_t) + size * sizeof (td_t *));
611         
612         /* fill the private part of the URB */
613         urb_priv->length = size;
614         urb_priv->ed = ed;      
615
616         /* allocate the TDs (updating hash chains) */
617         spin_lock_irqsave (&usb_ed_lock, flags);
618         for (i = 0; i < size; i++) { 
619                 urb_priv->td[i] = td_alloc (ohci, SLAB_ATOMIC);
620                 if (!urb_priv->td[i]) {
621                         urb_priv->length = i;
622                         urb_free_priv (ohci, urb_priv);
623                         spin_unlock_irqrestore (&usb_ed_lock, flags);
624                         usb_dec_dev_use (urb->dev);     
625                         return -ENOMEM;
626                 }
627         }       
628
629         if (ed->state == ED_NEW || (ed->state & ED_DEL)) {
630                 urb_free_priv (ohci, urb_priv);
631                 spin_unlock_irqrestore (&usb_ed_lock, flags);
632                 usb_dec_dev_use (urb->dev);     
633                 return -EINVAL;
634         }
635         
636         /* allocate and claim bandwidth if needed; ISO
637          * needs start frame index if it was't provided.
638          */
639         switch (usb_pipetype (pipe)) {
640                 case PIPE_ISOCHRONOUS:
641                         if (urb->transfer_flags & USB_ISO_ASAP) { 
642                                 urb->start_frame = ((ed->state == ED_OPER)
643                                         ? (ed->last_iso + 1)
644                                         : (le16_to_cpu (ohci->hcca->frame_no) + 10)) & 0xffff;
645                         }       
646                         /* FALLTHROUGH */
647                 case PIPE_INTERRUPT:
648                         if (urb->bandwidth == 0) {
649                                 bustime = usb_check_bandwidth (urb->dev, urb);
650                         }
651                         if (bustime < 0) {
652                                 urb_free_priv (ohci, urb_priv);
653                                 spin_unlock_irqrestore (&usb_ed_lock, flags);
654                                 usb_dec_dev_use (urb->dev);     
655                                 return bustime;
656                         }
657                         usb_claim_bandwidth (urb->dev, urb, bustime, usb_pipeisoc (urb->pipe));
658 #ifdef  DO_TIMEOUTS
659                         urb->timeout = 0;
660 #endif
661         }
662
663         urb->actual_length = 0;
664         urb->hcpriv = urb_priv;
665         urb->status = USB_ST_URB_PENDING;
666
667         /* link the ed into a chain if is not already */
668         if (ed->state != ED_OPER)
669                 ep_link (ohci, ed);
670
671         /* fill the TDs and link it to the ed */
672         td_submit_urb (urb);
673
674 #ifdef  DO_TIMEOUTS
675         /* maybe add to ordered list of timeouts */
676         if (urb->timeout) {
677                 struct list_head        *entry;
678
679                 // FIXME:  usb-uhci uses relative timeouts (like this),
680                 // while uhci uses absolute ones (probably better).
681                 // Pick one solution and change the affected drivers.
682                 urb->timeout += jiffies;
683
684                 list_for_each (entry, &ohci->timeout_list) {
685                         struct urb      *next_urb;
686
687                         next_urb = list_entry (entry, struct urb, urb_list);
688                         if (time_after_eq (urb->timeout, next_urb->timeout))
689                                 break;
690                 }
691                 list_add (&urb->urb_list, entry);
692
693                 /* drive timeouts by SF (messy, but works) */
694                 writel (OHCI_INTR_SF, &ohci->regs->intrenable); 
695         }
696 #endif
697
698         spin_unlock_irqrestore (&usb_ed_lock, flags);
699
700         return 0;       
701 }
702
703 /*-------------------------------------------------------------------------*/
704
705 /* deactivate all TDs and remove the private part of the URB */
706 /* interrupt callers must use async unlink mode */
707
708 static int sohci_unlink_urb (urb_t * urb)
709 {
710         unsigned long flags;
711         ohci_t * ohci;
712         
713         if (!urb) /* just to be sure */ 
714                 return -EINVAL;
715                 
716         if (!urb->dev || !urb->dev->bus)
717                 return -ENODEV;
718
719         ohci = (ohci_t *) urb->dev->bus->hcpriv; 
720
721 #ifdef DEBUG
722         urb_print (urb, "UNLINK", 1);
723 #endif            
724
725         /* handle a request to the virtual root hub */
726         if (usb_pipedevice (urb->pipe) == ohci->rh.devnum)
727                 return rh_unlink_urb (urb);
728
729         if (urb->hcpriv && (urb->status == USB_ST_URB_PENDING)) { 
730                 if (!ohci->disabled) {
731                         urb_priv_t  * urb_priv;
732
733                         /* interrupt code may not sleep; it must use
734                          * async status return to unlink pending urbs.
735                          */
736                         if (!(urb->transfer_flags & USB_ASYNC_UNLINK)
737                                         && in_interrupt ()) {
738                                 err ("bug in call from %p; use async!",
739                                         __builtin_return_address(0));
740                                 return -EWOULDBLOCK;
741                         }
742
743                         /* flag the urb and its TDs for deletion in some
744                          * upcoming SF interrupt delete list processing
745                          */
746                         spin_lock_irqsave (&usb_ed_lock, flags);
747                         urb_priv = urb->hcpriv;
748
749                         if (!urb_priv || (urb_priv->state == URB_DEL)) {
750                                 spin_unlock_irqrestore (&usb_ed_lock, flags);
751                                 return 0;
752                         }
753                                 
754                         urb_priv->state = URB_DEL; 
755                         ep_rm_ed (urb->dev, urb_priv->ed);
756                         urb_priv->ed->state |= ED_URB_DEL;
757
758                         if (!(urb->transfer_flags & USB_ASYNC_UNLINK)) {
759                                 DECLARE_WAIT_QUEUE_HEAD (unlink_wakeup); 
760                                 DECLARE_WAITQUEUE (wait, current);
761                                 int timeout = OHCI_UNLINK_TIMEOUT;
762
763                                 add_wait_queue (&unlink_wakeup, &wait);
764                                 urb_priv->wait = &unlink_wakeup;
765                                 spin_unlock_irqrestore (&usb_ed_lock, flags);
766
767                                 /* wait until all TDs are deleted */
768                                 set_current_state(TASK_UNINTERRUPTIBLE);
769                                 while (timeout && (urb->status == USB_ST_URB_PENDING))
770                                         timeout = schedule_timeout (timeout);
771                                 current->state = TASK_RUNNING;
772                                 remove_wait_queue (&unlink_wakeup, &wait); 
773                                 if (urb->status == USB_ST_URB_PENDING) {
774                                         err ("unlink URB timeout");
775                                         return -ETIMEDOUT;
776                                 }
777                         } else {
778                                 /* usb_dec_dev_use done in dl_del_list() */
779                                 urb->status = -EINPROGRESS;
780                                 spin_unlock_irqrestore (&usb_ed_lock, flags);
781                         }
782                 } else {
783                         urb_rm_priv (urb);
784                         if (urb->transfer_flags & USB_ASYNC_UNLINK) {
785                                 urb->status = -ECONNRESET;
786                                 if (urb->complete)
787                                         urb->complete (urb); 
788                         } else 
789                                 urb->status = -ENOENT;
790                 }       
791         }       
792         return 0;
793 }
794
795 /*-------------------------------------------------------------------------*/
796
797 /* allocate private data space for a usb device */
798
799 static int sohci_alloc_dev (struct usb_device *usb_dev)
800 {
801         struct ohci_device * dev;
802
803         dev = dev_alloc ((struct ohci *) usb_dev->bus->hcpriv, ALLOC_FLAGS);
804         if (!dev)
805                 return -ENOMEM;
806
807         usb_dev->hcpriv = dev;
808         return 0;
809 }
810
811 /*-------------------------------------------------------------------------*/
812
813 /* may be called from interrupt context */
814 /* frees private data space of usb device */
815   
816 static int sohci_free_dev (struct usb_device * usb_dev)
817 {
818         unsigned long flags;
819         int i, cnt = 0;
820         ed_t * ed;
821         struct ohci_device * dev = usb_to_ohci (usb_dev);
822         ohci_t * ohci = usb_dev->bus->hcpriv;
823         
824         if (!dev)
825                 return 0;
826         
827         if (usb_dev->devnum >= 0) {
828         
829                 /* driver disconnects should have unlinked all urbs
830                  * (freeing all the TDs, unlinking EDs) but we need
831                  * to defend against bugs that prevent that.
832                  */
833                 spin_lock_irqsave (&usb_ed_lock, flags);        
834                 for(i = 0; i < NUM_EDS; i++) {
835                         ed = &(dev->ed[i]);
836                         if (ed->state != ED_NEW) {
837                                 if (ed->state == ED_OPER) {
838                                         /* driver on that interface didn't unlink an urb */
839                                         dbg ("driver usb-%s dev %d ed 0x%x unfreed URB",
840                                                 ohci->ohci_dev->slot_name, usb_dev->devnum, i);
841                                         ep_unlink (ohci, ed);
842                                 }
843                                 ep_rm_ed (usb_dev, ed);
844                                 ed->state = ED_DEL;
845                                 cnt++;
846                         }
847                 }
848                 spin_unlock_irqrestore (&usb_ed_lock, flags);
849                 
850                 /* if the controller is running, tds for those unlinked
851                  * urbs get freed by dl_del_list at the next SF interrupt
852                  */
853                 if (cnt > 0) {
854
855                         if (ohci->disabled) {
856                                 /* FIXME: Something like this should kick in,
857                                  * though it's currently an exotic case ...
858                                  * the controller won't ever be touching
859                                  * these lists again!!
860                                 dl_del_list (ohci,
861                                         le16_to_cpu (ohci->hcca->frame_no) & 1);
862                                  */
863                                 warn ("TD leak, %d", cnt);
864
865                         } else if (!in_interrupt ()) {
866                                 DECLARE_WAIT_QUEUE_HEAD (freedev_wakeup); 
867                                 DECLARE_WAITQUEUE (wait, current);
868                                 int timeout = OHCI_UNLINK_TIMEOUT;
869
870                                 /* SF interrupt handler calls dl_del_list */
871                                 add_wait_queue (&freedev_wakeup, &wait);
872                                 dev->wait = &freedev_wakeup;
873                                 set_current_state(TASK_UNINTERRUPTIBLE);
874                                 while (timeout && dev->ed_cnt)
875                                         timeout = schedule_timeout (timeout);
876                                 current->state = TASK_RUNNING;
877                                 remove_wait_queue (&freedev_wakeup, &wait);
878                                 if (dev->ed_cnt) {
879                                         err ("free device %d timeout", usb_dev->devnum);
880                                         return -ETIMEDOUT;
881                                 }
882                         } else {
883                                 /* likely some interface's driver has a refcount bug */
884                                 err ("bus %s devnum %d deletion in interrupt",
885                                         ohci->ohci_dev->slot_name, usb_dev->devnum);
886                                 BUG ();
887                         }
888                 }
889         }
890
891         /* free device, and associated EDs */
892         dev_free (ohci, dev);
893
894         return 0;
895 }
896
897 /*-------------------------------------------------------------------------*/
898
899 /* tell us the current USB frame number */
900
901 static int sohci_get_current_frame_number (struct usb_device *usb_dev) 
902 {
903         ohci_t * ohci = usb_dev->bus->hcpriv;
904         
905         return le16_to_cpu (ohci->hcca->frame_no);
906 }
907
908 /*-------------------------------------------------------------------------*/
909
910 struct usb_operations sohci_device_operations = {
911         sohci_alloc_dev,
912         sohci_free_dev,
913         sohci_get_current_frame_number,
914         sohci_submit_urb,
915         sohci_unlink_urb
916 };
917
918 /*-------------------------------------------------------------------------*
919  * ED handling functions
920  *-------------------------------------------------------------------------*/  
921                 
922 /* search for the right branch to insert an interrupt ed into the int tree 
923  * do some load ballancing;
924  * returns the branch and 
925  * sets the interval to interval = 2^integer (ld (interval)) */
926
927 static int ep_int_ballance (ohci_t * ohci, int interval, int load)
928 {
929         int i, branch = 0;
930    
931         /* search for the least loaded interrupt endpoint branch of all 32 branches */
932         for (i = 0; i < 32; i++) 
933                 if (ohci->ohci_int_load [branch] > ohci->ohci_int_load [i]) branch = i; 
934   
935         branch = branch % interval;
936         for (i = branch; i < 32; i += interval) ohci->ohci_int_load [i] += load;
937
938         return branch;
939 }
940
941 /*-------------------------------------------------------------------------*/
942
943 /*  2^int( ld (inter)) */
944
945 static int ep_2_n_interval (int inter)
946 {       
947         int i;
948         for (i = 0; ((inter >> i) > 1 ) && (i < 5); i++); 
949         return 1 << i;
950 }
951
952 /*-------------------------------------------------------------------------*/
953
954 /* the int tree is a binary tree 
955  * in order to process it sequentially the indexes of the branches have to be mapped 
956  * the mapping reverses the bits of a word of num_bits length */
957  
958 static int ep_rev (int num_bits, int word)
959 {
960         int i, wout = 0;
961
962         for (i = 0; i < num_bits; i++) wout |= (((word >> i) & 1) << (num_bits - i - 1));
963         return wout;
964 }
965
966 /*-------------------------------------------------------------------------*/
967
968 /* link an ed into one of the HC chains */
969
970 static int ep_link (ohci_t * ohci, ed_t * edi)
971 {        
972         int int_branch;
973         int i;
974         int inter;
975         int interval;
976         int load;
977         __u32 * ed_p;
978         volatile ed_t * ed = edi;
979         
980         ed->state = ED_OPER;
981         
982         switch (ed->type) {
983         case PIPE_CONTROL:
984                 ed->hwNextED = 0;
985                 if (ohci->ed_controltail == NULL) {
986                         writel (ed->dma, &ohci->regs->ed_controlhead);
987                 } else {
988                         ohci->ed_controltail->hwNextED = cpu_to_le32 (ed->dma);
989                 }
990                 ed->ed_prev = ohci->ed_controltail;
991                 if (!ohci->ed_controltail && !ohci->ed_rm_list[0] &&
992                         !ohci->ed_rm_list[1] && !ohci->sleeping) {
993                         ohci->hc_control |= OHCI_CTRL_CLE;
994                         writel (ohci->hc_control, &ohci->regs->control);
995                 }
996                 ohci->ed_controltail = edi;       
997                 break;
998                 
999         case PIPE_BULK:
1000                 ed->hwNextED = 0;
1001                 if (ohci->ed_bulktail == NULL) {
1002                         writel (ed->dma, &ohci->regs->ed_bulkhead);
1003                 } else {
1004                         ohci->ed_bulktail->hwNextED = cpu_to_le32 (ed->dma);
1005                 }
1006                 ed->ed_prev = ohci->ed_bulktail;
1007                 if (!ohci->ed_bulktail && !ohci->ed_rm_list[0] &&
1008                         !ohci->ed_rm_list[1] && !ohci->sleeping) {
1009                         ohci->hc_control |= OHCI_CTRL_BLE;
1010                         writel (ohci->hc_control, &ohci->regs->control);
1011                 }
1012                 ohci->ed_bulktail = edi;          
1013                 break;
1014                 
1015         case PIPE_INTERRUPT:
1016                 load = ed->int_load;
1017                 interval = ep_2_n_interval (ed->int_period);
1018                 ed->int_interval = interval;
1019                 int_branch = ep_int_ballance (ohci, interval, load);
1020                 ed->int_branch = int_branch;
1021                 
1022                 for (i = 0; i < ep_rev (6, interval); i += inter) {
1023                         inter = 1;
1024                         for (ed_p = &(ohci->hcca->int_table[ep_rev (5, i) + int_branch]); 
1025                                 (*ed_p != 0) && ((dma_to_ed (ohci, le32_to_cpup (ed_p)))->int_interval >= interval); 
1026                                 ed_p = &((dma_to_ed (ohci, le32_to_cpup (ed_p)))->hwNextED)) 
1027                                         inter = ep_rev (6, (dma_to_ed (ohci, le32_to_cpup (ed_p)))->int_interval);
1028                         ed->hwNextED = *ed_p; 
1029                         *ed_p = cpu_to_le32 (ed->dma);
1030                 }
1031 #ifdef DEBUG
1032                 ep_print_int_eds (ohci, "LINK_INT");
1033 #endif
1034                 break;
1035                 
1036         case PIPE_ISOCHRONOUS:
1037                 ed->hwNextED = 0;
1038                 ed->int_interval = 1;
1039                 if (ohci->ed_isotail != NULL) {
1040                         ohci->ed_isotail->hwNextED = cpu_to_le32 (ed->dma);
1041                         ed->ed_prev = ohci->ed_isotail;
1042                 } else {
1043                         for ( i = 0; i < 32; i += inter) {
1044                                 inter = 1;
1045                                 for (ed_p = &(ohci->hcca->int_table[ep_rev (5, i)]); 
1046                                         *ed_p != 0; 
1047                                         ed_p = &((dma_to_ed (ohci, le32_to_cpup (ed_p)))->hwNextED)) 
1048                                                 inter = ep_rev (6, (dma_to_ed (ohci, le32_to_cpup (ed_p)))->int_interval);
1049                                 *ed_p = cpu_to_le32 (ed->dma);  
1050                         }       
1051                         ed->ed_prev = NULL;
1052                 }       
1053                 ohci->ed_isotail = edi;  
1054 #ifdef DEBUG
1055                 ep_print_int_eds (ohci, "LINK_ISO");
1056 #endif
1057                 break;
1058         }               
1059         return 0;
1060 }
1061
1062 /*-------------------------------------------------------------------------*/
1063
1064 /* unlink an ed from one of the HC chains. 
1065  * just the link to the ed is unlinked.
1066  * the link from the ed still points to another operational ed or 0
1067  * so the HC can eventually finish the processing of the unlinked ed */
1068
1069 static int ep_unlink (ohci_t * ohci, ed_t * ed) 
1070 {
1071         int int_branch;
1072         int i;
1073         int inter;
1074         int interval;
1075         __u32 * ed_p;
1076
1077         ed->hwINFO |= cpu_to_le32 (OHCI_ED_SKIP);
1078
1079         switch (ed->type) {
1080         case PIPE_CONTROL:
1081                 if (ed->ed_prev == NULL) {
1082                         if (!ed->hwNextED) {
1083                                 ohci->hc_control &= ~OHCI_CTRL_CLE;
1084                                 writel (ohci->hc_control, &ohci->regs->control);
1085                         }
1086                         writel (le32_to_cpup (&ed->hwNextED), &ohci->regs->ed_controlhead);
1087                 } else {
1088                         ed->ed_prev->hwNextED = ed->hwNextED;
1089                 }
1090                 if (ohci->ed_controltail == ed) {
1091                         ohci->ed_controltail = ed->ed_prev;
1092                 } else {
1093                         (dma_to_ed (ohci, le32_to_cpup (&ed->hwNextED)))->ed_prev = ed->ed_prev;
1094                 }
1095                 break;
1096       
1097         case PIPE_BULK:
1098                 if (ed->ed_prev == NULL) {
1099                         if (!ed->hwNextED) {
1100                                 ohci->hc_control &= ~OHCI_CTRL_BLE;
1101                                 writel (ohci->hc_control, &ohci->regs->control);
1102                         }
1103                         writel (le32_to_cpup (&ed->hwNextED), &ohci->regs->ed_bulkhead);
1104                 } else {
1105                         ed->ed_prev->hwNextED = ed->hwNextED;
1106                 }
1107                 if (ohci->ed_bulktail == ed) {
1108                         ohci->ed_bulktail = ed->ed_prev;
1109                 } else {
1110                         (dma_to_ed (ohci, le32_to_cpup (&ed->hwNextED)))->ed_prev = ed->ed_prev;
1111                 }
1112                 break;
1113       
1114         case PIPE_INTERRUPT:
1115                 int_branch = ed->int_branch;
1116                 interval = ed->int_interval;
1117
1118                 for (i = 0; i < ep_rev (6, interval); i += inter) {
1119                         for (ed_p = &(ohci->hcca->int_table[ep_rev (5, i) + int_branch]), inter = 1; 
1120                                 (*ed_p != 0) && (*ed_p != ed->hwNextED); 
1121                                 ed_p = &((dma_to_ed (ohci, le32_to_cpup (ed_p)))->hwNextED), 
1122                                 inter = ep_rev (6, (dma_to_ed (ohci, le32_to_cpup (ed_p)))->int_interval)) {                            
1123                                         if((dma_to_ed (ohci, le32_to_cpup (ed_p))) == ed) {
1124                                                 *ed_p = ed->hwNextED;           
1125                                                 break;
1126                                         }
1127                           }
1128                 }
1129                 for (i = int_branch; i < 32; i += interval)
1130                     ohci->ohci_int_load[i] -= ed->int_load;
1131 #ifdef DEBUG
1132                 ep_print_int_eds (ohci, "UNLINK_INT");
1133 #endif
1134                 break;
1135                 
1136         case PIPE_ISOCHRONOUS:
1137                 if (ohci->ed_isotail == ed)
1138                         ohci->ed_isotail = ed->ed_prev;
1139                 if (ed->hwNextED != 0) 
1140                     (dma_to_ed (ohci, le32_to_cpup (&ed->hwNextED)))->ed_prev = ed->ed_prev;
1141                                     
1142                 if (ed->ed_prev != NULL) {
1143                         ed->ed_prev->hwNextED = ed->hwNextED;
1144                 } else {
1145                         for (i = 0; i < 32; i++) {
1146                                 for (ed_p = &(ohci->hcca->int_table[ep_rev (5, i)]); 
1147                                                 *ed_p != 0; 
1148                                                 ed_p = &((dma_to_ed (ohci, le32_to_cpup (ed_p)))->hwNextED)) {
1149                                         // inter = ep_rev (6, (dma_to_ed (ohci, le32_to_cpup (ed_p)))->int_interval);
1150                                         if((dma_to_ed (ohci, le32_to_cpup (ed_p))) == ed) {
1151                                                 *ed_p = ed->hwNextED;           
1152                                                 break;
1153                                         }
1154                                 }
1155                         }       
1156                 }       
1157 #ifdef DEBUG
1158                 ep_print_int_eds (ohci, "UNLINK_ISO");
1159 #endif
1160                 break;
1161         }
1162         ed->state = ED_UNLINK;
1163         return 0;
1164 }
1165
1166
1167 /*-------------------------------------------------------------------------*/
1168
1169 /* add/reinit an endpoint; this should be done once at the usb_set_configuration command,
1170  * but the USB stack is a little bit stateless  so we do it at every transaction
1171  * if the state of the ed is ED_NEW then a dummy td is added and the state is changed to ED_UNLINK
1172  * in all other cases the state is left unchanged
1173  * the ed info fields are setted anyway even though most of them should not change */
1174  
1175 static ed_t * ep_add_ed (
1176         struct usb_device * usb_dev,
1177         unsigned int pipe,
1178         int interval,
1179         int load,
1180         int mem_flags
1181 )
1182 {
1183         ohci_t * ohci = usb_dev->bus->hcpriv;
1184         td_t * td;
1185         ed_t * ed_ret;
1186         volatile ed_t * ed; 
1187         unsigned long flags;
1188         
1189         
1190         spin_lock_irqsave (&usb_ed_lock, flags);
1191
1192         ed = ed_ret = &(usb_to_ohci (usb_dev)->ed[(usb_pipeendpoint (pipe) << 1) | 
1193                         (usb_pipecontrol (pipe)? 0: usb_pipeout (pipe))]);
1194
1195         if ((ed->state & ED_DEL) || (ed->state & ED_URB_DEL)) {
1196                 /* pending delete request */
1197                 spin_unlock_irqrestore (&usb_ed_lock, flags);
1198                 return NULL;
1199         }
1200         
1201         if (ed->state == ED_NEW) {
1202                 ed->hwINFO = cpu_to_le32 (OHCI_ED_SKIP); /* skip ed */
1203                 /* dummy td; end of td list for ed */
1204                 td = td_alloc (ohci, SLAB_ATOMIC);
1205                 /* hash the ed for later reverse mapping */
1206                 if (!td || !hash_add_ed (ohci, (ed_t *)ed)) {
1207                         /* out of memory */
1208                         if (td)
1209                             td_free(ohci, td);
1210                         spin_unlock_irqrestore (&usb_ed_lock, flags);
1211                         return NULL;
1212                 }
1213                 ed->hwTailP = cpu_to_le32 (td->td_dma);
1214                 ed->hwHeadP = ed->hwTailP;      
1215                 ed->state = ED_UNLINK;
1216                 ed->type = usb_pipetype (pipe);
1217                 usb_to_ohci (usb_dev)->ed_cnt++;
1218         }
1219
1220         ohci->dev[usb_pipedevice (pipe)] = usb_dev;
1221         
1222         ed->hwINFO = cpu_to_le32 (usb_pipedevice (pipe)
1223                         | usb_pipeendpoint (pipe) << 7
1224                         | (usb_pipeisoc (pipe)? 0x8000: 0)
1225                         | (usb_pipecontrol (pipe)? 0: (usb_pipeout (pipe)? 0x800: 0x1000)) 
1226                         | usb_pipeslow (pipe) << 13
1227                         | usb_maxpacket (usb_dev, pipe, usb_pipeout (pipe)) << 16);
1228   
1229         if (ed->type == PIPE_INTERRUPT && ed->state == ED_UNLINK) {
1230                 ed->int_period = interval;
1231                 ed->int_load = load;
1232         }
1233         
1234         spin_unlock_irqrestore (&usb_ed_lock, flags);
1235         return ed_ret; 
1236 }
1237
1238 /*-------------------------------------------------------------------------*/
1239  
1240 /* request the removal of an endpoint
1241  * put the ep on the rm_list and request a stop of the bulk or ctrl list 
1242  * real removal is done at the next start frame (SF) hardware interrupt */
1243  
1244 static void ep_rm_ed (struct usb_device * usb_dev, ed_t * ed)
1245 {    
1246         unsigned int frame;
1247         ohci_t * ohci = usb_dev->bus->hcpriv;
1248
1249         if ((ed->state & ED_DEL) || (ed->state & ED_URB_DEL))
1250                 return;
1251         
1252         ed->hwINFO |= cpu_to_le32 (OHCI_ED_SKIP);
1253
1254         if (!ohci->disabled) {
1255                 switch (ed->type) {
1256                         case PIPE_CONTROL: /* stop control list */
1257                                 ohci->hc_control &= ~OHCI_CTRL_CLE;
1258                                 writel (ohci->hc_control, &ohci->regs->control); 
1259                                 break;
1260                         case PIPE_BULK: /* stop bulk list */
1261                                 ohci->hc_control &= ~OHCI_CTRL_BLE;
1262                                 writel (ohci->hc_control, &ohci->regs->control); 
1263                                 break;
1264                 }
1265         }
1266
1267         frame = le16_to_cpu (ohci->hcca->frame_no) & 0x1;
1268         ed->ed_rm_list = ohci->ed_rm_list[frame];
1269         ohci->ed_rm_list[frame] = ed;
1270
1271         if (!ohci->disabled && !ohci->sleeping) {
1272                 /* enable SOF interrupt */
1273                 writel (OHCI_INTR_SF, &ohci->regs->intrstatus);
1274                 writel (OHCI_INTR_SF, &ohci->regs->intrenable);
1275         }
1276 }
1277
1278 /*-------------------------------------------------------------------------*
1279  * TD handling functions
1280  *-------------------------------------------------------------------------*/
1281
1282 /* enqueue next TD for this URB (OHCI spec 5.2.8.2) */
1283
1284 static void
1285 td_fill (ohci_t * ohci, unsigned int info,
1286         dma_addr_t data, int len,
1287         urb_t * urb, int index)
1288 {
1289         volatile td_t  * td, * td_pt;
1290         urb_priv_t * urb_priv = urb->hcpriv;
1291         
1292         if (index >= urb_priv->length) {
1293                 err("internal OHCI error: TD index > length");
1294                 return;
1295         }
1296         
1297         /* use this td as the next dummy */
1298         td_pt = urb_priv->td [index];
1299         td_pt->hwNextTD = 0;
1300
1301         /* fill the old dummy TD */
1302         td = urb_priv->td [index] = dma_to_td (ohci,
1303                         le32_to_cpup (&urb_priv->ed->hwTailP) & ~0xf);
1304
1305         td->ed = urb_priv->ed;
1306         td->next_dl_td = NULL;
1307         td->index = index;
1308         td->urb = urb; 
1309         td->data_dma = data;
1310         if (!len)
1311                 data = 0;
1312
1313         td->hwINFO = cpu_to_le32 (info);
1314         if ((td->ed->type) == PIPE_ISOCHRONOUS) {
1315                 td->hwCBP = cpu_to_le32 (data & 0xFFFFF000);
1316                 td->ed->last_iso = info & 0xffff;
1317         } else {
1318                 td->hwCBP = cpu_to_le32 (data); 
1319         }                       
1320         if (data)
1321                 td->hwBE = cpu_to_le32 (data + len - 1);
1322         else
1323                 td->hwBE = 0;
1324         td->hwNextTD = cpu_to_le32 (td_pt->td_dma);
1325         td->hwPSW [0] = cpu_to_le16 ((data & 0x0FFF) | 0xE000);
1326
1327         /* append to queue */
1328         td->ed->hwTailP = td->hwNextTD;
1329 }
1330
1331 /*-------------------------------------------------------------------------*/
1332  
1333 /* prepare all TDs of a transfer */
1334
1335 static void td_submit_urb (urb_t * urb)
1336
1337         urb_priv_t * urb_priv = urb->hcpriv;
1338         ohci_t * ohci = (ohci_t *) urb->dev->bus->hcpriv;
1339         dma_addr_t data;
1340         int data_len = urb->transfer_buffer_length;
1341         int cnt = 0; 
1342         __u32 info = 0;
1343         unsigned int toggle = 0;
1344
1345         /* OHCI handles the DATA-toggles itself, we just use the USB-toggle bits for reseting */
1346         if(usb_gettoggle(urb->dev, usb_pipeendpoint(urb->pipe), usb_pipeout(urb->pipe))) {
1347                 toggle = TD_T_TOGGLE;
1348         } else {
1349                 toggle = TD_T_DATA0;
1350                 usb_settoggle(urb->dev, usb_pipeendpoint(urb->pipe), usb_pipeout(urb->pipe), 1);
1351         }
1352         
1353         urb_priv->td_cnt = 0;
1354
1355         if (data_len) {
1356                 data = pci_map_single (ohci->ohci_dev,
1357                         urb->transfer_buffer, data_len,
1358                         usb_pipeout (urb->pipe)
1359                                 ? PCI_DMA_TODEVICE
1360                                 : PCI_DMA_FROMDEVICE
1361                         );
1362         } else
1363                 data = 0;
1364         
1365         switch (usb_pipetype (urb->pipe)) {
1366                 case PIPE_BULK:
1367                         info = usb_pipeout (urb->pipe)? 
1368                                 TD_CC | TD_DP_OUT : TD_CC | TD_DP_IN ;
1369                         while(data_len > 4096) {                
1370                                 td_fill (ohci, info | (cnt? TD_T_TOGGLE:toggle), data, 4096, urb, cnt);
1371                                 data += 4096; data_len -= 4096; cnt++;
1372                         }
1373                         info = usb_pipeout (urb->pipe)?
1374                                 TD_CC | TD_DP_OUT : TD_CC | TD_R | TD_DP_IN ;
1375                         td_fill (ohci, info | (cnt? TD_T_TOGGLE:toggle), data, data_len, urb, cnt);
1376                         cnt++;
1377                         if (!ohci->sleeping)
1378                                 writel (OHCI_BLF, &ohci->regs->cmdstatus); /* start bulk list */
1379                         break;
1380
1381                 case PIPE_INTERRUPT:
1382                         info = usb_pipeout (urb->pipe)? 
1383                                 TD_CC | TD_DP_OUT | toggle: TD_CC | TD_R | TD_DP_IN | toggle;
1384                         td_fill (ohci, info, data, data_len, urb, cnt++);
1385                         break;
1386
1387                 case PIPE_CONTROL:
1388                         info = TD_CC | TD_DP_SETUP | TD_T_DATA0;
1389                         td_fill (ohci, info,
1390                                 pci_map_single (ohci->ohci_dev,
1391                                         urb->setup_packet, 8,
1392                                         PCI_DMA_TODEVICE),
1393                                 8, urb, cnt++); 
1394                         if (data_len > 0) {  
1395                                 info = usb_pipeout (urb->pipe)? 
1396                                         TD_CC | TD_R | TD_DP_OUT | TD_T_DATA1 : TD_CC | TD_R | TD_DP_IN | TD_T_DATA1;
1397                                 /* NOTE:  mishandles transfers >8K, some >4K */
1398                                 td_fill (ohci, info, data, data_len, urb, cnt++);  
1399                         } 
1400                         info = usb_pipeout (urb->pipe)? 
1401                                 TD_CC | TD_DP_IN | TD_T_DATA1: TD_CC | TD_DP_OUT | TD_T_DATA1;
1402                         td_fill (ohci, info, data, 0, urb, cnt++);
1403                         if (!ohci->sleeping)
1404                                 writel (OHCI_CLF, &ohci->regs->cmdstatus); /* start Control list */
1405                         break;
1406
1407                 case PIPE_ISOCHRONOUS:
1408                         for (cnt = 0; cnt < urb->number_of_packets; cnt++) {
1409                                 td_fill (ohci, TD_CC|TD_ISO | ((urb->start_frame + cnt) & 0xffff), 
1410                                         data + urb->iso_frame_desc[cnt].offset, 
1411                                         urb->iso_frame_desc[cnt].length, urb, cnt); 
1412                         }
1413                         break;
1414         } 
1415         if (urb_priv->length != cnt) 
1416                 dbg("TD LENGTH %d != CNT %d", urb_priv->length, cnt);
1417 }
1418
1419 /*-------------------------------------------------------------------------*
1420  * Done List handling functions
1421  *-------------------------------------------------------------------------*/
1422
1423
1424 /* calculate the transfer length and update the urb */
1425
1426 static void dl_transfer_length(td_t * td)
1427 {
1428         __u32 tdINFO, tdBE, tdCBP;
1429         __u16 tdPSW;
1430         urb_t * urb = td->urb;
1431         urb_priv_t * urb_priv = urb->hcpriv;
1432         int dlen = 0;
1433         int cc = 0;
1434         
1435         tdINFO = le32_to_cpup (&td->hwINFO);
1436         tdBE   = le32_to_cpup (&td->hwBE);
1437         tdCBP  = le32_to_cpup (&td->hwCBP);
1438
1439
1440         if (tdINFO & TD_ISO) {
1441                 tdPSW = le16_to_cpu (td->hwPSW[0]);
1442                 cc = (tdPSW >> 12) & 0xF;
1443                 if (cc < 0xE)  {
1444                         if (usb_pipeout(urb->pipe)) {
1445                                 dlen = urb->iso_frame_desc[td->index].length;
1446                         } else {
1447                                 dlen = tdPSW & 0x3ff;
1448                         }
1449                         urb->actual_length += dlen;
1450                         urb->iso_frame_desc[td->index].actual_length = dlen;
1451                         if (!(urb->transfer_flags & USB_DISABLE_SPD) && (cc == TD_DATAUNDERRUN))
1452                                 cc = TD_CC_NOERROR;
1453                                          
1454                         urb->iso_frame_desc[td->index].status = cc_to_error[cc];
1455                 }
1456         } else { /* BULK, INT, CONTROL DATA */
1457                 if (!(usb_pipetype (urb->pipe) == PIPE_CONTROL && 
1458                                 ((td->index == 0) || (td->index == urb_priv->length - 1)))) {
1459                         if (tdBE != 0) {
1460                                 if (td->hwCBP == 0)
1461                                         urb->actual_length += tdBE - td->data_dma + 1;
1462                                 else
1463                                         urb->actual_length += tdCBP - td->data_dma;
1464                         }
1465                 }
1466         }
1467 }
1468
1469 /* handle an urb that is being unlinked */
1470
1471 static void dl_del_urb (urb_t * urb)
1472 {
1473         wait_queue_head_t * wait_head = ((urb_priv_t *)(urb->hcpriv))->wait;
1474
1475         urb_rm_priv_locked (urb);
1476
1477         if (urb->transfer_flags & USB_ASYNC_UNLINK) {
1478                 urb->status = -ECONNRESET;
1479                 if (urb->complete)
1480                         urb->complete (urb);
1481         } else {
1482                 urb->status = -ENOENT;
1483
1484                 /* unblock sohci_unlink_urb */
1485                 if (wait_head)
1486                         wake_up (wait_head);
1487         }
1488 }
1489
1490 /*-------------------------------------------------------------------------*/
1491
1492 /* replies to the request have to be on a FIFO basis so
1493  * we reverse the reversed done-list */
1494  
1495 static td_t * dl_reverse_done_list (ohci_t * ohci)
1496 {
1497         __u32 td_list_hc;
1498         td_t * td_rev = NULL;
1499         td_t * td_list = NULL;
1500         urb_priv_t * urb_priv = NULL;
1501         unsigned long flags;
1502         
1503         spin_lock_irqsave (&usb_ed_lock, flags);
1504         
1505         td_list_hc = le32_to_cpup (&ohci->hcca->done_head) & 0xfffffff0;
1506         ohci->hcca->done_head = 0;
1507         
1508         while (td_list_hc) {            
1509                 td_list = dma_to_td (ohci, td_list_hc);
1510
1511                 if (TD_CC_GET (le32_to_cpup (&td_list->hwINFO))) {
1512                         urb_priv = (urb_priv_t *) td_list->urb->hcpriv;
1513                         dbg(" USB-error/status: %x : %p", 
1514                                         TD_CC_GET (le32_to_cpup (&td_list->hwINFO)), td_list);
1515                         if (td_list->ed->hwHeadP & cpu_to_le32 (0x1)) {
1516                                 if (urb_priv && ((td_list->index + 1) < urb_priv->length)) {
1517                                         td_list->ed->hwHeadP = 
1518                                                 (urb_priv->td[urb_priv->length - 1]->hwNextTD & cpu_to_le32 (0xfffffff0)) |
1519                                                                         (td_list->ed->hwHeadP & cpu_to_le32 (0x2));
1520                                         urb_priv->td_cnt += urb_priv->length - td_list->index - 1;
1521                                 } else 
1522                                         td_list->ed->hwHeadP &= cpu_to_le32 (0xfffffff2);
1523                         }
1524                 }
1525
1526                 td_list->next_dl_td = td_rev;   
1527                 td_rev = td_list;
1528                 td_list_hc = le32_to_cpup (&td_list->hwNextTD) & 0xfffffff0;    
1529         }       
1530         spin_unlock_irqrestore (&usb_ed_lock, flags);
1531         return td_list;
1532 }
1533
1534 /*-------------------------------------------------------------------------*/
1535
1536 /* there are some pending requests to remove 
1537  * - some of the eds (if ed->state & ED_DEL (set by sohci_free_dev)
1538  * - some URBs/TDs if urb_priv->state == URB_DEL */
1539  
1540 static void dl_del_list (ohci_t  * ohci, unsigned int frame)
1541 {
1542         unsigned long flags;
1543         ed_t * ed;
1544         __u32 edINFO;
1545         __u32 tdINFO;
1546         td_t * td = NULL, * td_next = NULL, * tdHeadP = NULL, * tdTailP;
1547         __u32 * td_p;
1548         int ctrl = 0, bulk = 0;
1549
1550         spin_lock_irqsave (&usb_ed_lock, flags);
1551
1552         for (ed = ohci->ed_rm_list[frame]; ed != NULL; ed = ed->ed_rm_list) {
1553
1554                 tdTailP = dma_to_td (ohci, le32_to_cpup (&ed->hwTailP) & 0xfffffff0);
1555                 tdHeadP = dma_to_td (ohci, le32_to_cpup (&ed->hwHeadP) & 0xfffffff0);
1556                 edINFO = le32_to_cpup (&ed->hwINFO);
1557                 td_p = &ed->hwHeadP;
1558
1559                 for (td = tdHeadP; td != tdTailP; td = td_next) { 
1560                         urb_t * urb = td->urb;
1561                         urb_priv_t * urb_priv = td->urb->hcpriv;
1562                         
1563                         td_next = dma_to_td (ohci, le32_to_cpup (&td->hwNextTD) & 0xfffffff0);
1564                         if ((urb_priv->state == URB_DEL) || (ed->state & ED_DEL)) {
1565                                 tdINFO = le32_to_cpup (&td->hwINFO);
1566                                 if (TD_CC_GET (tdINFO) < 0xE)
1567                                         dl_transfer_length (td);
1568                                 *td_p = td->hwNextTD | (*td_p & cpu_to_le32 (0x3));
1569
1570                                 /* URB is done; clean up */
1571                                 if (++(urb_priv->td_cnt) == urb_priv->length)
1572                                         dl_del_urb (urb);
1573                         } else {
1574                                 td_p = &td->hwNextTD;
1575                         }
1576                 }
1577
1578                 if (ed->state & ED_DEL) { /* set by sohci_free_dev */
1579                         struct ohci_device * dev = usb_to_ohci (ohci->dev[edINFO & 0x7F]);
1580                         td_free (ohci, tdTailP); /* free dummy td */
1581                         ed->hwINFO = cpu_to_le32 (OHCI_ED_SKIP); 
1582                         ed->state = ED_NEW;
1583                         hash_free_ed(ohci, ed);
1584                         /* if all eds are removed wake up sohci_free_dev */
1585                         if (!--dev->ed_cnt) {
1586                                 wait_queue_head_t *wait_head = dev->wait;
1587
1588                                 dev->wait = 0;
1589                                 if (wait_head)
1590                                         wake_up (wait_head);
1591                         }
1592                 } else {
1593                         ed->state &= ~ED_URB_DEL;
1594                         tdHeadP = dma_to_td (ohci, le32_to_cpup (&ed->hwHeadP) & 0xfffffff0);
1595
1596                         if (tdHeadP == tdTailP) {
1597                                 if (ed->state == ED_OPER)
1598                                         ep_unlink(ohci, ed);
1599                                 td_free (ohci, tdTailP);
1600                                 ed->hwINFO = cpu_to_le32 (OHCI_ED_SKIP);
1601                                 ed->state = ED_NEW;
1602                                 hash_free_ed(ohci, ed);
1603                                 --(usb_to_ohci (ohci->dev[edINFO & 0x7F]))->ed_cnt;
1604                         } else
1605                                 ed->hwINFO &= ~cpu_to_le32 (OHCI_ED_SKIP);
1606                 }
1607
1608                 switch (ed->type) {
1609                         case PIPE_CONTROL:
1610                                 ctrl = 1;
1611                                 break;
1612                         case PIPE_BULK:
1613                                 bulk = 1;
1614                                 break;
1615                 }
1616         }
1617         
1618         /* maybe reenable control and bulk lists */ 
1619         if (!ohci->disabled) {
1620                 if (ctrl)       /* reset control list */
1621                         writel (0, &ohci->regs->ed_controlcurrent);
1622                 if (bulk)       /* reset bulk list */
1623                         writel (0, &ohci->regs->ed_bulkcurrent);
1624                 if (!ohci->ed_rm_list[!frame] && !ohci->sleeping) {
1625                         if (ohci->ed_controltail)
1626                                 ohci->hc_control |= OHCI_CTRL_CLE;
1627                         if (ohci->ed_bulktail)
1628                                 ohci->hc_control |= OHCI_CTRL_BLE;
1629                         writel (ohci->hc_control, &ohci->regs->control);   
1630                 }
1631         }
1632
1633         ohci->ed_rm_list[frame] = NULL;
1634         spin_unlock_irqrestore (&usb_ed_lock, flags);
1635 }
1636
1637
1638                 
1639 /*-------------------------------------------------------------------------*/
1640
1641 /* td done list */
1642
1643 static void dl_done_list (ohci_t * ohci, td_t * td_list)
1644 {
1645         td_t * td_list_next = NULL;
1646         ed_t * ed;
1647         int cc = 0;
1648         urb_t * urb;
1649         urb_priv_t * urb_priv;
1650         __u32 tdINFO, edHeadP, edTailP;
1651         
1652         unsigned long flags;
1653         
1654         while (td_list) {
1655                 td_list_next = td_list->next_dl_td;
1656                 
1657                 urb = td_list->urb;
1658                 urb_priv = urb->hcpriv;
1659                 tdINFO = le32_to_cpup (&td_list->hwINFO);
1660                 
1661                 ed = td_list->ed;
1662                 
1663                 dl_transfer_length(td_list);
1664                         
1665                 /* error code of transfer */
1666                 cc = TD_CC_GET (tdINFO);
1667                 if (cc == TD_CC_STALL)
1668                         usb_endpoint_halt(urb->dev,
1669                                 usb_pipeendpoint(urb->pipe),
1670                                 usb_pipeout(urb->pipe));
1671                 
1672                 if (!(urb->transfer_flags & USB_DISABLE_SPD)
1673                                 && (cc == TD_DATAUNDERRUN))
1674                         cc = TD_CC_NOERROR;
1675
1676                 if (++(urb_priv->td_cnt) == urb_priv->length) {
1677                         if ((ed->state & (ED_OPER | ED_UNLINK))
1678                                         && (urb_priv->state != URB_DEL)) {
1679                                 urb->status = cc_to_error[cc];
1680                                 sohci_return_urb (ohci, urb);
1681                         } else {
1682                                 spin_lock_irqsave (&usb_ed_lock, flags);
1683                                 dl_del_urb (urb);
1684                                 spin_unlock_irqrestore (&usb_ed_lock, flags);
1685                         }
1686                 }
1687                 
1688                 spin_lock_irqsave (&usb_ed_lock, flags);
1689                 if (ed->state != ED_NEW) { 
1690                         edHeadP = le32_to_cpup (&ed->hwHeadP) & 0xfffffff0;
1691                         edTailP = le32_to_cpup (&ed->hwTailP);
1692
1693                         /* unlink eds if they are not busy */
1694                         if ((edHeadP == edTailP) && (ed->state == ED_OPER)) 
1695                                 ep_unlink (ohci, ed);
1696                 }       
1697                 spin_unlock_irqrestore (&usb_ed_lock, flags);
1698         
1699                 td_list = td_list_next;
1700         }  
1701 }
1702
1703
1704
1705
1706 /*-------------------------------------------------------------------------*
1707  * Virtual Root Hub 
1708  *-------------------------------------------------------------------------*/
1709  
1710 /* Device descriptor */
1711 static __u8 root_hub_dev_des[] =
1712 {
1713         0x12,       /*  __u8  bLength; */
1714         0x01,       /*  __u8  bDescriptorType; Device */
1715         0x10,       /*  __u16 bcdUSB; v1.1 */
1716         0x01,
1717         0x09,       /*  __u8  bDeviceClass; HUB_CLASSCODE */
1718         0x00,       /*  __u8  bDeviceSubClass; */
1719         0x00,       /*  __u8  bDeviceProtocol; */
1720         0x08,       /*  __u8  bMaxPacketSize0; 8 Bytes */
1721         0x00,       /*  __u16 idVendor; */
1722         0x00,
1723         0x00,       /*  __u16 idProduct; */
1724         0x00,
1725         0x00,       /*  __u16 bcdDevice; */
1726         0x00,
1727         0x00,       /*  __u8  iManufacturer; */
1728         0x02,       /*  __u8  iProduct; */
1729         0x01,       /*  __u8  iSerialNumber; */
1730         0x01        /*  __u8  bNumConfigurations; */
1731 };
1732
1733
1734 /* Configuration descriptor */
1735 static __u8 root_hub_config_des[] =
1736 {
1737         0x09,       /*  __u8  bLength; */
1738         0x02,       /*  __u8  bDescriptorType; Configuration */
1739         0x19,       /*  __u16 wTotalLength; */
1740         0x00,
1741         0x01,       /*  __u8  bNumInterfaces; */
1742         0x01,       /*  __u8  bConfigurationValue; */
1743         0x00,       /*  __u8  iConfiguration; */
1744         0x40,       /*  __u8  bmAttributes; 
1745                  Bit 7: Bus-powered, 6: Self-powered, 5 Remote-wakwup, 4..0: resvd */
1746         0x00,       /*  __u8  MaxPower; */
1747       
1748         /* interface */   
1749         0x09,       /*  __u8  if_bLength; */
1750         0x04,       /*  __u8  if_bDescriptorType; Interface */
1751         0x00,       /*  __u8  if_bInterfaceNumber; */
1752         0x00,       /*  __u8  if_bAlternateSetting; */
1753         0x01,       /*  __u8  if_bNumEndpoints; */
1754         0x09,       /*  __u8  if_bInterfaceClass; HUB_CLASSCODE */
1755         0x00,       /*  __u8  if_bInterfaceSubClass; */
1756         0x00,       /*  __u8  if_bInterfaceProtocol; */
1757         0x00,       /*  __u8  if_iInterface; */
1758      
1759         /* endpoint */
1760         0x07,       /*  __u8  ep_bLength; */
1761         0x05,       /*  __u8  ep_bDescriptorType; Endpoint */
1762         0x81,       /*  __u8  ep_bEndpointAddress; IN Endpoint 1 */
1763         0x03,       /*  __u8  ep_bmAttributes; Interrupt */
1764         0x02,       /*  __u16 ep_wMaxPacketSize; ((MAX_ROOT_PORTS + 1) / 8 */
1765         0x00,
1766         0xff        /*  __u8  ep_bInterval; 255 ms */
1767 };
1768
1769 /* Hub class-specific descriptor is constructed dynamically */
1770
1771
1772 /*-------------------------------------------------------------------------*/
1773
1774 /* prepare Interrupt pipe data; HUB INTERRUPT ENDPOINT */ 
1775  
1776 static int rh_send_irq (ohci_t * ohci, void * rh_data, int rh_len)
1777 {
1778         int num_ports;
1779         int i;
1780         int ret;
1781         int len;
1782
1783         __u8 data[8];
1784
1785         num_ports = roothub_a (ohci) & RH_A_NDP; 
1786         if (num_ports > MAX_ROOT_PORTS) {
1787                 err ("bogus NDP=%d for OHCI usb-%s", num_ports,
1788                         ohci->ohci_dev->slot_name);
1789                 err ("rereads as NDP=%d",
1790                         readl (&ohci->regs->roothub.a) & RH_A_NDP);
1791                 /* retry later; "should not happen" */
1792                 return 0;
1793         }
1794         *(__u8 *) data = (roothub_status (ohci) & (RH_HS_LPSC | RH_HS_OCIC))
1795                 ? 1: 0;
1796         ret = *(__u8 *) data;
1797
1798         for ( i = 0; i < num_ports; i++) {
1799                 *(__u8 *) (data + (i + 1) / 8) |= 
1800                         ((roothub_portstatus (ohci, i) &
1801                                 (RH_PS_CSC | RH_PS_PESC | RH_PS_PSSC | RH_PS_OCIC | RH_PS_PRSC))
1802                             ? 1: 0) << ((i + 1) % 8);
1803                 ret += *(__u8 *) (data + (i + 1) / 8);
1804         }
1805         len = i/8 + 1;
1806   
1807         if (ret > 0) { 
1808                 memcpy(rh_data, data,
1809                        min_t(unsigned int, len,
1810                            min_t(unsigned int, rh_len, sizeof(data))));
1811                 return len;
1812         }
1813         return 0;
1814 }
1815
1816 /*-------------------------------------------------------------------------*/
1817
1818 /* Virtual Root Hub INTs are polled by this timer every "interval" ms */
1819  
1820 static void rh_int_timer_do (unsigned long ptr)
1821 {
1822         int len; 
1823
1824         urb_t * urb = (urb_t *) ptr;
1825         ohci_t * ohci = urb->dev->bus->hcpriv;
1826
1827         if (ohci->disabled)
1828                 return;
1829
1830         /* ignore timers firing during PM suspend, etc */
1831         if ((ohci->hc_control & OHCI_CTRL_HCFS) != OHCI_USB_OPER)
1832                 goto out;
1833
1834         if(ohci->rh.send) { 
1835                 len = rh_send_irq (ohci, urb->transfer_buffer, urb->transfer_buffer_length);
1836                 if (len > 0) {
1837                         urb->actual_length = len;
1838 #ifdef DEBUG
1839                         urb_print (urb, "RET-t(rh)", usb_pipeout (urb->pipe));
1840 #endif
1841                         if (urb->complete)
1842                                 urb->complete (urb);
1843                 }
1844         }
1845  out:
1846         rh_init_int_timer (urb);
1847 }
1848
1849 /*-------------------------------------------------------------------------*/
1850
1851 /* Root Hub INTs are polled by this timer */
1852
1853 static int rh_init_int_timer (urb_t * urb) 
1854 {
1855         ohci_t * ohci = urb->dev->bus->hcpriv;
1856
1857         ohci->rh.interval = urb->interval;
1858         init_timer (&ohci->rh.rh_int_timer);
1859         ohci->rh.rh_int_timer.function = rh_int_timer_do;
1860         ohci->rh.rh_int_timer.data = (unsigned long) urb;
1861         ohci->rh.rh_int_timer.expires = 
1862                         jiffies + (HZ * (urb->interval < 30? 30: urb->interval)) / 1000;
1863         add_timer (&ohci->rh.rh_int_timer);
1864         
1865         return 0;
1866 }
1867
1868 /*-------------------------------------------------------------------------*/
1869
1870 #define OK(x)                   len = (x); break
1871 #define WR_RH_STAT(x)           writel((x), &ohci->regs->roothub.status)
1872 #define WR_RH_PORTSTAT(x)       writel((x), &ohci->regs->roothub.portstatus[wIndex-1])
1873 #define RD_RH_STAT              roothub_status(ohci)
1874 #define RD_RH_PORTSTAT          roothub_portstatus(ohci,wIndex-1)
1875
1876 /* request to virtual root hub */
1877
1878 static int rh_submit_urb (urb_t * urb)
1879 {
1880         struct usb_device * usb_dev = urb->dev;
1881         ohci_t * ohci = usb_dev->bus->hcpriv;
1882         unsigned int pipe = urb->pipe;
1883         devrequest * cmd = (devrequest *) urb->setup_packet;
1884         void * data = urb->transfer_buffer;
1885         int leni = urb->transfer_buffer_length;
1886         int len = 0;
1887         int status = TD_CC_NOERROR;
1888         
1889         __u32 datab[4];
1890         __u8  * data_buf = (__u8 *) datab;
1891         
1892         __u16 bmRType_bReq;
1893         __u16 wValue; 
1894         __u16 wIndex;
1895         __u16 wLength;
1896
1897         if (usb_pipeint(pipe)) {
1898                 ohci->rh.urb =  urb;
1899                 ohci->rh.send = 1;
1900                 ohci->rh.interval = urb->interval;
1901                 rh_init_int_timer(urb);
1902                 urb->status = cc_to_error [TD_CC_NOERROR];
1903                 
1904                 return 0;
1905         }
1906
1907         bmRType_bReq  = cmd->requesttype | (cmd->request << 8);
1908         wValue        = le16_to_cpu (cmd->value);
1909         wIndex        = le16_to_cpu (cmd->index);
1910         wLength       = le16_to_cpu (cmd->length);
1911
1912         switch (bmRType_bReq) {
1913         /* Request Destination:
1914            without flags: Device, 
1915            RH_INTERFACE: interface, 
1916            RH_ENDPOINT: endpoint,
1917            RH_CLASS means HUB here, 
1918            RH_OTHER | RH_CLASS  almost ever means HUB_PORT here 
1919         */
1920   
1921                 case RH_GET_STATUS:                                             
1922                                 *(__u16 *) data_buf = cpu_to_le16 (1); OK (2);
1923                 case RH_GET_STATUS | RH_INTERFACE:                      
1924                                 *(__u16 *) data_buf = cpu_to_le16 (0); OK (2);
1925                 case RH_GET_STATUS | RH_ENDPOINT:                       
1926                                 *(__u16 *) data_buf = cpu_to_le16 (0); OK (2);   
1927                 case RH_GET_STATUS | RH_CLASS:                          
1928                                 *(__u32 *) data_buf = cpu_to_le32 (
1929                                         RD_RH_STAT & ~(RH_HS_CRWE | RH_HS_DRWE));
1930                                 OK (4);
1931                 case RH_GET_STATUS | RH_OTHER | RH_CLASS:       
1932                                 *(__u32 *) data_buf = cpu_to_le32 (RD_RH_PORTSTAT); OK (4);
1933
1934                 case RH_CLEAR_FEATURE | RH_ENDPOINT:  
1935                         switch (wValue) {
1936                                 case (RH_ENDPOINT_STALL): OK (0);
1937                         }
1938                         break;
1939
1940                 case RH_CLEAR_FEATURE | RH_CLASS:
1941                         switch (wValue) {
1942                                 case RH_C_HUB_LOCAL_POWER:
1943                                         OK(0);
1944                                 case (RH_C_HUB_OVER_CURRENT): 
1945                                                 WR_RH_STAT(RH_HS_OCIC); OK (0);
1946                         }
1947                         break;
1948                 
1949                 case RH_CLEAR_FEATURE | RH_OTHER | RH_CLASS:
1950                         switch (wValue) {
1951                                 case (RH_PORT_ENABLE):                  
1952                                                 WR_RH_PORTSTAT (RH_PS_CCS ); OK (0);
1953                                 case (RH_PORT_SUSPEND):                 
1954                                                 WR_RH_PORTSTAT (RH_PS_POCI); OK (0);
1955                                 case (RH_PORT_POWER):                   
1956                                                 WR_RH_PORTSTAT (RH_PS_LSDA); OK (0);
1957                                 case (RH_C_PORT_CONNECTION):    
1958                                                 WR_RH_PORTSTAT (RH_PS_CSC ); OK (0);
1959                                 case (RH_C_PORT_ENABLE):                
1960                                                 WR_RH_PORTSTAT (RH_PS_PESC); OK (0);
1961                                 case (RH_C_PORT_SUSPEND):               
1962                                                 WR_RH_PORTSTAT (RH_PS_PSSC); OK (0);
1963                                 case (RH_C_PORT_OVER_CURRENT):  
1964                                                 WR_RH_PORTSTAT (RH_PS_OCIC); OK (0);
1965                                 case (RH_C_PORT_RESET):                 
1966                                                 WR_RH_PORTSTAT (RH_PS_PRSC); OK (0); 
1967                         }
1968                         break;
1969  
1970                 case RH_SET_FEATURE | RH_OTHER | RH_CLASS:
1971                         switch (wValue) {
1972                                 case (RH_PORT_SUSPEND):                 
1973                                                 WR_RH_PORTSTAT (RH_PS_PSS ); OK (0); 
1974                                 case (RH_PORT_RESET): /* BUG IN HUP CODE *********/
1975                                                 if (RD_RH_PORTSTAT & RH_PS_CCS)
1976                                                     WR_RH_PORTSTAT (RH_PS_PRS);
1977                                                 OK (0);
1978                                 case (RH_PORT_POWER):                   
1979                                                 WR_RH_PORTSTAT (RH_PS_PPS ); OK (0); 
1980                                 case (RH_PORT_ENABLE): /* BUG IN HUP CODE *********/
1981                                                 if (RD_RH_PORTSTAT & RH_PS_CCS)
1982                                                     WR_RH_PORTSTAT (RH_PS_PES );
1983                                                 OK (0);
1984                         }
1985                         break;
1986
1987                 case RH_SET_ADDRESS: ohci->rh.devnum = wValue; OK(0);
1988
1989                 case RH_GET_DESCRIPTOR:
1990                         switch ((wValue & 0xff00) >> 8) {
1991                                 case (0x01): /* device descriptor */
1992                                         len = min_t(unsigned int,
1993                                                   leni,
1994                                                   min_t(unsigned int,
1995                                                       sizeof (root_hub_dev_des),
1996                                                       wLength));
1997                                         data_buf = root_hub_dev_des; OK(len);
1998                                 case (0x02): /* configuration descriptor */
1999                                         len = min_t(unsigned int,
2000                                                   leni,
2001                                                   min_t(unsigned int,
2002                                                       sizeof (root_hub_config_des),
2003                                                       wLength));
2004                                         data_buf = root_hub_config_des; OK(len);
2005                                 case (0x03): /* string descriptors */
2006                                         len = usb_root_hub_string (wValue & 0xff,
2007                                                 (int)(long) ohci->regs, "OHCI",
2008                                                 data, wLength);
2009                                         if (len > 0) {
2010                                                 data_buf = data;
2011                                                 OK(min_t(int, leni, len));
2012                                         }
2013                                         // else fallthrough
2014                                 default: 
2015                                         status = TD_CC_STALL;
2016                         }
2017                         break;
2018                 
2019                 case RH_GET_DESCRIPTOR | RH_CLASS:
2020                     {
2021                             __u32 temp = roothub_a (ohci);
2022
2023                             data_buf [0] = 9;           // min length;
2024                             data_buf [1] = 0x29;
2025                             data_buf [2] = temp & RH_A_NDP;
2026                             data_buf [3] = 0;
2027                             if (temp & RH_A_PSM)        /* per-port power switching? */
2028                                 data_buf [3] |= 0x1;
2029                             if (temp & RH_A_NOCP)       /* no overcurrent reporting? */
2030                                 data_buf [3] |= 0x10;
2031                             else if (temp & RH_A_OCPM)  /* per-port overcurrent reporting? */
2032                                 data_buf [3] |= 0x8;
2033
2034                             datab [1] = 0;
2035                             data_buf [5] = (temp & RH_A_POTPGT) >> 24;
2036                             temp = roothub_b (ohci);
2037                             data_buf [7] = temp & RH_B_DR;
2038                             if (data_buf [2] < 7) {
2039                                 data_buf [8] = 0xff;
2040                             } else {
2041                                 data_buf [0] += 2;
2042                                 data_buf [8] = (temp & RH_B_DR) >> 8;
2043                                 data_buf [10] = data_buf [9] = 0xff;
2044                             }
2045                                 
2046                             len = min_t(unsigned int, leni,
2047                                       min_t(unsigned int, data_buf [0], wLength));
2048                             OK (len);
2049                         }
2050  
2051                 case RH_GET_CONFIGURATION:      *(__u8 *) data_buf = 0x01; OK (1);
2052
2053                 case RH_SET_CONFIGURATION:      WR_RH_STAT (0x10000); OK (0);
2054
2055                 default: 
2056                         dbg ("unsupported root hub command");
2057                         status = TD_CC_STALL;
2058         }
2059         
2060 #ifdef  DEBUG
2061         // ohci_dump_roothub (ohci, 0);
2062 #endif
2063
2064         len = min_t(int, len, leni);
2065         if (data != data_buf)
2066             memcpy (data, data_buf, len);
2067         urb->actual_length = len;
2068         urb->status = cc_to_error [status];
2069         
2070 #ifdef DEBUG
2071         urb_print (urb, "RET(rh)", usb_pipeout (urb->pipe));
2072 #endif
2073
2074         urb->hcpriv = NULL;
2075         usb_dec_dev_use (usb_dev);
2076         urb->dev = NULL;
2077         if (urb->complete)
2078                 urb->complete (urb);
2079         return 0;
2080 }
2081
2082 /*-------------------------------------------------------------------------*/
2083
2084 static int rh_unlink_urb (urb_t * urb)
2085 {
2086         ohci_t * ohci = urb->dev->bus->hcpriv;
2087  
2088         if (ohci->rh.urb == urb) {
2089                 ohci->rh.send = 0;
2090                 del_timer (&ohci->rh.rh_int_timer);
2091                 ohci->rh.urb = NULL;
2092
2093                 urb->hcpriv = NULL;
2094                 usb_dec_dev_use(urb->dev);
2095                 urb->dev = NULL;
2096                 if (urb->transfer_flags & USB_ASYNC_UNLINK) {
2097                         urb->status = -ECONNRESET;
2098                         if (urb->complete)
2099                                 urb->complete (urb);
2100                 } else
2101                         urb->status = -ENOENT;
2102         }
2103         return 0;
2104 }
2105  
2106 /*-------------------------------------------------------------------------*
2107  * HC functions
2108  *-------------------------------------------------------------------------*/
2109
2110 /* reset the HC and BUS */
2111
2112 static int hc_reset (ohci_t * ohci)
2113 {
2114         int timeout = 30;
2115         int smm_timeout = 50; /* 0,5 sec */
2116                 
2117         if (readl (&ohci->regs->control) & OHCI_CTRL_IR) { /* SMM owns the HC */
2118                 writel (OHCI_OCR, &ohci->regs->cmdstatus); /* request ownership */
2119                 dbg("USB HC TakeOver from SMM");
2120                 while (readl (&ohci->regs->control) & OHCI_CTRL_IR) {
2121                         wait_ms (10);
2122                         if (--smm_timeout == 0) {
2123                                 err("USB HC TakeOver failed!");
2124                                 return -1;
2125                         }
2126                 }
2127         }       
2128                 
2129         /* Disable HC interrupts */
2130         writel (OHCI_INTR_MIE, &ohci->regs->intrdisable);
2131
2132         dbg("USB HC reset_hc usb-%s: ctrl = 0x%x ;",
2133                 ohci->ohci_dev->slot_name,
2134                 readl (&ohci->regs->control));
2135
2136         /* Reset USB (needed by some controllers) */
2137         writel (0, &ohci->regs->control);
2138         
2139         /* HC Reset requires max 10 ms delay */
2140         writel (OHCI_HCR,  &ohci->regs->cmdstatus);
2141         while ((readl (&ohci->regs->cmdstatus) & OHCI_HCR) != 0) {
2142                 if (--timeout == 0) {
2143                         err("USB HC reset timed out!");
2144                         return -1;
2145                 }       
2146                 udelay (1);
2147         }        
2148         return 0;
2149 }
2150
2151 /*-------------------------------------------------------------------------*/
2152
2153 /* Start an OHCI controller, set the BUS operational
2154  * enable interrupts 
2155  * connect the virtual root hub */
2156
2157 static int hc_start (ohci_t * ohci)
2158 {
2159         __u32 mask;
2160         unsigned int fminterval;
2161         struct usb_device  * usb_dev;
2162         struct ohci_device * dev;
2163         
2164         ohci->disabled = 1;
2165
2166         /* Tell the controller where the control and bulk lists are
2167          * The lists are empty now. */
2168          
2169         writel (0, &ohci->regs->ed_controlhead);
2170         writel (0, &ohci->regs->ed_bulkhead);
2171         
2172         writel (ohci->hcca_dma, &ohci->regs->hcca); /* a reset clears this */
2173    
2174         fminterval = 0x2edf;
2175         writel ((fminterval * 9) / 10, &ohci->regs->periodicstart);
2176         fminterval |= ((((fminterval - 210) * 6) / 7) << 16); 
2177         writel (fminterval, &ohci->regs->fminterval);   
2178         writel (0x628, &ohci->regs->lsthresh);
2179
2180         /* start controller operations */
2181         ohci->hc_control = OHCI_CONTROL_INIT | OHCI_USB_OPER;
2182         ohci->disabled = 0;
2183         writel (ohci->hc_control, &ohci->regs->control);
2184  
2185         /* Choose the interrupts we care about now, others later on demand */
2186         mask = OHCI_INTR_MIE | OHCI_INTR_UE | OHCI_INTR_WDH | OHCI_INTR_SO;
2187         writel (mask, &ohci->regs->intrenable);
2188         writel (mask, &ohci->regs->intrstatus);
2189
2190 #ifdef  OHCI_USE_NPS
2191         /* required for AMD-756 and some Mac platforms */
2192         writel ((roothub_a (ohci) | RH_A_NPS) & ~RH_A_PSM,
2193                 &ohci->regs->roothub.a);
2194         writel (RH_HS_LPSC, &ohci->regs->roothub.status);
2195 #endif  /* OHCI_USE_NPS */
2196
2197         // POTPGT delay is bits 24-31, in 2 ms units.
2198         mdelay ((roothub_a (ohci) >> 23) & 0x1fe);
2199  
2200         /* connect the virtual root hub */
2201         ohci->rh.devnum = 0;
2202         usb_dev = usb_alloc_dev (NULL, ohci->bus);
2203         if (!usb_dev) {
2204             ohci->disabled = 1;
2205             return -ENOMEM;
2206         }
2207
2208         dev = usb_to_ohci (usb_dev);
2209         ohci->bus->root_hub = usb_dev;
2210         usb_connect (usb_dev);
2211         if (usb_new_device (usb_dev) != 0) {
2212                 usb_free_dev (usb_dev); 
2213                 ohci->disabled = 1;
2214                 return -ENODEV;
2215         }
2216         
2217         return 0;
2218 }
2219
2220 /*-------------------------------------------------------------------------*/
2221
2222 /* called only from interrupt handler */
2223
2224 static void check_timeouts (struct ohci *ohci)
2225 {
2226         spin_lock (&usb_ed_lock);
2227         while (!list_empty (&ohci->timeout_list)) {
2228                 struct urb      *urb;
2229
2230                 urb = list_entry (ohci->timeout_list.next, struct urb, urb_list);
2231                 if (time_after (jiffies, urb->timeout))
2232                         break;
2233
2234                 list_del_init (&urb->urb_list);
2235                 if (urb->status != -EINPROGRESS)
2236                         continue;
2237
2238                 urb->transfer_flags |= USB_TIMEOUT_KILLED | USB_ASYNC_UNLINK;
2239                 spin_unlock (&usb_ed_lock);
2240
2241                 // outside the interrupt handler (in a timer...)
2242                 // this reference would race interrupts
2243                 sohci_unlink_urb (urb);
2244
2245                 spin_lock (&usb_ed_lock);
2246         }
2247         spin_unlock (&usb_ed_lock);
2248 }
2249
2250
2251 /*-------------------------------------------------------------------------*/
2252
2253 /* an interrupt happens */
2254
2255 static void hc_interrupt (int irq, void * __ohci, struct pt_regs * r)
2256 {
2257         ohci_t * ohci = __ohci;
2258         struct ohci_regs * regs = ohci->regs;
2259         int ints; 
2260
2261         if ((ohci->hcca->done_head != 0) && !(le32_to_cpup (&ohci->hcca->done_head) & 0x01)) {
2262                 ints =  OHCI_INTR_WDH;
2263         } else if ((ints = (readl (&regs->intrstatus) & readl (&regs->intrenable))) == 0) {
2264                 return;
2265         } 
2266
2267         // dbg("Interrupt: %x frame: %x", ints, le16_to_cpu (ohci->hcca->frame_no));
2268
2269         if (ints & OHCI_INTR_UE) {
2270                 ohci->disabled++;
2271                 err ("OHCI Unrecoverable Error, controller usb-%s disabled",
2272                         ohci->ohci_dev->slot_name);
2273                 // e.g. due to PCI Master/Target Abort
2274
2275 #ifdef  DEBUG
2276                 ohci_dump (ohci, 1);
2277 #else
2278                 // FIXME: be optimistic, hope that bug won't repeat often.
2279                 // Make some non-interrupt context restart the controller.
2280                 // Count and limit the retries though; either hardware or
2281                 // software errors can go forever...
2282 #endif
2283                 hc_reset (ohci);
2284         }
2285   
2286         if (ints & OHCI_INTR_WDH) {
2287                 writel (OHCI_INTR_WDH, &regs->intrdisable);     
2288                 dl_done_list (ohci, dl_reverse_done_list (ohci));
2289                 writel (OHCI_INTR_WDH, &regs->intrenable); 
2290         }
2291   
2292         if (ints & OHCI_INTR_SO) {
2293                 dbg("USB Schedule overrun");
2294                 writel (OHCI_INTR_SO, &regs->intrenable);        
2295         }
2296
2297         // FIXME:  this assumes SOF (1/ms) interrupts don't get lost...
2298         if (ints & OHCI_INTR_SF) { 
2299                 unsigned int frame = le16_to_cpu (ohci->hcca->frame_no) & 1;
2300                 writel (OHCI_INTR_SF, &regs->intrdisable);      
2301                 if (ohci->ed_rm_list[!frame] != NULL) {
2302                         dl_del_list (ohci, !frame);
2303                 }
2304                 if (ohci->ed_rm_list[frame] != NULL)
2305                         writel (OHCI_INTR_SF, &regs->intrenable);       
2306         }
2307
2308         if (!list_empty (&ohci->timeout_list)) {
2309                 check_timeouts (ohci);
2310 // FIXME:  enable SF as needed in a timer;
2311 // don't make lots of 1ms interrupts
2312 // On unloaded USB, think 4k ~= 4-5msec
2313                 if (!list_empty (&ohci->timeout_list))
2314                         writel (OHCI_INTR_SF, &regs->intrenable);       
2315         }
2316
2317         writel (ints, &regs->intrstatus);
2318         writel (OHCI_INTR_MIE, &regs->intrenable);      
2319 }
2320
2321 /*-------------------------------------------------------------------------*/
2322
2323 /* allocate OHCI */
2324
2325 static ohci_t * __devinit hc_alloc_ohci (struct pci_dev *dev, void * mem_base)
2326 {
2327         ohci_t * ohci;
2328         struct usb_bus * bus;
2329
2330         ohci = (ohci_t *) kmalloc (sizeof *ohci, GFP_KERNEL);
2331         if (!ohci)
2332                 return NULL;
2333                 
2334         memset (ohci, 0, sizeof (ohci_t));
2335
2336         ohci->hcca = pci_alloc_consistent (dev, sizeof *ohci->hcca,
2337                         &ohci->hcca_dma);
2338         if (!ohci->hcca) {
2339                 kfree (ohci);
2340                 return NULL;
2341         }
2342         memset (ohci->hcca, 0, sizeof (struct ohci_hcca));
2343
2344         ohci->disabled = 1;
2345         ohci->sleeping = 0;
2346         ohci->irq = -1;
2347         ohci->regs = mem_base;   
2348
2349         ohci->ohci_dev = dev;
2350         dev->driver_data = ohci;
2351  
2352         INIT_LIST_HEAD (&ohci->ohci_hcd_list);
2353         list_add (&ohci->ohci_hcd_list, &ohci_hcd_list);
2354
2355         INIT_LIST_HEAD (&ohci->timeout_list);
2356
2357         bus = usb_alloc_bus (&sohci_device_operations);
2358         if (!bus) {
2359                 kfree (ohci);
2360                 return NULL;
2361         }
2362
2363         ohci->bus = bus;
2364         bus->hcpriv = (void *) ohci;
2365
2366         return ohci;
2367
2368
2369
2370 /*-------------------------------------------------------------------------*/
2371
2372 /* De-allocate all resources.. */
2373
2374 static void hc_release_ohci (ohci_t * ohci)
2375 {       
2376         dbg ("USB HC release ohci usb-%s", ohci->ohci_dev->slot_name);
2377
2378         /* disconnect all devices */    
2379         if (ohci->bus->root_hub)
2380                 usb_disconnect (&ohci->bus->root_hub);
2381
2382         if (!ohci->disabled)
2383                 hc_reset (ohci);
2384         
2385         if (ohci->irq >= 0) {
2386                 free_irq (ohci->irq, ohci);
2387                 ohci->irq = -1;
2388         }
2389         ohci->ohci_dev->driver_data = 0;
2390
2391         usb_deregister_bus (ohci->bus);
2392         usb_free_bus (ohci->bus);
2393
2394         list_del (&ohci->ohci_hcd_list);
2395         INIT_LIST_HEAD (&ohci->ohci_hcd_list);
2396
2397         ohci_mem_cleanup (ohci);
2398     
2399         /* unmap the IO address space */
2400         iounmap (ohci->regs);
2401
2402         pci_free_consistent (ohci->ohci_dev, sizeof *ohci->hcca,
2403                 ohci->hcca, ohci->hcca_dma);
2404         kfree (ohci);
2405 }
2406
2407 /*-------------------------------------------------------------------------*/
2408
2409 /* Increment the module usage count, start the control thread and
2410  * return success. */
2411
2412 static struct pci_driver ohci_pci_driver;
2413  
2414 static int __devinit
2415 hc_found_ohci (struct pci_dev *dev, int irq,
2416         void *mem_base, const struct pci_device_id *id)
2417 {
2418         ohci_t * ohci;
2419         u8 latency, limit;
2420         char buf[8], *bufp = buf;
2421         int ret;
2422
2423 #ifndef __sparc__
2424         sprintf(buf, "%d", irq);
2425 #else
2426         bufp = __irq_itoa(irq);
2427 #endif
2428         printk(KERN_INFO __FILE__ ": USB OHCI at membase 0x%lx, IRQ %s\n",
2429                 (unsigned long) mem_base, bufp);
2430         printk(KERN_INFO __FILE__ ": usb-%s, %s\n", dev->slot_name, dev->name);
2431     
2432         ohci = hc_alloc_ohci (dev, mem_base);
2433         if (!ohci) {
2434                 return -ENOMEM;
2435         }
2436         if ((ret = ohci_mem_init (ohci)) < 0) {
2437                 hc_release_ohci (ohci);
2438                 return ret;
2439         }
2440         ohci->flags = id->driver_data;
2441         if (ohci->flags & OHCI_QUIRK_AMD756)
2442                 printk (KERN_INFO __FILE__ ": AMD756 erratum 4 workaround\n");
2443
2444         /* bad pci latencies can contribute to overruns */ 
2445         pci_read_config_byte (dev, PCI_LATENCY_TIMER, &latency);
2446         if (latency) {
2447                 pci_read_config_byte (dev, PCI_MAX_LAT, &limit);
2448                 if (limit && limit < latency) {
2449                         dbg ("PCI latency reduced to max %d", limit);
2450                         pci_write_config_byte (dev, PCI_LATENCY_TIMER, limit);
2451                         ohci->pci_latency = limit;
2452                 } else {
2453                         /* it might already have been reduced */
2454                         ohci->pci_latency = latency;
2455                 }
2456         }
2457
2458         if (hc_reset (ohci) < 0) {
2459                 hc_release_ohci (ohci);
2460                 return -ENODEV;
2461         }
2462
2463         /* FIXME this is a second HC reset; why?? */
2464         writel (ohci->hc_control = OHCI_USB_RESET, &ohci->regs->control);
2465         wait_ms (10);
2466
2467         usb_register_bus (ohci->bus);
2468         
2469         if (request_irq (irq, hc_interrupt, SA_SHIRQ,
2470                         ohci_pci_driver.name, ohci) != 0) {
2471                 err ("request interrupt %s failed", bufp);
2472                 hc_release_ohci (ohci);
2473                 return -EBUSY;
2474         }
2475         ohci->irq = irq;     
2476
2477         if (hc_start (ohci) < 0) {
2478                 err ("can't start usb-%s", dev->slot_name);
2479                 hc_release_ohci (ohci);
2480                 return -EBUSY;
2481         }
2482
2483 #ifdef  DEBUG
2484         ohci_dump (ohci, 1);
2485 #endif
2486         return 0;
2487 }
2488
2489 /*-------------------------------------------------------------------------*/
2490
2491 #ifdef  CONFIG_PM
2492
2493 /* controller died; cleanup debris, then restart */
2494 /* must not be called from interrupt context */
2495
2496 static void hc_restart (ohci_t *ohci)
2497 {
2498         int temp;
2499         int i;
2500
2501         if (ohci->pci_latency)
2502                 pci_write_config_byte (ohci->ohci_dev, PCI_LATENCY_TIMER, ohci->pci_latency);
2503
2504         ohci->disabled = 1;
2505         ohci->sleeping = 0;
2506         if (ohci->bus->root_hub)
2507                 usb_disconnect (&ohci->bus->root_hub);
2508         
2509         /* empty the interrupt branches */
2510         for (i = 0; i < NUM_INTS; i++) ohci->ohci_int_load[i] = 0;
2511         for (i = 0; i < NUM_INTS; i++) ohci->hcca->int_table[i] = 0;
2512         
2513         /* no EDs to remove */
2514         ohci->ed_rm_list [0] = NULL;
2515         ohci->ed_rm_list [1] = NULL;
2516
2517         /* empty control and bulk lists */       
2518         ohci->ed_isotail     = NULL;
2519         ohci->ed_controltail = NULL;
2520         ohci->ed_bulktail    = NULL;
2521
2522         if ((temp = hc_reset (ohci)) < 0 || (temp = hc_start (ohci)) < 0) {
2523                 err ("can't restart usb-%s, %d", ohci->ohci_dev->slot_name, temp);
2524         } else
2525                 dbg ("restart usb-%s completed", ohci->ohci_dev->slot_name);
2526 }
2527
2528 #endif  /* CONFIG_PM */
2529
2530 /*-------------------------------------------------------------------------*/
2531
2532 /* configured so that an OHCI device is always provided */
2533 /* always called with process context; sleeping is OK */
2534
2535 static int __devinit
2536 ohci_pci_probe (struct pci_dev *dev, const struct pci_device_id *id)
2537 {
2538         unsigned long mem_resource, mem_len;
2539         void *mem_base;
2540
2541         if (pci_enable_device(dev) < 0)
2542                 return -ENODEV;
2543
2544         if (!dev->irq) {
2545                 err("found OHCI device with no IRQ assigned. check BIOS settings!");
2546                 return -ENODEV;
2547         }
2548         
2549         /* we read its hardware registers as memory */
2550         mem_resource = pci_resource_start(dev, 0);
2551         mem_len = pci_resource_len(dev, 0);
2552         if (!request_mem_region (mem_resource, mem_len, ohci_pci_driver.name)) {
2553                 dbg ("controller already in use");
2554                 return -EBUSY;
2555         }
2556
2557         mem_base = ioremap_nocache (mem_resource, mem_len);
2558         if (!mem_base) {
2559                 err("Error mapping OHCI memory");
2560                 return -EFAULT;
2561         }
2562
2563         /* controller writes into our memory */
2564         pci_set_master (dev);
2565
2566         return hc_found_ohci (dev, dev->irq, mem_base, id);
2567
2568
2569 /*-------------------------------------------------------------------------*/
2570
2571 /* may be called from interrupt context [interface spec] */
2572 /* may be called without controller present */
2573 /* may be called with controller, bus, and devices active */
2574
2575 static void __devexit
2576 ohci_pci_remove (struct pci_dev *dev)
2577 {
2578         ohci_t          *ohci = (ohci_t *) dev->driver_data;
2579
2580         dbg ("remove %s controller usb-%s%s%s",
2581                 hcfs2string (ohci->hc_control & OHCI_CTRL_HCFS),
2582                 dev->slot_name,
2583                 ohci->disabled ? " (disabled)" : "",
2584                 in_interrupt () ? " in interrupt" : ""
2585                 );
2586 #ifdef  DEBUG
2587         ohci_dump (ohci, 1);
2588 #endif
2589
2590         /* don't wake up sleeping controllers, or block in interrupt context */
2591         if ((ohci->hc_control & OHCI_CTRL_HCFS) != OHCI_USB_OPER || in_interrupt ()) {
2592                 dbg ("controller being disabled");
2593                 ohci->disabled = 1;
2594         }
2595
2596         /* on return, USB will always be reset (if present) */
2597         if (ohci->disabled)
2598                 writel (ohci->hc_control = OHCI_USB_RESET,
2599                         &ohci->regs->control);
2600
2601         hc_release_ohci (ohci);
2602