Import 2.3.36
[davej-history.git] / drivers / usb / uhci.c
blobc071f5192e5cfc67c7bdd10a359d219ff9dd6e1b
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: uhci.c,v 1.149 1999/12/26 20:57:14 acher Exp $
18 #include <linux/config.h>
19 #include <linux/module.h>
20 #include <linux/pci.h>
21 #include <linux/kernel.h>
22 #include <linux/delay.h>
23 #include <linux/ioport.h>
24 #include <linux/sched.h>
25 #include <linux/malloc.h>
26 #include <linux/smp_lock.h>
27 #include <linux/errno.h>
28 #include <linux/unistd.h>
29 #include <linux/interrupt.h> /* for in_interrupt() */
30 #include <linux/init.h>
32 #include <asm/uaccess.h>
33 #include <asm/io.h>
34 #include <asm/irq.h>
35 #include <asm/system.h>
36 //#include <asm/spinlock.h>
38 #include "usb.h"
39 #include "uhci.h"
40 #include "uhci-debug.h"
42 //#define DEBUG
43 #ifdef DEBUG
44 #define dbg(format, args...) printk(format, ## args)
45 #else
46 #define dbg(format, args...)
47 #endif
49 #define _static static
51 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,3,0)
52 #define __init
53 #define __exit
54 #endif
56 #ifdef __alpha
57 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,3,0)
58 extern long __kernel_thread (unsigned long, int (*)(void *), void *);
59 static inline long kernel_thread (int (*fn) (void *), void *arg, unsigned long flags)
61 return __kernel_thread (flags | CLONE_VM, fn, arg);
63 #undef CONFIG_APM
64 #endif
65 #endif
67 #ifdef CONFIG_APM
68 #include <linux/apm_bios.h>
69 static int handle_apm_event (apm_event_t event);
70 #endif
72 /* We added an UHCI_SLAB slab support just for debugging purposes. In real
73 life this compile option is NOT recommended, because slab caches are not
74 suitable for modules.
77 // #define _UHCI_SLAB
78 #ifdef _UHCI_SLAB
79 static kmem_cache_t *uhci_desc_kmem;
80 static kmem_cache_t *urb_priv_kmem;
81 #endif
83 static int rh_submit_urb (purb_t purb);
84 static int rh_unlink_urb (purb_t purb);
85 static puhci_t devs = NULL;
87 /*-------------------------------------------------------------------*/
88 _static void queue_urb (puhci_t s, struct list_head *p, int do_lock)
90 unsigned long flags=0;
92 if (do_lock)
93 spin_lock_irqsave (&s->urb_list_lock, flags);
95 list_add_tail (p, &s->urb_list);
97 if (do_lock)
98 spin_unlock_irqrestore (&s->urb_list_lock, flags);
101 /*-------------------------------------------------------------------*/
102 _static void dequeue_urb (puhci_t s, struct list_head *p, int do_lock)
104 unsigned long flags=0;
106 if (do_lock)
107 spin_lock_irqsave (&s->urb_list_lock, flags);
109 list_del (p);
111 if (do_lock)
112 spin_unlock_irqrestore (&s->urb_list_lock, flags);
115 /*-------------------------------------------------------------------*/
116 _static int alloc_td (puhci_desc_t * new, int flags)
118 #ifdef _UHCI_SLAB
119 *new= kmem_cache_alloc(uhci_desc_kmem, in_interrupt ()? SLAB_ATOMIC : SLAB_KERNEL);
120 #else
121 *new = (uhci_desc_t *) kmalloc (sizeof (uhci_desc_t), in_interrupt ()? GFP_ATOMIC : GFP_KERNEL);
122 #endif
123 if (!*new)
124 return -ENOMEM;
126 memset (*new, 0, sizeof (uhci_desc_t));
127 (*new)->hw.td.link = UHCI_PTR_TERM | (flags & UHCI_PTR_BITS); // last by default
129 (*new)->type = TD_TYPE;
130 INIT_LIST_HEAD (&(*new)->vertical);
131 INIT_LIST_HEAD (&(*new)->horizontal);
133 return 0;
135 /*-------------------------------------------------------------------*/
136 /* insert td at last position in td-list of qh (vertical) */
137 _static int insert_td (puhci_t s, puhci_desc_t qh, puhci_desc_t new, int flags)
139 uhci_desc_t *prev;
140 unsigned long xxx;
142 spin_lock_irqsave (&s->td_lock, xxx);
144 list_add_tail (&new->vertical, &qh->vertical);
146 if (qh->hw.qh.element & UHCI_PTR_TERM) {
147 // virgin qh without any tds
148 qh->hw.qh.element = virt_to_bus (new); /* QH's cannot have the DEPTH bit set */
150 else {
151 // already tds inserted
152 prev = list_entry (new->vertical.prev, uhci_desc_t, vertical);
153 // implicitely remove TERM bit of prev
154 prev->hw.td.link = virt_to_bus (new) | (flags & UHCI_PTR_DEPTH);
157 spin_unlock_irqrestore (&s->td_lock, xxx);
159 return 0;
161 /*-------------------------------------------------------------------*/
162 /* insert new_td after td (horizontal) */
163 _static int insert_td_horizontal (puhci_t s, puhci_desc_t td, puhci_desc_t new, int flags)
165 uhci_desc_t *next;
166 unsigned long xxx;
168 spin_lock_irqsave (&s->td_lock, xxx);
170 next = list_entry (td->horizontal.next, uhci_desc_t, horizontal);
171 new->hw.td.link = td->hw.td.link;
172 list_add (&new->horizontal, &td->horizontal);
173 td->hw.td.link = virt_to_bus (new);
175 spin_unlock_irqrestore (&s->td_lock, xxx);
177 return 0;
179 /*-------------------------------------------------------------------*/
180 _static int unlink_td (puhci_t s, puhci_desc_t element)
182 uhci_desc_t *next, *prev;
183 int dir = 0;
184 unsigned long xxx;
186 spin_lock_irqsave (&s->td_lock, xxx);
188 next = list_entry (element->vertical.next, uhci_desc_t, vertical);
190 if (next == element) {
191 dir = 1;
192 next = list_entry (element->horizontal.next, uhci_desc_t, horizontal);
193 prev = list_entry (element->horizontal.prev, uhci_desc_t, horizontal);
195 else {
196 prev = list_entry (element->vertical.prev, uhci_desc_t, vertical);
199 if (prev->type == TD_TYPE)
200 prev->hw.td.link = element->hw.td.link;
201 else
202 prev->hw.qh.element = element->hw.td.link;
204 wmb ();
206 if (dir == 0)
207 list_del (&element->vertical);
208 else
209 list_del (&element->horizontal);
211 spin_unlock_irqrestore (&s->td_lock, xxx);
213 return 0;
215 /*-------------------------------------------------------------------*/
216 _static int delete_desc (puhci_desc_t element)
218 #ifdef _UHCI_SLAB
219 kmem_cache_free(uhci_desc_kmem, element);
220 #else
221 kfree (element);
222 #endif
223 return 0;
225 /*-------------------------------------------------------------------*/
226 // Allocates qh element
227 _static int alloc_qh (puhci_desc_t * new)
229 #ifdef _UHCI_SLAB
230 *new= kmem_cache_alloc(uhci_desc_kmem, in_interrupt ()? SLAB_ATOMIC : SLAB_KERNEL);
231 #else
232 *new = (uhci_desc_t *) kmalloc (sizeof (uhci_desc_t), in_interrupt ()? GFP_ATOMIC : GFP_KERNEL);
233 #endif
234 if (!*new)
235 return -ENOMEM;
237 memset (*new, 0, sizeof (uhci_desc_t));
238 (*new)->hw.qh.head = UHCI_PTR_TERM;
239 (*new)->hw.qh.element = UHCI_PTR_TERM;
240 (*new)->type = QH_TYPE;
241 INIT_LIST_HEAD (&(*new)->horizontal);
242 INIT_LIST_HEAD (&(*new)->vertical);
244 dbg (KERN_DEBUG MODSTR "Allocated qh @ %p\n", *new);
246 return 0;
248 /*-------------------------------------------------------------------*/
249 // inserts new qh before/after the qh at pos
250 // flags: 0: insert before pos, 1: insert after pos (for low speed transfers)
251 _static int insert_qh (puhci_t s, puhci_desc_t pos, puhci_desc_t new, int flags)
253 puhci_desc_t old;
254 unsigned long xxx;
256 spin_lock_irqsave (&s->qh_lock, xxx);
258 if (!flags) {
259 // (OLD) (POS) -> (OLD) (NEW) (POS)
260 old = list_entry (pos->horizontal.prev, uhci_desc_t, horizontal);
261 list_add_tail (&new->horizontal, &pos->horizontal);
262 new->hw.qh.head = MAKE_QH_ADDR (pos) ;
264 if (!(old->hw.qh.head & UHCI_PTR_TERM))
265 old->hw.qh.head = MAKE_QH_ADDR (new) ;
267 else {
268 // (POS) (OLD) -> (POS) (NEW) (OLD)
269 old = list_entry (pos->horizontal.next, uhci_desc_t, horizontal);
270 list_add (&new->horizontal, &pos->horizontal);
271 pos->hw.qh.head = MAKE_QH_ADDR (new) ;
272 new->hw.qh.head = MAKE_QH_ADDR (old);
275 wmb ();
277 spin_unlock_irqrestore (&s->qh_lock, xxx);
279 return 0;
281 /*-------------------------------------------------------------------*/
282 _static int unlink_qh (puhci_t s, puhci_desc_t element)
284 puhci_desc_t next, prev;
285 unsigned long xxx;
287 spin_lock_irqsave (&s->qh_lock, xxx);
289 next = list_entry (element->horizontal.next, uhci_desc_t, horizontal);
290 prev = list_entry (element->horizontal.prev, uhci_desc_t, horizontal);
291 prev->hw.qh.head = element->hw.qh.head;
292 wmb ();
293 list_del (&element->horizontal);
295 spin_unlock_irqrestore (&s->qh_lock, xxx);
297 return 0;
299 /*-------------------------------------------------------------------*/
300 _static int delete_qh (puhci_t s, puhci_desc_t qh)
302 puhci_desc_t td;
303 struct list_head *p;
305 list_del (&qh->horizontal);
307 while ((p = qh->vertical.next) != &qh->vertical) {
308 td = list_entry (p, uhci_desc_t, vertical);
309 unlink_td (s, td);
310 delete_desc (td);
313 delete_desc (qh);
315 return 0;
317 /*-------------------------------------------------------------------*/
318 void clean_td_chain (puhci_desc_t td)
320 struct list_head *p;
321 puhci_desc_t td1;
323 if (!td)
324 return;
326 while ((p = td->horizontal.next) != &td->horizontal) {
327 td1 = list_entry (p, uhci_desc_t, horizontal);
328 delete_desc (td1);
331 delete_desc (td);
333 /*-------------------------------------------------------------------*/
334 // Removes ALL qhs in chain (paranoia!)
335 _static void cleanup_skel (puhci_t s)
337 unsigned int n;
338 puhci_desc_t td;
340 printk (KERN_DEBUG MODSTR "Cleanup_skel\n");
342 for (n = 0; n < 8; n++) {
343 td = s->int_chain[n];
344 clean_td_chain (td);
347 if (s->iso_td) {
348 for (n = 0; n < 1024; n++) {
349 td = s->iso_td[n];
350 clean_td_chain (td);
352 kfree (s->iso_td);
355 if (s->framelist)
356 free_page ((unsigned long) s->framelist);
358 if (s->control_chain) {
359 // completed init_skel?
360 struct list_head *p;
361 puhci_desc_t qh, qh1;
363 qh = s->control_chain;
364 while ((p = qh->horizontal.next) != &qh->horizontal) {
365 qh1 = list_entry (p, uhci_desc_t, horizontal);
366 delete_qh (s, qh1);
368 delete_qh (s, qh);
370 else {
371 if (s->control_chain)
372 kfree (s->control_chain);
373 if (s->bulk_chain)
374 kfree (s->bulk_chain);
375 if (s->chain_end)
376 kfree (s->chain_end);
379 /*-------------------------------------------------------------------*/
380 // allocates framelist and qh-skeletons
381 // only HW-links provide continous linking, SW-links stay in their domain (ISO/INT)
382 _static int init_skel (puhci_t s)
384 int n, ret;
385 puhci_desc_t qh, td;
387 dbg (KERN_DEBUG MODSTR "init_skel\n");
389 s->framelist = (__u32 *) get_free_page (GFP_KERNEL);
391 if (!s->framelist)
392 return -ENOMEM;
394 memset (s->framelist, 0, 4096);
396 dbg (KERN_DEBUG MODSTR "allocating iso desc pointer list\n");
397 s->iso_td = (puhci_desc_t *) kmalloc (1024 * sizeof (puhci_desc_t), GFP_KERNEL);
399 if (!s->iso_td)
400 goto init_skel_cleanup;
402 s->control_chain = NULL;
403 s->bulk_chain = NULL;
404 s->chain_end = NULL;
406 dbg (KERN_DEBUG MODSTR "allocating iso descs\n");
407 for (n = 0; n < 1024; n++) {
408 // allocate skeleton iso/irq-tds
409 ret = alloc_td (&td, 0);
410 if (ret)
411 goto init_skel_cleanup;
412 s->iso_td[n] = td;
413 s->framelist[n] = ((__u32) virt_to_bus (td));
416 dbg (KERN_DEBUG MODSTR "allocating qh: chain_end\n");
417 ret = alloc_qh (&qh);
419 if (ret)
420 goto init_skel_cleanup;
422 s->chain_end = qh;
424 dbg (KERN_DEBUG MODSTR "allocating qh: bulk_chain\n");
425 ret = alloc_qh (&qh);
427 if (ret)
428 goto init_skel_cleanup;
430 insert_qh (s, s->chain_end, qh, 0);
431 s->bulk_chain = qh;
432 dbg (KERN_DEBUG MODSTR "allocating qh: control_chain\n");
433 ret = alloc_qh (&qh);
435 if (ret)
436 goto init_skel_cleanup;
438 insert_qh (s, s->bulk_chain, qh, 0);
439 s->control_chain = qh;
440 for (n = 0; n < 8; n++)
441 s->int_chain[n] = 0;
443 dbg (KERN_DEBUG MODSTR "Allocating skeleton INT-TDs\n");
445 for (n = 0; n < 8; n++) {
446 puhci_desc_t td;
448 alloc_td (&td, 0);
449 if (!td)
450 goto init_skel_cleanup;
451 s->int_chain[n] = td;
452 if (n == 0) {
453 s->int_chain[0]->hw.td.link = virt_to_bus (s->control_chain);
455 else {
456 s->int_chain[n]->hw.td.link = virt_to_bus (s->int_chain[0]);
460 dbg (KERN_DEBUG MODSTR "Linking skeleton INT-TDs\n");
462 for (n = 0; n < 1024; n++) {
463 // link all iso-tds to the interrupt chains
464 int m, o;
465 //dbg("framelist[%i]=%x\n",n,s->framelist[n]);
466 for (o = 1, m = 2; m <= 128; o++, m += m) {
467 // n&(m-1) = n%m
468 if ((n & (m - 1)) == ((m - 1) / 2)) {
469 ((puhci_desc_t) s->iso_td[n])->hw.td.link = virt_to_bus (s->int_chain[o]);
474 //uhci_show_queue(s->control_chain);
475 dbg (KERN_DEBUG MODSTR "init_skel exit\n");
476 return 0; // OK
478 init_skel_cleanup:
479 cleanup_skel (s);
480 return -ENOMEM;
483 /*-------------------------------------------------------------------*/
484 _static void fill_td (puhci_desc_t td, int status, int info, __u32 buffer)
486 td->hw.td.status = status;
487 td->hw.td.info = info;
488 td->hw.td.buffer = buffer;
491 /*-------------------------------------------------------------------*/
492 // LOW LEVEL STUFF
493 // assembles QHs und TDs for control, bulk and iso
494 /*-------------------------------------------------------------------*/
495 _static int uhci_submit_control_urb (purb_t purb)
497 puhci_desc_t qh, td;
498 puhci_t s = (puhci_t) purb->dev->bus->hcpriv;
499 purb_priv_t purb_priv = purb->hcpriv;
500 unsigned long destination, status;
501 int maxsze = usb_maxpacket (purb->dev, purb->pipe, usb_pipeout (purb->pipe));
502 unsigned long len, bytesrequested;
503 char *data;
505 dbg (KERN_DEBUG MODSTR "uhci_submit_control start\n");
506 alloc_qh (&qh); // alloc qh for this request
508 if (!qh)
509 return -ENOMEM;
511 alloc_td (&td, UHCI_PTR_DEPTH); // get td for setup stage
513 if (!td) {
514 delete_qh (s, qh);
515 return -ENOMEM;
518 /* The "pipe" thing contains the destination in bits 8--18 */
519 destination = (purb->pipe & PIPE_DEVEP_MASK) | USB_PID_SETUP;
521 /* 3 errors */
522 status = (purb->pipe & TD_CTRL_LS) | TD_CTRL_ACTIVE |
523 (purb->transfer_flags & USB_DISABLE_SPD ? 0 : TD_CTRL_SPD) | (3 << 27);
525 /* Build the TD for the control request, try forever, 8 bytes of data */
526 fill_td (td, status, destination | (7 << 21), virt_to_bus (purb->setup_packet));
528 /* If direction is "send", change the frame from SETUP (0x2D)
529 to OUT (0xE1). Else change it from SETUP to IN (0x69). */
531 destination ^= (USB_PID_SETUP ^ USB_PID_IN); /* SETUP -> IN */
533 if (usb_pipeout (purb->pipe))
534 destination ^= (USB_PID_IN ^ USB_PID_OUT); /* IN -> OUT */
536 insert_td (s, qh, td, 0); // queue 'setup stage'-td in qh
537 #if 0
538 printk ("SETUP to pipe %x: %x %x %x %x %x %x %x %x\n", purb->pipe,
539 purb->setup_packet[0], purb->setup_packet[1], purb->setup_packet[2], purb->setup_packet[3],
540 purb->setup_packet[4], purb->setup_packet[5], purb->setup_packet[6], purb->setup_packet[7]);
541 //uhci_show_td(td);
542 #endif
543 /* Build the DATA TD's */
544 len = purb->transfer_buffer_length;
545 bytesrequested = len;
546 data = purb->transfer_buffer;
548 while (len > 0) {
549 int pktsze = len;
551 alloc_td (&td, UHCI_PTR_DEPTH);
552 if (!td) {
553 delete_qh (s, qh);
554 return -ENOMEM;
557 if (pktsze > maxsze)
558 pktsze = maxsze;
560 destination ^= 1 << TD_TOKEN_TOGGLE; // toggle DATA0/1
562 fill_td (td, status, destination | ((pktsze - 1) << 21),
563 virt_to_bus (data)); // Status, pktsze bytes of data
565 insert_td (s, qh, td, UHCI_PTR_DEPTH); // queue 'data stage'-td in qh
567 data += pktsze;
568 len -= pktsze;
571 /* Build the final TD for control status */
572 /* It's only IN if the pipe is out AND we aren't expecting data */
573 destination &= ~0xFF;
575 if (usb_pipeout (purb->pipe) | (bytesrequested == 0))
576 destination |= USB_PID_IN;
577 else
578 destination |= USB_PID_OUT;
580 destination |= 1 << TD_TOKEN_TOGGLE; /* End in Data1 */
582 alloc_td (&td, UHCI_PTR_DEPTH);
584 if (!td) {
585 delete_qh (s, qh);
586 return -ENOMEM;
589 /* no limit on errors on final packet , 0 bytes of data */
590 fill_td (td, status | TD_CTRL_IOC, destination | (UHCI_NULL_DATA_SIZE << 21),
593 insert_td (s, qh, td, UHCI_PTR_DEPTH); // queue status td
596 list_add (&qh->desc_list, &purb_priv->desc_list);
598 /* Start it up... put low speed first */
599 if (purb->pipe & TD_CTRL_LS)
600 insert_qh (s, s->control_chain, qh, 1); // insert after control chain
601 else
602 insert_qh (s, s->bulk_chain, qh, 0); // insert before bulk chain
603 //uhci_show_queue(s->control_chain);
605 dbg (KERN_DEBUG MODSTR "uhci_submit_control end\n");
606 return 0;
608 /*-------------------------------------------------------------------*/
609 _static int uhci_submit_bulk_urb (purb_t purb)
611 puhci_t s = (puhci_t) purb->dev->bus->hcpriv;
612 purb_priv_t purb_priv = purb->hcpriv;
613 puhci_desc_t qh, td;
614 unsigned long destination, status;
615 char *data;
616 unsigned int pipe = purb->pipe;
617 int maxsze = usb_maxpacket (purb->dev, pipe, usb_pipeout (pipe));
618 int info, len;
620 /* shouldn't the clear_halt be done in the USB core or in the client driver? - Thomas */
621 if (usb_endpoint_halted (purb->dev, usb_pipeendpoint (pipe), usb_pipeout (pipe)) &&
622 usb_clear_halt (purb->dev, usb_pipeendpoint (pipe) | (pipe & USB_DIR_IN)))
623 return -EPIPE;
625 if (!maxsze)
626 return -EMSGSIZE;
627 /* FIXME: should tell the client that the endpoint is invalid, i.e. not in the descriptor */
629 alloc_qh (&qh); // get qh for this request
631 if (!qh)
632 return -ENOMEM;
634 /* The "pipe" thing contains the destination in bits 8--18. */
635 destination = (pipe & PIPE_DEVEP_MASK) | usb_packetid (pipe);
637 /* 3 errors */
638 status = (pipe & TD_CTRL_LS) | TD_CTRL_ACTIVE |
639 ((purb->transfer_flags & USB_DISABLE_SPD) ? 0 : TD_CTRL_SPD) | (3 << 27);
641 /* Build the TDs for the bulk request */
642 len = purb->transfer_buffer_length;
643 data = purb->transfer_buffer;
644 dbg (KERN_DEBUG MODSTR "uhci_submit_bulk_urb: pipe %x, len %d\n", pipe, len);
646 while (len > 0) {
647 int pktsze = len;
649 alloc_td (&td, UHCI_PTR_DEPTH);
651 if (!td) {
652 delete_qh (s, qh);
653 return -ENOMEM;
656 if (pktsze > maxsze)
657 pktsze = maxsze;
659 // pktsze bytes of data
660 info = destination | ((pktsze - 1) << 21) |
661 (usb_gettoggle (purb->dev, usb_pipeendpoint (pipe), usb_pipeout (pipe)) << TD_TOKEN_TOGGLE);
663 fill_td (td, status, info, virt_to_bus (data));
665 data += pktsze;
666 len -= pktsze;
668 if (!len)
669 td->hw.td.status |= TD_CTRL_IOC; // last one generates INT
670 //dbg("insert td %p, len %i\n",td,pktsze);
672 insert_td (s, qh, td, UHCI_PTR_DEPTH);
674 /* Alternate Data0/1 (start with Data0) */
675 usb_dotoggle (purb->dev, usb_pipeendpoint (pipe), usb_pipeout (pipe));
678 list_add (&qh->desc_list, &purb_priv->desc_list);
680 insert_qh (s, s->chain_end, qh, 0); // insert before end marker
681 //uhci_show_queue(s->bulk_chain);
683 dbg (KERN_DEBUG MODSTR "uhci_submit_bulk_urb: exit\n");
684 return 0;
686 /*-------------------------------------------------------------------*/
687 // unlinks an urb by dequeuing its qh, waits some frames and forgets it
688 // Problem: unlinking in interrupt requires waiting for one frame (udelay)
689 // to allow the whole structures to be safely removed
690 _static int uhci_unlink_urb (purb_t purb)
692 puhci_t s;
693 puhci_desc_t qh;
694 puhci_desc_t td;
695 purb_priv_t purb_priv;
696 unsigned long flags=0;
697 struct list_head *p;
699 if (!purb) // you never know...
700 return -1;
702 s = (puhci_t) purb->dev->bus->hcpriv; // get pointer to uhci struct
704 if (usb_pipedevice (purb->pipe) == s->rh.devnum)
705 return rh_unlink_urb (purb);
707 if(!in_interrupt()) {
708 // is the following really necessary? dequeue_urb has its own spinlock (GA)
709 spin_lock_irqsave (&s->unlink_urb_lock, flags); // do not allow interrupts
712 //dbg("unlink_urb called %p\n",purb);
713 if (purb->status == USB_ST_URB_PENDING) {
714 // URB probably still in work
715 purb_priv = purb->hcpriv;
716 dequeue_urb (s, &purb->urb_list,1);
717 purb->status = USB_ST_URB_KILLED; // mark urb as killed
719 if(!in_interrupt()) {
720 spin_unlock_irqrestore (&s->unlink_urb_lock, flags); // allow interrupts from here
723 switch (usb_pipetype (purb->pipe)) {
724 case PIPE_ISOCHRONOUS:
725 case PIPE_INTERRUPT:
726 for (p = purb_priv->desc_list.next; p != &purb_priv->desc_list; p = p->next) {
727 td = list_entry (p, uhci_desc_t, desc_list);
728 unlink_td (s, td);
730 // wait at least 1 Frame
731 if (in_interrupt ())
732 udelay (1000);
733 else
734 schedule_timeout (1 + 1 * HZ / 1000);
735 while ((p = purb_priv->desc_list.next) != &purb_priv->desc_list) {
736 td = list_entry (p, uhci_desc_t, desc_list);
737 list_del (p);
738 delete_desc (td);
740 break;
742 case PIPE_BULK:
743 case PIPE_CONTROL:
744 qh = list_entry (purb_priv->desc_list.next, uhci_desc_t, desc_list);
746 unlink_qh (s, qh); // remove this qh from qh-list
747 // wait at least 1 Frame
749 if (in_interrupt ())
750 udelay (1000);
751 else
752 schedule_timeout (1 + 1 * HZ / 1000);
753 delete_qh (s, qh); // remove it physically
757 #ifdef _UHCI_SLAB
758 kmem_cache_free(urb_priv_kmem, purb->hcpriv);
759 #else
760 kfree (purb->hcpriv);
761 #endif
762 if (purb->complete) {
763 dbg (KERN_DEBUG MODSTR "unlink_urb: calling completion\n");
764 purb->complete ((struct urb *) purb);
765 usb_dec_dev_use (purb->dev);
767 return 0;
769 else {
770 if(!in_interrupt())
771 spin_unlock_irqrestore (&s->unlink_urb_lock, flags); // allow interrupts from here
774 return 0;
776 /*-------------------------------------------------------------------*/
777 // In case of ASAP iso transfer, search the URB-list for already queued URBs
778 // for this EP and calculate the earliest start frame for the new
779 // URB (easy seamless URB continuation!)
780 _static int find_iso_limits (purb_t purb, unsigned int *start, unsigned int *end)
782 purb_t u, last_urb = NULL;
783 puhci_t s = (puhci_t) purb->dev->bus->hcpriv;
784 struct list_head *p = s->urb_list.next;
785 int ret=-1;
786 unsigned long flags;
788 spin_lock_irqsave (&s->urb_list_lock, flags);
790 for (; p != &s->urb_list; p = p->next) {
791 u = list_entry (p, urb_t, urb_list);
792 // look for pending URBs with identical pipe handle
793 // works only because iso doesn't toggle the data bit!
794 if ((purb->pipe == u->pipe) && (purb->dev == u->dev) && (u->status == USB_ST_URB_PENDING)) {
795 if (!last_urb)
796 *start = u->start_frame;
797 last_urb = u;
801 if (last_urb) {
802 *end = (last_urb->start_frame + last_urb->number_of_packets) & 1023;
803 ret=0;
806 spin_unlock_irqrestore(&s->urb_list_lock, flags);
808 return ret; // no previous urb found
811 /*-------------------------------------------------------------------*/
812 // adjust start_frame according to scheduling constraints (ASAP etc)
814 _static void jnx_show_desc (puhci_desc_t d)
816 switch (d->type) {
817 case TD_TYPE:
818 printk (KERN_DEBUG MODSTR "td @ 0x%08lx: link 0x%08x status 0x%08x info 0x%08x buffer 0x%08x\n",
819 (unsigned long) d, d->hw.td.link, d->hw.td.status, d->hw.td.info, d->hw.td.buffer);
820 if (!(d->hw.td.link & UHCI_PTR_TERM))
821 jnx_show_desc ((puhci_desc_t) bus_to_virt (d->hw.td.link & ~UHCI_PTR_BITS));
822 break;
824 case QH_TYPE:
825 printk (KERN_DEBUG MODSTR "qh @ 0x%08lx: head 0x%08x element 0x%08x\n",
826 (unsigned long) d, d->hw.qh.head, d->hw.qh.element);
827 if (!(d->hw.qh.element & UHCI_PTR_TERM))
828 jnx_show_desc ((puhci_desc_t) bus_to_virt (d->hw.qh.element & ~UHCI_PTR_BITS));
829 if (!(d->hw.qh.head & UHCI_PTR_TERM))
830 jnx_show_desc ((puhci_desc_t) bus_to_virt (d->hw.qh.head & ~UHCI_PTR_BITS));
831 break;
833 default:
834 printk (KERN_DEBUG MODSTR "desc @ 0x%08lx: invalid type %u\n",
835 (unsigned long) d, d->type);
839 /*-------------------------------------------------------------------*/
840 _static int iso_find_start (purb_t purb)
842 puhci_t s = (puhci_t) purb->dev->bus->hcpriv;
843 unsigned int now;
844 unsigned int start_limit = 0, stop_limit = 0, queued_size;
845 int limits;
847 now = UHCI_GET_CURRENT_FRAME (s) & 1023;
849 if ((unsigned) purb->number_of_packets > 900)
850 return -EFBIG;
852 limits = find_iso_limits (purb, &start_limit, &stop_limit);
853 queued_size = (stop_limit - start_limit) & 1023;
855 if (purb->transfer_flags & USB_ISO_ASAP) {
856 // first iso
857 if (limits) {
858 // 10ms setup should be enough //FIXME!
859 purb->start_frame = (now + 10) & 1023;
861 else {
862 purb->start_frame = stop_limit; //seamless linkage
864 if (((now - purb->start_frame) & 1023) <= (unsigned) purb->number_of_packets) {
865 printk (KERN_DEBUG MODSTR "iso_find_start: warning, ASAP gap, should not happen\n");
866 printk (KERN_DEBUG MODSTR "iso_find_start: now %u start_frame %u number_of_packets %u pipe 0x%08x\n",
867 now, purb->start_frame, purb->number_of_packets, purb->pipe);
869 puhci_t s = (puhci_t) purb->dev->bus->hcpriv;
870 struct list_head *p;
871 purb_t u;
872 int a = -1, b = -1;
873 unsigned long flags;
875 spin_lock_irqsave (&s->urb_list_lock, flags);
876 p=s->urb_list.next;
878 for (; p != &s->urb_list; p = p->next) {
879 u = list_entry (p, urb_t, urb_list);
880 if (purb->dev != u->dev)
881 continue;
882 printk (KERN_DEBUG MODSTR "urb: pipe 0x%08x status %d start_frame %u number_of_packets %u\n",
883 u->pipe, u->status, u->start_frame, u->number_of_packets);
884 if (!usb_pipeisoc (u->pipe))
885 continue;
886 if (a == -1)
887 a = u->start_frame;
888 b = (u->start_frame + u->number_of_packets - 1) & 1023;
890 spin_unlock_irqrestore(&s->urb_list_lock, flags);
891 #if 0
892 if (a != -1 && b != -1) {
893 do {
894 jnx_show_desc (s->iso_td[a]);
895 a = (a + 1) & 1023;
897 while (a != b);
899 #endif
901 purb->start_frame = (now + 5) & 1023; // 5ms setup should be enough //FIXME!
902 //return -EAGAIN; //FIXME
907 else {
908 purb->start_frame &= 1023;
909 if (((now - purb->start_frame) & 1023) < (unsigned) purb->number_of_packets) {
910 printk (KERN_DEBUG MODSTR "iso_find_start: now between start_frame and end\n");
911 return -EAGAIN;
915 /* check if either start_frame or start_frame+number_of_packets-1 lies between start_limit and stop_limit */
916 if (limits)
917 return 0;
918 if (((purb->start_frame - start_limit) & 1023) < queued_size ||
919 ((purb->start_frame + purb->number_of_packets - 1 - start_limit) & 1023) < queued_size) {
920 printk (KERN_DEBUG MODSTR "iso_find_start: start_frame %u number_of_packets %u start_limit %u stop_limit %u\n",
921 purb->start_frame, purb->number_of_packets, start_limit, stop_limit);
922 return -EAGAIN;
925 return 0;
927 /*-------------------------------------------------------------------*/
928 // submits USB interrupt (ie. polling ;-)
929 // ASAP-flag set implicitely
930 // if period==0, the the transfer is only done once (usb_scsi need this...)
932 _static int uhci_submit_int_urb (purb_t purb)
934 puhci_t s = (puhci_t) purb->dev->bus->hcpriv;
935 purb_priv_t purb_priv = purb->hcpriv;
936 int nint, n, ret;
937 puhci_desc_t td;
938 int status, destination;
939 int now;
940 int info;
941 unsigned int pipe = purb->pipe;
943 //printk("SUBMIT INT\n");
945 if (purb->interval < 0 || purb->interval >= 256)
946 return -EINVAL;
948 if (purb->interval == 0)
949 nint = 0;
950 else {
951 for (nint = 0, n = 1; nint <= 8; nint++, n += n) // round interval down to 2^n
953 if (purb->interval < n) {
954 purb->interval = n / 2;
955 break;
958 nint--;
960 dbg(KERN_INFO "Rounded interval to %i, chain %i\n", purb->interval, nint);
962 now = UHCI_GET_CURRENT_FRAME (s) & 1023;
963 purb->start_frame = now; // remember start frame, just in case...
965 purb->number_of_packets = 1;
967 // INT allows only one packet
968 if (purb->transfer_buffer_length > usb_maxpacket (purb->dev, pipe, usb_pipeout (pipe)))
969 return -EINVAL;
971 ret = alloc_td (&td, UHCI_PTR_DEPTH);
973 if (ret)
974 return -ENOMEM;
976 status = (pipe & TD_CTRL_LS) | TD_CTRL_ACTIVE | TD_CTRL_IOC |
977 (purb->transfer_flags & USB_DISABLE_SPD ? 0 : TD_CTRL_SPD) | (1 << 27);
979 destination = (purb->pipe & PIPE_DEVEP_MASK) | usb_packetid (purb->pipe) |
980 (((purb->transfer_buffer_length - 1) & 0x7ff) << 21);
983 info = destination | (usb_gettoggle (purb->dev, usb_pipeendpoint (pipe), usb_pipeout (pipe)) << TD_TOKEN_TOGGLE);
985 fill_td (td, status, info, virt_to_bus (purb->transfer_buffer));
986 list_add_tail (&td->desc_list, &purb_priv->desc_list);
987 insert_td_horizontal (s, s->int_chain[nint], td, UHCI_PTR_DEPTH); // store in INT-TDs
989 usb_dotoggle (purb->dev, usb_pipeendpoint (pipe), usb_pipeout (pipe));
991 #if 0
992 td = tdm[purb->number_of_packets];
993 fill_td (td, TD_CTRL_IOC, 0, 0);
994 insert_td_horizontal (s, s->iso_td[(purb->start_frame + (purb->number_of_packets) * purb->interval + 1) & 1023], td, UHCI_PTR_DEPTH);
995 list_add_tail (&td->desc_list, &purb_priv->desc_list);
996 #endif
998 return 0;
1000 /*-------------------------------------------------------------------*/
1001 _static int uhci_submit_iso_urb (purb_t purb)
1003 puhci_t s = (puhci_t) purb->dev->bus->hcpriv;
1004 purb_priv_t purb_priv = purb->hcpriv;
1005 int n, ret;
1006 puhci_desc_t td, *tdm;
1007 int status, destination;
1008 unsigned long flags;
1009 spinlock_t lock;
1011 spin_lock_init (&lock);
1012 spin_lock_irqsave (&lock, flags); // Disable IRQs to schedule all ISO-TDs in time
1014 ret = iso_find_start (purb); // adjusts purb->start_frame for later use
1016 if (ret)
1017 goto err;
1019 tdm = (puhci_desc_t *) kmalloc (purb->number_of_packets * sizeof (puhci_desc_t), in_interrupt ()? GFP_ATOMIC : GFP_KERNEL);
1021 if (!tdm) {
1022 ret = -ENOMEM;
1023 goto err;
1026 // First try to get all TDs
1027 for (n = 0; n < purb->number_of_packets; n++) {
1028 dbg (KERN_DEBUG MODSTR "n:%d purb->iso_frame_desc[n].length:%d\n", n, purb->iso_frame_desc[n].length);
1029 if (!purb->iso_frame_desc[n].length) {
1030 // allows ISO striping by setting length to zero in iso_descriptor
1031 tdm[n] = 0;
1032 continue;
1034 ret = alloc_td (&td, UHCI_PTR_DEPTH);
1035 if (ret) {
1036 int i; // Cleanup allocated TDs
1038 for (i = 0; i < n; n++)
1039 if (tdm[i])
1040 kfree (tdm[i]);
1041 kfree (tdm);
1042 ret = -ENOMEM;
1043 goto err;
1045 tdm[n] = td;
1048 status = TD_CTRL_ACTIVE | TD_CTRL_IOS; //| (purb->transfer_flags&USB_DISABLE_SPD?0:TD_CTRL_SPD);
1050 destination = (purb->pipe & PIPE_DEVEP_MASK) | usb_packetid (purb->pipe);
1052 // Queue all allocated TDs
1053 for (n = 0; n < purb->number_of_packets; n++) {
1054 td = tdm[n];
1055 if (!td)
1056 continue;
1057 if (n + 1 >= purb->number_of_packets)
1058 status |= TD_CTRL_IOC;
1060 fill_td (td, status, destination | (((purb->iso_frame_desc[n].length - 1) & 0x7ff) << 21),
1061 virt_to_bus (purb->transfer_buffer + purb->iso_frame_desc[n].offset));
1062 list_add_tail (&td->desc_list, &purb_priv->desc_list);
1063 insert_td_horizontal (s, s->iso_td[(purb->start_frame + n) & 1023], td, UHCI_PTR_DEPTH); // store in iso-tds
1064 //uhci_show_td(td);
1068 kfree (tdm);
1069 dbg ("ISO-INT# %i, start %i, now %i\n", purb->number_of_packets, purb->start_frame, UHCI_GET_CURRENT_FRAME (s) & 1023);
1070 ret = 0;
1072 err:
1073 spin_unlock_irqrestore (&lock, flags);
1074 return ret;
1077 /*-------------------------------------------------------------------*/
1078 _static int search_dev_ep (puhci_t s, purb_t purb)
1080 unsigned long flags;
1081 struct list_head *p = s->urb_list.next;
1082 purb_t tmp;
1084 dbg (KERN_DEBUG MODSTR "search_dev_ep:\n");
1085 spin_lock_irqsave (&s->urb_list_lock, flags);
1087 for (; p != &s->urb_list; p = p->next) {
1088 tmp = list_entry (p, urb_t, urb_list);
1089 dbg (KERN_DEBUG MODSTR "urb: %p\n", tmp);
1090 // we can accept this urb if it is not queued at this time
1091 // or if non-iso transfer requests should be scheduled for the same device and pipe
1092 if ((usb_pipetype (purb->pipe) != PIPE_ISOCHRONOUS &&
1093 tmp->dev == purb->dev && tmp->pipe == purb->pipe) || (purb == tmp)) {
1094 spin_unlock_irqrestore (&s->urb_list_lock, flags);
1095 return 1; // found another urb already queued for processing
1099 spin_unlock_irqrestore (&s->urb_list_lock, flags);
1101 return 0;
1103 /*-------------------------------------------------------------------*/
1104 _static int uhci_submit_urb (purb_t purb)
1106 puhci_t s;
1107 purb_priv_t purb_priv;
1108 int ret = 0;
1110 if (!purb->dev || !purb->dev->bus)
1111 return -ENODEV;
1113 s = (puhci_t) purb->dev->bus->hcpriv;
1114 //printk( MODSTR"submit_urb: %p type %d\n",purb,usb_pipetype(purb->pipe));
1116 if (usb_pipedevice (purb->pipe) == s->rh.devnum)
1117 return rh_submit_urb (purb); /* virtual root hub */
1119 usb_inc_dev_use (purb->dev);
1121 if (search_dev_ep (s, purb)) {
1122 usb_dec_dev_use (purb->dev);
1123 return -ENXIO; // urb already queued
1127 #ifdef _UHCI_SLAB
1128 purb_priv = kmem_cache_alloc(urb_priv_kmem, in_interrupt ()? SLAB_ATOMIC : SLAB_KERNEL);
1129 #else
1130 purb_priv = kmalloc (sizeof (urb_priv_t), in_interrupt ()? GFP_ATOMIC : GFP_KERNEL);
1131 #endif
1132 if (!purb_priv) {
1133 usb_dec_dev_use (purb->dev);
1134 return -ENOMEM;
1137 purb->hcpriv = purb_priv;
1138 INIT_LIST_HEAD (&purb_priv->desc_list);
1139 purb_priv->short_control_packet=0;
1140 dbg (KERN_DEBUG MODSTR "submit_urb: scheduling %p\n", purb);
1142 switch (usb_pipetype (purb->pipe)) {
1143 case PIPE_ISOCHRONOUS:
1144 ret = uhci_submit_iso_urb (purb);
1145 break;
1146 case PIPE_INTERRUPT:
1147 ret = uhci_submit_int_urb (purb);
1148 break;
1149 case PIPE_CONTROL:
1150 //dump_urb (purb);
1151 ret = uhci_submit_control_urb (purb);
1152 break;
1153 case PIPE_BULK:
1154 ret = uhci_submit_bulk_urb (purb);
1155 break;
1156 default:
1157 ret = -EINVAL;
1160 dbg (KERN_DEBUG MODSTR "submit_urb: scheduled with ret: %d\n", ret);
1162 if (ret != USB_ST_NOERROR) {
1163 usb_dec_dev_use (purb->dev);
1164 #ifdef _UHCI_SLAB
1165 kmem_cache_free(urb_priv_kmem, purb_priv);
1166 #else
1167 kfree (purb_priv);
1168 #endif
1169 return ret;
1172 purb->status = USB_ST_URB_PENDING;
1173 queue_urb (s, &purb->urb_list,1);
1174 dbg (KERN_DEBUG MODSTR "submit_urb: exit\n");
1176 return 0;
1178 /*-------------------------------------------------------------------
1179 Virtual Root Hub
1180 -------------------------------------------------------------------*/
1182 _static __u8 root_hub_dev_des[] =
1184 0x12, /* __u8 bLength; */
1185 0x01, /* __u8 bDescriptorType; Device */
1186 0x00, /* __u16 bcdUSB; v1.0 */
1187 0x01,
1188 0x09, /* __u8 bDeviceClass; HUB_CLASSCODE */
1189 0x00, /* __u8 bDeviceSubClass; */
1190 0x00, /* __u8 bDeviceProtocol; */
1191 0x08, /* __u8 bMaxPacketSize0; 8 Bytes */
1192 0x00, /* __u16 idVendor; */
1193 0x00,
1194 0x00, /* __u16 idProduct; */
1195 0x00,
1196 0x00, /* __u16 bcdDevice; */
1197 0x00,
1198 0x00, /* __u8 iManufacturer; */
1199 0x00, /* __u8 iProduct; */
1200 0x00, /* __u8 iSerialNumber; */
1201 0x01 /* __u8 bNumConfigurations; */
1205 /* Configuration descriptor */
1206 _static __u8 root_hub_config_des[] =
1208 0x09, /* __u8 bLength; */
1209 0x02, /* __u8 bDescriptorType; Configuration */
1210 0x19, /* __u16 wTotalLength; */
1211 0x00,
1212 0x01, /* __u8 bNumInterfaces; */
1213 0x01, /* __u8 bConfigurationValue; */
1214 0x00, /* __u8 iConfiguration; */
1215 0x40, /* __u8 bmAttributes;
1216 Bit 7: Bus-powered, 6: Self-powered, 5 Remote-wakwup, 4..0: resvd */
1217 0x00, /* __u8 MaxPower; */
1219 /* interface */
1220 0x09, /* __u8 if_bLength; */
1221 0x04, /* __u8 if_bDescriptorType; Interface */
1222 0x00, /* __u8 if_bInterfaceNumber; */
1223 0x00, /* __u8 if_bAlternateSetting; */
1224 0x01, /* __u8 if_bNumEndpoints; */
1225 0x09, /* __u8 if_bInterfaceClass; HUB_CLASSCODE */
1226 0x00, /* __u8 if_bInterfaceSubClass; */
1227 0x00, /* __u8 if_bInterfaceProtocol; */
1228 0x00, /* __u8 if_iInterface; */
1230 /* endpoint */
1231 0x07, /* __u8 ep_bLength; */
1232 0x05, /* __u8 ep_bDescriptorType; Endpoint */
1233 0x81, /* __u8 ep_bEndpointAddress; IN Endpoint 1 */
1234 0x03, /* __u8 ep_bmAttributes; Interrupt */
1235 0x08, /* __u16 ep_wMaxPacketSize; 8 Bytes */
1236 0x00,
1237 0xff /* __u8 ep_bInterval; 255 ms */
1241 _static __u8 root_hub_hub_des[] =
1243 0x09, /* __u8 bLength; */
1244 0x29, /* __u8 bDescriptorType; Hub-descriptor */
1245 0x02, /* __u8 bNbrPorts; */
1246 0x00, /* __u16 wHubCharacteristics; */
1247 0x00,
1248 0x01, /* __u8 bPwrOn2pwrGood; 2ms */
1249 0x00, /* __u8 bHubContrCurrent; 0 mA */
1250 0x00, /* __u8 DeviceRemovable; *** 7 Ports max *** */
1251 0xff /* __u8 PortPwrCtrlMask; *** 7 ports max *** */
1254 /*-------------------------------------------------------------------------*/
1255 /* prepare Interrupt pipe transaction data; HUB INTERRUPT ENDPOINT */
1256 _static int rh_send_irq (purb_t purb)
1259 int len = 1;
1260 int i;
1261 puhci_t uhci = purb->dev->bus->hcpriv;
1262 unsigned int io_addr = uhci->io_addr;
1263 __u16 data = 0;
1265 for (i = 0; i < uhci->rh.numports; i++) {
1266 data |= ((inw (io_addr + USBPORTSC1 + i * 2) & 0xa) > 0 ? (1 << (i + 1)) : 0);
1267 len = (i + 1) / 8 + 1;
1270 *(__u16 *) purb->transfer_buffer = cpu_to_le16 (data);
1271 purb->actual_length = len;
1272 purb->status = USB_ST_NOERROR;
1274 if ((data > 0) && (uhci->rh.send != 0)) {
1275 dbg (KERN_DEBUG MODSTR "Root-Hub INT complete: port1: %x port2: %x data: %x\n",
1276 inw (io_addr + USBPORTSC1), inw (io_addr + USBPORTSC2), data);
1277 purb->complete (purb);
1280 return USB_ST_NOERROR;
1283 /*-------------------------------------------------------------------------*/
1284 /* Virtual Root Hub INTs are polled by this timer every "intervall" ms */
1285 _static int rh_init_int_timer (purb_t purb);
1287 _static void rh_int_timer_do (unsigned long ptr)
1289 int len;
1291 purb_t purb = (purb_t) ptr;
1292 puhci_t uhci = purb->dev->bus->hcpriv;
1294 if (uhci->rh.send) {
1295 len = rh_send_irq (purb);
1296 if (len > 0) {
1297 purb->actual_length = len;
1298 if (purb->complete)
1299 purb->complete (purb);
1302 rh_init_int_timer (purb);
1305 /*-------------------------------------------------------------------------*/
1306 /* Root Hub INTs are polled by this timer */
1307 _static int rh_init_int_timer (purb_t purb)
1309 puhci_t uhci = purb->dev->bus->hcpriv;
1311 uhci->rh.interval = purb->interval;
1312 init_timer (&uhci->rh.rh_int_timer);
1313 uhci->rh.rh_int_timer.function = rh_int_timer_do;
1314 uhci->rh.rh_int_timer.data = (unsigned long) purb;
1315 uhci->rh.rh_int_timer.expires = jiffies + (HZ * (purb->interval < 30 ? 30 : purb->interval)) / 1000;
1316 add_timer (&uhci->rh.rh_int_timer);
1318 return 0;
1321 /*-------------------------------------------------------------------------*/
1322 #define OK(x) len = (x); break
1324 #define CLR_RH_PORTSTAT(x) \
1325 status = inw(io_addr+USBPORTSC1+2*(wIndex-1)); \
1326 status = (status & 0xfff5) & ~(x); \
1327 outw(status, io_addr+USBPORTSC1+2*(wIndex-1))
1329 #define SET_RH_PORTSTAT(x) \
1330 status = inw(io_addr+USBPORTSC1+2*(wIndex-1)); \
1331 status = (status & 0xfff5) | (x); \
1332 outw(status, io_addr+USBPORTSC1+2*(wIndex-1))
1335 /*-------------------------------------------------------------------------*/
1336 /****
1337 ** Root Hub Control Pipe
1338 *************************/
1341 _static int rh_submit_urb (purb_t purb)
1343 struct usb_device *usb_dev = purb->dev;
1344 puhci_t uhci = usb_dev->bus->hcpriv;
1345 unsigned int pipe = purb->pipe;
1346 devrequest *cmd = (devrequest *) purb->setup_packet;
1347 void *data = purb->transfer_buffer;
1348 int leni = purb->transfer_buffer_length;
1349 int len = 0;
1350 int status = 0;
1351 int stat = USB_ST_NOERROR;
1352 int i;
1353 unsigned int io_addr = uhci->io_addr;
1354 __u16 cstatus;
1356 __u16 bmRType_bReq;
1357 __u16 wValue;
1358 __u16 wIndex;
1359 __u16 wLength;
1361 if (usb_pipetype (pipe) == PIPE_INTERRUPT) {
1362 dbg (KERN_DEBUG MODSTR "Root-Hub submit IRQ: every %d ms\n", purb->interval);
1363 uhci->rh.urb = purb;
1364 uhci->rh.send = 1;
1365 uhci->rh.interval = purb->interval;
1366 rh_init_int_timer (purb);
1368 return USB_ST_NOERROR;
1372 bmRType_bReq = cmd->requesttype | cmd->request << 8;
1373 wValue = le16_to_cpu (cmd->value);
1374 wIndex = le16_to_cpu (cmd->index);
1375 wLength = le16_to_cpu (cmd->length);
1377 for (i = 0; i < 8; i++)
1378 uhci->rh.c_p_r[i] = 0;
1380 dbg(KERN_DEBUG MODSTR "Root-Hub: adr: %2x cmd(%1x): %04x %04x %04x %04x\n",
1381 uhci->rh.devnum, 8, bmRType_bReq, wValue, wIndex, wLength);
1383 switch (bmRType_bReq) {
1384 /* Request Destination:
1385 without flags: Device,
1386 RH_INTERFACE: interface,
1387 RH_ENDPOINT: endpoint,
1388 RH_CLASS means HUB here,
1389 RH_OTHER | RH_CLASS almost ever means HUB_PORT here
1392 case RH_GET_STATUS:
1393 *(__u16 *) data = cpu_to_le16 (1);
1394 OK (2);
1395 case RH_GET_STATUS | RH_INTERFACE:
1396 *(__u16 *) data = cpu_to_le16 (0);
1397 OK (2);
1398 case RH_GET_STATUS | RH_ENDPOINT:
1399 *(__u16 *) data = cpu_to_le16 (0);
1400 OK (2);
1401 case RH_GET_STATUS | RH_CLASS:
1402 *(__u32 *) data = cpu_to_le32 (0);
1403 OK (4); /* hub power ** */
1404 case RH_GET_STATUS | RH_OTHER | RH_CLASS:
1405 status = inw (io_addr + USBPORTSC1 + 2 * (wIndex - 1));
1406 cstatus = ((status & USBPORTSC_CSC) >> (1 - 0)) |
1407 ((status & USBPORTSC_PEC) >> (3 - 1)) |
1408 (uhci->rh.c_p_r[wIndex - 1] << (0 + 4));
1409 status = (status & USBPORTSC_CCS) |
1410 ((status & USBPORTSC_PE) >> (2 - 1)) |
1411 ((status & USBPORTSC_SUSP) >> (12 - 2)) |
1412 ((status & USBPORTSC_PR) >> (9 - 4)) |
1413 (1 << 8) | /* power on ** */
1414 ((status & USBPORTSC_LSDA) << (-8 + 9));
1416 *(__u16 *) data = cpu_to_le16 (status);
1417 *(__u16 *) (data + 2) = cpu_to_le16 (cstatus);
1418 OK (4);
1420 case RH_CLEAR_FEATURE | RH_ENDPOINT:
1421 switch (wValue) {
1422 case (RH_ENDPOINT_STALL):
1423 OK (0);
1425 break;
1427 case RH_CLEAR_FEATURE | RH_CLASS:
1428 switch (wValue) {
1429 case (RH_C_HUB_OVER_CURRENT):
1430 OK (0); /* hub power over current ** */
1432 break;
1434 case RH_CLEAR_FEATURE | RH_OTHER | RH_CLASS:
1435 switch (wValue) {
1436 case (RH_PORT_ENABLE):
1437 CLR_RH_PORTSTAT (USBPORTSC_PE);
1438 OK (0);
1439 case (RH_PORT_SUSPEND):
1440 CLR_RH_PORTSTAT (USBPORTSC_SUSP);
1441 OK (0);
1442 case (RH_PORT_POWER):
1443 OK (0); /* port power ** */
1444 case (RH_C_PORT_CONNECTION):
1445 SET_RH_PORTSTAT (USBPORTSC_CSC);
1446 OK (0);
1447 case (RH_C_PORT_ENABLE):
1448 SET_RH_PORTSTAT (USBPORTSC_PEC);
1449 OK (0);
1450 case (RH_C_PORT_SUSPEND):
1451 /*** WR_RH_PORTSTAT(RH_PS_PSSC); */
1452 OK (0);
1453 case (RH_C_PORT_OVER_CURRENT):
1454 OK (0); /* port power over current ** */
1455 case (RH_C_PORT_RESET):
1456 uhci->rh.c_p_r[wIndex - 1] = 0;
1457 OK (0);
1459 break;
1461 case RH_SET_FEATURE | RH_OTHER | RH_CLASS:
1462 switch (wValue) {
1463 case (RH_PORT_SUSPEND):
1464 SET_RH_PORTSTAT (USBPORTSC_SUSP);
1465 OK (0);
1466 case (RH_PORT_RESET):
1467 SET_RH_PORTSTAT (USBPORTSC_PR);
1468 wait_ms (10);
1469 uhci->rh.c_p_r[wIndex - 1] = 1;
1470 CLR_RH_PORTSTAT (USBPORTSC_PR);
1471 udelay (10);
1472 SET_RH_PORTSTAT (USBPORTSC_PE);
1473 wait_ms (10);
1474 SET_RH_PORTSTAT (0xa);
1475 OK (0);
1476 case (RH_PORT_POWER):
1477 OK (0); /* port power ** */
1478 case (RH_PORT_ENABLE):
1479 SET_RH_PORTSTAT (USBPORTSC_PE);
1480 OK (0);
1482 break;
1484 case RH_SET_ADDRESS:
1485 uhci->rh.devnum = wValue;
1486 OK (0);
1488 case RH_GET_DESCRIPTOR:
1489 switch ((wValue & 0xff00) >> 8) {
1490 case (0x01): /* device descriptor */
1491 len = min (leni, min (sizeof (root_hub_dev_des), wLength));
1492 memcpy (data, root_hub_dev_des, len);
1493 OK (len);
1494 case (0x02): /* configuration descriptor */
1495 len = min (leni, min (sizeof (root_hub_config_des), wLength));
1496 memcpy (data, root_hub_config_des, len);
1497 OK (len);
1498 case (0x03): /*string descriptors */
1499 stat = -EPIPE;
1501 break;
1503 case RH_GET_DESCRIPTOR | RH_CLASS:
1504 root_hub_hub_des[2] = uhci->rh.numports;
1505 len = min (leni, min (sizeof (root_hub_hub_des), wLength));
1506 memcpy (data, root_hub_hub_des, len);
1507 OK (len);
1509 case RH_GET_CONFIGURATION:
1510 *(__u8 *) data = 0x01;
1511 OK (1);
1513 case RH_SET_CONFIGURATION:
1514 OK (0);
1515 default:
1516 stat = -EPIPE;
1520 printk (KERN_DEBUG MODSTR "Root-Hub stat port1: %x port2: %x \n",
1521 inw (io_addr + USBPORTSC1), inw (io_addr + USBPORTSC2));
1523 purb->actual_length = len;
1524 purb->status = stat;
1525 if (purb->complete)
1526 purb->complete (purb);
1527 return USB_ST_NOERROR;
1529 /*-------------------------------------------------------------------------*/
1531 _static int rh_unlink_urb (purb_t purb)
1533 puhci_t uhci = purb->dev->bus->hcpriv;
1535 dbg (KERN_DEBUG MODSTR "Root-Hub unlink IRQ\n");
1536 uhci->rh.send = 0;
1537 del_timer (&uhci->rh.rh_int_timer);
1538 return 0;
1540 /*-------------------------------------------------------------------*/
1542 #define UHCI_DEBUG
1545 * Map status to standard result codes
1547 * <status> is (td->status & 0xFE0000) [a.k.a. uhci_status_bits(td->status)
1548 * <dir_out> is True for output TDs and False for input TDs.
1550 _static int uhci_map_status (int status, int dir_out)
1552 if (!status)
1553 return USB_ST_NOERROR;
1554 if (status & TD_CTRL_BITSTUFF) /* Bitstuff error */
1555 return USB_ST_BITSTUFF;
1556 if (status & TD_CTRL_CRCTIMEO) { /* CRC/Timeout */
1557 if (dir_out)
1558 return USB_ST_NORESPONSE;
1559 else
1560 return USB_ST_CRC;
1562 if (status & TD_CTRL_NAK) /* NAK */
1563 return USB_ST_TIMEOUT;
1564 if (status & TD_CTRL_BABBLE) /* Babble */
1565 return -EPIPE;
1566 if (status & TD_CTRL_DBUFERR) /* Buffer error */
1567 return USB_ST_BUFFERUNDERRUN;
1568 if (status & TD_CTRL_STALLED) /* Stalled */
1569 return -EPIPE;
1570 if (status & TD_CTRL_ACTIVE) /* Active */
1571 return USB_ST_NOERROR;
1573 return USB_ST_INTERNALERROR;
1577 * Only the USB core should call uhci_alloc_dev and uhci_free_dev
1579 _static int uhci_alloc_dev (struct usb_device *usb_dev)
1581 return 0;
1584 _static int uhci_free_dev (struct usb_device *usb_dev)
1586 return 0;
1590 * uhci_get_current_frame_number()
1592 * returns the current frame number for a USB bus/controller.
1594 _static int uhci_get_current_frame_number (struct usb_device *usb_dev)
1596 return UHCI_GET_CURRENT_FRAME ((puhci_t) usb_dev->bus->hcpriv);
1599 struct usb_operations uhci_device_operations =
1601 uhci_alloc_dev,
1602 uhci_free_dev,
1603 uhci_get_current_frame_number,
1604 uhci_submit_urb,
1605 uhci_unlink_urb
1609 * For IN-control transfers, process_transfer gets a bit more complicated,
1610 * since there are devices that return less data (eg. strings) than they
1611 * have announced. This leads to a queue abort due to the short packet,
1612 * the status stage is not executed. If this happens, the status stage
1613 * is manually re-executed.
1614 * FIXME: Stall-condition may override 'nearly' successful CTRL-IN-transfer
1615 * when the transfered length fits exactly in maxsze-packets. A bit
1616 * more intelligence is needed to detect this and finish without error.
1618 _static int process_transfer (puhci_t s, purb_t purb)
1620 int ret = USB_ST_NOERROR;
1621 purb_priv_t purb_priv = purb->hcpriv;
1622 struct list_head *qhl = purb_priv->desc_list.next;
1623 puhci_desc_t qh = list_entry (qhl, uhci_desc_t, desc_list);
1624 struct list_head *p = qh->vertical.next;
1625 puhci_desc_t desc= list_entry (purb_priv->desc_list.prev, uhci_desc_t, desc_list);
1626 puhci_desc_t last_desc = list_entry (desc->vertical.prev, uhci_desc_t, vertical);
1627 int data_toggle = usb_gettoggle (purb->dev, usb_pipeendpoint (purb->pipe), usb_pipeout (purb->pipe)); // save initial data_toggle
1630 // extracted and remapped info from TD
1631 int maxlength;
1632 int actual_length;
1633 int status = USB_ST_NOERROR;
1635 dbg (KERN_DEBUG MODSTR "process_transfer: urb contains bulk/control request\n");
1638 /* if the status phase has been retriggered and the
1639 queue is empty or the last status-TD is inactive, the retriggered
1640 status stage is completed
1642 #if 1
1643 if (purb_priv->short_control_packet &&
1644 ((qh->hw.qh.element == UHCI_PTR_TERM) ||(!(last_desc->hw.td.status & TD_CTRL_ACTIVE))))
1645 goto transfer_finished;
1646 #endif
1647 purb->actual_length=0;
1649 for (; p != &qh->vertical; p = p->next) {
1650 desc = list_entry (p, uhci_desc_t, vertical);
1652 if (desc->hw.td.status & TD_CTRL_ACTIVE) // do not process active TDs
1653 return ret;
1655 // extract transfer parameters from TD
1656 actual_length = (desc->hw.td.status + 1) & 0x7ff;
1657 maxlength = (((desc->hw.td.info >> 21) & 0x7ff) + 1) & 0x7ff;
1658 status = uhci_map_status (uhci_status_bits (desc->hw.td.status), usb_pipeout (purb->pipe));
1660 // see if EP is stalled
1661 if (status == -EPIPE) {
1662 // set up stalled condition
1663 usb_endpoint_halt (purb->dev, usb_pipeendpoint (purb->pipe), usb_pipeout (purb->pipe));
1666 // if any error occured stop processing of further TDs
1667 if (status != USB_ST_NOERROR) {
1668 // only set ret if status returned an error
1669 uhci_show_td (desc);
1670 ret = status;
1671 purb->error_count++;
1672 break;
1674 else if ((desc->hw.td.info & 0xff) != USB_PID_SETUP)
1675 purb->actual_length += actual_length;
1677 #if 0
1678 if (i++==0)
1679 uhci_show_td (desc); // show first TD of each transfer
1680 #endif
1682 // got less data than requested
1683 if ( (actual_length < maxlength)) {
1684 if (purb->transfer_flags & USB_DISABLE_SPD) {
1685 ret = USB_ST_SHORT_PACKET; // treat as real error
1686 printk (KERN_DEBUG MODSTR "process_transfer: SPD!!\n");
1687 break; // exit after this TD because SP was detected
1690 // short read during control-IN: re-start status stage
1691 if ((usb_pipetype (purb->pipe) == PIPE_CONTROL)) {
1692 if (uhci_packetid(last_desc->hw.td.info) == USB_PID_OUT) {
1693 uhci_show_td (last_desc);
1694 qh->hw.qh.element = virt_to_bus (last_desc); // re-trigger status stage
1695 printk("uhci: short packet during control transfer, retrigger status stage @ %p\n",last_desc);
1696 purb_priv->short_control_packet=1;
1697 return 0;
1700 // all other cases: short read is OK
1701 data_toggle = uhci_toggle (desc->hw.td.info);
1702 break;
1705 data_toggle = uhci_toggle (desc->hw.td.info);
1706 //printk(KERN_DEBUG MODSTR"process_transfer: len:%d status:%x mapped:%x toggle:%d\n", actual_length, desc->hw.td.status,status, data_toggle);
1709 usb_settoggle (purb->dev, usb_pipeendpoint (purb->pipe), usb_pipeout (purb->pipe), !data_toggle);
1710 transfer_finished:
1712 /* APC BackUPS Pro kludge */
1713 /* It tries to send all of the descriptor instead of */
1714 /* the amount we requested */
1715 if (desc->hw.td.status & TD_CTRL_IOC &&
1716 status & TD_CTRL_ACTIVE &&
1717 status & TD_CTRL_NAK )
1719 ret=0;
1720 status=0;
1723 unlink_qh (s, qh);
1724 delete_qh (s, qh);
1726 purb->status = status;
1728 dbg(KERN_DEBUG MODSTR"process_transfer: urb %p, wanted len %d, len %d status %x err %d\n",
1729 purb,purb->transfer_buffer_length,purb->actual_length, purb->status, purb->error_count);
1730 //dbg(KERN_DEBUG MODSTR"process_transfer: exit\n");
1731 return ret;
1734 _static int process_interrupt (puhci_t s, purb_t purb)
1736 int i, ret = USB_ST_URB_PENDING;
1737 purb_priv_t purb_priv = purb->hcpriv;
1738 struct list_head *p = purb_priv->desc_list.next;
1739 puhci_desc_t desc = list_entry (purb_priv->desc_list.prev, uhci_desc_t, desc_list);
1740 int data_toggle = usb_gettoggle (purb->dev, usb_pipeendpoint (purb->pipe),
1741 usb_pipeout (purb->pipe)); // save initial data_toggle
1742 // extracted and remapped info from TD
1744 int actual_length;
1745 int status = USB_ST_NOERROR;
1747 //printk(KERN_DEBUG MODSTR"urb contains interrupt request\n");
1749 for (i = 0; p != &purb_priv->desc_list; p = p->next, i++) // Maybe we allow more than one TD later ;-)
1751 desc = list_entry (p, uhci_desc_t, desc_list);
1753 if (desc->hw.td.status & TD_CTRL_ACTIVE) {
1754 // do not process active TDs
1755 //printk("TD ACT Status @%p %08x\n",desc,desc->hw.td.status);
1756 break;
1759 if (!desc->hw.td.status & TD_CTRL_IOC) {
1760 // do not process one-shot TDs, no recycling
1761 break;
1763 // extract transfer parameters from TD
1765 actual_length = (desc->hw.td.status + 1) & 0x7ff;
1766 status = uhci_map_status (uhci_status_bits (desc->hw.td.status), usb_pipeout (purb->pipe));
1768 // see if EP is stalled
1769 if (status == -EPIPE) {
1770 // set up stalled condition
1771 usb_endpoint_halt (purb->dev, usb_pipeendpoint (purb->pipe), usb_pipeout (purb->pipe));
1774 // if any error occured: ignore this td, and continue
1775 if (status != USB_ST_NOERROR) {
1776 purb->error_count++;
1777 goto recycle;
1779 else
1780 purb->actual_length = actual_length;
1782 // FIXME: SPD?
1784 data_toggle = uhci_toggle (desc->hw.td.info);
1786 if (purb->complete && status != USB_ST_TIMEOUT) {
1787 // for last td, no user completion is needed
1788 dbg (KERN_DEBUG MODSTR "process_interrupt: calling completion\n");
1789 purb->status = status;
1790 purb->complete ((struct urb *) purb);
1791 purb->status = USB_ST_URB_PENDING;
1793 recycle:
1794 // Recycle INT-TD if interval!=0, else mark TD as one-shot
1795 if (purb->interval) {
1796 desc->hw.td.status |= TD_CTRL_ACTIVE;
1797 desc->hw.td.info &= ~(1 << TD_TOKEN_TOGGLE);
1798 desc->hw.td.info |= (usb_gettoggle (purb->dev, usb_pipeendpoint (purb->pipe),
1799 usb_pipeout (purb->pipe)) << TD_TOKEN_TOGGLE);
1800 usb_dotoggle (purb->dev, usb_pipeendpoint (purb->pipe), usb_pipeout (purb->pipe));
1802 else {
1803 desc->hw.td.status &= ~TD_CTRL_IOC; // inactivate TD
1807 return ret;
1811 _static int process_iso (puhci_t s, purb_t purb)
1813 int i;
1814 int ret = USB_ST_NOERROR;
1815 purb_priv_t purb_priv = purb->hcpriv;
1816 struct list_head *p = purb_priv->desc_list.next;
1817 puhci_desc_t desc = list_entry (purb_priv->desc_list.prev, uhci_desc_t, desc_list);
1819 dbg ( /*KERN_DEBUG */ MODSTR "urb contains iso request\n");
1820 if (desc->hw.td.status & TD_CTRL_ACTIVE)
1821 return USB_ST_PARTIAL_ERROR; // last TD not finished
1823 purb->error_count = 0;
1824 purb->actual_length = 0;
1825 purb->status = USB_ST_NOERROR;
1827 for (i = 0; p != &purb_priv->desc_list; p = p->next, i++) {
1828 desc = list_entry (p, uhci_desc_t, desc_list);
1830 //uhci_show_td(desc);
1831 if (desc->hw.td.status & TD_CTRL_ACTIVE) {
1832 // means we have completed the last TD, but not the TDs before
1833 printk (KERN_DEBUG MODSTR "TD still active (%x)- grrr. paranoia!\n", desc->hw.td.status);
1834 ret = USB_ST_PARTIAL_ERROR;
1835 purb->iso_frame_desc[i].status = ret;
1836 unlink_td (s, desc);
1837 goto err;
1840 unlink_td (s, desc);
1842 if (purb->number_of_packets <= i) {
1843 dbg (KERN_DEBUG MODSTR "purb->number_of_packets (%d)<=(%d)\n", purb->number_of_packets, i);
1844 ret = USB_ST_URB_INVALID_ERROR;
1845 goto err;
1848 if (purb->iso_frame_desc[i].offset + purb->transfer_buffer != bus_to_virt (desc->hw.td.buffer)) {
1849 // Hm, something really weird is going on
1850 dbg (KERN_DEBUG MODSTR "Pointer Paranoia: %p!=%p\n", purb->iso_frame_desc[i].offset + purb->transfer_buffer, bus_to_virt (desc->hw.td.buffer));
1851 ret = USB_ST_URB_INVALID_ERROR;
1852 purb->iso_frame_desc[i].status = ret;
1853 goto err;
1855 purb->iso_frame_desc[i].actual_length = (desc->hw.td.status + 1) & 0x7ff;
1856 purb->iso_frame_desc[i].status = uhci_map_status (uhci_status_bits (desc->hw.td.status), usb_pipeout (purb->pipe));
1857 purb->actual_length += purb->iso_frame_desc[i].actual_length;
1859 err:
1861 if (purb->iso_frame_desc[i].status != USB_ST_NOERROR) {
1862 purb->error_count++;
1863 purb->status = purb->iso_frame_desc[i].status;
1865 dbg (KERN_DEBUG MODSTR "process_iso: len:%d status:%x\n",
1866 purb->iso_frame_desc[i].length, purb->iso_frame_desc[i].status);
1868 delete_desc (desc);
1869 list_del (p);
1871 dbg ( /*KERN_DEBUG */ MODSTR "process_iso: exit %i (%d)\n", i, ret);
1872 return ret;
1876 _static int process_urb (puhci_t s, struct list_head *p)
1878 int ret = USB_ST_NOERROR;
1879 purb_t purb;
1881 spin_lock(&s->urb_list_lock);
1882 purb=list_entry (p, urb_t, urb_list);
1883 dbg ( /*KERN_DEBUG */ MODSTR "found queued urb: %p\n", purb);
1885 switch (usb_pipetype (purb->pipe)) {
1886 case PIPE_CONTROL:
1887 case PIPE_BULK:
1888 ret = process_transfer (s, purb);
1889 break;
1890 case PIPE_ISOCHRONOUS:
1891 ret = process_iso (s, purb);
1892 break;
1893 case PIPE_INTERRUPT:
1894 ret = process_interrupt (s, purb);
1895 break;
1898 spin_unlock(&s->urb_list_lock);
1900 if (purb->status != USB_ST_URB_PENDING) {
1901 int proceed = 0;
1902 dbg ( /*KERN_DEBUG */ MODSTR "dequeued urb: %p\n", purb);
1903 dequeue_urb (s, p, 1);
1905 #ifdef _UHCI_SLAB
1906 kmem_cache_free(urb_priv_kmem, purb->hcpriv);
1907 #else
1908 kfree (purb->hcpriv);
1909 #endif
1911 if ((usb_pipetype (purb->pipe) != PIPE_INTERRUPT)) {
1912 purb_t tmp = purb->next; // pointer to first urb
1913 int is_ring = 0;
1915 if (purb->next) {
1916 do {
1917 if (tmp->status != USB_ST_URB_PENDING) {
1918 proceed = 1;
1919 break;
1921 tmp = tmp->next;
1923 while (tmp != NULL && tmp != purb->next);
1924 if (tmp == purb->next)
1925 is_ring = 1;
1928 // In case you need the current URB status for your completion handler
1929 if (purb->complete && (!proceed || (purb->transfer_flags & USB_URB_EARLY_COMPLETE))) {
1930 dbg (KERN_DEBUG MODSTR "process_transfer: calling early completion\n");
1931 purb->complete ((struct urb *) purb);
1932 if (!proceed && is_ring && (purb->status != USB_ST_URB_KILLED))
1933 uhci_submit_urb (purb);
1936 if (proceed && purb->next) {
1937 // if there are linked urbs - handle submitting of them right now.
1938 tmp = purb->next; // pointer to first urb
1940 do {
1941 if ((tmp->status != USB_ST_URB_PENDING) && (tmp->status != USB_ST_URB_KILLED) && uhci_submit_urb (tmp) != USB_ST_NOERROR)
1942 break;
1943 tmp = tmp->next;
1945 while (tmp != NULL && tmp != purb->next); // submit until we reach NULL or our own pointer or submit fails
1947 if (purb->complete && !(purb->transfer_flags & USB_URB_EARLY_COMPLETE)) {
1948 dbg ( /*KERN_DEBUG */ MODSTR "process_transfer: calling completion\n");
1949 purb->complete ((struct urb *) purb);
1952 usb_dec_dev_use (purb->dev);
1956 return ret;
1959 _static void uhci_interrupt (int irq, void *__uhci, struct pt_regs *regs)
1961 puhci_t s = __uhci;
1962 unsigned int io_addr = s->io_addr;
1963 unsigned short status;
1964 struct list_head *p, *p2;
1967 * Read the interrupt status, and write it back to clear the
1968 * interrupt cause
1970 dbg ("interrupt\n");
1971 status = inw (io_addr + USBSTS);
1973 if (!status) /* shared interrupt, not mine */
1974 return;
1976 if (status != 1) {
1977 printk (KERN_DEBUG MODSTR "interrupt, status %x\n", status);
1978 //uhci_show_status (s);
1980 //beep(1000);
1982 * the following is very subtle and was blatantly wrong before
1983 * traverse the list in *reverse* direction, because new entries
1984 * may be added at the end.
1985 * also, because process_urb may unlink the current urb,
1986 * we need to advance the list before
1987 * - Thomas Sailer
1990 spin_lock(&s->unlink_urb_lock);
1991 spin_lock (&s->urb_list_lock);
1992 p = s->urb_list.prev;
1993 spin_unlock (&s->urb_list_lock);
1995 while (p != &s->urb_list) {
1996 p2 = p;
1997 p = p->prev;
1998 process_urb (s, p2);
2001 spin_unlock(&s->unlink_urb_lock);
2003 outw (status, io_addr + USBSTS);
2004 #ifdef __alpha
2005 mb (); // ?
2006 #endif
2007 dbg ("done\n");
2010 _static void reset_hc (puhci_t s)
2012 unsigned int io_addr = s->io_addr;
2014 s->apm_state = 0;
2015 /* Global reset for 50ms */
2016 outw (USBCMD_GRESET, io_addr + USBCMD);
2017 wait_ms (50);
2018 outw (0, io_addr + USBCMD);
2019 wait_ms (10);
2022 _static void start_hc (puhci_t s)
2024 unsigned int io_addr = s->io_addr;
2025 int timeout = 1000;
2028 * Reset the HC - this will force us to get a
2029 * new notification of any already connected
2030 * ports due to the virtual disconnect that it
2031 * implies.
2033 outw (USBCMD_HCRESET, io_addr + USBCMD);
2035 while (inw (io_addr + USBCMD) & USBCMD_HCRESET) {
2036 if (!--timeout) {
2037 printk (KERN_ERR MODSTR "USBCMD_HCRESET timed out!\n");
2038 break;
2042 /* Turn on all interrupts */
2043 outw (USBINTR_TIMEOUT | USBINTR_RESUME | USBINTR_IOC | USBINTR_SP, io_addr + USBINTR);
2045 /* Start at frame 0 */
2046 outw (0, io_addr + USBFRNUM);
2047 outl (virt_to_bus (s->framelist), io_addr + USBFLBASEADD);
2049 /* Run and mark it configured with a 64-byte max packet */
2050 outw (USBCMD_RS | USBCMD_CF | USBCMD_MAXP, io_addr + USBCMD);
2051 s->apm_state = 1;
2054 _static void __exit uhci_cleanup_dev(puhci_t s)
2056 struct usb_device *root_hub = s->bus->root_hub;
2057 if (root_hub)
2058 usb_disconnect (&root_hub);
2060 usb_deregister_bus (s->bus);
2062 reset_hc (s);
2063 release_region (s->io_addr, s->io_size);
2064 free_irq (s->irq, s);
2065 usb_free_bus (s->bus);
2066 cleanup_skel (s);
2067 kfree (s);
2071 _static int __init uhci_start_usb (puhci_t s)
2072 { /* start it up */
2073 /* connect the virtual root hub */
2074 struct usb_device *usb_dev;
2076 usb_dev = usb_alloc_dev (NULL, s->bus);
2077 if (!usb_dev)
2078 return -1;
2080 s->bus->root_hub = usb_dev;
2081 usb_connect (usb_dev);
2083 if (usb_new_device (usb_dev) != 0) {
2084 usb_free_dev (usb_dev);
2085 return -1;
2088 return 0;
2091 _static int __init alloc_uhci (int irq, unsigned int io_addr, unsigned int io_size)
2093 puhci_t s;
2094 struct usb_bus *bus;
2096 s = kmalloc (sizeof (uhci_t), GFP_KERNEL);
2097 if (!s)
2098 return -1;
2100 memset (s, 0, sizeof (uhci_t));
2101 INIT_LIST_HEAD (&s->urb_list);
2102 spin_lock_init (&s->urb_list_lock);
2103 spin_lock_init (&s->qh_lock);
2104 spin_lock_init (&s->td_lock);
2105 spin_lock_init (&s->unlink_urb_lock);
2106 s->irq = -1;
2107 s->io_addr = io_addr;
2108 s->io_size = io_size;
2109 s->next = devs; //chain new uhci device into global list
2111 bus = usb_alloc_bus (&uhci_device_operations);
2112 if (!bus) {
2113 kfree (s);
2114 return -1;
2117 s->bus = bus;
2118 bus->hcpriv = s;
2120 /* UHCI specs says devices must have 2 ports, but goes on to say */
2121 /* they may have more but give no way to determine how many they */
2122 /* have, so default to 2 */
2123 /* According to the UHCI spec, Bit 7 is always set to 1. So we try */
2124 /* to use this to our advantage */
2126 for (s->maxports = 0; s->maxports < (io_size - 0x10) / 2; s->maxports++) {
2127 unsigned int portstatus;
2129 portstatus = inw (io_addr + 0x10 + (s->maxports * 2));
2130 printk ("port %i, adr %x status %x\n", s->maxports,
2131 io_addr + 0x10 + (s->maxports * 2), portstatus);
2132 if (!(portstatus & 0x0080))
2133 break;
2135 dbg (KERN_DEBUG MODSTR "Detected %d ports\n", s->maxports);
2137 /* This is experimental so anything less than 2 or greater than 8 is */
2138 /* something weird and we'll ignore it */
2139 if (s->maxports < 2 || s->maxports > 8) {
2140 dbg (KERN_DEBUG "Port count misdetected, forcing to 2 ports\n");
2141 s->maxports = 2;
2144 s->rh.numports = s->maxports;
2146 if (init_skel (s)) {
2147 usb_free_bus (bus);
2148 kfree(s);
2149 return -1;
2152 request_region (s->io_addr, io_size, MODNAME);
2153 reset_hc (s);
2154 usb_register_bus (s->bus);
2156 start_hc (s);
2158 if (request_irq (irq, uhci_interrupt, SA_SHIRQ, MODNAME, s)) {
2159 printk(MODSTR KERN_ERR"request_irq %d failed!\n",irq);
2160 usb_free_bus (bus);
2161 reset_hc (s);
2162 release_region (s->io_addr, s->io_size);
2163 cleanup_skel(s);
2164 kfree(s);
2165 return -1;
2168 s->irq = irq;
2170 if(uhci_start_usb (s) < 0) {
2171 uhci_cleanup_dev(s);
2172 return -1;
2175 //chain new uhci device into global list
2176 devs = s;
2178 return 0;
2181 _static int __init start_uhci (struct pci_dev *dev)
2183 int i;
2185 /* Search for the IO base address.. */
2186 for (i = 0; i < 6; i++) {
2187 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,3,8)
2188 unsigned int io_addr = dev->resource[i].start;
2189 unsigned int io_size =
2190 dev->resource[i].end - dev->resource[i].start + 1;
2191 if (!(dev->resource[i].flags & 1))
2192 continue;
2193 #else
2194 unsigned int io_addr = dev->base_address[i];
2195 unsigned int io_size = 0x14;
2196 if (!(io_addr & 1))
2197 continue;
2198 io_addr &= ~1;
2199 #endif
2201 /* Is it already in use? */
2202 if (check_region (io_addr, io_size))
2203 break;
2204 /* disable legacy emulation */
2205 pci_write_config_word (dev, USBLEGSUP, USBLEGSUP_DEFAULT);
2207 return alloc_uhci(dev->irq, io_addr, io_size);
2209 return -1;
2212 #ifdef CONFIG_APM
2213 _static int handle_apm_event (apm_event_t event)
2215 static int down = 0;
2216 puhci_t s = devs;
2217 printk ("handle_apm_event(%d)\n", event);
2218 switch (event) {
2219 case APM_SYS_SUSPEND:
2220 case APM_USER_SUSPEND:
2221 if (down) {
2222 dbg (KERN_DEBUG MODSTR "received extra suspend event\n");
2223 break;
2225 while (s) {
2226 reset_hc (s);
2227 s = s->next;
2229 down = 1;
2230 break;
2231 case APM_NORMAL_RESUME:
2232 case APM_CRITICAL_RESUME:
2233 if (!down) {
2234 dbg (KERN_DEBUG MODSTR "received bogus resume event\n");
2235 break;
2237 down = 0;
2238 while (s) {
2239 start_hc (s);
2240 s = s->next;
2242 break;
2244 return 0;
2246 #endif
2248 int __init uhci_init (void)
2250 int retval = -ENODEV;
2251 struct pci_dev *dev = NULL;
2252 u8 type;
2253 int i=0;
2255 #ifdef _UHCI_SLAB
2256 char *slabname=kmalloc(16, GFP_KERNEL);
2258 if(!slabname)
2259 return -ENOMEM;
2261 strcpy(slabname, "uhci_desc");
2262 uhci_desc_kmem = kmem_cache_create(slabname, sizeof(uhci_desc_t), 0, SLAB_HWCACHE_ALIGN, NULL, NULL);
2264 if(!uhci_desc_kmem) {
2265 printk(KERN_ERR MODSTR"kmem_cache_create for uhci_desc failed (out of memory)\n");
2266 return -ENOMEM;
2269 slabname=kmalloc(16, GFP_KERNEL);
2271 if(!slabname)
2272 return -ENOMEM;
2274 strcpy(slabname, "urb_priv");
2275 urb_priv_kmem = kmem_cache_create(slabname, sizeof(urb_priv_t), 0, SLAB_HWCACHE_ALIGN, NULL, NULL);
2277 if(!urb_priv_kmem) {
2278 printk(KERN_ERR MODSTR"kmem_cache_create for urb_priv_t failed (out of memory)\n");
2279 return -ENOMEM;
2281 #endif
2282 printk (KERN_INFO MODSTR VERSTR "\n");
2284 for (;;) {
2285 dev = pci_find_class (PCI_CLASS_SERIAL_USB << 8, dev);
2286 if (!dev)
2287 break;
2289 /* Is it UHCI */
2290 pci_read_config_byte (dev, PCI_CLASS_PROG, &type);
2291 if (type != 0)
2292 continue;
2294 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,3,8)
2295 pci_enable_device (dev);
2296 #endif
2297 if(!dev->irq)
2299 printk(KERN_ERR MODSTR"Found UHCI device with no IRQ assigned. Check BIOS settings!\n");
2300 continue;
2303 /* Ok set it up */
2304 retval = start_uhci (dev);
2306 if (!retval)
2307 i++;
2311 #ifdef CONFIG_APM
2312 if(i)
2313 apm_register_callback (&handle_apm_event);
2314 #endif
2315 return retval;
2318 void __exit uhci_cleanup (void)
2320 puhci_t s;
2321 while ((s = devs)) {
2322 devs = devs->next;
2323 uhci_cleanup_dev(s);
2325 #ifdef _UHCI_SLAB
2326 kmem_cache_shrink(uhci_desc_kmem);
2327 kmem_cache_shrink(urb_priv_kmem);
2328 #endif
2331 #ifdef MODULE
2332 int init_module (void)
2334 return uhci_init ();
2337 void cleanup_module (void)
2339 #ifdef CONFIG_APM
2340 apm_unregister_callback (&handle_apm_event);
2341 #endif
2342 uhci_cleanup ();
2345 #endif //MODULE