blob: 76b0a9e95a7a61ad320279872bb23cc2d071d765 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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)
Alan Sternb761d9d2006-05-12 11:41:59 -040016 * (C) Copyright 2004-2006 Alan Stern, stern@rowland.harvard.edu
Linus Torvalds1da177e2005-04-16 15:20:36 -070017 */
18
Linus Torvalds1da177e2005-04-16 15:20:36 -070019
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 */
Alan Sterndccf4a42005-12-17 17:58:46 -050028static void uhci_set_next_interrupt(struct uhci_hcd *uhci)
Linus Torvalds1da177e2005-04-16 15:20:36 -070029{
Alan Stern6c1b4452005-04-21 16:04:58 -040030 if (uhci->is_stopped)
Alan Stern1f09df82005-09-05 13:59:51 -040031 mod_timer(&uhci_to_hcd(uhci)->rh_timer, jiffies);
Linus Torvalds1da177e2005-04-16 15:20:36 -070032 uhci->term_td->status |= cpu_to_le32(TD_CTRL_IOC);
33}
34
35static inline void uhci_clear_next_interrupt(struct uhci_hcd *uhci)
36{
37 uhci->term_td->status &= ~cpu_to_le32(TD_CTRL_IOC);
38}
39
Alan Stern84afddd2006-05-12 11:35:45 -040040
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 */
46static 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
53static 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
59static 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
67static 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
Alan Stern25321782005-04-25 11:14:31 -040080static struct uhci_td *uhci_alloc_td(struct uhci_hcd *uhci)
Linus Torvalds1da177e2005-04-16 15:20:36 -070081{
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;
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 td->frame = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070091
92 INIT_LIST_HEAD(&td->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 INIT_LIST_HEAD(&td->fl_list);
94
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 return td;
96}
97
Alan Sterndccf4a42005-12-17 17:58:46 -050098static 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);
Alan Sterndccf4a42005-12-17 17:58:46 -0500102 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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108static 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
Alan Stern04538a22006-05-12 11:29:04 -0400116static 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
121static void uhci_remove_td_from_urbp(struct uhci_td *td)
122{
123 list_del_init(&td->list);
124}
125
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126/*
Alan Stern687f5f32005-11-30 17:16:19 -0500127 * We insert Isochronous URBs directly into the frame list at the beginning
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 */
Alan Sterndccf4a42005-12-17 17:58:46 -0500129static inline void uhci_insert_td_in_frame_list(struct uhci_hcd *uhci,
130 struct uhci_td *td, unsigned framenum)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131{
132 framenum &= (UHCI_NUMFRAMES - 1);
133
134 td->frame = framenum;
135
136 /* Is there a TD already mapped there? */
Alan Sterna1d59ce2005-09-16 14:22:51 -0400137 if (uhci->frame_cpu[framenum]) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 struct uhci_td *ftd, *ltd;
139
Alan Sterna1d59ce2005-09-16 14:22:51 -0400140 ftd = uhci->frame_cpu[framenum];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 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 {
Alan Sterna1d59ce2005-09-16 14:22:51 -0400149 td->link = uhci->frame[framenum];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 wmb();
Alan Sterna1d59ce2005-09-16 14:22:51 -0400151 uhci->frame[framenum] = cpu_to_le32(td->dma_handle);
152 uhci->frame_cpu[framenum] = td;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 }
154}
155
Alan Sterndccf4a42005-12-17 17:58:46 -0500156static inline void uhci_remove_td_from_frame_list(struct uhci_hcd *uhci,
Alan Sternb81d3432005-10-13 17:00:24 -0400157 struct uhci_td *td)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158{
159 /* If it's not inserted, don't remove it */
Alan Sternb81d3432005-10-13 17:00:24 -0400160 if (td->frame == -1) {
161 WARN_ON(!list_empty(&td->fl_list));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 return;
Alan Sternb81d3432005-10-13 17:00:24 -0400163 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164
Alan Sternb81d3432005-10-13 17:00:24 -0400165 if (uhci->frame_cpu[td->frame] == td) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 if (list_empty(&td->fl_list)) {
Alan Sterna1d59ce2005-09-16 14:22:51 -0400167 uhci->frame[td->frame] = td->link;
168 uhci->frame_cpu[td->frame] = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 } else {
170 struct uhci_td *ntd;
171
172 ntd = list_entry(td->fl_list.next, struct uhci_td, fl_list);
Alan Sterna1d59ce2005-09-16 14:22:51 -0400173 uhci->frame[td->frame] = cpu_to_le32(ntd->dma_handle);
174 uhci->frame_cpu[td->frame] = ntd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 }
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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 list_del_init(&td->fl_list);
184 td->frame = -1;
185}
186
Alan Sterndccf4a42005-12-17 17:58:46 -0500187/*
188 * Remove all the TDs for an Isochronous URB from the frame list
189 */
190static void uhci_unlink_isochronous_tds(struct uhci_hcd *uhci, struct urb *urb)
Alan Sternb81d3432005-10-13 17:00:24 -0400191{
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)
Alan Sterndccf4a42005-12-17 17:58:46 -0500196 uhci_remove_td_from_frame_list(uhci, td);
Alan Sternb81d3432005-10-13 17:00:24 -0400197 wmb();
198}
199
Alan Sterndccf4a42005-12-17 17:58:46 -0500200static struct uhci_qh *uhci_alloc_qh(struct uhci_hcd *uhci,
201 struct usb_device *udev, struct usb_host_endpoint *hep)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202{
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
Alan Stern59e29ed2006-05-12 11:19:19 -0400210 memset(qh, 0, sizeof(*qh));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 qh->dma_handle = dma_handle;
212
213 qh->element = UHCI_PTR_TERM;
214 qh->link = UHCI_PTR_TERM;
215
Alan Sterndccf4a42005-12-17 17:58:46 -0500216 INIT_LIST_HEAD(&qh->queue);
217 INIT_LIST_HEAD(&qh->node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218
Alan Sterndccf4a42005-12-17 17:58:46 -0500219 if (udev) { /* Normal QH */
Alan Sternaf0bb592005-12-17 18:00:12 -0500220 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 }
Alan Sterndccf4a42005-12-17 17:58:46 -0500225 qh->state = QH_STATE_IDLE;
226 qh->hep = hep;
227 qh->udev = udev;
228 hep->hcpriv = qh;
Alan Stern4de7d2c2006-05-05 16:26:58 -0400229 qh->type = hep->desc.bmAttributes & USB_ENDPOINT_XFERTYPE_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230
Alan Sterndccf4a42005-12-17 17:58:46 -0500231 } else { /* Skeleton QH */
232 qh->state = QH_STATE_ACTIVE;
Alan Stern4de7d2c2006-05-05 16:26:58 -0400233 qh->type = -1;
Alan Sterndccf4a42005-12-17 17:58:46 -0500234 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 return qh;
236}
237
238static void uhci_free_qh(struct uhci_hcd *uhci, struct uhci_qh *qh)
239{
Alan Sterndccf4a42005-12-17 17:58:46 -0500240 WARN_ON(qh->state != QH_STATE_IDLE && qh->udev);
241 if (!list_empty(&qh->queue))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 dev_warn(uhci_dev(uhci), "qh %p list not empty!\n", qh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243
Alan Sterndccf4a42005-12-17 17:58:46 -0500244 list_del(&qh->node);
245 if (qh->udev) {
246 qh->hep->hcpriv = NULL;
Alan Sternaf0bb592005-12-17 18:00:12 -0500247 uhci_free_td(uhci, qh->dummy_td);
Alan Sterndccf4a42005-12-17 17:58:46 -0500248 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 dma_pool_free(uhci->qh_pool, qh, qh->dma_handle);
250}
251
252/*
Alan Sterna0b458b2006-05-12 11:23:19 -0400253 * 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).
Alan Stern0ed8fee2005-12-17 18:02:38 -0500256 */
Alan Sterna0b458b2006-05-12 11:23:19 -0400257static void uhci_cleanup_queue(struct uhci_qh *qh,
258 struct urb *urb)
Alan Stern0ed8fee2005-12-17 18:02:38 -0500259{
Alan Sterna0b458b2006-05-12 11:23:19 -0400260 struct urb_priv *urbp = urb->hcpriv;
Alan Stern0ed8fee2005-12-17 18:02:38 -0500261 struct uhci_td *td;
262
Alan Sterna0b458b2006-05-12 11:23:19 -0400263 /* 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
Alan Stern0ed8fee2005-12-17 18:02:38 -0500285 /* 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. */
Alan Sterna0b458b2006-05-12 11:23:19 -0400287 if (qh_element(qh) == UHCI_PTR_TERM)
Alan Stern0ed8fee2005-12-17 18:02:38 -0500288 return;
289 qh->element = UHCI_PTR_TERM;
290
Alan Sterna0b458b2006-05-12 11:23:19 -0400291 /* Control pipes have to worry about toggles */
292 if (qh->type == USB_ENDPOINT_XFER_CONTROL)
Alan Stern0ed8fee2005-12-17 18:02:38 -0500293 return;
294
Alan Sterna0b458b2006-05-12 11:23:19 -0400295 /* Save the next toggle value */
Alan Stern59e29ed2006-05-12 11:19:19 -0400296 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));
Alan Stern0ed8fee2005-12-17 18:02:38 -0500300}
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 */
306static 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. */
Alan Stern1393adb2006-01-31 10:02:55 -0500326 urbp = list_prepare_entry(urbp, &qh->queue, node);
Alan Stern0ed8fee2005-12-17 18:02:38 -0500327 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/*
Alan Sterndccf4a42005-12-17 17:58:46 -0500355 * Put a QH on the schedule in both hardware and software
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 */
Alan Sterndccf4a42005-12-17 17:58:46 -0500357static void uhci_activate_qh(struct uhci_hcd *uhci, struct uhci_qh *qh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358{
Alan Sterndccf4a42005-12-17 17:58:46 -0500359 struct uhci_qh *pqh;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360
Alan Sterndccf4a42005-12-17 17:58:46 -0500361 WARN_ON(list_empty(&qh->queue));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362
Alan Sterndccf4a42005-12-17 17:58:46 -0500363 /* 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370
Alan Sterndccf4a42005-12-17 17:58:46 -0500371 qh->element = cpu_to_le32(td->dma_handle);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 }
373
Alan Stern84afddd2006-05-12 11:35:45 -0400374 /* Treat the queue as if it has just advanced */
375 qh->wait_expired = 0;
376 qh->advance_jiffies = jiffies;
377
Alan Sterndccf4a42005-12-17 17:58:46 -0500378 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 */
Alan Stern0ed8fee2005-12-17 18:02:38 -0500384 if (qh == uhci->next_qh)
385 uhci->next_qh = list_entry(qh->node.next, struct uhci_qh,
386 node);
Alan Sterndccf4a42005-12-17 17:58:46 -0500387 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394}
395
396/*
Alan Sterndccf4a42005-12-17 17:58:46 -0500397 * Take a QH off the hardware schedule
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 */
Alan Sterndccf4a42005-12-17 17:58:46 -0500399static void uhci_unlink_qh(struct uhci_hcd *uhci, struct uhci_qh *qh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400{
401 struct uhci_qh *pqh;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402
Alan Sterndccf4a42005-12-17 17:58:46 -0500403 if (qh->state == QH_STATE_UNLINKING)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 return;
Alan Sterndccf4a42005-12-17 17:58:46 -0500405 WARN_ON(qh->state != QH_STATE_ACTIVE || !qh->udev);
406 qh->state = QH_STATE_UNLINKING;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407
Alan Sterndccf4a42005-12-17 17:58:46 -0500408 /* 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();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412
413 uhci_get_current_frame_number(uhci);
Alan Sterndccf4a42005-12-17 17:58:46 -0500414 qh->unlink_frame = uhci->frame_number;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415
Alan Sterndccf4a42005-12-17 17:58:46 -0500416 /* Force an interrupt so we know when the QH is fully unlinked */
417 if (list_empty(&uhci->skel_unlink_qh->node))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 uhci_set_next_interrupt(uhci);
419
Alan Sterndccf4a42005-12-17 17:58:46 -0500420 /* Move the QH from its old list to the end of the unlinking list */
Alan Stern0ed8fee2005-12-17 18:02:38 -0500421 if (qh == uhci->next_qh)
422 uhci->next_qh = list_entry(qh->node.next, struct uhci_qh,
423 node);
Alan Sterndccf4a42005-12-17 17:58:46 -0500424 list_move_tail(&qh->node, &uhci->skel_unlink_qh->node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425}
426
Alan Sterndccf4a42005-12-17 17:58:46 -0500427/*
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 */
433static void uhci_make_qh_idle(struct uhci_hcd *uhci, struct uhci_qh *qh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434{
Alan Sterndccf4a42005-12-17 17:58:46 -0500435 WARN_ON(qh->state == QH_STATE_ACTIVE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436
Alan Stern0ed8fee2005-12-17 18:02:38 -0500437 if (qh == uhci->next_qh)
438 uhci->next_qh = list_entry(qh->node.next, struct uhci_qh,
439 node);
Alan Sterndccf4a42005-12-17 17:58:46 -0500440 list_move(&qh->node, &uhci->idle_qh_list);
441 qh->state = QH_STATE_IDLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442
Alan Stern59e29ed2006-05-12 11:19:19 -0400443 /* 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
Alan Sterndccf4a42005-12-17 17:58:46 -0500449 /* If anyone is waiting for a QH to become idle, wake them up */
450 if (uhci->num_waiting)
451 wake_up_all(&uhci->waitqh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452}
453
Alan Sterndccf4a42005-12-17 17:58:46 -0500454static inline struct urb_priv *uhci_alloc_urb_priv(struct uhci_hcd *uhci,
455 struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456{
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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 urbp->urb = urb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 urb->hcpriv = urbp;
Alan Sterndccf4a42005-12-17 17:58:46 -0500467
468 INIT_LIST_HEAD(&urbp->node);
469 INIT_LIST_HEAD(&urbp->td_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470
471 return urbp;
472}
473
Alan Sterndccf4a42005-12-17 17:58:46 -0500474static void uhci_free_urb_priv(struct uhci_hcd *uhci,
475 struct urb_priv *urbp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476{
477 struct uhci_td *td, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478
Alan Sterndccf4a42005-12-17 17:58:46 -0500479 if (!list_empty(&urbp->node))
480 dev_warn(uhci_dev(uhci), "urb %p still on QH's list!\n",
481 urbp->urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 list_for_each_entry_safe(td, tmp, &urbp->td_list, list) {
Alan Stern04538a22006-05-12 11:29:04 -0400484 uhci_remove_td_from_urbp(td);
485 uhci_free_td(uhci, td);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 }
487
Alan Sterndccf4a42005-12-17 17:58:46 -0500488 urbp->urb->hcpriv = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 kmem_cache_free(uhci_up_cachep, urbp);
490}
491
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492/*
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 */
500static 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 */
Alan Sterndccf4a42005-12-17 17:58:46 -0500525static int uhci_submit_control(struct uhci_hcd *uhci, struct urb *urb,
526 struct uhci_qh *qh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 struct uhci_td *td;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 unsigned long destination, status;
Alan Sterndccf4a42005-12-17 17:58:46 -0500530 int maxsze = le16_to_cpu(qh->hep->desc.wMaxPacketSize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 int len = urb->transfer_buffer_length;
532 dma_addr_t data = urb->transfer_dma;
Alan Sterndccf4a42005-12-17 17:58:46 -0500533 __le32 *plink;
Alan Stern04538a22006-05-12 11:29:04 -0400534 struct urb_priv *urbp = urb->hcpriv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535
536 /* The "pipe" thing contains the destination in bits 8--18 */
537 destination = (urb->pipe & PIPE_DEVEP_MASK) | USB_PID_SETUP;
538
Alan Sternaf0bb592005-12-17 18:00:12 -0500539 /* 3 errors, dummy TD remains inactive */
540 status = uhci_maxerr(3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 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 */
Alan Sternaf0bb592005-12-17 18:00:12 -0500547 td = qh->dummy_td;
Alan Stern04538a22006-05-12 11:29:04 -0400548 uhci_add_td_to_urbp(td, urbp);
Alan Sternfa346562005-11-30 11:57:51 -0500549 uhci_fill_td(td, status, destination | uhci_explen(8),
Alan Sterndccf4a42005-12-17 17:58:46 -0500550 urb->setup_dma);
551 plink = &td->link;
Alan Sternaf0bb592005-12-17 18:00:12 -0500552 status |= TD_CTRL_ACTIVE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553
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 /*
Alan Stern687f5f32005-11-30 17:16:19 -0500567 * Build the DATA TDs
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 */
569 while (len > 0) {
Alan Sterndccf4a42005-12-17 17:58:46 -0500570 int pktsze = min(len, maxsze);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571
Alan Stern25321782005-04-25 11:14:31 -0400572 td = uhci_alloc_td(uhci);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 if (!td)
Alan Sternaf0bb592005-12-17 18:00:12 -0500574 goto nomem;
Alan Sterndccf4a42005-12-17 17:58:46 -0500575 *plink = cpu_to_le32(td->dma_handle);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576
577 /* Alternate Data0/1 (start with Data1) */
578 destination ^= TD_TOKEN_TOGGLE;
579
Alan Stern04538a22006-05-12 11:29:04 -0400580 uhci_add_td_to_urbp(td, urbp);
Alan Sternfa346562005-11-30 11:57:51 -0500581 uhci_fill_td(td, status, destination | uhci_explen(pktsze),
Alan Sterndccf4a42005-12-17 17:58:46 -0500582 data);
583 plink = &td->link;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584
585 data += pktsze;
586 len -= pktsze;
587 }
588
589 /*
590 * Build the final TD for control status
591 */
Alan Stern25321782005-04-25 11:14:31 -0400592 td = uhci_alloc_td(uhci);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 if (!td)
Alan Sternaf0bb592005-12-17 18:00:12 -0500594 goto nomem;
Alan Sterndccf4a42005-12-17 17:58:46 -0500595 *plink = cpu_to_le32(td->dma_handle);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596
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
Alan Stern04538a22006-05-12 11:29:04 -0400611 uhci_add_td_to_urbp(td, urbp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 uhci_fill_td(td, status | TD_CTRL_IOC,
Alan Sterndccf4a42005-12-17 17:58:46 -0500613 destination | uhci_explen(0), 0);
Alan Sternaf0bb592005-12-17 18:00:12 -0500614 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;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628
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
Alan Stern630aa3c2006-01-23 17:17:21 -0500632 * isn't in the CONFIGURED state. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 if (urb->dev->speed == USB_SPEED_LOW ||
Alan Stern630aa3c2006-01-23 17:17:21 -0500634 urb->dev->state != USB_STATE_CONFIGURED)
Alan Sterndccf4a42005-12-17 17:58:46 -0500635 qh->skel = uhci->skel_ls_control_qh;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 else {
Alan Sterndccf4a42005-12-17 17:58:46 -0500637 qh->skel = uhci->skel_fs_control_qh;
Alan Stern84afddd2006-05-12 11:35:45 -0400638 uhci_add_fsbr(uhci, urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 }
Alan Stern59e29ed2006-05-12 11:19:19 -0400640
641 urb->actual_length = -8; /* Account for the SETUP packet */
Alan Sterndccf4a42005-12-17 17:58:46 -0500642 return 0;
Alan Sternaf0bb592005-12-17 18:00:12 -0500643
644nomem:
645 /* Remove the dummy TD from the td_list so it doesn't get freed */
Alan Stern04538a22006-05-12 11:29:04 -0400646 uhci_remove_td_from_urbp(qh->dummy_td);
Alan Sternaf0bb592005-12-17 18:00:12 -0500647 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648}
649
650/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 * Common submit for bulk and interrupt
652 */
Alan Sterndccf4a42005-12-17 17:58:46 -0500653static int uhci_submit_common(struct uhci_hcd *uhci, struct urb *urb,
654 struct uhci_qh *qh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655{
656 struct uhci_td *td;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 unsigned long destination, status;
Alan Sterndccf4a42005-12-17 17:58:46 -0500658 int maxsze = le16_to_cpu(qh->hep->desc.wMaxPacketSize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 int len = urb->transfer_buffer_length;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 dma_addr_t data = urb->transfer_dma;
Alan Sternaf0bb592005-12-17 18:00:12 -0500661 __le32 *plink;
Alan Stern04538a22006-05-12 11:29:04 -0400662 struct urb_priv *urbp = urb->hcpriv;
Alan Sternaf0bb592005-12-17 18:00:12 -0500663 unsigned int toggle;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664
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);
Alan Sternaf0bb592005-12-17 18:00:12 -0500670 toggle = usb_gettoggle(urb->dev, usb_pipeendpoint(urb->pipe),
671 usb_pipeout(urb->pipe));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672
Alan Sternaf0bb592005-12-17 18:00:12 -0500673 /* 3 errors, dummy TD remains inactive */
674 status = uhci_maxerr(3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 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 /*
Alan Stern687f5f32005-11-30 17:16:19 -0500681 * Build the DATA TDs
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 */
Alan Sternaf0bb592005-12-17 18:00:12 -0500683 plink = NULL;
684 td = qh->dummy_td;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 do { /* Allow zero length packets */
686 int pktsze = maxsze;
687
Alan Sterndccf4a42005-12-17 17:58:46 -0500688 if (len <= pktsze) { /* The last packet */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 pktsze = len;
690 if (!(urb->transfer_flags & URB_SHORT_NOT_OK))
691 status &= ~TD_CTRL_SPD;
692 }
693
Alan Sternaf0bb592005-12-17 18:00:12 -0500694 if (plink) {
695 td = uhci_alloc_td(uhci);
696 if (!td)
697 goto nomem;
698 *plink = cpu_to_le32(td->dma_handle);
699 }
Alan Stern04538a22006-05-12 11:29:04 -0400700 uhci_add_td_to_urbp(td, urbp);
Alan Sterndccf4a42005-12-17 17:58:46 -0500701 uhci_fill_td(td, status,
Alan Sternaf0bb592005-12-17 18:00:12 -0500702 destination | uhci_explen(pktsze) |
703 (toggle << TD_TOKEN_TOGGLE_SHIFT),
704 data);
Alan Sterndccf4a42005-12-17 17:58:46 -0500705 plink = &td->link;
Alan Sternaf0bb592005-12-17 18:00:12 -0500706 status |= TD_CTRL_ACTIVE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707
708 data += pktsze;
709 len -= maxsze;
Alan Sternaf0bb592005-12-17 18:00:12 -0500710 toggle ^= 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 } 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 */
Alan Sterndccf4a42005-12-17 17:58:46 -0500720 if ((urb->transfer_flags & URB_ZERO_PACKET) &&
721 usb_pipeout(urb->pipe) && len == 0 &&
722 urb->transfer_buffer_length > 0) {
Alan Stern25321782005-04-25 11:14:31 -0400723 td = uhci_alloc_td(uhci);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 if (!td)
Alan Sternaf0bb592005-12-17 18:00:12 -0500725 goto nomem;
Alan Sterndccf4a42005-12-17 17:58:46 -0500726 *plink = cpu_to_le32(td->dma_handle);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727
Alan Stern04538a22006-05-12 11:29:04 -0400728 uhci_add_td_to_urbp(td, urbp);
Alan Sternaf0bb592005-12-17 18:00:12 -0500729 uhci_fill_td(td, status,
730 destination | uhci_explen(0) |
731 (toggle << TD_TOKEN_TOGGLE_SHIFT),
732 data);
733 plink = &td->link;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734
Alan Sternaf0bb592005-12-17 18:00:12 -0500735 toggle ^= 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 }
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. */
Alan Sterndccf4a42005-12-17 17:58:46 -0500744 td->status |= __constant_cpu_to_le32(TD_CTRL_IOC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745
Alan Sternaf0bb592005-12-17 18:00:12 -0500746 /*
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);
Alan Sterndccf4a42005-12-17 17:58:46 -0500761 return 0;
Alan Sternaf0bb592005-12-17 18:00:12 -0500762
763nomem:
764 /* Remove the dummy TD from the td_list so it doesn't get freed */
Alan Stern04538a22006-05-12 11:29:04 -0400765 uhci_remove_td_from_urbp(qh->dummy_td);
Alan Sternaf0bb592005-12-17 18:00:12 -0500766 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767}
768
Alan Sterndccf4a42005-12-17 17:58:46 -0500769static inline int uhci_submit_bulk(struct uhci_hcd *uhci, struct urb *urb,
770 struct uhci_qh *qh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771{
772 int ret;
773
774 /* Can't have low-speed bulk transfers */
775 if (urb->dev->speed == USB_SPEED_LOW)
776 return -EINVAL;
777
Alan Sterndccf4a42005-12-17 17:58:46 -0500778 qh->skel = uhci->skel_bulk_qh;
779 ret = uhci_submit_common(uhci, urb, qh);
780 if (ret == 0)
Alan Stern84afddd2006-05-12 11:35:45 -0400781 uhci_add_fsbr(uhci, urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 return ret;
783}
784
Alan Sterndccf4a42005-12-17 17:58:46 -0500785static inline int uhci_submit_interrupt(struct uhci_hcd *uhci, struct urb *urb,
786 struct uhci_qh *qh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787{
Alan Sterndccf4a42005-12-17 17:58:46 -0500788 /* 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.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 */
Alan Sterndccf4a42005-12-17 17:58:46 -0500792 qh->skel = uhci->skelqh[__interval_to_skel(urb->interval)];
793 return uhci_submit_common(uhci, urb, qh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794}
795
796/*
Alan Sternb1869002006-05-12 11:14:25 -0400797 * Fix up the data structures following a short transfer
798 */
799static int uhci_fixup_short_transfer(struct uhci_hcd *uhci,
Alan Stern59e29ed2006-05-12 11:19:19 -0400800 struct uhci_qh *qh, struct urb_priv *urbp)
Alan Sternb1869002006-05-12 11:14:25 -0400801{
802 struct uhci_td *td;
Alan Stern59e29ed2006-05-12 11:19:19 -0400803 struct list_head *tmp;
804 int ret;
Alan Sternb1869002006-05-12 11:14:25 -0400805
806 td = list_entry(urbp->td_list.prev, struct uhci_td, list);
807 if (qh->type == USB_ENDPOINT_XFER_CONTROL) {
Alan Sternb1869002006-05-12 11:14:25 -0400808
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. */
Alan Stern59e29ed2006-05-12 11:19:19 -0400812 WARN_ON(list_empty(&urbp->td_list));
Alan Sternb1869002006-05-12 11:14:25 -0400813 qh->element = cpu_to_le32(td->dma_handle);
Alan Stern59e29ed2006-05-12 11:19:19 -0400814 tmp = td->list.prev;
Alan Sternb1869002006-05-12 11:14:25 -0400815 ret = -EINPROGRESS;
816
Alan Stern59e29ed2006-05-12 11:19:19 -0400817 } else {
Alan Sternb1869002006-05-12 11:14:25 -0400818
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. */
Alan Stern59e29ed2006-05-12 11:19:19 -0400822 qh->initial_toggle = uhci_toggle(td_token(qh->post_td)) ^ 1;
Alan Sternb1869002006-05-12 11:14:25 -0400823 uhci_fixup_toggles(qh, 1);
824
Alan Stern59e29ed2006-05-12 11:19:19 -0400825 if (list_empty(&urbp->td_list))
826 td = qh->post_td;
Alan Sternb1869002006-05-12 11:14:25 -0400827 qh->element = td->link;
Alan Stern59e29ed2006-05-12 11:19:19 -0400828 tmp = urbp->td_list.prev;
829 ret = 0;
Alan Sternb1869002006-05-12 11:14:25 -0400830 }
831
Alan Stern59e29ed2006-05-12 11:19:19 -0400832 /* 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
Alan Stern04538a22006-05-12 11:29:04 -0400837 uhci_remove_td_from_urbp(td);
838 uhci_free_td(uhci, td);
Alan Stern59e29ed2006-05-12 11:19:19 -0400839 }
Alan Sternb1869002006-05-12 11:14:25 -0400840 return ret;
841}
842
843/*
844 * Common result for control, bulk, and interrupt
845 */
846static 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;
Alan Stern59e29ed2006-05-12 11:19:19 -0400850 struct uhci_td *td, *tmp;
Alan Sternb1869002006-05-12 11:14:25 -0400851 unsigned status;
852 int ret = 0;
853
Alan Stern59e29ed2006-05-12 11:19:19 -0400854 list_for_each_entry_safe(td, tmp, &urbp->td_list, list) {
Alan Sternb1869002006-05-12 11:14:25 -0400855 unsigned int ctrlstat;
856 int len;
857
Alan Sternb1869002006-05-12 11:14:25 -0400858 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
Alan Stern04538a22006-05-12 11:29:04 -0400892 uhci_remove_td_from_urbp(td);
Alan Stern59e29ed2006-05-12 11:19:19 -0400893 if (qh->post_td)
Alan Stern04538a22006-05-12 11:29:04 -0400894 uhci_free_td(uhci, qh->post_td);
Alan Stern59e29ed2006-05-12 11:19:19 -0400895 qh->post_td = td;
896
Alan Sternb1869002006-05-12 11:14:25 -0400897 if (ret != 0)
898 goto err;
899 }
900 return ret;
901
902err:
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 */
Alan Stern59e29ed2006-05-12 11:19:19 -0400917 ret = uhci_fixup_short_transfer(uhci, qh, urbp);
Alan Sternb1869002006-05-12 11:14:25 -0400918 return ret;
919}
920
921/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922 * Isochronous transfers
923 */
Alan Sterndccf4a42005-12-17 17:58:46 -0500924static int uhci_submit_isochronous(struct uhci_hcd *uhci, struct urb *urb,
925 struct uhci_qh *qh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926{
Alan Sterndccf4a42005-12-17 17:58:46 -0500927 struct uhci_td *td = NULL; /* Since urb->number_of_packets > 0 */
Alan Stern0ed8fee2005-12-17 18:02:38 -0500928 int i, frame;
Alan Sterndccf4a42005-12-17 17:58:46 -0500929 unsigned long destination, status;
Alan Sternb81d3432005-10-13 17:00:24 -0400930 struct urb_priv *urbp = (struct urb_priv *) urb->hcpriv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931
Alan Stern0ed8fee2005-12-17 18:02:38 -0500932 if (urb->number_of_packets > 900) /* 900? Why? */
933 return -EFBIG;
934
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935 status = TD_CTRL_ACTIVE | TD_CTRL_IOS;
936 destination = (urb->pipe & PIPE_DEVEP_MASK) | usb_packetid(urb->pipe);
937
Alan Stern0ed8fee2005-12-17 18:02:38 -0500938 /* 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957
Alan Sternb81d3432005-10-13 17:00:24 -0400958 for (i = 0; i < urb->number_of_packets; i++) {
Alan Stern25321782005-04-25 11:14:31 -0400959 td = uhci_alloc_td(uhci);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960 if (!td)
961 return -ENOMEM;
962
Alan Stern04538a22006-05-12 11:29:04 -0400963 uhci_add_td_to_urbp(td, urbp);
Alan Sterndccf4a42005-12-17 17:58:46 -0500964 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);
Alan Sternb81d3432005-10-13 17:00:24 -0400968 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969
Alan Sterndccf4a42005-12-17 17:58:46 -0500970 /* 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 */
Alan Sternb81d3432005-10-13 17:00:24 -0400976 frame = urb->start_frame;
977 list_for_each_entry(td, &urbp->td_list, list) {
Alan Sterndccf4a42005-12-17 17:58:46 -0500978 uhci_insert_td_in_frame_list(uhci, td, frame);
Alan Sternb81d3432005-10-13 17:00:24 -0400979 frame += urb->interval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980 }
981
Alan Sterndccf4a42005-12-17 17:58:46 -0500982 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983}
984
985static 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
Alan Sternb81d3432005-10-13 17:00:24 -0400992 urb->actual_length = urb->error_count = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993
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
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020static int uhci_urb_enqueue(struct usb_hcd *hcd,
Alan Sterndccf4a42005-12-17 17:58:46 -05001021 struct usb_host_endpoint *hep,
Al Viro55016f12005-10-21 03:21:58 -04001022 struct urb *urb, gfp_t mem_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023{
1024 int ret;
1025 struct uhci_hcd *uhci = hcd_to_uhci(hcd);
1026 unsigned long flags;
Alan Sterndccf4a42005-12-17 17:58:46 -05001027 struct urb_priv *urbp;
1028 struct uhci_qh *qh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029 int bustime;
1030
1031 spin_lock_irqsave(&uhci->lock, flags);
1032
1033 ret = urb->status;
1034 if (ret != -EINPROGRESS) /* URB already unlinked! */
Alan Sterndccf4a42005-12-17 17:58:46 -05001035 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036
Alan Sterndccf4a42005-12-17 17:58:46 -05001037 ret = -ENOMEM;
1038 urbp = uhci_alloc_urb_priv(uhci, urb);
1039 if (!urbp)
1040 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041
Alan Sterndccf4a42005-12-17 17:58:46 -05001042 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;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048 }
Alan Sterndccf4a42005-12-17 17:58:46 -05001049 urbp->qh = qh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050
Alan Stern4de7d2c2006-05-05 16:26:58 -04001051 switch (qh->type) {
1052 case USB_ENDPOINT_XFER_CONTROL:
Alan Sterndccf4a42005-12-17 17:58:46 -05001053 ret = uhci_submit_control(uhci, urb, qh);
1054 break;
Alan Stern4de7d2c2006-05-05 16:26:58 -04001055 case USB_ENDPOINT_XFER_BULK:
Alan Sterndccf4a42005-12-17 17:58:46 -05001056 ret = uhci_submit_bulk(uhci, urb, qh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 break;
Alan Stern4de7d2c2006-05-05 16:26:58 -04001058 case USB_ENDPOINT_XFER_INT:
Alan Sterndccf4a42005-12-17 17:58:46 -05001059 if (list_empty(&qh->queue)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060 bustime = usb_check_bandwidth(urb->dev, urb);
1061 if (bustime < 0)
1062 ret = bustime;
1063 else {
Alan Sterndccf4a42005-12-17 17:58:46 -05001064 ret = uhci_submit_interrupt(uhci, urb, qh);
1065 if (ret == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066 usb_claim_bandwidth(urb->dev, urb, bustime, 0);
1067 }
1068 } else { /* inherit from parent */
Alan Sterndccf4a42005-12-17 17:58:46 -05001069 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075 }
1076 break;
Alan Stern4de7d2c2006-05-05 16:26:58 -04001077 case USB_ENDPOINT_XFER_ISOC:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078 bustime = usb_check_bandwidth(urb->dev, urb);
1079 if (bustime < 0) {
1080 ret = bustime;
1081 break;
1082 }
1083
Alan Sterndccf4a42005-12-17 17:58:46 -05001084 ret = uhci_submit_isochronous(uhci, urb, qh);
1085 if (ret == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086 usb_claim_bandwidth(urb->dev, urb, bustime, 1);
1087 break;
1088 }
Alan Sterndccf4a42005-12-17 17:58:46 -05001089 if (ret != 0)
1090 goto err_submit_failed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091
Alan Sterndccf4a42005-12-17 17:58:46 -05001092 /* Add this URB to the QH */
1093 urbp->qh = qh;
1094 list_add_tail(&urbp->node, &qh->queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095
Alan Sterndccf4a42005-12-17 17:58:46 -05001096 /* 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
Alan Stern27755622006-05-05 16:32:02 -04001098 * become idle, so we can activate it right away. But only if the
1099 * queue isn't stopped. */
Alan Stern84afddd2006-05-12 11:35:45 -04001100 if (qh->queue.next == &urbp->node && !qh->is_stopped) {
Alan Sterndccf4a42005-12-17 17:58:46 -05001101 uhci_activate_qh(uhci, qh);
Alan Stern84afddd2006-05-12 11:35:45 -04001102 uhci_qh_wants_fsbr(uhci, qh);
1103 }
Alan Sterndccf4a42005-12-17 17:58:46 -05001104 goto done;
1105
1106err_submit_failed:
1107 if (qh->state == QH_STATE_IDLE)
1108 uhci_make_qh_idle(uhci, qh); /* Reclaim unused QH */
1109
1110err_no_qh:
1111 uhci_free_urb_priv(uhci, urbp);
1112
1113done:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114 spin_unlock_irqrestore(&uhci->lock, flags);
1115 return ret;
1116}
1117
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118static 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;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128
Alan Sterndccf4a42005-12-17 17:58:46 -05001129 /* Remove Isochronous TDs from the frame list ASAP */
Alan Stern4de7d2c2006-05-05 16:26:58 -04001130 if (urbp->qh->type == USB_ENDPOINT_XFER_ISOC)
Alan Sterndccf4a42005-12-17 17:58:46 -05001131 uhci_unlink_isochronous_tds(uhci, urb);
1132 uhci_unlink_qh(uhci, urbp->qh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133
1134done:
1135 spin_unlock_irqrestore(&uhci->lock, flags);
1136 return 0;
1137}
1138
Alan Stern0ed8fee2005-12-17 18:02:38 -05001139/*
1140 * Finish unlinking an URB and give it back
1141 */
1142static 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)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146{
Alan Stern0ed8fee2005-12-17 18:02:38 -05001147 struct urb_priv *urbp = (struct urb_priv *) urb->hcpriv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148
Alan Stern0ed8fee2005-12-17 18:02:38 -05001149 /* Isochronous TDs get unlinked directly from the frame list */
Alan Stern4de7d2c2006-05-05 16:26:58 -04001150 if (qh->type == USB_ENDPOINT_XFER_ISOC)
Alan Stern0ed8fee2005-12-17 18:02:38 -05001151 uhci_unlink_isochronous_tds(uhci, urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152
Alan Stern0ed8fee2005-12-17 18:02:38 -05001153 /* 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
Alan Stern0ed8fee2005-12-17 18:02:38 -05001162 uhci_free_urb_priv(uhci, urbp);
1163
Alan Stern4de7d2c2006-05-05 16:26:58 -04001164 switch (qh->type) {
1165 case USB_ENDPOINT_XFER_ISOC:
Alan Stern0ed8fee2005-12-17 18:02:38 -05001166 /* Release bandwidth for Interrupt or Isoc. transfers */
1167 if (urb->bandwidth)
1168 usb_release_bandwidth(urb->dev, urb, 1);
1169 break;
Alan Stern4de7d2c2006-05-05 16:26:58 -04001170 case USB_ENDPOINT_XFER_INT:
Alan Stern0ed8fee2005-12-17 18:02:38 -05001171 /* 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
1202static 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
Alan Sternb1869002006-05-12 11:14:25 -04001213 if (qh->type == USB_ENDPOINT_XFER_ISOC)
Alan Stern0ed8fee2005-12-17 18:02:38 -05001214 status = uhci_result_isochronous(uhci, urb);
Alan Sternb1869002006-05-12 11:14:25 -04001215 else
Alan Stern0ed8fee2005-12-17 18:02:38 -05001216 status = uhci_result_common(uhci, urb);
Alan Stern0ed8fee2005-12-17 18:02:38 -05001217 if (status == -EINPROGRESS)
1218 break;
1219
1220 spin_lock(&urb->lock);
1221 if (urb->status == -EINPROGRESS) /* Not dequeued */
1222 urb->status = status;
1223 else
Alan Stern27755622006-05-05 16:32:02 -04001224 status = ECONNRESET; /* Not -ECONNRESET */
Alan Stern0ed8fee2005-12-17 18:02:38 -05001225 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. */
Alan Stern27755622006-05-05 16:32:02 -04001229 if (status == ECONNRESET) {
1230 if (QH_FINISHED_UNLINKING(qh))
1231 qh->is_stopped = 1;
1232 else if (!qh->is_stopped)
1233 return;
1234 }
Alan Stern0ed8fee2005-12-17 18:02:38 -05001235
1236 uhci_giveback_urb(uhci, qh, urb, regs);
Alan Stern27755622006-05-05 16:32:02 -04001237 if (status < 0)
Alan Stern0ed8fee2005-12-17 18:02:38 -05001238 break;
1239 }
1240
1241 /* If the QH is neither stopped nor finished unlinking (normal case),
1242 * our work here is done. */
Alan Stern27755622006-05-05 16:32:02 -04001243 if (QH_FINISHED_UNLINKING(qh))
1244 qh->is_stopped = 1;
1245 else if (!qh->is_stopped)
Alan Stern0ed8fee2005-12-17 18:02:38 -05001246 return;
1247
1248 /* Otherwise give back each of the dequeued URBs */
Alan Stern27755622006-05-05 16:32:02 -04001249restart:
Alan Stern0ed8fee2005-12-17 18:02:38 -05001250 list_for_each_entry(urbp, &qh->queue, node) {
1251 urb = urbp->urb;
1252 if (urb->status != -EINPROGRESS) {
Alan Sterna0b458b2006-05-12 11:23:19 -04001253 uhci_cleanup_queue(qh, urb);
Alan Stern0ed8fee2005-12-17 18:02:38 -05001254 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);
Alan Stern84afddd2006-05-12 11:35:45 -04001265
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
Alan Stern0ed8fee2005-12-17 18:02:38 -05001277 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284}
1285
Alan Stern0ed8fee2005-12-17 18:02:38 -05001286/*
Alan Stern84afddd2006-05-12 11:35:45 -04001287 * 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.
Alan Sternb761d9d2006-05-12 11:41:59 -04001290 *
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.
Alan Stern84afddd2006-05-12 11:35:45 -04001295 */
1296static 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)) {
Alan Sternb761d9d2006-05-12 11:41:59 -04001335
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
Alan Stern84afddd2006-05-12 11:35:45 -04001344 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/*
Alan Stern0ed8fee2005-12-17 18:02:38 -05001357 * Process events in the schedule, but only in one thread at a time
1358 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359static void uhci_scan_schedule(struct uhci_hcd *uhci, struct pt_regs *regs)
1360{
Alan Stern0ed8fee2005-12-17 18:02:38 -05001361 int i;
1362 struct uhci_qh *qh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363
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;
Alan Stern84afddd2006-05-12 11:35:45 -04001370rescan:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371 uhci->need_rescan = 0;
1372
Alan Stern6c1b4452005-04-21 16:04:58 -04001373 uhci_clear_next_interrupt(uhci);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374 uhci_get_current_frame_number(uhci);
1375
Alan Stern0ed8fee2005-12-17 18:02:38 -05001376 /* 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);
Alan Stern84afddd2006-05-12 11:35:45 -04001383
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 }
Alan Stern0ed8fee2005-12-17 18:02:38 -05001389 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391
1392 if (uhci->need_rescan)
1393 goto rescan;
1394 uhci->scan_in_progress = 0;
1395
Alan Stern84afddd2006-05-12 11:35:45 -04001396 if (uhci->fsbr_is_on && time_after(jiffies,
1397 uhci->fsbr_jiffies + FSBR_OFF_DELAY))
1398 uhci_fsbr_off(uhci);
1399
Alan Stern04538a22006-05-12 11:29:04 -04001400 if (list_empty(&uhci->skel_unlink_qh->node))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401 uhci_clear_next_interrupt(uhci);
1402 else
1403 uhci_set_next_interrupt(uhci);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404}