aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/usb/host/uhci-q.c
diff options
context:
space:
mode:
authorAlan Stern <stern@rowland.harvard.edu>2005-12-17 17:58:46 -0500
committerGreg Kroah-Hartman <gregkh@suse.de>2006-03-20 17:49:57 -0500
commitdccf4a48d47120a42382ba526f1a0848c13ba2a4 (patch)
tree788a0a9f491d1a42df1dee1781156ccfc363b6ef /drivers/usb/host/uhci-q.c
parent499003e815344304c7b0c93aad923ddf644d24e0 (diff)
[PATCH] UHCI: use one QH per endpoint, not per URB
This patch (as623) changes the uhci-hcd driver to make it use one QH per device endpoint, instead of a QH per URB as it does now. Numerous areas of the code are affected by this. For example, the distinction between "queued" URBs and non-"queued" URBs no longer exists; all URBs belong to a queue and some just happen to be at the queue's head. Signed-off-by: Alan Stern <stern@rowland.harvard.edu> Signed-off-by: Greg Kroah-Hartman <gregkh@suse.de>
Diffstat (limited to 'drivers/usb/host/uhci-q.c')
-rw-r--r--drivers/usb/host/uhci-q.c985
1 files changed, 456 insertions, 529 deletions
diff --git a/drivers/usb/host/uhci-q.c b/drivers/usb/host/uhci-q.c
index 782398045f9f..b1b551a3d14e 100644
--- a/drivers/usb/host/uhci-q.c
+++ b/drivers/usb/host/uhci-q.c
@@ -13,13 +13,9 @@
13 * (C) Copyright 2000 Yggdrasil Computing, Inc. (port of new PCI interface 13 * (C) Copyright 2000 Yggdrasil Computing, Inc. (port of new PCI interface
14 * support from usb-ohci.c by Adam Richter, adam@yggdrasil.com). 14 * support from usb-ohci.c by Adam Richter, adam@yggdrasil.com).
15 * (C) Copyright 1999 Gregory P. Smith (from usb-ohci.c) 15 * (C) Copyright 1999 Gregory P. Smith (from usb-ohci.c)
16 * (C) Copyright 2004 Alan Stern, stern@rowland.harvard.edu 16 * (C) Copyright 2004-2005 Alan Stern, stern@rowland.harvard.edu
17 */ 17 */
18 18
19static int uhci_urb_dequeue(struct usb_hcd *hcd, struct urb *urb);
20static void uhci_unlink_generic(struct uhci_hcd *uhci, struct urb *urb);
21static void uhci_remove_pending_urbps(struct uhci_hcd *uhci);
22static void uhci_free_pending_qhs(struct uhci_hcd *uhci);
23static void uhci_free_pending_tds(struct uhci_hcd *uhci); 19static void uhci_free_pending_tds(struct uhci_hcd *uhci);
24 20
25/* 21/*
@@ -30,7 +26,7 @@ static void uhci_free_pending_tds(struct uhci_hcd *uhci);
30 * games with the FSBR code to make sure we get the correct order in all 26 * games with the FSBR code to make sure we get the correct order in all
31 * the cases. I don't think it's worth the effort 27 * the cases. I don't think it's worth the effort
32 */ 28 */
33static inline void uhci_set_next_interrupt(struct uhci_hcd *uhci) 29static void uhci_set_next_interrupt(struct uhci_hcd *uhci)
34{ 30{
35 if (uhci->is_stopped) 31 if (uhci->is_stopped)
36 mod_timer(&uhci_to_hcd(uhci)->rh_timer, jiffies); 32 mod_timer(&uhci_to_hcd(uhci)->rh_timer, jiffies);
@@ -42,12 +38,6 @@ static inline void uhci_clear_next_interrupt(struct uhci_hcd *uhci)
42 uhci->term_td->status &= ~cpu_to_le32(TD_CTRL_IOC); 38 uhci->term_td->status &= ~cpu_to_le32(TD_CTRL_IOC);
43} 39}
44 40
45static inline void uhci_moveto_complete(struct uhci_hcd *uhci,
46 struct urb_priv *urbp)
47{
48 list_move_tail(&urbp->urb_list, &uhci->complete_list);
49}
50
51static struct uhci_td *uhci_alloc_td(struct uhci_hcd *uhci) 41static struct uhci_td *uhci_alloc_td(struct uhci_hcd *uhci)
52{ 42{
53 dma_addr_t dma_handle; 43 dma_addr_t dma_handle;
@@ -71,6 +61,18 @@ static struct uhci_td *uhci_alloc_td(struct uhci_hcd *uhci)
71 return td; 61 return td;
72} 62}
73 63
64static void uhci_free_td(struct uhci_hcd *uhci, struct uhci_td *td)
65{
66 if (!list_empty(&td->list))
67 dev_warn(uhci_dev(uhci), "td %p still in list!\n", td);
68 if (!list_empty(&td->remove_list))
69 dev_warn(uhci_dev(uhci), "td %p still in remove_list!\n", td);
70 if (!list_empty(&td->fl_list))
71 dev_warn(uhci_dev(uhci), "td %p still in fl_list!\n", td);
72
73 dma_pool_free(uhci->td_pool, td, td->dma_handle);
74}
75
74static inline void uhci_fill_td(struct uhci_td *td, u32 status, 76static inline void uhci_fill_td(struct uhci_td *td, u32 status,
75 u32 token, u32 buffer) 77 u32 token, u32 buffer)
76{ 78{
@@ -82,7 +84,8 @@ static inline void uhci_fill_td(struct uhci_td *td, u32 status,
82/* 84/*
83 * We insert Isochronous URBs directly into the frame list at the beginning 85 * We insert Isochronous URBs directly into the frame list at the beginning
84 */ 86 */
85static void uhci_insert_td_frame_list(struct uhci_hcd *uhci, struct uhci_td *td, unsigned framenum) 87static inline void uhci_insert_td_in_frame_list(struct uhci_hcd *uhci,
88 struct uhci_td *td, unsigned framenum)
86{ 89{
87 framenum &= (UHCI_NUMFRAMES - 1); 90 framenum &= (UHCI_NUMFRAMES - 1);
88 91
@@ -108,7 +111,7 @@ static void uhci_insert_td_frame_list(struct uhci_hcd *uhci, struct uhci_td *td,
108 } 111 }
109} 112}
110 113
111static inline void uhci_remove_td_frame_list(struct uhci_hcd *uhci, 114static inline void uhci_remove_td_from_frame_list(struct uhci_hcd *uhci,
112 struct uhci_td *td) 115 struct uhci_td *td)
113{ 116{
114 /* If it's not inserted, don't remove it */ 117 /* If it's not inserted, don't remove it */
@@ -139,48 +142,68 @@ static inline void uhci_remove_td_frame_list(struct uhci_hcd *uhci,
139 td->frame = -1; 142 td->frame = -1;
140} 143}
141 144
142static void unlink_isochronous_tds(struct uhci_hcd *uhci, struct urb *urb) 145/*
146 * Remove all the TDs for an Isochronous URB from the frame list
147 */
148static void uhci_unlink_isochronous_tds(struct uhci_hcd *uhci, struct urb *urb)
143{ 149{
144 struct urb_priv *urbp = (struct urb_priv *) urb->hcpriv; 150 struct urb_priv *urbp = (struct urb_priv *) urb->hcpriv;
145 struct uhci_td *td; 151 struct uhci_td *td;
146 152
147 list_for_each_entry(td, &urbp->td_list, list) 153 list_for_each_entry(td, &urbp->td_list, list)
148 uhci_remove_td_frame_list(uhci, td); 154 uhci_remove_td_from_frame_list(uhci, td);
149 wmb(); 155 wmb();
150} 156}
151 157
152/* 158/*
153 * Inserts a td list into qh. 159 * Remove an URB's TDs from the hardware schedule
154 */ 160 */
155static void uhci_insert_tds_in_qh(struct uhci_qh *qh, struct urb *urb, __le32 breadth) 161static void uhci_remove_tds_from_schedule(struct uhci_hcd *uhci,
162 struct urb *urb, int status)
156{ 163{
157 struct urb_priv *urbp = (struct urb_priv *)urb->hcpriv; 164 struct urb_priv *urbp = (struct urb_priv *) urb->hcpriv;
158 struct uhci_td *td;
159 __le32 *plink;
160 165
161 /* Ordering isn't important here yet since the QH hasn't been */ 166 /* Isochronous TDs get unlinked directly from the frame list */
162 /* inserted into the schedule yet */ 167 if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS) {
163 plink = &qh->element; 168 uhci_unlink_isochronous_tds(uhci, urb);
164 list_for_each_entry(td, &urbp->td_list, list) { 169 return;
165 *plink = cpu_to_le32(td->dma_handle) | breadth;
166 plink = &td->link;
167 } 170 }
168 *plink = UHCI_PTR_TERM;
169}
170 171
171static void uhci_free_td(struct uhci_hcd *uhci, struct uhci_td *td) 172 /* If the URB isn't first on its queue, adjust the link pointer
172{ 173 * of the last TD in the previous URB. */
173 if (!list_empty(&td->list)) 174 if (urbp->node.prev != &urbp->qh->queue) {
174 dev_warn(uhci_dev(uhci), "td %p still in list!\n", td); 175 struct urb_priv *purbp;
175 if (!list_empty(&td->remove_list)) 176 struct uhci_td *ptd, *ltd;
176 dev_warn(uhci_dev(uhci), "td %p still in remove_list!\n", td); 177
177 if (!list_empty(&td->fl_list)) 178 if (status == -EINPROGRESS)
178 dev_warn(uhci_dev(uhci), "td %p still in fl_list!\n", td); 179 status = 0;
180 purbp = list_entry(urbp->node.prev, struct urb_priv, node);
181 ptd = list_entry(purbp->td_list.prev, struct uhci_td,
182 list);
183 ltd = list_entry(urbp->td_list.prev, struct uhci_td,
184 list);
185 ptd->link = ltd->link;
186 }
179 187
180 dma_pool_free(uhci->td_pool, td, td->dma_handle); 188 /* If the URB completed with an error, then the QH element certainly
189 * points to one of the URB's TDs. If it completed normally then
190 * the QH element has certainly moved on to the next URB. And if
191 * the URB is still in progress then it must have been dequeued.
192 * The QH element either hasn't reached it yet or is somewhere in
193 * the middle. If the URB wasn't first we can assume that it
194 * hasn't started yet (see above): Otherwise all the preceding URBs
195 * would have completed and been removed from the queue, so this one
196 * _would_ be first.
197 *
198 * If the QH element is inside this URB, clear it. It will be
199 * set properly when the QH is activated.
200 */
201 if (status < 0)
202 urbp->qh->element = UHCI_PTR_TERM;
181} 203}
182 204
183static struct uhci_qh *uhci_alloc_qh(struct uhci_hcd *uhci) 205static struct uhci_qh *uhci_alloc_qh(struct uhci_hcd *uhci,
206 struct usb_device *udev, struct usb_host_endpoint *hep)
184{ 207{
185 dma_addr_t dma_handle; 208 dma_addr_t dma_handle;
186 struct uhci_qh *qh; 209 struct uhci_qh *qh;
@@ -194,256 +217,120 @@ static struct uhci_qh *uhci_alloc_qh(struct uhci_hcd *uhci)
194 qh->element = UHCI_PTR_TERM; 217 qh->element = UHCI_PTR_TERM;
195 qh->link = UHCI_PTR_TERM; 218 qh->link = UHCI_PTR_TERM;
196 219
197 qh->urbp = NULL; 220 INIT_LIST_HEAD(&qh->queue);
221 INIT_LIST_HEAD(&qh->node);
198 222
199 INIT_LIST_HEAD(&qh->list); 223 if (udev) { /* Normal QH */
200 INIT_LIST_HEAD(&qh->remove_list); 224 qh->state = QH_STATE_IDLE;
225 qh->hep = hep;
226 qh->udev = udev;
227 hep->hcpriv = qh;
228 usb_get_dev(udev);
201 229
230 } else { /* Skeleton QH */
231 qh->state = QH_STATE_ACTIVE;
232 qh->udev = NULL;
233 }
202 return qh; 234 return qh;
203} 235}
204 236
205static void uhci_free_qh(struct uhci_hcd *uhci, struct uhci_qh *qh) 237static void uhci_free_qh(struct uhci_hcd *uhci, struct uhci_qh *qh)
206{ 238{
207 if (!list_empty(&qh->list)) 239 WARN_ON(qh->state != QH_STATE_IDLE && qh->udev);
240 if (!list_empty(&qh->queue))
208 dev_warn(uhci_dev(uhci), "qh %p list not empty!\n", qh); 241 dev_warn(uhci_dev(uhci), "qh %p list not empty!\n", qh);
209 if (!list_empty(&qh->remove_list))
210 dev_warn(uhci_dev(uhci), "qh %p still in remove_list!\n", qh);
211 242
243 list_del(&qh->node);
244 if (qh->udev) {
245 qh->hep->hcpriv = NULL;
246 usb_put_dev(qh->udev);
247 }
212 dma_pool_free(uhci->qh_pool, qh, qh->dma_handle); 248 dma_pool_free(uhci->qh_pool, qh, qh->dma_handle);
213} 249}
214 250
215/* 251/*
216 * Append this urb's qh after the last qh in skelqh->list 252 * Put a QH on the schedule in both hardware and software
217 *
218 * Note that urb_priv.queue_list doesn't have a separate queue head;
219 * it's a ring with every element "live".
220 */ 253 */
221static void uhci_insert_qh(struct uhci_hcd *uhci, struct uhci_qh *skelqh, struct urb *urb) 254static void uhci_activate_qh(struct uhci_hcd *uhci, struct uhci_qh *qh)
222{ 255{
223 struct urb_priv *urbp = (struct urb_priv *)urb->hcpriv; 256 struct uhci_qh *pqh;
224 struct urb_priv *turbp;
225 struct uhci_qh *lqh;
226 257
227 /* Grab the last QH */ 258 WARN_ON(list_empty(&qh->queue));
228 lqh = list_entry(skelqh->list.prev, struct uhci_qh, list);
229 259
230 /* Point to the next skelqh */ 260 /* Set the element pointer if it isn't set already.
231 urbp->qh->link = lqh->link; 261 * This isn't needed for Isochronous queues, but it doesn't hurt. */
232 wmb(); /* Ordering is important */ 262 if (qh_element(qh) == UHCI_PTR_TERM) {
263 struct urb_priv *urbp = list_entry(qh->queue.next,
264 struct urb_priv, node);
265 struct uhci_td *td = list_entry(urbp->td_list.next,
266 struct uhci_td, list);
233 267
234 /* 268 qh->element = cpu_to_le32(td->dma_handle);
235 * Patch QHs for previous endpoint's queued URBs? HC goes
236 * here next, not to the next skelqh it now points to.
237 *
238 * lqh --> td ... --> qh ... --> td --> qh ... --> td
239 * | | |
240 * v v v
241 * +<----------------+-----------------+
242 * v
243 * newqh --> td ... --> td
244 * |
245 * v
246 * ...
247 *
248 * The HC could see (and use!) any of these as we write them.
249 */
250 lqh->link = cpu_to_le32(urbp->qh->dma_handle) | UHCI_PTR_QH;
251 if (lqh->urbp) {
252 list_for_each_entry(turbp, &lqh->urbp->queue_list, queue_list)
253 turbp->qh->link = lqh->link;
254 } 269 }
255 270
256 list_add_tail(&urbp->qh->list, &skelqh->list); 271 if (qh->state == QH_STATE_ACTIVE)
272 return;
273 qh->state = QH_STATE_ACTIVE;
274
275 /* Move the QH from its old list to the end of the appropriate
276 * skeleton's list */
277 list_move_tail(&qh->node, &qh->skel->node);
278
279 /* Link it into the schedule */
280 pqh = list_entry(qh->node.prev, struct uhci_qh, node);
281 qh->link = pqh->link;
282 wmb();
283 pqh->link = UHCI_PTR_QH | cpu_to_le32(qh->dma_handle);
257} 284}
258 285
259/* 286/*
260 * Start removal of QH from schedule; it finishes next frame. 287 * Take a QH off the hardware schedule
261 * TDs should be unlinked before this is called.
262 */ 288 */
263static void uhci_remove_qh(struct uhci_hcd *uhci, struct uhci_qh *qh) 289static void uhci_unlink_qh(struct uhci_hcd *uhci, struct uhci_qh *qh)
264{ 290{
265 struct uhci_qh *pqh; 291 struct uhci_qh *pqh;
266 __le32 newlink;
267 292
268 if (!qh) 293 if (qh->state == QH_STATE_UNLINKING)
269 return; 294 return;
295 WARN_ON(qh->state != QH_STATE_ACTIVE || !qh->udev);
296 qh->state = QH_STATE_UNLINKING;
270 297
271 /* 298 /* Unlink the QH from the schedule and record when we did it */
272 * Only go through the hoops if it's actually linked in 299 pqh = list_entry(qh->node.prev, struct uhci_qh, node);
273 */ 300 pqh->link = qh->link;
274 if (!list_empty(&qh->list)) { 301 mb();
275
276 /* If our queue is nonempty, make the next URB the head */
277 if (!list_empty(&qh->urbp->queue_list)) {
278 struct urb_priv *nurbp;
279
280 nurbp = list_entry(qh->urbp->queue_list.next,
281 struct urb_priv, queue_list);
282 nurbp->queued = 0;
283 list_add(&nurbp->qh->list, &qh->list);
284 newlink = cpu_to_le32(nurbp->qh->dma_handle) | UHCI_PTR_QH;
285 } else
286 newlink = qh->link;
287
288 /* Fix up the previous QH's queue to link to either
289 * the new head of this queue or the start of the
290 * next endpoint's queue. */
291 pqh = list_entry(qh->list.prev, struct uhci_qh, list);
292 pqh->link = newlink;
293 if (pqh->urbp) {
294 struct urb_priv *turbp;
295
296 list_for_each_entry(turbp, &pqh->urbp->queue_list,
297 queue_list)
298 turbp->qh->link = newlink;
299 }
300 wmb();
301
302 /* Leave qh->link in case the HC is on the QH now, it will */
303 /* continue the rest of the schedule */
304 qh->element = UHCI_PTR_TERM;
305
306 list_del_init(&qh->list);
307 }
308
309 list_del_init(&qh->urbp->queue_list);
310 qh->urbp = NULL;
311 302
312 uhci_get_current_frame_number(uhci); 303 uhci_get_current_frame_number(uhci);
313 if (uhci->frame_number + uhci->is_stopped != uhci->qh_remove_age) { 304 qh->unlink_frame = uhci->frame_number;
314 uhci_free_pending_qhs(uhci);
315 uhci->qh_remove_age = uhci->frame_number;
316 }
317 305
318 /* Check to see if the remove list is empty. Set the IOC bit */ 306 /* Force an interrupt so we know when the QH is fully unlinked */
319 /* to force an interrupt so we can remove the QH */ 307 if (list_empty(&uhci->skel_unlink_qh->node))
320 if (list_empty(&uhci->qh_remove_list))
321 uhci_set_next_interrupt(uhci); 308 uhci_set_next_interrupt(uhci);
322 309
323 list_add(&qh->remove_list, &uhci->qh_remove_list); 310 /* Move the QH from its old list to the end of the unlinking list */
311 list_move_tail(&qh->node, &uhci->skel_unlink_qh->node);
324} 312}
325 313
326static int uhci_fixup_toggle(struct urb *urb, unsigned int toggle) 314/*
327{ 315 * When we and the controller are through with a QH, it becomes IDLE.
328 struct urb_priv *urbp = (struct urb_priv *)urb->hcpriv; 316 * This happens when a QH has been off the schedule (on the unlinking
329 struct uhci_td *td; 317 * list) for more than one frame, or when an error occurs while adding
330 318 * the first URB onto a new QH.
331 list_for_each_entry(td, &urbp->td_list, list) { 319 */
332 if (toggle) 320static void uhci_make_qh_idle(struct uhci_hcd *uhci, struct uhci_qh *qh)
333 td->token |= cpu_to_le32(TD_TOKEN_TOGGLE);
334 else
335 td->token &= ~cpu_to_le32(TD_TOKEN_TOGGLE);
336
337 toggle ^= 1;
338 }
339
340 return toggle;
341}
342
343/* This function will append one URB's QH to another URB's QH. This is for */
344/* queuing interrupt, control or bulk transfers */
345static void uhci_append_queued_urb(struct uhci_hcd *uhci, struct urb *eurb, struct urb *urb)
346{
347 struct urb_priv *eurbp, *urbp, *furbp, *lurbp;
348 struct uhci_td *lltd;
349
350 eurbp = eurb->hcpriv;
351 urbp = urb->hcpriv;
352
353 /* Find the first URB in the queue */
354 furbp = eurbp;
355 if (eurbp->queued) {
356 list_for_each_entry(furbp, &eurbp->queue_list, queue_list)
357 if (!furbp->queued)
358 break;
359 }
360
361 lurbp = list_entry(furbp->queue_list.prev, struct urb_priv, queue_list);
362
363 lltd = list_entry(lurbp->td_list.prev, struct uhci_td, list);
364
365 /* Control transfers always start with toggle 0 */
366 if (!usb_pipecontrol(urb->pipe))
367 usb_settoggle(urb->dev, usb_pipeendpoint(urb->pipe),
368 usb_pipeout(urb->pipe),
369 uhci_fixup_toggle(urb,
370 uhci_toggle(td_token(lltd)) ^ 1));
371
372 /* All qhs in the queue need to link to the next queue */
373 urbp->qh->link = eurbp->qh->link;
374
375 wmb(); /* Make sure we flush everything */
376
377 lltd->link = cpu_to_le32(urbp->qh->dma_handle) | UHCI_PTR_QH;
378
379 list_add_tail(&urbp->queue_list, &furbp->queue_list);
380
381 urbp->queued = 1;
382}
383
384static void uhci_delete_queued_urb(struct uhci_hcd *uhci, struct urb *urb)
385{ 321{
386 struct urb_priv *urbp, *nurbp, *purbp, *turbp; 322 WARN_ON(qh->state == QH_STATE_ACTIVE);
387 struct uhci_td *pltd;
388 unsigned int toggle;
389
390 urbp = urb->hcpriv;
391
392 if (list_empty(&urbp->queue_list))
393 return;
394
395 nurbp = list_entry(urbp->queue_list.next, struct urb_priv, queue_list);
396
397 /*
398 * Fix up the toggle for the following URBs in the queue.
399 * Only needed for bulk and interrupt: control and isochronous
400 * endpoints don't propagate toggles between messages.
401 */
402 if (usb_pipebulk(urb->pipe) || usb_pipeint(urb->pipe)) {
403 if (!urbp->queued)
404 /* We just set the toggle in uhci_unlink_generic */
405 toggle = usb_gettoggle(urb->dev,
406 usb_pipeendpoint(urb->pipe),
407 usb_pipeout(urb->pipe));
408 else {
409 /* If we're in the middle of the queue, grab the */
410 /* toggle from the TD previous to us */
411 purbp = list_entry(urbp->queue_list.prev,
412 struct urb_priv, queue_list);
413 pltd = list_entry(purbp->td_list.prev,
414 struct uhci_td, list);
415 toggle = uhci_toggle(td_token(pltd)) ^ 1;
416 }
417
418 list_for_each_entry(turbp, &urbp->queue_list, queue_list) {
419 if (!turbp->queued)
420 break;
421 toggle = uhci_fixup_toggle(turbp->urb, toggle);
422 }
423 323
424 usb_settoggle(urb->dev, usb_pipeendpoint(urb->pipe), 324 list_move(&qh->node, &uhci->idle_qh_list);
425 usb_pipeout(urb->pipe), toggle); 325 qh->state = QH_STATE_IDLE;
426 }
427
428 if (urbp->queued) {
429 /* We're somewhere in the middle (or end). The case where
430 * we're at the head is handled in uhci_remove_qh(). */
431 purbp = list_entry(urbp->queue_list.prev, struct urb_priv,
432 queue_list);
433
434 pltd = list_entry(purbp->td_list.prev, struct uhci_td, list);
435 if (nurbp->queued)
436 pltd->link = cpu_to_le32(nurbp->qh->dma_handle) | UHCI_PTR_QH;
437 else
438 /* The next URB happens to be the beginning, so */
439 /* we're the last, end the chain */
440 pltd->link = UHCI_PTR_TERM;
441 }
442 326
443 /* urbp->queue_list is handled in uhci_remove_qh() */ 327 /* If anyone is waiting for a QH to become idle, wake them up */
328 if (uhci->num_waiting)
329 wake_up_all(&uhci->waitqh);
444} 330}
445 331
446static struct urb_priv *uhci_alloc_urb_priv(struct uhci_hcd *uhci, struct urb *urb) 332static inline struct urb_priv *uhci_alloc_urb_priv(struct uhci_hcd *uhci,
333 struct urb *urb)
447{ 334{
448 struct urb_priv *urbp; 335 struct urb_priv *urbp;
449 336
@@ -453,17 +340,14 @@ static struct urb_priv *uhci_alloc_urb_priv(struct uhci_hcd *uhci, struct urb *u
453 340
454 memset((void *)urbp, 0, sizeof(*urbp)); 341 memset((void *)urbp, 0, sizeof(*urbp));
455 342
456 urbp->fsbrtime = jiffies;
457 urbp->urb = urb; 343 urbp->urb = urb;
344 urb->hcpriv = urbp;
345 urbp->fsbrtime = jiffies;
458 346
347 INIT_LIST_HEAD(&urbp->node);
459 INIT_LIST_HEAD(&urbp->td_list); 348 INIT_LIST_HEAD(&urbp->td_list);
460 INIT_LIST_HEAD(&urbp->queue_list);
461 INIT_LIST_HEAD(&urbp->urb_list); 349 INIT_LIST_HEAD(&urbp->urb_list);
462 350
463 list_add_tail(&urbp->urb_list, &uhci->urb_list);
464
465 urb->hcpriv = urbp;
466
467 return urbp; 351 return urbp;
468} 352}
469 353
@@ -482,18 +366,17 @@ static void uhci_remove_td_from_urb(struct uhci_td *td)
482 list_del_init(&td->list); 366 list_del_init(&td->list);
483} 367}
484 368
485static void uhci_destroy_urb_priv(struct uhci_hcd *uhci, struct urb *urb) 369static void uhci_free_urb_priv(struct uhci_hcd *uhci,
370 struct urb_priv *urbp)
486{ 371{
487 struct uhci_td *td, *tmp; 372 struct uhci_td *td, *tmp;
488 struct urb_priv *urbp;
489
490 urbp = (struct urb_priv *)urb->hcpriv;
491 if (!urbp)
492 return;
493 373
494 if (!list_empty(&urbp->urb_list)) 374 if (!list_empty(&urbp->urb_list))
495 dev_warn(uhci_dev(uhci), "urb %p still on uhci->urb_list " 375 dev_warn(uhci_dev(uhci), "urb %p still on uhci->urb_list!\n",
496 "or uhci->remove_list!\n", urb); 376 urbp->urb);
377 if (!list_empty(&urbp->node))
378 dev_warn(uhci_dev(uhci), "urb %p still on QH's list!\n",
379 urbp->urb);
497 380
498 uhci_get_current_frame_number(uhci); 381 uhci_get_current_frame_number(uhci);
499 if (uhci->frame_number + uhci->is_stopped != uhci->td_remove_age) { 382 if (uhci->frame_number + uhci->is_stopped != uhci->td_remove_age) {
@@ -502,7 +385,7 @@ static void uhci_destroy_urb_priv(struct uhci_hcd *uhci, struct urb *urb)
502 } 385 }
503 386
504 /* Check to see if the remove list is empty. Set the IOC bit */ 387 /* Check to see if the remove list is empty. Set the IOC bit */
505 /* to force an interrupt so we can remove the TDs*/ 388 /* to force an interrupt so we can remove the TDs. */
506 if (list_empty(&uhci->td_remove_list)) 389 if (list_empty(&uhci->td_remove_list))
507 uhci_set_next_interrupt(uhci); 390 uhci_set_next_interrupt(uhci);
508 391
@@ -511,7 +394,7 @@ static void uhci_destroy_urb_priv(struct uhci_hcd *uhci, struct urb *urb)
511 list_add(&td->remove_list, &uhci->td_remove_list); 394 list_add(&td->remove_list, &uhci->td_remove_list);
512 } 395 }
513 396
514 urb->hcpriv = NULL; 397 urbp->urb->hcpriv = NULL;
515 kmem_cache_free(uhci_up_cachep, urbp); 398 kmem_cache_free(uhci_up_cachep, urbp);
516} 399}
517 400
@@ -568,17 +451,82 @@ static int uhci_map_status(int status, int dir_out)
568} 451}
569 452
570/* 453/*
454 * Fix up the data toggles for URBs in a queue, when one of them
455 * terminates early (short transfer, error, or dequeued).
456 */
457static void uhci_fixup_toggles(struct urb *urb)
458{
459 struct list_head *head;
460 struct uhci_td *td;
461 struct urb_priv *urbp = (struct urb_priv *) urb->hcpriv;
462 int prevactive = 0;
463 unsigned int toggle = 0;
464 struct urb_priv *turbp, *list_end;
465
466 /*
467 * We need to find out what the last successful toggle was so
468 * we can update the data toggles for the following transfers.
469 *
470 * There are 2 ways the last successful completed TD is found:
471 *
472 * 1) The TD is NOT active and the actual length < expected length
473 * 2) The TD is NOT active and it's the last TD in the chain
474 *
475 * and a third way the first uncompleted TD is found:
476 *
477 * 3) The TD is active and the previous TD is NOT active
478 */
479 head = &urbp->td_list;
480 list_for_each_entry(td, head, list) {
481 unsigned int ctrlstat = td_status(td);
482
483 if (!(ctrlstat & TD_CTRL_ACTIVE) &&
484 (uhci_actual_length(ctrlstat) <
485 uhci_expected_length(td_token(td)) ||
486 td->list.next == head))
487 toggle = uhci_toggle(td_token(td)) ^ 1;
488 else if ((ctrlstat & TD_CTRL_ACTIVE) && !prevactive)
489 toggle = uhci_toggle(td_token(td));
490
491 prevactive = ctrlstat & TD_CTRL_ACTIVE;
492 }
493
494 /*
495 * Fix up the toggle for the following URBs in the queue.
496 *
497 * We can stop as soon as we find an URB with toggles set correctly,
498 * because then all the following URBs will be correct also.
499 */
500 list_end = list_entry(&urbp->qh->queue, struct urb_priv, node);
501 turbp = urbp;
502 while ((turbp = list_entry(turbp->node.next, struct urb_priv, node))
503 != list_end) {
504 td = list_entry(turbp->td_list.next, struct uhci_td, list);
505 if (uhci_toggle(td_token(td)) == toggle)
506 return;
507
508 list_for_each_entry(td, &turbp->td_list, list) {
509 td->token ^= __constant_cpu_to_le32(TD_TOKEN_TOGGLE);
510 toggle ^= 1;
511 }
512 }
513
514 usb_settoggle(urb->dev, usb_pipeendpoint(urb->pipe),
515 usb_pipeout(urb->pipe), toggle);
516}
517
518/*
571 * Control transfers 519 * Control transfers
572 */ 520 */
573static int uhci_submit_control(struct uhci_hcd *uhci, struct urb *urb, struct urb *eurb) 521static int uhci_submit_control(struct uhci_hcd *uhci, struct urb *urb,
522 struct uhci_qh *qh)
574{ 523{
575 struct urb_priv *urbp = (struct urb_priv *)urb->hcpriv;
576 struct uhci_td *td; 524 struct uhci_td *td;
577 struct uhci_qh *qh, *skelqh;
578 unsigned long destination, status; 525 unsigned long destination, status;
579 int maxsze = usb_maxpacket(urb->dev, urb->pipe, usb_pipeout(urb->pipe)); 526 int maxsze = le16_to_cpu(qh->hep->desc.wMaxPacketSize);
580 int len = urb->transfer_buffer_length; 527 int len = urb->transfer_buffer_length;
581 dma_addr_t data = urb->transfer_dma; 528 dma_addr_t data = urb->transfer_dma;
529 __le32 *plink;
582 530
583 /* The "pipe" thing contains the destination in bits 8--18 */ 531 /* The "pipe" thing contains the destination in bits 8--18 */
584 destination = (urb->pipe & PIPE_DEVEP_MASK) | USB_PID_SETUP; 532 destination = (urb->pipe & PIPE_DEVEP_MASK) | USB_PID_SETUP;
@@ -597,7 +545,8 @@ static int uhci_submit_control(struct uhci_hcd *uhci, struct urb *urb, struct ur
597 545
598 uhci_add_td_to_urb(urb, td); 546 uhci_add_td_to_urb(urb, td);
599 uhci_fill_td(td, status, destination | uhci_explen(8), 547 uhci_fill_td(td, status, destination | uhci_explen(8),
600 urb->setup_dma); 548 urb->setup_dma);
549 plink = &td->link;
601 550
602 /* 551 /*
603 * If direction is "send", change the packet ID from SETUP (0x2D) 552 * If direction is "send", change the packet ID from SETUP (0x2D)
@@ -615,21 +564,20 @@ static int uhci_submit_control(struct uhci_hcd *uhci, struct urb *urb, struct ur
615 * Build the DATA TDs 564 * Build the DATA TDs
616 */ 565 */
617 while (len > 0) { 566 while (len > 0) {
618 int pktsze = len; 567 int pktsze = min(len, maxsze);
619
620 if (pktsze > maxsze)
621 pktsze = maxsze;
622 568
623 td = uhci_alloc_td(uhci); 569 td = uhci_alloc_td(uhci);
624 if (!td) 570 if (!td)
625 return -ENOMEM; 571 return -ENOMEM;
572 *plink = cpu_to_le32(td->dma_handle);
626 573
627 /* Alternate Data0/1 (start with Data1) */ 574 /* Alternate Data0/1 (start with Data1) */
628 destination ^= TD_TOKEN_TOGGLE; 575 destination ^= TD_TOKEN_TOGGLE;
629 576
630 uhci_add_td_to_urb(urb, td); 577 uhci_add_td_to_urb(urb, td);
631 uhci_fill_td(td, status, destination | uhci_explen(pktsze), 578 uhci_fill_td(td, status, destination | uhci_explen(pktsze),
632 data); 579 data);
580 plink = &td->link;
633 581
634 data += pktsze; 582 data += pktsze;
635 len -= pktsze; 583 len -= pktsze;
@@ -641,6 +589,7 @@ static int uhci_submit_control(struct uhci_hcd *uhci, struct urb *urb, struct ur
641 td = uhci_alloc_td(uhci); 589 td = uhci_alloc_td(uhci);
642 if (!td) 590 if (!td)
643 return -ENOMEM; 591 return -ENOMEM;
592 *plink = cpu_to_le32(td->dma_handle);
644 593
645 /* 594 /*
646 * It's IN if the pipe is an output pipe or we're not expecting 595 * It's IN if the pipe is an output pipe or we're not expecting
@@ -658,16 +607,7 @@ static int uhci_submit_control(struct uhci_hcd *uhci, struct urb *urb, struct ur
658 607
659 uhci_add_td_to_urb(urb, td); 608 uhci_add_td_to_urb(urb, td);
660 uhci_fill_td(td, status | TD_CTRL_IOC, 609 uhci_fill_td(td, status | TD_CTRL_IOC,
661 destination | uhci_explen(0), 0); 610 destination | uhci_explen(0), 0);
662
663 qh = uhci_alloc_qh(uhci);
664 if (!qh)
665 return -ENOMEM;
666
667 urbp->qh = qh;
668 qh->urbp = urbp;
669
670 uhci_insert_tds_in_qh(qh, urb, UHCI_PTR_BREADTH);
671 611
672 /* Low-speed transfers get a different queue, and won't hog the bus. 612 /* Low-speed transfers get a different queue, and won't hog the bus.
673 * Also, some devices enumerate better without FSBR; the easiest way 613 * Also, some devices enumerate better without FSBR; the easiest way
@@ -675,18 +615,13 @@ static int uhci_submit_control(struct uhci_hcd *uhci, struct urb *urb, struct ur
675 * isn't in the CONFIGURED state. */ 615 * isn't in the CONFIGURED state. */
676 if (urb->dev->speed == USB_SPEED_LOW || 616 if (urb->dev->speed == USB_SPEED_LOW ||
677 urb->dev->state != USB_STATE_CONFIGURED) 617 urb->dev->state != USB_STATE_CONFIGURED)
678 skelqh = uhci->skel_ls_control_qh; 618 qh->skel = uhci->skel_ls_control_qh;
679 else { 619 else {
680 skelqh = uhci->skel_fs_control_qh; 620 qh->skel = uhci->skel_fs_control_qh;
681 uhci_inc_fsbr(uhci, urb); 621 uhci_inc_fsbr(uhci, urb);
682 } 622 }
683 623
684 if (eurb) 624 return 0;
685 uhci_append_queued_urb(uhci, eurb, urb);
686 else
687 uhci_insert_qh(uhci, skelqh, urb);
688
689 return -EINPROGRESS;
690} 625}
691 626
692/* 627/*
@@ -703,7 +638,7 @@ static int usb_control_retrigger_status(struct uhci_hcd *uhci, struct urb *urb)
703 struct urb_priv *urbp = (struct urb_priv *)urb->hcpriv; 638 struct urb_priv *urbp = (struct urb_priv *)urb->hcpriv;
704 struct uhci_td *td; 639 struct uhci_td *td;
705 640
706 urbp->short_control_packet = 1; 641 urbp->short_transfer = 1;
707 642
708 td = list_entry(urbp->td_list.prev, struct uhci_td, list); 643 td = list_entry(urbp->td_list.prev, struct uhci_td, list);
709 urbp->qh->element = cpu_to_le32(td->dma_handle); 644 urbp->qh->element = cpu_to_le32(td->dma_handle);
@@ -720,16 +655,14 @@ static int uhci_result_control(struct uhci_hcd *uhci, struct urb *urb)
720 unsigned int status; 655 unsigned int status;
721 int ret = 0; 656 int ret = 0;
722 657
723 if (list_empty(&urbp->td_list))
724 return -EINVAL;
725
726 head = &urbp->td_list; 658 head = &urbp->td_list;
727 659 if (urbp->short_transfer) {
728 if (urbp->short_control_packet) {
729 tmp = head->prev; 660 tmp = head->prev;
730 goto status_stage; 661 goto status_stage;
731 } 662 }
732 663
664 urb->actual_length = 0;
665
733 tmp = head->next; 666 tmp = head->next;
734 td = list_entry(tmp, struct uhci_td, list); 667 td = list_entry(tmp, struct uhci_td, list);
735 668
@@ -742,8 +675,6 @@ static int uhci_result_control(struct uhci_hcd *uhci, struct urb *urb)
742 if (status) 675 if (status)
743 goto td_error; 676 goto td_error;
744 677
745 urb->actual_length = 0;
746
747 /* The rest of the TDs (but the last) are data */ 678 /* The rest of the TDs (but the last) are data */
748 tmp = tmp->next; 679 tmp = tmp->next;
749 while (tmp != head && tmp->next != head) { 680 while (tmp != head && tmp->next != head) {
@@ -770,10 +701,7 @@ static int uhci_result_control(struct uhci_hcd *uhci, struct urb *urb)
770 goto err; 701 goto err;
771 } 702 }
772 703
773 if (uhci_packetid(td_token(td)) == USB_PID_IN) 704 return usb_control_retrigger_status(uhci, urb);
774 return usb_control_retrigger_status(uhci, urb);
775 else
776 return 0;
777 } 705 }
778 } 706 }
779 707
@@ -825,15 +753,15 @@ err:
825/* 753/*
826 * Common submit for bulk and interrupt 754 * Common submit for bulk and interrupt
827 */ 755 */
828static int uhci_submit_common(struct uhci_hcd *uhci, struct urb *urb, struct urb *eurb, struct uhci_qh *skelqh) 756static int uhci_submit_common(struct uhci_hcd *uhci, struct urb *urb,
757 struct uhci_qh *qh)
829{ 758{
830 struct uhci_td *td; 759 struct uhci_td *td;
831 struct uhci_qh *qh;
832 unsigned long destination, status; 760 unsigned long destination, status;
833 int maxsze = usb_maxpacket(urb->dev, urb->pipe, usb_pipeout(urb->pipe)); 761 int maxsze = le16_to_cpu(qh->hep->desc.wMaxPacketSize);
834 int len = urb->transfer_buffer_length; 762 int len = urb->transfer_buffer_length;
835 struct urb_priv *urbp = (struct urb_priv *)urb->hcpriv;
836 dma_addr_t data = urb->transfer_dma; 763 dma_addr_t data = urb->transfer_dma;
764 __le32 *plink, fake_link;
837 765
838 if (len < 0) 766 if (len < 0)
839 return -EINVAL; 767 return -EINVAL;
@@ -841,7 +769,8 @@ static int uhci_submit_common(struct uhci_hcd *uhci, struct urb *urb, struct urb
841 /* The "pipe" thing contains the destination in bits 8--18 */ 769 /* The "pipe" thing contains the destination in bits 8--18 */
842 destination = (urb->pipe & PIPE_DEVEP_MASK) | usb_packetid(urb->pipe); 770 destination = (urb->pipe & PIPE_DEVEP_MASK) | usb_packetid(urb->pipe);
843 771
844 status = uhci_maxerr(3) | TD_CTRL_ACTIVE; 772 /* 3 errors */
773 status = TD_CTRL_ACTIVE | uhci_maxerr(3);
845 if (urb->dev->speed == USB_SPEED_LOW) 774 if (urb->dev->speed == USB_SPEED_LOW)
846 status |= TD_CTRL_LS; 775 status |= TD_CTRL_LS;
847 if (usb_pipein(urb->pipe)) 776 if (usb_pipein(urb->pipe))
@@ -850,10 +779,11 @@ static int uhci_submit_common(struct uhci_hcd *uhci, struct urb *urb, struct urb
850 /* 779 /*
851 * Build the DATA TDs 780 * Build the DATA TDs
852 */ 781 */
782 plink = &fake_link;
853 do { /* Allow zero length packets */ 783 do { /* Allow zero length packets */
854 int pktsze = maxsze; 784 int pktsze = maxsze;
855 785
856 if (pktsze >= len) { 786 if (len <= pktsze) { /* The last packet */
857 pktsze = len; 787 pktsze = len;
858 if (!(urb->transfer_flags & URB_SHORT_NOT_OK)) 788 if (!(urb->transfer_flags & URB_SHORT_NOT_OK))
859 status &= ~TD_CTRL_SPD; 789 status &= ~TD_CTRL_SPD;
@@ -862,12 +792,15 @@ static int uhci_submit_common(struct uhci_hcd *uhci, struct urb *urb, struct urb
862 td = uhci_alloc_td(uhci); 792 td = uhci_alloc_td(uhci);
863 if (!td) 793 if (!td)
864 return -ENOMEM; 794 return -ENOMEM;
795 *plink = cpu_to_le32(td->dma_handle);
865 796
866 uhci_add_td_to_urb(urb, td); 797 uhci_add_td_to_urb(urb, td);
867 uhci_fill_td(td, status, destination | uhci_explen(pktsze) | 798 uhci_fill_td(td, status,
799 destination | uhci_explen(pktsze) |
868 (usb_gettoggle(urb->dev, usb_pipeendpoint(urb->pipe), 800 (usb_gettoggle(urb->dev, usb_pipeendpoint(urb->pipe),
869 usb_pipeout(urb->pipe)) << TD_TOKEN_TOGGLE_SHIFT), 801 usb_pipeout(urb->pipe)) << TD_TOKEN_TOGGLE_SHIFT),
870 data); 802 data);
803 plink = &td->link;
871 804
872 data += pktsze; 805 data += pktsze;
873 len -= maxsze; 806 len -= maxsze;
@@ -883,11 +816,13 @@ static int uhci_submit_common(struct uhci_hcd *uhci, struct urb *urb, struct urb
883 * however, if transfer_length == 0, the zero packet was already 816 * however, if transfer_length == 0, the zero packet was already
884 * prepared above. 817 * prepared above.
885 */ 818 */
886 if (usb_pipeout(urb->pipe) && (urb->transfer_flags & URB_ZERO_PACKET) && 819 if ((urb->transfer_flags & URB_ZERO_PACKET) &&
887 !len && urb->transfer_buffer_length) { 820 usb_pipeout(urb->pipe) && len == 0 &&
821 urb->transfer_buffer_length > 0) {
888 td = uhci_alloc_td(uhci); 822 td = uhci_alloc_td(uhci);
889 if (!td) 823 if (!td)
890 return -ENOMEM; 824 return -ENOMEM;
825 *plink = cpu_to_le32(td->dma_handle);
891 826
892 uhci_add_td_to_urb(urb, td); 827 uhci_add_td_to_urb(urb, td);
893 uhci_fill_td(td, status, destination | uhci_explen(0) | 828 uhci_fill_td(td, status, destination | uhci_explen(0) |
@@ -905,24 +840,9 @@ static int uhci_submit_common(struct uhci_hcd *uhci, struct urb *urb, struct urb
905 * fast side but not enough to justify delaying an interrupt 840 * fast side but not enough to justify delaying an interrupt
906 * more than 2 or 3 URBs, so we will ignore the URB_NO_INTERRUPT 841 * more than 2 or 3 URBs, so we will ignore the URB_NO_INTERRUPT
907 * flag setting. */ 842 * flag setting. */
908 td->status |= cpu_to_le32(TD_CTRL_IOC); 843 td->status |= __constant_cpu_to_le32(TD_CTRL_IOC);
909
910 qh = uhci_alloc_qh(uhci);
911 if (!qh)
912 return -ENOMEM;
913
914 urbp->qh = qh;
915 qh->urbp = urbp;
916 844
917 /* Always breadth first */ 845 return 0;
918 uhci_insert_tds_in_qh(qh, urb, UHCI_PTR_BREADTH);
919
920 if (eurb)
921 uhci_append_queued_urb(uhci, eurb, urb);
922 else
923 uhci_insert_qh(uhci, skelqh, urb);
924
925 return -EINPROGRESS;
926} 846}
927 847
928/* 848/*
@@ -954,8 +874,24 @@ static int uhci_result_common(struct uhci_hcd *uhci, struct urb *urb)
954 if (urb->transfer_flags & URB_SHORT_NOT_OK) { 874 if (urb->transfer_flags & URB_SHORT_NOT_OK) {
955 ret = -EREMOTEIO; 875 ret = -EREMOTEIO;
956 goto err; 876 goto err;
957 } else 877 }
958 return 0; 878
879 /*
880 * This URB stopped short of its end. We have to
881 * fix up the toggles of the following URBs on the
882 * queue and restart the queue.
883 *
884 * Do this only the first time we encounter the
885 * short URB.
886 */
887 if (!urbp->short_transfer) {
888 urbp->short_transfer = 1;
889 uhci_fixup_toggles(urb);
890 td = list_entry(urbp->td_list.prev,
891 struct uhci_td, list);
892 urbp->qh->element = td->link;
893 }
894 break;
959 } 895 }
960 } 896 }
961 897
@@ -988,7 +924,8 @@ err:
988 return ret; 924 return ret;
989} 925}
990 926
991static inline int uhci_submit_bulk(struct uhci_hcd *uhci, struct urb *urb, struct urb *eurb) 927static inline int uhci_submit_bulk(struct uhci_hcd *uhci, struct urb *urb,
928 struct uhci_qh *qh)
992{ 929{
993 int ret; 930 int ret;
994 931
@@ -996,21 +933,22 @@ static inline int uhci_submit_bulk(struct uhci_hcd *uhci, struct urb *urb, struc
996 if (urb->dev->speed == USB_SPEED_LOW) 933 if (urb->dev->speed == USB_SPEED_LOW)
997 return -EINVAL; 934 return -EINVAL;
998 935
999 ret = uhci_submit_common(uhci, urb, eurb, uhci->skel_bulk_qh); 936 qh->skel = uhci->skel_bulk_qh;
1000 if (ret == -EINPROGRESS) 937 ret = uhci_submit_common(uhci, urb, qh);
938 if (ret == 0)
1001 uhci_inc_fsbr(uhci, urb); 939 uhci_inc_fsbr(uhci, urb);
1002
1003 return ret; 940 return ret;
1004} 941}
1005 942
1006static inline int uhci_submit_interrupt(struct uhci_hcd *uhci, struct urb *urb, struct urb *eurb) 943static inline int uhci_submit_interrupt(struct uhci_hcd *uhci, struct urb *urb,
944 struct uhci_qh *qh)
1007{ 945{
1008 /* USB 1.1 interrupt transfers only involve one packet per interval; 946 /* USB 1.1 interrupt transfers only involve one packet per interval.
1009 * that's the uhci_submit_common() "breadth first" policy. Drivers 947 * Drivers can submit URBs of any length, but longer ones will need
1010 * can submit urbs of any length, but longer ones might need many 948 * multiple intervals to complete.
1011 * intervals to complete.
1012 */ 949 */
1013 return uhci_submit_common(uhci, urb, eurb, uhci->skelqh[__interval_to_skel(urb->interval)]); 950 qh->skel = uhci->skelqh[__interval_to_skel(urb->interval)];
951 return uhci_submit_common(uhci, urb, qh);
1014} 952}
1015 953
1016/* 954/*
@@ -1072,11 +1010,12 @@ static int isochronous_find_start(struct uhci_hcd *uhci, struct urb *urb)
1072/* 1010/*
1073 * Isochronous transfers 1011 * Isochronous transfers
1074 */ 1012 */
1075static int uhci_submit_isochronous(struct uhci_hcd *uhci, struct urb *urb) 1013static int uhci_submit_isochronous(struct uhci_hcd *uhci, struct urb *urb,
1014 struct uhci_qh *qh)
1076{ 1015{
1077 struct uhci_td *td; 1016 struct uhci_td *td = NULL; /* Since urb->number_of_packets > 0 */
1078 int i, ret, frame; 1017 int i, ret, frame;
1079 int status, destination; 1018 unsigned long destination, status;
1080 struct urb_priv *urbp = (struct urb_priv *) urb->hcpriv; 1019 struct urb_priv *urbp = (struct urb_priv *) urb->hcpriv;
1081 1020
1082 status = TD_CTRL_ACTIVE | TD_CTRL_IOS; 1021 status = TD_CTRL_ACTIVE | TD_CTRL_IOS;
@@ -1092,20 +1031,25 @@ static int uhci_submit_isochronous(struct uhci_hcd *uhci, struct urb *urb)
1092 return -ENOMEM; 1031 return -ENOMEM;
1093 1032
1094 uhci_add_td_to_urb(urb, td); 1033 uhci_add_td_to_urb(urb, td);
1095 uhci_fill_td(td, status, destination | uhci_explen(urb->iso_frame_desc[i].length), 1034 uhci_fill_td(td, status, destination |
1096 urb->transfer_dma + urb->iso_frame_desc[i].offset); 1035 uhci_explen(urb->iso_frame_desc[i].length),
1097 1036 urb->transfer_dma +
1098 if (i + 1 >= urb->number_of_packets) 1037 urb->iso_frame_desc[i].offset);
1099 td->status |= cpu_to_le32(TD_CTRL_IOC);
1100 } 1038 }
1101 1039
1040 /* Set the interrupt-on-completion flag on the last packet. */
1041 td->status |= __constant_cpu_to_le32(TD_CTRL_IOC);
1042
1043 qh->skel = uhci->skel_iso_qh;
1044
1045 /* Add the TDs to the frame list */
1102 frame = urb->start_frame; 1046 frame = urb->start_frame;
1103 list_for_each_entry(td, &urbp->td_list, list) { 1047 list_for_each_entry(td, &urbp->td_list, list) {
1104 uhci_insert_td_frame_list(uhci, td, frame); 1048 uhci_insert_td_in_frame_list(uhci, td, frame);
1105 frame += urb->interval; 1049 frame += urb->interval;
1106 } 1050 }
1107 1051
1108 return -EINPROGRESS; 1052 return 0;
1109} 1053}
1110 1054
1111static int uhci_result_isochronous(struct uhci_hcd *uhci, struct urb *urb) 1055static int uhci_result_isochronous(struct uhci_hcd *uhci, struct urb *urb)
@@ -1139,80 +1083,67 @@ static int uhci_result_isochronous(struct uhci_hcd *uhci, struct urb *urb)
1139 1083
1140 i++; 1084 i++;
1141 } 1085 }
1142 unlink_isochronous_tds(uhci, urb);
1143 1086
1144 return ret; 1087 return ret;
1145} 1088}
1146 1089
1147static struct urb *uhci_find_urb_ep(struct uhci_hcd *uhci, struct urb *urb)
1148{
1149 struct urb_priv *up;
1150
1151 /* We don't match Isoc transfers since they are special */
1152 if (usb_pipeisoc(urb->pipe))
1153 return NULL;
1154
1155 list_for_each_entry(up, &uhci->urb_list, urb_list) {
1156 struct urb *u = up->urb;
1157
1158 if (u->dev == urb->dev && u->status == -EINPROGRESS) {
1159 /* For control, ignore the direction */
1160 if (usb_pipecontrol(urb->pipe) &&
1161 (u->pipe & ~USB_DIR_IN) == (urb->pipe & ~USB_DIR_IN))
1162 return u;
1163 else if (u->pipe == urb->pipe)
1164 return u;
1165 }
1166 }
1167
1168 return NULL;
1169}
1170
1171static int uhci_urb_enqueue(struct usb_hcd *hcd, 1090static int uhci_urb_enqueue(struct usb_hcd *hcd,
1172 struct usb_host_endpoint *ep, 1091 struct usb_host_endpoint *hep,
1173 struct urb *urb, gfp_t mem_flags) 1092 struct urb *urb, gfp_t mem_flags)
1174{ 1093{
1175 int ret; 1094 int ret;
1176 struct uhci_hcd *uhci = hcd_to_uhci(hcd); 1095 struct uhci_hcd *uhci = hcd_to_uhci(hcd);
1177 unsigned long flags; 1096 unsigned long flags;
1178 struct urb *eurb; 1097 struct urb_priv *urbp;
1098 struct uhci_qh *qh;
1179 int bustime; 1099 int bustime;
1180 1100
1181 spin_lock_irqsave(&uhci->lock, flags); 1101 spin_lock_irqsave(&uhci->lock, flags);
1182 1102
1183 ret = urb->status; 1103 ret = urb->status;
1184 if (ret != -EINPROGRESS) /* URB already unlinked! */ 1104 if (ret != -EINPROGRESS) /* URB already unlinked! */
1185 goto out; 1105 goto done;
1186 1106
1187 eurb = uhci_find_urb_ep(uhci, urb); 1107 ret = -ENOMEM;
1108 urbp = uhci_alloc_urb_priv(uhci, urb);
1109 if (!urbp)
1110 goto done;
1188 1111
1189 if (!uhci_alloc_urb_priv(uhci, urb)) { 1112 if (hep->hcpriv)
1190 ret = -ENOMEM; 1113 qh = (struct uhci_qh *) hep->hcpriv;
1191 goto out; 1114 else {
1115 qh = uhci_alloc_qh(uhci, urb->dev, hep);
1116 if (!qh)
1117 goto err_no_qh;
1192 } 1118 }
1119 urbp->qh = qh;
1193 1120
1194 switch (usb_pipetype(urb->pipe)) { 1121 switch (usb_pipetype(urb->pipe)) {
1195 case PIPE_CONTROL: 1122 case PIPE_CONTROL:
1196 ret = uhci_submit_control(uhci, urb, eurb); 1123 ret = uhci_submit_control(uhci, urb, qh);
1124 break;
1125 case PIPE_BULK:
1126 ret = uhci_submit_bulk(uhci, urb, qh);
1197 break; 1127 break;
1198 case PIPE_INTERRUPT: 1128 case PIPE_INTERRUPT:
1199 if (!eurb) { 1129 if (list_empty(&qh->queue)) {
1200 bustime = usb_check_bandwidth(urb->dev, urb); 1130 bustime = usb_check_bandwidth(urb->dev, urb);
1201 if (bustime < 0) 1131 if (bustime < 0)
1202 ret = bustime; 1132 ret = bustime;
1203 else { 1133 else {
1204 ret = uhci_submit_interrupt(uhci, urb, eurb); 1134 ret = uhci_submit_interrupt(uhci, urb, qh);
1205 if (ret == -EINPROGRESS) 1135 if (ret == 0)
1206 usb_claim_bandwidth(urb->dev, urb, bustime, 0); 1136 usb_claim_bandwidth(urb->dev, urb, bustime, 0);
1207 } 1137 }
1208 } else { /* inherit from parent */ 1138 } else { /* inherit from parent */
1209 urb->bandwidth = eurb->bandwidth; 1139 struct urb_priv *eurbp;
1210 ret = uhci_submit_interrupt(uhci, urb, eurb); 1140
1141 eurbp = list_entry(qh->queue.prev, struct urb_priv,
1142 node);
1143 urb->bandwidth = eurbp->urb->bandwidth;
1144 ret = uhci_submit_interrupt(uhci, urb, qh);
1211 } 1145 }
1212 break; 1146 break;
1213 case PIPE_BULK:
1214 ret = uhci_submit_bulk(uhci, urb, eurb);
1215 break;
1216 case PIPE_ISOCHRONOUS: 1147 case PIPE_ISOCHRONOUS:
1217 bustime = usb_check_bandwidth(urb->dev, urb); 1148 bustime = usb_check_bandwidth(urb->dev, urb);
1218 if (bustime < 0) { 1149 if (bustime < 0) {
@@ -1220,22 +1151,59 @@ static int uhci_urb_enqueue(struct usb_hcd *hcd,
1220 break; 1151 break;
1221 } 1152 }
1222 1153
1223 ret = uhci_submit_isochronous(uhci, urb); 1154 ret = uhci_submit_isochronous(uhci, urb, qh);
1224 if (ret == -EINPROGRESS) 1155 if (ret == 0)
1225 usb_claim_bandwidth(urb->dev, urb, bustime, 1); 1156 usb_claim_bandwidth(urb->dev, urb, bustime, 1);
1226 break; 1157 break;
1227 } 1158 }
1159 if (ret != 0)
1160 goto err_submit_failed;
1228 1161
1229 if (ret != -EINPROGRESS) { 1162 /* Add this URB to the QH */
1230 /* Submit failed, so delete it from the urb_list */ 1163 urbp->qh = qh;
1231 struct urb_priv *urbp = urb->hcpriv; 1164 list_add_tail(&urbp->node, &qh->queue);
1165 list_add_tail(&urbp->urb_list, &uhci->urb_list);
1232 1166
1233 list_del_init(&urbp->urb_list); 1167 /* If the new URB is the first and only one on this QH then either
1234 uhci_destroy_urb_priv(uhci, urb); 1168 * the QH is new and idle or else it's unlinked and waiting to
1235 } else 1169 * become idle, so we can activate it right away. */
1236 ret = 0; 1170 if (qh->queue.next == &urbp->node)
1171 uhci_activate_qh(uhci, qh);
1172
1173 /* If the QH is already active, we have a race with the hardware.
1174 * This won't get fixed until dummy TDs are added. */
1175 else if (qh->state == QH_STATE_ACTIVE) {
1176
1177 /* If the URB isn't first on its queue, adjust the link pointer
1178 * of the last TD in the previous URB. */
1179 if (urbp->node.prev != &urbp->qh->queue) {
1180 struct urb_priv *purbp = list_entry(urbp->node.prev,
1181 struct urb_priv, node);
1182 struct uhci_td *ptd = list_entry(purbp->td_list.prev,
1183 struct uhci_td, list);
1184 struct uhci_td *td = list_entry(urbp->td_list.next,
1185 struct uhci_td, list);
1186
1187 ptd->link = cpu_to_le32(td->dma_handle);
1188
1189 }
1190 if (qh_element(qh) == UHCI_PTR_TERM) {
1191 struct uhci_td *td = list_entry(urbp->td_list.next,
1192 struct uhci_td, list);
1193
1194 qh->element = cpu_to_le32(td->dma_handle);
1195 }
1196 }
1197 goto done;
1198
1199err_submit_failed:
1200 if (qh->state == QH_STATE_IDLE)
1201 uhci_make_qh_idle(uhci, qh); /* Reclaim unused QH */
1237 1202
1238out: 1203err_no_qh:
1204 uhci_free_urb_priv(uhci, urbp);
1205
1206done:
1239 spin_unlock_irqrestore(&uhci->lock, flags); 1207 spin_unlock_irqrestore(&uhci->lock, flags);
1240 return ret; 1208 return ret;
1241} 1209}
@@ -1245,119 +1213,115 @@ out:
1245 */ 1213 */
1246static void uhci_transfer_result(struct uhci_hcd *uhci, struct urb *urb) 1214static void uhci_transfer_result(struct uhci_hcd *uhci, struct urb *urb)
1247{ 1215{
1248 int ret = -EINPROGRESS; 1216 int status;
1249 struct urb_priv *urbp; 1217 int okay_to_giveback = 0;
1250 1218 struct urb_priv *urbp = (struct urb_priv *) urb->hcpriv;
1251 spin_lock(&urb->lock);
1252
1253 urbp = (struct urb_priv *)urb->hcpriv;
1254
1255 if (urb->status != -EINPROGRESS) /* URB already dequeued */
1256 goto out;
1257 1219
1258 switch (usb_pipetype(urb->pipe)) { 1220 switch (usb_pipetype(urb->pipe)) {
1259 case PIPE_CONTROL: 1221 case PIPE_CONTROL:
1260 ret = uhci_result_control(uhci, urb); 1222 status = uhci_result_control(uhci, urb);
1261 break;
1262 case PIPE_BULK:
1263 case PIPE_INTERRUPT:
1264 ret = uhci_result_common(uhci, urb);
1265 break; 1223 break;
1266 case PIPE_ISOCHRONOUS: 1224 case PIPE_ISOCHRONOUS:
1267 ret = uhci_result_isochronous(uhci, urb); 1225 status = uhci_result_isochronous(uhci, urb);
1226 break;
1227 default: /* PIPE_BULK or PIPE_INTERRUPT */
1228 status = uhci_result_common(uhci, urb);
1268 break; 1229 break;
1269 } 1230 }
1270 1231
1271 if (ret == -EINPROGRESS) 1232 spin_lock(&urb->lock);
1272 goto out; 1233 if (urb->status == -EINPROGRESS) { /* Not yet dequeued */
1273 urb->status = ret; 1234 if (status != -EINPROGRESS) { /* URB has completed */
1235 urb->status = status;
1236
1237 /* If the URB got a real error (as opposed to
1238 * simply being dequeued), we don't have to
1239 * unlink the QH. Fix this later... */
1240 if (status < 0)
1241 uhci_unlink_qh(uhci, urbp->qh);
1242 else
1243 okay_to_giveback = 1;
1244 }
1245 } else { /* Already dequeued */
1246 if (urbp->qh->state == QH_STATE_UNLINKING &&
1247 uhci->frame_number + uhci->is_stopped !=
1248 urbp->qh->unlink_frame)
1249 okay_to_giveback = 1;
1250 }
1251 spin_unlock(&urb->lock);
1252 if (!okay_to_giveback)
1253 return;
1274 1254
1275 switch (usb_pipetype(urb->pipe)) { 1255 switch (usb_pipetype(urb->pipe)) {
1276 case PIPE_CONTROL:
1277 case PIPE_BULK:
1278 case PIPE_ISOCHRONOUS: 1256 case PIPE_ISOCHRONOUS:
1279 /* Release bandwidth for Interrupt or Isoc. transfers */ 1257 /* Release bandwidth for Interrupt or Isoc. transfers */
1280 if (urb->bandwidth) 1258 if (urb->bandwidth)
1281 usb_release_bandwidth(urb->dev, urb, 1); 1259 usb_release_bandwidth(urb->dev, urb, 1);
1282 uhci_unlink_generic(uhci, urb);
1283 break; 1260 break;
1284 case PIPE_INTERRUPT: 1261 case PIPE_INTERRUPT:
1285 /* Release bandwidth for Interrupt or Isoc. transfers */ 1262 /* Release bandwidth for Interrupt or Isoc. transfers */
1286 /* Make sure we don't release if we have a queued URB */ 1263 /* Make sure we don't release if we have a queued URB */
1287 if (list_empty(&urbp->queue_list) && urb->bandwidth) 1264 if (list_empty(&urbp->qh->queue) && urb->bandwidth)
1288 usb_release_bandwidth(urb->dev, urb, 0); 1265 usb_release_bandwidth(urb->dev, urb, 0);
1289 else 1266 else
1290 /* bandwidth was passed on to queued URB, */ 1267 /* bandwidth was passed on to queued URB, */
1291 /* so don't let usb_unlink_urb() release it */ 1268 /* so don't let usb_unlink_urb() release it */
1292 urb->bandwidth = 0; 1269 urb->bandwidth = 0;
1293 uhci_unlink_generic(uhci, urb); 1270 /* Falls through */
1271 case PIPE_BULK:
1272 if (status < 0)
1273 uhci_fixup_toggles(urb);
1274 break;
1275 default: /* PIPE_CONTROL */
1294 break; 1276 break;
1295 default:
1296 dev_info(uhci_dev(uhci), "%s: unknown pipe type %d "
1297 "for urb %p\n",
1298 __FUNCTION__, usb_pipetype(urb->pipe), urb);
1299 } 1277 }
1300 1278
1301 /* Move it from uhci->urb_list to uhci->complete_list */ 1279 /* Take the URB's TDs off the hardware schedule */
1302 uhci_moveto_complete(uhci, urbp); 1280 uhci_remove_tds_from_schedule(uhci, urb, status);
1303 1281
1304out: 1282 /* Take the URB off the QH's queue and see if the QH is now unused */
1305 spin_unlock(&urb->lock); 1283 list_del_init(&urbp->node);
1306} 1284 if (list_empty(&urbp->qh->queue))
1307 1285 uhci_unlink_qh(uhci, urbp->qh);
1308static void uhci_unlink_generic(struct uhci_hcd *uhci, struct urb *urb)
1309{
1310 struct list_head *head;
1311 struct uhci_td *td;
1312 struct urb_priv *urbp = (struct urb_priv *)urb->hcpriv;
1313 int prevactive = 0;
1314 1286
1315 uhci_dec_fsbr(uhci, urb); /* Safe since it checks */ 1287 uhci_dec_fsbr(uhci, urb); /* Safe since it checks */
1316 1288
1317 /* 1289 /* Queue it for giving back */
1318 * Now we need to find out what the last successful toggle was 1290 list_move_tail(&urbp->urb_list, &uhci->complete_list);
1319 * so we can update the local data toggle for the next transfer 1291}
1320 *
1321 * There are 2 ways the last successful completed TD is found:
1322 *
1323 * 1) The TD is NOT active and the actual length < expected length
1324 * 2) The TD is NOT active and it's the last TD in the chain
1325 *
1326 * and a third way the first uncompleted TD is found:
1327 *
1328 * 3) The TD is active and the previous TD is NOT active
1329 *
1330 * Control and Isochronous ignore the toggle, so this is safe
1331 * for all types
1332 *
1333 * FIXME: The toggle fixups won't be 100% reliable until we
1334 * change over to using a single queue for each endpoint and
1335 * stop the queue before unlinking.
1336 */
1337 head = &urbp->td_list;
1338 list_for_each_entry(td, head, list) {
1339 unsigned int ctrlstat = td_status(td);
1340 1292
1341 if (!(ctrlstat & TD_CTRL_ACTIVE) && 1293/*
1342 (uhci_actual_length(ctrlstat) < 1294 * Check out the QHs waiting to be fully unlinked
1343 uhci_expected_length(td_token(td)) || 1295 */
1344 td->list.next == head)) 1296static void uhci_scan_unlinking_qhs(struct uhci_hcd *uhci)
1345 usb_settoggle(urb->dev, uhci_endpoint(td_token(td)), 1297{
1346 uhci_packetout(td_token(td)), 1298 struct uhci_qh *qh, *tmp;
1347 uhci_toggle(td_token(td)) ^ 1);
1348 else if ((ctrlstat & TD_CTRL_ACTIVE) && !prevactive)
1349 usb_settoggle(urb->dev, uhci_endpoint(td_token(td)),
1350 uhci_packetout(td_token(td)),
1351 uhci_toggle(td_token(td)));
1352 1299
1353 prevactive = ctrlstat & TD_CTRL_ACTIVE; 1300 list_for_each_entry_safe(qh, tmp, &uhci->skel_unlink_qh->node, node) {
1354 }
1355 1301
1356 uhci_delete_queued_urb(uhci, urb); 1302 /* If the queue is empty and the QH is fully unlinked then
1303 * it can become IDLE. */
1304 if (list_empty(&qh->queue)) {
1305 if (uhci->frame_number + uhci->is_stopped !=
1306 qh->unlink_frame)
1307 uhci_make_qh_idle(uhci, qh);
1357 1308
1358 /* The interrupt loop will reclaim the QHs */ 1309 /* If none of the QH's URBs have been dequeued then the QH
1359 uhci_remove_qh(uhci, urbp->qh); 1310 * should be re-activated. */
1360 urbp->qh = NULL; 1311 } else {
1312 struct urb_priv *urbp;
1313 int any_dequeued = 0;
1314
1315 list_for_each_entry(urbp, &qh->queue, node) {
1316 if (urbp->urb->status != -EINPROGRESS) {
1317 any_dequeued = 1;
1318 break;
1319 }
1320 }
1321 if (!any_dequeued)
1322 uhci_activate_qh(uhci, qh);
1323 }
1324 }
1361} 1325}
1362 1326
1363static int uhci_urb_dequeue(struct usb_hcd *hcd, struct urb *urb) 1327static int uhci_urb_dequeue(struct usb_hcd *hcd, struct urb *urb)
@@ -1370,22 +1334,11 @@ static int uhci_urb_dequeue(struct usb_hcd *hcd, struct urb *urb)
1370 urbp = urb->hcpriv; 1334 urbp = urb->hcpriv;
1371 if (!urbp) /* URB was never linked! */ 1335 if (!urbp) /* URB was never linked! */
1372 goto done; 1336 goto done;
1373 list_del_init(&urbp->urb_list);
1374 1337
1338 /* Remove Isochronous TDs from the frame list ASAP */
1375 if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS) 1339 if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS)
1376 unlink_isochronous_tds(uhci, urb); 1340 uhci_unlink_isochronous_tds(uhci, urb);
1377 uhci_unlink_generic(uhci, urb); 1341 uhci_unlink_qh(uhci, urbp->qh);
1378
1379 uhci_get_current_frame_number(uhci);
1380 if (uhci->frame_number + uhci->is_stopped != uhci->urb_remove_age) {
1381 uhci_remove_pending_urbps(uhci);
1382 uhci->urb_remove_age = uhci->frame_number;
1383 }
1384
1385 /* If we're the first, set the next interrupt bit */
1386 if (list_empty(&uhci->urb_remove_list))
1387 uhci_set_next_interrupt(uhci);
1388 list_add_tail(&urbp->urb_list, &uhci->urb_remove_list);
1389 1342
1390done: 1343done:
1391 spin_unlock_irqrestore(&uhci->lock, flags); 1344 spin_unlock_irqrestore(&uhci->lock, flags);
@@ -1426,17 +1379,6 @@ static int uhci_fsbr_timeout(struct uhci_hcd *uhci, struct urb *urb)
1426 return 0; 1379 return 0;
1427} 1380}
1428 1381
1429static void uhci_free_pending_qhs(struct uhci_hcd *uhci)
1430{
1431 struct uhci_qh *qh, *tmp;
1432
1433 list_for_each_entry_safe(qh, tmp, &uhci->qh_remove_list, remove_list) {
1434 list_del_init(&qh->remove_list);
1435
1436 uhci_free_qh(uhci, qh);
1437 }
1438}
1439
1440static void uhci_free_pending_tds(struct uhci_hcd *uhci) 1382static void uhci_free_pending_tds(struct uhci_hcd *uhci)
1441{ 1383{
1442 struct uhci_td *td, *tmp; 1384 struct uhci_td *td, *tmp;
@@ -1455,7 +1397,7 @@ __acquires(uhci->lock)
1455{ 1397{
1456 struct uhci_hcd *uhci = hcd_to_uhci(hcd); 1398 struct uhci_hcd *uhci = hcd_to_uhci(hcd);
1457 1399
1458 uhci_destroy_urb_priv(uhci, urb); 1400 uhci_free_urb_priv(uhci, (struct urb_priv *) (urb->hcpriv));
1459 1401
1460 spin_unlock(&uhci->lock); 1402 spin_unlock(&uhci->lock);
1461 usb_hcd_giveback_urb(hcd, urb, regs); 1403 usb_hcd_giveback_urb(hcd, urb, regs);
@@ -1474,13 +1416,6 @@ static void uhci_finish_completion(struct uhci_hcd *uhci, struct pt_regs *regs)
1474 } 1416 }
1475} 1417}
1476 1418
1477static void uhci_remove_pending_urbps(struct uhci_hcd *uhci)
1478{
1479
1480 /* Splice the urb_remove_list onto the end of the complete_list */
1481 list_splice_init(&uhci->urb_remove_list, uhci->complete_list.prev);
1482}
1483
1484/* Process events in the schedule, but only in one thread at a time */ 1419/* Process events in the schedule, but only in one thread at a time */
1485static void uhci_scan_schedule(struct uhci_hcd *uhci, struct pt_regs *regs) 1420static void uhci_scan_schedule(struct uhci_hcd *uhci, struct pt_regs *regs)
1486{ 1421{
@@ -1498,12 +1433,8 @@ static void uhci_scan_schedule(struct uhci_hcd *uhci, struct pt_regs *regs)
1498 uhci_clear_next_interrupt(uhci); 1433 uhci_clear_next_interrupt(uhci);
1499 uhci_get_current_frame_number(uhci); 1434 uhci_get_current_frame_number(uhci);
1500 1435
1501 if (uhci->frame_number + uhci->is_stopped != uhci->qh_remove_age)
1502 uhci_free_pending_qhs(uhci);
1503 if (uhci->frame_number + uhci->is_stopped != uhci->td_remove_age) 1436 if (uhci->frame_number + uhci->is_stopped != uhci->td_remove_age)
1504 uhci_free_pending_tds(uhci); 1437 uhci_free_pending_tds(uhci);
1505 if (uhci->frame_number + uhci->is_stopped != uhci->urb_remove_age)
1506 uhci_remove_pending_urbps(uhci);
1507 1438
1508 /* Walk the list of pending URBs to see which ones completed 1439 /* Walk the list of pending URBs to see which ones completed
1509 * (must be _safe because uhci_transfer_result() dequeues URBs) */ 1440 * (must be _safe because uhci_transfer_result() dequeues URBs) */
@@ -1516,25 +1447,21 @@ static void uhci_scan_schedule(struct uhci_hcd *uhci, struct pt_regs *regs)
1516 uhci_finish_completion(uhci, regs); 1447 uhci_finish_completion(uhci, regs);
1517 1448
1518 /* If the controller is stopped, we can finish these off right now */ 1449 /* If the controller is stopped, we can finish these off right now */
1519 if (uhci->is_stopped) { 1450 if (uhci->is_stopped)
1520 uhci_free_pending_qhs(uhci);
1521 uhci_free_pending_tds(uhci); 1451 uhci_free_pending_tds(uhci);
1522 uhci_remove_pending_urbps(uhci);
1523 }
1524 1452
1525 if (uhci->need_rescan) 1453 if (uhci->need_rescan)
1526 goto rescan; 1454 goto rescan;
1527 uhci->scan_in_progress = 0; 1455 uhci->scan_in_progress = 0;
1528 1456
1529 if (list_empty(&uhci->urb_remove_list) && 1457 /* Check out the QHs waiting for unlinking */
1530 list_empty(&uhci->td_remove_list) && 1458 uhci_scan_unlinking_qhs(uhci);
1531 list_empty(&uhci->qh_remove_list)) 1459
1460 if (list_empty(&uhci->td_remove_list) &&
1461 list_empty(&uhci->skel_unlink_qh->node))
1532 uhci_clear_next_interrupt(uhci); 1462 uhci_clear_next_interrupt(uhci);
1533 else 1463 else
1534 uhci_set_next_interrupt(uhci); 1464 uhci_set_next_interrupt(uhci);
1535
1536 /* Wake up anyone waiting for an URB to complete */
1537 wake_up_all(&uhci->waitqh);
1538} 1465}
1539 1466
1540static void check_fsbr(struct uhci_hcd *uhci) 1467static void check_fsbr(struct uhci_hcd *uhci)