blob: 96ce4c87c8719570351c17a1deca295c3e6d45a6 [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}
198
Alan Sterndccf4a42005-12-17 17:58:46 -0500199static struct uhci_qh *uhci_alloc_qh(struct uhci_hcd *uhci,
200 struct usb_device *udev, struct usb_host_endpoint *hep)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201{
202 dma_addr_t dma_handle;
203 struct uhci_qh *qh;
204
205 qh = dma_pool_alloc(uhci->qh_pool, GFP_ATOMIC, &dma_handle);
206 if (!qh)
207 return NULL;
208
Alan Stern59e29ed2006-05-12 11:19:19 -0400209 memset(qh, 0, sizeof(*qh));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 qh->dma_handle = dma_handle;
211
212 qh->element = UHCI_PTR_TERM;
213 qh->link = UHCI_PTR_TERM;
214
Alan Sterndccf4a42005-12-17 17:58:46 -0500215 INIT_LIST_HEAD(&qh->queue);
216 INIT_LIST_HEAD(&qh->node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217
Alan Sterndccf4a42005-12-17 17:58:46 -0500218 if (udev) { /* Normal QH */
Alan Sternaf0bb592005-12-17 18:00:12 -0500219 qh->dummy_td = uhci_alloc_td(uhci);
220 if (!qh->dummy_td) {
221 dma_pool_free(uhci->qh_pool, qh, dma_handle);
222 return NULL;
223 }
Alan Sterndccf4a42005-12-17 17:58:46 -0500224 qh->state = QH_STATE_IDLE;
225 qh->hep = hep;
226 qh->udev = udev;
227 hep->hcpriv = qh;
Alan Stern4de7d2c2006-05-05 16:26:58 -0400228 qh->type = hep->desc.bmAttributes & USB_ENDPOINT_XFERTYPE_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229
Alan Sterndccf4a42005-12-17 17:58:46 -0500230 } else { /* Skeleton QH */
231 qh->state = QH_STATE_ACTIVE;
Alan Stern4de7d2c2006-05-05 16:26:58 -0400232 qh->type = -1;
Alan Sterndccf4a42005-12-17 17:58:46 -0500233 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 return qh;
235}
236
237static void uhci_free_qh(struct uhci_hcd *uhci, struct uhci_qh *qh)
238{
Alan Sterndccf4a42005-12-17 17:58:46 -0500239 WARN_ON(qh->state != QH_STATE_IDLE && qh->udev);
240 if (!list_empty(&qh->queue))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 dev_warn(uhci_dev(uhci), "qh %p list not empty!\n", qh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242
Alan Sterndccf4a42005-12-17 17:58:46 -0500243 list_del(&qh->node);
244 if (qh->udev) {
245 qh->hep->hcpriv = NULL;
Alan Sternaf0bb592005-12-17 18:00:12 -0500246 uhci_free_td(uhci, qh->dummy_td);
Alan Sterndccf4a42005-12-17 17:58:46 -0500247 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 dma_pool_free(uhci->qh_pool, qh, qh->dma_handle);
249}
250
251/*
Alan Sterna0b458b2006-05-12 11:23:19 -0400252 * When a queue is stopped and a dequeued URB is given back, adjust
253 * the previous TD link (if the URB isn't first on the queue) or
254 * save its toggle value (if it is first and is currently executing).
Alan Stern10b8e472006-05-19 16:39:52 -0400255 *
256 * Returns 0 if the URB should not yet be given back, 1 otherwise.
Alan Stern0ed8fee2005-12-17 18:02:38 -0500257 */
Alan Stern10b8e472006-05-19 16:39:52 -0400258static int uhci_cleanup_queue(struct uhci_hcd *uhci, struct uhci_qh *qh,
Alan Sterna0b458b2006-05-12 11:23:19 -0400259 struct urb *urb)
Alan Stern0ed8fee2005-12-17 18:02:38 -0500260{
Alan Sterna0b458b2006-05-12 11:23:19 -0400261 struct urb_priv *urbp = urb->hcpriv;
Alan Stern0ed8fee2005-12-17 18:02:38 -0500262 struct uhci_td *td;
Alan Stern10b8e472006-05-19 16:39:52 -0400263 int ret = 1;
Alan Stern0ed8fee2005-12-17 18:02:38 -0500264
Alan Sterna0b458b2006-05-12 11:23:19 -0400265 /* Isochronous pipes don't use toggles and their TD link pointers
Alan Stern10b8e472006-05-19 16:39:52 -0400266 * get adjusted during uhci_urb_dequeue(). But since their queues
267 * cannot truly be stopped, we have to watch out for dequeues
268 * occurring after the nominal unlink frame. */
269 if (qh->type == USB_ENDPOINT_XFER_ISOC) {
270 ret = (uhci->frame_number + uhci->is_stopped !=
271 qh->unlink_frame);
272 return ret;
273 }
Alan Sterna0b458b2006-05-12 11:23:19 -0400274
275 /* If the URB isn't first on its queue, adjust the link pointer
276 * of the last TD in the previous URB. The toggle doesn't need
277 * to be saved since this URB can't be executing yet. */
278 if (qh->queue.next != &urbp->node) {
279 struct urb_priv *purbp;
280 struct uhci_td *ptd;
281
282 purbp = list_entry(urbp->node.prev, struct urb_priv, node);
283 WARN_ON(list_empty(&purbp->td_list));
284 ptd = list_entry(purbp->td_list.prev, struct uhci_td,
285 list);
286 td = list_entry(urbp->td_list.prev, struct uhci_td,
287 list);
288 ptd->link = td->link;
Alan Stern10b8e472006-05-19 16:39:52 -0400289 return ret;
Alan Sterna0b458b2006-05-12 11:23:19 -0400290 }
291
Alan Stern0ed8fee2005-12-17 18:02:38 -0500292 /* If the QH element pointer is UHCI_PTR_TERM then then currently
293 * executing URB has already been unlinked, so this one isn't it. */
Alan Sterna0b458b2006-05-12 11:23:19 -0400294 if (qh_element(qh) == UHCI_PTR_TERM)
Alan Stern10b8e472006-05-19 16:39:52 -0400295 return ret;
Alan Stern0ed8fee2005-12-17 18:02:38 -0500296 qh->element = UHCI_PTR_TERM;
297
Alan Sterna0b458b2006-05-12 11:23:19 -0400298 /* Control pipes have to worry about toggles */
299 if (qh->type == USB_ENDPOINT_XFER_CONTROL)
Alan Stern10b8e472006-05-19 16:39:52 -0400300 return ret;
Alan Stern0ed8fee2005-12-17 18:02:38 -0500301
Alan Sterna0b458b2006-05-12 11:23:19 -0400302 /* Save the next toggle value */
Alan Stern59e29ed2006-05-12 11:19:19 -0400303 WARN_ON(list_empty(&urbp->td_list));
304 td = list_entry(urbp->td_list.next, struct uhci_td, list);
305 qh->needs_fixup = 1;
306 qh->initial_toggle = uhci_toggle(td_token(td));
Alan Stern10b8e472006-05-19 16:39:52 -0400307 return ret;
Alan Stern0ed8fee2005-12-17 18:02:38 -0500308}
309
310/*
311 * Fix up the data toggles for URBs in a queue, when one of them
312 * terminates early (short transfer, error, or dequeued).
313 */
314static void uhci_fixup_toggles(struct uhci_qh *qh, int skip_first)
315{
316 struct urb_priv *urbp = NULL;
317 struct uhci_td *td;
318 unsigned int toggle = qh->initial_toggle;
319 unsigned int pipe;
320
321 /* Fixups for a short transfer start with the second URB in the
322 * queue (the short URB is the first). */
323 if (skip_first)
324 urbp = list_entry(qh->queue.next, struct urb_priv, node);
325
326 /* When starting with the first URB, if the QH element pointer is
327 * still valid then we know the URB's toggles are okay. */
328 else if (qh_element(qh) != UHCI_PTR_TERM)
329 toggle = 2;
330
331 /* Fix up the toggle for the URBs in the queue. Normally this
332 * loop won't run more than once: When an error or short transfer
333 * occurs, the queue usually gets emptied. */
Alan Stern1393adb2006-01-31 10:02:55 -0500334 urbp = list_prepare_entry(urbp, &qh->queue, node);
Alan Stern0ed8fee2005-12-17 18:02:38 -0500335 list_for_each_entry_continue(urbp, &qh->queue, node) {
336
337 /* If the first TD has the right toggle value, we don't
338 * need to change any toggles in this URB */
339 td = list_entry(urbp->td_list.next, struct uhci_td, list);
340 if (toggle > 1 || uhci_toggle(td_token(td)) == toggle) {
341 td = list_entry(urbp->td_list.next, struct uhci_td,
342 list);
343 toggle = uhci_toggle(td_token(td)) ^ 1;
344
345 /* Otherwise all the toggles in the URB have to be switched */
346 } else {
347 list_for_each_entry(td, &urbp->td_list, list) {
348 td->token ^= __constant_cpu_to_le32(
349 TD_TOKEN_TOGGLE);
350 toggle ^= 1;
351 }
352 }
353 }
354
355 wmb();
356 pipe = list_entry(qh->queue.next, struct urb_priv, node)->urb->pipe;
357 usb_settoggle(qh->udev, usb_pipeendpoint(pipe),
358 usb_pipeout(pipe), toggle);
359 qh->needs_fixup = 0;
360}
361
362/*
Alan Sterndccf4a42005-12-17 17:58:46 -0500363 * Put a QH on the schedule in both hardware and software
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 */
Alan Sterndccf4a42005-12-17 17:58:46 -0500365static void uhci_activate_qh(struct uhci_hcd *uhci, struct uhci_qh *qh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366{
Alan Sterndccf4a42005-12-17 17:58:46 -0500367 struct uhci_qh *pqh;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368
Alan Sterndccf4a42005-12-17 17:58:46 -0500369 WARN_ON(list_empty(&qh->queue));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370
Alan Sterndccf4a42005-12-17 17:58:46 -0500371 /* Set the element pointer if it isn't set already.
372 * This isn't needed for Isochronous queues, but it doesn't hurt. */
373 if (qh_element(qh) == UHCI_PTR_TERM) {
374 struct urb_priv *urbp = list_entry(qh->queue.next,
375 struct urb_priv, node);
376 struct uhci_td *td = list_entry(urbp->td_list.next,
377 struct uhci_td, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378
Alan Sterndccf4a42005-12-17 17:58:46 -0500379 qh->element = cpu_to_le32(td->dma_handle);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 }
381
Alan Stern84afddd2006-05-12 11:35:45 -0400382 /* Treat the queue as if it has just advanced */
383 qh->wait_expired = 0;
384 qh->advance_jiffies = jiffies;
385
Alan Sterndccf4a42005-12-17 17:58:46 -0500386 if (qh->state == QH_STATE_ACTIVE)
387 return;
388 qh->state = QH_STATE_ACTIVE;
389
390 /* Move the QH from its old list to the end of the appropriate
391 * skeleton's list */
Alan Stern0ed8fee2005-12-17 18:02:38 -0500392 if (qh == uhci->next_qh)
393 uhci->next_qh = list_entry(qh->node.next, struct uhci_qh,
394 node);
Alan Sterndccf4a42005-12-17 17:58:46 -0500395 list_move_tail(&qh->node, &qh->skel->node);
396
397 /* Link it into the schedule */
398 pqh = list_entry(qh->node.prev, struct uhci_qh, node);
399 qh->link = pqh->link;
400 wmb();
401 pqh->link = UHCI_PTR_QH | cpu_to_le32(qh->dma_handle);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402}
403
404/*
Alan Sterndccf4a42005-12-17 17:58:46 -0500405 * Take a QH off the hardware schedule
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 */
Alan Sterndccf4a42005-12-17 17:58:46 -0500407static void uhci_unlink_qh(struct uhci_hcd *uhci, struct uhci_qh *qh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408{
409 struct uhci_qh *pqh;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410
Alan Sterndccf4a42005-12-17 17:58:46 -0500411 if (qh->state == QH_STATE_UNLINKING)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 return;
Alan Sterndccf4a42005-12-17 17:58:46 -0500413 WARN_ON(qh->state != QH_STATE_ACTIVE || !qh->udev);
414 qh->state = QH_STATE_UNLINKING;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415
Alan Sterndccf4a42005-12-17 17:58:46 -0500416 /* Unlink the QH from the schedule and record when we did it */
417 pqh = list_entry(qh->node.prev, struct uhci_qh, node);
418 pqh->link = qh->link;
419 mb();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420
421 uhci_get_current_frame_number(uhci);
Alan Sterndccf4a42005-12-17 17:58:46 -0500422 qh->unlink_frame = uhci->frame_number;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423
Alan Sterndccf4a42005-12-17 17:58:46 -0500424 /* Force an interrupt so we know when the QH is fully unlinked */
425 if (list_empty(&uhci->skel_unlink_qh->node))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 uhci_set_next_interrupt(uhci);
427
Alan Sterndccf4a42005-12-17 17:58:46 -0500428 /* Move the QH from its old list to the end of the unlinking list */
Alan Stern0ed8fee2005-12-17 18:02:38 -0500429 if (qh == uhci->next_qh)
430 uhci->next_qh = list_entry(qh->node.next, struct uhci_qh,
431 node);
Alan Sterndccf4a42005-12-17 17:58:46 -0500432 list_move_tail(&qh->node, &uhci->skel_unlink_qh->node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433}
434
Alan Sterndccf4a42005-12-17 17:58:46 -0500435/*
436 * When we and the controller are through with a QH, it becomes IDLE.
437 * This happens when a QH has been off the schedule (on the unlinking
438 * list) for more than one frame, or when an error occurs while adding
439 * the first URB onto a new QH.
440 */
441static void uhci_make_qh_idle(struct uhci_hcd *uhci, struct uhci_qh *qh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442{
Alan Sterndccf4a42005-12-17 17:58:46 -0500443 WARN_ON(qh->state == QH_STATE_ACTIVE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444
Alan Stern0ed8fee2005-12-17 18:02:38 -0500445 if (qh == uhci->next_qh)
446 uhci->next_qh = list_entry(qh->node.next, struct uhci_qh,
447 node);
Alan Sterndccf4a42005-12-17 17:58:46 -0500448 list_move(&qh->node, &uhci->idle_qh_list);
449 qh->state = QH_STATE_IDLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450
Alan Stern59e29ed2006-05-12 11:19:19 -0400451 /* Now that the QH is idle, its post_td isn't being used */
452 if (qh->post_td) {
453 uhci_free_td(uhci, qh->post_td);
454 qh->post_td = NULL;
455 }
456
Alan Sterndccf4a42005-12-17 17:58:46 -0500457 /* If anyone is waiting for a QH to become idle, wake them up */
458 if (uhci->num_waiting)
459 wake_up_all(&uhci->waitqh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460}
461
Alan Sterndccf4a42005-12-17 17:58:46 -0500462static inline struct urb_priv *uhci_alloc_urb_priv(struct uhci_hcd *uhci,
463 struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464{
465 struct urb_priv *urbp;
466
467 urbp = kmem_cache_alloc(uhci_up_cachep, SLAB_ATOMIC);
468 if (!urbp)
469 return NULL;
470
471 memset((void *)urbp, 0, sizeof(*urbp));
472
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 urbp->urb = urb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 urb->hcpriv = urbp;
Alan Sterndccf4a42005-12-17 17:58:46 -0500475
476 INIT_LIST_HEAD(&urbp->node);
477 INIT_LIST_HEAD(&urbp->td_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478
479 return urbp;
480}
481
Alan Sterndccf4a42005-12-17 17:58:46 -0500482static void uhci_free_urb_priv(struct uhci_hcd *uhci,
483 struct urb_priv *urbp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484{
485 struct uhci_td *td, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486
Alan Sterndccf4a42005-12-17 17:58:46 -0500487 if (!list_empty(&urbp->node))
488 dev_warn(uhci_dev(uhci), "urb %p still on QH's list!\n",
489 urbp->urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 list_for_each_entry_safe(td, tmp, &urbp->td_list, list) {
Alan Stern04538a22006-05-12 11:29:04 -0400492 uhci_remove_td_from_urbp(td);
493 uhci_free_td(uhci, td);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 }
495
Alan Sterndccf4a42005-12-17 17:58:46 -0500496 urbp->urb->hcpriv = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 kmem_cache_free(uhci_up_cachep, urbp);
498}
499
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500/*
501 * Map status to standard result codes
502 *
503 * <status> is (td_status(td) & 0xF60000), a.k.a.
504 * uhci_status_bits(td_status(td)).
505 * Note: <status> does not include the TD_CTRL_NAK bit.
506 * <dir_out> is True for output TDs and False for input TDs.
507 */
508static int uhci_map_status(int status, int dir_out)
509{
510 if (!status)
511 return 0;
512 if (status & TD_CTRL_BITSTUFF) /* Bitstuff error */
513 return -EPROTO;
514 if (status & TD_CTRL_CRCTIMEO) { /* CRC/Timeout */
515 if (dir_out)
516 return -EPROTO;
517 else
518 return -EILSEQ;
519 }
520 if (status & TD_CTRL_BABBLE) /* Babble */
521 return -EOVERFLOW;
522 if (status & TD_CTRL_DBUFERR) /* Buffer error */
523 return -ENOSR;
524 if (status & TD_CTRL_STALLED) /* Stalled */
525 return -EPIPE;
526 WARN_ON(status & TD_CTRL_ACTIVE); /* Active */
527 return 0;
528}
529
530/*
531 * Control transfers
532 */
Alan Sterndccf4a42005-12-17 17:58:46 -0500533static int uhci_submit_control(struct uhci_hcd *uhci, struct urb *urb,
534 struct uhci_qh *qh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 struct uhci_td *td;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 unsigned long destination, status;
Alan Sterndccf4a42005-12-17 17:58:46 -0500538 int maxsze = le16_to_cpu(qh->hep->desc.wMaxPacketSize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 int len = urb->transfer_buffer_length;
540 dma_addr_t data = urb->transfer_dma;
Alan Sterndccf4a42005-12-17 17:58:46 -0500541 __le32 *plink;
Alan Stern04538a22006-05-12 11:29:04 -0400542 struct urb_priv *urbp = urb->hcpriv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543
544 /* The "pipe" thing contains the destination in bits 8--18 */
545 destination = (urb->pipe & PIPE_DEVEP_MASK) | USB_PID_SETUP;
546
Alan Sternaf0bb592005-12-17 18:00:12 -0500547 /* 3 errors, dummy TD remains inactive */
548 status = uhci_maxerr(3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 if (urb->dev->speed == USB_SPEED_LOW)
550 status |= TD_CTRL_LS;
551
552 /*
553 * Build the TD for the control request setup packet
554 */
Alan Sternaf0bb592005-12-17 18:00:12 -0500555 td = qh->dummy_td;
Alan Stern04538a22006-05-12 11:29:04 -0400556 uhci_add_td_to_urbp(td, urbp);
Alan Sternfa346562005-11-30 11:57:51 -0500557 uhci_fill_td(td, status, destination | uhci_explen(8),
Alan Sterndccf4a42005-12-17 17:58:46 -0500558 urb->setup_dma);
559 plink = &td->link;
Alan Sternaf0bb592005-12-17 18:00:12 -0500560 status |= TD_CTRL_ACTIVE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561
562 /*
563 * If direction is "send", change the packet ID from SETUP (0x2D)
564 * to OUT (0xE1). Else change it from SETUP to IN (0x69) and
565 * set Short Packet Detect (SPD) for all data packets.
566 */
567 if (usb_pipeout(urb->pipe))
568 destination ^= (USB_PID_SETUP ^ USB_PID_OUT);
569 else {
570 destination ^= (USB_PID_SETUP ^ USB_PID_IN);
571 status |= TD_CTRL_SPD;
572 }
573
574 /*
Alan Stern687f5f32005-11-30 17:16:19 -0500575 * Build the DATA TDs
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 */
577 while (len > 0) {
Alan Sterndccf4a42005-12-17 17:58:46 -0500578 int pktsze = min(len, maxsze);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579
Alan Stern25321782005-04-25 11:14:31 -0400580 td = uhci_alloc_td(uhci);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 if (!td)
Alan Sternaf0bb592005-12-17 18:00:12 -0500582 goto nomem;
Alan Sterndccf4a42005-12-17 17:58:46 -0500583 *plink = cpu_to_le32(td->dma_handle);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584
585 /* Alternate Data0/1 (start with Data1) */
586 destination ^= TD_TOKEN_TOGGLE;
587
Alan Stern04538a22006-05-12 11:29:04 -0400588 uhci_add_td_to_urbp(td, urbp);
Alan Sternfa346562005-11-30 11:57:51 -0500589 uhci_fill_td(td, status, destination | uhci_explen(pktsze),
Alan Sterndccf4a42005-12-17 17:58:46 -0500590 data);
591 plink = &td->link;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592
593 data += pktsze;
594 len -= pktsze;
595 }
596
597 /*
598 * Build the final TD for control status
599 */
Alan Stern25321782005-04-25 11:14:31 -0400600 td = uhci_alloc_td(uhci);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 if (!td)
Alan Sternaf0bb592005-12-17 18:00:12 -0500602 goto nomem;
Alan Sterndccf4a42005-12-17 17:58:46 -0500603 *plink = cpu_to_le32(td->dma_handle);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604
605 /*
606 * It's IN if the pipe is an output pipe or we're not expecting
607 * data back.
608 */
609 destination &= ~TD_TOKEN_PID_MASK;
610 if (usb_pipeout(urb->pipe) || !urb->transfer_buffer_length)
611 destination |= USB_PID_IN;
612 else
613 destination |= USB_PID_OUT;
614
615 destination |= TD_TOKEN_TOGGLE; /* End in Data1 */
616
617 status &= ~TD_CTRL_SPD;
618
Alan Stern04538a22006-05-12 11:29:04 -0400619 uhci_add_td_to_urbp(td, urbp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 uhci_fill_td(td, status | TD_CTRL_IOC,
Alan Sterndccf4a42005-12-17 17:58:46 -0500621 destination | uhci_explen(0), 0);
Alan Sternaf0bb592005-12-17 18:00:12 -0500622 plink = &td->link;
623
624 /*
625 * Build the new dummy TD and activate the old one
626 */
627 td = uhci_alloc_td(uhci);
628 if (!td)
629 goto nomem;
630 *plink = cpu_to_le32(td->dma_handle);
631
632 uhci_fill_td(td, 0, USB_PID_OUT | uhci_explen(0), 0);
633 wmb();
634 qh->dummy_td->status |= __constant_cpu_to_le32(TD_CTRL_ACTIVE);
635 qh->dummy_td = td;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636
637 /* Low-speed transfers get a different queue, and won't hog the bus.
638 * Also, some devices enumerate better without FSBR; the easiest way
639 * to do that is to put URBs on the low-speed queue while the device
Alan Stern630aa3c2006-01-23 17:17:21 -0500640 * isn't in the CONFIGURED state. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 if (urb->dev->speed == USB_SPEED_LOW ||
Alan Stern630aa3c2006-01-23 17:17:21 -0500642 urb->dev->state != USB_STATE_CONFIGURED)
Alan Sterndccf4a42005-12-17 17:58:46 -0500643 qh->skel = uhci->skel_ls_control_qh;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 else {
Alan Sterndccf4a42005-12-17 17:58:46 -0500645 qh->skel = uhci->skel_fs_control_qh;
Alan Stern84afddd2006-05-12 11:35:45 -0400646 uhci_add_fsbr(uhci, urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 }
Alan Stern59e29ed2006-05-12 11:19:19 -0400648
649 urb->actual_length = -8; /* Account for the SETUP packet */
Alan Sterndccf4a42005-12-17 17:58:46 -0500650 return 0;
Alan Sternaf0bb592005-12-17 18:00:12 -0500651
652nomem:
653 /* Remove the dummy TD from the td_list so it doesn't get freed */
Alan Stern04538a22006-05-12 11:29:04 -0400654 uhci_remove_td_from_urbp(qh->dummy_td);
Alan Sternaf0bb592005-12-17 18:00:12 -0500655 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656}
657
658/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 * Common submit for bulk and interrupt
660 */
Alan Sterndccf4a42005-12-17 17:58:46 -0500661static int uhci_submit_common(struct uhci_hcd *uhci, struct urb *urb,
662 struct uhci_qh *qh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663{
664 struct uhci_td *td;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 unsigned long destination, status;
Alan Sterndccf4a42005-12-17 17:58:46 -0500666 int maxsze = le16_to_cpu(qh->hep->desc.wMaxPacketSize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 int len = urb->transfer_buffer_length;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 dma_addr_t data = urb->transfer_dma;
Alan Sternaf0bb592005-12-17 18:00:12 -0500669 __le32 *plink;
Alan Stern04538a22006-05-12 11:29:04 -0400670 struct urb_priv *urbp = urb->hcpriv;
Alan Sternaf0bb592005-12-17 18:00:12 -0500671 unsigned int toggle;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672
673 if (len < 0)
674 return -EINVAL;
675
676 /* The "pipe" thing contains the destination in bits 8--18 */
677 destination = (urb->pipe & PIPE_DEVEP_MASK) | usb_packetid(urb->pipe);
Alan Sternaf0bb592005-12-17 18:00:12 -0500678 toggle = usb_gettoggle(urb->dev, usb_pipeendpoint(urb->pipe),
679 usb_pipeout(urb->pipe));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680
Alan Sternaf0bb592005-12-17 18:00:12 -0500681 /* 3 errors, dummy TD remains inactive */
682 status = uhci_maxerr(3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 if (urb->dev->speed == USB_SPEED_LOW)
684 status |= TD_CTRL_LS;
685 if (usb_pipein(urb->pipe))
686 status |= TD_CTRL_SPD;
687
688 /*
Alan Stern687f5f32005-11-30 17:16:19 -0500689 * Build the DATA TDs
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 */
Alan Sternaf0bb592005-12-17 18:00:12 -0500691 plink = NULL;
692 td = qh->dummy_td;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 do { /* Allow zero length packets */
694 int pktsze = maxsze;
695
Alan Sterndccf4a42005-12-17 17:58:46 -0500696 if (len <= pktsze) { /* The last packet */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 pktsze = len;
698 if (!(urb->transfer_flags & URB_SHORT_NOT_OK))
699 status &= ~TD_CTRL_SPD;
700 }
701
Alan Sternaf0bb592005-12-17 18:00:12 -0500702 if (plink) {
703 td = uhci_alloc_td(uhci);
704 if (!td)
705 goto nomem;
706 *plink = cpu_to_le32(td->dma_handle);
707 }
Alan Stern04538a22006-05-12 11:29:04 -0400708 uhci_add_td_to_urbp(td, urbp);
Alan Sterndccf4a42005-12-17 17:58:46 -0500709 uhci_fill_td(td, status,
Alan Sternaf0bb592005-12-17 18:00:12 -0500710 destination | uhci_explen(pktsze) |
711 (toggle << TD_TOKEN_TOGGLE_SHIFT),
712 data);
Alan Sterndccf4a42005-12-17 17:58:46 -0500713 plink = &td->link;
Alan Sternaf0bb592005-12-17 18:00:12 -0500714 status |= TD_CTRL_ACTIVE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715
716 data += pktsze;
717 len -= maxsze;
Alan Sternaf0bb592005-12-17 18:00:12 -0500718 toggle ^= 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 } while (len > 0);
720
721 /*
722 * URB_ZERO_PACKET means adding a 0-length packet, if direction
723 * is OUT and the transfer_length was an exact multiple of maxsze,
724 * hence (len = transfer_length - N * maxsze) == 0
725 * however, if transfer_length == 0, the zero packet was already
726 * prepared above.
727 */
Alan Sterndccf4a42005-12-17 17:58:46 -0500728 if ((urb->transfer_flags & URB_ZERO_PACKET) &&
729 usb_pipeout(urb->pipe) && len == 0 &&
730 urb->transfer_buffer_length > 0) {
Alan Stern25321782005-04-25 11:14:31 -0400731 td = uhci_alloc_td(uhci);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 if (!td)
Alan Sternaf0bb592005-12-17 18:00:12 -0500733 goto nomem;
Alan Sterndccf4a42005-12-17 17:58:46 -0500734 *plink = cpu_to_le32(td->dma_handle);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735
Alan Stern04538a22006-05-12 11:29:04 -0400736 uhci_add_td_to_urbp(td, urbp);
Alan Sternaf0bb592005-12-17 18:00:12 -0500737 uhci_fill_td(td, status,
738 destination | uhci_explen(0) |
739 (toggle << TD_TOKEN_TOGGLE_SHIFT),
740 data);
741 plink = &td->link;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742
Alan Sternaf0bb592005-12-17 18:00:12 -0500743 toggle ^= 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 }
745
746 /* Set the interrupt-on-completion flag on the last packet.
747 * A more-or-less typical 4 KB URB (= size of one memory page)
748 * will require about 3 ms to transfer; that's a little on the
749 * fast side but not enough to justify delaying an interrupt
750 * more than 2 or 3 URBs, so we will ignore the URB_NO_INTERRUPT
751 * flag setting. */
Alan Sterndccf4a42005-12-17 17:58:46 -0500752 td->status |= __constant_cpu_to_le32(TD_CTRL_IOC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753
Alan Sternaf0bb592005-12-17 18:00:12 -0500754 /*
755 * Build the new dummy TD and activate the old one
756 */
757 td = uhci_alloc_td(uhci);
758 if (!td)
759 goto nomem;
760 *plink = cpu_to_le32(td->dma_handle);
761
762 uhci_fill_td(td, 0, USB_PID_OUT | uhci_explen(0), 0);
763 wmb();
764 qh->dummy_td->status |= __constant_cpu_to_le32(TD_CTRL_ACTIVE);
765 qh->dummy_td = td;
766
767 usb_settoggle(urb->dev, usb_pipeendpoint(urb->pipe),
768 usb_pipeout(urb->pipe), toggle);
Alan Sterndccf4a42005-12-17 17:58:46 -0500769 return 0;
Alan Sternaf0bb592005-12-17 18:00:12 -0500770
771nomem:
772 /* Remove the dummy TD from the td_list so it doesn't get freed */
Alan Stern04538a22006-05-12 11:29:04 -0400773 uhci_remove_td_from_urbp(qh->dummy_td);
Alan Sternaf0bb592005-12-17 18:00:12 -0500774 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775}
776
Alan Sterndccf4a42005-12-17 17:58:46 -0500777static inline int uhci_submit_bulk(struct uhci_hcd *uhci, struct urb *urb,
778 struct uhci_qh *qh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779{
780 int ret;
781
782 /* Can't have low-speed bulk transfers */
783 if (urb->dev->speed == USB_SPEED_LOW)
784 return -EINVAL;
785
Alan Sterndccf4a42005-12-17 17:58:46 -0500786 qh->skel = uhci->skel_bulk_qh;
787 ret = uhci_submit_common(uhci, urb, qh);
788 if (ret == 0)
Alan Stern84afddd2006-05-12 11:35:45 -0400789 uhci_add_fsbr(uhci, urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 return ret;
791}
792
Alan Sterndccf4a42005-12-17 17:58:46 -0500793static inline int uhci_submit_interrupt(struct uhci_hcd *uhci, struct urb *urb,
794 struct uhci_qh *qh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795{
Alan Sterndccf4a42005-12-17 17:58:46 -0500796 /* USB 1.1 interrupt transfers only involve one packet per interval.
797 * Drivers can submit URBs of any length, but longer ones will need
798 * multiple intervals to complete.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 */
Alan Sterndccf4a42005-12-17 17:58:46 -0500800 qh->skel = uhci->skelqh[__interval_to_skel(urb->interval)];
801 return uhci_submit_common(uhci, urb, qh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802}
803
804/*
Alan Sternb1869002006-05-12 11:14:25 -0400805 * Fix up the data structures following a short transfer
806 */
807static int uhci_fixup_short_transfer(struct uhci_hcd *uhci,
Alan Stern59e29ed2006-05-12 11:19:19 -0400808 struct uhci_qh *qh, struct urb_priv *urbp)
Alan Sternb1869002006-05-12 11:14:25 -0400809{
810 struct uhci_td *td;
Alan Stern59e29ed2006-05-12 11:19:19 -0400811 struct list_head *tmp;
812 int ret;
Alan Sternb1869002006-05-12 11:14:25 -0400813
814 td = list_entry(urbp->td_list.prev, struct uhci_td, list);
815 if (qh->type == USB_ENDPOINT_XFER_CONTROL) {
Alan Sternb1869002006-05-12 11:14:25 -0400816
817 /* When a control transfer is short, we have to restart
818 * the queue at the status stage transaction, which is
819 * the last TD. */
Alan Stern59e29ed2006-05-12 11:19:19 -0400820 WARN_ON(list_empty(&urbp->td_list));
Alan Sternb1869002006-05-12 11:14:25 -0400821 qh->element = cpu_to_le32(td->dma_handle);
Alan Stern59e29ed2006-05-12 11:19:19 -0400822 tmp = td->list.prev;
Alan Sternb1869002006-05-12 11:14:25 -0400823 ret = -EINPROGRESS;
824
Alan Stern59e29ed2006-05-12 11:19:19 -0400825 } else {
Alan Sternb1869002006-05-12 11:14:25 -0400826
827 /* When a bulk/interrupt transfer is short, we have to
828 * fix up the toggles of the following URBs on the queue
829 * before restarting the queue at the next URB. */
Alan Stern59e29ed2006-05-12 11:19:19 -0400830 qh->initial_toggle = uhci_toggle(td_token(qh->post_td)) ^ 1;
Alan Sternb1869002006-05-12 11:14:25 -0400831 uhci_fixup_toggles(qh, 1);
832
Alan Stern59e29ed2006-05-12 11:19:19 -0400833 if (list_empty(&urbp->td_list))
834 td = qh->post_td;
Alan Sternb1869002006-05-12 11:14:25 -0400835 qh->element = td->link;
Alan Stern59e29ed2006-05-12 11:19:19 -0400836 tmp = urbp->td_list.prev;
837 ret = 0;
Alan Sternb1869002006-05-12 11:14:25 -0400838 }
839
Alan Stern59e29ed2006-05-12 11:19:19 -0400840 /* Remove all the TDs we skipped over, from tmp back to the start */
841 while (tmp != &urbp->td_list) {
842 td = list_entry(tmp, struct uhci_td, list);
843 tmp = tmp->prev;
844
Alan Stern04538a22006-05-12 11:29:04 -0400845 uhci_remove_td_from_urbp(td);
846 uhci_free_td(uhci, td);
Alan Stern59e29ed2006-05-12 11:19:19 -0400847 }
Alan Sternb1869002006-05-12 11:14:25 -0400848 return ret;
849}
850
851/*
852 * Common result for control, bulk, and interrupt
853 */
854static int uhci_result_common(struct uhci_hcd *uhci, struct urb *urb)
855{
856 struct urb_priv *urbp = urb->hcpriv;
857 struct uhci_qh *qh = urbp->qh;
Alan Stern59e29ed2006-05-12 11:19:19 -0400858 struct uhci_td *td, *tmp;
Alan Sternb1869002006-05-12 11:14:25 -0400859 unsigned status;
860 int ret = 0;
861
Alan Stern59e29ed2006-05-12 11:19:19 -0400862 list_for_each_entry_safe(td, tmp, &urbp->td_list, list) {
Alan Sternb1869002006-05-12 11:14:25 -0400863 unsigned int ctrlstat;
864 int len;
865
Alan Sternb1869002006-05-12 11:14:25 -0400866 ctrlstat = td_status(td);
867 status = uhci_status_bits(ctrlstat);
868 if (status & TD_CTRL_ACTIVE)
869 return -EINPROGRESS;
870
871 len = uhci_actual_length(ctrlstat);
872 urb->actual_length += len;
873
874 if (status) {
875 ret = uhci_map_status(status,
876 uhci_packetout(td_token(td)));
877 if ((debug == 1 && ret != -EPIPE) || debug > 1) {
878 /* Some debugging code */
879 dev_dbg(uhci_dev(uhci),
880 "%s: failed with status %x\n",
881 __FUNCTION__, status);
882
883 if (debug > 1 && errbuf) {
884 /* Print the chain for debugging */
885 uhci_show_qh(urbp->qh, errbuf,
886 ERRBUF_LEN, 0);
887 lprintk(errbuf);
888 }
889 }
890
891 } else if (len < uhci_expected_length(td_token(td))) {
892
893 /* We received a short packet */
894 if (urb->transfer_flags & URB_SHORT_NOT_OK)
895 ret = -EREMOTEIO;
896 else if (ctrlstat & TD_CTRL_SPD)
897 ret = 1;
898 }
899
Alan Stern04538a22006-05-12 11:29:04 -0400900 uhci_remove_td_from_urbp(td);
Alan Stern59e29ed2006-05-12 11:19:19 -0400901 if (qh->post_td)
Alan Stern04538a22006-05-12 11:29:04 -0400902 uhci_free_td(uhci, qh->post_td);
Alan Stern59e29ed2006-05-12 11:19:19 -0400903 qh->post_td = td;
904
Alan Sternb1869002006-05-12 11:14:25 -0400905 if (ret != 0)
906 goto err;
907 }
908 return ret;
909
910err:
911 if (ret < 0) {
912 /* In case a control transfer gets an error
913 * during the setup stage */
914 urb->actual_length = max(urb->actual_length, 0);
915
916 /* Note that the queue has stopped and save
917 * the next toggle value */
918 qh->element = UHCI_PTR_TERM;
919 qh->is_stopped = 1;
920 qh->needs_fixup = (qh->type != USB_ENDPOINT_XFER_CONTROL);
921 qh->initial_toggle = uhci_toggle(td_token(td)) ^
922 (ret == -EREMOTEIO);
923
924 } else /* Short packet received */
Alan Stern59e29ed2006-05-12 11:19:19 -0400925 ret = uhci_fixup_short_transfer(uhci, qh, urbp);
Alan Sternb1869002006-05-12 11:14:25 -0400926 return ret;
927}
928
929/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930 * Isochronous transfers
931 */
Alan Sterndccf4a42005-12-17 17:58:46 -0500932static int uhci_submit_isochronous(struct uhci_hcd *uhci, struct urb *urb,
933 struct uhci_qh *qh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934{
Alan Sterndccf4a42005-12-17 17:58:46 -0500935 struct uhci_td *td = NULL; /* Since urb->number_of_packets > 0 */
Alan Stern0ed8fee2005-12-17 18:02:38 -0500936 int i, frame;
Alan Sterndccf4a42005-12-17 17:58:46 -0500937 unsigned long destination, status;
Alan Sternb81d3432005-10-13 17:00:24 -0400938 struct urb_priv *urbp = (struct urb_priv *) urb->hcpriv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939
Alan Stern0ed8fee2005-12-17 18:02:38 -0500940 if (urb->number_of_packets > 900) /* 900? Why? */
941 return -EFBIG;
942
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 status = TD_CTRL_ACTIVE | TD_CTRL_IOS;
944 destination = (urb->pipe & PIPE_DEVEP_MASK) | usb_packetid(urb->pipe);
945
Alan Stern0ed8fee2005-12-17 18:02:38 -0500946 /* Figure out the starting frame number */
947 if (urb->transfer_flags & URB_ISO_ASAP) {
948 if (list_empty(&qh->queue)) {
949 uhci_get_current_frame_number(uhci);
950 urb->start_frame = (uhci->frame_number + 10);
951
952 } else { /* Go right after the last one */
953 struct urb *last_urb;
954
955 last_urb = list_entry(qh->queue.prev,
956 struct urb_priv, node)->urb;
957 urb->start_frame = (last_urb->start_frame +
958 last_urb->number_of_packets *
959 last_urb->interval);
960 }
961 } else {
962 /* FIXME: Sanity check */
963 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964
Alan Sternb81d3432005-10-13 17:00:24 -0400965 for (i = 0; i < urb->number_of_packets; i++) {
Alan Stern25321782005-04-25 11:14:31 -0400966 td = uhci_alloc_td(uhci);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 if (!td)
968 return -ENOMEM;
969
Alan Stern04538a22006-05-12 11:29:04 -0400970 uhci_add_td_to_urbp(td, urbp);
Alan Sterndccf4a42005-12-17 17:58:46 -0500971 uhci_fill_td(td, status, destination |
972 uhci_explen(urb->iso_frame_desc[i].length),
973 urb->transfer_dma +
974 urb->iso_frame_desc[i].offset);
Alan Sternb81d3432005-10-13 17:00:24 -0400975 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976
Alan Sterndccf4a42005-12-17 17:58:46 -0500977 /* Set the interrupt-on-completion flag on the last packet. */
978 td->status |= __constant_cpu_to_le32(TD_CTRL_IOC);
979
980 qh->skel = uhci->skel_iso_qh;
981
982 /* Add the TDs to the frame list */
Alan Sternb81d3432005-10-13 17:00:24 -0400983 frame = urb->start_frame;
984 list_for_each_entry(td, &urbp->td_list, list) {
Alan Sterndccf4a42005-12-17 17:58:46 -0500985 uhci_insert_td_in_frame_list(uhci, td, frame);
Alan Sternb81d3432005-10-13 17:00:24 -0400986 frame += urb->interval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 }
988
Alan Sterndccf4a42005-12-17 17:58:46 -0500989 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990}
991
992static int uhci_result_isochronous(struct uhci_hcd *uhci, struct urb *urb)
993{
994 struct uhci_td *td;
995 struct urb_priv *urbp = (struct urb_priv *)urb->hcpriv;
996 int status;
997 int i, ret = 0;
998
Alan Sternb81d3432005-10-13 17:00:24 -0400999 urb->actual_length = urb->error_count = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000
1001 i = 0;
1002 list_for_each_entry(td, &urbp->td_list, list) {
1003 int actlength;
1004 unsigned int ctrlstat = td_status(td);
1005
1006 if (ctrlstat & TD_CTRL_ACTIVE)
1007 return -EINPROGRESS;
1008
1009 actlength = uhci_actual_length(ctrlstat);
1010 urb->iso_frame_desc[i].actual_length = actlength;
1011 urb->actual_length += actlength;
1012
1013 status = uhci_map_status(uhci_status_bits(ctrlstat),
1014 usb_pipeout(urb->pipe));
1015 urb->iso_frame_desc[i].status = status;
1016 if (status) {
1017 urb->error_count++;
1018 ret = status;
1019 }
1020
1021 i++;
1022 }
1023
1024 return ret;
1025}
1026
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027static int uhci_urb_enqueue(struct usb_hcd *hcd,
Alan Sterndccf4a42005-12-17 17:58:46 -05001028 struct usb_host_endpoint *hep,
Al Viro55016f12005-10-21 03:21:58 -04001029 struct urb *urb, gfp_t mem_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030{
1031 int ret;
1032 struct uhci_hcd *uhci = hcd_to_uhci(hcd);
1033 unsigned long flags;
Alan Sterndccf4a42005-12-17 17:58:46 -05001034 struct urb_priv *urbp;
1035 struct uhci_qh *qh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036 int bustime;
1037
1038 spin_lock_irqsave(&uhci->lock, flags);
1039
1040 ret = urb->status;
1041 if (ret != -EINPROGRESS) /* URB already unlinked! */
Alan Sterndccf4a42005-12-17 17:58:46 -05001042 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043
Alan Sterndccf4a42005-12-17 17:58:46 -05001044 ret = -ENOMEM;
1045 urbp = uhci_alloc_urb_priv(uhci, urb);
1046 if (!urbp)
1047 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048
Alan Sterndccf4a42005-12-17 17:58:46 -05001049 if (hep->hcpriv)
1050 qh = (struct uhci_qh *) hep->hcpriv;
1051 else {
1052 qh = uhci_alloc_qh(uhci, urb->dev, hep);
1053 if (!qh)
1054 goto err_no_qh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 }
Alan Sterndccf4a42005-12-17 17:58:46 -05001056 urbp->qh = qh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057
Alan Stern4de7d2c2006-05-05 16:26:58 -04001058 switch (qh->type) {
1059 case USB_ENDPOINT_XFER_CONTROL:
Alan Sterndccf4a42005-12-17 17:58:46 -05001060 ret = uhci_submit_control(uhci, urb, qh);
1061 break;
Alan Stern4de7d2c2006-05-05 16:26:58 -04001062 case USB_ENDPOINT_XFER_BULK:
Alan Sterndccf4a42005-12-17 17:58:46 -05001063 ret = uhci_submit_bulk(uhci, urb, qh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064 break;
Alan Stern4de7d2c2006-05-05 16:26:58 -04001065 case USB_ENDPOINT_XFER_INT:
Alan Sterndccf4a42005-12-17 17:58:46 -05001066 if (list_empty(&qh->queue)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067 bustime = usb_check_bandwidth(urb->dev, urb);
1068 if (bustime < 0)
1069 ret = bustime;
1070 else {
Alan Sterndccf4a42005-12-17 17:58:46 -05001071 ret = uhci_submit_interrupt(uhci, urb, qh);
1072 if (ret == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073 usb_claim_bandwidth(urb->dev, urb, bustime, 0);
1074 }
1075 } else { /* inherit from parent */
Alan Sterndccf4a42005-12-17 17:58:46 -05001076 struct urb_priv *eurbp;
1077
1078 eurbp = list_entry(qh->queue.prev, struct urb_priv,
1079 node);
1080 urb->bandwidth = eurbp->urb->bandwidth;
1081 ret = uhci_submit_interrupt(uhci, urb, qh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082 }
1083 break;
Alan Stern4de7d2c2006-05-05 16:26:58 -04001084 case USB_ENDPOINT_XFER_ISOC:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085 bustime = usb_check_bandwidth(urb->dev, urb);
1086 if (bustime < 0) {
1087 ret = bustime;
1088 break;
1089 }
1090
Alan Sterndccf4a42005-12-17 17:58:46 -05001091 ret = uhci_submit_isochronous(uhci, urb, qh);
1092 if (ret == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093 usb_claim_bandwidth(urb->dev, urb, bustime, 1);
1094 break;
1095 }
Alan Sterndccf4a42005-12-17 17:58:46 -05001096 if (ret != 0)
1097 goto err_submit_failed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098
Alan Sterndccf4a42005-12-17 17:58:46 -05001099 /* Add this URB to the QH */
1100 urbp->qh = qh;
1101 list_add_tail(&urbp->node, &qh->queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102
Alan Sterndccf4a42005-12-17 17:58:46 -05001103 /* If the new URB is the first and only one on this QH then either
1104 * the QH is new and idle or else it's unlinked and waiting to
Alan Stern27755622006-05-05 16:32:02 -04001105 * become idle, so we can activate it right away. But only if the
1106 * queue isn't stopped. */
Alan Stern84afddd2006-05-12 11:35:45 -04001107 if (qh->queue.next == &urbp->node && !qh->is_stopped) {
Alan Sterndccf4a42005-12-17 17:58:46 -05001108 uhci_activate_qh(uhci, qh);
Alan Stern84afddd2006-05-12 11:35:45 -04001109 uhci_qh_wants_fsbr(uhci, qh);
1110 }
Alan Sterndccf4a42005-12-17 17:58:46 -05001111 goto done;
1112
1113err_submit_failed:
1114 if (qh->state == QH_STATE_IDLE)
1115 uhci_make_qh_idle(uhci, qh); /* Reclaim unused QH */
1116
1117err_no_qh:
1118 uhci_free_urb_priv(uhci, urbp);
1119
1120done:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121 spin_unlock_irqrestore(&uhci->lock, flags);
1122 return ret;
1123}
1124
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125static int uhci_urb_dequeue(struct usb_hcd *hcd, struct urb *urb)
1126{
1127 struct uhci_hcd *uhci = hcd_to_uhci(hcd);
1128 unsigned long flags;
1129 struct urb_priv *urbp;
Alan Stern10b8e472006-05-19 16:39:52 -04001130 struct uhci_qh *qh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131
1132 spin_lock_irqsave(&uhci->lock, flags);
1133 urbp = urb->hcpriv;
1134 if (!urbp) /* URB was never linked! */
1135 goto done;
Alan Stern10b8e472006-05-19 16:39:52 -04001136 qh = urbp->qh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137
Alan Sterndccf4a42005-12-17 17:58:46 -05001138 /* Remove Isochronous TDs from the frame list ASAP */
Alan Stern10b8e472006-05-19 16:39:52 -04001139 if (qh->type == USB_ENDPOINT_XFER_ISOC) {
Alan Sterndccf4a42005-12-17 17:58:46 -05001140 uhci_unlink_isochronous_tds(uhci, urb);
Alan Stern10b8e472006-05-19 16:39:52 -04001141 mb();
1142
1143 /* If the URB has already started, update the QH unlink time */
1144 uhci_get_current_frame_number(uhci);
1145 if (uhci_frame_before_eq(urb->start_frame, uhci->frame_number))
1146 qh->unlink_frame = uhci->frame_number;
1147 }
1148
1149 uhci_unlink_qh(uhci, qh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150
1151done:
1152 spin_unlock_irqrestore(&uhci->lock, flags);
1153 return 0;
1154}
1155
Alan Stern0ed8fee2005-12-17 18:02:38 -05001156/*
1157 * Finish unlinking an URB and give it back
1158 */
1159static void uhci_giveback_urb(struct uhci_hcd *uhci, struct uhci_qh *qh,
1160 struct urb *urb, struct pt_regs *regs)
1161__releases(uhci->lock)
1162__acquires(uhci->lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163{
Alan Stern0ed8fee2005-12-17 18:02:38 -05001164 struct urb_priv *urbp = (struct urb_priv *) urb->hcpriv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165
Alan Stern0ed8fee2005-12-17 18:02:38 -05001166 /* Isochronous TDs get unlinked directly from the frame list */
Alan Stern4de7d2c2006-05-05 16:26:58 -04001167 if (qh->type == USB_ENDPOINT_XFER_ISOC)
Alan Stern0ed8fee2005-12-17 18:02:38 -05001168 uhci_unlink_isochronous_tds(uhci, urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169
Alan Stern0ed8fee2005-12-17 18:02:38 -05001170 /* Take the URB off the QH's queue. If the queue is now empty,
1171 * this is a perfect time for a toggle fixup. */
1172 list_del_init(&urbp->node);
1173 if (list_empty(&qh->queue) && qh->needs_fixup) {
1174 usb_settoggle(urb->dev, usb_pipeendpoint(urb->pipe),
1175 usb_pipeout(urb->pipe), qh->initial_toggle);
1176 qh->needs_fixup = 0;
1177 }
1178
Alan Stern0ed8fee2005-12-17 18:02:38 -05001179 uhci_free_urb_priv(uhci, urbp);
1180
Alan Stern4de7d2c2006-05-05 16:26:58 -04001181 switch (qh->type) {
1182 case USB_ENDPOINT_XFER_ISOC:
Alan Stern0ed8fee2005-12-17 18:02:38 -05001183 /* Release bandwidth for Interrupt or Isoc. transfers */
1184 if (urb->bandwidth)
1185 usb_release_bandwidth(urb->dev, urb, 1);
1186 break;
Alan Stern4de7d2c2006-05-05 16:26:58 -04001187 case USB_ENDPOINT_XFER_INT:
Alan Stern0ed8fee2005-12-17 18:02:38 -05001188 /* Release bandwidth for Interrupt or Isoc. transfers */
1189 /* Make sure we don't release if we have a queued URB */
1190 if (list_empty(&qh->queue) && urb->bandwidth)
1191 usb_release_bandwidth(urb->dev, urb, 0);
1192 else
1193 /* bandwidth was passed on to queued URB, */
1194 /* so don't let usb_unlink_urb() release it */
1195 urb->bandwidth = 0;
1196 break;
1197 }
1198
1199 spin_unlock(&uhci->lock);
1200 usb_hcd_giveback_urb(uhci_to_hcd(uhci), urb, regs);
1201 spin_lock(&uhci->lock);
1202
1203 /* If the queue is now empty, we can unlink the QH and give up its
1204 * reserved bandwidth. */
1205 if (list_empty(&qh->queue)) {
1206 uhci_unlink_qh(uhci, qh);
1207
1208 /* Bandwidth stuff not yet implemented */
1209 }
1210}
1211
1212/*
1213 * Scan the URBs in a QH's queue
1214 */
1215#define QH_FINISHED_UNLINKING(qh) \
1216 (qh->state == QH_STATE_UNLINKING && \
1217 uhci->frame_number + uhci->is_stopped != qh->unlink_frame)
1218
1219static void uhci_scan_qh(struct uhci_hcd *uhci, struct uhci_qh *qh,
1220 struct pt_regs *regs)
1221{
1222 struct urb_priv *urbp;
1223 struct urb *urb;
1224 int status;
1225
1226 while (!list_empty(&qh->queue)) {
1227 urbp = list_entry(qh->queue.next, struct urb_priv, node);
1228 urb = urbp->urb;
1229
Alan Sternb1869002006-05-12 11:14:25 -04001230 if (qh->type == USB_ENDPOINT_XFER_ISOC)
Alan Stern0ed8fee2005-12-17 18:02:38 -05001231 status = uhci_result_isochronous(uhci, urb);
Alan Sternb1869002006-05-12 11:14:25 -04001232 else
Alan Stern0ed8fee2005-12-17 18:02:38 -05001233 status = uhci_result_common(uhci, urb);
Alan Stern0ed8fee2005-12-17 18:02:38 -05001234 if (status == -EINPROGRESS)
1235 break;
1236
1237 spin_lock(&urb->lock);
1238 if (urb->status == -EINPROGRESS) /* Not dequeued */
1239 urb->status = status;
1240 else
Alan Stern27755622006-05-05 16:32:02 -04001241 status = ECONNRESET; /* Not -ECONNRESET */
Alan Stern0ed8fee2005-12-17 18:02:38 -05001242 spin_unlock(&urb->lock);
1243
1244 /* Dequeued but completed URBs can't be given back unless
1245 * the QH is stopped or has finished unlinking. */
Alan Stern27755622006-05-05 16:32:02 -04001246 if (status == ECONNRESET) {
1247 if (QH_FINISHED_UNLINKING(qh))
1248 qh->is_stopped = 1;
1249 else if (!qh->is_stopped)
1250 return;
1251 }
Alan Stern0ed8fee2005-12-17 18:02:38 -05001252
1253 uhci_giveback_urb(uhci, qh, urb, regs);
Alan Stern27755622006-05-05 16:32:02 -04001254 if (status < 0)
Alan Stern0ed8fee2005-12-17 18:02:38 -05001255 break;
1256 }
1257
1258 /* If the QH is neither stopped nor finished unlinking (normal case),
1259 * our work here is done. */
Alan Stern27755622006-05-05 16:32:02 -04001260 if (QH_FINISHED_UNLINKING(qh))
1261 qh->is_stopped = 1;
1262 else if (!qh->is_stopped)
Alan Stern0ed8fee2005-12-17 18:02:38 -05001263 return;
1264
1265 /* Otherwise give back each of the dequeued URBs */
Alan Stern27755622006-05-05 16:32:02 -04001266restart:
Alan Stern0ed8fee2005-12-17 18:02:38 -05001267 list_for_each_entry(urbp, &qh->queue, node) {
1268 urb = urbp->urb;
1269 if (urb->status != -EINPROGRESS) {
Alan Stern10b8e472006-05-19 16:39:52 -04001270
1271 /* Fix up the TD links and save the toggles for
1272 * non-Isochronous queues. For Isochronous queues,
1273 * test for too-recent dequeues. */
1274 if (!uhci_cleanup_queue(uhci, qh, urb)) {
1275 qh->is_stopped = 0;
1276 return;
1277 }
Alan Stern0ed8fee2005-12-17 18:02:38 -05001278 uhci_giveback_urb(uhci, qh, urb, regs);
1279 goto restart;
1280 }
1281 }
1282 qh->is_stopped = 0;
1283
1284 /* There are no more dequeued URBs. If there are still URBs on the
1285 * queue, the QH can now be re-activated. */
1286 if (!list_empty(&qh->queue)) {
1287 if (qh->needs_fixup)
1288 uhci_fixup_toggles(qh, 0);
Alan Stern84afddd2006-05-12 11:35:45 -04001289
1290 /* If the first URB on the queue wants FSBR but its time
1291 * limit has expired, set the next TD to interrupt on
1292 * completion before reactivating the QH. */
1293 urbp = list_entry(qh->queue.next, struct urb_priv, node);
1294 if (urbp->fsbr && qh->wait_expired) {
1295 struct uhci_td *td = list_entry(urbp->td_list.next,
1296 struct uhci_td, list);
1297
1298 td->status |= __cpu_to_le32(TD_CTRL_IOC);
1299 }
1300
Alan Stern0ed8fee2005-12-17 18:02:38 -05001301 uhci_activate_qh(uhci, qh);
1302 }
1303
1304 /* The queue is empty. The QH can become idle if it is fully
1305 * unlinked. */
1306 else if (QH_FINISHED_UNLINKING(qh))
1307 uhci_make_qh_idle(uhci, qh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308}
1309
Alan Stern0ed8fee2005-12-17 18:02:38 -05001310/*
Alan Stern84afddd2006-05-12 11:35:45 -04001311 * Check for queues that have made some forward progress.
1312 * Returns 0 if the queue is not Isochronous, is ACTIVE, and
1313 * has not advanced since last examined; 1 otherwise.
Alan Sternb761d9d2006-05-12 11:41:59 -04001314 *
1315 * Early Intel controllers have a bug which causes qh->element sometimes
1316 * not to advance when a TD completes successfully. The queue remains
1317 * stuck on the inactive completed TD. We detect such cases and advance
1318 * the element pointer by hand.
Alan Stern84afddd2006-05-12 11:35:45 -04001319 */
1320static int uhci_advance_check(struct uhci_hcd *uhci, struct uhci_qh *qh)
1321{
1322 struct urb_priv *urbp = NULL;
1323 struct uhci_td *td;
1324 int ret = 1;
1325 unsigned status;
1326
1327 if (qh->type == USB_ENDPOINT_XFER_ISOC)
1328 return ret;
1329
1330 /* Treat an UNLINKING queue as though it hasn't advanced.
1331 * This is okay because reactivation will treat it as though
1332 * it has advanced, and if it is going to become IDLE then
1333 * this doesn't matter anyway. Furthermore it's possible
1334 * for an UNLINKING queue not to have any URBs at all, or
1335 * for its first URB not to have any TDs (if it was dequeued
1336 * just as it completed). So it's not easy in any case to
1337 * test whether such queues have advanced. */
1338 if (qh->state != QH_STATE_ACTIVE) {
1339 urbp = NULL;
1340 status = 0;
1341
1342 } else {
1343 urbp = list_entry(qh->queue.next, struct urb_priv, node);
1344 td = list_entry(urbp->td_list.next, struct uhci_td, list);
1345 status = td_status(td);
1346 if (!(status & TD_CTRL_ACTIVE)) {
1347
1348 /* We're okay, the queue has advanced */
1349 qh->wait_expired = 0;
1350 qh->advance_jiffies = jiffies;
1351 return ret;
1352 }
1353 ret = 0;
1354 }
1355
1356 /* The queue hasn't advanced; check for timeout */
1357 if (!qh->wait_expired && time_after(jiffies,
1358 qh->advance_jiffies + QH_WAIT_TIMEOUT)) {
Alan Sternb761d9d2006-05-12 11:41:59 -04001359
1360 /* Detect the Intel bug and work around it */
1361 if (qh->post_td && qh_element(qh) ==
1362 cpu_to_le32(qh->post_td->dma_handle)) {
1363 qh->element = qh->post_td->link;
1364 qh->advance_jiffies = jiffies;
1365 return 1;
1366 }
1367
Alan Stern84afddd2006-05-12 11:35:45 -04001368 qh->wait_expired = 1;
1369
1370 /* If the current URB wants FSBR, unlink it temporarily
1371 * so that we can safely set the next TD to interrupt on
1372 * completion. That way we'll know as soon as the queue
1373 * starts moving again. */
1374 if (urbp && urbp->fsbr && !(status & TD_CTRL_IOC))
1375 uhci_unlink_qh(uhci, qh);
1376 }
1377 return ret;
1378}
1379
1380/*
Alan Stern0ed8fee2005-12-17 18:02:38 -05001381 * Process events in the schedule, but only in one thread at a time
1382 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383static void uhci_scan_schedule(struct uhci_hcd *uhci, struct pt_regs *regs)
1384{
Alan Stern0ed8fee2005-12-17 18:02:38 -05001385 int i;
1386 struct uhci_qh *qh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387
1388 /* Don't allow re-entrant calls */
1389 if (uhci->scan_in_progress) {
1390 uhci->need_rescan = 1;
1391 return;
1392 }
1393 uhci->scan_in_progress = 1;
Alan Stern84afddd2006-05-12 11:35:45 -04001394rescan:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395 uhci->need_rescan = 0;
1396
Alan Stern6c1b4452005-04-21 16:04:58 -04001397 uhci_clear_next_interrupt(uhci);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398 uhci_get_current_frame_number(uhci);
1399
Alan Stern0ed8fee2005-12-17 18:02:38 -05001400 /* Go through all the QH queues and process the URBs in each one */
1401 for (i = 0; i < UHCI_NUM_SKELQH - 1; ++i) {
1402 uhci->next_qh = list_entry(uhci->skelqh[i]->node.next,
1403 struct uhci_qh, node);
1404 while ((qh = uhci->next_qh) != uhci->skelqh[i]) {
1405 uhci->next_qh = list_entry(qh->node.next,
1406 struct uhci_qh, node);
Alan Stern84afddd2006-05-12 11:35:45 -04001407
1408 if (uhci_advance_check(uhci, qh)) {
1409 uhci_scan_qh(uhci, qh, regs);
1410 if (qh->state == QH_STATE_ACTIVE)
1411 uhci_qh_wants_fsbr(uhci, qh);
1412 }
Alan Stern0ed8fee2005-12-17 18:02:38 -05001413 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415
1416 if (uhci->need_rescan)
1417 goto rescan;
1418 uhci->scan_in_progress = 0;
1419
Alan Stern84afddd2006-05-12 11:35:45 -04001420 if (uhci->fsbr_is_on && time_after(jiffies,
1421 uhci->fsbr_jiffies + FSBR_OFF_DELAY))
1422 uhci_fsbr_off(uhci);
1423
Alan Stern04538a22006-05-12 11:29:04 -04001424 if (list_empty(&uhci->skel_unlink_qh->node))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425 uhci_clear_next_interrupt(uhci);
1426 else
1427 uhci_set_next_interrupt(uhci);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428}