2 * Universal Host Controller Interface driver for USB.
4 * (C) Copyright 1999 Linus Torvalds
5 * (C) Copyright 1999-2000 Johannes Erdfelt, jerdfelt@sventech.com
6 * (C) Copyright 1999 Randy Dunlap
7 * (C) Copyright 1999 Georg Acher, acher@in.tum.de
8 * (C) Copyright 1999 Deti Fliegl, deti@fliegl.de
9 * (C) Copyright 1999 Thomas Sailer, sailer@ife.ee.ethz.ch
10 * (C) Copyright 1999 Roman Weissgaerber, weissg@vienna.at
12 * Intel documents this fairly well, and as far as I know there
13 * are no royalties or anything like that, but even so there are
14 * people who decided that they want to do the same thing in a
15 * completely different way.
17 * WARNING! The USB documentation is downright evil. Most of it
18 * is just crap, written by a committee. You're better off ignoring
19 * most of it, the important stuff is:
20 * - the low-level protocol (fairly simple but lots of small details)
21 * - working around the horridness of the rest
24 #include <linux/module.h>
25 #include <linux/pci.h>
26 #include <linux/kernel.h>
27 #include <linux/delay.h>
28 #include <linux/ioport.h>
29 #include <linux/sched.h>
30 #include <linux/malloc.h>
31 #include <linux/smp_lock.h>
32 #include <linux/errno.h>
33 #include <linux/unistd.h>
34 #include <linux/interrupt.h>
35 #include <linux/spinlock.h>
37 #include <linux/usb.h>
39 #include <asm/uaccess.h>
42 #include <asm/system.h>
45 #include "uhci-debug.h"
48 static int handle_pm_event(struct pm_dev
*dev
, pm_request_t rqst
, void *data
);
51 MODULE_PARM(debug
, "i");
53 static kmem_cache_t
*uhci_td_cachep
;
54 static kmem_cache_t
*uhci_qh_cachep
;
55 static kmem_cache_t
*uhci_up_cachep
; /* urb_priv */
57 static LIST_HEAD(uhci_list
);
59 static int rh_submit_urb(struct urb
*urb
);
60 static int rh_unlink_urb(struct urb
*urb
);
61 static int uhci_get_current_frame_number(struct usb_device
*dev
);
62 static int uhci_unlink_generic(struct urb
*urb
);
63 static int uhci_unlink_urb(struct urb
*urb
);
65 #define min(a,b) (((a)<(b))?(a):(b))
67 /* If a transfer is still active after this much time, turn off FSBR */
68 #define IDLE_TIMEOUT (HZ / 20) /* 50 ms */
71 * Only the USB core should call uhci_alloc_dev and uhci_free_dev
73 static int uhci_alloc_dev(struct usb_device
*dev
)
78 static int uhci_free_dev(struct usb_device
*dev
)
80 struct uhci
*uhci
= (struct uhci
*)dev
->bus
->hcpriv
;
81 struct list_head
*tmp
, *head
= &uhci
->urb_list
;
84 /* Walk through the entire URB list and forcefully remove any */
85 /* URBs that are still active for that device */
86 nested_lock(&uhci
->urblist_lock
, flags
);
89 struct urb
*u
= list_entry(tmp
, struct urb
, urb_list
);
96 nested_unlock(&uhci
->urblist_lock
, flags
);
101 static void uhci_add_urb_list(struct uhci
*uhci
, struct urb
*urb
)
105 nested_lock(&uhci
->urblist_lock
, flags
);
106 list_add(&urb
->urb_list
, &uhci
->urb_list
);
107 nested_unlock(&uhci
->urblist_lock
, flags
);
110 static void uhci_remove_urb_list(struct uhci
*uhci
, struct urb
*urb
)
114 nested_lock(&uhci
->urblist_lock
, flags
);
115 if (!list_empty(&urb
->urb_list
)) {
116 list_del(&urb
->urb_list
);
117 INIT_LIST_HEAD(&urb
->urb_list
);
119 nested_unlock(&uhci
->urblist_lock
, flags
);
122 void uhci_set_next_interrupt(struct uhci
*uhci
)
126 spin_lock_irqsave(&uhci
->framelist_lock
, flags
);
127 uhci
->skel_term_td
.status
|= TD_CTRL_IOC
;
128 spin_unlock_irqrestore(&uhci
->framelist_lock
, flags
);
131 void uhci_clear_next_interrupt(struct uhci
*uhci
)
135 spin_lock_irqsave(&uhci
->framelist_lock
, flags
);
136 uhci
->skel_term_td
.status
&= ~TD_CTRL_IOC
;
137 spin_unlock_irqrestore(&uhci
->framelist_lock
, flags
);
140 static struct uhci_td
*uhci_alloc_td(struct usb_device
*dev
)
144 td
= kmem_cache_alloc(uhci_td_cachep
, in_interrupt() ? SLAB_ATOMIC
: SLAB_KERNEL
);
148 td
->link
= UHCI_PTR_TERM
;
152 td
->nexttd
= td
->prevtd
= NULL
;
154 INIT_LIST_HEAD(&td
->list
);
156 usb_inc_dev_use(dev
);
161 static void inline uhci_fill_td(struct uhci_td
*td
, __u32 status
,
162 __u32 info
, __u32 buffer
)
169 static void uhci_insert_td(struct uhci
*uhci
, struct uhci_td
*skeltd
, struct uhci_td
*td
)
173 spin_lock_irqsave(&uhci
->framelist_lock
, flags
);
175 /* Fix the linked list pointers */
176 td
->nexttd
= skeltd
->nexttd
;
179 skeltd
->nexttd
->prevtd
= td
;
182 td
->link
= skeltd
->link
;
183 skeltd
->link
= virt_to_bus(td
);
185 spin_unlock_irqrestore(&uhci
->framelist_lock
, flags
);
189 * We insert Isochronous transfers directly into the frame list at the
191 * The layout looks as follows:
192 * frame list pointer -> iso td's (if any) ->
193 * periodic interrupt td (if frame 0) -> irq td's -> control qh -> bulk qh
196 static void uhci_insert_td_frame_list(struct uhci
*uhci
, struct uhci_td
*td
, unsigned framenum
)
199 struct uhci_td
*nexttd
;
201 framenum
%= UHCI_NUMFRAMES
;
203 spin_lock_irqsave(&uhci
->framelist_lock
, flags
);
205 td
->frameptr
= &uhci
->fl
->frame
[framenum
];
206 td
->link
= uhci
->fl
->frame
[framenum
];
207 if (!(td
->link
& (UHCI_PTR_TERM
| UHCI_PTR_QH
))) {
208 nexttd
= (struct uhci_td
*)uhci_ptr_to_virt(td
->link
);
211 nexttd
->frameptr
= NULL
;
213 uhci
->fl
->frame
[framenum
] = virt_to_bus(td
);
215 spin_unlock_irqrestore(&uhci
->framelist_lock
, flags
);
218 static void uhci_remove_td(struct uhci
*uhci
, struct uhci_td
*td
)
222 /* If it's not inserted, don't remove it */
223 if (!td
->frameptr
&& !td
->prevtd
&& !td
->nexttd
)
226 spin_lock_irqsave(&uhci
->framelist_lock
, flags
);
228 *(td
->frameptr
) = td
->link
;
230 td
->nexttd
->frameptr
= td
->frameptr
;
231 td
->nexttd
->prevtd
= NULL
;
237 td
->prevtd
->nexttd
= td
->nexttd
;
238 td
->prevtd
->link
= td
->link
;
241 td
->nexttd
->prevtd
= td
->prevtd
;
242 td
->prevtd
= td
->nexttd
= NULL
;
244 td
->link
= UHCI_PTR_TERM
;
245 spin_unlock_irqrestore(&uhci
->framelist_lock
, flags
);
249 * Inserts a td into qh list at the top.
251 static void uhci_insert_tds_in_qh(struct uhci_qh
*qh
, struct urb
*urb
, int breadth
)
253 struct list_head
*tmp
, *head
;
254 struct urb_priv
*urbp
= (struct urb_priv
*)urb
->hcpriv
;
255 struct uhci_td
*td
, *prevtd
;
265 td
= list_entry(tmp
, struct uhci_td
, list
);
267 /* Add the first TD to the QH element pointer */
268 qh
->element
= virt_to_bus(td
) | (breadth
? 0 : UHCI_PTR_DEPTH
);
272 /* Then link the rest of the TD's */
274 while (tmp
!= head
) {
275 td
= list_entry(tmp
, struct uhci_td
, list
);
279 prevtd
->link
= virt_to_bus(td
) | (breadth
? 0 : UHCI_PTR_DEPTH
);
284 prevtd
->link
= UHCI_PTR_TERM
;
287 static void uhci_free_td(struct uhci_td
*td
)
289 if (!list_empty(&td
->list
))
290 dbg("td is still in URB list!");
293 usb_dec_dev_use(td
->dev
);
295 kmem_cache_free(uhci_td_cachep
, td
);
298 static struct uhci_qh
*uhci_alloc_qh(struct usb_device
*dev
)
302 qh
= kmem_cache_alloc(uhci_qh_cachep
, in_interrupt() ? SLAB_ATOMIC
: SLAB_KERNEL
);
306 qh
->element
= UHCI_PTR_TERM
;
307 qh
->link
= UHCI_PTR_TERM
;
310 qh
->prevqh
= qh
->nextqh
= NULL
;
312 INIT_LIST_HEAD(&qh
->remove_list
);
314 usb_inc_dev_use(dev
);
319 static void uhci_free_qh(struct uhci_qh
*qh
)
322 usb_dec_dev_use(qh
->dev
);
324 kmem_cache_free(uhci_qh_cachep
, qh
);
327 static void uhci_insert_qh(struct uhci
*uhci
, struct uhci_qh
*skelqh
, struct uhci_qh
*qh
)
331 spin_lock_irqsave(&uhci
->framelist_lock
, flags
);
333 /* Fix the linked list pointers */
334 qh
->nextqh
= skelqh
->nextqh
;
337 skelqh
->nextqh
->prevqh
= qh
;
340 qh
->link
= skelqh
->link
;
341 skelqh
->link
= virt_to_bus(qh
) | UHCI_PTR_QH
;
343 spin_unlock_irqrestore(&uhci
->framelist_lock
, flags
);
346 static void uhci_remove_qh(struct uhci
*uhci
, struct uhci_qh
*qh
)
351 /* If the QH isn't queued, then we don't need to delay unlink it */
352 delayed
= (qh
->prevqh
|| qh
->nextqh
);
354 spin_lock_irqsave(&uhci
->framelist_lock
, flags
);
356 qh
->prevqh
->nextqh
= qh
->nextqh
;
357 qh
->prevqh
->link
= qh
->link
;
360 qh
->nextqh
->prevqh
= qh
->prevqh
;
361 qh
->prevqh
= qh
->nextqh
= NULL
;
362 qh
->element
= qh
->link
= UHCI_PTR_TERM
;
363 spin_unlock_irqrestore(&uhci
->framelist_lock
, flags
);
366 spin_lock_irqsave(&uhci
->qh_remove_lock
, flags
);
368 /* Check to see if the remove list is empty */
369 /* Set the IOC bit to force an interrupt so we can remove the QH */
370 if (list_empty(&uhci
->qh_remove_list
))
371 uhci_set_next_interrupt(uhci
);
374 list_add(&qh
->remove_list
, &uhci
->qh_remove_list
);
376 spin_unlock_irqrestore(&uhci
->qh_remove_lock
, flags
);
381 static spinlock_t uhci_append_urb_lock
= SPIN_LOCK_UNLOCKED
;
383 /* This function will append one URB's QH to another URB's QH. This is for */
384 /* USB_QUEUE_BULK support */
385 static void uhci_append_queued_urb(struct uhci
*uhci
, struct urb
*eurb
, struct urb
*urb
)
387 struct urb_priv
*eurbp
, *urbp
, *furbp
, *lurbp
;
388 struct list_head
*tmp
;
389 struct uhci_td
*td
, *ltd
;
392 eurbp
= eurb
->hcpriv
;
395 spin_lock_irqsave(&uhci_append_urb_lock
, flags
);
397 /* Find the beginning URB in the queue */
399 struct list_head
*head
= &eurbp
->urb_queue_list
;
402 while (tmp
!= head
) {
403 struct urb_priv
*turbp
=
404 list_entry(tmp
, struct urb_priv
, urb_queue_list
);
412 tmp
= &eurbp
->urb_queue_list
;
414 furbp
= list_entry(tmp
, struct urb_priv
, urb_queue_list
);
416 tmp
= furbp
->urb_queue_list
.prev
;
417 lurbp
= list_entry(tmp
, struct urb_priv
, urb_queue_list
);
419 /* Add this one to the end */
420 list_add_tail(&urbp
->urb_queue_list
, &furbp
->urb_queue_list
);
422 /* Grab the last TD from the last URB */
423 ltd
= list_entry(lurbp
->list
.prev
, struct uhci_td
, list
);
425 /* Grab the first TD from the first URB */
426 td
= list_entry(urbp
->list
.next
, struct uhci_td
, list
);
428 /* No breadth since this will only be called for bulk transfers */
429 ltd
->link
= virt_to_bus(td
);
431 spin_unlock_irqrestore(&uhci_append_urb_lock
, flags
);
434 static void uhci_delete_queued_urb(struct uhci
*uhci
, struct urb
*urb
)
436 struct urb_priv
*urbp
, *nurbp
;
441 spin_lock_irqsave(&uhci_append_urb_lock
, flags
);
443 nurbp
= list_entry(urbp
->urb_queue_list
.next
, struct urb_priv
,
447 /* We're the head, so just insert the QH for the next URB */
448 uhci_insert_qh(uhci
, &uhci
->skel_bulk_qh
, nurbp
->qh
);
451 struct urb_priv
*purbp
;
454 /* We're somewhere in the middle (or end). A bit trickier */
455 /* than the head scenario */
456 purbp
= list_entry(urbp
->urb_queue_list
.prev
, struct urb_priv
,
459 ptd
= list_entry(purbp
->list
.prev
, struct uhci_td
, list
);
461 /* Close the gap between the two */
462 ptd
->link
= virt_to_bus(list_entry(nurbp
->list
.next
,
463 struct uhci_td
, list
));
465 /* The next URB happens to be the beggining, so */
466 /* we're the last, end the chain */
467 ptd
->link
= UHCI_PTR_TERM
;
471 list_del(&urbp
->urb_queue_list
);
473 spin_unlock_irqrestore(&uhci_append_urb_lock
, flags
);
476 struct urb_priv
*uhci_alloc_urb_priv(struct urb
*urb
)
478 struct urb_priv
*urbp
;
480 urbp
= kmem_cache_alloc(uhci_up_cachep
, in_interrupt() ? SLAB_ATOMIC
: SLAB_KERNEL
);
484 memset((void *)urbp
, 0, sizeof(*urbp
));
486 urbp
->inserttime
= jiffies
;
489 INIT_LIST_HEAD(&urbp
->list
);
490 INIT_LIST_HEAD(&urbp
->urb_queue_list
);
494 usb_inc_dev_use(urb
->dev
);
499 static void uhci_add_td_to_urb(struct urb
*urb
, struct uhci_td
*td
)
501 struct urb_priv
*urbp
= (struct urb_priv
*)urb
->hcpriv
;
505 list_add_tail(&td
->list
, &urbp
->list
);
508 static void uhci_remove_td_from_urb(struct urb
*urb
, struct uhci_td
*td
)
510 urb
= NULL
; /* No warnings */
512 if (list_empty(&td
->list
))
516 INIT_LIST_HEAD(&td
->list
);
521 static void uhci_destroy_urb_priv(struct urb
*urb
)
523 struct list_head
*tmp
, *head
;
524 struct urb_priv
*urbp
;
529 spin_lock_irqsave(&urb
->lock
, flags
);
531 urbp
= (struct urb_priv
*)urb
->hcpriv
;
535 if (!urb
->dev
|| !urb
->dev
->bus
|| !urb
->dev
->bus
->hcpriv
)
538 uhci
= urb
->dev
->bus
->hcpriv
;
542 while (tmp
!= head
) {
543 td
= list_entry(tmp
, struct uhci_td
, list
);
547 uhci_remove_td_from_urb(urb
, td
);
549 uhci_remove_td(uhci
, td
);
555 kmem_cache_free(uhci_up_cachep
, urbp
);
557 usb_dec_dev_use(urb
->dev
);
560 spin_unlock_irqrestore(&urb
->lock
, flags
);
563 static void uhci_inc_fsbr(struct uhci
*uhci
, struct urb
*urb
)
566 struct urb_priv
*urbp
= (struct urb_priv
*)urb
->hcpriv
;
571 spin_lock_irqsave(&uhci
->framelist_lock
, flags
);
576 uhci
->skel_term_qh
.link
= virt_to_bus(&uhci
->skel_hs_control_qh
) | UHCI_PTR_QH
;
579 spin_unlock_irqrestore(&uhci
->framelist_lock
, flags
);
582 static void uhci_dec_fsbr(struct uhci
*uhci
, struct urb
*urb
)
585 struct urb_priv
*urbp
= (struct urb_priv
*)urb
->hcpriv
;
590 spin_lock_irqsave(&uhci
->framelist_lock
, flags
);
595 uhci
->skel_term_qh
.link
= UHCI_PTR_TERM
;
598 spin_unlock_irqrestore(&uhci
->framelist_lock
, flags
);
602 * Map status to standard result codes
604 * <status> is (td->status & 0xFE0000) [a.k.a. uhci_status_bits(td->status)]
605 * <dir_out> is True for output TDs and False for input TDs.
607 static int uhci_map_status(int status
, int dir_out
)
611 if (status
& TD_CTRL_BITSTUFF
) /* Bitstuff error */
613 if (status
& TD_CTRL_CRCTIMEO
) { /* CRC/Timeout */
619 if (status
& TD_CTRL_NAK
) /* NAK */
621 if (status
& TD_CTRL_BABBLE
) /* Babble */
623 if (status
& TD_CTRL_DBUFERR
) /* Buffer error */
625 if (status
& TD_CTRL_STALLED
) /* Stalled */
627 if (status
& TD_CTRL_ACTIVE
) /* Active */
636 static int uhci_submit_control(struct urb
*urb
)
638 struct urb_priv
*urbp
= (struct urb_priv
*)urb
->hcpriv
;
639 struct uhci
*uhci
= (struct uhci
*)urb
->dev
->bus
->hcpriv
;
642 unsigned long destination
, status
;
643 int maxsze
= usb_maxpacket(urb
->dev
, urb
->pipe
, usb_pipeout(urb
->pipe
));
644 int len
= urb
->transfer_buffer_length
;
645 unsigned char *data
= urb
->transfer_buffer
;
647 /* The "pipe" thing contains the destination in bits 8--18 */
648 destination
= (urb
->pipe
& PIPE_DEVEP_MASK
) | USB_PID_SETUP
;
651 status
= (urb
->pipe
& TD_CTRL_LS
) | TD_CTRL_ACTIVE
| (3 << 27);
654 * Build the TD for the control request
656 td
= uhci_alloc_td(urb
->dev
);
660 uhci_add_td_to_urb(urb
, td
);
661 uhci_fill_td(td
, status
, destination
| (7 << 21),
662 virt_to_bus(urb
->setup_packet
));
665 * If direction is "send", change the frame from SETUP (0x2D)
666 * to OUT (0xE1). Else change it from SETUP to IN (0x69).
668 destination
^= (USB_PID_SETUP
^ usb_packetid(urb
->pipe
));
670 if (!(urb
->transfer_flags
& USB_DISABLE_SPD
))
671 status
|= TD_CTRL_SPD
;
674 * Build the DATA TD's
682 td
= uhci_alloc_td(urb
->dev
);
686 /* Alternate Data0/1 (start with Data1) */
687 destination
^= 1 << TD_TOKEN_TOGGLE
;
689 uhci_add_td_to_urb(urb
, td
);
690 uhci_fill_td(td
, status
, destination
| ((pktsze
- 1) << 21),
698 * Build the final TD for control status
700 td
= uhci_alloc_td(urb
->dev
);
705 * It's IN if the pipe is an output pipe or we're not expecting
708 destination
&= ~TD_PID
;
709 if (usb_pipeout(urb
->pipe
) || !urb
->transfer_buffer_length
)
710 destination
|= USB_PID_IN
;
712 destination
|= USB_PID_OUT
;
714 destination
|= 1 << TD_TOKEN_TOGGLE
; /* End in Data1 */
716 status
&= ~TD_CTRL_SPD
;
718 uhci_add_td_to_urb(urb
, td
);
719 uhci_fill_td(td
, status
| TD_CTRL_IOC
,
720 destination
| (UHCI_NULL_DATA_SIZE
<< 21), 0);
722 qh
= uhci_alloc_qh(urb
->dev
);
726 /* Low speed or small transfers gets a different queue and treatment */
727 if (urb
->pipe
& TD_CTRL_LS
) {
728 uhci_insert_tds_in_qh(qh
, urb
, 0);
729 uhci_insert_qh(uhci
, &uhci
->skel_ls_control_qh
, qh
);
731 uhci_insert_tds_in_qh(qh
, urb
, 1);
732 uhci_insert_qh(uhci
, &uhci
->skel_hs_control_qh
, qh
);
733 uhci_inc_fsbr(uhci
, urb
);
738 uhci_add_urb_list(uhci
, urb
);
740 usb_inc_dev_use(urb
->dev
);
745 static int usb_control_retrigger_status(struct urb
*urb
);
747 static int uhci_result_control(struct urb
*urb
)
749 struct list_head
*tmp
, *head
;
750 struct urb_priv
*urbp
= urb
->hcpriv
;
759 if (head
->next
== head
)
762 if (urbp
->short_control_packet
) {
768 td
= list_entry(tmp
, struct uhci_td
, list
);
770 /* The first TD is the SETUP phase, check the status, but skip */
772 status
= uhci_status_bits(td
->status
);
773 if (status
& TD_CTRL_ACTIVE
)
779 urb
->actual_length
= 0;
781 /* The rest of the TD's (but the last) are data */
783 while (tmp
!= head
&& tmp
->next
!= head
) {
784 td
= list_entry(tmp
, struct uhci_td
, list
);
788 if (urbp
->fsbr_timeout
&& (td
->status
& TD_CTRL_IOC
) &&
789 !(td
->status
& TD_CTRL_ACTIVE
)) {
790 uhci_inc_fsbr(urb
->dev
->bus
->hcpriv
, urb
);
791 urbp
->fsbr_timeout
= 0;
792 td
->status
&= ~TD_CTRL_IOC
;
795 status
= uhci_status_bits(td
->status
);
796 if (status
& TD_CTRL_ACTIVE
)
799 urb
->actual_length
+= uhci_actual_length(td
->status
);
804 /* Check to see if we received a short packet */
805 if (uhci_actual_length(td
->status
) < uhci_expected_length(td
->info
)) {
806 if (urb
->transfer_flags
& USB_DISABLE_SPD
) {
811 if (uhci_packetid(td
->info
) == USB_PID_IN
)
812 return usb_control_retrigger_status(urb
);
819 td
= list_entry(tmp
, struct uhci_td
, list
);
821 /* Control status phase */
822 status
= uhci_status_bits(td
->status
);
824 #ifdef I_HAVE_BUGGY_APC_BACKUPS
825 /* APC BackUPS Pro kludge */
826 /* It tries to send all of the descriptor instead of the amount */
828 if (td
->status
& TD_CTRL_IOC
&& /* IOC is masked out by uhci_status_bits */
829 status
& TD_CTRL_ACTIVE
&&
830 status
& TD_CTRL_NAK
)
834 if (status
& TD_CTRL_ACTIVE
)
843 ret
= uhci_map_status(status
, uhci_packetout(td
->info
));
845 /* endpoint has stalled - mark it halted */
846 usb_endpoint_halt(urb
->dev
, uhci_endpoint(td
->info
),
847 uhci_packetout(td
->info
));
850 if (debug
&& ret
!= -EPIPE
) {
851 /* Some debugging code */
852 dbg("uhci_result_control() failed with status %x", status
);
854 /* Print the chain for debugging purposes */
855 uhci_show_urb_queue(urb
);
861 static int usb_control_retrigger_status(struct urb
*urb
)
863 struct list_head
*tmp
, *head
;
864 struct urb_priv
*urbp
= (struct urb_priv
*)urb
->hcpriv
;
865 struct uhci
*uhci
= urb
->dev
->bus
->hcpriv
;
867 urbp
->short_control_packet
= 1;
869 /* Create a new QH to avoid pointer overwriting problems */
870 uhci_remove_qh(uhci
, urbp
->qh
);
872 /* Delete all of the TD's except for the status TD at the end */
875 while (tmp
!= head
&& tmp
->next
!= head
) {
876 struct uhci_td
*td
= list_entry(tmp
, struct uhci_td
, list
);
880 uhci_remove_td_from_urb(urb
, td
);
882 uhci_remove_td(uhci
, td
);
887 urbp
->qh
= uhci_alloc_qh(urb
->dev
);
889 err("unable to allocate new QH for control retrigger");
893 /* One TD, who cares about Breadth first? */
894 uhci_insert_tds_in_qh(urbp
->qh
, urb
, 0);
896 /* Low speed or small transfers gets a different queue and treatment */
897 if (urb
->pipe
& TD_CTRL_LS
)
898 uhci_insert_qh(uhci
, &uhci
->skel_ls_control_qh
, urbp
->qh
);
900 uhci_insert_qh(uhci
, &uhci
->skel_hs_control_qh
, urbp
->qh
);
906 * Interrupt transfers
908 static int uhci_submit_interrupt(struct urb
*urb
)
911 unsigned long destination
, status
;
912 struct uhci
*uhci
= (struct uhci
*)urb
->dev
->bus
->hcpriv
;
914 if (urb
->transfer_buffer_length
> usb_maxpacket(urb
->dev
, urb
->pipe
, usb_pipeout(urb
->pipe
)))
917 /* The "pipe" thing contains the destination in bits 8--18 */
918 destination
= (urb
->pipe
& PIPE_DEVEP_MASK
) | usb_packetid(urb
->pipe
);
920 status
= (urb
->pipe
& TD_CTRL_LS
) | TD_CTRL_ACTIVE
| TD_CTRL_IOC
;
922 td
= uhci_alloc_td(urb
->dev
);
926 destination
|= (usb_gettoggle(urb
->dev
, usb_pipeendpoint(urb
->pipe
), usb_pipeout(urb
->pipe
)) << TD_TOKEN_TOGGLE
);
927 destination
|= ((urb
->transfer_buffer_length
- 1) << 21);
929 usb_dotoggle(urb
->dev
, usb_pipeendpoint(urb
->pipe
), usb_pipeout(urb
->pipe
));
931 uhci_add_td_to_urb(urb
, td
);
932 uhci_fill_td(td
, status
, destination
,
933 virt_to_bus(urb
->transfer_buffer
));
935 uhci_insert_td(uhci
, &uhci
->skeltd
[__interval_to_skel(urb
->interval
)], td
);
937 uhci_add_urb_list(uhci
, urb
);
942 static int uhci_result_interrupt(struct urb
*urb
)
944 struct list_head
*tmp
, *head
;
945 struct urb_priv
*urbp
= urb
->hcpriv
;
953 urb
->actual_length
= 0;
957 while (tmp
!= head
) {
958 td
= list_entry(tmp
, struct uhci_td
, list
);
962 if (urbp
->fsbr_timeout
&& (td
->status
& TD_CTRL_IOC
) &&
963 !(td
->status
& TD_CTRL_ACTIVE
)) {
964 uhci_inc_fsbr(urb
->dev
->bus
->hcpriv
, urb
);
965 urbp
->fsbr_timeout
= 0;
966 td
->status
&= ~TD_CTRL_IOC
;
969 status
= uhci_status_bits(td
->status
);
970 if (status
& TD_CTRL_ACTIVE
)
973 urb
->actual_length
+= uhci_actual_length(td
->status
);
978 if (uhci_actual_length(td
->status
) < uhci_expected_length(td
->info
)) {
979 usb_settoggle(urb
->dev
, uhci_endpoint(td
->info
),
980 uhci_packetout(td
->info
),
981 uhci_toggle(td
->info
) ^ 1);
983 if (urb
->transfer_flags
& USB_DISABLE_SPD
) {
994 ret
= uhci_map_status(status
, uhci_packetout(td
->info
));
996 /* endpoint has stalled - mark it halted */
997 usb_endpoint_halt(urb
->dev
, uhci_endpoint(td
->info
),
998 uhci_packetout(td
->info
));
1001 if (debug
&& ret
!= -EPIPE
) {
1002 /* Some debugging code */
1003 dbg("uhci_result_interrupt/bulk() failed with status %x",
1006 /* Print the chain for debugging purposes */
1008 uhci_show_urb_queue(urb
);
1016 static void uhci_reset_interrupt(struct urb
*urb
)
1018 struct list_head
*tmp
;
1019 struct urb_priv
*urbp
= (struct urb_priv
*)urb
->hcpriv
;
1025 tmp
= urbp
->list
.next
;
1026 td
= list_entry(tmp
, struct uhci_td
, list
);
1030 td
->status
= (td
->status
& 0x2F000000) | TD_CTRL_ACTIVE
| TD_CTRL_IOC
;
1031 td
->info
&= ~(1 << TD_TOKEN_TOGGLE
);
1032 td
->info
|= (usb_gettoggle(urb
->dev
, usb_pipeendpoint(urb
->pipe
), usb_pipeout(urb
->pipe
)) << TD_TOKEN_TOGGLE
);
1033 usb_dotoggle(urb
->dev
, usb_pipeendpoint(urb
->pipe
), usb_pipeout(urb
->pipe
));
1035 urb
->status
= -EINPROGRESS
;
1041 static int uhci_submit_bulk(struct urb
*urb
, struct urb
*eurb
)
1045 unsigned long destination
, status
;
1046 struct uhci
*uhci
= (struct uhci
*)urb
->dev
->bus
->hcpriv
;
1047 int maxsze
= usb_maxpacket(urb
->dev
, urb
->pipe
, usb_pipeout(urb
->pipe
));
1048 int len
= urb
->transfer_buffer_length
;
1049 unsigned char *data
= urb
->transfer_buffer
;
1050 struct urb_priv
*urbp
= (struct urb_priv
*)urb
->hcpriv
;
1055 /* Can't have low speed bulk transfers */
1056 if (urb
->pipe
& TD_CTRL_LS
)
1059 /* The "pipe" thing contains the destination in bits 8--18 */
1060 destination
= (urb
->pipe
& PIPE_DEVEP_MASK
) | usb_packetid(urb
->pipe
);
1063 status
= TD_CTRL_ACTIVE
| (3 << TD_CTRL_C_ERR_SHIFT
);
1065 if (!(urb
->transfer_flags
& USB_DISABLE_SPD
))
1066 status
|= TD_CTRL_SPD
;
1069 * Build the DATA TD's
1071 do { /* Allow zero length packets */
1074 if (pktsze
> maxsze
)
1077 td
= uhci_alloc_td(urb
->dev
);
1081 uhci_add_td_to_urb(urb
, td
);
1082 uhci_fill_td(td
, status
, destination
| ((pktsze
- 1) << 21) |
1083 (usb_gettoggle(urb
->dev
, usb_pipeendpoint(urb
->pipe
),
1084 usb_pipeout(urb
->pipe
)) << TD_TOKEN_TOGGLE
),
1091 td
->status
|= TD_CTRL_IOC
;
1093 usb_dotoggle(urb
->dev
, usb_pipeendpoint(urb
->pipe
),
1094 usb_pipeout(urb
->pipe
));
1097 qh
= uhci_alloc_qh(urb
->dev
);
1103 /* Always assume depth first */
1104 uhci_insert_tds_in_qh(qh
, urb
, 1);
1106 if (urb
->transfer_flags
& USB_QUEUE_BULK
&& eurb
) {
1108 uhci_append_queued_urb(uhci
, eurb
, urb
);
1110 uhci_insert_qh(uhci
, &uhci
->skel_bulk_qh
, qh
);
1112 uhci_add_urb_list(uhci
, urb
);
1114 uhci_inc_fsbr(uhci
, urb
);
1116 return -EINPROGRESS
;
1119 /* We can use the result interrupt since they're identical */
1120 #define uhci_result_bulk uhci_result_interrupt
1123 * Isochronous transfers
1125 static int isochronous_find_limits(struct urb
*urb
, unsigned int *start
, unsigned int *end
)
1127 struct urb
*last_urb
= NULL
;
1128 struct uhci
*uhci
= (struct uhci
*)urb
->dev
->bus
->hcpriv
;
1129 struct list_head
*tmp
, *head
= &uhci
->urb_list
;
1131 unsigned long flags
;
1133 nested_lock(&uhci
->urblist_lock
, flags
);
1135 while (tmp
!= head
) {
1136 struct urb
*u
= list_entry(tmp
, struct urb
, urb_list
);
1140 /* look for pending URB's with identical pipe handle */
1141 if ((urb
->pipe
== u
->pipe
) && (urb
->dev
== u
->dev
) &&
1142 (u
->status
== -EINPROGRESS
) && (u
!= urb
)) {
1144 *start
= u
->start_frame
;
1150 *end
= (last_urb
->start_frame
+ last_urb
->number_of_packets
) & 1023;
1153 ret
= -1; /* no previous urb found */
1155 nested_unlock(&uhci
->urblist_lock
, flags
);
1160 static int isochronous_find_start(struct urb
*urb
)
1163 unsigned int start
= 0, end
= 0;
1165 if (urb
->number_of_packets
> 900) /* 900? Why? */
1168 limits
= isochronous_find_limits(urb
, &start
, &end
);
1170 if (urb
->transfer_flags
& USB_ISO_ASAP
) {
1174 curframe
= uhci_get_current_frame_number(urb
->dev
) % UHCI_NUMFRAMES
;
1175 urb
->start_frame
= (curframe
+ 10) % UHCI_NUMFRAMES
;
1177 urb
->start_frame
= end
;
1179 urb
->start_frame
%= UHCI_NUMFRAMES
;
1180 /* FIXME: Sanity check */
1186 static int uhci_submit_isochronous(struct urb
*urb
)
1189 struct uhci
*uhci
= (struct uhci
*)urb
->dev
->bus
->hcpriv
;
1190 int i
, ret
, framenum
;
1191 int status
, destination
;
1193 status
= TD_CTRL_ACTIVE
| TD_CTRL_IOS
;
1194 destination
= (urb
->pipe
& PIPE_DEVEP_MASK
) | usb_packetid(urb
->pipe
);
1196 ret
= isochronous_find_start(urb
);
1200 framenum
= urb
->start_frame
;
1201 for (i
= 0; i
< urb
->number_of_packets
; i
++, framenum
++) {
1202 if (!urb
->iso_frame_desc
[i
].length
)
1205 td
= uhci_alloc_td(urb
->dev
);
1209 uhci_add_td_to_urb(urb
, td
);
1210 uhci_fill_td(td
, status
, destination
| ((urb
->iso_frame_desc
[i
].length
- 1) << 21),
1211 virt_to_bus(urb
->transfer_buffer
+ urb
->iso_frame_desc
[i
].offset
));
1213 if (i
+ 1 >= urb
->number_of_packets
)
1214 td
->status
|= TD_CTRL_IOC
;
1216 uhci_insert_td_frame_list(uhci
, td
, framenum
);
1219 uhci_add_urb_list(uhci
, urb
);
1221 return -EINPROGRESS
;
1224 static int uhci_result_isochronous(struct urb
*urb
)
1226 struct list_head
*tmp
, *head
;
1227 struct urb_priv
*urbp
= (struct urb_priv
*)urb
->hcpriv
;
1234 urb
->actual_length
= 0;
1239 while (tmp
!= head
) {
1240 struct uhci_td
*td
= list_entry(tmp
, struct uhci_td
, list
);
1245 if (td
->status
& TD_CTRL_ACTIVE
)
1246 return -EINPROGRESS
;
1248 actlength
= uhci_actual_length(td
->status
);
1249 urb
->iso_frame_desc
[i
].actual_length
= actlength
;
1250 urb
->actual_length
+= actlength
;
1252 status
= uhci_map_status(uhci_status_bits(td
->status
), usb_pipeout(urb
->pipe
));
1253 urb
->iso_frame_desc
[i
].status
= status
;
1265 static struct urb
*uhci_find_urb_ep(struct uhci
*uhci
, struct urb
*urb
)
1267 struct list_head
*tmp
, *head
= &uhci
->urb_list
;
1268 unsigned long flags
;
1269 struct urb
*u
= NULL
;
1271 if (usb_pipeisoc(urb
->pipe
))
1274 nested_lock(&uhci
->urblist_lock
, flags
);
1276 while (tmp
!= head
) {
1277 u
= list_entry(tmp
, struct urb
, urb_list
);
1281 if (u
->dev
== urb
->dev
&&
1282 u
->pipe
== urb
->pipe
)
1288 nested_unlock(&uhci
->urblist_lock
, flags
);
1293 static int uhci_submit_urb(struct urb
*urb
)
1297 unsigned long flags
;
1303 if (!urb
->dev
|| !urb
->dev
->bus
|| !urb
->dev
->bus
->hcpriv
)
1306 uhci
= (struct uhci
*)urb
->dev
->bus
->hcpriv
;
1308 /* Short circuit the virtual root hub */
1309 if (usb_pipedevice(urb
->pipe
) == uhci
->rh
.devnum
)
1310 return rh_submit_urb(urb
);
1312 u
= uhci_find_urb_ep(uhci
, urb
);
1313 if (u
&& !(urb
->transfer_flags
& USB_QUEUE_BULK
))
1316 spin_lock_irqsave(&urb
->lock
, flags
);
1318 if (!uhci_alloc_urb_priv(urb
)) {
1319 spin_unlock_irqrestore(&urb
->lock
, flags
);
1323 switch (usb_pipetype(urb
->pipe
)) {
1325 ret
= uhci_submit_control(urb
);
1327 case PIPE_INTERRUPT
:
1328 ret
= uhci_submit_interrupt(urb
);
1331 ret
= uhci_submit_bulk(urb
, u
);
1333 case PIPE_ISOCHRONOUS
:
1334 ret
= uhci_submit_isochronous(urb
);
1340 spin_unlock_irqrestore(&urb
->lock
, flags
);
1342 if (ret
== -EINPROGRESS
)
1345 uhci_unlink_generic(urb
);
1351 * Return the result of a transfer
1353 * Must be called with urblist_lock acquired
1355 static void uhci_transfer_result(struct urb
*urb
)
1358 int proceed
= 0, is_ring
= 0;
1360 unsigned long flags
;
1362 spin_lock_irqsave(&urb
->lock
, flags
);
1364 switch (usb_pipetype(urb
->pipe
)) {
1366 ret
= uhci_result_control(urb
);
1368 case PIPE_INTERRUPT
:
1369 ret
= uhci_result_interrupt(urb
);
1372 ret
= uhci_result_bulk(urb
);
1374 case PIPE_ISOCHRONOUS
:
1375 ret
= uhci_result_isochronous(urb
);
1381 spin_unlock_irqrestore(&urb
->lock
, flags
);
1383 if (ret
== -EINPROGRESS
)
1386 switch (usb_pipetype(urb
->pipe
)) {
1389 case PIPE_ISOCHRONOUS
:
1390 uhci_unlink_generic(urb
);
1392 case PIPE_INTERRUPT
:
1393 /* Interrupts are an exception */
1396 uhci_reset_interrupt(urb
);
1398 uhci_unlink_generic(urb
);
1399 return; /* <-- Note the return */
1405 if (turb
->status
!= -EINPROGRESS
) {
1411 } while (turb
&& turb
!= urb
&& turb
!= urb
->next
);
1413 if (turb
== urb
|| turb
== urb
->next
)
1417 if (urb
->complete
&& (!proceed
|| (urb
->transfer_flags
& USB_URB_EARLY_COMPLETE
))) {
1419 if (!proceed
&& is_ring
)
1420 uhci_submit_urb(urb
);
1423 if (proceed
&& urb
->next
) {
1426 if (turb
->status
!= -EINPROGRESS
&&
1427 uhci_submit_urb(turb
) != 0)
1430 } while (turb
&& turb
!= urb
->next
);
1432 if (urb
->complete
&& !(urb
->transfer_flags
& USB_URB_EARLY_COMPLETE
))
1437 static int uhci_unlink_generic(struct urb
*urb
)
1439 struct urb_priv
*urbp
= urb
->hcpriv
;
1440 struct uhci
*uhci
= (struct uhci
*)urb
->dev
->bus
->hcpriv
;
1445 uhci_dec_fsbr(uhci
, urb
); /* Safe since it checks */
1447 uhci_remove_urb_list(uhci
, urb
);
1450 /* The interrupt loop will reclaim the QH's */
1451 uhci_remove_qh(uhci
, urbp
->qh
);
1453 if (!list_empty(&urbp
->urb_queue_list
))
1454 uhci_delete_queued_urb(uhci
, urb
);
1456 uhci_destroy_urb_priv(urb
);
1461 static int uhci_unlink_urb(struct urb
*urb
)
1465 unsigned long flags
;
1470 if (!urb
->dev
|| !urb
->dev
->bus
)
1473 uhci
= (struct uhci
*)urb
->dev
->bus
->hcpriv
;
1475 /* Short circuit the virtual root hub */
1476 if (usb_pipedevice(urb
->pipe
) == uhci
->rh
.devnum
)
1477 return rh_unlink_urb(urb
);
1479 if (urb
->status
== -EINPROGRESS
) {
1480 uhci_unlink_generic(urb
);
1482 if (urb
->transfer_flags
& USB_ASYNC_UNLINK
) {
1483 urb
->status
= -ECONNABORTED
;
1485 spin_lock_irqsave(&uhci
->urb_remove_lock
, flags
);
1487 /* Check to see if the remove list is empty */
1488 if (list_empty(&uhci
->urb_remove_list
))
1489 uhci_set_next_interrupt(uhci
);
1491 list_add(&urb
->urb_list
, &uhci
->urb_remove_list
);
1493 spin_unlock_irqrestore(&uhci
->urb_remove_lock
, flags
);
1495 urb
->status
= -ENOENT
;
1497 if (in_interrupt()) { /* wait at least 1 frame */
1498 static int errorcount
= 10;
1501 dbg("uhci_unlink_urb called from interrupt for urb %p", urb
);
1504 schedule_timeout(1+1*HZ
/1000);
1514 static int uhci_fsbr_timeout(struct uhci
*uhci
, struct urb
*urb
)
1516 struct urb_priv
*urbp
= (struct urb_priv
*)urb
->hcpriv
;
1517 struct list_head
*head
, *tmp
;
1519 uhci_dec_fsbr(uhci
, urb
);
1521 /* There is a race with updating IOC in here, but it's not worth */
1522 /* trying to fix since this is merely an optimization. The only */
1523 /* time we'd lose is if the status of the packet got updated */
1524 /* and we'd be turning on FSBR next frame anyway, so it's a wash */
1525 urbp
->fsbr_timeout
= 1;
1529 while (tmp
!= head
) {
1530 struct uhci_td
*td
= list_entry(tmp
, struct uhci_td
, list
);
1534 if (td
->status
& TD_CTRL_ACTIVE
) {
1535 td
->status
|= TD_CTRL_IOC
;
1544 * uhci_get_current_frame_number()
1546 * returns the current frame number for a USB bus/controller.
1548 static int uhci_get_current_frame_number(struct usb_device
*dev
)
1550 struct uhci
*uhci
= (struct uhci
*)dev
->bus
->hcpriv
;
1552 return inw(uhci
->io_addr
+ USBFRNUM
);
1555 struct usb_operations uhci_device_operations
= {
1558 uhci_get_current_frame_number
,
1563 /* -------------------------------------------------------------------
1565 ------------------------------------------------------------------- */
1567 static __u8 root_hub_dev_des
[] =
1569 0x12, /* __u8 bLength; */
1570 0x01, /* __u8 bDescriptorType; Device */
1571 0x00, /* __u16 bcdUSB; v1.0 */
1573 0x09, /* __u8 bDeviceClass; HUB_CLASSCODE */
1574 0x00, /* __u8 bDeviceSubClass; */
1575 0x00, /* __u8 bDeviceProtocol; */
1576 0x08, /* __u8 bMaxPacketSize0; 8 Bytes */
1577 0x00, /* __u16 idVendor; */
1579 0x00, /* __u16 idProduct; */
1581 0x00, /* __u16 bcdDevice; */
1583 0x00, /* __u8 iManufacturer; */
1584 0x02, /* __u8 iProduct; */
1585 0x01, /* __u8 iSerialNumber; */
1586 0x01 /* __u8 bNumConfigurations; */
1590 /* Configuration descriptor */
1591 static __u8 root_hub_config_des
[] =
1593 0x09, /* __u8 bLength; */
1594 0x02, /* __u8 bDescriptorType; Configuration */
1595 0x19, /* __u16 wTotalLength; */
1597 0x01, /* __u8 bNumInterfaces; */
1598 0x01, /* __u8 bConfigurationValue; */
1599 0x00, /* __u8 iConfiguration; */
1600 0x40, /* __u8 bmAttributes;
1601 Bit 7: Bus-powered, 6: Self-powered,
1602 Bit 5 Remote-wakeup, 4..0: resvd */
1603 0x00, /* __u8 MaxPower; */
1606 0x09, /* __u8 if_bLength; */
1607 0x04, /* __u8 if_bDescriptorType; Interface */
1608 0x00, /* __u8 if_bInterfaceNumber; */
1609 0x00, /* __u8 if_bAlternateSetting; */
1610 0x01, /* __u8 if_bNumEndpoints; */
1611 0x09, /* __u8 if_bInterfaceClass; HUB_CLASSCODE */
1612 0x00, /* __u8 if_bInterfaceSubClass; */
1613 0x00, /* __u8 if_bInterfaceProtocol; */
1614 0x00, /* __u8 if_iInterface; */
1617 0x07, /* __u8 ep_bLength; */
1618 0x05, /* __u8 ep_bDescriptorType; Endpoint */
1619 0x81, /* __u8 ep_bEndpointAddress; IN Endpoint 1 */
1620 0x03, /* __u8 ep_bmAttributes; Interrupt */
1621 0x08, /* __u16 ep_wMaxPacketSize; 8 Bytes */
1623 0xff /* __u8 ep_bInterval; 255 ms */
1626 static __u8 root_hub_hub_des
[] =
1628 0x09, /* __u8 bLength; */
1629 0x29, /* __u8 bDescriptorType; Hub-descriptor */
1630 0x02, /* __u8 bNbrPorts; */
1631 0x00, /* __u16 wHubCharacteristics; */
1633 0x01, /* __u8 bPwrOn2pwrGood; 2ms */
1634 0x00, /* __u8 bHubContrCurrent; 0 mA */
1635 0x00, /* __u8 DeviceRemovable; *** 7 Ports max *** */
1636 0xff /* __u8 PortPwrCtrlMask; *** 7 ports max *** */
1639 /*-------------------------------------------------------------------------*/
1640 /* prepare Interrupt pipe transaction data; HUB INTERRUPT ENDPOINT */
1641 static int rh_send_irq(struct urb
*urb
)
1644 struct uhci
*uhci
= (struct uhci
*)urb
->dev
->bus
->hcpriv
;
1645 unsigned int io_addr
= uhci
->io_addr
;
1648 for (i
= 0; i
< uhci
->rh
.numports
; i
++) {
1649 data
|= ((inw(io_addr
+ USBPORTSC1
+ i
* 2) & 0xa) > 0 ? (1 << (i
+ 1)) : 0);
1650 len
= (i
+ 1) / 8 + 1;
1653 *(__u16
*) urb
->transfer_buffer
= cpu_to_le16(data
);
1654 urb
->actual_length
= len
;
1655 urb
->status
= USB_ST_NOERROR
;
1657 if ((data
> 0) && (uhci
->rh
.send
!= 0)) {
1658 dbg("root-hub INT complete: port1: %x port2: %x data: %x",
1659 inw(io_addr
+ USBPORTSC1
), inw(io_addr
+ USBPORTSC2
), data
);
1663 return USB_ST_NOERROR
;
1666 /*-------------------------------------------------------------------------*/
1667 /* Virtual Root Hub INTs are polled by this timer every "interval" ms */
1668 static int rh_init_int_timer(struct urb
*urb
);
1670 static void rh_int_timer_do(unsigned long ptr
)
1672 struct urb
*urb
= (struct urb
*)ptr
;
1673 struct uhci
*uhci
= (struct uhci
*)urb
->dev
->bus
->hcpriv
;
1674 struct list_head
*tmp
, *head
= &uhci
->urb_list
;
1675 struct urb_priv
*urbp
;
1677 unsigned long flags
;
1679 if (uhci
->rh
.send
) {
1680 len
= rh_send_irq(urb
);
1682 urb
->actual_length
= len
;
1688 nested_lock(&uhci
->urblist_lock
, flags
);
1690 while (tmp
!= head
) {
1691 struct urb
*u
= list_entry(tmp
, urb_t
, urb_list
);
1695 urbp
= (struct urb_priv
*)u
->hcpriv
;
1697 /* Check if the FSBR timed out */
1698 if (urbp
->fsbr
&& time_after(urbp
->inserttime
+ IDLE_TIMEOUT
, jiffies
))
1699 uhci_fsbr_timeout(uhci
, u
);
1701 /* Check if the URB timed out */
1702 if (u
->timeout
&& time_after(u
->timeout
, jiffies
)) {
1703 u
->transfer_flags
|= USB_ASYNC_UNLINK
| USB_TIMEOUT_KILLED
;
1708 nested_unlock(&uhci
->urblist_lock
, flags
);
1710 rh_init_int_timer(urb
);
1713 /*-------------------------------------------------------------------------*/
1714 /* Root Hub INTs are polled by this timer */
1715 static int rh_init_int_timer(struct urb
*urb
)
1717 struct uhci
*uhci
= (struct uhci
*)urb
->dev
->bus
->hcpriv
;
1719 uhci
->rh
.interval
= urb
->interval
;
1720 init_timer(&uhci
->rh
.rh_int_timer
);
1721 uhci
->rh
.rh_int_timer
.function
= rh_int_timer_do
;
1722 uhci
->rh
.rh_int_timer
.data
= (unsigned long)urb
;
1723 uhci
->rh
.rh_int_timer
.expires
= jiffies
+ (HZ
* (urb
->interval
< 30 ? 30 : urb
->interval
)) / 1000;
1724 add_timer(&uhci
->rh
.rh_int_timer
);
1729 /*-------------------------------------------------------------------------*/
1730 #define OK(x) len = (x); break
1732 #define CLR_RH_PORTSTAT(x) \
1733 status = inw(io_addr + USBPORTSC1 + 2 * (wIndex-1)); \
1734 status = (status & 0xfff5) & ~(x); \
1735 outw(status, io_addr + USBPORTSC1 + 2 * (wIndex-1))
1737 #define SET_RH_PORTSTAT(x) \
1738 status = inw(io_addr + USBPORTSC1 + 2 * (wIndex-1)); \
1739 status = (status & 0xfff5) | (x); \
1740 outw(status, io_addr + USBPORTSC1 + 2 * (wIndex-1))
1743 /*-------------------------------------------------------------------------*/
1744 /*************************
1745 ** Root Hub Control Pipe
1746 *************************/
1748 static int rh_submit_urb(struct urb
*urb
)
1750 struct uhci
*uhci
= (struct uhci
*)urb
->dev
->bus
->hcpriv
;
1751 unsigned int pipe
= urb
->pipe
;
1752 devrequest
*cmd
= (devrequest
*)urb
->setup_packet
;
1753 void *data
= urb
->transfer_buffer
;
1754 int leni
= urb
->transfer_buffer_length
;
1757 int stat
= USB_ST_NOERROR
;
1759 unsigned int io_addr
= uhci
->io_addr
;
1766 if (usb_pipetype(pipe
) == PIPE_INTERRUPT
) {
1769 uhci
->rh
.interval
= urb
->interval
;
1770 rh_init_int_timer(urb
);
1772 return USB_ST_NOERROR
;
1775 bmRType_bReq
= cmd
->requesttype
| cmd
->request
<< 8;
1776 wValue
= le16_to_cpu(cmd
->value
);
1777 wIndex
= le16_to_cpu(cmd
->index
);
1778 wLength
= le16_to_cpu(cmd
->length
);
1780 for (i
= 0; i
< 8; i
++)
1781 uhci
->rh
.c_p_r
[i
] = 0;
1783 switch (bmRType_bReq
) {
1784 /* Request Destination:
1785 without flags: Device,
1786 RH_INTERFACE: interface,
1787 RH_ENDPOINT: endpoint,
1788 RH_CLASS means HUB here,
1789 RH_OTHER | RH_CLASS almost ever means HUB_PORT here
1793 *(__u16
*)data
= cpu_to_le16(1);
1795 case RH_GET_STATUS
| RH_INTERFACE
:
1796 *(__u16
*)data
= cpu_to_le16(0);
1798 case RH_GET_STATUS
| RH_ENDPOINT
:
1799 *(__u16
*)data
= cpu_to_le16(0);
1801 case RH_GET_STATUS
| RH_CLASS
:
1802 *(__u32
*)data
= cpu_to_le32(0);
1803 OK(4); /* hub power */
1804 case RH_GET_STATUS
| RH_OTHER
| RH_CLASS
:
1805 status
= inw(io_addr
+ USBPORTSC1
+ 2 * (wIndex
- 1));
1806 cstatus
= ((status
& USBPORTSC_CSC
) >> (1 - 0)) |
1807 ((status
& USBPORTSC_PEC
) >> (3 - 1)) |
1808 (uhci
->rh
.c_p_r
[wIndex
- 1] << (0 + 4));
1809 status
= (status
& USBPORTSC_CCS
) |
1810 ((status
& USBPORTSC_PE
) >> (2 - 1)) |
1811 ((status
& USBPORTSC_SUSP
) >> (12 - 2)) |
1812 ((status
& USBPORTSC_PR
) >> (9 - 4)) |
1813 (1 << 8) | /* power on */
1814 ((status
& USBPORTSC_LSDA
) << (-8 + 9));
1816 *(__u16
*)data
= cpu_to_le16(status
);
1817 *(__u16
*)(data
+ 2) = cpu_to_le16(cstatus
);
1819 case RH_CLEAR_FEATURE
| RH_ENDPOINT
:
1821 case RH_ENDPOINT_STALL
:
1825 case RH_CLEAR_FEATURE
| RH_CLASS
:
1827 case RH_C_HUB_OVER_CURRENT
:
1828 OK(0); /* hub power over current */
1831 case RH_CLEAR_FEATURE
| RH_OTHER
| RH_CLASS
:
1833 case RH_PORT_ENABLE
:
1834 CLR_RH_PORTSTAT(USBPORTSC_PE
);
1836 case RH_PORT_SUSPEND
:
1837 CLR_RH_PORTSTAT(USBPORTSC_SUSP
);
1840 OK(0); /* port power */
1841 case RH_C_PORT_CONNECTION
:
1842 SET_RH_PORTSTAT(USBPORTSC_CSC
);
1844 case RH_C_PORT_ENABLE
:
1845 SET_RH_PORTSTAT(USBPORTSC_PEC
);
1847 case RH_C_PORT_SUSPEND
:
1848 /*** WR_RH_PORTSTAT(RH_PS_PSSC); */
1850 case RH_C_PORT_OVER_CURRENT
:
1851 OK(0); /* port power over current */
1852 case RH_C_PORT_RESET
:
1853 uhci
->rh
.c_p_r
[wIndex
- 1] = 0;
1857 case RH_SET_FEATURE
| RH_OTHER
| RH_CLASS
:
1859 case RH_PORT_SUSPEND
:
1860 SET_RH_PORTSTAT(USBPORTSC_SUSP
);
1863 SET_RH_PORTSTAT(USBPORTSC_PR
);
1864 wait_ms(50); /* USB v1.1 7.1.7.3 */
1865 uhci
->rh
.c_p_r
[wIndex
- 1] = 1;
1866 CLR_RH_PORTSTAT(USBPORTSC_PR
);
1868 SET_RH_PORTSTAT(USBPORTSC_PE
);
1870 SET_RH_PORTSTAT(0xa);
1873 OK(0); /* port power ** */
1874 case RH_PORT_ENABLE
:
1875 SET_RH_PORTSTAT(USBPORTSC_PE
);
1879 case RH_SET_ADDRESS
:
1880 uhci
->rh
.devnum
= wValue
;
1882 case RH_GET_DESCRIPTOR
:
1883 switch ((wValue
& 0xff00) >> 8) {
1884 case 0x01: /* device descriptor */
1885 len
= min(leni
, min(sizeof(root_hub_dev_des
), wLength
));
1886 memcpy(data
, root_hub_dev_des
, len
);
1888 case 0x02: /* configuration descriptor */
1889 len
= min(leni
, min(sizeof(root_hub_config_des
), wLength
));
1890 memcpy (data
, root_hub_config_des
, len
);
1892 case 0x03: /* string descriptors */
1893 len
= usb_root_hub_string (wValue
& 0xff,
1894 uhci
->io_addr
, "UHCI-alt",
1897 OK (min (leni
, len
));
1902 case RH_GET_DESCRIPTOR
| RH_CLASS
:
1903 root_hub_hub_des
[2] = uhci
->rh
.numports
;
1904 len
= min(leni
, min(sizeof(root_hub_hub_des
), wLength
));
1905 memcpy(data
, root_hub_hub_des
, len
);
1907 case RH_GET_CONFIGURATION
:
1908 *(__u8
*)data
= 0x01;
1910 case RH_SET_CONFIGURATION
:
1912 case RH_GET_INTERFACE
| RH_INTERFACE
:
1913 *(__u8
*)data
= 0x00;
1915 case RH_SET_INTERFACE
| RH_INTERFACE
:
1921 urb
->actual_length
= len
;
1926 return USB_ST_NOERROR
;
1928 /*-------------------------------------------------------------------------*/
1930 static int rh_unlink_urb(struct urb
*urb
)
1932 struct uhci
*uhci
= (struct uhci
*)urb
->dev
->bus
->hcpriv
;
1934 if (uhci
->rh
.urb
== urb
) {
1936 del_timer(&uhci
->rh
.rh_int_timer
);
1940 /*-------------------------------------------------------------------*/
1942 void uhci_free_pending_qhs(struct uhci
*uhci
)
1944 struct list_head
*tmp
, *head
;
1945 unsigned long flags
;
1947 /* Free any pending QH's */
1948 spin_lock_irqsave(&uhci
->qh_remove_lock
, flags
);
1949 head
= &uhci
->qh_remove_list
;
1951 while (tmp
!= head
) {
1952 struct uhci_qh
*qh
= list_entry(tmp
, struct uhci_qh
, remove_list
);
1956 list_del(&qh
->remove_list
);
1960 spin_unlock_irqrestore(&uhci
->qh_remove_lock
, flags
);
1963 static void uhci_interrupt(int irq
, void *__uhci
, struct pt_regs
*regs
)
1965 struct uhci
*uhci
= __uhci
;
1966 unsigned int io_addr
= uhci
->io_addr
;
1967 unsigned short status
;
1968 unsigned long flags
;
1969 struct list_head
*tmp
, *head
;
1972 * Read the interrupt status, and write it back to clear the
1975 status
= inw(io_addr
+ USBSTS
);
1976 if (!status
) /* shared interrupt, not mine */
1978 outw(status
, io_addr
+ USBSTS
);
1980 if (status
& ~(USBSTS_USBINT
| USBSTS_ERROR
)) {
1981 if (status
& USBSTS_RD
)
1982 printk(KERN_INFO
"uhci: resume detected, not implemented\n");
1983 if (status
& USBSTS_HSE
)
1984 printk(KERN_ERR
"uhci: host system error, PCI problems?\n");
1985 if (status
& USBSTS_HCPE
)
1986 printk(KERN_ERR
"uhci: host controller process error. something bad happened\n");
1987 if (status
& USBSTS_HCH
) {
1988 printk(KERN_ERR
"uhci: host controller halted. very bad\n");
1989 /* FIXME: Reset the controller, fix the offending TD */
1993 uhci_free_pending_qhs(uhci
);
1995 spin_lock(&uhci
->urb_remove_lock
);
1996 head
= &uhci
->urb_remove_list
;
1998 while (tmp
!= head
) {
1999 struct urb
*urb
= list_entry(tmp
, struct urb
, urb_list
);
2003 list_del(&urb
->urb_list
);
2008 spin_unlock(&uhci
->urb_remove_lock
);
2010 uhci_clear_next_interrupt(uhci
);
2012 /* Walk the list of pending TD's to see which ones completed */
2013 nested_lock(&uhci
->urblist_lock
, flags
);
2014 head
= &uhci
->urb_list
;
2016 while (tmp
!= head
) {
2017 struct urb
*urb
= list_entry(tmp
, struct urb
, urb_list
);
2021 /* Checks the status and does all of the magic necessary */
2022 uhci_transfer_result(urb
);
2024 nested_unlock(&uhci
->urblist_lock
, flags
);
2027 static void reset_hc(struct uhci
*uhci
)
2029 unsigned int io_addr
= uhci
->io_addr
;
2031 /* Global reset for 50ms */
2032 outw(USBCMD_GRESET
, io_addr
+ USBCMD
);
2034 outw(0, io_addr
+ USBCMD
);
2038 static void start_hc(struct uhci
*uhci
)
2040 unsigned int io_addr
= uhci
->io_addr
;
2044 * Reset the HC - this will force us to get a
2045 * new notification of any already connected
2046 * ports due to the virtual disconnect that it
2049 outw(USBCMD_HCRESET
, io_addr
+ USBCMD
);
2050 while (inw(io_addr
+ USBCMD
) & USBCMD_HCRESET
) {
2052 printk(KERN_ERR
"uhci: USBCMD_HCRESET timed out!\n");
2057 /* Turn on all interrupts */
2058 outw(USBINTR_TIMEOUT
| USBINTR_RESUME
| USBINTR_IOC
| USBINTR_SP
,
2061 /* Start at frame 0 */
2062 outw(0, io_addr
+ USBFRNUM
);
2063 outl(virt_to_bus(uhci
->fl
), io_addr
+ USBFLBASEADD
);
2065 /* Run and mark it configured with a 64-byte max packet */
2066 outw(USBCMD_RS
| USBCMD_CF
| USBCMD_MAXP
, io_addr
+ USBCMD
);
2070 * Allocate a frame list, and then setup the skeleton
2072 * The hardware doesn't really know any difference
2073 * in the queues, but the order does matter for the
2074 * protocols higher up. The order is:
2076 * - any isochronous events handled before any
2077 * of the queues. We don't do that here, because
2078 * we'll create the actual TD entries on demand.
2079 * - The first queue is the "interrupt queue".
2080 * - The second queue is the "control queue", split into low and high speed
2081 * - The third queue is "bulk data".
2083 static struct uhci
*alloc_uhci(unsigned int io_addr
, unsigned int io_size
)
2087 struct usb_bus
*bus
;
2089 uhci
= kmalloc(sizeof(*uhci
), GFP_KERNEL
);
2093 memset(uhci
, 0, sizeof(*uhci
));
2096 uhci
->io_addr
= io_addr
;
2097 uhci
->io_size
= io_size
;
2099 spin_lock_init(&uhci
->qh_remove_lock
);
2100 INIT_LIST_HEAD(&uhci
->qh_remove_list
);
2102 spin_lock_init(&uhci
->urb_remove_lock
);
2103 INIT_LIST_HEAD(&uhci
->urb_remove_list
);
2105 nested_init(&uhci
->urblist_lock
);
2106 INIT_LIST_HEAD(&uhci
->urb_list
);
2108 spin_lock_init(&uhci
->framelist_lock
);
2110 /* We need exactly one page (per UHCI specs), how convenient */
2111 /* We assume that one page is atleast 4k (1024 frames * 4 bytes) */
2112 uhci
->fl
= (void *)__get_free_page(GFP_KERNEL
);
2116 bus
= usb_alloc_bus(&uhci_device_operations
);
2123 /* Initialize the root hub */
2125 /* UHCI specs says devices must have 2 ports, but goes on to say */
2126 /* they may have more but give no way to determine how many they */
2127 /* have. However, according to the UHCI spec, Bit 7 is always set */
2128 /* to 1. So we try to use this to our advantage */
2129 for (port
= 0; port
< (io_size
- 0x10) / 2; port
++) {
2130 unsigned int portstatus
;
2132 portstatus
= inw(io_addr
+ 0x10 + (port
* 2));
2133 if (!(portstatus
& 0x0080))
2137 info("detected %d ports", port
);
2139 /* This is experimental so anything less than 2 or greater than 8 is */
2140 /* something weird and we'll ignore it */
2141 if (port
< 2 || port
> 8) {
2142 info("port count misdetected? forcing to 2 ports");
2146 uhci
->rh
.numports
= port
;
2149 * 9 Interrupt queues; link int2 to int1, int4 to int2, etc
2150 * then link int1 to control and control to bulk
2152 for (i
= 1; i
< 9; i
++) {
2153 struct uhci_td
*td
= &uhci
->skeltd
[i
];
2155 uhci_fill_td(td
, 0, (UHCI_NULL_DATA_SIZE
<< 21) | (0x7f << 8) | USB_PID_IN
, 0);
2156 td
->link
= virt_to_bus(&uhci
->skeltd
[i
- 1]);
2160 uhci_fill_td(&uhci
->skel_int1_td
, 0, (UHCI_NULL_DATA_SIZE
<< 21) | (0x7f << 8) | USB_PID_IN
, 0);
2161 uhci
->skel_int1_td
.link
= virt_to_bus(&uhci
->skel_ls_control_qh
) | UHCI_PTR_QH
;
2163 uhci
->skel_ls_control_qh
.link
= virt_to_bus(&uhci
->skel_hs_control_qh
) | UHCI_PTR_QH
;
2164 uhci
->skel_ls_control_qh
.element
= UHCI_PTR_TERM
;
2166 uhci
->skel_hs_control_qh
.link
= virt_to_bus(&uhci
->skel_bulk_qh
) | UHCI_PTR_QH
;
2167 uhci
->skel_hs_control_qh
.element
= UHCI_PTR_TERM
;
2169 uhci
->skel_bulk_qh
.link
= virt_to_bus(&uhci
->skel_term_qh
) | UHCI_PTR_QH
;
2170 uhci
->skel_bulk_qh
.element
= UHCI_PTR_TERM
;
2172 /* This dummy TD is to work around a bug in Intel PIIX controllers */
2173 uhci_fill_td(&uhci
->skel_term_td
, 0, (UHCI_NULL_DATA_SIZE
<< 21) | (0x7f << 8) | USB_PID_IN
, 0);
2174 uhci
->skel_term_td
.link
= UHCI_PTR_TERM
;
2176 uhci
->skel_term_qh
.link
= UHCI_PTR_TERM
;
2177 uhci
->skel_term_qh
.element
= virt_to_bus(&uhci
->skel_term_td
);
2180 * Fill the frame list: make all entries point to
2181 * the proper interrupt queue.
2183 * This is probably silly, but it's a simple way to
2184 * scatter the interrupt queues in a way that gives
2185 * us a reasonable dynamic range for irq latencies.
2187 for (i
= 0; i
< 1024; i
++) {
2188 struct uhci_td
*irq
= &uhci
->skel_int1_td
;
2211 /* Only place we don't use the frame list routines */
2212 uhci
->fl
->frame
[i
] = virt_to_bus(irq
);
2221 free_page((unsigned long)uhci
->fl
);
2229 * De-allocate all resources..
2231 static void release_uhci(struct uhci
*uhci
)
2233 if (uhci
->irq
>= 0) {
2234 free_irq(uhci
->irq
, uhci
);
2239 free_page((unsigned long)uhci
->fl
);
2243 usb_free_bus(uhci
->bus
);
2247 int uhci_start_root_hub(struct uhci
*uhci
)
2249 struct usb_device
*dev
;
2251 dev
= usb_alloc_dev(NULL
, uhci
->bus
);
2255 uhci
->bus
->root_hub
= dev
;
2258 if (usb_new_device(dev
) != 0) {
2268 * If we've successfully found a UHCI, now is the time to increment the
2269 * module usage count, and return success..
2271 static int setup_uhci(struct pci_dev
*dev
, int irq
, unsigned int io_addr
, unsigned int io_size
)
2275 char buf
[8], *bufp
= buf
;
2278 sprintf(buf
, "%d", irq
);
2280 bufp
= __irq_itoa(irq
);
2282 printk(KERN_INFO __FILE__
": USB UHCI at I/O 0x%x, IRQ %s\n",
2285 uhci
= alloc_uhci(io_addr
, io_size
);
2289 INIT_LIST_HEAD(&uhci
->uhci_list
);
2290 list_add(&uhci
->uhci_list
, &uhci_list
);
2292 request_region(uhci
->io_addr
, io_size
, "usb-uhci");
2296 usb_register_bus(uhci
->bus
);
2300 if (request_irq(irq
, uhci_interrupt
, SA_SHIRQ
, "usb-uhci", uhci
) == 0) {
2303 if (!uhci_start_root_hub(uhci
)) {
2304 struct pm_dev
*pmdev
;
2306 pmdev
= pm_register(PM_PCI_DEV
,
2315 /* Couldn't allocate IRQ if we got here */
2316 list_del(&uhci
->uhci_list
);
2317 INIT_LIST_HEAD(&uhci
->uhci_list
);
2320 release_region(uhci
->io_addr
, uhci
->io_size
);
2326 static int found_uhci(struct pci_dev
*dev
)
2330 /* disable legacy emulation */
2331 pci_write_config_word(dev
, USBLEGSUP
, USBLEGSUP_DEFAULT
);
2333 if (pci_enable_device(dev
) < 0)
2337 err("found UHCI device with no IRQ assigned. check BIOS settings!");
2341 /* Search for the IO base address.. */
2342 for (i
= 0; i
< 6; i
++) {
2343 unsigned int io_addr
= pci_resource_start(dev
, i
);
2344 unsigned int io_size
= pci_resource_len(dev
, i
);
2347 if (!(pci_resource_flags(dev
, i
) & IORESOURCE_IO
))
2350 /* Is it already in use? */
2351 if (check_region(io_addr
, io_size
))
2354 return setup_uhci(dev
, dev
->irq
, io_addr
, io_size
);
2360 static int handle_pm_event(struct pm_dev
*dev
, pm_request_t rqst
, void *data
)
2362 struct uhci
*uhci
= dev
->data
;
2378 struct pci_dev
*dev
;
2383 /* We throw all of the TD's and QH's into a kmem cache */
2384 /* TD's and QH's need to be 16 byte aligned and SLAB_HWCACHE_ALIGN */
2385 /* does this for us */
2386 uhci_td_cachep
= kmem_cache_create("uhci_td",
2387 sizeof(struct uhci_td
), 0,
2388 SLAB_HWCACHE_ALIGN
, NULL
, NULL
);
2390 if (!uhci_td_cachep
)
2393 uhci_qh_cachep
= kmem_cache_create("uhci_qh",
2394 sizeof(struct uhci_qh
), 0,
2395 SLAB_HWCACHE_ALIGN
, NULL
, NULL
);
2397 if (!uhci_qh_cachep
)
2400 uhci_up_cachep
= kmem_cache_create("uhci_urb_priv",
2401 sizeof(struct urb_priv
), 0, 0, NULL
, NULL
);
2403 if (!uhci_up_cachep
)
2409 dev
= pci_find_class(PCI_CLASS_SERIAL_USB
<< 8, dev
);
2413 /* Is it the UHCI programming interface? */
2414 pci_read_config_byte(dev
, PCI_CLASS_PROG
, &type
);
2419 retval
= found_uhci(dev
);
2422 /* We only want to return an error code if ther was an error */
2423 /* and we didn't find a UHCI controller */
2424 if (retval
&& list_empty(&uhci_list
))
2430 if (kmem_cache_destroy(uhci_up_cachep
))
2431 printk(KERN_INFO
"uhci: not all urb_priv's were freed\n");
2434 if (kmem_cache_destroy(uhci_qh_cachep
))
2435 printk(KERN_INFO
"uhci: not all QH's were freed\n");
2438 if (kmem_cache_destroy(uhci_td_cachep
))
2439 printk(KERN_INFO
"uhci: not all TD's were freed\n");
2445 void uhci_cleanup(void)
2447 struct list_head
*tmp
, *head
= &uhci_list
;
2450 while (tmp
!= head
) {
2451 struct uhci
*uhci
= list_entry(tmp
, struct uhci
, uhci_list
);
2455 list_del(&uhci
->uhci_list
);
2456 INIT_LIST_HEAD(&uhci
->uhci_list
);
2458 if (uhci
->bus
->root_hub
)
2459 usb_disconnect(&uhci
->bus
->root_hub
);
2461 usb_deregister_bus(uhci
->bus
);
2464 release_region(uhci
->io_addr
, uhci
->io_size
);
2466 uhci_free_pending_qhs(uhci
);
2471 if (kmem_cache_destroy(uhci_up_cachep
))
2472 printk(KERN_INFO
"uhci: not all urb_priv's were freed\n");
2474 if (kmem_cache_destroy(uhci_qh_cachep
))
2475 printk(KERN_INFO
"uhci: not all QH's were freed\n");
2477 if (kmem_cache_destroy(uhci_td_cachep
))
2478 printk(KERN_INFO
"uhci: not all TD's were freed\n");
2482 int init_module(void)
2487 void cleanup_module(void)
2489 pm_unregister_all(handle_pm_event
);