Import 2.4.0-test5pre2
[davej-history.git] / drivers / usb / uhci.c
bloba3d8d3d4240d1fcc79e1850ac776fa77bc81ff13
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");
53 static kmem_cache_t *uhci_td_cachep;
54 static kmem_cache_t *uhci_qh_cachep;
55 static kmem_cache_t *uhci_up_cachep; /* urb_priv */
57 static LIST_HEAD(uhci_list);
59 static int rh_submit_urb(struct urb *urb);
60 static int rh_unlink_urb(struct urb *urb);
61 static int uhci_get_current_frame_number(struct usb_device *dev);
62 static int uhci_unlink_generic(struct urb *urb);
63 static int uhci_unlink_urb(struct urb *urb);
65 #define min(a,b) (((a)<(b))?(a):(b))
67 /* If a transfer is still active after this much time, turn off FSBR */
68 #define IDLE_TIMEOUT (HZ / 20) /* 50 ms */
71 * Only the USB core should call uhci_alloc_dev and uhci_free_dev
73 static int uhci_alloc_dev(struct usb_device *dev)
75 return 0;
78 static int uhci_free_dev(struct usb_device *dev)
80 struct uhci *uhci = (struct uhci *)dev->bus->hcpriv;
81 struct list_head *tmp, *head = &uhci->urb_list;
82 unsigned long flags;
84 /* Walk through the entire URB list and forcefully remove any */
85 /* URBs that are still active for that device */
86 nested_lock(&uhci->urblist_lock, flags);
87 tmp = head->next;
88 while (tmp != head) {
89 struct urb *u = list_entry(tmp, struct urb, urb_list);
91 tmp = tmp->next;
93 if (u->dev == dev)
94 uhci_unlink_urb(u);
96 nested_unlock(&uhci->urblist_lock, flags);
98 return 0;
101 static void uhci_add_urb_list(struct uhci *uhci, struct urb *urb)
103 unsigned long flags;
105 nested_lock(&uhci->urblist_lock, flags);
106 list_add(&urb->urb_list, &uhci->urb_list);
107 nested_unlock(&uhci->urblist_lock, flags);
110 static void uhci_remove_urb_list(struct uhci *uhci, struct urb *urb)
112 unsigned long flags;
114 nested_lock(&uhci->urblist_lock, flags);
115 if (!list_empty(&urb->urb_list)) {
116 list_del(&urb->urb_list);
117 INIT_LIST_HEAD(&urb->urb_list);
119 nested_unlock(&uhci->urblist_lock, flags);
122 void uhci_set_next_interrupt(struct uhci *uhci)
124 unsigned long flags;
126 spin_lock_irqsave(&uhci->framelist_lock, flags);
127 uhci->skel_term_td.status |= TD_CTRL_IOC;
128 spin_unlock_irqrestore(&uhci->framelist_lock, flags);
131 void uhci_clear_next_interrupt(struct uhci *uhci)
133 unsigned long flags;
135 spin_lock_irqsave(&uhci->framelist_lock, flags);
136 uhci->skel_term_td.status &= ~TD_CTRL_IOC;
137 spin_unlock_irqrestore(&uhci->framelist_lock, flags);
140 static struct uhci_td *uhci_alloc_td(struct usb_device *dev)
142 struct uhci_td *td;
144 td = kmem_cache_alloc(uhci_td_cachep, in_interrupt() ? SLAB_ATOMIC : SLAB_KERNEL);
145 if (!td)
146 return NULL;
148 td->link = UHCI_PTR_TERM;
149 td->buffer = 0;
151 td->frameptr = NULL;
152 td->nexttd = td->prevtd = NULL;
153 td->dev = dev;
154 INIT_LIST_HEAD(&td->list);
156 usb_inc_dev_use(dev);
158 return td;
161 static void inline uhci_fill_td(struct uhci_td *td, __u32 status,
162 __u32 info, __u32 buffer)
164 td->status = status;
165 td->info = info;
166 td->buffer = buffer;
169 static void uhci_insert_td(struct uhci *uhci, struct uhci_td *skeltd, struct uhci_td *td)
171 unsigned long flags;
173 spin_lock_irqsave(&uhci->framelist_lock, flags);
175 /* Fix the linked list pointers */
176 td->nexttd = skeltd->nexttd;
177 td->prevtd = skeltd;
178 if (skeltd->nexttd)
179 skeltd->nexttd->prevtd = td;
180 skeltd->nexttd = td;
182 td->link = skeltd->link;
183 skeltd->link = virt_to_bus(td);
185 spin_unlock_irqrestore(&uhci->framelist_lock, flags);
189 * We insert Isochronous transfers directly into the frame list at the
190 * beginning
191 * The layout looks as follows:
192 * frame list pointer -> iso td's (if any) ->
193 * periodic interrupt td (if frame 0) -> irq td's -> control qh -> bulk qh
196 static void uhci_insert_td_frame_list(struct uhci *uhci, struct uhci_td *td, unsigned framenum)
198 unsigned long flags;
199 struct uhci_td *nexttd;
201 framenum %= UHCI_NUMFRAMES;
203 spin_lock_irqsave(&uhci->framelist_lock, flags);
205 td->frameptr = &uhci->fl->frame[framenum];
206 td->link = uhci->fl->frame[framenum];
207 if (!(td->link & (UHCI_PTR_TERM | UHCI_PTR_QH))) {
208 nexttd = (struct uhci_td *)uhci_ptr_to_virt(td->link);
209 td->nexttd = nexttd;
210 nexttd->prevtd = td;
211 nexttd->frameptr = NULL;
213 uhci->fl->frame[framenum] = virt_to_bus(td);
215 spin_unlock_irqrestore(&uhci->framelist_lock, flags);
218 static void uhci_remove_td(struct uhci *uhci, struct uhci_td *td)
220 unsigned long flags;
222 /* If it's not inserted, don't remove it */
223 if (!td->frameptr && !td->prevtd && !td->nexttd)
224 return;
226 spin_lock_irqsave(&uhci->framelist_lock, flags);
227 if (td->frameptr) {
228 *(td->frameptr) = td->link;
229 if (td->nexttd) {
230 td->nexttd->frameptr = td->frameptr;
231 td->nexttd->prevtd = NULL;
232 td->nexttd = NULL;
234 td->frameptr = NULL;
235 } else {
236 if (td->prevtd) {
237 td->prevtd->nexttd = td->nexttd;
238 td->prevtd->link = td->link;
240 if (td->nexttd)
241 td->nexttd->prevtd = td->prevtd;
242 td->prevtd = td->nexttd = NULL;
244 td->link = UHCI_PTR_TERM;
245 spin_unlock_irqrestore(&uhci->framelist_lock, flags);
249 * Inserts a td into qh list at the top.
251 static void uhci_insert_tds_in_qh(struct uhci_qh *qh, struct urb *urb, int breadth)
253 struct list_head *tmp, *head;
254 struct urb_priv *urbp = (struct urb_priv *)urb->hcpriv;
255 struct uhci_td *td, *prevtd;
257 if (!urbp)
258 return;
260 head = &urbp->list;
261 tmp = head->next;
262 if (head == tmp)
263 return;
265 td = list_entry(tmp, struct uhci_td, list);
267 /* Add the first TD to the QH element pointer */
268 qh->element = virt_to_bus(td) | (breadth ? 0 : UHCI_PTR_DEPTH);
270 prevtd = td;
272 /* Then link the rest of the TD's */
273 tmp = tmp->next;
274 while (tmp != head) {
275 td = list_entry(tmp, struct uhci_td, list);
277 tmp = tmp->next;
279 prevtd->link = virt_to_bus(td) | (breadth ? 0 : UHCI_PTR_DEPTH);
281 prevtd = td;
284 prevtd->link = UHCI_PTR_TERM;
287 static void uhci_free_td(struct uhci_td *td)
289 if (!list_empty(&td->list))
290 dbg("td is still in URB list!");
292 if (td->dev)
293 usb_dec_dev_use(td->dev);
295 kmem_cache_free(uhci_td_cachep, td);
298 static struct uhci_qh *uhci_alloc_qh(struct usb_device *dev)
300 struct uhci_qh *qh;
302 qh = kmem_cache_alloc(uhci_qh_cachep, in_interrupt() ? SLAB_ATOMIC : SLAB_KERNEL);
303 if (!qh)
304 return NULL;
306 qh->element = UHCI_PTR_TERM;
307 qh->link = UHCI_PTR_TERM;
309 qh->dev = dev;
310 qh->prevqh = qh->nextqh = NULL;
312 INIT_LIST_HEAD(&qh->remove_list);
314 usb_inc_dev_use(dev);
316 return qh;
319 static void uhci_free_qh(struct uhci_qh *qh)
321 if (qh->dev)
322 usb_dec_dev_use(qh->dev);
324 kmem_cache_free(uhci_qh_cachep, qh);
327 static void uhci_insert_qh(struct uhci *uhci, struct uhci_qh *skelqh, struct uhci_qh *qh)
329 unsigned long flags;
331 spin_lock_irqsave(&uhci->framelist_lock, flags);
333 /* Fix the linked list pointers */
334 qh->nextqh = skelqh->nextqh;
335 qh->prevqh = skelqh;
336 if (skelqh->nextqh)
337 skelqh->nextqh->prevqh = qh;
338 skelqh->nextqh = qh;
340 qh->link = skelqh->link;
341 skelqh->link = virt_to_bus(qh) | UHCI_PTR_QH;
343 spin_unlock_irqrestore(&uhci->framelist_lock, flags);
346 static void uhci_remove_qh(struct uhci *uhci, struct uhci_qh *qh)
348 unsigned long flags;
349 int delayed;
351 /* If the QH isn't queued, then we don't need to delay unlink it */
352 delayed = (qh->prevqh || qh->nextqh);
354 spin_lock_irqsave(&uhci->framelist_lock, flags);
355 if (qh->prevqh) {
356 qh->prevqh->nextqh = qh->nextqh;
357 qh->prevqh->link = qh->link;
359 if (qh->nextqh)
360 qh->nextqh->prevqh = qh->prevqh;
361 qh->prevqh = qh->nextqh = NULL;
362 qh->element = qh->link = UHCI_PTR_TERM;
363 spin_unlock_irqrestore(&uhci->framelist_lock, flags);
365 if (delayed) {
366 spin_lock_irqsave(&uhci->qh_remove_lock, flags);
368 /* Check to see if the remove list is empty */
369 /* Set the IOC bit to force an interrupt so we can remove the QH */
370 if (list_empty(&uhci->qh_remove_list))
371 uhci_set_next_interrupt(uhci);
373 /* Add it */
374 list_add(&qh->remove_list, &uhci->qh_remove_list);
376 spin_unlock_irqrestore(&uhci->qh_remove_lock, flags);
377 } else
378 uhci_free_qh(qh);
381 static spinlock_t uhci_append_urb_lock = SPIN_LOCK_UNLOCKED;
383 /* This function will append one URB's QH to another URB's QH. This is for */
384 /* USB_QUEUE_BULK support */
385 static void uhci_append_queued_urb(struct uhci *uhci, struct urb *eurb, struct urb *urb)
387 struct urb_priv *eurbp, *urbp, *furbp, *lurbp;
388 struct list_head *tmp;
389 struct uhci_td *td, *ltd;
390 unsigned long flags;
392 eurbp = eurb->hcpriv;
393 urbp = urb->hcpriv;
395 spin_lock_irqsave(&uhci_append_urb_lock, flags);
397 /* Find the beginning URB in the queue */
398 if (eurbp->queued) {
399 struct list_head *head = &eurbp->urb_queue_list;
401 tmp = head->next;
402 while (tmp != head) {
403 struct urb_priv *turbp =
404 list_entry(tmp, struct urb_priv, urb_queue_list);
406 tmp = tmp->next;
408 if (!turbp->queued)
409 break;
411 } else
412 tmp = &eurbp->urb_queue_list;
414 furbp = list_entry(tmp, struct urb_priv, urb_queue_list);
416 tmp = furbp->urb_queue_list.prev;
417 lurbp = list_entry(tmp, struct urb_priv, urb_queue_list);
419 /* Add this one to the end */
420 list_add_tail(&urbp->urb_queue_list, &furbp->urb_queue_list);
422 /* Grab the last TD from the last URB */
423 ltd = list_entry(lurbp->list.prev, struct uhci_td, list);
425 /* Grab the first TD from the first URB */
426 td = list_entry(urbp->list.next, struct uhci_td, list);
428 /* No breadth since this will only be called for bulk transfers */
429 ltd->link = virt_to_bus(td);
431 spin_unlock_irqrestore(&uhci_append_urb_lock, flags);
434 static void uhci_delete_queued_urb(struct uhci *uhci, struct urb *urb)
436 struct urb_priv *urbp, *nurbp;
437 unsigned long flags;
439 urbp = urb->hcpriv;
441 spin_lock_irqsave(&uhci_append_urb_lock, flags);
443 nurbp = list_entry(urbp->urb_queue_list.next, struct urb_priv,
444 urb_queue_list);
446 if (!urbp->queued) {
447 /* We're the head, so just insert the QH for the next URB */
448 uhci_insert_qh(uhci, &uhci->skel_bulk_qh, nurbp->qh);
449 nurbp->queued = 0;
450 } else {
451 struct urb_priv *purbp;
452 struct uhci_td *ptd;
454 /* We're somewhere in the middle (or end). A bit trickier */
455 /* than the head scenario */
456 purbp = list_entry(urbp->urb_queue_list.prev, struct urb_priv,
457 urb_queue_list);
459 ptd = list_entry(purbp->list.prev, struct uhci_td, list);
460 if (nurbp->queued)
461 /* Close the gap between the two */
462 ptd->link = virt_to_bus(list_entry(nurbp->list.next,
463 struct uhci_td, list));
464 else
465 /* The next URB happens to be the beggining, so */
466 /* we're the last, end the chain */
467 ptd->link = UHCI_PTR_TERM;
471 list_del(&urbp->urb_queue_list);
473 spin_unlock_irqrestore(&uhci_append_urb_lock, flags);
476 struct urb_priv *uhci_alloc_urb_priv(struct urb *urb)
478 struct urb_priv *urbp;
480 urbp = kmem_cache_alloc(uhci_up_cachep, in_interrupt() ? SLAB_ATOMIC : SLAB_KERNEL);
481 if (!urbp)
482 return NULL;
484 memset((void *)urbp, 0, sizeof(*urbp));
486 urbp->inserttime = jiffies;
487 urbp->urb = urb;
489 INIT_LIST_HEAD(&urbp->list);
490 INIT_LIST_HEAD(&urbp->urb_queue_list);
492 urb->hcpriv = urbp;
494 usb_inc_dev_use(urb->dev);
496 return urbp;
499 static void uhci_add_td_to_urb(struct urb *urb, struct uhci_td *td)
501 struct urb_priv *urbp = (struct urb_priv *)urb->hcpriv;
503 td->urb = urb;
505 list_add_tail(&td->list, &urbp->list);
508 static void uhci_remove_td_from_urb(struct urb *urb, struct uhci_td *td)
510 urb = NULL; /* No warnings */
512 if (list_empty(&td->list))
513 return;
515 list_del(&td->list);
516 INIT_LIST_HEAD(&td->list);
518 td->urb = NULL;
521 static void uhci_destroy_urb_priv(struct urb *urb)
523 struct list_head *tmp, *head;
524 struct urb_priv *urbp;
525 struct uhci *uhci;
526 struct uhci_td *td;
527 unsigned long flags;
529 spin_lock_irqsave(&urb->lock, flags);
531 urbp = (struct urb_priv *)urb->hcpriv;
532 if (!urbp)
533 goto unlock;
535 if (!urb->dev || !urb->dev->bus || !urb->dev->bus->hcpriv)
536 goto unlock;
538 uhci = urb->dev->bus->hcpriv;
540 head = &urbp->list;
541 tmp = head->next;
542 while (tmp != head) {
543 td = list_entry(tmp, struct uhci_td, list);
545 tmp = tmp->next;
547 uhci_remove_td_from_urb(urb, td);
549 uhci_remove_td(uhci, td);
551 uhci_free_td(td);
554 urb->hcpriv = NULL;
555 kmem_cache_free(uhci_up_cachep, urbp);
557 usb_dec_dev_use(urb->dev);
559 unlock:
560 spin_unlock_irqrestore(&urb->lock, flags);
563 static void uhci_inc_fsbr(struct uhci *uhci, struct urb *urb)
565 unsigned long flags;
566 struct urb_priv *urbp = (struct urb_priv *)urb->hcpriv;
568 if (!urbp)
569 return;
571 spin_lock_irqsave(&uhci->framelist_lock, flags);
573 if (!urbp->fsbr) {
574 urbp->fsbr = 1;
575 if (!uhci->fsbr++)
576 uhci->skel_term_qh.link = virt_to_bus(&uhci->skel_hs_control_qh) | UHCI_PTR_QH;
579 spin_unlock_irqrestore(&uhci->framelist_lock, flags);
582 static void uhci_dec_fsbr(struct uhci *uhci, struct urb *urb)
584 unsigned long flags;
585 struct urb_priv *urbp = (struct urb_priv *)urb->hcpriv;
587 if (!urbp)
588 return;
590 spin_lock_irqsave(&uhci->framelist_lock, flags);
592 if (urbp->fsbr) {
593 urbp->fsbr = 0;
594 if (!--uhci->fsbr)
595 uhci->skel_term_qh.link = UHCI_PTR_TERM;
598 spin_unlock_irqrestore(&uhci->framelist_lock, flags);
602 * Map status to standard result codes
604 * <status> is (td->status & 0xFE0000) [a.k.a. uhci_status_bits(td->status)]
605 * <dir_out> is True for output TDs and False for input TDs.
607 static int uhci_map_status(int status, int dir_out)
609 if (!status)
610 return 0;
611 if (status & TD_CTRL_BITSTUFF) /* Bitstuff error */
612 return -EPROTO;
613 if (status & TD_CTRL_CRCTIMEO) { /* CRC/Timeout */
614 if (dir_out)
615 return -ETIMEDOUT;
616 else
617 return -EILSEQ;
619 if (status & TD_CTRL_NAK) /* NAK */
620 return -ETIMEDOUT;
621 if (status & TD_CTRL_BABBLE) /* Babble */
622 return -EPIPE;
623 if (status & TD_CTRL_DBUFERR) /* Buffer error */
624 return -ENOSR;
625 if (status & TD_CTRL_STALLED) /* Stalled */
626 return -EPIPE;
627 if (status & TD_CTRL_ACTIVE) /* Active */
628 return 0;
630 return -EINVAL;
634 * Control transfers
636 static int uhci_submit_control(struct urb *urb)
638 struct urb_priv *urbp = (struct urb_priv *)urb->hcpriv;
639 struct uhci *uhci = (struct uhci *)urb->dev->bus->hcpriv;
640 struct uhci_td *td;
641 struct uhci_qh *qh;
642 unsigned long destination, status;
643 int maxsze = usb_maxpacket(urb->dev, urb->pipe, usb_pipeout(urb->pipe));
644 int len = urb->transfer_buffer_length;
645 unsigned char *data = urb->transfer_buffer;
647 /* The "pipe" thing contains the destination in bits 8--18 */
648 destination = (urb->pipe & PIPE_DEVEP_MASK) | USB_PID_SETUP;
650 /* 3 errors */
651 status = (urb->pipe & TD_CTRL_LS) | TD_CTRL_ACTIVE | (3 << 27);
654 * Build the TD for the control request
656 td = uhci_alloc_td(urb->dev);
657 if (!td)
658 return -ENOMEM;
660 uhci_add_td_to_urb(urb, td);
661 uhci_fill_td(td, status, destination | (7 << 21),
662 virt_to_bus(urb->setup_packet));
665 * If direction is "send", change the frame from SETUP (0x2D)
666 * to OUT (0xE1). Else change it from SETUP to IN (0x69).
668 destination ^= (USB_PID_SETUP ^ usb_packetid(urb->pipe));
670 if (!(urb->transfer_flags & USB_DISABLE_SPD))
671 status |= TD_CTRL_SPD;
674 * Build the DATA TD's
676 while (len > 0) {
677 int pktsze = len;
679 if (pktsze > maxsze)
680 pktsze = maxsze;
682 td = uhci_alloc_td(urb->dev);
683 if (!td)
684 return -ENOMEM;
686 /* Alternate Data0/1 (start with Data1) */
687 destination ^= 1 << TD_TOKEN_TOGGLE;
689 uhci_add_td_to_urb(urb, td);
690 uhci_fill_td(td, status, destination | ((pktsze - 1) << 21),
691 virt_to_bus(data));
693 data += pktsze;
694 len -= pktsze;
698 * Build the final TD for control status
700 td = uhci_alloc_td(urb->dev);
701 if (!td)
702 return -ENOMEM;
705 * It's IN if the pipe is an output pipe or we're not expecting
706 * data back.
708 destination &= ~TD_PID;
709 if (usb_pipeout(urb->pipe) || !urb->transfer_buffer_length)
710 destination |= USB_PID_IN;
711 else
712 destination |= USB_PID_OUT;
714 destination |= 1 << TD_TOKEN_TOGGLE; /* End in Data1 */
716 status &= ~TD_CTRL_SPD;
718 uhci_add_td_to_urb(urb, td);
719 uhci_fill_td(td, status | TD_CTRL_IOC,
720 destination | (UHCI_NULL_DATA_SIZE << 21), 0);
722 qh = uhci_alloc_qh(urb->dev);
723 if (!qh)
724 return -ENOMEM;
726 /* Low speed or small transfers gets a different queue and treatment */
727 if (urb->pipe & TD_CTRL_LS) {
728 uhci_insert_tds_in_qh(qh, urb, 0);
729 uhci_insert_qh(uhci, &uhci->skel_ls_control_qh, qh);
730 } else {
731 uhci_insert_tds_in_qh(qh, urb, 1);
732 uhci_insert_qh(uhci, &uhci->skel_hs_control_qh, qh);
733 uhci_inc_fsbr(uhci, urb);
736 urbp->qh = qh;
738 uhci_add_urb_list(uhci, urb);
740 usb_inc_dev_use(urb->dev);
742 return -EINPROGRESS;
745 static int usb_control_retrigger_status(struct urb *urb);
747 static int uhci_result_control(struct urb *urb)
749 struct list_head *tmp, *head;
750 struct urb_priv *urbp = urb->hcpriv;
751 struct uhci_td *td;
752 unsigned int status;
753 int ret = 0;
755 if (!urbp)
756 return -EINVAL;
758 head = &urbp->list;
759 if (head->next == head)
760 return -EINVAL;
762 if (urbp->short_control_packet) {
763 tmp = head->prev;
764 goto status_phase;
767 tmp = head->next;
768 td = list_entry(tmp, struct uhci_td, list);
770 /* The first TD is the SETUP phase, check the status, but skip */
771 /* the count */
772 status = uhci_status_bits(td->status);
773 if (status & TD_CTRL_ACTIVE)
774 return -EINPROGRESS;
776 if (status)
777 goto td_error;
779 urb->actual_length = 0;
781 /* The rest of the TD's (but the last) are data */
782 tmp = tmp->next;
783 while (tmp != head && tmp->next != head) {
784 td = list_entry(tmp, struct uhci_td, list);
786 tmp = tmp->next;
788 if (urbp->fsbr_timeout && (td->status & TD_CTRL_IOC) &&
789 !(td->status & TD_CTRL_ACTIVE)) {
790 uhci_inc_fsbr(urb->dev->bus->hcpriv, urb);
791 urbp->fsbr_timeout = 0;
792 td->status &= ~TD_CTRL_IOC;
795 status = uhci_status_bits(td->status);
796 if (status & TD_CTRL_ACTIVE)
797 return -EINPROGRESS;
799 urb->actual_length += uhci_actual_length(td->status);
801 if (status)
802 goto td_error;
804 /* Check to see if we received a short packet */
805 if (uhci_actual_length(td->status) < uhci_expected_length(td->info)) {
806 if (urb->transfer_flags & USB_DISABLE_SPD) {
807 ret = -EREMOTEIO;
808 goto err;
811 if (uhci_packetid(td->info) == USB_PID_IN)
812 return usb_control_retrigger_status(urb);
813 else
814 return 0;
818 status_phase:
819 td = list_entry(tmp, struct uhci_td, list);
821 /* Control status phase */
822 status = uhci_status_bits(td->status);
824 #ifdef I_HAVE_BUGGY_APC_BACKUPS
825 /* APC BackUPS Pro kludge */
826 /* It tries to send all of the descriptor instead of the amount */
827 /* we requested */
828 if (td->status & TD_CTRL_IOC && /* IOC is masked out by uhci_status_bits */
829 status & TD_CTRL_ACTIVE &&
830 status & TD_CTRL_NAK)
831 return 0;
832 #endif
834 if (status & TD_CTRL_ACTIVE)
835 return -EINPROGRESS;
837 if (status)
838 goto td_error;
840 return 0;
842 td_error:
843 ret = uhci_map_status(status, uhci_packetout(td->info));
844 if (ret == -EPIPE)
845 /* endpoint has stalled - mark it halted */
846 usb_endpoint_halt(urb->dev, uhci_endpoint(td->info),
847 uhci_packetout(td->info));
849 err:
850 if (debug && ret != -EPIPE) {
851 /* Some debugging code */
852 dbg("uhci_result_control() failed with status %x", status);
854 /* Print the chain for debugging purposes */
855 uhci_show_urb_queue(urb);
858 return ret;
861 static int usb_control_retrigger_status(struct urb *urb)
863 struct list_head *tmp, *head;
864 struct urb_priv *urbp = (struct urb_priv *)urb->hcpriv;
865 struct uhci *uhci = urb->dev->bus->hcpriv;
867 urbp->short_control_packet = 1;
869 /* Create a new QH to avoid pointer overwriting problems */
870 uhci_remove_qh(uhci, urbp->qh);
872 /* Delete all of the TD's except for the status TD at the end */
873 head = &urbp->list;
874 tmp = head->next;
875 while (tmp != head && tmp->next != head) {
876 struct uhci_td *td = list_entry(tmp, struct uhci_td, list);
878 tmp = tmp->next;
880 uhci_remove_td_from_urb(urb, td);
882 uhci_remove_td(uhci, td);
884 uhci_free_td(td);
887 urbp->qh = uhci_alloc_qh(urb->dev);
888 if (!urbp->qh) {
889 err("unable to allocate new QH for control retrigger");
890 return -ENOMEM;
893 /* One TD, who cares about Breadth first? */
894 uhci_insert_tds_in_qh(urbp->qh, urb, 0);
896 /* Low speed or small transfers gets a different queue and treatment */
897 if (urb->pipe & TD_CTRL_LS)
898 uhci_insert_qh(uhci, &uhci->skel_ls_control_qh, urbp->qh);
899 else
900 uhci_insert_qh(uhci, &uhci->skel_hs_control_qh, urbp->qh);
902 return -EINPROGRESS;
906 * Interrupt transfers
908 static int uhci_submit_interrupt(struct urb *urb)
910 struct uhci_td *td;
911 unsigned long destination, status;
912 struct uhci *uhci = (struct uhci *)urb->dev->bus->hcpriv;
914 if (urb->transfer_buffer_length > usb_maxpacket(urb->dev, urb->pipe, usb_pipeout(urb->pipe)))
915 return -EINVAL;
917 /* The "pipe" thing contains the destination in bits 8--18 */
918 destination = (urb->pipe & PIPE_DEVEP_MASK) | usb_packetid(urb->pipe);
920 status = (urb->pipe & TD_CTRL_LS) | TD_CTRL_ACTIVE | TD_CTRL_IOC;
922 td = uhci_alloc_td(urb->dev);
923 if (!td)
924 return -ENOMEM;
926 destination |= (usb_gettoggle(urb->dev, usb_pipeendpoint(urb->pipe), usb_pipeout(urb->pipe)) << TD_TOKEN_TOGGLE);
927 destination |= ((urb->transfer_buffer_length - 1) << 21);
929 usb_dotoggle(urb->dev, usb_pipeendpoint(urb->pipe), usb_pipeout(urb->pipe));
931 uhci_add_td_to_urb(urb, td);
932 uhci_fill_td(td, status, destination,
933 virt_to_bus(urb->transfer_buffer));
935 uhci_insert_td(uhci, &uhci->skeltd[__interval_to_skel(urb->interval)], td);
937 uhci_add_urb_list(uhci, urb);
939 return -EINPROGRESS;
942 static int uhci_result_interrupt(struct urb *urb)
944 struct list_head *tmp, *head;
945 struct urb_priv *urbp = urb->hcpriv;
946 struct uhci_td *td;
947 unsigned int status;
948 int ret = 0;
950 if (!urbp)
951 return -EINVAL;
953 urb->actual_length = 0;
955 head = &urbp->list;
956 tmp = head->next;
957 while (tmp != head) {
958 td = list_entry(tmp, struct uhci_td, list);
960 tmp = tmp->next;
962 if (urbp->fsbr_timeout && (td->status & TD_CTRL_IOC) &&
963 !(td->status & TD_CTRL_ACTIVE)) {
964 uhci_inc_fsbr(urb->dev->bus->hcpriv, urb);
965 urbp->fsbr_timeout = 0;
966 td->status &= ~TD_CTRL_IOC;
969 status = uhci_status_bits(td->status);
970 if (status & TD_CTRL_ACTIVE)
971 return -EINPROGRESS;
973 urb->actual_length += uhci_actual_length(td->status);
975 if (status)
976 goto td_error;
978 if (uhci_actual_length(td->status) < uhci_expected_length(td->info)) {
979 usb_settoggle(urb->dev, uhci_endpoint(td->info),
980 uhci_packetout(td->info),
981 uhci_toggle(td->info) ^ 1);
983 if (urb->transfer_flags & USB_DISABLE_SPD) {
984 ret = -EREMOTEIO;
985 goto err;
986 } else
987 return 0;
991 return 0;
993 td_error:
994 ret = uhci_map_status(status, uhci_packetout(td->info));
995 if (ret == -EPIPE)
996 /* endpoint has stalled - mark it halted */
997 usb_endpoint_halt(urb->dev, uhci_endpoint(td->info),
998 uhci_packetout(td->info));
1000 err:
1001 if (debug && ret != -EPIPE) {
1002 /* Some debugging code */
1003 dbg("uhci_result_interrupt/bulk() failed with status %x",
1004 status);
1006 /* Print the chain for debugging purposes */
1007 if (urbp->qh)
1008 uhci_show_urb_queue(urb);
1009 else
1010 uhci_show_td(td);
1013 return ret;
1016 static void uhci_reset_interrupt(struct urb *urb)
1018 struct list_head *tmp;
1019 struct urb_priv *urbp = (struct urb_priv *)urb->hcpriv;
1020 struct uhci_td *td;
1022 if (!urbp)
1023 return;
1025 tmp = urbp->list.next;
1026 td = list_entry(tmp, struct uhci_td, list);
1027 if (!td)
1028 return;
1030 td->status = (td->status & 0x2F000000) | TD_CTRL_ACTIVE | TD_CTRL_IOC;
1031 td->info &= ~(1 << TD_TOKEN_TOGGLE);
1032 td->info |= (usb_gettoggle(urb->dev, usb_pipeendpoint(urb->pipe), usb_pipeout(urb->pipe)) << TD_TOKEN_TOGGLE);
1033 usb_dotoggle(urb->dev, usb_pipeendpoint(urb->pipe), usb_pipeout(urb->pipe));
1035 urb->status = -EINPROGRESS;
1039 * Bulk transfers
1041 static int uhci_submit_bulk(struct urb *urb, struct urb *eurb)
1043 struct uhci_td *td;
1044 struct uhci_qh *qh;
1045 unsigned long destination, status;
1046 struct uhci *uhci = (struct uhci *)urb->dev->bus->hcpriv;
1047 int maxsze = usb_maxpacket(urb->dev, urb->pipe, usb_pipeout(urb->pipe));
1048 int len = urb->transfer_buffer_length;
1049 unsigned char *data = urb->transfer_buffer;
1050 struct urb_priv *urbp = (struct urb_priv *)urb->hcpriv;
1052 if (len < 0)
1053 return -EINVAL;
1055 /* Can't have low speed bulk transfers */
1056 if (urb->pipe & TD_CTRL_LS)
1057 return -EINVAL;
1059 /* The "pipe" thing contains the destination in bits 8--18 */
1060 destination = (urb->pipe & PIPE_DEVEP_MASK) | usb_packetid(urb->pipe);
1062 /* 3 errors */
1063 status = TD_CTRL_ACTIVE | (3 << TD_CTRL_C_ERR_SHIFT);
1065 if (!(urb->transfer_flags & USB_DISABLE_SPD))
1066 status |= TD_CTRL_SPD;
1069 * Build the DATA TD's
1071 do { /* Allow zero length packets */
1072 int pktsze = len;
1074 if (pktsze > maxsze)
1075 pktsze = maxsze;
1077 td = uhci_alloc_td(urb->dev);
1078 if (!td)
1079 return -ENOMEM;
1081 uhci_add_td_to_urb(urb, td);
1082 uhci_fill_td(td, status, destination | ((pktsze - 1) << 21) |
1083 (usb_gettoggle(urb->dev, usb_pipeendpoint(urb->pipe),
1084 usb_pipeout(urb->pipe)) << TD_TOKEN_TOGGLE),
1085 virt_to_bus(data));
1087 data += pktsze;
1088 len -= maxsze;
1090 if (len <= 0)
1091 td->status |= TD_CTRL_IOC;
1093 usb_dotoggle(urb->dev, usb_pipeendpoint(urb->pipe),
1094 usb_pipeout(urb->pipe));
1095 } while (len > 0);
1097 qh = uhci_alloc_qh(urb->dev);
1098 if (!qh)
1099 return -ENOMEM;
1101 urbp->qh = qh;
1103 /* Always assume depth first */
1104 uhci_insert_tds_in_qh(qh, urb, 1);
1106 if (urb->transfer_flags & USB_QUEUE_BULK && eurb) {
1107 urbp->queued = 1;
1108 uhci_append_queued_urb(uhci, eurb, urb);
1109 } else
1110 uhci_insert_qh(uhci, &uhci->skel_bulk_qh, qh);
1112 uhci_add_urb_list(uhci, urb);
1114 uhci_inc_fsbr(uhci, urb);
1116 return -EINPROGRESS;
1119 /* We can use the result interrupt since they're identical */
1120 #define uhci_result_bulk uhci_result_interrupt
1123 * Isochronous transfers
1125 static int isochronous_find_limits(struct urb *urb, unsigned int *start, unsigned int *end)
1127 struct urb *last_urb = NULL;
1128 struct uhci *uhci = (struct uhci *)urb->dev->bus->hcpriv;
1129 struct list_head *tmp, *head = &uhci->urb_list;
1130 int ret = 0;
1131 unsigned long flags;
1133 nested_lock(&uhci->urblist_lock, flags);
1134 tmp = head->next;
1135 while (tmp != head) {
1136 struct urb *u = list_entry(tmp, struct urb, urb_list);
1138 tmp = tmp->next;
1140 /* look for pending URB's with identical pipe handle */
1141 if ((urb->pipe == u->pipe) && (urb->dev == u->dev) &&
1142 (u->status == -EINPROGRESS) && (u != urb)) {
1143 if (!last_urb)
1144 *start = u->start_frame;
1145 last_urb = u;
1149 if (last_urb) {
1150 *end = (last_urb->start_frame + last_urb->number_of_packets) & 1023;
1151 ret = 0;
1152 } else
1153 ret = -1; /* no previous urb found */
1155 nested_unlock(&uhci->urblist_lock, flags);
1157 return ret;
1160 static int isochronous_find_start(struct urb *urb)
1162 int limits;
1163 unsigned int start = 0, end = 0;
1165 if (urb->number_of_packets > 900) /* 900? Why? */
1166 return -EFBIG;
1168 limits = isochronous_find_limits(urb, &start, &end);
1170 if (urb->transfer_flags & USB_ISO_ASAP) {
1171 if (limits) {
1172 int curframe;
1174 curframe = uhci_get_current_frame_number(urb->dev) % UHCI_NUMFRAMES;
1175 urb->start_frame = (curframe + 10) % UHCI_NUMFRAMES;
1176 } else
1177 urb->start_frame = end;
1178 } else {
1179 urb->start_frame %= UHCI_NUMFRAMES;
1180 /* FIXME: Sanity check */
1183 return 0;
1186 static int uhci_submit_isochronous(struct urb *urb)
1188 struct uhci_td *td;
1189 struct uhci *uhci = (struct uhci *)urb->dev->bus->hcpriv;
1190 int i, ret, framenum;
1191 int status, destination;
1193 status = TD_CTRL_ACTIVE | TD_CTRL_IOS;
1194 destination = (urb->pipe & PIPE_DEVEP_MASK) | usb_packetid(urb->pipe);
1196 ret = isochronous_find_start(urb);
1197 if (ret)
1198 return ret;
1200 framenum = urb->start_frame;
1201 for (i = 0; i < urb->number_of_packets; i++, framenum++) {
1202 if (!urb->iso_frame_desc[i].length)
1203 continue;
1205 td = uhci_alloc_td(urb->dev);
1206 if (!td)
1207 return -ENOMEM;
1209 uhci_add_td_to_urb(urb, td);
1210 uhci_fill_td(td, status, destination | ((urb->iso_frame_desc[i].length - 1) << 21),
1211 virt_to_bus(urb->transfer_buffer + urb->iso_frame_desc[i].offset));
1213 if (i + 1 >= urb->number_of_packets)
1214 td->status |= TD_CTRL_IOC;
1216 uhci_insert_td_frame_list(uhci, td, framenum);
1219 uhci_add_urb_list(uhci, urb);
1221 return -EINPROGRESS;
1224 static int uhci_result_isochronous(struct urb *urb)
1226 struct list_head *tmp, *head;
1227 struct urb_priv *urbp = (struct urb_priv *)urb->hcpriv;
1228 int status;
1229 int i, ret = 0;
1231 if (!urbp)
1232 return -EINVAL;
1234 urb->actual_length = 0;
1236 i = 0;
1237 head = &urbp->list;
1238 tmp = head->next;
1239 while (tmp != head) {
1240 struct uhci_td *td = list_entry(tmp, struct uhci_td, list);
1241 int actlength;
1243 tmp = tmp->next;
1245 if (td->status & TD_CTRL_ACTIVE)
1246 return -EINPROGRESS;
1248 actlength = uhci_actual_length(td->status);
1249 urb->iso_frame_desc[i].actual_length = actlength;
1250 urb->actual_length += actlength;
1252 status = uhci_map_status(uhci_status_bits(td->status), usb_pipeout(urb->pipe));
1253 urb->iso_frame_desc[i].status = status;
1254 if (status != 0) {
1255 urb->error_count++;
1256 ret = status;
1259 i++;
1262 return ret;
1265 static struct urb *uhci_find_urb_ep(struct uhci *uhci, struct urb *urb)
1267 struct list_head *tmp, *head = &uhci->urb_list;
1268 unsigned long flags;
1269 struct urb *u = NULL;
1271 if (usb_pipeisoc(urb->pipe))
1272 return NULL;
1274 nested_lock(&uhci->urblist_lock, flags);
1275 tmp = head->next;
1276 while (tmp != head) {
1277 u = list_entry(tmp, struct urb, urb_list);
1279 tmp = tmp->next;
1281 if (u->dev == urb->dev &&
1282 u->pipe == urb->pipe)
1283 goto found;
1285 u = NULL;
1287 found:
1288 nested_unlock(&uhci->urblist_lock, flags);
1290 return u;
1293 static int uhci_submit_urb(struct urb *urb)
1295 int ret = -EINVAL;
1296 struct uhci *uhci;
1297 unsigned long flags;
1298 struct urb *u;
1300 if (!urb)
1301 return -EINVAL;
1303 if (!urb->dev || !urb->dev->bus || !urb->dev->bus->hcpriv)
1304 return -ENODEV;
1306 uhci = (struct uhci *)urb->dev->bus->hcpriv;
1308 /* Short circuit the virtual root hub */
1309 if (usb_pipedevice(urb->pipe) == uhci->rh.devnum)
1310 return rh_submit_urb(urb);
1312 u = uhci_find_urb_ep(uhci, urb);
1313 if (u && !(urb->transfer_flags & USB_QUEUE_BULK))
1314 return -ENXIO;
1316 spin_lock_irqsave(&urb->lock, flags);
1318 if (!uhci_alloc_urb_priv(urb)) {
1319 spin_unlock_irqrestore(&urb->lock, flags);
1320 return -ENOMEM;
1323 switch (usb_pipetype(urb->pipe)) {
1324 case PIPE_CONTROL:
1325 ret = uhci_submit_control(urb);
1326 break;
1327 case PIPE_INTERRUPT:
1328 ret = uhci_submit_interrupt(urb);
1329 break;
1330 case PIPE_BULK:
1331 ret = uhci_submit_bulk(urb, u);
1332 break;
1333 case PIPE_ISOCHRONOUS:
1334 ret = uhci_submit_isochronous(urb);
1335 break;
1338 urb->status = ret;
1340 spin_unlock_irqrestore(&urb->lock, flags);
1342 if (ret == -EINPROGRESS)
1343 ret = 0;
1344 else
1345 uhci_unlink_generic(urb);
1347 return ret;
1351 * Return the result of a transfer
1353 * Must be called with urblist_lock acquired
1355 static void uhci_transfer_result(struct urb *urb)
1357 struct urb *turb;
1358 int proceed = 0, is_ring = 0;
1359 int ret = -EINVAL;
1360 unsigned long flags;
1362 spin_lock_irqsave(&urb->lock, flags);
1364 switch (usb_pipetype(urb->pipe)) {
1365 case PIPE_CONTROL:
1366 ret = uhci_result_control(urb);
1367 break;
1368 case PIPE_INTERRUPT:
1369 ret = uhci_result_interrupt(urb);
1370 break;
1371 case PIPE_BULK:
1372 ret = uhci_result_bulk(urb);
1373 break;
1374 case PIPE_ISOCHRONOUS:
1375 ret = uhci_result_isochronous(urb);
1376 break;
1379 urb->status = ret;
1381 spin_unlock_irqrestore(&urb->lock, flags);
1383 if (ret == -EINPROGRESS)
1384 return;
1386 switch (usb_pipetype(urb->pipe)) {
1387 case PIPE_CONTROL:
1388 case PIPE_BULK:
1389 case PIPE_ISOCHRONOUS:
1390 uhci_unlink_generic(urb);
1391 break;
1392 case PIPE_INTERRUPT:
1393 /* Interrupts are an exception */
1394 urb->complete(urb);
1395 if (urb->interval)
1396 uhci_reset_interrupt(urb);
1397 else
1398 uhci_unlink_generic(urb);
1399 return; /* <-- Note the return */
1402 if (urb->next) {
1403 turb = urb->next;
1404 do {
1405 if (turb->status != -EINPROGRESS) {
1406 proceed = 1;
1407 break;
1410 turb = turb->next;
1411 } while (turb && turb != urb && turb != urb->next);
1413 if (turb == urb || turb == urb->next)
1414 is_ring = 1;
1417 if (urb->complete && (!proceed || (urb->transfer_flags & USB_URB_EARLY_COMPLETE))) {
1418 urb->complete(urb);
1419 if (!proceed && is_ring)
1420 uhci_submit_urb(urb);
1423 if (proceed && urb->next) {
1424 turb = urb->next;
1425 do {
1426 if (turb->status != -EINPROGRESS &&
1427 uhci_submit_urb(turb) != 0)
1429 turb = turb->next;
1430 } while (turb && turb != urb->next);
1432 if (urb->complete && !(urb->transfer_flags & USB_URB_EARLY_COMPLETE))
1433 urb->complete(urb);
1437 static int uhci_unlink_generic(struct urb *urb)
1439 struct urb_priv *urbp = urb->hcpriv;
1440 struct uhci *uhci = (struct uhci *)urb->dev->bus->hcpriv;
1442 if (!urbp)
1443 return -EINVAL;
1445 uhci_dec_fsbr(uhci, urb); /* Safe since it checks */
1447 uhci_remove_urb_list(uhci, urb);
1449 if (urbp->qh)
1450 /* The interrupt loop will reclaim the QH's */
1451 uhci_remove_qh(uhci, urbp->qh);
1453 if (!list_empty(&urbp->urb_queue_list))
1454 uhci_delete_queued_urb(uhci, urb);
1456 uhci_destroy_urb_priv(urb);
1458 return 0;
1461 static int uhci_unlink_urb(struct urb *urb)
1463 struct uhci *uhci;
1464 int ret = 0;
1465 unsigned long flags;
1467 if (!urb)
1468 return -EINVAL;
1470 if (!urb->dev || !urb->dev->bus)
1471 return -ENODEV;
1473 uhci = (struct uhci *)urb->dev->bus->hcpriv;
1475 /* Short circuit the virtual root hub */
1476 if (usb_pipedevice(urb->pipe) == uhci->rh.devnum)
1477 return rh_unlink_urb(urb);
1479 if (urb->status == -EINPROGRESS) {
1480 uhci_unlink_generic(urb);
1482 if (urb->transfer_flags & USB_ASYNC_UNLINK) {
1483 urb->status = -ECONNABORTED;
1485 spin_lock_irqsave(&uhci->urb_remove_lock, flags);
1487 /* Check to see if the remove list is empty */
1488 if (list_empty(&uhci->urb_remove_list))
1489 uhci_set_next_interrupt(uhci);
1491 list_add(&urb->urb_list, &uhci->urb_remove_list);
1493 spin_unlock_irqrestore(&uhci->urb_remove_lock, flags);
1494 } else {
1495 urb->status = -ENOENT;
1497 if (in_interrupt()) { /* wait at least 1 frame */
1498 static int errorcount = 10;
1500 if (errorcount--)
1501 dbg("uhci_unlink_urb called from interrupt for urb %p", urb);
1502 udelay(1000);
1503 } else
1504 schedule_timeout(1+1*HZ/1000);
1506 if (urb->complete)
1507 urb->complete(urb);
1511 return ret;
1514 static int uhci_fsbr_timeout(struct uhci *uhci, struct urb *urb)
1516 struct urb_priv *urbp = (struct urb_priv *)urb->hcpriv;
1517 struct list_head *head, *tmp;
1519 uhci_dec_fsbr(uhci, urb);
1521 /* There is a race with updating IOC in here, but it's not worth */
1522 /* trying to fix since this is merely an optimization. The only */
1523 /* time we'd lose is if the status of the packet got updated */
1524 /* and we'd be turning on FSBR next frame anyway, so it's a wash */
1525 urbp->fsbr_timeout = 1;
1527 head = &urbp->list;
1528 tmp = head->next;
1529 while (tmp != head) {
1530 struct uhci_td *td = list_entry(tmp, struct uhci_td, list);
1532 tmp = tmp->next;
1534 if (td->status & TD_CTRL_ACTIVE) {
1535 td->status |= TD_CTRL_IOC;
1536 break;
1540 return 0;
1544 * uhci_get_current_frame_number()
1546 * returns the current frame number for a USB bus/controller.
1548 static int uhci_get_current_frame_number(struct usb_device *dev)
1550 struct uhci *uhci = (struct uhci *)dev->bus->hcpriv;
1552 return inw(uhci->io_addr + USBFRNUM);
1555 struct usb_operations uhci_device_operations = {
1556 uhci_alloc_dev,
1557 uhci_free_dev,
1558 uhci_get_current_frame_number,
1559 uhci_submit_urb,
1560 uhci_unlink_urb
1563 /* -------------------------------------------------------------------
1564 Virtual Root Hub
1565 ------------------------------------------------------------------- */
1567 static __u8 root_hub_dev_des[] =
1569 0x12, /* __u8 bLength; */
1570 0x01, /* __u8 bDescriptorType; Device */
1571 0x00, /* __u16 bcdUSB; v1.0 */
1572 0x01,
1573 0x09, /* __u8 bDeviceClass; HUB_CLASSCODE */
1574 0x00, /* __u8 bDeviceSubClass; */
1575 0x00, /* __u8 bDeviceProtocol; */
1576 0x08, /* __u8 bMaxPacketSize0; 8 Bytes */
1577 0x00, /* __u16 idVendor; */
1578 0x00,
1579 0x00, /* __u16 idProduct; */
1580 0x00,
1581 0x00, /* __u16 bcdDevice; */
1582 0x00,
1583 0x00, /* __u8 iManufacturer; */
1584 0x02, /* __u8 iProduct; */
1585 0x01, /* __u8 iSerialNumber; */
1586 0x01 /* __u8 bNumConfigurations; */
1590 /* Configuration descriptor */
1591 static __u8 root_hub_config_des[] =
1593 0x09, /* __u8 bLength; */
1594 0x02, /* __u8 bDescriptorType; Configuration */
1595 0x19, /* __u16 wTotalLength; */
1596 0x00,
1597 0x01, /* __u8 bNumInterfaces; */
1598 0x01, /* __u8 bConfigurationValue; */
1599 0x00, /* __u8 iConfiguration; */
1600 0x40, /* __u8 bmAttributes;
1601 Bit 7: Bus-powered, 6: Self-powered,
1602 Bit 5 Remote-wakeup, 4..0: resvd */
1603 0x00, /* __u8 MaxPower; */
1605 /* interface */
1606 0x09, /* __u8 if_bLength; */
1607 0x04, /* __u8 if_bDescriptorType; Interface */
1608 0x00, /* __u8 if_bInterfaceNumber; */
1609 0x00, /* __u8 if_bAlternateSetting; */
1610 0x01, /* __u8 if_bNumEndpoints; */
1611 0x09, /* __u8 if_bInterfaceClass; HUB_CLASSCODE */
1612 0x00, /* __u8 if_bInterfaceSubClass; */
1613 0x00, /* __u8 if_bInterfaceProtocol; */
1614 0x00, /* __u8 if_iInterface; */
1616 /* endpoint */
1617 0x07, /* __u8 ep_bLength; */
1618 0x05, /* __u8 ep_bDescriptorType; Endpoint */
1619 0x81, /* __u8 ep_bEndpointAddress; IN Endpoint 1 */
1620 0x03, /* __u8 ep_bmAttributes; Interrupt */
1621 0x08, /* __u16 ep_wMaxPacketSize; 8 Bytes */
1622 0x00,
1623 0xff /* __u8 ep_bInterval; 255 ms */
1626 static __u8 root_hub_hub_des[] =
1628 0x09, /* __u8 bLength; */
1629 0x29, /* __u8 bDescriptorType; Hub-descriptor */
1630 0x02, /* __u8 bNbrPorts; */
1631 0x00, /* __u16 wHubCharacteristics; */
1632 0x00,
1633 0x01, /* __u8 bPwrOn2pwrGood; 2ms */
1634 0x00, /* __u8 bHubContrCurrent; 0 mA */
1635 0x00, /* __u8 DeviceRemovable; *** 7 Ports max *** */
1636 0xff /* __u8 PortPwrCtrlMask; *** 7 ports max *** */
1639 /*-------------------------------------------------------------------------*/
1640 /* prepare Interrupt pipe transaction data; HUB INTERRUPT ENDPOINT */
1641 static int rh_send_irq(struct urb *urb)
1643 int i, len = 1;
1644 struct uhci *uhci = (struct uhci *)urb->dev->bus->hcpriv;
1645 unsigned int io_addr = uhci->io_addr;
1646 __u16 data = 0;
1648 for (i = 0; i < uhci->rh.numports; i++) {
1649 data |= ((inw(io_addr + USBPORTSC1 + i * 2) & 0xa) > 0 ? (1 << (i + 1)) : 0);
1650 len = (i + 1) / 8 + 1;
1653 *(__u16 *) urb->transfer_buffer = cpu_to_le16(data);
1654 urb->actual_length = len;
1655 urb->status = USB_ST_NOERROR;
1657 if ((data > 0) && (uhci->rh.send != 0)) {
1658 dbg("root-hub INT complete: port1: %x port2: %x data: %x",
1659 inw(io_addr + USBPORTSC1), inw(io_addr + USBPORTSC2), data);
1660 urb->complete(urb);
1663 return USB_ST_NOERROR;
1666 /*-------------------------------------------------------------------------*/
1667 /* Virtual Root Hub INTs are polled by this timer every "interval" ms */
1668 static int rh_init_int_timer(struct urb *urb);
1670 static void rh_int_timer_do(unsigned long ptr)
1672 struct urb *urb = (struct urb *)ptr;
1673 struct uhci *uhci = (struct uhci *)urb->dev->bus->hcpriv;
1674 struct list_head *tmp, *head = &uhci->urb_list;
1675 struct urb_priv *urbp;
1676 int len;
1677 unsigned long flags;
1679 if (uhci->rh.send) {
1680 len = rh_send_irq(urb);
1681 if (len > 0) {
1682 urb->actual_length = len;
1683 if (urb->complete)
1684 urb->complete(urb);
1688 nested_lock(&uhci->urblist_lock, flags);
1689 tmp = head->next;
1690 while (tmp != head) {
1691 struct urb *u = list_entry(tmp, urb_t, urb_list);
1693 tmp = tmp->next;
1695 urbp = (struct urb_priv *)u->hcpriv;
1696 if (urbp) {
1697 /* Check if the FSBR timed out */
1698 if (urbp->fsbr && time_after(urbp->inserttime + IDLE_TIMEOUT, jiffies))
1699 uhci_fsbr_timeout(uhci, u);
1701 /* Check if the URB timed out */
1702 if (u->timeout && time_after(u->timeout, jiffies)) {
1703 u->transfer_flags |= USB_ASYNC_UNLINK | USB_TIMEOUT_KILLED;
1704 uhci_unlink_urb(u);
1708 nested_unlock(&uhci->urblist_lock, flags);
1710 rh_init_int_timer(urb);
1713 /*-------------------------------------------------------------------------*/
1714 /* Root Hub INTs are polled by this timer */
1715 static int rh_init_int_timer(struct urb *urb)
1717 struct uhci *uhci = (struct uhci *)urb->dev->bus->hcpriv;
1719 uhci->rh.interval = urb->interval;
1720 init_timer(&uhci->rh.rh_int_timer);
1721 uhci->rh.rh_int_timer.function = rh_int_timer_do;
1722 uhci->rh.rh_int_timer.data = (unsigned long)urb;
1723 uhci->rh.rh_int_timer.expires = jiffies + (HZ * (urb->interval < 30 ? 30 : urb->interval)) / 1000;
1724 add_timer(&uhci->rh.rh_int_timer);
1726 return 0;
1729 /*-------------------------------------------------------------------------*/
1730 #define OK(x) len = (x); break
1732 #define CLR_RH_PORTSTAT(x) \
1733 status = inw(io_addr + USBPORTSC1 + 2 * (wIndex-1)); \
1734 status = (status & 0xfff5) & ~(x); \
1735 outw(status, io_addr + USBPORTSC1 + 2 * (wIndex-1))
1737 #define SET_RH_PORTSTAT(x) \
1738 status = inw(io_addr + USBPORTSC1 + 2 * (wIndex-1)); \
1739 status = (status & 0xfff5) | (x); \
1740 outw(status, io_addr + USBPORTSC1 + 2 * (wIndex-1))
1743 /*-------------------------------------------------------------------------*/
1744 /*************************
1745 ** Root Hub Control Pipe
1746 *************************/
1748 static int rh_submit_urb(struct urb *urb)
1750 struct uhci *uhci = (struct uhci *)urb->dev->bus->hcpriv;
1751 unsigned int pipe = urb->pipe;
1752 devrequest *cmd = (devrequest *)urb->setup_packet;
1753 void *data = urb->transfer_buffer;
1754 int leni = urb->transfer_buffer_length;
1755 int len = 0;
1756 int status = 0;
1757 int stat = USB_ST_NOERROR;
1758 int i;
1759 unsigned int io_addr = uhci->io_addr;
1760 __u16 cstatus;
1761 __u16 bmRType_bReq;
1762 __u16 wValue;
1763 __u16 wIndex;
1764 __u16 wLength;
1766 if (usb_pipetype(pipe) == PIPE_INTERRUPT) {
1767 uhci->rh.urb = urb;
1768 uhci->rh.send = 1;
1769 uhci->rh.interval = urb->interval;
1770 rh_init_int_timer(urb);
1772 return USB_ST_NOERROR;
1775 bmRType_bReq = cmd->requesttype | cmd->request << 8;
1776 wValue = le16_to_cpu(cmd->value);
1777 wIndex = le16_to_cpu(cmd->index);
1778 wLength = le16_to_cpu(cmd->length);
1780 for (i = 0; i < 8; i++)
1781 uhci->rh.c_p_r[i] = 0;
1783 switch (bmRType_bReq) {
1784 /* Request Destination:
1785 without flags: Device,
1786 RH_INTERFACE: interface,
1787 RH_ENDPOINT: endpoint,
1788 RH_CLASS means HUB here,
1789 RH_OTHER | RH_CLASS almost ever means HUB_PORT here
1792 case RH_GET_STATUS:
1793 *(__u16 *)data = cpu_to_le16(1);
1794 OK(2);
1795 case RH_GET_STATUS | RH_INTERFACE:
1796 *(__u16 *)data = cpu_to_le16(0);
1797 OK(2);
1798 case RH_GET_STATUS | RH_ENDPOINT:
1799 *(__u16 *)data = cpu_to_le16(0);
1800 OK(2);
1801 case RH_GET_STATUS | RH_CLASS:
1802 *(__u32 *)data = cpu_to_le32(0);
1803 OK(4); /* hub power */
1804 case RH_GET_STATUS | RH_OTHER | RH_CLASS:
1805 status = inw(io_addr + USBPORTSC1 + 2 * (wIndex - 1));
1806 cstatus = ((status & USBPORTSC_CSC) >> (1 - 0)) |
1807 ((status & USBPORTSC_PEC) >> (3 - 1)) |
1808 (uhci->rh.c_p_r[wIndex - 1] << (0 + 4));
1809 status = (status & USBPORTSC_CCS) |
1810 ((status & USBPORTSC_PE) >> (2 - 1)) |
1811 ((status & USBPORTSC_SUSP) >> (12 - 2)) |
1812 ((status & USBPORTSC_PR) >> (9 - 4)) |
1813 (1 << 8) | /* power on */
1814 ((status & USBPORTSC_LSDA) << (-8 + 9));
1816 *(__u16 *)data = cpu_to_le16(status);
1817 *(__u16 *)(data + 2) = cpu_to_le16(cstatus);
1818 OK(4);
1819 case RH_CLEAR_FEATURE | RH_ENDPOINT:
1820 switch (wValue) {
1821 case RH_ENDPOINT_STALL:
1822 OK(0);
1824 break;
1825 case RH_CLEAR_FEATURE | RH_CLASS:
1826 switch (wValue) {
1827 case RH_C_HUB_OVER_CURRENT:
1828 OK(0); /* hub power over current */
1830 break;
1831 case RH_CLEAR_FEATURE | RH_OTHER | RH_CLASS:
1832 switch (wValue) {
1833 case RH_PORT_ENABLE:
1834 CLR_RH_PORTSTAT(USBPORTSC_PE);
1835 OK(0);
1836 case RH_PORT_SUSPEND:
1837 CLR_RH_PORTSTAT(USBPORTSC_SUSP);
1838 OK(0);
1839 case RH_PORT_POWER:
1840 OK(0); /* port power */
1841 case RH_C_PORT_CONNECTION:
1842 SET_RH_PORTSTAT(USBPORTSC_CSC);
1843 OK(0);
1844 case RH_C_PORT_ENABLE:
1845 SET_RH_PORTSTAT(USBPORTSC_PEC);
1846 OK(0);
1847 case RH_C_PORT_SUSPEND:
1848 /*** WR_RH_PORTSTAT(RH_PS_PSSC); */
1849 OK(0);
1850 case RH_C_PORT_OVER_CURRENT:
1851 OK(0); /* port power over current */
1852 case RH_C_PORT_RESET:
1853 uhci->rh.c_p_r[wIndex - 1] = 0;
1854 OK(0);
1856 break;
1857 case RH_SET_FEATURE | RH_OTHER | RH_CLASS:
1858 switch (wValue) {
1859 case RH_PORT_SUSPEND:
1860 SET_RH_PORTSTAT(USBPORTSC_SUSP);
1861 OK(0);
1862 case RH_PORT_RESET:
1863 SET_RH_PORTSTAT(USBPORTSC_PR);
1864 wait_ms(50); /* USB v1.1 7.1.7.3 */
1865 uhci->rh.c_p_r[wIndex - 1] = 1;
1866 CLR_RH_PORTSTAT(USBPORTSC_PR);
1867 udelay(10);
1868 SET_RH_PORTSTAT(USBPORTSC_PE);
1869 wait_ms(10);
1870 SET_RH_PORTSTAT(0xa);
1871 OK(0);
1872 case RH_PORT_POWER:
1873 OK(0); /* port power ** */
1874 case RH_PORT_ENABLE:
1875 SET_RH_PORTSTAT(USBPORTSC_PE);
1876 OK(0);
1878 break;
1879 case RH_SET_ADDRESS:
1880 uhci->rh.devnum = wValue;
1881 OK(0);
1882 case RH_GET_DESCRIPTOR:
1883 switch ((wValue & 0xff00) >> 8) {
1884 case 0x01: /* device descriptor */
1885 len = min(leni, min(sizeof(root_hub_dev_des), wLength));
1886 memcpy(data, root_hub_dev_des, len);
1887 OK(len);
1888 case 0x02: /* configuration descriptor */
1889 len = min(leni, min(sizeof(root_hub_config_des), wLength));
1890 memcpy (data, root_hub_config_des, len);
1891 OK(len);
1892 case 0x03: /* string descriptors */
1893 len = usb_root_hub_string (wValue & 0xff,
1894 uhci->io_addr, "UHCI-alt",
1895 data, wLength);
1896 if (len > 0) {
1897 OK (min (leni, len));
1898 } else
1899 stat = -EPIPE;
1901 break;
1902 case RH_GET_DESCRIPTOR | RH_CLASS:
1903 root_hub_hub_des[2] = uhci->rh.numports;
1904 len = min(leni, min(sizeof(root_hub_hub_des), wLength));
1905 memcpy(data, root_hub_hub_des, len);
1906 OK(len);
1907 case RH_GET_CONFIGURATION:
1908 *(__u8 *)data = 0x01;
1909 OK(1);
1910 case RH_SET_CONFIGURATION:
1911 OK(0);
1912 case RH_GET_INTERFACE | RH_INTERFACE:
1913 *(__u8 *)data = 0x00;
1914 OK(1);
1915 case RH_SET_INTERFACE | RH_INTERFACE:
1916 OK(0);
1917 default:
1918 stat = -EPIPE;
1921 urb->actual_length = len;
1922 urb->status = stat;
1923 if (urb->complete)
1924 urb->complete(urb);
1926 return USB_ST_NOERROR;
1928 /*-------------------------------------------------------------------------*/
1930 static int rh_unlink_urb(struct urb *urb)
1932 struct uhci *uhci = (struct uhci *)urb->dev->bus->hcpriv;
1934 if (uhci->rh.urb == urb) {
1935 uhci->rh.send = 0;
1936 del_timer(&uhci->rh.rh_int_timer);
1938 return 0;
1940 /*-------------------------------------------------------------------*/
1942 void uhci_free_pending_qhs(struct uhci *uhci)
1944 struct list_head *tmp, *head;
1945 unsigned long flags;
1947 /* Free any pending QH's */
1948 spin_lock_irqsave(&uhci->qh_remove_lock, flags);
1949 head = &uhci->qh_remove_list;
1950 tmp = head->next;
1951 while (tmp != head) {
1952 struct uhci_qh *qh = list_entry(tmp, struct uhci_qh, remove_list);
1954 tmp = tmp->next;
1956 list_del(&qh->remove_list);
1958 uhci_free_qh(qh);
1960 spin_unlock_irqrestore(&uhci->qh_remove_lock, flags);
1963 static void uhci_interrupt(int irq, void *__uhci, struct pt_regs *regs)
1965 struct uhci *uhci = __uhci;
1966 unsigned int io_addr = uhci->io_addr;
1967 unsigned short status;
1968 unsigned long flags;
1969 struct list_head *tmp, *head;
1972 * Read the interrupt status, and write it back to clear the
1973 * interrupt cause
1975 status = inw(io_addr + USBSTS);
1976 if (!status) /* shared interrupt, not mine */
1977 return;
1978 outw(status, io_addr + USBSTS);
1980 if (status & ~(USBSTS_USBINT | USBSTS_ERROR)) {
1981 if (status & USBSTS_RD)
1982 printk(KERN_INFO "uhci: resume detected, not implemented\n");
1983 if (status & USBSTS_HSE)
1984 printk(KERN_ERR "uhci: host system error, PCI problems?\n");
1985 if (status & USBSTS_HCPE)
1986 printk(KERN_ERR "uhci: host controller process error. something bad happened\n");
1987 if (status & USBSTS_HCH) {
1988 printk(KERN_ERR "uhci: host controller halted. very bad\n");
1989 /* FIXME: Reset the controller, fix the offending TD */
1993 uhci_free_pending_qhs(uhci);
1995 spin_lock(&uhci->urb_remove_lock);
1996 head = &uhci->urb_remove_list;
1997 tmp = head->next;
1998 while (tmp != head) {
1999 struct urb *urb = list_entry(tmp, struct urb, urb_list);
2001 tmp = tmp->next;
2003 list_del(&urb->urb_list);
2005 if (urb->complete)
2006 urb->complete(urb);
2008 spin_unlock(&uhci->urb_remove_lock);
2010 uhci_clear_next_interrupt(uhci);
2012 /* Walk the list of pending TD's to see which ones completed */
2013 nested_lock(&uhci->urblist_lock, flags);
2014 head = &uhci->urb_list;
2015 tmp = head->next;
2016 while (tmp != head) {
2017 struct urb *urb = list_entry(tmp, struct urb, urb_list);
2019 tmp = tmp->next;
2021 /* Checks the status and does all of the magic necessary */
2022 uhci_transfer_result(urb);
2024 nested_unlock(&uhci->urblist_lock, flags);
2027 static void reset_hc(struct uhci *uhci)
2029 unsigned int io_addr = uhci->io_addr;
2031 /* Global reset for 50ms */
2032 outw(USBCMD_GRESET, io_addr + USBCMD);
2033 wait_ms(50);
2034 outw(0, io_addr + USBCMD);
2035 wait_ms(10);
2038 static void start_hc(struct uhci *uhci)
2040 unsigned int io_addr = uhci->io_addr;
2041 int timeout = 1000;
2044 * Reset the HC - this will force us to get a
2045 * new notification of any already connected
2046 * ports due to the virtual disconnect that it
2047 * implies.
2049 outw(USBCMD_HCRESET, io_addr + USBCMD);
2050 while (inw(io_addr + USBCMD) & USBCMD_HCRESET) {
2051 if (!--timeout) {
2052 printk(KERN_ERR "uhci: USBCMD_HCRESET timed out!\n");
2053 break;
2057 /* Turn on all interrupts */
2058 outw(USBINTR_TIMEOUT | USBINTR_RESUME | USBINTR_IOC | USBINTR_SP,
2059 io_addr + USBINTR);
2061 /* Start at frame 0 */
2062 outw(0, io_addr + USBFRNUM);
2063 outl(virt_to_bus(uhci->fl), io_addr + USBFLBASEADD);
2065 /* Run and mark it configured with a 64-byte max packet */
2066 outw(USBCMD_RS | USBCMD_CF | USBCMD_MAXP, io_addr + USBCMD);
2070 * Allocate a frame list, and then setup the skeleton
2072 * The hardware doesn't really know any difference
2073 * in the queues, but the order does matter for the
2074 * protocols higher up. The order is:
2076 * - any isochronous events handled before any
2077 * of the queues. We don't do that here, because
2078 * we'll create the actual TD entries on demand.
2079 * - The first queue is the "interrupt queue".
2080 * - The second queue is the "control queue", split into low and high speed
2081 * - The third queue is "bulk data".
2083 static struct uhci *alloc_uhci(unsigned int io_addr, unsigned int io_size)
2085 int i, port;
2086 struct uhci *uhci;
2087 struct usb_bus *bus;
2089 uhci = kmalloc(sizeof(*uhci), GFP_KERNEL);
2090 if (!uhci)
2091 return NULL;
2093 memset(uhci, 0, sizeof(*uhci));
2095 uhci->irq = -1;
2096 uhci->io_addr = io_addr;
2097 uhci->io_size = io_size;
2099 spin_lock_init(&uhci->qh_remove_lock);
2100 INIT_LIST_HEAD(&uhci->qh_remove_list);
2102 spin_lock_init(&uhci->urb_remove_lock);
2103 INIT_LIST_HEAD(&uhci->urb_remove_list);
2105 nested_init(&uhci->urblist_lock);
2106 INIT_LIST_HEAD(&uhci->urb_list);
2108 spin_lock_init(&uhci->framelist_lock);
2110 /* We need exactly one page (per UHCI specs), how convenient */
2111 /* We assume that one page is atleast 4k (1024 frames * 4 bytes) */
2112 uhci->fl = (void *)__get_free_page(GFP_KERNEL);
2113 if (!uhci->fl)
2114 goto au_free_uhci;
2116 bus = usb_alloc_bus(&uhci_device_operations);
2117 if (!bus)
2118 goto au_free_fl;
2120 uhci->bus = bus;
2121 bus->hcpriv = uhci;
2123 /* Initialize the root hub */
2125 /* UHCI specs says devices must have 2 ports, but goes on to say */
2126 /* they may have more but give no way to determine how many they */
2127 /* have. However, according to the UHCI spec, Bit 7 is always set */
2128 /* to 1. So we try to use this to our advantage */
2129 for (port = 0; port < (io_size - 0x10) / 2; port++) {
2130 unsigned int portstatus;
2132 portstatus = inw(io_addr + 0x10 + (port * 2));
2133 if (!(portstatus & 0x0080))
2134 break;
2136 if (debug)
2137 info("detected %d ports", port);
2139 /* This is experimental so anything less than 2 or greater than 8 is */
2140 /* something weird and we'll ignore it */
2141 if (port < 2 || port > 8) {
2142 info("port count misdetected? forcing to 2 ports");
2143 port = 2;
2146 uhci->rh.numports = port;
2149 * 9 Interrupt queues; link int2 to int1, int4 to int2, etc
2150 * then link int1 to control and control to bulk
2152 for (i = 1; i < 9; i++) {
2153 struct uhci_td *td = &uhci->skeltd[i];
2155 uhci_fill_td(td, 0, (UHCI_NULL_DATA_SIZE << 21) | (0x7f << 8) | USB_PID_IN, 0);
2156 td->link = virt_to_bus(&uhci->skeltd[i - 1]);
2160 uhci_fill_td(&uhci->skel_int1_td, 0, (UHCI_NULL_DATA_SIZE << 21) | (0x7f << 8) | USB_PID_IN, 0);
2161 uhci->skel_int1_td.link = virt_to_bus(&uhci->skel_ls_control_qh) | UHCI_PTR_QH;
2163 uhci->skel_ls_control_qh.link = virt_to_bus(&uhci->skel_hs_control_qh) | UHCI_PTR_QH;
2164 uhci->skel_ls_control_qh.element = UHCI_PTR_TERM;
2166 uhci->skel_hs_control_qh.link = virt_to_bus(&uhci->skel_bulk_qh) | UHCI_PTR_QH;
2167 uhci->skel_hs_control_qh.element = UHCI_PTR_TERM;
2169 uhci->skel_bulk_qh.link = virt_to_bus(&uhci->skel_term_qh) | UHCI_PTR_QH;
2170 uhci->skel_bulk_qh.element = UHCI_PTR_TERM;
2172 /* This dummy TD is to work around a bug in Intel PIIX controllers */
2173 uhci_fill_td(&uhci->skel_term_td, 0, (UHCI_NULL_DATA_SIZE << 21) | (0x7f << 8) | USB_PID_IN, 0);
2174 uhci->skel_term_td.link = UHCI_PTR_TERM;
2176 uhci->skel_term_qh.link = UHCI_PTR_TERM;
2177 uhci->skel_term_qh.element = virt_to_bus(&uhci->skel_term_td);
2180 * Fill the frame list: make all entries point to
2181 * the proper interrupt queue.
2183 * This is probably silly, but it's a simple way to
2184 * scatter the interrupt queues in a way that gives
2185 * us a reasonable dynamic range for irq latencies.
2187 for (i = 0; i < 1024; i++) {
2188 struct uhci_td *irq = &uhci->skel_int1_td;
2190 if (i & 1) {
2191 irq++;
2192 if (i & 2) {
2193 irq++;
2194 if (i & 4) {
2195 irq++;
2196 if (i & 8) {
2197 irq++;
2198 if (i & 16) {
2199 irq++;
2200 if (i & 32) {
2201 irq++;
2202 if (i & 64)
2203 irq++;
2211 /* Only place we don't use the frame list routines */
2212 uhci->fl->frame[i] = virt_to_bus(irq);
2215 return uhci;
2218 * error exits:
2220 au_free_fl:
2221 free_page((unsigned long)uhci->fl);
2222 au_free_uhci:
2223 kfree(uhci);
2225 return NULL;
2229 * De-allocate all resources..
2231 static void release_uhci(struct uhci *uhci)
2233 if (uhci->irq >= 0) {
2234 free_irq(uhci->irq, uhci);
2235 uhci->irq = -1;
2238 if (uhci->fl) {
2239 free_page((unsigned long)uhci->fl);
2240 uhci->fl = NULL;
2243 usb_free_bus(uhci->bus);
2244 kfree(uhci);
2247 int uhci_start_root_hub(struct uhci *uhci)
2249 struct usb_device *dev;
2251 dev = usb_alloc_dev(NULL, uhci->bus);
2252 if (!dev)
2253 return -1;
2255 uhci->bus->root_hub = dev;
2256 usb_connect(dev);
2258 if (usb_new_device(dev) != 0) {
2259 usb_free_dev(dev);
2261 return -1;
2264 return 0;
2268 * If we've successfully found a UHCI, now is the time to increment the
2269 * module usage count, and return success..
2271 static int setup_uhci(struct pci_dev *dev, int irq, unsigned int io_addr, unsigned int io_size)
2273 int retval;
2274 struct uhci *uhci;
2275 char buf[8], *bufp = buf;
2277 #ifndef __sparc__
2278 sprintf(buf, "%d", irq);
2279 #else
2280 bufp = __irq_itoa(irq);
2281 #endif
2282 printk(KERN_INFO __FILE__ ": USB UHCI at I/O 0x%x, IRQ %s\n",
2283 io_addr, bufp);
2285 uhci = alloc_uhci(io_addr, io_size);
2286 if (!uhci)
2287 return -ENOMEM;
2289 INIT_LIST_HEAD(&uhci->uhci_list);
2290 list_add(&uhci->uhci_list, &uhci_list);
2292 request_region(uhci->io_addr, io_size, "usb-uhci");
2294 reset_hc(uhci);
2296 usb_register_bus(uhci->bus);
2297 start_hc(uhci);
2299 retval = -EBUSY;
2300 if (request_irq(irq, uhci_interrupt, SA_SHIRQ, "usb-uhci", uhci) == 0) {
2301 uhci->irq = irq;
2303 if (!uhci_start_root_hub(uhci)) {
2304 struct pm_dev *pmdev;
2306 pmdev = pm_register(PM_PCI_DEV,
2307 PM_PCI_ID(dev),
2308 handle_pm_event);
2309 if (pmdev)
2310 pmdev->data = uhci;
2311 return 0;
2315 /* Couldn't allocate IRQ if we got here */
2316 list_del(&uhci->uhci_list);
2317 INIT_LIST_HEAD(&uhci->uhci_list);
2319 reset_hc(uhci);
2320 release_region(uhci->io_addr, uhci->io_size);
2321 release_uhci(uhci);
2323 return retval;
2326 static int found_uhci(struct pci_dev *dev)
2328 int i;
2330 /* disable legacy emulation */
2331 pci_write_config_word(dev, USBLEGSUP, USBLEGSUP_DEFAULT);
2333 if (pci_enable_device(dev) < 0)
2334 return -1;
2336 if (!dev->irq) {
2337 err("found UHCI device with no IRQ assigned. check BIOS settings!");
2338 return -1;
2341 /* Search for the IO base address.. */
2342 for (i = 0; i < 6; i++) {
2343 unsigned int io_addr = pci_resource_start(dev, i);
2344 unsigned int io_size = pci_resource_len(dev, i);
2346 /* IO address? */
2347 if (!(pci_resource_flags(dev, i) & IORESOURCE_IO))
2348 continue;
2350 /* Is it already in use? */
2351 if (check_region(io_addr, io_size))
2352 break;
2354 return setup_uhci(dev, dev->irq, io_addr, io_size);
2357 return -1;
2360 static int handle_pm_event(struct pm_dev *dev, pm_request_t rqst, void *data)
2362 struct uhci *uhci = dev->data;
2363 switch (rqst) {
2364 case PM_SUSPEND:
2365 reset_hc(uhci);
2366 break;
2367 case PM_RESUME:
2368 reset_hc(uhci);
2369 start_hc(uhci);
2370 break;
2372 return 0;
2375 int uhci_init(void)
2377 int retval;
2378 struct pci_dev *dev;
2379 u8 type;
2381 retval = -ENOMEM;
2383 /* We throw all of the TD's and QH's into a kmem cache */
2384 /* TD's and QH's need to be 16 byte aligned and SLAB_HWCACHE_ALIGN */
2385 /* does this for us */
2386 uhci_td_cachep = kmem_cache_create("uhci_td",
2387 sizeof(struct uhci_td), 0,
2388 SLAB_HWCACHE_ALIGN, NULL, NULL);
2390 if (!uhci_td_cachep)
2391 goto td_failed;
2393 uhci_qh_cachep = kmem_cache_create("uhci_qh",
2394 sizeof(struct uhci_qh), 0,
2395 SLAB_HWCACHE_ALIGN, NULL, NULL);
2397 if (!uhci_qh_cachep)
2398 goto qh_failed;
2400 uhci_up_cachep = kmem_cache_create("uhci_urb_priv",
2401 sizeof(struct urb_priv), 0, 0, NULL, NULL);
2403 if (!uhci_up_cachep)
2404 goto up_failed;
2406 retval = -ENODEV;
2407 dev = NULL;
2408 for (;;) {
2409 dev = pci_find_class(PCI_CLASS_SERIAL_USB << 8, dev);
2410 if (!dev)
2411 break;
2413 /* Is it the UHCI programming interface? */
2414 pci_read_config_byte(dev, PCI_CLASS_PROG, &type);
2415 if (type != 0)
2416 continue;
2418 /* Ok set it up */
2419 retval = found_uhci(dev);
2422 /* We only want to return an error code if ther was an error */
2423 /* and we didn't find a UHCI controller */
2424 if (retval && list_empty(&uhci_list))
2425 goto init_failed;
2427 return 0;
2429 init_failed:
2430 if (kmem_cache_destroy(uhci_up_cachep))
2431 printk(KERN_INFO "uhci: not all urb_priv's were freed\n");
2433 up_failed:
2434 if (kmem_cache_destroy(uhci_qh_cachep))
2435 printk(KERN_INFO "uhci: not all QH's were freed\n");
2437 qh_failed:
2438 if (kmem_cache_destroy(uhci_td_cachep))
2439 printk(KERN_INFO "uhci: not all TD's were freed\n");
2441 td_failed:
2442 return retval;
2445 void uhci_cleanup(void)
2447 struct list_head *tmp, *head = &uhci_list;
2449 tmp = head->next;
2450 while (tmp != head) {
2451 struct uhci *uhci = list_entry(tmp, struct uhci, uhci_list);
2453 tmp = tmp->next;
2455 list_del(&uhci->uhci_list);
2456 INIT_LIST_HEAD(&uhci->uhci_list);
2458 if (uhci->bus->root_hub)
2459 usb_disconnect(&uhci->bus->root_hub);
2461 usb_deregister_bus(uhci->bus);
2463 reset_hc(uhci);
2464 release_region(uhci->io_addr, uhci->io_size);
2466 uhci_free_pending_qhs(uhci);
2468 release_uhci(uhci);
2471 if (kmem_cache_destroy(uhci_up_cachep))
2472 printk(KERN_INFO "uhci: not all urb_priv's were freed\n");
2474 if (kmem_cache_destroy(uhci_qh_cachep))
2475 printk(KERN_INFO "uhci: not all QH's were freed\n");
2477 if (kmem_cache_destroy(uhci_td_cachep))
2478 printk(KERN_INFO "uhci: not all TD's were freed\n");
2481 #ifdef MODULE
2482 int init_module(void)
2484 return uhci_init();
2487 void cleanup_module(void)
2489 pm_unregister_all(handle_pm_event);
2490 uhci_cleanup();
2492 #endif //MODULE