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>
42 #include <asm/system.h>
47 #include <linux/apm_bios.h>
48 static int handle_apm_event(apm_event_t event
);
49 static int apm_resume
= 0;
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
);
66 * Map status to standard result codes
68 static int uhci_map_status(int status
, int dir_out
)
71 return USB_ST_NOERROR
;
72 if (status
& 0x02) /* Bitstuff error*/
73 return USB_ST_BITSTUFF
;
74 if (status
& 0x04) { /* CRC/Timeout */
76 return USB_ST_NORESPONSE
;
80 if (status
& 0x08) /* NAK */
81 return USB_ST_TIMEOUT
;
82 if (status
& 0x10) /* Babble */
84 if (status
& 0x20) /* Buffer error */
85 return USB_ST_BUFFERUNDERRUN
;
86 if (status
& 0x40) /* Stalled */
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
)
104 tmp
= uhci_ptr_to_virt(td
->qh
->element
);
109 /* locate the first failing td, if any */
112 status
= (tmp
->status
>> 16) & 0xff;
114 /* must reset the toggle on first error */
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);
124 *rval
+= (tmp
->status
& 0x3ff) + 1;
126 if ((tmp
->link
& UHCI_PTR_TERM
) ||
127 (tmp
->link
& UHCI_PTR_QH
))
129 tmp
= uhci_ptr_to_virt(tmp
->link
);
133 return USB_ST_NOERROR
;
135 /* Some debugging code */
142 tmp
= uhci_ptr_to_virt(td
->qh
->element
);
143 printk(KERN_DEBUG
"uhci_td_result() failed with status %x\n",
147 if ((tmp
->link
& UHCI_PTR_TERM
) ||
148 (tmp
->link
& UHCI_PTR_QH
))
150 tmp
= uhci_ptr_to_virt(tmp
->link
);
155 /* endpoint has stalled - mark it halted */
156 usb_endpoint_halt(dev
->usb
, usb_pipeendpoint(tmp
->info
));
160 if (status
== 0x80) {
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
175 * NOTE! This assumes that first->last is a valid
176 * list of TD's with the proper backpointers set
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
;
185 unsigned char success
;
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)
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
;
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
)
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");
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
;
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
)
280 static struct uhci_td
*uhci_td_alloc(struct uhci_device
*dev
)
284 td
= kmem_cache_alloc(uhci_td_cachep
, SLAB_KERNEL
);
289 if ((__u32
)td
& UHCI_PTR_BITS
)
290 printk("qh not 16 byte aligned!\n");
293 td
->link
= UHCI_PTR_TERM
;
301 INIT_LIST_HEAD(&td
->irq_list
);
302 atomic_set(&td
->refcnt
, 1);
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
)
317 qh
= kmem_cache_alloc(uhci_qh_cachep
, SLAB_KERNEL
);
322 if ((__u32
)qh
& UHCI_PTR_BITS
)
323 printk("qh not 16 byte aligned!\n");
326 qh
->element
= UHCI_PTR_TERM
;
327 qh
->link
= UHCI_PTR_TERM
;
331 init_waitqueue_head(&qh
->wakeup
);
332 atomic_set(&qh
->refcnt
, 1);
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
)
352 td
->completed
= completed
;
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
)
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
)
378 struct uhci_td
*curtd
;
379 unsigned int nextlink
;
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
);
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
);
398 if (nextlink
& UHCI_PTR_TERM
) /* Tail? */
401 curtd
= bus_to_virt(nextlink
& ~UHCI_PTR_BITS
);
403 printk(KERN_ERR
"runaway td's!?\n");
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
;
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
|
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
);
441 /* if period 0, insert into fast q */
443 td
->flags
|= UHCI_TD_REMOVE
;
444 qh
->skel
= &dev
->uhci
->skel_int2_qh
;
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
);
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
)
472 printk(KERN_DEBUG
"usb-uhci: releasing irq handle %p\n", handle
);
475 td
= (struct uhci_td
*)handle
;
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 */
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 */
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
;
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
);
516 if ((td
->status
>> 16) & 0xFF)
517 printk(KERN_DEBUG
"error: %d %X\n", i
,
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
;
535 if ((isodesc
->frame
< 0) || (isodesc
->frame
> 1023)) {
536 printk(KERN_ERR
"illegal frame number %d\n", isodesc
->frame
);
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
;
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
;
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 */
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;
580 frame
= (pisodesc
->endframe
+ 1) % 1024;
582 for (i
= 0; i
< isodesc
->num
; i
++) {
583 struct uhci_td
*td
= &isodesc
->td
[i
];
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;
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
;
609 struct uhci_iso_td
*isodesc
;
612 isodesc
= kmalloc(sizeof(*isodesc
), GFP_KERNEL
);
614 printk(KERN_ERR
"Couldn't allocate isodesc!\n");
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
;
628 printk(KERN_ERR
"couldn't allocate td's\n");
633 isodesc
->frame
= isodesc
->endframe
= -1;
636 * Build the DATA TD's
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
653 td
->info
= destination
| ((maxsze
- 1) << 21);
654 td
->buffer
= virt_to_bus(data
);
661 } while (i
< isodesc
->num
);
663 uhci_add_irq_list(dev
->uhci
, td
, completed
, dev_id
);
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 */
674 uhci_unschedule_isochronous(usb_dev
, isodesc
);
676 /* Remove it from the IRQ list */
677 uhci_remove_irq_list(&isodesc
->td
[isodesc
->num
- 1]);
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
))
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
);
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
);
719 /* FIXME: This is kinda kludged */
720 /* Walk the TD list and update the QH pointer */
722 struct uhci_td
*curtd
;
728 if (curtd
->link
& TD_CTRL_TERM
)
731 curtd
= uhci_ptr_to_virt(curtd
->link
);
734 printk(KERN_DEBUG
"runaway tds!\n");
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
);
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
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
;
785 int maxsze
= usb_maxpacket(usb_dev
, pipe
);
788 first
= td
= uhci_td_alloc(dev
);
792 /* The "pipe" thing contains the destination in bits 8--18, 0x2D is SETUP */
793 destination
= (pipe
& PIPE_DEVEP_MASK
) | 0x2D;
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 */
814 td
= uhci_td_alloc(dev
);
818 prevtd
->link
= virt_to_bus(td
) | UHCI_PTR_DEPTH
;
821 * Build the DATA TD's
824 /* Build the TD for control status */
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
;
842 td
= uhci_td_alloc(dev
);
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 */
857 td
->backptr
= &prevtd
->link
;
858 td
->link
= UHCI_PTR_TERM
; /* Terminate */
861 ret
= uhci_run_control(dev
, first
, td
);
870 if (nextlink
& UHCI_PTR_TERM
) /* Tail? */
873 td
= uhci_ptr_to_virt(nextlink
);
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]);
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
);
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
);
912 /* FIXME: This is kinda kludged */
913 /* Walk the TD list and update the QH pointer */
915 struct uhci_td
*curtd
;
921 if (curtd
->link
& UHCI_PTR_TERM
)
924 curtd
= uhci_ptr_to_virt(curtd
->link
);
927 printk(KERN_ERR
"runaway tds!\n");
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
);
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
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
;
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)))
971 /* The "pipe" thing contains the destination in bits 8--18 */
972 destination
= (pipe
& PIPE_DEVEP_MASK
) | usb_packetid (pipe
);
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
);
984 prevtd
= first
; //This is fake, but at least it's not NULL
986 /* Build the TD for control status */
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
;
1003 td
= uhci_td_alloc(dev
);
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!!! */
1020 ret
= uhci_run_bulk(dev
, first
, td
, rval
);
1024 struct uhci_td
*curtd
= first
;
1025 unsigned int nextlink
;
1028 nextlink
= curtd
->link
;
1029 uhci_remove_td(curtd
);
1030 uhci_td_free(curtd
);
1032 if (nextlink
& UHCI_PTR_TERM
) /* Tail? */
1035 curtd
= uhci_ptr_to_virt(nextlink
);
1039 printk(KERN_DEBUG
"runaway td's!?\n");
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
);
1064 /* Build the TD for control status */
1067 if (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
;
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 */
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! */
1117 uhci_remove_transfer(first
,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
);
1132 memset(usb_dev
, 0, sizeof(*usb_dev
));
1134 /* Allocate the UHCI device private data */
1135 dev
= kmalloc(sizeof(*dev
), GFP_KERNEL
);
1141 /* Initialize "dev" */
1142 memset(dev
, 0, sizeof(*dev
));
1144 usb_dev
->hcpriv
= dev
;
1147 usb_dev
->parent
= parent
;
1150 usb_dev
->bus
= parent
->bus
;
1151 dev
->uhci
= usb_to_uhci(parent
)->uhci
;
1157 static int uhci_usb_free(struct usb_device
*usb_dev
)
1159 struct uhci_device
*dev
= usb_to_uhci(usb_dev
);
1162 usb_destroy_configuration(usb_dev
);
1168 struct usb_operations uhci_device_operations
= {
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
1191 static void uhci_reset_port(unsigned int port
)
1193 unsigned short status
;
1196 outw(status
| USBPORTSC_PR
, port
); /* reset port */
1198 outw(status
& ~USBPORTSC_PR
, port
);
1202 outw(status
| USBPORTSC_PE
, port
); /* enable port */
1206 if(!(status
& USBPORTSC_PE
)) {
1207 outw(status
| USBPORTSC_PE
, port
); /* one more try at enabling port */
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
);
1225 printk(KERN_INFO
"uhci_connect_change: called for %d\n", nr
);
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
]);
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
)) {
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
);
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
1271 if (usb_new_device(usb_dev
)) {
1272 unsigned short status
= inw(port
);
1274 printk(KERN_INFO
"uhci: disabling malfunctioning port %d\n",
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
;
1293 unsigned short status
= inw(io_addr
);
1295 if (status
& USBPORTSC_CSC
)
1296 uhci_connect_change(uhci
, io_addr
, nr
);
1299 } while (nr
< maxchild
);
1302 static void uhci_interrupt_notify(struct uhci
*uhci
)
1304 struct list_head
*tmp
, *head
= &uhci
->interrupt_list
;
1307 spin_lock(&irqlist_lock
);
1309 while (tmp
!= head
) {
1310 struct uhci_td
*first
, *td
= list_entry(tmp
,
1311 struct uhci_td
, irq_list
);
1315 /* We check the TD which had the IOC bit as well as the */
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
);
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
))
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
);
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
;
1388 if (inw(io_addr
) & USBPORTSC_CSC
) {
1389 wake_up(&uhci_configure
);
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
);
1429 printk(KERN_ERR
"unable to allocate ticktd\n");
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 */
1440 uhci
->fl
->frame
[0] = virt_to_bus(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
);
1452 outw(0, io_addr
+ USBCMD
);
1456 static void start_hc(struct uhci
*uhci
)
1458 unsigned int io_addr
= uhci
->io_addr
;
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
1469 outw(USBCMD_HCRESET
, io_addr
+ USBCMD
);
1470 while (inw(io_addr
+ USBCMD
) & USBCMD_HCRESET
) {
1472 printk(KERN_ERR
"USBCMD_HCRESET timed out!\n");
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
)
1513 struct usb_bus
*bus
;
1514 struct uhci_device
*dev
;
1515 struct usb_device
*usb
;
1517 uhci
= kmalloc(sizeof(*uhci
), GFP_KERNEL
);
1521 memset(uhci
, 0, sizeof(*uhci
));
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
);
1532 bus
= usb_alloc_bus(&uhci_device_operations
);
1540 * Allocate the root_hub
1542 usb
= uhci_usb_alloc(NULL
);
1548 dev
= usb_to_uhci(usb
);
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 */
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
) |
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
;
1606 uhci
->fl
->frame
[i
] = virt_to_bus(irq
) | UHCI_PTR_QH
;
1618 free_page((unsigned long)uhci
->fl
);
1625 * De-allocate all resources..
1627 static void release_uhci(struct uhci
*uhci
)
1629 if (uhci
->irq
>= 0) {
1630 free_irq(uhci
->irq
, uhci
);
1635 uhci_td_free(uhci
->ticktd
);
1636 uhci
->ticktd
= NULL
;
1640 free_page((unsigned long)uhci
->fl
);
1644 usb_free_bus(uhci
->bus
);
1648 static int uhci_control_thread(void * __uhci
)
1650 struct uhci
*uhci
= (struct uhci
*)__uhci
;
1652 uhci
->control_running
= 1;
1657 * This thread doesn't need any user-level access,
1658 * so get rid of all our resources..
1661 exit_files(current
);
1663 strcpy(current
->comm
, "uhci-control");
1666 * Ok, all systems are go..
1670 int unsigned long signr
;
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(¤t
->sigmask_lock
);
1686 signr
= dequeue_signal(¤t
->blocked
, &info
);
1687 spin_unlock_irq(¤t
->sigmask_lock
);
1689 if (signr
== SIGUSR1
) {
1690 printk(KERN_DEBUG
"UHCI queue dump:\n");
1692 } else if (signr
== SIGUSR2
) {
1693 uhci_debug
= !uhci_debug
;
1694 printk(KERN_DEBUG
"UHCI debug toggle = %x\n",
1699 } while (uhci
->control_continue
);
1705 uhci
->control_running
= 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
)
1719 uhci
= alloc_uhci(io_addr
);
1723 INIT_LIST_HEAD(&uhci
->uhci_list
);
1724 list_add(&uhci
->uhci_list
, &uhci_list
);
1726 request_region(uhci
->io_addr
, 32, "usb-uhci");
1730 usb_register_bus(uhci
->bus
);
1733 uhci
->control_continue
= 1;
1736 if (request_irq(irq
, uhci_interrupt
, SA_SHIRQ
, "uhci", uhci
) == 0) {
1740 pid
= kernel_thread(uhci_control_thread
, uhci
,
1741 CLONE_FS
| CLONE_FILES
| CLONE_SIGHAND
);
1743 uhci
->control_pid
= pid
;
1752 release_region(uhci
->io_addr
, 32);
1758 static int start_uhci(struct pci_dev
*dev
)
1762 /* Search for the IO base address.. */
1763 for (i
= 0; i
< 6; i
++) {
1764 unsigned int io_addr
= dev
->resource
[i
].start
;
1767 if (!(dev
->resource
[i
].flags
& 1))
1771 /* Is it already in use? */
1772 if (check_region(io_addr
, 32))
1776 return found_uhci(dev
->irq
, io_addr
);
1782 static int handle_apm_event(apm_event_t event
)
1784 static int down
= 0;
1787 case APM_SYS_SUSPEND
:
1788 case APM_USER_SUSPEND
:
1790 printk(KERN_DEBUG
"uhci: received extra suspend event\n");
1795 case APM_NORMAL_RESUME
:
1796 case APM_CRITICAL_RESUME
:
1798 printk(KERN_DEBUG
"uhci: received bogus resume event\n");
1802 if (waitqueue_active(&uhci_configure
)) {
1804 wake_up(&uhci_configure
);
1815 struct pci_dev
*dev
= NULL
;
1819 /* FIXME: This is lame, but I guess it's better to leak memory than */
1821 name
= kmalloc(10, GFP_KERNEL
);
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
)
1834 name
= kmalloc(10, GFP_KERNEL
);
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
)
1849 dev
= pci_find_class(PCI_CLASS_SERIAL_USB
<< 8, dev
);
1853 pci_read_config_byte(dev
, PCI_CLASS_PROG
, &type
);
1857 retval
= start_uhci(dev
);
1862 apm_register_callback(&handle_apm_event
);
1869 void uhci_cleanup(void)
1871 struct list_head
*next
, *tmp
, *head
= &uhci_list
;
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
);
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);
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);
1898 printk(KERN_ERR
"uhci: giving up on killing uhci-control\n");
1902 for (i
= 0; i
< root_hub
->usb
->maxchild
; i
++)
1903 usb_disconnect(root_hub
->usb
->children
+ i
);
1905 usb_deregister_bus(uhci
->bus
);
1908 release_region(uhci
->io_addr
, 32);
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");
1923 int init_module(void)
1928 void cleanup_module(void)
1931 apm_unregister_callback(&handle_apm_event
);