Import 2.3.1pre2
[davej-history.git] / drivers / usb / uhci.c
blobc9a57627aa215d7155f44d87777e6649a3c81d44
1 /*
2 * Universal Host Controller Interface driver for USB.
4 * (C) Copyright 1999 Linus Torvalds
6 * Intel documents this fairly well, and as far as I know there
7 * are no royalties or anything like that, but even so there are
8 * people who decided that they want to do the same thing in a
9 * completely different way.
11 * Oh, well. The intel version is the more common by far. As such,
12 * that's the one I care about right now.
14 * WARNING! The USB documentation is downright evil. Most of it
15 * is just crap, written by a committee. You're better off ignoring
16 * most of it, the important stuff is:
17 * - the low-level protocol (fairly simple but lots of small details)
18 * - working around the horridness of the rest
21 /* 4/4/1999 added data toggle for interrupt pipes -keryan */
23 #include <linux/config.h>
24 #include <linux/module.h>
25 #include <linux/pci.h>
26 #include <linux/kernel.h>
27 #include <linux/delay.h>
28 #include <linux/ioport.h>
29 #include <linux/sched.h>
30 #include <linux/malloc.h>
31 #include <linux/smp_lock.h>
32 #include <linux/errno.h>
34 #include <asm/spinlock.h>
35 #include <asm/io.h>
36 #include <asm/irq.h>
37 #include <asm/system.h>
39 #include "uhci.h"
40 #include "inits.h"
42 #ifdef CONFIG_APM
43 #include <linux/apm_bios.h>
44 static int handle_apm_event(apm_event_t event);
45 static int apm_resume = 0;
46 #endif
48 #define compile_assert(x) do { switch (0) { case 1: case !(x): } } while (0)
50 static DECLARE_WAIT_QUEUE_HEAD(uhci_configure);
53 * Return the result of a TD..
55 static int uhci_td_result(struct uhci_device *dev, struct uhci_td *td)
57 unsigned int status;
59 status = (td->status >> 16) & 0xff;
61 /* Some debugging code */
62 if (status) {
63 int i = 10;
64 struct uhci_td *tmp = dev->control_td;
65 printk("uhci_td_result() failed with status %d\n", status);
66 show_status(dev->uhci);
67 do {
68 show_td(tmp);
69 tmp++;
70 if (!--i)
71 break;
72 } while (tmp <= td);
74 return status;
78 * Inserts a td into qh list at the top.
80 * Careful about atomicity: even on UP this
81 * requires a locked access due to the concurrent
82 * DMA engine.
84 * NOTE! This assumes that first->last is a valid
85 * list of TD's with the proper backpointers set
86 * up and all..
88 static void uhci_insert_tds_in_qh(struct uhci_qh *qh, struct uhci_td *first, struct uhci_td *last)
90 unsigned int link = qh->element;
91 unsigned int new = 4 | virt_to_bus(first);
93 for (;;) {
94 unsigned char success;
96 last->link = link;
97 first->backptr = &qh->element;
98 asm volatile("lock ; cmpxchg %4,%2 ; sete %0"
99 :"=q" (success), "=a" (link)
100 :"m" (qh->element), "1" (link), "r" (new)
101 :"memory");
102 if (success) {
103 /* Was there a successor entry? Fix it's backpointer.. */
104 if ((link & 1) == 0) {
105 struct uhci_td *next = bus_to_virt(link & ~15);
106 next->backptr = &last->link;
108 break;
113 static inline void uhci_insert_td_in_qh(struct uhci_qh *qh, struct uhci_td *td)
115 uhci_insert_tds_in_qh(qh, td, td);
118 static void uhci_insert_qh(struct uhci_qh *qh, struct uhci_qh *newqh)
120 newqh->link = qh->link;
121 qh->link = virt_to_bus(newqh) | 2;
124 static void uhci_remove_qh(struct uhci_qh *qh, struct uhci_qh *remqh)
126 unsigned int remphys = virt_to_bus(remqh);
127 struct uhci_qh *lqh = qh;
129 while ((lqh->link & ~0xF) != remphys) {
130 if (lqh->link & 1)
131 break;
133 lqh = bus_to_virt(lqh->link & ~0xF);
136 if (lqh->link & 1) {
137 printk("couldn't find qh in chain!\n");
138 return;
141 lqh->link = remqh->link;
145 * Removes td from qh if present.
147 * NOTE! We keep track of both forward and back-pointers,
148 * so this should be trivial, right?
150 * Wrong. While all TD insert/remove operations are synchronous
151 * on the CPU, the UHCI controller can (and does) play with the
152 * very first forward pointer. So we need to validate the backptr
153 * before we change it, so that we don't by mistake reset the QH
154 * head to something old.
156 static void uhci_remove_td(struct uhci_td *td)
158 unsigned int *backptr = td->backptr;
159 unsigned int link = td->link;
160 unsigned int me;
162 if (!backptr)
163 return;
165 td->backptr = NULL;
168 * This is the easy case: the UHCI will never change "td->link",
169 * so we can always just look at that and fix up the backpointer
170 * of any next element..
172 if (!(link & 1)) {
173 struct uhci_td *next = bus_to_virt(link & ~15);
174 next->backptr = backptr;
178 * The nasty case is "backptr->next", which we need to
179 * update to "link" _only_ if "backptr" still points
180 * to us (it may not: maybe backptr is a QH->element
181 * pointer and the UHCI has changed the value).
183 me = virt_to_bus(td) | (0xe & *backptr);
184 asm volatile("lock ; cmpxchg %0,%1"
186 :"r" (link), "m" (*backptr), "a" (me)
187 :"memory");
190 static struct uhci_qh *uhci_qh_allocate(struct uhci_device *dev)
192 struct uhci_qh *qh;
193 int inuse;
195 qh = dev->qh;
196 for (; (inuse = test_and_set_bit(0, &qh->inuse)) != 0 && qh < &dev->qh[UHCI_MAXQH]; qh++)
199 if (!inuse)
200 return(qh);
202 printk("ran out of qh's for dev %p\n", dev);
203 return(NULL);
206 static void uhci_qh_deallocate(struct uhci_qh *qh)
208 if (qh->element != 1)
209 printk("qh %p leaving dangling entries? (%X)\n", qh, qh->element);
211 qh->element = 1;
212 qh->link = 1;
214 clear_bit(0, &qh->inuse);
217 static struct uhci_td *uhci_td_allocate(struct uhci_device *dev)
219 struct uhci_td *td;
220 int inuse;
222 td = dev->td;
223 for (; (inuse = test_and_set_bit(0, &td->inuse)) != 0 && td < &dev->td[UHCI_MAXTD]; td++)
226 if (!inuse)
227 return(td);
229 printk("ran out of td's for dev %p\n", dev);
230 return(NULL);
234 * This MUST only be called when it has been removed from a QH already (or
235 * the QH has been removed from the skeleton
237 static void uhci_td_deallocate(struct uhci_td *td)
239 td->link = 1;
241 clear_bit(0, &td->inuse);
245 * UHCI interrupt list operations..
247 static spinlock_t irqlist_lock = SPIN_LOCK_UNLOCKED;
249 static void uhci_add_irq_list(struct uhci *uhci, struct uhci_td *td, usb_device_irq completed, void *dev_id)
251 unsigned long flags;
253 td->completed = completed;
254 td->dev_id = dev_id;
256 spin_lock_irqsave(&irqlist_lock, flags);
257 list_add(&td->irq_list, &uhci->interrupt_list);
258 spin_unlock_irqrestore(&irqlist_lock, flags);
261 static void uhci_remove_irq_list(struct uhci_td *td)
263 unsigned long flags;
265 spin_lock_irqsave(&irqlist_lock, flags);
266 list_del(&td->irq_list);
267 spin_unlock_irqrestore(&irqlist_lock, flags);
271 * Request a interrupt handler..
273 static int uhci_request_irq(struct usb_device *usb_dev, unsigned int pipe, usb_device_irq handler, int period, void *dev_id)
275 struct uhci_device *dev = usb_to_uhci(usb_dev);
276 struct uhci_td *td = uhci_td_allocate(dev);
277 struct uhci_qh *interrupt_qh = uhci_qh_allocate(dev);
279 unsigned int destination, status;
281 /* Destination: pipe destination with INPUT */
282 destination = (pipe & 0x0007ff00) | 0x69;
284 /* Status: slow/fast, Interrupt, Active, Short Packet Detect Infinite Errors */
285 status = (pipe & (1 << 26)) | (1 << 24) | (1 << 23) | (1 << 29) | (0 << 27);
287 if(interrupt_qh->element != 1)
288 printk("interrupt_qh->element = 0x%x\n",
289 interrupt_qh->element);
291 td->link = 1;
292 td->status = status; /* In */
293 td->info = destination | (7 << 21); /* 8 bytes of data */
294 td->buffer = virt_to_bus(dev->data);
295 td->qh = interrupt_qh;
296 interrupt_qh->skel = &dev->uhci->root_hub->skel_int8_qh;
298 uhci_add_irq_list(dev->uhci, td, handler, dev_id);
300 uhci_insert_td_in_qh(interrupt_qh, td);
302 /* Add it into the skeleton */
303 uhci_insert_qh(&dev->uhci->root_hub->skel_int8_qh, interrupt_qh);
304 return 0;
308 * Control thread operations: we just mark the last TD
309 * in a control thread as an interrupt TD, and wake up
310 * the front-end on completion.
312 * We need to remove the TD from the lists (both interrupt
313 * list and TD lists) by hand if something bad happens!
315 static DECLARE_WAIT_QUEUE_HEAD(control_wakeup);
317 static int uhci_control_completed(int status, void *buffer, void *dev_id)
319 wake_up(&control_wakeup);
320 return 0; /* Don't re-instate */
323 /* td points to the last td in the list, which interrupts on completion */
324 static int uhci_run_control(struct uhci_device *dev, struct uhci_td *first, struct uhci_td *last)
326 DECLARE_WAITQUEUE(wait, current);
327 struct uhci_qh *ctrl_qh = uhci_qh_allocate(dev);
328 struct uhci_td *curtd;
330 current->state = TASK_UNINTERRUPTIBLE;
331 add_wait_queue(&control_wakeup, &wait);
333 uhci_add_irq_list(dev->uhci, last, uhci_control_completed, NULL);
335 /* FIXME: This is kinda kludged */
336 /* Walk the TD list and update the QH pointer */
338 int maxcount = 100;
340 curtd = first;
341 do {
342 curtd->qh = ctrl_qh;
343 if (curtd->link & 1)
344 break;
346 curtd = bus_to_virt(curtd->link & ~0xF);
347 if (!--maxcount) {
348 printk("runaway tds!\n");
349 break;
351 } while (1);
354 uhci_insert_tds_in_qh(ctrl_qh, first, last);
356 /* Add it into the skeleton */
357 uhci_insert_qh(&dev->uhci->root_hub->skel_control_qh, ctrl_qh);
359 schedule_timeout(HZ/10);
361 remove_wait_queue(&control_wakeup, &wait);
363 /* Clean up in case it failed.. */
364 uhci_remove_irq_list(last);
366 #if 0
367 printk("Looking for tds [%p, %p]\n", dev->control_td, td);
368 #endif
370 /* Remove it from the skeleton */
371 uhci_remove_qh(&dev->uhci->root_hub->skel_control_qh, ctrl_qh);
373 uhci_qh_deallocate(ctrl_qh);
375 return uhci_td_result(dev, last);
379 * Send or receive a control message on a pipe.
381 * Note that the "pipe" structure is set up to map
382 * easily to the uhci destination fields.
384 * A control message is built up from three parts:
385 * - The command itself
386 * - [ optional ] data phase
387 * - Status complete phase
389 * The data phase can be an arbitrary number of TD's
390 * although we currently had better not have more than
391 * 29 TD's here (we have 31 TD's allocated for control
392 * operations, and two of them are used for command and
393 * status).
395 * 29 TD's is a minimum of 232 bytes worth of control
396 * information, that's just ridiculously high. Most
397 * control messages have just a few bytes of data.
399 static int uhci_control_msg(struct usb_device *usb_dev, unsigned int pipe, void *cmd, void *data, int len)
401 struct uhci_device *dev = usb_to_uhci(usb_dev);
402 struct uhci_td *first, *td, *prevtd;
403 unsigned long destination, status;
404 int ret;
406 if (len > usb_maxpacket(usb_dev->maxpacketsize) * 29)
407 printk("Warning, too much data for a control packet, crashing\n");
409 first = td = uhci_td_allocate(dev);
411 /* The "pipe" thing contains the destination in bits 8--18, 0x2D is SETUP */
412 destination = (pipe & 0x0007ff00) | 0x2D;
414 /* Status: slow/fast, Active, Short Packet Detect Three Errors */
415 status = (pipe & (1 << 26)) | (1 << 23) | (1 << 29) | (3 << 27);
418 * Build the TD for the control request
420 td->status = status; /* Try forever */
421 td->info = destination | (7 << 21); /* 8 bytes of data */
422 td->buffer = virt_to_bus(cmd);
425 * If direction is "send", change the frame from SETUP (0x2D)
426 * to OUT (0xE1). Else change it from SETUP to IN (0x69)
428 destination ^= (0x2D ^ 0x69); /* SETUP -> IN */
429 if (usb_pipeout(pipe))
430 destination ^= (0xE1 ^ 0x69); /* IN -> OUT */
432 prevtd = td;
433 td = uhci_td_allocate(dev);
434 prevtd->link = 4 | virt_to_bus(td);
437 * Build the DATA TD's
439 while (len > 0) {
440 /* Build the TD for control status */
441 int pktsze = len;
442 int maxsze = usb_maxpacket(pipe);
444 if (pktsze > maxsze)
445 pktsze = maxsze;
447 /* Alternate Data0/1 (start with Data1) */
448 destination ^= 1 << 19;
450 td->status = status; /* Status */
451 td->info = destination | ((pktsze-1) << 21); /* pktsze bytes of data */
452 td->buffer = virt_to_bus(data);
453 td->backptr = &prevtd->link;
455 prevtd = td;
456 td = uhci_td_allocate(dev);
457 prevtd->link = 4 | virt_to_bus(td); /* Update previous TD */
459 data += maxsze;
460 len -= maxsze;
464 * Build the final TD for control status
466 destination ^= (0xE1 ^ 0x69); /* OUT -> IN */
467 destination |= 1 << 19; /* End in Data1 */
469 td->link = 1; /* Terminate */
470 td->status = status | (1 << 24); /* IOC */
471 td->info = destination | (0x7ff << 21); /* 0 bytes of data */
472 td->buffer = 0;
473 td->backptr = &prevtd->link;
475 /* Start it up.. */
476 ret = uhci_run_control(dev, first, td);
479 int maxcount = 100;
480 struct uhci_td *curtd = first;
481 unsigned int nextlink;
483 do {
484 nextlink = curtd->link;
485 uhci_remove_td(curtd);
486 uhci_td_deallocate(curtd);
487 if (nextlink & 1) /* Tail? */
488 break;
490 curtd = bus_to_virt(nextlink & ~0xF);
491 if (!--maxcount) {
492 printk("runaway td's!?\n");
493 break;
495 } while (1);
498 return ret;
501 static struct usb_device *uhci_usb_allocate(struct usb_device *parent)
503 struct usb_device *usb_dev;
504 struct uhci_device *dev;
505 int i;
507 usb_dev = kmalloc(sizeof(*usb_dev), GFP_KERNEL);
508 if (!usb_dev)
509 return NULL;
511 memset(usb_dev, 0, sizeof(*usb_dev));
513 dev = kmalloc(sizeof(*dev), GFP_KERNEL);
514 if (!dev) {
515 usb_destroy_configuration(usb_dev);
516 kfree(usb_dev);
517 return NULL;
520 /* Initialize "dev" */
521 memset(dev, 0, sizeof(*dev));
523 usb_dev->hcpriv = dev;
524 dev->usb = usb_dev;
526 usb_dev->parent = parent;
528 if (parent) {
529 usb_dev->bus = parent->bus;
530 dev->uhci = usb_to_uhci(parent)->uhci;
533 /* Reset the QH's and TD's */
534 for (i = 0; i < UHCI_MAXQH; i++) {
535 dev->qh[i].link = 1;
536 dev->qh[i].element = 1;
537 dev->qh[i].inuse = 0;
540 for (i = 0; i < UHCI_MAXTD; i++) {
541 dev->td[i].link = 1;
542 dev->td[i].inuse = 0;
545 return usb_dev;
548 static int uhci_usb_deallocate(struct usb_device *usb_dev)
550 struct uhci_device *dev = usb_to_uhci(usb_dev);
551 int i;
553 /* There are UHCI_MAXTD preallocated tds */
554 for (i = 0; i < UHCI_MAXTD; ++i) {
555 struct uhci_td *td = dev->td + i;
557 /* And remove it from the irq list, if it's active */
558 if (td->status & (1 << 23))
559 uhci_remove_irq_list(td);
561 if (td->inuse)
562 uhci_remove_td(td);
565 /* Remove the td from any queues */
566 for (i = 0; i < UHCI_MAXQH; ++i) {
567 struct uhci_qh *qh = dev->qh + i;
569 if (qh->inuse)
570 uhci_remove_qh(qh->skel, qh);
573 kfree(dev);
574 usb_destroy_configuration(usb_dev);
575 kfree(usb_dev);
577 return 0;
580 struct usb_operations uhci_device_operations = {
581 uhci_usb_allocate,
582 uhci_usb_deallocate,
583 uhci_control_msg,
584 uhci_request_irq,
588 * This is just incredibly fragile. The timings must be just
589 * right, and they aren't really documented very well.
591 * Note the short delay between disabling reset and enabling
592 * the port..
594 static void uhci_reset_port(unsigned int port)
596 unsigned short status;
598 status = inw(port);
599 outw(status | USBPORTSC_PR, port); /* reset port */
600 wait_ms(10);
601 outw(status & ~USBPORTSC_PR, port);
602 udelay(5);
604 status = inw(port);
605 outw(status | USBPORTSC_PE, port); /* enable port */
606 wait_ms(10);
608 status = inw(port);
609 if(!(status & USBPORTSC_PE)) {
610 outw(status | USBPORTSC_PE, port); /* one more try at enabling port */
611 wait_ms(50);
618 * This gets called if the connect status on the root
619 * hub (and the root hub only) changes.
621 static void uhci_connect_change(struct uhci *uhci, unsigned int port, unsigned int nr)
623 struct usb_device *usb_dev;
624 struct uhci_device *dev;
625 unsigned short status;
627 printk("uhci_connect_change: called for %d\n", nr);
630 * Even if the status says we're connected,
631 * the fact that the status bits changed may
632 * that we got disconnected and then reconnected.
634 * So start off by getting rid of any old devices..
636 usb_disconnect(&uhci->root_hub->usb->children[nr]);
638 status = inw(port);
640 /* If we have nothing connected, then clear change status and disable the port */
641 status = (status & ~USBPORTSC_PE) | USBPORTSC_PEC;
642 if (!(status & USBPORTSC_CCS)) {
643 outw(status, port);
644 return;
648 * Ok, we got a new connection. Allocate a device to it,
649 * and find out what it wants to do..
651 usb_dev = uhci_usb_allocate(uhci->root_hub->usb);
652 dev = usb_dev->hcpriv;
654 dev->uhci = uhci;
656 usb_connect(usb_dev);
658 uhci->root_hub->usb->children[nr] = usb_dev;
660 wait_ms(200); /* wait for powerup */
661 uhci_reset_port(port);
663 /* Get speed information */
664 usb_dev->slow = (inw(port) & USBPORTSC_LSDA) ? 1 : 0;
667 * Ok, all the stuff specific to the root hub has been done.
668 * The rest is generic for any new USB attach, regardless of
669 * hub type.
671 usb_new_device(usb_dev);
675 * This gets called when the root hub configuration
676 * has changed. Just go through each port, seeing if
677 * there is something interesting happening.
679 static void uhci_check_configuration(struct uhci *uhci)
681 unsigned int io_addr = uhci->io_addr + USBPORTSC1;
682 int maxchild = uhci->root_hub->usb->maxchild;
683 int nr = 0;
685 do {
686 unsigned short status = inw(io_addr);
688 if (status & USBPORTSC_CSC)
689 uhci_connect_change(uhci, io_addr, nr);
691 nr++; io_addr += 2;
692 } while (nr < maxchild);
695 static void uhci_interrupt_notify(struct uhci *uhci)
697 struct list_head *head = &uhci->interrupt_list;
698 struct list_head *tmp;
700 spin_lock(&irqlist_lock);
701 tmp = head->next;
702 while (tmp != head) {
703 struct uhci_td *td = list_entry(tmp, struct uhci_td, irq_list);
704 struct list_head *next;
706 next = tmp->next;
708 if (!(td->status & (1 << 23))) { /* No longer active? */
709 /* remove from IRQ list */
710 __list_del(tmp->prev, next);
711 INIT_LIST_HEAD(tmp);
712 if (td->completed(td->status, bus_to_virt(td->buffer), td->dev_id)) {
713 struct uhci_qh *interrupt_qh = td->qh;
715 list_add(&td->irq_list, &uhci->interrupt_list);
716 td->info ^= 1 << 19; /* toggle between data0 and data1 */
717 td->status = (td->status & 0x2f000000) | (1 << 23) | (1 << 24); /* active */
719 /* Remove then readd? Is that necessary */
720 uhci_remove_td(td);
721 uhci_insert_td_in_qh(interrupt_qh, td);
723 /* If completed wants to not reactivate, then it's */
724 /* responsible for free'ing the TD's and QH's */
725 /* or another function (such as run_control) */
727 tmp = next;
729 spin_unlock(&irqlist_lock);
733 * Check port status - Connect Status Change - for
734 * each of the attached ports (defaults to two ports,
735 * but at least in theory there can be more of them).
737 * Wake up the configurator if something happened, we
738 * can't really do much at interrupt time.
740 static void uhci_root_hub_events(struct uhci *uhci, unsigned int io_addr)
742 if (waitqueue_active(&uhci_configure)) {
743 int ports = uhci->root_hub->usb->maxchild;
744 io_addr += USBPORTSC1;
745 do {
746 if (inw(io_addr) & USBPORTSC_CSC) {
747 wake_up(&uhci_configure);
748 return;
750 io_addr += 2;
751 } while (--ports > 0);
755 static void uhci_interrupt(int irq, void *__uhci, struct pt_regs *regs)
757 struct uhci *uhci = __uhci;
758 unsigned int io_addr = uhci->io_addr;
759 unsigned short status;
762 * Read the interrupt status, and write it back to clear the interrupt cause
764 status = inw(io_addr + USBSTS);
765 outw(status, io_addr + USBSTS);
767 /* Walk the list of pending TD's to see which ones completed.. */
768 uhci_interrupt_notify(uhci);
770 /* Check if there are any events on the root hub.. */
771 uhci_root_hub_events(uhci, io_addr);
775 * We init one packet, and mark it just IOC and _not_
776 * active. Which will result in no actual USB traffic,
777 * but _will_ result in an interrupt every second.
779 * Which is exactly what we want.
781 static void uhci_init_ticktd(struct uhci *uhci)
783 struct uhci_device *dev = uhci->root_hub;
784 struct uhci_td *td = uhci_td_allocate(dev);
786 td->link = 1;
787 td->status = (1 << 24); /* interrupt on completion */
788 td->info = (15 << 21) | 0x7f69; /* (ignored) input packet, 16 bytes, device 127 */
789 td->buffer = 0;
790 td->qh = NULL;
792 uhci->fl->frame[0] = virt_to_bus(td);
795 static void reset_hc(struct uhci *uhci)
797 unsigned int io_addr = uhci->io_addr;
799 /* Global reset for 50ms */
800 outw(USBCMD_GRESET, io_addr+USBCMD);
801 wait_ms(50);
802 outw(0, io_addr+USBCMD);
803 wait_ms(10);
806 static void start_hc(struct uhci *uhci)
808 unsigned int io_addr = uhci->io_addr;
809 int timeout = 1000;
811 uhci_init_ticktd(uhci);
814 * Reset the HC - this will force us to get a
815 * new notification of any already connected
816 * ports due to the virtual disconnect that it
817 * implies.
819 outw(USBCMD_HCRESET, io_addr + USBCMD);
820 while (inw(io_addr + USBCMD) & USBCMD_HCRESET) {
821 if (!--timeout) {
822 printk("USBCMD_HCRESET timed out!\n");
823 break;
827 outw(USBINTR_TIMEOUT | USBINTR_RESUME | USBINTR_IOC | USBINTR_SP, io_addr + USBINTR);
828 outw(0, io_addr + USBFRNUM);
829 outl(virt_to_bus(uhci->fl), io_addr + USBFLBASEADD);
831 /* Run and mark it configured with a 64-byte max packet */
832 outw(USBCMD_RS | USBCMD_CF, io_addr + USBCMD);
836 * Allocate a frame list, and four regular queues.
838 * The hardware doesn't really know any difference
839 * in the queues, but the order does matter for the
840 * protocols higher up. The order is:
842 * - any isochronous events handled before any
843 * of the queues. We don't do that here, because
844 * we'll create the actual TD entries on demand.
845 * - The first queue is the "interrupt queue".
846 * - The second queue is the "control queue".
847 * - The third queue is "bulk data".
849 * We could certainly have multiple queues of the same
850 * type, and maybe we should. We could have per-device
851 * queues, for example. We begin small.
853 static struct uhci *alloc_uhci(unsigned int io_addr)
855 int i;
856 struct uhci *uhci;
857 struct usb_bus *bus;
858 struct uhci_device *dev;
859 struct usb_device *usb;
861 uhci = kmalloc(sizeof(*uhci), GFP_KERNEL);
862 if (!uhci)
863 return NULL;
865 memset(uhci, 0, sizeof(*uhci));
867 uhci->irq = -1;
868 uhci->io_addr = io_addr;
869 INIT_LIST_HEAD(&uhci->interrupt_list);
871 /* We need exactly one page (per UHCI specs), how convenient */
872 uhci->fl = (void *)__get_free_page(GFP_KERNEL);
874 bus = kmalloc(sizeof(*bus), GFP_KERNEL);
875 if (!bus)
876 return NULL;
878 memset(bus, 0, sizeof(*bus));
880 uhci->bus = bus;
881 bus->hcpriv = uhci;
882 bus->op = &uhci_device_operations;
885 * We allocate a 8kB area for the UHCI hub. The area
886 * is described by the uhci_device structure, and basically
887 * contains everything needed for normal operation.
889 * The first page is the actual device descriptor for the
890 * hub.
892 * The second page is used for the frame list.
894 usb = uhci_usb_allocate(NULL);
895 if (!usb)
896 return NULL;
898 dev = uhci->root_hub = usb_to_uhci(usb);
900 usb->bus = bus;
902 /* Initialize the root hub */
903 /* UHCI specs says devices must have 2 ports, but goes on to say */
904 /* they may have more but give no way to determine how many they */
905 /* have, so default to 2 */
906 usb->maxchild = 2;
907 usb_init_root_hub(usb);
910 * Initialize the queues. They all start out empty,
911 * linked to each other in the proper order.
913 for (i = 1 ; i < 9; i++) {
914 dev->qh[i].link = 2 | virt_to_bus(&dev->skel_control_qh);
915 dev->qh[i].element = 1;
918 dev->skel_control_qh.link = 2 | virt_to_bus(&dev->skel_bulk0_qh);
919 dev->skel_control_qh.element = 1;
921 dev->skel_bulk0_qh.link = 2 | virt_to_bus(&dev->skel_bulk1_qh);
922 dev->skel_bulk0_qh.element = 1;
924 dev->skel_bulk1_qh.link = 2 | virt_to_bus(&dev->skel_bulk2_qh);
925 dev->skel_bulk1_qh.element = 1;
927 dev->skel_bulk2_qh.link = 2 | virt_to_bus(&dev->skel_bulk3_qh);
928 dev->skel_bulk2_qh.element = 1;
930 dev->skel_bulk3_qh.link = 1;
931 dev->skel_bulk3_qh.element = 1;
934 * Fill the frame list: make all entries point to
935 * the proper interrupt queue.
937 * This is probably silly, but it's a simple way to
938 * scatter the interrupt queues in a way that gives
939 * us a reasonable dynamic range for irq latencies.
941 for (i = 0; i < 1024; i++) {
942 struct uhci_qh * irq = &dev->skel_int2_qh;
943 if (i & 1) {
944 irq++;
945 if (i & 2) {
946 irq++;
947 if (i & 4) {
948 irq++;
949 if (i & 8) {
950 irq++;
951 if (i & 16) {
952 irq++;
953 if (i & 32) {
954 irq++;
955 if (i & 64) {
956 irq++;
964 uhci->fl->frame[i] = 2 | virt_to_bus(irq);
967 return uhci;
972 * De-allocate all resources..
974 static void release_uhci(struct uhci *uhci)
976 if (uhci->irq >= 0) {
977 free_irq(uhci->irq, uhci);
978 uhci->irq = -1;
981 #if 0
982 if (uhci->root_hub) {
983 uhci_usb_deallocate(uhci_to_usb(uhci->root_hub));
984 uhci->root_hub = NULL;
986 #endif
988 if (uhci->fl) {
989 free_page((unsigned long)uhci->fl);
990 uhci->fl = NULL;
993 kfree(uhci->bus);
994 kfree(uhci);
997 void cleanup_drivers(void);
999 static int uhci_control_thread(void * __uhci)
1001 struct uhci *uhci = (struct uhci *)__uhci;
1003 lock_kernel();
1004 request_region(uhci->io_addr, 32, "usb-uhci");
1007 * This thread doesn't need any user-level access,
1008 * so get rid of all our resources..
1010 printk("uhci_control_thread at %p\n", &uhci_control_thread);
1011 exit_mm(current);
1012 exit_files(current);
1013 exit_fs(current);
1015 strcpy(current->comm, "uhci-control");
1018 * Ok, all systems are go..
1020 start_hc(uhci);
1021 for(;;) {
1022 siginfo_t info;
1023 int unsigned long signr;
1025 interruptible_sleep_on(&uhci_configure);
1026 #ifdef CONFIG_APM
1027 if (apm_resume) {
1028 apm_resume = 0;
1029 start_hc(uhci);
1030 continue;
1032 #endif
1033 uhci_check_configuration(uhci);
1035 if(signal_pending(current)) {
1036 /* sending SIGUSR1 makes us print out some info */
1037 spin_lock_irq(&current->sigmask_lock);
1038 signr = dequeue_signal(&current->blocked, &info);
1039 spin_unlock_irq(&current->sigmask_lock);
1041 if(signr == SIGUSR1) {
1042 printk("UHCI queue dump:\n");
1043 show_queues(uhci);
1044 } else {
1045 break;
1050 #if 0
1051 if(uhci->root_hub)
1052 for(i = 0; i < uhci->root_hub->usb->maxchild; i++)
1053 usb_disconnect(uhci->root_hub->usb->children + i);
1054 #endif
1056 cleanup_drivers();
1058 reset_hc(uhci);
1059 release_region(uhci->io_addr, 32);
1061 release_uhci(uhci);
1062 MOD_DEC_USE_COUNT;
1064 printk("uhci_control_thread exiting\n");
1066 return 0;
1070 * If we've successfully found a UHCI, now is the time to increment the
1071 * module usage count, start the control thread, and return success..
1073 static int found_uhci(int irq, unsigned int io_addr)
1075 int retval;
1076 struct uhci *uhci;
1078 uhci = alloc_uhci(io_addr);
1079 if (!uhci)
1080 return -ENOMEM;
1082 reset_hc(uhci);
1084 retval = -EBUSY;
1085 if (request_irq(irq, uhci_interrupt, SA_SHIRQ, "usb", uhci) == 0) {
1086 int pid;
1088 MOD_INC_USE_COUNT;
1089 uhci->irq = irq;
1090 pid = kernel_thread(uhci_control_thread, uhci, CLONE_FS | CLONE_FILES | CLONE_SIGHAND);
1091 if (pid >= 0)
1092 return 0;
1094 MOD_DEC_USE_COUNT;
1095 retval = pid;
1097 release_uhci(uhci);
1098 return retval;
1101 static int start_uhci(struct pci_dev *dev)
1103 int i;
1105 /* Search for the IO base address.. */
1106 for (i = 0; i < 6; i++) {
1107 unsigned int io_addr = dev->base_address[i];
1109 /* IO address? */
1110 if (!(io_addr & 1))
1111 continue;
1113 io_addr &= PCI_BASE_ADDRESS_IO_MASK;
1115 /* Is it already in use? */
1116 if (check_region(io_addr, 32))
1117 break;
1119 return found_uhci(dev->irq, io_addr);
1121 return -1;
1124 #ifdef CONFIG_APM
1125 static int handle_apm_event(apm_event_t event)
1127 static int down = 0;
1129 switch (event) {
1130 case APM_SYS_SUSPEND:
1131 case APM_USER_SUSPEND:
1132 if (down) {
1133 printk(KERN_DEBUG "uhci: received extra suspend event\n");
1134 break;
1136 down = 1;
1137 break;
1138 case APM_NORMAL_RESUME:
1139 case APM_CRITICAL_RESUME:
1140 if (!down) {
1141 printk(KERN_DEBUG "uhci: received bogus resume event\n");
1142 break;
1144 down = 0;
1145 if (waitqueue_active(&uhci_configure)) {
1146 apm_resume = 1;
1147 wake_up(&uhci_configure);
1149 break;
1151 return 0;
1153 #endif
1155 #ifdef MODULE
1157 void cleanup_module(void)
1159 #ifdef CONFIG_APM
1160 apm_unregister_callback(&handle_apm_event);
1161 #endif
1164 #define uhci_init init_module
1166 #endif
1168 int uhci_init(void)
1170 int retval;
1171 struct pci_dev *dev = NULL;
1172 u8 type;
1174 retval = -ENODEV;
1175 for (;;) {
1176 dev = pci_find_class(PCI_CLASS_SERIAL_USB<<8, dev);
1177 if (!dev)
1178 break;
1179 /* Is it UHCI */
1180 pci_read_config_byte(dev, PCI_CLASS_PROG, &type);
1181 if(type != 0)
1182 continue;
1183 /* Ok set it up */
1184 retval = start_uhci(dev);
1185 if (retval < 0)
1186 continue;
1188 #ifdef CONFIG_USB_MOUSE
1189 usb_mouse_init();
1190 #endif
1191 #ifdef CONFIG_USB_KBD
1192 usb_kbd_init();
1193 #endif
1194 hub_init();
1195 #ifdef CONFIG_USB_AUDIO
1196 usb_audio_init();
1197 #endif
1198 #ifdef CONFIG_APM
1199 apm_register_callback(&handle_apm_event);
1200 #endif
1202 return 0;
1204 return retval;
1207 void cleanup_drivers(void)
1209 hub_cleanup();
1210 #ifdef CONFIG_USB_MOUSE
1211 usb_mouse_cleanup();
1212 #endif