Import 2.4.0-test5pre2
[davej-history.git] / drivers / usb / usb-uhci.c
blob626bfc93cd7ee0d283892985489605fcfdc21129
1 /*
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)
8 *
9 * HW-initalization based on material of
11 * (C) Copyright 1999 Linus Torvalds
12 * (C) Copyright 1999 Johannes Erdfelt
13 * (C) Copyright 1999 Randy Dunlap
15 * $Id: usb-uhci.c,v 1.232 2000/06/11 13:18:30 acher Exp $
18 #include <linux/config.h>
19 #include <linux/module.h>
20 #include <linux/pci.h>
21 #include <linux/kernel.h>
22 #include <linux/delay.h>
23 #include <linux/ioport.h>
24 #include <linux/sched.h>
25 #include <linux/malloc.h>
26 #include <linux/smp_lock.h>
27 #include <linux/errno.h>
28 #include <linux/unistd.h>
29 #include <linux/interrupt.h> /* for in_interrupt() */
30 #include <linux/init.h>
31 #include <linux/version.h>
32 #include <linux/pm.h>
34 #include <asm/uaccess.h>
35 #include <asm/io.h>
36 #include <asm/irq.h>
37 #include <asm/system.h>
39 /* This enables more detailed sanity checks in submit_iso */
40 //#define ISO_SANITY_CHECK
42 /* This enables debug printks */
43 #define DEBUG
45 /* This enables all symbols to be exported, to ease debugging oopses */
46 //#define DEBUG_SYMBOLS
48 /* This enables an extra UHCI slab for memory debugging */
49 #define DEBUG_SLAB
51 #define VERSTR "$Revision: 1.232 $ time " __TIME__ " " __DATE__
53 #include <linux/usb.h>
54 #include "usb-uhci.h"
55 #include "usb-uhci-debug.h"
57 #undef DEBUG
58 #undef dbg
59 #define dbg(format, arg...) do {} while (0)
60 #define DEBUG_SYMBOLS
61 #ifdef DEBUG_SYMBOLS
62 #define _static
63 #ifndef EXPORT_SYMTAB
64 #define EXPORT_SYMTAB
65 #endif
66 #else
67 #define _static static
68 #endif
70 #define queue_dbg dbg //err
71 #define async_dbg dbg //err
73 #ifdef DEBUG_SLAB
74 static kmem_cache_t *uhci_desc_kmem;
75 static kmem_cache_t *urb_priv_kmem;
76 #endif
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)
104 struct list_head *q;
105 uhci_desc_t *qh;
106 int now=UHCI_GET_CURRENT_FRAME(s);
108 q=s->free_desc.prev;
110 while (q != &s->free_desc) {
111 qh = list_entry (q, uhci_desc_t, horizontal);
112 q=qh->horizontal.prev;
114 if ((qh->last_used!=now) || force)
115 delete_qh(s,qh);
118 /*-------------------------------------------------------------------*/
119 _static void uhci_switch_timer_int(uhci_t *s)
122 if (!list_empty(&s->urb_unlinked)) {
123 s->td1ms->hw.td.status |= TD_CTRL_IOC;
125 else {
126 s->td1ms->hw.td.status &= ~TD_CTRL_IOC;
129 if (s->timeout_urbs) {
130 s->td32ms->hw.td.status |= TD_CTRL_IOC;
132 else {
133 s->td32ms->hw.td.status &= ~TD_CTRL_IOC;
136 wmb();
138 /*-------------------------------------------------------------------*/
139 #ifdef CONFIG_USB_UHCI_HIGH_BANDWIDTH
140 _static void enable_desc_loop(uhci_t *s, urb_t *urb)
142 int flags;
144 spin_lock_irqsave (&s->qh_lock, flags);
145 s->chain_end->hw.qh.head&=~UHCI_PTR_TERM;
146 mb();
147 s->loop_usage++;
148 ((urb_priv_t*)urb->hcpriv)->use_loop=1;
149 spin_unlock_irqrestore (&s->qh_lock, flags);
151 /*-------------------------------------------------------------------*/
152 _static void disable_desc_loop(uhci_t *s, urb_t *urb)
154 int flags;
156 spin_lock_irqsave (&s->qh_lock, flags);
158 if (((urb_priv_t*)urb->hcpriv)->use_loop) {
159 s->loop_usage--;
161 if (!s->loop_usage) {
162 s->chain_end->hw.qh.head|=UHCI_PTR_TERM;
163 mb();
165 ((urb_priv_t*)urb->hcpriv)->use_loop=0;
167 spin_unlock_irqrestore (&s->qh_lock, flags);
169 #endif
170 /*-------------------------------------------------------------------*/
171 _static void queue_urb_unlocked (uhci_t *s, urb_t *urb)
173 struct list_head *p=&urb->urb_list;
174 #ifdef CONFIG_USB_UHCI_HIGH_BANDWIDTH
176 int type;
177 type=usb_pipetype (urb->pipe);
179 if ((type == PIPE_BULK) || (type == PIPE_CONTROL))
180 enable_desc_loop(s, urb);
182 #endif
183 ((urb_priv_t*)urb->hcpriv)->started=jiffies;
184 list_add (p, &s->urb_list);
185 if (urb->timeout)
186 s->timeout_urbs++;
187 uhci_switch_timer_int(s);
189 /*-------------------------------------------------------------------*/
190 _static void queue_urb (uhci_t *s, urb_t *urb)
192 unsigned long flags=0;
194 spin_lock_irqsave (&s->urb_list_lock, flags);
195 queue_urb_unlocked(s,urb);
196 spin_unlock_irqrestore (&s->urb_list_lock, flags);
198 /*-------------------------------------------------------------------*/
199 _static void dequeue_urb (uhci_t *s, urb_t *urb)
201 #ifdef CONFIG_USB_UHCI_HIGH_BANDWIDTH
202 int type;
204 type=usb_pipetype (urb->pipe);
206 if ((type == PIPE_BULK) || (type == PIPE_CONTROL))
207 disable_desc_loop(s, urb);
208 #endif
210 list_del (&urb->urb_list);
211 if (urb->timeout && s->timeout_urbs)
212 s->timeout_urbs--;
215 /*-------------------------------------------------------------------*/
216 _static int alloc_td (uhci_desc_t ** new, int flags)
218 #ifdef DEBUG_SLAB
219 *new= kmem_cache_alloc(uhci_desc_kmem, SLAB_FLAG);
220 #else
221 *new = (uhci_desc_t *) kmalloc (sizeof (uhci_desc_t), KMALLOC_FLAG);
222 #endif
223 if (!*new)
224 return -ENOMEM;
225 memset (*new, 0, sizeof (uhci_desc_t));
226 (*new)->hw.td.link = UHCI_PTR_TERM | (flags & UHCI_PTR_BITS); // last by default
227 (*new)->type = TD_TYPE;
228 mb();
229 INIT_LIST_HEAD (&(*new)->vertical);
230 INIT_LIST_HEAD (&(*new)->horizontal);
232 return 0;
234 /*-------------------------------------------------------------------*/
235 // append a qh to td.link physically, the SW linkage is not affected
236 _static void append_qh(uhci_t *s, uhci_desc_t *td, uhci_desc_t* qh, int flags)
238 unsigned long xxx;
240 spin_lock_irqsave (&s->td_lock, xxx);
242 td->hw.td.link = virt_to_bus (qh) | (flags & UHCI_PTR_DEPTH) | UHCI_PTR_QH;
244 mb();
245 spin_unlock_irqrestore (&s->td_lock, xxx);
247 /*-------------------------------------------------------------------*/
248 /* insert td at last position in td-list of qh (vertical) */
249 _static int insert_td (uhci_t *s, uhci_desc_t *qh, uhci_desc_t* new, int flags)
251 uhci_desc_t *prev;
252 unsigned long xxx;
254 spin_lock_irqsave (&s->td_lock, xxx);
256 list_add_tail (&new->vertical, &qh->vertical);
258 prev = list_entry (new->vertical.prev, uhci_desc_t, vertical);
260 if (qh == prev ) {
261 // virgin qh without any tds
262 qh->hw.qh.element = virt_to_bus (new);
264 else {
265 // already tds inserted, implicitely remove TERM bit of prev
266 prev->hw.td.link = virt_to_bus (new) | (flags & UHCI_PTR_DEPTH);
268 mb();
269 spin_unlock_irqrestore (&s->td_lock, xxx);
271 return 0;
273 /*-------------------------------------------------------------------*/
274 /* insert new_td after td (horizontal) */
275 _static int insert_td_horizontal (uhci_t *s, uhci_desc_t *td, uhci_desc_t* new)
277 uhci_desc_t *next;
278 unsigned long flags;
280 spin_lock_irqsave (&s->td_lock, flags);
282 next = list_entry (td->horizontal.next, uhci_desc_t, horizontal);
283 list_add (&new->horizontal, &td->horizontal);
284 new->hw.td.link = td->hw.td.link;
285 td->hw.td.link = virt_to_bus (new);
286 mb();
287 spin_unlock_irqrestore (&s->td_lock, flags);
289 return 0;
291 /*-------------------------------------------------------------------*/
292 _static int unlink_td (uhci_t *s, uhci_desc_t *element, int phys_unlink)
294 uhci_desc_t *next, *prev;
295 int dir = 0;
296 unsigned long flags;
298 spin_lock_irqsave (&s->td_lock, flags);
300 next = list_entry (element->vertical.next, uhci_desc_t, vertical);
302 if (next == element) {
303 dir = 1;
304 prev = list_entry (element->horizontal.prev, uhci_desc_t, horizontal);
306 else
307 prev = list_entry (element->vertical.prev, uhci_desc_t, vertical);
309 if (phys_unlink) {
310 // really remove HW linking
311 if (prev->type == TD_TYPE)
312 prev->hw.td.link = element->hw.td.link;
313 else
314 prev->hw.qh.element = element->hw.td.link;
317 mb ();
319 if (dir == 0)
320 list_del (&element->vertical);
321 else
322 list_del (&element->horizontal);
324 spin_unlock_irqrestore (&s->td_lock, flags);
326 return 0;
329 /*-------------------------------------------------------------------*/
330 _static int delete_desc (uhci_desc_t *element)
332 #ifdef DEBUG_SLAB
333 kmem_cache_free(uhci_desc_kmem, element);
334 #else
335 kfree (element);
336 #endif
337 return 0;
339 /*-------------------------------------------------------------------*/
340 // Allocates qh element
341 _static int alloc_qh (uhci_desc_t ** new)
343 #ifdef DEBUG_SLAB
344 *new= kmem_cache_alloc(uhci_desc_kmem, SLAB_FLAG);
345 #else
346 *new = (uhci_desc_t *) kmalloc (sizeof (uhci_desc_t), KMALLOC_FLAG);
347 #endif
348 if (!*new)
349 return -ENOMEM;
350 memset (*new, 0, sizeof (uhci_desc_t));
351 (*new)->hw.qh.head = UHCI_PTR_TERM;
352 (*new)->hw.qh.element = UHCI_PTR_TERM;
353 (*new)->type = QH_TYPE;
355 mb();
356 INIT_LIST_HEAD (&(*new)->horizontal);
357 INIT_LIST_HEAD (&(*new)->vertical);
359 dbg("Allocated qh @ %p", *new);
361 return 0;
363 /*-------------------------------------------------------------------*/
364 // inserts new qh before/after the qh at pos
365 // flags: 0: insert before pos, 1: insert after pos (for low speed transfers)
366 _static int insert_qh (uhci_t *s, uhci_desc_t *pos, uhci_desc_t *new, int order)
368 uhci_desc_t *old;
369 unsigned long flags;
371 spin_lock_irqsave (&s->qh_lock, flags);
373 if (!order) {
374 // (OLD) (POS) -> (OLD) (NEW) (POS)
375 old = list_entry (pos->horizontal.prev, uhci_desc_t, horizontal);
376 list_add_tail (&new->horizontal, &pos->horizontal);
377 new->hw.qh.head = MAKE_QH_ADDR (pos) ;
378 if (!(old->hw.qh.head & UHCI_PTR_TERM))
379 old->hw.qh.head = MAKE_QH_ADDR (new) ;
381 else {
382 // (POS) (OLD) -> (POS) (NEW) (OLD)
383 old = list_entry (pos->horizontal.next, uhci_desc_t, horizontal);
384 list_add (&new->horizontal, &pos->horizontal);
385 new->hw.qh.head = MAKE_QH_ADDR (old);
386 pos->hw.qh.head = MAKE_QH_ADDR (new) ;
389 mb ();
391 spin_unlock_irqrestore (&s->qh_lock, flags);
393 return 0;
396 /*-------------------------------------------------------------------*/
397 _static int unlink_qh (uhci_t *s, uhci_desc_t *element)
399 uhci_desc_t *prev;
400 unsigned long flags;
402 spin_lock_irqsave (&s->qh_lock, flags);
404 prev = list_entry (element->horizontal.prev, uhci_desc_t, horizontal);
405 prev->hw.qh.head = element->hw.qh.head;
407 list_del(&element->horizontal);
409 mb ();
410 spin_unlock_irqrestore (&s->qh_lock, flags);
412 return 0;
414 /*-------------------------------------------------------------------*/
415 _static int delete_qh (uhci_t *s, uhci_desc_t *qh)
417 uhci_desc_t *td;
418 struct list_head *p;
420 list_del (&qh->horizontal);
422 while ((p = qh->vertical.next) != &qh->vertical) {
423 td = list_entry (p, uhci_desc_t, vertical);
424 dbg("unlink td @ %p",td);
425 unlink_td (s, td, 0); // no physical unlink
426 delete_desc (td);
429 delete_desc (qh);
431 return 0;
433 /*-------------------------------------------------------------------*/
434 _static void clean_td_chain (uhci_desc_t *td)
436 struct list_head *p;
437 uhci_desc_t *td1;
439 if (!td)
440 return;
442 while ((p = td->horizontal.next) != &td->horizontal) {
443 td1 = list_entry (p, uhci_desc_t, horizontal);
444 delete_desc (td1);
447 delete_desc (td);
450 /*-------------------------------------------------------------------*/
451 _static void fill_td (uhci_desc_t *td, int status, int info, __u32 buffer)
453 td->hw.td.status = status;
454 td->hw.td.info = info;
455 td->hw.td.buffer = buffer;
457 /*-------------------------------------------------------------------*/
458 // Removes ALL qhs in chain (paranoia!)
459 _static void cleanup_skel (uhci_t *s)
461 unsigned int n;
462 uhci_desc_t *td;
464 dbg("cleanup_skel");
466 clean_descs(s,1);
469 if (s->td32ms) {
471 unlink_td(s,s->td32ms,1);
472 delete_desc(s->td32ms);
475 for (n = 0; n < 8; n++) {
476 td = s->int_chain[n];
477 clean_td_chain (td);
480 if (s->iso_td) {
481 for (n = 0; n < 1024; n++) {
482 td = s->iso_td[n];
483 clean_td_chain (td);
485 kfree (s->iso_td);
488 if (s->framelist)
489 free_page ((unsigned long) s->framelist);
491 if (s->control_chain) {
492 // completed init_skel?
493 struct list_head *p;
494 uhci_desc_t *qh, *qh1;
496 qh = s->control_chain;
497 while ((p = qh->horizontal.next) != &qh->horizontal) {
498 qh1 = list_entry (p, uhci_desc_t, horizontal);
499 delete_qh (s, qh1);
502 delete_qh (s, qh);
504 else {
505 if (s->ls_control_chain)
506 delete_desc (s->ls_control_chain);
507 if (s->control_chain)
508 delete_desc(s->control_chain);
509 if (s->bulk_chain)
510 delete_desc (s->bulk_chain);
511 if (s->chain_end)
512 delete_desc (s->chain_end);
514 dbg("cleanup_skel finished");
516 /*-------------------------------------------------------------------*/
517 // allocates framelist and qh-skeletons
518 // only HW-links provide continous linking, SW-links stay in their domain (ISO/INT)
519 _static int init_skel (uhci_t *s)
521 int n, ret;
522 uhci_desc_t *qh, *td;
524 dbg("init_skel");
526 s->framelist = (__u32 *) get_free_page (GFP_KERNEL);
528 if (!s->framelist)
529 return -ENOMEM;
531 memset (s->framelist, 0, 4096);
533 dbg("allocating iso desc pointer list");
534 s->iso_td = (uhci_desc_t **) kmalloc (1024 * sizeof (uhci_desc_t*), GFP_KERNEL);
536 if (!s->iso_td)
537 goto init_skel_cleanup;
539 s->ls_control_chain = NULL;
540 s->control_chain = NULL;
541 s->bulk_chain = NULL;
542 s->chain_end = NULL;
544 dbg("allocating iso descs");
545 for (n = 0; n < 1024; n++) {
546 // allocate skeleton iso/irq-tds
547 ret = alloc_td (&td, 0);
548 if (ret)
549 goto init_skel_cleanup;
550 s->iso_td[n] = td;
551 s->framelist[n] = ((__u32) virt_to_bus (td));
554 dbg("allocating qh: chain_end");
555 ret = alloc_qh (&qh);
557 if (ret)
558 goto init_skel_cleanup;
560 s->chain_end = qh;
562 ret = alloc_td (&td, 0);
564 if (ret)
565 goto init_skel_cleanup;
567 fill_td (td, 0 * TD_CTRL_IOC, 0, 0); // generate 1ms interrupt (enabled on demand)
568 insert_td (s, qh, td, 0);
569 s->td1ms=td;
571 dbg("allocating qh: bulk_chain");
572 ret = alloc_qh (&qh);
573 if (ret)
574 goto init_skel_cleanup;
576 insert_qh (s, s->chain_end, qh, 0);
577 s->bulk_chain = qh;
579 dbg("allocating qh: control_chain");
580 ret = alloc_qh (&qh);
581 if (ret)
582 goto init_skel_cleanup;
584 insert_qh (s, s->bulk_chain, qh, 0);
585 s->control_chain = qh;
587 #ifdef CONFIG_USB_UHCI_HIGH_BANDWIDTH
588 // disabled reclamation loop
589 s->chain_end->hw.qh.head=virt_to_bus(s->control_chain) | UHCI_PTR_QH | UHCI_PTR_TERM;
590 #endif
592 dbg("allocating qh: ls_control_chain");
593 ret = alloc_qh (&qh);
594 if (ret)
595 goto init_skel_cleanup;
597 insert_qh (s, s->control_chain, qh, 0);
598 s->ls_control_chain = qh;
600 for (n = 0; n < 8; n++)
601 s->int_chain[n] = 0;
603 dbg("allocating skeleton INT-TDs");
605 for (n = 0; n < 8; n++) {
606 uhci_desc_t *td;
608 alloc_td (&td, 0);
609 if (!td)
610 goto init_skel_cleanup;
611 s->int_chain[n] = td;
612 if (n == 0) {
613 s->int_chain[0]->hw.td.link = virt_to_bus (s->ls_control_chain) | UHCI_PTR_QH;
615 else {
616 s->int_chain[n]->hw.td.link = virt_to_bus (s->int_chain[0]);
620 dbg("Linking skeleton INT-TDs");
622 for (n = 0; n < 1024; n++) {
623 // link all iso-tds to the interrupt chains
624 int m, o;
625 dbg("framelist[%i]=%x",n,s->framelist[n]);
626 if ((n&127)==127)
627 ((uhci_desc_t*) s->iso_td[n])->hw.td.link = virt_to_bus(s->int_chain[0]);
628 else
629 for (o = 1, m = 2; m <= 128; o++, m += m)
630 if ((n & (m - 1)) == ((m - 1) / 2))
631 ((uhci_desc_t*) s->iso_td[n])->hw.td.link = virt_to_bus (s->int_chain[o]);
634 ret = alloc_td (&td, 0);
636 if (ret)
637 goto init_skel_cleanup;
639 fill_td (td, 0 * TD_CTRL_IOC, 0, 0); // generate 32ms interrupt
640 s->td32ms=td;
642 insert_td_horizontal (s, s->int_chain[5], td);
644 mb();
645 //uhci_show_queue(s->control_chain);
646 dbg("init_skel exit");
647 return 0;
649 init_skel_cleanup:
650 cleanup_skel (s);
651 return -ENOMEM;
654 /*-------------------------------------------------------------------*/
655 // LOW LEVEL STUFF
656 // assembles QHs und TDs for control, bulk and iso
657 /*-------------------------------------------------------------------*/
658 _static int uhci_submit_control_urb (urb_t *urb)
660 uhci_desc_t *qh, *td;
661 uhci_t *s = (uhci_t*) urb->dev->bus->hcpriv;
662 urb_priv_t *urb_priv = urb->hcpriv;
663 unsigned long destination, status;
664 int maxsze = usb_maxpacket (urb->dev, urb->pipe, usb_pipeout (urb->pipe));
665 unsigned long len;
666 char *data;
667 int depth_first=USE_CTRL_DEPTH_FIRST; // UHCI descriptor chasing method
669 if (!maxsze) {
670 err("uhci_submit_control_urb: pipesize for pipe %x is zero", urb->pipe);
671 return -EINVAL;
674 dbg("uhci_submit_control start");
675 alloc_qh (&qh); // alloc qh for this request
677 if (!qh)
678 return -ENOMEM;
680 alloc_td (&td, UHCI_PTR_DEPTH * depth_first); // get td for setup stage
682 if (!td) {
683 delete_qh (s, qh);
684 return -ENOMEM;
687 /* The "pipe" thing contains the destination in bits 8--18 */
688 destination = (urb->pipe & PIPE_DEVEP_MASK) | USB_PID_SETUP;
690 /* 3 errors */
691 status = (urb->pipe & TD_CTRL_LS) | TD_CTRL_ACTIVE |
692 (urb->transfer_flags & USB_DISABLE_SPD ? 0 : TD_CTRL_SPD) | (3 << 27);
694 /* Build the TD for the control request, try forever, 8 bytes of data */
695 fill_td (td, status, destination | (7 << 21), virt_to_bus (urb->setup_packet));
697 insert_td (s, qh, td, 0); // queue 'setup stage'-td in qh
698 #if 0
700 char *sp=urb->setup_packet;
701 dbg("SETUP to pipe %x: %x %x %x %x %x %x %x %x", urb->pipe,
702 sp[0],sp[1],sp[2],sp[3],sp[4],sp[5],sp[6],sp[7]);
704 //uhci_show_td(td);
705 #endif
707 len = urb->transfer_buffer_length;
708 data = urb->transfer_buffer;
710 /* If direction is "send", change the frame from SETUP (0x2D)
711 to OUT (0xE1). Else change it from SETUP to IN (0x69). */
713 destination = (urb->pipe & PIPE_DEVEP_MASK) | (usb_pipeout (urb->pipe)?USB_PID_OUT:USB_PID_IN);
715 while (len > 0) {
716 int pktsze = len;
718 alloc_td (&td, UHCI_PTR_DEPTH * depth_first);
719 if (!td) {
720 delete_qh (s, qh);
721 return -ENOMEM;
724 if (pktsze > maxsze)
725 pktsze = maxsze;
727 destination ^= 1 << TD_TOKEN_TOGGLE; // toggle DATA0/1
729 fill_td (td, status, destination | ((pktsze - 1) << 21),
730 virt_to_bus (data)); // Status, pktsze bytes of data
732 insert_td (s, qh, td, UHCI_PTR_DEPTH * depth_first); // queue 'data stage'-td in qh
734 data += pktsze;
735 len -= pktsze;
738 /* Build the final TD for control status */
739 /* It's only IN if the pipe is out AND we aren't expecting data */
741 destination &= ~UHCI_PID;
743 if (usb_pipeout (urb->pipe) || (urb->transfer_buffer_length == 0))
744 destination |= USB_PID_IN;
745 else
746 destination |= USB_PID_OUT;
748 destination |= 1 << TD_TOKEN_TOGGLE; /* End in Data1 */
750 alloc_td (&td, UHCI_PTR_DEPTH);
752 if (!td) {
753 delete_qh (s, qh);
754 return -ENOMEM;
756 status &=~TD_CTRL_SPD;
758 /* no limit on errors on final packet , 0 bytes of data */
759 fill_td (td, status | TD_CTRL_IOC, destination | (UHCI_NULL_DATA_SIZE << 21),
762 insert_td (s, qh, td, UHCI_PTR_DEPTH * depth_first); // queue status td
764 list_add (&qh->desc_list, &urb_priv->desc_list);
766 urb->status = -EINPROGRESS;
767 queue_urb (s, urb); // queue before inserting in desc chain
769 qh->hw.qh.element &= ~UHCI_PTR_TERM;
771 //uhci_show_queue(qh);
772 /* Start it up... put low speed first */
773 if (urb->pipe & TD_CTRL_LS)
774 insert_qh (s, s->control_chain, qh, 0);
775 else
776 insert_qh (s, s->bulk_chain, qh, 0);
778 dbg("uhci_submit_control end");
779 return 0;
781 /*-------------------------------------------------------------------*/
782 // For queued bulk transfers, two additional QH helpers are allocated (nqh, bqh)
783 // Due to the linking with other bulk urbs, it has to be locked with urb_list_lock!
785 _static int uhci_submit_bulk_urb (urb_t *urb, urb_t *bulk_urb)
787 uhci_t *s = (uhci_t*) urb->dev->bus->hcpriv;
788 urb_priv_t *urb_priv = urb->hcpriv;
789 uhci_desc_t *qh, *td, *nqh, *bqh;
790 unsigned long destination, status;
791 char *data;
792 unsigned int pipe = urb->pipe;
793 int maxsze = usb_maxpacket (urb->dev, pipe, usb_pipeout (pipe));
794 int info, len;
795 int depth_first=USE_BULK_DEPTH_FIRST; // UHCI descriptor chasing method
796 urb_priv_t *upriv, *bpriv;
798 if (usb_endpoint_halted (urb->dev, usb_pipeendpoint (pipe), usb_pipeout (pipe)))
799 return -EPIPE;
801 if (urb->transfer_buffer_length < 0) {
802 err("Negative transfer length in submit_bulk");
803 return -EINVAL;
806 if (!maxsze)
807 return -EMSGSIZE;
809 queue_dbg("uhci_submit_bulk_urb: urb %p, old %p, pipe %08x, len %i",
810 urb,bulk_urb,urb->pipe,urb->transfer_buffer_length);
812 upriv=(urb_priv_t*)urb->hcpriv;
814 if (!bulk_urb) {
815 alloc_qh (&qh); // get qh for this request
817 if (!qh)
818 return -ENOMEM;
820 if (urb->transfer_flags & USB_QUEUE_BULK) {
821 alloc_qh(&nqh); // placeholder for clean unlink
822 if (!nqh) {
823 delete_desc (qh);
824 return -ENOMEM;
826 upriv->next_qh = nqh;
827 queue_dbg("new next qh %p",nqh);
830 else {
831 bpriv = (urb_priv_t*)bulk_urb->hcpriv;
832 qh = bpriv->bottom_qh; // re-use bottom qh and next qh
833 nqh = bpriv->next_qh;
834 upriv->next_qh=nqh;
835 bpriv->next_queued_urb=urb;
836 upriv->prev_queued_urb=bulk_urb;
839 queue_dbg("uhci_submit_bulk: qh=%p, nqh=%p\n",bqh,nqh);
841 if (urb->transfer_flags & USB_QUEUE_BULK) {
842 alloc_qh (&bqh); // "bottom" QH,
844 if (!bqh) {
845 if (!bulk_urb) {
846 delete_desc(qh);
847 delete_desc(nqh);
849 return -ENOMEM;
851 bqh->hw.qh.element = UHCI_PTR_TERM;
852 bqh->hw.qh.element = virt_to_bus(nqh)|UHCI_PTR_QH;
853 upriv->bottom_qh = bqh;
854 queue_dbg("uhci_submit_bulk: new bqh %p\n",bqh);
858 /* The "pipe" thing contains the destination in bits 8--18. */
859 destination = (pipe & PIPE_DEVEP_MASK) | usb_packetid (pipe);
861 /* 3 errors */
862 status = (pipe & TD_CTRL_LS) | TD_CTRL_ACTIVE |
863 ((urb->transfer_flags & USB_DISABLE_SPD) ? 0 : TD_CTRL_SPD) | (3 << 27);
865 /* Build the TDs for the bulk request */
866 len = urb->transfer_buffer_length;
867 data = urb->transfer_buffer;
869 do { // TBD: Really allow zero-length packets?
870 int pktsze = len;
872 alloc_td (&td, UHCI_PTR_DEPTH * depth_first);
874 if (!td) {
875 delete_qh (s, qh);
876 return -ENOMEM;
879 if (pktsze > maxsze)
880 pktsze = maxsze;
882 // pktsze bytes of data
883 info = destination | (((pktsze - 1)&UHCI_NULL_DATA_SIZE) << 21) |
884 (usb_gettoggle (urb->dev, usb_pipeendpoint (pipe), usb_pipeout (pipe)) << TD_TOKEN_TOGGLE);
886 fill_td (td, status, info, virt_to_bus (data));
888 data += pktsze;
889 len -= pktsze;
891 if (!len)
892 td->hw.td.status |= TD_CTRL_IOC; // last one generates INT
894 insert_td (s, qh, td, UHCI_PTR_DEPTH * depth_first);
895 usb_dotoggle (urb->dev, usb_pipeendpoint (pipe), usb_pipeout (pipe));
897 } while (len > 0);
899 list_add (&qh->desc_list, &urb_priv->desc_list);
901 if (urb->transfer_flags & USB_QUEUE_BULK) {
902 qh->hw.qh.element&=~UHCI_PTR_TERM;
903 append_qh(s, td, bqh, UHCI_PTR_DEPTH * depth_first);
906 urb->status = -EINPROGRESS;
907 queue_urb_unlocked (s, urb);
909 qh->hw.qh.element &= ~UHCI_PTR_TERM;
911 if (!bulk_urb) {
912 if (urb->transfer_flags & USB_QUEUE_BULK) {
913 spin_lock (&s->td_lock); // both QHs in one go
914 insert_qh (s, s->chain_end, qh, 0); // Main QH
915 insert_qh (s, s->chain_end, nqh, 0); // Helper QH
916 spin_unlock (&s->td_lock);
918 else
919 insert_qh (s, s->chain_end, qh, 0);
922 //uhci_show_queue(s->bulk_chain);
923 //dbg("uhci_submit_bulk_urb: exit\n");
924 return 0;
926 /*-------------------------------------------------------------------*/
927 _static void uhci_clean_iso_step1(uhci_t *s, urb_priv_t *urb_priv)
929 struct list_head *p;
930 uhci_desc_t *td;
932 for (p = urb_priv->desc_list.next; p != &urb_priv->desc_list; p = p->next) {
933 td = list_entry (p, uhci_desc_t, desc_list);
934 unlink_td (s, td, 1);
937 /*-------------------------------------------------------------------*/
938 _static void uhci_clean_iso_step2(uhci_t *s, urb_priv_t *urb_priv)
940 struct list_head *p;
941 uhci_desc_t *td;
943 while ((p = urb_priv->desc_list.next) != &urb_priv->desc_list) {
944 td = list_entry (p, uhci_desc_t, desc_list);
945 list_del (p);
946 delete_desc (td);
949 /*-------------------------------------------------------------------*/
950 // mode: 0: unlink + no deletion mark, 1: regular (unlink/delete-mark), 2: don't unlink
951 // looks a bit complicated because of all the bulk queueing goodies
953 _static void uhci_clean_transfer (uhci_t *s, urb_t *urb, uhci_desc_t *qh, int mode)
955 uhci_desc_t *bqh, *nqh, *prevqh;
956 int now;
957 urb_priv_t *priv=(urb_priv_t*)urb->hcpriv;
959 now=UHCI_GET_CURRENT_FRAME(s);
961 dbg("clean transfer urb %p, qh %p, mode %i",urb,qh,mode);
962 bqh=priv->bottom_qh;
964 if (!priv->next_queued_urb) { // no more appended bulk queues
966 if (mode != 2)
967 unlink_qh (s, qh);
969 if (priv->prev_queued_urb) {
970 urb_priv_t* ppriv=(urb_priv_t*)priv->prev_queued_urb->hcpriv;
972 ppriv->bottom_qh = priv->bottom_qh;
973 ppriv->next_queued_urb = NULL;
975 else if (bqh) { // queue dead
976 nqh=priv->next_qh;
978 if (mode != 2)
979 unlink_qh(s, nqh);
981 if (mode) {
982 nqh->last_used = bqh->last_used = now;
983 list_add_tail (&nqh->horizontal, &s->free_desc);
984 list_add_tail (&bqh->horizontal, &s->free_desc);
988 else { // there are queued urbs following
989 urb_t *nurb;
990 unsigned long flags;
992 nurb=priv->next_queued_urb;
993 spin_lock_irqsave (&s->qh_lock, flags);
995 if (!priv->prev_queued_urb) { // top
996 if (mode !=2) {
997 prevqh = list_entry (qh->horizontal.prev, uhci_desc_t, horizontal);
998 prevqh->hw.qh.head = virt_to_bus(bqh) | UHCI_PTR_QH;
999 queue_dbg ("TOP relink of %p to %p-%p",qh,prevqh,bqh);
1001 list_del (&qh->horizontal);
1002 list_add (&bqh->horizontal, &prevqh->horizontal);
1005 else { //intermediate
1006 urb_priv_t* ppriv=(urb_priv_t*)priv->prev_queued_urb->hcpriv;
1007 uhci_desc_t * bnqh;
1009 bnqh=list_entry (&((urb_priv_t*)(nurb->hcpriv))->desc_list.next, uhci_desc_t, desc_list);
1010 ppriv->bottom_qh=bnqh;
1011 ppriv->next_queued_urb=nurb;
1013 if (mode!=2) {
1014 prevqh = list_entry (ppriv->desc_list.next, uhci_desc_t, desc_list);
1015 prevqh->hw.qh.head = virt_to_bus(bqh) | UHCI_PTR_QH;
1016 queue_dbg ("IM relink of %p to %p-%p",qh,prevqh,bqh);
1019 mb();
1020 spin_unlock_irqrestore (&s->qh_lock, flags);
1021 ((urb_priv_t*)nurb->hcpriv)->prev_queued_urb=priv->prev_queued_urb;
1024 if (mode) {
1025 qh->last_used = now;
1026 list_add_tail (&qh->horizontal, &s->free_desc); // mark for later deletion
1029 /*-------------------------------------------------------------------*/
1030 // unlinks an urb by dequeuing its qh, waits some frames and forgets it
1031 _static int uhci_unlink_urb_sync (uhci_t *s, urb_t *urb)
1033 uhci_desc_t *qh;
1034 urb_priv_t *urb_priv;
1035 unsigned long flags=0;
1037 spin_lock_irqsave (&s->urb_list_lock, flags);
1039 if (urb->status == -EINPROGRESS) {
1040 // URB probably still in work
1041 dequeue_urb (s, urb);
1042 uhci_switch_timer_int(s);
1043 s->unlink_urb_done=1;
1044 spin_unlock_irqrestore (&s->urb_list_lock, flags);
1046 urb->status = -ENOENT; // mark urb as killed
1047 urb_priv = urb->hcpriv;
1049 switch (usb_pipetype (urb->pipe)) {
1051 case PIPE_INTERRUPT:
1052 usb_dotoggle (urb->dev, usb_pipeendpoint (urb->pipe), usb_pipeout (urb->pipe));
1054 case PIPE_ISOCHRONOUS:
1055 uhci_clean_iso_step1(s, urb_priv);
1056 uhci_wait_ms(1);
1057 uhci_clean_iso_step2(s, urb_priv);
1058 break;
1060 case PIPE_BULK:
1061 case PIPE_CONTROL:
1062 qh = list_entry (urb_priv->desc_list.next, uhci_desc_t, desc_list);
1063 spin_lock_irqsave (&s->urb_list_lock, flags);
1064 uhci_clean_transfer(s, urb, qh, 1);
1065 spin_unlock_irqrestore (&s->urb_list_lock, flags);
1066 uhci_wait_ms(1);
1069 #ifdef DEBUG_SLAB
1070 kmem_cache_free (urb_priv_kmem, urb->hcpriv);
1071 #else
1072 kfree (urb->hcpriv);
1073 #endif
1074 if (urb->complete) {
1075 dbg("unlink_urb: calling completion");
1076 urb->complete ((struct urb *) urb);
1078 usb_dec_dev_use (urb->dev);
1079 return 0;
1081 else
1082 spin_unlock_irqrestore (&s->urb_list_lock, flags);
1083 return 0;
1085 /*-------------------------------------------------------------------*/
1086 // async unlink_urb completion/cleanup work
1087 // has to be protected by urb_list_lock!
1088 // features: if set in transfer_flags, the resulting status of the killed
1089 // transaction is not overwritten
1091 _static void uhci_cleanup_unlink(uhci_t *s, int force)
1093 struct list_head *q;
1094 urb_t *urb;
1095 struct usb_device *dev;
1096 int pipe,now;
1097 urb_priv_t *urb_priv;
1099 q=s->urb_unlinked.next;
1100 now=UHCI_GET_CURRENT_FRAME(s);
1102 while (q != &s->urb_unlinked) {
1104 urb = list_entry (q, urb_t, urb_list);
1106 urb_priv = (urb_priv_t*)urb->hcpriv;
1107 q = urb->urb_list.next;
1109 if (force ||
1110 ((urb_priv->started != 0xffffffff) && (urb_priv->started != now))) {
1111 async_dbg("async cleanup %p",urb);
1112 switch (usb_pipetype (urb->pipe)) { // process descriptors
1113 case PIPE_CONTROL:
1114 process_transfer (s, urb, 2);
1115 break;
1116 case PIPE_BULK:
1117 if (!s->avoid_bulk.counter)
1118 process_transfer (s, urb, 2); // don't unlink (already done)
1119 else
1120 continue;
1121 break;
1122 case PIPE_ISOCHRONOUS:
1123 process_iso (s, urb, 1); // force, don't unlink
1124 break;
1125 case PIPE_INTERRUPT:
1126 process_interrupt (s, urb);
1127 break;
1130 if (!(urb->transfer_flags & USB_TIMEOUT_KILLED))
1131 urb->status = -ECONNRESET; // mark as asynchronously killed
1133 pipe = urb->pipe; // completion may destroy all...
1134 dev = urb->dev;
1135 urb_priv = urb->hcpriv;
1137 if (urb->complete) {
1138 spin_unlock(&s->urb_list_lock);
1139 urb->complete ((struct urb *) urb);
1140 spin_lock(&s->urb_list_lock);
1143 if (!(urb->transfer_flags & USB_TIMEOUT_KILLED))
1144 urb->status = -ENOENT; // now the urb is really dead
1145 switch (usb_pipetype (pipe)) {
1146 case PIPE_ISOCHRONOUS:
1147 case PIPE_INTERRUPT:
1148 uhci_clean_iso_step2(s, urb_priv);
1149 break;
1152 usb_dec_dev_use (dev);
1153 #ifdef DEBUG_SLAB
1154 kmem_cache_free (urb_priv_kmem, urb_priv);
1155 #else
1156 kfree (urb_priv);
1157 #endif
1159 list_del (&urb->urb_list);
1164 /*-------------------------------------------------------------------*/
1165 _static int uhci_unlink_urb_async (uhci_t *s,urb_t *urb)
1167 uhci_desc_t *qh;
1168 urb_priv_t *urb_priv;
1170 async_dbg("unlink_urb_async called %p",urb);
1172 if ((urb->status == -EINPROGRESS) ||
1173 ((usb_pipetype (urb->pipe) == PIPE_INTERRUPT) && ((urb_priv_t*)urb->hcpriv)->flags))
1175 ((urb_priv_t*)urb->hcpriv)->started = ~0;
1177 dequeue_urb (s, urb);
1178 list_add_tail (&urb->urb_list, &s->urb_unlinked); // store urb
1179 uhci_switch_timer_int(s);
1181 s->unlink_urb_done = 1;
1183 urb->status = -ECONNABORTED; // mark urb as "waiting to be killed"
1184 urb_priv = (urb_priv_t*)urb->hcpriv;
1186 switch (usb_pipetype (urb->pipe)) {
1187 case PIPE_INTERRUPT:
1188 usb_dotoggle (urb->dev, usb_pipeendpoint (urb->pipe), usb_pipeout (urb->pipe));
1190 case PIPE_ISOCHRONOUS:
1191 uhci_clean_iso_step1 (s, urb_priv);
1192 break;
1194 case PIPE_BULK:
1195 case PIPE_CONTROL:
1196 qh = list_entry (urb_priv->desc_list.next, uhci_desc_t, desc_list);
1197 uhci_clean_transfer (s, urb, qh, 0);
1198 break;
1200 ((urb_priv_t*)urb->hcpriv)->started = UHCI_GET_CURRENT_FRAME(s);
1203 return -EINPROGRESS;
1205 /*-------------------------------------------------------------------*/
1206 _static int uhci_unlink_urb (urb_t *urb)
1208 uhci_t *s;
1209 unsigned long flags=0;
1210 dbg("uhci_unlink_urb called for %p",urb);
1211 if (!urb || !urb->dev) // you never know...
1212 return -EINVAL;
1214 s = (uhci_t*) urb->dev->bus->hcpriv;
1216 if (usb_pipedevice (urb->pipe) == s->rh.devnum)
1217 return rh_unlink_urb (urb);
1219 if (!urb->hcpriv)
1220 return -EINVAL;
1222 if (urb->transfer_flags & USB_ASYNC_UNLINK) {
1223 int ret;
1225 spin_lock_irqsave (&s->urb_list_lock, flags);
1226 ret = uhci_unlink_urb_async(s, urb);
1227 spin_unlock_irqrestore (&s->urb_list_lock, flags);
1228 return ret;
1230 else
1231 return uhci_unlink_urb_sync(s, urb);
1233 /*-------------------------------------------------------------------*/
1234 // In case of ASAP iso transfer, search the URB-list for already queued URBs
1235 // for this EP and calculate the earliest start frame for the new
1236 // URB (easy seamless URB continuation!)
1237 _static int find_iso_limits (urb_t *urb, unsigned int *start, unsigned int *end)
1239 urb_t *u, *last_urb = NULL;
1240 uhci_t *s = (uhci_t*) urb->dev->bus->hcpriv;
1241 struct list_head *p;
1242 int ret=-1;
1243 unsigned long flags;
1245 spin_lock_irqsave (&s->urb_list_lock, flags);
1246 p=s->urb_list.prev;
1248 for (; p != &s->urb_list; p = p->prev) {
1249 u = list_entry (p, urb_t, urb_list);
1250 // look for pending URBs with identical pipe handle
1251 // works only because iso doesn't toggle the data bit!
1252 if ((urb->pipe == u->pipe) && (urb->dev == u->dev) && (u->status == -EINPROGRESS)) {
1253 if (!last_urb)
1254 *start = u->start_frame;
1255 last_urb = u;
1259 if (last_urb) {
1260 *end = (last_urb->start_frame + last_urb->number_of_packets) & 1023;
1261 ret=0;
1264 spin_unlock_irqrestore(&s->urb_list_lock, flags);
1266 return ret;
1268 /*-------------------------------------------------------------------*/
1269 // adjust start_frame according to scheduling constraints (ASAP etc)
1271 _static int iso_find_start (urb_t *urb)
1273 uhci_t *s = (uhci_t*) urb->dev->bus->hcpriv;
1274 unsigned int now;
1275 unsigned int start_limit = 0, stop_limit = 0, queued_size;
1276 int limits;
1278 now = UHCI_GET_CURRENT_FRAME (s) & 1023;
1280 if ((unsigned) urb->number_of_packets > 900)
1281 return -EFBIG;
1283 limits = find_iso_limits (urb, &start_limit, &stop_limit);
1284 queued_size = (stop_limit - start_limit) & 1023;
1286 if (urb->transfer_flags & USB_ISO_ASAP) {
1287 // first iso
1288 if (limits) {
1289 // 10ms setup should be enough //FIXME!
1290 urb->start_frame = (now + 10) & 1023;
1292 else {
1293 urb->start_frame = stop_limit; //seamless linkage
1295 if (((now - urb->start_frame) & 1023) <= (unsigned) urb->number_of_packets) {
1296 info("iso_find_start: gap in seamless isochronous scheduling");
1297 dbg("iso_find_start: now %u start_frame %u number_of_packets %u pipe 0x%08x",
1298 now, urb->start_frame, urb->number_of_packets, urb->pipe);
1299 urb->start_frame = (now + 5) & 1023; // 5ms setup should be enough //FIXME!
1303 else {
1304 urb->start_frame &= 1023;
1305 if (((now - urb->start_frame) & 1023) < (unsigned) urb->number_of_packets) {
1306 dbg("iso_find_start: now between start_frame and end");
1307 return -EAGAIN;
1311 /* check if either start_frame or start_frame+number_of_packets-1 lies between start_limit and stop_limit */
1312 if (limits)
1313 return 0;
1315 if (((urb->start_frame - start_limit) & 1023) < queued_size ||
1316 ((urb->start_frame + urb->number_of_packets - 1 - start_limit) & 1023) < queued_size) {
1317 dbg("iso_find_start: start_frame %u number_of_packets %u start_limit %u stop_limit %u",
1318 urb->start_frame, urb->number_of_packets, start_limit, stop_limit);
1319 return -EAGAIN;
1322 return 0;
1324 /*-------------------------------------------------------------------*/
1325 // submits USB interrupt (ie. polling ;-)
1326 // ASAP-flag set implicitely
1327 // if period==0, the the transfer is only done once
1329 _static int uhci_submit_int_urb (urb_t *urb)
1331 uhci_t *s = (uhci_t*) urb->dev->bus->hcpriv;
1332 urb_priv_t *urb_priv = urb->hcpriv;
1333 int nint, n, ret;
1334 uhci_desc_t *td;
1335 int status, destination;
1336 int info;
1337 unsigned int pipe = urb->pipe;
1339 if (urb->interval < 0 || urb->interval >= 256)
1340 return -EINVAL;
1342 if (urb->interval == 0)
1343 nint = 0;
1344 else {
1345 for (nint = 0, n = 1; nint <= 8; nint++, n += n) // round interval down to 2^n
1347 if (urb->interval < n) {
1348 urb->interval = n / 2;
1349 break;
1352 nint--;
1355 dbg("Rounded interval to %i, chain %i", urb->interval, nint);
1357 urb->start_frame = UHCI_GET_CURRENT_FRAME (s) & 1023; // remember start frame, just in case...
1359 urb->number_of_packets = 1;
1361 // INT allows only one packet
1362 if (urb->transfer_buffer_length > usb_maxpacket (urb->dev, pipe, usb_pipeout (pipe)))
1363 return -EINVAL;
1365 ret = alloc_td (&td, UHCI_PTR_DEPTH);
1367 if (ret)
1368 return -ENOMEM;
1370 status = (pipe & TD_CTRL_LS) | TD_CTRL_ACTIVE | TD_CTRL_IOC |
1371 (urb->transfer_flags & USB_DISABLE_SPD ? 0 : TD_CTRL_SPD) | (3 << 27);
1373 destination = (urb->pipe & PIPE_DEVEP_MASK) | usb_packetid (urb->pipe) |
1374 (((urb->transfer_buffer_length - 1) & 0x7ff) << 21);
1377 info = destination | (usb_gettoggle (urb->dev, usb_pipeendpoint (pipe), usb_pipeout (pipe)) << TD_TOKEN_TOGGLE);
1379 fill_td (td, status, info, virt_to_bus (urb->transfer_buffer));
1380 list_add_tail (&td->desc_list, &urb_priv->desc_list);
1382 urb->status = -EINPROGRESS;
1383 queue_urb (s, urb);
1385 insert_td_horizontal (s, s->int_chain[nint], td); // store in INT-TDs
1387 usb_dotoggle (urb->dev, usb_pipeendpoint (pipe), usb_pipeout (pipe));
1389 return 0;
1391 /*-------------------------------------------------------------------*/
1392 _static int uhci_submit_iso_urb (urb_t *urb)
1394 uhci_t *s = (uhci_t*) urb->dev->bus->hcpriv;
1395 urb_priv_t *urb_priv = urb->hcpriv;
1396 #ifdef ISO_SANITY_CHECK
1397 int pipe=urb->pipe;
1398 int maxsze = usb_maxpacket (urb->dev, pipe, usb_pipeout (pipe));
1399 #endif
1400 int n, ret, last=0;
1401 uhci_desc_t *td, **tdm;
1402 int status, destination;
1403 unsigned long flags;
1405 __save_flags(flags);
1406 __cli(); // Disable IRQs to schedule all ISO-TDs in time
1407 ret = iso_find_start (urb); // adjusts urb->start_frame for later use
1409 if (ret)
1410 goto err;
1412 tdm = (uhci_desc_t **) kmalloc (urb->number_of_packets * sizeof (uhci_desc_t*), KMALLOC_FLAG);
1414 if (!tdm) {
1415 ret = -ENOMEM;
1416 goto err;
1419 // First try to get all TDs
1420 for (n = 0; n < urb->number_of_packets; n++) {
1421 dbg("n:%d urb->iso_frame_desc[n].length:%d", n, urb->iso_frame_desc[n].length);
1422 if (!urb->iso_frame_desc[n].length) {
1423 // allows ISO striping by setting length to zero in iso_descriptor
1424 tdm[n] = 0;
1425 continue;
1428 #ifdef ISO_SANITY_CHECK
1429 if(urb->iso_frame_desc[n].length > maxsze) {
1430 err("submit_iso: urb->iso_frame_desc[%d].length(%d)>%d",n , urb->iso_frame_desc[n].length, maxsze);
1431 tdm[n] = 0;
1432 ret=-EINVAL;
1433 goto inval;
1435 else
1436 #endif
1437 ret = alloc_td (&td, UHCI_PTR_DEPTH);
1439 if (ret) {
1440 int i; // Cleanup allocated TDs
1442 for (i = 0; i < n; n++)
1443 if (tdm[i])
1444 delete_desc(tdm[i]);
1445 kfree (tdm);
1446 goto err;
1448 last=n;
1449 tdm[n] = td;
1452 status = TD_CTRL_ACTIVE | TD_CTRL_IOS; //| (urb->transfer_flags&USB_DISABLE_SPD?0:TD_CTRL_SPD);
1454 destination = (urb->pipe & PIPE_DEVEP_MASK) | usb_packetid (urb->pipe);
1457 // Queue all allocated TDs
1458 for (n = 0; n < urb->number_of_packets; n++) {
1459 td = tdm[n];
1460 if (!td)
1461 continue;
1463 if (n == last)
1464 status |= TD_CTRL_IOC;
1466 fill_td (td, status, destination | (((urb->iso_frame_desc[n].length - 1) & 0x7ff) << 21),
1467 virt_to_bus (urb->transfer_buffer + urb->iso_frame_desc[n].offset));
1468 list_add_tail (&td->desc_list, &urb_priv->desc_list);
1470 if (n == last) {
1471 urb->status = -EINPROGRESS;
1472 queue_urb (s, urb);
1474 insert_td_horizontal (s, s->iso_td[(urb->start_frame + n) & 1023], td); // store in iso-tds
1475 //uhci_show_td(td);
1479 kfree (tdm);
1480 dbg("ISO-INT# %i, start %i, now %i", urb->number_of_packets, urb->start_frame, UHCI_GET_CURRENT_FRAME (s) & 1023);
1481 ret = 0;
1483 err:
1484 __restore_flags(flags);
1485 return ret;
1488 /*-------------------------------------------------------------------*/
1489 // returns: 0 (no transfer queued), urb* (this urb already queued)
1491 _static urb_t* search_dev_ep (uhci_t *s, urb_t *urb)
1493 struct list_head *p;
1494 urb_t *tmp;
1495 unsigned int mask = usb_pipecontrol(urb->pipe) ? (~USB_DIR_IN) : (~0);
1497 dbg("search_dev_ep:");
1499 p=s->urb_list.next;
1501 for (; p != &s->urb_list; p = p->next) {
1502 tmp = list_entry (p, urb_t, urb_list);
1503 dbg("urb: %p", tmp);
1504 // we can accept this urb if it is not queued at this time
1505 // or if non-iso transfer requests should be scheduled for the same device and pipe
1506 if ((!usb_pipeisoc(urb->pipe) && (tmp->dev == urb->dev) && !((tmp->pipe ^ urb->pipe) & mask)) ||
1507 (urb == tmp)) {
1508 return tmp; // found another urb already queued for processing
1512 return 0;
1514 /*-------------------------------------------------------------------*/
1515 _static int uhci_submit_urb (urb_t *urb)
1517 uhci_t *s;
1518 urb_priv_t *urb_priv;
1519 int ret = 0;
1520 unsigned long flags;
1521 urb_t *bulk_urb=NULL;
1523 if (!urb->dev || !urb->dev->bus)
1524 return -ENODEV;
1526 s = (uhci_t*) urb->dev->bus->hcpriv;
1527 //dbg("submit_urb: %p type %d",urb,usb_pipetype(urb->pipe));
1529 if (!s->running)
1530 return -ENODEV;
1532 if (usb_pipedevice (urb->pipe) == s->rh.devnum)
1533 return rh_submit_urb (urb); /* virtual root hub */
1535 usb_inc_dev_use (urb->dev);
1537 spin_lock_irqsave (&s->urb_list_lock, flags);
1539 bulk_urb = search_dev_ep (s, urb);
1541 if (bulk_urb) {
1543 queue_dbg("found bulk urb %p\n",bulk_urb);
1545 if ((usb_pipetype (urb->pipe) != PIPE_BULK) ||
1546 ((usb_pipetype (urb->pipe) == PIPE_BULK) &&
1547 (!(urb->transfer_flags & USB_QUEUE_BULK) || !(bulk_urb->transfer_flags & USB_QUEUE_BULK)))) {
1548 spin_unlock_irqrestore (&s->urb_list_lock, flags);
1549 usb_dec_dev_use (urb->dev);
1550 err("ENXIO %08x, flags %x, urb %p, burb %p",urb->pipe,urb->transfer_flags,urb,bulk_urb);
1551 return -ENXIO; // urb already queued
1555 #ifdef DEBUG_SLAB
1556 urb_priv = kmem_cache_alloc(urb_priv_kmem, SLAB_FLAG);
1557 #else
1558 urb_priv = kmalloc (sizeof (urb_priv_t), KMALLOC_FLAG);
1559 #endif
1560 if (!urb_priv) {
1561 usb_dec_dev_use (urb->dev);
1562 spin_unlock_irqrestore (&s->urb_list_lock, flags);
1563 return -ENOMEM;
1566 urb->hcpriv = urb_priv;
1567 INIT_LIST_HEAD (&urb_priv->desc_list);
1568 urb_priv->flags = 0;
1569 dbg("submit_urb: scheduling %p", urb);
1570 urb_priv->next_queued_urb = NULL;
1571 urb_priv->prev_queued_urb = NULL;
1572 urb_priv->bottom_qh = NULL;
1573 urb_priv->next_qh = NULL;
1575 if (usb_pipetype (urb->pipe) == PIPE_BULK) {
1577 if (bulk_urb) {
1578 while (((urb_priv_t*)bulk_urb->hcpriv)->next_queued_urb) // find last queued bulk
1579 bulk_urb=((urb_priv_t*)bulk_urb->hcpriv)->next_queued_urb;
1581 ((urb_priv_t*)bulk_urb->hcpriv)->next_queued_urb=urb;
1583 atomic_inc (&s->avoid_bulk);
1584 ret = uhci_submit_bulk_urb (urb, bulk_urb);
1585 atomic_dec (&s->avoid_bulk);
1586 spin_unlock_irqrestore (&s->urb_list_lock, flags);
1588 else {
1589 spin_unlock_irqrestore (&s->urb_list_lock, flags);
1590 switch (usb_pipetype (urb->pipe)) {
1591 case PIPE_ISOCHRONOUS:
1592 ret = uhci_submit_iso_urb (urb);
1593 break;
1594 case PIPE_INTERRUPT:
1595 ret = uhci_submit_int_urb (urb);
1596 break;
1597 case PIPE_CONTROL:
1598 ret = uhci_submit_control_urb (urb);
1599 break;
1600 default:
1601 ret = -EINVAL;
1605 dbg("submit_urb: scheduled with ret: %d", ret);
1607 if (ret != 0) {
1608 usb_dec_dev_use (urb->dev);
1609 #ifdef DEBUG_SLAB
1610 kmem_cache_free(urb_priv_kmem, urb_priv);
1611 #else
1612 kfree (urb_priv);
1613 #endif
1614 return ret;
1617 return 0;
1620 // Checks for URB timeout and removes bandwidth reclamation
1621 // if URB idles too long
1622 _static void uhci_check_timeouts(uhci_t *s)
1624 struct list_head *p,*p2;
1625 urb_t *urb;
1626 int type;
1628 p = s->urb_list.prev;
1630 while (p != &s->urb_list) {
1631 urb_priv_t *hcpriv;
1633 p2 = p;
1634 p = p->prev;
1635 urb = list_entry (p2, urb_t, urb_list);
1636 type = usb_pipetype (urb->pipe);
1638 hcpriv = (urb_priv_t*)urb->hcpriv;
1640 if ( urb->timeout &&
1641 ((hcpriv->started + urb->timeout) < jiffies)) {
1642 urb->transfer_flags |= USB_TIMEOUT_KILLED | USB_ASYNC_UNLINK;
1643 async_dbg("uhci_check_timeout: timeout for %p",urb);
1644 uhci_unlink_urb_async(s, urb);
1646 #ifdef CONFIG_USB_UHCI_HIGH_BANDWIDTH
1647 else if (((type == PIPE_BULK) || (type == PIPE_CONTROL)) &&
1648 (hcpriv->use_loop) &&
1649 ((hcpriv->started + IDLE_TIMEOUT) < jiffies))
1650 disable_desc_loop(s, urb);
1651 #endif
1654 s->timeout_check=jiffies;
1657 /*-------------------------------------------------------------------
1658 Virtual Root Hub
1659 -------------------------------------------------------------------*/
1661 _static __u8 root_hub_dev_des[] =
1663 0x12, /* __u8 bLength; */
1664 0x01, /* __u8 bDescriptorType; Device */
1665 0x00, /* __u16 bcdUSB; v1.0 */
1666 0x01,
1667 0x09, /* __u8 bDeviceClass; HUB_CLASSCODE */
1668 0x00, /* __u8 bDeviceSubClass; */
1669 0x00, /* __u8 bDeviceProtocol; */
1670 0x08, /* __u8 bMaxPacketSize0; 8 Bytes */
1671 0x00, /* __u16 idVendor; */
1672 0x00,
1673 0x00, /* __u16 idProduct; */
1674 0x00,
1675 0x00, /* __u16 bcdDevice; */
1676 0x00,
1677 0x00, /* __u8 iManufacturer; */
1678 0x02, /* __u8 iProduct; */
1679 0x01, /* __u8 iSerialNumber; */
1680 0x01 /* __u8 bNumConfigurations; */
1684 /* Configuration descriptor */
1685 _static __u8 root_hub_config_des[] =
1687 0x09, /* __u8 bLength; */
1688 0x02, /* __u8 bDescriptorType; Configuration */
1689 0x19, /* __u16 wTotalLength; */
1690 0x00,
1691 0x01, /* __u8 bNumInterfaces; */
1692 0x01, /* __u8 bConfigurationValue; */
1693 0x00, /* __u8 iConfiguration; */
1694 0x40, /* __u8 bmAttributes;
1695 Bit 7: Bus-powered, 6: Self-powered, 5 Remote-wakwup, 4..0: resvd */
1696 0x00, /* __u8 MaxPower; */
1698 /* interface */
1699 0x09, /* __u8 if_bLength; */
1700 0x04, /* __u8 if_bDescriptorType; Interface */
1701 0x00, /* __u8 if_bInterfaceNumber; */
1702 0x00, /* __u8 if_bAlternateSetting; */
1703 0x01, /* __u8 if_bNumEndpoints; */
1704 0x09, /* __u8 if_bInterfaceClass; HUB_CLASSCODE */
1705 0x00, /* __u8 if_bInterfaceSubClass; */
1706 0x00, /* __u8 if_bInterfaceProtocol; */
1707 0x00, /* __u8 if_iInterface; */
1709 /* endpoint */
1710 0x07, /* __u8 ep_bLength; */
1711 0x05, /* __u8 ep_bDescriptorType; Endpoint */
1712 0x81, /* __u8 ep_bEndpointAddress; IN Endpoint 1 */
1713 0x03, /* __u8 ep_bmAttributes; Interrupt */
1714 0x08, /* __u16 ep_wMaxPacketSize; 8 Bytes */
1715 0x00,
1716 0xff /* __u8 ep_bInterval; 255 ms */
1720 _static __u8 root_hub_hub_des[] =
1722 0x09, /* __u8 bLength; */
1723 0x29, /* __u8 bDescriptorType; Hub-descriptor */
1724 0x02, /* __u8 bNbrPorts; */
1725 0x00, /* __u16 wHubCharacteristics; */
1726 0x00,
1727 0x01, /* __u8 bPwrOn2pwrGood; 2ms */
1728 0x00, /* __u8 bHubContrCurrent; 0 mA */
1729 0x00, /* __u8 DeviceRemovable; *** 7 Ports max *** */
1730 0xff /* __u8 PortPwrCtrlMask; *** 7 ports max *** */
1733 /*-------------------------------------------------------------------------*/
1734 /* prepare Interrupt pipe transaction data; HUB INTERRUPT ENDPOINT */
1735 _static int rh_send_irq (urb_t *urb)
1737 int len = 1;
1738 int i;
1739 uhci_t *uhci = urb->dev->bus->hcpriv;
1740 unsigned int io_addr = uhci->io_addr;
1741 __u16 data = 0;
1743 for (i = 0; i < uhci->rh.numports; i++) {
1744 data |= ((inw (io_addr + USBPORTSC1 + i * 2) & 0xa) > 0 ? (1 << (i + 1)) : 0);
1745 len = (i + 1) / 8 + 1;
1748 *(__u16 *) urb->transfer_buffer = cpu_to_le16 (data);
1749 urb->actual_length = len;
1750 urb->status = 0;
1752 if ((data > 0) && (uhci->rh.send != 0)) {
1753 dbg("Root-Hub INT complete: port1: %x port2: %x data: %x",
1754 inw (io_addr + USBPORTSC1), inw (io_addr + USBPORTSC2), data);
1755 urb->complete (urb);
1757 return 0;
1760 /*-------------------------------------------------------------------------*/
1761 /* Virtual Root Hub INTs are polled by this timer every "intervall" ms */
1762 _static int rh_init_int_timer (urb_t *urb);
1764 _static void rh_int_timer_do (unsigned long ptr)
1766 int len;
1767 urb_t *urb = (urb_t*) ptr;
1768 uhci_t *uhci = urb->dev->bus->hcpriv;
1770 if (uhci->rh.send) {
1771 len = rh_send_irq (urb);
1772 if (len > 0) {
1773 urb->actual_length = len;
1774 if (urb->complete)
1775 urb->complete (urb);
1778 rh_init_int_timer (urb);
1781 /*-------------------------------------------------------------------------*/
1782 /* Root Hub INTs are polled by this timer, polling interval 20ms */
1783 /* This time is also used for URB-timeout checking */
1785 _static int rh_init_int_timer (urb_t *urb)
1787 uhci_t *uhci = urb->dev->bus->hcpriv;
1789 uhci->rh.interval = urb->interval;
1790 init_timer (&uhci->rh.rh_int_timer);
1791 uhci->rh.rh_int_timer.function = rh_int_timer_do;
1792 uhci->rh.rh_int_timer.data = (unsigned long) urb;
1793 uhci->rh.rh_int_timer.expires = jiffies + (HZ * 20) / 1000;
1794 add_timer (&uhci->rh.rh_int_timer);
1796 return 0;
1799 /*-------------------------------------------------------------------------*/
1800 #define OK(x) len = (x); break
1802 #define CLR_RH_PORTSTAT(x) \
1803 status = inw(io_addr+USBPORTSC1+2*(wIndex-1)); \
1804 status = (status & 0xfff5) & ~(x); \
1805 outw(status, io_addr+USBPORTSC1+2*(wIndex-1))
1807 #define SET_RH_PORTSTAT(x) \
1808 status = inw(io_addr+USBPORTSC1+2*(wIndex-1)); \
1809 status = (status & 0xfff5) | (x); \
1810 outw(status, io_addr+USBPORTSC1+2*(wIndex-1))
1813 /*-------------------------------------------------------------------------*/
1814 /****
1815 ** Root Hub Control Pipe
1816 *************************/
1819 _static int rh_submit_urb (urb_t *urb)
1821 struct usb_device *usb_dev = urb->dev;
1822 uhci_t *uhci = usb_dev->bus->hcpriv;
1823 unsigned int pipe = urb->pipe;
1824 devrequest *cmd = (devrequest *) urb->setup_packet;
1825 void *data = urb->transfer_buffer;
1826 int leni = urb->transfer_buffer_length;
1827 int len = 0;
1828 int status = 0;
1829 int stat = 0;
1830 int i;
1831 unsigned int io_addr = uhci->io_addr;
1832 __u16 cstatus;
1834 __u16 bmRType_bReq;
1835 __u16 wValue;
1836 __u16 wIndex;
1837 __u16 wLength;
1839 if (usb_pipetype (pipe) == PIPE_INTERRUPT) {
1840 dbg("Root-Hub submit IRQ: every %d ms", urb->interval);
1841 uhci->rh.urb = urb;
1842 uhci->rh.send = 1;
1843 uhci->rh.interval = urb->interval;
1844 rh_init_int_timer (urb);
1846 return 0;
1850 bmRType_bReq = cmd->requesttype | cmd->request << 8;
1851 wValue = le16_to_cpu (cmd->value);
1852 wIndex = le16_to_cpu (cmd->index);
1853 wLength = le16_to_cpu (cmd->length);
1855 for (i = 0; i < 8; i++)
1856 uhci->rh.c_p_r[i] = 0;
1858 dbg("Root-Hub: adr: %2x cmd(%1x): %04x %04x %04x %04x",
1859 uhci->rh.devnum, 8, bmRType_bReq, wValue, wIndex, wLength);
1861 switch (bmRType_bReq) {
1862 /* Request Destination:
1863 without flags: Device,
1864 RH_INTERFACE: interface,
1865 RH_ENDPOINT: endpoint,
1866 RH_CLASS means HUB here,
1867 RH_OTHER | RH_CLASS almost ever means HUB_PORT here
1870 case RH_GET_STATUS:
1871 *(__u16 *) data = cpu_to_le16 (1);
1872 OK (2);
1873 case RH_GET_STATUS | RH_INTERFACE:
1874 *(__u16 *) data = cpu_to_le16 (0);
1875 OK (2);
1876 case RH_GET_STATUS | RH_ENDPOINT:
1877 *(__u16 *) data = cpu_to_le16 (0);
1878 OK (2);
1879 case RH_GET_STATUS | RH_CLASS:
1880 *(__u32 *) data = cpu_to_le32 (0);
1881 OK (4); /* hub power ** */
1882 case RH_GET_STATUS | RH_OTHER | RH_CLASS:
1883 status = inw (io_addr + USBPORTSC1 + 2 * (wIndex - 1));
1884 cstatus = ((status & USBPORTSC_CSC) >> (1 - 0)) |
1885 ((status & USBPORTSC_PEC) >> (3 - 1)) |
1886 (uhci->rh.c_p_r[wIndex - 1] << (0 + 4));
1887 status = (status & USBPORTSC_CCS) |
1888 ((status & USBPORTSC_PE) >> (2 - 1)) |
1889 ((status & USBPORTSC_SUSP) >> (12 - 2)) |
1890 ((status & USBPORTSC_PR) >> (9 - 4)) |
1891 (1 << 8) | /* power on ** */
1892 ((status & USBPORTSC_LSDA) << (-8 + 9));
1894 *(__u16 *) data = cpu_to_le16 (status);
1895 *(__u16 *) (data + 2) = cpu_to_le16 (cstatus);
1896 OK (4);
1898 case RH_CLEAR_FEATURE | RH_ENDPOINT:
1899 switch (wValue) {
1900 case (RH_ENDPOINT_STALL):
1901 OK (0);
1903 break;
1905 case RH_CLEAR_FEATURE | RH_CLASS:
1906 switch (wValue) {
1907 case (RH_C_HUB_OVER_CURRENT):
1908 OK (0); /* hub power over current ** */
1910 break;
1912 case RH_CLEAR_FEATURE | RH_OTHER | RH_CLASS:
1913 switch (wValue) {
1914 case (RH_PORT_ENABLE):
1915 CLR_RH_PORTSTAT (USBPORTSC_PE);
1916 OK (0);
1917 case (RH_PORT_SUSPEND):
1918 CLR_RH_PORTSTAT (USBPORTSC_SUSP);
1919 OK (0);
1920 case (RH_PORT_POWER):
1921 OK (0); /* port power ** */
1922 case (RH_C_PORT_CONNECTION):
1923 SET_RH_PORTSTAT (USBPORTSC_CSC);
1924 OK (0);
1925 case (RH_C_PORT_ENABLE):
1926 SET_RH_PORTSTAT (USBPORTSC_PEC);
1927 OK (0);
1928 case (RH_C_PORT_SUSPEND):
1929 /*** WR_RH_PORTSTAT(RH_PS_PSSC); */
1930 OK (0);
1931 case (RH_C_PORT_OVER_CURRENT):
1932 OK (0); /* port power over current ** */
1933 case (RH_C_PORT_RESET):
1934 uhci->rh.c_p_r[wIndex - 1] = 0;
1935 OK (0);
1937 break;
1939 case RH_SET_FEATURE | RH_OTHER | RH_CLASS:
1940 switch (wValue) {
1941 case (RH_PORT_SUSPEND):
1942 SET_RH_PORTSTAT (USBPORTSC_SUSP);
1943 OK (0);
1944 case (RH_PORT_RESET):
1945 SET_RH_PORTSTAT (USBPORTSC_PR);
1946 uhci_wait_ms (10);
1947 uhci->rh.c_p_r[wIndex - 1] = 1;
1948 CLR_RH_PORTSTAT (USBPORTSC_PR);
1949 udelay (10);
1950 SET_RH_PORTSTAT (USBPORTSC_PE);
1951 uhci_wait_ms (10);
1952 SET_RH_PORTSTAT (0xa);
1953 OK (0);
1954 case (RH_PORT_POWER):
1955 OK (0); /* port power ** */
1956 case (RH_PORT_ENABLE):
1957 SET_RH_PORTSTAT (USBPORTSC_PE);
1958 OK (0);
1960 break;
1962 case RH_SET_ADDRESS:
1963 uhci->rh.devnum = wValue;
1964 OK (0);
1966 case RH_GET_DESCRIPTOR:
1967 switch ((wValue & 0xff00) >> 8) {
1968 case (0x01): /* device descriptor */
1969 len = min (leni, min (sizeof (root_hub_dev_des), wLength));
1970 memcpy (data, root_hub_dev_des, len);
1971 OK (len);
1972 case (0x02): /* configuration descriptor */
1973 len = min (leni, min (sizeof (root_hub_config_des), wLength));
1974 memcpy (data, root_hub_config_des, len);
1975 OK (len);
1976 case (0x03): /* string descriptors */
1977 len = usb_root_hub_string (wValue & 0xff,
1978 uhci->io_addr, "UHCI",
1979 data, wLength);
1980 if (len > 0) {
1981 OK (min (leni, len));
1982 } else
1983 stat = -EPIPE;
1985 break;
1987 case RH_GET_DESCRIPTOR | RH_CLASS:
1988 root_hub_hub_des[2] = uhci->rh.numports;
1989 len = min (leni, min (sizeof (root_hub_hub_des), wLength));
1990 memcpy (data, root_hub_hub_des, len);
1991 OK (len);
1993 case RH_GET_CONFIGURATION:
1994 *(__u8 *) data = 0x01;
1995 OK (1);
1997 case RH_SET_CONFIGURATION:
1998 OK (0);
1999 default:
2000 stat = -EPIPE;
2003 dbg("Root-Hub stat port1: %x port2: %x",
2004 inw (io_addr + USBPORTSC1), inw (io_addr + USBPORTSC2));
2006 urb->actual_length = len;
2007 urb->status = stat;
2008 if (urb->complete)
2009 urb->complete (urb);
2010 return 0;
2012 /*-------------------------------------------------------------------------*/
2014 _static int rh_unlink_urb (urb_t *urb)
2016 uhci_t *uhci = urb->dev->bus->hcpriv;
2018 if (uhci->rh.urb==urb) {
2019 dbg("Root-Hub unlink IRQ");
2020 uhci->rh.send = 0;
2021 del_timer (&uhci->rh.rh_int_timer);
2023 return 0;
2025 /*-------------------------------------------------------------------*/
2028 * Map status to standard result codes
2030 * <status> is (td->status & 0xFE0000) [a.k.a. uhci_status_bits(td->status)
2031 * <dir_out> is True for output TDs and False for input TDs.
2033 _static int uhci_map_status (int status, int dir_out)
2035 if (!status)
2036 return 0;
2037 if (status & TD_CTRL_BITSTUFF) /* Bitstuff error */
2038 return -EPROTO;
2039 if (status & TD_CTRL_CRCTIMEO) { /* CRC/Timeout */
2040 if (dir_out)
2041 return -ETIMEDOUT;
2042 else
2043 return -EILSEQ;
2045 if (status & TD_CTRL_NAK) /* NAK */
2046 return -ETIMEDOUT;
2047 if (status & TD_CTRL_BABBLE) /* Babble */
2048 return -EPIPE;
2049 if (status & TD_CTRL_DBUFERR) /* Buffer error */
2050 return -ENOSR;
2051 if (status & TD_CTRL_STALLED) /* Stalled */
2052 return -EPIPE;
2053 if (status & TD_CTRL_ACTIVE) /* Active */
2054 return 0;
2056 return -EPROTO;
2060 * Only the USB core should call uhci_alloc_dev and uhci_free_dev
2062 _static int uhci_alloc_dev (struct usb_device *usb_dev)
2064 return 0;
2067 _static void uhci_unlink_urbs(uhci_t *s, struct usb_device *usb_dev, int remove_all)
2069 unsigned long flags;
2070 struct list_head *p;
2071 struct list_head *p2;
2072 urb_t *urb;
2074 spin_lock_irqsave (&s->urb_list_lock, flags);
2075 p = s->urb_list.prev;
2076 while (p != &s->urb_list) {
2077 p2 = p;
2078 p = p->prev ;
2079 urb = list_entry (p2, urb_t, urb_list);
2080 dbg("urb: %p, dev %p, %p", urb, usb_dev,urb->dev);
2082 //urb->transfer_flags |=USB_ASYNC_UNLINK;
2084 if (remove_all || (usb_dev == urb->dev)) {
2085 spin_unlock_irqrestore (&s->urb_list_lock, flags);
2086 warn("forced removing of queued URB %p due to disconnect",urb);
2087 uhci_unlink_urb(urb);
2088 urb->dev = NULL; // avoid further processing of this UR
2089 spin_lock_irqsave (&s->urb_list_lock, flags);
2090 p = s->urb_list.prev;
2093 spin_unlock_irqrestore (&s->urb_list_lock, flags);
2096 _static int uhci_free_dev (struct usb_device *usb_dev)
2098 uhci_t *s;
2101 if(!usb_dev || !usb_dev->bus || !usb_dev->bus->hcpriv)
2102 return -EINVAL;
2104 s=(uhci_t*) usb_dev->bus->hcpriv;
2105 uhci_unlink_urbs(s, usb_dev, 0);
2107 return 0;
2111 * uhci_get_current_frame_number()
2113 * returns the current frame number for a USB bus/controller.
2115 _static int uhci_get_current_frame_number (struct usb_device *usb_dev)
2117 return UHCI_GET_CURRENT_FRAME ((uhci_t*) usb_dev->bus->hcpriv);
2120 struct usb_operations uhci_device_operations =
2122 uhci_alloc_dev,
2123 uhci_free_dev,
2124 uhci_get_current_frame_number,
2125 uhci_submit_urb,
2126 uhci_unlink_urb
2130 * For IN-control transfers, process_transfer gets a bit more complicated,
2131 * since there are devices that return less data (eg. strings) than they
2132 * have announced. This leads to a queue abort due to the short packet,
2133 * the status stage is not executed. If this happens, the status stage
2134 * is manually re-executed.
2135 * mode: 0: QHs already unlinked
2138 _static int process_transfer (uhci_t *s, urb_t *urb, int mode)
2140 int ret = 0;
2141 urb_priv_t *urb_priv = urb->hcpriv;
2142 struct list_head *qhl = urb_priv->desc_list.next;
2143 uhci_desc_t *qh = list_entry (qhl, uhci_desc_t, desc_list);
2144 struct list_head *p = qh->vertical.next;
2145 uhci_desc_t *desc= list_entry (urb_priv->desc_list.prev, uhci_desc_t, desc_list);
2146 uhci_desc_t *last_desc = list_entry (desc->vertical.prev, uhci_desc_t, vertical);
2147 int data_toggle = usb_gettoggle (urb->dev, usb_pipeendpoint (urb->pipe), usb_pipeout (urb->pipe)); // save initial data_toggle
2148 int maxlength; // extracted and remapped info from TD
2149 int actual_length;
2150 int status = 0;
2152 //dbg("process_transfer: urb contains bulk/control request");
2154 /* if the status phase has been retriggered and the
2155 queue is empty or the last status-TD is inactive, the retriggered
2156 status stage is completed
2159 if (urb_priv->flags &&
2160 ((qh->hw.qh.element == UHCI_PTR_TERM) ||(!(last_desc->hw.td.status & TD_CTRL_ACTIVE))))
2161 goto transfer_finished;
2163 urb->actual_length=0;
2165 for (; p != &qh->vertical; p = p->next) {
2166 desc = list_entry (p, uhci_desc_t, vertical);
2168 if (desc->hw.td.status & TD_CTRL_ACTIVE) // do not process active TDs
2169 return ret;
2171 actual_length = (desc->hw.td.status + 1) & 0x7ff; // extract transfer parameters from TD
2172 maxlength = (((desc->hw.td.info >> 21) & 0x7ff) + 1) & 0x7ff;
2173 status = uhci_map_status (uhci_status_bits (desc->hw.td.status), usb_pipeout (urb->pipe));
2175 if (status == -EPIPE) { // see if EP is stalled
2176 // set up stalled condition
2177 usb_endpoint_halt (urb->dev, usb_pipeendpoint (urb->pipe), usb_pipeout (urb->pipe));
2180 if (status != 0) { // if any error occured stop processing of further TDs
2181 // only set ret if status returned an error
2182 if (status != -EPIPE)
2183 uhci_show_td (desc);
2184 ret = status;
2185 urb->error_count++;
2186 break;
2188 else if ((desc->hw.td.info & 0xff) != USB_PID_SETUP)
2189 urb->actual_length += actual_length;
2191 // got less data than requested
2192 if ( (actual_length < maxlength)) {
2193 if (urb->transfer_flags & USB_DISABLE_SPD) {
2194 status = -EREMOTEIO; // treat as real error
2195 dbg("process_transfer: SPD!!");
2196 break; // exit after this TD because SP was detected
2199 // short read during control-IN: re-start status stage
2200 if ((usb_pipetype (urb->pipe) == PIPE_CONTROL)) {
2201 if (uhci_packetid(last_desc->hw.td.info) == USB_PID_OUT) {
2203 qh->hw.qh.element = virt_to_bus (last_desc); // re-trigger status stage
2204 dbg("short packet during control transfer, retrigger status stage @ %p",last_desc);
2205 //uhci_show_td (desc);
2206 //uhci_show_td (last_desc);
2207 urb_priv->flags = 1; // mark as short control packet
2208 return 0;
2211 // all other cases: short read is OK
2212 data_toggle = uhci_toggle (desc->hw.td.info);
2213 break;
2216 data_toggle = uhci_toggle (desc->hw.td.info);
2217 queue_dbg("process_transfer: len:%d status:%x mapped:%x toggle:%d", actual_length, desc->hw.td.status,status, data_toggle);
2221 usb_settoggle (urb->dev, usb_pipeendpoint (urb->pipe), usb_pipeout (urb->pipe), !data_toggle);
2223 transfer_finished:
2225 uhci_clean_transfer(s, urb, qh, (mode==0?2:1));
2227 urb->status = status;
2229 #ifdef CONFIG_USB_UHCI_HIGH_BANDWIDTH
2230 disable_desc_loop(s,urb);
2231 #endif
2233 queue_dbg("process_transfer: (end) urb %p, wanted len %d, len %d status %x err %d",
2234 urb,urb->transfer_buffer_length,urb->actual_length, urb->status, urb->error_count);
2235 return ret;
2238 _static int process_interrupt (uhci_t *s, urb_t *urb)
2240 int i, ret = -EINPROGRESS;
2241 urb_priv_t *urb_priv = urb->hcpriv;
2242 struct list_head *p = urb_priv->desc_list.next;
2243 uhci_desc_t *desc = list_entry (urb_priv->desc_list.prev, uhci_desc_t, desc_list);
2245 int actual_length;
2246 int status = 0;
2248 //dbg("urb contains interrupt request");
2250 for (i = 0; p != &urb_priv->desc_list; p = p->next, i++) // Maybe we allow more than one TD later ;-)
2252 desc = list_entry (p, uhci_desc_t, desc_list);
2254 if (desc->hw.td.status & TD_CTRL_ACTIVE) {
2255 // do not process active TDs
2256 //dbg("TD ACT Status @%p %08x",desc,desc->hw.td.status);
2257 break;
2260 if (!desc->hw.td.status & TD_CTRL_IOC) {
2261 // do not process one-shot TDs, no recycling
2262 break;
2264 // extract transfer parameters from TD
2266 actual_length = (desc->hw.td.status + 1) & 0x7ff;
2267 status = uhci_map_status (uhci_status_bits (desc->hw.td.status), usb_pipeout (urb->pipe));
2269 // see if EP is stalled
2270 if (status == -EPIPE) {
2271 // set up stalled condition
2272 usb_endpoint_halt (urb->dev, usb_pipeendpoint (urb->pipe), usb_pipeout (urb->pipe));
2275 // if any error occured: ignore this td, and continue
2276 if (status != 0) {
2277 //uhci_show_td (desc);
2278 urb->error_count++;
2279 goto recycle;
2281 else
2282 urb->actual_length = actual_length;
2284 recycle:
2285 if (urb->complete) {
2286 //dbg("process_interrupt: calling completion, status %i",status);
2287 urb->status = status;
2288 ((urb_priv_t*)urb->hcpriv)->flags=1; // if unlink_urb is called during completion
2290 spin_unlock(&s->urb_list_lock);
2292 urb->complete ((struct urb *) urb);
2294 spin_lock(&s->urb_list_lock);
2296 ((urb_priv_t*)urb->hcpriv)->flags=0;
2299 if ((urb->status != -ECONNABORTED) && (urb->status != ECONNRESET) &&
2300 (urb->status != -ENOENT)) {
2302 urb->status = -EINPROGRESS;
2304 // Recycle INT-TD if interval!=0, else mark TD as one-shot
2305 if (urb->interval) {
2307 desc->hw.td.info &= ~(1 << TD_TOKEN_TOGGLE);
2308 if (status==0) {
2309 ((urb_priv_t*)urb->hcpriv)->started=jiffies;
2310 desc->hw.td.info |= (usb_gettoggle (urb->dev, usb_pipeendpoint (urb->pipe),
2311 usb_pipeout (urb->pipe)) << TD_TOKEN_TOGGLE);
2312 usb_dotoggle (urb->dev, usb_pipeendpoint (urb->pipe), usb_pipeout (urb->pipe));
2313 } else {
2314 desc->hw.td.info |= (!usb_gettoggle (urb->dev, usb_pipeendpoint (urb->pipe),
2315 usb_pipeout (urb->pipe)) << TD_TOKEN_TOGGLE);
2317 desc->hw.td.status= (urb->pipe & TD_CTRL_LS) | TD_CTRL_ACTIVE | TD_CTRL_IOC |
2318 (urb->transfer_flags & USB_DISABLE_SPD ? 0 : TD_CTRL_SPD) | (3 << 27);
2319 mb();
2321 else {
2322 uhci_unlink_urb_async(s, urb);
2323 desc->hw.td.status &= ~TD_CTRL_IOC; // inactivate TD
2328 return ret;
2331 // mode: 1: force processing, don't unlink tds (already unlinked)
2332 _static int process_iso (uhci_t *s, urb_t *urb, int mode)
2334 int i;
2335 int ret = 0;
2336 urb_priv_t *urb_priv = urb->hcpriv;
2337 struct list_head *p = urb_priv->desc_list.next;
2338 uhci_desc_t *desc = list_entry (urb_priv->desc_list.prev, uhci_desc_t, desc_list);
2340 dbg("urb contains iso request");
2341 if ((desc->hw.td.status & TD_CTRL_ACTIVE) && !mode)
2342 return -EXDEV; // last TD not finished
2344 urb->error_count = 0;
2345 urb->actual_length = 0;
2346 urb->status = 0;
2347 dbg("process iso urb %p, %li, %i, %i, %i %08x",urb,jiffies,UHCI_GET_CURRENT_FRAME(s),
2348 urb->number_of_packets,mode,desc->hw.td.status);
2350 for (i = 0; p != &urb_priv->desc_list; i++) {
2351 desc = list_entry (p, uhci_desc_t, desc_list);
2353 //uhci_show_td(desc);
2354 if (desc->hw.td.status & TD_CTRL_ACTIVE) {
2355 // means we have completed the last TD, but not the TDs before
2356 desc->hw.td.status &= ~TD_CTRL_ACTIVE;
2357 dbg("TD still active (%x)- grrr. paranoia!", desc->hw.td.status);
2358 ret = -EXDEV;
2359 urb->iso_frame_desc[i].status = ret;
2360 unlink_td (s, desc, 1);
2361 // FIXME: immediate deletion may be dangerous
2362 goto err;
2365 if (!mode)
2366 unlink_td (s, desc, 1);
2368 if (urb->number_of_packets <= i) {
2369 dbg("urb->number_of_packets (%d)<=(%d)", urb->number_of_packets, i);
2370 ret = -EINVAL;
2371 goto err;
2374 if (urb->iso_frame_desc[i].offset + urb->transfer_buffer != bus_to_virt (desc->hw.td.buffer)) {
2375 // Hm, something really weird is going on
2376 dbg("Pointer Paranoia: %p!=%p", urb->iso_frame_desc[i].offset + urb->transfer_buffer, bus_to_virt (desc->hw.td.buffer));
2377 ret = -EINVAL;
2378 urb->iso_frame_desc[i].status = ret;
2379 goto err;
2381 urb->iso_frame_desc[i].actual_length = (desc->hw.td.status + 1) & 0x7ff;
2382 urb->iso_frame_desc[i].status = uhci_map_status (uhci_status_bits (desc->hw.td.status), usb_pipeout (urb->pipe));
2383 urb->actual_length += urb->iso_frame_desc[i].actual_length;
2385 err:
2387 if (urb->iso_frame_desc[i].status != 0) {
2388 urb->error_count++;
2389 urb->status = urb->iso_frame_desc[i].status;
2391 dbg("process_iso: %i: len:%d %08x status:%x",
2392 i, urb->iso_frame_desc[i].actual_length, desc->hw.td.status,urb->iso_frame_desc[i].status);
2394 list_del (p);
2395 p = p->next;
2396 delete_desc (desc);
2399 dbg("process_iso: exit %i (%d), actual_len %i", i, ret,urb->actual_length);
2400 return ret;
2404 _static int process_urb (uhci_t *s, struct list_head *p)
2406 int ret = 0;
2407 urb_t *urb;
2410 urb=list_entry (p, urb_t, urb_list);
2411 //dbg("process_urb: found queued urb: %p", urb);
2413 switch (usb_pipetype (urb->pipe)) {
2414 case PIPE_CONTROL:
2415 ret = process_transfer (s, urb, 1);
2416 break;
2417 case PIPE_BULK:
2418 if (!s->avoid_bulk.counter)
2419 ret = process_transfer (s, urb, 1);
2420 else
2421 return 0;
2422 break;
2423 case PIPE_ISOCHRONOUS:
2424 ret = process_iso (s, urb, 0);
2425 break;
2426 case PIPE_INTERRUPT:
2427 ret = process_interrupt (s, urb);
2428 break;
2431 if (urb->status != -EINPROGRESS) {
2432 int proceed = 0;
2434 dbg("dequeued urb: %p", urb);
2435 dequeue_urb (s, urb);
2437 #ifdef DEBUG_SLAB
2438 kmem_cache_free(urb_priv_kmem, urb->hcpriv);
2439 #else
2440 kfree (urb->hcpriv);
2441 #endif
2443 if ((usb_pipetype (urb->pipe) != PIPE_INTERRUPT)) {
2444 urb_t *tmp = urb->next; // pointer to first urb
2445 int is_ring = 0;
2447 if (urb->next) {
2448 do {
2449 if (tmp->status != -EINPROGRESS) {
2450 proceed = 1;
2451 break;
2453 tmp = tmp->next;
2455 while (tmp != NULL && tmp != urb->next);
2456 if (tmp == urb->next)
2457 is_ring = 1;
2460 spin_unlock(&s->urb_list_lock);
2462 // In case you need the current URB status for your completion handler
2463 if (urb->complete && (!proceed || (urb->transfer_flags & USB_URB_EARLY_COMPLETE))) {
2464 dbg("process_transfer: calling early completion");
2465 urb->complete ((struct urb *) urb);
2466 if (!proceed && is_ring && (urb->status != -ENOENT))
2467 uhci_submit_urb (urb);
2470 if (proceed && urb->next) {
2471 // if there are linked urbs - handle submitting of them right now.
2472 tmp = urb->next; // pointer to first urb
2474 do {
2475 if ((tmp->status != -EINPROGRESS) && (tmp->status != -ENOENT) && uhci_submit_urb (tmp) != 0)
2476 break;
2477 tmp = tmp->next;
2479 while (tmp != NULL && tmp != urb->next); // submit until we reach NULL or our own pointer or submit fails
2481 if (urb->complete && !(urb->transfer_flags & USB_URB_EARLY_COMPLETE)) {
2482 dbg("process_transfer: calling completion");
2483 urb->complete ((struct urb *) urb);
2487 spin_lock(&s->urb_list_lock);
2489 usb_dec_dev_use (urb->dev);
2493 return ret;
2496 _static void uhci_interrupt (int irq, void *__uhci, struct pt_regs *regs)
2498 uhci_t *s = __uhci;
2499 unsigned int io_addr = s->io_addr;
2500 unsigned short status;
2501 struct list_head *p, *p2;
2504 * Read the interrupt status, and write it back to clear the
2505 * interrupt cause
2508 status = inw (io_addr + USBSTS);
2510 if (!status) /* shared interrupt, not mine */
2511 return;
2513 dbg("interrupt");
2515 if (status != 1) {
2516 warn("interrupt, status %x, frame# %i", status,
2517 UHCI_GET_CURRENT_FRAME(s));
2519 // remove host controller halted state
2520 if ((status&0x20) && (s->running)) {
2521 outw (USBCMD_RS | inw(io_addr + USBCMD), io_addr + USBCMD);
2523 //uhci_show_status (s);
2526 * traverse the list in *reverse* direction, because new entries
2527 * may be added at the end.
2528 * also, because process_urb may unlink the current urb,
2529 * we need to advance the list before
2532 spin_lock (&s->urb_list_lock);
2533 restart:
2534 s->unlink_urb_done=0;
2535 p = s->urb_list.prev;
2537 while (p != &s->urb_list) {
2538 p2 = p;
2539 p = p->prev;
2540 process_urb (s, p2);
2541 if (s->unlink_urb_done) {
2542 s->unlink_urb_done=0;
2543 goto restart;
2546 if ((jiffies - s->timeout_check) > (HZ/30))
2547 uhci_check_timeouts(s);
2549 clean_descs(s,0);
2550 uhci_cleanup_unlink(s, 0);
2551 uhci_switch_timer_int(s);
2553 spin_unlock (&s->urb_list_lock);
2555 outw (status, io_addr + USBSTS);
2557 //dbg("uhci_interrupt: done");
2560 _static void reset_hc (uhci_t *s)
2562 unsigned int io_addr = s->io_addr;
2564 s->apm_state = 0;
2565 /* Global reset for 50ms */
2566 outw (USBCMD_GRESET, io_addr + USBCMD);
2567 uhci_wait_ms (50);
2568 outw (0, io_addr + USBCMD);
2569 uhci_wait_ms (10);
2572 _static void start_hc (uhci_t *s)
2574 unsigned int io_addr = s->io_addr;
2575 int timeout = 1000;
2578 * Reset the HC - this will force us to get a
2579 * new notification of any already connected
2580 * ports due to the virtual disconnect that it
2581 * implies.
2583 outw (USBCMD_HCRESET, io_addr + USBCMD);
2585 while (inw (io_addr + USBCMD) & USBCMD_HCRESET) {
2586 if (!--timeout) {
2587 err("USBCMD_HCRESET timed out!");
2588 break;
2592 /* Turn on all interrupts */
2593 outw (USBINTR_TIMEOUT | USBINTR_RESUME | USBINTR_IOC | USBINTR_SP, io_addr + USBINTR);
2595 /* Start at frame 0 */
2596 outw (0, io_addr + USBFRNUM);
2597 outl (virt_to_bus (s->framelist), io_addr + USBFLBASEADD);
2599 /* Run and mark it configured with a 64-byte max packet */
2600 outw (USBCMD_RS | USBCMD_CF | USBCMD_MAXP, io_addr + USBCMD);
2601 s->apm_state = 1;
2602 s->running = 1;
2605 _static void uhci_cleanup_dev(uhci_t *s)
2607 struct usb_device *root_hub = s->bus->root_hub;
2609 s->running = 0; // Don't allow submit_urb
2611 if (root_hub)
2612 usb_disconnect (&root_hub);
2614 reset_hc (s);
2615 wait_ms (1);
2617 uhci_unlink_urbs (s, 0, 1); // Forced unlink of remaining URBs
2618 uhci_cleanup_unlink (s, 1); // force cleanup of async killed URBs
2620 usb_deregister_bus (s->bus);
2622 release_region (s->io_addr, s->io_size);
2623 free_irq (s->irq, s);
2624 usb_free_bus (s->bus);
2625 cleanup_skel (s);
2626 kfree (s);
2629 _static int __init uhci_start_usb (uhci_t *s)
2630 { /* start it up */
2631 /* connect the virtual root hub */
2632 struct usb_device *usb_dev;
2634 usb_dev = usb_alloc_dev (NULL, s->bus);
2635 if (!usb_dev)
2636 return -1;
2638 s->bus->root_hub = usb_dev;
2639 usb_connect (usb_dev);
2641 if (usb_new_device (usb_dev) != 0) {
2642 usb_free_dev (usb_dev);
2643 return -1;
2646 return 0;
2649 _static int handle_pm_event (struct pm_dev *dev, pm_request_t rqst, void *data)
2651 uhci_t *s = (uhci_t*) dev->data;
2652 dbg("handle_apm_event(%d)", rqst);
2653 if (s) {
2654 switch (rqst) {
2655 case PM_SUSPEND:
2656 reset_hc (s);
2657 break;
2658 case PM_RESUME:
2659 start_hc (s);
2660 break;
2663 return 0;
2666 _static int __init alloc_uhci (struct pci_dev *dev, int irq, unsigned int io_addr, unsigned int io_size)
2668 uhci_t *s;
2669 struct usb_bus *bus;
2670 struct pm_dev *pmdev;
2671 char buf[8], *bufp = buf;
2673 #ifndef __sparc__
2674 sprintf(buf, "%d", irq);
2675 #else
2676 bufp = __irq_itoa(irq);
2677 #endif
2678 printk(KERN_INFO __FILE__ ": USB UHCI at I/O 0x%x, IRQ %s\n",
2679 io_addr, bufp);
2681 s = kmalloc (sizeof (uhci_t), GFP_KERNEL);
2682 if (!s)
2683 return -1;
2685 memset (s, 0, sizeof (uhci_t));
2686 INIT_LIST_HEAD (&s->free_desc);
2687 INIT_LIST_HEAD (&s->urb_list);
2688 INIT_LIST_HEAD (&s->urb_unlinked);
2689 spin_lock_init (&s->urb_list_lock);
2690 spin_lock_init (&s->qh_lock);
2691 spin_lock_init (&s->td_lock);
2692 atomic_set(&s->avoid_bulk, 0);
2693 s->timeout_urbs = 0;
2694 s->irq = -1;
2695 s->io_addr = io_addr;
2696 s->io_size = io_size;
2697 s->next = devs; //chain new uhci device into global list
2698 s->timeout_check = 0;
2699 s->uhci_pci=dev;
2701 bus = usb_alloc_bus (&uhci_device_operations);
2702 if (!bus) {
2703 kfree (s);
2704 return -1;
2707 s->bus = bus;
2708 bus->hcpriv = s;
2710 /* UHCI specs says devices must have 2 ports, but goes on to say */
2711 /* they may have more but give no way to determine how many they */
2712 /* have, so default to 2 */
2713 /* According to the UHCI spec, Bit 7 is always set to 1. So we try */
2714 /* to use this to our advantage */
2716 for (s->maxports = 0; s->maxports < (io_size - 0x10) / 2; s->maxports++) {
2717 unsigned int portstatus;
2719 portstatus = inw (io_addr + 0x10 + (s->maxports * 2));
2720 dbg("port %i, adr %x status %x", s->maxports,
2721 io_addr + 0x10 + (s->maxports * 2), portstatus);
2722 if (!(portstatus & 0x0080))
2723 break;
2725 warn("Detected %d ports", s->maxports);
2727 /* This is experimental so anything less than 2 or greater than 8 is */
2728 /* something weird and we'll ignore it */
2729 if (s->maxports < 2 || s->maxports > 8) {
2730 dbg("Port count misdetected, forcing to 2 ports");
2731 s->maxports = 2;
2734 s->rh.numports = s->maxports;
2735 s->loop_usage=0;
2736 if (init_skel (s)) {
2737 usb_free_bus (bus);
2738 kfree(s);
2739 return -1;
2742 request_region (s->io_addr, io_size, MODNAME);
2743 reset_hc (s);
2744 usb_register_bus (s->bus);
2746 start_hc (s);
2748 if (request_irq (irq, uhci_interrupt, SA_SHIRQ, MODNAME, s)) {
2749 err("request_irq %d failed!",irq);
2750 usb_free_bus (bus);
2751 reset_hc (s);
2752 release_region (s->io_addr, s->io_size);
2753 cleanup_skel(s);
2754 kfree(s);
2755 return -1;
2758 s->irq = irq;
2760 if(uhci_start_usb (s) < 0) {
2761 uhci_cleanup_dev(s);
2762 return -1;
2765 //chain new uhci device into global list
2766 devs = s;
2768 pmdev = pm_register(PM_PCI_DEV, PM_PCI_ID(dev), handle_pm_event);
2769 if (pmdev)
2770 pmdev->data = s;
2772 return 0;
2775 _static int __init start_uhci (struct pci_dev *dev)
2777 int i;
2779 /* Search for the IO base address.. */
2780 for (i = 0; i < 6; i++) {
2782 unsigned int io_addr = dev->resource[i].start;
2783 unsigned int io_size =
2784 dev->resource[i].end - dev->resource[i].start + 1;
2785 if (!(dev->resource[i].flags & 1))
2786 continue;
2788 /* Is it already in use? */
2789 if (check_region (io_addr, io_size))
2790 break;
2791 /* disable legacy emulation */
2792 pci_write_config_word (dev, USBLEGSUP, USBLEGSUP_DEFAULT);
2793 if(dev->vendor==0x8086) {
2794 info("Intel USB controller: setting latency timer to %d", UHCI_LATENCY_TIMER);
2795 pci_write_config_byte(dev, PCI_LATENCY_TIMER, UHCI_LATENCY_TIMER);
2798 return alloc_uhci(dev, dev->irq, io_addr, io_size);
2800 return -1;
2803 int __init uhci_init (void)
2805 int retval = -ENODEV;
2806 struct pci_dev *dev = NULL;
2807 u8 type;
2808 int i=0;
2810 #ifdef DEBUG_SLAB
2812 uhci_desc_kmem = kmem_cache_create("uhci_desc", sizeof(uhci_desc_t), 0, SLAB_HWCACHE_ALIGN, NULL, NULL);
2814 if(!uhci_desc_kmem) {
2815 err("kmem_cache_create for uhci_desc failed (out of memory)");
2816 return -ENOMEM;
2819 urb_priv_kmem = kmem_cache_create("urb_priv", sizeof(urb_priv_t), 0, SLAB_HWCACHE_ALIGN, NULL, NULL);
2821 if(!urb_priv_kmem) {
2822 err("kmem_cache_create for urb_priv_t failed (out of memory)");
2823 return -ENOMEM;
2825 #endif
2826 info(VERSTR);
2828 #ifdef CONFIG_USB_UHCI_HIGH_BANDWIDTH
2829 info("High bandwidth mode enabled");
2830 #endif
2831 for (;;) {
2832 dev = pci_find_class (PCI_CLASS_SERIAL_USB << 8, dev);
2833 if (!dev)
2834 break;
2836 /* Is it UHCI */
2837 pci_read_config_byte (dev, PCI_CLASS_PROG, &type);
2838 if (type != 0)
2839 continue;
2841 if (pci_enable_device (dev) < 0)
2842 continue;
2844 if(!dev->irq)
2846 err("Found UHCI device with no IRQ assigned. Check BIOS settings!");
2847 continue;
2850 /* Ok set it up */
2851 retval = start_uhci (dev);
2853 if (!retval)
2854 i++;
2857 return retval;
2860 void __exit uhci_cleanup (void)
2862 uhci_t *s;
2863 while ((s = devs)) {
2864 devs = devs->next;
2865 uhci_cleanup_dev(s);
2867 #ifdef DEBUG_SLAB
2868 if(kmem_cache_destroy(uhci_desc_kmem))
2869 err("uhci_desc_kmem remained");
2871 if(kmem_cache_destroy(urb_priv_kmem))
2872 err("urb_priv_kmem remained");
2873 #endif
2876 #ifdef MODULE
2877 int init_module (void)
2879 return uhci_init ();
2882 void cleanup_module (void)
2884 pm_unregister_all (handle_pm_event);
2885 uhci_cleanup ();
2888 #endif //MODULE