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
;
124 * Return the result of a TD..
126 static int uhci_td_result(struct uhci_device
*dev
, struct uhci_td
*td
, unsigned long *rval
, int debug
)
130 int count
= 1000, actlength
, explength
;
135 /* Start at the TD first in the chain, if possible */
136 if (td
->qh
&& td
->qh
->first
)
142 return USB_ST_INTERNALERROR
;
144 /* Locate the first failing td, if any */
146 status
= uhci_status_bits(tmp
->status
);
151 /* The length field is only valid if the TD was completed */
152 if (!(tmp
->status
& TD_CTRL_ACTIVE
) && uhci_packetin(tmp
->info
)) {
153 explength
= uhci_expected_length(tmp
->info
);
154 actlength
= uhci_actual_length(tmp
->status
);
157 /* This check is bogus, at least for acm. It
158 always expects 64, but gets as many bytes
159 as available (typically 1) -- pavel */
160 if (0 && (explength
!= actlength
)) {
161 /* Reset the data toggle on error. */
162 if (debug
|| uhci_debug
)
163 printk(KERN_DEBUG
"Set toggle from %p rval %ld%c for status=%x to %d, exp=%d, act=%d\n",
164 tmp
, rval
? *rval
: 0,
165 rval
? '*' : '/', tmp
->status
,
166 uhci_toggle(tmp
->info
) ^ 1,
167 explength
, actlength
);
168 usb_settoggle(dev
->usb
, uhci_endpoint(tmp
->info
),
169 uhci_packetout(tmp
->info
),
170 uhci_toggle(tmp
->info
) ^ 1);
171 break; // Short packet
175 if ((tmp
->link
& UHCI_PTR_TERM
) ||
176 (tmp
->link
& UHCI_PTR_QH
))
179 tmp
= uhci_ptr_to_virt(tmp
->link
);
183 printk(KERN_ERR
"runaway td's in uhci_td_result!\n");
184 /* Force debugging on */
187 /* If we got to the last TD */
191 return USB_ST_NOERROR
;
193 /* APC BackUPS Pro kludge */
194 /* It tries to send all of the descriptor instead of */
195 /* the amount we requested */
196 if (tmp
->status
& TD_CTRL_IOC
&&
197 tmp
->status
& TD_CTRL_ACTIVE
&&
198 tmp
->status
& TD_CTRL_NAK
&&
199 tmp
->pipetype
== PIPE_CONTROL
)
200 return USB_ST_NOERROR
;
202 /* We got to an error, but the controller hasn't finished */
204 if (tmp
->status
& TD_CTRL_ACTIVE
)
205 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
&& !(tmp
->status
& TD_CTRL_NAK
))
211 return USB_ST_NOERROR
;
214 /* Some debugging code */
215 if (debug
&& uhci_debug
) {
216 printk(KERN_DEBUG
"uhci_td_result() failed with status %x\n",
219 /* Print the chain for debugging purposes */
221 uhci_show_queue(td
->qh
);
226 if (status
& TD_CTRL_STALLED
) {
227 /* endpoint has stalled - mark it halted */
228 usb_endpoint_halt(dev
->usb
, uhci_endpoint(tmp
->info
),
229 uhci_packetout(tmp
->info
));
233 if ((status
== TD_CTRL_ACTIVE
) && (!rval
))
234 return USB_ST_DATAUNDERRUN
;
236 return uhci_map_status(status
, uhci_packetout(tmp
->info
));
240 * Inserts a td into qh list at the top.
242 * Careful about atomicity: even on UP this
243 * requires a locked access due to the concurrent
246 * NOTE! This assumes that first->last is a valid
247 * list of TD's with the proper backpointers set
250 static void uhci_insert_tds_in_qh(struct uhci_qh
*qh
, struct uhci_td
*first
, struct uhci_td
*last
)
252 unsigned int link
= qh
->element
;
253 unsigned int new = virt_to_bus(first
) | UHCI_PTR_DEPTH
;
256 unsigned char success
;
259 first
->backptr
= &qh
->element
;
260 asm volatile("lock ; cmpxchg %4,%2 ; sete %0"
261 :"=q" (success
), "=a" (link
)
262 :"m" (qh
->element
), "1" (link
), "r" (new)
266 /* Was there a successor entry? Fix it's backpointer */
267 if ((link
& UHCI_PTR_TERM
) == 0) {
268 struct uhci_td
*next
= uhci_ptr_to_virt(link
);
269 next
->backptr
= &last
->link
;
280 static inline void uhci_insert_td_in_qh(struct uhci_qh
*qh
, struct uhci_td
*td
)
282 uhci_insert_tds_in_qh(qh
, td
, td
);
285 static void uhci_insert_qh(struct uhci_qh
*qh
, struct uhci_qh
*newqh
)
287 newqh
->link
= qh
->link
;
288 qh
->link
= virt_to_bus(newqh
) | UHCI_PTR_QH
;
291 static void uhci_remove_qh(struct uhci_qh
*qh
, struct uhci_qh
*remqh
)
293 struct uhci_qh
*lqh
= qh
;
295 while (uhci_ptr_to_virt(lqh
->link
) != remqh
) {
296 if (lqh
->link
& UHCI_PTR_TERM
)
299 lqh
= uhci_ptr_to_virt(lqh
->link
);
302 if (lqh
->link
& UHCI_PTR_TERM
) {
303 printk(KERN_DEBUG
"couldn't find qh in chain!\n");
307 lqh
->link
= remqh
->link
;
311 * Removes td from qh if present.
313 * NOTE! We keep track of both forward and back-pointers,
314 * so this should be trivial, right?
316 * Wrong. While all TD insert/remove operations are synchronous
317 * on the CPU, the UHCI controller can (and does) play with the
318 * very first forward pointer. So we need to validate the backptr
319 * before we change it, so that we don't by mistake reset the QH
320 * head to something old.
322 static void uhci_remove_td(struct uhci_td
*td
)
324 unsigned int *backptr
= td
->backptr
;
325 unsigned int link
= td
->link
;
334 * This is the easy case: the UHCI will never change "td->link",
335 * so we can always just look at that and fix up the backpointer
336 * of any next element..
338 if (!(link
& UHCI_PTR_TERM
)) {
339 struct uhci_td
*next
= uhci_ptr_to_virt(link
);
340 next
->backptr
= backptr
;
344 * The nasty case is "backptr->next", which we need to
345 * update to "link" _only_ if "backptr" still points
346 * to us (it may not: maybe backptr is a QH->element
347 * pointer and the UHCI has changed the value).
349 me
= virt_to_bus(td
) | (0xe & *backptr
);
350 asm volatile("lock ; cmpxchg %0,%1"
352 :"r" (link
), "m" (*backptr
), "a" (me
)
355 /* Reset it just in case */
356 td
->link
= UHCI_PTR_TERM
;
360 * Only the USB core should call uhci_alloc_dev and uhci_free_dev
362 static int uhci_alloc_dev(struct usb_device
*usb_dev
)
364 struct uhci_device
*dev
;
366 /* Allocate the UHCI device private data */
367 dev
= kmalloc(sizeof(*dev
), GFP_KERNEL
);
371 /* Initialize "dev" */
372 memset(dev
, 0, sizeof(*dev
));
374 usb_dev
->hcpriv
= dev
;
376 atomic_set(&dev
->refcnt
, 1);
379 dev
->uhci
= usb_to_uhci(usb_dev
->parent
)->uhci
;
384 static int uhci_free_dev(struct usb_device
*usb_dev
)
386 struct uhci_device
*dev
= usb_to_uhci(usb_dev
);
388 if (atomic_dec_and_test(&dev
->refcnt
))
394 static void uhci_inc_dev_use(struct uhci_device
*dev
)
396 atomic_inc(&dev
->refcnt
);
399 static void uhci_dec_dev_use(struct uhci_device
*dev
)
401 uhci_free_dev(dev
->usb
);
404 static struct uhci_td
*uhci_td_alloc(struct uhci_device
*dev
)
408 td
= kmem_cache_alloc(uhci_td_cachep
, SLAB_KERNEL
);
413 if ((__u32
)td
& UHCI_PTR_BITS
)
414 printk("qh not 16 byte aligned!\n");
417 td
->link
= UHCI_PTR_TERM
;
425 INIT_LIST_HEAD(&td
->irq_list
);
426 atomic_set(&td
->refcnt
, 1);
428 uhci_inc_dev_use(dev
);
433 static void uhci_td_free(struct uhci_td
*td
)
435 if (atomic_dec_and_test(&td
->refcnt
)) {
436 kmem_cache_free(uhci_td_cachep
, td
);
439 uhci_dec_dev_use(td
->dev
);
443 static struct uhci_qh
*uhci_qh_alloc(struct uhci_device
*dev
)
447 qh
= kmem_cache_alloc(uhci_qh_cachep
, SLAB_KERNEL
);
452 if ((__u32
)qh
& UHCI_PTR_BITS
)
453 printk("qh not 16 byte aligned!\n");
456 qh
->element
= UHCI_PTR_TERM
;
457 qh
->link
= UHCI_PTR_TERM
;
462 init_waitqueue_head(&qh
->wakeup
);
463 atomic_set(&qh
->refcnt
, 1);
465 uhci_inc_dev_use(dev
);
470 static void uhci_qh_free(struct uhci_qh
*qh
)
472 if (atomic_dec_and_test(&qh
->refcnt
)) {
473 kmem_cache_free(uhci_qh_cachep
, qh
);
476 uhci_dec_dev_use(qh
->dev
);
481 * UHCI interrupt list operations..
483 static spinlock_t irqlist_lock
= SPIN_LOCK_UNLOCKED
;
485 static void uhci_add_irq_list(struct uhci
*uhci
, struct uhci_td
*td
, usb_device_irq completed
, void *dev_id
)
489 td
->completed
= completed
;
492 spin_lock_irqsave(&irqlist_lock
, flags
);
493 list_add(&td
->irq_list
, &uhci
->interrupt_list
);
494 spin_unlock_irqrestore(&irqlist_lock
, flags
);
497 static void uhci_remove_irq_list(struct uhci_td
*td
)
501 spin_lock_irqsave(&irqlist_lock
, flags
);
502 list_del(&td
->irq_list
);
503 spin_unlock_irqrestore(&irqlist_lock
, flags
);
507 * frame list manipulation. Used for Isochronous transfers.
508 * the list of (iso) TD's enqueued in a frame list entry
509 * is basically a doubly linked list with link being
510 * the forward pointer and backptr the backward ptr.
511 * the frame list entry itself doesn't have a back ptr
512 * (therefore the list is not circular), and the forward pointer
513 * stops at link entries having the UHCI_PTR_TERM or the UHCI_PTR_QH
514 * bit set. Maybe it could be extended to handle the QH's also,
515 * but it doesn't seem necessary right now.
516 * The layout looks as follows:
517 * frame list pointer -> iso td's (if any) ->
518 * periodic interrupt td (if framelist 0) -> irq qh -> control qh -> bulk qh
521 static spinlock_t framelist_lock
= SPIN_LOCK_UNLOCKED
;
523 static void uhci_add_frame_list(struct uhci
*uhci
, struct uhci_td
*td
, unsigned framenum
)
526 struct uhci_td
*nexttd
;
528 framenum
%= UHCI_NUMFRAMES
;
529 spin_lock_irqsave(&framelist_lock
, flags
);
530 td
->backptr
= &uhci
->fl
->frame
[framenum
];
531 td
->link
= uhci
->fl
->frame
[framenum
];
532 if (!(td
->link
& (UHCI_PTR_TERM
| UHCI_PTR_QH
))) {
533 nexttd
= (struct uhci_td
*)uhci_ptr_to_virt(td
->link
);
534 nexttd
->backptr
= &td
->link
;
537 uhci
->fl
->frame
[framenum
] = virt_to_bus(td
);
538 spin_unlock_irqrestore(&framelist_lock
, flags
);
541 static void uhci_remove_frame_list(struct uhci
*uhci
, struct uhci_td
*td
)
544 struct uhci_td
*nexttd
;
548 spin_lock_irqsave(&framelist_lock
, flags
);
549 *(td
->backptr
) = td
->link
;
550 if (!(td
->link
& (UHCI_PTR_TERM
| UHCI_PTR_QH
))) {
551 nexttd
= (struct uhci_td
*)uhci_ptr_to_virt(td
->link
);
552 nexttd
->backptr
= td
->backptr
;
554 spin_unlock_irqrestore(&framelist_lock
, flags
);
557 * attention: td->link might still be in use by the
558 * hardware if the td is still active and the hardware
559 * was processing it. So td->link should be preserved
560 * until the frame number changes. Don't know what to do...
561 * udelay(1000) doesn't sound nice, and schedule()
562 * can't be used as this is called from within interrupt context.
564 /* for now warn if there's a possible problem */
565 if (td
->status
& TD_CTRL_ACTIVE
) {
566 unsigned frn
= inw(uhci
->io_addr
+ USBFRNUM
);
567 __u32 link
= uhci
->fl
->frame
[frn
% UHCI_NUMFRAMES
];
568 if (!(link
& (UHCI_PTR_TERM
| UHCI_PTR_QH
))) {
569 struct uhci_td
*tdl
= (struct uhci_td
*)uhci_ptr_to_virt(link
);
572 printk(KERN_WARNING
"uhci_remove_frame_list: td possibly still in use!!\n");
575 if (tdl
->link
& (UHCI_PTR_TERM
| UHCI_PTR_QH
))
577 tdl
= (struct uhci_td
*)uhci_ptr_to_virt(tdl
->link
);
585 * This function removes and disallocates all structures set up for a transfer.
586 * It takes the qh out of the skeleton, removes the tq and the td's.
587 * It only removes the associated interrupt handler if removeirq is set.
588 * The *td argument is any td in the list of td's.
590 static void uhci_remove_transfer(struct uhci_td
*td
, char removeirq
)
593 struct uhci_td
*curtd
;
594 unsigned int nextlink
;
596 if (td
->qh
&& td
->qh
->first
)
597 curtd
= td
->qh
->first
;
601 /* Remove it from the skeleton */
602 uhci_remove_qh(td
->qh
->skel
, td
->qh
);
603 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
);
615 if (nextlink
& UHCI_PTR_TERM
) /* Tail? */
618 curtd
= (struct uhci_td
*)uhci_ptr_to_virt(nextlink
);
620 printk(KERN_ERR
"runaway td's!?\n");
627 * Request an interrupt handler..
629 * Returns 0 (success) or negative (failure).
630 * Also returns/sets a "handle pointer" that release_irq can use to stop this
631 * interrupt. (It's really a pointer to the TD.)
633 static int uhci_request_irq(struct usb_device
*usb_dev
, unsigned int pipe
,
634 usb_device_irq handler
, int period
,
635 void *dev_id
, void **handle
, long bustime
)
637 struct uhci_device
*dev
= usb_to_uhci(usb_dev
);
638 struct uhci_td
*td
= uhci_td_alloc(dev
);
639 struct uhci_qh
*qh
= uhci_qh_alloc(dev
);
640 unsigned int destination
, status
;
650 /* Destination: pipe destination with INPUT */
651 destination
= (pipe
& PIPE_DEVEP_MASK
) | usb_packetid(pipe
);
653 /* Infinite errors is 0, so no bits */
654 status
= (pipe
& TD_CTRL_LS
) | TD_CTRL_IOC
| TD_CTRL_ACTIVE
| TD_CTRL_SPD
;
656 td
->link
= UHCI_PTR_TERM
; /* Terminate */
658 td
->info
= destination
| ((usb_maxpacket(usb_dev
, pipe
, usb_pipeout(pipe
)) - 1) << 21) |
659 (usb_gettoggle(usb_dev
, usb_pipeendpoint(pipe
), usb_pipeout(pipe
)) << TD_TOKEN_TOGGLE
);
661 td
->buffer
= virt_to_bus(dev
->data
);
664 td
->pipetype
= PIPE_INTERRUPT
;
665 td
->bandwidth_alloc
= bustime
;
667 /* if period 0, set _REMOVE flag */
669 td
->flags
|= UHCI_TD_REMOVE
;
671 qh
->skel
= &dev
->uhci
->skelqh
[__interval_to_skel(period
)];
673 uhci_add_irq_list(dev
->uhci
, td
, handler
, dev_id
);
675 uhci_insert_td_in_qh(qh
, td
);
677 /* Add it into the skeleton */
678 uhci_insert_qh(qh
->skel
, qh
);
680 *handle
= (void *)td
;
685 * Release an interrupt handler previously allocated using
686 * uhci_request_irq. This function does no validity checking, so make
687 * sure you're not releasing an already released handle as it may be
688 * in use by something else..
690 * This function can NOT be called from an interrupt.
692 static int uhci_release_irq(struct usb_device
*usb
, void *handle
)
697 td
= (struct uhci_td
*)handle
;
699 return USB_ST_INTERNALERROR
;
703 /* Remove it from the internal irq_list */
704 uhci_remove_irq_list(td
);
706 /* Remove the interrupt TD and QH */
708 uhci_remove_qh(qh
->skel
, qh
);
710 if (td
->completed
!= NULL
)
711 td
->completed(USB_ST_REMOVED
, NULL
, 0, td
->dev_id
);
713 /* Free the TD and QH */
717 return USB_ST_NOERROR
;
718 } /* uhci_release_irq() */
721 * uhci_get_current_frame_number()
723 * returns the current frame number for a USB bus/controller.
725 static int uhci_get_current_frame_number(struct usb_device
*usb_dev
)
727 return inw (usb_to_uhci(usb_dev
)->uhci
->io_addr
+ USBFRNUM
);
734 * Allocates some data structures.
735 * Initializes parts of them from the function parameters.
737 * It does not associate any data/buffer pointers or
738 * driver (caller) callback functions with the allocated
739 * data structures. Such associations are left until
742 * Returns 0 for success or negative value for error.
743 * Sets isocdesc before successful return.
745 static int uhci_init_isoc (struct usb_device
*usb_dev
,
747 int frame_count
, /* bandwidth % = 100 * this / 1024 */
749 struct usb_isoc_desc
**isocdesc
)
751 struct usb_isoc_desc
*id
;
756 /* Check some parameters. */
757 if ((frame_count
<= 0) || (frame_count
> UHCI_NUMFRAMES
)) {
758 #ifdef CONFIG_USB_DEBUG_ISOC
759 printk (KERN_DEBUG
"uhci_init_isoc: invalid frame_count (%d)\n",
765 if (!usb_pipeisoc (pipe
)) {
766 #ifdef CONFIG_USB_DEBUG_ISOC
767 printk (KERN_DEBUG
"uhci_init_isoc: NOT an Isoc. pipe\n");
772 id
= kmalloc (sizeof (*id
) +
773 (sizeof (struct isoc_frame_desc
) * frame_count
), GFP_KERNEL
);
777 memset (id
, 0, sizeof (*id
) +
778 (sizeof (struct isoc_frame_desc
) * frame_count
));
780 id
->td
= kmalloc (sizeof (struct uhci_td
) * frame_count
, GFP_KERNEL
);
786 memset (id
->td
, 0, sizeof (struct uhci_td
) * frame_count
);
788 for (i
= 0; i
< frame_count
; i
++)
789 INIT_LIST_HEAD(&((struct uhci_td
*)(id
->td
))[i
].irq_list
);
791 id
->frame_count
= frame_count
;
792 id
->frame_size
= usb_maxpacket (usb_dev
, pipe
, usb_pipeout(pipe
));
793 /* TBD: or make this a parameter to allow for frame_size
794 that is less than maxpacketsize */
795 id
->start_frame
= -1;
797 id
->usb_dev
= usb_dev
;
799 id
->context
= context
;
803 } /* end uhci_init_isoc */
808 * Associates data/buffer pointers/lengths and
809 * driver (caller) callback functions with the
810 * allocated Isoc. data structures and TDs.
812 * Then inserts the TDs into the USB controller frame list
813 * for its processing.
814 * And inserts the callback function into its TD.
816 * pr_isocdesc (previous Isoc. desc.) may be NULL.
817 * It is used only for chaining one list of TDs onto the
818 * end of the previous list of TDs.
820 * Returns 0 (success) or error code (negative value).
822 static int uhci_run_isoc (struct usb_isoc_desc
*isocdesc
,
823 struct usb_isoc_desc
*pr_isocdesc
)
825 struct uhci_device
*dev
= usb_to_uhci (isocdesc
->usb_dev
);
826 struct uhci
*uhci
= dev
->uhci
;
827 unsigned long destination
, status
;
829 int ix
, cur_frame
, pipeinput
, frlen
;
831 struct isoc_frame_desc
*fd
;
832 unsigned char *bufptr
;
834 if (!isocdesc
->callback_fn
) {
835 #ifdef CONFIG_USB_DEBUG_ISOC
836 printk (KERN_DEBUG
"uhci_run_isoc: caller must have a callback function\n");
841 /* Check buffer size large enough for maxpacketsize * frame_count. */
842 if (isocdesc
->buf_size
< (isocdesc
->frame_count
* isocdesc
->frame_size
)) {
843 #ifdef CONFIG_USB_DEBUG_ISOC
844 printk (KERN_DEBUG
"uhci_init_isoc: buf_size too small (%d < %d)\n",
845 isocdesc
->buf_size
, isocdesc
->frame_count
* isocdesc
->frame_size
);
850 /* Check buffer ptr for Null. */
851 if (!isocdesc
->data
) {
852 #ifdef CONFIG_USB_DEBUG_ISOC
853 printk (KERN_DEBUG
"uhci_init_isoc: data ptr is null\n");
858 #ifdef NEED_ALIGNMENT
859 /* Check data page alignment. */
860 if (((int)(isocdesc
->data
) & (PAGE_SIZE
- 1)) != 0) {
861 #ifdef CONFIG_USB_DEBUG_ISOC
862 printk (KERN_DEBUG
"uhci_init_isoc: buffer must be page-aligned (%p)\n",
867 #endif /* NEED_ALIGNMENT */
870 * Check start_type unless pr_isocdesc is used.
872 if (!pr_isocdesc
&& (isocdesc
->start_type
> START_TYPE_MAX
)) {
873 #ifdef CONFIG_USB_DEBUG_ISOC
874 printk (KERN_DEBUG
"uhci_run_isoc: invalid start_type (%d)\n",
875 isocdesc
->start_type
);
881 * Check start_frame for inside a valid range.
882 * Only allow transfer requests to be made 1.000 second
885 cur_frame
= uhci_get_current_frame_number (isocdesc
->usb_dev
);
887 /* if not START_ASAP (i.e., RELATIVE or ABSOLUTE): */
889 if (isocdesc
->start_type
== START_RELATIVE
) {
890 if ((isocdesc
->start_frame
< 0) || (isocdesc
->start_frame
> CAN_SCHEDULE_FRAMES
)) {
891 #ifdef CONFIG_USB_DEBUG_ISOC
892 printk (KERN_DEBUG
"uhci_init_isoc: bad start_frame value (%d)\n",
893 isocdesc
->start_frame
);
897 } /* end START_RELATIVE */
899 if (isocdesc
->start_type
== START_ABSOLUTE
) { /* within the scope of cur_frame */
900 ix
= USB_WRAP_FRAMENR(isocdesc
->start_frame
- cur_frame
);
901 if (ix
< START_FRAME_FUDGE
|| /* too small */
902 ix
> CAN_SCHEDULE_FRAMES
) { /* too large */
903 #ifdef CONFIG_USB_DEBUG_ISOC
904 printk (KERN_DEBUG
"uhci_init_isoc: bad start_frame value (%d,%d)\n",
905 isocdesc
->start_frame
, cur_frame
);
909 } /* end START_ABSOLUTE */
910 } /* end not pr_isocdesc */
913 * Set the start/end frame numbers.
916 isocdesc
->start_frame
= pr_isocdesc
->end_frame
+ 1;
917 } else if (isocdesc
->start_type
== START_RELATIVE
) {
918 if (isocdesc
->start_frame
< START_FRAME_FUDGE
)
919 isocdesc
->start_frame
= START_FRAME_FUDGE
;
920 isocdesc
->start_frame
+= cur_frame
;
921 } else if (isocdesc
->start_type
== START_ASAP
) {
922 isocdesc
->start_frame
= cur_frame
+ START_FRAME_FUDGE
;
925 /* and see if start_frame needs any correction */
926 /* only wrap to USB frame numbers, the frame_list insertion routine
927 takes care of the wrapping to the frame_list size */
928 isocdesc
->start_frame
= USB_WRAP_FRAMENR(isocdesc
->start_frame
);
930 /* and fix the end_frame value */
931 isocdesc
->end_frame
= USB_WRAP_FRAMENR(isocdesc
->start_frame
+ isocdesc
->frame_count
- 1);
933 isocdesc
->prev_completed_frame
= -1;
934 isocdesc
->cur_completed_frame
= -1;
936 destination
= (isocdesc
->pipe
& PIPE_DEVEP_MASK
) |
937 usb_packetid (isocdesc
->pipe
);
938 status
= TD_CTRL_ACTIVE
| TD_CTRL_IOS
; /* mark Isoc.; can't be low speed */
939 pipeinput
= usb_pipein (isocdesc
->pipe
);
940 cur_frame
= isocdesc
->start_frame
;
941 bufptr
= isocdesc
->data
;
944 * Build the Data TDs.
945 * TBD: Not using frame_spacing (Yet). Defaults to 1 (every frame).
946 * (frame_spacing is a way to request less bandwidth.)
947 * This can also be done by using frame_length = 0 in the
948 * frame_desc array, but this way won't take less bandwidth
949 * allocation into account.
952 if (isocdesc
->frame_spacing
<= 0)
953 isocdesc
->frame_spacing
= 1;
955 for (ix
= 0, td
= isocdesc
->td
, fd
= isocdesc
->frames
;
956 ix
< isocdesc
->frame_count
; ix
++, td
++, fd
++) {
957 frlen
= fd
->frame_length
;
958 if (frlen
> isocdesc
->frame_size
)
959 frlen
= isocdesc
->frame_size
;
962 td
->info
= destination
| /* use Actual len on OUT; max. on IN */
963 (pipeinput
? ((isocdesc
->frame_size
- 1) << 21)
964 : ((frlen
- 1) << 21));
967 td
->dev_id
= isocdesc
; /* can get dev_id or context from isocdesc */
969 td
->info
= destination
| ((frlen
- 1) << 21);
970 td
->buffer
= virt_to_bus (bufptr
);
972 td
->pipetype
= PIPE_ISOCHRONOUS
;
973 td
->isoc_td_number
= ix
; /* 0-based; does not wrap/overflow back to 0 */
975 if (isocdesc
->callback_frames
&&
976 (++cb_frames
>= isocdesc
->callback_frames
)) {
977 td
->status
|= TD_CTRL_IOC
;
978 td
->completed
= isocdesc
->callback_fn
;
980 uhci_add_irq_list (dev
->uhci
, td
, isocdesc
->callback_fn
, isocdesc
);
983 bufptr
+= fd
->frame_length
; /* or isocdesc->frame_size; */
986 * Insert the TD in the frame list.
988 uhci_add_frame_list(uhci
, td
, cur_frame
);
990 cur_frame
= USB_WRAP_FRAMENR(cur_frame
+1);
994 * Add IOC on the last TD.
997 if (!(td
->status
& TD_CTRL_IOC
)) {
998 td
->status
|= TD_CTRL_IOC
;
999 td
->completed
= isocdesc
->callback_fn
;
1000 uhci_add_irq_list(dev
->uhci
, td
, isocdesc
->callback_fn
, isocdesc
); /* TBD: D.K. ??? */
1004 } /* end uhci_run_isoc */
1009 * Marks a TD list as Inactive and removes it from the Isoc.
1012 * Does not free any memory resources.
1014 * Returns 0 for success or negative value for error.
1016 static int uhci_kill_isoc (struct usb_isoc_desc
*isocdesc
)
1018 struct uhci_device
*dev
= usb_to_uhci (isocdesc
->usb_dev
);
1019 struct uhci
*uhci
= dev
->uhci
;
1023 if (USB_WRAP_FRAMENR(isocdesc
->start_frame
) != isocdesc
->start_frame
) {
1024 #ifdef CONFIG_USB_DEBUG_ISOC
1025 printk (KERN_DEBUG
"uhci_kill_isoc: invalid start_frame (%d)\n",
1026 isocdesc
->start_frame
);
1031 for (ix
= 0, td
= isocdesc
->td
; ix
< isocdesc
->frame_count
; ix
++, td
++) {
1032 /* Deactivate and unlink */
1033 uhci_remove_frame_list(uhci
, td
);
1034 td
->status
&= ~(TD_CTRL_ACTIVE
| TD_CTRL_IOC
);
1037 isocdesc
->start_frame
= -1;
1039 } /* end uhci_kill_isoc */
1041 static void uhci_free_isoc (struct usb_isoc_desc
*isocdesc
)
1045 /* If still Active, kill it. */
1046 if (isocdesc
->start_frame
>= 0)
1047 uhci_kill_isoc(isocdesc
);
1049 /* Remove all TD's from the IRQ list. */
1050 for (i
= 0; i
< isocdesc
->frame_count
; i
++)
1051 uhci_remove_irq_list(((struct uhci_td
*)isocdesc
->td
) + i
);
1053 /* Free the associate memory. */
1055 kfree(isocdesc
->td
);
1058 } /* end uhci_free_isoc */
1061 * Control thread operations: we just mark the last TD
1062 * in a control thread as an interrupt TD, and wake up
1063 * the front-end on completion.
1065 * We need to remove the TD from the lists (both interrupt
1066 * list and TD lists) by hand if something bad happens!
1069 static int uhci_generic_completed(int status
, void *buffer
, int len
, void *dev_id
)
1071 wait_queue_head_t
*wakeup
= (wait_queue_head_t
*)dev_id
;
1073 if (waitqueue_active(wakeup
))
1076 printk("waitqueue empty!\n");
1078 return 0; /* Don't re-instate */
1081 /* td points to the last td in the list, which interrupts on completion */
1082 static int uhci_run_control(struct uhci_device
*dev
, struct uhci_td
*first
, struct uhci_td
*last
, int timeout
)
1084 DECLARE_WAITQUEUE(wait
, current
);
1085 struct uhci_qh
*qh
= uhci_qh_alloc(dev
);
1090 current
->state
= TASK_UNINTERRUPTIBLE
;
1091 add_wait_queue(&qh
->wakeup
, &wait
);
1093 uhci_add_irq_list(dev
->uhci
, last
, uhci_generic_completed
, &qh
->wakeup
);
1095 uhci_insert_tds_in_qh(qh
, first
, last
);
1097 /* Add it into the skeleton */
1098 uhci_insert_qh(&dev
->uhci
->skel_control_qh
, qh
);
1100 /* wait a user specified reasonable amount of time */
1101 schedule_timeout(timeout
);
1103 remove_wait_queue(&qh
->wakeup
, &wait
);
1105 /* Clean up in case it failed.. */
1106 uhci_remove_irq_list(last
);
1108 /* Remove it from the skeleton */
1109 uhci_remove_qh(&dev
->uhci
->skel_control_qh
, qh
);
1113 return uhci_td_result(dev
, last
, NULL
, 1);
1117 * Send or receive a control message on a pipe.
1119 * Note that the "pipe" structure is set up to map
1120 * easily to the uhci destination fields.
1122 * A control message is built up from three parts:
1123 * - The command itself
1124 * - [ optional ] data phase
1125 * - Status complete phase
1127 * The data phase can be an arbitrary number of TD's
1128 * although we currently had better not have more than
1129 * 29 TD's here (we have 31 TD's allocated for control
1130 * operations, and two of them are used for command and
1133 * 29 TD's is a minimum of 232 bytes worth of control
1134 * information, that's just ridiculously high. Most
1135 * control messages have just a few bytes of data.
1137 * 232 is not ridiculously high with many of the
1138 * configurations on audio devices, etc. anyway,
1139 * there is no restriction on length of transfers
1142 static int uhci_control_msg(struct usb_device
*usb_dev
, unsigned int pipe
, devrequest
*cmd
,
1143 void *data
, int len
, int timeout
)
1145 struct uhci_device
*dev
= usb_to_uhci(usb_dev
);
1146 struct uhci_td
*first
, *td
, *prevtd
;
1147 unsigned long destination
, status
;
1149 int maxsze
= usb_maxpacket(usb_dev
, pipe
, usb_pipeout(pipe
));
1151 unsigned long bytesrequested
= len
;
1152 unsigned long bytesread
= 0;
1154 unsigned char *orig_data
= (unsigned char *) data
;
1157 first
= td
= uhci_td_alloc(dev
);
1161 /* The "pipe" thing contains the destination in bits 8--18 */
1162 destination
= (pipe
& PIPE_DEVEP_MASK
) | USB_PID_SETUP
;
1165 status
= (pipe
& TD_CTRL_LS
) | TD_CTRL_ACTIVE
| TD_CTRL_SPD
| (3 << 27);
1168 * Build the TD for the control request
1170 td
->status
= status
; /* Try forever */
1171 td
->info
= destination
| (7 << 21); /* 8 bytes of data */
1172 td
->buffer
= virt_to_bus(cmd
);
1173 td
->pipetype
= PIPE_CONTROL
;
1176 * If direction is "send", change the frame from SETUP (0x2D)
1177 * to OUT (0xE1). Else change it from SETUP to IN (0x69).
1179 destination
^= (USB_PID_SETUP
^ USB_PID_IN
); /* SETUP -> IN */
1180 if (usb_pipeout(pipe
))
1181 destination
^= (USB_PID_IN
^ USB_PID_OUT
); /* IN -> OUT */
1184 td
= uhci_td_alloc(dev
);
1186 uhci_td_free(prevtd
);
1190 prevtd
->link
= virt_to_bus(td
) | UHCI_PTR_DEPTH
;
1193 * Build the DATA TD's
1196 /* Build the TD for control status */
1199 if (pktsze
> maxsze
)
1202 /* Alternate Data0/1 (start with Data1) */
1203 destination
^= 1 << TD_TOKEN_TOGGLE
;
1205 td
->status
= status
; /* Status */
1206 td
->info
= destination
| ((pktsze
- 1) << 21); /* pktsze bytes of data */
1207 td
->buffer
= virt_to_bus(data
);
1208 td
->backptr
= &prevtd
->link
;
1209 td
->pipetype
= PIPE_CONTROL
;
1215 td
= uhci_td_alloc(dev
);
1218 prevtd
->link
= virt_to_bus(td
) | UHCI_PTR_DEPTH
; /* Update previous TD */
1222 * Build the final TD for control status
1224 /* It's only IN if the pipe is out AND we aren't expecting data */
1225 destination
&= ~0xFF;
1226 if (usb_pipeout(pipe
) | (bytesrequested
== 0))
1227 destination
|= USB_PID_IN
;
1229 destination
|= USB_PID_OUT
;
1231 destination
|= 1 << TD_TOKEN_TOGGLE
; /* End in Data1 */
1233 td
->status
= status
| TD_CTRL_IOC
; /* no limit on errors on final packet */
1234 td
->info
= destination
| (UHCI_NULL_DATA_SIZE
<< 21); /* 0 bytes of data */
1236 td
->backptr
= &prevtd
->link
;
1237 td
->link
= UHCI_PTR_TERM
; /* Terminate */
1238 td
->pipetype
= PIPE_CONTROL
;
1241 ret
= uhci_run_control(dev
, first
, td
, timeout
);
1246 if (!uhci_status_bits(td
->status
) && ((td
->info
& 0xFF) ==
1248 bytesread
+= uhci_actual_length(td
->status
);
1250 nextlink
= td
->link
;
1254 if (nextlink
& UHCI_PTR_TERM
) /* Tail? */
1257 td
= uhci_ptr_to_virt(nextlink
);
1261 printk(KERN_ERR
"runaway td's!?\n");
1263 if (ret
&& (bytesread
>= bytesrequested
)) {
1264 printk(KERN_DEBUG
"Recovered sufficient data (asked for %ld, got %ld) from failed cmd\n",
1265 bytesrequested
, bytesread
);
1269 if (uhci_debug
&& ret
) {
1270 __u8
*p
= (__u8
*)cmd
;
1272 printk("dev %d, pipe %X requested %ld bytes, got %ld, status=%d:\n",
1273 usb_dev
->devnum
, pipe
, bytesrequested
, bytesread
, ret
);
1274 printk(KERN_DEBUG
"Failed cmd - %02X %02X %02X %02X %02X %02X %02X %02X\n",
1275 p
[0], p
[1], p
[2], p
[3], p
[4], p
[5], p
[6], p
[7]);
1279 if (!ret
&& usb_pipein(pipe
)) { /* good Input control msg */
1282 printk (KERN_CRIT
"ctrl msg [%02x %02x %04x %04x %04x] on pipe %x returned:\n",
1283 cmd
->requesttype
, cmd
->request
,
1284 cmd
->value
, cmd
->index
, cmd
->length
, pipe
);
1285 for (i
= 0; i
< bytesrequested
; ) {
1286 printk(" %02x", orig_data
[i
]);
1299 * Bulk thread operations: we just mark the last TD
1300 * in a bulk thread as an interrupt TD, and wake up
1301 * the front-end on completion.
1303 * We need to remove the TD from the lists (both interrupt
1304 * list and TD lists) by hand if something bad happens!
1307 /* td points to the last td in the list, which interrupts on completion */
1308 static int uhci_run_bulk(struct uhci_device
*dev
, struct uhci_td
*first
, struct uhci_td
*last
, unsigned long *rval
, int timeout
)
1310 DECLARE_WAITQUEUE(wait
, current
);
1311 struct uhci_qh
*qh
= uhci_qh_alloc(dev
);
1316 current
->state
= TASK_UNINTERRUPTIBLE
;
1317 add_wait_queue(&qh
->wakeup
, &wait
);
1319 uhci_add_irq_list(dev
->uhci
, last
, uhci_generic_completed
, &qh
->wakeup
);
1321 uhci_insert_tds_in_qh(qh
, first
, last
);
1323 /* Add it into the skeleton */
1324 uhci_insert_qh(&dev
->uhci
->skel_bulk_qh
, qh
);
1326 /* wait a user specified reasonable amount of time */
1327 schedule_timeout(timeout
);
1329 remove_wait_queue(&qh
->wakeup
, &wait
);
1331 /* Clean up in case it failed.. */
1332 uhci_remove_irq_list(last
);
1334 uhci_remove_qh(&dev
->uhci
->skel_bulk_qh
, qh
);
1338 return uhci_td_result(dev
, last
, rval
, 1);
1342 * Send or receive a bulk message on a pipe.
1344 * Note that the "pipe" structure is set up to map
1345 * easily to the uhci destination fields.
1347 * A bulk message is only built up from
1350 static int uhci_bulk_msg(struct usb_device
*usb_dev
, unsigned int pipe
, void *data
, int len
, unsigned long *rval
, int timeout
)
1352 struct uhci_device
*dev
= usb_to_uhci(usb_dev
);
1353 struct uhci_td
*first
, *td
, *prevtd
;
1354 unsigned long destination
, status
;
1356 int maxsze
= usb_maxpacket(usb_dev
, pipe
, usb_pipeout(pipe
));
1358 if (usb_endpoint_halted(usb_dev
, usb_pipeendpoint(pipe
), usb_pipeout(pipe
)) &&
1359 usb_clear_halt(usb_dev
, usb_pipeendpoint(pipe
) | (pipe
& USB_DIR_IN
)))
1360 return USB_ST_STALL
;
1362 /* The "pipe" thing contains the destination in bits 8--18. */
1363 destination
= (pipe
& PIPE_DEVEP_MASK
) | usb_packetid (pipe
);
1366 status
= (pipe
& TD_CTRL_LS
) | TD_CTRL_ACTIVE
| TD_CTRL_SPD
| (3 << 27);
1369 * Build the TDs for the bulk request
1371 first
= td
= uhci_td_alloc(dev
);
1375 prevtd
= first
; // This is fake, but at least it's not NULL
1377 /* Build the TD for control status */
1380 if (pktsze
> maxsze
)
1383 td
->status
= status
; /* Status */
1384 td
->info
= destination
| ((pktsze
-1) << 21) |
1385 (usb_gettoggle(usb_dev
, usb_pipeendpoint(pipe
),
1386 usb_pipeout(pipe
)) << TD_TOKEN_TOGGLE
); /* pktsze bytes of data */
1387 td
->buffer
= virt_to_bus(data
);
1388 td
->backptr
= &prevtd
->link
;
1389 td
->pipetype
= PIPE_BULK
;
1396 td
= uhci_td_alloc(dev
);
1400 prevtd
->link
= virt_to_bus(td
) | UHCI_PTR_DEPTH
;/* Update previous TD */
1403 /* Alternate Data0/1 (start with Data0) */
1404 usb_dotoggle(usb_dev
, usb_pipeendpoint(pipe
), usb_pipeout(pipe
));
1407 td
->link
= UHCI_PTR_TERM
; /* Terminate */
1408 td
->status
|= TD_CTRL_IOC
;
1410 /* CHANGE DIRECTION HERE! SAVE IT SOMEWHERE IN THE ENDPOINT!!! */
1413 ret
= uhci_run_bulk(dev
, first
, td
, rval
, timeout
);
1417 struct uhci_td
*curtd
= first
;
1418 unsigned int nextlink
;
1421 nextlink
= curtd
->link
;
1422 uhci_remove_td(curtd
);
1423 uhci_td_free(curtd
);
1425 if (nextlink
& UHCI_PTR_TERM
) /* Tail? */
1428 curtd
= uhci_ptr_to_virt(nextlink
);
1432 printk(KERN_DEBUG
"runaway td's!?\n");
1438 static void * uhci_request_bulk(struct usb_device
*usb_dev
, unsigned int pipe
, usb_device_irq handler
, void *data
, int len
, void *dev_id
)
1440 struct uhci_device
*dev
= usb_to_uhci(usb_dev
);
1441 struct uhci
*uhci
= dev
->uhci
;
1442 struct uhci_td
*first
, *td
, *prevtd
;
1443 struct uhci_qh
*bulk_qh
= uhci_qh_alloc(dev
);
1444 unsigned long destination
, status
;
1445 int maxsze
= usb_maxpacket(usb_dev
, pipe
, usb_pipeout(pipe
));
1447 /* The "pipe" thing contains the destination in bits 8--18. */
1448 destination
= (pipe
& PIPE_DEVEP_MASK
) | usb_packetid(pipe
);
1450 /* Infinite errors is 0 */
1451 status
= (pipe
& TD_CTRL_LS
) | TD_CTRL_ACTIVE
| TD_CTRL_SPD
;
1453 /* Build the TDs for the bulk request */
1454 first
= td
= uhci_td_alloc(dev
);
1457 /* Build the TD for control status */
1460 if (pktsze
> maxsze
)
1463 td
->status
= status
;
1464 td
->info
= destination
| ((pktsze
-1) << 21) |
1465 (usb_gettoggle(usb_dev
, usb_pipeendpoint(pipe
), usb_pipeout(pipe
)) << TD_TOKEN_TOGGLE
); /* pktsze bytes of data */
1466 td
->buffer
= virt_to_bus(data
);
1467 td
->backptr
= &prevtd
->link
;
1470 td
->pipetype
= PIPE_BULK
;
1476 td
= uhci_td_alloc(dev
);
1477 prevtd
->link
= virt_to_bus(td
) | UHCI_PTR_DEPTH
;
1480 /* Alternate Data0/1 */
1481 usb_dotoggle(usb_dev
, usb_pipeendpoint(pipe
), usb_pipeout(pipe
));
1484 first
->backptr
= NULL
;
1485 td
->link
= 1; /* Terminate */
1486 td
->status
= status
| TD_CTRL_IOC
; /* IOC */
1488 uhci_add_irq_list(dev
->uhci
, td
, handler
, dev_id
);
1490 uhci_insert_tds_in_qh(bulk_qh
, first
, td
);
1492 bulk_qh
->skel
= &uhci
->skel_bulk_qh
;
1493 uhci_insert_qh(&uhci
->skel_bulk_qh
, bulk_qh
);
1495 /* Return last td for removal */
1500 * Remove a handler from a pipe. This terminates the transfer.
1501 * We have some assumptions here:
1502 * There is only one queue using this pipe. (the one we remove)
1503 * Any data that is in the queue is useless for us, we throw it away.
1505 static int uhci_terminate_bulk(struct usb_device
*dev
, void *first
)
1507 /* none found? there is nothing to remove! */
1509 return USB_ST_REMOVED
;
1511 uhci_remove_transfer(first
, 1);
1513 return USB_ST_NOERROR
;
1516 struct usb_operations uhci_device_operations
= {
1524 uhci_terminate_bulk
,
1525 uhci_get_current_frame_number
,
1533 * This is just incredibly fragile. The timings must be just
1534 * right, and they aren't really documented very well.
1536 * Note the short delay between disabling reset and enabling
1539 static void uhci_reset_port(unsigned int port
)
1541 unsigned short status
;
1544 outw(status
| USBPORTSC_PR
, port
); /* reset port */
1546 outw(status
& ~USBPORTSC_PR
, port
);
1550 outw(status
| USBPORTSC_PE
, port
); /* enable port */
1554 if (!(status
& USBPORTSC_PE
)) {
1555 outw(status
| USBPORTSC_PE
, port
); /* one more try at enabling port */
1562 * This gets called if the connect status on the root
1563 * hub (and the root hub only) changes.
1565 static void uhci_connect_change(struct uhci
*uhci
, unsigned int port
, unsigned int nr
)
1567 struct usb_device
*usb_dev
;
1568 struct uhci_device
*dev
;
1569 unsigned short status
;
1570 struct uhci_device
*root_hub
= usb_to_uhci(uhci
->bus
->root_hub
);
1573 printk(KERN_INFO
"uhci_connect_change: called for %d\n", nr
);
1577 * Even if the status says we're connected,
1578 * the fact that the status bits changed may
1579 * that we got disconnected and then reconnected.
1581 * So start off by getting rid of any old devices..
1583 usb_disconnect(&root_hub
->usb
->children
[nr
]);
1587 /* If we have nothing connected, then clear change status and disable the port */
1588 status
= (status
& ~USBPORTSC_PE
) | USBPORTSC_PEC
;
1589 if (!(status
& USBPORTSC_CCS
)) {
1595 * Ok, we got a new connection. Allocate a device to it,
1596 * and find out what it wants to do..
1598 usb_dev
= usb_alloc_dev(root_hub
->usb
, root_hub
->usb
->bus
);
1602 dev
= usb_dev
->hcpriv
;
1604 usb_connect(usb_dev
);
1606 root_hub
->usb
->children
[nr
] = usb_dev
;
1608 wait_ms(200); /* wait for powerup */
1609 uhci_reset_port(port
);
1611 /* Get speed information */
1612 usb_dev
->slow
= (inw(port
) & USBPORTSC_LSDA
) ? 1 : 0;
1615 * Ok, all the stuff specific to the root hub has been done.
1616 * The rest is generic for any new USB attach, regardless of
1619 if (usb_new_device(usb_dev
)) {
1620 unsigned short status
= inw(port
);
1622 printk(KERN_INFO
"uhci: disabling malfunctioning port %d\n",
1624 outw(status
| USBPORTSC_PE
, port
);
1629 * This gets called when the root hub configuration
1630 * has changed. Just go through each port, seeing if
1631 * there is something interesting happening.
1633 static void uhci_check_configuration(struct uhci
*uhci
)
1635 struct uhci_device
*root_hub
= usb_to_uhci(uhci
->bus
->root_hub
);
1636 unsigned int io_addr
= uhci
->io_addr
+ USBPORTSC1
;
1637 int maxchild
= root_hub
->usb
->maxchild
;
1641 unsigned short status
= inw(io_addr
);
1643 if (status
& USBPORTSC_CSC
)
1644 uhci_connect_change(uhci
, io_addr
, nr
);
1647 } while (nr
< maxchild
);
1650 static int fixup_isoc_desc (struct uhci_td
*td
)
1652 struct usb_isoc_desc
*isocdesc
= td
->dev_id
;
1653 struct uhci_td
*prtd
;
1654 struct isoc_frame_desc
*frm
;
1655 int first_comp
= isocdesc
->cur_completed_frame
+ 1; /* 0-based */
1656 int cur_comp
= td
->isoc_td_number
; /* 0-based */
1660 if (first_comp
>= isocdesc
->frame_count
)
1662 num_comp
= cur_comp
- first_comp
+ 1;
1664 #ifdef CONFIG_USB_DEBUG_ISOC
1665 printk ("fixup_isoc_desc.1: td = %p, id = %p, first_comp = %d, cur_comp = %d, num_comp = %d\n",
1666 td
, isocdesc
, first_comp
, cur_comp
, num_comp
);
1669 for (ix
= 0, fx
= first_comp
, prtd
= ((struct uhci_td
*)(isocdesc
->td
))+first_comp
, frm
= &isocdesc
->frames
[first_comp
];
1670 ix
< num_comp
; ix
++) {
1671 frm
->frame_length
= uhci_actual_length (prtd
->status
);
1672 isocdesc
->total_length
+= frm
->frame_length
;
1674 if ((frm
->frame_status
= uhci_map_status (uhci_status_bits (prtd
->status
),
1675 uhci_packetout (prtd
->info
))))
1676 isocdesc
->error_count
++;
1680 if (++fx
>= isocdesc
->frame_count
) { /* wrap fx, prtd, and frm */
1682 prtd
= isocdesc
->td
;
1683 frm
= isocdesc
->frames
;
1688 * Update some other fields for drivers.
1690 isocdesc
->prev_completed_frame
= isocdesc
->cur_completed_frame
;
1691 isocdesc
->cur_completed_frame
= cur_comp
;
1692 isocdesc
->total_completed_frames
+= num_comp
; /* 1-based */
1694 #ifdef CONFIG_USB_DEBUG_ISOC
1695 printk ("fixup_isoc_desc.2: total_comp_frames = %d, total_length = %d, error_count = %d\n",
1696 isocdesc
->total_completed_frames
, isocdesc
->total_length
, isocdesc
->error_count
);
1697 #endif /* CONFIG_USB_DEBUG_ISOC */
1702 int uhci_isoc_callback(struct uhci
*uhci
, struct uhci_td
*td
, int status
, unsigned long rval
)
1704 struct usb_isoc_desc
*isocdesc
= td
->dev_id
;
1708 * Fixup the isocdesc for the driver: total_completed_frames,
1709 * error_count, total_length, frames array.
1711 * ret = callback_fn (int error_count, void *buffer,
1712 * int len, void *isocdesc);
1715 fixup_isoc_desc (td
);
1717 ret
= td
->completed (isocdesc
->error_count
, bus_to_virt (td
->buffer
),
1718 isocdesc
->total_length
, isocdesc
);
1721 * Isoc. handling of return value from td->completed (callback function)
1725 case CB_CONTINUE
: /* similar to the REMOVE condition below */
1730 case CB_REUSE
: /* similar to the re-add condition below,
1733 /* usb_dev = td->dev->usb; */
1735 list_add(&td
->irq_list
, &uhci
->interrupt_list
);
1737 td
->status
= (td
->status
& (TD_CTRL_SPD
| TD_CTRL_C_ERR_MASK
|
1738 TD_CTRL_LS
| TD_CTRL_IOS
| TD_CTRL_IOC
)) |
1741 /* The HC removes it, so re-add it */
1742 /* Insert into a QH? */
1743 uhci_insert_td_in_qh(td
->qh
, td
);
1746 case CB_RESTART
: /* similar to re-add, but mark ACTIVE */
1748 /* usb_dev = td->dev->usb; */
1750 list_add(&td
->irq_list
, &uhci
->interrupt_list
);
1752 td
->status
= (td
->status
& (TD_CTRL_SPD
| TD_CTRL_C_ERR_MASK
|
1753 TD_CTRL_LS
| TD_CTRL_IOS
| TD_CTRL_IOC
)) |
1754 TD_CTRL_ACTIVE
| TD_CTRL_IOC
;
1756 /* The HC removes it, so re-add it */
1757 uhci_insert_td_in_qh(td
->qh
, td
);
1760 case CB_ABORT
: /* kill/abort */
1762 uhci_kill_isoc (isocdesc
);
1764 } /* end isoc. TD switch */
1769 int uhci_callback(struct uhci
*uhci
, struct uhci_td
*td
, int status
, unsigned long rval
)
1771 if (td
->completed(status
, bus_to_virt(td
->buffer
), rval
, td
->dev_id
)) {
1772 struct usb_device
*usb_dev
= td
->dev
->usb
;
1774 list_add(&td
->irq_list
, &uhci
->interrupt_list
);
1776 usb_dotoggle(usb_dev
, uhci_endpoint(td
->info
), uhci_packetout(td
->info
));
1777 td
->info
&= ~(1 << TD_TOKEN_TOGGLE
); /* clear data toggle */
1778 td
->info
|= usb_gettoggle(usb_dev
, uhci_endpoint(td
->info
),
1779 uhci_packetout(td
->info
)) << TD_TOKEN_TOGGLE
; /* toggle between data0 and data1 */
1780 td
->status
= (td
->status
& 0x2F000000) | TD_CTRL_ACTIVE
| TD_CTRL_IOC
;
1781 /* The HC only removes it when it completed */
1782 /* successfully, so force remove and re-add it. */
1784 uhci_insert_td_in_qh(td
->qh
, td
);
1785 } else if (td
->flags
& UHCI_TD_REMOVE
) {
1786 struct usb_device
*usb_dev
= td
->dev
->usb
;
1788 /* marked for removal */
1789 td
->flags
&= ~UHCI_TD_REMOVE
;
1790 usb_dotoggle(usb_dev
, uhci_endpoint(td
->info
), uhci_packetout(td
->info
));
1791 uhci_remove_qh(td
->qh
->skel
, td
->qh
);
1792 uhci_qh_free(td
->qh
);
1793 if (td
->pipetype
== PIPE_INTERRUPT
)
1794 usb_release_bandwidth(usb_dev
, td
->bandwidth_alloc
);
1801 static void uhci_interrupt_notify(struct uhci
*uhci
)
1803 struct list_head
*tmp
, *head
= &uhci
->interrupt_list
;
1806 spin_lock(&irqlist_lock
);
1808 while (tmp
!= head
) {
1809 struct uhci_td
*td
= list_entry(tmp
, struct uhci_td
, irq_list
);
1814 /* We're interested if there was an error or if the chain of */
1815 /* TD's completed successfully */
1816 status
= uhci_td_result(td
->dev
, td
, &rval
, 0);
1818 if (status
== USB_ST_NOCHANGE
)
1821 /* remove from IRQ list */
1822 list_del(&td
->irq_list
);
1823 INIT_LIST_HEAD(&td
->irq_list
);
1825 if (td
->pipetype
== PIPE_ISOCHRONOUS
) {
1826 uhci_isoc_callback(uhci
, td
, status
, rval
);
1829 uhci_callback(uhci
, td
, status
, rval
);
1832 /* If completed does not wants to reactivate, then */
1833 /* it's responsible for free'ing the TD's and QH's */
1834 /* or another function (such as run_control) */
1836 spin_unlock(&irqlist_lock
);
1840 * Check port status - Connect Status Change - for
1841 * each of the attached ports (defaults to two ports,
1842 * but at least in theory there can be more of them).
1844 * Wake up the configurator if something happened, we
1845 * can't really do much at interrupt time.
1847 static void uhci_root_hub_events(struct uhci
*uhci
, unsigned int io_addr
)
1849 if (waitqueue_active(&uhci_configure
)) {
1850 struct uhci_device
*root_hub
= usb_to_uhci(uhci
->bus
->root_hub
);
1851 int ports
= root_hub
->usb
->maxchild
;
1853 io_addr
+= USBPORTSC1
;
1855 if (inw(io_addr
) & USBPORTSC_CSC
) {
1856 wake_up(&uhci_configure
);
1860 } while (--ports
> 0);
1864 static void uhci_interrupt(int irq
, void *__uhci
, struct pt_regs
*regs
)
1866 struct uhci
*uhci
= __uhci
;
1867 unsigned int io_addr
= uhci
->io_addr
;
1868 unsigned short status
;
1871 * Read the interrupt status, and write it back to clear the
1874 status
= inw(io_addr
+ USBSTS
);
1875 if (!status
) /* shared interrupt, not mine */
1877 outw(status
, io_addr
+ USBSTS
);
1879 /* Walk the list of pending TD's to see which ones completed.. */
1880 uhci_interrupt_notify(uhci
);
1882 /* Check if there are any events on the root hub.. */
1883 uhci_root_hub_events(uhci
, io_addr
);
1887 * We init one packet, and mark it just IOC and _not_
1888 * active. Which will result in no actual USB traffic,
1889 * but _will_ result in an interrupt every second.
1891 * Which is exactly what we want.
1893 static void uhci_init_ticktd(struct uhci
*uhci
)
1895 struct uhci_device
*dev
= usb_to_uhci(uhci
->bus
->root_hub
);
1896 struct uhci_td
*td
= uhci_td_alloc(dev
);
1899 printk(KERN_ERR
"unable to allocate ticktd\n");
1903 /* Don't clobber the frame */
1904 td
->link
= uhci
->fl
->frame
[0];
1905 td
->backptr
= &uhci
->fl
->frame
[0];
1906 td
->status
= TD_CTRL_IOC
;
1907 td
->info
= (15 << 21) | (0x7f << 8) | USB_PID_IN
;
1908 /* (ignored) input packet, 16 bytes, device 127 */
1913 uhci
->fl
->frame
[0] = virt_to_bus(td
);
1918 static void reset_hc(struct uhci
*uhci
)
1920 unsigned int io_addr
= uhci
->io_addr
;
1922 /* Global reset for 50ms */
1923 outw(USBCMD_GRESET
, io_addr
+ USBCMD
);
1925 outw(0, io_addr
+ USBCMD
);
1929 static void start_hc(struct uhci
*uhci
)
1931 unsigned int io_addr
= uhci
->io_addr
;
1934 uhci_init_ticktd(uhci
);
1937 * Reset the HC - this will force us to get a
1938 * new notification of any already connected
1939 * ports due to the virtual disconnect that it
1942 outw(USBCMD_HCRESET
, io_addr
+ USBCMD
);
1943 while (inw(io_addr
+ USBCMD
) & USBCMD_HCRESET
) {
1945 printk(KERN_ERR
"USBCMD_HCRESET timed out!\n");
1950 /* Turn on all interrupts */
1951 outw(USBINTR_TIMEOUT
| USBINTR_RESUME
| USBINTR_IOC
| USBINTR_SP
,
1954 /* Start at frame 0 */
1955 outw(0, io_addr
+ USBFRNUM
);
1956 outl(virt_to_bus(uhci
->fl
), io_addr
+ USBFLBASEADD
);
1958 /* Run and mark it configured with a 64-byte max packet */
1959 outw(USBCMD_RS
| USBCMD_CF
| USBCMD_MAXP
, io_addr
+ USBCMD
);
1963 * Allocate a frame list, and four regular queues.
1965 * The hardware doesn't really know any difference
1966 * in the queues, but the order does matter for the
1967 * protocols higher up. The order is:
1969 * - any isochronous events handled before any
1970 * of the queues. We don't do that here, because
1971 * we'll create the actual TD entries on demand.
1972 * - The first queue is the "interrupt queue".
1973 * - The second queue is the "control queue".
1974 * - The third queue is "bulk data".
1976 * We could certainly have multiple queues of the same
1977 * type, and maybe we should. We could have per-device
1978 * queues, for example. We begin small.
1980 * Queues are dynamically allocated for devices now,
1981 * this code only sets up the skeleton queue
1983 static struct uhci
*alloc_uhci(unsigned int io_addr
, unsigned int io_size
)
1987 struct usb_bus
*bus
;
1988 struct uhci_device
*dev
;
1989 struct usb_device
*usb
;
1991 uhci
= kmalloc(sizeof(*uhci
), GFP_KERNEL
);
1995 memset(uhci
, 0, sizeof(*uhci
));
1998 uhci
->io_addr
= io_addr
;
1999 uhci
->io_size
= io_size
;
2000 INIT_LIST_HEAD(&uhci
->interrupt_list
);
2002 /* We need exactly one page (per UHCI specs), how convenient */
2003 uhci
->fl
= (void *)__get_free_page(GFP_KERNEL
);
2007 bus
= usb_alloc_bus(&uhci_device_operations
);
2015 * Allocate the root_hub
2017 usb
= usb_alloc_dev(NULL
, bus
);
2023 dev
= usb_to_uhci(usb
);
2026 uhci
->bus
->root_hub
= uhci_to_usb(dev
);
2028 /* Initialize the root hub */
2030 /* UHCI specs says devices must have 2 ports, but goes on to say */
2031 /* they may have more but give no way to determine how many they */
2032 /* have, so default to 2 */
2033 /* According to the UHCI spec, Bit 7 is always set to 1. So we try */
2034 /* to use this to our advantage */
2035 for (port
= 0; port
< (io_size
- 0x10) / 2; port
++) {
2036 unsigned int portstatus
;
2038 portstatus
= inw(io_addr
+ 0x10 + (port
* 2));
2039 if (!(portstatus
& 0x0080))
2042 printk(KERN_DEBUG
"Detected %d ports\n", port
);
2044 /* This is experimental so anything less than 2 or greater than 8 is */
2045 /* something weird and we'll ignore it */
2046 if (port
< 2 || port
> 8) {
2047 printk(KERN_DEBUG
"Port count misdetected, forcing to 2 ports\n");
2051 usb
->maxchild
= port
;
2052 usb_init_root_hub(usb
);
2055 * 9 Interrupt queues; link int2 thru int256 to int1 first,
2056 * then link int1 to control and control to bulk
2058 for (i
= 1; i
< 9; i
++) {
2059 struct uhci_qh
*qh
= &uhci
->skelqh
[i
];
2061 qh
->link
= virt_to_bus(&uhci
->skel_int1_qh
) | UHCI_PTR_QH
;
2062 qh
->element
= UHCI_PTR_TERM
;
2065 uhci
->skel_int1_qh
.link
= virt_to_bus(&uhci
->skel_control_qh
) | UHCI_PTR_QH
;
2066 uhci
->skel_int1_qh
.element
= UHCI_PTR_TERM
;
2068 uhci
->skel_control_qh
.link
= virt_to_bus(&uhci
->skel_bulk_qh
) | UHCI_PTR_QH
;
2069 uhci
->skel_control_qh
.element
= UHCI_PTR_TERM
;
2071 uhci
->skel_bulk_qh
.link
= UHCI_PTR_TERM
;
2072 uhci
->skel_bulk_qh
.element
= UHCI_PTR_TERM
;
2075 * Fill the frame list: make all entries point to
2076 * the proper interrupt queue.
2078 * This is probably silly, but it's a simple way to
2079 * scatter the interrupt queues in a way that gives
2080 * us a reasonable dynamic range for irq latencies.
2082 for (i
= 0; i
< 1024; i
++) {
2083 struct uhci_qh
*irq
= &uhci
->skel_int2_qh
;
2105 uhci
->fl
->frame
[i
] = virt_to_bus(irq
) | UHCI_PTR_QH
;
2117 free_page((unsigned long)uhci
->fl
);
2124 * De-allocate all resources..
2126 static void release_uhci(struct uhci
*uhci
)
2128 if (uhci
->irq
>= 0) {
2129 free_irq(uhci
->irq
, uhci
);
2134 uhci_td_free(uhci
->ticktd
);
2135 uhci
->ticktd
= NULL
;
2139 free_page((unsigned long)uhci
->fl
);
2143 usb_free_bus(uhci
->bus
);
2147 static int uhci_control_thread(void *__uhci
)
2149 struct uhci
*uhci
= (struct uhci
*)__uhci
;
2151 uhci
->control_running
= 1;
2156 * This thread doesn't need any user-level access,
2157 * so get rid of all our resources..
2160 exit_files(current
);
2162 strcpy(current
->comm
, "uhci-control");
2165 * Ok, all systems are go..
2169 int unsigned long signr
;
2178 uhci_check_configuration(uhci
);
2180 interruptible_sleep_on(&uhci_configure
);
2182 if (signal_pending(current
)) {
2183 /* sending SIGUSR1 makes us print out some info */
2184 spin_lock_irq(¤t
->sigmask_lock
);
2185 signr
= dequeue_signal(¤t
->blocked
, &info
);
2186 spin_unlock_irq(¤t
->sigmask_lock
);
2188 if (signr
== SIGUSR1
) {
2189 printk(KERN_DEBUG
"UHCI queue dump:\n");
2190 uhci_show_queues(uhci
);
2191 } else if (signr
== SIGUSR2
) {
2192 uhci_debug
= !uhci_debug
;
2193 printk(KERN_DEBUG
"UHCI debug toggle = %x\n",
2198 } while (uhci
->control_continue
);
2204 uhci
->control_running
= 0;
2210 * If we've successfully found a UHCI, now is the time to increment the
2211 * module usage count, start the control thread, and return success..
2213 static int found_uhci(int irq
, unsigned int io_addr
, unsigned int io_size
)
2218 uhci
= alloc_uhci(io_addr
, io_size
);
2222 INIT_LIST_HEAD(&uhci
->uhci_list
);
2223 list_add(&uhci
->uhci_list
, &uhci_list
);
2225 request_region(uhci
->io_addr
, io_size
, "usb-uhci");
2229 usb_register_bus(uhci
->bus
);
2232 uhci
->control_continue
= 1;
2235 if (request_irq(irq
, uhci_interrupt
, SA_SHIRQ
, "uhci", uhci
) == 0) {
2239 pid
= kernel_thread(uhci_control_thread
, uhci
,
2240 CLONE_FS
| CLONE_FILES
| CLONE_SIGHAND
);
2242 uhci
->control_pid
= pid
;
2251 release_region(uhci
->io_addr
, uhci
->io_size
);
2257 static int start_uhci(struct pci_dev
*dev
)
2261 /* Search for the IO base address.. */
2262 for (i
= 0; i
< 6; i
++) {
2263 unsigned int io_addr
= dev
->resource
[i
].start
;
2264 unsigned int io_size
=
2265 dev
->resource
[i
].end
- dev
->resource
[i
].start
+ 1;
2268 if (!(dev
->resource
[i
].flags
& 1))
2271 /* Is it already in use? */
2272 if (check_region(io_addr
, io_size
))
2275 /* disable legacy emulation */
2276 pci_write_config_word(dev
, USBLEGSUP
, USBLEGSUP_DEFAULT
);
2278 pci_enable_device(dev
);
2280 return found_uhci(dev
->irq
, io_addr
, io_size
);
2286 static int handle_apm_event(apm_event_t event
)
2288 static int down
= 0;
2291 case APM_SYS_SUSPEND
:
2292 case APM_USER_SUSPEND
:
2294 printk(KERN_DEBUG
"uhci: received extra suspend event\n");
2299 case APM_NORMAL_RESUME
:
2300 case APM_CRITICAL_RESUME
:
2302 printk(KERN_DEBUG
"uhci: received bogus resume event\n");
2306 if (waitqueue_active(&uhci_configure
)) {
2308 wake_up(&uhci_configure
);
2319 struct pci_dev
*dev
= NULL
;
2322 uhci_td_cachep
= kmem_cache_create("uhci_td",
2323 sizeof(struct uhci_td
), 0,
2324 SLAB_HWCACHE_ALIGN
, NULL
, NULL
);
2326 if (!uhci_td_cachep
)
2329 uhci_qh_cachep
= kmem_cache_create("uhci_qh",
2330 sizeof(struct uhci_qh
), 0,
2331 SLAB_HWCACHE_ALIGN
, NULL
, NULL
);
2333 if (!uhci_qh_cachep
)
2338 dev
= pci_find_class(PCI_CLASS_SERIAL_USB
<< 8, dev
);
2343 pci_read_config_byte(dev
, PCI_CLASS_PROG
, &type
);
2348 retval
= start_uhci(dev
);
2353 apm_register_callback(&handle_apm_event
);
2360 void uhci_cleanup(void)
2362 struct list_head
*next
, *tmp
, *head
= &uhci_list
;
2366 while (tmp
!= head
) {
2367 struct uhci
*uhci
= list_entry(tmp
, struct uhci
, uhci_list
);
2368 struct uhci_device
*root_hub
= usb_to_uhci(uhci
->bus
->root_hub
);
2372 list_del(&uhci
->uhci_list
);
2373 INIT_LIST_HEAD(&uhci
->uhci_list
);
2375 /* Check if the process is still running */
2376 ret
= kill_proc(uhci
->control_pid
, 0, 1);
2378 /* Try a maximum of 10 seconds */
2379 int count
= 10 * 100;
2381 uhci
->control_continue
= 0;
2382 wake_up(&uhci_configure
);
2384 while (uhci
->control_running
&& --count
) {
2385 current
->state
= TASK_INTERRUPTIBLE
;
2386 schedule_timeout(1);
2390 printk(KERN_ERR
"uhci: giving up on killing uhci-control\n");
2394 for (i
= 0; i
< root_hub
->usb
->maxchild
; i
++)
2395 usb_disconnect(root_hub
->usb
->children
+ i
);
2397 usb_deregister_bus(uhci
->bus
);
2400 release_region(uhci
->io_addr
, uhci
->io_size
);
2407 if (kmem_cache_destroy(uhci_qh_cachep
))
2408 printk(KERN_INFO
"uhci: not all QH's were freed\n");
2410 if (kmem_cache_destroy(uhci_td_cachep
))
2411 printk(KERN_INFO
"uhci: not all TD's were freed\n");
2415 int init_module(void)
2420 void cleanup_module(void)
2423 apm_unregister_callback(&handle_apm_event
);