2 * Universal Host Controller Interface driver for USB.
4 * (C) Copyright 1999 Linus Torvalds
5 * (C) Copyright 1999 Johannes Erdfelt
6 * (C) Copyright 1999 Randy Dunlap
8 * Intel documents this fairly well, and as far as I know there
9 * are no royalties or anything like that, but even so there are
10 * people who decided that they want to do the same thing in a
11 * completely different way.
13 * Oh, well. The intel version is the more common by far. As such,
14 * that's the one I care about right now.
16 * WARNING! The USB documentation is downright evil. Most of it
17 * is just crap, written by a committee. You're better off ignoring
18 * most of it, the important stuff is:
19 * - the low-level protocol (fairly simple but lots of small details)
20 * - working around the horridness of the rest
23 /* 4/4/1999 added data toggle for interrupt pipes -keryan */
24 /* 5/16/1999 added global toggles for bulk and control */
25 /* 6/25/1999 added fix for data toggles on bidirectional bulk endpoints */
27 * 1999-09-02: Thomas Sailer <sailer@ife.ee.ethz.ch>
28 * Added explicit frame list manipulation routines
29 * for inserting/removing iso td's to/from the frame list.
30 * START_ABSOLUTE fixes
33 #include <linux/config.h>
34 #include <linux/module.h>
35 #include <linux/pci.h>
36 #include <linux/kernel.h>
37 #include <linux/delay.h>
38 #include <linux/ioport.h>
39 #include <linux/sched.h>
40 #include <linux/malloc.h>
41 #include <linux/smp_lock.h>
42 #include <linux/errno.h>
43 #include <linux/unistd.h>
44 #include <linux/spinlock.h>
46 #include <asm/uaccess.h>
49 #include <asm/system.h>
54 #include <linux/apm_bios.h>
55 static int handle_apm_event(apm_event_t event
);
56 static int apm_resume
= 0;
59 static int uhci_debug
= 1;
61 #define compile_assert(x) do { switch (0) { case 1: case !(x): } } while (0)
63 static DECLARE_WAIT_QUEUE_HEAD(uhci_configure
);
65 static kmem_cache_t
*uhci_td_cachep
;
66 static kmem_cache_t
*uhci_qh_cachep
;
68 static LIST_HEAD(uhci_list
);
76 static int uhci_get_current_frame_number (struct usb_device
*usb_dev
);
78 static int uhci_init_isoc (struct usb_device
*usb_dev
,
82 struct usb_isoc_desc
**isocdesc
);
84 static void uhci_free_isoc (struct usb_isoc_desc
*isocdesc
);
86 static int uhci_run_isoc (struct usb_isoc_desc
*isocdesc
,
87 struct usb_isoc_desc
*pr_isocdesc
);
89 static int uhci_kill_isoc (struct usb_isoc_desc
*isocdesc
);
92 * Map status to standard result codes
94 * <status> is (td->status & 0xFE0000) [a.k.a. uhci_status_bits(td->status)
95 * <dir_out> is True for output TDs and False for input TDs.
97 static int uhci_map_status(int status
, int dir_out
)
100 return USB_ST_NOERROR
;
101 if (status
& TD_CTRL_BITSTUFF
) /* Bitstuff error */
102 return USB_ST_BITSTUFF
;
103 if (status
& TD_CTRL_CRCTIMEO
) { /* CRC/Timeout */
105 return USB_ST_NORESPONSE
;
109 if (status
& TD_CTRL_NAK
) /* NAK */
110 return USB_ST_TIMEOUT
;
111 if (status
& TD_CTRL_BABBLE
) /* Babble */
113 if (status
& TD_CTRL_DBUFERR
) /* Buffer error */
114 return USB_ST_BUFFERUNDERRUN
;
115 if (status
& TD_CTRL_STALLED
) /* Stalled */
117 if (status
& TD_CTRL_ACTIVE
) /* Active */
118 return USB_ST_NOERROR
;
120 return USB_ST_INTERNALERROR
;
123 * Return the result of a TD..
125 static int uhci_td_result(struct uhci_device
*dev
, struct uhci_td
*td
, unsigned long *rval
, int debug
)
129 int count
= 1000, bytesreceived
= 0;
134 /* Start at the TD first in the chain, if possible */
135 if (td
->qh
&& td
->qh
->first
)
141 return USB_ST_INTERNALERROR
;
143 /* Locate the first failing td, if any */
145 status
= uhci_status_bits(tmp
->status
);
151 /* Must reset the toggle on first error */
153 printk(KERN_DEBUG
"Set toggle from %x rval %ld\n",
154 (unsigned int)tmp
, rval
? *rval
: 0);
157 usb_settoggle(dev
->usb
, uhci_endpoint(tmp
->info
),
158 uhci_packetout(tmp
->info
) ^ 1,
159 uhci_toggle(tmp
->info
));
163 if (rval
&& ((tmp
->info
& 0xFF) == USB_PID_IN
))
164 *rval
+= uhci_actual_length(tmp
->status
);
167 /* The length field is only valid if the TD was completed */
168 if (!(tmp
->status
& TD_CTRL_ACTIVE
) && uhci_packetin(tmp
->info
)) {
169 bytesreceived
+= uhci_actual_length(tmp
->status
);
171 *rval
+= uhci_actual_length(tmp
->status
);
174 if ((tmp
->link
& UHCI_PTR_TERM
) ||
175 (tmp
->link
& UHCI_PTR_QH
))
178 tmp
= uhci_ptr_to_virt(tmp
->link
);
182 printk(KERN_ERR
"runaway td's in uhci_td_result!\n");
183 /* Force debugging on */
186 /* If we got to the last TD */
190 return USB_ST_NOERROR
;
192 /* APC BackUPS Pro kludge */
193 /* It tries to send all of the descriptor instead of */
194 /* the amount we requested */
195 if (tmp
->status
& TD_CTRL_IOC
&&
196 tmp
->status
& TD_CTRL_ACTIVE
&&
197 tmp
->status
& TD_CTRL_NAK
)
198 return USB_ST_NOERROR
;
201 /* We got to an error, but the controller hasn't finished */
203 if (tmp
->status
& TD_CTRL_ACTIVE
)
204 return USB_ST_NOCHANGE
;
207 /* If this wasn't the last TD and SPD is set, ACTIVE */
208 /* is not and NAK isn't then we received a short */
210 if (tmp
->status
& TD_CTRL_SPD
&&
211 !(tmp
->status
& TD_CTRL_NAK
))
212 return USB_ST_NOERROR
;
215 /* Some debugging code */
216 if (debug
&& uhci_debug
) {
217 printk(KERN_DEBUG
"uhci_td_result() failed with status %x\n",
220 /* Print the chain for debugging purposes */
222 uhci_show_queue(td
->qh
);
227 if (status
& TD_CTRL_STALLED
) {
228 /* endpoint has stalled - mark it halted */
229 usb_endpoint_halt(dev
->usb
, uhci_endpoint(tmp
->info
),
230 uhci_packetout(tmp
->info
));
234 if ((status
== TD_CTRL_ACTIVE
) && (!rval
))
235 return USB_ST_DATAUNDERRUN
;
237 return uhci_map_status(status
, uhci_packetout(tmp
->info
));
241 * Inserts a td into qh list at the top.
243 * Careful about atomicity: even on UP this
244 * requires a locked access due to the concurrent
247 * NOTE! This assumes that first->last is a valid
248 * list of TD's with the proper backpointers set
251 static void uhci_insert_tds_in_qh(struct uhci_qh
*qh
, struct uhci_td
*first
, struct uhci_td
*last
)
253 unsigned int link
= qh
->element
;
254 unsigned int new = virt_to_bus(first
) | UHCI_PTR_DEPTH
;
257 unsigned char success
;
260 first
->backptr
= &qh
->element
;
261 asm volatile("lock ; cmpxchg %4,%2 ; sete %0"
262 :"=q" (success
), "=a" (link
)
263 :"m" (qh
->element
), "1" (link
), "r" (new)
267 /* Was there a successor entry? Fix it's backpointer */
268 if ((link
& UHCI_PTR_TERM
) == 0) {
269 struct uhci_td
*next
= uhci_ptr_to_virt(link
);
270 next
->backptr
= &last
->link
;
281 static inline void uhci_insert_td_in_qh(struct uhci_qh
*qh
, struct uhci_td
*td
)
283 uhci_insert_tds_in_qh(qh
, td
, td
);
286 static void uhci_insert_qh(struct uhci_qh
*qh
, struct uhci_qh
*newqh
)
288 newqh
->link
= qh
->link
;
289 qh
->link
= virt_to_bus(newqh
) | UHCI_PTR_QH
;
292 static void uhci_remove_qh(struct uhci_qh
*qh
, struct uhci_qh
*remqh
)
294 struct uhci_qh
*lqh
= qh
;
296 while (uhci_ptr_to_virt(lqh
->link
) != remqh
) {
297 if (lqh
->link
& UHCI_PTR_TERM
)
300 lqh
= uhci_ptr_to_virt(lqh
->link
);
303 if (lqh
->link
& UHCI_PTR_TERM
) {
304 printk(KERN_DEBUG
"couldn't find qh in chain!\n");
308 lqh
->link
= remqh
->link
;
312 * Removes td from qh if present.
314 * NOTE! We keep track of both forward and back-pointers,
315 * so this should be trivial, right?
317 * Wrong. While all TD insert/remove operations are synchronous
318 * on the CPU, the UHCI controller can (and does) play with the
319 * very first forward pointer. So we need to validate the backptr
320 * before we change it, so that we don't by mistake reset the QH
321 * head to something old.
323 static void uhci_remove_td(struct uhci_td
*td
)
325 unsigned int *backptr
= td
->backptr
;
326 unsigned int link
= td
->link
;
335 * This is the easy case: the UHCI will never change "td->link",
336 * so we can always just look at that and fix up the backpointer
337 * of any next element..
339 if (!(link
& UHCI_PTR_TERM
)) {
340 struct uhci_td
*next
= uhci_ptr_to_virt(link
);
341 next
->backptr
= backptr
;
345 * The nasty case is "backptr->next", which we need to
346 * update to "link" _only_ if "backptr" still points
347 * to us (it may not: maybe backptr is a QH->element
348 * pointer and the UHCI has changed the value).
350 me
= virt_to_bus(td
) | (0xe & *backptr
);
351 asm volatile("lock ; cmpxchg %0,%1"
353 :"r" (link
), "m" (*backptr
), "a" (me
)
356 /* Reset it just in case */
357 td
->link
= UHCI_PTR_TERM
;
361 * Only the USB core should call uhci_alloc_dev and uhci_free_dev
363 static int uhci_alloc_dev(struct usb_device
*usb_dev
)
365 struct uhci_device
*dev
;
367 /* Allocate the UHCI device private data */
368 dev
= kmalloc(sizeof(*dev
), GFP_KERNEL
);
372 /* Initialize "dev" */
373 memset(dev
, 0, sizeof(*dev
));
375 usb_dev
->hcpriv
= dev
;
377 atomic_set(&dev
->refcnt
, 1);
380 dev
->uhci
= usb_to_uhci(usb_dev
->parent
)->uhci
;
385 static int uhci_free_dev(struct usb_device
*usb_dev
)
387 struct uhci_device
*dev
= usb_to_uhci(usb_dev
);
389 if (atomic_dec_and_test(&dev
->refcnt
))
395 static void uhci_inc_dev_use(struct uhci_device
*dev
)
397 atomic_inc(&dev
->refcnt
);
400 static void uhci_dec_dev_use(struct uhci_device
*dev
)
402 uhci_free_dev(dev
->usb
);
405 static struct uhci_td
*uhci_td_alloc(struct uhci_device
*dev
)
409 td
= kmem_cache_alloc(uhci_td_cachep
, SLAB_KERNEL
);
414 if ((__u32
)td
& UHCI_PTR_BITS
)
415 printk("qh not 16 byte aligned!\n");
418 td
->link
= UHCI_PTR_TERM
;
426 INIT_LIST_HEAD(&td
->irq_list
);
427 atomic_set(&td
->refcnt
, 1);
429 uhci_inc_dev_use(dev
);
434 static void uhci_td_free(struct uhci_td
*td
)
436 if (atomic_dec_and_test(&td
->refcnt
)) {
437 kmem_cache_free(uhci_td_cachep
, td
);
440 uhci_dec_dev_use(td
->dev
);
444 static struct uhci_qh
*uhci_qh_alloc(struct uhci_device
*dev
)
448 qh
= kmem_cache_alloc(uhci_qh_cachep
, SLAB_KERNEL
);
453 if ((__u32
)qh
& UHCI_PTR_BITS
)
454 printk("qh not 16 byte aligned!\n");
457 qh
->element
= UHCI_PTR_TERM
;
458 qh
->link
= UHCI_PTR_TERM
;
463 init_waitqueue_head(&qh
->wakeup
);
464 atomic_set(&qh
->refcnt
, 1);
466 uhci_inc_dev_use(dev
);
471 static void uhci_qh_free(struct uhci_qh
*qh
)
473 if (atomic_dec_and_test(&qh
->refcnt
)) {
474 kmem_cache_free(uhci_qh_cachep
, qh
);
477 uhci_dec_dev_use(qh
->dev
);
482 * UHCI interrupt list operations..
484 static spinlock_t irqlist_lock
= SPIN_LOCK_UNLOCKED
;
486 static void uhci_add_irq_list(struct uhci
*uhci
, struct uhci_td
*td
, usb_device_irq completed
, void *dev_id
)
490 td
->completed
= completed
;
493 spin_lock_irqsave(&irqlist_lock
, flags
);
494 list_add(&td
->irq_list
, &uhci
->interrupt_list
);
495 spin_unlock_irqrestore(&irqlist_lock
, flags
);
498 static void uhci_remove_irq_list(struct uhci_td
*td
)
502 spin_lock_irqsave(&irqlist_lock
, flags
);
503 list_del(&td
->irq_list
);
504 spin_unlock_irqrestore(&irqlist_lock
, flags
);
508 * frame list manipulation. Used for Isochronous transfers.
509 * the list of (iso) TD's enqueued in a frame list entry
510 * is basically a doubly linked list with link being
511 * the forward pointer and backptr the backward ptr.
512 * the frame list entry itself doesn't have a back ptr
513 * (therefore the list is not circular), and the forward pointer
514 * stops at link entries having the UHCI_PTR_TERM or the UHCI_PTR_QH
515 * bit set. Maybe it could be extended to handle the QH's also,
516 * but it doesn't seem necessary right now.
517 * The layout looks as follows:
518 * frame list pointer -> iso td's (if any) ->
519 * periodic interrupt td (if framelist 0) -> irq qh -> control qh -> bulk qh
522 static spinlock_t framelist_lock
= SPIN_LOCK_UNLOCKED
;
524 static void uhci_add_frame_list(struct uhci
*uhci
, struct uhci_td
*td
, unsigned framenum
)
527 struct uhci_td
*nexttd
;
529 framenum
%= UHCI_NUMFRAMES
;
530 spin_lock_irqsave(&framelist_lock
, flags
);
531 td
->backptr
= &uhci
->fl
->frame
[framenum
];
532 td
->link
= uhci
->fl
->frame
[framenum
];
533 if (!(td
->link
& (UHCI_PTR_TERM
| UHCI_PTR_QH
))) {
534 nexttd
= (struct uhci_td
*)bus_to_virt(td
->link
& ~15);
535 nexttd
->backptr
= &td
->link
;
538 uhci
->fl
->frame
[framenum
] = virt_to_bus(td
);
539 spin_unlock_irqrestore(&framelist_lock
, flags
);
542 static void uhci_remove_frame_list(struct uhci
*uhci
, struct uhci_td
*td
)
545 struct uhci_td
*nexttd
;
549 spin_lock_irqsave(&framelist_lock
, flags
);
550 *(td
->backptr
) = td
->link
;
551 if (!(td
->link
& (UHCI_PTR_TERM
| UHCI_PTR_QH
))) {
552 nexttd
= (struct uhci_td
*)bus_to_virt(td
->link
& ~15);
553 nexttd
->backptr
= td
->backptr
;
555 spin_unlock_irqrestore(&framelist_lock
, flags
);
558 * attention: td->link might still be in use by the
559 * hardware if the td is still active and the hardware
560 * was processing it. So td->link should be preserved
561 * until the frame number changes. Don't know what to do...
562 * udelay(1000) doesn't sound nice, and schedule()
563 * can't be used as this is called from within interrupt context.
565 /* for now warn if there's a possible problem */
566 if (td
->status
& TD_CTRL_ACTIVE
) {
567 unsigned frn
= inw(uhci
->io_addr
+ USBFRNUM
);
568 __u32 link
= uhci
->fl
->frame
[frn
% UHCI_NUMFRAMES
];
569 if (!(link
& (UHCI_PTR_TERM
| UHCI_PTR_QH
))) {
570 struct uhci_td
*tdl
= (struct uhci_td
*)bus_to_virt(link
& ~15);
573 printk(KERN_WARNING
"uhci_remove_frame_list: td possibly still in use!!\n");
576 if (tdl
->link
& (UHCI_PTR_TERM
| UHCI_PTR_QH
))
578 tdl
= (struct uhci_td
*)bus_to_virt(tdl
->link
& ~15);
586 * This function removes and disallocates all structures set up for a transfer.
587 * It takes the qh out of the skeleton, removes the tq and the td's.
588 * It only removes the associated interrupt handler if removeirq is set.
589 * The *td argument is any td in the list of td's.
591 static void uhci_remove_transfer(struct uhci_td
*td
, char removeirq
)
594 struct uhci_td
*curtd
;
595 unsigned int nextlink
;
597 if (td
->qh
&& td
->qh
->first
)
598 curtd
= td
->qh
->first
;
602 /* Remove it from the skeleton */
603 uhci_remove_qh(td
->qh
->skel
, td
->qh
);
604 uhci_qh_free(td
->qh
);
606 nextlink
= curtd
->link
;
608 /* IOC? => remove handler */
609 if (removeirq
&& (td
->status
& TD_CTRL_IOC
))
610 uhci_remove_irq_list(td
);
612 uhci_remove_td(curtd
);
614 if (nextlink
& UHCI_PTR_TERM
) /* Tail? */
617 curtd
= bus_to_virt(nextlink
& ~UHCI_PTR_BITS
);
619 printk(KERN_ERR
"runaway td's!?\n");
626 * Request a interrupt handler..
628 * Returns 0 (success) or negative (failure).
629 * Also returns/sets a "handle pointer" that release_irq can use to stop this
630 * interrupt. (It's really a pointer to the TD).
632 static int uhci_request_irq(struct usb_device
*usb_dev
, unsigned int pipe
, usb_device_irq handler
, int period
, void *dev_id
, void **handle
)
634 struct uhci_device
*dev
= usb_to_uhci(usb_dev
);
635 struct uhci_td
*td
= uhci_td_alloc(dev
);
636 struct uhci_qh
*qh
= uhci_qh_alloc(dev
);
637 unsigned int destination
, status
;
647 /* Destination: pipe destination with INPUT */
648 destination
= (pipe
& PIPE_DEVEP_MASK
) | usb_packetid(pipe
);
650 /* Infinite errors is 0, so no bits */
651 status
= (pipe
& TD_CTRL_LS
) | TD_CTRL_IOC
| TD_CTRL_ACTIVE
| TD_CTRL_SPD
;
653 td
->link
= UHCI_PTR_TERM
; /* Terminate */
655 td
->info
= destination
| ((usb_maxpacket(usb_dev
, pipe
, usb_pipeout(pipe
)) - 1) << 21) |
656 (usb_gettoggle(usb_dev
, usb_pipeendpoint(pipe
), usb_pipeout(pipe
)) << TD_TOKEN_TOGGLE
);
658 td
->buffer
= virt_to_bus(dev
->data
);
662 /* if period 0, insert into fast q */
664 td
->flags
|= UHCI_TD_REMOVE
;
665 qh
->skel
= &dev
->uhci
->skel_int2_qh
;
667 qh
->skel
= &dev
->uhci
->skel_int8_qh
;
669 uhci_add_irq_list(dev
->uhci
, td
, handler
, dev_id
);
671 uhci_insert_td_in_qh(qh
, td
);
673 /* Add it into the skeleton */
674 uhci_insert_qh(qh
->skel
, qh
);
676 *handle
= (void *)td
;
681 * Release an interrupt handler previously allocated using
682 * uhci_request_irq. This function does no validity checking, so make
683 * sure you're not releasing an already released handle as it may be
684 * in use by something else..
686 * This function can NOT be called from an interrupt.
688 int uhci_release_irq(struct usb_device
*usb
, void *handle
)
693 td
= (struct uhci_td
*)handle
;
695 return USB_ST_INTERNALERROR
;
697 /* Remove it from the internal irq_list */
698 uhci_remove_irq_list(td
);
700 /* Remove the interrupt TD and QH */
703 uhci_remove_qh(qh
->skel
, qh
);
705 if (td
->completed
!= NULL
)
706 td
->completed(USB_ST_REMOVED
, NULL
, 0, td
->dev_id
);
708 /* Free the TD and QH */
712 return USB_ST_NOERROR
;
713 } /* uhci_release_irq() */
716 * uhci_get_current_frame_number()
718 * returns the current frame number for a USB bus/controller.
720 static int uhci_get_current_frame_number(struct usb_device
*usb_dev
)
722 return inw (usb_to_uhci(usb_dev
)->uhci
->io_addr
+ USBFRNUM
);
729 * Allocates some data structures.
730 * Initializes parts of them from the function parameters.
732 * It does not associate any data/buffer pointers or
733 * driver (caller) callback functions with the allocated
734 * data structures. Such associations are left until
737 * Returns 0 for success or negative value for error.
738 * Sets isocdesc before successful return.
740 static int uhci_init_isoc (struct usb_device
*usb_dev
,
742 int frame_count
, /* bandwidth % = 100 * this / 1000 */
744 struct usb_isoc_desc
**isocdesc
)
746 struct usb_isoc_desc
*id
;
751 /* Check some parameters. */
752 if ((frame_count
<= 0) || (frame_count
> UHCI_NUMFRAMES
)) {
753 #ifdef CONFIG_USB_DEBUG_ISOC
754 printk (KERN_DEBUG
"uhci_init_isoc: invalid frame_count (%d)\n",
760 if (!usb_pipeisoc (pipe
)) {
761 #ifdef CONFIG_USB_DEBUG_ISOC
762 printk (KERN_DEBUG
"uhci_init_isoc: NOT an Isoc. pipe\n");
767 id
= kmalloc (sizeof (*id
) +
768 (sizeof (struct isoc_frame_desc
) * frame_count
), GFP_KERNEL
);
772 memset (id
, 0, sizeof (*id
) +
773 (sizeof (struct isoc_frame_desc
) * frame_count
));
775 id
->td
= kmalloc (sizeof (struct uhci_td
) * frame_count
, GFP_KERNEL
);
781 memset (id
->td
, 0, sizeof (struct uhci_td
) * frame_count
);
783 for (i
= 0; i
< frame_count
; i
++)
784 INIT_LIST_HEAD(&((struct uhci_td
*)(id
->td
))[i
].irq_list
);
786 id
->frame_count
= frame_count
;
787 id
->frame_size
= usb_maxpacket (usb_dev
, pipe
, usb_pipeout(pipe
));
788 /* TBD: or make this a parameter to allow for frame_size
789 that is less than maxpacketsize */
790 id
->start_frame
= -1;
792 id
->usb_dev
= usb_dev
;
794 id
->context
= context
;
798 } /* end uhci_init_isoc */
803 * Associates data/buffer pointers/lengths and
804 * driver (caller) callback functions with the
805 * allocated Isoc. data structures and TDs.
807 * Then inserts the TDs into the USB controller frame list
808 * for its processing.
809 * And inserts the callback function into its TD.
811 * pr_isocdesc (previous Isoc. desc.) may be NULL.
812 * It is used only for chaining one list of TDs onto the
813 * end of the previous list of TDs.
815 * Returns 0 (success) or error code (negative value).
817 static int uhci_run_isoc (struct usb_isoc_desc
*isocdesc
,
818 struct usb_isoc_desc
*pr_isocdesc
)
820 struct uhci_device
*dev
= usb_to_uhci (isocdesc
->usb_dev
);
821 struct uhci
*uhci
= dev
->uhci
;
822 unsigned long destination
, status
;
824 int ix
, cur_frame
, pipeinput
, frlen
;
826 struct isoc_frame_desc
*fd
;
827 unsigned char *bufptr
;
829 if (!isocdesc
->callback_fn
) {
830 #ifdef CONFIG_USB_DEBUG_ISOC
831 printk (KERN_DEBUG
"uhci_run_isoc: caller must have a callback function\n");
836 /* Check buffer size large enough for maxpacketsize * frame_count. */
837 if (isocdesc
->buf_size
< (isocdesc
->frame_count
* isocdesc
->frame_size
)) {
838 #ifdef CONFIG_USB_DEBUG_ISOC
839 printk (KERN_DEBUG
"uhci_init_isoc: buf_size too small (%d < %d)\n",
840 isocdesc
->buf_size
, isocdesc
->frame_count
* isocdesc
->frame_size
);
845 /* Check buffer ptr for Null. */
846 if (!isocdesc
->data
) {
847 #ifdef CONFIG_USB_DEBUG_ISOC
848 printk (KERN_DEBUG
"uhci_init_isoc: data ptr is null\n");
853 #ifdef NEED_ALIGNMENT
854 /* Check data page alignment. */
855 if (((int)(isocdesc
->data
) & (PAGE_SIZE
- 1)) != 0) {
856 #ifdef CONFIG_USB_DEBUG_ISOC
857 printk (KERN_DEBUG
"uhci_init_isoc: buffer must be page-aligned (%p)\n",
862 #endif /* NEED_ALIGNMENT */
865 * Check start_type unless pr_isocdesc is used.
867 if (!pr_isocdesc
&& (isocdesc
->start_type
> START_TYPE_MAX
)) {
868 #ifdef CONFIG_USB_DEBUG_ISOC
869 printk (KERN_DEBUG
"uhci_run_isoc: invalid start_type (%d)\n",
870 isocdesc
->start_type
);
876 * Check start_frame for inside a valid range.
877 * Only allow transfer requests to be made 1.000 second
880 cur_frame
= uhci_get_current_frame_number (isocdesc
->usb_dev
);
882 /* if not START_ASAP (i.e., RELATIVE or ABSOLUTE): */
884 if (isocdesc
->start_type
== START_RELATIVE
) {
885 if ((isocdesc
->start_frame
< 0) || (isocdesc
->start_frame
> CAN_SCHEDULE_FRAMES
)) {
886 #ifdef CONFIG_USB_DEBUG_ISOC
887 printk (KERN_DEBUG
"uhci_init_isoc: bad start_frame value (%d)\n",
888 isocdesc
->start_frame
);
892 } /* end START_RELATIVE */
894 if (isocdesc
->start_type
== START_ABSOLUTE
) { /* within the scope of cur_frame */
895 ix
= USB_WRAP_FRAMENR(isocdesc
->start_frame
- cur_frame
);
896 if (ix
< START_FRAME_FUDGE
|| /* too small */
897 ix
> CAN_SCHEDULE_FRAMES
) { /* too large */
898 #ifdef CONFIG_USB_DEBUG_ISOC
899 printk (KERN_DEBUG
"uhci_init_isoc: bad start_frame value (%d,%d)\n",
900 isocdesc
->start_frame
, cur_frame
);
904 } /* end START_ABSOLUTE */
905 } /* end not pr_isocdesc */
908 * Set the start/end frame numbers.
911 isocdesc
->start_frame
= pr_isocdesc
->end_frame
+ 1;
912 } else if (isocdesc
->start_type
== START_RELATIVE
) {
913 if (isocdesc
->start_frame
< START_FRAME_FUDGE
)
914 isocdesc
->start_frame
= START_FRAME_FUDGE
;
915 isocdesc
->start_frame
+= cur_frame
;
916 } else if (isocdesc
->start_type
== START_ASAP
) {
917 isocdesc
->start_frame
= cur_frame
+ START_FRAME_FUDGE
;
920 /* and see if start_frame needs any correction */
921 /* only wrap to USB frame numbers, the frame_list insertion routine
922 takes care of the wrapping to the frame_list size */
923 isocdesc
->start_frame
= USB_WRAP_FRAMENR(isocdesc
->start_frame
);
925 /* and fix the end_frame value */
926 isocdesc
->end_frame
= USB_WRAP_FRAMENR(isocdesc
->start_frame
+ isocdesc
->frame_count
- 1);
928 isocdesc
->prev_completed_frame
= -1;
929 isocdesc
->cur_completed_frame
= -1;
931 destination
= (isocdesc
->pipe
& PIPE_DEVEP_MASK
) |
932 usb_packetid (isocdesc
->pipe
);
933 status
= TD_CTRL_ACTIVE
| TD_CTRL_IOS
; /* mark Isoc.; can't be low speed */
934 pipeinput
= usb_pipein (isocdesc
->pipe
);
935 cur_frame
= isocdesc
->start_frame
;
936 bufptr
= isocdesc
->data
;
939 * Build the Data TDs.
940 * TBD: Not using frame_spacing (Yet). Defaults to 1 (every frame).
941 * (frame_spacing is a way to request less bandwidth.)
942 * This can also be done by using frame_length = 0 in the
943 * frame_desc array, but this way won't take less bandwidth
944 * allocation into account.
947 if (isocdesc
->frame_spacing
<= 0)
948 isocdesc
->frame_spacing
= 1;
950 for (ix
= 0, td
= isocdesc
->td
, fd
= isocdesc
->frames
;
951 ix
< isocdesc
->frame_count
; ix
++, td
++, fd
++) {
952 frlen
= fd
->frame_length
;
953 if (frlen
> isocdesc
->frame_size
)
954 frlen
= isocdesc
->frame_size
;
957 td
->info
= destination
| /* use Actual len on OUT; max. on IN */
958 (pipeinput
? ((isocdesc
->frame_size
- 1) << 21)
959 : ((frlen
- 1) << 21));
962 td
->dev_id
= isocdesc
; /* can get dev_id or context from isocdesc */
964 td
->info
= destination
| ((frlen
- 1) << 21);
965 td
->buffer
= virt_to_bus (bufptr
);
967 td
->isoc_td_number
= ix
; /* 0-based; does not wrap/overflow back to 0 */
969 if (isocdesc
->callback_frames
&&
970 (++cb_frames
>= isocdesc
->callback_frames
)) {
971 td
->status
|= TD_CTRL_IOC
;
972 td
->completed
= isocdesc
->callback_fn
;
974 uhci_add_irq_list (dev
->uhci
, td
, isocdesc
->callback_fn
, isocdesc
->context
);
977 bufptr
+= fd
->frame_length
; /* or isocdesc->frame_size; */
980 * Insert the TD in the frame list.
982 uhci_add_frame_list(uhci
, td
, cur_frame
);
984 cur_frame
= USB_WRAP_FRAMENR(cur_frame
+1);
988 * Add IOC on the last TD.
991 if (!(td
->status
& TD_CTRL_IOC
)) {
992 td
->status
|= TD_CTRL_IOC
;
993 td
->completed
= isocdesc
->callback_fn
;
994 uhci_add_irq_list(dev
->uhci
, td
, isocdesc
->callback_fn
, isocdesc
->context
); /* TBD: D.K. ??? */
997 } /* end uhci_run_isoc */
1002 * Marks a TD list as Inactive and removes it from the Isoc.
1005 * Does not free any memory resources.
1007 * Returns 0 for success or negative value for error.
1009 static int uhci_kill_isoc (struct usb_isoc_desc
*isocdesc
)
1011 struct uhci_device
*dev
= usb_to_uhci (isocdesc
->usb_dev
);
1012 struct uhci
*uhci
= dev
->uhci
;
1016 if (USB_WRAP_FRAMENR(isocdesc
->start_frame
) != isocdesc
->start_frame
) {
1017 #ifdef CONFIG_USB_DEBUG_ISOC
1018 printk (KERN_DEBUG
"uhci_kill_isoc: invalid start_frame (%d)\n",
1019 isocdesc
->start_frame
);
1024 for (ix
= 0, td
= isocdesc
->td
; ix
< isocdesc
->frame_count
; ix
++, td
++) {
1025 uhci_remove_frame_list(uhci
, td
);
1026 td
->status
&= ~(TD_CTRL_ACTIVE
| TD_CTRL_IOC
);
1029 isocdesc
->start_frame
= -1;
1031 } /* end uhci_kill_isoc */
1033 static void uhci_free_isoc (struct usb_isoc_desc
*isocdesc
)
1037 /* If still Active, kill it. */
1038 if (isocdesc
->start_frame
>= 0)
1039 uhci_kill_isoc(isocdesc
);
1041 /* Remove all td's from the IRQ list. */
1042 for(i
= 0; i
< isocdesc
->frame_count
; i
++)
1043 uhci_remove_irq_list(((struct uhci_td
*)(isocdesc
->td
))+i
);
1045 /* Free the associate memory. */
1047 kfree(isocdesc
->td
);
1050 } /* end uhci_free_isoc */
1053 * Control thread operations: we just mark the last TD
1054 * in a control thread as an interrupt TD, and wake up
1055 * the front-end on completion.
1057 * We need to remove the TD from the lists (both interrupt
1058 * list and TD lists) by hand if something bad happens!
1061 static int uhci_generic_completed(int status
, void *buffer
, int len
, void *dev_id
)
1063 wait_queue_head_t
*wakeup
= (wait_queue_head_t
*)dev_id
;
1065 if (waitqueue_active(wakeup
))
1068 printk("waitqueue empty!\n");
1070 return 0; /* Don't re-instate */
1073 /* td points to the last td in the list, which interrupts on completion */
1074 static int uhci_run_control(struct uhci_device
*dev
, struct uhci_td
*first
, struct uhci_td
*last
, int timeout
)
1076 DECLARE_WAITQUEUE(wait
, current
);
1077 struct uhci_qh
*qh
= uhci_qh_alloc(dev
);
1082 current
->state
= TASK_UNINTERRUPTIBLE
;
1083 add_wait_queue(&qh
->wakeup
, &wait
);
1085 uhci_add_irq_list(dev
->uhci
, last
, uhci_generic_completed
, &qh
->wakeup
);
1087 uhci_insert_tds_in_qh(qh
, first
, last
);
1089 /* Add it into the skeleton */
1090 uhci_insert_qh(&dev
->uhci
->skel_control_qh
, qh
);
1092 /* wait a user specified reasonable amount of time */
1093 schedule_timeout(timeout
);
1095 remove_wait_queue(&qh
->wakeup
, &wait
);
1097 /* Clean up in case it failed.. */
1098 uhci_remove_irq_list(last
);
1100 /* Remove it from the skeleton */
1101 uhci_remove_qh(&dev
->uhci
->skel_control_qh
, qh
);
1105 return uhci_td_result(dev
, last
, NULL
, 1);
1109 * Send or receive a control message on a pipe.
1111 * Note that the "pipe" structure is set up to map
1112 * easily to the uhci destination fields.
1114 * A control message is built up from three parts:
1115 * - The command itself
1116 * - [ optional ] data phase
1117 * - Status complete phase
1119 * The data phase can be an arbitrary number of TD's
1120 * although we currently had better not have more than
1121 * 29 TD's here (we have 31 TD's allocated for control
1122 * operations, and two of them are used for command and
1125 * 29 TD's is a minimum of 232 bytes worth of control
1126 * information, that's just ridiculously high. Most
1127 * control messages have just a few bytes of data.
1129 * 232 is not ridiculously high with many of the
1130 * configurations on audio devices, etc. anyway,
1131 * there is no restriction on length of transfers
1134 static int uhci_control_msg(struct usb_device
*usb_dev
, unsigned int pipe
, devrequest
*cmd
, void *data
, int len
, int timeout
)
1136 struct uhci_device
*dev
= usb_to_uhci(usb_dev
);
1137 struct uhci_td
*first
, *td
, *prevtd
;
1138 unsigned long destination
, status
;
1140 int maxsze
= usb_maxpacket(usb_dev
, pipe
, usb_pipeout(pipe
));
1142 unsigned long bytesrequested
= len
;
1143 unsigned long bytesread
= 0;
1145 first
= td
= uhci_td_alloc(dev
);
1149 /* The "pipe" thing contains the destination in bits 8--18 */
1150 destination
= (pipe
& PIPE_DEVEP_MASK
) | USB_PID_SETUP
;
1153 status
= (pipe
& TD_CTRL_LS
) | TD_CTRL_ACTIVE
| TD_CTRL_SPD
| (3 << 27);
1156 * Build the TD for the control request
1158 td
->status
= status
; /* Try forever */
1159 td
->info
= destination
| (7 << 21); /* 8 bytes of data */
1160 td
->buffer
= virt_to_bus(cmd
);
1163 * If direction is "send", change the frame from SETUP (0x2D)
1164 * to OUT (0xE1). Else change it from SETUP to IN (0x69).
1166 destination
^= (USB_PID_SETUP
^ USB_PID_IN
); /* SETUP -> IN */
1167 if (usb_pipeout(pipe
))
1168 destination
^= (USB_PID_IN
^ USB_PID_OUT
); /* IN -> OUT */
1171 td
= uhci_td_alloc(dev
);
1175 prevtd
->link
= virt_to_bus(td
) | UHCI_PTR_DEPTH
;
1178 * Build the DATA TD's
1181 /* Build the TD for control status */
1184 if (pktsze
> maxsze
)
1187 /* Alternate Data0/1 (start with Data1) */
1188 destination
^= 1 << TD_TOKEN_TOGGLE
;
1190 td
->status
= status
; /* Status */
1191 td
->info
= destination
| ((pktsze
- 1) << 21); /* pktsze bytes of data */
1192 td
->buffer
= virt_to_bus(data
);
1193 td
->backptr
= &prevtd
->link
;
1199 td
= uhci_td_alloc(dev
);
1202 prevtd
->link
= virt_to_bus(td
) | UHCI_PTR_DEPTH
; /* Update previous TD */
1206 * Build the final TD for control status
1208 /* It's only IN if the pipe is out AND we aren't expecting data */
1209 destination
&= ~0xFF;
1210 if (usb_pipeout(pipe
) | (bytesrequested
== 0))
1211 destination
|= USB_PID_IN
;
1213 destination
|= USB_PID_OUT
;
1215 destination
|= 1 << TD_TOKEN_TOGGLE
; /* End in Data1 */
1217 td
->status
= status
| TD_CTRL_IOC
; /* no limit on errors on final packet */
1218 td
->info
= destination
| (UHCI_NULL_DATA_SIZE
<< 21); /* 0 bytes of data */
1220 td
->backptr
= &prevtd
->link
;
1221 td
->link
= UHCI_PTR_TERM
; /* Terminate */
1224 ret
= uhci_run_control(dev
, first
, td
, timeout
);
1229 if (!uhci_status_bits(td
->status
) && ((td
->info
& 0xFF) ==
1231 bytesread
+= uhci_actual_length(td
->status
);
1233 nextlink
= td
->link
;
1237 if (nextlink
& UHCI_PTR_TERM
) /* Tail? */
1240 td
= uhci_ptr_to_virt(nextlink
);
1244 printk(KERN_ERR
"runaway td's!?\n");
1246 if (ret
&& (bytesread
>= bytesrequested
)) {
1247 printk(KERN_DEBUG
"Recovered sufficient data (asked for %ld, got %ld) from failed cmd\n",
1248 bytesrequested
, bytesread
);
1252 if (uhci_debug
&& ret
) {
1253 __u8
*p
= (__u8
*)cmd
;
1255 printk(KERN_DEBUG
"Failed cmd - %02X %02X %02X %02X %02X %02X %02X %02X\n",
1256 p
[0], p
[1], p
[2], p
[3], p
[4], p
[5], p
[6], p
[7]);
1262 * Bulk thread operations: we just mark the last TD
1263 * in a bulk thread as an interrupt TD, and wake up
1264 * the front-end on completion.
1266 * We need to remove the TD from the lists (both interrupt
1267 * list and TD lists) by hand if something bad happens!
1270 /* td points to the last td in the list, which interrupts on completion */
1271 static int uhci_run_bulk(struct uhci_device
*dev
, struct uhci_td
*first
, struct uhci_td
*last
, unsigned long *rval
, int timeout
)
1273 DECLARE_WAITQUEUE(wait
, current
);
1274 struct uhci_qh
*qh
= uhci_qh_alloc(dev
);
1279 current
->state
= TASK_UNINTERRUPTIBLE
;
1280 add_wait_queue(&qh
->wakeup
, &wait
);
1282 uhci_add_irq_list(dev
->uhci
, last
, uhci_generic_completed
, &qh
->wakeup
);
1284 uhci_insert_tds_in_qh(qh
, first
, last
);
1286 /* Add it into the skeleton */
1287 uhci_insert_qh(&dev
->uhci
->skel_bulk_qh
, qh
);
1289 /* wait a user specified reasonable amount of time */
1290 schedule_timeout(timeout
);
1292 remove_wait_queue(&qh
->wakeup
, &wait
);
1294 /* Clean up in case it failed.. */
1295 uhci_remove_irq_list(last
);
1297 uhci_remove_qh(&dev
->uhci
->skel_bulk_qh
, qh
);
1301 return uhci_td_result(dev
, last
, rval
, 1);
1305 * Send or receive a bulk message on a pipe.
1307 * Note that the "pipe" structure is set up to map
1308 * easily to the uhci destination fields.
1310 * A bulk message is only built up from
1313 static int uhci_bulk_msg(struct usb_device
*usb_dev
, unsigned int pipe
, void *data
, int len
, unsigned long *rval
, int timeout
)
1315 struct uhci_device
*dev
= usb_to_uhci(usb_dev
);
1316 struct uhci_td
*first
, *td
, *prevtd
;
1317 unsigned long destination
, status
;
1319 int maxsze
= usb_maxpacket(usb_dev
, pipe
, usb_pipeout(pipe
));
1321 if (usb_endpoint_halted(usb_dev
, usb_pipeendpoint(pipe
), usb_pipeout(pipe
)) &&
1322 usb_clear_halt(usb_dev
, usb_pipeendpoint(pipe
) | (pipe
& USB_DIR_IN
)))
1323 return USB_ST_STALL
;
1325 /* The "pipe" thing contains the destination in bits 8--18. */
1326 destination
= (pipe
& PIPE_DEVEP_MASK
) | usb_packetid (pipe
);
1329 status
= (pipe
& TD_CTRL_LS
) | TD_CTRL_ACTIVE
| TD_CTRL_SPD
| (3 << 27);
1332 * Build the TDs for the bulk request
1334 first
= td
= uhci_td_alloc(dev
);
1338 prevtd
= first
; //This is fake, but at least it's not NULL
1340 /* Build the TD for control status */
1343 if (pktsze
> maxsze
)
1346 td
->status
= status
; /* Status */
1347 td
->info
= destination
| ((pktsze
-1) << 21) |
1348 (usb_gettoggle(usb_dev
, usb_pipeendpoint(pipe
),
1349 usb_pipeout(pipe
)) << TD_TOKEN_TOGGLE
); /* pktsze bytes of data */
1350 td
->buffer
= virt_to_bus(data
);
1351 td
->backptr
= &prevtd
->link
;
1358 td
= uhci_td_alloc(dev
);
1362 prevtd
->link
= virt_to_bus(td
) | UHCI_PTR_DEPTH
;/* Update previous TD */
1365 /* Alternate Data0/1 (start with Data0) */
1366 usb_dotoggle(usb_dev
, usb_pipeendpoint(pipe
), usb_pipeout(pipe
));
1369 td
->link
= 1; /* Terminate */
1370 td
->status
|= TD_CTRL_IOC
;
1372 /* CHANGE DIRECTION HERE! SAVE IT SOMEWHERE IN THE ENDPOINT!!! */
1375 ret
= uhci_run_bulk(dev
, first
, td
, rval
, timeout
);
1379 struct uhci_td
*curtd
= first
;
1380 unsigned int nextlink
;
1383 nextlink
= curtd
->link
;
1384 uhci_remove_td(curtd
);
1385 uhci_td_free(curtd
);
1387 if (nextlink
& UHCI_PTR_TERM
) /* Tail? */
1390 curtd
= uhci_ptr_to_virt(nextlink
);
1394 printk(KERN_DEBUG
"runaway td's!?\n");
1400 static void * uhci_request_bulk(struct usb_device
*usb_dev
, unsigned int pipe
, usb_device_irq handler
, void *data
, int len
, void *dev_id
)
1402 struct uhci_device
*dev
= usb_to_uhci(usb_dev
);
1403 struct uhci
*uhci
= dev
->uhci
;
1404 struct uhci_td
*first
, *td
, *prevtd
;
1405 struct uhci_qh
*bulk_qh
= uhci_qh_alloc(dev
);
1406 unsigned long destination
, status
;
1407 int maxsze
= usb_maxpacket(usb_dev
, pipe
, usb_pipeout(pipe
));
1409 /* The "pipe" thing contains the destination in bits 8--18. */
1410 destination
= (pipe
& PIPE_DEVEP_MASK
) | usb_packetid(pipe
);
1412 /* Infinite errors is 0 */
1413 status
= (pipe
& TD_CTRL_LS
) | TD_CTRL_ACTIVE
| TD_CTRL_SPD
;
1415 /* Build the TDs for the bulk request */
1416 first
= td
= uhci_td_alloc(dev
);
1419 /* Build the TD for control status */
1422 if (pktsze
> maxsze
)
1425 td
->status
= status
;
1426 td
->info
= destination
| ((pktsze
-1) << 21) |
1427 (usb_gettoggle(usb_dev
, usb_pipeendpoint(pipe
), usb_pipeout(pipe
)) << TD_TOKEN_TOGGLE
); /* pktsze bytes of data */
1428 td
->buffer
= virt_to_bus(data
);
1429 td
->backptr
= &prevtd
->link
;
1437 td
= uhci_td_alloc(dev
);
1438 prevtd
->link
= virt_to_bus(td
) | UHCI_PTR_DEPTH
;
1441 /* Alternate Data0/1 */
1442 usb_dotoggle(usb_dev
, usb_pipeendpoint(pipe
), usb_pipeout(pipe
));
1445 first
->backptr
= NULL
;
1446 td
->link
= 1; /* Terminate */
1447 td
->status
= status
| TD_CTRL_IOC
; /* IOC */
1449 uhci_add_irq_list(dev
->uhci
, td
, handler
, dev_id
);
1451 uhci_insert_tds_in_qh(bulk_qh
, first
, td
);
1453 bulk_qh
->skel
= &uhci
->skel_bulk_qh
;
1454 uhci_insert_qh(&uhci
->skel_bulk_qh
, bulk_qh
);
1456 /* Return last td for removal */
1461 * Remove a handler from a pipe. This terminates the transfer.
1462 * We have some assumptions here:
1463 * There is only one queue using this pipe. (the one we remove)
1464 * Any data that is in the queue is useless for us, we throw it away.
1466 static int uhci_terminate_bulk(struct usb_device
*dev
, void *first
)
1468 /* none found? there is nothing to remove! */
1472 uhci_remove_transfer(first
, 1);
1477 struct usb_operations uhci_device_operations
= {
1485 uhci_terminate_bulk
,
1486 uhci_get_current_frame_number
,
1494 * This is just incredibly fragile. The timings must be just
1495 * right, and they aren't really documented very well.
1497 * Note the short delay between disabling reset and enabling
1500 static void uhci_reset_port(unsigned int port
)
1502 unsigned short status
;
1505 outw(status
| USBPORTSC_PR
, port
); /* reset port */
1507 outw(status
& ~USBPORTSC_PR
, port
);
1511 outw(status
| USBPORTSC_PE
, port
); /* enable port */
1515 if (!(status
& USBPORTSC_PE
)) {
1516 outw(status
| USBPORTSC_PE
, port
); /* one more try at enabling port */
1523 * This gets called if the connect status on the root
1524 * hub (and the root hub only) changes.
1526 static void uhci_connect_change(struct uhci
*uhci
, unsigned int port
, unsigned int nr
)
1528 struct usb_device
*usb_dev
;
1529 struct uhci_device
*dev
;
1530 unsigned short status
;
1531 struct uhci_device
*root_hub
= usb_to_uhci(uhci
->bus
->root_hub
);
1534 printk(KERN_INFO
"uhci_connect_change: called for %d\n", nr
);
1538 * Even if the status says we're connected,
1539 * the fact that the status bits changed may
1540 * that we got disconnected and then reconnected.
1542 * So start off by getting rid of any old devices..
1544 usb_disconnect(&root_hub
->usb
->children
[nr
]);
1548 /* If we have nothing connected, then clear change status and disable the port */
1549 status
= (status
& ~USBPORTSC_PE
) | USBPORTSC_PEC
;
1550 if (!(status
& USBPORTSC_CCS
)) {
1556 * Ok, we got a new connection. Allocate a device to it,
1557 * and find out what it wants to do..
1559 usb_dev
= usb_alloc_dev(root_hub
->usb
, root_hub
->usb
->bus
);
1563 dev
= usb_dev
->hcpriv
;
1565 usb_connect(usb_dev
);
1567 root_hub
->usb
->children
[nr
] = usb_dev
;
1569 wait_ms(200); /* wait for powerup */
1570 uhci_reset_port(port
);
1572 /* Get speed information */
1573 usb_dev
->slow
= (inw(port
) & USBPORTSC_LSDA
) ? 1 : 0;
1576 * Ok, all the stuff specific to the root hub has been done.
1577 * The rest is generic for any new USB attach, regardless of
1580 if (usb_new_device(usb_dev
)) {
1581 unsigned short status
= inw(port
);
1583 printk(KERN_INFO
"uhci: disabling malfunctioning port %d\n",
1585 outw(status
| USBPORTSC_PE
, port
);
1590 * This gets called when the root hub configuration
1591 * has changed. Just go through each port, seeing if
1592 * there is something interesting happening.
1594 static void uhci_check_configuration(struct uhci
*uhci
)
1596 struct uhci_device
*root_hub
= usb_to_uhci(uhci
->bus
->root_hub
);
1597 unsigned int io_addr
= uhci
->io_addr
+ USBPORTSC1
;
1598 int maxchild
= root_hub
->usb
->maxchild
;
1602 unsigned short status
= inw(io_addr
);
1604 if (status
& USBPORTSC_CSC
)
1605 uhci_connect_change(uhci
, io_addr
, nr
);
1608 } while (nr
< maxchild
);
1612 static int fixup_isoc_desc (struct uhci_td
*td
)
1614 struct usb_isoc_desc
*isocdesc
= td
->dev_id
;
1615 struct uhci_td
*prtd
;
1616 struct isoc_frame_desc
*frm
;
1617 int first_comp
= isocdesc
->cur_completed_frame
+ 1; /* 0-based */
1618 int cur_comp
= td
->isoc_td_number
; /* 0-based */
1622 if (first_comp
>= isocdesc
->frame_count
)
1624 num_comp
= cur_comp
- first_comp
+ 1;
1626 #ifdef CONFIG_USB_DEBUG_ISOC
1627 printk ("fixup_isoc_desc.1: td = %p, id = %p, first_comp = %d, cur_comp = %d, num_comp = %d\n",
1628 td
, isocdesc
, first_comp
, cur_comp
, num_comp
);
1631 for (ix
= 0, fx
= first_comp
, prtd
= ((struct uhci_td
*)(isocdesc
->td
))+first_comp
, frm
= &isocdesc
->frames
[first_comp
];
1632 ix
< num_comp
; ix
++) {
1633 frm
->frame_length
= uhci_actual_length (prtd
->status
);
1634 isocdesc
->total_length
+= frm
->frame_length
;
1636 if ((frm
->frame_status
= uhci_map_status (uhci_status_bits (prtd
->status
),
1637 uhci_packetout (prtd
->info
))))
1638 isocdesc
->error_count
++;
1642 if (++fx
>= isocdesc
->frame_count
) { /* wrap fx, prtd, and frm */
1644 prtd
= isocdesc
->td
;
1645 frm
= isocdesc
->frames
;
1650 * Update some other fields for drivers.
1652 isocdesc
->prev_completed_frame
= isocdesc
->cur_completed_frame
;
1653 isocdesc
->cur_completed_frame
= cur_comp
;
1654 isocdesc
->total_completed_frames
+= num_comp
; /* 1-based */
1656 #ifdef CONFIG_USB_DEBUG_ISOC
1657 printk ("fixup_isoc_desc.2: total_comp_frames = %d, total_length = %d, error_count = %d\n",
1658 isocdesc
->total_completed_frames
, isocdesc
->total_length
, isocdesc
->error_count
);
1659 #endif /* CONFIG_USB_DEBUG_ISOC */
1665 static void uhci_interrupt_notify(struct uhci
*uhci
)
1667 struct list_head
*tmp
, *head
= &uhci
->interrupt_list
;
1670 spin_lock(&irqlist_lock
);
1672 while (tmp
!= head
) {
1673 struct uhci_td
*td
= list_entry(tmp
, struct uhci_td
, irq_list
);
1678 /* We're interested if there was an error or if the chain of */
1679 /* TD's completed successfully */
1680 status
= uhci_td_result(td
->dev
, td
, &rval
, 0);
1682 if ((status
== USB_ST_NOERROR
) && (td
->status
& TD_CTRL_ACTIVE
))
1685 /* remove from IRQ list */
1686 list_del(&td
->irq_list
);
1687 INIT_LIST_HEAD(&td
->irq_list
);
1689 if (td
->completed(status
, bus_to_virt(td
->buffer
), rval
,
1691 struct usb_device
*usb_dev
= td
->dev
->usb
;
1693 list_add(&td
->irq_list
, &uhci
->interrupt_list
);
1695 usb_dotoggle(usb_dev
, usb_pipeendpoint(td
->info
), usb_pipeout(td
->info
) ^ 1);
1696 td
->info
&= ~(1 << 19); /* clear data toggle */
1697 td
->info
|= usb_gettoggle(usb_dev
, usb_pipeendpoint(td
->info
),
1698 uhci_packetout(td
->info
)) << 19; /* toggle between data0 and data1 */
1699 td
->status
= (td
->status
& 0x2F000000) | TD_CTRL_ACTIVE
| TD_CTRL_IOC
;
1700 /* The HC only removes it when it completed */
1701 /* successfully, so force remove and readd it */
1703 uhci_insert_td_in_qh(td
->qh
, td
);
1704 } else if (td
->flags
& UHCI_TD_REMOVE
) {
1705 struct usb_device
*usb_dev
= td
->dev
->usb
;
1707 /* marked for removal */
1708 td
->flags
&= ~UHCI_TD_REMOVE
;
1709 usb_dotoggle(usb_dev
, usb_pipeendpoint(td
->info
), uhci_packetout(td
->info
));
1710 uhci_remove_qh(td
->qh
->skel
, td
->qh
);
1711 uhci_qh_free(td
->qh
);
1715 /* If completed does not wants to reactivate, then */
1716 /* it's responsible for free'ing the TD's and QH's */
1717 /* or another function (such as run_control) */
1719 spin_unlock(&irqlist_lock
);
1723 * Check port status - Connect Status Change - for
1724 * each of the attached ports (defaults to two ports,
1725 * but at least in theory there can be more of them).
1727 * Wake up the configurator if something happened, we
1728 * can't really do much at interrupt time.
1730 static void uhci_root_hub_events(struct uhci
*uhci
, unsigned int io_addr
)
1732 if (waitqueue_active(&uhci_configure
)) {
1733 struct uhci_device
*root_hub
= usb_to_uhci(uhci
->bus
->root_hub
);
1734 int ports
= root_hub
->usb
->maxchild
;
1736 io_addr
+= USBPORTSC1
;
1738 if (inw(io_addr
) & USBPORTSC_CSC
) {
1739 wake_up(&uhci_configure
);
1743 } while (--ports
> 0);
1747 static void uhci_interrupt(int irq
, void *__uhci
, struct pt_regs
*regs
)
1749 struct uhci
*uhci
= __uhci
;
1750 unsigned int io_addr
= uhci
->io_addr
;
1751 unsigned short status
;
1754 * Read the interrupt status, and write it back to clear the
1757 status
= inw(io_addr
+ USBSTS
);
1758 if (!status
) /* shared interrupt, not mine */
1760 outw(status
, io_addr
+ USBSTS
);
1762 /* Walk the list of pending TD's to see which ones completed.. */
1763 uhci_interrupt_notify(uhci
);
1765 /* Check if there are any events on the root hub.. */
1766 uhci_root_hub_events(uhci
, io_addr
);
1770 * We init one packet, and mark it just IOC and _not_
1771 * active. Which will result in no actual USB traffic,
1772 * but _will_ result in an interrupt every second.
1774 * Which is exactly what we want.
1776 static void uhci_init_ticktd(struct uhci
*uhci
)
1778 struct uhci_device
*dev
= usb_to_uhci(uhci
->bus
->root_hub
);
1779 struct uhci_td
*td
= uhci_td_alloc(dev
);
1782 printk(KERN_ERR
"unable to allocate ticktd\n");
1786 /* Don't clobber the frame */
1787 td
->link
= uhci
->fl
->frame
[0];
1788 td
->backptr
= &uhci
->fl
->frame
[0];
1789 td
->status
= TD_CTRL_IOC
;
1790 td
->info
= (15 << 21) | (0x7f << 8) | USB_PID_IN
; /* (ignored) input packet, 16 bytes, device 127 */
1794 uhci
->fl
->frame
[0] = virt_to_bus(td
);
1799 static void reset_hc(struct uhci
*uhci
)
1801 unsigned int io_addr
= uhci
->io_addr
;
1803 /* Global reset for 50ms */
1804 outw(USBCMD_GRESET
, io_addr
+ USBCMD
);
1806 outw(0, io_addr
+ USBCMD
);
1810 static void start_hc(struct uhci
*uhci
)
1812 unsigned int io_addr
= uhci
->io_addr
;
1815 uhci_init_ticktd(uhci
);
1818 * Reset the HC - this will force us to get a
1819 * new notification of any already connected
1820 * ports due to the virtual disconnect that it
1823 outw(USBCMD_HCRESET
, io_addr
+ USBCMD
);
1824 while (inw(io_addr
+ USBCMD
) & USBCMD_HCRESET
) {
1826 printk(KERN_ERR
"USBCMD_HCRESET timed out!\n");
1831 /* Turn on all interrupts */
1832 outw(USBINTR_TIMEOUT
| USBINTR_RESUME
| USBINTR_IOC
| USBINTR_SP
, io_addr
+ USBINTR
);
1834 /* Start at frame 0 */
1835 outw(0, io_addr
+ USBFRNUM
);
1836 outl(virt_to_bus(uhci
->fl
), io_addr
+ USBFLBASEADD
);
1838 /* Run and mark it configured with a 64-byte max packet */
1839 outw(USBCMD_RS
| USBCMD_CF
| USBCMD_MAXP
, io_addr
+ USBCMD
);
1843 * Allocate a frame list, and four regular queues.
1845 * The hardware doesn't really know any difference
1846 * in the queues, but the order does matter for the
1847 * protocols higher up. The order is:
1849 * - any isochronous events handled before any
1850 * of the queues. We don't do that here, because
1851 * we'll create the actual TD entries on demand.
1852 * - The first queue is the "interrupt queue".
1853 * - The second queue is the "control queue".
1854 * - The third queue is "bulk data".
1856 * We could certainly have multiple queues of the same
1857 * type, and maybe we should. We could have per-device
1858 * queues, for example. We begin small.
1860 * Queues are dynamically allocated for devices now,
1861 * this code only sets up the skeleton queue
1863 static struct uhci
*alloc_uhci(unsigned int io_addr
, unsigned int io_size
)
1867 struct usb_bus
*bus
;
1868 struct uhci_device
*dev
;
1869 struct usb_device
*usb
;
1871 uhci
= kmalloc(sizeof(*uhci
), GFP_KERNEL
);
1875 memset(uhci
, 0, sizeof(*uhci
));
1878 uhci
->io_addr
= io_addr
;
1879 uhci
->io_size
= io_size
;
1880 INIT_LIST_HEAD(&uhci
->interrupt_list
);
1882 /* We need exactly one page (per UHCI specs), how convenient */
1883 uhci
->fl
= (void *)__get_free_page(GFP_KERNEL
);
1887 bus
= usb_alloc_bus(&uhci_device_operations
);
1895 * Allocate the root_hub
1897 usb
= usb_alloc_dev(NULL
, bus
);
1903 dev
= usb_to_uhci(usb
);
1906 uhci
->bus
->root_hub
= uhci_to_usb(dev
);
1908 /* Initialize the root hub */
1910 /* UHCI specs says devices must have 2 ports, but goes on to say */
1911 /* they may have more but give no way to determine how many they */
1912 /* have, so default to 2 */
1913 /* According to the UHCI spec, Bit 7 is always set to 1. So we try */
1914 /* to use this to our advantage */
1915 for (port
= 0; port
< (io_size
- 0x10) / 2; port
++) {
1916 unsigned int portstatus
;
1918 portstatus
= inw(io_addr
+ 0x10 + (port
* 2));
1919 if (!(portstatus
& 0x0080))
1922 printk(KERN_DEBUG
"Detected %d ports\n", port
);
1924 /* This is experimental so anything less than 2 or greater than 8 is */
1925 /* something weird and we'll ignore it */
1926 if (port
< 2 || port
> 8) {
1927 printk(KERN_DEBUG
"Port count misdetected, forcing to 2 ports\n");
1931 usb
->maxchild
= port
;
1932 usb_init_root_hub(usb
);
1934 /* 8 Interrupt queues */
1935 for (i
= 0; i
< 8; i
++) {
1936 struct uhci_qh
*qh
= &uhci
->skelqh
[i
];
1938 qh
->link
= virt_to_bus(&uhci
->skel_control_qh
) | UHCI_PTR_QH
;
1939 qh
->element
= UHCI_PTR_TERM
;
1942 uhci
->skel_control_qh
.link
= virt_to_bus(&uhci
->skel_bulk_qh
) |
1944 uhci
->skel_control_qh
.element
= UHCI_PTR_TERM
;
1946 uhci
->skel_bulk_qh
.link
= UHCI_PTR_TERM
;
1947 uhci
->skel_bulk_qh
.element
= UHCI_PTR_TERM
;
1950 * Fill the frame list: make all entries point to
1951 * the proper interrupt queue.
1953 * This is probably silly, but it's a simple way to
1954 * scatter the interrupt queues in a way that gives
1955 * us a reasonable dynamic range for irq latencies.
1957 for (i
= 0; i
< 1024; i
++) {
1958 struct uhci_qh
*irq
= &uhci
->skel_int2_qh
;
1980 uhci
->fl
->frame
[i
] = virt_to_bus(irq
) | UHCI_PTR_QH
;
1992 free_page((unsigned long)uhci
->fl
);
1999 * De-allocate all resources..
2001 static void release_uhci(struct uhci
*uhci
)
2003 if (uhci
->irq
>= 0) {
2004 free_irq(uhci
->irq
, uhci
);
2009 uhci_td_free(uhci
->ticktd
);
2010 uhci
->ticktd
= NULL
;
2014 free_page((unsigned long)uhci
->fl
);
2018 usb_free_bus(uhci
->bus
);
2022 static int uhci_control_thread(void *__uhci
)
2024 struct uhci
*uhci
= (struct uhci
*)__uhci
;
2026 uhci
->control_running
= 1;
2031 * This thread doesn't need any user-level access,
2032 * so get rid of all our resources..
2035 exit_files(current
);
2037 strcpy(current
->comm
, "uhci-control");
2040 * Ok, all systems are go..
2044 int unsigned long signr
;
2053 uhci_check_configuration(uhci
);
2055 interruptible_sleep_on(&uhci_configure
);
2057 if (signal_pending(current
)) {
2058 /* sending SIGUSR1 makes us print out some info */
2059 spin_lock_irq(¤t
->sigmask_lock
);
2060 signr
= dequeue_signal(¤t
->blocked
, &info
);
2061 spin_unlock_irq(¤t
->sigmask_lock
);
2063 if (signr
== SIGUSR1
) {
2064 printk(KERN_DEBUG
"UHCI queue dump:\n");
2065 uhci_show_queues(uhci
);
2066 } else if (signr
== SIGUSR2
) {
2067 uhci_debug
= !uhci_debug
;
2068 printk(KERN_DEBUG
"UHCI debug toggle = %x\n",
2073 } while (uhci
->control_continue
);
2079 uhci
->control_running
= 0;
2085 * If we've successfully found a UHCI, now is the time to increment the
2086 * module usage count, start the control thread, and return success..
2088 static int found_uhci(int irq
, unsigned int io_addr
, unsigned int io_size
)
2093 uhci
= alloc_uhci(io_addr
, io_size
);
2097 INIT_LIST_HEAD(&uhci
->uhci_list
);
2098 list_add(&uhci
->uhci_list
, &uhci_list
);
2100 request_region(uhci
->io_addr
, io_size
, "usb-uhci");
2104 usb_register_bus(uhci
->bus
);
2107 uhci
->control_continue
= 1;
2110 if (request_irq(irq
, uhci_interrupt
, SA_SHIRQ
, "uhci", uhci
) == 0) {
2114 pid
= kernel_thread(uhci_control_thread
, uhci
,
2115 CLONE_FS
| CLONE_FILES
| CLONE_SIGHAND
);
2117 uhci
->control_pid
= pid
;
2126 release_region(uhci
->io_addr
, uhci
->io_size
);
2132 static int start_uhci(struct pci_dev
*dev
)
2136 /* Search for the IO base address.. */
2137 for (i
= 0; i
< 6; i
++) {
2138 unsigned int io_addr
= dev
->resource
[i
].start
;
2139 unsigned int io_size
=
2140 dev
->resource
[i
].end
- dev
->resource
[i
].start
+ 1;
2143 if (!(dev
->resource
[i
].flags
& 1))
2146 /* Is it already in use? */
2147 if (check_region(io_addr
, io_size
))
2150 /* disable legacy emulation */
2151 pci_write_config_word(dev
, USBLEGSUP
, USBLEGSUP_DEFAULT
);
2153 return found_uhci(dev
->irq
, io_addr
, io_size
);
2159 static int handle_apm_event(apm_event_t event
)
2161 static int down
= 0;
2164 case APM_SYS_SUSPEND
:
2165 case APM_USER_SUSPEND
:
2167 printk(KERN_DEBUG
"uhci: received extra suspend event\n");
2172 case APM_NORMAL_RESUME
:
2173 case APM_CRITICAL_RESUME
:
2175 printk(KERN_DEBUG
"uhci: received bogus resume event\n");
2179 if (waitqueue_active(&uhci_configure
)) {
2181 wake_up(&uhci_configure
);
2192 struct pci_dev
*dev
= NULL
;
2195 uhci_td_cachep
= kmem_cache_create("uhci_td",
2196 sizeof(struct uhci_td
), 0,
2197 SLAB_HWCACHE_ALIGN
, NULL
, NULL
);
2199 if (!uhci_td_cachep
)
2202 uhci_qh_cachep
= kmem_cache_create("uhci_qh",
2203 sizeof(struct uhci_qh
), 0,
2204 SLAB_HWCACHE_ALIGN
, NULL
, NULL
);
2206 if (!uhci_qh_cachep
)
2211 dev
= pci_find_class(PCI_CLASS_SERIAL_USB
<< 8, dev
);
2216 pci_read_config_byte(dev
, PCI_CLASS_PROG
, &type
);
2221 retval
= start_uhci(dev
);
2226 apm_register_callback(&handle_apm_event
);
2233 void uhci_cleanup(void)
2235 struct list_head
*next
, *tmp
, *head
= &uhci_list
;
2239 while (tmp
!= head
) {
2240 struct uhci
*uhci
= list_entry(tmp
, struct uhci
, uhci_list
);
2241 struct uhci_device
*root_hub
= usb_to_uhci(uhci
->bus
->root_hub
);
2245 list_del(&uhci
->uhci_list
);
2246 INIT_LIST_HEAD(&uhci
->uhci_list
);
2248 /* Check if the process is still running */
2249 ret
= kill_proc(uhci
->control_pid
, 0, 1);
2251 /* Try a maximum of 10 seconds */
2252 int count
= 10 * 100;
2254 uhci
->control_continue
= 0;
2255 wake_up(&uhci_configure
);
2257 while (uhci
->control_running
&& --count
) {
2258 current
->state
= TASK_INTERRUPTIBLE
;
2259 schedule_timeout(1);
2263 printk(KERN_ERR
"uhci: giving up on killing uhci-control\n");
2267 for (i
= 0; i
< root_hub
->usb
->maxchild
; i
++)
2268 usb_disconnect(root_hub
->usb
->children
+ i
);
2270 usb_deregister_bus(uhci
->bus
);
2273 release_region(uhci
->io_addr
, uhci
->io_size
);
2280 if (kmem_cache_destroy(uhci_qh_cachep
))
2281 printk(KERN_INFO
"uhci: not all QH's were freed\n");
2283 if (kmem_cache_destroy(uhci_td_cachep
))
2284 printk(KERN_INFO
"uhci: not all TD's were freed\n");
2288 int init_module(void)
2293 void cleanup_module(void)
2296 apm_unregister_callback(&handle_apm_event
);