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 #include <linux/version.h>
32 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,3,44)
36 #include <asm/uaccess.h>
39 #include <asm/system.h>
41 /* This enables more detailed sanity checks in submit_iso */
42 //#define ISO_SANITY_CHECK
44 /* This enables debug printks */
47 /* This enables all symbols to be exported, to ease debugging oopses */
48 //#define DEBUG_SYMBOLS
50 /* This enables an extra UHCI slab for memory debugging */
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 if ((qh
->last_used
!=now
) || force
)
115 q
=qh
->horizontal
.prev
;
118 /*-------------------------------------------------------------------*/
119 #ifdef CONFIG_USB_UHCI_HIGH_BANDWIDTH
120 _static
void enable_desc_loop(uhci_t
*s
, urb_t
*urb
)
124 spin_lock_irqsave (&s
->qh_lock
, flags
);
125 s
->chain_end
->hw
.qh
.head
&=~UHCI_PTR_TERM
;
128 ((urb_priv_t
*)urb
->hcpriv
)->use_loop
=1;
129 spin_unlock_irqrestore (&s
->qh_lock
, flags
);
131 /*-------------------------------------------------------------------*/
132 _static
void disable_desc_loop(uhci_t
*s
, urb_t
*urb
)
136 spin_lock_irqsave (&s
->qh_lock
, flags
);
138 if (((urb_priv_t
*)urb
->hcpriv
)->use_loop
) {
141 if (!s
->loop_usage
) {
142 s
->chain_end
->hw
.qh
.head
|=UHCI_PTR_TERM
;
145 ((urb_priv_t
*)urb
->hcpriv
)->use_loop
=0;
147 spin_unlock_irqrestore (&s
->qh_lock
, flags
);
150 /*-------------------------------------------------------------------*/
151 _static
void queue_urb_unlocked (uhci_t
*s
, urb_t
*urb
)
153 struct list_head
*p
=&urb
->urb_list
;
154 #ifdef CONFIG_USB_UHCI_HIGH_BANDWIDTH
157 type
=usb_pipetype (urb
->pipe
);
159 if ((type
== PIPE_BULK
) || (type
== PIPE_CONTROL
))
160 enable_desc_loop(s
, urb
);
163 ((urb_priv_t
*)urb
->hcpriv
)->started
=jiffies
;
164 list_add (p
, &s
->urb_list
);
166 /*-------------------------------------------------------------------*/
167 _static
void queue_urb (uhci_t
*s
, urb_t
*urb
)
169 unsigned long flags
=0;
171 spin_lock_irqsave (&s
->urb_list_lock
, flags
);
172 queue_urb_unlocked(s
,urb
);
173 spin_unlock_irqrestore (&s
->urb_list_lock
, flags
);
175 /*-------------------------------------------------------------------*/
176 _static
void dequeue_urb (uhci_t
*s
, urb_t
*urb
)
178 #ifdef CONFIG_USB_UHCI_HIGH_BANDWIDTH
181 type
=usb_pipetype (urb
->pipe
);
183 if ((type
== PIPE_BULK
) || (type
== PIPE_CONTROL
))
184 disable_desc_loop(s
, urb
);
187 list_del (&urb
->urb_list
);
189 /*-------------------------------------------------------------------*/
190 _static
int alloc_td (uhci_desc_t
** new, int flags
)
193 *new= kmem_cache_alloc(uhci_desc_kmem
, SLAB_FLAG
);
195 *new = (uhci_desc_t
*) kmalloc (sizeof (uhci_desc_t
), KMALLOC_FLAG
);
199 memset (*new, 0, sizeof (uhci_desc_t
));
200 (*new)->hw
.td
.link
= UHCI_PTR_TERM
| (flags
& UHCI_PTR_BITS
); // last by default
201 (*new)->type
= TD_TYPE
;
203 INIT_LIST_HEAD (&(*new)->vertical
);
204 INIT_LIST_HEAD (&(*new)->horizontal
);
208 /*-------------------------------------------------------------------*/
209 // append a qh to td.link physically, the SW linkage is not affected
210 _static
void append_qh(uhci_t
*s
, uhci_desc_t
*td
, uhci_desc_t
* qh
, int flags
)
214 spin_lock_irqsave (&s
->td_lock
, xxx
);
216 td
->hw
.td
.link
= virt_to_bus (qh
) | (flags
& UHCI_PTR_DEPTH
) | UHCI_PTR_QH
;
219 spin_unlock_irqrestore (&s
->td_lock
, xxx
);
221 /*-------------------------------------------------------------------*/
222 /* insert td at last position in td-list of qh (vertical) */
223 _static
int insert_td (uhci_t
*s
, uhci_desc_t
*qh
, uhci_desc_t
* new, int flags
)
228 spin_lock_irqsave (&s
->td_lock
, xxx
);
230 list_add_tail (&new->vertical
, &qh
->vertical
);
232 prev
= list_entry (new->vertical
.prev
, uhci_desc_t
, vertical
);
235 // virgin qh without any tds
236 qh
->hw
.qh
.element
= virt_to_bus (new);
239 // already tds inserted, implicitely remove TERM bit of prev
240 prev
->hw
.td
.link
= virt_to_bus (new) | (flags
& UHCI_PTR_DEPTH
);
243 spin_unlock_irqrestore (&s
->td_lock
, xxx
);
247 /*-------------------------------------------------------------------*/
248 /* insert new_td after td (horizontal) */
249 _static
int insert_td_horizontal (uhci_t
*s
, uhci_desc_t
*td
, uhci_desc_t
* new)
254 spin_lock_irqsave (&s
->td_lock
, flags
);
256 next
= list_entry (td
->horizontal
.next
, uhci_desc_t
, horizontal
);
257 list_add (&new->horizontal
, &td
->horizontal
);
258 new->hw
.td
.link
= td
->hw
.td
.link
;
259 td
->hw
.td
.link
= virt_to_bus (new);
261 spin_unlock_irqrestore (&s
->td_lock
, flags
);
265 /*-------------------------------------------------------------------*/
266 _static
int unlink_td (uhci_t
*s
, uhci_desc_t
*element
, int phys_unlink
)
268 uhci_desc_t
*next
, *prev
;
272 spin_lock_irqsave (&s
->td_lock
, flags
);
274 next
= list_entry (element
->vertical
.next
, uhci_desc_t
, vertical
);
276 if (next
== element
) {
278 prev
= list_entry (element
->horizontal
.prev
, uhci_desc_t
, horizontal
);
281 prev
= list_entry (element
->vertical
.prev
, uhci_desc_t
, vertical
);
284 // really remove HW linking
285 if (prev
->type
== TD_TYPE
)
286 prev
->hw
.td
.link
= element
->hw
.td
.link
;
288 prev
->hw
.qh
.element
= element
->hw
.td
.link
;
294 list_del (&element
->vertical
);
296 list_del (&element
->horizontal
);
298 spin_unlock_irqrestore (&s
->td_lock
, flags
);
303 /*-------------------------------------------------------------------*/
304 _static
int delete_desc (uhci_desc_t
*element
)
307 kmem_cache_free(uhci_desc_kmem
, element
);
313 /*-------------------------------------------------------------------*/
314 // Allocates qh element
315 _static
int alloc_qh (uhci_desc_t
** new)
318 *new= kmem_cache_alloc(uhci_desc_kmem
, SLAB_FLAG
);
320 *new = (uhci_desc_t
*) kmalloc (sizeof (uhci_desc_t
), KMALLOC_FLAG
);
324 memset (*new, 0, sizeof (uhci_desc_t
));
325 (*new)->hw
.qh
.head
= UHCI_PTR_TERM
;
326 (*new)->hw
.qh
.element
= UHCI_PTR_TERM
;
327 (*new)->type
= QH_TYPE
;
330 INIT_LIST_HEAD (&(*new)->horizontal
);
331 INIT_LIST_HEAD (&(*new)->vertical
);
333 dbg("Allocated qh @ %p", *new);
337 /*-------------------------------------------------------------------*/
338 // inserts new qh before/after the qh at pos
339 // flags: 0: insert before pos, 1: insert after pos (for low speed transfers)
340 _static
int insert_qh (uhci_t
*s
, uhci_desc_t
*pos
, uhci_desc_t
*new, int order
)
345 spin_lock_irqsave (&s
->qh_lock
, flags
);
348 // (OLD) (POS) -> (OLD) (NEW) (POS)
349 old
= list_entry (pos
->horizontal
.prev
, uhci_desc_t
, horizontal
);
350 list_add_tail (&new->horizontal
, &pos
->horizontal
);
351 new->hw
.qh
.head
= MAKE_QH_ADDR (pos
) ;
352 if (!(old
->hw
.qh
.head
& UHCI_PTR_TERM
))
353 old
->hw
.qh
.head
= MAKE_QH_ADDR (new) ;
356 // (POS) (OLD) -> (POS) (NEW) (OLD)
357 old
= list_entry (pos
->horizontal
.next
, uhci_desc_t
, horizontal
);
358 list_add (&new->horizontal
, &pos
->horizontal
);
359 new->hw
.qh
.head
= MAKE_QH_ADDR (old
);
360 pos
->hw
.qh
.head
= MAKE_QH_ADDR (new) ;
365 spin_unlock_irqrestore (&s
->qh_lock
, flags
);
370 /*-------------------------------------------------------------------*/
371 _static
int unlink_qh (uhci_t
*s
, uhci_desc_t
*element
)
376 spin_lock_irqsave (&s
->qh_lock
, flags
);
378 prev
= list_entry (element
->horizontal
.prev
, uhci_desc_t
, horizontal
);
379 prev
->hw
.qh
.head
= element
->hw
.qh
.head
;
381 list_del(&element
->horizontal
);
384 spin_unlock_irqrestore (&s
->qh_lock
, flags
);
388 /*-------------------------------------------------------------------*/
389 _static
int delete_qh (uhci_t
*s
, uhci_desc_t
*qh
)
394 list_del (&qh
->horizontal
);
396 while ((p
= qh
->vertical
.next
) != &qh
->vertical
) {
397 td
= list_entry (p
, uhci_desc_t
, vertical
);
398 dbg("unlink td @ %p",td
);
399 unlink_td (s
, td
, 0); // no physical unlink
407 /*-------------------------------------------------------------------*/
408 _static
void clean_td_chain (uhci_desc_t
*td
)
416 while ((p
= td
->horizontal
.next
) != &td
->horizontal
) {
417 td1
= list_entry (p
, uhci_desc_t
, horizontal
);
424 /*-------------------------------------------------------------------*/
425 _static
void fill_td (uhci_desc_t
*td
, int status
, int info
, __u32 buffer
)
427 td
->hw
.td
.status
= status
;
428 td
->hw
.td
.info
= info
;
429 td
->hw
.td
.buffer
= buffer
;
431 /*-------------------------------------------------------------------*/
432 // Removes ALL qhs in chain (paranoia!)
433 _static
void cleanup_skel (uhci_t
*s
)
442 for (n
= 0; n
< 8; n
++) {
443 td
= s
->int_chain
[n
];
448 for (n
= 0; n
< 1024; n
++) {
456 free_page ((unsigned long) s
->framelist
);
458 if (s
->control_chain
) {
459 // completed init_skel?
461 uhci_desc_t
*qh
, *qh1
;
463 qh
= s
->control_chain
;
464 while ((p
= qh
->horizontal
.next
) != &qh
->horizontal
) {
465 qh1
= list_entry (p
, uhci_desc_t
, horizontal
);
472 if (s
->ls_control_chain
)
473 delete_desc (s
->ls_control_chain
);
474 if (s
->control_chain
)
475 delete_desc(s
->control_chain
);
477 delete_desc (s
->bulk_chain
);
479 delete_desc (s
->chain_end
);
481 dbg("cleanup_skel finished");
483 /*-------------------------------------------------------------------*/
484 // allocates framelist and qh-skeletons
485 // only HW-links provide continous linking, SW-links stay in their domain (ISO/INT)
486 _static
int init_skel (uhci_t
*s
)
489 uhci_desc_t
*qh
, *td
;
493 s
->framelist
= (__u32
*) get_free_page (GFP_KERNEL
);
498 memset (s
->framelist
, 0, 4096);
500 dbg("allocating iso desc pointer list");
501 s
->iso_td
= (uhci_desc_t
**) kmalloc (1024 * sizeof (uhci_desc_t
*), GFP_KERNEL
);
504 goto init_skel_cleanup
;
506 s
->ls_control_chain
= NULL
;
507 s
->control_chain
= NULL
;
508 s
->bulk_chain
= NULL
;
511 dbg("allocating iso descs");
512 for (n
= 0; n
< 1024; n
++) {
513 // allocate skeleton iso/irq-tds
514 ret
= alloc_td (&td
, 0);
516 goto init_skel_cleanup
;
518 s
->framelist
[n
] = ((__u32
) virt_to_bus (td
));
521 dbg("allocating qh: chain_end");
522 ret
= alloc_qh (&qh
);
525 goto init_skel_cleanup
;
529 ret
= alloc_td (&td
, 0);
532 goto init_skel_cleanup
;
534 fill_td (td
, TD_CTRL_IOC
, 0, 0); // generate 1ms interrupt
535 insert_td (s
, qh
, td
, 0);
537 dbg("allocating qh: bulk_chain");
538 ret
= alloc_qh (&qh
);
540 goto init_skel_cleanup
;
542 insert_qh (s
, s
->chain_end
, qh
, 0);
545 dbg("allocating qh: control_chain");
546 ret
= alloc_qh (&qh
);
548 goto init_skel_cleanup
;
550 insert_qh (s
, s
->bulk_chain
, qh
, 0);
551 s
->control_chain
= qh
;
553 #ifdef CONFIG_USB_UHCI_HIGH_BANDWIDTH
554 // disabled reclamation loop
555 s
->chain_end
->hw
.qh
.head
=virt_to_bus(s
->control_chain
) | UHCI_PTR_QH
| UHCI_PTR_TERM
;
558 dbg("allocating qh: ls_control_chain");
559 ret
= alloc_qh (&qh
);
561 goto init_skel_cleanup
;
563 insert_qh (s
, s
->control_chain
, qh
, 0);
564 s
->ls_control_chain
= qh
;
566 for (n
= 0; n
< 8; n
++)
569 dbg("allocating skeleton INT-TDs");
571 for (n
= 0; n
< 8; n
++) {
576 goto init_skel_cleanup
;
577 s
->int_chain
[n
] = td
;
579 s
->int_chain
[0]->hw
.td
.link
= virt_to_bus (s
->ls_control_chain
) | UHCI_PTR_QH
;
582 s
->int_chain
[n
]->hw
.td
.link
= virt_to_bus (s
->int_chain
[0]);
586 dbg("Linking skeleton INT-TDs");
588 for (n
= 0; n
< 1024; n
++) {
589 // link all iso-tds to the interrupt chains
591 dbg("framelist[%i]=%x",n
,s
->framelist
[n
]);
593 ((uhci_desc_t
*) s
->iso_td
[n
])->hw
.td
.link
= virt_to_bus(s
->int_chain
[0]);
595 for (o
= 1, m
= 2; m
<= 128; o
++, m
+= m
)
596 if ((n
& (m
- 1)) == ((m
- 1) / 2))
597 ((uhci_desc_t
*) s
->iso_td
[n
])->hw
.td
.link
= virt_to_bus (s
->int_chain
[o
]);
601 //uhci_show_queue(s->control_chain);
602 dbg("init_skel exit");
610 /*-------------------------------------------------------------------*/
612 // assembles QHs und TDs for control, bulk and iso
613 /*-------------------------------------------------------------------*/
614 _static
int uhci_submit_control_urb (urb_t
*urb
)
616 uhci_desc_t
*qh
, *td
;
617 uhci_t
*s
= (uhci_t
*) urb
->dev
->bus
->hcpriv
;
618 urb_priv_t
*urb_priv
= urb
->hcpriv
;
619 unsigned long destination
, status
;
620 int maxsze
= usb_maxpacket (urb
->dev
, urb
->pipe
, usb_pipeout (urb
->pipe
));
623 int depth_first
=USE_CTRL_DEPTH_FIRST
; // UHCI descriptor chasing method
626 err("uhci_submit_control_urb: pipesize for pipe %x is zero", urb
->pipe
);
630 dbg("uhci_submit_control start");
631 alloc_qh (&qh
); // alloc qh for this request
636 alloc_td (&td
, UHCI_PTR_DEPTH
* depth_first
); // get td for setup stage
643 /* The "pipe" thing contains the destination in bits 8--18 */
644 destination
= (urb
->pipe
& PIPE_DEVEP_MASK
) | USB_PID_SETUP
;
647 status
= (urb
->pipe
& TD_CTRL_LS
) | TD_CTRL_ACTIVE
|
648 (urb
->transfer_flags
& USB_DISABLE_SPD
? 0 : TD_CTRL_SPD
) | (3 << 27);
650 /* Build the TD for the control request, try forever, 8 bytes of data */
651 fill_td (td
, status
, destination
| (7 << 21), virt_to_bus (urb
->setup_packet
));
653 insert_td (s
, qh
, td
, 0); // queue 'setup stage'-td in qh
656 char *sp
=urb
->setup_packet
;
657 dbg("SETUP to pipe %x: %x %x %x %x %x %x %x %x", urb
->pipe
,
658 sp
[0],sp
[1],sp
[2],sp
[3],sp
[4],sp
[5],sp
[6],sp
[7]);
663 len
= urb
->transfer_buffer_length
;
664 data
= urb
->transfer_buffer
;
666 /* If direction is "send", change the frame from SETUP (0x2D)
667 to OUT (0xE1). Else change it from SETUP to IN (0x69). */
669 destination
= (urb
->pipe
& PIPE_DEVEP_MASK
) | (usb_pipeout (urb
->pipe
)?USB_PID_OUT
:USB_PID_IN
);
674 alloc_td (&td
, UHCI_PTR_DEPTH
* depth_first
);
683 destination
^= 1 << TD_TOKEN_TOGGLE
; // toggle DATA0/1
685 fill_td (td
, status
, destination
| ((pktsze
- 1) << 21),
686 virt_to_bus (data
)); // Status, pktsze bytes of data
688 insert_td (s
, qh
, td
, UHCI_PTR_DEPTH
* depth_first
); // queue 'data stage'-td in qh
694 /* Build the final TD for control status */
695 /* It's only IN if the pipe is out AND we aren't expecting data */
697 destination
&= ~UHCI_PID
;
699 if (usb_pipeout (urb
->pipe
) || (urb
->transfer_buffer_length
== 0))
700 destination
|= USB_PID_IN
;
702 destination
|= USB_PID_OUT
;
704 destination
|= 1 << TD_TOKEN_TOGGLE
; /* End in Data1 */
706 alloc_td (&td
, UHCI_PTR_DEPTH
);
712 status
&=~TD_CTRL_SPD
;
714 /* no limit on errors on final packet , 0 bytes of data */
715 fill_td (td
, status
| TD_CTRL_IOC
, destination
| (UHCI_NULL_DATA_SIZE
<< 21),
718 insert_td (s
, qh
, td
, UHCI_PTR_DEPTH
* depth_first
); // queue status td
720 list_add (&qh
->desc_list
, &urb_priv
->desc_list
);
722 urb
->status
= -EINPROGRESS
;
723 queue_urb (s
, urb
); // queue before inserting in desc chain
725 qh
->hw
.qh
.element
&= ~UHCI_PTR_TERM
;
727 //uhci_show_queue(qh);
728 /* Start it up... put low speed first */
729 if (urb
->pipe
& TD_CTRL_LS
)
730 insert_qh (s
, s
->control_chain
, qh
, 0);
732 insert_qh (s
, s
->bulk_chain
, qh
, 0);
734 dbg("uhci_submit_control end");
737 /*-------------------------------------------------------------------*/
738 // For queued bulk transfers, two additional QH helpers are allocated (nqh, bqh)
739 // Due to the linking with other bulk urbs, it has to be locked with urb_list_lock!
741 _static
int uhci_submit_bulk_urb (urb_t
*urb
, urb_t
*bulk_urb
)
743 uhci_t
*s
= (uhci_t
*) urb
->dev
->bus
->hcpriv
;
744 urb_priv_t
*urb_priv
= urb
->hcpriv
;
745 uhci_desc_t
*qh
, *td
, *nqh
, *bqh
;
746 unsigned long destination
, status
;
748 unsigned int pipe
= urb
->pipe
;
749 int maxsze
= usb_maxpacket (urb
->dev
, pipe
, usb_pipeout (pipe
));
751 int depth_first
=USE_BULK_DEPTH_FIRST
; // UHCI descriptor chasing method
752 urb_priv_t
*upriv
, *bpriv
;
754 if (usb_endpoint_halted (urb
->dev
, usb_pipeendpoint (pipe
), usb_pipeout (pipe
)))
757 if (urb
->transfer_buffer_length
< 0) {
758 err("Negative transfer length in submit_bulk");
765 queue_dbg("uhci_submit_bulk_urb: urb %p, old %p, pipe %08x, len %i",
766 urb
,bulk_urb
,urb
->pipe
,urb
->transfer_buffer_length
);
768 upriv
=(urb_priv_t
*)urb
->hcpriv
;
771 alloc_qh (&qh
); // get qh for this request
776 if (urb
->transfer_flags
& USB_QUEUE_BULK
) {
777 alloc_qh(&nqh
); // placeholder for clean unlink
782 upriv
->next_qh
= nqh
;
783 queue_dbg("new next qh %p",nqh
);
787 bpriv
= (urb_priv_t
*)bulk_urb
->hcpriv
;
788 qh
= bpriv
->bottom_qh
; // re-use bottom qh and next qh
789 nqh
= bpriv
->next_qh
;
791 bpriv
->next_queued_urb
=urb
;
792 upriv
->prev_queued_urb
=bulk_urb
;
795 queue_dbg("uhci_submit_bulk: qh=%p, nqh=%p\n",bqh
,nqh
);
797 if (urb
->transfer_flags
& USB_QUEUE_BULK
) {
798 alloc_qh (&bqh
); // "bottom" QH,
807 bqh
->hw
.qh
.element
= UHCI_PTR_TERM
;
808 bqh
->hw
.qh
.element
= virt_to_bus(nqh
)|UHCI_PTR_QH
;
809 upriv
->bottom_qh
= bqh
;
810 queue_dbg("uhci_submit_bulk: new bqh %p\n",bqh
);
814 /* The "pipe" thing contains the destination in bits 8--18. */
815 destination
= (pipe
& PIPE_DEVEP_MASK
) | usb_packetid (pipe
);
818 status
= (pipe
& TD_CTRL_LS
) | TD_CTRL_ACTIVE
|
819 ((urb
->transfer_flags
& USB_DISABLE_SPD
) ? 0 : TD_CTRL_SPD
) | (3 << 27);
821 /* Build the TDs for the bulk request */
822 len
= urb
->transfer_buffer_length
;
823 data
= urb
->transfer_buffer
;
825 do { // TBD: Really allow zero-length packets?
828 alloc_td (&td
, UHCI_PTR_DEPTH
* depth_first
);
838 // pktsze bytes of data
839 info
= destination
| (((pktsze
- 1)&UHCI_NULL_DATA_SIZE
) << 21) |
840 (usb_gettoggle (urb
->dev
, usb_pipeendpoint (pipe
), usb_pipeout (pipe
)) << TD_TOKEN_TOGGLE
);
842 fill_td (td
, status
, info
, virt_to_bus (data
));
848 td
->hw
.td
.status
|= TD_CTRL_IOC
; // last one generates INT
850 insert_td (s
, qh
, td
, UHCI_PTR_DEPTH
* depth_first
);
851 usb_dotoggle (urb
->dev
, usb_pipeendpoint (pipe
), usb_pipeout (pipe
));
855 list_add (&qh
->desc_list
, &urb_priv
->desc_list
);
857 if (urb
->transfer_flags
& USB_QUEUE_BULK
) {
858 qh
->hw
.qh
.element
&=~UHCI_PTR_TERM
;
859 append_qh(s
, td
, bqh
, UHCI_PTR_DEPTH
* depth_first
);
862 urb
->status
= -EINPROGRESS
;
863 queue_urb_unlocked (s
, urb
);
865 qh
->hw
.qh
.element
&= ~UHCI_PTR_TERM
;
868 if (urb
->transfer_flags
& USB_QUEUE_BULK
) {
869 spin_lock (&s
->td_lock
); // both QHs in one go
870 insert_qh (s
, s
->chain_end
, qh
, 0); // Main QH
871 insert_qh (s
, s
->chain_end
, nqh
, 0); // Helper QH
872 spin_unlock (&s
->td_lock
);
875 insert_qh (s
, s
->chain_end
, qh
, 0);
878 //uhci_show_queue(s->bulk_chain);
879 //dbg("uhci_submit_bulk_urb: exit\n");
882 /*-------------------------------------------------------------------*/
883 _static
void uhci_clean_iso_step1(uhci_t
*s
, urb_priv_t
*urb_priv
)
888 for (p
= urb_priv
->desc_list
.next
; p
!= &urb_priv
->desc_list
; p
= p
->next
) {
889 td
= list_entry (p
, uhci_desc_t
, desc_list
);
890 unlink_td (s
, td
, 1);
893 /*-------------------------------------------------------------------*/
894 _static
void uhci_clean_iso_step2(uhci_t
*s
, urb_priv_t
*urb_priv
)
899 while ((p
= urb_priv
->desc_list
.next
) != &urb_priv
->desc_list
) {
900 td
= list_entry (p
, uhci_desc_t
, desc_list
);
905 /*-------------------------------------------------------------------*/
906 // mode: 0: unlink + no deletion mark, 1: regular (unlink/delete-mark), 2: don't unlink
907 // looks a bit complicated because of all the bulk queueing goodies
909 _static
void uhci_clean_transfer (uhci_t
*s
, urb_t
*urb
, uhci_desc_t
*qh
, int mode
)
911 uhci_desc_t
*bqh
, *nqh
, *prevqh
;
913 urb_priv_t
*priv
=(urb_priv_t
*)urb
->hcpriv
;
915 now
=UHCI_GET_CURRENT_FRAME(s
);
917 dbg("clean transfer urb %p, qh %p, mode %i",urb
,qh
,mode
);
920 if (!priv
->next_queued_urb
) { // no more appended bulk queues
925 if (priv
->prev_queued_urb
) {
926 urb_priv_t
* ppriv
=(urb_priv_t
*)priv
->prev_queued_urb
->hcpriv
;
928 ppriv
->bottom_qh
= priv
->bottom_qh
;
929 ppriv
->next_queued_urb
= NULL
;
931 else if (bqh
) { // queue dead
938 nqh
->last_used
= bqh
->last_used
= now
;
939 list_add_tail (&nqh
->horizontal
, &s
->free_desc
);
940 list_add_tail (&bqh
->horizontal
, &s
->free_desc
);
944 else { // there are queued urbs following
948 nurb
=priv
->next_queued_urb
;
949 spin_lock_irqsave (&s
->qh_lock
, flags
);
951 if (!priv
->prev_queued_urb
) { // top
953 prevqh
= list_entry (qh
->horizontal
.prev
, uhci_desc_t
, horizontal
);
954 prevqh
->hw
.qh
.head
= virt_to_bus(bqh
) | UHCI_PTR_QH
;
955 queue_dbg ("TOP relink of %p to %p-%p",qh
,prevqh
,bqh
);
957 list_del (&qh
->horizontal
);
958 list_add (&bqh
->horizontal
, &prevqh
->horizontal
);
961 else { //intermediate
962 urb_priv_t
* ppriv
=(urb_priv_t
*)priv
->prev_queued_urb
->hcpriv
;
965 bnqh
=list_entry (&((urb_priv_t
*)(nurb
->hcpriv
))->desc_list
.next
, uhci_desc_t
, desc_list
);
966 ppriv
->bottom_qh
=bnqh
;
967 ppriv
->next_queued_urb
=nurb
;
970 prevqh
= list_entry (ppriv
->desc_list
.next
, uhci_desc_t
, desc_list
);
971 prevqh
->hw
.qh
.head
= virt_to_bus(bqh
) | UHCI_PTR_QH
;
972 queue_dbg ("IM relink of %p to %p-%p",qh
,prevqh
,bqh
);
976 spin_unlock_irqrestore (&s
->qh_lock
, flags
);
977 ((urb_priv_t
*)nurb
->hcpriv
)->prev_queued_urb
=priv
->prev_queued_urb
;
982 list_add_tail (&qh
->horizontal
, &s
->free_desc
); // mark for later deletion
985 /*-------------------------------------------------------------------*/
986 // unlinks an urb by dequeuing its qh, waits some frames and forgets it
987 _static
int uhci_unlink_urb_sync (uhci_t
*s
, urb_t
*urb
)
990 urb_priv_t
*urb_priv
;
991 unsigned long flags
=0;
993 spin_lock_irqsave (&s
->urb_list_lock
, flags
);
995 if (urb
->status
== -EINPROGRESS
) {
996 // URB probably still in work
997 dequeue_urb (s
, urb
);
998 s
->unlink_urb_done
=1;
999 spin_unlock_irqrestore (&s
->urb_list_lock
, flags
);
1001 urb
->status
= -ENOENT
; // mark urb as killed
1002 urb_priv
= urb
->hcpriv
;
1004 switch (usb_pipetype (urb
->pipe
)) {
1005 case PIPE_ISOCHRONOUS
:
1006 case PIPE_INTERRUPT
:
1007 uhci_clean_iso_step1(s
, urb_priv
);
1009 uhci_clean_iso_step2(s
, urb_priv
);
1014 qh
= list_entry (urb_priv
->desc_list
.next
, uhci_desc_t
, desc_list
);
1015 spin_lock_irqsave (&s
->urb_list_lock
, flags
);
1016 uhci_clean_transfer(s
, urb
, qh
, 1);
1017 spin_unlock_irqrestore (&s
->urb_list_lock
, flags
);
1022 kmem_cache_free (urb_priv_kmem
, urb
->hcpriv
);
1024 kfree (urb
->hcpriv
);
1026 if (urb
->complete
) {
1027 dbg("unlink_urb: calling completion");
1028 urb
->complete ((struct urb
*) urb
);
1030 usb_dec_dev_use (urb
->dev
);
1034 spin_unlock_irqrestore (&s
->urb_list_lock
, flags
);
1037 /*-------------------------------------------------------------------*/
1038 // async unlink_urb completion/cleanup work
1039 // has to be protected by urb_list_lock!
1040 // features: if set in transfer_flags, the resulting status of the killed
1041 // transaction is not overwritten
1043 _static
void uhci_cleanup_unlink(uhci_t
*s
, int force
)
1045 struct list_head
*q
;
1047 struct usb_device
*dev
;
1049 urb_priv_t
*urb_priv
;
1051 q
=s
->urb_unlinked
.next
;
1052 now
=UHCI_GET_CURRENT_FRAME(s
);
1054 while (q
!= &s
->urb_unlinked
) {
1056 urb
= list_entry (q
, urb_t
, urb_list
);
1058 urb_priv
= (urb_priv_t
*)urb
->hcpriv
;
1059 q
= urb
->urb_list
.next
;
1062 ((urb_priv
->started
!= 0xffffffff) && (urb_priv
->started
!= now
))) {
1063 async_dbg("async cleanup %p",urb
);
1064 switch (usb_pipetype (urb
->pipe
)) { // process descriptors
1066 process_transfer (s
, urb
, 2);
1069 if (!s
->avoid_bulk
.counter
)
1070 process_transfer (s
, urb
, 2); // don't unlink (already done)
1074 case PIPE_ISOCHRONOUS
:
1075 process_iso (s
, urb
, 1); // force, don't unlink
1077 case PIPE_INTERRUPT
:
1078 process_interrupt (s
, urb
);
1082 if (!(urb
->transfer_flags
& USB_TIMEOUT_KILLED
))
1083 urb
->status
= -ECONNRESET
; // mark as asynchronously killed
1085 pipe
= urb
->pipe
; // completion may destroy all...
1087 urb_priv
= urb
->hcpriv
;
1089 if (urb
->complete
) {
1090 spin_unlock(&s
->urb_list_lock
);
1091 urb
->complete ((struct urb
*) urb
);
1092 spin_lock(&s
->urb_list_lock
);
1095 if (!(urb
->transfer_flags
& USB_TIMEOUT_KILLED
))
1096 urb
->status
= -ENOENT
; // now the urb is really dead
1098 usb_dec_dev_use (dev
);
1100 kmem_cache_free (urb_priv_kmem
, urb_priv
);
1104 switch (usb_pipetype (pipe
)) {
1105 case PIPE_ISOCHRONOUS
:
1106 case PIPE_INTERRUPT
:
1107 uhci_clean_iso_step2(s
, urb_priv
);
1110 list_del (&urb
->urb_list
);
1115 /*-------------------------------------------------------------------*/
1116 _static
int uhci_unlink_urb_async (uhci_t
*s
,urb_t
*urb
)
1119 urb_priv_t
*urb_priv
;
1121 async_dbg("unlink_urb_async called %p",urb
);
1123 if (urb
->status
== -EINPROGRESS
) {
1124 ((urb_priv_t
*)urb
->hcpriv
)->started
= ~0;
1125 dequeue_urb (s
, urb
);
1126 list_add_tail (&urb
->urb_list
, &s
->urb_unlinked
); // store urb
1128 s
->unlink_urb_done
= 1;
1130 urb
->status
= -ECONNABORTED
; // mark urb as "waiting to be killed"
1131 urb_priv
= (urb_priv_t
*)urb
->hcpriv
;
1133 switch (usb_pipetype (urb
->pipe
)) {
1134 case PIPE_ISOCHRONOUS
:
1135 case PIPE_INTERRUPT
:
1136 uhci_clean_iso_step1 (s
, urb_priv
);
1141 qh
= list_entry (urb_priv
->desc_list
.next
, uhci_desc_t
, desc_list
);
1142 uhci_clean_transfer (s
, urb
, qh
, 0);
1145 ((urb_priv_t
*)urb
->hcpriv
)->started
= UHCI_GET_CURRENT_FRAME(s
);
1148 return -EINPROGRESS
;
1150 /*-------------------------------------------------------------------*/
1151 _static
int uhci_unlink_urb (urb_t
*urb
)
1154 unsigned long flags
=0;
1155 dbg("uhci_unlink_urb called for %p",urb
);
1156 if (!urb
|| !urb
->dev
) // you never know...
1159 s
= (uhci_t
*) urb
->dev
->bus
->hcpriv
;
1161 if (usb_pipedevice (urb
->pipe
) == s
->rh
.devnum
)
1162 return rh_unlink_urb (urb
);
1167 if (urb
->transfer_flags
& USB_ASYNC_UNLINK
) {
1170 spin_lock_irqsave (&s
->urb_list_lock
, flags
);
1171 ret
= uhci_unlink_urb_async(s
, urb
);
1172 spin_unlock_irqrestore (&s
->urb_list_lock
, flags
);
1176 return uhci_unlink_urb_sync(s
, urb
);
1178 /*-------------------------------------------------------------------*/
1179 // In case of ASAP iso transfer, search the URB-list for already queued URBs
1180 // for this EP and calculate the earliest start frame for the new
1181 // URB (easy seamless URB continuation!)
1182 _static
int find_iso_limits (urb_t
*urb
, unsigned int *start
, unsigned int *end
)
1184 urb_t
*u
, *last_urb
= NULL
;
1185 uhci_t
*s
= (uhci_t
*) urb
->dev
->bus
->hcpriv
;
1186 struct list_head
*p
;
1188 unsigned long flags
;
1190 spin_lock_irqsave (&s
->urb_list_lock
, flags
);
1193 for (; p
!= &s
->urb_list
; p
= p
->prev
) {
1194 u
= list_entry (p
, urb_t
, urb_list
);
1195 // look for pending URBs with identical pipe handle
1196 // works only because iso doesn't toggle the data bit!
1197 if ((urb
->pipe
== u
->pipe
) && (urb
->dev
== u
->dev
) && (u
->status
== -EINPROGRESS
)) {
1199 *start
= u
->start_frame
;
1205 *end
= (last_urb
->start_frame
+ last_urb
->number_of_packets
) & 1023;
1209 spin_unlock_irqrestore(&s
->urb_list_lock
, flags
);
1213 /*-------------------------------------------------------------------*/
1214 // adjust start_frame according to scheduling constraints (ASAP etc)
1216 _static
int iso_find_start (urb_t
*urb
)
1218 uhci_t
*s
= (uhci_t
*) urb
->dev
->bus
->hcpriv
;
1220 unsigned int start_limit
= 0, stop_limit
= 0, queued_size
;
1223 now
= UHCI_GET_CURRENT_FRAME (s
) & 1023;
1225 if ((unsigned) urb
->number_of_packets
> 900)
1228 limits
= find_iso_limits (urb
, &start_limit
, &stop_limit
);
1229 queued_size
= (stop_limit
- start_limit
) & 1023;
1231 if (urb
->transfer_flags
& USB_ISO_ASAP
) {
1234 // 10ms setup should be enough //FIXME!
1235 urb
->start_frame
= (now
+ 10) & 1023;
1238 urb
->start_frame
= stop_limit
; //seamless linkage
1240 if (((now
- urb
->start_frame
) & 1023) <= (unsigned) urb
->number_of_packets
) {
1241 info("iso_find_start: gap in seamless isochronous scheduling");
1242 dbg("iso_find_start: now %u start_frame %u number_of_packets %u pipe 0x%08x",
1243 now
, urb
->start_frame
, urb
->number_of_packets
, urb
->pipe
);
1244 urb
->start_frame
= (now
+ 5) & 1023; // 5ms setup should be enough //FIXME!
1249 urb
->start_frame
&= 1023;
1250 if (((now
- urb
->start_frame
) & 1023) < (unsigned) urb
->number_of_packets
) {
1251 dbg("iso_find_start: now between start_frame and end");
1256 /* check if either start_frame or start_frame+number_of_packets-1 lies between start_limit and stop_limit */
1260 if (((urb
->start_frame
- start_limit
) & 1023) < queued_size
||
1261 ((urb
->start_frame
+ urb
->number_of_packets
- 1 - start_limit
) & 1023) < queued_size
) {
1262 dbg("iso_find_start: start_frame %u number_of_packets %u start_limit %u stop_limit %u",
1263 urb
->start_frame
, urb
->number_of_packets
, start_limit
, stop_limit
);
1269 /*-------------------------------------------------------------------*/
1270 // submits USB interrupt (ie. polling ;-)
1271 // ASAP-flag set implicitely
1272 // if period==0, the the transfer is only done once
1274 _static
int uhci_submit_int_urb (urb_t
*urb
)
1276 uhci_t
*s
= (uhci_t
*) urb
->dev
->bus
->hcpriv
;
1277 urb_priv_t
*urb_priv
= urb
->hcpriv
;
1280 int status
, destination
;
1282 unsigned int pipe
= urb
->pipe
;
1284 if (urb
->interval
< 0 || urb
->interval
>= 256)
1287 if (urb
->interval
== 0)
1290 for (nint
= 0, n
= 1; nint
<= 8; nint
++, n
+= n
) // round interval down to 2^n
1292 if (urb
->interval
< n
) {
1293 urb
->interval
= n
/ 2;
1300 dbg("Rounded interval to %i, chain %i", urb
->interval
, nint
);
1302 urb
->start_frame
= UHCI_GET_CURRENT_FRAME (s
) & 1023; // remember start frame, just in case...
1304 urb
->number_of_packets
= 1;
1306 // INT allows only one packet
1307 if (urb
->transfer_buffer_length
> usb_maxpacket (urb
->dev
, pipe
, usb_pipeout (pipe
)))
1310 ret
= alloc_td (&td
, UHCI_PTR_DEPTH
);
1315 status
= (pipe
& TD_CTRL_LS
) | TD_CTRL_ACTIVE
| TD_CTRL_IOC
|
1316 (urb
->transfer_flags
& USB_DISABLE_SPD
? 0 : TD_CTRL_SPD
) | (3 << 27);
1318 destination
= (urb
->pipe
& PIPE_DEVEP_MASK
) | usb_packetid (urb
->pipe
) |
1319 (((urb
->transfer_buffer_length
- 1) & 0x7ff) << 21);
1322 info
= destination
| (usb_gettoggle (urb
->dev
, usb_pipeendpoint (pipe
), usb_pipeout (pipe
)) << TD_TOKEN_TOGGLE
);
1324 fill_td (td
, status
, info
, virt_to_bus (urb
->transfer_buffer
));
1325 list_add_tail (&td
->desc_list
, &urb_priv
->desc_list
);
1327 urb
->status
= -EINPROGRESS
;
1330 insert_td_horizontal (s
, s
->int_chain
[nint
], td
); // store in INT-TDs
1332 usb_dotoggle (urb
->dev
, usb_pipeendpoint (pipe
), usb_pipeout (pipe
));
1336 /*-------------------------------------------------------------------*/
1337 _static
int uhci_submit_iso_urb (urb_t
*urb
)
1339 uhci_t
*s
= (uhci_t
*) urb
->dev
->bus
->hcpriv
;
1340 urb_priv_t
*urb_priv
= urb
->hcpriv
;
1342 int maxsze
= usb_maxpacket (urb
->dev
, pipe
, usb_pipeout (pipe
));
1344 uhci_desc_t
*td
, **tdm
;
1345 int status
, destination
;
1346 unsigned long flags
;
1348 __save_flags(flags
);
1349 __cli(); // Disable IRQs to schedule all ISO-TDs in time
1350 ret
= iso_find_start (urb
); // adjusts urb->start_frame for later use
1355 tdm
= (uhci_desc_t
**) kmalloc (urb
->number_of_packets
* sizeof (uhci_desc_t
*), KMALLOC_FLAG
);
1362 // First try to get all TDs
1363 for (n
= 0; n
< urb
->number_of_packets
; n
++) {
1364 dbg("n:%d urb->iso_frame_desc[n].length:%d", n
, urb
->iso_frame_desc
[n
].length
);
1365 if (!urb
->iso_frame_desc
[n
].length
) {
1366 // allows ISO striping by setting length to zero in iso_descriptor
1371 if(urb
->iso_frame_desc
[n
].length
> maxsze
) {
1372 #ifdef ISO_SANITY_CHECK
1373 err("submit_iso: urb->iso_frame_desc[%d].length(%d)>%d",n
, urb
->iso_frame_desc
[n
].length
, maxsze
);
1380 ret
= alloc_td (&td
, UHCI_PTR_DEPTH
);
1383 int i
; // Cleanup allocated TDs
1385 for (i
= 0; i
< n
; n
++)
1387 delete_desc(tdm
[i
]);
1395 status
= TD_CTRL_ACTIVE
| TD_CTRL_IOS
; //| (urb->transfer_flags&USB_DISABLE_SPD?0:TD_CTRL_SPD);
1397 destination
= (urb
->pipe
& PIPE_DEVEP_MASK
) | usb_packetid (urb
->pipe
);
1400 // Queue all allocated TDs
1401 for (n
= 0; n
< urb
->number_of_packets
; n
++) {
1407 status
|= TD_CTRL_IOC
;
1409 fill_td (td
, status
, destination
| (((urb
->iso_frame_desc
[n
].length
- 1) & 0x7ff) << 21),
1410 virt_to_bus (urb
->transfer_buffer
+ urb
->iso_frame_desc
[n
].offset
));
1411 list_add_tail (&td
->desc_list
, &urb_priv
->desc_list
);
1414 urb
->status
= -EINPROGRESS
;
1417 insert_td_horizontal (s
, s
->iso_td
[(urb
->start_frame
+ n
) & 1023], td
); // store in iso-tds
1423 dbg("ISO-INT# %i, start %i, now %i", urb
->number_of_packets
, urb
->start_frame
, UHCI_GET_CURRENT_FRAME (s
) & 1023);
1427 __restore_flags(flags
);
1431 /*-------------------------------------------------------------------*/
1432 // returns: 0 (no transfer queued), urb* (this urb already queued)
1434 _static urb_t
* search_dev_ep (uhci_t
*s
, urb_t
*urb
)
1436 struct list_head
*p
;
1438 unsigned int mask
= usb_pipecontrol(urb
->pipe
) ? (~USB_DIR_IN
) : (~0);
1440 dbg("search_dev_ep:");
1444 for (; p
!= &s
->urb_list
; p
= p
->next
) {
1445 tmp
= list_entry (p
, urb_t
, urb_list
);
1446 dbg("urb: %p", tmp
);
1447 // we can accept this urb if it is not queued at this time
1448 // or if non-iso transfer requests should be scheduled for the same device and pipe
1449 if ((!usb_pipeisoc(urb
->pipe
) && (tmp
->dev
== urb
->dev
) && !((tmp
->pipe
^ urb
->pipe
) & mask
)) ||
1451 return tmp
; // found another urb already queued for processing
1457 /*-------------------------------------------------------------------*/
1458 _static
int uhci_submit_urb (urb_t
*urb
)
1461 urb_priv_t
*urb_priv
;
1463 unsigned long flags
;
1464 urb_t
*bulk_urb
=NULL
;
1466 if (!urb
->dev
|| !urb
->dev
->bus
)
1469 s
= (uhci_t
*) urb
->dev
->bus
->hcpriv
;
1470 //dbg("submit_urb: %p type %d",urb,usb_pipetype(urb->pipe));
1475 if (usb_pipedevice (urb
->pipe
) == s
->rh
.devnum
)
1476 return rh_submit_urb (urb
); /* virtual root hub */
1478 usb_inc_dev_use (urb
->dev
);
1480 spin_lock_irqsave (&s
->urb_list_lock
, flags
);
1482 bulk_urb
= search_dev_ep (s
, urb
);
1486 queue_dbg("found bulk urb %p\n",bulk_urb
);
1488 if ((usb_pipetype (urb
->pipe
) != PIPE_BULK
) ||
1489 ((usb_pipetype (urb
->pipe
) == PIPE_BULK
) &&
1490 (!(urb
->transfer_flags
& USB_QUEUE_BULK
) || !(bulk_urb
->transfer_flags
& USB_QUEUE_BULK
)))) {
1491 spin_unlock_irqrestore (&s
->urb_list_lock
, flags
);
1492 usb_dec_dev_use (urb
->dev
);
1493 err("ENXIO1 %08x, flags %x, urb %p, burb %p",urb
->pipe
,urb
->transfer_flags
,urb
,bulk_urb
);
1494 return -ENXIO
; // urb already queued
1499 urb_priv
= kmem_cache_alloc(urb_priv_kmem
, SLAB_FLAG
);
1501 urb_priv
= kmalloc (sizeof (urb_priv_t
), KMALLOC_FLAG
);
1504 usb_dec_dev_use (urb
->dev
);
1505 spin_unlock_irqrestore (&s
->urb_list_lock
, flags
);
1509 urb
->hcpriv
= urb_priv
;
1510 INIT_LIST_HEAD (&urb_priv
->desc_list
);
1511 urb_priv
->short_control_packet
= 0;
1512 dbg("submit_urb: scheduling %p", urb
);
1513 urb_priv
->next_queued_urb
= NULL
;
1514 urb_priv
->prev_queued_urb
= NULL
;
1515 urb_priv
->bottom_qh
= NULL
;
1516 urb_priv
->next_qh
= NULL
;
1518 if (usb_pipetype (urb
->pipe
) == PIPE_BULK
) {
1521 while (((urb_priv_t
*)bulk_urb
->hcpriv
)->next_queued_urb
) // find last queued bulk
1522 bulk_urb
=((urb_priv_t
*)bulk_urb
->hcpriv
)->next_queued_urb
;
1524 ((urb_priv_t
*)bulk_urb
->hcpriv
)->next_queued_urb
=urb
;
1526 atomic_inc (&s
->avoid_bulk
);
1527 ret
= uhci_submit_bulk_urb (urb
, bulk_urb
);
1528 atomic_dec (&s
->avoid_bulk
);
1529 spin_unlock_irqrestore (&s
->urb_list_lock
, flags
);
1532 spin_unlock_irqrestore (&s
->urb_list_lock
, flags
);
1533 switch (usb_pipetype (urb
->pipe
)) {
1534 case PIPE_ISOCHRONOUS
:
1535 ret
= uhci_submit_iso_urb (urb
);
1537 case PIPE_INTERRUPT
:
1538 ret
= uhci_submit_int_urb (urb
);
1541 ret
= uhci_submit_control_urb (urb
);
1548 dbg("submit_urb: scheduled with ret: %d", ret
);
1551 usb_dec_dev_use (urb
->dev
);
1553 kmem_cache_free(urb_priv_kmem
, urb_priv
);
1563 // Checks for URB timeout and removes bandwidth reclamation
1564 // if URB idles too long
1565 _static
void uhci_check_timeouts(uhci_t
*s
)
1567 struct list_head
*p
,*p2
;
1571 p
= s
->urb_list
.prev
;
1573 while (p
!= &s
->urb_list
) {
1578 urb
= list_entry (p2
, urb_t
, urb_list
);
1579 type
= usb_pipetype (urb
->pipe
);
1581 hcpriv
= (urb_priv_t
*)urb
->hcpriv
;
1583 if ( urb
->timeout
&&
1584 ((hcpriv
->started
+ urb
->timeout
) < jiffies
)) {
1585 urb
->transfer_flags
|= USB_TIMEOUT_KILLED
| USB_ASYNC_UNLINK
;
1586 async_dbg("uhci_check_timeout: timeout for %p",urb
);
1587 uhci_unlink_urb_async(s
, urb
);
1589 #ifdef CONFIG_USB_UHCI_HIGH_BANDWIDTH
1590 else if (((type
== PIPE_BULK
) || (type
== PIPE_CONTROL
)) &&
1591 (hcpriv
->use_loop
) &&
1592 ((hcpriv
->started
+ IDLE_TIMEOUT
) < jiffies
))
1593 disable_desc_loop(s
, urb
);
1599 /*-------------------------------------------------------------------
1601 -------------------------------------------------------------------*/
1603 _static __u8 root_hub_dev_des
[] =
1605 0x12, /* __u8 bLength; */
1606 0x01, /* __u8 bDescriptorType; Device */
1607 0x00, /* __u16 bcdUSB; v1.0 */
1609 0x09, /* __u8 bDeviceClass; HUB_CLASSCODE */
1610 0x00, /* __u8 bDeviceSubClass; */
1611 0x00, /* __u8 bDeviceProtocol; */
1612 0x08, /* __u8 bMaxPacketSize0; 8 Bytes */
1613 0x00, /* __u16 idVendor; */
1615 0x00, /* __u16 idProduct; */
1617 0x00, /* __u16 bcdDevice; */
1619 0x00, /* __u8 iManufacturer; */
1620 0x00, /* __u8 iProduct; */
1621 0x00, /* __u8 iSerialNumber; */
1622 0x01 /* __u8 bNumConfigurations; */
1626 /* Configuration descriptor */
1627 _static __u8 root_hub_config_des
[] =
1629 0x09, /* __u8 bLength; */
1630 0x02, /* __u8 bDescriptorType; Configuration */
1631 0x19, /* __u16 wTotalLength; */
1633 0x01, /* __u8 bNumInterfaces; */
1634 0x01, /* __u8 bConfigurationValue; */
1635 0x00, /* __u8 iConfiguration; */
1636 0x40, /* __u8 bmAttributes;
1637 Bit 7: Bus-powered, 6: Self-powered, 5 Remote-wakwup, 4..0: resvd */
1638 0x00, /* __u8 MaxPower; */
1641 0x09, /* __u8 if_bLength; */
1642 0x04, /* __u8 if_bDescriptorType; Interface */
1643 0x00, /* __u8 if_bInterfaceNumber; */
1644 0x00, /* __u8 if_bAlternateSetting; */
1645 0x01, /* __u8 if_bNumEndpoints; */
1646 0x09, /* __u8 if_bInterfaceClass; HUB_CLASSCODE */
1647 0x00, /* __u8 if_bInterfaceSubClass; */
1648 0x00, /* __u8 if_bInterfaceProtocol; */
1649 0x00, /* __u8 if_iInterface; */
1652 0x07, /* __u8 ep_bLength; */
1653 0x05, /* __u8 ep_bDescriptorType; Endpoint */
1654 0x81, /* __u8 ep_bEndpointAddress; IN Endpoint 1 */
1655 0x03, /* __u8 ep_bmAttributes; Interrupt */
1656 0x08, /* __u16 ep_wMaxPacketSize; 8 Bytes */
1658 0xff /* __u8 ep_bInterval; 255 ms */
1662 _static __u8 root_hub_hub_des
[] =
1664 0x09, /* __u8 bLength; */
1665 0x29, /* __u8 bDescriptorType; Hub-descriptor */
1666 0x02, /* __u8 bNbrPorts; */
1667 0x00, /* __u16 wHubCharacteristics; */
1669 0x01, /* __u8 bPwrOn2pwrGood; 2ms */
1670 0x00, /* __u8 bHubContrCurrent; 0 mA */
1671 0x00, /* __u8 DeviceRemovable; *** 7 Ports max *** */
1672 0xff /* __u8 PortPwrCtrlMask; *** 7 ports max *** */
1675 /*-------------------------------------------------------------------------*/
1676 /* prepare Interrupt pipe transaction data; HUB INTERRUPT ENDPOINT */
1677 _static
int rh_send_irq (urb_t
*urb
)
1681 uhci_t
*uhci
= urb
->dev
->bus
->hcpriv
;
1682 unsigned int io_addr
= uhci
->io_addr
;
1685 for (i
= 0; i
< uhci
->rh
.numports
; i
++) {
1686 data
|= ((inw (io_addr
+ USBPORTSC1
+ i
* 2) & 0xa) > 0 ? (1 << (i
+ 1)) : 0);
1687 len
= (i
+ 1) / 8 + 1;
1690 *(__u16
*) urb
->transfer_buffer
= cpu_to_le16 (data
);
1691 urb
->actual_length
= len
;
1694 if ((data
> 0) && (uhci
->rh
.send
!= 0)) {
1695 dbg("Root-Hub INT complete: port1: %x port2: %x data: %x",
1696 inw (io_addr
+ USBPORTSC1
), inw (io_addr
+ USBPORTSC2
), data
);
1697 urb
->complete (urb
);
1702 /*-------------------------------------------------------------------------*/
1703 /* Virtual Root Hub INTs are polled by this timer every "intervall" ms */
1704 _static
int rh_init_int_timer (urb_t
*urb
);
1706 _static
void rh_int_timer_do (unsigned long ptr
)
1709 urb_t
*urb
= (urb_t
*) ptr
;
1710 uhci_t
*uhci
= urb
->dev
->bus
->hcpriv
;
1712 if (uhci
->rh
.send
) {
1713 len
= rh_send_irq (urb
);
1715 urb
->actual_length
= len
;
1717 urb
->complete (urb
);
1720 rh_init_int_timer (urb
);
1723 /*-------------------------------------------------------------------------*/
1724 /* Root Hub INTs are polled by this timer, polling interval 20ms */
1725 /* This time is also used for URB-timeout checking */
1727 _static
int rh_init_int_timer (urb_t
*urb
)
1729 uhci_t
*uhci
= urb
->dev
->bus
->hcpriv
;
1731 uhci
->rh
.interval
= urb
->interval
;
1732 init_timer (&uhci
->rh
.rh_int_timer
);
1733 uhci
->rh
.rh_int_timer
.function
= rh_int_timer_do
;
1734 uhci
->rh
.rh_int_timer
.data
= (unsigned long) urb
;
1735 uhci
->rh
.rh_int_timer
.expires
= jiffies
+ (HZ
* 20) / 1000;
1736 add_timer (&uhci
->rh
.rh_int_timer
);
1741 /*-------------------------------------------------------------------------*/
1742 #define OK(x) len = (x); break
1744 #define CLR_RH_PORTSTAT(x) \
1745 status = inw(io_addr+USBPORTSC1+2*(wIndex-1)); \
1746 status = (status & 0xfff5) & ~(x); \
1747 outw(status, io_addr+USBPORTSC1+2*(wIndex-1))
1749 #define SET_RH_PORTSTAT(x) \
1750 status = inw(io_addr+USBPORTSC1+2*(wIndex-1)); \
1751 status = (status & 0xfff5) | (x); \
1752 outw(status, io_addr+USBPORTSC1+2*(wIndex-1))
1755 /*-------------------------------------------------------------------------*/
1757 ** Root Hub Control Pipe
1758 *************************/
1761 _static
int rh_submit_urb (urb_t
*urb
)
1763 struct usb_device
*usb_dev
= urb
->dev
;
1764 uhci_t
*uhci
= usb_dev
->bus
->hcpriv
;
1765 unsigned int pipe
= urb
->pipe
;
1766 devrequest
*cmd
= (devrequest
*) urb
->setup_packet
;
1767 void *data
= urb
->transfer_buffer
;
1768 int leni
= urb
->transfer_buffer_length
;
1773 unsigned int io_addr
= uhci
->io_addr
;
1781 if (usb_pipetype (pipe
) == PIPE_INTERRUPT
) {
1782 dbg("Root-Hub submit IRQ: every %d ms", urb
->interval
);
1785 uhci
->rh
.interval
= urb
->interval
;
1786 rh_init_int_timer (urb
);
1792 bmRType_bReq
= cmd
->requesttype
| cmd
->request
<< 8;
1793 wValue
= le16_to_cpu (cmd
->value
);
1794 wIndex
= le16_to_cpu (cmd
->index
);
1795 wLength
= le16_to_cpu (cmd
->length
);
1797 for (i
= 0; i
< 8; i
++)
1798 uhci
->rh
.c_p_r
[i
] = 0;
1800 dbg("Root-Hub: adr: %2x cmd(%1x): %04x %04x %04x %04x",
1801 uhci
->rh
.devnum
, 8, bmRType_bReq
, wValue
, wIndex
, wLength
);
1803 switch (bmRType_bReq
) {
1804 /* Request Destination:
1805 without flags: Device,
1806 RH_INTERFACE: interface,
1807 RH_ENDPOINT: endpoint,
1808 RH_CLASS means HUB here,
1809 RH_OTHER | RH_CLASS almost ever means HUB_PORT here
1813 *(__u16
*) data
= cpu_to_le16 (1);
1815 case RH_GET_STATUS
| RH_INTERFACE
:
1816 *(__u16
*) data
= cpu_to_le16 (0);
1818 case RH_GET_STATUS
| RH_ENDPOINT
:
1819 *(__u16
*) data
= cpu_to_le16 (0);
1821 case RH_GET_STATUS
| RH_CLASS
:
1822 *(__u32
*) data
= cpu_to_le32 (0);
1823 OK (4); /* hub power ** */
1824 case RH_GET_STATUS
| RH_OTHER
| RH_CLASS
:
1825 status
= inw (io_addr
+ USBPORTSC1
+ 2 * (wIndex
- 1));
1826 cstatus
= ((status
& USBPORTSC_CSC
) >> (1 - 0)) |
1827 ((status
& USBPORTSC_PEC
) >> (3 - 1)) |
1828 (uhci
->rh
.c_p_r
[wIndex
- 1] << (0 + 4));
1829 status
= (status
& USBPORTSC_CCS
) |
1830 ((status
& USBPORTSC_PE
) >> (2 - 1)) |
1831 ((status
& USBPORTSC_SUSP
) >> (12 - 2)) |
1832 ((status
& USBPORTSC_PR
) >> (9 - 4)) |
1833 (1 << 8) | /* power on ** */
1834 ((status
& USBPORTSC_LSDA
) << (-8 + 9));
1836 *(__u16
*) data
= cpu_to_le16 (status
);
1837 *(__u16
*) (data
+ 2) = cpu_to_le16 (cstatus
);
1840 case RH_CLEAR_FEATURE
| RH_ENDPOINT
:
1842 case (RH_ENDPOINT_STALL
):
1847 case RH_CLEAR_FEATURE
| RH_CLASS
:
1849 case (RH_C_HUB_OVER_CURRENT
):
1850 OK (0); /* hub power over current ** */
1854 case RH_CLEAR_FEATURE
| RH_OTHER
| RH_CLASS
:
1856 case (RH_PORT_ENABLE
):
1857 CLR_RH_PORTSTAT (USBPORTSC_PE
);
1859 case (RH_PORT_SUSPEND
):
1860 CLR_RH_PORTSTAT (USBPORTSC_SUSP
);
1862 case (RH_PORT_POWER
):
1863 OK (0); /* port power ** */
1864 case (RH_C_PORT_CONNECTION
):
1865 SET_RH_PORTSTAT (USBPORTSC_CSC
);
1867 case (RH_C_PORT_ENABLE
):
1868 SET_RH_PORTSTAT (USBPORTSC_PEC
);
1870 case (RH_C_PORT_SUSPEND
):
1871 /*** WR_RH_PORTSTAT(RH_PS_PSSC); */
1873 case (RH_C_PORT_OVER_CURRENT
):
1874 OK (0); /* port power over current ** */
1875 case (RH_C_PORT_RESET
):
1876 uhci
->rh
.c_p_r
[wIndex
- 1] = 0;
1881 case RH_SET_FEATURE
| RH_OTHER
| RH_CLASS
:
1883 case (RH_PORT_SUSPEND
):
1884 SET_RH_PORTSTAT (USBPORTSC_SUSP
);
1886 case (RH_PORT_RESET
):
1887 SET_RH_PORTSTAT (USBPORTSC_PR
);
1889 uhci
->rh
.c_p_r
[wIndex
- 1] = 1;
1890 CLR_RH_PORTSTAT (USBPORTSC_PR
);
1892 SET_RH_PORTSTAT (USBPORTSC_PE
);
1894 SET_RH_PORTSTAT (0xa);
1896 case (RH_PORT_POWER
):
1897 OK (0); /* port power ** */
1898 case (RH_PORT_ENABLE
):
1899 SET_RH_PORTSTAT (USBPORTSC_PE
);
1904 case RH_SET_ADDRESS
:
1905 uhci
->rh
.devnum
= wValue
;
1908 case RH_GET_DESCRIPTOR
:
1909 switch ((wValue
& 0xff00) >> 8) {
1910 case (0x01): /* device descriptor */
1911 len
= min (leni
, min (sizeof (root_hub_dev_des
), wLength
));
1912 memcpy (data
, root_hub_dev_des
, len
);
1914 case (0x02): /* configuration descriptor */
1915 len
= min (leni
, min (sizeof (root_hub_config_des
), wLength
));
1916 memcpy (data
, root_hub_config_des
, len
);
1918 case (0x03): /*string descriptors */
1923 case RH_GET_DESCRIPTOR
| RH_CLASS
:
1924 root_hub_hub_des
[2] = uhci
->rh
.numports
;
1925 len
= min (leni
, min (sizeof (root_hub_hub_des
), wLength
));
1926 memcpy (data
, root_hub_hub_des
, len
);
1929 case RH_GET_CONFIGURATION
:
1930 *(__u8
*) data
= 0x01;
1933 case RH_SET_CONFIGURATION
:
1939 dbg("Root-Hub stat port1: %x port2: %x",
1940 inw (io_addr
+ USBPORTSC1
), inw (io_addr
+ USBPORTSC2
));
1942 urb
->actual_length
= len
;
1945 urb
->complete (urb
);
1948 /*-------------------------------------------------------------------------*/
1950 _static
int rh_unlink_urb (urb_t
*urb
)
1952 uhci_t
*uhci
= urb
->dev
->bus
->hcpriv
;
1954 if (uhci
->rh
.urb
==urb
) {
1955 dbg("Root-Hub unlink IRQ");
1957 del_timer (&uhci
->rh
.rh_int_timer
);
1961 /*-------------------------------------------------------------------*/
1964 * Map status to standard result codes
1966 * <status> is (td->status & 0xFE0000) [a.k.a. uhci_status_bits(td->status)
1967 * <dir_out> is True for output TDs and False for input TDs.
1969 _static
int uhci_map_status (int status
, int dir_out
)
1973 if (status
& TD_CTRL_BITSTUFF
) /* Bitstuff error */
1975 if (status
& TD_CTRL_CRCTIMEO
) { /* CRC/Timeout */
1981 if (status
& TD_CTRL_NAK
) /* NAK */
1983 if (status
& TD_CTRL_BABBLE
) /* Babble */
1985 if (status
& TD_CTRL_DBUFERR
) /* Buffer error */
1987 if (status
& TD_CTRL_STALLED
) /* Stalled */
1989 if (status
& TD_CTRL_ACTIVE
) /* Active */
1996 * Only the USB core should call uhci_alloc_dev and uhci_free_dev
1998 _static
int uhci_alloc_dev (struct usb_device
*usb_dev
)
2003 _static
void uhci_unlink_urbs(uhci_t
*s
, struct usb_device
*usb_dev
, int remove_all
)
2005 unsigned long flags
;
2006 struct list_head
*p
;
2007 struct list_head
*p2
;
2010 spin_lock_irqsave (&s
->urb_list_lock
, flags
);
2011 p
= s
->urb_list
.prev
;
2012 while (p
!= &s
->urb_list
) {
2015 urb
= list_entry (p2
, urb_t
, urb_list
);
2016 dbg("urb: %p, dev %p, %p", urb
, usb_dev
,urb
->dev
);
2018 //urb->transfer_flags |=USB_ASYNC_UNLINK;
2020 if (remove_all
|| (usb_dev
== urb
->dev
)) {
2021 spin_unlock_irqrestore (&s
->urb_list_lock
, flags
);
2022 warn("forced removing of queued URB %p due to disconnect",urb
);
2023 uhci_unlink_urb(urb
);
2024 urb
->dev
= NULL
; // avoid further processing of this UR
2025 spin_lock_irqsave (&s
->urb_list_lock
, flags
);
2026 p
= s
->urb_list
.prev
;
2029 spin_unlock_irqrestore (&s
->urb_list_lock
, flags
);
2032 _static
int uhci_free_dev (struct usb_device
*usb_dev
)
2037 if(!usb_dev
|| !usb_dev
->bus
|| !usb_dev
->bus
->hcpriv
)
2040 s
=(uhci_t
*) usb_dev
->bus
->hcpriv
;
2041 uhci_unlink_urbs(s
, usb_dev
, 0);
2047 * uhci_get_current_frame_number()
2049 * returns the current frame number for a USB bus/controller.
2051 _static
int uhci_get_current_frame_number (struct usb_device
*usb_dev
)
2053 return UHCI_GET_CURRENT_FRAME ((uhci_t
*) usb_dev
->bus
->hcpriv
);
2056 struct usb_operations uhci_device_operations
=
2060 uhci_get_current_frame_number
,
2066 * For IN-control transfers, process_transfer gets a bit more complicated,
2067 * since there are devices that return less data (eg. strings) than they
2068 * have announced. This leads to a queue abort due to the short packet,
2069 * the status stage is not executed. If this happens, the status stage
2070 * is manually re-executed.
2071 * mode: 0: QHs already unlinked
2074 _static
int process_transfer (uhci_t
*s
, urb_t
*urb
, int mode
)
2077 urb_priv_t
*urb_priv
= urb
->hcpriv
;
2078 struct list_head
*qhl
= urb_priv
->desc_list
.next
;
2079 uhci_desc_t
*qh
= list_entry (qhl
, uhci_desc_t
, desc_list
);
2080 struct list_head
*p
= qh
->vertical
.next
;
2081 uhci_desc_t
*desc
= list_entry (urb_priv
->desc_list
.prev
, uhci_desc_t
, desc_list
);
2082 uhci_desc_t
*last_desc
= list_entry (desc
->vertical
.prev
, uhci_desc_t
, vertical
);
2083 int data_toggle
= usb_gettoggle (urb
->dev
, usb_pipeendpoint (urb
->pipe
), usb_pipeout (urb
->pipe
)); // save initial data_toggle
2084 int maxlength
; // extracted and remapped info from TD
2088 //dbg("process_transfer: urb contains bulk/control request");
2090 /* if the status phase has been retriggered and the
2091 queue is empty or the last status-TD is inactive, the retriggered
2092 status stage is completed
2095 if (urb_priv
->short_control_packet
&&
2096 ((qh
->hw
.qh
.element
== UHCI_PTR_TERM
) ||(!(last_desc
->hw
.td
.status
& TD_CTRL_ACTIVE
))))
2097 goto transfer_finished
;
2099 urb
->actual_length
=0;
2101 for (; p
!= &qh
->vertical
; p
= p
->next
) {
2102 desc
= list_entry (p
, uhci_desc_t
, vertical
);
2104 if (desc
->hw
.td
.status
& TD_CTRL_ACTIVE
) // do not process active TDs
2107 actual_length
= (desc
->hw
.td
.status
+ 1) & 0x7ff; // extract transfer parameters from TD
2108 maxlength
= (((desc
->hw
.td
.info
>> 21) & 0x7ff) + 1) & 0x7ff;
2109 status
= uhci_map_status (uhci_status_bits (desc
->hw
.td
.status
), usb_pipeout (urb
->pipe
));
2111 if (status
== -EPIPE
) { // see if EP is stalled
2112 // set up stalled condition
2113 usb_endpoint_halt (urb
->dev
, usb_pipeendpoint (urb
->pipe
), usb_pipeout (urb
->pipe
));
2116 if (status
!= 0) { // if any error occured stop processing of further TDs
2117 // only set ret if status returned an error
2118 if (status
!= -EPIPE
)
2119 uhci_show_td (desc
);
2124 else if ((desc
->hw
.td
.info
& 0xff) != USB_PID_SETUP
)
2125 urb
->actual_length
+= actual_length
;
2127 // got less data than requested
2128 if ( (actual_length
< maxlength
)) {
2129 if (urb
->transfer_flags
& USB_DISABLE_SPD
) {
2130 status
= -EREMOTEIO
; // treat as real error
2131 dbg("process_transfer: SPD!!");
2132 break; // exit after this TD because SP was detected
2135 // short read during control-IN: re-start status stage
2136 if ((usb_pipetype (urb
->pipe
) == PIPE_CONTROL
)) {
2137 if (uhci_packetid(last_desc
->hw
.td
.info
) == USB_PID_OUT
) {
2139 qh
->hw
.qh
.element
= virt_to_bus (last_desc
); // re-trigger status stage
2140 dbg("short packet during control transfer, retrigger status stage @ %p",last_desc
);
2141 //uhci_show_td (desc);
2142 //uhci_show_td (last_desc);
2143 urb_priv
->short_control_packet
=1;
2147 // all other cases: short read is OK
2148 data_toggle
= uhci_toggle (desc
->hw
.td
.info
);
2152 data_toggle
= uhci_toggle (desc
->hw
.td
.info
);
2153 queue_dbg("process_transfer: len:%d status:%x mapped:%x toggle:%d", actual_length
, desc
->hw
.td
.status
,status
, data_toggle
);
2157 usb_settoggle (urb
->dev
, usb_pipeendpoint (urb
->pipe
), usb_pipeout (urb
->pipe
), !data_toggle
);
2161 uhci_clean_transfer(s
, urb
, qh
, (mode
==0?2:1));
2163 urb
->status
= status
;
2165 #ifdef CONFIG_USB_UHCI_HIGH_BANDWIDTH
2166 disable_desc_loop(s
,urb
);
2169 queue_dbg("process_transfer: (end) urb %p, wanted len %d, len %d status %x err %d",
2170 urb
,urb
->transfer_buffer_length
,urb
->actual_length
, urb
->status
, urb
->error_count
);
2174 _static
int process_interrupt (uhci_t
*s
, urb_t
*urb
)
2176 int i
, ret
= -EINPROGRESS
;
2177 urb_priv_t
*urb_priv
= urb
->hcpriv
;
2178 struct list_head
*p
= urb_priv
->desc_list
.next
;
2179 uhci_desc_t
*desc
= list_entry (urb_priv
->desc_list
.prev
, uhci_desc_t
, desc_list
);
2184 //dbg("urb contains interrupt request");
2186 for (i
= 0; p
!= &urb_priv
->desc_list
; p
= p
->next
, i
++) // Maybe we allow more than one TD later ;-)
2188 desc
= list_entry (p
, uhci_desc_t
, desc_list
);
2190 if (desc
->hw
.td
.status
& TD_CTRL_ACTIVE
) {
2191 // do not process active TDs
2192 //dbg("TD ACT Status @%p %08x",desc,desc->hw.td.status);
2196 if (!desc
->hw
.td
.status
& TD_CTRL_IOC
) {
2197 // do not process one-shot TDs, no recycling
2200 // extract transfer parameters from TD
2202 actual_length
= (desc
->hw
.td
.status
+ 1) & 0x7ff;
2203 status
= uhci_map_status (uhci_status_bits (desc
->hw
.td
.status
), usb_pipeout (urb
->pipe
));
2205 // see if EP is stalled
2206 if (status
== -EPIPE
) {
2207 // set up stalled condition
2208 usb_endpoint_halt (urb
->dev
, usb_pipeendpoint (urb
->pipe
), usb_pipeout (urb
->pipe
));
2211 // if any error occured: ignore this td, and continue
2213 //uhci_show_td (desc);
2218 urb
->actual_length
= actual_length
;
2221 if (urb
->complete
) {
2222 //dbg("process_interrupt: calling completion, status %i",status);
2223 urb
->status
= status
;
2225 spin_unlock(&s
->urb_list_lock
);
2227 urb
->complete ((struct urb
*) urb
);
2229 spin_lock(&s
->urb_list_lock
);
2231 urb
->status
= -EINPROGRESS
;
2234 // Recycle INT-TD if interval!=0, else mark TD as one-shot
2235 if (urb
->interval
) {
2237 desc
->hw
.td
.info
&= ~(1 << TD_TOKEN_TOGGLE
);
2239 ((urb_priv_t
*)urb
->hcpriv
)->started
=jiffies
;
2240 desc
->hw
.td
.info
|= (usb_gettoggle (urb
->dev
, usb_pipeendpoint (urb
->pipe
),
2241 usb_pipeout (urb
->pipe
)) << TD_TOKEN_TOGGLE
);
2242 usb_dotoggle (urb
->dev
, usb_pipeendpoint (urb
->pipe
), usb_pipeout (urb
->pipe
));
2244 desc
->hw
.td
.info
|= (!usb_gettoggle (urb
->dev
, usb_pipeendpoint (urb
->pipe
),
2245 usb_pipeout (urb
->pipe
)) << TD_TOKEN_TOGGLE
);
2247 desc
->hw
.td
.status
= (urb
->pipe
& TD_CTRL_LS
) | TD_CTRL_ACTIVE
| TD_CTRL_IOC
|
2248 (urb
->transfer_flags
& USB_DISABLE_SPD
? 0 : TD_CTRL_SPD
) | (3 << 27);
2252 desc
->hw
.td
.status
&= ~TD_CTRL_IOC
; // inactivate TD
2259 // mode: 1: force processing, don't unlink tds (already unlinked)
2260 _static
int process_iso (uhci_t
*s
, urb_t
*urb
, int mode
)
2264 urb_priv_t
*urb_priv
= urb
->hcpriv
;
2265 struct list_head
*p
= urb_priv
->desc_list
.next
;
2266 uhci_desc_t
*desc
= list_entry (urb_priv
->desc_list
.prev
, uhci_desc_t
, desc_list
);
2268 dbg("urb contains iso request");
2269 if ((desc
->hw
.td
.status
& TD_CTRL_ACTIVE
) && !mode
)
2270 return -EXDEV
; // last TD not finished
2272 urb
->error_count
= 0;
2273 urb
->actual_length
= 0;
2275 dbg("process iso urb %p, %li, %i, %i, %i %08x",urb
,jiffies
,UHCI_GET_CURRENT_FRAME(s
),
2276 urb
->number_of_packets
,mode
,desc
->hw
.td
.status
);
2278 for (i
= 0; p
!= &urb_priv
->desc_list
; p
= p
->next
, i
++) {
2279 desc
= list_entry (p
, uhci_desc_t
, desc_list
);
2281 //uhci_show_td(desc);
2282 if (desc
->hw
.td
.status
& TD_CTRL_ACTIVE
) {
2283 // means we have completed the last TD, but not the TDs before
2284 desc
->hw
.td
.status
&= ~TD_CTRL_ACTIVE
;
2285 dbg("TD still active (%x)- grrr. paranoia!", desc
->hw
.td
.status
);
2287 urb
->iso_frame_desc
[i
].status
= ret
;
2288 unlink_td (s
, desc
, 1);
2289 // FIXME: immediate deletion may be dangerous
2294 unlink_td (s
, desc
, 1);
2296 if (urb
->number_of_packets
<= i
) {
2297 dbg("urb->number_of_packets (%d)<=(%d)", urb
->number_of_packets
, i
);
2302 if (urb
->iso_frame_desc
[i
].offset
+ urb
->transfer_buffer
!= bus_to_virt (desc
->hw
.td
.buffer
)) {
2303 // Hm, something really weird is going on
2304 dbg("Pointer Paranoia: %p!=%p", urb
->iso_frame_desc
[i
].offset
+ urb
->transfer_buffer
, bus_to_virt (desc
->hw
.td
.buffer
));
2306 urb
->iso_frame_desc
[i
].status
= ret
;
2309 urb
->iso_frame_desc
[i
].actual_length
= (desc
->hw
.td
.status
+ 1) & 0x7ff;
2310 urb
->iso_frame_desc
[i
].status
= uhci_map_status (uhci_status_bits (desc
->hw
.td
.status
), usb_pipeout (urb
->pipe
));
2311 urb
->actual_length
+= urb
->iso_frame_desc
[i
].actual_length
;
2315 if (urb
->iso_frame_desc
[i
].status
!= 0) {
2317 urb
->status
= urb
->iso_frame_desc
[i
].status
;
2319 dbg("process_iso: %i: len:%d %08x status:%x",
2320 i
, urb
->iso_frame_desc
[i
].actual_length
, desc
->hw
.td
.status
,urb
->iso_frame_desc
[i
].status
);
2326 dbg("process_iso: exit %i (%d), actual_len %i", i
, ret
,urb
->actual_length
);
2331 _static
int process_urb (uhci_t
*s
, struct list_head
*p
)
2337 urb
=list_entry (p
, urb_t
, urb_list
);
2338 //dbg("process_urb: found queued urb: %p", urb);
2340 switch (usb_pipetype (urb
->pipe
)) {
2342 ret
= process_transfer (s
, urb
, 1);
2345 if (!s
->avoid_bulk
.counter
)
2346 ret
= process_transfer (s
, urb
, 1);
2350 case PIPE_ISOCHRONOUS
:
2351 ret
= process_iso (s
, urb
, 0);
2353 case PIPE_INTERRUPT
:
2354 ret
= process_interrupt (s
, urb
);
2358 if (urb
->status
!= -EINPROGRESS
) {
2361 dbg("dequeued urb: %p", urb
);
2362 dequeue_urb (s
, urb
);
2365 kmem_cache_free(urb_priv_kmem
, urb
->hcpriv
);
2367 kfree (urb
->hcpriv
);
2370 if ((usb_pipetype (urb
->pipe
) != PIPE_INTERRUPT
)) {
2371 urb_t
*tmp
= urb
->next
; // pointer to first urb
2376 if (tmp
->status
!= -EINPROGRESS
) {
2382 while (tmp
!= NULL
&& tmp
!= urb
->next
);
2383 if (tmp
== urb
->next
)
2387 spin_unlock(&s
->urb_list_lock
);
2389 // In case you need the current URB status for your completion handler
2390 if (urb
->complete
&& (!proceed
|| (urb
->transfer_flags
& USB_URB_EARLY_COMPLETE
))) {
2391 dbg("process_transfer: calling early completion");
2392 urb
->complete ((struct urb
*) urb
);
2393 if (!proceed
&& is_ring
&& (urb
->status
!= -ENOENT
))
2394 uhci_submit_urb (urb
);
2397 if (proceed
&& urb
->next
) {
2398 // if there are linked urbs - handle submitting of them right now.
2399 tmp
= urb
->next
; // pointer to first urb
2402 if ((tmp
->status
!= -EINPROGRESS
) && (tmp
->status
!= -ENOENT
) && uhci_submit_urb (tmp
) != 0)
2406 while (tmp
!= NULL
&& tmp
!= urb
->next
); // submit until we reach NULL or our own pointer or submit fails
2408 if (urb
->complete
&& !(urb
->transfer_flags
& USB_URB_EARLY_COMPLETE
)) {
2409 dbg("process_transfer: calling completion");
2410 urb
->complete ((struct urb
*) urb
);
2414 spin_lock(&s
->urb_list_lock
);
2416 usb_dec_dev_use (urb
->dev
);
2423 _static
void uhci_interrupt (int irq
, void *__uhci
, struct pt_regs
*regs
)
2426 unsigned int io_addr
= s
->io_addr
;
2427 unsigned short status
;
2428 struct list_head
*p
, *p2
;
2431 * Read the interrupt status, and write it back to clear the
2435 status
= inw (io_addr
+ USBSTS
);
2437 if (!status
) /* shared interrupt, not mine */
2443 warn("interrupt, status %x, frame# %i", status
,
2444 UHCI_GET_CURRENT_FRAME(s
));
2446 // remove host controller halted state
2447 if ((status
&0x20) && (s
->running
)) {
2448 outw (USBCMD_RS
| inw(io_addr
+ USBCMD
), io_addr
+ USBCMD
);
2450 //uhci_show_status (s);
2453 * traverse the list in *reverse* direction, because new entries
2454 * may be added at the end.
2455 * also, because process_urb may unlink the current urb,
2456 * we need to advance the list before
2459 spin_lock (&s
->urb_list_lock
);
2461 s
->unlink_urb_done
=0;
2462 p
= s
->urb_list
.prev
;
2464 while (p
!= &s
->urb_list
) {
2467 process_urb (s
, p2
);
2468 if (s
->unlink_urb_done
) {
2469 s
->unlink_urb_done
=0;
2473 if ((s
->frame_counter
& 63) == 0)
2474 uhci_check_timeouts(s
);
2477 uhci_cleanup_unlink(s
, 0);
2479 spin_unlock (&s
->urb_list_lock
);
2482 outw (status
, io_addr
+ USBSTS
);
2484 //dbg("uhci_interrupt: done");
2487 _static
void reset_hc (uhci_t
*s
)
2489 unsigned int io_addr
= s
->io_addr
;
2492 /* Global reset for 50ms */
2493 outw (USBCMD_GRESET
, io_addr
+ USBCMD
);
2495 outw (0, io_addr
+ USBCMD
);
2499 _static
void start_hc (uhci_t
*s
)
2501 unsigned int io_addr
= s
->io_addr
;
2505 * Reset the HC - this will force us to get a
2506 * new notification of any already connected
2507 * ports due to the virtual disconnect that it
2510 outw (USBCMD_HCRESET
, io_addr
+ USBCMD
);
2512 while (inw (io_addr
+ USBCMD
) & USBCMD_HCRESET
) {
2514 err("USBCMD_HCRESET timed out!");
2519 /* Turn on all interrupts */
2520 outw (USBINTR_TIMEOUT
| USBINTR_RESUME
| USBINTR_IOC
| USBINTR_SP
, io_addr
+ USBINTR
);
2522 /* Start at frame 0 */
2523 outw (0, io_addr
+ USBFRNUM
);
2524 outl (virt_to_bus (s
->framelist
), io_addr
+ USBFLBASEADD
);
2526 /* Run and mark it configured with a 64-byte max packet */
2527 outw (USBCMD_RS
| USBCMD_CF
| USBCMD_MAXP
, io_addr
+ USBCMD
);
2532 _static
void __exit
uhci_cleanup_dev(uhci_t
*s
)
2534 struct usb_device
*root_hub
= s
->bus
->root_hub
;
2536 s
->running
= 0; // Don't allow submit_urb
2539 usb_disconnect (&root_hub
);
2544 uhci_unlink_urbs (s
, 0, 1); // Forced unlink of remaining URBs
2545 uhci_cleanup_unlink (s
, 1); // force cleanup of async killed URBs
2547 usb_deregister_bus (s
->bus
);
2549 release_region (s
->io_addr
, s
->io_size
);
2550 free_irq (s
->irq
, s
);
2551 usb_free_bus (s
->bus
);
2556 _static
int __init
uhci_start_usb (uhci_t
*s
)
2558 /* connect the virtual root hub */
2559 struct usb_device
*usb_dev
;
2561 usb_dev
= usb_alloc_dev (NULL
, s
->bus
);
2565 s
->bus
->root_hub
= usb_dev
;
2566 usb_connect (usb_dev
);
2568 if (usb_new_device (usb_dev
) != 0) {
2569 usb_free_dev (usb_dev
);
2576 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,3,44)
2577 _static
int handle_pm_event (struct pm_dev
*dev
, pm_request_t rqst
, void *data
)
2579 uhci_t
*s
= (uhci_t
*) dev
->data
;
2580 dbg("handle_apm_event(%d)", rqst
);
2595 _static
int __init
alloc_uhci (struct pci_dev
*dev
, int irq
, unsigned int io_addr
, unsigned int io_size
)
2598 struct usb_bus
*bus
;
2599 struct pm_dev
*pmdev
;
2601 s
= kmalloc (sizeof (uhci_t
), GFP_KERNEL
);
2605 memset (s
, 0, sizeof (uhci_t
));
2606 INIT_LIST_HEAD (&s
->free_desc
);
2607 INIT_LIST_HEAD (&s
->urb_list
);
2608 INIT_LIST_HEAD (&s
->urb_unlinked
);
2609 spin_lock_init (&s
->urb_list_lock
);
2610 spin_lock_init (&s
->qh_lock
);
2611 spin_lock_init (&s
->td_lock
);
2612 atomic_set(&s
->avoid_bulk
, 0);
2614 s
->io_addr
= io_addr
;
2615 s
->io_size
= io_size
;
2616 s
->next
= devs
; //chain new uhci device into global list
2617 s
->frame_counter
= 0;
2619 bus
= usb_alloc_bus (&uhci_device_operations
);
2628 /* UHCI specs says devices must have 2 ports, but goes on to say */
2629 /* they may have more but give no way to determine how many they */
2630 /* have, so default to 2 */
2631 /* According to the UHCI spec, Bit 7 is always set to 1. So we try */
2632 /* to use this to our advantage */
2634 for (s
->maxports
= 0; s
->maxports
< (io_size
- 0x10) / 2; s
->maxports
++) {
2635 unsigned int portstatus
;
2637 portstatus
= inw (io_addr
+ 0x10 + (s
->maxports
* 2));
2638 dbg("port %i, adr %x status %x", s
->maxports
,
2639 io_addr
+ 0x10 + (s
->maxports
* 2), portstatus
);
2640 if (!(portstatus
& 0x0080))
2643 warn("Detected %d ports", s
->maxports
);
2645 /* This is experimental so anything less than 2 or greater than 8 is */
2646 /* something weird and we'll ignore it */
2647 if (s
->maxports
< 2 || s
->maxports
> 8) {
2648 dbg("Port count misdetected, forcing to 2 ports");
2652 s
->rh
.numports
= s
->maxports
;
2654 if (init_skel (s
)) {
2660 request_region (s
->io_addr
, io_size
, MODNAME
);
2662 usb_register_bus (s
->bus
);
2666 if (request_irq (irq
, uhci_interrupt
, SA_SHIRQ
, MODNAME
, s
)) {
2667 err("request_irq %d failed!",irq
);
2670 release_region (s
->io_addr
, s
->io_size
);
2678 if(uhci_start_usb (s
) < 0) {
2679 uhci_cleanup_dev(s
);
2683 //chain new uhci device into global list
2685 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,3,44)
2686 pmdev
= pm_register(PM_PCI_DEV
, PM_PCI_ID(dev
), handle_pm_event
);
2693 _static
int __init
start_uhci (struct pci_dev
*dev
)
2697 /* Search for the IO base address.. */
2698 for (i
= 0; i
< 6; i
++) {
2699 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,3,8)
2700 unsigned int io_addr
= dev
->resource
[i
].start
;
2701 unsigned int io_size
=
2702 dev
->resource
[i
].end
- dev
->resource
[i
].start
+ 1;
2703 if (!(dev
->resource
[i
].flags
& 1))
2706 unsigned int io_addr
= dev
->base_address
[i
];
2707 unsigned int io_size
= 0x14;
2713 /* Is it already in use? */
2714 if (check_region (io_addr
, io_size
))
2716 /* disable legacy emulation */
2717 pci_write_config_word (dev
, USBLEGSUP
, USBLEGSUP_DEFAULT
);
2718 if(dev
->vendor
==0x8086) {
2719 info("Intel USB controller: setting latency timer to %d", UHCI_LATENCY_TIMER
);
2720 pci_write_config_byte(dev
, PCI_LATENCY_TIMER
, UHCI_LATENCY_TIMER
);
2722 return alloc_uhci(dev
, dev
->irq
, io_addr
, io_size
);
2727 int __init
uhci_init (void)
2729 int retval
= -ENODEV
;
2730 struct pci_dev
*dev
= NULL
;
2736 uhci_desc_kmem
= kmem_cache_create("uhci_desc", sizeof(uhci_desc_t
), 0, SLAB_HWCACHE_ALIGN
, NULL
, NULL
);
2738 if(!uhci_desc_kmem
) {
2739 err("kmem_cache_create for uhci_desc failed (out of memory)");
2743 urb_priv_kmem
= kmem_cache_create("urb_priv", sizeof(urb_priv_t
), 0, SLAB_HWCACHE_ALIGN
, NULL
, NULL
);
2745 if(!urb_priv_kmem
) {
2746 err("kmem_cache_create for urb_priv_t failed (out of memory)");
2752 #ifdef CONFIG_USB_UHCI_HIGH_BANDWIDTH
2753 info("High bandwidth mode enabled");
2756 dev
= pci_find_class (PCI_CLASS_SERIAL_USB
<< 8, dev
);
2761 pci_read_config_byte (dev
, PCI_CLASS_PROG
, &type
);
2765 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,3,8)
2766 if (pci_enable_device (dev
) < 0)
2771 err("Found UHCI device with no IRQ assigned. Check BIOS settings!");
2776 retval
= start_uhci (dev
);
2785 void __exit
uhci_cleanup (void)
2788 while ((s
= devs
)) {
2790 uhci_cleanup_dev(s
);
2793 if(kmem_cache_destroy(uhci_desc_kmem
))
2794 err("uhci_desc_kmem remained");
2796 if(kmem_cache_destroy(urb_priv_kmem
))
2797 err("urb_priv_kmem remained");
2802 int init_module (void)
2804 return uhci_init ();
2807 void cleanup_module (void)
2809 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,3,44)
2810 pm_unregister_all (handle_pm_event
);