- Andries Brouwer: final isofs pieces.
[davej-history.git] / drivers / usb / uhci.c
blobec039aaa24db5533551822d4047fbc9e9debfd60
1 /*
2 * Universal Host Controller Interface driver for USB.
4 * (C) Copyright 1999 Linus Torvalds
5 * (C) Copyright 1999-2000 Johannes Erdfelt, johannes@erdfelt.com
6 * (C) Copyright 1999 Randy Dunlap
7 * (C) Copyright 1999 Georg Acher, acher@in.tum.de
8 * (C) Copyright 1999 Deti Fliegl, deti@fliegl.de
9 * (C) Copyright 1999 Thomas Sailer, sailer@ife.ee.ethz.ch
10 * (C) Copyright 1999 Roman Weissgaerber, weissg@vienna.at
11 * (C) Copyright 2000 Yggdrasil Computing, Inc. (port of new PCI interface
12 * support from usb-ohci.c by Adam Richter, adam@yggdrasil.com).
13 * (C) Copyright 1999 Gregory P. Smith (from usb-ohci.c)
16 * Intel documents this fairly well, and as far as I know there
17 * are no royalties or anything like that, but even so there are
18 * people who decided that they want to do the same thing in a
19 * completely different way.
21 * WARNING! The USB documentation is downright evil. Most of it
22 * is just crap, written by a committee. You're better off ignoring
23 * most of it, the important stuff is:
24 * - the low-level protocol (fairly simple but lots of small details)
25 * - working around the horridness of the rest
28 #include <linux/module.h>
29 #include <linux/pci.h>
30 #include <linux/kernel.h>
31 #include <linux/init.h>
32 #include <linux/delay.h>
33 #include <linux/ioport.h>
34 #include <linux/sched.h>
35 #include <linux/malloc.h>
36 #include <linux/smp_lock.h>
37 #include <linux/errno.h>
38 #include <linux/unistd.h>
39 #include <linux/interrupt.h>
40 #include <linux/spinlock.h>
41 #define DEBUG
42 #include <linux/usb.h>
44 #include <asm/uaccess.h>
45 #include <asm/io.h>
46 #include <asm/irq.h>
47 #include <asm/system.h>
49 #include "uhci.h"
50 #include "uhci-debug.h"
52 #include <linux/pm.h>
54 static int debug = 1;
55 MODULE_PARM(debug, "i");
56 MODULE_PARM_DESC(debug, "Debug level");
58 static kmem_cache_t *uhci_td_cachep;
59 static kmem_cache_t *uhci_qh_cachep;
60 static kmem_cache_t *uhci_up_cachep; /* urb_priv */
62 static int rh_submit_urb(struct urb *urb);
63 static int rh_unlink_urb(struct urb *urb);
64 static int uhci_get_current_frame_number(struct usb_device *dev);
65 static int uhci_unlink_generic(struct urb *urb);
66 static int uhci_unlink_urb(struct urb *urb);
68 #define min(a,b) (((a)<(b))?(a):(b))
70 /* If a transfer is still active after this much time, turn off FSBR */
71 #define IDLE_TIMEOUT (HZ / 20) /* 50 ms */
74 * Only the USB core should call uhci_alloc_dev and uhci_free_dev
76 static int uhci_alloc_dev(struct usb_device *dev)
78 return 0;
81 static int uhci_free_dev(struct usb_device *dev)
83 struct uhci *uhci = (struct uhci *)dev->bus->hcpriv;
84 struct list_head *tmp, *head = &uhci->urb_list;
85 unsigned long flags;
87 /* Walk through the entire URB list and forcefully remove any */
88 /* URBs that are still active for that device */
89 nested_lock(&uhci->urblist_lock, flags);
90 tmp = head->next;
91 while (tmp != head) {
92 struct urb *u = list_entry(tmp, struct urb, urb_list);
94 tmp = tmp->next;
96 if (u->dev == dev)
97 uhci_unlink_urb(u);
99 nested_unlock(&uhci->urblist_lock, flags);
101 return 0;
104 static void uhci_add_urb_list(struct uhci *uhci, struct urb *urb)
106 unsigned long flags;
108 nested_lock(&uhci->urblist_lock, flags);
109 list_add(&urb->urb_list, &uhci->urb_list);
110 nested_unlock(&uhci->urblist_lock, flags);
113 static void uhci_remove_urb_list(struct uhci *uhci, struct urb *urb)
115 unsigned long flags;
117 nested_lock(&uhci->urblist_lock, flags);
118 if (!list_empty(&urb->urb_list)) {
119 list_del(&urb->urb_list);
120 INIT_LIST_HEAD(&urb->urb_list);
122 nested_unlock(&uhci->urblist_lock, flags);
125 void uhci_set_next_interrupt(struct uhci *uhci)
127 unsigned long flags;
129 spin_lock_irqsave(&uhci->framelist_lock, flags);
130 uhci->skel_term_td.status |= TD_CTRL_IOC;
131 spin_unlock_irqrestore(&uhci->framelist_lock, flags);
134 void uhci_clear_next_interrupt(struct uhci *uhci)
136 unsigned long flags;
138 spin_lock_irqsave(&uhci->framelist_lock, flags);
139 uhci->skel_term_td.status &= ~TD_CTRL_IOC;
140 spin_unlock_irqrestore(&uhci->framelist_lock, flags);
143 static struct uhci_td *uhci_alloc_td(struct usb_device *dev)
145 struct uhci_td *td;
147 td = kmem_cache_alloc(uhci_td_cachep, in_interrupt() ? SLAB_ATOMIC : SLAB_KERNEL);
148 if (!td)
149 return NULL;
151 td->link = UHCI_PTR_TERM;
152 td->buffer = 0;
154 td->frameptr = NULL;
155 td->nexttd = td->prevtd = NULL;
156 td->dev = dev;
157 INIT_LIST_HEAD(&td->list);
159 usb_inc_dev_use(dev);
161 return td;
164 static void inline uhci_fill_td(struct uhci_td *td, __u32 status,
165 __u32 info, __u32 buffer)
167 td->status = status;
168 td->info = info;
169 td->buffer = buffer;
172 static void uhci_insert_td(struct uhci *uhci, struct uhci_td *skeltd, struct uhci_td *td)
174 unsigned long flags;
176 spin_lock_irqsave(&uhci->framelist_lock, flags);
178 /* Fix the linked list pointers */
179 td->nexttd = skeltd->nexttd;
180 td->prevtd = skeltd;
181 if (skeltd->nexttd)
182 skeltd->nexttd->prevtd = td;
183 skeltd->nexttd = td;
185 td->link = skeltd->link;
186 skeltd->link = virt_to_bus(td);
188 spin_unlock_irqrestore(&uhci->framelist_lock, flags);
192 * We insert Isochronous transfers directly into the frame list at the
193 * beginning
194 * The layout looks as follows:
195 * frame list pointer -> iso td's (if any) ->
196 * periodic interrupt td (if frame 0) -> irq td's -> control qh -> bulk qh
199 static void uhci_insert_td_frame_list(struct uhci *uhci, struct uhci_td *td, unsigned framenum)
201 unsigned long flags;
202 struct uhci_td *nexttd;
204 framenum %= UHCI_NUMFRAMES;
206 spin_lock_irqsave(&uhci->framelist_lock, flags);
208 td->frameptr = &uhci->fl->frame[framenum];
209 td->link = uhci->fl->frame[framenum];
210 if (!(td->link & (UHCI_PTR_TERM | UHCI_PTR_QH))) {
211 nexttd = (struct uhci_td *)uhci_ptr_to_virt(td->link);
212 td->nexttd = nexttd;
213 nexttd->prevtd = td;
214 nexttd->frameptr = NULL;
216 uhci->fl->frame[framenum] = virt_to_bus(td);
218 spin_unlock_irqrestore(&uhci->framelist_lock, flags);
221 static void uhci_remove_td(struct uhci *uhci, struct uhci_td *td)
223 unsigned long flags;
225 /* If it's not inserted, don't remove it */
226 if (!td->frameptr && !td->prevtd && !td->nexttd)
227 return;
229 spin_lock_irqsave(&uhci->framelist_lock, flags);
230 if (td->frameptr) {
231 *(td->frameptr) = td->link;
232 if (td->nexttd) {
233 td->nexttd->frameptr = td->frameptr;
234 td->nexttd->prevtd = NULL;
235 td->nexttd = NULL;
237 td->frameptr = NULL;
238 } else {
239 if (td->prevtd) {
240 td->prevtd->nexttd = td->nexttd;
241 td->prevtd->link = td->link;
243 if (td->nexttd)
244 td->nexttd->prevtd = td->prevtd;
245 td->prevtd = td->nexttd = NULL;
247 td->link = UHCI_PTR_TERM;
248 spin_unlock_irqrestore(&uhci->framelist_lock, flags);
252 * Inserts a td into qh list at the top.
254 static void uhci_insert_tds_in_qh(struct uhci_qh *qh, struct urb *urb, int breadth)
256 struct list_head *tmp, *head;
257 struct urb_priv *urbp = (struct urb_priv *)urb->hcpriv;
258 struct uhci_td *td, *prevtd;
260 if (!urbp)
261 return;
263 head = &urbp->list;
264 tmp = head->next;
265 if (head == tmp)
266 return;
268 td = list_entry(tmp, struct uhci_td, list);
270 /* Add the first TD to the QH element pointer */
271 qh->element = virt_to_bus(td) | (breadth ? 0 : UHCI_PTR_DEPTH);
273 prevtd = td;
275 /* Then link the rest of the TD's */
276 tmp = tmp->next;
277 while (tmp != head) {
278 td = list_entry(tmp, struct uhci_td, list);
280 tmp = tmp->next;
282 prevtd->link = virt_to_bus(td) | (breadth ? 0 : UHCI_PTR_DEPTH);
284 prevtd = td;
287 prevtd->link = UHCI_PTR_TERM;
290 static void uhci_free_td(struct uhci_td *td)
292 if (!list_empty(&td->list))
293 dbg("td is still in URB list!");
295 if (td->dev)
296 usb_dec_dev_use(td->dev);
298 kmem_cache_free(uhci_td_cachep, td);
301 static struct uhci_qh *uhci_alloc_qh(struct usb_device *dev)
303 struct uhci_qh *qh;
305 qh = kmem_cache_alloc(uhci_qh_cachep, in_interrupt() ? SLAB_ATOMIC : SLAB_KERNEL);
306 if (!qh)
307 return NULL;
309 qh->element = UHCI_PTR_TERM;
310 qh->link = UHCI_PTR_TERM;
312 qh->dev = dev;
313 qh->prevqh = qh->nextqh = NULL;
315 INIT_LIST_HEAD(&qh->remove_list);
317 usb_inc_dev_use(dev);
319 return qh;
322 static void uhci_free_qh(struct uhci_qh *qh)
324 if (qh->dev)
325 usb_dec_dev_use(qh->dev);
327 kmem_cache_free(uhci_qh_cachep, qh);
330 static void uhci_insert_qh(struct uhci *uhci, struct uhci_qh *skelqh, struct uhci_qh *qh)
332 unsigned long flags;
334 spin_lock_irqsave(&uhci->framelist_lock, flags);
336 /* Fix the linked list pointers */
337 qh->nextqh = skelqh->nextqh;
338 qh->prevqh = skelqh;
339 if (skelqh->nextqh)
340 skelqh->nextqh->prevqh = qh;
341 skelqh->nextqh = qh;
343 qh->link = skelqh->link;
344 skelqh->link = virt_to_bus(qh) | UHCI_PTR_QH;
346 spin_unlock_irqrestore(&uhci->framelist_lock, flags);
349 static void uhci_remove_qh(struct uhci *uhci, struct uhci_qh *qh)
351 unsigned long flags;
352 int delayed;
354 /* If the QH isn't queued, then we don't need to delay unlink it */
355 delayed = (qh->prevqh || qh->nextqh);
357 spin_lock_irqsave(&uhci->framelist_lock, flags);
358 if (qh->prevqh) {
359 qh->prevqh->nextqh = qh->nextqh;
360 qh->prevqh->link = qh->link;
362 if (qh->nextqh)
363 qh->nextqh->prevqh = qh->prevqh;
364 qh->prevqh = qh->nextqh = NULL;
365 qh->element = qh->link = UHCI_PTR_TERM;
366 spin_unlock_irqrestore(&uhci->framelist_lock, flags);
368 if (delayed) {
369 spin_lock_irqsave(&uhci->qh_remove_lock, flags);
371 /* Check to see if the remove list is empty */
372 /* Set the IOC bit to force an interrupt so we can remove the QH */
373 if (list_empty(&uhci->qh_remove_list))
374 uhci_set_next_interrupt(uhci);
376 /* Add it */
377 list_add(&qh->remove_list, &uhci->qh_remove_list);
379 spin_unlock_irqrestore(&uhci->qh_remove_lock, flags);
380 } else
381 uhci_free_qh(qh);
384 static spinlock_t uhci_append_urb_lock = SPIN_LOCK_UNLOCKED;
386 /* This function will append one URB's QH to another URB's QH. This is for */
387 /* USB_QUEUE_BULK support */
388 static void uhci_append_queued_urb(struct uhci *uhci, struct urb *eurb, struct urb *urb)
390 struct urb_priv *eurbp, *urbp, *furbp, *lurbp;
391 struct list_head *tmp;
392 struct uhci_td *td, *ltd;
393 unsigned long flags;
395 eurbp = eurb->hcpriv;
396 urbp = urb->hcpriv;
398 spin_lock_irqsave(&uhci_append_urb_lock, flags);
400 /* Find the beginning URB in the queue */
401 if (eurbp->queued) {
402 struct list_head *head = &eurbp->urb_queue_list;
404 tmp = head->next;
405 while (tmp != head) {
406 struct urb_priv *turbp =
407 list_entry(tmp, struct urb_priv, urb_queue_list);
409 tmp = tmp->next;
411 if (!turbp->queued)
412 break;
414 } else
415 tmp = &eurbp->urb_queue_list;
417 furbp = list_entry(tmp, struct urb_priv, urb_queue_list);
419 tmp = furbp->urb_queue_list.prev;
420 lurbp = list_entry(tmp, struct urb_priv, urb_queue_list);
422 /* Add this one to the end */
423 list_add_tail(&urbp->urb_queue_list, &furbp->urb_queue_list);
425 /* Grab the last TD from the last URB */
426 ltd = list_entry(lurbp->list.prev, struct uhci_td, list);
428 /* Grab the first TD from the first URB */
429 td = list_entry(urbp->list.next, struct uhci_td, list);
431 /* No breadth since this will only be called for bulk transfers */
432 ltd->link = virt_to_bus(td);
434 spin_unlock_irqrestore(&uhci_append_urb_lock, flags);
437 static void uhci_delete_queued_urb(struct uhci *uhci, struct urb *urb)
439 struct urb_priv *urbp, *nurbp;
440 unsigned long flags;
442 urbp = urb->hcpriv;
444 spin_lock_irqsave(&uhci_append_urb_lock, flags);
446 nurbp = list_entry(urbp->urb_queue_list.next, struct urb_priv,
447 urb_queue_list);
449 if (!urbp->queued) {
450 /* We're the head, so just insert the QH for the next URB */
451 uhci_insert_qh(uhci, &uhci->skel_bulk_qh, nurbp->qh);
452 nurbp->queued = 0;
453 } else {
454 struct urb_priv *purbp;
455 struct uhci_td *ptd;
457 /* We're somewhere in the middle (or end). A bit trickier */
458 /* than the head scenario */
459 purbp = list_entry(urbp->urb_queue_list.prev, struct urb_priv,
460 urb_queue_list);
462 ptd = list_entry(purbp->list.prev, struct uhci_td, list);
463 if (nurbp->queued)
464 /* Close the gap between the two */
465 ptd->link = virt_to_bus(list_entry(nurbp->list.next,
466 struct uhci_td, list));
467 else
468 /* The next URB happens to be the beggining, so */
469 /* we're the last, end the chain */
470 ptd->link = UHCI_PTR_TERM;
474 list_del(&urbp->urb_queue_list);
476 spin_unlock_irqrestore(&uhci_append_urb_lock, flags);
479 struct urb_priv *uhci_alloc_urb_priv(struct urb *urb)
481 struct urb_priv *urbp;
483 urbp = kmem_cache_alloc(uhci_up_cachep, in_interrupt() ? SLAB_ATOMIC : SLAB_KERNEL);
484 if (!urbp)
485 return NULL;
487 memset((void *)urbp, 0, sizeof(*urbp));
489 urbp->inserttime = jiffies;
490 urbp->urb = urb;
492 INIT_LIST_HEAD(&urbp->list);
493 INIT_LIST_HEAD(&urbp->urb_queue_list);
495 urb->hcpriv = urbp;
497 return urbp;
500 static void uhci_add_td_to_urb(struct urb *urb, struct uhci_td *td)
502 struct urb_priv *urbp = (struct urb_priv *)urb->hcpriv;
504 td->urb = urb;
506 list_add_tail(&td->list, &urbp->list);
509 static void uhci_remove_td_from_urb(struct urb *urb, struct uhci_td *td)
511 urb = NULL; /* No warnings */
513 if (list_empty(&td->list))
514 return;
516 list_del(&td->list);
517 INIT_LIST_HEAD(&td->list);
519 td->urb = NULL;
522 static void uhci_destroy_urb_priv(struct urb *urb)
524 struct list_head *tmp, *head;
525 struct urb_priv *urbp;
526 struct uhci *uhci;
527 struct uhci_td *td;
528 unsigned long flags;
530 spin_lock_irqsave(&urb->lock, flags);
532 urbp = (struct urb_priv *)urb->hcpriv;
533 if (!urbp)
534 goto unlock;
536 if (!urb->dev || !urb->dev->bus || !urb->dev->bus->hcpriv)
537 goto unlock;
539 uhci = urb->dev->bus->hcpriv;
541 head = &urbp->list;
542 tmp = head->next;
543 while (tmp != head) {
544 td = list_entry(tmp, struct uhci_td, list);
546 tmp = tmp->next;
548 uhci_remove_td_from_urb(urb, td);
550 uhci_remove_td(uhci, td);
552 uhci_free_td(td);
555 urb->hcpriv = NULL;
556 kmem_cache_free(uhci_up_cachep, urbp);
558 unlock:
559 spin_unlock_irqrestore(&urb->lock, flags);
562 static void uhci_inc_fsbr(struct uhci *uhci, struct urb *urb)
564 unsigned long flags;
565 struct urb_priv *urbp = (struct urb_priv *)urb->hcpriv;
567 if (!urbp)
568 return;
570 spin_lock_irqsave(&uhci->framelist_lock, flags);
572 if ((!(urb->transfer_flags & USB_NO_FSBR)) && (!urbp->fsbr)) {
573 urbp->fsbr = 1;
574 if (!uhci->fsbr++)
575 uhci->skel_term_qh.link = virt_to_bus(&uhci->skel_hs_control_qh) | UHCI_PTR_QH;
578 spin_unlock_irqrestore(&uhci->framelist_lock, flags);
581 static void uhci_dec_fsbr(struct uhci *uhci, struct urb *urb)
583 unsigned long flags;
584 struct urb_priv *urbp = (struct urb_priv *)urb->hcpriv;
586 if (!urbp)
587 return;
589 spin_lock_irqsave(&uhci->framelist_lock, flags);
591 if ((!(urb->transfer_flags & USB_NO_FSBR)) && urbp->fsbr) {
592 urbp->fsbr = 0;
593 if (!--uhci->fsbr)
594 uhci->skel_term_qh.link = UHCI_PTR_TERM;
597 spin_unlock_irqrestore(&uhci->framelist_lock, flags);
601 * Map status to standard result codes
603 * <status> is (td->status & 0xFE0000) [a.k.a. uhci_status_bits(td->status)]
604 * <dir_out> is True for output TDs and False for input TDs.
606 static int uhci_map_status(int status, int dir_out)
608 if (!status)
609 return 0;
610 if (status & TD_CTRL_BITSTUFF) /* Bitstuff error */
611 return -EPROTO;
612 if (status & TD_CTRL_CRCTIMEO) { /* CRC/Timeout */
613 if (dir_out)
614 return -ETIMEDOUT;
615 else
616 return -EILSEQ;
618 if (status & TD_CTRL_NAK) /* NAK */
619 return -ETIMEDOUT;
620 if (status & TD_CTRL_BABBLE) /* Babble */
621 return -EPIPE;
622 if (status & TD_CTRL_DBUFERR) /* Buffer error */
623 return -ENOSR;
624 if (status & TD_CTRL_STALLED) /* Stalled */
625 return -EPIPE;
626 if (status & TD_CTRL_ACTIVE) /* Active */
627 return 0;
629 return -EINVAL;
633 * Control transfers
635 static int uhci_submit_control(struct urb *urb)
637 struct urb_priv *urbp = (struct urb_priv *)urb->hcpriv;
638 struct uhci *uhci = (struct uhci *)urb->dev->bus->hcpriv;
639 struct uhci_td *td;
640 struct uhci_qh *qh;
641 unsigned long destination, status;
642 int maxsze = usb_maxpacket(urb->dev, urb->pipe, usb_pipeout(urb->pipe));
643 int len = urb->transfer_buffer_length;
644 unsigned char *data = urb->transfer_buffer;
646 /* The "pipe" thing contains the destination in bits 8--18 */
647 destination = (urb->pipe & PIPE_DEVEP_MASK) | USB_PID_SETUP;
649 /* 3 errors */
650 status = (urb->pipe & TD_CTRL_LS) | TD_CTRL_ACTIVE | (3 << 27);
653 * Build the TD for the control request
655 td = uhci_alloc_td(urb->dev);
656 if (!td)
657 return -ENOMEM;
659 uhci_add_td_to_urb(urb, td);
660 uhci_fill_td(td, status, destination | (7 << 21),
661 virt_to_bus(urb->setup_packet));
664 * If direction is "send", change the frame from SETUP (0x2D)
665 * to OUT (0xE1). Else change it from SETUP to IN (0x69).
667 destination ^= (USB_PID_SETUP ^ usb_packetid(urb->pipe));
669 if (!(urb->transfer_flags & USB_DISABLE_SPD))
670 status |= TD_CTRL_SPD;
673 * Build the DATA TD's
675 while (len > 0) {
676 int pktsze = len;
678 if (pktsze > maxsze)
679 pktsze = maxsze;
681 td = uhci_alloc_td(urb->dev);
682 if (!td)
683 return -ENOMEM;
685 /* Alternate Data0/1 (start with Data1) */
686 destination ^= 1 << TD_TOKEN_TOGGLE;
688 uhci_add_td_to_urb(urb, td);
689 uhci_fill_td(td, status, destination | ((pktsze - 1) << 21),
690 virt_to_bus(data));
692 data += pktsze;
693 len -= pktsze;
697 * Build the final TD for control status
699 td = uhci_alloc_td(urb->dev);
700 if (!td)
701 return -ENOMEM;
704 * It's IN if the pipe is an output pipe or we're not expecting
705 * data back.
707 destination &= ~TD_PID;
708 if (usb_pipeout(urb->pipe) || !urb->transfer_buffer_length)
709 destination |= USB_PID_IN;
710 else
711 destination |= USB_PID_OUT;
713 destination |= 1 << TD_TOKEN_TOGGLE; /* End in Data1 */
715 status &= ~TD_CTRL_SPD;
717 uhci_add_td_to_urb(urb, td);
718 uhci_fill_td(td, status | TD_CTRL_IOC,
719 destination | (UHCI_NULL_DATA_SIZE << 21), 0);
721 qh = uhci_alloc_qh(urb->dev);
722 if (!qh)
723 return -ENOMEM;
725 /* Low speed or small transfers gets a different queue and treatment */
726 if (urb->pipe & TD_CTRL_LS) {
727 uhci_insert_tds_in_qh(qh, urb, 0);
728 uhci_insert_qh(uhci, &uhci->skel_ls_control_qh, qh);
729 } else {
730 uhci_insert_tds_in_qh(qh, urb, 1);
731 uhci_insert_qh(uhci, &uhci->skel_hs_control_qh, qh);
732 uhci_inc_fsbr(uhci, urb);
735 urbp->qh = qh;
737 uhci_add_urb_list(uhci, urb);
739 return -EINPROGRESS;
742 static int usb_control_retrigger_status(struct urb *urb);
744 static int uhci_result_control(struct urb *urb)
746 struct list_head *tmp, *head;
747 struct urb_priv *urbp = urb->hcpriv;
748 struct uhci_td *td;
749 unsigned int status;
750 int ret = 0;
752 if (!urbp)
753 return -EINVAL;
755 head = &urbp->list;
756 if (head->next == head)
757 return -EINVAL;
759 if (urbp->short_control_packet) {
760 tmp = head->prev;
761 goto status_phase;
764 tmp = head->next;
765 td = list_entry(tmp, struct uhci_td, list);
767 /* The first TD is the SETUP phase, check the status, but skip */
768 /* the count */
769 status = uhci_status_bits(td->status);
770 if (status & TD_CTRL_ACTIVE)
771 return -EINPROGRESS;
773 if (status)
774 goto td_error;
776 urb->actual_length = 0;
778 /* The rest of the TD's (but the last) are data */
779 tmp = tmp->next;
780 while (tmp != head && tmp->next != head) {
781 td = list_entry(tmp, struct uhci_td, list);
783 tmp = tmp->next;
785 if (urbp->fsbr_timeout && (td->status & TD_CTRL_IOC) &&
786 !(td->status & TD_CTRL_ACTIVE)) {
787 uhci_inc_fsbr(urb->dev->bus->hcpriv, urb);
788 urbp->fsbr_timeout = 0;
789 td->status &= ~TD_CTRL_IOC;
792 status = uhci_status_bits(td->status);
793 if (status & TD_CTRL_ACTIVE)
794 return -EINPROGRESS;
796 urb->actual_length += uhci_actual_length(td->status);
798 if (status)
799 goto td_error;
801 /* Check to see if we received a short packet */
802 if (uhci_actual_length(td->status) < uhci_expected_length(td->info)) {
803 if (urb->transfer_flags & USB_DISABLE_SPD) {
804 ret = -EREMOTEIO;
805 goto err;
808 if (uhci_packetid(td->info) == USB_PID_IN)
809 return usb_control_retrigger_status(urb);
810 else
811 return 0;
815 status_phase:
816 td = list_entry(tmp, struct uhci_td, list);
818 /* Control status phase */
819 status = uhci_status_bits(td->status);
821 #ifdef I_HAVE_BUGGY_APC_BACKUPS
822 /* APC BackUPS Pro kludge */
823 /* It tries to send all of the descriptor instead of the amount */
824 /* we requested */
825 if (td->status & TD_CTRL_IOC && /* IOC is masked out by uhci_status_bits */
826 status & TD_CTRL_ACTIVE &&
827 status & TD_CTRL_NAK)
828 return 0;
829 #endif
831 if (status & TD_CTRL_ACTIVE)
832 return -EINPROGRESS;
834 if (status)
835 goto td_error;
837 return 0;
839 td_error:
840 ret = uhci_map_status(status, uhci_packetout(td->info));
841 if (ret == -EPIPE)
842 /* endpoint has stalled - mark it halted */
843 usb_endpoint_halt(urb->dev, uhci_endpoint(td->info),
844 uhci_packetout(td->info));
846 err:
847 if (debug && ret != -EPIPE) {
848 /* Some debugging code */
849 dbg("uhci_result_control() failed with status %x", status);
851 /* Print the chain for debugging purposes */
852 uhci_show_urb_queue(urb);
855 return ret;
858 static int usb_control_retrigger_status(struct urb *urb)
860 struct list_head *tmp, *head;
861 struct urb_priv *urbp = (struct urb_priv *)urb->hcpriv;
862 struct uhci *uhci = urb->dev->bus->hcpriv;
864 urbp->short_control_packet = 1;
866 /* Create a new QH to avoid pointer overwriting problems */
867 uhci_remove_qh(uhci, urbp->qh);
869 /* Delete all of the TD's except for the status TD at the end */
870 head = &urbp->list;
871 tmp = head->next;
872 while (tmp != head && tmp->next != head) {
873 struct uhci_td *td = list_entry(tmp, struct uhci_td, list);
875 tmp = tmp->next;
877 uhci_remove_td_from_urb(urb, td);
879 uhci_remove_td(uhci, td);
881 uhci_free_td(td);
884 urbp->qh = uhci_alloc_qh(urb->dev);
885 if (!urbp->qh) {
886 err("unable to allocate new QH for control retrigger");
887 return -ENOMEM;
890 /* One TD, who cares about Breadth first? */
891 uhci_insert_tds_in_qh(urbp->qh, urb, 0);
893 /* Low speed or small transfers gets a different queue and treatment */
894 if (urb->pipe & TD_CTRL_LS)
895 uhci_insert_qh(uhci, &uhci->skel_ls_control_qh, urbp->qh);
896 else
897 uhci_insert_qh(uhci, &uhci->skel_hs_control_qh, urbp->qh);
899 return -EINPROGRESS;
903 * Interrupt transfers
905 static int uhci_submit_interrupt(struct urb *urb)
907 struct uhci_td *td;
908 unsigned long destination, status;
909 struct uhci *uhci = (struct uhci *)urb->dev->bus->hcpriv;
911 if (urb->transfer_buffer_length > usb_maxpacket(urb->dev, urb->pipe, usb_pipeout(urb->pipe)))
912 return -EINVAL;
914 /* The "pipe" thing contains the destination in bits 8--18 */
915 destination = (urb->pipe & PIPE_DEVEP_MASK) | usb_packetid(urb->pipe);
917 status = (urb->pipe & TD_CTRL_LS) | TD_CTRL_ACTIVE | TD_CTRL_IOC;
919 td = uhci_alloc_td(urb->dev);
920 if (!td)
921 return -ENOMEM;
923 destination |= (usb_gettoggle(urb->dev, usb_pipeendpoint(urb->pipe), usb_pipeout(urb->pipe)) << TD_TOKEN_TOGGLE);
924 destination |= ((urb->transfer_buffer_length - 1) << 21);
926 usb_dotoggle(urb->dev, usb_pipeendpoint(urb->pipe), usb_pipeout(urb->pipe));
928 uhci_add_td_to_urb(urb, td);
929 uhci_fill_td(td, status, destination,
930 virt_to_bus(urb->transfer_buffer));
932 uhci_insert_td(uhci, &uhci->skeltd[__interval_to_skel(urb->interval)], td);
934 uhci_add_urb_list(uhci, urb);
936 return -EINPROGRESS;
939 static int uhci_result_interrupt(struct urb *urb)
941 struct list_head *tmp, *head;
942 struct urb_priv *urbp = urb->hcpriv;
943 struct uhci_td *td;
944 unsigned int status;
945 int ret = 0;
947 if (!urbp)
948 return -EINVAL;
950 urb->actual_length = 0;
952 head = &urbp->list;
953 tmp = head->next;
954 while (tmp != head) {
955 td = list_entry(tmp, struct uhci_td, list);
957 tmp = tmp->next;
959 if (urbp->fsbr_timeout && (td->status & TD_CTRL_IOC) &&
960 !(td->status & TD_CTRL_ACTIVE)) {
961 uhci_inc_fsbr(urb->dev->bus->hcpriv, urb);
962 urbp->fsbr_timeout = 0;
963 td->status &= ~TD_CTRL_IOC;
966 status = uhci_status_bits(td->status);
967 if (status & TD_CTRL_ACTIVE)
968 return -EINPROGRESS;
970 urb->actual_length += uhci_actual_length(td->status);
972 if (status)
973 goto td_error;
975 if (uhci_actual_length(td->status) < uhci_expected_length(td->info)) {
976 usb_settoggle(urb->dev, uhci_endpoint(td->info),
977 uhci_packetout(td->info),
978 uhci_toggle(td->info) ^ 1);
980 if (urb->transfer_flags & USB_DISABLE_SPD) {
981 ret = -EREMOTEIO;
982 goto err;
983 } else
984 return 0;
988 return 0;
990 td_error:
991 ret = uhci_map_status(status, uhci_packetout(td->info));
992 if (ret == -EPIPE)
993 /* endpoint has stalled - mark it halted */
994 usb_endpoint_halt(urb->dev, uhci_endpoint(td->info),
995 uhci_packetout(td->info));
997 err:
998 if (debug && ret != -EPIPE) {
999 /* Some debugging code */
1000 dbg("uhci_result_interrupt/bulk() failed with status %x",
1001 status);
1003 /* Print the chain for debugging purposes */
1004 if (urbp->qh)
1005 uhci_show_urb_queue(urb);
1006 else
1007 uhci_show_td(td);
1010 return ret;
1013 static void uhci_reset_interrupt(struct urb *urb)
1015 struct list_head *tmp;
1016 struct urb_priv *urbp = (struct urb_priv *)urb->hcpriv;
1017 struct uhci_td *td;
1019 if (!urbp)
1020 return;
1022 tmp = urbp->list.next;
1023 td = list_entry(tmp, struct uhci_td, list);
1024 if (!td)
1025 return;
1027 td->status = (td->status & 0x2F000000) | TD_CTRL_ACTIVE | TD_CTRL_IOC;
1028 td->info &= ~(1 << TD_TOKEN_TOGGLE);
1029 td->info |= (usb_gettoggle(urb->dev, usb_pipeendpoint(urb->pipe), usb_pipeout(urb->pipe)) << TD_TOKEN_TOGGLE);
1030 usb_dotoggle(urb->dev, usb_pipeendpoint(urb->pipe), usb_pipeout(urb->pipe));
1032 urb->status = -EINPROGRESS;
1036 * Bulk transfers
1038 static int uhci_submit_bulk(struct urb *urb, struct urb *eurb)
1040 struct uhci_td *td;
1041 struct uhci_qh *qh;
1042 unsigned long destination, status;
1043 struct uhci *uhci = (struct uhci *)urb->dev->bus->hcpriv;
1044 int maxsze = usb_maxpacket(urb->dev, urb->pipe, usb_pipeout(urb->pipe));
1045 int len = urb->transfer_buffer_length;
1046 unsigned char *data = urb->transfer_buffer;
1047 struct urb_priv *urbp = (struct urb_priv *)urb->hcpriv;
1049 if (len < 0)
1050 return -EINVAL;
1052 /* Can't have low speed bulk transfers */
1053 if (urb->pipe & TD_CTRL_LS)
1054 return -EINVAL;
1056 /* The "pipe" thing contains the destination in bits 8--18 */
1057 destination = (urb->pipe & PIPE_DEVEP_MASK) | usb_packetid(urb->pipe);
1059 /* 3 errors */
1060 status = TD_CTRL_ACTIVE | (3 << TD_CTRL_C_ERR_SHIFT);
1062 if (!(urb->transfer_flags & USB_DISABLE_SPD))
1063 status |= TD_CTRL_SPD;
1066 * Build the DATA TD's
1068 do { /* Allow zero length packets */
1069 int pktsze = len;
1071 if (pktsze > maxsze)
1072 pktsze = maxsze;
1074 td = uhci_alloc_td(urb->dev);
1075 if (!td)
1076 return -ENOMEM;
1078 uhci_add_td_to_urb(urb, td);
1079 uhci_fill_td(td, status, destination | ((pktsze - 1) << 21) |
1080 (usb_gettoggle(urb->dev, usb_pipeendpoint(urb->pipe),
1081 usb_pipeout(urb->pipe)) << TD_TOKEN_TOGGLE),
1082 virt_to_bus(data));
1084 data += pktsze;
1085 len -= maxsze;
1087 if (len <= 0)
1088 td->status |= TD_CTRL_IOC;
1090 usb_dotoggle(urb->dev, usb_pipeendpoint(urb->pipe),
1091 usb_pipeout(urb->pipe));
1092 } while (len > 0);
1094 qh = uhci_alloc_qh(urb->dev);
1095 if (!qh)
1096 return -ENOMEM;
1098 urbp->qh = qh;
1100 /* Always assume depth first */
1101 uhci_insert_tds_in_qh(qh, urb, 1);
1103 if (urb->transfer_flags & USB_QUEUE_BULK && eurb) {
1104 urbp->queued = 1;
1105 uhci_append_queued_urb(uhci, eurb, urb);
1106 } else
1107 uhci_insert_qh(uhci, &uhci->skel_bulk_qh, qh);
1109 uhci_add_urb_list(uhci, urb);
1111 uhci_inc_fsbr(uhci, urb);
1113 return -EINPROGRESS;
1116 /* We can use the result interrupt since they're identical */
1117 #define uhci_result_bulk uhci_result_interrupt
1120 * Isochronous transfers
1122 static int isochronous_find_limits(struct urb *urb, unsigned int *start, unsigned int *end)
1124 struct urb *last_urb = NULL;
1125 struct uhci *uhci = (struct uhci *)urb->dev->bus->hcpriv;
1126 struct list_head *tmp, *head = &uhci->urb_list;
1127 int ret = 0;
1128 unsigned long flags;
1130 nested_lock(&uhci->urblist_lock, flags);
1131 tmp = head->next;
1132 while (tmp != head) {
1133 struct urb *u = list_entry(tmp, struct urb, urb_list);
1135 tmp = tmp->next;
1137 /* look for pending URB's with identical pipe handle */
1138 if ((urb->pipe == u->pipe) && (urb->dev == u->dev) &&
1139 (u->status == -EINPROGRESS) && (u != urb)) {
1140 if (!last_urb)
1141 *start = u->start_frame;
1142 last_urb = u;
1146 if (last_urb) {
1147 *end = (last_urb->start_frame + last_urb->number_of_packets) & 1023;
1148 ret = 0;
1149 } else
1150 ret = -1; /* no previous urb found */
1152 nested_unlock(&uhci->urblist_lock, flags);
1154 return ret;
1157 static int isochronous_find_start(struct urb *urb)
1159 int limits;
1160 unsigned int start = 0, end = 0;
1162 if (urb->number_of_packets > 900) /* 900? Why? */
1163 return -EFBIG;
1165 limits = isochronous_find_limits(urb, &start, &end);
1167 if (urb->transfer_flags & USB_ISO_ASAP) {
1168 if (limits) {
1169 int curframe;
1171 curframe = uhci_get_current_frame_number(urb->dev) % UHCI_NUMFRAMES;
1172 urb->start_frame = (curframe + 10) % UHCI_NUMFRAMES;
1173 } else
1174 urb->start_frame = end;
1175 } else {
1176 urb->start_frame %= UHCI_NUMFRAMES;
1177 /* FIXME: Sanity check */
1180 return 0;
1183 static int uhci_submit_isochronous(struct urb *urb)
1185 struct uhci_td *td;
1186 struct uhci *uhci = (struct uhci *)urb->dev->bus->hcpriv;
1187 int i, ret, framenum;
1188 int status, destination;
1190 status = TD_CTRL_ACTIVE | TD_CTRL_IOS;
1191 destination = (urb->pipe & PIPE_DEVEP_MASK) | usb_packetid(urb->pipe);
1193 ret = isochronous_find_start(urb);
1194 if (ret)
1195 return ret;
1197 framenum = urb->start_frame;
1198 for (i = 0; i < urb->number_of_packets; i++, framenum++) {
1199 if (!urb->iso_frame_desc[i].length)
1200 continue;
1202 td = uhci_alloc_td(urb->dev);
1203 if (!td)
1204 return -ENOMEM;
1206 uhci_add_td_to_urb(urb, td);
1207 uhci_fill_td(td, status, destination | ((urb->iso_frame_desc[i].length - 1) << 21),
1208 virt_to_bus(urb->transfer_buffer + urb->iso_frame_desc[i].offset));
1210 if (i + 1 >= urb->number_of_packets)
1211 td->status |= TD_CTRL_IOC;
1213 uhci_insert_td_frame_list(uhci, td, framenum);
1216 uhci_add_urb_list(uhci, urb);
1218 return -EINPROGRESS;
1221 static int uhci_result_isochronous(struct urb *urb)
1223 struct list_head *tmp, *head;
1224 struct urb_priv *urbp = (struct urb_priv *)urb->hcpriv;
1225 int status;
1226 int i, ret = 0;
1228 if (!urbp)
1229 return -EINVAL;
1231 urb->actual_length = 0;
1233 i = 0;
1234 head = &urbp->list;
1235 tmp = head->next;
1236 while (tmp != head) {
1237 struct uhci_td *td = list_entry(tmp, struct uhci_td, list);
1238 int actlength;
1240 tmp = tmp->next;
1242 if (td->status & TD_CTRL_ACTIVE)
1243 return -EINPROGRESS;
1245 actlength = uhci_actual_length(td->status);
1246 urb->iso_frame_desc[i].actual_length = actlength;
1247 urb->actual_length += actlength;
1249 status = uhci_map_status(uhci_status_bits(td->status), usb_pipeout(urb->pipe));
1250 urb->iso_frame_desc[i].status = status;
1251 if (status != 0) {
1252 urb->error_count++;
1253 ret = status;
1256 i++;
1259 return ret;
1262 static struct urb *uhci_find_urb_ep(struct uhci *uhci, struct urb *urb)
1264 struct list_head *tmp, *head = &uhci->urb_list;
1265 unsigned long flags;
1266 struct urb *u = NULL;
1268 if (usb_pipeisoc(urb->pipe))
1269 return NULL;
1271 nested_lock(&uhci->urblist_lock, flags);
1272 tmp = head->next;
1273 while (tmp != head) {
1274 u = list_entry(tmp, struct urb, urb_list);
1276 tmp = tmp->next;
1278 if (u->dev == urb->dev &&
1279 u->pipe == urb->pipe)
1280 goto found;
1282 u = NULL;
1284 found:
1285 nested_unlock(&uhci->urblist_lock, flags);
1287 return u;
1290 static int uhci_submit_urb(struct urb *urb)
1292 int ret = -EINVAL;
1293 struct uhci *uhci;
1294 unsigned long flags;
1295 struct urb *u;
1296 int bustime;
1298 if (!urb)
1299 return -EINVAL;
1301 if (!urb->dev || !urb->dev->bus || !urb->dev->bus->hcpriv)
1302 return -ENODEV;
1304 uhci = (struct uhci *)urb->dev->bus->hcpriv;
1306 /* Short circuit the virtual root hub */
1307 if (usb_pipedevice(urb->pipe) == uhci->rh.devnum)
1308 return rh_submit_urb(urb);
1310 u = uhci_find_urb_ep(uhci, urb);
1311 if (u && !(urb->transfer_flags & USB_QUEUE_BULK))
1312 return -ENXIO;
1314 usb_inc_dev_use(urb->dev);
1315 spin_lock_irqsave(&urb->lock, flags);
1317 if (!uhci_alloc_urb_priv(urb)) {
1318 spin_unlock_irqrestore(&urb->lock, flags);
1319 usb_dec_dev_use(urb->dev);
1321 return -ENOMEM;
1324 switch (usb_pipetype(urb->pipe)) {
1325 case PIPE_CONTROL:
1326 ret = uhci_submit_control(urb);
1327 break;
1328 case PIPE_INTERRUPT:
1329 if (urb->bandwidth == 0) { /* not yet checked/allocated */
1330 bustime = usb_check_bandwidth(urb->dev, urb);
1331 if (bustime < 0)
1332 ret = bustime;
1333 else {
1334 ret = uhci_submit_interrupt(urb);
1335 if (ret == -EINPROGRESS)
1336 usb_claim_bandwidth(urb->dev, urb, bustime, 0);
1338 } else /* bandwidth is already set */
1339 ret = uhci_submit_interrupt(urb);
1340 break;
1341 case PIPE_BULK:
1342 ret = uhci_submit_bulk(urb, u);
1343 break;
1344 case PIPE_ISOCHRONOUS:
1345 if (urb->bandwidth == 0) { /* not yet checked/allocated */
1346 if (urb->number_of_packets <= 0) {
1347 ret = -EINVAL;
1348 break;
1350 bustime = usb_check_bandwidth(urb->dev, urb);
1351 if (bustime < 0) {
1352 ret = bustime;
1353 break;
1356 ret = uhci_submit_isochronous(urb);
1357 if (ret == -EINPROGRESS)
1358 usb_claim_bandwidth(urb->dev, urb, bustime, 1);
1359 } else /* bandwidth is already set */
1360 ret = uhci_submit_isochronous(urb);
1361 break;
1364 urb->status = ret;
1366 spin_unlock_irqrestore(&urb->lock, flags);
1368 if (ret == -EINPROGRESS)
1369 ret = 0;
1370 else {
1371 uhci_unlink_generic(urb);
1372 usb_dec_dev_use(urb->dev);
1375 return ret;
1379 * Return the result of a transfer
1381 * Must be called with urblist_lock acquired
1383 static void uhci_transfer_result(struct urb *urb)
1385 struct usb_device *dev = urb->dev;
1386 struct urb *turb;
1387 int proceed = 0, is_ring = 0;
1388 int ret = -EINVAL;
1389 unsigned long flags;
1391 spin_lock_irqsave(&urb->lock, flags);
1393 switch (usb_pipetype(urb->pipe)) {
1394 case PIPE_CONTROL:
1395 ret = uhci_result_control(urb);
1396 break;
1397 case PIPE_INTERRUPT:
1398 ret = uhci_result_interrupt(urb);
1399 break;
1400 case PIPE_BULK:
1401 ret = uhci_result_bulk(urb);
1402 break;
1403 case PIPE_ISOCHRONOUS:
1404 ret = uhci_result_isochronous(urb);
1405 break;
1408 urb->status = ret;
1410 spin_unlock_irqrestore(&urb->lock, flags);
1412 if (ret == -EINPROGRESS)
1413 return;
1415 switch (usb_pipetype(urb->pipe)) {
1416 case PIPE_CONTROL:
1417 case PIPE_BULK:
1418 case PIPE_ISOCHRONOUS:
1419 /* Release bandwidth for Interrupt or Isoc. transfers */
1420 /* Spinlock needed ? */
1421 if (urb->bandwidth)
1422 usb_release_bandwidth(urb->dev, urb, 1);
1423 uhci_unlink_generic(urb);
1424 break;
1425 case PIPE_INTERRUPT:
1426 /* Interrupts are an exception */
1427 if (urb->interval) {
1428 urb->complete(urb);
1429 uhci_reset_interrupt(urb);
1430 return;
1433 /* Release bandwidth for Interrupt or Isoc. transfers */
1434 /* Spinlock needed ? */
1435 if (urb->bandwidth)
1436 usb_release_bandwidth(urb->dev, urb, 0);
1437 uhci_unlink_generic(urb);
1438 break;
1441 if (urb->next) {
1442 turb = urb->next;
1443 do {
1444 if (turb->status != -EINPROGRESS) {
1445 proceed = 1;
1446 break;
1449 turb = turb->next;
1450 } while (turb && turb != urb && turb != urb->next);
1452 if (turb == urb || turb == urb->next)
1453 is_ring = 1;
1456 if (urb->complete && !proceed) {
1457 urb->complete(urb);
1458 if (!proceed && is_ring)
1459 uhci_submit_urb(urb);
1462 if (proceed && urb->next) {
1463 turb = urb->next;
1464 do {
1465 if (turb->status != -EINPROGRESS &&
1466 uhci_submit_urb(turb) != 0)
1468 turb = turb->next;
1469 } while (turb && turb != urb->next);
1471 if (urb->complete)
1472 urb->complete(urb);
1475 /* We decrement the usage count after we're done with everything */
1476 usb_dec_dev_use(dev);
1479 static int uhci_unlink_generic(struct urb *urb)
1481 struct urb_priv *urbp = urb->hcpriv;
1482 struct uhci *uhci = (struct uhci *)urb->dev->bus->hcpriv;
1484 if (!urbp)
1485 return -EINVAL;
1487 uhci_dec_fsbr(uhci, urb); /* Safe since it checks */
1489 uhci_remove_urb_list(uhci, urb);
1491 if (urbp->qh)
1492 /* The interrupt loop will reclaim the QH's */
1493 uhci_remove_qh(uhci, urbp->qh);
1495 if (!list_empty(&urbp->urb_queue_list))
1496 uhci_delete_queued_urb(uhci, urb);
1498 uhci_destroy_urb_priv(urb);
1500 urb->dev = NULL;
1502 return 0;
1505 static int uhci_unlink_urb(struct urb *urb)
1507 struct uhci *uhci;
1508 int ret = 0;
1509 unsigned long flags;
1511 if (!urb)
1512 return -EINVAL;
1514 if (!urb->dev || !urb->dev->bus)
1515 return -ENODEV;
1517 uhci = (struct uhci *)urb->dev->bus->hcpriv;
1519 /* Short circuit the virtual root hub */
1520 if (usb_pipedevice(urb->pipe) == uhci->rh.devnum)
1521 return rh_unlink_urb(urb);
1523 /* Release bandwidth for Interrupt or Isoc. transfers */
1524 /* Spinlock needed ? */
1525 if (urb->bandwidth) {
1526 switch (usb_pipetype(urb->pipe)) {
1527 case PIPE_INTERRUPT:
1528 usb_release_bandwidth(urb->dev, urb, 0);
1529 break;
1530 case PIPE_ISOCHRONOUS:
1531 usb_release_bandwidth(urb->dev, urb, 1);
1532 break;
1533 default:
1534 break;
1538 if (urb->status == -EINPROGRESS) {
1539 uhci_unlink_generic(urb);
1541 if (urb->transfer_flags & USB_ASYNC_UNLINK) {
1542 urb->status = -ECONNABORTED;
1544 spin_lock_irqsave(&uhci->urb_remove_lock, flags);
1546 /* Check to see if the remove list is empty */
1547 if (list_empty(&uhci->urb_remove_list))
1548 uhci_set_next_interrupt(uhci);
1550 list_add(&urb->urb_list, &uhci->urb_remove_list);
1552 spin_unlock_irqrestore(&uhci->urb_remove_lock, flags);
1553 } else {
1554 urb->status = -ENOENT;
1556 if (in_interrupt()) { /* wait at least 1 frame */
1557 static int errorcount = 10;
1559 if (errorcount--)
1560 dbg("uhci_unlink_urb called from interrupt for urb %p", urb);
1561 udelay(1000);
1562 } else
1563 schedule_timeout(1+1*HZ/1000);
1565 if (urb->complete)
1566 urb->complete(urb);
1570 return ret;
1573 static int uhci_fsbr_timeout(struct uhci *uhci, struct urb *urb)
1575 struct urb_priv *urbp = (struct urb_priv *)urb->hcpriv;
1576 struct list_head *head, *tmp;
1578 uhci_dec_fsbr(uhci, urb);
1580 /* There is a race with updating IOC in here, but it's not worth */
1581 /* trying to fix since this is merely an optimization. The only */
1582 /* time we'd lose is if the status of the packet got updated */
1583 /* and we'd be turning on FSBR next frame anyway, so it's a wash */
1584 urbp->fsbr_timeout = 1;
1586 head = &urbp->list;
1587 tmp = head->next;
1588 while (tmp != head) {
1589 struct uhci_td *td = list_entry(tmp, struct uhci_td, list);
1591 tmp = tmp->next;
1593 if (td->status & TD_CTRL_ACTIVE) {
1594 td->status |= TD_CTRL_IOC;
1595 break;
1599 return 0;
1603 * uhci_get_current_frame_number()
1605 * returns the current frame number for a USB bus/controller.
1607 static int uhci_get_current_frame_number(struct usb_device *dev)
1609 struct uhci *uhci = (struct uhci *)dev->bus->hcpriv;
1611 return inw(uhci->io_addr + USBFRNUM);
1614 struct usb_operations uhci_device_operations = {
1615 uhci_alloc_dev,
1616 uhci_free_dev,
1617 uhci_get_current_frame_number,
1618 uhci_submit_urb,
1619 uhci_unlink_urb
1622 /* -------------------------------------------------------------------
1623 Virtual Root Hub
1624 ------------------------------------------------------------------- */
1626 static __u8 root_hub_dev_des[] =
1628 0x12, /* __u8 bLength; */
1629 0x01, /* __u8 bDescriptorType; Device */
1630 0x00, /* __u16 bcdUSB; v1.0 */
1631 0x01,
1632 0x09, /* __u8 bDeviceClass; HUB_CLASSCODE */
1633 0x00, /* __u8 bDeviceSubClass; */
1634 0x00, /* __u8 bDeviceProtocol; */
1635 0x08, /* __u8 bMaxPacketSize0; 8 Bytes */
1636 0x00, /* __u16 idVendor; */
1637 0x00,
1638 0x00, /* __u16 idProduct; */
1639 0x00,
1640 0x00, /* __u16 bcdDevice; */
1641 0x00,
1642 0x00, /* __u8 iManufacturer; */
1643 0x02, /* __u8 iProduct; */
1644 0x01, /* __u8 iSerialNumber; */
1645 0x01 /* __u8 bNumConfigurations; */
1649 /* Configuration descriptor */
1650 static __u8 root_hub_config_des[] =
1652 0x09, /* __u8 bLength; */
1653 0x02, /* __u8 bDescriptorType; Configuration */
1654 0x19, /* __u16 wTotalLength; */
1655 0x00,
1656 0x01, /* __u8 bNumInterfaces; */
1657 0x01, /* __u8 bConfigurationValue; */
1658 0x00, /* __u8 iConfiguration; */
1659 0x40, /* __u8 bmAttributes;
1660 Bit 7: Bus-powered, 6: Self-powered,
1661 Bit 5 Remote-wakeup, 4..0: resvd */
1662 0x00, /* __u8 MaxPower; */
1664 /* interface */
1665 0x09, /* __u8 if_bLength; */
1666 0x04, /* __u8 if_bDescriptorType; Interface */
1667 0x00, /* __u8 if_bInterfaceNumber; */
1668 0x00, /* __u8 if_bAlternateSetting; */
1669 0x01, /* __u8 if_bNumEndpoints; */
1670 0x09, /* __u8 if_bInterfaceClass; HUB_CLASSCODE */
1671 0x00, /* __u8 if_bInterfaceSubClass; */
1672 0x00, /* __u8 if_bInterfaceProtocol; */
1673 0x00, /* __u8 if_iInterface; */
1675 /* endpoint */
1676 0x07, /* __u8 ep_bLength; */
1677 0x05, /* __u8 ep_bDescriptorType; Endpoint */
1678 0x81, /* __u8 ep_bEndpointAddress; IN Endpoint 1 */
1679 0x03, /* __u8 ep_bmAttributes; Interrupt */
1680 0x08, /* __u16 ep_wMaxPacketSize; 8 Bytes */
1681 0x00,
1682 0xff /* __u8 ep_bInterval; 255 ms */
1685 static __u8 root_hub_hub_des[] =
1687 0x09, /* __u8 bLength; */
1688 0x29, /* __u8 bDescriptorType; Hub-descriptor */
1689 0x02, /* __u8 bNbrPorts; */
1690 0x00, /* __u16 wHubCharacteristics; */
1691 0x00,
1692 0x01, /* __u8 bPwrOn2pwrGood; 2ms */
1693 0x00, /* __u8 bHubContrCurrent; 0 mA */
1694 0x00, /* __u8 DeviceRemovable; *** 7 Ports max *** */
1695 0xff /* __u8 PortPwrCtrlMask; *** 7 ports max *** */
1698 /*-------------------------------------------------------------------------*/
1699 /* prepare Interrupt pipe transaction data; HUB INTERRUPT ENDPOINT */
1700 static int rh_send_irq(struct urb *urb)
1702 int i, len = 1;
1703 struct uhci *uhci = (struct uhci *)urb->dev->bus->hcpriv;
1704 unsigned int io_addr = uhci->io_addr;
1705 __u16 data = 0;
1707 for (i = 0; i < uhci->rh.numports; i++) {
1708 data |= ((inw(io_addr + USBPORTSC1 + i * 2) & 0xa) > 0 ? (1 << (i + 1)) : 0);
1709 len = (i + 1) / 8 + 1;
1712 *(__u16 *) urb->transfer_buffer = cpu_to_le16(data);
1713 urb->actual_length = len;
1714 urb->status = USB_ST_NOERROR;
1716 if ((data > 0) && (uhci->rh.send != 0)) {
1717 dbg("root-hub INT complete: port1: %x port2: %x data: %x",
1718 inw(io_addr + USBPORTSC1), inw(io_addr + USBPORTSC2), data);
1719 urb->complete(urb);
1722 return USB_ST_NOERROR;
1725 /*-------------------------------------------------------------------------*/
1726 /* Virtual Root Hub INTs are polled by this timer every "interval" ms */
1727 static int rh_init_int_timer(struct urb *urb);
1729 static void rh_int_timer_do(unsigned long ptr)
1731 struct urb *urb = (struct urb *)ptr;
1732 struct uhci *uhci = (struct uhci *)urb->dev->bus->hcpriv;
1733 struct list_head *tmp, *head = &uhci->urb_list;
1734 struct urb_priv *urbp;
1735 int len;
1736 unsigned long flags;
1738 if (uhci->rh.send) {
1739 len = rh_send_irq(urb);
1740 if (len > 0) {
1741 urb->actual_length = len;
1742 if (urb->complete)
1743 urb->complete(urb);
1747 nested_lock(&uhci->urblist_lock, flags);
1748 tmp = head->next;
1749 while (tmp != head) {
1750 struct urb *u = list_entry(tmp, urb_t, urb_list);
1752 tmp = tmp->next;
1754 urbp = (struct urb_priv *)u->hcpriv;
1755 if (urbp) {
1756 /* Check if the FSBR timed out */
1757 if (urbp->fsbr && time_after(urbp->inserttime + IDLE_TIMEOUT, jiffies))
1758 uhci_fsbr_timeout(uhci, u);
1760 /* Check if the URB timed out */
1761 if (u->timeout && time_after(u->timeout, jiffies)) {
1762 u->transfer_flags |= USB_ASYNC_UNLINK | USB_TIMEOUT_KILLED;
1763 uhci_unlink_urb(u);
1767 nested_unlock(&uhci->urblist_lock, flags);
1769 rh_init_int_timer(urb);
1772 /*-------------------------------------------------------------------------*/
1773 /* Root Hub INTs are polled by this timer */
1774 static int rh_init_int_timer(struct urb *urb)
1776 struct uhci *uhci = (struct uhci *)urb->dev->bus->hcpriv;
1778 uhci->rh.interval = urb->interval;
1779 init_timer(&uhci->rh.rh_int_timer);
1780 uhci->rh.rh_int_timer.function = rh_int_timer_do;
1781 uhci->rh.rh_int_timer.data = (unsigned long)urb;
1782 uhci->rh.rh_int_timer.expires = jiffies + (HZ * (urb->interval < 30 ? 30 : urb->interval)) / 1000;
1783 add_timer(&uhci->rh.rh_int_timer);
1785 return 0;
1788 /*-------------------------------------------------------------------------*/
1789 #define OK(x) len = (x); break
1791 #define CLR_RH_PORTSTAT(x) \
1792 status = inw(io_addr + USBPORTSC1 + 2 * (wIndex-1)); \
1793 status = (status & 0xfff5) & ~(x); \
1794 outw(status, io_addr + USBPORTSC1 + 2 * (wIndex-1))
1796 #define SET_RH_PORTSTAT(x) \
1797 status = inw(io_addr + USBPORTSC1 + 2 * (wIndex-1)); \
1798 status = (status & 0xfff5) | (x); \
1799 outw(status, io_addr + USBPORTSC1 + 2 * (wIndex-1))
1802 /*-------------------------------------------------------------------------*/
1803 /*************************
1804 ** Root Hub Control Pipe
1805 *************************/
1807 static int rh_submit_urb(struct urb *urb)
1809 struct uhci *uhci = (struct uhci *)urb->dev->bus->hcpriv;
1810 unsigned int pipe = urb->pipe;
1811 devrequest *cmd = (devrequest *)urb->setup_packet;
1812 void *data = urb->transfer_buffer;
1813 int leni = urb->transfer_buffer_length;
1814 int len = 0;
1815 int status = 0;
1816 int stat = USB_ST_NOERROR;
1817 int i;
1818 unsigned int io_addr = uhci->io_addr;
1819 __u16 cstatus;
1820 __u16 bmRType_bReq;
1821 __u16 wValue;
1822 __u16 wIndex;
1823 __u16 wLength;
1825 if (usb_pipetype(pipe) == PIPE_INTERRUPT) {
1826 uhci->rh.urb = urb;
1827 uhci->rh.send = 1;
1828 uhci->rh.interval = urb->interval;
1829 rh_init_int_timer(urb);
1831 return USB_ST_NOERROR;
1834 bmRType_bReq = cmd->requesttype | cmd->request << 8;
1835 wValue = le16_to_cpu(cmd->value);
1836 wIndex = le16_to_cpu(cmd->index);
1837 wLength = le16_to_cpu(cmd->length);
1839 for (i = 0; i < 8; i++)
1840 uhci->rh.c_p_r[i] = 0;
1842 switch (bmRType_bReq) {
1843 /* Request Destination:
1844 without flags: Device,
1845 RH_INTERFACE: interface,
1846 RH_ENDPOINT: endpoint,
1847 RH_CLASS means HUB here,
1848 RH_OTHER | RH_CLASS almost ever means HUB_PORT here
1851 case RH_GET_STATUS:
1852 *(__u16 *)data = cpu_to_le16(1);
1853 OK(2);
1854 case RH_GET_STATUS | RH_INTERFACE:
1855 *(__u16 *)data = cpu_to_le16(0);
1856 OK(2);
1857 case RH_GET_STATUS | RH_ENDPOINT:
1858 *(__u16 *)data = cpu_to_le16(0);
1859 OK(2);
1860 case RH_GET_STATUS | RH_CLASS:
1861 *(__u32 *)data = cpu_to_le32(0);
1862 OK(4); /* hub power */
1863 case RH_GET_STATUS | RH_OTHER | RH_CLASS:
1864 status = inw(io_addr + USBPORTSC1 + 2 * (wIndex - 1));
1865 cstatus = ((status & USBPORTSC_CSC) >> (1 - 0)) |
1866 ((status & USBPORTSC_PEC) >> (3 - 1)) |
1867 (uhci->rh.c_p_r[wIndex - 1] << (0 + 4));
1868 status = (status & USBPORTSC_CCS) |
1869 ((status & USBPORTSC_PE) >> (2 - 1)) |
1870 ((status & USBPORTSC_SUSP) >> (12 - 2)) |
1871 ((status & USBPORTSC_PR) >> (9 - 4)) |
1872 (1 << 8) | /* power on */
1873 ((status & USBPORTSC_LSDA) << (-8 + 9));
1875 *(__u16 *)data = cpu_to_le16(status);
1876 *(__u16 *)(data + 2) = cpu_to_le16(cstatus);
1877 OK(4);
1878 case RH_CLEAR_FEATURE | RH_ENDPOINT:
1879 switch (wValue) {
1880 case RH_ENDPOINT_STALL:
1881 OK(0);
1883 break;
1884 case RH_CLEAR_FEATURE | RH_CLASS:
1885 switch (wValue) {
1886 case RH_C_HUB_OVER_CURRENT:
1887 OK(0); /* hub power over current */
1889 break;
1890 case RH_CLEAR_FEATURE | RH_OTHER | RH_CLASS:
1891 switch (wValue) {
1892 case RH_PORT_ENABLE:
1893 CLR_RH_PORTSTAT(USBPORTSC_PE);
1894 OK(0);
1895 case RH_PORT_SUSPEND:
1896 CLR_RH_PORTSTAT(USBPORTSC_SUSP);
1897 OK(0);
1898 case RH_PORT_POWER:
1899 OK(0); /* port power */
1900 case RH_C_PORT_CONNECTION:
1901 SET_RH_PORTSTAT(USBPORTSC_CSC);
1902 OK(0);
1903 case RH_C_PORT_ENABLE:
1904 SET_RH_PORTSTAT(USBPORTSC_PEC);
1905 OK(0);
1906 case RH_C_PORT_SUSPEND:
1907 /*** WR_RH_PORTSTAT(RH_PS_PSSC); */
1908 OK(0);
1909 case RH_C_PORT_OVER_CURRENT:
1910 OK(0); /* port power over current */
1911 case RH_C_PORT_RESET:
1912 uhci->rh.c_p_r[wIndex - 1] = 0;
1913 OK(0);
1915 break;
1916 case RH_SET_FEATURE | RH_OTHER | RH_CLASS:
1917 switch (wValue) {
1918 case RH_PORT_SUSPEND:
1919 SET_RH_PORTSTAT(USBPORTSC_SUSP);
1920 OK(0);
1921 case RH_PORT_RESET:
1922 SET_RH_PORTSTAT(USBPORTSC_PR);
1923 wait_ms(50); /* USB v1.1 7.1.7.3 */
1924 uhci->rh.c_p_r[wIndex - 1] = 1;
1925 CLR_RH_PORTSTAT(USBPORTSC_PR);
1926 udelay(10);
1927 SET_RH_PORTSTAT(USBPORTSC_PE);
1928 wait_ms(10);
1929 SET_RH_PORTSTAT(0xa);
1930 OK(0);
1931 case RH_PORT_POWER:
1932 OK(0); /* port power ** */
1933 case RH_PORT_ENABLE:
1934 SET_RH_PORTSTAT(USBPORTSC_PE);
1935 OK(0);
1937 break;
1938 case RH_SET_ADDRESS:
1939 uhci->rh.devnum = wValue;
1940 OK(0);
1941 case RH_GET_DESCRIPTOR:
1942 switch ((wValue & 0xff00) >> 8) {
1943 case 0x01: /* device descriptor */
1944 len = min(leni, min(sizeof(root_hub_dev_des), wLength));
1945 memcpy(data, root_hub_dev_des, len);
1946 OK(len);
1947 case 0x02: /* configuration descriptor */
1948 len = min(leni, min(sizeof(root_hub_config_des), wLength));
1949 memcpy (data, root_hub_config_des, len);
1950 OK(len);
1951 case 0x03: /* string descriptors */
1952 len = usb_root_hub_string (wValue & 0xff,
1953 uhci->io_addr, "UHCI-alt",
1954 data, wLength);
1955 if (len > 0) {
1956 OK (min (leni, len));
1957 } else
1958 stat = -EPIPE;
1960 break;
1961 case RH_GET_DESCRIPTOR | RH_CLASS:
1962 root_hub_hub_des[2] = uhci->rh.numports;
1963 len = min(leni, min(sizeof(root_hub_hub_des), wLength));
1964 memcpy(data, root_hub_hub_des, len);
1965 OK(len);
1966 case RH_GET_CONFIGURATION:
1967 *(__u8 *)data = 0x01;
1968 OK(1);
1969 case RH_SET_CONFIGURATION:
1970 OK(0);
1971 case RH_GET_INTERFACE | RH_INTERFACE:
1972 *(__u8 *)data = 0x00;
1973 OK(1);
1974 case RH_SET_INTERFACE | RH_INTERFACE:
1975 OK(0);
1976 default:
1977 stat = -EPIPE;
1980 urb->actual_length = len;
1981 urb->status = stat;
1982 if (urb->complete)
1983 urb->complete(urb);
1985 return USB_ST_NOERROR;
1987 /*-------------------------------------------------------------------------*/
1989 static int rh_unlink_urb(struct urb *urb)
1991 struct uhci *uhci = (struct uhci *)urb->dev->bus->hcpriv;
1993 if (uhci->rh.urb == urb) {
1994 uhci->rh.send = 0;
1995 del_timer(&uhci->rh.rh_int_timer);
1997 return 0;
1999 /*-------------------------------------------------------------------*/
2001 void uhci_free_pending_qhs(struct uhci *uhci)
2003 struct list_head *tmp, *head;
2004 unsigned long flags;
2006 /* Free any pending QH's */
2007 spin_lock_irqsave(&uhci->qh_remove_lock, flags);
2008 head = &uhci->qh_remove_list;
2009 tmp = head->next;
2010 while (tmp != head) {
2011 struct uhci_qh *qh = list_entry(tmp, struct uhci_qh, remove_list);
2013 tmp = tmp->next;
2015 list_del(&qh->remove_list);
2017 uhci_free_qh(qh);
2019 spin_unlock_irqrestore(&uhci->qh_remove_lock, flags);
2022 static void uhci_interrupt(int irq, void *__uhci, struct pt_regs *regs)
2024 struct uhci *uhci = __uhci;
2025 unsigned int io_addr = uhci->io_addr;
2026 unsigned short status;
2027 unsigned long flags;
2028 struct list_head *tmp, *head;
2031 * Read the interrupt status, and write it back to clear the
2032 * interrupt cause
2034 status = inw(io_addr + USBSTS);
2035 if (!status) /* shared interrupt, not mine */
2036 return;
2037 outw(status, io_addr + USBSTS);
2039 if (status & ~(USBSTS_USBINT | USBSTS_ERROR)) {
2040 if (status & USBSTS_RD)
2041 printk(KERN_INFO "uhci: resume detected, not implemented\n");
2042 if (status & USBSTS_HSE)
2043 printk(KERN_ERR "uhci: host system error, PCI problems?\n");
2044 if (status & USBSTS_HCPE)
2045 printk(KERN_ERR "uhci: host controller process error. something bad happened\n");
2046 if (status & USBSTS_HCH) {
2047 printk(KERN_ERR "uhci: host controller halted. very bad\n");
2048 /* FIXME: Reset the controller, fix the offending TD */
2052 uhci_free_pending_qhs(uhci);
2054 spin_lock(&uhci->urb_remove_lock);
2055 head = &uhci->urb_remove_list;
2056 tmp = head->next;
2057 while (tmp != head) {
2058 struct urb *urb = list_entry(tmp, struct urb, urb_list);
2060 tmp = tmp->next;
2062 list_del(&urb->urb_list);
2064 if (urb->complete)
2065 urb->complete(urb);
2067 spin_unlock(&uhci->urb_remove_lock);
2069 uhci_clear_next_interrupt(uhci);
2071 /* Walk the list of pending TD's to see which ones completed */
2072 nested_lock(&uhci->urblist_lock, flags);
2073 head = &uhci->urb_list;
2074 tmp = head->next;
2075 while (tmp != head) {
2076 struct urb *urb = list_entry(tmp, struct urb, urb_list);
2078 tmp = tmp->next;
2080 /* Checks the status and does all of the magic necessary */
2081 uhci_transfer_result(urb);
2083 nested_unlock(&uhci->urblist_lock, flags);
2086 static void reset_hc(struct uhci *uhci)
2088 unsigned int io_addr = uhci->io_addr;
2090 /* Global reset for 50ms */
2091 outw(USBCMD_GRESET, io_addr + USBCMD);
2092 wait_ms(50);
2093 outw(0, io_addr + USBCMD);
2094 wait_ms(10);
2097 static void start_hc(struct uhci *uhci)
2099 unsigned int io_addr = uhci->io_addr;
2100 int timeout = 1000;
2103 * Reset the HC - this will force us to get a
2104 * new notification of any already connected
2105 * ports due to the virtual disconnect that it
2106 * implies.
2108 outw(USBCMD_HCRESET, io_addr + USBCMD);
2109 while (inw(io_addr + USBCMD) & USBCMD_HCRESET) {
2110 if (!--timeout) {
2111 printk(KERN_ERR "uhci: USBCMD_HCRESET timed out!\n");
2112 break;
2116 /* Turn on all interrupts */
2117 outw(USBINTR_TIMEOUT | USBINTR_RESUME | USBINTR_IOC | USBINTR_SP,
2118 io_addr + USBINTR);
2120 /* Start at frame 0 */
2121 outw(0, io_addr + USBFRNUM);
2122 outl(virt_to_bus(uhci->fl), io_addr + USBFLBASEADD);
2124 /* Run and mark it configured with a 64-byte max packet */
2125 outw(USBCMD_RS | USBCMD_CF | USBCMD_MAXP, io_addr + USBCMD);
2129 * Allocate a frame list, and then setup the skeleton
2131 * The hardware doesn't really know any difference
2132 * in the queues, but the order does matter for the
2133 * protocols higher up. The order is:
2135 * - any isochronous events handled before any
2136 * of the queues. We don't do that here, because
2137 * we'll create the actual TD entries on demand.
2138 * - The first queue is the "interrupt queue".
2139 * - The second queue is the "control queue", split into low and high speed
2140 * - The third queue is "bulk data".
2142 static struct uhci *alloc_uhci(unsigned int io_addr, unsigned int io_size)
2144 int i, port;
2145 struct uhci *uhci;
2146 struct usb_bus *bus;
2148 uhci = kmalloc(sizeof(*uhci), GFP_KERNEL);
2149 if (!uhci)
2150 return NULL;
2152 memset(uhci, 0, sizeof(*uhci));
2154 uhci->irq = -1;
2155 uhci->io_addr = io_addr;
2156 uhci->io_size = io_size;
2158 spin_lock_init(&uhci->qh_remove_lock);
2159 INIT_LIST_HEAD(&uhci->qh_remove_list);
2161 spin_lock_init(&uhci->urb_remove_lock);
2162 INIT_LIST_HEAD(&uhci->urb_remove_list);
2164 nested_init(&uhci->urblist_lock);
2165 INIT_LIST_HEAD(&uhci->urb_list);
2167 spin_lock_init(&uhci->framelist_lock);
2169 /* We need exactly one page (per UHCI specs), how convenient */
2170 /* We assume that one page is atleast 4k (1024 frames * 4 bytes) */
2171 uhci->fl = (void *)__get_free_page(GFP_KERNEL);
2172 if (!uhci->fl)
2173 goto au_free_uhci;
2175 bus = usb_alloc_bus(&uhci_device_operations);
2176 if (!bus)
2177 goto au_free_fl;
2179 uhci->bus = bus;
2180 bus->hcpriv = uhci;
2182 /* Initialize the root hub */
2184 /* UHCI specs says devices must have 2 ports, but goes on to say */
2185 /* they may have more but give no way to determine how many they */
2186 /* have. However, according to the UHCI spec, Bit 7 is always set */
2187 /* to 1. So we try to use this to our advantage */
2188 for (port = 0; port < (io_size - 0x10) / 2; port++) {
2189 unsigned int portstatus;
2191 portstatus = inw(io_addr + 0x10 + (port * 2));
2192 if (!(portstatus & 0x0080))
2193 break;
2195 if (debug)
2196 info("detected %d ports", port);
2198 /* This is experimental so anything less than 2 or greater than 8 is */
2199 /* something weird and we'll ignore it */
2200 if (port < 2 || port > 8) {
2201 info("port count misdetected? forcing to 2 ports");
2202 port = 2;
2205 uhci->rh.numports = port;
2208 * 9 Interrupt queues; link int2 to int1, int4 to int2, etc
2209 * then link int1 to control and control to bulk
2211 for (i = 1; i < 9; i++) {
2212 struct uhci_td *td = &uhci->skeltd[i];
2214 uhci_fill_td(td, 0, (UHCI_NULL_DATA_SIZE << 21) | (0x7f << 8) | USB_PID_IN, 0);
2215 td->link = virt_to_bus(&uhci->skeltd[i - 1]);
2219 uhci_fill_td(&uhci->skel_int1_td, 0, (UHCI_NULL_DATA_SIZE << 21) | (0x7f << 8) | USB_PID_IN, 0);
2220 uhci->skel_int1_td.link = virt_to_bus(&uhci->skel_ls_control_qh) | UHCI_PTR_QH;
2222 uhci->skel_ls_control_qh.link = virt_to_bus(&uhci->skel_hs_control_qh) | UHCI_PTR_QH;
2223 uhci->skel_ls_control_qh.element = UHCI_PTR_TERM;
2225 uhci->skel_hs_control_qh.link = virt_to_bus(&uhci->skel_bulk_qh) | UHCI_PTR_QH;
2226 uhci->skel_hs_control_qh.element = UHCI_PTR_TERM;
2228 uhci->skel_bulk_qh.link = virt_to_bus(&uhci->skel_term_qh) | UHCI_PTR_QH;
2229 uhci->skel_bulk_qh.element = UHCI_PTR_TERM;
2231 /* This dummy TD is to work around a bug in Intel PIIX controllers */
2232 uhci_fill_td(&uhci->skel_term_td, 0, (UHCI_NULL_DATA_SIZE << 21) | (0x7f << 8) | USB_PID_IN, 0);
2233 uhci->skel_term_td.link = UHCI_PTR_TERM;
2235 uhci->skel_term_qh.link = UHCI_PTR_TERM;
2236 uhci->skel_term_qh.element = virt_to_bus(&uhci->skel_term_td);
2239 * Fill the frame list: make all entries point to
2240 * the proper interrupt queue.
2242 * This is probably silly, but it's a simple way to
2243 * scatter the interrupt queues in a way that gives
2244 * us a reasonable dynamic range for irq latencies.
2246 for (i = 0; i < 1024; i++) {
2247 struct uhci_td *irq = &uhci->skel_int1_td;
2249 if (i & 1) {
2250 irq++;
2251 if (i & 2) {
2252 irq++;
2253 if (i & 4) {
2254 irq++;
2255 if (i & 8) {
2256 irq++;
2257 if (i & 16) {
2258 irq++;
2259 if (i & 32) {
2260 irq++;
2261 if (i & 64)
2262 irq++;
2270 /* Only place we don't use the frame list routines */
2271 uhci->fl->frame[i] = virt_to_bus(irq);
2274 return uhci;
2277 * error exits:
2279 au_free_fl:
2280 free_page((unsigned long)uhci->fl);
2281 au_free_uhci:
2282 kfree(uhci);
2284 return NULL;
2288 * De-allocate all resources..
2290 static void release_uhci(struct uhci *uhci)
2292 if (uhci->irq >= 0) {
2293 free_irq(uhci->irq, uhci);
2294 uhci->irq = -1;
2297 if (uhci->fl) {
2298 free_page((unsigned long)uhci->fl);
2299 uhci->fl = NULL;
2302 usb_free_bus(uhci->bus);
2303 kfree(uhci);
2306 int uhci_start_root_hub(struct uhci *uhci)
2308 struct usb_device *dev;
2310 dev = usb_alloc_dev(NULL, uhci->bus);
2311 if (!dev)
2312 return -1;
2314 uhci->bus->root_hub = dev;
2315 usb_connect(dev);
2317 if (usb_new_device(dev) != 0) {
2318 usb_free_dev(dev);
2320 return -1;
2323 return 0;
2327 * If we've successfully found a UHCI, now is the time to increment the
2328 * module usage count, and return success..
2330 static int setup_uhci(struct pci_dev *dev, int irq, unsigned int io_addr, unsigned int io_size)
2332 int retval;
2333 struct uhci *uhci;
2334 char buf[8], *bufp = buf;
2336 #ifndef __sparc__
2337 sprintf(buf, "%d", irq);
2338 #else
2339 bufp = __irq_itoa(irq);
2340 #endif
2341 printk(KERN_INFO __FILE__ ": USB UHCI at I/O 0x%x, IRQ %s\n",
2342 io_addr, bufp);
2344 uhci = alloc_uhci(io_addr, io_size);
2345 if (!uhci)
2346 return -ENOMEM;
2347 dev->driver_data = uhci;
2349 request_region(uhci->io_addr, io_size, "usb-uhci");
2351 reset_hc(uhci);
2353 usb_register_bus(uhci->bus);
2354 start_hc(uhci);
2356 retval = -EBUSY;
2357 if (request_irq(irq, uhci_interrupt, SA_SHIRQ, "usb-uhci", uhci) == 0) {
2358 uhci->irq = irq;
2360 pci_write_config_word(dev, USBLEGSUP, USBLEGSUP_DEFAULT);
2362 if (!uhci_start_root_hub(uhci))
2363 return 0;
2366 /* Couldn't allocate IRQ if we got here */
2368 reset_hc(uhci);
2369 release_region(uhci->io_addr, uhci->io_size);
2370 release_uhci(uhci);
2372 return retval;
2375 static int __devinit uhci_pci_probe(struct pci_dev *dev, const struct pci_device_id *id)
2377 int i;
2379 /* disable legacy emulation */
2380 pci_write_config_word(dev, USBLEGSUP, 0);
2382 if (pci_enable_device(dev) < 0)
2383 return -ENODEV;
2385 if (!dev->irq) {
2386 err("found UHCI device with no IRQ assigned. check BIOS settings!");
2387 return -ENODEV;
2390 /* Search for the IO base address.. */
2391 for (i = 0; i < 6; i++) {
2392 unsigned int io_addr = pci_resource_start(dev, i);
2393 unsigned int io_size = pci_resource_len(dev, i);
2395 /* IO address? */
2396 if (!(pci_resource_flags(dev, i) & IORESOURCE_IO))
2397 continue;
2399 /* Is it already in use? */
2400 if (check_region(io_addr, io_size))
2401 break;
2403 return setup_uhci(dev, dev->irq, io_addr, io_size);
2406 return -ENODEV;
2409 static void __devexit uhci_pci_remove(struct pci_dev *dev)
2411 struct uhci *uhci = dev->driver_data;
2413 if (uhci->bus->root_hub)
2414 usb_disconnect(&uhci->bus->root_hub);
2416 usb_deregister_bus(uhci->bus);
2418 reset_hc(uhci);
2419 release_region(uhci->io_addr, uhci->io_size);
2421 uhci_free_pending_qhs(uhci);
2423 release_uhci(uhci);
2426 static void uhci_pci_suspend(struct pci_dev *dev)
2428 reset_hc((struct uhci *) dev->driver_data);
2431 static void uhci_pci_resume(struct pci_dev *dev)
2433 reset_hc((struct uhci *) dev->driver_data);
2434 start_hc((struct uhci *) dev->driver_data);
2437 /*-------------------------------------------------------------------------*/
2439 static const struct pci_device_id __devinitdata uhci_pci_ids [] = { {
2441 /* handle any USB UHCI controller */
2442 class: ((PCI_CLASS_SERIAL_USB << 8) | 0x00),
2443 class_mask: ~0,
2445 /* no matter who makes it */
2446 vendor: PCI_ANY_ID,
2447 device: PCI_ANY_ID,
2448 subvendor: PCI_ANY_ID,
2449 subdevice: PCI_ANY_ID,
2451 }, { /* end: all zeroes */ }
2454 MODULE_DEVICE_TABLE (pci, uhci_pci_ids);
2456 static struct pci_driver uhci_pci_driver = {
2457 name: "usb-uhci",
2458 id_table: &uhci_pci_ids [0],
2460 probe: uhci_pci_probe,
2461 remove: uhci_pci_remove,
2463 #ifdef CONFIG_PM
2464 suspend: uhci_pci_suspend,
2465 resume: uhci_pci_resume,
2466 #endif /* PM */
2470 static int __init uhci_hcd_init(void)
2472 int retval;
2474 retval = -ENOMEM;
2476 /* We throw all of the TD's and QH's into a kmem cache */
2477 /* TD's and QH's need to be 16 byte aligned and SLAB_HWCACHE_ALIGN */
2478 /* does this for us */
2479 uhci_td_cachep = kmem_cache_create("uhci_td",
2480 sizeof(struct uhci_td), 0,
2481 SLAB_HWCACHE_ALIGN, NULL, NULL);
2483 if (!uhci_td_cachep)
2484 goto td_failed;
2486 uhci_qh_cachep = kmem_cache_create("uhci_qh",
2487 sizeof(struct uhci_qh), 0,
2488 SLAB_HWCACHE_ALIGN, NULL, NULL);
2490 if (!uhci_qh_cachep)
2491 goto qh_failed;
2493 uhci_up_cachep = kmem_cache_create("uhci_urb_priv",
2494 sizeof(struct urb_priv), 0, 0, NULL, NULL);
2496 if (!uhci_up_cachep)
2497 goto up_failed;
2499 retval = pci_module_init (&uhci_pci_driver);
2500 if (retval)
2501 goto init_failed;
2503 return 0;
2505 init_failed:
2506 if (kmem_cache_destroy(uhci_up_cachep))
2507 printk(KERN_INFO "uhci: not all urb_priv's were freed\n");
2509 up_failed:
2510 if (kmem_cache_destroy(uhci_qh_cachep))
2511 printk(KERN_INFO "uhci: not all QH's were freed\n");
2513 qh_failed:
2514 if (kmem_cache_destroy(uhci_td_cachep))
2515 printk(KERN_INFO "uhci: not all TD's were freed\n");
2517 td_failed:
2518 return retval;
2521 static void __exit uhci_hcd_cleanup (void)
2523 pci_unregister_driver (&uhci_pci_driver);
2525 if (kmem_cache_destroy(uhci_up_cachep))
2526 printk(KERN_INFO "uhci: not all urb_priv's were freed\n");
2528 if (kmem_cache_destroy(uhci_qh_cachep))
2529 printk(KERN_INFO "uhci: not all QH's were freed\n");
2531 if (kmem_cache_destroy(uhci_td_cachep))
2532 printk(KERN_INFO "uhci: not all TD's were freed\n");
2535 module_init(uhci_hcd_init);
2536 module_exit(uhci_hcd_cleanup);
2538 MODULE_AUTHOR("Linus Torvalds, Johannes Erdfelt, Randy Dunlap, Georg Acher, Deti Fliegl, Thomas Sailer, Roman Weissgaerber");
2539 MODULE_DESCRIPTION("USB Universal Host Controller Interface driver");