xref: /openbmc/linux/drivers/usb/host/uhci-q.c (revision e7e7c360fb07020b24652843aec442325baad0ce)
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)
1617230acdSAlan Stern  * (C) Copyright 2004-2007 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 
4084afddd7SAlan Stern 
4184afddd7SAlan Stern /*
4284afddd7SAlan Stern  * Full-Speed Bandwidth Reclamation (FSBR).
4384afddd7SAlan Stern  * We turn on FSBR whenever a queue that wants it is advancing,
4484afddd7SAlan Stern  * and leave it on for a short time thereafter.
4584afddd7SAlan Stern  */
4684afddd7SAlan Stern static void uhci_fsbr_on(struct uhci_hcd *uhci)
4784afddd7SAlan Stern {
48e009f1b2SAlan Stern 	struct uhci_qh *lqh;
4917230acdSAlan Stern 
50e009f1b2SAlan Stern 	/* The terminating skeleton QH always points back to the first
51e009f1b2SAlan Stern 	 * FSBR QH.  Make the last async QH point to the terminating
52e009f1b2SAlan Stern 	 * skeleton QH. */
5384afddd7SAlan Stern 	uhci->fsbr_is_on = 1;
5417230acdSAlan Stern 	lqh = list_entry(uhci->skel_async_qh->node.prev,
5517230acdSAlan Stern 			struct uhci_qh, node);
56e009f1b2SAlan Stern 	lqh->link = LINK_TO_QH(uhci->skel_term_qh);
5784afddd7SAlan Stern }
5884afddd7SAlan Stern 
5984afddd7SAlan Stern static void uhci_fsbr_off(struct uhci_hcd *uhci)
6084afddd7SAlan Stern {
6117230acdSAlan Stern 	struct uhci_qh *lqh;
6217230acdSAlan Stern 
63e009f1b2SAlan Stern 	/* Remove the link from the last async QH to the terminating
64e009f1b2SAlan Stern 	 * skeleton QH. */
6584afddd7SAlan Stern 	uhci->fsbr_is_on = 0;
6617230acdSAlan Stern 	lqh = list_entry(uhci->skel_async_qh->node.prev,
6717230acdSAlan Stern 			struct uhci_qh, node);
68e009f1b2SAlan Stern 	lqh->link = UHCI_PTR_TERM;
6984afddd7SAlan Stern }
7084afddd7SAlan Stern 
7184afddd7SAlan Stern static void uhci_add_fsbr(struct uhci_hcd *uhci, struct urb *urb)
7284afddd7SAlan Stern {
7384afddd7SAlan Stern 	struct urb_priv *urbp = urb->hcpriv;
7484afddd7SAlan Stern 
7584afddd7SAlan Stern 	if (!(urb->transfer_flags & URB_NO_FSBR))
7684afddd7SAlan Stern 		urbp->fsbr = 1;
7784afddd7SAlan Stern }
7884afddd7SAlan Stern 
79c5e3b741SAlan Stern static void uhci_urbp_wants_fsbr(struct uhci_hcd *uhci, struct urb_priv *urbp)
8084afddd7SAlan Stern {
8184afddd7SAlan Stern 	if (urbp->fsbr) {
82c5e3b741SAlan Stern 		uhci->fsbr_is_wanted = 1;
8384afddd7SAlan Stern 		if (!uhci->fsbr_is_on)
8484afddd7SAlan Stern 			uhci_fsbr_on(uhci);
85c5e3b741SAlan Stern 		else if (uhci->fsbr_expiring) {
86c5e3b741SAlan Stern 			uhci->fsbr_expiring = 0;
87c5e3b741SAlan Stern 			del_timer(&uhci->fsbr_timer);
8884afddd7SAlan Stern 		}
8984afddd7SAlan Stern 	}
90c5e3b741SAlan Stern }
91c5e3b741SAlan Stern 
92c5e3b741SAlan Stern static void uhci_fsbr_timeout(unsigned long _uhci)
93c5e3b741SAlan Stern {
94c5e3b741SAlan Stern 	struct uhci_hcd *uhci = (struct uhci_hcd *) _uhci;
95c5e3b741SAlan Stern 	unsigned long flags;
96c5e3b741SAlan Stern 
97c5e3b741SAlan Stern 	spin_lock_irqsave(&uhci->lock, flags);
98c5e3b741SAlan Stern 	if (uhci->fsbr_expiring) {
99c5e3b741SAlan Stern 		uhci->fsbr_expiring = 0;
100c5e3b741SAlan Stern 		uhci_fsbr_off(uhci);
101c5e3b741SAlan Stern 	}
102c5e3b741SAlan Stern 	spin_unlock_irqrestore(&uhci->lock, flags);
103c5e3b741SAlan Stern }
10484afddd7SAlan Stern 
10584afddd7SAlan Stern 
1062532178aSAlan Stern static struct uhci_td *uhci_alloc_td(struct uhci_hcd *uhci)
1071da177e4SLinus Torvalds {
1081da177e4SLinus Torvalds 	dma_addr_t dma_handle;
1091da177e4SLinus Torvalds 	struct uhci_td *td;
1101da177e4SLinus Torvalds 
1111da177e4SLinus Torvalds 	td = dma_pool_alloc(uhci->td_pool, GFP_ATOMIC, &dma_handle);
1121da177e4SLinus Torvalds 	if (!td)
1131da177e4SLinus Torvalds 		return NULL;
1141da177e4SLinus Torvalds 
1151da177e4SLinus Torvalds 	td->dma_handle = dma_handle;
1161da177e4SLinus Torvalds 	td->frame = -1;
1171da177e4SLinus Torvalds 
1181da177e4SLinus Torvalds 	INIT_LIST_HEAD(&td->list);
1191da177e4SLinus Torvalds 	INIT_LIST_HEAD(&td->fl_list);
1201da177e4SLinus Torvalds 
1211da177e4SLinus Torvalds 	return td;
1221da177e4SLinus Torvalds }
1231da177e4SLinus Torvalds 
124dccf4a48SAlan Stern static void uhci_free_td(struct uhci_hcd *uhci, struct uhci_td *td)
125dccf4a48SAlan Stern {
1260cef7727SAlan Stern 	if (!list_empty(&td->list)) {
127dccf4a48SAlan Stern 		dev_warn(uhci_dev(uhci), "td %p still in list!\n", td);
1280cef7727SAlan Stern 		WARN_ON(1);
1290cef7727SAlan Stern 	}
1300cef7727SAlan Stern 	if (!list_empty(&td->fl_list)) {
131dccf4a48SAlan Stern 		dev_warn(uhci_dev(uhci), "td %p still in fl_list!\n", td);
1320cef7727SAlan Stern 		WARN_ON(1);
1330cef7727SAlan Stern 	}
134dccf4a48SAlan Stern 
135dccf4a48SAlan Stern 	dma_pool_free(uhci->td_pool, td, td->dma_handle);
136dccf4a48SAlan Stern }
137dccf4a48SAlan Stern 
1381da177e4SLinus Torvalds static inline void uhci_fill_td(struct uhci_td *td, u32 status,
1391da177e4SLinus Torvalds 		u32 token, u32 buffer)
1401da177e4SLinus Torvalds {
1411da177e4SLinus Torvalds 	td->status = cpu_to_le32(status);
1421da177e4SLinus Torvalds 	td->token = cpu_to_le32(token);
1431da177e4SLinus Torvalds 	td->buffer = cpu_to_le32(buffer);
1441da177e4SLinus Torvalds }
1451da177e4SLinus Torvalds 
14604538a25SAlan Stern static void uhci_add_td_to_urbp(struct uhci_td *td, struct urb_priv *urbp)
14704538a25SAlan Stern {
14804538a25SAlan Stern 	list_add_tail(&td->list, &urbp->td_list);
14904538a25SAlan Stern }
15004538a25SAlan Stern 
15104538a25SAlan Stern static void uhci_remove_td_from_urbp(struct uhci_td *td)
15204538a25SAlan Stern {
15304538a25SAlan Stern 	list_del_init(&td->list);
15404538a25SAlan Stern }
15504538a25SAlan Stern 
1561da177e4SLinus Torvalds /*
157687f5f34SAlan Stern  * We insert Isochronous URBs directly into the frame list at the beginning
1581da177e4SLinus Torvalds  */
159dccf4a48SAlan Stern static inline void uhci_insert_td_in_frame_list(struct uhci_hcd *uhci,
160dccf4a48SAlan Stern 		struct uhci_td *td, unsigned framenum)
1611da177e4SLinus Torvalds {
1621da177e4SLinus Torvalds 	framenum &= (UHCI_NUMFRAMES - 1);
1631da177e4SLinus Torvalds 
1641da177e4SLinus Torvalds 	td->frame = framenum;
1651da177e4SLinus Torvalds 
1661da177e4SLinus Torvalds 	/* Is there a TD already mapped there? */
167a1d59ce8SAlan Stern 	if (uhci->frame_cpu[framenum]) {
1681da177e4SLinus Torvalds 		struct uhci_td *ftd, *ltd;
1691da177e4SLinus Torvalds 
170a1d59ce8SAlan Stern 		ftd = uhci->frame_cpu[framenum];
1711da177e4SLinus Torvalds 		ltd = list_entry(ftd->fl_list.prev, struct uhci_td, fl_list);
1721da177e4SLinus Torvalds 
1731da177e4SLinus Torvalds 		list_add_tail(&td->fl_list, &ftd->fl_list);
1741da177e4SLinus Torvalds 
1751da177e4SLinus Torvalds 		td->link = ltd->link;
1761da177e4SLinus Torvalds 		wmb();
17728b9325eSAlan Stern 		ltd->link = LINK_TO_TD(td);
1781da177e4SLinus Torvalds 	} else {
179a1d59ce8SAlan Stern 		td->link = uhci->frame[framenum];
1801da177e4SLinus Torvalds 		wmb();
18128b9325eSAlan Stern 		uhci->frame[framenum] = LINK_TO_TD(td);
182a1d59ce8SAlan Stern 		uhci->frame_cpu[framenum] = td;
1831da177e4SLinus Torvalds 	}
1841da177e4SLinus Torvalds }
1851da177e4SLinus Torvalds 
186dccf4a48SAlan Stern static inline void uhci_remove_td_from_frame_list(struct uhci_hcd *uhci,
187b81d3436SAlan Stern 		struct uhci_td *td)
1881da177e4SLinus Torvalds {
1891da177e4SLinus Torvalds 	/* If it's not inserted, don't remove it */
190b81d3436SAlan Stern 	if (td->frame == -1) {
191b81d3436SAlan Stern 		WARN_ON(!list_empty(&td->fl_list));
1921da177e4SLinus Torvalds 		return;
193b81d3436SAlan Stern 	}
1941da177e4SLinus Torvalds 
195b81d3436SAlan Stern 	if (uhci->frame_cpu[td->frame] == td) {
1961da177e4SLinus Torvalds 		if (list_empty(&td->fl_list)) {
197a1d59ce8SAlan Stern 			uhci->frame[td->frame] = td->link;
198a1d59ce8SAlan Stern 			uhci->frame_cpu[td->frame] = NULL;
1991da177e4SLinus Torvalds 		} else {
2001da177e4SLinus Torvalds 			struct uhci_td *ntd;
2011da177e4SLinus Torvalds 
2021da177e4SLinus Torvalds 			ntd = list_entry(td->fl_list.next, struct uhci_td, fl_list);
20328b9325eSAlan Stern 			uhci->frame[td->frame] = LINK_TO_TD(ntd);
204a1d59ce8SAlan Stern 			uhci->frame_cpu[td->frame] = ntd;
2051da177e4SLinus Torvalds 		}
2061da177e4SLinus Torvalds 	} else {
2071da177e4SLinus Torvalds 		struct uhci_td *ptd;
2081da177e4SLinus Torvalds 
2091da177e4SLinus Torvalds 		ptd = list_entry(td->fl_list.prev, struct uhci_td, fl_list);
2101da177e4SLinus Torvalds 		ptd->link = td->link;
2111da177e4SLinus Torvalds 	}
2121da177e4SLinus Torvalds 
2131da177e4SLinus Torvalds 	list_del_init(&td->fl_list);
2141da177e4SLinus Torvalds 	td->frame = -1;
2151da177e4SLinus Torvalds }
2161da177e4SLinus Torvalds 
217c8155cc5SAlan Stern static inline void uhci_remove_tds_from_frame(struct uhci_hcd *uhci,
218c8155cc5SAlan Stern 		unsigned int framenum)
219c8155cc5SAlan Stern {
220c8155cc5SAlan Stern 	struct uhci_td *ftd, *ltd;
221c8155cc5SAlan Stern 
222c8155cc5SAlan Stern 	framenum &= (UHCI_NUMFRAMES - 1);
223c8155cc5SAlan Stern 
224c8155cc5SAlan Stern 	ftd = uhci->frame_cpu[framenum];
225c8155cc5SAlan Stern 	if (ftd) {
226c8155cc5SAlan Stern 		ltd = list_entry(ftd->fl_list.prev, struct uhci_td, fl_list);
227c8155cc5SAlan Stern 		uhci->frame[framenum] = ltd->link;
228c8155cc5SAlan Stern 		uhci->frame_cpu[framenum] = NULL;
229c8155cc5SAlan Stern 
230c8155cc5SAlan Stern 		while (!list_empty(&ftd->fl_list))
231c8155cc5SAlan Stern 			list_del_init(ftd->fl_list.prev);
232c8155cc5SAlan Stern 	}
233c8155cc5SAlan Stern }
234c8155cc5SAlan Stern 
235dccf4a48SAlan Stern /*
236dccf4a48SAlan Stern  * Remove all the TDs for an Isochronous URB from the frame list
237dccf4a48SAlan Stern  */
238dccf4a48SAlan Stern static void uhci_unlink_isochronous_tds(struct uhci_hcd *uhci, struct urb *urb)
239b81d3436SAlan Stern {
240b81d3436SAlan Stern 	struct urb_priv *urbp = (struct urb_priv *) urb->hcpriv;
241b81d3436SAlan Stern 	struct uhci_td *td;
242b81d3436SAlan Stern 
243b81d3436SAlan Stern 	list_for_each_entry(td, &urbp->td_list, list)
244dccf4a48SAlan Stern 		uhci_remove_td_from_frame_list(uhci, td);
245b81d3436SAlan Stern }
246b81d3436SAlan Stern 
247dccf4a48SAlan Stern static struct uhci_qh *uhci_alloc_qh(struct uhci_hcd *uhci,
248dccf4a48SAlan Stern 		struct usb_device *udev, struct usb_host_endpoint *hep)
2491da177e4SLinus Torvalds {
2501da177e4SLinus Torvalds 	dma_addr_t dma_handle;
2511da177e4SLinus Torvalds 	struct uhci_qh *qh;
2521da177e4SLinus Torvalds 
2531da177e4SLinus Torvalds 	qh = dma_pool_alloc(uhci->qh_pool, GFP_ATOMIC, &dma_handle);
2541da177e4SLinus Torvalds 	if (!qh)
2551da177e4SLinus Torvalds 		return NULL;
2561da177e4SLinus Torvalds 
25759e29ed9SAlan Stern 	memset(qh, 0, sizeof(*qh));
2581da177e4SLinus Torvalds 	qh->dma_handle = dma_handle;
2591da177e4SLinus Torvalds 
2601da177e4SLinus Torvalds 	qh->element = UHCI_PTR_TERM;
2611da177e4SLinus Torvalds 	qh->link = UHCI_PTR_TERM;
2621da177e4SLinus Torvalds 
263dccf4a48SAlan Stern 	INIT_LIST_HEAD(&qh->queue);
264dccf4a48SAlan Stern 	INIT_LIST_HEAD(&qh->node);
2651da177e4SLinus Torvalds 
266dccf4a48SAlan Stern 	if (udev) {		/* Normal QH */
26785a975d0SAlan Stern 		qh->type = hep->desc.bmAttributes & USB_ENDPOINT_XFERTYPE_MASK;
26885a975d0SAlan Stern 		if (qh->type != USB_ENDPOINT_XFER_ISOC) {
269af0bb599SAlan Stern 			qh->dummy_td = uhci_alloc_td(uhci);
270af0bb599SAlan Stern 			if (!qh->dummy_td) {
271af0bb599SAlan Stern 				dma_pool_free(uhci->qh_pool, qh, dma_handle);
272af0bb599SAlan Stern 				return NULL;
273af0bb599SAlan Stern 			}
27485a975d0SAlan Stern 		}
275dccf4a48SAlan Stern 		qh->state = QH_STATE_IDLE;
276dccf4a48SAlan Stern 		qh->hep = hep;
277dccf4a48SAlan Stern 		qh->udev = udev;
278dccf4a48SAlan Stern 		hep->hcpriv = qh;
2791da177e4SLinus Torvalds 
2803ca2a321SAlan Stern 		if (qh->type == USB_ENDPOINT_XFER_INT ||
2813ca2a321SAlan Stern 				qh->type == USB_ENDPOINT_XFER_ISOC)
2823ca2a321SAlan Stern 			qh->load = usb_calc_bus_time(udev->speed,
2833ca2a321SAlan Stern 					usb_endpoint_dir_in(&hep->desc),
2843ca2a321SAlan Stern 					qh->type == USB_ENDPOINT_XFER_ISOC,
2853ca2a321SAlan Stern 					le16_to_cpu(hep->desc.wMaxPacketSize))
2863ca2a321SAlan Stern 				/ 1000 + 1;
2873ca2a321SAlan Stern 
288dccf4a48SAlan Stern 	} else {		/* Skeleton QH */
289dccf4a48SAlan Stern 		qh->state = QH_STATE_ACTIVE;
2904de7d2c2SAlan Stern 		qh->type = -1;
291dccf4a48SAlan Stern 	}
2921da177e4SLinus Torvalds 	return qh;
2931da177e4SLinus Torvalds }
2941da177e4SLinus Torvalds 
2951da177e4SLinus Torvalds static void uhci_free_qh(struct uhci_hcd *uhci, struct uhci_qh *qh)
2961da177e4SLinus Torvalds {
297dccf4a48SAlan Stern 	WARN_ON(qh->state != QH_STATE_IDLE && qh->udev);
2980cef7727SAlan Stern 	if (!list_empty(&qh->queue)) {
2991da177e4SLinus Torvalds 		dev_warn(uhci_dev(uhci), "qh %p list not empty!\n", qh);
3000cef7727SAlan Stern 		WARN_ON(1);
3010cef7727SAlan Stern 	}
3021da177e4SLinus Torvalds 
303dccf4a48SAlan Stern 	list_del(&qh->node);
304dccf4a48SAlan Stern 	if (qh->udev) {
305dccf4a48SAlan Stern 		qh->hep->hcpriv = NULL;
30685a975d0SAlan Stern 		if (qh->dummy_td)
307af0bb599SAlan Stern 			uhci_free_td(uhci, qh->dummy_td);
308dccf4a48SAlan Stern 	}
3091da177e4SLinus Torvalds 	dma_pool_free(uhci->qh_pool, qh, qh->dma_handle);
3101da177e4SLinus Torvalds }
3111da177e4SLinus Torvalds 
3121da177e4SLinus Torvalds /*
313a0b458b6SAlan Stern  * When a queue is stopped and a dequeued URB is given back, adjust
314a0b458b6SAlan Stern  * the previous TD link (if the URB isn't first on the queue) or
315a0b458b6SAlan Stern  * save its toggle value (if it is first and is currently executing).
31610b8e47dSAlan Stern  *
31710b8e47dSAlan Stern  * Returns 0 if the URB should not yet be given back, 1 otherwise.
3180ed8fee1SAlan Stern  */
31910b8e47dSAlan Stern static int uhci_cleanup_queue(struct uhci_hcd *uhci, struct uhci_qh *qh,
320a0b458b6SAlan Stern 		struct urb *urb)
3210ed8fee1SAlan Stern {
322a0b458b6SAlan Stern 	struct urb_priv *urbp = urb->hcpriv;
3230ed8fee1SAlan Stern 	struct uhci_td *td;
32410b8e47dSAlan Stern 	int ret = 1;
3250ed8fee1SAlan Stern 
326a0b458b6SAlan Stern 	/* Isochronous pipes don't use toggles and their TD link pointers
32710b8e47dSAlan Stern 	 * get adjusted during uhci_urb_dequeue().  But since their queues
32810b8e47dSAlan Stern 	 * cannot truly be stopped, we have to watch out for dequeues
32910b8e47dSAlan Stern 	 * occurring after the nominal unlink frame. */
33010b8e47dSAlan Stern 	if (qh->type == USB_ENDPOINT_XFER_ISOC) {
33110b8e47dSAlan Stern 		ret = (uhci->frame_number + uhci->is_stopped !=
33210b8e47dSAlan Stern 				qh->unlink_frame);
333c5e3b741SAlan Stern 		goto done;
33410b8e47dSAlan Stern 	}
335a0b458b6SAlan Stern 
336a0b458b6SAlan Stern 	/* If the URB isn't first on its queue, adjust the link pointer
337a0b458b6SAlan Stern 	 * of the last TD in the previous URB.  The toggle doesn't need
338a0b458b6SAlan Stern 	 * to be saved since this URB can't be executing yet. */
339a0b458b6SAlan Stern 	if (qh->queue.next != &urbp->node) {
340a0b458b6SAlan Stern 		struct urb_priv *purbp;
341a0b458b6SAlan Stern 		struct uhci_td *ptd;
342a0b458b6SAlan Stern 
343a0b458b6SAlan Stern 		purbp = list_entry(urbp->node.prev, struct urb_priv, node);
344a0b458b6SAlan Stern 		WARN_ON(list_empty(&purbp->td_list));
345a0b458b6SAlan Stern 		ptd = list_entry(purbp->td_list.prev, struct uhci_td,
346a0b458b6SAlan Stern 				list);
347a0b458b6SAlan Stern 		td = list_entry(urbp->td_list.prev, struct uhci_td,
348a0b458b6SAlan Stern 				list);
349a0b458b6SAlan Stern 		ptd->link = td->link;
350c5e3b741SAlan Stern 		goto done;
351a0b458b6SAlan Stern 	}
352a0b458b6SAlan Stern 
3530ed8fee1SAlan Stern 	/* If the QH element pointer is UHCI_PTR_TERM then then currently
3540ed8fee1SAlan Stern 	 * executing URB has already been unlinked, so this one isn't it. */
355a0b458b6SAlan Stern 	if (qh_element(qh) == UHCI_PTR_TERM)
356c5e3b741SAlan Stern 		goto done;
3570ed8fee1SAlan Stern 	qh->element = UHCI_PTR_TERM;
3580ed8fee1SAlan Stern 
35985a975d0SAlan Stern 	/* Control pipes don't have to worry about toggles */
360a0b458b6SAlan Stern 	if (qh->type == USB_ENDPOINT_XFER_CONTROL)
361c5e3b741SAlan Stern 		goto done;
3620ed8fee1SAlan Stern 
363a0b458b6SAlan Stern 	/* Save the next toggle value */
36459e29ed9SAlan Stern 	WARN_ON(list_empty(&urbp->td_list));
36559e29ed9SAlan Stern 	td = list_entry(urbp->td_list.next, struct uhci_td, list);
3660ed8fee1SAlan Stern 	qh->needs_fixup = 1;
3670ed8fee1SAlan Stern 	qh->initial_toggle = uhci_toggle(td_token(td));
368c5e3b741SAlan Stern 
369c5e3b741SAlan Stern done:
37010b8e47dSAlan Stern 	return ret;
3710ed8fee1SAlan Stern }
3720ed8fee1SAlan Stern 
3730ed8fee1SAlan Stern /*
3740ed8fee1SAlan Stern  * Fix up the data toggles for URBs in a queue, when one of them
3750ed8fee1SAlan Stern  * terminates early (short transfer, error, or dequeued).
3760ed8fee1SAlan Stern  */
3770ed8fee1SAlan Stern static void uhci_fixup_toggles(struct uhci_qh *qh, int skip_first)
3780ed8fee1SAlan Stern {
3790ed8fee1SAlan Stern 	struct urb_priv *urbp = NULL;
3800ed8fee1SAlan Stern 	struct uhci_td *td;
3810ed8fee1SAlan Stern 	unsigned int toggle = qh->initial_toggle;
3820ed8fee1SAlan Stern 	unsigned int pipe;
3830ed8fee1SAlan Stern 
3840ed8fee1SAlan Stern 	/* Fixups for a short transfer start with the second URB in the
3850ed8fee1SAlan Stern 	 * queue (the short URB is the first). */
3860ed8fee1SAlan Stern 	if (skip_first)
3870ed8fee1SAlan Stern 		urbp = list_entry(qh->queue.next, struct urb_priv, node);
3880ed8fee1SAlan Stern 
3890ed8fee1SAlan Stern 	/* When starting with the first URB, if the QH element pointer is
3900ed8fee1SAlan Stern 	 * still valid then we know the URB's toggles are okay. */
3910ed8fee1SAlan Stern 	else if (qh_element(qh) != UHCI_PTR_TERM)
3920ed8fee1SAlan Stern 		toggle = 2;
3930ed8fee1SAlan Stern 
3940ed8fee1SAlan Stern 	/* Fix up the toggle for the URBs in the queue.  Normally this
3950ed8fee1SAlan Stern 	 * loop won't run more than once: When an error or short transfer
3960ed8fee1SAlan Stern 	 * occurs, the queue usually gets emptied. */
3971393adb2SAlan Stern 	urbp = list_prepare_entry(urbp, &qh->queue, node);
3980ed8fee1SAlan Stern 	list_for_each_entry_continue(urbp, &qh->queue, node) {
3990ed8fee1SAlan Stern 
4000ed8fee1SAlan Stern 		/* If the first TD has the right toggle value, we don't
4010ed8fee1SAlan Stern 		 * need to change any toggles in this URB */
4020ed8fee1SAlan Stern 		td = list_entry(urbp->td_list.next, struct uhci_td, list);
4030ed8fee1SAlan Stern 		if (toggle > 1 || uhci_toggle(td_token(td)) == toggle) {
404db59b464SAlan Stern 			td = list_entry(urbp->td_list.prev, struct uhci_td,
4050ed8fee1SAlan Stern 					list);
4060ed8fee1SAlan Stern 			toggle = uhci_toggle(td_token(td)) ^ 1;
4070ed8fee1SAlan Stern 
4080ed8fee1SAlan Stern 		/* Otherwise all the toggles in the URB have to be switched */
4090ed8fee1SAlan Stern 		} else {
4100ed8fee1SAlan Stern 			list_for_each_entry(td, &urbp->td_list, list) {
4110ed8fee1SAlan Stern 				td->token ^= __constant_cpu_to_le32(
4120ed8fee1SAlan Stern 							TD_TOKEN_TOGGLE);
4130ed8fee1SAlan Stern 				toggle ^= 1;
4140ed8fee1SAlan Stern 			}
4150ed8fee1SAlan Stern 		}
4160ed8fee1SAlan Stern 	}
4170ed8fee1SAlan Stern 
4180ed8fee1SAlan Stern 	wmb();
4190ed8fee1SAlan Stern 	pipe = list_entry(qh->queue.next, struct urb_priv, node)->urb->pipe;
4200ed8fee1SAlan Stern 	usb_settoggle(qh->udev, usb_pipeendpoint(pipe),
4210ed8fee1SAlan Stern 			usb_pipeout(pipe), toggle);
4220ed8fee1SAlan Stern 	qh->needs_fixup = 0;
4230ed8fee1SAlan Stern }
4240ed8fee1SAlan Stern 
4250ed8fee1SAlan Stern /*
42617230acdSAlan Stern  * Link an Isochronous QH into its skeleton's list
42717230acdSAlan Stern  */
42817230acdSAlan Stern static inline void link_iso(struct uhci_hcd *uhci, struct uhci_qh *qh)
42917230acdSAlan Stern {
43017230acdSAlan Stern 	list_add_tail(&qh->node, &uhci->skel_iso_qh->node);
43117230acdSAlan Stern 
43217230acdSAlan Stern 	/* Isochronous QHs aren't linked by the hardware */
43317230acdSAlan Stern }
43417230acdSAlan Stern 
43517230acdSAlan Stern /*
43617230acdSAlan Stern  * Link a high-period interrupt QH into the schedule at the end of its
43717230acdSAlan Stern  * skeleton's list
43817230acdSAlan Stern  */
43917230acdSAlan Stern static void link_interrupt(struct uhci_hcd *uhci, struct uhci_qh *qh)
44017230acdSAlan Stern {
44117230acdSAlan Stern 	struct uhci_qh *pqh;
44217230acdSAlan Stern 
44317230acdSAlan Stern 	list_add_tail(&qh->node, &uhci->skelqh[qh->skel]->node);
44417230acdSAlan Stern 
44517230acdSAlan Stern 	pqh = list_entry(qh->node.prev, struct uhci_qh, node);
44617230acdSAlan Stern 	qh->link = pqh->link;
44717230acdSAlan Stern 	wmb();
44817230acdSAlan Stern 	pqh->link = LINK_TO_QH(qh);
44917230acdSAlan Stern }
45017230acdSAlan Stern 
45117230acdSAlan Stern /*
45217230acdSAlan Stern  * Link a period-1 interrupt or async QH into the schedule at the
45317230acdSAlan Stern  * correct spot in the async skeleton's list, and update the FSBR link
45417230acdSAlan Stern  */
45517230acdSAlan Stern static void link_async(struct uhci_hcd *uhci, struct uhci_qh *qh)
45617230acdSAlan Stern {
457e009f1b2SAlan Stern 	struct uhci_qh *pqh;
45817230acdSAlan Stern 	__le32 link_to_new_qh;
45917230acdSAlan Stern 
46017230acdSAlan Stern 	/* Find the predecessor QH for our new one and insert it in the list.
46117230acdSAlan Stern 	 * The list of QHs is expected to be short, so linear search won't
46217230acdSAlan Stern 	 * take too long. */
46317230acdSAlan Stern 	list_for_each_entry_reverse(pqh, &uhci->skel_async_qh->node, node) {
46417230acdSAlan Stern 		if (pqh->skel <= qh->skel)
46517230acdSAlan Stern 			break;
46617230acdSAlan Stern 	}
46717230acdSAlan Stern 	list_add(&qh->node, &pqh->node);
46817230acdSAlan Stern 
46917230acdSAlan Stern 	/* Link it into the schedule */
470e009f1b2SAlan Stern 	qh->link = pqh->link;
47117230acdSAlan Stern 	wmb();
472e009f1b2SAlan Stern 	link_to_new_qh = LINK_TO_QH(qh);
473e009f1b2SAlan Stern 	pqh->link = link_to_new_qh;
474e009f1b2SAlan Stern 
475e009f1b2SAlan Stern 	/* If this is now the first FSBR QH, link the terminating skeleton
476e009f1b2SAlan Stern 	 * QH to it. */
477e009f1b2SAlan Stern 	if (pqh->skel < SKEL_FSBR && qh->skel >= SKEL_FSBR)
478e009f1b2SAlan Stern 		uhci->skel_term_qh->link = link_to_new_qh;
47917230acdSAlan Stern }
48017230acdSAlan Stern 
48117230acdSAlan Stern /*
482dccf4a48SAlan Stern  * Put a QH on the schedule in both hardware and software
4831da177e4SLinus Torvalds  */
484dccf4a48SAlan Stern static void uhci_activate_qh(struct uhci_hcd *uhci, struct uhci_qh *qh)
4851da177e4SLinus Torvalds {
486dccf4a48SAlan Stern 	WARN_ON(list_empty(&qh->queue));
487dccf4a48SAlan Stern 
488dccf4a48SAlan Stern 	/* Set the element pointer if it isn't set already.
489dccf4a48SAlan Stern 	 * This isn't needed for Isochronous queues, but it doesn't hurt. */
490dccf4a48SAlan Stern 	if (qh_element(qh) == UHCI_PTR_TERM) {
491dccf4a48SAlan Stern 		struct urb_priv *urbp = list_entry(qh->queue.next,
492dccf4a48SAlan Stern 				struct urb_priv, node);
493dccf4a48SAlan Stern 		struct uhci_td *td = list_entry(urbp->td_list.next,
494dccf4a48SAlan Stern 				struct uhci_td, list);
495dccf4a48SAlan Stern 
49628b9325eSAlan Stern 		qh->element = LINK_TO_TD(td);
497dccf4a48SAlan Stern 	}
498dccf4a48SAlan Stern 
49984afddd7SAlan Stern 	/* Treat the queue as if it has just advanced */
50084afddd7SAlan Stern 	qh->wait_expired = 0;
50184afddd7SAlan Stern 	qh->advance_jiffies = jiffies;
50284afddd7SAlan Stern 
503dccf4a48SAlan Stern 	if (qh->state == QH_STATE_ACTIVE)
5041da177e4SLinus Torvalds 		return;
505dccf4a48SAlan Stern 	qh->state = QH_STATE_ACTIVE;
506dccf4a48SAlan Stern 
50717230acdSAlan Stern 	/* Move the QH from its old list to the correct spot in the appropriate
508dccf4a48SAlan Stern 	 * skeleton's list */
5090ed8fee1SAlan Stern 	if (qh == uhci->next_qh)
5100ed8fee1SAlan Stern 		uhci->next_qh = list_entry(qh->node.next, struct uhci_qh,
5110ed8fee1SAlan Stern 				node);
51217230acdSAlan Stern 	list_del(&qh->node);
513dccf4a48SAlan Stern 
51417230acdSAlan Stern 	if (qh->skel == SKEL_ISO)
51517230acdSAlan Stern 		link_iso(uhci, qh);
51617230acdSAlan Stern 	else if (qh->skel < SKEL_ASYNC)
51717230acdSAlan Stern 		link_interrupt(uhci, qh);
51817230acdSAlan Stern 	else
51917230acdSAlan Stern 		link_async(uhci, qh);
52017230acdSAlan Stern }
52117230acdSAlan Stern 
52217230acdSAlan Stern /*
52317230acdSAlan Stern  * Unlink a high-period interrupt QH from the schedule
52417230acdSAlan Stern  */
52517230acdSAlan Stern static void unlink_interrupt(struct uhci_hcd *uhci, struct uhci_qh *qh)
52617230acdSAlan Stern {
52717230acdSAlan Stern 	struct uhci_qh *pqh;
52817230acdSAlan Stern 
529dccf4a48SAlan Stern 	pqh = list_entry(qh->node.prev, struct uhci_qh, node);
53017230acdSAlan Stern 	pqh->link = qh->link;
53117230acdSAlan Stern 	mb();
53217230acdSAlan Stern }
53317230acdSAlan Stern 
53417230acdSAlan Stern /*
53517230acdSAlan Stern  * Unlink a period-1 interrupt or async QH from the schedule
53617230acdSAlan Stern  */
53717230acdSAlan Stern static void unlink_async(struct uhci_hcd *uhci, struct uhci_qh *qh)
53817230acdSAlan Stern {
539e009f1b2SAlan Stern 	struct uhci_qh *pqh;
54017230acdSAlan Stern 	__le32 link_to_next_qh = qh->link;
54117230acdSAlan Stern 
54217230acdSAlan Stern 	pqh = list_entry(qh->node.prev, struct uhci_qh, node);
54317230acdSAlan Stern 	pqh->link = link_to_next_qh;
544e009f1b2SAlan Stern 
545e009f1b2SAlan Stern 	/* If this was the old first FSBR QH, link the terminating skeleton
546e009f1b2SAlan Stern 	 * QH to the next (new first FSBR) QH. */
547e009f1b2SAlan Stern 	if (pqh->skel < SKEL_FSBR && qh->skel >= SKEL_FSBR)
548e009f1b2SAlan Stern 		uhci->skel_term_qh->link = link_to_next_qh;
54917230acdSAlan Stern 	mb();
550dccf4a48SAlan Stern }
5511da177e4SLinus Torvalds 
5521da177e4SLinus Torvalds /*
553dccf4a48SAlan Stern  * Take a QH off the hardware schedule
5541da177e4SLinus Torvalds  */
555dccf4a48SAlan Stern static void uhci_unlink_qh(struct uhci_hcd *uhci, struct uhci_qh *qh)
556dccf4a48SAlan Stern {
557dccf4a48SAlan Stern 	if (qh->state == QH_STATE_UNLINKING)
558dccf4a48SAlan Stern 		return;
559dccf4a48SAlan Stern 	WARN_ON(qh->state != QH_STATE_ACTIVE || !qh->udev);
560dccf4a48SAlan Stern 	qh->state = QH_STATE_UNLINKING;
5611da177e4SLinus Torvalds 
562dccf4a48SAlan Stern 	/* Unlink the QH from the schedule and record when we did it */
56317230acdSAlan Stern 	if (qh->skel == SKEL_ISO)
56417230acdSAlan Stern 		;
56517230acdSAlan Stern 	else if (qh->skel < SKEL_ASYNC)
56617230acdSAlan Stern 		unlink_interrupt(uhci, qh);
56717230acdSAlan Stern 	else
56817230acdSAlan Stern 		unlink_async(uhci, qh);
5691da177e4SLinus Torvalds 
5701da177e4SLinus Torvalds 	uhci_get_current_frame_number(uhci);
571dccf4a48SAlan Stern 	qh->unlink_frame = uhci->frame_number;
5721da177e4SLinus Torvalds 
573dccf4a48SAlan Stern 	/* Force an interrupt so we know when the QH is fully unlinked */
574dccf4a48SAlan Stern 	if (list_empty(&uhci->skel_unlink_qh->node))
5751da177e4SLinus Torvalds 		uhci_set_next_interrupt(uhci);
5761da177e4SLinus Torvalds 
577dccf4a48SAlan Stern 	/* Move the QH from its old list to the end of the unlinking list */
5780ed8fee1SAlan Stern 	if (qh == uhci->next_qh)
5790ed8fee1SAlan Stern 		uhci->next_qh = list_entry(qh->node.next, struct uhci_qh,
5800ed8fee1SAlan Stern 				node);
581dccf4a48SAlan Stern 	list_move_tail(&qh->node, &uhci->skel_unlink_qh->node);
5821da177e4SLinus Torvalds }
5831da177e4SLinus Torvalds 
5841da177e4SLinus Torvalds /*
585dccf4a48SAlan Stern  * When we and the controller are through with a QH, it becomes IDLE.
586dccf4a48SAlan Stern  * This happens when a QH has been off the schedule (on the unlinking
587dccf4a48SAlan Stern  * list) for more than one frame, or when an error occurs while adding
588dccf4a48SAlan Stern  * the first URB onto a new QH.
5891da177e4SLinus Torvalds  */
590dccf4a48SAlan Stern static void uhci_make_qh_idle(struct uhci_hcd *uhci, struct uhci_qh *qh)
591dccf4a48SAlan Stern {
592dccf4a48SAlan Stern 	WARN_ON(qh->state == QH_STATE_ACTIVE);
593dccf4a48SAlan Stern 
5940ed8fee1SAlan Stern 	if (qh == uhci->next_qh)
5950ed8fee1SAlan Stern 		uhci->next_qh = list_entry(qh->node.next, struct uhci_qh,
5960ed8fee1SAlan Stern 				node);
597dccf4a48SAlan Stern 	list_move(&qh->node, &uhci->idle_qh_list);
598dccf4a48SAlan Stern 	qh->state = QH_STATE_IDLE;
599dccf4a48SAlan Stern 
60059e29ed9SAlan Stern 	/* Now that the QH is idle, its post_td isn't being used */
60159e29ed9SAlan Stern 	if (qh->post_td) {
60259e29ed9SAlan Stern 		uhci_free_td(uhci, qh->post_td);
60359e29ed9SAlan Stern 		qh->post_td = NULL;
60459e29ed9SAlan Stern 	}
60559e29ed9SAlan Stern 
606dccf4a48SAlan Stern 	/* If anyone is waiting for a QH to become idle, wake them up */
607dccf4a48SAlan Stern 	if (uhci->num_waiting)
608dccf4a48SAlan Stern 		wake_up_all(&uhci->waitqh);
6091da177e4SLinus Torvalds }
6101da177e4SLinus Torvalds 
6113ca2a321SAlan Stern /*
6123ca2a321SAlan Stern  * Find the highest existing bandwidth load for a given phase and period.
6133ca2a321SAlan Stern  */
6143ca2a321SAlan Stern static int uhci_highest_load(struct uhci_hcd *uhci, int phase, int period)
6153ca2a321SAlan Stern {
6163ca2a321SAlan Stern 	int highest_load = uhci->load[phase];
6173ca2a321SAlan Stern 
6183ca2a321SAlan Stern 	for (phase += period; phase < MAX_PHASE; phase += period)
6193ca2a321SAlan Stern 		highest_load = max_t(int, highest_load, uhci->load[phase]);
6203ca2a321SAlan Stern 	return highest_load;
6213ca2a321SAlan Stern }
6223ca2a321SAlan Stern 
6233ca2a321SAlan Stern /*
6243ca2a321SAlan Stern  * Set qh->phase to the optimal phase for a periodic transfer and
6253ca2a321SAlan Stern  * check whether the bandwidth requirement is acceptable.
6263ca2a321SAlan Stern  */
6273ca2a321SAlan Stern static int uhci_check_bandwidth(struct uhci_hcd *uhci, struct uhci_qh *qh)
6283ca2a321SAlan Stern {
6293ca2a321SAlan Stern 	int minimax_load;
6303ca2a321SAlan Stern 
6313ca2a321SAlan Stern 	/* Find the optimal phase (unless it is already set) and get
6323ca2a321SAlan Stern 	 * its load value. */
6333ca2a321SAlan Stern 	if (qh->phase >= 0)
6343ca2a321SAlan Stern 		minimax_load = uhci_highest_load(uhci, qh->phase, qh->period);
6353ca2a321SAlan Stern 	else {
6363ca2a321SAlan Stern 		int phase, load;
6373ca2a321SAlan Stern 		int max_phase = min_t(int, MAX_PHASE, qh->period);
6383ca2a321SAlan Stern 
6393ca2a321SAlan Stern 		qh->phase = 0;
6403ca2a321SAlan Stern 		minimax_load = uhci_highest_load(uhci, qh->phase, qh->period);
6413ca2a321SAlan Stern 		for (phase = 1; phase < max_phase; ++phase) {
6423ca2a321SAlan Stern 			load = uhci_highest_load(uhci, phase, qh->period);
6433ca2a321SAlan Stern 			if (load < minimax_load) {
6443ca2a321SAlan Stern 				minimax_load = load;
6453ca2a321SAlan Stern 				qh->phase = phase;
6463ca2a321SAlan Stern 			}
6473ca2a321SAlan Stern 		}
6483ca2a321SAlan Stern 	}
6493ca2a321SAlan Stern 
6503ca2a321SAlan Stern 	/* Maximum allowable periodic bandwidth is 90%, or 900 us per frame */
6513ca2a321SAlan Stern 	if (minimax_load + qh->load > 900) {
6523ca2a321SAlan Stern 		dev_dbg(uhci_dev(uhci), "bandwidth allocation failed: "
6533ca2a321SAlan Stern 				"period %d, phase %d, %d + %d us\n",
6543ca2a321SAlan Stern 				qh->period, qh->phase, minimax_load, qh->load);
6553ca2a321SAlan Stern 		return -ENOSPC;
6563ca2a321SAlan Stern 	}
6573ca2a321SAlan Stern 	return 0;
6583ca2a321SAlan Stern }
6593ca2a321SAlan Stern 
6603ca2a321SAlan Stern /*
6613ca2a321SAlan Stern  * Reserve a periodic QH's bandwidth in the schedule
6623ca2a321SAlan Stern  */
6633ca2a321SAlan Stern static void uhci_reserve_bandwidth(struct uhci_hcd *uhci, struct uhci_qh *qh)
6643ca2a321SAlan Stern {
6653ca2a321SAlan Stern 	int i;
6663ca2a321SAlan Stern 	int load = qh->load;
6673ca2a321SAlan Stern 	char *p = "??";
6683ca2a321SAlan Stern 
6693ca2a321SAlan Stern 	for (i = qh->phase; i < MAX_PHASE; i += qh->period) {
6703ca2a321SAlan Stern 		uhci->load[i] += load;
6713ca2a321SAlan Stern 		uhci->total_load += load;
6723ca2a321SAlan Stern 	}
6733ca2a321SAlan Stern 	uhci_to_hcd(uhci)->self.bandwidth_allocated =
6743ca2a321SAlan Stern 			uhci->total_load / MAX_PHASE;
6753ca2a321SAlan Stern 	switch (qh->type) {
6763ca2a321SAlan Stern 	case USB_ENDPOINT_XFER_INT:
6773ca2a321SAlan Stern 		++uhci_to_hcd(uhci)->self.bandwidth_int_reqs;
6783ca2a321SAlan Stern 		p = "INT";
6793ca2a321SAlan Stern 		break;
6803ca2a321SAlan Stern 	case USB_ENDPOINT_XFER_ISOC:
6813ca2a321SAlan Stern 		++uhci_to_hcd(uhci)->self.bandwidth_isoc_reqs;
6823ca2a321SAlan Stern 		p = "ISO";
6833ca2a321SAlan Stern 		break;
6843ca2a321SAlan Stern 	}
6853ca2a321SAlan Stern 	qh->bandwidth_reserved = 1;
6863ca2a321SAlan Stern 	dev_dbg(uhci_dev(uhci),
6873ca2a321SAlan Stern 			"%s dev %d ep%02x-%s, period %d, phase %d, %d us\n",
6883ca2a321SAlan Stern 			"reserve", qh->udev->devnum,
6893ca2a321SAlan Stern 			qh->hep->desc.bEndpointAddress, p,
6903ca2a321SAlan Stern 			qh->period, qh->phase, load);
6913ca2a321SAlan Stern }
6923ca2a321SAlan Stern 
6933ca2a321SAlan Stern /*
6943ca2a321SAlan Stern  * Release a periodic QH's bandwidth reservation
6953ca2a321SAlan Stern  */
6963ca2a321SAlan Stern static void uhci_release_bandwidth(struct uhci_hcd *uhci, struct uhci_qh *qh)
6973ca2a321SAlan Stern {
6983ca2a321SAlan Stern 	int i;
6993ca2a321SAlan Stern 	int load = qh->load;
7003ca2a321SAlan Stern 	char *p = "??";
7013ca2a321SAlan Stern 
7023ca2a321SAlan Stern 	for (i = qh->phase; i < MAX_PHASE; i += qh->period) {
7033ca2a321SAlan Stern 		uhci->load[i] -= load;
7043ca2a321SAlan Stern 		uhci->total_load -= load;
7053ca2a321SAlan Stern 	}
7063ca2a321SAlan Stern 	uhci_to_hcd(uhci)->self.bandwidth_allocated =
7073ca2a321SAlan Stern 			uhci->total_load / MAX_PHASE;
7083ca2a321SAlan Stern 	switch (qh->type) {
7093ca2a321SAlan Stern 	case USB_ENDPOINT_XFER_INT:
7103ca2a321SAlan Stern 		--uhci_to_hcd(uhci)->self.bandwidth_int_reqs;
7113ca2a321SAlan Stern 		p = "INT";
7123ca2a321SAlan Stern 		break;
7133ca2a321SAlan Stern 	case USB_ENDPOINT_XFER_ISOC:
7143ca2a321SAlan Stern 		--uhci_to_hcd(uhci)->self.bandwidth_isoc_reqs;
7153ca2a321SAlan Stern 		p = "ISO";
7163ca2a321SAlan Stern 		break;
7173ca2a321SAlan Stern 	}
7183ca2a321SAlan Stern 	qh->bandwidth_reserved = 0;
7193ca2a321SAlan Stern 	dev_dbg(uhci_dev(uhci),
7203ca2a321SAlan Stern 			"%s dev %d ep%02x-%s, period %d, phase %d, %d us\n",
7213ca2a321SAlan Stern 			"release", qh->udev->devnum,
7223ca2a321SAlan Stern 			qh->hep->desc.bEndpointAddress, p,
7233ca2a321SAlan Stern 			qh->period, qh->phase, load);
7243ca2a321SAlan Stern }
7253ca2a321SAlan Stern 
726dccf4a48SAlan Stern static inline struct urb_priv *uhci_alloc_urb_priv(struct uhci_hcd *uhci,
727dccf4a48SAlan Stern 		struct urb *urb)
7281da177e4SLinus Torvalds {
7291da177e4SLinus Torvalds 	struct urb_priv *urbp;
7301da177e4SLinus Torvalds 
731c3762229SRobert P. J. Day 	urbp = kmem_cache_zalloc(uhci_up_cachep, GFP_ATOMIC);
7321da177e4SLinus Torvalds 	if (!urbp)
7331da177e4SLinus Torvalds 		return NULL;
7341da177e4SLinus Torvalds 
7351da177e4SLinus Torvalds 	urbp->urb = urb;
7361da177e4SLinus Torvalds 	urb->hcpriv = urbp;
737dccf4a48SAlan Stern 
738dccf4a48SAlan Stern 	INIT_LIST_HEAD(&urbp->node);
739dccf4a48SAlan Stern 	INIT_LIST_HEAD(&urbp->td_list);
7401da177e4SLinus Torvalds 
7411da177e4SLinus Torvalds 	return urbp;
7421da177e4SLinus Torvalds }
7431da177e4SLinus Torvalds 
744dccf4a48SAlan Stern static void uhci_free_urb_priv(struct uhci_hcd *uhci,
745dccf4a48SAlan Stern 		struct urb_priv *urbp)
7461da177e4SLinus Torvalds {
7471da177e4SLinus Torvalds 	struct uhci_td *td, *tmp;
7481da177e4SLinus Torvalds 
7490cef7727SAlan Stern 	if (!list_empty(&urbp->node)) {
750dccf4a48SAlan Stern 		dev_warn(uhci_dev(uhci), "urb %p still on QH's list!\n",
751dccf4a48SAlan Stern 				urbp->urb);
7520cef7727SAlan Stern 		WARN_ON(1);
7530cef7727SAlan Stern 	}
7541da177e4SLinus Torvalds 
7551da177e4SLinus Torvalds 	list_for_each_entry_safe(td, tmp, &urbp->td_list, list) {
75604538a25SAlan Stern 		uhci_remove_td_from_urbp(td);
75704538a25SAlan Stern 		uhci_free_td(uhci, td);
7581da177e4SLinus Torvalds 	}
7591da177e4SLinus Torvalds 
760dccf4a48SAlan Stern 	urbp->urb->hcpriv = NULL;
7611da177e4SLinus Torvalds 	kmem_cache_free(uhci_up_cachep, urbp);
7621da177e4SLinus Torvalds }
7631da177e4SLinus Torvalds 
7641da177e4SLinus Torvalds /*
7651da177e4SLinus Torvalds  * Map status to standard result codes
7661da177e4SLinus Torvalds  *
7671da177e4SLinus Torvalds  * <status> is (td_status(td) & 0xF60000), a.k.a.
7681da177e4SLinus Torvalds  * uhci_status_bits(td_status(td)).
7691da177e4SLinus Torvalds  * Note: <status> does not include the TD_CTRL_NAK bit.
7701da177e4SLinus Torvalds  * <dir_out> is True for output TDs and False for input TDs.
7711da177e4SLinus Torvalds  */
7721da177e4SLinus Torvalds static int uhci_map_status(int status, int dir_out)
7731da177e4SLinus Torvalds {
7741da177e4SLinus Torvalds 	if (!status)
7751da177e4SLinus Torvalds 		return 0;
7761da177e4SLinus Torvalds 	if (status & TD_CTRL_BITSTUFF)			/* Bitstuff error */
7771da177e4SLinus Torvalds 		return -EPROTO;
7781da177e4SLinus Torvalds 	if (status & TD_CTRL_CRCTIMEO) {		/* CRC/Timeout */
7791da177e4SLinus Torvalds 		if (dir_out)
7801da177e4SLinus Torvalds 			return -EPROTO;
7811da177e4SLinus Torvalds 		else
7821da177e4SLinus Torvalds 			return -EILSEQ;
7831da177e4SLinus Torvalds 	}
7841da177e4SLinus Torvalds 	if (status & TD_CTRL_BABBLE)			/* Babble */
7851da177e4SLinus Torvalds 		return -EOVERFLOW;
7861da177e4SLinus Torvalds 	if (status & TD_CTRL_DBUFERR)			/* Buffer error */
7871da177e4SLinus Torvalds 		return -ENOSR;
7881da177e4SLinus Torvalds 	if (status & TD_CTRL_STALLED)			/* Stalled */
7891da177e4SLinus Torvalds 		return -EPIPE;
7901da177e4SLinus Torvalds 	return 0;
7911da177e4SLinus Torvalds }
7921da177e4SLinus Torvalds 
7931da177e4SLinus Torvalds /*
7941da177e4SLinus Torvalds  * Control transfers
7951da177e4SLinus Torvalds  */
796dccf4a48SAlan Stern static int uhci_submit_control(struct uhci_hcd *uhci, struct urb *urb,
797dccf4a48SAlan Stern 		struct uhci_qh *qh)
7981da177e4SLinus Torvalds {
7991da177e4SLinus Torvalds 	struct uhci_td *td;
8001da177e4SLinus Torvalds 	unsigned long destination, status;
801dccf4a48SAlan Stern 	int maxsze = le16_to_cpu(qh->hep->desc.wMaxPacketSize);
8021da177e4SLinus Torvalds 	int len = urb->transfer_buffer_length;
8031da177e4SLinus Torvalds 	dma_addr_t data = urb->transfer_dma;
804dccf4a48SAlan Stern 	__le32 *plink;
80504538a25SAlan Stern 	struct urb_priv *urbp = urb->hcpriv;
80617230acdSAlan Stern 	int skel;
8071da177e4SLinus Torvalds 
8081da177e4SLinus Torvalds 	/* The "pipe" thing contains the destination in bits 8--18 */
8091da177e4SLinus Torvalds 	destination = (urb->pipe & PIPE_DEVEP_MASK) | USB_PID_SETUP;
8101da177e4SLinus Torvalds 
811af0bb599SAlan Stern 	/* 3 errors, dummy TD remains inactive */
812af0bb599SAlan Stern 	status = uhci_maxerr(3);
8131da177e4SLinus Torvalds 	if (urb->dev->speed == USB_SPEED_LOW)
8141da177e4SLinus Torvalds 		status |= TD_CTRL_LS;
8151da177e4SLinus Torvalds 
8161da177e4SLinus Torvalds 	/*
8171da177e4SLinus Torvalds 	 * Build the TD for the control request setup packet
8181da177e4SLinus Torvalds 	 */
819af0bb599SAlan Stern 	td = qh->dummy_td;
82004538a25SAlan Stern 	uhci_add_td_to_urbp(td, urbp);
821fa346568SAlan Stern 	uhci_fill_td(td, status, destination | uhci_explen(8),
8221da177e4SLinus Torvalds 			urb->setup_dma);
823dccf4a48SAlan Stern 	plink = &td->link;
824af0bb599SAlan Stern 	status |= TD_CTRL_ACTIVE;
8251da177e4SLinus Torvalds 
8261da177e4SLinus Torvalds 	/*
8271da177e4SLinus Torvalds 	 * If direction is "send", change the packet ID from SETUP (0x2D)
8281da177e4SLinus Torvalds 	 * to OUT (0xE1).  Else change it from SETUP to IN (0x69) and
8291da177e4SLinus Torvalds 	 * set Short Packet Detect (SPD) for all data packets.
830*e7e7c360SAlan Stern 	 *
831*e7e7c360SAlan Stern 	 * 0-length transfers always get treated as "send".
8321da177e4SLinus Torvalds 	 */
833*e7e7c360SAlan Stern 	if (usb_pipeout(urb->pipe) || len == 0)
8341da177e4SLinus Torvalds 		destination ^= (USB_PID_SETUP ^ USB_PID_OUT);
8351da177e4SLinus Torvalds 	else {
8361da177e4SLinus Torvalds 		destination ^= (USB_PID_SETUP ^ USB_PID_IN);
8371da177e4SLinus Torvalds 		status |= TD_CTRL_SPD;
8381da177e4SLinus Torvalds 	}
8391da177e4SLinus Torvalds 
8401da177e4SLinus Torvalds 	/*
841687f5f34SAlan Stern 	 * Build the DATA TDs
8421da177e4SLinus Torvalds 	 */
8431da177e4SLinus Torvalds 	while (len > 0) {
844*e7e7c360SAlan Stern 		int pktsze = maxsze;
845*e7e7c360SAlan Stern 
846*e7e7c360SAlan Stern 		if (len <= pktsze) {		/* The last data packet */
847*e7e7c360SAlan Stern 			pktsze = len;
848*e7e7c360SAlan Stern 			status &= ~TD_CTRL_SPD;
849*e7e7c360SAlan Stern 		}
8501da177e4SLinus Torvalds 
8512532178aSAlan Stern 		td = uhci_alloc_td(uhci);
8521da177e4SLinus Torvalds 		if (!td)
853af0bb599SAlan Stern 			goto nomem;
85428b9325eSAlan Stern 		*plink = LINK_TO_TD(td);
8551da177e4SLinus Torvalds 
8561da177e4SLinus Torvalds 		/* Alternate Data0/1 (start with Data1) */
8571da177e4SLinus Torvalds 		destination ^= TD_TOKEN_TOGGLE;
8581da177e4SLinus Torvalds 
85904538a25SAlan Stern 		uhci_add_td_to_urbp(td, urbp);
860fa346568SAlan Stern 		uhci_fill_td(td, status, destination | uhci_explen(pktsze),
8611da177e4SLinus Torvalds 				data);
862dccf4a48SAlan Stern 		plink = &td->link;
8631da177e4SLinus Torvalds 
8641da177e4SLinus Torvalds 		data += pktsze;
8651da177e4SLinus Torvalds 		len -= pktsze;
8661da177e4SLinus Torvalds 	}
8671da177e4SLinus Torvalds 
8681da177e4SLinus Torvalds 	/*
8691da177e4SLinus Torvalds 	 * Build the final TD for control status
8701da177e4SLinus Torvalds 	 */
8712532178aSAlan Stern 	td = uhci_alloc_td(uhci);
8721da177e4SLinus Torvalds 	if (!td)
873af0bb599SAlan Stern 		goto nomem;
87428b9325eSAlan Stern 	*plink = LINK_TO_TD(td);
8751da177e4SLinus Torvalds 
876*e7e7c360SAlan Stern 	/* Change direction for the status transaction */
877*e7e7c360SAlan Stern 	destination ^= (USB_PID_IN ^ USB_PID_OUT);
8781da177e4SLinus Torvalds 	destination |= TD_TOKEN_TOGGLE;		/* End in Data1 */
8791da177e4SLinus Torvalds 
88004538a25SAlan Stern 	uhci_add_td_to_urbp(td, urbp);
8811da177e4SLinus Torvalds 	uhci_fill_td(td, status | TD_CTRL_IOC,
882fa346568SAlan Stern 			destination | uhci_explen(0), 0);
883af0bb599SAlan Stern 	plink = &td->link;
884af0bb599SAlan Stern 
885af0bb599SAlan Stern 	/*
886af0bb599SAlan Stern 	 * Build the new dummy TD and activate the old one
887af0bb599SAlan Stern 	 */
888af0bb599SAlan Stern 	td = uhci_alloc_td(uhci);
889af0bb599SAlan Stern 	if (!td)
890af0bb599SAlan Stern 		goto nomem;
89128b9325eSAlan Stern 	*plink = LINK_TO_TD(td);
892af0bb599SAlan Stern 
893af0bb599SAlan Stern 	uhci_fill_td(td, 0, USB_PID_OUT | uhci_explen(0), 0);
894af0bb599SAlan Stern 	wmb();
895af0bb599SAlan Stern 	qh->dummy_td->status |= __constant_cpu_to_le32(TD_CTRL_ACTIVE);
896af0bb599SAlan Stern 	qh->dummy_td = td;
8971da177e4SLinus Torvalds 
8981da177e4SLinus Torvalds 	/* Low-speed transfers get a different queue, and won't hog the bus.
8991da177e4SLinus Torvalds 	 * Also, some devices enumerate better without FSBR; the easiest way
9001da177e4SLinus Torvalds 	 * to do that is to put URBs on the low-speed queue while the device
901630aa3cfSAlan Stern 	 * isn't in the CONFIGURED state. */
9021da177e4SLinus Torvalds 	if (urb->dev->speed == USB_SPEED_LOW ||
903630aa3cfSAlan Stern 			urb->dev->state != USB_STATE_CONFIGURED)
90417230acdSAlan Stern 		skel = SKEL_LS_CONTROL;
9051da177e4SLinus Torvalds 	else {
90617230acdSAlan Stern 		skel = SKEL_FS_CONTROL;
90784afddd7SAlan Stern 		uhci_add_fsbr(uhci, urb);
9081da177e4SLinus Torvalds 	}
90917230acdSAlan Stern 	if (qh->state != QH_STATE_ACTIVE)
91017230acdSAlan Stern 		qh->skel = skel;
91159e29ed9SAlan Stern 
91259e29ed9SAlan Stern 	urb->actual_length = -8;	/* Account for the SETUP packet */
913dccf4a48SAlan Stern 	return 0;
914af0bb599SAlan Stern 
915af0bb599SAlan Stern nomem:
916af0bb599SAlan Stern 	/* Remove the dummy TD from the td_list so it doesn't get freed */
91704538a25SAlan Stern 	uhci_remove_td_from_urbp(qh->dummy_td);
918af0bb599SAlan Stern 	return -ENOMEM;
9191da177e4SLinus Torvalds }
9201da177e4SLinus Torvalds 
9211da177e4SLinus Torvalds /*
9221da177e4SLinus Torvalds  * Common submit for bulk and interrupt
9231da177e4SLinus Torvalds  */
924dccf4a48SAlan Stern static int uhci_submit_common(struct uhci_hcd *uhci, struct urb *urb,
925dccf4a48SAlan Stern 		struct uhci_qh *qh)
9261da177e4SLinus Torvalds {
9271da177e4SLinus Torvalds 	struct uhci_td *td;
9281da177e4SLinus Torvalds 	unsigned long destination, status;
929dccf4a48SAlan Stern 	int maxsze = le16_to_cpu(qh->hep->desc.wMaxPacketSize);
9301da177e4SLinus Torvalds 	int len = urb->transfer_buffer_length;
9311da177e4SLinus Torvalds 	dma_addr_t data = urb->transfer_dma;
932af0bb599SAlan Stern 	__le32 *plink;
93304538a25SAlan Stern 	struct urb_priv *urbp = urb->hcpriv;
934af0bb599SAlan Stern 	unsigned int toggle;
9351da177e4SLinus Torvalds 
9361da177e4SLinus Torvalds 	if (len < 0)
9371da177e4SLinus Torvalds 		return -EINVAL;
9381da177e4SLinus Torvalds 
9391da177e4SLinus Torvalds 	/* The "pipe" thing contains the destination in bits 8--18 */
9401da177e4SLinus Torvalds 	destination = (urb->pipe & PIPE_DEVEP_MASK) | usb_packetid(urb->pipe);
941af0bb599SAlan Stern 	toggle = usb_gettoggle(urb->dev, usb_pipeendpoint(urb->pipe),
942af0bb599SAlan Stern 			 usb_pipeout(urb->pipe));
9431da177e4SLinus Torvalds 
944af0bb599SAlan Stern 	/* 3 errors, dummy TD remains inactive */
945af0bb599SAlan Stern 	status = uhci_maxerr(3);
9461da177e4SLinus Torvalds 	if (urb->dev->speed == USB_SPEED_LOW)
9471da177e4SLinus Torvalds 		status |= TD_CTRL_LS;
9481da177e4SLinus Torvalds 	if (usb_pipein(urb->pipe))
9491da177e4SLinus Torvalds 		status |= TD_CTRL_SPD;
9501da177e4SLinus Torvalds 
9511da177e4SLinus Torvalds 	/*
952687f5f34SAlan Stern 	 * Build the DATA TDs
9531da177e4SLinus Torvalds 	 */
954af0bb599SAlan Stern 	plink = NULL;
955af0bb599SAlan Stern 	td = qh->dummy_td;
9561da177e4SLinus Torvalds 	do {	/* Allow zero length packets */
9571da177e4SLinus Torvalds 		int pktsze = maxsze;
9581da177e4SLinus Torvalds 
959dccf4a48SAlan Stern 		if (len <= pktsze) {		/* The last packet */
9601da177e4SLinus Torvalds 			pktsze = len;
9611da177e4SLinus Torvalds 			if (!(urb->transfer_flags & URB_SHORT_NOT_OK))
9621da177e4SLinus Torvalds 				status &= ~TD_CTRL_SPD;
9631da177e4SLinus Torvalds 		}
9641da177e4SLinus Torvalds 
965af0bb599SAlan Stern 		if (plink) {
9662532178aSAlan Stern 			td = uhci_alloc_td(uhci);
9671da177e4SLinus Torvalds 			if (!td)
968af0bb599SAlan Stern 				goto nomem;
96928b9325eSAlan Stern 			*plink = LINK_TO_TD(td);
970af0bb599SAlan Stern 		}
97104538a25SAlan Stern 		uhci_add_td_to_urbp(td, urbp);
972dccf4a48SAlan Stern 		uhci_fill_td(td, status,
973dccf4a48SAlan Stern 				destination | uhci_explen(pktsze) |
974af0bb599SAlan Stern 					(toggle << TD_TOKEN_TOGGLE_SHIFT),
9751da177e4SLinus Torvalds 				data);
976dccf4a48SAlan Stern 		plink = &td->link;
977af0bb599SAlan Stern 		status |= TD_CTRL_ACTIVE;
9781da177e4SLinus Torvalds 
9791da177e4SLinus Torvalds 		data += pktsze;
9801da177e4SLinus Torvalds 		len -= maxsze;
981af0bb599SAlan Stern 		toggle ^= 1;
9821da177e4SLinus Torvalds 	} while (len > 0);
9831da177e4SLinus Torvalds 
9841da177e4SLinus Torvalds 	/*
9851da177e4SLinus Torvalds 	 * URB_ZERO_PACKET means adding a 0-length packet, if direction
9861da177e4SLinus Torvalds 	 * is OUT and the transfer_length was an exact multiple of maxsze,
9871da177e4SLinus Torvalds 	 * hence (len = transfer_length - N * maxsze) == 0
9881da177e4SLinus Torvalds 	 * however, if transfer_length == 0, the zero packet was already
9891da177e4SLinus Torvalds 	 * prepared above.
9901da177e4SLinus Torvalds 	 */
991dccf4a48SAlan Stern 	if ((urb->transfer_flags & URB_ZERO_PACKET) &&
992dccf4a48SAlan Stern 			usb_pipeout(urb->pipe) && len == 0 &&
993dccf4a48SAlan Stern 			urb->transfer_buffer_length > 0) {
9942532178aSAlan Stern 		td = uhci_alloc_td(uhci);
9951da177e4SLinus Torvalds 		if (!td)
996af0bb599SAlan Stern 			goto nomem;
99728b9325eSAlan Stern 		*plink = LINK_TO_TD(td);
9981da177e4SLinus Torvalds 
99904538a25SAlan Stern 		uhci_add_td_to_urbp(td, urbp);
1000af0bb599SAlan Stern 		uhci_fill_td(td, status,
1001af0bb599SAlan Stern 				destination | uhci_explen(0) |
1002af0bb599SAlan Stern 					(toggle << TD_TOKEN_TOGGLE_SHIFT),
10031da177e4SLinus Torvalds 				data);
1004af0bb599SAlan Stern 		plink = &td->link;
10051da177e4SLinus Torvalds 
1006af0bb599SAlan Stern 		toggle ^= 1;
10071da177e4SLinus Torvalds 	}
10081da177e4SLinus Torvalds 
10091da177e4SLinus Torvalds 	/* Set the interrupt-on-completion flag on the last packet.
10101da177e4SLinus Torvalds 	 * A more-or-less typical 4 KB URB (= size of one memory page)
10111da177e4SLinus Torvalds 	 * will require about 3 ms to transfer; that's a little on the
10121da177e4SLinus Torvalds 	 * fast side but not enough to justify delaying an interrupt
10131da177e4SLinus Torvalds 	 * more than 2 or 3 URBs, so we will ignore the URB_NO_INTERRUPT
10141da177e4SLinus Torvalds 	 * flag setting. */
1015dccf4a48SAlan Stern 	td->status |= __constant_cpu_to_le32(TD_CTRL_IOC);
10161da177e4SLinus Torvalds 
1017af0bb599SAlan Stern 	/*
1018af0bb599SAlan Stern 	 * Build the new dummy TD and activate the old one
1019af0bb599SAlan Stern 	 */
1020af0bb599SAlan Stern 	td = uhci_alloc_td(uhci);
1021af0bb599SAlan Stern 	if (!td)
1022af0bb599SAlan Stern 		goto nomem;
102328b9325eSAlan Stern 	*plink = LINK_TO_TD(td);
1024af0bb599SAlan Stern 
1025af0bb599SAlan Stern 	uhci_fill_td(td, 0, USB_PID_OUT | uhci_explen(0), 0);
1026af0bb599SAlan Stern 	wmb();
1027af0bb599SAlan Stern 	qh->dummy_td->status |= __constant_cpu_to_le32(TD_CTRL_ACTIVE);
1028af0bb599SAlan Stern 	qh->dummy_td = td;
1029af0bb599SAlan Stern 
1030af0bb599SAlan Stern 	usb_settoggle(urb->dev, usb_pipeendpoint(urb->pipe),
1031af0bb599SAlan Stern 			usb_pipeout(urb->pipe), toggle);
1032dccf4a48SAlan Stern 	return 0;
1033af0bb599SAlan Stern 
1034af0bb599SAlan Stern nomem:
1035af0bb599SAlan Stern 	/* Remove the dummy TD from the td_list so it doesn't get freed */
103604538a25SAlan Stern 	uhci_remove_td_from_urbp(qh->dummy_td);
1037af0bb599SAlan Stern 	return -ENOMEM;
10381da177e4SLinus Torvalds }
10391da177e4SLinus Torvalds 
104017230acdSAlan Stern static int uhci_submit_bulk(struct uhci_hcd *uhci, struct urb *urb,
1041dccf4a48SAlan Stern 		struct uhci_qh *qh)
10421da177e4SLinus Torvalds {
10431da177e4SLinus Torvalds 	int ret;
10441da177e4SLinus Torvalds 
10451da177e4SLinus Torvalds 	/* Can't have low-speed bulk transfers */
10461da177e4SLinus Torvalds 	if (urb->dev->speed == USB_SPEED_LOW)
10471da177e4SLinus Torvalds 		return -EINVAL;
10481da177e4SLinus Torvalds 
104917230acdSAlan Stern 	if (qh->state != QH_STATE_ACTIVE)
105017230acdSAlan Stern 		qh->skel = SKEL_BULK;
1051dccf4a48SAlan Stern 	ret = uhci_submit_common(uhci, urb, qh);
1052dccf4a48SAlan Stern 	if (ret == 0)
105384afddd7SAlan Stern 		uhci_add_fsbr(uhci, urb);
10541da177e4SLinus Torvalds 	return ret;
10551da177e4SLinus Torvalds }
10561da177e4SLinus Torvalds 
1057caf3827aSAlan Stern static int uhci_submit_interrupt(struct uhci_hcd *uhci, struct urb *urb,
1058dccf4a48SAlan Stern 		struct uhci_qh *qh)
10591da177e4SLinus Torvalds {
10603ca2a321SAlan Stern 	int ret;
1061caf3827aSAlan Stern 
1062dccf4a48SAlan Stern 	/* USB 1.1 interrupt transfers only involve one packet per interval.
1063dccf4a48SAlan Stern 	 * Drivers can submit URBs of any length, but longer ones will need
1064dccf4a48SAlan Stern 	 * multiple intervals to complete.
10651da177e4SLinus Torvalds 	 */
1066caf3827aSAlan Stern 
10673ca2a321SAlan Stern 	if (!qh->bandwidth_reserved) {
10683ca2a321SAlan Stern 		int exponent;
10693ca2a321SAlan Stern 
1070caf3827aSAlan Stern 		/* Figure out which power-of-two queue to use */
1071caf3827aSAlan Stern 		for (exponent = 7; exponent >= 0; --exponent) {
1072caf3827aSAlan Stern 			if ((1 << exponent) <= urb->interval)
1073caf3827aSAlan Stern 				break;
1074caf3827aSAlan Stern 		}
1075caf3827aSAlan Stern 		if (exponent < 0)
1076caf3827aSAlan Stern 			return -EINVAL;
10773ca2a321SAlan Stern 		qh->period = 1 << exponent;
107817230acdSAlan Stern 		qh->skel = SKEL_INDEX(exponent);
1079caf3827aSAlan Stern 
10803ca2a321SAlan Stern 		/* For now, interrupt phase is fixed by the layout
10813ca2a321SAlan Stern 		 * of the QH lists. */
10823ca2a321SAlan Stern 		qh->phase = (qh->period / 2) & (MAX_PHASE - 1);
10833ca2a321SAlan Stern 		ret = uhci_check_bandwidth(uhci, qh);
10843ca2a321SAlan Stern 		if (ret)
10853ca2a321SAlan Stern 			return ret;
10863ca2a321SAlan Stern 	} else if (qh->period > urb->interval)
10873ca2a321SAlan Stern 		return -EINVAL;		/* Can't decrease the period */
10883ca2a321SAlan Stern 
10893ca2a321SAlan Stern 	ret = uhci_submit_common(uhci, urb, qh);
10903ca2a321SAlan Stern 	if (ret == 0) {
10913ca2a321SAlan Stern 		urb->interval = qh->period;
10923ca2a321SAlan Stern 		if (!qh->bandwidth_reserved)
10933ca2a321SAlan Stern 			uhci_reserve_bandwidth(uhci, qh);
10943ca2a321SAlan Stern 	}
10953ca2a321SAlan Stern 	return ret;
10961da177e4SLinus Torvalds }
10971da177e4SLinus Torvalds 
10981da177e4SLinus Torvalds /*
1099b1869000SAlan Stern  * Fix up the data structures following a short transfer
1100b1869000SAlan Stern  */
1101b1869000SAlan Stern static int uhci_fixup_short_transfer(struct uhci_hcd *uhci,
110259e29ed9SAlan Stern 		struct uhci_qh *qh, struct urb_priv *urbp)
1103b1869000SAlan Stern {
1104b1869000SAlan Stern 	struct uhci_td *td;
110559e29ed9SAlan Stern 	struct list_head *tmp;
110659e29ed9SAlan Stern 	int ret;
1107b1869000SAlan Stern 
1108b1869000SAlan Stern 	td = list_entry(urbp->td_list.prev, struct uhci_td, list);
1109b1869000SAlan Stern 	if (qh->type == USB_ENDPOINT_XFER_CONTROL) {
1110b1869000SAlan Stern 
1111b1869000SAlan Stern 		/* When a control transfer is short, we have to restart
1112b1869000SAlan Stern 		 * the queue at the status stage transaction, which is
1113b1869000SAlan Stern 		 * the last TD. */
111459e29ed9SAlan Stern 		WARN_ON(list_empty(&urbp->td_list));
111528b9325eSAlan Stern 		qh->element = LINK_TO_TD(td);
111659e29ed9SAlan Stern 		tmp = td->list.prev;
1117b1869000SAlan Stern 		ret = -EINPROGRESS;
1118b1869000SAlan Stern 
111959e29ed9SAlan Stern 	} else {
1120b1869000SAlan Stern 
1121b1869000SAlan Stern 		/* When a bulk/interrupt transfer is short, we have to
1122b1869000SAlan Stern 		 * fix up the toggles of the following URBs on the queue
1123b1869000SAlan Stern 		 * before restarting the queue at the next URB. */
112459e29ed9SAlan Stern 		qh->initial_toggle = uhci_toggle(td_token(qh->post_td)) ^ 1;
1125b1869000SAlan Stern 		uhci_fixup_toggles(qh, 1);
1126b1869000SAlan Stern 
112759e29ed9SAlan Stern 		if (list_empty(&urbp->td_list))
112859e29ed9SAlan Stern 			td = qh->post_td;
1129b1869000SAlan Stern 		qh->element = td->link;
113059e29ed9SAlan Stern 		tmp = urbp->td_list.prev;
113159e29ed9SAlan Stern 		ret = 0;
1132b1869000SAlan Stern 	}
1133b1869000SAlan Stern 
113459e29ed9SAlan Stern 	/* Remove all the TDs we skipped over, from tmp back to the start */
113559e29ed9SAlan Stern 	while (tmp != &urbp->td_list) {
113659e29ed9SAlan Stern 		td = list_entry(tmp, struct uhci_td, list);
113759e29ed9SAlan Stern 		tmp = tmp->prev;
113859e29ed9SAlan Stern 
113904538a25SAlan Stern 		uhci_remove_td_from_urbp(td);
114004538a25SAlan Stern 		uhci_free_td(uhci, td);
114159e29ed9SAlan Stern 	}
1142b1869000SAlan Stern 	return ret;
1143b1869000SAlan Stern }
1144b1869000SAlan Stern 
1145b1869000SAlan Stern /*
1146b1869000SAlan Stern  * Common result for control, bulk, and interrupt
1147b1869000SAlan Stern  */
1148b1869000SAlan Stern static int uhci_result_common(struct uhci_hcd *uhci, struct urb *urb)
1149b1869000SAlan Stern {
1150b1869000SAlan Stern 	struct urb_priv *urbp = urb->hcpriv;
1151b1869000SAlan Stern 	struct uhci_qh *qh = urbp->qh;
115259e29ed9SAlan Stern 	struct uhci_td *td, *tmp;
1153b1869000SAlan Stern 	unsigned status;
1154b1869000SAlan Stern 	int ret = 0;
1155b1869000SAlan Stern 
115659e29ed9SAlan Stern 	list_for_each_entry_safe(td, tmp, &urbp->td_list, list) {
1157b1869000SAlan Stern 		unsigned int ctrlstat;
1158b1869000SAlan Stern 		int len;
1159b1869000SAlan Stern 
1160b1869000SAlan Stern 		ctrlstat = td_status(td);
1161b1869000SAlan Stern 		status = uhci_status_bits(ctrlstat);
1162b1869000SAlan Stern 		if (status & TD_CTRL_ACTIVE)
1163b1869000SAlan Stern 			return -EINPROGRESS;
1164b1869000SAlan Stern 
1165b1869000SAlan Stern 		len = uhci_actual_length(ctrlstat);
1166b1869000SAlan Stern 		urb->actual_length += len;
1167b1869000SAlan Stern 
1168b1869000SAlan Stern 		if (status) {
1169b1869000SAlan Stern 			ret = uhci_map_status(status,
1170b1869000SAlan Stern 					uhci_packetout(td_token(td)));
1171b1869000SAlan Stern 			if ((debug == 1 && ret != -EPIPE) || debug > 1) {
1172b1869000SAlan Stern 				/* Some debugging code */
1173be3cbc5fSDavid Brownell 				dev_dbg(&urb->dev->dev,
1174b1869000SAlan Stern 						"%s: failed with status %x\n",
1175b1869000SAlan Stern 						__FUNCTION__, status);
1176b1869000SAlan Stern 
1177b1869000SAlan Stern 				if (debug > 1 && errbuf) {
1178b1869000SAlan Stern 					/* Print the chain for debugging */
1179e009f1b2SAlan Stern 					uhci_show_qh(uhci, urbp->qh, errbuf,
1180b1869000SAlan Stern 							ERRBUF_LEN, 0);
1181b1869000SAlan Stern 					lprintk(errbuf);
1182b1869000SAlan Stern 				}
1183b1869000SAlan Stern 			}
1184b1869000SAlan Stern 
1185*e7e7c360SAlan Stern 		/* Did we receive a short packet? */
1186b1869000SAlan Stern 		} else if (len < uhci_expected_length(td_token(td))) {
1187b1869000SAlan Stern 
1188*e7e7c360SAlan Stern 			/* For control transfers, go to the status TD if
1189*e7e7c360SAlan Stern 			 * this isn't already the last data TD */
1190*e7e7c360SAlan Stern 			if (qh->type == USB_ENDPOINT_XFER_CONTROL) {
1191*e7e7c360SAlan Stern 				if (td->list.next != urbp->td_list.prev)
1192*e7e7c360SAlan Stern 					ret = 1;
1193*e7e7c360SAlan Stern 			}
1194*e7e7c360SAlan Stern 
1195*e7e7c360SAlan Stern 			/* For bulk and interrupt, this may be an error */
1196*e7e7c360SAlan Stern 			else if (urb->transfer_flags & URB_SHORT_NOT_OK)
1197b1869000SAlan Stern 				ret = -EREMOTEIO;
1198f443ddf1SAlan Stern 
1199f443ddf1SAlan Stern 			/* Fixup needed only if this isn't the URB's last TD */
1200f443ddf1SAlan Stern 			else if (&td->list != urbp->td_list.prev)
1201b1869000SAlan Stern 				ret = 1;
1202b1869000SAlan Stern 		}
1203b1869000SAlan Stern 
120404538a25SAlan Stern 		uhci_remove_td_from_urbp(td);
120559e29ed9SAlan Stern 		if (qh->post_td)
120604538a25SAlan Stern 			uhci_free_td(uhci, qh->post_td);
120759e29ed9SAlan Stern 		qh->post_td = td;
120859e29ed9SAlan Stern 
1209b1869000SAlan Stern 		if (ret != 0)
1210b1869000SAlan Stern 			goto err;
1211b1869000SAlan Stern 	}
1212b1869000SAlan Stern 	return ret;
1213b1869000SAlan Stern 
1214b1869000SAlan Stern err:
1215b1869000SAlan Stern 	if (ret < 0) {
1216b1869000SAlan Stern 		/* Note that the queue has stopped and save
1217b1869000SAlan Stern 		 * the next toggle value */
1218b1869000SAlan Stern 		qh->element = UHCI_PTR_TERM;
1219b1869000SAlan Stern 		qh->is_stopped = 1;
1220b1869000SAlan Stern 		qh->needs_fixup = (qh->type != USB_ENDPOINT_XFER_CONTROL);
1221b1869000SAlan Stern 		qh->initial_toggle = uhci_toggle(td_token(td)) ^
1222b1869000SAlan Stern 				(ret == -EREMOTEIO);
1223b1869000SAlan Stern 
1224b1869000SAlan Stern 	} else		/* Short packet received */
122559e29ed9SAlan Stern 		ret = uhci_fixup_short_transfer(uhci, qh, urbp);
1226b1869000SAlan Stern 	return ret;
1227b1869000SAlan Stern }
1228b1869000SAlan Stern 
1229b1869000SAlan Stern /*
12301da177e4SLinus Torvalds  * Isochronous transfers
12311da177e4SLinus Torvalds  */
1232dccf4a48SAlan Stern static int uhci_submit_isochronous(struct uhci_hcd *uhci, struct urb *urb,
1233dccf4a48SAlan Stern 		struct uhci_qh *qh)
12341da177e4SLinus Torvalds {
1235dccf4a48SAlan Stern 	struct uhci_td *td = NULL;	/* Since urb->number_of_packets > 0 */
12360ed8fee1SAlan Stern 	int i, frame;
1237dccf4a48SAlan Stern 	unsigned long destination, status;
1238b81d3436SAlan Stern 	struct urb_priv *urbp = (struct urb_priv *) urb->hcpriv;
12391da177e4SLinus Torvalds 
1240caf3827aSAlan Stern 	/* Values must not be too big (could overflow below) */
1241caf3827aSAlan Stern 	if (urb->interval >= UHCI_NUMFRAMES ||
1242caf3827aSAlan Stern 			urb->number_of_packets >= UHCI_NUMFRAMES)
1243caf3827aSAlan Stern 		return -EFBIG;
1244caf3827aSAlan Stern 
1245caf3827aSAlan Stern 	/* Check the period and figure out the starting frame number */
12463ca2a321SAlan Stern 	if (!qh->bandwidth_reserved) {
12473ca2a321SAlan Stern 		qh->period = urb->interval;
1248caf3827aSAlan Stern 		if (urb->transfer_flags & URB_ISO_ASAP) {
12493ca2a321SAlan Stern 			qh->phase = -1;		/* Find the best phase */
12503ca2a321SAlan Stern 			i = uhci_check_bandwidth(uhci, qh);
12513ca2a321SAlan Stern 			if (i)
12523ca2a321SAlan Stern 				return i;
12533ca2a321SAlan Stern 
12543ca2a321SAlan Stern 			/* Allow a little time to allocate the TDs */
1255c8155cc5SAlan Stern 			uhci_get_current_frame_number(uhci);
12563ca2a321SAlan Stern 			frame = uhci->frame_number + 10;
12573ca2a321SAlan Stern 
12583ca2a321SAlan Stern 			/* Move forward to the first frame having the
12593ca2a321SAlan Stern 			 * correct phase */
12603ca2a321SAlan Stern 			urb->start_frame = frame + ((qh->phase - frame) &
12613ca2a321SAlan Stern 					(qh->period - 1));
1262caf3827aSAlan Stern 		} else {
1263c8155cc5SAlan Stern 			i = urb->start_frame - uhci->last_iso_frame;
1264caf3827aSAlan Stern 			if (i <= 0 || i >= UHCI_NUMFRAMES)
1265caf3827aSAlan Stern 				return -EINVAL;
12663ca2a321SAlan Stern 			qh->phase = urb->start_frame & (qh->period - 1);
12673ca2a321SAlan Stern 			i = uhci_check_bandwidth(uhci, qh);
12683ca2a321SAlan Stern 			if (i)
12693ca2a321SAlan Stern 				return i;
1270caf3827aSAlan Stern 		}
12713ca2a321SAlan Stern 
1272caf3827aSAlan Stern 	} else if (qh->period != urb->interval) {
1273caf3827aSAlan Stern 		return -EINVAL;		/* Can't change the period */
1274caf3827aSAlan Stern 
1275caf3827aSAlan Stern 	} else {	/* Pick up where the last URB leaves off */
1276caf3827aSAlan Stern 		if (list_empty(&qh->queue)) {
1277c8155cc5SAlan Stern 			frame = qh->iso_frame;
1278caf3827aSAlan Stern 		} else {
1279caf3827aSAlan Stern 			struct urb *lurb;
1280caf3827aSAlan Stern 
1281caf3827aSAlan Stern 			lurb = list_entry(qh->queue.prev,
1282caf3827aSAlan Stern 					struct urb_priv, node)->urb;
1283caf3827aSAlan Stern 			frame = lurb->start_frame +
1284caf3827aSAlan Stern 					lurb->number_of_packets *
1285caf3827aSAlan Stern 					lurb->interval;
1286caf3827aSAlan Stern 		}
1287caf3827aSAlan Stern 		if (urb->transfer_flags & URB_ISO_ASAP)
1288caf3827aSAlan Stern 			urb->start_frame = frame;
1289c8155cc5SAlan Stern 		else if (urb->start_frame != frame)
1290c8155cc5SAlan Stern 			return -EINVAL;
1291caf3827aSAlan Stern 	}
1292caf3827aSAlan Stern 
1293caf3827aSAlan Stern 	/* Make sure we won't have to go too far into the future */
1294c8155cc5SAlan Stern 	if (uhci_frame_before_eq(uhci->last_iso_frame + UHCI_NUMFRAMES,
1295caf3827aSAlan Stern 			urb->start_frame + urb->number_of_packets *
1296caf3827aSAlan Stern 				urb->interval))
12970ed8fee1SAlan Stern 		return -EFBIG;
12980ed8fee1SAlan Stern 
12991da177e4SLinus Torvalds 	status = TD_CTRL_ACTIVE | TD_CTRL_IOS;
13001da177e4SLinus Torvalds 	destination = (urb->pipe & PIPE_DEVEP_MASK) | usb_packetid(urb->pipe);
13011da177e4SLinus Torvalds 
1302b81d3436SAlan Stern 	for (i = 0; i < urb->number_of_packets; i++) {
13032532178aSAlan Stern 		td = uhci_alloc_td(uhci);
13041da177e4SLinus Torvalds 		if (!td)
13051da177e4SLinus Torvalds 			return -ENOMEM;
13061da177e4SLinus Torvalds 
130704538a25SAlan Stern 		uhci_add_td_to_urbp(td, urbp);
1308dccf4a48SAlan Stern 		uhci_fill_td(td, status, destination |
1309dccf4a48SAlan Stern 				uhci_explen(urb->iso_frame_desc[i].length),
1310dccf4a48SAlan Stern 				urb->transfer_dma +
1311dccf4a48SAlan Stern 					urb->iso_frame_desc[i].offset);
1312b81d3436SAlan Stern 	}
13131da177e4SLinus Torvalds 
1314dccf4a48SAlan Stern 	/* Set the interrupt-on-completion flag on the last packet. */
1315dccf4a48SAlan Stern 	td->status |= __constant_cpu_to_le32(TD_CTRL_IOC);
1316dccf4a48SAlan Stern 
1317dccf4a48SAlan Stern 	/* Add the TDs to the frame list */
1318b81d3436SAlan Stern 	frame = urb->start_frame;
1319b81d3436SAlan Stern 	list_for_each_entry(td, &urbp->td_list, list) {
1320dccf4a48SAlan Stern 		uhci_insert_td_in_frame_list(uhci, td, frame);
1321c8155cc5SAlan Stern 		frame += qh->period;
1322c8155cc5SAlan Stern 	}
1323c8155cc5SAlan Stern 
1324c8155cc5SAlan Stern 	if (list_empty(&qh->queue)) {
1325c8155cc5SAlan Stern 		qh->iso_packet_desc = &urb->iso_frame_desc[0];
1326c8155cc5SAlan Stern 		qh->iso_frame = urb->start_frame;
1327c8155cc5SAlan Stern 		qh->iso_status = 0;
13281da177e4SLinus Torvalds 	}
13291da177e4SLinus Torvalds 
133017230acdSAlan Stern 	qh->skel = SKEL_ISO;
13313ca2a321SAlan Stern 	if (!qh->bandwidth_reserved)
13323ca2a321SAlan Stern 		uhci_reserve_bandwidth(uhci, qh);
1333dccf4a48SAlan Stern 	return 0;
13341da177e4SLinus Torvalds }
13351da177e4SLinus Torvalds 
13361da177e4SLinus Torvalds static int uhci_result_isochronous(struct uhci_hcd *uhci, struct urb *urb)
13371da177e4SLinus Torvalds {
1338c8155cc5SAlan Stern 	struct uhci_td *td, *tmp;
1339c8155cc5SAlan Stern 	struct urb_priv *urbp = urb->hcpriv;
1340c8155cc5SAlan Stern 	struct uhci_qh *qh = urbp->qh;
1341c8155cc5SAlan Stern 
1342c8155cc5SAlan Stern 	list_for_each_entry_safe(td, tmp, &urbp->td_list, list) {
1343c8155cc5SAlan Stern 		unsigned int ctrlstat;
13441da177e4SLinus Torvalds 		int status;
13451da177e4SLinus Torvalds 		int actlength;
13461da177e4SLinus Torvalds 
1347c8155cc5SAlan Stern 		if (uhci_frame_before_eq(uhci->cur_iso_frame, qh->iso_frame))
13481da177e4SLinus Torvalds 			return -EINPROGRESS;
13491da177e4SLinus Torvalds 
1350c8155cc5SAlan Stern 		uhci_remove_tds_from_frame(uhci, qh->iso_frame);
13511da177e4SLinus Torvalds 
1352c8155cc5SAlan Stern 		ctrlstat = td_status(td);
1353c8155cc5SAlan Stern 		if (ctrlstat & TD_CTRL_ACTIVE) {
1354c8155cc5SAlan Stern 			status = -EXDEV;	/* TD was added too late? */
1355c8155cc5SAlan Stern 		} else {
13561da177e4SLinus Torvalds 			status = uhci_map_status(uhci_status_bits(ctrlstat),
13571da177e4SLinus Torvalds 					usb_pipeout(urb->pipe));
1358c8155cc5SAlan Stern 			actlength = uhci_actual_length(ctrlstat);
1359c8155cc5SAlan Stern 
1360c8155cc5SAlan Stern 			urb->actual_length += actlength;
1361c8155cc5SAlan Stern 			qh->iso_packet_desc->actual_length = actlength;
1362c8155cc5SAlan Stern 			qh->iso_packet_desc->status = status;
1363c8155cc5SAlan Stern 		}
1364c8155cc5SAlan Stern 
13651da177e4SLinus Torvalds 		if (status) {
13661da177e4SLinus Torvalds 			urb->error_count++;
1367c8155cc5SAlan Stern 			qh->iso_status = status;
13681da177e4SLinus Torvalds 		}
13691da177e4SLinus Torvalds 
1370c8155cc5SAlan Stern 		uhci_remove_td_from_urbp(td);
1371c8155cc5SAlan Stern 		uhci_free_td(uhci, td);
1372c8155cc5SAlan Stern 		qh->iso_frame += qh->period;
1373c8155cc5SAlan Stern 		++qh->iso_packet_desc;
13741da177e4SLinus Torvalds 	}
1375c8155cc5SAlan Stern 	return qh->iso_status;
13761da177e4SLinus Torvalds }
13771da177e4SLinus Torvalds 
13781da177e4SLinus Torvalds static int uhci_urb_enqueue(struct usb_hcd *hcd,
1379dccf4a48SAlan Stern 		struct usb_host_endpoint *hep,
138055016f10SAl Viro 		struct urb *urb, gfp_t mem_flags)
13811da177e4SLinus Torvalds {
13821da177e4SLinus Torvalds 	int ret;
13831da177e4SLinus Torvalds 	struct uhci_hcd *uhci = hcd_to_uhci(hcd);
13841da177e4SLinus Torvalds 	unsigned long flags;
1385dccf4a48SAlan Stern 	struct urb_priv *urbp;
1386dccf4a48SAlan Stern 	struct uhci_qh *qh;
13871da177e4SLinus Torvalds 
13881da177e4SLinus Torvalds 	spin_lock_irqsave(&uhci->lock, flags);
13891da177e4SLinus Torvalds 
13901da177e4SLinus Torvalds 	ret = urb->status;
13911da177e4SLinus Torvalds 	if (ret != -EINPROGRESS)		/* URB already unlinked! */
1392dccf4a48SAlan Stern 		goto done;
13931da177e4SLinus Torvalds 
13941da177e4SLinus Torvalds 	ret = -ENOMEM;
1395dccf4a48SAlan Stern 	urbp = uhci_alloc_urb_priv(uhci, urb);
1396dccf4a48SAlan Stern 	if (!urbp)
1397dccf4a48SAlan Stern 		goto done;
1398dccf4a48SAlan Stern 
1399dccf4a48SAlan Stern 	if (hep->hcpriv)
1400dccf4a48SAlan Stern 		qh = (struct uhci_qh *) hep->hcpriv;
1401dccf4a48SAlan Stern 	else {
1402dccf4a48SAlan Stern 		qh = uhci_alloc_qh(uhci, urb->dev, hep);
1403dccf4a48SAlan Stern 		if (!qh)
1404dccf4a48SAlan Stern 			goto err_no_qh;
14051da177e4SLinus Torvalds 	}
1406dccf4a48SAlan Stern 	urbp->qh = qh;
14071da177e4SLinus Torvalds 
14084de7d2c2SAlan Stern 	switch (qh->type) {
14094de7d2c2SAlan Stern 	case USB_ENDPOINT_XFER_CONTROL:
1410dccf4a48SAlan Stern 		ret = uhci_submit_control(uhci, urb, qh);
1411dccf4a48SAlan Stern 		break;
14124de7d2c2SAlan Stern 	case USB_ENDPOINT_XFER_BULK:
1413dccf4a48SAlan Stern 		ret = uhci_submit_bulk(uhci, urb, qh);
14141da177e4SLinus Torvalds 		break;
14154de7d2c2SAlan Stern 	case USB_ENDPOINT_XFER_INT:
1416dccf4a48SAlan Stern 		ret = uhci_submit_interrupt(uhci, urb, qh);
14171da177e4SLinus Torvalds 		break;
14184de7d2c2SAlan Stern 	case USB_ENDPOINT_XFER_ISOC:
1419c8155cc5SAlan Stern 		urb->error_count = 0;
1420dccf4a48SAlan Stern 		ret = uhci_submit_isochronous(uhci, urb, qh);
14211da177e4SLinus Torvalds 		break;
14221da177e4SLinus Torvalds 	}
1423dccf4a48SAlan Stern 	if (ret != 0)
1424dccf4a48SAlan Stern 		goto err_submit_failed;
14251da177e4SLinus Torvalds 
1426dccf4a48SAlan Stern 	/* Add this URB to the QH */
1427dccf4a48SAlan Stern 	urbp->qh = qh;
1428dccf4a48SAlan Stern 	list_add_tail(&urbp->node, &qh->queue);
14291da177e4SLinus Torvalds 
1430dccf4a48SAlan Stern 	/* If the new URB is the first and only one on this QH then either
1431dccf4a48SAlan Stern 	 * the QH is new and idle or else it's unlinked and waiting to
14322775562aSAlan Stern 	 * become idle, so we can activate it right away.  But only if the
14332775562aSAlan Stern 	 * queue isn't stopped. */
143484afddd7SAlan Stern 	if (qh->queue.next == &urbp->node && !qh->is_stopped) {
1435dccf4a48SAlan Stern 		uhci_activate_qh(uhci, qh);
1436c5e3b741SAlan Stern 		uhci_urbp_wants_fsbr(uhci, urbp);
143784afddd7SAlan Stern 	}
1438dccf4a48SAlan Stern 	goto done;
1439dccf4a48SAlan Stern 
1440dccf4a48SAlan Stern err_submit_failed:
1441dccf4a48SAlan Stern 	if (qh->state == QH_STATE_IDLE)
1442dccf4a48SAlan Stern 		uhci_make_qh_idle(uhci, qh);	/* Reclaim unused QH */
1443dccf4a48SAlan Stern 
1444dccf4a48SAlan Stern err_no_qh:
1445dccf4a48SAlan Stern 	uhci_free_urb_priv(uhci, urbp);
1446dccf4a48SAlan Stern 
1447dccf4a48SAlan Stern done:
14481da177e4SLinus Torvalds 	spin_unlock_irqrestore(&uhci->lock, flags);
14491da177e4SLinus Torvalds 	return ret;
14501da177e4SLinus Torvalds }
14511da177e4SLinus Torvalds 
14521da177e4SLinus Torvalds static int uhci_urb_dequeue(struct usb_hcd *hcd, struct urb *urb)
14531da177e4SLinus Torvalds {
14541da177e4SLinus Torvalds 	struct uhci_hcd *uhci = hcd_to_uhci(hcd);
14551da177e4SLinus Torvalds 	unsigned long flags;
14561da177e4SLinus Torvalds 	struct urb_priv *urbp;
145710b8e47dSAlan Stern 	struct uhci_qh *qh;
14581da177e4SLinus Torvalds 
14591da177e4SLinus Torvalds 	spin_lock_irqsave(&uhci->lock, flags);
14601da177e4SLinus Torvalds 	urbp = urb->hcpriv;
14611da177e4SLinus Torvalds 	if (!urbp)			/* URB was never linked! */
14621da177e4SLinus Torvalds 		goto done;
146310b8e47dSAlan Stern 	qh = urbp->qh;
14641da177e4SLinus Torvalds 
1465dccf4a48SAlan Stern 	/* Remove Isochronous TDs from the frame list ASAP */
146610b8e47dSAlan Stern 	if (qh->type == USB_ENDPOINT_XFER_ISOC) {
1467dccf4a48SAlan Stern 		uhci_unlink_isochronous_tds(uhci, urb);
146810b8e47dSAlan Stern 		mb();
146910b8e47dSAlan Stern 
147010b8e47dSAlan Stern 		/* If the URB has already started, update the QH unlink time */
147110b8e47dSAlan Stern 		uhci_get_current_frame_number(uhci);
147210b8e47dSAlan Stern 		if (uhci_frame_before_eq(urb->start_frame, uhci->frame_number))
147310b8e47dSAlan Stern 			qh->unlink_frame = uhci->frame_number;
147410b8e47dSAlan Stern 	}
147510b8e47dSAlan Stern 
147610b8e47dSAlan Stern 	uhci_unlink_qh(uhci, qh);
14771da177e4SLinus Torvalds 
14781da177e4SLinus Torvalds done:
14791da177e4SLinus Torvalds 	spin_unlock_irqrestore(&uhci->lock, flags);
14801da177e4SLinus Torvalds 	return 0;
14811da177e4SLinus Torvalds }
14821da177e4SLinus Torvalds 
14830ed8fee1SAlan Stern /*
14840ed8fee1SAlan Stern  * Finish unlinking an URB and give it back
14850ed8fee1SAlan Stern  */
14860ed8fee1SAlan Stern static void uhci_giveback_urb(struct uhci_hcd *uhci, struct uhci_qh *qh,
14877d12e780SDavid Howells 		struct urb *urb)
14880ed8fee1SAlan Stern __releases(uhci->lock)
14890ed8fee1SAlan Stern __acquires(uhci->lock)
14901da177e4SLinus Torvalds {
14911da177e4SLinus Torvalds 	struct urb_priv *urbp = (struct urb_priv *) urb->hcpriv;
14921da177e4SLinus Torvalds 
1493*e7e7c360SAlan Stern 	if (qh->type == USB_ENDPOINT_XFER_CONTROL) {
1494*e7e7c360SAlan Stern 
1495*e7e7c360SAlan Stern 		/* urb->actual_length < 0 means the setup transaction didn't
1496*e7e7c360SAlan Stern 		 * complete successfully.  Either it failed or the URB was
1497*e7e7c360SAlan Stern 		 * unlinked first.  Regardless, don't confuse people with a
1498*e7e7c360SAlan Stern 		 * negative length. */
1499*e7e7c360SAlan Stern 		urb->actual_length = max(urb->actual_length, 0);
1500*e7e7c360SAlan Stern 
1501*e7e7c360SAlan Stern 		/* Report erroneous short transfers */
1502*e7e7c360SAlan Stern 		if (unlikely((urb->transfer_flags & URB_SHORT_NOT_OK) &&
1503*e7e7c360SAlan Stern 				urb->actual_length <
1504*e7e7c360SAlan Stern 					urb->transfer_buffer_length &&
1505*e7e7c360SAlan Stern 				urb->status == 0))
1506*e7e7c360SAlan Stern 			urb->status = -EREMOTEIO;
1507*e7e7c360SAlan Stern 	}
1508*e7e7c360SAlan Stern 
1509c8155cc5SAlan Stern 	/* When giving back the first URB in an Isochronous queue,
1510c8155cc5SAlan Stern 	 * reinitialize the QH's iso-related members for the next URB. */
1511*e7e7c360SAlan Stern 	else if (qh->type == USB_ENDPOINT_XFER_ISOC &&
1512c8155cc5SAlan Stern 			urbp->node.prev == &qh->queue &&
1513c8155cc5SAlan Stern 			urbp->node.next != &qh->queue) {
1514c8155cc5SAlan Stern 		struct urb *nurb = list_entry(urbp->node.next,
1515c8155cc5SAlan Stern 				struct urb_priv, node)->urb;
1516c8155cc5SAlan Stern 
1517c8155cc5SAlan Stern 		qh->iso_packet_desc = &nurb->iso_frame_desc[0];
1518c8155cc5SAlan Stern 		qh->iso_frame = nurb->start_frame;
1519c8155cc5SAlan Stern 		qh->iso_status = 0;
1520c8155cc5SAlan Stern 	}
15211da177e4SLinus Torvalds 
15220ed8fee1SAlan Stern 	/* Take the URB off the QH's queue.  If the queue is now empty,
15230ed8fee1SAlan Stern 	 * this is a perfect time for a toggle fixup. */
15240ed8fee1SAlan Stern 	list_del_init(&urbp->node);
15250ed8fee1SAlan Stern 	if (list_empty(&qh->queue) && qh->needs_fixup) {
15260ed8fee1SAlan Stern 		usb_settoggle(urb->dev, usb_pipeendpoint(urb->pipe),
15270ed8fee1SAlan Stern 				usb_pipeout(urb->pipe), qh->initial_toggle);
15280ed8fee1SAlan Stern 		qh->needs_fixup = 0;
15290ed8fee1SAlan Stern 	}
15300ed8fee1SAlan Stern 
15310ed8fee1SAlan Stern 	uhci_free_urb_priv(uhci, urbp);
15320ed8fee1SAlan Stern 
15330ed8fee1SAlan Stern 	spin_unlock(&uhci->lock);
15347d12e780SDavid Howells 	usb_hcd_giveback_urb(uhci_to_hcd(uhci), urb);
15350ed8fee1SAlan Stern 	spin_lock(&uhci->lock);
15360ed8fee1SAlan Stern 
15370ed8fee1SAlan Stern 	/* If the queue is now empty, we can unlink the QH and give up its
15380ed8fee1SAlan Stern 	 * reserved bandwidth. */
15390ed8fee1SAlan Stern 	if (list_empty(&qh->queue)) {
15400ed8fee1SAlan Stern 		uhci_unlink_qh(uhci, qh);
15413ca2a321SAlan Stern 		if (qh->bandwidth_reserved)
15423ca2a321SAlan Stern 			uhci_release_bandwidth(uhci, qh);
15430ed8fee1SAlan Stern 	}
15440ed8fee1SAlan Stern }
15450ed8fee1SAlan Stern 
15460ed8fee1SAlan Stern /*
15470ed8fee1SAlan Stern  * Scan the URBs in a QH's queue
15480ed8fee1SAlan Stern  */
15490ed8fee1SAlan Stern #define QH_FINISHED_UNLINKING(qh)			\
15500ed8fee1SAlan Stern 		(qh->state == QH_STATE_UNLINKING &&	\
15510ed8fee1SAlan Stern 		uhci->frame_number + uhci->is_stopped != qh->unlink_frame)
15520ed8fee1SAlan Stern 
15537d12e780SDavid Howells static void uhci_scan_qh(struct uhci_hcd *uhci, struct uhci_qh *qh)
15540ed8fee1SAlan Stern {
15550ed8fee1SAlan Stern 	struct urb_priv *urbp;
15560ed8fee1SAlan Stern 	struct urb *urb;
15570ed8fee1SAlan Stern 	int status;
15580ed8fee1SAlan Stern 
15590ed8fee1SAlan Stern 	while (!list_empty(&qh->queue)) {
15600ed8fee1SAlan Stern 		urbp = list_entry(qh->queue.next, struct urb_priv, node);
15610ed8fee1SAlan Stern 		urb = urbp->urb;
15620ed8fee1SAlan Stern 
1563b1869000SAlan Stern 		if (qh->type == USB_ENDPOINT_XFER_ISOC)
15640ed8fee1SAlan Stern 			status = uhci_result_isochronous(uhci, urb);
1565b1869000SAlan Stern 		else
15660ed8fee1SAlan Stern 			status = uhci_result_common(uhci, urb);
15670ed8fee1SAlan Stern 		if (status == -EINPROGRESS)
15680ed8fee1SAlan Stern 			break;
15690ed8fee1SAlan Stern 
15700ed8fee1SAlan Stern 		spin_lock(&urb->lock);
15710ed8fee1SAlan Stern 		if (urb->status == -EINPROGRESS)	/* Not dequeued */
15720ed8fee1SAlan Stern 			urb->status = status;
15730ed8fee1SAlan Stern 		else
15742775562aSAlan Stern 			status = ECONNRESET;		/* Not -ECONNRESET */
15750ed8fee1SAlan Stern 		spin_unlock(&urb->lock);
15760ed8fee1SAlan Stern 
15770ed8fee1SAlan Stern 		/* Dequeued but completed URBs can't be given back unless
15780ed8fee1SAlan Stern 		 * the QH is stopped or has finished unlinking. */
15792775562aSAlan Stern 		if (status == ECONNRESET) {
15802775562aSAlan Stern 			if (QH_FINISHED_UNLINKING(qh))
15812775562aSAlan Stern 				qh->is_stopped = 1;
15822775562aSAlan Stern 			else if (!qh->is_stopped)
15830ed8fee1SAlan Stern 				return;
15842775562aSAlan Stern 		}
15850ed8fee1SAlan Stern 
15867d12e780SDavid Howells 		uhci_giveback_urb(uhci, qh, urb);
15877ceb932fSAlan Stern 		if (status < 0 && qh->type != USB_ENDPOINT_XFER_ISOC)
15880ed8fee1SAlan Stern 			break;
15890ed8fee1SAlan Stern 	}
15900ed8fee1SAlan Stern 
15910ed8fee1SAlan Stern 	/* If the QH is neither stopped nor finished unlinking (normal case),
15920ed8fee1SAlan Stern 	 * our work here is done. */
15932775562aSAlan Stern 	if (QH_FINISHED_UNLINKING(qh))
15942775562aSAlan Stern 		qh->is_stopped = 1;
15952775562aSAlan Stern 	else if (!qh->is_stopped)
15960ed8fee1SAlan Stern 		return;
15970ed8fee1SAlan Stern 
15980ed8fee1SAlan Stern 	/* Otherwise give back each of the dequeued URBs */
15992775562aSAlan Stern restart:
16000ed8fee1SAlan Stern 	list_for_each_entry(urbp, &qh->queue, node) {
16010ed8fee1SAlan Stern 		urb = urbp->urb;
16020ed8fee1SAlan Stern 		if (urb->status != -EINPROGRESS) {
160310b8e47dSAlan Stern 
160410b8e47dSAlan Stern 			/* Fix up the TD links and save the toggles for
160510b8e47dSAlan Stern 			 * non-Isochronous queues.  For Isochronous queues,
160610b8e47dSAlan Stern 			 * test for too-recent dequeues. */
160710b8e47dSAlan Stern 			if (!uhci_cleanup_queue(uhci, qh, urb)) {
160810b8e47dSAlan Stern 				qh->is_stopped = 0;
160910b8e47dSAlan Stern 				return;
161010b8e47dSAlan Stern 			}
16117d12e780SDavid Howells 			uhci_giveback_urb(uhci, qh, urb);
16120ed8fee1SAlan Stern 			goto restart;
16130ed8fee1SAlan Stern 		}
16140ed8fee1SAlan Stern 	}
16150ed8fee1SAlan Stern 	qh->is_stopped = 0;
16160ed8fee1SAlan Stern 
16170ed8fee1SAlan Stern 	/* There are no more dequeued URBs.  If there are still URBs on the
16180ed8fee1SAlan Stern 	 * queue, the QH can now be re-activated. */
16190ed8fee1SAlan Stern 	if (!list_empty(&qh->queue)) {
16200ed8fee1SAlan Stern 		if (qh->needs_fixup)
16210ed8fee1SAlan Stern 			uhci_fixup_toggles(qh, 0);
162284afddd7SAlan Stern 
162384afddd7SAlan Stern 		/* If the first URB on the queue wants FSBR but its time
162484afddd7SAlan Stern 		 * limit has expired, set the next TD to interrupt on
162584afddd7SAlan Stern 		 * completion before reactivating the QH. */
162684afddd7SAlan Stern 		urbp = list_entry(qh->queue.next, struct urb_priv, node);
162784afddd7SAlan Stern 		if (urbp->fsbr && qh->wait_expired) {
162884afddd7SAlan Stern 			struct uhci_td *td = list_entry(urbp->td_list.next,
162984afddd7SAlan Stern 					struct uhci_td, list);
163084afddd7SAlan Stern 
163184afddd7SAlan Stern 			td->status |= __cpu_to_le32(TD_CTRL_IOC);
163284afddd7SAlan Stern 		}
163384afddd7SAlan Stern 
16340ed8fee1SAlan Stern 		uhci_activate_qh(uhci, qh);
16350ed8fee1SAlan Stern 	}
16360ed8fee1SAlan Stern 
16370ed8fee1SAlan Stern 	/* The queue is empty.  The QH can become idle if it is fully
16380ed8fee1SAlan Stern 	 * unlinked. */
16390ed8fee1SAlan Stern 	else if (QH_FINISHED_UNLINKING(qh))
16400ed8fee1SAlan Stern 		uhci_make_qh_idle(uhci, qh);
16411da177e4SLinus Torvalds }
16421da177e4SLinus Torvalds 
16430ed8fee1SAlan Stern /*
164484afddd7SAlan Stern  * Check for queues that have made some forward progress.
164584afddd7SAlan Stern  * Returns 0 if the queue is not Isochronous, is ACTIVE, and
164684afddd7SAlan Stern  * has not advanced since last examined; 1 otherwise.
1647b761d9d8SAlan Stern  *
1648b761d9d8SAlan Stern  * Early Intel controllers have a bug which causes qh->element sometimes
1649b761d9d8SAlan Stern  * not to advance when a TD completes successfully.  The queue remains
1650b761d9d8SAlan Stern  * stuck on the inactive completed TD.  We detect such cases and advance
1651b761d9d8SAlan Stern  * the element pointer by hand.
165284afddd7SAlan Stern  */
165384afddd7SAlan Stern static int uhci_advance_check(struct uhci_hcd *uhci, struct uhci_qh *qh)
165484afddd7SAlan Stern {
165584afddd7SAlan Stern 	struct urb_priv *urbp = NULL;
165684afddd7SAlan Stern 	struct uhci_td *td;
165784afddd7SAlan Stern 	int ret = 1;
165884afddd7SAlan Stern 	unsigned status;
165984afddd7SAlan Stern 
166084afddd7SAlan Stern 	if (qh->type == USB_ENDPOINT_XFER_ISOC)
1661c5e3b741SAlan Stern 		goto done;
166284afddd7SAlan Stern 
166384afddd7SAlan Stern 	/* Treat an UNLINKING queue as though it hasn't advanced.
166484afddd7SAlan Stern 	 * This is okay because reactivation will treat it as though
166584afddd7SAlan Stern 	 * it has advanced, and if it is going to become IDLE then
166684afddd7SAlan Stern 	 * this doesn't matter anyway.  Furthermore it's possible
166784afddd7SAlan Stern 	 * for an UNLINKING queue not to have any URBs at all, or
166884afddd7SAlan Stern 	 * for its first URB not to have any TDs (if it was dequeued
166984afddd7SAlan Stern 	 * just as it completed).  So it's not easy in any case to
167084afddd7SAlan Stern 	 * test whether such queues have advanced. */
167184afddd7SAlan Stern 	if (qh->state != QH_STATE_ACTIVE) {
167284afddd7SAlan Stern 		urbp = NULL;
167384afddd7SAlan Stern 		status = 0;
167484afddd7SAlan Stern 
167584afddd7SAlan Stern 	} else {
167684afddd7SAlan Stern 		urbp = list_entry(qh->queue.next, struct urb_priv, node);
167784afddd7SAlan Stern 		td = list_entry(urbp->td_list.next, struct uhci_td, list);
167884afddd7SAlan Stern 		status = td_status(td);
167984afddd7SAlan Stern 		if (!(status & TD_CTRL_ACTIVE)) {
168084afddd7SAlan Stern 
168184afddd7SAlan Stern 			/* We're okay, the queue has advanced */
168284afddd7SAlan Stern 			qh->wait_expired = 0;
168384afddd7SAlan Stern 			qh->advance_jiffies = jiffies;
1684c5e3b741SAlan Stern 			goto done;
168584afddd7SAlan Stern 		}
168684afddd7SAlan Stern 		ret = 0;
168784afddd7SAlan Stern 	}
168884afddd7SAlan Stern 
168984afddd7SAlan Stern 	/* The queue hasn't advanced; check for timeout */
1690c5e3b741SAlan Stern 	if (qh->wait_expired)
1691c5e3b741SAlan Stern 		goto done;
1692c5e3b741SAlan Stern 
1693c5e3b741SAlan Stern 	if (time_after(jiffies, qh->advance_jiffies + QH_WAIT_TIMEOUT)) {
1694b761d9d8SAlan Stern 
1695b761d9d8SAlan Stern 		/* Detect the Intel bug and work around it */
169628b9325eSAlan Stern 		if (qh->post_td && qh_element(qh) == LINK_TO_TD(qh->post_td)) {
1697b761d9d8SAlan Stern 			qh->element = qh->post_td->link;
1698b761d9d8SAlan Stern 			qh->advance_jiffies = jiffies;
1699c5e3b741SAlan Stern 			ret = 1;
1700c5e3b741SAlan Stern 			goto done;
1701b761d9d8SAlan Stern 		}
1702b761d9d8SAlan Stern 
170384afddd7SAlan Stern 		qh->wait_expired = 1;
170484afddd7SAlan Stern 
170584afddd7SAlan Stern 		/* If the current URB wants FSBR, unlink it temporarily
170684afddd7SAlan Stern 		 * so that we can safely set the next TD to interrupt on
170784afddd7SAlan Stern 		 * completion.  That way we'll know as soon as the queue
170884afddd7SAlan Stern 		 * starts moving again. */
170984afddd7SAlan Stern 		if (urbp && urbp->fsbr && !(status & TD_CTRL_IOC))
171084afddd7SAlan Stern 			uhci_unlink_qh(uhci, qh);
1711c5e3b741SAlan Stern 
1712c5e3b741SAlan Stern 	} else {
1713c5e3b741SAlan Stern 		/* Unmoving but not-yet-expired queues keep FSBR alive */
1714c5e3b741SAlan Stern 		if (urbp)
1715c5e3b741SAlan Stern 			uhci_urbp_wants_fsbr(uhci, urbp);
171684afddd7SAlan Stern 	}
1717c5e3b741SAlan Stern 
1718c5e3b741SAlan Stern done:
171984afddd7SAlan Stern 	return ret;
172084afddd7SAlan Stern }
172184afddd7SAlan Stern 
172284afddd7SAlan Stern /*
17230ed8fee1SAlan Stern  * Process events in the schedule, but only in one thread at a time
17240ed8fee1SAlan Stern  */
17257d12e780SDavid Howells static void uhci_scan_schedule(struct uhci_hcd *uhci)
17261da177e4SLinus Torvalds {
17270ed8fee1SAlan Stern 	int i;
17280ed8fee1SAlan Stern 	struct uhci_qh *qh;
17291da177e4SLinus Torvalds 
17301da177e4SLinus Torvalds 	/* Don't allow re-entrant calls */
17311da177e4SLinus Torvalds 	if (uhci->scan_in_progress) {
17321da177e4SLinus Torvalds 		uhci->need_rescan = 1;
17331da177e4SLinus Torvalds 		return;
17341da177e4SLinus Torvalds 	}
17351da177e4SLinus Torvalds 	uhci->scan_in_progress = 1;
17361da177e4SLinus Torvalds rescan:
17371da177e4SLinus Torvalds 	uhci->need_rescan = 0;
1738c5e3b741SAlan Stern 	uhci->fsbr_is_wanted = 0;
17391da177e4SLinus Torvalds 
17406c1b445cSAlan Stern 	uhci_clear_next_interrupt(uhci);
17411da177e4SLinus Torvalds 	uhci_get_current_frame_number(uhci);
1742c8155cc5SAlan Stern 	uhci->cur_iso_frame = uhci->frame_number;
17431da177e4SLinus Torvalds 
17440ed8fee1SAlan Stern 	/* Go through all the QH queues and process the URBs in each one */
17450ed8fee1SAlan Stern 	for (i = 0; i < UHCI_NUM_SKELQH - 1; ++i) {
17460ed8fee1SAlan Stern 		uhci->next_qh = list_entry(uhci->skelqh[i]->node.next,
17470ed8fee1SAlan Stern 				struct uhci_qh, node);
17480ed8fee1SAlan Stern 		while ((qh = uhci->next_qh) != uhci->skelqh[i]) {
17490ed8fee1SAlan Stern 			uhci->next_qh = list_entry(qh->node.next,
17500ed8fee1SAlan Stern 					struct uhci_qh, node);
175184afddd7SAlan Stern 
175284afddd7SAlan Stern 			if (uhci_advance_check(uhci, qh)) {
17537d12e780SDavid Howells 				uhci_scan_qh(uhci, qh);
1754c5e3b741SAlan Stern 				if (qh->state == QH_STATE_ACTIVE) {
1755c5e3b741SAlan Stern 					uhci_urbp_wants_fsbr(uhci,
1756c5e3b741SAlan Stern 	list_entry(qh->queue.next, struct urb_priv, node));
1757c5e3b741SAlan Stern 				}
175884afddd7SAlan Stern 			}
17591da177e4SLinus Torvalds 		}
17600ed8fee1SAlan Stern 	}
17611da177e4SLinus Torvalds 
1762c8155cc5SAlan Stern 	uhci->last_iso_frame = uhci->cur_iso_frame;
17631da177e4SLinus Torvalds 	if (uhci->need_rescan)
17641da177e4SLinus Torvalds 		goto rescan;
17651da177e4SLinus Torvalds 	uhci->scan_in_progress = 0;
17661da177e4SLinus Torvalds 
1767c5e3b741SAlan Stern 	if (uhci->fsbr_is_on && !uhci->fsbr_is_wanted &&
1768c5e3b741SAlan Stern 			!uhci->fsbr_expiring) {
1769c5e3b741SAlan Stern 		uhci->fsbr_expiring = 1;
1770c5e3b741SAlan Stern 		mod_timer(&uhci->fsbr_timer, jiffies + FSBR_OFF_DELAY);
1771c5e3b741SAlan Stern 	}
177284afddd7SAlan Stern 
177304538a25SAlan Stern 	if (list_empty(&uhci->skel_unlink_qh->node))
17741da177e4SLinus Torvalds 		uhci_clear_next_interrupt(uhci);
17751da177e4SLinus Torvalds 	else
17761da177e4SLinus Torvalds 		uhci_set_next_interrupt(uhci);
17771da177e4SLinus Torvalds }
1778