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.222 2000/03/13 21:18:02 fliegl 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 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,3,44)
35 #include <asm/uaccess.h>
38 #include <asm/system.h>
40 /* This enables more detailed sanity checks in submit_iso */
41 //#define ISO_SANITY_CHECK
43 /* This enables debug printks */
46 /* This enables all symbols to be exported, to ease debugging oopses */
47 //#define DEBUG_SYMBOLS
49 /* This enables an extra UHCI slab for memory debugging */
52 #include <linux/usb.h>
54 #include "usb-uhci-debug.h"
58 #define dbg(format, arg...) do {} while (0)
66 #define _static static
69 #define queue_dbg dbg //err
70 #define async_dbg dbg //err
73 static kmem_cache_t
*uhci_desc_kmem
;
74 static kmem_cache_t
*urb_priv_kmem
;
77 #define SLAB_FLAG (in_interrupt ()? SLAB_ATOMIC : SLAB_KERNEL)
78 #define KMALLOC_FLAG (in_interrupt ()? GFP_ATOMIC : GFP_KERNEL)
80 #define CONFIG_USB_UHCI_HIGH_BANDWIDTH
81 #define USE_CTRL_DEPTH_FIRST 0 // 0: Breadth first, 1: Depth first
82 #define USE_BULK_DEPTH_FIRST 0 // 0: Breadth first, 1: Depth first
84 // stop bandwidth reclamation after (roughly) 50ms
85 #define IDLE_TIMEOUT (HZ/20)
87 _static
int rh_submit_urb (urb_t
*urb
);
88 _static
int rh_unlink_urb (urb_t
*urb
);
89 _static
int delete_qh (uhci_t
*s
, uhci_desc_t
*qh
);
90 _static
int process_transfer (uhci_t
*s
, urb_t
*urb
, int mode
);
91 _static
int process_interrupt (uhci_t
*s
, urb_t
*urb
);
92 _static
int process_iso (uhci_t
*s
, urb_t
*urb
, int force
);
94 static uhci_t
*devs
= NULL
;
96 /* used by userspace UHCI data structure dumper */
97 uhci_t
**uhci_devices
= &devs
;
99 /*-------------------------------------------------------------------*/
100 // Cleans up collected QHs
101 void clean_descs(uhci_t
*s
, int force
)
105 int now
=UHCI_GET_CURRENT_FRAME(s
);
109 while (q
!= &s
->free_desc
) {
110 qh
= list_entry (q
, uhci_desc_t
, horizontal
);
111 if ((qh
->last_used
!=now
) || force
)
114 q
=qh
->horizontal
.prev
;
117 /*-------------------------------------------------------------------*/
118 #ifdef CONFIG_USB_UHCI_HIGH_BANDWIDTH
119 _static
void enable_desc_loop(uhci_t
*s
, urb_t
*urb
)
123 spin_lock_irqsave (&s
->qh_lock
, flags
);
124 s
->chain_end
->hw
.qh
.head
&=~UHCI_PTR_TERM
;
127 ((urb_priv_t
*)urb
->hcpriv
)->use_loop
=1;
128 spin_unlock_irqrestore (&s
->qh_lock
, flags
);
130 /*-------------------------------------------------------------------*/
131 _static
void disable_desc_loop(uhci_t
*s
, urb_t
*urb
)
135 spin_lock_irqsave (&s
->qh_lock
, flags
);
137 if (((urb_priv_t
*)urb
->hcpriv
)->use_loop
) {
140 if (!s
->loop_usage
) {
141 s
->chain_end
->hw
.qh
.head
|=UHCI_PTR_TERM
;
144 ((urb_priv_t
*)urb
->hcpriv
)->use_loop
=0;
146 spin_unlock_irqrestore (&s
->qh_lock
, flags
);
149 /*-------------------------------------------------------------------*/
150 _static
void queue_urb_unlocked (uhci_t
*s
, urb_t
*urb
)
152 struct list_head
*p
=&urb
->urb_list
;
153 #ifdef CONFIG_USB_UHCI_HIGH_BANDWIDTH
156 type
=usb_pipetype (urb
->pipe
);
158 if ((type
== PIPE_BULK
) || (type
== PIPE_CONTROL
))
159 enable_desc_loop(s
, urb
);
162 ((urb_priv_t
*)urb
->hcpriv
)->started
=jiffies
;
163 list_add (p
, &s
->urb_list
);
165 /*-------------------------------------------------------------------*/
166 _static
void queue_urb (uhci_t
*s
, urb_t
*urb
)
168 unsigned long flags
=0;
170 spin_lock_irqsave (&s
->urb_list_lock
, flags
);
171 queue_urb_unlocked(s
,urb
);
172 spin_unlock_irqrestore (&s
->urb_list_lock
, flags
);
174 /*-------------------------------------------------------------------*/
175 _static
void dequeue_urb (uhci_t
*s
, urb_t
*urb
)
177 #ifdef CONFIG_USB_UHCI_HIGH_BANDWIDTH
180 type
=usb_pipetype (urb
->pipe
);
182 if ((type
== PIPE_BULK
) || (type
== PIPE_CONTROL
))
183 disable_desc_loop(s
, urb
);
186 list_del (&urb
->urb_list
);
188 /*-------------------------------------------------------------------*/
189 _static
int alloc_td (uhci_desc_t
** new, int flags
)
192 *new= kmem_cache_alloc(uhci_desc_kmem
, SLAB_FLAG
);
194 *new = (uhci_desc_t
*) kmalloc (sizeof (uhci_desc_t
), KMALLOC_FLAG
);
198 memset (*new, 0, sizeof (uhci_desc_t
));
199 (*new)->hw
.td
.link
= UHCI_PTR_TERM
| (flags
& UHCI_PTR_BITS
); // last by default
200 (*new)->type
= TD_TYPE
;
202 INIT_LIST_HEAD (&(*new)->vertical
);
203 INIT_LIST_HEAD (&(*new)->horizontal
);
207 /*-------------------------------------------------------------------*/
208 // append a qh to td.link physically, the SW linkage is not affected
209 _static
void append_qh(uhci_t
*s
, uhci_desc_t
*td
, uhci_desc_t
* qh
, int flags
)
213 spin_lock_irqsave (&s
->td_lock
, xxx
);
215 td
->hw
.td
.link
= virt_to_bus (qh
) | (flags
& UHCI_PTR_DEPTH
) | UHCI_PTR_QH
;
218 spin_unlock_irqrestore (&s
->td_lock
, xxx
);
220 /*-------------------------------------------------------------------*/
221 /* insert td at last position in td-list of qh (vertical) */
222 _static
int insert_td (uhci_t
*s
, uhci_desc_t
*qh
, uhci_desc_t
* new, int flags
)
227 spin_lock_irqsave (&s
->td_lock
, xxx
);
229 list_add_tail (&new->vertical
, &qh
->vertical
);
231 prev
= list_entry (new->vertical
.prev
, uhci_desc_t
, vertical
);
234 // virgin qh without any tds
235 qh
->hw
.qh
.element
= virt_to_bus (new);
238 // already tds inserted, implicitely remove TERM bit of prev
239 prev
->hw
.td
.link
= virt_to_bus (new) | (flags
& UHCI_PTR_DEPTH
);
242 spin_unlock_irqrestore (&s
->td_lock
, xxx
);
246 /*-------------------------------------------------------------------*/
247 /* insert new_td after td (horizontal) */
248 _static
int insert_td_horizontal (uhci_t
*s
, uhci_desc_t
*td
, uhci_desc_t
* new)
253 spin_lock_irqsave (&s
->td_lock
, flags
);
255 next
= list_entry (td
->horizontal
.next
, uhci_desc_t
, horizontal
);
256 list_add (&new->horizontal
, &td
->horizontal
);
257 new->hw
.td
.link
= td
->hw
.td
.link
;
258 td
->hw
.td
.link
= virt_to_bus (new);
260 spin_unlock_irqrestore (&s
->td_lock
, flags
);
264 /*-------------------------------------------------------------------*/
265 _static
int unlink_td (uhci_t
*s
, uhci_desc_t
*element
, int phys_unlink
)
267 uhci_desc_t
*next
, *prev
;
271 spin_lock_irqsave (&s
->td_lock
, flags
);
273 next
= list_entry (element
->vertical
.next
, uhci_desc_t
, vertical
);
275 if (next
== element
) {
277 prev
= list_entry (element
->horizontal
.prev
, uhci_desc_t
, horizontal
);
280 prev
= list_entry (element
->vertical
.prev
, uhci_desc_t
, vertical
);
283 // really remove HW linking
284 if (prev
->type
== TD_TYPE
)
285 prev
->hw
.td
.link
= element
->hw
.td
.link
;
287 prev
->hw
.qh
.element
= element
->hw
.td
.link
;
293 list_del (&element
->vertical
);
295 list_del (&element
->horizontal
);
297 spin_unlock_irqrestore (&s
->td_lock
, flags
);
302 /*-------------------------------------------------------------------*/
303 _static
int delete_desc (uhci_desc_t
*element
)
306 kmem_cache_free(uhci_desc_kmem
, element
);
312 /*-------------------------------------------------------------------*/
313 // Allocates qh element
314 _static
int alloc_qh (uhci_desc_t
** new)
317 *new= kmem_cache_alloc(uhci_desc_kmem
, SLAB_FLAG
);
319 *new = (uhci_desc_t
*) kmalloc (sizeof (uhci_desc_t
), KMALLOC_FLAG
);
323 memset (*new, 0, sizeof (uhci_desc_t
));
324 (*new)->hw
.qh
.head
= UHCI_PTR_TERM
;
325 (*new)->hw
.qh
.element
= UHCI_PTR_TERM
;
326 (*new)->type
= QH_TYPE
;
329 INIT_LIST_HEAD (&(*new)->horizontal
);
330 INIT_LIST_HEAD (&(*new)->vertical
);
332 dbg("Allocated qh @ %p", *new);
336 /*-------------------------------------------------------------------*/
337 // inserts new qh before/after the qh at pos
338 // flags: 0: insert before pos, 1: insert after pos (for low speed transfers)
339 _static
int insert_qh (uhci_t
*s
, uhci_desc_t
*pos
, uhci_desc_t
*new, int order
)
344 spin_lock_irqsave (&s
->qh_lock
, flags
);
347 // (OLD) (POS) -> (OLD) (NEW) (POS)
348 old
= list_entry (pos
->horizontal
.prev
, uhci_desc_t
, horizontal
);
349 list_add_tail (&new->horizontal
, &pos
->horizontal
);
350 new->hw
.qh
.head
= MAKE_QH_ADDR (pos
) ;
351 if (!(old
->hw
.qh
.head
& UHCI_PTR_TERM
))
352 old
->hw
.qh
.head
= MAKE_QH_ADDR (new) ;
355 // (POS) (OLD) -> (POS) (NEW) (OLD)
356 old
= list_entry (pos
->horizontal
.next
, uhci_desc_t
, horizontal
);
357 list_add (&new->horizontal
, &pos
->horizontal
);
358 new->hw
.qh
.head
= MAKE_QH_ADDR (old
);
359 pos
->hw
.qh
.head
= MAKE_QH_ADDR (new) ;
364 spin_unlock_irqrestore (&s
->qh_lock
, flags
);
369 /*-------------------------------------------------------------------*/
370 _static
int unlink_qh (uhci_t
*s
, uhci_desc_t
*element
)
375 spin_lock_irqsave (&s
->qh_lock
, flags
);
377 prev
= list_entry (element
->horizontal
.prev
, uhci_desc_t
, horizontal
);
378 prev
->hw
.qh
.head
= element
->hw
.qh
.head
;
380 list_del(&element
->horizontal
);
383 spin_unlock_irqrestore (&s
->qh_lock
, flags
);
387 /*-------------------------------------------------------------------*/
388 _static
int delete_qh (uhci_t
*s
, uhci_desc_t
*qh
)
393 list_del (&qh
->horizontal
);
395 while ((p
= qh
->vertical
.next
) != &qh
->vertical
) {
396 td
= list_entry (p
, uhci_desc_t
, vertical
);
397 dbg("unlink td @ %p",td
);
398 unlink_td (s
, td
, 0); // no physical unlink
406 /*-------------------------------------------------------------------*/
407 _static
void clean_td_chain (uhci_desc_t
*td
)
415 while ((p
= td
->horizontal
.next
) != &td
->horizontal
) {
416 td1
= list_entry (p
, uhci_desc_t
, horizontal
);
423 /*-------------------------------------------------------------------*/
424 _static
void fill_td (uhci_desc_t
*td
, int status
, int info
, __u32 buffer
)
426 td
->hw
.td
.status
= status
;
427 td
->hw
.td
.info
= info
;
428 td
->hw
.td
.buffer
= buffer
;
430 /*-------------------------------------------------------------------*/
431 // Removes ALL qhs in chain (paranoia!)
432 _static
void cleanup_skel (uhci_t
*s
)
441 for (n
= 0; n
< 8; n
++) {
442 td
= s
->int_chain
[n
];
447 for (n
= 0; n
< 1024; n
++) {
455 free_page ((unsigned long) s
->framelist
);
457 if (s
->control_chain
) {
458 // completed init_skel?
460 uhci_desc_t
*qh
, *qh1
;
462 qh
= s
->control_chain
;
463 while ((p
= qh
->horizontal
.next
) != &qh
->horizontal
) {
464 qh1
= list_entry (p
, uhci_desc_t
, horizontal
);
471 if (s
->ls_control_chain
)
472 delete_desc (s
->ls_control_chain
);
473 if (s
->control_chain
)
474 delete_desc(s
->control_chain
);
476 delete_desc (s
->bulk_chain
);
478 delete_desc (s
->chain_end
);
480 dbg("cleanup_skel finished");
482 /*-------------------------------------------------------------------*/
483 // allocates framelist and qh-skeletons
484 // only HW-links provide continous linking, SW-links stay in their domain (ISO/INT)
485 _static
int init_skel (uhci_t
*s
)
488 uhci_desc_t
*qh
, *td
;
492 s
->framelist
= (__u32
*) get_free_page (GFP_KERNEL
);
497 memset (s
->framelist
, 0, 4096);
499 dbg("allocating iso desc pointer list");
500 s
->iso_td
= (uhci_desc_t
**) kmalloc (1024 * sizeof (uhci_desc_t
*), GFP_KERNEL
);
503 goto init_skel_cleanup
;
505 s
->ls_control_chain
= NULL
;
506 s
->control_chain
= NULL
;
507 s
->bulk_chain
= NULL
;
510 dbg("allocating iso descs");
511 for (n
= 0; n
< 1024; n
++) {
512 // allocate skeleton iso/irq-tds
513 ret
= alloc_td (&td
, 0);
515 goto init_skel_cleanup
;
517 s
->framelist
[n
] = ((__u32
) virt_to_bus (td
));
520 dbg("allocating qh: chain_end");
521 ret
= alloc_qh (&qh
);
524 goto init_skel_cleanup
;
528 ret
= alloc_td (&td
, 0);
531 goto init_skel_cleanup
;
533 fill_td (td
, TD_CTRL_IOC
, 0, 0); // generate 1ms interrupt
534 insert_td (s
, qh
, td
, 0);
536 dbg("allocating qh: bulk_chain");
537 ret
= alloc_qh (&qh
);
539 goto init_skel_cleanup
;
541 insert_qh (s
, s
->chain_end
, qh
, 0);
544 dbg("allocating qh: control_chain");
545 ret
= alloc_qh (&qh
);
547 goto init_skel_cleanup
;
549 insert_qh (s
, s
->bulk_chain
, qh
, 0);
550 s
->control_chain
= qh
;
552 #ifdef CONFIG_USB_UHCI_HIGH_BANDWIDTH
553 // disabled reclamation loop
554 s
->chain_end
->hw
.qh
.head
=virt_to_bus(s
->control_chain
) | UHCI_PTR_QH
| UHCI_PTR_TERM
;
557 dbg("allocating qh: ls_control_chain");
558 ret
= alloc_qh (&qh
);
560 goto init_skel_cleanup
;
562 insert_qh (s
, s
->control_chain
, qh
, 0);
563 s
->ls_control_chain
= qh
;
565 for (n
= 0; n
< 8; n
++)
568 dbg("allocating skeleton INT-TDs");
570 for (n
= 0; n
< 8; n
++) {
575 goto init_skel_cleanup
;
576 s
->int_chain
[n
] = td
;
578 s
->int_chain
[0]->hw
.td
.link
= virt_to_bus (s
->ls_control_chain
) | UHCI_PTR_QH
;
581 s
->int_chain
[n
]->hw
.td
.link
= virt_to_bus (s
->int_chain
[0]);
585 dbg("Linking skeleton INT-TDs");
587 for (n
= 0; n
< 1024; n
++) {
588 // link all iso-tds to the interrupt chains
590 dbg("framelist[%i]=%x",n
,s
->framelist
[n
]);
592 ((uhci_desc_t
*) s
->iso_td
[n
])->hw
.td
.link
= virt_to_bus(s
->int_chain
[0]);
594 for (o
= 1, m
= 2; m
<= 128; o
++, m
+= m
)
595 if ((n
& (m
- 1)) == ((m
- 1) / 2))
596 ((uhci_desc_t
*) s
->iso_td
[n
])->hw
.td
.link
= virt_to_bus (s
->int_chain
[o
]);
600 //uhci_show_queue(s->control_chain);
601 dbg("init_skel exit");
609 /*-------------------------------------------------------------------*/
611 // assembles QHs und TDs for control, bulk and iso
612 /*-------------------------------------------------------------------*/
613 _static
int uhci_submit_control_urb (urb_t
*urb
)
615 uhci_desc_t
*qh
, *td
;
616 uhci_t
*s
= (uhci_t
*) urb
->dev
->bus
->hcpriv
;
617 urb_priv_t
*urb_priv
= urb
->hcpriv
;
618 unsigned long destination
, status
;
619 int maxsze
= usb_maxpacket (urb
->dev
, urb
->pipe
, usb_pipeout (urb
->pipe
));
622 int depth_first
=USE_CTRL_DEPTH_FIRST
; // UHCI descriptor chasing method
625 err("uhci_submit_control_urb: pipesize for pipe %x is zero", urb
->pipe
);
629 dbg("uhci_submit_control start");
630 alloc_qh (&qh
); // alloc qh for this request
635 alloc_td (&td
, UHCI_PTR_DEPTH
* depth_first
); // get td for setup stage
642 /* The "pipe" thing contains the destination in bits 8--18 */
643 destination
= (urb
->pipe
& PIPE_DEVEP_MASK
) | USB_PID_SETUP
;
646 status
= (urb
->pipe
& TD_CTRL_LS
) | TD_CTRL_ACTIVE
|
647 (urb
->transfer_flags
& USB_DISABLE_SPD
? 0 : TD_CTRL_SPD
) | (3 << 27);
649 /* Build the TD for the control request, try forever, 8 bytes of data */
650 fill_td (td
, status
, destination
| (7 << 21), virt_to_bus (urb
->setup_packet
));
652 insert_td (s
, qh
, td
, 0); // queue 'setup stage'-td in qh
655 char *sp
=urb
->setup_packet
;
656 dbg("SETUP to pipe %x: %x %x %x %x %x %x %x %x", urb
->pipe
,
657 sp
[0],sp
[1],sp
[2],sp
[3],sp
[4],sp
[5],sp
[6],sp
[7]);
662 len
= urb
->transfer_buffer_length
;
663 data
= urb
->transfer_buffer
;
665 /* If direction is "send", change the frame from SETUP (0x2D)
666 to OUT (0xE1). Else change it from SETUP to IN (0x69). */
668 destination
= (urb
->pipe
& PIPE_DEVEP_MASK
) | (usb_pipeout (urb
->pipe
)?USB_PID_OUT
:USB_PID_IN
);
673 alloc_td (&td
, UHCI_PTR_DEPTH
* depth_first
);
682 destination
^= 1 << TD_TOKEN_TOGGLE
; // toggle DATA0/1
684 fill_td (td
, status
, destination
| ((pktsze
- 1) << 21),
685 virt_to_bus (data
)); // Status, pktsze bytes of data
687 insert_td (s
, qh
, td
, UHCI_PTR_DEPTH
* depth_first
); // queue 'data stage'-td in qh
693 /* Build the final TD for control status */
694 /* It's only IN if the pipe is out AND we aren't expecting data */
696 destination
&= ~UHCI_PID
;
698 if (usb_pipeout (urb
->pipe
) || (urb
->transfer_buffer_length
== 0))
699 destination
|= USB_PID_IN
;
701 destination
|= USB_PID_OUT
;
703 destination
|= 1 << TD_TOKEN_TOGGLE
; /* End in Data1 */
705 alloc_td (&td
, UHCI_PTR_DEPTH
);
711 status
&=~TD_CTRL_SPD
;
713 /* no limit on errors on final packet , 0 bytes of data */
714 fill_td (td
, status
| TD_CTRL_IOC
, destination
| (UHCI_NULL_DATA_SIZE
<< 21),
717 insert_td (s
, qh
, td
, UHCI_PTR_DEPTH
* depth_first
); // queue status td
719 list_add (&qh
->desc_list
, &urb_priv
->desc_list
);
721 urb
->status
= -EINPROGRESS
;
722 queue_urb (s
, urb
); // queue before inserting in desc chain
724 qh
->hw
.qh
.element
&= ~UHCI_PTR_TERM
;
726 //uhci_show_queue(qh);
727 /* Start it up... put low speed first */
728 if (urb
->pipe
& TD_CTRL_LS
)
729 insert_qh (s
, s
->control_chain
, qh
, 0);
731 insert_qh (s
, s
->bulk_chain
, qh
, 0);
733 dbg("uhci_submit_control end");
736 /*-------------------------------------------------------------------*/
737 // For queued bulk transfers, two additional QH helpers are allocated (nqh, bqh)
738 // Due to the linking with other bulk urbs, it has to be locked with urb_list_lock!
740 _static
int uhci_submit_bulk_urb (urb_t
*urb
, urb_t
*bulk_urb
)
742 uhci_t
*s
= (uhci_t
*) urb
->dev
->bus
->hcpriv
;
743 urb_priv_t
*urb_priv
= urb
->hcpriv
;
744 uhci_desc_t
*qh
, *td
, *nqh
, *bqh
;
745 unsigned long destination
, status
;
747 unsigned int pipe
= urb
->pipe
;
748 int maxsze
= usb_maxpacket (urb
->dev
, pipe
, usb_pipeout (pipe
));
750 int depth_first
=USE_BULK_DEPTH_FIRST
; // UHCI descriptor chasing method
751 urb_priv_t
*upriv
, *bpriv
;
753 if (usb_endpoint_halted (urb
->dev
, usb_pipeendpoint (pipe
), usb_pipeout (pipe
)))
756 if (urb
->transfer_buffer_length
< 0) {
757 err("Negative transfer length in submit_bulk");
764 queue_dbg("uhci_submit_bulk_urb: urb %p, old %p, pipe %08x, len %i",
765 urb
,bulk_urb
,urb
->pipe
,urb
->transfer_buffer_length
);
767 upriv
=(urb_priv_t
*)urb
->hcpriv
;
770 alloc_qh (&qh
); // get qh for this request
775 if (urb
->transfer_flags
& USB_QUEUE_BULK
) {
776 alloc_qh(&nqh
); // placeholder for clean unlink
781 upriv
->next_qh
= nqh
;
782 queue_dbg("new next qh %p",nqh
);
786 bpriv
= (urb_priv_t
*)bulk_urb
->hcpriv
;
787 qh
= bpriv
->bottom_qh
; // re-use bottom qh and next qh
788 nqh
= bpriv
->next_qh
;
790 bpriv
->next_queued_urb
=urb
;
791 upriv
->prev_queued_urb
=bulk_urb
;
794 queue_dbg("uhci_submit_bulk: qh=%p, nqh=%p\n",bqh
,nqh
);
796 if (urb
->transfer_flags
& USB_QUEUE_BULK
) {
797 alloc_qh (&bqh
); // "bottom" QH,
806 bqh
->hw
.qh
.element
= UHCI_PTR_TERM
;
807 bqh
->hw
.qh
.element
= virt_to_bus(nqh
)|UHCI_PTR_QH
;
808 upriv
->bottom_qh
= bqh
;
809 queue_dbg("uhci_submit_bulk: new bqh %p\n",bqh
);
813 /* The "pipe" thing contains the destination in bits 8--18. */
814 destination
= (pipe
& PIPE_DEVEP_MASK
) | usb_packetid (pipe
);
817 status
= (pipe
& TD_CTRL_LS
) | TD_CTRL_ACTIVE
|
818 ((urb
->transfer_flags
& USB_DISABLE_SPD
) ? 0 : TD_CTRL_SPD
) | (3 << 27);
820 /* Build the TDs for the bulk request */
821 len
= urb
->transfer_buffer_length
;
822 data
= urb
->transfer_buffer
;
824 do { // TBD: Really allow zero-length packets?
827 alloc_td (&td
, UHCI_PTR_DEPTH
* depth_first
);
837 // pktsze bytes of data
838 info
= destination
| (((pktsze
- 1)&UHCI_NULL_DATA_SIZE
) << 21) |
839 (usb_gettoggle (urb
->dev
, usb_pipeendpoint (pipe
), usb_pipeout (pipe
)) << TD_TOKEN_TOGGLE
);
841 fill_td (td
, status
, info
, virt_to_bus (data
));
847 td
->hw
.td
.status
|= TD_CTRL_IOC
; // last one generates INT
849 insert_td (s
, qh
, td
, UHCI_PTR_DEPTH
* depth_first
);
850 usb_dotoggle (urb
->dev
, usb_pipeendpoint (pipe
), usb_pipeout (pipe
));
854 list_add (&qh
->desc_list
, &urb_priv
->desc_list
);
856 if (urb
->transfer_flags
& USB_QUEUE_BULK
) {
857 qh
->hw
.qh
.element
&=~UHCI_PTR_TERM
;
858 append_qh(s
, td
, bqh
, UHCI_PTR_DEPTH
* depth_first
);
861 urb
->status
= -EINPROGRESS
;
862 queue_urb_unlocked (s
, urb
);
864 qh
->hw
.qh
.element
&= ~UHCI_PTR_TERM
;
867 if (urb
->transfer_flags
& USB_QUEUE_BULK
) {
868 spin_lock (&s
->td_lock
); // both QHs in one go
869 insert_qh (s
, s
->chain_end
, qh
, 0); // Main QH
870 insert_qh (s
, s
->chain_end
, nqh
, 0); // Helper QH
871 spin_unlock (&s
->td_lock
);
874 insert_qh (s
, s
->chain_end
, qh
, 0);
877 //uhci_show_queue(s->bulk_chain);
878 //dbg("uhci_submit_bulk_urb: exit\n");
881 /*-------------------------------------------------------------------*/
882 _static
void uhci_clean_iso_step1(uhci_t
*s
, urb_priv_t
*urb_priv
)
887 for (p
= urb_priv
->desc_list
.next
; p
!= &urb_priv
->desc_list
; p
= p
->next
) {
888 td
= list_entry (p
, uhci_desc_t
, desc_list
);
889 unlink_td (s
, td
, 1);
892 /*-------------------------------------------------------------------*/
893 _static
void uhci_clean_iso_step2(uhci_t
*s
, urb_priv_t
*urb_priv
)
898 while ((p
= urb_priv
->desc_list
.next
) != &urb_priv
->desc_list
) {
899 td
= list_entry (p
, uhci_desc_t
, desc_list
);
904 /*-------------------------------------------------------------------*/
905 // mode: 0: unlink + no deletion mark, 1: regular (unlink/delete-mark), 2: don't unlink
906 // looks a bit complicated because of all the bulk queueing goodies
908 _static
void uhci_clean_transfer (uhci_t
*s
, urb_t
*urb
, uhci_desc_t
*qh
, int mode
)
910 uhci_desc_t
*bqh
, *nqh
, *prevqh
;
912 urb_priv_t
*priv
=(urb_priv_t
*)urb
->hcpriv
;
914 now
=UHCI_GET_CURRENT_FRAME(s
);
916 dbg("clean transfer urb %p, qh %p, mode %i",urb
,qh
,mode
);
919 if (!priv
->next_queued_urb
) { // no more appended bulk queues
924 if (priv
->prev_queued_urb
) {
925 urb_priv_t
* ppriv
=(urb_priv_t
*)priv
->prev_queued_urb
->hcpriv
;
927 ppriv
->bottom_qh
= priv
->bottom_qh
;
928 ppriv
->next_queued_urb
= NULL
;
930 else if (bqh
) { // queue dead
937 nqh
->last_used
= bqh
->last_used
= now
;
938 list_add_tail (&nqh
->horizontal
, &s
->free_desc
);
939 list_add_tail (&bqh
->horizontal
, &s
->free_desc
);
943 else { // there are queued urbs following
947 nurb
=priv
->next_queued_urb
;
948 spin_lock_irqsave (&s
->qh_lock
, flags
);
950 if (!priv
->prev_queued_urb
) { // top
952 prevqh
= list_entry (qh
->horizontal
.prev
, uhci_desc_t
, horizontal
);
953 prevqh
->hw
.qh
.head
= virt_to_bus(bqh
) | UHCI_PTR_QH
;
954 queue_dbg ("TOP relink of %p to %p-%p",qh
,prevqh
,bqh
);
956 list_del (&qh
->horizontal
);
957 list_add (&bqh
->horizontal
, &prevqh
->horizontal
);
960 else { //intermediate
961 urb_priv_t
* ppriv
=(urb_priv_t
*)priv
->prev_queued_urb
->hcpriv
;
964 bnqh
=list_entry (&((urb_priv_t
*)(nurb
->hcpriv
))->desc_list
.next
, uhci_desc_t
, desc_list
);
965 ppriv
->bottom_qh
=bnqh
;
966 ppriv
->next_queued_urb
=nurb
;
969 prevqh
= list_entry (ppriv
->desc_list
.next
, uhci_desc_t
, desc_list
);
970 prevqh
->hw
.qh
.head
= virt_to_bus(bqh
) | UHCI_PTR_QH
;
971 queue_dbg ("IM relink of %p to %p-%p",qh
,prevqh
,bqh
);
975 spin_unlock_irqrestore (&s
->qh_lock
, flags
);
976 ((urb_priv_t
*)nurb
->hcpriv
)->prev_queued_urb
=priv
->prev_queued_urb
;
981 list_add_tail (&qh
->horizontal
, &s
->free_desc
); // mark for later deletion
984 /*-------------------------------------------------------------------*/
985 // unlinks an urb by dequeuing its qh, waits some frames and forgets it
986 _static
int uhci_unlink_urb_sync (uhci_t
*s
, urb_t
*urb
)
989 urb_priv_t
*urb_priv
;
990 unsigned long flags
=0;
992 spin_lock_irqsave (&s
->urb_list_lock
, flags
);
994 if (urb
->status
== -EINPROGRESS
) {
995 // URB probably still in work
996 dequeue_urb (s
, urb
);
997 s
->unlink_urb_done
=1;
998 spin_unlock_irqrestore (&s
->urb_list_lock
, flags
);
1000 urb
->status
= -ENOENT
; // mark urb as killed
1001 urb_priv
= urb
->hcpriv
;
1003 switch (usb_pipetype (urb
->pipe
)) {
1004 case PIPE_ISOCHRONOUS
:
1005 case PIPE_INTERRUPT
:
1006 uhci_clean_iso_step1(s
, urb_priv
);
1008 uhci_clean_iso_step2(s
, urb_priv
);
1013 qh
= list_entry (urb_priv
->desc_list
.next
, uhci_desc_t
, desc_list
);
1014 spin_lock_irqsave (&s
->urb_list_lock
, flags
);
1015 uhci_clean_transfer(s
, urb
, qh
, 1);
1016 spin_unlock_irqrestore (&s
->urb_list_lock
, flags
);
1021 kmem_cache_free (urb_priv_kmem
, urb
->hcpriv
);
1023 kfree (urb
->hcpriv
);
1025 if (urb
->complete
) {
1026 dbg("unlink_urb: calling completion");
1027 urb
->complete ((struct urb
*) urb
);
1029 usb_dec_dev_use (urb
->dev
);
1033 spin_unlock_irqrestore (&s
->urb_list_lock
, flags
);
1036 /*-------------------------------------------------------------------*/
1037 // async unlink_urb completion/cleanup work
1038 // has to be protected by urb_list_lock!
1039 // features: if set in transfer_flags, the resulting status of the killed
1040 // transaction is not overwritten
1042 _static
void uhci_cleanup_unlink(uhci_t
*s
, int force
)
1044 struct list_head
*q
;
1046 struct usb_device
*dev
;
1048 urb_priv_t
*urb_priv
;
1050 q
=s
->urb_unlinked
.next
;
1051 now
=UHCI_GET_CURRENT_FRAME(s
);
1053 while (q
!= &s
->urb_unlinked
) {
1055 urb
= list_entry (q
, urb_t
, urb_list
);
1057 urb_priv
= (urb_priv_t
*)urb
->hcpriv
;
1058 q
= urb
->urb_list
.next
;
1061 ((urb_priv
->started
!= 0xffffffff) && (urb_priv
->started
!= now
))) {
1062 async_dbg("async cleanup %p",urb
);
1063 switch (usb_pipetype (urb
->pipe
)) { // process descriptors
1065 process_transfer (s
, urb
, 2);
1068 if (!s
->avoid_bulk
.counter
)
1069 process_transfer (s
, urb
, 2); // don't unlink (already done)
1073 case PIPE_ISOCHRONOUS
:
1074 process_iso (s
, urb
, 1); // force, don't unlink
1076 case PIPE_INTERRUPT
:
1077 process_interrupt (s
, urb
);
1081 if (!(urb
->transfer_flags
& USB_TIMEOUT_KILLED
))
1082 urb
->status
= -ECONNRESET
; // mark as asynchronously killed
1084 pipe
= urb
->pipe
; // completion may destroy all...
1086 urb_priv
= urb
->hcpriv
;
1088 if (urb
->complete
) {
1089 spin_unlock(&s
->urb_list_lock
);
1090 urb
->complete ((struct urb
*) urb
);
1091 spin_lock(&s
->urb_list_lock
);
1094 if (!(urb
->transfer_flags
& USB_TIMEOUT_KILLED
))
1095 urb
->status
= -ENOENT
; // now the urb is really dead
1097 usb_dec_dev_use (dev
);
1099 kmem_cache_free (urb_priv_kmem
, urb_priv
);
1103 switch (usb_pipetype (pipe
)) {
1104 case PIPE_ISOCHRONOUS
:
1105 case PIPE_INTERRUPT
:
1106 uhci_clean_iso_step2(s
, urb_priv
);
1109 list_del (&urb
->urb_list
);
1114 /*-------------------------------------------------------------------*/
1115 _static
int uhci_unlink_urb_async (uhci_t
*s
,urb_t
*urb
)
1118 urb_priv_t
*urb_priv
;
1120 async_dbg("unlink_urb_async called %p",urb
);
1122 if (urb
->status
== -EINPROGRESS
) {
1123 ((urb_priv_t
*)urb
->hcpriv
)->started
= ~0;
1124 dequeue_urb (s
, urb
);
1125 list_add_tail (&urb
->urb_list
, &s
->urb_unlinked
); // store urb
1127 s
->unlink_urb_done
= 1;
1129 urb
->status
= -ECONNABORTED
; // mark urb as "waiting to be killed"
1130 urb_priv
= (urb_priv_t
*)urb
->hcpriv
;
1132 switch (usb_pipetype (urb
->pipe
)) {
1133 case PIPE_ISOCHRONOUS
:
1134 case PIPE_INTERRUPT
:
1135 uhci_clean_iso_step1 (s
, urb_priv
);
1140 qh
= list_entry (urb_priv
->desc_list
.next
, uhci_desc_t
, desc_list
);
1141 uhci_clean_transfer (s
, urb
, qh
, 0);
1144 ((urb_priv_t
*)urb
->hcpriv
)->started
= UHCI_GET_CURRENT_FRAME(s
);
1147 return -EINPROGRESS
;
1149 /*-------------------------------------------------------------------*/
1150 _static
int uhci_unlink_urb (urb_t
*urb
)
1153 unsigned long flags
=0;
1154 dbg("uhci_unlink_urb called for %p",urb
);
1155 if (!urb
|| !urb
->dev
) // you never know...
1158 s
= (uhci_t
*) urb
->dev
->bus
->hcpriv
;
1160 if (usb_pipedevice (urb
->pipe
) == s
->rh
.devnum
)
1161 return rh_unlink_urb (urb
);
1166 if (urb
->transfer_flags
& USB_ASYNC_UNLINK
) {
1169 spin_lock_irqsave (&s
->urb_list_lock
, flags
);
1170 ret
= uhci_unlink_urb_async(s
, urb
);
1171 spin_unlock_irqrestore (&s
->urb_list_lock
, flags
);
1175 return uhci_unlink_urb_sync(s
, urb
);
1177 /*-------------------------------------------------------------------*/
1178 // In case of ASAP iso transfer, search the URB-list for already queued URBs
1179 // for this EP and calculate the earliest start frame for the new
1180 // URB (easy seamless URB continuation!)
1181 _static
int find_iso_limits (urb_t
*urb
, unsigned int *start
, unsigned int *end
)
1183 urb_t
*u
, *last_urb
= NULL
;
1184 uhci_t
*s
= (uhci_t
*) urb
->dev
->bus
->hcpriv
;
1185 struct list_head
*p
;
1187 unsigned long flags
;
1189 spin_lock_irqsave (&s
->urb_list_lock
, flags
);
1192 for (; p
!= &s
->urb_list
; p
= p
->prev
) {
1193 u
= list_entry (p
, urb_t
, urb_list
);
1194 // look for pending URBs with identical pipe handle
1195 // works only because iso doesn't toggle the data bit!
1196 if ((urb
->pipe
== u
->pipe
) && (urb
->dev
== u
->dev
) && (u
->status
== -EINPROGRESS
)) {
1198 *start
= u
->start_frame
;
1204 *end
= (last_urb
->start_frame
+ last_urb
->number_of_packets
) & 1023;
1208 spin_unlock_irqrestore(&s
->urb_list_lock
, flags
);
1212 /*-------------------------------------------------------------------*/
1213 // adjust start_frame according to scheduling constraints (ASAP etc)
1215 _static
int iso_find_start (urb_t
*urb
)
1217 uhci_t
*s
= (uhci_t
*) urb
->dev
->bus
->hcpriv
;
1219 unsigned int start_limit
= 0, stop_limit
= 0, queued_size
;
1222 now
= UHCI_GET_CURRENT_FRAME (s
) & 1023;
1224 if ((unsigned) urb
->number_of_packets
> 900)
1227 limits
= find_iso_limits (urb
, &start_limit
, &stop_limit
);
1228 queued_size
= (stop_limit
- start_limit
) & 1023;
1230 if (urb
->transfer_flags
& USB_ISO_ASAP
) {
1233 // 10ms setup should be enough //FIXME!
1234 urb
->start_frame
= (now
+ 10) & 1023;
1237 urb
->start_frame
= stop_limit
; //seamless linkage
1239 if (((now
- urb
->start_frame
) & 1023) <= (unsigned) urb
->number_of_packets
) {
1240 info("iso_find_start: gap in seamless isochronous scheduling");
1241 dbg("iso_find_start: now %u start_frame %u number_of_packets %u pipe 0x%08x",
1242 now
, urb
->start_frame
, urb
->number_of_packets
, urb
->pipe
);
1243 urb
->start_frame
= (now
+ 5) & 1023; // 5ms setup should be enough //FIXME!
1248 urb
->start_frame
&= 1023;
1249 if (((now
- urb
->start_frame
) & 1023) < (unsigned) urb
->number_of_packets
) {
1250 dbg("iso_find_start: now between start_frame and end");
1255 /* check if either start_frame or start_frame+number_of_packets-1 lies between start_limit and stop_limit */
1259 if (((urb
->start_frame
- start_limit
) & 1023) < queued_size
||
1260 ((urb
->start_frame
+ urb
->number_of_packets
- 1 - start_limit
) & 1023) < queued_size
) {
1261 dbg("iso_find_start: start_frame %u number_of_packets %u start_limit %u stop_limit %u",
1262 urb
->start_frame
, urb
->number_of_packets
, start_limit
, stop_limit
);
1268 /*-------------------------------------------------------------------*/
1269 // submits USB interrupt (ie. polling ;-)
1270 // ASAP-flag set implicitely
1271 // if period==0, the the transfer is only done once
1273 _static
int uhci_submit_int_urb (urb_t
*urb
)
1275 uhci_t
*s
= (uhci_t
*) urb
->dev
->bus
->hcpriv
;
1276 urb_priv_t
*urb_priv
= urb
->hcpriv
;
1279 int status
, destination
;
1281 unsigned int pipe
= urb
->pipe
;
1283 if (urb
->interval
< 0 || urb
->interval
>= 256)
1286 if (urb
->interval
== 0)
1289 for (nint
= 0, n
= 1; nint
<= 8; nint
++, n
+= n
) // round interval down to 2^n
1291 if (urb
->interval
< n
) {
1292 urb
->interval
= n
/ 2;
1299 dbg("Rounded interval to %i, chain %i", urb
->interval
, nint
);
1301 urb
->start_frame
= UHCI_GET_CURRENT_FRAME (s
) & 1023; // remember start frame, just in case...
1303 urb
->number_of_packets
= 1;
1305 // INT allows only one packet
1306 if (urb
->transfer_buffer_length
> usb_maxpacket (urb
->dev
, pipe
, usb_pipeout (pipe
)))
1309 ret
= alloc_td (&td
, UHCI_PTR_DEPTH
);
1314 status
= (pipe
& TD_CTRL_LS
) | TD_CTRL_ACTIVE
| TD_CTRL_IOC
|
1315 (urb
->transfer_flags
& USB_DISABLE_SPD
? 0 : TD_CTRL_SPD
) | (3 << 27);
1317 destination
= (urb
->pipe
& PIPE_DEVEP_MASK
) | usb_packetid (urb
->pipe
) |
1318 (((urb
->transfer_buffer_length
- 1) & 0x7ff) << 21);
1321 info
= destination
| (usb_gettoggle (urb
->dev
, usb_pipeendpoint (pipe
), usb_pipeout (pipe
)) << TD_TOKEN_TOGGLE
);
1323 fill_td (td
, status
, info
, virt_to_bus (urb
->transfer_buffer
));
1324 list_add_tail (&td
->desc_list
, &urb_priv
->desc_list
);
1326 urb
->status
= -EINPROGRESS
;
1329 insert_td_horizontal (s
, s
->int_chain
[nint
], td
); // store in INT-TDs
1331 usb_dotoggle (urb
->dev
, usb_pipeendpoint (pipe
), usb_pipeout (pipe
));
1335 /*-------------------------------------------------------------------*/
1336 _static
int uhci_submit_iso_urb (urb_t
*urb
)
1338 uhci_t
*s
= (uhci_t
*) urb
->dev
->bus
->hcpriv
;
1339 urb_priv_t
*urb_priv
= urb
->hcpriv
;
1341 int maxsze
= usb_maxpacket (urb
->dev
, pipe
, usb_pipeout (pipe
));
1343 uhci_desc_t
*td
, **tdm
;
1344 int status
, destination
;
1345 unsigned long flags
;
1347 __save_flags(flags
);
1348 __cli(); // Disable IRQs to schedule all ISO-TDs in time
1349 ret
= iso_find_start (urb
); // adjusts urb->start_frame for later use
1354 tdm
= (uhci_desc_t
**) kmalloc (urb
->number_of_packets
* sizeof (uhci_desc_t
*), KMALLOC_FLAG
);
1361 // First try to get all TDs
1362 for (n
= 0; n
< urb
->number_of_packets
; n
++) {
1363 dbg("n:%d urb->iso_frame_desc[n].length:%d", n
, urb
->iso_frame_desc
[n
].length
);
1364 if (!urb
->iso_frame_desc
[n
].length
) {
1365 // allows ISO striping by setting length to zero in iso_descriptor
1370 if(urb
->iso_frame_desc
[n
].length
> maxsze
) {
1371 #ifdef ISO_SANITY_CHECK
1372 err("submit_iso: urb->iso_frame_desc[%d].length(%d)>%d",n
, urb
->iso_frame_desc
[n
].length
, maxsze
);
1379 ret
= alloc_td (&td
, UHCI_PTR_DEPTH
);
1382 int i
; // Cleanup allocated TDs
1384 for (i
= 0; i
< n
; n
++)
1386 delete_desc(tdm
[i
]);
1394 status
= TD_CTRL_ACTIVE
| TD_CTRL_IOS
; //| (urb->transfer_flags&USB_DISABLE_SPD?0:TD_CTRL_SPD);
1396 destination
= (urb
->pipe
& PIPE_DEVEP_MASK
) | usb_packetid (urb
->pipe
);
1399 // Queue all allocated TDs
1400 for (n
= 0; n
< urb
->number_of_packets
; n
++) {
1406 status
|= TD_CTRL_IOC
;
1408 fill_td (td
, status
, destination
| (((urb
->iso_frame_desc
[n
].length
- 1) & 0x7ff) << 21),
1409 virt_to_bus (urb
->transfer_buffer
+ urb
->iso_frame_desc
[n
].offset
));
1410 list_add_tail (&td
->desc_list
, &urb_priv
->desc_list
);
1413 urb
->status
= -EINPROGRESS
;
1416 insert_td_horizontal (s
, s
->iso_td
[(urb
->start_frame
+ n
) & 1023], td
); // store in iso-tds
1422 dbg("ISO-INT# %i, start %i, now %i", urb
->number_of_packets
, urb
->start_frame
, UHCI_GET_CURRENT_FRAME (s
) & 1023);
1426 __restore_flags(flags
);
1430 /*-------------------------------------------------------------------*/
1431 // returns: 0 (no transfer queued), urb* (this urb already queued)
1433 _static urb_t
* search_dev_ep (uhci_t
*s
, urb_t
*urb
)
1435 struct list_head
*p
;
1437 unsigned int mask
= usb_pipecontrol(urb
->pipe
) ? (~USB_DIR_IN
) : (~0);
1439 dbg("search_dev_ep:");
1443 for (; p
!= &s
->urb_list
; p
= p
->next
) {
1444 tmp
= list_entry (p
, urb_t
, urb_list
);
1445 dbg("urb: %p", tmp
);
1446 // we can accept this urb if it is not queued at this time
1447 // or if non-iso transfer requests should be scheduled for the same device and pipe
1448 if ((!usb_pipeisoc(urb
->pipe
) && (tmp
->dev
== urb
->dev
) && !((tmp
->pipe
^ urb
->pipe
) & mask
)) ||
1450 return tmp
; // found another urb already queued for processing
1456 /*-------------------------------------------------------------------*/
1457 _static
int uhci_submit_urb (urb_t
*urb
)
1460 urb_priv_t
*urb_priv
;
1462 unsigned long flags
;
1463 urb_t
*bulk_urb
=NULL
;
1465 if (!urb
->dev
|| !urb
->dev
->bus
)
1468 s
= (uhci_t
*) urb
->dev
->bus
->hcpriv
;
1469 //dbg("submit_urb: %p type %d",urb,usb_pipetype(urb->pipe));
1474 if (usb_pipedevice (urb
->pipe
) == s
->rh
.devnum
)
1475 return rh_submit_urb (urb
); /* virtual root hub */
1477 usb_inc_dev_use (urb
->dev
);
1479 spin_lock_irqsave (&s
->urb_list_lock
, flags
);
1481 bulk_urb
= search_dev_ep (s
, urb
);
1485 queue_dbg("found bulk urb %p\n",bulk_urb
);
1487 if ((usb_pipetype (urb
->pipe
) != PIPE_BULK
) ||
1488 ((usb_pipetype (urb
->pipe
) == PIPE_BULK
) &&
1489 (!(urb
->transfer_flags
& USB_QUEUE_BULK
) || !(bulk_urb
->transfer_flags
& USB_QUEUE_BULK
)))) {
1490 spin_unlock_irqrestore (&s
->urb_list_lock
, flags
);
1491 usb_dec_dev_use (urb
->dev
);
1492 err("ENXIO1 %08x, flags %x, urb %p, burb %p",urb
->pipe
,urb
->transfer_flags
,urb
,bulk_urb
);
1493 return -ENXIO
; // urb already queued
1498 urb_priv
= kmem_cache_alloc(urb_priv_kmem
, SLAB_FLAG
);
1500 urb_priv
= kmalloc (sizeof (urb_priv_t
), KMALLOC_FLAG
);
1503 usb_dec_dev_use (urb
->dev
);
1504 spin_unlock_irqrestore (&s
->urb_list_lock
, flags
);
1508 urb
->hcpriv
= urb_priv
;
1509 INIT_LIST_HEAD (&urb_priv
->desc_list
);
1510 urb_priv
->short_control_packet
= 0;
1511 dbg("submit_urb: scheduling %p", urb
);
1512 urb_priv
->next_queued_urb
= NULL
;
1513 urb_priv
->prev_queued_urb
= NULL
;
1514 urb_priv
->bottom_qh
= NULL
;
1515 urb_priv
->next_qh
= NULL
;
1517 if (usb_pipetype (urb
->pipe
) == PIPE_BULK
) {
1520 while (((urb_priv_t
*)bulk_urb
->hcpriv
)->next_queued_urb
) // find last queued bulk
1521 bulk_urb
=((urb_priv_t
*)bulk_urb
->hcpriv
)->next_queued_urb
;
1523 ((urb_priv_t
*)bulk_urb
->hcpriv
)->next_queued_urb
=urb
;
1525 atomic_inc (&s
->avoid_bulk
);
1526 ret
= uhci_submit_bulk_urb (urb
, bulk_urb
);
1527 atomic_dec (&s
->avoid_bulk
);
1528 spin_unlock_irqrestore (&s
->urb_list_lock
, flags
);
1531 spin_unlock_irqrestore (&s
->urb_list_lock
, flags
);
1532 switch (usb_pipetype (urb
->pipe
)) {
1533 case PIPE_ISOCHRONOUS
:
1534 ret
= uhci_submit_iso_urb (urb
);
1536 case PIPE_INTERRUPT
:
1537 ret
= uhci_submit_int_urb (urb
);
1540 ret
= uhci_submit_control_urb (urb
);
1547 dbg("submit_urb: scheduled with ret: %d", ret
);
1550 usb_dec_dev_use (urb
->dev
);
1552 kmem_cache_free(urb_priv_kmem
, urb_priv
);
1562 // Checks for URB timeout and removes bandwidth reclamation
1563 // if URB idles too long
1564 _static
void uhci_check_timeouts(uhci_t
*s
)
1566 struct list_head
*p
,*p2
;
1570 p
= s
->urb_list
.prev
;
1572 while (p
!= &s
->urb_list
) {
1577 urb
= list_entry (p2
, urb_t
, urb_list
);
1578 type
= usb_pipetype (urb
->pipe
);
1580 hcpriv
= (urb_priv_t
*)urb
->hcpriv
;
1582 if ( urb
->timeout
&&
1583 ((hcpriv
->started
+ urb
->timeout
) < jiffies
)) {
1584 urb
->transfer_flags
|= USB_TIMEOUT_KILLED
| USB_ASYNC_UNLINK
;
1585 async_dbg("uhci_check_timeout: timeout for %p",urb
);
1586 uhci_unlink_urb_async(s
, urb
);
1588 #ifdef CONFIG_USB_UHCI_HIGH_BANDWIDTH
1589 else if (((type
== PIPE_BULK
) || (type
== PIPE_CONTROL
)) &&
1590 (hcpriv
->use_loop
) &&
1591 ((hcpriv
->started
+ IDLE_TIMEOUT
) < jiffies
))
1592 disable_desc_loop(s
, urb
);
1598 /*-------------------------------------------------------------------
1600 -------------------------------------------------------------------*/
1602 _static __u8 root_hub_dev_des
[] =
1604 0x12, /* __u8 bLength; */
1605 0x01, /* __u8 bDescriptorType; Device */
1606 0x00, /* __u16 bcdUSB; v1.0 */
1608 0x09, /* __u8 bDeviceClass; HUB_CLASSCODE */
1609 0x00, /* __u8 bDeviceSubClass; */
1610 0x00, /* __u8 bDeviceProtocol; */
1611 0x08, /* __u8 bMaxPacketSize0; 8 Bytes */
1612 0x00, /* __u16 idVendor; */
1614 0x00, /* __u16 idProduct; */
1616 0x00, /* __u16 bcdDevice; */
1618 0x00, /* __u8 iManufacturer; */
1619 0x00, /* __u8 iProduct; */
1620 0x00, /* __u8 iSerialNumber; */
1621 0x01 /* __u8 bNumConfigurations; */
1625 /* Configuration descriptor */
1626 _static __u8 root_hub_config_des
[] =
1628 0x09, /* __u8 bLength; */
1629 0x02, /* __u8 bDescriptorType; Configuration */
1630 0x19, /* __u16 wTotalLength; */
1632 0x01, /* __u8 bNumInterfaces; */
1633 0x01, /* __u8 bConfigurationValue; */
1634 0x00, /* __u8 iConfiguration; */
1635 0x40, /* __u8 bmAttributes;
1636 Bit 7: Bus-powered, 6: Self-powered, 5 Remote-wakwup, 4..0: resvd */
1637 0x00, /* __u8 MaxPower; */
1640 0x09, /* __u8 if_bLength; */
1641 0x04, /* __u8 if_bDescriptorType; Interface */
1642 0x00, /* __u8 if_bInterfaceNumber; */
1643 0x00, /* __u8 if_bAlternateSetting; */
1644 0x01, /* __u8 if_bNumEndpoints; */
1645 0x09, /* __u8 if_bInterfaceClass; HUB_CLASSCODE */
1646 0x00, /* __u8 if_bInterfaceSubClass; */
1647 0x00, /* __u8 if_bInterfaceProtocol; */
1648 0x00, /* __u8 if_iInterface; */
1651 0x07, /* __u8 ep_bLength; */
1652 0x05, /* __u8 ep_bDescriptorType; Endpoint */
1653 0x81, /* __u8 ep_bEndpointAddress; IN Endpoint 1 */
1654 0x03, /* __u8 ep_bmAttributes; Interrupt */
1655 0x08, /* __u16 ep_wMaxPacketSize; 8 Bytes */
1657 0xff /* __u8 ep_bInterval; 255 ms */
1661 _static __u8 root_hub_hub_des
[] =
1663 0x09, /* __u8 bLength; */
1664 0x29, /* __u8 bDescriptorType; Hub-descriptor */
1665 0x02, /* __u8 bNbrPorts; */
1666 0x00, /* __u16 wHubCharacteristics; */
1668 0x01, /* __u8 bPwrOn2pwrGood; 2ms */
1669 0x00, /* __u8 bHubContrCurrent; 0 mA */
1670 0x00, /* __u8 DeviceRemovable; *** 7 Ports max *** */
1671 0xff /* __u8 PortPwrCtrlMask; *** 7 ports max *** */
1674 /*-------------------------------------------------------------------------*/
1675 /* prepare Interrupt pipe transaction data; HUB INTERRUPT ENDPOINT */
1676 _static
int rh_send_irq (urb_t
*urb
)
1680 uhci_t
*uhci
= urb
->dev
->bus
->hcpriv
;
1681 unsigned int io_addr
= uhci
->io_addr
;
1684 for (i
= 0; i
< uhci
->rh
.numports
; i
++) {
1685 data
|= ((inw (io_addr
+ USBPORTSC1
+ i
* 2) & 0xa) > 0 ? (1 << (i
+ 1)) : 0);
1686 len
= (i
+ 1) / 8 + 1;
1689 *(__u16
*) urb
->transfer_buffer
= cpu_to_le16 (data
);
1690 urb
->actual_length
= len
;
1693 if ((data
> 0) && (uhci
->rh
.send
!= 0)) {
1694 dbg("Root-Hub INT complete: port1: %x port2: %x data: %x",
1695 inw (io_addr
+ USBPORTSC1
), inw (io_addr
+ USBPORTSC2
), data
);
1696 urb
->complete (urb
);
1701 /*-------------------------------------------------------------------------*/
1702 /* Virtual Root Hub INTs are polled by this timer every "intervall" ms */
1703 _static
int rh_init_int_timer (urb_t
*urb
);
1705 _static
void rh_int_timer_do (unsigned long ptr
)
1708 urb_t
*urb
= (urb_t
*) ptr
;
1709 uhci_t
*uhci
= urb
->dev
->bus
->hcpriv
;
1711 if (uhci
->rh
.send
) {
1712 len
= rh_send_irq (urb
);
1714 urb
->actual_length
= len
;
1716 urb
->complete (urb
);
1719 rh_init_int_timer (urb
);
1722 /*-------------------------------------------------------------------------*/
1723 /* Root Hub INTs are polled by this timer, polling interval 20ms */
1724 /* This time is also used for URB-timeout checking */
1726 _static
int rh_init_int_timer (urb_t
*urb
)
1728 uhci_t
*uhci
= urb
->dev
->bus
->hcpriv
;
1730 uhci
->rh
.interval
= urb
->interval
;
1731 init_timer (&uhci
->rh
.rh_int_timer
);
1732 uhci
->rh
.rh_int_timer
.function
= rh_int_timer_do
;
1733 uhci
->rh
.rh_int_timer
.data
= (unsigned long) urb
;
1734 uhci
->rh
.rh_int_timer
.expires
= jiffies
+ (HZ
* 20) / 1000;
1735 add_timer (&uhci
->rh
.rh_int_timer
);
1740 /*-------------------------------------------------------------------------*/
1741 #define OK(x) len = (x); break
1743 #define CLR_RH_PORTSTAT(x) \
1744 status = inw(io_addr+USBPORTSC1+2*(wIndex-1)); \
1745 status = (status & 0xfff5) & ~(x); \
1746 outw(status, io_addr+USBPORTSC1+2*(wIndex-1))
1748 #define SET_RH_PORTSTAT(x) \
1749 status = inw(io_addr+USBPORTSC1+2*(wIndex-1)); \
1750 status = (status & 0xfff5) | (x); \
1751 outw(status, io_addr+USBPORTSC1+2*(wIndex-1))
1754 /*-------------------------------------------------------------------------*/
1756 ** Root Hub Control Pipe
1757 *************************/
1760 _static
int rh_submit_urb (urb_t
*urb
)
1762 struct usb_device
*usb_dev
= urb
->dev
;
1763 uhci_t
*uhci
= usb_dev
->bus
->hcpriv
;
1764 unsigned int pipe
= urb
->pipe
;
1765 devrequest
*cmd
= (devrequest
*) urb
->setup_packet
;
1766 void *data
= urb
->transfer_buffer
;
1767 int leni
= urb
->transfer_buffer_length
;
1772 unsigned int io_addr
= uhci
->io_addr
;
1780 if (usb_pipetype (pipe
) == PIPE_INTERRUPT
) {
1781 dbg("Root-Hub submit IRQ: every %d ms", urb
->interval
);
1784 uhci
->rh
.interval
= urb
->interval
;
1785 rh_init_int_timer (urb
);
1791 bmRType_bReq
= cmd
->requesttype
| cmd
->request
<< 8;
1792 wValue
= le16_to_cpu (cmd
->value
);
1793 wIndex
= le16_to_cpu (cmd
->index
);
1794 wLength
= le16_to_cpu (cmd
->length
);
1796 for (i
= 0; i
< 8; i
++)
1797 uhci
->rh
.c_p_r
[i
] = 0;
1799 dbg("Root-Hub: adr: %2x cmd(%1x): %04x %04x %04x %04x",
1800 uhci
->rh
.devnum
, 8, bmRType_bReq
, wValue
, wIndex
, wLength
);
1802 switch (bmRType_bReq
) {
1803 /* Request Destination:
1804 without flags: Device,
1805 RH_INTERFACE: interface,
1806 RH_ENDPOINT: endpoint,
1807 RH_CLASS means HUB here,
1808 RH_OTHER | RH_CLASS almost ever means HUB_PORT here
1812 *(__u16
*) data
= cpu_to_le16 (1);
1814 case RH_GET_STATUS
| RH_INTERFACE
:
1815 *(__u16
*) data
= cpu_to_le16 (0);
1817 case RH_GET_STATUS
| RH_ENDPOINT
:
1818 *(__u16
*) data
= cpu_to_le16 (0);
1820 case RH_GET_STATUS
| RH_CLASS
:
1821 *(__u32
*) data
= cpu_to_le32 (0);
1822 OK (4); /* hub power ** */
1823 case RH_GET_STATUS
| RH_OTHER
| RH_CLASS
:
1824 status
= inw (io_addr
+ USBPORTSC1
+ 2 * (wIndex
- 1));
1825 cstatus
= ((status
& USBPORTSC_CSC
) >> (1 - 0)) |
1826 ((status
& USBPORTSC_PEC
) >> (3 - 1)) |
1827 (uhci
->rh
.c_p_r
[wIndex
- 1] << (0 + 4));
1828 status
= (status
& USBPORTSC_CCS
) |
1829 ((status
& USBPORTSC_PE
) >> (2 - 1)) |
1830 ((status
& USBPORTSC_SUSP
) >> (12 - 2)) |
1831 ((status
& USBPORTSC_PR
) >> (9 - 4)) |
1832 (1 << 8) | /* power on ** */
1833 ((status
& USBPORTSC_LSDA
) << (-8 + 9));
1835 *(__u16
*) data
= cpu_to_le16 (status
);
1836 *(__u16
*) (data
+ 2) = cpu_to_le16 (cstatus
);
1839 case RH_CLEAR_FEATURE
| RH_ENDPOINT
:
1841 case (RH_ENDPOINT_STALL
):
1846 case RH_CLEAR_FEATURE
| RH_CLASS
:
1848 case (RH_C_HUB_OVER_CURRENT
):
1849 OK (0); /* hub power over current ** */
1853 case RH_CLEAR_FEATURE
| RH_OTHER
| RH_CLASS
:
1855 case (RH_PORT_ENABLE
):
1856 CLR_RH_PORTSTAT (USBPORTSC_PE
);
1858 case (RH_PORT_SUSPEND
):
1859 CLR_RH_PORTSTAT (USBPORTSC_SUSP
);
1861 case (RH_PORT_POWER
):
1862 OK (0); /* port power ** */
1863 case (RH_C_PORT_CONNECTION
):
1864 SET_RH_PORTSTAT (USBPORTSC_CSC
);
1866 case (RH_C_PORT_ENABLE
):
1867 SET_RH_PORTSTAT (USBPORTSC_PEC
);
1869 case (RH_C_PORT_SUSPEND
):
1870 /*** WR_RH_PORTSTAT(RH_PS_PSSC); */
1872 case (RH_C_PORT_OVER_CURRENT
):
1873 OK (0); /* port power over current ** */
1874 case (RH_C_PORT_RESET
):
1875 uhci
->rh
.c_p_r
[wIndex
- 1] = 0;
1880 case RH_SET_FEATURE
| RH_OTHER
| RH_CLASS
:
1882 case (RH_PORT_SUSPEND
):
1883 SET_RH_PORTSTAT (USBPORTSC_SUSP
);
1885 case (RH_PORT_RESET
):
1886 SET_RH_PORTSTAT (USBPORTSC_PR
);
1888 uhci
->rh
.c_p_r
[wIndex
- 1] = 1;
1889 CLR_RH_PORTSTAT (USBPORTSC_PR
);
1891 SET_RH_PORTSTAT (USBPORTSC_PE
);
1893 SET_RH_PORTSTAT (0xa);
1895 case (RH_PORT_POWER
):
1896 OK (0); /* port power ** */
1897 case (RH_PORT_ENABLE
):
1898 SET_RH_PORTSTAT (USBPORTSC_PE
);
1903 case RH_SET_ADDRESS
:
1904 uhci
->rh
.devnum
= wValue
;
1907 case RH_GET_DESCRIPTOR
:
1908 switch ((wValue
& 0xff00) >> 8) {
1909 case (0x01): /* device descriptor */
1910 len
= min (leni
, min (sizeof (root_hub_dev_des
), wLength
));
1911 memcpy (data
, root_hub_dev_des
, len
);
1913 case (0x02): /* configuration descriptor */
1914 len
= min (leni
, min (sizeof (root_hub_config_des
), wLength
));
1915 memcpy (data
, root_hub_config_des
, len
);
1917 case (0x03): /*string descriptors */
1922 case RH_GET_DESCRIPTOR
| RH_CLASS
:
1923 root_hub_hub_des
[2] = uhci
->rh
.numports
;
1924 len
= min (leni
, min (sizeof (root_hub_hub_des
), wLength
));
1925 memcpy (data
, root_hub_hub_des
, len
);
1928 case RH_GET_CONFIGURATION
:
1929 *(__u8
*) data
= 0x01;
1932 case RH_SET_CONFIGURATION
:
1938 dbg("Root-Hub stat port1: %x port2: %x",
1939 inw (io_addr
+ USBPORTSC1
), inw (io_addr
+ USBPORTSC2
));
1941 urb
->actual_length
= len
;
1944 urb
->complete (urb
);
1947 /*-------------------------------------------------------------------------*/
1949 _static
int rh_unlink_urb (urb_t
*urb
)
1951 uhci_t
*uhci
= urb
->dev
->bus
->hcpriv
;
1953 if (uhci
->rh
.urb
==urb
) {
1954 dbg("Root-Hub unlink IRQ");
1956 del_timer (&uhci
->rh
.rh_int_timer
);
1960 /*-------------------------------------------------------------------*/
1963 * Map status to standard result codes
1965 * <status> is (td->status & 0xFE0000) [a.k.a. uhci_status_bits(td->status)
1966 * <dir_out> is True for output TDs and False for input TDs.
1968 _static
int uhci_map_status (int status
, int dir_out
)
1972 if (status
& TD_CTRL_BITSTUFF
) /* Bitstuff error */
1974 if (status
& TD_CTRL_CRCTIMEO
) { /* CRC/Timeout */
1980 if (status
& TD_CTRL_NAK
) /* NAK */
1982 if (status
& TD_CTRL_BABBLE
) /* Babble */
1984 if (status
& TD_CTRL_DBUFERR
) /* Buffer error */
1986 if (status
& TD_CTRL_STALLED
) /* Stalled */
1988 if (status
& TD_CTRL_ACTIVE
) /* Active */
1995 * Only the USB core should call uhci_alloc_dev and uhci_free_dev
1997 _static
int uhci_alloc_dev (struct usb_device
*usb_dev
)
2002 _static
void uhci_unlink_urbs(uhci_t
*s
, struct usb_device
*usb_dev
, int remove_all
)
2004 unsigned long flags
;
2005 struct list_head
*p
;
2006 struct list_head
*p2
;
2009 spin_lock_irqsave (&s
->urb_list_lock
, flags
);
2010 p
= s
->urb_list
.prev
;
2011 while (p
!= &s
->urb_list
) {
2014 urb
= list_entry (p2
, urb_t
, urb_list
);
2015 dbg("urb: %p, dev %p, %p", urb
, usb_dev
,urb
->dev
);
2017 //urb->transfer_flags |=USB_ASYNC_UNLINK;
2019 if (remove_all
|| (usb_dev
== urb
->dev
)) {
2020 spin_unlock_irqrestore (&s
->urb_list_lock
, flags
);
2021 warn("forced removing of queued URB %p due to disconnect",urb
);
2022 uhci_unlink_urb(urb
);
2023 urb
->dev
= NULL
; // avoid further processing of this UR
2024 spin_lock_irqsave (&s
->urb_list_lock
, flags
);
2025 p
= s
->urb_list
.prev
;
2028 spin_unlock_irqrestore (&s
->urb_list_lock
, flags
);
2031 _static
int uhci_free_dev (struct usb_device
*usb_dev
)
2036 if(!usb_dev
|| !usb_dev
->bus
|| !usb_dev
->bus
->hcpriv
)
2039 s
=(uhci_t
*) usb_dev
->bus
->hcpriv
;
2040 uhci_unlink_urbs(s
, usb_dev
, 0);
2046 * uhci_get_current_frame_number()
2048 * returns the current frame number for a USB bus/controller.
2050 _static
int uhci_get_current_frame_number (struct usb_device
*usb_dev
)
2052 return UHCI_GET_CURRENT_FRAME ((uhci_t
*) usb_dev
->bus
->hcpriv
);
2055 struct usb_operations uhci_device_operations
=
2059 uhci_get_current_frame_number
,
2065 * For IN-control transfers, process_transfer gets a bit more complicated,
2066 * since there are devices that return less data (eg. strings) than they
2067 * have announced. This leads to a queue abort due to the short packet,
2068 * the status stage is not executed. If this happens, the status stage
2069 * is manually re-executed.
2070 * mode: 0: QHs already unlinked
2073 _static
int process_transfer (uhci_t
*s
, urb_t
*urb
, int mode
)
2076 urb_priv_t
*urb_priv
= urb
->hcpriv
;
2077 struct list_head
*qhl
= urb_priv
->desc_list
.next
;
2078 uhci_desc_t
*qh
= list_entry (qhl
, uhci_desc_t
, desc_list
);
2079 struct list_head
*p
= qh
->vertical
.next
;
2080 uhci_desc_t
*desc
= list_entry (urb_priv
->desc_list
.prev
, uhci_desc_t
, desc_list
);
2081 uhci_desc_t
*last_desc
= list_entry (desc
->vertical
.prev
, uhci_desc_t
, vertical
);
2082 int data_toggle
= usb_gettoggle (urb
->dev
, usb_pipeendpoint (urb
->pipe
), usb_pipeout (urb
->pipe
)); // save initial data_toggle
2083 int maxlength
; // extracted and remapped info from TD
2087 //dbg("process_transfer: urb contains bulk/control request");
2089 /* if the status phase has been retriggered and the
2090 queue is empty or the last status-TD is inactive, the retriggered
2091 status stage is completed
2094 if (urb_priv
->short_control_packet
&&
2095 ((qh
->hw
.qh
.element
== UHCI_PTR_TERM
) ||(!(last_desc
->hw
.td
.status
& TD_CTRL_ACTIVE
))))
2096 goto transfer_finished
;
2098 urb
->actual_length
=0;
2100 for (; p
!= &qh
->vertical
; p
= p
->next
) {
2101 desc
= list_entry (p
, uhci_desc_t
, vertical
);
2103 if (desc
->hw
.td
.status
& TD_CTRL_ACTIVE
) // do not process active TDs
2106 actual_length
= (desc
->hw
.td
.status
+ 1) & 0x7ff; // extract transfer parameters from TD
2107 maxlength
= (((desc
->hw
.td
.info
>> 21) & 0x7ff) + 1) & 0x7ff;
2108 status
= uhci_map_status (uhci_status_bits (desc
->hw
.td
.status
), usb_pipeout (urb
->pipe
));
2110 if (status
== -EPIPE
) { // see if EP is stalled
2111 // set up stalled condition
2112 usb_endpoint_halt (urb
->dev
, usb_pipeendpoint (urb
->pipe
), usb_pipeout (urb
->pipe
));
2115 if (status
!= 0) { // if any error occured stop processing of further TDs
2116 // only set ret if status returned an error
2117 if (status
!= -EPIPE
)
2118 uhci_show_td (desc
);
2123 else if ((desc
->hw
.td
.info
& 0xff) != USB_PID_SETUP
)
2124 urb
->actual_length
+= actual_length
;
2126 // got less data than requested
2127 if ( (actual_length
< maxlength
)) {
2128 if (urb
->transfer_flags
& USB_DISABLE_SPD
) {
2129 status
= -EREMOTEIO
; // treat as real error
2130 dbg("process_transfer: SPD!!");
2131 break; // exit after this TD because SP was detected
2134 // short read during control-IN: re-start status stage
2135 if ((usb_pipetype (urb
->pipe
) == PIPE_CONTROL
)) {
2136 if (uhci_packetid(last_desc
->hw
.td
.info
) == USB_PID_OUT
) {
2138 qh
->hw
.qh
.element
= virt_to_bus (last_desc
); // re-trigger status stage
2139 dbg("short packet during control transfer, retrigger status stage @ %p",last_desc
);
2140 //uhci_show_td (desc);
2141 //uhci_show_td (last_desc);
2142 urb_priv
->short_control_packet
=1;
2146 // all other cases: short read is OK
2147 data_toggle
= uhci_toggle (desc
->hw
.td
.info
);
2151 data_toggle
= uhci_toggle (desc
->hw
.td
.info
);
2152 queue_dbg("process_transfer: len:%d status:%x mapped:%x toggle:%d", actual_length
, desc
->hw
.td
.status
,status
, data_toggle
);
2156 usb_settoggle (urb
->dev
, usb_pipeendpoint (urb
->pipe
), usb_pipeout (urb
->pipe
), !data_toggle
);
2160 uhci_clean_transfer(s
, urb
, qh
, (mode
==0?2:1));
2162 urb
->status
= status
;
2164 #ifdef CONFIG_USB_UHCI_HIGH_BANDWIDTH
2165 disable_desc_loop(s
,urb
);
2168 queue_dbg("process_transfer: (end) urb %p, wanted len %d, len %d status %x err %d",
2169 urb
,urb
->transfer_buffer_length
,urb
->actual_length
, urb
->status
, urb
->error_count
);
2173 _static
int process_interrupt (uhci_t
*s
, urb_t
*urb
)
2175 int i
, ret
= -EINPROGRESS
;
2176 urb_priv_t
*urb_priv
= urb
->hcpriv
;
2177 struct list_head
*p
= urb_priv
->desc_list
.next
;
2178 uhci_desc_t
*desc
= list_entry (urb_priv
->desc_list
.prev
, uhci_desc_t
, desc_list
);
2183 //dbg("urb contains interrupt request");
2185 for (i
= 0; p
!= &urb_priv
->desc_list
; p
= p
->next
, i
++) // Maybe we allow more than one TD later ;-)
2187 desc
= list_entry (p
, uhci_desc_t
, desc_list
);
2189 if (desc
->hw
.td
.status
& TD_CTRL_ACTIVE
) {
2190 // do not process active TDs
2191 //dbg("TD ACT Status @%p %08x",desc,desc->hw.td.status);
2195 if (!desc
->hw
.td
.status
& TD_CTRL_IOC
) {
2196 // do not process one-shot TDs, no recycling
2199 // extract transfer parameters from TD
2201 actual_length
= (desc
->hw
.td
.status
+ 1) & 0x7ff;
2202 status
= uhci_map_status (uhci_status_bits (desc
->hw
.td
.status
), usb_pipeout (urb
->pipe
));
2204 // see if EP is stalled
2205 if (status
== -EPIPE
) {
2206 // set up stalled condition
2207 usb_endpoint_halt (urb
->dev
, usb_pipeendpoint (urb
->pipe
), usb_pipeout (urb
->pipe
));
2210 // if any error occured: ignore this td, and continue
2212 //uhci_show_td (desc);
2217 urb
->actual_length
= actual_length
;
2220 if (urb
->complete
) {
2221 //dbg("process_interrupt: calling completion, status %i",status);
2222 urb
->status
= status
;
2224 spin_unlock(&s
->urb_list_lock
);
2226 urb
->complete ((struct urb
*) urb
);
2228 spin_lock(&s
->urb_list_lock
);
2230 urb
->status
= -EINPROGRESS
;
2233 // Recycle INT-TD if interval!=0, else mark TD as one-shot
2234 if (urb
->interval
) {
2236 desc
->hw
.td
.info
&= ~(1 << TD_TOKEN_TOGGLE
);
2238 ((urb_priv_t
*)urb
->hcpriv
)->started
=jiffies
;
2239 desc
->hw
.td
.info
|= (usb_gettoggle (urb
->dev
, usb_pipeendpoint (urb
->pipe
),
2240 usb_pipeout (urb
->pipe
)) << TD_TOKEN_TOGGLE
);
2241 usb_dotoggle (urb
->dev
, usb_pipeendpoint (urb
->pipe
), usb_pipeout (urb
->pipe
));
2243 desc
->hw
.td
.info
|= (!usb_gettoggle (urb
->dev
, usb_pipeendpoint (urb
->pipe
),
2244 usb_pipeout (urb
->pipe
)) << TD_TOKEN_TOGGLE
);
2246 desc
->hw
.td
.status
= (urb
->pipe
& TD_CTRL_LS
) | TD_CTRL_ACTIVE
| TD_CTRL_IOC
|
2247 (urb
->transfer_flags
& USB_DISABLE_SPD
? 0 : TD_CTRL_SPD
) | (3 << 27);
2251 desc
->hw
.td
.status
&= ~TD_CTRL_IOC
; // inactivate TD
2258 // mode: 1: force processing, don't unlink tds (already unlinked)
2259 _static
int process_iso (uhci_t
*s
, urb_t
*urb
, int mode
)
2263 urb_priv_t
*urb_priv
= urb
->hcpriv
;
2264 struct list_head
*p
= urb_priv
->desc_list
.next
;
2265 uhci_desc_t
*desc
= list_entry (urb_priv
->desc_list
.prev
, uhci_desc_t
, desc_list
);
2267 dbg("urb contains iso request");
2268 if ((desc
->hw
.td
.status
& TD_CTRL_ACTIVE
) && !mode
)
2269 return -EXDEV
; // last TD not finished
2271 urb
->error_count
= 0;
2272 urb
->actual_length
= 0;
2274 dbg("process iso urb %p, %li, %i, %i, %i %08x",urb
,jiffies
,UHCI_GET_CURRENT_FRAME(s
),
2275 urb
->number_of_packets
,mode
,desc
->hw
.td
.status
);
2277 for (i
= 0; p
!= &urb_priv
->desc_list
; p
= p
->next
, i
++) {
2278 desc
= list_entry (p
, uhci_desc_t
, desc_list
);
2280 //uhci_show_td(desc);
2281 if (desc
->hw
.td
.status
& TD_CTRL_ACTIVE
) {
2282 // means we have completed the last TD, but not the TDs before
2283 desc
->hw
.td
.status
&= ~TD_CTRL_ACTIVE
;
2284 dbg("TD still active (%x)- grrr. paranoia!", desc
->hw
.td
.status
);
2286 urb
->iso_frame_desc
[i
].status
= ret
;
2287 unlink_td (s
, desc
, 1);
2288 // FIXME: immediate deletion may be dangerous
2293 unlink_td (s
, desc
, 1);
2295 if (urb
->number_of_packets
<= i
) {
2296 dbg("urb->number_of_packets (%d)<=(%d)", urb
->number_of_packets
, i
);
2301 if (urb
->iso_frame_desc
[i
].offset
+ urb
->transfer_buffer
!= bus_to_virt (desc
->hw
.td
.buffer
)) {
2302 // Hm, something really weird is going on
2303 dbg("Pointer Paranoia: %p!=%p", urb
->iso_frame_desc
[i
].offset
+ urb
->transfer_buffer
, bus_to_virt (desc
->hw
.td
.buffer
));
2305 urb
->iso_frame_desc
[i
].status
= ret
;
2308 urb
->iso_frame_desc
[i
].actual_length
= (desc
->hw
.td
.status
+ 1) & 0x7ff;
2309 urb
->iso_frame_desc
[i
].status
= uhci_map_status (uhci_status_bits (desc
->hw
.td
.status
), usb_pipeout (urb
->pipe
));
2310 urb
->actual_length
+= urb
->iso_frame_desc
[i
].actual_length
;
2314 if (urb
->iso_frame_desc
[i
].status
!= 0) {
2316 urb
->status
= urb
->iso_frame_desc
[i
].status
;
2318 dbg("process_iso: %i: len:%d %08x status:%x",
2319 i
, urb
->iso_frame_desc
[i
].actual_length
, desc
->hw
.td
.status
,urb
->iso_frame_desc
[i
].status
);
2325 dbg("process_iso: exit %i (%d), actual_len %i", i
, ret
,urb
->actual_length
);
2330 _static
int process_urb (uhci_t
*s
, struct list_head
*p
)
2336 urb
=list_entry (p
, urb_t
, urb_list
);
2337 //dbg("process_urb: found queued urb: %p", urb);
2339 switch (usb_pipetype (urb
->pipe
)) {
2341 ret
= process_transfer (s
, urb
, 1);
2344 if (!s
->avoid_bulk
.counter
)
2345 ret
= process_transfer (s
, urb
, 1);
2349 case PIPE_ISOCHRONOUS
:
2350 ret
= process_iso (s
, urb
, 0);
2352 case PIPE_INTERRUPT
:
2353 ret
= process_interrupt (s
, urb
);
2357 if (urb
->status
!= -EINPROGRESS
) {
2360 dbg("dequeued urb: %p", urb
);
2361 dequeue_urb (s
, urb
);
2364 kmem_cache_free(urb_priv_kmem
, urb
->hcpriv
);
2366 kfree (urb
->hcpriv
);
2369 if ((usb_pipetype (urb
->pipe
) != PIPE_INTERRUPT
)) {
2370 urb_t
*tmp
= urb
->next
; // pointer to first urb
2375 if (tmp
->status
!= -EINPROGRESS
) {
2381 while (tmp
!= NULL
&& tmp
!= urb
->next
);
2382 if (tmp
== urb
->next
)
2386 spin_unlock(&s
->urb_list_lock
);
2388 // In case you need the current URB status for your completion handler
2389 if (urb
->complete
&& (!proceed
|| (urb
->transfer_flags
& USB_URB_EARLY_COMPLETE
))) {
2390 dbg("process_transfer: calling early completion");
2391 urb
->complete ((struct urb
*) urb
);
2392 if (!proceed
&& is_ring
&& (urb
->status
!= -ENOENT
))
2393 uhci_submit_urb (urb
);
2396 if (proceed
&& urb
->next
) {
2397 // if there are linked urbs - handle submitting of them right now.
2398 tmp
= urb
->next
; // pointer to first urb
2401 if ((tmp
->status
!= -EINPROGRESS
) && (tmp
->status
!= -ENOENT
) && uhci_submit_urb (tmp
) != 0)
2405 while (tmp
!= NULL
&& tmp
!= urb
->next
); // submit until we reach NULL or our own pointer or submit fails
2407 if (urb
->complete
&& !(urb
->transfer_flags
& USB_URB_EARLY_COMPLETE
)) {
2408 dbg("process_transfer: calling completion");
2409 urb
->complete ((struct urb
*) urb
);
2413 spin_lock(&s
->urb_list_lock
);
2415 usb_dec_dev_use (urb
->dev
);
2422 _static
void uhci_interrupt (int irq
, void *__uhci
, struct pt_regs
*regs
)
2425 unsigned int io_addr
= s
->io_addr
;
2426 unsigned short status
;
2427 struct list_head
*p
, *p2
;
2430 * Read the interrupt status, and write it back to clear the
2434 status
= inw (io_addr
+ USBSTS
);
2436 if (!status
) /* shared interrupt, not mine */
2442 warn("interrupt, status %x, frame# %i", status
,
2443 UHCI_GET_CURRENT_FRAME(s
));
2445 // remove host controller halted state
2446 if ((status
&0x20) && (s
->running
)) {
2447 outw (USBCMD_RS
| inw(io_addr
+ USBCMD
), io_addr
+ USBCMD
);
2449 //uhci_show_status (s);
2452 * traverse the list in *reverse* direction, because new entries
2453 * may be added at the end.
2454 * also, because process_urb may unlink the current urb,
2455 * we need to advance the list before
2458 spin_lock (&s
->urb_list_lock
);
2460 s
->unlink_urb_done
=0;
2461 p
= s
->urb_list
.prev
;
2463 while (p
!= &s
->urb_list
) {
2466 process_urb (s
, p2
);
2467 if (s
->unlink_urb_done
) {
2468 s
->unlink_urb_done
=0;
2472 if ((s
->frame_counter
& 63) == 0)
2473 uhci_check_timeouts(s
);
2476 uhci_cleanup_unlink(s
, 0);
2478 spin_unlock (&s
->urb_list_lock
);
2481 outw (status
, io_addr
+ USBSTS
);
2483 //dbg("uhci_interrupt: done");
2486 _static
void reset_hc (uhci_t
*s
)
2488 unsigned int io_addr
= s
->io_addr
;
2491 /* Global reset for 50ms */
2492 outw (USBCMD_GRESET
, io_addr
+ USBCMD
);
2494 outw (0, io_addr
+ USBCMD
);
2498 _static
void start_hc (uhci_t
*s
)
2500 unsigned int io_addr
= s
->io_addr
;
2504 * Reset the HC - this will force us to get a
2505 * new notification of any already connected
2506 * ports due to the virtual disconnect that it
2509 outw (USBCMD_HCRESET
, io_addr
+ USBCMD
);
2511 while (inw (io_addr
+ USBCMD
) & USBCMD_HCRESET
) {
2513 err("USBCMD_HCRESET timed out!");
2518 /* Turn on all interrupts */
2519 outw (USBINTR_TIMEOUT
| USBINTR_RESUME
| USBINTR_IOC
| USBINTR_SP
, io_addr
+ USBINTR
);
2521 /* Start at frame 0 */
2522 outw (0, io_addr
+ USBFRNUM
);
2523 outl (virt_to_bus (s
->framelist
), io_addr
+ USBFLBASEADD
);
2525 /* Run and mark it configured with a 64-byte max packet */
2526 outw (USBCMD_RS
| USBCMD_CF
| USBCMD_MAXP
, io_addr
+ USBCMD
);
2531 _static
void __exit
uhci_cleanup_dev(uhci_t
*s
)
2533 struct usb_device
*root_hub
= s
->bus
->root_hub
;
2535 s
->running
= 0; // Don't allow submit_urb
2538 usb_disconnect (&root_hub
);
2543 uhci_unlink_urbs (s
, 0, 1); // Forced unlink of remaining URBs
2544 uhci_cleanup_unlink (s
, 1); // force cleanup of async killed URBs
2546 usb_deregister_bus (s
->bus
);
2548 release_region (s
->io_addr
, s
->io_size
);
2549 free_irq (s
->irq
, s
);
2550 usb_free_bus (s
->bus
);
2555 _static
int __init
uhci_start_usb (uhci_t
*s
)
2557 /* connect the virtual root hub */
2558 struct usb_device
*usb_dev
;
2560 usb_dev
= usb_alloc_dev (NULL
, s
->bus
);
2564 s
->bus
->root_hub
= usb_dev
;
2565 usb_connect (usb_dev
);
2567 if (usb_new_device (usb_dev
) != 0) {
2568 usb_free_dev (usb_dev
);
2575 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,3,44)
2576 _static
int handle_pm_event (struct pm_dev
*dev
, pm_request_t rqst
, void *data
)
2578 uhci_t
*s
= (uhci_t
*) dev
->data
;
2579 dbg("handle_apm_event(%d)", rqst
);
2594 _static
int __init
alloc_uhci (struct pci_dev
*dev
, int irq
, unsigned int io_addr
, unsigned int io_size
)
2597 struct usb_bus
*bus
;
2598 struct pm_dev
*pmdev
;
2600 s
= kmalloc (sizeof (uhci_t
), GFP_KERNEL
);
2604 memset (s
, 0, sizeof (uhci_t
));
2605 INIT_LIST_HEAD (&s
->free_desc
);
2606 INIT_LIST_HEAD (&s
->urb_list
);
2607 INIT_LIST_HEAD (&s
->urb_unlinked
);
2608 spin_lock_init (&s
->urb_list_lock
);
2609 spin_lock_init (&s
->qh_lock
);
2610 spin_lock_init (&s
->td_lock
);
2611 atomic_set(&s
->avoid_bulk
, 0);
2613 s
->io_addr
= io_addr
;
2614 s
->io_size
= io_size
;
2615 s
->next
= devs
; //chain new uhci device into global list
2616 s
->frame_counter
= 0;
2618 bus
= usb_alloc_bus (&uhci_device_operations
);
2627 /* UHCI specs says devices must have 2 ports, but goes on to say */
2628 /* they may have more but give no way to determine how many they */
2629 /* have, so default to 2 */
2630 /* According to the UHCI spec, Bit 7 is always set to 1. So we try */
2631 /* to use this to our advantage */
2633 for (s
->maxports
= 0; s
->maxports
< (io_size
- 0x10) / 2; s
->maxports
++) {
2634 unsigned int portstatus
;
2636 portstatus
= inw (io_addr
+ 0x10 + (s
->maxports
* 2));
2637 dbg("port %i, adr %x status %x", s
->maxports
,
2638 io_addr
+ 0x10 + (s
->maxports
* 2), portstatus
);
2639 if (!(portstatus
& 0x0080))
2642 warn("Detected %d ports", s
->maxports
);
2644 /* This is experimental so anything less than 2 or greater than 8 is */
2645 /* something weird and we'll ignore it */
2646 if (s
->maxports
< 2 || s
->maxports
> 8) {
2647 dbg("Port count misdetected, forcing to 2 ports");
2651 s
->rh
.numports
= s
->maxports
;
2653 if (init_skel (s
)) {
2659 request_region (s
->io_addr
, io_size
, MODNAME
);
2661 usb_register_bus (s
->bus
);
2665 if (request_irq (irq
, uhci_interrupt
, SA_SHIRQ
, MODNAME
, s
)) {
2666 err("request_irq %d failed!",irq
);
2669 release_region (s
->io_addr
, s
->io_size
);
2677 if(uhci_start_usb (s
) < 0) {
2678 uhci_cleanup_dev(s
);
2682 //chain new uhci device into global list
2684 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,3,44)
2685 pmdev
= pm_register(PM_PCI_DEV
, PM_PCI_ID(dev
), handle_pm_event
);
2692 _static
int __init
start_uhci (struct pci_dev
*dev
)
2696 /* Search for the IO base address.. */
2697 for (i
= 0; i
< 6; i
++) {
2698 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,3,8)
2699 unsigned int io_addr
= dev
->resource
[i
].start
;
2700 unsigned int io_size
=
2701 dev
->resource
[i
].end
- dev
->resource
[i
].start
+ 1;
2702 if (!(dev
->resource
[i
].flags
& 1))
2705 unsigned int io_addr
= dev
->base_address
[i
];
2706 unsigned int io_size
= 0x14;
2712 /* Is it already in use? */
2713 if (check_region (io_addr
, io_size
))
2715 /* disable legacy emulation */
2716 pci_write_config_word (dev
, USBLEGSUP
, USBLEGSUP_DEFAULT
);
2717 if(dev
->vendor
==0x8086) {
2718 info("Intel USB controller: setting latency timer to %d", UHCI_LATENCY_TIMER
);
2719 pci_write_config_byte(dev
, PCI_LATENCY_TIMER
, UHCI_LATENCY_TIMER
);
2721 return alloc_uhci(dev
, dev
->irq
, io_addr
, io_size
);
2726 int __init
uhci_init (void)
2728 int retval
= -ENODEV
;
2729 struct pci_dev
*dev
= NULL
;
2735 uhci_desc_kmem
= kmem_cache_create("uhci_desc", sizeof(uhci_desc_t
), 0, SLAB_HWCACHE_ALIGN
, NULL
, NULL
);
2737 if(!uhci_desc_kmem
) {
2738 err("kmem_cache_create for uhci_desc failed (out of memory)");
2742 urb_priv_kmem
= kmem_cache_create("urb_priv", sizeof(urb_priv_t
), 0, SLAB_HWCACHE_ALIGN
, NULL
, NULL
);
2744 if(!urb_priv_kmem
) {
2745 err("kmem_cache_create for urb_priv_t failed (out of memory)");
2751 #ifdef CONFIG_USB_UHCI_HIGH_BANDWIDTH
2752 info("High bandwidth mode enabled");
2755 dev
= pci_find_class (PCI_CLASS_SERIAL_USB
<< 8, dev
);
2760 pci_read_config_byte (dev
, PCI_CLASS_PROG
, &type
);
2764 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,3,8)
2765 if (pci_enable_device (dev
) < 0)
2770 err("Found UHCI device with no IRQ assigned. Check BIOS settings!");
2775 retval
= start_uhci (dev
);
2784 void __exit
uhci_cleanup (void)
2787 while ((s
= devs
)) {
2789 uhci_cleanup_dev(s
);
2792 if(kmem_cache_destroy(uhci_desc_kmem
))
2793 err("uhci_desc_kmem remained");
2795 if(kmem_cache_destroy(urb_priv_kmem
))
2796 err("urb_priv_kmem remained");
2801 int init_module (void)
2803 return uhci_init ();
2806 void cleanup_module (void)
2808 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,3,44)
2809 pm_unregister_all (handle_pm_event
);