aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/usb/host/uhci-q.c
diff options
context:
space:
mode:
Diffstat (limited to 'drivers/usb/host/uhci-q.c')
-rw-r--r--drivers/usb/host/uhci-q.c1294
1 files changed, 571 insertions, 723 deletions
diff --git a/drivers/usb/host/uhci-q.c b/drivers/usb/host/uhci-q.c
index 782398045f9f..a06d84c19e13 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;
@@ -58,10 +48,6 @@ static struct uhci_td *uhci_alloc_td(struct uhci_hcd *uhci)
58 return NULL; 48 return NULL;
59 49
60 td->dma_handle = dma_handle; 50 td->dma_handle = dma_handle;
61
62 td->link = UHCI_PTR_TERM;
63 td->buffer = 0;
64
65 td->frame = -1; 51 td->frame = -1;
66 52
67 INIT_LIST_HEAD(&td->list); 53 INIT_LIST_HEAD(&td->list);
@@ -71,6 +57,18 @@ static struct uhci_td *uhci_alloc_td(struct uhci_hcd *uhci)
71 return td; 57 return td;
72} 58}
73 59
60static void uhci_free_td(struct uhci_hcd *uhci, struct uhci_td *td)
61{
62 if (!list_empty(&td->list))
63 dev_warn(uhci_dev(uhci), "td %p still in list!\n", td);
64 if (!list_empty(&td->remove_list))
65 dev_warn(uhci_dev(uhci), "td %p still in remove_list!\n", td);
66 if (!list_empty(&td->fl_list))
67 dev_warn(uhci_dev(uhci), "td %p still in fl_list!\n", td);
68
69 dma_pool_free(uhci->td_pool, td, td->dma_handle);
70}
71
74static inline void uhci_fill_td(struct uhci_td *td, u32 status, 72static inline void uhci_fill_td(struct uhci_td *td, u32 status,
75 u32 token, u32 buffer) 73 u32 token, u32 buffer)
76{ 74{
@@ -82,7 +80,8 @@ static inline void uhci_fill_td(struct uhci_td *td, u32 status,
82/* 80/*
83 * We insert Isochronous URBs directly into the frame list at the beginning 81 * We insert Isochronous URBs directly into the frame list at the beginning
84 */ 82 */
85static void uhci_insert_td_frame_list(struct uhci_hcd *uhci, struct uhci_td *td, unsigned framenum) 83static inline void uhci_insert_td_in_frame_list(struct uhci_hcd *uhci,
84 struct uhci_td *td, unsigned framenum)
86{ 85{
87 framenum &= (UHCI_NUMFRAMES - 1); 86 framenum &= (UHCI_NUMFRAMES - 1);
88 87
@@ -108,7 +107,7 @@ static void uhci_insert_td_frame_list(struct uhci_hcd *uhci, struct uhci_td *td,
108 } 107 }
109} 108}
110 109
111static inline void uhci_remove_td_frame_list(struct uhci_hcd *uhci, 110static inline void uhci_remove_td_from_frame_list(struct uhci_hcd *uhci,
112 struct uhci_td *td) 111 struct uhci_td *td)
113{ 112{
114 /* If it's not inserted, don't remove it */ 113 /* If it's not inserted, don't remove it */
@@ -139,48 +138,21 @@ static inline void uhci_remove_td_frame_list(struct uhci_hcd *uhci,
139 td->frame = -1; 138 td->frame = -1;
140} 139}
141 140
142static void unlink_isochronous_tds(struct uhci_hcd *uhci, struct urb *urb) 141/*
142 * Remove all the TDs for an Isochronous URB from the frame list
143 */
144static void uhci_unlink_isochronous_tds(struct uhci_hcd *uhci, struct urb *urb)
143{ 145{
144 struct urb_priv *urbp = (struct urb_priv *) urb->hcpriv; 146 struct urb_priv *urbp = (struct urb_priv *) urb->hcpriv;
145 struct uhci_td *td; 147 struct uhci_td *td;
146 148
147 list_for_each_entry(td, &urbp->td_list, list) 149 list_for_each_entry(td, &urbp->td_list, list)
148 uhci_remove_td_frame_list(uhci, td); 150 uhci_remove_td_from_frame_list(uhci, td);
149 wmb(); 151 wmb();
150} 152}
151 153
152/* 154static struct uhci_qh *uhci_alloc_qh(struct uhci_hcd *uhci,
153 * Inserts a td list into qh. 155 struct usb_device *udev, struct usb_host_endpoint *hep)
154 */
155static void uhci_insert_tds_in_qh(struct uhci_qh *qh, struct urb *urb, __le32 breadth)
156{
157 struct urb_priv *urbp = (struct urb_priv *)urb->hcpriv;
158 struct uhci_td *td;
159 __le32 *plink;
160
161 /* Ordering isn't important here yet since the QH hasn't been */
162 /* inserted into the schedule yet */
163 plink = &qh->element;
164 list_for_each_entry(td, &urbp->td_list, list) {
165 *plink = cpu_to_le32(td->dma_handle) | breadth;
166 plink = &td->link;
167 }
168 *plink = UHCI_PTR_TERM;
169}
170
171static void uhci_free_td(struct uhci_hcd *uhci, struct uhci_td *td)
172{
173 if (!list_empty(&td->list))
174 dev_warn(uhci_dev(uhci), "td %p still in list!\n", td);
175 if (!list_empty(&td->remove_list))
176 dev_warn(uhci_dev(uhci), "td %p still in remove_list!\n", td);
177 if (!list_empty(&td->fl_list))
178 dev_warn(uhci_dev(uhci), "td %p still in fl_list!\n", td);
179
180 dma_pool_free(uhci->td_pool, td, td->dma_handle);
181}
182
183static struct uhci_qh *uhci_alloc_qh(struct uhci_hcd *uhci)
184{ 156{
185 dma_addr_t dma_handle; 157 dma_addr_t dma_handle;
186 struct uhci_qh *qh; 158 struct uhci_qh *qh;
@@ -194,256 +166,217 @@ static struct uhci_qh *uhci_alloc_qh(struct uhci_hcd *uhci)
194 qh->element = UHCI_PTR_TERM; 166 qh->element = UHCI_PTR_TERM;
195 qh->link = UHCI_PTR_TERM; 167 qh->link = UHCI_PTR_TERM;
196 168
197 qh->urbp = NULL; 169 INIT_LIST_HEAD(&qh->queue);
198 170 INIT_LIST_HEAD(&qh->node);
199 INIT_LIST_HEAD(&qh->list);
200 INIT_LIST_HEAD(&qh->remove_list);
201 171
172 if (udev) { /* Normal QH */
173 qh->dummy_td = uhci_alloc_td(uhci);
174 if (!qh->dummy_td) {
175 dma_pool_free(uhci->qh_pool, qh, dma_handle);
176 return NULL;
177 }
178 qh->state = QH_STATE_IDLE;
179 qh->hep = hep;
180 qh->udev = udev;
181 hep->hcpriv = qh;
182
183 } else { /* Skeleton QH */
184 qh->state = QH_STATE_ACTIVE;
185 qh->udev = NULL;
186 }
202 return qh; 187 return qh;
203} 188}
204 189
205static void uhci_free_qh(struct uhci_hcd *uhci, struct uhci_qh *qh) 190static void uhci_free_qh(struct uhci_hcd *uhci, struct uhci_qh *qh)
206{ 191{
207 if (!list_empty(&qh->list)) 192 WARN_ON(qh->state != QH_STATE_IDLE && qh->udev);
193 if (!list_empty(&qh->queue))
208 dev_warn(uhci_dev(uhci), "qh %p list not empty!\n", qh); 194 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 195
196 list_del(&qh->node);
197 if (qh->udev) {
198 qh->hep->hcpriv = NULL;
199 uhci_free_td(uhci, qh->dummy_td);
200 }
212 dma_pool_free(uhci->qh_pool, qh, qh->dma_handle); 201 dma_pool_free(uhci->qh_pool, qh, qh->dma_handle);
213} 202}
214 203
215/* 204/*
216 * Append this urb's qh after the last qh in skelqh->list 205 * When the currently executing URB is dequeued, save its current toggle value
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 */ 206 */
221static void uhci_insert_qh(struct uhci_hcd *uhci, struct uhci_qh *skelqh, struct urb *urb) 207static void uhci_save_toggle(struct uhci_qh *qh, struct urb *urb)
222{ 208{
223 struct urb_priv *urbp = (struct urb_priv *)urb->hcpriv; 209 struct urb_priv *urbp = (struct urb_priv *) urb->hcpriv;
224 struct urb_priv *turbp; 210 struct uhci_td *td;
225 struct uhci_qh *lqh;
226 211
227 /* Grab the last QH */ 212 /* If the QH element pointer is UHCI_PTR_TERM then then currently
228 lqh = list_entry(skelqh->list.prev, struct uhci_qh, list); 213 * executing URB has already been unlinked, so this one isn't it. */
214 if (qh_element(qh) == UHCI_PTR_TERM ||
215 qh->queue.next != &urbp->node)
216 return;
217 qh->element = UHCI_PTR_TERM;
229 218
230 /* Point to the next skelqh */ 219 /* Only bulk and interrupt pipes have to worry about toggles */
231 urbp->qh->link = lqh->link; 220 if (!(usb_pipetype(urb->pipe) == PIPE_BULK ||
232 wmb(); /* Ordering is important */ 221 usb_pipetype(urb->pipe) == PIPE_INTERRUPT))
222 return;
233 223
234 /* 224 /* Find the first active TD; that's the device's toggle state */
235 * Patch QHs for previous endpoint's queued URBs? HC goes 225 list_for_each_entry(td, &urbp->td_list, list) {
236 * here next, not to the next skelqh it now points to. 226 if (td_status(td) & TD_CTRL_ACTIVE) {
237 * 227 qh->needs_fixup = 1;
238 * lqh --> td ... --> qh ... --> td --> qh ... --> td 228 qh->initial_toggle = uhci_toggle(td_token(td));
239 * | | | 229 return;
240 * v v v 230 }
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 } 231 }
255 232
256 list_add_tail(&urbp->qh->list, &skelqh->list); 233 WARN_ON(1);
257} 234}
258 235
259/* 236/*
260 * Start removal of QH from schedule; it finishes next frame. 237 * Fix up the data toggles for URBs in a queue, when one of them
261 * TDs should be unlinked before this is called. 238 * terminates early (short transfer, error, or dequeued).
262 */ 239 */
263static void uhci_remove_qh(struct uhci_hcd *uhci, struct uhci_qh *qh) 240static void uhci_fixup_toggles(struct uhci_qh *qh, int skip_first)
264{ 241{
265 struct uhci_qh *pqh; 242 struct urb_priv *urbp = NULL;
266 __le32 newlink; 243 struct uhci_td *td;
267 244 unsigned int toggle = qh->initial_toggle;
268 if (!qh) 245 unsigned int pipe;
269 return; 246
270 247 /* Fixups for a short transfer start with the second URB in the
271 /* 248 * queue (the short URB is the first). */
272 * Only go through the hoops if it's actually linked in 249 if (skip_first)
273 */ 250 urbp = list_entry(qh->queue.next, struct urb_priv, node);
274 if (!list_empty(&qh->list)) { 251
275 252 /* When starting with the first URB, if the QH element pointer is
276 /* If our queue is nonempty, make the next URB the head */ 253 * still valid then we know the URB's toggles are okay. */
277 if (!list_empty(&qh->urbp->queue_list)) { 254 else if (qh_element(qh) != UHCI_PTR_TERM)
278 struct urb_priv *nurbp; 255 toggle = 2;
279 256
280 nurbp = list_entry(qh->urbp->queue_list.next, 257 /* Fix up the toggle for the URBs in the queue. Normally this
281 struct urb_priv, queue_list); 258 * loop won't run more than once: When an error or short transfer
282 nurbp->queued = 0; 259 * occurs, the queue usually gets emptied. */
283 list_add(&nurbp->qh->list, &qh->list); 260 urbp = list_prepare_entry(urbp, &qh->queue, node);
284 newlink = cpu_to_le32(nurbp->qh->dma_handle) | UHCI_PTR_QH; 261 list_for_each_entry_continue(urbp, &qh->queue, node) {
285 } else 262
286 newlink = qh->link; 263 /* If the first TD has the right toggle value, we don't
287 264 * need to change any toggles in this URB */
288 /* Fix up the previous QH's queue to link to either 265 td = list_entry(urbp->td_list.next, struct uhci_td, list);
289 * the new head of this queue or the start of the 266 if (toggle > 1 || uhci_toggle(td_token(td)) == toggle) {
290 * next endpoint's queue. */ 267 td = list_entry(urbp->td_list.next, struct uhci_td,
291 pqh = list_entry(qh->list.prev, struct uhci_qh, list); 268 list);
292 pqh->link = newlink; 269 toggle = uhci_toggle(td_token(td)) ^ 1;
293 if (pqh->urbp) { 270
294 struct urb_priv *turbp; 271 /* Otherwise all the toggles in the URB have to be switched */
295 272 } else {
296 list_for_each_entry(turbp, &pqh->urbp->queue_list, 273 list_for_each_entry(td, &urbp->td_list, list) {
297 queue_list) 274 td->token ^= __constant_cpu_to_le32(
298 turbp->qh->link = newlink; 275 TD_TOKEN_TOGGLE);
276 toggle ^= 1;
277 }
299 } 278 }
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
312 uhci_get_current_frame_number(uhci);
313 if (uhci->frame_number + uhci->is_stopped != uhci->qh_remove_age) {
314 uhci_free_pending_qhs(uhci);
315 uhci->qh_remove_age = uhci->frame_number;
316 } 279 }
317 280
318 /* Check to see if the remove list is empty. Set the IOC bit */ 281 wmb();
319 /* to force an interrupt so we can remove the QH */ 282 pipe = list_entry(qh->queue.next, struct urb_priv, node)->urb->pipe;
320 if (list_empty(&uhci->qh_remove_list)) 283 usb_settoggle(qh->udev, usb_pipeendpoint(pipe),
321 uhci_set_next_interrupt(uhci); 284 usb_pipeout(pipe), toggle);
322 285 qh->needs_fixup = 0;
323 list_add(&qh->remove_list, &uhci->qh_remove_list);
324} 286}
325 287
326static int uhci_fixup_toggle(struct urb *urb, unsigned int toggle) 288/*
289 * Put a QH on the schedule in both hardware and software
290 */
291static void uhci_activate_qh(struct uhci_hcd *uhci, struct uhci_qh *qh)
327{ 292{
328 struct urb_priv *urbp = (struct urb_priv *)urb->hcpriv; 293 struct uhci_qh *pqh;
329 struct uhci_td *td;
330
331 list_for_each_entry(td, &urbp->td_list, list) {
332 if (toggle)
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 294
343/* This function will append one URB's QH to another URB's QH. This is for */ 295 WARN_ON(list_empty(&qh->queue));
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 296
350 eurbp = eurb->hcpriv; 297 /* Set the element pointer if it isn't set already.
351 urbp = urb->hcpriv; 298 * This isn't needed for Isochronous queues, but it doesn't hurt. */
299 if (qh_element(qh) == UHCI_PTR_TERM) {
300 struct urb_priv *urbp = list_entry(qh->queue.next,
301 struct urb_priv, node);
302 struct uhci_td *td = list_entry(urbp->td_list.next,
303 struct uhci_td, list);
352 304
353 /* Find the first URB in the queue */ 305 qh->element = cpu_to_le32(td->dma_handle);
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 } 306 }
360 307
361 lurbp = list_entry(furbp->queue_list.prev, struct urb_priv, queue_list); 308 if (qh->state == QH_STATE_ACTIVE)
362 309 return;
363 lltd = list_entry(lurbp->td_list.prev, struct uhci_td, list); 310 qh->state = QH_STATE_ACTIVE;
364 311
365 /* Control transfers always start with toggle 0 */ 312 /* Move the QH from its old list to the end of the appropriate
366 if (!usb_pipecontrol(urb->pipe)) 313 * skeleton's list */
367 usb_settoggle(urb->dev, usb_pipeendpoint(urb->pipe), 314 if (qh == uhci->next_qh)
368 usb_pipeout(urb->pipe), 315 uhci->next_qh = list_entry(qh->node.next, struct uhci_qh,
369 uhci_fixup_toggle(urb, 316 node);
370 uhci_toggle(td_token(lltd)) ^ 1)); 317 list_move_tail(&qh->node, &qh->skel->node);
371 318
372 /* All qhs in the queue need to link to the next queue */ 319 /* Link it into the schedule */
373 urbp->qh->link = eurbp->qh->link; 320 pqh = list_entry(qh->node.prev, struct uhci_qh, node);
374 321 qh->link = pqh->link;
375 wmb(); /* Make sure we flush everything */ 322 wmb();
376 323 pqh->link = UHCI_PTR_QH | cpu_to_le32(qh->dma_handle);
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} 324}
383 325
384static void uhci_delete_queued_urb(struct uhci_hcd *uhci, struct urb *urb) 326/*
327 * Take a QH off the hardware schedule
328 */
329static void uhci_unlink_qh(struct uhci_hcd *uhci, struct uhci_qh *qh)
385{ 330{
386 struct urb_priv *urbp, *nurbp, *purbp, *turbp; 331 struct uhci_qh *pqh;
387 struct uhci_td *pltd;
388 unsigned int toggle;
389
390 urbp = urb->hcpriv;
391 332
392 if (list_empty(&urbp->queue_list)) 333 if (qh->state == QH_STATE_UNLINKING)
393 return; 334 return;
335 WARN_ON(qh->state != QH_STATE_ACTIVE || !qh->udev);
336 qh->state = QH_STATE_UNLINKING;
394 337
395 nurbp = list_entry(urbp->queue_list.next, struct urb_priv, queue_list); 338 /* Unlink the QH from the schedule and record when we did it */
339 pqh = list_entry(qh->node.prev, struct uhci_qh, node);
340 pqh->link = qh->link;
341 mb();
396 342
397 /* 343 uhci_get_current_frame_number(uhci);
398 * Fix up the toggle for the following URBs in the queue. 344 qh->unlink_frame = uhci->frame_number;
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 345
418 list_for_each_entry(turbp, &urbp->queue_list, queue_list) { 346 /* Force an interrupt so we know when the QH is fully unlinked */
419 if (!turbp->queued) 347 if (list_empty(&uhci->skel_unlink_qh->node))
420 break; 348 uhci_set_next_interrupt(uhci);
421 toggle = uhci_fixup_toggle(turbp->urb, toggle);
422 }
423 349
424 usb_settoggle(urb->dev, usb_pipeendpoint(urb->pipe), 350 /* Move the QH from its old list to the end of the unlinking list */
425 usb_pipeout(urb->pipe), toggle); 351 if (qh == uhci->next_qh)
426 } 352 uhci->next_qh = list_entry(qh->node.next, struct uhci_qh,
353 node);
354 list_move_tail(&qh->node, &uhci->skel_unlink_qh->node);
355}
427 356
428 if (urbp->queued) { 357/*
429 /* We're somewhere in the middle (or end). The case where 358 * When we and the controller are through with a QH, it becomes IDLE.
430 * we're at the head is handled in uhci_remove_qh(). */ 359 * This happens when a QH has been off the schedule (on the unlinking
431 purbp = list_entry(urbp->queue_list.prev, struct urb_priv, 360 * list) for more than one frame, or when an error occurs while adding
432 queue_list); 361 * the first URB onto a new QH.
362 */
363static void uhci_make_qh_idle(struct uhci_hcd *uhci, struct uhci_qh *qh)
364{
365 WARN_ON(qh->state == QH_STATE_ACTIVE);
433 366
434 pltd = list_entry(purbp->td_list.prev, struct uhci_td, list); 367 if (qh == uhci->next_qh)
435 if (nurbp->queued) 368 uhci->next_qh = list_entry(qh->node.next, struct uhci_qh,
436 pltd->link = cpu_to_le32(nurbp->qh->dma_handle) | UHCI_PTR_QH; 369 node);
437 else 370 list_move(&qh->node, &uhci->idle_qh_list);
438 /* The next URB happens to be the beginning, so */ 371 qh->state = QH_STATE_IDLE;
439 /* we're the last, end the chain */
440 pltd->link = UHCI_PTR_TERM;
441 }
442 372
443 /* urbp->queue_list is handled in uhci_remove_qh() */ 373 /* If anyone is waiting for a QH to become idle, wake them up */
374 if (uhci->num_waiting)
375 wake_up_all(&uhci->waitqh);
444} 376}
445 377
446static struct urb_priv *uhci_alloc_urb_priv(struct uhci_hcd *uhci, struct urb *urb) 378static inline struct urb_priv *uhci_alloc_urb_priv(struct uhci_hcd *uhci,
379 struct urb *urb)
447{ 380{
448 struct urb_priv *urbp; 381 struct urb_priv *urbp;
449 382
@@ -453,16 +386,11 @@ static struct urb_priv *uhci_alloc_urb_priv(struct uhci_hcd *uhci, struct urb *u
453 386
454 memset((void *)urbp, 0, sizeof(*urbp)); 387 memset((void *)urbp, 0, sizeof(*urbp));
455 388
456 urbp->fsbrtime = jiffies;
457 urbp->urb = urb; 389 urbp->urb = urb;
390 urb->hcpriv = urbp;
458 391
392 INIT_LIST_HEAD(&urbp->node);
459 INIT_LIST_HEAD(&urbp->td_list); 393 INIT_LIST_HEAD(&urbp->td_list);
460 INIT_LIST_HEAD(&urbp->queue_list);
461 INIT_LIST_HEAD(&urbp->urb_list);
462
463 list_add_tail(&urbp->urb_list, &uhci->urb_list);
464
465 urb->hcpriv = urbp;
466 394
467 return urbp; 395 return urbp;
468} 396}
@@ -482,18 +410,14 @@ static void uhci_remove_td_from_urb(struct uhci_td *td)
482 list_del_init(&td->list); 410 list_del_init(&td->list);
483} 411}
484 412
485static void uhci_destroy_urb_priv(struct uhci_hcd *uhci, struct urb *urb) 413static void uhci_free_urb_priv(struct uhci_hcd *uhci,
414 struct urb_priv *urbp)
486{ 415{
487 struct uhci_td *td, *tmp; 416 struct uhci_td *td, *tmp;
488 struct urb_priv *urbp;
489
490 urbp = (struct urb_priv *)urb->hcpriv;
491 if (!urbp)
492 return;
493 417
494 if (!list_empty(&urbp->urb_list)) 418 if (!list_empty(&urbp->node))
495 dev_warn(uhci_dev(uhci), "urb %p still on uhci->urb_list " 419 dev_warn(uhci_dev(uhci), "urb %p still on QH's list!\n",
496 "or uhci->remove_list!\n", urb); 420 urbp->urb);
497 421
498 uhci_get_current_frame_number(uhci); 422 uhci_get_current_frame_number(uhci);
499 if (uhci->frame_number + uhci->is_stopped != uhci->td_remove_age) { 423 if (uhci->frame_number + uhci->is_stopped != uhci->td_remove_age) {
@@ -502,7 +426,7 @@ static void uhci_destroy_urb_priv(struct uhci_hcd *uhci, struct urb *urb)
502 } 426 }
503 427
504 /* Check to see if the remove list is empty. Set the IOC bit */ 428 /* Check to see if the remove list is empty. Set the IOC bit */
505 /* to force an interrupt so we can remove the TDs*/ 429 /* to force an interrupt so we can remove the TDs. */
506 if (list_empty(&uhci->td_remove_list)) 430 if (list_empty(&uhci->td_remove_list))
507 uhci_set_next_interrupt(uhci); 431 uhci_set_next_interrupt(uhci);
508 432
@@ -511,7 +435,7 @@ static void uhci_destroy_urb_priv(struct uhci_hcd *uhci, struct urb *urb)
511 list_add(&td->remove_list, &uhci->td_remove_list); 435 list_add(&td->remove_list, &uhci->td_remove_list);
512 } 436 }
513 437
514 urb->hcpriv = NULL; 438 urbp->urb->hcpriv = NULL;
515 kmem_cache_free(uhci_up_cachep, urbp); 439 kmem_cache_free(uhci_up_cachep, urbp);
516} 440}
517 441
@@ -570,34 +494,33 @@ static int uhci_map_status(int status, int dir_out)
570/* 494/*
571 * Control transfers 495 * Control transfers
572 */ 496 */
573static int uhci_submit_control(struct uhci_hcd *uhci, struct urb *urb, struct urb *eurb) 497static int uhci_submit_control(struct uhci_hcd *uhci, struct urb *urb,
498 struct uhci_qh *qh)
574{ 499{
575 struct urb_priv *urbp = (struct urb_priv *)urb->hcpriv;
576 struct uhci_td *td; 500 struct uhci_td *td;
577 struct uhci_qh *qh, *skelqh;
578 unsigned long destination, status; 501 unsigned long destination, status;
579 int maxsze = usb_maxpacket(urb->dev, urb->pipe, usb_pipeout(urb->pipe)); 502 int maxsze = le16_to_cpu(qh->hep->desc.wMaxPacketSize);
580 int len = urb->transfer_buffer_length; 503 int len = urb->transfer_buffer_length;
581 dma_addr_t data = urb->transfer_dma; 504 dma_addr_t data = urb->transfer_dma;
505 __le32 *plink;
582 506
583 /* The "pipe" thing contains the destination in bits 8--18 */ 507 /* The "pipe" thing contains the destination in bits 8--18 */
584 destination = (urb->pipe & PIPE_DEVEP_MASK) | USB_PID_SETUP; 508 destination = (urb->pipe & PIPE_DEVEP_MASK) | USB_PID_SETUP;
585 509
586 /* 3 errors */ 510 /* 3 errors, dummy TD remains inactive */
587 status = TD_CTRL_ACTIVE | uhci_maxerr(3); 511 status = uhci_maxerr(3);
588 if (urb->dev->speed == USB_SPEED_LOW) 512 if (urb->dev->speed == USB_SPEED_LOW)
589 status |= TD_CTRL_LS; 513 status |= TD_CTRL_LS;
590 514
591 /* 515 /*
592 * Build the TD for the control request setup packet 516 * Build the TD for the control request setup packet
593 */ 517 */
594 td = uhci_alloc_td(uhci); 518 td = qh->dummy_td;
595 if (!td)
596 return -ENOMEM;
597
598 uhci_add_td_to_urb(urb, td); 519 uhci_add_td_to_urb(urb, td);
599 uhci_fill_td(td, status, destination | uhci_explen(8), 520 uhci_fill_td(td, status, destination | uhci_explen(8),
600 urb->setup_dma); 521 urb->setup_dma);
522 plink = &td->link;
523 status |= TD_CTRL_ACTIVE;
601 524
602 /* 525 /*
603 * If direction is "send", change the packet ID from SETUP (0x2D) 526 * If direction is "send", change the packet ID from SETUP (0x2D)
@@ -615,21 +538,20 @@ static int uhci_submit_control(struct uhci_hcd *uhci, struct urb *urb, struct ur
615 * Build the DATA TDs 538 * Build the DATA TDs
616 */ 539 */
617 while (len > 0) { 540 while (len > 0) {
618 int pktsze = len; 541 int pktsze = min(len, maxsze);
619
620 if (pktsze > maxsze)
621 pktsze = maxsze;
622 542
623 td = uhci_alloc_td(uhci); 543 td = uhci_alloc_td(uhci);
624 if (!td) 544 if (!td)
625 return -ENOMEM; 545 goto nomem;
546 *plink = cpu_to_le32(td->dma_handle);
626 547
627 /* Alternate Data0/1 (start with Data1) */ 548 /* Alternate Data0/1 (start with Data1) */
628 destination ^= TD_TOKEN_TOGGLE; 549 destination ^= TD_TOKEN_TOGGLE;
629 550
630 uhci_add_td_to_urb(urb, td); 551 uhci_add_td_to_urb(urb, td);
631 uhci_fill_td(td, status, destination | uhci_explen(pktsze), 552 uhci_fill_td(td, status, destination | uhci_explen(pktsze),
632 data); 553 data);
554 plink = &td->link;
633 555
634 data += pktsze; 556 data += pktsze;
635 len -= pktsze; 557 len -= pktsze;
@@ -640,7 +562,8 @@ static int uhci_submit_control(struct uhci_hcd *uhci, struct urb *urb, struct ur
640 */ 562 */
641 td = uhci_alloc_td(uhci); 563 td = uhci_alloc_td(uhci);
642 if (!td) 564 if (!td)
643 return -ENOMEM; 565 goto nomem;
566 *plink = cpu_to_le32(td->dma_handle);
644 567
645 /* 568 /*
646 * It's IN if the pipe is an output pipe or we're not expecting 569 * It's IN if the pipe is an output pipe or we're not expecting
@@ -658,16 +581,21 @@ static int uhci_submit_control(struct uhci_hcd *uhci, struct urb *urb, struct ur
658 581
659 uhci_add_td_to_urb(urb, td); 582 uhci_add_td_to_urb(urb, td);
660 uhci_fill_td(td, status | TD_CTRL_IOC, 583 uhci_fill_td(td, status | TD_CTRL_IOC,
661 destination | uhci_explen(0), 0); 584 destination | uhci_explen(0), 0);
662 585 plink = &td->link;
663 qh = uhci_alloc_qh(uhci);
664 if (!qh)
665 return -ENOMEM;
666 586
667 urbp->qh = qh; 587 /*
668 qh->urbp = urbp; 588 * Build the new dummy TD and activate the old one
589 */
590 td = uhci_alloc_td(uhci);
591 if (!td)
592 goto nomem;
593 *plink = cpu_to_le32(td->dma_handle);
669 594
670 uhci_insert_tds_in_qh(qh, urb, UHCI_PTR_BREADTH); 595 uhci_fill_td(td, 0, USB_PID_OUT | uhci_explen(0), 0);
596 wmb();
597 qh->dummy_td->status |= __constant_cpu_to_le32(TD_CTRL_ACTIVE);
598 qh->dummy_td = td;
671 599
672 /* Low-speed transfers get a different queue, and won't hog the bus. 600 /* Low-speed transfers get a different queue, and won't hog the bus.
673 * Also, some devices enumerate better without FSBR; the easiest way 601 * Also, some devices enumerate better without FSBR; the easiest way
@@ -675,18 +603,17 @@ static int uhci_submit_control(struct uhci_hcd *uhci, struct urb *urb, struct ur
675 * isn't in the CONFIGURED state. */ 603 * isn't in the CONFIGURED state. */
676 if (urb->dev->speed == USB_SPEED_LOW || 604 if (urb->dev->speed == USB_SPEED_LOW ||
677 urb->dev->state != USB_STATE_CONFIGURED) 605 urb->dev->state != USB_STATE_CONFIGURED)
678 skelqh = uhci->skel_ls_control_qh; 606 qh->skel = uhci->skel_ls_control_qh;
679 else { 607 else {
680 skelqh = uhci->skel_fs_control_qh; 608 qh->skel = uhci->skel_fs_control_qh;
681 uhci_inc_fsbr(uhci, urb); 609 uhci_inc_fsbr(uhci, urb);
682 } 610 }
611 return 0;
683 612
684 if (eurb) 613nomem:
685 uhci_append_queued_urb(uhci, eurb, urb); 614 /* Remove the dummy TD from the td_list so it doesn't get freed */
686 else 615 uhci_remove_td_from_urb(qh->dummy_td);
687 uhci_insert_qh(uhci, skelqh, urb); 616 return -ENOMEM;
688
689 return -EINPROGRESS;
690} 617}
691 618
692/* 619/*
@@ -703,7 +630,7 @@ static int usb_control_retrigger_status(struct uhci_hcd *uhci, struct urb *urb)
703 struct urb_priv *urbp = (struct urb_priv *)urb->hcpriv; 630 struct urb_priv *urbp = (struct urb_priv *)urb->hcpriv;
704 struct uhci_td *td; 631 struct uhci_td *td;
705 632
706 urbp->short_control_packet = 1; 633 urbp->short_transfer = 1;
707 634
708 td = list_entry(urbp->td_list.prev, struct uhci_td, list); 635 td = list_entry(urbp->td_list.prev, struct uhci_td, list);
709 urbp->qh->element = cpu_to_le32(td->dma_handle); 636 urbp->qh->element = cpu_to_le32(td->dma_handle);
@@ -720,16 +647,14 @@ static int uhci_result_control(struct uhci_hcd *uhci, struct urb *urb)
720 unsigned int status; 647 unsigned int status;
721 int ret = 0; 648 int ret = 0;
722 649
723 if (list_empty(&urbp->td_list))
724 return -EINVAL;
725
726 head = &urbp->td_list; 650 head = &urbp->td_list;
727 651 if (urbp->short_transfer) {
728 if (urbp->short_control_packet) {
729 tmp = head->prev; 652 tmp = head->prev;
730 goto status_stage; 653 goto status_stage;
731 } 654 }
732 655
656 urb->actual_length = 0;
657
733 tmp = head->next; 658 tmp = head->next;
734 td = list_entry(tmp, struct uhci_td, list); 659 td = list_entry(tmp, struct uhci_td, list);
735 660
@@ -742,8 +667,6 @@ static int uhci_result_control(struct uhci_hcd *uhci, struct urb *urb)
742 if (status) 667 if (status)
743 goto td_error; 668 goto td_error;
744 669
745 urb->actual_length = 0;
746
747 /* The rest of the TDs (but the last) are data */ 670 /* The rest of the TDs (but the last) are data */
748 tmp = tmp->next; 671 tmp = tmp->next;
749 while (tmp != head && tmp->next != head) { 672 while (tmp != head && tmp->next != head) {
@@ -770,10 +693,7 @@ static int uhci_result_control(struct uhci_hcd *uhci, struct urb *urb)
770 goto err; 693 goto err;
771 } 694 }
772 695
773 if (uhci_packetid(td_token(td)) == USB_PID_IN) 696 return usb_control_retrigger_status(uhci, urb);
774 return usb_control_retrigger_status(uhci, urb);
775 else
776 return 0;
777 } 697 }
778 } 698 }
779 699
@@ -814,34 +734,40 @@ err:
814 if (errbuf) { 734 if (errbuf) {
815 /* Print the chain for debugging purposes */ 735 /* Print the chain for debugging purposes */
816 uhci_show_qh(urbp->qh, errbuf, ERRBUF_LEN, 0); 736 uhci_show_qh(urbp->qh, errbuf, ERRBUF_LEN, 0);
817
818 lprintk(errbuf); 737 lprintk(errbuf);
819 } 738 }
820 } 739 }
821 740
741 /* Note that the queue has stopped */
742 urbp->qh->element = UHCI_PTR_TERM;
743 urbp->qh->is_stopped = 1;
822 return ret; 744 return ret;
823} 745}
824 746
825/* 747/*
826 * Common submit for bulk and interrupt 748 * Common submit for bulk and interrupt
827 */ 749 */
828static int uhci_submit_common(struct uhci_hcd *uhci, struct urb *urb, struct urb *eurb, struct uhci_qh *skelqh) 750static int uhci_submit_common(struct uhci_hcd *uhci, struct urb *urb,
751 struct uhci_qh *qh)
829{ 752{
830 struct uhci_td *td; 753 struct uhci_td *td;
831 struct uhci_qh *qh;
832 unsigned long destination, status; 754 unsigned long destination, status;
833 int maxsze = usb_maxpacket(urb->dev, urb->pipe, usb_pipeout(urb->pipe)); 755 int maxsze = le16_to_cpu(qh->hep->desc.wMaxPacketSize);
834 int len = urb->transfer_buffer_length; 756 int len = urb->transfer_buffer_length;
835 struct urb_priv *urbp = (struct urb_priv *)urb->hcpriv;
836 dma_addr_t data = urb->transfer_dma; 757 dma_addr_t data = urb->transfer_dma;
758 __le32 *plink;
759 unsigned int toggle;
837 760
838 if (len < 0) 761 if (len < 0)
839 return -EINVAL; 762 return -EINVAL;
840 763
841 /* The "pipe" thing contains the destination in bits 8--18 */ 764 /* The "pipe" thing contains the destination in bits 8--18 */
842 destination = (urb->pipe & PIPE_DEVEP_MASK) | usb_packetid(urb->pipe); 765 destination = (urb->pipe & PIPE_DEVEP_MASK) | usb_packetid(urb->pipe);
766 toggle = usb_gettoggle(urb->dev, usb_pipeendpoint(urb->pipe),
767 usb_pipeout(urb->pipe));
843 768
844 status = uhci_maxerr(3) | TD_CTRL_ACTIVE; 769 /* 3 errors, dummy TD remains inactive */
770 status = uhci_maxerr(3);
845 if (urb->dev->speed == USB_SPEED_LOW) 771 if (urb->dev->speed == USB_SPEED_LOW)
846 status |= TD_CTRL_LS; 772 status |= TD_CTRL_LS;
847 if (usb_pipein(urb->pipe)) 773 if (usb_pipein(urb->pipe))
@@ -850,30 +776,34 @@ static int uhci_submit_common(struct uhci_hcd *uhci, struct urb *urb, struct urb
850 /* 776 /*
851 * Build the DATA TDs 777 * Build the DATA TDs
852 */ 778 */
779 plink = NULL;
780 td = qh->dummy_td;
853 do { /* Allow zero length packets */ 781 do { /* Allow zero length packets */
854 int pktsze = maxsze; 782 int pktsze = maxsze;
855 783
856 if (pktsze >= len) { 784 if (len <= pktsze) { /* The last packet */
857 pktsze = len; 785 pktsze = len;
858 if (!(urb->transfer_flags & URB_SHORT_NOT_OK)) 786 if (!(urb->transfer_flags & URB_SHORT_NOT_OK))
859 status &= ~TD_CTRL_SPD; 787 status &= ~TD_CTRL_SPD;
860 } 788 }
861 789
862 td = uhci_alloc_td(uhci); 790 if (plink) {
863 if (!td) 791 td = uhci_alloc_td(uhci);
864 return -ENOMEM; 792 if (!td)
865 793 goto nomem;
794 *plink = cpu_to_le32(td->dma_handle);
795 }
866 uhci_add_td_to_urb(urb, td); 796 uhci_add_td_to_urb(urb, td);
867 uhci_fill_td(td, status, destination | uhci_explen(pktsze) | 797 uhci_fill_td(td, status,
868 (usb_gettoggle(urb->dev, usb_pipeendpoint(urb->pipe), 798 destination | uhci_explen(pktsze) |
869 usb_pipeout(urb->pipe)) << TD_TOKEN_TOGGLE_SHIFT), 799 (toggle << TD_TOKEN_TOGGLE_SHIFT),
870 data); 800 data);
801 plink = &td->link;
802 status |= TD_CTRL_ACTIVE;
871 803
872 data += pktsze; 804 data += pktsze;
873 len -= maxsze; 805 len -= maxsze;
874 806 toggle ^= 1;
875 usb_dotoggle(urb->dev, usb_pipeendpoint(urb->pipe),
876 usb_pipeout(urb->pipe));
877 } while (len > 0); 807 } while (len > 0);
878 808
879 /* 809 /*
@@ -883,20 +813,22 @@ 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 813 * however, if transfer_length == 0, the zero packet was already
884 * prepared above. 814 * prepared above.
885 */ 815 */
886 if (usb_pipeout(urb->pipe) && (urb->transfer_flags & URB_ZERO_PACKET) && 816 if ((urb->transfer_flags & URB_ZERO_PACKET) &&
887 !len && urb->transfer_buffer_length) { 817 usb_pipeout(urb->pipe) && len == 0 &&
818 urb->transfer_buffer_length > 0) {
888 td = uhci_alloc_td(uhci); 819 td = uhci_alloc_td(uhci);
889 if (!td) 820 if (!td)
890 return -ENOMEM; 821 goto nomem;
822 *plink = cpu_to_le32(td->dma_handle);
891 823
892 uhci_add_td_to_urb(urb, td); 824 uhci_add_td_to_urb(urb, td);
893 uhci_fill_td(td, status, destination | uhci_explen(0) | 825 uhci_fill_td(td, status,
894 (usb_gettoggle(urb->dev, usb_pipeendpoint(urb->pipe), 826 destination | uhci_explen(0) |
895 usb_pipeout(urb->pipe)) << TD_TOKEN_TOGGLE_SHIFT), 827 (toggle << TD_TOKEN_TOGGLE_SHIFT),
896 data); 828 data);
829 plink = &td->link;
897 830
898 usb_dotoggle(urb->dev, usb_pipeendpoint(urb->pipe), 831 toggle ^= 1;
899 usb_pipeout(urb->pipe));
900 } 832 }
901 833
902 /* Set the interrupt-on-completion flag on the last packet. 834 /* Set the interrupt-on-completion flag on the last packet.
@@ -905,24 +837,29 @@ 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 837 * 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 838 * more than 2 or 3 URBs, so we will ignore the URB_NO_INTERRUPT
907 * flag setting. */ 839 * flag setting. */
908 td->status |= cpu_to_le32(TD_CTRL_IOC); 840 td->status |= __constant_cpu_to_le32(TD_CTRL_IOC);
909 841
910 qh = uhci_alloc_qh(uhci); 842 /*
911 if (!qh) 843 * Build the new dummy TD and activate the old one
912 return -ENOMEM; 844 */
913 845 td = uhci_alloc_td(uhci);
914 urbp->qh = qh; 846 if (!td)
915 qh->urbp = urbp; 847 goto nomem;
848 *plink = cpu_to_le32(td->dma_handle);
916 849
917 /* Always breadth first */ 850 uhci_fill_td(td, 0, USB_PID_OUT | uhci_explen(0), 0);
918 uhci_insert_tds_in_qh(qh, urb, UHCI_PTR_BREADTH); 851 wmb();
852 qh->dummy_td->status |= __constant_cpu_to_le32(TD_CTRL_ACTIVE);
853 qh->dummy_td = td;
919 854
920 if (eurb) 855 usb_settoggle(urb->dev, usb_pipeendpoint(urb->pipe),
921 uhci_append_queued_urb(uhci, eurb, urb); 856 usb_pipeout(urb->pipe), toggle);
922 else 857 return 0;
923 uhci_insert_qh(uhci, skelqh, urb);
924 858
925 return -EINPROGRESS; 859nomem:
860 /* Remove the dummy TD from the td_list so it doesn't get freed */
861 uhci_remove_td_from_urb(qh->dummy_td);
862 return -ENOMEM;
926} 863}
927 864
928/* 865/*
@@ -954,8 +891,27 @@ static int uhci_result_common(struct uhci_hcd *uhci, struct urb *urb)
954 if (urb->transfer_flags & URB_SHORT_NOT_OK) { 891 if (urb->transfer_flags & URB_SHORT_NOT_OK) {
955 ret = -EREMOTEIO; 892 ret = -EREMOTEIO;
956 goto err; 893 goto err;
957 } else 894 }
958 return 0; 895
896 /*
897 * This URB stopped short of its end. We have to
898 * fix up the toggles of the following URBs on the
899 * queue and restart the queue.
900 *
901 * Do this only the first time we encounter the
902 * short URB.
903 */
904 if (!urbp->short_transfer) {
905 urbp->short_transfer = 1;
906 urbp->qh->initial_toggle =
907 uhci_toggle(td_token(td)) ^ 1;
908 uhci_fixup_toggles(urbp->qh, 1);
909
910 td = list_entry(urbp->td_list.prev,
911 struct uhci_td, list);
912 urbp->qh->element = td->link;
913 }
914 break;
959 } 915 }
960 } 916 }
961 917
@@ -964,31 +920,30 @@ static int uhci_result_common(struct uhci_hcd *uhci, struct urb *urb)
964td_error: 920td_error:
965 ret = uhci_map_status(status, uhci_packetout(td_token(td))); 921 ret = uhci_map_status(status, uhci_packetout(td_token(td)));
966 922
967err:
968 /*
969 * Enable this chunk of code if you want to see some more debugging.
970 * But be careful, it has the tendancy to starve out khubd and prevent
971 * disconnects from happening successfully if you have a slow debug
972 * log interface (like a serial console.
973 */
974#if 0
975 if ((debug == 1 && ret != -EPIPE) || debug > 1) { 923 if ((debug == 1 && ret != -EPIPE) || debug > 1) {
976 /* Some debugging code */ 924 /* Some debugging code */
977 dev_dbg(uhci_dev(uhci), "%s: failed with status %x\n", 925 dev_dbg(uhci_dev(uhci), "%s: failed with status %x\n",
978 __FUNCTION__, status); 926 __FUNCTION__, status);
979 927
980 if (errbuf) { 928 if (debug > 1 && errbuf) {
981 /* Print the chain for debugging purposes */ 929 /* Print the chain for debugging purposes */
982 uhci_show_qh(urbp->qh, errbuf, ERRBUF_LEN, 0); 930 uhci_show_qh(urbp->qh, errbuf, ERRBUF_LEN, 0);
983
984 lprintk(errbuf); 931 lprintk(errbuf);
985 } 932 }
986 } 933 }
987#endif 934err:
935
936 /* Note that the queue has stopped and save the next toggle value */
937 urbp->qh->element = UHCI_PTR_TERM;
938 urbp->qh->is_stopped = 1;
939 urbp->qh->needs_fixup = 1;
940 urbp->qh->initial_toggle = uhci_toggle(td_token(td)) ^
941 (ret == -EREMOTEIO);
988 return ret; 942 return ret;
989} 943}
990 944
991static inline int uhci_submit_bulk(struct uhci_hcd *uhci, struct urb *urb, struct urb *eurb) 945static inline int uhci_submit_bulk(struct uhci_hcd *uhci, struct urb *urb,
946 struct uhci_qh *qh)
992{ 947{
993 int ret; 948 int ret;
994 949
@@ -996,95 +951,60 @@ static inline int uhci_submit_bulk(struct uhci_hcd *uhci, struct urb *urb, struc
996 if (urb->dev->speed == USB_SPEED_LOW) 951 if (urb->dev->speed == USB_SPEED_LOW)
997 return -EINVAL; 952 return -EINVAL;
998 953
999 ret = uhci_submit_common(uhci, urb, eurb, uhci->skel_bulk_qh); 954 qh->skel = uhci->skel_bulk_qh;
1000 if (ret == -EINPROGRESS) 955 ret = uhci_submit_common(uhci, urb, qh);
956 if (ret == 0)
1001 uhci_inc_fsbr(uhci, urb); 957 uhci_inc_fsbr(uhci, urb);
1002
1003 return ret; 958 return ret;
1004} 959}
1005 960
1006static inline int uhci_submit_interrupt(struct uhci_hcd *uhci, struct urb *urb, struct urb *eurb) 961static inline int uhci_submit_interrupt(struct uhci_hcd *uhci, struct urb *urb,
962 struct uhci_qh *qh)
1007{ 963{
1008 /* USB 1.1 interrupt transfers only involve one packet per interval; 964 /* USB 1.1 interrupt transfers only involve one packet per interval.
1009 * that's the uhci_submit_common() "breadth first" policy. Drivers 965 * 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 966 * multiple intervals to complete.
1011 * intervals to complete.
1012 */ 967 */
1013 return uhci_submit_common(uhci, urb, eurb, uhci->skelqh[__interval_to_skel(urb->interval)]); 968 qh->skel = uhci->skelqh[__interval_to_skel(urb->interval)];
969 return uhci_submit_common(uhci, urb, qh);
1014} 970}
1015 971
1016/* 972/*
1017 * Isochronous transfers 973 * Isochronous transfers
1018 */ 974 */
1019static int isochronous_find_limits(struct uhci_hcd *uhci, struct urb *urb, unsigned int *start, unsigned int *end) 975static int uhci_submit_isochronous(struct uhci_hcd *uhci, struct urb *urb,
1020{ 976 struct uhci_qh *qh)
1021 struct urb *last_urb = NULL;
1022 struct urb_priv *up;
1023 int ret = 0;
1024
1025 list_for_each_entry(up, &uhci->urb_list, urb_list) {
1026 struct urb *u = up->urb;
1027
1028 /* look for pending URBs with identical pipe handle */
1029 if ((urb->pipe == u->pipe) && (urb->dev == u->dev) &&
1030 (u->status == -EINPROGRESS) && (u != urb)) {
1031 if (!last_urb)
1032 *start = u->start_frame;
1033 last_urb = u;
1034 }
1035 }
1036
1037 if (last_urb) {
1038 *end = (last_urb->start_frame + last_urb->number_of_packets *
1039 last_urb->interval) & (UHCI_NUMFRAMES-1);
1040 ret = 0;
1041 } else
1042 ret = -1; /* no previous urb found */
1043
1044 return ret;
1045}
1046
1047static int isochronous_find_start(struct uhci_hcd *uhci, struct urb *urb)
1048{ 977{
1049 int limits; 978 struct uhci_td *td = NULL; /* Since urb->number_of_packets > 0 */
1050 unsigned int start = 0, end = 0; 979 int i, frame;
980 unsigned long destination, status;
981 struct urb_priv *urbp = (struct urb_priv *) urb->hcpriv;
1051 982
1052 if (urb->number_of_packets > 900) /* 900? Why? */ 983 if (urb->number_of_packets > 900) /* 900? Why? */
1053 return -EFBIG; 984 return -EFBIG;
1054 985
1055 limits = isochronous_find_limits(uhci, urb, &start, &end); 986 status = TD_CTRL_ACTIVE | TD_CTRL_IOS;
987 destination = (urb->pipe & PIPE_DEVEP_MASK) | usb_packetid(urb->pipe);
1056 988
989 /* Figure out the starting frame number */
1057 if (urb->transfer_flags & URB_ISO_ASAP) { 990 if (urb->transfer_flags & URB_ISO_ASAP) {
1058 if (limits) { 991 if (list_empty(&qh->queue)) {
1059 uhci_get_current_frame_number(uhci); 992 uhci_get_current_frame_number(uhci);
1060 urb->start_frame = (uhci->frame_number + 10) 993 urb->start_frame = (uhci->frame_number + 10);
1061 & (UHCI_NUMFRAMES - 1); 994
1062 } else 995 } else { /* Go right after the last one */
1063 urb->start_frame = end; 996 struct urb *last_urb;
997
998 last_urb = list_entry(qh->queue.prev,
999 struct urb_priv, node)->urb;
1000 urb->start_frame = (last_urb->start_frame +
1001 last_urb->number_of_packets *
1002 last_urb->interval);
1003 }
1064 } else { 1004 } else {
1065 urb->start_frame &= (UHCI_NUMFRAMES - 1);
1066 /* FIXME: Sanity check */ 1005 /* FIXME: Sanity check */
1067 } 1006 }
1068 1007 urb->start_frame &= (UHCI_NUMFRAMES - 1);
1069 return 0;
1070}
1071
1072/*
1073 * Isochronous transfers
1074 */
1075static int uhci_submit_isochronous(struct uhci_hcd *uhci, struct urb *urb)
1076{
1077 struct uhci_td *td;
1078 int i, ret, frame;
1079 int status, destination;
1080 struct urb_priv *urbp = (struct urb_priv *) urb->hcpriv;
1081
1082 status = TD_CTRL_ACTIVE | TD_CTRL_IOS;
1083 destination = (urb->pipe & PIPE_DEVEP_MASK) | usb_packetid(urb->pipe);
1084
1085 ret = isochronous_find_start(uhci, urb);
1086 if (ret)
1087 return ret;
1088 1008
1089 for (i = 0; i < urb->number_of_packets; i++) { 1009 for (i = 0; i < urb->number_of_packets; i++) {
1090 td = uhci_alloc_td(uhci); 1010 td = uhci_alloc_td(uhci);
@@ -1092,20 +1012,25 @@ static int uhci_submit_isochronous(struct uhci_hcd *uhci, struct urb *urb)
1092 return -ENOMEM; 1012 return -ENOMEM;
1093 1013
1094 uhci_add_td_to_urb(urb, td); 1014 uhci_add_td_to_urb(urb, td);
1095 uhci_fill_td(td, status, destination | uhci_explen(urb->iso_frame_desc[i].length), 1015 uhci_fill_td(td, status, destination |
1096 urb->transfer_dma + urb->iso_frame_desc[i].offset); 1016 uhci_explen(urb->iso_frame_desc[i].length),
1097 1017 urb->transfer_dma +
1098 if (i + 1 >= urb->number_of_packets) 1018 urb->iso_frame_desc[i].offset);
1099 td->status |= cpu_to_le32(TD_CTRL_IOC);
1100 } 1019 }
1101 1020
1021 /* Set the interrupt-on-completion flag on the last packet. */
1022 td->status |= __constant_cpu_to_le32(TD_CTRL_IOC);
1023
1024 qh->skel = uhci->skel_iso_qh;
1025
1026 /* Add the TDs to the frame list */
1102 frame = urb->start_frame; 1027 frame = urb->start_frame;
1103 list_for_each_entry(td, &urbp->td_list, list) { 1028 list_for_each_entry(td, &urbp->td_list, list) {
1104 uhci_insert_td_frame_list(uhci, td, frame); 1029 uhci_insert_td_in_frame_list(uhci, td, frame);
1105 frame += urb->interval; 1030 frame += urb->interval;
1106 } 1031 }
1107 1032
1108 return -EINPROGRESS; 1033 return 0;
1109} 1034}
1110 1035
1111static int uhci_result_isochronous(struct uhci_hcd *uhci, struct urb *urb) 1036static int uhci_result_isochronous(struct uhci_hcd *uhci, struct urb *urb)
@@ -1139,80 +1064,67 @@ static int uhci_result_isochronous(struct uhci_hcd *uhci, struct urb *urb)
1139 1064
1140 i++; 1065 i++;
1141 } 1066 }
1142 unlink_isochronous_tds(uhci, urb);
1143 1067
1144 return ret; 1068 return ret;
1145} 1069}
1146 1070
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, 1071static int uhci_urb_enqueue(struct usb_hcd *hcd,
1172 struct usb_host_endpoint *ep, 1072 struct usb_host_endpoint *hep,
1173 struct urb *urb, gfp_t mem_flags) 1073 struct urb *urb, gfp_t mem_flags)
1174{ 1074{
1175 int ret; 1075 int ret;
1176 struct uhci_hcd *uhci = hcd_to_uhci(hcd); 1076 struct uhci_hcd *uhci = hcd_to_uhci(hcd);
1177 unsigned long flags; 1077 unsigned long flags;
1178 struct urb *eurb; 1078 struct urb_priv *urbp;
1079 struct uhci_qh *qh;
1179 int bustime; 1080 int bustime;
1180 1081
1181 spin_lock_irqsave(&uhci->lock, flags); 1082 spin_lock_irqsave(&uhci->lock, flags);
1182 1083
1183 ret = urb->status; 1084 ret = urb->status;
1184 if (ret != -EINPROGRESS) /* URB already unlinked! */ 1085 if (ret != -EINPROGRESS) /* URB already unlinked! */
1185 goto out; 1086 goto done;
1186 1087
1187 eurb = uhci_find_urb_ep(uhci, urb); 1088 ret = -ENOMEM;
1089 urbp = uhci_alloc_urb_priv(uhci, urb);
1090 if (!urbp)
1091 goto done;
1188 1092
1189 if (!uhci_alloc_urb_priv(uhci, urb)) { 1093 if (hep->hcpriv)
1190 ret = -ENOMEM; 1094 qh = (struct uhci_qh *) hep->hcpriv;
1191 goto out; 1095 else {
1096 qh = uhci_alloc_qh(uhci, urb->dev, hep);
1097 if (!qh)
1098 goto err_no_qh;
1192 } 1099 }
1100 urbp->qh = qh;
1193 1101
1194 switch (usb_pipetype(urb->pipe)) { 1102 switch (usb_pipetype(urb->pipe)) {
1195 case PIPE_CONTROL: 1103 case PIPE_CONTROL:
1196 ret = uhci_submit_control(uhci, urb, eurb); 1104 ret = uhci_submit_control(uhci, urb, qh);
1105 break;
1106 case PIPE_BULK:
1107 ret = uhci_submit_bulk(uhci, urb, qh);
1197 break; 1108 break;
1198 case PIPE_INTERRUPT: 1109 case PIPE_INTERRUPT:
1199 if (!eurb) { 1110 if (list_empty(&qh->queue)) {
1200 bustime = usb_check_bandwidth(urb->dev, urb); 1111 bustime = usb_check_bandwidth(urb->dev, urb);
1201 if (bustime < 0) 1112 if (bustime < 0)
1202 ret = bustime; 1113 ret = bustime;
1203 else { 1114 else {
1204 ret = uhci_submit_interrupt(uhci, urb, eurb); 1115 ret = uhci_submit_interrupt(uhci, urb, qh);
1205 if (ret == -EINPROGRESS) 1116 if (ret == 0)
1206 usb_claim_bandwidth(urb->dev, urb, bustime, 0); 1117 usb_claim_bandwidth(urb->dev, urb, bustime, 0);
1207 } 1118 }
1208 } else { /* inherit from parent */ 1119 } else { /* inherit from parent */
1209 urb->bandwidth = eurb->bandwidth; 1120 struct urb_priv *eurbp;
1210 ret = uhci_submit_interrupt(uhci, urb, eurb); 1121
1122 eurbp = list_entry(qh->queue.prev, struct urb_priv,
1123 node);
1124 urb->bandwidth = eurbp->urb->bandwidth;
1125 ret = uhci_submit_interrupt(uhci, urb, qh);
1211 } 1126 }
1212 break; 1127 break;
1213 case PIPE_BULK:
1214 ret = uhci_submit_bulk(uhci, urb, eurb);
1215 break;
1216 case PIPE_ISOCHRONOUS: 1128 case PIPE_ISOCHRONOUS:
1217 bustime = usb_check_bandwidth(urb->dev, urb); 1129 bustime = usb_check_bandwidth(urb->dev, urb);
1218 if (bustime < 0) { 1130 if (bustime < 0) {
@@ -1220,221 +1132,208 @@ static int uhci_urb_enqueue(struct usb_hcd *hcd,
1220 break; 1132 break;
1221 } 1133 }
1222 1134
1223 ret = uhci_submit_isochronous(uhci, urb); 1135 ret = uhci_submit_isochronous(uhci, urb, qh);
1224 if (ret == -EINPROGRESS) 1136 if (ret == 0)
1225 usb_claim_bandwidth(urb->dev, urb, bustime, 1); 1137 usb_claim_bandwidth(urb->dev, urb, bustime, 1);
1226 break; 1138 break;
1227 } 1139 }
1140 if (ret != 0)
1141 goto err_submit_failed;
1142
1143 /* Add this URB to the QH */
1144 urbp->qh = qh;
1145 list_add_tail(&urbp->node, &qh->queue);
1228 1146
1229 if (ret != -EINPROGRESS) { 1147 /* If the new URB is the first and only one on this QH then either
1230 /* Submit failed, so delete it from the urb_list */ 1148 * the QH is new and idle or else it's unlinked and waiting to
1231 struct urb_priv *urbp = urb->hcpriv; 1149 * become idle, so we can activate it right away. */
1150 if (qh->queue.next == &urbp->node)
1151 uhci_activate_qh(uhci, qh);
1152 goto done;
1232 1153
1233 list_del_init(&urbp->urb_list); 1154err_submit_failed:
1234 uhci_destroy_urb_priv(uhci, urb); 1155 if (qh->state == QH_STATE_IDLE)
1235 } else 1156 uhci_make_qh_idle(uhci, qh); /* Reclaim unused QH */
1236 ret = 0;
1237 1157
1238out: 1158err_no_qh:
1159 uhci_free_urb_priv(uhci, urbp);
1160
1161done:
1239 spin_unlock_irqrestore(&uhci->lock, flags); 1162 spin_unlock_irqrestore(&uhci->lock, flags);
1240 return ret; 1163 return ret;
1241} 1164}
1242 1165
1243/* 1166static int uhci_urb_dequeue(struct usb_hcd *hcd, struct urb *urb)
1244 * Return the result of a transfer
1245 */
1246static void uhci_transfer_result(struct uhci_hcd *uhci, struct urb *urb)
1247{ 1167{
1248 int ret = -EINPROGRESS; 1168 struct uhci_hcd *uhci = hcd_to_uhci(hcd);
1169 unsigned long flags;
1249 struct urb_priv *urbp; 1170 struct urb_priv *urbp;
1250 1171
1251 spin_lock(&urb->lock); 1172 spin_lock_irqsave(&uhci->lock, flags);
1173 urbp = urb->hcpriv;
1174 if (!urbp) /* URB was never linked! */
1175 goto done;
1176
1177 /* Remove Isochronous TDs from the frame list ASAP */
1178 if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS)
1179 uhci_unlink_isochronous_tds(uhci, urb);
1180 uhci_unlink_qh(uhci, urbp->qh);
1181
1182done:
1183 spin_unlock_irqrestore(&uhci->lock, flags);
1184 return 0;
1185}
1252 1186
1253 urbp = (struct urb_priv *)urb->hcpriv; 1187/*
1188 * Finish unlinking an URB and give it back
1189 */
1190static void uhci_giveback_urb(struct uhci_hcd *uhci, struct uhci_qh *qh,
1191 struct urb *urb, struct pt_regs *regs)
1192__releases(uhci->lock)
1193__acquires(uhci->lock)
1194{
1195 struct urb_priv *urbp = (struct urb_priv *) urb->hcpriv;
1254 1196
1255 if (urb->status != -EINPROGRESS) /* URB already dequeued */ 1197 /* Isochronous TDs get unlinked directly from the frame list */
1256 goto out; 1198 if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS)
1199 uhci_unlink_isochronous_tds(uhci, urb);
1200
1201 /* If the URB isn't first on its queue, adjust the link pointer
1202 * of the last TD in the previous URB. */
1203 else if (qh->queue.next != &urbp->node) {
1204 struct urb_priv *purbp;
1205 struct uhci_td *ptd, *ltd;
1206
1207 purbp = list_entry(urbp->node.prev, struct urb_priv, node);
1208 ptd = list_entry(purbp->td_list.prev, struct uhci_td,
1209 list);
1210 ltd = list_entry(urbp->td_list.prev, struct uhci_td,
1211 list);
1212 ptd->link = ltd->link;
1213 }
1257 1214
1258 switch (usb_pipetype(urb->pipe)) { 1215 /* Take the URB off the QH's queue. If the queue is now empty,
1259 case PIPE_CONTROL: 1216 * this is a perfect time for a toggle fixup. */
1260 ret = uhci_result_control(uhci, urb); 1217 list_del_init(&urbp->node);
1261 break; 1218 if (list_empty(&qh->queue) && qh->needs_fixup) {
1262 case PIPE_BULK: 1219 usb_settoggle(urb->dev, usb_pipeendpoint(urb->pipe),
1263 case PIPE_INTERRUPT: 1220 usb_pipeout(urb->pipe), qh->initial_toggle);
1264 ret = uhci_result_common(uhci, urb); 1221 qh->needs_fixup = 0;
1265 break;
1266 case PIPE_ISOCHRONOUS:
1267 ret = uhci_result_isochronous(uhci, urb);
1268 break;
1269 } 1222 }
1270 1223
1271 if (ret == -EINPROGRESS) 1224 uhci_dec_fsbr(uhci, urb); /* Safe since it checks */
1272 goto out; 1225 uhci_free_urb_priv(uhci, urbp);
1273 urb->status = ret;
1274 1226
1275 switch (usb_pipetype(urb->pipe)) { 1227 switch (usb_pipetype(urb->pipe)) {
1276 case PIPE_CONTROL:
1277 case PIPE_BULK:
1278 case PIPE_ISOCHRONOUS: 1228 case PIPE_ISOCHRONOUS:
1279 /* Release bandwidth for Interrupt or Isoc. transfers */ 1229 /* Release bandwidth for Interrupt or Isoc. transfers */
1280 if (urb->bandwidth) 1230 if (urb->bandwidth)
1281 usb_release_bandwidth(urb->dev, urb, 1); 1231 usb_release_bandwidth(urb->dev, urb, 1);
1282 uhci_unlink_generic(uhci, urb);
1283 break; 1232 break;
1284 case PIPE_INTERRUPT: 1233 case PIPE_INTERRUPT:
1285 /* Release bandwidth for Interrupt or Isoc. transfers */ 1234 /* Release bandwidth for Interrupt or Isoc. transfers */
1286 /* Make sure we don't release if we have a queued URB */ 1235 /* Make sure we don't release if we have a queued URB */
1287 if (list_empty(&urbp->queue_list) && urb->bandwidth) 1236 if (list_empty(&qh->queue) && urb->bandwidth)
1288 usb_release_bandwidth(urb->dev, urb, 0); 1237 usb_release_bandwidth(urb->dev, urb, 0);
1289 else 1238 else
1290 /* bandwidth was passed on to queued URB, */ 1239 /* bandwidth was passed on to queued URB, */
1291 /* so don't let usb_unlink_urb() release it */ 1240 /* so don't let usb_unlink_urb() release it */
1292 urb->bandwidth = 0; 1241 urb->bandwidth = 0;
1293 uhci_unlink_generic(uhci, urb);
1294 break; 1242 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 } 1243 }
1300 1244
1301 /* Move it from uhci->urb_list to uhci->complete_list */ 1245 spin_unlock(&uhci->lock);
1302 uhci_moveto_complete(uhci, urbp); 1246 usb_hcd_giveback_urb(uhci_to_hcd(uhci), urb, regs);
1303 1247 spin_lock(&uhci->lock);
1304out:
1305 spin_unlock(&urb->lock);
1306}
1307
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
1315 uhci_dec_fsbr(uhci, urb); /* Safe since it checks */
1316 1248
1317 /* 1249 /* If the queue is now empty, we can unlink the QH and give up its
1318 * Now we need to find out what the last successful toggle was 1250 * reserved bandwidth. */
1319 * so we can update the local data toggle for the next transfer 1251 if (list_empty(&qh->queue)) {
1320 * 1252 uhci_unlink_qh(uhci, qh);
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 1253
1341 if (!(ctrlstat & TD_CTRL_ACTIVE) && 1254 /* Bandwidth stuff not yet implemented */
1342 (uhci_actual_length(ctrlstat) <
1343 uhci_expected_length(td_token(td)) ||
1344 td->list.next == head))
1345 usb_settoggle(urb->dev, uhci_endpoint(td_token(td)),
1346 uhci_packetout(td_token(td)),
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
1353 prevactive = ctrlstat & TD_CTRL_ACTIVE;
1354 } 1255 }
1355
1356 uhci_delete_queued_urb(uhci, urb);
1357
1358 /* The interrupt loop will reclaim the QHs */
1359 uhci_remove_qh(uhci, urbp->qh);
1360 urbp->qh = NULL;
1361} 1256}
1362 1257
1363static int uhci_urb_dequeue(struct usb_hcd *hcd, struct urb *urb) 1258/*
1259 * Scan the URBs in a QH's queue
1260 */
1261#define QH_FINISHED_UNLINKING(qh) \
1262 (qh->state == QH_STATE_UNLINKING && \
1263 uhci->frame_number + uhci->is_stopped != qh->unlink_frame)
1264
1265static void uhci_scan_qh(struct uhci_hcd *uhci, struct uhci_qh *qh,
1266 struct pt_regs *regs)
1364{ 1267{
1365 struct uhci_hcd *uhci = hcd_to_uhci(hcd);
1366 unsigned long flags;
1367 struct urb_priv *urbp; 1268 struct urb_priv *urbp;
1269 struct urb *urb;
1270 int status;
1368 1271
1369 spin_lock_irqsave(&uhci->lock, flags); 1272 while (!list_empty(&qh->queue)) {
1370 urbp = urb->hcpriv; 1273 urbp = list_entry(qh->queue.next, struct urb_priv, node);
1371 if (!urbp) /* URB was never linked! */ 1274 urb = urbp->urb;
1372 goto done;
1373 list_del_init(&urbp->urb_list);
1374
1375 if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS)
1376 unlink_isochronous_tds(uhci, urb);
1377 uhci_unlink_generic(uhci, urb);
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
1390done:
1391 spin_unlock_irqrestore(&uhci->lock, flags);
1392 return 0;
1393}
1394 1275
1395static int uhci_fsbr_timeout(struct uhci_hcd *uhci, struct urb *urb) 1276 switch (usb_pipetype(urb->pipe)) {
1396{ 1277 case PIPE_CONTROL:
1397 struct urb_priv *urbp = (struct urb_priv *)urb->hcpriv; 1278 status = uhci_result_control(uhci, urb);
1398 struct list_head *head; 1279 break;
1399 struct uhci_td *td; 1280 case PIPE_ISOCHRONOUS:
1400 int count = 0; 1281 status = uhci_result_isochronous(uhci, urb);
1401 1282 break;
1402 uhci_dec_fsbr(uhci, urb); 1283 default: /* PIPE_BULK or PIPE_INTERRUPT */
1284 status = uhci_result_common(uhci, urb);
1285 break;
1286 }
1287 if (status == -EINPROGRESS)
1288 break;
1403 1289
1404 urbp->fsbr_timeout = 1; 1290 spin_lock(&urb->lock);
1291 if (urb->status == -EINPROGRESS) /* Not dequeued */
1292 urb->status = status;
1293 else
1294 status = -ECONNRESET;
1295 spin_unlock(&urb->lock);
1405 1296
1406 /* 1297 /* Dequeued but completed URBs can't be given back unless
1407 * Ideally we would want to fix qh->element as well, but it's 1298 * the QH is stopped or has finished unlinking. */
1408 * read/write by the HC, so that can introduce a race. It's not 1299 if (status == -ECONNRESET &&
1409 * really worth the hassle 1300 !(qh->is_stopped || QH_FINISHED_UNLINKING(qh)))
1410 */ 1301 return;
1411 1302
1412 head = &urbp->td_list; 1303 uhci_giveback_urb(uhci, qh, urb, regs);
1413 list_for_each_entry(td, head, list) { 1304 if (qh->is_stopped)
1414 /* 1305 break;
1415 * Make sure we don't do the last one (since it'll have the
1416 * TERM bit set) as well as we skip every so many TDs to
1417 * make sure it doesn't hog the bandwidth
1418 */
1419 if (td->list.next != head && (count % DEPTH_INTERVAL) ==
1420 (DEPTH_INTERVAL - 1))
1421 td->link |= UHCI_PTR_DEPTH;
1422
1423 count++;
1424 } 1306 }
1425 1307
1426 return 0; 1308 /* If the QH is neither stopped nor finished unlinking (normal case),
1427} 1309 * our work here is done. */
1428 1310 restart:
1429static void uhci_free_pending_qhs(struct uhci_hcd *uhci) 1311 if (!(qh->is_stopped || QH_FINISHED_UNLINKING(qh)))
1430{ 1312 return;
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 1313
1436 uhci_free_qh(uhci, qh); 1314 /* Otherwise give back each of the dequeued URBs */
1315 list_for_each_entry(urbp, &qh->queue, node) {
1316 urb = urbp->urb;
1317 if (urb->status != -EINPROGRESS) {
1318 uhci_save_toggle(qh, urb);
1319 uhci_giveback_urb(uhci, qh, urb, regs);
1320 goto restart;
1321 }
1322 }
1323 qh->is_stopped = 0;
1324
1325 /* There are no more dequeued URBs. If there are still URBs on the
1326 * queue, the QH can now be re-activated. */
1327 if (!list_empty(&qh->queue)) {
1328 if (qh->needs_fixup)
1329 uhci_fixup_toggles(qh, 0);
1330 uhci_activate_qh(uhci, qh);
1437 } 1331 }
1332
1333 /* The queue is empty. The QH can become idle if it is fully
1334 * unlinked. */
1335 else if (QH_FINISHED_UNLINKING(qh))
1336 uhci_make_qh_idle(uhci, qh);
1438} 1337}
1439 1338
1440static void uhci_free_pending_tds(struct uhci_hcd *uhci) 1339static void uhci_free_pending_tds(struct uhci_hcd *uhci)
@@ -1448,43 +1347,13 @@ static void uhci_free_pending_tds(struct uhci_hcd *uhci)
1448 } 1347 }
1449} 1348}
1450 1349
1451static void 1350/*
1452uhci_finish_urb(struct usb_hcd *hcd, struct urb *urb, struct pt_regs *regs) 1351 * Process events in the schedule, but only in one thread at a time
1453__releases(uhci->lock) 1352 */
1454__acquires(uhci->lock)
1455{
1456 struct uhci_hcd *uhci = hcd_to_uhci(hcd);
1457
1458 uhci_destroy_urb_priv(uhci, urb);
1459
1460 spin_unlock(&uhci->lock);
1461 usb_hcd_giveback_urb(hcd, urb, regs);
1462 spin_lock(&uhci->lock);
1463}
1464
1465static void uhci_finish_completion(struct uhci_hcd *uhci, struct pt_regs *regs)
1466{
1467 struct urb_priv *urbp, *tmp;
1468
1469 list_for_each_entry_safe(urbp, tmp, &uhci->complete_list, urb_list) {
1470 struct urb *urb = urbp->urb;
1471
1472 list_del_init(&urbp->urb_list);
1473 uhci_finish_urb(uhci_to_hcd(uhci), urb, regs);
1474 }
1475}
1476
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 */
1485static void uhci_scan_schedule(struct uhci_hcd *uhci, struct pt_regs *regs) 1353static void uhci_scan_schedule(struct uhci_hcd *uhci, struct pt_regs *regs)
1486{ 1354{
1487 struct urb_priv *urbp, *tmp; 1355 int i;
1356 struct uhci_qh *qh;
1488 1357
1489 /* Don't allow re-entrant calls */ 1358 /* Don't allow re-entrant calls */
1490 if (uhci->scan_in_progress) { 1359 if (uhci->scan_in_progress) {
@@ -1498,60 +1367,39 @@ static void uhci_scan_schedule(struct uhci_hcd *uhci, struct pt_regs *regs)
1498 uhci_clear_next_interrupt(uhci); 1367 uhci_clear_next_interrupt(uhci);
1499 uhci_get_current_frame_number(uhci); 1368 uhci_get_current_frame_number(uhci);
1500 1369
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) 1370 if (uhci->frame_number + uhci->is_stopped != uhci->td_remove_age)
1504 uhci_free_pending_tds(uhci); 1371 uhci_free_pending_tds(uhci);
1505 if (uhci->frame_number + uhci->is_stopped != uhci->urb_remove_age)
1506 uhci_remove_pending_urbps(uhci);
1507
1508 /* Walk the list of pending URBs to see which ones completed
1509 * (must be _safe because uhci_transfer_result() dequeues URBs) */
1510 list_for_each_entry_safe(urbp, tmp, &uhci->urb_list, urb_list) {
1511 struct urb *urb = urbp->urb;
1512 1372
1513 /* Checks the status and does all of the magic necessary */ 1373 /* Go through all the QH queues and process the URBs in each one */
1514 uhci_transfer_result(uhci, urb); 1374 for (i = 0; i < UHCI_NUM_SKELQH - 1; ++i) {
1515 } 1375 uhci->next_qh = list_entry(uhci->skelqh[i]->node.next,
1516 uhci_finish_completion(uhci, regs); 1376 struct uhci_qh, node);
1517 1377 while ((qh = uhci->next_qh) != uhci->skelqh[i]) {
1518 /* If the controller is stopped, we can finish these off right now */ 1378 uhci->next_qh = list_entry(qh->node.next,
1519 if (uhci->is_stopped) { 1379 struct uhci_qh, node);
1520 uhci_free_pending_qhs(uhci); 1380 uhci_scan_qh(uhci, qh, regs);
1521 uhci_free_pending_tds(uhci); 1381 }
1522 uhci_remove_pending_urbps(uhci);
1523 } 1382 }
1524 1383
1525 if (uhci->need_rescan) 1384 if (uhci->need_rescan)
1526 goto rescan; 1385 goto rescan;
1527 uhci->scan_in_progress = 0; 1386 uhci->scan_in_progress = 0;
1528 1387
1529 if (list_empty(&uhci->urb_remove_list) && 1388 /* If the controller is stopped, we can finish these off right now */
1530 list_empty(&uhci->td_remove_list) && 1389 if (uhci->is_stopped)
1531 list_empty(&uhci->qh_remove_list)) 1390 uhci_free_pending_tds(uhci);
1391
1392 if (list_empty(&uhci->td_remove_list) &&
1393 list_empty(&uhci->skel_unlink_qh->node))
1532 uhci_clear_next_interrupt(uhci); 1394 uhci_clear_next_interrupt(uhci);
1533 else 1395 else
1534 uhci_set_next_interrupt(uhci); 1396 uhci_set_next_interrupt(uhci);
1535
1536 /* Wake up anyone waiting for an URB to complete */
1537 wake_up_all(&uhci->waitqh);
1538} 1397}
1539 1398
1540static void check_fsbr(struct uhci_hcd *uhci) 1399static void check_fsbr(struct uhci_hcd *uhci)
1541{ 1400{
1542 struct urb_priv *up; 1401 /* For now, don't scan URBs for FSBR timeouts.
1543 1402 * Add it back in later... */
1544 list_for_each_entry(up, &uhci->urb_list, urb_list) {
1545 struct urb *u = up->urb;
1546
1547 spin_lock(&u->lock);
1548
1549 /* Check if the FSBR timed out */
1550 if (up->fsbr && !up->fsbr_timeout && time_after_eq(jiffies, up->fsbrtime + IDLE_TIMEOUT))
1551 uhci_fsbr_timeout(uhci, u);
1552
1553 spin_unlock(&u->lock);
1554 }
1555 1403
1556 /* Really disable FSBR */ 1404 /* Really disable FSBR */
1557 if (!uhci->fsbr && uhci->fsbrtimeout && time_after_eq(jiffies, uhci->fsbrtimeout)) { 1405 if (!uhci->fsbr && uhci->fsbrtimeout && time_after_eq(jiffies, uhci->fsbrtimeout)) {