Import 2.3.13
[davej-history.git] / drivers / usb / uhci.c
blobd42a6b3471a275008c716b2feccbf32290d28ee9
1 /*
2 * Universal Host Controller Interface driver for USB.
4 * (C) Copyright 1999 Linus Torvalds
5 * (C) Copyright 1999 Johannes Erdfelt
7 * Intel documents this fairly well, and as far as I know there
8 * are no royalties or anything like that, but even so there are
9 * people who decided that they want to do the same thing in a
10 * completely different way.
12 * Oh, well. The intel version is the more common by far. As such,
13 * that's the one I care about right now.
15 * WARNING! The USB documentation is downright evil. Most of it
16 * is just crap, written by a committee. You're better off ignoring
17 * most of it, the important stuff is:
18 * - the low-level protocol (fairly simple but lots of small details)
19 * - working around the horridness of the rest
22 /* 4/4/1999 added data toggle for interrupt pipes -keryan */
23 /* 5/16/1999 added global toggles for bulk and control */
24 /* 6/25/1999 added fix for data toggles on bidirectional bulk endpoints */
26 #include <linux/config.h>
27 #include <linux/module.h>
28 #include <linux/pci.h>
29 #include <linux/kernel.h>
30 #include <linux/delay.h>
31 #include <linux/ioport.h>
32 #include <linux/sched.h>
33 #include <linux/malloc.h>
34 #include <linux/smp_lock.h>
35 #include <linux/errno.h>
36 #include <linux/unistd.h>
38 #include <asm/uaccess.h>
39 #include <asm/spinlock.h>
40 #include <asm/io.h>
41 #include <asm/irq.h>
42 #include <asm/system.h>
44 #include "uhci.h"
46 #ifdef CONFIG_APM
47 #include <linux/apm_bios.h>
48 static int handle_apm_event(apm_event_t event);
49 static int apm_resume = 0;
50 #endif
52 static int uhci_debug = 1;
54 #define compile_assert(x) do { switch (0) { case 1: case !(x): } } while (0)
56 static DECLARE_WAIT_QUEUE_HEAD(uhci_configure);
58 static kmem_cache_t *uhci_td_cachep;
59 static kmem_cache_t *uhci_qh_cachep;
61 static LIST_HEAD(uhci_list);
63 #define UHCI_DEBUG
66 * Map status to standard result codes
68 static int uhci_map_status(int status, int dir_out)
70 if (!status)
71 return USB_ST_NOERROR;
72 if (status & 0x02) /* Bitstuff error*/
73 return USB_ST_BITSTUFF;
74 if (status & 0x04) { /* CRC/Timeout */
75 if (dir_out)
76 return USB_ST_NORESPONSE;
77 else
78 return USB_ST_CRC;
80 if (status & 0x08) /* NAK */
81 return USB_ST_TIMEOUT;
82 if (status & 0x10) /* Babble */
83 return USB_ST_STALL;
84 if (status & 0x20) /* Buffer error */
85 return USB_ST_BUFFERUNDERRUN;
86 if (status & 0x40) /* Stalled */
87 return USB_ST_STALL;
88 if (status & 0x80) /* Active */
89 return USB_ST_NOERROR;
91 return USB_ST_INTERNALERROR;
94 * Return the result of a TD..
96 static int uhci_td_result(struct uhci_device *dev, struct uhci_td *td, unsigned long *rval)
98 unsigned int status;
99 struct uhci_td *tmp;
101 if (!td->qh)
102 tmp = td;
103 else
104 tmp = uhci_ptr_to_virt(td->qh->element);
106 if (rval)
107 *rval = 0;
109 /* locate the first failing td, if any */
111 do {
112 status = (tmp->status >> 16) & 0xff;
113 if (status) {
114 /* must reset the toggle on first error */
115 if (uhci_debug) {
116 printk(KERN_DEBUG "Set toggle from %x rval %ld\n",
117 (unsigned int)tmp, rval ? *rval : 0);
119 usb_settoggle(dev->usb, usb_pipeendpoint(tmp->info),
120 usb_pipeout(tmp->info), (tmp->info >> 19) & 1);
121 break;
122 } else {
123 if (rval)
124 *rval += (tmp->status & 0x3ff) + 1;
126 if ((tmp->link & UHCI_PTR_TERM) ||
127 (tmp->link & UHCI_PTR_QH))
128 break;
129 tmp = uhci_ptr_to_virt(tmp->link);
130 } while (1);
132 if (!status)
133 return USB_ST_NOERROR;
135 /* Some debugging code */
136 if (uhci_debug) {
137 int count = 10;
139 if (!td->qh)
140 tmp = td;
141 else
142 tmp = uhci_ptr_to_virt(td->qh->element);
143 printk(KERN_DEBUG "uhci_td_result() failed with status %x\n",
144 status);
145 do {
146 show_td(tmp);
147 if ((tmp->link & UHCI_PTR_TERM) ||
148 (tmp->link & UHCI_PTR_QH))
149 break;
150 tmp = uhci_ptr_to_virt(tmp->link);
151 } while (--count);
154 if (status & 0x40) {
155 /* endpoint has stalled - mark it halted */
156 usb_endpoint_halt(dev->usb, usb_pipeendpoint(tmp->info));
157 return USB_ST_STALL;
160 if (status == 0x80) {
161 /* still active */
162 if (!rval)
163 return USB_ST_DATAUNDERRUN;
165 return uhci_map_status(status, usb_pipeout(tmp->info));
169 * Inserts a td into qh list at the top.
171 * Careful about atomicity: even on UP this
172 * requires a locked access due to the concurrent
173 * DMA engine.
175 * NOTE! This assumes that first->last is a valid
176 * list of TD's with the proper backpointers set
177 * up and all..
179 static void uhci_insert_tds_in_qh(struct uhci_qh *qh, struct uhci_td *first, struct uhci_td *last)
181 unsigned int link = qh->element;
182 unsigned int new = virt_to_bus(first) | UHCI_PTR_DEPTH;
184 for (;;) {
185 unsigned char success;
187 last->link = link;
188 first->backptr = &qh->element;
189 asm volatile("lock ; cmpxchg %4,%2 ; sete %0"
190 :"=q" (success), "=a" (link)
191 :"m" (qh->element), "1" (link), "r" (new)
192 :"memory");
193 if (success) {
194 /* Was there a successor entry? Fix it's backpointer */
195 if ((link & UHCI_PTR_TERM) == 0) {
196 struct uhci_td *next = uhci_ptr_to_virt(link);
197 next->backptr = &last->link;
199 break;
204 static inline void uhci_insert_td_in_qh(struct uhci_qh *qh, struct uhci_td *td)
206 uhci_insert_tds_in_qh(qh, td, td);
209 static void uhci_insert_qh(struct uhci_qh *qh, struct uhci_qh *newqh)
211 newqh->link = qh->link;
212 qh->link = virt_to_bus(newqh) | UHCI_PTR_QH;
215 static void uhci_remove_qh(struct uhci_qh *qh, struct uhci_qh *remqh)
217 struct uhci_qh *lqh = qh;
219 while (uhci_ptr_to_virt(lqh->link) != remqh) {
220 if (lqh->link & UHCI_PTR_TERM)
221 break;
223 lqh = uhci_ptr_to_virt(lqh->link);
226 if (lqh->link & UHCI_PTR_TERM) {
227 printk(KERN_DEBUG "couldn't find qh in chain!\n");
228 return;
231 lqh->link = remqh->link;
235 * Removes td from qh if present.
237 * NOTE! We keep track of both forward and back-pointers,
238 * so this should be trivial, right?
240 * Wrong. While all TD insert/remove operations are synchronous
241 * on the CPU, the UHCI controller can (and does) play with the
242 * very first forward pointer. So we need to validate the backptr
243 * before we change it, so that we don't by mistake reset the QH
244 * head to something old.
246 static void uhci_remove_td(struct uhci_td *td)
248 unsigned int *backptr = td->backptr;
249 unsigned int link = td->link;
250 unsigned int me;
252 if (!backptr)
253 return;
255 td->backptr = NULL;
258 * This is the easy case: the UHCI will never change "td->link",
259 * so we can always just look at that and fix up the backpointer
260 * of any next element..
262 if (!(link & UHCI_PTR_TERM)) {
263 struct uhci_td *next = uhci_ptr_to_virt(link);
264 next->backptr = backptr;
268 * The nasty case is "backptr->next", which we need to
269 * update to "link" _only_ if "backptr" still points
270 * to us (it may not: maybe backptr is a QH->element
271 * pointer and the UHCI has changed the value).
273 me = virt_to_bus(td) | (0xe & *backptr);
274 asm volatile("lock ; cmpxchg %0,%1"
276 :"r" (link), "m" (*backptr), "a" (me)
277 :"memory");
280 static struct uhci_td *uhci_td_alloc(struct uhci_device *dev)
282 struct uhci_td *td;
284 td = kmem_cache_alloc(uhci_td_cachep, SLAB_KERNEL);
285 if (!td)
286 return NULL;
288 #ifdef UHCI_DEBUG
289 if ((__u32)td & UHCI_PTR_BITS)
290 printk("qh not 16 byte aligned!\n");
291 #endif
293 td->link = UHCI_PTR_TERM;
294 td->buffer = 0;
296 td->backptr = NULL;
297 td->qh = NULL;
298 td->dev_id = NULL;
299 td->dev = dev;
300 td->flags = 0;
301 INIT_LIST_HEAD(&td->irq_list);
302 atomic_set(&td->refcnt, 1);
304 return td;
307 static void uhci_td_free(struct uhci_td *td)
309 if (atomic_dec_and_test(&td->refcnt))
310 kmem_cache_free(uhci_td_cachep, td);
313 static struct uhci_qh *uhci_qh_alloc(struct uhci_device *dev)
315 struct uhci_qh *qh;
317 qh = kmem_cache_alloc(uhci_qh_cachep, SLAB_KERNEL);
318 if (!qh)
319 return NULL;
321 #ifdef UHCI_DEBUG
322 if ((__u32)qh & UHCI_PTR_BITS)
323 printk("qh not 16 byte aligned!\n");
324 #endif
326 qh->element = UHCI_PTR_TERM;
327 qh->link = UHCI_PTR_TERM;
329 qh->dev = dev;
330 qh->skel = NULL;
331 init_waitqueue_head(&qh->wakeup);
332 atomic_set(&qh->refcnt, 1);
334 return qh;
337 static void uhci_qh_free(struct uhci_qh *qh)
339 if (atomic_dec_and_test(&qh->refcnt))
340 kmem_cache_free(uhci_qh_cachep, qh);
344 * UHCI interrupt list operations..
346 static spinlock_t irqlist_lock = SPIN_LOCK_UNLOCKED;
348 static void uhci_add_irq_list(struct uhci *uhci, struct uhci_td *td, usb_device_irq completed, void *dev_id)
350 unsigned long flags;
352 td->completed = completed;
353 td->dev_id = dev_id;
355 spin_lock_irqsave(&irqlist_lock, flags);
356 list_add(&td->irq_list, &uhci->interrupt_list);
357 spin_unlock_irqrestore(&irqlist_lock, flags);
360 static void uhci_remove_irq_list(struct uhci_td *td)
362 unsigned long flags;
364 spin_lock_irqsave(&irqlist_lock, flags);
365 list_del(&td->irq_list);
366 spin_unlock_irqrestore(&irqlist_lock, flags);
370 * This function removes and disallcoates all structures set up for an transfer.
371 * It takes the qh out of the skeleton, removes the tq and the td's.
372 * It only removes the associated interrupt handler if removeirq ist set.
373 * The *td argument is any td in the list of td's.
375 static void uhci_remove_transfer(struct uhci_td *td, char removeirq)
377 int maxcount = 1000;
378 struct uhci_td *curtd;
379 unsigned int nextlink;
381 if (!td->qh)
382 curtd = td;
383 else
384 curtd = uhci_ptr_to_virt(td->qh->element);
386 /* Remove it from the skeleton */
387 uhci_remove_qh(td->qh->skel, td->qh);
388 uhci_qh_free(td->qh);
389 do {
390 nextlink = curtd->link;
392 /* IOC? => remove handler */
393 if (removeirq && (td->status & TD_CTRL_IOC))
394 uhci_remove_irq_list(td);
396 uhci_remove_td(curtd);
397 uhci_td_free(curtd);
398 if (nextlink & UHCI_PTR_TERM) /* Tail? */
399 break;
401 curtd = bus_to_virt(nextlink & ~UHCI_PTR_BITS);
402 if (!--maxcount) {
403 printk(KERN_ERR "runaway td's!?\n");
404 break;
406 } while (1);
410 * Request a interrupt handler..
412 * Returns: a "handle pointer" that release_irq can use to stop this
413 * interrupt. (It's really a pointer to the TD).
415 static void *uhci_request_irq(struct usb_device *usb_dev, unsigned int pipe, usb_device_irq handler, int period, void *dev_id)
417 struct uhci_device *dev = usb_to_uhci(usb_dev);
418 struct uhci_td *td = uhci_td_alloc(dev);
419 struct uhci_qh *qh = uhci_qh_alloc(dev);
420 unsigned int destination, status;
422 if (!td || !qh)
423 return NULL;
425 /* Destination: pipe destination with INPUT */
426 destination = (pipe & PIPE_DEVEP_MASK) | usb_packetid(pipe);
428 /* Infinite errors is 0, so no bits */
429 status = (pipe & TD_CTRL_LS) | TD_CTRL_IOC | TD_CTRL_ACTIVE |
430 TD_CTRL_SPD;
432 td->link = UHCI_PTR_TERM; /* Terminate */
433 td->status = status; /* In */
434 td->info = destination | ((usb_maxpacket(usb_dev, pipe) - 1) << 21) |
435 (usb_gettoggle(usb_dev, usb_pipeendpoint(pipe),
436 usb_pipeout(pipe)) << 19);
437 td->buffer = virt_to_bus(dev->data);
438 td->qh = qh;
439 td->dev = dev;
441 /* if period 0, insert into fast q */
442 if (period == 0) {
443 td->flags |= UHCI_TD_REMOVE;
444 qh->skel = &dev->uhci->skel_int2_qh;
445 } else
446 qh->skel = &dev->uhci->skel_int8_qh;
448 uhci_add_irq_list(dev->uhci, td, handler, dev_id);
450 uhci_insert_td_in_qh(qh, td);
452 /* Add it into the skeleton */
453 uhci_insert_qh(qh->skel, qh);
455 return (void *)td;
459 * Release an interrupt handler previously allocated using
460 * uhci_request_irq. This function does no validity checking, so make
461 * sure you're not releasing an already released handle as it may be
462 * in use by something else..
464 * This function can NOT be called from an interrupt.
466 int uhci_release_irq(void *handle)
468 struct uhci_td *td;
469 struct uhci_qh *qh;
471 #ifdef UHCI_DEBUG
472 printk(KERN_DEBUG "usb-uhci: releasing irq handle %p\n", handle);
473 #endif
475 td = (struct uhci_td *)handle;
476 if (!td)
477 return USB_ST_INTERNALERROR;
479 /* Remove it from the internal irq_list */
480 uhci_remove_irq_list(td);
482 /* Remove the interrupt TD and QH */
483 uhci_remove_td(td);
484 qh = td->qh;
485 uhci_remove_qh(qh->skel, qh);
487 if (td->completed != NULL)
488 td->completed(USB_ST_REMOVED, NULL, 0, td->dev_id);
490 /* Free the TD and QH */
491 uhci_td_free(td);
492 uhci_qh_free(qh);
494 return USB_ST_NOERROR;
495 } /* uhci_release_irq() */
498 * Isochronous operations
500 static int uhci_compress_isochronous(struct usb_device *usb_dev, void *_isodesc)
502 struct uhci_iso_td *isodesc = (struct uhci_iso_td *)_isodesc;
503 char *data = isodesc->data;
504 int i, totlen = 0;
506 for (i = 0; i < isodesc->num; i++) {
507 struct uhci_td *td = &isodesc->td[i];
508 char *cdata = uhci_ptr_to_virt(td->buffer);
509 int n = (td->status + 1) & 0x7FF;
511 if ((cdata != data) && (n))
512 memmove(data, cdata, n);
514 #ifdef UHCI_DEBUG
515 /* Debugging */
516 if ((td->status >> 16) & 0xFF)
517 printk(KERN_DEBUG "error: %d %X\n", i,
518 (td->status >> 16));
519 #endif
521 data += n;
522 totlen += n;
525 return totlen;
528 static int uhci_unschedule_isochronous(struct usb_device *usb_dev, void *_isodesc)
530 struct uhci_device *dev = usb_to_uhci(usb_dev);
531 struct uhci *uhci = dev->uhci;
532 struct uhci_iso_td *isodesc = (struct uhci_iso_td *)_isodesc;
533 int i;
535 if ((isodesc->frame < 0) || (isodesc->frame > 1023)) {
536 printk(KERN_ERR "illegal frame number %d\n", isodesc->frame);
537 return 1;
540 /* FIXME: Use uhci_remove_td */
542 /* Remove from previous frames */
543 for (i = 0; i < isodesc->num; i++) {
544 struct uhci_td *td = &isodesc->td[i];
546 /* Turn off Active and IOC bits */
547 td->status &= ~(3 << 23);
548 td->status &= ~(TD_CTRL_ACTIVE | TD_CTRL_IOC);
550 uhci->fl->frame[(isodesc->frame + i) % 1024] = td->link;
553 isodesc->frame = -1;
555 return 0;
558 /* td points to the one td we allocated for isochronous transfers */
559 static int uhci_schedule_isochronous(struct usb_device *usb_dev, void *_isodesc, void *_pisodesc)
561 struct uhci_device *dev = usb_to_uhci(usb_dev);
562 struct uhci *uhci = dev->uhci;
563 struct uhci_iso_td *isodesc = (struct uhci_iso_td *)_isodesc;
564 struct uhci_iso_td *pisodesc = (struct uhci_iso_td *)_pisodesc;
565 int frame, i;
567 if (isodesc->frame != -1) {
568 printk(KERN_ERR "isoc queue not removed\n");
569 uhci_unschedule_isochronous(usb_dev, isodesc);
572 /* Insert TD into list */
573 if (!pisodesc) {
574 /* It's not guaranteed to be 1-1024 */
575 frame = inw(uhci->io_addr + USBFRNUM) % 1024;
577 /* HACK: Start 2 frames from now */
578 frame = (frame + 2) % 1024;
579 } else
580 frame = (pisodesc->endframe + 1) % 1024;
582 for (i = 0; i < isodesc->num; i++) {
583 struct uhci_td *td = &isodesc->td[i];
585 /* Active */
586 td->status |= TD_CTRL_ACTIVE;
587 td->backptr = &uhci->fl->frame[(frame + i) % 1024];
588 td->link = uhci->fl->frame[(frame + i) % 1024];
589 uhci->fl->frame[(frame + i) % 1024] = virt_to_bus(td);
592 /* IOC on the last TD */
593 isodesc->td[i - 1].status |= TD_CTRL_IOC;
595 isodesc->frame = frame;
596 isodesc->endframe = (frame + isodesc->num - 1) % 1024;
598 return 0;
602 * Initialize isochronous queue
604 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)
606 struct uhci_device *dev = usb_to_uhci(usb_dev);
607 unsigned long destination, status;
608 struct uhci_td *td;
609 struct uhci_iso_td *isodesc;
610 int i;
612 isodesc = kmalloc(sizeof(*isodesc), GFP_KERNEL);
613 if (!isodesc) {
614 printk(KERN_ERR "Couldn't allocate isodesc!\n");
615 return NULL;
618 memset(isodesc, 0, sizeof(*isodesc));
620 /* Carefully work around the non contiguous pages */
621 isodesc->num = len / maxsze;
622 isodesc->td = kmalloc(sizeof(struct uhci_td) * isodesc->num, GFP_KERNEL);
623 isodesc->frame = isodesc->endframe = -1;
624 isodesc->data = data;
625 isodesc->maxsze = maxsze;
627 if (!isodesc->td) {
628 printk(KERN_ERR "couldn't allocate td's\n");
629 kfree(isodesc);
630 return NULL;
633 isodesc->frame = isodesc->endframe = -1;
636 * Build the DATA TD's
638 i = 0;
639 do {
640 /* Build the TD for control status */
641 td = &isodesc->td[i];
643 /* The "pipe" thing contains the destination in bits 8--18 */
644 destination = (pipe & PIPE_DEVEP_MASK)
645 | usb_packetid (pipe); /* add IN or OUT */
647 status = (pipe & TD_CTRL_LS) | TD_CTRL_ACTIVE | TD_CTRL_IOS;
650 * Build the TD for the control request
652 td->status = status;
653 td->info = destination | ((maxsze - 1) << 21);
654 td->buffer = virt_to_bus(data);
655 td->backptr = NULL;
657 i++;
659 data += maxsze;
660 len -= maxsze;
661 } while (i < isodesc->num);
663 uhci_add_irq_list(dev->uhci, td, completed, dev_id);
665 return isodesc;
668 static void uhci_delete_isochronous(struct usb_device *usb_dev, void *_isodesc)
670 struct uhci_iso_td *isodesc = (struct uhci_iso_td *)_isodesc;
672 /* If it's still scheduled, unschedule them */
673 if (isodesc->frame)
674 uhci_unschedule_isochronous(usb_dev, isodesc);
676 /* Remove it from the IRQ list */
677 uhci_remove_irq_list(&isodesc->td[isodesc->num - 1]);
679 kfree(isodesc->td);
680 kfree(isodesc);
684 * Control thread operations: we just mark the last TD
685 * in a control thread as an interrupt TD, and wake up
686 * the front-end on completion.
688 * We need to remove the TD from the lists (both interrupt
689 * list and TD lists) by hand if something bad happens!
692 static int uhci_generic_completed(int status, void *buffer, int len, void *dev_id)
694 wait_queue_head_t *wakeup = (wait_queue_head_t *)dev_id;
696 if (waitqueue_active(wakeup))
697 wake_up(wakeup);
698 else
699 printk("waitqueue empty!\n");
701 return 0; /* Don't re-instate */
704 /* td points to the last td in the list, which interrupts on completion */
705 static int uhci_run_control(struct uhci_device *dev, struct uhci_td *first, struct uhci_td *last)
707 DECLARE_WAITQUEUE(wait, current);
708 struct uhci_qh *qh = uhci_qh_alloc(dev);
710 if (!qh)
711 return -1;
713 current->state = TASK_UNINTERRUPTIBLE;
714 add_wait_queue(&qh->wakeup, &wait);
716 uhci_add_irq_list(dev->uhci, last, uhci_generic_completed, &qh->wakeup);
718 #if 0
719 /* FIXME: This is kinda kludged */
720 /* Walk the TD list and update the QH pointer */
722 struct uhci_td *curtd;
723 int count = 100;
725 curtd = first;
726 do {
727 curtd->qh = ctrl_qh;
728 if (curtd->link & TD_CTRL_TERM)
729 break;
731 curtd = uhci_ptr_to_virt(curtd->link);
732 } while (--count);
733 if (!count)
734 printk(KERN_DEBUG "runaway tds!\n");
736 #endif
738 uhci_insert_tds_in_qh(qh, first, last);
740 /* Add it into the skeleton */
741 uhci_insert_qh(&dev->uhci->skel_control_qh, qh);
743 schedule_timeout(HZ * 5); /* 5 seconds */
745 remove_wait_queue(&qh->wakeup, &wait);
747 /* Clean up in case it failed.. */
748 uhci_remove_irq_list(last);
750 /* Remove it from the skeleton */
751 uhci_remove_qh(&dev->uhci->skel_control_qh, qh);
753 uhci_qh_free(qh);
755 return uhci_td_result(dev, last, NULL);
759 * Send or receive a control message on a pipe.
761 * Note that the "pipe" structure is set up to map
762 * easily to the uhci destination fields.
764 * A control message is built up from three parts:
765 * - The command itself
766 * - [ optional ] data phase
767 * - Status complete phase
769 * The data phase can be an arbitrary number of TD's
770 * although we currently had better not have more than
771 * 29 TD's here (we have 31 TD's allocated for control
772 * operations, and two of them are used for command and
773 * status).
775 * 29 TD's is a minimum of 232 bytes worth of control
776 * information, that's just ridiculously high. Most
777 * control messages have just a few bytes of data.
779 static int uhci_control_msg(struct usb_device *usb_dev, unsigned int pipe, devrequest *cmd, void *data, int len)
781 struct uhci_device *dev = usb_to_uhci(usb_dev);
782 struct uhci_td *first, *td, *prevtd;
783 unsigned long destination, status;
784 int ret, count;
785 int maxsze = usb_maxpacket(usb_dev, pipe);
786 __u32 nextlink;
788 first = td = uhci_td_alloc(dev);
789 if (!td)
790 return -ENOMEM;
792 /* The "pipe" thing contains the destination in bits 8--18, 0x2D is SETUP */
793 destination = (pipe & PIPE_DEVEP_MASK) | 0x2D;
795 /* 3 errors */
796 status = (pipe & TD_CTRL_LS) | TD_CTRL_ACTIVE | TD_CTRL_SPD | (3 << 27);
799 * Build the TD for the control request
801 td->status = status; /* Try forever */
802 td->info = destination | (7 << 21); /* 8 bytes of data */
803 td->buffer = virt_to_bus(cmd);
806 * If direction is "send", change the frame from SETUP (0x2D)
807 * to OUT (0xE1). Else change it from SETUP to IN (0x69)
809 destination ^= (0x2D ^ 0x69); /* SETUP -> IN */
810 if (usb_pipeout(pipe))
811 destination ^= (0xE1 ^ 0x69); /* IN -> OUT */
813 prevtd = td;
814 td = uhci_td_alloc(dev);
815 if (!td)
816 return -ENOMEM;
818 prevtd->link = virt_to_bus(td) | UHCI_PTR_DEPTH;
821 * Build the DATA TD's
823 while (len > 0) {
824 /* Build the TD for control status */
825 int pktsze = len;
827 if (pktsze > maxsze)
828 pktsze = maxsze;
830 /* Alternate Data0/1 (start with Data1) */
831 destination ^= 1 << 19;
833 td->status = status; /* Status */
834 td->info = destination | ((pktsze - 1) << 21); /* pktsze bytes of data */
835 td->buffer = virt_to_bus(data);
836 td->backptr = &prevtd->link;
838 data += pktsze;
839 len -= pktsze;
841 prevtd = td;
842 td = uhci_td_alloc(dev);
843 if (!td)
844 return -ENOMEM;
845 prevtd->link = virt_to_bus(td) | UHCI_PTR_DEPTH; /* Update previous TD */
849 * Build the final TD for control status
851 destination ^= (0xE1 ^ 0x69); /* OUT -> IN */
852 destination |= 1 << 19; /* End in Data1 */
854 td->status = status | TD_CTRL_IOC; /* no limit on errors on final packet */
855 td->info = destination | (UHCI_NULL_DATA_SIZE << 21); /* 0 bytes of data */
856 td->buffer = 0;
857 td->backptr = &prevtd->link;
858 td->link = UHCI_PTR_TERM; /* Terminate */
860 /* Start it up.. */
861 ret = uhci_run_control(dev, first, td);
863 count = 100;
864 td = first;
865 do {
866 nextlink = td->link;
867 uhci_remove_td(td);
868 uhci_td_free(td);
870 if (nextlink & UHCI_PTR_TERM) /* Tail? */
871 break;
873 td = uhci_ptr_to_virt(nextlink);
874 } while (--count);
876 if (!count)
877 printk(KERN_ERR "runaway td's!?\n");
879 if (uhci_debug && ret) {
880 __u8 *p = (__u8 *)cmd;
882 printk(KERN_DEBUG "Failed cmd - %02X %02X %02X %02X %02X %02X %02X %02X\n",
883 p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7]);
885 return ret;
889 * Bulk thread operations: we just mark the last TD
890 * in a bulk thread as an interrupt TD, and wake up
891 * the front-end on completion.
893 * We need to remove the TD from the lists (both interrupt
894 * list and TD lists) by hand if something bad happens!
897 /* td points to the last td in the list, which interrupts on completion */
898 static int uhci_run_bulk(struct uhci_device *dev, struct uhci_td *first, struct uhci_td *last, unsigned long *rval)
900 DECLARE_WAITQUEUE(wait, current);
901 struct uhci_qh *qh = uhci_qh_alloc(dev);
903 if (!qh)
904 return -ENOMEM;
906 current->state = TASK_UNINTERRUPTIBLE;
907 add_wait_queue(&qh->wakeup, &wait);
909 uhci_add_irq_list(dev->uhci, last, uhci_generic_completed, &qh->wakeup);
911 #if 0
912 /* FIXME: This is kinda kludged */
913 /* Walk the TD list and update the QH pointer */
915 struct uhci_td *curtd;
916 int count = 100;
918 curtd = first;
919 do {
920 curtd->qh = bulk_qh;
921 if (curtd->link & UHCI_PTR_TERM)
922 break;
924 curtd = uhci_ptr_to_virt(curtd->link);
925 } while (--count);
926 if (!count)
927 printk(KERN_ERR "runaway tds!\n");
929 #endif
931 uhci_insert_tds_in_qh(qh, first, last);
933 /* Add it into the skeleton */
934 uhci_insert_qh(&dev->uhci->skel_bulk_qh, qh);
936 schedule_timeout(HZ*5); /* 5 seconds */
938 remove_wait_queue(&qh->wakeup, &wait);
940 /* Clean up in case it failed.. */
941 uhci_remove_irq_list(last);
943 uhci_remove_qh(&dev->uhci->skel_bulk_qh, qh);
945 uhci_qh_free(qh);
947 return uhci_td_result(dev, last, rval);
951 * Send or receive a bulk message on a pipe.
953 * Note that the "pipe" structure is set up to map
954 * easily to the uhci destination fields.
956 * A bulk message is only built up from
957 * the data phase
959 static int uhci_bulk_msg(struct usb_device *usb_dev, unsigned int pipe, void *data, int len, unsigned long *rval)
961 struct uhci_device *dev = usb_to_uhci(usb_dev);
962 struct uhci_td *first, *td, *prevtd;
963 unsigned long destination, status;
964 int ret;
965 int maxsze = usb_maxpacket(usb_dev, pipe);
967 if (usb_endpoint_halted(usb_dev, usb_pipeendpoint(pipe)) &&
968 usb_clear_halt(usb_dev, usb_pipeendpoint(pipe) | (pipe & 0x80)))
969 return USB_ST_STALL;
971 /* The "pipe" thing contains the destination in bits 8--18 */
972 destination = (pipe & PIPE_DEVEP_MASK) | usb_packetid (pipe);
974 /* 3 errors */
975 status = (pipe & TD_CTRL_LS) | TD_CTRL_ACTIVE | TD_CTRL_SPD | (3 << 27);
978 * Build the TDs for the bulk request
980 first = td = uhci_td_alloc(dev);
981 if (!td)
982 return -ENOMEM;
984 prevtd = first; //This is fake, but at least it's not NULL
985 while (len > 0) {
986 /* Build the TD for control status */
987 int pktsze = len;
989 if (pktsze > maxsze)
990 pktsze = maxsze;
992 td->status = status; /* Status */
993 td->info = destination | ((pktsze-1) << 21) |
994 (usb_gettoggle(usb_dev, usb_pipeendpoint(pipe), usb_pipeout(pipe)) << 19); /* pktsze bytes of data */
995 td->buffer = virt_to_bus(data);
996 td->backptr = &prevtd->link;
998 data += maxsze;
999 len -= maxsze;
1001 if (len > 0) {
1002 prevtd = td;
1003 td = uhci_td_alloc(dev);
1004 if (!td)
1005 return -ENOMEM;
1007 prevtd->link = virt_to_bus(td) | UHCI_PTR_DEPTH;/* Update previous TD */
1010 /* Alternate Data0/1 (start with Data0) */
1011 usb_dotoggle(usb_dev, usb_pipeendpoint(pipe), usb_pipeout(pipe));
1014 td->link = 1; /* Terminate */
1015 td->status |= TD_CTRL_IOC;
1017 /* CHANGE DIRECTION HERE! SAVE IT SOMEWHERE IN THE ENDPOINT!!! */
1019 /* Start it up.. */
1020 ret = uhci_run_bulk(dev, first, td, rval);
1023 int count = 100;
1024 struct uhci_td *curtd = first;
1025 unsigned int nextlink;
1027 do {
1028 nextlink = curtd->link;
1029 uhci_remove_td(curtd);
1030 uhci_td_free(curtd);
1032 if (nextlink & UHCI_PTR_TERM) /* Tail? */
1033 break;
1035 curtd = uhci_ptr_to_virt(nextlink);
1036 } while (--count);
1038 if (!count)
1039 printk(KERN_DEBUG "runaway td's!?\n");
1042 return ret;
1045 static void * uhci_request_bulk(struct usb_device *usb_dev, unsigned int pipe, usb_device_irq handler, void *data, int len, void *dev_id)
1047 struct uhci_device *dev = usb_to_uhci(usb_dev);
1048 struct uhci *uhci = dev->uhci;
1049 struct uhci_td *first, *td, *prevtd;
1050 struct uhci_qh *bulk_qh = uhci_qh_alloc(dev);
1051 unsigned long destination, status;
1052 int maxsze = usb_maxpacket(usb_dev, pipe);
1054 /* The "pipe" thing contains the destination in bits 8--18, 0x69 is IN */
1055 destination = (pipe & 0x0007ff00) | usb_packetid(pipe);
1057 /* Infinite errors is 0 */
1058 status = (pipe & TD_CTRL_LS) | TD_CTRL_ACTIVE | TD_CTRL_SPD;
1060 /* Build the TDs for the bulk request */
1061 first = td = uhci_td_alloc(dev);
1062 prevtd = td;
1063 while (len > 0) {
1064 /* Build the TD for control status */
1065 int pktsze = len;
1067 if (pktsze > maxsze)
1068 pktsze = maxsze;
1070 td->status = status; /* Status */
1071 td->info = destination | ((pktsze-1) << 21) |
1072 (usb_gettoggle(usb_dev, usb_pipeendpoint(pipe), usb_pipeout(pipe)) << 19); /* pktsze bytes of data */
1073 td->buffer = virt_to_bus(data);
1074 td->backptr = &prevtd->link;
1075 td->qh = bulk_qh;
1076 td->dev = dev;
1077 data += pktsze;
1078 len -= pktsze;
1080 if (len > 0) {
1081 prevtd = td;
1082 td = uhci_td_alloc(dev);
1083 prevtd->link = virt_to_bus(td) | UHCI_PTR_DEPTH;
1086 /* Alternate Data0/1 */
1087 usb_dotoggle(usb_dev, usb_pipeendpoint(pipe), usb_pipeout(pipe));
1090 first->backptr = NULL;
1091 td->link = 1; /* Terminate */
1092 td->status = status | TD_CTRL_IOC; /* IOC */
1094 uhci_add_irq_list(dev->uhci, td, handler, dev_id);
1096 uhci_insert_tds_in_qh(bulk_qh, first, td);
1098 bulk_qh->skel = &uhci->skel_bulk_qh;
1099 uhci_insert_qh(&uhci->skel_bulk_qh, bulk_qh);
1101 /* Return last td for removal */
1102 return td;
1106 *Remove a handler from a pipe. This terminates the transfer.
1107 *We have some assumptions here:
1108 * There is only one queue using this pipe. (the one we remove)
1109 * Any data that is in the queue is useless for us, we throw it away.
1111 static int uhci_terminate_bulk(struct usb_device *dev, void * first)
1113 /* none found? there is nothing to remove! */
1114 if (!first)
1115 return 0;
1117 uhci_remove_transfer(first,1);
1119 return 1;
1122 static struct usb_device *uhci_usb_alloc(struct usb_device *parent)
1124 struct usb_device *usb_dev;
1125 struct uhci_device *dev;
1127 /* Allocate the USB device */
1128 usb_dev = kmalloc(sizeof(*usb_dev), GFP_KERNEL);
1129 if (!usb_dev)
1130 return NULL;
1132 memset(usb_dev, 0, sizeof(*usb_dev));
1134 /* Allocate the UHCI device private data */
1135 dev = kmalloc(sizeof(*dev), GFP_KERNEL);
1136 if (!dev) {
1137 kfree(usb_dev);
1138 return NULL;
1141 /* Initialize "dev" */
1142 memset(dev, 0, sizeof(*dev));
1144 usb_dev->hcpriv = dev;
1145 dev->usb = usb_dev;
1147 usb_dev->parent = parent;
1149 if (parent) {
1150 usb_dev->bus = parent->bus;
1151 dev->uhci = usb_to_uhci(parent)->uhci;
1154 return usb_dev;
1157 static int uhci_usb_free(struct usb_device *usb_dev)
1159 struct uhci_device *dev = usb_to_uhci(usb_dev);
1161 kfree(dev);
1162 usb_destroy_configuration(usb_dev);
1163 kfree(usb_dev);
1165 return 0;
1168 struct usb_operations uhci_device_operations = {
1169 uhci_usb_alloc,
1170 uhci_usb_free,
1171 uhci_control_msg,
1172 uhci_bulk_msg,
1173 uhci_request_irq,
1174 uhci_release_irq,
1175 uhci_request_bulk,
1176 uhci_terminate_bulk,
1177 uhci_allocate_isochronous,
1178 uhci_delete_isochronous,
1179 uhci_schedule_isochronous,
1180 uhci_unschedule_isochronous,
1181 uhci_compress_isochronous
1185 * This is just incredibly fragile. The timings must be just
1186 * right, and they aren't really documented very well.
1188 * Note the short delay between disabling reset and enabling
1189 * the port..
1191 static void uhci_reset_port(unsigned int port)
1193 unsigned short status;
1195 status = inw(port);
1196 outw(status | USBPORTSC_PR, port); /* reset port */
1197 wait_ms(10);
1198 outw(status & ~USBPORTSC_PR, port);
1199 udelay(5);
1201 status = inw(port);
1202 outw(status | USBPORTSC_PE, port); /* enable port */
1203 wait_ms(10);
1205 status = inw(port);
1206 if(!(status & USBPORTSC_PE)) {
1207 outw(status | USBPORTSC_PE, port); /* one more try at enabling port */
1208 wait_ms(50);
1214 * This gets called if the connect status on the root
1215 * hub (and the root hub only) changes.
1217 static void uhci_connect_change(struct uhci *uhci, unsigned int port, unsigned int nr)
1219 struct usb_device *usb_dev;
1220 struct uhci_device *dev;
1221 unsigned short status;
1222 struct uhci_device *root_hub = usb_to_uhci(uhci->bus->root_hub);
1224 #ifdef UHCI_DEBUG
1225 printk(KERN_INFO "uhci_connect_change: called for %d\n", nr);
1226 #endif
1229 * Even if the status says we're connected,
1230 * the fact that the status bits changed may
1231 * that we got disconnected and then reconnected.
1233 * So start off by getting rid of any old devices..
1235 usb_disconnect(&root_hub->usb->children[nr]);
1237 status = inw(port);
1239 /* If we have nothing connected, then clear change status and disable the port */
1240 status = (status & ~USBPORTSC_PE) | USBPORTSC_PEC;
1241 if (!(status & USBPORTSC_CCS)) {
1242 outw(status, port);
1243 return;
1247 * Ok, we got a new connection. Allocate a device to it,
1248 * and find out what it wants to do..
1250 usb_dev = uhci_usb_alloc(root_hub->usb);
1251 if (!usb_dev)
1252 return;
1254 dev = usb_dev->hcpriv;
1256 usb_connect(usb_dev);
1258 root_hub->usb->children[nr] = usb_dev;
1260 wait_ms(200); /* wait for powerup */
1261 uhci_reset_port(port);
1263 /* Get speed information */
1264 usb_dev->slow = (inw(port) & USBPORTSC_LSDA) ? 1 : 0;
1267 * Ok, all the stuff specific to the root hub has been done.
1268 * The rest is generic for any new USB attach, regardless of
1269 * hub type.
1271 if (usb_new_device(usb_dev)) {
1272 unsigned short status = inw(port);
1274 printk(KERN_INFO "uhci: disabling malfunctioning port %d\n",
1275 nr + 1);
1276 outw(status | USBPORTSC_PE, port);
1281 * This gets called when the root hub configuration
1282 * has changed. Just go through each port, seeing if
1283 * there is something interesting happening.
1285 static void uhci_check_configuration(struct uhci *uhci)
1287 struct uhci_device *root_hub = usb_to_uhci(uhci->bus->root_hub);
1288 unsigned int io_addr = uhci->io_addr + USBPORTSC1;
1289 int maxchild = root_hub->usb->maxchild;
1290 int nr = 0;
1292 do {
1293 unsigned short status = inw(io_addr);
1295 if (status & USBPORTSC_CSC)
1296 uhci_connect_change(uhci, io_addr, nr);
1298 nr++; io_addr += 2;
1299 } while (nr < maxchild);
1302 static void uhci_interrupt_notify(struct uhci *uhci)
1304 struct list_head *tmp, *head = &uhci->interrupt_list;
1305 int status;
1307 spin_lock(&irqlist_lock);
1308 tmp = head->next;
1309 while (tmp != head) {
1310 struct uhci_td *first, *td = list_entry(tmp,
1311 struct uhci_td, irq_list);
1313 tmp = tmp->next;
1315 /* We check the TD which had the IOC bit as well as the */
1316 /* first TD */
1317 /* XXX: Shouldn't we check all of the TD's in the chain? */
1318 if ((td->qh) && (td->qh->element & ~UHCI_PTR_BITS))
1319 first = uhci_link_to_td(td->qh->element);
1320 else
1321 first = NULL;
1323 /* If any of the error bits are set OR the active is NOT set */
1324 /* then we're interested in this TD */
1325 status = td->status & 0xF60000;
1327 if ((!(status ^ TD_CTRL_ACTIVE)) && (first) &&
1328 (!(first->status & TD_CTRL_ACTIVE)))
1329 status = first->status & 0xF60000;
1331 if (!(status ^ TD_CTRL_ACTIVE))
1332 continue;
1335 /* remove from IRQ list */
1336 list_del(&td->irq_list);
1337 INIT_LIST_HEAD(&td->irq_list);
1339 if (td->completed(uhci_map_status(status, 0),
1340 bus_to_virt(td->buffer), -1, td->dev_id)) {
1341 list_add(&td->irq_list, &uhci->interrupt_list);
1343 /* Isochronous TD's don't need this */
1344 if (!(td->status & TD_CTRL_IOS)) {
1345 struct usb_device *usb_dev = td->dev->usb;
1347 usb_dotoggle(usb_dev, usb_pipeendpoint(td->info), usb_pipeout(td->info));
1348 td->info &= ~(1 << 19); /* clear data toggle */
1349 td->info |= usb_gettoggle(usb_dev, usb_pipeendpoint(td->info), usb_pipeout(td->info)) << 19; /* toggle between data0 and data1 */
1350 td->status = (td->status & 0x2F000000) | TD_CTRL_ACTIVE | TD_CTRL_IOC;
1351 /* The HC removes it, so readd it */
1352 uhci_insert_td_in_qh(td->qh, td);
1354 } else if (td->flags & UHCI_TD_REMOVE) {
1355 struct usb_device *usb_dev = td->dev->usb;
1357 /* marked for removal */
1358 td->flags &= ~UHCI_TD_REMOVE;
1359 usb_dotoggle(usb_dev, usb_pipeendpoint(td->info), usb_pipeout(td->info));
1360 uhci_remove_qh(td->qh->skel, td->qh);
1361 uhci_qh_free(td->qh);
1362 uhci_td_free(td);
1365 /* If completed does not wants to reactivate, then */
1366 /* it's responsible for free'ing the TD's and QH's */
1367 /* or another function (such as run_control) */
1369 spin_unlock(&irqlist_lock);
1373 * Check port status - Connect Status Change - for
1374 * each of the attached ports (defaults to two ports,
1375 * but at least in theory there can be more of them).
1377 * Wake up the configurator if something happened, we
1378 * can't really do much at interrupt time.
1380 static void uhci_root_hub_events(struct uhci *uhci, unsigned int io_addr)
1382 if (waitqueue_active(&uhci_configure)) {
1383 struct uhci_device *root_hub = usb_to_uhci(uhci->bus->root_hub);
1384 int ports = root_hub->usb->maxchild;
1386 io_addr += USBPORTSC1;
1387 do {
1388 if (inw(io_addr) & USBPORTSC_CSC) {
1389 wake_up(&uhci_configure);
1390 return;
1392 io_addr += 2;
1393 } while (--ports > 0);
1397 static void uhci_interrupt(int irq, void *__uhci, struct pt_regs *regs)
1399 struct uhci *uhci = __uhci;
1400 unsigned int io_addr = uhci->io_addr;
1401 unsigned short status;
1404 * Read the interrupt status, and write it back to clear the interrupt cause
1406 status = inw(io_addr + USBSTS);
1407 outw(status, io_addr + USBSTS);
1409 /* Walk the list of pending TD's to see which ones completed.. */
1410 uhci_interrupt_notify(uhci);
1412 /* Check if there are any events on the root hub.. */
1413 uhci_root_hub_events(uhci, io_addr);
1417 * We init one packet, and mark it just IOC and _not_
1418 * active. Which will result in no actual USB traffic,
1419 * but _will_ result in an interrupt every second.
1421 * Which is exactly what we want.
1423 static void uhci_init_ticktd(struct uhci *uhci)
1425 struct uhci_device *dev = usb_to_uhci(uhci->bus->root_hub);
1426 struct uhci_td *td = uhci_td_alloc(dev);
1428 if (!td) {
1429 printk(KERN_ERR "unable to allocate ticktd\n");
1430 return;
1433 /* Don't clobber the frame */
1434 td->link = uhci->fl->frame[0];
1435 td->status = TD_CTRL_IOC;
1436 td->info = (15 << 21) | 0x7f69; /* (ignored) input packet, 16 bytes, device 127 */
1437 td->buffer = 0;
1438 td->qh = NULL;
1440 uhci->fl->frame[0] = virt_to_bus(td);
1442 uhci->ticktd = td;
1445 static void reset_hc(struct uhci *uhci)
1447 unsigned int io_addr = uhci->io_addr;
1449 /* Global reset for 50ms */
1450 outw(USBCMD_GRESET, io_addr + USBCMD);
1451 wait_ms(50);
1452 outw(0, io_addr + USBCMD);
1453 wait_ms(10);
1456 static void start_hc(struct uhci *uhci)
1458 unsigned int io_addr = uhci->io_addr;
1459 int timeout = 1000;
1461 uhci_init_ticktd(uhci);
1464 * Reset the HC - this will force us to get a
1465 * new notification of any already connected
1466 * ports due to the virtual disconnect that it
1467 * implies.
1469 outw(USBCMD_HCRESET, io_addr + USBCMD);
1470 while (inw(io_addr + USBCMD) & USBCMD_HCRESET) {
1471 if (!--timeout) {
1472 printk(KERN_ERR "USBCMD_HCRESET timed out!\n");
1473 break;
1477 /* Turn on all interrupts */
1478 outw(USBINTR_TIMEOUT | USBINTR_RESUME | USBINTR_IOC | USBINTR_SP, io_addr + USBINTR);
1480 /* Start at frame 0 */
1481 outw(0, io_addr + USBFRNUM);
1482 outl(virt_to_bus(uhci->fl), io_addr + USBFLBASEADD);
1484 /* Run and mark it configured with a 64-byte max packet */
1485 outw(USBCMD_RS | USBCMD_CF | USBCMD_MAXP, io_addr + USBCMD);
1489 * Allocate a frame list, and four regular queues.
1491 * The hardware doesn't really know any difference
1492 * in the queues, but the order does matter for the
1493 * protocols higher up. The order is:
1495 * - any isochronous events handled before any
1496 * of the queues. We don't do that here, because
1497 * we'll create the actual TD entries on demand.
1498 * - The first queue is the "interrupt queue".
1499 * - The second queue is the "control queue".
1500 * - The third queue is "bulk data".
1502 * We could certainly have multiple queues of the same
1503 * type, and maybe we should. We could have per-device
1504 * queues, for example. We begin small.
1506 * Queues are dynamically allocated for devices now,
1507 * this code only sets up the skeleton queue
1509 static struct uhci *alloc_uhci(unsigned int io_addr)
1511 int i;
1512 struct uhci *uhci;
1513 struct usb_bus *bus;
1514 struct uhci_device *dev;
1515 struct usb_device *usb;
1517 uhci = kmalloc(sizeof(*uhci), GFP_KERNEL);
1518 if (!uhci)
1519 return NULL;
1521 memset(uhci, 0, sizeof(*uhci));
1523 uhci->irq = -1;
1524 uhci->io_addr = io_addr;
1525 INIT_LIST_HEAD(&uhci->interrupt_list);
1527 /* We need exactly one page (per UHCI specs), how convenient */
1528 uhci->fl = (void *)__get_free_page(GFP_KERNEL);
1529 if (!uhci->fl)
1530 goto au_free_uhci;
1532 bus = usb_alloc_bus(&uhci_device_operations);
1533 if (!bus)
1534 goto au_free_fl;
1536 uhci->bus = bus;
1537 bus->hcpriv = uhci;
1540 * Allocate the root_hub
1542 usb = uhci_usb_alloc(NULL);
1543 if (!usb)
1544 goto au_free_bus;
1546 usb->bus = bus;
1548 dev = usb_to_uhci(usb);
1549 dev->uhci = uhci;
1551 uhci->bus->root_hub = uhci_to_usb(dev);
1553 /* Initialize the root hub */
1554 /* UHCI specs says devices must have 2 ports, but goes on to say */
1555 /* they may have more but give no way to determine how many they */
1556 /* have, so default to 2 */
1557 usb->maxchild = 2;
1558 usb_init_root_hub(usb);
1560 /* 8 Interrupt queues */
1561 for (i = 0; i < 8; i++) {
1562 struct uhci_qh *qh = &uhci->skelqh[i];
1564 qh->link = virt_to_bus(&uhci->skel_control_qh) | UHCI_PTR_QH;
1565 qh->element = UHCI_PTR_TERM;
1568 uhci->skel_control_qh.link = virt_to_bus(&uhci->skel_bulk_qh) |
1569 UHCI_PTR_QH;
1570 uhci->skel_control_qh.element = UHCI_PTR_TERM;
1572 uhci->skel_bulk_qh.link = UHCI_PTR_TERM;
1573 uhci->skel_bulk_qh.element = UHCI_PTR_TERM;
1576 * Fill the frame list: make all entries point to
1577 * the proper interrupt queue.
1579 * This is probably silly, but it's a simple way to
1580 * scatter the interrupt queues in a way that gives
1581 * us a reasonable dynamic range for irq latencies.
1583 for (i = 0; i < 1024; i++) {
1584 struct uhci_qh *irq = &uhci->skel_int2_qh;
1585 if (i & 1) {
1586 irq++;
1587 if (i & 2) {
1588 irq++;
1589 if (i & 4) {
1590 irq++;
1591 if (i & 8) {
1592 irq++;
1593 if (i & 16) {
1594 irq++;
1595 if (i & 32) {
1596 irq++;
1597 if (i & 64) {
1598 irq++;
1606 uhci->fl->frame[i] = virt_to_bus(irq) | UHCI_PTR_QH;
1609 return uhci;
1612 * error exits:
1615 au_free_bus:
1616 usb_free_bus(bus);
1617 au_free_fl:
1618 free_page((unsigned long)uhci->fl);
1619 au_free_uhci:
1620 kfree(uhci);
1621 return NULL;
1625 * De-allocate all resources..
1627 static void release_uhci(struct uhci *uhci)
1629 if (uhci->irq >= 0) {
1630 free_irq(uhci->irq, uhci);
1631 uhci->irq = -1;
1634 if (uhci->ticktd) {
1635 uhci_td_free(uhci->ticktd);
1636 uhci->ticktd = NULL;
1639 if (uhci->fl) {
1640 free_page((unsigned long)uhci->fl);
1641 uhci->fl = NULL;
1644 usb_free_bus(uhci->bus);
1645 kfree(uhci);
1648 static int uhci_control_thread(void * __uhci)
1650 struct uhci *uhci = (struct uhci *)__uhci;
1652 uhci->control_running = 1;
1654 lock_kernel();
1657 * This thread doesn't need any user-level access,
1658 * so get rid of all our resources..
1660 exit_mm(current);
1661 exit_files(current);
1663 strcpy(current->comm, "uhci-control");
1666 * Ok, all systems are go..
1668 do {
1669 siginfo_t info;
1670 int unsigned long signr;
1672 #ifdef CONFIG_APM
1673 if (apm_resume) {
1674 apm_resume = 0;
1675 start_hc(uhci);
1676 continue;
1678 #endif
1679 uhci_check_configuration(uhci);
1681 interruptible_sleep_on(&uhci_configure);
1683 if (signal_pending(current)) {
1684 /* sending SIGUSR1 makes us print out some info */
1685 spin_lock_irq(&current->sigmask_lock);
1686 signr = dequeue_signal(&current->blocked, &info);
1687 spin_unlock_irq(&current->sigmask_lock);
1689 if (signr == SIGUSR1) {
1690 printk(KERN_DEBUG "UHCI queue dump:\n");
1691 show_queues(uhci);
1692 } else if (signr == SIGUSR2) {
1693 uhci_debug = !uhci_debug;
1694 printk(KERN_DEBUG "UHCI debug toggle = %x\n",
1695 uhci_debug);
1696 } else
1697 break;
1699 } while (uhci->control_continue);
1702 MOD_DEC_USE_COUNT;
1705 uhci->control_running = 0;
1707 return 0;
1711 * If we've successfully found a UHCI, now is the time to increment the
1712 * module usage count, start the control thread, and return success..
1714 static int found_uhci(int irq, unsigned int io_addr)
1716 int retval;
1717 struct uhci *uhci;
1719 uhci = alloc_uhci(io_addr);
1720 if (!uhci)
1721 return -ENOMEM;
1723 INIT_LIST_HEAD(&uhci->uhci_list);
1724 list_add(&uhci->uhci_list, &uhci_list);
1726 request_region(uhci->io_addr, 32, "usb-uhci");
1728 reset_hc(uhci);
1730 usb_register_bus(uhci->bus);
1731 start_hc(uhci);
1733 uhci->control_continue = 1;
1735 retval = -EBUSY;
1736 if (request_irq(irq, uhci_interrupt, SA_SHIRQ, "uhci", uhci) == 0) {
1737 int pid;
1739 uhci->irq = irq;
1740 pid = kernel_thread(uhci_control_thread, uhci,
1741 CLONE_FS | CLONE_FILES | CLONE_SIGHAND);
1742 if (pid >= 0) {
1743 uhci->control_pid = pid;
1745 return(pid);
1748 retval = pid;
1751 reset_hc(uhci);
1752 release_region(uhci->io_addr, 32);
1754 release_uhci(uhci);
1755 return retval;
1758 static int start_uhci(struct pci_dev *dev)
1760 int i;
1762 /* Search for the IO base address.. */
1763 for (i = 0; i < 6; i++) {
1764 unsigned int io_addr = dev->resource[i].start;
1766 /* IO address? */
1767 if (!(dev->resource[i].flags & 1))
1768 continue;
1770 #if 0
1771 /* Is it already in use? */
1772 if (check_region(io_addr, 32))
1773 break;
1774 #endif
1776 return found_uhci(dev->irq, io_addr);
1778 return -1;
1781 #ifdef CONFIG_APM
1782 static int handle_apm_event(apm_event_t event)
1784 static int down = 0;
1786 switch (event) {
1787 case APM_SYS_SUSPEND:
1788 case APM_USER_SUSPEND:
1789 if (down) {
1790 printk(KERN_DEBUG "uhci: received extra suspend event\n");
1791 break;
1793 down = 1;
1794 break;
1795 case APM_NORMAL_RESUME:
1796 case APM_CRITICAL_RESUME:
1797 if (!down) {
1798 printk(KERN_DEBUG "uhci: received bogus resume event\n");
1799 break;
1801 down = 0;
1802 if (waitqueue_active(&uhci_configure)) {
1803 apm_resume = 1;
1804 wake_up(&uhci_configure);
1806 break;
1808 return 0;
1810 #endif
1812 int uhci_init(void)
1814 int retval;
1815 struct pci_dev *dev = NULL;
1816 u8 type;
1817 char *name;
1819 /* FIXME: This is lame, but I guess it's better to leak memory than */
1820 /* crash */
1821 name = kmalloc(10, GFP_KERNEL);
1822 if (!name)
1823 return -ENOMEM;
1825 strcpy(name, "uhci_td");
1827 uhci_td_cachep = kmem_cache_create(name,
1828 sizeof(struct uhci_td), 0,
1829 SLAB_HWCACHE_ALIGN, NULL, NULL);
1831 if (!uhci_td_cachep)
1832 return -ENOMEM;
1834 name = kmalloc(10, GFP_KERNEL);
1835 if (!name)
1836 return -ENOMEM;
1838 strcpy(name, "uhci_qh");
1840 uhci_qh_cachep = kmem_cache_create(name,
1841 sizeof(struct uhci_qh), 0,
1842 SLAB_HWCACHE_ALIGN, NULL, NULL);
1844 if (!uhci_qh_cachep)
1845 return -ENOMEM;
1847 retval = -ENODEV;
1848 for (;;) {
1849 dev = pci_find_class(PCI_CLASS_SERIAL_USB << 8, dev);
1850 if (!dev)
1851 break;
1852 /* Is it UHCI */
1853 pci_read_config_byte(dev, PCI_CLASS_PROG, &type);
1854 if(type != 0)
1855 continue;
1856 /* Ok set it up */
1857 retval = start_uhci(dev);
1858 if (retval < 0)
1859 continue;
1861 #ifdef CONFIG_APM
1862 apm_register_callback(&handle_apm_event);
1863 #endif
1864 return 0;
1866 return retval;
1869 void uhci_cleanup(void)
1871 struct list_head *next, *tmp, *head = &uhci_list;
1872 int ret, i;
1874 tmp = head->next;
1875 while (tmp != head) {
1876 struct uhci *uhci = list_entry(tmp, struct uhci, uhci_list);
1877 struct uhci_device *root_hub = usb_to_uhci(uhci->bus->root_hub);
1879 next = tmp->next;
1881 list_del(&uhci->uhci_list);
1882 INIT_LIST_HEAD(&uhci->uhci_list);
1884 /* Check if the process is still running */
1885 ret = kill_proc(uhci->control_pid, 0, 1);
1886 if (!ret) {
1887 int count = 10;
1889 uhci->control_continue = 0;
1890 wake_up(&uhci_configure);
1892 while (uhci->control_running && --count) {
1893 current->state = TASK_INTERRUPTIBLE;
1894 schedule_timeout(1);
1897 if (!count)
1898 printk(KERN_ERR "uhci: giving up on killing uhci-control\n");
1901 if (root_hub)
1902 for (i = 0; i < root_hub->usb->maxchild; i++)
1903 usb_disconnect(root_hub->usb->children + i);
1905 usb_deregister_bus(uhci->bus);
1907 reset_hc(uhci);
1908 release_region(uhci->io_addr, 32);
1910 release_uhci(uhci);
1912 tmp = next;
1915 if (kmem_cache_destroy(uhci_qh_cachep))
1916 printk(KERN_INFO "uhci: not all QH's were freed\n");
1918 if (kmem_cache_destroy(uhci_td_cachep))
1919 printk(KERN_INFO "uhci: not all TD's were freed\n");
1922 #ifdef MODULE
1923 int init_module(void)
1925 return uhci_init();
1928 void cleanup_module(void)
1930 #ifdef CONFIG_APM
1931 apm_unregister_callback(&handle_apm_event);
1932 #endif
1933 uhci_cleanup();
1935 #endif //MODULE