Import 2.3.11pre6
[davej-history.git] / drivers / usb / uhci.c
blob32b0d41fcaccd0a84e405c82a79c33e0364ce154
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 */
22 /* 5/16/1999 added global toggles for bulk and control */
23 /* 6/25/1999 added fix for data toggles on bidirectional bulk endpoints */
25 #include <linux/config.h>
26 #include <linux/module.h>
27 #include <linux/pci.h>
28 #include <linux/kernel.h>
29 #include <linux/delay.h>
30 #include <linux/ioport.h>
31 #include <linux/sched.h>
32 #include <linux/malloc.h>
33 #include <linux/smp_lock.h>
34 #include <linux/errno.h>
35 #include <linux/unistd.h>
37 #include <asm/uaccess.h>
38 #include <asm/spinlock.h>
39 #include <asm/io.h>
40 #include <asm/irq.h>
41 #include <asm/system.h>
43 #include "uhci.h"
45 #ifdef CONFIG_APM
46 #include <linux/apm_bios.h>
47 static int handle_apm_event(apm_event_t event);
48 static int apm_resume = 0;
49 #endif
51 static int uhci_debug = 1;
53 #define compile_assert(x) do { switch (0) { case 1: case !(x): } } while (0)
55 static DECLARE_WAIT_QUEUE_HEAD(uhci_configure);
58 * Map status to standard result codes
60 static int uhci_map_status(int status, int dir_out)
62 if (!status)
63 return USB_ST_NOERROR;
64 if (status & 0x02) /* Bitstuff error*/
65 return USB_ST_BITSTUFF;
66 if (status & 0x04) { /* CRC/Timeout */
67 if (dir_out)
68 return USB_ST_NORESPONSE;
69 else
70 return USB_ST_CRC;
72 if (status & 0x08) /* NAK */
73 return USB_ST_TIMEOUT;
74 if (status & 0x10) /* Babble */
75 return USB_ST_STALL;
76 if (status & 0x20) /* Buffer error */
77 return USB_ST_BUFFERUNDERRUN;
78 if (status & 0x40) /* Stalled */
79 return USB_ST_STALL;
80 if (status & 0x80) /* Active */
81 return USB_ST_NOERROR;
82 return USB_ST_INTERNALERROR;
85 * Return the result of a TD..
87 static int uhci_td_result(struct uhci_device *dev, struct uhci_td *td, unsigned long *rval)
89 unsigned int status;
90 struct uhci_td *tmp = td->first;
92 if(rval)
93 *rval = 0;
95 /* locate the first failing td, if any */
97 do {
98 status = (tmp->status >> 16) & 0xff;
99 if (status) {
100 /* must reset the toggle on first error */
101 if (uhci_debug) {
102 printk("Set toggle from %x rval %ld\n", (unsigned int)tmp, rval ? *rval : 0);
104 usb_settoggle(dev->usb, usb_pipeendpoint(tmp->info), usb_pipeout(tmp->info), (tmp->info >> 19) & 1);
105 break;
106 } else {
107 if(rval)
108 *rval += (tmp->status & 0x3ff) + 1;
110 if ((tmp->link & 1) || (tmp->link & 2))
111 break;
112 tmp = bus_to_virt(tmp->link & ~0xF);
113 } while (1);
116 if (!status)
117 return USB_ST_NOERROR;
119 /* Some debugging code */
120 if (uhci_debug /* && (!usb_pipeendpoint(tmp->info) || !(status & 0x08))*/ ) {
121 int i = 10;
123 tmp = td->first;
124 printk("uhci_td_result() failed with status %x\n", status);
125 //show_status(dev->uhci);
126 do {
127 show_td(tmp);
128 if ((tmp->link & 1) || (tmp->link & 2))
129 break;
130 tmp = bus_to_virt(tmp->link & ~0xF);
131 if (!--i)
132 break;
133 } while (1);
136 if (status & 0x40) {
137 /* endpoint has stalled - mark it halted */
139 usb_endpoint_halt(dev->usb, usb_pipeendpoint(tmp->info));
140 return USB_ST_STALL;
144 if (status == 0x80) {
145 /* still active */
146 if (!rval)
147 return USB_ST_DATAUNDERRUN;
149 return uhci_map_status(status, usb_pipeout(tmp->info));
153 * Inserts a td into qh list at the top.
155 * Careful about atomicity: even on UP this
156 * requires a locked access due to the concurrent
157 * DMA engine.
159 * NOTE! This assumes that first->last is a valid
160 * list of TD's with the proper backpointers set
161 * up and all..
163 static void uhci_insert_tds_in_qh(struct uhci_qh *qh, struct uhci_td *first, struct uhci_td *last)
165 unsigned int link = qh->element;
166 unsigned int new = 4 | virt_to_bus(first);
168 for (;;) {
169 unsigned char success;
171 last->link = link;
172 first->backptr = &qh->element;
173 asm volatile("lock ; cmpxchg %4,%2 ; sete %0"
174 :"=q" (success), "=a" (link)
175 :"m" (qh->element), "1" (link), "r" (new)
176 :"memory");
177 if (success) {
178 /* Was there a successor entry? Fix it's backpointer.. */
179 if ((link & 1) == 0) {
180 struct uhci_td *next = bus_to_virt(link & ~15);
181 next->backptr = &last->link;
183 break;
188 static inline void uhci_insert_td_in_qh(struct uhci_qh *qh, struct uhci_td *td)
190 uhci_insert_tds_in_qh(qh, td, td);
193 static void uhci_insert_qh(struct uhci_qh *qh, struct uhci_qh *newqh)
195 newqh->link = qh->link;
196 qh->link = virt_to_bus(newqh) | 2;
199 static void uhci_remove_qh(struct uhci_qh *qh, struct uhci_qh *remqh)
201 unsigned int remphys = virt_to_bus(remqh);
202 struct uhci_qh *lqh = qh;
204 while ((lqh->link & ~0xF) != remphys) {
205 if (lqh->link & 1)
206 break;
208 lqh = bus_to_virt(lqh->link & ~0xF);
211 if (lqh->link & 1) {
212 printk("couldn't find qh in chain!\n");
213 return;
216 lqh->link = remqh->link;
220 * Removes td from qh if present.
222 * NOTE! We keep track of both forward and back-pointers,
223 * so this should be trivial, right?
225 * Wrong. While all TD insert/remove operations are synchronous
226 * on the CPU, the UHCI controller can (and does) play with the
227 * very first forward pointer. So we need to validate the backptr
228 * before we change it, so that we don't by mistake reset the QH
229 * head to something old.
231 static void uhci_remove_td(struct uhci_td *td)
233 unsigned int *backptr = td->backptr;
234 unsigned int link = td->link;
235 unsigned int me;
237 if (!backptr)
238 return;
240 td->backptr = NULL;
243 * This is the easy case: the UHCI will never change "td->link",
244 * so we can always just look at that and fix up the backpointer
245 * of any next element..
247 if (!(link & 1)) {
248 struct uhci_td *next = bus_to_virt(link & ~15);
249 next->backptr = backptr;
253 * The nasty case is "backptr->next", which we need to
254 * update to "link" _only_ if "backptr" still points
255 * to us (it may not: maybe backptr is a QH->element
256 * pointer and the UHCI has changed the value).
258 me = virt_to_bus(td) | (0xe & *backptr);
259 asm volatile("lock ; cmpxchg %0,%1"
261 :"r" (link), "m" (*backptr), "a" (me)
262 :"memory");
265 static struct uhci_qh *uhci_qh_allocate(struct uhci_device *dev)
267 struct uhci_qh *qh;
268 int inuse;
270 qh = dev->qh;
271 for (; (inuse = test_and_set_bit(0, &qh->inuse)) != 0 && qh < &dev->qh[UHCI_MAXQH]; qh++)
274 if (!inuse)
275 return(qh);
277 printk("ran out of qh's for dev %p\n", dev);
278 return(NULL);
281 static void uhci_qh_deallocate(struct uhci_qh *qh)
283 // if (qh->element != 1)
284 // printk("qh %p leaving dangling entries? (%X)\n", qh, qh->element);
286 qh->element = 1;
287 qh->link = 1;
289 clear_bit(0, &qh->inuse);
292 static struct uhci_td *uhci_td_allocate(struct uhci_device *dev)
294 struct uhci_td *td;
295 int inuse;
297 td = dev->td;
298 for (; (inuse = test_and_set_bit(0, &td->inuse)) != 0 && td < &dev->td[UHCI_MAXTD]; td++)
301 if (!inuse) {
302 td->inuse = 1;
303 return(td);
306 printk("ran out of td's for dev %p\n", dev);
307 return(NULL);
311 * This MUST only be called when it has been removed from a QH already (or
312 * the QH has been removed from the skeleton
314 static void uhci_td_deallocate(struct uhci_td *td)
316 td->link = 1;
318 clear_bit(0, &td->inuse);
322 * UHCI interrupt list operations..
324 static spinlock_t irqlist_lock = SPIN_LOCK_UNLOCKED;
326 static void uhci_add_irq_list(struct uhci *uhci, struct uhci_td *td, usb_device_irq completed, void *dev_id)
328 unsigned long flags;
330 td->completed = completed;
331 td->dev_id = dev_id;
333 spin_lock_irqsave(&irqlist_lock, flags);
334 list_add(&td->irq_list, &uhci->interrupt_list);
335 spin_unlock_irqrestore(&irqlist_lock, flags);
338 static void uhci_remove_irq_list(struct uhci_td *td)
340 unsigned long flags;
342 spin_lock_irqsave(&irqlist_lock, flags);
343 list_del(&td->irq_list);
344 spin_unlock_irqrestore(&irqlist_lock, flags);
349 * Request a interrupt handler..
351 * Returns: a "handle pointer" that release_irq can use to stop this
352 * interrupt. (It's really a pointer to the TD).
354 static void* uhci_request_irq(struct usb_device *usb_dev, unsigned int pipe, usb_device_irq handler, int period, void *dev_id)
356 struct uhci_device *dev = usb_to_uhci(usb_dev);
357 struct uhci_device *root_hub=usb_to_uhci(dev->uhci->bus->root_hub);
358 struct uhci_td *td = uhci_td_allocate(dev);
359 struct uhci_qh *interrupt_qh = uhci_qh_allocate(dev);
361 unsigned int destination, status;
363 /* Destination: pipe destination with INPUT */
364 destination = (pipe & PIPE_DEVEP_MASK) | 0x69;
366 /* Status: slow/fast, Interrupt, Active, Short Packet Detect Infinite Errors */
367 status = (pipe & (1 << 26)) | (1 << 24) | (1 << 23) | (1 << 29) | (0 << 27);
369 if(interrupt_qh->element != 1)
370 printk("interrupt_qh->element = 0x%x\n",
371 interrupt_qh->element);
373 td->link = 1;
374 td->status = status; /* In */
375 td->info = destination | ((usb_maxpacket(usb_dev, pipe) - 1) << 21) |
376 (usb_gettoggle(usb_dev, usb_pipeendpoint(pipe), usb_pipeout(pipe)) << 19);
377 td->buffer = virt_to_bus(dev->data);
378 td->first = td;
379 td->qh = interrupt_qh;
380 td->dev = usb_dev;
382 /* if period 0, insert into fast q */
384 if (period == 0) {
385 td->inuse |= 2;
386 interrupt_qh->skel = &root_hub->skel_int2_qh;
387 } else
388 interrupt_qh->skel = &root_hub->skel_int8_qh;
390 uhci_add_irq_list(dev->uhci, td, handler, dev_id);
392 uhci_insert_td_in_qh(interrupt_qh, td);
394 /* Add it into the skeleton */
395 uhci_insert_qh(interrupt_qh->skel, interrupt_qh);
397 return (void*)td;
401 * Remove running irq td from queues
403 * This function is not used anymore.
405 #if 0
406 static int uhci_remove_irq(struct usb_device *usb_dev, unsigned int pipe, usb_device_irq handler, int period, void *dev_id)
408 struct uhci_device *dev = usb_to_uhci(usb_dev);
409 struct uhci_device *root_hub=usb_to_uhci(dev->uhci->bus->root_hub);
410 struct uhci_td *td;
411 struct uhci_qh *interrupt_qh;
412 unsigned long flags;
413 struct list_head *head = &dev->uhci->interrupt_list;
414 struct list_head *tmp;
416 spin_lock_irqsave(&irqlist_lock, flags);
418 /* find the TD in the interrupt list */
420 tmp = head->next;
421 while (tmp != head) {
422 td = list_entry(tmp, struct uhci_td, irq_list);
423 if (td->dev_id == dev_id && td->completed == handler) {
425 /* found the right one - let's remove it */
427 /* notify removal */
429 td->completed(USB_ST_REMOVED, NULL, 0, td->dev_id);
431 /* this is DANGEROUS - not sure whether this is right */
433 list_del(&td->irq_list);
434 uhci_remove_td(td);
435 interrupt_qh = td->qh;
436 uhci_remove_qh(interrupt_qh->skel, interrupt_qh);
437 uhci_td_deallocate(td);
438 uhci_qh_deallocate(interrupt_qh);
439 spin_unlock_irqrestore(&irqlist_lock, flags);
440 return USB_ST_NOERROR;
443 spin_unlock_irqrestore(&irqlist_lock, flags);
444 return USB_ST_INTERNALERROR;
446 #endif
449 * Release an interrupt handler previously allocated using
450 * uhci_request_irq. This function does no validity checking, so make
451 * sure you're not releasing an already released handle as it may be
452 * in use by something else..
454 * This function can NOT be called from an interrupt.
456 int uhci_release_irq(void* handle)
458 struct uhci_td *td;
459 struct uhci_qh *interrupt_qh;
460 unsigned long flags;
462 #ifdef UHCI_DEBUG
463 printk("usb-uhci: Releasing irq handle %p\n", handle);
464 #endif
466 td = (struct uhci_td*)handle;
467 if (td == NULL)
468 return USB_ST_INTERNALERROR;
470 /* Remove it from the internal irq_list */
471 spin_lock_irqsave(&irqlist_lock, flags);
472 list_del(&td->irq_list);
473 spin_unlock_irqrestore(&irqlist_lock, flags);
475 /* Remove the interrupt TD and QH */
476 uhci_remove_td(td);
477 interrupt_qh = td->qh;
478 uhci_remove_qh(interrupt_qh->skel, interrupt_qh);
480 if (td->completed != NULL)
481 td->completed(USB_ST_REMOVED, NULL, 0, td->dev_id);
483 /* Free the TD and QH */
484 uhci_td_deallocate(td);
485 uhci_qh_deallocate(interrupt_qh);
487 return USB_ST_NOERROR;
488 } /* uhci_release_irq() */
492 * Isochronous thread operations
495 static int uhci_compress_isochronous(struct usb_device *usb_dev, void *_isodesc)
497 struct uhci_iso_td *isodesc = (struct uhci_iso_td *)_isodesc;
498 char *data = isodesc->data;
499 int i, totlen = 0;
501 for (i = 0; i < isodesc->num; i++) {
502 char *cdata = bus_to_virt(isodesc->td[i].buffer & ~0xF);
503 int n = (isodesc->td[i].status + 1) & 0x7FF;
505 if ((cdata != data) && (n))
506 memmove(data, cdata, n);
508 #if 0
509 if (n && n != 960)
510 printk("underrun: %d %d\n", i, n);
511 #endif
512 if ((isodesc->td[i].status >> 16) & 0xFF)
513 printk("error: %d %X\n", i, (isodesc->td[i].status >> 16));
515 data += n;
516 totlen += n;
519 return totlen;
522 static int uhci_unschedule_isochronous(struct usb_device *usb_dev, void *_isodesc)
524 struct uhci_device *dev = usb_to_uhci(usb_dev);
525 struct uhci *uhci = dev->uhci;
526 struct uhci_iso_td *isodesc = (struct uhci_iso_td *)_isodesc;
527 int i;
529 if ((isodesc->frame < 0) || (isodesc->frame > 1023))
530 return 1;
532 /* Remove from previous frames */
533 for (i = 0; i < isodesc->num; i++) {
534 /* Turn off Active and IOC bits */
535 isodesc->td[i].status &= ~(3 << 23);
536 uhci->fl->frame[(isodesc->frame + i) % 1024] = isodesc->td[i].link;
539 isodesc->frame = -1;
541 return 0;
544 /* td points to the one td we allocated for isochronous transfers */
545 static int uhci_schedule_isochronous(struct usb_device *usb_dev, void *_isodesc, void *_pisodesc)
547 struct uhci_device *dev = usb_to_uhci(usb_dev);
548 struct uhci *uhci = dev->uhci;
549 struct uhci_iso_td *isodesc = (struct uhci_iso_td *)_isodesc;
550 struct uhci_iso_td *pisodesc = (struct uhci_iso_td *)_pisodesc;
551 int frame, i;
553 if (isodesc->frame != -1) {
554 printk("isoc queue not removed\n");
555 uhci_unschedule_isochronous(usb_dev, isodesc);
558 /* Insert TD into list */
559 if (!pisodesc) {
560 frame = inw(uhci->io_addr + USBFRNUM) % 1024;
561 /* HACK: Start 2 frames from now */
562 frame = (frame + 2) % 1024;
563 } else
564 frame = (pisodesc->endframe + 1) % 1024;
566 #if 0
567 printk("scheduling first at frame %d\n", frame);
568 #endif
570 for (i = 0; i < isodesc->num; i++) {
571 /* Active */
572 isodesc->td[i].status |= (1 << 23);
573 isodesc->td[i].backptr = &uhci->fl->frame[(frame + i) % 1024];
574 isodesc->td[i].link = uhci->fl->frame[(frame + i) % 1024];
575 uhci->fl->frame[(frame + i) % 1024] = virt_to_bus(&isodesc->td[i]);
578 #if 0
579 printk("last at frame %d\n", (frame + i - 1) % 1024);
580 #endif
582 /* Interrupt */
583 isodesc->td[i - 1].status |= (1 << 24);
585 isodesc->frame = frame;
586 isodesc->endframe = (frame + isodesc->num - 1) % 1024;
588 #if 0
589 return uhci_td_result(dev, td[num - 1]);
590 #endif
591 return 0;
595 * Initialize isochronous queue
597 static void *uhci_allocate_isochronous(struct usb_device *usb_dev, unsigned int pipe, void *data, int len, int maxsze, usb_device_irq completed, void *dev_id)
599 struct uhci_device *dev = usb_to_uhci(usb_dev);
600 unsigned long destination, status;
601 struct uhci_td *td;
602 struct uhci_iso_td *isodesc;
603 int i;
605 isodesc = kmalloc(sizeof(*isodesc), GFP_KERNEL);
606 if (!isodesc) {
607 printk("Couldn't allocate isodesc!\n");
608 return NULL;
610 memset(isodesc, 0, sizeof(*isodesc));
612 /* Carefully work around the non contiguous pages */
613 isodesc->num = (len / PAGE_SIZE) * (PAGE_SIZE / maxsze);
614 isodesc->td = kmalloc(sizeof(struct uhci_td) * isodesc->num, GFP_KERNEL);
615 isodesc->frame = isodesc->endframe = -1;
616 isodesc->data = data;
617 isodesc->maxsze = maxsze;
619 if (!isodesc->td) {
620 printk("Couldn't allocate td's\n");
621 kfree(isodesc);
622 return NULL;
625 isodesc->frame = isodesc->endframe = -1;
628 * Build the DATA TD's
630 i = 0;
631 do {
632 /* Build the TD for control status */
633 td = &isodesc->td[i];
635 /* The "pipe" thing contains the destination in bits 8--18 */
636 destination = (pipe & PIPE_DEVEP_MASK);
638 if (usb_pipeout(pipe))
639 destination |= 0xE1; /* OUT */
640 else
641 destination |= 0x69; /* IN */
643 /* Status: slow/fast, Active, Isochronous */
644 status = (pipe & (1 << 26)) | (1 << 23) | (1 << 25);
647 * Build the TD for the control request
649 td->status = status;
650 td->info = destination | ((maxsze - 1) << 21);
651 td->buffer = virt_to_bus(data);
652 td->first = td;
653 td->backptr = NULL;
655 i++;
657 data += maxsze;
659 if (((int)data % PAGE_SIZE) + maxsze >= PAGE_SIZE)
660 data = (char *)(((int)data + maxsze) & ~(PAGE_SIZE - 1));
662 len -= maxsze;
663 } while (i < isodesc->num);
665 /* IOC on the last TD */
666 td->status |= (1 << 24);
667 uhci_add_irq_list(dev->uhci, td, completed, dev_id);
669 return isodesc;
672 static void uhci_delete_isochronous(struct usb_device *usb_dev, void *_isodesc)
674 struct uhci_iso_td *isodesc = (struct uhci_iso_td *)_isodesc;
676 /* If it's still scheduled, unschedule them */
677 if (isodesc->frame)
678 uhci_unschedule_isochronous(usb_dev, isodesc);
680 /* Remove it from the IRQ list */
681 uhci_remove_irq_list(&isodesc->td[isodesc->num - 1]);
683 kfree(isodesc->td);
684 kfree(isodesc);
688 * Control thread operations: we just mark the last TD
689 * in a control thread as an interrupt TD, and wake up
690 * the front-end on completion.
692 * We need to remove the TD from the lists (both interrupt
693 * list and TD lists) by hand if something bad happens!
695 static DECLARE_WAIT_QUEUE_HEAD(control_wakeup);
697 static int uhci_control_completed(int status, void *buffer, int len, void *dev_id)
699 wake_up(&control_wakeup);
700 return 0; /* Don't re-instate */
703 /* td points to the last td in the list, which interrupts on completion */
704 static int uhci_run_control(struct uhci_device *dev, struct uhci_td *first, struct uhci_td *last)
706 DECLARE_WAITQUEUE(wait, current);
707 struct uhci_qh *ctrl_qh = uhci_qh_allocate(dev);
708 struct uhci_td *curtd;
709 struct uhci_device *root_hub=usb_to_uhci(dev->uhci->bus->root_hub);
710 current->state = TASK_UNINTERRUPTIBLE;
711 add_wait_queue(&control_wakeup, &wait);
713 uhci_add_irq_list(dev->uhci, last, uhci_control_completed, NULL);
715 /* FIXME: This is kinda kludged */
716 /* Walk the TD list and update the QH pointer */
718 int maxcount = 100;
720 curtd = first;
721 do {
722 curtd->qh = ctrl_qh;
723 if (curtd->link & 1)
724 break;
726 curtd = bus_to_virt(curtd->link & ~0xF);
727 if (!--maxcount) {
728 printk("runaway tds!\n");
729 break;
731 } while (1);
734 uhci_insert_tds_in_qh(ctrl_qh, first, last);
736 /* Add it into the skeleton */
737 uhci_insert_qh(&root_hub->skel_control_qh, ctrl_qh);
739 // control should be full here...
740 // printk("control\n");
741 // show_status(dev->uhci);
742 // show_queues(dev->uhci);
744 schedule_timeout(HZ*5);
746 // control should be empty here...
747 // show_status(dev->uhci);
748 // show_queues(dev->uhci);
750 remove_wait_queue(&control_wakeup, &wait);
752 /* Clean up in case it failed.. */
753 uhci_remove_irq_list(last);
755 #if 0
756 printk("Looking for tds [%p, %p]\n", dev->control_td, td);
757 #endif
759 /* Remove it from the skeleton */
760 uhci_remove_qh(&root_hub->skel_control_qh, ctrl_qh);
762 uhci_qh_deallocate(ctrl_qh);
764 return uhci_td_result(dev, last, NULL);
768 * Send or receive a control message on a pipe.
770 * Note that the "pipe" structure is set up to map
771 * easily to the uhci destination fields.
773 * A control message is built up from three parts:
774 * - The command itself
775 * - [ optional ] data phase
776 * - Status complete phase
778 * The data phase can be an arbitrary number of TD's
779 * although we currently had better not have more than
780 * 29 TD's here (we have 31 TD's allocated for control
781 * operations, and two of them are used for command and
782 * status).
784 * 29 TD's is a minimum of 232 bytes worth of control
785 * information, that's just ridiculously high. Most
786 * control messages have just a few bytes of data.
788 static int uhci_control_msg(struct usb_device *usb_dev, unsigned int pipe, devrequest *cmd, void *data, int len)
790 struct uhci_device *dev = usb_to_uhci(usb_dev);
791 struct uhci_td *first, *td, *prevtd;
792 unsigned long destination, status;
793 int ret;
794 int maxsze = usb_maxpacket(usb_dev, pipe);
796 if (len > maxsze * 29)
797 printk("Warning, too much data for a control packet, crashing\n");
799 first = td = uhci_td_allocate(dev);
801 /* The "pipe" thing contains the destination in bits 8--18, 0x2D is SETUP */
802 destination = (pipe & PIPE_DEVEP_MASK) | 0x2D;
804 /* Status: slow/fast, Active, Short Packet Detect Three Errors */
805 status = (pipe & (1 << 26)) | (1 << 23) | (1 << 29) | (3 << 27);
808 * Build the TD for the control request
810 td->status = status; /* Try forever */
811 td->info = destination | (7 << 21); /* 8 bytes of data */
812 td->buffer = virt_to_bus(cmd);
813 td->first = td;
816 * If direction is "send", change the frame from SETUP (0x2D)
817 * to OUT (0xE1). Else change it from SETUP to IN (0x69)
819 destination ^= (0x2D ^ 0x69); /* SETUP -> IN */
820 if (usb_pipeout(pipe))
821 destination ^= (0xE1 ^ 0x69); /* IN -> OUT */
823 prevtd = td;
824 td = uhci_td_allocate(dev);
825 prevtd->link = 4 | virt_to_bus(td);
828 * Build the DATA TD's
830 while (len > 0) {
831 /* Build the TD for control status */
832 int pktsze = len;
834 if (pktsze > maxsze)
835 pktsze = maxsze;
837 /* Alternate Data0/1 (start with Data1) */
838 destination ^= 1 << 19;
840 td->status = status; /* Status */
841 td->info = destination | ((pktsze-1) << 21); /* pktsze bytes of data */
842 td->buffer = virt_to_bus(data);
843 td->first = first;
844 td->backptr = &prevtd->link;
846 data += pktsze;
847 len -= pktsze;
849 prevtd = td;
850 td = uhci_td_allocate(dev);
851 prevtd->link = 4 | virt_to_bus(td); /* Update previous TD */
856 * Build the final TD for control status
858 destination ^= (0xE1 ^ 0x69); /* OUT -> IN */
859 destination |= 1 << 19; /* End in Data1 */
861 td->backptr = &prevtd->link;
862 td->status = (status /* & ~(3 << 27) */) | (1 << 24); /* no limit on final packet */
863 td->info = destination | (UHCI_NULL_DATA_SIZE << 21); /* 0 bytes of data */
864 td->buffer = 0;
865 td->first = first;
866 td->link = 1; /* Terminate */
869 /* Start it up.. */
870 ret = uhci_run_control(dev, first, td);
873 int maxcount = 100;
874 struct uhci_td *curtd = first;
875 unsigned int nextlink;
877 do {
878 nextlink = curtd->link;
879 uhci_remove_td(curtd);
880 uhci_td_deallocate(curtd);
881 if (nextlink & 1) /* Tail? */
882 break;
884 curtd = bus_to_virt(nextlink & ~0xF);
885 if (!--maxcount) {
886 printk("runaway td's!?\n");
887 break;
889 } while (1);
892 if (uhci_debug && ret) {
893 __u8 *p = (__u8 *)cmd;
895 printk("Failed cmd - %02X %02X %02X %02X %02X %02X %02X %02X\n",
896 p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7]);
898 return ret;
903 * Bulk thread operations: we just mark the last TD
904 * in a bulk thread as an interrupt TD, and wake up
905 * the front-end on completion.
907 * We need to remove the TD from the lists (both interrupt
908 * list and TD lists) by hand if something bad happens!
910 static DECLARE_WAIT_QUEUE_HEAD(bulk_wakeup);
912 static int uhci_bulk_completed(int status, void *buffer, int len, void *dev_id)
914 wake_up(&bulk_wakeup);
915 return 0; /* Don't re-instate */
918 /* td points to the last td in the list, which interrupts on completion */
919 static int uhci_run_bulk(struct uhci_device *dev, struct uhci_td *first, struct uhci_td *last, unsigned long *rval)
921 DECLARE_WAITQUEUE(wait, current);
922 struct uhci_qh *bulk_qh = uhci_qh_allocate(dev);
923 struct uhci_td *curtd;
924 struct uhci_device *root_hub=usb_to_uhci(dev->uhci->bus->root_hub);
926 current->state = TASK_UNINTERRUPTIBLE;
927 add_wait_queue(&bulk_wakeup, &wait);
929 uhci_add_irq_list(dev->uhci, last, uhci_bulk_completed, NULL);
931 /* FIXME: This is kinda kludged */
932 /* Walk the TD list and update the QH pointer */
934 int maxcount = 100;
936 curtd = first;
937 do {
938 curtd->qh = bulk_qh;
939 if (curtd->link & 1)
940 break;
942 curtd = bus_to_virt(curtd->link & ~0xF);
943 if (!--maxcount) {
944 printk("runaway tds!\n");
945 break;
947 } while (1);
950 uhci_insert_tds_in_qh(bulk_qh, first, last);
952 /* Add it into the skeleton */
953 uhci_insert_qh(&root_hub->skel_bulk0_qh, bulk_qh);
955 // now we're in the queue... but don't ask WHAT is in there ;-(
956 // printk("bulk\n");
957 // show_status(dev->uhci);
958 // show_queues(dev->uhci);
960 schedule_timeout(HZ*5);
961 // show_status(dev->uhci);
962 // show_queues(dev->uhci);
964 //show_queue(first->qh);
965 remove_wait_queue(&bulk_wakeup, &wait);
967 /* Clean up in case it failed.. */
968 uhci_remove_irq_list(last);
970 #if 0
971 printk("Looking for tds [%p, %p]\n", dev->control_td, td);
972 #endif
974 /* Remove it from the skeleton */
975 uhci_remove_qh(&root_hub->skel_bulk0_qh, bulk_qh);
977 uhci_qh_deallocate(bulk_qh);
979 return uhci_td_result(dev, last, rval);
983 * Send or receive a bulk message on a pipe.
985 * Note that the "pipe" structure is set up to map
986 * easily to the uhci destination fields.
988 * A bulk message is only built up from
989 * the data phase
991 * The data phase can be an arbitrary number of TD's
992 * although we currently had better not have more than
993 * 31 TD's here.
995 * 31 TD's is a minimum of 248 bytes worth of bulk
996 * information.
998 static int uhci_bulk_msg(struct usb_device *usb_dev, unsigned int pipe, void *data, int len, unsigned long *rval)
1000 struct uhci_device *dev = usb_to_uhci(usb_dev);
1001 struct uhci_td *first, *td, *prevtd;
1002 unsigned long destination, status;
1003 int ret;
1004 int maxsze = usb_maxpacket(usb_dev, pipe);
1006 if (usb_endpoint_halted(usb_dev, usb_pipeendpoint(pipe)) &&
1007 usb_clear_halt(usb_dev, usb_pipeendpoint(pipe) | (pipe & 0x80)))
1008 return USB_ST_STALL;
1010 if (len > maxsze * 31)
1011 printk("Warning, too much data for a bulk packet, crashing (%d/%d)\n", len, maxsze);
1013 /* The "pipe" thing contains the destination in bits 8--18, 0x69 is IN */
1015 IS THIS NECCESARY? PERHAPS WE CAN JUST USE THE PIPE
1016 LOOK AT: usb_pipeout and the pipe bits
1017 I FORGOT WHAT IT EXACTLY DOES
1019 if (usb_pipeout(pipe)) {
1020 destination = (pipe & PIPE_DEVEP_MASK) | 0xE1;
1022 else {
1023 destination = (pipe & PIPE_DEVEP_MASK) | 0x69;
1026 /* Status: slow/fast, Active, Short Packet Detect Three Errors */
1027 status = (pipe & (1 << 26)) | (1 << 23) | (1 << 29) | (3 << 27);
1030 * Build the TDs for the bulk request
1032 first = td = uhci_td_allocate(dev);
1033 prevtd = first; //This is fake, but at least it's not NULL
1034 while (len > 0) {
1035 /* Build the TD for control status */
1036 int pktsze = len;
1038 if (pktsze > maxsze)
1039 pktsze = maxsze;
1041 td->status = status; /* Status */
1042 td->info = destination | ((pktsze-1) << 21) |
1043 (usb_gettoggle(usb_dev, usb_pipeendpoint(pipe), usb_pipeout(pipe)) << 19); /* pktsze bytes of data */
1044 td->buffer = virt_to_bus(data);
1045 td->backptr = &prevtd->link;
1046 td->first = first;
1048 data += maxsze;
1049 len -= maxsze;
1051 if (len > 0) {
1052 prevtd = td;
1053 td = uhci_td_allocate(dev);
1054 prevtd->link = 4 | virt_to_bus(td); /* Update previous TD */
1057 /* Alternate Data0/1 (start with Data0) */
1058 usb_dotoggle(usb_dev, usb_pipeendpoint(pipe), usb_pipeout(pipe));
1060 td->link = 1; /* Terminate */
1061 td->status |= (1 << 24); /* IOC */
1063 /* CHANGE DIRECTION HERE! SAVE IT SOMEWHERE IN THE ENDPOINT!!! */
1065 /* Start it up.. */
1066 ret = uhci_run_bulk(dev, first, td, rval);
1069 int maxcount = 100;
1070 struct uhci_td *curtd = first;
1071 unsigned int nextlink;
1073 do {
1074 nextlink = curtd->link;
1075 uhci_remove_td(curtd);
1076 uhci_td_deallocate(curtd);
1077 if (nextlink & 1) /* Tail? */
1078 break;
1080 curtd = bus_to_virt(nextlink & ~0xF);
1081 if (!--maxcount) {
1082 printk("runaway td's!?\n");
1083 break;
1085 } while (1);
1088 return ret;
1091 static struct usb_device *uhci_usb_allocate(struct usb_device *parent)
1093 struct usb_device *usb_dev;
1094 struct uhci_device *dev;
1095 int i;
1097 usb_dev = kmalloc(sizeof(*usb_dev), GFP_KERNEL);
1098 if (!usb_dev)
1099 return NULL;
1101 memset(usb_dev, 0, sizeof(*usb_dev));
1103 dev = kmalloc(sizeof(*dev), GFP_KERNEL);
1104 if (!dev) {
1105 usb_destroy_configuration(usb_dev);
1106 kfree(usb_dev);
1107 return NULL;
1110 /* Initialize "dev" */
1111 memset(dev, 0, sizeof(*dev));
1113 usb_dev->hcpriv = dev;
1114 dev->usb = usb_dev;
1116 usb_dev->parent = parent;
1118 if (parent) {
1119 usb_dev->bus = parent->bus;
1120 dev->uhci = usb_to_uhci(parent)->uhci;
1123 /* Reset the QH's and TD's */
1124 for (i = 0; i < UHCI_MAXQH; i++) {
1125 dev->qh[i].link = 1;
1126 dev->qh[i].element = 1;
1127 dev->qh[i].inuse = 0;
1130 for (i = 0; i < UHCI_MAXTD; i++) {
1131 dev->td[i].link = 1;
1132 dev->td[i].inuse = 0;
1135 return usb_dev;
1138 static int uhci_usb_deallocate(struct usb_device *usb_dev)
1140 struct uhci_device *dev = usb_to_uhci(usb_dev);
1141 int i;
1143 /* There are UHCI_MAXTD preallocated tds */
1144 for (i = 0; i < UHCI_MAXTD; ++i) {
1145 struct uhci_td *td = dev->td + i;
1147 if (td->inuse & 1) {
1148 uhci_remove_td(td);
1150 /* And remove it from the irq list, if it's active */
1151 if (td->status & (1 << 23))
1152 td->status &= ~(1 << 23);
1153 #if 0
1154 uhci_remove_irq_list(td);
1155 #endif
1159 /* Remove the td from any queues */
1160 for (i = 0; i < UHCI_MAXQH; ++i) {
1161 struct uhci_qh *qh = dev->qh + i;
1163 if (qh->inuse & 1)
1164 uhci_remove_qh(qh->skel, qh);
1167 kfree(dev);
1168 usb_destroy_configuration(usb_dev);
1169 kfree(usb_dev);
1171 return 0;
1174 struct usb_operations uhci_device_operations = {
1175 uhci_usb_allocate,
1176 uhci_usb_deallocate,
1177 uhci_control_msg,
1178 uhci_bulk_msg,
1179 uhci_request_irq,
1180 uhci_release_irq,
1181 uhci_allocate_isochronous,
1182 uhci_delete_isochronous,
1183 uhci_schedule_isochronous,
1184 uhci_unschedule_isochronous,
1185 uhci_compress_isochronous
1189 * This is just incredibly fragile. The timings must be just
1190 * right, and they aren't really documented very well.
1192 * Note the short delay between disabling reset and enabling
1193 * the port..
1195 static void uhci_reset_port(unsigned int port)
1197 unsigned short status;
1199 status = inw(port);
1200 outw(status | USBPORTSC_PR, port); /* reset port */
1201 wait_ms(10);
1202 outw(status & ~USBPORTSC_PR, port);
1203 udelay(5);
1205 status = inw(port);
1206 outw(status | USBPORTSC_PE, port); /* enable port */
1207 wait_ms(10);
1209 status = inw(port);
1210 if(!(status & USBPORTSC_PE)) {
1211 outw(status | USBPORTSC_PE, port); /* one more try at enabling port */
1212 wait_ms(50);
1219 * This gets called if the connect status on the root
1220 * hub (and the root hub only) changes.
1222 static void uhci_connect_change(struct uhci *uhci, unsigned int port, unsigned int nr)
1224 struct usb_device *usb_dev;
1225 struct uhci_device *dev;
1226 unsigned short status;
1227 struct uhci_device *root_hub=usb_to_uhci(uhci->bus->root_hub);
1228 printk("uhci_connect_change: called for %d\n", nr);
1231 * Even if the status says we're connected,
1232 * the fact that the status bits changed may
1233 * that we got disconnected and then reconnected.
1235 * So start off by getting rid of any old devices..
1237 usb_disconnect(&root_hub->usb->children[nr]);
1239 status = inw(port);
1241 /* If we have nothing connected, then clear change status and disable the port */
1242 status = (status & ~USBPORTSC_PE) | USBPORTSC_PEC;
1243 if (!(status & USBPORTSC_CCS)) {
1244 outw(status, port);
1245 return;
1249 * Ok, we got a new connection. Allocate a device to it,
1250 * and find out what it wants to do..
1252 usb_dev = uhci_usb_allocate(root_hub->usb);
1253 if (!usb_dev)
1254 return;
1256 dev = usb_dev->hcpriv;
1258 dev->uhci = uhci;
1260 usb_connect(usb_dev);
1262 root_hub->usb->children[nr] = usb_dev;
1264 wait_ms(200); /* wait for powerup */
1265 uhci_reset_port(port);
1267 /* Get speed information */
1268 usb_dev->slow = (inw(port) & USBPORTSC_LSDA) ? 1 : 0;
1271 * Ok, all the stuff specific to the root hub has been done.
1272 * The rest is generic for any new USB attach, regardless of
1273 * hub type.
1275 usb_new_device(usb_dev);
1279 * This gets called when the root hub configuration
1280 * has changed. Just go through each port, seeing if
1281 * there is something interesting happening.
1283 static void uhci_check_configuration(struct uhci *uhci)
1285 struct uhci_device * root_hub=usb_to_uhci(uhci->bus->root_hub);
1286 unsigned int io_addr = uhci->io_addr + USBPORTSC1;
1287 int maxchild = root_hub->usb->maxchild;
1288 int nr = 0;
1290 do {
1291 unsigned short status = inw(io_addr);
1293 if (status & USBPORTSC_CSC)
1294 uhci_connect_change(uhci, io_addr, nr);
1296 nr++; io_addr += 2;
1297 } while (nr < maxchild);
1300 static void uhci_interrupt_notify(struct uhci *uhci)
1302 struct list_head *head = &uhci->interrupt_list;
1303 struct list_head *tmp;
1304 int status;
1306 spin_lock(&irqlist_lock);
1307 tmp = head->next;
1308 while (tmp != head) {
1309 struct uhci_td *td = list_entry(tmp, struct uhci_td, irq_list);
1310 struct list_head *next;
1312 next = tmp->next;
1314 if (!((status = td->status) & (1 << 23)) || /* No longer active? */
1315 ((td->qh->element & ~15) &&
1316 !((status = uhci_link_to_td(td->qh->element)->status) & (1 <<23)) &&
1317 (status & 0x760000) /* is in error state (Stall, db, babble, timeout, bitstuff) */)) {
1318 /* remove from IRQ list */
1319 __list_del(tmp->prev, next);
1320 INIT_LIST_HEAD(tmp);
1321 if (td->completed(uhci_map_status(status, 0), bus_to_virt(td->buffer), -1, td->dev_id)) {
1322 list_add(&td->irq_list, &uhci->interrupt_list);
1324 if (!(td->status & (1 << 25))) {
1325 struct uhci_qh *interrupt_qh = td->qh;
1327 usb_dotoggle(td->dev, usb_pipeendpoint(td->info), usb_pipeout(td->info));
1328 td->info &= ~(1 << 19); /* clear data toggle */
1329 td->info |= usb_gettoggle(td->dev, usb_pipeendpoint(td->info), usb_pipeout(td->info)) << 19; /* toggle between data0 and data1 */
1330 td->status = (td->status & 0x2f000000) | (1 << 23) | (1 << 24); /* active */
1332 /* Remove then readd? Is that necessary */
1333 uhci_remove_td(td);
1334 uhci_insert_td_in_qh(interrupt_qh, td);
1336 } else if (td->inuse & 2) {
1337 struct uhci_qh *interrupt_qh = td->qh;
1338 /* marked for removal */
1339 td->inuse &= ~2;
1340 usb_dotoggle(td->dev, usb_pipeendpoint(td->info), usb_pipeout(td->info));
1341 uhci_remove_qh(interrupt_qh->skel, interrupt_qh);
1342 uhci_qh_deallocate(interrupt_qh);
1343 uhci_td_deallocate(td);
1345 /* If completed wants to not reactivate, then it's */
1346 /* responsible for free'ing the TD's and QH's */
1347 /* or another function (such as run_control) */
1349 tmp = next;
1351 spin_unlock(&irqlist_lock);
1355 * Check port status - Connect Status Change - for
1356 * each of the attached ports (defaults to two ports,
1357 * but at least in theory there can be more of them).
1359 * Wake up the configurator if something happened, we
1360 * can't really do much at interrupt time.
1362 static void uhci_root_hub_events(struct uhci *uhci, unsigned int io_addr)
1364 if (waitqueue_active(&uhci_configure)) {
1365 struct uhci_device * root_hub=usb_to_uhci(uhci->bus->root_hub);
1366 int ports = root_hub->usb->maxchild;
1367 io_addr += USBPORTSC1;
1368 do {
1369 if (inw(io_addr) & USBPORTSC_CSC) {
1370 wake_up(&uhci_configure);
1371 return;
1373 io_addr += 2;
1374 } while (--ports > 0);
1378 static void uhci_interrupt(int irq, void *__uhci, struct pt_regs *regs)
1380 struct uhci *uhci = __uhci;
1381 unsigned int io_addr = uhci->io_addr;
1382 unsigned short status;
1385 * Read the interrupt status, and write it back to clear the interrupt cause
1387 status = inw(io_addr + USBSTS);
1388 outw(status, io_addr + USBSTS);
1390 // if ((status & ~0x21) != 0)
1391 // printk("interrupt: %X\n", status);
1393 /* Walk the list of pending TD's to see which ones completed.. */
1394 uhci_interrupt_notify(uhci);
1396 /* Check if there are any events on the root hub.. */
1397 uhci_root_hub_events(uhci, io_addr);
1401 * We init one packet, and mark it just IOC and _not_
1402 * active. Which will result in no actual USB traffic,
1403 * but _will_ result in an interrupt every second.
1405 * Which is exactly what we want.
1407 static void uhci_init_ticktd(struct uhci *uhci)
1409 struct uhci_device *dev = usb_to_uhci(uhci->bus->root_hub);
1410 struct uhci_td *td = uhci_td_allocate(dev);
1412 td->link = 1;
1413 td->status = (1 << 24); /* interrupt on completion */
1414 td->info = (15 << 21) | 0x7f69; /* (ignored) input packet, 16 bytes, device 127 */
1415 td->buffer = 0;
1416 td->first = td;
1417 td->qh = NULL;
1419 uhci->fl->frame[0] = virt_to_bus(td);
1422 static void reset_hc(struct uhci *uhci)
1424 unsigned int io_addr = uhci->io_addr;
1426 /* Global reset for 50ms */
1427 outw(USBCMD_GRESET, io_addr+USBCMD);
1428 wait_ms(50);
1429 outw(0, io_addr+USBCMD);
1430 wait_ms(10);
1433 static void start_hc(struct uhci *uhci)
1435 unsigned int io_addr = uhci->io_addr;
1436 int timeout = 1000;
1438 uhci_init_ticktd(uhci);
1441 * Reset the HC - this will force us to get a
1442 * new notification of any already connected
1443 * ports due to the virtual disconnect that it
1444 * implies.
1446 outw(USBCMD_HCRESET, io_addr + USBCMD);
1447 while (inw(io_addr + USBCMD) & USBCMD_HCRESET) {
1448 if (!--timeout) {
1449 printk("USBCMD_HCRESET timed out!\n");
1450 break;
1455 outw(USBINTR_TIMEOUT | USBINTR_RESUME | USBINTR_IOC | USBINTR_SP, io_addr + USBINTR);
1456 outw(0, io_addr + USBFRNUM);
1457 outl(virt_to_bus(uhci->fl), io_addr + USBFLBASEADD);
1459 /* Run and mark it configured with a 64-byte max packet */
1460 outw(USBCMD_RS | USBCMD_CF | USBCMD_MAXP, io_addr + USBCMD);
1464 * Allocate a frame list, and four regular queues.
1466 * The hardware doesn't really know any difference
1467 * in the queues, but the order does matter for the
1468 * protocols higher up. The order is:
1470 * - any isochronous events handled before any
1471 * of the queues. We don't do that here, because
1472 * we'll create the actual TD entries on demand.
1473 * - The first queue is the "interrupt queue".
1474 * - The second queue is the "control queue".
1475 * - The third queue is "bulk data".
1477 * We could certainly have multiple queues of the same
1478 * type, and maybe we should. We could have per-device
1479 * queues, for example. We begin small.
1481 static struct uhci *alloc_uhci(unsigned int io_addr)
1483 int i;
1484 struct uhci *uhci;
1485 struct usb_bus *bus;
1486 struct uhci_device *dev;
1487 struct usb_device *usb;
1489 uhci = kmalloc(sizeof(*uhci), GFP_KERNEL);
1490 if (!uhci)
1491 return NULL;
1493 memset(uhci, 0, sizeof(*uhci));
1495 uhci->irq = -1;
1496 uhci->io_addr = io_addr;
1497 INIT_LIST_HEAD(&uhci->interrupt_list);
1499 /* We need exactly one page (per UHCI specs), how convenient */
1500 uhci->fl = (void *)__get_free_page(GFP_KERNEL);
1501 if (!uhci->fl)
1502 goto au_free_uhci;
1504 bus = kmalloc(sizeof(*bus), GFP_KERNEL);
1505 if (!bus)
1506 goto au_free_fl;
1508 memset(bus, 0, sizeof(*bus));
1510 uhci->bus = bus;
1511 bus->hcpriv = uhci;
1512 bus->op = &uhci_device_operations;
1515 * We allocate a 8kB area for the UHCI hub. The area
1516 * is described by the uhci_device structure, and basically
1517 * contains everything needed for normal operation.
1519 * The first page is the actual device descriptor for the
1520 * hub.
1522 * The second page is used for the frame list.
1524 usb = uhci_usb_allocate(NULL);
1525 if (!usb)
1526 goto au_free_bus;
1528 usb->bus = bus;
1529 dev = usb_to_uhci(usb);
1530 uhci->bus->root_hub=uhci_to_usb(dev);
1531 /* Initialize the root hub */
1532 /* UHCI specs says devices must have 2 ports, but goes on to say */
1533 /* they may have more but give no way to determine how many they */
1534 /* have, so default to 2 */
1535 usb->maxchild = 2;
1536 usb_init_root_hub(usb);
1539 * Initialize the queues. They all start out empty,
1540 * linked to each other in the proper order.
1542 for (i = 1 ; i < 9; i++) {
1543 dev->qh[i].link = 2 | virt_to_bus(&dev->skel_control_qh);
1544 dev->qh[i].element = 1;
1547 dev->skel_control_qh.link = 2 | virt_to_bus(&dev->skel_bulk0_qh);
1548 dev->skel_control_qh.element = 1;
1550 dev->skel_bulk0_qh.link = 2 | virt_to_bus(&dev->skel_bulk1_qh);
1551 dev->skel_bulk0_qh.element = 1;
1553 dev->skel_bulk1_qh.link = 2 | virt_to_bus(&dev->skel_bulk2_qh);
1554 dev->skel_bulk1_qh.element = 1;
1556 dev->skel_bulk2_qh.link = 2 | virt_to_bus(&dev->skel_bulk3_qh);
1557 dev->skel_bulk2_qh.element = 1;
1559 dev->skel_bulk3_qh.link = 1;
1560 dev->skel_bulk3_qh.element = 1;
1563 * Fill the frame list: make all entries point to
1564 * the proper interrupt queue.
1566 * This is probably silly, but it's a simple way to
1567 * scatter the interrupt queues in a way that gives
1568 * us a reasonable dynamic range for irq latencies.
1570 for (i = 0; i < 1024; i++) {
1571 struct uhci_qh * irq = &dev->skel_int2_qh;
1572 if (i & 1) {
1573 irq++;
1574 if (i & 2) {
1575 irq++;
1576 if (i & 4) {
1577 irq++;
1578 if (i & 8) {
1579 irq++;
1580 if (i & 16) {
1581 irq++;
1582 if (i & 32) {
1583 irq++;
1584 if (i & 64) {
1585 irq++;
1593 uhci->fl->frame[i] = 2 | virt_to_bus(irq);
1596 return uhci;
1599 * error exits:
1602 au_free_bus:
1603 kfree (bus);
1604 au_free_fl:
1605 free_page ((unsigned long)uhci->fl);
1606 au_free_uhci:
1607 kfree (uhci);
1608 return NULL;
1613 * De-allocate all resources..
1615 static void release_uhci(struct uhci *uhci)
1617 if (uhci->irq >= 0) {
1618 free_irq(uhci->irq, uhci);
1619 uhci->irq = -1;
1622 #if 0
1623 if (uhci->bus->root_hub) {
1624 uhci_usb_deallocate(uhci_to_usb(uhci->bus->root_hub));
1625 uhci->bus->root_hub = NULL;
1627 #endif
1629 if (uhci->fl) {
1630 free_page((unsigned long)uhci->fl);
1631 uhci->fl = NULL;
1634 kfree(uhci->bus);
1635 kfree(uhci);
1638 static int uhci_control_thread(void * __uhci)
1640 struct uhci *uhci = (struct uhci *)__uhci;
1641 struct uhci_device * root_hub =usb_to_uhci(uhci->bus->root_hub);
1643 lock_kernel();
1644 request_region(uhci->io_addr, 32, "usb-uhci");
1647 * This thread doesn't need any user-level access,
1648 * so get rid of all our resources..
1650 printk("uhci_control_thread at %p\n", &uhci_control_thread);
1651 exit_mm(current);
1652 exit_files(current);
1653 //exit_fs(current);
1655 strcpy(current->comm, "uhci-control");
1658 * Ok, all systems are go..
1660 start_hc(uhci);
1661 usb_register_bus(uhci->bus);
1662 for(;;) {
1663 siginfo_t info;
1664 int unsigned long signr;
1666 interruptible_sleep_on(&uhci_configure);
1667 #ifdef CONFIG_APM
1668 if (apm_resume) {
1669 apm_resume = 0;
1670 start_hc(uhci);
1671 continue;
1673 #endif
1674 uhci_check_configuration(uhci);
1676 if(signal_pending(current)) {
1677 /* sending SIGUSR1 makes us print out some info */
1678 spin_lock_irq(&current->sigmask_lock);
1679 signr = dequeue_signal(&current->blocked, &info);
1680 spin_unlock_irq(&current->sigmask_lock);
1682 if(signr == SIGUSR1) {
1683 printk("UHCI queue dump:\n");
1684 show_queues(uhci);
1685 } else if (signr == SIGUSR2) {
1686 uhci_debug = !uhci_debug;
1687 printk("UHCI debug toggle = %x\n", uhci_debug);
1688 } else {
1689 break;
1695 int i;
1696 if(root_hub)
1697 for(i = 0; i < root_hub->usb->maxchild; i++)
1698 usb_disconnect(root_hub->usb->children + i);
1701 usb_deregister_bus(uhci->bus);
1703 reset_hc(uhci);
1704 release_region(uhci->io_addr, 32);
1706 release_uhci(uhci);
1707 MOD_DEC_USE_COUNT;
1709 printk("uhci_control_thread exiting\n");
1711 return 0;
1715 * If we've successfully found a UHCI, now is the time to increment the
1716 * module usage count, start the control thread, and return success..
1718 static int found_uhci(int irq, unsigned int io_addr)
1720 int retval;
1721 struct uhci *uhci;
1723 uhci = alloc_uhci(io_addr);
1724 if (!uhci)
1725 return -ENOMEM;
1727 reset_hc(uhci);
1729 retval = -EBUSY;
1730 if (request_irq(irq, uhci_interrupt, SA_SHIRQ, "usb", uhci) == 0) {
1731 int pid;
1732 MOD_INC_USE_COUNT;
1733 uhci->irq = irq;
1734 pid = kernel_thread(uhci_control_thread, uhci,
1735 CLONE_FS | CLONE_FILES | CLONE_SIGHAND);
1736 if (pid >= 0)
1737 return 0;
1739 MOD_DEC_USE_COUNT;
1740 retval = pid;
1742 release_uhci(uhci);
1743 return retval;
1746 static int start_uhci(struct pci_dev *dev)
1748 int i;
1750 /* Search for the IO base address.. */
1751 for (i = 0; i < 6; i++) {
1752 unsigned int io_addr = dev->base_address[i];
1754 /* IO address? */
1755 if (!(io_addr & 1))
1756 continue;
1758 io_addr &= PCI_BASE_ADDRESS_IO_MASK;
1760 /* Is it already in use? */
1761 if (check_region(io_addr, 32))
1762 break;
1764 return found_uhci(dev->irq, io_addr);
1766 return -1;
1769 #ifdef CONFIG_APM
1770 static int handle_apm_event(apm_event_t event)
1772 static int down = 0;
1774 switch (event) {
1775 case APM_SYS_SUSPEND:
1776 case APM_USER_SUSPEND:
1777 if (down) {
1778 printk(KERN_DEBUG "uhci: received extra suspend event\n");
1779 break;
1781 down = 1;
1782 break;
1783 case APM_NORMAL_RESUME:
1784 case APM_CRITICAL_RESUME:
1785 if (!down) {
1786 printk(KERN_DEBUG "uhci: received bogus resume event\n");
1787 break;
1789 down = 0;
1790 if (waitqueue_active(&uhci_configure)) {
1791 apm_resume = 1;
1792 wake_up(&uhci_configure);
1794 break;
1796 return 0;
1798 #endif
1801 int uhci_init(void)
1803 int retval;
1804 struct pci_dev *dev = NULL;
1805 u8 type;
1807 retval = -ENODEV;
1808 for (;;) {
1809 dev = pci_find_class(PCI_CLASS_SERIAL_USB<<8, dev);
1810 if (!dev)
1811 break;
1812 /* Is it UHCI */
1813 pci_read_config_byte(dev, PCI_CLASS_PROG, &type);
1814 if(type != 0)
1815 continue;
1816 /* Ok set it up */
1817 retval = start_uhci(dev);
1818 if (retval < 0)
1819 continue;
1821 #ifdef CONFIG_APM
1822 apm_register_callback(&handle_apm_event);
1823 #endif
1824 return 0;
1826 return retval;
1829 #ifdef MODULE
1830 int init_module(void)
1832 return uhci_init();
1835 void cleanup_module(void)
1837 #ifdef CONFIG_APM
1838 apm_unregister_callback(&handle_apm_event);
1839 #endif
1841 #endif //MODULE