Linux 2.4.0-test7pre2
[davej-history.git] / drivers / usb / uhci.c
blob8423b448ab3386e3d4f239d847b93607002cfd3c
1 /*
2 * Universal Host Controller Interface driver for USB.
4 * (C) Copyright 1999 Linus Torvalds
5 * (C) Copyright 1999-2000 Johannes Erdfelt, jerdfelt@sventech.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
12 * Intel documents this fairly well, and as far as I know there
13 * are no royalties or anything like that, but even so there are
14 * people who decided that they want to do the same thing in a
15 * completely different way.
17 * WARNING! The USB documentation is downright evil. Most of it
18 * is just crap, written by a committee. You're better off ignoring
19 * most of it, the important stuff is:
20 * - the low-level protocol (fairly simple but lots of small details)
21 * - working around the horridness of the rest
24 #include <linux/module.h>
25 #include <linux/pci.h>
26 #include <linux/kernel.h>
27 #include <linux/delay.h>
28 #include <linux/ioport.h>
29 #include <linux/sched.h>
30 #include <linux/malloc.h>
31 #include <linux/smp_lock.h>
32 #include <linux/errno.h>
33 #include <linux/unistd.h>
34 #include <linux/interrupt.h>
35 #include <linux/spinlock.h>
36 #define DEBUG
37 #include <linux/usb.h>
39 #include <asm/uaccess.h>
40 #include <asm/io.h>
41 #include <asm/irq.h>
42 #include <asm/system.h>
44 #include "uhci.h"
45 #include "uhci-debug.h"
47 #include <linux/pm.h>
48 static int handle_pm_event(struct pm_dev *dev, pm_request_t rqst, void *data);
50 static int debug = 1;
51 MODULE_PARM(debug, "i");
52 MODULE_PARM_DESC(debug, "Debug level");
54 static kmem_cache_t *uhci_td_cachep;
55 static kmem_cache_t *uhci_qh_cachep;
56 static kmem_cache_t *uhci_up_cachep; /* urb_priv */
58 static LIST_HEAD(uhci_list);
60 static int rh_submit_urb(struct urb *urb);
61 static int rh_unlink_urb(struct urb *urb);
62 static int uhci_get_current_frame_number(struct usb_device *dev);
63 static int uhci_unlink_generic(struct urb *urb);
64 static int uhci_unlink_urb(struct urb *urb);
66 #define min(a,b) (((a)<(b))?(a):(b))
68 /* If a transfer is still active after this much time, turn off FSBR */
69 #define IDLE_TIMEOUT (HZ / 20) /* 50 ms */
72 * Only the USB core should call uhci_alloc_dev and uhci_free_dev
74 static int uhci_alloc_dev(struct usb_device *dev)
76 return 0;
79 static int uhci_free_dev(struct usb_device *dev)
81 struct uhci *uhci = (struct uhci *)dev->bus->hcpriv;
82 struct list_head *tmp, *head = &uhci->urb_list;
83 unsigned long flags;
85 /* Walk through the entire URB list and forcefully remove any */
86 /* URBs that are still active for that device */
87 nested_lock(&uhci->urblist_lock, flags);
88 tmp = head->next;
89 while (tmp != head) {
90 struct urb *u = list_entry(tmp, struct urb, urb_list);
92 tmp = tmp->next;
94 if (u->dev == dev)
95 uhci_unlink_urb(u);
97 nested_unlock(&uhci->urblist_lock, flags);
99 return 0;
102 static void uhci_add_urb_list(struct uhci *uhci, struct urb *urb)
104 unsigned long flags;
106 nested_lock(&uhci->urblist_lock, flags);
107 list_add(&urb->urb_list, &uhci->urb_list);
108 nested_unlock(&uhci->urblist_lock, flags);
111 static void uhci_remove_urb_list(struct uhci *uhci, struct urb *urb)
113 unsigned long flags;
115 nested_lock(&uhci->urblist_lock, flags);
116 if (!list_empty(&urb->urb_list)) {
117 list_del(&urb->urb_list);
118 INIT_LIST_HEAD(&urb->urb_list);
120 nested_unlock(&uhci->urblist_lock, flags);
123 void uhci_set_next_interrupt(struct uhci *uhci)
125 unsigned long flags;
127 spin_lock_irqsave(&uhci->framelist_lock, flags);
128 uhci->skel_term_td.status |= TD_CTRL_IOC;
129 spin_unlock_irqrestore(&uhci->framelist_lock, flags);
132 void uhci_clear_next_interrupt(struct uhci *uhci)
134 unsigned long flags;
136 spin_lock_irqsave(&uhci->framelist_lock, flags);
137 uhci->skel_term_td.status &= ~TD_CTRL_IOC;
138 spin_unlock_irqrestore(&uhci->framelist_lock, flags);
141 static struct uhci_td *uhci_alloc_td(struct usb_device *dev)
143 struct uhci_td *td;
145 td = kmem_cache_alloc(uhci_td_cachep, in_interrupt() ? SLAB_ATOMIC : SLAB_KERNEL);
146 if (!td)
147 return NULL;
149 td->link = UHCI_PTR_TERM;
150 td->buffer = 0;
152 td->frameptr = NULL;
153 td->nexttd = td->prevtd = NULL;
154 td->dev = dev;
155 INIT_LIST_HEAD(&td->list);
157 usb_inc_dev_use(dev);
159 return td;
162 static void inline uhci_fill_td(struct uhci_td *td, __u32 status,
163 __u32 info, __u32 buffer)
165 td->status = status;
166 td->info = info;
167 td->buffer = buffer;
170 static void uhci_insert_td(struct uhci *uhci, struct uhci_td *skeltd, struct uhci_td *td)
172 unsigned long flags;
174 spin_lock_irqsave(&uhci->framelist_lock, flags);
176 /* Fix the linked list pointers */
177 td->nexttd = skeltd->nexttd;
178 td->prevtd = skeltd;
179 if (skeltd->nexttd)
180 skeltd->nexttd->prevtd = td;
181 skeltd->nexttd = td;
183 td->link = skeltd->link;
184 skeltd->link = virt_to_bus(td);
186 spin_unlock_irqrestore(&uhci->framelist_lock, flags);
190 * We insert Isochronous transfers directly into the frame list at the
191 * beginning
192 * The layout looks as follows:
193 * frame list pointer -> iso td's (if any) ->
194 * periodic interrupt td (if frame 0) -> irq td's -> control qh -> bulk qh
197 static void uhci_insert_td_frame_list(struct uhci *uhci, struct uhci_td *td, unsigned framenum)
199 unsigned long flags;
200 struct uhci_td *nexttd;
202 framenum %= UHCI_NUMFRAMES;
204 spin_lock_irqsave(&uhci->framelist_lock, flags);
206 td->frameptr = &uhci->fl->frame[framenum];
207 td->link = uhci->fl->frame[framenum];
208 if (!(td->link & (UHCI_PTR_TERM | UHCI_PTR_QH))) {
209 nexttd = (struct uhci_td *)uhci_ptr_to_virt(td->link);
210 td->nexttd = nexttd;
211 nexttd->prevtd = td;
212 nexttd->frameptr = NULL;
214 uhci->fl->frame[framenum] = virt_to_bus(td);
216 spin_unlock_irqrestore(&uhci->framelist_lock, flags);
219 static void uhci_remove_td(struct uhci *uhci, struct uhci_td *td)
221 unsigned long flags;
223 /* If it's not inserted, don't remove it */
224 if (!td->frameptr && !td->prevtd && !td->nexttd)
225 return;
227 spin_lock_irqsave(&uhci->framelist_lock, flags);
228 if (td->frameptr) {
229 *(td->frameptr) = td->link;
230 if (td->nexttd) {
231 td->nexttd->frameptr = td->frameptr;
232 td->nexttd->prevtd = NULL;
233 td->nexttd = NULL;
235 td->frameptr = NULL;
236 } else {
237 if (td->prevtd) {
238 td->prevtd->nexttd = td->nexttd;
239 td->prevtd->link = td->link;
241 if (td->nexttd)
242 td->nexttd->prevtd = td->prevtd;
243 td->prevtd = td->nexttd = NULL;
245 td->link = UHCI_PTR_TERM;
246 spin_unlock_irqrestore(&uhci->framelist_lock, flags);
250 * Inserts a td into qh list at the top.
252 static void uhci_insert_tds_in_qh(struct uhci_qh *qh, struct urb *urb, int breadth)
254 struct list_head *tmp, *head;
255 struct urb_priv *urbp = (struct urb_priv *)urb->hcpriv;
256 struct uhci_td *td, *prevtd;
258 if (!urbp)
259 return;
261 head = &urbp->list;
262 tmp = head->next;
263 if (head == tmp)
264 return;
266 td = list_entry(tmp, struct uhci_td, list);
268 /* Add the first TD to the QH element pointer */
269 qh->element = virt_to_bus(td) | (breadth ? 0 : UHCI_PTR_DEPTH);
271 prevtd = td;
273 /* Then link the rest of the TD's */
274 tmp = tmp->next;
275 while (tmp != head) {
276 td = list_entry(tmp, struct uhci_td, list);
278 tmp = tmp->next;
280 prevtd->link = virt_to_bus(td) | (breadth ? 0 : UHCI_PTR_DEPTH);
282 prevtd = td;
285 prevtd->link = UHCI_PTR_TERM;
288 static void uhci_free_td(struct uhci_td *td)
290 if (!list_empty(&td->list))
291 dbg("td is still in URB list!");
293 if (td->dev)
294 usb_dec_dev_use(td->dev);
296 kmem_cache_free(uhci_td_cachep, td);
299 static struct uhci_qh *uhci_alloc_qh(struct usb_device *dev)
301 struct uhci_qh *qh;
303 qh = kmem_cache_alloc(uhci_qh_cachep, in_interrupt() ? SLAB_ATOMIC : SLAB_KERNEL);
304 if (!qh)
305 return NULL;
307 qh->element = UHCI_PTR_TERM;
308 qh->link = UHCI_PTR_TERM;
310 qh->dev = dev;
311 qh->prevqh = qh->nextqh = NULL;
313 INIT_LIST_HEAD(&qh->remove_list);
315 usb_inc_dev_use(dev);
317 return qh;
320 static void uhci_free_qh(struct uhci_qh *qh)
322 if (qh->dev)
323 usb_dec_dev_use(qh->dev);
325 kmem_cache_free(uhci_qh_cachep, qh);
328 static void uhci_insert_qh(struct uhci *uhci, struct uhci_qh *skelqh, struct uhci_qh *qh)
330 unsigned long flags;
332 spin_lock_irqsave(&uhci->framelist_lock, flags);
334 /* Fix the linked list pointers */
335 qh->nextqh = skelqh->nextqh;
336 qh->prevqh = skelqh;
337 if (skelqh->nextqh)
338 skelqh->nextqh->prevqh = qh;
339 skelqh->nextqh = qh;
341 qh->link = skelqh->link;
342 skelqh->link = virt_to_bus(qh) | UHCI_PTR_QH;
344 spin_unlock_irqrestore(&uhci->framelist_lock, flags);
347 static void uhci_remove_qh(struct uhci *uhci, struct uhci_qh *qh)
349 unsigned long flags;
350 int delayed;
352 /* If the QH isn't queued, then we don't need to delay unlink it */
353 delayed = (qh->prevqh || qh->nextqh);
355 spin_lock_irqsave(&uhci->framelist_lock, flags);
356 if (qh->prevqh) {
357 qh->prevqh->nextqh = qh->nextqh;
358 qh->prevqh->link = qh->link;
360 if (qh->nextqh)
361 qh->nextqh->prevqh = qh->prevqh;
362 qh->prevqh = qh->nextqh = NULL;
363 qh->element = qh->link = UHCI_PTR_TERM;
364 spin_unlock_irqrestore(&uhci->framelist_lock, flags);
366 if (delayed) {
367 spin_lock_irqsave(&uhci->qh_remove_lock, flags);
369 /* Check to see if the remove list is empty */
370 /* Set the IOC bit to force an interrupt so we can remove the QH */
371 if (list_empty(&uhci->qh_remove_list))
372 uhci_set_next_interrupt(uhci);
374 /* Add it */
375 list_add(&qh->remove_list, &uhci->qh_remove_list);
377 spin_unlock_irqrestore(&uhci->qh_remove_lock, flags);
378 } else
379 uhci_free_qh(qh);
382 static spinlock_t uhci_append_urb_lock = SPIN_LOCK_UNLOCKED;
384 /* This function will append one URB's QH to another URB's QH. This is for */
385 /* USB_QUEUE_BULK support */
386 static void uhci_append_queued_urb(struct uhci *uhci, struct urb *eurb, struct urb *urb)
388 struct urb_priv *eurbp, *urbp, *furbp, *lurbp;
389 struct list_head *tmp;
390 struct uhci_td *td, *ltd;
391 unsigned long flags;
393 eurbp = eurb->hcpriv;
394 urbp = urb->hcpriv;
396 spin_lock_irqsave(&uhci_append_urb_lock, flags);
398 /* Find the beginning URB in the queue */
399 if (eurbp->queued) {
400 struct list_head *head = &eurbp->urb_queue_list;
402 tmp = head->next;
403 while (tmp != head) {
404 struct urb_priv *turbp =
405 list_entry(tmp, struct urb_priv, urb_queue_list);
407 tmp = tmp->next;
409 if (!turbp->queued)
410 break;
412 } else
413 tmp = &eurbp->urb_queue_list;
415 furbp = list_entry(tmp, struct urb_priv, urb_queue_list);
417 tmp = furbp->urb_queue_list.prev;
418 lurbp = list_entry(tmp, struct urb_priv, urb_queue_list);
420 /* Add this one to the end */
421 list_add_tail(&urbp->urb_queue_list, &furbp->urb_queue_list);
423 /* Grab the last TD from the last URB */
424 ltd = list_entry(lurbp->list.prev, struct uhci_td, list);
426 /* Grab the first TD from the first URB */
427 td = list_entry(urbp->list.next, struct uhci_td, list);
429 /* No breadth since this will only be called for bulk transfers */
430 ltd->link = virt_to_bus(td);
432 spin_unlock_irqrestore(&uhci_append_urb_lock, flags);
435 static void uhci_delete_queued_urb(struct uhci *uhci, struct urb *urb)
437 struct urb_priv *urbp, *nurbp;
438 unsigned long flags;
440 urbp = urb->hcpriv;
442 spin_lock_irqsave(&uhci_append_urb_lock, flags);
444 nurbp = list_entry(urbp->urb_queue_list.next, struct urb_priv,
445 urb_queue_list);
447 if (!urbp->queued) {
448 /* We're the head, so just insert the QH for the next URB */
449 uhci_insert_qh(uhci, &uhci->skel_bulk_qh, nurbp->qh);
450 nurbp->queued = 0;
451 } else {
452 struct urb_priv *purbp;
453 struct uhci_td *ptd;
455 /* We're somewhere in the middle (or end). A bit trickier */
456 /* than the head scenario */
457 purbp = list_entry(urbp->urb_queue_list.prev, struct urb_priv,
458 urb_queue_list);
460 ptd = list_entry(purbp->list.prev, struct uhci_td, list);
461 if (nurbp->queued)
462 /* Close the gap between the two */
463 ptd->link = virt_to_bus(list_entry(nurbp->list.next,
464 struct uhci_td, list));
465 else
466 /* The next URB happens to be the beggining, so */
467 /* we're the last, end the chain */
468 ptd->link = UHCI_PTR_TERM;
472 list_del(&urbp->urb_queue_list);
474 spin_unlock_irqrestore(&uhci_append_urb_lock, flags);
477 struct urb_priv *uhci_alloc_urb_priv(struct urb *urb)
479 struct urb_priv *urbp;
481 urbp = kmem_cache_alloc(uhci_up_cachep, in_interrupt() ? SLAB_ATOMIC : SLAB_KERNEL);
482 if (!urbp)
483 return NULL;
485 memset((void *)urbp, 0, sizeof(*urbp));
487 urbp->inserttime = jiffies;
488 urbp->urb = urb;
490 INIT_LIST_HEAD(&urbp->list);
491 INIT_LIST_HEAD(&urbp->urb_queue_list);
493 urb->hcpriv = urbp;
495 usb_inc_dev_use(urb->dev);
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 usb_dec_dev_use(urb->dev);
560 unlock:
561 spin_unlock_irqrestore(&urb->lock, flags);
564 static void uhci_inc_fsbr(struct uhci *uhci, struct urb *urb)
566 unsigned long flags;
567 struct urb_priv *urbp = (struct urb_priv *)urb->hcpriv;
569 if (!urbp)
570 return;
572 spin_lock_irqsave(&uhci->framelist_lock, flags);
574 if (!urbp->fsbr) {
575 urbp->fsbr = 1;
576 if (!uhci->fsbr++)
577 uhci->skel_term_qh.link = virt_to_bus(&uhci->skel_hs_control_qh) | UHCI_PTR_QH;
580 spin_unlock_irqrestore(&uhci->framelist_lock, flags);
583 static void uhci_dec_fsbr(struct uhci *uhci, struct urb *urb)
585 unsigned long flags;
586 struct urb_priv *urbp = (struct urb_priv *)urb->hcpriv;
588 if (!urbp)
589 return;
591 spin_lock_irqsave(&uhci->framelist_lock, flags);
593 if (urbp->fsbr) {
594 urbp->fsbr = 0;
595 if (!--uhci->fsbr)
596 uhci->skel_term_qh.link = UHCI_PTR_TERM;
599 spin_unlock_irqrestore(&uhci->framelist_lock, flags);
603 * Map status to standard result codes
605 * <status> is (td->status & 0xFE0000) [a.k.a. uhci_status_bits(td->status)]
606 * <dir_out> is True for output TDs and False for input TDs.
608 static int uhci_map_status(int status, int dir_out)
610 if (!status)
611 return 0;
612 if (status & TD_CTRL_BITSTUFF) /* Bitstuff error */
613 return -EPROTO;
614 if (status & TD_CTRL_CRCTIMEO) { /* CRC/Timeout */
615 if (dir_out)
616 return -ETIMEDOUT;
617 else
618 return -EILSEQ;
620 if (status & TD_CTRL_NAK) /* NAK */
621 return -ETIMEDOUT;
622 if (status & TD_CTRL_BABBLE) /* Babble */
623 return -EPIPE;
624 if (status & TD_CTRL_DBUFERR) /* Buffer error */
625 return -ENOSR;
626 if (status & TD_CTRL_STALLED) /* Stalled */
627 return -EPIPE;
628 if (status & TD_CTRL_ACTIVE) /* Active */
629 return 0;
631 return -EINVAL;
635 * Control transfers
637 static int uhci_submit_control(struct urb *urb)
639 struct urb_priv *urbp = (struct urb_priv *)urb->hcpriv;
640 struct uhci *uhci = (struct uhci *)urb->dev->bus->hcpriv;
641 struct uhci_td *td;
642 struct uhci_qh *qh;
643 unsigned long destination, status;
644 int maxsze = usb_maxpacket(urb->dev, urb->pipe, usb_pipeout(urb->pipe));
645 int len = urb->transfer_buffer_length;
646 unsigned char *data = urb->transfer_buffer;
648 /* The "pipe" thing contains the destination in bits 8--18 */
649 destination = (urb->pipe & PIPE_DEVEP_MASK) | USB_PID_SETUP;
651 /* 3 errors */
652 status = (urb->pipe & TD_CTRL_LS) | TD_CTRL_ACTIVE | (3 << 27);
655 * Build the TD for the control request
657 td = uhci_alloc_td(urb->dev);
658 if (!td)
659 return -ENOMEM;
661 uhci_add_td_to_urb(urb, td);
662 uhci_fill_td(td, status, destination | (7 << 21),
663 virt_to_bus(urb->setup_packet));
666 * If direction is "send", change the frame from SETUP (0x2D)
667 * to OUT (0xE1). Else change it from SETUP to IN (0x69).
669 destination ^= (USB_PID_SETUP ^ usb_packetid(urb->pipe));
671 if (!(urb->transfer_flags & USB_DISABLE_SPD))
672 status |= TD_CTRL_SPD;
675 * Build the DATA TD's
677 while (len > 0) {
678 int pktsze = len;
680 if (pktsze > maxsze)
681 pktsze = maxsze;
683 td = uhci_alloc_td(urb->dev);
684 if (!td)
685 return -ENOMEM;
687 /* Alternate Data0/1 (start with Data1) */
688 destination ^= 1 << TD_TOKEN_TOGGLE;
690 uhci_add_td_to_urb(urb, td);
691 uhci_fill_td(td, status, destination | ((pktsze - 1) << 21),
692 virt_to_bus(data));
694 data += pktsze;
695 len -= pktsze;
699 * Build the final TD for control status
701 td = uhci_alloc_td(urb->dev);
702 if (!td)
703 return -ENOMEM;
706 * It's IN if the pipe is an output pipe or we're not expecting
707 * data back.
709 destination &= ~TD_PID;
710 if (usb_pipeout(urb->pipe) || !urb->transfer_buffer_length)
711 destination |= USB_PID_IN;
712 else
713 destination |= USB_PID_OUT;
715 destination |= 1 << TD_TOKEN_TOGGLE; /* End in Data1 */
717 status &= ~TD_CTRL_SPD;
719 uhci_add_td_to_urb(urb, td);
720 uhci_fill_td(td, status | TD_CTRL_IOC,
721 destination | (UHCI_NULL_DATA_SIZE << 21), 0);
723 qh = uhci_alloc_qh(urb->dev);
724 if (!qh)
725 return -ENOMEM;
727 /* Low speed or small transfers gets a different queue and treatment */
728 if (urb->pipe & TD_CTRL_LS) {
729 uhci_insert_tds_in_qh(qh, urb, 0);
730 uhci_insert_qh(uhci, &uhci->skel_ls_control_qh, qh);
731 } else {
732 uhci_insert_tds_in_qh(qh, urb, 1);
733 uhci_insert_qh(uhci, &uhci->skel_hs_control_qh, qh);
734 uhci_inc_fsbr(uhci, urb);
737 urbp->qh = qh;
739 uhci_add_urb_list(uhci, urb);
741 usb_inc_dev_use(urb->dev);
743 return -EINPROGRESS;
746 static int usb_control_retrigger_status(struct urb *urb);
748 static int uhci_result_control(struct urb *urb)
750 struct list_head *tmp, *head;
751 struct urb_priv *urbp = urb->hcpriv;
752 struct uhci_td *td;
753 unsigned int status;
754 int ret = 0;
756 if (!urbp)
757 return -EINVAL;
759 head = &urbp->list;
760 if (head->next == head)
761 return -EINVAL;
763 if (urbp->short_control_packet) {
764 tmp = head->prev;
765 goto status_phase;
768 tmp = head->next;
769 td = list_entry(tmp, struct uhci_td, list);
771 /* The first TD is the SETUP phase, check the status, but skip */
772 /* the count */
773 status = uhci_status_bits(td->status);
774 if (status & TD_CTRL_ACTIVE)
775 return -EINPROGRESS;
777 if (status)
778 goto td_error;
780 urb->actual_length = 0;
782 /* The rest of the TD's (but the last) are data */
783 tmp = tmp->next;
784 while (tmp != head && tmp->next != head) {
785 td = list_entry(tmp, struct uhci_td, list);
787 tmp = tmp->next;
789 if (urbp->fsbr_timeout && (td->status & TD_CTRL_IOC) &&
790 !(td->status & TD_CTRL_ACTIVE)) {
791 uhci_inc_fsbr(urb->dev->bus->hcpriv, urb);
792 urbp->fsbr_timeout = 0;
793 td->status &= ~TD_CTRL_IOC;
796 status = uhci_status_bits(td->status);
797 if (status & TD_CTRL_ACTIVE)
798 return -EINPROGRESS;
800 urb->actual_length += uhci_actual_length(td->status);
802 if (status)
803 goto td_error;
805 /* Check to see if we received a short packet */
806 if (uhci_actual_length(td->status) < uhci_expected_length(td->info)) {
807 if (urb->transfer_flags & USB_DISABLE_SPD) {
808 ret = -EREMOTEIO;
809 goto err;
812 if (uhci_packetid(td->info) == USB_PID_IN)
813 return usb_control_retrigger_status(urb);
814 else
815 return 0;
819 status_phase:
820 td = list_entry(tmp, struct uhci_td, list);
822 /* Control status phase */
823 status = uhci_status_bits(td->status);
825 #ifdef I_HAVE_BUGGY_APC_BACKUPS
826 /* APC BackUPS Pro kludge */
827 /* It tries to send all of the descriptor instead of the amount */
828 /* we requested */
829 if (td->status & TD_CTRL_IOC && /* IOC is masked out by uhci_status_bits */
830 status & TD_CTRL_ACTIVE &&
831 status & TD_CTRL_NAK)
832 return 0;
833 #endif
835 if (status & TD_CTRL_ACTIVE)
836 return -EINPROGRESS;
838 if (status)
839 goto td_error;
841 return 0;
843 td_error:
844 ret = uhci_map_status(status, uhci_packetout(td->info));
845 if (ret == -EPIPE)
846 /* endpoint has stalled - mark it halted */
847 usb_endpoint_halt(urb->dev, uhci_endpoint(td->info),
848 uhci_packetout(td->info));
850 err:
851 if (debug && ret != -EPIPE) {
852 /* Some debugging code */
853 dbg("uhci_result_control() failed with status %x", status);
855 /* Print the chain for debugging purposes */
856 uhci_show_urb_queue(urb);
859 return ret;
862 static int usb_control_retrigger_status(struct urb *urb)
864 struct list_head *tmp, *head;
865 struct urb_priv *urbp = (struct urb_priv *)urb->hcpriv;
866 struct uhci *uhci = urb->dev->bus->hcpriv;
868 urbp->short_control_packet = 1;
870 /* Create a new QH to avoid pointer overwriting problems */
871 uhci_remove_qh(uhci, urbp->qh);
873 /* Delete all of the TD's except for the status TD at the end */
874 head = &urbp->list;
875 tmp = head->next;
876 while (tmp != head && tmp->next != head) {
877 struct uhci_td *td = list_entry(tmp, struct uhci_td, list);
879 tmp = tmp->next;
881 uhci_remove_td_from_urb(urb, td);
883 uhci_remove_td(uhci, td);
885 uhci_free_td(td);
888 urbp->qh = uhci_alloc_qh(urb->dev);
889 if (!urbp->qh) {
890 err("unable to allocate new QH for control retrigger");
891 return -ENOMEM;
894 /* One TD, who cares about Breadth first? */
895 uhci_insert_tds_in_qh(urbp->qh, urb, 0);
897 /* Low speed or small transfers gets a different queue and treatment */
898 if (urb->pipe & TD_CTRL_LS)
899 uhci_insert_qh(uhci, &uhci->skel_ls_control_qh, urbp->qh);
900 else
901 uhci_insert_qh(uhci, &uhci->skel_hs_control_qh, urbp->qh);
903 return -EINPROGRESS;
907 * Interrupt transfers
909 static int uhci_submit_interrupt(struct urb *urb)
911 struct uhci_td *td;
912 unsigned long destination, status;
913 struct uhci *uhci = (struct uhci *)urb->dev->bus->hcpriv;
915 if (urb->transfer_buffer_length > usb_maxpacket(urb->dev, urb->pipe, usb_pipeout(urb->pipe)))
916 return -EINVAL;
918 /* The "pipe" thing contains the destination in bits 8--18 */
919 destination = (urb->pipe & PIPE_DEVEP_MASK) | usb_packetid(urb->pipe);
921 status = (urb->pipe & TD_CTRL_LS) | TD_CTRL_ACTIVE | TD_CTRL_IOC;
923 td = uhci_alloc_td(urb->dev);
924 if (!td)
925 return -ENOMEM;
927 destination |= (usb_gettoggle(urb->dev, usb_pipeendpoint(urb->pipe), usb_pipeout(urb->pipe)) << TD_TOKEN_TOGGLE);
928 destination |= ((urb->transfer_buffer_length - 1) << 21);
930 usb_dotoggle(urb->dev, usb_pipeendpoint(urb->pipe), usb_pipeout(urb->pipe));
932 uhci_add_td_to_urb(urb, td);
933 uhci_fill_td(td, status, destination,
934 virt_to_bus(urb->transfer_buffer));
936 uhci_insert_td(uhci, &uhci->skeltd[__interval_to_skel(urb->interval)], td);
938 uhci_add_urb_list(uhci, urb);
940 return -EINPROGRESS;
943 static int uhci_result_interrupt(struct urb *urb)
945 struct list_head *tmp, *head;
946 struct urb_priv *urbp = urb->hcpriv;
947 struct uhci_td *td;
948 unsigned int status;
949 int ret = 0;
951 if (!urbp)
952 return -EINVAL;
954 urb->actual_length = 0;
956 head = &urbp->list;
957 tmp = head->next;
958 while (tmp != head) {
959 td = list_entry(tmp, struct uhci_td, list);
961 tmp = tmp->next;
963 if (urbp->fsbr_timeout && (td->status & TD_CTRL_IOC) &&
964 !(td->status & TD_CTRL_ACTIVE)) {
965 uhci_inc_fsbr(urb->dev->bus->hcpriv, urb);
966 urbp->fsbr_timeout = 0;
967 td->status &= ~TD_CTRL_IOC;
970 status = uhci_status_bits(td->status);
971 if (status & TD_CTRL_ACTIVE)
972 return -EINPROGRESS;
974 urb->actual_length += uhci_actual_length(td->status);
976 if (status)
977 goto td_error;
979 if (uhci_actual_length(td->status) < uhci_expected_length(td->info)) {
980 usb_settoggle(urb->dev, uhci_endpoint(td->info),
981 uhci_packetout(td->info),
982 uhci_toggle(td->info) ^ 1);
984 if (urb->transfer_flags & USB_DISABLE_SPD) {
985 ret = -EREMOTEIO;
986 goto err;
987 } else
988 return 0;
992 return 0;
994 td_error:
995 ret = uhci_map_status(status, uhci_packetout(td->info));
996 if (ret == -EPIPE)
997 /* endpoint has stalled - mark it halted */
998 usb_endpoint_halt(urb->dev, uhci_endpoint(td->info),
999 uhci_packetout(td->info));
1001 err:
1002 if (debug && ret != -EPIPE) {
1003 /* Some debugging code */
1004 dbg("uhci_result_interrupt/bulk() failed with status %x",
1005 status);
1007 /* Print the chain for debugging purposes */
1008 if (urbp->qh)
1009 uhci_show_urb_queue(urb);
1010 else
1011 uhci_show_td(td);
1014 return ret;
1017 static void uhci_reset_interrupt(struct urb *urb)
1019 struct list_head *tmp;
1020 struct urb_priv *urbp = (struct urb_priv *)urb->hcpriv;
1021 struct uhci_td *td;
1023 if (!urbp)
1024 return;
1026 tmp = urbp->list.next;
1027 td = list_entry(tmp, struct uhci_td, list);
1028 if (!td)
1029 return;
1031 td->status = (td->status & 0x2F000000) | TD_CTRL_ACTIVE | TD_CTRL_IOC;
1032 td->info &= ~(1 << TD_TOKEN_TOGGLE);
1033 td->info |= (usb_gettoggle(urb->dev, usb_pipeendpoint(urb->pipe), usb_pipeout(urb->pipe)) << TD_TOKEN_TOGGLE);
1034 usb_dotoggle(urb->dev, usb_pipeendpoint(urb->pipe), usb_pipeout(urb->pipe));
1036 urb->status = -EINPROGRESS;
1040 * Bulk transfers
1042 static int uhci_submit_bulk(struct urb *urb, struct urb *eurb)
1044 struct uhci_td *td;
1045 struct uhci_qh *qh;
1046 unsigned long destination, status;
1047 struct uhci *uhci = (struct uhci *)urb->dev->bus->hcpriv;
1048 int maxsze = usb_maxpacket(urb->dev, urb->pipe, usb_pipeout(urb->pipe));
1049 int len = urb->transfer_buffer_length;
1050 unsigned char *data = urb->transfer_buffer;
1051 struct urb_priv *urbp = (struct urb_priv *)urb->hcpriv;
1053 if (len < 0)
1054 return -EINVAL;
1056 /* Can't have low speed bulk transfers */
1057 if (urb->pipe & TD_CTRL_LS)
1058 return -EINVAL;
1060 /* The "pipe" thing contains the destination in bits 8--18 */
1061 destination = (urb->pipe & PIPE_DEVEP_MASK) | usb_packetid(urb->pipe);
1063 /* 3 errors */
1064 status = TD_CTRL_ACTIVE | (3 << TD_CTRL_C_ERR_SHIFT);
1066 if (!(urb->transfer_flags & USB_DISABLE_SPD))
1067 status |= TD_CTRL_SPD;
1070 * Build the DATA TD's
1072 do { /* Allow zero length packets */
1073 int pktsze = len;
1075 if (pktsze > maxsze)
1076 pktsze = maxsze;
1078 td = uhci_alloc_td(urb->dev);
1079 if (!td)
1080 return -ENOMEM;
1082 uhci_add_td_to_urb(urb, td);
1083 uhci_fill_td(td, status, destination | ((pktsze - 1) << 21) |
1084 (usb_gettoggle(urb->dev, usb_pipeendpoint(urb->pipe),
1085 usb_pipeout(urb->pipe)) << TD_TOKEN_TOGGLE),
1086 virt_to_bus(data));
1088 data += pktsze;
1089 len -= maxsze;
1091 if (len <= 0)
1092 td->status |= TD_CTRL_IOC;
1094 usb_dotoggle(urb->dev, usb_pipeendpoint(urb->pipe),
1095 usb_pipeout(urb->pipe));
1096 } while (len > 0);
1098 qh = uhci_alloc_qh(urb->dev);
1099 if (!qh)
1100 return -ENOMEM;
1102 urbp->qh = qh;
1104 /* Always assume depth first */
1105 uhci_insert_tds_in_qh(qh, urb, 1);
1107 if (urb->transfer_flags & USB_QUEUE_BULK && eurb) {
1108 urbp->queued = 1;
1109 uhci_append_queued_urb(uhci, eurb, urb);
1110 } else
1111 uhci_insert_qh(uhci, &uhci->skel_bulk_qh, qh);
1113 uhci_add_urb_list(uhci, urb);
1115 uhci_inc_fsbr(uhci, urb);
1117 return -EINPROGRESS;
1120 /* We can use the result interrupt since they're identical */
1121 #define uhci_result_bulk uhci_result_interrupt
1124 * Isochronous transfers
1126 static int isochronous_find_limits(struct urb *urb, unsigned int *start, unsigned int *end)
1128 struct urb *last_urb = NULL;
1129 struct uhci *uhci = (struct uhci *)urb->dev->bus->hcpriv;
1130 struct list_head *tmp, *head = &uhci->urb_list;
1131 int ret = 0;
1132 unsigned long flags;
1134 nested_lock(&uhci->urblist_lock, flags);
1135 tmp = head->next;
1136 while (tmp != head) {
1137 struct urb *u = list_entry(tmp, struct urb, urb_list);
1139 tmp = tmp->next;
1141 /* look for pending URB's with identical pipe handle */
1142 if ((urb->pipe == u->pipe) && (urb->dev == u->dev) &&
1143 (u->status == -EINPROGRESS) && (u != urb)) {
1144 if (!last_urb)
1145 *start = u->start_frame;
1146 last_urb = u;
1150 if (last_urb) {
1151 *end = (last_urb->start_frame + last_urb->number_of_packets) & 1023;
1152 ret = 0;
1153 } else
1154 ret = -1; /* no previous urb found */
1156 nested_unlock(&uhci->urblist_lock, flags);
1158 return ret;
1161 static int isochronous_find_start(struct urb *urb)
1163 int limits;
1164 unsigned int start = 0, end = 0;
1166 if (urb->number_of_packets > 900) /* 900? Why? */
1167 return -EFBIG;
1169 limits = isochronous_find_limits(urb, &start, &end);
1171 if (urb->transfer_flags & USB_ISO_ASAP) {
1172 if (limits) {
1173 int curframe;
1175 curframe = uhci_get_current_frame_number(urb->dev) % UHCI_NUMFRAMES;
1176 urb->start_frame = (curframe + 10) % UHCI_NUMFRAMES;
1177 } else
1178 urb->start_frame = end;
1179 } else {
1180 urb->start_frame %= UHCI_NUMFRAMES;
1181 /* FIXME: Sanity check */
1184 return 0;
1187 static int uhci_submit_isochronous(struct urb *urb)
1189 struct uhci_td *td;
1190 struct uhci *uhci = (struct uhci *)urb->dev->bus->hcpriv;
1191 int i, ret, framenum;
1192 int status, destination;
1194 status = TD_CTRL_ACTIVE | TD_CTRL_IOS;
1195 destination = (urb->pipe & PIPE_DEVEP_MASK) | usb_packetid(urb->pipe);
1197 ret = isochronous_find_start(urb);
1198 if (ret)
1199 return ret;
1201 framenum = urb->start_frame;
1202 for (i = 0; i < urb->number_of_packets; i++, framenum++) {
1203 if (!urb->iso_frame_desc[i].length)
1204 continue;
1206 td = uhci_alloc_td(urb->dev);
1207 if (!td)
1208 return -ENOMEM;
1210 uhci_add_td_to_urb(urb, td);
1211 uhci_fill_td(td, status, destination | ((urb->iso_frame_desc[i].length - 1) << 21),
1212 virt_to_bus(urb->transfer_buffer + urb->iso_frame_desc[i].offset));
1214 if (i + 1 >= urb->number_of_packets)
1215 td->status |= TD_CTRL_IOC;
1217 uhci_insert_td_frame_list(uhci, td, framenum);
1220 uhci_add_urb_list(uhci, urb);
1222 return -EINPROGRESS;
1225 static int uhci_result_isochronous(struct urb *urb)
1227 struct list_head *tmp, *head;
1228 struct urb_priv *urbp = (struct urb_priv *)urb->hcpriv;
1229 int status;
1230 int i, ret = 0;
1232 if (!urbp)
1233 return -EINVAL;
1235 urb->actual_length = 0;
1237 i = 0;
1238 head = &urbp->list;
1239 tmp = head->next;
1240 while (tmp != head) {
1241 struct uhci_td *td = list_entry(tmp, struct uhci_td, list);
1242 int actlength;
1244 tmp = tmp->next;
1246 if (td->status & TD_CTRL_ACTIVE)
1247 return -EINPROGRESS;
1249 actlength = uhci_actual_length(td->status);
1250 urb->iso_frame_desc[i].actual_length = actlength;
1251 urb->actual_length += actlength;
1253 status = uhci_map_status(uhci_status_bits(td->status), usb_pipeout(urb->pipe));
1254 urb->iso_frame_desc[i].status = status;
1255 if (status != 0) {
1256 urb->error_count++;
1257 ret = status;
1260 i++;
1263 return ret;
1266 static struct urb *uhci_find_urb_ep(struct uhci *uhci, struct urb *urb)
1268 struct list_head *tmp, *head = &uhci->urb_list;
1269 unsigned long flags;
1270 struct urb *u = NULL;
1272 if (usb_pipeisoc(urb->pipe))
1273 return NULL;
1275 nested_lock(&uhci->urblist_lock, flags);
1276 tmp = head->next;
1277 while (tmp != head) {
1278 u = list_entry(tmp, struct urb, urb_list);
1280 tmp = tmp->next;
1282 if (u->dev == urb->dev &&
1283 u->pipe == urb->pipe)
1284 goto found;
1286 u = NULL;
1288 found:
1289 nested_unlock(&uhci->urblist_lock, flags);
1291 return u;
1294 static int uhci_submit_urb(struct urb *urb)
1296 int ret = -EINVAL;
1297 struct uhci *uhci;
1298 unsigned long flags;
1299 struct urb *u;
1300 int bustime;
1302 if (!urb)
1303 return -EINVAL;
1305 if (!urb->dev || !urb->dev->bus || !urb->dev->bus->hcpriv)
1306 return -ENODEV;
1308 uhci = (struct uhci *)urb->dev->bus->hcpriv;
1310 /* Short circuit the virtual root hub */
1311 if (usb_pipedevice(urb->pipe) == uhci->rh.devnum)
1312 return rh_submit_urb(urb);
1314 u = uhci_find_urb_ep(uhci, urb);
1315 if (u && !(urb->transfer_flags & USB_QUEUE_BULK))
1316 return -ENXIO;
1318 spin_lock_irqsave(&urb->lock, flags);
1320 if (!uhci_alloc_urb_priv(urb)) {
1321 spin_unlock_irqrestore(&urb->lock, flags);
1322 return -ENOMEM;
1325 switch (usb_pipetype(urb->pipe)) {
1326 case PIPE_CONTROL:
1327 ret = uhci_submit_control(urb);
1328 break;
1329 case PIPE_INTERRUPT:
1330 if (urb->bandwidth == 0) { /* not yet checked/allocated */
1331 bustime = usb_check_bandwidth (urb->dev, urb);
1332 if (bustime < 0)
1333 ret = bustime;
1334 else {
1335 ret = uhci_submit_interrupt(urb);
1336 if (ret == -EINPROGRESS)
1337 usb_claim_bandwidth (urb->dev, urb, bustime, 0);
1339 } else { /* bandwidth is already set */
1340 ret = uhci_submit_interrupt(urb);
1342 break;
1343 case PIPE_BULK:
1344 ret = uhci_submit_bulk(urb, u);
1345 break;
1346 case PIPE_ISOCHRONOUS:
1347 if (urb->bandwidth == 0) { /* not yet checked/allocated */
1348 if (urb->number_of_packets <= 0) {
1349 ret = -EINVAL;
1350 break;
1352 bustime = usb_check_bandwidth (urb->dev, urb);
1353 if (bustime < 0) {
1354 ret = bustime;
1355 break;
1358 ret = uhci_submit_isochronous(urb);
1359 if (ret == -EINPROGRESS)
1360 usb_claim_bandwidth (urb->dev, urb, bustime, 1);
1361 } else { /* bandwidth is already set */
1362 ret = uhci_submit_isochronous(urb);
1364 break;
1367 urb->status = ret;
1369 spin_unlock_irqrestore(&urb->lock, flags);
1371 if (ret == -EINPROGRESS)
1372 ret = 0;
1373 else
1374 uhci_unlink_generic(urb);
1376 return ret;
1380 * Return the result of a transfer
1382 * Must be called with urblist_lock acquired
1384 static void uhci_transfer_result(struct urb *urb)
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 urb->complete(urb);
1428 if (urb->interval)
1429 uhci_reset_interrupt(urb);
1430 else {
1431 /* Release bandwidth for Interrupt or Isoc. transfers */
1432 /* Spinlock needed ? */
1433 if (urb->bandwidth)
1434 usb_release_bandwidth (urb->dev, urb, 0);
1435 uhci_unlink_generic(urb);
1437 return; /* <-- Note the return */
1440 if (urb->next) {
1441 turb = urb->next;
1442 do {
1443 if (turb->status != -EINPROGRESS) {
1444 proceed = 1;
1445 break;
1448 turb = turb->next;
1449 } while (turb && turb != urb && turb != urb->next);
1451 if (turb == urb || turb == urb->next)
1452 is_ring = 1;
1455 if (urb->complete && (!proceed || (urb->transfer_flags & USB_URB_EARLY_COMPLETE))) {
1456 urb->complete(urb);
1457 if (!proceed && is_ring)
1458 uhci_submit_urb(urb);
1461 if (proceed && urb->next) {
1462 turb = urb->next;
1463 do {
1464 if (turb->status != -EINPROGRESS &&
1465 uhci_submit_urb(turb) != 0)
1467 turb = turb->next;
1468 } while (turb && turb != urb->next);
1470 if (urb->complete && !(urb->transfer_flags & USB_URB_EARLY_COMPLETE))
1471 urb->complete(urb);
1475 static int uhci_unlink_generic(struct urb *urb)
1477 struct urb_priv *urbp = urb->hcpriv;
1478 struct uhci *uhci = (struct uhci *)urb->dev->bus->hcpriv;
1480 if (!urbp)
1481 return -EINVAL;
1483 uhci_dec_fsbr(uhci, urb); /* Safe since it checks */
1485 uhci_remove_urb_list(uhci, urb);
1487 if (urbp->qh)
1488 /* The interrupt loop will reclaim the QH's */
1489 uhci_remove_qh(uhci, urbp->qh);
1491 if (!list_empty(&urbp->urb_queue_list))
1492 uhci_delete_queued_urb(uhci, urb);
1494 uhci_destroy_urb_priv(urb);
1496 return 0;
1499 static int uhci_unlink_urb(struct urb *urb)
1501 struct uhci *uhci;
1502 int ret = 0;
1503 unsigned long flags;
1505 if (!urb)
1506 return -EINVAL;
1508 if (!urb->dev || !urb->dev->bus)
1509 return -ENODEV;
1511 uhci = (struct uhci *)urb->dev->bus->hcpriv;
1513 /* Short circuit the virtual root hub */
1514 if (usb_pipedevice(urb->pipe) == uhci->rh.devnum)
1515 return rh_unlink_urb(urb);
1517 /* Release bandwidth for Interrupt or Isoc. transfers */
1518 /* Spinlock needed ? */
1519 if (urb->bandwidth) {
1520 switch (usb_pipetype(urb->pipe)) {
1521 case PIPE_INTERRUPT:
1522 usb_release_bandwidth (urb->dev, urb, 0);
1523 break;
1524 case PIPE_ISOCHRONOUS:
1525 usb_release_bandwidth (urb->dev, urb, 1);
1526 break;
1527 default:
1528 break;
1532 if (urb->status == -EINPROGRESS) {
1533 uhci_unlink_generic(urb);
1535 if (urb->transfer_flags & USB_ASYNC_UNLINK) {
1536 urb->status = -ECONNABORTED;
1538 spin_lock_irqsave(&uhci->urb_remove_lock, flags);
1540 /* Check to see if the remove list is empty */
1541 if (list_empty(&uhci->urb_remove_list))
1542 uhci_set_next_interrupt(uhci);
1544 list_add(&urb->urb_list, &uhci->urb_remove_list);
1546 spin_unlock_irqrestore(&uhci->urb_remove_lock, flags);
1547 } else {
1548 urb->status = -ENOENT;
1550 if (in_interrupt()) { /* wait at least 1 frame */
1551 static int errorcount = 10;
1553 if (errorcount--)
1554 dbg("uhci_unlink_urb called from interrupt for urb %p", urb);
1555 udelay(1000);
1556 } else
1557 schedule_timeout(1+1*HZ/1000);
1559 if (urb->complete)
1560 urb->complete(urb);
1564 return ret;
1567 static int uhci_fsbr_timeout(struct uhci *uhci, struct urb *urb)
1569 struct urb_priv *urbp = (struct urb_priv *)urb->hcpriv;
1570 struct list_head *head, *tmp;
1572 uhci_dec_fsbr(uhci, urb);
1574 /* There is a race with updating IOC in here, but it's not worth */
1575 /* trying to fix since this is merely an optimization. The only */
1576 /* time we'd lose is if the status of the packet got updated */
1577 /* and we'd be turning on FSBR next frame anyway, so it's a wash */
1578 urbp->fsbr_timeout = 1;
1580 head = &urbp->list;
1581 tmp = head->next;
1582 while (tmp != head) {
1583 struct uhci_td *td = list_entry(tmp, struct uhci_td, list);
1585 tmp = tmp->next;
1587 if (td->status & TD_CTRL_ACTIVE) {
1588 td->status |= TD_CTRL_IOC;
1589 break;
1593 return 0;
1597 * uhci_get_current_frame_number()
1599 * returns the current frame number for a USB bus/controller.
1601 static int uhci_get_current_frame_number(struct usb_device *dev)
1603 struct uhci *uhci = (struct uhci *)dev->bus->hcpriv;
1605 return inw(uhci->io_addr + USBFRNUM);
1608 struct usb_operations uhci_device_operations = {
1609 uhci_alloc_dev,
1610 uhci_free_dev,
1611 uhci_get_current_frame_number,
1612 uhci_submit_urb,
1613 uhci_unlink_urb
1616 /* -------------------------------------------------------------------
1617 Virtual Root Hub
1618 ------------------------------------------------------------------- */
1620 static __u8 root_hub_dev_des[] =
1622 0x12, /* __u8 bLength; */
1623 0x01, /* __u8 bDescriptorType; Device */
1624 0x00, /* __u16 bcdUSB; v1.0 */
1625 0x01,
1626 0x09, /* __u8 bDeviceClass; HUB_CLASSCODE */
1627 0x00, /* __u8 bDeviceSubClass; */
1628 0x00, /* __u8 bDeviceProtocol; */
1629 0x08, /* __u8 bMaxPacketSize0; 8 Bytes */
1630 0x00, /* __u16 idVendor; */
1631 0x00,
1632 0x00, /* __u16 idProduct; */
1633 0x00,
1634 0x00, /* __u16 bcdDevice; */
1635 0x00,
1636 0x00, /* __u8 iManufacturer; */
1637 0x02, /* __u8 iProduct; */
1638 0x01, /* __u8 iSerialNumber; */
1639 0x01 /* __u8 bNumConfigurations; */
1643 /* Configuration descriptor */
1644 static __u8 root_hub_config_des[] =
1646 0x09, /* __u8 bLength; */
1647 0x02, /* __u8 bDescriptorType; Configuration */
1648 0x19, /* __u16 wTotalLength; */
1649 0x00,
1650 0x01, /* __u8 bNumInterfaces; */
1651 0x01, /* __u8 bConfigurationValue; */
1652 0x00, /* __u8 iConfiguration; */
1653 0x40, /* __u8 bmAttributes;
1654 Bit 7: Bus-powered, 6: Self-powered,
1655 Bit 5 Remote-wakeup, 4..0: resvd */
1656 0x00, /* __u8 MaxPower; */
1658 /* interface */
1659 0x09, /* __u8 if_bLength; */
1660 0x04, /* __u8 if_bDescriptorType; Interface */
1661 0x00, /* __u8 if_bInterfaceNumber; */
1662 0x00, /* __u8 if_bAlternateSetting; */
1663 0x01, /* __u8 if_bNumEndpoints; */
1664 0x09, /* __u8 if_bInterfaceClass; HUB_CLASSCODE */
1665 0x00, /* __u8 if_bInterfaceSubClass; */
1666 0x00, /* __u8 if_bInterfaceProtocol; */
1667 0x00, /* __u8 if_iInterface; */
1669 /* endpoint */
1670 0x07, /* __u8 ep_bLength; */
1671 0x05, /* __u8 ep_bDescriptorType; Endpoint */
1672 0x81, /* __u8 ep_bEndpointAddress; IN Endpoint 1 */
1673 0x03, /* __u8 ep_bmAttributes; Interrupt */
1674 0x08, /* __u16 ep_wMaxPacketSize; 8 Bytes */
1675 0x00,
1676 0xff /* __u8 ep_bInterval; 255 ms */
1679 static __u8 root_hub_hub_des[] =
1681 0x09, /* __u8 bLength; */
1682 0x29, /* __u8 bDescriptorType; Hub-descriptor */
1683 0x02, /* __u8 bNbrPorts; */
1684 0x00, /* __u16 wHubCharacteristics; */
1685 0x00,
1686 0x01, /* __u8 bPwrOn2pwrGood; 2ms */
1687 0x00, /* __u8 bHubContrCurrent; 0 mA */
1688 0x00, /* __u8 DeviceRemovable; *** 7 Ports max *** */
1689 0xff /* __u8 PortPwrCtrlMask; *** 7 ports max *** */
1692 /*-------------------------------------------------------------------------*/
1693 /* prepare Interrupt pipe transaction data; HUB INTERRUPT ENDPOINT */
1694 static int rh_send_irq(struct urb *urb)
1696 int i, len = 1;
1697 struct uhci *uhci = (struct uhci *)urb->dev->bus->hcpriv;
1698 unsigned int io_addr = uhci->io_addr;
1699 __u16 data = 0;
1701 for (i = 0; i < uhci->rh.numports; i++) {
1702 data |= ((inw(io_addr + USBPORTSC1 + i * 2) & 0xa) > 0 ? (1 << (i + 1)) : 0);
1703 len = (i + 1) / 8 + 1;
1706 *(__u16 *) urb->transfer_buffer = cpu_to_le16(data);
1707 urb->actual_length = len;
1708 urb->status = USB_ST_NOERROR;
1710 if ((data > 0) && (uhci->rh.send != 0)) {
1711 dbg("root-hub INT complete: port1: %x port2: %x data: %x",
1712 inw(io_addr + USBPORTSC1), inw(io_addr + USBPORTSC2), data);
1713 urb->complete(urb);
1716 return USB_ST_NOERROR;
1719 /*-------------------------------------------------------------------------*/
1720 /* Virtual Root Hub INTs are polled by this timer every "interval" ms */
1721 static int rh_init_int_timer(struct urb *urb);
1723 static void rh_int_timer_do(unsigned long ptr)
1725 struct urb *urb = (struct urb *)ptr;
1726 struct uhci *uhci = (struct uhci *)urb->dev->bus->hcpriv;
1727 struct list_head *tmp, *head = &uhci->urb_list;
1728 struct urb_priv *urbp;
1729 int len;
1730 unsigned long flags;
1732 if (uhci->rh.send) {
1733 len = rh_send_irq(urb);
1734 if (len > 0) {
1735 urb->actual_length = len;
1736 if (urb->complete)
1737 urb->complete(urb);
1741 nested_lock(&uhci->urblist_lock, flags);
1742 tmp = head->next;
1743 while (tmp != head) {
1744 struct urb *u = list_entry(tmp, urb_t, urb_list);
1746 tmp = tmp->next;
1748 urbp = (struct urb_priv *)u->hcpriv;
1749 if (urbp) {
1750 /* Check if the FSBR timed out */
1751 if (urbp->fsbr && time_after(urbp->inserttime + IDLE_TIMEOUT, jiffies))
1752 uhci_fsbr_timeout(uhci, u);
1754 /* Check if the URB timed out */
1755 if (u->timeout && time_after(u->timeout, jiffies)) {
1756 u->transfer_flags |= USB_ASYNC_UNLINK | USB_TIMEOUT_KILLED;
1757 uhci_unlink_urb(u);
1761 nested_unlock(&uhci->urblist_lock, flags);
1763 rh_init_int_timer(urb);
1766 /*-------------------------------------------------------------------------*/
1767 /* Root Hub INTs are polled by this timer */
1768 static int rh_init_int_timer(struct urb *urb)
1770 struct uhci *uhci = (struct uhci *)urb->dev->bus->hcpriv;
1772 uhci->rh.interval = urb->interval;
1773 init_timer(&uhci->rh.rh_int_timer);
1774 uhci->rh.rh_int_timer.function = rh_int_timer_do;
1775 uhci->rh.rh_int_timer.data = (unsigned long)urb;
1776 uhci->rh.rh_int_timer.expires = jiffies + (HZ * (urb->interval < 30 ? 30 : urb->interval)) / 1000;
1777 add_timer(&uhci->rh.rh_int_timer);
1779 return 0;
1782 /*-------------------------------------------------------------------------*/
1783 #define OK(x) len = (x); break
1785 #define CLR_RH_PORTSTAT(x) \
1786 status = inw(io_addr + USBPORTSC1 + 2 * (wIndex-1)); \
1787 status = (status & 0xfff5) & ~(x); \
1788 outw(status, io_addr + USBPORTSC1 + 2 * (wIndex-1))
1790 #define SET_RH_PORTSTAT(x) \
1791 status = inw(io_addr + USBPORTSC1 + 2 * (wIndex-1)); \
1792 status = (status & 0xfff5) | (x); \
1793 outw(status, io_addr + USBPORTSC1 + 2 * (wIndex-1))
1796 /*-------------------------------------------------------------------------*/
1797 /*************************
1798 ** Root Hub Control Pipe
1799 *************************/
1801 static int rh_submit_urb(struct urb *urb)
1803 struct uhci *uhci = (struct uhci *)urb->dev->bus->hcpriv;
1804 unsigned int pipe = urb->pipe;
1805 devrequest *cmd = (devrequest *)urb->setup_packet;
1806 void *data = urb->transfer_buffer;
1807 int leni = urb->transfer_buffer_length;
1808 int len = 0;
1809 int status = 0;
1810 int stat = USB_ST_NOERROR;
1811 int i;
1812 unsigned int io_addr = uhci->io_addr;
1813 __u16 cstatus;
1814 __u16 bmRType_bReq;
1815 __u16 wValue;
1816 __u16 wIndex;
1817 __u16 wLength;
1819 if (usb_pipetype(pipe) == PIPE_INTERRUPT) {
1820 uhci->rh.urb = urb;
1821 uhci->rh.send = 1;
1822 uhci->rh.interval = urb->interval;
1823 rh_init_int_timer(urb);
1825 return USB_ST_NOERROR;
1828 bmRType_bReq = cmd->requesttype | cmd->request << 8;
1829 wValue = le16_to_cpu(cmd->value);
1830 wIndex = le16_to_cpu(cmd->index);
1831 wLength = le16_to_cpu(cmd->length);
1833 for (i = 0; i < 8; i++)
1834 uhci->rh.c_p_r[i] = 0;
1836 switch (bmRType_bReq) {
1837 /* Request Destination:
1838 without flags: Device,
1839 RH_INTERFACE: interface,
1840 RH_ENDPOINT: endpoint,
1841 RH_CLASS means HUB here,
1842 RH_OTHER | RH_CLASS almost ever means HUB_PORT here
1845 case RH_GET_STATUS:
1846 *(__u16 *)data = cpu_to_le16(1);
1847 OK(2);
1848 case RH_GET_STATUS | RH_INTERFACE:
1849 *(__u16 *)data = cpu_to_le16(0);
1850 OK(2);
1851 case RH_GET_STATUS | RH_ENDPOINT:
1852 *(__u16 *)data = cpu_to_le16(0);
1853 OK(2);
1854 case RH_GET_STATUS | RH_CLASS:
1855 *(__u32 *)data = cpu_to_le32(0);
1856 OK(4); /* hub power */
1857 case RH_GET_STATUS | RH_OTHER | RH_CLASS:
1858 status = inw(io_addr + USBPORTSC1 + 2 * (wIndex - 1));
1859 cstatus = ((status & USBPORTSC_CSC) >> (1 - 0)) |
1860 ((status & USBPORTSC_PEC) >> (3 - 1)) |
1861 (uhci->rh.c_p_r[wIndex - 1] << (0 + 4));
1862 status = (status & USBPORTSC_CCS) |
1863 ((status & USBPORTSC_PE) >> (2 - 1)) |
1864 ((status & USBPORTSC_SUSP) >> (12 - 2)) |
1865 ((status & USBPORTSC_PR) >> (9 - 4)) |
1866 (1 << 8) | /* power on */
1867 ((status & USBPORTSC_LSDA) << (-8 + 9));
1869 *(__u16 *)data = cpu_to_le16(status);
1870 *(__u16 *)(data + 2) = cpu_to_le16(cstatus);
1871 OK(4);
1872 case RH_CLEAR_FEATURE | RH_ENDPOINT:
1873 switch (wValue) {
1874 case RH_ENDPOINT_STALL:
1875 OK(0);
1877 break;
1878 case RH_CLEAR_FEATURE | RH_CLASS:
1879 switch (wValue) {
1880 case RH_C_HUB_OVER_CURRENT:
1881 OK(0); /* hub power over current */
1883 break;
1884 case RH_CLEAR_FEATURE | RH_OTHER | RH_CLASS:
1885 switch (wValue) {
1886 case RH_PORT_ENABLE:
1887 CLR_RH_PORTSTAT(USBPORTSC_PE);
1888 OK(0);
1889 case RH_PORT_SUSPEND:
1890 CLR_RH_PORTSTAT(USBPORTSC_SUSP);
1891 OK(0);
1892 case RH_PORT_POWER:
1893 OK(0); /* port power */
1894 case RH_C_PORT_CONNECTION:
1895 SET_RH_PORTSTAT(USBPORTSC_CSC);
1896 OK(0);
1897 case RH_C_PORT_ENABLE:
1898 SET_RH_PORTSTAT(USBPORTSC_PEC);
1899 OK(0);
1900 case RH_C_PORT_SUSPEND:
1901 /*** WR_RH_PORTSTAT(RH_PS_PSSC); */
1902 OK(0);
1903 case RH_C_PORT_OVER_CURRENT:
1904 OK(0); /* port power over current */
1905 case RH_C_PORT_RESET:
1906 uhci->rh.c_p_r[wIndex - 1] = 0;
1907 OK(0);
1909 break;
1910 case RH_SET_FEATURE | RH_OTHER | RH_CLASS:
1911 switch (wValue) {
1912 case RH_PORT_SUSPEND:
1913 SET_RH_PORTSTAT(USBPORTSC_SUSP);
1914 OK(0);
1915 case RH_PORT_RESET:
1916 SET_RH_PORTSTAT(USBPORTSC_PR);
1917 wait_ms(50); /* USB v1.1 7.1.7.3 */
1918 uhci->rh.c_p_r[wIndex - 1] = 1;
1919 CLR_RH_PORTSTAT(USBPORTSC_PR);
1920 udelay(10);
1921 SET_RH_PORTSTAT(USBPORTSC_PE);
1922 wait_ms(10);
1923 SET_RH_PORTSTAT(0xa);
1924 OK(0);
1925 case RH_PORT_POWER:
1926 OK(0); /* port power ** */
1927 case RH_PORT_ENABLE:
1928 SET_RH_PORTSTAT(USBPORTSC_PE);
1929 OK(0);
1931 break;
1932 case RH_SET_ADDRESS:
1933 uhci->rh.devnum = wValue;
1934 OK(0);
1935 case RH_GET_DESCRIPTOR:
1936 switch ((wValue & 0xff00) >> 8) {
1937 case 0x01: /* device descriptor */
1938 len = min(leni, min(sizeof(root_hub_dev_des), wLength));
1939 memcpy(data, root_hub_dev_des, len);
1940 OK(len);
1941 case 0x02: /* configuration descriptor */
1942 len = min(leni, min(sizeof(root_hub_config_des), wLength));
1943 memcpy (data, root_hub_config_des, len);
1944 OK(len);
1945 case 0x03: /* string descriptors */
1946 len = usb_root_hub_string (wValue & 0xff,
1947 uhci->io_addr, "UHCI-alt",
1948 data, wLength);
1949 if (len > 0) {
1950 OK (min (leni, len));
1951 } else
1952 stat = -EPIPE;
1954 break;
1955 case RH_GET_DESCRIPTOR | RH_CLASS:
1956 root_hub_hub_des[2] = uhci->rh.numports;
1957 len = min(leni, min(sizeof(root_hub_hub_des), wLength));
1958 memcpy(data, root_hub_hub_des, len);
1959 OK(len);
1960 case RH_GET_CONFIGURATION:
1961 *(__u8 *)data = 0x01;
1962 OK(1);
1963 case RH_SET_CONFIGURATION:
1964 OK(0);
1965 case RH_GET_INTERFACE | RH_INTERFACE:
1966 *(__u8 *)data = 0x00;
1967 OK(1);
1968 case RH_SET_INTERFACE | RH_INTERFACE:
1969 OK(0);
1970 default:
1971 stat = -EPIPE;
1974 urb->actual_length = len;
1975 urb->status = stat;
1976 if (urb->complete)
1977 urb->complete(urb);
1979 return USB_ST_NOERROR;
1981 /*-------------------------------------------------------------------------*/
1983 static int rh_unlink_urb(struct urb *urb)
1985 struct uhci *uhci = (struct uhci *)urb->dev->bus->hcpriv;
1987 if (uhci->rh.urb == urb) {
1988 uhci->rh.send = 0;
1989 del_timer(&uhci->rh.rh_int_timer);
1991 return 0;
1993 /*-------------------------------------------------------------------*/
1995 void uhci_free_pending_qhs(struct uhci *uhci)
1997 struct list_head *tmp, *head;
1998 unsigned long flags;
2000 /* Free any pending QH's */
2001 spin_lock_irqsave(&uhci->qh_remove_lock, flags);
2002 head = &uhci->qh_remove_list;
2003 tmp = head->next;
2004 while (tmp != head) {
2005 struct uhci_qh *qh = list_entry(tmp, struct uhci_qh, remove_list);
2007 tmp = tmp->next;
2009 list_del(&qh->remove_list);
2011 uhci_free_qh(qh);
2013 spin_unlock_irqrestore(&uhci->qh_remove_lock, flags);
2016 static void uhci_interrupt(int irq, void *__uhci, struct pt_regs *regs)
2018 struct uhci *uhci = __uhci;
2019 unsigned int io_addr = uhci->io_addr;
2020 unsigned short status;
2021 unsigned long flags;
2022 struct list_head *tmp, *head;
2025 * Read the interrupt status, and write it back to clear the
2026 * interrupt cause
2028 status = inw(io_addr + USBSTS);
2029 if (!status) /* shared interrupt, not mine */
2030 return;
2031 outw(status, io_addr + USBSTS);
2033 if (status & ~(USBSTS_USBINT | USBSTS_ERROR)) {
2034 if (status & USBSTS_RD)
2035 printk(KERN_INFO "uhci: resume detected, not implemented\n");
2036 if (status & USBSTS_HSE)
2037 printk(KERN_ERR "uhci: host system error, PCI problems?\n");
2038 if (status & USBSTS_HCPE)
2039 printk(KERN_ERR "uhci: host controller process error. something bad happened\n");
2040 if (status & USBSTS_HCH) {
2041 printk(KERN_ERR "uhci: host controller halted. very bad\n");
2042 /* FIXME: Reset the controller, fix the offending TD */
2046 uhci_free_pending_qhs(uhci);
2048 spin_lock(&uhci->urb_remove_lock);
2049 head = &uhci->urb_remove_list;
2050 tmp = head->next;
2051 while (tmp != head) {
2052 struct urb *urb = list_entry(tmp, struct urb, urb_list);
2054 tmp = tmp->next;
2056 list_del(&urb->urb_list);
2058 if (urb->complete)
2059 urb->complete(urb);
2061 spin_unlock(&uhci->urb_remove_lock);
2063 uhci_clear_next_interrupt(uhci);
2065 /* Walk the list of pending TD's to see which ones completed */
2066 nested_lock(&uhci->urblist_lock, flags);
2067 head = &uhci->urb_list;
2068 tmp = head->next;
2069 while (tmp != head) {
2070 struct urb *urb = list_entry(tmp, struct urb, urb_list);
2072 tmp = tmp->next;
2074 /* Checks the status and does all of the magic necessary */
2075 uhci_transfer_result(urb);
2077 nested_unlock(&uhci->urblist_lock, flags);
2080 static void reset_hc(struct uhci *uhci)
2082 unsigned int io_addr = uhci->io_addr;
2084 /* Global reset for 50ms */
2085 outw(USBCMD_GRESET, io_addr + USBCMD);
2086 wait_ms(50);
2087 outw(0, io_addr + USBCMD);
2088 wait_ms(10);
2091 static void start_hc(struct uhci *uhci)
2093 unsigned int io_addr = uhci->io_addr;
2094 int timeout = 1000;
2097 * Reset the HC - this will force us to get a
2098 * new notification of any already connected
2099 * ports due to the virtual disconnect that it
2100 * implies.
2102 outw(USBCMD_HCRESET, io_addr + USBCMD);
2103 while (inw(io_addr + USBCMD) & USBCMD_HCRESET) {
2104 if (!--timeout) {
2105 printk(KERN_ERR "uhci: USBCMD_HCRESET timed out!\n");
2106 break;
2110 /* Turn on all interrupts */
2111 outw(USBINTR_TIMEOUT | USBINTR_RESUME | USBINTR_IOC | USBINTR_SP,
2112 io_addr + USBINTR);
2114 /* Start at frame 0 */
2115 outw(0, io_addr + USBFRNUM);
2116 outl(virt_to_bus(uhci->fl), io_addr + USBFLBASEADD);
2118 /* Run and mark it configured with a 64-byte max packet */
2119 outw(USBCMD_RS | USBCMD_CF | USBCMD_MAXP, io_addr + USBCMD);
2123 * Allocate a frame list, and then setup the skeleton
2125 * The hardware doesn't really know any difference
2126 * in the queues, but the order does matter for the
2127 * protocols higher up. The order is:
2129 * - any isochronous events handled before any
2130 * of the queues. We don't do that here, because
2131 * we'll create the actual TD entries on demand.
2132 * - The first queue is the "interrupt queue".
2133 * - The second queue is the "control queue", split into low and high speed
2134 * - The third queue is "bulk data".
2136 static struct uhci *alloc_uhci(unsigned int io_addr, unsigned int io_size)
2138 int i, port;
2139 struct uhci *uhci;
2140 struct usb_bus *bus;
2142 uhci = kmalloc(sizeof(*uhci), GFP_KERNEL);
2143 if (!uhci)
2144 return NULL;
2146 memset(uhci, 0, sizeof(*uhci));
2148 uhci->irq = -1;
2149 uhci->io_addr = io_addr;
2150 uhci->io_size = io_size;
2152 spin_lock_init(&uhci->qh_remove_lock);
2153 INIT_LIST_HEAD(&uhci->qh_remove_list);
2155 spin_lock_init(&uhci->urb_remove_lock);
2156 INIT_LIST_HEAD(&uhci->urb_remove_list);
2158 nested_init(&uhci->urblist_lock);
2159 INIT_LIST_HEAD(&uhci->urb_list);
2161 spin_lock_init(&uhci->framelist_lock);
2163 /* We need exactly one page (per UHCI specs), how convenient */
2164 /* We assume that one page is atleast 4k (1024 frames * 4 bytes) */
2165 uhci->fl = (void *)__get_free_page(GFP_KERNEL);
2166 if (!uhci->fl)
2167 goto au_free_uhci;
2169 bus = usb_alloc_bus(&uhci_device_operations);
2170 if (!bus)
2171 goto au_free_fl;
2173 uhci->bus = bus;
2174 bus->hcpriv = uhci;
2176 /* Initialize the root hub */
2178 /* UHCI specs says devices must have 2 ports, but goes on to say */
2179 /* they may have more but give no way to determine how many they */
2180 /* have. However, according to the UHCI spec, Bit 7 is always set */
2181 /* to 1. So we try to use this to our advantage */
2182 for (port = 0; port < (io_size - 0x10) / 2; port++) {
2183 unsigned int portstatus;
2185 portstatus = inw(io_addr + 0x10 + (port * 2));
2186 if (!(portstatus & 0x0080))
2187 break;
2189 if (debug)
2190 info("detected %d ports", port);
2192 /* This is experimental so anything less than 2 or greater than 8 is */
2193 /* something weird and we'll ignore it */
2194 if (port < 2 || port > 8) {
2195 info("port count misdetected? forcing to 2 ports");
2196 port = 2;
2199 uhci->rh.numports = port;
2202 * 9 Interrupt queues; link int2 to int1, int4 to int2, etc
2203 * then link int1 to control and control to bulk
2205 for (i = 1; i < 9; i++) {
2206 struct uhci_td *td = &uhci->skeltd[i];
2208 uhci_fill_td(td, 0, (UHCI_NULL_DATA_SIZE << 21) | (0x7f << 8) | USB_PID_IN, 0);
2209 td->link = virt_to_bus(&uhci->skeltd[i - 1]);
2213 uhci_fill_td(&uhci->skel_int1_td, 0, (UHCI_NULL_DATA_SIZE << 21) | (0x7f << 8) | USB_PID_IN, 0);
2214 uhci->skel_int1_td.link = virt_to_bus(&uhci->skel_ls_control_qh) | UHCI_PTR_QH;
2216 uhci->skel_ls_control_qh.link = virt_to_bus(&uhci->skel_hs_control_qh) | UHCI_PTR_QH;
2217 uhci->skel_ls_control_qh.element = UHCI_PTR_TERM;
2219 uhci->skel_hs_control_qh.link = virt_to_bus(&uhci->skel_bulk_qh) | UHCI_PTR_QH;
2220 uhci->skel_hs_control_qh.element = UHCI_PTR_TERM;
2222 uhci->skel_bulk_qh.link = virt_to_bus(&uhci->skel_term_qh) | UHCI_PTR_QH;
2223 uhci->skel_bulk_qh.element = UHCI_PTR_TERM;
2225 /* This dummy TD is to work around a bug in Intel PIIX controllers */
2226 uhci_fill_td(&uhci->skel_term_td, 0, (UHCI_NULL_DATA_SIZE << 21) | (0x7f << 8) | USB_PID_IN, 0);
2227 uhci->skel_term_td.link = UHCI_PTR_TERM;
2229 uhci->skel_term_qh.link = UHCI_PTR_TERM;
2230 uhci->skel_term_qh.element = virt_to_bus(&uhci->skel_term_td);
2233 * Fill the frame list: make all entries point to
2234 * the proper interrupt queue.
2236 * This is probably silly, but it's a simple way to
2237 * scatter the interrupt queues in a way that gives
2238 * us a reasonable dynamic range for irq latencies.
2240 for (i = 0; i < 1024; i++) {
2241 struct uhci_td *irq = &uhci->skel_int1_td;
2243 if (i & 1) {
2244 irq++;
2245 if (i & 2) {
2246 irq++;
2247 if (i & 4) {
2248 irq++;
2249 if (i & 8) {
2250 irq++;
2251 if (i & 16) {
2252 irq++;
2253 if (i & 32) {
2254 irq++;
2255 if (i & 64)
2256 irq++;
2264 /* Only place we don't use the frame list routines */
2265 uhci->fl->frame[i] = virt_to_bus(irq);
2268 return uhci;
2271 * error exits:
2273 au_free_fl:
2274 free_page((unsigned long)uhci->fl);
2275 au_free_uhci:
2276 kfree(uhci);
2278 return NULL;
2282 * De-allocate all resources..
2284 static void release_uhci(struct uhci *uhci)
2286 if (uhci->irq >= 0) {
2287 free_irq(uhci->irq, uhci);
2288 uhci->irq = -1;
2291 if (uhci->fl) {
2292 free_page((unsigned long)uhci->fl);
2293 uhci->fl = NULL;
2296 usb_free_bus(uhci->bus);
2297 kfree(uhci);
2300 int uhci_start_root_hub(struct uhci *uhci)
2302 struct usb_device *dev;
2304 dev = usb_alloc_dev(NULL, uhci->bus);
2305 if (!dev)
2306 return -1;
2308 uhci->bus->root_hub = dev;
2309 usb_connect(dev);
2311 if (usb_new_device(dev) != 0) {
2312 usb_free_dev(dev);
2314 return -1;
2317 return 0;
2321 * If we've successfully found a UHCI, now is the time to increment the
2322 * module usage count, and return success..
2324 static int setup_uhci(struct pci_dev *dev, int irq, unsigned int io_addr, unsigned int io_size)
2326 int retval;
2327 struct uhci *uhci;
2328 char buf[8], *bufp = buf;
2330 #ifndef __sparc__
2331 sprintf(buf, "%d", irq);
2332 #else
2333 bufp = __irq_itoa(irq);
2334 #endif
2335 printk(KERN_INFO __FILE__ ": USB UHCI at I/O 0x%x, IRQ %s\n",
2336 io_addr, bufp);
2338 uhci = alloc_uhci(io_addr, io_size);
2339 if (!uhci)
2340 return -ENOMEM;
2342 INIT_LIST_HEAD(&uhci->uhci_list);
2343 list_add(&uhci->uhci_list, &uhci_list);
2345 request_region(uhci->io_addr, io_size, "usb-uhci");
2347 reset_hc(uhci);
2349 usb_register_bus(uhci->bus);
2350 start_hc(uhci);
2352 retval = -EBUSY;
2353 if (request_irq(irq, uhci_interrupt, SA_SHIRQ, "usb-uhci", uhci) == 0) {
2354 uhci->irq = irq;
2356 if (!uhci_start_root_hub(uhci)) {
2357 struct pm_dev *pmdev;
2359 pmdev = pm_register(PM_PCI_DEV,
2360 PM_PCI_ID(dev),
2361 handle_pm_event);
2362 if (pmdev)
2363 pmdev->data = uhci;
2364 return 0;
2368 /* Couldn't allocate IRQ if we got here */
2369 list_del(&uhci->uhci_list);
2370 INIT_LIST_HEAD(&uhci->uhci_list);
2372 reset_hc(uhci);
2373 release_region(uhci->io_addr, uhci->io_size);
2374 release_uhci(uhci);
2376 return retval;
2379 static int found_uhci(struct pci_dev *dev)
2381 int i;
2383 /* disable legacy emulation */
2384 pci_write_config_word(dev, USBLEGSUP, USBLEGSUP_DEFAULT);
2386 if (pci_enable_device(dev) < 0)
2387 return -1;
2389 if (!dev->irq) {
2390 err("found UHCI device with no IRQ assigned. check BIOS settings!");
2391 return -1;
2394 /* Search for the IO base address.. */
2395 for (i = 0; i < 6; i++) {
2396 unsigned int io_addr = pci_resource_start(dev, i);
2397 unsigned int io_size = pci_resource_len(dev, i);
2399 /* IO address? */
2400 if (!(pci_resource_flags(dev, i) & IORESOURCE_IO))
2401 continue;
2403 /* Is it already in use? */
2404 if (check_region(io_addr, io_size))
2405 break;
2407 return setup_uhci(dev, dev->irq, io_addr, io_size);
2410 return -1;
2413 static int handle_pm_event(struct pm_dev *dev, pm_request_t rqst, void *data)
2415 struct uhci *uhci = dev->data;
2416 switch (rqst) {
2417 case PM_SUSPEND:
2418 reset_hc(uhci);
2419 break;
2420 case PM_RESUME:
2421 reset_hc(uhci);
2422 start_hc(uhci);
2423 break;
2425 return 0;
2428 int uhci_init(void)
2430 int retval;
2431 struct pci_dev *dev;
2432 u8 type;
2434 retval = -ENOMEM;
2436 /* We throw all of the TD's and QH's into a kmem cache */
2437 /* TD's and QH's need to be 16 byte aligned and SLAB_HWCACHE_ALIGN */
2438 /* does this for us */
2439 uhci_td_cachep = kmem_cache_create("uhci_td",
2440 sizeof(struct uhci_td), 0,
2441 SLAB_HWCACHE_ALIGN, NULL, NULL);
2443 if (!uhci_td_cachep)
2444 goto td_failed;
2446 uhci_qh_cachep = kmem_cache_create("uhci_qh",
2447 sizeof(struct uhci_qh), 0,
2448 SLAB_HWCACHE_ALIGN, NULL, NULL);
2450 if (!uhci_qh_cachep)
2451 goto qh_failed;
2453 uhci_up_cachep = kmem_cache_create("uhci_urb_priv",
2454 sizeof(struct urb_priv), 0, 0, NULL, NULL);
2456 if (!uhci_up_cachep)
2457 goto up_failed;
2459 retval = -ENODEV;
2460 dev = NULL;
2461 for (;;) {
2462 dev = pci_find_class(PCI_CLASS_SERIAL_USB << 8, dev);
2463 if (!dev)
2464 break;
2466 /* Is it the UHCI programming interface? */
2467 pci_read_config_byte(dev, PCI_CLASS_PROG, &type);
2468 if (type != 0)
2469 continue;
2471 /* Ok set it up */
2472 retval = found_uhci(dev);
2475 /* We only want to return an error code if ther was an error */
2476 /* and we didn't find a UHCI controller */
2477 if (retval && list_empty(&uhci_list))
2478 goto init_failed;
2480 return 0;
2482 init_failed:
2483 if (kmem_cache_destroy(uhci_up_cachep))
2484 printk(KERN_INFO "uhci: not all urb_priv's were freed\n");
2486 up_failed:
2487 if (kmem_cache_destroy(uhci_qh_cachep))
2488 printk(KERN_INFO "uhci: not all QH's were freed\n");
2490 qh_failed:
2491 if (kmem_cache_destroy(uhci_td_cachep))
2492 printk(KERN_INFO "uhci: not all TD's were freed\n");
2494 td_failed:
2495 return retval;
2498 void uhci_cleanup(void)
2500 struct list_head *tmp, *head = &uhci_list;
2502 tmp = head->next;
2503 while (tmp != head) {
2504 struct uhci *uhci = list_entry(tmp, struct uhci, uhci_list);
2506 tmp = tmp->next;
2508 list_del(&uhci->uhci_list);
2509 INIT_LIST_HEAD(&uhci->uhci_list);
2511 if (uhci->bus->root_hub)
2512 usb_disconnect(&uhci->bus->root_hub);
2514 usb_deregister_bus(uhci->bus);
2516 reset_hc(uhci);
2517 release_region(uhci->io_addr, uhci->io_size);
2519 uhci_free_pending_qhs(uhci);
2521 release_uhci(uhci);
2524 if (kmem_cache_destroy(uhci_up_cachep))
2525 printk(KERN_INFO "uhci: not all urb_priv's were freed\n");
2527 if (kmem_cache_destroy(uhci_qh_cachep))
2528 printk(KERN_INFO "uhci: not all QH's were freed\n");
2530 if (kmem_cache_destroy(uhci_td_cachep))
2531 printk(KERN_INFO "uhci: not all TD's were freed\n");
2534 #ifdef MODULE
2535 int init_module(void)
2537 return uhci_init();
2540 void cleanup_module(void)
2542 pm_unregister_all(handle_pm_event);
2543 uhci_cleanup();
2546 MODULE_AUTHOR("Linus Torvalds, Johannes Erdfelt, Randy Dunlap, Georg Acher, Deti Fliegl, Thomas Sailer, Roman Weissgaerber");
2547 MODULE_DESCRIPTION("USB Universal Host Controller Interface driver");
2548 #endif //MODULE