2 * OHCI HCD (Host Controller Driver) for USB.
4 * (C) Copyright 1999 Roman Weissgaerber <weissg@vienna.at>
5 * (C) Copyright 2000-2002 David Brownell <dbrownell@users.sourceforge.net>
7 * This file is licenced under the GPL.
10 #include <linux/irq.h>
12 /*-------------------------------------------------------------------------*/
15 * URB goes back to driver, and isn't reissued.
16 * It's completely gone from HC data structures.
17 * PRECONDITION: ahcd lock held, irqs blocked.
20 finish_urb(struct admhcd *ahcd, struct urb *urb)
21 __releases(ahcd->lock)
22 __acquires(ahcd->lock)
24 urb_priv_free(ahcd, urb->hcpriv);
27 spin_lock(&urb->lock);
28 if (likely(urb->status == -EINPROGRESS))
31 /* report short control reads right even though the data TD always
32 * has TD_R set. (much simpler, but creates the 1-td limit.)
34 if (unlikely(urb->transfer_flags & URB_SHORT_NOT_OK)
35 && unlikely(usb_pipecontrol(urb->pipe))
36 && urb->actual_length < urb->transfer_buffer_length
37 && usb_pipein(urb->pipe)
38 && urb->status == 0) {
39 urb->status = -EREMOTEIO;
40 #ifdef ADMHC_VERBOSE_DEBUG
41 urb_print(ahcd, urb, "SHORT", usb_pipeout(urb->pipe));
44 spin_unlock(&urb->lock);
46 switch (usb_pipetype(urb->pipe)) {
47 case PIPE_ISOCHRONOUS:
48 admhcd_to_hcd(ahcd)->self.bandwidth_isoc_reqs--;
51 admhcd_to_hcd(ahcd)->self.bandwidth_int_reqs--;
55 #ifdef ADMHC_VERBOSE_DEBUG
56 urb_print(ahcd, urb, "RET", usb_pipeout (urb->pipe));
59 /* urb->complete() can reenter this HCD */
60 spin_unlock(&ahcd->lock);
61 usb_hcd_giveback_urb(admhcd_to_hcd(ahcd), urb);
62 spin_lock(&ahcd->lock);
66 /*-------------------------------------------------------------------------*
67 * ED handling functions
68 *-------------------------------------------------------------------------*/
71 /* search for the right schedule branch to use for a periodic ed.
72 * does some load balancing; returns the branch, or negative errno.
74 static int balance(struct admhcd *ahcd, int interval, int load)
76 int i, branch = -ENOSPC;
78 /* iso periods can be huge; iso tds specify frame numbers */
79 if (interval > NUM_INTS)
82 /* search for the least loaded schedule branch of that period
83 * that has enough bandwidth left unreserved.
85 for (i = 0; i < interval ; i++) {
86 if (branch < 0 || ahcd->load [branch] > ahcd->load [i]) {
89 /* usb 1.1 says 90% of one frame */
90 for (j = i; j < NUM_INTS; j += interval) {
91 if ((ahcd->load [j] + load) > 900)
103 /*-------------------------------------------------------------------------*/
106 /* both iso and interrupt requests have periods; this routine puts them
107 * into the schedule tree in the apppropriate place. most iso devices use
108 * 1msec periods, but that's not required.
110 static void periodic_link (struct admhcd *ahcd, struct ed *ed)
114 admhc_vdbg (ahcd, "link %sed %p branch %d [%dus.], interval %d\n",
115 (ed->hwINFO & cpu_to_hc32(ahcd, ED_ISO)) ? "iso " : "",
116 ed, ed->branch, ed->load, ed->interval);
118 for (i = ed->branch; i < NUM_INTS; i += ed->interval) {
119 struct ed **prev = &ahcd->periodic [i];
120 __hc32 *prev_p = &ahcd->hcca->int_table [i];
121 struct ed *here = *prev;
123 /* sorting each branch by period (slow before fast)
124 * lets us share the faster parts of the tree.
125 * (plus maybe: put interrupt eds before iso)
127 while (here && ed != here) {
128 if (ed->interval > here->interval)
130 prev = &here->ed_next;
131 prev_p = &here->hwNextED;
137 ed->hwNextED = *prev_p;
140 *prev_p = cpu_to_hc32(ahcd, ed->dma);
143 ahcd->load [i] += ed->load;
145 admhcd_to_hcd(ahcd)->self.bandwidth_allocated += ed->load / ed->interval;
149 /* link an ed into the HC chain */
151 static int ed_schedule(struct admhcd *ahcd, struct ed *ed)
155 if (admhcd_to_hcd(ahcd)->state == HC_STATE_QUIESCING)
160 old_tail = ahcd->ed_tails[ed->type];
162 ed->ed_next = old_tail->ed_next;
164 ed->ed_next->ed_prev = ed;
165 ed->hwNextED = cpu_to_hc32(ahcd, ed->ed_next->dma);
167 ed->ed_prev = old_tail;
169 old_tail->ed_next = ed;
170 old_tail->hwNextED = cpu_to_hc32(ahcd, ed->dma);
172 ahcd->ed_tails[ed->type] = ed;
174 admhc_dma_enable(ahcd);
179 /*-------------------------------------------------------------------------*/
182 /* scan the periodic table to find and unlink this ED */
183 static void periodic_unlink (struct admhcd *ahcd, struct ed *ed)
187 for (i = ed->branch; i < NUM_INTS; i += ed->interval) {
189 struct ed **prev = &ahcd->periodic [i];
190 __hc32 *prev_p = &ahcd->hcca->int_table [i];
192 while (*prev && (temp = *prev) != ed) {
193 prev_p = &temp->hwNextED;
194 prev = &temp->ed_next;
197 *prev_p = ed->hwNextED;
200 ahcd->load [i] -= ed->load;
203 admhcd_to_hcd(ahcd)->self.bandwidth_allocated -= ed->load / ed->interval;
204 admhc_vdbg (ahcd, "unlink %sed %p branch %d [%dus.], interval %d\n",
205 (ed->hwINFO & cpu_to_hc32(ahcd, ED_ISO)) ? "iso " : "",
206 ed, ed->branch, ed->load, ed->interval);
210 /* unlink an ed from the HC chain.
211 * just the link to the ed is unlinked.
212 * the link from the ed still points to another operational ed or 0
213 * so the HC can eventually finish the processing of the unlinked ed
214 * (assuming it already started that, which needn't be true).
216 * ED_UNLINK is a transient state: the HC may still see this ED, but soon
217 * it won't. ED_SKIP means the HC will finish its current transaction,
218 * but won't start anything new. The TD queue may still grow; device
219 * drivers don't know about this HCD-internal state.
221 * When the HC can't see the ED, something changes ED_UNLINK to one of:
223 * - ED_OPER: when there's any request queued, the ED gets rescheduled
224 * immediately. HC should be working on them.
226 * - ED_IDLE: when there's no TD queue. there's no reason for the HC
227 * to care about this ED; safe to disable the endpoint.
229 * When finish_unlinks() runs later, after SOF interrupt, it will often
230 * complete one or more URB unlinks before making that state change.
232 static void ed_deschedule(struct admhcd *ahcd, struct ed *ed)
235 #ifdef ADMHC_VERBOSE_DEBUG
236 admhc_dump_ed(ahcd, "ED-DESCHED", ed, 1);
239 ed->hwINFO |= cpu_to_hc32(ahcd, ED_SKIP);
241 ed->state = ED_UNLINK;
243 /* remove this ED from the HC list */
244 ed->ed_prev->hwNextED = ed->hwNextED;
246 /* and remove it from our list also */
247 ed->ed_prev->ed_next = ed->ed_next;
250 ed->ed_next->ed_prev = ed->ed_prev;
252 if (ahcd->ed_tails[ed->type] == ed)
253 ahcd->ed_tails[ed->type] = ed->ed_prev;
256 /*-------------------------------------------------------------------------*/
258 static struct ed *ed_create(struct admhcd *ahcd, unsigned int type, u32 info)
263 ed = ed_alloc(ahcd, GFP_ATOMIC);
267 /* dummy td; end of td list for this ed */
268 td = td_alloc(ahcd, GFP_ATOMIC);
276 case PIPE_ISOCHRONOUS:
285 ed->hwINFO = cpu_to_hc32(ahcd, info);
286 ed->hwTailP = cpu_to_hc32(ahcd, td->td_dma);
287 ed->hwHeadP = ed->hwTailP; /* ED_C, ED_H zeroed */
297 /* get and maybe (re)init an endpoint. init _should_ be done only as part
298 * of enumeration, usb_set_configuration() or usb_set_interface().
300 static struct ed *ed_get(struct admhcd *ahcd, struct usb_host_endpoint *ep,
301 struct usb_device *udev, unsigned int pipe, int interval)
306 spin_lock_irqsave(&ahcd->lock, flags);
312 /* FIXME: usbcore changes dev->devnum before SET_ADDRESS
313 * suceeds ... otherwise we wouldn't need "pipe".
315 info = usb_pipedevice(pipe);
316 info |= (ep->desc.bEndpointAddress & ~USB_DIR_IN) << ED_EN_SHIFT;
317 info |= le16_to_cpu(ep->desc.wMaxPacketSize) << ED_MPS_SHIFT;
318 if (udev->speed == USB_SPEED_FULL)
319 info |= ED_SPEED_FULL;
321 ed = ed_create(ahcd, usb_pipetype(pipe), info);
326 spin_unlock_irqrestore(&ahcd->lock, flags);
331 /*-------------------------------------------------------------------------*/
333 /* request unlinking of an endpoint from an operational HC.
334 * put the ep on the rm_list
335 * real work is done at the next start frame (SOFI) hardware interrupt
336 * caller guarantees HCD is running, so hardware access is safe,
337 * and that ed->state is ED_OPER
339 static void start_ed_unlink(struct admhcd *ahcd, struct ed *ed)
342 #ifdef ADMHC_VERBOSE_DEBUG
343 admhc_dump_ed(ahcd, "ED-UNLINK", ed, 1);
346 ed->hwINFO |= cpu_to_hc32(ahcd, ED_DEQUEUE);
347 ed_deschedule(ahcd, ed);
349 /* add this ED into the remove list */
350 ed->ed_rm_next = ahcd->ed_rm_list;
351 ahcd->ed_rm_list = ed;
353 /* enable SOF interrupt */
354 admhc_intr_ack(ahcd, ADMHC_INTR_SOFI);
355 admhc_intr_enable(ahcd, ADMHC_INTR_SOFI);
356 /* flush those writes */
357 admhc_writel_flush(ahcd);
359 /* SOF interrupt might get delayed; record the frame counter value that
360 * indicates when the HC isn't looking at it, so concurrent unlinks
361 * behave. frame_no wraps every 2^16 msec, and changes right before
364 ed->tick = admhc_frame_no(ahcd) + 1;
367 /*-------------------------------------------------------------------------*
368 * TD handling functions
369 *-------------------------------------------------------------------------*/
371 /* enqueue next TD for this URB (OHCI spec 5.2.8.2) */
374 td_fill(struct admhcd *ahcd, u32 info, dma_addr_t data, int len,
375 struct urb *urb, int index)
377 struct td *td, *td_pt;
378 struct urb_priv *urb_priv = urb->hcpriv;
383 if (index == (urb_priv->td_cnt - 1) &&
384 ((urb->transfer_flags & URB_NO_INTERRUPT) == 0))
387 if (index == (urb_priv->td_cnt - 1))
391 /* use this td as the next dummy */
392 td_pt = urb_priv->td[index];
394 /* fill the old dummy TD */
395 td = urb_priv->td[index] = urb_priv->ed->dummy;
396 urb_priv->ed->dummy = td_pt;
398 td->ed = urb_priv->ed;
399 td->next_dl_td = NULL;
407 cbl |= (len & TD_BL_MASK);
411 /* setup hardware specific fields */
412 td->hwINFO = cpu_to_hc32(ahcd, info);
413 td->hwDBP = cpu_to_hc32(ahcd, data);
414 td->hwCBL = cpu_to_hc32(ahcd, cbl);
415 td->hwNextTD = cpu_to_hc32(ahcd, td_pt->td_dma);
417 /* append to queue */
418 list_add_tail(&td->td_list, &td->ed->td_list);
420 /* hash it for later reverse mapping */
421 hash = TD_HASH_FUNC(td->td_dma);
422 td->td_hash = ahcd->td_hash[hash];
423 ahcd->td_hash[hash] = td;
425 /* HC might read the TD (or cachelines) right away ... */
427 td->ed->hwTailP = td->hwNextTD;
430 /*-------------------------------------------------------------------------*/
432 /* Prepare all TDs of a transfer, and queue them onto the ED.
433 * Caller guarantees HC is active.
434 * Usually the ED is already on the schedule, so TDs might be
435 * processed as soon as they're queued.
437 static void td_submit_urb(struct admhcd *ahcd, struct urb *urb)
439 struct urb_priv *urb_priv = urb->hcpriv;
441 int data_len = urb->transfer_buffer_length;
444 int is_out = usb_pipeout(urb->pipe);
447 /* OHCI handles the bulk/interrupt data toggles itself. We just
448 * use the device toggle bits for resetting, and rely on the fact
449 * that resetting toggle is meaningless if the endpoint is active.
452 if (usb_gettoggle(urb->dev, usb_pipeendpoint(urb->pipe), is_out)) {
456 usb_settoggle(urb->dev, usb_pipeendpoint (urb->pipe),
460 urb_priv->td_idx = 0;
461 list_add(&urb_priv->pending, &ahcd->pending);
464 data = urb->transfer_dma;
468 /* NOTE: TD_CC is set so we can tell which TDs the HC processed by
469 * using TD_CC_GET, as well as by seeing them on the done list.
470 * (CC = NotAccessed ... 0x0F, or 0x0E in PSWs for ISO.)
472 switch (urb_priv->ed->type) {
475 ? TD_T_CARRY | TD_SCC_NOTACCESSED | TD_DP_OUT
476 : TD_T_CARRY | TD_SCC_NOTACCESSED | TD_DP_IN;
478 /* setup service interval and starting frame number */
479 info |= (urb->start_frame & TD_FN_MASK);
480 info |= (urb->interval & TD_ISI_MASK) << TD_ISI_SHIFT;
482 td_fill(ahcd, info, data, data_len, urb, cnt);
485 admhcd_to_hcd(ahcd)->self.bandwidth_int_reqs++;
490 ? TD_SCC_NOTACCESSED | TD_DP_OUT
491 : TD_SCC_NOTACCESSED | TD_DP_IN;
493 /* TDs _could_ transfer up to 8K each */
494 while (data_len > TD_DATALEN_MAX) {
495 td_fill(ahcd, info | ((cnt) ? TD_T_CARRY : toggle),
496 data, TD_DATALEN_MAX, urb, cnt);
497 data += TD_DATALEN_MAX;
498 data_len -= TD_DATALEN_MAX;
502 td_fill(ahcd, info | ((cnt) ? TD_T_CARRY : toggle), data,
506 if ((urb->transfer_flags & URB_ZERO_PACKET)
507 && (cnt < urb_priv->td_cnt)) {
508 td_fill(ahcd, info | ((cnt) ? TD_T_CARRY : toggle),
514 /* control manages DATA0/DATA1 toggle per-request; SETUP resets it,
515 * any DATA phase works normally, and the STATUS ack is special.
518 /* fill a TD for the setup */
519 info = TD_SCC_NOTACCESSED | TD_DP_SETUP | TD_T_DATA0;
520 td_fill(ahcd, info, urb->setup_dma, 8, urb, cnt++);
523 /* fill a TD for the data */
524 info = TD_SCC_NOTACCESSED | TD_T_DATA1;
525 info |= is_out ? TD_DP_OUT : TD_DP_IN;
526 /* NOTE: mishandles transfers >8K, some >4K */
527 td_fill(ahcd, info, data, data_len, urb, cnt++);
530 /* fill a TD for the ACK */
531 info = (is_out || data_len == 0)
532 ? TD_SCC_NOTACCESSED | TD_DP_IN | TD_T_DATA1
533 : TD_SCC_NOTACCESSED | TD_DP_OUT | TD_T_DATA1;
534 td_fill(ahcd, info, data, 0, urb, cnt++);
538 /* ISO has no retransmit, so no toggle;
539 * Each TD could handle multiple consecutive frames (interval 1);
540 * we could often reduce the number of TDs here.
542 case PIPE_ISOCHRONOUS:
543 info = TD_SCC_NOTACCESSED;
544 for (cnt = 0; cnt < urb->number_of_packets; cnt++) {
545 int frame = urb->start_frame;
547 frame += cnt * urb->interval;
549 td_fill(ahcd, info | frame,
550 data + urb->iso_frame_desc[cnt].offset,
551 urb->iso_frame_desc[cnt].length, urb, cnt);
553 admhcd_to_hcd(ahcd)->self.bandwidth_isoc_reqs++;
557 if (urb_priv->td_cnt != cnt)
558 admhc_err(ahcd, "bad number of tds created for urb %p\n", urb);
561 /*-------------------------------------------------------------------------*
562 * Done List handling functions
563 *-------------------------------------------------------------------------*/
565 /* calculate transfer length/status and update the urb
566 * PRECONDITION: irqsafe (only for urb->status locking)
568 static int td_done(struct admhcd *ahcd, struct urb *urb, struct td *td)
570 struct urb_priv *urb_priv = urb->hcpriv;
571 u32 info = hc32_to_cpup(ahcd, &td->hwINFO);
572 int type = usb_pipetype(urb->pipe);
575 cc = TD_CC_GET(info);
577 /* ISO ... drivers see per-TD length/status */
578 if (type == PIPE_ISOCHRONOUS) {
583 /* NOTE: assumes FC in tdINFO == 0, and that
584 * only the first of 0..MAXPSW psws is used.
588 if (tdINFO & TD_CC) /* hc didn't touch? */
591 if (usb_pipeout (urb->pipe))
592 dlen = urb->iso_frame_desc [td->index].length;
594 /* short reads are always OK for ISO */
595 if (cc == TD_DATAUNDERRUN)
597 dlen = tdPSW & 0x3ff;
599 urb->actual_length += dlen;
600 urb->iso_frame_desc [td->index].actual_length = dlen;
601 urb->iso_frame_desc [td->index].status = cc_to_error [cc];
603 if (cc != TD_CC_NOERROR)
605 "urb %p iso td %p (%d) len %d cc %d\n",
606 urb, td, 1 + td->index, dlen, cc);
608 /* BULK, INT, CONTROL ... drivers see aggregate length/status,
609 * except that "setup" bytes aren't counted and "short" transfers
610 * might not be reported as errors.
613 u32 bl = TD_BL_GET(hc32_to_cpup(ahcd, &td->hwCBL));
614 u32 tdDBP = hc32_to_cpup(ahcd, &td->hwDBP);
616 /* update packet status if needed (short is normally ok) */
617 if (cc == TD_CC_DATAUNDERRUN
618 && !(urb->transfer_flags & URB_SHORT_NOT_OK))
621 if (cc != TD_CC_NOERROR && cc < TD_CC_HCD0) {
622 spin_lock(&urb->lock);
623 if (urb->status == -EINPROGRESS)
624 urb->status = cc_to_error[cc];
625 spin_unlock(&urb->lock);
628 /* count all non-empty packets except control SETUP packet */
629 if ((type != PIPE_CONTROL || td->index != 0) && tdDBP != 0) {
630 urb->actual_length += tdDBP - td->data_dma + bl;
633 if (cc != TD_CC_NOERROR && cc < TD_CC_HCD0)
635 "urb %p td %p (%d) cc %d, len=%d/%d\n",
636 urb, td, td->index, cc,
638 urb->transfer_buffer_length);
641 list_del(&td->td_list);
647 /*-------------------------------------------------------------------------*/
649 static inline struct td *
650 ed_halted(struct admhcd *ahcd, struct td *td, int cc, struct td *rev)
652 struct urb *urb = td->urb;
653 struct ed *ed = td->ed;
654 struct list_head *tmp = td->td_list.next;
655 __hc32 toggle = ed->hwHeadP & cpu_to_hc32(ahcd, ED_C);
657 admhc_dump_ed(ahcd, "ed halted", td->ed, 1);
658 /* clear ed halt; this is the td that caused it, but keep it inactive
659 * until its urb->complete() has a chance to clean up.
661 ed->hwINFO |= cpu_to_hc32(ahcd, ED_SKIP);
663 ed->hwHeadP &= ~cpu_to_hc32(ahcd, ED_H);
665 /* put any later tds from this urb onto the donelist, after 'td',
666 * order won't matter here: no errors, and nothing was transferred.
667 * also patch the ed so it looks as if those tds completed normally.
669 while (tmp != &ed->td_list) {
673 next = list_entry(tmp, struct td, td_list);
674 tmp = next->td_list.next;
676 if (next->urb != urb)
679 /* NOTE: if multi-td control DATA segments get supported,
680 * this urb had one of them, this td wasn't the last td
681 * in that segment (TD_R clear), this ed halted because
682 * of a short read, _and_ URB_SHORT_NOT_OK is clear ...
683 * then we need to leave the control STATUS packet queued
688 info |= cpu_to_hc32(ahcd, TD_DONE);
690 info &= ~cpu_to_hc32(ahcd, TD_CC);
693 next->next_dl_td = rev;
696 ed->hwHeadP = next->hwNextTD | toggle;
699 /* help for troubleshooting: report anything that
700 * looks odd ... that doesn't include protocol stalls
701 * (or maybe some other things)
704 case TD_CC_DATAUNDERRUN:
705 if ((urb->transfer_flags & URB_SHORT_NOT_OK) == 0)
709 if (usb_pipecontrol(urb->pipe))
714 "urb %p path %s ep%d%s %08x cc %d --> status %d\n",
715 urb, urb->dev->devpath,
716 usb_pipeendpoint (urb->pipe),
717 usb_pipein (urb->pipe) ? "in" : "out",
718 hc32_to_cpu(ahcd, td->hwINFO),
719 cc, cc_to_error [cc]);
725 /*-------------------------------------------------------------------------*/
727 /* there are some urbs/eds to unlink; called in_irq(), with HCD locked */
729 finish_unlinks(struct admhcd *ahcd, u16 tick)
731 struct ed *ed, **last;
734 for (last = &ahcd->ed_rm_list, ed = *last; ed != NULL; ed = *last) {
735 struct list_head *entry, *tmp;
736 int completed, modified;
739 /* only take off EDs that the HC isn't using, accounting for
740 * frame counter wraps and EDs with partially retired TDs
742 if (likely(HC_IS_RUNNING(admhcd_to_hcd(ahcd)->state))) {
743 if (tick_before (tick, ed->tick)) {
745 last = &ed->ed_rm_next;
749 if (!list_empty(&ed->td_list)) {
753 td = list_entry(ed->td_list.next, struct td,
755 head = hc32_to_cpu(ahcd, ed->hwHeadP) &
758 /* INTR_WDH may need to clean up first */
759 if (td->td_dma != head)
765 /* reentrancy: if we drop the schedule lock, someone might
766 * have modified this list. normally it's just prepending
767 * entries (which we'd ignore), but paranoia won't hurt.
769 *last = ed->ed_rm_next;
770 ed->ed_rm_next = NULL;
773 /* unlink urbs as requested, but rescan the list after
774 * we call a completion since it might have unlinked
775 * another (earlier) urb
777 * When we get here, the HC doesn't see this ed. But it
778 * must not be rescheduled until all completed URBs have
779 * been given back to the driver.
784 list_for_each_safe(entry, tmp, &ed->td_list) {
787 struct urb_priv *urb_priv;
790 td = list_entry(entry, struct td, td_list);
792 urb_priv = td->urb->hcpriv;
794 if (urb->status == -EINPROGRESS) {
795 prev = &td->hwNextTD;
799 if ((urb_priv) == NULL)
802 /* patch pointer hc uses */
803 savebits = *prev & ~cpu_to_hc32(ahcd, TD_MASK);
804 *prev = td->hwNextTD | savebits;
806 /* HC may have partly processed this TD */
807 #ifdef ADMHC_VERBOSE_DEBUG
808 urb_print(ahcd, urb, "PARTIAL", 0);
810 td_done(ahcd, urb, td);
812 /* if URB is done, clean up */
813 if (urb_priv->td_idx == urb_priv->td_cnt) {
814 modified = completed = 1;
815 finish_urb(ahcd, urb);
818 if (completed && !list_empty(&ed->td_list))
821 /* ED's now officially unlinked, hc doesn't see */
823 ed->hwHeadP &= ~cpu_to_hc32(ahcd, ED_H);
826 ed->hwINFO &= ~cpu_to_hc32(ahcd, ED_SKIP | ED_DEQUEUE);
828 /* but if there's work queued, reschedule */
829 if (!list_empty(&ed->td_list)) {
830 if (HC_IS_RUNNING(admhcd_to_hcd(ahcd)->state))
831 ed_schedule(ahcd, ed);
839 /*-------------------------------------------------------------------------*/
842 * Process normal completions (error or success) and clean the schedules.
844 * This is the main path for handing urbs back to drivers. The only other
845 * path is finish_unlinks(), which unlinks URBs using ed_rm_list, instead of
846 * scanning the (re-reversed) donelist as this does.
849 static void ed_unhalt(struct admhcd *ahcd, struct ed *ed, struct urb *urb)
851 struct list_head *entry,*tmp;
852 __hc32 toggle = ed->hwHeadP & cpu_to_hc32(ahcd, ED_C);
854 #ifdef ADMHC_VERBOSE_DEBUG
855 admhc_dump_ed(ahcd, "UNHALT", ed, 0);
857 /* clear ed halt; this is the td that caused it, but keep it inactive
858 * until its urb->complete() has a chance to clean up.
860 ed->hwINFO |= cpu_to_hc32(ahcd, ED_SKIP);
862 ed->hwHeadP &= ~cpu_to_hc32(ahcd, ED_H);
864 list_for_each_safe(entry, tmp, &ed->td_list) {
865 struct td *td = list_entry(entry, struct td, td_list);
872 info &= ~cpu_to_hc32(ahcd, TD_CC | TD_OWN);
875 ed->hwHeadP = td->hwNextTD | toggle;
881 static void ed_intr_refill(struct admhcd *ahcd, struct ed *ed)
883 __hc32 toggle = ed->hwHeadP & cpu_to_hc32(ahcd, ED_C);
885 ed->hwHeadP = ed->hwTailP | toggle;
889 static inline int is_ed_halted(struct admhcd *ahcd, struct ed *ed)
891 return ((hc32_to_cpup(ahcd, &ed->hwHeadP) & ED_H) == ED_H);
894 static inline int is_td_halted(struct admhcd *ahcd, struct ed *ed,
897 return ((hc32_to_cpup(ahcd, &ed->hwHeadP) & TD_MASK) ==
898 (hc32_to_cpup(ahcd, &td->hwNextTD) & TD_MASK));
901 static void ed_update(struct admhcd *ahcd, struct ed *ed)
903 struct list_head *entry,*tmp;
905 #ifdef ADMHC_VERBOSE_DEBUG
906 admhc_dump_ed(ahcd, "UPDATE", ed, 1);
909 list_for_each_safe(entry, tmp, &ed->td_list) {
910 struct td *td = list_entry(entry, struct td, td_list);
911 struct urb *urb = td->urb;
912 struct urb_priv *urb_priv = urb->hcpriv;
915 if (hc32_to_cpup(ahcd, &td->hwINFO) & TD_OWN)
918 /* update URB's length and status from TD */
919 cc = td_done(ahcd, urb, td);
920 if (is_ed_halted(ahcd, ed) && is_td_halted(ahcd, ed, td))
921 ed_unhalt(ahcd, ed, urb);
923 if (ed->type == PIPE_INTERRUPT)
924 ed_intr_refill(ahcd,ed);
926 /* If all this urb's TDs are done, call complete() */
927 if (urb_priv->td_idx == urb_priv->td_cnt)
928 finish_urb(ahcd, urb);
930 /* clean schedule: unlink EDs that are no longer busy */
931 if (list_empty(&ed->td_list)) {
932 if (ed->state == ED_OPER)
933 start_ed_unlink(ahcd, ed);
935 /* ... reenabling halted EDs only after fault cleanup */
936 } else if ((ed->hwINFO & cpu_to_hc32(ahcd,
937 ED_SKIP | ED_DEQUEUE))
938 == cpu_to_hc32(ahcd, ED_SKIP)) {
939 td = list_entry(ed->td_list.next, struct td, td_list);
941 if (!(td->hwINFO & cpu_to_hc32(ahcd, TD_DONE))) {
942 ed->hwINFO &= ~cpu_to_hc32(ahcd, ED_SKIP);
943 /* ... hc may need waking-up */
946 admhc_writel (ahcd, OHCI_CLF,
947 &ahcd->regs->cmdstatus);
950 admhc_writel (ahcd, OHCI_BLF,
951 &ahcd->regs->cmdstatus);
956 if ((td->hwINFO & cpu_to_hc32(ahcd, TD_OWN)))
957 ed->hwINFO &= ~cpu_to_hc32(ahcd, ED_SKIP);
964 /* there are some tds completed; called in_irq(), with HCD locked */
965 static void admhc_td_complete(struct admhcd *ahcd)
969 for (ed = ahcd->ed_head; ed; ed = ed->ed_next) {
970 if (ed->state != ED_OPER)