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: uhci.c,v 1.149 1999/12/26 20:57:14 acher Exp $
18 #include <linux/config.h>
19 #include <linux/module.h>
20 #include <linux/pci.h>
21 #include <linux/kernel.h>
22 #include <linux/delay.h>
23 #include <linux/ioport.h>
24 #include <linux/sched.h>
25 #include <linux/malloc.h>
26 #include <linux/smp_lock.h>
27 #include <linux/errno.h>
28 #include <linux/unistd.h>
29 #include <linux/interrupt.h> /* for in_interrupt() */
30 #include <linux/init.h>
32 #include <asm/uaccess.h>
35 #include <asm/system.h>
36 //#include <asm/spinlock.h>
40 #include "uhci-debug.h"
44 #define dbg(format, args...) printk(format, ## args)
46 #define dbg(format, args...)
49 #define _static static
51 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,3,0)
57 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,3,0)
58 extern long __kernel_thread (unsigned long, int (*)(void *), void *);
59 static inline long kernel_thread (int (*fn
) (void *), void *arg
, unsigned long flags
)
61 return __kernel_thread (flags
| CLONE_VM
, fn
, arg
);
68 #include <linux/apm_bios.h>
69 static int handle_apm_event (apm_event_t event
);
72 /* We added an UHCI_SLAB slab support just for debugging purposes. In real
73 life this compile option is NOT recommended, because slab caches are not
79 static kmem_cache_t
*uhci_desc_kmem
;
80 static kmem_cache_t
*urb_priv_kmem
;
83 static int rh_submit_urb (purb_t purb
);
84 static int rh_unlink_urb (purb_t purb
);
85 static puhci_t devs
= NULL
;
87 /*-------------------------------------------------------------------*/
88 _static
void queue_urb (puhci_t s
, struct list_head
*p
, int do_lock
)
90 unsigned long flags
=0;
93 spin_lock_irqsave (&s
->urb_list_lock
, flags
);
95 list_add_tail (p
, &s
->urb_list
);
98 spin_unlock_irqrestore (&s
->urb_list_lock
, flags
);
101 /*-------------------------------------------------------------------*/
102 _static
void dequeue_urb (puhci_t s
, struct list_head
*p
, int do_lock
)
104 unsigned long flags
=0;
107 spin_lock_irqsave (&s
->urb_list_lock
, flags
);
112 spin_unlock_irqrestore (&s
->urb_list_lock
, flags
);
115 /*-------------------------------------------------------------------*/
116 _static
int alloc_td (puhci_desc_t
* new, int flags
)
119 *new= kmem_cache_alloc(uhci_desc_kmem
, in_interrupt ()? SLAB_ATOMIC
: SLAB_KERNEL
);
121 *new = (uhci_desc_t
*) kmalloc (sizeof (uhci_desc_t
), in_interrupt ()? GFP_ATOMIC
: GFP_KERNEL
);
126 memset (*new, 0, sizeof (uhci_desc_t
));
127 (*new)->hw
.td
.link
= UHCI_PTR_TERM
| (flags
& UHCI_PTR_BITS
); // last by default
129 (*new)->type
= TD_TYPE
;
130 INIT_LIST_HEAD (&(*new)->vertical
);
131 INIT_LIST_HEAD (&(*new)->horizontal
);
135 /*-------------------------------------------------------------------*/
136 /* insert td at last position in td-list of qh (vertical) */
137 _static
int insert_td (puhci_t s
, puhci_desc_t qh
, puhci_desc_t
new, int flags
)
142 spin_lock_irqsave (&s
->td_lock
, xxx
);
144 list_add_tail (&new->vertical
, &qh
->vertical
);
146 if (qh
->hw
.qh
.element
& UHCI_PTR_TERM
) {
147 // virgin qh without any tds
148 qh
->hw
.qh
.element
= virt_to_bus (new); /* QH's cannot have the DEPTH bit set */
151 // already tds inserted
152 prev
= list_entry (new->vertical
.prev
, uhci_desc_t
, vertical
);
153 // implicitely remove TERM bit of prev
154 prev
->hw
.td
.link
= virt_to_bus (new) | (flags
& UHCI_PTR_DEPTH
);
157 spin_unlock_irqrestore (&s
->td_lock
, xxx
);
161 /*-------------------------------------------------------------------*/
162 /* insert new_td after td (horizontal) */
163 _static
int insert_td_horizontal (puhci_t s
, puhci_desc_t td
, puhci_desc_t
new, int flags
)
168 spin_lock_irqsave (&s
->td_lock
, xxx
);
170 next
= list_entry (td
->horizontal
.next
, uhci_desc_t
, horizontal
);
171 new->hw
.td
.link
= td
->hw
.td
.link
;
172 list_add (&new->horizontal
, &td
->horizontal
);
173 td
->hw
.td
.link
= virt_to_bus (new);
175 spin_unlock_irqrestore (&s
->td_lock
, xxx
);
179 /*-------------------------------------------------------------------*/
180 _static
int unlink_td (puhci_t s
, puhci_desc_t element
)
182 uhci_desc_t
*next
, *prev
;
186 spin_lock_irqsave (&s
->td_lock
, xxx
);
188 next
= list_entry (element
->vertical
.next
, uhci_desc_t
, vertical
);
190 if (next
== element
) {
192 next
= list_entry (element
->horizontal
.next
, uhci_desc_t
, horizontal
);
193 prev
= list_entry (element
->horizontal
.prev
, uhci_desc_t
, horizontal
);
196 prev
= list_entry (element
->vertical
.prev
, uhci_desc_t
, vertical
);
199 if (prev
->type
== TD_TYPE
)
200 prev
->hw
.td
.link
= element
->hw
.td
.link
;
202 prev
->hw
.qh
.element
= element
->hw
.td
.link
;
207 list_del (&element
->vertical
);
209 list_del (&element
->horizontal
);
211 spin_unlock_irqrestore (&s
->td_lock
, xxx
);
215 /*-------------------------------------------------------------------*/
216 _static
int delete_desc (puhci_desc_t element
)
219 kmem_cache_free(uhci_desc_kmem
, element
);
225 /*-------------------------------------------------------------------*/
226 // Allocates qh element
227 _static
int alloc_qh (puhci_desc_t
* new)
230 *new= kmem_cache_alloc(uhci_desc_kmem
, in_interrupt ()? SLAB_ATOMIC
: SLAB_KERNEL
);
232 *new = (uhci_desc_t
*) kmalloc (sizeof (uhci_desc_t
), in_interrupt ()? GFP_ATOMIC
: GFP_KERNEL
);
237 memset (*new, 0, sizeof (uhci_desc_t
));
238 (*new)->hw
.qh
.head
= UHCI_PTR_TERM
;
239 (*new)->hw
.qh
.element
= UHCI_PTR_TERM
;
240 (*new)->type
= QH_TYPE
;
241 INIT_LIST_HEAD (&(*new)->horizontal
);
242 INIT_LIST_HEAD (&(*new)->vertical
);
244 dbg (KERN_DEBUG MODSTR
"Allocated qh @ %p\n", *new);
248 /*-------------------------------------------------------------------*/
249 // inserts new qh before/after the qh at pos
250 // flags: 0: insert before pos, 1: insert after pos (for low speed transfers)
251 _static
int insert_qh (puhci_t s
, puhci_desc_t pos
, puhci_desc_t
new, int flags
)
256 spin_lock_irqsave (&s
->qh_lock
, xxx
);
259 // (OLD) (POS) -> (OLD) (NEW) (POS)
260 old
= list_entry (pos
->horizontal
.prev
, uhci_desc_t
, horizontal
);
261 list_add_tail (&new->horizontal
, &pos
->horizontal
);
262 new->hw
.qh
.head
= MAKE_QH_ADDR (pos
) ;
264 if (!(old
->hw
.qh
.head
& UHCI_PTR_TERM
))
265 old
->hw
.qh
.head
= MAKE_QH_ADDR (new) ;
268 // (POS) (OLD) -> (POS) (NEW) (OLD)
269 old
= list_entry (pos
->horizontal
.next
, uhci_desc_t
, horizontal
);
270 list_add (&new->horizontal
, &pos
->horizontal
);
271 pos
->hw
.qh
.head
= MAKE_QH_ADDR (new) ;
272 new->hw
.qh
.head
= MAKE_QH_ADDR (old
);
277 spin_unlock_irqrestore (&s
->qh_lock
, xxx
);
281 /*-------------------------------------------------------------------*/
282 _static
int unlink_qh (puhci_t s
, puhci_desc_t element
)
284 puhci_desc_t next
, prev
;
287 spin_lock_irqsave (&s
->qh_lock
, xxx
);
289 next
= list_entry (element
->horizontal
.next
, uhci_desc_t
, horizontal
);
290 prev
= list_entry (element
->horizontal
.prev
, uhci_desc_t
, horizontal
);
291 prev
->hw
.qh
.head
= element
->hw
.qh
.head
;
293 list_del (&element
->horizontal
);
295 spin_unlock_irqrestore (&s
->qh_lock
, xxx
);
299 /*-------------------------------------------------------------------*/
300 _static
int delete_qh (puhci_t s
, puhci_desc_t qh
)
305 list_del (&qh
->horizontal
);
307 while ((p
= qh
->vertical
.next
) != &qh
->vertical
) {
308 td
= list_entry (p
, uhci_desc_t
, vertical
);
317 /*-------------------------------------------------------------------*/
318 void clean_td_chain (puhci_desc_t td
)
326 while ((p
= td
->horizontal
.next
) != &td
->horizontal
) {
327 td1
= list_entry (p
, uhci_desc_t
, horizontal
);
333 /*-------------------------------------------------------------------*/
334 // Removes ALL qhs in chain (paranoia!)
335 _static
void cleanup_skel (puhci_t s
)
340 printk (KERN_DEBUG MODSTR
"Cleanup_skel\n");
342 for (n
= 0; n
< 8; n
++) {
343 td
= s
->int_chain
[n
];
348 for (n
= 0; n
< 1024; n
++) {
356 free_page ((unsigned long) s
->framelist
);
358 if (s
->control_chain
) {
359 // completed init_skel?
361 puhci_desc_t qh
, qh1
;
363 qh
= s
->control_chain
;
364 while ((p
= qh
->horizontal
.next
) != &qh
->horizontal
) {
365 qh1
= list_entry (p
, uhci_desc_t
, horizontal
);
371 if (s
->control_chain
)
372 kfree (s
->control_chain
);
374 kfree (s
->bulk_chain
);
376 kfree (s
->chain_end
);
379 /*-------------------------------------------------------------------*/
380 // allocates framelist and qh-skeletons
381 // only HW-links provide continous linking, SW-links stay in their domain (ISO/INT)
382 _static
int init_skel (puhci_t s
)
387 dbg (KERN_DEBUG MODSTR
"init_skel\n");
389 s
->framelist
= (__u32
*) get_free_page (GFP_KERNEL
);
394 memset (s
->framelist
, 0, 4096);
396 dbg (KERN_DEBUG MODSTR
"allocating iso desc pointer list\n");
397 s
->iso_td
= (puhci_desc_t
*) kmalloc (1024 * sizeof (puhci_desc_t
), GFP_KERNEL
);
400 goto init_skel_cleanup
;
402 s
->control_chain
= NULL
;
403 s
->bulk_chain
= NULL
;
406 dbg (KERN_DEBUG MODSTR
"allocating iso descs\n");
407 for (n
= 0; n
< 1024; n
++) {
408 // allocate skeleton iso/irq-tds
409 ret
= alloc_td (&td
, 0);
411 goto init_skel_cleanup
;
413 s
->framelist
[n
] = ((__u32
) virt_to_bus (td
));
416 dbg (KERN_DEBUG MODSTR
"allocating qh: chain_end\n");
417 ret
= alloc_qh (&qh
);
420 goto init_skel_cleanup
;
424 dbg (KERN_DEBUG MODSTR
"allocating qh: bulk_chain\n");
425 ret
= alloc_qh (&qh
);
428 goto init_skel_cleanup
;
430 insert_qh (s
, s
->chain_end
, qh
, 0);
432 dbg (KERN_DEBUG MODSTR
"allocating qh: control_chain\n");
433 ret
= alloc_qh (&qh
);
436 goto init_skel_cleanup
;
438 insert_qh (s
, s
->bulk_chain
, qh
, 0);
439 s
->control_chain
= qh
;
440 for (n
= 0; n
< 8; n
++)
443 dbg (KERN_DEBUG MODSTR
"Allocating skeleton INT-TDs\n");
445 for (n
= 0; n
< 8; n
++) {
450 goto init_skel_cleanup
;
451 s
->int_chain
[n
] = td
;
453 s
->int_chain
[0]->hw
.td
.link
= virt_to_bus (s
->control_chain
);
456 s
->int_chain
[n
]->hw
.td
.link
= virt_to_bus (s
->int_chain
[0]);
460 dbg (KERN_DEBUG MODSTR
"Linking skeleton INT-TDs\n");
462 for (n
= 0; n
< 1024; n
++) {
463 // link all iso-tds to the interrupt chains
465 //dbg("framelist[%i]=%x\n",n,s->framelist[n]);
466 for (o
= 1, m
= 2; m
<= 128; o
++, m
+= m
) {
468 if ((n
& (m
- 1)) == ((m
- 1) / 2)) {
469 ((puhci_desc_t
) s
->iso_td
[n
])->hw
.td
.link
= virt_to_bus (s
->int_chain
[o
]);
474 //uhci_show_queue(s->control_chain);
475 dbg (KERN_DEBUG MODSTR
"init_skel exit\n");
483 /*-------------------------------------------------------------------*/
484 _static
void fill_td (puhci_desc_t td
, int status
, int info
, __u32 buffer
)
486 td
->hw
.td
.status
= status
;
487 td
->hw
.td
.info
= info
;
488 td
->hw
.td
.buffer
= buffer
;
491 /*-------------------------------------------------------------------*/
493 // assembles QHs und TDs for control, bulk and iso
494 /*-------------------------------------------------------------------*/
495 _static
int uhci_submit_control_urb (purb_t purb
)
498 puhci_t s
= (puhci_t
) purb
->dev
->bus
->hcpriv
;
499 purb_priv_t purb_priv
= purb
->hcpriv
;
500 unsigned long destination
, status
;
501 int maxsze
= usb_maxpacket (purb
->dev
, purb
->pipe
, usb_pipeout (purb
->pipe
));
502 unsigned long len
, bytesrequested
;
505 dbg (KERN_DEBUG MODSTR
"uhci_submit_control start\n");
506 alloc_qh (&qh
); // alloc qh for this request
511 alloc_td (&td
, UHCI_PTR_DEPTH
); // get td for setup stage
518 /* The "pipe" thing contains the destination in bits 8--18 */
519 destination
= (purb
->pipe
& PIPE_DEVEP_MASK
) | USB_PID_SETUP
;
522 status
= (purb
->pipe
& TD_CTRL_LS
) | TD_CTRL_ACTIVE
|
523 (purb
->transfer_flags
& USB_DISABLE_SPD
? 0 : TD_CTRL_SPD
) | (3 << 27);
525 /* Build the TD for the control request, try forever, 8 bytes of data */
526 fill_td (td
, status
, destination
| (7 << 21), virt_to_bus (purb
->setup_packet
));
528 /* If direction is "send", change the frame from SETUP (0x2D)
529 to OUT (0xE1). Else change it from SETUP to IN (0x69). */
531 destination
^= (USB_PID_SETUP
^ USB_PID_IN
); /* SETUP -> IN */
533 if (usb_pipeout (purb
->pipe
))
534 destination
^= (USB_PID_IN
^ USB_PID_OUT
); /* IN -> OUT */
536 insert_td (s
, qh
, td
, 0); // queue 'setup stage'-td in qh
538 printk ("SETUP to pipe %x: %x %x %x %x %x %x %x %x\n", purb
->pipe
,
539 purb
->setup_packet
[0], purb
->setup_packet
[1], purb
->setup_packet
[2], purb
->setup_packet
[3],
540 purb
->setup_packet
[4], purb
->setup_packet
[5], purb
->setup_packet
[6], purb
->setup_packet
[7]);
543 /* Build the DATA TD's */
544 len
= purb
->transfer_buffer_length
;
545 bytesrequested
= len
;
546 data
= purb
->transfer_buffer
;
551 alloc_td (&td
, UHCI_PTR_DEPTH
);
560 destination
^= 1 << TD_TOKEN_TOGGLE
; // toggle DATA0/1
562 fill_td (td
, status
, destination
| ((pktsze
- 1) << 21),
563 virt_to_bus (data
)); // Status, pktsze bytes of data
565 insert_td (s
, qh
, td
, UHCI_PTR_DEPTH
); // queue 'data stage'-td in qh
571 /* Build the final TD for control status */
572 /* It's only IN if the pipe is out AND we aren't expecting data */
573 destination
&= ~0xFF;
575 if (usb_pipeout (purb
->pipe
) | (bytesrequested
== 0))
576 destination
|= USB_PID_IN
;
578 destination
|= USB_PID_OUT
;
580 destination
|= 1 << TD_TOKEN_TOGGLE
; /* End in Data1 */
582 alloc_td (&td
, UHCI_PTR_DEPTH
);
589 /* no limit on errors on final packet , 0 bytes of data */
590 fill_td (td
, status
| TD_CTRL_IOC
, destination
| (UHCI_NULL_DATA_SIZE
<< 21),
593 insert_td (s
, qh
, td
, UHCI_PTR_DEPTH
); // queue status td
596 list_add (&qh
->desc_list
, &purb_priv
->desc_list
);
598 /* Start it up... put low speed first */
599 if (purb
->pipe
& TD_CTRL_LS
)
600 insert_qh (s
, s
->control_chain
, qh
, 1); // insert after control chain
602 insert_qh (s
, s
->bulk_chain
, qh
, 0); // insert before bulk chain
603 //uhci_show_queue(s->control_chain);
605 dbg (KERN_DEBUG MODSTR
"uhci_submit_control end\n");
608 /*-------------------------------------------------------------------*/
609 _static
int uhci_submit_bulk_urb (purb_t purb
)
611 puhci_t s
= (puhci_t
) purb
->dev
->bus
->hcpriv
;
612 purb_priv_t purb_priv
= purb
->hcpriv
;
614 unsigned long destination
, status
;
616 unsigned int pipe
= purb
->pipe
;
617 int maxsze
= usb_maxpacket (purb
->dev
, pipe
, usb_pipeout (pipe
));
620 /* shouldn't the clear_halt be done in the USB core or in the client driver? - Thomas */
621 if (usb_endpoint_halted (purb
->dev
, usb_pipeendpoint (pipe
), usb_pipeout (pipe
)) &&
622 usb_clear_halt (purb
->dev
, usb_pipeendpoint (pipe
) | (pipe
& USB_DIR_IN
)))
627 /* FIXME: should tell the client that the endpoint is invalid, i.e. not in the descriptor */
629 alloc_qh (&qh
); // get qh for this request
634 /* The "pipe" thing contains the destination in bits 8--18. */
635 destination
= (pipe
& PIPE_DEVEP_MASK
) | usb_packetid (pipe
);
638 status
= (pipe
& TD_CTRL_LS
) | TD_CTRL_ACTIVE
|
639 ((purb
->transfer_flags
& USB_DISABLE_SPD
) ? 0 : TD_CTRL_SPD
) | (3 << 27);
641 /* Build the TDs for the bulk request */
642 len
= purb
->transfer_buffer_length
;
643 data
= purb
->transfer_buffer
;
644 dbg (KERN_DEBUG MODSTR
"uhci_submit_bulk_urb: pipe %x, len %d\n", pipe
, len
);
649 alloc_td (&td
, UHCI_PTR_DEPTH
);
659 // pktsze bytes of data
660 info
= destination
| ((pktsze
- 1) << 21) |
661 (usb_gettoggle (purb
->dev
, usb_pipeendpoint (pipe
), usb_pipeout (pipe
)) << TD_TOKEN_TOGGLE
);
663 fill_td (td
, status
, info
, virt_to_bus (data
));
669 td
->hw
.td
.status
|= TD_CTRL_IOC
; // last one generates INT
670 //dbg("insert td %p, len %i\n",td,pktsze);
672 insert_td (s
, qh
, td
, UHCI_PTR_DEPTH
);
674 /* Alternate Data0/1 (start with Data0) */
675 usb_dotoggle (purb
->dev
, usb_pipeendpoint (pipe
), usb_pipeout (pipe
));
678 list_add (&qh
->desc_list
, &purb_priv
->desc_list
);
680 insert_qh (s
, s
->chain_end
, qh
, 0); // insert before end marker
681 //uhci_show_queue(s->bulk_chain);
683 dbg (KERN_DEBUG MODSTR
"uhci_submit_bulk_urb: exit\n");
686 /*-------------------------------------------------------------------*/
687 // unlinks an urb by dequeuing its qh, waits some frames and forgets it
688 // Problem: unlinking in interrupt requires waiting for one frame (udelay)
689 // to allow the whole structures to be safely removed
690 _static
int uhci_unlink_urb (purb_t purb
)
695 purb_priv_t purb_priv
;
696 unsigned long flags
=0;
699 if (!purb
) // you never know...
702 s
= (puhci_t
) purb
->dev
->bus
->hcpriv
; // get pointer to uhci struct
704 if (usb_pipedevice (purb
->pipe
) == s
->rh
.devnum
)
705 return rh_unlink_urb (purb
);
707 if(!in_interrupt()) {
708 // is the following really necessary? dequeue_urb has its own spinlock (GA)
709 spin_lock_irqsave (&s
->unlink_urb_lock
, flags
); // do not allow interrupts
712 //dbg("unlink_urb called %p\n",purb);
713 if (purb
->status
== USB_ST_URB_PENDING
) {
714 // URB probably still in work
715 purb_priv
= purb
->hcpriv
;
716 dequeue_urb (s
, &purb
->urb_list
,1);
717 purb
->status
= USB_ST_URB_KILLED
; // mark urb as killed
719 if(!in_interrupt()) {
720 spin_unlock_irqrestore (&s
->unlink_urb_lock
, flags
); // allow interrupts from here
723 switch (usb_pipetype (purb
->pipe
)) {
724 case PIPE_ISOCHRONOUS
:
726 for (p
= purb_priv
->desc_list
.next
; p
!= &purb_priv
->desc_list
; p
= p
->next
) {
727 td
= list_entry (p
, uhci_desc_t
, desc_list
);
730 // wait at least 1 Frame
734 schedule_timeout (1 + 1 * HZ
/ 1000);
735 while ((p
= purb_priv
->desc_list
.next
) != &purb_priv
->desc_list
) {
736 td
= list_entry (p
, uhci_desc_t
, desc_list
);
744 qh
= list_entry (purb_priv
->desc_list
.next
, uhci_desc_t
, desc_list
);
746 unlink_qh (s
, qh
); // remove this qh from qh-list
747 // wait at least 1 Frame
752 schedule_timeout (1 + 1 * HZ
/ 1000);
753 delete_qh (s
, qh
); // remove it physically
758 kmem_cache_free(urb_priv_kmem
, purb
->hcpriv
);
760 kfree (purb
->hcpriv
);
762 if (purb
->complete
) {
763 dbg (KERN_DEBUG MODSTR
"unlink_urb: calling completion\n");
764 purb
->complete ((struct urb
*) purb
);
765 usb_dec_dev_use (purb
->dev
);
771 spin_unlock_irqrestore (&s
->unlink_urb_lock
, flags
); // allow interrupts from here
776 /*-------------------------------------------------------------------*/
777 // In case of ASAP iso transfer, search the URB-list for already queued URBs
778 // for this EP and calculate the earliest start frame for the new
779 // URB (easy seamless URB continuation!)
780 _static
int find_iso_limits (purb_t purb
, unsigned int *start
, unsigned int *end
)
782 purb_t u
, last_urb
= NULL
;
783 puhci_t s
= (puhci_t
) purb
->dev
->bus
->hcpriv
;
784 struct list_head
*p
= s
->urb_list
.next
;
788 spin_lock_irqsave (&s
->urb_list_lock
, flags
);
790 for (; p
!= &s
->urb_list
; p
= p
->next
) {
791 u
= list_entry (p
, urb_t
, urb_list
);
792 // look for pending URBs with identical pipe handle
793 // works only because iso doesn't toggle the data bit!
794 if ((purb
->pipe
== u
->pipe
) && (purb
->dev
== u
->dev
) && (u
->status
== USB_ST_URB_PENDING
)) {
796 *start
= u
->start_frame
;
802 *end
= (last_urb
->start_frame
+ last_urb
->number_of_packets
) & 1023;
806 spin_unlock_irqrestore(&s
->urb_list_lock
, flags
);
808 return ret
; // no previous urb found
811 /*-------------------------------------------------------------------*/
812 // adjust start_frame according to scheduling constraints (ASAP etc)
814 _static
void jnx_show_desc (puhci_desc_t d
)
818 printk (KERN_DEBUG MODSTR
"td @ 0x%08lx: link 0x%08x status 0x%08x info 0x%08x buffer 0x%08x\n",
819 (unsigned long) d
, d
->hw
.td
.link
, d
->hw
.td
.status
, d
->hw
.td
.info
, d
->hw
.td
.buffer
);
820 if (!(d
->hw
.td
.link
& UHCI_PTR_TERM
))
821 jnx_show_desc ((puhci_desc_t
) bus_to_virt (d
->hw
.td
.link
& ~UHCI_PTR_BITS
));
825 printk (KERN_DEBUG MODSTR
"qh @ 0x%08lx: head 0x%08x element 0x%08x\n",
826 (unsigned long) d
, d
->hw
.qh
.head
, d
->hw
.qh
.element
);
827 if (!(d
->hw
.qh
.element
& UHCI_PTR_TERM
))
828 jnx_show_desc ((puhci_desc_t
) bus_to_virt (d
->hw
.qh
.element
& ~UHCI_PTR_BITS
));
829 if (!(d
->hw
.qh
.head
& UHCI_PTR_TERM
))
830 jnx_show_desc ((puhci_desc_t
) bus_to_virt (d
->hw
.qh
.head
& ~UHCI_PTR_BITS
));
834 printk (KERN_DEBUG MODSTR
"desc @ 0x%08lx: invalid type %u\n",
835 (unsigned long) d
, d
->type
);
839 /*-------------------------------------------------------------------*/
840 _static
int iso_find_start (purb_t purb
)
842 puhci_t s
= (puhci_t
) purb
->dev
->bus
->hcpriv
;
844 unsigned int start_limit
= 0, stop_limit
= 0, queued_size
;
847 now
= UHCI_GET_CURRENT_FRAME (s
) & 1023;
849 if ((unsigned) purb
->number_of_packets
> 900)
852 limits
= find_iso_limits (purb
, &start_limit
, &stop_limit
);
853 queued_size
= (stop_limit
- start_limit
) & 1023;
855 if (purb
->transfer_flags
& USB_ISO_ASAP
) {
858 // 10ms setup should be enough //FIXME!
859 purb
->start_frame
= (now
+ 10) & 1023;
862 purb
->start_frame
= stop_limit
; //seamless linkage
864 if (((now
- purb
->start_frame
) & 1023) <= (unsigned) purb
->number_of_packets
) {
865 printk (KERN_DEBUG MODSTR
"iso_find_start: warning, ASAP gap, should not happen\n");
866 printk (KERN_DEBUG MODSTR
"iso_find_start: now %u start_frame %u number_of_packets %u pipe 0x%08x\n",
867 now
, purb
->start_frame
, purb
->number_of_packets
, purb
->pipe
);
869 puhci_t s
= (puhci_t
) purb
->dev
->bus
->hcpriv
;
875 spin_lock_irqsave (&s
->urb_list_lock
, flags
);
878 for (; p
!= &s
->urb_list
; p
= p
->next
) {
879 u
= list_entry (p
, urb_t
, urb_list
);
880 if (purb
->dev
!= u
->dev
)
882 printk (KERN_DEBUG MODSTR
"urb: pipe 0x%08x status %d start_frame %u number_of_packets %u\n",
883 u
->pipe
, u
->status
, u
->start_frame
, u
->number_of_packets
);
884 if (!usb_pipeisoc (u
->pipe
))
888 b
= (u
->start_frame
+ u
->number_of_packets
- 1) & 1023;
890 spin_unlock_irqrestore(&s
->urb_list_lock
, flags
);
892 if (a
!= -1 && b
!= -1) {
894 jnx_show_desc (s
->iso_td
[a
]);
901 purb
->start_frame
= (now
+ 5) & 1023; // 5ms setup should be enough //FIXME!
902 //return -EAGAIN; //FIXME
908 purb
->start_frame
&= 1023;
909 if (((now
- purb
->start_frame
) & 1023) < (unsigned) purb
->number_of_packets
) {
910 printk (KERN_DEBUG MODSTR
"iso_find_start: now between start_frame and end\n");
915 /* check if either start_frame or start_frame+number_of_packets-1 lies between start_limit and stop_limit */
918 if (((purb
->start_frame
- start_limit
) & 1023) < queued_size
||
919 ((purb
->start_frame
+ purb
->number_of_packets
- 1 - start_limit
) & 1023) < queued_size
) {
920 printk (KERN_DEBUG MODSTR
"iso_find_start: start_frame %u number_of_packets %u start_limit %u stop_limit %u\n",
921 purb
->start_frame
, purb
->number_of_packets
, start_limit
, stop_limit
);
927 /*-------------------------------------------------------------------*/
928 // submits USB interrupt (ie. polling ;-)
929 // ASAP-flag set implicitely
930 // if period==0, the the transfer is only done once (usb_scsi need this...)
932 _static
int uhci_submit_int_urb (purb_t purb
)
934 puhci_t s
= (puhci_t
) purb
->dev
->bus
->hcpriv
;
935 purb_priv_t purb_priv
= purb
->hcpriv
;
938 int status
, destination
;
941 unsigned int pipe
= purb
->pipe
;
943 //printk("SUBMIT INT\n");
945 if (purb
->interval
< 0 || purb
->interval
>= 256)
948 if (purb
->interval
== 0)
951 for (nint
= 0, n
= 1; nint
<= 8; nint
++, n
+= n
) // round interval down to 2^n
953 if (purb
->interval
< n
) {
954 purb
->interval
= n
/ 2;
960 dbg(KERN_INFO
"Rounded interval to %i, chain %i\n", purb
->interval
, nint
);
962 now
= UHCI_GET_CURRENT_FRAME (s
) & 1023;
963 purb
->start_frame
= now
; // remember start frame, just in case...
965 purb
->number_of_packets
= 1;
967 // INT allows only one packet
968 if (purb
->transfer_buffer_length
> usb_maxpacket (purb
->dev
, pipe
, usb_pipeout (pipe
)))
971 ret
= alloc_td (&td
, UHCI_PTR_DEPTH
);
976 status
= (pipe
& TD_CTRL_LS
) | TD_CTRL_ACTIVE
| TD_CTRL_IOC
|
977 (purb
->transfer_flags
& USB_DISABLE_SPD
? 0 : TD_CTRL_SPD
) | (1 << 27);
979 destination
= (purb
->pipe
& PIPE_DEVEP_MASK
) | usb_packetid (purb
->pipe
) |
980 (((purb
->transfer_buffer_length
- 1) & 0x7ff) << 21);
983 info
= destination
| (usb_gettoggle (purb
->dev
, usb_pipeendpoint (pipe
), usb_pipeout (pipe
)) << TD_TOKEN_TOGGLE
);
985 fill_td (td
, status
, info
, virt_to_bus (purb
->transfer_buffer
));
986 list_add_tail (&td
->desc_list
, &purb_priv
->desc_list
);
987 insert_td_horizontal (s
, s
->int_chain
[nint
], td
, UHCI_PTR_DEPTH
); // store in INT-TDs
989 usb_dotoggle (purb
->dev
, usb_pipeendpoint (pipe
), usb_pipeout (pipe
));
992 td
= tdm
[purb
->number_of_packets
];
993 fill_td (td
, TD_CTRL_IOC
, 0, 0);
994 insert_td_horizontal (s
, s
->iso_td
[(purb
->start_frame
+ (purb
->number_of_packets
) * purb
->interval
+ 1) & 1023], td
, UHCI_PTR_DEPTH
);
995 list_add_tail (&td
->desc_list
, &purb_priv
->desc_list
);
1000 /*-------------------------------------------------------------------*/
1001 _static
int uhci_submit_iso_urb (purb_t purb
)
1003 puhci_t s
= (puhci_t
) purb
->dev
->bus
->hcpriv
;
1004 purb_priv_t purb_priv
= purb
->hcpriv
;
1006 puhci_desc_t td
, *tdm
;
1007 int status
, destination
;
1008 unsigned long flags
;
1011 spin_lock_init (&lock
);
1012 spin_lock_irqsave (&lock
, flags
); // Disable IRQs to schedule all ISO-TDs in time
1014 ret
= iso_find_start (purb
); // adjusts purb->start_frame for later use
1019 tdm
= (puhci_desc_t
*) kmalloc (purb
->number_of_packets
* sizeof (puhci_desc_t
), in_interrupt ()? GFP_ATOMIC
: GFP_KERNEL
);
1026 // First try to get all TDs
1027 for (n
= 0; n
< purb
->number_of_packets
; n
++) {
1028 dbg (KERN_DEBUG MODSTR
"n:%d purb->iso_frame_desc[n].length:%d\n", n
, purb
->iso_frame_desc
[n
].length
);
1029 if (!purb
->iso_frame_desc
[n
].length
) {
1030 // allows ISO striping by setting length to zero in iso_descriptor
1034 ret
= alloc_td (&td
, UHCI_PTR_DEPTH
);
1036 int i
; // Cleanup allocated TDs
1038 for (i
= 0; i
< n
; n
++)
1048 status
= TD_CTRL_ACTIVE
| TD_CTRL_IOS
; //| (purb->transfer_flags&USB_DISABLE_SPD?0:TD_CTRL_SPD);
1050 destination
= (purb
->pipe
& PIPE_DEVEP_MASK
) | usb_packetid (purb
->pipe
);
1052 // Queue all allocated TDs
1053 for (n
= 0; n
< purb
->number_of_packets
; n
++) {
1057 if (n
+ 1 >= purb
->number_of_packets
)
1058 status
|= TD_CTRL_IOC
;
1060 fill_td (td
, status
, destination
| (((purb
->iso_frame_desc
[n
].length
- 1) & 0x7ff) << 21),
1061 virt_to_bus (purb
->transfer_buffer
+ purb
->iso_frame_desc
[n
].offset
));
1062 list_add_tail (&td
->desc_list
, &purb_priv
->desc_list
);
1063 insert_td_horizontal (s
, s
->iso_td
[(purb
->start_frame
+ n
) & 1023], td
, UHCI_PTR_DEPTH
); // store in iso-tds
1069 dbg ("ISO-INT# %i, start %i, now %i\n", purb
->number_of_packets
, purb
->start_frame
, UHCI_GET_CURRENT_FRAME (s
) & 1023);
1073 spin_unlock_irqrestore (&lock
, flags
);
1077 /*-------------------------------------------------------------------*/
1078 _static
int search_dev_ep (puhci_t s
, purb_t purb
)
1080 unsigned long flags
;
1081 struct list_head
*p
= s
->urb_list
.next
;
1084 dbg (KERN_DEBUG MODSTR
"search_dev_ep:\n");
1085 spin_lock_irqsave (&s
->urb_list_lock
, flags
);
1087 for (; p
!= &s
->urb_list
; p
= p
->next
) {
1088 tmp
= list_entry (p
, urb_t
, urb_list
);
1089 dbg (KERN_DEBUG MODSTR
"urb: %p\n", tmp
);
1090 // we can accept this urb if it is not queued at this time
1091 // or if non-iso transfer requests should be scheduled for the same device and pipe
1092 if ((usb_pipetype (purb
->pipe
) != PIPE_ISOCHRONOUS
&&
1093 tmp
->dev
== purb
->dev
&& tmp
->pipe
== purb
->pipe
) || (purb
== tmp
)) {
1094 spin_unlock_irqrestore (&s
->urb_list_lock
, flags
);
1095 return 1; // found another urb already queued for processing
1099 spin_unlock_irqrestore (&s
->urb_list_lock
, flags
);
1103 /*-------------------------------------------------------------------*/
1104 _static
int uhci_submit_urb (purb_t purb
)
1107 purb_priv_t purb_priv
;
1110 if (!purb
->dev
|| !purb
->dev
->bus
)
1113 s
= (puhci_t
) purb
->dev
->bus
->hcpriv
;
1114 //printk( MODSTR"submit_urb: %p type %d\n",purb,usb_pipetype(purb->pipe));
1116 if (usb_pipedevice (purb
->pipe
) == s
->rh
.devnum
)
1117 return rh_submit_urb (purb
); /* virtual root hub */
1119 usb_inc_dev_use (purb
->dev
);
1121 if (search_dev_ep (s
, purb
)) {
1122 usb_dec_dev_use (purb
->dev
);
1123 return -ENXIO
; // urb already queued
1128 purb_priv
= kmem_cache_alloc(urb_priv_kmem
, in_interrupt ()? SLAB_ATOMIC
: SLAB_KERNEL
);
1130 purb_priv
= kmalloc (sizeof (urb_priv_t
), in_interrupt ()? GFP_ATOMIC
: GFP_KERNEL
);
1133 usb_dec_dev_use (purb
->dev
);
1137 purb
->hcpriv
= purb_priv
;
1138 INIT_LIST_HEAD (&purb_priv
->desc_list
);
1139 purb_priv
->short_control_packet
=0;
1140 dbg (KERN_DEBUG MODSTR
"submit_urb: scheduling %p\n", purb
);
1142 switch (usb_pipetype (purb
->pipe
)) {
1143 case PIPE_ISOCHRONOUS
:
1144 ret
= uhci_submit_iso_urb (purb
);
1146 case PIPE_INTERRUPT
:
1147 ret
= uhci_submit_int_urb (purb
);
1151 ret
= uhci_submit_control_urb (purb
);
1154 ret
= uhci_submit_bulk_urb (purb
);
1160 dbg (KERN_DEBUG MODSTR
"submit_urb: scheduled with ret: %d\n", ret
);
1162 if (ret
!= USB_ST_NOERROR
) {
1163 usb_dec_dev_use (purb
->dev
);
1165 kmem_cache_free(urb_priv_kmem
, purb_priv
);
1172 purb
->status
= USB_ST_URB_PENDING
;
1173 queue_urb (s
, &purb
->urb_list
,1);
1174 dbg (KERN_DEBUG MODSTR
"submit_urb: exit\n");
1178 /*-------------------------------------------------------------------
1180 -------------------------------------------------------------------*/
1182 _static __u8 root_hub_dev_des
[] =
1184 0x12, /* __u8 bLength; */
1185 0x01, /* __u8 bDescriptorType; Device */
1186 0x00, /* __u16 bcdUSB; v1.0 */
1188 0x09, /* __u8 bDeviceClass; HUB_CLASSCODE */
1189 0x00, /* __u8 bDeviceSubClass; */
1190 0x00, /* __u8 bDeviceProtocol; */
1191 0x08, /* __u8 bMaxPacketSize0; 8 Bytes */
1192 0x00, /* __u16 idVendor; */
1194 0x00, /* __u16 idProduct; */
1196 0x00, /* __u16 bcdDevice; */
1198 0x00, /* __u8 iManufacturer; */
1199 0x00, /* __u8 iProduct; */
1200 0x00, /* __u8 iSerialNumber; */
1201 0x01 /* __u8 bNumConfigurations; */
1205 /* Configuration descriptor */
1206 _static __u8 root_hub_config_des
[] =
1208 0x09, /* __u8 bLength; */
1209 0x02, /* __u8 bDescriptorType; Configuration */
1210 0x19, /* __u16 wTotalLength; */
1212 0x01, /* __u8 bNumInterfaces; */
1213 0x01, /* __u8 bConfigurationValue; */
1214 0x00, /* __u8 iConfiguration; */
1215 0x40, /* __u8 bmAttributes;
1216 Bit 7: Bus-powered, 6: Self-powered, 5 Remote-wakwup, 4..0: resvd */
1217 0x00, /* __u8 MaxPower; */
1220 0x09, /* __u8 if_bLength; */
1221 0x04, /* __u8 if_bDescriptorType; Interface */
1222 0x00, /* __u8 if_bInterfaceNumber; */
1223 0x00, /* __u8 if_bAlternateSetting; */
1224 0x01, /* __u8 if_bNumEndpoints; */
1225 0x09, /* __u8 if_bInterfaceClass; HUB_CLASSCODE */
1226 0x00, /* __u8 if_bInterfaceSubClass; */
1227 0x00, /* __u8 if_bInterfaceProtocol; */
1228 0x00, /* __u8 if_iInterface; */
1231 0x07, /* __u8 ep_bLength; */
1232 0x05, /* __u8 ep_bDescriptorType; Endpoint */
1233 0x81, /* __u8 ep_bEndpointAddress; IN Endpoint 1 */
1234 0x03, /* __u8 ep_bmAttributes; Interrupt */
1235 0x08, /* __u16 ep_wMaxPacketSize; 8 Bytes */
1237 0xff /* __u8 ep_bInterval; 255 ms */
1241 _static __u8 root_hub_hub_des
[] =
1243 0x09, /* __u8 bLength; */
1244 0x29, /* __u8 bDescriptorType; Hub-descriptor */
1245 0x02, /* __u8 bNbrPorts; */
1246 0x00, /* __u16 wHubCharacteristics; */
1248 0x01, /* __u8 bPwrOn2pwrGood; 2ms */
1249 0x00, /* __u8 bHubContrCurrent; 0 mA */
1250 0x00, /* __u8 DeviceRemovable; *** 7 Ports max *** */
1251 0xff /* __u8 PortPwrCtrlMask; *** 7 ports max *** */
1254 /*-------------------------------------------------------------------------*/
1255 /* prepare Interrupt pipe transaction data; HUB INTERRUPT ENDPOINT */
1256 _static
int rh_send_irq (purb_t purb
)
1261 puhci_t uhci
= purb
->dev
->bus
->hcpriv
;
1262 unsigned int io_addr
= uhci
->io_addr
;
1265 for (i
= 0; i
< uhci
->rh
.numports
; i
++) {
1266 data
|= ((inw (io_addr
+ USBPORTSC1
+ i
* 2) & 0xa) > 0 ? (1 << (i
+ 1)) : 0);
1267 len
= (i
+ 1) / 8 + 1;
1270 *(__u16
*) purb
->transfer_buffer
= cpu_to_le16 (data
);
1271 purb
->actual_length
= len
;
1272 purb
->status
= USB_ST_NOERROR
;
1274 if ((data
> 0) && (uhci
->rh
.send
!= 0)) {
1275 dbg (KERN_DEBUG MODSTR
"Root-Hub INT complete: port1: %x port2: %x data: %x\n",
1276 inw (io_addr
+ USBPORTSC1
), inw (io_addr
+ USBPORTSC2
), data
);
1277 purb
->complete (purb
);
1280 return USB_ST_NOERROR
;
1283 /*-------------------------------------------------------------------------*/
1284 /* Virtual Root Hub INTs are polled by this timer every "intervall" ms */
1285 _static
int rh_init_int_timer (purb_t purb
);
1287 _static
void rh_int_timer_do (unsigned long ptr
)
1291 purb_t purb
= (purb_t
) ptr
;
1292 puhci_t uhci
= purb
->dev
->bus
->hcpriv
;
1294 if (uhci
->rh
.send
) {
1295 len
= rh_send_irq (purb
);
1297 purb
->actual_length
= len
;
1299 purb
->complete (purb
);
1302 rh_init_int_timer (purb
);
1305 /*-------------------------------------------------------------------------*/
1306 /* Root Hub INTs are polled by this timer */
1307 _static
int rh_init_int_timer (purb_t purb
)
1309 puhci_t uhci
= purb
->dev
->bus
->hcpriv
;
1311 uhci
->rh
.interval
= purb
->interval
;
1312 init_timer (&uhci
->rh
.rh_int_timer
);
1313 uhci
->rh
.rh_int_timer
.function
= rh_int_timer_do
;
1314 uhci
->rh
.rh_int_timer
.data
= (unsigned long) purb
;
1315 uhci
->rh
.rh_int_timer
.expires
= jiffies
+ (HZ
* (purb
->interval
< 30 ? 30 : purb
->interval
)) / 1000;
1316 add_timer (&uhci
->rh
.rh_int_timer
);
1321 /*-------------------------------------------------------------------------*/
1322 #define OK(x) len = (x); break
1324 #define CLR_RH_PORTSTAT(x) \
1325 status = inw(io_addr+USBPORTSC1+2*(wIndex-1)); \
1326 status = (status & 0xfff5) & ~(x); \
1327 outw(status, io_addr+USBPORTSC1+2*(wIndex-1))
1329 #define SET_RH_PORTSTAT(x) \
1330 status = inw(io_addr+USBPORTSC1+2*(wIndex-1)); \
1331 status = (status & 0xfff5) | (x); \
1332 outw(status, io_addr+USBPORTSC1+2*(wIndex-1))
1335 /*-------------------------------------------------------------------------*/
1337 ** Root Hub Control Pipe
1338 *************************/
1341 _static
int rh_submit_urb (purb_t purb
)
1343 struct usb_device
*usb_dev
= purb
->dev
;
1344 puhci_t uhci
= usb_dev
->bus
->hcpriv
;
1345 unsigned int pipe
= purb
->pipe
;
1346 devrequest
*cmd
= (devrequest
*) purb
->setup_packet
;
1347 void *data
= purb
->transfer_buffer
;
1348 int leni
= purb
->transfer_buffer_length
;
1351 int stat
= USB_ST_NOERROR
;
1353 unsigned int io_addr
= uhci
->io_addr
;
1361 if (usb_pipetype (pipe
) == PIPE_INTERRUPT
) {
1362 dbg (KERN_DEBUG MODSTR
"Root-Hub submit IRQ: every %d ms\n", purb
->interval
);
1363 uhci
->rh
.urb
= purb
;
1365 uhci
->rh
.interval
= purb
->interval
;
1366 rh_init_int_timer (purb
);
1368 return USB_ST_NOERROR
;
1372 bmRType_bReq
= cmd
->requesttype
| cmd
->request
<< 8;
1373 wValue
= le16_to_cpu (cmd
->value
);
1374 wIndex
= le16_to_cpu (cmd
->index
);
1375 wLength
= le16_to_cpu (cmd
->length
);
1377 for (i
= 0; i
< 8; i
++)
1378 uhci
->rh
.c_p_r
[i
] = 0;
1380 dbg(KERN_DEBUG MODSTR
"Root-Hub: adr: %2x cmd(%1x): %04x %04x %04x %04x\n",
1381 uhci
->rh
.devnum
, 8, bmRType_bReq
, wValue
, wIndex
, wLength
);
1383 switch (bmRType_bReq
) {
1384 /* Request Destination:
1385 without flags: Device,
1386 RH_INTERFACE: interface,
1387 RH_ENDPOINT: endpoint,
1388 RH_CLASS means HUB here,
1389 RH_OTHER | RH_CLASS almost ever means HUB_PORT here
1393 *(__u16
*) data
= cpu_to_le16 (1);
1395 case RH_GET_STATUS
| RH_INTERFACE
:
1396 *(__u16
*) data
= cpu_to_le16 (0);
1398 case RH_GET_STATUS
| RH_ENDPOINT
:
1399 *(__u16
*) data
= cpu_to_le16 (0);
1401 case RH_GET_STATUS
| RH_CLASS
:
1402 *(__u32
*) data
= cpu_to_le32 (0);
1403 OK (4); /* hub power ** */
1404 case RH_GET_STATUS
| RH_OTHER
| RH_CLASS
:
1405 status
= inw (io_addr
+ USBPORTSC1
+ 2 * (wIndex
- 1));
1406 cstatus
= ((status
& USBPORTSC_CSC
) >> (1 - 0)) |
1407 ((status
& USBPORTSC_PEC
) >> (3 - 1)) |
1408 (uhci
->rh
.c_p_r
[wIndex
- 1] << (0 + 4));
1409 status
= (status
& USBPORTSC_CCS
) |
1410 ((status
& USBPORTSC_PE
) >> (2 - 1)) |
1411 ((status
& USBPORTSC_SUSP
) >> (12 - 2)) |
1412 ((status
& USBPORTSC_PR
) >> (9 - 4)) |
1413 (1 << 8) | /* power on ** */
1414 ((status
& USBPORTSC_LSDA
) << (-8 + 9));
1416 *(__u16
*) data
= cpu_to_le16 (status
);
1417 *(__u16
*) (data
+ 2) = cpu_to_le16 (cstatus
);
1420 case RH_CLEAR_FEATURE
| RH_ENDPOINT
:
1422 case (RH_ENDPOINT_STALL
):
1427 case RH_CLEAR_FEATURE
| RH_CLASS
:
1429 case (RH_C_HUB_OVER_CURRENT
):
1430 OK (0); /* hub power over current ** */
1434 case RH_CLEAR_FEATURE
| RH_OTHER
| RH_CLASS
:
1436 case (RH_PORT_ENABLE
):
1437 CLR_RH_PORTSTAT (USBPORTSC_PE
);
1439 case (RH_PORT_SUSPEND
):
1440 CLR_RH_PORTSTAT (USBPORTSC_SUSP
);
1442 case (RH_PORT_POWER
):
1443 OK (0); /* port power ** */
1444 case (RH_C_PORT_CONNECTION
):
1445 SET_RH_PORTSTAT (USBPORTSC_CSC
);
1447 case (RH_C_PORT_ENABLE
):
1448 SET_RH_PORTSTAT (USBPORTSC_PEC
);
1450 case (RH_C_PORT_SUSPEND
):
1451 /*** WR_RH_PORTSTAT(RH_PS_PSSC); */
1453 case (RH_C_PORT_OVER_CURRENT
):
1454 OK (0); /* port power over current ** */
1455 case (RH_C_PORT_RESET
):
1456 uhci
->rh
.c_p_r
[wIndex
- 1] = 0;
1461 case RH_SET_FEATURE
| RH_OTHER
| RH_CLASS
:
1463 case (RH_PORT_SUSPEND
):
1464 SET_RH_PORTSTAT (USBPORTSC_SUSP
);
1466 case (RH_PORT_RESET
):
1467 SET_RH_PORTSTAT (USBPORTSC_PR
);
1469 uhci
->rh
.c_p_r
[wIndex
- 1] = 1;
1470 CLR_RH_PORTSTAT (USBPORTSC_PR
);
1472 SET_RH_PORTSTAT (USBPORTSC_PE
);
1474 SET_RH_PORTSTAT (0xa);
1476 case (RH_PORT_POWER
):
1477 OK (0); /* port power ** */
1478 case (RH_PORT_ENABLE
):
1479 SET_RH_PORTSTAT (USBPORTSC_PE
);
1484 case RH_SET_ADDRESS
:
1485 uhci
->rh
.devnum
= wValue
;
1488 case RH_GET_DESCRIPTOR
:
1489 switch ((wValue
& 0xff00) >> 8) {
1490 case (0x01): /* device descriptor */
1491 len
= min (leni
, min (sizeof (root_hub_dev_des
), wLength
));
1492 memcpy (data
, root_hub_dev_des
, len
);
1494 case (0x02): /* configuration descriptor */
1495 len
= min (leni
, min (sizeof (root_hub_config_des
), wLength
));
1496 memcpy (data
, root_hub_config_des
, len
);
1498 case (0x03): /*string descriptors */
1503 case RH_GET_DESCRIPTOR
| RH_CLASS
:
1504 root_hub_hub_des
[2] = uhci
->rh
.numports
;
1505 len
= min (leni
, min (sizeof (root_hub_hub_des
), wLength
));
1506 memcpy (data
, root_hub_hub_des
, len
);
1509 case RH_GET_CONFIGURATION
:
1510 *(__u8
*) data
= 0x01;
1513 case RH_SET_CONFIGURATION
:
1520 printk (KERN_DEBUG MODSTR
"Root-Hub stat port1: %x port2: %x \n",
1521 inw (io_addr
+ USBPORTSC1
), inw (io_addr
+ USBPORTSC2
));
1523 purb
->actual_length
= len
;
1524 purb
->status
= stat
;
1526 purb
->complete (purb
);
1527 return USB_ST_NOERROR
;
1529 /*-------------------------------------------------------------------------*/
1531 _static
int rh_unlink_urb (purb_t purb
)
1533 puhci_t uhci
= purb
->dev
->bus
->hcpriv
;
1535 dbg (KERN_DEBUG MODSTR
"Root-Hub unlink IRQ\n");
1537 del_timer (&uhci
->rh
.rh_int_timer
);
1540 /*-------------------------------------------------------------------*/
1545 * Map status to standard result codes
1547 * <status> is (td->status & 0xFE0000) [a.k.a. uhci_status_bits(td->status)
1548 * <dir_out> is True for output TDs and False for input TDs.
1550 _static
int uhci_map_status (int status
, int dir_out
)
1553 return USB_ST_NOERROR
;
1554 if (status
& TD_CTRL_BITSTUFF
) /* Bitstuff error */
1555 return USB_ST_BITSTUFF
;
1556 if (status
& TD_CTRL_CRCTIMEO
) { /* CRC/Timeout */
1558 return USB_ST_NORESPONSE
;
1562 if (status
& TD_CTRL_NAK
) /* NAK */
1563 return USB_ST_TIMEOUT
;
1564 if (status
& TD_CTRL_BABBLE
) /* Babble */
1566 if (status
& TD_CTRL_DBUFERR
) /* Buffer error */
1567 return USB_ST_BUFFERUNDERRUN
;
1568 if (status
& TD_CTRL_STALLED
) /* Stalled */
1570 if (status
& TD_CTRL_ACTIVE
) /* Active */
1571 return USB_ST_NOERROR
;
1573 return USB_ST_INTERNALERROR
;
1577 * Only the USB core should call uhci_alloc_dev and uhci_free_dev
1579 _static
int uhci_alloc_dev (struct usb_device
*usb_dev
)
1584 _static
int uhci_free_dev (struct usb_device
*usb_dev
)
1590 * uhci_get_current_frame_number()
1592 * returns the current frame number for a USB bus/controller.
1594 _static
int uhci_get_current_frame_number (struct usb_device
*usb_dev
)
1596 return UHCI_GET_CURRENT_FRAME ((puhci_t
) usb_dev
->bus
->hcpriv
);
1599 struct usb_operations uhci_device_operations
=
1603 uhci_get_current_frame_number
,
1609 * For IN-control transfers, process_transfer gets a bit more complicated,
1610 * since there are devices that return less data (eg. strings) than they
1611 * have announced. This leads to a queue abort due to the short packet,
1612 * the status stage is not executed. If this happens, the status stage
1613 * is manually re-executed.
1614 * FIXME: Stall-condition may override 'nearly' successful CTRL-IN-transfer
1615 * when the transfered length fits exactly in maxsze-packets. A bit
1616 * more intelligence is needed to detect this and finish without error.
1618 _static
int process_transfer (puhci_t s
, purb_t purb
)
1620 int ret
= USB_ST_NOERROR
;
1621 purb_priv_t purb_priv
= purb
->hcpriv
;
1622 struct list_head
*qhl
= purb_priv
->desc_list
.next
;
1623 puhci_desc_t qh
= list_entry (qhl
, uhci_desc_t
, desc_list
);
1624 struct list_head
*p
= qh
->vertical
.next
;
1625 puhci_desc_t desc
= list_entry (purb_priv
->desc_list
.prev
, uhci_desc_t
, desc_list
);
1626 puhci_desc_t last_desc
= list_entry (desc
->vertical
.prev
, uhci_desc_t
, vertical
);
1627 int data_toggle
= usb_gettoggle (purb
->dev
, usb_pipeendpoint (purb
->pipe
), usb_pipeout (purb
->pipe
)); // save initial data_toggle
1630 // extracted and remapped info from TD
1633 int status
= USB_ST_NOERROR
;
1635 dbg (KERN_DEBUG MODSTR
"process_transfer: urb contains bulk/control request\n");
1638 /* if the status phase has been retriggered and the
1639 queue is empty or the last status-TD is inactive, the retriggered
1640 status stage is completed
1643 if (purb_priv
->short_control_packet
&&
1644 ((qh
->hw
.qh
.element
== UHCI_PTR_TERM
) ||(!(last_desc
->hw
.td
.status
& TD_CTRL_ACTIVE
))))
1645 goto transfer_finished
;
1647 purb
->actual_length
=0;
1649 for (; p
!= &qh
->vertical
; p
= p
->next
) {
1650 desc
= list_entry (p
, uhci_desc_t
, vertical
);
1652 if (desc
->hw
.td
.status
& TD_CTRL_ACTIVE
) // do not process active TDs
1655 // extract transfer parameters from TD
1656 actual_length
= (desc
->hw
.td
.status
+ 1) & 0x7ff;
1657 maxlength
= (((desc
->hw
.td
.info
>> 21) & 0x7ff) + 1) & 0x7ff;
1658 status
= uhci_map_status (uhci_status_bits (desc
->hw
.td
.status
), usb_pipeout (purb
->pipe
));
1660 // see if EP is stalled
1661 if (status
== -EPIPE
) {
1662 // set up stalled condition
1663 usb_endpoint_halt (purb
->dev
, usb_pipeendpoint (purb
->pipe
), usb_pipeout (purb
->pipe
));
1666 // if any error occured stop processing of further TDs
1667 if (status
!= USB_ST_NOERROR
) {
1668 // only set ret if status returned an error
1669 uhci_show_td (desc
);
1671 purb
->error_count
++;
1674 else if ((desc
->hw
.td
.info
& 0xff) != USB_PID_SETUP
)
1675 purb
->actual_length
+= actual_length
;
1679 uhci_show_td (desc
); // show first TD of each transfer
1682 // got less data than requested
1683 if ( (actual_length
< maxlength
)) {
1684 if (purb
->transfer_flags
& USB_DISABLE_SPD
) {
1685 ret
= USB_ST_SHORT_PACKET
; // treat as real error
1686 printk (KERN_DEBUG MODSTR
"process_transfer: SPD!!\n");
1687 break; // exit after this TD because SP was detected
1690 // short read during control-IN: re-start status stage
1691 if ((usb_pipetype (purb
->pipe
) == PIPE_CONTROL
)) {
1692 if (uhci_packetid(last_desc
->hw
.td
.info
) == USB_PID_OUT
) {
1693 uhci_show_td (last_desc
);
1694 qh
->hw
.qh
.element
= virt_to_bus (last_desc
); // re-trigger status stage
1695 printk("uhci: short packet during control transfer, retrigger status stage @ %p\n",last_desc
);
1696 purb_priv
->short_control_packet
=1;
1700 // all other cases: short read is OK
1701 data_toggle
= uhci_toggle (desc
->hw
.td
.info
);
1705 data_toggle
= uhci_toggle (desc
->hw
.td
.info
);
1706 //printk(KERN_DEBUG MODSTR"process_transfer: len:%d status:%x mapped:%x toggle:%d\n", actual_length, desc->hw.td.status,status, data_toggle);
1709 usb_settoggle (purb
->dev
, usb_pipeendpoint (purb
->pipe
), usb_pipeout (purb
->pipe
), !data_toggle
);
1712 /* APC BackUPS Pro kludge */
1713 /* It tries to send all of the descriptor instead of */
1714 /* the amount we requested */
1715 if (desc
->hw
.td
.status
& TD_CTRL_IOC
&&
1716 status
& TD_CTRL_ACTIVE
&&
1717 status
& TD_CTRL_NAK
)
1726 purb
->status
= status
;
1728 dbg(KERN_DEBUG MODSTR
"process_transfer: urb %p, wanted len %d, len %d status %x err %d\n",
1729 purb
,purb
->transfer_buffer_length
,purb
->actual_length
, purb
->status
, purb
->error_count
);
1730 //dbg(KERN_DEBUG MODSTR"process_transfer: exit\n");
1734 _static
int process_interrupt (puhci_t s
, purb_t purb
)
1736 int i
, ret
= USB_ST_URB_PENDING
;
1737 purb_priv_t purb_priv
= purb
->hcpriv
;
1738 struct list_head
*p
= purb_priv
->desc_list
.next
;
1739 puhci_desc_t desc
= list_entry (purb_priv
->desc_list
.prev
, uhci_desc_t
, desc_list
);
1740 int data_toggle
= usb_gettoggle (purb
->dev
, usb_pipeendpoint (purb
->pipe
),
1741 usb_pipeout (purb
->pipe
)); // save initial data_toggle
1742 // extracted and remapped info from TD
1745 int status
= USB_ST_NOERROR
;
1747 //printk(KERN_DEBUG MODSTR"urb contains interrupt request\n");
1749 for (i
= 0; p
!= &purb_priv
->desc_list
; p
= p
->next
, i
++) // Maybe we allow more than one TD later ;-)
1751 desc
= list_entry (p
, uhci_desc_t
, desc_list
);
1753 if (desc
->hw
.td
.status
& TD_CTRL_ACTIVE
) {
1754 // do not process active TDs
1755 //printk("TD ACT Status @%p %08x\n",desc,desc->hw.td.status);
1759 if (!desc
->hw
.td
.status
& TD_CTRL_IOC
) {
1760 // do not process one-shot TDs, no recycling
1763 // extract transfer parameters from TD
1765 actual_length
= (desc
->hw
.td
.status
+ 1) & 0x7ff;
1766 status
= uhci_map_status (uhci_status_bits (desc
->hw
.td
.status
), usb_pipeout (purb
->pipe
));
1768 // see if EP is stalled
1769 if (status
== -EPIPE
) {
1770 // set up stalled condition
1771 usb_endpoint_halt (purb
->dev
, usb_pipeendpoint (purb
->pipe
), usb_pipeout (purb
->pipe
));
1774 // if any error occured: ignore this td, and continue
1775 if (status
!= USB_ST_NOERROR
) {
1776 purb
->error_count
++;
1780 purb
->actual_length
= actual_length
;
1784 data_toggle
= uhci_toggle (desc
->hw
.td
.info
);
1786 if (purb
->complete
&& status
!= USB_ST_TIMEOUT
) {
1787 // for last td, no user completion is needed
1788 dbg (KERN_DEBUG MODSTR
"process_interrupt: calling completion\n");
1789 purb
->status
= status
;
1790 purb
->complete ((struct urb
*) purb
);
1791 purb
->status
= USB_ST_URB_PENDING
;
1794 // Recycle INT-TD if interval!=0, else mark TD as one-shot
1795 if (purb
->interval
) {
1796 desc
->hw
.td
.status
|= TD_CTRL_ACTIVE
;
1797 desc
->hw
.td
.info
&= ~(1 << TD_TOKEN_TOGGLE
);
1798 desc
->hw
.td
.info
|= (usb_gettoggle (purb
->dev
, usb_pipeendpoint (purb
->pipe
),
1799 usb_pipeout (purb
->pipe
)) << TD_TOKEN_TOGGLE
);
1800 usb_dotoggle (purb
->dev
, usb_pipeendpoint (purb
->pipe
), usb_pipeout (purb
->pipe
));
1803 desc
->hw
.td
.status
&= ~TD_CTRL_IOC
; // inactivate TD
1811 _static
int process_iso (puhci_t s
, purb_t purb
)
1814 int ret
= USB_ST_NOERROR
;
1815 purb_priv_t purb_priv
= purb
->hcpriv
;
1816 struct list_head
*p
= purb_priv
->desc_list
.next
;
1817 puhci_desc_t desc
= list_entry (purb_priv
->desc_list
.prev
, uhci_desc_t
, desc_list
);
1819 dbg ( /*KERN_DEBUG */ MODSTR
"urb contains iso request\n");
1820 if (desc
->hw
.td
.status
& TD_CTRL_ACTIVE
)
1821 return USB_ST_PARTIAL_ERROR
; // last TD not finished
1823 purb
->error_count
= 0;
1824 purb
->actual_length
= 0;
1825 purb
->status
= USB_ST_NOERROR
;
1827 for (i
= 0; p
!= &purb_priv
->desc_list
; p
= p
->next
, i
++) {
1828 desc
= list_entry (p
, uhci_desc_t
, desc_list
);
1830 //uhci_show_td(desc);
1831 if (desc
->hw
.td
.status
& TD_CTRL_ACTIVE
) {
1832 // means we have completed the last TD, but not the TDs before
1833 printk (KERN_DEBUG MODSTR
"TD still active (%x)- grrr. paranoia!\n", desc
->hw
.td
.status
);
1834 ret
= USB_ST_PARTIAL_ERROR
;
1835 purb
->iso_frame_desc
[i
].status
= ret
;
1836 unlink_td (s
, desc
);
1840 unlink_td (s
, desc
);
1842 if (purb
->number_of_packets
<= i
) {
1843 dbg (KERN_DEBUG MODSTR
"purb->number_of_packets (%d)<=(%d)\n", purb
->number_of_packets
, i
);
1844 ret
= USB_ST_URB_INVALID_ERROR
;
1848 if (purb
->iso_frame_desc
[i
].offset
+ purb
->transfer_buffer
!= bus_to_virt (desc
->hw
.td
.buffer
)) {
1849 // Hm, something really weird is going on
1850 dbg (KERN_DEBUG MODSTR
"Pointer Paranoia: %p!=%p\n", purb
->iso_frame_desc
[i
].offset
+ purb
->transfer_buffer
, bus_to_virt (desc
->hw
.td
.buffer
));
1851 ret
= USB_ST_URB_INVALID_ERROR
;
1852 purb
->iso_frame_desc
[i
].status
= ret
;
1855 purb
->iso_frame_desc
[i
].actual_length
= (desc
->hw
.td
.status
+ 1) & 0x7ff;
1856 purb
->iso_frame_desc
[i
].status
= uhci_map_status (uhci_status_bits (desc
->hw
.td
.status
), usb_pipeout (purb
->pipe
));
1857 purb
->actual_length
+= purb
->iso_frame_desc
[i
].actual_length
;
1861 if (purb
->iso_frame_desc
[i
].status
!= USB_ST_NOERROR
) {
1862 purb
->error_count
++;
1863 purb
->status
= purb
->iso_frame_desc
[i
].status
;
1865 dbg (KERN_DEBUG MODSTR
"process_iso: len:%d status:%x\n",
1866 purb
->iso_frame_desc
[i
].length
, purb
->iso_frame_desc
[i
].status
);
1871 dbg ( /*KERN_DEBUG */ MODSTR
"process_iso: exit %i (%d)\n", i
, ret
);
1876 _static
int process_urb (puhci_t s
, struct list_head
*p
)
1878 int ret
= USB_ST_NOERROR
;
1881 spin_lock(&s
->urb_list_lock
);
1882 purb
=list_entry (p
, urb_t
, urb_list
);
1883 dbg ( /*KERN_DEBUG */ MODSTR
"found queued urb: %p\n", purb
);
1885 switch (usb_pipetype (purb
->pipe
)) {
1888 ret
= process_transfer (s
, purb
);
1890 case PIPE_ISOCHRONOUS
:
1891 ret
= process_iso (s
, purb
);
1893 case PIPE_INTERRUPT
:
1894 ret
= process_interrupt (s
, purb
);
1898 spin_unlock(&s
->urb_list_lock
);
1900 if (purb
->status
!= USB_ST_URB_PENDING
) {
1902 dbg ( /*KERN_DEBUG */ MODSTR
"dequeued urb: %p\n", purb
);
1903 dequeue_urb (s
, p
, 1);
1906 kmem_cache_free(urb_priv_kmem
, purb
->hcpriv
);
1908 kfree (purb
->hcpriv
);
1911 if ((usb_pipetype (purb
->pipe
) != PIPE_INTERRUPT
)) {
1912 purb_t tmp
= purb
->next
; // pointer to first urb
1917 if (tmp
->status
!= USB_ST_URB_PENDING
) {
1923 while (tmp
!= NULL
&& tmp
!= purb
->next
);
1924 if (tmp
== purb
->next
)
1928 // In case you need the current URB status for your completion handler
1929 if (purb
->complete
&& (!proceed
|| (purb
->transfer_flags
& USB_URB_EARLY_COMPLETE
))) {
1930 dbg (KERN_DEBUG MODSTR
"process_transfer: calling early completion\n");
1931 purb
->complete ((struct urb
*) purb
);
1932 if (!proceed
&& is_ring
&& (purb
->status
!= USB_ST_URB_KILLED
))
1933 uhci_submit_urb (purb
);
1936 if (proceed
&& purb
->next
) {
1937 // if there are linked urbs - handle submitting of them right now.
1938 tmp
= purb
->next
; // pointer to first urb
1941 if ((tmp
->status
!= USB_ST_URB_PENDING
) && (tmp
->status
!= USB_ST_URB_KILLED
) && uhci_submit_urb (tmp
) != USB_ST_NOERROR
)
1945 while (tmp
!= NULL
&& tmp
!= purb
->next
); // submit until we reach NULL or our own pointer or submit fails
1947 if (purb
->complete
&& !(purb
->transfer_flags
& USB_URB_EARLY_COMPLETE
)) {
1948 dbg ( /*KERN_DEBUG */ MODSTR
"process_transfer: calling completion\n");
1949 purb
->complete ((struct urb
*) purb
);
1952 usb_dec_dev_use (purb
->dev
);
1959 _static
void uhci_interrupt (int irq
, void *__uhci
, struct pt_regs
*regs
)
1962 unsigned int io_addr
= s
->io_addr
;
1963 unsigned short status
;
1964 struct list_head
*p
, *p2
;
1967 * Read the interrupt status, and write it back to clear the
1970 dbg ("interrupt\n");
1971 status
= inw (io_addr
+ USBSTS
);
1973 if (!status
) /* shared interrupt, not mine */
1977 printk (KERN_DEBUG MODSTR
"interrupt, status %x\n", status
);
1978 //uhci_show_status (s);
1982 * the following is very subtle and was blatantly wrong before
1983 * traverse the list in *reverse* direction, because new entries
1984 * may be added at the end.
1985 * also, because process_urb may unlink the current urb,
1986 * we need to advance the list before
1990 spin_lock(&s
->unlink_urb_lock
);
1991 spin_lock (&s
->urb_list_lock
);
1992 p
= s
->urb_list
.prev
;
1993 spin_unlock (&s
->urb_list_lock
);
1995 while (p
!= &s
->urb_list
) {
1998 process_urb (s
, p2
);
2001 spin_unlock(&s
->unlink_urb_lock
);
2003 outw (status
, io_addr
+ USBSTS
);
2010 _static
void reset_hc (puhci_t s
)
2012 unsigned int io_addr
= s
->io_addr
;
2015 /* Global reset for 50ms */
2016 outw (USBCMD_GRESET
, io_addr
+ USBCMD
);
2018 outw (0, io_addr
+ USBCMD
);
2022 _static
void start_hc (puhci_t s
)
2024 unsigned int io_addr
= s
->io_addr
;
2028 * Reset the HC - this will force us to get a
2029 * new notification of any already connected
2030 * ports due to the virtual disconnect that it
2033 outw (USBCMD_HCRESET
, io_addr
+ USBCMD
);
2035 while (inw (io_addr
+ USBCMD
) & USBCMD_HCRESET
) {
2037 printk (KERN_ERR MODSTR
"USBCMD_HCRESET timed out!\n");
2042 /* Turn on all interrupts */
2043 outw (USBINTR_TIMEOUT
| USBINTR_RESUME
| USBINTR_IOC
| USBINTR_SP
, io_addr
+ USBINTR
);
2045 /* Start at frame 0 */
2046 outw (0, io_addr
+ USBFRNUM
);
2047 outl (virt_to_bus (s
->framelist
), io_addr
+ USBFLBASEADD
);
2049 /* Run and mark it configured with a 64-byte max packet */
2050 outw (USBCMD_RS
| USBCMD_CF
| USBCMD_MAXP
, io_addr
+ USBCMD
);
2054 _static
void __exit
uhci_cleanup_dev(puhci_t s
)
2056 struct usb_device
*root_hub
= s
->bus
->root_hub
;
2058 usb_disconnect (&root_hub
);
2060 usb_deregister_bus (s
->bus
);
2063 release_region (s
->io_addr
, s
->io_size
);
2064 free_irq (s
->irq
, s
);
2065 usb_free_bus (s
->bus
);
2071 _static
int __init
uhci_start_usb (puhci_t s
)
2073 /* connect the virtual root hub */
2074 struct usb_device
*usb_dev
;
2076 usb_dev
= usb_alloc_dev (NULL
, s
->bus
);
2080 s
->bus
->root_hub
= usb_dev
;
2081 usb_connect (usb_dev
);
2083 if (usb_new_device (usb_dev
) != 0) {
2084 usb_free_dev (usb_dev
);
2091 _static
int __init
alloc_uhci (int irq
, unsigned int io_addr
, unsigned int io_size
)
2094 struct usb_bus
*bus
;
2096 s
= kmalloc (sizeof (uhci_t
), GFP_KERNEL
);
2100 memset (s
, 0, sizeof (uhci_t
));
2101 INIT_LIST_HEAD (&s
->urb_list
);
2102 spin_lock_init (&s
->urb_list_lock
);
2103 spin_lock_init (&s
->qh_lock
);
2104 spin_lock_init (&s
->td_lock
);
2105 spin_lock_init (&s
->unlink_urb_lock
);
2107 s
->io_addr
= io_addr
;
2108 s
->io_size
= io_size
;
2109 s
->next
= devs
; //chain new uhci device into global list
2111 bus
= usb_alloc_bus (&uhci_device_operations
);
2120 /* UHCI specs says devices must have 2 ports, but goes on to say */
2121 /* they may have more but give no way to determine how many they */
2122 /* have, so default to 2 */
2123 /* According to the UHCI spec, Bit 7 is always set to 1. So we try */
2124 /* to use this to our advantage */
2126 for (s
->maxports
= 0; s
->maxports
< (io_size
- 0x10) / 2; s
->maxports
++) {
2127 unsigned int portstatus
;
2129 portstatus
= inw (io_addr
+ 0x10 + (s
->maxports
* 2));
2130 printk ("port %i, adr %x status %x\n", s
->maxports
,
2131 io_addr
+ 0x10 + (s
->maxports
* 2), portstatus
);
2132 if (!(portstatus
& 0x0080))
2135 dbg (KERN_DEBUG MODSTR
"Detected %d ports\n", s
->maxports
);
2137 /* This is experimental so anything less than 2 or greater than 8 is */
2138 /* something weird and we'll ignore it */
2139 if (s
->maxports
< 2 || s
->maxports
> 8) {
2140 dbg (KERN_DEBUG
"Port count misdetected, forcing to 2 ports\n");
2144 s
->rh
.numports
= s
->maxports
;
2146 if (init_skel (s
)) {
2152 request_region (s
->io_addr
, io_size
, MODNAME
);
2154 usb_register_bus (s
->bus
);
2158 if (request_irq (irq
, uhci_interrupt
, SA_SHIRQ
, MODNAME
, s
)) {
2159 printk(MODSTR KERN_ERR
"request_irq %d failed!\n",irq
);
2162 release_region (s
->io_addr
, s
->io_size
);
2170 if(uhci_start_usb (s
) < 0) {
2171 uhci_cleanup_dev(s
);
2175 //chain new uhci device into global list
2181 _static
int __init
start_uhci (struct pci_dev
*dev
)
2185 /* Search for the IO base address.. */
2186 for (i
= 0; i
< 6; i
++) {
2187 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,3,8)
2188 unsigned int io_addr
= dev
->resource
[i
].start
;
2189 unsigned int io_size
=
2190 dev
->resource
[i
].end
- dev
->resource
[i
].start
+ 1;
2191 if (!(dev
->resource
[i
].flags
& 1))
2194 unsigned int io_addr
= dev
->base_address
[i
];
2195 unsigned int io_size
= 0x14;
2201 /* Is it already in use? */
2202 if (check_region (io_addr
, io_size
))
2204 /* disable legacy emulation */
2205 pci_write_config_word (dev
, USBLEGSUP
, USBLEGSUP_DEFAULT
);
2207 return alloc_uhci(dev
->irq
, io_addr
, io_size
);
2213 _static
int handle_apm_event (apm_event_t event
)
2215 static int down
= 0;
2217 printk ("handle_apm_event(%d)\n", event
);
2219 case APM_SYS_SUSPEND
:
2220 case APM_USER_SUSPEND
:
2222 dbg (KERN_DEBUG MODSTR
"received extra suspend event\n");
2231 case APM_NORMAL_RESUME
:
2232 case APM_CRITICAL_RESUME
:
2234 dbg (KERN_DEBUG MODSTR
"received bogus resume event\n");
2248 int __init
uhci_init (void)
2250 int retval
= -ENODEV
;
2251 struct pci_dev
*dev
= NULL
;
2256 char *slabname
=kmalloc(16, GFP_KERNEL
);
2261 strcpy(slabname
, "uhci_desc");
2262 uhci_desc_kmem
= kmem_cache_create(slabname
, sizeof(uhci_desc_t
), 0, SLAB_HWCACHE_ALIGN
, NULL
, NULL
);
2264 if(!uhci_desc_kmem
) {
2265 printk(KERN_ERR MODSTR
"kmem_cache_create for uhci_desc failed (out of memory)\n");
2269 slabname
=kmalloc(16, GFP_KERNEL
);
2274 strcpy(slabname
, "urb_priv");
2275 urb_priv_kmem
= kmem_cache_create(slabname
, sizeof(urb_priv_t
), 0, SLAB_HWCACHE_ALIGN
, NULL
, NULL
);
2277 if(!urb_priv_kmem
) {
2278 printk(KERN_ERR MODSTR
"kmem_cache_create for urb_priv_t failed (out of memory)\n");
2282 printk (KERN_INFO MODSTR VERSTR
"\n");
2285 dev
= pci_find_class (PCI_CLASS_SERIAL_USB
<< 8, dev
);
2290 pci_read_config_byte (dev
, PCI_CLASS_PROG
, &type
);
2294 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,3,8)
2295 pci_enable_device (dev
);
2299 printk(KERN_ERR MODSTR
"Found UHCI device with no IRQ assigned. Check BIOS settings!\n");
2304 retval
= start_uhci (dev
);
2313 apm_register_callback (&handle_apm_event
);
2318 void __exit
uhci_cleanup (void)
2321 while ((s
= devs
)) {
2323 uhci_cleanup_dev(s
);
2326 kmem_cache_shrink(uhci_desc_kmem
);
2327 kmem_cache_shrink(urb_priv_kmem
);
2332 int init_module (void)
2334 return uhci_init ();
2337 void cleanup_module (void)
2340 apm_unregister_callback (&handle_apm_event
);