Import 2.3.99pre2-1
[davej-history.git] / drivers / usb / usb-uhci.c
blob5dbde295985663655b6306e1baaa12d2112bb954
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.222 2000/03/13 21:18:02 fliegl Exp $
18 #include <linux/config.h>
19 #include <linux/module.h>
20 #include <linux/pci.h>
21 #include <linux/kernel.h>
22 #include <linux/delay.h>
23 #include <linux/ioport.h>
24 #include <linux/sched.h>
25 #include <linux/malloc.h>
26 #include <linux/smp_lock.h>
27 #include <linux/errno.h>
28 #include <linux/unistd.h>
29 #include <linux/interrupt.h> /* for in_interrupt() */
30 #include <linux/init.h>
31 #include <linux/version.h>
32 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,3,44)
33 #include <linux/pm.h>
34 #endif
36 #include <asm/uaccess.h>
37 #include <asm/io.h>
38 #include <asm/irq.h>
39 #include <asm/system.h>
41 /* This enables more detailed sanity checks in submit_iso */
42 //#define ISO_SANITY_CHECK
44 /* This enables debug printks */
45 #define DEBUG
47 /* This enables all symbols to be exported, to ease debugging oopses */
48 //#define DEBUG_SYMBOLS
50 /* This enables an extra UHCI slab for memory debugging */
51 #define DEBUG_SLAB
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 if ((qh->last_used!=now) || force)
113 delete_qh(s,qh);
115 q=qh->horizontal.prev;
118 /*-------------------------------------------------------------------*/
119 #ifdef CONFIG_USB_UHCI_HIGH_BANDWIDTH
120 _static void enable_desc_loop(uhci_t *s, urb_t *urb)
122 int flags;
124 spin_lock_irqsave (&s->qh_lock, flags);
125 s->chain_end->hw.qh.head&=~UHCI_PTR_TERM;
126 mb();
127 s->loop_usage++;
128 ((urb_priv_t*)urb->hcpriv)->use_loop=1;
129 spin_unlock_irqrestore (&s->qh_lock, flags);
131 /*-------------------------------------------------------------------*/
132 _static void disable_desc_loop(uhci_t *s, urb_t *urb)
134 int flags;
136 spin_lock_irqsave (&s->qh_lock, flags);
138 if (((urb_priv_t*)urb->hcpriv)->use_loop) {
139 s->loop_usage--;
141 if (!s->loop_usage) {
142 s->chain_end->hw.qh.head|=UHCI_PTR_TERM;
143 mb();
145 ((urb_priv_t*)urb->hcpriv)->use_loop=0;
147 spin_unlock_irqrestore (&s->qh_lock, flags);
149 #endif
150 /*-------------------------------------------------------------------*/
151 _static void queue_urb_unlocked (uhci_t *s, urb_t *urb)
153 struct list_head *p=&urb->urb_list;
154 #ifdef CONFIG_USB_UHCI_HIGH_BANDWIDTH
156 int type;
157 type=usb_pipetype (urb->pipe);
159 if ((type == PIPE_BULK) || (type == PIPE_CONTROL))
160 enable_desc_loop(s, urb);
162 #endif
163 ((urb_priv_t*)urb->hcpriv)->started=jiffies;
164 list_add (p, &s->urb_list);
166 /*-------------------------------------------------------------------*/
167 _static void queue_urb (uhci_t *s, urb_t *urb)
169 unsigned long flags=0;
171 spin_lock_irqsave (&s->urb_list_lock, flags);
172 queue_urb_unlocked(s,urb);
173 spin_unlock_irqrestore (&s->urb_list_lock, flags);
175 /*-------------------------------------------------------------------*/
176 _static void dequeue_urb (uhci_t *s, urb_t *urb)
178 #ifdef CONFIG_USB_UHCI_HIGH_BANDWIDTH
179 int type;
181 type=usb_pipetype (urb->pipe);
183 if ((type == PIPE_BULK) || (type == PIPE_CONTROL))
184 disable_desc_loop(s, urb);
185 #endif
187 list_del (&urb->urb_list);
189 /*-------------------------------------------------------------------*/
190 _static int alloc_td (uhci_desc_t ** new, int flags)
192 #ifdef DEBUG_SLAB
193 *new= kmem_cache_alloc(uhci_desc_kmem, SLAB_FLAG);
194 #else
195 *new = (uhci_desc_t *) kmalloc (sizeof (uhci_desc_t), KMALLOC_FLAG);
196 #endif
197 if (!*new)
198 return -ENOMEM;
199 memset (*new, 0, sizeof (uhci_desc_t));
200 (*new)->hw.td.link = UHCI_PTR_TERM | (flags & UHCI_PTR_BITS); // last by default
201 (*new)->type = TD_TYPE;
202 mb();
203 INIT_LIST_HEAD (&(*new)->vertical);
204 INIT_LIST_HEAD (&(*new)->horizontal);
206 return 0;
208 /*-------------------------------------------------------------------*/
209 // append a qh to td.link physically, the SW linkage is not affected
210 _static void append_qh(uhci_t *s, uhci_desc_t *td, uhci_desc_t* qh, int flags)
212 unsigned long xxx;
214 spin_lock_irqsave (&s->td_lock, xxx);
216 td->hw.td.link = virt_to_bus (qh) | (flags & UHCI_PTR_DEPTH) | UHCI_PTR_QH;
218 mb();
219 spin_unlock_irqrestore (&s->td_lock, xxx);
221 /*-------------------------------------------------------------------*/
222 /* insert td at last position in td-list of qh (vertical) */
223 _static int insert_td (uhci_t *s, uhci_desc_t *qh, uhci_desc_t* new, int flags)
225 uhci_desc_t *prev;
226 unsigned long xxx;
228 spin_lock_irqsave (&s->td_lock, xxx);
230 list_add_tail (&new->vertical, &qh->vertical);
232 prev = list_entry (new->vertical.prev, uhci_desc_t, vertical);
234 if (qh == prev ) {
235 // virgin qh without any tds
236 qh->hw.qh.element = virt_to_bus (new);
238 else {
239 // already tds inserted, implicitely remove TERM bit of prev
240 prev->hw.td.link = virt_to_bus (new) | (flags & UHCI_PTR_DEPTH);
242 mb();
243 spin_unlock_irqrestore (&s->td_lock, xxx);
245 return 0;
247 /*-------------------------------------------------------------------*/
248 /* insert new_td after td (horizontal) */
249 _static int insert_td_horizontal (uhci_t *s, uhci_desc_t *td, uhci_desc_t* new)
251 uhci_desc_t *next;
252 unsigned long flags;
254 spin_lock_irqsave (&s->td_lock, flags);
256 next = list_entry (td->horizontal.next, uhci_desc_t, horizontal);
257 list_add (&new->horizontal, &td->horizontal);
258 new->hw.td.link = td->hw.td.link;
259 td->hw.td.link = virt_to_bus (new);
260 mb();
261 spin_unlock_irqrestore (&s->td_lock, flags);
263 return 0;
265 /*-------------------------------------------------------------------*/
266 _static int unlink_td (uhci_t *s, uhci_desc_t *element, int phys_unlink)
268 uhci_desc_t *next, *prev;
269 int dir = 0;
270 unsigned long flags;
272 spin_lock_irqsave (&s->td_lock, flags);
274 next = list_entry (element->vertical.next, uhci_desc_t, vertical);
276 if (next == element) {
277 dir = 1;
278 prev = list_entry (element->horizontal.prev, uhci_desc_t, horizontal);
280 else
281 prev = list_entry (element->vertical.prev, uhci_desc_t, vertical);
283 if (phys_unlink) {
284 // really remove HW linking
285 if (prev->type == TD_TYPE)
286 prev->hw.td.link = element->hw.td.link;
287 else
288 prev->hw.qh.element = element->hw.td.link;
291 mb ();
293 if (dir == 0)
294 list_del (&element->vertical);
295 else
296 list_del (&element->horizontal);
298 spin_unlock_irqrestore (&s->td_lock, flags);
300 return 0;
303 /*-------------------------------------------------------------------*/
304 _static int delete_desc (uhci_desc_t *element)
306 #ifdef DEBUG_SLAB
307 kmem_cache_free(uhci_desc_kmem, element);
308 #else
309 kfree (element);
310 #endif
311 return 0;
313 /*-------------------------------------------------------------------*/
314 // Allocates qh element
315 _static int alloc_qh (uhci_desc_t ** new)
317 #ifdef DEBUG_SLAB
318 *new= kmem_cache_alloc(uhci_desc_kmem, SLAB_FLAG);
319 #else
320 *new = (uhci_desc_t *) kmalloc (sizeof (uhci_desc_t), KMALLOC_FLAG);
321 #endif
322 if (!*new)
323 return -ENOMEM;
324 memset (*new, 0, sizeof (uhci_desc_t));
325 (*new)->hw.qh.head = UHCI_PTR_TERM;
326 (*new)->hw.qh.element = UHCI_PTR_TERM;
327 (*new)->type = QH_TYPE;
329 mb();
330 INIT_LIST_HEAD (&(*new)->horizontal);
331 INIT_LIST_HEAD (&(*new)->vertical);
333 dbg("Allocated qh @ %p", *new);
335 return 0;
337 /*-------------------------------------------------------------------*/
338 // inserts new qh before/after the qh at pos
339 // flags: 0: insert before pos, 1: insert after pos (for low speed transfers)
340 _static int insert_qh (uhci_t *s, uhci_desc_t *pos, uhci_desc_t *new, int order)
342 uhci_desc_t *old;
343 unsigned long flags;
345 spin_lock_irqsave (&s->qh_lock, flags);
347 if (!order) {
348 // (OLD) (POS) -> (OLD) (NEW) (POS)
349 old = list_entry (pos->horizontal.prev, uhci_desc_t, horizontal);
350 list_add_tail (&new->horizontal, &pos->horizontal);
351 new->hw.qh.head = MAKE_QH_ADDR (pos) ;
352 if (!(old->hw.qh.head & UHCI_PTR_TERM))
353 old->hw.qh.head = MAKE_QH_ADDR (new) ;
355 else {
356 // (POS) (OLD) -> (POS) (NEW) (OLD)
357 old = list_entry (pos->horizontal.next, uhci_desc_t, horizontal);
358 list_add (&new->horizontal, &pos->horizontal);
359 new->hw.qh.head = MAKE_QH_ADDR (old);
360 pos->hw.qh.head = MAKE_QH_ADDR (new) ;
363 mb ();
365 spin_unlock_irqrestore (&s->qh_lock, flags);
367 return 0;
370 /*-------------------------------------------------------------------*/
371 _static int unlink_qh (uhci_t *s, uhci_desc_t *element)
373 uhci_desc_t *prev;
374 unsigned long flags;
376 spin_lock_irqsave (&s->qh_lock, flags);
378 prev = list_entry (element->horizontal.prev, uhci_desc_t, horizontal);
379 prev->hw.qh.head = element->hw.qh.head;
381 list_del(&element->horizontal);
383 mb ();
384 spin_unlock_irqrestore (&s->qh_lock, flags);
386 return 0;
388 /*-------------------------------------------------------------------*/
389 _static int delete_qh (uhci_t *s, uhci_desc_t *qh)
391 uhci_desc_t *td;
392 struct list_head *p;
394 list_del (&qh->horizontal);
396 while ((p = qh->vertical.next) != &qh->vertical) {
397 td = list_entry (p, uhci_desc_t, vertical);
398 dbg("unlink td @ %p",td);
399 unlink_td (s, td, 0); // no physical unlink
400 delete_desc (td);
403 delete_desc (qh);
405 return 0;
407 /*-------------------------------------------------------------------*/
408 _static void clean_td_chain (uhci_desc_t *td)
410 struct list_head *p;
411 uhci_desc_t *td1;
413 if (!td)
414 return;
416 while ((p = td->horizontal.next) != &td->horizontal) {
417 td1 = list_entry (p, uhci_desc_t, horizontal);
418 delete_desc (td1);
421 delete_desc (td);
424 /*-------------------------------------------------------------------*/
425 _static void fill_td (uhci_desc_t *td, int status, int info, __u32 buffer)
427 td->hw.td.status = status;
428 td->hw.td.info = info;
429 td->hw.td.buffer = buffer;
431 /*-------------------------------------------------------------------*/
432 // Removes ALL qhs in chain (paranoia!)
433 _static void cleanup_skel (uhci_t *s)
435 unsigned int n;
436 uhci_desc_t *td;
438 dbg("cleanup_skel");
440 clean_descs(s,1);
442 for (n = 0; n < 8; n++) {
443 td = s->int_chain[n];
444 clean_td_chain (td);
447 if (s->iso_td) {
448 for (n = 0; n < 1024; n++) {
449 td = s->iso_td[n];
450 clean_td_chain (td);
452 kfree (s->iso_td);
455 if (s->framelist)
456 free_page ((unsigned long) s->framelist);
458 if (s->control_chain) {
459 // completed init_skel?
460 struct list_head *p;
461 uhci_desc_t *qh, *qh1;
463 qh = s->control_chain;
464 while ((p = qh->horizontal.next) != &qh->horizontal) {
465 qh1 = list_entry (p, uhci_desc_t, horizontal);
466 delete_qh (s, qh1);
469 delete_qh (s, qh);
471 else {
472 if (s->ls_control_chain)
473 delete_desc (s->ls_control_chain);
474 if (s->control_chain)
475 delete_desc(s->control_chain);
476 if (s->bulk_chain)
477 delete_desc (s->bulk_chain);
478 if (s->chain_end)
479 delete_desc (s->chain_end);
481 dbg("cleanup_skel finished");
483 /*-------------------------------------------------------------------*/
484 // allocates framelist and qh-skeletons
485 // only HW-links provide continous linking, SW-links stay in their domain (ISO/INT)
486 _static int init_skel (uhci_t *s)
488 int n, ret;
489 uhci_desc_t *qh, *td;
491 dbg("init_skel");
493 s->framelist = (__u32 *) get_free_page (GFP_KERNEL);
495 if (!s->framelist)
496 return -ENOMEM;
498 memset (s->framelist, 0, 4096);
500 dbg("allocating iso desc pointer list");
501 s->iso_td = (uhci_desc_t **) kmalloc (1024 * sizeof (uhci_desc_t*), GFP_KERNEL);
503 if (!s->iso_td)
504 goto init_skel_cleanup;
506 s->ls_control_chain = NULL;
507 s->control_chain = NULL;
508 s->bulk_chain = NULL;
509 s->chain_end = NULL;
511 dbg("allocating iso descs");
512 for (n = 0; n < 1024; n++) {
513 // allocate skeleton iso/irq-tds
514 ret = alloc_td (&td, 0);
515 if (ret)
516 goto init_skel_cleanup;
517 s->iso_td[n] = td;
518 s->framelist[n] = ((__u32) virt_to_bus (td));
521 dbg("allocating qh: chain_end");
522 ret = alloc_qh (&qh);
524 if (ret)
525 goto init_skel_cleanup;
527 s->chain_end = qh;
529 ret = alloc_td (&td, 0);
531 if (ret)
532 goto init_skel_cleanup;
534 fill_td (td, TD_CTRL_IOC, 0, 0); // generate 1ms interrupt
535 insert_td (s, qh, td, 0);
537 dbg("allocating qh: bulk_chain");
538 ret = alloc_qh (&qh);
539 if (ret)
540 goto init_skel_cleanup;
542 insert_qh (s, s->chain_end, qh, 0);
543 s->bulk_chain = qh;
545 dbg("allocating qh: control_chain");
546 ret = alloc_qh (&qh);
547 if (ret)
548 goto init_skel_cleanup;
550 insert_qh (s, s->bulk_chain, qh, 0);
551 s->control_chain = qh;
553 #ifdef CONFIG_USB_UHCI_HIGH_BANDWIDTH
554 // disabled reclamation loop
555 s->chain_end->hw.qh.head=virt_to_bus(s->control_chain) | UHCI_PTR_QH | UHCI_PTR_TERM;
556 #endif
558 dbg("allocating qh: ls_control_chain");
559 ret = alloc_qh (&qh);
560 if (ret)
561 goto init_skel_cleanup;
563 insert_qh (s, s->control_chain, qh, 0);
564 s->ls_control_chain = qh;
566 for (n = 0; n < 8; n++)
567 s->int_chain[n] = 0;
569 dbg("allocating skeleton INT-TDs");
571 for (n = 0; n < 8; n++) {
572 uhci_desc_t *td;
574 alloc_td (&td, 0);
575 if (!td)
576 goto init_skel_cleanup;
577 s->int_chain[n] = td;
578 if (n == 0) {
579 s->int_chain[0]->hw.td.link = virt_to_bus (s->ls_control_chain) | UHCI_PTR_QH;
581 else {
582 s->int_chain[n]->hw.td.link = virt_to_bus (s->int_chain[0]);
586 dbg("Linking skeleton INT-TDs");
588 for (n = 0; n < 1024; n++) {
589 // link all iso-tds to the interrupt chains
590 int m, o;
591 dbg("framelist[%i]=%x",n,s->framelist[n]);
592 if ((n&127)==127)
593 ((uhci_desc_t*) s->iso_td[n])->hw.td.link = virt_to_bus(s->int_chain[0]);
594 else
595 for (o = 1, m = 2; m <= 128; o++, m += m)
596 if ((n & (m - 1)) == ((m - 1) / 2))
597 ((uhci_desc_t*) s->iso_td[n])->hw.td.link = virt_to_bus (s->int_chain[o]);
600 mb();
601 //uhci_show_queue(s->control_chain);
602 dbg("init_skel exit");
603 return 0;
605 init_skel_cleanup:
606 cleanup_skel (s);
607 return -ENOMEM;
610 /*-------------------------------------------------------------------*/
611 // LOW LEVEL STUFF
612 // assembles QHs und TDs for control, bulk and iso
613 /*-------------------------------------------------------------------*/
614 _static int uhci_submit_control_urb (urb_t *urb)
616 uhci_desc_t *qh, *td;
617 uhci_t *s = (uhci_t*) urb->dev->bus->hcpriv;
618 urb_priv_t *urb_priv = urb->hcpriv;
619 unsigned long destination, status;
620 int maxsze = usb_maxpacket (urb->dev, urb->pipe, usb_pipeout (urb->pipe));
621 unsigned long len;
622 char *data;
623 int depth_first=USE_CTRL_DEPTH_FIRST; // UHCI descriptor chasing method
625 if (!maxsze) {
626 err("uhci_submit_control_urb: pipesize for pipe %x is zero", urb->pipe);
627 return -EINVAL;
630 dbg("uhci_submit_control start");
631 alloc_qh (&qh); // alloc qh for this request
633 if (!qh)
634 return -ENOMEM;
636 alloc_td (&td, UHCI_PTR_DEPTH * depth_first); // get td for setup stage
638 if (!td) {
639 delete_qh (s, qh);
640 return -ENOMEM;
643 /* The "pipe" thing contains the destination in bits 8--18 */
644 destination = (urb->pipe & PIPE_DEVEP_MASK) | USB_PID_SETUP;
646 /* 3 errors */
647 status = (urb->pipe & TD_CTRL_LS) | TD_CTRL_ACTIVE |
648 (urb->transfer_flags & USB_DISABLE_SPD ? 0 : TD_CTRL_SPD) | (3 << 27);
650 /* Build the TD for the control request, try forever, 8 bytes of data */
651 fill_td (td, status, destination | (7 << 21), virt_to_bus (urb->setup_packet));
653 insert_td (s, qh, td, 0); // queue 'setup stage'-td in qh
654 #if 0
656 char *sp=urb->setup_packet;
657 dbg("SETUP to pipe %x: %x %x %x %x %x %x %x %x", urb->pipe,
658 sp[0],sp[1],sp[2],sp[3],sp[4],sp[5],sp[6],sp[7]);
660 //uhci_show_td(td);
661 #endif
663 len = urb->transfer_buffer_length;
664 data = urb->transfer_buffer;
666 /* If direction is "send", change the frame from SETUP (0x2D)
667 to OUT (0xE1). Else change it from SETUP to IN (0x69). */
669 destination = (urb->pipe & PIPE_DEVEP_MASK) | (usb_pipeout (urb->pipe)?USB_PID_OUT:USB_PID_IN);
671 while (len > 0) {
672 int pktsze = len;
674 alloc_td (&td, UHCI_PTR_DEPTH * depth_first);
675 if (!td) {
676 delete_qh (s, qh);
677 return -ENOMEM;
680 if (pktsze > maxsze)
681 pktsze = maxsze;
683 destination ^= 1 << TD_TOKEN_TOGGLE; // toggle DATA0/1
685 fill_td (td, status, destination | ((pktsze - 1) << 21),
686 virt_to_bus (data)); // Status, pktsze bytes of data
688 insert_td (s, qh, td, UHCI_PTR_DEPTH * depth_first); // queue 'data stage'-td in qh
690 data += pktsze;
691 len -= pktsze;
694 /* Build the final TD for control status */
695 /* It's only IN if the pipe is out AND we aren't expecting data */
697 destination &= ~UHCI_PID;
699 if (usb_pipeout (urb->pipe) || (urb->transfer_buffer_length == 0))
700 destination |= USB_PID_IN;
701 else
702 destination |= USB_PID_OUT;
704 destination |= 1 << TD_TOKEN_TOGGLE; /* End in Data1 */
706 alloc_td (&td, UHCI_PTR_DEPTH);
708 if (!td) {
709 delete_qh (s, qh);
710 return -ENOMEM;
712 status &=~TD_CTRL_SPD;
714 /* no limit on errors on final packet , 0 bytes of data */
715 fill_td (td, status | TD_CTRL_IOC, destination | (UHCI_NULL_DATA_SIZE << 21),
718 insert_td (s, qh, td, UHCI_PTR_DEPTH * depth_first); // queue status td
720 list_add (&qh->desc_list, &urb_priv->desc_list);
722 urb->status = -EINPROGRESS;
723 queue_urb (s, urb); // queue before inserting in desc chain
725 qh->hw.qh.element &= ~UHCI_PTR_TERM;
727 //uhci_show_queue(qh);
728 /* Start it up... put low speed first */
729 if (urb->pipe & TD_CTRL_LS)
730 insert_qh (s, s->control_chain, qh, 0);
731 else
732 insert_qh (s, s->bulk_chain, qh, 0);
734 dbg("uhci_submit_control end");
735 return 0;
737 /*-------------------------------------------------------------------*/
738 // For queued bulk transfers, two additional QH helpers are allocated (nqh, bqh)
739 // Due to the linking with other bulk urbs, it has to be locked with urb_list_lock!
741 _static int uhci_submit_bulk_urb (urb_t *urb, urb_t *bulk_urb)
743 uhci_t *s = (uhci_t*) urb->dev->bus->hcpriv;
744 urb_priv_t *urb_priv = urb->hcpriv;
745 uhci_desc_t *qh, *td, *nqh, *bqh;
746 unsigned long destination, status;
747 char *data;
748 unsigned int pipe = urb->pipe;
749 int maxsze = usb_maxpacket (urb->dev, pipe, usb_pipeout (pipe));
750 int info, len;
751 int depth_first=USE_BULK_DEPTH_FIRST; // UHCI descriptor chasing method
752 urb_priv_t *upriv, *bpriv;
754 if (usb_endpoint_halted (urb->dev, usb_pipeendpoint (pipe), usb_pipeout (pipe)))
755 return -EPIPE;
757 if (urb->transfer_buffer_length < 0) {
758 err("Negative transfer length in submit_bulk");
759 return -EINVAL;
762 if (!maxsze)
763 return -EMSGSIZE;
765 queue_dbg("uhci_submit_bulk_urb: urb %p, old %p, pipe %08x, len %i",
766 urb,bulk_urb,urb->pipe,urb->transfer_buffer_length);
768 upriv=(urb_priv_t*)urb->hcpriv;
770 if (!bulk_urb) {
771 alloc_qh (&qh); // get qh for this request
773 if (!qh)
774 return -ENOMEM;
776 if (urb->transfer_flags & USB_QUEUE_BULK) {
777 alloc_qh(&nqh); // placeholder for clean unlink
778 if (!nqh) {
779 delete_desc (qh);
780 return -ENOMEM;
782 upriv->next_qh = nqh;
783 queue_dbg("new next qh %p",nqh);
786 else {
787 bpriv = (urb_priv_t*)bulk_urb->hcpriv;
788 qh = bpriv->bottom_qh; // re-use bottom qh and next qh
789 nqh = bpriv->next_qh;
790 upriv->next_qh=nqh;
791 bpriv->next_queued_urb=urb;
792 upriv->prev_queued_urb=bulk_urb;
795 queue_dbg("uhci_submit_bulk: qh=%p, nqh=%p\n",bqh,nqh);
797 if (urb->transfer_flags & USB_QUEUE_BULK) {
798 alloc_qh (&bqh); // "bottom" QH,
800 if (!bqh) {
801 if (!bulk_urb) {
802 delete_desc(qh);
803 delete_desc(nqh);
805 return -ENOMEM;
807 bqh->hw.qh.element = UHCI_PTR_TERM;
808 bqh->hw.qh.element = virt_to_bus(nqh)|UHCI_PTR_QH;
809 upriv->bottom_qh = bqh;
810 queue_dbg("uhci_submit_bulk: new bqh %p\n",bqh);
814 /* The "pipe" thing contains the destination in bits 8--18. */
815 destination = (pipe & PIPE_DEVEP_MASK) | usb_packetid (pipe);
817 /* 3 errors */
818 status = (pipe & TD_CTRL_LS) | TD_CTRL_ACTIVE |
819 ((urb->transfer_flags & USB_DISABLE_SPD) ? 0 : TD_CTRL_SPD) | (3 << 27);
821 /* Build the TDs for the bulk request */
822 len = urb->transfer_buffer_length;
823 data = urb->transfer_buffer;
825 do { // TBD: Really allow zero-length packets?
826 int pktsze = len;
828 alloc_td (&td, UHCI_PTR_DEPTH * depth_first);
830 if (!td) {
831 delete_qh (s, qh);
832 return -ENOMEM;
835 if (pktsze > maxsze)
836 pktsze = maxsze;
838 // pktsze bytes of data
839 info = destination | (((pktsze - 1)&UHCI_NULL_DATA_SIZE) << 21) |
840 (usb_gettoggle (urb->dev, usb_pipeendpoint (pipe), usb_pipeout (pipe)) << TD_TOKEN_TOGGLE);
842 fill_td (td, status, info, virt_to_bus (data));
844 data += pktsze;
845 len -= pktsze;
847 if (!len)
848 td->hw.td.status |= TD_CTRL_IOC; // last one generates INT
850 insert_td (s, qh, td, UHCI_PTR_DEPTH * depth_first);
851 usb_dotoggle (urb->dev, usb_pipeendpoint (pipe), usb_pipeout (pipe));
853 } while (len > 0);
855 list_add (&qh->desc_list, &urb_priv->desc_list);
857 if (urb->transfer_flags & USB_QUEUE_BULK) {
858 qh->hw.qh.element&=~UHCI_PTR_TERM;
859 append_qh(s, td, bqh, UHCI_PTR_DEPTH * depth_first);
862 urb->status = -EINPROGRESS;
863 queue_urb_unlocked (s, urb);
865 qh->hw.qh.element &= ~UHCI_PTR_TERM;
867 if (!bulk_urb) {
868 if (urb->transfer_flags & USB_QUEUE_BULK) {
869 spin_lock (&s->td_lock); // both QHs in one go
870 insert_qh (s, s->chain_end, qh, 0); // Main QH
871 insert_qh (s, s->chain_end, nqh, 0); // Helper QH
872 spin_unlock (&s->td_lock);
874 else
875 insert_qh (s, s->chain_end, qh, 0);
878 //uhci_show_queue(s->bulk_chain);
879 //dbg("uhci_submit_bulk_urb: exit\n");
880 return 0;
882 /*-------------------------------------------------------------------*/
883 _static void uhci_clean_iso_step1(uhci_t *s, urb_priv_t *urb_priv)
885 struct list_head *p;
886 uhci_desc_t *td;
888 for (p = urb_priv->desc_list.next; p != &urb_priv->desc_list; p = p->next) {
889 td = list_entry (p, uhci_desc_t, desc_list);
890 unlink_td (s, td, 1);
893 /*-------------------------------------------------------------------*/
894 _static void uhci_clean_iso_step2(uhci_t *s, urb_priv_t *urb_priv)
896 struct list_head *p;
897 uhci_desc_t *td;
899 while ((p = urb_priv->desc_list.next) != &urb_priv->desc_list) {
900 td = list_entry (p, uhci_desc_t, desc_list);
901 list_del (p);
902 delete_desc (td);
905 /*-------------------------------------------------------------------*/
906 // mode: 0: unlink + no deletion mark, 1: regular (unlink/delete-mark), 2: don't unlink
907 // looks a bit complicated because of all the bulk queueing goodies
909 _static void uhci_clean_transfer (uhci_t *s, urb_t *urb, uhci_desc_t *qh, int mode)
911 uhci_desc_t *bqh, *nqh, *prevqh;
912 int now;
913 urb_priv_t *priv=(urb_priv_t*)urb->hcpriv;
915 now=UHCI_GET_CURRENT_FRAME(s);
917 dbg("clean transfer urb %p, qh %p, mode %i",urb,qh,mode);
918 bqh=priv->bottom_qh;
920 if (!priv->next_queued_urb) { // no more appended bulk queues
922 if (mode != 2)
923 unlink_qh (s, qh);
925 if (priv->prev_queued_urb) {
926 urb_priv_t* ppriv=(urb_priv_t*)priv->prev_queued_urb->hcpriv;
928 ppriv->bottom_qh = priv->bottom_qh;
929 ppriv->next_queued_urb = NULL;
931 else if (bqh) { // queue dead
932 nqh=priv->next_qh;
934 if (mode != 2)
935 unlink_qh(s, nqh);
937 if (mode) {
938 nqh->last_used = bqh->last_used = now;
939 list_add_tail (&nqh->horizontal, &s->free_desc);
940 list_add_tail (&bqh->horizontal, &s->free_desc);
944 else { // there are queued urbs following
945 urb_t *nurb;
946 unsigned long flags;
948 nurb=priv->next_queued_urb;
949 spin_lock_irqsave (&s->qh_lock, flags);
951 if (!priv->prev_queued_urb) { // top
952 if (mode !=2) {
953 prevqh = list_entry (qh->horizontal.prev, uhci_desc_t, horizontal);
954 prevqh->hw.qh.head = virt_to_bus(bqh) | UHCI_PTR_QH;
955 queue_dbg ("TOP relink of %p to %p-%p",qh,prevqh,bqh);
957 list_del (&qh->horizontal);
958 list_add (&bqh->horizontal, &prevqh->horizontal);
961 else { //intermediate
962 urb_priv_t* ppriv=(urb_priv_t*)priv->prev_queued_urb->hcpriv;
963 uhci_desc_t * bnqh;
965 bnqh=list_entry (&((urb_priv_t*)(nurb->hcpriv))->desc_list.next, uhci_desc_t, desc_list);
966 ppriv->bottom_qh=bnqh;
967 ppriv->next_queued_urb=nurb;
969 if (mode!=2) {
970 prevqh = list_entry (ppriv->desc_list.next, uhci_desc_t, desc_list);
971 prevqh->hw.qh.head = virt_to_bus(bqh) | UHCI_PTR_QH;
972 queue_dbg ("IM relink of %p to %p-%p",qh,prevqh,bqh);
975 mb();
976 spin_unlock_irqrestore (&s->qh_lock, flags);
977 ((urb_priv_t*)nurb->hcpriv)->prev_queued_urb=priv->prev_queued_urb;
980 if (mode) {
981 qh->last_used = now;
982 list_add_tail (&qh->horizontal, &s->free_desc); // mark for later deletion
985 /*-------------------------------------------------------------------*/
986 // unlinks an urb by dequeuing its qh, waits some frames and forgets it
987 _static int uhci_unlink_urb_sync (uhci_t *s, urb_t *urb)
989 uhci_desc_t *qh;
990 urb_priv_t *urb_priv;
991 unsigned long flags=0;
993 spin_lock_irqsave (&s->urb_list_lock, flags);
995 if (urb->status == -EINPROGRESS) {
996 // URB probably still in work
997 dequeue_urb (s, urb);
998 s->unlink_urb_done=1;
999 spin_unlock_irqrestore (&s->urb_list_lock, flags);
1001 urb->status = -ENOENT; // mark urb as killed
1002 urb_priv = urb->hcpriv;
1004 switch (usb_pipetype (urb->pipe)) {
1005 case PIPE_ISOCHRONOUS:
1006 case PIPE_INTERRUPT:
1007 uhci_clean_iso_step1(s, urb_priv);
1008 uhci_wait_ms(1);
1009 uhci_clean_iso_step2(s, urb_priv);
1010 break;
1012 case PIPE_BULK:
1013 case PIPE_CONTROL:
1014 qh = list_entry (urb_priv->desc_list.next, uhci_desc_t, desc_list);
1015 spin_lock_irqsave (&s->urb_list_lock, flags);
1016 uhci_clean_transfer(s, urb, qh, 1);
1017 spin_unlock_irqrestore (&s->urb_list_lock, flags);
1018 uhci_wait_ms(1);
1021 #ifdef DEBUG_SLAB
1022 kmem_cache_free (urb_priv_kmem, urb->hcpriv);
1023 #else
1024 kfree (urb->hcpriv);
1025 #endif
1026 if (urb->complete) {
1027 dbg("unlink_urb: calling completion");
1028 urb->complete ((struct urb *) urb);
1030 usb_dec_dev_use (urb->dev);
1031 return 0;
1033 else
1034 spin_unlock_irqrestore (&s->urb_list_lock, flags);
1035 return 0;
1037 /*-------------------------------------------------------------------*/
1038 // async unlink_urb completion/cleanup work
1039 // has to be protected by urb_list_lock!
1040 // features: if set in transfer_flags, the resulting status of the killed
1041 // transaction is not overwritten
1043 _static void uhci_cleanup_unlink(uhci_t *s, int force)
1045 struct list_head *q;
1046 urb_t *urb;
1047 struct usb_device *dev;
1048 int pipe,now;
1049 urb_priv_t *urb_priv;
1051 q=s->urb_unlinked.next;
1052 now=UHCI_GET_CURRENT_FRAME(s);
1054 while (q != &s->urb_unlinked) {
1056 urb = list_entry (q, urb_t, urb_list);
1058 urb_priv = (urb_priv_t*)urb->hcpriv;
1059 q = urb->urb_list.next;
1061 if (force ||
1062 ((urb_priv->started != 0xffffffff) && (urb_priv->started != now))) {
1063 async_dbg("async cleanup %p",urb);
1064 switch (usb_pipetype (urb->pipe)) { // process descriptors
1065 case PIPE_CONTROL:
1066 process_transfer (s, urb, 2);
1067 break;
1068 case PIPE_BULK:
1069 if (!s->avoid_bulk.counter)
1070 process_transfer (s, urb, 2); // don't unlink (already done)
1071 else
1072 continue;
1073 break;
1074 case PIPE_ISOCHRONOUS:
1075 process_iso (s, urb, 1); // force, don't unlink
1076 break;
1077 case PIPE_INTERRUPT:
1078 process_interrupt (s, urb);
1079 break;
1082 if (!(urb->transfer_flags & USB_TIMEOUT_KILLED))
1083 urb->status = -ECONNRESET; // mark as asynchronously killed
1085 pipe = urb->pipe; // completion may destroy all...
1086 dev = urb->dev;
1087 urb_priv = urb->hcpriv;
1089 if (urb->complete) {
1090 spin_unlock(&s->urb_list_lock);
1091 urb->complete ((struct urb *) urb);
1092 spin_lock(&s->urb_list_lock);
1095 if (!(urb->transfer_flags & USB_TIMEOUT_KILLED))
1096 urb->status = -ENOENT; // now the urb is really dead
1098 usb_dec_dev_use (dev);
1099 #ifdef DEBUG_SLAB
1100 kmem_cache_free (urb_priv_kmem, urb_priv);
1101 #else
1102 kfree (urb_priv);
1103 #endif
1104 switch (usb_pipetype (pipe)) {
1105 case PIPE_ISOCHRONOUS:
1106 case PIPE_INTERRUPT:
1107 uhci_clean_iso_step2(s, urb_priv);
1108 break;
1110 list_del (&urb->urb_list);
1115 /*-------------------------------------------------------------------*/
1116 _static int uhci_unlink_urb_async (uhci_t *s,urb_t *urb)
1118 uhci_desc_t *qh;
1119 urb_priv_t *urb_priv;
1121 async_dbg("unlink_urb_async called %p",urb);
1123 if (urb->status == -EINPROGRESS) {
1124 ((urb_priv_t*)urb->hcpriv)->started = ~0;
1125 dequeue_urb (s, urb);
1126 list_add_tail (&urb->urb_list, &s->urb_unlinked); // store urb
1128 s->unlink_urb_done = 1;
1130 urb->status = -ECONNABORTED; // mark urb as "waiting to be killed"
1131 urb_priv = (urb_priv_t*)urb->hcpriv;
1133 switch (usb_pipetype (urb->pipe)) {
1134 case PIPE_ISOCHRONOUS:
1135 case PIPE_INTERRUPT:
1136 uhci_clean_iso_step1 (s, urb_priv);
1137 break;
1139 case PIPE_BULK:
1140 case PIPE_CONTROL:
1141 qh = list_entry (urb_priv->desc_list.next, uhci_desc_t, desc_list);
1142 uhci_clean_transfer (s, urb, qh, 0);
1143 break;
1145 ((urb_priv_t*)urb->hcpriv)->started = UHCI_GET_CURRENT_FRAME(s);
1148 return -EINPROGRESS;
1150 /*-------------------------------------------------------------------*/
1151 _static int uhci_unlink_urb (urb_t *urb)
1153 uhci_t *s;
1154 unsigned long flags=0;
1155 dbg("uhci_unlink_urb called for %p",urb);
1156 if (!urb || !urb->dev) // you never know...
1157 return -EINVAL;
1159 s = (uhci_t*) urb->dev->bus->hcpriv;
1161 if (usb_pipedevice (urb->pipe) == s->rh.devnum)
1162 return rh_unlink_urb (urb);
1164 if (!urb->hcpriv)
1165 return -EINVAL;
1167 if (urb->transfer_flags & USB_ASYNC_UNLINK) {
1168 int ret;
1170 spin_lock_irqsave (&s->urb_list_lock, flags);
1171 ret = uhci_unlink_urb_async(s, urb);
1172 spin_unlock_irqrestore (&s->urb_list_lock, flags);
1173 return ret;
1175 else
1176 return uhci_unlink_urb_sync(s, urb);
1178 /*-------------------------------------------------------------------*/
1179 // In case of ASAP iso transfer, search the URB-list for already queued URBs
1180 // for this EP and calculate the earliest start frame for the new
1181 // URB (easy seamless URB continuation!)
1182 _static int find_iso_limits (urb_t *urb, unsigned int *start, unsigned int *end)
1184 urb_t *u, *last_urb = NULL;
1185 uhci_t *s = (uhci_t*) urb->dev->bus->hcpriv;
1186 struct list_head *p;
1187 int ret=-1;
1188 unsigned long flags;
1190 spin_lock_irqsave (&s->urb_list_lock, flags);
1191 p=s->urb_list.prev;
1193 for (; p != &s->urb_list; p = p->prev) {
1194 u = list_entry (p, urb_t, urb_list);
1195 // look for pending URBs with identical pipe handle
1196 // works only because iso doesn't toggle the data bit!
1197 if ((urb->pipe == u->pipe) && (urb->dev == u->dev) && (u->status == -EINPROGRESS)) {
1198 if (!last_urb)
1199 *start = u->start_frame;
1200 last_urb = u;
1204 if (last_urb) {
1205 *end = (last_urb->start_frame + last_urb->number_of_packets) & 1023;
1206 ret=0;
1209 spin_unlock_irqrestore(&s->urb_list_lock, flags);
1211 return ret;
1213 /*-------------------------------------------------------------------*/
1214 // adjust start_frame according to scheduling constraints (ASAP etc)
1216 _static int iso_find_start (urb_t *urb)
1218 uhci_t *s = (uhci_t*) urb->dev->bus->hcpriv;
1219 unsigned int now;
1220 unsigned int start_limit = 0, stop_limit = 0, queued_size;
1221 int limits;
1223 now = UHCI_GET_CURRENT_FRAME (s) & 1023;
1225 if ((unsigned) urb->number_of_packets > 900)
1226 return -EFBIG;
1228 limits = find_iso_limits (urb, &start_limit, &stop_limit);
1229 queued_size = (stop_limit - start_limit) & 1023;
1231 if (urb->transfer_flags & USB_ISO_ASAP) {
1232 // first iso
1233 if (limits) {
1234 // 10ms setup should be enough //FIXME!
1235 urb->start_frame = (now + 10) & 1023;
1237 else {
1238 urb->start_frame = stop_limit; //seamless linkage
1240 if (((now - urb->start_frame) & 1023) <= (unsigned) urb->number_of_packets) {
1241 info("iso_find_start: gap in seamless isochronous scheduling");
1242 dbg("iso_find_start: now %u start_frame %u number_of_packets %u pipe 0x%08x",
1243 now, urb->start_frame, urb->number_of_packets, urb->pipe);
1244 urb->start_frame = (now + 5) & 1023; // 5ms setup should be enough //FIXME!
1248 else {
1249 urb->start_frame &= 1023;
1250 if (((now - urb->start_frame) & 1023) < (unsigned) urb->number_of_packets) {
1251 dbg("iso_find_start: now between start_frame and end");
1252 return -EAGAIN;
1256 /* check if either start_frame or start_frame+number_of_packets-1 lies between start_limit and stop_limit */
1257 if (limits)
1258 return 0;
1260 if (((urb->start_frame - start_limit) & 1023) < queued_size ||
1261 ((urb->start_frame + urb->number_of_packets - 1 - start_limit) & 1023) < queued_size) {
1262 dbg("iso_find_start: start_frame %u number_of_packets %u start_limit %u stop_limit %u",
1263 urb->start_frame, urb->number_of_packets, start_limit, stop_limit);
1264 return -EAGAIN;
1267 return 0;
1269 /*-------------------------------------------------------------------*/
1270 // submits USB interrupt (ie. polling ;-)
1271 // ASAP-flag set implicitely
1272 // if period==0, the the transfer is only done once
1274 _static int uhci_submit_int_urb (urb_t *urb)
1276 uhci_t *s = (uhci_t*) urb->dev->bus->hcpriv;
1277 urb_priv_t *urb_priv = urb->hcpriv;
1278 int nint, n, ret;
1279 uhci_desc_t *td;
1280 int status, destination;
1281 int info;
1282 unsigned int pipe = urb->pipe;
1284 if (urb->interval < 0 || urb->interval >= 256)
1285 return -EINVAL;
1287 if (urb->interval == 0)
1288 nint = 0;
1289 else {
1290 for (nint = 0, n = 1; nint <= 8; nint++, n += n) // round interval down to 2^n
1292 if (urb->interval < n) {
1293 urb->interval = n / 2;
1294 break;
1297 nint--;
1300 dbg("Rounded interval to %i, chain %i", urb->interval, nint);
1302 urb->start_frame = UHCI_GET_CURRENT_FRAME (s) & 1023; // remember start frame, just in case...
1304 urb->number_of_packets = 1;
1306 // INT allows only one packet
1307 if (urb->transfer_buffer_length > usb_maxpacket (urb->dev, pipe, usb_pipeout (pipe)))
1308 return -EINVAL;
1310 ret = alloc_td (&td, UHCI_PTR_DEPTH);
1312 if (ret)
1313 return -ENOMEM;
1315 status = (pipe & TD_CTRL_LS) | TD_CTRL_ACTIVE | TD_CTRL_IOC |
1316 (urb->transfer_flags & USB_DISABLE_SPD ? 0 : TD_CTRL_SPD) | (3 << 27);
1318 destination = (urb->pipe & PIPE_DEVEP_MASK) | usb_packetid (urb->pipe) |
1319 (((urb->transfer_buffer_length - 1) & 0x7ff) << 21);
1322 info = destination | (usb_gettoggle (urb->dev, usb_pipeendpoint (pipe), usb_pipeout (pipe)) << TD_TOKEN_TOGGLE);
1324 fill_td (td, status, info, virt_to_bus (urb->transfer_buffer));
1325 list_add_tail (&td->desc_list, &urb_priv->desc_list);
1327 urb->status = -EINPROGRESS;
1328 queue_urb (s, urb);
1330 insert_td_horizontal (s, s->int_chain[nint], td); // store in INT-TDs
1332 usb_dotoggle (urb->dev, usb_pipeendpoint (pipe), usb_pipeout (pipe));
1334 return 0;
1336 /*-------------------------------------------------------------------*/
1337 _static int uhci_submit_iso_urb (urb_t *urb)
1339 uhci_t *s = (uhci_t*) urb->dev->bus->hcpriv;
1340 urb_priv_t *urb_priv = urb->hcpriv;
1341 int pipe=urb->pipe;
1342 int maxsze = usb_maxpacket (urb->dev, pipe, usb_pipeout (pipe));
1343 int n, ret, last=0;
1344 uhci_desc_t *td, **tdm;
1345 int status, destination;
1346 unsigned long flags;
1348 __save_flags(flags);
1349 __cli(); // Disable IRQs to schedule all ISO-TDs in time
1350 ret = iso_find_start (urb); // adjusts urb->start_frame for later use
1352 if (ret)
1353 goto err;
1355 tdm = (uhci_desc_t **) kmalloc (urb->number_of_packets * sizeof (uhci_desc_t*), KMALLOC_FLAG);
1357 if (!tdm) {
1358 ret = -ENOMEM;
1359 goto err;
1362 // First try to get all TDs
1363 for (n = 0; n < urb->number_of_packets; n++) {
1364 dbg("n:%d urb->iso_frame_desc[n].length:%d", n, urb->iso_frame_desc[n].length);
1365 if (!urb->iso_frame_desc[n].length) {
1366 // allows ISO striping by setting length to zero in iso_descriptor
1367 tdm[n] = 0;
1368 continue;
1371 if(urb->iso_frame_desc[n].length > maxsze) {
1372 #ifdef ISO_SANITY_CHECK
1373 err("submit_iso: urb->iso_frame_desc[%d].length(%d)>%d",n , urb->iso_frame_desc[n].length, maxsze);
1374 tdm[n] = 0;
1375 ret=-EINVAL;
1376 goto inval;
1377 #endif
1380 ret = alloc_td (&td, UHCI_PTR_DEPTH);
1381 inval:
1382 if (ret) {
1383 int i; // Cleanup allocated TDs
1385 for (i = 0; i < n; n++)
1386 if (tdm[i])
1387 delete_desc(tdm[i]);
1388 kfree (tdm);
1389 goto err;
1391 last=n;
1392 tdm[n] = td;
1395 status = TD_CTRL_ACTIVE | TD_CTRL_IOS; //| (urb->transfer_flags&USB_DISABLE_SPD?0:TD_CTRL_SPD);
1397 destination = (urb->pipe & PIPE_DEVEP_MASK) | usb_packetid (urb->pipe);
1400 // Queue all allocated TDs
1401 for (n = 0; n < urb->number_of_packets; n++) {
1402 td = tdm[n];
1403 if (!td)
1404 continue;
1406 if (n == last)
1407 status |= TD_CTRL_IOC;
1409 fill_td (td, status, destination | (((urb->iso_frame_desc[n].length - 1) & 0x7ff) << 21),
1410 virt_to_bus (urb->transfer_buffer + urb->iso_frame_desc[n].offset));
1411 list_add_tail (&td->desc_list, &urb_priv->desc_list);
1413 if (n == last) {
1414 urb->status = -EINPROGRESS;
1415 queue_urb (s, urb);
1417 insert_td_horizontal (s, s->iso_td[(urb->start_frame + n) & 1023], td); // store in iso-tds
1418 //uhci_show_td(td);
1422 kfree (tdm);
1423 dbg("ISO-INT# %i, start %i, now %i", urb->number_of_packets, urb->start_frame, UHCI_GET_CURRENT_FRAME (s) & 1023);
1424 ret = 0;
1426 err:
1427 __restore_flags(flags);
1428 return ret;
1431 /*-------------------------------------------------------------------*/
1432 // returns: 0 (no transfer queued), urb* (this urb already queued)
1434 _static urb_t* search_dev_ep (uhci_t *s, urb_t *urb)
1436 struct list_head *p;
1437 urb_t *tmp;
1438 unsigned int mask = usb_pipecontrol(urb->pipe) ? (~USB_DIR_IN) : (~0);
1440 dbg("search_dev_ep:");
1442 p=s->urb_list.next;
1444 for (; p != &s->urb_list; p = p->next) {
1445 tmp = list_entry (p, urb_t, urb_list);
1446 dbg("urb: %p", tmp);
1447 // we can accept this urb if it is not queued at this time
1448 // or if non-iso transfer requests should be scheduled for the same device and pipe
1449 if ((!usb_pipeisoc(urb->pipe) && (tmp->dev == urb->dev) && !((tmp->pipe ^ urb->pipe) & mask)) ||
1450 (urb == tmp)) {
1451 return tmp; // found another urb already queued for processing
1455 return 0;
1457 /*-------------------------------------------------------------------*/
1458 _static int uhci_submit_urb (urb_t *urb)
1460 uhci_t *s;
1461 urb_priv_t *urb_priv;
1462 int ret = 0;
1463 unsigned long flags;
1464 urb_t *bulk_urb=NULL;
1466 if (!urb->dev || !urb->dev->bus)
1467 return -ENODEV;
1469 s = (uhci_t*) urb->dev->bus->hcpriv;
1470 //dbg("submit_urb: %p type %d",urb,usb_pipetype(urb->pipe));
1472 if (!s->running)
1473 return -ENODEV;
1475 if (usb_pipedevice (urb->pipe) == s->rh.devnum)
1476 return rh_submit_urb (urb); /* virtual root hub */
1478 usb_inc_dev_use (urb->dev);
1480 spin_lock_irqsave (&s->urb_list_lock, flags);
1482 bulk_urb = search_dev_ep (s, urb);
1484 if (bulk_urb) {
1486 queue_dbg("found bulk urb %p\n",bulk_urb);
1488 if ((usb_pipetype (urb->pipe) != PIPE_BULK) ||
1489 ((usb_pipetype (urb->pipe) == PIPE_BULK) &&
1490 (!(urb->transfer_flags & USB_QUEUE_BULK) || !(bulk_urb->transfer_flags & USB_QUEUE_BULK)))) {
1491 spin_unlock_irqrestore (&s->urb_list_lock, flags);
1492 usb_dec_dev_use (urb->dev);
1493 err("ENXIO1 %08x, flags %x, urb %p, burb %p",urb->pipe,urb->transfer_flags,urb,bulk_urb);
1494 return -ENXIO; // urb already queued
1498 #ifdef DEBUG_SLAB
1499 urb_priv = kmem_cache_alloc(urb_priv_kmem, SLAB_FLAG);
1500 #else
1501 urb_priv = kmalloc (sizeof (urb_priv_t), KMALLOC_FLAG);
1502 #endif
1503 if (!urb_priv) {
1504 usb_dec_dev_use (urb->dev);
1505 spin_unlock_irqrestore (&s->urb_list_lock, flags);
1506 return -ENOMEM;
1509 urb->hcpriv = urb_priv;
1510 INIT_LIST_HEAD (&urb_priv->desc_list);
1511 urb_priv->short_control_packet = 0;
1512 dbg("submit_urb: scheduling %p", urb);
1513 urb_priv->next_queued_urb = NULL;
1514 urb_priv->prev_queued_urb = NULL;
1515 urb_priv->bottom_qh = NULL;
1516 urb_priv->next_qh = NULL;
1518 if (usb_pipetype (urb->pipe) == PIPE_BULK) {
1520 if (bulk_urb) {
1521 while (((urb_priv_t*)bulk_urb->hcpriv)->next_queued_urb) // find last queued bulk
1522 bulk_urb=((urb_priv_t*)bulk_urb->hcpriv)->next_queued_urb;
1524 ((urb_priv_t*)bulk_urb->hcpriv)->next_queued_urb=urb;
1526 atomic_inc (&s->avoid_bulk);
1527 ret = uhci_submit_bulk_urb (urb, bulk_urb);
1528 atomic_dec (&s->avoid_bulk);
1529 spin_unlock_irqrestore (&s->urb_list_lock, flags);
1531 else {
1532 spin_unlock_irqrestore (&s->urb_list_lock, flags);
1533 switch (usb_pipetype (urb->pipe)) {
1534 case PIPE_ISOCHRONOUS:
1535 ret = uhci_submit_iso_urb (urb);
1536 break;
1537 case PIPE_INTERRUPT:
1538 ret = uhci_submit_int_urb (urb);
1539 break;
1540 case PIPE_CONTROL:
1541 ret = uhci_submit_control_urb (urb);
1542 break;
1543 default:
1544 ret = -EINVAL;
1548 dbg("submit_urb: scheduled with ret: %d", ret);
1550 if (ret != 0) {
1551 usb_dec_dev_use (urb->dev);
1552 #ifdef DEBUG_SLAB
1553 kmem_cache_free(urb_priv_kmem, urb_priv);
1554 #else
1555 kfree (urb_priv);
1556 #endif
1557 return ret;
1560 return 0;
1563 // Checks for URB timeout and removes bandwidth reclamation
1564 // if URB idles too long
1565 _static void uhci_check_timeouts(uhci_t *s)
1567 struct list_head *p,*p2;
1568 urb_t *urb;
1569 int type;
1571 p = s->urb_list.prev;
1573 while (p != &s->urb_list) {
1574 urb_priv_t *hcpriv;
1576 p2 = p;
1577 p = p->prev;
1578 urb = list_entry (p2, urb_t, urb_list);
1579 type = usb_pipetype (urb->pipe);
1581 hcpriv = (urb_priv_t*)urb->hcpriv;
1583 if ( urb->timeout &&
1584 ((hcpriv->started + urb->timeout) < jiffies)) {
1585 urb->transfer_flags |= USB_TIMEOUT_KILLED | USB_ASYNC_UNLINK;
1586 async_dbg("uhci_check_timeout: timeout for %p",urb);
1587 uhci_unlink_urb_async(s, urb);
1589 #ifdef CONFIG_USB_UHCI_HIGH_BANDWIDTH
1590 else if (((type == PIPE_BULK) || (type == PIPE_CONTROL)) &&
1591 (hcpriv->use_loop) &&
1592 ((hcpriv->started + IDLE_TIMEOUT) < jiffies))
1593 disable_desc_loop(s, urb);
1594 #endif
1599 /*-------------------------------------------------------------------
1600 Virtual Root Hub
1601 -------------------------------------------------------------------*/
1603 _static __u8 root_hub_dev_des[] =
1605 0x12, /* __u8 bLength; */
1606 0x01, /* __u8 bDescriptorType; Device */
1607 0x00, /* __u16 bcdUSB; v1.0 */
1608 0x01,
1609 0x09, /* __u8 bDeviceClass; HUB_CLASSCODE */
1610 0x00, /* __u8 bDeviceSubClass; */
1611 0x00, /* __u8 bDeviceProtocol; */
1612 0x08, /* __u8 bMaxPacketSize0; 8 Bytes */
1613 0x00, /* __u16 idVendor; */
1614 0x00,
1615 0x00, /* __u16 idProduct; */
1616 0x00,
1617 0x00, /* __u16 bcdDevice; */
1618 0x00,
1619 0x00, /* __u8 iManufacturer; */
1620 0x00, /* __u8 iProduct; */
1621 0x00, /* __u8 iSerialNumber; */
1622 0x01 /* __u8 bNumConfigurations; */
1626 /* Configuration descriptor */
1627 _static __u8 root_hub_config_des[] =
1629 0x09, /* __u8 bLength; */
1630 0x02, /* __u8 bDescriptorType; Configuration */
1631 0x19, /* __u16 wTotalLength; */
1632 0x00,
1633 0x01, /* __u8 bNumInterfaces; */
1634 0x01, /* __u8 bConfigurationValue; */
1635 0x00, /* __u8 iConfiguration; */
1636 0x40, /* __u8 bmAttributes;
1637 Bit 7: Bus-powered, 6: Self-powered, 5 Remote-wakwup, 4..0: resvd */
1638 0x00, /* __u8 MaxPower; */
1640 /* interface */
1641 0x09, /* __u8 if_bLength; */
1642 0x04, /* __u8 if_bDescriptorType; Interface */
1643 0x00, /* __u8 if_bInterfaceNumber; */
1644 0x00, /* __u8 if_bAlternateSetting; */
1645 0x01, /* __u8 if_bNumEndpoints; */
1646 0x09, /* __u8 if_bInterfaceClass; HUB_CLASSCODE */
1647 0x00, /* __u8 if_bInterfaceSubClass; */
1648 0x00, /* __u8 if_bInterfaceProtocol; */
1649 0x00, /* __u8 if_iInterface; */
1651 /* endpoint */
1652 0x07, /* __u8 ep_bLength; */
1653 0x05, /* __u8 ep_bDescriptorType; Endpoint */
1654 0x81, /* __u8 ep_bEndpointAddress; IN Endpoint 1 */
1655 0x03, /* __u8 ep_bmAttributes; Interrupt */
1656 0x08, /* __u16 ep_wMaxPacketSize; 8 Bytes */
1657 0x00,
1658 0xff /* __u8 ep_bInterval; 255 ms */
1662 _static __u8 root_hub_hub_des[] =
1664 0x09, /* __u8 bLength; */
1665 0x29, /* __u8 bDescriptorType; Hub-descriptor */
1666 0x02, /* __u8 bNbrPorts; */
1667 0x00, /* __u16 wHubCharacteristics; */
1668 0x00,
1669 0x01, /* __u8 bPwrOn2pwrGood; 2ms */
1670 0x00, /* __u8 bHubContrCurrent; 0 mA */
1671 0x00, /* __u8 DeviceRemovable; *** 7 Ports max *** */
1672 0xff /* __u8 PortPwrCtrlMask; *** 7 ports max *** */
1675 /*-------------------------------------------------------------------------*/
1676 /* prepare Interrupt pipe transaction data; HUB INTERRUPT ENDPOINT */
1677 _static int rh_send_irq (urb_t *urb)
1679 int len = 1;
1680 int i;
1681 uhci_t *uhci = urb->dev->bus->hcpriv;
1682 unsigned int io_addr = uhci->io_addr;
1683 __u16 data = 0;
1685 for (i = 0; i < uhci->rh.numports; i++) {
1686 data |= ((inw (io_addr + USBPORTSC1 + i * 2) & 0xa) > 0 ? (1 << (i + 1)) : 0);
1687 len = (i + 1) / 8 + 1;
1690 *(__u16 *) urb->transfer_buffer = cpu_to_le16 (data);
1691 urb->actual_length = len;
1692 urb->status = 0;
1694 if ((data > 0) && (uhci->rh.send != 0)) {
1695 dbg("Root-Hub INT complete: port1: %x port2: %x data: %x",
1696 inw (io_addr + USBPORTSC1), inw (io_addr + USBPORTSC2), data);
1697 urb->complete (urb);
1699 return 0;
1702 /*-------------------------------------------------------------------------*/
1703 /* Virtual Root Hub INTs are polled by this timer every "intervall" ms */
1704 _static int rh_init_int_timer (urb_t *urb);
1706 _static void rh_int_timer_do (unsigned long ptr)
1708 int len;
1709 urb_t *urb = (urb_t*) ptr;
1710 uhci_t *uhci = urb->dev->bus->hcpriv;
1712 if (uhci->rh.send) {
1713 len = rh_send_irq (urb);
1714 if (len > 0) {
1715 urb->actual_length = len;
1716 if (urb->complete)
1717 urb->complete (urb);
1720 rh_init_int_timer (urb);
1723 /*-------------------------------------------------------------------------*/
1724 /* Root Hub INTs are polled by this timer, polling interval 20ms */
1725 /* This time is also used for URB-timeout checking */
1727 _static int rh_init_int_timer (urb_t *urb)
1729 uhci_t *uhci = urb->dev->bus->hcpriv;
1731 uhci->rh.interval = urb->interval;
1732 init_timer (&uhci->rh.rh_int_timer);
1733 uhci->rh.rh_int_timer.function = rh_int_timer_do;
1734 uhci->rh.rh_int_timer.data = (unsigned long) urb;
1735 uhci->rh.rh_int_timer.expires = jiffies + (HZ * 20) / 1000;
1736 add_timer (&uhci->rh.rh_int_timer);
1738 return 0;
1741 /*-------------------------------------------------------------------------*/
1742 #define OK(x) len = (x); break
1744 #define CLR_RH_PORTSTAT(x) \
1745 status = inw(io_addr+USBPORTSC1+2*(wIndex-1)); \
1746 status = (status & 0xfff5) & ~(x); \
1747 outw(status, io_addr+USBPORTSC1+2*(wIndex-1))
1749 #define SET_RH_PORTSTAT(x) \
1750 status = inw(io_addr+USBPORTSC1+2*(wIndex-1)); \
1751 status = (status & 0xfff5) | (x); \
1752 outw(status, io_addr+USBPORTSC1+2*(wIndex-1))
1755 /*-------------------------------------------------------------------------*/
1756 /****
1757 ** Root Hub Control Pipe
1758 *************************/
1761 _static int rh_submit_urb (urb_t *urb)
1763 struct usb_device *usb_dev = urb->dev;
1764 uhci_t *uhci = usb_dev->bus->hcpriv;
1765 unsigned int pipe = urb->pipe;
1766 devrequest *cmd = (devrequest *) urb->setup_packet;
1767 void *data = urb->transfer_buffer;
1768 int leni = urb->transfer_buffer_length;
1769 int len = 0;
1770 int status = 0;
1771 int stat = 0;
1772 int i;
1773 unsigned int io_addr = uhci->io_addr;
1774 __u16 cstatus;
1776 __u16 bmRType_bReq;
1777 __u16 wValue;
1778 __u16 wIndex;
1779 __u16 wLength;
1781 if (usb_pipetype (pipe) == PIPE_INTERRUPT) {
1782 dbg("Root-Hub submit IRQ: every %d ms", urb->interval);
1783 uhci->rh.urb = urb;
1784 uhci->rh.send = 1;
1785 uhci->rh.interval = urb->interval;
1786 rh_init_int_timer (urb);
1788 return 0;
1792 bmRType_bReq = cmd->requesttype | cmd->request << 8;
1793 wValue = le16_to_cpu (cmd->value);
1794 wIndex = le16_to_cpu (cmd->index);
1795 wLength = le16_to_cpu (cmd->length);
1797 for (i = 0; i < 8; i++)
1798 uhci->rh.c_p_r[i] = 0;
1800 dbg("Root-Hub: adr: %2x cmd(%1x): %04x %04x %04x %04x",
1801 uhci->rh.devnum, 8, bmRType_bReq, wValue, wIndex, wLength);
1803 switch (bmRType_bReq) {
1804 /* Request Destination:
1805 without flags: Device,
1806 RH_INTERFACE: interface,
1807 RH_ENDPOINT: endpoint,
1808 RH_CLASS means HUB here,
1809 RH_OTHER | RH_CLASS almost ever means HUB_PORT here
1812 case RH_GET_STATUS:
1813 *(__u16 *) data = cpu_to_le16 (1);
1814 OK (2);
1815 case RH_GET_STATUS | RH_INTERFACE:
1816 *(__u16 *) data = cpu_to_le16 (0);
1817 OK (2);
1818 case RH_GET_STATUS | RH_ENDPOINT:
1819 *(__u16 *) data = cpu_to_le16 (0);
1820 OK (2);
1821 case RH_GET_STATUS | RH_CLASS:
1822 *(__u32 *) data = cpu_to_le32 (0);
1823 OK (4); /* hub power ** */
1824 case RH_GET_STATUS | RH_OTHER | RH_CLASS:
1825 status = inw (io_addr + USBPORTSC1 + 2 * (wIndex - 1));
1826 cstatus = ((status & USBPORTSC_CSC) >> (1 - 0)) |
1827 ((status & USBPORTSC_PEC) >> (3 - 1)) |
1828 (uhci->rh.c_p_r[wIndex - 1] << (0 + 4));
1829 status = (status & USBPORTSC_CCS) |
1830 ((status & USBPORTSC_PE) >> (2 - 1)) |
1831 ((status & USBPORTSC_SUSP) >> (12 - 2)) |
1832 ((status & USBPORTSC_PR) >> (9 - 4)) |
1833 (1 << 8) | /* power on ** */
1834 ((status & USBPORTSC_LSDA) << (-8 + 9));
1836 *(__u16 *) data = cpu_to_le16 (status);
1837 *(__u16 *) (data + 2) = cpu_to_le16 (cstatus);
1838 OK (4);
1840 case RH_CLEAR_FEATURE | RH_ENDPOINT:
1841 switch (wValue) {
1842 case (RH_ENDPOINT_STALL):
1843 OK (0);
1845 break;
1847 case RH_CLEAR_FEATURE | RH_CLASS:
1848 switch (wValue) {
1849 case (RH_C_HUB_OVER_CURRENT):
1850 OK (0); /* hub power over current ** */
1852 break;
1854 case RH_CLEAR_FEATURE | RH_OTHER | RH_CLASS:
1855 switch (wValue) {
1856 case (RH_PORT_ENABLE):
1857 CLR_RH_PORTSTAT (USBPORTSC_PE);
1858 OK (0);
1859 case (RH_PORT_SUSPEND):
1860 CLR_RH_PORTSTAT (USBPORTSC_SUSP);
1861 OK (0);
1862 case (RH_PORT_POWER):
1863 OK (0); /* port power ** */
1864 case (RH_C_PORT_CONNECTION):
1865 SET_RH_PORTSTAT (USBPORTSC_CSC);
1866 OK (0);
1867 case (RH_C_PORT_ENABLE):
1868 SET_RH_PORTSTAT (USBPORTSC_PEC);
1869 OK (0);
1870 case (RH_C_PORT_SUSPEND):
1871 /*** WR_RH_PORTSTAT(RH_PS_PSSC); */
1872 OK (0);
1873 case (RH_C_PORT_OVER_CURRENT):
1874 OK (0); /* port power over current ** */
1875 case (RH_C_PORT_RESET):
1876 uhci->rh.c_p_r[wIndex - 1] = 0;
1877 OK (0);
1879 break;
1881 case RH_SET_FEATURE | RH_OTHER | RH_CLASS:
1882 switch (wValue) {
1883 case (RH_PORT_SUSPEND):
1884 SET_RH_PORTSTAT (USBPORTSC_SUSP);
1885 OK (0);
1886 case (RH_PORT_RESET):
1887 SET_RH_PORTSTAT (USBPORTSC_PR);
1888 uhci_wait_ms (10);
1889 uhci->rh.c_p_r[wIndex - 1] = 1;
1890 CLR_RH_PORTSTAT (USBPORTSC_PR);
1891 udelay (10);
1892 SET_RH_PORTSTAT (USBPORTSC_PE);
1893 uhci_wait_ms (10);
1894 SET_RH_PORTSTAT (0xa);
1895 OK (0);
1896 case (RH_PORT_POWER):
1897 OK (0); /* port power ** */
1898 case (RH_PORT_ENABLE):
1899 SET_RH_PORTSTAT (USBPORTSC_PE);
1900 OK (0);
1902 break;
1904 case RH_SET_ADDRESS:
1905 uhci->rh.devnum = wValue;
1906 OK (0);
1908 case RH_GET_DESCRIPTOR:
1909 switch ((wValue & 0xff00) >> 8) {
1910 case (0x01): /* device descriptor */
1911 len = min (leni, min (sizeof (root_hub_dev_des), wLength));
1912 memcpy (data, root_hub_dev_des, len);
1913 OK (len);
1914 case (0x02): /* configuration descriptor */
1915 len = min (leni, min (sizeof (root_hub_config_des), wLength));
1916 memcpy (data, root_hub_config_des, len);
1917 OK (len);
1918 case (0x03): /*string descriptors */
1919 stat = -EPIPE;
1921 break;
1923 case RH_GET_DESCRIPTOR | RH_CLASS:
1924 root_hub_hub_des[2] = uhci->rh.numports;
1925 len = min (leni, min (sizeof (root_hub_hub_des), wLength));
1926 memcpy (data, root_hub_hub_des, len);
1927 OK (len);
1929 case RH_GET_CONFIGURATION:
1930 *(__u8 *) data = 0x01;
1931 OK (1);
1933 case RH_SET_CONFIGURATION:
1934 OK (0);
1935 default:
1936 stat = -EPIPE;
1939 dbg("Root-Hub stat port1: %x port2: %x",
1940 inw (io_addr + USBPORTSC1), inw (io_addr + USBPORTSC2));
1942 urb->actual_length = len;
1943 urb->status = stat;
1944 if (urb->complete)
1945 urb->complete (urb);
1946 return 0;
1948 /*-------------------------------------------------------------------------*/
1950 _static int rh_unlink_urb (urb_t *urb)
1952 uhci_t *uhci = urb->dev->bus->hcpriv;
1954 if (uhci->rh.urb==urb) {
1955 dbg("Root-Hub unlink IRQ");
1956 uhci->rh.send = 0;
1957 del_timer (&uhci->rh.rh_int_timer);
1959 return 0;
1961 /*-------------------------------------------------------------------*/
1964 * Map status to standard result codes
1966 * <status> is (td->status & 0xFE0000) [a.k.a. uhci_status_bits(td->status)
1967 * <dir_out> is True for output TDs and False for input TDs.
1969 _static int uhci_map_status (int status, int dir_out)
1971 if (!status)
1972 return 0;
1973 if (status & TD_CTRL_BITSTUFF) /* Bitstuff error */
1974 return -EPROTO;
1975 if (status & TD_CTRL_CRCTIMEO) { /* CRC/Timeout */
1976 if (dir_out)
1977 return -ETIMEDOUT;
1978 else
1979 return -EILSEQ;
1981 if (status & TD_CTRL_NAK) /* NAK */
1982 return -ETIMEDOUT;
1983 if (status & TD_CTRL_BABBLE) /* Babble */
1984 return -EPIPE;
1985 if (status & TD_CTRL_DBUFERR) /* Buffer error */
1986 return -ENOSR;
1987 if (status & TD_CTRL_STALLED) /* Stalled */
1988 return -EPIPE;
1989 if (status & TD_CTRL_ACTIVE) /* Active */
1990 return 0;
1992 return -EPROTO;
1996 * Only the USB core should call uhci_alloc_dev and uhci_free_dev
1998 _static int uhci_alloc_dev (struct usb_device *usb_dev)
2000 return 0;
2003 _static void uhci_unlink_urbs(uhci_t *s, struct usb_device *usb_dev, int remove_all)
2005 unsigned long flags;
2006 struct list_head *p;
2007 struct list_head *p2;
2008 urb_t *urb;
2010 spin_lock_irqsave (&s->urb_list_lock, flags);
2011 p = s->urb_list.prev;
2012 while (p != &s->urb_list) {
2013 p2 = p;
2014 p = p->prev ;
2015 urb = list_entry (p2, urb_t, urb_list);
2016 dbg("urb: %p, dev %p, %p", urb, usb_dev,urb->dev);
2018 //urb->transfer_flags |=USB_ASYNC_UNLINK;
2020 if (remove_all || (usb_dev == urb->dev)) {
2021 spin_unlock_irqrestore (&s->urb_list_lock, flags);
2022 warn("forced removing of queued URB %p due to disconnect",urb);
2023 uhci_unlink_urb(urb);
2024 urb->dev = NULL; // avoid further processing of this UR
2025 spin_lock_irqsave (&s->urb_list_lock, flags);
2026 p = s->urb_list.prev;
2029 spin_unlock_irqrestore (&s->urb_list_lock, flags);
2032 _static int uhci_free_dev (struct usb_device *usb_dev)
2034 uhci_t *s;
2037 if(!usb_dev || !usb_dev->bus || !usb_dev->bus->hcpriv)
2038 return -EINVAL;
2040 s=(uhci_t*) usb_dev->bus->hcpriv;
2041 uhci_unlink_urbs(s, usb_dev, 0);
2043 return 0;
2047 * uhci_get_current_frame_number()
2049 * returns the current frame number for a USB bus/controller.
2051 _static int uhci_get_current_frame_number (struct usb_device *usb_dev)
2053 return UHCI_GET_CURRENT_FRAME ((uhci_t*) usb_dev->bus->hcpriv);
2056 struct usb_operations uhci_device_operations =
2058 uhci_alloc_dev,
2059 uhci_free_dev,
2060 uhci_get_current_frame_number,
2061 uhci_submit_urb,
2062 uhci_unlink_urb
2066 * For IN-control transfers, process_transfer gets a bit more complicated,
2067 * since there are devices that return less data (eg. strings) than they
2068 * have announced. This leads to a queue abort due to the short packet,
2069 * the status stage is not executed. If this happens, the status stage
2070 * is manually re-executed.
2071 * mode: 0: QHs already unlinked
2074 _static int process_transfer (uhci_t *s, urb_t *urb, int mode)
2076 int ret = 0;
2077 urb_priv_t *urb_priv = urb->hcpriv;
2078 struct list_head *qhl = urb_priv->desc_list.next;
2079 uhci_desc_t *qh = list_entry (qhl, uhci_desc_t, desc_list);
2080 struct list_head *p = qh->vertical.next;
2081 uhci_desc_t *desc= list_entry (urb_priv->desc_list.prev, uhci_desc_t, desc_list);
2082 uhci_desc_t *last_desc = list_entry (desc->vertical.prev, uhci_desc_t, vertical);
2083 int data_toggle = usb_gettoggle (urb->dev, usb_pipeendpoint (urb->pipe), usb_pipeout (urb->pipe)); // save initial data_toggle
2084 int maxlength; // extracted and remapped info from TD
2085 int actual_length;
2086 int status = 0;
2088 //dbg("process_transfer: urb contains bulk/control request");
2090 /* if the status phase has been retriggered and the
2091 queue is empty or the last status-TD is inactive, the retriggered
2092 status stage is completed
2095 if (urb_priv->short_control_packet &&
2096 ((qh->hw.qh.element == UHCI_PTR_TERM) ||(!(last_desc->hw.td.status & TD_CTRL_ACTIVE))))
2097 goto transfer_finished;
2099 urb->actual_length=0;
2101 for (; p != &qh->vertical; p = p->next) {
2102 desc = list_entry (p, uhci_desc_t, vertical);
2104 if (desc->hw.td.status & TD_CTRL_ACTIVE) // do not process active TDs
2105 return ret;
2107 actual_length = (desc->hw.td.status + 1) & 0x7ff; // extract transfer parameters from TD
2108 maxlength = (((desc->hw.td.info >> 21) & 0x7ff) + 1) & 0x7ff;
2109 status = uhci_map_status (uhci_status_bits (desc->hw.td.status), usb_pipeout (urb->pipe));
2111 if (status == -EPIPE) { // see if EP is stalled
2112 // set up stalled condition
2113 usb_endpoint_halt (urb->dev, usb_pipeendpoint (urb->pipe), usb_pipeout (urb->pipe));
2116 if (status != 0) { // if any error occured stop processing of further TDs
2117 // only set ret if status returned an error
2118 if (status != -EPIPE)
2119 uhci_show_td (desc);
2120 ret = status;
2121 urb->error_count++;
2122 break;
2124 else if ((desc->hw.td.info & 0xff) != USB_PID_SETUP)
2125 urb->actual_length += actual_length;
2127 // got less data than requested
2128 if ( (actual_length < maxlength)) {
2129 if (urb->transfer_flags & USB_DISABLE_SPD) {
2130 status = -EREMOTEIO; // treat as real error
2131 dbg("process_transfer: SPD!!");
2132 break; // exit after this TD because SP was detected
2135 // short read during control-IN: re-start status stage
2136 if ((usb_pipetype (urb->pipe) == PIPE_CONTROL)) {
2137 if (uhci_packetid(last_desc->hw.td.info) == USB_PID_OUT) {
2139 qh->hw.qh.element = virt_to_bus (last_desc); // re-trigger status stage
2140 dbg("short packet during control transfer, retrigger status stage @ %p",last_desc);
2141 //uhci_show_td (desc);
2142 //uhci_show_td (last_desc);
2143 urb_priv->short_control_packet=1;
2144 return 0;
2147 // all other cases: short read is OK
2148 data_toggle = uhci_toggle (desc->hw.td.info);
2149 break;
2152 data_toggle = uhci_toggle (desc->hw.td.info);
2153 queue_dbg("process_transfer: len:%d status:%x mapped:%x toggle:%d", actual_length, desc->hw.td.status,status, data_toggle);
2157 usb_settoggle (urb->dev, usb_pipeendpoint (urb->pipe), usb_pipeout (urb->pipe), !data_toggle);
2159 transfer_finished:
2161 uhci_clean_transfer(s, urb, qh, (mode==0?2:1));
2163 urb->status = status;
2165 #ifdef CONFIG_USB_UHCI_HIGH_BANDWIDTH
2166 disable_desc_loop(s,urb);
2167 #endif
2169 queue_dbg("process_transfer: (end) urb %p, wanted len %d, len %d status %x err %d",
2170 urb,urb->transfer_buffer_length,urb->actual_length, urb->status, urb->error_count);
2171 return ret;
2174 _static int process_interrupt (uhci_t *s, urb_t *urb)
2176 int i, ret = -EINPROGRESS;
2177 urb_priv_t *urb_priv = urb->hcpriv;
2178 struct list_head *p = urb_priv->desc_list.next;
2179 uhci_desc_t *desc = list_entry (urb_priv->desc_list.prev, uhci_desc_t, desc_list);
2181 int actual_length;
2182 int status = 0;
2184 //dbg("urb contains interrupt request");
2186 for (i = 0; p != &urb_priv->desc_list; p = p->next, i++) // Maybe we allow more than one TD later ;-)
2188 desc = list_entry (p, uhci_desc_t, desc_list);
2190 if (desc->hw.td.status & TD_CTRL_ACTIVE) {
2191 // do not process active TDs
2192 //dbg("TD ACT Status @%p %08x",desc,desc->hw.td.status);
2193 break;
2196 if (!desc->hw.td.status & TD_CTRL_IOC) {
2197 // do not process one-shot TDs, no recycling
2198 break;
2200 // extract transfer parameters from TD
2202 actual_length = (desc->hw.td.status + 1) & 0x7ff;
2203 status = uhci_map_status (uhci_status_bits (desc->hw.td.status), usb_pipeout (urb->pipe));
2205 // see if EP is stalled
2206 if (status == -EPIPE) {
2207 // set up stalled condition
2208 usb_endpoint_halt (urb->dev, usb_pipeendpoint (urb->pipe), usb_pipeout (urb->pipe));
2211 // if any error occured: ignore this td, and continue
2212 if (status != 0) {
2213 //uhci_show_td (desc);
2214 urb->error_count++;
2215 goto recycle;
2217 else
2218 urb->actual_length = actual_length;
2220 recycle:
2221 if (urb->complete) {
2222 //dbg("process_interrupt: calling completion, status %i",status);
2223 urb->status = status;
2225 spin_unlock(&s->urb_list_lock);
2227 urb->complete ((struct urb *) urb);
2229 spin_lock(&s->urb_list_lock);
2231 urb->status = -EINPROGRESS;
2234 // Recycle INT-TD if interval!=0, else mark TD as one-shot
2235 if (urb->interval) {
2237 desc->hw.td.info &= ~(1 << TD_TOKEN_TOGGLE);
2238 if (status==0) {
2239 ((urb_priv_t*)urb->hcpriv)->started=jiffies;
2240 desc->hw.td.info |= (usb_gettoggle (urb->dev, usb_pipeendpoint (urb->pipe),
2241 usb_pipeout (urb->pipe)) << TD_TOKEN_TOGGLE);
2242 usb_dotoggle (urb->dev, usb_pipeendpoint (urb->pipe), usb_pipeout (urb->pipe));
2243 } else {
2244 desc->hw.td.info |= (!usb_gettoggle (urb->dev, usb_pipeendpoint (urb->pipe),
2245 usb_pipeout (urb->pipe)) << TD_TOKEN_TOGGLE);
2247 desc->hw.td.status= (urb->pipe & TD_CTRL_LS) | TD_CTRL_ACTIVE | TD_CTRL_IOC |
2248 (urb->transfer_flags & USB_DISABLE_SPD ? 0 : TD_CTRL_SPD) | (3 << 27);
2249 mb();
2251 else {
2252 desc->hw.td.status &= ~TD_CTRL_IOC; // inactivate TD
2256 return ret;
2259 // mode: 1: force processing, don't unlink tds (already unlinked)
2260 _static int process_iso (uhci_t *s, urb_t *urb, int mode)
2262 int i;
2263 int ret = 0;
2264 urb_priv_t *urb_priv = urb->hcpriv;
2265 struct list_head *p = urb_priv->desc_list.next;
2266 uhci_desc_t *desc = list_entry (urb_priv->desc_list.prev, uhci_desc_t, desc_list);
2268 dbg("urb contains iso request");
2269 if ((desc->hw.td.status & TD_CTRL_ACTIVE) && !mode)
2270 return -EXDEV; // last TD not finished
2272 urb->error_count = 0;
2273 urb->actual_length = 0;
2274 urb->status = 0;
2275 dbg("process iso urb %p, %li, %i, %i, %i %08x",urb,jiffies,UHCI_GET_CURRENT_FRAME(s),
2276 urb->number_of_packets,mode,desc->hw.td.status);
2278 for (i = 0; p != &urb_priv->desc_list; p = p->next, i++) {
2279 desc = list_entry (p, uhci_desc_t, desc_list);
2281 //uhci_show_td(desc);
2282 if (desc->hw.td.status & TD_CTRL_ACTIVE) {
2283 // means we have completed the last TD, but not the TDs before
2284 desc->hw.td.status &= ~TD_CTRL_ACTIVE;
2285 dbg("TD still active (%x)- grrr. paranoia!", desc->hw.td.status);
2286 ret = -EXDEV;
2287 urb->iso_frame_desc[i].status = ret;
2288 unlink_td (s, desc, 1);
2289 // FIXME: immediate deletion may be dangerous
2290 goto err;
2293 if (!mode)
2294 unlink_td (s, desc, 1);
2296 if (urb->number_of_packets <= i) {
2297 dbg("urb->number_of_packets (%d)<=(%d)", urb->number_of_packets, i);
2298 ret = -EINVAL;
2299 goto err;
2302 if (urb->iso_frame_desc[i].offset + urb->transfer_buffer != bus_to_virt (desc->hw.td.buffer)) {
2303 // Hm, something really weird is going on
2304 dbg("Pointer Paranoia: %p!=%p", urb->iso_frame_desc[i].offset + urb->transfer_buffer, bus_to_virt (desc->hw.td.buffer));
2305 ret = -EINVAL;
2306 urb->iso_frame_desc[i].status = ret;
2307 goto err;
2309 urb->iso_frame_desc[i].actual_length = (desc->hw.td.status + 1) & 0x7ff;
2310 urb->iso_frame_desc[i].status = uhci_map_status (uhci_status_bits (desc->hw.td.status), usb_pipeout (urb->pipe));
2311 urb->actual_length += urb->iso_frame_desc[i].actual_length;
2313 err:
2315 if (urb->iso_frame_desc[i].status != 0) {
2316 urb->error_count++;
2317 urb->status = urb->iso_frame_desc[i].status;
2319 dbg("process_iso: %i: len:%d %08x status:%x",
2320 i, urb->iso_frame_desc[i].actual_length, desc->hw.td.status,urb->iso_frame_desc[i].status);
2322 delete_desc (desc);
2323 list_del (p);
2326 dbg("process_iso: exit %i (%d), actual_len %i", i, ret,urb->actual_length);
2327 return ret;
2331 _static int process_urb (uhci_t *s, struct list_head *p)
2333 int ret = 0;
2334 urb_t *urb;
2337 urb=list_entry (p, urb_t, urb_list);
2338 //dbg("process_urb: found queued urb: %p", urb);
2340 switch (usb_pipetype (urb->pipe)) {
2341 case PIPE_CONTROL:
2342 ret = process_transfer (s, urb, 1);
2343 break;
2344 case PIPE_BULK:
2345 if (!s->avoid_bulk.counter)
2346 ret = process_transfer (s, urb, 1);
2347 else
2348 return 0;
2349 break;
2350 case PIPE_ISOCHRONOUS:
2351 ret = process_iso (s, urb, 0);
2352 break;
2353 case PIPE_INTERRUPT:
2354 ret = process_interrupt (s, urb);
2355 break;
2358 if (urb->status != -EINPROGRESS) {
2359 int proceed = 0;
2361 dbg("dequeued urb: %p", urb);
2362 dequeue_urb (s, urb);
2364 #ifdef DEBUG_SLAB
2365 kmem_cache_free(urb_priv_kmem, urb->hcpriv);
2366 #else
2367 kfree (urb->hcpriv);
2368 #endif
2370 if ((usb_pipetype (urb->pipe) != PIPE_INTERRUPT)) {
2371 urb_t *tmp = urb->next; // pointer to first urb
2372 int is_ring = 0;
2374 if (urb->next) {
2375 do {
2376 if (tmp->status != -EINPROGRESS) {
2377 proceed = 1;
2378 break;
2380 tmp = tmp->next;
2382 while (tmp != NULL && tmp != urb->next);
2383 if (tmp == urb->next)
2384 is_ring = 1;
2387 spin_unlock(&s->urb_list_lock);
2389 // In case you need the current URB status for your completion handler
2390 if (urb->complete && (!proceed || (urb->transfer_flags & USB_URB_EARLY_COMPLETE))) {
2391 dbg("process_transfer: calling early completion");
2392 urb->complete ((struct urb *) urb);
2393 if (!proceed && is_ring && (urb->status != -ENOENT))
2394 uhci_submit_urb (urb);
2397 if (proceed && urb->next) {
2398 // if there are linked urbs - handle submitting of them right now.
2399 tmp = urb->next; // pointer to first urb
2401 do {
2402 if ((tmp->status != -EINPROGRESS) && (tmp->status != -ENOENT) && uhci_submit_urb (tmp) != 0)
2403 break;
2404 tmp = tmp->next;
2406 while (tmp != NULL && tmp != urb->next); // submit until we reach NULL or our own pointer or submit fails
2408 if (urb->complete && !(urb->transfer_flags & USB_URB_EARLY_COMPLETE)) {
2409 dbg("process_transfer: calling completion");
2410 urb->complete ((struct urb *) urb);
2414 spin_lock(&s->urb_list_lock);
2416 usb_dec_dev_use (urb->dev);
2420 return ret;
2423 _static void uhci_interrupt (int irq, void *__uhci, struct pt_regs *regs)
2425 uhci_t *s = __uhci;
2426 unsigned int io_addr = s->io_addr;
2427 unsigned short status;
2428 struct list_head *p, *p2;
2431 * Read the interrupt status, and write it back to clear the
2432 * interrupt cause
2435 status = inw (io_addr + USBSTS);
2437 if (!status) /* shared interrupt, not mine */
2438 return;
2440 dbg("interrupt");
2442 if (status != 1) {
2443 warn("interrupt, status %x, frame# %i", status,
2444 UHCI_GET_CURRENT_FRAME(s));
2446 // remove host controller halted state
2447 if ((status&0x20) && (s->running)) {
2448 outw (USBCMD_RS | inw(io_addr + USBCMD), io_addr + USBCMD);
2450 //uhci_show_status (s);
2453 * traverse the list in *reverse* direction, because new entries
2454 * may be added at the end.
2455 * also, because process_urb may unlink the current urb,
2456 * we need to advance the list before
2459 spin_lock (&s->urb_list_lock);
2460 restart:
2461 s->unlink_urb_done=0;
2462 p = s->urb_list.prev;
2464 while (p != &s->urb_list) {
2465 p2 = p;
2466 p = p->prev;
2467 process_urb (s, p2);
2468 if (s->unlink_urb_done) {
2469 s->unlink_urb_done=0;
2470 goto restart;
2473 if ((s->frame_counter & 63) == 0)
2474 uhci_check_timeouts(s);
2476 clean_descs(s,0);
2477 uhci_cleanup_unlink(s, 0);
2479 spin_unlock (&s->urb_list_lock);
2481 s->frame_counter++;
2482 outw (status, io_addr + USBSTS);
2484 //dbg("uhci_interrupt: done");
2487 _static void reset_hc (uhci_t *s)
2489 unsigned int io_addr = s->io_addr;
2491 s->apm_state = 0;
2492 /* Global reset for 50ms */
2493 outw (USBCMD_GRESET, io_addr + USBCMD);
2494 uhci_wait_ms (50);
2495 outw (0, io_addr + USBCMD);
2496 uhci_wait_ms (10);
2499 _static void start_hc (uhci_t *s)
2501 unsigned int io_addr = s->io_addr;
2502 int timeout = 1000;
2505 * Reset the HC - this will force us to get a
2506 * new notification of any already connected
2507 * ports due to the virtual disconnect that it
2508 * implies.
2510 outw (USBCMD_HCRESET, io_addr + USBCMD);
2512 while (inw (io_addr + USBCMD) & USBCMD_HCRESET) {
2513 if (!--timeout) {
2514 err("USBCMD_HCRESET timed out!");
2515 break;
2519 /* Turn on all interrupts */
2520 outw (USBINTR_TIMEOUT | USBINTR_RESUME | USBINTR_IOC | USBINTR_SP, io_addr + USBINTR);
2522 /* Start at frame 0 */
2523 outw (0, io_addr + USBFRNUM);
2524 outl (virt_to_bus (s->framelist), io_addr + USBFLBASEADD);
2526 /* Run and mark it configured with a 64-byte max packet */
2527 outw (USBCMD_RS | USBCMD_CF | USBCMD_MAXP, io_addr + USBCMD);
2528 s->apm_state = 1;
2529 s->running = 1;
2532 _static void __exit uhci_cleanup_dev(uhci_t *s)
2534 struct usb_device *root_hub = s->bus->root_hub;
2536 s->running = 0; // Don't allow submit_urb
2538 if (root_hub)
2539 usb_disconnect (&root_hub);
2541 reset_hc (s);
2542 wait_ms (1);
2544 uhci_unlink_urbs (s, 0, 1); // Forced unlink of remaining URBs
2545 uhci_cleanup_unlink (s, 1); // force cleanup of async killed URBs
2547 usb_deregister_bus (s->bus);
2549 release_region (s->io_addr, s->io_size);
2550 free_irq (s->irq, s);
2551 usb_free_bus (s->bus);
2552 cleanup_skel (s);
2553 kfree (s);
2556 _static int __init uhci_start_usb (uhci_t *s)
2557 { /* start it up */
2558 /* connect the virtual root hub */
2559 struct usb_device *usb_dev;
2561 usb_dev = usb_alloc_dev (NULL, s->bus);
2562 if (!usb_dev)
2563 return -1;
2565 s->bus->root_hub = usb_dev;
2566 usb_connect (usb_dev);
2568 if (usb_new_device (usb_dev) != 0) {
2569 usb_free_dev (usb_dev);
2570 return -1;
2573 return 0;
2576 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,3,44)
2577 _static int handle_pm_event (struct pm_dev *dev, pm_request_t rqst, void *data)
2579 uhci_t *s = (uhci_t*) dev->data;
2580 dbg("handle_apm_event(%d)", rqst);
2581 if (s) {
2582 switch (rqst) {
2583 case PM_SUSPEND:
2584 reset_hc (s);
2585 break;
2586 case PM_RESUME:
2587 start_hc (s);
2588 break;
2591 return 0;
2593 #endif
2595 _static int __init alloc_uhci (struct pci_dev *dev, int irq, unsigned int io_addr, unsigned int io_size)
2597 uhci_t *s;
2598 struct usb_bus *bus;
2599 struct pm_dev *pmdev;
2601 s = kmalloc (sizeof (uhci_t), GFP_KERNEL);
2602 if (!s)
2603 return -1;
2605 memset (s, 0, sizeof (uhci_t));
2606 INIT_LIST_HEAD (&s->free_desc);
2607 INIT_LIST_HEAD (&s->urb_list);
2608 INIT_LIST_HEAD (&s->urb_unlinked);
2609 spin_lock_init (&s->urb_list_lock);
2610 spin_lock_init (&s->qh_lock);
2611 spin_lock_init (&s->td_lock);
2612 atomic_set(&s->avoid_bulk, 0);
2613 s->irq = -1;
2614 s->io_addr = io_addr;
2615 s->io_size = io_size;
2616 s->next = devs; //chain new uhci device into global list
2617 s->frame_counter = 0;
2619 bus = usb_alloc_bus (&uhci_device_operations);
2620 if (!bus) {
2621 kfree (s);
2622 return -1;
2625 s->bus = bus;
2626 bus->hcpriv = s;
2628 /* UHCI specs says devices must have 2 ports, but goes on to say */
2629 /* they may have more but give no way to determine how many they */
2630 /* have, so default to 2 */
2631 /* According to the UHCI spec, Bit 7 is always set to 1. So we try */
2632 /* to use this to our advantage */
2634 for (s->maxports = 0; s->maxports < (io_size - 0x10) / 2; s->maxports++) {
2635 unsigned int portstatus;
2637 portstatus = inw (io_addr + 0x10 + (s->maxports * 2));
2638 dbg("port %i, adr %x status %x", s->maxports,
2639 io_addr + 0x10 + (s->maxports * 2), portstatus);
2640 if (!(portstatus & 0x0080))
2641 break;
2643 warn("Detected %d ports", s->maxports);
2645 /* This is experimental so anything less than 2 or greater than 8 is */
2646 /* something weird and we'll ignore it */
2647 if (s->maxports < 2 || s->maxports > 8) {
2648 dbg("Port count misdetected, forcing to 2 ports");
2649 s->maxports = 2;
2652 s->rh.numports = s->maxports;
2653 s->loop_usage=0;
2654 if (init_skel (s)) {
2655 usb_free_bus (bus);
2656 kfree(s);
2657 return -1;
2660 request_region (s->io_addr, io_size, MODNAME);
2661 reset_hc (s);
2662 usb_register_bus (s->bus);
2664 start_hc (s);
2666 if (request_irq (irq, uhci_interrupt, SA_SHIRQ, MODNAME, s)) {
2667 err("request_irq %d failed!",irq);
2668 usb_free_bus (bus);
2669 reset_hc (s);
2670 release_region (s->io_addr, s->io_size);
2671 cleanup_skel(s);
2672 kfree(s);
2673 return -1;
2676 s->irq = irq;
2678 if(uhci_start_usb (s) < 0) {
2679 uhci_cleanup_dev(s);
2680 return -1;
2683 //chain new uhci device into global list
2684 devs = s;
2685 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,3,44)
2686 pmdev = pm_register(PM_PCI_DEV, PM_PCI_ID(dev), handle_pm_event);
2687 if (pmdev)
2688 pmdev->data = s;
2689 #endif
2690 return 0;
2693 _static int __init start_uhci (struct pci_dev *dev)
2695 int i;
2697 /* Search for the IO base address.. */
2698 for (i = 0; i < 6; i++) {
2699 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,3,8)
2700 unsigned int io_addr = dev->resource[i].start;
2701 unsigned int io_size =
2702 dev->resource[i].end - dev->resource[i].start + 1;
2703 if (!(dev->resource[i].flags & 1))
2704 continue;
2705 #else
2706 unsigned int io_addr = dev->base_address[i];
2707 unsigned int io_size = 0x14;
2708 if (!(io_addr & 1))
2709 continue;
2710 io_addr &= ~1;
2711 #endif
2713 /* Is it already in use? */
2714 if (check_region (io_addr, io_size))
2715 break;
2716 /* disable legacy emulation */
2717 pci_write_config_word (dev, USBLEGSUP, USBLEGSUP_DEFAULT);
2718 if(dev->vendor==0x8086) {
2719 info("Intel USB controller: setting latency timer to %d", UHCI_LATENCY_TIMER);
2720 pci_write_config_byte(dev, PCI_LATENCY_TIMER, UHCI_LATENCY_TIMER);
2722 return alloc_uhci(dev, dev->irq, io_addr, io_size);
2724 return -1;
2727 int __init uhci_init (void)
2729 int retval = -ENODEV;
2730 struct pci_dev *dev = NULL;
2731 u8 type;
2732 int i=0;
2734 #ifdef DEBUG_SLAB
2736 uhci_desc_kmem = kmem_cache_create("uhci_desc", sizeof(uhci_desc_t), 0, SLAB_HWCACHE_ALIGN, NULL, NULL);
2738 if(!uhci_desc_kmem) {
2739 err("kmem_cache_create for uhci_desc failed (out of memory)");
2740 return -ENOMEM;
2743 urb_priv_kmem = kmem_cache_create("urb_priv", sizeof(urb_priv_t), 0, SLAB_HWCACHE_ALIGN, NULL, NULL);
2745 if(!urb_priv_kmem) {
2746 err("kmem_cache_create for urb_priv_t failed (out of memory)");
2747 return -ENOMEM;
2749 #endif
2750 info(VERSTR);
2752 #ifdef CONFIG_USB_UHCI_HIGH_BANDWIDTH
2753 info("High bandwidth mode enabled");
2754 #endif
2755 for (;;) {
2756 dev = pci_find_class (PCI_CLASS_SERIAL_USB << 8, dev);
2757 if (!dev)
2758 break;
2760 /* Is it UHCI */
2761 pci_read_config_byte (dev, PCI_CLASS_PROG, &type);
2762 if (type != 0)
2763 continue;
2765 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,3,8)
2766 if (pci_enable_device (dev) < 0)
2767 continue;
2768 #endif
2769 if(!dev->irq)
2771 err("Found UHCI device with no IRQ assigned. Check BIOS settings!");
2772 continue;
2775 /* Ok set it up */
2776 retval = start_uhci (dev);
2778 if (!retval)
2779 i++;
2782 return retval;
2785 void __exit uhci_cleanup (void)
2787 uhci_t *s;
2788 while ((s = devs)) {
2789 devs = devs->next;
2790 uhci_cleanup_dev(s);
2792 #ifdef DEBUG_SLAB
2793 if(kmem_cache_destroy(uhci_desc_kmem))
2794 err("uhci_desc_kmem remained");
2796 if(kmem_cache_destroy(urb_priv_kmem))
2797 err("urb_priv_kmem remained");
2798 #endif
2801 #ifdef MODULE
2802 int init_module (void)
2804 return uhci_init ();
2807 void cleanup_module (void)
2809 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,3,44)
2810 pm_unregister_all (handle_pm_event);
2811 #endif
2812 uhci_cleanup ();
2815 #endif //MODULE