xref: /openbmc/linux/drivers/usb/host/uhci-q.c (revision 59e29ed91cff90b27d393c7a3d3ac9c3fcaea7dd)
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 static void uhci_free_pending_tds(struct uhci_hcd *uhci);
201da177e4SLinus Torvalds 
211da177e4SLinus Torvalds /*
221da177e4SLinus Torvalds  * Technically, updating td->status here is a race, but it's not really a
231da177e4SLinus Torvalds  * problem. The worst that can happen is that we set the IOC bit again
241da177e4SLinus Torvalds  * generating a spurious interrupt. We could fix this by creating another
251da177e4SLinus Torvalds  * QH and leaving the IOC bit always set, but then we would have to play
261da177e4SLinus Torvalds  * games with the FSBR code to make sure we get the correct order in all
271da177e4SLinus Torvalds  * the cases. I don't think it's worth the effort
281da177e4SLinus Torvalds  */
29dccf4a48SAlan Stern static void uhci_set_next_interrupt(struct uhci_hcd *uhci)
301da177e4SLinus Torvalds {
316c1b445cSAlan Stern 	if (uhci->is_stopped)
321f09df8bSAlan Stern 		mod_timer(&uhci_to_hcd(uhci)->rh_timer, jiffies);
331da177e4SLinus Torvalds 	uhci->term_td->status |= cpu_to_le32(TD_CTRL_IOC);
341da177e4SLinus Torvalds }
351da177e4SLinus Torvalds 
361da177e4SLinus Torvalds static inline void uhci_clear_next_interrupt(struct uhci_hcd *uhci)
371da177e4SLinus Torvalds {
381da177e4SLinus Torvalds 	uhci->term_td->status &= ~cpu_to_le32(TD_CTRL_IOC);
391da177e4SLinus Torvalds }
401da177e4SLinus Torvalds 
412532178aSAlan Stern static struct uhci_td *uhci_alloc_td(struct uhci_hcd *uhci)
421da177e4SLinus Torvalds {
431da177e4SLinus Torvalds 	dma_addr_t dma_handle;
441da177e4SLinus Torvalds 	struct uhci_td *td;
451da177e4SLinus Torvalds 
461da177e4SLinus Torvalds 	td = dma_pool_alloc(uhci->td_pool, GFP_ATOMIC, &dma_handle);
471da177e4SLinus Torvalds 	if (!td)
481da177e4SLinus Torvalds 		return NULL;
491da177e4SLinus Torvalds 
501da177e4SLinus Torvalds 	td->dma_handle = dma_handle;
511da177e4SLinus Torvalds 	td->frame = -1;
521da177e4SLinus Torvalds 
531da177e4SLinus Torvalds 	INIT_LIST_HEAD(&td->list);
541da177e4SLinus Torvalds 	INIT_LIST_HEAD(&td->remove_list);
551da177e4SLinus Torvalds 	INIT_LIST_HEAD(&td->fl_list);
561da177e4SLinus Torvalds 
571da177e4SLinus Torvalds 	return td;
581da177e4SLinus Torvalds }
591da177e4SLinus Torvalds 
60dccf4a48SAlan Stern static void uhci_free_td(struct uhci_hcd *uhci, struct uhci_td *td)
61dccf4a48SAlan Stern {
62dccf4a48SAlan Stern 	if (!list_empty(&td->list))
63dccf4a48SAlan Stern 		dev_warn(uhci_dev(uhci), "td %p still in list!\n", td);
64dccf4a48SAlan Stern 	if (!list_empty(&td->remove_list))
65dccf4a48SAlan Stern 		dev_warn(uhci_dev(uhci), "td %p still in remove_list!\n", td);
66dccf4a48SAlan Stern 	if (!list_empty(&td->fl_list))
67dccf4a48SAlan Stern 		dev_warn(uhci_dev(uhci), "td %p still in fl_list!\n", td);
68dccf4a48SAlan Stern 
69dccf4a48SAlan Stern 	dma_pool_free(uhci->td_pool, td, td->dma_handle);
70dccf4a48SAlan Stern }
71dccf4a48SAlan Stern 
721da177e4SLinus Torvalds static inline void uhci_fill_td(struct uhci_td *td, u32 status,
731da177e4SLinus Torvalds 		u32 token, u32 buffer)
741da177e4SLinus Torvalds {
751da177e4SLinus Torvalds 	td->status = cpu_to_le32(status);
761da177e4SLinus Torvalds 	td->token = cpu_to_le32(token);
771da177e4SLinus Torvalds 	td->buffer = cpu_to_le32(buffer);
781da177e4SLinus Torvalds }
791da177e4SLinus Torvalds 
801da177e4SLinus Torvalds /*
81687f5f34SAlan Stern  * We insert Isochronous URBs directly into the frame list at the beginning
821da177e4SLinus Torvalds  */
83dccf4a48SAlan Stern static inline void uhci_insert_td_in_frame_list(struct uhci_hcd *uhci,
84dccf4a48SAlan Stern 		struct uhci_td *td, unsigned framenum)
851da177e4SLinus Torvalds {
861da177e4SLinus Torvalds 	framenum &= (UHCI_NUMFRAMES - 1);
871da177e4SLinus Torvalds 
881da177e4SLinus Torvalds 	td->frame = framenum;
891da177e4SLinus Torvalds 
901da177e4SLinus Torvalds 	/* Is there a TD already mapped there? */
91a1d59ce8SAlan Stern 	if (uhci->frame_cpu[framenum]) {
921da177e4SLinus Torvalds 		struct uhci_td *ftd, *ltd;
931da177e4SLinus Torvalds 
94a1d59ce8SAlan Stern 		ftd = uhci->frame_cpu[framenum];
951da177e4SLinus Torvalds 		ltd = list_entry(ftd->fl_list.prev, struct uhci_td, fl_list);
961da177e4SLinus Torvalds 
971da177e4SLinus Torvalds 		list_add_tail(&td->fl_list, &ftd->fl_list);
981da177e4SLinus Torvalds 
991da177e4SLinus Torvalds 		td->link = ltd->link;
1001da177e4SLinus Torvalds 		wmb();
1011da177e4SLinus Torvalds 		ltd->link = cpu_to_le32(td->dma_handle);
1021da177e4SLinus Torvalds 	} else {
103a1d59ce8SAlan Stern 		td->link = uhci->frame[framenum];
1041da177e4SLinus Torvalds 		wmb();
105a1d59ce8SAlan Stern 		uhci->frame[framenum] = cpu_to_le32(td->dma_handle);
106a1d59ce8SAlan Stern 		uhci->frame_cpu[framenum] = td;
1071da177e4SLinus Torvalds 	}
1081da177e4SLinus Torvalds }
1091da177e4SLinus Torvalds 
110dccf4a48SAlan Stern static inline void uhci_remove_td_from_frame_list(struct uhci_hcd *uhci,
111b81d3436SAlan Stern 		struct uhci_td *td)
1121da177e4SLinus Torvalds {
1131da177e4SLinus Torvalds 	/* If it's not inserted, don't remove it */
114b81d3436SAlan Stern 	if (td->frame == -1) {
115b81d3436SAlan Stern 		WARN_ON(!list_empty(&td->fl_list));
1161da177e4SLinus Torvalds 		return;
117b81d3436SAlan Stern 	}
1181da177e4SLinus Torvalds 
119b81d3436SAlan Stern 	if (uhci->frame_cpu[td->frame] == td) {
1201da177e4SLinus Torvalds 		if (list_empty(&td->fl_list)) {
121a1d59ce8SAlan Stern 			uhci->frame[td->frame] = td->link;
122a1d59ce8SAlan Stern 			uhci->frame_cpu[td->frame] = NULL;
1231da177e4SLinus Torvalds 		} else {
1241da177e4SLinus Torvalds 			struct uhci_td *ntd;
1251da177e4SLinus Torvalds 
1261da177e4SLinus Torvalds 			ntd = list_entry(td->fl_list.next, struct uhci_td, fl_list);
127a1d59ce8SAlan Stern 			uhci->frame[td->frame] = cpu_to_le32(ntd->dma_handle);
128a1d59ce8SAlan Stern 			uhci->frame_cpu[td->frame] = ntd;
1291da177e4SLinus Torvalds 		}
1301da177e4SLinus Torvalds 	} else {
1311da177e4SLinus Torvalds 		struct uhci_td *ptd;
1321da177e4SLinus Torvalds 
1331da177e4SLinus Torvalds 		ptd = list_entry(td->fl_list.prev, struct uhci_td, fl_list);
1341da177e4SLinus Torvalds 		ptd->link = td->link;
1351da177e4SLinus Torvalds 	}
1361da177e4SLinus Torvalds 
1371da177e4SLinus Torvalds 	list_del_init(&td->fl_list);
1381da177e4SLinus Torvalds 	td->frame = -1;
1391da177e4SLinus Torvalds }
1401da177e4SLinus Torvalds 
141dccf4a48SAlan Stern /*
142dccf4a48SAlan Stern  * Remove all the TDs for an Isochronous URB from the frame list
143dccf4a48SAlan Stern  */
144dccf4a48SAlan Stern static void uhci_unlink_isochronous_tds(struct uhci_hcd *uhci, struct urb *urb)
145b81d3436SAlan Stern {
146b81d3436SAlan Stern 	struct urb_priv *urbp = (struct urb_priv *) urb->hcpriv;
147b81d3436SAlan Stern 	struct uhci_td *td;
148b81d3436SAlan Stern 
149b81d3436SAlan Stern 	list_for_each_entry(td, &urbp->td_list, list)
150dccf4a48SAlan Stern 		uhci_remove_td_from_frame_list(uhci, td);
151b81d3436SAlan Stern 	wmb();
152b81d3436SAlan Stern }
153b81d3436SAlan Stern 
154dccf4a48SAlan Stern static struct uhci_qh *uhci_alloc_qh(struct uhci_hcd *uhci,
155dccf4a48SAlan Stern 		struct usb_device *udev, struct usb_host_endpoint *hep)
1561da177e4SLinus Torvalds {
1571da177e4SLinus Torvalds 	dma_addr_t dma_handle;
1581da177e4SLinus Torvalds 	struct uhci_qh *qh;
1591da177e4SLinus Torvalds 
1601da177e4SLinus Torvalds 	qh = dma_pool_alloc(uhci->qh_pool, GFP_ATOMIC, &dma_handle);
1611da177e4SLinus Torvalds 	if (!qh)
1621da177e4SLinus Torvalds 		return NULL;
1631da177e4SLinus Torvalds 
164*59e29ed9SAlan Stern 	memset(qh, 0, sizeof(*qh));
1651da177e4SLinus Torvalds 	qh->dma_handle = dma_handle;
1661da177e4SLinus Torvalds 
1671da177e4SLinus Torvalds 	qh->element = UHCI_PTR_TERM;
1681da177e4SLinus Torvalds 	qh->link = UHCI_PTR_TERM;
1691da177e4SLinus Torvalds 
170dccf4a48SAlan Stern 	INIT_LIST_HEAD(&qh->queue);
171dccf4a48SAlan Stern 	INIT_LIST_HEAD(&qh->node);
1721da177e4SLinus Torvalds 
173dccf4a48SAlan Stern 	if (udev) {		/* Normal QH */
174af0bb599SAlan Stern 		qh->dummy_td = uhci_alloc_td(uhci);
175af0bb599SAlan Stern 		if (!qh->dummy_td) {
176af0bb599SAlan Stern 			dma_pool_free(uhci->qh_pool, qh, dma_handle);
177af0bb599SAlan Stern 			return NULL;
178af0bb599SAlan Stern 		}
179dccf4a48SAlan Stern 		qh->state = QH_STATE_IDLE;
180dccf4a48SAlan Stern 		qh->hep = hep;
181dccf4a48SAlan Stern 		qh->udev = udev;
182dccf4a48SAlan Stern 		hep->hcpriv = qh;
1834de7d2c2SAlan Stern 		qh->type = hep->desc.bmAttributes & USB_ENDPOINT_XFERTYPE_MASK;
1841da177e4SLinus Torvalds 
185dccf4a48SAlan Stern 	} else {		/* Skeleton QH */
186dccf4a48SAlan Stern 		qh->state = QH_STATE_ACTIVE;
1874de7d2c2SAlan Stern 		qh->type = -1;
188dccf4a48SAlan Stern 	}
1891da177e4SLinus Torvalds 	return qh;
1901da177e4SLinus Torvalds }
1911da177e4SLinus Torvalds 
1921da177e4SLinus Torvalds static void uhci_free_qh(struct uhci_hcd *uhci, struct uhci_qh *qh)
1931da177e4SLinus Torvalds {
194dccf4a48SAlan Stern 	WARN_ON(qh->state != QH_STATE_IDLE && qh->udev);
195dccf4a48SAlan Stern 	if (!list_empty(&qh->queue))
1961da177e4SLinus Torvalds 		dev_warn(uhci_dev(uhci), "qh %p list not empty!\n", qh);
1971da177e4SLinus Torvalds 
198dccf4a48SAlan Stern 	list_del(&qh->node);
199dccf4a48SAlan Stern 	if (qh->udev) {
200dccf4a48SAlan Stern 		qh->hep->hcpriv = NULL;
201af0bb599SAlan Stern 		uhci_free_td(uhci, qh->dummy_td);
202dccf4a48SAlan Stern 	}
2031da177e4SLinus Torvalds 	dma_pool_free(uhci->qh_pool, qh, qh->dma_handle);
2041da177e4SLinus Torvalds }
2051da177e4SLinus Torvalds 
2061da177e4SLinus Torvalds /*
2070ed8fee1SAlan Stern  * When the currently executing URB is dequeued, save its current toggle value
2080ed8fee1SAlan Stern  */
2090ed8fee1SAlan Stern static void uhci_save_toggle(struct uhci_qh *qh, struct urb *urb)
2100ed8fee1SAlan Stern {
2110ed8fee1SAlan Stern 	struct urb_priv *urbp = (struct urb_priv *) urb->hcpriv;
2120ed8fee1SAlan Stern 	struct uhci_td *td;
2130ed8fee1SAlan Stern 
2140ed8fee1SAlan Stern 	/* If the QH element pointer is UHCI_PTR_TERM then then currently
2150ed8fee1SAlan Stern 	 * executing URB has already been unlinked, so this one isn't it. */
2160ed8fee1SAlan Stern 	if (qh_element(qh) == UHCI_PTR_TERM ||
2170ed8fee1SAlan Stern 				qh->queue.next != &urbp->node)
2180ed8fee1SAlan Stern 		return;
2190ed8fee1SAlan Stern 	qh->element = UHCI_PTR_TERM;
2200ed8fee1SAlan Stern 
2210ed8fee1SAlan Stern 	/* Only bulk and interrupt pipes have to worry about toggles */
2224de7d2c2SAlan Stern 	if (!(qh->type == USB_ENDPOINT_XFER_BULK ||
2234de7d2c2SAlan Stern 			qh->type == USB_ENDPOINT_XFER_INT))
2240ed8fee1SAlan Stern 		return;
2250ed8fee1SAlan Stern 
226*59e29ed9SAlan Stern 	WARN_ON(list_empty(&urbp->td_list));
227*59e29ed9SAlan Stern 	td = list_entry(urbp->td_list.next, struct uhci_td, list);
2280ed8fee1SAlan Stern 	qh->needs_fixup = 1;
2290ed8fee1SAlan Stern 	qh->initial_toggle = uhci_toggle(td_token(td));
2300ed8fee1SAlan Stern }
2310ed8fee1SAlan Stern 
2320ed8fee1SAlan Stern /*
2330ed8fee1SAlan Stern  * Fix up the data toggles for URBs in a queue, when one of them
2340ed8fee1SAlan Stern  * terminates early (short transfer, error, or dequeued).
2350ed8fee1SAlan Stern  */
2360ed8fee1SAlan Stern static void uhci_fixup_toggles(struct uhci_qh *qh, int skip_first)
2370ed8fee1SAlan Stern {
2380ed8fee1SAlan Stern 	struct urb_priv *urbp = NULL;
2390ed8fee1SAlan Stern 	struct uhci_td *td;
2400ed8fee1SAlan Stern 	unsigned int toggle = qh->initial_toggle;
2410ed8fee1SAlan Stern 	unsigned int pipe;
2420ed8fee1SAlan Stern 
2430ed8fee1SAlan Stern 	/* Fixups for a short transfer start with the second URB in the
2440ed8fee1SAlan Stern 	 * queue (the short URB is the first). */
2450ed8fee1SAlan Stern 	if (skip_first)
2460ed8fee1SAlan Stern 		urbp = list_entry(qh->queue.next, struct urb_priv, node);
2470ed8fee1SAlan Stern 
2480ed8fee1SAlan Stern 	/* When starting with the first URB, if the QH element pointer is
2490ed8fee1SAlan Stern 	 * still valid then we know the URB's toggles are okay. */
2500ed8fee1SAlan Stern 	else if (qh_element(qh) != UHCI_PTR_TERM)
2510ed8fee1SAlan Stern 		toggle = 2;
2520ed8fee1SAlan Stern 
2530ed8fee1SAlan Stern 	/* Fix up the toggle for the URBs in the queue.  Normally this
2540ed8fee1SAlan Stern 	 * loop won't run more than once: When an error or short transfer
2550ed8fee1SAlan Stern 	 * occurs, the queue usually gets emptied. */
2561393adb2SAlan Stern 	urbp = list_prepare_entry(urbp, &qh->queue, node);
2570ed8fee1SAlan Stern 	list_for_each_entry_continue(urbp, &qh->queue, node) {
2580ed8fee1SAlan Stern 
2590ed8fee1SAlan Stern 		/* If the first TD has the right toggle value, we don't
2600ed8fee1SAlan Stern 		 * need to change any toggles in this URB */
2610ed8fee1SAlan Stern 		td = list_entry(urbp->td_list.next, struct uhci_td, list);
2620ed8fee1SAlan Stern 		if (toggle > 1 || uhci_toggle(td_token(td)) == toggle) {
2630ed8fee1SAlan Stern 			td = list_entry(urbp->td_list.next, struct uhci_td,
2640ed8fee1SAlan Stern 					list);
2650ed8fee1SAlan Stern 			toggle = uhci_toggle(td_token(td)) ^ 1;
2660ed8fee1SAlan Stern 
2670ed8fee1SAlan Stern 		/* Otherwise all the toggles in the URB have to be switched */
2680ed8fee1SAlan Stern 		} else {
2690ed8fee1SAlan Stern 			list_for_each_entry(td, &urbp->td_list, list) {
2700ed8fee1SAlan Stern 				td->token ^= __constant_cpu_to_le32(
2710ed8fee1SAlan Stern 							TD_TOKEN_TOGGLE);
2720ed8fee1SAlan Stern 				toggle ^= 1;
2730ed8fee1SAlan Stern 			}
2740ed8fee1SAlan Stern 		}
2750ed8fee1SAlan Stern 	}
2760ed8fee1SAlan Stern 
2770ed8fee1SAlan Stern 	wmb();
2780ed8fee1SAlan Stern 	pipe = list_entry(qh->queue.next, struct urb_priv, node)->urb->pipe;
2790ed8fee1SAlan Stern 	usb_settoggle(qh->udev, usb_pipeendpoint(pipe),
2800ed8fee1SAlan Stern 			usb_pipeout(pipe), toggle);
2810ed8fee1SAlan Stern 	qh->needs_fixup = 0;
2820ed8fee1SAlan Stern }
2830ed8fee1SAlan Stern 
2840ed8fee1SAlan Stern /*
285dccf4a48SAlan Stern  * Put a QH on the schedule in both hardware and software
2861da177e4SLinus Torvalds  */
287dccf4a48SAlan Stern static void uhci_activate_qh(struct uhci_hcd *uhci, struct uhci_qh *qh)
2881da177e4SLinus Torvalds {
2891da177e4SLinus Torvalds 	struct uhci_qh *pqh;
2901da177e4SLinus Torvalds 
291dccf4a48SAlan Stern 	WARN_ON(list_empty(&qh->queue));
292dccf4a48SAlan Stern 
293dccf4a48SAlan Stern 	/* Set the element pointer if it isn't set already.
294dccf4a48SAlan Stern 	 * This isn't needed for Isochronous queues, but it doesn't hurt. */
295dccf4a48SAlan Stern 	if (qh_element(qh) == UHCI_PTR_TERM) {
296dccf4a48SAlan Stern 		struct urb_priv *urbp = list_entry(qh->queue.next,
297dccf4a48SAlan Stern 				struct urb_priv, node);
298dccf4a48SAlan Stern 		struct uhci_td *td = list_entry(urbp->td_list.next,
299dccf4a48SAlan Stern 				struct uhci_td, list);
300dccf4a48SAlan Stern 
301dccf4a48SAlan Stern 		qh->element = cpu_to_le32(td->dma_handle);
302dccf4a48SAlan Stern 	}
303dccf4a48SAlan Stern 
304dccf4a48SAlan Stern 	if (qh->state == QH_STATE_ACTIVE)
3051da177e4SLinus Torvalds 		return;
306dccf4a48SAlan Stern 	qh->state = QH_STATE_ACTIVE;
307dccf4a48SAlan Stern 
308dccf4a48SAlan Stern 	/* Move the QH from its old list to the end of the appropriate
309dccf4a48SAlan Stern 	 * skeleton's list */
3100ed8fee1SAlan Stern 	if (qh == uhci->next_qh)
3110ed8fee1SAlan Stern 		uhci->next_qh = list_entry(qh->node.next, struct uhci_qh,
3120ed8fee1SAlan Stern 				node);
313dccf4a48SAlan Stern 	list_move_tail(&qh->node, &qh->skel->node);
314dccf4a48SAlan Stern 
315dccf4a48SAlan Stern 	/* Link it into the schedule */
316dccf4a48SAlan Stern 	pqh = list_entry(qh->node.prev, struct uhci_qh, node);
317dccf4a48SAlan Stern 	qh->link = pqh->link;
318dccf4a48SAlan Stern 	wmb();
319dccf4a48SAlan Stern 	pqh->link = UHCI_PTR_QH | cpu_to_le32(qh->dma_handle);
320dccf4a48SAlan Stern }
3211da177e4SLinus Torvalds 
3221da177e4SLinus Torvalds /*
323dccf4a48SAlan Stern  * Take a QH off the hardware schedule
3241da177e4SLinus Torvalds  */
325dccf4a48SAlan Stern static void uhci_unlink_qh(struct uhci_hcd *uhci, struct uhci_qh *qh)
326dccf4a48SAlan Stern {
327dccf4a48SAlan Stern 	struct uhci_qh *pqh;
3281da177e4SLinus Torvalds 
329dccf4a48SAlan Stern 	if (qh->state == QH_STATE_UNLINKING)
330dccf4a48SAlan Stern 		return;
331dccf4a48SAlan Stern 	WARN_ON(qh->state != QH_STATE_ACTIVE || !qh->udev);
332dccf4a48SAlan Stern 	qh->state = QH_STATE_UNLINKING;
3331da177e4SLinus Torvalds 
334dccf4a48SAlan Stern 	/* Unlink the QH from the schedule and record when we did it */
335dccf4a48SAlan Stern 	pqh = list_entry(qh->node.prev, struct uhci_qh, node);
336dccf4a48SAlan Stern 	pqh->link = qh->link;
337dccf4a48SAlan Stern 	mb();
3381da177e4SLinus Torvalds 
3391da177e4SLinus Torvalds 	uhci_get_current_frame_number(uhci);
340dccf4a48SAlan Stern 	qh->unlink_frame = uhci->frame_number;
3411da177e4SLinus Torvalds 
342dccf4a48SAlan Stern 	/* Force an interrupt so we know when the QH is fully unlinked */
343dccf4a48SAlan Stern 	if (list_empty(&uhci->skel_unlink_qh->node))
3441da177e4SLinus Torvalds 		uhci_set_next_interrupt(uhci);
3451da177e4SLinus Torvalds 
346dccf4a48SAlan Stern 	/* Move the QH from its old list to the end of the unlinking list */
3470ed8fee1SAlan Stern 	if (qh == uhci->next_qh)
3480ed8fee1SAlan Stern 		uhci->next_qh = list_entry(qh->node.next, struct uhci_qh,
3490ed8fee1SAlan Stern 				node);
350dccf4a48SAlan Stern 	list_move_tail(&qh->node, &uhci->skel_unlink_qh->node);
3511da177e4SLinus Torvalds }
3521da177e4SLinus Torvalds 
3531da177e4SLinus Torvalds /*
354dccf4a48SAlan Stern  * When we and the controller are through with a QH, it becomes IDLE.
355dccf4a48SAlan Stern  * This happens when a QH has been off the schedule (on the unlinking
356dccf4a48SAlan Stern  * list) for more than one frame, or when an error occurs while adding
357dccf4a48SAlan Stern  * the first URB onto a new QH.
3581da177e4SLinus Torvalds  */
359dccf4a48SAlan Stern static void uhci_make_qh_idle(struct uhci_hcd *uhci, struct uhci_qh *qh)
360dccf4a48SAlan Stern {
361dccf4a48SAlan Stern 	WARN_ON(qh->state == QH_STATE_ACTIVE);
362dccf4a48SAlan Stern 
3630ed8fee1SAlan Stern 	if (qh == uhci->next_qh)
3640ed8fee1SAlan Stern 		uhci->next_qh = list_entry(qh->node.next, struct uhci_qh,
3650ed8fee1SAlan Stern 				node);
366dccf4a48SAlan Stern 	list_move(&qh->node, &uhci->idle_qh_list);
367dccf4a48SAlan Stern 	qh->state = QH_STATE_IDLE;
368dccf4a48SAlan Stern 
369*59e29ed9SAlan Stern 	/* Now that the QH is idle, its post_td isn't being used */
370*59e29ed9SAlan Stern 	if (qh->post_td) {
371*59e29ed9SAlan Stern 		uhci_free_td(uhci, qh->post_td);
372*59e29ed9SAlan Stern 		qh->post_td = NULL;
373*59e29ed9SAlan Stern 	}
374*59e29ed9SAlan Stern 
375dccf4a48SAlan Stern 	/* If anyone is waiting for a QH to become idle, wake them up */
376dccf4a48SAlan Stern 	if (uhci->num_waiting)
377dccf4a48SAlan Stern 		wake_up_all(&uhci->waitqh);
3781da177e4SLinus Torvalds }
3791da177e4SLinus Torvalds 
380dccf4a48SAlan Stern static inline struct urb_priv *uhci_alloc_urb_priv(struct uhci_hcd *uhci,
381dccf4a48SAlan Stern 		struct urb *urb)
3821da177e4SLinus Torvalds {
3831da177e4SLinus Torvalds 	struct urb_priv *urbp;
3841da177e4SLinus Torvalds 
3851da177e4SLinus Torvalds 	urbp = kmem_cache_alloc(uhci_up_cachep, SLAB_ATOMIC);
3861da177e4SLinus Torvalds 	if (!urbp)
3871da177e4SLinus Torvalds 		return NULL;
3881da177e4SLinus Torvalds 
3891da177e4SLinus Torvalds 	memset((void *)urbp, 0, sizeof(*urbp));
3901da177e4SLinus Torvalds 
3911da177e4SLinus Torvalds 	urbp->urb = urb;
3921da177e4SLinus Torvalds 	urb->hcpriv = urbp;
393dccf4a48SAlan Stern 
394dccf4a48SAlan Stern 	INIT_LIST_HEAD(&urbp->node);
395dccf4a48SAlan Stern 	INIT_LIST_HEAD(&urbp->td_list);
3961da177e4SLinus Torvalds 
3971da177e4SLinus Torvalds 	return urbp;
3981da177e4SLinus Torvalds }
3991da177e4SLinus Torvalds 
4001da177e4SLinus Torvalds static void uhci_add_td_to_urb(struct urb *urb, struct uhci_td *td)
4011da177e4SLinus Torvalds {
4021da177e4SLinus Torvalds 	struct urb_priv *urbp = (struct urb_priv *)urb->hcpriv;
4031da177e4SLinus Torvalds 
4041da177e4SLinus Torvalds 	list_add_tail(&td->list, &urbp->td_list);
4051da177e4SLinus Torvalds }
4061da177e4SLinus Torvalds 
4071da177e4SLinus Torvalds static void uhci_remove_td_from_urb(struct uhci_td *td)
4081da177e4SLinus Torvalds {
4091da177e4SLinus Torvalds 	if (list_empty(&td->list))
4101da177e4SLinus Torvalds 		return;
4111da177e4SLinus Torvalds 
4121da177e4SLinus Torvalds 	list_del_init(&td->list);
4131da177e4SLinus Torvalds }
4141da177e4SLinus Torvalds 
415dccf4a48SAlan Stern static void uhci_free_urb_priv(struct uhci_hcd *uhci,
416dccf4a48SAlan Stern 		struct urb_priv *urbp)
4171da177e4SLinus Torvalds {
4181da177e4SLinus Torvalds 	struct uhci_td *td, *tmp;
4191da177e4SLinus Torvalds 
420dccf4a48SAlan Stern 	if (!list_empty(&urbp->node))
421dccf4a48SAlan Stern 		dev_warn(uhci_dev(uhci), "urb %p still on QH's list!\n",
422dccf4a48SAlan Stern 				urbp->urb);
4231da177e4SLinus Torvalds 
4241da177e4SLinus Torvalds 	uhci_get_current_frame_number(uhci);
4251da177e4SLinus Torvalds 	if (uhci->frame_number + uhci->is_stopped != uhci->td_remove_age) {
4261da177e4SLinus Torvalds 		uhci_free_pending_tds(uhci);
4271da177e4SLinus Torvalds 		uhci->td_remove_age = uhci->frame_number;
4281da177e4SLinus Torvalds 	}
4291da177e4SLinus Torvalds 
4301da177e4SLinus Torvalds 	/* Check to see if the remove list is empty. Set the IOC bit */
431dccf4a48SAlan Stern 	/* to force an interrupt so we can remove the TDs. */
4321da177e4SLinus Torvalds 	if (list_empty(&uhci->td_remove_list))
4331da177e4SLinus Torvalds 		uhci_set_next_interrupt(uhci);
4341da177e4SLinus Torvalds 
4351da177e4SLinus Torvalds 	list_for_each_entry_safe(td, tmp, &urbp->td_list, list) {
4361da177e4SLinus Torvalds 		uhci_remove_td_from_urb(td);
4371da177e4SLinus Torvalds 		list_add(&td->remove_list, &uhci->td_remove_list);
4381da177e4SLinus Torvalds 	}
4391da177e4SLinus Torvalds 
440dccf4a48SAlan Stern 	urbp->urb->hcpriv = NULL;
4411da177e4SLinus Torvalds 	kmem_cache_free(uhci_up_cachep, urbp);
4421da177e4SLinus Torvalds }
4431da177e4SLinus Torvalds 
4441da177e4SLinus Torvalds static void uhci_inc_fsbr(struct uhci_hcd *uhci, struct urb *urb)
4451da177e4SLinus Torvalds {
4461da177e4SLinus Torvalds 	struct urb_priv *urbp = (struct urb_priv *)urb->hcpriv;
4471da177e4SLinus Torvalds 
4481da177e4SLinus Torvalds 	if ((!(urb->transfer_flags & URB_NO_FSBR)) && !urbp->fsbr) {
4491da177e4SLinus Torvalds 		urbp->fsbr = 1;
4501da177e4SLinus Torvalds 		if (!uhci->fsbr++ && !uhci->fsbrtimeout)
4511da177e4SLinus Torvalds 			uhci->skel_term_qh->link = cpu_to_le32(uhci->skel_fs_control_qh->dma_handle) | UHCI_PTR_QH;
4521da177e4SLinus Torvalds 	}
4531da177e4SLinus Torvalds }
4541da177e4SLinus Torvalds 
4551da177e4SLinus Torvalds static void uhci_dec_fsbr(struct uhci_hcd *uhci, struct urb *urb)
4561da177e4SLinus Torvalds {
4571da177e4SLinus Torvalds 	struct urb_priv *urbp = (struct urb_priv *)urb->hcpriv;
4581da177e4SLinus Torvalds 
4591da177e4SLinus Torvalds 	if ((!(urb->transfer_flags & URB_NO_FSBR)) && urbp->fsbr) {
4601da177e4SLinus Torvalds 		urbp->fsbr = 0;
4611da177e4SLinus Torvalds 		if (!--uhci->fsbr)
4621da177e4SLinus Torvalds 			uhci->fsbrtimeout = jiffies + FSBR_DELAY;
4631da177e4SLinus Torvalds 	}
4641da177e4SLinus Torvalds }
4651da177e4SLinus Torvalds 
4661da177e4SLinus Torvalds /*
4671da177e4SLinus Torvalds  * Map status to standard result codes
4681da177e4SLinus Torvalds  *
4691da177e4SLinus Torvalds  * <status> is (td_status(td) & 0xF60000), a.k.a.
4701da177e4SLinus Torvalds  * uhci_status_bits(td_status(td)).
4711da177e4SLinus Torvalds  * Note: <status> does not include the TD_CTRL_NAK bit.
4721da177e4SLinus Torvalds  * <dir_out> is True for output TDs and False for input TDs.
4731da177e4SLinus Torvalds  */
4741da177e4SLinus Torvalds static int uhci_map_status(int status, int dir_out)
4751da177e4SLinus Torvalds {
4761da177e4SLinus Torvalds 	if (!status)
4771da177e4SLinus Torvalds 		return 0;
4781da177e4SLinus Torvalds 	if (status & TD_CTRL_BITSTUFF)			/* Bitstuff error */
4791da177e4SLinus Torvalds 		return -EPROTO;
4801da177e4SLinus Torvalds 	if (status & TD_CTRL_CRCTIMEO) {		/* CRC/Timeout */
4811da177e4SLinus Torvalds 		if (dir_out)
4821da177e4SLinus Torvalds 			return -EPROTO;
4831da177e4SLinus Torvalds 		else
4841da177e4SLinus Torvalds 			return -EILSEQ;
4851da177e4SLinus Torvalds 	}
4861da177e4SLinus Torvalds 	if (status & TD_CTRL_BABBLE)			/* Babble */
4871da177e4SLinus Torvalds 		return -EOVERFLOW;
4881da177e4SLinus Torvalds 	if (status & TD_CTRL_DBUFERR)			/* Buffer error */
4891da177e4SLinus Torvalds 		return -ENOSR;
4901da177e4SLinus Torvalds 	if (status & TD_CTRL_STALLED)			/* Stalled */
4911da177e4SLinus Torvalds 		return -EPIPE;
4921da177e4SLinus Torvalds 	WARN_ON(status & TD_CTRL_ACTIVE);		/* Active */
4931da177e4SLinus Torvalds 	return 0;
4941da177e4SLinus Torvalds }
4951da177e4SLinus Torvalds 
4961da177e4SLinus Torvalds /*
4971da177e4SLinus Torvalds  * Control transfers
4981da177e4SLinus Torvalds  */
499dccf4a48SAlan Stern static int uhci_submit_control(struct uhci_hcd *uhci, struct urb *urb,
500dccf4a48SAlan Stern 		struct uhci_qh *qh)
5011da177e4SLinus Torvalds {
5021da177e4SLinus Torvalds 	struct uhci_td *td;
5031da177e4SLinus Torvalds 	unsigned long destination, status;
504dccf4a48SAlan Stern 	int maxsze = le16_to_cpu(qh->hep->desc.wMaxPacketSize);
5051da177e4SLinus Torvalds 	int len = urb->transfer_buffer_length;
5061da177e4SLinus Torvalds 	dma_addr_t data = urb->transfer_dma;
507dccf4a48SAlan Stern 	__le32 *plink;
5081da177e4SLinus Torvalds 
5091da177e4SLinus Torvalds 	/* The "pipe" thing contains the destination in bits 8--18 */
5101da177e4SLinus Torvalds 	destination = (urb->pipe & PIPE_DEVEP_MASK) | USB_PID_SETUP;
5111da177e4SLinus Torvalds 
512af0bb599SAlan Stern 	/* 3 errors, dummy TD remains inactive */
513af0bb599SAlan Stern 	status = uhci_maxerr(3);
5141da177e4SLinus Torvalds 	if (urb->dev->speed == USB_SPEED_LOW)
5151da177e4SLinus Torvalds 		status |= TD_CTRL_LS;
5161da177e4SLinus Torvalds 
5171da177e4SLinus Torvalds 	/*
5181da177e4SLinus Torvalds 	 * Build the TD for the control request setup packet
5191da177e4SLinus Torvalds 	 */
520af0bb599SAlan Stern 	td = qh->dummy_td;
5211da177e4SLinus Torvalds 	uhci_add_td_to_urb(urb, td);
522fa346568SAlan Stern 	uhci_fill_td(td, status, destination | uhci_explen(8),
5231da177e4SLinus Torvalds 			urb->setup_dma);
524dccf4a48SAlan Stern 	plink = &td->link;
525af0bb599SAlan Stern 	status |= TD_CTRL_ACTIVE;
5261da177e4SLinus Torvalds 
5271da177e4SLinus Torvalds 	/*
5281da177e4SLinus Torvalds 	 * If direction is "send", change the packet ID from SETUP (0x2D)
5291da177e4SLinus Torvalds 	 * to OUT (0xE1).  Else change it from SETUP to IN (0x69) and
5301da177e4SLinus Torvalds 	 * set Short Packet Detect (SPD) for all data packets.
5311da177e4SLinus Torvalds 	 */
5321da177e4SLinus Torvalds 	if (usb_pipeout(urb->pipe))
5331da177e4SLinus Torvalds 		destination ^= (USB_PID_SETUP ^ USB_PID_OUT);
5341da177e4SLinus Torvalds 	else {
5351da177e4SLinus Torvalds 		destination ^= (USB_PID_SETUP ^ USB_PID_IN);
5361da177e4SLinus Torvalds 		status |= TD_CTRL_SPD;
5371da177e4SLinus Torvalds 	}
5381da177e4SLinus Torvalds 
5391da177e4SLinus Torvalds 	/*
540687f5f34SAlan Stern 	 * Build the DATA TDs
5411da177e4SLinus Torvalds 	 */
5421da177e4SLinus Torvalds 	while (len > 0) {
543dccf4a48SAlan Stern 		int pktsze = min(len, maxsze);
5441da177e4SLinus Torvalds 
5452532178aSAlan Stern 		td = uhci_alloc_td(uhci);
5461da177e4SLinus Torvalds 		if (!td)
547af0bb599SAlan Stern 			goto nomem;
548dccf4a48SAlan Stern 		*plink = cpu_to_le32(td->dma_handle);
5491da177e4SLinus Torvalds 
5501da177e4SLinus Torvalds 		/* Alternate Data0/1 (start with Data1) */
5511da177e4SLinus Torvalds 		destination ^= TD_TOKEN_TOGGLE;
5521da177e4SLinus Torvalds 
5531da177e4SLinus Torvalds 		uhci_add_td_to_urb(urb, td);
554fa346568SAlan Stern 		uhci_fill_td(td, status, destination | uhci_explen(pktsze),
5551da177e4SLinus Torvalds 				data);
556dccf4a48SAlan Stern 		plink = &td->link;
5571da177e4SLinus Torvalds 
5581da177e4SLinus Torvalds 		data += pktsze;
5591da177e4SLinus Torvalds 		len -= pktsze;
5601da177e4SLinus Torvalds 	}
5611da177e4SLinus Torvalds 
5621da177e4SLinus Torvalds 	/*
5631da177e4SLinus Torvalds 	 * Build the final TD for control status
5641da177e4SLinus Torvalds 	 */
5652532178aSAlan Stern 	td = uhci_alloc_td(uhci);
5661da177e4SLinus Torvalds 	if (!td)
567af0bb599SAlan Stern 		goto nomem;
568dccf4a48SAlan Stern 	*plink = cpu_to_le32(td->dma_handle);
5691da177e4SLinus Torvalds 
5701da177e4SLinus Torvalds 	/*
5711da177e4SLinus Torvalds 	 * It's IN if the pipe is an output pipe or we're not expecting
5721da177e4SLinus Torvalds 	 * data back.
5731da177e4SLinus Torvalds 	 */
5741da177e4SLinus Torvalds 	destination &= ~TD_TOKEN_PID_MASK;
5751da177e4SLinus Torvalds 	if (usb_pipeout(urb->pipe) || !urb->transfer_buffer_length)
5761da177e4SLinus Torvalds 		destination |= USB_PID_IN;
5771da177e4SLinus Torvalds 	else
5781da177e4SLinus Torvalds 		destination |= USB_PID_OUT;
5791da177e4SLinus Torvalds 
5801da177e4SLinus Torvalds 	destination |= TD_TOKEN_TOGGLE;		/* End in Data1 */
5811da177e4SLinus Torvalds 
5821da177e4SLinus Torvalds 	status &= ~TD_CTRL_SPD;
5831da177e4SLinus Torvalds 
5841da177e4SLinus Torvalds 	uhci_add_td_to_urb(urb, td);
5851da177e4SLinus Torvalds 	uhci_fill_td(td, status | TD_CTRL_IOC,
586fa346568SAlan Stern 			destination | uhci_explen(0), 0);
587af0bb599SAlan Stern 	plink = &td->link;
588af0bb599SAlan Stern 
589af0bb599SAlan Stern 	/*
590af0bb599SAlan Stern 	 * Build the new dummy TD and activate the old one
591af0bb599SAlan Stern 	 */
592af0bb599SAlan Stern 	td = uhci_alloc_td(uhci);
593af0bb599SAlan Stern 	if (!td)
594af0bb599SAlan Stern 		goto nomem;
595af0bb599SAlan Stern 	*plink = cpu_to_le32(td->dma_handle);
596af0bb599SAlan Stern 
597af0bb599SAlan Stern 	uhci_fill_td(td, 0, USB_PID_OUT | uhci_explen(0), 0);
598af0bb599SAlan Stern 	wmb();
599af0bb599SAlan Stern 	qh->dummy_td->status |= __constant_cpu_to_le32(TD_CTRL_ACTIVE);
600af0bb599SAlan Stern 	qh->dummy_td = td;
6011da177e4SLinus Torvalds 
6021da177e4SLinus Torvalds 	/* Low-speed transfers get a different queue, and won't hog the bus.
6031da177e4SLinus Torvalds 	 * Also, some devices enumerate better without FSBR; the easiest way
6041da177e4SLinus Torvalds 	 * to do that is to put URBs on the low-speed queue while the device
605630aa3cfSAlan Stern 	 * isn't in the CONFIGURED state. */
6061da177e4SLinus Torvalds 	if (urb->dev->speed == USB_SPEED_LOW ||
607630aa3cfSAlan Stern 			urb->dev->state != USB_STATE_CONFIGURED)
608dccf4a48SAlan Stern 		qh->skel = uhci->skel_ls_control_qh;
6091da177e4SLinus Torvalds 	else {
610dccf4a48SAlan Stern 		qh->skel = uhci->skel_fs_control_qh;
6111da177e4SLinus Torvalds 		uhci_inc_fsbr(uhci, urb);
6121da177e4SLinus Torvalds 	}
613*59e29ed9SAlan Stern 
614*59e29ed9SAlan Stern 	urb->actual_length = -8;	/* Account for the SETUP packet */
615dccf4a48SAlan Stern 	return 0;
616af0bb599SAlan Stern 
617af0bb599SAlan Stern nomem:
618af0bb599SAlan Stern 	/* Remove the dummy TD from the td_list so it doesn't get freed */
619af0bb599SAlan Stern 	uhci_remove_td_from_urb(qh->dummy_td);
620af0bb599SAlan Stern 	return -ENOMEM;
6211da177e4SLinus Torvalds }
6221da177e4SLinus Torvalds 
6231da177e4SLinus Torvalds /*
6241da177e4SLinus Torvalds  * Common submit for bulk and interrupt
6251da177e4SLinus Torvalds  */
626dccf4a48SAlan Stern static int uhci_submit_common(struct uhci_hcd *uhci, struct urb *urb,
627dccf4a48SAlan Stern 		struct uhci_qh *qh)
6281da177e4SLinus Torvalds {
6291da177e4SLinus Torvalds 	struct uhci_td *td;
6301da177e4SLinus Torvalds 	unsigned long destination, status;
631dccf4a48SAlan Stern 	int maxsze = le16_to_cpu(qh->hep->desc.wMaxPacketSize);
6321da177e4SLinus Torvalds 	int len = urb->transfer_buffer_length;
6331da177e4SLinus Torvalds 	dma_addr_t data = urb->transfer_dma;
634af0bb599SAlan Stern 	__le32 *plink;
635af0bb599SAlan Stern 	unsigned int toggle;
6361da177e4SLinus Torvalds 
6371da177e4SLinus Torvalds 	if (len < 0)
6381da177e4SLinus Torvalds 		return -EINVAL;
6391da177e4SLinus Torvalds 
6401da177e4SLinus Torvalds 	/* The "pipe" thing contains the destination in bits 8--18 */
6411da177e4SLinus Torvalds 	destination = (urb->pipe & PIPE_DEVEP_MASK) | usb_packetid(urb->pipe);
642af0bb599SAlan Stern 	toggle = usb_gettoggle(urb->dev, usb_pipeendpoint(urb->pipe),
643af0bb599SAlan Stern 			 usb_pipeout(urb->pipe));
6441da177e4SLinus Torvalds 
645af0bb599SAlan Stern 	/* 3 errors, dummy TD remains inactive */
646af0bb599SAlan Stern 	status = uhci_maxerr(3);
6471da177e4SLinus Torvalds 	if (urb->dev->speed == USB_SPEED_LOW)
6481da177e4SLinus Torvalds 		status |= TD_CTRL_LS;
6491da177e4SLinus Torvalds 	if (usb_pipein(urb->pipe))
6501da177e4SLinus Torvalds 		status |= TD_CTRL_SPD;
6511da177e4SLinus Torvalds 
6521da177e4SLinus Torvalds 	/*
653687f5f34SAlan Stern 	 * Build the DATA TDs
6541da177e4SLinus Torvalds 	 */
655af0bb599SAlan Stern 	plink = NULL;
656af0bb599SAlan Stern 	td = qh->dummy_td;
6571da177e4SLinus Torvalds 	do {	/* Allow zero length packets */
6581da177e4SLinus Torvalds 		int pktsze = maxsze;
6591da177e4SLinus Torvalds 
660dccf4a48SAlan Stern 		if (len <= pktsze) {		/* The last packet */
6611da177e4SLinus Torvalds 			pktsze = len;
6621da177e4SLinus Torvalds 			if (!(urb->transfer_flags & URB_SHORT_NOT_OK))
6631da177e4SLinus Torvalds 				status &= ~TD_CTRL_SPD;
6641da177e4SLinus Torvalds 		}
6651da177e4SLinus Torvalds 
666af0bb599SAlan Stern 		if (plink) {
6672532178aSAlan Stern 			td = uhci_alloc_td(uhci);
6681da177e4SLinus Torvalds 			if (!td)
669af0bb599SAlan Stern 				goto nomem;
670dccf4a48SAlan Stern 			*plink = cpu_to_le32(td->dma_handle);
671af0bb599SAlan Stern 		}
6721da177e4SLinus Torvalds 		uhci_add_td_to_urb(urb, td);
673dccf4a48SAlan Stern 		uhci_fill_td(td, status,
674dccf4a48SAlan Stern 				destination | uhci_explen(pktsze) |
675af0bb599SAlan Stern 					(toggle << TD_TOKEN_TOGGLE_SHIFT),
6761da177e4SLinus Torvalds 				data);
677dccf4a48SAlan Stern 		plink = &td->link;
678af0bb599SAlan Stern 		status |= TD_CTRL_ACTIVE;
6791da177e4SLinus Torvalds 
6801da177e4SLinus Torvalds 		data += pktsze;
6811da177e4SLinus Torvalds 		len -= maxsze;
682af0bb599SAlan Stern 		toggle ^= 1;
6831da177e4SLinus Torvalds 	} while (len > 0);
6841da177e4SLinus Torvalds 
6851da177e4SLinus Torvalds 	/*
6861da177e4SLinus Torvalds 	 * URB_ZERO_PACKET means adding a 0-length packet, if direction
6871da177e4SLinus Torvalds 	 * is OUT and the transfer_length was an exact multiple of maxsze,
6881da177e4SLinus Torvalds 	 * hence (len = transfer_length - N * maxsze) == 0
6891da177e4SLinus Torvalds 	 * however, if transfer_length == 0, the zero packet was already
6901da177e4SLinus Torvalds 	 * prepared above.
6911da177e4SLinus Torvalds 	 */
692dccf4a48SAlan Stern 	if ((urb->transfer_flags & URB_ZERO_PACKET) &&
693dccf4a48SAlan Stern 			usb_pipeout(urb->pipe) && len == 0 &&
694dccf4a48SAlan Stern 			urb->transfer_buffer_length > 0) {
6952532178aSAlan Stern 		td = uhci_alloc_td(uhci);
6961da177e4SLinus Torvalds 		if (!td)
697af0bb599SAlan Stern 			goto nomem;
698dccf4a48SAlan Stern 		*plink = cpu_to_le32(td->dma_handle);
6991da177e4SLinus Torvalds 
7001da177e4SLinus Torvalds 		uhci_add_td_to_urb(urb, td);
701af0bb599SAlan Stern 		uhci_fill_td(td, status,
702af0bb599SAlan Stern 				destination | uhci_explen(0) |
703af0bb599SAlan Stern 					(toggle << TD_TOKEN_TOGGLE_SHIFT),
7041da177e4SLinus Torvalds 				data);
705af0bb599SAlan Stern 		plink = &td->link;
7061da177e4SLinus Torvalds 
707af0bb599SAlan Stern 		toggle ^= 1;
7081da177e4SLinus Torvalds 	}
7091da177e4SLinus Torvalds 
7101da177e4SLinus Torvalds 	/* Set the interrupt-on-completion flag on the last packet.
7111da177e4SLinus Torvalds 	 * A more-or-less typical 4 KB URB (= size of one memory page)
7121da177e4SLinus Torvalds 	 * will require about 3 ms to transfer; that's a little on the
7131da177e4SLinus Torvalds 	 * fast side but not enough to justify delaying an interrupt
7141da177e4SLinus Torvalds 	 * more than 2 or 3 URBs, so we will ignore the URB_NO_INTERRUPT
7151da177e4SLinus Torvalds 	 * flag setting. */
716dccf4a48SAlan Stern 	td->status |= __constant_cpu_to_le32(TD_CTRL_IOC);
7171da177e4SLinus Torvalds 
718af0bb599SAlan Stern 	/*
719af0bb599SAlan Stern 	 * Build the new dummy TD and activate the old one
720af0bb599SAlan Stern 	 */
721af0bb599SAlan Stern 	td = uhci_alloc_td(uhci);
722af0bb599SAlan Stern 	if (!td)
723af0bb599SAlan Stern 		goto nomem;
724af0bb599SAlan Stern 	*plink = cpu_to_le32(td->dma_handle);
725af0bb599SAlan Stern 
726af0bb599SAlan Stern 	uhci_fill_td(td, 0, USB_PID_OUT | uhci_explen(0), 0);
727af0bb599SAlan Stern 	wmb();
728af0bb599SAlan Stern 	qh->dummy_td->status |= __constant_cpu_to_le32(TD_CTRL_ACTIVE);
729af0bb599SAlan Stern 	qh->dummy_td = td;
730af0bb599SAlan Stern 
731af0bb599SAlan Stern 	usb_settoggle(urb->dev, usb_pipeendpoint(urb->pipe),
732af0bb599SAlan Stern 			usb_pipeout(urb->pipe), toggle);
733dccf4a48SAlan Stern 	return 0;
734af0bb599SAlan Stern 
735af0bb599SAlan Stern nomem:
736af0bb599SAlan Stern 	/* Remove the dummy TD from the td_list so it doesn't get freed */
737af0bb599SAlan Stern 	uhci_remove_td_from_urb(qh->dummy_td);
738af0bb599SAlan Stern 	return -ENOMEM;
7391da177e4SLinus Torvalds }
7401da177e4SLinus Torvalds 
741dccf4a48SAlan Stern static inline int uhci_submit_bulk(struct uhci_hcd *uhci, struct urb *urb,
742dccf4a48SAlan Stern 		struct uhci_qh *qh)
7431da177e4SLinus Torvalds {
7441da177e4SLinus Torvalds 	int ret;
7451da177e4SLinus Torvalds 
7461da177e4SLinus Torvalds 	/* Can't have low-speed bulk transfers */
7471da177e4SLinus Torvalds 	if (urb->dev->speed == USB_SPEED_LOW)
7481da177e4SLinus Torvalds 		return -EINVAL;
7491da177e4SLinus Torvalds 
750dccf4a48SAlan Stern 	qh->skel = uhci->skel_bulk_qh;
751dccf4a48SAlan Stern 	ret = uhci_submit_common(uhci, urb, qh);
752dccf4a48SAlan Stern 	if (ret == 0)
7531da177e4SLinus Torvalds 		uhci_inc_fsbr(uhci, urb);
7541da177e4SLinus Torvalds 	return ret;
7551da177e4SLinus Torvalds }
7561da177e4SLinus Torvalds 
757dccf4a48SAlan Stern static inline int uhci_submit_interrupt(struct uhci_hcd *uhci, struct urb *urb,
758dccf4a48SAlan Stern 		struct uhci_qh *qh)
7591da177e4SLinus Torvalds {
760dccf4a48SAlan Stern 	/* USB 1.1 interrupt transfers only involve one packet per interval.
761dccf4a48SAlan Stern 	 * Drivers can submit URBs of any length, but longer ones will need
762dccf4a48SAlan Stern 	 * multiple intervals to complete.
7631da177e4SLinus Torvalds 	 */
764dccf4a48SAlan Stern 	qh->skel = uhci->skelqh[__interval_to_skel(urb->interval)];
765dccf4a48SAlan Stern 	return uhci_submit_common(uhci, urb, qh);
7661da177e4SLinus Torvalds }
7671da177e4SLinus Torvalds 
7681da177e4SLinus Torvalds /*
769b1869000SAlan Stern  * Fix up the data structures following a short transfer
770b1869000SAlan Stern  */
771b1869000SAlan Stern static int uhci_fixup_short_transfer(struct uhci_hcd *uhci,
772*59e29ed9SAlan Stern 		struct uhci_qh *qh, struct urb_priv *urbp)
773b1869000SAlan Stern {
774b1869000SAlan Stern 	struct uhci_td *td;
775*59e29ed9SAlan Stern 	struct list_head *tmp;
776*59e29ed9SAlan Stern 	int ret;
777b1869000SAlan Stern 
778b1869000SAlan Stern 	td = list_entry(urbp->td_list.prev, struct uhci_td, list);
779b1869000SAlan Stern 	if (qh->type == USB_ENDPOINT_XFER_CONTROL) {
780b1869000SAlan Stern 
781b1869000SAlan Stern 		/* When a control transfer is short, we have to restart
782b1869000SAlan Stern 		 * the queue at the status stage transaction, which is
783b1869000SAlan Stern 		 * the last TD. */
784*59e29ed9SAlan Stern 		WARN_ON(list_empty(&urbp->td_list));
785b1869000SAlan Stern 		qh->element = cpu_to_le32(td->dma_handle);
786*59e29ed9SAlan Stern 		tmp = td->list.prev;
787b1869000SAlan Stern 		ret = -EINPROGRESS;
788b1869000SAlan Stern 
789*59e29ed9SAlan Stern 	} else {
790b1869000SAlan Stern 
791b1869000SAlan Stern 		/* When a bulk/interrupt transfer is short, we have to
792b1869000SAlan Stern 		 * fix up the toggles of the following URBs on the queue
793b1869000SAlan Stern 		 * before restarting the queue at the next URB. */
794*59e29ed9SAlan Stern 		qh->initial_toggle = uhci_toggle(td_token(qh->post_td)) ^ 1;
795b1869000SAlan Stern 		uhci_fixup_toggles(qh, 1);
796b1869000SAlan Stern 
797*59e29ed9SAlan Stern 		if (list_empty(&urbp->td_list))
798*59e29ed9SAlan Stern 			td = qh->post_td;
799b1869000SAlan Stern 		qh->element = td->link;
800*59e29ed9SAlan Stern 		tmp = urbp->td_list.prev;
801*59e29ed9SAlan Stern 		ret = 0;
802b1869000SAlan Stern 	}
803b1869000SAlan Stern 
804*59e29ed9SAlan Stern 	/* Remove all the TDs we skipped over, from tmp back to the start */
805*59e29ed9SAlan Stern 	while (tmp != &urbp->td_list) {
806*59e29ed9SAlan Stern 		td = list_entry(tmp, struct uhci_td, list);
807*59e29ed9SAlan Stern 		tmp = tmp->prev;
808*59e29ed9SAlan Stern 
809*59e29ed9SAlan Stern 		uhci_remove_td_from_urb(td);
810*59e29ed9SAlan Stern 		list_add(&td->remove_list, &uhci->td_remove_list);
811*59e29ed9SAlan Stern 	}
812b1869000SAlan Stern 	return ret;
813b1869000SAlan Stern }
814b1869000SAlan Stern 
815b1869000SAlan Stern /*
816b1869000SAlan Stern  * Common result for control, bulk, and interrupt
817b1869000SAlan Stern  */
818b1869000SAlan Stern static int uhci_result_common(struct uhci_hcd *uhci, struct urb *urb)
819b1869000SAlan Stern {
820b1869000SAlan Stern 	struct urb_priv *urbp = urb->hcpriv;
821b1869000SAlan Stern 	struct uhci_qh *qh = urbp->qh;
822*59e29ed9SAlan Stern 	struct uhci_td *td, *tmp;
823b1869000SAlan Stern 	unsigned status;
824b1869000SAlan Stern 	int ret = 0;
825b1869000SAlan Stern 
826*59e29ed9SAlan Stern 	list_for_each_entry_safe(td, tmp, &urbp->td_list, list) {
827b1869000SAlan Stern 		unsigned int ctrlstat;
828b1869000SAlan Stern 		int len;
829b1869000SAlan Stern 
830b1869000SAlan Stern 		ctrlstat = td_status(td);
831b1869000SAlan Stern 		status = uhci_status_bits(ctrlstat);
832b1869000SAlan Stern 		if (status & TD_CTRL_ACTIVE)
833b1869000SAlan Stern 			return -EINPROGRESS;
834b1869000SAlan Stern 
835b1869000SAlan Stern 		len = uhci_actual_length(ctrlstat);
836b1869000SAlan Stern 		urb->actual_length += len;
837b1869000SAlan Stern 
838b1869000SAlan Stern 		if (status) {
839b1869000SAlan Stern 			ret = uhci_map_status(status,
840b1869000SAlan Stern 					uhci_packetout(td_token(td)));
841b1869000SAlan Stern 			if ((debug == 1 && ret != -EPIPE) || debug > 1) {
842b1869000SAlan Stern 				/* Some debugging code */
843b1869000SAlan Stern 				dev_dbg(uhci_dev(uhci),
844b1869000SAlan Stern 						"%s: failed with status %x\n",
845b1869000SAlan Stern 						__FUNCTION__, status);
846b1869000SAlan Stern 
847b1869000SAlan Stern 				if (debug > 1 && errbuf) {
848b1869000SAlan Stern 					/* Print the chain for debugging */
849b1869000SAlan Stern 					uhci_show_qh(urbp->qh, errbuf,
850b1869000SAlan Stern 							ERRBUF_LEN, 0);
851b1869000SAlan Stern 					lprintk(errbuf);
852b1869000SAlan Stern 				}
853b1869000SAlan Stern 			}
854b1869000SAlan Stern 
855b1869000SAlan Stern 		} else if (len < uhci_expected_length(td_token(td))) {
856b1869000SAlan Stern 
857b1869000SAlan Stern 			/* We received a short packet */
858b1869000SAlan Stern 			if (urb->transfer_flags & URB_SHORT_NOT_OK)
859b1869000SAlan Stern 				ret = -EREMOTEIO;
860b1869000SAlan Stern 			else if (ctrlstat & TD_CTRL_SPD)
861b1869000SAlan Stern 				ret = 1;
862b1869000SAlan Stern 		}
863b1869000SAlan Stern 
864*59e29ed9SAlan Stern 		uhci_remove_td_from_urb(td);
865*59e29ed9SAlan Stern 		if (qh->post_td)
866*59e29ed9SAlan Stern 			list_add(&qh->post_td->remove_list,
867*59e29ed9SAlan Stern 					&uhci->td_remove_list);
868*59e29ed9SAlan Stern 		qh->post_td = td;
869*59e29ed9SAlan Stern 
870b1869000SAlan Stern 		if (ret != 0)
871b1869000SAlan Stern 			goto err;
872b1869000SAlan Stern 	}
873b1869000SAlan Stern 	return ret;
874b1869000SAlan Stern 
875b1869000SAlan Stern err:
876b1869000SAlan Stern 	if (ret < 0) {
877b1869000SAlan Stern 		/* In case a control transfer gets an error
878b1869000SAlan Stern 		 * during the setup stage */
879b1869000SAlan Stern 		urb->actual_length = max(urb->actual_length, 0);
880b1869000SAlan Stern 
881b1869000SAlan Stern 		/* Note that the queue has stopped and save
882b1869000SAlan Stern 		 * the next toggle value */
883b1869000SAlan Stern 		qh->element = UHCI_PTR_TERM;
884b1869000SAlan Stern 		qh->is_stopped = 1;
885b1869000SAlan Stern 		qh->needs_fixup = (qh->type != USB_ENDPOINT_XFER_CONTROL);
886b1869000SAlan Stern 		qh->initial_toggle = uhci_toggle(td_token(td)) ^
887b1869000SAlan Stern 				(ret == -EREMOTEIO);
888b1869000SAlan Stern 
889b1869000SAlan Stern 	} else		/* Short packet received */
890*59e29ed9SAlan Stern 		ret = uhci_fixup_short_transfer(uhci, qh, urbp);
891b1869000SAlan Stern 	return ret;
892b1869000SAlan Stern }
893b1869000SAlan Stern 
894b1869000SAlan Stern /*
8951da177e4SLinus Torvalds  * Isochronous transfers
8961da177e4SLinus Torvalds  */
897dccf4a48SAlan Stern static int uhci_submit_isochronous(struct uhci_hcd *uhci, struct urb *urb,
898dccf4a48SAlan Stern 		struct uhci_qh *qh)
8991da177e4SLinus Torvalds {
900dccf4a48SAlan Stern 	struct uhci_td *td = NULL;	/* Since urb->number_of_packets > 0 */
9010ed8fee1SAlan Stern 	int i, frame;
902dccf4a48SAlan Stern 	unsigned long destination, status;
903b81d3436SAlan Stern 	struct urb_priv *urbp = (struct urb_priv *) urb->hcpriv;
9041da177e4SLinus Torvalds 
9050ed8fee1SAlan Stern 	if (urb->number_of_packets > 900)	/* 900? Why? */
9060ed8fee1SAlan Stern 		return -EFBIG;
9070ed8fee1SAlan Stern 
9081da177e4SLinus Torvalds 	status = TD_CTRL_ACTIVE | TD_CTRL_IOS;
9091da177e4SLinus Torvalds 	destination = (urb->pipe & PIPE_DEVEP_MASK) | usb_packetid(urb->pipe);
9101da177e4SLinus Torvalds 
9110ed8fee1SAlan Stern 	/* Figure out the starting frame number */
9120ed8fee1SAlan Stern 	if (urb->transfer_flags & URB_ISO_ASAP) {
9130ed8fee1SAlan Stern 		if (list_empty(&qh->queue)) {
9140ed8fee1SAlan Stern 			uhci_get_current_frame_number(uhci);
9150ed8fee1SAlan Stern 			urb->start_frame = (uhci->frame_number + 10);
9160ed8fee1SAlan Stern 
9170ed8fee1SAlan Stern 		} else {		/* Go right after the last one */
9180ed8fee1SAlan Stern 			struct urb *last_urb;
9190ed8fee1SAlan Stern 
9200ed8fee1SAlan Stern 			last_urb = list_entry(qh->queue.prev,
9210ed8fee1SAlan Stern 					struct urb_priv, node)->urb;
9220ed8fee1SAlan Stern 			urb->start_frame = (last_urb->start_frame +
9230ed8fee1SAlan Stern 					last_urb->number_of_packets *
9240ed8fee1SAlan Stern 					last_urb->interval);
9250ed8fee1SAlan Stern 		}
9260ed8fee1SAlan Stern 	} else {
9270ed8fee1SAlan Stern 		/* FIXME: Sanity check */
9280ed8fee1SAlan Stern 	}
9290ed8fee1SAlan Stern 	urb->start_frame &= (UHCI_NUMFRAMES - 1);
9301da177e4SLinus Torvalds 
931b81d3436SAlan Stern 	for (i = 0; i < urb->number_of_packets; i++) {
9322532178aSAlan Stern 		td = uhci_alloc_td(uhci);
9331da177e4SLinus Torvalds 		if (!td)
9341da177e4SLinus Torvalds 			return -ENOMEM;
9351da177e4SLinus Torvalds 
9361da177e4SLinus Torvalds 		uhci_add_td_to_urb(urb, td);
937dccf4a48SAlan Stern 		uhci_fill_td(td, status, destination |
938dccf4a48SAlan Stern 				uhci_explen(urb->iso_frame_desc[i].length),
939dccf4a48SAlan Stern 				urb->transfer_dma +
940dccf4a48SAlan Stern 					urb->iso_frame_desc[i].offset);
941b81d3436SAlan Stern 	}
9421da177e4SLinus Torvalds 
943dccf4a48SAlan Stern 	/* Set the interrupt-on-completion flag on the last packet. */
944dccf4a48SAlan Stern 	td->status |= __constant_cpu_to_le32(TD_CTRL_IOC);
945dccf4a48SAlan Stern 
946dccf4a48SAlan Stern 	qh->skel = uhci->skel_iso_qh;
947dccf4a48SAlan Stern 
948dccf4a48SAlan Stern 	/* Add the TDs to the frame list */
949b81d3436SAlan Stern 	frame = urb->start_frame;
950b81d3436SAlan Stern 	list_for_each_entry(td, &urbp->td_list, list) {
951dccf4a48SAlan Stern 		uhci_insert_td_in_frame_list(uhci, td, frame);
952b81d3436SAlan Stern 		frame += urb->interval;
9531da177e4SLinus Torvalds 	}
9541da177e4SLinus Torvalds 
955dccf4a48SAlan Stern 	return 0;
9561da177e4SLinus Torvalds }
9571da177e4SLinus Torvalds 
9581da177e4SLinus Torvalds static int uhci_result_isochronous(struct uhci_hcd *uhci, struct urb *urb)
9591da177e4SLinus Torvalds {
9601da177e4SLinus Torvalds 	struct uhci_td *td;
9611da177e4SLinus Torvalds 	struct urb_priv *urbp = (struct urb_priv *)urb->hcpriv;
9621da177e4SLinus Torvalds 	int status;
9631da177e4SLinus Torvalds 	int i, ret = 0;
9641da177e4SLinus Torvalds 
965b81d3436SAlan Stern 	urb->actual_length = urb->error_count = 0;
9661da177e4SLinus Torvalds 
9671da177e4SLinus Torvalds 	i = 0;
9681da177e4SLinus Torvalds 	list_for_each_entry(td, &urbp->td_list, list) {
9691da177e4SLinus Torvalds 		int actlength;
9701da177e4SLinus Torvalds 		unsigned int ctrlstat = td_status(td);
9711da177e4SLinus Torvalds 
9721da177e4SLinus Torvalds 		if (ctrlstat & TD_CTRL_ACTIVE)
9731da177e4SLinus Torvalds 			return -EINPROGRESS;
9741da177e4SLinus Torvalds 
9751da177e4SLinus Torvalds 		actlength = uhci_actual_length(ctrlstat);
9761da177e4SLinus Torvalds 		urb->iso_frame_desc[i].actual_length = actlength;
9771da177e4SLinus Torvalds 		urb->actual_length += actlength;
9781da177e4SLinus Torvalds 
9791da177e4SLinus Torvalds 		status = uhci_map_status(uhci_status_bits(ctrlstat),
9801da177e4SLinus Torvalds 				usb_pipeout(urb->pipe));
9811da177e4SLinus Torvalds 		urb->iso_frame_desc[i].status = status;
9821da177e4SLinus Torvalds 		if (status) {
9831da177e4SLinus Torvalds 			urb->error_count++;
9841da177e4SLinus Torvalds 			ret = status;
9851da177e4SLinus Torvalds 		}
9861da177e4SLinus Torvalds 
9871da177e4SLinus Torvalds 		i++;
9881da177e4SLinus Torvalds 	}
9891da177e4SLinus Torvalds 
9901da177e4SLinus Torvalds 	return ret;
9911da177e4SLinus Torvalds }
9921da177e4SLinus Torvalds 
9931da177e4SLinus Torvalds static int uhci_urb_enqueue(struct usb_hcd *hcd,
994dccf4a48SAlan Stern 		struct usb_host_endpoint *hep,
99555016f10SAl Viro 		struct urb *urb, gfp_t mem_flags)
9961da177e4SLinus Torvalds {
9971da177e4SLinus Torvalds 	int ret;
9981da177e4SLinus Torvalds 	struct uhci_hcd *uhci = hcd_to_uhci(hcd);
9991da177e4SLinus Torvalds 	unsigned long flags;
1000dccf4a48SAlan Stern 	struct urb_priv *urbp;
1001dccf4a48SAlan Stern 	struct uhci_qh *qh;
10021da177e4SLinus Torvalds 	int bustime;
10031da177e4SLinus Torvalds 
10041da177e4SLinus Torvalds 	spin_lock_irqsave(&uhci->lock, flags);
10051da177e4SLinus Torvalds 
10061da177e4SLinus Torvalds 	ret = urb->status;
10071da177e4SLinus Torvalds 	if (ret != -EINPROGRESS)		/* URB already unlinked! */
1008dccf4a48SAlan Stern 		goto done;
10091da177e4SLinus Torvalds 
10101da177e4SLinus Torvalds 	ret = -ENOMEM;
1011dccf4a48SAlan Stern 	urbp = uhci_alloc_urb_priv(uhci, urb);
1012dccf4a48SAlan Stern 	if (!urbp)
1013dccf4a48SAlan Stern 		goto done;
1014dccf4a48SAlan Stern 
1015dccf4a48SAlan Stern 	if (hep->hcpriv)
1016dccf4a48SAlan Stern 		qh = (struct uhci_qh *) hep->hcpriv;
1017dccf4a48SAlan Stern 	else {
1018dccf4a48SAlan Stern 		qh = uhci_alloc_qh(uhci, urb->dev, hep);
1019dccf4a48SAlan Stern 		if (!qh)
1020dccf4a48SAlan Stern 			goto err_no_qh;
10211da177e4SLinus Torvalds 	}
1022dccf4a48SAlan Stern 	urbp->qh = qh;
10231da177e4SLinus Torvalds 
10244de7d2c2SAlan Stern 	switch (qh->type) {
10254de7d2c2SAlan Stern 	case USB_ENDPOINT_XFER_CONTROL:
1026dccf4a48SAlan Stern 		ret = uhci_submit_control(uhci, urb, qh);
1027dccf4a48SAlan Stern 		break;
10284de7d2c2SAlan Stern 	case USB_ENDPOINT_XFER_BULK:
1029dccf4a48SAlan Stern 		ret = uhci_submit_bulk(uhci, urb, qh);
10301da177e4SLinus Torvalds 		break;
10314de7d2c2SAlan Stern 	case USB_ENDPOINT_XFER_INT:
1032dccf4a48SAlan Stern 		if (list_empty(&qh->queue)) {
10331da177e4SLinus Torvalds 			bustime = usb_check_bandwidth(urb->dev, urb);
10341da177e4SLinus Torvalds 			if (bustime < 0)
10351da177e4SLinus Torvalds 				ret = bustime;
10361da177e4SLinus Torvalds 			else {
1037dccf4a48SAlan Stern 				ret = uhci_submit_interrupt(uhci, urb, qh);
1038dccf4a48SAlan Stern 				if (ret == 0)
10391da177e4SLinus Torvalds 					usb_claim_bandwidth(urb->dev, urb, bustime, 0);
10401da177e4SLinus Torvalds 			}
10411da177e4SLinus Torvalds 		} else {	/* inherit from parent */
1042dccf4a48SAlan Stern 			struct urb_priv *eurbp;
1043dccf4a48SAlan Stern 
1044dccf4a48SAlan Stern 			eurbp = list_entry(qh->queue.prev, struct urb_priv,
1045dccf4a48SAlan Stern 					node);
1046dccf4a48SAlan Stern 			urb->bandwidth = eurbp->urb->bandwidth;
1047dccf4a48SAlan Stern 			ret = uhci_submit_interrupt(uhci, urb, qh);
10481da177e4SLinus Torvalds 		}
10491da177e4SLinus Torvalds 		break;
10504de7d2c2SAlan Stern 	case USB_ENDPOINT_XFER_ISOC:
10511da177e4SLinus Torvalds 		bustime = usb_check_bandwidth(urb->dev, urb);
10521da177e4SLinus Torvalds 		if (bustime < 0) {
10531da177e4SLinus Torvalds 			ret = bustime;
10541da177e4SLinus Torvalds 			break;
10551da177e4SLinus Torvalds 		}
10561da177e4SLinus Torvalds 
1057dccf4a48SAlan Stern 		ret = uhci_submit_isochronous(uhci, urb, qh);
1058dccf4a48SAlan Stern 		if (ret == 0)
10591da177e4SLinus Torvalds 			usb_claim_bandwidth(urb->dev, urb, bustime, 1);
10601da177e4SLinus Torvalds 		break;
10611da177e4SLinus Torvalds 	}
1062dccf4a48SAlan Stern 	if (ret != 0)
1063dccf4a48SAlan Stern 		goto err_submit_failed;
10641da177e4SLinus Torvalds 
1065dccf4a48SAlan Stern 	/* Add this URB to the QH */
1066dccf4a48SAlan Stern 	urbp->qh = qh;
1067dccf4a48SAlan Stern 	list_add_tail(&urbp->node, &qh->queue);
10681da177e4SLinus Torvalds 
1069dccf4a48SAlan Stern 	/* If the new URB is the first and only one on this QH then either
1070dccf4a48SAlan Stern 	 * the QH is new and idle or else it's unlinked and waiting to
10712775562aSAlan Stern 	 * become idle, so we can activate it right away.  But only if the
10722775562aSAlan Stern 	 * queue isn't stopped. */
10732775562aSAlan Stern 	if (qh->queue.next == &urbp->node && !qh->is_stopped)
1074dccf4a48SAlan Stern 		uhci_activate_qh(uhci, qh);
1075dccf4a48SAlan Stern 	goto done;
1076dccf4a48SAlan Stern 
1077dccf4a48SAlan Stern err_submit_failed:
1078dccf4a48SAlan Stern 	if (qh->state == QH_STATE_IDLE)
1079dccf4a48SAlan Stern 		uhci_make_qh_idle(uhci, qh);	/* Reclaim unused QH */
1080dccf4a48SAlan Stern 
1081dccf4a48SAlan Stern err_no_qh:
1082dccf4a48SAlan Stern 	uhci_free_urb_priv(uhci, urbp);
1083dccf4a48SAlan Stern 
1084dccf4a48SAlan Stern done:
10851da177e4SLinus Torvalds 	spin_unlock_irqrestore(&uhci->lock, flags);
10861da177e4SLinus Torvalds 	return ret;
10871da177e4SLinus Torvalds }
10881da177e4SLinus Torvalds 
10891da177e4SLinus Torvalds static int uhci_urb_dequeue(struct usb_hcd *hcd, struct urb *urb)
10901da177e4SLinus Torvalds {
10911da177e4SLinus Torvalds 	struct uhci_hcd *uhci = hcd_to_uhci(hcd);
10921da177e4SLinus Torvalds 	unsigned long flags;
10931da177e4SLinus Torvalds 	struct urb_priv *urbp;
10941da177e4SLinus Torvalds 
10951da177e4SLinus Torvalds 	spin_lock_irqsave(&uhci->lock, flags);
10961da177e4SLinus Torvalds 	urbp = urb->hcpriv;
10971da177e4SLinus Torvalds 	if (!urbp)			/* URB was never linked! */
10981da177e4SLinus Torvalds 		goto done;
10991da177e4SLinus Torvalds 
1100dccf4a48SAlan Stern 	/* Remove Isochronous TDs from the frame list ASAP */
11014de7d2c2SAlan Stern 	if (urbp->qh->type == USB_ENDPOINT_XFER_ISOC)
1102dccf4a48SAlan Stern 		uhci_unlink_isochronous_tds(uhci, urb);
1103dccf4a48SAlan Stern 	uhci_unlink_qh(uhci, urbp->qh);
11041da177e4SLinus Torvalds 
11051da177e4SLinus Torvalds done:
11061da177e4SLinus Torvalds 	spin_unlock_irqrestore(&uhci->lock, flags);
11071da177e4SLinus Torvalds 	return 0;
11081da177e4SLinus Torvalds }
11091da177e4SLinus Torvalds 
11100ed8fee1SAlan Stern /*
11110ed8fee1SAlan Stern  * Finish unlinking an URB and give it back
11120ed8fee1SAlan Stern  */
11130ed8fee1SAlan Stern static void uhci_giveback_urb(struct uhci_hcd *uhci, struct uhci_qh *qh,
11140ed8fee1SAlan Stern 		struct urb *urb, struct pt_regs *regs)
11150ed8fee1SAlan Stern __releases(uhci->lock)
11160ed8fee1SAlan Stern __acquires(uhci->lock)
11171da177e4SLinus Torvalds {
11181da177e4SLinus Torvalds 	struct urb_priv *urbp = (struct urb_priv *) urb->hcpriv;
11191da177e4SLinus Torvalds 
11200ed8fee1SAlan Stern 	/* Isochronous TDs get unlinked directly from the frame list */
11214de7d2c2SAlan Stern 	if (qh->type == USB_ENDPOINT_XFER_ISOC)
11220ed8fee1SAlan Stern 		uhci_unlink_isochronous_tds(uhci, urb);
11231da177e4SLinus Torvalds 
11240ed8fee1SAlan Stern 	/* If the URB isn't first on its queue, adjust the link pointer
11250ed8fee1SAlan Stern 	 * of the last TD in the previous URB. */
11260ed8fee1SAlan Stern 	else if (qh->queue.next != &urbp->node) {
11270ed8fee1SAlan Stern 		struct urb_priv *purbp;
11280ed8fee1SAlan Stern 		struct uhci_td *ptd, *ltd;
11291da177e4SLinus Torvalds 
11300ed8fee1SAlan Stern 		purbp = list_entry(urbp->node.prev, struct urb_priv, node);
1131*59e29ed9SAlan Stern 		WARN_ON(list_empty(&purbp->td_list));
11320ed8fee1SAlan Stern 		ptd = list_entry(purbp->td_list.prev, struct uhci_td,
11330ed8fee1SAlan Stern 				list);
11340ed8fee1SAlan Stern 		ltd = list_entry(urbp->td_list.prev, struct uhci_td,
11350ed8fee1SAlan Stern 				list);
11360ed8fee1SAlan Stern 		ptd->link = ltd->link;
11371da177e4SLinus Torvalds 	}
11381da177e4SLinus Torvalds 
11390ed8fee1SAlan Stern 	/* Take the URB off the QH's queue.  If the queue is now empty,
11400ed8fee1SAlan Stern 	 * this is a perfect time for a toggle fixup. */
11410ed8fee1SAlan Stern 	list_del_init(&urbp->node);
11420ed8fee1SAlan Stern 	if (list_empty(&qh->queue) && qh->needs_fixup) {
11430ed8fee1SAlan Stern 		usb_settoggle(urb->dev, usb_pipeendpoint(urb->pipe),
11440ed8fee1SAlan Stern 				usb_pipeout(urb->pipe), qh->initial_toggle);
11450ed8fee1SAlan Stern 		qh->needs_fixup = 0;
11460ed8fee1SAlan Stern 	}
11470ed8fee1SAlan Stern 
11480ed8fee1SAlan Stern 	uhci_dec_fsbr(uhci, urb);	/* Safe since it checks */
11490ed8fee1SAlan Stern 	uhci_free_urb_priv(uhci, urbp);
11500ed8fee1SAlan Stern 
11514de7d2c2SAlan Stern 	switch (qh->type) {
11524de7d2c2SAlan Stern 	case USB_ENDPOINT_XFER_ISOC:
11530ed8fee1SAlan Stern 		/* Release bandwidth for Interrupt or Isoc. transfers */
11540ed8fee1SAlan Stern 		if (urb->bandwidth)
11550ed8fee1SAlan Stern 			usb_release_bandwidth(urb->dev, urb, 1);
11560ed8fee1SAlan Stern 		break;
11574de7d2c2SAlan Stern 	case USB_ENDPOINT_XFER_INT:
11580ed8fee1SAlan Stern 		/* Release bandwidth for Interrupt or Isoc. transfers */
11590ed8fee1SAlan Stern 		/* Make sure we don't release if we have a queued URB */
11600ed8fee1SAlan Stern 		if (list_empty(&qh->queue) && urb->bandwidth)
11610ed8fee1SAlan Stern 			usb_release_bandwidth(urb->dev, urb, 0);
11620ed8fee1SAlan Stern 		else
11630ed8fee1SAlan Stern 			/* bandwidth was passed on to queued URB, */
11640ed8fee1SAlan Stern 			/* so don't let usb_unlink_urb() release it */
11650ed8fee1SAlan Stern 			urb->bandwidth = 0;
11660ed8fee1SAlan Stern 		break;
11670ed8fee1SAlan Stern 	}
11680ed8fee1SAlan Stern 
11690ed8fee1SAlan Stern 	spin_unlock(&uhci->lock);
11700ed8fee1SAlan Stern 	usb_hcd_giveback_urb(uhci_to_hcd(uhci), urb, regs);
11710ed8fee1SAlan Stern 	spin_lock(&uhci->lock);
11720ed8fee1SAlan Stern 
11730ed8fee1SAlan Stern 	/* If the queue is now empty, we can unlink the QH and give up its
11740ed8fee1SAlan Stern 	 * reserved bandwidth. */
11750ed8fee1SAlan Stern 	if (list_empty(&qh->queue)) {
11760ed8fee1SAlan Stern 		uhci_unlink_qh(uhci, qh);
11770ed8fee1SAlan Stern 
11780ed8fee1SAlan Stern 		/* Bandwidth stuff not yet implemented */
11790ed8fee1SAlan Stern 	}
11800ed8fee1SAlan Stern }
11810ed8fee1SAlan Stern 
11820ed8fee1SAlan Stern /*
11830ed8fee1SAlan Stern  * Scan the URBs in a QH's queue
11840ed8fee1SAlan Stern  */
11850ed8fee1SAlan Stern #define QH_FINISHED_UNLINKING(qh)			\
11860ed8fee1SAlan Stern 		(qh->state == QH_STATE_UNLINKING &&	\
11870ed8fee1SAlan Stern 		uhci->frame_number + uhci->is_stopped != qh->unlink_frame)
11880ed8fee1SAlan Stern 
11890ed8fee1SAlan Stern static void uhci_scan_qh(struct uhci_hcd *uhci, struct uhci_qh *qh,
11900ed8fee1SAlan Stern 		struct pt_regs *regs)
11910ed8fee1SAlan Stern {
11920ed8fee1SAlan Stern 	struct urb_priv *urbp;
11930ed8fee1SAlan Stern 	struct urb *urb;
11940ed8fee1SAlan Stern 	int status;
11950ed8fee1SAlan Stern 
11960ed8fee1SAlan Stern 	while (!list_empty(&qh->queue)) {
11970ed8fee1SAlan Stern 		urbp = list_entry(qh->queue.next, struct urb_priv, node);
11980ed8fee1SAlan Stern 		urb = urbp->urb;
11990ed8fee1SAlan Stern 
1200b1869000SAlan Stern 		if (qh->type == USB_ENDPOINT_XFER_ISOC)
12010ed8fee1SAlan Stern 			status = uhci_result_isochronous(uhci, urb);
1202b1869000SAlan Stern 		else
12030ed8fee1SAlan Stern 			status = uhci_result_common(uhci, urb);
12040ed8fee1SAlan Stern 		if (status == -EINPROGRESS)
12050ed8fee1SAlan Stern 			break;
12060ed8fee1SAlan Stern 
12070ed8fee1SAlan Stern 		spin_lock(&urb->lock);
12080ed8fee1SAlan Stern 		if (urb->status == -EINPROGRESS)	/* Not dequeued */
12090ed8fee1SAlan Stern 			urb->status = status;
12100ed8fee1SAlan Stern 		else
12112775562aSAlan Stern 			status = ECONNRESET;		/* Not -ECONNRESET */
12120ed8fee1SAlan Stern 		spin_unlock(&urb->lock);
12130ed8fee1SAlan Stern 
12140ed8fee1SAlan Stern 		/* Dequeued but completed URBs can't be given back unless
12150ed8fee1SAlan Stern 		 * the QH is stopped or has finished unlinking. */
12162775562aSAlan Stern 		if (status == ECONNRESET) {
12172775562aSAlan Stern 			if (QH_FINISHED_UNLINKING(qh))
12182775562aSAlan Stern 				qh->is_stopped = 1;
12192775562aSAlan Stern 			else if (!qh->is_stopped)
12200ed8fee1SAlan Stern 				return;
12212775562aSAlan Stern 		}
12220ed8fee1SAlan Stern 
12230ed8fee1SAlan Stern 		uhci_giveback_urb(uhci, qh, urb, regs);
12242775562aSAlan Stern 		if (status < 0)
12250ed8fee1SAlan Stern 			break;
12260ed8fee1SAlan Stern 	}
12270ed8fee1SAlan Stern 
12280ed8fee1SAlan Stern 	/* If the QH is neither stopped nor finished unlinking (normal case),
12290ed8fee1SAlan Stern 	 * our work here is done. */
12302775562aSAlan Stern 	if (QH_FINISHED_UNLINKING(qh))
12312775562aSAlan Stern 		qh->is_stopped = 1;
12322775562aSAlan Stern 	else if (!qh->is_stopped)
12330ed8fee1SAlan Stern 		return;
12340ed8fee1SAlan Stern 
12350ed8fee1SAlan Stern 	/* Otherwise give back each of the dequeued URBs */
12362775562aSAlan Stern restart:
12370ed8fee1SAlan Stern 	list_for_each_entry(urbp, &qh->queue, node) {
12380ed8fee1SAlan Stern 		urb = urbp->urb;
12390ed8fee1SAlan Stern 		if (urb->status != -EINPROGRESS) {
12400ed8fee1SAlan Stern 			uhci_save_toggle(qh, urb);
12410ed8fee1SAlan Stern 			uhci_giveback_urb(uhci, qh, urb, regs);
12420ed8fee1SAlan Stern 			goto restart;
12430ed8fee1SAlan Stern 		}
12440ed8fee1SAlan Stern 	}
12450ed8fee1SAlan Stern 	qh->is_stopped = 0;
12460ed8fee1SAlan Stern 
12470ed8fee1SAlan Stern 	/* There are no more dequeued URBs.  If there are still URBs on the
12480ed8fee1SAlan Stern 	 * queue, the QH can now be re-activated. */
12490ed8fee1SAlan Stern 	if (!list_empty(&qh->queue)) {
12500ed8fee1SAlan Stern 		if (qh->needs_fixup)
12510ed8fee1SAlan Stern 			uhci_fixup_toggles(qh, 0);
12520ed8fee1SAlan Stern 		uhci_activate_qh(uhci, qh);
12530ed8fee1SAlan Stern 	}
12540ed8fee1SAlan Stern 
12550ed8fee1SAlan Stern 	/* The queue is empty.  The QH can become idle if it is fully
12560ed8fee1SAlan Stern 	 * unlinked. */
12570ed8fee1SAlan Stern 	else if (QH_FINISHED_UNLINKING(qh))
12580ed8fee1SAlan Stern 		uhci_make_qh_idle(uhci, qh);
12591da177e4SLinus Torvalds }
12601da177e4SLinus Torvalds 
12611da177e4SLinus Torvalds static void uhci_free_pending_tds(struct uhci_hcd *uhci)
12621da177e4SLinus Torvalds {
12631da177e4SLinus Torvalds 	struct uhci_td *td, *tmp;
12641da177e4SLinus Torvalds 
12651da177e4SLinus Torvalds 	list_for_each_entry_safe(td, tmp, &uhci->td_remove_list, remove_list) {
12661da177e4SLinus Torvalds 		list_del_init(&td->remove_list);
12671da177e4SLinus Torvalds 
12681da177e4SLinus Torvalds 		uhci_free_td(uhci, td);
12691da177e4SLinus Torvalds 	}
12701da177e4SLinus Torvalds }
12711da177e4SLinus Torvalds 
12720ed8fee1SAlan Stern /*
12730ed8fee1SAlan Stern  * Process events in the schedule, but only in one thread at a time
12740ed8fee1SAlan Stern  */
12751da177e4SLinus Torvalds static void uhci_scan_schedule(struct uhci_hcd *uhci, struct pt_regs *regs)
12761da177e4SLinus Torvalds {
12770ed8fee1SAlan Stern 	int i;
12780ed8fee1SAlan Stern 	struct uhci_qh *qh;
12791da177e4SLinus Torvalds 
12801da177e4SLinus Torvalds 	/* Don't allow re-entrant calls */
12811da177e4SLinus Torvalds 	if (uhci->scan_in_progress) {
12821da177e4SLinus Torvalds 		uhci->need_rescan = 1;
12831da177e4SLinus Torvalds 		return;
12841da177e4SLinus Torvalds 	}
12851da177e4SLinus Torvalds 	uhci->scan_in_progress = 1;
12861da177e4SLinus Torvalds  rescan:
12871da177e4SLinus Torvalds 	uhci->need_rescan = 0;
12881da177e4SLinus Torvalds 
12896c1b445cSAlan Stern 	uhci_clear_next_interrupt(uhci);
12901da177e4SLinus Torvalds 	uhci_get_current_frame_number(uhci);
12911da177e4SLinus Torvalds 
12921da177e4SLinus Torvalds 	if (uhci->frame_number + uhci->is_stopped != uhci->td_remove_age)
12931da177e4SLinus Torvalds 		uhci_free_pending_tds(uhci);
12941da177e4SLinus Torvalds 
12950ed8fee1SAlan Stern 	/* Go through all the QH queues and process the URBs in each one */
12960ed8fee1SAlan Stern 	for (i = 0; i < UHCI_NUM_SKELQH - 1; ++i) {
12970ed8fee1SAlan Stern 		uhci->next_qh = list_entry(uhci->skelqh[i]->node.next,
12980ed8fee1SAlan Stern 				struct uhci_qh, node);
12990ed8fee1SAlan Stern 		while ((qh = uhci->next_qh) != uhci->skelqh[i]) {
13000ed8fee1SAlan Stern 			uhci->next_qh = list_entry(qh->node.next,
13010ed8fee1SAlan Stern 					struct uhci_qh, node);
13020ed8fee1SAlan Stern 			uhci_scan_qh(uhci, qh, regs);
13031da177e4SLinus Torvalds 		}
13040ed8fee1SAlan Stern 	}
13051da177e4SLinus Torvalds 
13061da177e4SLinus Torvalds 	if (uhci->need_rescan)
13071da177e4SLinus Torvalds 		goto rescan;
13081da177e4SLinus Torvalds 	uhci->scan_in_progress = 0;
13091da177e4SLinus Torvalds 
13100ed8fee1SAlan Stern 	/* If the controller is stopped, we can finish these off right now */
13110ed8fee1SAlan Stern 	if (uhci->is_stopped)
13120ed8fee1SAlan Stern 		uhci_free_pending_tds(uhci);
1313dccf4a48SAlan Stern 
1314dccf4a48SAlan Stern 	if (list_empty(&uhci->td_remove_list) &&
1315dccf4a48SAlan Stern 			list_empty(&uhci->skel_unlink_qh->node))
13161da177e4SLinus Torvalds 		uhci_clear_next_interrupt(uhci);
13171da177e4SLinus Torvalds 	else
13181da177e4SLinus Torvalds 		uhci_set_next_interrupt(uhci);
13191da177e4SLinus Torvalds }
1320f5946f82SAlan Stern 
1321f5946f82SAlan Stern static void check_fsbr(struct uhci_hcd *uhci)
1322f5946f82SAlan Stern {
13230ed8fee1SAlan Stern 	/* For now, don't scan URBs for FSBR timeouts.
13240ed8fee1SAlan Stern 	 * Add it back in later... */
1325f5946f82SAlan Stern 
1326f5946f82SAlan Stern 	/* Really disable FSBR */
1327f5946f82SAlan Stern 	if (!uhci->fsbr && uhci->fsbrtimeout && time_after_eq(jiffies, uhci->fsbrtimeout)) {
1328f5946f82SAlan Stern 		uhci->fsbrtimeout = 0;
1329f5946f82SAlan Stern 		uhci->skel_term_qh->link = UHCI_PTR_TERM;
1330f5946f82SAlan Stern 	}
1331f5946f82SAlan Stern }
1332