pre-2.3.4..
[davej-history.git] / drivers / usb / uhci.c
blob95bed335b0a580255d7d82a3e42b612f381925ac
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 */
24 #include <linux/config.h>
25 #include <linux/module.h>
26 #include <linux/pci.h>
27 #include <linux/kernel.h>
28 #include <linux/delay.h>
29 #include <linux/ioport.h>
30 #include <linux/sched.h>
31 #include <linux/malloc.h>
32 #include <linux/smp_lock.h>
33 #include <linux/errno.h>
35 #include <asm/spinlock.h>
36 #include <asm/io.h>
37 #include <asm/irq.h>
38 #include <asm/system.h>
40 #include "uhci.h"
41 #include "inits.h"
43 #ifdef CONFIG_APM
44 #include <linux/apm_bios.h>
45 static int handle_apm_event(apm_event_t event);
46 static int apm_resume = 0;
47 #endif
49 #define compile_assert(x) do { switch (0) { case 1: case !(x): } } while (0)
51 static DECLARE_WAIT_QUEUE_HEAD(uhci_configure);
54 * Return the result of a TD..
56 static int uhci_td_result(struct uhci_device *dev, struct uhci_td *td, unsigned long *rval)
58 unsigned int status;
59 struct uhci_td *tmp = td->first;
61 /* locate the first failing td, if any */
63 do {
64 status = (tmp->status >> 16) & 0xff;
65 if (status)
66 break;
67 if ((tmp->link & 1) || (tmp->link & 2))
68 break;
69 tmp = bus_to_virt(tmp->link & ~0xF);
70 } while (1);
72 if(rval)
73 *rval = 0;
74 /* Some debugging code */
75 if (status && (!usb_pipeendpoint(tmp->info) || !(status & 0x08)) ) {
76 int i = 10;
78 tmp = td->first;
79 printk("uhci_td_result() failed with status %x\n", status);
80 show_status(dev->uhci);
81 do {
82 show_td(tmp);
83 if ((tmp->link & 1) || (tmp->link & 2))
84 break;
85 tmp = bus_to_virt(tmp->link & ~0xF);
86 if (!--i)
87 break;
88 } while (1);
90 if (usb_pipeendpoint(tmp->info) && (status & 0x08)) {
91 // printk("uhci_td_result() - NAK\n");
92 /* find total length xferred and reset toggle on failed packets */
93 tmp = td->first;
94 do {
95 /* sum up packets that did not fail */
96 if(rval && !((tmp->status >> 16) & 0xff))
97 *rval += (tmp->status & 0x3ff) + 1;
99 /*
100 * Note - only the first to fail will be marked NAK
102 if (tmp->status & 0xFF0000) {
103 /* must reset the toggle on any error */
104 usb_settoggle(dev->usb, usb_pipeendpoint(tmp->info), (tmp->info >> 19) & 1);
105 break;
108 if ((tmp->link & 1) || (tmp->link & 2))
109 break;
110 tmp = bus_to_virt(tmp->link & ~0xF);
111 } while (1);
112 #if 0
113 if (rval) {
114 printk("uhci_td_result returning partial count %d\n", *rval);
116 #endif
118 return status;
122 * Inserts a td into qh list at the top.
124 * Careful about atomicity: even on UP this
125 * requires a locked access due to the concurrent
126 * DMA engine.
128 * NOTE! This assumes that first->last is a valid
129 * list of TD's with the proper backpointers set
130 * up and all..
132 static void uhci_insert_tds_in_qh(struct uhci_qh *qh, struct uhci_td *first, struct uhci_td *last)
134 unsigned int link = qh->element;
135 unsigned int new = 4 | virt_to_bus(first);
137 for (;;) {
138 unsigned char success;
140 last->link = link;
141 first->backptr = &qh->element;
142 asm volatile("lock ; cmpxchg %4,%2 ; sete %0"
143 :"=q" (success), "=a" (link)
144 :"m" (qh->element), "1" (link), "r" (new)
145 :"memory");
146 if (success) {
147 /* Was there a successor entry? Fix it's backpointer.. */
148 if ((link & 1) == 0) {
149 struct uhci_td *next = bus_to_virt(link & ~15);
150 next->backptr = &last->link;
152 break;
157 static inline void uhci_insert_td_in_qh(struct uhci_qh *qh, struct uhci_td *td)
159 uhci_insert_tds_in_qh(qh, td, td);
162 static void uhci_insert_qh(struct uhci_qh *qh, struct uhci_qh *newqh)
164 newqh->link = qh->link;
165 qh->link = virt_to_bus(newqh) | 2;
168 static void uhci_remove_qh(struct uhci_qh *qh, struct uhci_qh *remqh)
170 unsigned int remphys = virt_to_bus(remqh);
171 struct uhci_qh *lqh = qh;
173 while ((lqh->link & ~0xF) != remphys) {
174 if (lqh->link & 1)
175 break;
177 lqh = bus_to_virt(lqh->link & ~0xF);
180 if (lqh->link & 1) {
181 printk("couldn't find qh in chain!\n");
182 return;
185 lqh->link = remqh->link;
189 * Removes td from qh if present.
191 * NOTE! We keep track of both forward and back-pointers,
192 * so this should be trivial, right?
194 * Wrong. While all TD insert/remove operations are synchronous
195 * on the CPU, the UHCI controller can (and does) play with the
196 * very first forward pointer. So we need to validate the backptr
197 * before we change it, so that we don't by mistake reset the QH
198 * head to something old.
200 static void uhci_remove_td(struct uhci_td *td)
202 unsigned int *backptr = td->backptr;
203 unsigned int link = td->link;
204 unsigned int me;
206 if (!backptr)
207 return;
209 td->backptr = NULL;
212 * This is the easy case: the UHCI will never change "td->link",
213 * so we can always just look at that and fix up the backpointer
214 * of any next element..
216 if (!(link & 1)) {
217 struct uhci_td *next = bus_to_virt(link & ~15);
218 next->backptr = backptr;
222 * The nasty case is "backptr->next", which we need to
223 * update to "link" _only_ if "backptr" still points
224 * to us (it may not: maybe backptr is a QH->element
225 * pointer and the UHCI has changed the value).
227 me = virt_to_bus(td) | (0xe & *backptr);
228 asm volatile("lock ; cmpxchg %0,%1"
230 :"r" (link), "m" (*backptr), "a" (me)
231 :"memory");
234 static struct uhci_qh *uhci_qh_allocate(struct uhci_device *dev)
236 struct uhci_qh *qh;
237 int inuse;
239 qh = dev->qh;
240 for (; (inuse = test_and_set_bit(0, &qh->inuse)) != 0 && qh < &dev->qh[UHCI_MAXQH]; qh++)
243 if (!inuse)
244 return(qh);
246 printk("ran out of qh's for dev %p\n", dev);
247 return(NULL);
250 static void uhci_qh_deallocate(struct uhci_qh *qh)
252 // if (qh->element != 1)
253 // printk("qh %p leaving dangling entries? (%X)\n", qh, qh->element);
255 qh->element = 1;
256 qh->link = 1;
258 clear_bit(0, &qh->inuse);
261 static struct uhci_td *uhci_td_allocate(struct uhci_device *dev)
263 struct uhci_td *td;
264 int inuse;
266 td = dev->td;
267 for (; (inuse = test_and_set_bit(0, &td->inuse)) != 0 && td < &dev->td[UHCI_MAXTD]; td++)
270 if (!inuse)
271 return(td);
273 printk("ran out of td's for dev %p\n", dev);
274 return(NULL);
278 * This MUST only be called when it has been removed from a QH already (or
279 * the QH has been removed from the skeleton
281 static void uhci_td_deallocate(struct uhci_td *td)
283 td->link = 1;
285 clear_bit(0, &td->inuse);
289 * UHCI interrupt list operations..
291 static spinlock_t irqlist_lock = SPIN_LOCK_UNLOCKED;
293 static void uhci_add_irq_list(struct uhci *uhci, struct uhci_td *td, usb_device_irq completed, void *dev_id)
295 unsigned long flags;
297 td->completed = completed;
298 td->dev_id = dev_id;
300 spin_lock_irqsave(&irqlist_lock, flags);
301 list_add(&td->irq_list, &uhci->interrupt_list);
302 spin_unlock_irqrestore(&irqlist_lock, flags);
305 static void uhci_remove_irq_list(struct uhci_td *td)
307 unsigned long flags;
309 spin_lock_irqsave(&irqlist_lock, flags);
310 list_del(&td->irq_list);
311 spin_unlock_irqrestore(&irqlist_lock, flags);
315 * Request a interrupt handler..
317 static int uhci_request_irq(struct usb_device *usb_dev, unsigned int pipe, usb_device_irq handler, int period, void *dev_id)
319 struct uhci_device *dev = usb_to_uhci(usb_dev);
320 struct uhci_td *td = uhci_td_allocate(dev);
321 struct uhci_qh *interrupt_qh = uhci_qh_allocate(dev);
323 unsigned int destination, status;
325 /* Destination: pipe destination with INPUT */
326 destination = (pipe & 0x0007ff00) | 0x69;
328 /* Status: slow/fast, Interrupt, Active, Short Packet Detect Infinite Errors */
329 status = (pipe & (1 << 26)) | (1 << 24) | (1 << 23) | (1 << 29) | (0 << 27);
331 if(interrupt_qh->element != 1)
332 printk("interrupt_qh->element = 0x%x\n",
333 interrupt_qh->element);
335 td->link = 1;
336 td->status = status; /* In */
337 td->info = destination | (7 << 21); /* 8 bytes of data */
338 td->buffer = virt_to_bus(dev->data);
339 td->first = td;
340 td->qh = interrupt_qh;
341 interrupt_qh->skel = &dev->uhci->root_hub->skel_int8_qh;
343 uhci_add_irq_list(dev->uhci, td, handler, dev_id);
345 uhci_insert_td_in_qh(interrupt_qh, td);
347 /* Add it into the skeleton */
348 uhci_insert_qh(&dev->uhci->root_hub->skel_int8_qh, interrupt_qh);
349 return 0;
353 * Isochronous thread operations
356 int uhci_compress_isochronous(struct usb_device *usb_dev, void *_isodesc)
358 struct uhci_iso_td *isodesc = (struct uhci_iso_td *)_isodesc;
359 char *data = isodesc->data;
360 int i, totlen = 0;
362 for (i = 0; i < isodesc->num; i++) {
363 char *cdata = bus_to_virt(isodesc->td[i].buffer & ~0xF);
364 int n = (isodesc->td[i].status + 1) & 0x7FF;
366 if ((cdata != data) && (n))
367 memmove(data, cdata, n);
369 #if 0
370 if (n && n != 960)
371 printk("underrun: %d %d\n", i, n);
372 #endif
373 if ((isodesc->td[i].status >> 16) & 0xFF)
374 printk("error: %d %X\n", i, (isodesc->td[i].status >> 16));
376 data += n;
377 totlen += n;
380 return totlen;
383 int uhci_unsched_isochronous(struct usb_device *usb_dev, void *_isodesc)
385 struct uhci_device *dev = usb_to_uhci(usb_dev);
386 struct uhci *uhci = dev->uhci;
387 struct uhci_iso_td *isodesc = (struct uhci_iso_td *)_isodesc;
388 int i;
390 if ((isodesc->frame < 0) || (isodesc->frame > 1023))
391 return 1;
393 /* Remove from previous frames */
394 for (i = 0; i < isodesc->num; i++) {
395 /* Turn off Active and IOC bits */
396 isodesc->td[i].status &= ~(3 << 23);
397 uhci->fl->frame[(isodesc->frame + i) % 1024] = isodesc->td[i].link;
400 isodesc->frame = -1;
402 return 0;
405 /* td points to the one td we allocated for isochronous transfers */
406 int uhci_sched_isochronous(struct usb_device *usb_dev, void *_isodesc, void *_pisodesc)
408 struct uhci_device *dev = usb_to_uhci(usb_dev);
409 struct uhci *uhci = dev->uhci;
410 struct uhci_iso_td *isodesc = (struct uhci_iso_td *)_isodesc;
411 struct uhci_iso_td *pisodesc = (struct uhci_iso_td *)_pisodesc;
412 int frame, i;
414 if (isodesc->frame != -1) {
415 printk("isoc queue not removed\n");
416 uhci_unsched_isochronous(usb_dev, isodesc);
419 /* Insert TD into list */
420 if (!pisodesc) {
421 frame = inw(uhci->io_addr + USBFRNUM) % 1024;
422 /* HACK: Start 2 frames from now */
423 frame = (frame + 2) % 1024;
424 } else
425 frame = (pisodesc->endframe + 1) % 1024;
427 #if 0
428 printk("scheduling first at frame %d\n", frame);
429 #endif
431 for (i = 0; i < isodesc->num; i++) {
432 /* Active */
433 isodesc->td[i].status |= (1 << 23);
434 isodesc->td[i].backptr = &uhci->fl->frame[(frame + i) % 1024];
435 isodesc->td[i].link = uhci->fl->frame[(frame + i) % 1024];
436 uhci->fl->frame[(frame + i) % 1024] = virt_to_bus(&isodesc->td[i]);
439 #if 0
440 printk("last at frame %d\n", (frame + i - 1) % 1024);
441 #endif
443 /* Interrupt */
444 isodesc->td[i - 1].status |= (1 << 24);
446 isodesc->frame = frame;
447 isodesc->endframe = (frame + isodesc->num - 1) % 1024;
449 #if 0
450 return uhci_td_result(dev, td[num - 1]);
451 #endif
452 return 0;
456 * Initialize isochronous queue
458 void *uhci_alloc_isochronous(struct usb_device *usb_dev, unsigned int pipe, void *data, int len, int maxsze, usb_device_irq completed, void *dev_id)
460 struct uhci_device *dev = usb_to_uhci(usb_dev);
461 unsigned long destination, status;
462 struct uhci_td *td;
463 struct uhci_iso_td *isodesc;
464 int i;
466 isodesc = kmalloc(sizeof(*isodesc), GFP_KERNEL);
467 if (!isodesc) {
468 printk("Couldn't allocate isodesc!\n");
469 return NULL;
471 memset(isodesc, 0, sizeof(*isodesc));
473 /* Carefully work around the non contiguous pages */
474 isodesc->num = (len / PAGE_SIZE) * (PAGE_SIZE / maxsze);
475 isodesc->td = kmalloc(sizeof(struct uhci_td) * isodesc->num, GFP_KERNEL);
476 isodesc->frame = isodesc->endframe = -1;
477 isodesc->data = data;
478 isodesc->maxsze = maxsze;
480 if (!isodesc->td) {
481 printk("Couldn't allocate td's\n");
482 kfree(isodesc);
483 return NULL;
486 isodesc->frame = isodesc->endframe = -1;
489 * Build the DATA TD's
491 i = 0;
492 do {
493 /* Build the TD for control status */
494 td = &isodesc->td[i];
496 /* The "pipe" thing contains the destination in bits 8--18 */
497 destination = (pipe & 0x0007ff00);
499 if (usb_pipeout(pipe))
500 destination |= 0xE1; /* OUT */
501 else
502 destination |= 0x69; /* IN */
504 /* Status: slow/fast, Active, Isochronous */
505 status = (pipe & (1 << 26)) | (1 << 23) | (1 << 25);
508 * Build the TD for the control request
510 td->status = status;
511 td->info = destination | ((maxsze - 1) << 21);
512 td->buffer = virt_to_bus(data);
513 td->first = td;
514 td->backptr = NULL;
516 i++;
518 data += maxsze;
520 if (((int)data % PAGE_SIZE) + maxsze >= PAGE_SIZE)
521 data = (char *)(((int)data + maxsze) & ~(PAGE_SIZE - 1));
523 len -= maxsze;
524 } while (i < isodesc->num);
526 /* IOC on the last TD */
527 td->status |= (1 << 24);
528 uhci_add_irq_list(dev->uhci, td, completed, dev_id);
530 return isodesc;
533 void uhci_delete_isochronous(struct usb_device *usb_dev, void *_isodesc)
535 struct uhci_iso_td *isodesc = (struct uhci_iso_td *)_isodesc;
537 /* If it's still scheduled, unschedule them */
538 if (isodesc->frame)
539 uhci_unsched_isochronous(usb_dev, isodesc);
541 /* Remove it from the IRQ list */
542 uhci_remove_irq_list(&isodesc->td[isodesc->num - 1]);
544 kfree(isodesc->td);
545 kfree(isodesc);
549 * Control thread operations: we just mark the last TD
550 * in a control thread as an interrupt TD, and wake up
551 * the front-end on completion.
553 * We need to remove the TD from the lists (both interrupt
554 * list and TD lists) by hand if something bad happens!
556 static DECLARE_WAIT_QUEUE_HEAD(control_wakeup);
558 static int uhci_control_completed(int status, void *buffer, void *dev_id)
560 wake_up(&control_wakeup);
561 return 0; /* Don't re-instate */
564 /* td points to the last td in the list, which interrupts on completion */
565 static int uhci_run_control(struct uhci_device *dev, struct uhci_td *first, struct uhci_td *last)
567 DECLARE_WAITQUEUE(wait, current);
568 struct uhci_qh *ctrl_qh = uhci_qh_allocate(dev);
569 struct uhci_td *curtd;
571 current->state = TASK_UNINTERRUPTIBLE;
572 add_wait_queue(&control_wakeup, &wait);
574 uhci_add_irq_list(dev->uhci, last, uhci_control_completed, NULL);
576 /* FIXME: This is kinda kludged */
577 /* Walk the TD list and update the QH pointer */
579 int maxcount = 100;
581 curtd = first;
582 do {
583 curtd->qh = ctrl_qh;
584 if (curtd->link & 1)
585 break;
587 curtd = bus_to_virt(curtd->link & ~0xF);
588 if (!--maxcount) {
589 printk("runaway tds!\n");
590 break;
592 } while (1);
595 uhci_insert_tds_in_qh(ctrl_qh, first, last);
597 /* Add it into the skeleton */
598 uhci_insert_qh(&dev->uhci->root_hub->skel_control_qh, ctrl_qh);
600 schedule_timeout(HZ/10);
602 remove_wait_queue(&control_wakeup, &wait);
604 /* Clean up in case it failed.. */
605 uhci_remove_irq_list(last);
607 #if 0
608 printk("Looking for tds [%p, %p]\n", dev->control_td, td);
609 #endif
611 /* Remove it from the skeleton */
612 uhci_remove_qh(&dev->uhci->root_hub->skel_control_qh, ctrl_qh);
614 uhci_qh_deallocate(ctrl_qh);
616 return uhci_td_result(dev, last, NULL);
620 * Send or receive a control message on a pipe.
622 * Note that the "pipe" structure is set up to map
623 * easily to the uhci destination fields.
625 * A control message is built up from three parts:
626 * - The command itself
627 * - [ optional ] data phase
628 * - Status complete phase
630 * The data phase can be an arbitrary number of TD's
631 * although we currently had better not have more than
632 * 29 TD's here (we have 31 TD's allocated for control
633 * operations, and two of them are used for command and
634 * status).
636 * 29 TD's is a minimum of 232 bytes worth of control
637 * information, that's just ridiculously high. Most
638 * control messages have just a few bytes of data.
640 static int uhci_control_msg(struct usb_device *usb_dev, unsigned int pipe, void *cmd, void *data, int len)
642 struct uhci_device *dev = usb_to_uhci(usb_dev);
643 struct uhci_td *first, *td, *prevtd;
644 unsigned long destination, status;
645 int ret;
646 int maxsze = usb_maxpacket(usb_dev, pipe);
648 if (len > maxsze * 29)
649 printk("Warning, too much data for a control packet, crashing\n");
651 first = td = uhci_td_allocate(dev);
653 /* The "pipe" thing contains the destination in bits 8--18, 0x2D is SETUP */
654 destination = (pipe & 0x0007ff00) | 0x2D;
656 /* Status: slow/fast, Active, Short Packet Detect Three Errors */
657 status = (pipe & (1 << 26)) | (1 << 23) | (1 << 29) | (3 << 27);
660 * Build the TD for the control request
662 td->status = status; /* Try forever */
663 td->info = destination | (7 << 21); /* 8 bytes of data */
664 td->buffer = virt_to_bus(cmd);
665 td->first = td;
668 * If direction is "send", change the frame from SETUP (0x2D)
669 * to OUT (0xE1). Else change it from SETUP to IN (0x69)
671 destination ^= (0x2D ^ 0x69); /* SETUP -> IN */
672 if (usb_pipeout(pipe))
673 destination ^= (0xE1 ^ 0x69); /* IN -> OUT */
675 prevtd = td;
676 td = uhci_td_allocate(dev);
677 prevtd->link = 4 | virt_to_bus(td);
680 * Build the DATA TD's
682 while (len > 0) {
683 /* Build the TD for control status */
684 int pktsze = len;
686 if (pktsze > maxsze)
687 pktsze = maxsze;
689 /* Alternate Data0/1 (start with Data1) */
690 destination ^= 1 << 19;
692 td->status = status; /* Status */
693 td->info = destination | ((pktsze-1) << 21); /* pktsze bytes of data */
694 td->buffer = virt_to_bus(data);
695 td->first = first;
696 td->backptr = &prevtd->link;
698 data += pktsze;
699 len -= pktsze;
701 prevtd = td;
702 td = uhci_td_allocate(dev);
703 prevtd->link = 4 | virt_to_bus(td); /* Update previous TD */
708 * Build the final TD for control status
710 destination ^= (0xE1 ^ 0x69); /* OUT -> IN */
711 destination |= 1 << 19; /* End in Data1 */
713 td->link = 1; /* Terminate */
714 td->status = status | (1 << 24); /* IOC */
715 td->info = destination | (0x7ff << 21); /* 0 bytes of data */
716 td->buffer = 0;
717 td->first = first;
718 td->backptr = &prevtd->link;
720 /* Start it up.. */
721 ret = uhci_run_control(dev, first, td);
724 int maxcount = 100;
725 struct uhci_td *curtd = first;
726 unsigned int nextlink;
728 do {
729 nextlink = curtd->link;
730 uhci_remove_td(curtd);
731 uhci_td_deallocate(curtd);
732 if (nextlink & 1) /* Tail? */
733 break;
735 curtd = bus_to_virt(nextlink & ~0xF);
736 if (!--maxcount) {
737 printk("runaway td's!?\n");
738 break;
740 } while (1);
743 if (ret) {
744 __u8 *p = cmd;
746 printk("Failed cmd - %02X %02X %02X %02X %02X %02X %02X %02X\n",
747 p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7]);
749 return ret;
754 * Bulk thread operations: we just mark the last TD
755 * in a bulk thread as an interrupt TD, and wake up
756 * the front-end on completion.
758 * We need to remove the TD from the lists (both interrupt
759 * list and TD lists) by hand if something bad happens!
761 static DECLARE_WAIT_QUEUE_HEAD(bulk_wakeup);
763 static int uhci_bulk_completed(int status, void *buffer, void *dev_id)
765 wake_up(&bulk_wakeup);
766 return 0; /* Don't re-instate */
769 /* td points to the last td in the list, which interrupts on completion */
770 static int uhci_run_bulk(struct uhci_device *dev, struct uhci_td *first, struct uhci_td *last, unsigned long *rval)
772 DECLARE_WAITQUEUE(wait, current);
773 struct uhci_qh *bulk_qh = uhci_qh_allocate(dev);
774 struct uhci_td *curtd;
778 current->state = TASK_UNINTERRUPTIBLE;
779 add_wait_queue(&bulk_wakeup, &wait);
781 uhci_add_irq_list(dev->uhci, last, uhci_bulk_completed, NULL);
783 /* FIXME: This is kinda kludged */
784 /* Walk the TD list and update the QH pointer */
786 int maxcount = 100;
788 curtd = first;
789 do {
790 curtd->qh = bulk_qh;
791 if (curtd->link & 1)
792 break;
794 curtd = bus_to_virt(curtd->link & ~0xF);
795 if (!--maxcount) {
796 printk("runaway tds!\n");
797 break;
799 } while (1);
802 uhci_insert_tds_in_qh(bulk_qh, first, last);
804 // bulk0 is empty here...
805 // show_status(dev->uhci);
806 // show_queues(dev->uhci);
808 /* Add it into the skeleton */
809 /*WARNING! HUB HAS NO BULK QH TIL NOW!!!!!!!!!!!*/
810 uhci_insert_qh(&dev->uhci->root_hub->skel_bulk0_qh, bulk_qh);
812 // now we're in the queue... but don't ask WHAT is in there ;-(
813 // show_status(dev->uhci);
814 // show_queues(dev->uhci);
816 schedule_timeout(HZ/10);
818 remove_wait_queue(&bulk_wakeup, &wait);
820 /* Clean up in case it failed.. */
821 uhci_remove_irq_list(last);
823 #if 0
824 printk("Looking for tds [%p, %p]\n", dev->control_td, td);
825 #endif
827 /* Remove it from the skeleton */
828 uhci_remove_qh(&dev->uhci->root_hub->skel_bulk0_qh, bulk_qh);
830 uhci_qh_deallocate(bulk_qh);
832 return uhci_td_result(dev, last, rval);
836 * Send or receive a bulk message on a pipe.
838 * Note that the "pipe" structure is set up to map
839 * easily to the uhci destination fields.
841 * A bulk message is only built up from
842 * the data phase
844 * The data phase can be an arbitrary number of TD's
845 * although we currently had better not have more than
846 * 31 TD's here.
848 * 31 TD's is a minimum of 248 bytes worth of bulk
849 * information.
851 static int uhci_bulk_msg(struct usb_device *usb_dev, unsigned int pipe, void *data, int len, unsigned long *rval)
853 struct uhci_device *dev = usb_to_uhci(usb_dev);
854 struct uhci_td *first, *td, *prevtd;
855 unsigned long destination, status;
856 int ret;
857 int maxsze = usb_maxpacket(usb_dev, pipe);
859 if (len > maxsze * 31)
860 printk("Warning, too much data for a bulk packet, crashing (%d/%d)\n", len, maxsze);
862 /* The "pipe" thing contains the destination in bits 8--18, 0x69 is IN */
864 IS THIS NECCESARY? PERHAPS WE CAN JUST USE THE PIPE
865 LOOK AT: usb_pipeout and the pipe bits
866 I FORGOT WHAT IT EXACTLY DOES
868 if (usb_pipeout(pipe)) {
869 destination = (pipe & 0x0007ff00) | 0xE1;
871 else {
872 destination = (pipe & 0x0007ff00) | 0x69;
875 /* Status: slow/fast, Active, Short Packet Detect Three Errors */
876 status = (pipe & (1 << 26)) | (1 << 23) | (1 << 29) | (3 << 27);
879 * Build the TDs for the bulk request
881 first = td = uhci_td_allocate(dev);
882 prevtd = first; //This is fake, but at least it's not NULL
883 while (len > 0) {
884 /* Build the TD for control status */
885 int pktsze = len;
887 if (pktsze > maxsze)
888 pktsze = maxsze;
890 td->status = status; /* Status */
891 td->info = destination | ((pktsze-1) << 21) |
892 (usb_gettoggle(usb_dev, usb_pipeendpoint(pipe)) << 19); /* pktsze bytes of data */
893 td->buffer = virt_to_bus(data);
894 td->backptr = &prevtd->link;
895 td->first = first;
897 data += maxsze;
898 len -= maxsze;
900 if (len > 0) {
901 prevtd = td;
902 td = uhci_td_allocate(dev);
903 prevtd->link = 4 | virt_to_bus(td); /* Update previous TD */
906 /* Alternate Data0/1 (start with Data0) */
907 usb_dotoggle(usb_dev, usb_pipeendpoint(pipe));
909 td->link = 1; /* Terminate */
911 /* Start it up.. */
912 ret = uhci_run_bulk(dev, first, td, rval);
915 int maxcount = 100;
916 struct uhci_td *curtd = first;
917 unsigned int nextlink;
919 do {
920 nextlink = curtd->link;
921 uhci_remove_td(curtd);
922 uhci_td_deallocate(curtd);
923 if (nextlink & 1) /* Tail? */
924 break;
926 curtd = bus_to_virt(nextlink & ~0xF);
927 if (!--maxcount) {
928 printk("runaway td's!?\n");
929 break;
931 } while (1);
934 return ret;
937 static struct usb_device *uhci_usb_allocate(struct usb_device *parent)
939 struct usb_device *usb_dev;
940 struct uhci_device *dev;
941 int i;
943 usb_dev = kmalloc(sizeof(*usb_dev), GFP_KERNEL);
944 if (!usb_dev)
945 return NULL;
947 memset(usb_dev, 0, sizeof(*usb_dev));
949 dev = kmalloc(sizeof(*dev), GFP_KERNEL);
950 if (!dev) {
951 usb_destroy_configuration(usb_dev);
952 kfree(usb_dev);
953 return NULL;
956 /* Initialize "dev" */
957 memset(dev, 0, sizeof(*dev));
959 usb_dev->hcpriv = dev;
960 dev->usb = usb_dev;
962 usb_dev->parent = parent;
964 if (parent) {
965 usb_dev->bus = parent->bus;
966 dev->uhci = usb_to_uhci(parent)->uhci;
969 /* Reset the QH's and TD's */
970 for (i = 0; i < UHCI_MAXQH; i++) {
971 dev->qh[i].link = 1;
972 dev->qh[i].element = 1;
973 dev->qh[i].inuse = 0;
976 for (i = 0; i < UHCI_MAXTD; i++) {
977 dev->td[i].link = 1;
978 dev->td[i].inuse = 0;
981 return usb_dev;
984 static int uhci_usb_deallocate(struct usb_device *usb_dev)
986 struct uhci_device *dev = usb_to_uhci(usb_dev);
987 int i;
989 /* There are UHCI_MAXTD preallocated tds */
990 for (i = 0; i < UHCI_MAXTD; ++i) {
991 struct uhci_td *td = dev->td + i;
993 if (td->inuse) {
994 uhci_remove_td(td);
996 /* And remove it from the irq list, if it's active */
997 if (td->status & (1 << 23))
998 td->status &= ~(1 << 23);
999 #if 0
1000 uhci_remove_irq_list(td);
1001 #endif
1005 /* Remove the td from any queues */
1006 for (i = 0; i < UHCI_MAXQH; ++i) {
1007 struct uhci_qh *qh = dev->qh + i;
1009 if (qh->inuse)
1010 uhci_remove_qh(qh->skel, qh);
1013 kfree(dev);
1014 usb_destroy_configuration(usb_dev);
1015 kfree(usb_dev);
1017 return 0;
1020 struct usb_operations uhci_device_operations = {
1021 uhci_usb_allocate,
1022 uhci_usb_deallocate,
1023 uhci_control_msg,
1024 uhci_bulk_msg,
1025 uhci_request_irq,
1029 * This is just incredibly fragile. The timings must be just
1030 * right, and they aren't really documented very well.
1032 * Note the short delay between disabling reset and enabling
1033 * the port..
1035 static void uhci_reset_port(unsigned int port)
1037 unsigned short status;
1039 status = inw(port);
1040 outw(status | USBPORTSC_PR, port); /* reset port */
1041 wait_ms(10);
1042 outw(status & ~USBPORTSC_PR, port);
1043 udelay(5);
1045 status = inw(port);
1046 outw(status | USBPORTSC_PE, port); /* enable port */
1047 wait_ms(10);
1049 status = inw(port);
1050 if(!(status & USBPORTSC_PE)) {
1051 outw(status | USBPORTSC_PE, port); /* one more try at enabling port */
1052 wait_ms(50);
1059 * This gets called if the connect status on the root
1060 * hub (and the root hub only) changes.
1062 static void uhci_connect_change(struct uhci *uhci, unsigned int port, unsigned int nr)
1064 struct usb_device *usb_dev;
1065 struct uhci_device *dev;
1066 unsigned short status;
1068 printk("uhci_connect_change: called for %d\n", nr);
1071 * Even if the status says we're connected,
1072 * the fact that the status bits changed may
1073 * that we got disconnected and then reconnected.
1075 * So start off by getting rid of any old devices..
1077 usb_disconnect(&uhci->root_hub->usb->children[nr]);
1079 status = inw(port);
1081 /* If we have nothing connected, then clear change status and disable the port */
1082 status = (status & ~USBPORTSC_PE) | USBPORTSC_PEC;
1083 if (!(status & USBPORTSC_CCS)) {
1084 outw(status, port);
1085 return;
1089 * Ok, we got a new connection. Allocate a device to it,
1090 * and find out what it wants to do..
1092 usb_dev = uhci_usb_allocate(uhci->root_hub->usb);
1093 dev = usb_dev->hcpriv;
1095 dev->uhci = uhci;
1097 usb_connect(usb_dev);
1099 uhci->root_hub->usb->children[nr] = usb_dev;
1101 wait_ms(200); /* wait for powerup */
1102 uhci_reset_port(port);
1104 /* Get speed information */
1105 usb_dev->slow = (inw(port) & USBPORTSC_LSDA) ? 1 : 0;
1108 * Ok, all the stuff specific to the root hub has been done.
1109 * The rest is generic for any new USB attach, regardless of
1110 * hub type.
1112 usb_new_device(usb_dev);
1116 * This gets called when the root hub configuration
1117 * has changed. Just go through each port, seeing if
1118 * there is something interesting happening.
1120 static void uhci_check_configuration(struct uhci *uhci)
1122 unsigned int io_addr = uhci->io_addr + USBPORTSC1;
1123 int maxchild = uhci->root_hub->usb->maxchild;
1124 int nr = 0;
1126 do {
1127 unsigned short status = inw(io_addr);
1129 if (status & USBPORTSC_CSC)
1130 uhci_connect_change(uhci, io_addr, nr);
1132 nr++; io_addr += 2;
1133 } while (nr < maxchild);
1136 static void uhci_interrupt_notify(struct uhci *uhci)
1138 struct list_head *head = &uhci->interrupt_list;
1139 struct list_head *tmp;
1141 spin_lock(&irqlist_lock);
1142 tmp = head->next;
1143 while (tmp != head) {
1144 struct uhci_td *td = list_entry(tmp, struct uhci_td, irq_list);
1145 struct list_head *next;
1147 next = tmp->next;
1149 if (!(td->status & (1 << 23))) { /* No longer active? */
1150 /* remove from IRQ list */
1151 __list_del(tmp->prev, next);
1152 INIT_LIST_HEAD(tmp);
1153 if (td->completed(td->status, bus_to_virt(td->buffer), td->dev_id)) {
1154 list_add(&td->irq_list, &uhci->interrupt_list);
1156 if (!(td->status & (1 << 25))) {
1157 struct uhci_qh *interrupt_qh = td->qh;
1159 td->info ^= 1 << 19; /* toggle between data0 and data1 */
1160 td->status = (td->status & 0x2f000000) | (1 << 23) | (1 << 24); /* active */
1162 /* Remove then readd? Is that necessary */
1163 uhci_remove_td(td);
1164 uhci_insert_td_in_qh(interrupt_qh, td);
1167 /* If completed wants to not reactivate, then it's */
1168 /* responsible for free'ing the TD's and QH's */
1169 /* or another function (such as run_control) */
1171 tmp = next;
1173 spin_unlock(&irqlist_lock);
1177 * Check port status - Connect Status Change - for
1178 * each of the attached ports (defaults to two ports,
1179 * but at least in theory there can be more of them).
1181 * Wake up the configurator if something happened, we
1182 * can't really do much at interrupt time.
1184 static void uhci_root_hub_events(struct uhci *uhci, unsigned int io_addr)
1186 if (waitqueue_active(&uhci_configure)) {
1187 int ports = uhci->root_hub->usb->maxchild;
1188 io_addr += USBPORTSC1;
1189 do {
1190 if (inw(io_addr) & USBPORTSC_CSC) {
1191 wake_up(&uhci_configure);
1192 return;
1194 io_addr += 2;
1195 } while (--ports > 0);
1199 static void uhci_interrupt(int irq, void *__uhci, struct pt_regs *regs)
1201 struct uhci *uhci = __uhci;
1202 unsigned int io_addr = uhci->io_addr;
1203 unsigned short status;
1206 * Read the interrupt status, and write it back to clear the interrupt cause
1208 status = inw(io_addr + USBSTS);
1209 outw(status, io_addr + USBSTS);
1211 // if ((status & ~0x21) != 0)
1212 // printk("interrupt: %X\n", status);
1214 /* Walk the list of pending TD's to see which ones completed.. */
1215 uhci_interrupt_notify(uhci);
1217 /* Check if there are any events on the root hub.. */
1218 uhci_root_hub_events(uhci, io_addr);
1222 * We init one packet, and mark it just IOC and _not_
1223 * active. Which will result in no actual USB traffic,
1224 * but _will_ result in an interrupt every second.
1226 * Which is exactly what we want.
1228 static void uhci_init_ticktd(struct uhci *uhci)
1230 struct uhci_device *dev = uhci->root_hub;
1231 struct uhci_td *td = uhci_td_allocate(dev);
1233 td->link = 1;
1234 td->status = (1 << 24); /* interrupt on completion */
1235 td->info = (15 << 21) | 0x7f69; /* (ignored) input packet, 16 bytes, device 127 */
1236 td->buffer = 0;
1237 td->first = td;
1238 td->qh = NULL;
1240 uhci->fl->frame[0] = virt_to_bus(td);
1243 static void reset_hc(struct uhci *uhci)
1245 unsigned int io_addr = uhci->io_addr;
1247 /* Global reset for 50ms */
1248 outw(USBCMD_GRESET, io_addr+USBCMD);
1249 wait_ms(50);
1250 outw(0, io_addr+USBCMD);
1251 wait_ms(10);
1254 static void start_hc(struct uhci *uhci)
1256 unsigned int io_addr = uhci->io_addr;
1257 int timeout = 1000;
1259 uhci_init_ticktd(uhci);
1262 * Reset the HC - this will force us to get a
1263 * new notification of any already connected
1264 * ports due to the virtual disconnect that it
1265 * implies.
1267 outw(USBCMD_HCRESET, io_addr + USBCMD);
1268 while (inw(io_addr + USBCMD) & USBCMD_HCRESET) {
1269 if (!--timeout) {
1270 printk("USBCMD_HCRESET timed out!\n");
1271 break;
1276 outw(USBINTR_TIMEOUT | USBINTR_RESUME | USBINTR_IOC | USBINTR_SP, io_addr + USBINTR);
1277 outw(0, io_addr + USBFRNUM);
1278 outl(virt_to_bus(uhci->fl), io_addr + USBFLBASEADD);
1280 /* Run and mark it configured with a 64-byte max packet */
1281 outw(USBCMD_RS | USBCMD_CF | USBCMD_MAXP, io_addr + USBCMD);
1285 * Allocate a frame list, and four regular queues.
1287 * The hardware doesn't really know any difference
1288 * in the queues, but the order does matter for the
1289 * protocols higher up. The order is:
1291 * - any isochronous events handled before any
1292 * of the queues. We don't do that here, because
1293 * we'll create the actual TD entries on demand.
1294 * - The first queue is the "interrupt queue".
1295 * - The second queue is the "control queue".
1296 * - The third queue is "bulk data".
1298 * We could certainly have multiple queues of the same
1299 * type, and maybe we should. We could have per-device
1300 * queues, for example. We begin small.
1302 static struct uhci *alloc_uhci(unsigned int io_addr)
1304 int i;
1305 struct uhci *uhci;
1306 struct usb_bus *bus;
1307 struct uhci_device *dev;
1308 struct usb_device *usb;
1310 uhci = kmalloc(sizeof(*uhci), GFP_KERNEL);
1311 if (!uhci)
1312 return NULL;
1314 memset(uhci, 0, sizeof(*uhci));
1316 uhci->irq = -1;
1317 uhci->io_addr = io_addr;
1318 INIT_LIST_HEAD(&uhci->interrupt_list);
1320 /* We need exactly one page (per UHCI specs), how convenient */
1321 uhci->fl = (void *)__get_free_page(GFP_KERNEL);
1323 bus = kmalloc(sizeof(*bus), GFP_KERNEL);
1324 if (!bus)
1325 return NULL;
1327 memset(bus, 0, sizeof(*bus));
1329 uhci->bus = bus;
1330 bus->hcpriv = uhci;
1331 bus->op = &uhci_device_operations;
1334 * We allocate a 8kB area for the UHCI hub. The area
1335 * is described by the uhci_device structure, and basically
1336 * contains everything needed for normal operation.
1338 * The first page is the actual device descriptor for the
1339 * hub.
1341 * The second page is used for the frame list.
1343 usb = uhci_usb_allocate(NULL);
1344 if (!usb)
1345 return NULL;
1347 dev = uhci->root_hub = usb_to_uhci(usb);
1349 usb->bus = bus;
1351 /* Initialize the root hub */
1352 /* UHCI specs says devices must have 2 ports, but goes on to say */
1353 /* they may have more but give no way to determine how many they */
1354 /* have, so default to 2 */
1355 usb->maxchild = 2;
1356 usb_init_root_hub(usb);
1359 * Initialize the queues. They all start out empty,
1360 * linked to each other in the proper order.
1362 for (i = 1 ; i < 9; i++) {
1363 dev->qh[i].link = 2 | virt_to_bus(&dev->skel_control_qh);
1364 dev->qh[i].element = 1;
1367 dev->skel_control_qh.link = 2 | virt_to_bus(&dev->skel_bulk0_qh);
1368 dev->skel_control_qh.element = 1;
1370 dev->skel_bulk0_qh.link = 2 | virt_to_bus(&dev->skel_bulk1_qh);
1371 dev->skel_bulk0_qh.element = 1;
1373 dev->skel_bulk1_qh.link = 2 | virt_to_bus(&dev->skel_bulk2_qh);
1374 dev->skel_bulk1_qh.element = 1;
1376 dev->skel_bulk2_qh.link = 2 | virt_to_bus(&dev->skel_bulk3_qh);
1377 dev->skel_bulk2_qh.element = 1;
1379 dev->skel_bulk3_qh.link = 1;
1380 dev->skel_bulk3_qh.element = 1;
1383 * Fill the frame list: make all entries point to
1384 * the proper interrupt queue.
1386 * This is probably silly, but it's a simple way to
1387 * scatter the interrupt queues in a way that gives
1388 * us a reasonable dynamic range for irq latencies.
1390 for (i = 0; i < 1024; i++) {
1391 struct uhci_qh * irq = &dev->skel_int2_qh;
1392 if (i & 1) {
1393 irq++;
1394 if (i & 2) {
1395 irq++;
1396 if (i & 4) {
1397 irq++;
1398 if (i & 8) {
1399 irq++;
1400 if (i & 16) {
1401 irq++;
1402 if (i & 32) {
1403 irq++;
1404 if (i & 64) {
1405 irq++;
1413 uhci->fl->frame[i] = 2 | virt_to_bus(irq);
1416 return uhci;
1421 * De-allocate all resources..
1423 static void release_uhci(struct uhci *uhci)
1425 if (uhci->irq >= 0) {
1426 free_irq(uhci->irq, uhci);
1427 uhci->irq = -1;
1430 #if 0
1431 if (uhci->root_hub) {
1432 uhci_usb_deallocate(uhci_to_usb(uhci->root_hub));
1433 uhci->root_hub = NULL;
1435 #endif
1437 if (uhci->fl) {
1438 free_page((unsigned long)uhci->fl);
1439 uhci->fl = NULL;
1442 kfree(uhci->bus);
1443 kfree(uhci);
1446 void cleanup_drivers(void);
1448 static int uhci_control_thread(void * __uhci)
1450 struct uhci *uhci = (struct uhci *)__uhci;
1452 lock_kernel();
1453 request_region(uhci->io_addr, 32, "usb-uhci");
1456 * This thread doesn't need any user-level access,
1457 * so get rid of all our resources..
1459 printk("uhci_control_thread at %p\n", &uhci_control_thread);
1460 exit_mm(current);
1461 exit_files(current);
1462 exit_fs(current);
1464 strcpy(current->comm, "uhci-control");
1467 * Ok, all systems are go..
1469 start_hc(uhci);
1470 for(;;) {
1471 siginfo_t info;
1472 int unsigned long signr;
1474 interruptible_sleep_on(&uhci_configure);
1475 #ifdef CONFIG_APM
1476 if (apm_resume) {
1477 apm_resume = 0;
1478 start_hc(uhci);
1479 continue;
1481 #endif
1482 uhci_check_configuration(uhci);
1484 if(signal_pending(current)) {
1485 /* sending SIGUSR1 makes us print out some info */
1486 spin_lock_irq(&current->sigmask_lock);
1487 signr = dequeue_signal(&current->blocked, &info);
1488 spin_unlock_irq(&current->sigmask_lock);
1490 if(signr == SIGUSR1) {
1491 printk("UHCI queue dump:\n");
1492 show_queues(uhci);
1493 } else {
1494 break;
1500 int i;
1501 if(uhci->root_hub)
1502 for(i = 0; i < uhci->root_hub->usb->maxchild; i++)
1503 usb_disconnect(uhci->root_hub->usb->children + i);
1506 cleanup_drivers();
1508 reset_hc(uhci);
1509 release_region(uhci->io_addr, 32);
1511 release_uhci(uhci);
1512 MOD_DEC_USE_COUNT;
1514 printk("uhci_control_thread exiting\n");
1516 return 0;
1520 * If we've successfully found a UHCI, now is the time to increment the
1521 * module usage count, start the control thread, and return success..
1523 static int found_uhci(int irq, unsigned int io_addr)
1525 int retval;
1526 struct uhci *uhci;
1528 uhci = alloc_uhci(io_addr);
1529 if (!uhci)
1530 return -ENOMEM;
1532 reset_hc(uhci);
1534 retval = -EBUSY;
1535 if (request_irq(irq, uhci_interrupt, SA_SHIRQ, "usb", uhci) == 0) {
1536 int pid;
1538 MOD_INC_USE_COUNT;
1539 uhci->irq = irq;
1540 pid = kernel_thread(uhci_control_thread, uhci, CLONE_FS | CLONE_FILES | CLONE_SIGHAND);
1541 if (pid >= 0)
1542 return 0;
1544 MOD_DEC_USE_COUNT;
1545 retval = pid;
1547 release_uhci(uhci);
1548 return retval;
1551 static int start_uhci(struct pci_dev *dev)
1553 int i;
1555 /* Search for the IO base address.. */
1556 for (i = 0; i < 6; i++) {
1557 unsigned int io_addr = dev->base_address[i];
1559 /* IO address? */
1560 if (!(io_addr & 1))
1561 continue;
1563 io_addr &= PCI_BASE_ADDRESS_IO_MASK;
1565 /* Is it already in use? */
1566 if (check_region(io_addr, 32))
1567 break;
1569 return found_uhci(dev->irq, io_addr);
1571 return -1;
1574 #ifdef CONFIG_APM
1575 static int handle_apm_event(apm_event_t event)
1577 static int down = 0;
1579 switch (event) {
1580 case APM_SYS_SUSPEND:
1581 case APM_USER_SUSPEND:
1582 if (down) {
1583 printk(KERN_DEBUG "uhci: received extra suspend event\n");
1584 break;
1586 down = 1;
1587 break;
1588 case APM_NORMAL_RESUME:
1589 case APM_CRITICAL_RESUME:
1590 if (!down) {
1591 printk(KERN_DEBUG "uhci: received bogus resume event\n");
1592 break;
1594 down = 0;
1595 if (waitqueue_active(&uhci_configure)) {
1596 apm_resume = 1;
1597 wake_up(&uhci_configure);
1599 break;
1601 return 0;
1603 #endif
1605 #ifdef MODULE
1607 void cleanup_module(void)
1609 #ifdef CONFIG_APM
1610 apm_unregister_callback(&handle_apm_event);
1611 #endif
1614 int init_modules(void)
1616 return uhci_init();
1619 #endif
1621 int uhci_init(void)
1623 int retval;
1624 struct pci_dev *dev = NULL;
1625 u8 type;
1627 retval = -ENODEV;
1628 for (;;) {
1629 dev = pci_find_class(PCI_CLASS_SERIAL_USB<<8, dev);
1630 if (!dev)
1631 break;
1632 /* Is it UHCI */
1633 pci_read_config_byte(dev, PCI_CLASS_PROG, &type);
1634 if(type != 0)
1635 continue;
1636 /* Ok set it up */
1637 retval = start_uhci(dev);
1638 if (retval < 0)
1639 continue;
1641 #ifdef CONFIG_APM
1642 apm_register_callback(&handle_apm_event);
1643 #endif
1644 return 0;
1646 return retval;