blob: 7acc23473c637f42c0fef4e693ea2169385b1beb [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;
Alan Sterncaf38272006-05-19 16:44:55 -0400766 qh->period = urb->interval;
Alan Sternaf0bb592005-12-17 18:00:12 -0500767
768 usb_settoggle(urb->dev, usb_pipeendpoint(urb->pipe),
769 usb_pipeout(urb->pipe), toggle);
Alan Sterndccf4a42005-12-17 17:58:46 -0500770 return 0;
Alan Sternaf0bb592005-12-17 18:00:12 -0500771
772nomem:
773 /* Remove the dummy TD from the td_list so it doesn't get freed */
Alan Stern04538a22006-05-12 11:29:04 -0400774 uhci_remove_td_from_urbp(qh->dummy_td);
Alan Sternaf0bb592005-12-17 18:00:12 -0500775 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776}
777
Alan Sterndccf4a42005-12-17 17:58:46 -0500778static inline int uhci_submit_bulk(struct uhci_hcd *uhci, struct urb *urb,
779 struct uhci_qh *qh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780{
781 int ret;
782
783 /* Can't have low-speed bulk transfers */
784 if (urb->dev->speed == USB_SPEED_LOW)
785 return -EINVAL;
786
Alan Sterndccf4a42005-12-17 17:58:46 -0500787 qh->skel = uhci->skel_bulk_qh;
788 ret = uhci_submit_common(uhci, urb, qh);
789 if (ret == 0)
Alan Stern84afddd2006-05-12 11:35:45 -0400790 uhci_add_fsbr(uhci, urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 return ret;
792}
793
Alan Sterncaf38272006-05-19 16:44:55 -0400794static int uhci_submit_interrupt(struct uhci_hcd *uhci, struct urb *urb,
Alan Sterndccf4a42005-12-17 17:58:46 -0500795 struct uhci_qh *qh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796{
Alan Sterncaf38272006-05-19 16:44:55 -0400797 int exponent;
798
Alan Sterndccf4a42005-12-17 17:58:46 -0500799 /* USB 1.1 interrupt transfers only involve one packet per interval.
800 * Drivers can submit URBs of any length, but longer ones will need
801 * multiple intervals to complete.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 */
Alan Sterncaf38272006-05-19 16:44:55 -0400803
804 /* Figure out which power-of-two queue to use */
805 for (exponent = 7; exponent >= 0; --exponent) {
806 if ((1 << exponent) <= urb->interval)
807 break;
808 }
809 if (exponent < 0)
810 return -EINVAL;
811 urb->interval = 1 << exponent;
812
813 if (qh->period == 0)
814 qh->skel = uhci->skelqh[UHCI_SKEL_INDEX(exponent)];
815 else if (qh->period != urb->interval)
816 return -EINVAL; /* Can't change the period */
817
Alan Sterndccf4a42005-12-17 17:58:46 -0500818 return uhci_submit_common(uhci, urb, qh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819}
820
821/*
Alan Sternb1869002006-05-12 11:14:25 -0400822 * Fix up the data structures following a short transfer
823 */
824static int uhci_fixup_short_transfer(struct uhci_hcd *uhci,
Alan Stern59e29ed2006-05-12 11:19:19 -0400825 struct uhci_qh *qh, struct urb_priv *urbp)
Alan Sternb1869002006-05-12 11:14:25 -0400826{
827 struct uhci_td *td;
Alan Stern59e29ed2006-05-12 11:19:19 -0400828 struct list_head *tmp;
829 int ret;
Alan Sternb1869002006-05-12 11:14:25 -0400830
831 td = list_entry(urbp->td_list.prev, struct uhci_td, list);
832 if (qh->type == USB_ENDPOINT_XFER_CONTROL) {
Alan Sternb1869002006-05-12 11:14:25 -0400833
834 /* When a control transfer is short, we have to restart
835 * the queue at the status stage transaction, which is
836 * the last TD. */
Alan Stern59e29ed2006-05-12 11:19:19 -0400837 WARN_ON(list_empty(&urbp->td_list));
Alan Sternb1869002006-05-12 11:14:25 -0400838 qh->element = cpu_to_le32(td->dma_handle);
Alan Stern59e29ed2006-05-12 11:19:19 -0400839 tmp = td->list.prev;
Alan Sternb1869002006-05-12 11:14:25 -0400840 ret = -EINPROGRESS;
841
Alan Stern59e29ed2006-05-12 11:19:19 -0400842 } else {
Alan Sternb1869002006-05-12 11:14:25 -0400843
844 /* When a bulk/interrupt transfer is short, we have to
845 * fix up the toggles of the following URBs on the queue
846 * before restarting the queue at the next URB. */
Alan Stern59e29ed2006-05-12 11:19:19 -0400847 qh->initial_toggle = uhci_toggle(td_token(qh->post_td)) ^ 1;
Alan Sternb1869002006-05-12 11:14:25 -0400848 uhci_fixup_toggles(qh, 1);
849
Alan Stern59e29ed2006-05-12 11:19:19 -0400850 if (list_empty(&urbp->td_list))
851 td = qh->post_td;
Alan Sternb1869002006-05-12 11:14:25 -0400852 qh->element = td->link;
Alan Stern59e29ed2006-05-12 11:19:19 -0400853 tmp = urbp->td_list.prev;
854 ret = 0;
Alan Sternb1869002006-05-12 11:14:25 -0400855 }
856
Alan Stern59e29ed2006-05-12 11:19:19 -0400857 /* Remove all the TDs we skipped over, from tmp back to the start */
858 while (tmp != &urbp->td_list) {
859 td = list_entry(tmp, struct uhci_td, list);
860 tmp = tmp->prev;
861
Alan Stern04538a22006-05-12 11:29:04 -0400862 uhci_remove_td_from_urbp(td);
863 uhci_free_td(uhci, td);
Alan Stern59e29ed2006-05-12 11:19:19 -0400864 }
Alan Sternb1869002006-05-12 11:14:25 -0400865 return ret;
866}
867
868/*
869 * Common result for control, bulk, and interrupt
870 */
871static int uhci_result_common(struct uhci_hcd *uhci, struct urb *urb)
872{
873 struct urb_priv *urbp = urb->hcpriv;
874 struct uhci_qh *qh = urbp->qh;
Alan Stern59e29ed2006-05-12 11:19:19 -0400875 struct uhci_td *td, *tmp;
Alan Sternb1869002006-05-12 11:14:25 -0400876 unsigned status;
877 int ret = 0;
878
Alan Stern59e29ed2006-05-12 11:19:19 -0400879 list_for_each_entry_safe(td, tmp, &urbp->td_list, list) {
Alan Sternb1869002006-05-12 11:14:25 -0400880 unsigned int ctrlstat;
881 int len;
882
Alan Sternb1869002006-05-12 11:14:25 -0400883 ctrlstat = td_status(td);
884 status = uhci_status_bits(ctrlstat);
885 if (status & TD_CTRL_ACTIVE)
886 return -EINPROGRESS;
887
888 len = uhci_actual_length(ctrlstat);
889 urb->actual_length += len;
890
891 if (status) {
892 ret = uhci_map_status(status,
893 uhci_packetout(td_token(td)));
894 if ((debug == 1 && ret != -EPIPE) || debug > 1) {
895 /* Some debugging code */
896 dev_dbg(uhci_dev(uhci),
897 "%s: failed with status %x\n",
898 __FUNCTION__, status);
899
900 if (debug > 1 && errbuf) {
901 /* Print the chain for debugging */
902 uhci_show_qh(urbp->qh, errbuf,
903 ERRBUF_LEN, 0);
904 lprintk(errbuf);
905 }
906 }
907
908 } else if (len < uhci_expected_length(td_token(td))) {
909
910 /* We received a short packet */
911 if (urb->transfer_flags & URB_SHORT_NOT_OK)
912 ret = -EREMOTEIO;
913 else if (ctrlstat & TD_CTRL_SPD)
914 ret = 1;
915 }
916
Alan Stern04538a22006-05-12 11:29:04 -0400917 uhci_remove_td_from_urbp(td);
Alan Stern59e29ed2006-05-12 11:19:19 -0400918 if (qh->post_td)
Alan Stern04538a22006-05-12 11:29:04 -0400919 uhci_free_td(uhci, qh->post_td);
Alan Stern59e29ed2006-05-12 11:19:19 -0400920 qh->post_td = td;
921
Alan Sternb1869002006-05-12 11:14:25 -0400922 if (ret != 0)
923 goto err;
924 }
925 return ret;
926
927err:
928 if (ret < 0) {
929 /* In case a control transfer gets an error
930 * during the setup stage */
931 urb->actual_length = max(urb->actual_length, 0);
932
933 /* Note that the queue has stopped and save
934 * the next toggle value */
935 qh->element = UHCI_PTR_TERM;
936 qh->is_stopped = 1;
937 qh->needs_fixup = (qh->type != USB_ENDPOINT_XFER_CONTROL);
938 qh->initial_toggle = uhci_toggle(td_token(td)) ^
939 (ret == -EREMOTEIO);
940
941 } else /* Short packet received */
Alan Stern59e29ed2006-05-12 11:19:19 -0400942 ret = uhci_fixup_short_transfer(uhci, qh, urbp);
Alan Sternb1869002006-05-12 11:14:25 -0400943 return ret;
944}
945
946/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947 * Isochronous transfers
948 */
Alan Sterndccf4a42005-12-17 17:58:46 -0500949static int uhci_submit_isochronous(struct uhci_hcd *uhci, struct urb *urb,
950 struct uhci_qh *qh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951{
Alan Sterndccf4a42005-12-17 17:58:46 -0500952 struct uhci_td *td = NULL; /* Since urb->number_of_packets > 0 */
Alan Stern0ed8fee2005-12-17 18:02:38 -0500953 int i, frame;
Alan Sterndccf4a42005-12-17 17:58:46 -0500954 unsigned long destination, status;
Alan Sternb81d3432005-10-13 17:00:24 -0400955 struct urb_priv *urbp = (struct urb_priv *) urb->hcpriv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956
Alan Sterncaf38272006-05-19 16:44:55 -0400957 /* Values must not be too big (could overflow below) */
958 if (urb->interval >= UHCI_NUMFRAMES ||
959 urb->number_of_packets >= UHCI_NUMFRAMES)
960 return -EFBIG;
961
962 /* Check the period and figure out the starting frame number */
963 uhci_get_current_frame_number(uhci);
964 if (qh->period == 0) {
965 if (urb->transfer_flags & URB_ISO_ASAP) {
966 urb->start_frame = uhci->frame_number + 10;
967 } else {
968 i = urb->start_frame - uhci->frame_number;
969 if (i <= 0 || i >= UHCI_NUMFRAMES)
970 return -EINVAL;
971 }
972 } else if (qh->period != urb->interval) {
973 return -EINVAL; /* Can't change the period */
974
975 } else { /* Pick up where the last URB leaves off */
976 if (list_empty(&qh->queue)) {
977 frame = uhci->frame_number + 10;
978 } else {
979 struct urb *lurb;
980
981 lurb = list_entry(qh->queue.prev,
982 struct urb_priv, node)->urb;
983 frame = lurb->start_frame +
984 lurb->number_of_packets *
985 lurb->interval;
986 }
987 if (urb->transfer_flags & URB_ISO_ASAP)
988 urb->start_frame = frame;
989 /* FIXME: Sanity check */
990 }
991
992 /* Make sure we won't have to go too far into the future */
993 if (uhci_frame_before_eq(uhci->frame_number + UHCI_NUMFRAMES,
994 urb->start_frame + urb->number_of_packets *
995 urb->interval))
Alan Stern0ed8fee2005-12-17 18:02:38 -0500996 return -EFBIG;
997
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998 status = TD_CTRL_ACTIVE | TD_CTRL_IOS;
999 destination = (urb->pipe & PIPE_DEVEP_MASK) | usb_packetid(urb->pipe);
1000
Alan Sternb81d3432005-10-13 17:00:24 -04001001 for (i = 0; i < urb->number_of_packets; i++) {
Alan Stern25321782005-04-25 11:14:31 -04001002 td = uhci_alloc_td(uhci);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 if (!td)
1004 return -ENOMEM;
1005
Alan Stern04538a22006-05-12 11:29:04 -04001006 uhci_add_td_to_urbp(td, urbp);
Alan Sterndccf4a42005-12-17 17:58:46 -05001007 uhci_fill_td(td, status, destination |
1008 uhci_explen(urb->iso_frame_desc[i].length),
1009 urb->transfer_dma +
1010 urb->iso_frame_desc[i].offset);
Alan Sternb81d3432005-10-13 17:00:24 -04001011 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012
Alan Sterndccf4a42005-12-17 17:58:46 -05001013 /* Set the interrupt-on-completion flag on the last packet. */
1014 td->status |= __constant_cpu_to_le32(TD_CTRL_IOC);
1015
1016 qh->skel = uhci->skel_iso_qh;
Alan Sterncaf38272006-05-19 16:44:55 -04001017 qh->period = urb->interval;
Alan Sterndccf4a42005-12-17 17:58:46 -05001018
1019 /* Add the TDs to the frame list */
Alan Sternb81d3432005-10-13 17:00:24 -04001020 frame = urb->start_frame;
1021 list_for_each_entry(td, &urbp->td_list, list) {
Alan Sterndccf4a42005-12-17 17:58:46 -05001022 uhci_insert_td_in_frame_list(uhci, td, frame);
Alan Sternb81d3432005-10-13 17:00:24 -04001023 frame += urb->interval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024 }
1025
Alan Sterndccf4a42005-12-17 17:58:46 -05001026 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027}
1028
1029static int uhci_result_isochronous(struct uhci_hcd *uhci, struct urb *urb)
1030{
1031 struct uhci_td *td;
1032 struct urb_priv *urbp = (struct urb_priv *)urb->hcpriv;
1033 int status;
1034 int i, ret = 0;
1035
Alan Sternb81d3432005-10-13 17:00:24 -04001036 urb->actual_length = urb->error_count = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037
1038 i = 0;
1039 list_for_each_entry(td, &urbp->td_list, list) {
1040 int actlength;
1041 unsigned int ctrlstat = td_status(td);
1042
1043 if (ctrlstat & TD_CTRL_ACTIVE)
1044 return -EINPROGRESS;
1045
1046 actlength = uhci_actual_length(ctrlstat);
1047 urb->iso_frame_desc[i].actual_length = actlength;
1048 urb->actual_length += actlength;
1049
1050 status = uhci_map_status(uhci_status_bits(ctrlstat),
1051 usb_pipeout(urb->pipe));
1052 urb->iso_frame_desc[i].status = status;
1053 if (status) {
1054 urb->error_count++;
1055 ret = status;
1056 }
1057
1058 i++;
1059 }
1060
1061 return ret;
1062}
1063
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064static int uhci_urb_enqueue(struct usb_hcd *hcd,
Alan Sterndccf4a42005-12-17 17:58:46 -05001065 struct usb_host_endpoint *hep,
Al Viro55016f12005-10-21 03:21:58 -04001066 struct urb *urb, gfp_t mem_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067{
1068 int ret;
1069 struct uhci_hcd *uhci = hcd_to_uhci(hcd);
1070 unsigned long flags;
Alan Sterndccf4a42005-12-17 17:58:46 -05001071 struct urb_priv *urbp;
1072 struct uhci_qh *qh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073 int bustime;
1074
1075 spin_lock_irqsave(&uhci->lock, flags);
1076
1077 ret = urb->status;
1078 if (ret != -EINPROGRESS) /* URB already unlinked! */
Alan Sterndccf4a42005-12-17 17:58:46 -05001079 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080
Alan Sterndccf4a42005-12-17 17:58:46 -05001081 ret = -ENOMEM;
1082 urbp = uhci_alloc_urb_priv(uhci, urb);
1083 if (!urbp)
1084 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085
Alan Sterndccf4a42005-12-17 17:58:46 -05001086 if (hep->hcpriv)
1087 qh = (struct uhci_qh *) hep->hcpriv;
1088 else {
1089 qh = uhci_alloc_qh(uhci, urb->dev, hep);
1090 if (!qh)
1091 goto err_no_qh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092 }
Alan Sterndccf4a42005-12-17 17:58:46 -05001093 urbp->qh = qh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094
Alan Stern4de7d2c2006-05-05 16:26:58 -04001095 switch (qh->type) {
1096 case USB_ENDPOINT_XFER_CONTROL:
Alan Sterndccf4a42005-12-17 17:58:46 -05001097 ret = uhci_submit_control(uhci, urb, qh);
1098 break;
Alan Stern4de7d2c2006-05-05 16:26:58 -04001099 case USB_ENDPOINT_XFER_BULK:
Alan Sterndccf4a42005-12-17 17:58:46 -05001100 ret = uhci_submit_bulk(uhci, urb, qh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101 break;
Alan Stern4de7d2c2006-05-05 16:26:58 -04001102 case USB_ENDPOINT_XFER_INT:
Alan Sterndccf4a42005-12-17 17:58:46 -05001103 if (list_empty(&qh->queue)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104 bustime = usb_check_bandwidth(urb->dev, urb);
1105 if (bustime < 0)
1106 ret = bustime;
1107 else {
Alan Sterndccf4a42005-12-17 17:58:46 -05001108 ret = uhci_submit_interrupt(uhci, urb, qh);
1109 if (ret == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110 usb_claim_bandwidth(urb->dev, urb, bustime, 0);
1111 }
1112 } else { /* inherit from parent */
Alan Sterndccf4a42005-12-17 17:58:46 -05001113 struct urb_priv *eurbp;
1114
1115 eurbp = list_entry(qh->queue.prev, struct urb_priv,
1116 node);
1117 urb->bandwidth = eurbp->urb->bandwidth;
1118 ret = uhci_submit_interrupt(uhci, urb, qh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119 }
1120 break;
Alan Stern4de7d2c2006-05-05 16:26:58 -04001121 case USB_ENDPOINT_XFER_ISOC:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122 bustime = usb_check_bandwidth(urb->dev, urb);
1123 if (bustime < 0) {
1124 ret = bustime;
1125 break;
1126 }
1127
Alan Sterndccf4a42005-12-17 17:58:46 -05001128 ret = uhci_submit_isochronous(uhci, urb, qh);
1129 if (ret == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130 usb_claim_bandwidth(urb->dev, urb, bustime, 1);
1131 break;
1132 }
Alan Sterndccf4a42005-12-17 17:58:46 -05001133 if (ret != 0)
1134 goto err_submit_failed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135
Alan Sterndccf4a42005-12-17 17:58:46 -05001136 /* Add this URB to the QH */
1137 urbp->qh = qh;
1138 list_add_tail(&urbp->node, &qh->queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139
Alan Sterndccf4a42005-12-17 17:58:46 -05001140 /* If the new URB is the first and only one on this QH then either
1141 * the QH is new and idle or else it's unlinked and waiting to
Alan Stern27755622006-05-05 16:32:02 -04001142 * become idle, so we can activate it right away. But only if the
1143 * queue isn't stopped. */
Alan Stern84afddd2006-05-12 11:35:45 -04001144 if (qh->queue.next == &urbp->node && !qh->is_stopped) {
Alan Sterndccf4a42005-12-17 17:58:46 -05001145 uhci_activate_qh(uhci, qh);
Alan Stern84afddd2006-05-12 11:35:45 -04001146 uhci_qh_wants_fsbr(uhci, qh);
1147 }
Alan Sterndccf4a42005-12-17 17:58:46 -05001148 goto done;
1149
1150err_submit_failed:
1151 if (qh->state == QH_STATE_IDLE)
1152 uhci_make_qh_idle(uhci, qh); /* Reclaim unused QH */
1153
1154err_no_qh:
1155 uhci_free_urb_priv(uhci, urbp);
1156
1157done:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158 spin_unlock_irqrestore(&uhci->lock, flags);
1159 return ret;
1160}
1161
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162static int uhci_urb_dequeue(struct usb_hcd *hcd, struct urb *urb)
1163{
1164 struct uhci_hcd *uhci = hcd_to_uhci(hcd);
1165 unsigned long flags;
1166 struct urb_priv *urbp;
Alan Stern10b8e472006-05-19 16:39:52 -04001167 struct uhci_qh *qh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168
1169 spin_lock_irqsave(&uhci->lock, flags);
1170 urbp = urb->hcpriv;
1171 if (!urbp) /* URB was never linked! */
1172 goto done;
Alan Stern10b8e472006-05-19 16:39:52 -04001173 qh = urbp->qh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174
Alan Sterndccf4a42005-12-17 17:58:46 -05001175 /* Remove Isochronous TDs from the frame list ASAP */
Alan Stern10b8e472006-05-19 16:39:52 -04001176 if (qh->type == USB_ENDPOINT_XFER_ISOC) {
Alan Sterndccf4a42005-12-17 17:58:46 -05001177 uhci_unlink_isochronous_tds(uhci, urb);
Alan Stern10b8e472006-05-19 16:39:52 -04001178 mb();
1179
1180 /* If the URB has already started, update the QH unlink time */
1181 uhci_get_current_frame_number(uhci);
1182 if (uhci_frame_before_eq(urb->start_frame, uhci->frame_number))
1183 qh->unlink_frame = uhci->frame_number;
1184 }
1185
1186 uhci_unlink_qh(uhci, qh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187
1188done:
1189 spin_unlock_irqrestore(&uhci->lock, flags);
1190 return 0;
1191}
1192
Alan Stern0ed8fee2005-12-17 18:02:38 -05001193/*
1194 * Finish unlinking an URB and give it back
1195 */
1196static void uhci_giveback_urb(struct uhci_hcd *uhci, struct uhci_qh *qh,
1197 struct urb *urb, struct pt_regs *regs)
1198__releases(uhci->lock)
1199__acquires(uhci->lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200{
Alan Stern0ed8fee2005-12-17 18:02:38 -05001201 struct urb_priv *urbp = (struct urb_priv *) urb->hcpriv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202
Alan Stern0ed8fee2005-12-17 18:02:38 -05001203 /* Isochronous TDs get unlinked directly from the frame list */
Alan Stern4de7d2c2006-05-05 16:26:58 -04001204 if (qh->type == USB_ENDPOINT_XFER_ISOC)
Alan Stern0ed8fee2005-12-17 18:02:38 -05001205 uhci_unlink_isochronous_tds(uhci, urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206
Alan Stern0ed8fee2005-12-17 18:02:38 -05001207 /* Take the URB off the QH's queue. If the queue is now empty,
1208 * this is a perfect time for a toggle fixup. */
1209 list_del_init(&urbp->node);
1210 if (list_empty(&qh->queue) && qh->needs_fixup) {
1211 usb_settoggle(urb->dev, usb_pipeendpoint(urb->pipe),
1212 usb_pipeout(urb->pipe), qh->initial_toggle);
1213 qh->needs_fixup = 0;
1214 }
1215
Alan Stern0ed8fee2005-12-17 18:02:38 -05001216 uhci_free_urb_priv(uhci, urbp);
1217
Alan Stern4de7d2c2006-05-05 16:26:58 -04001218 switch (qh->type) {
1219 case USB_ENDPOINT_XFER_ISOC:
Alan Stern0ed8fee2005-12-17 18:02:38 -05001220 /* Release bandwidth for Interrupt or Isoc. transfers */
1221 if (urb->bandwidth)
1222 usb_release_bandwidth(urb->dev, urb, 1);
1223 break;
Alan Stern4de7d2c2006-05-05 16:26:58 -04001224 case USB_ENDPOINT_XFER_INT:
Alan Stern0ed8fee2005-12-17 18:02:38 -05001225 /* Release bandwidth for Interrupt or Isoc. transfers */
1226 /* Make sure we don't release if we have a queued URB */
1227 if (list_empty(&qh->queue) && urb->bandwidth)
1228 usb_release_bandwidth(urb->dev, urb, 0);
1229 else
1230 /* bandwidth was passed on to queued URB, */
1231 /* so don't let usb_unlink_urb() release it */
1232 urb->bandwidth = 0;
1233 break;
1234 }
1235
1236 spin_unlock(&uhci->lock);
1237 usb_hcd_giveback_urb(uhci_to_hcd(uhci), urb, regs);
1238 spin_lock(&uhci->lock);
1239
1240 /* If the queue is now empty, we can unlink the QH and give up its
1241 * reserved bandwidth. */
1242 if (list_empty(&qh->queue)) {
1243 uhci_unlink_qh(uhci, qh);
1244
1245 /* Bandwidth stuff not yet implemented */
Alan Sterncaf38272006-05-19 16:44:55 -04001246 qh->period = 0;
Alan Stern0ed8fee2005-12-17 18:02:38 -05001247 }
1248}
1249
1250/*
1251 * Scan the URBs in a QH's queue
1252 */
1253#define QH_FINISHED_UNLINKING(qh) \
1254 (qh->state == QH_STATE_UNLINKING && \
1255 uhci->frame_number + uhci->is_stopped != qh->unlink_frame)
1256
1257static void uhci_scan_qh(struct uhci_hcd *uhci, struct uhci_qh *qh,
1258 struct pt_regs *regs)
1259{
1260 struct urb_priv *urbp;
1261 struct urb *urb;
1262 int status;
1263
1264 while (!list_empty(&qh->queue)) {
1265 urbp = list_entry(qh->queue.next, struct urb_priv, node);
1266 urb = urbp->urb;
1267
Alan Sternb1869002006-05-12 11:14:25 -04001268 if (qh->type == USB_ENDPOINT_XFER_ISOC)
Alan Stern0ed8fee2005-12-17 18:02:38 -05001269 status = uhci_result_isochronous(uhci, urb);
Alan Sternb1869002006-05-12 11:14:25 -04001270 else
Alan Stern0ed8fee2005-12-17 18:02:38 -05001271 status = uhci_result_common(uhci, urb);
Alan Stern0ed8fee2005-12-17 18:02:38 -05001272 if (status == -EINPROGRESS)
1273 break;
1274
1275 spin_lock(&urb->lock);
1276 if (urb->status == -EINPROGRESS) /* Not dequeued */
1277 urb->status = status;
1278 else
Alan Stern27755622006-05-05 16:32:02 -04001279 status = ECONNRESET; /* Not -ECONNRESET */
Alan Stern0ed8fee2005-12-17 18:02:38 -05001280 spin_unlock(&urb->lock);
1281
1282 /* Dequeued but completed URBs can't be given back unless
1283 * the QH is stopped or has finished unlinking. */
Alan Stern27755622006-05-05 16:32:02 -04001284 if (status == ECONNRESET) {
1285 if (QH_FINISHED_UNLINKING(qh))
1286 qh->is_stopped = 1;
1287 else if (!qh->is_stopped)
1288 return;
1289 }
Alan Stern0ed8fee2005-12-17 18:02:38 -05001290
1291 uhci_giveback_urb(uhci, qh, urb, regs);
Alan Stern27755622006-05-05 16:32:02 -04001292 if (status < 0)
Alan Stern0ed8fee2005-12-17 18:02:38 -05001293 break;
1294 }
1295
1296 /* If the QH is neither stopped nor finished unlinking (normal case),
1297 * our work here is done. */
Alan Stern27755622006-05-05 16:32:02 -04001298 if (QH_FINISHED_UNLINKING(qh))
1299 qh->is_stopped = 1;
1300 else if (!qh->is_stopped)
Alan Stern0ed8fee2005-12-17 18:02:38 -05001301 return;
1302
1303 /* Otherwise give back each of the dequeued URBs */
Alan Stern27755622006-05-05 16:32:02 -04001304restart:
Alan Stern0ed8fee2005-12-17 18:02:38 -05001305 list_for_each_entry(urbp, &qh->queue, node) {
1306 urb = urbp->urb;
1307 if (urb->status != -EINPROGRESS) {
Alan Stern10b8e472006-05-19 16:39:52 -04001308
1309 /* Fix up the TD links and save the toggles for
1310 * non-Isochronous queues. For Isochronous queues,
1311 * test for too-recent dequeues. */
1312 if (!uhci_cleanup_queue(uhci, qh, urb)) {
1313 qh->is_stopped = 0;
1314 return;
1315 }
Alan Stern0ed8fee2005-12-17 18:02:38 -05001316 uhci_giveback_urb(uhci, qh, urb, regs);
1317 goto restart;
1318 }
1319 }
1320 qh->is_stopped = 0;
1321
1322 /* There are no more dequeued URBs. If there are still URBs on the
1323 * queue, the QH can now be re-activated. */
1324 if (!list_empty(&qh->queue)) {
1325 if (qh->needs_fixup)
1326 uhci_fixup_toggles(qh, 0);
Alan Stern84afddd2006-05-12 11:35:45 -04001327
1328 /* If the first URB on the queue wants FSBR but its time
1329 * limit has expired, set the next TD to interrupt on
1330 * completion before reactivating the QH. */
1331 urbp = list_entry(qh->queue.next, struct urb_priv, node);
1332 if (urbp->fsbr && qh->wait_expired) {
1333 struct uhci_td *td = list_entry(urbp->td_list.next,
1334 struct uhci_td, list);
1335
1336 td->status |= __cpu_to_le32(TD_CTRL_IOC);
1337 }
1338
Alan Stern0ed8fee2005-12-17 18:02:38 -05001339 uhci_activate_qh(uhci, qh);
1340 }
1341
1342 /* The queue is empty. The QH can become idle if it is fully
1343 * unlinked. */
1344 else if (QH_FINISHED_UNLINKING(qh))
1345 uhci_make_qh_idle(uhci, qh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346}
1347
Alan Stern0ed8fee2005-12-17 18:02:38 -05001348/*
Alan Stern84afddd2006-05-12 11:35:45 -04001349 * Check for queues that have made some forward progress.
1350 * Returns 0 if the queue is not Isochronous, is ACTIVE, and
1351 * has not advanced since last examined; 1 otherwise.
Alan Sternb761d9d2006-05-12 11:41:59 -04001352 *
1353 * Early Intel controllers have a bug which causes qh->element sometimes
1354 * not to advance when a TD completes successfully. The queue remains
1355 * stuck on the inactive completed TD. We detect such cases and advance
1356 * the element pointer by hand.
Alan Stern84afddd2006-05-12 11:35:45 -04001357 */
1358static int uhci_advance_check(struct uhci_hcd *uhci, struct uhci_qh *qh)
1359{
1360 struct urb_priv *urbp = NULL;
1361 struct uhci_td *td;
1362 int ret = 1;
1363 unsigned status;
1364
1365 if (qh->type == USB_ENDPOINT_XFER_ISOC)
1366 return ret;
1367
1368 /* Treat an UNLINKING queue as though it hasn't advanced.
1369 * This is okay because reactivation will treat it as though
1370 * it has advanced, and if it is going to become IDLE then
1371 * this doesn't matter anyway. Furthermore it's possible
1372 * for an UNLINKING queue not to have any URBs at all, or
1373 * for its first URB not to have any TDs (if it was dequeued
1374 * just as it completed). So it's not easy in any case to
1375 * test whether such queues have advanced. */
1376 if (qh->state != QH_STATE_ACTIVE) {
1377 urbp = NULL;
1378 status = 0;
1379
1380 } else {
1381 urbp = list_entry(qh->queue.next, struct urb_priv, node);
1382 td = list_entry(urbp->td_list.next, struct uhci_td, list);
1383 status = td_status(td);
1384 if (!(status & TD_CTRL_ACTIVE)) {
1385
1386 /* We're okay, the queue has advanced */
1387 qh->wait_expired = 0;
1388 qh->advance_jiffies = jiffies;
1389 return ret;
1390 }
1391 ret = 0;
1392 }
1393
1394 /* The queue hasn't advanced; check for timeout */
1395 if (!qh->wait_expired && time_after(jiffies,
1396 qh->advance_jiffies + QH_WAIT_TIMEOUT)) {
Alan Sternb761d9d2006-05-12 11:41:59 -04001397
1398 /* Detect the Intel bug and work around it */
1399 if (qh->post_td && qh_element(qh) ==
1400 cpu_to_le32(qh->post_td->dma_handle)) {
1401 qh->element = qh->post_td->link;
1402 qh->advance_jiffies = jiffies;
1403 return 1;
1404 }
1405
Alan Stern84afddd2006-05-12 11:35:45 -04001406 qh->wait_expired = 1;
1407
1408 /* If the current URB wants FSBR, unlink it temporarily
1409 * so that we can safely set the next TD to interrupt on
1410 * completion. That way we'll know as soon as the queue
1411 * starts moving again. */
1412 if (urbp && urbp->fsbr && !(status & TD_CTRL_IOC))
1413 uhci_unlink_qh(uhci, qh);
1414 }
1415 return ret;
1416}
1417
1418/*
Alan Stern0ed8fee2005-12-17 18:02:38 -05001419 * Process events in the schedule, but only in one thread at a time
1420 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421static void uhci_scan_schedule(struct uhci_hcd *uhci, struct pt_regs *regs)
1422{
Alan Stern0ed8fee2005-12-17 18:02:38 -05001423 int i;
1424 struct uhci_qh *qh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425
1426 /* Don't allow re-entrant calls */
1427 if (uhci->scan_in_progress) {
1428 uhci->need_rescan = 1;
1429 return;
1430 }
1431 uhci->scan_in_progress = 1;
Alan Stern84afddd2006-05-12 11:35:45 -04001432rescan:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433 uhci->need_rescan = 0;
1434
Alan Stern6c1b4452005-04-21 16:04:58 -04001435 uhci_clear_next_interrupt(uhci);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436 uhci_get_current_frame_number(uhci);
1437
Alan Stern0ed8fee2005-12-17 18:02:38 -05001438 /* Go through all the QH queues and process the URBs in each one */
1439 for (i = 0; i < UHCI_NUM_SKELQH - 1; ++i) {
1440 uhci->next_qh = list_entry(uhci->skelqh[i]->node.next,
1441 struct uhci_qh, node);
1442 while ((qh = uhci->next_qh) != uhci->skelqh[i]) {
1443 uhci->next_qh = list_entry(qh->node.next,
1444 struct uhci_qh, node);
Alan Stern84afddd2006-05-12 11:35:45 -04001445
1446 if (uhci_advance_check(uhci, qh)) {
1447 uhci_scan_qh(uhci, qh, regs);
1448 if (qh->state == QH_STATE_ACTIVE)
1449 uhci_qh_wants_fsbr(uhci, qh);
1450 }
Alan Stern0ed8fee2005-12-17 18:02:38 -05001451 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453
1454 if (uhci->need_rescan)
1455 goto rescan;
1456 uhci->scan_in_progress = 0;
1457
Alan Stern84afddd2006-05-12 11:35:45 -04001458 if (uhci->fsbr_is_on && time_after(jiffies,
1459 uhci->fsbr_jiffies + FSBR_OFF_DELAY))
1460 uhci_fsbr_off(uhci);
1461
Alan Stern04538a22006-05-12 11:29:04 -04001462 if (list_empty(&uhci->skel_unlink_qh->node))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463 uhci_clear_next_interrupt(uhci);
1464 else
1465 uhci_set_next_interrupt(uhci);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466}