[PATCH] UHCI: Work around old Intel bug
[linux-2.6] / drivers / usb / host / uhci-q.c
1 /*
2  * Universal Host Controller Interface driver for USB.
3  *
4  * Maintainer: Alan Stern <stern@rowland.harvard.edu>
5  *
6  * (C) Copyright 1999 Linus Torvalds
7  * (C) Copyright 1999-2002 Johannes Erdfelt, johannes@erdfelt.com
8  * (C) Copyright 1999 Randy Dunlap
9  * (C) Copyright 1999 Georg Acher, acher@in.tum.de
10  * (C) Copyright 1999 Deti Fliegl, deti@fliegl.de
11  * (C) Copyright 1999 Thomas Sailer, sailer@ife.ee.ethz.ch
12  * (C) Copyright 1999 Roman Weissgaerber, weissg@vienna.at
13  * (C) Copyright 2000 Yggdrasil Computing, Inc. (port of new PCI interface
14  *               support from usb-ohci.c by Adam Richter, adam@yggdrasil.com).
15  * (C) Copyright 1999 Gregory P. Smith (from usb-ohci.c)
16  * (C) Copyright 2004-2006 Alan Stern, stern@rowland.harvard.edu
17  */
18
19
20 /*
21  * Technically, updating td->status here is a race, but it's not really a
22  * problem. The worst that can happen is that we set the IOC bit again
23  * generating a spurious interrupt. We could fix this by creating another
24  * QH and leaving the IOC bit always set, but then we would have to play
25  * games with the FSBR code to make sure we get the correct order in all
26  * the cases. I don't think it's worth the effort
27  */
28 static void uhci_set_next_interrupt(struct uhci_hcd *uhci)
29 {
30         if (uhci->is_stopped)
31                 mod_timer(&uhci_to_hcd(uhci)->rh_timer, jiffies);
32         uhci->term_td->status |= cpu_to_le32(TD_CTRL_IOC); 
33 }
34
35 static inline void uhci_clear_next_interrupt(struct uhci_hcd *uhci)
36 {
37         uhci->term_td->status &= ~cpu_to_le32(TD_CTRL_IOC);
38 }
39
40
41 /*
42  * Full-Speed Bandwidth Reclamation (FSBR).
43  * We turn on FSBR whenever a queue that wants it is advancing,
44  * and leave it on for a short time thereafter.
45  */
46 static void uhci_fsbr_on(struct uhci_hcd *uhci)
47 {
48         uhci->fsbr_is_on = 1;
49         uhci->skel_term_qh->link = cpu_to_le32(
50                         uhci->skel_fs_control_qh->dma_handle) | UHCI_PTR_QH;
51 }
52
53 static void uhci_fsbr_off(struct uhci_hcd *uhci)
54 {
55         uhci->fsbr_is_on = 0;
56         uhci->skel_term_qh->link = UHCI_PTR_TERM;
57 }
58
59 static void uhci_add_fsbr(struct uhci_hcd *uhci, struct urb *urb)
60 {
61         struct urb_priv *urbp = urb->hcpriv;
62
63         if (!(urb->transfer_flags & URB_NO_FSBR))
64                 urbp->fsbr = 1;
65 }
66
67 static void uhci_qh_wants_fsbr(struct uhci_hcd *uhci, struct uhci_qh *qh)
68 {
69         struct urb_priv *urbp =
70                         list_entry(qh->queue.next, struct urb_priv, node);
71
72         if (urbp->fsbr) {
73                 uhci->fsbr_jiffies = jiffies;
74                 if (!uhci->fsbr_is_on)
75                         uhci_fsbr_on(uhci);
76         }
77 }
78
79
80 static struct uhci_td *uhci_alloc_td(struct uhci_hcd *uhci)
81 {
82         dma_addr_t dma_handle;
83         struct uhci_td *td;
84
85         td = dma_pool_alloc(uhci->td_pool, GFP_ATOMIC, &dma_handle);
86         if (!td)
87                 return NULL;
88
89         td->dma_handle = dma_handle;
90         td->frame = -1;
91
92         INIT_LIST_HEAD(&td->list);
93         INIT_LIST_HEAD(&td->fl_list);
94
95         return td;
96 }
97
98 static void uhci_free_td(struct uhci_hcd *uhci, struct uhci_td *td)
99 {
100         if (!list_empty(&td->list))
101                 dev_warn(uhci_dev(uhci), "td %p still in list!\n", td);
102         if (!list_empty(&td->fl_list))
103                 dev_warn(uhci_dev(uhci), "td %p still in fl_list!\n", td);
104
105         dma_pool_free(uhci->td_pool, td, td->dma_handle);
106 }
107
108 static inline void uhci_fill_td(struct uhci_td *td, u32 status,
109                 u32 token, u32 buffer)
110 {
111         td->status = cpu_to_le32(status);
112         td->token = cpu_to_le32(token);
113         td->buffer = cpu_to_le32(buffer);
114 }
115
116 static void uhci_add_td_to_urbp(struct uhci_td *td, struct urb_priv *urbp)
117 {
118         list_add_tail(&td->list, &urbp->td_list);
119 }
120
121 static void uhci_remove_td_from_urbp(struct uhci_td *td)
122 {
123         list_del_init(&td->list);
124 }
125
126 /*
127  * We insert Isochronous URBs directly into the frame list at the beginning
128  */
129 static inline void uhci_insert_td_in_frame_list(struct uhci_hcd *uhci,
130                 struct uhci_td *td, unsigned framenum)
131 {
132         framenum &= (UHCI_NUMFRAMES - 1);
133
134         td->frame = framenum;
135
136         /* Is there a TD already mapped there? */
137         if (uhci->frame_cpu[framenum]) {
138                 struct uhci_td *ftd, *ltd;
139
140                 ftd = uhci->frame_cpu[framenum];
141                 ltd = list_entry(ftd->fl_list.prev, struct uhci_td, fl_list);
142
143                 list_add_tail(&td->fl_list, &ftd->fl_list);
144
145                 td->link = ltd->link;
146                 wmb();
147                 ltd->link = cpu_to_le32(td->dma_handle);
148         } else {
149                 td->link = uhci->frame[framenum];
150                 wmb();
151                 uhci->frame[framenum] = cpu_to_le32(td->dma_handle);
152                 uhci->frame_cpu[framenum] = td;
153         }
154 }
155
156 static inline void uhci_remove_td_from_frame_list(struct uhci_hcd *uhci,
157                 struct uhci_td *td)
158 {
159         /* If it's not inserted, don't remove it */
160         if (td->frame == -1) {
161                 WARN_ON(!list_empty(&td->fl_list));
162                 return;
163         }
164
165         if (uhci->frame_cpu[td->frame] == td) {
166                 if (list_empty(&td->fl_list)) {
167                         uhci->frame[td->frame] = td->link;
168                         uhci->frame_cpu[td->frame] = NULL;
169                 } else {
170                         struct uhci_td *ntd;
171
172                         ntd = list_entry(td->fl_list.next, struct uhci_td, fl_list);
173                         uhci->frame[td->frame] = cpu_to_le32(ntd->dma_handle);
174                         uhci->frame_cpu[td->frame] = ntd;
175                 }
176         } else {
177                 struct uhci_td *ptd;
178
179                 ptd = list_entry(td->fl_list.prev, struct uhci_td, fl_list);
180                 ptd->link = td->link;
181         }
182
183         list_del_init(&td->fl_list);
184         td->frame = -1;
185 }
186
187 /*
188  * Remove all the TDs for an Isochronous URB from the frame list
189  */
190 static void uhci_unlink_isochronous_tds(struct uhci_hcd *uhci, struct urb *urb)
191 {
192         struct urb_priv *urbp = (struct urb_priv *) urb->hcpriv;
193         struct uhci_td *td;
194
195         list_for_each_entry(td, &urbp->td_list, list)
196                 uhci_remove_td_from_frame_list(uhci, td);
197         wmb();
198 }
199
200 static struct uhci_qh *uhci_alloc_qh(struct uhci_hcd *uhci,
201                 struct usb_device *udev, struct usb_host_endpoint *hep)
202 {
203         dma_addr_t dma_handle;
204         struct uhci_qh *qh;
205
206         qh = dma_pool_alloc(uhci->qh_pool, GFP_ATOMIC, &dma_handle);
207         if (!qh)
208                 return NULL;
209
210         memset(qh, 0, sizeof(*qh));
211         qh->dma_handle = dma_handle;
212
213         qh->element = UHCI_PTR_TERM;
214         qh->link = UHCI_PTR_TERM;
215
216         INIT_LIST_HEAD(&qh->queue);
217         INIT_LIST_HEAD(&qh->node);
218
219         if (udev) {             /* Normal QH */
220                 qh->dummy_td = uhci_alloc_td(uhci);
221                 if (!qh->dummy_td) {
222                         dma_pool_free(uhci->qh_pool, qh, dma_handle);
223                         return NULL;
224                 }
225                 qh->state = QH_STATE_IDLE;
226                 qh->hep = hep;
227                 qh->udev = udev;
228                 hep->hcpriv = qh;
229                 qh->type = hep->desc.bmAttributes & USB_ENDPOINT_XFERTYPE_MASK;
230
231         } else {                /* Skeleton QH */
232                 qh->state = QH_STATE_ACTIVE;
233                 qh->type = -1;
234         }
235         return qh;
236 }
237
238 static void uhci_free_qh(struct uhci_hcd *uhci, struct uhci_qh *qh)
239 {
240         WARN_ON(qh->state != QH_STATE_IDLE && qh->udev);
241         if (!list_empty(&qh->queue))
242                 dev_warn(uhci_dev(uhci), "qh %p list not empty!\n", qh);
243
244         list_del(&qh->node);
245         if (qh->udev) {
246                 qh->hep->hcpriv = NULL;
247                 uhci_free_td(uhci, qh->dummy_td);
248         }
249         dma_pool_free(uhci->qh_pool, qh, qh->dma_handle);
250 }
251
252 /*
253  * When a queue is stopped and a dequeued URB is given back, adjust
254  * the previous TD link (if the URB isn't first on the queue) or
255  * save its toggle value (if it is first and is currently executing).
256  */
257 static void uhci_cleanup_queue(struct uhci_qh *qh,
258                 struct urb *urb)
259 {
260         struct urb_priv *urbp = urb->hcpriv;
261         struct uhci_td *td;
262
263         /* Isochronous pipes don't use toggles and their TD link pointers
264          * get adjusted during uhci_urb_dequeue(). */
265         if (qh->type == USB_ENDPOINT_XFER_ISOC)
266                 return;
267
268         /* If the URB isn't first on its queue, adjust the link pointer
269          * of the last TD in the previous URB.  The toggle doesn't need
270          * to be saved since this URB can't be executing yet. */
271         if (qh->queue.next != &urbp->node) {
272                 struct urb_priv *purbp;
273                 struct uhci_td *ptd;
274
275                 purbp = list_entry(urbp->node.prev, struct urb_priv, node);
276                 WARN_ON(list_empty(&purbp->td_list));
277                 ptd = list_entry(purbp->td_list.prev, struct uhci_td,
278                                 list);
279                 td = list_entry(urbp->td_list.prev, struct uhci_td,
280                                 list);
281                 ptd->link = td->link;
282                 return;
283         }
284
285         /* If the QH element pointer is UHCI_PTR_TERM then then currently
286          * executing URB has already been unlinked, so this one isn't it. */
287         if (qh_element(qh) == UHCI_PTR_TERM)
288                 return;
289         qh->element = UHCI_PTR_TERM;
290
291         /* Control pipes have to worry about toggles */
292         if (qh->type == USB_ENDPOINT_XFER_CONTROL)
293                 return;
294
295         /* Save the next toggle value */
296         WARN_ON(list_empty(&urbp->td_list));
297         td = list_entry(urbp->td_list.next, struct uhci_td, list);
298         qh->needs_fixup = 1;
299         qh->initial_toggle = uhci_toggle(td_token(td));
300 }
301
302 /*
303  * Fix up the data toggles for URBs in a queue, when one of them
304  * terminates early (short transfer, error, or dequeued).
305  */
306 static void uhci_fixup_toggles(struct uhci_qh *qh, int skip_first)
307 {
308         struct urb_priv *urbp = NULL;
309         struct uhci_td *td;
310         unsigned int toggle = qh->initial_toggle;
311         unsigned int pipe;
312
313         /* Fixups for a short transfer start with the second URB in the
314          * queue (the short URB is the first). */
315         if (skip_first)
316                 urbp = list_entry(qh->queue.next, struct urb_priv, node);
317
318         /* When starting with the first URB, if the QH element pointer is
319          * still valid then we know the URB's toggles are okay. */
320         else if (qh_element(qh) != UHCI_PTR_TERM)
321                 toggle = 2;
322
323         /* Fix up the toggle for the URBs in the queue.  Normally this
324          * loop won't run more than once: When an error or short transfer
325          * occurs, the queue usually gets emptied. */
326         urbp = list_prepare_entry(urbp, &qh->queue, node);
327         list_for_each_entry_continue(urbp, &qh->queue, node) {
328
329                 /* If the first TD has the right toggle value, we don't
330                  * need to change any toggles in this URB */
331                 td = list_entry(urbp->td_list.next, struct uhci_td, list);
332                 if (toggle > 1 || uhci_toggle(td_token(td)) == toggle) {
333                         td = list_entry(urbp->td_list.next, struct uhci_td,
334                                         list);
335                         toggle = uhci_toggle(td_token(td)) ^ 1;
336
337                 /* Otherwise all the toggles in the URB have to be switched */
338                 } else {
339                         list_for_each_entry(td, &urbp->td_list, list) {
340                                 td->token ^= __constant_cpu_to_le32(
341                                                         TD_TOKEN_TOGGLE);
342                                 toggle ^= 1;
343                         }
344                 }
345         }
346
347         wmb();
348         pipe = list_entry(qh->queue.next, struct urb_priv, node)->urb->pipe;
349         usb_settoggle(qh->udev, usb_pipeendpoint(pipe),
350                         usb_pipeout(pipe), toggle);
351         qh->needs_fixup = 0;
352 }
353
354 /*
355  * Put a QH on the schedule in both hardware and software
356  */
357 static void uhci_activate_qh(struct uhci_hcd *uhci, struct uhci_qh *qh)
358 {
359         struct uhci_qh *pqh;
360
361         WARN_ON(list_empty(&qh->queue));
362
363         /* Set the element pointer if it isn't set already.
364          * This isn't needed for Isochronous queues, but it doesn't hurt. */
365         if (qh_element(qh) == UHCI_PTR_TERM) {
366                 struct urb_priv *urbp = list_entry(qh->queue.next,
367                                 struct urb_priv, node);
368                 struct uhci_td *td = list_entry(urbp->td_list.next,
369                                 struct uhci_td, list);
370
371                 qh->element = cpu_to_le32(td->dma_handle);
372         }
373
374         /* Treat the queue as if it has just advanced */
375         qh->wait_expired = 0;
376         qh->advance_jiffies = jiffies;
377
378         if (qh->state == QH_STATE_ACTIVE)
379                 return;
380         qh->state = QH_STATE_ACTIVE;
381
382         /* Move the QH from its old list to the end of the appropriate
383          * skeleton's list */
384         if (qh == uhci->next_qh)
385                 uhci->next_qh = list_entry(qh->node.next, struct uhci_qh,
386                                 node);
387         list_move_tail(&qh->node, &qh->skel->node);
388
389         /* Link it into the schedule */
390         pqh = list_entry(qh->node.prev, struct uhci_qh, node);
391         qh->link = pqh->link;
392         wmb();
393         pqh->link = UHCI_PTR_QH | cpu_to_le32(qh->dma_handle);
394 }
395
396 /*
397  * Take a QH off the hardware schedule
398  */
399 static void uhci_unlink_qh(struct uhci_hcd *uhci, struct uhci_qh *qh)
400 {
401         struct uhci_qh *pqh;
402
403         if (qh->state == QH_STATE_UNLINKING)
404                 return;
405         WARN_ON(qh->state != QH_STATE_ACTIVE || !qh->udev);
406         qh->state = QH_STATE_UNLINKING;
407
408         /* Unlink the QH from the schedule and record when we did it */
409         pqh = list_entry(qh->node.prev, struct uhci_qh, node);
410         pqh->link = qh->link;
411         mb();
412
413         uhci_get_current_frame_number(uhci);
414         qh->unlink_frame = uhci->frame_number;
415
416         /* Force an interrupt so we know when the QH is fully unlinked */
417         if (list_empty(&uhci->skel_unlink_qh->node))
418                 uhci_set_next_interrupt(uhci);
419
420         /* Move the QH from its old list to the end of the unlinking list */
421         if (qh == uhci->next_qh)
422                 uhci->next_qh = list_entry(qh->node.next, struct uhci_qh,
423                                 node);
424         list_move_tail(&qh->node, &uhci->skel_unlink_qh->node);
425 }
426
427 /*
428  * When we and the controller are through with a QH, it becomes IDLE.
429  * This happens when a QH has been off the schedule (on the unlinking
430  * list) for more than one frame, or when an error occurs while adding
431  * the first URB onto a new QH.
432  */
433 static void uhci_make_qh_idle(struct uhci_hcd *uhci, struct uhci_qh *qh)
434 {
435         WARN_ON(qh->state == QH_STATE_ACTIVE);
436
437         if (qh == uhci->next_qh)
438                 uhci->next_qh = list_entry(qh->node.next, struct uhci_qh,
439                                 node);
440         list_move(&qh->node, &uhci->idle_qh_list);
441         qh->state = QH_STATE_IDLE;
442
443         /* Now that the QH is idle, its post_td isn't being used */
444         if (qh->post_td) {
445                 uhci_free_td(uhci, qh->post_td);
446                 qh->post_td = NULL;
447         }
448
449         /* If anyone is waiting for a QH to become idle, wake them up */
450         if (uhci->num_waiting)
451                 wake_up_all(&uhci->waitqh);
452 }
453
454 static inline struct urb_priv *uhci_alloc_urb_priv(struct uhci_hcd *uhci,
455                 struct urb *urb)
456 {
457         struct urb_priv *urbp;
458
459         urbp = kmem_cache_alloc(uhci_up_cachep, SLAB_ATOMIC);
460         if (!urbp)
461                 return NULL;
462
463         memset((void *)urbp, 0, sizeof(*urbp));
464
465         urbp->urb = urb;
466         urb->hcpriv = urbp;
467         
468         INIT_LIST_HEAD(&urbp->node);
469         INIT_LIST_HEAD(&urbp->td_list);
470
471         return urbp;
472 }
473
474 static void uhci_free_urb_priv(struct uhci_hcd *uhci,
475                 struct urb_priv *urbp)
476 {
477         struct uhci_td *td, *tmp;
478
479         if (!list_empty(&urbp->node))
480                 dev_warn(uhci_dev(uhci), "urb %p still on QH's list!\n",
481                                 urbp->urb);
482
483         list_for_each_entry_safe(td, tmp, &urbp->td_list, list) {
484                 uhci_remove_td_from_urbp(td);
485                 uhci_free_td(uhci, td);
486         }
487
488         urbp->urb->hcpriv = NULL;
489         kmem_cache_free(uhci_up_cachep, urbp);
490 }
491
492 /*
493  * Map status to standard result codes
494  *
495  * <status> is (td_status(td) & 0xF60000), a.k.a.
496  * uhci_status_bits(td_status(td)).
497  * Note: <status> does not include the TD_CTRL_NAK bit.
498  * <dir_out> is True for output TDs and False for input TDs.
499  */
500 static int uhci_map_status(int status, int dir_out)
501 {
502         if (!status)
503                 return 0;
504         if (status & TD_CTRL_BITSTUFF)                  /* Bitstuff error */
505                 return -EPROTO;
506         if (status & TD_CTRL_CRCTIMEO) {                /* CRC/Timeout */
507                 if (dir_out)
508                         return -EPROTO;
509                 else
510                         return -EILSEQ;
511         }
512         if (status & TD_CTRL_BABBLE)                    /* Babble */
513                 return -EOVERFLOW;
514         if (status & TD_CTRL_DBUFERR)                   /* Buffer error */
515                 return -ENOSR;
516         if (status & TD_CTRL_STALLED)                   /* Stalled */
517                 return -EPIPE;
518         WARN_ON(status & TD_CTRL_ACTIVE);               /* Active */
519         return 0;
520 }
521
522 /*
523  * Control transfers
524  */
525 static int uhci_submit_control(struct uhci_hcd *uhci, struct urb *urb,
526                 struct uhci_qh *qh)
527 {
528         struct uhci_td *td;
529         unsigned long destination, status;
530         int maxsze = le16_to_cpu(qh->hep->desc.wMaxPacketSize);
531         int len = urb->transfer_buffer_length;
532         dma_addr_t data = urb->transfer_dma;
533         __le32 *plink;
534         struct urb_priv *urbp = urb->hcpriv;
535
536         /* The "pipe" thing contains the destination in bits 8--18 */
537         destination = (urb->pipe & PIPE_DEVEP_MASK) | USB_PID_SETUP;
538
539         /* 3 errors, dummy TD remains inactive */
540         status = uhci_maxerr(3);
541         if (urb->dev->speed == USB_SPEED_LOW)
542                 status |= TD_CTRL_LS;
543
544         /*
545          * Build the TD for the control request setup packet
546          */
547         td = qh->dummy_td;
548         uhci_add_td_to_urbp(td, urbp);
549         uhci_fill_td(td, status, destination | uhci_explen(8),
550                         urb->setup_dma);
551         plink = &td->link;
552         status |= TD_CTRL_ACTIVE;
553
554         /*
555          * If direction is "send", change the packet ID from SETUP (0x2D)
556          * to OUT (0xE1).  Else change it from SETUP to IN (0x69) and
557          * set Short Packet Detect (SPD) for all data packets.
558          */
559         if (usb_pipeout(urb->pipe))
560                 destination ^= (USB_PID_SETUP ^ USB_PID_OUT);
561         else {
562                 destination ^= (USB_PID_SETUP ^ USB_PID_IN);
563                 status |= TD_CTRL_SPD;
564         }
565
566         /*
567          * Build the DATA TDs
568          */
569         while (len > 0) {
570                 int pktsze = min(len, maxsze);
571
572                 td = uhci_alloc_td(uhci);
573                 if (!td)
574                         goto nomem;
575                 *plink = cpu_to_le32(td->dma_handle);
576
577                 /* Alternate Data0/1 (start with Data1) */
578                 destination ^= TD_TOKEN_TOGGLE;
579         
580                 uhci_add_td_to_urbp(td, urbp);
581                 uhci_fill_td(td, status, destination | uhci_explen(pktsze),
582                                 data);
583                 plink = &td->link;
584
585                 data += pktsze;
586                 len -= pktsze;
587         }
588
589         /*
590          * Build the final TD for control status 
591          */
592         td = uhci_alloc_td(uhci);
593         if (!td)
594                 goto nomem;
595         *plink = cpu_to_le32(td->dma_handle);
596
597         /*
598          * It's IN if the pipe is an output pipe or we're not expecting
599          * data back.
600          */
601         destination &= ~TD_TOKEN_PID_MASK;
602         if (usb_pipeout(urb->pipe) || !urb->transfer_buffer_length)
603                 destination |= USB_PID_IN;
604         else
605                 destination |= USB_PID_OUT;
606
607         destination |= TD_TOKEN_TOGGLE;         /* End in Data1 */
608
609         status &= ~TD_CTRL_SPD;
610
611         uhci_add_td_to_urbp(td, urbp);
612         uhci_fill_td(td, status | TD_CTRL_IOC,
613                         destination | uhci_explen(0), 0);
614         plink = &td->link;
615
616         /*
617          * Build the new dummy TD and activate the old one
618          */
619         td = uhci_alloc_td(uhci);
620         if (!td)
621                 goto nomem;
622         *plink = cpu_to_le32(td->dma_handle);
623
624         uhci_fill_td(td, 0, USB_PID_OUT | uhci_explen(0), 0);
625         wmb();
626         qh->dummy_td->status |= __constant_cpu_to_le32(TD_CTRL_ACTIVE);
627         qh->dummy_td = td;
628
629         /* Low-speed transfers get a different queue, and won't hog the bus.
630          * Also, some devices enumerate better without FSBR; the easiest way
631          * to do that is to put URBs on the low-speed queue while the device
632          * isn't in the CONFIGURED state. */
633         if (urb->dev->speed == USB_SPEED_LOW ||
634                         urb->dev->state != USB_STATE_CONFIGURED)
635                 qh->skel = uhci->skel_ls_control_qh;
636         else {
637                 qh->skel = uhci->skel_fs_control_qh;
638                 uhci_add_fsbr(uhci, urb);
639         }
640
641         urb->actual_length = -8;        /* Account for the SETUP packet */
642         return 0;
643
644 nomem:
645         /* Remove the dummy TD from the td_list so it doesn't get freed */
646         uhci_remove_td_from_urbp(qh->dummy_td);
647         return -ENOMEM;
648 }
649
650 /*
651  * Common submit for bulk and interrupt
652  */
653 static int uhci_submit_common(struct uhci_hcd *uhci, struct urb *urb,
654                 struct uhci_qh *qh)
655 {
656         struct uhci_td *td;
657         unsigned long destination, status;
658         int maxsze = le16_to_cpu(qh->hep->desc.wMaxPacketSize);
659         int len = urb->transfer_buffer_length;
660         dma_addr_t data = urb->transfer_dma;
661         __le32 *plink;
662         struct urb_priv *urbp = urb->hcpriv;
663         unsigned int toggle;
664
665         if (len < 0)
666                 return -EINVAL;
667
668         /* The "pipe" thing contains the destination in bits 8--18 */
669         destination = (urb->pipe & PIPE_DEVEP_MASK) | usb_packetid(urb->pipe);
670         toggle = usb_gettoggle(urb->dev, usb_pipeendpoint(urb->pipe),
671                          usb_pipeout(urb->pipe));
672
673         /* 3 errors, dummy TD remains inactive */
674         status = uhci_maxerr(3);
675         if (urb->dev->speed == USB_SPEED_LOW)
676                 status |= TD_CTRL_LS;
677         if (usb_pipein(urb->pipe))
678                 status |= TD_CTRL_SPD;
679
680         /*
681          * Build the DATA TDs
682          */
683         plink = NULL;
684         td = qh->dummy_td;
685         do {    /* Allow zero length packets */
686                 int pktsze = maxsze;
687
688                 if (len <= pktsze) {            /* The last packet */
689                         pktsze = len;
690                         if (!(urb->transfer_flags & URB_SHORT_NOT_OK))
691                                 status &= ~TD_CTRL_SPD;
692                 }
693
694                 if (plink) {
695                         td = uhci_alloc_td(uhci);
696                         if (!td)
697                                 goto nomem;
698                         *plink = cpu_to_le32(td->dma_handle);
699                 }
700                 uhci_add_td_to_urbp(td, urbp);
701                 uhci_fill_td(td, status,
702                                 destination | uhci_explen(pktsze) |
703                                         (toggle << TD_TOKEN_TOGGLE_SHIFT),
704                                 data);
705                 plink = &td->link;
706                 status |= TD_CTRL_ACTIVE;
707
708                 data += pktsze;
709                 len -= maxsze;
710                 toggle ^= 1;
711         } while (len > 0);
712
713         /*
714          * URB_ZERO_PACKET means adding a 0-length packet, if direction
715          * is OUT and the transfer_length was an exact multiple of maxsze,
716          * hence (len = transfer_length - N * maxsze) == 0
717          * however, if transfer_length == 0, the zero packet was already
718          * prepared above.
719          */
720         if ((urb->transfer_flags & URB_ZERO_PACKET) &&
721                         usb_pipeout(urb->pipe) && len == 0 &&
722                         urb->transfer_buffer_length > 0) {
723                 td = uhci_alloc_td(uhci);
724                 if (!td)
725                         goto nomem;
726                 *plink = cpu_to_le32(td->dma_handle);
727
728                 uhci_add_td_to_urbp(td, urbp);
729                 uhci_fill_td(td, status,
730                                 destination | uhci_explen(0) |
731                                         (toggle << TD_TOKEN_TOGGLE_SHIFT),
732                                 data);
733                 plink = &td->link;
734
735                 toggle ^= 1;
736         }
737
738         /* Set the interrupt-on-completion flag on the last packet.
739          * A more-or-less typical 4 KB URB (= size of one memory page)
740          * will require about 3 ms to transfer; that's a little on the
741          * fast side but not enough to justify delaying an interrupt
742          * more than 2 or 3 URBs, so we will ignore the URB_NO_INTERRUPT
743          * flag setting. */
744         td->status |= __constant_cpu_to_le32(TD_CTRL_IOC);
745
746         /*
747          * Build the new dummy TD and activate the old one
748          */
749         td = uhci_alloc_td(uhci);
750         if (!td)
751                 goto nomem;
752         *plink = cpu_to_le32(td->dma_handle);
753
754         uhci_fill_td(td, 0, USB_PID_OUT | uhci_explen(0), 0);
755         wmb();
756         qh->dummy_td->status |= __constant_cpu_to_le32(TD_CTRL_ACTIVE);
757         qh->dummy_td = td;
758
759         usb_settoggle(urb->dev, usb_pipeendpoint(urb->pipe),
760                         usb_pipeout(urb->pipe), toggle);
761         return 0;
762
763 nomem:
764         /* Remove the dummy TD from the td_list so it doesn't get freed */
765         uhci_remove_td_from_urbp(qh->dummy_td);
766         return -ENOMEM;
767 }
768
769 static inline int uhci_submit_bulk(struct uhci_hcd *uhci, struct urb *urb,
770                 struct uhci_qh *qh)
771 {
772         int ret;
773
774         /* Can't have low-speed bulk transfers */
775         if (urb->dev->speed == USB_SPEED_LOW)
776                 return -EINVAL;
777
778         qh->skel = uhci->skel_bulk_qh;
779         ret = uhci_submit_common(uhci, urb, qh);
780         if (ret == 0)
781                 uhci_add_fsbr(uhci, urb);
782         return ret;
783 }
784
785 static inline int uhci_submit_interrupt(struct uhci_hcd *uhci, struct urb *urb,
786                 struct uhci_qh *qh)
787 {
788         /* USB 1.1 interrupt transfers only involve one packet per interval.
789          * Drivers can submit URBs of any length, but longer ones will need
790          * multiple intervals to complete.
791          */
792         qh->skel = uhci->skelqh[__interval_to_skel(urb->interval)];
793         return uhci_submit_common(uhci, urb, qh);
794 }
795
796 /*
797  * Fix up the data structures following a short transfer
798  */
799 static int uhci_fixup_short_transfer(struct uhci_hcd *uhci,
800                 struct uhci_qh *qh, struct urb_priv *urbp)
801 {
802         struct uhci_td *td;
803         struct list_head *tmp;
804         int ret;
805
806         td = list_entry(urbp->td_list.prev, struct uhci_td, list);
807         if (qh->type == USB_ENDPOINT_XFER_CONTROL) {
808
809                 /* When a control transfer is short, we have to restart
810                  * the queue at the status stage transaction, which is
811                  * the last TD. */
812                 WARN_ON(list_empty(&urbp->td_list));
813                 qh->element = cpu_to_le32(td->dma_handle);
814                 tmp = td->list.prev;
815                 ret = -EINPROGRESS;
816
817         } else {
818
819                 /* When a bulk/interrupt transfer is short, we have to
820                  * fix up the toggles of the following URBs on the queue
821                  * before restarting the queue at the next URB. */
822                 qh->initial_toggle = uhci_toggle(td_token(qh->post_td)) ^ 1;
823                 uhci_fixup_toggles(qh, 1);
824
825                 if (list_empty(&urbp->td_list))
826                         td = qh->post_td;
827                 qh->element = td->link;
828                 tmp = urbp->td_list.prev;
829                 ret = 0;
830         }
831
832         /* Remove all the TDs we skipped over, from tmp back to the start */
833         while (tmp != &urbp->td_list) {
834                 td = list_entry(tmp, struct uhci_td, list);
835                 tmp = tmp->prev;
836
837                 uhci_remove_td_from_urbp(td);
838                 uhci_free_td(uhci, td);
839         }
840         return ret;
841 }
842
843 /*
844  * Common result for control, bulk, and interrupt
845  */
846 static int uhci_result_common(struct uhci_hcd *uhci, struct urb *urb)
847 {
848         struct urb_priv *urbp = urb->hcpriv;
849         struct uhci_qh *qh = urbp->qh;
850         struct uhci_td *td, *tmp;
851         unsigned status;
852         int ret = 0;
853
854         list_for_each_entry_safe(td, tmp, &urbp->td_list, list) {
855                 unsigned int ctrlstat;
856                 int len;
857
858                 ctrlstat = td_status(td);
859                 status = uhci_status_bits(ctrlstat);
860                 if (status & TD_CTRL_ACTIVE)
861                         return -EINPROGRESS;
862
863                 len = uhci_actual_length(ctrlstat);
864                 urb->actual_length += len;
865
866                 if (status) {
867                         ret = uhci_map_status(status,
868                                         uhci_packetout(td_token(td)));
869                         if ((debug == 1 && ret != -EPIPE) || debug > 1) {
870                                 /* Some debugging code */
871                                 dev_dbg(uhci_dev(uhci),
872                                                 "%s: failed with status %x\n",
873                                                 __FUNCTION__, status);
874
875                                 if (debug > 1 && errbuf) {
876                                         /* Print the chain for debugging */
877                                         uhci_show_qh(urbp->qh, errbuf,
878                                                         ERRBUF_LEN, 0);
879                                         lprintk(errbuf);
880                                 }
881                         }
882
883                 } else if (len < uhci_expected_length(td_token(td))) {
884
885                         /* We received a short packet */
886                         if (urb->transfer_flags & URB_SHORT_NOT_OK)
887                                 ret = -EREMOTEIO;
888                         else if (ctrlstat & TD_CTRL_SPD)
889                                 ret = 1;
890                 }
891
892                 uhci_remove_td_from_urbp(td);
893                 if (qh->post_td)
894                         uhci_free_td(uhci, qh->post_td);
895                 qh->post_td = td;
896
897                 if (ret != 0)
898                         goto err;
899         }
900         return ret;
901
902 err:
903         if (ret < 0) {
904                 /* In case a control transfer gets an error
905                  * during the setup stage */
906                 urb->actual_length = max(urb->actual_length, 0);
907
908                 /* Note that the queue has stopped and save
909                  * the next toggle value */
910                 qh->element = UHCI_PTR_TERM;
911                 qh->is_stopped = 1;
912                 qh->needs_fixup = (qh->type != USB_ENDPOINT_XFER_CONTROL);
913                 qh->initial_toggle = uhci_toggle(td_token(td)) ^
914                                 (ret == -EREMOTEIO);
915
916         } else          /* Short packet received */
917                 ret = uhci_fixup_short_transfer(uhci, qh, urbp);
918         return ret;
919 }
920
921 /*
922  * Isochronous transfers
923  */
924 static int uhci_submit_isochronous(struct uhci_hcd *uhci, struct urb *urb,
925                 struct uhci_qh *qh)
926 {
927         struct uhci_td *td = NULL;      /* Since urb->number_of_packets > 0 */
928         int i, frame;
929         unsigned long destination, status;
930         struct urb_priv *urbp = (struct urb_priv *) urb->hcpriv;
931
932         if (urb->number_of_packets > 900)       /* 900? Why? */
933                 return -EFBIG;
934
935         status = TD_CTRL_ACTIVE | TD_CTRL_IOS;
936         destination = (urb->pipe & PIPE_DEVEP_MASK) | usb_packetid(urb->pipe);
937
938         /* Figure out the starting frame number */
939         if (urb->transfer_flags & URB_ISO_ASAP) {
940                 if (list_empty(&qh->queue)) {
941                         uhci_get_current_frame_number(uhci);
942                         urb->start_frame = (uhci->frame_number + 10);
943
944                 } else {                /* Go right after the last one */
945                         struct urb *last_urb;
946
947                         last_urb = list_entry(qh->queue.prev,
948                                         struct urb_priv, node)->urb;
949                         urb->start_frame = (last_urb->start_frame +
950                                         last_urb->number_of_packets *
951                                         last_urb->interval);
952                 }
953         } else {
954                 /* FIXME: Sanity check */
955         }
956         urb->start_frame &= (UHCI_NUMFRAMES - 1);
957
958         for (i = 0; i < urb->number_of_packets; i++) {
959                 td = uhci_alloc_td(uhci);
960                 if (!td)
961                         return -ENOMEM;
962
963                 uhci_add_td_to_urbp(td, urbp);
964                 uhci_fill_td(td, status, destination |
965                                 uhci_explen(urb->iso_frame_desc[i].length),
966                                 urb->transfer_dma +
967                                         urb->iso_frame_desc[i].offset);
968         }
969
970         /* Set the interrupt-on-completion flag on the last packet. */
971         td->status |= __constant_cpu_to_le32(TD_CTRL_IOC);
972
973         qh->skel = uhci->skel_iso_qh;
974
975         /* Add the TDs to the frame list */
976         frame = urb->start_frame;
977         list_for_each_entry(td, &urbp->td_list, list) {
978                 uhci_insert_td_in_frame_list(uhci, td, frame);
979                 frame += urb->interval;
980         }
981
982         return 0;
983 }
984
985 static int uhci_result_isochronous(struct uhci_hcd *uhci, struct urb *urb)
986 {
987         struct uhci_td *td;
988         struct urb_priv *urbp = (struct urb_priv *)urb->hcpriv;
989         int status;
990         int i, ret = 0;
991
992         urb->actual_length = urb->error_count = 0;
993
994         i = 0;
995         list_for_each_entry(td, &urbp->td_list, list) {
996                 int actlength;
997                 unsigned int ctrlstat = td_status(td);
998
999                 if (ctrlstat & TD_CTRL_ACTIVE)
1000                         return -EINPROGRESS;
1001
1002                 actlength = uhci_actual_length(ctrlstat);
1003                 urb->iso_frame_desc[i].actual_length = actlength;
1004                 urb->actual_length += actlength;
1005
1006                 status = uhci_map_status(uhci_status_bits(ctrlstat),
1007                                 usb_pipeout(urb->pipe));
1008                 urb->iso_frame_desc[i].status = status;
1009                 if (status) {
1010                         urb->error_count++;
1011                         ret = status;
1012                 }
1013
1014                 i++;
1015         }
1016
1017         return ret;
1018 }
1019
1020 static int uhci_urb_enqueue(struct usb_hcd *hcd,
1021                 struct usb_host_endpoint *hep,
1022                 struct urb *urb, gfp_t mem_flags)
1023 {
1024         int ret;
1025         struct uhci_hcd *uhci = hcd_to_uhci(hcd);
1026         unsigned long flags;
1027         struct urb_priv *urbp;
1028         struct uhci_qh *qh;
1029         int bustime;
1030
1031         spin_lock_irqsave(&uhci->lock, flags);
1032
1033         ret = urb->status;
1034         if (ret != -EINPROGRESS)                /* URB already unlinked! */
1035                 goto done;
1036
1037         ret = -ENOMEM;
1038         urbp = uhci_alloc_urb_priv(uhci, urb);
1039         if (!urbp)
1040                 goto done;
1041
1042         if (hep->hcpriv)
1043                 qh = (struct uhci_qh *) hep->hcpriv;
1044         else {
1045                 qh = uhci_alloc_qh(uhci, urb->dev, hep);
1046                 if (!qh)
1047                         goto err_no_qh;
1048         }
1049         urbp->qh = qh;
1050
1051         switch (qh->type) {
1052         case USB_ENDPOINT_XFER_CONTROL:
1053                 ret = uhci_submit_control(uhci, urb, qh);
1054                 break;
1055         case USB_ENDPOINT_XFER_BULK:
1056                 ret = uhci_submit_bulk(uhci, urb, qh);
1057                 break;
1058         case USB_ENDPOINT_XFER_INT:
1059                 if (list_empty(&qh->queue)) {
1060                         bustime = usb_check_bandwidth(urb->dev, urb);
1061                         if (bustime < 0)
1062                                 ret = bustime;
1063                         else {
1064                                 ret = uhci_submit_interrupt(uhci, urb, qh);
1065                                 if (ret == 0)
1066                                         usb_claim_bandwidth(urb->dev, urb, bustime, 0);
1067                         }
1068                 } else {        /* inherit from parent */
1069                         struct urb_priv *eurbp;
1070
1071                         eurbp = list_entry(qh->queue.prev, struct urb_priv,
1072                                         node);
1073                         urb->bandwidth = eurbp->urb->bandwidth;
1074                         ret = uhci_submit_interrupt(uhci, urb, qh);
1075                 }
1076                 break;
1077         case USB_ENDPOINT_XFER_ISOC:
1078                 bustime = usb_check_bandwidth(urb->dev, urb);
1079                 if (bustime < 0) {
1080                         ret = bustime;
1081                         break;
1082                 }
1083
1084                 ret = uhci_submit_isochronous(uhci, urb, qh);
1085                 if (ret == 0)
1086                         usb_claim_bandwidth(urb->dev, urb, bustime, 1);
1087                 break;
1088         }
1089         if (ret != 0)
1090                 goto err_submit_failed;
1091
1092         /* Add this URB to the QH */
1093         urbp->qh = qh;
1094         list_add_tail(&urbp->node, &qh->queue);
1095
1096         /* If the new URB is the first and only one on this QH then either
1097          * the QH is new and idle or else it's unlinked and waiting to
1098          * become idle, so we can activate it right away.  But only if the
1099          * queue isn't stopped. */
1100         if (qh->queue.next == &urbp->node && !qh->is_stopped) {
1101                 uhci_activate_qh(uhci, qh);
1102                 uhci_qh_wants_fsbr(uhci, qh);
1103         }
1104         goto done;
1105
1106 err_submit_failed:
1107         if (qh->state == QH_STATE_IDLE)
1108                 uhci_make_qh_idle(uhci, qh);    /* Reclaim unused QH */
1109
1110 err_no_qh:
1111         uhci_free_urb_priv(uhci, urbp);
1112
1113 done:
1114         spin_unlock_irqrestore(&uhci->lock, flags);
1115         return ret;
1116 }
1117
1118 static int uhci_urb_dequeue(struct usb_hcd *hcd, struct urb *urb)
1119 {
1120         struct uhci_hcd *uhci = hcd_to_uhci(hcd);
1121         unsigned long flags;
1122         struct urb_priv *urbp;
1123
1124         spin_lock_irqsave(&uhci->lock, flags);
1125         urbp = urb->hcpriv;
1126         if (!urbp)                      /* URB was never linked! */
1127                 goto done;
1128
1129         /* Remove Isochronous TDs from the frame list ASAP */
1130         if (urbp->qh->type == USB_ENDPOINT_XFER_ISOC)
1131                 uhci_unlink_isochronous_tds(uhci, urb);
1132         uhci_unlink_qh(uhci, urbp->qh);
1133
1134 done:
1135         spin_unlock_irqrestore(&uhci->lock, flags);
1136         return 0;
1137 }
1138
1139 /*
1140  * Finish unlinking an URB and give it back
1141  */
1142 static void uhci_giveback_urb(struct uhci_hcd *uhci, struct uhci_qh *qh,
1143                 struct urb *urb, struct pt_regs *regs)
1144 __releases(uhci->lock)
1145 __acquires(uhci->lock)
1146 {
1147         struct urb_priv *urbp = (struct urb_priv *) urb->hcpriv;
1148
1149         /* Isochronous TDs get unlinked directly from the frame list */
1150         if (qh->type == USB_ENDPOINT_XFER_ISOC)
1151                 uhci_unlink_isochronous_tds(uhci, urb);
1152
1153         /* Take the URB off the QH's queue.  If the queue is now empty,
1154          * this is a perfect time for a toggle fixup. */
1155         list_del_init(&urbp->node);
1156         if (list_empty(&qh->queue) && qh->needs_fixup) {
1157                 usb_settoggle(urb->dev, usb_pipeendpoint(urb->pipe),
1158                                 usb_pipeout(urb->pipe), qh->initial_toggle);
1159                 qh->needs_fixup = 0;
1160         }
1161
1162         uhci_free_urb_priv(uhci, urbp);
1163
1164         switch (qh->type) {
1165         case USB_ENDPOINT_XFER_ISOC:
1166                 /* Release bandwidth for Interrupt or Isoc. transfers */
1167                 if (urb->bandwidth)
1168                         usb_release_bandwidth(urb->dev, urb, 1);
1169                 break;
1170         case USB_ENDPOINT_XFER_INT:
1171                 /* Release bandwidth for Interrupt or Isoc. transfers */
1172                 /* Make sure we don't release if we have a queued URB */
1173                 if (list_empty(&qh->queue) && urb->bandwidth)
1174                         usb_release_bandwidth(urb->dev, urb, 0);
1175                 else
1176                         /* bandwidth was passed on to queued URB, */
1177                         /* so don't let usb_unlink_urb() release it */
1178                         urb->bandwidth = 0;
1179                 break;
1180         }
1181
1182         spin_unlock(&uhci->lock);
1183         usb_hcd_giveback_urb(uhci_to_hcd(uhci), urb, regs);
1184         spin_lock(&uhci->lock);
1185
1186         /* If the queue is now empty, we can unlink the QH and give up its
1187          * reserved bandwidth. */
1188         if (list_empty(&qh->queue)) {
1189                 uhci_unlink_qh(uhci, qh);
1190
1191                 /* Bandwidth stuff not yet implemented */
1192         }
1193 }
1194
1195 /*
1196  * Scan the URBs in a QH's queue
1197  */
1198 #define QH_FINISHED_UNLINKING(qh)                       \
1199                 (qh->state == QH_STATE_UNLINKING &&     \
1200                 uhci->frame_number + uhci->is_stopped != qh->unlink_frame)
1201
1202 static void uhci_scan_qh(struct uhci_hcd *uhci, struct uhci_qh *qh,
1203                 struct pt_regs *regs)
1204 {
1205         struct urb_priv *urbp;
1206         struct urb *urb;
1207         int status;
1208
1209         while (!list_empty(&qh->queue)) {
1210                 urbp = list_entry(qh->queue.next, struct urb_priv, node);
1211                 urb = urbp->urb;
1212
1213                 if (qh->type == USB_ENDPOINT_XFER_ISOC)
1214                         status = uhci_result_isochronous(uhci, urb);
1215                 else
1216                         status = uhci_result_common(uhci, urb);
1217                 if (status == -EINPROGRESS)
1218                         break;
1219
1220                 spin_lock(&urb->lock);
1221                 if (urb->status == -EINPROGRESS)        /* Not dequeued */
1222                         urb->status = status;
1223                 else
1224                         status = ECONNRESET;            /* Not -ECONNRESET */
1225                 spin_unlock(&urb->lock);
1226
1227                 /* Dequeued but completed URBs can't be given back unless
1228                  * the QH is stopped or has finished unlinking. */
1229                 if (status == ECONNRESET) {
1230                         if (QH_FINISHED_UNLINKING(qh))
1231                                 qh->is_stopped = 1;
1232                         else if (!qh->is_stopped)
1233                                 return;
1234                 }
1235
1236                 uhci_giveback_urb(uhci, qh, urb, regs);
1237                 if (status < 0)
1238                         break;
1239         }
1240
1241         /* If the QH is neither stopped nor finished unlinking (normal case),
1242          * our work here is done. */
1243         if (QH_FINISHED_UNLINKING(qh))
1244                 qh->is_stopped = 1;
1245         else if (!qh->is_stopped)
1246                 return;
1247
1248         /* Otherwise give back each of the dequeued URBs */
1249 restart:
1250         list_for_each_entry(urbp, &qh->queue, node) {
1251                 urb = urbp->urb;
1252                 if (urb->status != -EINPROGRESS) {
1253                         uhci_cleanup_queue(qh, urb);
1254                         uhci_giveback_urb(uhci, qh, urb, regs);
1255                         goto restart;
1256                 }
1257         }
1258         qh->is_stopped = 0;
1259
1260         /* There are no more dequeued URBs.  If there are still URBs on the
1261          * queue, the QH can now be re-activated. */
1262         if (!list_empty(&qh->queue)) {
1263                 if (qh->needs_fixup)
1264                         uhci_fixup_toggles(qh, 0);
1265
1266                 /* If the first URB on the queue wants FSBR but its time
1267                  * limit has expired, set the next TD to interrupt on
1268                  * completion before reactivating the QH. */
1269                 urbp = list_entry(qh->queue.next, struct urb_priv, node);
1270                 if (urbp->fsbr && qh->wait_expired) {
1271                         struct uhci_td *td = list_entry(urbp->td_list.next,
1272                                         struct uhci_td, list);
1273
1274                         td->status |= __cpu_to_le32(TD_CTRL_IOC);
1275                 }
1276
1277                 uhci_activate_qh(uhci, qh);
1278         }
1279
1280         /* The queue is empty.  The QH can become idle if it is fully
1281          * unlinked. */
1282         else if (QH_FINISHED_UNLINKING(qh))
1283                 uhci_make_qh_idle(uhci, qh);
1284 }
1285
1286 /*
1287  * Check for queues that have made some forward progress.
1288  * Returns 0 if the queue is not Isochronous, is ACTIVE, and
1289  * has not advanced since last examined; 1 otherwise.
1290  *
1291  * Early Intel controllers have a bug which causes qh->element sometimes
1292  * not to advance when a TD completes successfully.  The queue remains
1293  * stuck on the inactive completed TD.  We detect such cases and advance
1294  * the element pointer by hand.
1295  */
1296 static int uhci_advance_check(struct uhci_hcd *uhci, struct uhci_qh *qh)
1297 {
1298         struct urb_priv *urbp = NULL;
1299         struct uhci_td *td;
1300         int ret = 1;
1301         unsigned status;
1302
1303         if (qh->type == USB_ENDPOINT_XFER_ISOC)
1304                 return ret;
1305
1306         /* Treat an UNLINKING queue as though it hasn't advanced.
1307          * This is okay because reactivation will treat it as though
1308          * it has advanced, and if it is going to become IDLE then
1309          * this doesn't matter anyway.  Furthermore it's possible
1310          * for an UNLINKING queue not to have any URBs at all, or
1311          * for its first URB not to have any TDs (if it was dequeued
1312          * just as it completed).  So it's not easy in any case to
1313          * test whether such queues have advanced. */
1314         if (qh->state != QH_STATE_ACTIVE) {
1315                 urbp = NULL;
1316                 status = 0;
1317
1318         } else {
1319                 urbp = list_entry(qh->queue.next, struct urb_priv, node);
1320                 td = list_entry(urbp->td_list.next, struct uhci_td, list);
1321                 status = td_status(td);
1322                 if (!(status & TD_CTRL_ACTIVE)) {
1323
1324                         /* We're okay, the queue has advanced */
1325                         qh->wait_expired = 0;
1326                         qh->advance_jiffies = jiffies;
1327                         return ret;
1328                 }
1329                 ret = 0;
1330         }
1331
1332         /* The queue hasn't advanced; check for timeout */
1333         if (!qh->wait_expired && time_after(jiffies,
1334                         qh->advance_jiffies + QH_WAIT_TIMEOUT)) {
1335
1336                 /* Detect the Intel bug and work around it */
1337                 if (qh->post_td && qh_element(qh) ==
1338                                 cpu_to_le32(qh->post_td->dma_handle)) {
1339                         qh->element = qh->post_td->link;
1340                         qh->advance_jiffies = jiffies;
1341                         return 1;
1342                 }
1343
1344                 qh->wait_expired = 1;
1345
1346                 /* If the current URB wants FSBR, unlink it temporarily
1347                  * so that we can safely set the next TD to interrupt on
1348                  * completion.  That way we'll know as soon as the queue
1349                  * starts moving again. */
1350                 if (urbp && urbp->fsbr && !(status & TD_CTRL_IOC))
1351                         uhci_unlink_qh(uhci, qh);
1352         }
1353         return ret;
1354 }
1355
1356 /*
1357  * Process events in the schedule, but only in one thread at a time
1358  */
1359 static void uhci_scan_schedule(struct uhci_hcd *uhci, struct pt_regs *regs)
1360 {
1361         int i;
1362         struct uhci_qh *qh;
1363
1364         /* Don't allow re-entrant calls */
1365         if (uhci->scan_in_progress) {
1366                 uhci->need_rescan = 1;
1367                 return;
1368         }
1369         uhci->scan_in_progress = 1;
1370 rescan:
1371         uhci->need_rescan = 0;
1372
1373         uhci_clear_next_interrupt(uhci);
1374         uhci_get_current_frame_number(uhci);
1375
1376         /* Go through all the QH queues and process the URBs in each one */
1377         for (i = 0; i < UHCI_NUM_SKELQH - 1; ++i) {
1378                 uhci->next_qh = list_entry(uhci->skelqh[i]->node.next,
1379                                 struct uhci_qh, node);
1380                 while ((qh = uhci->next_qh) != uhci->skelqh[i]) {
1381                         uhci->next_qh = list_entry(qh->node.next,
1382                                         struct uhci_qh, node);
1383
1384                         if (uhci_advance_check(uhci, qh)) {
1385                                 uhci_scan_qh(uhci, qh, regs);
1386                                 if (qh->state == QH_STATE_ACTIVE)
1387                                         uhci_qh_wants_fsbr(uhci, qh);
1388                         }
1389                 }
1390         }
1391
1392         if (uhci->need_rescan)
1393                 goto rescan;
1394         uhci->scan_in_progress = 0;
1395
1396         if (uhci->fsbr_is_on && time_after(jiffies,
1397                         uhci->fsbr_jiffies + FSBR_OFF_DELAY))
1398                 uhci_fsbr_off(uhci);
1399
1400         if (list_empty(&uhci->skel_unlink_qh->node))
1401                 uhci_clear_next_interrupt(uhci);
1402         else
1403                 uhci_set_next_interrupt(uhci);
1404 }