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
)
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
);
149 /* Must reset the toggle on first error */
151 printk(KERN_DEBUG
"Set toggle from %x rval %ld\n",
152 (unsigned int)tmp
, rval
? *rval
: 0);
155 usb_settoggle(dev
->usb
, uhci_endpoint(tmp
->info
),
156 uhci_packetout(tmp
->info
) ^ 1,
157 uhci_toggle(tmp
->info
));
161 if (rval
&& ((tmp
->info
& 0xFF) == USB_PID_IN
))
162 *rval
+= uhci_actual_length(tmp
->status
);
165 if ((tmp
->link
& UHCI_PTR_TERM
) ||
166 (tmp
->link
& UHCI_PTR_QH
))
169 tmp
= uhci_ptr_to_virt(tmp
->link
);
173 printk(KERN_ERR
"runaway td's in uhci_td_result!\n");
174 /* Force debugging on */
177 return USB_ST_NOERROR
;
179 /* Some debugging code */
180 if (debug
&& uhci_debug
) {
181 printk(KERN_DEBUG
"uhci_td_result() failed with status %x\n",
184 /* Print the chain for debugging purposes */
186 uhci_show_queue(td
->qh
);
191 if (status
& TD_CTRL_STALLED
) {
192 /* endpoint has stalled - mark it halted */
193 usb_endpoint_halt(dev
->usb
, uhci_endpoint(tmp
->info
),
194 uhci_packetout(tmp
->info
));
198 if ((status
== TD_CTRL_ACTIVE
) && (!rval
))
199 return USB_ST_DATAUNDERRUN
;
201 return uhci_map_status(status
, uhci_packetout(tmp
->info
));
205 * Inserts a td into qh list at the top.
207 * Careful about atomicity: even on UP this
208 * requires a locked access due to the concurrent
211 * NOTE! This assumes that first->last is a valid
212 * list of TD's with the proper backpointers set
215 static void uhci_insert_tds_in_qh(struct uhci_qh
*qh
, struct uhci_td
*first
, struct uhci_td
*last
)
217 unsigned int link
= qh
->element
;
218 unsigned int new = virt_to_bus(first
) | UHCI_PTR_DEPTH
;
221 unsigned char success
;
224 first
->backptr
= &qh
->element
;
225 asm volatile("lock ; cmpxchg %4,%2 ; sete %0"
226 :"=q" (success
), "=a" (link
)
227 :"m" (qh
->element
), "1" (link
), "r" (new)
231 /* Was there a successor entry? Fix it's backpointer */
232 if ((link
& UHCI_PTR_TERM
) == 0) {
233 struct uhci_td
*next
= uhci_ptr_to_virt(link
);
234 next
->backptr
= &last
->link
;
245 static inline void uhci_insert_td_in_qh(struct uhci_qh
*qh
, struct uhci_td
*td
)
247 uhci_insert_tds_in_qh(qh
, td
, td
);
250 static void uhci_insert_qh(struct uhci_qh
*qh
, struct uhci_qh
*newqh
)
252 newqh
->link
= qh
->link
;
253 qh
->link
= virt_to_bus(newqh
) | UHCI_PTR_QH
;
256 static void uhci_remove_qh(struct uhci_qh
*qh
, struct uhci_qh
*remqh
)
258 struct uhci_qh
*lqh
= qh
;
260 while (uhci_ptr_to_virt(lqh
->link
) != remqh
) {
261 if (lqh
->link
& UHCI_PTR_TERM
)
264 lqh
= uhci_ptr_to_virt(lqh
->link
);
267 if (lqh
->link
& UHCI_PTR_TERM
) {
268 printk(KERN_DEBUG
"couldn't find qh in chain!\n");
272 lqh
->link
= remqh
->link
;
276 * Removes td from qh if present.
278 * NOTE! We keep track of both forward and back-pointers,
279 * so this should be trivial, right?
281 * Wrong. While all TD insert/remove operations are synchronous
282 * on the CPU, the UHCI controller can (and does) play with the
283 * very first forward pointer. So we need to validate the backptr
284 * before we change it, so that we don't by mistake reset the QH
285 * head to something old.
287 static void uhci_remove_td(struct uhci_td
*td
)
289 unsigned int *backptr
= td
->backptr
;
290 unsigned int link
= td
->link
;
299 * This is the easy case: the UHCI will never change "td->link",
300 * so we can always just look at that and fix up the backpointer
301 * of any next element..
303 if (!(link
& UHCI_PTR_TERM
)) {
304 struct uhci_td
*next
= uhci_ptr_to_virt(link
);
305 next
->backptr
= backptr
;
309 * The nasty case is "backptr->next", which we need to
310 * update to "link" _only_ if "backptr" still points
311 * to us (it may not: maybe backptr is a QH->element
312 * pointer and the UHCI has changed the value).
314 me
= virt_to_bus(td
) | (0xe & *backptr
);
315 asm volatile("lock ; cmpxchg %0,%1"
317 :"r" (link
), "m" (*backptr
), "a" (me
)
320 /* Reset it just in case */
321 td
->link
= UHCI_PTR_TERM
;
325 * Only the USB core should call uhci_alloc_dev and uhci_free_dev
327 static int uhci_alloc_dev(struct usb_device
*usb_dev
)
329 struct uhci_device
*dev
;
331 /* Allocate the UHCI device private data */
332 dev
= kmalloc(sizeof(*dev
), GFP_KERNEL
);
336 /* Initialize "dev" */
337 memset(dev
, 0, sizeof(*dev
));
339 usb_dev
->hcpriv
= dev
;
341 atomic_set(&dev
->refcnt
, 1);
344 dev
->uhci
= usb_to_uhci(usb_dev
->parent
)->uhci
;
349 static int uhci_free_dev(struct usb_device
*usb_dev
)
351 struct uhci_device
*dev
= usb_to_uhci(usb_dev
);
353 if (atomic_dec_and_test(&dev
->refcnt
))
359 static void uhci_inc_dev_use(struct uhci_device
*dev
)
361 atomic_inc(&dev
->refcnt
);
364 static void uhci_dec_dev_use(struct uhci_device
*dev
)
366 uhci_free_dev(dev
->usb
);
369 static struct uhci_td
*uhci_td_alloc(struct uhci_device
*dev
)
373 td
= kmem_cache_alloc(uhci_td_cachep
, SLAB_KERNEL
);
378 if ((__u32
)td
& UHCI_PTR_BITS
)
379 printk("qh not 16 byte aligned!\n");
382 td
->link
= UHCI_PTR_TERM
;
390 INIT_LIST_HEAD(&td
->irq_list
);
391 atomic_set(&td
->refcnt
, 1);
393 uhci_inc_dev_use(dev
);
398 static void uhci_td_free(struct uhci_td
*td
)
400 if (atomic_dec_and_test(&td
->refcnt
)) {
401 kmem_cache_free(uhci_td_cachep
, td
);
404 uhci_dec_dev_use(td
->dev
);
408 static struct uhci_qh
*uhci_qh_alloc(struct uhci_device
*dev
)
412 qh
= kmem_cache_alloc(uhci_qh_cachep
, SLAB_KERNEL
);
417 if ((__u32
)qh
& UHCI_PTR_BITS
)
418 printk("qh not 16 byte aligned!\n");
421 qh
->element
= UHCI_PTR_TERM
;
422 qh
->link
= UHCI_PTR_TERM
;
427 init_waitqueue_head(&qh
->wakeup
);
428 atomic_set(&qh
->refcnt
, 1);
430 uhci_inc_dev_use(dev
);
435 static void uhci_qh_free(struct uhci_qh
*qh
)
437 if (atomic_dec_and_test(&qh
->refcnt
)) {
438 kmem_cache_free(uhci_qh_cachep
, qh
);
441 uhci_dec_dev_use(qh
->dev
);
446 * UHCI interrupt list operations..
448 static spinlock_t irqlist_lock
= SPIN_LOCK_UNLOCKED
;
450 static void uhci_add_irq_list(struct uhci
*uhci
, struct uhci_td
*td
, usb_device_irq completed
, void *dev_id
)
454 td
->completed
= completed
;
457 spin_lock_irqsave(&irqlist_lock
, flags
);
458 list_add(&td
->irq_list
, &uhci
->interrupt_list
);
459 spin_unlock_irqrestore(&irqlist_lock
, flags
);
462 static void uhci_remove_irq_list(struct uhci_td
*td
)
466 spin_lock_irqsave(&irqlist_lock
, flags
);
467 list_del(&td
->irq_list
);
468 spin_unlock_irqrestore(&irqlist_lock
, flags
);
472 * frame list manipulation. Used for Isochronous transfers.
473 * the list of (iso) TD's enqueued in a frame list entry
474 * is basically a doubly linked list with link being
475 * the forward pointer and backptr the backward ptr.
476 * the frame list entry itself doesn't have a back ptr
477 * (therefore the list is not circular), and the forward pointer
478 * stops at link entries having the UHCI_PTR_TERM or the UHCI_PTR_QH
479 * bit set. Maybe it could be extended to handle the QH's also,
480 * but it doesn't seem necessary right now.
481 * The layout looks as follows:
482 * frame list pointer -> iso td's (if any) ->
483 * periodic interrupt td (if framelist 0) -> irq qh -> control qh -> bulk qh
486 static spinlock_t framelist_lock
= SPIN_LOCK_UNLOCKED
;
488 static void uhci_add_frame_list(struct uhci
*uhci
, struct uhci_td
*td
, unsigned framenum
)
491 struct uhci_td
*nexttd
;
493 framenum
%= UHCI_NUMFRAMES
;
494 spin_lock_irqsave(&framelist_lock
, flags
);
495 td
->backptr
= &uhci
->fl
->frame
[framenum
];
496 td
->link
= uhci
->fl
->frame
[framenum
];
497 if (!(td
->link
& (UHCI_PTR_TERM
| UHCI_PTR_QH
))) {
498 nexttd
= (struct uhci_td
*)bus_to_virt(td
->link
& ~15);
499 nexttd
->backptr
= &td
->link
;
502 uhci
->fl
->frame
[framenum
] = virt_to_bus(td
);
503 spin_unlock_irqrestore(&framelist_lock
, flags
);
506 static void uhci_remove_frame_list(struct uhci
*uhci
, struct uhci_td
*td
)
509 struct uhci_td
*nexttd
;
513 spin_lock_irqsave(&framelist_lock
, flags
);
514 *(td
->backptr
) = td
->link
;
515 if (!(td
->link
& (UHCI_PTR_TERM
| UHCI_PTR_QH
))) {
516 nexttd
= (struct uhci_td
*)bus_to_virt(td
->link
& ~15);
517 nexttd
->backptr
= td
->backptr
;
519 spin_unlock_irqrestore(&framelist_lock
, flags
);
522 * attention: td->link might still be in use by the
523 * hardware if the td is still active and the hardware
524 * was processing it. So td->link should be preserved
525 * until the frame number changes. Don't know what to do...
526 * udelay(1000) doesn't sound nice, and schedule()
527 * can't be used as this is called from within interrupt context.
529 /* for now warn if there's a possible problem */
530 if (td
->status
& TD_CTRL_ACTIVE
) {
531 unsigned frn
= inw(uhci
->io_addr
+ USBFRNUM
);
532 __u32 link
= uhci
->fl
->frame
[frn
% UHCI_NUMFRAMES
];
533 if (!(link
& (UHCI_PTR_TERM
| UHCI_PTR_QH
))) {
534 struct uhci_td
*tdl
= (struct uhci_td
*)bus_to_virt(link
& ~15);
537 printk(KERN_WARNING
"uhci_remove_frame_list: td possibly still in use!!\n");
540 if (tdl
->link
& (UHCI_PTR_TERM
| UHCI_PTR_QH
))
542 tdl
= (struct uhci_td
*)bus_to_virt(tdl
->link
& ~15);
550 * This function removes and disallocates all structures set up for a transfer.
551 * It takes the qh out of the skeleton, removes the tq and the td's.
552 * It only removes the associated interrupt handler if removeirq is set.
553 * The *td argument is any td in the list of td's.
555 static void uhci_remove_transfer(struct uhci_td
*td
, char removeirq
)
558 struct uhci_td
*curtd
;
559 unsigned int nextlink
;
561 if (td
->qh
&& td
->qh
->first
)
562 curtd
= td
->qh
->first
;
566 /* Remove it from the skeleton */
567 uhci_remove_qh(td
->qh
->skel
, td
->qh
);
568 uhci_qh_free(td
->qh
);
570 nextlink
= curtd
->link
;
572 /* IOC? => remove handler */
573 if (removeirq
&& (td
->status
& TD_CTRL_IOC
))
574 uhci_remove_irq_list(td
);
576 uhci_remove_td(curtd
);
578 if (nextlink
& UHCI_PTR_TERM
) /* Tail? */
581 curtd
= bus_to_virt(nextlink
& ~UHCI_PTR_BITS
);
583 printk(KERN_ERR
"runaway td's!?\n");
590 * Request a interrupt handler..
592 * Returns: a "handle pointer" that release_irq can use to stop this
593 * interrupt. (It's really a pointer to the TD).
595 static void *uhci_request_irq(struct usb_device
*usb_dev
, unsigned int pipe
, usb_device_irq handler
, int period
, void *dev_id
)
597 struct uhci_device
*dev
= usb_to_uhci(usb_dev
);
598 struct uhci_td
*td
= uhci_td_alloc(dev
);
599 struct uhci_qh
*qh
= uhci_qh_alloc(dev
);
600 unsigned int destination
, status
;
605 /* Destination: pipe destination with INPUT */
606 destination
= (pipe
& PIPE_DEVEP_MASK
) | usb_packetid(pipe
);
608 /* Infinite errors is 0, so no bits */
609 status
= (pipe
& TD_CTRL_LS
) | TD_CTRL_IOC
| TD_CTRL_ACTIVE
|
612 td
->link
= UHCI_PTR_TERM
; /* Terminate */
613 td
->status
= status
; /* In */
614 td
->info
= destination
| ((usb_maxpacket(usb_dev
, pipe
, usb_pipeout(pipe
)) - 1) << 21) |
615 (usb_gettoggle(usb_dev
, usb_pipeendpoint(pipe
), usb_pipeout(pipe
)) << TD_TOKEN_TOGGLE
);
617 td
->buffer
= virt_to_bus(dev
->data
);
621 /* if period 0, insert into fast q */
623 td
->flags
|= UHCI_TD_REMOVE
;
624 qh
->skel
= &dev
->uhci
->skel_int2_qh
;
626 qh
->skel
= &dev
->uhci
->skel_int8_qh
;
628 uhci_add_irq_list(dev
->uhci
, td
, handler
, dev_id
);
630 uhci_insert_td_in_qh(qh
, td
);
632 /* Add it into the skeleton */
633 uhci_insert_qh(qh
->skel
, qh
);
639 * Release an interrupt handler previously allocated using
640 * uhci_request_irq. This function does no validity checking, so make
641 * sure you're not releasing an already released handle as it may be
642 * in use by something else..
644 * This function can NOT be called from an interrupt.
646 int uhci_release_irq(struct usb_device
*usb
, void *handle
)
651 td
= (struct uhci_td
*)handle
;
653 return USB_ST_INTERNALERROR
;
655 /* Remove it from the internal irq_list */
656 uhci_remove_irq_list(td
);
658 /* Remove the interrupt TD and QH */
661 uhci_remove_qh(qh
->skel
, qh
);
663 if (td
->completed
!= NULL
)
664 td
->completed(USB_ST_REMOVED
, NULL
, 0, td
->dev_id
);
666 /* Free the TD and QH */
670 return USB_ST_NOERROR
;
671 } /* uhci_release_irq() */
674 * uhci_get_current_frame_number()
676 * returns the current frame number for a USB bus/controller.
678 static int uhci_get_current_frame_number(struct usb_device
*usb_dev
)
680 return inw (usb_to_uhci(usb_dev
)->uhci
->io_addr
+ USBFRNUM
);
687 * Checks bus bandwidth allocation for this USB bus.
688 * Allocates some data structures.
689 * Initializes parts of them from the function parameters.
691 * It does not associate any data/buffer pointers or
692 * driver (caller) callback functions with the allocated
693 * data structures. Such associations are left until
696 * Returns 0 for success or negative value for error.
697 * Sets isocdesc before successful return.
699 static int uhci_init_isoc (struct usb_device
*usb_dev
,
701 int frame_count
, /* bandwidth % = 100 * this / 1000 */
703 struct usb_isoc_desc
**isocdesc
)
705 struct usb_isoc_desc
*id
;
708 #ifdef BANDWIDTH_ALLOCATION
709 /* TBD: add bandwidth allocation/checking/management HERE. */
710 /* TBD: some way to factor in frame_spacing ??? */
715 /* Check some parameters. */
716 if ((frame_count
<= 0) || (frame_count
> UHCI_NUMFRAMES
)) {
717 #ifdef CONFIG_USB_DEBUG_ISOC
718 printk (KERN_DEBUG
"uhci_init_isoc: invalid frame_count (%d)\n",
724 if (!usb_pipeisoc (pipe
)) {
725 #ifdef CONFIG_USB_DEBUG_ISOC
726 printk (KERN_DEBUG
"uhci_init_isoc: NOT an Isoc. pipe\n");
731 id
= kmalloc (sizeof (*id
) +
732 (sizeof (struct isoc_frame_desc
) * frame_count
), GFP_KERNEL
);
736 memset (id
, 0, sizeof (*id
) +
737 (sizeof (struct isoc_frame_desc
) * frame_count
));
739 id
->td
= kmalloc (sizeof (struct uhci_td
) * frame_count
, GFP_KERNEL
);
745 memset (id
->td
, 0, sizeof (struct uhci_td
) * frame_count
);
747 for (i
= 0; i
< frame_count
; i
++)
748 INIT_LIST_HEAD(&((struct uhci_td
*)(id
->td
))[i
].irq_list
);
750 id
->frame_count
= frame_count
;
751 id
->frame_size
= usb_maxpacket (usb_dev
, pipe
, usb_pipeout(pipe
));
752 /* TBD: or make this a parameter to allow for frame_size
753 that is less than maxpacketsize */
754 id
->start_frame
= -1;
756 id
->usb_dev
= usb_dev
;
758 id
->context
= context
;
762 } /* end uhci_init_isoc */
767 * Associates data/buffer pointers/lengths and
768 * driver (caller) callback functions with the
769 * allocated Isoc. data structures and TDs.
771 * Then inserts the TDs into the USB controller frame list
772 * for its processing.
773 * And inserts the callback function into its TD.
775 * pr_isocdesc (previous Isoc. desc.) may be NULL.
776 * It is used only for chaining one list of TDs onto the
777 * end of the previous list of TDs.
779 * Returns 0 (success) or error code (negative value).
781 static int uhci_run_isoc (struct usb_isoc_desc
*isocdesc
,
782 struct usb_isoc_desc
*pr_isocdesc
)
784 struct uhci_device
*dev
= usb_to_uhci (isocdesc
->usb_dev
);
785 struct uhci
*uhci
= dev
->uhci
;
786 unsigned long destination
, status
;
788 int ix
, cur_frame
, pipeinput
, frlen
;
790 struct isoc_frame_desc
*fd
;
791 unsigned char *bufptr
;
793 if (!isocdesc
->callback_fn
) {
794 #ifdef CONFIG_USB_DEBUG_ISOC
795 printk (KERN_DEBUG
"uhci_run_isoc: caller must have a callback function\n");
800 /* Check buffer size large enough for maxpacketsize * frame_count. */
801 if (isocdesc
->buf_size
< (isocdesc
->frame_count
* isocdesc
->frame_size
)) {
802 #ifdef CONFIG_USB_DEBUG_ISOC
803 printk (KERN_DEBUG
"uhci_init_isoc: buf_size too small (%d < %d)\n",
804 isocdesc
->buf_size
, isocdesc
->frame_count
* isocdesc
->frame_size
);
809 /* Check buffer ptr for Null. */
810 if (!isocdesc
->data
) {
811 #ifdef CONFIG_USB_DEBUG_ISOC
812 printk (KERN_DEBUG
"uhci_init_isoc: data ptr is null\n");
817 #ifdef NEED_ALIGNMENT
818 /* Check data page alignment. */
819 if (((int)(isocdesc
->data
) & (PAGE_SIZE
- 1)) != 0) {
820 #ifdef CONFIG_USB_DEBUG_ISOC
821 printk (KERN_DEBUG
"uhci_init_isoc: buffer must be page-aligned (%p)\n",
826 #endif /* NEED_ALIGNMENT */
829 * Check start_type unless pr_isocdesc is used.
831 if (!pr_isocdesc
&& (isocdesc
->start_type
> START_TYPE_MAX
)) {
832 #ifdef CONFIG_USB_DEBUG_ISOC
833 printk (KERN_DEBUG
"uhci_run_isoc: invalid start_type (%d)\n",
834 isocdesc
->start_type
);
840 * Check start_frame for inside a valid range.
841 * Only allow transfer requests to be made 1.000 second
844 cur_frame
= uhci_get_current_frame_number (isocdesc
->usb_dev
);
846 /* if not START_ASAP (i.e., RELATIVE or ABSOLUTE): */
848 if (isocdesc
->start_type
== START_RELATIVE
) {
849 if ((isocdesc
->start_frame
< 0) || (isocdesc
->start_frame
> CAN_SCHEDULE_FRAMES
)) {
850 #ifdef CONFIG_USB_DEBUG_ISOC
851 printk (KERN_DEBUG
"uhci_init_isoc: bad start_frame value (%d)\n",
852 isocdesc
->start_frame
);
856 } /* end START_RELATIVE */
858 if (isocdesc
->start_type
== START_ABSOLUTE
) { /* within the scope of cur_frame */
859 ix
= USB_WRAP_FRAMENR(isocdesc
->start_frame
- cur_frame
);
860 if (ix
< START_FRAME_FUDGE
|| /* too small */
861 ix
> CAN_SCHEDULE_FRAMES
) { /* too large */
862 #ifdef CONFIG_USB_DEBUG_ISOC
863 printk (KERN_DEBUG
"uhci_init_isoc: bad start_frame value (%d,%d)\n",
864 isocdesc
->start_frame
, cur_frame
);
868 } /* end START_ABSOLUTE */
872 * Set the start/end frame numbers.
875 isocdesc
->start_frame
= pr_isocdesc
->end_frame
+ 1;
876 } else if (isocdesc
->start_type
== START_RELATIVE
) {
877 if (isocdesc
->start_frame
< START_FRAME_FUDGE
)
878 isocdesc
->start_frame
= START_FRAME_FUDGE
;
879 isocdesc
->start_frame
+= cur_frame
;
880 } else if (isocdesc
->start_type
== START_ASAP
) {
881 isocdesc
->start_frame
= cur_frame
+ START_FRAME_FUDGE
;
884 /* and see if start_frame needs any correction */
885 /* only wrap to USB frame numbers, the frame_list insertion routine
886 takes care of the wrapping to the frame_list size */
887 isocdesc
->start_frame
= USB_WRAP_FRAMENR(isocdesc
->start_frame
);
889 /* and fix the end_frame value */
890 isocdesc
->end_frame
= USB_WRAP_FRAMENR(isocdesc
->start_frame
+ isocdesc
->frame_count
- 1);
892 isocdesc
->prev_completed_frame
= -1;
893 isocdesc
->cur_completed_frame
= -1;
895 destination
= (isocdesc
->pipe
& PIPE_DEVEP_MASK
) |
896 usb_packetid (isocdesc
->pipe
);
897 status
= TD_CTRL_ACTIVE
| TD_CTRL_IOS
; /* mark Isoc.; can't be low speed */
898 pipeinput
= usb_pipein (isocdesc
->pipe
);
899 cur_frame
= isocdesc
->start_frame
;
900 bufptr
= isocdesc
->data
;
903 * Build the Data TDs.
904 * TBD: Not using frame_spacing (Yet). Defaults to 1 (every frame).
905 * (frame_spacing is a way to request less bandwidth.)
906 * This can also be done by using frame_length = 0 in the
907 * frame_desc array, but this way won't take less bandwidth
908 * allocation into account.
911 if (isocdesc
->frame_spacing
<= 0)
912 isocdesc
->frame_spacing
= 1;
914 for (ix
= 0, td
= isocdesc
->td
, fd
= isocdesc
->frames
;
915 ix
< isocdesc
->frame_count
; ix
++, td
++, fd
++) {
916 frlen
= fd
->frame_length
;
917 if (frlen
> isocdesc
->frame_size
)
918 frlen
= isocdesc
->frame_size
;
921 td
->info
= destination
| /* use Actual len on OUT; max. on IN */
922 (pipeinput
? ((isocdesc
->frame_size
- 1) << 21)
923 : ((frlen
- 1) << 21));
926 td
->dev_id
= isocdesc
; /* can get dev_id or context from isocdesc */
928 td
->info
= destination
| ((frlen
- 1) << 21);
929 td
->buffer
= virt_to_bus (bufptr
);
931 td
->isoc_td_number
= ix
; /* 0-based; does not wrap/overflow back to 0 */
933 if (isocdesc
->callback_frames
&&
934 (++cb_frames
>= isocdesc
->callback_frames
)) {
935 td
->status
|= TD_CTRL_IOC
;
936 td
->completed
= isocdesc
->callback_fn
;
938 uhci_add_irq_list (dev
->uhci
, td
, isocdesc
->callback_fn
, isocdesc
->context
);
941 bufptr
+= fd
->frame_length
; /* or isocdesc->frame_size; */
944 * Insert the TD in the frame list.
946 uhci_add_frame_list(uhci
, td
, cur_frame
);
948 cur_frame
= USB_WRAP_FRAMENR(cur_frame
+1);
952 * Add IOC on the last TD.
955 if (!(td
->status
& TD_CTRL_IOC
)) {
956 td
->status
|= TD_CTRL_IOC
;
957 td
->completed
= isocdesc
->callback_fn
;
958 uhci_add_irq_list(dev
->uhci
, td
, isocdesc
->callback_fn
, isocdesc
->context
); /* TBD: D.K. ??? */
961 } /* end uhci_run_isoc */
966 * Marks a TD list as Inactive and removes it from the Isoc.
969 * Does not free any memory resources.
971 * Returns 0 for success or negative value for error.
973 static int uhci_kill_isoc (struct usb_isoc_desc
*isocdesc
)
975 struct uhci_device
*dev
= usb_to_uhci (isocdesc
->usb_dev
);
976 struct uhci
*uhci
= dev
->uhci
;
980 if (USB_WRAP_FRAMENR(isocdesc
->start_frame
) != isocdesc
->start_frame
) {
981 #ifdef CONFIG_USB_DEBUG_ISOC
982 printk (KERN_DEBUG
"uhci_kill_isoc: invalid start_frame (%d)\n",
983 isocdesc
->start_frame
);
988 for (ix
= 0, td
= isocdesc
->td
; ix
< isocdesc
->frame_count
; ix
++, td
++) {
989 uhci_remove_frame_list(uhci
, td
);
990 td
->status
&= ~(TD_CTRL_ACTIVE
| TD_CTRL_IOC
);
993 isocdesc
->start_frame
= -1;
995 } /* end uhci_kill_isoc */
997 static void uhci_free_isoc (struct usb_isoc_desc
*isocdesc
)
1001 /* If still Active, kill it. */
1002 if (isocdesc
->start_frame
>= 0)
1003 uhci_kill_isoc(isocdesc
);
1005 /* Remove all td's from the IRQ list. */
1006 for(i
= 0; i
< isocdesc
->frame_count
; i
++)
1007 uhci_remove_irq_list(((struct uhci_td
*)(isocdesc
->td
))+i
);
1009 /* Free the associate memory. */
1011 kfree(isocdesc
->td
);
1014 } /* end uhci_free_isoc */
1017 * Control thread operations: we just mark the last TD
1018 * in a control thread as an interrupt TD, and wake up
1019 * the front-end on completion.
1021 * We need to remove the TD from the lists (both interrupt
1022 * list and TD lists) by hand if something bad happens!
1025 static int uhci_generic_completed(int status
, void *buffer
, int len
, void *dev_id
)
1027 wait_queue_head_t
*wakeup
= (wait_queue_head_t
*)dev_id
;
1029 if (waitqueue_active(wakeup
))
1032 printk("waitqueue empty!\n");
1034 return 0; /* Don't re-instate */
1037 /* td points to the last td in the list, which interrupts on completion */
1038 static int uhci_run_control(struct uhci_device
*dev
, struct uhci_td
*first
, struct uhci_td
*last
)
1040 DECLARE_WAITQUEUE(wait
, current
);
1041 struct uhci_qh
*qh
= uhci_qh_alloc(dev
);
1046 current
->state
= TASK_UNINTERRUPTIBLE
;
1047 add_wait_queue(&qh
->wakeup
, &wait
);
1049 uhci_add_irq_list(dev
->uhci
, last
, uhci_generic_completed
, &qh
->wakeup
);
1051 uhci_insert_tds_in_qh(qh
, first
, last
);
1053 /* Add it into the skeleton */
1054 uhci_insert_qh(&dev
->uhci
->skel_control_qh
, qh
);
1056 schedule_timeout(HZ
* 5); /* 5 seconds */
1058 remove_wait_queue(&qh
->wakeup
, &wait
);
1060 /* Clean up in case it failed.. */
1061 uhci_remove_irq_list(last
);
1063 /* Remove it from the skeleton */
1064 uhci_remove_qh(&dev
->uhci
->skel_control_qh
, qh
);
1068 return uhci_td_result(dev
, last
, NULL
, 1);
1072 * Send or receive a control message on a pipe.
1074 * Note that the "pipe" structure is set up to map
1075 * easily to the uhci destination fields.
1077 * A control message is built up from three parts:
1078 * - The command itself
1079 * - [ optional ] data phase
1080 * - Status complete phase
1082 * The data phase can be an arbitrary number of TD's
1083 * although we currently had better not have more than
1084 * 29 TD's here (we have 31 TD's allocated for control
1085 * operations, and two of them are used for command and
1088 * 29 TD's is a minimum of 232 bytes worth of control
1089 * information, that's just ridiculously high. Most
1090 * control messages have just a few bytes of data.
1092 * 232 is not ridiculously high with many of the
1093 * configurations on audio devices, etc. anyway,
1094 * there is no restriction on length of transfers
1097 static int uhci_control_msg(struct usb_device
*usb_dev
, unsigned int pipe
, devrequest
*cmd
, void *data
, int len
)
1099 struct uhci_device
*dev
= usb_to_uhci(usb_dev
);
1100 struct uhci_td
*first
, *td
, *prevtd
;
1101 unsigned long destination
, status
;
1103 int maxsze
= usb_maxpacket(usb_dev
, pipe
, usb_pipeout(pipe
));
1105 unsigned long bytesrequested
= len
;
1106 unsigned long bytesread
= 0;
1108 first
= td
= uhci_td_alloc(dev
);
1112 /* The "pipe" thing contains the destination in bits 8--18 */
1113 destination
= (pipe
& PIPE_DEVEP_MASK
) | USB_PID_SETUP
;
1116 status
= (pipe
& TD_CTRL_LS
) | TD_CTRL_ACTIVE
| TD_CTRL_SPD
| (3 << 27);
1119 * Build the TD for the control request
1121 td
->status
= status
; /* Try forever */
1122 td
->info
= destination
| (7 << 21); /* 8 bytes of data */
1123 td
->buffer
= virt_to_bus(cmd
);
1126 * If direction is "send", change the frame from SETUP (0x2D)
1127 * to OUT (0xE1). Else change it from SETUP to IN (0x69).
1129 destination
^= (USB_PID_SETUP
^ USB_PID_IN
); /* SETUP -> IN */
1130 if (usb_pipeout(pipe
))
1131 destination
^= (USB_PID_IN
^ USB_PID_OUT
); /* IN -> OUT */
1134 td
= uhci_td_alloc(dev
);
1138 prevtd
->link
= virt_to_bus(td
) | UHCI_PTR_DEPTH
;
1141 * Build the DATA TD's
1144 /* Build the TD for control status */
1147 if (pktsze
> maxsze
)
1150 /* Alternate Data0/1 (start with Data1) */
1151 destination
^= 1 << TD_TOKEN_TOGGLE
;
1153 td
->status
= status
; /* Status */
1154 td
->info
= destination
| ((pktsze
- 1) << 21); /* pktsze bytes of data */
1155 td
->buffer
= virt_to_bus(data
);
1156 td
->backptr
= &prevtd
->link
;
1162 td
= uhci_td_alloc(dev
);
1165 prevtd
->link
= virt_to_bus(td
) | UHCI_PTR_DEPTH
; /* Update previous TD */
1169 * Build the final TD for control status
1171 /* It's only IN if the pipe is out AND we aren't expecting data */
1172 destination
&= ~0xFF;
1173 if (usb_pipeout(pipe
) | (bytesrequested
== 0))
1174 destination
|= USB_PID_IN
;
1176 destination
|= USB_PID_OUT
;
1178 destination
|= 1 << TD_TOKEN_TOGGLE
; /* End in Data1 */
1180 td
->status
= status
| TD_CTRL_IOC
; /* no limit on errors on final packet */
1181 td
->info
= destination
| (UHCI_NULL_DATA_SIZE
<< 21); /* 0 bytes of data */
1183 td
->backptr
= &prevtd
->link
;
1184 td
->link
= UHCI_PTR_TERM
; /* Terminate */
1187 ret
= uhci_run_control(dev
, first
, td
);
1192 if (!uhci_status_bits(td
->status
) && ((td
->info
& 0xFF) ==
1194 bytesread
+= uhci_actual_length(td
->status
);
1196 nextlink
= td
->link
;
1200 if (nextlink
& UHCI_PTR_TERM
) /* Tail? */
1203 td
= uhci_ptr_to_virt(nextlink
);
1207 printk(KERN_ERR
"runaway td's!?\n");
1209 if (ret
&& (bytesread
>= bytesrequested
)) {
1210 printk(KERN_DEBUG
"Recovered sufficient data (asked for %ld, got %ld) from failed cmd\n",
1211 bytesrequested
, bytesread
);
1215 if (uhci_debug
&& ret
) {
1216 __u8
*p
= (__u8
*)cmd
;
1218 printk(KERN_DEBUG
"Failed cmd - %02X %02X %02X %02X %02X %02X %02X %02X\n",
1219 p
[0], p
[1], p
[2], p
[3], p
[4], p
[5], p
[6], p
[7]);
1225 * Bulk thread operations: we just mark the last TD
1226 * in a bulk thread as an interrupt TD, and wake up
1227 * the front-end on completion.
1229 * We need to remove the TD from the lists (both interrupt
1230 * list and TD lists) by hand if something bad happens!
1233 /* td points to the last td in the list, which interrupts on completion */
1234 static int uhci_run_bulk(struct uhci_device
*dev
, struct uhci_td
*first
, struct uhci_td
*last
, unsigned long *rval
)
1236 DECLARE_WAITQUEUE(wait
, current
);
1237 struct uhci_qh
*qh
= uhci_qh_alloc(dev
);
1242 current
->state
= TASK_UNINTERRUPTIBLE
;
1243 add_wait_queue(&qh
->wakeup
, &wait
);
1245 uhci_add_irq_list(dev
->uhci
, last
, uhci_generic_completed
, &qh
->wakeup
);
1247 uhci_insert_tds_in_qh(qh
, first
, last
);
1249 /* Add it into the skeleton */
1250 uhci_insert_qh(&dev
->uhci
->skel_bulk_qh
, qh
);
1252 schedule_timeout(HZ
* 5); /* 5 seconds */
1254 remove_wait_queue(&qh
->wakeup
, &wait
);
1256 /* Clean up in case it failed.. */
1257 uhci_remove_irq_list(last
);
1259 uhci_remove_qh(&dev
->uhci
->skel_bulk_qh
, qh
);
1263 return uhci_td_result(dev
, last
, rval
, 1);
1267 * Send or receive a bulk message on a pipe.
1269 * Note that the "pipe" structure is set up to map
1270 * easily to the uhci destination fields.
1272 * A bulk message is only built up from
1275 static int uhci_bulk_msg(struct usb_device
*usb_dev
, unsigned int pipe
, void *data
, int len
, unsigned long *rval
)
1277 struct uhci_device
*dev
= usb_to_uhci(usb_dev
);
1278 struct uhci_td
*first
, *td
, *prevtd
;
1279 unsigned long destination
, status
;
1281 int maxsze
= usb_maxpacket(usb_dev
, pipe
, usb_pipeout(pipe
));
1283 if (usb_endpoint_halted(usb_dev
, usb_pipeendpoint(pipe
), usb_pipeout(pipe
)) &&
1284 usb_clear_halt(usb_dev
, usb_pipeendpoint(pipe
) | (pipe
& USB_DIR_IN
)))
1285 return USB_ST_STALL
;
1287 /* The "pipe" thing contains the destination in bits 8--18. */
1288 destination
= (pipe
& PIPE_DEVEP_MASK
) | usb_packetid (pipe
);
1291 status
= (pipe
& TD_CTRL_LS
) | TD_CTRL_ACTIVE
| TD_CTRL_SPD
| (3 << 27);
1294 * Build the TDs for the bulk request
1296 first
= td
= uhci_td_alloc(dev
);
1300 prevtd
= first
; //This is fake, but at least it's not NULL
1302 /* Build the TD for control status */
1305 if (pktsze
> maxsze
)
1308 td
->status
= status
; /* Status */
1309 td
->info
= destination
| ((pktsze
-1) << 21) |
1310 (usb_gettoggle(usb_dev
, usb_pipeendpoint(pipe
),
1311 usb_pipeout(pipe
)) << TD_TOKEN_TOGGLE
); /* pktsze bytes of data */
1312 td
->buffer
= virt_to_bus(data
);
1313 td
->backptr
= &prevtd
->link
;
1320 td
= uhci_td_alloc(dev
);
1324 prevtd
->link
= virt_to_bus(td
) | UHCI_PTR_DEPTH
;/* Update previous TD */
1327 /* Alternate Data0/1 (start with Data0) */
1328 usb_dotoggle(usb_dev
, usb_pipeendpoint(pipe
), usb_pipeout(pipe
));
1331 td
->link
= 1; /* Terminate */
1332 td
->status
|= TD_CTRL_IOC
;
1334 /* CHANGE DIRECTION HERE! SAVE IT SOMEWHERE IN THE ENDPOINT!!! */
1337 ret
= uhci_run_bulk(dev
, first
, td
, rval
);
1341 struct uhci_td
*curtd
= first
;
1342 unsigned int nextlink
;
1345 nextlink
= curtd
->link
;
1346 uhci_remove_td(curtd
);
1347 uhci_td_free(curtd
);
1349 if (nextlink
& UHCI_PTR_TERM
) /* Tail? */
1352 curtd
= uhci_ptr_to_virt(nextlink
);
1356 printk(KERN_DEBUG
"runaway td's!?\n");
1362 static void * uhci_request_bulk(struct usb_device
*usb_dev
, unsigned int pipe
, usb_device_irq handler
, void *data
, int len
, void *dev_id
)
1364 struct uhci_device
*dev
= usb_to_uhci(usb_dev
);
1365 struct uhci
*uhci
= dev
->uhci
;
1366 struct uhci_td
*first
, *td
, *prevtd
;
1367 struct uhci_qh
*bulk_qh
= uhci_qh_alloc(dev
);
1368 unsigned long destination
, status
;
1369 int maxsze
= usb_maxpacket(usb_dev
, pipe
, usb_pipeout(pipe
));
1371 /* The "pipe" thing contains the destination in bits 8--18. */
1372 destination
= (pipe
& PIPE_DEVEP_MASK
) | usb_packetid(pipe
);
1374 /* Infinite errors is 0 */
1375 status
= (pipe
& TD_CTRL_LS
) | TD_CTRL_ACTIVE
| TD_CTRL_SPD
;
1377 /* Build the TDs for the bulk request */
1378 first
= td
= uhci_td_alloc(dev
);
1381 /* Build the TD for control status */
1384 if (pktsze
> maxsze
)
1387 td
->status
= status
; /* Status */
1388 td
->info
= destination
| ((pktsze
-1) << 21) |
1389 (usb_gettoggle(usb_dev
, usb_pipeendpoint(pipe
), usb_pipeout(pipe
)) << TD_TOKEN_TOGGLE
); /* pktsze bytes of data */
1390 td
->buffer
= virt_to_bus(data
);
1391 td
->backptr
= &prevtd
->link
;
1399 td
= uhci_td_alloc(dev
);
1400 prevtd
->link
= virt_to_bus(td
) | UHCI_PTR_DEPTH
;
1403 /* Alternate Data0/1 */
1404 usb_dotoggle(usb_dev
, usb_pipeendpoint(pipe
), usb_pipeout(pipe
));
1407 first
->backptr
= NULL
;
1408 td
->link
= 1; /* Terminate */
1409 td
->status
= status
| TD_CTRL_IOC
; /* IOC */
1411 uhci_add_irq_list(dev
->uhci
, td
, handler
, dev_id
);
1413 uhci_insert_tds_in_qh(bulk_qh
, first
, td
);
1415 bulk_qh
->skel
= &uhci
->skel_bulk_qh
;
1416 uhci_insert_qh(&uhci
->skel_bulk_qh
, bulk_qh
);
1418 /* Return last td for removal */
1423 * Remove a handler from a pipe. This terminates the transfer.
1424 * We have some assumptions here:
1425 * There is only one queue using this pipe. (the one we remove)
1426 * Any data that is in the queue is useless for us, we throw it away.
1428 static int uhci_terminate_bulk(struct usb_device
*dev
, void *first
)
1430 /* none found? there is nothing to remove! */
1434 uhci_remove_transfer(first
, 1);
1439 struct usb_operations uhci_device_operations
= {
1447 uhci_terminate_bulk
,
1448 uhci_get_current_frame_number
,
1456 * This is just incredibly fragile. The timings must be just
1457 * right, and they aren't really documented very well.
1459 * Note the short delay between disabling reset and enabling
1462 static void uhci_reset_port(unsigned int port
)
1464 unsigned short status
;
1467 outw(status
| USBPORTSC_PR
, port
); /* reset port */
1469 outw(status
& ~USBPORTSC_PR
, port
);
1473 outw(status
| USBPORTSC_PE
, port
); /* enable port */
1477 if (!(status
& USBPORTSC_PE
)) {
1478 outw(status
| USBPORTSC_PE
, port
); /* one more try at enabling port */
1485 * This gets called if the connect status on the root
1486 * hub (and the root hub only) changes.
1488 static void uhci_connect_change(struct uhci
*uhci
, unsigned int port
, unsigned int nr
)
1490 struct usb_device
*usb_dev
;
1491 struct uhci_device
*dev
;
1492 unsigned short status
;
1493 struct uhci_device
*root_hub
= usb_to_uhci(uhci
->bus
->root_hub
);
1496 printk(KERN_INFO
"uhci_connect_change: called for %d\n", nr
);
1500 * Even if the status says we're connected,
1501 * the fact that the status bits changed may
1502 * that we got disconnected and then reconnected.
1504 * So start off by getting rid of any old devices..
1506 usb_disconnect(&root_hub
->usb
->children
[nr
]);
1510 /* If we have nothing connected, then clear change status and disable the port */
1511 status
= (status
& ~USBPORTSC_PE
) | USBPORTSC_PEC
;
1512 if (!(status
& USBPORTSC_CCS
)) {
1518 * Ok, we got a new connection. Allocate a device to it,
1519 * and find out what it wants to do..
1521 usb_dev
= usb_alloc_dev(root_hub
->usb
, root_hub
->usb
->bus
);
1525 dev
= usb_dev
->hcpriv
;
1527 usb_connect(usb_dev
);
1529 root_hub
->usb
->children
[nr
] = usb_dev
;
1531 wait_ms(200); /* wait for powerup */
1532 uhci_reset_port(port
);
1534 /* Get speed information */
1535 usb_dev
->slow
= (inw(port
) & USBPORTSC_LSDA
) ? 1 : 0;
1538 * Ok, all the stuff specific to the root hub has been done.
1539 * The rest is generic for any new USB attach, regardless of
1542 if (usb_new_device(usb_dev
)) {
1543 unsigned short status
= inw(port
);
1545 printk(KERN_INFO
"uhci: disabling malfunctioning port %d\n",
1547 outw(status
| USBPORTSC_PE
, port
);
1552 * This gets called when the root hub configuration
1553 * has changed. Just go through each port, seeing if
1554 * there is something interesting happening.
1556 static void uhci_check_configuration(struct uhci
*uhci
)
1558 struct uhci_device
*root_hub
= usb_to_uhci(uhci
->bus
->root_hub
);
1559 unsigned int io_addr
= uhci
->io_addr
+ USBPORTSC1
;
1560 int maxchild
= root_hub
->usb
->maxchild
;
1564 unsigned short status
= inw(io_addr
);
1566 if (status
& USBPORTSC_CSC
)
1567 uhci_connect_change(uhci
, io_addr
, nr
);
1570 } while (nr
< maxchild
);
1573 static int fixup_isoc_desc (struct uhci_td
*td
)
1575 struct usb_isoc_desc
*isocdesc
= td
->dev_id
;
1576 struct uhci_td
*prtd
;
1577 struct isoc_frame_desc
*frm
;
1578 int first_comp
= isocdesc
->cur_completed_frame
+ 1; /* 0-based */
1579 int cur_comp
= td
->isoc_td_number
; /* 0-based */
1583 if (first_comp
>= isocdesc
->frame_count
)
1585 num_comp
= cur_comp
- first_comp
+ 1;
1587 #ifdef CONFIG_USB_DEBUG_ISOC
1588 printk ("fixup_isoc_desc.1: td = %p, id = %p, first_comp = %d, cur_comp = %d, num_comp = %d\n",
1589 td
, isocdesc
, first_comp
, cur_comp
, num_comp
);
1592 for (ix
= 0, fx
= first_comp
, prtd
= ((struct uhci_td
*)(isocdesc
->td
))+first_comp
, frm
= &isocdesc
->frames
[first_comp
];
1593 ix
< num_comp
; ix
++) {
1594 frm
->frame_length
= uhci_actual_length (prtd
->status
);
1595 isocdesc
->total_length
+= frm
->frame_length
;
1597 if ((frm
->frame_status
= uhci_map_status (uhci_status_bits (prtd
->status
),
1598 uhci_packetout (prtd
->info
))))
1599 isocdesc
->error_count
++;
1603 if (++fx
>= isocdesc
->frame_count
) { /* wrap fx, prtd, and frm */
1605 prtd
= isocdesc
->td
;
1606 frm
= isocdesc
->frames
;
1611 * Update some other fields for drivers.
1613 isocdesc
->prev_completed_frame
= isocdesc
->cur_completed_frame
;
1614 isocdesc
->cur_completed_frame
= cur_comp
;
1615 isocdesc
->total_completed_frames
+= num_comp
; /* 1-based */
1617 #ifdef CONFIG_USB_DEBUG_ISOC
1618 printk ("fixup_isoc_desc.2: total_comp_frames = %d, total_length = %d, error_count = %d\n",
1619 isocdesc
->total_completed_frames
, isocdesc
->total_length
, isocdesc
->error_count
);
1620 #endif /* CONFIG_USB_DEBUG_ISOC */
1625 static void uhci_interrupt_notify(struct uhci
*uhci
)
1627 struct list_head
*tmp
, *head
= &uhci
->interrupt_list
;
1630 spin_lock(&irqlist_lock
);
1632 while (tmp
!= head
) {
1633 struct uhci_td
*td
= list_entry(tmp
, struct uhci_td
, irq_list
);
1638 /* We're interested if there was an error or if the chain of */
1639 /* TD's completed successfully */
1640 status
= uhci_td_result(td
->dev
, td
, &rval
, 0);
1642 if ((status
== USB_ST_NOERROR
) && (td
->status
& TD_CTRL_ACTIVE
))
1645 /* remove from IRQ list */
1646 list_del(&td
->irq_list
);
1647 INIT_LIST_HEAD(&td
->irq_list
);
1649 if (td
->completed(status
, bus_to_virt(td
->buffer
), rval
,
1651 struct usb_device
*usb_dev
= td
->dev
->usb
;
1653 list_add(&td
->irq_list
, &uhci
->interrupt_list
);
1655 usb_dotoggle(usb_dev
, usb_pipeendpoint(td
->info
), usb_pipeout(td
->info
) ^ 1);
1656 td
->info
&= ~(1 << 19); /* clear data toggle */
1657 td
->info
|= usb_gettoggle(usb_dev
, usb_pipeendpoint(td
->info
),
1658 uhci_packetout(td
->info
)) << 19; /* toggle between data0 and data1 */
1659 td
->status
= (td
->status
& 0x2F000000) | TD_CTRL_ACTIVE
| TD_CTRL_IOC
;
1660 /* The HC only removes it when it completed */
1661 /* successfully, so force remove and readd it */
1663 uhci_insert_td_in_qh(td
->qh
, td
);
1664 } else if (td
->flags
& UHCI_TD_REMOVE
) {
1665 struct usb_device
*usb_dev
= td
->dev
->usb
;
1667 /* marked for removal */
1668 td
->flags
&= ~UHCI_TD_REMOVE
;
1669 usb_dotoggle(usb_dev
, usb_pipeendpoint(td
->info
), uhci_packetout(td
->info
));
1670 uhci_remove_qh(td
->qh
->skel
, td
->qh
);
1671 uhci_qh_free(td
->qh
);
1675 /* If completed does not wants to reactivate, then */
1676 /* it's responsible for free'ing the TD's and QH's */
1677 /* or another function (such as run_control) */
1679 spin_unlock(&irqlist_lock
);
1683 * Check port status - Connect Status Change - for
1684 * each of the attached ports (defaults to two ports,
1685 * but at least in theory there can be more of them).
1687 * Wake up the configurator if something happened, we
1688 * can't really do much at interrupt time.
1690 static void uhci_root_hub_events(struct uhci
*uhci
, unsigned int io_addr
)
1692 if (waitqueue_active(&uhci_configure
)) {
1693 struct uhci_device
*root_hub
= usb_to_uhci(uhci
->bus
->root_hub
);
1694 int ports
= root_hub
->usb
->maxchild
;
1696 io_addr
+= USBPORTSC1
;
1698 if (inw(io_addr
) & USBPORTSC_CSC
) {
1699 wake_up(&uhci_configure
);
1703 } while (--ports
> 0);
1707 static void uhci_interrupt(int irq
, void *__uhci
, struct pt_regs
*regs
)
1709 struct uhci
*uhci
= __uhci
;
1710 unsigned int io_addr
= uhci
->io_addr
;
1711 unsigned short status
;
1714 * Read the interrupt status, and write it back to clear the
1717 status
= inw(io_addr
+ USBSTS
);
1718 outw(status
, io_addr
+ USBSTS
);
1720 /* Walk the list of pending TD's to see which ones completed.. */
1721 uhci_interrupt_notify(uhci
);
1723 /* Check if there are any events on the root hub.. */
1724 uhci_root_hub_events(uhci
, io_addr
);
1728 * We init one packet, and mark it just IOC and _not_
1729 * active. Which will result in no actual USB traffic,
1730 * but _will_ result in an interrupt every second.
1732 * Which is exactly what we want.
1734 static void uhci_init_ticktd(struct uhci
*uhci
)
1736 struct uhci_device
*dev
= usb_to_uhci(uhci
->bus
->root_hub
);
1737 struct uhci_td
*td
= uhci_td_alloc(dev
);
1740 printk(KERN_ERR
"unable to allocate ticktd\n");
1744 /* Don't clobber the frame */
1745 td
->link
= uhci
->fl
->frame
[0];
1746 td
->backptr
= &uhci
->fl
->frame
[0];
1747 td
->status
= TD_CTRL_IOC
;
1748 td
->info
= (15 << 21) | (0x7f << 8) | USB_PID_IN
; /* (ignored) input packet, 16 bytes, device 127 */
1752 uhci
->fl
->frame
[0] = virt_to_bus(td
);
1757 static void reset_hc(struct uhci
*uhci
)
1759 unsigned int io_addr
= uhci
->io_addr
;
1761 /* Global reset for 50ms */
1762 outw(USBCMD_GRESET
, io_addr
+ USBCMD
);
1764 outw(0, io_addr
+ USBCMD
);
1768 static void start_hc(struct uhci
*uhci
)
1770 unsigned int io_addr
= uhci
->io_addr
;
1773 uhci_init_ticktd(uhci
);
1776 * Reset the HC - this will force us to get a
1777 * new notification of any already connected
1778 * ports due to the virtual disconnect that it
1781 outw(USBCMD_HCRESET
, io_addr
+ USBCMD
);
1782 while (inw(io_addr
+ USBCMD
) & USBCMD_HCRESET
) {
1784 printk(KERN_ERR
"USBCMD_HCRESET timed out!\n");
1789 /* Turn on all interrupts */
1790 outw(USBINTR_TIMEOUT
| USBINTR_RESUME
| USBINTR_IOC
| USBINTR_SP
, io_addr
+ USBINTR
);
1792 /* Start at frame 0 */
1793 outw(0, io_addr
+ USBFRNUM
);
1794 outl(virt_to_bus(uhci
->fl
), io_addr
+ USBFLBASEADD
);
1796 /* Run and mark it configured with a 64-byte max packet */
1797 outw(USBCMD_RS
| USBCMD_CF
| USBCMD_MAXP
, io_addr
+ USBCMD
);
1801 * Allocate a frame list, and four regular queues.
1803 * The hardware doesn't really know any difference
1804 * in the queues, but the order does matter for the
1805 * protocols higher up. The order is:
1807 * - any isochronous events handled before any
1808 * of the queues. We don't do that here, because
1809 * we'll create the actual TD entries on demand.
1810 * - The first queue is the "interrupt queue".
1811 * - The second queue is the "control queue".
1812 * - The third queue is "bulk data".
1814 * We could certainly have multiple queues of the same
1815 * type, and maybe we should. We could have per-device
1816 * queues, for example. We begin small.
1818 * Queues are dynamically allocated for devices now,
1819 * this code only sets up the skeleton queue
1821 static struct uhci
*alloc_uhci(unsigned int io_addr
, unsigned int io_size
)
1825 struct usb_bus
*bus
;
1826 struct uhci_device
*dev
;
1827 struct usb_device
*usb
;
1829 uhci
= kmalloc(sizeof(*uhci
), GFP_KERNEL
);
1833 memset(uhci
, 0, sizeof(*uhci
));
1836 uhci
->io_addr
= io_addr
;
1837 uhci
->io_size
= io_size
;
1838 INIT_LIST_HEAD(&uhci
->interrupt_list
);
1840 /* We need exactly one page (per UHCI specs), how convenient */
1841 uhci
->fl
= (void *)__get_free_page(GFP_KERNEL
);
1845 bus
= usb_alloc_bus(&uhci_device_operations
);
1853 * Allocate the root_hub
1855 usb
= usb_alloc_dev(NULL
, bus
);
1861 dev
= usb_to_uhci(usb
);
1864 uhci
->bus
->root_hub
= uhci_to_usb(dev
);
1866 /* Initialize the root hub */
1868 /* UHCI specs says devices must have 2 ports, but goes on to say */
1869 /* they may have more but give no way to determine how many they */
1870 /* have, so default to 2 */
1871 /* According to the UHCI spec, Bit 7 is always set to 1. So we try */
1872 /* to use this to our advantage */
1873 for (port
= 0; port
< (io_size
- 0x10) / 2; port
++) {
1874 unsigned int portstatus
;
1876 portstatus
= inw(io_addr
+ 0x10 + (port
* 2));
1877 if (!(portstatus
& 0x0080))
1880 printk(KERN_DEBUG
"Detected %d ports\n", port
);
1882 /* This is experimental so anything less than 2 or greater than 8 is */
1883 /* something weird and we'll ignore it */
1884 if (port
< 2 || port
> 8) {
1885 printk(KERN_DEBUG
"Port count misdetected, forcing to 2 ports\n");
1889 usb
->maxchild
= port
;
1890 usb_init_root_hub(usb
);
1892 /* 8 Interrupt queues */
1893 for (i
= 0; i
< 8; i
++) {
1894 struct uhci_qh
*qh
= &uhci
->skelqh
[i
];
1896 qh
->link
= virt_to_bus(&uhci
->skel_control_qh
) | UHCI_PTR_QH
;
1897 qh
->element
= UHCI_PTR_TERM
;
1900 uhci
->skel_control_qh
.link
= virt_to_bus(&uhci
->skel_bulk_qh
) |
1902 uhci
->skel_control_qh
.element
= UHCI_PTR_TERM
;
1904 uhci
->skel_bulk_qh
.link
= UHCI_PTR_TERM
;
1905 uhci
->skel_bulk_qh
.element
= UHCI_PTR_TERM
;
1908 * Fill the frame list: make all entries point to
1909 * the proper interrupt queue.
1911 * This is probably silly, but it's a simple way to
1912 * scatter the interrupt queues in a way that gives
1913 * us a reasonable dynamic range for irq latencies.
1915 for (i
= 0; i
< 1024; i
++) {
1916 struct uhci_qh
*irq
= &uhci
->skel_int2_qh
;
1938 uhci
->fl
->frame
[i
] = virt_to_bus(irq
) | UHCI_PTR_QH
;
1950 free_page((unsigned long)uhci
->fl
);
1957 * De-allocate all resources..
1959 static void release_uhci(struct uhci
*uhci
)
1961 if (uhci
->irq
>= 0) {
1962 free_irq(uhci
->irq
, uhci
);
1967 uhci_td_free(uhci
->ticktd
);
1968 uhci
->ticktd
= NULL
;
1972 free_page((unsigned long)uhci
->fl
);
1976 usb_free_bus(uhci
->bus
);
1980 static int uhci_control_thread(void *__uhci
)
1982 struct uhci
*uhci
= (struct uhci
*)__uhci
;
1984 uhci
->control_running
= 1;
1989 * This thread doesn't need any user-level access,
1990 * so get rid of all our resources..
1993 exit_files(current
);
1995 strcpy(current
->comm
, "uhci-control");
1998 * Ok, all systems are go..
2002 int unsigned long signr
;
2011 uhci_check_configuration(uhci
);
2013 interruptible_sleep_on(&uhci_configure
);
2015 if (signal_pending(current
)) {
2016 /* sending SIGUSR1 makes us print out some info */
2017 spin_lock_irq(¤t
->sigmask_lock
);
2018 signr
= dequeue_signal(¤t
->blocked
, &info
);
2019 spin_unlock_irq(¤t
->sigmask_lock
);
2021 if (signr
== SIGUSR1
) {
2022 printk(KERN_DEBUG
"UHCI queue dump:\n");
2023 uhci_show_queues(uhci
);
2024 } else if (signr
== SIGUSR2
) {
2025 uhci_debug
= !uhci_debug
;
2026 printk(KERN_DEBUG
"UHCI debug toggle = %x\n",
2031 } while (uhci
->control_continue
);
2037 uhci
->control_running
= 0;
2043 * If we've successfully found a UHCI, now is the time to increment the
2044 * module usage count, start the control thread, and return success..
2046 static int found_uhci(int irq
, unsigned int io_addr
, unsigned int io_size
)
2051 uhci
= alloc_uhci(io_addr
, io_size
);
2055 INIT_LIST_HEAD(&uhci
->uhci_list
);
2056 list_add(&uhci
->uhci_list
, &uhci_list
);
2058 request_region(uhci
->io_addr
, io_size
, "usb-uhci");
2062 usb_register_bus(uhci
->bus
);
2065 uhci
->control_continue
= 1;
2068 if (request_irq(irq
, uhci_interrupt
, SA_SHIRQ
, "uhci", uhci
) == 0) {
2072 pid
= kernel_thread(uhci_control_thread
, uhci
,
2073 CLONE_FS
| CLONE_FILES
| CLONE_SIGHAND
);
2075 uhci
->control_pid
= pid
;
2084 release_region(uhci
->io_addr
, uhci
->io_size
);
2090 static int start_uhci(struct pci_dev
*dev
)
2094 /* Search for the IO base address.. */
2095 for (i
= 0; i
< 6; i
++) {
2096 unsigned int io_addr
= dev
->resource
[i
].start
;
2097 unsigned int io_size
=
2098 dev
->resource
[i
].end
- dev
->resource
[i
].start
;
2101 if (!(dev
->resource
[i
].flags
& 1))
2104 /* Is it already in use? */
2105 if (check_region(io_addr
, io_size
))
2108 return found_uhci(dev
->irq
, io_addr
, io_size
);
2114 static int handle_apm_event(apm_event_t event
)
2116 static int down
= 0;
2119 case APM_SYS_SUSPEND
:
2120 case APM_USER_SUSPEND
:
2122 printk(KERN_DEBUG
"uhci: received extra suspend event\n");
2127 case APM_NORMAL_RESUME
:
2128 case APM_CRITICAL_RESUME
:
2130 printk(KERN_DEBUG
"uhci: received bogus resume event\n");
2134 if (waitqueue_active(&uhci_configure
)) {
2136 wake_up(&uhci_configure
);
2147 struct pci_dev
*dev
= NULL
;
2150 uhci_td_cachep
= kmem_cache_create("uhci_td",
2151 sizeof(struct uhci_td
), 0,
2152 SLAB_HWCACHE_ALIGN
, NULL
, NULL
);
2154 if (!uhci_td_cachep
)
2157 uhci_qh_cachep
= kmem_cache_create("uhci_qh",
2158 sizeof(struct uhci_qh
), 0,
2159 SLAB_HWCACHE_ALIGN
, NULL
, NULL
);
2161 if (!uhci_qh_cachep
)
2166 dev
= pci_find_class(PCI_CLASS_SERIAL_USB
<< 8, dev
);
2171 pci_read_config_byte(dev
, PCI_CLASS_PROG
, &type
);
2176 retval
= start_uhci(dev
);
2181 apm_register_callback(&handle_apm_event
);
2188 void uhci_cleanup(void)
2190 struct list_head
*next
, *tmp
, *head
= &uhci_list
;
2194 while (tmp
!= head
) {
2195 struct uhci
*uhci
= list_entry(tmp
, struct uhci
, uhci_list
);
2196 struct uhci_device
*root_hub
= usb_to_uhci(uhci
->bus
->root_hub
);
2200 list_del(&uhci
->uhci_list
);
2201 INIT_LIST_HEAD(&uhci
->uhci_list
);
2203 /* Check if the process is still running */
2204 ret
= kill_proc(uhci
->control_pid
, 0, 1);
2206 /* Try a maximum of 10 seconds */
2207 int count
= 10 * 100;
2209 uhci
->control_continue
= 0;
2210 wake_up(&uhci_configure
);
2212 while (uhci
->control_running
&& --count
) {
2213 current
->state
= TASK_INTERRUPTIBLE
;
2214 schedule_timeout(1);
2218 printk(KERN_ERR
"uhci: giving up on killing uhci-control\n");
2222 for (i
= 0; i
< root_hub
->usb
->maxchild
; i
++)
2223 usb_disconnect(root_hub
->usb
->children
+ i
);
2225 usb_deregister_bus(uhci
->bus
);
2228 release_region(uhci
->io_addr
, uhci
->io_size
);
2235 if (kmem_cache_destroy(uhci_qh_cachep
))
2236 printk(KERN_INFO
"uhci: not all QH's were freed\n");
2238 if (kmem_cache_destroy(uhci_td_cachep
))
2239 printk(KERN_INFO
"uhci: not all TD's were freed\n");
2243 int init_module(void)
2248 void cleanup_module(void)
2251 apm_unregister_callback(&handle_apm_event
);