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 static void urb_free_priv (struct ohci_hcd *hc, urb_priv_t *urb_priv)
12 int last = urb_priv->length - 1;
18 for (i = 0; i <= last; i++) {
19 td = urb_priv->td [i];
25 list_del (&urb_priv->pending);
29 /*-------------------------------------------------------------------------*/
32 * URB goes back to driver, and isn't reissued.
33 * It's completely gone from HC data structures.
34 * PRECONDITION: ohci lock held, irqs blocked.
37 finish_urb (struct ohci_hcd *ohci, struct urb *urb, struct pt_regs *regs)
38 __releases(ohci->lock)
39 __acquires(ohci->lock)
41 // ASSERT (urb->hcpriv != 0);
43 urb_free_priv (ohci, urb->hcpriv);
46 spin_lock (&urb->lock);
47 if (likely (urb->status == -EINPROGRESS))
49 /* report short control reads right even though the data TD always
50 * has TD_R set. (much simpler, but creates the 1-td limit.)
52 if (unlikely (urb->transfer_flags & URB_SHORT_NOT_OK)
53 && unlikely (usb_pipecontrol (urb->pipe))
54 && urb->actual_length < urb->transfer_buffer_length
55 && usb_pipein (urb->pipe)
56 && urb->status == 0) {
57 urb->status = -EREMOTEIO;
59 spin_unlock (&urb->lock);
61 switch (usb_pipetype (urb->pipe)) {
62 case PIPE_ISOCHRONOUS:
63 ohci_to_hcd(ohci)->self.bandwidth_isoc_reqs--;
66 ohci_to_hcd(ohci)->self.bandwidth_int_reqs--;
70 #ifdef OHCI_VERBOSE_DEBUG
71 urb_print (urb, "RET", usb_pipeout (urb->pipe));
74 /* urb->complete() can reenter this HCD */
75 spin_unlock (&ohci->lock);
76 usb_hcd_giveback_urb (ohci_to_hcd(ohci), urb, regs);
77 spin_lock (&ohci->lock);
79 /* stop periodic dma if it's not needed */
80 if (ohci_to_hcd(ohci)->self.bandwidth_isoc_reqs == 0
81 && ohci_to_hcd(ohci)->self.bandwidth_int_reqs == 0) {
82 ohci->hc_control &= ~(OHCI_CTRL_PLE|OHCI_CTRL_IE);
83 ohci_writel (ohci, ohci->hc_control, &ohci->regs->control);
88 /*-------------------------------------------------------------------------*
89 * ED handling functions
90 *-------------------------------------------------------------------------*/
92 /* search for the right schedule branch to use for a periodic ed.
93 * does some load balancing; returns the branch, or negative errno.
95 static int balance (struct ohci_hcd *ohci, int interval, int load)
97 int i, branch = -ENOSPC;
99 /* iso periods can be huge; iso tds specify frame numbers */
100 if (interval > NUM_INTS)
103 /* search for the least loaded schedule branch of that period
104 * that has enough bandwidth left unreserved.
106 for (i = 0; i < interval ; i++) {
107 if (branch < 0 || ohci->load [branch] > ohci->load [i]) {
108 #if 1 /* CONFIG_USB_BANDWIDTH */
111 /* usb 1.1 says 90% of one frame */
112 for (j = i; j < NUM_INTS; j += interval) {
113 if ((ohci->load [j] + load) > 900)
125 /*-------------------------------------------------------------------------*/
127 /* both iso and interrupt requests have periods; this routine puts them
128 * into the schedule tree in the apppropriate place. most iso devices use
129 * 1msec periods, but that's not required.
131 static void periodic_link (struct ohci_hcd *ohci, struct ed *ed)
135 ohci_vdbg (ohci, "link %sed %p branch %d [%dus.], interval %d\n",
136 (ed->hwINFO & cpu_to_hc32 (ohci, ED_ISO)) ? "iso " : "",
137 ed, ed->branch, ed->load, ed->interval);
139 for (i = ed->branch; i < NUM_INTS; i += ed->interval) {
140 struct ed **prev = &ohci->periodic [i];
141 __hc32 *prev_p = &ohci->hcca->int_table [i];
142 struct ed *here = *prev;
144 /* sorting each branch by period (slow before fast)
145 * lets us share the faster parts of the tree.
146 * (plus maybe: put interrupt eds before iso)
148 while (here && ed != here) {
149 if (ed->interval > here->interval)
151 prev = &here->ed_next;
152 prev_p = &here->hwNextED;
158 ed->hwNextED = *prev_p;
161 *prev_p = cpu_to_hc32(ohci, ed->dma);
164 ohci->load [i] += ed->load;
166 ohci_to_hcd(ohci)->self.bandwidth_allocated += ed->load / ed->interval;
169 /* link an ed into one of the HC chains */
171 static int ed_schedule (struct ohci_hcd *ohci, struct ed *ed)
175 if (ohci_to_hcd(ohci)->state == USB_STATE_QUIESCING)
184 /* we care about rm_list when setting CLE/BLE in case the HC was at
185 * work on some TD when CLE/BLE was turned off, and isn't quiesced
186 * yet. finish_unlinks() restarts as needed, some upcoming INTR_SF.
188 * control and bulk EDs are doubly linked (ed_next, ed_prev), but
189 * periodic ones are singly linked (ed_next). that's because the
190 * periodic schedule encodes a tree like figure 3-5 in the ohci
191 * spec: each qh can have several "previous" nodes, and the tree
192 * doesn't have unused/idle descriptors.
196 if (ohci->ed_controltail == NULL) {
197 WARN_ON (ohci->hc_control & OHCI_CTRL_CLE);
198 ohci_writel (ohci, ed->dma,
199 &ohci->regs->ed_controlhead);
201 ohci->ed_controltail->ed_next = ed;
202 ohci->ed_controltail->hwNextED = cpu_to_hc32 (ohci,
205 ed->ed_prev = ohci->ed_controltail;
206 if (!ohci->ed_controltail && !ohci->ed_rm_list) {
208 ohci->hc_control |= OHCI_CTRL_CLE;
209 ohci_writel (ohci, 0, &ohci->regs->ed_controlcurrent);
210 ohci_writel (ohci, ohci->hc_control,
211 &ohci->regs->control);
213 ohci->ed_controltail = ed;
217 if (ohci->ed_bulktail == NULL) {
218 WARN_ON (ohci->hc_control & OHCI_CTRL_BLE);
219 ohci_writel (ohci, ed->dma, &ohci->regs->ed_bulkhead);
221 ohci->ed_bulktail->ed_next = ed;
222 ohci->ed_bulktail->hwNextED = cpu_to_hc32 (ohci,
225 ed->ed_prev = ohci->ed_bulktail;
226 if (!ohci->ed_bulktail && !ohci->ed_rm_list) {
228 ohci->hc_control |= OHCI_CTRL_BLE;
229 ohci_writel (ohci, 0, &ohci->regs->ed_bulkcurrent);
230 ohci_writel (ohci, ohci->hc_control,
231 &ohci->regs->control);
233 ohci->ed_bulktail = ed;
236 // case PIPE_INTERRUPT:
237 // case PIPE_ISOCHRONOUS:
239 branch = balance (ohci, ed->interval, ed->load);
242 "ERR %d, interval %d msecs, load %d\n",
243 branch, ed->interval, ed->load);
244 // FIXME if there are TDs queued, fail them!
248 periodic_link (ohci, ed);
251 /* the HC may not see the schedule updates yet, but if it does
252 * then they'll be properly ordered.
257 /*-------------------------------------------------------------------------*/
259 /* scan the periodic table to find and unlink this ED */
260 static void periodic_unlink (struct ohci_hcd *ohci, struct ed *ed)
264 for (i = ed->branch; i < NUM_INTS; i += ed->interval) {
266 struct ed **prev = &ohci->periodic [i];
267 __hc32 *prev_p = &ohci->hcca->int_table [i];
269 while (*prev && (temp = *prev) != ed) {
270 prev_p = &temp->hwNextED;
271 prev = &temp->ed_next;
274 *prev_p = ed->hwNextED;
277 ohci->load [i] -= ed->load;
279 ohci_to_hcd(ohci)->self.bandwidth_allocated -= ed->load / ed->interval;
281 ohci_vdbg (ohci, "unlink %sed %p branch %d [%dus.], interval %d\n",
282 (ed->hwINFO & cpu_to_hc32 (ohci, ED_ISO)) ? "iso " : "",
283 ed, ed->branch, ed->load, ed->interval);
286 /* unlink an ed from one of the HC chains.
287 * just the link to the ed is unlinked.
288 * the link from the ed still points to another operational ed or 0
289 * so the HC can eventually finish the processing of the unlinked ed
290 * (assuming it already started that, which needn't be true).
292 * ED_UNLINK is a transient state: the HC may still see this ED, but soon
293 * it won't. ED_SKIP means the HC will finish its current transaction,
294 * but won't start anything new. The TD queue may still grow; device
295 * drivers don't know about this HCD-internal state.
297 * When the HC can't see the ED, something changes ED_UNLINK to one of:
299 * - ED_OPER: when there's any request queued, the ED gets rescheduled
300 * immediately. HC should be working on them.
302 * - ED_IDLE: when there's no TD queue. there's no reason for the HC
303 * to care about this ED; safe to disable the endpoint.
305 * When finish_unlinks() runs later, after SOF interrupt, it will often
306 * complete one or more URB unlinks before making that state change.
308 static void ed_deschedule (struct ohci_hcd *ohci, struct ed *ed)
310 ed->hwINFO |= cpu_to_hc32 (ohci, ED_SKIP);
312 ed->state = ED_UNLINK;
314 /* To deschedule something from the control or bulk list, just
315 * clear CLE/BLE and wait. There's no safe way to scrub out list
316 * head/current registers until later, and "later" isn't very
317 * tightly specified. Figure 6-5 and Section 6.4.2.2 show how
318 * the HC is reading the ED queues (while we modify them).
320 * For now, ed_schedule() is "later". It might be good paranoia
321 * to scrub those registers in finish_unlinks(), in case of bugs
322 * that make the HC try to use them.
326 /* remove ED from the HC's list: */
327 if (ed->ed_prev == NULL) {
329 ohci->hc_control &= ~OHCI_CTRL_CLE;
330 ohci_writel (ohci, ohci->hc_control,
331 &ohci->regs->control);
332 // a ohci_readl() later syncs CLE with the HC
335 hc32_to_cpup (ohci, &ed->hwNextED),
336 &ohci->regs->ed_controlhead);
338 ed->ed_prev->ed_next = ed->ed_next;
339 ed->ed_prev->hwNextED = ed->hwNextED;
341 /* remove ED from the HCD's list: */
342 if (ohci->ed_controltail == ed) {
343 ohci->ed_controltail = ed->ed_prev;
344 if (ohci->ed_controltail)
345 ohci->ed_controltail->ed_next = NULL;
346 } else if (ed->ed_next) {
347 ed->ed_next->ed_prev = ed->ed_prev;
352 /* remove ED from the HC's list: */
353 if (ed->ed_prev == NULL) {
355 ohci->hc_control &= ~OHCI_CTRL_BLE;
356 ohci_writel (ohci, ohci->hc_control,
357 &ohci->regs->control);
358 // a ohci_readl() later syncs BLE with the HC
361 hc32_to_cpup (ohci, &ed->hwNextED),
362 &ohci->regs->ed_bulkhead);
364 ed->ed_prev->ed_next = ed->ed_next;
365 ed->ed_prev->hwNextED = ed->hwNextED;
367 /* remove ED from the HCD's list: */
368 if (ohci->ed_bulktail == ed) {
369 ohci->ed_bulktail = ed->ed_prev;
370 if (ohci->ed_bulktail)
371 ohci->ed_bulktail->ed_next = NULL;
372 } else if (ed->ed_next) {
373 ed->ed_next->ed_prev = ed->ed_prev;
377 // case PIPE_INTERRUPT:
378 // case PIPE_ISOCHRONOUS:
380 periodic_unlink (ohci, ed);
386 /*-------------------------------------------------------------------------*/
388 /* get and maybe (re)init an endpoint. init _should_ be done only as part
389 * of enumeration, usb_set_configuration() or usb_set_interface().
391 static struct ed *ed_get (
392 struct ohci_hcd *ohci,
393 struct usb_host_endpoint *ep,
394 struct usb_device *udev,
401 spin_lock_irqsave (&ohci->lock, flags);
403 if (!(ed = ep->hcpriv)) {
408 ed = ed_alloc (ohci, GFP_ATOMIC);
414 /* dummy td; end of td list for ed */
415 td = td_alloc (ohci, GFP_ATOMIC);
423 ed->hwTailP = cpu_to_hc32 (ohci, td->td_dma);
424 ed->hwHeadP = ed->hwTailP; /* ED_C, ED_H zeroed */
427 is_out = !(ep->desc.bEndpointAddress & USB_DIR_IN);
429 /* FIXME usbcore changes dev->devnum before SET_ADDRESS
430 * suceeds ... otherwise we wouldn't need "pipe".
432 info = usb_pipedevice (pipe);
433 ed->type = usb_pipetype(pipe);
435 info |= (ep->desc.bEndpointAddress & ~USB_DIR_IN) << 7;
436 info |= le16_to_cpu(ep->desc.wMaxPacketSize) << 16;
437 if (udev->speed == USB_SPEED_LOW)
439 /* only control transfers store pids in tds */
440 if (ed->type != PIPE_CONTROL) {
441 info |= is_out ? ED_OUT : ED_IN;
442 if (ed->type != PIPE_BULK) {
443 /* periodic transfers... */
444 if (ed->type == PIPE_ISOCHRONOUS)
446 else if (interval > 32) /* iso can be bigger */
448 ed->interval = interval;
449 ed->load = usb_calc_bus_time (
450 udev->speed, !is_out,
451 ed->type == PIPE_ISOCHRONOUS,
452 le16_to_cpu(ep->desc.wMaxPacketSize))
456 ed->hwINFO = cpu_to_hc32(ohci, info);
462 spin_unlock_irqrestore (&ohci->lock, flags);
466 /*-------------------------------------------------------------------------*/
468 /* request unlinking of an endpoint from an operational HC.
469 * put the ep on the rm_list
470 * real work is done at the next start frame (SF) hardware interrupt
471 * caller guarantees HCD is running, so hardware access is safe,
472 * and that ed->state is ED_OPER
474 static void start_ed_unlink (struct ohci_hcd *ohci, struct ed *ed)
476 ed->hwINFO |= cpu_to_hc32 (ohci, ED_DEQUEUE);
477 ed_deschedule (ohci, ed);
479 /* rm_list is just singly linked, for simplicity */
480 ed->ed_next = ohci->ed_rm_list;
482 ohci->ed_rm_list = ed;
484 /* enable SOF interrupt */
485 ohci_writel (ohci, OHCI_INTR_SF, &ohci->regs->intrstatus);
486 ohci_writel (ohci, OHCI_INTR_SF, &ohci->regs->intrenable);
487 // flush those writes, and get latest HCCA contents
488 (void) ohci_readl (ohci, &ohci->regs->control);
490 /* SF interrupt might get delayed; record the frame counter value that
491 * indicates when the HC isn't looking at it, so concurrent unlinks
492 * behave. frame_no wraps every 2^16 msec, and changes right before
495 ed->tick = ohci_frame_no(ohci) + 1;
499 /*-------------------------------------------------------------------------*
500 * TD handling functions
501 *-------------------------------------------------------------------------*/
503 /* enqueue next TD for this URB (OHCI spec 5.2.8.2) */
506 td_fill (struct ohci_hcd *ohci, u32 info,
507 dma_addr_t data, int len,
508 struct urb *urb, int index)
510 struct td *td, *td_pt;
511 struct urb_priv *urb_priv = urb->hcpriv;
512 int is_iso = info & TD_ISO;
515 // ASSERT (index < urb_priv->length);
517 /* aim for only one interrupt per urb. mostly applies to control
518 * and iso; other urbs rarely need more than one TD per urb.
519 * this way, only final tds (or ones with an error) cause IRQs.
520 * at least immediately; use DI=6 in case any control request is
521 * tempted to die part way through. (and to force the hc to flush
522 * its donelist soonish, even on unlink paths.)
524 * NOTE: could delay interrupts even for the last TD, and get fewer
525 * interrupts ... increasing per-urb latency by sharing interrupts.
526 * Drivers that queue bulk urbs may request that behavior.
528 if (index != (urb_priv->length - 1)
529 || (urb->transfer_flags & URB_NO_INTERRUPT))
530 info |= TD_DI_SET (6);
532 /* use this td as the next dummy */
533 td_pt = urb_priv->td [index];
535 /* fill the old dummy TD */
536 td = urb_priv->td [index] = urb_priv->ed->dummy;
537 urb_priv->ed->dummy = td_pt;
539 td->ed = urb_priv->ed;
540 td->next_dl_td = NULL;
547 td->hwINFO = cpu_to_hc32 (ohci, info);
549 td->hwCBP = cpu_to_hc32 (ohci, data & 0xFFFFF000);
550 td->hwPSW [0] = cpu_to_hc16 (ohci, (data & 0x0FFF) | 0xE000);
551 td->ed->last_iso = info & 0xffff;
553 td->hwCBP = cpu_to_hc32 (ohci, data);
556 td->hwBE = cpu_to_hc32 (ohci, data + len - 1);
559 td->hwNextTD = cpu_to_hc32 (ohci, td_pt->td_dma);
561 /* append to queue */
562 list_add_tail (&td->td_list, &td->ed->td_list);
564 /* hash it for later reverse mapping */
565 hash = TD_HASH_FUNC (td->td_dma);
566 td->td_hash = ohci->td_hash [hash];
567 ohci->td_hash [hash] = td;
569 /* HC might read the TD (or cachelines) right away ... */
571 td->ed->hwTailP = td->hwNextTD;
574 /*-------------------------------------------------------------------------*/
576 /* Prepare all TDs of a transfer, and queue them onto the ED.
577 * Caller guarantees HC is active.
578 * Usually the ED is already on the schedule, so TDs might be
579 * processed as soon as they're queued.
581 static void td_submit_urb (
582 struct ohci_hcd *ohci,
585 struct urb_priv *urb_priv = urb->hcpriv;
587 int data_len = urb->transfer_buffer_length;
590 int is_out = usb_pipeout (urb->pipe);
593 /* OHCI handles the bulk/interrupt data toggles itself. We just
594 * use the device toggle bits for resetting, and rely on the fact
595 * that resetting toggle is meaningless if the endpoint is active.
597 if (!usb_gettoggle (urb->dev, usb_pipeendpoint (urb->pipe), is_out)) {
598 usb_settoggle (urb->dev, usb_pipeendpoint (urb->pipe),
600 urb_priv->ed->hwHeadP &= ~cpu_to_hc32 (ohci, ED_C);
603 urb_priv->td_cnt = 0;
604 list_add (&urb_priv->pending, &ohci->pending);
607 data = urb->transfer_dma;
611 /* NOTE: TD_CC is set so we can tell which TDs the HC processed by
612 * using TD_CC_GET, as well as by seeing them on the done list.
613 * (CC = NotAccessed ... 0x0F, or 0x0E in PSWs for ISO.)
615 switch (urb_priv->ed->type) {
617 /* Bulk and interrupt are identical except for where in the schedule
621 /* ... and periodic urbs have extra accounting */
622 periodic = ohci_to_hcd(ohci)->self.bandwidth_int_reqs++ == 0
623 && ohci_to_hcd(ohci)->self.bandwidth_isoc_reqs == 0;
627 ? TD_T_TOGGLE | TD_CC | TD_DP_OUT
628 : TD_T_TOGGLE | TD_CC | TD_DP_IN;
629 /* TDs _could_ transfer up to 8K each */
630 while (data_len > 4096) {
631 td_fill (ohci, info, data, 4096, urb, cnt);
636 /* maybe avoid ED halt on final TD short read */
637 if (!(urb->transfer_flags & URB_SHORT_NOT_OK))
639 td_fill (ohci, info, data, data_len, urb, cnt);
641 if ((urb->transfer_flags & URB_ZERO_PACKET)
642 && cnt < urb_priv->length) {
643 td_fill (ohci, info, 0, 0, urb, cnt);
646 /* maybe kickstart bulk list */
647 if (urb_priv->ed->type == PIPE_BULK) {
649 ohci_writel (ohci, OHCI_BLF, &ohci->regs->cmdstatus);
653 /* control manages DATA0/DATA1 toggle per-request; SETUP resets it,
654 * any DATA phase works normally, and the STATUS ack is special.
657 info = TD_CC | TD_DP_SETUP | TD_T_DATA0;
658 td_fill (ohci, info, urb->setup_dma, 8, urb, cnt++);
660 info = TD_CC | TD_R | TD_T_DATA1;
661 info |= is_out ? TD_DP_OUT : TD_DP_IN;
662 /* NOTE: mishandles transfers >8K, some >4K */
663 td_fill (ohci, info, data, data_len, urb, cnt++);
666 ? TD_CC | TD_DP_IN | TD_T_DATA1
667 : TD_CC | TD_DP_OUT | TD_T_DATA1;
668 td_fill (ohci, info, data, 0, urb, cnt++);
669 /* maybe kickstart control list */
671 ohci_writel (ohci, OHCI_CLF, &ohci->regs->cmdstatus);
674 /* ISO has no retransmit, so no toggle; and it uses special TDs.
675 * Each TD could handle multiple consecutive frames (interval 1);
676 * we could often reduce the number of TDs here.
678 case PIPE_ISOCHRONOUS:
679 for (cnt = 0; cnt < urb->number_of_packets; cnt++) {
680 int frame = urb->start_frame;
682 // FIXME scheduling should handle frame counter
683 // roll-around ... exotic case (and OHCI has
684 // a 2^16 iso range, vs other HCs max of 2^10)
685 frame += cnt * urb->interval;
687 td_fill (ohci, TD_CC | TD_ISO | frame,
688 data + urb->iso_frame_desc [cnt].offset,
689 urb->iso_frame_desc [cnt].length, urb, cnt);
691 periodic = ohci_to_hcd(ohci)->self.bandwidth_isoc_reqs++ == 0
692 && ohci_to_hcd(ohci)->self.bandwidth_int_reqs == 0;
696 /* start periodic dma if needed */
699 ohci->hc_control |= OHCI_CTRL_PLE|OHCI_CTRL_IE;
700 ohci_writel (ohci, ohci->hc_control, &ohci->regs->control);
703 // ASSERT (urb_priv->length == cnt);
706 /*-------------------------------------------------------------------------*
707 * Done List handling functions
708 *-------------------------------------------------------------------------*/
710 /* calculate transfer length/status and update the urb
711 * PRECONDITION: irqsafe (only for urb->status locking)
713 static void td_done (struct ohci_hcd *ohci, struct urb *urb, struct td *td)
715 u32 tdINFO = hc32_to_cpup (ohci, &td->hwINFO);
718 list_del (&td->td_list);
720 /* ISO ... drivers see per-TD length/status */
721 if (tdINFO & TD_ISO) {
722 u16 tdPSW = hc16_to_cpu (ohci, td->hwPSW [0]);
725 /* NOTE: assumes FC in tdINFO == 0 (and MAXPSW == 1) */
727 cc = (tdPSW >> 12) & 0xF;
728 if (tdINFO & TD_CC) /* hc didn't touch? */
731 if (usb_pipeout (urb->pipe))
732 dlen = urb->iso_frame_desc [td->index].length;
734 /* short reads are always OK for ISO */
735 if (cc == TD_DATAUNDERRUN)
737 dlen = tdPSW & 0x3ff;
739 urb->actual_length += dlen;
740 urb->iso_frame_desc [td->index].actual_length = dlen;
741 urb->iso_frame_desc [td->index].status = cc_to_error [cc];
743 if (cc != TD_CC_NOERROR)
745 "urb %p iso td %p (%d) len %d cc %d\n",
746 urb, td, 1 + td->index, dlen, cc);
748 /* BULK, INT, CONTROL ... drivers see aggregate length/status,
749 * except that "setup" bytes aren't counted and "short" transfers
750 * might not be reported as errors.
753 int type = usb_pipetype (urb->pipe);
754 u32 tdBE = hc32_to_cpup (ohci, &td->hwBE);
756 cc = TD_CC_GET (tdINFO);
758 /* update packet status if needed (short is normally ok) */
759 if (cc == TD_DATAUNDERRUN
760 && !(urb->transfer_flags & URB_SHORT_NOT_OK))
762 if (cc != TD_CC_NOERROR && cc < 0x0E) {
763 spin_lock (&urb->lock);
764 if (urb->status == -EINPROGRESS)
765 urb->status = cc_to_error [cc];
766 spin_unlock (&urb->lock);
769 /* count all non-empty packets except control SETUP packet */
770 if ((type != PIPE_CONTROL || td->index != 0) && tdBE != 0) {
772 urb->actual_length += tdBE - td->data_dma + 1;
774 urb->actual_length +=
775 hc32_to_cpup (ohci, &td->hwCBP)
779 if (cc != TD_CC_NOERROR && cc < 0x0E)
781 "urb %p td %p (%d) cc %d, len=%d/%d\n",
782 urb, td, 1 + td->index, cc,
784 urb->transfer_buffer_length);
788 /*-------------------------------------------------------------------------*/
790 static inline struct td *
791 ed_halted (struct ohci_hcd *ohci, struct td *td, int cc, struct td *rev)
793 struct urb *urb = td->urb;
794 struct ed *ed = td->ed;
795 struct list_head *tmp = td->td_list.next;
796 __hc32 toggle = ed->hwHeadP & cpu_to_hc32 (ohci, ED_C);
798 /* clear ed halt; this is the td that caused it, but keep it inactive
799 * until its urb->complete() has a chance to clean up.
801 ed->hwINFO |= cpu_to_hc32 (ohci, ED_SKIP);
803 ed->hwHeadP &= ~cpu_to_hc32 (ohci, ED_H);
805 /* put any later tds from this urb onto the donelist, after 'td',
806 * order won't matter here: no errors, and nothing was transferred.
807 * also patch the ed so it looks as if those tds completed normally.
809 while (tmp != &ed->td_list) {
813 next = list_entry (tmp, struct td, td_list);
814 tmp = next->td_list.next;
816 if (next->urb != urb)
819 /* NOTE: if multi-td control DATA segments get supported,
820 * this urb had one of them, this td wasn't the last td
821 * in that segment (TD_R clear), this ed halted because
822 * of a short read, _and_ URB_SHORT_NOT_OK is clear ...
823 * then we need to leave the control STATUS packet queued
827 info |= cpu_to_hc32 (ohci, TD_DONE);
828 info &= ~cpu_to_hc32 (ohci, TD_CC);
831 next->next_dl_td = rev;
834 ed->hwHeadP = next->hwNextTD | toggle;
837 /* help for troubleshooting: report anything that
838 * looks odd ... that doesn't include protocol stalls
839 * (or maybe some other things)
842 case TD_DATAUNDERRUN:
843 if ((urb->transfer_flags & URB_SHORT_NOT_OK) == 0)
847 if (usb_pipecontrol (urb->pipe))
852 "urb %p path %s ep%d%s %08x cc %d --> status %d\n",
853 urb, urb->dev->devpath,
854 usb_pipeendpoint (urb->pipe),
855 usb_pipein (urb->pipe) ? "in" : "out",
856 hc32_to_cpu (ohci, td->hwINFO),
857 cc, cc_to_error [cc]);
863 /* replies to the request have to be on a FIFO basis so
864 * we unreverse the hc-reversed done-list
866 static struct td *dl_reverse_done_list (struct ohci_hcd *ohci)
869 struct td *td_rev = NULL;
870 struct td *td = NULL;
872 td_dma = hc32_to_cpup (ohci, &ohci->hcca->done_head);
873 ohci->hcca->done_head = 0;
876 /* get TD from hc's singly linked list, and
877 * prepend to ours. ed->td_list changes later.
882 td = dma_to_td (ohci, td_dma);
884 ohci_err (ohci, "bad entry %8x\n", td_dma);
888 td->hwINFO |= cpu_to_hc32 (ohci, TD_DONE);
889 cc = TD_CC_GET (hc32_to_cpup (ohci, &td->hwINFO));
891 /* Non-iso endpoints can halt on error; un-halt,
892 * and dequeue any other TDs from this urb.
893 * No other TD could have caused the halt.
895 if (cc != TD_CC_NOERROR
896 && (td->ed->hwHeadP & cpu_to_hc32 (ohci, ED_H)))
897 td_rev = ed_halted (ohci, td, cc, td_rev);
899 td->next_dl_td = td_rev;
901 td_dma = hc32_to_cpup (ohci, &td->hwNextTD);
906 /*-------------------------------------------------------------------------*/
908 /* there are some urbs/eds to unlink; called in_irq(), with HCD locked */
910 finish_unlinks (struct ohci_hcd *ohci, u16 tick, struct pt_regs *regs)
912 struct ed *ed, **last;
915 for (last = &ohci->ed_rm_list, ed = *last; ed != NULL; ed = *last) {
916 struct list_head *entry, *tmp;
917 int completed, modified;
920 /* only take off EDs that the HC isn't using, accounting for
921 * frame counter wraps and EDs with partially retired TDs
923 if (likely (regs && HCD_IS_RUNNING(ohci_to_hcd(ohci)->state))) {
924 if (tick_before (tick, ed->tick)) {
930 if (!list_empty (&ed->td_list)) {
934 td = list_entry (ed->td_list.next, struct td,
936 head = hc32_to_cpu (ohci, ed->hwHeadP) &
939 /* INTR_WDH may need to clean up first */
940 if (td->td_dma != head)
945 /* reentrancy: if we drop the schedule lock, someone might
946 * have modified this list. normally it's just prepending
947 * entries (which we'd ignore), but paranoia won't hurt.
953 /* unlink urbs as requested, but rescan the list after
954 * we call a completion since it might have unlinked
955 * another (earlier) urb
957 * When we get here, the HC doesn't see this ed. But it
958 * must not be rescheduled until all completed URBs have
959 * been given back to the driver.
964 list_for_each_safe (entry, tmp, &ed->td_list) {
967 urb_priv_t *urb_priv;
970 td = list_entry (entry, struct td, td_list);
972 urb_priv = td->urb->hcpriv;
974 if (urb->status == -EINPROGRESS) {
975 prev = &td->hwNextTD;
979 /* patch pointer hc uses */
980 savebits = *prev & ~cpu_to_hc32 (ohci, TD_MASK);
981 *prev = td->hwNextTD | savebits;
983 /* HC may have partly processed this TD */
984 td_done (ohci, urb, td);
987 /* if URB is done, clean up */
988 if (urb_priv->td_cnt == urb_priv->length) {
989 modified = completed = 1;
990 finish_urb (ohci, urb, regs);
993 if (completed && !list_empty (&ed->td_list))
996 /* ED's now officially unlinked, hc doesn't see */
998 ed->hwHeadP &= ~cpu_to_hc32(ohci, ED_H);
1001 ed->hwINFO &= ~cpu_to_hc32 (ohci, ED_SKIP | ED_DEQUEUE);
1003 /* but if there's work queued, reschedule */
1004 if (!list_empty (&ed->td_list)) {
1005 if (HCD_IS_RUNNING(ohci_to_hcd(ohci)->state))
1006 ed_schedule (ohci, ed);
1013 /* maybe reenable control and bulk lists */
1014 if (HCD_IS_RUNNING(ohci_to_hcd(ohci)->state)
1015 && ohci_to_hcd(ohci)->state != USB_STATE_QUIESCING
1016 && !ohci->ed_rm_list) {
1017 u32 command = 0, control = 0;
1019 if (ohci->ed_controltail) {
1020 command |= OHCI_CLF;
1021 if (!(ohci->hc_control & OHCI_CTRL_CLE)) {
1022 control |= OHCI_CTRL_CLE;
1023 ohci_writel (ohci, 0,
1024 &ohci->regs->ed_controlcurrent);
1027 if (ohci->ed_bulktail) {
1028 command |= OHCI_BLF;
1029 if (!(ohci->hc_control & OHCI_CTRL_BLE)) {
1030 control |= OHCI_CTRL_BLE;
1031 ohci_writel (ohci, 0,
1032 &ohci->regs->ed_bulkcurrent);
1036 /* CLE/BLE to enable, CLF/BLF to (maybe) kickstart */
1038 ohci->hc_control |= control;
1039 ohci_writel (ohci, ohci->hc_control,
1040 &ohci->regs->control);
1043 ohci_writel (ohci, command, &ohci->regs->cmdstatus);
1049 /*-------------------------------------------------------------------------*/
1052 * Process normal completions (error or success) and clean the schedules.
1054 * This is the main path for handing urbs back to drivers. The only other
1055 * path is finish_unlinks(), which unlinks URBs using ed_rm_list, instead of
1056 * scanning the (re-reversed) donelist as this does.
1059 dl_done_list (struct ohci_hcd *ohci, struct pt_regs *regs)
1061 struct td *td = dl_reverse_done_list (ohci);
1064 struct td *td_next = td->next_dl_td;
1065 struct urb *urb = td->urb;
1066 urb_priv_t *urb_priv = urb->hcpriv;
1067 struct ed *ed = td->ed;
1069 /* update URB's length and status from TD */
1070 td_done (ohci, urb, td);
1073 /* If all this urb's TDs are done, call complete() */
1074 if (urb_priv->td_cnt == urb_priv->length)
1075 finish_urb (ohci, urb, regs);
1077 /* clean schedule: unlink EDs that are no longer busy */
1078 if (list_empty (&ed->td_list)) {
1079 if (ed->state == ED_OPER)
1080 start_ed_unlink (ohci, ed);
1082 /* ... reenabling halted EDs only after fault cleanup */
1083 } else if ((ed->hwINFO & cpu_to_hc32 (ohci, ED_SKIP | ED_DEQUEUE))
1084 == cpu_to_hc32 (ohci, ED_SKIP)) {
1085 td = list_entry (ed->td_list.next, struct td, td_list);
1086 if (!(td->hwINFO & cpu_to_hc32 (ohci, TD_DONE))) {
1087 ed->hwINFO &= ~cpu_to_hc32 (ohci, ED_SKIP);
1088 /* ... hc may need waking-up */
1091 ohci_writel (ohci, OHCI_CLF,
1092 &ohci->regs->cmdstatus);
1095 ohci_writel (ohci, OHCI_BLF,
1096 &ohci->regs->cmdstatus);