ehci-q.c revision 1da177e4c3f41524e886b7f1b8a0c1fc7321cac2
11c1e45d17b663d4749af456ab7c2fc1f36405ef8Hans Verkuil/*
21c1e45d17b663d4749af456ab7c2fc1f36405ef8Hans Verkuil * Copyright (c) 2001-2002 by David Brownell
31c1e45d17b663d4749af456ab7c2fc1f36405ef8Hans Verkuil *
41c1e45d17b663d4749af456ab7c2fc1f36405ef8Hans Verkuil * This program is free software; you can redistribute it and/or modify it
56afdeaf865b729287e02aafc61d8d013b89996efAndy Walls * under the terms of the GNU General Public License as published by the
61c1e45d17b663d4749af456ab7c2fc1f36405ef8Hans Verkuil * Free Software Foundation; either version 2 of the License, or (at your
71c1e45d17b663d4749af456ab7c2fc1f36405ef8Hans Verkuil * option) any later version.
81c1e45d17b663d4749af456ab7c2fc1f36405ef8Hans Verkuil *
91c1e45d17b663d4749af456ab7c2fc1f36405ef8Hans Verkuil * This program is distributed in the hope that it will be useful, but
101c1e45d17b663d4749af456ab7c2fc1f36405ef8Hans Verkuil * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
111c1e45d17b663d4749af456ab7c2fc1f36405ef8Hans Verkuil * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
121c1e45d17b663d4749af456ab7c2fc1f36405ef8Hans Verkuil * for more details.
131c1e45d17b663d4749af456ab7c2fc1f36405ef8Hans Verkuil *
141c1e45d17b663d4749af456ab7c2fc1f36405ef8Hans Verkuil * You should have received a copy of the GNU General Public License
151c1e45d17b663d4749af456ab7c2fc1f36405ef8Hans Verkuil * along with this program; if not, write to the Free Software Foundation,
161c1e45d17b663d4749af456ab7c2fc1f36405ef8Hans Verkuil * Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
171c1e45d17b663d4749af456ab7c2fc1f36405ef8Hans Verkuil */
181c1e45d17b663d4749af456ab7c2fc1f36405ef8Hans Verkuil
191c1e45d17b663d4749af456ab7c2fc1f36405ef8Hans Verkuil/* this file is part of ehci-hcd.c */
201c1e45d17b663d4749af456ab7c2fc1f36405ef8Hans Verkuil
211c1e45d17b663d4749af456ab7c2fc1f36405ef8Hans Verkuil/*-------------------------------------------------------------------------*/
221c1e45d17b663d4749af456ab7c2fc1f36405ef8Hans Verkuil
231c1e45d17b663d4749af456ab7c2fc1f36405ef8Hans Verkuil/*
241c1e45d17b663d4749af456ab7c2fc1f36405ef8Hans Verkuil * EHCI hardware queue manipulation ... the core.  QH/QTD manipulation.
251c1e45d17b663d4749af456ab7c2fc1f36405ef8Hans Verkuil *
26b1526421eac9a912b2cda7e147f1da2aa31be278Andy Walls * Control, bulk, and interrupt traffic all use "qh" lists.  They list "qtd"
271c1e45d17b663d4749af456ab7c2fc1f36405ef8Hans Verkuil * entries describing USB transactions, max 16-20kB/entry (with 4kB-aligned
281c1e45d17b663d4749af456ab7c2fc1f36405ef8Hans Verkuil * buffers needed for the larger number).  We use one QH per endpoint, queue
291c1e45d17b663d4749af456ab7c2fc1f36405ef8Hans Verkuil * multiple urbs (all three types) per endpoint.  URBs may need several qtds.
30ee2d64f5ccc71b5c5191e92ea91a12b65f9ca060Andy Walls *
31ee2d64f5ccc71b5c5191e92ea91a12b65f9ca060Andy Walls * ISO traffic uses "ISO TD" (itd, and sitd) records, and (along with
329972de904216828c9f9f9d638df52206aa2bacd1Devin Heitmueller * interrupts) needs careful scheduling.  Performance improvements can be
33ee2d64f5ccc71b5c5191e92ea91a12b65f9ca060Andy Walls * an ongoing challenge.  That's in "ehci-sched.c".
34ee2d64f5ccc71b5c5191e92ea91a12b65f9ca060Andy Walls *
351c1e45d17b663d4749af456ab7c2fc1f36405ef8Hans Verkuil * USB 1.1 devices are handled (a) by "companion" OHCI or UHCI root hubs,
361c1e45d17b663d4749af456ab7c2fc1f36405ef8Hans Verkuil * or otherwise through transaction translators (TTs) in USB 2.0 hubs using
371c1e45d17b663d4749af456ab7c2fc1f36405ef8Hans Verkuil * (b) special fields in qh entries or (c) split iso entries.  TTs will
381c1e45d17b663d4749af456ab7c2fc1f36405ef8Hans Verkuil * buffer low/full speed data so the host collects it at high speed.
391c1e45d17b663d4749af456ab7c2fc1f36405ef8Hans Verkuil */
401c1e45d17b663d4749af456ab7c2fc1f36405ef8Hans Verkuil
411c1e45d17b663d4749af456ab7c2fc1f36405ef8Hans Verkuil/*-------------------------------------------------------------------------*/
421c1e45d17b663d4749af456ab7c2fc1f36405ef8Hans Verkuil
431c1e45d17b663d4749af456ab7c2fc1f36405ef8Hans Verkuil/* fill a qtd, returning how much of the buffer we were able to queue up */
441c1e45d17b663d4749af456ab7c2fc1f36405ef8Hans Verkuil
451c1e45d17b663d4749af456ab7c2fc1f36405ef8Hans Verkuilstatic int
461c1e45d17b663d4749af456ab7c2fc1f36405ef8Hans Verkuilqtd_fill (struct ehci_qtd *qtd, dma_addr_t buf, size_t len,
471c1e45d17b663d4749af456ab7c2fc1f36405ef8Hans Verkuil		int token, int maxpacket)
481c1e45d17b663d4749af456ab7c2fc1f36405ef8Hans Verkuil{
491c1e45d17b663d4749af456ab7c2fc1f36405ef8Hans Verkuil	int	i, count;
501c1e45d17b663d4749af456ab7c2fc1f36405ef8Hans Verkuil	u64	addr = buf;
511c1e45d17b663d4749af456ab7c2fc1f36405ef8Hans Verkuil
521c1e45d17b663d4749af456ab7c2fc1f36405ef8Hans Verkuil	/* one buffer entry per 4K ... first might be short or unaligned */
531c1e45d17b663d4749af456ab7c2fc1f36405ef8Hans Verkuil	qtd->hw_buf [0] = cpu_to_le32 ((u32)addr);
541c1e45d17b663d4749af456ab7c2fc1f36405ef8Hans Verkuil	qtd->hw_buf_hi [0] = cpu_to_le32 ((u32)(addr >> 32));
551c1e45d17b663d4749af456ab7c2fc1f36405ef8Hans Verkuil	count = 0x1000 - (buf & 0x0fff);	/* rest of that page */
561c1e45d17b663d4749af456ab7c2fc1f36405ef8Hans Verkuil	if (likely (len < count))		/* ... iff needed */
571c1e45d17b663d4749af456ab7c2fc1f36405ef8Hans Verkuil		count = len;
581c1e45d17b663d4749af456ab7c2fc1f36405ef8Hans Verkuil	else {
591c1e45d17b663d4749af456ab7c2fc1f36405ef8Hans Verkuil		buf +=  0x1000;
601c1e45d17b663d4749af456ab7c2fc1f36405ef8Hans Verkuil		buf &= ~0x0fff;
611c1e45d17b663d4749af456ab7c2fc1f36405ef8Hans Verkuil
621c1e45d17b663d4749af456ab7c2fc1f36405ef8Hans Verkuil		/* per-qtd limit: from 16K to 20K (best alignment) */
631c1e45d17b663d4749af456ab7c2fc1f36405ef8Hans Verkuil		for (i = 1; count < len && i < 5; i++) {
641c1e45d17b663d4749af456ab7c2fc1f36405ef8Hans Verkuil			addr = buf;
651c1e45d17b663d4749af456ab7c2fc1f36405ef8Hans Verkuil			qtd->hw_buf [i] = cpu_to_le32 ((u32)addr);
661c1e45d17b663d4749af456ab7c2fc1f36405ef8Hans Verkuil			qtd->hw_buf_hi [i] = cpu_to_le32 ((u32)(addr >> 32));
671c1e45d17b663d4749af456ab7c2fc1f36405ef8Hans Verkuil			buf += 0x1000;
681c1e45d17b663d4749af456ab7c2fc1f36405ef8Hans Verkuil			if ((count + 0x1000) < len)
691c1e45d17b663d4749af456ab7c2fc1f36405ef8Hans Verkuil				count += 0x1000;
701c1e45d17b663d4749af456ab7c2fc1f36405ef8Hans Verkuil			else
711c1e45d17b663d4749af456ab7c2fc1f36405ef8Hans Verkuil				count = len;
721c1e45d17b663d4749af456ab7c2fc1f36405ef8Hans Verkuil		}
731c1e45d17b663d4749af456ab7c2fc1f36405ef8Hans Verkuil
741c1e45d17b663d4749af456ab7c2fc1f36405ef8Hans Verkuil		/* short packets may only terminate transfers */
751c1e45d17b663d4749af456ab7c2fc1f36405ef8Hans Verkuil		if (count != len)
761c1e45d17b663d4749af456ab7c2fc1f36405ef8Hans Verkuil			count -= (count % maxpacket);
771c1e45d17b663d4749af456ab7c2fc1f36405ef8Hans Verkuil	}
781c1e45d17b663d4749af456ab7c2fc1f36405ef8Hans Verkuil	qtd->hw_token = cpu_to_le32 ((count << 16) | token);
791c1e45d17b663d4749af456ab7c2fc1f36405ef8Hans Verkuil	qtd->length = count;
801c1e45d17b663d4749af456ab7c2fc1f36405ef8Hans Verkuil
811c1e45d17b663d4749af456ab7c2fc1f36405ef8Hans Verkuil	return count;
821c1e45d17b663d4749af456ab7c2fc1f36405ef8Hans Verkuil}
831c1e45d17b663d4749af456ab7c2fc1f36405ef8Hans Verkuil
84b7101de3fff596b35e45cd9fb7007caa07e97c9aSteven Toth/*-------------------------------------------------------------------------*/
851c1e45d17b663d4749af456ab7c2fc1f36405ef8Hans Verkuil
861c1e45d17b663d4749af456ab7c2fc1f36405ef8Hans Verkuilstatic inline void
874e6b61047db2a77a250b6510bdb3c20c41aee591Andy Wallsqh_update (struct ehci_hcd *ehci, struct ehci_qh *qh, struct ehci_qtd *qtd)
88350145a4f7d0edffdccdace1221efc6d1d362a36Andy Walls{
89350145a4f7d0edffdccdace1221efc6d1d362a36Andy Walls	/* writes to an active overlay are unsafe */
90fd6b9c978dc3447b9b4677d8949ef3ea7f946abcAndy Walls	BUG_ON(qh->qh_state != QH_STATE_IDLE);
91fd6b9c978dc3447b9b4677d8949ef3ea7f946abcAndy Walls
921c1e45d17b663d4749af456ab7c2fc1f36405ef8Hans Verkuil	qh->hw_qtd_next = QTD_NEXT (qtd->qtd_dma);
931c1e45d17b663d4749af456ab7c2fc1f36405ef8Hans Verkuil	qh->hw_alt_next = EHCI_LIST_END;
941c1e45d17b663d4749af456ab7c2fc1f36405ef8Hans Verkuil
951c1e45d17b663d4749af456ab7c2fc1f36405ef8Hans Verkuil	/* Except for control endpoints, we make hardware maintain data
961c1e45d17b663d4749af456ab7c2fc1f36405ef8Hans Verkuil	 * toggle (like OHCI) ... here (re)initialize the toggle in the QH,
971c1e45d17b663d4749af456ab7c2fc1f36405ef8Hans Verkuil	 * and set the pseudo-toggle in udev. Only usb_clear_halt() will
981c1e45d17b663d4749af456ab7c2fc1f36405ef8Hans Verkuil	 * ever clear it.
991c1e45d17b663d4749af456ab7c2fc1f36405ef8Hans Verkuil	 */
1001c1e45d17b663d4749af456ab7c2fc1f36405ef8Hans Verkuil	if (!(qh->hw_info1 & cpu_to_le32(1 << 14))) {
1011c1e45d17b663d4749af456ab7c2fc1f36405ef8Hans Verkuil		unsigned	is_out, epnum;
1021c1e45d17b663d4749af456ab7c2fc1f36405ef8Hans Verkuil
1031c1e45d17b663d4749af456ab7c2fc1f36405ef8Hans Verkuil		is_out = !(qtd->hw_token & cpu_to_le32(1 << 8));
1041c1e45d17b663d4749af456ab7c2fc1f36405ef8Hans Verkuil		epnum = (le32_to_cpup(&qh->hw_info1) >> 8) & 0x0f;
10550299994181b835e5a6ee2882df2ee07e7fb4491Andy Walls		if (unlikely (!usb_gettoggle (qh->dev, epnum, is_out))) {
10650299994181b835e5a6ee2882df2ee07e7fb4491Andy Walls			qh->hw_token &= ~__constant_cpu_to_le32 (QTD_TOGGLE);
107ee2d64f5ccc71b5c5191e92ea91a12b65f9ca060Andy Walls			usb_settoggle (qh->dev, epnum, is_out, 1);
108ee2d64f5ccc71b5c5191e92ea91a12b65f9ca060Andy Walls		}
109ee2d64f5ccc71b5c5191e92ea91a12b65f9ca060Andy Walls	}
110ee2d64f5ccc71b5c5191e92ea91a12b65f9ca060Andy Walls
11150299994181b835e5a6ee2882df2ee07e7fb4491Andy Walls	/* HC must see latest qtd and qh data before we clear ACTIVE+HALT */
11250299994181b835e5a6ee2882df2ee07e7fb4491Andy Walls	wmb ();
11350299994181b835e5a6ee2882df2ee07e7fb4491Andy Walls	qh->hw_token &= __constant_cpu_to_le32 (QTD_TOGGLE | QTD_STS_PING);
11450299994181b835e5a6ee2882df2ee07e7fb4491Andy Walls}
11550299994181b835e5a6ee2882df2ee07e7fb4491Andy Walls
11650299994181b835e5a6ee2882df2ee07e7fb4491Andy Walls/* if it weren't for a common silicon quirk (writing the dummy into the qh
11750299994181b835e5a6ee2882df2ee07e7fb4491Andy Walls * overlay, so qh->hw_token wrongly becomes inactive/halted), only fault
11850299994181b835e5a6ee2882df2ee07e7fb4491Andy Walls * recovery (including urb dequeue) would need software changes to a QH...
11950299994181b835e5a6ee2882df2ee07e7fb4491Andy Walls */
12050299994181b835e5a6ee2882df2ee07e7fb4491Andy Wallsstatic void
12150299994181b835e5a6ee2882df2ee07e7fb4491Andy Wallsqh_refresh (struct ehci_hcd *ehci, struct ehci_qh *qh)
12250299994181b835e5a6ee2882df2ee07e7fb4491Andy Walls{
123ee2d64f5ccc71b5c5191e92ea91a12b65f9ca060Andy Walls	struct ehci_qtd *qtd;
124ee2d64f5ccc71b5c5191e92ea91a12b65f9ca060Andy Walls
125ee2d64f5ccc71b5c5191e92ea91a12b65f9ca060Andy Walls	if (list_empty (&qh->qtd_list))
126ee2d64f5ccc71b5c5191e92ea91a12b65f9ca060Andy Walls		qtd = qh->dummy;
12750299994181b835e5a6ee2882df2ee07e7fb4491Andy Walls	else {
12850299994181b835e5a6ee2882df2ee07e7fb4491Andy Walls		qtd = list_entry (qh->qtd_list.next,
129ee2d64f5ccc71b5c5191e92ea91a12b65f9ca060Andy Walls				struct ehci_qtd, qtd_list);
130ee2d64f5ccc71b5c5191e92ea91a12b65f9ca060Andy Walls		/* first qtd may already be partially processed */
131ee2d64f5ccc71b5c5191e92ea91a12b65f9ca060Andy Walls		if (cpu_to_le32 (qtd->qtd_dma) == qh->hw_current)
132ee2d64f5ccc71b5c5191e92ea91a12b65f9ca060Andy Walls			qtd = NULL;
133ee2d64f5ccc71b5c5191e92ea91a12b65f9ca060Andy Walls	}
134ee2d64f5ccc71b5c5191e92ea91a12b65f9ca060Andy Walls
135ee2d64f5ccc71b5c5191e92ea91a12b65f9ca060Andy Walls	if (qtd)
13652fcb3ecc6707f52dfe4297f96b7609d4ba517fbAndy Walls		qh_update (ehci, qh, qtd);
13752fcb3ecc6707f52dfe4297f96b7609d4ba517fbAndy Walls}
13852fcb3ecc6707f52dfe4297f96b7609d4ba517fbAndy Walls
13952fcb3ecc6707f52dfe4297f96b7609d4ba517fbAndy Walls/*-------------------------------------------------------------------------*/
140754f9969c323559a12bce1475f3c1e6574129856Andy Walls
14152fcb3ecc6707f52dfe4297f96b7609d4ba517fbAndy Wallsstatic void qtd_copy_status (
14252fcb3ecc6707f52dfe4297f96b7609d4ba517fbAndy Walls	struct ehci_hcd *ehci,
14352fcb3ecc6707f52dfe4297f96b7609d4ba517fbAndy Walls	struct urb *urb,
14452fcb3ecc6707f52dfe4297f96b7609d4ba517fbAndy Walls	size_t length,
14552fcb3ecc6707f52dfe4297f96b7609d4ba517fbAndy Walls	u32 token
14652fcb3ecc6707f52dfe4297f96b7609d4ba517fbAndy Walls)
14752fcb3ecc6707f52dfe4297f96b7609d4ba517fbAndy Walls{
14852fcb3ecc6707f52dfe4297f96b7609d4ba517fbAndy Walls	/* count IN/OUT bytes, not SETUP (even short packets) */
14952fcb3ecc6707f52dfe4297f96b7609d4ba517fbAndy Walls	if (likely (QTD_PID (token) != 2))
150754f9969c323559a12bce1475f3c1e6574129856Andy Walls		urb->actual_length += length - QTD_LENGTH (token);
15152fcb3ecc6707f52dfe4297f96b7609d4ba517fbAndy Walls
15252fcb3ecc6707f52dfe4297f96b7609d4ba517fbAndy Walls	/* don't modify error codes */
15352fcb3ecc6707f52dfe4297f96b7609d4ba517fbAndy Walls	if (unlikely (urb->status != -EINPROGRESS))
15452fcb3ecc6707f52dfe4297f96b7609d4ba517fbAndy Walls		return;
15552fcb3ecc6707f52dfe4297f96b7609d4ba517fbAndy Walls
15652fcb3ecc6707f52dfe4297f96b7609d4ba517fbAndy Walls	/* force cleanup after short read; not always an error */
15752fcb3ecc6707f52dfe4297f96b7609d4ba517fbAndy Walls	if (unlikely (IS_SHORT_READ (token)))
158754f9969c323559a12bce1475f3c1e6574129856Andy Walls		urb->status = -EREMOTEIO;
15952fcb3ecc6707f52dfe4297f96b7609d4ba517fbAndy Walls
16052fcb3ecc6707f52dfe4297f96b7609d4ba517fbAndy Walls	/* serious "can't proceed" faults reported by the hardware */
16152fcb3ecc6707f52dfe4297f96b7609d4ba517fbAndy Walls	if (token & QTD_STS_HALT) {
162b7101de3fff596b35e45cd9fb7007caa07e97c9aSteven Toth		if (token & QTD_STS_BABBLE) {
163b7101de3fff596b35e45cd9fb7007caa07e97c9aSteven Toth			/* FIXME "must" disable babbling device's port too */
164b7101de3fff596b35e45cd9fb7007caa07e97c9aSteven Toth			urb->status = -EOVERFLOW;
165b7101de3fff596b35e45cd9fb7007caa07e97c9aSteven Toth		} else if (token & QTD_STS_MMF) {
166b7101de3fff596b35e45cd9fb7007caa07e97c9aSteven Toth			/* fs/ls interrupt xfer missed the complete-split */
167bea3c54f52ba84ddd8bfc9228255ff501e9b5c21Simon Farnsworth			urb->status = -EPROTO;
168b7101de3fff596b35e45cd9fb7007caa07e97c9aSteven Toth		} else if (token & QTD_STS_DBE) {
169b7101de3fff596b35e45cd9fb7007caa07e97c9aSteven Toth			urb->status = (QTD_PID (token) == 1) /* IN ? */
170b7101de3fff596b35e45cd9fb7007caa07e97c9aSteven Toth				? -ENOSR  /* hc couldn't read data */
171b7101de3fff596b35e45cd9fb7007caa07e97c9aSteven Toth				: -ECOMM; /* hc couldn't write data */
172b7101de3fff596b35e45cd9fb7007caa07e97c9aSteven Toth		} else if (token & QTD_STS_XACT) {
173b7101de3fff596b35e45cd9fb7007caa07e97c9aSteven Toth			/* timeout, bad crc, wrong PID, etc; retried */
174b7101de3fff596b35e45cd9fb7007caa07e97c9aSteven Toth			if (QTD_CERR (token))
175b7101de3fff596b35e45cd9fb7007caa07e97c9aSteven Toth				urb->status = -EPIPE;
176b7101de3fff596b35e45cd9fb7007caa07e97c9aSteven Toth			else {
177b7101de3fff596b35e45cd9fb7007caa07e97c9aSteven Toth				ehci_dbg (ehci, "devpath %s ep%d%s 3strikes\n",
178b7101de3fff596b35e45cd9fb7007caa07e97c9aSteven Toth					urb->dev->devpath,
1791bf5842fe3b61d2dbbced96dbd27ad26fe93444aSimon Farnsworth					usb_pipeendpoint (urb->pipe),
180b7101de3fff596b35e45cd9fb7007caa07e97c9aSteven Toth					usb_pipein (urb->pipe) ? "in" : "out");
181b7101de3fff596b35e45cd9fb7007caa07e97c9aSteven Toth				urb->status = -EPROTO;
182b7101de3fff596b35e45cd9fb7007caa07e97c9aSteven Toth			}
183b7101de3fff596b35e45cd9fb7007caa07e97c9aSteven Toth		/* CERR nonzero + no errors + halt --> stall */
184b7101de3fff596b35e45cd9fb7007caa07e97c9aSteven Toth		} else if (QTD_CERR (token))
185b7101de3fff596b35e45cd9fb7007caa07e97c9aSteven Toth			urb->status = -EPIPE;
186b7101de3fff596b35e45cd9fb7007caa07e97c9aSteven Toth		else	/* unknown */
187b7101de3fff596b35e45cd9fb7007caa07e97c9aSteven Toth			urb->status = -EPROTO;
188b7101de3fff596b35e45cd9fb7007caa07e97c9aSteven Toth
189b7101de3fff596b35e45cd9fb7007caa07e97c9aSteven Toth		ehci_vdbg (ehci,
190b7101de3fff596b35e45cd9fb7007caa07e97c9aSteven Toth			"dev%d ep%d%s qtd token %08x --> status %d\n",
191b7101de3fff596b35e45cd9fb7007caa07e97c9aSteven Toth			usb_pipedevice (urb->pipe),
192b7101de3fff596b35e45cd9fb7007caa07e97c9aSteven Toth			usb_pipeendpoint (urb->pipe),
193b7101de3fff596b35e45cd9fb7007caa07e97c9aSteven Toth			usb_pipein (urb->pipe) ? "in" : "out",
194b7101de3fff596b35e45cd9fb7007caa07e97c9aSteven Toth			token, urb->status);
195b7101de3fff596b35e45cd9fb7007caa07e97c9aSteven Toth
196b7101de3fff596b35e45cd9fb7007caa07e97c9aSteven Toth		/* if async CSPLIT failed, try cleaning out the TT buffer */
197b7101de3fff596b35e45cd9fb7007caa07e97c9aSteven Toth		if (urb->status != -EPIPE
198b7101de3fff596b35e45cd9fb7007caa07e97c9aSteven Toth				&& urb->dev->tt && !usb_pipeint (urb->pipe)
19909fc9802c31a9358a4e34642aa5f569111752879Simon Farnsworth				&& ((token & QTD_STS_MMF) != 0
200b7101de3fff596b35e45cd9fb7007caa07e97c9aSteven Toth					|| QTD_CERR(token) == 0)
201b7101de3fff596b35e45cd9fb7007caa07e97c9aSteven Toth				&& (!ehci_is_TDI(ehci)
202b7101de3fff596b35e45cd9fb7007caa07e97c9aSteven Toth                	                || urb->dev->tt->hub !=
203b7101de3fff596b35e45cd9fb7007caa07e97c9aSteven Toth					   ehci_to_hcd(ehci)->self.root_hub)) {
204b7101de3fff596b35e45cd9fb7007caa07e97c9aSteven Toth#ifdef DEBUG
205bea3c54f52ba84ddd8bfc9228255ff501e9b5c21Simon Farnsworth			struct usb_device *tt = urb->dev->tt->hub;
206b7101de3fff596b35e45cd9fb7007caa07e97c9aSteven Toth			dev_dbg (&tt->dev,
207b7101de3fff596b35e45cd9fb7007caa07e97c9aSteven Toth				"clear tt buffer port %d, a%d ep%d t%08x\n",
208b7101de3fff596b35e45cd9fb7007caa07e97c9aSteven Toth				urb->dev->ttport, urb->dev->devnum,
209b7101de3fff596b35e45cd9fb7007caa07e97c9aSteven Toth				usb_pipeendpoint (urb->pipe), token);
210b7101de3fff596b35e45cd9fb7007caa07e97c9aSteven Toth#endif /* DEBUG */
2111bf5842fe3b61d2dbbced96dbd27ad26fe93444aSimon Farnsworth			usb_hub_tt_clear_buffer (urb->dev, urb->pipe);
212b7101de3fff596b35e45cd9fb7007caa07e97c9aSteven Toth		}
213b7101de3fff596b35e45cd9fb7007caa07e97c9aSteven Toth	}
214b7101de3fff596b35e45cd9fb7007caa07e97c9aSteven Toth}
215b7101de3fff596b35e45cd9fb7007caa07e97c9aSteven Toth
2169972de904216828c9f9f9d638df52206aa2bacd1Devin Heitmuellerstatic void
2179972de904216828c9f9f9d638df52206aa2bacd1Devin Heitmuellerehci_urb_done (struct ehci_hcd *ehci, struct urb *urb, struct pt_regs *regs)
2189972de904216828c9f9f9d638df52206aa2bacd1Devin Heitmueller__releases(ehci->lock)
2199972de904216828c9f9f9d638df52206aa2bacd1Devin Heitmueller__acquires(ehci->lock)
2209972de904216828c9f9f9d638df52206aa2bacd1Devin Heitmueller{
2219972de904216828c9f9f9d638df52206aa2bacd1Devin Heitmueller	if (likely (urb->hcpriv != NULL)) {
2229972de904216828c9f9f9d638df52206aa2bacd1Devin Heitmueller		struct ehci_qh	*qh = (struct ehci_qh *) urb->hcpriv;
2239972de904216828c9f9f9d638df52206aa2bacd1Devin Heitmueller
2249972de904216828c9f9f9d638df52206aa2bacd1Devin Heitmueller		/* S-mask in a QH means it's an interrupt urb */
2259972de904216828c9f9f9d638df52206aa2bacd1Devin Heitmueller		if ((qh->hw_info2 & __constant_cpu_to_le32 (0x00ff)) != 0) {
2269972de904216828c9f9f9d638df52206aa2bacd1Devin Heitmueller
2279972de904216828c9f9f9d638df52206aa2bacd1Devin Heitmueller			/* ... update hc-wide periodic stats (for usbfs) */
2289972de904216828c9f9f9d638df52206aa2bacd1Devin Heitmueller			ehci_to_hcd(ehci)->self.bandwidth_int_reqs--;
2299972de904216828c9f9f9d638df52206aa2bacd1Devin Heitmueller		}
2309972de904216828c9f9f9d638df52206aa2bacd1Devin Heitmueller		qh_put (qh);
2319972de904216828c9f9f9d638df52206aa2bacd1Devin Heitmueller	}
2329972de904216828c9f9f9d638df52206aa2bacd1Devin Heitmueller
2339972de904216828c9f9f9d638df52206aa2bacd1Devin Heitmueller	spin_lock (&urb->lock);
2349972de904216828c9f9f9d638df52206aa2bacd1Devin Heitmueller	urb->hcpriv = NULL;
2359972de904216828c9f9f9d638df52206aa2bacd1Devin Heitmueller	switch (urb->status) {
2369972de904216828c9f9f9d638df52206aa2bacd1Devin Heitmueller	case -EINPROGRESS:		/* success */
2379972de904216828c9f9f9d638df52206aa2bacd1Devin Heitmueller		urb->status = 0;
2389972de904216828c9f9f9d638df52206aa2bacd1Devin Heitmueller	default:			/* fault */
2399972de904216828c9f9f9d638df52206aa2bacd1Devin Heitmueller		COUNT (ehci->stats.complete);
2409972de904216828c9f9f9d638df52206aa2bacd1Devin Heitmueller		break;
2419972de904216828c9f9f9d638df52206aa2bacd1Devin Heitmueller	case -EREMOTEIO:		/* fault or normal */
2429972de904216828c9f9f9d638df52206aa2bacd1Devin Heitmueller		if (!(urb->transfer_flags & URB_SHORT_NOT_OK))
2439972de904216828c9f9f9d638df52206aa2bacd1Devin Heitmueller			urb->status = 0;
244deed75ed9f7576ada4bca02e6c851833a352a38dAndy Walls		COUNT (ehci->stats.complete);
245ee2d64f5ccc71b5c5191e92ea91a12b65f9ca060Andy Walls		break;
246bca11a5721917d6d5874571813673a2669ffec4bAndy Walls	case -ECONNRESET:		/* canceled */
247ee2d64f5ccc71b5c5191e92ea91a12b65f9ca060Andy Walls	case -ENOENT:
248ee2d64f5ccc71b5c5191e92ea91a12b65f9ca060Andy Walls		COUNT (ehci->stats.unlink);
249ee2d64f5ccc71b5c5191e92ea91a12b65f9ca060Andy Walls		break;
25052fcb3ecc6707f52dfe4297f96b7609d4ba517fbAndy Walls	}
251ee2d64f5ccc71b5c5191e92ea91a12b65f9ca060Andy Walls	spin_unlock (&urb->lock);
252ee2d64f5ccc71b5c5191e92ea91a12b65f9ca060Andy Walls
253ee2d64f5ccc71b5c5191e92ea91a12b65f9ca060Andy Walls#ifdef EHCI_URB_TRACE
254ee2d64f5ccc71b5c5191e92ea91a12b65f9ca060Andy Walls	ehci_dbg (ehci,
255ee2d64f5ccc71b5c5191e92ea91a12b65f9ca060Andy Walls		"%s %s urb %p ep%d%s status %d len %d/%d\n",
256ee2d64f5ccc71b5c5191e92ea91a12b65f9ca060Andy Walls		__FUNCTION__, urb->dev->devpath, urb,
257ee2d64f5ccc71b5c5191e92ea91a12b65f9ca060Andy Walls		usb_pipeendpoint (urb->pipe),
258ee2d64f5ccc71b5c5191e92ea91a12b65f9ca060Andy Walls		usb_pipein (urb->pipe) ? "in" : "out",
259bca11a5721917d6d5874571813673a2669ffec4bAndy Walls		urb->status,
260bca11a5721917d6d5874571813673a2669ffec4bAndy Walls		urb->actual_length, urb->transfer_buffer_length);
261bca11a5721917d6d5874571813673a2669ffec4bAndy Walls#endif
262ee2d64f5ccc71b5c5191e92ea91a12b65f9ca060Andy Walls
263ee2d64f5ccc71b5c5191e92ea91a12b65f9ca060Andy Walls	/* complete() can reenter this HCD */
264ee2d64f5ccc71b5c5191e92ea91a12b65f9ca060Andy Walls	spin_unlock (&ehci->lock);
265ee2d64f5ccc71b5c5191e92ea91a12b65f9ca060Andy Walls	usb_hcd_giveback_urb (ehci_to_hcd(ehci), urb, regs);
266ee2d64f5ccc71b5c5191e92ea91a12b65f9ca060Andy Walls	spin_lock (&ehci->lock);
267ee2d64f5ccc71b5c5191e92ea91a12b65f9ca060Andy Walls}
268bca11a5721917d6d5874571813673a2669ffec4bAndy Walls
269bca11a5721917d6d5874571813673a2669ffec4bAndy Wallsstatic void start_unlink_async (struct ehci_hcd *ehci, struct ehci_qh *qh);
270bca11a5721917d6d5874571813673a2669ffec4bAndy Wallsstatic void unlink_async (struct ehci_hcd *ehci, struct ehci_qh *qh);
27152fcb3ecc6707f52dfe4297f96b7609d4ba517fbAndy Walls
272bca11a5721917d6d5874571813673a2669ffec4bAndy Wallsstatic void intr_deschedule (struct ehci_hcd *ehci, struct ehci_qh *qh);
273bca11a5721917d6d5874571813673a2669ffec4bAndy Wallsstatic int qh_schedule (struct ehci_hcd *ehci, struct ehci_qh *qh);
274bca11a5721917d6d5874571813673a2669ffec4bAndy Walls
275bca11a5721917d6d5874571813673a2669ffec4bAndy Walls/*
276bca11a5721917d6d5874571813673a2669ffec4bAndy Walls * Process and free completed qtds for a qh, returning URBs to drivers.
277bca11a5721917d6d5874571813673a2669ffec4bAndy Walls * Chases up to qh->hw_current.  Returns number of completions called,
278bca11a5721917d6d5874571813673a2669ffec4bAndy Walls * indicating how much "real" work we did.
279bca11a5721917d6d5874571813673a2669ffec4bAndy Walls */
280bca11a5721917d6d5874571813673a2669ffec4bAndy Walls#define HALT_BIT __constant_cpu_to_le32(QTD_STS_HALT)
281bca11a5721917d6d5874571813673a2669ffec4bAndy Wallsstatic unsigned
28252fcb3ecc6707f52dfe4297f96b7609d4ba517fbAndy Wallsqh_completions (struct ehci_hcd *ehci, struct ehci_qh *qh, struct pt_regs *regs)
283bca11a5721917d6d5874571813673a2669ffec4bAndy Walls{
284bca11a5721917d6d5874571813673a2669ffec4bAndy Walls	struct ehci_qtd		*last = NULL, *end = qh->dummy;
285bca11a5721917d6d5874571813673a2669ffec4bAndy Walls	struct list_head	*entry, *tmp;
286bca11a5721917d6d5874571813673a2669ffec4bAndy Walls	int			stopped;
28752fcb3ecc6707f52dfe4297f96b7609d4ba517fbAndy Walls	unsigned		count = 0;
288abb096de82f6f920a06ca935f76925261e66b556Andy Walls	int			do_status = 0;
289bca11a5721917d6d5874571813673a2669ffec4bAndy Walls	u8			state;
290bca11a5721917d6d5874571813673a2669ffec4bAndy Walls
291fa655dda5ce6e5ac4a9b94fd451358edca2ddab8Andy Walls	if (unlikely (list_empty (&qh->qtd_list)))
292fa655dda5ce6e5ac4a9b94fd451358edca2ddab8Andy Walls		return count;
293bca11a5721917d6d5874571813673a2669ffec4bAndy Walls
29452fcb3ecc6707f52dfe4297f96b7609d4ba517fbAndy Walls	/* completions (or tasks on other cpus) must never clobber HALT
295bca11a5721917d6d5874571813673a2669ffec4bAndy Walls	 * till we've gone through and cleaned everything up, even when
296bca11a5721917d6d5874571813673a2669ffec4bAndy Walls	 * they add urbs to this qh's queue or mark them for unlinking.
297bca11a5721917d6d5874571813673a2669ffec4bAndy Walls	 *
29852fcb3ecc6707f52dfe4297f96b7609d4ba517fbAndy Walls	 * NOTE:  unlinking expects to be done in queue order.
299abb096de82f6f920a06ca935f76925261e66b556Andy Walls	 */
30052fcb3ecc6707f52dfe4297f96b7609d4ba517fbAndy Walls	state = qh->qh_state;
30152fcb3ecc6707f52dfe4297f96b7609d4ba517fbAndy Walls	qh->qh_state = QH_STATE_COMPLETING;
30252fcb3ecc6707f52dfe4297f96b7609d4ba517fbAndy Walls	stopped = (state == QH_STATE_IDLE);
303bca11a5721917d6d5874571813673a2669ffec4bAndy Walls
304ee2d64f5ccc71b5c5191e92ea91a12b65f9ca060Andy Walls	/* remove de-activated QTDs from front of queue.
305ee2d64f5ccc71b5c5191e92ea91a12b65f9ca060Andy Walls	 * after faults (including short reads), cleanup this urb
306ee2d64f5ccc71b5c5191e92ea91a12b65f9ca060Andy Walls	 * then let the queue advance.
30740c5520f55924ba87090d0d93222baad74202559Andy Walls	 * if queue is stopped, handles unlinks.
30852fcb3ecc6707f52dfe4297f96b7609d4ba517fbAndy Walls	 */
30940c5520f55924ba87090d0d93222baad74202559Andy Walls	list_for_each_safe (entry, tmp, &qh->qtd_list) {
3109972de904216828c9f9f9d638df52206aa2bacd1Devin Heitmueller		struct ehci_qtd	*qtd;
3119972de904216828c9f9f9d638df52206aa2bacd1Devin Heitmueller		struct urb	*urb;
3129972de904216828c9f9f9d638df52206aa2bacd1Devin Heitmueller		u32		token = 0;
3139972de904216828c9f9f9d638df52206aa2bacd1Devin Heitmueller
3149972de904216828c9f9f9d638df52206aa2bacd1Devin Heitmueller		qtd = list_entry (entry, struct ehci_qtd, qtd_list);
3159972de904216828c9f9f9d638df52206aa2bacd1Devin Heitmueller		urb = qtd->urb;
3169972de904216828c9f9f9d638df52206aa2bacd1Devin Heitmueller
3179972de904216828c9f9f9d638df52206aa2bacd1Devin Heitmueller		/* clean up any state from previous QTD ...*/
3189972de904216828c9f9f9d638df52206aa2bacd1Devin Heitmueller		if (last) {
3199972de904216828c9f9f9d638df52206aa2bacd1Devin Heitmueller			if (likely (last->urb != urb)) {
3209972de904216828c9f9f9d638df52206aa2bacd1Devin Heitmueller				ehci_urb_done (ehci, last->urb, regs);
321b7101de3fff596b35e45cd9fb7007caa07e97c9aSteven Toth				count++;
322b7101de3fff596b35e45cd9fb7007caa07e97c9aSteven Toth			}
323b7101de3fff596b35e45cd9fb7007caa07e97c9aSteven Toth			ehci_qtd_free (ehci, last);
3249972de904216828c9f9f9d638df52206aa2bacd1Devin Heitmueller			last = NULL;
32552fcb3ecc6707f52dfe4297f96b7609d4ba517fbAndy Walls		}
326ef99179710d6ec04d6783afdf8387523c7087920Andy Walls
327ef99179710d6ec04d6783afdf8387523c7087920Andy Walls		/* ignore urbs submitted during completions we reported */
328ef99179710d6ec04d6783afdf8387523c7087920Andy Walls		if (qtd == end)
329ee2d64f5ccc71b5c5191e92ea91a12b65f9ca060Andy Walls			break;
33052fcb3ecc6707f52dfe4297f96b7609d4ba517fbAndy Walls
33140c5520f55924ba87090d0d93222baad74202559Andy Walls		/* hardware copies qtd out of qh overlay */
33240c5520f55924ba87090d0d93222baad74202559Andy Walls		rmb ();
333ee2d64f5ccc71b5c5191e92ea91a12b65f9ca060Andy Walls		token = le32_to_cpu (qtd->hw_token);
334ee2d64f5ccc71b5c5191e92ea91a12b65f9ca060Andy Walls
335ee2d64f5ccc71b5c5191e92ea91a12b65f9ca060Andy Walls		/* always clean up qtds the hc de-activated */
336ee2d64f5ccc71b5c5191e92ea91a12b65f9ca060Andy Walls		if ((token & QTD_STS_ACTIVE) == 0) {
337ee2d64f5ccc71b5c5191e92ea91a12b65f9ca060Andy Walls
338deed75ed9f7576ada4bca02e6c851833a352a38dAndy Walls			if ((token & QTD_STS_HALT) != 0) {
339ee2d64f5ccc71b5c5191e92ea91a12b65f9ca060Andy Walls				stopped = 1;
340ee2d64f5ccc71b5c5191e92ea91a12b65f9ca060Andy Walls
341ee2d64f5ccc71b5c5191e92ea91a12b65f9ca060Andy Walls			/* magic dummy for some short reads; qh won't advance.
342ee2d64f5ccc71b5c5191e92ea91a12b65f9ca060Andy Walls			 * that silicon quirk can kick in with this dummy too.
343ee2d64f5ccc71b5c5191e92ea91a12b65f9ca060Andy Walls			 */
344ee2d64f5ccc71b5c5191e92ea91a12b65f9ca060Andy Walls			} else if (IS_SHORT_READ (token)
345ee2d64f5ccc71b5c5191e92ea91a12b65f9ca060Andy Walls					&& !(qtd->hw_alt_next & EHCI_LIST_END)) {
346ee2d64f5ccc71b5c5191e92ea91a12b65f9ca060Andy Walls				stopped = 1;
347ee2d64f5ccc71b5c5191e92ea91a12b65f9ca060Andy Walls				goto halt;
348ee2d64f5ccc71b5c5191e92ea91a12b65f9ca060Andy Walls			}
349deed75ed9f7576ada4bca02e6c851833a352a38dAndy Walls
350ee2d64f5ccc71b5c5191e92ea91a12b65f9ca060Andy Walls		/* stop scanning when we reach qtds the hc is using */
351ee2d64f5ccc71b5c5191e92ea91a12b65f9ca060Andy Walls		} else if (likely (!stopped
352ee2d64f5ccc71b5c5191e92ea91a12b65f9ca060Andy Walls				&& HC_IS_RUNNING (ehci_to_hcd(ehci)->state))) {
353ee2d64f5ccc71b5c5191e92ea91a12b65f9ca060Andy Walls			break;
354ee2d64f5ccc71b5c5191e92ea91a12b65f9ca060Andy Walls
355ee2d64f5ccc71b5c5191e92ea91a12b65f9ca060Andy Walls		} else {
356ee2d64f5ccc71b5c5191e92ea91a12b65f9ca060Andy Walls			stopped = 1;
357ee2d64f5ccc71b5c5191e92ea91a12b65f9ca060Andy Walls
358ee2d64f5ccc71b5c5191e92ea91a12b65f9ca060Andy Walls			if (unlikely (!HC_IS_RUNNING (ehci_to_hcd(ehci)->state)))
359ee2d64f5ccc71b5c5191e92ea91a12b65f9ca060Andy Walls				urb->status = -ESHUTDOWN;
360ee2d64f5ccc71b5c5191e92ea91a12b65f9ca060Andy Walls
361ee2d64f5ccc71b5c5191e92ea91a12b65f9ca060Andy Walls			/* ignore active urbs unless some previous qtd
362ee2d64f5ccc71b5c5191e92ea91a12b65f9ca060Andy Walls			 * for the urb faulted (including short read) or
363ee2d64f5ccc71b5c5191e92ea91a12b65f9ca060Andy Walls			 * its urb was canceled.  we may patch qh or qtds.
364ee2d64f5ccc71b5c5191e92ea91a12b65f9ca060Andy Walls			 */
365ee2d64f5ccc71b5c5191e92ea91a12b65f9ca060Andy Walls			if (likely (urb->status == -EINPROGRESS))
366ee2d64f5ccc71b5c5191e92ea91a12b65f9ca060Andy Walls				continue;
367ee2d64f5ccc71b5c5191e92ea91a12b65f9ca060Andy Walls
368ee2d64f5ccc71b5c5191e92ea91a12b65f9ca060Andy Walls			/* issue status after short control reads */
369ee2d64f5ccc71b5c5191e92ea91a12b65f9ca060Andy Walls			if (unlikely (do_status != 0)
370ee2d64f5ccc71b5c5191e92ea91a12b65f9ca060Andy Walls					&& QTD_PID (token) == 0 /* OUT */) {
371ee2d64f5ccc71b5c5191e92ea91a12b65f9ca060Andy Walls				do_status = 0;
372ee2d64f5ccc71b5c5191e92ea91a12b65f9ca060Andy Walls				continue;
373ee2d64f5ccc71b5c5191e92ea91a12b65f9ca060Andy Walls			}
374ee2d64f5ccc71b5c5191e92ea91a12b65f9ca060Andy Walls
375ee2d64f5ccc71b5c5191e92ea91a12b65f9ca060Andy Walls			/* token in overlay may be most current */
376ee2d64f5ccc71b5c5191e92ea91a12b65f9ca060Andy Walls			if (state == QH_STATE_IDLE
377ee2d64f5ccc71b5c5191e92ea91a12b65f9ca060Andy Walls					&& cpu_to_le32 (qtd->qtd_dma)
378deed75ed9f7576ada4bca02e6c851833a352a38dAndy Walls						== qh->hw_current)
379ee2d64f5ccc71b5c5191e92ea91a12b65f9ca060Andy Walls				token = le32_to_cpu (qh->hw_token);
380ee2d64f5ccc71b5c5191e92ea91a12b65f9ca060Andy Walls
381ee2d64f5ccc71b5c5191e92ea91a12b65f9ca060Andy Walls			/* force halt for unlinked or blocked qh, so we'll
382ee2d64f5ccc71b5c5191e92ea91a12b65f9ca060Andy Walls			 * patch the qh later and so that completions can't
383deed75ed9f7576ada4bca02e6c851833a352a38dAndy Walls			 * activate it while we "know" it's stopped.
384ee2d64f5ccc71b5c5191e92ea91a12b65f9ca060Andy Walls			 */
385deed75ed9f7576ada4bca02e6c851833a352a38dAndy Walls			if ((HALT_BIT & qh->hw_token) == 0) {
386deed75ed9f7576ada4bca02e6c851833a352a38dAndy Wallshalt:
387ee2d64f5ccc71b5c5191e92ea91a12b65f9ca060Andy Walls				qh->hw_token |= HALT_BIT;
388ee2d64f5ccc71b5c5191e92ea91a12b65f9ca060Andy Walls				wmb ();
389deed75ed9f7576ada4bca02e6c851833a352a38dAndy Walls			}
390ee2d64f5ccc71b5c5191e92ea91a12b65f9ca060Andy Walls		}
391ee2d64f5ccc71b5c5191e92ea91a12b65f9ca060Andy Walls
392ee2d64f5ccc71b5c5191e92ea91a12b65f9ca060Andy Walls		/* remove it from the queue */
393ee2d64f5ccc71b5c5191e92ea91a12b65f9ca060Andy Walls		spin_lock (&urb->lock);
394ee2d64f5ccc71b5c5191e92ea91a12b65f9ca060Andy Walls		qtd_copy_status (ehci, urb, qtd->length, token);
395ee2d64f5ccc71b5c5191e92ea91a12b65f9ca060Andy Walls		do_status = (urb->status == -EREMOTEIO)
396ee2d64f5ccc71b5c5191e92ea91a12b65f9ca060Andy Walls				&& usb_pipecontrol (urb->pipe);
397deed75ed9f7576ada4bca02e6c851833a352a38dAndy Walls		spin_unlock (&urb->lock);
3981c1e45d17b663d4749af456ab7c2fc1f36405ef8Hans Verkuil
399990c81c8afcd71eced2482ad59950ea755eddc7fAl Viro		if (stopped && qtd->qtd_list.prev != &qh->qtd_list) {
400ee2d64f5ccc71b5c5191e92ea91a12b65f9ca060Andy Walls			last = list_entry (qtd->qtd_list.prev,
4011c1e45d17b663d4749af456ab7c2fc1f36405ef8Hans Verkuil					struct ehci_qtd, qtd_list);
402ee2d64f5ccc71b5c5191e92ea91a12b65f9ca060Andy Walls			last->hw_next = qtd->hw_next;
4031c1e45d17b663d4749af456ab7c2fc1f36405ef8Hans Verkuil		}
4041c1e45d17b663d4749af456ab7c2fc1f36405ef8Hans Verkuil		list_del (&qtd->qtd_list);
4051c1e45d17b663d4749af456ab7c2fc1f36405ef8Hans Verkuil		last = qtd;
4061c1e45d17b663d4749af456ab7c2fc1f36405ef8Hans Verkuil	}
4071c1e45d17b663d4749af456ab7c2fc1f36405ef8Hans Verkuil
4081c1e45d17b663d4749af456ab7c2fc1f36405ef8Hans Verkuil	/* last urb's completion might still need calling */
4091c1e45d17b663d4749af456ab7c2fc1f36405ef8Hans Verkuil	if (likely (last != NULL)) {
4101c1e45d17b663d4749af456ab7c2fc1f36405ef8Hans Verkuil		ehci_urb_done (ehci, last->urb, regs);
4111c1e45d17b663d4749af456ab7c2fc1f36405ef8Hans Verkuil		count++;
41272c2d6d3ac91d1b9efb482ff4a8dd68e3d867965Andy Walls		ehci_qtd_free (ehci, last);
413ee2d64f5ccc71b5c5191e92ea91a12b65f9ca060Andy Walls	}
414ee2d64f5ccc71b5c5191e92ea91a12b65f9ca060Andy Walls
4151c1e45d17b663d4749af456ab7c2fc1f36405ef8Hans Verkuil	/* restore original state; caller must unlink or relink */
4161c1e45d17b663d4749af456ab7c2fc1f36405ef8Hans Verkuil	qh->qh_state = state;
417ee2d64f5ccc71b5c5191e92ea91a12b65f9ca060Andy Walls
418ee2d64f5ccc71b5c5191e92ea91a12b65f9ca060Andy Walls	/* be sure the hardware's done with the qh before refreshing
419ee2d64f5ccc71b5c5191e92ea91a12b65f9ca060Andy Walls	 * it after fault cleanup, or recovering from silicon wrongly
42072a4f8081af1c53a1673c173ce0fdd85c4b7d403Andy Walls	 * overlaying the dummy qtd (which reduces DMA chatter).
421bca11a5721917d6d5874571813673a2669ffec4bAndy Walls	 */
422bca11a5721917d6d5874571813673a2669ffec4bAndy Walls	if (stopped != 0 || qh->hw_qtd_next == EHCI_LIST_END) {
423bca11a5721917d6d5874571813673a2669ffec4bAndy Walls		switch (state) {
424bca11a5721917d6d5874571813673a2669ffec4bAndy Walls		case QH_STATE_IDLE:
42572a4f8081af1c53a1673c173ce0fdd85c4b7d403Andy Walls			qh_refresh(ehci, qh);
426ee2d64f5ccc71b5c5191e92ea91a12b65f9ca060Andy Walls			break;
42772a4f8081af1c53a1673c173ce0fdd85c4b7d403Andy Walls		case QH_STATE_LINKED:
428ee2d64f5ccc71b5c5191e92ea91a12b65f9ca060Andy Walls			/* should be rare for periodic transfers,
429f056d29eebd2c8800cf42528ba0470c77a928821Andy Walls			 * except maybe high bandwidth ...
430ee2d64f5ccc71b5c5191e92ea91a12b65f9ca060Andy Walls			 */
431ee2d64f5ccc71b5c5191e92ea91a12b65f9ca060Andy Walls			if (qh->period) {
432ee2d64f5ccc71b5c5191e92ea91a12b65f9ca060Andy Walls				intr_deschedule (ehci, qh);
433deed75ed9f7576ada4bca02e6c851833a352a38dAndy Walls				(void) qh_schedule (ehci, qh);
434ee2d64f5ccc71b5c5191e92ea91a12b65f9ca060Andy Walls			} else
435ee2d64f5ccc71b5c5191e92ea91a12b65f9ca060Andy Walls				unlink_async (ehci, qh);
436ee2d64f5ccc71b5c5191e92ea91a12b65f9ca060Andy Walls			break;
437ee2d64f5ccc71b5c5191e92ea91a12b65f9ca060Andy Walls		/* otherwise, unlink already started */
438ee2d64f5ccc71b5c5191e92ea91a12b65f9ca060Andy Walls		}
439ee2d64f5ccc71b5c5191e92ea91a12b65f9ca060Andy Walls	}
440ee2d64f5ccc71b5c5191e92ea91a12b65f9ca060Andy Walls
441ee2d64f5ccc71b5c5191e92ea91a12b65f9ca060Andy Walls	return count;
442ee2d64f5ccc71b5c5191e92ea91a12b65f9ca060Andy Walls}
443ee2d64f5ccc71b5c5191e92ea91a12b65f9ca060Andy Walls
444ee2d64f5ccc71b5c5191e92ea91a12b65f9ca060Andy Walls/*-------------------------------------------------------------------------*/
44572a4f8081af1c53a1673c173ce0fdd85c4b7d403Andy Walls
446ee2d64f5ccc71b5c5191e92ea91a12b65f9ca060Andy Walls// high bandwidth multiplier, as encoded in highspeed endpoint descriptors
447ee2d64f5ccc71b5c5191e92ea91a12b65f9ca060Andy Walls#define hb_mult(wMaxPacketSize) (1 + (((wMaxPacketSize) >> 11) & 0x03))
448ee2d64f5ccc71b5c5191e92ea91a12b65f9ca060Andy Walls// ... and packet size, for any kind of endpoint descriptor
449ee2d64f5ccc71b5c5191e92ea91a12b65f9ca060Andy Walls#define max_packet(wMaxPacketSize) ((wMaxPacketSize) & 0x07ff)
450ee2d64f5ccc71b5c5191e92ea91a12b65f9ca060Andy Walls
451ee2d64f5ccc71b5c5191e92ea91a12b65f9ca060Andy Walls/*
45272a4f8081af1c53a1673c173ce0fdd85c4b7d403Andy Walls * reverse of qh_urb_transaction:  free a list of TDs.
45372a4f8081af1c53a1673c173ce0fdd85c4b7d403Andy Walls * used for cleanup after errors, before HC sees an URB's TDs.
454ee2d64f5ccc71b5c5191e92ea91a12b65f9ca060Andy Walls */
455ee2d64f5ccc71b5c5191e92ea91a12b65f9ca060Andy Wallsstatic void qtd_list_free (
456ee2d64f5ccc71b5c5191e92ea91a12b65f9ca060Andy Walls	struct ehci_hcd		*ehci,
457ee2d64f5ccc71b5c5191e92ea91a12b65f9ca060Andy Walls	struct urb		*urb,
458ee2d64f5ccc71b5c5191e92ea91a12b65f9ca060Andy Walls	struct list_head	*qtd_list
459deed75ed9f7576ada4bca02e6c851833a352a38dAndy Walls) {
460ee2d64f5ccc71b5c5191e92ea91a12b65f9ca060Andy Walls	struct list_head	*entry, *temp;
461ee2d64f5ccc71b5c5191e92ea91a12b65f9ca060Andy Walls
462ee2d64f5ccc71b5c5191e92ea91a12b65f9ca060Andy Walls	list_for_each_safe (entry, temp, qtd_list) {
463ee2d64f5ccc71b5c5191e92ea91a12b65f9ca060Andy Walls		struct ehci_qtd	*qtd;
464ee2d64f5ccc71b5c5191e92ea91a12b65f9ca060Andy Walls
465ee2d64f5ccc71b5c5191e92ea91a12b65f9ca060Andy Walls		qtd = list_entry (entry, struct ehci_qtd, qtd_list);
466ee2d64f5ccc71b5c5191e92ea91a12b65f9ca060Andy Walls		list_del (&qtd->qtd_list);
467ee2d64f5ccc71b5c5191e92ea91a12b65f9ca060Andy Walls		ehci_qtd_free (ehci, qtd);
468ee2d64f5ccc71b5c5191e92ea91a12b65f9ca060Andy Walls	}
469ee2d64f5ccc71b5c5191e92ea91a12b65f9ca060Andy Walls}
470ee2d64f5ccc71b5c5191e92ea91a12b65f9ca060Andy Walls
471ee2d64f5ccc71b5c5191e92ea91a12b65f9ca060Andy Walls/*
472ee2d64f5ccc71b5c5191e92ea91a12b65f9ca060Andy Walls * create a list of filled qtds for this URB; won't link into qh.
47372a4f8081af1c53a1673c173ce0fdd85c4b7d403Andy Walls */
474ee2d64f5ccc71b5c5191e92ea91a12b65f9ca060Andy Wallsstatic struct list_head *
475ee2d64f5ccc71b5c5191e92ea91a12b65f9ca060Andy Wallsqh_urb_transaction (
476ee2d64f5ccc71b5c5191e92ea91a12b65f9ca060Andy Walls	struct ehci_hcd		*ehci,
477ee2d64f5ccc71b5c5191e92ea91a12b65f9ca060Andy Walls	struct urb		*urb,
478ee2d64f5ccc71b5c5191e92ea91a12b65f9ca060Andy Walls	struct list_head	*head,
479ee2d64f5ccc71b5c5191e92ea91a12b65f9ca060Andy Walls	int			flags
480deed75ed9f7576ada4bca02e6c851833a352a38dAndy Walls) {
481ee2d64f5ccc71b5c5191e92ea91a12b65f9ca060Andy Walls	struct ehci_qtd		*qtd, *qtd_prev;
482ee2d64f5ccc71b5c5191e92ea91a12b65f9ca060Andy Walls	dma_addr_t		buf;
483ee2d64f5ccc71b5c5191e92ea91a12b65f9ca060Andy Walls	int			len, maxpacket;
484ee2d64f5ccc71b5c5191e92ea91a12b65f9ca060Andy Walls	int			is_input;
485ee2d64f5ccc71b5c5191e92ea91a12b65f9ca060Andy Walls	u32			token;
486ee2d64f5ccc71b5c5191e92ea91a12b65f9ca060Andy Walls
487ee2d64f5ccc71b5c5191e92ea91a12b65f9ca060Andy Walls	/*
488ee2d64f5ccc71b5c5191e92ea91a12b65f9ca060Andy Walls	 * URBs map to sequences of QTDs:  one logical transaction
48972a4f8081af1c53a1673c173ce0fdd85c4b7d403Andy Walls	 */
490ee2d64f5ccc71b5c5191e92ea91a12b65f9ca060Andy Walls	qtd = ehci_qtd_alloc (ehci, flags);
491ee2d64f5ccc71b5c5191e92ea91a12b65f9ca060Andy Walls	if (unlikely (!qtd))
49272a4f8081af1c53a1673c173ce0fdd85c4b7d403Andy Walls		return NULL;
493ee2d64f5ccc71b5c5191e92ea91a12b65f9ca060Andy Walls	list_add_tail (&qtd->qtd_list, head);
494ee2d64f5ccc71b5c5191e92ea91a12b65f9ca060Andy Walls	qtd->urb = urb;
495ee2d64f5ccc71b5c5191e92ea91a12b65f9ca060Andy Walls
496ee2d64f5ccc71b5c5191e92ea91a12b65f9ca060Andy Walls	token = QTD_STS_ACTIVE;
497ee2d64f5ccc71b5c5191e92ea91a12b65f9ca060Andy Walls	token |= (EHCI_TUNE_CERR << 10);
498ee2d64f5ccc71b5c5191e92ea91a12b65f9ca060Andy Walls	/* for split transactions, SplitXState initialized to zero */
499ee2d64f5ccc71b5c5191e92ea91a12b65f9ca060Andy Walls
500ee2d64f5ccc71b5c5191e92ea91a12b65f9ca060Andy Walls	len = urb->transfer_buffer_length;
501ee2d64f5ccc71b5c5191e92ea91a12b65f9ca060Andy Walls	is_input = usb_pipein (urb->pipe);
502ee2d64f5ccc71b5c5191e92ea91a12b65f9ca060Andy Walls	if (usb_pipecontrol (urb->pipe)) {
503ee2d64f5ccc71b5c5191e92ea91a12b65f9ca060Andy Walls		/* SETUP pid */
504ee2d64f5ccc71b5c5191e92ea91a12b65f9ca060Andy Walls		qtd_fill (qtd, urb->setup_dma, sizeof (struct usb_ctrlrequest),
505ee2d64f5ccc71b5c5191e92ea91a12b65f9ca060Andy Walls			token | (2 /* "setup" */ << 8), 8);
506ee2d64f5ccc71b5c5191e92ea91a12b65f9ca060Andy Walls
507ee2d64f5ccc71b5c5191e92ea91a12b65f9ca060Andy Walls		/* ... and always at least one more pid */
508ee2d64f5ccc71b5c5191e92ea91a12b65f9ca060Andy Walls		token ^= QTD_TOGGLE;
5091c1e45d17b663d4749af456ab7c2fc1f36405ef8Hans Verkuil		qtd_prev = qtd;
5101c1e45d17b663d4749af456ab7c2fc1f36405ef8Hans Verkuil		qtd = ehci_qtd_alloc (ehci, flags);
511ee2d64f5ccc71b5c5191e92ea91a12b65f9ca060Andy Walls		if (unlikely (!qtd))
512deed75ed9f7576ada4bca02e6c851833a352a38dAndy Walls			goto cleanup;
513ee2d64f5ccc71b5c5191e92ea91a12b65f9ca060Andy Walls		qtd->urb = urb;
514ee2d64f5ccc71b5c5191e92ea91a12b65f9ca060Andy Walls		qtd_prev->hw_next = QTD_NEXT (qtd->qtd_dma);
515deed75ed9f7576ada4bca02e6c851833a352a38dAndy Walls		list_add_tail (&qtd->qtd_list, head);
516ee2d64f5ccc71b5c5191e92ea91a12b65f9ca060Andy Walls	}
517deed75ed9f7576ada4bca02e6c851833a352a38dAndy Walls
518ee2d64f5ccc71b5c5191e92ea91a12b65f9ca060Andy Walls	/*
519ee2d64f5ccc71b5c5191e92ea91a12b65f9ca060Andy Walls	 * data transfer stage:  buffer setup
520ee2d64f5ccc71b5c5191e92ea91a12b65f9ca060Andy Walls	 */
521ee2d64f5ccc71b5c5191e92ea91a12b65f9ca060Andy Walls	if (likely (len > 0))
522ee2d64f5ccc71b5c5191e92ea91a12b65f9ca060Andy Walls		buf = urb->transfer_dma;
523ee2d64f5ccc71b5c5191e92ea91a12b65f9ca060Andy Walls	else
524ee2d64f5ccc71b5c5191e92ea91a12b65f9ca060Andy Walls		buf = 0;
525ee2d64f5ccc71b5c5191e92ea91a12b65f9ca060Andy Walls
526deed75ed9f7576ada4bca02e6c851833a352a38dAndy Walls	/* for zero length DATA stages, STATUS is always IN */
527deed75ed9f7576ada4bca02e6c851833a352a38dAndy Walls	if (!buf || is_input)
528ee2d64f5ccc71b5c5191e92ea91a12b65f9ca060Andy Walls		token |= (1 /* "in" */ << 8);
529ee2d64f5ccc71b5c5191e92ea91a12b65f9ca060Andy Walls	/* else it's already initted to "out" pid (0 << 8) */
530ee2d64f5ccc71b5c5191e92ea91a12b65f9ca060Andy Walls
531ee2d64f5ccc71b5c5191e92ea91a12b65f9ca060Andy Walls	maxpacket = max_packet(usb_maxpacket(urb->dev, urb->pipe, !is_input));
532ee2d64f5ccc71b5c5191e92ea91a12b65f9ca060Andy Walls
533ee2d64f5ccc71b5c5191e92ea91a12b65f9ca060Andy Walls	/*
534ee2d64f5ccc71b5c5191e92ea91a12b65f9ca060Andy Walls	 * buffer gets wrapped in one or more qtds;
535ee2d64f5ccc71b5c5191e92ea91a12b65f9ca060Andy Walls	 * last one may be "short" (including zero len)
536ee2d64f5ccc71b5c5191e92ea91a12b65f9ca060Andy Walls	 * and may serve as a control status ack
537ee2d64f5ccc71b5c5191e92ea91a12b65f9ca060Andy Walls	 */
538ee2d64f5ccc71b5c5191e92ea91a12b65f9ca060Andy Walls	for (;;) {
539deed75ed9f7576ada4bca02e6c851833a352a38dAndy Walls		int this_qtd_len;
540ee2d64f5ccc71b5c5191e92ea91a12b65f9ca060Andy Walls
541ee2d64f5ccc71b5c5191e92ea91a12b65f9ca060Andy Walls		this_qtd_len = qtd_fill (qtd, buf, len, token, maxpacket);
542ee2d64f5ccc71b5c5191e92ea91a12b65f9ca060Andy Walls		len -= this_qtd_len;
543ee2d64f5ccc71b5c5191e92ea91a12b65f9ca060Andy Walls		buf += this_qtd_len;
544ee2d64f5ccc71b5c5191e92ea91a12b65f9ca060Andy Walls		if (is_input)
545ee2d64f5ccc71b5c5191e92ea91a12b65f9ca060Andy Walls			qtd->hw_alt_next = ehci->async->hw_alt_next;
546ee2d64f5ccc71b5c5191e92ea91a12b65f9ca060Andy Walls
547ee2d64f5ccc71b5c5191e92ea91a12b65f9ca060Andy Walls		/* qh makes control packets use qtd toggle; maybe switch it */
548ee2d64f5ccc71b5c5191e92ea91a12b65f9ca060Andy Walls		if ((maxpacket & (this_qtd_len + (maxpacket - 1))) == 0)
549ee2d64f5ccc71b5c5191e92ea91a12b65f9ca060Andy Walls			token ^= QTD_TOGGLE;
550ee2d64f5ccc71b5c5191e92ea91a12b65f9ca060Andy Walls
551ee2d64f5ccc71b5c5191e92ea91a12b65f9ca060Andy Walls		if (likely (len <= 0))
552ee2d64f5ccc71b5c5191e92ea91a12b65f9ca060Andy Walls			break;
553deed75ed9f7576ada4bca02e6c851833a352a38dAndy Walls
554ee2d64f5ccc71b5c5191e92ea91a12b65f9ca060Andy Walls		qtd_prev = qtd;
555ee2d64f5ccc71b5c5191e92ea91a12b65f9ca060Andy Walls		qtd = ehci_qtd_alloc (ehci, flags);
556ee2d64f5ccc71b5c5191e92ea91a12b65f9ca060Andy Walls		if (unlikely (!qtd))
557ee2d64f5ccc71b5c5191e92ea91a12b65f9ca060Andy Walls			goto cleanup;
558ee2d64f5ccc71b5c5191e92ea91a12b65f9ca060Andy Walls		qtd->urb = urb;
559ee2d64f5ccc71b5c5191e92ea91a12b65f9ca060Andy Walls		qtd_prev->hw_next = QTD_NEXT (qtd->qtd_dma);
56072a4f8081af1c53a1673c173ce0fdd85c4b7d403Andy Walls		list_add_tail (&qtd->qtd_list, head);
561ee2d64f5ccc71b5c5191e92ea91a12b65f9ca060Andy Walls	}
562ee2d64f5ccc71b5c5191e92ea91a12b65f9ca060Andy Walls
563d6c7e5f8faad080e75bace5c4f2265e3513e3510Andy Walls	/* unless the bulk/interrupt caller wants a chance to clean
564d6c7e5f8faad080e75bace5c4f2265e3513e3510Andy Walls	 * up after short reads, hc should advance qh past this urb
565d6c7e5f8faad080e75bace5c4f2265e3513e3510Andy Walls	 */
566d6c7e5f8faad080e75bace5c4f2265e3513e3510Andy Walls	if (likely ((urb->transfer_flags & URB_SHORT_NOT_OK) == 0
567d6c7e5f8faad080e75bace5c4f2265e3513e3510Andy Walls				|| usb_pipecontrol (urb->pipe)))
568d6c7e5f8faad080e75bace5c4f2265e3513e3510Andy Walls		qtd->hw_alt_next = EHCI_LIST_END;
569ee2d64f5ccc71b5c5191e92ea91a12b65f9ca060Andy Walls
570ee2d64f5ccc71b5c5191e92ea91a12b65f9ca060Andy Walls	/*
571bca11a5721917d6d5874571813673a2669ffec4bAndy Walls	 * control requests may need a terminating data "status" ack;
572bca11a5721917d6d5874571813673a2669ffec4bAndy Walls	 * bulk ones may need a terminating short packet (zero length).
573bca11a5721917d6d5874571813673a2669ffec4bAndy Walls	 */
574bca11a5721917d6d5874571813673a2669ffec4bAndy Walls	if (likely (buf != 0)) {
57550299994181b835e5a6ee2882df2ee07e7fb4491Andy Walls		int	one_more = 0;
57650299994181b835e5a6ee2882df2ee07e7fb4491Andy Walls
57772a4f8081af1c53a1673c173ce0fdd85c4b7d403Andy Walls		if (usb_pipecontrol (urb->pipe)) {
578ee2d64f5ccc71b5c5191e92ea91a12b65f9ca060Andy Walls			one_more = 1;
579ee2d64f5ccc71b5c5191e92ea91a12b65f9ca060Andy Walls			token ^= 0x0100;	/* "in" <--> "out"  */
580ee2d64f5ccc71b5c5191e92ea91a12b65f9ca060Andy Walls			token |= QTD_TOGGLE;	/* force DATA1 */
581ee2d64f5ccc71b5c5191e92ea91a12b65f9ca060Andy Walls		} else if (usb_pipebulk (urb->pipe)
582ee2d64f5ccc71b5c5191e92ea91a12b65f9ca060Andy Walls				&& (urb->transfer_flags & URB_ZERO_PACKET)
583ee2d64f5ccc71b5c5191e92ea91a12b65f9ca060Andy Walls				&& !(urb->transfer_buffer_length % maxpacket)) {
58472a4f8081af1c53a1673c173ce0fdd85c4b7d403Andy Walls			one_more = 1;
585ee2d64f5ccc71b5c5191e92ea91a12b65f9ca060Andy Walls		}
586deed75ed9f7576ada4bca02e6c851833a352a38dAndy Walls		if (one_more) {
587ee2d64f5ccc71b5c5191e92ea91a12b65f9ca060Andy Walls			qtd_prev = qtd;
588ee2d64f5ccc71b5c5191e92ea91a12b65f9ca060Andy Walls			qtd = ehci_qtd_alloc (ehci, flags);
589ee2d64f5ccc71b5c5191e92ea91a12b65f9ca060Andy Walls			if (unlikely (!qtd))
590ee2d64f5ccc71b5c5191e92ea91a12b65f9ca060Andy Walls				goto cleanup;
591ee2d64f5ccc71b5c5191e92ea91a12b65f9ca060Andy Walls			qtd->urb = urb;
592ee2d64f5ccc71b5c5191e92ea91a12b65f9ca060Andy Walls			qtd_prev->hw_next = QTD_NEXT (qtd->qtd_dma);
593ee2d64f5ccc71b5c5191e92ea91a12b65f9ca060Andy Walls			list_add_tail (&qtd->qtd_list, head);
594ee2d64f5ccc71b5c5191e92ea91a12b65f9ca060Andy Walls
5951c1e45d17b663d4749af456ab7c2fc1f36405ef8Hans Verkuil			/* never any data in such packets */
5961c1e45d17b663d4749af456ab7c2fc1f36405ef8Hans Verkuil			qtd_fill (qtd, 0, 0, token, 0);
5971c1e45d17b663d4749af456ab7c2fc1f36405ef8Hans Verkuil		}
598ac50441720332f22a9d85ac03151d6acb7bc55d6Andy Walls	}
599990c81c8afcd71eced2482ad59950ea755eddc7fAl Viro
600ac50441720332f22a9d85ac03151d6acb7bc55d6Andy Walls	/* by default, enable interrupt on urb completion */
6011c1e45d17b663d4749af456ab7c2fc1f36405ef8Hans Verkuil	if (likely (!(urb->transfer_flags & URB_NO_INTERRUPT)))
60272c2d6d3ac91d1b9efb482ff4a8dd68e3d867965Andy Walls		qtd->hw_token |= __constant_cpu_to_le32 (QTD_IOC);
6035f0a3cfcfd315d87de8f80af49b114daf7137823Andy Walls	return head;
6041c1e45d17b663d4749af456ab7c2fc1f36405ef8Hans Verkuil
60550299994181b835e5a6ee2882df2ee07e7fb4491Andy Wallscleanup:
6065f0a3cfcfd315d87de8f80af49b114daf7137823Andy Walls	qtd_list_free (ehci, urb, head);
6071c1e45d17b663d4749af456ab7c2fc1f36405ef8Hans Verkuil	return NULL;
6081c1e45d17b663d4749af456ab7c2fc1f36405ef8Hans Verkuil}
6091c1e45d17b663d4749af456ab7c2fc1f36405ef8Hans Verkuil
6101c1e45d17b663d4749af456ab7c2fc1f36405ef8Hans Verkuil/*-------------------------------------------------------------------------*/
6111c1e45d17b663d4749af456ab7c2fc1f36405ef8Hans Verkuil
6121c1e45d17b663d4749af456ab7c2fc1f36405ef8Hans Verkuil// Would be best to create all qh's from config descriptors,
61350299994181b835e5a6ee2882df2ee07e7fb4491Andy Walls// when each interface/altsetting is established.  Unlink
61450299994181b835e5a6ee2882df2ee07e7fb4491Andy Walls// any previous qh and cancel its urbs first; endpoints are
61550299994181b835e5a6ee2882df2ee07e7fb4491Andy Walls// implicitly reset then (data toggle too).
61650299994181b835e5a6ee2882df2ee07e7fb4491Andy Walls// That'd mean updating how usbcore talks to HCDs. (2.7?)
61750299994181b835e5a6ee2882df2ee07e7fb4491Andy Walls
61850299994181b835e5a6ee2882df2ee07e7fb4491Andy Walls
61950299994181b835e5a6ee2882df2ee07e7fb4491Andy Walls/*
62050299994181b835e5a6ee2882df2ee07e7fb4491Andy Walls * Each QH holds a qtd list; a QH is used for everything except iso.
62150299994181b835e5a6ee2882df2ee07e7fb4491Andy Walls *
62250299994181b835e5a6ee2882df2ee07e7fb4491Andy Walls * For interrupt urbs, the scheduler must set the microframe scheduling
62350299994181b835e5a6ee2882df2ee07e7fb4491Andy Walls * mask(s) each time the QH gets scheduled.  For highspeed, that's
62472c2d6d3ac91d1b9efb482ff4a8dd68e3d867965Andy Walls * just one microframe in the s-mask.  For split interrupt transactions
62572c2d6d3ac91d1b9efb482ff4a8dd68e3d867965Andy Walls * there are additional complications: c-mask, maybe FSTNs.
62672c2d6d3ac91d1b9efb482ff4a8dd68e3d867965Andy Walls */
62772c2d6d3ac91d1b9efb482ff4a8dd68e3d867965Andy Wallsstatic struct ehci_qh *
62872c2d6d3ac91d1b9efb482ff4a8dd68e3d867965Andy Wallsqh_make (
629ac50441720332f22a9d85ac03151d6acb7bc55d6Andy Walls	struct ehci_hcd		*ehci,
630ac50441720332f22a9d85ac03151d6acb7bc55d6Andy Walls	struct urb		*urb,
631ac50441720332f22a9d85ac03151d6acb7bc55d6Andy Walls	int			flags
63272c2d6d3ac91d1b9efb482ff4a8dd68e3d867965Andy Walls) {
63372c2d6d3ac91d1b9efb482ff4a8dd68e3d867965Andy Walls	struct ehci_qh		*qh = ehci_qh_alloc (ehci, flags);
63472c2d6d3ac91d1b9efb482ff4a8dd68e3d867965Andy Walls	u32			info1 = 0, info2 = 0;
63572c2d6d3ac91d1b9efb482ff4a8dd68e3d867965Andy Walls	int			is_input, type;
636ac50441720332f22a9d85ac03151d6acb7bc55d6Andy Walls	int			maxp = 0;
637ac50441720332f22a9d85ac03151d6acb7bc55d6Andy Walls
638ac50441720332f22a9d85ac03151d6acb7bc55d6Andy Walls	if (!qh)
63972c2d6d3ac91d1b9efb482ff4a8dd68e3d867965Andy Walls		return qh;
64072c2d6d3ac91d1b9efb482ff4a8dd68e3d867965Andy Walls
64172c2d6d3ac91d1b9efb482ff4a8dd68e3d867965Andy Walls	/*
64272c2d6d3ac91d1b9efb482ff4a8dd68e3d867965Andy Walls	 * init endpoint/device data for this QH
64372c2d6d3ac91d1b9efb482ff4a8dd68e3d867965Andy Walls	 */
64472c2d6d3ac91d1b9efb482ff4a8dd68e3d867965Andy Walls	info1 |= usb_pipeendpoint (urb->pipe) << 8;
64572c2d6d3ac91d1b9efb482ff4a8dd68e3d867965Andy Walls	info1 |= usb_pipedevice (urb->pipe) << 0;
646ac50441720332f22a9d85ac03151d6acb7bc55d6Andy Walls
647ac50441720332f22a9d85ac03151d6acb7bc55d6Andy Walls	is_input = usb_pipein (urb->pipe);
648ac50441720332f22a9d85ac03151d6acb7bc55d6Andy Walls	type = usb_pipetype (urb->pipe);
649ac50441720332f22a9d85ac03151d6acb7bc55d6Andy Walls	maxp = usb_maxpacket (urb->dev, urb->pipe, !is_input);
650ac50441720332f22a9d85ac03151d6acb7bc55d6Andy Walls
651ac50441720332f22a9d85ac03151d6acb7bc55d6Andy Walls	/* Compute interrupt scheduling parameters just once, and save.
652ac50441720332f22a9d85ac03151d6acb7bc55d6Andy Walls	 * - allowing for high bandwidth, how many nsec/uframe are used?
653ac50441720332f22a9d85ac03151d6acb7bc55d6Andy Walls	 * - split transactions need a second CSPLIT uframe; same question
654ac50441720332f22a9d85ac03151d6acb7bc55d6Andy Walls	 * - splits also need a schedule gap (for full/low speed I/O)
655ac50441720332f22a9d85ac03151d6acb7bc55d6Andy Walls	 * - qh has a polling interval
656ac50441720332f22a9d85ac03151d6acb7bc55d6Andy Walls	 *
657ac50441720332f22a9d85ac03151d6acb7bc55d6Andy Walls	 * For control/bulk requests, the HC or TT handles these.
6582bb49f1b9f6a4f50222bc8a6b1e9df87a432c52cAndy Walls	 */
659ac50441720332f22a9d85ac03151d6acb7bc55d6Andy Walls	if (type == PIPE_INTERRUPT) {
660ac50441720332f22a9d85ac03151d6acb7bc55d6Andy Walls		qh->usecs = usb_calc_bus_time (USB_SPEED_HIGH, is_input, 0,
661330c6ec8942765e81f237bd58020da1b161935ceAndy Walls				hb_mult (maxp) * max_packet (maxp));
662ac50441720332f22a9d85ac03151d6acb7bc55d6Andy Walls		qh->start = NO_FRAME;
663ac50441720332f22a9d85ac03151d6acb7bc55d6Andy Walls
664ac50441720332f22a9d85ac03151d6acb7bc55d6Andy Walls		if (urb->dev->speed == USB_SPEED_HIGH) {
665ac50441720332f22a9d85ac03151d6acb7bc55d6Andy Walls			qh->c_usecs = 0;
666ac50441720332f22a9d85ac03151d6acb7bc55d6Andy Walls			qh->gap_uf = 0;
667330c6ec8942765e81f237bd58020da1b161935ceAndy Walls
6682bb49f1b9f6a4f50222bc8a6b1e9df87a432c52cAndy Walls			qh->period = urb->interval >> 3;
6692bb49f1b9f6a4f50222bc8a6b1e9df87a432c52cAndy Walls			if (qh->period == 0 && urb->interval != 1) {
670ac50441720332f22a9d85ac03151d6acb7bc55d6Andy Walls				/* NOTE interval 2 or 4 uframes could work.
671ac50441720332f22a9d85ac03151d6acb7bc55d6Andy Walls				 * But interval 1 scheduling is simpler, and
672ac50441720332f22a9d85ac03151d6acb7bc55d6Andy Walls				 * includes high bandwidth.
6731c1e45d17b663d4749af456ab7c2fc1f36405ef8Hans Verkuil				 */
674b1526421eac9a912b2cda7e147f1da2aa31be278Andy Walls				dbg ("intr period %d uframes, NYET!",
6751c1e45d17b663d4749af456ab7c2fc1f36405ef8Hans Verkuil						urb->interval);
676b1526421eac9a912b2cda7e147f1da2aa31be278Andy Walls				goto done;
677b1526421eac9a912b2cda7e147f1da2aa31be278Andy Walls			}
678b1526421eac9a912b2cda7e147f1da2aa31be278Andy Walls		} else {
679ac50441720332f22a9d85ac03151d6acb7bc55d6Andy Walls			/* gap is f(FS/LS transfer times) */
6801c1e45d17b663d4749af456ab7c2fc1f36405ef8Hans Verkuil			qh->gap_uf = 1 + usb_calc_bus_time (urb->dev->speed,
681330c6ec8942765e81f237bd58020da1b161935ceAndy Walls					is_input, 0, maxp) / (125 * 1000);
682330c6ec8942765e81f237bd58020da1b161935ceAndy Walls
683330c6ec8942765e81f237bd58020da1b161935ceAndy Walls			/* FIXME this just approximates SPLIT/CSPLIT times */
6842bb49f1b9f6a4f50222bc8a6b1e9df87a432c52cAndy Walls			if (is_input) {		// SPLIT, gap, CSPLIT+DATA
685ac50441720332f22a9d85ac03151d6acb7bc55d6Andy Walls				qh->c_usecs = qh->usecs + HS_USECS (0);
686ac50441720332f22a9d85ac03151d6acb7bc55d6Andy Walls				qh->usecs = HS_USECS (1);
687ac50441720332f22a9d85ac03151d6acb7bc55d6Andy Walls			} else {		// SPLIT+DATA, gap, CSPLIT
6885f0a3cfcfd315d87de8f80af49b114daf7137823Andy Walls				qh->usecs += HS_USECS (1);
6895f0a3cfcfd315d87de8f80af49b114daf7137823Andy Walls				qh->c_usecs = HS_USECS (0);
6905f0a3cfcfd315d87de8f80af49b114daf7137823Andy Walls			}
691f056d29eebd2c8800cf42528ba0470c77a928821Andy Walls
6921c1e45d17b663d4749af456ab7c2fc1f36405ef8Hans Verkuil			qh->period = urb->interval;
6935f0a3cfcfd315d87de8f80af49b114daf7137823Andy Walls		}
6945f0a3cfcfd315d87de8f80af49b114daf7137823Andy Walls	}
6955f0a3cfcfd315d87de8f80af49b114daf7137823Andy Walls
6965f0a3cfcfd315d87de8f80af49b114daf7137823Andy Walls	/* support for tt scheduling, and access to toggles */
6975f0a3cfcfd315d87de8f80af49b114daf7137823Andy Walls	qh->dev = usb_get_dev (urb->dev);
6985f0a3cfcfd315d87de8f80af49b114daf7137823Andy Walls
6995f0a3cfcfd315d87de8f80af49b114daf7137823Andy Walls	/* using TT? */
7005f0a3cfcfd315d87de8f80af49b114daf7137823Andy Walls	switch (urb->dev->speed) {
7015f0a3cfcfd315d87de8f80af49b114daf7137823Andy Walls	case USB_SPEED_LOW:
7025f0a3cfcfd315d87de8f80af49b114daf7137823Andy Walls		info1 |= (1 << 12);	/* EPS "low" */
7035f0a3cfcfd315d87de8f80af49b114daf7137823Andy Walls		/* FALL THROUGH */
7042bb49f1b9f6a4f50222bc8a6b1e9df87a432c52cAndy Walls
7055f0a3cfcfd315d87de8f80af49b114daf7137823Andy Walls	case USB_SPEED_FULL:
70672c2d6d3ac91d1b9efb482ff4a8dd68e3d867965Andy Walls		/* EPS 0 means "full" */
7075f0a3cfcfd315d87de8f80af49b114daf7137823Andy Walls		if (type != PIPE_INTERRUPT)
7085f0a3cfcfd315d87de8f80af49b114daf7137823Andy Walls			info1 |= (EHCI_TUNE_RL_TT << 28);
7095f0a3cfcfd315d87de8f80af49b114daf7137823Andy Walls		if (type == PIPE_CONTROL) {
7105f0a3cfcfd315d87de8f80af49b114daf7137823Andy Walls			info1 |= (1 << 27);	/* for TT */
7115f0a3cfcfd315d87de8f80af49b114daf7137823Andy Walls			info1 |= 1 << 14;	/* toggle from qtd */
7125f0a3cfcfd315d87de8f80af49b114daf7137823Andy Walls		}
7135f0a3cfcfd315d87de8f80af49b114daf7137823Andy Walls		info1 |= maxp << 16;
7145f0a3cfcfd315d87de8f80af49b114daf7137823Andy Walls
7155f0a3cfcfd315d87de8f80af49b114daf7137823Andy Walls		info2 |= (EHCI_TUNE_MULT_TT << 30);
7165f0a3cfcfd315d87de8f80af49b114daf7137823Andy Walls		info2 |= urb->dev->ttport << 23;
7175f0a3cfcfd315d87de8f80af49b114daf7137823Andy Walls
7185f0a3cfcfd315d87de8f80af49b114daf7137823Andy Walls		/* set the address of the TT; for TDI's integrated
7195f0a3cfcfd315d87de8f80af49b114daf7137823Andy Walls		 * root hub tt, leave it zeroed.
7205f0a3cfcfd315d87de8f80af49b114daf7137823Andy Walls		 */
7211c1e45d17b663d4749af456ab7c2fc1f36405ef8Hans Verkuil		if (!ehci_is_TDI(ehci)
722330c6ec8942765e81f237bd58020da1b161935ceAndy Walls				|| urb->dev->tt->hub !=
723330c6ec8942765e81f237bd58020da1b161935ceAndy Walls					ehci_to_hcd(ehci)->self.root_hub)
7245f0a3cfcfd315d87de8f80af49b114daf7137823Andy Walls			info2 |= urb->dev->tt->hub->devnum << 16;
7255f0a3cfcfd315d87de8f80af49b114daf7137823Andy Walls
7265f0a3cfcfd315d87de8f80af49b114daf7137823Andy Walls		/* NOTE:  if (PIPE_INTERRUPT) { scheduler sets c-mask } */
7275f0a3cfcfd315d87de8f80af49b114daf7137823Andy Walls
7285f0a3cfcfd315d87de8f80af49b114daf7137823Andy Walls		break;
729330c6ec8942765e81f237bd58020da1b161935ceAndy Walls
7305f0a3cfcfd315d87de8f80af49b114daf7137823Andy Walls	case USB_SPEED_HIGH:		/* no TT involved */
73172c2d6d3ac91d1b9efb482ff4a8dd68e3d867965Andy Walls		info1 |= (2 << 12);	/* EPS "high" */
732ac50441720332f22a9d85ac03151d6acb7bc55d6Andy Walls		if (type == PIPE_CONTROL) {
7331c1e45d17b663d4749af456ab7c2fc1f36405ef8Hans Verkuil			info1 |= (EHCI_TUNE_RL_HS << 28);
734b1526421eac9a912b2cda7e147f1da2aa31be278Andy Walls			info1 |= 64 << 16;	/* usb2 fixed maxpacket */
735b1526421eac9a912b2cda7e147f1da2aa31be278Andy Walls			info1 |= 1 << 14;	/* toggle from qtd */
73672c2d6d3ac91d1b9efb482ff4a8dd68e3d867965Andy Walls			info2 |= (EHCI_TUNE_MULT_HS << 30);
737ac50441720332f22a9d85ac03151d6acb7bc55d6Andy Walls		} else if (type == PIPE_BULK) {
738ac50441720332f22a9d85ac03151d6acb7bc55d6Andy Walls			info1 |= (EHCI_TUNE_RL_HS << 28);
739ac50441720332f22a9d85ac03151d6acb7bc55d6Andy Walls			info1 |= 512 << 16;	/* usb2 fixed maxpacket */
74052fcb3ecc6707f52dfe4297f96b7609d4ba517fbAndy Walls			info2 |= (EHCI_TUNE_MULT_HS << 30);
741ac50441720332f22a9d85ac03151d6acb7bc55d6Andy Walls		} else {		/* PIPE_INTERRUPT */
742ac50441720332f22a9d85ac03151d6acb7bc55d6Andy Walls			info1 |= max_packet (maxp) << 16;
74372c2d6d3ac91d1b9efb482ff4a8dd68e3d867965Andy Walls			info2 |= hb_mult (maxp) << 30;
7441c1e45d17b663d4749af456ab7c2fc1f36405ef8Hans Verkuil		}
745ac50441720332f22a9d85ac03151d6acb7bc55d6Andy Walls		break;
7461c1e45d17b663d4749af456ab7c2fc1f36405ef8Hans Verkuil	default:
7471c1e45d17b663d4749af456ab7c2fc1f36405ef8Hans Verkuil 		dbg ("bogus dev %p speed %d", urb->dev, urb->dev->speed);
7481c1e45d17b663d4749af456ab7c2fc1f36405ef8Hans Verkuildone:
7491c1e45d17b663d4749af456ab7c2fc1f36405ef8Hans Verkuil		qh_put (qh);
7501c1e45d17b663d4749af456ab7c2fc1f36405ef8Hans Verkuil		return NULL;
7511c1e45d17b663d4749af456ab7c2fc1f36405ef8Hans Verkuil	}
7521c1e45d17b663d4749af456ab7c2fc1f36405ef8Hans Verkuil
7531c1e45d17b663d4749af456ab7c2fc1f36405ef8Hans Verkuil	/* NOTE:  if (PIPE_INTERRUPT) { scheduler sets s-mask } */
754ac50441720332f22a9d85ac03151d6acb7bc55d6Andy Walls
7551c1e45d17b663d4749af456ab7c2fc1f36405ef8Hans Verkuil	/* init as live, toggle clear, advance to dummy */
7561c1e45d17b663d4749af456ab7c2fc1f36405ef8Hans Verkuil	qh->qh_state = QH_STATE_IDLE;
7571c1e45d17b663d4749af456ab7c2fc1f36405ef8Hans Verkuil	qh->hw_info1 = cpu_to_le32 (info1);
7581c1e45d17b663d4749af456ab7c2fc1f36405ef8Hans Verkuil	qh->hw_info2 = cpu_to_le32 (info2);
7591c1e45d17b663d4749af456ab7c2fc1f36405ef8Hans Verkuil	usb_settoggle (urb->dev, usb_pipeendpoint (urb->pipe), !is_input, 1);
7601c1e45d17b663d4749af456ab7c2fc1f36405ef8Hans Verkuil	qh_refresh (ehci, qh);
7611c1e45d17b663d4749af456ab7c2fc1f36405ef8Hans Verkuil	return qh;
7621c1e45d17b663d4749af456ab7c2fc1f36405ef8Hans Verkuil}
7631c1e45d17b663d4749af456ab7c2fc1f36405ef8Hans Verkuil
7641c1e45d17b663d4749af456ab7c2fc1f36405ef8Hans Verkuil/*-------------------------------------------------------------------------*/
7651c1e45d17b663d4749af456ab7c2fc1f36405ef8Hans Verkuil
7661c1e45d17b663d4749af456ab7c2fc1f36405ef8Hans Verkuil/* move qh (and its qtds) onto async queue; maybe enable queue.  */
7671c1e45d17b663d4749af456ab7c2fc1f36405ef8Hans Verkuil
7681c1e45d17b663d4749af456ab7c2fc1f36405ef8Hans Verkuilstatic void qh_link_async (struct ehci_hcd *ehci, struct ehci_qh *qh)
7691c1e45d17b663d4749af456ab7c2fc1f36405ef8Hans Verkuil{
7701c1e45d17b663d4749af456ab7c2fc1f36405ef8Hans Verkuil	__le32		dma = QH_NEXT (qh->qh_dma);
7711c1e45d17b663d4749af456ab7c2fc1f36405ef8Hans Verkuil	struct ehci_qh	*head;
7721c1e45d17b663d4749af456ab7c2fc1f36405ef8Hans Verkuil
7731c1e45d17b663d4749af456ab7c2fc1f36405ef8Hans Verkuil	/* (re)start the async schedule? */
7741c1e45d17b663d4749af456ab7c2fc1f36405ef8Hans Verkuil	head = ehci->async;
7751c1e45d17b663d4749af456ab7c2fc1f36405ef8Hans Verkuil	timer_action_done (ehci, TIMER_ASYNC_OFF);
7761c1e45d17b663d4749af456ab7c2fc1f36405ef8Hans Verkuil	if (!head->qh_next.qh) {
777a75b9be1c2fb52dee765d35f29031dd788d522ebHans Verkuil		u32	cmd = readl (&ehci->regs->command);
778a75b9be1c2fb52dee765d35f29031dd788d522ebHans Verkuil
7791c1e45d17b663d4749af456ab7c2fc1f36405ef8Hans Verkuil		if (!(cmd & CMD_ASE)) {
7801c1e45d17b663d4749af456ab7c2fc1f36405ef8Hans Verkuil			/* in case a clear of CMD_ASE didn't take yet */
7811c1e45d17b663d4749af456ab7c2fc1f36405ef8Hans Verkuil			(void) handshake (&ehci->regs->status, STS_ASS, 0, 150);
7821c1e45d17b663d4749af456ab7c2fc1f36405ef8Hans Verkuil			cmd |= CMD_ASE | CMD_RUN;
7831c1e45d17b663d4749af456ab7c2fc1f36405ef8Hans Verkuil			writel (cmd, &ehci->regs->command);
7841c1e45d17b663d4749af456ab7c2fc1f36405ef8Hans Verkuil			ehci_to_hcd(ehci)->state = HC_STATE_RUNNING;
7851c1e45d17b663d4749af456ab7c2fc1f36405ef8Hans Verkuil			/* posted write need not be known to HC yet ... */
7861c1e45d17b663d4749af456ab7c2fc1f36405ef8Hans Verkuil		}
7871c1e45d17b663d4749af456ab7c2fc1f36405ef8Hans Verkuil	}
7881c1e45d17b663d4749af456ab7c2fc1f36405ef8Hans Verkuil
7891c1e45d17b663d4749af456ab7c2fc1f36405ef8Hans Verkuil	/* clear halt and/or toggle; and maybe recover from silicon quirk */
7901c1e45d17b663d4749af456ab7c2fc1f36405ef8Hans Verkuil	if (qh->qh_state == QH_STATE_IDLE)
7911c1e45d17b663d4749af456ab7c2fc1f36405ef8Hans Verkuil		qh_refresh (ehci, qh);
7921c1e45d17b663d4749af456ab7c2fc1f36405ef8Hans Verkuil
7931c1e45d17b663d4749af456ab7c2fc1f36405ef8Hans Verkuil	/* splice right after start */
7941c1e45d17b663d4749af456ab7c2fc1f36405ef8Hans Verkuil	qh->qh_next = head->qh_next;
7951c1e45d17b663d4749af456ab7c2fc1f36405ef8Hans Verkuil	qh->hw_next = head->hw_next;
7961c1e45d17b663d4749af456ab7c2fc1f36405ef8Hans Verkuil	wmb ();
7971c1e45d17b663d4749af456ab7c2fc1f36405ef8Hans Verkuil
7981c1e45d17b663d4749af456ab7c2fc1f36405ef8Hans Verkuil	head->qh_next.qh = qh;
7991c1e45d17b663d4749af456ab7c2fc1f36405ef8Hans Verkuil	head->hw_next = dma;
8001c1e45d17b663d4749af456ab7c2fc1f36405ef8Hans Verkuil
8011c1e45d17b663d4749af456ab7c2fc1f36405ef8Hans Verkuil	qh->qh_state = QH_STATE_LINKED;
8021c1e45d17b663d4749af456ab7c2fc1f36405ef8Hans Verkuil	/* qtd completions reported later by interrupt */
8031c1e45d17b663d4749af456ab7c2fc1f36405ef8Hans Verkuil}
8041c1e45d17b663d4749af456ab7c2fc1f36405ef8Hans Verkuil
8051c1e45d17b663d4749af456ab7c2fc1f36405ef8Hans Verkuil/*-------------------------------------------------------------------------*/
8061c1e45d17b663d4749af456ab7c2fc1f36405ef8Hans Verkuil
8071c1e45d17b663d4749af456ab7c2fc1f36405ef8Hans Verkuil#define	QH_ADDR_MASK	__constant_cpu_to_le32(0x7f)
8081c1e45d17b663d4749af456ab7c2fc1f36405ef8Hans Verkuil
8091c1e45d17b663d4749af456ab7c2fc1f36405ef8Hans Verkuil/*
8101c1e45d17b663d4749af456ab7c2fc1f36405ef8Hans Verkuil * For control/bulk/interrupt, return QH with these TDs appended.
8111c1e45d17b663d4749af456ab7c2fc1f36405ef8Hans Verkuil * Allocates and initializes the QH if necessary.
8121c1e45d17b663d4749af456ab7c2fc1f36405ef8Hans Verkuil * Returns null if it can't allocate a QH it needs to.
8131c1e45d17b663d4749af456ab7c2fc1f36405ef8Hans Verkuil * If the QH has TDs (urbs) already, that's great.
8141c1e45d17b663d4749af456ab7c2fc1f36405ef8Hans Verkuil */
8151c1e45d17b663d4749af456ab7c2fc1f36405ef8Hans Verkuilstatic struct ehci_qh *qh_append_tds (
8161c1e45d17b663d4749af456ab7c2fc1f36405ef8Hans Verkuil	struct ehci_hcd		*ehci,
8171c1e45d17b663d4749af456ab7c2fc1f36405ef8Hans Verkuil	struct urb		*urb,
8181c1e45d17b663d4749af456ab7c2fc1f36405ef8Hans Verkuil	struct list_head	*qtd_list,
8191c1e45d17b663d4749af456ab7c2fc1f36405ef8Hans Verkuil	int			epnum,
8201c1e45d17b663d4749af456ab7c2fc1f36405ef8Hans Verkuil	void			**ptr
8211c1e45d17b663d4749af456ab7c2fc1f36405ef8Hans Verkuil)
8221c1e45d17b663d4749af456ab7c2fc1f36405ef8Hans Verkuil{
8231c1e45d17b663d4749af456ab7c2fc1f36405ef8Hans Verkuil	struct ehci_qh		*qh = NULL;
8241c1e45d17b663d4749af456ab7c2fc1f36405ef8Hans Verkuil
8251c1e45d17b663d4749af456ab7c2fc1f36405ef8Hans Verkuil	qh = (struct ehci_qh *) *ptr;
8261c1e45d17b663d4749af456ab7c2fc1f36405ef8Hans Verkuil	if (unlikely (qh == NULL)) {
8271c1e45d17b663d4749af456ab7c2fc1f36405ef8Hans Verkuil		/* can't sleep here, we have ehci->lock... */
8281c1e45d17b663d4749af456ab7c2fc1f36405ef8Hans Verkuil		qh = qh_make (ehci, urb, GFP_ATOMIC);
8291c1e45d17b663d4749af456ab7c2fc1f36405ef8Hans Verkuil		*ptr = qh;
8301c1e45d17b663d4749af456ab7c2fc1f36405ef8Hans Verkuil	}
8311c1e45d17b663d4749af456ab7c2fc1f36405ef8Hans Verkuil	if (likely (qh != NULL)) {
8321c1e45d17b663d4749af456ab7c2fc1f36405ef8Hans Verkuil		struct ehci_qtd	*qtd;
8331c1e45d17b663d4749af456ab7c2fc1f36405ef8Hans Verkuil
8341c1e45d17b663d4749af456ab7c2fc1f36405ef8Hans Verkuil		if (unlikely (list_empty (qtd_list)))
8351c1e45d17b663d4749af456ab7c2fc1f36405ef8Hans Verkuil			qtd = NULL;
8361c1e45d17b663d4749af456ab7c2fc1f36405ef8Hans Verkuil		else
8371c1e45d17b663d4749af456ab7c2fc1f36405ef8Hans Verkuil			qtd = list_entry (qtd_list->next, struct ehci_qtd,
8381c1e45d17b663d4749af456ab7c2fc1f36405ef8Hans Verkuil					qtd_list);
8391c1e45d17b663d4749af456ab7c2fc1f36405ef8Hans Verkuil
8401c1e45d17b663d4749af456ab7c2fc1f36405ef8Hans Verkuil		/* control qh may need patching ... */
8411c1e45d17b663d4749af456ab7c2fc1f36405ef8Hans Verkuil		if (unlikely (epnum == 0)) {
8421c1e45d17b663d4749af456ab7c2fc1f36405ef8Hans Verkuil
8431c1e45d17b663d4749af456ab7c2fc1f36405ef8Hans Verkuil                        /* usb_reset_device() briefly reverts to address 0 */
8441c1e45d17b663d4749af456ab7c2fc1f36405ef8Hans Verkuil                        if (usb_pipedevice (urb->pipe) == 0)
8451c1e45d17b663d4749af456ab7c2fc1f36405ef8Hans Verkuil                                qh->hw_info1 &= ~QH_ADDR_MASK;
8461c1e45d17b663d4749af456ab7c2fc1f36405ef8Hans Verkuil		}
8471c1e45d17b663d4749af456ab7c2fc1f36405ef8Hans Verkuil
8481c1e45d17b663d4749af456ab7c2fc1f36405ef8Hans Verkuil		/* just one way to queue requests: swap with the dummy qtd.
8491c1e45d17b663d4749af456ab7c2fc1f36405ef8Hans Verkuil		 * only hc or qh_refresh() ever modify the overlay.
8501c1e45d17b663d4749af456ab7c2fc1f36405ef8Hans Verkuil		 */
8511c1e45d17b663d4749af456ab7c2fc1f36405ef8Hans Verkuil		if (likely (qtd != NULL)) {
8521c1e45d17b663d4749af456ab7c2fc1f36405ef8Hans Verkuil			struct ehci_qtd		*dummy;
8531c1e45d17b663d4749af456ab7c2fc1f36405ef8Hans Verkuil			dma_addr_t		dma;
8541c1e45d17b663d4749af456ab7c2fc1f36405ef8Hans Verkuil			__le32			token;
8551c1e45d17b663d4749af456ab7c2fc1f36405ef8Hans Verkuil
8561c1e45d17b663d4749af456ab7c2fc1f36405ef8Hans Verkuil			/* to avoid racing the HC, use the dummy td instead of
8571c1e45d17b663d4749af456ab7c2fc1f36405ef8Hans Verkuil			 * the first td of our list (becomes new dummy).  both
8581c1e45d17b663d4749af456ab7c2fc1f36405ef8Hans Verkuil			 * tds stay deactivated until we're done, when the
8591c1e45d17b663d4749af456ab7c2fc1f36405ef8Hans Verkuil			 * HC is allowed to fetch the old dummy (4.10.2).
8601c1e45d17b663d4749af456ab7c2fc1f36405ef8Hans Verkuil			 */
8611c1e45d17b663d4749af456ab7c2fc1f36405ef8Hans Verkuil			token = qtd->hw_token;
8621c1e45d17b663d4749af456ab7c2fc1f36405ef8Hans Verkuil			qtd->hw_token = HALT_BIT;
8631c1e45d17b663d4749af456ab7c2fc1f36405ef8Hans Verkuil			wmb ();
8641c1e45d17b663d4749af456ab7c2fc1f36405ef8Hans Verkuil			dummy = qh->dummy;
8651c1e45d17b663d4749af456ab7c2fc1f36405ef8Hans Verkuil
8661c1e45d17b663d4749af456ab7c2fc1f36405ef8Hans Verkuil			dma = dummy->qtd_dma;
8671c1e45d17b663d4749af456ab7c2fc1f36405ef8Hans Verkuil			*dummy = *qtd;
8681c1e45d17b663d4749af456ab7c2fc1f36405ef8Hans Verkuil			dummy->qtd_dma = dma;
8691c1e45d17b663d4749af456ab7c2fc1f36405ef8Hans Verkuil
870			list_del (&qtd->qtd_list);
871			list_add (&dummy->qtd_list, qtd_list);
872			__list_splice (qtd_list, qh->qtd_list.prev);
873
874			ehci_qtd_init (qtd, qtd->qtd_dma);
875			qh->dummy = qtd;
876
877			/* hc must see the new dummy at list end */
878			dma = qtd->qtd_dma;
879			qtd = list_entry (qh->qtd_list.prev,
880					struct ehci_qtd, qtd_list);
881			qtd->hw_next = QTD_NEXT (dma);
882
883			/* let the hc process these next qtds */
884			wmb ();
885			dummy->hw_token = token;
886
887			urb->hcpriv = qh_get (qh);
888		}
889	}
890	return qh;
891}
892
893/*-------------------------------------------------------------------------*/
894
895static int
896submit_async (
897	struct ehci_hcd		*ehci,
898	struct usb_host_endpoint *ep,
899	struct urb		*urb,
900	struct list_head	*qtd_list,
901	int			mem_flags
902) {
903	struct ehci_qtd		*qtd;
904	int			epnum;
905	unsigned long		flags;
906	struct ehci_qh		*qh = NULL;
907
908	qtd = list_entry (qtd_list->next, struct ehci_qtd, qtd_list);
909	epnum = ep->desc.bEndpointAddress;
910
911#ifdef EHCI_URB_TRACE
912	ehci_dbg (ehci,
913		"%s %s urb %p ep%d%s len %d, qtd %p [qh %p]\n",
914		__FUNCTION__, urb->dev->devpath, urb,
915		epnum & 0x0f, (epnum & USB_DIR_IN) ? "in" : "out",
916		urb->transfer_buffer_length,
917		qtd, ep->hcpriv);
918#endif
919
920	spin_lock_irqsave (&ehci->lock, flags);
921	qh = qh_append_tds (ehci, urb, qtd_list, epnum, &ep->hcpriv);
922
923	/* Control/bulk operations through TTs don't need scheduling,
924	 * the HC and TT handle it when the TT has a buffer ready.
925	 */
926	if (likely (qh != NULL)) {
927		if (likely (qh->qh_state == QH_STATE_IDLE))
928			qh_link_async (ehci, qh_get (qh));
929	}
930	spin_unlock_irqrestore (&ehci->lock, flags);
931	if (unlikely (qh == NULL)) {
932		qtd_list_free (ehci, urb, qtd_list);
933		return -ENOMEM;
934	}
935	return 0;
936}
937
938/*-------------------------------------------------------------------------*/
939
940/* the async qh for the qtds being reclaimed are now unlinked from the HC */
941
942static void end_unlink_async (struct ehci_hcd *ehci, struct pt_regs *regs)
943{
944	struct ehci_qh		*qh = ehci->reclaim;
945	struct ehci_qh		*next;
946
947	timer_action_done (ehci, TIMER_IAA_WATCHDOG);
948
949	// qh->hw_next = cpu_to_le32 (qh->qh_dma);
950	qh->qh_state = QH_STATE_IDLE;
951	qh->qh_next.qh = NULL;
952	qh_put (qh);			// refcount from reclaim
953
954	/* other unlink(s) may be pending (in QH_STATE_UNLINK_WAIT) */
955	next = qh->reclaim;
956	ehci->reclaim = next;
957	ehci->reclaim_ready = 0;
958	qh->reclaim = NULL;
959
960	qh_completions (ehci, qh, regs);
961
962	if (!list_empty (&qh->qtd_list)
963			&& HC_IS_RUNNING (ehci_to_hcd(ehci)->state))
964		qh_link_async (ehci, qh);
965	else {
966		qh_put (qh);		// refcount from async list
967
968		/* it's not free to turn the async schedule on/off; leave it
969		 * active but idle for a while once it empties.
970		 */
971		if (HC_IS_RUNNING (ehci_to_hcd(ehci)->state)
972				&& ehci->async->qh_next.qh == NULL)
973			timer_action (ehci, TIMER_ASYNC_OFF);
974	}
975
976	if (next) {
977		ehci->reclaim = NULL;
978		start_unlink_async (ehci, next);
979	}
980}
981
982/* makes sure the async qh will become idle */
983/* caller must own ehci->lock */
984
985static void start_unlink_async (struct ehci_hcd *ehci, struct ehci_qh *qh)
986{
987	int		cmd = readl (&ehci->regs->command);
988	struct ehci_qh	*prev;
989
990#ifdef DEBUG
991	assert_spin_locked(&ehci->lock);
992	if (ehci->reclaim
993			|| (qh->qh_state != QH_STATE_LINKED
994				&& qh->qh_state != QH_STATE_UNLINK_WAIT)
995			)
996		BUG ();
997#endif
998
999	/* stop async schedule right now? */
1000	if (unlikely (qh == ehci->async)) {
1001		/* can't get here without STS_ASS set */
1002		if (ehci_to_hcd(ehci)->state != HC_STATE_HALT) {
1003			writel (cmd & ~CMD_ASE, &ehci->regs->command);
1004			wmb ();
1005			// handshake later, if we need to
1006		}
1007		timer_action_done (ehci, TIMER_ASYNC_OFF);
1008		return;
1009	}
1010
1011	qh->qh_state = QH_STATE_UNLINK;
1012	ehci->reclaim = qh = qh_get (qh);
1013
1014	prev = ehci->async;
1015	while (prev->qh_next.qh != qh)
1016		prev = prev->qh_next.qh;
1017
1018	prev->hw_next = qh->hw_next;
1019	prev->qh_next = qh->qh_next;
1020	wmb ();
1021
1022	if (unlikely (ehci_to_hcd(ehci)->state == HC_STATE_HALT)) {
1023		/* if (unlikely (qh->reclaim != 0))
1024		 * 	this will recurse, probably not much
1025		 */
1026		end_unlink_async (ehci, NULL);
1027		return;
1028	}
1029
1030	ehci->reclaim_ready = 0;
1031	cmd |= CMD_IAAD;
1032	writel (cmd, &ehci->regs->command);
1033	(void) readl (&ehci->regs->command);
1034	timer_action (ehci, TIMER_IAA_WATCHDOG);
1035}
1036
1037/*-------------------------------------------------------------------------*/
1038
1039static void
1040scan_async (struct ehci_hcd *ehci, struct pt_regs *regs)
1041{
1042	struct ehci_qh		*qh;
1043	enum ehci_timer_action	action = TIMER_IO_WATCHDOG;
1044
1045	if (!++(ehci->stamp))
1046		ehci->stamp++;
1047	timer_action_done (ehci, TIMER_ASYNC_SHRINK);
1048rescan:
1049	qh = ehci->async->qh_next.qh;
1050	if (likely (qh != NULL)) {
1051		do {
1052			/* clean any finished work for this qh */
1053			if (!list_empty (&qh->qtd_list)
1054					&& qh->stamp != ehci->stamp) {
1055				int temp;
1056
1057				/* unlinks could happen here; completion
1058				 * reporting drops the lock.  rescan using
1059				 * the latest schedule, but don't rescan
1060				 * qhs we already finished (no looping).
1061				 */
1062				qh = qh_get (qh);
1063				qh->stamp = ehci->stamp;
1064				temp = qh_completions (ehci, qh, regs);
1065				qh_put (qh);
1066				if (temp != 0) {
1067					goto rescan;
1068				}
1069			}
1070
1071			/* unlink idle entries, reducing HC PCI usage as well
1072			 * as HCD schedule-scanning costs.  delay for any qh
1073			 * we just scanned, there's a not-unusual case that it
1074			 * doesn't stay idle for long.
1075			 * (plus, avoids some kind of re-activation race.)
1076			 */
1077			if (list_empty (&qh->qtd_list)) {
1078				if (qh->stamp == ehci->stamp)
1079					action = TIMER_ASYNC_SHRINK;
1080				else if (!ehci->reclaim
1081					    && qh->qh_state == QH_STATE_LINKED)
1082					start_unlink_async (ehci, qh);
1083			}
1084
1085			qh = qh->qh_next.qh;
1086		} while (qh);
1087	}
1088	if (action == TIMER_ASYNC_SHRINK)
1089		timer_action (ehci, TIMER_ASYNC_SHRINK);
1090}
1091