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>
38 #include <asm/system.h>
44 #include <linux/apm_bios.h>
45 static int handle_apm_event(apm_event_t event
);
46 static int apm_resume
= 0;
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
)
59 struct uhci_td
*tmp
= td
->first
;
61 /* locate the first failing td, if any */
64 status
= (tmp
->status
>> 16) & 0xff;
67 if ((tmp
->link
& 1) || (tmp
->link
& 2))
69 tmp
= bus_to_virt(tmp
->link
& ~0xF);
74 /* Some debugging code */
75 if (status
&& (!usb_pipeendpoint(tmp
->info
) || !(status
& 0x08)) ) {
79 printk("uhci_td_result() failed with status %x\n", status
);
80 show_status(dev
->uhci
);
83 if ((tmp
->link
& 1) || (tmp
->link
& 2))
85 tmp
= bus_to_virt(tmp
->link
& ~0xF);
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 */
95 /* sum up packets that did not fail */
96 if(rval
&& !((tmp
->status
>> 16) & 0xff))
97 *rval
+= (tmp
->status
& 0x3ff) + 1;
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);
108 if ((tmp
->link
& 1) || (tmp
->link
& 2))
110 tmp
= bus_to_virt(tmp
->link
& ~0xF);
114 printk("uhci_td_result returning partial count %d\n", *rval
);
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
128 * NOTE! This assumes that first->last is a valid
129 * list of TD's with the proper backpointers set
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
);
138 unsigned char success
;
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)
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
;
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
) {
177 lqh
= bus_to_virt(lqh
->link
& ~0xF);
181 printk("couldn't find qh in chain!\n");
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
;
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..
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
)
234 static struct uhci_qh
*uhci_qh_allocate(struct uhci_device
*dev
)
240 for (; (inuse
= test_and_set_bit(0, &qh
->inuse
)) != 0 && qh
< &dev
->qh
[UHCI_MAXQH
]; qh
++)
246 printk("ran out of qh's for dev %p\n", dev
);
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);
258 clear_bit(0, &qh
->inuse
);
261 static struct uhci_td
*uhci_td_allocate(struct uhci_device
*dev
)
267 for (; (inuse
= test_and_set_bit(0, &td
->inuse
)) != 0 && td
< &dev
->td
[UHCI_MAXTD
]; td
++)
273 printk("ran out of td's for dev %p\n", dev
);
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
)
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
)
297 td
->completed
= completed
;
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
)
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
);
336 td
->status
= status
; /* In */
337 td
->info
= destination
| (7 << 21); /* 8 bytes of data */
338 td
->buffer
= virt_to_bus(dev
->data
);
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
);
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
;
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
);
371 printk("underrun: %d %d\n", i
, n
);
373 if ((isodesc
->td
[i
].status
>> 16) & 0xFF)
374 printk("error: %d %X\n", i
, (isodesc
->td
[i
].status
>> 16));
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
;
390 if ((isodesc
->frame
< 0) || (isodesc
->frame
> 1023))
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
;
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
;
414 if (isodesc
->frame
!= -1) {
415 printk("isoc queue not removed\n");
416 uhci_unsched_isochronous(usb_dev
, isodesc
);
419 /* Insert TD into list */
421 frame
= inw(uhci
->io_addr
+ USBFRNUM
) % 1024;
422 /* HACK: Start 2 frames from now */
423 frame
= (frame
+ 2) % 1024;
425 frame
= (pisodesc
->endframe
+ 1) % 1024;
428 printk("scheduling first at frame %d\n", frame
);
431 for (i
= 0; i
< isodesc
->num
; i
++) {
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
]);
440 printk("last at frame %d\n", (frame
+ i
- 1) % 1024);
444 isodesc
->td
[i
- 1].status
|= (1 << 24);
446 isodesc
->frame
= frame
;
447 isodesc
->endframe
= (frame
+ isodesc
->num
- 1) % 1024;
450 return uhci_td_result(dev
, td
[num
- 1]);
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
;
463 struct uhci_iso_td
*isodesc
;
466 isodesc
= kmalloc(sizeof(*isodesc
), GFP_KERNEL
);
468 printk("Couldn't allocate isodesc!\n");
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
;
481 printk("Couldn't allocate td's\n");
486 isodesc
->frame
= isodesc
->endframe
= -1;
489 * Build the DATA TD's
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 */
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
511 td
->info
= destination
| ((maxsze
- 1) << 21);
512 td
->buffer
= virt_to_bus(data
);
520 if (((int)data
% PAGE_SIZE
) + maxsze
>= PAGE_SIZE
)
521 data
= (char *)(((int)data
+ maxsze
) & ~(PAGE_SIZE
- 1));
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
);
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 */
539 uhci_unsched_isochronous(usb_dev
, isodesc
);
541 /* Remove it from the IRQ list */
542 uhci_remove_irq_list(&isodesc
->td
[isodesc
->num
- 1]);
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 */
587 curtd
= bus_to_virt(curtd
->link
& ~0xF);
589 printk("runaway tds!\n");
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
);
608 printk("Looking for tds [%p, %p]\n", dev
->control_td
, td
);
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
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
;
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
);
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 */
676 td
= uhci_td_allocate(dev
);
677 prevtd
->link
= 4 | virt_to_bus(td
);
680 * Build the DATA TD's
683 /* Build the TD for control status */
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
);
696 td
->backptr
= &prevtd
->link
;
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 */
718 td
->backptr
= &prevtd
->link
;
721 ret
= uhci_run_control(dev
, first
, td
);
725 struct uhci_td
*curtd
= first
;
726 unsigned int nextlink
;
729 nextlink
= curtd
->link
;
730 uhci_remove_td(curtd
);
731 uhci_td_deallocate(curtd
);
732 if (nextlink
& 1) /* Tail? */
735 curtd
= bus_to_virt(nextlink
& ~0xF);
737 printk("runaway td's!?\n");
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]);
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 */
794 curtd
= bus_to_virt(curtd
->link
& ~0xF);
796 printk("runaway tds!\n");
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
);
824 printk("Looking for tds [%p, %p]\n", dev
->control_td
, td
);
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
844 * The data phase can be an arbitrary number of TD's
845 * although we currently had better not have more than
848 * 31 TD's is a minimum of 248 bytes worth of bulk
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
;
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;
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
884 /* Build the TD for control status */
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
;
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 */
912 ret
= uhci_run_bulk(dev
, first
, td
, rval
);
916 struct uhci_td
*curtd
= first
;
917 unsigned int nextlink
;
920 nextlink
= curtd
->link
;
921 uhci_remove_td(curtd
);
922 uhci_td_deallocate(curtd
);
923 if (nextlink
& 1) /* Tail? */
926 curtd
= bus_to_virt(nextlink
& ~0xF);
928 printk("runaway td's!?\n");
937 static struct usb_device
*uhci_usb_allocate(struct usb_device
*parent
)
939 struct usb_device
*usb_dev
;
940 struct uhci_device
*dev
;
943 usb_dev
= kmalloc(sizeof(*usb_dev
), GFP_KERNEL
);
947 memset(usb_dev
, 0, sizeof(*usb_dev
));
949 dev
= kmalloc(sizeof(*dev
), GFP_KERNEL
);
951 usb_destroy_configuration(usb_dev
);
956 /* Initialize "dev" */
957 memset(dev
, 0, sizeof(*dev
));
959 usb_dev
->hcpriv
= dev
;
962 usb_dev
->parent
= 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
++) {
972 dev
->qh
[i
].element
= 1;
973 dev
->qh
[i
].inuse
= 0;
976 for (i
= 0; i
< UHCI_MAXTD
; i
++) {
978 dev
->td
[i
].inuse
= 0;
984 static int uhci_usb_deallocate(struct usb_device
*usb_dev
)
986 struct uhci_device
*dev
= usb_to_uhci(usb_dev
);
989 /* There are UHCI_MAXTD preallocated tds */
990 for (i
= 0; i
< UHCI_MAXTD
; ++i
) {
991 struct uhci_td
*td
= dev
->td
+ i
;
996 /* And remove it from the irq list, if it's active */
997 if (td
->status
& (1 << 23))
998 td
->status
&= ~(1 << 23);
1000 uhci_remove_irq_list(td
);
1005 /* Remove the td from any queues */
1006 for (i
= 0; i
< UHCI_MAXQH
; ++i
) {
1007 struct uhci_qh
*qh
= dev
->qh
+ i
;
1010 uhci_remove_qh(qh
->skel
, qh
);
1014 usb_destroy_configuration(usb_dev
);
1020 struct usb_operations uhci_device_operations
= {
1022 uhci_usb_deallocate
,
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
1035 static void uhci_reset_port(unsigned int port
)
1037 unsigned short status
;
1040 outw(status
| USBPORTSC_PR
, port
); /* reset port */
1042 outw(status
& ~USBPORTSC_PR
, port
);
1046 outw(status
| USBPORTSC_PE
, port
); /* enable port */
1050 if(!(status
& USBPORTSC_PE
)) {
1051 outw(status
| USBPORTSC_PE
, port
); /* one more try at enabling port */
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
]);
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
)) {
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
;
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
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
;
1127 unsigned short status
= inw(io_addr
);
1129 if (status
& USBPORTSC_CSC
)
1130 uhci_connect_change(uhci
, io_addr
, nr
);
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
);
1143 while (tmp
!= head
) {
1144 struct uhci_td
*td
= list_entry(tmp
, struct uhci_td
, irq_list
);
1145 struct list_head
*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 */
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) */
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
;
1190 if (inw(io_addr
) & USBPORTSC_CSC
) {
1191 wake_up(&uhci_configure
);
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
);
1234 td
->status
= (1 << 24); /* interrupt on completion */
1235 td
->info
= (15 << 21) | 0x7f69; /* (ignored) input packet, 16 bytes, device 127 */
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
);
1250 outw(0, io_addr
+USBCMD
);
1254 static void start_hc(struct uhci
*uhci
)
1256 unsigned int io_addr
= uhci
->io_addr
;
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
1267 outw(USBCMD_HCRESET
, io_addr
+ USBCMD
);
1268 while (inw(io_addr
+ USBCMD
) & USBCMD_HCRESET
) {
1270 printk("USBCMD_HCRESET timed out!\n");
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
)
1306 struct usb_bus
*bus
;
1307 struct uhci_device
*dev
;
1308 struct usb_device
*usb
;
1310 uhci
= kmalloc(sizeof(*uhci
), GFP_KERNEL
);
1314 memset(uhci
, 0, sizeof(*uhci
));
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
);
1327 memset(bus
, 0, sizeof(*bus
));
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
1341 * The second page is used for the frame list.
1343 usb
= uhci_usb_allocate(NULL
);
1347 dev
= uhci
->root_hub
= usb_to_uhci(usb
);
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 */
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
;
1413 uhci
->fl
->frame
[i
] = 2 | virt_to_bus(irq
);
1421 * De-allocate all resources..
1423 static void release_uhci(struct uhci
*uhci
)
1425 if (uhci
->irq
>= 0) {
1426 free_irq(uhci
->irq
, uhci
);
1431 if (uhci
->root_hub
) {
1432 uhci_usb_deallocate(uhci_to_usb(uhci
->root_hub
));
1433 uhci
->root_hub
= NULL
;
1438 free_page((unsigned long)uhci
->fl
);
1446 void cleanup_drivers(void);
1448 static int uhci_control_thread(void * __uhci
)
1450 struct uhci
*uhci
= (struct uhci
*)__uhci
;
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
);
1461 exit_files(current
);
1464 strcpy(current
->comm
, "uhci-control");
1467 * Ok, all systems are go..
1472 int unsigned long signr
;
1474 interruptible_sleep_on(&uhci_configure
);
1482 uhci_check_configuration(uhci
);
1484 if(signal_pending(current
)) {
1485 /* sending SIGUSR1 makes us print out some info */
1486 spin_lock_irq(¤t
->sigmask_lock
);
1487 signr
= dequeue_signal(¤t
->blocked
, &info
);
1488 spin_unlock_irq(¤t
->sigmask_lock
);
1490 if(signr
== SIGUSR1
) {
1491 printk("UHCI queue dump:\n");
1502 for(i
= 0; i
< uhci
->root_hub
->usb
->maxchild
; i
++)
1503 usb_disconnect(uhci
->root_hub
->usb
->children
+ i
);
1509 release_region(uhci
->io_addr
, 32);
1514 printk("uhci_control_thread exiting\n");
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
)
1528 uhci
= alloc_uhci(io_addr
);
1535 if (request_irq(irq
, uhci_interrupt
, SA_SHIRQ
, "usb", uhci
) == 0) {
1540 pid
= kernel_thread(uhci_control_thread
, uhci
, CLONE_FS
| CLONE_FILES
| CLONE_SIGHAND
);
1551 static int start_uhci(struct pci_dev
*dev
)
1555 /* Search for the IO base address.. */
1556 for (i
= 0; i
< 6; i
++) {
1557 unsigned int io_addr
= dev
->base_address
[i
];
1563 io_addr
&= PCI_BASE_ADDRESS_IO_MASK
;
1565 /* Is it already in use? */
1566 if (check_region(io_addr
, 32))
1569 return found_uhci(dev
->irq
, io_addr
);
1575 static int handle_apm_event(apm_event_t event
)
1577 static int down
= 0;
1580 case APM_SYS_SUSPEND
:
1581 case APM_USER_SUSPEND
:
1583 printk(KERN_DEBUG
"uhci: received extra suspend event\n");
1588 case APM_NORMAL_RESUME
:
1589 case APM_CRITICAL_RESUME
:
1591 printk(KERN_DEBUG
"uhci: received bogus resume event\n");
1595 if (waitqueue_active(&uhci_configure
)) {
1597 wake_up(&uhci_configure
);
1607 void cleanup_module(void)
1610 apm_unregister_callback(&handle_apm_event
);
1614 int init_modules(void)
1624 struct pci_dev
*dev
= NULL
;
1629 dev
= pci_find_class(PCI_CLASS_SERIAL_USB
<<8, dev
);
1633 pci_read_config_byte(dev
, PCI_CLASS_PROG
, &type
);
1637 retval
= start_uhci(dev
);
1642 apm_register_callback(&handle_apm_event
);