xref: /openbmc/linux/drivers/usb/host/uhci-q.c (revision 04538a255ac8b404c20cbf15867c9829254c470f)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  * Universal Host Controller Interface driver for USB.
31da177e4SLinus Torvalds  *
41da177e4SLinus Torvalds  * Maintainer: Alan Stern <stern@rowland.harvard.edu>
51da177e4SLinus Torvalds  *
61da177e4SLinus Torvalds  * (C) Copyright 1999 Linus Torvalds
71da177e4SLinus Torvalds  * (C) Copyright 1999-2002 Johannes Erdfelt, johannes@erdfelt.com
81da177e4SLinus Torvalds  * (C) Copyright 1999 Randy Dunlap
91da177e4SLinus Torvalds  * (C) Copyright 1999 Georg Acher, acher@in.tum.de
101da177e4SLinus Torvalds  * (C) Copyright 1999 Deti Fliegl, deti@fliegl.de
111da177e4SLinus Torvalds  * (C) Copyright 1999 Thomas Sailer, sailer@ife.ee.ethz.ch
121da177e4SLinus Torvalds  * (C) Copyright 1999 Roman Weissgaerber, weissg@vienna.at
131da177e4SLinus Torvalds  * (C) Copyright 2000 Yggdrasil Computing, Inc. (port of new PCI interface
141da177e4SLinus Torvalds  *               support from usb-ohci.c by Adam Richter, adam@yggdrasil.com).
151da177e4SLinus Torvalds  * (C) Copyright 1999 Gregory P. Smith (from usb-ohci.c)
16dccf4a48SAlan Stern  * (C) Copyright 2004-2005 Alan Stern, stern@rowland.harvard.edu
171da177e4SLinus Torvalds  */
181da177e4SLinus Torvalds 
191da177e4SLinus Torvalds 
201da177e4SLinus Torvalds /*
211da177e4SLinus Torvalds  * Technically, updating td->status here is a race, but it's not really a
221da177e4SLinus Torvalds  * problem. The worst that can happen is that we set the IOC bit again
231da177e4SLinus Torvalds  * generating a spurious interrupt. We could fix this by creating another
241da177e4SLinus Torvalds  * QH and leaving the IOC bit always set, but then we would have to play
251da177e4SLinus Torvalds  * games with the FSBR code to make sure we get the correct order in all
261da177e4SLinus Torvalds  * the cases. I don't think it's worth the effort
271da177e4SLinus Torvalds  */
28dccf4a48SAlan Stern static void uhci_set_next_interrupt(struct uhci_hcd *uhci)
291da177e4SLinus Torvalds {
306c1b445cSAlan Stern 	if (uhci->is_stopped)
311f09df8bSAlan Stern 		mod_timer(&uhci_to_hcd(uhci)->rh_timer, jiffies);
321da177e4SLinus Torvalds 	uhci->term_td->status |= cpu_to_le32(TD_CTRL_IOC);
331da177e4SLinus Torvalds }
341da177e4SLinus Torvalds 
351da177e4SLinus Torvalds static inline void uhci_clear_next_interrupt(struct uhci_hcd *uhci)
361da177e4SLinus Torvalds {
371da177e4SLinus Torvalds 	uhci->term_td->status &= ~cpu_to_le32(TD_CTRL_IOC);
381da177e4SLinus Torvalds }
391da177e4SLinus Torvalds 
402532178aSAlan Stern static struct uhci_td *uhci_alloc_td(struct uhci_hcd *uhci)
411da177e4SLinus Torvalds {
421da177e4SLinus Torvalds 	dma_addr_t dma_handle;
431da177e4SLinus Torvalds 	struct uhci_td *td;
441da177e4SLinus Torvalds 
451da177e4SLinus Torvalds 	td = dma_pool_alloc(uhci->td_pool, GFP_ATOMIC, &dma_handle);
461da177e4SLinus Torvalds 	if (!td)
471da177e4SLinus Torvalds 		return NULL;
481da177e4SLinus Torvalds 
491da177e4SLinus Torvalds 	td->dma_handle = dma_handle;
501da177e4SLinus Torvalds 	td->frame = -1;
511da177e4SLinus Torvalds 
521da177e4SLinus Torvalds 	INIT_LIST_HEAD(&td->list);
531da177e4SLinus Torvalds 	INIT_LIST_HEAD(&td->fl_list);
541da177e4SLinus Torvalds 
551da177e4SLinus Torvalds 	return td;
561da177e4SLinus Torvalds }
571da177e4SLinus Torvalds 
58dccf4a48SAlan Stern static void uhci_free_td(struct uhci_hcd *uhci, struct uhci_td *td)
59dccf4a48SAlan Stern {
60dccf4a48SAlan Stern 	if (!list_empty(&td->list))
61dccf4a48SAlan Stern 		dev_warn(uhci_dev(uhci), "td %p still in list!\n", td);
62dccf4a48SAlan Stern 	if (!list_empty(&td->fl_list))
63dccf4a48SAlan Stern 		dev_warn(uhci_dev(uhci), "td %p still in fl_list!\n", td);
64dccf4a48SAlan Stern 
65dccf4a48SAlan Stern 	dma_pool_free(uhci->td_pool, td, td->dma_handle);
66dccf4a48SAlan Stern }
67dccf4a48SAlan Stern 
681da177e4SLinus Torvalds static inline void uhci_fill_td(struct uhci_td *td, u32 status,
691da177e4SLinus Torvalds 		u32 token, u32 buffer)
701da177e4SLinus Torvalds {
711da177e4SLinus Torvalds 	td->status = cpu_to_le32(status);
721da177e4SLinus Torvalds 	td->token = cpu_to_le32(token);
731da177e4SLinus Torvalds 	td->buffer = cpu_to_le32(buffer);
741da177e4SLinus Torvalds }
751da177e4SLinus Torvalds 
76*04538a25SAlan Stern static void uhci_add_td_to_urbp(struct uhci_td *td, struct urb_priv *urbp)
77*04538a25SAlan Stern {
78*04538a25SAlan Stern 	list_add_tail(&td->list, &urbp->td_list);
79*04538a25SAlan Stern }
80*04538a25SAlan Stern 
81*04538a25SAlan Stern static void uhci_remove_td_from_urbp(struct uhci_td *td)
82*04538a25SAlan Stern {
83*04538a25SAlan Stern 	list_del_init(&td->list);
84*04538a25SAlan Stern }
85*04538a25SAlan Stern 
861da177e4SLinus Torvalds /*
87687f5f34SAlan Stern  * We insert Isochronous URBs directly into the frame list at the beginning
881da177e4SLinus Torvalds  */
89dccf4a48SAlan Stern static inline void uhci_insert_td_in_frame_list(struct uhci_hcd *uhci,
90dccf4a48SAlan Stern 		struct uhci_td *td, unsigned framenum)
911da177e4SLinus Torvalds {
921da177e4SLinus Torvalds 	framenum &= (UHCI_NUMFRAMES - 1);
931da177e4SLinus Torvalds 
941da177e4SLinus Torvalds 	td->frame = framenum;
951da177e4SLinus Torvalds 
961da177e4SLinus Torvalds 	/* Is there a TD already mapped there? */
97a1d59ce8SAlan Stern 	if (uhci->frame_cpu[framenum]) {
981da177e4SLinus Torvalds 		struct uhci_td *ftd, *ltd;
991da177e4SLinus Torvalds 
100a1d59ce8SAlan Stern 		ftd = uhci->frame_cpu[framenum];
1011da177e4SLinus Torvalds 		ltd = list_entry(ftd->fl_list.prev, struct uhci_td, fl_list);
1021da177e4SLinus Torvalds 
1031da177e4SLinus Torvalds 		list_add_tail(&td->fl_list, &ftd->fl_list);
1041da177e4SLinus Torvalds 
1051da177e4SLinus Torvalds 		td->link = ltd->link;
1061da177e4SLinus Torvalds 		wmb();
1071da177e4SLinus Torvalds 		ltd->link = cpu_to_le32(td->dma_handle);
1081da177e4SLinus Torvalds 	} else {
109a1d59ce8SAlan Stern 		td->link = uhci->frame[framenum];
1101da177e4SLinus Torvalds 		wmb();
111a1d59ce8SAlan Stern 		uhci->frame[framenum] = cpu_to_le32(td->dma_handle);
112a1d59ce8SAlan Stern 		uhci->frame_cpu[framenum] = td;
1131da177e4SLinus Torvalds 	}
1141da177e4SLinus Torvalds }
1151da177e4SLinus Torvalds 
116dccf4a48SAlan Stern static inline void uhci_remove_td_from_frame_list(struct uhci_hcd *uhci,
117b81d3436SAlan Stern 		struct uhci_td *td)
1181da177e4SLinus Torvalds {
1191da177e4SLinus Torvalds 	/* If it's not inserted, don't remove it */
120b81d3436SAlan Stern 	if (td->frame == -1) {
121b81d3436SAlan Stern 		WARN_ON(!list_empty(&td->fl_list));
1221da177e4SLinus Torvalds 		return;
123b81d3436SAlan Stern 	}
1241da177e4SLinus Torvalds 
125b81d3436SAlan Stern 	if (uhci->frame_cpu[td->frame] == td) {
1261da177e4SLinus Torvalds 		if (list_empty(&td->fl_list)) {
127a1d59ce8SAlan Stern 			uhci->frame[td->frame] = td->link;
128a1d59ce8SAlan Stern 			uhci->frame_cpu[td->frame] = NULL;
1291da177e4SLinus Torvalds 		} else {
1301da177e4SLinus Torvalds 			struct uhci_td *ntd;
1311da177e4SLinus Torvalds 
1321da177e4SLinus Torvalds 			ntd = list_entry(td->fl_list.next, struct uhci_td, fl_list);
133a1d59ce8SAlan Stern 			uhci->frame[td->frame] = cpu_to_le32(ntd->dma_handle);
134a1d59ce8SAlan Stern 			uhci->frame_cpu[td->frame] = ntd;
1351da177e4SLinus Torvalds 		}
1361da177e4SLinus Torvalds 	} else {
1371da177e4SLinus Torvalds 		struct uhci_td *ptd;
1381da177e4SLinus Torvalds 
1391da177e4SLinus Torvalds 		ptd = list_entry(td->fl_list.prev, struct uhci_td, fl_list);
1401da177e4SLinus Torvalds 		ptd->link = td->link;
1411da177e4SLinus Torvalds 	}
1421da177e4SLinus Torvalds 
1431da177e4SLinus Torvalds 	list_del_init(&td->fl_list);
1441da177e4SLinus Torvalds 	td->frame = -1;
1451da177e4SLinus Torvalds }
1461da177e4SLinus Torvalds 
147dccf4a48SAlan Stern /*
148dccf4a48SAlan Stern  * Remove all the TDs for an Isochronous URB from the frame list
149dccf4a48SAlan Stern  */
150dccf4a48SAlan Stern static void uhci_unlink_isochronous_tds(struct uhci_hcd *uhci, struct urb *urb)
151b81d3436SAlan Stern {
152b81d3436SAlan Stern 	struct urb_priv *urbp = (struct urb_priv *) urb->hcpriv;
153b81d3436SAlan Stern 	struct uhci_td *td;
154b81d3436SAlan Stern 
155b81d3436SAlan Stern 	list_for_each_entry(td, &urbp->td_list, list)
156dccf4a48SAlan Stern 		uhci_remove_td_from_frame_list(uhci, td);
157b81d3436SAlan Stern 	wmb();
158b81d3436SAlan Stern }
159b81d3436SAlan Stern 
160dccf4a48SAlan Stern static struct uhci_qh *uhci_alloc_qh(struct uhci_hcd *uhci,
161dccf4a48SAlan Stern 		struct usb_device *udev, struct usb_host_endpoint *hep)
1621da177e4SLinus Torvalds {
1631da177e4SLinus Torvalds 	dma_addr_t dma_handle;
1641da177e4SLinus Torvalds 	struct uhci_qh *qh;
1651da177e4SLinus Torvalds 
1661da177e4SLinus Torvalds 	qh = dma_pool_alloc(uhci->qh_pool, GFP_ATOMIC, &dma_handle);
1671da177e4SLinus Torvalds 	if (!qh)
1681da177e4SLinus Torvalds 		return NULL;
1691da177e4SLinus Torvalds 
17059e29ed9SAlan Stern 	memset(qh, 0, sizeof(*qh));
1711da177e4SLinus Torvalds 	qh->dma_handle = dma_handle;
1721da177e4SLinus Torvalds 
1731da177e4SLinus Torvalds 	qh->element = UHCI_PTR_TERM;
1741da177e4SLinus Torvalds 	qh->link = UHCI_PTR_TERM;
1751da177e4SLinus Torvalds 
176dccf4a48SAlan Stern 	INIT_LIST_HEAD(&qh->queue);
177dccf4a48SAlan Stern 	INIT_LIST_HEAD(&qh->node);
1781da177e4SLinus Torvalds 
179dccf4a48SAlan Stern 	if (udev) {		/* Normal QH */
180af0bb599SAlan Stern 		qh->dummy_td = uhci_alloc_td(uhci);
181af0bb599SAlan Stern 		if (!qh->dummy_td) {
182af0bb599SAlan Stern 			dma_pool_free(uhci->qh_pool, qh, dma_handle);
183af0bb599SAlan Stern 			return NULL;
184af0bb599SAlan Stern 		}
185dccf4a48SAlan Stern 		qh->state = QH_STATE_IDLE;
186dccf4a48SAlan Stern 		qh->hep = hep;
187dccf4a48SAlan Stern 		qh->udev = udev;
188dccf4a48SAlan Stern 		hep->hcpriv = qh;
1894de7d2c2SAlan Stern 		qh->type = hep->desc.bmAttributes & USB_ENDPOINT_XFERTYPE_MASK;
1901da177e4SLinus Torvalds 
191dccf4a48SAlan Stern 	} else {		/* Skeleton QH */
192dccf4a48SAlan Stern 		qh->state = QH_STATE_ACTIVE;
1934de7d2c2SAlan Stern 		qh->type = -1;
194dccf4a48SAlan Stern 	}
1951da177e4SLinus Torvalds 	return qh;
1961da177e4SLinus Torvalds }
1971da177e4SLinus Torvalds 
1981da177e4SLinus Torvalds static void uhci_free_qh(struct uhci_hcd *uhci, struct uhci_qh *qh)
1991da177e4SLinus Torvalds {
200dccf4a48SAlan Stern 	WARN_ON(qh->state != QH_STATE_IDLE && qh->udev);
201dccf4a48SAlan Stern 	if (!list_empty(&qh->queue))
2021da177e4SLinus Torvalds 		dev_warn(uhci_dev(uhci), "qh %p list not empty!\n", qh);
2031da177e4SLinus Torvalds 
204dccf4a48SAlan Stern 	list_del(&qh->node);
205dccf4a48SAlan Stern 	if (qh->udev) {
206dccf4a48SAlan Stern 		qh->hep->hcpriv = NULL;
207af0bb599SAlan Stern 		uhci_free_td(uhci, qh->dummy_td);
208dccf4a48SAlan Stern 	}
2091da177e4SLinus Torvalds 	dma_pool_free(uhci->qh_pool, qh, qh->dma_handle);
2101da177e4SLinus Torvalds }
2111da177e4SLinus Torvalds 
2121da177e4SLinus Torvalds /*
213a0b458b6SAlan Stern  * When a queue is stopped and a dequeued URB is given back, adjust
214a0b458b6SAlan Stern  * the previous TD link (if the URB isn't first on the queue) or
215a0b458b6SAlan Stern  * save its toggle value (if it is first and is currently executing).
2160ed8fee1SAlan Stern  */
217a0b458b6SAlan Stern static void uhci_cleanup_queue(struct uhci_qh *qh,
218a0b458b6SAlan Stern 		struct urb *urb)
2190ed8fee1SAlan Stern {
220a0b458b6SAlan Stern 	struct urb_priv *urbp = urb->hcpriv;
2210ed8fee1SAlan Stern 	struct uhci_td *td;
2220ed8fee1SAlan Stern 
223a0b458b6SAlan Stern 	/* Isochronous pipes don't use toggles and their TD link pointers
224a0b458b6SAlan Stern 	 * get adjusted during uhci_urb_dequeue(). */
225a0b458b6SAlan Stern 	if (qh->type == USB_ENDPOINT_XFER_ISOC)
226a0b458b6SAlan Stern 		return;
227a0b458b6SAlan Stern 
228a0b458b6SAlan Stern 	/* If the URB isn't first on its queue, adjust the link pointer
229a0b458b6SAlan Stern 	 * of the last TD in the previous URB.  The toggle doesn't need
230a0b458b6SAlan Stern 	 * to be saved since this URB can't be executing yet. */
231a0b458b6SAlan Stern 	if (qh->queue.next != &urbp->node) {
232a0b458b6SAlan Stern 		struct urb_priv *purbp;
233a0b458b6SAlan Stern 		struct uhci_td *ptd;
234a0b458b6SAlan Stern 
235a0b458b6SAlan Stern 		purbp = list_entry(urbp->node.prev, struct urb_priv, node);
236a0b458b6SAlan Stern 		WARN_ON(list_empty(&purbp->td_list));
237a0b458b6SAlan Stern 		ptd = list_entry(purbp->td_list.prev, struct uhci_td,
238a0b458b6SAlan Stern 				list);
239a0b458b6SAlan Stern 		td = list_entry(urbp->td_list.prev, struct uhci_td,
240a0b458b6SAlan Stern 				list);
241a0b458b6SAlan Stern 		ptd->link = td->link;
242a0b458b6SAlan Stern 		return;
243a0b458b6SAlan Stern 	}
244a0b458b6SAlan Stern 
2450ed8fee1SAlan Stern 	/* If the QH element pointer is UHCI_PTR_TERM then then currently
2460ed8fee1SAlan Stern 	 * executing URB has already been unlinked, so this one isn't it. */
247a0b458b6SAlan Stern 	if (qh_element(qh) == UHCI_PTR_TERM)
2480ed8fee1SAlan Stern 		return;
2490ed8fee1SAlan Stern 	qh->element = UHCI_PTR_TERM;
2500ed8fee1SAlan Stern 
251a0b458b6SAlan Stern 	/* Control pipes have to worry about toggles */
252a0b458b6SAlan Stern 	if (qh->type == USB_ENDPOINT_XFER_CONTROL)
2530ed8fee1SAlan Stern 		return;
2540ed8fee1SAlan Stern 
255a0b458b6SAlan Stern 	/* Save the next toggle value */
25659e29ed9SAlan Stern 	WARN_ON(list_empty(&urbp->td_list));
25759e29ed9SAlan Stern 	td = list_entry(urbp->td_list.next, struct uhci_td, list);
2580ed8fee1SAlan Stern 	qh->needs_fixup = 1;
2590ed8fee1SAlan Stern 	qh->initial_toggle = uhci_toggle(td_token(td));
2600ed8fee1SAlan Stern }
2610ed8fee1SAlan Stern 
2620ed8fee1SAlan Stern /*
2630ed8fee1SAlan Stern  * Fix up the data toggles for URBs in a queue, when one of them
2640ed8fee1SAlan Stern  * terminates early (short transfer, error, or dequeued).
2650ed8fee1SAlan Stern  */
2660ed8fee1SAlan Stern static void uhci_fixup_toggles(struct uhci_qh *qh, int skip_first)
2670ed8fee1SAlan Stern {
2680ed8fee1SAlan Stern 	struct urb_priv *urbp = NULL;
2690ed8fee1SAlan Stern 	struct uhci_td *td;
2700ed8fee1SAlan Stern 	unsigned int toggle = qh->initial_toggle;
2710ed8fee1SAlan Stern 	unsigned int pipe;
2720ed8fee1SAlan Stern 
2730ed8fee1SAlan Stern 	/* Fixups for a short transfer start with the second URB in the
2740ed8fee1SAlan Stern 	 * queue (the short URB is the first). */
2750ed8fee1SAlan Stern 	if (skip_first)
2760ed8fee1SAlan Stern 		urbp = list_entry(qh->queue.next, struct urb_priv, node);
2770ed8fee1SAlan Stern 
2780ed8fee1SAlan Stern 	/* When starting with the first URB, if the QH element pointer is
2790ed8fee1SAlan Stern 	 * still valid then we know the URB's toggles are okay. */
2800ed8fee1SAlan Stern 	else if (qh_element(qh) != UHCI_PTR_TERM)
2810ed8fee1SAlan Stern 		toggle = 2;
2820ed8fee1SAlan Stern 
2830ed8fee1SAlan Stern 	/* Fix up the toggle for the URBs in the queue.  Normally this
2840ed8fee1SAlan Stern 	 * loop won't run more than once: When an error or short transfer
2850ed8fee1SAlan Stern 	 * occurs, the queue usually gets emptied. */
2861393adb2SAlan Stern 	urbp = list_prepare_entry(urbp, &qh->queue, node);
2870ed8fee1SAlan Stern 	list_for_each_entry_continue(urbp, &qh->queue, node) {
2880ed8fee1SAlan Stern 
2890ed8fee1SAlan Stern 		/* If the first TD has the right toggle value, we don't
2900ed8fee1SAlan Stern 		 * need to change any toggles in this URB */
2910ed8fee1SAlan Stern 		td = list_entry(urbp->td_list.next, struct uhci_td, list);
2920ed8fee1SAlan Stern 		if (toggle > 1 || uhci_toggle(td_token(td)) == toggle) {
2930ed8fee1SAlan Stern 			td = list_entry(urbp->td_list.next, struct uhci_td,
2940ed8fee1SAlan Stern 					list);
2950ed8fee1SAlan Stern 			toggle = uhci_toggle(td_token(td)) ^ 1;
2960ed8fee1SAlan Stern 
2970ed8fee1SAlan Stern 		/* Otherwise all the toggles in the URB have to be switched */
2980ed8fee1SAlan Stern 		} else {
2990ed8fee1SAlan Stern 			list_for_each_entry(td, &urbp->td_list, list) {
3000ed8fee1SAlan Stern 				td->token ^= __constant_cpu_to_le32(
3010ed8fee1SAlan Stern 							TD_TOKEN_TOGGLE);
3020ed8fee1SAlan Stern 				toggle ^= 1;
3030ed8fee1SAlan Stern 			}
3040ed8fee1SAlan Stern 		}
3050ed8fee1SAlan Stern 	}
3060ed8fee1SAlan Stern 
3070ed8fee1SAlan Stern 	wmb();
3080ed8fee1SAlan Stern 	pipe = list_entry(qh->queue.next, struct urb_priv, node)->urb->pipe;
3090ed8fee1SAlan Stern 	usb_settoggle(qh->udev, usb_pipeendpoint(pipe),
3100ed8fee1SAlan Stern 			usb_pipeout(pipe), toggle);
3110ed8fee1SAlan Stern 	qh->needs_fixup = 0;
3120ed8fee1SAlan Stern }
3130ed8fee1SAlan Stern 
3140ed8fee1SAlan Stern /*
315dccf4a48SAlan Stern  * Put a QH on the schedule in both hardware and software
3161da177e4SLinus Torvalds  */
317dccf4a48SAlan Stern static void uhci_activate_qh(struct uhci_hcd *uhci, struct uhci_qh *qh)
3181da177e4SLinus Torvalds {
3191da177e4SLinus Torvalds 	struct uhci_qh *pqh;
3201da177e4SLinus Torvalds 
321dccf4a48SAlan Stern 	WARN_ON(list_empty(&qh->queue));
322dccf4a48SAlan Stern 
323dccf4a48SAlan Stern 	/* Set the element pointer if it isn't set already.
324dccf4a48SAlan Stern 	 * This isn't needed for Isochronous queues, but it doesn't hurt. */
325dccf4a48SAlan Stern 	if (qh_element(qh) == UHCI_PTR_TERM) {
326dccf4a48SAlan Stern 		struct urb_priv *urbp = list_entry(qh->queue.next,
327dccf4a48SAlan Stern 				struct urb_priv, node);
328dccf4a48SAlan Stern 		struct uhci_td *td = list_entry(urbp->td_list.next,
329dccf4a48SAlan Stern 				struct uhci_td, list);
330dccf4a48SAlan Stern 
331dccf4a48SAlan Stern 		qh->element = cpu_to_le32(td->dma_handle);
332dccf4a48SAlan Stern 	}
333dccf4a48SAlan Stern 
334dccf4a48SAlan Stern 	if (qh->state == QH_STATE_ACTIVE)
3351da177e4SLinus Torvalds 		return;
336dccf4a48SAlan Stern 	qh->state = QH_STATE_ACTIVE;
337dccf4a48SAlan Stern 
338dccf4a48SAlan Stern 	/* Move the QH from its old list to the end of the appropriate
339dccf4a48SAlan Stern 	 * skeleton's list */
3400ed8fee1SAlan Stern 	if (qh == uhci->next_qh)
3410ed8fee1SAlan Stern 		uhci->next_qh = list_entry(qh->node.next, struct uhci_qh,
3420ed8fee1SAlan Stern 				node);
343dccf4a48SAlan Stern 	list_move_tail(&qh->node, &qh->skel->node);
344dccf4a48SAlan Stern 
345dccf4a48SAlan Stern 	/* Link it into the schedule */
346dccf4a48SAlan Stern 	pqh = list_entry(qh->node.prev, struct uhci_qh, node);
347dccf4a48SAlan Stern 	qh->link = pqh->link;
348dccf4a48SAlan Stern 	wmb();
349dccf4a48SAlan Stern 	pqh->link = UHCI_PTR_QH | cpu_to_le32(qh->dma_handle);
350dccf4a48SAlan Stern }
3511da177e4SLinus Torvalds 
3521da177e4SLinus Torvalds /*
353dccf4a48SAlan Stern  * Take a QH off the hardware schedule
3541da177e4SLinus Torvalds  */
355dccf4a48SAlan Stern static void uhci_unlink_qh(struct uhci_hcd *uhci, struct uhci_qh *qh)
356dccf4a48SAlan Stern {
357dccf4a48SAlan Stern 	struct uhci_qh *pqh;
3581da177e4SLinus Torvalds 
359dccf4a48SAlan Stern 	if (qh->state == QH_STATE_UNLINKING)
360dccf4a48SAlan Stern 		return;
361dccf4a48SAlan Stern 	WARN_ON(qh->state != QH_STATE_ACTIVE || !qh->udev);
362dccf4a48SAlan Stern 	qh->state = QH_STATE_UNLINKING;
3631da177e4SLinus Torvalds 
364dccf4a48SAlan Stern 	/* Unlink the QH from the schedule and record when we did it */
365dccf4a48SAlan Stern 	pqh = list_entry(qh->node.prev, struct uhci_qh, node);
366dccf4a48SAlan Stern 	pqh->link = qh->link;
367dccf4a48SAlan Stern 	mb();
3681da177e4SLinus Torvalds 
3691da177e4SLinus Torvalds 	uhci_get_current_frame_number(uhci);
370dccf4a48SAlan Stern 	qh->unlink_frame = uhci->frame_number;
3711da177e4SLinus Torvalds 
372dccf4a48SAlan Stern 	/* Force an interrupt so we know when the QH is fully unlinked */
373dccf4a48SAlan Stern 	if (list_empty(&uhci->skel_unlink_qh->node))
3741da177e4SLinus Torvalds 		uhci_set_next_interrupt(uhci);
3751da177e4SLinus Torvalds 
376dccf4a48SAlan Stern 	/* Move the QH from its old list to the end of the unlinking list */
3770ed8fee1SAlan Stern 	if (qh == uhci->next_qh)
3780ed8fee1SAlan Stern 		uhci->next_qh = list_entry(qh->node.next, struct uhci_qh,
3790ed8fee1SAlan Stern 				node);
380dccf4a48SAlan Stern 	list_move_tail(&qh->node, &uhci->skel_unlink_qh->node);
3811da177e4SLinus Torvalds }
3821da177e4SLinus Torvalds 
3831da177e4SLinus Torvalds /*
384dccf4a48SAlan Stern  * When we and the controller are through with a QH, it becomes IDLE.
385dccf4a48SAlan Stern  * This happens when a QH has been off the schedule (on the unlinking
386dccf4a48SAlan Stern  * list) for more than one frame, or when an error occurs while adding
387dccf4a48SAlan Stern  * the first URB onto a new QH.
3881da177e4SLinus Torvalds  */
389dccf4a48SAlan Stern static void uhci_make_qh_idle(struct uhci_hcd *uhci, struct uhci_qh *qh)
390dccf4a48SAlan Stern {
391dccf4a48SAlan Stern 	WARN_ON(qh->state == QH_STATE_ACTIVE);
392dccf4a48SAlan Stern 
3930ed8fee1SAlan Stern 	if (qh == uhci->next_qh)
3940ed8fee1SAlan Stern 		uhci->next_qh = list_entry(qh->node.next, struct uhci_qh,
3950ed8fee1SAlan Stern 				node);
396dccf4a48SAlan Stern 	list_move(&qh->node, &uhci->idle_qh_list);
397dccf4a48SAlan Stern 	qh->state = QH_STATE_IDLE;
398dccf4a48SAlan Stern 
39959e29ed9SAlan Stern 	/* Now that the QH is idle, its post_td isn't being used */
40059e29ed9SAlan Stern 	if (qh->post_td) {
40159e29ed9SAlan Stern 		uhci_free_td(uhci, qh->post_td);
40259e29ed9SAlan Stern 		qh->post_td = NULL;
40359e29ed9SAlan Stern 	}
40459e29ed9SAlan Stern 
405dccf4a48SAlan Stern 	/* If anyone is waiting for a QH to become idle, wake them up */
406dccf4a48SAlan Stern 	if (uhci->num_waiting)
407dccf4a48SAlan Stern 		wake_up_all(&uhci->waitqh);
4081da177e4SLinus Torvalds }
4091da177e4SLinus Torvalds 
410dccf4a48SAlan Stern static inline struct urb_priv *uhci_alloc_urb_priv(struct uhci_hcd *uhci,
411dccf4a48SAlan Stern 		struct urb *urb)
4121da177e4SLinus Torvalds {
4131da177e4SLinus Torvalds 	struct urb_priv *urbp;
4141da177e4SLinus Torvalds 
4151da177e4SLinus Torvalds 	urbp = kmem_cache_alloc(uhci_up_cachep, SLAB_ATOMIC);
4161da177e4SLinus Torvalds 	if (!urbp)
4171da177e4SLinus Torvalds 		return NULL;
4181da177e4SLinus Torvalds 
4191da177e4SLinus Torvalds 	memset((void *)urbp, 0, sizeof(*urbp));
4201da177e4SLinus Torvalds 
4211da177e4SLinus Torvalds 	urbp->urb = urb;
4221da177e4SLinus Torvalds 	urb->hcpriv = urbp;
423dccf4a48SAlan Stern 
424dccf4a48SAlan Stern 	INIT_LIST_HEAD(&urbp->node);
425dccf4a48SAlan Stern 	INIT_LIST_HEAD(&urbp->td_list);
4261da177e4SLinus Torvalds 
4271da177e4SLinus Torvalds 	return urbp;
4281da177e4SLinus Torvalds }
4291da177e4SLinus Torvalds 
430dccf4a48SAlan Stern static void uhci_free_urb_priv(struct uhci_hcd *uhci,
431dccf4a48SAlan Stern 		struct urb_priv *urbp)
4321da177e4SLinus Torvalds {
4331da177e4SLinus Torvalds 	struct uhci_td *td, *tmp;
4341da177e4SLinus Torvalds 
435dccf4a48SAlan Stern 	if (!list_empty(&urbp->node))
436dccf4a48SAlan Stern 		dev_warn(uhci_dev(uhci), "urb %p still on QH's list!\n",
437dccf4a48SAlan Stern 				urbp->urb);
4381da177e4SLinus Torvalds 
4391da177e4SLinus Torvalds 	list_for_each_entry_safe(td, tmp, &urbp->td_list, list) {
440*04538a25SAlan Stern 		uhci_remove_td_from_urbp(td);
441*04538a25SAlan Stern 		uhci_free_td(uhci, td);
4421da177e4SLinus Torvalds 	}
4431da177e4SLinus Torvalds 
444dccf4a48SAlan Stern 	urbp->urb->hcpriv = NULL;
4451da177e4SLinus Torvalds 	kmem_cache_free(uhci_up_cachep, urbp);
4461da177e4SLinus Torvalds }
4471da177e4SLinus Torvalds 
4481da177e4SLinus Torvalds static void uhci_inc_fsbr(struct uhci_hcd *uhci, struct urb *urb)
4491da177e4SLinus Torvalds {
4501da177e4SLinus Torvalds 	struct urb_priv *urbp = (struct urb_priv *)urb->hcpriv;
4511da177e4SLinus Torvalds 
4521da177e4SLinus Torvalds 	if ((!(urb->transfer_flags & URB_NO_FSBR)) && !urbp->fsbr) {
4531da177e4SLinus Torvalds 		urbp->fsbr = 1;
4541da177e4SLinus Torvalds 		if (!uhci->fsbr++ && !uhci->fsbrtimeout)
4551da177e4SLinus Torvalds 			uhci->skel_term_qh->link = cpu_to_le32(uhci->skel_fs_control_qh->dma_handle) | UHCI_PTR_QH;
4561da177e4SLinus Torvalds 	}
4571da177e4SLinus Torvalds }
4581da177e4SLinus Torvalds 
4591da177e4SLinus Torvalds static void uhci_dec_fsbr(struct uhci_hcd *uhci, struct urb *urb)
4601da177e4SLinus Torvalds {
4611da177e4SLinus Torvalds 	struct urb_priv *urbp = (struct urb_priv *)urb->hcpriv;
4621da177e4SLinus Torvalds 
4631da177e4SLinus Torvalds 	if ((!(urb->transfer_flags & URB_NO_FSBR)) && urbp->fsbr) {
4641da177e4SLinus Torvalds 		urbp->fsbr = 0;
4651da177e4SLinus Torvalds 		if (!--uhci->fsbr)
4661da177e4SLinus Torvalds 			uhci->fsbrtimeout = jiffies + FSBR_DELAY;
4671da177e4SLinus Torvalds 	}
4681da177e4SLinus Torvalds }
4691da177e4SLinus Torvalds 
4701da177e4SLinus Torvalds /*
4711da177e4SLinus Torvalds  * Map status to standard result codes
4721da177e4SLinus Torvalds  *
4731da177e4SLinus Torvalds  * <status> is (td_status(td) & 0xF60000), a.k.a.
4741da177e4SLinus Torvalds  * uhci_status_bits(td_status(td)).
4751da177e4SLinus Torvalds  * Note: <status> does not include the TD_CTRL_NAK bit.
4761da177e4SLinus Torvalds  * <dir_out> is True for output TDs and False for input TDs.
4771da177e4SLinus Torvalds  */
4781da177e4SLinus Torvalds static int uhci_map_status(int status, int dir_out)
4791da177e4SLinus Torvalds {
4801da177e4SLinus Torvalds 	if (!status)
4811da177e4SLinus Torvalds 		return 0;
4821da177e4SLinus Torvalds 	if (status & TD_CTRL_BITSTUFF)			/* Bitstuff error */
4831da177e4SLinus Torvalds 		return -EPROTO;
4841da177e4SLinus Torvalds 	if (status & TD_CTRL_CRCTIMEO) {		/* CRC/Timeout */
4851da177e4SLinus Torvalds 		if (dir_out)
4861da177e4SLinus Torvalds 			return -EPROTO;
4871da177e4SLinus Torvalds 		else
4881da177e4SLinus Torvalds 			return -EILSEQ;
4891da177e4SLinus Torvalds 	}
4901da177e4SLinus Torvalds 	if (status & TD_CTRL_BABBLE)			/* Babble */
4911da177e4SLinus Torvalds 		return -EOVERFLOW;
4921da177e4SLinus Torvalds 	if (status & TD_CTRL_DBUFERR)			/* Buffer error */
4931da177e4SLinus Torvalds 		return -ENOSR;
4941da177e4SLinus Torvalds 	if (status & TD_CTRL_STALLED)			/* Stalled */
4951da177e4SLinus Torvalds 		return -EPIPE;
4961da177e4SLinus Torvalds 	WARN_ON(status & TD_CTRL_ACTIVE);		/* Active */
4971da177e4SLinus Torvalds 	return 0;
4981da177e4SLinus Torvalds }
4991da177e4SLinus Torvalds 
5001da177e4SLinus Torvalds /*
5011da177e4SLinus Torvalds  * Control transfers
5021da177e4SLinus Torvalds  */
503dccf4a48SAlan Stern static int uhci_submit_control(struct uhci_hcd *uhci, struct urb *urb,
504dccf4a48SAlan Stern 		struct uhci_qh *qh)
5051da177e4SLinus Torvalds {
5061da177e4SLinus Torvalds 	struct uhci_td *td;
5071da177e4SLinus Torvalds 	unsigned long destination, status;
508dccf4a48SAlan Stern 	int maxsze = le16_to_cpu(qh->hep->desc.wMaxPacketSize);
5091da177e4SLinus Torvalds 	int len = urb->transfer_buffer_length;
5101da177e4SLinus Torvalds 	dma_addr_t data = urb->transfer_dma;
511dccf4a48SAlan Stern 	__le32 *plink;
512*04538a25SAlan Stern 	struct urb_priv *urbp = urb->hcpriv;
5131da177e4SLinus Torvalds 
5141da177e4SLinus Torvalds 	/* The "pipe" thing contains the destination in bits 8--18 */
5151da177e4SLinus Torvalds 	destination = (urb->pipe & PIPE_DEVEP_MASK) | USB_PID_SETUP;
5161da177e4SLinus Torvalds 
517af0bb599SAlan Stern 	/* 3 errors, dummy TD remains inactive */
518af0bb599SAlan Stern 	status = uhci_maxerr(3);
5191da177e4SLinus Torvalds 	if (urb->dev->speed == USB_SPEED_LOW)
5201da177e4SLinus Torvalds 		status |= TD_CTRL_LS;
5211da177e4SLinus Torvalds 
5221da177e4SLinus Torvalds 	/*
5231da177e4SLinus Torvalds 	 * Build the TD for the control request setup packet
5241da177e4SLinus Torvalds 	 */
525af0bb599SAlan Stern 	td = qh->dummy_td;
526*04538a25SAlan Stern 	uhci_add_td_to_urbp(td, urbp);
527fa346568SAlan Stern 	uhci_fill_td(td, status, destination | uhci_explen(8),
5281da177e4SLinus Torvalds 			urb->setup_dma);
529dccf4a48SAlan Stern 	plink = &td->link;
530af0bb599SAlan Stern 	status |= TD_CTRL_ACTIVE;
5311da177e4SLinus Torvalds 
5321da177e4SLinus Torvalds 	/*
5331da177e4SLinus Torvalds 	 * If direction is "send", change the packet ID from SETUP (0x2D)
5341da177e4SLinus Torvalds 	 * to OUT (0xE1).  Else change it from SETUP to IN (0x69) and
5351da177e4SLinus Torvalds 	 * set Short Packet Detect (SPD) for all data packets.
5361da177e4SLinus Torvalds 	 */
5371da177e4SLinus Torvalds 	if (usb_pipeout(urb->pipe))
5381da177e4SLinus Torvalds 		destination ^= (USB_PID_SETUP ^ USB_PID_OUT);
5391da177e4SLinus Torvalds 	else {
5401da177e4SLinus Torvalds 		destination ^= (USB_PID_SETUP ^ USB_PID_IN);
5411da177e4SLinus Torvalds 		status |= TD_CTRL_SPD;
5421da177e4SLinus Torvalds 	}
5431da177e4SLinus Torvalds 
5441da177e4SLinus Torvalds 	/*
545687f5f34SAlan Stern 	 * Build the DATA TDs
5461da177e4SLinus Torvalds 	 */
5471da177e4SLinus Torvalds 	while (len > 0) {
548dccf4a48SAlan Stern 		int pktsze = min(len, maxsze);
5491da177e4SLinus Torvalds 
5502532178aSAlan Stern 		td = uhci_alloc_td(uhci);
5511da177e4SLinus Torvalds 		if (!td)
552af0bb599SAlan Stern 			goto nomem;
553dccf4a48SAlan Stern 		*plink = cpu_to_le32(td->dma_handle);
5541da177e4SLinus Torvalds 
5551da177e4SLinus Torvalds 		/* Alternate Data0/1 (start with Data1) */
5561da177e4SLinus Torvalds 		destination ^= TD_TOKEN_TOGGLE;
5571da177e4SLinus Torvalds 
558*04538a25SAlan Stern 		uhci_add_td_to_urbp(td, urbp);
559fa346568SAlan Stern 		uhci_fill_td(td, status, destination | uhci_explen(pktsze),
5601da177e4SLinus Torvalds 				data);
561dccf4a48SAlan Stern 		plink = &td->link;
5621da177e4SLinus Torvalds 
5631da177e4SLinus Torvalds 		data += pktsze;
5641da177e4SLinus Torvalds 		len -= pktsze;
5651da177e4SLinus Torvalds 	}
5661da177e4SLinus Torvalds 
5671da177e4SLinus Torvalds 	/*
5681da177e4SLinus Torvalds 	 * Build the final TD for control status
5691da177e4SLinus Torvalds 	 */
5702532178aSAlan Stern 	td = uhci_alloc_td(uhci);
5711da177e4SLinus Torvalds 	if (!td)
572af0bb599SAlan Stern 		goto nomem;
573dccf4a48SAlan Stern 	*plink = cpu_to_le32(td->dma_handle);
5741da177e4SLinus Torvalds 
5751da177e4SLinus Torvalds 	/*
5761da177e4SLinus Torvalds 	 * It's IN if the pipe is an output pipe or we're not expecting
5771da177e4SLinus Torvalds 	 * data back.
5781da177e4SLinus Torvalds 	 */
5791da177e4SLinus Torvalds 	destination &= ~TD_TOKEN_PID_MASK;
5801da177e4SLinus Torvalds 	if (usb_pipeout(urb->pipe) || !urb->transfer_buffer_length)
5811da177e4SLinus Torvalds 		destination |= USB_PID_IN;
5821da177e4SLinus Torvalds 	else
5831da177e4SLinus Torvalds 		destination |= USB_PID_OUT;
5841da177e4SLinus Torvalds 
5851da177e4SLinus Torvalds 	destination |= TD_TOKEN_TOGGLE;		/* End in Data1 */
5861da177e4SLinus Torvalds 
5871da177e4SLinus Torvalds 	status &= ~TD_CTRL_SPD;
5881da177e4SLinus Torvalds 
589*04538a25SAlan Stern 	uhci_add_td_to_urbp(td, urbp);
5901da177e4SLinus Torvalds 	uhci_fill_td(td, status | TD_CTRL_IOC,
591fa346568SAlan Stern 			destination | uhci_explen(0), 0);
592af0bb599SAlan Stern 	plink = &td->link;
593af0bb599SAlan Stern 
594af0bb599SAlan Stern 	/*
595af0bb599SAlan Stern 	 * Build the new dummy TD and activate the old one
596af0bb599SAlan Stern 	 */
597af0bb599SAlan Stern 	td = uhci_alloc_td(uhci);
598af0bb599SAlan Stern 	if (!td)
599af0bb599SAlan Stern 		goto nomem;
600af0bb599SAlan Stern 	*plink = cpu_to_le32(td->dma_handle);
601af0bb599SAlan Stern 
602af0bb599SAlan Stern 	uhci_fill_td(td, 0, USB_PID_OUT | uhci_explen(0), 0);
603af0bb599SAlan Stern 	wmb();
604af0bb599SAlan Stern 	qh->dummy_td->status |= __constant_cpu_to_le32(TD_CTRL_ACTIVE);
605af0bb599SAlan Stern 	qh->dummy_td = td;
6061da177e4SLinus Torvalds 
6071da177e4SLinus Torvalds 	/* Low-speed transfers get a different queue, and won't hog the bus.
6081da177e4SLinus Torvalds 	 * Also, some devices enumerate better without FSBR; the easiest way
6091da177e4SLinus Torvalds 	 * to do that is to put URBs on the low-speed queue while the device
610630aa3cfSAlan Stern 	 * isn't in the CONFIGURED state. */
6111da177e4SLinus Torvalds 	if (urb->dev->speed == USB_SPEED_LOW ||
612630aa3cfSAlan Stern 			urb->dev->state != USB_STATE_CONFIGURED)
613dccf4a48SAlan Stern 		qh->skel = uhci->skel_ls_control_qh;
6141da177e4SLinus Torvalds 	else {
615dccf4a48SAlan Stern 		qh->skel = uhci->skel_fs_control_qh;
6161da177e4SLinus Torvalds 		uhci_inc_fsbr(uhci, urb);
6171da177e4SLinus Torvalds 	}
61859e29ed9SAlan Stern 
61959e29ed9SAlan Stern 	urb->actual_length = -8;	/* Account for the SETUP packet */
620dccf4a48SAlan Stern 	return 0;
621af0bb599SAlan Stern 
622af0bb599SAlan Stern nomem:
623af0bb599SAlan Stern 	/* Remove the dummy TD from the td_list so it doesn't get freed */
624*04538a25SAlan Stern 	uhci_remove_td_from_urbp(qh->dummy_td);
625af0bb599SAlan Stern 	return -ENOMEM;
6261da177e4SLinus Torvalds }
6271da177e4SLinus Torvalds 
6281da177e4SLinus Torvalds /*
6291da177e4SLinus Torvalds  * Common submit for bulk and interrupt
6301da177e4SLinus Torvalds  */
631dccf4a48SAlan Stern static int uhci_submit_common(struct uhci_hcd *uhci, struct urb *urb,
632dccf4a48SAlan Stern 		struct uhci_qh *qh)
6331da177e4SLinus Torvalds {
6341da177e4SLinus Torvalds 	struct uhci_td *td;
6351da177e4SLinus Torvalds 	unsigned long destination, status;
636dccf4a48SAlan Stern 	int maxsze = le16_to_cpu(qh->hep->desc.wMaxPacketSize);
6371da177e4SLinus Torvalds 	int len = urb->transfer_buffer_length;
6381da177e4SLinus Torvalds 	dma_addr_t data = urb->transfer_dma;
639af0bb599SAlan Stern 	__le32 *plink;
640*04538a25SAlan Stern 	struct urb_priv *urbp = urb->hcpriv;
641af0bb599SAlan Stern 	unsigned int toggle;
6421da177e4SLinus Torvalds 
6431da177e4SLinus Torvalds 	if (len < 0)
6441da177e4SLinus Torvalds 		return -EINVAL;
6451da177e4SLinus Torvalds 
6461da177e4SLinus Torvalds 	/* The "pipe" thing contains the destination in bits 8--18 */
6471da177e4SLinus Torvalds 	destination = (urb->pipe & PIPE_DEVEP_MASK) | usb_packetid(urb->pipe);
648af0bb599SAlan Stern 	toggle = usb_gettoggle(urb->dev, usb_pipeendpoint(urb->pipe),
649af0bb599SAlan Stern 			 usb_pipeout(urb->pipe));
6501da177e4SLinus Torvalds 
651af0bb599SAlan Stern 	/* 3 errors, dummy TD remains inactive */
652af0bb599SAlan Stern 	status = uhci_maxerr(3);
6531da177e4SLinus Torvalds 	if (urb->dev->speed == USB_SPEED_LOW)
6541da177e4SLinus Torvalds 		status |= TD_CTRL_LS;
6551da177e4SLinus Torvalds 	if (usb_pipein(urb->pipe))
6561da177e4SLinus Torvalds 		status |= TD_CTRL_SPD;
6571da177e4SLinus Torvalds 
6581da177e4SLinus Torvalds 	/*
659687f5f34SAlan Stern 	 * Build the DATA TDs
6601da177e4SLinus Torvalds 	 */
661af0bb599SAlan Stern 	plink = NULL;
662af0bb599SAlan Stern 	td = qh->dummy_td;
6631da177e4SLinus Torvalds 	do {	/* Allow zero length packets */
6641da177e4SLinus Torvalds 		int pktsze = maxsze;
6651da177e4SLinus Torvalds 
666dccf4a48SAlan Stern 		if (len <= pktsze) {		/* The last packet */
6671da177e4SLinus Torvalds 			pktsze = len;
6681da177e4SLinus Torvalds 			if (!(urb->transfer_flags & URB_SHORT_NOT_OK))
6691da177e4SLinus Torvalds 				status &= ~TD_CTRL_SPD;
6701da177e4SLinus Torvalds 		}
6711da177e4SLinus Torvalds 
672af0bb599SAlan Stern 		if (plink) {
6732532178aSAlan Stern 			td = uhci_alloc_td(uhci);
6741da177e4SLinus Torvalds 			if (!td)
675af0bb599SAlan Stern 				goto nomem;
676dccf4a48SAlan Stern 			*plink = cpu_to_le32(td->dma_handle);
677af0bb599SAlan Stern 		}
678*04538a25SAlan Stern 		uhci_add_td_to_urbp(td, urbp);
679dccf4a48SAlan Stern 		uhci_fill_td(td, status,
680dccf4a48SAlan Stern 				destination | uhci_explen(pktsze) |
681af0bb599SAlan Stern 					(toggle << TD_TOKEN_TOGGLE_SHIFT),
6821da177e4SLinus Torvalds 				data);
683dccf4a48SAlan Stern 		plink = &td->link;
684af0bb599SAlan Stern 		status |= TD_CTRL_ACTIVE;
6851da177e4SLinus Torvalds 
6861da177e4SLinus Torvalds 		data += pktsze;
6871da177e4SLinus Torvalds 		len -= maxsze;
688af0bb599SAlan Stern 		toggle ^= 1;
6891da177e4SLinus Torvalds 	} while (len > 0);
6901da177e4SLinus Torvalds 
6911da177e4SLinus Torvalds 	/*
6921da177e4SLinus Torvalds 	 * URB_ZERO_PACKET means adding a 0-length packet, if direction
6931da177e4SLinus Torvalds 	 * is OUT and the transfer_length was an exact multiple of maxsze,
6941da177e4SLinus Torvalds 	 * hence (len = transfer_length - N * maxsze) == 0
6951da177e4SLinus Torvalds 	 * however, if transfer_length == 0, the zero packet was already
6961da177e4SLinus Torvalds 	 * prepared above.
6971da177e4SLinus Torvalds 	 */
698dccf4a48SAlan Stern 	if ((urb->transfer_flags & URB_ZERO_PACKET) &&
699dccf4a48SAlan Stern 			usb_pipeout(urb->pipe) && len == 0 &&
700dccf4a48SAlan Stern 			urb->transfer_buffer_length > 0) {
7012532178aSAlan Stern 		td = uhci_alloc_td(uhci);
7021da177e4SLinus Torvalds 		if (!td)
703af0bb599SAlan Stern 			goto nomem;
704dccf4a48SAlan Stern 		*plink = cpu_to_le32(td->dma_handle);
7051da177e4SLinus Torvalds 
706*04538a25SAlan Stern 		uhci_add_td_to_urbp(td, urbp);
707af0bb599SAlan Stern 		uhci_fill_td(td, status,
708af0bb599SAlan Stern 				destination | uhci_explen(0) |
709af0bb599SAlan Stern 					(toggle << TD_TOKEN_TOGGLE_SHIFT),
7101da177e4SLinus Torvalds 				data);
711af0bb599SAlan Stern 		plink = &td->link;
7121da177e4SLinus Torvalds 
713af0bb599SAlan Stern 		toggle ^= 1;
7141da177e4SLinus Torvalds 	}
7151da177e4SLinus Torvalds 
7161da177e4SLinus Torvalds 	/* Set the interrupt-on-completion flag on the last packet.
7171da177e4SLinus Torvalds 	 * A more-or-less typical 4 KB URB (= size of one memory page)
7181da177e4SLinus Torvalds 	 * will require about 3 ms to transfer; that's a little on the
7191da177e4SLinus Torvalds 	 * fast side but not enough to justify delaying an interrupt
7201da177e4SLinus Torvalds 	 * more than 2 or 3 URBs, so we will ignore the URB_NO_INTERRUPT
7211da177e4SLinus Torvalds 	 * flag setting. */
722dccf4a48SAlan Stern 	td->status |= __constant_cpu_to_le32(TD_CTRL_IOC);
7231da177e4SLinus Torvalds 
724af0bb599SAlan Stern 	/*
725af0bb599SAlan Stern 	 * Build the new dummy TD and activate the old one
726af0bb599SAlan Stern 	 */
727af0bb599SAlan Stern 	td = uhci_alloc_td(uhci);
728af0bb599SAlan Stern 	if (!td)
729af0bb599SAlan Stern 		goto nomem;
730af0bb599SAlan Stern 	*plink = cpu_to_le32(td->dma_handle);
731af0bb599SAlan Stern 
732af0bb599SAlan Stern 	uhci_fill_td(td, 0, USB_PID_OUT | uhci_explen(0), 0);
733af0bb599SAlan Stern 	wmb();
734af0bb599SAlan Stern 	qh->dummy_td->status |= __constant_cpu_to_le32(TD_CTRL_ACTIVE);
735af0bb599SAlan Stern 	qh->dummy_td = td;
736af0bb599SAlan Stern 
737af0bb599SAlan Stern 	usb_settoggle(urb->dev, usb_pipeendpoint(urb->pipe),
738af0bb599SAlan Stern 			usb_pipeout(urb->pipe), toggle);
739dccf4a48SAlan Stern 	return 0;
740af0bb599SAlan Stern 
741af0bb599SAlan Stern nomem:
742af0bb599SAlan Stern 	/* Remove the dummy TD from the td_list so it doesn't get freed */
743*04538a25SAlan Stern 	uhci_remove_td_from_urbp(qh->dummy_td);
744af0bb599SAlan Stern 	return -ENOMEM;
7451da177e4SLinus Torvalds }
7461da177e4SLinus Torvalds 
747dccf4a48SAlan Stern static inline int uhci_submit_bulk(struct uhci_hcd *uhci, struct urb *urb,
748dccf4a48SAlan Stern 		struct uhci_qh *qh)
7491da177e4SLinus Torvalds {
7501da177e4SLinus Torvalds 	int ret;
7511da177e4SLinus Torvalds 
7521da177e4SLinus Torvalds 	/* Can't have low-speed bulk transfers */
7531da177e4SLinus Torvalds 	if (urb->dev->speed == USB_SPEED_LOW)
7541da177e4SLinus Torvalds 		return -EINVAL;
7551da177e4SLinus Torvalds 
756dccf4a48SAlan Stern 	qh->skel = uhci->skel_bulk_qh;
757dccf4a48SAlan Stern 	ret = uhci_submit_common(uhci, urb, qh);
758dccf4a48SAlan Stern 	if (ret == 0)
7591da177e4SLinus Torvalds 		uhci_inc_fsbr(uhci, urb);
7601da177e4SLinus Torvalds 	return ret;
7611da177e4SLinus Torvalds }
7621da177e4SLinus Torvalds 
763dccf4a48SAlan Stern static inline int uhci_submit_interrupt(struct uhci_hcd *uhci, struct urb *urb,
764dccf4a48SAlan Stern 		struct uhci_qh *qh)
7651da177e4SLinus Torvalds {
766dccf4a48SAlan Stern 	/* USB 1.1 interrupt transfers only involve one packet per interval.
767dccf4a48SAlan Stern 	 * Drivers can submit URBs of any length, but longer ones will need
768dccf4a48SAlan Stern 	 * multiple intervals to complete.
7691da177e4SLinus Torvalds 	 */
770dccf4a48SAlan Stern 	qh->skel = uhci->skelqh[__interval_to_skel(urb->interval)];
771dccf4a48SAlan Stern 	return uhci_submit_common(uhci, urb, qh);
7721da177e4SLinus Torvalds }
7731da177e4SLinus Torvalds 
7741da177e4SLinus Torvalds /*
775b1869000SAlan Stern  * Fix up the data structures following a short transfer
776b1869000SAlan Stern  */
777b1869000SAlan Stern static int uhci_fixup_short_transfer(struct uhci_hcd *uhci,
77859e29ed9SAlan Stern 		struct uhci_qh *qh, struct urb_priv *urbp)
779b1869000SAlan Stern {
780b1869000SAlan Stern 	struct uhci_td *td;
78159e29ed9SAlan Stern 	struct list_head *tmp;
78259e29ed9SAlan Stern 	int ret;
783b1869000SAlan Stern 
784b1869000SAlan Stern 	td = list_entry(urbp->td_list.prev, struct uhci_td, list);
785b1869000SAlan Stern 	if (qh->type == USB_ENDPOINT_XFER_CONTROL) {
786b1869000SAlan Stern 
787b1869000SAlan Stern 		/* When a control transfer is short, we have to restart
788b1869000SAlan Stern 		 * the queue at the status stage transaction, which is
789b1869000SAlan Stern 		 * the last TD. */
79059e29ed9SAlan Stern 		WARN_ON(list_empty(&urbp->td_list));
791b1869000SAlan Stern 		qh->element = cpu_to_le32(td->dma_handle);
79259e29ed9SAlan Stern 		tmp = td->list.prev;
793b1869000SAlan Stern 		ret = -EINPROGRESS;
794b1869000SAlan Stern 
79559e29ed9SAlan Stern 	} else {
796b1869000SAlan Stern 
797b1869000SAlan Stern 		/* When a bulk/interrupt transfer is short, we have to
798b1869000SAlan Stern 		 * fix up the toggles of the following URBs on the queue
799b1869000SAlan Stern 		 * before restarting the queue at the next URB. */
80059e29ed9SAlan Stern 		qh->initial_toggle = uhci_toggle(td_token(qh->post_td)) ^ 1;
801b1869000SAlan Stern 		uhci_fixup_toggles(qh, 1);
802b1869000SAlan Stern 
80359e29ed9SAlan Stern 		if (list_empty(&urbp->td_list))
80459e29ed9SAlan Stern 			td = qh->post_td;
805b1869000SAlan Stern 		qh->element = td->link;
80659e29ed9SAlan Stern 		tmp = urbp->td_list.prev;
80759e29ed9SAlan Stern 		ret = 0;
808b1869000SAlan Stern 	}
809b1869000SAlan Stern 
81059e29ed9SAlan Stern 	/* Remove all the TDs we skipped over, from tmp back to the start */
81159e29ed9SAlan Stern 	while (tmp != &urbp->td_list) {
81259e29ed9SAlan Stern 		td = list_entry(tmp, struct uhci_td, list);
81359e29ed9SAlan Stern 		tmp = tmp->prev;
81459e29ed9SAlan Stern 
815*04538a25SAlan Stern 		uhci_remove_td_from_urbp(td);
816*04538a25SAlan Stern 		uhci_free_td(uhci, td);
81759e29ed9SAlan Stern 	}
818b1869000SAlan Stern 	return ret;
819b1869000SAlan Stern }
820b1869000SAlan Stern 
821b1869000SAlan Stern /*
822b1869000SAlan Stern  * Common result for control, bulk, and interrupt
823b1869000SAlan Stern  */
824b1869000SAlan Stern static int uhci_result_common(struct uhci_hcd *uhci, struct urb *urb)
825b1869000SAlan Stern {
826b1869000SAlan Stern 	struct urb_priv *urbp = urb->hcpriv;
827b1869000SAlan Stern 	struct uhci_qh *qh = urbp->qh;
82859e29ed9SAlan Stern 	struct uhci_td *td, *tmp;
829b1869000SAlan Stern 	unsigned status;
830b1869000SAlan Stern 	int ret = 0;
831b1869000SAlan Stern 
83259e29ed9SAlan Stern 	list_for_each_entry_safe(td, tmp, &urbp->td_list, list) {
833b1869000SAlan Stern 		unsigned int ctrlstat;
834b1869000SAlan Stern 		int len;
835b1869000SAlan Stern 
836b1869000SAlan Stern 		ctrlstat = td_status(td);
837b1869000SAlan Stern 		status = uhci_status_bits(ctrlstat);
838b1869000SAlan Stern 		if (status & TD_CTRL_ACTIVE)
839b1869000SAlan Stern 			return -EINPROGRESS;
840b1869000SAlan Stern 
841b1869000SAlan Stern 		len = uhci_actual_length(ctrlstat);
842b1869000SAlan Stern 		urb->actual_length += len;
843b1869000SAlan Stern 
844b1869000SAlan Stern 		if (status) {
845b1869000SAlan Stern 			ret = uhci_map_status(status,
846b1869000SAlan Stern 					uhci_packetout(td_token(td)));
847b1869000SAlan Stern 			if ((debug == 1 && ret != -EPIPE) || debug > 1) {
848b1869000SAlan Stern 				/* Some debugging code */
849b1869000SAlan Stern 				dev_dbg(uhci_dev(uhci),
850b1869000SAlan Stern 						"%s: failed with status %x\n",
851b1869000SAlan Stern 						__FUNCTION__, status);
852b1869000SAlan Stern 
853b1869000SAlan Stern 				if (debug > 1 && errbuf) {
854b1869000SAlan Stern 					/* Print the chain for debugging */
855b1869000SAlan Stern 					uhci_show_qh(urbp->qh, errbuf,
856b1869000SAlan Stern 							ERRBUF_LEN, 0);
857b1869000SAlan Stern 					lprintk(errbuf);
858b1869000SAlan Stern 				}
859b1869000SAlan Stern 			}
860b1869000SAlan Stern 
861b1869000SAlan Stern 		} else if (len < uhci_expected_length(td_token(td))) {
862b1869000SAlan Stern 
863b1869000SAlan Stern 			/* We received a short packet */
864b1869000SAlan Stern 			if (urb->transfer_flags & URB_SHORT_NOT_OK)
865b1869000SAlan Stern 				ret = -EREMOTEIO;
866b1869000SAlan Stern 			else if (ctrlstat & TD_CTRL_SPD)
867b1869000SAlan Stern 				ret = 1;
868b1869000SAlan Stern 		}
869b1869000SAlan Stern 
870*04538a25SAlan Stern 		uhci_remove_td_from_urbp(td);
87159e29ed9SAlan Stern 		if (qh->post_td)
872*04538a25SAlan Stern 			uhci_free_td(uhci, qh->post_td);
87359e29ed9SAlan Stern 		qh->post_td = td;
87459e29ed9SAlan Stern 
875b1869000SAlan Stern 		if (ret != 0)
876b1869000SAlan Stern 			goto err;
877b1869000SAlan Stern 	}
878b1869000SAlan Stern 	return ret;
879b1869000SAlan Stern 
880b1869000SAlan Stern err:
881b1869000SAlan Stern 	if (ret < 0) {
882b1869000SAlan Stern 		/* In case a control transfer gets an error
883b1869000SAlan Stern 		 * during the setup stage */
884b1869000SAlan Stern 		urb->actual_length = max(urb->actual_length, 0);
885b1869000SAlan Stern 
886b1869000SAlan Stern 		/* Note that the queue has stopped and save
887b1869000SAlan Stern 		 * the next toggle value */
888b1869000SAlan Stern 		qh->element = UHCI_PTR_TERM;
889b1869000SAlan Stern 		qh->is_stopped = 1;
890b1869000SAlan Stern 		qh->needs_fixup = (qh->type != USB_ENDPOINT_XFER_CONTROL);
891b1869000SAlan Stern 		qh->initial_toggle = uhci_toggle(td_token(td)) ^
892b1869000SAlan Stern 				(ret == -EREMOTEIO);
893b1869000SAlan Stern 
894b1869000SAlan Stern 	} else		/* Short packet received */
89559e29ed9SAlan Stern 		ret = uhci_fixup_short_transfer(uhci, qh, urbp);
896b1869000SAlan Stern 	return ret;
897b1869000SAlan Stern }
898b1869000SAlan Stern 
899b1869000SAlan Stern /*
9001da177e4SLinus Torvalds  * Isochronous transfers
9011da177e4SLinus Torvalds  */
902dccf4a48SAlan Stern static int uhci_submit_isochronous(struct uhci_hcd *uhci, struct urb *urb,
903dccf4a48SAlan Stern 		struct uhci_qh *qh)
9041da177e4SLinus Torvalds {
905dccf4a48SAlan Stern 	struct uhci_td *td = NULL;	/* Since urb->number_of_packets > 0 */
9060ed8fee1SAlan Stern 	int i, frame;
907dccf4a48SAlan Stern 	unsigned long destination, status;
908b81d3436SAlan Stern 	struct urb_priv *urbp = (struct urb_priv *) urb->hcpriv;
9091da177e4SLinus Torvalds 
9100ed8fee1SAlan Stern 	if (urb->number_of_packets > 900)	/* 900? Why? */
9110ed8fee1SAlan Stern 		return -EFBIG;
9120ed8fee1SAlan Stern 
9131da177e4SLinus Torvalds 	status = TD_CTRL_ACTIVE | TD_CTRL_IOS;
9141da177e4SLinus Torvalds 	destination = (urb->pipe & PIPE_DEVEP_MASK) | usb_packetid(urb->pipe);
9151da177e4SLinus Torvalds 
9160ed8fee1SAlan Stern 	/* Figure out the starting frame number */
9170ed8fee1SAlan Stern 	if (urb->transfer_flags & URB_ISO_ASAP) {
9180ed8fee1SAlan Stern 		if (list_empty(&qh->queue)) {
9190ed8fee1SAlan Stern 			uhci_get_current_frame_number(uhci);
9200ed8fee1SAlan Stern 			urb->start_frame = (uhci->frame_number + 10);
9210ed8fee1SAlan Stern 
9220ed8fee1SAlan Stern 		} else {		/* Go right after the last one */
9230ed8fee1SAlan Stern 			struct urb *last_urb;
9240ed8fee1SAlan Stern 
9250ed8fee1SAlan Stern 			last_urb = list_entry(qh->queue.prev,
9260ed8fee1SAlan Stern 					struct urb_priv, node)->urb;
9270ed8fee1SAlan Stern 			urb->start_frame = (last_urb->start_frame +
9280ed8fee1SAlan Stern 					last_urb->number_of_packets *
9290ed8fee1SAlan Stern 					last_urb->interval);
9300ed8fee1SAlan Stern 		}
9310ed8fee1SAlan Stern 	} else {
9320ed8fee1SAlan Stern 		/* FIXME: Sanity check */
9330ed8fee1SAlan Stern 	}
9340ed8fee1SAlan Stern 	urb->start_frame &= (UHCI_NUMFRAMES - 1);
9351da177e4SLinus Torvalds 
936b81d3436SAlan Stern 	for (i = 0; i < urb->number_of_packets; i++) {
9372532178aSAlan Stern 		td = uhci_alloc_td(uhci);
9381da177e4SLinus Torvalds 		if (!td)
9391da177e4SLinus Torvalds 			return -ENOMEM;
9401da177e4SLinus Torvalds 
941*04538a25SAlan Stern 		uhci_add_td_to_urbp(td, urbp);
942dccf4a48SAlan Stern 		uhci_fill_td(td, status, destination |
943dccf4a48SAlan Stern 				uhci_explen(urb->iso_frame_desc[i].length),
944dccf4a48SAlan Stern 				urb->transfer_dma +
945dccf4a48SAlan Stern 					urb->iso_frame_desc[i].offset);
946b81d3436SAlan Stern 	}
9471da177e4SLinus Torvalds 
948dccf4a48SAlan Stern 	/* Set the interrupt-on-completion flag on the last packet. */
949dccf4a48SAlan Stern 	td->status |= __constant_cpu_to_le32(TD_CTRL_IOC);
950dccf4a48SAlan Stern 
951dccf4a48SAlan Stern 	qh->skel = uhci->skel_iso_qh;
952dccf4a48SAlan Stern 
953dccf4a48SAlan Stern 	/* Add the TDs to the frame list */
954b81d3436SAlan Stern 	frame = urb->start_frame;
955b81d3436SAlan Stern 	list_for_each_entry(td, &urbp->td_list, list) {
956dccf4a48SAlan Stern 		uhci_insert_td_in_frame_list(uhci, td, frame);
957b81d3436SAlan Stern 		frame += urb->interval;
9581da177e4SLinus Torvalds 	}
9591da177e4SLinus Torvalds 
960dccf4a48SAlan Stern 	return 0;
9611da177e4SLinus Torvalds }
9621da177e4SLinus Torvalds 
9631da177e4SLinus Torvalds static int uhci_result_isochronous(struct uhci_hcd *uhci, struct urb *urb)
9641da177e4SLinus Torvalds {
9651da177e4SLinus Torvalds 	struct uhci_td *td;
9661da177e4SLinus Torvalds 	struct urb_priv *urbp = (struct urb_priv *)urb->hcpriv;
9671da177e4SLinus Torvalds 	int status;
9681da177e4SLinus Torvalds 	int i, ret = 0;
9691da177e4SLinus Torvalds 
970b81d3436SAlan Stern 	urb->actual_length = urb->error_count = 0;
9711da177e4SLinus Torvalds 
9721da177e4SLinus Torvalds 	i = 0;
9731da177e4SLinus Torvalds 	list_for_each_entry(td, &urbp->td_list, list) {
9741da177e4SLinus Torvalds 		int actlength;
9751da177e4SLinus Torvalds 		unsigned int ctrlstat = td_status(td);
9761da177e4SLinus Torvalds 
9771da177e4SLinus Torvalds 		if (ctrlstat & TD_CTRL_ACTIVE)
9781da177e4SLinus Torvalds 			return -EINPROGRESS;
9791da177e4SLinus Torvalds 
9801da177e4SLinus Torvalds 		actlength = uhci_actual_length(ctrlstat);
9811da177e4SLinus Torvalds 		urb->iso_frame_desc[i].actual_length = actlength;
9821da177e4SLinus Torvalds 		urb->actual_length += actlength;
9831da177e4SLinus Torvalds 
9841da177e4SLinus Torvalds 		status = uhci_map_status(uhci_status_bits(ctrlstat),
9851da177e4SLinus Torvalds 				usb_pipeout(urb->pipe));
9861da177e4SLinus Torvalds 		urb->iso_frame_desc[i].status = status;
9871da177e4SLinus Torvalds 		if (status) {
9881da177e4SLinus Torvalds 			urb->error_count++;
9891da177e4SLinus Torvalds 			ret = status;
9901da177e4SLinus Torvalds 		}
9911da177e4SLinus Torvalds 
9921da177e4SLinus Torvalds 		i++;
9931da177e4SLinus Torvalds 	}
9941da177e4SLinus Torvalds 
9951da177e4SLinus Torvalds 	return ret;
9961da177e4SLinus Torvalds }
9971da177e4SLinus Torvalds 
9981da177e4SLinus Torvalds static int uhci_urb_enqueue(struct usb_hcd *hcd,
999dccf4a48SAlan Stern 		struct usb_host_endpoint *hep,
100055016f10SAl Viro 		struct urb *urb, gfp_t mem_flags)
10011da177e4SLinus Torvalds {
10021da177e4SLinus Torvalds 	int ret;
10031da177e4SLinus Torvalds 	struct uhci_hcd *uhci = hcd_to_uhci(hcd);
10041da177e4SLinus Torvalds 	unsigned long flags;
1005dccf4a48SAlan Stern 	struct urb_priv *urbp;
1006dccf4a48SAlan Stern 	struct uhci_qh *qh;
10071da177e4SLinus Torvalds 	int bustime;
10081da177e4SLinus Torvalds 
10091da177e4SLinus Torvalds 	spin_lock_irqsave(&uhci->lock, flags);
10101da177e4SLinus Torvalds 
10111da177e4SLinus Torvalds 	ret = urb->status;
10121da177e4SLinus Torvalds 	if (ret != -EINPROGRESS)		/* URB already unlinked! */
1013dccf4a48SAlan Stern 		goto done;
10141da177e4SLinus Torvalds 
10151da177e4SLinus Torvalds 	ret = -ENOMEM;
1016dccf4a48SAlan Stern 	urbp = uhci_alloc_urb_priv(uhci, urb);
1017dccf4a48SAlan Stern 	if (!urbp)
1018dccf4a48SAlan Stern 		goto done;
1019dccf4a48SAlan Stern 
1020dccf4a48SAlan Stern 	if (hep->hcpriv)
1021dccf4a48SAlan Stern 		qh = (struct uhci_qh *) hep->hcpriv;
1022dccf4a48SAlan Stern 	else {
1023dccf4a48SAlan Stern 		qh = uhci_alloc_qh(uhci, urb->dev, hep);
1024dccf4a48SAlan Stern 		if (!qh)
1025dccf4a48SAlan Stern 			goto err_no_qh;
10261da177e4SLinus Torvalds 	}
1027dccf4a48SAlan Stern 	urbp->qh = qh;
10281da177e4SLinus Torvalds 
10294de7d2c2SAlan Stern 	switch (qh->type) {
10304de7d2c2SAlan Stern 	case USB_ENDPOINT_XFER_CONTROL:
1031dccf4a48SAlan Stern 		ret = uhci_submit_control(uhci, urb, qh);
1032dccf4a48SAlan Stern 		break;
10334de7d2c2SAlan Stern 	case USB_ENDPOINT_XFER_BULK:
1034dccf4a48SAlan Stern 		ret = uhci_submit_bulk(uhci, urb, qh);
10351da177e4SLinus Torvalds 		break;
10364de7d2c2SAlan Stern 	case USB_ENDPOINT_XFER_INT:
1037dccf4a48SAlan Stern 		if (list_empty(&qh->queue)) {
10381da177e4SLinus Torvalds 			bustime = usb_check_bandwidth(urb->dev, urb);
10391da177e4SLinus Torvalds 			if (bustime < 0)
10401da177e4SLinus Torvalds 				ret = bustime;
10411da177e4SLinus Torvalds 			else {
1042dccf4a48SAlan Stern 				ret = uhci_submit_interrupt(uhci, urb, qh);
1043dccf4a48SAlan Stern 				if (ret == 0)
10441da177e4SLinus Torvalds 					usb_claim_bandwidth(urb->dev, urb, bustime, 0);
10451da177e4SLinus Torvalds 			}
10461da177e4SLinus Torvalds 		} else {	/* inherit from parent */
1047dccf4a48SAlan Stern 			struct urb_priv *eurbp;
1048dccf4a48SAlan Stern 
1049dccf4a48SAlan Stern 			eurbp = list_entry(qh->queue.prev, struct urb_priv,
1050dccf4a48SAlan Stern 					node);
1051dccf4a48SAlan Stern 			urb->bandwidth = eurbp->urb->bandwidth;
1052dccf4a48SAlan Stern 			ret = uhci_submit_interrupt(uhci, urb, qh);
10531da177e4SLinus Torvalds 		}
10541da177e4SLinus Torvalds 		break;
10554de7d2c2SAlan Stern 	case USB_ENDPOINT_XFER_ISOC:
10561da177e4SLinus Torvalds 		bustime = usb_check_bandwidth(urb->dev, urb);
10571da177e4SLinus Torvalds 		if (bustime < 0) {
10581da177e4SLinus Torvalds 			ret = bustime;
10591da177e4SLinus Torvalds 			break;
10601da177e4SLinus Torvalds 		}
10611da177e4SLinus Torvalds 
1062dccf4a48SAlan Stern 		ret = uhci_submit_isochronous(uhci, urb, qh);
1063dccf4a48SAlan Stern 		if (ret == 0)
10641da177e4SLinus Torvalds 			usb_claim_bandwidth(urb->dev, urb, bustime, 1);
10651da177e4SLinus Torvalds 		break;
10661da177e4SLinus Torvalds 	}
1067dccf4a48SAlan Stern 	if (ret != 0)
1068dccf4a48SAlan Stern 		goto err_submit_failed;
10691da177e4SLinus Torvalds 
1070dccf4a48SAlan Stern 	/* Add this URB to the QH */
1071dccf4a48SAlan Stern 	urbp->qh = qh;
1072dccf4a48SAlan Stern 	list_add_tail(&urbp->node, &qh->queue);
10731da177e4SLinus Torvalds 
1074dccf4a48SAlan Stern 	/* If the new URB is the first and only one on this QH then either
1075dccf4a48SAlan Stern 	 * the QH is new and idle or else it's unlinked and waiting to
10762775562aSAlan Stern 	 * become idle, so we can activate it right away.  But only if the
10772775562aSAlan Stern 	 * queue isn't stopped. */
10782775562aSAlan Stern 	if (qh->queue.next == &urbp->node && !qh->is_stopped)
1079dccf4a48SAlan Stern 		uhci_activate_qh(uhci, qh);
1080dccf4a48SAlan Stern 	goto done;
1081dccf4a48SAlan Stern 
1082dccf4a48SAlan Stern err_submit_failed:
1083dccf4a48SAlan Stern 	if (qh->state == QH_STATE_IDLE)
1084dccf4a48SAlan Stern 		uhci_make_qh_idle(uhci, qh);	/* Reclaim unused QH */
1085dccf4a48SAlan Stern 
1086dccf4a48SAlan Stern err_no_qh:
1087dccf4a48SAlan Stern 	uhci_free_urb_priv(uhci, urbp);
1088dccf4a48SAlan Stern 
1089dccf4a48SAlan Stern done:
10901da177e4SLinus Torvalds 	spin_unlock_irqrestore(&uhci->lock, flags);
10911da177e4SLinus Torvalds 	return ret;
10921da177e4SLinus Torvalds }
10931da177e4SLinus Torvalds 
10941da177e4SLinus Torvalds static int uhci_urb_dequeue(struct usb_hcd *hcd, struct urb *urb)
10951da177e4SLinus Torvalds {
10961da177e4SLinus Torvalds 	struct uhci_hcd *uhci = hcd_to_uhci(hcd);
10971da177e4SLinus Torvalds 	unsigned long flags;
10981da177e4SLinus Torvalds 	struct urb_priv *urbp;
10991da177e4SLinus Torvalds 
11001da177e4SLinus Torvalds 	spin_lock_irqsave(&uhci->lock, flags);
11011da177e4SLinus Torvalds 	urbp = urb->hcpriv;
11021da177e4SLinus Torvalds 	if (!urbp)			/* URB was never linked! */
11031da177e4SLinus Torvalds 		goto done;
11041da177e4SLinus Torvalds 
1105dccf4a48SAlan Stern 	/* Remove Isochronous TDs from the frame list ASAP */
11064de7d2c2SAlan Stern 	if (urbp->qh->type == USB_ENDPOINT_XFER_ISOC)
1107dccf4a48SAlan Stern 		uhci_unlink_isochronous_tds(uhci, urb);
1108dccf4a48SAlan Stern 	uhci_unlink_qh(uhci, urbp->qh);
11091da177e4SLinus Torvalds 
11101da177e4SLinus Torvalds done:
11111da177e4SLinus Torvalds 	spin_unlock_irqrestore(&uhci->lock, flags);
11121da177e4SLinus Torvalds 	return 0;
11131da177e4SLinus Torvalds }
11141da177e4SLinus Torvalds 
11150ed8fee1SAlan Stern /*
11160ed8fee1SAlan Stern  * Finish unlinking an URB and give it back
11170ed8fee1SAlan Stern  */
11180ed8fee1SAlan Stern static void uhci_giveback_urb(struct uhci_hcd *uhci, struct uhci_qh *qh,
11190ed8fee1SAlan Stern 		struct urb *urb, struct pt_regs *regs)
11200ed8fee1SAlan Stern __releases(uhci->lock)
11210ed8fee1SAlan Stern __acquires(uhci->lock)
11221da177e4SLinus Torvalds {
11231da177e4SLinus Torvalds 	struct urb_priv *urbp = (struct urb_priv *) urb->hcpriv;
11241da177e4SLinus Torvalds 
11250ed8fee1SAlan Stern 	/* Isochronous TDs get unlinked directly from the frame list */
11264de7d2c2SAlan Stern 	if (qh->type == USB_ENDPOINT_XFER_ISOC)
11270ed8fee1SAlan Stern 		uhci_unlink_isochronous_tds(uhci, urb);
11281da177e4SLinus Torvalds 
11290ed8fee1SAlan Stern 	/* Take the URB off the QH's queue.  If the queue is now empty,
11300ed8fee1SAlan Stern 	 * this is a perfect time for a toggle fixup. */
11310ed8fee1SAlan Stern 	list_del_init(&urbp->node);
11320ed8fee1SAlan Stern 	if (list_empty(&qh->queue) && qh->needs_fixup) {
11330ed8fee1SAlan Stern 		usb_settoggle(urb->dev, usb_pipeendpoint(urb->pipe),
11340ed8fee1SAlan Stern 				usb_pipeout(urb->pipe), qh->initial_toggle);
11350ed8fee1SAlan Stern 		qh->needs_fixup = 0;
11360ed8fee1SAlan Stern 	}
11370ed8fee1SAlan Stern 
11380ed8fee1SAlan Stern 	uhci_dec_fsbr(uhci, urb);	/* Safe since it checks */
11390ed8fee1SAlan Stern 	uhci_free_urb_priv(uhci, urbp);
11400ed8fee1SAlan Stern 
11414de7d2c2SAlan Stern 	switch (qh->type) {
11424de7d2c2SAlan Stern 	case USB_ENDPOINT_XFER_ISOC:
11430ed8fee1SAlan Stern 		/* Release bandwidth for Interrupt or Isoc. transfers */
11440ed8fee1SAlan Stern 		if (urb->bandwidth)
11450ed8fee1SAlan Stern 			usb_release_bandwidth(urb->dev, urb, 1);
11460ed8fee1SAlan Stern 		break;
11474de7d2c2SAlan Stern 	case USB_ENDPOINT_XFER_INT:
11480ed8fee1SAlan Stern 		/* Release bandwidth for Interrupt or Isoc. transfers */
11490ed8fee1SAlan Stern 		/* Make sure we don't release if we have a queued URB */
11500ed8fee1SAlan Stern 		if (list_empty(&qh->queue) && urb->bandwidth)
11510ed8fee1SAlan Stern 			usb_release_bandwidth(urb->dev, urb, 0);
11520ed8fee1SAlan Stern 		else
11530ed8fee1SAlan Stern 			/* bandwidth was passed on to queued URB, */
11540ed8fee1SAlan Stern 			/* so don't let usb_unlink_urb() release it */
11550ed8fee1SAlan Stern 			urb->bandwidth = 0;
11560ed8fee1SAlan Stern 		break;
11570ed8fee1SAlan Stern 	}
11580ed8fee1SAlan Stern 
11590ed8fee1SAlan Stern 	spin_unlock(&uhci->lock);
11600ed8fee1SAlan Stern 	usb_hcd_giveback_urb(uhci_to_hcd(uhci), urb, regs);
11610ed8fee1SAlan Stern 	spin_lock(&uhci->lock);
11620ed8fee1SAlan Stern 
11630ed8fee1SAlan Stern 	/* If the queue is now empty, we can unlink the QH and give up its
11640ed8fee1SAlan Stern 	 * reserved bandwidth. */
11650ed8fee1SAlan Stern 	if (list_empty(&qh->queue)) {
11660ed8fee1SAlan Stern 		uhci_unlink_qh(uhci, qh);
11670ed8fee1SAlan Stern 
11680ed8fee1SAlan Stern 		/* Bandwidth stuff not yet implemented */
11690ed8fee1SAlan Stern 	}
11700ed8fee1SAlan Stern }
11710ed8fee1SAlan Stern 
11720ed8fee1SAlan Stern /*
11730ed8fee1SAlan Stern  * Scan the URBs in a QH's queue
11740ed8fee1SAlan Stern  */
11750ed8fee1SAlan Stern #define QH_FINISHED_UNLINKING(qh)			\
11760ed8fee1SAlan Stern 		(qh->state == QH_STATE_UNLINKING &&	\
11770ed8fee1SAlan Stern 		uhci->frame_number + uhci->is_stopped != qh->unlink_frame)
11780ed8fee1SAlan Stern 
11790ed8fee1SAlan Stern static void uhci_scan_qh(struct uhci_hcd *uhci, struct uhci_qh *qh,
11800ed8fee1SAlan Stern 		struct pt_regs *regs)
11810ed8fee1SAlan Stern {
11820ed8fee1SAlan Stern 	struct urb_priv *urbp;
11830ed8fee1SAlan Stern 	struct urb *urb;
11840ed8fee1SAlan Stern 	int status;
11850ed8fee1SAlan Stern 
11860ed8fee1SAlan Stern 	while (!list_empty(&qh->queue)) {
11870ed8fee1SAlan Stern 		urbp = list_entry(qh->queue.next, struct urb_priv, node);
11880ed8fee1SAlan Stern 		urb = urbp->urb;
11890ed8fee1SAlan Stern 
1190b1869000SAlan Stern 		if (qh->type == USB_ENDPOINT_XFER_ISOC)
11910ed8fee1SAlan Stern 			status = uhci_result_isochronous(uhci, urb);
1192b1869000SAlan Stern 		else
11930ed8fee1SAlan Stern 			status = uhci_result_common(uhci, urb);
11940ed8fee1SAlan Stern 		if (status == -EINPROGRESS)
11950ed8fee1SAlan Stern 			break;
11960ed8fee1SAlan Stern 
11970ed8fee1SAlan Stern 		spin_lock(&urb->lock);
11980ed8fee1SAlan Stern 		if (urb->status == -EINPROGRESS)	/* Not dequeued */
11990ed8fee1SAlan Stern 			urb->status = status;
12000ed8fee1SAlan Stern 		else
12012775562aSAlan Stern 			status = ECONNRESET;		/* Not -ECONNRESET */
12020ed8fee1SAlan Stern 		spin_unlock(&urb->lock);
12030ed8fee1SAlan Stern 
12040ed8fee1SAlan Stern 		/* Dequeued but completed URBs can't be given back unless
12050ed8fee1SAlan Stern 		 * the QH is stopped or has finished unlinking. */
12062775562aSAlan Stern 		if (status == ECONNRESET) {
12072775562aSAlan Stern 			if (QH_FINISHED_UNLINKING(qh))
12082775562aSAlan Stern 				qh->is_stopped = 1;
12092775562aSAlan Stern 			else if (!qh->is_stopped)
12100ed8fee1SAlan Stern 				return;
12112775562aSAlan Stern 		}
12120ed8fee1SAlan Stern 
12130ed8fee1SAlan Stern 		uhci_giveback_urb(uhci, qh, urb, regs);
12142775562aSAlan Stern 		if (status < 0)
12150ed8fee1SAlan Stern 			break;
12160ed8fee1SAlan Stern 	}
12170ed8fee1SAlan Stern 
12180ed8fee1SAlan Stern 	/* If the QH is neither stopped nor finished unlinking (normal case),
12190ed8fee1SAlan Stern 	 * our work here is done. */
12202775562aSAlan Stern 	if (QH_FINISHED_UNLINKING(qh))
12212775562aSAlan Stern 		qh->is_stopped = 1;
12222775562aSAlan Stern 	else if (!qh->is_stopped)
12230ed8fee1SAlan Stern 		return;
12240ed8fee1SAlan Stern 
12250ed8fee1SAlan Stern 	/* Otherwise give back each of the dequeued URBs */
12262775562aSAlan Stern restart:
12270ed8fee1SAlan Stern 	list_for_each_entry(urbp, &qh->queue, node) {
12280ed8fee1SAlan Stern 		urb = urbp->urb;
12290ed8fee1SAlan Stern 		if (urb->status != -EINPROGRESS) {
1230a0b458b6SAlan Stern 			uhci_cleanup_queue(qh, urb);
12310ed8fee1SAlan Stern 			uhci_giveback_urb(uhci, qh, urb, regs);
12320ed8fee1SAlan Stern 			goto restart;
12330ed8fee1SAlan Stern 		}
12340ed8fee1SAlan Stern 	}
12350ed8fee1SAlan Stern 	qh->is_stopped = 0;
12360ed8fee1SAlan Stern 
12370ed8fee1SAlan Stern 	/* There are no more dequeued URBs.  If there are still URBs on the
12380ed8fee1SAlan Stern 	 * queue, the QH can now be re-activated. */
12390ed8fee1SAlan Stern 	if (!list_empty(&qh->queue)) {
12400ed8fee1SAlan Stern 		if (qh->needs_fixup)
12410ed8fee1SAlan Stern 			uhci_fixup_toggles(qh, 0);
12420ed8fee1SAlan Stern 		uhci_activate_qh(uhci, qh);
12430ed8fee1SAlan Stern 	}
12440ed8fee1SAlan Stern 
12450ed8fee1SAlan Stern 	/* The queue is empty.  The QH can become idle if it is fully
12460ed8fee1SAlan Stern 	 * unlinked. */
12470ed8fee1SAlan Stern 	else if (QH_FINISHED_UNLINKING(qh))
12480ed8fee1SAlan Stern 		uhci_make_qh_idle(uhci, qh);
12491da177e4SLinus Torvalds }
12501da177e4SLinus Torvalds 
12510ed8fee1SAlan Stern /*
12520ed8fee1SAlan Stern  * Process events in the schedule, but only in one thread at a time
12530ed8fee1SAlan Stern  */
12541da177e4SLinus Torvalds static void uhci_scan_schedule(struct uhci_hcd *uhci, struct pt_regs *regs)
12551da177e4SLinus Torvalds {
12560ed8fee1SAlan Stern 	int i;
12570ed8fee1SAlan Stern 	struct uhci_qh *qh;
12581da177e4SLinus Torvalds 
12591da177e4SLinus Torvalds 	/* Don't allow re-entrant calls */
12601da177e4SLinus Torvalds 	if (uhci->scan_in_progress) {
12611da177e4SLinus Torvalds 		uhci->need_rescan = 1;
12621da177e4SLinus Torvalds 		return;
12631da177e4SLinus Torvalds 	}
12641da177e4SLinus Torvalds 	uhci->scan_in_progress = 1;
12651da177e4SLinus Torvalds  rescan:
12661da177e4SLinus Torvalds 	uhci->need_rescan = 0;
12671da177e4SLinus Torvalds 
12686c1b445cSAlan Stern 	uhci_clear_next_interrupt(uhci);
12691da177e4SLinus Torvalds 	uhci_get_current_frame_number(uhci);
12701da177e4SLinus Torvalds 
12710ed8fee1SAlan Stern 	/* Go through all the QH queues and process the URBs in each one */
12720ed8fee1SAlan Stern 	for (i = 0; i < UHCI_NUM_SKELQH - 1; ++i) {
12730ed8fee1SAlan Stern 		uhci->next_qh = list_entry(uhci->skelqh[i]->node.next,
12740ed8fee1SAlan Stern 				struct uhci_qh, node);
12750ed8fee1SAlan Stern 		while ((qh = uhci->next_qh) != uhci->skelqh[i]) {
12760ed8fee1SAlan Stern 			uhci->next_qh = list_entry(qh->node.next,
12770ed8fee1SAlan Stern 					struct uhci_qh, node);
12780ed8fee1SAlan Stern 			uhci_scan_qh(uhci, qh, regs);
12791da177e4SLinus Torvalds 		}
12800ed8fee1SAlan Stern 	}
12811da177e4SLinus Torvalds 
12821da177e4SLinus Torvalds 	if (uhci->need_rescan)
12831da177e4SLinus Torvalds 		goto rescan;
12841da177e4SLinus Torvalds 	uhci->scan_in_progress = 0;
12851da177e4SLinus Torvalds 
1286*04538a25SAlan Stern 	if (list_empty(&uhci->skel_unlink_qh->node))
12871da177e4SLinus Torvalds 		uhci_clear_next_interrupt(uhci);
12881da177e4SLinus Torvalds 	else
12891da177e4SLinus Torvalds 		uhci_set_next_interrupt(uhci);
12901da177e4SLinus Torvalds }
1291f5946f82SAlan Stern 
1292f5946f82SAlan Stern static void check_fsbr(struct uhci_hcd *uhci)
1293f5946f82SAlan Stern {
12940ed8fee1SAlan Stern 	/* For now, don't scan URBs for FSBR timeouts.
12950ed8fee1SAlan Stern 	 * Add it back in later... */
1296f5946f82SAlan Stern 
1297f5946f82SAlan Stern 	/* Really disable FSBR */
1298f5946f82SAlan Stern 	if (!uhci->fsbr && uhci->fsbrtimeout && time_after_eq(jiffies, uhci->fsbrtimeout)) {
1299f5946f82SAlan Stern 		uhci->fsbrtimeout = 0;
1300f5946f82SAlan Stern 		uhci->skel_term_qh->link = UHCI_PTR_TERM;
1301f5946f82SAlan Stern 	}
1302f5946f82SAlan Stern }
1303