Merge with 2.3.99-pre1.
[linux-2.6/linux-mips.git] / drivers / usb / usb-uhci.c
blob85a5cd476d5d9cfe7d69997365320081322f32ca
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 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,3,44)
32 #include <linux/pm.h>
33 #endif
35 #include <asm/uaccess.h>
36 #include <asm/io.h>
37 #include <asm/irq.h>
38 #include <asm/system.h>
40 /* This enables more detailed sanity checks in submit_iso */
41 //#define ISO_SANITY_CHECK
43 /* This enables debug printks */
44 #define DEBUG
46 /* This enables all symbols to be exported, to ease debugging oopses */
47 //#define DEBUG_SYMBOLS
49 /* This enables an extra UHCI slab for memory debugging */
50 #define DEBUG_SLAB
52 #include <linux/usb.h>
53 #include "usb-uhci.h"
54 #include "usb-uhci-debug.h"
56 #undef DEBUG
57 #undef dbg
58 #define dbg(format, arg...) do {} while (0)
59 #define DEBUG_SYMBOLS
60 #ifdef DEBUG_SYMBOLS
61 #define _static
62 #ifndef EXPORT_SYMTAB
63 #define EXPORT_SYMTAB
64 #endif
65 #else
66 #define _static static
67 #endif
69 #define queue_dbg dbg //err
70 #define async_dbg dbg //err
72 #ifdef DEBUG_SLAB
73 static kmem_cache_t *uhci_desc_kmem;
74 static kmem_cache_t *urb_priv_kmem;
75 #endif
77 #define SLAB_FLAG (in_interrupt ()? SLAB_ATOMIC : SLAB_KERNEL)
78 #define KMALLOC_FLAG (in_interrupt ()? GFP_ATOMIC : GFP_KERNEL)
80 #define CONFIG_USB_UHCI_HIGH_BANDWIDTH
81 #define USE_CTRL_DEPTH_FIRST 0 // 0: Breadth first, 1: Depth first
82 #define USE_BULK_DEPTH_FIRST 0 // 0: Breadth first, 1: Depth first
84 // stop bandwidth reclamation after (roughly) 50ms
85 #define IDLE_TIMEOUT (HZ/20)
87 _static int rh_submit_urb (urb_t *urb);
88 _static int rh_unlink_urb (urb_t *urb);
89 _static int delete_qh (uhci_t *s, uhci_desc_t *qh);
90 _static int process_transfer (uhci_t *s, urb_t *urb, int mode);
91 _static int process_interrupt (uhci_t *s, urb_t *urb);
92 _static int process_iso (uhci_t *s, urb_t *urb, int force);
94 static uhci_t *devs = NULL;
96 /* used by userspace UHCI data structure dumper */
97 uhci_t **uhci_devices = &devs;
99 /*-------------------------------------------------------------------*/
100 // Cleans up collected QHs
101 void clean_descs(uhci_t *s, int force)
103 struct list_head *q;
104 uhci_desc_t *qh;
105 int now=UHCI_GET_CURRENT_FRAME(s);
107 q=s->free_desc.prev;
109 while (q != &s->free_desc) {
110 qh = list_entry (q, uhci_desc_t, horizontal);
111 if ((qh->last_used!=now) || force)
112 delete_qh(s,qh);
114 q=qh->horizontal.prev;
117 /*-------------------------------------------------------------------*/
118 #ifdef CONFIG_USB_UHCI_HIGH_BANDWIDTH
119 _static void enable_desc_loop(uhci_t *s, urb_t *urb)
121 int flags;
123 spin_lock_irqsave (&s->qh_lock, flags);
124 s->chain_end->hw.qh.head&=~UHCI_PTR_TERM;
125 mb();
126 s->loop_usage++;
127 ((urb_priv_t*)urb->hcpriv)->use_loop=1;
128 spin_unlock_irqrestore (&s->qh_lock, flags);
130 /*-------------------------------------------------------------------*/
131 _static void disable_desc_loop(uhci_t *s, urb_t *urb)
133 int flags;
135 spin_lock_irqsave (&s->qh_lock, flags);
137 if (((urb_priv_t*)urb->hcpriv)->use_loop) {
138 s->loop_usage--;
140 if (!s->loop_usage) {
141 s->chain_end->hw.qh.head|=UHCI_PTR_TERM;
142 mb();
144 ((urb_priv_t*)urb->hcpriv)->use_loop=0;
146 spin_unlock_irqrestore (&s->qh_lock, flags);
148 #endif
149 /*-------------------------------------------------------------------*/
150 _static void queue_urb_unlocked (uhci_t *s, urb_t *urb)
152 struct list_head *p=&urb->urb_list;
153 #ifdef CONFIG_USB_UHCI_HIGH_BANDWIDTH
155 int type;
156 type=usb_pipetype (urb->pipe);
158 if ((type == PIPE_BULK) || (type == PIPE_CONTROL))
159 enable_desc_loop(s, urb);
161 #endif
162 ((urb_priv_t*)urb->hcpriv)->started=jiffies;
163 list_add (p, &s->urb_list);
165 /*-------------------------------------------------------------------*/
166 _static void queue_urb (uhci_t *s, urb_t *urb)
168 unsigned long flags=0;
170 spin_lock_irqsave (&s->urb_list_lock, flags);
171 queue_urb_unlocked(s,urb);
172 spin_unlock_irqrestore (&s->urb_list_lock, flags);
174 /*-------------------------------------------------------------------*/
175 _static void dequeue_urb (uhci_t *s, urb_t *urb)
177 #ifdef CONFIG_USB_UHCI_HIGH_BANDWIDTH
178 int type;
180 type=usb_pipetype (urb->pipe);
182 if ((type == PIPE_BULK) || (type == PIPE_CONTROL))
183 disable_desc_loop(s, urb);
184 #endif
186 list_del (&urb->urb_list);
188 /*-------------------------------------------------------------------*/
189 _static int alloc_td (uhci_desc_t ** new, int flags)
191 #ifdef DEBUG_SLAB
192 *new= kmem_cache_alloc(uhci_desc_kmem, SLAB_FLAG);
193 #else
194 *new = (uhci_desc_t *) kmalloc (sizeof (uhci_desc_t), KMALLOC_FLAG);
195 #endif
196 if (!*new)
197 return -ENOMEM;
198 memset (*new, 0, sizeof (uhci_desc_t));
199 (*new)->hw.td.link = UHCI_PTR_TERM | (flags & UHCI_PTR_BITS); // last by default
200 (*new)->type = TD_TYPE;
201 mb();
202 INIT_LIST_HEAD (&(*new)->vertical);
203 INIT_LIST_HEAD (&(*new)->horizontal);
205 return 0;
207 /*-------------------------------------------------------------------*/
208 // append a qh to td.link physically, the SW linkage is not affected
209 _static void append_qh(uhci_t *s, uhci_desc_t *td, uhci_desc_t* qh, int flags)
211 unsigned long xxx;
213 spin_lock_irqsave (&s->td_lock, xxx);
215 td->hw.td.link = virt_to_bus (qh) | (flags & UHCI_PTR_DEPTH) | UHCI_PTR_QH;
217 mb();
218 spin_unlock_irqrestore (&s->td_lock, xxx);
220 /*-------------------------------------------------------------------*/
221 /* insert td at last position in td-list of qh (vertical) */
222 _static int insert_td (uhci_t *s, uhci_desc_t *qh, uhci_desc_t* new, int flags)
224 uhci_desc_t *prev;
225 unsigned long xxx;
227 spin_lock_irqsave (&s->td_lock, xxx);
229 list_add_tail (&new->vertical, &qh->vertical);
231 prev = list_entry (new->vertical.prev, uhci_desc_t, vertical);
233 if (qh == prev ) {
234 // virgin qh without any tds
235 qh->hw.qh.element = virt_to_bus (new);
237 else {
238 // already tds inserted, implicitely remove TERM bit of prev
239 prev->hw.td.link = virt_to_bus (new) | (flags & UHCI_PTR_DEPTH);
241 mb();
242 spin_unlock_irqrestore (&s->td_lock, xxx);
244 return 0;
246 /*-------------------------------------------------------------------*/
247 /* insert new_td after td (horizontal) */
248 _static int insert_td_horizontal (uhci_t *s, uhci_desc_t *td, uhci_desc_t* new)
250 uhci_desc_t *next;
251 unsigned long flags;
253 spin_lock_irqsave (&s->td_lock, flags);
255 next = list_entry (td->horizontal.next, uhci_desc_t, horizontal);
256 list_add (&new->horizontal, &td->horizontal);
257 new->hw.td.link = td->hw.td.link;
258 td->hw.td.link = virt_to_bus (new);
259 mb();
260 spin_unlock_irqrestore (&s->td_lock, flags);
262 return 0;
264 /*-------------------------------------------------------------------*/
265 _static int unlink_td (uhci_t *s, uhci_desc_t *element, int phys_unlink)
267 uhci_desc_t *next, *prev;
268 int dir = 0;
269 unsigned long flags;
271 spin_lock_irqsave (&s->td_lock, flags);
273 next = list_entry (element->vertical.next, uhci_desc_t, vertical);
275 if (next == element) {
276 dir = 1;
277 prev = list_entry (element->horizontal.prev, uhci_desc_t, horizontal);
279 else
280 prev = list_entry (element->vertical.prev, uhci_desc_t, vertical);
282 if (phys_unlink) {
283 // really remove HW linking
284 if (prev->type == TD_TYPE)
285 prev->hw.td.link = element->hw.td.link;
286 else
287 prev->hw.qh.element = element->hw.td.link;
290 mb ();
292 if (dir == 0)
293 list_del (&element->vertical);
294 else
295 list_del (&element->horizontal);
297 spin_unlock_irqrestore (&s->td_lock, flags);
299 return 0;
302 /*-------------------------------------------------------------------*/
303 _static int delete_desc (uhci_desc_t *element)
305 #ifdef DEBUG_SLAB
306 kmem_cache_free(uhci_desc_kmem, element);
307 #else
308 kfree (element);
309 #endif
310 return 0;
312 /*-------------------------------------------------------------------*/
313 // Allocates qh element
314 _static int alloc_qh (uhci_desc_t ** new)
316 #ifdef DEBUG_SLAB
317 *new= kmem_cache_alloc(uhci_desc_kmem, SLAB_FLAG);
318 #else
319 *new = (uhci_desc_t *) kmalloc (sizeof (uhci_desc_t), KMALLOC_FLAG);
320 #endif
321 if (!*new)
322 return -ENOMEM;
323 memset (*new, 0, sizeof (uhci_desc_t));
324 (*new)->hw.qh.head = UHCI_PTR_TERM;
325 (*new)->hw.qh.element = UHCI_PTR_TERM;
326 (*new)->type = QH_TYPE;
328 mb();
329 INIT_LIST_HEAD (&(*new)->horizontal);
330 INIT_LIST_HEAD (&(*new)->vertical);
332 dbg("Allocated qh @ %p", *new);
334 return 0;
336 /*-------------------------------------------------------------------*/
337 // inserts new qh before/after the qh at pos
338 // flags: 0: insert before pos, 1: insert after pos (for low speed transfers)
339 _static int insert_qh (uhci_t *s, uhci_desc_t *pos, uhci_desc_t *new, int order)
341 uhci_desc_t *old;
342 unsigned long flags;
344 spin_lock_irqsave (&s->qh_lock, flags);
346 if (!order) {
347 // (OLD) (POS) -> (OLD) (NEW) (POS)
348 old = list_entry (pos->horizontal.prev, uhci_desc_t, horizontal);
349 list_add_tail (&new->horizontal, &pos->horizontal);
350 new->hw.qh.head = MAKE_QH_ADDR (pos) ;
351 if (!(old->hw.qh.head & UHCI_PTR_TERM))
352 old->hw.qh.head = MAKE_QH_ADDR (new) ;
354 else {
355 // (POS) (OLD) -> (POS) (NEW) (OLD)
356 old = list_entry (pos->horizontal.next, uhci_desc_t, horizontal);
357 list_add (&new->horizontal, &pos->horizontal);
358 new->hw.qh.head = MAKE_QH_ADDR (old);
359 pos->hw.qh.head = MAKE_QH_ADDR (new) ;
362 mb ();
364 spin_unlock_irqrestore (&s->qh_lock, flags);
366 return 0;
369 /*-------------------------------------------------------------------*/
370 _static int unlink_qh (uhci_t *s, uhci_desc_t *element)
372 uhci_desc_t *prev;
373 unsigned long flags;
375 spin_lock_irqsave (&s->qh_lock, flags);
377 prev = list_entry (element->horizontal.prev, uhci_desc_t, horizontal);
378 prev->hw.qh.head = element->hw.qh.head;
380 list_del(&element->horizontal);
382 mb ();
383 spin_unlock_irqrestore (&s->qh_lock, flags);
385 return 0;
387 /*-------------------------------------------------------------------*/
388 _static int delete_qh (uhci_t *s, uhci_desc_t *qh)
390 uhci_desc_t *td;
391 struct list_head *p;
393 list_del (&qh->horizontal);
395 while ((p = qh->vertical.next) != &qh->vertical) {
396 td = list_entry (p, uhci_desc_t, vertical);
397 dbg("unlink td @ %p",td);
398 unlink_td (s, td, 0); // no physical unlink
399 delete_desc (td);
402 delete_desc (qh);
404 return 0;
406 /*-------------------------------------------------------------------*/
407 _static void clean_td_chain (uhci_desc_t *td)
409 struct list_head *p;
410 uhci_desc_t *td1;
412 if (!td)
413 return;
415 while ((p = td->horizontal.next) != &td->horizontal) {
416 td1 = list_entry (p, uhci_desc_t, horizontal);
417 delete_desc (td1);
420 delete_desc (td);
423 /*-------------------------------------------------------------------*/
424 _static void fill_td (uhci_desc_t *td, int status, int info, __u32 buffer)
426 td->hw.td.status = status;
427 td->hw.td.info = info;
428 td->hw.td.buffer = buffer;
430 /*-------------------------------------------------------------------*/
431 // Removes ALL qhs in chain (paranoia!)
432 _static void cleanup_skel (uhci_t *s)
434 unsigned int n;
435 uhci_desc_t *td;
437 dbg("cleanup_skel");
439 clean_descs(s,1);
441 for (n = 0; n < 8; n++) {
442 td = s->int_chain[n];
443 clean_td_chain (td);
446 if (s->iso_td) {
447 for (n = 0; n < 1024; n++) {
448 td = s->iso_td[n];
449 clean_td_chain (td);
451 kfree (s->iso_td);
454 if (s->framelist)
455 free_page ((unsigned long) s->framelist);
457 if (s->control_chain) {
458 // completed init_skel?
459 struct list_head *p;
460 uhci_desc_t *qh, *qh1;
462 qh = s->control_chain;
463 while ((p = qh->horizontal.next) != &qh->horizontal) {
464 qh1 = list_entry (p, uhci_desc_t, horizontal);
465 delete_qh (s, qh1);
468 delete_qh (s, qh);
470 else {
471 if (s->ls_control_chain)
472 delete_desc (s->ls_control_chain);
473 if (s->control_chain)
474 delete_desc(s->control_chain);
475 if (s->bulk_chain)
476 delete_desc (s->bulk_chain);
477 if (s->chain_end)
478 delete_desc (s->chain_end);
480 dbg("cleanup_skel finished");
482 /*-------------------------------------------------------------------*/
483 // allocates framelist and qh-skeletons
484 // only HW-links provide continous linking, SW-links stay in their domain (ISO/INT)
485 _static int init_skel (uhci_t *s)
487 int n, ret;
488 uhci_desc_t *qh, *td;
490 dbg("init_skel");
492 s->framelist = (__u32 *) get_free_page (GFP_KERNEL);
494 if (!s->framelist)
495 return -ENOMEM;
497 memset (s->framelist, 0, 4096);
499 dbg("allocating iso desc pointer list");
500 s->iso_td = (uhci_desc_t **) kmalloc (1024 * sizeof (uhci_desc_t*), GFP_KERNEL);
502 if (!s->iso_td)
503 goto init_skel_cleanup;
505 s->ls_control_chain = NULL;
506 s->control_chain = NULL;
507 s->bulk_chain = NULL;
508 s->chain_end = NULL;
510 dbg("allocating iso descs");
511 for (n = 0; n < 1024; n++) {
512 // allocate skeleton iso/irq-tds
513 ret = alloc_td (&td, 0);
514 if (ret)
515 goto init_skel_cleanup;
516 s->iso_td[n] = td;
517 s->framelist[n] = ((__u32) virt_to_bus (td));
520 dbg("allocating qh: chain_end");
521 ret = alloc_qh (&qh);
523 if (ret)
524 goto init_skel_cleanup;
526 s->chain_end = qh;
528 ret = alloc_td (&td, 0);
530 if (ret)
531 goto init_skel_cleanup;
533 fill_td (td, TD_CTRL_IOC, 0, 0); // generate 1ms interrupt
534 insert_td (s, qh, td, 0);
536 dbg("allocating qh: bulk_chain");
537 ret = alloc_qh (&qh);
538 if (ret)
539 goto init_skel_cleanup;
541 insert_qh (s, s->chain_end, qh, 0);
542 s->bulk_chain = qh;
544 dbg("allocating qh: control_chain");
545 ret = alloc_qh (&qh);
546 if (ret)
547 goto init_skel_cleanup;
549 insert_qh (s, s->bulk_chain, qh, 0);
550 s->control_chain = qh;
552 #ifdef CONFIG_USB_UHCI_HIGH_BANDWIDTH
553 // disabled reclamation loop
554 s->chain_end->hw.qh.head=virt_to_bus(s->control_chain) | UHCI_PTR_QH | UHCI_PTR_TERM;
555 #endif
557 dbg("allocating qh: ls_control_chain");
558 ret = alloc_qh (&qh);
559 if (ret)
560 goto init_skel_cleanup;
562 insert_qh (s, s->control_chain, qh, 0);
563 s->ls_control_chain = qh;
565 for (n = 0; n < 8; n++)
566 s->int_chain[n] = 0;
568 dbg("allocating skeleton INT-TDs");
570 for (n = 0; n < 8; n++) {
571 uhci_desc_t *td;
573 alloc_td (&td, 0);
574 if (!td)
575 goto init_skel_cleanup;
576 s->int_chain[n] = td;
577 if (n == 0) {
578 s->int_chain[0]->hw.td.link = virt_to_bus (s->ls_control_chain) | UHCI_PTR_QH;
580 else {
581 s->int_chain[n]->hw.td.link = virt_to_bus (s->int_chain[0]);
585 dbg("Linking skeleton INT-TDs");
587 for (n = 0; n < 1024; n++) {
588 // link all iso-tds to the interrupt chains
589 int m, o;
590 dbg("framelist[%i]=%x",n,s->framelist[n]);
591 if ((n&127)==127)
592 ((uhci_desc_t*) s->iso_td[n])->hw.td.link = virt_to_bus(s->int_chain[0]);
593 else
594 for (o = 1, m = 2; m <= 128; o++, m += m)
595 if ((n & (m - 1)) == ((m - 1) / 2))
596 ((uhci_desc_t*) s->iso_td[n])->hw.td.link = virt_to_bus (s->int_chain[o]);
599 mb();
600 //uhci_show_queue(s->control_chain);
601 dbg("init_skel exit");
602 return 0;
604 init_skel_cleanup:
605 cleanup_skel (s);
606 return -ENOMEM;
609 /*-------------------------------------------------------------------*/
610 // LOW LEVEL STUFF
611 // assembles QHs und TDs for control, bulk and iso
612 /*-------------------------------------------------------------------*/
613 _static int uhci_submit_control_urb (urb_t *urb)
615 uhci_desc_t *qh, *td;
616 uhci_t *s = (uhci_t*) urb->dev->bus->hcpriv;
617 urb_priv_t *urb_priv = urb->hcpriv;
618 unsigned long destination, status;
619 int maxsze = usb_maxpacket (urb->dev, urb->pipe, usb_pipeout (urb->pipe));
620 unsigned long len;
621 char *data;
622 int depth_first=USE_CTRL_DEPTH_FIRST; // UHCI descriptor chasing method
624 if (!maxsze) {
625 err("uhci_submit_control_urb: pipesize for pipe %x is zero", urb->pipe);
626 return -EINVAL;
629 dbg("uhci_submit_control start");
630 alloc_qh (&qh); // alloc qh for this request
632 if (!qh)
633 return -ENOMEM;
635 alloc_td (&td, UHCI_PTR_DEPTH * depth_first); // get td for setup stage
637 if (!td) {
638 delete_qh (s, qh);
639 return -ENOMEM;
642 /* The "pipe" thing contains the destination in bits 8--18 */
643 destination = (urb->pipe & PIPE_DEVEP_MASK) | USB_PID_SETUP;
645 /* 3 errors */
646 status = (urb->pipe & TD_CTRL_LS) | TD_CTRL_ACTIVE |
647 (urb->transfer_flags & USB_DISABLE_SPD ? 0 : TD_CTRL_SPD) | (3 << 27);
649 /* Build the TD for the control request, try forever, 8 bytes of data */
650 fill_td (td, status, destination | (7 << 21), virt_to_bus (urb->setup_packet));
652 insert_td (s, qh, td, 0); // queue 'setup stage'-td in qh
653 #if 0
655 char *sp=urb->setup_packet;
656 dbg("SETUP to pipe %x: %x %x %x %x %x %x %x %x", urb->pipe,
657 sp[0],sp[1],sp[2],sp[3],sp[4],sp[5],sp[6],sp[7]);
659 //uhci_show_td(td);
660 #endif
662 len = urb->transfer_buffer_length;
663 data = urb->transfer_buffer;
665 /* If direction is "send", change the frame from SETUP (0x2D)
666 to OUT (0xE1). Else change it from SETUP to IN (0x69). */
668 destination = (urb->pipe & PIPE_DEVEP_MASK) | (usb_pipeout (urb->pipe)?USB_PID_OUT:USB_PID_IN);
670 while (len > 0) {
671 int pktsze = len;
673 alloc_td (&td, UHCI_PTR_DEPTH * depth_first);
674 if (!td) {
675 delete_qh (s, qh);
676 return -ENOMEM;
679 if (pktsze > maxsze)
680 pktsze = maxsze;
682 destination ^= 1 << TD_TOKEN_TOGGLE; // toggle DATA0/1
684 fill_td (td, status, destination | ((pktsze - 1) << 21),
685 virt_to_bus (data)); // Status, pktsze bytes of data
687 insert_td (s, qh, td, UHCI_PTR_DEPTH * depth_first); // queue 'data stage'-td in qh
689 data += pktsze;
690 len -= pktsze;
693 /* Build the final TD for control status */
694 /* It's only IN if the pipe is out AND we aren't expecting data */
696 destination &= ~UHCI_PID;
698 if (usb_pipeout (urb->pipe) || (urb->transfer_buffer_length == 0))
699 destination |= USB_PID_IN;
700 else
701 destination |= USB_PID_OUT;
703 destination |= 1 << TD_TOKEN_TOGGLE; /* End in Data1 */
705 alloc_td (&td, UHCI_PTR_DEPTH);
707 if (!td) {
708 delete_qh (s, qh);
709 return -ENOMEM;
711 status &=~TD_CTRL_SPD;
713 /* no limit on errors on final packet , 0 bytes of data */
714 fill_td (td, status | TD_CTRL_IOC, destination | (UHCI_NULL_DATA_SIZE << 21),
717 insert_td (s, qh, td, UHCI_PTR_DEPTH * depth_first); // queue status td
719 list_add (&qh->desc_list, &urb_priv->desc_list);
721 urb->status = -EINPROGRESS;
722 queue_urb (s, urb); // queue before inserting in desc chain
724 qh->hw.qh.element &= ~UHCI_PTR_TERM;
726 //uhci_show_queue(qh);
727 /* Start it up... put low speed first */
728 if (urb->pipe & TD_CTRL_LS)
729 insert_qh (s, s->control_chain, qh, 0);
730 else
731 insert_qh (s, s->bulk_chain, qh, 0);
733 dbg("uhci_submit_control end");
734 return 0;
736 /*-------------------------------------------------------------------*/
737 // For queued bulk transfers, two additional QH helpers are allocated (nqh, bqh)
738 // Due to the linking with other bulk urbs, it has to be locked with urb_list_lock!
740 _static int uhci_submit_bulk_urb (urb_t *urb, urb_t *bulk_urb)
742 uhci_t *s = (uhci_t*) urb->dev->bus->hcpriv;
743 urb_priv_t *urb_priv = urb->hcpriv;
744 uhci_desc_t *qh, *td, *nqh, *bqh;
745 unsigned long destination, status;
746 char *data;
747 unsigned int pipe = urb->pipe;
748 int maxsze = usb_maxpacket (urb->dev, pipe, usb_pipeout (pipe));
749 int info, len;
750 int depth_first=USE_BULK_DEPTH_FIRST; // UHCI descriptor chasing method
751 urb_priv_t *upriv, *bpriv;
753 if (usb_endpoint_halted (urb->dev, usb_pipeendpoint (pipe), usb_pipeout (pipe)))
754 return -EPIPE;
756 if (urb->transfer_buffer_length < 0) {
757 err("Negative transfer length in submit_bulk");
758 return -EINVAL;
761 if (!maxsze)
762 return -EMSGSIZE;
764 queue_dbg("uhci_submit_bulk_urb: urb %p, old %p, pipe %08x, len %i",
765 urb,bulk_urb,urb->pipe,urb->transfer_buffer_length);
767 upriv=(urb_priv_t*)urb->hcpriv;
769 if (!bulk_urb) {
770 alloc_qh (&qh); // get qh for this request
772 if (!qh)
773 return -ENOMEM;
775 if (urb->transfer_flags & USB_QUEUE_BULK) {
776 alloc_qh(&nqh); // placeholder for clean unlink
777 if (!nqh) {
778 delete_desc (qh);
779 return -ENOMEM;
781 upriv->next_qh = nqh;
782 queue_dbg("new next qh %p",nqh);
785 else {
786 bpriv = (urb_priv_t*)bulk_urb->hcpriv;
787 qh = bpriv->bottom_qh; // re-use bottom qh and next qh
788 nqh = bpriv->next_qh;
789 upriv->next_qh=nqh;
790 bpriv->next_queued_urb=urb;
791 upriv->prev_queued_urb=bulk_urb;
794 queue_dbg("uhci_submit_bulk: qh=%p, nqh=%p\n",bqh,nqh);
796 if (urb->transfer_flags & USB_QUEUE_BULK) {
797 alloc_qh (&bqh); // "bottom" QH,
799 if (!bqh) {
800 if (!bulk_urb) {
801 delete_desc(qh);
802 delete_desc(nqh);
804 return -ENOMEM;
806 bqh->hw.qh.element = UHCI_PTR_TERM;
807 bqh->hw.qh.element = virt_to_bus(nqh)|UHCI_PTR_QH;
808 upriv->bottom_qh = bqh;
809 queue_dbg("uhci_submit_bulk: new bqh %p\n",bqh);
813 /* The "pipe" thing contains the destination in bits 8--18. */
814 destination = (pipe & PIPE_DEVEP_MASK) | usb_packetid (pipe);
816 /* 3 errors */
817 status = (pipe & TD_CTRL_LS) | TD_CTRL_ACTIVE |
818 ((urb->transfer_flags & USB_DISABLE_SPD) ? 0 : TD_CTRL_SPD) | (3 << 27);
820 /* Build the TDs for the bulk request */
821 len = urb->transfer_buffer_length;
822 data = urb->transfer_buffer;
824 do { // TBD: Really allow zero-length packets?
825 int pktsze = len;
827 alloc_td (&td, UHCI_PTR_DEPTH * depth_first);
829 if (!td) {
830 delete_qh (s, qh);
831 return -ENOMEM;
834 if (pktsze > maxsze)
835 pktsze = maxsze;
837 // pktsze bytes of data
838 info = destination | (((pktsze - 1)&UHCI_NULL_DATA_SIZE) << 21) |
839 (usb_gettoggle (urb->dev, usb_pipeendpoint (pipe), usb_pipeout (pipe)) << TD_TOKEN_TOGGLE);
841 fill_td (td, status, info, virt_to_bus (data));
843 data += pktsze;
844 len -= pktsze;
846 if (!len)
847 td->hw.td.status |= TD_CTRL_IOC; // last one generates INT
849 insert_td (s, qh, td, UHCI_PTR_DEPTH * depth_first);
850 usb_dotoggle (urb->dev, usb_pipeendpoint (pipe), usb_pipeout (pipe));
852 } while (len > 0);
854 list_add (&qh->desc_list, &urb_priv->desc_list);
856 if (urb->transfer_flags & USB_QUEUE_BULK) {
857 qh->hw.qh.element&=~UHCI_PTR_TERM;
858 append_qh(s, td, bqh, UHCI_PTR_DEPTH * depth_first);
861 urb->status = -EINPROGRESS;
862 queue_urb_unlocked (s, urb);
864 qh->hw.qh.element &= ~UHCI_PTR_TERM;
866 if (!bulk_urb) {
867 if (urb->transfer_flags & USB_QUEUE_BULK) {
868 spin_lock (&s->td_lock); // both QHs in one go
869 insert_qh (s, s->chain_end, qh, 0); // Main QH
870 insert_qh (s, s->chain_end, nqh, 0); // Helper QH
871 spin_unlock (&s->td_lock);
873 else
874 insert_qh (s, s->chain_end, qh, 0);
877 //uhci_show_queue(s->bulk_chain);
878 //dbg("uhci_submit_bulk_urb: exit\n");
879 return 0;
881 /*-------------------------------------------------------------------*/
882 _static void uhci_clean_iso_step1(uhci_t *s, urb_priv_t *urb_priv)
884 struct list_head *p;
885 uhci_desc_t *td;
887 for (p = urb_priv->desc_list.next; p != &urb_priv->desc_list; p = p->next) {
888 td = list_entry (p, uhci_desc_t, desc_list);
889 unlink_td (s, td, 1);
892 /*-------------------------------------------------------------------*/
893 _static void uhci_clean_iso_step2(uhci_t *s, urb_priv_t *urb_priv)
895 struct list_head *p;
896 uhci_desc_t *td;
898 while ((p = urb_priv->desc_list.next) != &urb_priv->desc_list) {
899 td = list_entry (p, uhci_desc_t, desc_list);
900 list_del (p);
901 delete_desc (td);
904 /*-------------------------------------------------------------------*/
905 // mode: 0: unlink + no deletion mark, 1: regular (unlink/delete-mark), 2: don't unlink
906 // looks a bit complicated because of all the bulk queueing goodies
908 _static void uhci_clean_transfer (uhci_t *s, urb_t *urb, uhci_desc_t *qh, int mode)
910 uhci_desc_t *bqh, *nqh, *prevqh;
911 int now;
912 urb_priv_t *priv=(urb_priv_t*)urb->hcpriv;
914 now=UHCI_GET_CURRENT_FRAME(s);
916 dbg("clean transfer urb %p, qh %p, mode %i",urb,qh,mode);
917 bqh=priv->bottom_qh;
919 if (!priv->next_queued_urb) { // no more appended bulk queues
921 if (mode != 2)
922 unlink_qh (s, qh);
924 if (priv->prev_queued_urb) {
925 urb_priv_t* ppriv=(urb_priv_t*)priv->prev_queued_urb->hcpriv;
927 ppriv->bottom_qh = priv->bottom_qh;
928 ppriv->next_queued_urb = NULL;
930 else if (bqh) { // queue dead
931 nqh=priv->next_qh;
933 if (mode != 2)
934 unlink_qh(s, nqh);
936 if (mode) {
937 nqh->last_used = bqh->last_used = now;
938 list_add_tail (&nqh->horizontal, &s->free_desc);
939 list_add_tail (&bqh->horizontal, &s->free_desc);
943 else { // there are queued urbs following
944 urb_t *nurb;
945 unsigned long flags;
947 nurb=priv->next_queued_urb;
948 spin_lock_irqsave (&s->qh_lock, flags);
950 if (!priv->prev_queued_urb) { // top
951 if (mode !=2) {
952 prevqh = list_entry (qh->horizontal.prev, uhci_desc_t, horizontal);
953 prevqh->hw.qh.head = virt_to_bus(bqh) | UHCI_PTR_QH;
954 queue_dbg ("TOP relink of %p to %p-%p",qh,prevqh,bqh);
956 list_del (&qh->horizontal);
957 list_add (&bqh->horizontal, &prevqh->horizontal);
960 else { //intermediate
961 urb_priv_t* ppriv=(urb_priv_t*)priv->prev_queued_urb->hcpriv;
962 uhci_desc_t * bnqh;
964 bnqh=list_entry (&((urb_priv_t*)(nurb->hcpriv))->desc_list.next, uhci_desc_t, desc_list);
965 ppriv->bottom_qh=bnqh;
966 ppriv->next_queued_urb=nurb;
968 if (mode!=2) {
969 prevqh = list_entry (ppriv->desc_list.next, uhci_desc_t, desc_list);
970 prevqh->hw.qh.head = virt_to_bus(bqh) | UHCI_PTR_QH;
971 queue_dbg ("IM relink of %p to %p-%p",qh,prevqh,bqh);
974 mb();
975 spin_unlock_irqrestore (&s->qh_lock, flags);
976 ((urb_priv_t*)nurb->hcpriv)->prev_queued_urb=priv->prev_queued_urb;
979 if (mode) {
980 qh->last_used = now;
981 list_add_tail (&qh->horizontal, &s->free_desc); // mark for later deletion
984 /*-------------------------------------------------------------------*/
985 // unlinks an urb by dequeuing its qh, waits some frames and forgets it
986 _static int uhci_unlink_urb_sync (uhci_t *s, urb_t *urb)
988 uhci_desc_t *qh;
989 urb_priv_t *urb_priv;
990 unsigned long flags=0;
992 spin_lock_irqsave (&s->urb_list_lock, flags);
994 if (urb->status == -EINPROGRESS) {
995 // URB probably still in work
996 dequeue_urb (s, urb);
997 s->unlink_urb_done=1;
998 spin_unlock_irqrestore (&s->urb_list_lock, flags);
1000 urb->status = -ENOENT; // mark urb as killed
1001 urb_priv = urb->hcpriv;
1003 switch (usb_pipetype (urb->pipe)) {
1004 case PIPE_ISOCHRONOUS:
1005 case PIPE_INTERRUPT:
1006 uhci_clean_iso_step1(s, urb_priv);
1007 uhci_wait_ms(1);
1008 uhci_clean_iso_step2(s, urb_priv);
1009 break;
1011 case PIPE_BULK:
1012 case PIPE_CONTROL:
1013 qh = list_entry (urb_priv->desc_list.next, uhci_desc_t, desc_list);
1014 spin_lock_irqsave (&s->urb_list_lock, flags);
1015 uhci_clean_transfer(s, urb, qh, 1);
1016 spin_unlock_irqrestore (&s->urb_list_lock, flags);
1017 uhci_wait_ms(1);
1020 #ifdef DEBUG_SLAB
1021 kmem_cache_free (urb_priv_kmem, urb->hcpriv);
1022 #else
1023 kfree (urb->hcpriv);
1024 #endif
1025 if (urb->complete) {
1026 dbg("unlink_urb: calling completion");
1027 urb->complete ((struct urb *) urb);
1029 usb_dec_dev_use (urb->dev);
1030 return 0;
1032 else
1033 spin_unlock_irqrestore (&s->urb_list_lock, flags);
1034 return 0;
1036 /*-------------------------------------------------------------------*/
1037 // async unlink_urb completion/cleanup work
1038 // has to be protected by urb_list_lock!
1039 // features: if set in transfer_flags, the resulting status of the killed
1040 // transaction is not overwritten
1042 _static void uhci_cleanup_unlink(uhci_t *s, int force)
1044 struct list_head *q;
1045 urb_t *urb;
1046 struct usb_device *dev;
1047 int pipe,now;
1048 urb_priv_t *urb_priv;
1050 q=s->urb_unlinked.next;
1051 now=UHCI_GET_CURRENT_FRAME(s);
1053 while (q != &s->urb_unlinked) {
1055 urb = list_entry (q, urb_t, urb_list);
1057 urb_priv = (urb_priv_t*)urb->hcpriv;
1058 q = urb->urb_list.next;
1060 if (force ||
1061 ((urb_priv->started != 0xffffffff) && (urb_priv->started != now))) {
1062 async_dbg("async cleanup %p",urb);
1063 switch (usb_pipetype (urb->pipe)) { // process descriptors
1064 case PIPE_CONTROL:
1065 process_transfer (s, urb, 2);
1066 break;
1067 case PIPE_BULK:
1068 if (!s->avoid_bulk.counter)
1069 process_transfer (s, urb, 2); // don't unlink (already done)
1070 else
1071 continue;
1072 break;
1073 case PIPE_ISOCHRONOUS:
1074 process_iso (s, urb, 1); // force, don't unlink
1075 break;
1076 case PIPE_INTERRUPT:
1077 process_interrupt (s, urb);
1078 break;
1081 if (!(urb->transfer_flags & USB_TIMEOUT_KILLED))
1082 urb->status = -ECONNRESET; // mark as asynchronously killed
1084 pipe = urb->pipe; // completion may destroy all...
1085 dev = urb->dev;
1086 urb_priv = urb->hcpriv;
1088 if (urb->complete) {
1089 spin_unlock(&s->urb_list_lock);
1090 urb->complete ((struct urb *) urb);
1091 spin_lock(&s->urb_list_lock);
1094 if (!(urb->transfer_flags & USB_TIMEOUT_KILLED))
1095 urb->status = -ENOENT; // now the urb is really dead
1097 usb_dec_dev_use (dev);
1098 #ifdef DEBUG_SLAB
1099 kmem_cache_free (urb_priv_kmem, urb_priv);
1100 #else
1101 kfree (urb_priv);
1102 #endif
1103 switch (usb_pipetype (pipe)) {
1104 case PIPE_ISOCHRONOUS:
1105 case PIPE_INTERRUPT:
1106 uhci_clean_iso_step2(s, urb_priv);
1107 break;
1109 list_del (&urb->urb_list);
1114 /*-------------------------------------------------------------------*/
1115 _static int uhci_unlink_urb_async (uhci_t *s,urb_t *urb)
1117 uhci_desc_t *qh;
1118 urb_priv_t *urb_priv;
1120 async_dbg("unlink_urb_async called %p",urb);
1122 if (urb->status == -EINPROGRESS) {
1123 ((urb_priv_t*)urb->hcpriv)->started = ~0;
1124 dequeue_urb (s, urb);
1125 list_add_tail (&urb->urb_list, &s->urb_unlinked); // store urb
1127 s->unlink_urb_done = 1;
1129 urb->status = -ECONNABORTED; // mark urb as "waiting to be killed"
1130 urb_priv = (urb_priv_t*)urb->hcpriv;
1132 switch (usb_pipetype (urb->pipe)) {
1133 case PIPE_ISOCHRONOUS:
1134 case PIPE_INTERRUPT:
1135 uhci_clean_iso_step1 (s, urb_priv);
1136 break;
1138 case PIPE_BULK:
1139 case PIPE_CONTROL:
1140 qh = list_entry (urb_priv->desc_list.next, uhci_desc_t, desc_list);
1141 uhci_clean_transfer (s, urb, qh, 0);
1142 break;
1144 ((urb_priv_t*)urb->hcpriv)->started = UHCI_GET_CURRENT_FRAME(s);
1147 return -EINPROGRESS;
1149 /*-------------------------------------------------------------------*/
1150 _static int uhci_unlink_urb (urb_t *urb)
1152 uhci_t *s;
1153 unsigned long flags=0;
1154 dbg("uhci_unlink_urb called for %p",urb);
1155 if (!urb || !urb->dev) // you never know...
1156 return -EINVAL;
1158 s = (uhci_t*) urb->dev->bus->hcpriv;
1160 if (usb_pipedevice (urb->pipe) == s->rh.devnum)
1161 return rh_unlink_urb (urb);
1163 if (!urb->hcpriv)
1164 return -EINVAL;
1166 if (urb->transfer_flags & USB_ASYNC_UNLINK) {
1167 int ret;
1169 spin_lock_irqsave (&s->urb_list_lock, flags);
1170 ret = uhci_unlink_urb_async(s, urb);
1171 spin_unlock_irqrestore (&s->urb_list_lock, flags);
1172 return ret;
1174 else
1175 return uhci_unlink_urb_sync(s, urb);
1177 /*-------------------------------------------------------------------*/
1178 // In case of ASAP iso transfer, search the URB-list for already queued URBs
1179 // for this EP and calculate the earliest start frame for the new
1180 // URB (easy seamless URB continuation!)
1181 _static int find_iso_limits (urb_t *urb, unsigned int *start, unsigned int *end)
1183 urb_t *u, *last_urb = NULL;
1184 uhci_t *s = (uhci_t*) urb->dev->bus->hcpriv;
1185 struct list_head *p;
1186 int ret=-1;
1187 unsigned long flags;
1189 spin_lock_irqsave (&s->urb_list_lock, flags);
1190 p=s->urb_list.prev;
1192 for (; p != &s->urb_list; p = p->prev) {
1193 u = list_entry (p, urb_t, urb_list);
1194 // look for pending URBs with identical pipe handle
1195 // works only because iso doesn't toggle the data bit!
1196 if ((urb->pipe == u->pipe) && (urb->dev == u->dev) && (u->status == -EINPROGRESS)) {
1197 if (!last_urb)
1198 *start = u->start_frame;
1199 last_urb = u;
1203 if (last_urb) {
1204 *end = (last_urb->start_frame + last_urb->number_of_packets) & 1023;
1205 ret=0;
1208 spin_unlock_irqrestore(&s->urb_list_lock, flags);
1210 return ret;
1212 /*-------------------------------------------------------------------*/
1213 // adjust start_frame according to scheduling constraints (ASAP etc)
1215 _static int iso_find_start (urb_t *urb)
1217 uhci_t *s = (uhci_t*) urb->dev->bus->hcpriv;
1218 unsigned int now;
1219 unsigned int start_limit = 0, stop_limit = 0, queued_size;
1220 int limits;
1222 now = UHCI_GET_CURRENT_FRAME (s) & 1023;
1224 if ((unsigned) urb->number_of_packets > 900)
1225 return -EFBIG;
1227 limits = find_iso_limits (urb, &start_limit, &stop_limit);
1228 queued_size = (stop_limit - start_limit) & 1023;
1230 if (urb->transfer_flags & USB_ISO_ASAP) {
1231 // first iso
1232 if (limits) {
1233 // 10ms setup should be enough //FIXME!
1234 urb->start_frame = (now + 10) & 1023;
1236 else {
1237 urb->start_frame = stop_limit; //seamless linkage
1239 if (((now - urb->start_frame) & 1023) <= (unsigned) urb->number_of_packets) {
1240 info("iso_find_start: gap in seamless isochronous scheduling");
1241 dbg("iso_find_start: now %u start_frame %u number_of_packets %u pipe 0x%08x",
1242 now, urb->start_frame, urb->number_of_packets, urb->pipe);
1243 urb->start_frame = (now + 5) & 1023; // 5ms setup should be enough //FIXME!
1247 else {
1248 urb->start_frame &= 1023;
1249 if (((now - urb->start_frame) & 1023) < (unsigned) urb->number_of_packets) {
1250 dbg("iso_find_start: now between start_frame and end");
1251 return -EAGAIN;
1255 /* check if either start_frame or start_frame+number_of_packets-1 lies between start_limit and stop_limit */
1256 if (limits)
1257 return 0;
1259 if (((urb->start_frame - start_limit) & 1023) < queued_size ||
1260 ((urb->start_frame + urb->number_of_packets - 1 - start_limit) & 1023) < queued_size) {
1261 dbg("iso_find_start: start_frame %u number_of_packets %u start_limit %u stop_limit %u",
1262 urb->start_frame, urb->number_of_packets, start_limit, stop_limit);
1263 return -EAGAIN;
1266 return 0;
1268 /*-------------------------------------------------------------------*/
1269 // submits USB interrupt (ie. polling ;-)
1270 // ASAP-flag set implicitely
1271 // if period==0, the the transfer is only done once
1273 _static int uhci_submit_int_urb (urb_t *urb)
1275 uhci_t *s = (uhci_t*) urb->dev->bus->hcpriv;
1276 urb_priv_t *urb_priv = urb->hcpriv;
1277 int nint, n, ret;
1278 uhci_desc_t *td;
1279 int status, destination;
1280 int info;
1281 unsigned int pipe = urb->pipe;
1283 if (urb->interval < 0 || urb->interval >= 256)
1284 return -EINVAL;
1286 if (urb->interval == 0)
1287 nint = 0;
1288 else {
1289 for (nint = 0, n = 1; nint <= 8; nint++, n += n) // round interval down to 2^n
1291 if (urb->interval < n) {
1292 urb->interval = n / 2;
1293 break;
1296 nint--;
1299 dbg("Rounded interval to %i, chain %i", urb->interval, nint);
1301 urb->start_frame = UHCI_GET_CURRENT_FRAME (s) & 1023; // remember start frame, just in case...
1303 urb->number_of_packets = 1;
1305 // INT allows only one packet
1306 if (urb->transfer_buffer_length > usb_maxpacket (urb->dev, pipe, usb_pipeout (pipe)))
1307 return -EINVAL;
1309 ret = alloc_td (&td, UHCI_PTR_DEPTH);
1311 if (ret)
1312 return -ENOMEM;
1314 status = (pipe & TD_CTRL_LS) | TD_CTRL_ACTIVE | TD_CTRL_IOC |
1315 (urb->transfer_flags & USB_DISABLE_SPD ? 0 : TD_CTRL_SPD) | (3 << 27);
1317 destination = (urb->pipe & PIPE_DEVEP_MASK) | usb_packetid (urb->pipe) |
1318 (((urb->transfer_buffer_length - 1) & 0x7ff) << 21);
1321 info = destination | (usb_gettoggle (urb->dev, usb_pipeendpoint (pipe), usb_pipeout (pipe)) << TD_TOKEN_TOGGLE);
1323 fill_td (td, status, info, virt_to_bus (urb->transfer_buffer));
1324 list_add_tail (&td->desc_list, &urb_priv->desc_list);
1326 urb->status = -EINPROGRESS;
1327 queue_urb (s, urb);
1329 insert_td_horizontal (s, s->int_chain[nint], td); // store in INT-TDs
1331 usb_dotoggle (urb->dev, usb_pipeendpoint (pipe), usb_pipeout (pipe));
1333 return 0;
1335 /*-------------------------------------------------------------------*/
1336 _static int uhci_submit_iso_urb (urb_t *urb)
1338 uhci_t *s = (uhci_t*) urb->dev->bus->hcpriv;
1339 urb_priv_t *urb_priv = urb->hcpriv;
1340 int pipe=urb->pipe;
1341 int maxsze = usb_maxpacket (urb->dev, pipe, usb_pipeout (pipe));
1342 int n, ret, last=0;
1343 uhci_desc_t *td, **tdm;
1344 int status, destination;
1345 unsigned long flags;
1347 __save_flags(flags);
1348 __cli(); // Disable IRQs to schedule all ISO-TDs in time
1349 ret = iso_find_start (urb); // adjusts urb->start_frame for later use
1351 if (ret)
1352 goto err;
1354 tdm = (uhci_desc_t **) kmalloc (urb->number_of_packets * sizeof (uhci_desc_t*), KMALLOC_FLAG);
1356 if (!tdm) {
1357 ret = -ENOMEM;
1358 goto err;
1361 // First try to get all TDs
1362 for (n = 0; n < urb->number_of_packets; n++) {
1363 dbg("n:%d urb->iso_frame_desc[n].length:%d", n, urb->iso_frame_desc[n].length);
1364 if (!urb->iso_frame_desc[n].length) {
1365 // allows ISO striping by setting length to zero in iso_descriptor
1366 tdm[n] = 0;
1367 continue;
1370 if(urb->iso_frame_desc[n].length > maxsze) {
1371 #ifdef ISO_SANITY_CHECK
1372 err("submit_iso: urb->iso_frame_desc[%d].length(%d)>%d",n , urb->iso_frame_desc[n].length, maxsze);
1373 tdm[n] = 0;
1374 ret=-EINVAL;
1375 goto inval;
1376 #endif
1379 ret = alloc_td (&td, UHCI_PTR_DEPTH);
1380 inval:
1381 if (ret) {
1382 int i; // Cleanup allocated TDs
1384 for (i = 0; i < n; n++)
1385 if (tdm[i])
1386 delete_desc(tdm[i]);
1387 kfree (tdm);
1388 goto err;
1390 last=n;
1391 tdm[n] = td;
1394 status = TD_CTRL_ACTIVE | TD_CTRL_IOS; //| (urb->transfer_flags&USB_DISABLE_SPD?0:TD_CTRL_SPD);
1396 destination = (urb->pipe & PIPE_DEVEP_MASK) | usb_packetid (urb->pipe);
1399 // Queue all allocated TDs
1400 for (n = 0; n < urb->number_of_packets; n++) {
1401 td = tdm[n];
1402 if (!td)
1403 continue;
1405 if (n == last)
1406 status |= TD_CTRL_IOC;
1408 fill_td (td, status, destination | (((urb->iso_frame_desc[n].length - 1) & 0x7ff) << 21),
1409 virt_to_bus (urb->transfer_buffer + urb->iso_frame_desc[n].offset));
1410 list_add_tail (&td->desc_list, &urb_priv->desc_list);
1412 if (n == last) {
1413 urb->status = -EINPROGRESS;
1414 queue_urb (s, urb);
1416 insert_td_horizontal (s, s->iso_td[(urb->start_frame + n) & 1023], td); // store in iso-tds
1417 //uhci_show_td(td);
1421 kfree (tdm);
1422 dbg("ISO-INT# %i, start %i, now %i", urb->number_of_packets, urb->start_frame, UHCI_GET_CURRENT_FRAME (s) & 1023);
1423 ret = 0;
1425 err:
1426 __restore_flags(flags);
1427 return ret;
1430 /*-------------------------------------------------------------------*/
1431 // returns: 0 (no transfer queued), urb* (this urb already queued)
1433 _static urb_t* search_dev_ep (uhci_t *s, urb_t *urb)
1435 struct list_head *p;
1436 urb_t *tmp;
1437 unsigned int mask = usb_pipecontrol(urb->pipe) ? (~USB_DIR_IN) : (~0);
1439 dbg("search_dev_ep:");
1441 p=s->urb_list.next;
1443 for (; p != &s->urb_list; p = p->next) {
1444 tmp = list_entry (p, urb_t, urb_list);
1445 dbg("urb: %p", tmp);
1446 // we can accept this urb if it is not queued at this time
1447 // or if non-iso transfer requests should be scheduled for the same device and pipe
1448 if ((!usb_pipeisoc(urb->pipe) && (tmp->dev == urb->dev) && !((tmp->pipe ^ urb->pipe) & mask)) ||
1449 (urb == tmp)) {
1450 return tmp; // found another urb already queued for processing
1454 return 0;
1456 /*-------------------------------------------------------------------*/
1457 _static int uhci_submit_urb (urb_t *urb)
1459 uhci_t *s;
1460 urb_priv_t *urb_priv;
1461 int ret = 0;
1462 unsigned long flags;
1463 urb_t *bulk_urb=NULL;
1465 if (!urb->dev || !urb->dev->bus)
1466 return -ENODEV;
1468 s = (uhci_t*) urb->dev->bus->hcpriv;
1469 //dbg("submit_urb: %p type %d",urb,usb_pipetype(urb->pipe));
1471 if (!s->running)
1472 return -ENODEV;
1474 if (usb_pipedevice (urb->pipe) == s->rh.devnum)
1475 return rh_submit_urb (urb); /* virtual root hub */
1477 usb_inc_dev_use (urb->dev);
1479 spin_lock_irqsave (&s->urb_list_lock, flags);
1481 bulk_urb = search_dev_ep (s, urb);
1483 if (bulk_urb) {
1485 queue_dbg("found bulk urb %p\n",bulk_urb);
1487 if ((usb_pipetype (urb->pipe) != PIPE_BULK) ||
1488 ((usb_pipetype (urb->pipe) == PIPE_BULK) &&
1489 (!(urb->transfer_flags & USB_QUEUE_BULK) || !(bulk_urb->transfer_flags & USB_QUEUE_BULK)))) {
1490 spin_unlock_irqrestore (&s->urb_list_lock, flags);
1491 usb_dec_dev_use (urb->dev);
1492 err("ENXIO1 %08x, flags %x, urb %p, burb %p",urb->pipe,urb->transfer_flags,urb,bulk_urb);
1493 return -ENXIO; // urb already queued
1497 #ifdef DEBUG_SLAB
1498 urb_priv = kmem_cache_alloc(urb_priv_kmem, SLAB_FLAG);
1499 #else
1500 urb_priv = kmalloc (sizeof (urb_priv_t), KMALLOC_FLAG);
1501 #endif
1502 if (!urb_priv) {
1503 usb_dec_dev_use (urb->dev);
1504 spin_unlock_irqrestore (&s->urb_list_lock, flags);
1505 return -ENOMEM;
1508 urb->hcpriv = urb_priv;
1509 INIT_LIST_HEAD (&urb_priv->desc_list);
1510 urb_priv->short_control_packet = 0;
1511 dbg("submit_urb: scheduling %p", urb);
1512 urb_priv->next_queued_urb = NULL;
1513 urb_priv->prev_queued_urb = NULL;
1514 urb_priv->bottom_qh = NULL;
1515 urb_priv->next_qh = NULL;
1517 if (usb_pipetype (urb->pipe) == PIPE_BULK) {
1519 if (bulk_urb) {
1520 while (((urb_priv_t*)bulk_urb->hcpriv)->next_queued_urb) // find last queued bulk
1521 bulk_urb=((urb_priv_t*)bulk_urb->hcpriv)->next_queued_urb;
1523 ((urb_priv_t*)bulk_urb->hcpriv)->next_queued_urb=urb;
1525 atomic_inc (&s->avoid_bulk);
1526 ret = uhci_submit_bulk_urb (urb, bulk_urb);
1527 atomic_dec (&s->avoid_bulk);
1528 spin_unlock_irqrestore (&s->urb_list_lock, flags);
1530 else {
1531 spin_unlock_irqrestore (&s->urb_list_lock, flags);
1532 switch (usb_pipetype (urb->pipe)) {
1533 case PIPE_ISOCHRONOUS:
1534 ret = uhci_submit_iso_urb (urb);
1535 break;
1536 case PIPE_INTERRUPT:
1537 ret = uhci_submit_int_urb (urb);
1538 break;
1539 case PIPE_CONTROL:
1540 ret = uhci_submit_control_urb (urb);
1541 break;
1542 default:
1543 ret = -EINVAL;
1547 dbg("submit_urb: scheduled with ret: %d", ret);
1549 if (ret != 0) {
1550 usb_dec_dev_use (urb->dev);
1551 #ifdef DEBUG_SLAB
1552 kmem_cache_free(urb_priv_kmem, urb_priv);
1553 #else
1554 kfree (urb_priv);
1555 #endif
1556 return ret;
1559 return 0;
1562 // Checks for URB timeout and removes bandwidth reclamation
1563 // if URB idles too long
1564 _static void uhci_check_timeouts(uhci_t *s)
1566 struct list_head *p,*p2;
1567 urb_t *urb;
1568 int type;
1570 p = s->urb_list.prev;
1572 while (p != &s->urb_list) {
1573 urb_priv_t *hcpriv;
1575 p2 = p;
1576 p = p->prev;
1577 urb = list_entry (p2, urb_t, urb_list);
1578 type = usb_pipetype (urb->pipe);
1580 hcpriv = (urb_priv_t*)urb->hcpriv;
1582 if ( urb->timeout &&
1583 ((hcpriv->started + urb->timeout) < jiffies)) {
1584 urb->transfer_flags |= USB_TIMEOUT_KILLED | USB_ASYNC_UNLINK;
1585 async_dbg("uhci_check_timeout: timeout for %p",urb);
1586 uhci_unlink_urb_async(s, urb);
1588 #ifdef CONFIG_USB_UHCI_HIGH_BANDWIDTH
1589 else if (((type == PIPE_BULK) || (type == PIPE_CONTROL)) &&
1590 (hcpriv->use_loop) &&
1591 ((hcpriv->started + IDLE_TIMEOUT) < jiffies))
1592 disable_desc_loop(s, urb);
1593 #endif
1598 /*-------------------------------------------------------------------
1599 Virtual Root Hub
1600 -------------------------------------------------------------------*/
1602 _static __u8 root_hub_dev_des[] =
1604 0x12, /* __u8 bLength; */
1605 0x01, /* __u8 bDescriptorType; Device */
1606 0x00, /* __u16 bcdUSB; v1.0 */
1607 0x01,
1608 0x09, /* __u8 bDeviceClass; HUB_CLASSCODE */
1609 0x00, /* __u8 bDeviceSubClass; */
1610 0x00, /* __u8 bDeviceProtocol; */
1611 0x08, /* __u8 bMaxPacketSize0; 8 Bytes */
1612 0x00, /* __u16 idVendor; */
1613 0x00,
1614 0x00, /* __u16 idProduct; */
1615 0x00,
1616 0x00, /* __u16 bcdDevice; */
1617 0x00,
1618 0x00, /* __u8 iManufacturer; */
1619 0x00, /* __u8 iProduct; */
1620 0x00, /* __u8 iSerialNumber; */
1621 0x01 /* __u8 bNumConfigurations; */
1625 /* Configuration descriptor */
1626 _static __u8 root_hub_config_des[] =
1628 0x09, /* __u8 bLength; */
1629 0x02, /* __u8 bDescriptorType; Configuration */
1630 0x19, /* __u16 wTotalLength; */
1631 0x00,
1632 0x01, /* __u8 bNumInterfaces; */
1633 0x01, /* __u8 bConfigurationValue; */
1634 0x00, /* __u8 iConfiguration; */
1635 0x40, /* __u8 bmAttributes;
1636 Bit 7: Bus-powered, 6: Self-powered, 5 Remote-wakwup, 4..0: resvd */
1637 0x00, /* __u8 MaxPower; */
1639 /* interface */
1640 0x09, /* __u8 if_bLength; */
1641 0x04, /* __u8 if_bDescriptorType; Interface */
1642 0x00, /* __u8 if_bInterfaceNumber; */
1643 0x00, /* __u8 if_bAlternateSetting; */
1644 0x01, /* __u8 if_bNumEndpoints; */
1645 0x09, /* __u8 if_bInterfaceClass; HUB_CLASSCODE */
1646 0x00, /* __u8 if_bInterfaceSubClass; */
1647 0x00, /* __u8 if_bInterfaceProtocol; */
1648 0x00, /* __u8 if_iInterface; */
1650 /* endpoint */
1651 0x07, /* __u8 ep_bLength; */
1652 0x05, /* __u8 ep_bDescriptorType; Endpoint */
1653 0x81, /* __u8 ep_bEndpointAddress; IN Endpoint 1 */
1654 0x03, /* __u8 ep_bmAttributes; Interrupt */
1655 0x08, /* __u16 ep_wMaxPacketSize; 8 Bytes */
1656 0x00,
1657 0xff /* __u8 ep_bInterval; 255 ms */
1661 _static __u8 root_hub_hub_des[] =
1663 0x09, /* __u8 bLength; */
1664 0x29, /* __u8 bDescriptorType; Hub-descriptor */
1665 0x02, /* __u8 bNbrPorts; */
1666 0x00, /* __u16 wHubCharacteristics; */
1667 0x00,
1668 0x01, /* __u8 bPwrOn2pwrGood; 2ms */
1669 0x00, /* __u8 bHubContrCurrent; 0 mA */
1670 0x00, /* __u8 DeviceRemovable; *** 7 Ports max *** */
1671 0xff /* __u8 PortPwrCtrlMask; *** 7 ports max *** */
1674 /*-------------------------------------------------------------------------*/
1675 /* prepare Interrupt pipe transaction data; HUB INTERRUPT ENDPOINT */
1676 _static int rh_send_irq (urb_t *urb)
1678 int len = 1;
1679 int i;
1680 uhci_t *uhci = urb->dev->bus->hcpriv;
1681 unsigned int io_addr = uhci->io_addr;
1682 __u16 data = 0;
1684 for (i = 0; i < uhci->rh.numports; i++) {
1685 data |= ((inw (io_addr + USBPORTSC1 + i * 2) & 0xa) > 0 ? (1 << (i + 1)) : 0);
1686 len = (i + 1) / 8 + 1;
1689 *(__u16 *) urb->transfer_buffer = cpu_to_le16 (data);
1690 urb->actual_length = len;
1691 urb->status = 0;
1693 if ((data > 0) && (uhci->rh.send != 0)) {
1694 dbg("Root-Hub INT complete: port1: %x port2: %x data: %x",
1695 inw (io_addr + USBPORTSC1), inw (io_addr + USBPORTSC2), data);
1696 urb->complete (urb);
1698 return 0;
1701 /*-------------------------------------------------------------------------*/
1702 /* Virtual Root Hub INTs are polled by this timer every "intervall" ms */
1703 _static int rh_init_int_timer (urb_t *urb);
1705 _static void rh_int_timer_do (unsigned long ptr)
1707 int len;
1708 urb_t *urb = (urb_t*) ptr;
1709 uhci_t *uhci = urb->dev->bus->hcpriv;
1711 if (uhci->rh.send) {
1712 len = rh_send_irq (urb);
1713 if (len > 0) {
1714 urb->actual_length = len;
1715 if (urb->complete)
1716 urb->complete (urb);
1719 rh_init_int_timer (urb);
1722 /*-------------------------------------------------------------------------*/
1723 /* Root Hub INTs are polled by this timer, polling interval 20ms */
1724 /* This time is also used for URB-timeout checking */
1726 _static int rh_init_int_timer (urb_t *urb)
1728 uhci_t *uhci = urb->dev->bus->hcpriv;
1730 uhci->rh.interval = urb->interval;
1731 init_timer (&uhci->rh.rh_int_timer);
1732 uhci->rh.rh_int_timer.function = rh_int_timer_do;
1733 uhci->rh.rh_int_timer.data = (unsigned long) urb;
1734 uhci->rh.rh_int_timer.expires = jiffies + (HZ * 20) / 1000;
1735 add_timer (&uhci->rh.rh_int_timer);
1737 return 0;
1740 /*-------------------------------------------------------------------------*/
1741 #define OK(x) len = (x); break
1743 #define CLR_RH_PORTSTAT(x) \
1744 status = inw(io_addr+USBPORTSC1+2*(wIndex-1)); \
1745 status = (status & 0xfff5) & ~(x); \
1746 outw(status, io_addr+USBPORTSC1+2*(wIndex-1))
1748 #define SET_RH_PORTSTAT(x) \
1749 status = inw(io_addr+USBPORTSC1+2*(wIndex-1)); \
1750 status = (status & 0xfff5) | (x); \
1751 outw(status, io_addr+USBPORTSC1+2*(wIndex-1))
1754 /*-------------------------------------------------------------------------*/
1755 /****
1756 ** Root Hub Control Pipe
1757 *************************/
1760 _static int rh_submit_urb (urb_t *urb)
1762 struct usb_device *usb_dev = urb->dev;
1763 uhci_t *uhci = usb_dev->bus->hcpriv;
1764 unsigned int pipe = urb->pipe;
1765 devrequest *cmd = (devrequest *) urb->setup_packet;
1766 void *data = urb->transfer_buffer;
1767 int leni = urb->transfer_buffer_length;
1768 int len = 0;
1769 int status = 0;
1770 int stat = 0;
1771 int i;
1772 unsigned int io_addr = uhci->io_addr;
1773 __u16 cstatus;
1775 __u16 bmRType_bReq;
1776 __u16 wValue;
1777 __u16 wIndex;
1778 __u16 wLength;
1780 if (usb_pipetype (pipe) == PIPE_INTERRUPT) {
1781 dbg("Root-Hub submit IRQ: every %d ms", urb->interval);
1782 uhci->rh.urb = urb;
1783 uhci->rh.send = 1;
1784 uhci->rh.interval = urb->interval;
1785 rh_init_int_timer (urb);
1787 return 0;
1791 bmRType_bReq = cmd->requesttype | cmd->request << 8;
1792 wValue = le16_to_cpu (cmd->value);
1793 wIndex = le16_to_cpu (cmd->index);
1794 wLength = le16_to_cpu (cmd->length);
1796 for (i = 0; i < 8; i++)
1797 uhci->rh.c_p_r[i] = 0;
1799 dbg("Root-Hub: adr: %2x cmd(%1x): %04x %04x %04x %04x",
1800 uhci->rh.devnum, 8, bmRType_bReq, wValue, wIndex, wLength);
1802 switch (bmRType_bReq) {
1803 /* Request Destination:
1804 without flags: Device,
1805 RH_INTERFACE: interface,
1806 RH_ENDPOINT: endpoint,
1807 RH_CLASS means HUB here,
1808 RH_OTHER | RH_CLASS almost ever means HUB_PORT here
1811 case RH_GET_STATUS:
1812 *(__u16 *) data = cpu_to_le16 (1);
1813 OK (2);
1814 case RH_GET_STATUS | RH_INTERFACE:
1815 *(__u16 *) data = cpu_to_le16 (0);
1816 OK (2);
1817 case RH_GET_STATUS | RH_ENDPOINT:
1818 *(__u16 *) data = cpu_to_le16 (0);
1819 OK (2);
1820 case RH_GET_STATUS | RH_CLASS:
1821 *(__u32 *) data = cpu_to_le32 (0);
1822 OK (4); /* hub power ** */
1823 case RH_GET_STATUS | RH_OTHER | RH_CLASS:
1824 status = inw (io_addr + USBPORTSC1 + 2 * (wIndex - 1));
1825 cstatus = ((status & USBPORTSC_CSC) >> (1 - 0)) |
1826 ((status & USBPORTSC_PEC) >> (3 - 1)) |
1827 (uhci->rh.c_p_r[wIndex - 1] << (0 + 4));
1828 status = (status & USBPORTSC_CCS) |
1829 ((status & USBPORTSC_PE) >> (2 - 1)) |
1830 ((status & USBPORTSC_SUSP) >> (12 - 2)) |
1831 ((status & USBPORTSC_PR) >> (9 - 4)) |
1832 (1 << 8) | /* power on ** */
1833 ((status & USBPORTSC_LSDA) << (-8 + 9));
1835 *(__u16 *) data = cpu_to_le16 (status);
1836 *(__u16 *) (data + 2) = cpu_to_le16 (cstatus);
1837 OK (4);
1839 case RH_CLEAR_FEATURE | RH_ENDPOINT:
1840 switch (wValue) {
1841 case (RH_ENDPOINT_STALL):
1842 OK (0);
1844 break;
1846 case RH_CLEAR_FEATURE | RH_CLASS:
1847 switch (wValue) {
1848 case (RH_C_HUB_OVER_CURRENT):
1849 OK (0); /* hub power over current ** */
1851 break;
1853 case RH_CLEAR_FEATURE | RH_OTHER | RH_CLASS:
1854 switch (wValue) {
1855 case (RH_PORT_ENABLE):
1856 CLR_RH_PORTSTAT (USBPORTSC_PE);
1857 OK (0);
1858 case (RH_PORT_SUSPEND):
1859 CLR_RH_PORTSTAT (USBPORTSC_SUSP);
1860 OK (0);
1861 case (RH_PORT_POWER):
1862 OK (0); /* port power ** */
1863 case (RH_C_PORT_CONNECTION):
1864 SET_RH_PORTSTAT (USBPORTSC_CSC);
1865 OK (0);
1866 case (RH_C_PORT_ENABLE):
1867 SET_RH_PORTSTAT (USBPORTSC_PEC);
1868 OK (0);
1869 case (RH_C_PORT_SUSPEND):
1870 /*** WR_RH_PORTSTAT(RH_PS_PSSC); */
1871 OK (0);
1872 case (RH_C_PORT_OVER_CURRENT):
1873 OK (0); /* port power over current ** */
1874 case (RH_C_PORT_RESET):
1875 uhci->rh.c_p_r[wIndex - 1] = 0;
1876 OK (0);
1878 break;
1880 case RH_SET_FEATURE | RH_OTHER | RH_CLASS:
1881 switch (wValue) {
1882 case (RH_PORT_SUSPEND):
1883 SET_RH_PORTSTAT (USBPORTSC_SUSP);
1884 OK (0);
1885 case (RH_PORT_RESET):
1886 SET_RH_PORTSTAT (USBPORTSC_PR);
1887 uhci_wait_ms (10);
1888 uhci->rh.c_p_r[wIndex - 1] = 1;
1889 CLR_RH_PORTSTAT (USBPORTSC_PR);
1890 udelay (10);
1891 SET_RH_PORTSTAT (USBPORTSC_PE);
1892 uhci_wait_ms (10);
1893 SET_RH_PORTSTAT (0xa);
1894 OK (0);
1895 case (RH_PORT_POWER):
1896 OK (0); /* port power ** */
1897 case (RH_PORT_ENABLE):
1898 SET_RH_PORTSTAT (USBPORTSC_PE);
1899 OK (0);
1901 break;
1903 case RH_SET_ADDRESS:
1904 uhci->rh.devnum = wValue;
1905 OK (0);
1907 case RH_GET_DESCRIPTOR:
1908 switch ((wValue & 0xff00) >> 8) {
1909 case (0x01): /* device descriptor */
1910 len = min (leni, min (sizeof (root_hub_dev_des), wLength));
1911 memcpy (data, root_hub_dev_des, len);
1912 OK (len);
1913 case (0x02): /* configuration descriptor */
1914 len = min (leni, min (sizeof (root_hub_config_des), wLength));
1915 memcpy (data, root_hub_config_des, len);
1916 OK (len);
1917 case (0x03): /*string descriptors */
1918 stat = -EPIPE;
1920 break;
1922 case RH_GET_DESCRIPTOR | RH_CLASS:
1923 root_hub_hub_des[2] = uhci->rh.numports;
1924 len = min (leni, min (sizeof (root_hub_hub_des), wLength));
1925 memcpy (data, root_hub_hub_des, len);
1926 OK (len);
1928 case RH_GET_CONFIGURATION:
1929 *(__u8 *) data = 0x01;
1930 OK (1);
1932 case RH_SET_CONFIGURATION:
1933 OK (0);
1934 default:
1935 stat = -EPIPE;
1938 dbg("Root-Hub stat port1: %x port2: %x",
1939 inw (io_addr + USBPORTSC1), inw (io_addr + USBPORTSC2));
1941 urb->actual_length = len;
1942 urb->status = stat;
1943 if (urb->complete)
1944 urb->complete (urb);
1945 return 0;
1947 /*-------------------------------------------------------------------------*/
1949 _static int rh_unlink_urb (urb_t *urb)
1951 uhci_t *uhci = urb->dev->bus->hcpriv;
1953 if (uhci->rh.urb==urb) {
1954 dbg("Root-Hub unlink IRQ");
1955 uhci->rh.send = 0;
1956 del_timer (&uhci->rh.rh_int_timer);
1958 return 0;
1960 /*-------------------------------------------------------------------*/
1963 * Map status to standard result codes
1965 * <status> is (td->status & 0xFE0000) [a.k.a. uhci_status_bits(td->status)
1966 * <dir_out> is True for output TDs and False for input TDs.
1968 _static int uhci_map_status (int status, int dir_out)
1970 if (!status)
1971 return 0;
1972 if (status & TD_CTRL_BITSTUFF) /* Bitstuff error */
1973 return -EPROTO;
1974 if (status & TD_CTRL_CRCTIMEO) { /* CRC/Timeout */
1975 if (dir_out)
1976 return -ETIMEDOUT;
1977 else
1978 return -EILSEQ;
1980 if (status & TD_CTRL_NAK) /* NAK */
1981 return -ETIMEDOUT;
1982 if (status & TD_CTRL_BABBLE) /* Babble */
1983 return -EPIPE;
1984 if (status & TD_CTRL_DBUFERR) /* Buffer error */
1985 return -ENOSR;
1986 if (status & TD_CTRL_STALLED) /* Stalled */
1987 return -EPIPE;
1988 if (status & TD_CTRL_ACTIVE) /* Active */
1989 return 0;
1991 return -EPROTO;
1995 * Only the USB core should call uhci_alloc_dev and uhci_free_dev
1997 _static int uhci_alloc_dev (struct usb_device *usb_dev)
1999 return 0;
2002 _static void uhci_unlink_urbs(uhci_t *s, struct usb_device *usb_dev, int remove_all)
2004 unsigned long flags;
2005 struct list_head *p;
2006 struct list_head *p2;
2007 urb_t *urb;
2009 spin_lock_irqsave (&s->urb_list_lock, flags);
2010 p = s->urb_list.prev;
2011 while (p != &s->urb_list) {
2012 p2 = p;
2013 p = p->prev ;
2014 urb = list_entry (p2, urb_t, urb_list);
2015 dbg("urb: %p, dev %p, %p", urb, usb_dev,urb->dev);
2017 //urb->transfer_flags |=USB_ASYNC_UNLINK;
2019 if (remove_all || (usb_dev == urb->dev)) {
2020 spin_unlock_irqrestore (&s->urb_list_lock, flags);
2021 warn("forced removing of queued URB %p due to disconnect",urb);
2022 uhci_unlink_urb(urb);
2023 urb->dev = NULL; // avoid further processing of this UR
2024 spin_lock_irqsave (&s->urb_list_lock, flags);
2025 p = s->urb_list.prev;
2028 spin_unlock_irqrestore (&s->urb_list_lock, flags);
2031 _static int uhci_free_dev (struct usb_device *usb_dev)
2033 uhci_t *s;
2036 if(!usb_dev || !usb_dev->bus || !usb_dev->bus->hcpriv)
2037 return -EINVAL;
2039 s=(uhci_t*) usb_dev->bus->hcpriv;
2040 uhci_unlink_urbs(s, usb_dev, 0);
2042 return 0;
2046 * uhci_get_current_frame_number()
2048 * returns the current frame number for a USB bus/controller.
2050 _static int uhci_get_current_frame_number (struct usb_device *usb_dev)
2052 return UHCI_GET_CURRENT_FRAME ((uhci_t*) usb_dev->bus->hcpriv);
2055 struct usb_operations uhci_device_operations =
2057 uhci_alloc_dev,
2058 uhci_free_dev,
2059 uhci_get_current_frame_number,
2060 uhci_submit_urb,
2061 uhci_unlink_urb
2065 * For IN-control transfers, process_transfer gets a bit more complicated,
2066 * since there are devices that return less data (eg. strings) than they
2067 * have announced. This leads to a queue abort due to the short packet,
2068 * the status stage is not executed. If this happens, the status stage
2069 * is manually re-executed.
2070 * mode: 0: QHs already unlinked
2073 _static int process_transfer (uhci_t *s, urb_t *urb, int mode)
2075 int ret = 0;
2076 urb_priv_t *urb_priv = urb->hcpriv;
2077 struct list_head *qhl = urb_priv->desc_list.next;
2078 uhci_desc_t *qh = list_entry (qhl, uhci_desc_t, desc_list);
2079 struct list_head *p = qh->vertical.next;
2080 uhci_desc_t *desc= list_entry (urb_priv->desc_list.prev, uhci_desc_t, desc_list);
2081 uhci_desc_t *last_desc = list_entry (desc->vertical.prev, uhci_desc_t, vertical);
2082 int data_toggle = usb_gettoggle (urb->dev, usb_pipeendpoint (urb->pipe), usb_pipeout (urb->pipe)); // save initial data_toggle
2083 int maxlength; // extracted and remapped info from TD
2084 int actual_length;
2085 int status = 0;
2087 //dbg("process_transfer: urb contains bulk/control request");
2089 /* if the status phase has been retriggered and the
2090 queue is empty or the last status-TD is inactive, the retriggered
2091 status stage is completed
2094 if (urb_priv->short_control_packet &&
2095 ((qh->hw.qh.element == UHCI_PTR_TERM) ||(!(last_desc->hw.td.status & TD_CTRL_ACTIVE))))
2096 goto transfer_finished;
2098 urb->actual_length=0;
2100 for (; p != &qh->vertical; p = p->next) {
2101 desc = list_entry (p, uhci_desc_t, vertical);
2103 if (desc->hw.td.status & TD_CTRL_ACTIVE) // do not process active TDs
2104 return ret;
2106 actual_length = (desc->hw.td.status + 1) & 0x7ff; // extract transfer parameters from TD
2107 maxlength = (((desc->hw.td.info >> 21) & 0x7ff) + 1) & 0x7ff;
2108 status = uhci_map_status (uhci_status_bits (desc->hw.td.status), usb_pipeout (urb->pipe));
2110 if (status == -EPIPE) { // see if EP is stalled
2111 // set up stalled condition
2112 usb_endpoint_halt (urb->dev, usb_pipeendpoint (urb->pipe), usb_pipeout (urb->pipe));
2115 if (status != 0) { // if any error occured stop processing of further TDs
2116 // only set ret if status returned an error
2117 if (status != -EPIPE)
2118 uhci_show_td (desc);
2119 ret = status;
2120 urb->error_count++;
2121 break;
2123 else if ((desc->hw.td.info & 0xff) != USB_PID_SETUP)
2124 urb->actual_length += actual_length;
2126 // got less data than requested
2127 if ( (actual_length < maxlength)) {
2128 if (urb->transfer_flags & USB_DISABLE_SPD) {
2129 status = -EREMOTEIO; // treat as real error
2130 dbg("process_transfer: SPD!!");
2131 break; // exit after this TD because SP was detected
2134 // short read during control-IN: re-start status stage
2135 if ((usb_pipetype (urb->pipe) == PIPE_CONTROL)) {
2136 if (uhci_packetid(last_desc->hw.td.info) == USB_PID_OUT) {
2138 qh->hw.qh.element = virt_to_bus (last_desc); // re-trigger status stage
2139 dbg("short packet during control transfer, retrigger status stage @ %p",last_desc);
2140 //uhci_show_td (desc);
2141 //uhci_show_td (last_desc);
2142 urb_priv->short_control_packet=1;
2143 return 0;
2146 // all other cases: short read is OK
2147 data_toggle = uhci_toggle (desc->hw.td.info);
2148 break;
2151 data_toggle = uhci_toggle (desc->hw.td.info);
2152 queue_dbg("process_transfer: len:%d status:%x mapped:%x toggle:%d", actual_length, desc->hw.td.status,status, data_toggle);
2156 usb_settoggle (urb->dev, usb_pipeendpoint (urb->pipe), usb_pipeout (urb->pipe), !data_toggle);
2158 transfer_finished:
2160 uhci_clean_transfer(s, urb, qh, (mode==0?2:1));
2162 urb->status = status;
2164 #ifdef CONFIG_USB_UHCI_HIGH_BANDWIDTH
2165 disable_desc_loop(s,urb);
2166 #endif
2168 queue_dbg("process_transfer: (end) urb %p, wanted len %d, len %d status %x err %d",
2169 urb,urb->transfer_buffer_length,urb->actual_length, urb->status, urb->error_count);
2170 return ret;
2173 _static int process_interrupt (uhci_t *s, urb_t *urb)
2175 int i, ret = -EINPROGRESS;
2176 urb_priv_t *urb_priv = urb->hcpriv;
2177 struct list_head *p = urb_priv->desc_list.next;
2178 uhci_desc_t *desc = list_entry (urb_priv->desc_list.prev, uhci_desc_t, desc_list);
2180 int actual_length;
2181 int status = 0;
2183 //dbg("urb contains interrupt request");
2185 for (i = 0; p != &urb_priv->desc_list; p = p->next, i++) // Maybe we allow more than one TD later ;-)
2187 desc = list_entry (p, uhci_desc_t, desc_list);
2189 if (desc->hw.td.status & TD_CTRL_ACTIVE) {
2190 // do not process active TDs
2191 //dbg("TD ACT Status @%p %08x",desc,desc->hw.td.status);
2192 break;
2195 if (!desc->hw.td.status & TD_CTRL_IOC) {
2196 // do not process one-shot TDs, no recycling
2197 break;
2199 // extract transfer parameters from TD
2201 actual_length = (desc->hw.td.status + 1) & 0x7ff;
2202 status = uhci_map_status (uhci_status_bits (desc->hw.td.status), usb_pipeout (urb->pipe));
2204 // see if EP is stalled
2205 if (status == -EPIPE) {
2206 // set up stalled condition
2207 usb_endpoint_halt (urb->dev, usb_pipeendpoint (urb->pipe), usb_pipeout (urb->pipe));
2210 // if any error occured: ignore this td, and continue
2211 if (status != 0) {
2212 //uhci_show_td (desc);
2213 urb->error_count++;
2214 goto recycle;
2216 else
2217 urb->actual_length = actual_length;
2219 recycle:
2220 if (urb->complete) {
2221 //dbg("process_interrupt: calling completion, status %i",status);
2222 urb->status = status;
2224 spin_unlock(&s->urb_list_lock);
2226 urb->complete ((struct urb *) urb);
2228 spin_lock(&s->urb_list_lock);
2230 urb->status = -EINPROGRESS;
2233 // Recycle INT-TD if interval!=0, else mark TD as one-shot
2234 if (urb->interval) {
2236 desc->hw.td.info &= ~(1 << TD_TOKEN_TOGGLE);
2237 if (status==0) {
2238 ((urb_priv_t*)urb->hcpriv)->started=jiffies;
2239 desc->hw.td.info |= (usb_gettoggle (urb->dev, usb_pipeendpoint (urb->pipe),
2240 usb_pipeout (urb->pipe)) << TD_TOKEN_TOGGLE);
2241 usb_dotoggle (urb->dev, usb_pipeendpoint (urb->pipe), usb_pipeout (urb->pipe));
2242 } else {
2243 desc->hw.td.info |= (!usb_gettoggle (urb->dev, usb_pipeendpoint (urb->pipe),
2244 usb_pipeout (urb->pipe)) << TD_TOKEN_TOGGLE);
2246 desc->hw.td.status= (urb->pipe & TD_CTRL_LS) | TD_CTRL_ACTIVE | TD_CTRL_IOC |
2247 (urb->transfer_flags & USB_DISABLE_SPD ? 0 : TD_CTRL_SPD) | (3 << 27);
2248 mb();
2250 else {
2251 desc->hw.td.status &= ~TD_CTRL_IOC; // inactivate TD
2255 return ret;
2258 // mode: 1: force processing, don't unlink tds (already unlinked)
2259 _static int process_iso (uhci_t *s, urb_t *urb, int mode)
2261 int i;
2262 int ret = 0;
2263 urb_priv_t *urb_priv = urb->hcpriv;
2264 struct list_head *p = urb_priv->desc_list.next;
2265 uhci_desc_t *desc = list_entry (urb_priv->desc_list.prev, uhci_desc_t, desc_list);
2267 dbg("urb contains iso request");
2268 if ((desc->hw.td.status & TD_CTRL_ACTIVE) && !mode)
2269 return -EXDEV; // last TD not finished
2271 urb->error_count = 0;
2272 urb->actual_length = 0;
2273 urb->status = 0;
2274 dbg("process iso urb %p, %li, %i, %i, %i %08x",urb,jiffies,UHCI_GET_CURRENT_FRAME(s),
2275 urb->number_of_packets,mode,desc->hw.td.status);
2277 for (i = 0; p != &urb_priv->desc_list; p = p->next, i++) {
2278 desc = list_entry (p, uhci_desc_t, desc_list);
2280 //uhci_show_td(desc);
2281 if (desc->hw.td.status & TD_CTRL_ACTIVE) {
2282 // means we have completed the last TD, but not the TDs before
2283 desc->hw.td.status &= ~TD_CTRL_ACTIVE;
2284 dbg("TD still active (%x)- grrr. paranoia!", desc->hw.td.status);
2285 ret = -EXDEV;
2286 urb->iso_frame_desc[i].status = ret;
2287 unlink_td (s, desc, 1);
2288 // FIXME: immediate deletion may be dangerous
2289 goto err;
2292 if (!mode)
2293 unlink_td (s, desc, 1);
2295 if (urb->number_of_packets <= i) {
2296 dbg("urb->number_of_packets (%d)<=(%d)", urb->number_of_packets, i);
2297 ret = -EINVAL;
2298 goto err;
2301 if (urb->iso_frame_desc[i].offset + urb->transfer_buffer != bus_to_virt (desc->hw.td.buffer)) {
2302 // Hm, something really weird is going on
2303 dbg("Pointer Paranoia: %p!=%p", urb->iso_frame_desc[i].offset + urb->transfer_buffer, bus_to_virt (desc->hw.td.buffer));
2304 ret = -EINVAL;
2305 urb->iso_frame_desc[i].status = ret;
2306 goto err;
2308 urb->iso_frame_desc[i].actual_length = (desc->hw.td.status + 1) & 0x7ff;
2309 urb->iso_frame_desc[i].status = uhci_map_status (uhci_status_bits (desc->hw.td.status), usb_pipeout (urb->pipe));
2310 urb->actual_length += urb->iso_frame_desc[i].actual_length;
2312 err:
2314 if (urb->iso_frame_desc[i].status != 0) {
2315 urb->error_count++;
2316 urb->status = urb->iso_frame_desc[i].status;
2318 dbg("process_iso: %i: len:%d %08x status:%x",
2319 i, urb->iso_frame_desc[i].actual_length, desc->hw.td.status,urb->iso_frame_desc[i].status);
2321 delete_desc (desc);
2322 list_del (p);
2325 dbg("process_iso: exit %i (%d), actual_len %i", i, ret,urb->actual_length);
2326 return ret;
2330 _static int process_urb (uhci_t *s, struct list_head *p)
2332 int ret = 0;
2333 urb_t *urb;
2336 urb=list_entry (p, urb_t, urb_list);
2337 //dbg("process_urb: found queued urb: %p", urb);
2339 switch (usb_pipetype (urb->pipe)) {
2340 case PIPE_CONTROL:
2341 ret = process_transfer (s, urb, 1);
2342 break;
2343 case PIPE_BULK:
2344 if (!s->avoid_bulk.counter)
2345 ret = process_transfer (s, urb, 1);
2346 else
2347 return 0;
2348 break;
2349 case PIPE_ISOCHRONOUS:
2350 ret = process_iso (s, urb, 0);
2351 break;
2352 case PIPE_INTERRUPT:
2353 ret = process_interrupt (s, urb);
2354 break;
2357 if (urb->status != -EINPROGRESS) {
2358 int proceed = 0;
2360 dbg("dequeued urb: %p", urb);
2361 dequeue_urb (s, urb);
2363 #ifdef DEBUG_SLAB
2364 kmem_cache_free(urb_priv_kmem, urb->hcpriv);
2365 #else
2366 kfree (urb->hcpriv);
2367 #endif
2369 if ((usb_pipetype (urb->pipe) != PIPE_INTERRUPT)) {
2370 urb_t *tmp = urb->next; // pointer to first urb
2371 int is_ring = 0;
2373 if (urb->next) {
2374 do {
2375 if (tmp->status != -EINPROGRESS) {
2376 proceed = 1;
2377 break;
2379 tmp = tmp->next;
2381 while (tmp != NULL && tmp != urb->next);
2382 if (tmp == urb->next)
2383 is_ring = 1;
2386 spin_unlock(&s->urb_list_lock);
2388 // In case you need the current URB status for your completion handler
2389 if (urb->complete && (!proceed || (urb->transfer_flags & USB_URB_EARLY_COMPLETE))) {
2390 dbg("process_transfer: calling early completion");
2391 urb->complete ((struct urb *) urb);
2392 if (!proceed && is_ring && (urb->status != -ENOENT))
2393 uhci_submit_urb (urb);
2396 if (proceed && urb->next) {
2397 // if there are linked urbs - handle submitting of them right now.
2398 tmp = urb->next; // pointer to first urb
2400 do {
2401 if ((tmp->status != -EINPROGRESS) && (tmp->status != -ENOENT) && uhci_submit_urb (tmp) != 0)
2402 break;
2403 tmp = tmp->next;
2405 while (tmp != NULL && tmp != urb->next); // submit until we reach NULL or our own pointer or submit fails
2407 if (urb->complete && !(urb->transfer_flags & USB_URB_EARLY_COMPLETE)) {
2408 dbg("process_transfer: calling completion");
2409 urb->complete ((struct urb *) urb);
2413 spin_lock(&s->urb_list_lock);
2415 usb_dec_dev_use (urb->dev);
2419 return ret;
2422 _static void uhci_interrupt (int irq, void *__uhci, struct pt_regs *regs)
2424 uhci_t *s = __uhci;
2425 unsigned int io_addr = s->io_addr;
2426 unsigned short status;
2427 struct list_head *p, *p2;
2430 * Read the interrupt status, and write it back to clear the
2431 * interrupt cause
2434 status = inw (io_addr + USBSTS);
2436 if (!status) /* shared interrupt, not mine */
2437 return;
2439 dbg("interrupt");
2441 if (status != 1) {
2442 warn("interrupt, status %x, frame# %i", status,
2443 UHCI_GET_CURRENT_FRAME(s));
2445 // remove host controller halted state
2446 if ((status&0x20) && (s->running)) {
2447 outw (USBCMD_RS | inw(io_addr + USBCMD), io_addr + USBCMD);
2449 //uhci_show_status (s);
2452 * traverse the list in *reverse* direction, because new entries
2453 * may be added at the end.
2454 * also, because process_urb may unlink the current urb,
2455 * we need to advance the list before
2458 spin_lock (&s->urb_list_lock);
2459 restart:
2460 s->unlink_urb_done=0;
2461 p = s->urb_list.prev;
2463 while (p != &s->urb_list) {
2464 p2 = p;
2465 p = p->prev;
2466 process_urb (s, p2);
2467 if (s->unlink_urb_done) {
2468 s->unlink_urb_done=0;
2469 goto restart;
2472 if ((s->frame_counter & 63) == 0)
2473 uhci_check_timeouts(s);
2475 clean_descs(s,0);
2476 uhci_cleanup_unlink(s, 0);
2478 spin_unlock (&s->urb_list_lock);
2480 s->frame_counter++;
2481 outw (status, io_addr + USBSTS);
2483 //dbg("uhci_interrupt: done");
2486 _static void reset_hc (uhci_t *s)
2488 unsigned int io_addr = s->io_addr;
2490 s->apm_state = 0;
2491 /* Global reset for 50ms */
2492 outw (USBCMD_GRESET, io_addr + USBCMD);
2493 uhci_wait_ms (50);
2494 outw (0, io_addr + USBCMD);
2495 uhci_wait_ms (10);
2498 _static void start_hc (uhci_t *s)
2500 unsigned int io_addr = s->io_addr;
2501 int timeout = 1000;
2504 * Reset the HC - this will force us to get a
2505 * new notification of any already connected
2506 * ports due to the virtual disconnect that it
2507 * implies.
2509 outw (USBCMD_HCRESET, io_addr + USBCMD);
2511 while (inw (io_addr + USBCMD) & USBCMD_HCRESET) {
2512 if (!--timeout) {
2513 err("USBCMD_HCRESET timed out!");
2514 break;
2518 /* Turn on all interrupts */
2519 outw (USBINTR_TIMEOUT | USBINTR_RESUME | USBINTR_IOC | USBINTR_SP, io_addr + USBINTR);
2521 /* Start at frame 0 */
2522 outw (0, io_addr + USBFRNUM);
2523 outl (virt_to_bus (s->framelist), io_addr + USBFLBASEADD);
2525 /* Run and mark it configured with a 64-byte max packet */
2526 outw (USBCMD_RS | USBCMD_CF | USBCMD_MAXP, io_addr + USBCMD);
2527 s->apm_state = 1;
2528 s->running = 1;
2531 _static void __exit uhci_cleanup_dev(uhci_t *s)
2533 struct usb_device *root_hub = s->bus->root_hub;
2535 s->running = 0; // Don't allow submit_urb
2537 if (root_hub)
2538 usb_disconnect (&root_hub);
2540 reset_hc (s);
2541 wait_ms (1);
2543 uhci_unlink_urbs (s, 0, 1); // Forced unlink of remaining URBs
2544 uhci_cleanup_unlink (s, 1); // force cleanup of async killed URBs
2546 usb_deregister_bus (s->bus);
2548 release_region (s->io_addr, s->io_size);
2549 free_irq (s->irq, s);
2550 usb_free_bus (s->bus);
2551 cleanup_skel (s);
2552 kfree (s);
2555 _static int __init uhci_start_usb (uhci_t *s)
2556 { /* start it up */
2557 /* connect the virtual root hub */
2558 struct usb_device *usb_dev;
2560 usb_dev = usb_alloc_dev (NULL, s->bus);
2561 if (!usb_dev)
2562 return -1;
2564 s->bus->root_hub = usb_dev;
2565 usb_connect (usb_dev);
2567 if (usb_new_device (usb_dev) != 0) {
2568 usb_free_dev (usb_dev);
2569 return -1;
2572 return 0;
2575 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,3,44)
2576 _static int handle_pm_event (struct pm_dev *dev, pm_request_t rqst, void *data)
2578 uhci_t *s = (uhci_t*) dev->data;
2579 dbg("handle_apm_event(%d)", rqst);
2580 if (s) {
2581 switch (rqst) {
2582 case PM_SUSPEND:
2583 reset_hc (s);
2584 break;
2585 case PM_RESUME:
2586 start_hc (s);
2587 break;
2590 return 0;
2592 #endif
2594 _static int __init alloc_uhci (struct pci_dev *dev, int irq, unsigned int io_addr, unsigned int io_size)
2596 uhci_t *s;
2597 struct usb_bus *bus;
2598 struct pm_dev *pmdev;
2600 s = kmalloc (sizeof (uhci_t), GFP_KERNEL);
2601 if (!s)
2602 return -1;
2604 memset (s, 0, sizeof (uhci_t));
2605 INIT_LIST_HEAD (&s->free_desc);
2606 INIT_LIST_HEAD (&s->urb_list);
2607 INIT_LIST_HEAD (&s->urb_unlinked);
2608 spin_lock_init (&s->urb_list_lock);
2609 spin_lock_init (&s->qh_lock);
2610 spin_lock_init (&s->td_lock);
2611 atomic_set(&s->avoid_bulk, 0);
2612 s->irq = -1;
2613 s->io_addr = io_addr;
2614 s->io_size = io_size;
2615 s->next = devs; //chain new uhci device into global list
2616 s->frame_counter = 0;
2618 bus = usb_alloc_bus (&uhci_device_operations);
2619 if (!bus) {
2620 kfree (s);
2621 return -1;
2624 s->bus = bus;
2625 bus->hcpriv = s;
2627 /* UHCI specs says devices must have 2 ports, but goes on to say */
2628 /* they may have more but give no way to determine how many they */
2629 /* have, so default to 2 */
2630 /* According to the UHCI spec, Bit 7 is always set to 1. So we try */
2631 /* to use this to our advantage */
2633 for (s->maxports = 0; s->maxports < (io_size - 0x10) / 2; s->maxports++) {
2634 unsigned int portstatus;
2636 portstatus = inw (io_addr + 0x10 + (s->maxports * 2));
2637 dbg("port %i, adr %x status %x", s->maxports,
2638 io_addr + 0x10 + (s->maxports * 2), portstatus);
2639 if (!(portstatus & 0x0080))
2640 break;
2642 warn("Detected %d ports", s->maxports);
2644 /* This is experimental so anything less than 2 or greater than 8 is */
2645 /* something weird and we'll ignore it */
2646 if (s->maxports < 2 || s->maxports > 8) {
2647 dbg("Port count misdetected, forcing to 2 ports");
2648 s->maxports = 2;
2651 s->rh.numports = s->maxports;
2652 s->loop_usage=0;
2653 if (init_skel (s)) {
2654 usb_free_bus (bus);
2655 kfree(s);
2656 return -1;
2659 request_region (s->io_addr, io_size, MODNAME);
2660 reset_hc (s);
2661 usb_register_bus (s->bus);
2663 start_hc (s);
2665 if (request_irq (irq, uhci_interrupt, SA_SHIRQ, MODNAME, s)) {
2666 err("request_irq %d failed!",irq);
2667 usb_free_bus (bus);
2668 reset_hc (s);
2669 release_region (s->io_addr, s->io_size);
2670 cleanup_skel(s);
2671 kfree(s);
2672 return -1;
2675 s->irq = irq;
2677 if(uhci_start_usb (s) < 0) {
2678 uhci_cleanup_dev(s);
2679 return -1;
2682 //chain new uhci device into global list
2683 devs = s;
2684 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,3,44)
2685 pmdev = pm_register(PM_PCI_DEV, PM_PCI_ID(dev), handle_pm_event);
2686 if (pmdev)
2687 pmdev->data = s;
2688 #endif
2689 return 0;
2692 _static int __init start_uhci (struct pci_dev *dev)
2694 int i;
2696 /* Search for the IO base address.. */
2697 for (i = 0; i < 6; i++) {
2698 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,3,8)
2699 unsigned int io_addr = dev->resource[i].start;
2700 unsigned int io_size =
2701 dev->resource[i].end - dev->resource[i].start + 1;
2702 if (!(dev->resource[i].flags & 1))
2703 continue;
2704 #else
2705 unsigned int io_addr = dev->base_address[i];
2706 unsigned int io_size = 0x14;
2707 if (!(io_addr & 1))
2708 continue;
2709 io_addr &= ~1;
2710 #endif
2712 /* Is it already in use? */
2713 if (check_region (io_addr, io_size))
2714 break;
2715 /* disable legacy emulation */
2716 pci_write_config_word (dev, USBLEGSUP, USBLEGSUP_DEFAULT);
2717 if(dev->vendor==0x8086) {
2718 info("Intel USB controller: setting latency timer to %d", UHCI_LATENCY_TIMER);
2719 pci_write_config_byte(dev, PCI_LATENCY_TIMER, UHCI_LATENCY_TIMER);
2721 return alloc_uhci(dev, dev->irq, io_addr, io_size);
2723 return -1;
2726 int __init uhci_init (void)
2728 int retval = -ENODEV;
2729 struct pci_dev *dev = NULL;
2730 u8 type;
2731 int i=0;
2733 #ifdef DEBUG_SLAB
2735 uhci_desc_kmem = kmem_cache_create("uhci_desc", sizeof(uhci_desc_t), 0, SLAB_HWCACHE_ALIGN, NULL, NULL);
2737 if(!uhci_desc_kmem) {
2738 err("kmem_cache_create for uhci_desc failed (out of memory)");
2739 return -ENOMEM;
2742 urb_priv_kmem = kmem_cache_create("urb_priv", sizeof(urb_priv_t), 0, SLAB_HWCACHE_ALIGN, NULL, NULL);
2744 if(!urb_priv_kmem) {
2745 err("kmem_cache_create for urb_priv_t failed (out of memory)");
2746 return -ENOMEM;
2748 #endif
2749 info(VERSTR);
2751 #ifdef CONFIG_USB_UHCI_HIGH_BANDWIDTH
2752 info("High bandwidth mode enabled");
2753 #endif
2754 for (;;) {
2755 dev = pci_find_class (PCI_CLASS_SERIAL_USB << 8, dev);
2756 if (!dev)
2757 break;
2759 /* Is it UHCI */
2760 pci_read_config_byte (dev, PCI_CLASS_PROG, &type);
2761 if (type != 0)
2762 continue;
2764 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,3,8)
2765 if (pci_enable_device (dev) < 0)
2766 continue;
2767 #endif
2768 if(!dev->irq)
2770 err("Found UHCI device with no IRQ assigned. Check BIOS settings!");
2771 continue;
2774 /* Ok set it up */
2775 retval = start_uhci (dev);
2777 if (!retval)
2778 i++;
2781 return retval;
2784 void __exit uhci_cleanup (void)
2786 uhci_t *s;
2787 while ((s = devs)) {
2788 devs = devs->next;
2789 uhci_cleanup_dev(s);
2791 #ifdef DEBUG_SLAB
2792 if(kmem_cache_destroy(uhci_desc_kmem))
2793 err("uhci_desc_kmem remained");
2795 if(kmem_cache_destroy(urb_priv_kmem))
2796 err("urb_priv_kmem remained");
2797 #endif
2800 #ifdef MODULE
2801 int init_module (void)
2803 return uhci_init ();
2806 void cleanup_module (void)
2808 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,3,44)
2809 pm_unregister_all (handle_pm_event);
2810 #endif
2811 uhci_cleanup ();
2814 #endif //MODULE