2 * Universal Host Controller Interface driver for USB (take II).
4 * (c) 1999-2000 Georg Acher, acher@in.tum.de (executive slave) (base guitar)
5 * Deti Fliegl, deti@fliegl.de (executive slave) (lead voice)
6 * Thomas Sailer, sailer@ife.ee.ethz.ch (chief consultant) (cheer leader)
7 * Roman Weissgaerber, weissg@vienna.at (virt root hub) (studio porter)
9 * HW-initalization based on material of
11 * (C) Copyright 1999 Linus Torvalds
12 * (C) Copyright 1999 Johannes Erdfelt
13 * (C) Copyright 1999 Randy Dunlap
15 * $Id: usb-uhci.c,v 1.236 2000/08/02 20:28:28 acher Exp $
18 #include <linux/config.h>
19 #include <linux/module.h>
20 #include <linux/pci.h>
21 #include <linux/kernel.h>
22 #include <linux/delay.h>
23 #include <linux/ioport.h>
24 #include <linux/sched.h>
25 #include <linux/malloc.h>
26 #include <linux/smp_lock.h>
27 #include <linux/errno.h>
28 #include <linux/unistd.h>
29 #include <linux/interrupt.h> /* for in_interrupt() */
30 #include <linux/init.h>
31 #include <linux/version.h>
34 #include <asm/uaccess.h>
37 #include <asm/system.h>
39 /* This enables more detailed sanity checks in submit_iso */
40 //#define ISO_SANITY_CHECK
42 /* This enables debug printks */
45 /* This enables all symbols to be exported, to ease debugging oopses */
46 //#define DEBUG_SYMBOLS
48 /* This enables an extra UHCI slab for memory debugging */
51 #define VERSTR "$Revision: 1.236 $ time " __TIME__ " " __DATE__
53 #include <linux/usb.h>
55 #include "usb-uhci-debug.h"
59 #define dbg(format, arg...) do {} while (0)
67 #define _static static
70 #define queue_dbg dbg //err
71 #define async_dbg dbg //err
74 static kmem_cache_t
*uhci_desc_kmem
;
75 static kmem_cache_t
*urb_priv_kmem
;
78 #define SLAB_FLAG (in_interrupt ()? SLAB_ATOMIC : SLAB_KERNEL)
79 #define KMALLOC_FLAG (in_interrupt ()? GFP_ATOMIC : GFP_KERNEL)
81 #define CONFIG_USB_UHCI_HIGH_BANDWIDTH
82 #define USE_CTRL_DEPTH_FIRST 0 // 0: Breadth first, 1: Depth first
83 #define USE_BULK_DEPTH_FIRST 0 // 0: Breadth first, 1: Depth first
85 // stop bandwidth reclamation after (roughly) 50ms
86 #define IDLE_TIMEOUT (HZ/20)
88 _static
int rh_submit_urb (urb_t
*urb
);
89 _static
int rh_unlink_urb (urb_t
*urb
);
90 _static
int delete_qh (uhci_t
*s
, uhci_desc_t
*qh
);
91 _static
int process_transfer (uhci_t
*s
, urb_t
*urb
, int mode
);
92 _static
int process_interrupt (uhci_t
*s
, urb_t
*urb
);
93 _static
int process_iso (uhci_t
*s
, urb_t
*urb
, int force
);
95 static uhci_t
*devs
= NULL
;
97 /* used by userspace UHCI data structure dumper */
98 uhci_t
**uhci_devices
= &devs
;
100 /*-------------------------------------------------------------------*/
101 // Cleans up collected QHs
102 void clean_descs(uhci_t
*s
, int force
)
106 int now
=UHCI_GET_CURRENT_FRAME(s
);
110 while (q
!= &s
->free_desc
) {
111 qh
= list_entry (q
, uhci_desc_t
, horizontal
);
112 q
=qh
->horizontal
.prev
;
114 if ((qh
->last_used
!=now
) || force
)
118 /*-------------------------------------------------------------------*/
119 _static
void uhci_switch_timer_int(uhci_t
*s
)
122 if (!list_empty(&s
->urb_unlinked
)) {
123 s
->td1ms
->hw
.td
.status
|= TD_CTRL_IOC
;
126 s
->td1ms
->hw
.td
.status
&= ~TD_CTRL_IOC
;
129 if (s
->timeout_urbs
) {
130 s
->td32ms
->hw
.td
.status
|= TD_CTRL_IOC
;
133 s
->td32ms
->hw
.td
.status
&= ~TD_CTRL_IOC
;
138 /*-------------------------------------------------------------------*/
139 #ifdef CONFIG_USB_UHCI_HIGH_BANDWIDTH
140 _static
void enable_desc_loop(uhci_t
*s
, urb_t
*urb
)
144 spin_lock_irqsave (&s
->qh_lock
, flags
);
145 s
->chain_end
->hw
.qh
.head
&=~UHCI_PTR_TERM
;
148 ((urb_priv_t
*)urb
->hcpriv
)->use_loop
=1;
149 spin_unlock_irqrestore (&s
->qh_lock
, flags
);
151 /*-------------------------------------------------------------------*/
152 _static
void disable_desc_loop(uhci_t
*s
, urb_t
*urb
)
156 spin_lock_irqsave (&s
->qh_lock
, flags
);
158 if (((urb_priv_t
*)urb
->hcpriv
)->use_loop
) {
161 if (!s
->loop_usage
) {
162 s
->chain_end
->hw
.qh
.head
|=UHCI_PTR_TERM
;
165 ((urb_priv_t
*)urb
->hcpriv
)->use_loop
=0;
167 spin_unlock_irqrestore (&s
->qh_lock
, flags
);
170 /*-------------------------------------------------------------------*/
171 _static
void queue_urb_unlocked (uhci_t
*s
, urb_t
*urb
)
173 struct list_head
*p
=&urb
->urb_list
;
174 #ifdef CONFIG_USB_UHCI_HIGH_BANDWIDTH
177 type
=usb_pipetype (urb
->pipe
);
179 if ((type
== PIPE_BULK
) || (type
== PIPE_CONTROL
))
180 enable_desc_loop(s
, urb
);
183 ((urb_priv_t
*)urb
->hcpriv
)->started
=jiffies
;
184 list_add (p
, &s
->urb_list
);
187 uhci_switch_timer_int(s
);
189 /*-------------------------------------------------------------------*/
190 _static
void queue_urb (uhci_t
*s
, urb_t
*urb
)
192 unsigned long flags
=0;
194 spin_lock_irqsave (&s
->urb_list_lock
, flags
);
195 queue_urb_unlocked(s
,urb
);
196 spin_unlock_irqrestore (&s
->urb_list_lock
, flags
);
198 /*-------------------------------------------------------------------*/
199 _static
void dequeue_urb (uhci_t
*s
, urb_t
*urb
)
201 #ifdef CONFIG_USB_UHCI_HIGH_BANDWIDTH
204 type
=usb_pipetype (urb
->pipe
);
206 if ((type
== PIPE_BULK
) || (type
== PIPE_CONTROL
))
207 disable_desc_loop(s
, urb
);
210 list_del (&urb
->urb_list
);
211 if (urb
->timeout
&& s
->timeout_urbs
)
215 /*-------------------------------------------------------------------*/
216 _static
int alloc_td (uhci_desc_t
** new, int flags
)
219 *new= kmem_cache_alloc(uhci_desc_kmem
, SLAB_FLAG
);
221 *new = (uhci_desc_t
*) kmalloc (sizeof (uhci_desc_t
), KMALLOC_FLAG
);
225 memset (*new, 0, sizeof (uhci_desc_t
));
226 (*new)->hw
.td
.link
= UHCI_PTR_TERM
| (flags
& UHCI_PTR_BITS
); // last by default
227 (*new)->type
= TD_TYPE
;
229 INIT_LIST_HEAD (&(*new)->vertical
);
230 INIT_LIST_HEAD (&(*new)->horizontal
);
234 /*-------------------------------------------------------------------*/
235 // append a qh to td.link physically, the SW linkage is not affected
236 _static
void append_qh(uhci_t
*s
, uhci_desc_t
*td
, uhci_desc_t
* qh
, int flags
)
240 spin_lock_irqsave (&s
->td_lock
, xxx
);
242 td
->hw
.td
.link
= virt_to_bus (qh
) | (flags
& UHCI_PTR_DEPTH
) | UHCI_PTR_QH
;
245 spin_unlock_irqrestore (&s
->td_lock
, xxx
);
247 /*-------------------------------------------------------------------*/
248 /* insert td at last position in td-list of qh (vertical) */
249 _static
int insert_td (uhci_t
*s
, uhci_desc_t
*qh
, uhci_desc_t
* new, int flags
)
254 spin_lock_irqsave (&s
->td_lock
, xxx
);
256 list_add_tail (&new->vertical
, &qh
->vertical
);
258 prev
= list_entry (new->vertical
.prev
, uhci_desc_t
, vertical
);
261 // virgin qh without any tds
262 qh
->hw
.qh
.element
= virt_to_bus (new);
265 // already tds inserted, implicitely remove TERM bit of prev
266 prev
->hw
.td
.link
= virt_to_bus (new) | (flags
& UHCI_PTR_DEPTH
);
269 spin_unlock_irqrestore (&s
->td_lock
, xxx
);
273 /*-------------------------------------------------------------------*/
274 /* insert new_td after td (horizontal) */
275 _static
int insert_td_horizontal (uhci_t
*s
, uhci_desc_t
*td
, uhci_desc_t
* new)
280 spin_lock_irqsave (&s
->td_lock
, flags
);
282 next
= list_entry (td
->horizontal
.next
, uhci_desc_t
, horizontal
);
283 list_add (&new->horizontal
, &td
->horizontal
);
284 new->hw
.td
.link
= td
->hw
.td
.link
;
285 td
->hw
.td
.link
= virt_to_bus (new);
287 spin_unlock_irqrestore (&s
->td_lock
, flags
);
291 /*-------------------------------------------------------------------*/
292 _static
int unlink_td (uhci_t
*s
, uhci_desc_t
*element
, int phys_unlink
)
294 uhci_desc_t
*next
, *prev
;
298 spin_lock_irqsave (&s
->td_lock
, flags
);
300 next
= list_entry (element
->vertical
.next
, uhci_desc_t
, vertical
);
302 if (next
== element
) {
304 prev
= list_entry (element
->horizontal
.prev
, uhci_desc_t
, horizontal
);
307 prev
= list_entry (element
->vertical
.prev
, uhci_desc_t
, vertical
);
310 // really remove HW linking
311 if (prev
->type
== TD_TYPE
)
312 prev
->hw
.td
.link
= element
->hw
.td
.link
;
314 prev
->hw
.qh
.element
= element
->hw
.td
.link
;
320 list_del (&element
->vertical
);
322 list_del (&element
->horizontal
);
324 spin_unlock_irqrestore (&s
->td_lock
, flags
);
329 /*-------------------------------------------------------------------*/
330 _static
int delete_desc (uhci_desc_t
*element
)
333 kmem_cache_free(uhci_desc_kmem
, element
);
339 /*-------------------------------------------------------------------*/
340 // Allocates qh element
341 _static
int alloc_qh (uhci_desc_t
** new)
344 *new= kmem_cache_alloc(uhci_desc_kmem
, SLAB_FLAG
);
346 *new = (uhci_desc_t
*) kmalloc (sizeof (uhci_desc_t
), KMALLOC_FLAG
);
350 memset (*new, 0, sizeof (uhci_desc_t
));
351 (*new)->hw
.qh
.head
= UHCI_PTR_TERM
;
352 (*new)->hw
.qh
.element
= UHCI_PTR_TERM
;
353 (*new)->type
= QH_TYPE
;
356 INIT_LIST_HEAD (&(*new)->horizontal
);
357 INIT_LIST_HEAD (&(*new)->vertical
);
359 dbg("Allocated qh @ %p", *new);
363 /*-------------------------------------------------------------------*/
364 // inserts new qh before/after the qh at pos
365 // flags: 0: insert before pos, 1: insert after pos (for low speed transfers)
366 _static
int insert_qh (uhci_t
*s
, uhci_desc_t
*pos
, uhci_desc_t
*new, int order
)
371 spin_lock_irqsave (&s
->qh_lock
, flags
);
374 // (OLD) (POS) -> (OLD) (NEW) (POS)
375 old
= list_entry (pos
->horizontal
.prev
, uhci_desc_t
, horizontal
);
376 list_add_tail (&new->horizontal
, &pos
->horizontal
);
377 new->hw
.qh
.head
= MAKE_QH_ADDR (pos
) ;
378 if (!(old
->hw
.qh
.head
& UHCI_PTR_TERM
))
379 old
->hw
.qh
.head
= MAKE_QH_ADDR (new) ;
382 // (POS) (OLD) -> (POS) (NEW) (OLD)
383 old
= list_entry (pos
->horizontal
.next
, uhci_desc_t
, horizontal
);
384 list_add (&new->horizontal
, &pos
->horizontal
);
385 new->hw
.qh
.head
= MAKE_QH_ADDR (old
);
386 pos
->hw
.qh
.head
= MAKE_QH_ADDR (new) ;
391 spin_unlock_irqrestore (&s
->qh_lock
, flags
);
396 /*-------------------------------------------------------------------*/
397 _static
int unlink_qh (uhci_t
*s
, uhci_desc_t
*element
)
402 spin_lock_irqsave (&s
->qh_lock
, flags
);
404 prev
= list_entry (element
->horizontal
.prev
, uhci_desc_t
, horizontal
);
405 prev
->hw
.qh
.head
= element
->hw
.qh
.head
;
407 list_del(&element
->horizontal
);
410 spin_unlock_irqrestore (&s
->qh_lock
, flags
);
414 /*-------------------------------------------------------------------*/
415 _static
int delete_qh (uhci_t
*s
, uhci_desc_t
*qh
)
420 list_del (&qh
->horizontal
);
422 while ((p
= qh
->vertical
.next
) != &qh
->vertical
) {
423 td
= list_entry (p
, uhci_desc_t
, vertical
);
424 dbg("unlink td @ %p",td
);
425 unlink_td (s
, td
, 0); // no physical unlink
433 /*-------------------------------------------------------------------*/
434 _static
void clean_td_chain (uhci_desc_t
*td
)
442 while ((p
= td
->horizontal
.next
) != &td
->horizontal
) {
443 td1
= list_entry (p
, uhci_desc_t
, horizontal
);
450 /*-------------------------------------------------------------------*/
451 _static
void fill_td (uhci_desc_t
*td
, int status
, int info
, __u32 buffer
)
453 td
->hw
.td
.status
= status
;
454 td
->hw
.td
.info
= info
;
455 td
->hw
.td
.buffer
= buffer
;
457 /*-------------------------------------------------------------------*/
458 // Removes ALL qhs in chain (paranoia!)
459 _static
void cleanup_skel (uhci_t
*s
)
471 unlink_td(s
,s
->td32ms
,1);
472 delete_desc(s
->td32ms
);
475 for (n
= 0; n
< 8; n
++) {
476 td
= s
->int_chain
[n
];
481 for (n
= 0; n
< 1024; n
++) {
489 free_page ((unsigned long) s
->framelist
);
491 if (s
->control_chain
) {
492 // completed init_skel?
494 uhci_desc_t
*qh
, *qh1
;
496 qh
= s
->control_chain
;
497 while ((p
= qh
->horizontal
.next
) != &qh
->horizontal
) {
498 qh1
= list_entry (p
, uhci_desc_t
, horizontal
);
505 if (s
->ls_control_chain
)
506 delete_desc (s
->ls_control_chain
);
507 if (s
->control_chain
)
508 delete_desc(s
->control_chain
);
510 delete_desc (s
->bulk_chain
);
512 delete_desc (s
->chain_end
);
514 dbg("cleanup_skel finished");
516 /*-------------------------------------------------------------------*/
517 // allocates framelist and qh-skeletons
518 // only HW-links provide continous linking, SW-links stay in their domain (ISO/INT)
519 _static
int init_skel (uhci_t
*s
)
522 uhci_desc_t
*qh
, *td
;
526 s
->framelist
= (__u32
*) get_free_page (GFP_KERNEL
);
531 memset (s
->framelist
, 0, 4096);
533 dbg("allocating iso desc pointer list");
534 s
->iso_td
= (uhci_desc_t
**) kmalloc (1024 * sizeof (uhci_desc_t
*), GFP_KERNEL
);
537 goto init_skel_cleanup
;
539 s
->ls_control_chain
= NULL
;
540 s
->control_chain
= NULL
;
541 s
->bulk_chain
= NULL
;
544 dbg("allocating iso descs");
545 for (n
= 0; n
< 1024; n
++) {
546 // allocate skeleton iso/irq-tds
547 ret
= alloc_td (&td
, 0);
549 goto init_skel_cleanup
;
551 s
->framelist
[n
] = ((__u32
) virt_to_bus (td
));
554 dbg("allocating qh: chain_end");
555 ret
= alloc_qh (&qh
);
558 goto init_skel_cleanup
;
562 ret
= alloc_td (&td
, 0);
565 goto init_skel_cleanup
;
567 fill_td (td
, 0 * TD_CTRL_IOC
, 0, 0); // generate 1ms interrupt (enabled on demand)
568 insert_td (s
, qh
, td
, 0);
571 dbg("allocating qh: bulk_chain");
572 ret
= alloc_qh (&qh
);
574 goto init_skel_cleanup
;
576 insert_qh (s
, s
->chain_end
, qh
, 0);
579 dbg("allocating qh: control_chain");
580 ret
= alloc_qh (&qh
);
582 goto init_skel_cleanup
;
584 insert_qh (s
, s
->bulk_chain
, qh
, 0);
585 s
->control_chain
= qh
;
587 #ifdef CONFIG_USB_UHCI_HIGH_BANDWIDTH
588 // disabled reclamation loop
589 s
->chain_end
->hw
.qh
.head
=virt_to_bus(s
->control_chain
) | UHCI_PTR_QH
| UHCI_PTR_TERM
;
592 dbg("allocating qh: ls_control_chain");
593 ret
= alloc_qh (&qh
);
595 goto init_skel_cleanup
;
597 insert_qh (s
, s
->control_chain
, qh
, 0);
598 s
->ls_control_chain
= qh
;
600 for (n
= 0; n
< 8; n
++)
603 dbg("allocating skeleton INT-TDs");
605 for (n
= 0; n
< 8; n
++) {
610 goto init_skel_cleanup
;
611 s
->int_chain
[n
] = td
;
613 s
->int_chain
[0]->hw
.td
.link
= virt_to_bus (s
->ls_control_chain
) | UHCI_PTR_QH
;
616 s
->int_chain
[n
]->hw
.td
.link
= virt_to_bus (s
->int_chain
[0]);
620 dbg("Linking skeleton INT-TDs");
622 for (n
= 0; n
< 1024; n
++) {
623 // link all iso-tds to the interrupt chains
625 dbg("framelist[%i]=%x",n
,s
->framelist
[n
]);
627 ((uhci_desc_t
*) s
->iso_td
[n
])->hw
.td
.link
= virt_to_bus(s
->int_chain
[0]);
629 for (o
= 1, m
= 2; m
<= 128; o
++, m
+= m
)
630 if ((n
& (m
- 1)) == ((m
- 1) / 2))
631 ((uhci_desc_t
*) s
->iso_td
[n
])->hw
.td
.link
= virt_to_bus (s
->int_chain
[o
]);
634 ret
= alloc_td (&td
, 0);
637 goto init_skel_cleanup
;
639 fill_td (td
, 0 * TD_CTRL_IOC
, 0, 0); // generate 32ms interrupt
642 insert_td_horizontal (s
, s
->int_chain
[5], td
);
645 //uhci_show_queue(s->control_chain);
646 dbg("init_skel exit");
654 /*-------------------------------------------------------------------*/
656 // assembles QHs und TDs for control, bulk and iso
657 /*-------------------------------------------------------------------*/
658 _static
int uhci_submit_control_urb (urb_t
*urb
)
660 uhci_desc_t
*qh
, *td
;
661 uhci_t
*s
= (uhci_t
*) urb
->dev
->bus
->hcpriv
;
662 urb_priv_t
*urb_priv
= urb
->hcpriv
;
663 unsigned long destination
, status
;
664 int maxsze
= usb_maxpacket (urb
->dev
, urb
->pipe
, usb_pipeout (urb
->pipe
));
667 int depth_first
=USE_CTRL_DEPTH_FIRST
; // UHCI descriptor chasing method
670 err("uhci_submit_control_urb: pipesize for pipe %x is zero", urb
->pipe
);
674 dbg("uhci_submit_control start");
675 alloc_qh (&qh
); // alloc qh for this request
680 alloc_td (&td
, UHCI_PTR_DEPTH
* depth_first
); // get td for setup stage
687 /* The "pipe" thing contains the destination in bits 8--18 */
688 destination
= (urb
->pipe
& PIPE_DEVEP_MASK
) | USB_PID_SETUP
;
691 status
= (urb
->pipe
& TD_CTRL_LS
) | TD_CTRL_ACTIVE
|
692 (urb
->transfer_flags
& USB_DISABLE_SPD
? 0 : TD_CTRL_SPD
) | (3 << 27);
694 /* Build the TD for the control request, try forever, 8 bytes of data */
695 fill_td (td
, status
, destination
| (7 << 21), virt_to_bus (urb
->setup_packet
));
697 insert_td (s
, qh
, td
, 0); // queue 'setup stage'-td in qh
700 char *sp
=urb
->setup_packet
;
701 dbg("SETUP to pipe %x: %x %x %x %x %x %x %x %x", urb
->pipe
,
702 sp
[0],sp
[1],sp
[2],sp
[3],sp
[4],sp
[5],sp
[6],sp
[7]);
707 len
= urb
->transfer_buffer_length
;
708 data
= urb
->transfer_buffer
;
710 /* If direction is "send", change the frame from SETUP (0x2D)
711 to OUT (0xE1). Else change it from SETUP to IN (0x69). */
713 destination
= (urb
->pipe
& PIPE_DEVEP_MASK
) | (usb_pipeout (urb
->pipe
)?USB_PID_OUT
:USB_PID_IN
);
718 alloc_td (&td
, UHCI_PTR_DEPTH
* depth_first
);
727 destination
^= 1 << TD_TOKEN_TOGGLE
; // toggle DATA0/1
729 fill_td (td
, status
, destination
| ((pktsze
- 1) << 21),
730 virt_to_bus (data
)); // Status, pktsze bytes of data
732 insert_td (s
, qh
, td
, UHCI_PTR_DEPTH
* depth_first
); // queue 'data stage'-td in qh
738 /* Build the final TD for control status */
739 /* It's only IN if the pipe is out AND we aren't expecting data */
741 destination
&= ~UHCI_PID
;
743 if (usb_pipeout (urb
->pipe
) || (urb
->transfer_buffer_length
== 0))
744 destination
|= USB_PID_IN
;
746 destination
|= USB_PID_OUT
;
748 destination
|= 1 << TD_TOKEN_TOGGLE
; /* End in Data1 */
750 alloc_td (&td
, UHCI_PTR_DEPTH
);
756 status
&=~TD_CTRL_SPD
;
758 /* no limit on errors on final packet , 0 bytes of data */
759 fill_td (td
, status
| TD_CTRL_IOC
, destination
| (UHCI_NULL_DATA_SIZE
<< 21),
762 insert_td (s
, qh
, td
, UHCI_PTR_DEPTH
* depth_first
); // queue status td
764 list_add (&qh
->desc_list
, &urb_priv
->desc_list
);
766 urb
->status
= -EINPROGRESS
;
767 queue_urb (s
, urb
); // queue before inserting in desc chain
769 qh
->hw
.qh
.element
&= ~UHCI_PTR_TERM
;
771 //uhci_show_queue(qh);
772 /* Start it up... put low speed first */
773 if (urb
->pipe
& TD_CTRL_LS
)
774 insert_qh (s
, s
->control_chain
, qh
, 0);
776 insert_qh (s
, s
->bulk_chain
, qh
, 0);
778 dbg("uhci_submit_control end");
781 /*-------------------------------------------------------------------*/
782 // For queued bulk transfers, two additional QH helpers are allocated (nqh, bqh)
783 // Due to the linking with other bulk urbs, it has to be locked with urb_list_lock!
785 _static
int uhci_submit_bulk_urb (urb_t
*urb
, urb_t
*bulk_urb
)
787 uhci_t
*s
= (uhci_t
*) urb
->dev
->bus
->hcpriv
;
788 urb_priv_t
*urb_priv
= urb
->hcpriv
;
789 uhci_desc_t
*qh
, *td
, *nqh
, *bqh
;
790 unsigned long destination
, status
;
792 unsigned int pipe
= urb
->pipe
;
793 int maxsze
= usb_maxpacket (urb
->dev
, pipe
, usb_pipeout (pipe
));
795 int depth_first
=USE_BULK_DEPTH_FIRST
; // UHCI descriptor chasing method
796 urb_priv_t
*upriv
, *bpriv
;
798 if (usb_endpoint_halted (urb
->dev
, usb_pipeendpoint (pipe
), usb_pipeout (pipe
)))
801 if (urb
->transfer_buffer_length
< 0) {
802 err("Negative transfer length in submit_bulk");
809 queue_dbg("uhci_submit_bulk_urb: urb %p, old %p, pipe %08x, len %i",
810 urb
,bulk_urb
,urb
->pipe
,urb
->transfer_buffer_length
);
812 upriv
=(urb_priv_t
*)urb
->hcpriv
;
815 alloc_qh (&qh
); // get qh for this request
820 if (urb
->transfer_flags
& USB_QUEUE_BULK
) {
821 alloc_qh(&nqh
); // placeholder for clean unlink
826 upriv
->next_qh
= nqh
;
827 queue_dbg("new next qh %p",nqh
);
831 bpriv
= (urb_priv_t
*)bulk_urb
->hcpriv
;
832 qh
= bpriv
->bottom_qh
; // re-use bottom qh and next qh
833 nqh
= bpriv
->next_qh
;
835 bpriv
->next_queued_urb
=urb
;
836 upriv
->prev_queued_urb
=bulk_urb
;
839 queue_dbg("uhci_submit_bulk: qh=%p, nqh=%p\n",bqh
,nqh
);
841 if (urb
->transfer_flags
& USB_QUEUE_BULK
) {
842 alloc_qh (&bqh
); // "bottom" QH,
851 bqh
->hw
.qh
.element
= UHCI_PTR_TERM
;
852 bqh
->hw
.qh
.element
= virt_to_bus(nqh
)|UHCI_PTR_QH
;
853 upriv
->bottom_qh
= bqh
;
854 queue_dbg("uhci_submit_bulk: new bqh %p\n",bqh
);
858 /* The "pipe" thing contains the destination in bits 8--18. */
859 destination
= (pipe
& PIPE_DEVEP_MASK
) | usb_packetid (pipe
);
862 status
= (pipe
& TD_CTRL_LS
) | TD_CTRL_ACTIVE
|
863 ((urb
->transfer_flags
& USB_DISABLE_SPD
) ? 0 : TD_CTRL_SPD
) | (3 << 27);
865 /* Build the TDs for the bulk request */
866 len
= urb
->transfer_buffer_length
;
867 data
= urb
->transfer_buffer
;
869 do { // TBD: Really allow zero-length packets?
872 alloc_td (&td
, UHCI_PTR_DEPTH
* depth_first
);
882 // pktsze bytes of data
883 info
= destination
| (((pktsze
- 1)&UHCI_NULL_DATA_SIZE
) << 21) |
884 (usb_gettoggle (urb
->dev
, usb_pipeendpoint (pipe
), usb_pipeout (pipe
)) << TD_TOKEN_TOGGLE
);
886 fill_td (td
, status
, info
, virt_to_bus (data
));
891 last
= (len
== 0 && (usb_pipein(pipe
) || pktsze
< maxsze
|| (urb
->transfer_flags
& USB_DISABLE_SPD
)));
894 td
->hw
.td
.status
|= TD_CTRL_IOC
; // last one generates INT
896 insert_td (s
, qh
, td
, UHCI_PTR_DEPTH
* depth_first
);
897 usb_dotoggle (urb
->dev
, usb_pipeendpoint (pipe
), usb_pipeout (pipe
));
901 list_add (&qh
->desc_list
, &urb_priv
->desc_list
);
903 if (urb
->transfer_flags
& USB_QUEUE_BULK
) {
904 qh
->hw
.qh
.element
&=~UHCI_PTR_TERM
;
905 append_qh(s
, td
, bqh
, UHCI_PTR_DEPTH
* depth_first
);
908 urb
->status
= -EINPROGRESS
;
909 queue_urb_unlocked (s
, urb
);
911 qh
->hw
.qh
.element
&= ~UHCI_PTR_TERM
;
914 if (urb
->transfer_flags
& USB_QUEUE_BULK
) {
915 spin_lock (&s
->td_lock
); // both QHs in one go
916 insert_qh (s
, s
->chain_end
, qh
, 0); // Main QH
917 insert_qh (s
, s
->chain_end
, nqh
, 0); // Helper QH
918 spin_unlock (&s
->td_lock
);
921 insert_qh (s
, s
->chain_end
, qh
, 0);
924 //uhci_show_queue(s->bulk_chain);
925 //dbg("uhci_submit_bulk_urb: exit\n");
928 /*-------------------------------------------------------------------*/
929 _static
void uhci_clean_iso_step1(uhci_t
*s
, urb_priv_t
*urb_priv
)
934 for (p
= urb_priv
->desc_list
.next
; p
!= &urb_priv
->desc_list
; p
= p
->next
) {
935 td
= list_entry (p
, uhci_desc_t
, desc_list
);
936 unlink_td (s
, td
, 1);
939 /*-------------------------------------------------------------------*/
940 _static
void uhci_clean_iso_step2(uhci_t
*s
, urb_priv_t
*urb_priv
)
945 while ((p
= urb_priv
->desc_list
.next
) != &urb_priv
->desc_list
) {
946 td
= list_entry (p
, uhci_desc_t
, desc_list
);
951 /*-------------------------------------------------------------------*/
952 // mode: 0: unlink + no deletion mark, 1: regular (unlink/delete-mark), 2: don't unlink
953 // looks a bit complicated because of all the bulk queueing goodies
955 _static
void uhci_clean_transfer (uhci_t
*s
, urb_t
*urb
, uhci_desc_t
*qh
, int mode
)
957 uhci_desc_t
*bqh
, *nqh
, *prevqh
;
959 urb_priv_t
*priv
=(urb_priv_t
*)urb
->hcpriv
;
961 now
=UHCI_GET_CURRENT_FRAME(s
);
963 dbg("clean transfer urb %p, qh %p, mode %i",urb
,qh
,mode
);
966 if (!priv
->next_queued_urb
) { // no more appended bulk queues
971 if (priv
->prev_queued_urb
) {
972 urb_priv_t
* ppriv
=(urb_priv_t
*)priv
->prev_queued_urb
->hcpriv
;
974 ppriv
->bottom_qh
= priv
->bottom_qh
;
975 ppriv
->next_queued_urb
= NULL
;
977 else if (bqh
) { // queue dead
984 nqh
->last_used
= bqh
->last_used
= now
;
985 list_add_tail (&nqh
->horizontal
, &s
->free_desc
);
986 list_add_tail (&bqh
->horizontal
, &s
->free_desc
);
990 else { // there are queued urbs following
994 nurb
=priv
->next_queued_urb
;
995 spin_lock_irqsave (&s
->qh_lock
, flags
);
997 if (!priv
->prev_queued_urb
) { // top
999 prevqh
= list_entry (qh
->horizontal
.prev
, uhci_desc_t
, horizontal
);
1000 prevqh
->hw
.qh
.head
= virt_to_bus(bqh
) | UHCI_PTR_QH
;
1001 queue_dbg ("TOP relink of %p to %p-%p",qh
,prevqh
,bqh
);
1003 list_del (&qh
->horizontal
);
1004 list_add (&bqh
->horizontal
, &prevqh
->horizontal
);
1007 else { //intermediate
1008 urb_priv_t
* ppriv
=(urb_priv_t
*)priv
->prev_queued_urb
->hcpriv
;
1011 bnqh
=list_entry (&((urb_priv_t
*)(nurb
->hcpriv
))->desc_list
.next
, uhci_desc_t
, desc_list
);
1012 ppriv
->bottom_qh
=bnqh
;
1013 ppriv
->next_queued_urb
=nurb
;
1016 prevqh
= list_entry (ppriv
->desc_list
.next
, uhci_desc_t
, desc_list
);
1017 prevqh
->hw
.qh
.head
= virt_to_bus(bqh
) | UHCI_PTR_QH
;
1018 queue_dbg ("IM relink of %p to %p-%p",qh
,prevqh
,bqh
);
1022 spin_unlock_irqrestore (&s
->qh_lock
, flags
);
1023 ((urb_priv_t
*)nurb
->hcpriv
)->prev_queued_urb
=priv
->prev_queued_urb
;
1027 qh
->last_used
= now
;
1028 list_add_tail (&qh
->horizontal
, &s
->free_desc
); // mark for later deletion
1031 /*-------------------------------------------------------------------*/
1032 // unlinks an urb by dequeuing its qh, waits some frames and forgets it
1033 _static
int uhci_unlink_urb_sync (uhci_t
*s
, urb_t
*urb
)
1036 urb_priv_t
*urb_priv
;
1037 unsigned long flags
=0;
1039 spin_lock_irqsave (&s
->urb_list_lock
, flags
);
1041 if (!in_interrupt()) // shouldn't be called from interrupt at all...
1042 spin_lock(&urb
->lock
);
1044 if (urb
->status
== -EINPROGRESS
) {
1045 // URB probably still in work
1046 dequeue_urb (s
, urb
);
1047 uhci_switch_timer_int(s
);
1048 s
->unlink_urb_done
=1;
1050 if (!in_interrupt())
1051 spin_unlock(&urb
->lock
);
1053 spin_unlock_irqrestore (&s
->urb_list_lock
, flags
);
1055 urb
->status
= -ENOENT
; // mark urb as killed
1056 urb_priv
= urb
->hcpriv
;
1058 switch (usb_pipetype (urb
->pipe
)) {
1060 case PIPE_INTERRUPT
:
1061 usb_dotoggle (urb
->dev
, usb_pipeendpoint (urb
->pipe
), usb_pipeout (urb
->pipe
));
1063 case PIPE_ISOCHRONOUS
:
1064 uhci_clean_iso_step1(s
, urb_priv
);
1066 uhci_clean_iso_step2(s
, urb_priv
);
1071 qh
= list_entry (urb_priv
->desc_list
.next
, uhci_desc_t
, desc_list
);
1072 spin_lock_irqsave (&s
->urb_list_lock
, flags
);
1073 uhci_clean_transfer(s
, urb
, qh
, 1);
1074 spin_unlock_irqrestore (&s
->urb_list_lock
, flags
);
1079 kmem_cache_free (urb_priv_kmem
, urb
->hcpriv
);
1081 kfree (urb
->hcpriv
);
1083 if (urb
->complete
) {
1084 dbg("unlink_urb: calling completion");
1085 urb
->complete ((struct urb
*) urb
);
1087 usb_dec_dev_use (urb
->dev
);
1090 if (!in_interrupt())
1091 spin_unlock(&urb
->lock
);
1092 spin_unlock_irqrestore (&s
->urb_list_lock
, flags
);
1097 /*-------------------------------------------------------------------*/
1098 // async unlink_urb completion/cleanup work
1099 // has to be protected by urb_list_lock!
1100 // features: if set in transfer_flags, the resulting status of the killed
1101 // transaction is not overwritten
1103 _static
void uhci_cleanup_unlink(uhci_t
*s
, int force
)
1105 struct list_head
*q
;
1107 struct usb_device
*dev
;
1109 urb_priv_t
*urb_priv
;
1111 q
=s
->urb_unlinked
.next
;
1112 now
=UHCI_GET_CURRENT_FRAME(s
);
1114 while (q
!= &s
->urb_unlinked
) {
1116 urb
= list_entry (q
, urb_t
, urb_list
);
1118 urb_priv
= (urb_priv_t
*)urb
->hcpriv
;
1119 q
= urb
->urb_list
.next
;
1122 ((urb_priv
->started
!= 0xffffffff) && (urb_priv
->started
!= now
))) {
1123 async_dbg("async cleanup %p",urb
);
1124 switch (usb_pipetype (urb
->pipe
)) { // process descriptors
1126 process_transfer (s
, urb
, 2);
1129 if (!s
->avoid_bulk
.counter
)
1130 process_transfer (s
, urb
, 2); // don't unlink (already done)
1134 case PIPE_ISOCHRONOUS
:
1135 process_iso (s
, urb
, 1); // force, don't unlink
1137 case PIPE_INTERRUPT
:
1138 process_interrupt (s
, urb
);
1142 if (!(urb
->transfer_flags
& USB_TIMEOUT_KILLED
))
1143 urb
->status
= -ECONNRESET
; // mark as asynchronously killed
1145 pipe
= urb
->pipe
; // completion may destroy all...
1147 urb_priv
= urb
->hcpriv
;
1149 if (urb
->complete
) {
1150 spin_unlock(&s
->urb_list_lock
);
1151 urb
->complete ((struct urb
*) urb
);
1152 spin_lock(&s
->urb_list_lock
);
1155 if (!(urb
->transfer_flags
& USB_TIMEOUT_KILLED
))
1156 urb
->status
= -ENOENT
; // now the urb is really dead
1157 switch (usb_pipetype (pipe
)) {
1158 case PIPE_ISOCHRONOUS
:
1159 case PIPE_INTERRUPT
:
1160 uhci_clean_iso_step2(s
, urb_priv
);
1164 usb_dec_dev_use (dev
);
1166 kmem_cache_free (urb_priv_kmem
, urb_priv
);
1171 list_del (&urb
->urb_list
);
1176 /*-------------------------------------------------------------------*/
1177 _static
int uhci_unlink_urb_async (uhci_t
*s
,urb_t
*urb
)
1180 urb_priv_t
*urb_priv
;
1182 async_dbg("unlink_urb_async called %p",urb
);
1184 if ((urb
->status
== -EINPROGRESS
) ||
1185 ((usb_pipetype (urb
->pipe
) == PIPE_INTERRUPT
) && ((urb_priv_t
*)urb
->hcpriv
)->flags
))
1187 ((urb_priv_t
*)urb
->hcpriv
)->started
= ~0;
1189 dequeue_urb (s
, urb
);
1190 list_add_tail (&urb
->urb_list
, &s
->urb_unlinked
); // store urb
1191 uhci_switch_timer_int(s
);
1193 s
->unlink_urb_done
= 1;
1195 urb
->status
= -ECONNABORTED
; // mark urb as "waiting to be killed"
1196 urb_priv
= (urb_priv_t
*)urb
->hcpriv
;
1198 switch (usb_pipetype (urb
->pipe
)) {
1199 case PIPE_INTERRUPT
:
1200 usb_dotoggle (urb
->dev
, usb_pipeendpoint (urb
->pipe
), usb_pipeout (urb
->pipe
));
1202 case PIPE_ISOCHRONOUS
:
1203 uhci_clean_iso_step1 (s
, urb_priv
);
1208 qh
= list_entry (urb_priv
->desc_list
.next
, uhci_desc_t
, desc_list
);
1209 uhci_clean_transfer (s
, urb
, qh
, 0);
1212 ((urb_priv_t
*)urb
->hcpriv
)->started
= UHCI_GET_CURRENT_FRAME(s
);
1213 return -EINPROGRESS
; // completion will follow
1216 return 0; // URB already dead
1218 /*-------------------------------------------------------------------*/
1219 _static
int uhci_unlink_urb (urb_t
*urb
)
1222 unsigned long flags
=0;
1223 dbg("uhci_unlink_urb called for %p",urb
);
1224 if (!urb
|| !urb
->dev
) // you never know...
1227 s
= (uhci_t
*) urb
->dev
->bus
->hcpriv
;
1229 if (usb_pipedevice (urb
->pipe
) == s
->rh
.devnum
)
1230 return rh_unlink_urb (urb
);
1235 if (urb
->transfer_flags
& USB_ASYNC_UNLINK
) {
1238 spin_lock_irqsave (&s
->urb_list_lock
, flags
);
1240 // The URB needs to be locked if called outside completion context
1242 if (!in_interrupt())
1243 spin_lock(&urb
->lock
);
1245 ret
= uhci_unlink_urb_async(s
, urb
);
1247 if (!in_interrupt())
1248 spin_unlock(&urb
->lock
);
1250 spin_unlock_irqrestore (&s
->urb_list_lock
, flags
);
1255 return uhci_unlink_urb_sync(s
, urb
);
1257 /*-------------------------------------------------------------------*/
1258 // In case of ASAP iso transfer, search the URB-list for already queued URBs
1259 // for this EP and calculate the earliest start frame for the new
1260 // URB (easy seamless URB continuation!)
1261 _static
int find_iso_limits (urb_t
*urb
, unsigned int *start
, unsigned int *end
)
1263 urb_t
*u
, *last_urb
= NULL
;
1264 uhci_t
*s
= (uhci_t
*) urb
->dev
->bus
->hcpriv
;
1265 struct list_head
*p
;
1267 unsigned long flags
;
1269 spin_lock_irqsave (&s
->urb_list_lock
, flags
);
1272 for (; p
!= &s
->urb_list
; p
= p
->prev
) {
1273 u
= list_entry (p
, urb_t
, urb_list
);
1274 // look for pending URBs with identical pipe handle
1275 // works only because iso doesn't toggle the data bit!
1276 if ((urb
->pipe
== u
->pipe
) && (urb
->dev
== u
->dev
) && (u
->status
== -EINPROGRESS
)) {
1278 *start
= u
->start_frame
;
1284 *end
= (last_urb
->start_frame
+ last_urb
->number_of_packets
) & 1023;
1288 spin_unlock_irqrestore(&s
->urb_list_lock
, flags
);
1292 /*-------------------------------------------------------------------*/
1293 // adjust start_frame according to scheduling constraints (ASAP etc)
1295 _static
int iso_find_start (urb_t
*urb
)
1297 uhci_t
*s
= (uhci_t
*) urb
->dev
->bus
->hcpriv
;
1299 unsigned int start_limit
= 0, stop_limit
= 0, queued_size
;
1302 now
= UHCI_GET_CURRENT_FRAME (s
) & 1023;
1304 if ((unsigned) urb
->number_of_packets
> 900)
1307 limits
= find_iso_limits (urb
, &start_limit
, &stop_limit
);
1308 queued_size
= (stop_limit
- start_limit
) & 1023;
1310 if (urb
->transfer_flags
& USB_ISO_ASAP
) {
1313 // 10ms setup should be enough //FIXME!
1314 urb
->start_frame
= (now
+ 10) & 1023;
1317 urb
->start_frame
= stop_limit
; //seamless linkage
1319 if (((now
- urb
->start_frame
) & 1023) <= (unsigned) urb
->number_of_packets
) {
1320 info("iso_find_start: gap in seamless isochronous scheduling");
1321 dbg("iso_find_start: now %u start_frame %u number_of_packets %u pipe 0x%08x",
1322 now
, urb
->start_frame
, urb
->number_of_packets
, urb
->pipe
);
1323 urb
->start_frame
= (now
+ 5) & 1023; // 5ms setup should be enough //FIXME!
1328 urb
->start_frame
&= 1023;
1329 if (((now
- urb
->start_frame
) & 1023) < (unsigned) urb
->number_of_packets
) {
1330 dbg("iso_find_start: now between start_frame and end");
1335 /* check if either start_frame or start_frame+number_of_packets-1 lies between start_limit and stop_limit */
1339 if (((urb
->start_frame
- start_limit
) & 1023) < queued_size
||
1340 ((urb
->start_frame
+ urb
->number_of_packets
- 1 - start_limit
) & 1023) < queued_size
) {
1341 dbg("iso_find_start: start_frame %u number_of_packets %u start_limit %u stop_limit %u",
1342 urb
->start_frame
, urb
->number_of_packets
, start_limit
, stop_limit
);
1348 /*-------------------------------------------------------------------*/
1349 // submits USB interrupt (ie. polling ;-)
1350 // ASAP-flag set implicitely
1351 // if period==0, the the transfer is only done once
1353 _static
int uhci_submit_int_urb (urb_t
*urb
)
1355 uhci_t
*s
= (uhci_t
*) urb
->dev
->bus
->hcpriv
;
1356 urb_priv_t
*urb_priv
= urb
->hcpriv
;
1359 int status
, destination
;
1361 unsigned int pipe
= urb
->pipe
;
1363 if (urb
->interval
< 0 || urb
->interval
>= 256)
1366 if (urb
->interval
== 0)
1369 for (nint
= 0, n
= 1; nint
<= 8; nint
++, n
+= n
) // round interval down to 2^n
1371 if (urb
->interval
< n
) {
1372 urb
->interval
= n
/ 2;
1379 dbg("Rounded interval to %i, chain %i", urb
->interval
, nint
);
1381 urb
->start_frame
= UHCI_GET_CURRENT_FRAME (s
) & 1023; // remember start frame, just in case...
1383 urb
->number_of_packets
= 1;
1385 // INT allows only one packet
1386 if (urb
->transfer_buffer_length
> usb_maxpacket (urb
->dev
, pipe
, usb_pipeout (pipe
)))
1389 ret
= alloc_td (&td
, UHCI_PTR_DEPTH
);
1394 status
= (pipe
& TD_CTRL_LS
) | TD_CTRL_ACTIVE
| TD_CTRL_IOC
|
1395 (urb
->transfer_flags
& USB_DISABLE_SPD
? 0 : TD_CTRL_SPD
) | (3 << 27);
1397 destination
= (urb
->pipe
& PIPE_DEVEP_MASK
) | usb_packetid (urb
->pipe
) |
1398 (((urb
->transfer_buffer_length
- 1) & 0x7ff) << 21);
1401 info
= destination
| (usb_gettoggle (urb
->dev
, usb_pipeendpoint (pipe
), usb_pipeout (pipe
)) << TD_TOKEN_TOGGLE
);
1403 fill_td (td
, status
, info
, virt_to_bus (urb
->transfer_buffer
));
1404 list_add_tail (&td
->desc_list
, &urb_priv
->desc_list
);
1406 urb
->status
= -EINPROGRESS
;
1409 insert_td_horizontal (s
, s
->int_chain
[nint
], td
); // store in INT-TDs
1411 usb_dotoggle (urb
->dev
, usb_pipeendpoint (pipe
), usb_pipeout (pipe
));
1415 /*-------------------------------------------------------------------*/
1416 _static
int uhci_submit_iso_urb (urb_t
*urb
)
1418 uhci_t
*s
= (uhci_t
*) urb
->dev
->bus
->hcpriv
;
1419 urb_priv_t
*urb_priv
= urb
->hcpriv
;
1420 #ifdef ISO_SANITY_CHECK
1422 int maxsze
= usb_maxpacket (urb
->dev
, pipe
, usb_pipeout (pipe
));
1425 uhci_desc_t
*td
, **tdm
;
1426 int status
, destination
;
1427 unsigned long flags
;
1429 __save_flags(flags
);
1430 __cli(); // Disable IRQs to schedule all ISO-TDs in time
1431 ret
= iso_find_start (urb
); // adjusts urb->start_frame for later use
1436 tdm
= (uhci_desc_t
**) kmalloc (urb
->number_of_packets
* sizeof (uhci_desc_t
*), KMALLOC_FLAG
);
1443 // First try to get all TDs
1444 for (n
= 0; n
< urb
->number_of_packets
; n
++) {
1445 dbg("n:%d urb->iso_frame_desc[n].length:%d", n
, urb
->iso_frame_desc
[n
].length
);
1446 if (!urb
->iso_frame_desc
[n
].length
) {
1447 // allows ISO striping by setting length to zero in iso_descriptor
1452 #ifdef ISO_SANITY_CHECK
1453 if(urb
->iso_frame_desc
[n
].length
> maxsze
) {
1455 err("submit_iso: urb->iso_frame_desc[%d].length(%d)>%d",n
, urb
->iso_frame_desc
[n
].length
, maxsze
);
1461 ret
= alloc_td (&td
, UHCI_PTR_DEPTH
);
1464 int i
; // Cleanup allocated TDs
1466 for (i
= 0; i
< n
; n
++)
1468 delete_desc(tdm
[i
]);
1476 status
= TD_CTRL_ACTIVE
| TD_CTRL_IOS
; //| (urb->transfer_flags&USB_DISABLE_SPD?0:TD_CTRL_SPD);
1478 destination
= (urb
->pipe
& PIPE_DEVEP_MASK
) | usb_packetid (urb
->pipe
);
1481 // Queue all allocated TDs
1482 for (n
= 0; n
< urb
->number_of_packets
; n
++) {
1488 status
|= TD_CTRL_IOC
;
1490 fill_td (td
, status
, destination
| (((urb
->iso_frame_desc
[n
].length
- 1) & 0x7ff) << 21),
1491 virt_to_bus (urb
->transfer_buffer
+ urb
->iso_frame_desc
[n
].offset
));
1492 list_add_tail (&td
->desc_list
, &urb_priv
->desc_list
);
1495 urb
->status
= -EINPROGRESS
;
1498 insert_td_horizontal (s
, s
->iso_td
[(urb
->start_frame
+ n
) & 1023], td
); // store in iso-tds
1504 dbg("ISO-INT# %i, start %i, now %i", urb
->number_of_packets
, urb
->start_frame
, UHCI_GET_CURRENT_FRAME (s
) & 1023);
1508 __restore_flags(flags
);
1512 /*-------------------------------------------------------------------*/
1513 // returns: 0 (no transfer queued), urb* (this urb already queued)
1515 _static urb_t
* search_dev_ep (uhci_t
*s
, urb_t
*urb
)
1517 struct list_head
*p
;
1519 unsigned int mask
= usb_pipecontrol(urb
->pipe
) ? (~USB_DIR_IN
) : (~0);
1521 dbg("search_dev_ep:");
1525 for (; p
!= &s
->urb_list
; p
= p
->next
) {
1526 tmp
= list_entry (p
, urb_t
, urb_list
);
1527 dbg("urb: %p", tmp
);
1528 // we can accept this urb if it is not queued at this time
1529 // or if non-iso transfer requests should be scheduled for the same device and pipe
1530 if ((!usb_pipeisoc(urb
->pipe
) && (tmp
->dev
== urb
->dev
) && !((tmp
->pipe
^ urb
->pipe
) & mask
)) ||
1532 return tmp
; // found another urb already queued for processing
1538 /*-------------------------------------------------------------------*/
1539 _static
int uhci_submit_urb (urb_t
*urb
)
1542 urb_priv_t
*urb_priv
;
1544 unsigned long flags
;
1545 urb_t
*bulk_urb
=NULL
;
1547 if (!urb
->dev
|| !urb
->dev
->bus
)
1550 s
= (uhci_t
*) urb
->dev
->bus
->hcpriv
;
1551 //dbg("submit_urb: %p type %d",urb,usb_pipetype(urb->pipe));
1556 if (usb_pipedevice (urb
->pipe
) == s
->rh
.devnum
)
1557 return rh_submit_urb (urb
); /* virtual root hub */
1559 usb_inc_dev_use (urb
->dev
);
1561 spin_lock_irqsave (&s
->urb_list_lock
, flags
);
1563 bulk_urb
= search_dev_ep (s
, urb
);
1567 queue_dbg("found bulk urb %p\n",bulk_urb
);
1569 if ((usb_pipetype (urb
->pipe
) != PIPE_BULK
) ||
1570 ((usb_pipetype (urb
->pipe
) == PIPE_BULK
) &&
1571 (!(urb
->transfer_flags
& USB_QUEUE_BULK
) || !(bulk_urb
->transfer_flags
& USB_QUEUE_BULK
)))) {
1572 spin_unlock_irqrestore (&s
->urb_list_lock
, flags
);
1573 usb_dec_dev_use (urb
->dev
);
1574 err("ENXIO %08x, flags %x, urb %p, burb %p",urb
->pipe
,urb
->transfer_flags
,urb
,bulk_urb
);
1575 return -ENXIO
; // urb already queued
1580 urb_priv
= kmem_cache_alloc(urb_priv_kmem
, SLAB_FLAG
);
1582 urb_priv
= kmalloc (sizeof (urb_priv_t
), KMALLOC_FLAG
);
1585 usb_dec_dev_use (urb
->dev
);
1586 spin_unlock_irqrestore (&s
->urb_list_lock
, flags
);
1590 urb
->hcpriv
= urb_priv
;
1591 INIT_LIST_HEAD (&urb_priv
->desc_list
);
1592 urb_priv
->flags
= 0;
1593 dbg("submit_urb: scheduling %p", urb
);
1594 urb_priv
->next_queued_urb
= NULL
;
1595 urb_priv
->prev_queued_urb
= NULL
;
1596 urb_priv
->bottom_qh
= NULL
;
1597 urb_priv
->next_qh
= NULL
;
1599 if (usb_pipetype (urb
->pipe
) == PIPE_BULK
) {
1602 while (((urb_priv_t
*)bulk_urb
->hcpriv
)->next_queued_urb
) // find last queued bulk
1603 bulk_urb
=((urb_priv_t
*)bulk_urb
->hcpriv
)->next_queued_urb
;
1605 ((urb_priv_t
*)bulk_urb
->hcpriv
)->next_queued_urb
=urb
;
1607 atomic_inc (&s
->avoid_bulk
);
1608 ret
= uhci_submit_bulk_urb (urb
, bulk_urb
);
1609 atomic_dec (&s
->avoid_bulk
);
1610 spin_unlock_irqrestore (&s
->urb_list_lock
, flags
);
1613 spin_unlock_irqrestore (&s
->urb_list_lock
, flags
);
1614 switch (usb_pipetype (urb
->pipe
)) {
1615 case PIPE_ISOCHRONOUS
:
1616 ret
= uhci_submit_iso_urb (urb
);
1618 case PIPE_INTERRUPT
:
1619 ret
= uhci_submit_int_urb (urb
);
1622 ret
= uhci_submit_control_urb (urb
);
1629 dbg("submit_urb: scheduled with ret: %d", ret
);
1632 usb_dec_dev_use (urb
->dev
);
1634 kmem_cache_free(urb_priv_kmem
, urb_priv
);
1644 // Checks for URB timeout and removes bandwidth reclamation
1645 // if URB idles too long
1646 _static
void uhci_check_timeouts(uhci_t
*s
)
1648 struct list_head
*p
,*p2
;
1652 p
= s
->urb_list
.prev
;
1654 while (p
!= &s
->urb_list
) {
1659 urb
= list_entry (p2
, urb_t
, urb_list
);
1660 type
= usb_pipetype (urb
->pipe
);
1662 hcpriv
= (urb_priv_t
*)urb
->hcpriv
;
1664 if ( urb
->timeout
&&
1665 ((hcpriv
->started
+ urb
->timeout
) < jiffies
)) {
1666 urb
->transfer_flags
|= USB_TIMEOUT_KILLED
| USB_ASYNC_UNLINK
;
1667 async_dbg("uhci_check_timeout: timeout for %p",urb
);
1668 uhci_unlink_urb_async(s
, urb
);
1670 #ifdef CONFIG_USB_UHCI_HIGH_BANDWIDTH
1671 else if (((type
== PIPE_BULK
) || (type
== PIPE_CONTROL
)) &&
1672 (hcpriv
->use_loop
) &&
1673 ((hcpriv
->started
+ IDLE_TIMEOUT
) < jiffies
))
1674 disable_desc_loop(s
, urb
);
1678 s
->timeout_check
=jiffies
;
1681 /*-------------------------------------------------------------------
1683 -------------------------------------------------------------------*/
1685 _static __u8 root_hub_dev_des
[] =
1687 0x12, /* __u8 bLength; */
1688 0x01, /* __u8 bDescriptorType; Device */
1689 0x00, /* __u16 bcdUSB; v1.0 */
1691 0x09, /* __u8 bDeviceClass; HUB_CLASSCODE */
1692 0x00, /* __u8 bDeviceSubClass; */
1693 0x00, /* __u8 bDeviceProtocol; */
1694 0x08, /* __u8 bMaxPacketSize0; 8 Bytes */
1695 0x00, /* __u16 idVendor; */
1697 0x00, /* __u16 idProduct; */
1699 0x00, /* __u16 bcdDevice; */
1701 0x00, /* __u8 iManufacturer; */
1702 0x02, /* __u8 iProduct; */
1703 0x01, /* __u8 iSerialNumber; */
1704 0x01 /* __u8 bNumConfigurations; */
1708 /* Configuration descriptor */
1709 _static __u8 root_hub_config_des
[] =
1711 0x09, /* __u8 bLength; */
1712 0x02, /* __u8 bDescriptorType; Configuration */
1713 0x19, /* __u16 wTotalLength; */
1715 0x01, /* __u8 bNumInterfaces; */
1716 0x01, /* __u8 bConfigurationValue; */
1717 0x00, /* __u8 iConfiguration; */
1718 0x40, /* __u8 bmAttributes;
1719 Bit 7: Bus-powered, 6: Self-powered, 5 Remote-wakwup, 4..0: resvd */
1720 0x00, /* __u8 MaxPower; */
1723 0x09, /* __u8 if_bLength; */
1724 0x04, /* __u8 if_bDescriptorType; Interface */
1725 0x00, /* __u8 if_bInterfaceNumber; */
1726 0x00, /* __u8 if_bAlternateSetting; */
1727 0x01, /* __u8 if_bNumEndpoints; */
1728 0x09, /* __u8 if_bInterfaceClass; HUB_CLASSCODE */
1729 0x00, /* __u8 if_bInterfaceSubClass; */
1730 0x00, /* __u8 if_bInterfaceProtocol; */
1731 0x00, /* __u8 if_iInterface; */
1734 0x07, /* __u8 ep_bLength; */
1735 0x05, /* __u8 ep_bDescriptorType; Endpoint */
1736 0x81, /* __u8 ep_bEndpointAddress; IN Endpoint 1 */
1737 0x03, /* __u8 ep_bmAttributes; Interrupt */
1738 0x08, /* __u16 ep_wMaxPacketSize; 8 Bytes */
1740 0xff /* __u8 ep_bInterval; 255 ms */
1744 _static __u8 root_hub_hub_des
[] =
1746 0x09, /* __u8 bLength; */
1747 0x29, /* __u8 bDescriptorType; Hub-descriptor */
1748 0x02, /* __u8 bNbrPorts; */
1749 0x00, /* __u16 wHubCharacteristics; */
1751 0x01, /* __u8 bPwrOn2pwrGood; 2ms */
1752 0x00, /* __u8 bHubContrCurrent; 0 mA */
1753 0x00, /* __u8 DeviceRemovable; *** 7 Ports max *** */
1754 0xff /* __u8 PortPwrCtrlMask; *** 7 ports max *** */
1757 /*-------------------------------------------------------------------------*/
1758 /* prepare Interrupt pipe transaction data; HUB INTERRUPT ENDPOINT */
1759 _static
int rh_send_irq (urb_t
*urb
)
1763 uhci_t
*uhci
= urb
->dev
->bus
->hcpriv
;
1764 unsigned int io_addr
= uhci
->io_addr
;
1767 for (i
= 0; i
< uhci
->rh
.numports
; i
++) {
1768 data
|= ((inw (io_addr
+ USBPORTSC1
+ i
* 2) & 0xa) > 0 ? (1 << (i
+ 1)) : 0);
1769 len
= (i
+ 1) / 8 + 1;
1772 *(__u16
*) urb
->transfer_buffer
= cpu_to_le16 (data
);
1773 urb
->actual_length
= len
;
1776 if ((data
> 0) && (uhci
->rh
.send
!= 0)) {
1777 dbg("Root-Hub INT complete: port1: %x port2: %x data: %x",
1778 inw (io_addr
+ USBPORTSC1
), inw (io_addr
+ USBPORTSC2
), data
);
1779 urb
->complete (urb
);
1784 /*-------------------------------------------------------------------------*/
1785 /* Virtual Root Hub INTs are polled by this timer every "intervall" ms */
1786 _static
int rh_init_int_timer (urb_t
*urb
);
1788 _static
void rh_int_timer_do (unsigned long ptr
)
1791 urb_t
*urb
= (urb_t
*) ptr
;
1792 uhci_t
*uhci
= urb
->dev
->bus
->hcpriv
;
1794 if (uhci
->rh
.send
) {
1795 len
= rh_send_irq (urb
);
1797 urb
->actual_length
= len
;
1799 urb
->complete (urb
);
1802 rh_init_int_timer (urb
);
1805 /*-------------------------------------------------------------------------*/
1806 /* Root Hub INTs are polled by this timer, polling interval 20ms */
1807 /* This time is also used for URB-timeout checking */
1809 _static
int rh_init_int_timer (urb_t
*urb
)
1811 uhci_t
*uhci
= urb
->dev
->bus
->hcpriv
;
1813 uhci
->rh
.interval
= urb
->interval
;
1814 init_timer (&uhci
->rh
.rh_int_timer
);
1815 uhci
->rh
.rh_int_timer
.function
= rh_int_timer_do
;
1816 uhci
->rh
.rh_int_timer
.data
= (unsigned long) urb
;
1817 uhci
->rh
.rh_int_timer
.expires
= jiffies
+ (HZ
* 20) / 1000;
1818 add_timer (&uhci
->rh
.rh_int_timer
);
1823 /*-------------------------------------------------------------------------*/
1824 #define OK(x) len = (x); break
1826 #define CLR_RH_PORTSTAT(x) \
1827 status = inw(io_addr+USBPORTSC1+2*(wIndex-1)); \
1828 status = (status & 0xfff5) & ~(x); \
1829 outw(status, io_addr+USBPORTSC1+2*(wIndex-1))
1831 #define SET_RH_PORTSTAT(x) \
1832 status = inw(io_addr+USBPORTSC1+2*(wIndex-1)); \
1833 status = (status & 0xfff5) | (x); \
1834 outw(status, io_addr+USBPORTSC1+2*(wIndex-1))
1837 /*-------------------------------------------------------------------------*/
1839 ** Root Hub Control Pipe
1840 *************************/
1843 _static
int rh_submit_urb (urb_t
*urb
)
1845 struct usb_device
*usb_dev
= urb
->dev
;
1846 uhci_t
*uhci
= usb_dev
->bus
->hcpriv
;
1847 unsigned int pipe
= urb
->pipe
;
1848 devrequest
*cmd
= (devrequest
*) urb
->setup_packet
;
1849 void *data
= urb
->transfer_buffer
;
1850 int leni
= urb
->transfer_buffer_length
;
1855 unsigned int io_addr
= uhci
->io_addr
;
1863 if (usb_pipetype (pipe
) == PIPE_INTERRUPT
) {
1864 dbg("Root-Hub submit IRQ: every %d ms", urb
->interval
);
1867 uhci
->rh
.interval
= urb
->interval
;
1868 rh_init_int_timer (urb
);
1874 bmRType_bReq
= cmd
->requesttype
| cmd
->request
<< 8;
1875 wValue
= le16_to_cpu (cmd
->value
);
1876 wIndex
= le16_to_cpu (cmd
->index
);
1877 wLength
= le16_to_cpu (cmd
->length
);
1879 for (i
= 0; i
< 8; i
++)
1880 uhci
->rh
.c_p_r
[i
] = 0;
1882 dbg("Root-Hub: adr: %2x cmd(%1x): %04x %04x %04x %04x",
1883 uhci
->rh
.devnum
, 8, bmRType_bReq
, wValue
, wIndex
, wLength
);
1885 switch (bmRType_bReq
) {
1886 /* Request Destination:
1887 without flags: Device,
1888 RH_INTERFACE: interface,
1889 RH_ENDPOINT: endpoint,
1890 RH_CLASS means HUB here,
1891 RH_OTHER | RH_CLASS almost ever means HUB_PORT here
1895 *(__u16
*) data
= cpu_to_le16 (1);
1897 case RH_GET_STATUS
| RH_INTERFACE
:
1898 *(__u16
*) data
= cpu_to_le16 (0);
1900 case RH_GET_STATUS
| RH_ENDPOINT
:
1901 *(__u16
*) data
= cpu_to_le16 (0);
1903 case RH_GET_STATUS
| RH_CLASS
:
1904 *(__u32
*) data
= cpu_to_le32 (0);
1905 OK (4); /* hub power ** */
1906 case RH_GET_STATUS
| RH_OTHER
| RH_CLASS
:
1907 status
= inw (io_addr
+ USBPORTSC1
+ 2 * (wIndex
- 1));
1908 cstatus
= ((status
& USBPORTSC_CSC
) >> (1 - 0)) |
1909 ((status
& USBPORTSC_PEC
) >> (3 - 1)) |
1910 (uhci
->rh
.c_p_r
[wIndex
- 1] << (0 + 4));
1911 status
= (status
& USBPORTSC_CCS
) |
1912 ((status
& USBPORTSC_PE
) >> (2 - 1)) |
1913 ((status
& USBPORTSC_SUSP
) >> (12 - 2)) |
1914 ((status
& USBPORTSC_PR
) >> (9 - 4)) |
1915 (1 << 8) | /* power on ** */
1916 ((status
& USBPORTSC_LSDA
) << (-8 + 9));
1918 *(__u16
*) data
= cpu_to_le16 (status
);
1919 *(__u16
*) (data
+ 2) = cpu_to_le16 (cstatus
);
1922 case RH_CLEAR_FEATURE
| RH_ENDPOINT
:
1924 case (RH_ENDPOINT_STALL
):
1929 case RH_CLEAR_FEATURE
| RH_CLASS
:
1931 case (RH_C_HUB_OVER_CURRENT
):
1932 OK (0); /* hub power over current ** */
1936 case RH_CLEAR_FEATURE
| RH_OTHER
| RH_CLASS
:
1938 case (RH_PORT_ENABLE
):
1939 CLR_RH_PORTSTAT (USBPORTSC_PE
);
1941 case (RH_PORT_SUSPEND
):
1942 CLR_RH_PORTSTAT (USBPORTSC_SUSP
);
1944 case (RH_PORT_POWER
):
1945 OK (0); /* port power ** */
1946 case (RH_C_PORT_CONNECTION
):
1947 SET_RH_PORTSTAT (USBPORTSC_CSC
);
1949 case (RH_C_PORT_ENABLE
):
1950 SET_RH_PORTSTAT (USBPORTSC_PEC
);
1952 case (RH_C_PORT_SUSPEND
):
1953 /*** WR_RH_PORTSTAT(RH_PS_PSSC); */
1955 case (RH_C_PORT_OVER_CURRENT
):
1956 OK (0); /* port power over current ** */
1957 case (RH_C_PORT_RESET
):
1958 uhci
->rh
.c_p_r
[wIndex
- 1] = 0;
1963 case RH_SET_FEATURE
| RH_OTHER
| RH_CLASS
:
1965 case (RH_PORT_SUSPEND
):
1966 SET_RH_PORTSTAT (USBPORTSC_SUSP
);
1968 case (RH_PORT_RESET
):
1969 SET_RH_PORTSTAT (USBPORTSC_PR
);
1971 uhci
->rh
.c_p_r
[wIndex
- 1] = 1;
1972 CLR_RH_PORTSTAT (USBPORTSC_PR
);
1974 SET_RH_PORTSTAT (USBPORTSC_PE
);
1976 SET_RH_PORTSTAT (0xa);
1978 case (RH_PORT_POWER
):
1979 OK (0); /* port power ** */
1980 case (RH_PORT_ENABLE
):
1981 SET_RH_PORTSTAT (USBPORTSC_PE
);
1986 case RH_SET_ADDRESS
:
1987 uhci
->rh
.devnum
= wValue
;
1990 case RH_GET_DESCRIPTOR
:
1991 switch ((wValue
& 0xff00) >> 8) {
1992 case (0x01): /* device descriptor */
1993 len
= min (leni
, min (sizeof (root_hub_dev_des
), wLength
));
1994 memcpy (data
, root_hub_dev_des
, len
);
1996 case (0x02): /* configuration descriptor */
1997 len
= min (leni
, min (sizeof (root_hub_config_des
), wLength
));
1998 memcpy (data
, root_hub_config_des
, len
);
2000 case (0x03): /* string descriptors */
2001 len
= usb_root_hub_string (wValue
& 0xff,
2002 uhci
->io_addr
, "UHCI",
2005 OK (min (leni
, len
));
2011 case RH_GET_DESCRIPTOR
| RH_CLASS
:
2012 root_hub_hub_des
[2] = uhci
->rh
.numports
;
2013 len
= min (leni
, min (sizeof (root_hub_hub_des
), wLength
));
2014 memcpy (data
, root_hub_hub_des
, len
);
2017 case RH_GET_CONFIGURATION
:
2018 *(__u8
*) data
= 0x01;
2021 case RH_SET_CONFIGURATION
:
2027 dbg("Root-Hub stat port1: %x port2: %x",
2028 inw (io_addr
+ USBPORTSC1
), inw (io_addr
+ USBPORTSC2
));
2030 urb
->actual_length
= len
;
2033 urb
->complete (urb
);
2036 /*-------------------------------------------------------------------------*/
2038 _static
int rh_unlink_urb (urb_t
*urb
)
2040 uhci_t
*uhci
= urb
->dev
->bus
->hcpriv
;
2042 if (uhci
->rh
.urb
==urb
) {
2043 dbg("Root-Hub unlink IRQ");
2045 del_timer (&uhci
->rh
.rh_int_timer
);
2049 /*-------------------------------------------------------------------*/
2052 * Map status to standard result codes
2054 * <status> is (td->status & 0xFE0000) [a.k.a. uhci_status_bits(td->status)
2055 * <dir_out> is True for output TDs and False for input TDs.
2057 _static
int uhci_map_status (int status
, int dir_out
)
2061 if (status
& TD_CTRL_BITSTUFF
) /* Bitstuff error */
2063 if (status
& TD_CTRL_CRCTIMEO
) { /* CRC/Timeout */
2069 if (status
& TD_CTRL_NAK
) /* NAK */
2071 if (status
& TD_CTRL_BABBLE
) /* Babble */
2073 if (status
& TD_CTRL_DBUFERR
) /* Buffer error */
2075 if (status
& TD_CTRL_STALLED
) /* Stalled */
2077 if (status
& TD_CTRL_ACTIVE
) /* Active */
2084 * Only the USB core should call uhci_alloc_dev and uhci_free_dev
2086 _static
int uhci_alloc_dev (struct usb_device
*usb_dev
)
2091 _static
void uhci_unlink_urbs(uhci_t
*s
, struct usb_device
*usb_dev
, int remove_all
)
2093 unsigned long flags
;
2094 struct list_head
*p
;
2095 struct list_head
*p2
;
2098 spin_lock_irqsave (&s
->urb_list_lock
, flags
);
2099 p
= s
->urb_list
.prev
;
2100 while (p
!= &s
->urb_list
) {
2103 urb
= list_entry (p2
, urb_t
, urb_list
);
2104 dbg("urb: %p, dev %p, %p", urb
, usb_dev
,urb
->dev
);
2106 //urb->transfer_flags |=USB_ASYNC_UNLINK;
2108 if (remove_all
|| (usb_dev
== urb
->dev
)) {
2109 spin_unlock_irqrestore (&s
->urb_list_lock
, flags
);
2110 warn("forced removing of queued URB %p due to disconnect",urb
);
2111 uhci_unlink_urb(urb
);
2112 urb
->dev
= NULL
; // avoid further processing of this UR
2113 spin_lock_irqsave (&s
->urb_list_lock
, flags
);
2114 p
= s
->urb_list
.prev
;
2117 spin_unlock_irqrestore (&s
->urb_list_lock
, flags
);
2120 _static
int uhci_free_dev (struct usb_device
*usb_dev
)
2125 if(!usb_dev
|| !usb_dev
->bus
|| !usb_dev
->bus
->hcpriv
)
2128 s
=(uhci_t
*) usb_dev
->bus
->hcpriv
;
2129 uhci_unlink_urbs(s
, usb_dev
, 0);
2135 * uhci_get_current_frame_number()
2137 * returns the current frame number for a USB bus/controller.
2139 _static
int uhci_get_current_frame_number (struct usb_device
*usb_dev
)
2141 return UHCI_GET_CURRENT_FRAME ((uhci_t
*) usb_dev
->bus
->hcpriv
);
2144 struct usb_operations uhci_device_operations
=
2148 uhci_get_current_frame_number
,
2154 * For IN-control transfers, process_transfer gets a bit more complicated,
2155 * since there are devices that return less data (eg. strings) than they
2156 * have announced. This leads to a queue abort due to the short packet,
2157 * the status stage is not executed. If this happens, the status stage
2158 * is manually re-executed.
2159 * mode: 0: QHs already unlinked
2162 _static
int process_transfer (uhci_t
*s
, urb_t
*urb
, int mode
)
2165 urb_priv_t
*urb_priv
= urb
->hcpriv
;
2166 struct list_head
*qhl
= urb_priv
->desc_list
.next
;
2167 uhci_desc_t
*qh
= list_entry (qhl
, uhci_desc_t
, desc_list
);
2168 struct list_head
*p
= qh
->vertical
.next
;
2169 uhci_desc_t
*desc
= list_entry (urb_priv
->desc_list
.prev
, uhci_desc_t
, desc_list
);
2170 uhci_desc_t
*last_desc
= list_entry (desc
->vertical
.prev
, uhci_desc_t
, vertical
);
2171 int data_toggle
= usb_gettoggle (urb
->dev
, usb_pipeendpoint (urb
->pipe
), usb_pipeout (urb
->pipe
)); // save initial data_toggle
2172 int maxlength
; // extracted and remapped info from TD
2176 //dbg("process_transfer: urb contains bulk/control request");
2178 /* if the status phase has been retriggered and the
2179 queue is empty or the last status-TD is inactive, the retriggered
2180 status stage is completed
2183 if (urb_priv
->flags
&&
2184 ((qh
->hw
.qh
.element
== UHCI_PTR_TERM
) ||(!(last_desc
->hw
.td
.status
& TD_CTRL_ACTIVE
))))
2185 goto transfer_finished
;
2187 urb
->actual_length
=0;
2189 for (; p
!= &qh
->vertical
; p
= p
->next
) {
2190 desc
= list_entry (p
, uhci_desc_t
, vertical
);
2192 if (desc
->hw
.td
.status
& TD_CTRL_ACTIVE
) // do not process active TDs
2195 actual_length
= (desc
->hw
.td
.status
+ 1) & 0x7ff; // extract transfer parameters from TD
2196 maxlength
= (((desc
->hw
.td
.info
>> 21) & 0x7ff) + 1) & 0x7ff;
2197 status
= uhci_map_status (uhci_status_bits (desc
->hw
.td
.status
), usb_pipeout (urb
->pipe
));
2199 if (status
== -EPIPE
) { // see if EP is stalled
2200 // set up stalled condition
2201 usb_endpoint_halt (urb
->dev
, usb_pipeendpoint (urb
->pipe
), usb_pipeout (urb
->pipe
));
2204 if (status
&& (status
!= -EPIPE
)) { // if any error occurred stop processing of further TDs
2205 // only set ret if status returned an error
2211 else if ((desc
->hw
.td
.info
& 0xff) != USB_PID_SETUP
)
2212 urb
->actual_length
+= actual_length
;
2214 // got less data than requested
2215 if ( (actual_length
< maxlength
)) {
2216 if (urb
->transfer_flags
& USB_DISABLE_SPD
) {
2217 status
= -EREMOTEIO
; // treat as real error
2218 dbg("process_transfer: SPD!!");
2219 break; // exit after this TD because SP was detected
2222 // short read during control-IN: re-start status stage
2223 if ((usb_pipetype (urb
->pipe
) == PIPE_CONTROL
)) {
2224 if (uhci_packetid(last_desc
->hw
.td
.info
) == USB_PID_OUT
) {
2226 qh
->hw
.qh
.element
= virt_to_bus (last_desc
); // re-trigger status stage
2227 dbg("short packet during control transfer, retrigger status stage @ %p",last_desc
);
2228 //uhci_show_td (desc);
2229 //uhci_show_td (last_desc);
2230 urb_priv
->flags
= 1; // mark as short control packet
2234 // all other cases: short read is OK
2235 data_toggle
= uhci_toggle (desc
->hw
.td
.info
);
2241 data_toggle
= uhci_toggle (desc
->hw
.td
.info
);
2242 queue_dbg("process_transfer: len:%d status:%x mapped:%x toggle:%d", actual_length
, desc
->hw
.td
.status
,status
, data_toggle
);
2246 usb_settoggle (urb
->dev
, usb_pipeendpoint (urb
->pipe
), usb_pipeout (urb
->pipe
), !data_toggle
);
2250 uhci_clean_transfer(s
, urb
, qh
, (mode
==0?2:1));
2252 urb
->status
= status
;
2254 #ifdef CONFIG_USB_UHCI_HIGH_BANDWIDTH
2255 disable_desc_loop(s
,urb
);
2258 queue_dbg("process_transfer: (end) urb %p, wanted len %d, len %d status %x err %d",
2259 urb
,urb
->transfer_buffer_length
,urb
->actual_length
, urb
->status
, urb
->error_count
);
2263 _static
int process_interrupt (uhci_t
*s
, urb_t
*urb
)
2265 int i
, ret
= -EINPROGRESS
;
2266 urb_priv_t
*urb_priv
= urb
->hcpriv
;
2267 struct list_head
*p
= urb_priv
->desc_list
.next
;
2268 uhci_desc_t
*desc
= list_entry (urb_priv
->desc_list
.prev
, uhci_desc_t
, desc_list
);
2273 //dbg("urb contains interrupt request");
2275 for (i
= 0; p
!= &urb_priv
->desc_list
; p
= p
->next
, i
++) // Maybe we allow more than one TD later ;-)
2277 desc
= list_entry (p
, uhci_desc_t
, desc_list
);
2279 if (desc
->hw
.td
.status
& TD_CTRL_ACTIVE
) {
2280 // do not process active TDs
2281 //dbg("TD ACT Status @%p %08x",desc,desc->hw.td.status);
2285 if (!desc
->hw
.td
.status
& TD_CTRL_IOC
) {
2286 // do not process one-shot TDs, no recycling
2289 // extract transfer parameters from TD
2291 actual_length
= (desc
->hw
.td
.status
+ 1) & 0x7ff;
2292 status
= uhci_map_status (uhci_status_bits (desc
->hw
.td
.status
), usb_pipeout (urb
->pipe
));
2294 // see if EP is stalled
2295 if (status
== -EPIPE
) {
2296 // set up stalled condition
2297 usb_endpoint_halt (urb
->dev
, usb_pipeendpoint (urb
->pipe
), usb_pipeout (urb
->pipe
));
2300 // if any error occured: ignore this td, and continue
2302 //uhci_show_td (desc);
2307 urb
->actual_length
= actual_length
;
2310 if (urb
->complete
) {
2311 //dbg("process_interrupt: calling completion, status %i",status);
2312 urb
->status
= status
;
2313 ((urb_priv_t
*)urb
->hcpriv
)->flags
=1; // if unlink_urb is called during completion
2315 spin_unlock(&s
->urb_list_lock
);
2317 urb
->complete ((struct urb
*) urb
);
2319 spin_lock(&s
->urb_list_lock
);
2321 ((urb_priv_t
*)urb
->hcpriv
)->flags
=0;
2324 if ((urb
->status
!= -ECONNABORTED
) && (urb
->status
!= ECONNRESET
) &&
2325 (urb
->status
!= -ENOENT
)) {
2327 urb
->status
= -EINPROGRESS
;
2329 // Recycle INT-TD if interval!=0, else mark TD as one-shot
2330 if (urb
->interval
) {
2332 desc
->hw
.td
.info
&= ~(1 << TD_TOKEN_TOGGLE
);
2334 ((urb_priv_t
*)urb
->hcpriv
)->started
=jiffies
;
2335 desc
->hw
.td
.info
|= (usb_gettoggle (urb
->dev
, usb_pipeendpoint (urb
->pipe
),
2336 usb_pipeout (urb
->pipe
)) << TD_TOKEN_TOGGLE
);
2337 usb_dotoggle (urb
->dev
, usb_pipeendpoint (urb
->pipe
), usb_pipeout (urb
->pipe
));
2339 desc
->hw
.td
.info
|= (!usb_gettoggle (urb
->dev
, usb_pipeendpoint (urb
->pipe
),
2340 usb_pipeout (urb
->pipe
)) << TD_TOKEN_TOGGLE
);
2342 desc
->hw
.td
.status
= (urb
->pipe
& TD_CTRL_LS
) | TD_CTRL_ACTIVE
| TD_CTRL_IOC
|
2343 (urb
->transfer_flags
& USB_DISABLE_SPD
? 0 : TD_CTRL_SPD
) | (3 << 27);
2347 uhci_unlink_urb_async(s
, urb
);
2348 desc
->hw
.td
.status
&= ~TD_CTRL_IOC
; // inactivate TD
2356 // mode: 1: force processing, don't unlink tds (already unlinked)
2357 _static
int process_iso (uhci_t
*s
, urb_t
*urb
, int mode
)
2361 urb_priv_t
*urb_priv
= urb
->hcpriv
;
2362 struct list_head
*p
= urb_priv
->desc_list
.next
;
2363 uhci_desc_t
*desc
= list_entry (urb_priv
->desc_list
.prev
, uhci_desc_t
, desc_list
);
2365 dbg("urb contains iso request");
2366 if ((desc
->hw
.td
.status
& TD_CTRL_ACTIVE
) && !mode
)
2367 return -EXDEV
; // last TD not finished
2369 urb
->error_count
= 0;
2370 urb
->actual_length
= 0;
2372 dbg("process iso urb %p, %li, %i, %i, %i %08x",urb
,jiffies
,UHCI_GET_CURRENT_FRAME(s
),
2373 urb
->number_of_packets
,mode
,desc
->hw
.td
.status
);
2375 for (i
= 0; p
!= &urb_priv
->desc_list
; i
++) {
2376 desc
= list_entry (p
, uhci_desc_t
, desc_list
);
2378 //uhci_show_td(desc);
2379 if (desc
->hw
.td
.status
& TD_CTRL_ACTIVE
) {
2380 // means we have completed the last TD, but not the TDs before
2381 desc
->hw
.td
.status
&= ~TD_CTRL_ACTIVE
;
2382 dbg("TD still active (%x)- grrr. paranoia!", desc
->hw
.td
.status
);
2384 urb
->iso_frame_desc
[i
].status
= ret
;
2385 unlink_td (s
, desc
, 1);
2386 // FIXME: immediate deletion may be dangerous
2391 unlink_td (s
, desc
, 1);
2393 if (urb
->number_of_packets
<= i
) {
2394 dbg("urb->number_of_packets (%d)<=(%d)", urb
->number_of_packets
, i
);
2399 if (urb
->iso_frame_desc
[i
].offset
+ urb
->transfer_buffer
!= bus_to_virt (desc
->hw
.td
.buffer
)) {
2400 // Hm, something really weird is going on
2401 dbg("Pointer Paranoia: %p!=%p", urb
->iso_frame_desc
[i
].offset
+ urb
->transfer_buffer
, bus_to_virt (desc
->hw
.td
.buffer
));
2403 urb
->iso_frame_desc
[i
].status
= ret
;
2406 urb
->iso_frame_desc
[i
].actual_length
= (desc
->hw
.td
.status
+ 1) & 0x7ff;
2407 urb
->iso_frame_desc
[i
].status
= uhci_map_status (uhci_status_bits (desc
->hw
.td
.status
), usb_pipeout (urb
->pipe
));
2408 urb
->actual_length
+= urb
->iso_frame_desc
[i
].actual_length
;
2412 if (urb
->iso_frame_desc
[i
].status
!= 0) {
2414 urb
->status
= urb
->iso_frame_desc
[i
].status
;
2416 dbg("process_iso: %i: len:%d %08x status:%x",
2417 i
, urb
->iso_frame_desc
[i
].actual_length
, desc
->hw
.td
.status
,urb
->iso_frame_desc
[i
].status
);
2424 dbg("process_iso: exit %i (%d), actual_len %i", i
, ret
,urb
->actual_length
);
2429 _static
int process_urb (uhci_t
*s
, struct list_head
*p
)
2435 urb
=list_entry (p
, urb_t
, urb_list
);
2436 //dbg("process_urb: found queued urb: %p", urb);
2438 switch (usb_pipetype (urb
->pipe
)) {
2440 ret
= process_transfer (s
, urb
, 1);
2443 if (!s
->avoid_bulk
.counter
)
2444 ret
= process_transfer (s
, urb
, 1);
2448 case PIPE_ISOCHRONOUS
:
2449 ret
= process_iso (s
, urb
, 0);
2451 case PIPE_INTERRUPT
:
2452 ret
= process_interrupt (s
, urb
);
2456 if (urb
->status
!= -EINPROGRESS
) {
2459 dbg("dequeued urb: %p", urb
);
2460 dequeue_urb (s
, urb
);
2463 kmem_cache_free(urb_priv_kmem
, urb
->hcpriv
);
2465 kfree (urb
->hcpriv
);
2468 if ((usb_pipetype (urb
->pipe
) != PIPE_INTERRUPT
)) {
2469 urb_t
*tmp
= urb
->next
; // pointer to first urb
2474 if (tmp
->status
!= -EINPROGRESS
) {
2480 while (tmp
!= NULL
&& tmp
!= urb
->next
);
2481 if (tmp
== urb
->next
)
2485 spin_lock(&urb
->lock
);
2486 spin_unlock(&s
->urb_list_lock
);
2488 // In case you need the current URB status for your completion handler (before resubmit)
2489 if (urb
->complete
&& (!proceed
)) {
2490 dbg("process_transfer: calling early completion");
2491 urb
->complete ((struct urb
*) urb
);
2492 if (!proceed
&& is_ring
&& (urb
->status
!= -ENOENT
))
2493 uhci_submit_urb (urb
);
2496 if (proceed
&& urb
->next
) {
2497 // if there are linked urbs - handle submitting of them right now.
2498 tmp
= urb
->next
; // pointer to first urb
2501 if ((tmp
->status
!= -EINPROGRESS
) && (tmp
->status
!= -ENOENT
) && uhci_submit_urb (tmp
) != 0)
2505 while (tmp
!= NULL
&& tmp
!= urb
->next
); // submit until we reach NULL or our own pointer or submit fails
2507 if (urb
->complete
) {
2508 dbg("process_transfer: calling completion");
2509 urb
->complete ((struct urb
*) urb
);
2513 usb_dec_dev_use (urb
->dev
);
2514 spin_unlock(&urb
->lock
);
2515 spin_lock(&s
->urb_list_lock
);
2522 _static
void uhci_interrupt (int irq
, void *__uhci
, struct pt_regs
*regs
)
2525 unsigned int io_addr
= s
->io_addr
;
2526 unsigned short status
;
2527 struct list_head
*p
, *p2
;
2530 * Read the interrupt status, and write it back to clear the
2534 status
= inw (io_addr
+ USBSTS
);
2536 if (!status
) /* shared interrupt, not mine */
2542 warn("interrupt, status %x, frame# %i", status
,
2543 UHCI_GET_CURRENT_FRAME(s
));
2545 // remove host controller halted state
2546 if ((status
&0x20) && (s
->running
)) {
2547 outw (USBCMD_RS
| inw(io_addr
+ USBCMD
), io_addr
+ USBCMD
);
2549 //uhci_show_status (s);
2552 * traverse the list in *reverse* direction, because new entries
2553 * may be added at the end.
2554 * also, because process_urb may unlink the current urb,
2555 * we need to advance the list before
2558 spin_lock (&s
->urb_list_lock
);
2560 s
->unlink_urb_done
=0;
2561 p
= s
->urb_list
.prev
;
2563 while (p
!= &s
->urb_list
) {
2566 process_urb (s
, p2
);
2567 if (s
->unlink_urb_done
) {
2568 s
->unlink_urb_done
=0;
2572 if ((jiffies
- s
->timeout_check
) > (HZ
/30))
2573 uhci_check_timeouts(s
);
2576 uhci_cleanup_unlink(s
, 0);
2577 uhci_switch_timer_int(s
);
2579 spin_unlock (&s
->urb_list_lock
);
2581 outw (status
, io_addr
+ USBSTS
);
2583 //dbg("uhci_interrupt: done");
2586 _static
void reset_hc (uhci_t
*s
)
2588 unsigned int io_addr
= s
->io_addr
;
2591 /* Global reset for 50ms */
2592 outw (USBCMD_GRESET
, io_addr
+ USBCMD
);
2594 outw (0, io_addr
+ USBCMD
);
2598 _static
void start_hc (uhci_t
*s
)
2600 unsigned int io_addr
= s
->io_addr
;
2604 * Reset the HC - this will force us to get a
2605 * new notification of any already connected
2606 * ports due to the virtual disconnect that it
2609 outw (USBCMD_HCRESET
, io_addr
+ USBCMD
);
2611 while (inw (io_addr
+ USBCMD
) & USBCMD_HCRESET
) {
2613 err("USBCMD_HCRESET timed out!");
2618 /* Turn on all interrupts */
2619 outw (USBINTR_TIMEOUT
| USBINTR_RESUME
| USBINTR_IOC
| USBINTR_SP
, io_addr
+ USBINTR
);
2621 /* Start at frame 0 */
2622 outw (0, io_addr
+ USBFRNUM
);
2623 outl (virt_to_bus (s
->framelist
), io_addr
+ USBFLBASEADD
);
2625 /* Run and mark it configured with a 64-byte max packet */
2626 outw (USBCMD_RS
| USBCMD_CF
| USBCMD_MAXP
, io_addr
+ USBCMD
);
2631 _static
void uhci_cleanup_dev(uhci_t
*s
)
2633 struct usb_device
*root_hub
= s
->bus
->root_hub
;
2635 s
->running
= 0; // Don't allow submit_urb
2638 usb_disconnect (&root_hub
);
2643 uhci_unlink_urbs (s
, 0, 1); // Forced unlink of remaining URBs
2644 uhci_cleanup_unlink (s
, 1); // force cleanup of async killed URBs
2646 usb_deregister_bus (s
->bus
);
2648 release_region (s
->io_addr
, s
->io_size
);
2649 free_irq (s
->irq
, s
);
2650 usb_free_bus (s
->bus
);
2655 _static
int __init
uhci_start_usb (uhci_t
*s
)
2657 /* connect the virtual root hub */
2658 struct usb_device
*usb_dev
;
2660 usb_dev
= usb_alloc_dev (NULL
, s
->bus
);
2664 s
->bus
->root_hub
= usb_dev
;
2665 usb_connect (usb_dev
);
2667 if (usb_new_device (usb_dev
) != 0) {
2668 usb_free_dev (usb_dev
);
2675 _static
int handle_pm_event (struct pm_dev
*dev
, pm_request_t rqst
, void *data
)
2677 uhci_t
*s
= (uhci_t
*) dev
->data
;
2678 dbg("handle_apm_event(%d)", rqst
);
2692 _static
int __init
alloc_uhci (struct pci_dev
*dev
, int irq
, unsigned int io_addr
, unsigned int io_size
)
2695 struct usb_bus
*bus
;
2696 struct pm_dev
*pmdev
;
2697 char buf
[8], *bufp
= buf
;
2700 sprintf(buf
, "%d", irq
);
2702 bufp
= __irq_itoa(irq
);
2704 printk(KERN_INFO __FILE__
": USB UHCI at I/O 0x%x, IRQ %s\n",
2707 s
= kmalloc (sizeof (uhci_t
), GFP_KERNEL
);
2711 memset (s
, 0, sizeof (uhci_t
));
2712 INIT_LIST_HEAD (&s
->free_desc
);
2713 INIT_LIST_HEAD (&s
->urb_list
);
2714 INIT_LIST_HEAD (&s
->urb_unlinked
);
2715 spin_lock_init (&s
->urb_list_lock
);
2716 spin_lock_init (&s
->qh_lock
);
2717 spin_lock_init (&s
->td_lock
);
2718 atomic_set(&s
->avoid_bulk
, 0);
2719 s
->timeout_urbs
= 0;
2721 s
->io_addr
= io_addr
;
2722 s
->io_size
= io_size
;
2723 s
->next
= devs
; //chain new uhci device into global list
2724 s
->timeout_check
= 0;
2727 bus
= usb_alloc_bus (&uhci_device_operations
);
2736 /* UHCI specs says devices must have 2 ports, but goes on to say */
2737 /* they may have more but give no way to determine how many they */
2738 /* have, so default to 2 */
2739 /* According to the UHCI spec, Bit 7 is always set to 1. So we try */
2740 /* to use this to our advantage */
2742 for (s
->maxports
= 0; s
->maxports
< (io_size
- 0x10) / 2; s
->maxports
++) {
2743 unsigned int portstatus
;
2745 portstatus
= inw (io_addr
+ 0x10 + (s
->maxports
* 2));
2746 dbg("port %i, adr %x status %x", s
->maxports
,
2747 io_addr
+ 0x10 + (s
->maxports
* 2), portstatus
);
2748 if (!(portstatus
& 0x0080))
2751 warn("Detected %d ports", s
->maxports
);
2753 /* This is experimental so anything less than 2 or greater than 8 is */
2754 /* something weird and we'll ignore it */
2755 if (s
->maxports
< 2 || s
->maxports
> 8) {
2756 dbg("Port count misdetected, forcing to 2 ports");
2760 s
->rh
.numports
= s
->maxports
;
2762 if (init_skel (s
)) {
2768 request_region (s
->io_addr
, io_size
, MODNAME
);
2770 usb_register_bus (s
->bus
);
2774 if (request_irq (irq
, uhci_interrupt
, SA_SHIRQ
, MODNAME
, s
)) {
2775 err("request_irq %d failed!",irq
);
2778 release_region (s
->io_addr
, s
->io_size
);
2786 if(uhci_start_usb (s
) < 0) {
2787 uhci_cleanup_dev(s
);
2791 //chain new uhci device into global list
2794 pmdev
= pm_register(PM_PCI_DEV
, PM_PCI_ID(dev
), handle_pm_event
);
2801 _static
int __init
start_uhci (struct pci_dev
*dev
)
2805 /* Search for the IO base address.. */
2806 for (i
= 0; i
< 6; i
++) {
2808 unsigned int io_addr
= dev
->resource
[i
].start
;
2809 unsigned int io_size
=
2810 dev
->resource
[i
].end
- dev
->resource
[i
].start
+ 1;
2811 if (!(dev
->resource
[i
].flags
& 1))
2814 /* Is it already in use? */
2815 if (check_region (io_addr
, io_size
))
2817 /* disable legacy emulation */
2818 pci_write_config_word (dev
, USBLEGSUP
, USBLEGSUP_DEFAULT
);
2820 return alloc_uhci(dev
, dev
->irq
, io_addr
, io_size
);
2825 int __init
uhci_init (void)
2827 int retval
= -ENODEV
;
2828 struct pci_dev
*dev
= NULL
;
2834 uhci_desc_kmem
= kmem_cache_create("uhci_desc", sizeof(uhci_desc_t
), 0, SLAB_HWCACHE_ALIGN
, NULL
, NULL
);
2836 if(!uhci_desc_kmem
) {
2837 err("kmem_cache_create for uhci_desc failed (out of memory)");
2841 urb_priv_kmem
= kmem_cache_create("urb_priv", sizeof(urb_priv_t
), 0, SLAB_HWCACHE_ALIGN
, NULL
, NULL
);
2843 if(!urb_priv_kmem
) {
2844 err("kmem_cache_create for urb_priv_t failed (out of memory)");
2850 #ifdef CONFIG_USB_UHCI_HIGH_BANDWIDTH
2851 info("High bandwidth mode enabled");
2854 dev
= pci_find_class (PCI_CLASS_SERIAL_USB
<< 8, dev
);
2859 pci_read_config_byte (dev
, PCI_CLASS_PROG
, &type
);
2863 if (pci_enable_device (dev
) < 0)
2868 err("Found UHCI device with no IRQ assigned. Check BIOS settings!");
2873 retval
= start_uhci (dev
);
2882 void __exit
uhci_cleanup (void)
2885 while ((s
= devs
)) {
2887 uhci_cleanup_dev(s
);
2890 if(kmem_cache_destroy(uhci_desc_kmem
))
2891 err("uhci_desc_kmem remained");
2893 if(kmem_cache_destroy(urb_priv_kmem
))
2894 err("urb_priv_kmem remained");
2899 int init_module (void)
2901 return uhci_init ();
2904 void cleanup_module (void)
2906 pm_unregister_all (handle_pm_event
);
2910 MODULE_AUTHOR("Georg Acher, Deti Fliegl, Thomas Sailer, Roman Weissgaerber");
2911 MODULE_DESCRIPTION("USB Universal Host Controller Interface driver");