xref: /linux/drivers/usb/host/ohci-q.c (revision 89a0fd18a96eb1f8732714b575073f8a8d69c009)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  * OHCI HCD (Host Controller Driver) for USB.
31da177e4SLinus Torvalds  *
41da177e4SLinus Torvalds  * (C) Copyright 1999 Roman Weissgaerber <weissg@vienna.at>
51da177e4SLinus Torvalds  * (C) Copyright 2000-2002 David Brownell <dbrownell@users.sourceforge.net>
61da177e4SLinus Torvalds  *
71da177e4SLinus Torvalds  * This file is licenced under the GPL.
81da177e4SLinus Torvalds  */
91da177e4SLinus Torvalds 
107d12e780SDavid Howells #include <linux/irq.h>
117d12e780SDavid Howells 
121da177e4SLinus Torvalds static void urb_free_priv (struct ohci_hcd *hc, urb_priv_t *urb_priv)
131da177e4SLinus Torvalds {
141da177e4SLinus Torvalds 	int		last = urb_priv->length - 1;
151da177e4SLinus Torvalds 
161da177e4SLinus Torvalds 	if (last >= 0) {
171da177e4SLinus Torvalds 		int		i;
181da177e4SLinus Torvalds 		struct td	*td;
191da177e4SLinus Torvalds 
201da177e4SLinus Torvalds 		for (i = 0; i <= last; i++) {
211da177e4SLinus Torvalds 			td = urb_priv->td [i];
221da177e4SLinus Torvalds 			if (td)
231da177e4SLinus Torvalds 				td_free (hc, td);
241da177e4SLinus Torvalds 		}
251da177e4SLinus Torvalds 	}
261da177e4SLinus Torvalds 
271da177e4SLinus Torvalds 	list_del (&urb_priv->pending);
281da177e4SLinus Torvalds 	kfree (urb_priv);
291da177e4SLinus Torvalds }
301da177e4SLinus Torvalds 
311da177e4SLinus Torvalds /*-------------------------------------------------------------------------*/
321da177e4SLinus Torvalds 
331da177e4SLinus Torvalds /*
341da177e4SLinus Torvalds  * URB goes back to driver, and isn't reissued.
351da177e4SLinus Torvalds  * It's completely gone from HC data structures.
361da177e4SLinus Torvalds  * PRECONDITION:  ohci lock held, irqs blocked.
371da177e4SLinus Torvalds  */
381da177e4SLinus Torvalds static void
397d12e780SDavid Howells finish_urb (struct ohci_hcd *ohci, struct urb *urb)
401da177e4SLinus Torvalds __releases(ohci->lock)
411da177e4SLinus Torvalds __acquires(ohci->lock)
421da177e4SLinus Torvalds {
431da177e4SLinus Torvalds 	// ASSERT (urb->hcpriv != 0);
441da177e4SLinus Torvalds 
451da177e4SLinus Torvalds 	urb_free_priv (ohci, urb->hcpriv);
461da177e4SLinus Torvalds 	urb->hcpriv = NULL;
471da177e4SLinus Torvalds 
481da177e4SLinus Torvalds 	spin_lock (&urb->lock);
491da177e4SLinus Torvalds 	if (likely (urb->status == -EINPROGRESS))
501da177e4SLinus Torvalds 		urb->status = 0;
511da177e4SLinus Torvalds 	/* report short control reads right even though the data TD always
521da177e4SLinus Torvalds 	 * has TD_R set.  (much simpler, but creates the 1-td limit.)
531da177e4SLinus Torvalds 	 */
541da177e4SLinus Torvalds 	if (unlikely (urb->transfer_flags & URB_SHORT_NOT_OK)
551da177e4SLinus Torvalds 			&& unlikely (usb_pipecontrol (urb->pipe))
561da177e4SLinus Torvalds 			&& urb->actual_length < urb->transfer_buffer_length
571da177e4SLinus Torvalds 			&& usb_pipein (urb->pipe)
581da177e4SLinus Torvalds 			&& urb->status == 0) {
591da177e4SLinus Torvalds 		urb->status = -EREMOTEIO;
601da177e4SLinus Torvalds 	}
611da177e4SLinus Torvalds 	spin_unlock (&urb->lock);
621da177e4SLinus Torvalds 
631da177e4SLinus Torvalds 	switch (usb_pipetype (urb->pipe)) {
641da177e4SLinus Torvalds 	case PIPE_ISOCHRONOUS:
651da177e4SLinus Torvalds 		ohci_to_hcd(ohci)->self.bandwidth_isoc_reqs--;
661da177e4SLinus Torvalds 		break;
671da177e4SLinus Torvalds 	case PIPE_INTERRUPT:
681da177e4SLinus Torvalds 		ohci_to_hcd(ohci)->self.bandwidth_int_reqs--;
691da177e4SLinus Torvalds 		break;
701da177e4SLinus Torvalds 	}
711da177e4SLinus Torvalds 
721da177e4SLinus Torvalds #ifdef OHCI_VERBOSE_DEBUG
731da177e4SLinus Torvalds 	urb_print (urb, "RET", usb_pipeout (urb->pipe));
741da177e4SLinus Torvalds #endif
751da177e4SLinus Torvalds 
761da177e4SLinus Torvalds 	/* urb->complete() can reenter this HCD */
771da177e4SLinus Torvalds 	spin_unlock (&ohci->lock);
787d12e780SDavid Howells 	usb_hcd_giveback_urb (ohci_to_hcd(ohci), urb);
791da177e4SLinus Torvalds 	spin_lock (&ohci->lock);
801da177e4SLinus Torvalds 
811da177e4SLinus Torvalds 	/* stop periodic dma if it's not needed */
821da177e4SLinus Torvalds 	if (ohci_to_hcd(ohci)->self.bandwidth_isoc_reqs == 0
831da177e4SLinus Torvalds 			&& ohci_to_hcd(ohci)->self.bandwidth_int_reqs == 0) {
841da177e4SLinus Torvalds 		ohci->hc_control &= ~(OHCI_CTRL_PLE|OHCI_CTRL_IE);
851da177e4SLinus Torvalds 		ohci_writel (ohci, ohci->hc_control, &ohci->regs->control);
861da177e4SLinus Torvalds 	}
871da177e4SLinus Torvalds }
881da177e4SLinus Torvalds 
891da177e4SLinus Torvalds 
901da177e4SLinus Torvalds /*-------------------------------------------------------------------------*
911da177e4SLinus Torvalds  * ED handling functions
921da177e4SLinus Torvalds  *-------------------------------------------------------------------------*/
931da177e4SLinus Torvalds 
941da177e4SLinus Torvalds /* search for the right schedule branch to use for a periodic ed.
951da177e4SLinus Torvalds  * does some load balancing; returns the branch, or negative errno.
961da177e4SLinus Torvalds  */
971da177e4SLinus Torvalds static int balance (struct ohci_hcd *ohci, int interval, int load)
981da177e4SLinus Torvalds {
991da177e4SLinus Torvalds 	int	i, branch = -ENOSPC;
1001da177e4SLinus Torvalds 
1011da177e4SLinus Torvalds 	/* iso periods can be huge; iso tds specify frame numbers */
1021da177e4SLinus Torvalds 	if (interval > NUM_INTS)
1031da177e4SLinus Torvalds 		interval = NUM_INTS;
1041da177e4SLinus Torvalds 
1051da177e4SLinus Torvalds 	/* search for the least loaded schedule branch of that period
1061da177e4SLinus Torvalds 	 * that has enough bandwidth left unreserved.
1071da177e4SLinus Torvalds 	 */
1081da177e4SLinus Torvalds 	for (i = 0; i < interval ; i++) {
1091da177e4SLinus Torvalds 		if (branch < 0 || ohci->load [branch] > ohci->load [i]) {
1101da177e4SLinus Torvalds 			int	j;
1111da177e4SLinus Torvalds 
1121da177e4SLinus Torvalds 			/* usb 1.1 says 90% of one frame */
1131da177e4SLinus Torvalds 			for (j = i; j < NUM_INTS; j += interval) {
1141da177e4SLinus Torvalds 				if ((ohci->load [j] + load) > 900)
1151da177e4SLinus Torvalds 					break;
1161da177e4SLinus Torvalds 			}
1171da177e4SLinus Torvalds 			if (j < NUM_INTS)
1181da177e4SLinus Torvalds 				continue;
1191da177e4SLinus Torvalds 			branch = i;
1201da177e4SLinus Torvalds 		}
1211da177e4SLinus Torvalds 	}
1221da177e4SLinus Torvalds 	return branch;
1231da177e4SLinus Torvalds }
1241da177e4SLinus Torvalds 
1251da177e4SLinus Torvalds /*-------------------------------------------------------------------------*/
1261da177e4SLinus Torvalds 
1271da177e4SLinus Torvalds /* both iso and interrupt requests have periods; this routine puts them
1281da177e4SLinus Torvalds  * into the schedule tree in the apppropriate place.  most iso devices use
1291da177e4SLinus Torvalds  * 1msec periods, but that's not required.
1301da177e4SLinus Torvalds  */
1311da177e4SLinus Torvalds static void periodic_link (struct ohci_hcd *ohci, struct ed *ed)
1321da177e4SLinus Torvalds {
1331da177e4SLinus Torvalds 	unsigned	i;
1341da177e4SLinus Torvalds 
1351da177e4SLinus Torvalds 	ohci_vdbg (ohci, "link %sed %p branch %d [%dus.], interval %d\n",
1361da177e4SLinus Torvalds 		(ed->hwINFO & cpu_to_hc32 (ohci, ED_ISO)) ? "iso " : "",
1371da177e4SLinus Torvalds 		ed, ed->branch, ed->load, ed->interval);
1381da177e4SLinus Torvalds 
1391da177e4SLinus Torvalds 	for (i = ed->branch; i < NUM_INTS; i += ed->interval) {
1401da177e4SLinus Torvalds 		struct ed	**prev = &ohci->periodic [i];
1411da177e4SLinus Torvalds 		__hc32		*prev_p = &ohci->hcca->int_table [i];
1421da177e4SLinus Torvalds 		struct ed	*here = *prev;
1431da177e4SLinus Torvalds 
1441da177e4SLinus Torvalds 		/* sorting each branch by period (slow before fast)
1451da177e4SLinus Torvalds 		 * lets us share the faster parts of the tree.
1461da177e4SLinus Torvalds 		 * (plus maybe: put interrupt eds before iso)
1471da177e4SLinus Torvalds 		 */
1481da177e4SLinus Torvalds 		while (here && ed != here) {
1491da177e4SLinus Torvalds 			if (ed->interval > here->interval)
1501da177e4SLinus Torvalds 				break;
1511da177e4SLinus Torvalds 			prev = &here->ed_next;
1521da177e4SLinus Torvalds 			prev_p = &here->hwNextED;
1531da177e4SLinus Torvalds 			here = *prev;
1541da177e4SLinus Torvalds 		}
1551da177e4SLinus Torvalds 		if (ed != here) {
1561da177e4SLinus Torvalds 			ed->ed_next = here;
1571da177e4SLinus Torvalds 			if (here)
1581da177e4SLinus Torvalds 				ed->hwNextED = *prev_p;
1591da177e4SLinus Torvalds 			wmb ();
1601da177e4SLinus Torvalds 			*prev = ed;
1611da177e4SLinus Torvalds 			*prev_p = cpu_to_hc32(ohci, ed->dma);
1621da177e4SLinus Torvalds 			wmb();
1631da177e4SLinus Torvalds 		}
1641da177e4SLinus Torvalds 		ohci->load [i] += ed->load;
1651da177e4SLinus Torvalds 	}
1661da177e4SLinus Torvalds 	ohci_to_hcd(ohci)->self.bandwidth_allocated += ed->load / ed->interval;
1671da177e4SLinus Torvalds }
1681da177e4SLinus Torvalds 
1691da177e4SLinus Torvalds /* link an ed into one of the HC chains */
1701da177e4SLinus Torvalds 
1711da177e4SLinus Torvalds static int ed_schedule (struct ohci_hcd *ohci, struct ed *ed)
1721da177e4SLinus Torvalds {
1731da177e4SLinus Torvalds 	int	branch;
1741da177e4SLinus Torvalds 
1751da177e4SLinus Torvalds 	if (ohci_to_hcd(ohci)->state == HC_STATE_QUIESCING)
1761da177e4SLinus Torvalds 		return -EAGAIN;
1771da177e4SLinus Torvalds 
1781da177e4SLinus Torvalds 	ed->state = ED_OPER;
1791da177e4SLinus Torvalds 	ed->ed_prev = NULL;
1801da177e4SLinus Torvalds 	ed->ed_next = NULL;
1811da177e4SLinus Torvalds 	ed->hwNextED = 0;
182*89a0fd18SMike Nuss 	if (quirk_zfmicro(ohci)
183*89a0fd18SMike Nuss 			&& (ed->type == PIPE_INTERRUPT)
184*89a0fd18SMike Nuss 			&& !(ohci->eds_scheduled++))
185*89a0fd18SMike Nuss 		mod_timer(&ohci->unlink_watchdog, round_jiffies_relative(HZ));
1861da177e4SLinus Torvalds 	wmb ();
1871da177e4SLinus Torvalds 
1881da177e4SLinus Torvalds 	/* we care about rm_list when setting CLE/BLE in case the HC was at
1891da177e4SLinus Torvalds 	 * work on some TD when CLE/BLE was turned off, and isn't quiesced
1901da177e4SLinus Torvalds 	 * yet.  finish_unlinks() restarts as needed, some upcoming INTR_SF.
1911da177e4SLinus Torvalds 	 *
1921da177e4SLinus Torvalds 	 * control and bulk EDs are doubly linked (ed_next, ed_prev), but
1931da177e4SLinus Torvalds 	 * periodic ones are singly linked (ed_next). that's because the
1941da177e4SLinus Torvalds 	 * periodic schedule encodes a tree like figure 3-5 in the ohci
1951da177e4SLinus Torvalds 	 * spec:  each qh can have several "previous" nodes, and the tree
1961da177e4SLinus Torvalds 	 * doesn't have unused/idle descriptors.
1971da177e4SLinus Torvalds 	 */
1981da177e4SLinus Torvalds 	switch (ed->type) {
1991da177e4SLinus Torvalds 	case PIPE_CONTROL:
2001da177e4SLinus Torvalds 		if (ohci->ed_controltail == NULL) {
2011da177e4SLinus Torvalds 			WARN_ON (ohci->hc_control & OHCI_CTRL_CLE);
2021da177e4SLinus Torvalds 			ohci_writel (ohci, ed->dma,
2031da177e4SLinus Torvalds 					&ohci->regs->ed_controlhead);
2041da177e4SLinus Torvalds 		} else {
2051da177e4SLinus Torvalds 			ohci->ed_controltail->ed_next = ed;
2061da177e4SLinus Torvalds 			ohci->ed_controltail->hwNextED = cpu_to_hc32 (ohci,
2071da177e4SLinus Torvalds 								ed->dma);
2081da177e4SLinus Torvalds 		}
2091da177e4SLinus Torvalds 		ed->ed_prev = ohci->ed_controltail;
2101da177e4SLinus Torvalds 		if (!ohci->ed_controltail && !ohci->ed_rm_list) {
2111da177e4SLinus Torvalds 			wmb();
2121da177e4SLinus Torvalds 			ohci->hc_control |= OHCI_CTRL_CLE;
2131da177e4SLinus Torvalds 			ohci_writel (ohci, 0, &ohci->regs->ed_controlcurrent);
2141da177e4SLinus Torvalds 			ohci_writel (ohci, ohci->hc_control,
2151da177e4SLinus Torvalds 					&ohci->regs->control);
2161da177e4SLinus Torvalds 		}
2171da177e4SLinus Torvalds 		ohci->ed_controltail = ed;
2181da177e4SLinus Torvalds 		break;
2191da177e4SLinus Torvalds 
2201da177e4SLinus Torvalds 	case PIPE_BULK:
2211da177e4SLinus Torvalds 		if (ohci->ed_bulktail == NULL) {
2221da177e4SLinus Torvalds 			WARN_ON (ohci->hc_control & OHCI_CTRL_BLE);
2231da177e4SLinus Torvalds 			ohci_writel (ohci, ed->dma, &ohci->regs->ed_bulkhead);
2241da177e4SLinus Torvalds 		} else {
2251da177e4SLinus Torvalds 			ohci->ed_bulktail->ed_next = ed;
2261da177e4SLinus Torvalds 			ohci->ed_bulktail->hwNextED = cpu_to_hc32 (ohci,
2271da177e4SLinus Torvalds 								ed->dma);
2281da177e4SLinus Torvalds 		}
2291da177e4SLinus Torvalds 		ed->ed_prev = ohci->ed_bulktail;
2301da177e4SLinus Torvalds 		if (!ohci->ed_bulktail && !ohci->ed_rm_list) {
2311da177e4SLinus Torvalds 			wmb();
2321da177e4SLinus Torvalds 			ohci->hc_control |= OHCI_CTRL_BLE;
2331da177e4SLinus Torvalds 			ohci_writel (ohci, 0, &ohci->regs->ed_bulkcurrent);
2341da177e4SLinus Torvalds 			ohci_writel (ohci, ohci->hc_control,
2351da177e4SLinus Torvalds 					&ohci->regs->control);
2361da177e4SLinus Torvalds 		}
2371da177e4SLinus Torvalds 		ohci->ed_bulktail = ed;
2381da177e4SLinus Torvalds 		break;
2391da177e4SLinus Torvalds 
2401da177e4SLinus Torvalds 	// case PIPE_INTERRUPT:
2411da177e4SLinus Torvalds 	// case PIPE_ISOCHRONOUS:
2421da177e4SLinus Torvalds 	default:
2431da177e4SLinus Torvalds 		branch = balance (ohci, ed->interval, ed->load);
2441da177e4SLinus Torvalds 		if (branch < 0) {
2451da177e4SLinus Torvalds 			ohci_dbg (ohci,
2461da177e4SLinus Torvalds 				"ERR %d, interval %d msecs, load %d\n",
2471da177e4SLinus Torvalds 				branch, ed->interval, ed->load);
2481da177e4SLinus Torvalds 			// FIXME if there are TDs queued, fail them!
2491da177e4SLinus Torvalds 			return branch;
2501da177e4SLinus Torvalds 		}
2511da177e4SLinus Torvalds 		ed->branch = branch;
2521da177e4SLinus Torvalds 		periodic_link (ohci, ed);
2531da177e4SLinus Torvalds 	}
2541da177e4SLinus Torvalds 
2551da177e4SLinus Torvalds 	/* the HC may not see the schedule updates yet, but if it does
2561da177e4SLinus Torvalds 	 * then they'll be properly ordered.
2571da177e4SLinus Torvalds 	 */
2581da177e4SLinus Torvalds 	return 0;
2591da177e4SLinus Torvalds }
2601da177e4SLinus Torvalds 
2611da177e4SLinus Torvalds /*-------------------------------------------------------------------------*/
2621da177e4SLinus Torvalds 
2631da177e4SLinus Torvalds /* scan the periodic table to find and unlink this ED */
2641da177e4SLinus Torvalds static void periodic_unlink (struct ohci_hcd *ohci, struct ed *ed)
2651da177e4SLinus Torvalds {
2661da177e4SLinus Torvalds 	int	i;
2671da177e4SLinus Torvalds 
2681da177e4SLinus Torvalds 	for (i = ed->branch; i < NUM_INTS; i += ed->interval) {
2691da177e4SLinus Torvalds 		struct ed	*temp;
2701da177e4SLinus Torvalds 		struct ed	**prev = &ohci->periodic [i];
2711da177e4SLinus Torvalds 		__hc32		*prev_p = &ohci->hcca->int_table [i];
2721da177e4SLinus Torvalds 
2731da177e4SLinus Torvalds 		while (*prev && (temp = *prev) != ed) {
2741da177e4SLinus Torvalds 			prev_p = &temp->hwNextED;
2751da177e4SLinus Torvalds 			prev = &temp->ed_next;
2761da177e4SLinus Torvalds 		}
2771da177e4SLinus Torvalds 		if (*prev) {
2781da177e4SLinus Torvalds 			*prev_p = ed->hwNextED;
2791da177e4SLinus Torvalds 			*prev = ed->ed_next;
2801da177e4SLinus Torvalds 		}
2811da177e4SLinus Torvalds 		ohci->load [i] -= ed->load;
2821da177e4SLinus Torvalds 	}
2831da177e4SLinus Torvalds 	ohci_to_hcd(ohci)->self.bandwidth_allocated -= ed->load / ed->interval;
2841da177e4SLinus Torvalds 
2851da177e4SLinus Torvalds 	ohci_vdbg (ohci, "unlink %sed %p branch %d [%dus.], interval %d\n",
2861da177e4SLinus Torvalds 		(ed->hwINFO & cpu_to_hc32 (ohci, ED_ISO)) ? "iso " : "",
2871da177e4SLinus Torvalds 		ed, ed->branch, ed->load, ed->interval);
2881da177e4SLinus Torvalds }
2891da177e4SLinus Torvalds 
2901da177e4SLinus Torvalds /* unlink an ed from one of the HC chains.
2911da177e4SLinus Torvalds  * just the link to the ed is unlinked.
2921da177e4SLinus Torvalds  * the link from the ed still points to another operational ed or 0
2931da177e4SLinus Torvalds  * so the HC can eventually finish the processing of the unlinked ed
2941da177e4SLinus Torvalds  * (assuming it already started that, which needn't be true).
2951da177e4SLinus Torvalds  *
2961da177e4SLinus Torvalds  * ED_UNLINK is a transient state: the HC may still see this ED, but soon
2971da177e4SLinus Torvalds  * it won't.  ED_SKIP means the HC will finish its current transaction,
2981da177e4SLinus Torvalds  * but won't start anything new.  The TD queue may still grow; device
2991da177e4SLinus Torvalds  * drivers don't know about this HCD-internal state.
3001da177e4SLinus Torvalds  *
3011da177e4SLinus Torvalds  * When the HC can't see the ED, something changes ED_UNLINK to one of:
3021da177e4SLinus Torvalds  *
3031da177e4SLinus Torvalds  *  - ED_OPER: when there's any request queued, the ED gets rescheduled
3041da177e4SLinus Torvalds  *    immediately.  HC should be working on them.
3051da177e4SLinus Torvalds  *
3061da177e4SLinus Torvalds  *  - ED_IDLE:  when there's no TD queue. there's no reason for the HC
3071da177e4SLinus Torvalds  *    to care about this ED; safe to disable the endpoint.
3081da177e4SLinus Torvalds  *
3091da177e4SLinus Torvalds  * When finish_unlinks() runs later, after SOF interrupt, it will often
3101da177e4SLinus Torvalds  * complete one or more URB unlinks before making that state change.
3111da177e4SLinus Torvalds  */
3121da177e4SLinus Torvalds static void ed_deschedule (struct ohci_hcd *ohci, struct ed *ed)
3131da177e4SLinus Torvalds {
3141da177e4SLinus Torvalds 	ed->hwINFO |= cpu_to_hc32 (ohci, ED_SKIP);
3151da177e4SLinus Torvalds 	wmb ();
3161da177e4SLinus Torvalds 	ed->state = ED_UNLINK;
3171da177e4SLinus Torvalds 
3181da177e4SLinus Torvalds 	/* To deschedule something from the control or bulk list, just
3191da177e4SLinus Torvalds 	 * clear CLE/BLE and wait.  There's no safe way to scrub out list
3201da177e4SLinus Torvalds 	 * head/current registers until later, and "later" isn't very
3211da177e4SLinus Torvalds 	 * tightly specified.  Figure 6-5 and Section 6.4.2.2 show how
3221da177e4SLinus Torvalds 	 * the HC is reading the ED queues (while we modify them).
3231da177e4SLinus Torvalds 	 *
3241da177e4SLinus Torvalds 	 * For now, ed_schedule() is "later".  It might be good paranoia
3251da177e4SLinus Torvalds 	 * to scrub those registers in finish_unlinks(), in case of bugs
3261da177e4SLinus Torvalds 	 * that make the HC try to use them.
3271da177e4SLinus Torvalds 	 */
3281da177e4SLinus Torvalds 	switch (ed->type) {
3291da177e4SLinus Torvalds 	case PIPE_CONTROL:
3301da177e4SLinus Torvalds 		/* remove ED from the HC's list: */
3311da177e4SLinus Torvalds 		if (ed->ed_prev == NULL) {
3321da177e4SLinus Torvalds 			if (!ed->hwNextED) {
3331da177e4SLinus Torvalds 				ohci->hc_control &= ~OHCI_CTRL_CLE;
3341da177e4SLinus Torvalds 				ohci_writel (ohci, ohci->hc_control,
3351da177e4SLinus Torvalds 						&ohci->regs->control);
3361da177e4SLinus Torvalds 				// a ohci_readl() later syncs CLE with the HC
3371da177e4SLinus Torvalds 			} else
3381da177e4SLinus Torvalds 				ohci_writel (ohci,
3391da177e4SLinus Torvalds 					hc32_to_cpup (ohci, &ed->hwNextED),
3401da177e4SLinus Torvalds 					&ohci->regs->ed_controlhead);
3411da177e4SLinus Torvalds 		} else {
3421da177e4SLinus Torvalds 			ed->ed_prev->ed_next = ed->ed_next;
3431da177e4SLinus Torvalds 			ed->ed_prev->hwNextED = ed->hwNextED;
3441da177e4SLinus Torvalds 		}
3451da177e4SLinus Torvalds 		/* remove ED from the HCD's list: */
3461da177e4SLinus Torvalds 		if (ohci->ed_controltail == ed) {
3471da177e4SLinus Torvalds 			ohci->ed_controltail = ed->ed_prev;
3481da177e4SLinus Torvalds 			if (ohci->ed_controltail)
3491da177e4SLinus Torvalds 				ohci->ed_controltail->ed_next = NULL;
3501da177e4SLinus Torvalds 		} else if (ed->ed_next) {
3511da177e4SLinus Torvalds 			ed->ed_next->ed_prev = ed->ed_prev;
3521da177e4SLinus Torvalds 		}
3531da177e4SLinus Torvalds 		break;
3541da177e4SLinus Torvalds 
3551da177e4SLinus Torvalds 	case PIPE_BULK:
3561da177e4SLinus Torvalds 		/* remove ED from the HC's list: */
3571da177e4SLinus Torvalds 		if (ed->ed_prev == NULL) {
3581da177e4SLinus Torvalds 			if (!ed->hwNextED) {
3591da177e4SLinus Torvalds 				ohci->hc_control &= ~OHCI_CTRL_BLE;
3601da177e4SLinus Torvalds 				ohci_writel (ohci, ohci->hc_control,
3611da177e4SLinus Torvalds 						&ohci->regs->control);
3621da177e4SLinus Torvalds 				// a ohci_readl() later syncs BLE with the HC
3631da177e4SLinus Torvalds 			} else
3641da177e4SLinus Torvalds 				ohci_writel (ohci,
3651da177e4SLinus Torvalds 					hc32_to_cpup (ohci, &ed->hwNextED),
3661da177e4SLinus Torvalds 					&ohci->regs->ed_bulkhead);
3671da177e4SLinus Torvalds 		} else {
3681da177e4SLinus Torvalds 			ed->ed_prev->ed_next = ed->ed_next;
3691da177e4SLinus Torvalds 			ed->ed_prev->hwNextED = ed->hwNextED;
3701da177e4SLinus Torvalds 		}
3711da177e4SLinus Torvalds 		/* remove ED from the HCD's list: */
3721da177e4SLinus Torvalds 		if (ohci->ed_bulktail == ed) {
3731da177e4SLinus Torvalds 			ohci->ed_bulktail = ed->ed_prev;
3741da177e4SLinus Torvalds 			if (ohci->ed_bulktail)
3751da177e4SLinus Torvalds 				ohci->ed_bulktail->ed_next = NULL;
3761da177e4SLinus Torvalds 		} else if (ed->ed_next) {
3771da177e4SLinus Torvalds 			ed->ed_next->ed_prev = ed->ed_prev;
3781da177e4SLinus Torvalds 		}
3791da177e4SLinus Torvalds 		break;
3801da177e4SLinus Torvalds 
3811da177e4SLinus Torvalds 	// case PIPE_INTERRUPT:
3821da177e4SLinus Torvalds 	// case PIPE_ISOCHRONOUS:
3831da177e4SLinus Torvalds 	default:
3841da177e4SLinus Torvalds 		periodic_unlink (ohci, ed);
3851da177e4SLinus Torvalds 		break;
3861da177e4SLinus Torvalds 	}
3871da177e4SLinus Torvalds }
3881da177e4SLinus Torvalds 
3891da177e4SLinus Torvalds 
3901da177e4SLinus Torvalds /*-------------------------------------------------------------------------*/
3911da177e4SLinus Torvalds 
3921da177e4SLinus Torvalds /* get and maybe (re)init an endpoint. init _should_ be done only as part
3931da177e4SLinus Torvalds  * of enumeration, usb_set_configuration() or usb_set_interface().
3941da177e4SLinus Torvalds  */
3951da177e4SLinus Torvalds static struct ed *ed_get (
3961da177e4SLinus Torvalds 	struct ohci_hcd		*ohci,
3971da177e4SLinus Torvalds 	struct usb_host_endpoint *ep,
3981da177e4SLinus Torvalds 	struct usb_device	*udev,
3991da177e4SLinus Torvalds 	unsigned int		pipe,
4001da177e4SLinus Torvalds 	int			interval
4011da177e4SLinus Torvalds ) {
4021da177e4SLinus Torvalds 	struct ed		*ed;
4031da177e4SLinus Torvalds 	unsigned long		flags;
4041da177e4SLinus Torvalds 
4051da177e4SLinus Torvalds 	spin_lock_irqsave (&ohci->lock, flags);
4061da177e4SLinus Torvalds 
4071da177e4SLinus Torvalds 	if (!(ed = ep->hcpriv)) {
4081da177e4SLinus Torvalds 		struct td	*td;
4091da177e4SLinus Torvalds 		int		is_out;
4101da177e4SLinus Torvalds 		u32		info;
4111da177e4SLinus Torvalds 
4121da177e4SLinus Torvalds 		ed = ed_alloc (ohci, GFP_ATOMIC);
4131da177e4SLinus Torvalds 		if (!ed) {
4141da177e4SLinus Torvalds 			/* out of memory */
4151da177e4SLinus Torvalds 			goto done;
4161da177e4SLinus Torvalds 		}
4171da177e4SLinus Torvalds 
4181da177e4SLinus Torvalds 		/* dummy td; end of td list for ed */
4191da177e4SLinus Torvalds 		td = td_alloc (ohci, GFP_ATOMIC);
4201da177e4SLinus Torvalds 		if (!td) {
4211da177e4SLinus Torvalds 			/* out of memory */
4221da177e4SLinus Torvalds 			ed_free (ohci, ed);
4231da177e4SLinus Torvalds 			ed = NULL;
4241da177e4SLinus Torvalds 			goto done;
4251da177e4SLinus Torvalds 		}
4261da177e4SLinus Torvalds 		ed->dummy = td;
4271da177e4SLinus Torvalds 		ed->hwTailP = cpu_to_hc32 (ohci, td->td_dma);
4281da177e4SLinus Torvalds 		ed->hwHeadP = ed->hwTailP;	/* ED_C, ED_H zeroed */
4291da177e4SLinus Torvalds 		ed->state = ED_IDLE;
4301da177e4SLinus Torvalds 
4311da177e4SLinus Torvalds 		is_out = !(ep->desc.bEndpointAddress & USB_DIR_IN);
4321da177e4SLinus Torvalds 
4331da177e4SLinus Torvalds 		/* FIXME usbcore changes dev->devnum before SET_ADDRESS
4341da177e4SLinus Torvalds 		 * suceeds ... otherwise we wouldn't need "pipe".
4351da177e4SLinus Torvalds 		 */
4361da177e4SLinus Torvalds 		info = usb_pipedevice (pipe);
4371da177e4SLinus Torvalds 		ed->type = usb_pipetype(pipe);
4381da177e4SLinus Torvalds 
4391da177e4SLinus Torvalds 		info |= (ep->desc.bEndpointAddress & ~USB_DIR_IN) << 7;
4401da177e4SLinus Torvalds 		info |= le16_to_cpu(ep->desc.wMaxPacketSize) << 16;
4411da177e4SLinus Torvalds 		if (udev->speed == USB_SPEED_LOW)
4421da177e4SLinus Torvalds 			info |= ED_LOWSPEED;
4431da177e4SLinus Torvalds 		/* only control transfers store pids in tds */
4441da177e4SLinus Torvalds 		if (ed->type != PIPE_CONTROL) {
4451da177e4SLinus Torvalds 			info |= is_out ? ED_OUT : ED_IN;
4461da177e4SLinus Torvalds 			if (ed->type != PIPE_BULK) {
4471da177e4SLinus Torvalds 				/* periodic transfers... */
4481da177e4SLinus Torvalds 				if (ed->type == PIPE_ISOCHRONOUS)
4491da177e4SLinus Torvalds 					info |= ED_ISO;
4501da177e4SLinus Torvalds 				else if (interval > 32)	/* iso can be bigger */
4511da177e4SLinus Torvalds 					interval = 32;
4521da177e4SLinus Torvalds 				ed->interval = interval;
4531da177e4SLinus Torvalds 				ed->load = usb_calc_bus_time (
4541da177e4SLinus Torvalds 					udev->speed, !is_out,
4551da177e4SLinus Torvalds 					ed->type == PIPE_ISOCHRONOUS,
4561da177e4SLinus Torvalds 					le16_to_cpu(ep->desc.wMaxPacketSize))
4571da177e4SLinus Torvalds 						/ 1000;
4581da177e4SLinus Torvalds 			}
4591da177e4SLinus Torvalds 		}
4601da177e4SLinus Torvalds 		ed->hwINFO = cpu_to_hc32(ohci, info);
4611da177e4SLinus Torvalds 
4621da177e4SLinus Torvalds 		ep->hcpriv = ed;
4631da177e4SLinus Torvalds 	}
4641da177e4SLinus Torvalds 
4651da177e4SLinus Torvalds done:
4661da177e4SLinus Torvalds 	spin_unlock_irqrestore (&ohci->lock, flags);
4671da177e4SLinus Torvalds 	return ed;
4681da177e4SLinus Torvalds }
4691da177e4SLinus Torvalds 
4701da177e4SLinus Torvalds /*-------------------------------------------------------------------------*/
4711da177e4SLinus Torvalds 
4721da177e4SLinus Torvalds /* request unlinking of an endpoint from an operational HC.
4731da177e4SLinus Torvalds  * put the ep on the rm_list
4741da177e4SLinus Torvalds  * real work is done at the next start frame (SF) hardware interrupt
4751da177e4SLinus Torvalds  * caller guarantees HCD is running, so hardware access is safe,
4761da177e4SLinus Torvalds  * and that ed->state is ED_OPER
4771da177e4SLinus Torvalds  */
4781da177e4SLinus Torvalds static void start_ed_unlink (struct ohci_hcd *ohci, struct ed *ed)
4791da177e4SLinus Torvalds {
4801da177e4SLinus Torvalds 	ed->hwINFO |= cpu_to_hc32 (ohci, ED_DEQUEUE);
4811da177e4SLinus Torvalds 	ed_deschedule (ohci, ed);
4821da177e4SLinus Torvalds 
4831da177e4SLinus Torvalds 	/* rm_list is just singly linked, for simplicity */
4841da177e4SLinus Torvalds 	ed->ed_next = ohci->ed_rm_list;
4851da177e4SLinus Torvalds 	ed->ed_prev = NULL;
4861da177e4SLinus Torvalds 	ohci->ed_rm_list = ed;
4871da177e4SLinus Torvalds 
4881da177e4SLinus Torvalds 	/* enable SOF interrupt */
4891da177e4SLinus Torvalds 	ohci_writel (ohci, OHCI_INTR_SF, &ohci->regs->intrstatus);
4901da177e4SLinus Torvalds 	ohci_writel (ohci, OHCI_INTR_SF, &ohci->regs->intrenable);
4911da177e4SLinus Torvalds 	// flush those writes, and get latest HCCA contents
4921da177e4SLinus Torvalds 	(void) ohci_readl (ohci, &ohci->regs->control);
4931da177e4SLinus Torvalds 
4941da177e4SLinus Torvalds 	/* SF interrupt might get delayed; record the frame counter value that
4951da177e4SLinus Torvalds 	 * indicates when the HC isn't looking at it, so concurrent unlinks
4961da177e4SLinus Torvalds 	 * behave.  frame_no wraps every 2^16 msec, and changes right before
4971da177e4SLinus Torvalds 	 * SF is triggered.
4981da177e4SLinus Torvalds 	 */
4991da177e4SLinus Torvalds 	ed->tick = ohci_frame_no(ohci) + 1;
5001da177e4SLinus Torvalds 
5011da177e4SLinus Torvalds }
5021da177e4SLinus Torvalds 
5031da177e4SLinus Torvalds /*-------------------------------------------------------------------------*
5041da177e4SLinus Torvalds  * TD handling functions
5051da177e4SLinus Torvalds  *-------------------------------------------------------------------------*/
5061da177e4SLinus Torvalds 
5071da177e4SLinus Torvalds /* enqueue next TD for this URB (OHCI spec 5.2.8.2) */
5081da177e4SLinus Torvalds 
5091da177e4SLinus Torvalds static void
5101da177e4SLinus Torvalds td_fill (struct ohci_hcd *ohci, u32 info,
5111da177e4SLinus Torvalds 	dma_addr_t data, int len,
5121da177e4SLinus Torvalds 	struct urb *urb, int index)
5131da177e4SLinus Torvalds {
5141da177e4SLinus Torvalds 	struct td		*td, *td_pt;
5151da177e4SLinus Torvalds 	struct urb_priv		*urb_priv = urb->hcpriv;
5161da177e4SLinus Torvalds 	int			is_iso = info & TD_ISO;
5171da177e4SLinus Torvalds 	int			hash;
5181da177e4SLinus Torvalds 
5191da177e4SLinus Torvalds 	// ASSERT (index < urb_priv->length);
5201da177e4SLinus Torvalds 
5211da177e4SLinus Torvalds 	/* aim for only one interrupt per urb.  mostly applies to control
5221da177e4SLinus Torvalds 	 * and iso; other urbs rarely need more than one TD per urb.
5231da177e4SLinus Torvalds 	 * this way, only final tds (or ones with an error) cause IRQs.
5241da177e4SLinus Torvalds 	 * at least immediately; use DI=6 in case any control request is
5251da177e4SLinus Torvalds 	 * tempted to die part way through.  (and to force the hc to flush
5261da177e4SLinus Torvalds 	 * its donelist soonish, even on unlink paths.)
5271da177e4SLinus Torvalds 	 *
5281da177e4SLinus Torvalds 	 * NOTE: could delay interrupts even for the last TD, and get fewer
5291da177e4SLinus Torvalds 	 * interrupts ... increasing per-urb latency by sharing interrupts.
5301da177e4SLinus Torvalds 	 * Drivers that queue bulk urbs may request that behavior.
5311da177e4SLinus Torvalds 	 */
5321da177e4SLinus Torvalds 	if (index != (urb_priv->length - 1)
5331da177e4SLinus Torvalds 			|| (urb->transfer_flags & URB_NO_INTERRUPT))
5341da177e4SLinus Torvalds 		info |= TD_DI_SET (6);
5351da177e4SLinus Torvalds 
5361da177e4SLinus Torvalds 	/* use this td as the next dummy */
5371da177e4SLinus Torvalds 	td_pt = urb_priv->td [index];
5381da177e4SLinus Torvalds 
5391da177e4SLinus Torvalds 	/* fill the old dummy TD */
5401da177e4SLinus Torvalds 	td = urb_priv->td [index] = urb_priv->ed->dummy;
5411da177e4SLinus Torvalds 	urb_priv->ed->dummy = td_pt;
5421da177e4SLinus Torvalds 
5431da177e4SLinus Torvalds 	td->ed = urb_priv->ed;
5441da177e4SLinus Torvalds 	td->next_dl_td = NULL;
5451da177e4SLinus Torvalds 	td->index = index;
5461da177e4SLinus Torvalds 	td->urb = urb;
5471da177e4SLinus Torvalds 	td->data_dma = data;
5481da177e4SLinus Torvalds 	if (!len)
5491da177e4SLinus Torvalds 		data = 0;
5501da177e4SLinus Torvalds 
5511da177e4SLinus Torvalds 	td->hwINFO = cpu_to_hc32 (ohci, info);
5521da177e4SLinus Torvalds 	if (is_iso) {
5531da177e4SLinus Torvalds 		td->hwCBP = cpu_to_hc32 (ohci, data & 0xFFFFF000);
5541da177e4SLinus Torvalds 		*ohci_hwPSWp(ohci, td, 0) = cpu_to_hc16 (ohci,
5551da177e4SLinus Torvalds 						(data & 0x0FFF) | 0xE000);
5561da177e4SLinus Torvalds 		td->ed->last_iso = info & 0xffff;
5571da177e4SLinus Torvalds 	} else {
5581da177e4SLinus Torvalds 		td->hwCBP = cpu_to_hc32 (ohci, data);
5591da177e4SLinus Torvalds 	}
5601da177e4SLinus Torvalds 	if (data)
5611da177e4SLinus Torvalds 		td->hwBE = cpu_to_hc32 (ohci, data + len - 1);
5621da177e4SLinus Torvalds 	else
5631da177e4SLinus Torvalds 		td->hwBE = 0;
5641da177e4SLinus Torvalds 	td->hwNextTD = cpu_to_hc32 (ohci, td_pt->td_dma);
5651da177e4SLinus Torvalds 
5661da177e4SLinus Torvalds 	/* append to queue */
5671da177e4SLinus Torvalds 	list_add_tail (&td->td_list, &td->ed->td_list);
5681da177e4SLinus Torvalds 
5691da177e4SLinus Torvalds 	/* hash it for later reverse mapping */
5701da177e4SLinus Torvalds 	hash = TD_HASH_FUNC (td->td_dma);
5711da177e4SLinus Torvalds 	td->td_hash = ohci->td_hash [hash];
5721da177e4SLinus Torvalds 	ohci->td_hash [hash] = td;
5731da177e4SLinus Torvalds 
5741da177e4SLinus Torvalds 	/* HC might read the TD (or cachelines) right away ... */
5751da177e4SLinus Torvalds 	wmb ();
5761da177e4SLinus Torvalds 	td->ed->hwTailP = td->hwNextTD;
5771da177e4SLinus Torvalds }
5781da177e4SLinus Torvalds 
5791da177e4SLinus Torvalds /*-------------------------------------------------------------------------*/
5801da177e4SLinus Torvalds 
5811da177e4SLinus Torvalds /* Prepare all TDs of a transfer, and queue them onto the ED.
5821da177e4SLinus Torvalds  * Caller guarantees HC is active.
5831da177e4SLinus Torvalds  * Usually the ED is already on the schedule, so TDs might be
5841da177e4SLinus Torvalds  * processed as soon as they're queued.
5851da177e4SLinus Torvalds  */
5861da177e4SLinus Torvalds static void td_submit_urb (
5871da177e4SLinus Torvalds 	struct ohci_hcd	*ohci,
5881da177e4SLinus Torvalds 	struct urb	*urb
5891da177e4SLinus Torvalds ) {
5901da177e4SLinus Torvalds 	struct urb_priv	*urb_priv = urb->hcpriv;
5911da177e4SLinus Torvalds 	dma_addr_t	data;
5921da177e4SLinus Torvalds 	int		data_len = urb->transfer_buffer_length;
5931da177e4SLinus Torvalds 	int		cnt = 0;
5941da177e4SLinus Torvalds 	u32		info = 0;
5951da177e4SLinus Torvalds 	int		is_out = usb_pipeout (urb->pipe);
5961da177e4SLinus Torvalds 	int		periodic = 0;
5971da177e4SLinus Torvalds 
5981da177e4SLinus Torvalds 	/* OHCI handles the bulk/interrupt data toggles itself.  We just
5991da177e4SLinus Torvalds 	 * use the device toggle bits for resetting, and rely on the fact
6001da177e4SLinus Torvalds 	 * that resetting toggle is meaningless if the endpoint is active.
6011da177e4SLinus Torvalds 	 */
6021da177e4SLinus Torvalds 	if (!usb_gettoggle (urb->dev, usb_pipeendpoint (urb->pipe), is_out)) {
6031da177e4SLinus Torvalds 		usb_settoggle (urb->dev, usb_pipeendpoint (urb->pipe),
6041da177e4SLinus Torvalds 			is_out, 1);
6051da177e4SLinus Torvalds 		urb_priv->ed->hwHeadP &= ~cpu_to_hc32 (ohci, ED_C);
6061da177e4SLinus Torvalds 	}
6071da177e4SLinus Torvalds 
6081da177e4SLinus Torvalds 	urb_priv->td_cnt = 0;
6091da177e4SLinus Torvalds 	list_add (&urb_priv->pending, &ohci->pending);
6101da177e4SLinus Torvalds 
6111da177e4SLinus Torvalds 	if (data_len)
6121da177e4SLinus Torvalds 		data = urb->transfer_dma;
6131da177e4SLinus Torvalds 	else
6141da177e4SLinus Torvalds 		data = 0;
6151da177e4SLinus Torvalds 
6161da177e4SLinus Torvalds 	/* NOTE:  TD_CC is set so we can tell which TDs the HC processed by
6171da177e4SLinus Torvalds 	 * using TD_CC_GET, as well as by seeing them on the done list.
6181da177e4SLinus Torvalds 	 * (CC = NotAccessed ... 0x0F, or 0x0E in PSWs for ISO.)
6191da177e4SLinus Torvalds 	 */
6201da177e4SLinus Torvalds 	switch (urb_priv->ed->type) {
6211da177e4SLinus Torvalds 
6221da177e4SLinus Torvalds 	/* Bulk and interrupt are identical except for where in the schedule
6231da177e4SLinus Torvalds 	 * their EDs live.
6241da177e4SLinus Torvalds 	 */
6251da177e4SLinus Torvalds 	case PIPE_INTERRUPT:
6261da177e4SLinus Torvalds 		/* ... and periodic urbs have extra accounting */
6271da177e4SLinus Torvalds 		periodic = ohci_to_hcd(ohci)->self.bandwidth_int_reqs++ == 0
6281da177e4SLinus Torvalds 			&& ohci_to_hcd(ohci)->self.bandwidth_isoc_reqs == 0;
6291da177e4SLinus Torvalds 		/* FALLTHROUGH */
6301da177e4SLinus Torvalds 	case PIPE_BULK:
6311da177e4SLinus Torvalds 		info = is_out
6321da177e4SLinus Torvalds 			? TD_T_TOGGLE | TD_CC | TD_DP_OUT
6331da177e4SLinus Torvalds 			: TD_T_TOGGLE | TD_CC | TD_DP_IN;
6341da177e4SLinus Torvalds 		/* TDs _could_ transfer up to 8K each */
6351da177e4SLinus Torvalds 		while (data_len > 4096) {
6361da177e4SLinus Torvalds 			td_fill (ohci, info, data, 4096, urb, cnt);
6371da177e4SLinus Torvalds 			data += 4096;
6381da177e4SLinus Torvalds 			data_len -= 4096;
6391da177e4SLinus Torvalds 			cnt++;
6401da177e4SLinus Torvalds 		}
6411da177e4SLinus Torvalds 		/* maybe avoid ED halt on final TD short read */
6421da177e4SLinus Torvalds 		if (!(urb->transfer_flags & URB_SHORT_NOT_OK))
6431da177e4SLinus Torvalds 			info |= TD_R;
6441da177e4SLinus Torvalds 		td_fill (ohci, info, data, data_len, urb, cnt);
6451da177e4SLinus Torvalds 		cnt++;
6461da177e4SLinus Torvalds 		if ((urb->transfer_flags & URB_ZERO_PACKET)
6471da177e4SLinus Torvalds 				&& cnt < urb_priv->length) {
6481da177e4SLinus Torvalds 			td_fill (ohci, info, 0, 0, urb, cnt);
6491da177e4SLinus Torvalds 			cnt++;
6501da177e4SLinus Torvalds 		}
6511da177e4SLinus Torvalds 		/* maybe kickstart bulk list */
6521da177e4SLinus Torvalds 		if (urb_priv->ed->type == PIPE_BULK) {
6531da177e4SLinus Torvalds 			wmb ();
6541da177e4SLinus Torvalds 			ohci_writel (ohci, OHCI_BLF, &ohci->regs->cmdstatus);
6551da177e4SLinus Torvalds 		}
6561da177e4SLinus Torvalds 		break;
6571da177e4SLinus Torvalds 
6581da177e4SLinus Torvalds 	/* control manages DATA0/DATA1 toggle per-request; SETUP resets it,
6591da177e4SLinus Torvalds 	 * any DATA phase works normally, and the STATUS ack is special.
6601da177e4SLinus Torvalds 	 */
6611da177e4SLinus Torvalds 	case PIPE_CONTROL:
6621da177e4SLinus Torvalds 		info = TD_CC | TD_DP_SETUP | TD_T_DATA0;
6631da177e4SLinus Torvalds 		td_fill (ohci, info, urb->setup_dma, 8, urb, cnt++);
6641da177e4SLinus Torvalds 		if (data_len > 0) {
6651da177e4SLinus Torvalds 			info = TD_CC | TD_R | TD_T_DATA1;
6661da177e4SLinus Torvalds 			info |= is_out ? TD_DP_OUT : TD_DP_IN;
6671da177e4SLinus Torvalds 			/* NOTE:  mishandles transfers >8K, some >4K */
6681da177e4SLinus Torvalds 			td_fill (ohci, info, data, data_len, urb, cnt++);
6691da177e4SLinus Torvalds 		}
6701da177e4SLinus Torvalds 		info = (is_out || data_len == 0)
6711da177e4SLinus Torvalds 			? TD_CC | TD_DP_IN | TD_T_DATA1
6721da177e4SLinus Torvalds 			: TD_CC | TD_DP_OUT | TD_T_DATA1;
6731da177e4SLinus Torvalds 		td_fill (ohci, info, data, 0, urb, cnt++);
6741da177e4SLinus Torvalds 		/* maybe kickstart control list */
6751da177e4SLinus Torvalds 		wmb ();
6761da177e4SLinus Torvalds 		ohci_writel (ohci, OHCI_CLF, &ohci->regs->cmdstatus);
6771da177e4SLinus Torvalds 		break;
6781da177e4SLinus Torvalds 
6791da177e4SLinus Torvalds 	/* ISO has no retransmit, so no toggle; and it uses special TDs.
6801da177e4SLinus Torvalds 	 * Each TD could handle multiple consecutive frames (interval 1);
6811da177e4SLinus Torvalds 	 * we could often reduce the number of TDs here.
6821da177e4SLinus Torvalds 	 */
6831da177e4SLinus Torvalds 	case PIPE_ISOCHRONOUS:
6841da177e4SLinus Torvalds 		for (cnt = 0; cnt < urb->number_of_packets; cnt++) {
6851da177e4SLinus Torvalds 			int	frame = urb->start_frame;
6861da177e4SLinus Torvalds 
6871da177e4SLinus Torvalds 			// FIXME scheduling should handle frame counter
6881da177e4SLinus Torvalds 			// roll-around ... exotic case (and OHCI has
6891da177e4SLinus Torvalds 			// a 2^16 iso range, vs other HCs max of 2^10)
6901da177e4SLinus Torvalds 			frame += cnt * urb->interval;
6911da177e4SLinus Torvalds 			frame &= 0xffff;
6921da177e4SLinus Torvalds 			td_fill (ohci, TD_CC | TD_ISO | frame,
6931da177e4SLinus Torvalds 				data + urb->iso_frame_desc [cnt].offset,
6941da177e4SLinus Torvalds 				urb->iso_frame_desc [cnt].length, urb, cnt);
6951da177e4SLinus Torvalds 		}
6961da177e4SLinus Torvalds 		periodic = ohci_to_hcd(ohci)->self.bandwidth_isoc_reqs++ == 0
6971da177e4SLinus Torvalds 			&& ohci_to_hcd(ohci)->self.bandwidth_int_reqs == 0;
6981da177e4SLinus Torvalds 		break;
6991da177e4SLinus Torvalds 	}
7001da177e4SLinus Torvalds 
7011da177e4SLinus Torvalds 	/* start periodic dma if needed */
7021da177e4SLinus Torvalds 	if (periodic) {
7031da177e4SLinus Torvalds 		wmb ();
7041da177e4SLinus Torvalds 		ohci->hc_control |= OHCI_CTRL_PLE|OHCI_CTRL_IE;
7051da177e4SLinus Torvalds 		ohci_writel (ohci, ohci->hc_control, &ohci->regs->control);
7061da177e4SLinus Torvalds 	}
7071da177e4SLinus Torvalds 
7081da177e4SLinus Torvalds 	// ASSERT (urb_priv->length == cnt);
7091da177e4SLinus Torvalds }
7101da177e4SLinus Torvalds 
7111da177e4SLinus Torvalds /*-------------------------------------------------------------------------*
7121da177e4SLinus Torvalds  * Done List handling functions
7131da177e4SLinus Torvalds  *-------------------------------------------------------------------------*/
7141da177e4SLinus Torvalds 
7151da177e4SLinus Torvalds /* calculate transfer length/status and update the urb
7161da177e4SLinus Torvalds  * PRECONDITION:  irqsafe (only for urb->status locking)
7171da177e4SLinus Torvalds  */
7181da177e4SLinus Torvalds static void td_done (struct ohci_hcd *ohci, struct urb *urb, struct td *td)
7191da177e4SLinus Torvalds {
7201da177e4SLinus Torvalds 	u32	tdINFO = hc32_to_cpup (ohci, &td->hwINFO);
7211da177e4SLinus Torvalds 	int	cc = 0;
7221da177e4SLinus Torvalds 
7231da177e4SLinus Torvalds 	list_del (&td->td_list);
7241da177e4SLinus Torvalds 
7251da177e4SLinus Torvalds 	/* ISO ... drivers see per-TD length/status */
7261da177e4SLinus Torvalds 	if (tdINFO & TD_ISO) {
7271da177e4SLinus Torvalds 		u16	tdPSW = ohci_hwPSW (ohci, td, 0);
7281da177e4SLinus Torvalds 		int	dlen = 0;
7291da177e4SLinus Torvalds 
7301da177e4SLinus Torvalds 		/* NOTE:  assumes FC in tdINFO == 0, and that
7311da177e4SLinus Torvalds 		 * only the first of 0..MAXPSW psws is used.
7321da177e4SLinus Torvalds 		 */
7331da177e4SLinus Torvalds 
7341da177e4SLinus Torvalds 		cc = (tdPSW >> 12) & 0xF;
7351da177e4SLinus Torvalds 		if (tdINFO & TD_CC)	/* hc didn't touch? */
7361da177e4SLinus Torvalds 			return;
7371da177e4SLinus Torvalds 
7381da177e4SLinus Torvalds 		if (usb_pipeout (urb->pipe))
7391da177e4SLinus Torvalds 			dlen = urb->iso_frame_desc [td->index].length;
7401da177e4SLinus Torvalds 		else {
7411da177e4SLinus Torvalds 			/* short reads are always OK for ISO */
7421da177e4SLinus Torvalds 			if (cc == TD_DATAUNDERRUN)
7431da177e4SLinus Torvalds 				cc = TD_CC_NOERROR;
7441da177e4SLinus Torvalds 			dlen = tdPSW & 0x3ff;
7451da177e4SLinus Torvalds 		}
7461da177e4SLinus Torvalds 		urb->actual_length += dlen;
7471da177e4SLinus Torvalds 		urb->iso_frame_desc [td->index].actual_length = dlen;
7481da177e4SLinus Torvalds 		urb->iso_frame_desc [td->index].status = cc_to_error [cc];
7491da177e4SLinus Torvalds 
7501da177e4SLinus Torvalds 		if (cc != TD_CC_NOERROR)
7511da177e4SLinus Torvalds 			ohci_vdbg (ohci,
7521da177e4SLinus Torvalds 				"urb %p iso td %p (%d) len %d cc %d\n",
7531da177e4SLinus Torvalds 				urb, td, 1 + td->index, dlen, cc);
7541da177e4SLinus Torvalds 
7551da177e4SLinus Torvalds 	/* BULK, INT, CONTROL ... drivers see aggregate length/status,
7561da177e4SLinus Torvalds 	 * except that "setup" bytes aren't counted and "short" transfers
7571da177e4SLinus Torvalds 	 * might not be reported as errors.
7581da177e4SLinus Torvalds 	 */
7591da177e4SLinus Torvalds 	} else {
7601da177e4SLinus Torvalds 		int	type = usb_pipetype (urb->pipe);
7611da177e4SLinus Torvalds 		u32	tdBE = hc32_to_cpup (ohci, &td->hwBE);
7621da177e4SLinus Torvalds 
7631da177e4SLinus Torvalds 		cc = TD_CC_GET (tdINFO);
7641da177e4SLinus Torvalds 
7651da177e4SLinus Torvalds 		/* update packet status if needed (short is normally ok) */
7661da177e4SLinus Torvalds 		if (cc == TD_DATAUNDERRUN
7671da177e4SLinus Torvalds 				&& !(urb->transfer_flags & URB_SHORT_NOT_OK))
7681da177e4SLinus Torvalds 			cc = TD_CC_NOERROR;
7691da177e4SLinus Torvalds 		if (cc != TD_CC_NOERROR && cc < 0x0E) {
7701da177e4SLinus Torvalds 			spin_lock (&urb->lock);
7711da177e4SLinus Torvalds 			if (urb->status == -EINPROGRESS)
7721da177e4SLinus Torvalds 				urb->status = cc_to_error [cc];
7731da177e4SLinus Torvalds 			spin_unlock (&urb->lock);
7741da177e4SLinus Torvalds 		}
7751da177e4SLinus Torvalds 
7761da177e4SLinus Torvalds 		/* count all non-empty packets except control SETUP packet */
7771da177e4SLinus Torvalds 		if ((type != PIPE_CONTROL || td->index != 0) && tdBE != 0) {
7781da177e4SLinus Torvalds 			if (td->hwCBP == 0)
7791da177e4SLinus Torvalds 				urb->actual_length += tdBE - td->data_dma + 1;
7801da177e4SLinus Torvalds 			else
7811da177e4SLinus Torvalds 				urb->actual_length +=
7821da177e4SLinus Torvalds 					  hc32_to_cpup (ohci, &td->hwCBP)
7831da177e4SLinus Torvalds 					- td->data_dma;
7841da177e4SLinus Torvalds 		}
7851da177e4SLinus Torvalds 
7861da177e4SLinus Torvalds 		if (cc != TD_CC_NOERROR && cc < 0x0E)
7871da177e4SLinus Torvalds 			ohci_vdbg (ohci,
7881da177e4SLinus Torvalds 				"urb %p td %p (%d) cc %d, len=%d/%d\n",
7891da177e4SLinus Torvalds 				urb, td, 1 + td->index, cc,
7901da177e4SLinus Torvalds 				urb->actual_length,
7911da177e4SLinus Torvalds 				urb->transfer_buffer_length);
7921da177e4SLinus Torvalds 	}
7931da177e4SLinus Torvalds }
7941da177e4SLinus Torvalds 
7951da177e4SLinus Torvalds /*-------------------------------------------------------------------------*/
7961da177e4SLinus Torvalds 
7971da177e4SLinus Torvalds static inline struct td *
7981da177e4SLinus Torvalds ed_halted (struct ohci_hcd *ohci, struct td *td, int cc, struct td *rev)
7991da177e4SLinus Torvalds {
8001da177e4SLinus Torvalds 	struct urb		*urb = td->urb;
8011da177e4SLinus Torvalds 	struct ed		*ed = td->ed;
8021da177e4SLinus Torvalds 	struct list_head	*tmp = td->td_list.next;
8031da177e4SLinus Torvalds 	__hc32			toggle = ed->hwHeadP & cpu_to_hc32 (ohci, ED_C);
8041da177e4SLinus Torvalds 
8051da177e4SLinus Torvalds 	/* clear ed halt; this is the td that caused it, but keep it inactive
8061da177e4SLinus Torvalds 	 * until its urb->complete() has a chance to clean up.
8071da177e4SLinus Torvalds 	 */
8081da177e4SLinus Torvalds 	ed->hwINFO |= cpu_to_hc32 (ohci, ED_SKIP);
8091da177e4SLinus Torvalds 	wmb ();
8101da177e4SLinus Torvalds 	ed->hwHeadP &= ~cpu_to_hc32 (ohci, ED_H);
8111da177e4SLinus Torvalds 
8121da177e4SLinus Torvalds 	/* put any later tds from this urb onto the donelist, after 'td',
8131da177e4SLinus Torvalds 	 * order won't matter here: no errors, and nothing was transferred.
8141da177e4SLinus Torvalds 	 * also patch the ed so it looks as if those tds completed normally.
8151da177e4SLinus Torvalds 	 */
8161da177e4SLinus Torvalds 	while (tmp != &ed->td_list) {
8171da177e4SLinus Torvalds 		struct td	*next;
8181da177e4SLinus Torvalds 		__hc32		info;
8191da177e4SLinus Torvalds 
8201da177e4SLinus Torvalds 		next = list_entry (tmp, struct td, td_list);
8211da177e4SLinus Torvalds 		tmp = next->td_list.next;
8221da177e4SLinus Torvalds 
8231da177e4SLinus Torvalds 		if (next->urb != urb)
8241da177e4SLinus Torvalds 			break;
8251da177e4SLinus Torvalds 
8261da177e4SLinus Torvalds 		/* NOTE: if multi-td control DATA segments get supported,
8271da177e4SLinus Torvalds 		 * this urb had one of them, this td wasn't the last td
8281da177e4SLinus Torvalds 		 * in that segment (TD_R clear), this ed halted because
8291da177e4SLinus Torvalds 		 * of a short read, _and_ URB_SHORT_NOT_OK is clear ...
8301da177e4SLinus Torvalds 		 * then we need to leave the control STATUS packet queued
8311da177e4SLinus Torvalds 		 * and clear ED_SKIP.
8321da177e4SLinus Torvalds 		 */
8331da177e4SLinus Torvalds 		info = next->hwINFO;
8341da177e4SLinus Torvalds 		info |= cpu_to_hc32 (ohci, TD_DONE);
8351da177e4SLinus Torvalds 		info &= ~cpu_to_hc32 (ohci, TD_CC);
8361da177e4SLinus Torvalds 		next->hwINFO = info;
8371da177e4SLinus Torvalds 
8381da177e4SLinus Torvalds 		next->next_dl_td = rev;
8391da177e4SLinus Torvalds 		rev = next;
8401da177e4SLinus Torvalds 
8411da177e4SLinus Torvalds 		ed->hwHeadP = next->hwNextTD | toggle;
8421da177e4SLinus Torvalds 	}
8431da177e4SLinus Torvalds 
8441da177e4SLinus Torvalds 	/* help for troubleshooting:  report anything that
8451da177e4SLinus Torvalds 	 * looks odd ... that doesn't include protocol stalls
8461da177e4SLinus Torvalds 	 * (or maybe some other things)
8471da177e4SLinus Torvalds 	 */
8481da177e4SLinus Torvalds 	switch (cc) {
8491da177e4SLinus Torvalds 	case TD_DATAUNDERRUN:
8501da177e4SLinus Torvalds 		if ((urb->transfer_flags & URB_SHORT_NOT_OK) == 0)
8511da177e4SLinus Torvalds 			break;
8521da177e4SLinus Torvalds 		/* fallthrough */
8531da177e4SLinus Torvalds 	case TD_CC_STALL:
8541da177e4SLinus Torvalds 		if (usb_pipecontrol (urb->pipe))
8551da177e4SLinus Torvalds 			break;
8561da177e4SLinus Torvalds 		/* fallthrough */
8571da177e4SLinus Torvalds 	default:
8581da177e4SLinus Torvalds 		ohci_dbg (ohci,
8591da177e4SLinus Torvalds 			"urb %p path %s ep%d%s %08x cc %d --> status %d\n",
8601da177e4SLinus Torvalds 			urb, urb->dev->devpath,
8611da177e4SLinus Torvalds 			usb_pipeendpoint (urb->pipe),
8621da177e4SLinus Torvalds 			usb_pipein (urb->pipe) ? "in" : "out",
8631da177e4SLinus Torvalds 			hc32_to_cpu (ohci, td->hwINFO),
8641da177e4SLinus Torvalds 			cc, cc_to_error [cc]);
8651da177e4SLinus Torvalds 	}
8661da177e4SLinus Torvalds 
8671da177e4SLinus Torvalds 	return rev;
8681da177e4SLinus Torvalds }
8691da177e4SLinus Torvalds 
8701da177e4SLinus Torvalds /* replies to the request have to be on a FIFO basis so
8711da177e4SLinus Torvalds  * we unreverse the hc-reversed done-list
8721da177e4SLinus Torvalds  */
8731da177e4SLinus Torvalds static struct td *dl_reverse_done_list (struct ohci_hcd *ohci)
8741da177e4SLinus Torvalds {
8751da177e4SLinus Torvalds 	u32		td_dma;
8761da177e4SLinus Torvalds 	struct td	*td_rev = NULL;
8771da177e4SLinus Torvalds 	struct td	*td = NULL;
8781da177e4SLinus Torvalds 
8791da177e4SLinus Torvalds 	td_dma = hc32_to_cpup (ohci, &ohci->hcca->done_head);
8801da177e4SLinus Torvalds 	ohci->hcca->done_head = 0;
8811da177e4SLinus Torvalds 	wmb();
8821da177e4SLinus Torvalds 
8831da177e4SLinus Torvalds 	/* get TD from hc's singly linked list, and
8841da177e4SLinus Torvalds 	 * prepend to ours.  ed->td_list changes later.
8851da177e4SLinus Torvalds 	 */
8861da177e4SLinus Torvalds 	while (td_dma) {
8871da177e4SLinus Torvalds 		int		cc;
8881da177e4SLinus Torvalds 
8891da177e4SLinus Torvalds 		td = dma_to_td (ohci, td_dma);
8901da177e4SLinus Torvalds 		if (!td) {
8911da177e4SLinus Torvalds 			ohci_err (ohci, "bad entry %8x\n", td_dma);
8921da177e4SLinus Torvalds 			break;
8931da177e4SLinus Torvalds 		}
8941da177e4SLinus Torvalds 
8951da177e4SLinus Torvalds 		td->hwINFO |= cpu_to_hc32 (ohci, TD_DONE);
8961da177e4SLinus Torvalds 		cc = TD_CC_GET (hc32_to_cpup (ohci, &td->hwINFO));
8971da177e4SLinus Torvalds 
8981da177e4SLinus Torvalds 		/* Non-iso endpoints can halt on error; un-halt,
8991da177e4SLinus Torvalds 		 * and dequeue any other TDs from this urb.
9001da177e4SLinus Torvalds 		 * No other TD could have caused the halt.
9011da177e4SLinus Torvalds 		 */
9021da177e4SLinus Torvalds 		if (cc != TD_CC_NOERROR
9031da177e4SLinus Torvalds 				&& (td->ed->hwHeadP & cpu_to_hc32 (ohci, ED_H)))
9041da177e4SLinus Torvalds 			td_rev = ed_halted (ohci, td, cc, td_rev);
9051da177e4SLinus Torvalds 
9061da177e4SLinus Torvalds 		td->next_dl_td = td_rev;
9071da177e4SLinus Torvalds 		td_rev = td;
9081da177e4SLinus Torvalds 		td_dma = hc32_to_cpup (ohci, &td->hwNextTD);
9091da177e4SLinus Torvalds 	}
9101da177e4SLinus Torvalds 	return td_rev;
9111da177e4SLinus Torvalds }
9121da177e4SLinus Torvalds 
9131da177e4SLinus Torvalds /*-------------------------------------------------------------------------*/
9141da177e4SLinus Torvalds 
9151da177e4SLinus Torvalds /* there are some urbs/eds to unlink; called in_irq(), with HCD locked */
9161da177e4SLinus Torvalds static void
9177d12e780SDavid Howells finish_unlinks (struct ohci_hcd *ohci, u16 tick)
9181da177e4SLinus Torvalds {
9191da177e4SLinus Torvalds 	struct ed	*ed, **last;
9201da177e4SLinus Torvalds 
9211da177e4SLinus Torvalds rescan_all:
9221da177e4SLinus Torvalds 	for (last = &ohci->ed_rm_list, ed = *last; ed != NULL; ed = *last) {
9231da177e4SLinus Torvalds 		struct list_head	*entry, *tmp;
9241da177e4SLinus Torvalds 		int			completed, modified;
9251da177e4SLinus Torvalds 		__hc32			*prev;
9261da177e4SLinus Torvalds 
9271da177e4SLinus Torvalds 		/* only take off EDs that the HC isn't using, accounting for
9281da177e4SLinus Torvalds 		 * frame counter wraps and EDs with partially retired TDs
9291da177e4SLinus Torvalds 		 */
930da66b719SDavid Brownell 		if (likely (HC_IS_RUNNING(ohci_to_hcd(ohci)->state))) {
9311da177e4SLinus Torvalds 			if (tick_before (tick, ed->tick)) {
9321da177e4SLinus Torvalds skip_ed:
9331da177e4SLinus Torvalds 				last = &ed->ed_next;
9341da177e4SLinus Torvalds 				continue;
9351da177e4SLinus Torvalds 			}
9361da177e4SLinus Torvalds 
9371da177e4SLinus Torvalds 			if (!list_empty (&ed->td_list)) {
9381da177e4SLinus Torvalds 				struct td	*td;
9391da177e4SLinus Torvalds 				u32		head;
9401da177e4SLinus Torvalds 
9411da177e4SLinus Torvalds 				td = list_entry (ed->td_list.next, struct td,
9421da177e4SLinus Torvalds 							td_list);
9431da177e4SLinus Torvalds 				head = hc32_to_cpu (ohci, ed->hwHeadP) &
9441da177e4SLinus Torvalds 								TD_MASK;
9451da177e4SLinus Torvalds 
9461da177e4SLinus Torvalds 				/* INTR_WDH may need to clean up first */
947*89a0fd18SMike Nuss 				if (td->td_dma != head) {
948*89a0fd18SMike Nuss 					if (ed == ohci->ed_to_check)
949*89a0fd18SMike Nuss 						ohci->ed_to_check = NULL;
950*89a0fd18SMike Nuss 					else
9511da177e4SLinus Torvalds 						goto skip_ed;
9521da177e4SLinus Torvalds 				}
9531da177e4SLinus Torvalds 			}
954*89a0fd18SMike Nuss 		}
9551da177e4SLinus Torvalds 
9561da177e4SLinus Torvalds 		/* reentrancy:  if we drop the schedule lock, someone might
9571da177e4SLinus Torvalds 		 * have modified this list.  normally it's just prepending
9581da177e4SLinus Torvalds 		 * entries (which we'd ignore), but paranoia won't hurt.
9591da177e4SLinus Torvalds 		 */
9601da177e4SLinus Torvalds 		*last = ed->ed_next;
9611da177e4SLinus Torvalds 		ed->ed_next = NULL;
9621da177e4SLinus Torvalds 		modified = 0;
9631da177e4SLinus Torvalds 
9641da177e4SLinus Torvalds 		/* unlink urbs as requested, but rescan the list after
9651da177e4SLinus Torvalds 		 * we call a completion since it might have unlinked
9661da177e4SLinus Torvalds 		 * another (earlier) urb
9671da177e4SLinus Torvalds 		 *
9681da177e4SLinus Torvalds 		 * When we get here, the HC doesn't see this ed.  But it
9691da177e4SLinus Torvalds 		 * must not be rescheduled until all completed URBs have
9701da177e4SLinus Torvalds 		 * been given back to the driver.
9711da177e4SLinus Torvalds 		 */
9721da177e4SLinus Torvalds rescan_this:
9731da177e4SLinus Torvalds 		completed = 0;
9741da177e4SLinus Torvalds 		prev = &ed->hwHeadP;
9751da177e4SLinus Torvalds 		list_for_each_safe (entry, tmp, &ed->td_list) {
9761da177e4SLinus Torvalds 			struct td	*td;
9771da177e4SLinus Torvalds 			struct urb	*urb;
9781da177e4SLinus Torvalds 			urb_priv_t	*urb_priv;
9791da177e4SLinus Torvalds 			__hc32		savebits;
9801da177e4SLinus Torvalds 
9811da177e4SLinus Torvalds 			td = list_entry (entry, struct td, td_list);
9821da177e4SLinus Torvalds 			urb = td->urb;
9831da177e4SLinus Torvalds 			urb_priv = td->urb->hcpriv;
9841da177e4SLinus Torvalds 
9851da177e4SLinus Torvalds 			if (urb->status == -EINPROGRESS) {
9861da177e4SLinus Torvalds 				prev = &td->hwNextTD;
9871da177e4SLinus Torvalds 				continue;
9881da177e4SLinus Torvalds 			}
9891da177e4SLinus Torvalds 
9901da177e4SLinus Torvalds 			/* patch pointer hc uses */
9911da177e4SLinus Torvalds 			savebits = *prev & ~cpu_to_hc32 (ohci, TD_MASK);
9921da177e4SLinus Torvalds 			*prev = td->hwNextTD | savebits;
9931da177e4SLinus Torvalds 
9941da177e4SLinus Torvalds 			/* HC may have partly processed this TD */
9951da177e4SLinus Torvalds 			td_done (ohci, urb, td);
9961da177e4SLinus Torvalds 			urb_priv->td_cnt++;
9971da177e4SLinus Torvalds 
9981da177e4SLinus Torvalds 			/* if URB is done, clean up */
9991da177e4SLinus Torvalds 			if (urb_priv->td_cnt == urb_priv->length) {
10001da177e4SLinus Torvalds 				modified = completed = 1;
10017d12e780SDavid Howells 				finish_urb (ohci, urb);
10021da177e4SLinus Torvalds 			}
10031da177e4SLinus Torvalds 		}
10041da177e4SLinus Torvalds 		if (completed && !list_empty (&ed->td_list))
10051da177e4SLinus Torvalds 			goto rescan_this;
10061da177e4SLinus Torvalds 
10071da177e4SLinus Torvalds 		/* ED's now officially unlinked, hc doesn't see */
10081da177e4SLinus Torvalds 		ed->state = ED_IDLE;
1009*89a0fd18SMike Nuss 		if (quirk_zfmicro(ohci) && ed->type == PIPE_INTERRUPT)
1010*89a0fd18SMike Nuss 			ohci->eds_scheduled--;
10111da177e4SLinus Torvalds 		ed->hwHeadP &= ~cpu_to_hc32(ohci, ED_H);
10121da177e4SLinus Torvalds 		ed->hwNextED = 0;
10131da177e4SLinus Torvalds 		wmb ();
10141da177e4SLinus Torvalds 		ed->hwINFO &= ~cpu_to_hc32 (ohci, ED_SKIP | ED_DEQUEUE);
10151da177e4SLinus Torvalds 
10161da177e4SLinus Torvalds 		/* but if there's work queued, reschedule */
10171da177e4SLinus Torvalds 		if (!list_empty (&ed->td_list)) {
10181da177e4SLinus Torvalds 			if (HC_IS_RUNNING(ohci_to_hcd(ohci)->state))
10191da177e4SLinus Torvalds 				ed_schedule (ohci, ed);
10201da177e4SLinus Torvalds 		}
10211da177e4SLinus Torvalds 
10221da177e4SLinus Torvalds 		if (modified)
10231da177e4SLinus Torvalds 			goto rescan_all;
10241da177e4SLinus Torvalds 	}
10251da177e4SLinus Torvalds 
10261da177e4SLinus Torvalds 	/* maybe reenable control and bulk lists */
10271da177e4SLinus Torvalds 	if (HC_IS_RUNNING(ohci_to_hcd(ohci)->state)
10281da177e4SLinus Torvalds 			&& ohci_to_hcd(ohci)->state != HC_STATE_QUIESCING
10291da177e4SLinus Torvalds 			&& !ohci->ed_rm_list) {
10301da177e4SLinus Torvalds 		u32	command = 0, control = 0;
10311da177e4SLinus Torvalds 
10321da177e4SLinus Torvalds 		if (ohci->ed_controltail) {
10331da177e4SLinus Torvalds 			command |= OHCI_CLF;
1034*89a0fd18SMike Nuss 			if (quirk_zfmicro(ohci))
10350e498763SDavid Brownell 				mdelay(1);
10361da177e4SLinus Torvalds 			if (!(ohci->hc_control & OHCI_CTRL_CLE)) {
10371da177e4SLinus Torvalds 				control |= OHCI_CTRL_CLE;
10381da177e4SLinus Torvalds 				ohci_writel (ohci, 0,
10391da177e4SLinus Torvalds 					&ohci->regs->ed_controlcurrent);
10401da177e4SLinus Torvalds 			}
10411da177e4SLinus Torvalds 		}
10421da177e4SLinus Torvalds 		if (ohci->ed_bulktail) {
10431da177e4SLinus Torvalds 			command |= OHCI_BLF;
1044*89a0fd18SMike Nuss 			if (quirk_zfmicro(ohci))
10450e498763SDavid Brownell 				mdelay(1);
10461da177e4SLinus Torvalds 			if (!(ohci->hc_control & OHCI_CTRL_BLE)) {
10471da177e4SLinus Torvalds 				control |= OHCI_CTRL_BLE;
10481da177e4SLinus Torvalds 				ohci_writel (ohci, 0,
10491da177e4SLinus Torvalds 					&ohci->regs->ed_bulkcurrent);
10501da177e4SLinus Torvalds 			}
10511da177e4SLinus Torvalds 		}
10521da177e4SLinus Torvalds 
10531da177e4SLinus Torvalds 		/* CLE/BLE to enable, CLF/BLF to (maybe) kickstart */
10541da177e4SLinus Torvalds 		if (control) {
10551da177e4SLinus Torvalds 			ohci->hc_control |= control;
1056*89a0fd18SMike Nuss 			if (quirk_zfmicro(ohci))
10570e498763SDavid Brownell 				mdelay(1);
10581da177e4SLinus Torvalds 			ohci_writel (ohci, ohci->hc_control,
10591da177e4SLinus Torvalds 					&ohci->regs->control);
10601da177e4SLinus Torvalds 		}
10610e498763SDavid Brownell 		if (command) {
1062*89a0fd18SMike Nuss 			if (quirk_zfmicro(ohci))
10630e498763SDavid Brownell 				mdelay(1);
10641da177e4SLinus Torvalds 			ohci_writel (ohci, command, &ohci->regs->cmdstatus);
10651da177e4SLinus Torvalds 		}
10661da177e4SLinus Torvalds 	}
10670e498763SDavid Brownell }
10681da177e4SLinus Torvalds 
10691da177e4SLinus Torvalds 
10701da177e4SLinus Torvalds 
10711da177e4SLinus Torvalds /*-------------------------------------------------------------------------*/
10721da177e4SLinus Torvalds 
10731da177e4SLinus Torvalds /*
1074*89a0fd18SMike Nuss  * Used to take back a TD from the host controller. This would normally be
1075*89a0fd18SMike Nuss  * called from within dl_done_list, however it may be called directly if the
1076*89a0fd18SMike Nuss  * HC no longer sees the TD and it has not appeared on the donelist (after
1077*89a0fd18SMike Nuss  * two frames).  This bug has been observed on ZF Micro systems.
10781da177e4SLinus Torvalds  */
1079*89a0fd18SMike Nuss static void takeback_td(struct ohci_hcd *ohci, struct td *td)
10801da177e4SLinus Torvalds {
10811da177e4SLinus Torvalds 	struct urb	*urb = td->urb;
10821da177e4SLinus Torvalds 	urb_priv_t	*urb_priv = urb->hcpriv;
10831da177e4SLinus Torvalds 	struct ed	*ed = td->ed;
10841da177e4SLinus Torvalds 
10851da177e4SLinus Torvalds 	/* update URB's length and status from TD */
10861da177e4SLinus Torvalds 	td_done(ohci, urb, td);
10871da177e4SLinus Torvalds 	urb_priv->td_cnt++;
10881da177e4SLinus Torvalds 
10891da177e4SLinus Torvalds 	/* If all this urb's TDs are done, call complete() */
10901da177e4SLinus Torvalds 	if (urb_priv->td_cnt == urb_priv->length)
10917d12e780SDavid Howells 		finish_urb(ohci, urb);
10921da177e4SLinus Torvalds 
10931da177e4SLinus Torvalds 	/* clean schedule:  unlink EDs that are no longer busy */
10941da177e4SLinus Torvalds 	if (list_empty(&ed->td_list)) {
10951da177e4SLinus Torvalds 		if (ed->state == ED_OPER)
10961da177e4SLinus Torvalds 			start_ed_unlink(ohci, ed);
10971da177e4SLinus Torvalds 
10981da177e4SLinus Torvalds 	/* ... reenabling halted EDs only after fault cleanup */
1099*89a0fd18SMike Nuss 	} else if ((ed->hwINFO & cpu_to_hc32(ohci, ED_SKIP | ED_DEQUEUE))
11001da177e4SLinus Torvalds 			== cpu_to_hc32(ohci, ED_SKIP)) {
11011da177e4SLinus Torvalds 		td = list_entry(ed->td_list.next, struct td, td_list);
11021da177e4SLinus Torvalds 		if (!(td->hwINFO & cpu_to_hc32(ohci, TD_DONE))) {
11031da177e4SLinus Torvalds 			ed->hwINFO &= ~cpu_to_hc32(ohci, ED_SKIP);
11041da177e4SLinus Torvalds 			/* ... hc may need waking-up */
11051da177e4SLinus Torvalds 			switch (ed->type) {
11061da177e4SLinus Torvalds 			case PIPE_CONTROL:
11071da177e4SLinus Torvalds 				ohci_writel(ohci, OHCI_CLF,
11081da177e4SLinus Torvalds 						&ohci->regs->cmdstatus);
11091da177e4SLinus Torvalds 				break;
11101da177e4SLinus Torvalds 			case PIPE_BULK:
11111da177e4SLinus Torvalds 				ohci_writel(ohci, OHCI_BLF,
11121da177e4SLinus Torvalds 						&ohci->regs->cmdstatus);
11131da177e4SLinus Torvalds 				break;
11141da177e4SLinus Torvalds 			}
11151da177e4SLinus Torvalds 		}
11161da177e4SLinus Torvalds 	}
1117*89a0fd18SMike Nuss }
11181da177e4SLinus Torvalds 
1119*89a0fd18SMike Nuss /*
1120*89a0fd18SMike Nuss  * Process normal completions (error or success) and clean the schedules.
1121*89a0fd18SMike Nuss  *
1122*89a0fd18SMike Nuss  * This is the main path for handing urbs back to drivers.  The only other
1123*89a0fd18SMike Nuss  * normal path is finish_unlinks(), which unlinks URBs using ed_rm_list,
1124*89a0fd18SMike Nuss  * instead of scanning the (re-reversed) donelist as this does.  There's
1125*89a0fd18SMike Nuss  * an abnormal path too, handling a quirk in some Compaq silicon:  URBs
1126*89a0fd18SMike Nuss  * with TDs that appear to be orphaned are directly reclaimed.
1127*89a0fd18SMike Nuss  */
1128*89a0fd18SMike Nuss static void
1129*89a0fd18SMike Nuss dl_done_list (struct ohci_hcd *ohci)
1130*89a0fd18SMike Nuss {
1131*89a0fd18SMike Nuss 	struct td	*td = dl_reverse_done_list (ohci);
1132*89a0fd18SMike Nuss 
1133*89a0fd18SMike Nuss 	while (td) {
1134*89a0fd18SMike Nuss 		struct td	*td_next = td->next_dl_td;
1135*89a0fd18SMike Nuss 		takeback_td(ohci, td);
11361da177e4SLinus Torvalds 		td = td_next;
11371da177e4SLinus Torvalds 	}
11381da177e4SLinus Torvalds }
1139