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