ohci-q.c revision 55d8496837cf124f68656e4242a5e20eb592fd54
11da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/*
21da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * OHCI HCD (Host Controller Driver) for USB.
31da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *
41da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * (C) Copyright 1999 Roman Weissgaerber <weissg@vienna.at>
51da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * (C) Copyright 2000-2002 David Brownell <dbrownell@users.sourceforge.net>
61da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *
7475be4d85a274d0961593db41cf85689db1d583cJoe Perches * This file is licenced under the GPL.
81da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds */
91da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
101da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#include <linux/irq.h>
111da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
121da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic void urb_free_priv (struct ohci_hcd *hc, urb_priv_t *urb_priv)
131da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
141da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	int		last = urb_priv->length - 1;
151da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
161da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (last >= 0) {
171da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		int		i;
181da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		struct td	*td;
191da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
201da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		for (i = 0; i <= last; i++) {
215a0e3ad6af8660be21ca98a971cd00f331318c05Tejun Heo			td = urb_priv->td [i];
221da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			if (td)
231da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds				td_free (hc, td);
241da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		}
251da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
261da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
271da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	list_del (&urb_priv->pending);
281da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	kfree (urb_priv);
291da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
301da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
31f4e64333f829af6b2fe536b0f556d7a6b561c0efSam Ravnborg/*-------------------------------------------------------------------------*/
321da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
331da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/*
341da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * URB goes back to driver, and isn't reissued.
351da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * It's completely gone from HC data structures.
361da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * PRECONDITION:  ohci lock held, irqs blocked.
371da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds */
381da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic void
391da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsfinish_urb(struct ohci_hcd *ohci, struct urb *urb, int status)
401da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds__releases(ohci->lock)
411da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds__acquires(ohci->lock)
421da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
431da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	// ASSERT (urb->hcpriv != 0);
441da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
451da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	urb_free_priv (ohci, urb->hcpriv);
461da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (likely(status == -EINPROGRESS))
471da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		status = 0;
481da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
491da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	switch (usb_pipetype (urb->pipe)) {
501da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	case PIPE_ISOCHRONOUS:
511da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		ohci_to_hcd(ohci)->self.bandwidth_isoc_reqs--;
52475be4d85a274d0961593db41cf85689db1d583cJoe Perches		break;
53475be4d85a274d0961593db41cf85689db1d583cJoe Perches	case PIPE_INTERRUPT:
54475be4d85a274d0961593db41cf85689db1d583cJoe Perches		ohci_to_hcd(ohci)->self.bandwidth_int_reqs--;
55475be4d85a274d0961593db41cf85689db1d583cJoe Perches		break;
56475be4d85a274d0961593db41cf85689db1d583cJoe Perches	}
57475be4d85a274d0961593db41cf85689db1d583cJoe Perches
58475be4d85a274d0961593db41cf85689db1d583cJoe Perches#ifdef OHCI_VERBOSE_DEBUG
59475be4d85a274d0961593db41cf85689db1d583cJoe Perches	urb_print(urb, "RET", usb_pipeout (urb->pipe), status);
60475be4d85a274d0961593db41cf85689db1d583cJoe Perches#endif
61475be4d85a274d0961593db41cf85689db1d583cJoe Perches
62475be4d85a274d0961593db41cf85689db1d583cJoe Perches	/* urb->complete() can reenter this HCD */
63475be4d85a274d0961593db41cf85689db1d583cJoe Perches	usb_hcd_unlink_urb_from_ep(ohci_to_hcd(ohci), urb);
64475be4d85a274d0961593db41cf85689db1d583cJoe Perches	spin_unlock (&ohci->lock);
65475be4d85a274d0961593db41cf85689db1d583cJoe Perches	urb->status = status;
66475be4d85a274d0961593db41cf85689db1d583cJoe Perches	usb_hcd_giveback_urb (ohci_to_hcd(ohci), urb);
67475be4d85a274d0961593db41cf85689db1d583cJoe Perches	spin_lock (&ohci->lock);
68475be4d85a274d0961593db41cf85689db1d583cJoe Perches
69475be4d85a274d0961593db41cf85689db1d583cJoe Perches	/* stop periodic dma if it's not needed */
70475be4d85a274d0961593db41cf85689db1d583cJoe Perches	if (ohci_to_hcd(ohci)->self.bandwidth_isoc_reqs == 0
71475be4d85a274d0961593db41cf85689db1d583cJoe Perches			&& ohci_to_hcd(ohci)->self.bandwidth_int_reqs == 0) {
72475be4d85a274d0961593db41cf85689db1d583cJoe Perches		ohci->hc_control &= ~(OHCI_CTRL_PLE|OHCI_CTRL_IE);
73475be4d85a274d0961593db41cf85689db1d583cJoe Perches		ohci_writel (ohci, ohci->hc_control, &ohci->regs->control);
74475be4d85a274d0961593db41cf85689db1d583cJoe Perches	}
75475be4d85a274d0961593db41cf85689db1d583cJoe Perches}
76475be4d85a274d0961593db41cf85689db1d583cJoe Perches
77475be4d85a274d0961593db41cf85689db1d583cJoe Perches
78475be4d85a274d0961593db41cf85689db1d583cJoe Perches/*-------------------------------------------------------------------------*
79475be4d85a274d0961593db41cf85689db1d583cJoe Perches * ED handling functions
801da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *-------------------------------------------------------------------------*/
811da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
821da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/* search for the right schedule branch to use for a periodic ed.
831da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * does some load balancing; returns the branch, or negative errno.
84c4028958b6ecad064b1a6303a6a5906d4fe48d73David Howells */
851da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic int balance (struct ohci_hcd *ohci, int interval, int load)
86c4028958b6ecad064b1a6303a6a5906d4fe48d73David Howells{
87c4028958b6ecad064b1a6303a6a5906d4fe48d73David Howells	int	i, branch = -ENOSPC;
881da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
89475be4d85a274d0961593db41cf85689db1d583cJoe Perches	/* iso periods can be huge; iso tds specify frame numbers */
901da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (interval > NUM_INTS)
911da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		interval = NUM_INTS;
921da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
931da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/* search for the least loaded schedule branch of that period
941da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 * that has enough bandwidth left unreserved.
951da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 */
961da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	for (i = 0; i < interval ; i++) {
971da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		if (branch < 0 || ohci->load [branch] > ohci->load [i]) {
981da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			int	j;
991da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
100475be4d85a274d0961593db41cf85689db1d583cJoe Perches			/* usb 1.1 says 90% of one frame */
1011da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			for (j = i; j < NUM_INTS; j += interval) {
1021da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds				if ((ohci->load [j] + load) > 900)
1031da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds					break;
1041da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			}
1051da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			if (j < NUM_INTS)
1061da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds				continue;
1071da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			branch = i;
1081da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		}
1091da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
1101da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return branch;
1111da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
1121da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1131da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/*-------------------------------------------------------------------------*/
1141da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
115672c3fd9069e5a138f9d4afc9aeb5aa34aacce32Adrian Bunk/* both iso and interrupt requests have periods; this routine puts them
1161da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * into the schedule tree in the apppropriate place.  most iso devices use
1171da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * 1msec periods, but that's not required.
1181da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds */
1191da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic void periodic_link (struct ohci_hcd *ohci, struct ed *ed)
1201da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
1211da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	unsigned	i;
1221da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1231da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	ohci_vdbg (ohci, "link %sed %p branch %d [%dus.], interval %d\n",
1241da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		(ed->hwINFO & cpu_to_hc32 (ohci, ED_ISO)) ? "iso " : "",
1251da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		ed, ed->branch, ed->load, ed->interval);
1261da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1271da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	for (i = ed->branch; i < NUM_INTS; i += ed->interval) {
1281da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		struct ed	**prev = &ohci->periodic [i];
1291da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		__hc32		*prev_p = &ohci->hcca->int_table [i];
1301da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		struct ed	*here = *prev;
1311da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1321da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		/* sorting each branch by period (slow before fast)
1331da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		 * lets us share the faster parts of the tree.
1341da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		 * (plus maybe: put interrupt eds before iso)
1351da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		 */
1361da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		while (here && ed != here) {
1371da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			if (ed->interval > here->interval)
1381da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds				break;
1391da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			prev = &here->ed_next;
1401da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			prev_p = &here->hwNextED;
1411da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			here = *prev;
1421da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		}
1431da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		if (ed != here) {
1441da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			ed->ed_next = here;
1451da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			if (here)
1461da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds				ed->hwNextED = *prev_p;
1471da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			wmb ();
1481da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			*prev = ed;
1491da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			*prev_p = cpu_to_hc32(ohci, ed->dma);
1501da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			wmb();
1511da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		}
1521da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		ohci->load [i] += ed->load;
1531da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
1541da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	ohci_to_hcd(ohci)->self.bandwidth_allocated += ed->load / ed->interval;
1551da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
1561da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1571da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/* link an ed into one of the HC chains */
1581da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1591da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic int ed_schedule (struct ohci_hcd *ohci, struct ed *ed)
1601da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
1611da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	int	branch;
1621da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1631da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (ohci_to_hcd(ohci)->state == HC_STATE_QUIESCING)
1641da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		return -EAGAIN;
1651da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1661da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	ed->state = ED_OPER;
1671da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	ed->ed_prev = NULL;
1681da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	ed->ed_next = NULL;
1691da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	ed->hwNextED = 0;
1701da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (quirk_zfmicro(ohci)
1711da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			&& (ed->type == PIPE_INTERRUPT)
1721da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			&& !(ohci->eds_scheduled++))
1731da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		mod_timer(&ohci->unlink_watchdog, round_jiffies_relative(HZ));
1741da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	wmb ();
1751da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1761da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/* we care about rm_list when setting CLE/BLE in case the HC was at
1771da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 * work on some TD when CLE/BLE was turned off, and isn't quiesced
1781da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 * yet.  finish_unlinks() restarts as needed, some upcoming INTR_SF.
1791da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 *
1801da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 * control and bulk EDs are doubly linked (ed_next, ed_prev), but
1811da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 * periodic ones are singly linked (ed_next). that's because the
1821da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 * periodic schedule encodes a tree like figure 3-5 in the ohci
1831da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 * spec:  each qh can have several "previous" nodes, and the tree
1841da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 * doesn't have unused/idle descriptors.
1851da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 */
1861da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	switch (ed->type) {
1871da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	case PIPE_CONTROL:
1881da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		if (ohci->ed_controltail == NULL) {
1891da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			WARN_ON (ohci->hc_control & OHCI_CTRL_CLE);
1901da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			ohci_writel (ohci, ed->dma,
1911da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds					&ohci->regs->ed_controlhead);
1921da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		} else {
1931da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			ohci->ed_controltail->ed_next = ed;
1941da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			ohci->ed_controltail->hwNextED = cpu_to_hc32 (ohci,
1951da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds								ed->dma);
1961da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		}
1971da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		ed->ed_prev = ohci->ed_controltail;
1981da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		if (!ohci->ed_controltail && !ohci->ed_rm_list) {
1991da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			wmb();
2001da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			ohci->hc_control |= OHCI_CTRL_CLE;
2011da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			ohci_writel (ohci, 0, &ohci->regs->ed_controlcurrent);
2021da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			ohci_writel (ohci, ohci->hc_control,
2031da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds					&ohci->regs->control);
2041da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		}
2051da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		ohci->ed_controltail = ed;
2061da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		break;
2071da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
2081da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	case PIPE_BULK:
2091da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		if (ohci->ed_bulktail == NULL) {
2101da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			WARN_ON (ohci->hc_control & OHCI_CTRL_BLE);
2111da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			ohci_writel (ohci, ed->dma, &ohci->regs->ed_bulkhead);
2121da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		} else {
2131da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			ohci->ed_bulktail->ed_next = ed;
2141da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			ohci->ed_bulktail->hwNextED = cpu_to_hc32 (ohci,
2151da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds								ed->dma);
2161da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		}
2171da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		ed->ed_prev = ohci->ed_bulktail;
2181da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		if (!ohci->ed_bulktail && !ohci->ed_rm_list) {
2191da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			wmb();
2201da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			ohci->hc_control |= OHCI_CTRL_BLE;
2211da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			ohci_writel (ohci, 0, &ohci->regs->ed_bulkcurrent);
2221da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			ohci_writel (ohci, ohci->hc_control,
2231da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds					&ohci->regs->control);
2241da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		}
2251da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		ohci->ed_bulktail = ed;
2261da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		break;
2271da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
2281da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	// case PIPE_INTERRUPT:
2291da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	// case PIPE_ISOCHRONOUS:
2301da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	default:
2311da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		branch = balance (ohci, ed->interval, ed->load);
2321da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		if (branch < 0) {
2331da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			ohci_dbg (ohci,
2341da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds				"ERR %d, interval %d msecs, load %d\n",
2351da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds				branch, ed->interval, ed->load);
2361da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			// FIXME if there are TDs queued, fail them!
2371da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			return branch;
2381da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		}
2391da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		ed->branch = branch;
2401da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		periodic_link (ohci, ed);
2411da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
2421da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
2431da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/* the HC may not see the schedule updates yet, but if it does
2441da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 * then they'll be properly ordered.
2451da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 */
2461da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return 0;
2471da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
2481da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
2491da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/*-------------------------------------------------------------------------*/
2501da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
2511da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/* scan the periodic table to find and unlink this ED */
2521da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic void periodic_unlink (struct ohci_hcd *ohci, struct ed *ed)
2531da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
2541da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	int	i;
2551da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
2561da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	for (i = ed->branch; i < NUM_INTS; i += ed->interval) {
2571da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		struct ed	*temp;
2581da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		struct ed	**prev = &ohci->periodic [i];
2591da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		__hc32		*prev_p = &ohci->hcca->int_table [i];
260475be4d85a274d0961593db41cf85689db1d583cJoe Perches
2611da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		while (*prev && (temp = *prev) != ed) {
2621da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			prev_p = &temp->hwNextED;
2631da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			prev = &temp->ed_next;
264475be4d85a274d0961593db41cf85689db1d583cJoe Perches		}
2651da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		if (*prev) {
2661da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			*prev_p = ed->hwNextED;
2671da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			*prev = ed->ed_next;
2681da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		}
2691da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		ohci->load [i] -= ed->load;
2701da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
2711da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	ohci_to_hcd(ohci)->self.bandwidth_allocated -= ed->load / ed->interval;
2721da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
2731da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	ohci_vdbg (ohci, "unlink %sed %p branch %d [%dus.], interval %d\n",
274475be4d85a274d0961593db41cf85689db1d583cJoe Perches		(ed->hwINFO & cpu_to_hc32 (ohci, ED_ISO)) ? "iso " : "",
2751da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		ed, ed->branch, ed->load, ed->interval);
2761da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
2771da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
2781da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/* unlink an ed from one of the HC chains.
2791da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * just the link to the ed is unlinked.
2801da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * the link from the ed still points to another operational ed or 0
2811da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * so the HC can eventually finish the processing of the unlinked ed
2821da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * (assuming it already started that, which needn't be true).
2831da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *
2841da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * ED_UNLINK is a transient state: the HC may still see this ED, but soon
2851da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * it won't.  ED_SKIP means the HC will finish its current transaction,
2861da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * but won't start anything new.  The TD queue may still grow; device
2871da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * drivers don't know about this HCD-internal state.
2881da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *
2891da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * When the HC can't see the ED, something changes ED_UNLINK to one of:
2901da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *
2911da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *  - ED_OPER: when there's any request queued, the ED gets rescheduled
2921da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *    immediately.  HC should be working on them.
2931da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *
2941da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *  - ED_IDLE:  when there's no TD queue. there's no reason for the HC
2951da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *    to care about this ED; safe to disable the endpoint.
2961da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *
2971da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * When finish_unlinks() runs later, after SOF interrupt, it will often
2981da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * complete one or more URB unlinks before making that state change.
2991da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds */
3001da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic void ed_deschedule (struct ohci_hcd *ohci, struct ed *ed)
3011da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
3021da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	ed->hwINFO |= cpu_to_hc32 (ohci, ED_SKIP);
3031da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	wmb ();
3041da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	ed->state = ED_UNLINK;
3051da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
3061da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/* To deschedule something from the control or bulk list, just
3071da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 * clear CLE/BLE and wait.  There's no safe way to scrub out list
3081da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 * head/current registers until later, and "later" isn't very
3091da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 * tightly specified.  Figure 6-5 and Section 6.4.2.2 show how
3101da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 * the HC is reading the ED queues (while we modify them).
3111da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 *
3121da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 * For now, ed_schedule() is "later".  It might be good paranoia
3131da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 * to scrub those registers in finish_unlinks(), in case of bugs
3141da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 * that make the HC try to use them.
3151da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 */
3161da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	switch (ed->type) {
3171da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	case PIPE_CONTROL:
3181da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		/* remove ED from the HC's list: */
3191da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		if (ed->ed_prev == NULL) {
3201da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			if (!ed->hwNextED) {
3211da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds				ohci->hc_control &= ~OHCI_CTRL_CLE;
3221da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds				ohci_writel (ohci, ohci->hc_control,
3231da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds						&ohci->regs->control);
3241da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds				// a ohci_readl() later syncs CLE with the HC
3251da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			} else
3261da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds				ohci_writel (ohci,
3271da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds					hc32_to_cpup (ohci, &ed->hwNextED),
3281da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds					&ohci->regs->ed_controlhead);
3291da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		} else {
3301da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			ed->ed_prev->ed_next = ed->ed_next;
3311da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			ed->ed_prev->hwNextED = ed->hwNextED;
3321da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		}
3331da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		/* remove ED from the HCD's list: */
3341da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		if (ohci->ed_controltail == ed) {
3351da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			ohci->ed_controltail = ed->ed_prev;
3361da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			if (ohci->ed_controltail)
337475be4d85a274d0961593db41cf85689db1d583cJoe Perches				ohci->ed_controltail->ed_next = NULL;
3381da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		} else if (ed->ed_next) {
3391da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			ed->ed_next->ed_prev = ed->ed_prev;
3401da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		}
3411da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		break;
3421da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
343475be4d85a274d0961593db41cf85689db1d583cJoe Perches	case PIPE_BULK:
3441da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		/* remove ED from the HC's list: */
3451da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		if (ed->ed_prev == NULL) {
3461da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			if (!ed->hwNextED) {
3471da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds				ohci->hc_control &= ~OHCI_CTRL_BLE;
3481da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds				ohci_writel (ohci, ohci->hc_control,
3491da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds						&ohci->regs->control);
3501da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds				// a ohci_readl() later syncs BLE with the HC
3511da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			} else
3521da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds				ohci_writel (ohci,
3531da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds					hc32_to_cpup (ohci, &ed->hwNextED),
3541da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds					&ohci->regs->ed_bulkhead);
3551da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		} else {
3561da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			ed->ed_prev->ed_next = ed->ed_next;
3571da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			ed->ed_prev->hwNextED = ed->hwNextED;
3581da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		}
3591da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		/* remove ED from the HCD's list: */
3601da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		if (ohci->ed_bulktail == ed) {
3611da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			ohci->ed_bulktail = ed->ed_prev;
3621da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			if (ohci->ed_bulktail)
3631da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds				ohci->ed_bulktail->ed_next = NULL;
3641da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		} else if (ed->ed_next) {
3651da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			ed->ed_next->ed_prev = ed->ed_prev;
3661da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		}
367475be4d85a274d0961593db41cf85689db1d583cJoe Perches		break;
3681da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
3691da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	// case PIPE_INTERRUPT:
3701da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	// case PIPE_ISOCHRONOUS:
371475be4d85a274d0961593db41cf85689db1d583cJoe Perches	default:
3721da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		periodic_unlink (ohci, ed);
3731da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		break;
3741da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
3751da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
3761da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
3771da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
3781da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/*-------------------------------------------------------------------------*/
3791da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
3801da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/* get and maybe (re)init an endpoint. init _should_ be done only as part
3811da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * of enumeration, usb_set_configuration() or usb_set_interface().
3821da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds */
3831da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic struct ed *ed_get (
3841da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct ohci_hcd		*ohci,
3851da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct usb_host_endpoint *ep,
3861da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct usb_device	*udev,
387475be4d85a274d0961593db41cf85689db1d583cJoe Perches	unsigned int		pipe,
388475be4d85a274d0961593db41cf85689db1d583cJoe Perches	int			interval
389475be4d85a274d0961593db41cf85689db1d583cJoe Perches) {
3901da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct ed		*ed;
3911da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	unsigned long		flags;
3921da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
3931da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	spin_lock_irqsave (&ohci->lock, flags);
3941da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
395475be4d85a274d0961593db41cf85689db1d583cJoe Perches	if (!(ed = ep->hcpriv)) {
3961da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		struct td	*td;
3971da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		int		is_out;
3981da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		u32		info;
3991da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
4001da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		ed = ed_alloc (ohci, GFP_ATOMIC);
4011da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		if (!ed) {
4021da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			/* out of memory */
4031da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			goto done;
404475be4d85a274d0961593db41cf85689db1d583cJoe Perches		}
4051da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
406475be4d85a274d0961593db41cf85689db1d583cJoe Perches		/* dummy td; end of td list for ed */
4071da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		td = td_alloc (ohci, GFP_ATOMIC);
408475be4d85a274d0961593db41cf85689db1d583cJoe Perches		if (!td) {
4091da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			/* out of memory */
410475be4d85a274d0961593db41cf85689db1d583cJoe Perches			ed_free (ohci, ed);
411475be4d85a274d0961593db41cf85689db1d583cJoe Perches			ed = NULL;
412475be4d85a274d0961593db41cf85689db1d583cJoe Perches			goto done;
4131da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		}
4141da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		ed->dummy = td;
4151da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		ed->hwTailP = cpu_to_hc32 (ohci, td->td_dma);
4161da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		ed->hwHeadP = ed->hwTailP;	/* ED_C, ED_H zeroed */
4171da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		ed->state = ED_IDLE;
418475be4d85a274d0961593db41cf85689db1d583cJoe Perches
4191da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		is_out = !(ep->desc.bEndpointAddress & USB_DIR_IN);
4201da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
4211da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		/* FIXME usbcore changes dev->devnum before SET_ADDRESS
4221da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		 * suceeds ... otherwise we wouldn't need "pipe".
4231da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		 */
4241da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		info = usb_pipedevice (pipe);
4251da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		ed->type = usb_pipetype(pipe);
4261da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
427475be4d85a274d0961593db41cf85689db1d583cJoe Perches		info |= (ep->desc.bEndpointAddress & ~USB_DIR_IN) << 7;
4281da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		info |= le16_to_cpu(ep->desc.wMaxPacketSize) << 16;
429475be4d85a274d0961593db41cf85689db1d583cJoe Perches		if (udev->speed == USB_SPEED_LOW)
4301da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			info |= ED_LOWSPEED;
431475be4d85a274d0961593db41cf85689db1d583cJoe Perches		/* only control transfers store pids in tds */
4321da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		if (ed->type != PIPE_CONTROL) {
4331da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			info |= is_out ? ED_OUT : ED_IN;
4341da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			if (ed->type != PIPE_BULK) {
4351da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds				/* periodic transfers... */
4361da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds				if (ed->type == PIPE_ISOCHRONOUS)
4371da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds					info |= ED_ISO;
4381da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds				else if (interval > 32)	/* iso can be bigger */
4391da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds					interval = 32;
4401da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds				ed->interval = interval;
4411da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds				ed->load = usb_calc_bus_time (
4421da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds					udev->speed, !is_out,
4431da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds					ed->type == PIPE_ISOCHRONOUS,
4441da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds					le16_to_cpu(ep->desc.wMaxPacketSize))
4451da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds						/ 1000;
446475be4d85a274d0961593db41cf85689db1d583cJoe Perches			}
447475be4d85a274d0961593db41cf85689db1d583cJoe Perches		}
448475be4d85a274d0961593db41cf85689db1d583cJoe Perches		ed->hwINFO = cpu_to_hc32(ohci, info);
449475be4d85a274d0961593db41cf85689db1d583cJoe Perches
450475be4d85a274d0961593db41cf85689db1d583cJoe Perches		ep->hcpriv = ed;
451475be4d85a274d0961593db41cf85689db1d583cJoe Perches	}
452475be4d85a274d0961593db41cf85689db1d583cJoe Perches
453475be4d85a274d0961593db41cf85689db1d583cJoe Perchesdone:
4541da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	spin_unlock_irqrestore (&ohci->lock, flags);
455475be4d85a274d0961593db41cf85689db1d583cJoe Perches	return ed;
456475be4d85a274d0961593db41cf85689db1d583cJoe Perches}
4571da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
458475be4d85a274d0961593db41cf85689db1d583cJoe Perches/*-------------------------------------------------------------------------*/
459475be4d85a274d0961593db41cf85689db1d583cJoe Perches
460475be4d85a274d0961593db41cf85689db1d583cJoe Perches/* request unlinking of an endpoint from an operational HC.
4611da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * put the ep on the rm_list
462475be4d85a274d0961593db41cf85689db1d583cJoe Perches * real work is done at the next start frame (SF) hardware interrupt
463475be4d85a274d0961593db41cf85689db1d583cJoe Perches * caller guarantees HCD is running, so hardware access is safe,
4641da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * and that ed->state is ED_OPER
465475be4d85a274d0961593db41cf85689db1d583cJoe Perches */
466475be4d85a274d0961593db41cf85689db1d583cJoe Perchesstatic void start_ed_unlink (struct ohci_hcd *ohci, struct ed *ed)
467475be4d85a274d0961593db41cf85689db1d583cJoe Perches{
468475be4d85a274d0961593db41cf85689db1d583cJoe Perches	ed->hwINFO |= cpu_to_hc32 (ohci, ED_DEQUEUE);
469475be4d85a274d0961593db41cf85689db1d583cJoe Perches	ed_deschedule (ohci, ed);
470475be4d85a274d0961593db41cf85689db1d583cJoe Perches
471475be4d85a274d0961593db41cf85689db1d583cJoe Perches	/* rm_list is just singly linked, for simplicity */
472475be4d85a274d0961593db41cf85689db1d583cJoe Perches	ed->ed_next = ohci->ed_rm_list;
473475be4d85a274d0961593db41cf85689db1d583cJoe Perches	ed->ed_prev = NULL;
474475be4d85a274d0961593db41cf85689db1d583cJoe Perches	ohci->ed_rm_list = ed;
475475be4d85a274d0961593db41cf85689db1d583cJoe Perches
476475be4d85a274d0961593db41cf85689db1d583cJoe Perches	/* enable SOF interrupt */
477475be4d85a274d0961593db41cf85689db1d583cJoe Perches	ohci_writel (ohci, OHCI_INTR_SF, &ohci->regs->intrstatus);
478475be4d85a274d0961593db41cf85689db1d583cJoe Perches	ohci_writel (ohci, OHCI_INTR_SF, &ohci->regs->intrenable);
479475be4d85a274d0961593db41cf85689db1d583cJoe Perches	// flush those writes, and get latest HCCA contents
480475be4d85a274d0961593db41cf85689db1d583cJoe Perches	(void) ohci_readl (ohci, &ohci->regs->control);
481475be4d85a274d0961593db41cf85689db1d583cJoe Perches
4821da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/* SF interrupt might get delayed; record the frame counter value that
483475be4d85a274d0961593db41cf85689db1d583cJoe Perches	 * indicates when the HC isn't looking at it, so concurrent unlinks
484475be4d85a274d0961593db41cf85689db1d583cJoe Perches	 * behave.  frame_no wraps every 2^16 msec, and changes right before
4851da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 * SF is triggered.
486475be4d85a274d0961593db41cf85689db1d583cJoe Perches	 */
487475be4d85a274d0961593db41cf85689db1d583cJoe Perches	ed->tick = ohci_frame_no(ohci) + 1;
488475be4d85a274d0961593db41cf85689db1d583cJoe Perches
489475be4d85a274d0961593db41cf85689db1d583cJoe Perches}
490475be4d85a274d0961593db41cf85689db1d583cJoe Perches
4911da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/*-------------------------------------------------------------------------*
492475be4d85a274d0961593db41cf85689db1d583cJoe Perches * TD handling functions
493475be4d85a274d0961593db41cf85689db1d583cJoe Perches *-------------------------------------------------------------------------*/
4941da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
495475be4d85a274d0961593db41cf85689db1d583cJoe Perches/* enqueue next TD for this URB (OHCI spec 5.2.8.2) */
496475be4d85a274d0961593db41cf85689db1d583cJoe Perches
497475be4d85a274d0961593db41cf85689db1d583cJoe Perchesstatic void
498475be4d85a274d0961593db41cf85689db1d583cJoe Perchestd_fill (struct ohci_hcd *ohci, u32 info,
499475be4d85a274d0961593db41cf85689db1d583cJoe Perches	dma_addr_t data, int len,
500475be4d85a274d0961593db41cf85689db1d583cJoe Perches	struct urb *urb, int index)
501475be4d85a274d0961593db41cf85689db1d583cJoe Perches{
502475be4d85a274d0961593db41cf85689db1d583cJoe Perches	struct td		*td, *td_pt;
503475be4d85a274d0961593db41cf85689db1d583cJoe Perches	struct urb_priv		*urb_priv = urb->hcpriv;
504475be4d85a274d0961593db41cf85689db1d583cJoe Perches	int			is_iso = info & TD_ISO;
505475be4d85a274d0961593db41cf85689db1d583cJoe Perches	int			hash;
5061da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
507475be4d85a274d0961593db41cf85689db1d583cJoe Perches	// ASSERT (index < urb_priv->length);
508475be4d85a274d0961593db41cf85689db1d583cJoe Perches
509475be4d85a274d0961593db41cf85689db1d583cJoe Perches	/* aim for only one interrupt per urb.  mostly applies to control
510475be4d85a274d0961593db41cf85689db1d583cJoe Perches	 * and iso; other urbs rarely need more than one TD per urb.
511475be4d85a274d0961593db41cf85689db1d583cJoe Perches	 * this way, only final tds (or ones with an error) cause IRQs.
512475be4d85a274d0961593db41cf85689db1d583cJoe Perches	 * at least immediately; use DI=6 in case any control request is
513475be4d85a274d0961593db41cf85689db1d583cJoe Perches	 * tempted to die part way through.  (and to force the hc to flush
514475be4d85a274d0961593db41cf85689db1d583cJoe Perches	 * its donelist soonish, even on unlink paths.)
515475be4d85a274d0961593db41cf85689db1d583cJoe Perches	 *
516475be4d85a274d0961593db41cf85689db1d583cJoe Perches	 * NOTE: could delay interrupts even for the last TD, and get fewer
517475be4d85a274d0961593db41cf85689db1d583cJoe Perches	 * interrupts ... increasing per-urb latency by sharing interrupts.
518475be4d85a274d0961593db41cf85689db1d583cJoe Perches	 * Drivers that queue bulk urbs may request that behavior.
519475be4d85a274d0961593db41cf85689db1d583cJoe Perches	 */
520475be4d85a274d0961593db41cf85689db1d583cJoe Perches	if (index != (urb_priv->length - 1)
521475be4d85a274d0961593db41cf85689db1d583cJoe Perches			|| (urb->transfer_flags & URB_NO_INTERRUPT))
522475be4d85a274d0961593db41cf85689db1d583cJoe Perches		info |= TD_DI_SET (6);
523475be4d85a274d0961593db41cf85689db1d583cJoe Perches
524475be4d85a274d0961593db41cf85689db1d583cJoe Perches	/* use this td as the next dummy */
525475be4d85a274d0961593db41cf85689db1d583cJoe Perches	td_pt = urb_priv->td [index];
526475be4d85a274d0961593db41cf85689db1d583cJoe Perches
527475be4d85a274d0961593db41cf85689db1d583cJoe Perches	/* fill the old dummy TD */
528475be4d85a274d0961593db41cf85689db1d583cJoe Perches	td = urb_priv->td [index] = urb_priv->ed->dummy;
529475be4d85a274d0961593db41cf85689db1d583cJoe Perches	urb_priv->ed->dummy = td_pt;
530475be4d85a274d0961593db41cf85689db1d583cJoe Perches
531475be4d85a274d0961593db41cf85689db1d583cJoe Perches	td->ed = urb_priv->ed;
532475be4d85a274d0961593db41cf85689db1d583cJoe Perches	td->next_dl_td = NULL;
5331da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	td->index = index;
5341da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	td->urb = urb;
535475be4d85a274d0961593db41cf85689db1d583cJoe Perches	td->data_dma = data;
5361da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (!len)
537475be4d85a274d0961593db41cf85689db1d583cJoe Perches		data = 0;
538475be4d85a274d0961593db41cf85689db1d583cJoe Perches
539475be4d85a274d0961593db41cf85689db1d583cJoe Perches	td->hwINFO = cpu_to_hc32 (ohci, info);
540475be4d85a274d0961593db41cf85689db1d583cJoe Perches	if (is_iso) {
541475be4d85a274d0961593db41cf85689db1d583cJoe Perches		td->hwCBP = cpu_to_hc32 (ohci, data & 0xFFFFF000);
542475be4d85a274d0961593db41cf85689db1d583cJoe Perches		*ohci_hwPSWp(ohci, td, 0) = cpu_to_hc16 (ohci,
543475be4d85a274d0961593db41cf85689db1d583cJoe Perches						(data & 0x0FFF) | 0xE000);
544475be4d85a274d0961593db41cf85689db1d583cJoe Perches		td->ed->last_iso = info & 0xffff;
545475be4d85a274d0961593db41cf85689db1d583cJoe Perches	} else {
546475be4d85a274d0961593db41cf85689db1d583cJoe Perches		td->hwCBP = cpu_to_hc32 (ohci, data);
547475be4d85a274d0961593db41cf85689db1d583cJoe Perches	}
548475be4d85a274d0961593db41cf85689db1d583cJoe Perches	if (data)
549475be4d85a274d0961593db41cf85689db1d583cJoe Perches		td->hwBE = cpu_to_hc32 (ohci, data + len - 1);
550475be4d85a274d0961593db41cf85689db1d583cJoe Perches	else
551475be4d85a274d0961593db41cf85689db1d583cJoe Perches		td->hwBE = 0;
552475be4d85a274d0961593db41cf85689db1d583cJoe Perches	td->hwNextTD = cpu_to_hc32 (ohci, td_pt->td_dma);
553475be4d85a274d0961593db41cf85689db1d583cJoe Perches
554475be4d85a274d0961593db41cf85689db1d583cJoe Perches	/* append to queue */
555475be4d85a274d0961593db41cf85689db1d583cJoe Perches	list_add_tail (&td->td_list, &td->ed->td_list);
556475be4d85a274d0961593db41cf85689db1d583cJoe Perches
557475be4d85a274d0961593db41cf85689db1d583cJoe Perches	/* hash it for later reverse mapping */
558475be4d85a274d0961593db41cf85689db1d583cJoe Perches	hash = TD_HASH_FUNC (td->td_dma);
559475be4d85a274d0961593db41cf85689db1d583cJoe Perches	td->td_hash = ohci->td_hash [hash];
560475be4d85a274d0961593db41cf85689db1d583cJoe Perches	ohci->td_hash [hash] = td;
561475be4d85a274d0961593db41cf85689db1d583cJoe Perches
562475be4d85a274d0961593db41cf85689db1d583cJoe Perches	/* HC might read the TD (or cachelines) right away ... */
5631da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	wmb ();
5641da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	td->ed->hwTailP = td->hwNextTD;
5651da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
566672c3fd9069e5a138f9d4afc9aeb5aa34aacce32Adrian Bunk
5671da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/*-------------------------------------------------------------------------*/
5681da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
5691da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/* Prepare all TDs of a transfer, and queue them onto the ED.
5701da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * Caller guarantees HC is active.
5711da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * Usually the ED is already on the schedule, so TDs might be
572672c3fd9069e5a138f9d4afc9aeb5aa34aacce32Adrian Bunk * processed as soon as they're queued.
5733c7208f253571ee5f157b98f0e315b5172afe092Jesper Juhl */
5743c7208f253571ee5f157b98f0e315b5172afe092Jesper Juhlstatic void td_submit_urb (
5753c7208f253571ee5f157b98f0e315b5172afe092Jesper Juhl	struct ohci_hcd	*ohci,
5763c7208f253571ee5f157b98f0e315b5172afe092Jesper Juhl	struct urb	*urb
5773c7208f253571ee5f157b98f0e315b5172afe092Jesper Juhl) {
5783c7208f253571ee5f157b98f0e315b5172afe092Jesper Juhl	struct urb_priv	*urb_priv = urb->hcpriv;
5791da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	dma_addr_t	data;
5801da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	int		data_len = urb->transfer_buffer_length;
5811da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	int		cnt = 0;
5821da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	u32		info = 0;
5831da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	int		is_out = usb_pipeout (urb->pipe);
5841da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	int		periodic = 0;
5851da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
5861da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/* OHCI handles the bulk/interrupt data toggles itself.  We just
5871da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 * use the device toggle bits for resetting, and rely on the fact
5881da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 * that resetting toggle is meaningless if the endpoint is active.
5891da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 */
590475be4d85a274d0961593db41cf85689db1d583cJoe Perches	if (!usb_gettoggle (urb->dev, usb_pipeendpoint (urb->pipe), is_out)) {
5911da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		usb_settoggle (urb->dev, usb_pipeendpoint (urb->pipe),
5921da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			is_out, 1);
5931da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		urb_priv->ed->hwHeadP &= ~cpu_to_hc32 (ohci, ED_C);
5941da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
5951da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
5961da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	urb_priv->td_cnt = 0;
5971da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	list_add (&urb_priv->pending, &ohci->pending);
5981da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
5991da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (data_len)
6001da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		data = urb->transfer_dma;
6011da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	else
6021da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		data = 0;
6031da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
6041da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/* NOTE:  TD_CC is set so we can tell which TDs the HC processed by
6051da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 * using TD_CC_GET, as well as by seeing them on the done list.
6061da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 * (CC = NotAccessed ... 0x0F, or 0x0E in PSWs for ISO.)
6071da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 */
6081da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	switch (urb_priv->ed->type) {
6091da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
6101da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/* Bulk and interrupt are identical except for where in the schedule
6111da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 * their EDs live.
6127d12e780e003f93433d49ce78cfedf4b4c52adc5David Howells	 */
6131da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	case PIPE_INTERRUPT:
6141da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		/* ... and periodic urbs have extra accounting */
6151da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		periodic = ohci_to_hcd(ohci)->self.bandwidth_int_reqs++ == 0
6161da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			&& ohci_to_hcd(ohci)->self.bandwidth_isoc_reqs == 0;
617d348c2a3c8ad0948592f9a1138170002497903e2Sam Ravnborg		/* FALLTHROUGH */
6181da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	case PIPE_BULK:
6191da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		info = is_out
6201da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			? TD_T_TOGGLE | TD_CC | TD_DP_OUT
6211da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			: TD_T_TOGGLE | TD_CC | TD_DP_IN;
6221da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		/* TDs _could_ transfer up to 8K each */
623475be4d85a274d0961593db41cf85689db1d583cJoe Perches		while (data_len > 4096) {
624475be4d85a274d0961593db41cf85689db1d583cJoe Perches			td_fill (ohci, info, data, 4096, urb, cnt);
6251da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			data += 4096;
6261da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			data_len -= 4096;
6271da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			cnt++;
6281da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		}
6291da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		/* maybe avoid ED halt on final TD short read */
6301da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		if (!(urb->transfer_flags & URB_SHORT_NOT_OK))
6311da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			info |= TD_R;
6321da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		td_fill (ohci, info, data, data_len, urb, cnt);
6331da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		cnt++;
6341da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		if ((urb->transfer_flags & URB_ZERO_PACKET)
6351da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds				&& cnt < urb_priv->length) {
6361da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			td_fill (ohci, info, 0, 0, urb, cnt);
6371da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			cnt++;
6381da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		}
6391da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		/* maybe kickstart bulk list */
6401da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		if (urb_priv->ed->type == PIPE_BULK) {
6411da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			wmb ();
6421da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			ohci_writel (ohci, OHCI_BLF, &ohci->regs->cmdstatus);
6431da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		}
6441da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		break;
6451da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
6461da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/* control manages DATA0/DATA1 toggle per-request; SETUP resets it,
6471da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 * any DATA phase works normally, and the STATUS ack is special.
648d348c2a3c8ad0948592f9a1138170002497903e2Sam Ravnborg	 */
6491da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	case PIPE_CONTROL:
6501da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		info = TD_CC | TD_DP_SETUP | TD_T_DATA0;
6511da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		td_fill (ohci, info, urb->setup_dma, 8, urb, cnt++);
6521da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		if (data_len > 0) {
6531da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			info = TD_CC | TD_R | TD_T_DATA1;
6541da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			info |= is_out ? TD_DP_OUT : TD_DP_IN;
6551da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			/* NOTE:  mishandles transfers >8K, some >4K */
6561da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			td_fill (ohci, info, data, data_len, urb, cnt++);
6571da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		}
6581da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		info = (is_out || data_len == 0)
6591da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			? TD_CC | TD_DP_IN | TD_T_DATA1
6601da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			: TD_CC | TD_DP_OUT | TD_T_DATA1;
6611da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		td_fill (ohci, info, data, 0, urb, cnt++);
6621da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		/* maybe kickstart control list */
6631da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		wmb ();
6641da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		ohci_writel (ohci, OHCI_CLF, &ohci->regs->cmdstatus);
6651da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		break;
6661da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
6671da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/* ISO has no retransmit, so no toggle; and it uses special TDs.
6681da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 * Each TD could handle multiple consecutive frames (interval 1);
6691da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 * we could often reduce the number of TDs here.
6701da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 */
6711da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	case PIPE_ISOCHRONOUS:
6721da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		for (cnt = 0; cnt < urb->number_of_packets; cnt++) {
6731da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			int	frame = urb->start_frame;
6741da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
675c4028958b6ecad064b1a6303a6a5906d4fe48d73David Howells			// FIXME scheduling should handle frame counter
6761da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			// roll-around ... exotic case (and OHCI has
6771da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			// a 2^16 iso range, vs other HCs max of 2^10)
6781da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			frame += cnt * urb->interval;
6791da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			frame &= 0xffff;
680			td_fill (ohci, TD_CC | TD_ISO | frame,
681				data + urb->iso_frame_desc [cnt].offset,
682				urb->iso_frame_desc [cnt].length, urb, cnt);
683		}
684		periodic = ohci_to_hcd(ohci)->self.bandwidth_isoc_reqs++ == 0
685			&& ohci_to_hcd(ohci)->self.bandwidth_int_reqs == 0;
686		break;
687	}
688
689	/* start periodic dma if needed */
690	if (periodic) {
691		wmb ();
692		ohci->hc_control |= OHCI_CTRL_PLE|OHCI_CTRL_IE;
693		ohci_writel (ohci, ohci->hc_control, &ohci->regs->control);
694	}
695
696	// ASSERT (urb_priv->length == cnt);
697}
698
699/*-------------------------------------------------------------------------*
700 * Done List handling functions
701 *-------------------------------------------------------------------------*/
702
703/* calculate transfer length/status and update the urb */
704static int td_done(struct ohci_hcd *ohci, struct urb *urb, struct td *td)
705{
706	u32	tdINFO = hc32_to_cpup (ohci, &td->hwINFO);
707	int	cc = 0;
708	int	status = -EINPROGRESS;
709
710	list_del (&td->td_list);
711
712	/* ISO ... drivers see per-TD length/status */
713	if (tdINFO & TD_ISO) {
714		u16	tdPSW = ohci_hwPSW(ohci, td, 0);
715		int	dlen = 0;
716
717		/* NOTE:  assumes FC in tdINFO == 0, and that
718		 * only the first of 0..MAXPSW psws is used.
719		 */
720
721		cc = (tdPSW >> 12) & 0xF;
722		if (tdINFO & TD_CC)	/* hc didn't touch? */
723			return status;
724
725		if (usb_pipeout (urb->pipe))
726			dlen = urb->iso_frame_desc [td->index].length;
727		else {
728			/* short reads are always OK for ISO */
729			if (cc == TD_DATAUNDERRUN)
730				cc = TD_CC_NOERROR;
731			dlen = tdPSW & 0x3ff;
732		}
733		urb->actual_length += dlen;
734		urb->iso_frame_desc [td->index].actual_length = dlen;
735		urb->iso_frame_desc [td->index].status = cc_to_error [cc];
736
737		if (cc != TD_CC_NOERROR)
738			ohci_vdbg (ohci,
739				"urb %p iso td %p (%d) len %d cc %d\n",
740				urb, td, 1 + td->index, dlen, cc);
741
742	/* BULK, INT, CONTROL ... drivers see aggregate length/status,
743	 * except that "setup" bytes aren't counted and "short" transfers
744	 * might not be reported as errors.
745	 */
746	} else {
747		int	type = usb_pipetype (urb->pipe);
748		u32	tdBE = hc32_to_cpup (ohci, &td->hwBE);
749
750		cc = TD_CC_GET (tdINFO);
751
752		/* update packet status if needed (short is normally ok) */
753		if (cc == TD_DATAUNDERRUN
754				&& !(urb->transfer_flags & URB_SHORT_NOT_OK))
755			cc = TD_CC_NOERROR;
756		if (cc != TD_CC_NOERROR && cc < 0x0E)
757			status = cc_to_error[cc];
758
759		/* count all non-empty packets except control SETUP packet */
760		if ((type != PIPE_CONTROL || td->index != 0) && tdBE != 0) {
761			if (td->hwCBP == 0)
762				urb->actual_length += tdBE - td->data_dma + 1;
763			else
764				urb->actual_length +=
765					  hc32_to_cpup (ohci, &td->hwCBP)
766					- td->data_dma;
767		}
768
769		if (cc != TD_CC_NOERROR && cc < 0x0E)
770			ohci_vdbg (ohci,
771				"urb %p td %p (%d) cc %d, len=%d/%d\n",
772				urb, td, 1 + td->index, cc,
773				urb->actual_length,
774				urb->transfer_buffer_length);
775	}
776	return status;
777}
778
779/*-------------------------------------------------------------------------*/
780
781static void ed_halted(struct ohci_hcd *ohci, struct td *td, int cc)
782{
783	struct urb		*urb = td->urb;
784	urb_priv_t		*urb_priv = urb->hcpriv;
785	struct ed		*ed = td->ed;
786	struct list_head	*tmp = td->td_list.next;
787	__hc32			toggle = ed->hwHeadP & cpu_to_hc32 (ohci, ED_C);
788
789	/* clear ed halt; this is the td that caused it, but keep it inactive
790	 * until its urb->complete() has a chance to clean up.
791	 */
792	ed->hwINFO |= cpu_to_hc32 (ohci, ED_SKIP);
793	wmb ();
794	ed->hwHeadP &= ~cpu_to_hc32 (ohci, ED_H);
795
796	/* Get rid of all later tds from this urb.  We don't have
797	 * to be careful: no errors and nothing was transferred.
798	 * Also patch the ed so it looks as if those tds completed normally.
799	 */
800	while (tmp != &ed->td_list) {
801		struct td	*next;
802
803		next = list_entry (tmp, struct td, td_list);
804		tmp = next->td_list.next;
805
806		if (next->urb != urb)
807			break;
808
809		/* NOTE: if multi-td control DATA segments get supported,
810		 * this urb had one of them, this td wasn't the last td
811		 * in that segment (TD_R clear), this ed halted because
812		 * of a short read, _and_ URB_SHORT_NOT_OK is clear ...
813		 * then we need to leave the control STATUS packet queued
814		 * and clear ED_SKIP.
815		 */
816
817		list_del(&next->td_list);
818		urb_priv->td_cnt++;
819		ed->hwHeadP = next->hwNextTD | toggle;
820	}
821
822	/* help for troubleshooting:  report anything that
823	 * looks odd ... that doesn't include protocol stalls
824	 * (or maybe some other things)
825	 */
826	switch (cc) {
827	case TD_DATAUNDERRUN:
828		if ((urb->transfer_flags & URB_SHORT_NOT_OK) == 0)
829			break;
830		/* fallthrough */
831	case TD_CC_STALL:
832		if (usb_pipecontrol (urb->pipe))
833			break;
834		/* fallthrough */
835	default:
836		ohci_dbg (ohci,
837			"urb %p path %s ep%d%s %08x cc %d --> status %d\n",
838			urb, urb->dev->devpath,
839			usb_pipeendpoint (urb->pipe),
840			usb_pipein (urb->pipe) ? "in" : "out",
841			hc32_to_cpu (ohci, td->hwINFO),
842			cc, cc_to_error [cc]);
843	}
844}
845
846/* replies to the request have to be on a FIFO basis so
847 * we unreverse the hc-reversed done-list
848 */
849static struct td *dl_reverse_done_list (struct ohci_hcd *ohci)
850{
851	u32		td_dma;
852	struct td	*td_rev = NULL;
853	struct td	*td = NULL;
854
855	td_dma = hc32_to_cpup (ohci, &ohci->hcca->done_head);
856	ohci->hcca->done_head = 0;
857	wmb();
858
859	/* get TD from hc's singly linked list, and
860	 * prepend to ours.  ed->td_list changes later.
861	 */
862	while (td_dma) {
863		int		cc;
864
865		td = dma_to_td (ohci, td_dma);
866		if (!td) {
867			ohci_err (ohci, "bad entry %8x\n", td_dma);
868			break;
869		}
870
871		td->hwINFO |= cpu_to_hc32 (ohci, TD_DONE);
872		cc = TD_CC_GET (hc32_to_cpup (ohci, &td->hwINFO));
873
874		/* Non-iso endpoints can halt on error; un-halt,
875		 * and dequeue any other TDs from this urb.
876		 * No other TD could have caused the halt.
877		 */
878		if (cc != TD_CC_NOERROR
879				&& (td->ed->hwHeadP & cpu_to_hc32 (ohci, ED_H)))
880			ed_halted(ohci, td, cc);
881
882		td->next_dl_td = td_rev;
883		td_rev = td;
884		td_dma = hc32_to_cpup (ohci, &td->hwNextTD);
885	}
886	return td_rev;
887}
888
889/*-------------------------------------------------------------------------*/
890
891/* there are some urbs/eds to unlink; called in_irq(), with HCD locked */
892static void
893finish_unlinks (struct ohci_hcd *ohci, u16 tick)
894{
895	struct ed	*ed, **last;
896
897rescan_all:
898	for (last = &ohci->ed_rm_list, ed = *last; ed != NULL; ed = *last) {
899		struct list_head	*entry, *tmp;
900		int			completed, modified;
901		__hc32			*prev;
902
903		/* only take off EDs that the HC isn't using, accounting for
904		 * frame counter wraps and EDs with partially retired TDs
905		 */
906		if (likely (HC_IS_RUNNING(ohci_to_hcd(ohci)->state))) {
907			if (tick_before (tick, ed->tick)) {
908skip_ed:
909				last = &ed->ed_next;
910				continue;
911			}
912
913			if (!list_empty (&ed->td_list)) {
914				struct td	*td;
915				u32		head;
916
917				td = list_entry (ed->td_list.next, struct td,
918							td_list);
919				head = hc32_to_cpu (ohci, ed->hwHeadP) &
920								TD_MASK;
921
922				/* INTR_WDH may need to clean up first */
923				if (td->td_dma != head) {
924					if (ed == ohci->ed_to_check)
925						ohci->ed_to_check = NULL;
926					else
927						goto skip_ed;
928				}
929			}
930		}
931
932		/* reentrancy:  if we drop the schedule lock, someone might
933		 * have modified this list.  normally it's just prepending
934		 * entries (which we'd ignore), but paranoia won't hurt.
935		 */
936		*last = ed->ed_next;
937		ed->ed_next = NULL;
938		modified = 0;
939
940		/* unlink urbs as requested, but rescan the list after
941		 * we call a completion since it might have unlinked
942		 * another (earlier) urb
943		 *
944		 * When we get here, the HC doesn't see this ed.  But it
945		 * must not be rescheduled until all completed URBs have
946		 * been given back to the driver.
947		 */
948rescan_this:
949		completed = 0;
950		prev = &ed->hwHeadP;
951		list_for_each_safe (entry, tmp, &ed->td_list) {
952			struct td	*td;
953			struct urb	*urb;
954			urb_priv_t	*urb_priv;
955			__hc32		savebits;
956
957			td = list_entry (entry, struct td, td_list);
958			urb = td->urb;
959			urb_priv = td->urb->hcpriv;
960
961			if (!urb->unlinked) {
962				prev = &td->hwNextTD;
963				continue;
964			}
965
966			/* patch pointer hc uses */
967			savebits = *prev & ~cpu_to_hc32 (ohci, TD_MASK);
968			*prev = td->hwNextTD | savebits;
969
970			/* HC may have partly processed this TD */
971			td_done (ohci, urb, td);
972			urb_priv->td_cnt++;
973
974			/* if URB is done, clean up */
975			if (urb_priv->td_cnt == urb_priv->length) {
976				modified = completed = 1;
977				finish_urb(ohci, urb, 0);
978			}
979		}
980		if (completed && !list_empty (&ed->td_list))
981			goto rescan_this;
982
983		/* ED's now officially unlinked, hc doesn't see */
984		ed->state = ED_IDLE;
985		if (quirk_zfmicro(ohci) && ed->type == PIPE_INTERRUPT)
986			ohci->eds_scheduled--;
987		ed->hwHeadP &= ~cpu_to_hc32(ohci, ED_H);
988		ed->hwNextED = 0;
989		wmb ();
990		ed->hwINFO &= ~cpu_to_hc32 (ohci, ED_SKIP | ED_DEQUEUE);
991
992		/* but if there's work queued, reschedule */
993		if (!list_empty (&ed->td_list)) {
994			if (HC_IS_RUNNING(ohci_to_hcd(ohci)->state))
995				ed_schedule (ohci, ed);
996		}
997
998		if (modified)
999			goto rescan_all;
1000	}
1001
1002	/* maybe reenable control and bulk lists */
1003	if (HC_IS_RUNNING(ohci_to_hcd(ohci)->state)
1004			&& ohci_to_hcd(ohci)->state != HC_STATE_QUIESCING
1005			&& !ohci->ed_rm_list) {
1006		u32	command = 0, control = 0;
1007
1008		if (ohci->ed_controltail) {
1009			command |= OHCI_CLF;
1010			if (quirk_zfmicro(ohci))
1011				mdelay(1);
1012			if (!(ohci->hc_control & OHCI_CTRL_CLE)) {
1013				control |= OHCI_CTRL_CLE;
1014				ohci_writel (ohci, 0,
1015					&ohci->regs->ed_controlcurrent);
1016			}
1017		}
1018		if (ohci->ed_bulktail) {
1019			command |= OHCI_BLF;
1020			if (quirk_zfmicro(ohci))
1021				mdelay(1);
1022			if (!(ohci->hc_control & OHCI_CTRL_BLE)) {
1023				control |= OHCI_CTRL_BLE;
1024				ohci_writel (ohci, 0,
1025					&ohci->regs->ed_bulkcurrent);
1026			}
1027		}
1028
1029		/* CLE/BLE to enable, CLF/BLF to (maybe) kickstart */
1030		if (control) {
1031			ohci->hc_control |= control;
1032			if (quirk_zfmicro(ohci))
1033				mdelay(1);
1034			ohci_writel (ohci, ohci->hc_control,
1035					&ohci->regs->control);
1036		}
1037		if (command) {
1038			if (quirk_zfmicro(ohci))
1039				mdelay(1);
1040			ohci_writel (ohci, command, &ohci->regs->cmdstatus);
1041		}
1042	}
1043}
1044
1045
1046
1047/*-------------------------------------------------------------------------*/
1048
1049/*
1050 * Used to take back a TD from the host controller. This would normally be
1051 * called from within dl_done_list, however it may be called directly if the
1052 * HC no longer sees the TD and it has not appeared on the donelist (after
1053 * two frames).  This bug has been observed on ZF Micro systems.
1054 */
1055static void takeback_td(struct ohci_hcd *ohci, struct td *td)
1056{
1057	struct urb	*urb = td->urb;
1058	urb_priv_t	*urb_priv = urb->hcpriv;
1059	struct ed	*ed = td->ed;
1060	int		status;
1061
1062	/* update URB's length and status from TD */
1063	status = td_done(ohci, urb, td);
1064	urb_priv->td_cnt++;
1065
1066	/* If all this urb's TDs are done, call complete() */
1067	if (urb_priv->td_cnt == urb_priv->length)
1068		finish_urb(ohci, urb, status);
1069
1070	/* clean schedule:  unlink EDs that are no longer busy */
1071	if (list_empty(&ed->td_list)) {
1072		if (ed->state == ED_OPER)
1073			start_ed_unlink(ohci, ed);
1074
1075	/* ... reenabling halted EDs only after fault cleanup */
1076	} else if ((ed->hwINFO & cpu_to_hc32(ohci, ED_SKIP | ED_DEQUEUE))
1077			== cpu_to_hc32(ohci, ED_SKIP)) {
1078		td = list_entry(ed->td_list.next, struct td, td_list);
1079		if (!(td->hwINFO & cpu_to_hc32(ohci, TD_DONE))) {
1080			ed->hwINFO &= ~cpu_to_hc32(ohci, ED_SKIP);
1081			/* ... hc may need waking-up */
1082			switch (ed->type) {
1083			case PIPE_CONTROL:
1084				ohci_writel(ohci, OHCI_CLF,
1085						&ohci->regs->cmdstatus);
1086				break;
1087			case PIPE_BULK:
1088				ohci_writel(ohci, OHCI_BLF,
1089						&ohci->regs->cmdstatus);
1090				break;
1091			}
1092		}
1093	}
1094}
1095
1096/*
1097 * Process normal completions (error or success) and clean the schedules.
1098 *
1099 * This is the main path for handing urbs back to drivers.  The only other
1100 * normal path is finish_unlinks(), which unlinks URBs using ed_rm_list,
1101 * instead of scanning the (re-reversed) donelist as this does.  There's
1102 * an abnormal path too, handling a quirk in some Compaq silicon:  URBs
1103 * with TDs that appear to be orphaned are directly reclaimed.
1104 */
1105static void
1106dl_done_list (struct ohci_hcd *ohci)
1107{
1108	struct td	*td = dl_reverse_done_list (ohci);
1109
1110	while (td) {
1111		struct td	*td_next = td->next_dl_td;
1112		takeback_td(ohci, td);
1113		td = td_next;
1114	}
1115}
1116