blob: b173d914d7488d9694b8281d0aa671a8ac7d46c5 [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 Sternc8155cc2006-05-19 16:52:35 -0400187static inline void uhci_remove_tds_from_frame(struct uhci_hcd *uhci,
188 unsigned int framenum)
189{
190 struct uhci_td *ftd, *ltd;
191
192 framenum &= (UHCI_NUMFRAMES - 1);
193
194 ftd = uhci->frame_cpu[framenum];
195 if (ftd) {
196 ltd = list_entry(ftd->fl_list.prev, struct uhci_td, fl_list);
197 uhci->frame[framenum] = ltd->link;
198 uhci->frame_cpu[framenum] = NULL;
199
200 while (!list_empty(&ftd->fl_list))
201 list_del_init(ftd->fl_list.prev);
202 }
203}
204
Alan Sterndccf4a42005-12-17 17:58:46 -0500205/*
206 * Remove all the TDs for an Isochronous URB from the frame list
207 */
208static void uhci_unlink_isochronous_tds(struct uhci_hcd *uhci, struct urb *urb)
Alan Sternb81d3432005-10-13 17:00:24 -0400209{
210 struct urb_priv *urbp = (struct urb_priv *) urb->hcpriv;
211 struct uhci_td *td;
212
213 list_for_each_entry(td, &urbp->td_list, list)
Alan Sterndccf4a42005-12-17 17:58:46 -0500214 uhci_remove_td_from_frame_list(uhci, td);
Alan Sternb81d3432005-10-13 17:00:24 -0400215}
216
Alan Sterndccf4a42005-12-17 17:58:46 -0500217static struct uhci_qh *uhci_alloc_qh(struct uhci_hcd *uhci,
218 struct usb_device *udev, struct usb_host_endpoint *hep)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219{
220 dma_addr_t dma_handle;
221 struct uhci_qh *qh;
222
223 qh = dma_pool_alloc(uhci->qh_pool, GFP_ATOMIC, &dma_handle);
224 if (!qh)
225 return NULL;
226
Alan Stern59e29ed2006-05-12 11:19:19 -0400227 memset(qh, 0, sizeof(*qh));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 qh->dma_handle = dma_handle;
229
230 qh->element = UHCI_PTR_TERM;
231 qh->link = UHCI_PTR_TERM;
232
Alan Sterndccf4a42005-12-17 17:58:46 -0500233 INIT_LIST_HEAD(&qh->queue);
234 INIT_LIST_HEAD(&qh->node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235
Alan Sterndccf4a42005-12-17 17:58:46 -0500236 if (udev) { /* Normal QH */
Alan Sternaf0bb592005-12-17 18:00:12 -0500237 qh->dummy_td = uhci_alloc_td(uhci);
238 if (!qh->dummy_td) {
239 dma_pool_free(uhci->qh_pool, qh, dma_handle);
240 return NULL;
241 }
Alan Sterndccf4a42005-12-17 17:58:46 -0500242 qh->state = QH_STATE_IDLE;
243 qh->hep = hep;
244 qh->udev = udev;
245 hep->hcpriv = qh;
Alan Stern4de7d2c2006-05-05 16:26:58 -0400246 qh->type = hep->desc.bmAttributes & USB_ENDPOINT_XFERTYPE_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247
Alan Sterndccf4a42005-12-17 17:58:46 -0500248 } else { /* Skeleton QH */
249 qh->state = QH_STATE_ACTIVE;
Alan Stern4de7d2c2006-05-05 16:26:58 -0400250 qh->type = -1;
Alan Sterndccf4a42005-12-17 17:58:46 -0500251 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 return qh;
253}
254
255static void uhci_free_qh(struct uhci_hcd *uhci, struct uhci_qh *qh)
256{
Alan Sterndccf4a42005-12-17 17:58:46 -0500257 WARN_ON(qh->state != QH_STATE_IDLE && qh->udev);
258 if (!list_empty(&qh->queue))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 dev_warn(uhci_dev(uhci), "qh %p list not empty!\n", qh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260
Alan Sterndccf4a42005-12-17 17:58:46 -0500261 list_del(&qh->node);
262 if (qh->udev) {
263 qh->hep->hcpriv = NULL;
Alan Sternaf0bb592005-12-17 18:00:12 -0500264 uhci_free_td(uhci, qh->dummy_td);
Alan Sterndccf4a42005-12-17 17:58:46 -0500265 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 dma_pool_free(uhci->qh_pool, qh, qh->dma_handle);
267}
268
269/*
Alan Sterna0b458b2006-05-12 11:23:19 -0400270 * When a queue is stopped and a dequeued URB is given back, adjust
271 * the previous TD link (if the URB isn't first on the queue) or
272 * save its toggle value (if it is first and is currently executing).
Alan Stern10b8e472006-05-19 16:39:52 -0400273 *
274 * Returns 0 if the URB should not yet be given back, 1 otherwise.
Alan Stern0ed8fee2005-12-17 18:02:38 -0500275 */
Alan Stern10b8e472006-05-19 16:39:52 -0400276static int uhci_cleanup_queue(struct uhci_hcd *uhci, struct uhci_qh *qh,
Alan Sterna0b458b2006-05-12 11:23:19 -0400277 struct urb *urb)
Alan Stern0ed8fee2005-12-17 18:02:38 -0500278{
Alan Sterna0b458b2006-05-12 11:23:19 -0400279 struct urb_priv *urbp = urb->hcpriv;
Alan Stern0ed8fee2005-12-17 18:02:38 -0500280 struct uhci_td *td;
Alan Stern10b8e472006-05-19 16:39:52 -0400281 int ret = 1;
Alan Stern0ed8fee2005-12-17 18:02:38 -0500282
Alan Sterna0b458b2006-05-12 11:23:19 -0400283 /* Isochronous pipes don't use toggles and their TD link pointers
Alan Stern10b8e472006-05-19 16:39:52 -0400284 * get adjusted during uhci_urb_dequeue(). But since their queues
285 * cannot truly be stopped, we have to watch out for dequeues
286 * occurring after the nominal unlink frame. */
287 if (qh->type == USB_ENDPOINT_XFER_ISOC) {
288 ret = (uhci->frame_number + uhci->is_stopped !=
289 qh->unlink_frame);
290 return ret;
291 }
Alan Sterna0b458b2006-05-12 11:23:19 -0400292
293 /* If the URB isn't first on its queue, adjust the link pointer
294 * of the last TD in the previous URB. The toggle doesn't need
295 * to be saved since this URB can't be executing yet. */
296 if (qh->queue.next != &urbp->node) {
297 struct urb_priv *purbp;
298 struct uhci_td *ptd;
299
300 purbp = list_entry(urbp->node.prev, struct urb_priv, node);
301 WARN_ON(list_empty(&purbp->td_list));
302 ptd = list_entry(purbp->td_list.prev, struct uhci_td,
303 list);
304 td = list_entry(urbp->td_list.prev, struct uhci_td,
305 list);
306 ptd->link = td->link;
Alan Stern10b8e472006-05-19 16:39:52 -0400307 return ret;
Alan Sterna0b458b2006-05-12 11:23:19 -0400308 }
309
Alan Stern0ed8fee2005-12-17 18:02:38 -0500310 /* If the QH element pointer is UHCI_PTR_TERM then then currently
311 * executing URB has already been unlinked, so this one isn't it. */
Alan Sterna0b458b2006-05-12 11:23:19 -0400312 if (qh_element(qh) == UHCI_PTR_TERM)
Alan Stern10b8e472006-05-19 16:39:52 -0400313 return ret;
Alan Stern0ed8fee2005-12-17 18:02:38 -0500314 qh->element = UHCI_PTR_TERM;
315
Alan Sterna0b458b2006-05-12 11:23:19 -0400316 /* Control pipes have to worry about toggles */
317 if (qh->type == USB_ENDPOINT_XFER_CONTROL)
Alan Stern10b8e472006-05-19 16:39:52 -0400318 return ret;
Alan Stern0ed8fee2005-12-17 18:02:38 -0500319
Alan Sterna0b458b2006-05-12 11:23:19 -0400320 /* Save the next toggle value */
Alan Stern59e29ed2006-05-12 11:19:19 -0400321 WARN_ON(list_empty(&urbp->td_list));
322 td = list_entry(urbp->td_list.next, struct uhci_td, list);
323 qh->needs_fixup = 1;
324 qh->initial_toggle = uhci_toggle(td_token(td));
Alan Stern10b8e472006-05-19 16:39:52 -0400325 return ret;
Alan Stern0ed8fee2005-12-17 18:02:38 -0500326}
327
328/*
329 * Fix up the data toggles for URBs in a queue, when one of them
330 * terminates early (short transfer, error, or dequeued).
331 */
332static void uhci_fixup_toggles(struct uhci_qh *qh, int skip_first)
333{
334 struct urb_priv *urbp = NULL;
335 struct uhci_td *td;
336 unsigned int toggle = qh->initial_toggle;
337 unsigned int pipe;
338
339 /* Fixups for a short transfer start with the second URB in the
340 * queue (the short URB is the first). */
341 if (skip_first)
342 urbp = list_entry(qh->queue.next, struct urb_priv, node);
343
344 /* When starting with the first URB, if the QH element pointer is
345 * still valid then we know the URB's toggles are okay. */
346 else if (qh_element(qh) != UHCI_PTR_TERM)
347 toggle = 2;
348
349 /* Fix up the toggle for the URBs in the queue. Normally this
350 * loop won't run more than once: When an error or short transfer
351 * occurs, the queue usually gets emptied. */
Alan Stern1393adb2006-01-31 10:02:55 -0500352 urbp = list_prepare_entry(urbp, &qh->queue, node);
Alan Stern0ed8fee2005-12-17 18:02:38 -0500353 list_for_each_entry_continue(urbp, &qh->queue, node) {
354
355 /* If the first TD has the right toggle value, we don't
356 * need to change any toggles in this URB */
357 td = list_entry(urbp->td_list.next, struct uhci_td, list);
358 if (toggle > 1 || uhci_toggle(td_token(td)) == toggle) {
359 td = list_entry(urbp->td_list.next, struct uhci_td,
360 list);
361 toggle = uhci_toggle(td_token(td)) ^ 1;
362
363 /* Otherwise all the toggles in the URB have to be switched */
364 } else {
365 list_for_each_entry(td, &urbp->td_list, list) {
366 td->token ^= __constant_cpu_to_le32(
367 TD_TOKEN_TOGGLE);
368 toggle ^= 1;
369 }
370 }
371 }
372
373 wmb();
374 pipe = list_entry(qh->queue.next, struct urb_priv, node)->urb->pipe;
375 usb_settoggle(qh->udev, usb_pipeendpoint(pipe),
376 usb_pipeout(pipe), toggle);
377 qh->needs_fixup = 0;
378}
379
380/*
Alan Sterndccf4a42005-12-17 17:58:46 -0500381 * Put a QH on the schedule in both hardware and software
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 */
Alan Sterndccf4a42005-12-17 17:58:46 -0500383static void uhci_activate_qh(struct uhci_hcd *uhci, struct uhci_qh *qh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384{
Alan Sterndccf4a42005-12-17 17:58:46 -0500385 struct uhci_qh *pqh;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386
Alan Sterndccf4a42005-12-17 17:58:46 -0500387 WARN_ON(list_empty(&qh->queue));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388
Alan Sterndccf4a42005-12-17 17:58:46 -0500389 /* Set the element pointer if it isn't set already.
390 * This isn't needed for Isochronous queues, but it doesn't hurt. */
391 if (qh_element(qh) == UHCI_PTR_TERM) {
392 struct urb_priv *urbp = list_entry(qh->queue.next,
393 struct urb_priv, node);
394 struct uhci_td *td = list_entry(urbp->td_list.next,
395 struct uhci_td, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396
Alan Sterndccf4a42005-12-17 17:58:46 -0500397 qh->element = cpu_to_le32(td->dma_handle);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 }
399
Alan Stern84afddd2006-05-12 11:35:45 -0400400 /* Treat the queue as if it has just advanced */
401 qh->wait_expired = 0;
402 qh->advance_jiffies = jiffies;
403
Alan Sterndccf4a42005-12-17 17:58:46 -0500404 if (qh->state == QH_STATE_ACTIVE)
405 return;
406 qh->state = QH_STATE_ACTIVE;
407
408 /* Move the QH from its old list to the end of the appropriate
409 * skeleton's list */
Alan Stern0ed8fee2005-12-17 18:02:38 -0500410 if (qh == uhci->next_qh)
411 uhci->next_qh = list_entry(qh->node.next, struct uhci_qh,
412 node);
Alan Sterndccf4a42005-12-17 17:58:46 -0500413 list_move_tail(&qh->node, &qh->skel->node);
414
415 /* Link it into the schedule */
416 pqh = list_entry(qh->node.prev, struct uhci_qh, node);
417 qh->link = pqh->link;
418 wmb();
419 pqh->link = UHCI_PTR_QH | cpu_to_le32(qh->dma_handle);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420}
421
422/*
Alan Sterndccf4a42005-12-17 17:58:46 -0500423 * Take a QH off the hardware schedule
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 */
Alan Sterndccf4a42005-12-17 17:58:46 -0500425static void uhci_unlink_qh(struct uhci_hcd *uhci, struct uhci_qh *qh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426{
427 struct uhci_qh *pqh;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428
Alan Sterndccf4a42005-12-17 17:58:46 -0500429 if (qh->state == QH_STATE_UNLINKING)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 return;
Alan Sterndccf4a42005-12-17 17:58:46 -0500431 WARN_ON(qh->state != QH_STATE_ACTIVE || !qh->udev);
432 qh->state = QH_STATE_UNLINKING;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433
Alan Sterndccf4a42005-12-17 17:58:46 -0500434 /* Unlink the QH from the schedule and record when we did it */
435 pqh = list_entry(qh->node.prev, struct uhci_qh, node);
436 pqh->link = qh->link;
437 mb();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438
439 uhci_get_current_frame_number(uhci);
Alan Sterndccf4a42005-12-17 17:58:46 -0500440 qh->unlink_frame = uhci->frame_number;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441
Alan Sterndccf4a42005-12-17 17:58:46 -0500442 /* Force an interrupt so we know when the QH is fully unlinked */
443 if (list_empty(&uhci->skel_unlink_qh->node))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 uhci_set_next_interrupt(uhci);
445
Alan Sterndccf4a42005-12-17 17:58:46 -0500446 /* Move the QH from its old list to the end of the unlinking list */
Alan Stern0ed8fee2005-12-17 18:02:38 -0500447 if (qh == uhci->next_qh)
448 uhci->next_qh = list_entry(qh->node.next, struct uhci_qh,
449 node);
Alan Sterndccf4a42005-12-17 17:58:46 -0500450 list_move_tail(&qh->node, &uhci->skel_unlink_qh->node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451}
452
Alan Sterndccf4a42005-12-17 17:58:46 -0500453/*
454 * When we and the controller are through with a QH, it becomes IDLE.
455 * This happens when a QH has been off the schedule (on the unlinking
456 * list) for more than one frame, or when an error occurs while adding
457 * the first URB onto a new QH.
458 */
459static void uhci_make_qh_idle(struct uhci_hcd *uhci, struct uhci_qh *qh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460{
Alan Sterndccf4a42005-12-17 17:58:46 -0500461 WARN_ON(qh->state == QH_STATE_ACTIVE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462
Alan Stern0ed8fee2005-12-17 18:02:38 -0500463 if (qh == uhci->next_qh)
464 uhci->next_qh = list_entry(qh->node.next, struct uhci_qh,
465 node);
Alan Sterndccf4a42005-12-17 17:58:46 -0500466 list_move(&qh->node, &uhci->idle_qh_list);
467 qh->state = QH_STATE_IDLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468
Alan Stern59e29ed2006-05-12 11:19:19 -0400469 /* Now that the QH is idle, its post_td isn't being used */
470 if (qh->post_td) {
471 uhci_free_td(uhci, qh->post_td);
472 qh->post_td = NULL;
473 }
474
Alan Sterndccf4a42005-12-17 17:58:46 -0500475 /* If anyone is waiting for a QH to become idle, wake them up */
476 if (uhci->num_waiting)
477 wake_up_all(&uhci->waitqh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478}
479
Alan Sterndccf4a42005-12-17 17:58:46 -0500480static inline struct urb_priv *uhci_alloc_urb_priv(struct uhci_hcd *uhci,
481 struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482{
483 struct urb_priv *urbp;
484
485 urbp = kmem_cache_alloc(uhci_up_cachep, SLAB_ATOMIC);
486 if (!urbp)
487 return NULL;
488
489 memset((void *)urbp, 0, sizeof(*urbp));
490
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 urbp->urb = urb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 urb->hcpriv = urbp;
Alan Sterndccf4a42005-12-17 17:58:46 -0500493
494 INIT_LIST_HEAD(&urbp->node);
495 INIT_LIST_HEAD(&urbp->td_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496
497 return urbp;
498}
499
Alan Sterndccf4a42005-12-17 17:58:46 -0500500static void uhci_free_urb_priv(struct uhci_hcd *uhci,
501 struct urb_priv *urbp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502{
503 struct uhci_td *td, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504
Alan Sterndccf4a42005-12-17 17:58:46 -0500505 if (!list_empty(&urbp->node))
506 dev_warn(uhci_dev(uhci), "urb %p still on QH's list!\n",
507 urbp->urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 list_for_each_entry_safe(td, tmp, &urbp->td_list, list) {
Alan Stern04538a22006-05-12 11:29:04 -0400510 uhci_remove_td_from_urbp(td);
511 uhci_free_td(uhci, td);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 }
513
Alan Sterndccf4a42005-12-17 17:58:46 -0500514 urbp->urb->hcpriv = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 kmem_cache_free(uhci_up_cachep, urbp);
516}
517
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518/*
519 * Map status to standard result codes
520 *
521 * <status> is (td_status(td) & 0xF60000), a.k.a.
522 * uhci_status_bits(td_status(td)).
523 * Note: <status> does not include the TD_CTRL_NAK bit.
524 * <dir_out> is True for output TDs and False for input TDs.
525 */
526static int uhci_map_status(int status, int dir_out)
527{
528 if (!status)
529 return 0;
530 if (status & TD_CTRL_BITSTUFF) /* Bitstuff error */
531 return -EPROTO;
532 if (status & TD_CTRL_CRCTIMEO) { /* CRC/Timeout */
533 if (dir_out)
534 return -EPROTO;
535 else
536 return -EILSEQ;
537 }
538 if (status & TD_CTRL_BABBLE) /* Babble */
539 return -EOVERFLOW;
540 if (status & TD_CTRL_DBUFERR) /* Buffer error */
541 return -ENOSR;
542 if (status & TD_CTRL_STALLED) /* Stalled */
543 return -EPIPE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 return 0;
545}
546
547/*
548 * Control transfers
549 */
Alan Sterndccf4a42005-12-17 17:58:46 -0500550static int uhci_submit_control(struct uhci_hcd *uhci, struct urb *urb,
551 struct uhci_qh *qh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 struct uhci_td *td;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 unsigned long destination, status;
Alan Sterndccf4a42005-12-17 17:58:46 -0500555 int maxsze = le16_to_cpu(qh->hep->desc.wMaxPacketSize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 int len = urb->transfer_buffer_length;
557 dma_addr_t data = urb->transfer_dma;
Alan Sterndccf4a42005-12-17 17:58:46 -0500558 __le32 *plink;
Alan Stern04538a22006-05-12 11:29:04 -0400559 struct urb_priv *urbp = urb->hcpriv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560
561 /* The "pipe" thing contains the destination in bits 8--18 */
562 destination = (urb->pipe & PIPE_DEVEP_MASK) | USB_PID_SETUP;
563
Alan Sternaf0bb592005-12-17 18:00:12 -0500564 /* 3 errors, dummy TD remains inactive */
565 status = uhci_maxerr(3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 if (urb->dev->speed == USB_SPEED_LOW)
567 status |= TD_CTRL_LS;
568
569 /*
570 * Build the TD for the control request setup packet
571 */
Alan Sternaf0bb592005-12-17 18:00:12 -0500572 td = qh->dummy_td;
Alan Stern04538a22006-05-12 11:29:04 -0400573 uhci_add_td_to_urbp(td, urbp);
Alan Sternfa346562005-11-30 11:57:51 -0500574 uhci_fill_td(td, status, destination | uhci_explen(8),
Alan Sterndccf4a42005-12-17 17:58:46 -0500575 urb->setup_dma);
576 plink = &td->link;
Alan Sternaf0bb592005-12-17 18:00:12 -0500577 status |= TD_CTRL_ACTIVE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578
579 /*
580 * If direction is "send", change the packet ID from SETUP (0x2D)
581 * to OUT (0xE1). Else change it from SETUP to IN (0x69) and
582 * set Short Packet Detect (SPD) for all data packets.
583 */
584 if (usb_pipeout(urb->pipe))
585 destination ^= (USB_PID_SETUP ^ USB_PID_OUT);
586 else {
587 destination ^= (USB_PID_SETUP ^ USB_PID_IN);
588 status |= TD_CTRL_SPD;
589 }
590
591 /*
Alan Stern687f5f32005-11-30 17:16:19 -0500592 * Build the DATA TDs
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 */
594 while (len > 0) {
Alan Sterndccf4a42005-12-17 17:58:46 -0500595 int pktsze = min(len, maxsze);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596
Alan Stern25321782005-04-25 11:14:31 -0400597 td = uhci_alloc_td(uhci);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 if (!td)
Alan Sternaf0bb592005-12-17 18:00:12 -0500599 goto nomem;
Alan Sterndccf4a42005-12-17 17:58:46 -0500600 *plink = cpu_to_le32(td->dma_handle);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601
602 /* Alternate Data0/1 (start with Data1) */
603 destination ^= TD_TOKEN_TOGGLE;
604
Alan Stern04538a22006-05-12 11:29:04 -0400605 uhci_add_td_to_urbp(td, urbp);
Alan Sternfa346562005-11-30 11:57:51 -0500606 uhci_fill_td(td, status, destination | uhci_explen(pktsze),
Alan Sterndccf4a42005-12-17 17:58:46 -0500607 data);
608 plink = &td->link;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609
610 data += pktsze;
611 len -= pktsze;
612 }
613
614 /*
615 * Build the final TD for control status
616 */
Alan Stern25321782005-04-25 11:14:31 -0400617 td = uhci_alloc_td(uhci);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 if (!td)
Alan Sternaf0bb592005-12-17 18:00:12 -0500619 goto nomem;
Alan Sterndccf4a42005-12-17 17:58:46 -0500620 *plink = cpu_to_le32(td->dma_handle);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621
622 /*
623 * It's IN if the pipe is an output pipe or we're not expecting
624 * data back.
625 */
626 destination &= ~TD_TOKEN_PID_MASK;
627 if (usb_pipeout(urb->pipe) || !urb->transfer_buffer_length)
628 destination |= USB_PID_IN;
629 else
630 destination |= USB_PID_OUT;
631
632 destination |= TD_TOKEN_TOGGLE; /* End in Data1 */
633
634 status &= ~TD_CTRL_SPD;
635
Alan Stern04538a22006-05-12 11:29:04 -0400636 uhci_add_td_to_urbp(td, urbp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 uhci_fill_td(td, status | TD_CTRL_IOC,
Alan Sterndccf4a42005-12-17 17:58:46 -0500638 destination | uhci_explen(0), 0);
Alan Sternaf0bb592005-12-17 18:00:12 -0500639 plink = &td->link;
640
641 /*
642 * Build the new dummy TD and activate the old one
643 */
644 td = uhci_alloc_td(uhci);
645 if (!td)
646 goto nomem;
647 *plink = cpu_to_le32(td->dma_handle);
648
649 uhci_fill_td(td, 0, USB_PID_OUT | uhci_explen(0), 0);
650 wmb();
651 qh->dummy_td->status |= __constant_cpu_to_le32(TD_CTRL_ACTIVE);
652 qh->dummy_td = td;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653
654 /* Low-speed transfers get a different queue, and won't hog the bus.
655 * Also, some devices enumerate better without FSBR; the easiest way
656 * to do that is to put URBs on the low-speed queue while the device
Alan Stern630aa3c2006-01-23 17:17:21 -0500657 * isn't in the CONFIGURED state. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 if (urb->dev->speed == USB_SPEED_LOW ||
Alan Stern630aa3c2006-01-23 17:17:21 -0500659 urb->dev->state != USB_STATE_CONFIGURED)
Alan Sterndccf4a42005-12-17 17:58:46 -0500660 qh->skel = uhci->skel_ls_control_qh;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 else {
Alan Sterndccf4a42005-12-17 17:58:46 -0500662 qh->skel = uhci->skel_fs_control_qh;
Alan Stern84afddd2006-05-12 11:35:45 -0400663 uhci_add_fsbr(uhci, urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 }
Alan Stern59e29ed2006-05-12 11:19:19 -0400665
666 urb->actual_length = -8; /* Account for the SETUP packet */
Alan Sterndccf4a42005-12-17 17:58:46 -0500667 return 0;
Alan Sternaf0bb592005-12-17 18:00:12 -0500668
669nomem:
670 /* Remove the dummy TD from the td_list so it doesn't get freed */
Alan Stern04538a22006-05-12 11:29:04 -0400671 uhci_remove_td_from_urbp(qh->dummy_td);
Alan Sternaf0bb592005-12-17 18:00:12 -0500672 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673}
674
675/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 * Common submit for bulk and interrupt
677 */
Alan Sterndccf4a42005-12-17 17:58:46 -0500678static int uhci_submit_common(struct uhci_hcd *uhci, struct urb *urb,
679 struct uhci_qh *qh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680{
681 struct uhci_td *td;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 unsigned long destination, status;
Alan Sterndccf4a42005-12-17 17:58:46 -0500683 int maxsze = le16_to_cpu(qh->hep->desc.wMaxPacketSize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 int len = urb->transfer_buffer_length;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 dma_addr_t data = urb->transfer_dma;
Alan Sternaf0bb592005-12-17 18:00:12 -0500686 __le32 *plink;
Alan Stern04538a22006-05-12 11:29:04 -0400687 struct urb_priv *urbp = urb->hcpriv;
Alan Sternaf0bb592005-12-17 18:00:12 -0500688 unsigned int toggle;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689
690 if (len < 0)
691 return -EINVAL;
692
693 /* The "pipe" thing contains the destination in bits 8--18 */
694 destination = (urb->pipe & PIPE_DEVEP_MASK) | usb_packetid(urb->pipe);
Alan Sternaf0bb592005-12-17 18:00:12 -0500695 toggle = usb_gettoggle(urb->dev, usb_pipeendpoint(urb->pipe),
696 usb_pipeout(urb->pipe));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697
Alan Sternaf0bb592005-12-17 18:00:12 -0500698 /* 3 errors, dummy TD remains inactive */
699 status = uhci_maxerr(3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 if (urb->dev->speed == USB_SPEED_LOW)
701 status |= TD_CTRL_LS;
702 if (usb_pipein(urb->pipe))
703 status |= TD_CTRL_SPD;
704
705 /*
Alan Stern687f5f32005-11-30 17:16:19 -0500706 * Build the DATA TDs
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 */
Alan Sternaf0bb592005-12-17 18:00:12 -0500708 plink = NULL;
709 td = qh->dummy_td;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 do { /* Allow zero length packets */
711 int pktsze = maxsze;
712
Alan Sterndccf4a42005-12-17 17:58:46 -0500713 if (len <= pktsze) { /* The last packet */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 pktsze = len;
715 if (!(urb->transfer_flags & URB_SHORT_NOT_OK))
716 status &= ~TD_CTRL_SPD;
717 }
718
Alan Sternaf0bb592005-12-17 18:00:12 -0500719 if (plink) {
720 td = uhci_alloc_td(uhci);
721 if (!td)
722 goto nomem;
723 *plink = cpu_to_le32(td->dma_handle);
724 }
Alan Stern04538a22006-05-12 11:29:04 -0400725 uhci_add_td_to_urbp(td, urbp);
Alan Sterndccf4a42005-12-17 17:58:46 -0500726 uhci_fill_td(td, status,
Alan Sternaf0bb592005-12-17 18:00:12 -0500727 destination | uhci_explen(pktsze) |
728 (toggle << TD_TOKEN_TOGGLE_SHIFT),
729 data);
Alan Sterndccf4a42005-12-17 17:58:46 -0500730 plink = &td->link;
Alan Sternaf0bb592005-12-17 18:00:12 -0500731 status |= TD_CTRL_ACTIVE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732
733 data += pktsze;
734 len -= maxsze;
Alan Sternaf0bb592005-12-17 18:00:12 -0500735 toggle ^= 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 } while (len > 0);
737
738 /*
739 * URB_ZERO_PACKET means adding a 0-length packet, if direction
740 * is OUT and the transfer_length was an exact multiple of maxsze,
741 * hence (len = transfer_length - N * maxsze) == 0
742 * however, if transfer_length == 0, the zero packet was already
743 * prepared above.
744 */
Alan Sterndccf4a42005-12-17 17:58:46 -0500745 if ((urb->transfer_flags & URB_ZERO_PACKET) &&
746 usb_pipeout(urb->pipe) && len == 0 &&
747 urb->transfer_buffer_length > 0) {
Alan Stern25321782005-04-25 11:14:31 -0400748 td = uhci_alloc_td(uhci);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 if (!td)
Alan Sternaf0bb592005-12-17 18:00:12 -0500750 goto nomem;
Alan Sterndccf4a42005-12-17 17:58:46 -0500751 *plink = cpu_to_le32(td->dma_handle);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752
Alan Stern04538a22006-05-12 11:29:04 -0400753 uhci_add_td_to_urbp(td, urbp);
Alan Sternaf0bb592005-12-17 18:00:12 -0500754 uhci_fill_td(td, status,
755 destination | uhci_explen(0) |
756 (toggle << TD_TOKEN_TOGGLE_SHIFT),
757 data);
758 plink = &td->link;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759
Alan Sternaf0bb592005-12-17 18:00:12 -0500760 toggle ^= 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 }
762
763 /* Set the interrupt-on-completion flag on the last packet.
764 * A more-or-less typical 4 KB URB (= size of one memory page)
765 * will require about 3 ms to transfer; that's a little on the
766 * fast side but not enough to justify delaying an interrupt
767 * more than 2 or 3 URBs, so we will ignore the URB_NO_INTERRUPT
768 * flag setting. */
Alan Sterndccf4a42005-12-17 17:58:46 -0500769 td->status |= __constant_cpu_to_le32(TD_CTRL_IOC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770
Alan Sternaf0bb592005-12-17 18:00:12 -0500771 /*
772 * Build the new dummy TD and activate the old one
773 */
774 td = uhci_alloc_td(uhci);
775 if (!td)
776 goto nomem;
777 *plink = cpu_to_le32(td->dma_handle);
778
779 uhci_fill_td(td, 0, USB_PID_OUT | uhci_explen(0), 0);
780 wmb();
781 qh->dummy_td->status |= __constant_cpu_to_le32(TD_CTRL_ACTIVE);
782 qh->dummy_td = td;
Alan Sterncaf38272006-05-19 16:44:55 -0400783 qh->period = urb->interval;
Alan Sternaf0bb592005-12-17 18:00:12 -0500784
785 usb_settoggle(urb->dev, usb_pipeendpoint(urb->pipe),
786 usb_pipeout(urb->pipe), toggle);
Alan Sterndccf4a42005-12-17 17:58:46 -0500787 return 0;
Alan Sternaf0bb592005-12-17 18:00:12 -0500788
789nomem:
790 /* Remove the dummy TD from the td_list so it doesn't get freed */
Alan Stern04538a22006-05-12 11:29:04 -0400791 uhci_remove_td_from_urbp(qh->dummy_td);
Alan Sternaf0bb592005-12-17 18:00:12 -0500792 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793}
794
Alan Sterndccf4a42005-12-17 17:58:46 -0500795static inline int uhci_submit_bulk(struct uhci_hcd *uhci, struct urb *urb,
796 struct uhci_qh *qh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797{
798 int ret;
799
800 /* Can't have low-speed bulk transfers */
801 if (urb->dev->speed == USB_SPEED_LOW)
802 return -EINVAL;
803
Alan Sterndccf4a42005-12-17 17:58:46 -0500804 qh->skel = uhci->skel_bulk_qh;
805 ret = uhci_submit_common(uhci, urb, qh);
806 if (ret == 0)
Alan Stern84afddd2006-05-12 11:35:45 -0400807 uhci_add_fsbr(uhci, urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 return ret;
809}
810
Alan Sterncaf38272006-05-19 16:44:55 -0400811static int uhci_submit_interrupt(struct uhci_hcd *uhci, struct urb *urb,
Alan Sterndccf4a42005-12-17 17:58:46 -0500812 struct uhci_qh *qh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813{
Alan Sterncaf38272006-05-19 16:44:55 -0400814 int exponent;
815
Alan Sterndccf4a42005-12-17 17:58:46 -0500816 /* USB 1.1 interrupt transfers only involve one packet per interval.
817 * Drivers can submit URBs of any length, but longer ones will need
818 * multiple intervals to complete.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 */
Alan Sterncaf38272006-05-19 16:44:55 -0400820
821 /* Figure out which power-of-two queue to use */
822 for (exponent = 7; exponent >= 0; --exponent) {
823 if ((1 << exponent) <= urb->interval)
824 break;
825 }
826 if (exponent < 0)
827 return -EINVAL;
828 urb->interval = 1 << exponent;
829
830 if (qh->period == 0)
831 qh->skel = uhci->skelqh[UHCI_SKEL_INDEX(exponent)];
832 else if (qh->period != urb->interval)
833 return -EINVAL; /* Can't change the period */
834
Alan Sterndccf4a42005-12-17 17:58:46 -0500835 return uhci_submit_common(uhci, urb, qh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836}
837
838/*
Alan Sternb1869002006-05-12 11:14:25 -0400839 * Fix up the data structures following a short transfer
840 */
841static int uhci_fixup_short_transfer(struct uhci_hcd *uhci,
Alan Stern59e29ed2006-05-12 11:19:19 -0400842 struct uhci_qh *qh, struct urb_priv *urbp)
Alan Sternb1869002006-05-12 11:14:25 -0400843{
844 struct uhci_td *td;
Alan Stern59e29ed2006-05-12 11:19:19 -0400845 struct list_head *tmp;
846 int ret;
Alan Sternb1869002006-05-12 11:14:25 -0400847
848 td = list_entry(urbp->td_list.prev, struct uhci_td, list);
849 if (qh->type == USB_ENDPOINT_XFER_CONTROL) {
Alan Sternb1869002006-05-12 11:14:25 -0400850
851 /* When a control transfer is short, we have to restart
852 * the queue at the status stage transaction, which is
853 * the last TD. */
Alan Stern59e29ed2006-05-12 11:19:19 -0400854 WARN_ON(list_empty(&urbp->td_list));
Alan Sternb1869002006-05-12 11:14:25 -0400855 qh->element = cpu_to_le32(td->dma_handle);
Alan Stern59e29ed2006-05-12 11:19:19 -0400856 tmp = td->list.prev;
Alan Sternb1869002006-05-12 11:14:25 -0400857 ret = -EINPROGRESS;
858
Alan Stern59e29ed2006-05-12 11:19:19 -0400859 } else {
Alan Sternb1869002006-05-12 11:14:25 -0400860
861 /* When a bulk/interrupt transfer is short, we have to
862 * fix up the toggles of the following URBs on the queue
863 * before restarting the queue at the next URB. */
Alan Stern59e29ed2006-05-12 11:19:19 -0400864 qh->initial_toggle = uhci_toggle(td_token(qh->post_td)) ^ 1;
Alan Sternb1869002006-05-12 11:14:25 -0400865 uhci_fixup_toggles(qh, 1);
866
Alan Stern59e29ed2006-05-12 11:19:19 -0400867 if (list_empty(&urbp->td_list))
868 td = qh->post_td;
Alan Sternb1869002006-05-12 11:14:25 -0400869 qh->element = td->link;
Alan Stern59e29ed2006-05-12 11:19:19 -0400870 tmp = urbp->td_list.prev;
871 ret = 0;
Alan Sternb1869002006-05-12 11:14:25 -0400872 }
873
Alan Stern59e29ed2006-05-12 11:19:19 -0400874 /* Remove all the TDs we skipped over, from tmp back to the start */
875 while (tmp != &urbp->td_list) {
876 td = list_entry(tmp, struct uhci_td, list);
877 tmp = tmp->prev;
878
Alan Stern04538a22006-05-12 11:29:04 -0400879 uhci_remove_td_from_urbp(td);
880 uhci_free_td(uhci, td);
Alan Stern59e29ed2006-05-12 11:19:19 -0400881 }
Alan Sternb1869002006-05-12 11:14:25 -0400882 return ret;
883}
884
885/*
886 * Common result for control, bulk, and interrupt
887 */
888static int uhci_result_common(struct uhci_hcd *uhci, struct urb *urb)
889{
890 struct urb_priv *urbp = urb->hcpriv;
891 struct uhci_qh *qh = urbp->qh;
Alan Stern59e29ed2006-05-12 11:19:19 -0400892 struct uhci_td *td, *tmp;
Alan Sternb1869002006-05-12 11:14:25 -0400893 unsigned status;
894 int ret = 0;
895
Alan Stern59e29ed2006-05-12 11:19:19 -0400896 list_for_each_entry_safe(td, tmp, &urbp->td_list, list) {
Alan Sternb1869002006-05-12 11:14:25 -0400897 unsigned int ctrlstat;
898 int len;
899
Alan Sternb1869002006-05-12 11:14:25 -0400900 ctrlstat = td_status(td);
901 status = uhci_status_bits(ctrlstat);
902 if (status & TD_CTRL_ACTIVE)
903 return -EINPROGRESS;
904
905 len = uhci_actual_length(ctrlstat);
906 urb->actual_length += len;
907
908 if (status) {
909 ret = uhci_map_status(status,
910 uhci_packetout(td_token(td)));
911 if ((debug == 1 && ret != -EPIPE) || debug > 1) {
912 /* Some debugging code */
David Brownellbe3cbc52006-06-05 12:16:39 -0400913 dev_dbg(&urb->dev->dev,
Alan Sternb1869002006-05-12 11:14:25 -0400914 "%s: failed with status %x\n",
915 __FUNCTION__, status);
916
917 if (debug > 1 && errbuf) {
918 /* Print the chain for debugging */
919 uhci_show_qh(urbp->qh, errbuf,
920 ERRBUF_LEN, 0);
921 lprintk(errbuf);
922 }
923 }
924
925 } else if (len < uhci_expected_length(td_token(td))) {
926
927 /* We received a short packet */
928 if (urb->transfer_flags & URB_SHORT_NOT_OK)
929 ret = -EREMOTEIO;
930 else if (ctrlstat & TD_CTRL_SPD)
931 ret = 1;
932 }
933
Alan Stern04538a22006-05-12 11:29:04 -0400934 uhci_remove_td_from_urbp(td);
Alan Stern59e29ed2006-05-12 11:19:19 -0400935 if (qh->post_td)
Alan Stern04538a22006-05-12 11:29:04 -0400936 uhci_free_td(uhci, qh->post_td);
Alan Stern59e29ed2006-05-12 11:19:19 -0400937 qh->post_td = td;
938
Alan Sternb1869002006-05-12 11:14:25 -0400939 if (ret != 0)
940 goto err;
941 }
942 return ret;
943
944err:
945 if (ret < 0) {
946 /* In case a control transfer gets an error
947 * during the setup stage */
948 urb->actual_length = max(urb->actual_length, 0);
949
950 /* Note that the queue has stopped and save
951 * the next toggle value */
952 qh->element = UHCI_PTR_TERM;
953 qh->is_stopped = 1;
954 qh->needs_fixup = (qh->type != USB_ENDPOINT_XFER_CONTROL);
955 qh->initial_toggle = uhci_toggle(td_token(td)) ^
956 (ret == -EREMOTEIO);
957
958 } else /* Short packet received */
Alan Stern59e29ed2006-05-12 11:19:19 -0400959 ret = uhci_fixup_short_transfer(uhci, qh, urbp);
Alan Sternb1869002006-05-12 11:14:25 -0400960 return ret;
961}
962
963/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964 * Isochronous transfers
965 */
Alan Sterndccf4a42005-12-17 17:58:46 -0500966static int uhci_submit_isochronous(struct uhci_hcd *uhci, struct urb *urb,
967 struct uhci_qh *qh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968{
Alan Sterndccf4a42005-12-17 17:58:46 -0500969 struct uhci_td *td = NULL; /* Since urb->number_of_packets > 0 */
Alan Stern0ed8fee2005-12-17 18:02:38 -0500970 int i, frame;
Alan Sterndccf4a42005-12-17 17:58:46 -0500971 unsigned long destination, status;
Alan Sternb81d3432005-10-13 17:00:24 -0400972 struct urb_priv *urbp = (struct urb_priv *) urb->hcpriv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973
Alan Sterncaf38272006-05-19 16:44:55 -0400974 /* Values must not be too big (could overflow below) */
975 if (urb->interval >= UHCI_NUMFRAMES ||
976 urb->number_of_packets >= UHCI_NUMFRAMES)
977 return -EFBIG;
978
979 /* Check the period and figure out the starting frame number */
Alan Sterncaf38272006-05-19 16:44:55 -0400980 if (qh->period == 0) {
981 if (urb->transfer_flags & URB_ISO_ASAP) {
Alan Sternc8155cc2006-05-19 16:52:35 -0400982 uhci_get_current_frame_number(uhci);
Alan Sterncaf38272006-05-19 16:44:55 -0400983 urb->start_frame = uhci->frame_number + 10;
984 } else {
Alan Sternc8155cc2006-05-19 16:52:35 -0400985 i = urb->start_frame - uhci->last_iso_frame;
Alan Sterncaf38272006-05-19 16:44:55 -0400986 if (i <= 0 || i >= UHCI_NUMFRAMES)
987 return -EINVAL;
988 }
989 } else if (qh->period != urb->interval) {
990 return -EINVAL; /* Can't change the period */
991
992 } else { /* Pick up where the last URB leaves off */
993 if (list_empty(&qh->queue)) {
Alan Sternc8155cc2006-05-19 16:52:35 -0400994 frame = qh->iso_frame;
Alan Sterncaf38272006-05-19 16:44:55 -0400995 } else {
996 struct urb *lurb;
997
998 lurb = list_entry(qh->queue.prev,
999 struct urb_priv, node)->urb;
1000 frame = lurb->start_frame +
1001 lurb->number_of_packets *
1002 lurb->interval;
1003 }
1004 if (urb->transfer_flags & URB_ISO_ASAP)
1005 urb->start_frame = frame;
Alan Sternc8155cc2006-05-19 16:52:35 -04001006 else if (urb->start_frame != frame)
1007 return -EINVAL;
Alan Sterncaf38272006-05-19 16:44:55 -04001008 }
1009
1010 /* Make sure we won't have to go too far into the future */
Alan Sternc8155cc2006-05-19 16:52:35 -04001011 if (uhci_frame_before_eq(uhci->last_iso_frame + UHCI_NUMFRAMES,
Alan Sterncaf38272006-05-19 16:44:55 -04001012 urb->start_frame + urb->number_of_packets *
1013 urb->interval))
Alan Stern0ed8fee2005-12-17 18:02:38 -05001014 return -EFBIG;
1015
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 status = TD_CTRL_ACTIVE | TD_CTRL_IOS;
1017 destination = (urb->pipe & PIPE_DEVEP_MASK) | usb_packetid(urb->pipe);
1018
Alan Sternb81d3432005-10-13 17:00:24 -04001019 for (i = 0; i < urb->number_of_packets; i++) {
Alan Stern25321782005-04-25 11:14:31 -04001020 td = uhci_alloc_td(uhci);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 if (!td)
1022 return -ENOMEM;
1023
Alan Stern04538a22006-05-12 11:29:04 -04001024 uhci_add_td_to_urbp(td, urbp);
Alan Sterndccf4a42005-12-17 17:58:46 -05001025 uhci_fill_td(td, status, destination |
1026 uhci_explen(urb->iso_frame_desc[i].length),
1027 urb->transfer_dma +
1028 urb->iso_frame_desc[i].offset);
Alan Sternb81d3432005-10-13 17:00:24 -04001029 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030
Alan Sterndccf4a42005-12-17 17:58:46 -05001031 /* Set the interrupt-on-completion flag on the last packet. */
1032 td->status |= __constant_cpu_to_le32(TD_CTRL_IOC);
1033
1034 qh->skel = uhci->skel_iso_qh;
Alan Sterncaf38272006-05-19 16:44:55 -04001035 qh->period = urb->interval;
Alan Sterndccf4a42005-12-17 17:58:46 -05001036
1037 /* Add the TDs to the frame list */
Alan Sternb81d3432005-10-13 17:00:24 -04001038 frame = urb->start_frame;
1039 list_for_each_entry(td, &urbp->td_list, list) {
Alan Sterndccf4a42005-12-17 17:58:46 -05001040 uhci_insert_td_in_frame_list(uhci, td, frame);
Alan Sternc8155cc2006-05-19 16:52:35 -04001041 frame += qh->period;
1042 }
1043
1044 if (list_empty(&qh->queue)) {
1045 qh->iso_packet_desc = &urb->iso_frame_desc[0];
1046 qh->iso_frame = urb->start_frame;
1047 qh->iso_status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048 }
1049
Alan Sterndccf4a42005-12-17 17:58:46 -05001050 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051}
1052
1053static int uhci_result_isochronous(struct uhci_hcd *uhci, struct urb *urb)
1054{
Alan Sternc8155cc2006-05-19 16:52:35 -04001055 struct uhci_td *td, *tmp;
1056 struct urb_priv *urbp = urb->hcpriv;
1057 struct uhci_qh *qh = urbp->qh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058
Alan Sternc8155cc2006-05-19 16:52:35 -04001059 list_for_each_entry_safe(td, tmp, &urbp->td_list, list) {
1060 unsigned int ctrlstat;
1061 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 int actlength;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063
Alan Sternc8155cc2006-05-19 16:52:35 -04001064 if (uhci_frame_before_eq(uhci->cur_iso_frame, qh->iso_frame))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065 return -EINPROGRESS;
1066
Alan Sternc8155cc2006-05-19 16:52:35 -04001067 uhci_remove_tds_from_frame(uhci, qh->iso_frame);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068
Alan Sternc8155cc2006-05-19 16:52:35 -04001069 ctrlstat = td_status(td);
1070 if (ctrlstat & TD_CTRL_ACTIVE) {
1071 status = -EXDEV; /* TD was added too late? */
1072 } else {
1073 status = uhci_map_status(uhci_status_bits(ctrlstat),
1074 usb_pipeout(urb->pipe));
1075 actlength = uhci_actual_length(ctrlstat);
1076
1077 urb->actual_length += actlength;
1078 qh->iso_packet_desc->actual_length = actlength;
1079 qh->iso_packet_desc->status = status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080 }
1081
Alan Sternc8155cc2006-05-19 16:52:35 -04001082 if (status) {
1083 urb->error_count++;
1084 qh->iso_status = status;
1085 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086
Alan Sternc8155cc2006-05-19 16:52:35 -04001087 uhci_remove_td_from_urbp(td);
1088 uhci_free_td(uhci, td);
1089 qh->iso_frame += qh->period;
1090 ++qh->iso_packet_desc;
1091 }
1092 return qh->iso_status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093}
1094
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095static int uhci_urb_enqueue(struct usb_hcd *hcd,
Alan Sterndccf4a42005-12-17 17:58:46 -05001096 struct usb_host_endpoint *hep,
Al Viro55016f12005-10-21 03:21:58 -04001097 struct urb *urb, gfp_t mem_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098{
1099 int ret;
1100 struct uhci_hcd *uhci = hcd_to_uhci(hcd);
1101 unsigned long flags;
Alan Sterndccf4a42005-12-17 17:58:46 -05001102 struct urb_priv *urbp;
1103 struct uhci_qh *qh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104 int bustime;
1105
1106 spin_lock_irqsave(&uhci->lock, flags);
1107
1108 ret = urb->status;
1109 if (ret != -EINPROGRESS) /* URB already unlinked! */
Alan Sterndccf4a42005-12-17 17:58:46 -05001110 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111
Alan Sterndccf4a42005-12-17 17:58:46 -05001112 ret = -ENOMEM;
1113 urbp = uhci_alloc_urb_priv(uhci, urb);
1114 if (!urbp)
1115 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116
Alan Sterndccf4a42005-12-17 17:58:46 -05001117 if (hep->hcpriv)
1118 qh = (struct uhci_qh *) hep->hcpriv;
1119 else {
1120 qh = uhci_alloc_qh(uhci, urb->dev, hep);
1121 if (!qh)
1122 goto err_no_qh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123 }
Alan Sterndccf4a42005-12-17 17:58:46 -05001124 urbp->qh = qh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125
Alan Stern4de7d2c2006-05-05 16:26:58 -04001126 switch (qh->type) {
1127 case USB_ENDPOINT_XFER_CONTROL:
Alan Sterndccf4a42005-12-17 17:58:46 -05001128 ret = uhci_submit_control(uhci, urb, qh);
1129 break;
Alan Stern4de7d2c2006-05-05 16:26:58 -04001130 case USB_ENDPOINT_XFER_BULK:
Alan Sterndccf4a42005-12-17 17:58:46 -05001131 ret = uhci_submit_bulk(uhci, urb, qh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132 break;
Alan Stern4de7d2c2006-05-05 16:26:58 -04001133 case USB_ENDPOINT_XFER_INT:
Alan Sterndccf4a42005-12-17 17:58:46 -05001134 if (list_empty(&qh->queue)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135 bustime = usb_check_bandwidth(urb->dev, urb);
1136 if (bustime < 0)
1137 ret = bustime;
1138 else {
Alan Sterndccf4a42005-12-17 17:58:46 -05001139 ret = uhci_submit_interrupt(uhci, urb, qh);
1140 if (ret == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141 usb_claim_bandwidth(urb->dev, urb, bustime, 0);
1142 }
1143 } else { /* inherit from parent */
Alan Sterndccf4a42005-12-17 17:58:46 -05001144 struct urb_priv *eurbp;
1145
1146 eurbp = list_entry(qh->queue.prev, struct urb_priv,
1147 node);
1148 urb->bandwidth = eurbp->urb->bandwidth;
1149 ret = uhci_submit_interrupt(uhci, urb, qh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150 }
1151 break;
Alan Stern4de7d2c2006-05-05 16:26:58 -04001152 case USB_ENDPOINT_XFER_ISOC:
Alan Sternc8155cc2006-05-19 16:52:35 -04001153 urb->error_count = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154 bustime = usb_check_bandwidth(urb->dev, urb);
1155 if (bustime < 0) {
1156 ret = bustime;
1157 break;
1158 }
1159
Alan Sterndccf4a42005-12-17 17:58:46 -05001160 ret = uhci_submit_isochronous(uhci, urb, qh);
1161 if (ret == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162 usb_claim_bandwidth(urb->dev, urb, bustime, 1);
1163 break;
1164 }
Alan Sterndccf4a42005-12-17 17:58:46 -05001165 if (ret != 0)
1166 goto err_submit_failed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167
Alan Sterndccf4a42005-12-17 17:58:46 -05001168 /* Add this URB to the QH */
1169 urbp->qh = qh;
1170 list_add_tail(&urbp->node, &qh->queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171
Alan Sterndccf4a42005-12-17 17:58:46 -05001172 /* If the new URB is the first and only one on this QH then either
1173 * the QH is new and idle or else it's unlinked and waiting to
Alan Stern27755622006-05-05 16:32:02 -04001174 * become idle, so we can activate it right away. But only if the
1175 * queue isn't stopped. */
Alan Stern84afddd2006-05-12 11:35:45 -04001176 if (qh->queue.next == &urbp->node && !qh->is_stopped) {
Alan Sterndccf4a42005-12-17 17:58:46 -05001177 uhci_activate_qh(uhci, qh);
Alan Stern84afddd2006-05-12 11:35:45 -04001178 uhci_qh_wants_fsbr(uhci, qh);
1179 }
Alan Sterndccf4a42005-12-17 17:58:46 -05001180 goto done;
1181
1182err_submit_failed:
1183 if (qh->state == QH_STATE_IDLE)
1184 uhci_make_qh_idle(uhci, qh); /* Reclaim unused QH */
1185
1186err_no_qh:
1187 uhci_free_urb_priv(uhci, urbp);
1188
1189done:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190 spin_unlock_irqrestore(&uhci->lock, flags);
1191 return ret;
1192}
1193
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194static int uhci_urb_dequeue(struct usb_hcd *hcd, struct urb *urb)
1195{
1196 struct uhci_hcd *uhci = hcd_to_uhci(hcd);
1197 unsigned long flags;
1198 struct urb_priv *urbp;
Alan Stern10b8e472006-05-19 16:39:52 -04001199 struct uhci_qh *qh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200
1201 spin_lock_irqsave(&uhci->lock, flags);
1202 urbp = urb->hcpriv;
1203 if (!urbp) /* URB was never linked! */
1204 goto done;
Alan Stern10b8e472006-05-19 16:39:52 -04001205 qh = urbp->qh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206
Alan Sterndccf4a42005-12-17 17:58:46 -05001207 /* Remove Isochronous TDs from the frame list ASAP */
Alan Stern10b8e472006-05-19 16:39:52 -04001208 if (qh->type == USB_ENDPOINT_XFER_ISOC) {
Alan Sterndccf4a42005-12-17 17:58:46 -05001209 uhci_unlink_isochronous_tds(uhci, urb);
Alan Stern10b8e472006-05-19 16:39:52 -04001210 mb();
1211
1212 /* If the URB has already started, update the QH unlink time */
1213 uhci_get_current_frame_number(uhci);
1214 if (uhci_frame_before_eq(urb->start_frame, uhci->frame_number))
1215 qh->unlink_frame = uhci->frame_number;
1216 }
1217
1218 uhci_unlink_qh(uhci, qh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219
1220done:
1221 spin_unlock_irqrestore(&uhci->lock, flags);
1222 return 0;
1223}
1224
Alan Stern0ed8fee2005-12-17 18:02:38 -05001225/*
1226 * Finish unlinking an URB and give it back
1227 */
1228static void uhci_giveback_urb(struct uhci_hcd *uhci, struct uhci_qh *qh,
1229 struct urb *urb, struct pt_regs *regs)
1230__releases(uhci->lock)
1231__acquires(uhci->lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232{
Alan Stern0ed8fee2005-12-17 18:02:38 -05001233 struct urb_priv *urbp = (struct urb_priv *) urb->hcpriv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234
Alan Sternc8155cc2006-05-19 16:52:35 -04001235 /* When giving back the first URB in an Isochronous queue,
1236 * reinitialize the QH's iso-related members for the next URB. */
1237 if (qh->type == USB_ENDPOINT_XFER_ISOC &&
1238 urbp->node.prev == &qh->queue &&
1239 urbp->node.next != &qh->queue) {
1240 struct urb *nurb = list_entry(urbp->node.next,
1241 struct urb_priv, node)->urb;
1242
1243 qh->iso_packet_desc = &nurb->iso_frame_desc[0];
1244 qh->iso_frame = nurb->start_frame;
1245 qh->iso_status = 0;
1246 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247
Alan Stern0ed8fee2005-12-17 18:02:38 -05001248 /* Take the URB off the QH's queue. If the queue is now empty,
1249 * this is a perfect time for a toggle fixup. */
1250 list_del_init(&urbp->node);
1251 if (list_empty(&qh->queue) && qh->needs_fixup) {
1252 usb_settoggle(urb->dev, usb_pipeendpoint(urb->pipe),
1253 usb_pipeout(urb->pipe), qh->initial_toggle);
1254 qh->needs_fixup = 0;
1255 }
1256
Alan Stern0ed8fee2005-12-17 18:02:38 -05001257 uhci_free_urb_priv(uhci, urbp);
1258
Alan Stern4de7d2c2006-05-05 16:26:58 -04001259 switch (qh->type) {
1260 case USB_ENDPOINT_XFER_ISOC:
Alan Stern0ed8fee2005-12-17 18:02:38 -05001261 /* Release bandwidth for Interrupt or Isoc. transfers */
1262 if (urb->bandwidth)
1263 usb_release_bandwidth(urb->dev, urb, 1);
1264 break;
Alan Stern4de7d2c2006-05-05 16:26:58 -04001265 case USB_ENDPOINT_XFER_INT:
Alan Stern0ed8fee2005-12-17 18:02:38 -05001266 /* Release bandwidth for Interrupt or Isoc. transfers */
1267 /* Make sure we don't release if we have a queued URB */
1268 if (list_empty(&qh->queue) && urb->bandwidth)
1269 usb_release_bandwidth(urb->dev, urb, 0);
1270 else
1271 /* bandwidth was passed on to queued URB, */
1272 /* so don't let usb_unlink_urb() release it */
1273 urb->bandwidth = 0;
1274 break;
1275 }
1276
1277 spin_unlock(&uhci->lock);
1278 usb_hcd_giveback_urb(uhci_to_hcd(uhci), urb, regs);
1279 spin_lock(&uhci->lock);
1280
1281 /* If the queue is now empty, we can unlink the QH and give up its
1282 * reserved bandwidth. */
1283 if (list_empty(&qh->queue)) {
1284 uhci_unlink_qh(uhci, qh);
1285
1286 /* Bandwidth stuff not yet implemented */
Alan Sterncaf38272006-05-19 16:44:55 -04001287 qh->period = 0;
Alan Stern0ed8fee2005-12-17 18:02:38 -05001288 }
1289}
1290
1291/*
1292 * Scan the URBs in a QH's queue
1293 */
1294#define QH_FINISHED_UNLINKING(qh) \
1295 (qh->state == QH_STATE_UNLINKING && \
1296 uhci->frame_number + uhci->is_stopped != qh->unlink_frame)
1297
1298static void uhci_scan_qh(struct uhci_hcd *uhci, struct uhci_qh *qh,
1299 struct pt_regs *regs)
1300{
1301 struct urb_priv *urbp;
1302 struct urb *urb;
1303 int status;
1304
1305 while (!list_empty(&qh->queue)) {
1306 urbp = list_entry(qh->queue.next, struct urb_priv, node);
1307 urb = urbp->urb;
1308
Alan Sternb1869002006-05-12 11:14:25 -04001309 if (qh->type == USB_ENDPOINT_XFER_ISOC)
Alan Stern0ed8fee2005-12-17 18:02:38 -05001310 status = uhci_result_isochronous(uhci, urb);
Alan Sternb1869002006-05-12 11:14:25 -04001311 else
Alan Stern0ed8fee2005-12-17 18:02:38 -05001312 status = uhci_result_common(uhci, urb);
Alan Stern0ed8fee2005-12-17 18:02:38 -05001313 if (status == -EINPROGRESS)
1314 break;
1315
1316 spin_lock(&urb->lock);
1317 if (urb->status == -EINPROGRESS) /* Not dequeued */
1318 urb->status = status;
1319 else
Alan Stern27755622006-05-05 16:32:02 -04001320 status = ECONNRESET; /* Not -ECONNRESET */
Alan Stern0ed8fee2005-12-17 18:02:38 -05001321 spin_unlock(&urb->lock);
1322
1323 /* Dequeued but completed URBs can't be given back unless
1324 * the QH is stopped or has finished unlinking. */
Alan Stern27755622006-05-05 16:32:02 -04001325 if (status == ECONNRESET) {
1326 if (QH_FINISHED_UNLINKING(qh))
1327 qh->is_stopped = 1;
1328 else if (!qh->is_stopped)
1329 return;
1330 }
Alan Stern0ed8fee2005-12-17 18:02:38 -05001331
1332 uhci_giveback_urb(uhci, qh, urb, regs);
Alan Stern27755622006-05-05 16:32:02 -04001333 if (status < 0)
Alan Stern0ed8fee2005-12-17 18:02:38 -05001334 break;
1335 }
1336
1337 /* If the QH is neither stopped nor finished unlinking (normal case),
1338 * our work here is done. */
Alan Stern27755622006-05-05 16:32:02 -04001339 if (QH_FINISHED_UNLINKING(qh))
1340 qh->is_stopped = 1;
1341 else if (!qh->is_stopped)
Alan Stern0ed8fee2005-12-17 18:02:38 -05001342 return;
1343
1344 /* Otherwise give back each of the dequeued URBs */
Alan Stern27755622006-05-05 16:32:02 -04001345restart:
Alan Stern0ed8fee2005-12-17 18:02:38 -05001346 list_for_each_entry(urbp, &qh->queue, node) {
1347 urb = urbp->urb;
1348 if (urb->status != -EINPROGRESS) {
Alan Stern10b8e472006-05-19 16:39:52 -04001349
1350 /* Fix up the TD links and save the toggles for
1351 * non-Isochronous queues. For Isochronous queues,
1352 * test for too-recent dequeues. */
1353 if (!uhci_cleanup_queue(uhci, qh, urb)) {
1354 qh->is_stopped = 0;
1355 return;
1356 }
Alan Stern0ed8fee2005-12-17 18:02:38 -05001357 uhci_giveback_urb(uhci, qh, urb, regs);
1358 goto restart;
1359 }
1360 }
1361 qh->is_stopped = 0;
1362
1363 /* There are no more dequeued URBs. If there are still URBs on the
1364 * queue, the QH can now be re-activated. */
1365 if (!list_empty(&qh->queue)) {
1366 if (qh->needs_fixup)
1367 uhci_fixup_toggles(qh, 0);
Alan Stern84afddd2006-05-12 11:35:45 -04001368
1369 /* If the first URB on the queue wants FSBR but its time
1370 * limit has expired, set the next TD to interrupt on
1371 * completion before reactivating the QH. */
1372 urbp = list_entry(qh->queue.next, struct urb_priv, node);
1373 if (urbp->fsbr && qh->wait_expired) {
1374 struct uhci_td *td = list_entry(urbp->td_list.next,
1375 struct uhci_td, list);
1376
1377 td->status |= __cpu_to_le32(TD_CTRL_IOC);
1378 }
1379
Alan Stern0ed8fee2005-12-17 18:02:38 -05001380 uhci_activate_qh(uhci, qh);
1381 }
1382
1383 /* The queue is empty. The QH can become idle if it is fully
1384 * unlinked. */
1385 else if (QH_FINISHED_UNLINKING(qh))
1386 uhci_make_qh_idle(uhci, qh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387}
1388
Alan Stern0ed8fee2005-12-17 18:02:38 -05001389/*
Alan Stern84afddd2006-05-12 11:35:45 -04001390 * Check for queues that have made some forward progress.
1391 * Returns 0 if the queue is not Isochronous, is ACTIVE, and
1392 * has not advanced since last examined; 1 otherwise.
Alan Sternb761d9d2006-05-12 11:41:59 -04001393 *
1394 * Early Intel controllers have a bug which causes qh->element sometimes
1395 * not to advance when a TD completes successfully. The queue remains
1396 * stuck on the inactive completed TD. We detect such cases and advance
1397 * the element pointer by hand.
Alan Stern84afddd2006-05-12 11:35:45 -04001398 */
1399static int uhci_advance_check(struct uhci_hcd *uhci, struct uhci_qh *qh)
1400{
1401 struct urb_priv *urbp = NULL;
1402 struct uhci_td *td;
1403 int ret = 1;
1404 unsigned status;
1405
1406 if (qh->type == USB_ENDPOINT_XFER_ISOC)
1407 return ret;
1408
1409 /* Treat an UNLINKING queue as though it hasn't advanced.
1410 * This is okay because reactivation will treat it as though
1411 * it has advanced, and if it is going to become IDLE then
1412 * this doesn't matter anyway. Furthermore it's possible
1413 * for an UNLINKING queue not to have any URBs at all, or
1414 * for its first URB not to have any TDs (if it was dequeued
1415 * just as it completed). So it's not easy in any case to
1416 * test whether such queues have advanced. */
1417 if (qh->state != QH_STATE_ACTIVE) {
1418 urbp = NULL;
1419 status = 0;
1420
1421 } else {
1422 urbp = list_entry(qh->queue.next, struct urb_priv, node);
1423 td = list_entry(urbp->td_list.next, struct uhci_td, list);
1424 status = td_status(td);
1425 if (!(status & TD_CTRL_ACTIVE)) {
1426
1427 /* We're okay, the queue has advanced */
1428 qh->wait_expired = 0;
1429 qh->advance_jiffies = jiffies;
1430 return ret;
1431 }
1432 ret = 0;
1433 }
1434
1435 /* The queue hasn't advanced; check for timeout */
1436 if (!qh->wait_expired && time_after(jiffies,
1437 qh->advance_jiffies + QH_WAIT_TIMEOUT)) {
Alan Sternb761d9d2006-05-12 11:41:59 -04001438
1439 /* Detect the Intel bug and work around it */
1440 if (qh->post_td && qh_element(qh) ==
1441 cpu_to_le32(qh->post_td->dma_handle)) {
1442 qh->element = qh->post_td->link;
1443 qh->advance_jiffies = jiffies;
1444 return 1;
1445 }
1446
Alan Stern84afddd2006-05-12 11:35:45 -04001447 qh->wait_expired = 1;
1448
1449 /* If the current URB wants FSBR, unlink it temporarily
1450 * so that we can safely set the next TD to interrupt on
1451 * completion. That way we'll know as soon as the queue
1452 * starts moving again. */
1453 if (urbp && urbp->fsbr && !(status & TD_CTRL_IOC))
1454 uhci_unlink_qh(uhci, qh);
1455 }
1456 return ret;
1457}
1458
1459/*
Alan Stern0ed8fee2005-12-17 18:02:38 -05001460 * Process events in the schedule, but only in one thread at a time
1461 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462static void uhci_scan_schedule(struct uhci_hcd *uhci, struct pt_regs *regs)
1463{
Alan Stern0ed8fee2005-12-17 18:02:38 -05001464 int i;
1465 struct uhci_qh *qh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466
1467 /* Don't allow re-entrant calls */
1468 if (uhci->scan_in_progress) {
1469 uhci->need_rescan = 1;
1470 return;
1471 }
1472 uhci->scan_in_progress = 1;
Alan Stern84afddd2006-05-12 11:35:45 -04001473rescan:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474 uhci->need_rescan = 0;
1475
Alan Stern6c1b4452005-04-21 16:04:58 -04001476 uhci_clear_next_interrupt(uhci);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477 uhci_get_current_frame_number(uhci);
Alan Sternc8155cc2006-05-19 16:52:35 -04001478 uhci->cur_iso_frame = uhci->frame_number;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479
Alan Stern0ed8fee2005-12-17 18:02:38 -05001480 /* Go through all the QH queues and process the URBs in each one */
1481 for (i = 0; i < UHCI_NUM_SKELQH - 1; ++i) {
1482 uhci->next_qh = list_entry(uhci->skelqh[i]->node.next,
1483 struct uhci_qh, node);
1484 while ((qh = uhci->next_qh) != uhci->skelqh[i]) {
1485 uhci->next_qh = list_entry(qh->node.next,
1486 struct uhci_qh, node);
Alan Stern84afddd2006-05-12 11:35:45 -04001487
1488 if (uhci_advance_check(uhci, qh)) {
1489 uhci_scan_qh(uhci, qh, regs);
1490 if (qh->state == QH_STATE_ACTIVE)
1491 uhci_qh_wants_fsbr(uhci, qh);
1492 }
Alan Stern0ed8fee2005-12-17 18:02:38 -05001493 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495
Alan Sternc8155cc2006-05-19 16:52:35 -04001496 uhci->last_iso_frame = uhci->cur_iso_frame;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497 if (uhci->need_rescan)
1498 goto rescan;
1499 uhci->scan_in_progress = 0;
1500
Alan Stern84afddd2006-05-12 11:35:45 -04001501 if (uhci->fsbr_is_on && time_after(jiffies,
1502 uhci->fsbr_jiffies + FSBR_OFF_DELAY))
1503 uhci_fsbr_off(uhci);
1504
Alan Stern04538a22006-05-12 11:29:04 -04001505 if (list_empty(&uhci->skel_unlink_qh->node))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506 uhci_clear_next_interrupt(uhci);
1507 else
1508 uhci_set_next_interrupt(uhci);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509}