xref: /linux/drivers/usb/host/ohci-q.c (revision 7d8021c967648accd1b78e5e1ddaad655cd2c61f)
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>
115a0e3ad6STejun Heo #include <linux/slab.h>
127d12e780SDavid Howells 
131da177e4SLinus Torvalds static void urb_free_priv (struct ohci_hcd *hc, urb_priv_t *urb_priv)
141da177e4SLinus Torvalds {
151da177e4SLinus Torvalds 	int		last = urb_priv->length - 1;
161da177e4SLinus Torvalds 
171da177e4SLinus Torvalds 	if (last >= 0) {
181da177e4SLinus Torvalds 		int		i;
191da177e4SLinus Torvalds 		struct td	*td;
201da177e4SLinus Torvalds 
211da177e4SLinus Torvalds 		for (i = 0; i <= last; i++) {
221da177e4SLinus Torvalds 			td = urb_priv->td [i];
231da177e4SLinus Torvalds 			if (td)
241da177e4SLinus Torvalds 				td_free (hc, td);
251da177e4SLinus Torvalds 		}
261da177e4SLinus Torvalds 	}
271da177e4SLinus Torvalds 
281da177e4SLinus Torvalds 	list_del (&urb_priv->pending);
291da177e4SLinus Torvalds 	kfree (urb_priv);
301da177e4SLinus Torvalds }
311da177e4SLinus Torvalds 
321da177e4SLinus Torvalds /*-------------------------------------------------------------------------*/
331da177e4SLinus Torvalds 
341da177e4SLinus Torvalds /*
351da177e4SLinus Torvalds  * URB goes back to driver, and isn't reissued.
361da177e4SLinus Torvalds  * It's completely gone from HC data structures.
371da177e4SLinus Torvalds  * PRECONDITION:  ohci lock held, irqs blocked.
381da177e4SLinus Torvalds  */
391da177e4SLinus Torvalds static void
4055d84968SAlan Stern finish_urb(struct ohci_hcd *ohci, struct urb *urb, int status)
411da177e4SLinus Torvalds __releases(ohci->lock)
421da177e4SLinus Torvalds __acquires(ohci->lock)
431da177e4SLinus Torvalds {
442621d011SManjunath Goudar 	struct device *dev = ohci_to_hcd(ohci)->self.controller;
45a8693424SAlan Stern 	struct usb_host_endpoint *ep = urb->ep;
46a8693424SAlan Stern 	struct urb_priv *urb_priv;
47a8693424SAlan Stern 
481da177e4SLinus Torvalds 	// ASSERT (urb->hcpriv != 0);
491da177e4SLinus Torvalds 
50a8693424SAlan Stern  restart:
511da177e4SLinus Torvalds 	urb_free_priv (ohci, urb->hcpriv);
52ece1d77eSChen Gang 	urb->hcpriv = NULL;
5355d84968SAlan Stern 	if (likely(status == -EINPROGRESS))
5455d84968SAlan Stern 		status = 0;
551da177e4SLinus Torvalds 
561da177e4SLinus Torvalds 	switch (usb_pipetype (urb->pipe)) {
571da177e4SLinus Torvalds 	case PIPE_ISOCHRONOUS:
581da177e4SLinus Torvalds 		ohci_to_hcd(ohci)->self.bandwidth_isoc_reqs--;
59a1f17a87SLibin Yang 		if (ohci_to_hcd(ohci)->self.bandwidth_isoc_reqs == 0) {
60a1f17a87SLibin Yang 			if (quirk_amdiso(ohci))
61ad93562bSAndiry Xu 				usb_amd_quirk_pll_enable();
62a1f17a87SLibin Yang 			if (quirk_amdprefetch(ohci))
632621d011SManjunath Goudar 				sb800_prefetch(dev, 0);
64a1f17a87SLibin Yang 		}
651da177e4SLinus Torvalds 		break;
661da177e4SLinus Torvalds 	case PIPE_INTERRUPT:
671da177e4SLinus Torvalds 		ohci_to_hcd(ohci)->self.bandwidth_int_reqs--;
681da177e4SLinus Torvalds 		break;
691da177e4SLinus Torvalds 	}
701da177e4SLinus Torvalds 
711da177e4SLinus Torvalds 	/* urb->complete() can reenter this HCD */
72e9df41c5SAlan Stern 	usb_hcd_unlink_urb_from_ep(ohci_to_hcd(ohci), urb);
731da177e4SLinus Torvalds 	spin_unlock (&ohci->lock);
744a00027dSAlan Stern 	usb_hcd_giveback_urb(ohci_to_hcd(ohci), urb, status);
751da177e4SLinus Torvalds 	spin_lock (&ohci->lock);
761da177e4SLinus Torvalds 
771da177e4SLinus Torvalds 	/* stop periodic dma if it's not needed */
781da177e4SLinus Torvalds 	if (ohci_to_hcd(ohci)->self.bandwidth_isoc_reqs == 0
791da177e4SLinus Torvalds 			&& ohci_to_hcd(ohci)->self.bandwidth_int_reqs == 0) {
801da177e4SLinus Torvalds 		ohci->hc_control &= ~(OHCI_CTRL_PLE|OHCI_CTRL_IE);
811da177e4SLinus Torvalds 		ohci_writel (ohci, ohci->hc_control, &ohci->regs->control);
821da177e4SLinus Torvalds 	}
83a8693424SAlan Stern 
84a8693424SAlan Stern 	/*
85a8693424SAlan Stern 	 * An isochronous URB that is sumitted too late won't have any TDs
86a8693424SAlan Stern 	 * (marked by the fact that the td_cnt value is larger than the
87a8693424SAlan Stern 	 * actual number of TDs).  If the next URB on this endpoint is like
88a8693424SAlan Stern 	 * that, give it back now.
89a8693424SAlan Stern 	 */
90a8693424SAlan Stern 	if (!list_empty(&ep->urb_list)) {
91a8693424SAlan Stern 		urb = list_first_entry(&ep->urb_list, struct urb, urb_list);
92a8693424SAlan Stern 		urb_priv = urb->hcpriv;
93a8693424SAlan Stern 		if (urb_priv->td_cnt > urb_priv->length) {
94a8693424SAlan Stern 			status = 0;
95a8693424SAlan Stern 			goto restart;
96a8693424SAlan Stern 		}
97a8693424SAlan Stern 	}
981da177e4SLinus Torvalds }
991da177e4SLinus Torvalds 
1001da177e4SLinus Torvalds 
1011da177e4SLinus Torvalds /*-------------------------------------------------------------------------*
1021da177e4SLinus Torvalds  * ED handling functions
1031da177e4SLinus Torvalds  *-------------------------------------------------------------------------*/
1041da177e4SLinus Torvalds 
1051da177e4SLinus Torvalds /* search for the right schedule branch to use for a periodic ed.
1061da177e4SLinus Torvalds  * does some load balancing; returns the branch, or negative errno.
1071da177e4SLinus Torvalds  */
1081da177e4SLinus Torvalds static int balance (struct ohci_hcd *ohci, int interval, int load)
1091da177e4SLinus Torvalds {
1101da177e4SLinus Torvalds 	int	i, branch = -ENOSPC;
1111da177e4SLinus Torvalds 
1121da177e4SLinus Torvalds 	/* iso periods can be huge; iso tds specify frame numbers */
1131da177e4SLinus Torvalds 	if (interval > NUM_INTS)
1141da177e4SLinus Torvalds 		interval = NUM_INTS;
1151da177e4SLinus Torvalds 
1161da177e4SLinus Torvalds 	/* search for the least loaded schedule branch of that period
1171da177e4SLinus Torvalds 	 * that has enough bandwidth left unreserved.
1181da177e4SLinus Torvalds 	 */
1191da177e4SLinus Torvalds 	for (i = 0; i < interval ; i++) {
1201da177e4SLinus Torvalds 		if (branch < 0 || ohci->load [branch] > ohci->load [i]) {
1211da177e4SLinus Torvalds 			int	j;
1221da177e4SLinus Torvalds 
1231da177e4SLinus Torvalds 			/* usb 1.1 says 90% of one frame */
1241da177e4SLinus Torvalds 			for (j = i; j < NUM_INTS; j += interval) {
1251da177e4SLinus Torvalds 				if ((ohci->load [j] + load) > 900)
1261da177e4SLinus Torvalds 					break;
1271da177e4SLinus Torvalds 			}
1281da177e4SLinus Torvalds 			if (j < NUM_INTS)
1291da177e4SLinus Torvalds 				continue;
1301da177e4SLinus Torvalds 			branch = i;
1311da177e4SLinus Torvalds 		}
1321da177e4SLinus Torvalds 	}
1331da177e4SLinus Torvalds 	return branch;
1341da177e4SLinus Torvalds }
1351da177e4SLinus Torvalds 
1361da177e4SLinus Torvalds /*-------------------------------------------------------------------------*/
1371da177e4SLinus Torvalds 
1381da177e4SLinus Torvalds /* both iso and interrupt requests have periods; this routine puts them
1391da177e4SLinus Torvalds  * into the schedule tree in the apppropriate place.  most iso devices use
1401da177e4SLinus Torvalds  * 1msec periods, but that's not required.
1411da177e4SLinus Torvalds  */
1421da177e4SLinus Torvalds static void periodic_link (struct ohci_hcd *ohci, struct ed *ed)
1431da177e4SLinus Torvalds {
1441da177e4SLinus Torvalds 	unsigned	i;
1451da177e4SLinus Torvalds 
146d2c4254fSOliver Neukum 	ohci_dbg(ohci, "link %sed %p branch %d [%dus.], interval %d\n",
1471da177e4SLinus Torvalds 		(ed->hwINFO & cpu_to_hc32 (ohci, ED_ISO)) ? "iso " : "",
1481da177e4SLinus Torvalds 		ed, ed->branch, ed->load, ed->interval);
1491da177e4SLinus Torvalds 
1501da177e4SLinus Torvalds 	for (i = ed->branch; i < NUM_INTS; i += ed->interval) {
1511da177e4SLinus Torvalds 		struct ed	**prev = &ohci->periodic [i];
1521da177e4SLinus Torvalds 		__hc32		*prev_p = &ohci->hcca->int_table [i];
1531da177e4SLinus Torvalds 		struct ed	*here = *prev;
1541da177e4SLinus Torvalds 
1551da177e4SLinus Torvalds 		/* sorting each branch by period (slow before fast)
1561da177e4SLinus Torvalds 		 * lets us share the faster parts of the tree.
1571da177e4SLinus Torvalds 		 * (plus maybe: put interrupt eds before iso)
1581da177e4SLinus Torvalds 		 */
1591da177e4SLinus Torvalds 		while (here && ed != here) {
1601da177e4SLinus Torvalds 			if (ed->interval > here->interval)
1611da177e4SLinus Torvalds 				break;
1621da177e4SLinus Torvalds 			prev = &here->ed_next;
1631da177e4SLinus Torvalds 			prev_p = &here->hwNextED;
1641da177e4SLinus Torvalds 			here = *prev;
1651da177e4SLinus Torvalds 		}
1661da177e4SLinus Torvalds 		if (ed != here) {
1671da177e4SLinus Torvalds 			ed->ed_next = here;
1681da177e4SLinus Torvalds 			if (here)
1691da177e4SLinus Torvalds 				ed->hwNextED = *prev_p;
1701da177e4SLinus Torvalds 			wmb ();
1711da177e4SLinus Torvalds 			*prev = ed;
1721da177e4SLinus Torvalds 			*prev_p = cpu_to_hc32(ohci, ed->dma);
1731da177e4SLinus Torvalds 			wmb();
1741da177e4SLinus Torvalds 		}
1751da177e4SLinus Torvalds 		ohci->load [i] += ed->load;
1761da177e4SLinus Torvalds 	}
1771da177e4SLinus Torvalds 	ohci_to_hcd(ohci)->self.bandwidth_allocated += ed->load / ed->interval;
1781da177e4SLinus Torvalds }
1791da177e4SLinus Torvalds 
1801da177e4SLinus Torvalds /* link an ed into one of the HC chains */
1811da177e4SLinus Torvalds 
1821da177e4SLinus Torvalds static int ed_schedule (struct ohci_hcd *ohci, struct ed *ed)
1831da177e4SLinus Torvalds {
1841da177e4SLinus Torvalds 	int	branch;
1851da177e4SLinus Torvalds 
1861da177e4SLinus Torvalds 	ed->state = ED_OPER;
1871da177e4SLinus Torvalds 	ed->ed_prev = NULL;
1881da177e4SLinus Torvalds 	ed->ed_next = NULL;
1891da177e4SLinus Torvalds 	ed->hwNextED = 0;
1901da177e4SLinus Torvalds 	wmb ();
1911da177e4SLinus Torvalds 
1921da177e4SLinus Torvalds 	/* we care about rm_list when setting CLE/BLE in case the HC was at
1931da177e4SLinus Torvalds 	 * work on some TD when CLE/BLE was turned off, and isn't quiesced
1941da177e4SLinus Torvalds 	 * yet.  finish_unlinks() restarts as needed, some upcoming INTR_SF.
1951da177e4SLinus Torvalds 	 *
1961da177e4SLinus Torvalds 	 * control and bulk EDs are doubly linked (ed_next, ed_prev), but
1971da177e4SLinus Torvalds 	 * periodic ones are singly linked (ed_next). that's because the
1981da177e4SLinus Torvalds 	 * periodic schedule encodes a tree like figure 3-5 in the ohci
1991da177e4SLinus Torvalds 	 * spec:  each qh can have several "previous" nodes, and the tree
2001da177e4SLinus Torvalds 	 * doesn't have unused/idle descriptors.
2011da177e4SLinus Torvalds 	 */
2021da177e4SLinus Torvalds 	switch (ed->type) {
2031da177e4SLinus Torvalds 	case PIPE_CONTROL:
2041da177e4SLinus Torvalds 		if (ohci->ed_controltail == NULL) {
2051da177e4SLinus Torvalds 			WARN_ON (ohci->hc_control & OHCI_CTRL_CLE);
2061da177e4SLinus Torvalds 			ohci_writel (ohci, ed->dma,
2071da177e4SLinus Torvalds 					&ohci->regs->ed_controlhead);
2081da177e4SLinus Torvalds 		} else {
2091da177e4SLinus Torvalds 			ohci->ed_controltail->ed_next = ed;
2101da177e4SLinus Torvalds 			ohci->ed_controltail->hwNextED = cpu_to_hc32 (ohci,
2111da177e4SLinus Torvalds 								ed->dma);
2121da177e4SLinus Torvalds 		}
2131da177e4SLinus Torvalds 		ed->ed_prev = ohci->ed_controltail;
2141da177e4SLinus Torvalds 		if (!ohci->ed_controltail && !ohci->ed_rm_list) {
2151da177e4SLinus Torvalds 			wmb();
2161da177e4SLinus Torvalds 			ohci->hc_control |= OHCI_CTRL_CLE;
2171da177e4SLinus Torvalds 			ohci_writel (ohci, 0, &ohci->regs->ed_controlcurrent);
2181da177e4SLinus Torvalds 			ohci_writel (ohci, ohci->hc_control,
2191da177e4SLinus Torvalds 					&ohci->regs->control);
2201da177e4SLinus Torvalds 		}
2211da177e4SLinus Torvalds 		ohci->ed_controltail = ed;
2221da177e4SLinus Torvalds 		break;
2231da177e4SLinus Torvalds 
2241da177e4SLinus Torvalds 	case PIPE_BULK:
2251da177e4SLinus Torvalds 		if (ohci->ed_bulktail == NULL) {
2261da177e4SLinus Torvalds 			WARN_ON (ohci->hc_control & OHCI_CTRL_BLE);
2271da177e4SLinus Torvalds 			ohci_writel (ohci, ed->dma, &ohci->regs->ed_bulkhead);
2281da177e4SLinus Torvalds 		} else {
2291da177e4SLinus Torvalds 			ohci->ed_bulktail->ed_next = ed;
2301da177e4SLinus Torvalds 			ohci->ed_bulktail->hwNextED = cpu_to_hc32 (ohci,
2311da177e4SLinus Torvalds 								ed->dma);
2321da177e4SLinus Torvalds 		}
2331da177e4SLinus Torvalds 		ed->ed_prev = ohci->ed_bulktail;
2341da177e4SLinus Torvalds 		if (!ohci->ed_bulktail && !ohci->ed_rm_list) {
2351da177e4SLinus Torvalds 			wmb();
2361da177e4SLinus Torvalds 			ohci->hc_control |= OHCI_CTRL_BLE;
2371da177e4SLinus Torvalds 			ohci_writel (ohci, 0, &ohci->regs->ed_bulkcurrent);
2381da177e4SLinus Torvalds 			ohci_writel (ohci, ohci->hc_control,
2391da177e4SLinus Torvalds 					&ohci->regs->control);
2401da177e4SLinus Torvalds 		}
2411da177e4SLinus Torvalds 		ohci->ed_bulktail = ed;
2421da177e4SLinus Torvalds 		break;
2431da177e4SLinus Torvalds 
2441da177e4SLinus Torvalds 	// case PIPE_INTERRUPT:
2451da177e4SLinus Torvalds 	// case PIPE_ISOCHRONOUS:
2461da177e4SLinus Torvalds 	default:
2471da177e4SLinus Torvalds 		branch = balance (ohci, ed->interval, ed->load);
2481da177e4SLinus Torvalds 		if (branch < 0) {
2491da177e4SLinus Torvalds 			ohci_dbg (ohci,
2501da177e4SLinus Torvalds 				"ERR %d, interval %d msecs, load %d\n",
2511da177e4SLinus Torvalds 				branch, ed->interval, ed->load);
2521da177e4SLinus Torvalds 			// FIXME if there are TDs queued, fail them!
2531da177e4SLinus Torvalds 			return branch;
2541da177e4SLinus Torvalds 		}
2551da177e4SLinus Torvalds 		ed->branch = branch;
2561da177e4SLinus Torvalds 		periodic_link (ohci, ed);
2571da177e4SLinus Torvalds 	}
2581da177e4SLinus Torvalds 
2591da177e4SLinus Torvalds 	/* the HC may not see the schedule updates yet, but if it does
2601da177e4SLinus Torvalds 	 * then they'll be properly ordered.
2611da177e4SLinus Torvalds 	 */
2621da177e4SLinus Torvalds 	return 0;
2631da177e4SLinus Torvalds }
2641da177e4SLinus Torvalds 
2651da177e4SLinus Torvalds /*-------------------------------------------------------------------------*/
2661da177e4SLinus Torvalds 
2671da177e4SLinus Torvalds /* scan the periodic table to find and unlink this ED */
2681da177e4SLinus Torvalds static void periodic_unlink (struct ohci_hcd *ohci, struct ed *ed)
2691da177e4SLinus Torvalds {
2701da177e4SLinus Torvalds 	int	i;
2711da177e4SLinus Torvalds 
2721da177e4SLinus Torvalds 	for (i = ed->branch; i < NUM_INTS; i += ed->interval) {
2731da177e4SLinus Torvalds 		struct ed	*temp;
2741da177e4SLinus Torvalds 		struct ed	**prev = &ohci->periodic [i];
2751da177e4SLinus Torvalds 		__hc32		*prev_p = &ohci->hcca->int_table [i];
2761da177e4SLinus Torvalds 
2771da177e4SLinus Torvalds 		while (*prev && (temp = *prev) != ed) {
2781da177e4SLinus Torvalds 			prev_p = &temp->hwNextED;
2791da177e4SLinus Torvalds 			prev = &temp->ed_next;
2801da177e4SLinus Torvalds 		}
2811da177e4SLinus Torvalds 		if (*prev) {
2821da177e4SLinus Torvalds 			*prev_p = ed->hwNextED;
2831da177e4SLinus Torvalds 			*prev = ed->ed_next;
2841da177e4SLinus Torvalds 		}
2851da177e4SLinus Torvalds 		ohci->load [i] -= ed->load;
2861da177e4SLinus Torvalds 	}
2871da177e4SLinus Torvalds 	ohci_to_hcd(ohci)->self.bandwidth_allocated -= ed->load / ed->interval;
2881da177e4SLinus Torvalds 
289d2c4254fSOliver Neukum 	ohci_dbg(ohci, "unlink %sed %p branch %d [%dus.], interval %d\n",
2901da177e4SLinus Torvalds 		(ed->hwINFO & cpu_to_hc32 (ohci, ED_ISO)) ? "iso " : "",
2911da177e4SLinus Torvalds 		ed, ed->branch, ed->load, ed->interval);
2921da177e4SLinus Torvalds }
2931da177e4SLinus Torvalds 
2941da177e4SLinus Torvalds /* unlink an ed from one of the HC chains.
2951da177e4SLinus Torvalds  * just the link to the ed is unlinked.
2961da177e4SLinus Torvalds  * the link from the ed still points to another operational ed or 0
2971da177e4SLinus Torvalds  * so the HC can eventually finish the processing of the unlinked ed
2981da177e4SLinus Torvalds  * (assuming it already started that, which needn't be true).
2991da177e4SLinus Torvalds  *
3001da177e4SLinus Torvalds  * ED_UNLINK is a transient state: the HC may still see this ED, but soon
3011da177e4SLinus Torvalds  * it won't.  ED_SKIP means the HC will finish its current transaction,
3021da177e4SLinus Torvalds  * but won't start anything new.  The TD queue may still grow; device
3031da177e4SLinus Torvalds  * drivers don't know about this HCD-internal state.
3041da177e4SLinus Torvalds  *
3051da177e4SLinus Torvalds  * When the HC can't see the ED, something changes ED_UNLINK to one of:
3061da177e4SLinus Torvalds  *
3071da177e4SLinus Torvalds  *  - ED_OPER: when there's any request queued, the ED gets rescheduled
3081da177e4SLinus Torvalds  *    immediately.  HC should be working on them.
3091da177e4SLinus Torvalds  *
310977dcfdcSAlan Stern  *  - ED_IDLE: when there's no TD queue or the HC isn't running.
3111da177e4SLinus Torvalds  *
3121da177e4SLinus Torvalds  * When finish_unlinks() runs later, after SOF interrupt, it will often
3131da177e4SLinus Torvalds  * complete one or more URB unlinks before making that state change.
3141da177e4SLinus Torvalds  */
3151da177e4SLinus Torvalds static void ed_deschedule (struct ohci_hcd *ohci, struct ed *ed)
3161da177e4SLinus Torvalds {
3171da177e4SLinus Torvalds 	ed->hwINFO |= cpu_to_hc32 (ohci, ED_SKIP);
3181da177e4SLinus Torvalds 	wmb ();
3191da177e4SLinus Torvalds 	ed->state = ED_UNLINK;
3201da177e4SLinus Torvalds 
3211da177e4SLinus Torvalds 	/* To deschedule something from the control or bulk list, just
3221da177e4SLinus Torvalds 	 * clear CLE/BLE and wait.  There's no safe way to scrub out list
3231da177e4SLinus Torvalds 	 * head/current registers until later, and "later" isn't very
3241da177e4SLinus Torvalds 	 * tightly specified.  Figure 6-5 and Section 6.4.2.2 show how
3251da177e4SLinus Torvalds 	 * the HC is reading the ED queues (while we modify them).
3261da177e4SLinus Torvalds 	 *
3271da177e4SLinus Torvalds 	 * For now, ed_schedule() is "later".  It might be good paranoia
3281da177e4SLinus Torvalds 	 * to scrub those registers in finish_unlinks(), in case of bugs
3291da177e4SLinus Torvalds 	 * that make the HC try to use them.
3301da177e4SLinus Torvalds 	 */
3311da177e4SLinus Torvalds 	switch (ed->type) {
3321da177e4SLinus Torvalds 	case PIPE_CONTROL:
3331da177e4SLinus Torvalds 		/* remove ED from the HC's list: */
3341da177e4SLinus Torvalds 		if (ed->ed_prev == NULL) {
3351da177e4SLinus Torvalds 			if (!ed->hwNextED) {
3361da177e4SLinus Torvalds 				ohci->hc_control &= ~OHCI_CTRL_CLE;
3371da177e4SLinus Torvalds 				ohci_writel (ohci, ohci->hc_control,
3381da177e4SLinus Torvalds 						&ohci->regs->control);
3391da177e4SLinus Torvalds 				// a ohci_readl() later syncs CLE with the HC
3401da177e4SLinus Torvalds 			} else
3411da177e4SLinus Torvalds 				ohci_writel (ohci,
3421da177e4SLinus Torvalds 					hc32_to_cpup (ohci, &ed->hwNextED),
3431da177e4SLinus Torvalds 					&ohci->regs->ed_controlhead);
3441da177e4SLinus Torvalds 		} else {
3451da177e4SLinus Torvalds 			ed->ed_prev->ed_next = ed->ed_next;
3461da177e4SLinus Torvalds 			ed->ed_prev->hwNextED = ed->hwNextED;
3471da177e4SLinus Torvalds 		}
3481da177e4SLinus Torvalds 		/* remove ED from the HCD's list: */
3491da177e4SLinus Torvalds 		if (ohci->ed_controltail == ed) {
3501da177e4SLinus Torvalds 			ohci->ed_controltail = ed->ed_prev;
3511da177e4SLinus Torvalds 			if (ohci->ed_controltail)
3521da177e4SLinus Torvalds 				ohci->ed_controltail->ed_next = NULL;
3531da177e4SLinus Torvalds 		} else if (ed->ed_next) {
3541da177e4SLinus Torvalds 			ed->ed_next->ed_prev = ed->ed_prev;
3551da177e4SLinus Torvalds 		}
3561da177e4SLinus Torvalds 		break;
3571da177e4SLinus Torvalds 
3581da177e4SLinus Torvalds 	case PIPE_BULK:
3591da177e4SLinus Torvalds 		/* remove ED from the HC's list: */
3601da177e4SLinus Torvalds 		if (ed->ed_prev == NULL) {
3611da177e4SLinus Torvalds 			if (!ed->hwNextED) {
3621da177e4SLinus Torvalds 				ohci->hc_control &= ~OHCI_CTRL_BLE;
3631da177e4SLinus Torvalds 				ohci_writel (ohci, ohci->hc_control,
3641da177e4SLinus Torvalds 						&ohci->regs->control);
3651da177e4SLinus Torvalds 				// a ohci_readl() later syncs BLE with the HC
3661da177e4SLinus Torvalds 			} else
3671da177e4SLinus Torvalds 				ohci_writel (ohci,
3681da177e4SLinus Torvalds 					hc32_to_cpup (ohci, &ed->hwNextED),
3691da177e4SLinus Torvalds 					&ohci->regs->ed_bulkhead);
3701da177e4SLinus Torvalds 		} else {
3711da177e4SLinus Torvalds 			ed->ed_prev->ed_next = ed->ed_next;
3721da177e4SLinus Torvalds 			ed->ed_prev->hwNextED = ed->hwNextED;
3731da177e4SLinus Torvalds 		}
3741da177e4SLinus Torvalds 		/* remove ED from the HCD's list: */
3751da177e4SLinus Torvalds 		if (ohci->ed_bulktail == ed) {
3761da177e4SLinus Torvalds 			ohci->ed_bulktail = ed->ed_prev;
3771da177e4SLinus Torvalds 			if (ohci->ed_bulktail)
3781da177e4SLinus Torvalds 				ohci->ed_bulktail->ed_next = NULL;
3791da177e4SLinus Torvalds 		} else if (ed->ed_next) {
3801da177e4SLinus Torvalds 			ed->ed_next->ed_prev = ed->ed_prev;
3811da177e4SLinus Torvalds 		}
3821da177e4SLinus Torvalds 		break;
3831da177e4SLinus Torvalds 
3841da177e4SLinus Torvalds 	// case PIPE_INTERRUPT:
3851da177e4SLinus Torvalds 	// case PIPE_ISOCHRONOUS:
3861da177e4SLinus Torvalds 	default:
3871da177e4SLinus Torvalds 		periodic_unlink (ohci, ed);
3881da177e4SLinus Torvalds 		break;
3891da177e4SLinus Torvalds 	}
3901da177e4SLinus Torvalds }
3911da177e4SLinus Torvalds 
3921da177e4SLinus Torvalds 
3931da177e4SLinus Torvalds /*-------------------------------------------------------------------------*/
3941da177e4SLinus Torvalds 
3951da177e4SLinus Torvalds /* get and maybe (re)init an endpoint. init _should_ be done only as part
3961da177e4SLinus Torvalds  * of enumeration, usb_set_configuration() or usb_set_interface().
3971da177e4SLinus Torvalds  */
3981da177e4SLinus Torvalds static struct ed *ed_get (
3991da177e4SLinus Torvalds 	struct ohci_hcd		*ohci,
4001da177e4SLinus Torvalds 	struct usb_host_endpoint *ep,
4011da177e4SLinus Torvalds 	struct usb_device	*udev,
4021da177e4SLinus Torvalds 	unsigned int		pipe,
4031da177e4SLinus Torvalds 	int			interval
4041da177e4SLinus Torvalds ) {
4051da177e4SLinus Torvalds 	struct ed		*ed;
4061da177e4SLinus Torvalds 	unsigned long		flags;
4071da177e4SLinus Torvalds 
4081da177e4SLinus Torvalds 	spin_lock_irqsave (&ohci->lock, flags);
4091da177e4SLinus Torvalds 
4108af93191SGreg Kroah-Hartman 	ed = ep->hcpriv;
4118af93191SGreg Kroah-Hartman 	if (!ed) {
4121da177e4SLinus Torvalds 		struct td	*td;
4131da177e4SLinus Torvalds 		int		is_out;
4141da177e4SLinus Torvalds 		u32		info;
4151da177e4SLinus Torvalds 
4161da177e4SLinus Torvalds 		ed = ed_alloc (ohci, GFP_ATOMIC);
4171da177e4SLinus Torvalds 		if (!ed) {
4181da177e4SLinus Torvalds 			/* out of memory */
4191da177e4SLinus Torvalds 			goto done;
4201da177e4SLinus Torvalds 		}
4211da177e4SLinus Torvalds 
4221da177e4SLinus Torvalds 		/* dummy td; end of td list for ed */
4231da177e4SLinus Torvalds 		td = td_alloc (ohci, GFP_ATOMIC);
4241da177e4SLinus Torvalds 		if (!td) {
4251da177e4SLinus Torvalds 			/* out of memory */
4261da177e4SLinus Torvalds 			ed_free (ohci, ed);
4271da177e4SLinus Torvalds 			ed = NULL;
4281da177e4SLinus Torvalds 			goto done;
4291da177e4SLinus Torvalds 		}
4301da177e4SLinus Torvalds 		ed->dummy = td;
4311da177e4SLinus Torvalds 		ed->hwTailP = cpu_to_hc32 (ohci, td->td_dma);
4321da177e4SLinus Torvalds 		ed->hwHeadP = ed->hwTailP;	/* ED_C, ED_H zeroed */
4331da177e4SLinus Torvalds 		ed->state = ED_IDLE;
4341da177e4SLinus Torvalds 
4351da177e4SLinus Torvalds 		is_out = !(ep->desc.bEndpointAddress & USB_DIR_IN);
4361da177e4SLinus Torvalds 
4371da177e4SLinus Torvalds 		/* FIXME usbcore changes dev->devnum before SET_ADDRESS
4384b26d50bSAnand Gadiyar 		 * succeeds ... otherwise we wouldn't need "pipe".
4391da177e4SLinus Torvalds 		 */
4401da177e4SLinus Torvalds 		info = usb_pipedevice (pipe);
4411da177e4SLinus Torvalds 		ed->type = usb_pipetype(pipe);
4421da177e4SLinus Torvalds 
4431da177e4SLinus Torvalds 		info |= (ep->desc.bEndpointAddress & ~USB_DIR_IN) << 7;
44429cc8897SKuninori Morimoto 		info |= usb_endpoint_maxp(&ep->desc) << 16;
4451da177e4SLinus Torvalds 		if (udev->speed == USB_SPEED_LOW)
4461da177e4SLinus Torvalds 			info |= ED_LOWSPEED;
4471da177e4SLinus Torvalds 		/* only control transfers store pids in tds */
4481da177e4SLinus Torvalds 		if (ed->type != PIPE_CONTROL) {
4491da177e4SLinus Torvalds 			info |= is_out ? ED_OUT : ED_IN;
4501da177e4SLinus Torvalds 			if (ed->type != PIPE_BULK) {
4511da177e4SLinus Torvalds 				/* periodic transfers... */
4521da177e4SLinus Torvalds 				if (ed->type == PIPE_ISOCHRONOUS)
4531da177e4SLinus Torvalds 					info |= ED_ISO;
4541da177e4SLinus Torvalds 				else if (interval > 32)	/* iso can be bigger */
4551da177e4SLinus Torvalds 					interval = 32;
4561da177e4SLinus Torvalds 				ed->interval = interval;
4571da177e4SLinus Torvalds 				ed->load = usb_calc_bus_time (
4581da177e4SLinus Torvalds 					udev->speed, !is_out,
4591da177e4SLinus Torvalds 					ed->type == PIPE_ISOCHRONOUS,
46029cc8897SKuninori Morimoto 					usb_endpoint_maxp(&ep->desc))
4611da177e4SLinus Torvalds 						/ 1000;
4621da177e4SLinus Torvalds 			}
4631da177e4SLinus Torvalds 		}
4641da177e4SLinus Torvalds 		ed->hwINFO = cpu_to_hc32(ohci, info);
4651da177e4SLinus Torvalds 
4661da177e4SLinus Torvalds 		ep->hcpriv = ed;
4671da177e4SLinus Torvalds 	}
4681da177e4SLinus Torvalds 
4691da177e4SLinus Torvalds done:
4701da177e4SLinus Torvalds 	spin_unlock_irqrestore (&ohci->lock, flags);
4711da177e4SLinus Torvalds 	return ed;
4721da177e4SLinus Torvalds }
4731da177e4SLinus Torvalds 
4741da177e4SLinus Torvalds /*-------------------------------------------------------------------------*/
4751da177e4SLinus Torvalds 
4761da177e4SLinus Torvalds /* request unlinking of an endpoint from an operational HC.
4771da177e4SLinus Torvalds  * put the ep on the rm_list
4781da177e4SLinus Torvalds  * real work is done at the next start frame (SF) hardware interrupt
4791da177e4SLinus Torvalds  * caller guarantees HCD is running, so hardware access is safe,
4801da177e4SLinus Torvalds  * and that ed->state is ED_OPER
4811da177e4SLinus Torvalds  */
4821da177e4SLinus Torvalds static void start_ed_unlink (struct ohci_hcd *ohci, struct ed *ed)
4831da177e4SLinus Torvalds {
4841da177e4SLinus Torvalds 	ed->hwINFO |= cpu_to_hc32 (ohci, ED_DEQUEUE);
4851da177e4SLinus Torvalds 	ed_deschedule (ohci, ed);
4861da177e4SLinus Torvalds 
4871da177e4SLinus Torvalds 	/* rm_list is just singly linked, for simplicity */
4881da177e4SLinus Torvalds 	ed->ed_next = ohci->ed_rm_list;
4891da177e4SLinus Torvalds 	ed->ed_prev = NULL;
4901da177e4SLinus Torvalds 	ohci->ed_rm_list = ed;
4911da177e4SLinus Torvalds 
4921da177e4SLinus Torvalds 	/* enable SOF interrupt */
4931da177e4SLinus Torvalds 	ohci_writel (ohci, OHCI_INTR_SF, &ohci->regs->intrstatus);
4941da177e4SLinus Torvalds 	ohci_writel (ohci, OHCI_INTR_SF, &ohci->regs->intrenable);
4951da177e4SLinus Torvalds 	// flush those writes, and get latest HCCA contents
4961da177e4SLinus Torvalds 	(void) ohci_readl (ohci, &ohci->regs->control);
4971da177e4SLinus Torvalds 
4981da177e4SLinus Torvalds 	/* SF interrupt might get delayed; record the frame counter value that
4991da177e4SLinus Torvalds 	 * indicates when the HC isn't looking at it, so concurrent unlinks
5001da177e4SLinus Torvalds 	 * behave.  frame_no wraps every 2^16 msec, and changes right before
5011da177e4SLinus Torvalds 	 * SF is triggered.
5021da177e4SLinus Torvalds 	 */
5031da177e4SLinus Torvalds 	ed->tick = ohci_frame_no(ohci) + 1;
5041da177e4SLinus Torvalds 
5051da177e4SLinus Torvalds }
5061da177e4SLinus Torvalds 
5071da177e4SLinus Torvalds /*-------------------------------------------------------------------------*
5081da177e4SLinus Torvalds  * TD handling functions
5091da177e4SLinus Torvalds  *-------------------------------------------------------------------------*/
5101da177e4SLinus Torvalds 
5111da177e4SLinus Torvalds /* enqueue next TD for this URB (OHCI spec 5.2.8.2) */
5121da177e4SLinus Torvalds 
5131da177e4SLinus Torvalds static void
5141da177e4SLinus Torvalds td_fill (struct ohci_hcd *ohci, u32 info,
5151da177e4SLinus Torvalds 	dma_addr_t data, int len,
5161da177e4SLinus Torvalds 	struct urb *urb, int index)
5171da177e4SLinus Torvalds {
5181da177e4SLinus Torvalds 	struct td		*td, *td_pt;
5191da177e4SLinus Torvalds 	struct urb_priv		*urb_priv = urb->hcpriv;
5201da177e4SLinus Torvalds 	int			is_iso = info & TD_ISO;
5211da177e4SLinus Torvalds 	int			hash;
5221da177e4SLinus Torvalds 
5231da177e4SLinus Torvalds 	// ASSERT (index < urb_priv->length);
5241da177e4SLinus Torvalds 
5251da177e4SLinus Torvalds 	/* aim for only one interrupt per urb.  mostly applies to control
5261da177e4SLinus Torvalds 	 * and iso; other urbs rarely need more than one TD per urb.
5271da177e4SLinus Torvalds 	 * this way, only final tds (or ones with an error) cause IRQs.
5281da177e4SLinus Torvalds 	 * at least immediately; use DI=6 in case any control request is
5291da177e4SLinus Torvalds 	 * tempted to die part way through.  (and to force the hc to flush
5301da177e4SLinus Torvalds 	 * its donelist soonish, even on unlink paths.)
5311da177e4SLinus Torvalds 	 *
5321da177e4SLinus Torvalds 	 * NOTE: could delay interrupts even for the last TD, and get fewer
5331da177e4SLinus Torvalds 	 * interrupts ... increasing per-urb latency by sharing interrupts.
5341da177e4SLinus Torvalds 	 * Drivers that queue bulk urbs may request that behavior.
5351da177e4SLinus Torvalds 	 */
5361da177e4SLinus Torvalds 	if (index != (urb_priv->length - 1)
5371da177e4SLinus Torvalds 			|| (urb->transfer_flags & URB_NO_INTERRUPT))
5381da177e4SLinus Torvalds 		info |= TD_DI_SET (6);
5391da177e4SLinus Torvalds 
5401da177e4SLinus Torvalds 	/* use this td as the next dummy */
5411da177e4SLinus Torvalds 	td_pt = urb_priv->td [index];
5421da177e4SLinus Torvalds 
5431da177e4SLinus Torvalds 	/* fill the old dummy TD */
5441da177e4SLinus Torvalds 	td = urb_priv->td [index] = urb_priv->ed->dummy;
5451da177e4SLinus Torvalds 	urb_priv->ed->dummy = td_pt;
5461da177e4SLinus Torvalds 
5471da177e4SLinus Torvalds 	td->ed = urb_priv->ed;
5481da177e4SLinus Torvalds 	td->next_dl_td = NULL;
5491da177e4SLinus Torvalds 	td->index = index;
5501da177e4SLinus Torvalds 	td->urb = urb;
5511da177e4SLinus Torvalds 	td->data_dma = data;
5521da177e4SLinus Torvalds 	if (!len)
5531da177e4SLinus Torvalds 		data = 0;
5541da177e4SLinus Torvalds 
5551da177e4SLinus Torvalds 	td->hwINFO = cpu_to_hc32 (ohci, info);
5561da177e4SLinus Torvalds 	if (is_iso) {
5571da177e4SLinus Torvalds 		td->hwCBP = cpu_to_hc32 (ohci, data & 0xFFFFF000);
5581da177e4SLinus Torvalds 		*ohci_hwPSWp(ohci, td, 0) = cpu_to_hc16 (ohci,
5591da177e4SLinus Torvalds 						(data & 0x0FFF) | 0xE000);
5601da177e4SLinus Torvalds 	} else {
5611da177e4SLinus Torvalds 		td->hwCBP = cpu_to_hc32 (ohci, data);
5621da177e4SLinus Torvalds 	}
5631da177e4SLinus Torvalds 	if (data)
5641da177e4SLinus Torvalds 		td->hwBE = cpu_to_hc32 (ohci, data + len - 1);
5651da177e4SLinus Torvalds 	else
5661da177e4SLinus Torvalds 		td->hwBE = 0;
5671da177e4SLinus Torvalds 	td->hwNextTD = cpu_to_hc32 (ohci, td_pt->td_dma);
5681da177e4SLinus Torvalds 
5691da177e4SLinus Torvalds 	/* append to queue */
5701da177e4SLinus Torvalds 	list_add_tail (&td->td_list, &td->ed->td_list);
5711da177e4SLinus Torvalds 
5721da177e4SLinus Torvalds 	/* hash it for later reverse mapping */
5731da177e4SLinus Torvalds 	hash = TD_HASH_FUNC (td->td_dma);
5741da177e4SLinus Torvalds 	td->td_hash = ohci->td_hash [hash];
5751da177e4SLinus Torvalds 	ohci->td_hash [hash] = td;
5761da177e4SLinus Torvalds 
5771da177e4SLinus Torvalds 	/* HC might read the TD (or cachelines) right away ... */
5781da177e4SLinus Torvalds 	wmb ();
5791da177e4SLinus Torvalds 	td->ed->hwTailP = td->hwNextTD;
5801da177e4SLinus Torvalds }
5811da177e4SLinus Torvalds 
5821da177e4SLinus Torvalds /*-------------------------------------------------------------------------*/
5831da177e4SLinus Torvalds 
5841da177e4SLinus Torvalds /* Prepare all TDs of a transfer, and queue them onto the ED.
5851da177e4SLinus Torvalds  * Caller guarantees HC is active.
5861da177e4SLinus Torvalds  * Usually the ED is already on the schedule, so TDs might be
5871da177e4SLinus Torvalds  * processed as soon as they're queued.
5881da177e4SLinus Torvalds  */
5891da177e4SLinus Torvalds static void td_submit_urb (
5901da177e4SLinus Torvalds 	struct ohci_hcd	*ohci,
5911da177e4SLinus Torvalds 	struct urb	*urb
5921da177e4SLinus Torvalds ) {
5931da177e4SLinus Torvalds 	struct urb_priv	*urb_priv = urb->hcpriv;
5942621d011SManjunath Goudar 	struct device *dev = ohci_to_hcd(ohci)->self.controller;
5951da177e4SLinus Torvalds 	dma_addr_t	data;
5961da177e4SLinus Torvalds 	int		data_len = urb->transfer_buffer_length;
5971da177e4SLinus Torvalds 	int		cnt = 0;
5981da177e4SLinus Torvalds 	u32		info = 0;
5991da177e4SLinus Torvalds 	int		is_out = usb_pipeout (urb->pipe);
6001da177e4SLinus Torvalds 	int		periodic = 0;
6016f65126cSAlan Stern 	int		i, this_sg_len, n;
6026f65126cSAlan Stern 	struct scatterlist	*sg;
6031da177e4SLinus Torvalds 
6041da177e4SLinus Torvalds 	/* OHCI handles the bulk/interrupt data toggles itself.  We just
6051da177e4SLinus Torvalds 	 * use the device toggle bits for resetting, and rely on the fact
6061da177e4SLinus Torvalds 	 * that resetting toggle is meaningless if the endpoint is active.
6071da177e4SLinus Torvalds 	 */
6081da177e4SLinus Torvalds 	if (!usb_gettoggle (urb->dev, usb_pipeendpoint (urb->pipe), is_out)) {
6091da177e4SLinus Torvalds 		usb_settoggle (urb->dev, usb_pipeendpoint (urb->pipe),
6101da177e4SLinus Torvalds 			is_out, 1);
6111da177e4SLinus Torvalds 		urb_priv->ed->hwHeadP &= ~cpu_to_hc32 (ohci, ED_C);
6121da177e4SLinus Torvalds 	}
6131da177e4SLinus Torvalds 
6141da177e4SLinus Torvalds 	list_add (&urb_priv->pending, &ohci->pending);
6151da177e4SLinus Torvalds 
6166f65126cSAlan Stern 	i = urb->num_mapped_sgs;
6176f65126cSAlan Stern 	if (data_len > 0 && i > 0) {
6186f65126cSAlan Stern 		sg = urb->sg;
6196f65126cSAlan Stern 		data = sg_dma_address(sg);
6206f65126cSAlan Stern 
6216f65126cSAlan Stern 		/*
6226f65126cSAlan Stern 		 * urb->transfer_buffer_length may be smaller than the
6236f65126cSAlan Stern 		 * size of the scatterlist (or vice versa)
6246f65126cSAlan Stern 		 */
6256f65126cSAlan Stern 		this_sg_len = min_t(int, sg_dma_len(sg), data_len);
6266f65126cSAlan Stern 	} else {
6276f65126cSAlan Stern 		sg = NULL;
6281da177e4SLinus Torvalds 		if (data_len)
6291da177e4SLinus Torvalds 			data = urb->transfer_dma;
6301da177e4SLinus Torvalds 		else
6311da177e4SLinus Torvalds 			data = 0;
6326f65126cSAlan Stern 		this_sg_len = data_len;
6336f65126cSAlan Stern 	}
6341da177e4SLinus Torvalds 
6351da177e4SLinus Torvalds 	/* NOTE:  TD_CC is set so we can tell which TDs the HC processed by
6361da177e4SLinus Torvalds 	 * using TD_CC_GET, as well as by seeing them on the done list.
6371da177e4SLinus Torvalds 	 * (CC = NotAccessed ... 0x0F, or 0x0E in PSWs for ISO.)
6381da177e4SLinus Torvalds 	 */
6391da177e4SLinus Torvalds 	switch (urb_priv->ed->type) {
6401da177e4SLinus Torvalds 
6411da177e4SLinus Torvalds 	/* Bulk and interrupt are identical except for where in the schedule
6421da177e4SLinus Torvalds 	 * their EDs live.
6431da177e4SLinus Torvalds 	 */
6441da177e4SLinus Torvalds 	case PIPE_INTERRUPT:
6451da177e4SLinus Torvalds 		/* ... and periodic urbs have extra accounting */
6461da177e4SLinus Torvalds 		periodic = ohci_to_hcd(ohci)->self.bandwidth_int_reqs++ == 0
6471da177e4SLinus Torvalds 			&& ohci_to_hcd(ohci)->self.bandwidth_isoc_reqs == 0;
6481da177e4SLinus Torvalds 		/* FALLTHROUGH */
6491da177e4SLinus Torvalds 	case PIPE_BULK:
6501da177e4SLinus Torvalds 		info = is_out
6511da177e4SLinus Torvalds 			? TD_T_TOGGLE | TD_CC | TD_DP_OUT
6521da177e4SLinus Torvalds 			: TD_T_TOGGLE | TD_CC | TD_DP_IN;
6531da177e4SLinus Torvalds 		/* TDs _could_ transfer up to 8K each */
6546f65126cSAlan Stern 		for (;;) {
6556f65126cSAlan Stern 			n = min(this_sg_len, 4096);
6566f65126cSAlan Stern 
6571da177e4SLinus Torvalds 			/* maybe avoid ED halt on final TD short read */
6586f65126cSAlan Stern 			if (n >= data_len || (i == 1 && n >= this_sg_len)) {
6591da177e4SLinus Torvalds 				if (!(urb->transfer_flags & URB_SHORT_NOT_OK))
6601da177e4SLinus Torvalds 					info |= TD_R;
6616f65126cSAlan Stern 			}
6626f65126cSAlan Stern 			td_fill(ohci, info, data, n, urb, cnt);
6636f65126cSAlan Stern 			this_sg_len -= n;
6646f65126cSAlan Stern 			data_len -= n;
6656f65126cSAlan Stern 			data += n;
6661da177e4SLinus Torvalds 			cnt++;
6676f65126cSAlan Stern 
6686f65126cSAlan Stern 			if (this_sg_len <= 0) {
6696f65126cSAlan Stern 				if (--i <= 0 || data_len <= 0)
6706f65126cSAlan Stern 					break;
6716f65126cSAlan Stern 				sg = sg_next(sg);
6726f65126cSAlan Stern 				data = sg_dma_address(sg);
6736f65126cSAlan Stern 				this_sg_len = min_t(int, sg_dma_len(sg),
6746f65126cSAlan Stern 						data_len);
6756f65126cSAlan Stern 			}
6766f65126cSAlan Stern 		}
6771da177e4SLinus Torvalds 		if ((urb->transfer_flags & URB_ZERO_PACKET)
6781da177e4SLinus Torvalds 				&& cnt < urb_priv->length) {
6791da177e4SLinus Torvalds 			td_fill (ohci, info, 0, 0, urb, cnt);
6801da177e4SLinus Torvalds 			cnt++;
6811da177e4SLinus Torvalds 		}
6821da177e4SLinus Torvalds 		/* maybe kickstart bulk list */
6831da177e4SLinus Torvalds 		if (urb_priv->ed->type == PIPE_BULK) {
6841da177e4SLinus Torvalds 			wmb ();
6851da177e4SLinus Torvalds 			ohci_writel (ohci, OHCI_BLF, &ohci->regs->cmdstatus);
6861da177e4SLinus Torvalds 		}
6871da177e4SLinus Torvalds 		break;
6881da177e4SLinus Torvalds 
6891da177e4SLinus Torvalds 	/* control manages DATA0/DATA1 toggle per-request; SETUP resets it,
6901da177e4SLinus Torvalds 	 * any DATA phase works normally, and the STATUS ack is special.
6911da177e4SLinus Torvalds 	 */
6921da177e4SLinus Torvalds 	case PIPE_CONTROL:
6931da177e4SLinus Torvalds 		info = TD_CC | TD_DP_SETUP | TD_T_DATA0;
6941da177e4SLinus Torvalds 		td_fill (ohci, info, urb->setup_dma, 8, urb, cnt++);
6951da177e4SLinus Torvalds 		if (data_len > 0) {
6961da177e4SLinus Torvalds 			info = TD_CC | TD_R | TD_T_DATA1;
6971da177e4SLinus Torvalds 			info |= is_out ? TD_DP_OUT : TD_DP_IN;
6981da177e4SLinus Torvalds 			/* NOTE:  mishandles transfers >8K, some >4K */
6991da177e4SLinus Torvalds 			td_fill (ohci, info, data, data_len, urb, cnt++);
7001da177e4SLinus Torvalds 		}
7011da177e4SLinus Torvalds 		info = (is_out || data_len == 0)
7021da177e4SLinus Torvalds 			? TD_CC | TD_DP_IN | TD_T_DATA1
7031da177e4SLinus Torvalds 			: TD_CC | TD_DP_OUT | TD_T_DATA1;
7041da177e4SLinus Torvalds 		td_fill (ohci, info, data, 0, urb, cnt++);
7051da177e4SLinus Torvalds 		/* maybe kickstart control list */
7061da177e4SLinus Torvalds 		wmb ();
7071da177e4SLinus Torvalds 		ohci_writel (ohci, OHCI_CLF, &ohci->regs->cmdstatus);
7081da177e4SLinus Torvalds 		break;
7091da177e4SLinus Torvalds 
7101da177e4SLinus Torvalds 	/* ISO has no retransmit, so no toggle; and it uses special TDs.
7111da177e4SLinus Torvalds 	 * Each TD could handle multiple consecutive frames (interval 1);
7121da177e4SLinus Torvalds 	 * we could often reduce the number of TDs here.
7131da177e4SLinus Torvalds 	 */
7141da177e4SLinus Torvalds 	case PIPE_ISOCHRONOUS:
7156a41b4d3SAlan Stern 		for (cnt = urb_priv->td_cnt; cnt < urb->number_of_packets;
7166a41b4d3SAlan Stern 				cnt++) {
7171da177e4SLinus Torvalds 			int	frame = urb->start_frame;
7181da177e4SLinus Torvalds 
7191da177e4SLinus Torvalds 			// FIXME scheduling should handle frame counter
7201da177e4SLinus Torvalds 			// roll-around ... exotic case (and OHCI has
7211da177e4SLinus Torvalds 			// a 2^16 iso range, vs other HCs max of 2^10)
7221da177e4SLinus Torvalds 			frame += cnt * urb->interval;
7231da177e4SLinus Torvalds 			frame &= 0xffff;
7241da177e4SLinus Torvalds 			td_fill (ohci, TD_CC | TD_ISO | frame,
7251da177e4SLinus Torvalds 				data + urb->iso_frame_desc [cnt].offset,
7261da177e4SLinus Torvalds 				urb->iso_frame_desc [cnt].length, urb, cnt);
7271da177e4SLinus Torvalds 		}
728a1f17a87SLibin Yang 		if (ohci_to_hcd(ohci)->self.bandwidth_isoc_reqs == 0) {
729a1f17a87SLibin Yang 			if (quirk_amdiso(ohci))
730ad93562bSAndiry Xu 				usb_amd_quirk_pll_disable();
731a1f17a87SLibin Yang 			if (quirk_amdprefetch(ohci))
7322621d011SManjunath Goudar 				sb800_prefetch(dev, 1);
733a1f17a87SLibin Yang 		}
7341da177e4SLinus Torvalds 		periodic = ohci_to_hcd(ohci)->self.bandwidth_isoc_reqs++ == 0
7351da177e4SLinus Torvalds 			&& ohci_to_hcd(ohci)->self.bandwidth_int_reqs == 0;
7361da177e4SLinus Torvalds 		break;
7371da177e4SLinus Torvalds 	}
7381da177e4SLinus Torvalds 
7391da177e4SLinus Torvalds 	/* start periodic dma if needed */
7401da177e4SLinus Torvalds 	if (periodic) {
7411da177e4SLinus Torvalds 		wmb ();
7421da177e4SLinus Torvalds 		ohci->hc_control |= OHCI_CTRL_PLE|OHCI_CTRL_IE;
7431da177e4SLinus Torvalds 		ohci_writel (ohci, ohci->hc_control, &ohci->regs->control);
7441da177e4SLinus Torvalds 	}
7451da177e4SLinus Torvalds 
7461da177e4SLinus Torvalds 	// ASSERT (urb_priv->length == cnt);
7471da177e4SLinus Torvalds }
7481da177e4SLinus Torvalds 
7491da177e4SLinus Torvalds /*-------------------------------------------------------------------------*
7501da177e4SLinus Torvalds  * Done List handling functions
7511da177e4SLinus Torvalds  *-------------------------------------------------------------------------*/
7521da177e4SLinus Torvalds 
75355d84968SAlan Stern /* calculate transfer length/status and update the urb */
75455d84968SAlan Stern static int td_done(struct ohci_hcd *ohci, struct urb *urb, struct td *td)
7551da177e4SLinus Torvalds {
7561da177e4SLinus Torvalds 	u32	tdINFO = hc32_to_cpup (ohci, &td->hwINFO);
7571da177e4SLinus Torvalds 	int	cc = 0;
75855d84968SAlan Stern 	int	status = -EINPROGRESS;
7591da177e4SLinus Torvalds 
7601da177e4SLinus Torvalds 	list_del (&td->td_list);
7611da177e4SLinus Torvalds 
7621da177e4SLinus Torvalds 	/* ISO ... drivers see per-TD length/status */
7631da177e4SLinus Torvalds 	if (tdINFO & TD_ISO) {
7641da177e4SLinus Torvalds 		u16	tdPSW = ohci_hwPSW(ohci, td, 0);
7651da177e4SLinus Torvalds 		int	dlen = 0;
7661da177e4SLinus Torvalds 
7671da177e4SLinus Torvalds 		/* NOTE:  assumes FC in tdINFO == 0, and that
7681da177e4SLinus Torvalds 		 * only the first of 0..MAXPSW psws is used.
7691da177e4SLinus Torvalds 		 */
7701da177e4SLinus Torvalds 
7711da177e4SLinus Torvalds 		cc = (tdPSW >> 12) & 0xF;
7721da177e4SLinus Torvalds 		if (tdINFO & TD_CC)	/* hc didn't touch? */
77355d84968SAlan Stern 			return status;
7741da177e4SLinus Torvalds 
7751da177e4SLinus Torvalds 		if (usb_pipeout (urb->pipe))
7761da177e4SLinus Torvalds 			dlen = urb->iso_frame_desc [td->index].length;
7771da177e4SLinus Torvalds 		else {
7781da177e4SLinus Torvalds 			/* short reads are always OK for ISO */
7791da177e4SLinus Torvalds 			if (cc == TD_DATAUNDERRUN)
7801da177e4SLinus Torvalds 				cc = TD_CC_NOERROR;
7811da177e4SLinus Torvalds 			dlen = tdPSW & 0x3ff;
7821da177e4SLinus Torvalds 		}
7831da177e4SLinus Torvalds 		urb->actual_length += dlen;
7841da177e4SLinus Torvalds 		urb->iso_frame_desc [td->index].actual_length = dlen;
7851da177e4SLinus Torvalds 		urb->iso_frame_desc [td->index].status = cc_to_error [cc];
7861da177e4SLinus Torvalds 
7871da177e4SLinus Torvalds 		if (cc != TD_CC_NOERROR)
788d2c4254fSOliver Neukum 			ohci_dbg(ohci,
7891da177e4SLinus Torvalds 				"urb %p iso td %p (%d) len %d cc %d\n",
7901da177e4SLinus Torvalds 				urb, td, 1 + td->index, dlen, cc);
7911da177e4SLinus Torvalds 
7921da177e4SLinus Torvalds 	/* BULK, INT, CONTROL ... drivers see aggregate length/status,
7931da177e4SLinus Torvalds 	 * except that "setup" bytes aren't counted and "short" transfers
7941da177e4SLinus Torvalds 	 * might not be reported as errors.
7951da177e4SLinus Torvalds 	 */
7961da177e4SLinus Torvalds 	} else {
7971da177e4SLinus Torvalds 		int	type = usb_pipetype (urb->pipe);
7981da177e4SLinus Torvalds 		u32	tdBE = hc32_to_cpup (ohci, &td->hwBE);
7991da177e4SLinus Torvalds 
8001da177e4SLinus Torvalds 		cc = TD_CC_GET (tdINFO);
8011da177e4SLinus Torvalds 
8021da177e4SLinus Torvalds 		/* update packet status if needed (short is normally ok) */
8031da177e4SLinus Torvalds 		if (cc == TD_DATAUNDERRUN
8041da177e4SLinus Torvalds 				&& !(urb->transfer_flags & URB_SHORT_NOT_OK))
8051da177e4SLinus Torvalds 			cc = TD_CC_NOERROR;
80655d84968SAlan Stern 		if (cc != TD_CC_NOERROR && cc < 0x0E)
80755d84968SAlan Stern 			status = cc_to_error[cc];
8081da177e4SLinus Torvalds 
8091da177e4SLinus Torvalds 		/* count all non-empty packets except control SETUP packet */
8101da177e4SLinus Torvalds 		if ((type != PIPE_CONTROL || td->index != 0) && tdBE != 0) {
8111da177e4SLinus Torvalds 			if (td->hwCBP == 0)
8121da177e4SLinus Torvalds 				urb->actual_length += tdBE - td->data_dma + 1;
8131da177e4SLinus Torvalds 			else
8141da177e4SLinus Torvalds 				urb->actual_length +=
8151da177e4SLinus Torvalds 					  hc32_to_cpup (ohci, &td->hwCBP)
8161da177e4SLinus Torvalds 					- td->data_dma;
8171da177e4SLinus Torvalds 		}
8181da177e4SLinus Torvalds 
8191da177e4SLinus Torvalds 		if (cc != TD_CC_NOERROR && cc < 0x0E)
820d2c4254fSOliver Neukum 			ohci_dbg(ohci,
8211da177e4SLinus Torvalds 				"urb %p td %p (%d) cc %d, len=%d/%d\n",
8221da177e4SLinus Torvalds 				urb, td, 1 + td->index, cc,
8231da177e4SLinus Torvalds 				urb->actual_length,
8241da177e4SLinus Torvalds 				urb->transfer_buffer_length);
8251da177e4SLinus Torvalds 	}
82655d84968SAlan Stern 	return status;
8271da177e4SLinus Torvalds }
8281da177e4SLinus Torvalds 
8291da177e4SLinus Torvalds /*-------------------------------------------------------------------------*/
8301da177e4SLinus Torvalds 
8316e8fe43bSAlan Stern static void ed_halted(struct ohci_hcd *ohci, struct td *td, int cc)
8321da177e4SLinus Torvalds {
8331da177e4SLinus Torvalds 	struct urb		*urb = td->urb;
8346e8fe43bSAlan Stern 	urb_priv_t		*urb_priv = urb->hcpriv;
8351da177e4SLinus Torvalds 	struct ed		*ed = td->ed;
8361da177e4SLinus Torvalds 	struct list_head	*tmp = td->td_list.next;
8371da177e4SLinus Torvalds 	__hc32			toggle = ed->hwHeadP & cpu_to_hc32 (ohci, ED_C);
8381da177e4SLinus Torvalds 
8391da177e4SLinus Torvalds 	/* clear ed halt; this is the td that caused it, but keep it inactive
8401da177e4SLinus Torvalds 	 * until its urb->complete() has a chance to clean up.
8411da177e4SLinus Torvalds 	 */
8421da177e4SLinus Torvalds 	ed->hwINFO |= cpu_to_hc32 (ohci, ED_SKIP);
8431da177e4SLinus Torvalds 	wmb ();
8441da177e4SLinus Torvalds 	ed->hwHeadP &= ~cpu_to_hc32 (ohci, ED_H);
8451da177e4SLinus Torvalds 
8466e8fe43bSAlan Stern 	/* Get rid of all later tds from this urb.  We don't have
8476e8fe43bSAlan Stern 	 * to be careful: no errors and nothing was transferred.
8486e8fe43bSAlan Stern 	 * Also patch the ed so it looks as if those tds completed normally.
8491da177e4SLinus Torvalds 	 */
8501da177e4SLinus Torvalds 	while (tmp != &ed->td_list) {
8511da177e4SLinus Torvalds 		struct td	*next;
8521da177e4SLinus Torvalds 
8531da177e4SLinus Torvalds 		next = list_entry (tmp, struct td, td_list);
8541da177e4SLinus Torvalds 		tmp = next->td_list.next;
8551da177e4SLinus Torvalds 
8561da177e4SLinus Torvalds 		if (next->urb != urb)
8571da177e4SLinus Torvalds 			break;
8581da177e4SLinus Torvalds 
8591da177e4SLinus Torvalds 		/* NOTE: if multi-td control DATA segments get supported,
8601da177e4SLinus Torvalds 		 * this urb had one of them, this td wasn't the last td
8611da177e4SLinus Torvalds 		 * in that segment (TD_R clear), this ed halted because
8621da177e4SLinus Torvalds 		 * of a short read, _and_ URB_SHORT_NOT_OK is clear ...
8631da177e4SLinus Torvalds 		 * then we need to leave the control STATUS packet queued
8641da177e4SLinus Torvalds 		 * and clear ED_SKIP.
8651da177e4SLinus Torvalds 		 */
8661da177e4SLinus Torvalds 
8676e8fe43bSAlan Stern 		list_del(&next->td_list);
8686e8fe43bSAlan Stern 		urb_priv->td_cnt++;
8691da177e4SLinus Torvalds 		ed->hwHeadP = next->hwNextTD | toggle;
8701da177e4SLinus Torvalds 	}
8711da177e4SLinus Torvalds 
8721da177e4SLinus Torvalds 	/* help for troubleshooting:  report anything that
8731da177e4SLinus Torvalds 	 * looks odd ... that doesn't include protocol stalls
8741da177e4SLinus Torvalds 	 * (or maybe some other things)
8751da177e4SLinus Torvalds 	 */
8761da177e4SLinus Torvalds 	switch (cc) {
8771da177e4SLinus Torvalds 	case TD_DATAUNDERRUN:
8781da177e4SLinus Torvalds 		if ((urb->transfer_flags & URB_SHORT_NOT_OK) == 0)
8791da177e4SLinus Torvalds 			break;
8801da177e4SLinus Torvalds 		/* fallthrough */
8811da177e4SLinus Torvalds 	case TD_CC_STALL:
8821da177e4SLinus Torvalds 		if (usb_pipecontrol (urb->pipe))
8831da177e4SLinus Torvalds 			break;
8841da177e4SLinus Torvalds 		/* fallthrough */
8851da177e4SLinus Torvalds 	default:
8861da177e4SLinus Torvalds 		ohci_dbg (ohci,
8871da177e4SLinus Torvalds 			"urb %p path %s ep%d%s %08x cc %d --> status %d\n",
8881da177e4SLinus Torvalds 			urb, urb->dev->devpath,
8891da177e4SLinus Torvalds 			usb_pipeendpoint (urb->pipe),
8901da177e4SLinus Torvalds 			usb_pipein (urb->pipe) ? "in" : "out",
8911da177e4SLinus Torvalds 			hc32_to_cpu (ohci, td->hwINFO),
8921da177e4SLinus Torvalds 			cc, cc_to_error [cc]);
8931da177e4SLinus Torvalds 	}
8941da177e4SLinus Torvalds }
8951da177e4SLinus Torvalds 
896c6fcb85eSAlan Stern /* Add a TD to the done list */
897c6fcb85eSAlan Stern static void add_to_done_list(struct ohci_hcd *ohci, struct td *td)
898c6fcb85eSAlan Stern {
899c6fcb85eSAlan Stern 	struct td	*td2, *td_prev;
900c6fcb85eSAlan Stern 	struct ed	*ed;
901c6fcb85eSAlan Stern 
902c6fcb85eSAlan Stern 	if (td->next_dl_td)
903c6fcb85eSAlan Stern 		return;		/* Already on the list */
904c6fcb85eSAlan Stern 
905c6fcb85eSAlan Stern 	/* Add all the TDs going back until we reach one that's on the list */
906c6fcb85eSAlan Stern 	ed = td->ed;
907c6fcb85eSAlan Stern 	td2 = td_prev = td;
908c6fcb85eSAlan Stern 	list_for_each_entry_continue_reverse(td2, &ed->td_list, td_list) {
909c6fcb85eSAlan Stern 		if (td2->next_dl_td)
910c6fcb85eSAlan Stern 			break;
911c6fcb85eSAlan Stern 		td2->next_dl_td = td_prev;
912c6fcb85eSAlan Stern 		td_prev = td2;
913c6fcb85eSAlan Stern 	}
914c6fcb85eSAlan Stern 
915c6fcb85eSAlan Stern 	if (ohci->dl_end)
916c6fcb85eSAlan Stern 		ohci->dl_end->next_dl_td = td_prev;
917c6fcb85eSAlan Stern 	else
918c6fcb85eSAlan Stern 		ohci->dl_start = td_prev;
919c6fcb85eSAlan Stern 
920c6fcb85eSAlan Stern 	/*
921c6fcb85eSAlan Stern 	 * Make td->next_dl_td point to td itself, to mark the fact
922c6fcb85eSAlan Stern 	 * that td is on the done list.
9231da177e4SLinus Torvalds 	 */
924c6fcb85eSAlan Stern 	ohci->dl_end = td->next_dl_td = td;
92581e38333SAlan Stern 
92681e38333SAlan Stern 	/* Did we just add the latest pending TD? */
92781e38333SAlan Stern 	td2 = ed->pending_td;
92881e38333SAlan Stern 	if (td2 && td2->next_dl_td)
92981e38333SAlan Stern 		ed->pending_td = NULL;
930c6fcb85eSAlan Stern }
931c6fcb85eSAlan Stern 
932c6fcb85eSAlan Stern /* Get the entries on the hardware done queue and put them on our list */
933c6fcb85eSAlan Stern static void update_done_list(struct ohci_hcd *ohci)
9341da177e4SLinus Torvalds {
9351da177e4SLinus Torvalds 	u32		td_dma;
9361da177e4SLinus Torvalds 	struct td	*td = NULL;
9371da177e4SLinus Torvalds 
9381da177e4SLinus Torvalds 	td_dma = hc32_to_cpup (ohci, &ohci->hcca->done_head);
9391da177e4SLinus Torvalds 	ohci->hcca->done_head = 0;
9401da177e4SLinus Torvalds 	wmb();
9411da177e4SLinus Torvalds 
9421da177e4SLinus Torvalds 	/* get TD from hc's singly linked list, and
943c6fcb85eSAlan Stern 	 * add to ours.  ed->td_list changes later.
9441da177e4SLinus Torvalds 	 */
9451da177e4SLinus Torvalds 	while (td_dma) {
9461da177e4SLinus Torvalds 		int		cc;
9471da177e4SLinus Torvalds 
9481da177e4SLinus Torvalds 		td = dma_to_td (ohci, td_dma);
9491da177e4SLinus Torvalds 		if (!td) {
9501da177e4SLinus Torvalds 			ohci_err (ohci, "bad entry %8x\n", td_dma);
9511da177e4SLinus Torvalds 			break;
9521da177e4SLinus Torvalds 		}
9531da177e4SLinus Torvalds 
9541da177e4SLinus Torvalds 		td->hwINFO |= cpu_to_hc32 (ohci, TD_DONE);
9551da177e4SLinus Torvalds 		cc = TD_CC_GET (hc32_to_cpup (ohci, &td->hwINFO));
9561da177e4SLinus Torvalds 
9571da177e4SLinus Torvalds 		/* Non-iso endpoints can halt on error; un-halt,
9581da177e4SLinus Torvalds 		 * and dequeue any other TDs from this urb.
9591da177e4SLinus Torvalds 		 * No other TD could have caused the halt.
9601da177e4SLinus Torvalds 		 */
9611da177e4SLinus Torvalds 		if (cc != TD_CC_NOERROR
9621da177e4SLinus Torvalds 				&& (td->ed->hwHeadP & cpu_to_hc32 (ohci, ED_H)))
9636e8fe43bSAlan Stern 			ed_halted(ohci, td, cc);
9641da177e4SLinus Torvalds 
9651da177e4SLinus Torvalds 		td_dma = hc32_to_cpup (ohci, &td->hwNextTD);
966c6fcb85eSAlan Stern 		add_to_done_list(ohci, td);
9671da177e4SLinus Torvalds 	}
9681da177e4SLinus Torvalds }
9691da177e4SLinus Torvalds 
9701da177e4SLinus Torvalds /*-------------------------------------------------------------------------*/
9711da177e4SLinus Torvalds 
9721da177e4SLinus Torvalds /* there are some urbs/eds to unlink; called in_irq(), with HCD locked */
973cdb4dd15SAlan Stern static void finish_unlinks(struct ohci_hcd *ohci)
9741da177e4SLinus Torvalds {
975cdb4dd15SAlan Stern 	unsigned	tick = ohci_frame_no(ohci);
9761da177e4SLinus Torvalds 	struct ed	*ed, **last;
9771da177e4SLinus Torvalds 
9781da177e4SLinus Torvalds rescan_all:
9791da177e4SLinus Torvalds 	for (last = &ohci->ed_rm_list, ed = *last; ed != NULL; ed = *last) {
9801da177e4SLinus Torvalds 		struct list_head	*entry, *tmp;
9811da177e4SLinus Torvalds 		int			completed, modified;
9821da177e4SLinus Torvalds 		__hc32			*prev;
9831da177e4SLinus Torvalds 
9841da177e4SLinus Torvalds 		/* only take off EDs that the HC isn't using, accounting for
9851da177e4SLinus Torvalds 		 * frame counter wraps and EDs with partially retired TDs
9861da177e4SLinus Torvalds 		 */
987c6fcb85eSAlan Stern 		if (likely(ohci->rh_state == OHCI_RH_RUNNING) &&
988c6fcb85eSAlan Stern 				tick_before(tick, ed->tick)) {
9891da177e4SLinus Torvalds skip_ed:
9901da177e4SLinus Torvalds 			last = &ed->ed_next;
9911da177e4SLinus Torvalds 			continue;
9921da177e4SLinus Torvalds 		}
9931da177e4SLinus Torvalds 		if (!list_empty(&ed->td_list)) {
9941da177e4SLinus Torvalds 			struct td	*td;
9951da177e4SLinus Torvalds 			u32		head;
9961da177e4SLinus Torvalds 
997c6fcb85eSAlan Stern 			td = list_first_entry(&ed->td_list, struct td, td_list);
9981da177e4SLinus Torvalds 
9991da177e4SLinus Torvalds 			/* INTR_WDH may need to clean up first */
1000c6fcb85eSAlan Stern 			head = hc32_to_cpu(ohci, ed->hwHeadP) & TD_MASK;
1001c6fcb85eSAlan Stern 			if (td->td_dma != head &&
1002c6fcb85eSAlan Stern 					ohci->rh_state == OHCI_RH_RUNNING)
10031da177e4SLinus Torvalds 				goto skip_ed;
1004c6fcb85eSAlan Stern 
1005c6fcb85eSAlan Stern 			/* Don't mess up anything already on the done list */
1006c6fcb85eSAlan Stern 			if (td->next_dl_td)
1007c6fcb85eSAlan Stern 				goto skip_ed;
10081da177e4SLinus Torvalds 		}
10091da177e4SLinus Torvalds 
1010977dcfdcSAlan Stern 		/* ED's now officially unlinked, hc doesn't see */
1011977dcfdcSAlan Stern 		ed->hwHeadP &= ~cpu_to_hc32(ohci, ED_H);
1012977dcfdcSAlan Stern 		ed->hwNextED = 0;
1013977dcfdcSAlan Stern 		wmb();
1014977dcfdcSAlan Stern 		ed->hwINFO &= ~cpu_to_hc32(ohci, ED_SKIP | ED_DEQUEUE);
1015977dcfdcSAlan Stern 
10161da177e4SLinus Torvalds 		/* reentrancy:  if we drop the schedule lock, someone might
10171da177e4SLinus Torvalds 		 * have modified this list.  normally it's just prepending
10181da177e4SLinus Torvalds 		 * entries (which we'd ignore), but paranoia won't hurt.
10191da177e4SLinus Torvalds 		 */
10201da177e4SLinus Torvalds 		modified = 0;
10211da177e4SLinus Torvalds 
10221da177e4SLinus Torvalds 		/* unlink urbs as requested, but rescan the list after
10231da177e4SLinus Torvalds 		 * we call a completion since it might have unlinked
10241da177e4SLinus Torvalds 		 * another (earlier) urb
10251da177e4SLinus Torvalds 		 *
10261da177e4SLinus Torvalds 		 * When we get here, the HC doesn't see this ed.  But it
10271da177e4SLinus Torvalds 		 * must not be rescheduled until all completed URBs have
10281da177e4SLinus Torvalds 		 * been given back to the driver.
10291da177e4SLinus Torvalds 		 */
10301da177e4SLinus Torvalds rescan_this:
10311da177e4SLinus Torvalds 		completed = 0;
10321da177e4SLinus Torvalds 		prev = &ed->hwHeadP;
10331da177e4SLinus Torvalds 		list_for_each_safe (entry, tmp, &ed->td_list) {
10341da177e4SLinus Torvalds 			struct td	*td;
10351da177e4SLinus Torvalds 			struct urb	*urb;
10361da177e4SLinus Torvalds 			urb_priv_t	*urb_priv;
10371da177e4SLinus Torvalds 			__hc32		savebits;
103829c8f6a7SDavid Brownell 			u32		tdINFO;
10391da177e4SLinus Torvalds 
10401da177e4SLinus Torvalds 			td = list_entry (entry, struct td, td_list);
10411da177e4SLinus Torvalds 			urb = td->urb;
10421da177e4SLinus Torvalds 			urb_priv = td->urb->hcpriv;
10431da177e4SLinus Torvalds 
1044eb231054SAlan Stern 			if (!urb->unlinked) {
10451da177e4SLinus Torvalds 				prev = &td->hwNextTD;
10461da177e4SLinus Torvalds 				continue;
10471da177e4SLinus Torvalds 			}
10481da177e4SLinus Torvalds 
10491da177e4SLinus Torvalds 			/* patch pointer hc uses */
10501da177e4SLinus Torvalds 			savebits = *prev & ~cpu_to_hc32 (ohci, TD_MASK);
10511da177e4SLinus Torvalds 			*prev = td->hwNextTD | savebits;
10521da177e4SLinus Torvalds 
105329c8f6a7SDavid Brownell 			/* If this was unlinked, the TD may not have been
105429c8f6a7SDavid Brownell 			 * retired ... so manually save the data toggle.
105529c8f6a7SDavid Brownell 			 * The controller ignores the value we save for
105629c8f6a7SDavid Brownell 			 * control and ISO endpoints.
105729c8f6a7SDavid Brownell 			 */
105829c8f6a7SDavid Brownell 			tdINFO = hc32_to_cpup(ohci, &td->hwINFO);
105929c8f6a7SDavid Brownell 			if ((tdINFO & TD_T) == TD_T_DATA0)
106029c8f6a7SDavid Brownell 				ed->hwHeadP &= ~cpu_to_hc32(ohci, ED_C);
106129c8f6a7SDavid Brownell 			else if ((tdINFO & TD_T) == TD_T_DATA1)
106229c8f6a7SDavid Brownell 				ed->hwHeadP |= cpu_to_hc32(ohci, ED_C);
106329c8f6a7SDavid Brownell 
10641da177e4SLinus Torvalds 			/* HC may have partly processed this TD */
10651da177e4SLinus Torvalds 			td_done (ohci, urb, td);
10661da177e4SLinus Torvalds 			urb_priv->td_cnt++;
10671da177e4SLinus Torvalds 
10681da177e4SLinus Torvalds 			/* if URB is done, clean up */
1069a8693424SAlan Stern 			if (urb_priv->td_cnt >= urb_priv->length) {
10701da177e4SLinus Torvalds 				modified = completed = 1;
107155d84968SAlan Stern 				finish_urb(ohci, urb, 0);
10721da177e4SLinus Torvalds 			}
10731da177e4SLinus Torvalds 		}
10741da177e4SLinus Torvalds 		if (completed && !list_empty (&ed->td_list))
10751da177e4SLinus Torvalds 			goto rescan_this;
10761da177e4SLinus Torvalds 
1077977dcfdcSAlan Stern 		/*
1078977dcfdcSAlan Stern 		 * If no TDs are queued, take ED off the ed_rm_list.
1079977dcfdcSAlan Stern 		 * Otherwise, if the HC is running, reschedule.
1080977dcfdcSAlan Stern 		 * If not, leave it on the list for further dequeues.
1081977dcfdcSAlan Stern 		 */
1082977dcfdcSAlan Stern 		if (list_empty(&ed->td_list)) {
1083977dcfdcSAlan Stern 			*last = ed->ed_next;
1084977dcfdcSAlan Stern 			ed->ed_next = NULL;
1085*7d8021c9SAlan Stern 			ed->state = ED_IDLE;
108681e38333SAlan Stern 			list_del(&ed->in_use_list);
1087977dcfdcSAlan Stern 		} else if (ohci->rh_state == OHCI_RH_RUNNING) {
1088977dcfdcSAlan Stern 			*last = ed->ed_next;
1089977dcfdcSAlan Stern 			ed->ed_next = NULL;
10901da177e4SLinus Torvalds 			ed_schedule(ohci, ed);
1091977dcfdcSAlan Stern 		} else {
1092977dcfdcSAlan Stern 			last = &ed->ed_next;
10931da177e4SLinus Torvalds 		}
10941da177e4SLinus Torvalds 
10951da177e4SLinus Torvalds 		if (modified)
10961da177e4SLinus Torvalds 			goto rescan_all;
10971da177e4SLinus Torvalds 	}
10981da177e4SLinus Torvalds 
10991da177e4SLinus Torvalds 	/* maybe reenable control and bulk lists */
1100b7463c71SAlan Stern 	if (ohci->rh_state == OHCI_RH_RUNNING && !ohci->ed_rm_list) {
11011da177e4SLinus Torvalds 		u32	command = 0, control = 0;
11021da177e4SLinus Torvalds 
11031da177e4SLinus Torvalds 		if (ohci->ed_controltail) {
11041da177e4SLinus Torvalds 			command |= OHCI_CLF;
110589a0fd18SMike Nuss 			if (quirk_zfmicro(ohci))
11060e498763SDavid Brownell 				mdelay(1);
11071da177e4SLinus Torvalds 			if (!(ohci->hc_control & OHCI_CTRL_CLE)) {
11081da177e4SLinus Torvalds 				control |= OHCI_CTRL_CLE;
11091da177e4SLinus Torvalds 				ohci_writel (ohci, 0,
11101da177e4SLinus Torvalds 					&ohci->regs->ed_controlcurrent);
11111da177e4SLinus Torvalds 			}
11121da177e4SLinus Torvalds 		}
11131da177e4SLinus Torvalds 		if (ohci->ed_bulktail) {
11141da177e4SLinus Torvalds 			command |= OHCI_BLF;
111589a0fd18SMike Nuss 			if (quirk_zfmicro(ohci))
11160e498763SDavid Brownell 				mdelay(1);
11171da177e4SLinus Torvalds 			if (!(ohci->hc_control & OHCI_CTRL_BLE)) {
11181da177e4SLinus Torvalds 				control |= OHCI_CTRL_BLE;
11191da177e4SLinus Torvalds 				ohci_writel (ohci, 0,
11201da177e4SLinus Torvalds 					&ohci->regs->ed_bulkcurrent);
11211da177e4SLinus Torvalds 			}
11221da177e4SLinus Torvalds 		}
11231da177e4SLinus Torvalds 
11241da177e4SLinus Torvalds 		/* CLE/BLE to enable, CLF/BLF to (maybe) kickstart */
11251da177e4SLinus Torvalds 		if (control) {
11261da177e4SLinus Torvalds 			ohci->hc_control |= control;
112789a0fd18SMike Nuss 			if (quirk_zfmicro(ohci))
11280e498763SDavid Brownell 				mdelay(1);
11291da177e4SLinus Torvalds 			ohci_writel (ohci, ohci->hc_control,
11301da177e4SLinus Torvalds 					&ohci->regs->control);
11311da177e4SLinus Torvalds 		}
11320e498763SDavid Brownell 		if (command) {
113389a0fd18SMike Nuss 			if (quirk_zfmicro(ohci))
11340e498763SDavid Brownell 				mdelay(1);
11351da177e4SLinus Torvalds 			ohci_writel (ohci, command, &ohci->regs->cmdstatus);
11361da177e4SLinus Torvalds 		}
11371da177e4SLinus Torvalds 	}
11380e498763SDavid Brownell }
11391da177e4SLinus Torvalds 
11401da177e4SLinus Torvalds 
11411da177e4SLinus Torvalds 
11421da177e4SLinus Torvalds /*-------------------------------------------------------------------------*/
11431da177e4SLinus Torvalds 
114495d9a01dSAlan Stern /* Take back a TD from the host controller */
114589a0fd18SMike Nuss static void takeback_td(struct ohci_hcd *ohci, struct td *td)
11461da177e4SLinus Torvalds {
11471da177e4SLinus Torvalds 	struct urb	*urb = td->urb;
11481da177e4SLinus Torvalds 	urb_priv_t	*urb_priv = urb->hcpriv;
11491da177e4SLinus Torvalds 	struct ed	*ed = td->ed;
115055d84968SAlan Stern 	int		status;
11511da177e4SLinus Torvalds 
11521da177e4SLinus Torvalds 	/* update URB's length and status from TD */
115355d84968SAlan Stern 	status = td_done(ohci, urb, td);
11541da177e4SLinus Torvalds 	urb_priv->td_cnt++;
11551da177e4SLinus Torvalds 
11561da177e4SLinus Torvalds 	/* If all this urb's TDs are done, call complete() */
1157a8693424SAlan Stern 	if (urb_priv->td_cnt >= urb_priv->length)
115855d84968SAlan Stern 		finish_urb(ohci, urb, status);
11591da177e4SLinus Torvalds 
11601da177e4SLinus Torvalds 	/* clean schedule:  unlink EDs that are no longer busy */
11611da177e4SLinus Torvalds 	if (list_empty(&ed->td_list)) {
11621da177e4SLinus Torvalds 		if (ed->state == ED_OPER)
11631da177e4SLinus Torvalds 			start_ed_unlink(ohci, ed);
11641da177e4SLinus Torvalds 
11651da177e4SLinus Torvalds 	/* ... reenabling halted EDs only after fault cleanup */
116689a0fd18SMike Nuss 	} else if ((ed->hwINFO & cpu_to_hc32(ohci, ED_SKIP | ED_DEQUEUE))
11671da177e4SLinus Torvalds 			== cpu_to_hc32(ohci, ED_SKIP)) {
11681da177e4SLinus Torvalds 		td = list_entry(ed->td_list.next, struct td, td_list);
11691da177e4SLinus Torvalds 		if (!(td->hwINFO & cpu_to_hc32(ohci, TD_DONE))) {
11701da177e4SLinus Torvalds 			ed->hwINFO &= ~cpu_to_hc32(ohci, ED_SKIP);
11711da177e4SLinus Torvalds 			/* ... hc may need waking-up */
11721da177e4SLinus Torvalds 			switch (ed->type) {
11731da177e4SLinus Torvalds 			case PIPE_CONTROL:
11741da177e4SLinus Torvalds 				ohci_writel(ohci, OHCI_CLF,
11751da177e4SLinus Torvalds 						&ohci->regs->cmdstatus);
11761da177e4SLinus Torvalds 				break;
11771da177e4SLinus Torvalds 			case PIPE_BULK:
11781da177e4SLinus Torvalds 				ohci_writel(ohci, OHCI_BLF,
11791da177e4SLinus Torvalds 						&ohci->regs->cmdstatus);
11801da177e4SLinus Torvalds 				break;
11811da177e4SLinus Torvalds 			}
11821da177e4SLinus Torvalds 		}
11831da177e4SLinus Torvalds 	}
118489a0fd18SMike Nuss }
11851da177e4SLinus Torvalds 
118689a0fd18SMike Nuss /*
118789a0fd18SMike Nuss  * Process normal completions (error or success) and clean the schedules.
118889a0fd18SMike Nuss  *
118989a0fd18SMike Nuss  * This is the main path for handing urbs back to drivers.  The only other
119089a0fd18SMike Nuss  * normal path is finish_unlinks(), which unlinks URBs using ed_rm_list,
119195d9a01dSAlan Stern  * instead of scanning the (re-reversed) donelist as this does.
119289a0fd18SMike Nuss  */
1193c6fcb85eSAlan Stern static void process_done_list(struct ohci_hcd *ohci)
119489a0fd18SMike Nuss {
1195c6fcb85eSAlan Stern 	struct td	*td;
119689a0fd18SMike Nuss 
1197c6fcb85eSAlan Stern 	while (ohci->dl_start) {
1198c6fcb85eSAlan Stern 		td = ohci->dl_start;
1199c6fcb85eSAlan Stern 		if (td == ohci->dl_end)
1200c6fcb85eSAlan Stern 			ohci->dl_start = ohci->dl_end = NULL;
1201c6fcb85eSAlan Stern 		else
1202c6fcb85eSAlan Stern 			ohci->dl_start = td->next_dl_td;
120350ce5c06SAlan Stern 
120489a0fd18SMike Nuss 		takeback_td(ohci, td);
12051da177e4SLinus Torvalds 	}
12061da177e4SLinus Torvalds }
1207cdb4dd15SAlan Stern 
1208cdb4dd15SAlan Stern /*
1209cdb4dd15SAlan Stern  * TD takeback and URB giveback must be single-threaded.
1210cdb4dd15SAlan Stern  * This routine takes care of it all.
1211cdb4dd15SAlan Stern  */
1212cdb4dd15SAlan Stern static void ohci_work(struct ohci_hcd *ohci)
1213cdb4dd15SAlan Stern {
1214cdb4dd15SAlan Stern 	if (ohci->working) {
1215cdb4dd15SAlan Stern 		ohci->restart_work = 1;
1216cdb4dd15SAlan Stern 		return;
1217cdb4dd15SAlan Stern 	}
1218cdb4dd15SAlan Stern 	ohci->working = 1;
1219cdb4dd15SAlan Stern 
1220cdb4dd15SAlan Stern  restart:
1221cdb4dd15SAlan Stern 	process_done_list(ohci);
1222cdb4dd15SAlan Stern 	if (ohci->ed_rm_list)
1223cdb4dd15SAlan Stern 		finish_unlinks(ohci);
1224cdb4dd15SAlan Stern 
1225cdb4dd15SAlan Stern 	if (ohci->restart_work) {
1226cdb4dd15SAlan Stern 		ohci->restart_work = 0;
1227cdb4dd15SAlan Stern 		goto restart;
1228cdb4dd15SAlan Stern 	}
1229cdb4dd15SAlan Stern 	ohci->working = 0;
1230cdb4dd15SAlan Stern }
1231