2 * Universal Host Controller Interface driver for USB (take II).
4 * (c) 1999 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.232 2000/06/11 13:18:30 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.232 $ 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
));
892 td
->hw
.td
.status
|= TD_CTRL_IOC
; // last one generates INT
894 insert_td (s
, qh
, td
, UHCI_PTR_DEPTH
* depth_first
);
895 usb_dotoggle (urb
->dev
, usb_pipeendpoint (pipe
), usb_pipeout (pipe
));
899 list_add (&qh
->desc_list
, &urb_priv
->desc_list
);
901 if (urb
->transfer_flags
& USB_QUEUE_BULK
) {
902 qh
->hw
.qh
.element
&=~UHCI_PTR_TERM
;
903 append_qh(s
, td
, bqh
, UHCI_PTR_DEPTH
* depth_first
);
906 urb
->status
= -EINPROGRESS
;
907 queue_urb_unlocked (s
, urb
);
909 qh
->hw
.qh
.element
&= ~UHCI_PTR_TERM
;
912 if (urb
->transfer_flags
& USB_QUEUE_BULK
) {
913 spin_lock (&s
->td_lock
); // both QHs in one go
914 insert_qh (s
, s
->chain_end
, qh
, 0); // Main QH
915 insert_qh (s
, s
->chain_end
, nqh
, 0); // Helper QH
916 spin_unlock (&s
->td_lock
);
919 insert_qh (s
, s
->chain_end
, qh
, 0);
922 //uhci_show_queue(s->bulk_chain);
923 //dbg("uhci_submit_bulk_urb: exit\n");
926 /*-------------------------------------------------------------------*/
927 _static
void uhci_clean_iso_step1(uhci_t
*s
, urb_priv_t
*urb_priv
)
932 for (p
= urb_priv
->desc_list
.next
; p
!= &urb_priv
->desc_list
; p
= p
->next
) {
933 td
= list_entry (p
, uhci_desc_t
, desc_list
);
934 unlink_td (s
, td
, 1);
937 /*-------------------------------------------------------------------*/
938 _static
void uhci_clean_iso_step2(uhci_t
*s
, urb_priv_t
*urb_priv
)
943 while ((p
= urb_priv
->desc_list
.next
) != &urb_priv
->desc_list
) {
944 td
= list_entry (p
, uhci_desc_t
, desc_list
);
949 /*-------------------------------------------------------------------*/
950 // mode: 0: unlink + no deletion mark, 1: regular (unlink/delete-mark), 2: don't unlink
951 // looks a bit complicated because of all the bulk queueing goodies
953 _static
void uhci_clean_transfer (uhci_t
*s
, urb_t
*urb
, uhci_desc_t
*qh
, int mode
)
955 uhci_desc_t
*bqh
, *nqh
, *prevqh
;
957 urb_priv_t
*priv
=(urb_priv_t
*)urb
->hcpriv
;
959 now
=UHCI_GET_CURRENT_FRAME(s
);
961 dbg("clean transfer urb %p, qh %p, mode %i",urb
,qh
,mode
);
964 if (!priv
->next_queued_urb
) { // no more appended bulk queues
969 if (priv
->prev_queued_urb
) {
970 urb_priv_t
* ppriv
=(urb_priv_t
*)priv
->prev_queued_urb
->hcpriv
;
972 ppriv
->bottom_qh
= priv
->bottom_qh
;
973 ppriv
->next_queued_urb
= NULL
;
975 else if (bqh
) { // queue dead
982 nqh
->last_used
= bqh
->last_used
= now
;
983 list_add_tail (&nqh
->horizontal
, &s
->free_desc
);
984 list_add_tail (&bqh
->horizontal
, &s
->free_desc
);
988 else { // there are queued urbs following
992 nurb
=priv
->next_queued_urb
;
993 spin_lock_irqsave (&s
->qh_lock
, flags
);
995 if (!priv
->prev_queued_urb
) { // top
997 prevqh
= list_entry (qh
->horizontal
.prev
, uhci_desc_t
, horizontal
);
998 prevqh
->hw
.qh
.head
= virt_to_bus(bqh
) | UHCI_PTR_QH
;
999 queue_dbg ("TOP relink of %p to %p-%p",qh
,prevqh
,bqh
);
1001 list_del (&qh
->horizontal
);
1002 list_add (&bqh
->horizontal
, &prevqh
->horizontal
);
1005 else { //intermediate
1006 urb_priv_t
* ppriv
=(urb_priv_t
*)priv
->prev_queued_urb
->hcpriv
;
1009 bnqh
=list_entry (&((urb_priv_t
*)(nurb
->hcpriv
))->desc_list
.next
, uhci_desc_t
, desc_list
);
1010 ppriv
->bottom_qh
=bnqh
;
1011 ppriv
->next_queued_urb
=nurb
;
1014 prevqh
= list_entry (ppriv
->desc_list
.next
, uhci_desc_t
, desc_list
);
1015 prevqh
->hw
.qh
.head
= virt_to_bus(bqh
) | UHCI_PTR_QH
;
1016 queue_dbg ("IM relink of %p to %p-%p",qh
,prevqh
,bqh
);
1020 spin_unlock_irqrestore (&s
->qh_lock
, flags
);
1021 ((urb_priv_t
*)nurb
->hcpriv
)->prev_queued_urb
=priv
->prev_queued_urb
;
1025 qh
->last_used
= now
;
1026 list_add_tail (&qh
->horizontal
, &s
->free_desc
); // mark for later deletion
1029 /*-------------------------------------------------------------------*/
1030 // unlinks an urb by dequeuing its qh, waits some frames and forgets it
1031 _static
int uhci_unlink_urb_sync (uhci_t
*s
, urb_t
*urb
)
1034 urb_priv_t
*urb_priv
;
1035 unsigned long flags
=0;
1037 spin_lock_irqsave (&s
->urb_list_lock
, flags
);
1039 if (urb
->status
== -EINPROGRESS
) {
1040 // URB probably still in work
1041 dequeue_urb (s
, urb
);
1042 uhci_switch_timer_int(s
);
1043 s
->unlink_urb_done
=1;
1044 spin_unlock_irqrestore (&s
->urb_list_lock
, flags
);
1046 urb
->status
= -ENOENT
; // mark urb as killed
1047 urb_priv
= urb
->hcpriv
;
1049 switch (usb_pipetype (urb
->pipe
)) {
1051 case PIPE_INTERRUPT
:
1052 usb_dotoggle (urb
->dev
, usb_pipeendpoint (urb
->pipe
), usb_pipeout (urb
->pipe
));
1054 case PIPE_ISOCHRONOUS
:
1055 uhci_clean_iso_step1(s
, urb_priv
);
1057 uhci_clean_iso_step2(s
, urb_priv
);
1062 qh
= list_entry (urb_priv
->desc_list
.next
, uhci_desc_t
, desc_list
);
1063 spin_lock_irqsave (&s
->urb_list_lock
, flags
);
1064 uhci_clean_transfer(s
, urb
, qh
, 1);
1065 spin_unlock_irqrestore (&s
->urb_list_lock
, flags
);
1070 kmem_cache_free (urb_priv_kmem
, urb
->hcpriv
);
1072 kfree (urb
->hcpriv
);
1074 if (urb
->complete
) {
1075 dbg("unlink_urb: calling completion");
1076 urb
->complete ((struct urb
*) urb
);
1078 usb_dec_dev_use (urb
->dev
);
1082 spin_unlock_irqrestore (&s
->urb_list_lock
, flags
);
1085 /*-------------------------------------------------------------------*/
1086 // async unlink_urb completion/cleanup work
1087 // has to be protected by urb_list_lock!
1088 // features: if set in transfer_flags, the resulting status of the killed
1089 // transaction is not overwritten
1091 _static
void uhci_cleanup_unlink(uhci_t
*s
, int force
)
1093 struct list_head
*q
;
1095 struct usb_device
*dev
;
1097 urb_priv_t
*urb_priv
;
1099 q
=s
->urb_unlinked
.next
;
1100 now
=UHCI_GET_CURRENT_FRAME(s
);
1102 while (q
!= &s
->urb_unlinked
) {
1104 urb
= list_entry (q
, urb_t
, urb_list
);
1106 urb_priv
= (urb_priv_t
*)urb
->hcpriv
;
1107 q
= urb
->urb_list
.next
;
1110 ((urb_priv
->started
!= 0xffffffff) && (urb_priv
->started
!= now
))) {
1111 async_dbg("async cleanup %p",urb
);
1112 switch (usb_pipetype (urb
->pipe
)) { // process descriptors
1114 process_transfer (s
, urb
, 2);
1117 if (!s
->avoid_bulk
.counter
)
1118 process_transfer (s
, urb
, 2); // don't unlink (already done)
1122 case PIPE_ISOCHRONOUS
:
1123 process_iso (s
, urb
, 1); // force, don't unlink
1125 case PIPE_INTERRUPT
:
1126 process_interrupt (s
, urb
);
1130 if (!(urb
->transfer_flags
& USB_TIMEOUT_KILLED
))
1131 urb
->status
= -ECONNRESET
; // mark as asynchronously killed
1133 pipe
= urb
->pipe
; // completion may destroy all...
1135 urb_priv
= urb
->hcpriv
;
1137 if (urb
->complete
) {
1138 spin_unlock(&s
->urb_list_lock
);
1139 urb
->complete ((struct urb
*) urb
);
1140 spin_lock(&s
->urb_list_lock
);
1143 if (!(urb
->transfer_flags
& USB_TIMEOUT_KILLED
))
1144 urb
->status
= -ENOENT
; // now the urb is really dead
1145 switch (usb_pipetype (pipe
)) {
1146 case PIPE_ISOCHRONOUS
:
1147 case PIPE_INTERRUPT
:
1148 uhci_clean_iso_step2(s
, urb_priv
);
1152 usb_dec_dev_use (dev
);
1154 kmem_cache_free (urb_priv_kmem
, urb_priv
);
1159 list_del (&urb
->urb_list
);
1164 /*-------------------------------------------------------------------*/
1165 _static
int uhci_unlink_urb_async (uhci_t
*s
,urb_t
*urb
)
1168 urb_priv_t
*urb_priv
;
1170 async_dbg("unlink_urb_async called %p",urb
);
1172 if ((urb
->status
== -EINPROGRESS
) ||
1173 ((usb_pipetype (urb
->pipe
) == PIPE_INTERRUPT
) && ((urb_priv_t
*)urb
->hcpriv
)->flags
))
1175 ((urb_priv_t
*)urb
->hcpriv
)->started
= ~0;
1177 dequeue_urb (s
, urb
);
1178 list_add_tail (&urb
->urb_list
, &s
->urb_unlinked
); // store urb
1179 uhci_switch_timer_int(s
);
1181 s
->unlink_urb_done
= 1;
1183 urb
->status
= -ECONNABORTED
; // mark urb as "waiting to be killed"
1184 urb_priv
= (urb_priv_t
*)urb
->hcpriv
;
1186 switch (usb_pipetype (urb
->pipe
)) {
1187 case PIPE_INTERRUPT
:
1188 usb_dotoggle (urb
->dev
, usb_pipeendpoint (urb
->pipe
), usb_pipeout (urb
->pipe
));
1190 case PIPE_ISOCHRONOUS
:
1191 uhci_clean_iso_step1 (s
, urb_priv
);
1196 qh
= list_entry (urb_priv
->desc_list
.next
, uhci_desc_t
, desc_list
);
1197 uhci_clean_transfer (s
, urb
, qh
, 0);
1200 ((urb_priv_t
*)urb
->hcpriv
)->started
= UHCI_GET_CURRENT_FRAME(s
);
1203 return -EINPROGRESS
;
1205 /*-------------------------------------------------------------------*/
1206 _static
int uhci_unlink_urb (urb_t
*urb
)
1209 unsigned long flags
=0;
1210 dbg("uhci_unlink_urb called for %p",urb
);
1211 if (!urb
|| !urb
->dev
) // you never know...
1214 s
= (uhci_t
*) urb
->dev
->bus
->hcpriv
;
1216 if (usb_pipedevice (urb
->pipe
) == s
->rh
.devnum
)
1217 return rh_unlink_urb (urb
);
1222 if (urb
->transfer_flags
& USB_ASYNC_UNLINK
) {
1225 spin_lock_irqsave (&s
->urb_list_lock
, flags
);
1226 ret
= uhci_unlink_urb_async(s
, urb
);
1227 spin_unlock_irqrestore (&s
->urb_list_lock
, flags
);
1231 return uhci_unlink_urb_sync(s
, urb
);
1233 /*-------------------------------------------------------------------*/
1234 // In case of ASAP iso transfer, search the URB-list for already queued URBs
1235 // for this EP and calculate the earliest start frame for the new
1236 // URB (easy seamless URB continuation!)
1237 _static
int find_iso_limits (urb_t
*urb
, unsigned int *start
, unsigned int *end
)
1239 urb_t
*u
, *last_urb
= NULL
;
1240 uhci_t
*s
= (uhci_t
*) urb
->dev
->bus
->hcpriv
;
1241 struct list_head
*p
;
1243 unsigned long flags
;
1245 spin_lock_irqsave (&s
->urb_list_lock
, flags
);
1248 for (; p
!= &s
->urb_list
; p
= p
->prev
) {
1249 u
= list_entry (p
, urb_t
, urb_list
);
1250 // look for pending URBs with identical pipe handle
1251 // works only because iso doesn't toggle the data bit!
1252 if ((urb
->pipe
== u
->pipe
) && (urb
->dev
== u
->dev
) && (u
->status
== -EINPROGRESS
)) {
1254 *start
= u
->start_frame
;
1260 *end
= (last_urb
->start_frame
+ last_urb
->number_of_packets
) & 1023;
1264 spin_unlock_irqrestore(&s
->urb_list_lock
, flags
);
1268 /*-------------------------------------------------------------------*/
1269 // adjust start_frame according to scheduling constraints (ASAP etc)
1271 _static
int iso_find_start (urb_t
*urb
)
1273 uhci_t
*s
= (uhci_t
*) urb
->dev
->bus
->hcpriv
;
1275 unsigned int start_limit
= 0, stop_limit
= 0, queued_size
;
1278 now
= UHCI_GET_CURRENT_FRAME (s
) & 1023;
1280 if ((unsigned) urb
->number_of_packets
> 900)
1283 limits
= find_iso_limits (urb
, &start_limit
, &stop_limit
);
1284 queued_size
= (stop_limit
- start_limit
) & 1023;
1286 if (urb
->transfer_flags
& USB_ISO_ASAP
) {
1289 // 10ms setup should be enough //FIXME!
1290 urb
->start_frame
= (now
+ 10) & 1023;
1293 urb
->start_frame
= stop_limit
; //seamless linkage
1295 if (((now
- urb
->start_frame
) & 1023) <= (unsigned) urb
->number_of_packets
) {
1296 info("iso_find_start: gap in seamless isochronous scheduling");
1297 dbg("iso_find_start: now %u start_frame %u number_of_packets %u pipe 0x%08x",
1298 now
, urb
->start_frame
, urb
->number_of_packets
, urb
->pipe
);
1299 urb
->start_frame
= (now
+ 5) & 1023; // 5ms setup should be enough //FIXME!
1304 urb
->start_frame
&= 1023;
1305 if (((now
- urb
->start_frame
) & 1023) < (unsigned) urb
->number_of_packets
) {
1306 dbg("iso_find_start: now between start_frame and end");
1311 /* check if either start_frame or start_frame+number_of_packets-1 lies between start_limit and stop_limit */
1315 if (((urb
->start_frame
- start_limit
) & 1023) < queued_size
||
1316 ((urb
->start_frame
+ urb
->number_of_packets
- 1 - start_limit
) & 1023) < queued_size
) {
1317 dbg("iso_find_start: start_frame %u number_of_packets %u start_limit %u stop_limit %u",
1318 urb
->start_frame
, urb
->number_of_packets
, start_limit
, stop_limit
);
1324 /*-------------------------------------------------------------------*/
1325 // submits USB interrupt (ie. polling ;-)
1326 // ASAP-flag set implicitely
1327 // if period==0, the the transfer is only done once
1329 _static
int uhci_submit_int_urb (urb_t
*urb
)
1331 uhci_t
*s
= (uhci_t
*) urb
->dev
->bus
->hcpriv
;
1332 urb_priv_t
*urb_priv
= urb
->hcpriv
;
1335 int status
, destination
;
1337 unsigned int pipe
= urb
->pipe
;
1339 if (urb
->interval
< 0 || urb
->interval
>= 256)
1342 if (urb
->interval
== 0)
1345 for (nint
= 0, n
= 1; nint
<= 8; nint
++, n
+= n
) // round interval down to 2^n
1347 if (urb
->interval
< n
) {
1348 urb
->interval
= n
/ 2;
1355 dbg("Rounded interval to %i, chain %i", urb
->interval
, nint
);
1357 urb
->start_frame
= UHCI_GET_CURRENT_FRAME (s
) & 1023; // remember start frame, just in case...
1359 urb
->number_of_packets
= 1;
1361 // INT allows only one packet
1362 if (urb
->transfer_buffer_length
> usb_maxpacket (urb
->dev
, pipe
, usb_pipeout (pipe
)))
1365 ret
= alloc_td (&td
, UHCI_PTR_DEPTH
);
1370 status
= (pipe
& TD_CTRL_LS
) | TD_CTRL_ACTIVE
| TD_CTRL_IOC
|
1371 (urb
->transfer_flags
& USB_DISABLE_SPD
? 0 : TD_CTRL_SPD
) | (3 << 27);
1373 destination
= (urb
->pipe
& PIPE_DEVEP_MASK
) | usb_packetid (urb
->pipe
) |
1374 (((urb
->transfer_buffer_length
- 1) & 0x7ff) << 21);
1377 info
= destination
| (usb_gettoggle (urb
->dev
, usb_pipeendpoint (pipe
), usb_pipeout (pipe
)) << TD_TOKEN_TOGGLE
);
1379 fill_td (td
, status
, info
, virt_to_bus (urb
->transfer_buffer
));
1380 list_add_tail (&td
->desc_list
, &urb_priv
->desc_list
);
1382 urb
->status
= -EINPROGRESS
;
1385 insert_td_horizontal (s
, s
->int_chain
[nint
], td
); // store in INT-TDs
1387 usb_dotoggle (urb
->dev
, usb_pipeendpoint (pipe
), usb_pipeout (pipe
));
1391 /*-------------------------------------------------------------------*/
1392 _static
int uhci_submit_iso_urb (urb_t
*urb
)
1394 uhci_t
*s
= (uhci_t
*) urb
->dev
->bus
->hcpriv
;
1395 urb_priv_t
*urb_priv
= urb
->hcpriv
;
1396 #ifdef ISO_SANITY_CHECK
1398 int maxsze
= usb_maxpacket (urb
->dev
, pipe
, usb_pipeout (pipe
));
1401 uhci_desc_t
*td
, **tdm
;
1402 int status
, destination
;
1403 unsigned long flags
;
1405 __save_flags(flags
);
1406 __cli(); // Disable IRQs to schedule all ISO-TDs in time
1407 ret
= iso_find_start (urb
); // adjusts urb->start_frame for later use
1412 tdm
= (uhci_desc_t
**) kmalloc (urb
->number_of_packets
* sizeof (uhci_desc_t
*), KMALLOC_FLAG
);
1419 // First try to get all TDs
1420 for (n
= 0; n
< urb
->number_of_packets
; n
++) {
1421 dbg("n:%d urb->iso_frame_desc[n].length:%d", n
, urb
->iso_frame_desc
[n
].length
);
1422 if (!urb
->iso_frame_desc
[n
].length
) {
1423 // allows ISO striping by setting length to zero in iso_descriptor
1428 #ifdef ISO_SANITY_CHECK
1429 if(urb
->iso_frame_desc
[n
].length
> maxsze
) {
1430 err("submit_iso: urb->iso_frame_desc[%d].length(%d)>%d",n
, urb
->iso_frame_desc
[n
].length
, maxsze
);
1437 ret
= alloc_td (&td
, UHCI_PTR_DEPTH
);
1440 int i
; // Cleanup allocated TDs
1442 for (i
= 0; i
< n
; n
++)
1444 delete_desc(tdm
[i
]);
1452 status
= TD_CTRL_ACTIVE
| TD_CTRL_IOS
; //| (urb->transfer_flags&USB_DISABLE_SPD?0:TD_CTRL_SPD);
1454 destination
= (urb
->pipe
& PIPE_DEVEP_MASK
) | usb_packetid (urb
->pipe
);
1457 // Queue all allocated TDs
1458 for (n
= 0; n
< urb
->number_of_packets
; n
++) {
1464 status
|= TD_CTRL_IOC
;
1466 fill_td (td
, status
, destination
| (((urb
->iso_frame_desc
[n
].length
- 1) & 0x7ff) << 21),
1467 virt_to_bus (urb
->transfer_buffer
+ urb
->iso_frame_desc
[n
].offset
));
1468 list_add_tail (&td
->desc_list
, &urb_priv
->desc_list
);
1471 urb
->status
= -EINPROGRESS
;
1474 insert_td_horizontal (s
, s
->iso_td
[(urb
->start_frame
+ n
) & 1023], td
); // store in iso-tds
1480 dbg("ISO-INT# %i, start %i, now %i", urb
->number_of_packets
, urb
->start_frame
, UHCI_GET_CURRENT_FRAME (s
) & 1023);
1484 __restore_flags(flags
);
1488 /*-------------------------------------------------------------------*/
1489 // returns: 0 (no transfer queued), urb* (this urb already queued)
1491 _static urb_t
* search_dev_ep (uhci_t
*s
, urb_t
*urb
)
1493 struct list_head
*p
;
1495 unsigned int mask
= usb_pipecontrol(urb
->pipe
) ? (~USB_DIR_IN
) : (~0);
1497 dbg("search_dev_ep:");
1501 for (; p
!= &s
->urb_list
; p
= p
->next
) {
1502 tmp
= list_entry (p
, urb_t
, urb_list
);
1503 dbg("urb: %p", tmp
);
1504 // we can accept this urb if it is not queued at this time
1505 // or if non-iso transfer requests should be scheduled for the same device and pipe
1506 if ((!usb_pipeisoc(urb
->pipe
) && (tmp
->dev
== urb
->dev
) && !((tmp
->pipe
^ urb
->pipe
) & mask
)) ||
1508 return tmp
; // found another urb already queued for processing
1514 /*-------------------------------------------------------------------*/
1515 _static
int uhci_submit_urb (urb_t
*urb
)
1518 urb_priv_t
*urb_priv
;
1520 unsigned long flags
;
1521 urb_t
*bulk_urb
=NULL
;
1523 if (!urb
->dev
|| !urb
->dev
->bus
)
1526 s
= (uhci_t
*) urb
->dev
->bus
->hcpriv
;
1527 //dbg("submit_urb: %p type %d",urb,usb_pipetype(urb->pipe));
1532 if (usb_pipedevice (urb
->pipe
) == s
->rh
.devnum
)
1533 return rh_submit_urb (urb
); /* virtual root hub */
1535 usb_inc_dev_use (urb
->dev
);
1537 spin_lock_irqsave (&s
->urb_list_lock
, flags
);
1539 bulk_urb
= search_dev_ep (s
, urb
);
1543 queue_dbg("found bulk urb %p\n",bulk_urb
);
1545 if ((usb_pipetype (urb
->pipe
) != PIPE_BULK
) ||
1546 ((usb_pipetype (urb
->pipe
) == PIPE_BULK
) &&
1547 (!(urb
->transfer_flags
& USB_QUEUE_BULK
) || !(bulk_urb
->transfer_flags
& USB_QUEUE_BULK
)))) {
1548 spin_unlock_irqrestore (&s
->urb_list_lock
, flags
);
1549 usb_dec_dev_use (urb
->dev
);
1550 err("ENXIO %08x, flags %x, urb %p, burb %p",urb
->pipe
,urb
->transfer_flags
,urb
,bulk_urb
);
1551 return -ENXIO
; // urb already queued
1556 urb_priv
= kmem_cache_alloc(urb_priv_kmem
, SLAB_FLAG
);
1558 urb_priv
= kmalloc (sizeof (urb_priv_t
), KMALLOC_FLAG
);
1561 usb_dec_dev_use (urb
->dev
);
1562 spin_unlock_irqrestore (&s
->urb_list_lock
, flags
);
1566 urb
->hcpriv
= urb_priv
;
1567 INIT_LIST_HEAD (&urb_priv
->desc_list
);
1568 urb_priv
->flags
= 0;
1569 dbg("submit_urb: scheduling %p", urb
);
1570 urb_priv
->next_queued_urb
= NULL
;
1571 urb_priv
->prev_queued_urb
= NULL
;
1572 urb_priv
->bottom_qh
= NULL
;
1573 urb_priv
->next_qh
= NULL
;
1575 if (usb_pipetype (urb
->pipe
) == PIPE_BULK
) {
1578 while (((urb_priv_t
*)bulk_urb
->hcpriv
)->next_queued_urb
) // find last queued bulk
1579 bulk_urb
=((urb_priv_t
*)bulk_urb
->hcpriv
)->next_queued_urb
;
1581 ((urb_priv_t
*)bulk_urb
->hcpriv
)->next_queued_urb
=urb
;
1583 atomic_inc (&s
->avoid_bulk
);
1584 ret
= uhci_submit_bulk_urb (urb
, bulk_urb
);
1585 atomic_dec (&s
->avoid_bulk
);
1586 spin_unlock_irqrestore (&s
->urb_list_lock
, flags
);
1589 spin_unlock_irqrestore (&s
->urb_list_lock
, flags
);
1590 switch (usb_pipetype (urb
->pipe
)) {
1591 case PIPE_ISOCHRONOUS
:
1592 ret
= uhci_submit_iso_urb (urb
);
1594 case PIPE_INTERRUPT
:
1595 ret
= uhci_submit_int_urb (urb
);
1598 ret
= uhci_submit_control_urb (urb
);
1605 dbg("submit_urb: scheduled with ret: %d", ret
);
1608 usb_dec_dev_use (urb
->dev
);
1610 kmem_cache_free(urb_priv_kmem
, urb_priv
);
1620 // Checks for URB timeout and removes bandwidth reclamation
1621 // if URB idles too long
1622 _static
void uhci_check_timeouts(uhci_t
*s
)
1624 struct list_head
*p
,*p2
;
1628 p
= s
->urb_list
.prev
;
1630 while (p
!= &s
->urb_list
) {
1635 urb
= list_entry (p2
, urb_t
, urb_list
);
1636 type
= usb_pipetype (urb
->pipe
);
1638 hcpriv
= (urb_priv_t
*)urb
->hcpriv
;
1640 if ( urb
->timeout
&&
1641 ((hcpriv
->started
+ urb
->timeout
) < jiffies
)) {
1642 urb
->transfer_flags
|= USB_TIMEOUT_KILLED
| USB_ASYNC_UNLINK
;
1643 async_dbg("uhci_check_timeout: timeout for %p",urb
);
1644 uhci_unlink_urb_async(s
, urb
);
1646 #ifdef CONFIG_USB_UHCI_HIGH_BANDWIDTH
1647 else if (((type
== PIPE_BULK
) || (type
== PIPE_CONTROL
)) &&
1648 (hcpriv
->use_loop
) &&
1649 ((hcpriv
->started
+ IDLE_TIMEOUT
) < jiffies
))
1650 disable_desc_loop(s
, urb
);
1654 s
->timeout_check
=jiffies
;
1657 /*-------------------------------------------------------------------
1659 -------------------------------------------------------------------*/
1661 _static __u8 root_hub_dev_des
[] =
1663 0x12, /* __u8 bLength; */
1664 0x01, /* __u8 bDescriptorType; Device */
1665 0x00, /* __u16 bcdUSB; v1.0 */
1667 0x09, /* __u8 bDeviceClass; HUB_CLASSCODE */
1668 0x00, /* __u8 bDeviceSubClass; */
1669 0x00, /* __u8 bDeviceProtocol; */
1670 0x08, /* __u8 bMaxPacketSize0; 8 Bytes */
1671 0x00, /* __u16 idVendor; */
1673 0x00, /* __u16 idProduct; */
1675 0x00, /* __u16 bcdDevice; */
1677 0x00, /* __u8 iManufacturer; */
1678 0x02, /* __u8 iProduct; */
1679 0x01, /* __u8 iSerialNumber; */
1680 0x01 /* __u8 bNumConfigurations; */
1684 /* Configuration descriptor */
1685 _static __u8 root_hub_config_des
[] =
1687 0x09, /* __u8 bLength; */
1688 0x02, /* __u8 bDescriptorType; Configuration */
1689 0x19, /* __u16 wTotalLength; */
1691 0x01, /* __u8 bNumInterfaces; */
1692 0x01, /* __u8 bConfigurationValue; */
1693 0x00, /* __u8 iConfiguration; */
1694 0x40, /* __u8 bmAttributes;
1695 Bit 7: Bus-powered, 6: Self-powered, 5 Remote-wakwup, 4..0: resvd */
1696 0x00, /* __u8 MaxPower; */
1699 0x09, /* __u8 if_bLength; */
1700 0x04, /* __u8 if_bDescriptorType; Interface */
1701 0x00, /* __u8 if_bInterfaceNumber; */
1702 0x00, /* __u8 if_bAlternateSetting; */
1703 0x01, /* __u8 if_bNumEndpoints; */
1704 0x09, /* __u8 if_bInterfaceClass; HUB_CLASSCODE */
1705 0x00, /* __u8 if_bInterfaceSubClass; */
1706 0x00, /* __u8 if_bInterfaceProtocol; */
1707 0x00, /* __u8 if_iInterface; */
1710 0x07, /* __u8 ep_bLength; */
1711 0x05, /* __u8 ep_bDescriptorType; Endpoint */
1712 0x81, /* __u8 ep_bEndpointAddress; IN Endpoint 1 */
1713 0x03, /* __u8 ep_bmAttributes; Interrupt */
1714 0x08, /* __u16 ep_wMaxPacketSize; 8 Bytes */
1716 0xff /* __u8 ep_bInterval; 255 ms */
1720 _static __u8 root_hub_hub_des
[] =
1722 0x09, /* __u8 bLength; */
1723 0x29, /* __u8 bDescriptorType; Hub-descriptor */
1724 0x02, /* __u8 bNbrPorts; */
1725 0x00, /* __u16 wHubCharacteristics; */
1727 0x01, /* __u8 bPwrOn2pwrGood; 2ms */
1728 0x00, /* __u8 bHubContrCurrent; 0 mA */
1729 0x00, /* __u8 DeviceRemovable; *** 7 Ports max *** */
1730 0xff /* __u8 PortPwrCtrlMask; *** 7 ports max *** */
1733 /*-------------------------------------------------------------------------*/
1734 /* prepare Interrupt pipe transaction data; HUB INTERRUPT ENDPOINT */
1735 _static
int rh_send_irq (urb_t
*urb
)
1739 uhci_t
*uhci
= urb
->dev
->bus
->hcpriv
;
1740 unsigned int io_addr
= uhci
->io_addr
;
1743 for (i
= 0; i
< uhci
->rh
.numports
; i
++) {
1744 data
|= ((inw (io_addr
+ USBPORTSC1
+ i
* 2) & 0xa) > 0 ? (1 << (i
+ 1)) : 0);
1745 len
= (i
+ 1) / 8 + 1;
1748 *(__u16
*) urb
->transfer_buffer
= cpu_to_le16 (data
);
1749 urb
->actual_length
= len
;
1752 if ((data
> 0) && (uhci
->rh
.send
!= 0)) {
1753 dbg("Root-Hub INT complete: port1: %x port2: %x data: %x",
1754 inw (io_addr
+ USBPORTSC1
), inw (io_addr
+ USBPORTSC2
), data
);
1755 urb
->complete (urb
);
1760 /*-------------------------------------------------------------------------*/
1761 /* Virtual Root Hub INTs are polled by this timer every "intervall" ms */
1762 _static
int rh_init_int_timer (urb_t
*urb
);
1764 _static
void rh_int_timer_do (unsigned long ptr
)
1767 urb_t
*urb
= (urb_t
*) ptr
;
1768 uhci_t
*uhci
= urb
->dev
->bus
->hcpriv
;
1770 if (uhci
->rh
.send
) {
1771 len
= rh_send_irq (urb
);
1773 urb
->actual_length
= len
;
1775 urb
->complete (urb
);
1778 rh_init_int_timer (urb
);
1781 /*-------------------------------------------------------------------------*/
1782 /* Root Hub INTs are polled by this timer, polling interval 20ms */
1783 /* This time is also used for URB-timeout checking */
1785 _static
int rh_init_int_timer (urb_t
*urb
)
1787 uhci_t
*uhci
= urb
->dev
->bus
->hcpriv
;
1789 uhci
->rh
.interval
= urb
->interval
;
1790 init_timer (&uhci
->rh
.rh_int_timer
);
1791 uhci
->rh
.rh_int_timer
.function
= rh_int_timer_do
;
1792 uhci
->rh
.rh_int_timer
.data
= (unsigned long) urb
;
1793 uhci
->rh
.rh_int_timer
.expires
= jiffies
+ (HZ
* 20) / 1000;
1794 add_timer (&uhci
->rh
.rh_int_timer
);
1799 /*-------------------------------------------------------------------------*/
1800 #define OK(x) len = (x); break
1802 #define CLR_RH_PORTSTAT(x) \
1803 status = inw(io_addr+USBPORTSC1+2*(wIndex-1)); \
1804 status = (status & 0xfff5) & ~(x); \
1805 outw(status, io_addr+USBPORTSC1+2*(wIndex-1))
1807 #define SET_RH_PORTSTAT(x) \
1808 status = inw(io_addr+USBPORTSC1+2*(wIndex-1)); \
1809 status = (status & 0xfff5) | (x); \
1810 outw(status, io_addr+USBPORTSC1+2*(wIndex-1))
1813 /*-------------------------------------------------------------------------*/
1815 ** Root Hub Control Pipe
1816 *************************/
1819 _static
int rh_submit_urb (urb_t
*urb
)
1821 struct usb_device
*usb_dev
= urb
->dev
;
1822 uhci_t
*uhci
= usb_dev
->bus
->hcpriv
;
1823 unsigned int pipe
= urb
->pipe
;
1824 devrequest
*cmd
= (devrequest
*) urb
->setup_packet
;
1825 void *data
= urb
->transfer_buffer
;
1826 int leni
= urb
->transfer_buffer_length
;
1831 unsigned int io_addr
= uhci
->io_addr
;
1839 if (usb_pipetype (pipe
) == PIPE_INTERRUPT
) {
1840 dbg("Root-Hub submit IRQ: every %d ms", urb
->interval
);
1843 uhci
->rh
.interval
= urb
->interval
;
1844 rh_init_int_timer (urb
);
1850 bmRType_bReq
= cmd
->requesttype
| cmd
->request
<< 8;
1851 wValue
= le16_to_cpu (cmd
->value
);
1852 wIndex
= le16_to_cpu (cmd
->index
);
1853 wLength
= le16_to_cpu (cmd
->length
);
1855 for (i
= 0; i
< 8; i
++)
1856 uhci
->rh
.c_p_r
[i
] = 0;
1858 dbg("Root-Hub: adr: %2x cmd(%1x): %04x %04x %04x %04x",
1859 uhci
->rh
.devnum
, 8, bmRType_bReq
, wValue
, wIndex
, wLength
);
1861 switch (bmRType_bReq
) {
1862 /* Request Destination:
1863 without flags: Device,
1864 RH_INTERFACE: interface,
1865 RH_ENDPOINT: endpoint,
1866 RH_CLASS means HUB here,
1867 RH_OTHER | RH_CLASS almost ever means HUB_PORT here
1871 *(__u16
*) data
= cpu_to_le16 (1);
1873 case RH_GET_STATUS
| RH_INTERFACE
:
1874 *(__u16
*) data
= cpu_to_le16 (0);
1876 case RH_GET_STATUS
| RH_ENDPOINT
:
1877 *(__u16
*) data
= cpu_to_le16 (0);
1879 case RH_GET_STATUS
| RH_CLASS
:
1880 *(__u32
*) data
= cpu_to_le32 (0);
1881 OK (4); /* hub power ** */
1882 case RH_GET_STATUS
| RH_OTHER
| RH_CLASS
:
1883 status
= inw (io_addr
+ USBPORTSC1
+ 2 * (wIndex
- 1));
1884 cstatus
= ((status
& USBPORTSC_CSC
) >> (1 - 0)) |
1885 ((status
& USBPORTSC_PEC
) >> (3 - 1)) |
1886 (uhci
->rh
.c_p_r
[wIndex
- 1] << (0 + 4));
1887 status
= (status
& USBPORTSC_CCS
) |
1888 ((status
& USBPORTSC_PE
) >> (2 - 1)) |
1889 ((status
& USBPORTSC_SUSP
) >> (12 - 2)) |
1890 ((status
& USBPORTSC_PR
) >> (9 - 4)) |
1891 (1 << 8) | /* power on ** */
1892 ((status
& USBPORTSC_LSDA
) << (-8 + 9));
1894 *(__u16
*) data
= cpu_to_le16 (status
);
1895 *(__u16
*) (data
+ 2) = cpu_to_le16 (cstatus
);
1898 case RH_CLEAR_FEATURE
| RH_ENDPOINT
:
1900 case (RH_ENDPOINT_STALL
):
1905 case RH_CLEAR_FEATURE
| RH_CLASS
:
1907 case (RH_C_HUB_OVER_CURRENT
):
1908 OK (0); /* hub power over current ** */
1912 case RH_CLEAR_FEATURE
| RH_OTHER
| RH_CLASS
:
1914 case (RH_PORT_ENABLE
):
1915 CLR_RH_PORTSTAT (USBPORTSC_PE
);
1917 case (RH_PORT_SUSPEND
):
1918 CLR_RH_PORTSTAT (USBPORTSC_SUSP
);
1920 case (RH_PORT_POWER
):
1921 OK (0); /* port power ** */
1922 case (RH_C_PORT_CONNECTION
):
1923 SET_RH_PORTSTAT (USBPORTSC_CSC
);
1925 case (RH_C_PORT_ENABLE
):
1926 SET_RH_PORTSTAT (USBPORTSC_PEC
);
1928 case (RH_C_PORT_SUSPEND
):
1929 /*** WR_RH_PORTSTAT(RH_PS_PSSC); */
1931 case (RH_C_PORT_OVER_CURRENT
):
1932 OK (0); /* port power over current ** */
1933 case (RH_C_PORT_RESET
):
1934 uhci
->rh
.c_p_r
[wIndex
- 1] = 0;
1939 case RH_SET_FEATURE
| RH_OTHER
| RH_CLASS
:
1941 case (RH_PORT_SUSPEND
):
1942 SET_RH_PORTSTAT (USBPORTSC_SUSP
);
1944 case (RH_PORT_RESET
):
1945 SET_RH_PORTSTAT (USBPORTSC_PR
);
1947 uhci
->rh
.c_p_r
[wIndex
- 1] = 1;
1948 CLR_RH_PORTSTAT (USBPORTSC_PR
);
1950 SET_RH_PORTSTAT (USBPORTSC_PE
);
1952 SET_RH_PORTSTAT (0xa);
1954 case (RH_PORT_POWER
):
1955 OK (0); /* port power ** */
1956 case (RH_PORT_ENABLE
):
1957 SET_RH_PORTSTAT (USBPORTSC_PE
);
1962 case RH_SET_ADDRESS
:
1963 uhci
->rh
.devnum
= wValue
;
1966 case RH_GET_DESCRIPTOR
:
1967 switch ((wValue
& 0xff00) >> 8) {
1968 case (0x01): /* device descriptor */
1969 len
= min (leni
, min (sizeof (root_hub_dev_des
), wLength
));
1970 memcpy (data
, root_hub_dev_des
, len
);
1972 case (0x02): /* configuration descriptor */
1973 len
= min (leni
, min (sizeof (root_hub_config_des
), wLength
));
1974 memcpy (data
, root_hub_config_des
, len
);
1976 case (0x03): /* string descriptors */
1977 len
= usb_root_hub_string (wValue
& 0xff,
1978 uhci
->io_addr
, "UHCI",
1981 OK (min (leni
, len
));
1987 case RH_GET_DESCRIPTOR
| RH_CLASS
:
1988 root_hub_hub_des
[2] = uhci
->rh
.numports
;
1989 len
= min (leni
, min (sizeof (root_hub_hub_des
), wLength
));
1990 memcpy (data
, root_hub_hub_des
, len
);
1993 case RH_GET_CONFIGURATION
:
1994 *(__u8
*) data
= 0x01;
1997 case RH_SET_CONFIGURATION
:
2003 dbg("Root-Hub stat port1: %x port2: %x",
2004 inw (io_addr
+ USBPORTSC1
), inw (io_addr
+ USBPORTSC2
));
2006 urb
->actual_length
= len
;
2009 urb
->complete (urb
);
2012 /*-------------------------------------------------------------------------*/
2014 _static
int rh_unlink_urb (urb_t
*urb
)
2016 uhci_t
*uhci
= urb
->dev
->bus
->hcpriv
;
2018 if (uhci
->rh
.urb
==urb
) {
2019 dbg("Root-Hub unlink IRQ");
2021 del_timer (&uhci
->rh
.rh_int_timer
);
2025 /*-------------------------------------------------------------------*/
2028 * Map status to standard result codes
2030 * <status> is (td->status & 0xFE0000) [a.k.a. uhci_status_bits(td->status)
2031 * <dir_out> is True for output TDs and False for input TDs.
2033 _static
int uhci_map_status (int status
, int dir_out
)
2037 if (status
& TD_CTRL_BITSTUFF
) /* Bitstuff error */
2039 if (status
& TD_CTRL_CRCTIMEO
) { /* CRC/Timeout */
2045 if (status
& TD_CTRL_NAK
) /* NAK */
2047 if (status
& TD_CTRL_BABBLE
) /* Babble */
2049 if (status
& TD_CTRL_DBUFERR
) /* Buffer error */
2051 if (status
& TD_CTRL_STALLED
) /* Stalled */
2053 if (status
& TD_CTRL_ACTIVE
) /* Active */
2060 * Only the USB core should call uhci_alloc_dev and uhci_free_dev
2062 _static
int uhci_alloc_dev (struct usb_device
*usb_dev
)
2067 _static
void uhci_unlink_urbs(uhci_t
*s
, struct usb_device
*usb_dev
, int remove_all
)
2069 unsigned long flags
;
2070 struct list_head
*p
;
2071 struct list_head
*p2
;
2074 spin_lock_irqsave (&s
->urb_list_lock
, flags
);
2075 p
= s
->urb_list
.prev
;
2076 while (p
!= &s
->urb_list
) {
2079 urb
= list_entry (p2
, urb_t
, urb_list
);
2080 dbg("urb: %p, dev %p, %p", urb
, usb_dev
,urb
->dev
);
2082 //urb->transfer_flags |=USB_ASYNC_UNLINK;
2084 if (remove_all
|| (usb_dev
== urb
->dev
)) {
2085 spin_unlock_irqrestore (&s
->urb_list_lock
, flags
);
2086 warn("forced removing of queued URB %p due to disconnect",urb
);
2087 uhci_unlink_urb(urb
);
2088 urb
->dev
= NULL
; // avoid further processing of this UR
2089 spin_lock_irqsave (&s
->urb_list_lock
, flags
);
2090 p
= s
->urb_list
.prev
;
2093 spin_unlock_irqrestore (&s
->urb_list_lock
, flags
);
2096 _static
int uhci_free_dev (struct usb_device
*usb_dev
)
2101 if(!usb_dev
|| !usb_dev
->bus
|| !usb_dev
->bus
->hcpriv
)
2104 s
=(uhci_t
*) usb_dev
->bus
->hcpriv
;
2105 uhci_unlink_urbs(s
, usb_dev
, 0);
2111 * uhci_get_current_frame_number()
2113 * returns the current frame number for a USB bus/controller.
2115 _static
int uhci_get_current_frame_number (struct usb_device
*usb_dev
)
2117 return UHCI_GET_CURRENT_FRAME ((uhci_t
*) usb_dev
->bus
->hcpriv
);
2120 struct usb_operations uhci_device_operations
=
2124 uhci_get_current_frame_number
,
2130 * For IN-control transfers, process_transfer gets a bit more complicated,
2131 * since there are devices that return less data (eg. strings) than they
2132 * have announced. This leads to a queue abort due to the short packet,
2133 * the status stage is not executed. If this happens, the status stage
2134 * is manually re-executed.
2135 * mode: 0: QHs already unlinked
2138 _static
int process_transfer (uhci_t
*s
, urb_t
*urb
, int mode
)
2141 urb_priv_t
*urb_priv
= urb
->hcpriv
;
2142 struct list_head
*qhl
= urb_priv
->desc_list
.next
;
2143 uhci_desc_t
*qh
= list_entry (qhl
, uhci_desc_t
, desc_list
);
2144 struct list_head
*p
= qh
->vertical
.next
;
2145 uhci_desc_t
*desc
= list_entry (urb_priv
->desc_list
.prev
, uhci_desc_t
, desc_list
);
2146 uhci_desc_t
*last_desc
= list_entry (desc
->vertical
.prev
, uhci_desc_t
, vertical
);
2147 int data_toggle
= usb_gettoggle (urb
->dev
, usb_pipeendpoint (urb
->pipe
), usb_pipeout (urb
->pipe
)); // save initial data_toggle
2148 int maxlength
; // extracted and remapped info from TD
2152 //dbg("process_transfer: urb contains bulk/control request");
2154 /* if the status phase has been retriggered and the
2155 queue is empty or the last status-TD is inactive, the retriggered
2156 status stage is completed
2159 if (urb_priv
->flags
&&
2160 ((qh
->hw
.qh
.element
== UHCI_PTR_TERM
) ||(!(last_desc
->hw
.td
.status
& TD_CTRL_ACTIVE
))))
2161 goto transfer_finished
;
2163 urb
->actual_length
=0;
2165 for (; p
!= &qh
->vertical
; p
= p
->next
) {
2166 desc
= list_entry (p
, uhci_desc_t
, vertical
);
2168 if (desc
->hw
.td
.status
& TD_CTRL_ACTIVE
) // do not process active TDs
2171 actual_length
= (desc
->hw
.td
.status
+ 1) & 0x7ff; // extract transfer parameters from TD
2172 maxlength
= (((desc
->hw
.td
.info
>> 21) & 0x7ff) + 1) & 0x7ff;
2173 status
= uhci_map_status (uhci_status_bits (desc
->hw
.td
.status
), usb_pipeout (urb
->pipe
));
2175 if (status
== -EPIPE
) { // see if EP is stalled
2176 // set up stalled condition
2177 usb_endpoint_halt (urb
->dev
, usb_pipeendpoint (urb
->pipe
), usb_pipeout (urb
->pipe
));
2180 if (status
!= 0) { // if any error occured stop processing of further TDs
2181 // only set ret if status returned an error
2182 if (status
!= -EPIPE
)
2183 uhci_show_td (desc
);
2188 else if ((desc
->hw
.td
.info
& 0xff) != USB_PID_SETUP
)
2189 urb
->actual_length
+= actual_length
;
2191 // got less data than requested
2192 if ( (actual_length
< maxlength
)) {
2193 if (urb
->transfer_flags
& USB_DISABLE_SPD
) {
2194 status
= -EREMOTEIO
; // treat as real error
2195 dbg("process_transfer: SPD!!");
2196 break; // exit after this TD because SP was detected
2199 // short read during control-IN: re-start status stage
2200 if ((usb_pipetype (urb
->pipe
) == PIPE_CONTROL
)) {
2201 if (uhci_packetid(last_desc
->hw
.td
.info
) == USB_PID_OUT
) {
2203 qh
->hw
.qh
.element
= virt_to_bus (last_desc
); // re-trigger status stage
2204 dbg("short packet during control transfer, retrigger status stage @ %p",last_desc
);
2205 //uhci_show_td (desc);
2206 //uhci_show_td (last_desc);
2207 urb_priv
->flags
= 1; // mark as short control packet
2211 // all other cases: short read is OK
2212 data_toggle
= uhci_toggle (desc
->hw
.td
.info
);
2216 data_toggle
= uhci_toggle (desc
->hw
.td
.info
);
2217 queue_dbg("process_transfer: len:%d status:%x mapped:%x toggle:%d", actual_length
, desc
->hw
.td
.status
,status
, data_toggle
);
2221 usb_settoggle (urb
->dev
, usb_pipeendpoint (urb
->pipe
), usb_pipeout (urb
->pipe
), !data_toggle
);
2225 uhci_clean_transfer(s
, urb
, qh
, (mode
==0?2:1));
2227 urb
->status
= status
;
2229 #ifdef CONFIG_USB_UHCI_HIGH_BANDWIDTH
2230 disable_desc_loop(s
,urb
);
2233 queue_dbg("process_transfer: (end) urb %p, wanted len %d, len %d status %x err %d",
2234 urb
,urb
->transfer_buffer_length
,urb
->actual_length
, urb
->status
, urb
->error_count
);
2238 _static
int process_interrupt (uhci_t
*s
, urb_t
*urb
)
2240 int i
, ret
= -EINPROGRESS
;
2241 urb_priv_t
*urb_priv
= urb
->hcpriv
;
2242 struct list_head
*p
= urb_priv
->desc_list
.next
;
2243 uhci_desc_t
*desc
= list_entry (urb_priv
->desc_list
.prev
, uhci_desc_t
, desc_list
);
2248 //dbg("urb contains interrupt request");
2250 for (i
= 0; p
!= &urb_priv
->desc_list
; p
= p
->next
, i
++) // Maybe we allow more than one TD later ;-)
2252 desc
= list_entry (p
, uhci_desc_t
, desc_list
);
2254 if (desc
->hw
.td
.status
& TD_CTRL_ACTIVE
) {
2255 // do not process active TDs
2256 //dbg("TD ACT Status @%p %08x",desc,desc->hw.td.status);
2260 if (!desc
->hw
.td
.status
& TD_CTRL_IOC
) {
2261 // do not process one-shot TDs, no recycling
2264 // extract transfer parameters from TD
2266 actual_length
= (desc
->hw
.td
.status
+ 1) & 0x7ff;
2267 status
= uhci_map_status (uhci_status_bits (desc
->hw
.td
.status
), usb_pipeout (urb
->pipe
));
2269 // see if EP is stalled
2270 if (status
== -EPIPE
) {
2271 // set up stalled condition
2272 usb_endpoint_halt (urb
->dev
, usb_pipeendpoint (urb
->pipe
), usb_pipeout (urb
->pipe
));
2275 // if any error occured: ignore this td, and continue
2277 //uhci_show_td (desc);
2282 urb
->actual_length
= actual_length
;
2285 if (urb
->complete
) {
2286 //dbg("process_interrupt: calling completion, status %i",status);
2287 urb
->status
= status
;
2288 ((urb_priv_t
*)urb
->hcpriv
)->flags
=1; // if unlink_urb is called during completion
2290 spin_unlock(&s
->urb_list_lock
);
2292 urb
->complete ((struct urb
*) urb
);
2294 spin_lock(&s
->urb_list_lock
);
2296 ((urb_priv_t
*)urb
->hcpriv
)->flags
=0;
2299 if ((urb
->status
!= -ECONNABORTED
) && (urb
->status
!= ECONNRESET
) &&
2300 (urb
->status
!= -ENOENT
)) {
2302 urb
->status
= -EINPROGRESS
;
2304 // Recycle INT-TD if interval!=0, else mark TD as one-shot
2305 if (urb
->interval
) {
2307 desc
->hw
.td
.info
&= ~(1 << TD_TOKEN_TOGGLE
);
2309 ((urb_priv_t
*)urb
->hcpriv
)->started
=jiffies
;
2310 desc
->hw
.td
.info
|= (usb_gettoggle (urb
->dev
, usb_pipeendpoint (urb
->pipe
),
2311 usb_pipeout (urb
->pipe
)) << TD_TOKEN_TOGGLE
);
2312 usb_dotoggle (urb
->dev
, usb_pipeendpoint (urb
->pipe
), usb_pipeout (urb
->pipe
));
2314 desc
->hw
.td
.info
|= (!usb_gettoggle (urb
->dev
, usb_pipeendpoint (urb
->pipe
),
2315 usb_pipeout (urb
->pipe
)) << TD_TOKEN_TOGGLE
);
2317 desc
->hw
.td
.status
= (urb
->pipe
& TD_CTRL_LS
) | TD_CTRL_ACTIVE
| TD_CTRL_IOC
|
2318 (urb
->transfer_flags
& USB_DISABLE_SPD
? 0 : TD_CTRL_SPD
) | (3 << 27);
2322 uhci_unlink_urb_async(s
, urb
);
2323 desc
->hw
.td
.status
&= ~TD_CTRL_IOC
; // inactivate TD
2331 // mode: 1: force processing, don't unlink tds (already unlinked)
2332 _static
int process_iso (uhci_t
*s
, urb_t
*urb
, int mode
)
2336 urb_priv_t
*urb_priv
= urb
->hcpriv
;
2337 struct list_head
*p
= urb_priv
->desc_list
.next
;
2338 uhci_desc_t
*desc
= list_entry (urb_priv
->desc_list
.prev
, uhci_desc_t
, desc_list
);
2340 dbg("urb contains iso request");
2341 if ((desc
->hw
.td
.status
& TD_CTRL_ACTIVE
) && !mode
)
2342 return -EXDEV
; // last TD not finished
2344 urb
->error_count
= 0;
2345 urb
->actual_length
= 0;
2347 dbg("process iso urb %p, %li, %i, %i, %i %08x",urb
,jiffies
,UHCI_GET_CURRENT_FRAME(s
),
2348 urb
->number_of_packets
,mode
,desc
->hw
.td
.status
);
2350 for (i
= 0; p
!= &urb_priv
->desc_list
; i
++) {
2351 desc
= list_entry (p
, uhci_desc_t
, desc_list
);
2353 //uhci_show_td(desc);
2354 if (desc
->hw
.td
.status
& TD_CTRL_ACTIVE
) {
2355 // means we have completed the last TD, but not the TDs before
2356 desc
->hw
.td
.status
&= ~TD_CTRL_ACTIVE
;
2357 dbg("TD still active (%x)- grrr. paranoia!", desc
->hw
.td
.status
);
2359 urb
->iso_frame_desc
[i
].status
= ret
;
2360 unlink_td (s
, desc
, 1);
2361 // FIXME: immediate deletion may be dangerous
2366 unlink_td (s
, desc
, 1);
2368 if (urb
->number_of_packets
<= i
) {
2369 dbg("urb->number_of_packets (%d)<=(%d)", urb
->number_of_packets
, i
);
2374 if (urb
->iso_frame_desc
[i
].offset
+ urb
->transfer_buffer
!= bus_to_virt (desc
->hw
.td
.buffer
)) {
2375 // Hm, something really weird is going on
2376 dbg("Pointer Paranoia: %p!=%p", urb
->iso_frame_desc
[i
].offset
+ urb
->transfer_buffer
, bus_to_virt (desc
->hw
.td
.buffer
));
2378 urb
->iso_frame_desc
[i
].status
= ret
;
2381 urb
->iso_frame_desc
[i
].actual_length
= (desc
->hw
.td
.status
+ 1) & 0x7ff;
2382 urb
->iso_frame_desc
[i
].status
= uhci_map_status (uhci_status_bits (desc
->hw
.td
.status
), usb_pipeout (urb
->pipe
));
2383 urb
->actual_length
+= urb
->iso_frame_desc
[i
].actual_length
;
2387 if (urb
->iso_frame_desc
[i
].status
!= 0) {
2389 urb
->status
= urb
->iso_frame_desc
[i
].status
;
2391 dbg("process_iso: %i: len:%d %08x status:%x",
2392 i
, urb
->iso_frame_desc
[i
].actual_length
, desc
->hw
.td
.status
,urb
->iso_frame_desc
[i
].status
);
2399 dbg("process_iso: exit %i (%d), actual_len %i", i
, ret
,urb
->actual_length
);
2404 _static
int process_urb (uhci_t
*s
, struct list_head
*p
)
2410 urb
=list_entry (p
, urb_t
, urb_list
);
2411 //dbg("process_urb: found queued urb: %p", urb);
2413 switch (usb_pipetype (urb
->pipe
)) {
2415 ret
= process_transfer (s
, urb
, 1);
2418 if (!s
->avoid_bulk
.counter
)
2419 ret
= process_transfer (s
, urb
, 1);
2423 case PIPE_ISOCHRONOUS
:
2424 ret
= process_iso (s
, urb
, 0);
2426 case PIPE_INTERRUPT
:
2427 ret
= process_interrupt (s
, urb
);
2431 if (urb
->status
!= -EINPROGRESS
) {
2434 dbg("dequeued urb: %p", urb
);
2435 dequeue_urb (s
, urb
);
2438 kmem_cache_free(urb_priv_kmem
, urb
->hcpriv
);
2440 kfree (urb
->hcpriv
);
2443 if ((usb_pipetype (urb
->pipe
) != PIPE_INTERRUPT
)) {
2444 urb_t
*tmp
= urb
->next
; // pointer to first urb
2449 if (tmp
->status
!= -EINPROGRESS
) {
2455 while (tmp
!= NULL
&& tmp
!= urb
->next
);
2456 if (tmp
== urb
->next
)
2460 spin_unlock(&s
->urb_list_lock
);
2462 // In case you need the current URB status for your completion handler
2463 if (urb
->complete
&& (!proceed
|| (urb
->transfer_flags
& USB_URB_EARLY_COMPLETE
))) {
2464 dbg("process_transfer: calling early completion");
2465 urb
->complete ((struct urb
*) urb
);
2466 if (!proceed
&& is_ring
&& (urb
->status
!= -ENOENT
))
2467 uhci_submit_urb (urb
);
2470 if (proceed
&& urb
->next
) {
2471 // if there are linked urbs - handle submitting of them right now.
2472 tmp
= urb
->next
; // pointer to first urb
2475 if ((tmp
->status
!= -EINPROGRESS
) && (tmp
->status
!= -ENOENT
) && uhci_submit_urb (tmp
) != 0)
2479 while (tmp
!= NULL
&& tmp
!= urb
->next
); // submit until we reach NULL or our own pointer or submit fails
2481 if (urb
->complete
&& !(urb
->transfer_flags
& USB_URB_EARLY_COMPLETE
)) {
2482 dbg("process_transfer: calling completion");
2483 urb
->complete ((struct urb
*) urb
);
2487 spin_lock(&s
->urb_list_lock
);
2489 usb_dec_dev_use (urb
->dev
);
2496 _static
void uhci_interrupt (int irq
, void *__uhci
, struct pt_regs
*regs
)
2499 unsigned int io_addr
= s
->io_addr
;
2500 unsigned short status
;
2501 struct list_head
*p
, *p2
;
2504 * Read the interrupt status, and write it back to clear the
2508 status
= inw (io_addr
+ USBSTS
);
2510 if (!status
) /* shared interrupt, not mine */
2516 warn("interrupt, status %x, frame# %i", status
,
2517 UHCI_GET_CURRENT_FRAME(s
));
2519 // remove host controller halted state
2520 if ((status
&0x20) && (s
->running
)) {
2521 outw (USBCMD_RS
| inw(io_addr
+ USBCMD
), io_addr
+ USBCMD
);
2523 //uhci_show_status (s);
2526 * traverse the list in *reverse* direction, because new entries
2527 * may be added at the end.
2528 * also, because process_urb may unlink the current urb,
2529 * we need to advance the list before
2532 spin_lock (&s
->urb_list_lock
);
2534 s
->unlink_urb_done
=0;
2535 p
= s
->urb_list
.prev
;
2537 while (p
!= &s
->urb_list
) {
2540 process_urb (s
, p2
);
2541 if (s
->unlink_urb_done
) {
2542 s
->unlink_urb_done
=0;
2546 if ((jiffies
- s
->timeout_check
) > (HZ
/30))
2547 uhci_check_timeouts(s
);
2550 uhci_cleanup_unlink(s
, 0);
2551 uhci_switch_timer_int(s
);
2553 spin_unlock (&s
->urb_list_lock
);
2555 outw (status
, io_addr
+ USBSTS
);
2557 //dbg("uhci_interrupt: done");
2560 _static
void reset_hc (uhci_t
*s
)
2562 unsigned int io_addr
= s
->io_addr
;
2565 /* Global reset for 50ms */
2566 outw (USBCMD_GRESET
, io_addr
+ USBCMD
);
2568 outw (0, io_addr
+ USBCMD
);
2572 _static
void start_hc (uhci_t
*s
)
2574 unsigned int io_addr
= s
->io_addr
;
2578 * Reset the HC - this will force us to get a
2579 * new notification of any already connected
2580 * ports due to the virtual disconnect that it
2583 outw (USBCMD_HCRESET
, io_addr
+ USBCMD
);
2585 while (inw (io_addr
+ USBCMD
) & USBCMD_HCRESET
) {
2587 err("USBCMD_HCRESET timed out!");
2592 /* Turn on all interrupts */
2593 outw (USBINTR_TIMEOUT
| USBINTR_RESUME
| USBINTR_IOC
| USBINTR_SP
, io_addr
+ USBINTR
);
2595 /* Start at frame 0 */
2596 outw (0, io_addr
+ USBFRNUM
);
2597 outl (virt_to_bus (s
->framelist
), io_addr
+ USBFLBASEADD
);
2599 /* Run and mark it configured with a 64-byte max packet */
2600 outw (USBCMD_RS
| USBCMD_CF
| USBCMD_MAXP
, io_addr
+ USBCMD
);
2605 _static
void uhci_cleanup_dev(uhci_t
*s
)
2607 struct usb_device
*root_hub
= s
->bus
->root_hub
;
2609 s
->running
= 0; // Don't allow submit_urb
2612 usb_disconnect (&root_hub
);
2617 uhci_unlink_urbs (s
, 0, 1); // Forced unlink of remaining URBs
2618 uhci_cleanup_unlink (s
, 1); // force cleanup of async killed URBs
2620 usb_deregister_bus (s
->bus
);
2622 release_region (s
->io_addr
, s
->io_size
);
2623 free_irq (s
->irq
, s
);
2624 usb_free_bus (s
->bus
);
2629 _static
int __init
uhci_start_usb (uhci_t
*s
)
2631 /* connect the virtual root hub */
2632 struct usb_device
*usb_dev
;
2634 usb_dev
= usb_alloc_dev (NULL
, s
->bus
);
2638 s
->bus
->root_hub
= usb_dev
;
2639 usb_connect (usb_dev
);
2641 if (usb_new_device (usb_dev
) != 0) {
2642 usb_free_dev (usb_dev
);
2649 _static
int handle_pm_event (struct pm_dev
*dev
, pm_request_t rqst
, void *data
)
2651 uhci_t
*s
= (uhci_t
*) dev
->data
;
2652 dbg("handle_apm_event(%d)", rqst
);
2666 _static
int __init
alloc_uhci (struct pci_dev
*dev
, int irq
, unsigned int io_addr
, unsigned int io_size
)
2669 struct usb_bus
*bus
;
2670 struct pm_dev
*pmdev
;
2671 char buf
[8], *bufp
= buf
;
2674 sprintf(buf
, "%d", irq
);
2676 bufp
= __irq_itoa(irq
);
2678 printk(KERN_INFO __FILE__
": USB UHCI at I/O 0x%x, IRQ %s\n",
2681 s
= kmalloc (sizeof (uhci_t
), GFP_KERNEL
);
2685 memset (s
, 0, sizeof (uhci_t
));
2686 INIT_LIST_HEAD (&s
->free_desc
);
2687 INIT_LIST_HEAD (&s
->urb_list
);
2688 INIT_LIST_HEAD (&s
->urb_unlinked
);
2689 spin_lock_init (&s
->urb_list_lock
);
2690 spin_lock_init (&s
->qh_lock
);
2691 spin_lock_init (&s
->td_lock
);
2692 atomic_set(&s
->avoid_bulk
, 0);
2693 s
->timeout_urbs
= 0;
2695 s
->io_addr
= io_addr
;
2696 s
->io_size
= io_size
;
2697 s
->next
= devs
; //chain new uhci device into global list
2698 s
->timeout_check
= 0;
2701 bus
= usb_alloc_bus (&uhci_device_operations
);
2710 /* UHCI specs says devices must have 2 ports, but goes on to say */
2711 /* they may have more but give no way to determine how many they */
2712 /* have, so default to 2 */
2713 /* According to the UHCI spec, Bit 7 is always set to 1. So we try */
2714 /* to use this to our advantage */
2716 for (s
->maxports
= 0; s
->maxports
< (io_size
- 0x10) / 2; s
->maxports
++) {
2717 unsigned int portstatus
;
2719 portstatus
= inw (io_addr
+ 0x10 + (s
->maxports
* 2));
2720 dbg("port %i, adr %x status %x", s
->maxports
,
2721 io_addr
+ 0x10 + (s
->maxports
* 2), portstatus
);
2722 if (!(portstatus
& 0x0080))
2725 warn("Detected %d ports", s
->maxports
);
2727 /* This is experimental so anything less than 2 or greater than 8 is */
2728 /* something weird and we'll ignore it */
2729 if (s
->maxports
< 2 || s
->maxports
> 8) {
2730 dbg("Port count misdetected, forcing to 2 ports");
2734 s
->rh
.numports
= s
->maxports
;
2736 if (init_skel (s
)) {
2742 request_region (s
->io_addr
, io_size
, MODNAME
);
2744 usb_register_bus (s
->bus
);
2748 if (request_irq (irq
, uhci_interrupt
, SA_SHIRQ
, MODNAME
, s
)) {
2749 err("request_irq %d failed!",irq
);
2752 release_region (s
->io_addr
, s
->io_size
);
2760 if(uhci_start_usb (s
) < 0) {
2761 uhci_cleanup_dev(s
);
2765 //chain new uhci device into global list
2768 pmdev
= pm_register(PM_PCI_DEV
, PM_PCI_ID(dev
), handle_pm_event
);
2775 _static
int __init
start_uhci (struct pci_dev
*dev
)
2779 /* Search for the IO base address.. */
2780 for (i
= 0; i
< 6; i
++) {
2782 unsigned int io_addr
= dev
->resource
[i
].start
;
2783 unsigned int io_size
=
2784 dev
->resource
[i
].end
- dev
->resource
[i
].start
+ 1;
2785 if (!(dev
->resource
[i
].flags
& 1))
2788 /* Is it already in use? */
2789 if (check_region (io_addr
, io_size
))
2791 /* disable legacy emulation */
2792 pci_write_config_word (dev
, USBLEGSUP
, USBLEGSUP_DEFAULT
);
2793 if(dev
->vendor
==0x8086) {
2794 info("Intel USB controller: setting latency timer to %d", UHCI_LATENCY_TIMER
);
2795 pci_write_config_byte(dev
, PCI_LATENCY_TIMER
, UHCI_LATENCY_TIMER
);
2798 return alloc_uhci(dev
, dev
->irq
, io_addr
, io_size
);
2803 int __init
uhci_init (void)
2805 int retval
= -ENODEV
;
2806 struct pci_dev
*dev
= NULL
;
2812 uhci_desc_kmem
= kmem_cache_create("uhci_desc", sizeof(uhci_desc_t
), 0, SLAB_HWCACHE_ALIGN
, NULL
, NULL
);
2814 if(!uhci_desc_kmem
) {
2815 err("kmem_cache_create for uhci_desc failed (out of memory)");
2819 urb_priv_kmem
= kmem_cache_create("urb_priv", sizeof(urb_priv_t
), 0, SLAB_HWCACHE_ALIGN
, NULL
, NULL
);
2821 if(!urb_priv_kmem
) {
2822 err("kmem_cache_create for urb_priv_t failed (out of memory)");
2828 #ifdef CONFIG_USB_UHCI_HIGH_BANDWIDTH
2829 info("High bandwidth mode enabled");
2832 dev
= pci_find_class (PCI_CLASS_SERIAL_USB
<< 8, dev
);
2837 pci_read_config_byte (dev
, PCI_CLASS_PROG
, &type
);
2841 if (pci_enable_device (dev
) < 0)
2846 err("Found UHCI device with no IRQ assigned. Check BIOS settings!");
2851 retval
= start_uhci (dev
);
2860 void __exit
uhci_cleanup (void)
2863 while ((s
= devs
)) {
2865 uhci_cleanup_dev(s
);
2868 if(kmem_cache_destroy(uhci_desc_kmem
))
2869 err("uhci_desc_kmem remained");
2871 if(kmem_cache_destroy(urb_priv_kmem
))
2872 err("urb_priv_kmem remained");
2877 int init_module (void)
2879 return uhci_init ();
2882 void cleanup_module (void)
2884 pm_unregister_all (handle_pm_event
);