Merge with Linux 2.4.0-test6-pre2.
[linux-2.6/linux-mips.git] / drivers / usb / usb-uhci.c
blobbd721cba20fe301ca7e9686265df82493a36b23d
1 /*
2 * Universal Host Controller Interface driver for USB (take II).
4 * (c) 1999-2000 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.236 2000/08/02 20:28:28 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.236 $ 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, last;
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 last = (len == 0 && (usb_pipein(pipe) || pktsze < maxsze || (urb->transfer_flags & USB_DISABLE_SPD)));
893 if (last)
894 td->hw.td.status |= TD_CTRL_IOC; // last one generates INT
896 insert_td (s, qh, td, UHCI_PTR_DEPTH * depth_first);
897 usb_dotoggle (urb->dev, usb_pipeendpoint (pipe), usb_pipeout (pipe));
899 } while (!last);
901 list_add (&qh->desc_list, &urb_priv->desc_list);
903 if (urb->transfer_flags & USB_QUEUE_BULK) {
904 qh->hw.qh.element&=~UHCI_PTR_TERM;
905 append_qh(s, td, bqh, UHCI_PTR_DEPTH * depth_first);
908 urb->status = -EINPROGRESS;
909 queue_urb_unlocked (s, urb);
911 qh->hw.qh.element &= ~UHCI_PTR_TERM;
913 if (!bulk_urb) {
914 if (urb->transfer_flags & USB_QUEUE_BULK) {
915 spin_lock (&s->td_lock); // both QHs in one go
916 insert_qh (s, s->chain_end, qh, 0); // Main QH
917 insert_qh (s, s->chain_end, nqh, 0); // Helper QH
918 spin_unlock (&s->td_lock);
920 else
921 insert_qh (s, s->chain_end, qh, 0);
924 //uhci_show_queue(s->bulk_chain);
925 //dbg("uhci_submit_bulk_urb: exit\n");
926 return 0;
928 /*-------------------------------------------------------------------*/
929 _static void uhci_clean_iso_step1(uhci_t *s, urb_priv_t *urb_priv)
931 struct list_head *p;
932 uhci_desc_t *td;
934 for (p = urb_priv->desc_list.next; p != &urb_priv->desc_list; p = p->next) {
935 td = list_entry (p, uhci_desc_t, desc_list);
936 unlink_td (s, td, 1);
939 /*-------------------------------------------------------------------*/
940 _static void uhci_clean_iso_step2(uhci_t *s, urb_priv_t *urb_priv)
942 struct list_head *p;
943 uhci_desc_t *td;
945 while ((p = urb_priv->desc_list.next) != &urb_priv->desc_list) {
946 td = list_entry (p, uhci_desc_t, desc_list);
947 list_del (p);
948 delete_desc (td);
951 /*-------------------------------------------------------------------*/
952 // mode: 0: unlink + no deletion mark, 1: regular (unlink/delete-mark), 2: don't unlink
953 // looks a bit complicated because of all the bulk queueing goodies
955 _static void uhci_clean_transfer (uhci_t *s, urb_t *urb, uhci_desc_t *qh, int mode)
957 uhci_desc_t *bqh, *nqh, *prevqh;
958 int now;
959 urb_priv_t *priv=(urb_priv_t*)urb->hcpriv;
961 now=UHCI_GET_CURRENT_FRAME(s);
963 dbg("clean transfer urb %p, qh %p, mode %i",urb,qh,mode);
964 bqh=priv->bottom_qh;
966 if (!priv->next_queued_urb) { // no more appended bulk queues
968 if (mode != 2)
969 unlink_qh (s, qh);
971 if (priv->prev_queued_urb) {
972 urb_priv_t* ppriv=(urb_priv_t*)priv->prev_queued_urb->hcpriv;
974 ppriv->bottom_qh = priv->bottom_qh;
975 ppriv->next_queued_urb = NULL;
977 else if (bqh) { // queue dead
978 nqh=priv->next_qh;
980 if (mode != 2)
981 unlink_qh(s, nqh);
983 if (mode) {
984 nqh->last_used = bqh->last_used = now;
985 list_add_tail (&nqh->horizontal, &s->free_desc);
986 list_add_tail (&bqh->horizontal, &s->free_desc);
990 else { // there are queued urbs following
991 urb_t *nurb;
992 unsigned long flags;
994 nurb=priv->next_queued_urb;
995 spin_lock_irqsave (&s->qh_lock, flags);
997 if (!priv->prev_queued_urb) { // top
998 if (mode !=2) {
999 prevqh = list_entry (qh->horizontal.prev, uhci_desc_t, horizontal);
1000 prevqh->hw.qh.head = virt_to_bus(bqh) | UHCI_PTR_QH;
1001 queue_dbg ("TOP relink of %p to %p-%p",qh,prevqh,bqh);
1003 list_del (&qh->horizontal);
1004 list_add (&bqh->horizontal, &prevqh->horizontal);
1007 else { //intermediate
1008 urb_priv_t* ppriv=(urb_priv_t*)priv->prev_queued_urb->hcpriv;
1009 uhci_desc_t * bnqh;
1011 bnqh=list_entry (&((urb_priv_t*)(nurb->hcpriv))->desc_list.next, uhci_desc_t, desc_list);
1012 ppriv->bottom_qh=bnqh;
1013 ppriv->next_queued_urb=nurb;
1015 if (mode!=2) {
1016 prevqh = list_entry (ppriv->desc_list.next, uhci_desc_t, desc_list);
1017 prevqh->hw.qh.head = virt_to_bus(bqh) | UHCI_PTR_QH;
1018 queue_dbg ("IM relink of %p to %p-%p",qh,prevqh,bqh);
1021 mb();
1022 spin_unlock_irqrestore (&s->qh_lock, flags);
1023 ((urb_priv_t*)nurb->hcpriv)->prev_queued_urb=priv->prev_queued_urb;
1026 if (mode) {
1027 qh->last_used = now;
1028 list_add_tail (&qh->horizontal, &s->free_desc); // mark for later deletion
1031 /*-------------------------------------------------------------------*/
1032 // unlinks an urb by dequeuing its qh, waits some frames and forgets it
1033 _static int uhci_unlink_urb_sync (uhci_t *s, urb_t *urb)
1035 uhci_desc_t *qh;
1036 urb_priv_t *urb_priv;
1037 unsigned long flags=0;
1039 spin_lock_irqsave (&s->urb_list_lock, flags);
1041 if (!in_interrupt()) // shouldn't be called from interrupt at all...
1042 spin_lock(&urb->lock);
1044 if (urb->status == -EINPROGRESS) {
1045 // URB probably still in work
1046 dequeue_urb (s, urb);
1047 uhci_switch_timer_int(s);
1048 s->unlink_urb_done=1;
1050 if (!in_interrupt())
1051 spin_unlock(&urb->lock);
1053 spin_unlock_irqrestore (&s->urb_list_lock, flags);
1055 urb->status = -ENOENT; // mark urb as killed
1056 urb_priv = urb->hcpriv;
1058 switch (usb_pipetype (urb->pipe)) {
1060 case PIPE_INTERRUPT:
1061 usb_dotoggle (urb->dev, usb_pipeendpoint (urb->pipe), usb_pipeout (urb->pipe));
1063 case PIPE_ISOCHRONOUS:
1064 uhci_clean_iso_step1(s, urb_priv);
1065 uhci_wait_ms(1);
1066 uhci_clean_iso_step2(s, urb_priv);
1067 break;
1069 case PIPE_BULK:
1070 case PIPE_CONTROL:
1071 qh = list_entry (urb_priv->desc_list.next, uhci_desc_t, desc_list);
1072 spin_lock_irqsave (&s->urb_list_lock, flags);
1073 uhci_clean_transfer(s, urb, qh, 1);
1074 spin_unlock_irqrestore (&s->urb_list_lock, flags);
1075 uhci_wait_ms(1);
1078 #ifdef DEBUG_SLAB
1079 kmem_cache_free (urb_priv_kmem, urb->hcpriv);
1080 #else
1081 kfree (urb->hcpriv);
1082 #endif
1083 if (urb->complete) {
1084 dbg("unlink_urb: calling completion");
1085 urb->complete ((struct urb *) urb);
1087 usb_dec_dev_use (urb->dev);
1089 else {
1090 if (!in_interrupt())
1091 spin_unlock(&urb->lock);
1092 spin_unlock_irqrestore (&s->urb_list_lock, flags);
1095 return 0;
1097 /*-------------------------------------------------------------------*/
1098 // async unlink_urb completion/cleanup work
1099 // has to be protected by urb_list_lock!
1100 // features: if set in transfer_flags, the resulting status of the killed
1101 // transaction is not overwritten
1103 _static void uhci_cleanup_unlink(uhci_t *s, int force)
1105 struct list_head *q;
1106 urb_t *urb;
1107 struct usb_device *dev;
1108 int pipe,now;
1109 urb_priv_t *urb_priv;
1111 q=s->urb_unlinked.next;
1112 now=UHCI_GET_CURRENT_FRAME(s);
1114 while (q != &s->urb_unlinked) {
1116 urb = list_entry (q, urb_t, urb_list);
1118 urb_priv = (urb_priv_t*)urb->hcpriv;
1119 q = urb->urb_list.next;
1121 if (force ||
1122 ((urb_priv->started != 0xffffffff) && (urb_priv->started != now))) {
1123 async_dbg("async cleanup %p",urb);
1124 switch (usb_pipetype (urb->pipe)) { // process descriptors
1125 case PIPE_CONTROL:
1126 process_transfer (s, urb, 2);
1127 break;
1128 case PIPE_BULK:
1129 if (!s->avoid_bulk.counter)
1130 process_transfer (s, urb, 2); // don't unlink (already done)
1131 else
1132 continue;
1133 break;
1134 case PIPE_ISOCHRONOUS:
1135 process_iso (s, urb, 1); // force, don't unlink
1136 break;
1137 case PIPE_INTERRUPT:
1138 process_interrupt (s, urb);
1139 break;
1142 if (!(urb->transfer_flags & USB_TIMEOUT_KILLED))
1143 urb->status = -ECONNRESET; // mark as asynchronously killed
1145 pipe = urb->pipe; // completion may destroy all...
1146 dev = urb->dev;
1147 urb_priv = urb->hcpriv;
1149 if (urb->complete) {
1150 spin_unlock(&s->urb_list_lock);
1151 urb->complete ((struct urb *) urb);
1152 spin_lock(&s->urb_list_lock);
1155 if (!(urb->transfer_flags & USB_TIMEOUT_KILLED))
1156 urb->status = -ENOENT; // now the urb is really dead
1157 switch (usb_pipetype (pipe)) {
1158 case PIPE_ISOCHRONOUS:
1159 case PIPE_INTERRUPT:
1160 uhci_clean_iso_step2(s, urb_priv);
1161 break;
1164 usb_dec_dev_use (dev);
1165 #ifdef DEBUG_SLAB
1166 kmem_cache_free (urb_priv_kmem, urb_priv);
1167 #else
1168 kfree (urb_priv);
1169 #endif
1171 list_del (&urb->urb_list);
1176 /*-------------------------------------------------------------------*/
1177 _static int uhci_unlink_urb_async (uhci_t *s,urb_t *urb)
1179 uhci_desc_t *qh;
1180 urb_priv_t *urb_priv;
1182 async_dbg("unlink_urb_async called %p",urb);
1184 if ((urb->status == -EINPROGRESS) ||
1185 ((usb_pipetype (urb->pipe) == PIPE_INTERRUPT) && ((urb_priv_t*)urb->hcpriv)->flags))
1187 ((urb_priv_t*)urb->hcpriv)->started = ~0;
1189 dequeue_urb (s, urb);
1190 list_add_tail (&urb->urb_list, &s->urb_unlinked); // store urb
1191 uhci_switch_timer_int(s);
1193 s->unlink_urb_done = 1;
1195 urb->status = -ECONNABORTED; // mark urb as "waiting to be killed"
1196 urb_priv = (urb_priv_t*)urb->hcpriv;
1198 switch (usb_pipetype (urb->pipe)) {
1199 case PIPE_INTERRUPT:
1200 usb_dotoggle (urb->dev, usb_pipeendpoint (urb->pipe), usb_pipeout (urb->pipe));
1202 case PIPE_ISOCHRONOUS:
1203 uhci_clean_iso_step1 (s, urb_priv);
1204 break;
1206 case PIPE_BULK:
1207 case PIPE_CONTROL:
1208 qh = list_entry (urb_priv->desc_list.next, uhci_desc_t, desc_list);
1209 uhci_clean_transfer (s, urb, qh, 0);
1210 break;
1212 ((urb_priv_t*)urb->hcpriv)->started = UHCI_GET_CURRENT_FRAME(s);
1213 return -EINPROGRESS; // completion will follow
1216 return 0; // URB already dead
1218 /*-------------------------------------------------------------------*/
1219 _static int uhci_unlink_urb (urb_t *urb)
1221 uhci_t *s;
1222 unsigned long flags=0;
1223 dbg("uhci_unlink_urb called for %p",urb);
1224 if (!urb || !urb->dev) // you never know...
1225 return -EINVAL;
1227 s = (uhci_t*) urb->dev->bus->hcpriv;
1229 if (usb_pipedevice (urb->pipe) == s->rh.devnum)
1230 return rh_unlink_urb (urb);
1232 if (!urb->hcpriv)
1233 return -EINVAL;
1235 if (urb->transfer_flags & USB_ASYNC_UNLINK) {
1236 int ret;
1238 spin_lock_irqsave (&s->urb_list_lock, flags);
1240 // The URB needs to be locked if called outside completion context
1242 if (!in_interrupt())
1243 spin_lock(&urb->lock);
1245 ret = uhci_unlink_urb_async(s, urb);
1247 if (!in_interrupt())
1248 spin_unlock(&urb->lock);
1250 spin_unlock_irqrestore (&s->urb_list_lock, flags);
1252 return ret;
1254 else
1255 return uhci_unlink_urb_sync(s, urb);
1257 /*-------------------------------------------------------------------*/
1258 // In case of ASAP iso transfer, search the URB-list for already queued URBs
1259 // for this EP and calculate the earliest start frame for the new
1260 // URB (easy seamless URB continuation!)
1261 _static int find_iso_limits (urb_t *urb, unsigned int *start, unsigned int *end)
1263 urb_t *u, *last_urb = NULL;
1264 uhci_t *s = (uhci_t*) urb->dev->bus->hcpriv;
1265 struct list_head *p;
1266 int ret=-1;
1267 unsigned long flags;
1269 spin_lock_irqsave (&s->urb_list_lock, flags);
1270 p=s->urb_list.prev;
1272 for (; p != &s->urb_list; p = p->prev) {
1273 u = list_entry (p, urb_t, urb_list);
1274 // look for pending URBs with identical pipe handle
1275 // works only because iso doesn't toggle the data bit!
1276 if ((urb->pipe == u->pipe) && (urb->dev == u->dev) && (u->status == -EINPROGRESS)) {
1277 if (!last_urb)
1278 *start = u->start_frame;
1279 last_urb = u;
1283 if (last_urb) {
1284 *end = (last_urb->start_frame + last_urb->number_of_packets) & 1023;
1285 ret=0;
1288 spin_unlock_irqrestore(&s->urb_list_lock, flags);
1290 return ret;
1292 /*-------------------------------------------------------------------*/
1293 // adjust start_frame according to scheduling constraints (ASAP etc)
1295 _static int iso_find_start (urb_t *urb)
1297 uhci_t *s = (uhci_t*) urb->dev->bus->hcpriv;
1298 unsigned int now;
1299 unsigned int start_limit = 0, stop_limit = 0, queued_size;
1300 int limits;
1302 now = UHCI_GET_CURRENT_FRAME (s) & 1023;
1304 if ((unsigned) urb->number_of_packets > 900)
1305 return -EFBIG;
1307 limits = find_iso_limits (urb, &start_limit, &stop_limit);
1308 queued_size = (stop_limit - start_limit) & 1023;
1310 if (urb->transfer_flags & USB_ISO_ASAP) {
1311 // first iso
1312 if (limits) {
1313 // 10ms setup should be enough //FIXME!
1314 urb->start_frame = (now + 10) & 1023;
1316 else {
1317 urb->start_frame = stop_limit; //seamless linkage
1319 if (((now - urb->start_frame) & 1023) <= (unsigned) urb->number_of_packets) {
1320 info("iso_find_start: gap in seamless isochronous scheduling");
1321 dbg("iso_find_start: now %u start_frame %u number_of_packets %u pipe 0x%08x",
1322 now, urb->start_frame, urb->number_of_packets, urb->pipe);
1323 urb->start_frame = (now + 5) & 1023; // 5ms setup should be enough //FIXME!
1327 else {
1328 urb->start_frame &= 1023;
1329 if (((now - urb->start_frame) & 1023) < (unsigned) urb->number_of_packets) {
1330 dbg("iso_find_start: now between start_frame and end");
1331 return -EAGAIN;
1335 /* check if either start_frame or start_frame+number_of_packets-1 lies between start_limit and stop_limit */
1336 if (limits)
1337 return 0;
1339 if (((urb->start_frame - start_limit) & 1023) < queued_size ||
1340 ((urb->start_frame + urb->number_of_packets - 1 - start_limit) & 1023) < queued_size) {
1341 dbg("iso_find_start: start_frame %u number_of_packets %u start_limit %u stop_limit %u",
1342 urb->start_frame, urb->number_of_packets, start_limit, stop_limit);
1343 return -EAGAIN;
1346 return 0;
1348 /*-------------------------------------------------------------------*/
1349 // submits USB interrupt (ie. polling ;-)
1350 // ASAP-flag set implicitely
1351 // if period==0, the the transfer is only done once
1353 _static int uhci_submit_int_urb (urb_t *urb)
1355 uhci_t *s = (uhci_t*) urb->dev->bus->hcpriv;
1356 urb_priv_t *urb_priv = urb->hcpriv;
1357 int nint, n, ret;
1358 uhci_desc_t *td;
1359 int status, destination;
1360 int info;
1361 unsigned int pipe = urb->pipe;
1363 if (urb->interval < 0 || urb->interval >= 256)
1364 return -EINVAL;
1366 if (urb->interval == 0)
1367 nint = 0;
1368 else {
1369 for (nint = 0, n = 1; nint <= 8; nint++, n += n) // round interval down to 2^n
1371 if (urb->interval < n) {
1372 urb->interval = n / 2;
1373 break;
1376 nint--;
1379 dbg("Rounded interval to %i, chain %i", urb->interval, nint);
1381 urb->start_frame = UHCI_GET_CURRENT_FRAME (s) & 1023; // remember start frame, just in case...
1383 urb->number_of_packets = 1;
1385 // INT allows only one packet
1386 if (urb->transfer_buffer_length > usb_maxpacket (urb->dev, pipe, usb_pipeout (pipe)))
1387 return -EINVAL;
1389 ret = alloc_td (&td, UHCI_PTR_DEPTH);
1391 if (ret)
1392 return -ENOMEM;
1394 status = (pipe & TD_CTRL_LS) | TD_CTRL_ACTIVE | TD_CTRL_IOC |
1395 (urb->transfer_flags & USB_DISABLE_SPD ? 0 : TD_CTRL_SPD) | (3 << 27);
1397 destination = (urb->pipe & PIPE_DEVEP_MASK) | usb_packetid (urb->pipe) |
1398 (((urb->transfer_buffer_length - 1) & 0x7ff) << 21);
1401 info = destination | (usb_gettoggle (urb->dev, usb_pipeendpoint (pipe), usb_pipeout (pipe)) << TD_TOKEN_TOGGLE);
1403 fill_td (td, status, info, virt_to_bus (urb->transfer_buffer));
1404 list_add_tail (&td->desc_list, &urb_priv->desc_list);
1406 urb->status = -EINPROGRESS;
1407 queue_urb (s, urb);
1409 insert_td_horizontal (s, s->int_chain[nint], td); // store in INT-TDs
1411 usb_dotoggle (urb->dev, usb_pipeendpoint (pipe), usb_pipeout (pipe));
1413 return 0;
1415 /*-------------------------------------------------------------------*/
1416 _static int uhci_submit_iso_urb (urb_t *urb)
1418 uhci_t *s = (uhci_t*) urb->dev->bus->hcpriv;
1419 urb_priv_t *urb_priv = urb->hcpriv;
1420 #ifdef ISO_SANITY_CHECK
1421 int pipe=urb->pipe;
1422 int maxsze = usb_maxpacket (urb->dev, pipe, usb_pipeout (pipe));
1423 #endif
1424 int n, ret, last=0;
1425 uhci_desc_t *td, **tdm;
1426 int status, destination;
1427 unsigned long flags;
1429 __save_flags(flags);
1430 __cli(); // Disable IRQs to schedule all ISO-TDs in time
1431 ret = iso_find_start (urb); // adjusts urb->start_frame for later use
1433 if (ret)
1434 goto err;
1436 tdm = (uhci_desc_t **) kmalloc (urb->number_of_packets * sizeof (uhci_desc_t*), KMALLOC_FLAG);
1438 if (!tdm) {
1439 ret = -ENOMEM;
1440 goto err;
1443 // First try to get all TDs
1444 for (n = 0; n < urb->number_of_packets; n++) {
1445 dbg("n:%d urb->iso_frame_desc[n].length:%d", n, urb->iso_frame_desc[n].length);
1446 if (!urb->iso_frame_desc[n].length) {
1447 // allows ISO striping by setting length to zero in iso_descriptor
1448 tdm[n] = 0;
1449 continue;
1452 #ifdef ISO_SANITY_CHECK
1453 if(urb->iso_frame_desc[n].length > maxsze) {
1455 err("submit_iso: urb->iso_frame_desc[%d].length(%d)>%d",n , urb->iso_frame_desc[n].length, maxsze);
1456 tdm[n] = 0;
1457 ret=-EINVAL;
1459 else
1460 #endif
1461 ret = alloc_td (&td, UHCI_PTR_DEPTH);
1463 if (ret) {
1464 int i; // Cleanup allocated TDs
1466 for (i = 0; i < n; n++)
1467 if (tdm[i])
1468 delete_desc(tdm[i]);
1469 kfree (tdm);
1470 goto err;
1472 last=n;
1473 tdm[n] = td;
1476 status = TD_CTRL_ACTIVE | TD_CTRL_IOS; //| (urb->transfer_flags&USB_DISABLE_SPD?0:TD_CTRL_SPD);
1478 destination = (urb->pipe & PIPE_DEVEP_MASK) | usb_packetid (urb->pipe);
1481 // Queue all allocated TDs
1482 for (n = 0; n < urb->number_of_packets; n++) {
1483 td = tdm[n];
1484 if (!td)
1485 continue;
1487 if (n == last)
1488 status |= TD_CTRL_IOC;
1490 fill_td (td, status, destination | (((urb->iso_frame_desc[n].length - 1) & 0x7ff) << 21),
1491 virt_to_bus (urb->transfer_buffer + urb->iso_frame_desc[n].offset));
1492 list_add_tail (&td->desc_list, &urb_priv->desc_list);
1494 if (n == last) {
1495 urb->status = -EINPROGRESS;
1496 queue_urb (s, urb);
1498 insert_td_horizontal (s, s->iso_td[(urb->start_frame + n) & 1023], td); // store in iso-tds
1499 //uhci_show_td(td);
1503 kfree (tdm);
1504 dbg("ISO-INT# %i, start %i, now %i", urb->number_of_packets, urb->start_frame, UHCI_GET_CURRENT_FRAME (s) & 1023);
1505 ret = 0;
1507 err:
1508 __restore_flags(flags);
1509 return ret;
1512 /*-------------------------------------------------------------------*/
1513 // returns: 0 (no transfer queued), urb* (this urb already queued)
1515 _static urb_t* search_dev_ep (uhci_t *s, urb_t *urb)
1517 struct list_head *p;
1518 urb_t *tmp;
1519 unsigned int mask = usb_pipecontrol(urb->pipe) ? (~USB_DIR_IN) : (~0);
1521 dbg("search_dev_ep:");
1523 p=s->urb_list.next;
1525 for (; p != &s->urb_list; p = p->next) {
1526 tmp = list_entry (p, urb_t, urb_list);
1527 dbg("urb: %p", tmp);
1528 // we can accept this urb if it is not queued at this time
1529 // or if non-iso transfer requests should be scheduled for the same device and pipe
1530 if ((!usb_pipeisoc(urb->pipe) && (tmp->dev == urb->dev) && !((tmp->pipe ^ urb->pipe) & mask)) ||
1531 (urb == tmp)) {
1532 return tmp; // found another urb already queued for processing
1536 return 0;
1538 /*-------------------------------------------------------------------*/
1539 _static int uhci_submit_urb (urb_t *urb)
1541 uhci_t *s;
1542 urb_priv_t *urb_priv;
1543 int ret = 0;
1544 unsigned long flags;
1545 urb_t *bulk_urb=NULL;
1547 if (!urb->dev || !urb->dev->bus)
1548 return -ENODEV;
1550 s = (uhci_t*) urb->dev->bus->hcpriv;
1551 //dbg("submit_urb: %p type %d",urb,usb_pipetype(urb->pipe));
1553 if (!s->running)
1554 return -ENODEV;
1556 if (usb_pipedevice (urb->pipe) == s->rh.devnum)
1557 return rh_submit_urb (urb); /* virtual root hub */
1559 usb_inc_dev_use (urb->dev);
1561 spin_lock_irqsave (&s->urb_list_lock, flags);
1563 bulk_urb = search_dev_ep (s, urb);
1565 if (bulk_urb) {
1567 queue_dbg("found bulk urb %p\n",bulk_urb);
1569 if ((usb_pipetype (urb->pipe) != PIPE_BULK) ||
1570 ((usb_pipetype (urb->pipe) == PIPE_BULK) &&
1571 (!(urb->transfer_flags & USB_QUEUE_BULK) || !(bulk_urb->transfer_flags & USB_QUEUE_BULK)))) {
1572 spin_unlock_irqrestore (&s->urb_list_lock, flags);
1573 usb_dec_dev_use (urb->dev);
1574 err("ENXIO %08x, flags %x, urb %p, burb %p",urb->pipe,urb->transfer_flags,urb,bulk_urb);
1575 return -ENXIO; // urb already queued
1579 #ifdef DEBUG_SLAB
1580 urb_priv = kmem_cache_alloc(urb_priv_kmem, SLAB_FLAG);
1581 #else
1582 urb_priv = kmalloc (sizeof (urb_priv_t), KMALLOC_FLAG);
1583 #endif
1584 if (!urb_priv) {
1585 usb_dec_dev_use (urb->dev);
1586 spin_unlock_irqrestore (&s->urb_list_lock, flags);
1587 return -ENOMEM;
1590 urb->hcpriv = urb_priv;
1591 INIT_LIST_HEAD (&urb_priv->desc_list);
1592 urb_priv->flags = 0;
1593 dbg("submit_urb: scheduling %p", urb);
1594 urb_priv->next_queued_urb = NULL;
1595 urb_priv->prev_queued_urb = NULL;
1596 urb_priv->bottom_qh = NULL;
1597 urb_priv->next_qh = NULL;
1599 if (usb_pipetype (urb->pipe) == PIPE_BULK) {
1601 if (bulk_urb) {
1602 while (((urb_priv_t*)bulk_urb->hcpriv)->next_queued_urb) // find last queued bulk
1603 bulk_urb=((urb_priv_t*)bulk_urb->hcpriv)->next_queued_urb;
1605 ((urb_priv_t*)bulk_urb->hcpriv)->next_queued_urb=urb;
1607 atomic_inc (&s->avoid_bulk);
1608 ret = uhci_submit_bulk_urb (urb, bulk_urb);
1609 atomic_dec (&s->avoid_bulk);
1610 spin_unlock_irqrestore (&s->urb_list_lock, flags);
1612 else {
1613 spin_unlock_irqrestore (&s->urb_list_lock, flags);
1614 switch (usb_pipetype (urb->pipe)) {
1615 case PIPE_ISOCHRONOUS:
1616 ret = uhci_submit_iso_urb (urb);
1617 break;
1618 case PIPE_INTERRUPT:
1619 ret = uhci_submit_int_urb (urb);
1620 break;
1621 case PIPE_CONTROL:
1622 ret = uhci_submit_control_urb (urb);
1623 break;
1624 default:
1625 ret = -EINVAL;
1629 dbg("submit_urb: scheduled with ret: %d", ret);
1631 if (ret != 0) {
1632 usb_dec_dev_use (urb->dev);
1633 #ifdef DEBUG_SLAB
1634 kmem_cache_free(urb_priv_kmem, urb_priv);
1635 #else
1636 kfree (urb_priv);
1637 #endif
1638 return ret;
1641 return 0;
1644 // Checks for URB timeout and removes bandwidth reclamation
1645 // if URB idles too long
1646 _static void uhci_check_timeouts(uhci_t *s)
1648 struct list_head *p,*p2;
1649 urb_t *urb;
1650 int type;
1652 p = s->urb_list.prev;
1654 while (p != &s->urb_list) {
1655 urb_priv_t *hcpriv;
1657 p2 = p;
1658 p = p->prev;
1659 urb = list_entry (p2, urb_t, urb_list);
1660 type = usb_pipetype (urb->pipe);
1662 hcpriv = (urb_priv_t*)urb->hcpriv;
1664 if ( urb->timeout &&
1665 ((hcpriv->started + urb->timeout) < jiffies)) {
1666 urb->transfer_flags |= USB_TIMEOUT_KILLED | USB_ASYNC_UNLINK;
1667 async_dbg("uhci_check_timeout: timeout for %p",urb);
1668 uhci_unlink_urb_async(s, urb);
1670 #ifdef CONFIG_USB_UHCI_HIGH_BANDWIDTH
1671 else if (((type == PIPE_BULK) || (type == PIPE_CONTROL)) &&
1672 (hcpriv->use_loop) &&
1673 ((hcpriv->started + IDLE_TIMEOUT) < jiffies))
1674 disable_desc_loop(s, urb);
1675 #endif
1678 s->timeout_check=jiffies;
1681 /*-------------------------------------------------------------------
1682 Virtual Root Hub
1683 -------------------------------------------------------------------*/
1685 _static __u8 root_hub_dev_des[] =
1687 0x12, /* __u8 bLength; */
1688 0x01, /* __u8 bDescriptorType; Device */
1689 0x00, /* __u16 bcdUSB; v1.0 */
1690 0x01,
1691 0x09, /* __u8 bDeviceClass; HUB_CLASSCODE */
1692 0x00, /* __u8 bDeviceSubClass; */
1693 0x00, /* __u8 bDeviceProtocol; */
1694 0x08, /* __u8 bMaxPacketSize0; 8 Bytes */
1695 0x00, /* __u16 idVendor; */
1696 0x00,
1697 0x00, /* __u16 idProduct; */
1698 0x00,
1699 0x00, /* __u16 bcdDevice; */
1700 0x00,
1701 0x00, /* __u8 iManufacturer; */
1702 0x02, /* __u8 iProduct; */
1703 0x01, /* __u8 iSerialNumber; */
1704 0x01 /* __u8 bNumConfigurations; */
1708 /* Configuration descriptor */
1709 _static __u8 root_hub_config_des[] =
1711 0x09, /* __u8 bLength; */
1712 0x02, /* __u8 bDescriptorType; Configuration */
1713 0x19, /* __u16 wTotalLength; */
1714 0x00,
1715 0x01, /* __u8 bNumInterfaces; */
1716 0x01, /* __u8 bConfigurationValue; */
1717 0x00, /* __u8 iConfiguration; */
1718 0x40, /* __u8 bmAttributes;
1719 Bit 7: Bus-powered, 6: Self-powered, 5 Remote-wakwup, 4..0: resvd */
1720 0x00, /* __u8 MaxPower; */
1722 /* interface */
1723 0x09, /* __u8 if_bLength; */
1724 0x04, /* __u8 if_bDescriptorType; Interface */
1725 0x00, /* __u8 if_bInterfaceNumber; */
1726 0x00, /* __u8 if_bAlternateSetting; */
1727 0x01, /* __u8 if_bNumEndpoints; */
1728 0x09, /* __u8 if_bInterfaceClass; HUB_CLASSCODE */
1729 0x00, /* __u8 if_bInterfaceSubClass; */
1730 0x00, /* __u8 if_bInterfaceProtocol; */
1731 0x00, /* __u8 if_iInterface; */
1733 /* endpoint */
1734 0x07, /* __u8 ep_bLength; */
1735 0x05, /* __u8 ep_bDescriptorType; Endpoint */
1736 0x81, /* __u8 ep_bEndpointAddress; IN Endpoint 1 */
1737 0x03, /* __u8 ep_bmAttributes; Interrupt */
1738 0x08, /* __u16 ep_wMaxPacketSize; 8 Bytes */
1739 0x00,
1740 0xff /* __u8 ep_bInterval; 255 ms */
1744 _static __u8 root_hub_hub_des[] =
1746 0x09, /* __u8 bLength; */
1747 0x29, /* __u8 bDescriptorType; Hub-descriptor */
1748 0x02, /* __u8 bNbrPorts; */
1749 0x00, /* __u16 wHubCharacteristics; */
1750 0x00,
1751 0x01, /* __u8 bPwrOn2pwrGood; 2ms */
1752 0x00, /* __u8 bHubContrCurrent; 0 mA */
1753 0x00, /* __u8 DeviceRemovable; *** 7 Ports max *** */
1754 0xff /* __u8 PortPwrCtrlMask; *** 7 ports max *** */
1757 /*-------------------------------------------------------------------------*/
1758 /* prepare Interrupt pipe transaction data; HUB INTERRUPT ENDPOINT */
1759 _static int rh_send_irq (urb_t *urb)
1761 int len = 1;
1762 int i;
1763 uhci_t *uhci = urb->dev->bus->hcpriv;
1764 unsigned int io_addr = uhci->io_addr;
1765 __u16 data = 0;
1767 for (i = 0; i < uhci->rh.numports; i++) {
1768 data |= ((inw (io_addr + USBPORTSC1 + i * 2) & 0xa) > 0 ? (1 << (i + 1)) : 0);
1769 len = (i + 1) / 8 + 1;
1772 *(__u16 *) urb->transfer_buffer = cpu_to_le16 (data);
1773 urb->actual_length = len;
1774 urb->status = 0;
1776 if ((data > 0) && (uhci->rh.send != 0)) {
1777 dbg("Root-Hub INT complete: port1: %x port2: %x data: %x",
1778 inw (io_addr + USBPORTSC1), inw (io_addr + USBPORTSC2), data);
1779 urb->complete (urb);
1781 return 0;
1784 /*-------------------------------------------------------------------------*/
1785 /* Virtual Root Hub INTs are polled by this timer every "intervall" ms */
1786 _static int rh_init_int_timer (urb_t *urb);
1788 _static void rh_int_timer_do (unsigned long ptr)
1790 int len;
1791 urb_t *urb = (urb_t*) ptr;
1792 uhci_t *uhci = urb->dev->bus->hcpriv;
1794 if (uhci->rh.send) {
1795 len = rh_send_irq (urb);
1796 if (len > 0) {
1797 urb->actual_length = len;
1798 if (urb->complete)
1799 urb->complete (urb);
1802 rh_init_int_timer (urb);
1805 /*-------------------------------------------------------------------------*/
1806 /* Root Hub INTs are polled by this timer, polling interval 20ms */
1807 /* This time is also used for URB-timeout checking */
1809 _static int rh_init_int_timer (urb_t *urb)
1811 uhci_t *uhci = urb->dev->bus->hcpriv;
1813 uhci->rh.interval = urb->interval;
1814 init_timer (&uhci->rh.rh_int_timer);
1815 uhci->rh.rh_int_timer.function = rh_int_timer_do;
1816 uhci->rh.rh_int_timer.data = (unsigned long) urb;
1817 uhci->rh.rh_int_timer.expires = jiffies + (HZ * 20) / 1000;
1818 add_timer (&uhci->rh.rh_int_timer);
1820 return 0;
1823 /*-------------------------------------------------------------------------*/
1824 #define OK(x) len = (x); break
1826 #define CLR_RH_PORTSTAT(x) \
1827 status = inw(io_addr+USBPORTSC1+2*(wIndex-1)); \
1828 status = (status & 0xfff5) & ~(x); \
1829 outw(status, io_addr+USBPORTSC1+2*(wIndex-1))
1831 #define SET_RH_PORTSTAT(x) \
1832 status = inw(io_addr+USBPORTSC1+2*(wIndex-1)); \
1833 status = (status & 0xfff5) | (x); \
1834 outw(status, io_addr+USBPORTSC1+2*(wIndex-1))
1837 /*-------------------------------------------------------------------------*/
1838 /****
1839 ** Root Hub Control Pipe
1840 *************************/
1843 _static int rh_submit_urb (urb_t *urb)
1845 struct usb_device *usb_dev = urb->dev;
1846 uhci_t *uhci = usb_dev->bus->hcpriv;
1847 unsigned int pipe = urb->pipe;
1848 devrequest *cmd = (devrequest *) urb->setup_packet;
1849 void *data = urb->transfer_buffer;
1850 int leni = urb->transfer_buffer_length;
1851 int len = 0;
1852 int status = 0;
1853 int stat = 0;
1854 int i;
1855 unsigned int io_addr = uhci->io_addr;
1856 __u16 cstatus;
1858 __u16 bmRType_bReq;
1859 __u16 wValue;
1860 __u16 wIndex;
1861 __u16 wLength;
1863 if (usb_pipetype (pipe) == PIPE_INTERRUPT) {
1864 dbg("Root-Hub submit IRQ: every %d ms", urb->interval);
1865 uhci->rh.urb = urb;
1866 uhci->rh.send = 1;
1867 uhci->rh.interval = urb->interval;
1868 rh_init_int_timer (urb);
1870 return 0;
1874 bmRType_bReq = cmd->requesttype | cmd->request << 8;
1875 wValue = le16_to_cpu (cmd->value);
1876 wIndex = le16_to_cpu (cmd->index);
1877 wLength = le16_to_cpu (cmd->length);
1879 for (i = 0; i < 8; i++)
1880 uhci->rh.c_p_r[i] = 0;
1882 dbg("Root-Hub: adr: %2x cmd(%1x): %04x %04x %04x %04x",
1883 uhci->rh.devnum, 8, bmRType_bReq, wValue, wIndex, wLength);
1885 switch (bmRType_bReq) {
1886 /* Request Destination:
1887 without flags: Device,
1888 RH_INTERFACE: interface,
1889 RH_ENDPOINT: endpoint,
1890 RH_CLASS means HUB here,
1891 RH_OTHER | RH_CLASS almost ever means HUB_PORT here
1894 case RH_GET_STATUS:
1895 *(__u16 *) data = cpu_to_le16 (1);
1896 OK (2);
1897 case RH_GET_STATUS | RH_INTERFACE:
1898 *(__u16 *) data = cpu_to_le16 (0);
1899 OK (2);
1900 case RH_GET_STATUS | RH_ENDPOINT:
1901 *(__u16 *) data = cpu_to_le16 (0);
1902 OK (2);
1903 case RH_GET_STATUS | RH_CLASS:
1904 *(__u32 *) data = cpu_to_le32 (0);
1905 OK (4); /* hub power ** */
1906 case RH_GET_STATUS | RH_OTHER | RH_CLASS:
1907 status = inw (io_addr + USBPORTSC1 + 2 * (wIndex - 1));
1908 cstatus = ((status & USBPORTSC_CSC) >> (1 - 0)) |
1909 ((status & USBPORTSC_PEC) >> (3 - 1)) |
1910 (uhci->rh.c_p_r[wIndex - 1] << (0 + 4));
1911 status = (status & USBPORTSC_CCS) |
1912 ((status & USBPORTSC_PE) >> (2 - 1)) |
1913 ((status & USBPORTSC_SUSP) >> (12 - 2)) |
1914 ((status & USBPORTSC_PR) >> (9 - 4)) |
1915 (1 << 8) | /* power on ** */
1916 ((status & USBPORTSC_LSDA) << (-8 + 9));
1918 *(__u16 *) data = cpu_to_le16 (status);
1919 *(__u16 *) (data + 2) = cpu_to_le16 (cstatus);
1920 OK (4);
1922 case RH_CLEAR_FEATURE | RH_ENDPOINT:
1923 switch (wValue) {
1924 case (RH_ENDPOINT_STALL):
1925 OK (0);
1927 break;
1929 case RH_CLEAR_FEATURE | RH_CLASS:
1930 switch (wValue) {
1931 case (RH_C_HUB_OVER_CURRENT):
1932 OK (0); /* hub power over current ** */
1934 break;
1936 case RH_CLEAR_FEATURE | RH_OTHER | RH_CLASS:
1937 switch (wValue) {
1938 case (RH_PORT_ENABLE):
1939 CLR_RH_PORTSTAT (USBPORTSC_PE);
1940 OK (0);
1941 case (RH_PORT_SUSPEND):
1942 CLR_RH_PORTSTAT (USBPORTSC_SUSP);
1943 OK (0);
1944 case (RH_PORT_POWER):
1945 OK (0); /* port power ** */
1946 case (RH_C_PORT_CONNECTION):
1947 SET_RH_PORTSTAT (USBPORTSC_CSC);
1948 OK (0);
1949 case (RH_C_PORT_ENABLE):
1950 SET_RH_PORTSTAT (USBPORTSC_PEC);
1951 OK (0);
1952 case (RH_C_PORT_SUSPEND):
1953 /*** WR_RH_PORTSTAT(RH_PS_PSSC); */
1954 OK (0);
1955 case (RH_C_PORT_OVER_CURRENT):
1956 OK (0); /* port power over current ** */
1957 case (RH_C_PORT_RESET):
1958 uhci->rh.c_p_r[wIndex - 1] = 0;
1959 OK (0);
1961 break;
1963 case RH_SET_FEATURE | RH_OTHER | RH_CLASS:
1964 switch (wValue) {
1965 case (RH_PORT_SUSPEND):
1966 SET_RH_PORTSTAT (USBPORTSC_SUSP);
1967 OK (0);
1968 case (RH_PORT_RESET):
1969 SET_RH_PORTSTAT (USBPORTSC_PR);
1970 uhci_wait_ms (10);
1971 uhci->rh.c_p_r[wIndex - 1] = 1;
1972 CLR_RH_PORTSTAT (USBPORTSC_PR);
1973 udelay (10);
1974 SET_RH_PORTSTAT (USBPORTSC_PE);
1975 uhci_wait_ms (10);
1976 SET_RH_PORTSTAT (0xa);
1977 OK (0);
1978 case (RH_PORT_POWER):
1979 OK (0); /* port power ** */
1980 case (RH_PORT_ENABLE):
1981 SET_RH_PORTSTAT (USBPORTSC_PE);
1982 OK (0);
1984 break;
1986 case RH_SET_ADDRESS:
1987 uhci->rh.devnum = wValue;
1988 OK (0);
1990 case RH_GET_DESCRIPTOR:
1991 switch ((wValue & 0xff00) >> 8) {
1992 case (0x01): /* device descriptor */
1993 len = min (leni, min (sizeof (root_hub_dev_des), wLength));
1994 memcpy (data, root_hub_dev_des, len);
1995 OK (len);
1996 case (0x02): /* configuration descriptor */
1997 len = min (leni, min (sizeof (root_hub_config_des), wLength));
1998 memcpy (data, root_hub_config_des, len);
1999 OK (len);
2000 case (0x03): /* string descriptors */
2001 len = usb_root_hub_string (wValue & 0xff,
2002 uhci->io_addr, "UHCI",
2003 data, wLength);
2004 if (len > 0) {
2005 OK (min (leni, len));
2006 } else
2007 stat = -EPIPE;
2009 break;
2011 case RH_GET_DESCRIPTOR | RH_CLASS:
2012 root_hub_hub_des[2] = uhci->rh.numports;
2013 len = min (leni, min (sizeof (root_hub_hub_des), wLength));
2014 memcpy (data, root_hub_hub_des, len);
2015 OK (len);
2017 case RH_GET_CONFIGURATION:
2018 *(__u8 *) data = 0x01;
2019 OK (1);
2021 case RH_SET_CONFIGURATION:
2022 OK (0);
2023 default:
2024 stat = -EPIPE;
2027 dbg("Root-Hub stat port1: %x port2: %x",
2028 inw (io_addr + USBPORTSC1), inw (io_addr + USBPORTSC2));
2030 urb->actual_length = len;
2031 urb->status = stat;
2032 if (urb->complete)
2033 urb->complete (urb);
2034 return 0;
2036 /*-------------------------------------------------------------------------*/
2038 _static int rh_unlink_urb (urb_t *urb)
2040 uhci_t *uhci = urb->dev->bus->hcpriv;
2042 if (uhci->rh.urb==urb) {
2043 dbg("Root-Hub unlink IRQ");
2044 uhci->rh.send = 0;
2045 del_timer (&uhci->rh.rh_int_timer);
2047 return 0;
2049 /*-------------------------------------------------------------------*/
2052 * Map status to standard result codes
2054 * <status> is (td->status & 0xFE0000) [a.k.a. uhci_status_bits(td->status)
2055 * <dir_out> is True for output TDs and False for input TDs.
2057 _static int uhci_map_status (int status, int dir_out)
2059 if (!status)
2060 return 0;
2061 if (status & TD_CTRL_BITSTUFF) /* Bitstuff error */
2062 return -EPROTO;
2063 if (status & TD_CTRL_CRCTIMEO) { /* CRC/Timeout */
2064 if (dir_out)
2065 return -ETIMEDOUT;
2066 else
2067 return -EILSEQ;
2069 if (status & TD_CTRL_NAK) /* NAK */
2070 return -ETIMEDOUT;
2071 if (status & TD_CTRL_BABBLE) /* Babble */
2072 return -EPIPE;
2073 if (status & TD_CTRL_DBUFERR) /* Buffer error */
2074 return -ENOSR;
2075 if (status & TD_CTRL_STALLED) /* Stalled */
2076 return -EPIPE;
2077 if (status & TD_CTRL_ACTIVE) /* Active */
2078 return 0;
2080 return -EPROTO;
2084 * Only the USB core should call uhci_alloc_dev and uhci_free_dev
2086 _static int uhci_alloc_dev (struct usb_device *usb_dev)
2088 return 0;
2091 _static void uhci_unlink_urbs(uhci_t *s, struct usb_device *usb_dev, int remove_all)
2093 unsigned long flags;
2094 struct list_head *p;
2095 struct list_head *p2;
2096 urb_t *urb;
2098 spin_lock_irqsave (&s->urb_list_lock, flags);
2099 p = s->urb_list.prev;
2100 while (p != &s->urb_list) {
2101 p2 = p;
2102 p = p->prev ;
2103 urb = list_entry (p2, urb_t, urb_list);
2104 dbg("urb: %p, dev %p, %p", urb, usb_dev,urb->dev);
2106 //urb->transfer_flags |=USB_ASYNC_UNLINK;
2108 if (remove_all || (usb_dev == urb->dev)) {
2109 spin_unlock_irqrestore (&s->urb_list_lock, flags);
2110 warn("forced removing of queued URB %p due to disconnect",urb);
2111 uhci_unlink_urb(urb);
2112 urb->dev = NULL; // avoid further processing of this UR
2113 spin_lock_irqsave (&s->urb_list_lock, flags);
2114 p = s->urb_list.prev;
2117 spin_unlock_irqrestore (&s->urb_list_lock, flags);
2120 _static int uhci_free_dev (struct usb_device *usb_dev)
2122 uhci_t *s;
2125 if(!usb_dev || !usb_dev->bus || !usb_dev->bus->hcpriv)
2126 return -EINVAL;
2128 s=(uhci_t*) usb_dev->bus->hcpriv;
2129 uhci_unlink_urbs(s, usb_dev, 0);
2131 return 0;
2135 * uhci_get_current_frame_number()
2137 * returns the current frame number for a USB bus/controller.
2139 _static int uhci_get_current_frame_number (struct usb_device *usb_dev)
2141 return UHCI_GET_CURRENT_FRAME ((uhci_t*) usb_dev->bus->hcpriv);
2144 struct usb_operations uhci_device_operations =
2146 uhci_alloc_dev,
2147 uhci_free_dev,
2148 uhci_get_current_frame_number,
2149 uhci_submit_urb,
2150 uhci_unlink_urb
2154 * For IN-control transfers, process_transfer gets a bit more complicated,
2155 * since there are devices that return less data (eg. strings) than they
2156 * have announced. This leads to a queue abort due to the short packet,
2157 * the status stage is not executed. If this happens, the status stage
2158 * is manually re-executed.
2159 * mode: 0: QHs already unlinked
2162 _static int process_transfer (uhci_t *s, urb_t *urb, int mode)
2164 int ret = 0;
2165 urb_priv_t *urb_priv = urb->hcpriv;
2166 struct list_head *qhl = urb_priv->desc_list.next;
2167 uhci_desc_t *qh = list_entry (qhl, uhci_desc_t, desc_list);
2168 struct list_head *p = qh->vertical.next;
2169 uhci_desc_t *desc= list_entry (urb_priv->desc_list.prev, uhci_desc_t, desc_list);
2170 uhci_desc_t *last_desc = list_entry (desc->vertical.prev, uhci_desc_t, vertical);
2171 int data_toggle = usb_gettoggle (urb->dev, usb_pipeendpoint (urb->pipe), usb_pipeout (urb->pipe)); // save initial data_toggle
2172 int maxlength; // extracted and remapped info from TD
2173 int actual_length;
2174 int status = 0;
2176 //dbg("process_transfer: urb contains bulk/control request");
2178 /* if the status phase has been retriggered and the
2179 queue is empty or the last status-TD is inactive, the retriggered
2180 status stage is completed
2183 if (urb_priv->flags &&
2184 ((qh->hw.qh.element == UHCI_PTR_TERM) ||(!(last_desc->hw.td.status & TD_CTRL_ACTIVE))))
2185 goto transfer_finished;
2187 urb->actual_length=0;
2189 for (; p != &qh->vertical; p = p->next) {
2190 desc = list_entry (p, uhci_desc_t, vertical);
2192 if (desc->hw.td.status & TD_CTRL_ACTIVE) // do not process active TDs
2193 return ret;
2195 actual_length = (desc->hw.td.status + 1) & 0x7ff; // extract transfer parameters from TD
2196 maxlength = (((desc->hw.td.info >> 21) & 0x7ff) + 1) & 0x7ff;
2197 status = uhci_map_status (uhci_status_bits (desc->hw.td.status), usb_pipeout (urb->pipe));
2199 if (status == -EPIPE) { // see if EP is stalled
2200 // set up stalled condition
2201 usb_endpoint_halt (urb->dev, usb_pipeendpoint (urb->pipe), usb_pipeout (urb->pipe));
2204 if (status && (status != -EPIPE)) { // if any error occurred stop processing of further TDs
2205 // only set ret if status returned an error
2206 is_error:
2207 ret = status;
2208 urb->error_count++;
2209 break;
2211 else if ((desc->hw.td.info & 0xff) != USB_PID_SETUP)
2212 urb->actual_length += actual_length;
2214 // got less data than requested
2215 if ( (actual_length < maxlength)) {
2216 if (urb->transfer_flags & USB_DISABLE_SPD) {
2217 status = -EREMOTEIO; // treat as real error
2218 dbg("process_transfer: SPD!!");
2219 break; // exit after this TD because SP was detected
2222 // short read during control-IN: re-start status stage
2223 if ((usb_pipetype (urb->pipe) == PIPE_CONTROL)) {
2224 if (uhci_packetid(last_desc->hw.td.info) == USB_PID_OUT) {
2226 qh->hw.qh.element = virt_to_bus (last_desc); // re-trigger status stage
2227 dbg("short packet during control transfer, retrigger status stage @ %p",last_desc);
2228 //uhci_show_td (desc);
2229 //uhci_show_td (last_desc);
2230 urb_priv->flags = 1; // mark as short control packet
2231 return 0;
2234 // all other cases: short read is OK
2235 data_toggle = uhci_toggle (desc->hw.td.info);
2236 break;
2238 else if (status)
2239 goto is_error;
2241 data_toggle = uhci_toggle (desc->hw.td.info);
2242 queue_dbg("process_transfer: len:%d status:%x mapped:%x toggle:%d", actual_length, desc->hw.td.status,status, data_toggle);
2246 usb_settoggle (urb->dev, usb_pipeendpoint (urb->pipe), usb_pipeout (urb->pipe), !data_toggle);
2248 transfer_finished:
2250 uhci_clean_transfer(s, urb, qh, (mode==0?2:1));
2252 urb->status = status;
2254 #ifdef CONFIG_USB_UHCI_HIGH_BANDWIDTH
2255 disable_desc_loop(s,urb);
2256 #endif
2258 queue_dbg("process_transfer: (end) urb %p, wanted len %d, len %d status %x err %d",
2259 urb,urb->transfer_buffer_length,urb->actual_length, urb->status, urb->error_count);
2260 return ret;
2263 _static int process_interrupt (uhci_t *s, urb_t *urb)
2265 int i, ret = -EINPROGRESS;
2266 urb_priv_t *urb_priv = urb->hcpriv;
2267 struct list_head *p = urb_priv->desc_list.next;
2268 uhci_desc_t *desc = list_entry (urb_priv->desc_list.prev, uhci_desc_t, desc_list);
2270 int actual_length;
2271 int status = 0;
2273 //dbg("urb contains interrupt request");
2275 for (i = 0; p != &urb_priv->desc_list; p = p->next, i++) // Maybe we allow more than one TD later ;-)
2277 desc = list_entry (p, uhci_desc_t, desc_list);
2279 if (desc->hw.td.status & TD_CTRL_ACTIVE) {
2280 // do not process active TDs
2281 //dbg("TD ACT Status @%p %08x",desc,desc->hw.td.status);
2282 break;
2285 if (!desc->hw.td.status & TD_CTRL_IOC) {
2286 // do not process one-shot TDs, no recycling
2287 break;
2289 // extract transfer parameters from TD
2291 actual_length = (desc->hw.td.status + 1) & 0x7ff;
2292 status = uhci_map_status (uhci_status_bits (desc->hw.td.status), usb_pipeout (urb->pipe));
2294 // see if EP is stalled
2295 if (status == -EPIPE) {
2296 // set up stalled condition
2297 usb_endpoint_halt (urb->dev, usb_pipeendpoint (urb->pipe), usb_pipeout (urb->pipe));
2300 // if any error occured: ignore this td, and continue
2301 if (status != 0) {
2302 //uhci_show_td (desc);
2303 urb->error_count++;
2304 goto recycle;
2306 else
2307 urb->actual_length = actual_length;
2309 recycle:
2310 if (urb->complete) {
2311 //dbg("process_interrupt: calling completion, status %i",status);
2312 urb->status = status;
2313 ((urb_priv_t*)urb->hcpriv)->flags=1; // if unlink_urb is called during completion
2315 spin_unlock(&s->urb_list_lock);
2317 urb->complete ((struct urb *) urb);
2319 spin_lock(&s->urb_list_lock);
2321 ((urb_priv_t*)urb->hcpriv)->flags=0;
2324 if ((urb->status != -ECONNABORTED) && (urb->status != ECONNRESET) &&
2325 (urb->status != -ENOENT)) {
2327 urb->status = -EINPROGRESS;
2329 // Recycle INT-TD if interval!=0, else mark TD as one-shot
2330 if (urb->interval) {
2332 desc->hw.td.info &= ~(1 << TD_TOKEN_TOGGLE);
2333 if (status==0) {
2334 ((urb_priv_t*)urb->hcpriv)->started=jiffies;
2335 desc->hw.td.info |= (usb_gettoggle (urb->dev, usb_pipeendpoint (urb->pipe),
2336 usb_pipeout (urb->pipe)) << TD_TOKEN_TOGGLE);
2337 usb_dotoggle (urb->dev, usb_pipeendpoint (urb->pipe), usb_pipeout (urb->pipe));
2338 } else {
2339 desc->hw.td.info |= (!usb_gettoggle (urb->dev, usb_pipeendpoint (urb->pipe),
2340 usb_pipeout (urb->pipe)) << TD_TOKEN_TOGGLE);
2342 desc->hw.td.status= (urb->pipe & TD_CTRL_LS) | TD_CTRL_ACTIVE | TD_CTRL_IOC |
2343 (urb->transfer_flags & USB_DISABLE_SPD ? 0 : TD_CTRL_SPD) | (3 << 27);
2344 mb();
2346 else {
2347 uhci_unlink_urb_async(s, urb);
2348 desc->hw.td.status &= ~TD_CTRL_IOC; // inactivate TD
2353 return ret;
2356 // mode: 1: force processing, don't unlink tds (already unlinked)
2357 _static int process_iso (uhci_t *s, urb_t *urb, int mode)
2359 int i;
2360 int ret = 0;
2361 urb_priv_t *urb_priv = urb->hcpriv;
2362 struct list_head *p = urb_priv->desc_list.next;
2363 uhci_desc_t *desc = list_entry (urb_priv->desc_list.prev, uhci_desc_t, desc_list);
2365 dbg("urb contains iso request");
2366 if ((desc->hw.td.status & TD_CTRL_ACTIVE) && !mode)
2367 return -EXDEV; // last TD not finished
2369 urb->error_count = 0;
2370 urb->actual_length = 0;
2371 urb->status = 0;
2372 dbg("process iso urb %p, %li, %i, %i, %i %08x",urb,jiffies,UHCI_GET_CURRENT_FRAME(s),
2373 urb->number_of_packets,mode,desc->hw.td.status);
2375 for (i = 0; p != &urb_priv->desc_list; i++) {
2376 desc = list_entry (p, uhci_desc_t, desc_list);
2378 //uhci_show_td(desc);
2379 if (desc->hw.td.status & TD_CTRL_ACTIVE) {
2380 // means we have completed the last TD, but not the TDs before
2381 desc->hw.td.status &= ~TD_CTRL_ACTIVE;
2382 dbg("TD still active (%x)- grrr. paranoia!", desc->hw.td.status);
2383 ret = -EXDEV;
2384 urb->iso_frame_desc[i].status = ret;
2385 unlink_td (s, desc, 1);
2386 // FIXME: immediate deletion may be dangerous
2387 goto err;
2390 if (!mode)
2391 unlink_td (s, desc, 1);
2393 if (urb->number_of_packets <= i) {
2394 dbg("urb->number_of_packets (%d)<=(%d)", urb->number_of_packets, i);
2395 ret = -EINVAL;
2396 goto err;
2399 if (urb->iso_frame_desc[i].offset + urb->transfer_buffer != bus_to_virt (desc->hw.td.buffer)) {
2400 // Hm, something really weird is going on
2401 dbg("Pointer Paranoia: %p!=%p", urb->iso_frame_desc[i].offset + urb->transfer_buffer, bus_to_virt (desc->hw.td.buffer));
2402 ret = -EINVAL;
2403 urb->iso_frame_desc[i].status = ret;
2404 goto err;
2406 urb->iso_frame_desc[i].actual_length = (desc->hw.td.status + 1) & 0x7ff;
2407 urb->iso_frame_desc[i].status = uhci_map_status (uhci_status_bits (desc->hw.td.status), usb_pipeout (urb->pipe));
2408 urb->actual_length += urb->iso_frame_desc[i].actual_length;
2410 err:
2412 if (urb->iso_frame_desc[i].status != 0) {
2413 urb->error_count++;
2414 urb->status = urb->iso_frame_desc[i].status;
2416 dbg("process_iso: %i: len:%d %08x status:%x",
2417 i, urb->iso_frame_desc[i].actual_length, desc->hw.td.status,urb->iso_frame_desc[i].status);
2419 list_del (p);
2420 p = p->next;
2421 delete_desc (desc);
2424 dbg("process_iso: exit %i (%d), actual_len %i", i, ret,urb->actual_length);
2425 return ret;
2429 _static int process_urb (uhci_t *s, struct list_head *p)
2431 int ret = 0;
2432 urb_t *urb;
2435 urb=list_entry (p, urb_t, urb_list);
2436 //dbg("process_urb: found queued urb: %p", urb);
2438 switch (usb_pipetype (urb->pipe)) {
2439 case PIPE_CONTROL:
2440 ret = process_transfer (s, urb, 1);
2441 break;
2442 case PIPE_BULK:
2443 if (!s->avoid_bulk.counter)
2444 ret = process_transfer (s, urb, 1);
2445 else
2446 return 0;
2447 break;
2448 case PIPE_ISOCHRONOUS:
2449 ret = process_iso (s, urb, 0);
2450 break;
2451 case PIPE_INTERRUPT:
2452 ret = process_interrupt (s, urb);
2453 break;
2456 if (urb->status != -EINPROGRESS) {
2457 int proceed = 0;
2459 dbg("dequeued urb: %p", urb);
2460 dequeue_urb (s, urb);
2462 #ifdef DEBUG_SLAB
2463 kmem_cache_free(urb_priv_kmem, urb->hcpriv);
2464 #else
2465 kfree (urb->hcpriv);
2466 #endif
2468 if ((usb_pipetype (urb->pipe) != PIPE_INTERRUPT)) {
2469 urb_t *tmp = urb->next; // pointer to first urb
2470 int is_ring = 0;
2472 if (urb->next) {
2473 do {
2474 if (tmp->status != -EINPROGRESS) {
2475 proceed = 1;
2476 break;
2478 tmp = tmp->next;
2480 while (tmp != NULL && tmp != urb->next);
2481 if (tmp == urb->next)
2482 is_ring = 1;
2485 spin_lock(&urb->lock);
2486 spin_unlock(&s->urb_list_lock);
2488 // In case you need the current URB status for your completion handler (before resubmit)
2489 if (urb->complete && (!proceed )) {
2490 dbg("process_transfer: calling early completion");
2491 urb->complete ((struct urb *) urb);
2492 if (!proceed && is_ring && (urb->status != -ENOENT))
2493 uhci_submit_urb (urb);
2496 if (proceed && urb->next) {
2497 // if there are linked urbs - handle submitting of them right now.
2498 tmp = urb->next; // pointer to first urb
2500 do {
2501 if ((tmp->status != -EINPROGRESS) && (tmp->status != -ENOENT) && uhci_submit_urb (tmp) != 0)
2502 break;
2503 tmp = tmp->next;
2505 while (tmp != NULL && tmp != urb->next); // submit until we reach NULL or our own pointer or submit fails
2507 if (urb->complete) {
2508 dbg("process_transfer: calling completion");
2509 urb->complete ((struct urb *) urb);
2513 usb_dec_dev_use (urb->dev);
2514 spin_unlock(&urb->lock);
2515 spin_lock(&s->urb_list_lock);
2519 return ret;
2522 _static void uhci_interrupt (int irq, void *__uhci, struct pt_regs *regs)
2524 uhci_t *s = __uhci;
2525 unsigned int io_addr = s->io_addr;
2526 unsigned short status;
2527 struct list_head *p, *p2;
2530 * Read the interrupt status, and write it back to clear the
2531 * interrupt cause
2534 status = inw (io_addr + USBSTS);
2536 if (!status) /* shared interrupt, not mine */
2537 return;
2539 dbg("interrupt");
2541 if (status != 1) {
2542 warn("interrupt, status %x, frame# %i", status,
2543 UHCI_GET_CURRENT_FRAME(s));
2545 // remove host controller halted state
2546 if ((status&0x20) && (s->running)) {
2547 outw (USBCMD_RS | inw(io_addr + USBCMD), io_addr + USBCMD);
2549 //uhci_show_status (s);
2552 * traverse the list in *reverse* direction, because new entries
2553 * may be added at the end.
2554 * also, because process_urb may unlink the current urb,
2555 * we need to advance the list before
2558 spin_lock (&s->urb_list_lock);
2559 restart:
2560 s->unlink_urb_done=0;
2561 p = s->urb_list.prev;
2563 while (p != &s->urb_list) {
2564 p2 = p;
2565 p = p->prev;
2566 process_urb (s, p2);
2567 if (s->unlink_urb_done) {
2568 s->unlink_urb_done=0;
2569 goto restart;
2572 if ((jiffies - s->timeout_check) > (HZ/30))
2573 uhci_check_timeouts(s);
2575 clean_descs(s,0);
2576 uhci_cleanup_unlink(s, 0);
2577 uhci_switch_timer_int(s);
2579 spin_unlock (&s->urb_list_lock);
2581 outw (status, io_addr + USBSTS);
2583 //dbg("uhci_interrupt: done");
2586 _static void reset_hc (uhci_t *s)
2588 unsigned int io_addr = s->io_addr;
2590 s->apm_state = 0;
2591 /* Global reset for 50ms */
2592 outw (USBCMD_GRESET, io_addr + USBCMD);
2593 uhci_wait_ms (50);
2594 outw (0, io_addr + USBCMD);
2595 uhci_wait_ms (10);
2598 _static void start_hc (uhci_t *s)
2600 unsigned int io_addr = s->io_addr;
2601 int timeout = 1000;
2604 * Reset the HC - this will force us to get a
2605 * new notification of any already connected
2606 * ports due to the virtual disconnect that it
2607 * implies.
2609 outw (USBCMD_HCRESET, io_addr + USBCMD);
2611 while (inw (io_addr + USBCMD) & USBCMD_HCRESET) {
2612 if (!--timeout) {
2613 err("USBCMD_HCRESET timed out!");
2614 break;
2618 /* Turn on all interrupts */
2619 outw (USBINTR_TIMEOUT | USBINTR_RESUME | USBINTR_IOC | USBINTR_SP, io_addr + USBINTR);
2621 /* Start at frame 0 */
2622 outw (0, io_addr + USBFRNUM);
2623 outl (virt_to_bus (s->framelist), io_addr + USBFLBASEADD);
2625 /* Run and mark it configured with a 64-byte max packet */
2626 outw (USBCMD_RS | USBCMD_CF | USBCMD_MAXP, io_addr + USBCMD);
2627 s->apm_state = 1;
2628 s->running = 1;
2631 _static void uhci_cleanup_dev(uhci_t *s)
2633 struct usb_device *root_hub = s->bus->root_hub;
2635 s->running = 0; // Don't allow submit_urb
2637 if (root_hub)
2638 usb_disconnect (&root_hub);
2640 reset_hc (s);
2641 wait_ms (1);
2643 uhci_unlink_urbs (s, 0, 1); // Forced unlink of remaining URBs
2644 uhci_cleanup_unlink (s, 1); // force cleanup of async killed URBs
2646 usb_deregister_bus (s->bus);
2648 release_region (s->io_addr, s->io_size);
2649 free_irq (s->irq, s);
2650 usb_free_bus (s->bus);
2651 cleanup_skel (s);
2652 kfree (s);
2655 _static int __init uhci_start_usb (uhci_t *s)
2656 { /* start it up */
2657 /* connect the virtual root hub */
2658 struct usb_device *usb_dev;
2660 usb_dev = usb_alloc_dev (NULL, s->bus);
2661 if (!usb_dev)
2662 return -1;
2664 s->bus->root_hub = usb_dev;
2665 usb_connect (usb_dev);
2667 if (usb_new_device (usb_dev) != 0) {
2668 usb_free_dev (usb_dev);
2669 return -1;
2672 return 0;
2675 _static int handle_pm_event (struct pm_dev *dev, pm_request_t rqst, void *data)
2677 uhci_t *s = (uhci_t*) dev->data;
2678 dbg("handle_apm_event(%d)", rqst);
2679 if (s) {
2680 switch (rqst) {
2681 case PM_SUSPEND:
2682 reset_hc (s);
2683 break;
2684 case PM_RESUME:
2685 start_hc (s);
2686 break;
2689 return 0;
2692 _static int __init alloc_uhci (struct pci_dev *dev, int irq, unsigned int io_addr, unsigned int io_size)
2694 uhci_t *s;
2695 struct usb_bus *bus;
2696 struct pm_dev *pmdev;
2697 char buf[8], *bufp = buf;
2699 #ifndef __sparc__
2700 sprintf(buf, "%d", irq);
2701 #else
2702 bufp = __irq_itoa(irq);
2703 #endif
2704 printk(KERN_INFO __FILE__ ": USB UHCI at I/O 0x%x, IRQ %s\n",
2705 io_addr, bufp);
2707 s = kmalloc (sizeof (uhci_t), GFP_KERNEL);
2708 if (!s)
2709 return -1;
2711 memset (s, 0, sizeof (uhci_t));
2712 INIT_LIST_HEAD (&s->free_desc);
2713 INIT_LIST_HEAD (&s->urb_list);
2714 INIT_LIST_HEAD (&s->urb_unlinked);
2715 spin_lock_init (&s->urb_list_lock);
2716 spin_lock_init (&s->qh_lock);
2717 spin_lock_init (&s->td_lock);
2718 atomic_set(&s->avoid_bulk, 0);
2719 s->timeout_urbs = 0;
2720 s->irq = -1;
2721 s->io_addr = io_addr;
2722 s->io_size = io_size;
2723 s->next = devs; //chain new uhci device into global list
2724 s->timeout_check = 0;
2725 s->uhci_pci=dev;
2727 bus = usb_alloc_bus (&uhci_device_operations);
2728 if (!bus) {
2729 kfree (s);
2730 return -1;
2733 s->bus = bus;
2734 bus->hcpriv = s;
2736 /* UHCI specs says devices must have 2 ports, but goes on to say */
2737 /* they may have more but give no way to determine how many they */
2738 /* have, so default to 2 */
2739 /* According to the UHCI spec, Bit 7 is always set to 1. So we try */
2740 /* to use this to our advantage */
2742 for (s->maxports = 0; s->maxports < (io_size - 0x10) / 2; s->maxports++) {
2743 unsigned int portstatus;
2745 portstatus = inw (io_addr + 0x10 + (s->maxports * 2));
2746 dbg("port %i, adr %x status %x", s->maxports,
2747 io_addr + 0x10 + (s->maxports * 2), portstatus);
2748 if (!(portstatus & 0x0080))
2749 break;
2751 warn("Detected %d ports", s->maxports);
2753 /* This is experimental so anything less than 2 or greater than 8 is */
2754 /* something weird and we'll ignore it */
2755 if (s->maxports < 2 || s->maxports > 8) {
2756 dbg("Port count misdetected, forcing to 2 ports");
2757 s->maxports = 2;
2760 s->rh.numports = s->maxports;
2761 s->loop_usage=0;
2762 if (init_skel (s)) {
2763 usb_free_bus (bus);
2764 kfree(s);
2765 return -1;
2768 request_region (s->io_addr, io_size, MODNAME);
2769 reset_hc (s);
2770 usb_register_bus (s->bus);
2772 start_hc (s);
2774 if (request_irq (irq, uhci_interrupt, SA_SHIRQ, MODNAME, s)) {
2775 err("request_irq %d failed!",irq);
2776 usb_free_bus (bus);
2777 reset_hc (s);
2778 release_region (s->io_addr, s->io_size);
2779 cleanup_skel(s);
2780 kfree(s);
2781 return -1;
2784 s->irq = irq;
2786 if(uhci_start_usb (s) < 0) {
2787 uhci_cleanup_dev(s);
2788 return -1;
2791 //chain new uhci device into global list
2792 devs = s;
2794 pmdev = pm_register(PM_PCI_DEV, PM_PCI_ID(dev), handle_pm_event);
2795 if (pmdev)
2796 pmdev->data = s;
2798 return 0;
2801 _static int __init start_uhci (struct pci_dev *dev)
2803 int i;
2805 /* Search for the IO base address.. */
2806 for (i = 0; i < 6; i++) {
2808 unsigned int io_addr = dev->resource[i].start;
2809 unsigned int io_size =
2810 dev->resource[i].end - dev->resource[i].start + 1;
2811 if (!(dev->resource[i].flags & 1))
2812 continue;
2814 /* Is it already in use? */
2815 if (check_region (io_addr, io_size))
2816 break;
2817 /* disable legacy emulation */
2818 pci_write_config_word (dev, USBLEGSUP, USBLEGSUP_DEFAULT);
2820 return alloc_uhci(dev, dev->irq, io_addr, io_size);
2822 return -1;
2825 int __init uhci_init (void)
2827 int retval = -ENODEV;
2828 struct pci_dev *dev = NULL;
2829 u8 type;
2830 int i=0;
2832 #ifdef DEBUG_SLAB
2834 uhci_desc_kmem = kmem_cache_create("uhci_desc", sizeof(uhci_desc_t), 0, SLAB_HWCACHE_ALIGN, NULL, NULL);
2836 if(!uhci_desc_kmem) {
2837 err("kmem_cache_create for uhci_desc failed (out of memory)");
2838 return -ENOMEM;
2841 urb_priv_kmem = kmem_cache_create("urb_priv", sizeof(urb_priv_t), 0, SLAB_HWCACHE_ALIGN, NULL, NULL);
2843 if(!urb_priv_kmem) {
2844 err("kmem_cache_create for urb_priv_t failed (out of memory)");
2845 return -ENOMEM;
2847 #endif
2848 info(VERSTR);
2850 #ifdef CONFIG_USB_UHCI_HIGH_BANDWIDTH
2851 info("High bandwidth mode enabled");
2852 #endif
2853 for (;;) {
2854 dev = pci_find_class (PCI_CLASS_SERIAL_USB << 8, dev);
2855 if (!dev)
2856 break;
2858 /* Is it UHCI */
2859 pci_read_config_byte (dev, PCI_CLASS_PROG, &type);
2860 if (type != 0)
2861 continue;
2863 if (pci_enable_device (dev) < 0)
2864 continue;
2866 if(!dev->irq)
2868 err("Found UHCI device with no IRQ assigned. Check BIOS settings!");
2869 continue;
2872 /* Ok set it up */
2873 retval = start_uhci (dev);
2875 if (!retval)
2876 i++;
2879 return retval;
2882 void __exit uhci_cleanup (void)
2884 uhci_t *s;
2885 while ((s = devs)) {
2886 devs = devs->next;
2887 uhci_cleanup_dev(s);
2889 #ifdef DEBUG_SLAB
2890 if(kmem_cache_destroy(uhci_desc_kmem))
2891 err("uhci_desc_kmem remained");
2893 if(kmem_cache_destroy(urb_priv_kmem))
2894 err("urb_priv_kmem remained");
2895 #endif
2898 #ifdef MODULE
2899 int init_module (void)
2901 return uhci_init ();
2904 void cleanup_module (void)
2906 pm_unregister_all (handle_pm_event);
2907 uhci_cleanup ();
2910 MODULE_AUTHOR("Georg Acher, Deti Fliegl, Thomas Sailer, Roman Weissgaerber");
2911 MODULE_DESCRIPTION("USB Universal Host Controller Interface driver");
2912 #endif //MODULE