[PATCH] USB UHCI: Add root hub states
[safe/jmp/linux-2.6] / drivers / usb / host / uhci-hcd.c
1 /*
2  * Universal Host Controller Interface driver for USB.
3  *
4  * Maintainer: Alan Stern <stern@rowland.harvard.edu>
5  *
6  * (C) Copyright 1999 Linus Torvalds
7  * (C) Copyright 1999-2002 Johannes Erdfelt, johannes@erdfelt.com
8  * (C) Copyright 1999 Randy Dunlap
9  * (C) Copyright 1999 Georg Acher, acher@in.tum.de
10  * (C) Copyright 1999 Deti Fliegl, deti@fliegl.de
11  * (C) Copyright 1999 Thomas Sailer, sailer@ife.ee.ethz.ch
12  * (C) Copyright 1999 Roman Weissgaerber, weissg@vienna.at
13  * (C) Copyright 2000 Yggdrasil Computing, Inc. (port of new PCI interface
14  *               support from usb-ohci.c by Adam Richter, adam@yggdrasil.com).
15  * (C) Copyright 1999 Gregory P. Smith (from usb-ohci.c)
16  * (C) Copyright 2004 Alan Stern, stern@rowland.harvard.edu
17  *
18  * Intel documents this fairly well, and as far as I know there
19  * are no royalties or anything like that, but even so there are
20  * people who decided that they want to do the same thing in a
21  * completely different way.
22  *
23  * WARNING! The USB documentation is downright evil. Most of it
24  * is just crap, written by a committee. You're better off ignoring
25  * most of it, the important stuff is:
26  *  - the low-level protocol (fairly simple but lots of small details)
27  *  - working around the horridness of the rest
28  */
29
30 #include <linux/config.h>
31 #ifdef CONFIG_USB_DEBUG
32 #define DEBUG
33 #else
34 #undef DEBUG
35 #endif
36 #include <linux/module.h>
37 #include <linux/pci.h>
38 #include <linux/kernel.h>
39 #include <linux/init.h>
40 #include <linux/delay.h>
41 #include <linux/ioport.h>
42 #include <linux/sched.h>
43 #include <linux/slab.h>
44 #include <linux/smp_lock.h>
45 #include <linux/errno.h>
46 #include <linux/unistd.h>
47 #include <linux/interrupt.h>
48 #include <linux/spinlock.h>
49 #include <linux/debugfs.h>
50 #include <linux/pm.h>
51 #include <linux/dmapool.h>
52 #include <linux/dma-mapping.h>
53 #include <linux/usb.h>
54 #include <linux/bitops.h>
55
56 #include <asm/uaccess.h>
57 #include <asm/io.h>
58 #include <asm/irq.h>
59 #include <asm/system.h>
60
61 #include "../core/hcd.h"
62 #include "uhci-hcd.h"
63
64 /*
65  * Version Information
66  */
67 #define DRIVER_VERSION "v2.3"
68 #define DRIVER_AUTHOR "Linus 'Frodo Rabbit' Torvalds, Johannes Erdfelt, \
69 Randy Dunlap, Georg Acher, Deti Fliegl, Thomas Sailer, Roman Weissgaerber, \
70 Alan Stern"
71 #define DRIVER_DESC "USB Universal Host Controller Interface driver"
72
73 /*
74  * debug = 0, no debugging messages
75  * debug = 1, dump failed URB's except for stalls
76  * debug = 2, dump all failed URB's (including stalls)
77  *            show all queues in /debug/uhci/[pci_addr]
78  * debug = 3, show all TD's in URB's when dumping
79  */
80 #ifdef DEBUG
81 static int debug = 1;
82 #else
83 static int debug = 0;
84 #endif
85 module_param(debug, int, S_IRUGO | S_IWUSR);
86 MODULE_PARM_DESC(debug, "Debug level");
87 static char *errbuf;
88 #define ERRBUF_LEN    (32 * 1024)
89
90 static kmem_cache_t *uhci_up_cachep;    /* urb_priv */
91
92 static void uhci_get_current_frame_number(struct uhci_hcd *uhci);
93
94 /* If a transfer is still active after this much time, turn off FSBR */
95 #define IDLE_TIMEOUT    msecs_to_jiffies(50)
96 #define FSBR_DELAY      msecs_to_jiffies(50)
97
98 /* When we timeout an idle transfer for FSBR, we'll switch it over to */
99 /* depth first traversal. We'll do it in groups of this number of TD's */
100 /* to make sure it doesn't hog all of the bandwidth */
101 #define DEPTH_INTERVAL 5
102
103 static inline void restart_timer(struct uhci_hcd *uhci)
104 {
105         mod_timer(&uhci->stall_timer, jiffies + msecs_to_jiffies(100));
106 }
107
108 #include "uhci-hub.c"
109 #include "uhci-debug.c"
110 #include "uhci-q.c"
111
112 static void reset_hc(struct uhci_hcd *uhci)
113 {
114         unsigned long io_addr = uhci->io_addr;
115
116         /* Turn off PIRQ, SMI, and all interrupts.  This also turns off
117          * the BIOS's USB Legacy Support.
118          */
119         pci_write_config_word(to_pci_dev(uhci_dev(uhci)), USBLEGSUP, 0);
120         outw(0, uhci->io_addr + USBINTR);
121
122         /* Global reset for 50ms */
123         outw(USBCMD_GRESET, io_addr + USBCMD);
124         msleep(50);
125         outw(0, io_addr + USBCMD);
126
127         /* Another 10ms delay */
128         msleep(10);
129         uhci->resume_detect = 0;
130         uhci->is_stopped = UHCI_IS_STOPPED;
131         uhci->rh_state = UHCI_RH_RESET;
132 }
133
134 static int resume_detect_interrupts_are_broken(struct uhci_hcd *uhci)
135 {
136         int port;
137
138         switch (to_pci_dev(uhci_dev(uhci))->vendor) {
139             default:
140                 break;
141
142             case PCI_VENDOR_ID_GENESYS:
143                 /* Genesys Logic's GL880S controllers don't generate
144                  * resume-detect interrupts.
145                  */
146                 return 1;
147
148             case PCI_VENDOR_ID_INTEL:
149                 /* Some of Intel's USB controllers have a bug that causes
150                  * resume-detect interrupts if any port has an over-current
151                  * condition.  To make matters worse, some motherboards
152                  * hardwire unused USB ports' over-current inputs active!
153                  * To prevent problems, we will not enable resume-detect
154                  * interrupts if any ports are OC.
155                  */
156                 for (port = 0; port < uhci->rh_numports; ++port) {
157                         if (inw(uhci->io_addr + USBPORTSC1 + port * 2) &
158                                         USBPORTSC_OC)
159                                 return 1;
160                 }
161                 break;
162         }
163         return 0;
164 }
165
166 static void suspend_hc(struct uhci_hcd *uhci, enum uhci_rh_state new_state)
167 __releases(uhci->lock)
168 __acquires(uhci->lock)
169 {
170         int auto_stop;
171         int int_enable;
172
173         auto_stop = (new_state == UHCI_RH_AUTO_STOPPED);
174         dev_dbg(uhci_dev(uhci), "%s%s\n", __FUNCTION__,
175                         (auto_stop ? " (auto-stop)" : ""));
176
177         /* If we get a suspend request when we're already auto-stopped
178          * then there's nothing to do.
179          */
180         if (uhci->rh_state == UHCI_RH_AUTO_STOPPED) {
181                 uhci->rh_state = new_state;
182                 return;
183         }
184
185         /* Enable resume-detect interrupts if they work.
186          * Then enter Global Suspend mode, still configured.
187          */
188         int_enable = (resume_detect_interrupts_are_broken(uhci) ?
189                         0 : USBINTR_RESUME);
190         outw(int_enable, uhci->io_addr + USBINTR);
191         outw(USBCMD_EGSM | USBCMD_CF, uhci->io_addr + USBCMD);
192         udelay(5);
193
194         /* If we're auto-stopping then no devices have been attached
195          * for a while, so there shouldn't be any active URBs and the
196          * controller should stop after a few microseconds.  Otherwise
197          * we will give the controller one frame to stop.
198          */
199         if (!auto_stop && !(inw(uhci->io_addr + USBSTS) & USBSTS_HCH)) {
200                 uhci->rh_state = UHCI_RH_SUSPENDING;
201                 spin_unlock_irq(&uhci->lock);
202                 msleep(1);
203                 spin_lock_irq(&uhci->lock);
204         }
205         if (!(inw(uhci->io_addr + USBSTS) & USBSTS_HCH))
206                 dev_warn(uhci_dev(uhci), "Controller not stopped yet!\n");
207
208         uhci_get_current_frame_number(uhci);
209         smp_wmb();
210
211         uhci->rh_state = new_state;
212         uhci->is_stopped = UHCI_IS_STOPPED;
213         uhci->resume_detect = 0;
214
215         uhci_scan_schedule(uhci, NULL);
216 }
217
218 static void wakeup_hc(struct uhci_hcd *uhci)
219 __releases(uhci->lock)
220 __acquires(uhci->lock)
221 {
222         dev_dbg(uhci_dev(uhci), "%s%s\n", __FUNCTION__,
223                         uhci->rh_state == UHCI_RH_AUTO_STOPPED ?
224                                 " (auto-start)" : "");
225
226         /* If we are auto-stopped then no devices are attached so there's
227          * no need for wakeup signals.  Otherwise we send Global Resume
228          * for 20 ms.
229          */
230         if (uhci->rh_state == UHCI_RH_SUSPENDED) {
231                 uhci->rh_state = UHCI_RH_RESUMING;
232                 outw(USBCMD_FGR | USBCMD_EGSM | USBCMD_CF,
233                                 uhci->io_addr + USBCMD);
234                 spin_unlock_irq(&uhci->lock);
235                 msleep(20);
236                 spin_lock_irq(&uhci->lock);
237
238                 /* End Global Resume and wait for EOP to be sent */
239                 outw(USBCMD_CF, uhci->io_addr + USBCMD);
240                 udelay(4);
241                 if (inw(uhci->io_addr + USBCMD) & USBCMD_FGR)
242                         dev_warn(uhci_dev(uhci), "FGR not stopped yet!\n");
243         }
244
245         uhci->rh_state = UHCI_RH_RUNNING;
246         uhci->is_stopped = 0;
247         smp_wmb();
248
249         /* Mark it configured and running with a 64-byte max packet.
250          * All interrupts are enabled, even though RD won't do anything.
251          */
252         outw(USBCMD_RS | USBCMD_CF | USBCMD_MAXP, uhci->io_addr + USBCMD);
253         outw(USBINTR_TIMEOUT | USBINTR_RESUME | USBINTR_IOC | USBINTR_SP,
254                         uhci->io_addr + USBINTR);
255 }
256
257 static int start_hc(struct uhci_hcd *uhci)
258 {
259         unsigned long io_addr = uhci->io_addr;
260         int timeout = 10;
261
262         /*
263          * Reset the HC - this will force us to get a
264          * new notification of any already connected
265          * ports due to the virtual disconnect that it
266          * implies.
267          */
268         outw(USBCMD_HCRESET, io_addr + USBCMD);
269         while (inw(io_addr + USBCMD) & USBCMD_HCRESET) {
270                 if (--timeout < 0) {
271                         dev_err(uhci_dev(uhci), "USBCMD_HCRESET timed out!\n");
272                         return -ETIMEDOUT;
273                 }
274                 msleep(1);
275         }
276
277         /* Mark controller as running before we enable interrupts */
278         uhci_to_hcd(uhci)->state = HC_STATE_RUNNING;
279
280         /* Turn on PIRQ and all interrupts */
281         pci_write_config_word(to_pci_dev(uhci_dev(uhci)), USBLEGSUP,
282                         USBLEGSUP_DEFAULT);
283         outw(USBINTR_TIMEOUT | USBINTR_RESUME | USBINTR_IOC | USBINTR_SP,
284                 io_addr + USBINTR);
285
286         /* Start at frame 0 */
287         outw(0, io_addr + USBFRNUM);
288         outl(uhci->fl->dma_handle, io_addr + USBFLBASEADD);
289
290         /* Run and mark it configured with a 64-byte max packet */
291         uhci->rh_state = UHCI_RH_RUNNING;
292         outw(USBCMD_RS | USBCMD_CF | USBCMD_MAXP, io_addr + USBCMD);
293         uhci->is_stopped = 0;
294
295         return 0;
296 }
297
298 static void rh_state_transitions(struct uhci_hcd *uhci)
299 {
300         switch (uhci->rh_state) {
301             case UHCI_RH_RUNNING:
302                 /* are any devices attached? */
303                 if (!any_ports_active(uhci)) {
304                         uhci->rh_state = UHCI_RH_RUNNING_NODEVS;
305                         uhci->auto_stop_time = jiffies + HZ;
306                 }
307                 break;
308
309             case UHCI_RH_RUNNING_NODEVS:
310                 /* auto-stop if nothing connected for 1 second */
311                 if (any_ports_active(uhci))
312                         uhci->rh_state = UHCI_RH_RUNNING;
313                 else if (time_after_eq(jiffies, uhci->auto_stop_time))
314                         suspend_hc(uhci, UHCI_RH_AUTO_STOPPED);
315                 break;
316
317             case UHCI_RH_AUTO_STOPPED:
318                 /* wakeup if requested by a device */
319                 if (uhci->resume_detect)
320                         wakeup_hc(uhci);
321                 break;
322
323             default:
324                 break;
325         }
326 }
327
328 static void stall_callback(unsigned long _uhci)
329 {
330         struct uhci_hcd *uhci = (struct uhci_hcd *) _uhci;
331         unsigned long flags;
332
333         spin_lock_irqsave(&uhci->lock, flags);
334         uhci_scan_schedule(uhci, NULL);
335         check_fsbr(uhci);
336
337         /* Poll for and perform state transitions */
338         rh_state_transitions(uhci);
339         if (unlikely(uhci->suspended_ports))
340                 uhci_check_ports(uhci);
341
342         restart_timer(uhci);
343         spin_unlock_irqrestore(&uhci->lock, flags);
344 }
345
346 static irqreturn_t uhci_irq(struct usb_hcd *hcd, struct pt_regs *regs)
347 {
348         struct uhci_hcd *uhci = hcd_to_uhci(hcd);
349         unsigned long io_addr = uhci->io_addr;
350         unsigned short status;
351
352         /*
353          * Read the interrupt status, and write it back to clear the
354          * interrupt cause.  Contrary to the UHCI specification, the
355          * "HC Halted" status bit is persistent: it is RO, not R/WC.
356          */
357         status = inw(io_addr + USBSTS);
358         if (!(status & ~USBSTS_HCH))    /* shared interrupt, not mine */
359                 return IRQ_NONE;
360         outw(status, io_addr + USBSTS);         /* Clear it */
361
362         if (status & ~(USBSTS_USBINT | USBSTS_ERROR | USBSTS_RD)) {
363                 if (status & USBSTS_HSE)
364                         dev_err(uhci_dev(uhci), "host system error, "
365                                         "PCI problems?\n");
366                 if (status & USBSTS_HCPE)
367                         dev_err(uhci_dev(uhci), "host controller process "
368                                         "error, something bad happened!\n");
369                 if ((status & USBSTS_HCH) &&
370                                 uhci->rh_state >= UHCI_RH_RUNNING) {
371                         dev_err(uhci_dev(uhci), "host controller halted, "
372                                         "very bad!\n");
373                         /* FIXME: Reset the controller, fix the offending TD */
374                 }
375         }
376
377         if (status & USBSTS_RD)
378                 uhci->resume_detect = 1;
379
380         spin_lock(&uhci->lock);
381         uhci_scan_schedule(uhci, regs);
382         spin_unlock(&uhci->lock);
383
384         return IRQ_HANDLED;
385 }
386
387 /*
388  * Store the current frame number in uhci->frame_number if the controller
389  * is runnning
390  */
391 static void uhci_get_current_frame_number(struct uhci_hcd *uhci)
392 {
393         if (!uhci->is_stopped)
394                 uhci->frame_number = inw(uhci->io_addr + USBFRNUM);
395 }
396
397 /*
398  * De-allocate all resources
399  */
400 static void release_uhci(struct uhci_hcd *uhci)
401 {
402         int i;
403
404         for (i = 0; i < UHCI_NUM_SKELQH; i++)
405                 if (uhci->skelqh[i]) {
406                         uhci_free_qh(uhci, uhci->skelqh[i]);
407                         uhci->skelqh[i] = NULL;
408                 }
409
410         if (uhci->term_td) {
411                 uhci_free_td(uhci, uhci->term_td);
412                 uhci->term_td = NULL;
413         }
414
415         if (uhci->qh_pool) {
416                 dma_pool_destroy(uhci->qh_pool);
417                 uhci->qh_pool = NULL;
418         }
419
420         if (uhci->td_pool) {
421                 dma_pool_destroy(uhci->td_pool);
422                 uhci->td_pool = NULL;
423         }
424
425         if (uhci->fl) {
426                 dma_free_coherent(uhci_dev(uhci), sizeof(*uhci->fl),
427                                 uhci->fl, uhci->fl->dma_handle);
428                 uhci->fl = NULL;
429         }
430
431         if (uhci->dentry) {
432                 debugfs_remove(uhci->dentry);
433                 uhci->dentry = NULL;
434         }
435 }
436
437 static int uhci_reset(struct usb_hcd *hcd)
438 {
439         struct uhci_hcd *uhci = hcd_to_uhci(hcd);
440
441         uhci->io_addr = (unsigned long) hcd->rsrc_start;
442
443         /* Kick BIOS off this hardware and reset, so we won't get
444          * interrupts from any previous setup.
445          */
446         reset_hc(uhci);
447         return 0;
448 }
449
450 /*
451  * Allocate a frame list, and then setup the skeleton
452  *
453  * The hardware doesn't really know any difference
454  * in the queues, but the order does matter for the
455  * protocols higher up. The order is:
456  *
457  *  - any isochronous events handled before any
458  *    of the queues. We don't do that here, because
459  *    we'll create the actual TD entries on demand.
460  *  - The first queue is the interrupt queue.
461  *  - The second queue is the control queue, split into low- and full-speed
462  *  - The third queue is bulk queue.
463  *  - The fourth queue is the bandwidth reclamation queue, which loops back
464  *    to the full-speed control queue.
465  */
466 static int uhci_start(struct usb_hcd *hcd)
467 {
468         struct uhci_hcd *uhci = hcd_to_uhci(hcd);
469         int retval = -EBUSY;
470         int i, port;
471         unsigned io_size;
472         dma_addr_t dma_handle;
473         struct usb_device *udev;
474         struct dentry *dentry;
475
476         io_size = (unsigned) hcd->rsrc_len;
477
478         dentry = debugfs_create_file(hcd->self.bus_name, S_IFREG|S_IRUGO|S_IWUSR, uhci_debugfs_root, uhci, &uhci_debug_operations);
479         if (!dentry) {
480                 dev_err(uhci_dev(uhci), "couldn't create uhci debugfs entry\n");
481                 retval = -ENOMEM;
482                 goto err_create_debug_entry;
483         }
484         uhci->dentry = dentry;
485
486         uhci->fsbr = 0;
487         uhci->fsbrtimeout = 0;
488
489         spin_lock_init(&uhci->lock);
490         INIT_LIST_HEAD(&uhci->qh_remove_list);
491
492         INIT_LIST_HEAD(&uhci->td_remove_list);
493
494         INIT_LIST_HEAD(&uhci->urb_remove_list);
495
496         INIT_LIST_HEAD(&uhci->urb_list);
497
498         INIT_LIST_HEAD(&uhci->complete_list);
499
500         init_waitqueue_head(&uhci->waitqh);
501
502         init_timer(&uhci->stall_timer);
503         uhci->stall_timer.function = stall_callback;
504         uhci->stall_timer.data = (unsigned long) uhci;
505
506         uhci->fl = dma_alloc_coherent(uhci_dev(uhci), sizeof(*uhci->fl),
507                         &dma_handle, 0);
508         if (!uhci->fl) {
509                 dev_err(uhci_dev(uhci), "unable to allocate "
510                                 "consistent memory for frame list\n");
511                 goto err_alloc_fl;
512         }
513
514         memset((void *)uhci->fl, 0, sizeof(*uhci->fl));
515
516         uhci->fl->dma_handle = dma_handle;
517
518         uhci->td_pool = dma_pool_create("uhci_td", uhci_dev(uhci),
519                         sizeof(struct uhci_td), 16, 0);
520         if (!uhci->td_pool) {
521                 dev_err(uhci_dev(uhci), "unable to create td dma_pool\n");
522                 goto err_create_td_pool;
523         }
524
525         uhci->qh_pool = dma_pool_create("uhci_qh", uhci_dev(uhci),
526                         sizeof(struct uhci_qh), 16, 0);
527         if (!uhci->qh_pool) {
528                 dev_err(uhci_dev(uhci), "unable to create qh dma_pool\n");
529                 goto err_create_qh_pool;
530         }
531
532         /* Initialize the root hub */
533
534         /* UHCI specs says devices must have 2 ports, but goes on to say */
535         /*  they may have more but give no way to determine how many they */
536         /*  have. However, according to the UHCI spec, Bit 7 is always set */
537         /*  to 1. So we try to use this to our advantage */
538         for (port = 0; port < (io_size - 0x10) / 2; port++) {
539                 unsigned int portstatus;
540
541                 portstatus = inw(uhci->io_addr + 0x10 + (port * 2));
542                 if (!(portstatus & 0x0080))
543                         break;
544         }
545         if (debug)
546                 dev_info(uhci_dev(uhci), "detected %d ports\n", port);
547
548         /* This is experimental so anything less than 2 or greater than 8 is */
549         /*  something weird and we'll ignore it */
550         if (port < 2 || port > UHCI_RH_MAXCHILD) {
551                 dev_info(uhci_dev(uhci), "port count misdetected? "
552                                 "forcing to 2 ports\n");
553                 port = 2;
554         }
555
556         uhci->rh_numports = port;
557
558         udev = usb_alloc_dev(NULL, &hcd->self, 0);
559         if (!udev) {
560                 dev_err(uhci_dev(uhci), "unable to allocate root hub\n");
561                 goto err_alloc_root_hub;
562         }
563
564         uhci->term_td = uhci_alloc_td(uhci, udev);
565         if (!uhci->term_td) {
566                 dev_err(uhci_dev(uhci), "unable to allocate terminating TD\n");
567                 goto err_alloc_term_td;
568         }
569
570         for (i = 0; i < UHCI_NUM_SKELQH; i++) {
571                 uhci->skelqh[i] = uhci_alloc_qh(uhci, udev);
572                 if (!uhci->skelqh[i]) {
573                         dev_err(uhci_dev(uhci), "unable to allocate QH\n");
574                         goto err_alloc_skelqh;
575                 }
576         }
577
578         /*
579          * 8 Interrupt queues; link all higher int queues to int1,
580          * then link int1 to control and control to bulk
581          */
582         uhci->skel_int128_qh->link =
583                         uhci->skel_int64_qh->link =
584                         uhci->skel_int32_qh->link =
585                         uhci->skel_int16_qh->link =
586                         uhci->skel_int8_qh->link =
587                         uhci->skel_int4_qh->link =
588                         uhci->skel_int2_qh->link =
589                         cpu_to_le32(uhci->skel_int1_qh->dma_handle) | UHCI_PTR_QH;
590         uhci->skel_int1_qh->link = cpu_to_le32(uhci->skel_ls_control_qh->dma_handle) | UHCI_PTR_QH;
591
592         uhci->skel_ls_control_qh->link = cpu_to_le32(uhci->skel_fs_control_qh->dma_handle) | UHCI_PTR_QH;
593         uhci->skel_fs_control_qh->link = cpu_to_le32(uhci->skel_bulk_qh->dma_handle) | UHCI_PTR_QH;
594         uhci->skel_bulk_qh->link = cpu_to_le32(uhci->skel_term_qh->dma_handle) | UHCI_PTR_QH;
595
596         /* This dummy TD is to work around a bug in Intel PIIX controllers */
597         uhci_fill_td(uhci->term_td, 0, (UHCI_NULL_DATA_SIZE << 21) |
598                 (0x7f << TD_TOKEN_DEVADDR_SHIFT) | USB_PID_IN, 0);
599         uhci->term_td->link = cpu_to_le32(uhci->term_td->dma_handle);
600
601         uhci->skel_term_qh->link = UHCI_PTR_TERM;
602         uhci->skel_term_qh->element = cpu_to_le32(uhci->term_td->dma_handle);
603
604         /*
605          * Fill the frame list: make all entries point to the proper
606          * interrupt queue.
607          *
608          * The interrupt queues will be interleaved as evenly as possible.
609          * There's not much to be done about period-1 interrupts; they have
610          * to occur in every frame.  But we can schedule period-2 interrupts
611          * in odd-numbered frames, period-4 interrupts in frames congruent
612          * to 2 (mod 4), and so on.  This way each frame only has two
613          * interrupt QHs, which will help spread out bandwidth utilization.
614          */
615         for (i = 0; i < UHCI_NUMFRAMES; i++) {
616                 int irq;
617
618                 /*
619                  * ffs (Find First bit Set) does exactly what we need:
620                  * 1,3,5,...  => ffs = 0 => use skel_int2_qh = skelqh[6],
621                  * 2,6,10,... => ffs = 1 => use skel_int4_qh = skelqh[5], etc.
622                  * ffs > 6 => not on any high-period queue, so use
623                  *      skel_int1_qh = skelqh[7].
624                  * Add UHCI_NUMFRAMES to insure at least one bit is set.
625                  */
626                 irq = 6 - (int) __ffs(i + UHCI_NUMFRAMES);
627                 if (irq < 0)
628                         irq = 7;
629
630                 /* Only place we don't use the frame list routines */
631                 uhci->fl->frame[i] = UHCI_PTR_QH |
632                                 cpu_to_le32(uhci->skelqh[irq]->dma_handle);
633         }
634
635         /*
636          * Some architectures require a full mb() to enforce completion of
637          * the memory writes above before the I/O transfers in start_hc().
638          */
639         mb();
640         if ((retval = start_hc(uhci)) != 0)
641                 goto err_alloc_skelqh;
642
643         restart_timer(uhci);
644
645         udev->speed = USB_SPEED_FULL;
646
647         if (usb_hcd_register_root_hub(udev, hcd) != 0) {
648                 dev_err(uhci_dev(uhci), "unable to start root hub\n");
649                 retval = -ENOMEM;
650                 goto err_start_root_hub;
651         }
652
653         return 0;
654
655 /*
656  * error exits:
657  */
658 err_start_root_hub:
659         reset_hc(uhci);
660
661         del_timer_sync(&uhci->stall_timer);
662
663 err_alloc_skelqh:
664         for (i = 0; i < UHCI_NUM_SKELQH; i++)
665                 if (uhci->skelqh[i]) {
666                         uhci_free_qh(uhci, uhci->skelqh[i]);
667                         uhci->skelqh[i] = NULL;
668                 }
669
670         uhci_free_td(uhci, uhci->term_td);
671         uhci->term_td = NULL;
672
673 err_alloc_term_td:
674         usb_put_dev(udev);
675
676 err_alloc_root_hub:
677         dma_pool_destroy(uhci->qh_pool);
678         uhci->qh_pool = NULL;
679
680 err_create_qh_pool:
681         dma_pool_destroy(uhci->td_pool);
682         uhci->td_pool = NULL;
683
684 err_create_td_pool:
685         dma_free_coherent(uhci_dev(uhci), sizeof(*uhci->fl),
686                         uhci->fl, uhci->fl->dma_handle);
687         uhci->fl = NULL;
688
689 err_alloc_fl:
690         debugfs_remove(uhci->dentry);
691         uhci->dentry = NULL;
692
693 err_create_debug_entry:
694         return retval;
695 }
696
697 static void uhci_stop(struct usb_hcd *hcd)
698 {
699         struct uhci_hcd *uhci = hcd_to_uhci(hcd);
700
701         del_timer_sync(&uhci->stall_timer);
702         reset_hc(uhci);
703
704         spin_lock_irq(&uhci->lock);
705         uhci_scan_schedule(uhci, NULL);
706         spin_unlock_irq(&uhci->lock);
707         
708         release_uhci(uhci);
709 }
710
711 #ifdef CONFIG_PM
712 static int uhci_suspend(struct usb_hcd *hcd, pm_message_t message)
713 {
714         struct uhci_hcd *uhci = hcd_to_uhci(hcd);
715
716         spin_lock_irq(&uhci->lock);
717         suspend_hc(uhci, UHCI_RH_SUSPENDED);
718         spin_unlock_irq(&uhci->lock);
719         return 0;
720 }
721
722 static int uhci_resume(struct usb_hcd *hcd)
723 {
724         struct uhci_hcd *uhci = hcd_to_uhci(hcd);
725
726         spin_lock_irq(&uhci->lock);
727         if (uhci->rh_state == UHCI_RH_SUSPENDED) {
728
729                 /*
730                  * Some systems don't maintain the UHCI register values
731                  * during a PM suspend/resume cycle, so reinitialize
732                  * the Frame Number, Framelist Base Address, Interrupt
733                  * Enable, and Legacy Support registers.
734                  */
735                 pci_write_config_word(to_pci_dev(uhci_dev(uhci)), USBLEGSUP,
736                                 0);
737                 outw(uhci->frame_number, uhci->io_addr + USBFRNUM);
738                 outl(uhci->fl->dma_handle, uhci->io_addr + USBFLBASEADD);
739                 outw(USBINTR_TIMEOUT | USBINTR_RESUME | USBINTR_IOC |
740                                 USBINTR_SP, uhci->io_addr + USBINTR);
741                 pci_write_config_word(to_pci_dev(uhci_dev(uhci)), USBLEGSUP,
742                                 USBLEGSUP_DEFAULT);
743                 wakeup_hc(uhci);
744         }
745         spin_unlock_irq(&uhci->lock);
746
747         hcd->state = HC_STATE_RUNNING;
748         return 0;
749 }
750 #endif
751
752 /* Wait until all the URBs for a particular device/endpoint are gone */
753 static void uhci_hcd_endpoint_disable(struct usb_hcd *hcd,
754                 struct usb_host_endpoint *ep)
755 {
756         struct uhci_hcd *uhci = hcd_to_uhci(hcd);
757
758         wait_event_interruptible(uhci->waitqh, list_empty(&ep->urb_list));
759 }
760
761 static int uhci_hcd_get_frame_number(struct usb_hcd *hcd)
762 {
763         struct uhci_hcd *uhci = hcd_to_uhci(hcd);
764         unsigned long flags;
765         int is_stopped;
766         int frame_number;
767
768         /* Minimize latency by avoiding the spinlock */
769         local_irq_save(flags);
770         is_stopped = uhci->is_stopped;
771         smp_rmb();
772         frame_number = (is_stopped ? uhci->frame_number :
773                         inw(uhci->io_addr + USBFRNUM));
774         local_irq_restore(flags);
775         return frame_number;
776 }
777
778 static const char hcd_name[] = "uhci_hcd";
779
780 static const struct hc_driver uhci_driver = {
781         .description =          hcd_name,
782         .product_desc =         "UHCI Host Controller",
783         .hcd_priv_size =        sizeof(struct uhci_hcd),
784
785         /* Generic hardware linkage */
786         .irq =                  uhci_irq,
787         .flags =                HCD_USB11,
788
789         /* Basic lifecycle operations */
790         .reset =                uhci_reset,
791         .start =                uhci_start,
792 #ifdef CONFIG_PM
793         .suspend =              uhci_suspend,
794         .resume =               uhci_resume,
795 #endif
796         .stop =                 uhci_stop,
797
798         .urb_enqueue =          uhci_urb_enqueue,
799         .urb_dequeue =          uhci_urb_dequeue,
800
801         .endpoint_disable =     uhci_hcd_endpoint_disable,
802         .get_frame_number =     uhci_hcd_get_frame_number,
803
804         .hub_status_data =      uhci_hub_status_data,
805         .hub_control =          uhci_hub_control,
806 };
807
808 static const struct pci_device_id uhci_pci_ids[] = { {
809         /* handle any USB UHCI controller */
810         PCI_DEVICE_CLASS(((PCI_CLASS_SERIAL_USB << 8) | 0x00), ~0),
811         .driver_data =  (unsigned long) &uhci_driver,
812         }, { /* end: all zeroes */ }
813 };
814
815 MODULE_DEVICE_TABLE(pci, uhci_pci_ids);
816
817 static struct pci_driver uhci_pci_driver = {
818         .name =         (char *)hcd_name,
819         .id_table =     uhci_pci_ids,
820
821         .probe =        usb_hcd_pci_probe,
822         .remove =       usb_hcd_pci_remove,
823
824 #ifdef  CONFIG_PM
825         .suspend =      usb_hcd_pci_suspend,
826         .resume =       usb_hcd_pci_resume,
827 #endif  /* PM */
828 };
829  
830 static int __init uhci_hcd_init(void)
831 {
832         int retval = -ENOMEM;
833
834         printk(KERN_INFO DRIVER_DESC " " DRIVER_VERSION "\n");
835
836         if (usb_disabled())
837                 return -ENODEV;
838
839         if (debug) {
840                 errbuf = kmalloc(ERRBUF_LEN, GFP_KERNEL);
841                 if (!errbuf)
842                         goto errbuf_failed;
843         }
844
845         uhci_debugfs_root = debugfs_create_dir("uhci", NULL);
846         if (!uhci_debugfs_root)
847                 goto debug_failed;
848
849         uhci_up_cachep = kmem_cache_create("uhci_urb_priv",
850                 sizeof(struct urb_priv), 0, 0, NULL, NULL);
851         if (!uhci_up_cachep)
852                 goto up_failed;
853
854         retval = pci_register_driver(&uhci_pci_driver);
855         if (retval)
856                 goto init_failed;
857
858         return 0;
859
860 init_failed:
861         if (kmem_cache_destroy(uhci_up_cachep))
862                 warn("not all urb_priv's were freed!");
863
864 up_failed:
865         debugfs_remove(uhci_debugfs_root);
866
867 debug_failed:
868         kfree(errbuf);
869
870 errbuf_failed:
871
872         return retval;
873 }
874
875 static void __exit uhci_hcd_cleanup(void) 
876 {
877         pci_unregister_driver(&uhci_pci_driver);
878         
879         if (kmem_cache_destroy(uhci_up_cachep))
880                 warn("not all urb_priv's were freed!");
881
882         debugfs_remove(uhci_debugfs_root);
883         kfree(errbuf);
884 }
885
886 module_init(uhci_hcd_init);
887 module_exit(uhci_hcd_cleanup);
888
889 MODULE_AUTHOR(DRIVER_AUTHOR);
890 MODULE_DESCRIPTION(DRIVER_DESC);
891 MODULE_LICENSE("GPL");