blob: 5afcc52272847583b0f9f280286c86699127f0c1 [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
Alan Sternc5e3b742006-06-05 12:28:57 -040067static void uhci_urbp_wants_fsbr(struct uhci_hcd *uhci, struct urb_priv *urbp)
Alan Stern84afddd2006-05-12 11:35:45 -040068{
Alan Stern84afddd2006-05-12 11:35:45 -040069 if (urbp->fsbr) {
Alan Sternc5e3b742006-06-05 12:28:57 -040070 uhci->fsbr_is_wanted = 1;
Alan Stern84afddd2006-05-12 11:35:45 -040071 if (!uhci->fsbr_is_on)
72 uhci_fsbr_on(uhci);
Alan Sternc5e3b742006-06-05 12:28:57 -040073 else if (uhci->fsbr_expiring) {
74 uhci->fsbr_expiring = 0;
75 del_timer(&uhci->fsbr_timer);
76 }
Alan Stern84afddd2006-05-12 11:35:45 -040077 }
78}
79
Alan Sternc5e3b742006-06-05 12:28:57 -040080static void uhci_fsbr_timeout(unsigned long _uhci)
81{
82 struct uhci_hcd *uhci = (struct uhci_hcd *) _uhci;
83 unsigned long flags;
84
85 spin_lock_irqsave(&uhci->lock, flags);
86 if (uhci->fsbr_expiring) {
87 uhci->fsbr_expiring = 0;
88 uhci_fsbr_off(uhci);
89 }
90 spin_unlock_irqrestore(&uhci->lock, flags);
91}
92
Alan Stern84afddd2006-05-12 11:35:45 -040093
Alan Stern25321782005-04-25 11:14:31 -040094static struct uhci_td *uhci_alloc_td(struct uhci_hcd *uhci)
Linus Torvalds1da177e2005-04-16 15:20:36 -070095{
96 dma_addr_t dma_handle;
97 struct uhci_td *td;
98
99 td = dma_pool_alloc(uhci->td_pool, GFP_ATOMIC, &dma_handle);
100 if (!td)
101 return NULL;
102
103 td->dma_handle = dma_handle;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 td->frame = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105
106 INIT_LIST_HEAD(&td->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 INIT_LIST_HEAD(&td->fl_list);
108
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 return td;
110}
111
Alan Sterndccf4a42005-12-17 17:58:46 -0500112static void uhci_free_td(struct uhci_hcd *uhci, struct uhci_td *td)
113{
114 if (!list_empty(&td->list))
115 dev_warn(uhci_dev(uhci), "td %p still in list!\n", td);
Alan Sterndccf4a42005-12-17 17:58:46 -0500116 if (!list_empty(&td->fl_list))
117 dev_warn(uhci_dev(uhci), "td %p still in fl_list!\n", td);
118
119 dma_pool_free(uhci->td_pool, td, td->dma_handle);
120}
121
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122static inline void uhci_fill_td(struct uhci_td *td, u32 status,
123 u32 token, u32 buffer)
124{
125 td->status = cpu_to_le32(status);
126 td->token = cpu_to_le32(token);
127 td->buffer = cpu_to_le32(buffer);
128}
129
Alan Stern04538a22006-05-12 11:29:04 -0400130static void uhci_add_td_to_urbp(struct uhci_td *td, struct urb_priv *urbp)
131{
132 list_add_tail(&td->list, &urbp->td_list);
133}
134
135static void uhci_remove_td_from_urbp(struct uhci_td *td)
136{
137 list_del_init(&td->list);
138}
139
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140/*
Alan Stern687f5f32005-11-30 17:16:19 -0500141 * We insert Isochronous URBs directly into the frame list at the beginning
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 */
Alan Sterndccf4a42005-12-17 17:58:46 -0500143static inline void uhci_insert_td_in_frame_list(struct uhci_hcd *uhci,
144 struct uhci_td *td, unsigned framenum)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145{
146 framenum &= (UHCI_NUMFRAMES - 1);
147
148 td->frame = framenum;
149
150 /* Is there a TD already mapped there? */
Alan Sterna1d59ce2005-09-16 14:22:51 -0400151 if (uhci->frame_cpu[framenum]) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 struct uhci_td *ftd, *ltd;
153
Alan Sterna1d59ce2005-09-16 14:22:51 -0400154 ftd = uhci->frame_cpu[framenum];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 ltd = list_entry(ftd->fl_list.prev, struct uhci_td, fl_list);
156
157 list_add_tail(&td->fl_list, &ftd->fl_list);
158
159 td->link = ltd->link;
160 wmb();
161 ltd->link = cpu_to_le32(td->dma_handle);
162 } else {
Alan Sterna1d59ce2005-09-16 14:22:51 -0400163 td->link = uhci->frame[framenum];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 wmb();
Alan Sterna1d59ce2005-09-16 14:22:51 -0400165 uhci->frame[framenum] = cpu_to_le32(td->dma_handle);
166 uhci->frame_cpu[framenum] = td;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 }
168}
169
Alan Sterndccf4a42005-12-17 17:58:46 -0500170static inline void uhci_remove_td_from_frame_list(struct uhci_hcd *uhci,
Alan Sternb81d3432005-10-13 17:00:24 -0400171 struct uhci_td *td)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172{
173 /* If it's not inserted, don't remove it */
Alan Sternb81d3432005-10-13 17:00:24 -0400174 if (td->frame == -1) {
175 WARN_ON(!list_empty(&td->fl_list));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 return;
Alan Sternb81d3432005-10-13 17:00:24 -0400177 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178
Alan Sternb81d3432005-10-13 17:00:24 -0400179 if (uhci->frame_cpu[td->frame] == td) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 if (list_empty(&td->fl_list)) {
Alan Sterna1d59ce2005-09-16 14:22:51 -0400181 uhci->frame[td->frame] = td->link;
182 uhci->frame_cpu[td->frame] = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 } else {
184 struct uhci_td *ntd;
185
186 ntd = list_entry(td->fl_list.next, struct uhci_td, fl_list);
Alan Sterna1d59ce2005-09-16 14:22:51 -0400187 uhci->frame[td->frame] = cpu_to_le32(ntd->dma_handle);
188 uhci->frame_cpu[td->frame] = ntd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 }
190 } else {
191 struct uhci_td *ptd;
192
193 ptd = list_entry(td->fl_list.prev, struct uhci_td, fl_list);
194 ptd->link = td->link;
195 }
196
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 list_del_init(&td->fl_list);
198 td->frame = -1;
199}
200
Alan Sternc8155cc2006-05-19 16:52:35 -0400201static inline void uhci_remove_tds_from_frame(struct uhci_hcd *uhci,
202 unsigned int framenum)
203{
204 struct uhci_td *ftd, *ltd;
205
206 framenum &= (UHCI_NUMFRAMES - 1);
207
208 ftd = uhci->frame_cpu[framenum];
209 if (ftd) {
210 ltd = list_entry(ftd->fl_list.prev, struct uhci_td, fl_list);
211 uhci->frame[framenum] = ltd->link;
212 uhci->frame_cpu[framenum] = NULL;
213
214 while (!list_empty(&ftd->fl_list))
215 list_del_init(ftd->fl_list.prev);
216 }
217}
218
Alan Sterndccf4a42005-12-17 17:58:46 -0500219/*
220 * Remove all the TDs for an Isochronous URB from the frame list
221 */
222static void uhci_unlink_isochronous_tds(struct uhci_hcd *uhci, struct urb *urb)
Alan Sternb81d3432005-10-13 17:00:24 -0400223{
224 struct urb_priv *urbp = (struct urb_priv *) urb->hcpriv;
225 struct uhci_td *td;
226
227 list_for_each_entry(td, &urbp->td_list, list)
Alan Sterndccf4a42005-12-17 17:58:46 -0500228 uhci_remove_td_from_frame_list(uhci, td);
Alan Sternb81d3432005-10-13 17:00:24 -0400229}
230
Alan Sterndccf4a42005-12-17 17:58:46 -0500231static struct uhci_qh *uhci_alloc_qh(struct uhci_hcd *uhci,
232 struct usb_device *udev, struct usb_host_endpoint *hep)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233{
234 dma_addr_t dma_handle;
235 struct uhci_qh *qh;
236
237 qh = dma_pool_alloc(uhci->qh_pool, GFP_ATOMIC, &dma_handle);
238 if (!qh)
239 return NULL;
240
Alan Stern59e29ed2006-05-12 11:19:19 -0400241 memset(qh, 0, sizeof(*qh));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 qh->dma_handle = dma_handle;
243
244 qh->element = UHCI_PTR_TERM;
245 qh->link = UHCI_PTR_TERM;
246
Alan Sterndccf4a42005-12-17 17:58:46 -0500247 INIT_LIST_HEAD(&qh->queue);
248 INIT_LIST_HEAD(&qh->node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249
Alan Sterndccf4a42005-12-17 17:58:46 -0500250 if (udev) { /* Normal QH */
Alan Stern85a975d2007-01-08 12:01:43 -0500251 qh->type = hep->desc.bmAttributes & USB_ENDPOINT_XFERTYPE_MASK;
252 if (qh->type != USB_ENDPOINT_XFER_ISOC) {
253 qh->dummy_td = uhci_alloc_td(uhci);
254 if (!qh->dummy_td) {
255 dma_pool_free(uhci->qh_pool, qh, dma_handle);
256 return NULL;
257 }
Alan Sternaf0bb592005-12-17 18:00:12 -0500258 }
Alan Sterndccf4a42005-12-17 17:58:46 -0500259 qh->state = QH_STATE_IDLE;
260 qh->hep = hep;
261 qh->udev = udev;
262 hep->hcpriv = qh;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263
Alan Sterndccf4a42005-12-17 17:58:46 -0500264 } else { /* Skeleton QH */
265 qh->state = QH_STATE_ACTIVE;
Alan Stern4de7d2c2006-05-05 16:26:58 -0400266 qh->type = -1;
Alan Sterndccf4a42005-12-17 17:58:46 -0500267 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 return qh;
269}
270
271static void uhci_free_qh(struct uhci_hcd *uhci, struct uhci_qh *qh)
272{
Alan Sterndccf4a42005-12-17 17:58:46 -0500273 WARN_ON(qh->state != QH_STATE_IDLE && qh->udev);
274 if (!list_empty(&qh->queue))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 dev_warn(uhci_dev(uhci), "qh %p list not empty!\n", qh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276
Alan Sterndccf4a42005-12-17 17:58:46 -0500277 list_del(&qh->node);
278 if (qh->udev) {
279 qh->hep->hcpriv = NULL;
Alan Stern85a975d2007-01-08 12:01:43 -0500280 if (qh->dummy_td)
281 uhci_free_td(uhci, qh->dummy_td);
Alan Sterndccf4a42005-12-17 17:58:46 -0500282 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 dma_pool_free(uhci->qh_pool, qh, qh->dma_handle);
284}
285
286/*
Alan Sterna0b458b2006-05-12 11:23:19 -0400287 * When a queue is stopped and a dequeued URB is given back, adjust
288 * the previous TD link (if the URB isn't first on the queue) or
289 * save its toggle value (if it is first and is currently executing).
Alan Stern10b8e472006-05-19 16:39:52 -0400290 *
291 * Returns 0 if the URB should not yet be given back, 1 otherwise.
Alan Stern0ed8fee2005-12-17 18:02:38 -0500292 */
Alan Stern10b8e472006-05-19 16:39:52 -0400293static int uhci_cleanup_queue(struct uhci_hcd *uhci, struct uhci_qh *qh,
Alan Sterna0b458b2006-05-12 11:23:19 -0400294 struct urb *urb)
Alan Stern0ed8fee2005-12-17 18:02:38 -0500295{
Alan Sterna0b458b2006-05-12 11:23:19 -0400296 struct urb_priv *urbp = urb->hcpriv;
Alan Stern0ed8fee2005-12-17 18:02:38 -0500297 struct uhci_td *td;
Alan Stern10b8e472006-05-19 16:39:52 -0400298 int ret = 1;
Alan Stern0ed8fee2005-12-17 18:02:38 -0500299
Alan Sterna0b458b2006-05-12 11:23:19 -0400300 /* Isochronous pipes don't use toggles and their TD link pointers
Alan Stern10b8e472006-05-19 16:39:52 -0400301 * get adjusted during uhci_urb_dequeue(). But since their queues
302 * cannot truly be stopped, we have to watch out for dequeues
303 * occurring after the nominal unlink frame. */
304 if (qh->type == USB_ENDPOINT_XFER_ISOC) {
305 ret = (uhci->frame_number + uhci->is_stopped !=
306 qh->unlink_frame);
Alan Sternc5e3b742006-06-05 12:28:57 -0400307 goto done;
Alan Stern10b8e472006-05-19 16:39:52 -0400308 }
Alan Sterna0b458b2006-05-12 11:23:19 -0400309
310 /* If the URB isn't first on its queue, adjust the link pointer
311 * of the last TD in the previous URB. The toggle doesn't need
312 * to be saved since this URB can't be executing yet. */
313 if (qh->queue.next != &urbp->node) {
314 struct urb_priv *purbp;
315 struct uhci_td *ptd;
316
317 purbp = list_entry(urbp->node.prev, struct urb_priv, node);
318 WARN_ON(list_empty(&purbp->td_list));
319 ptd = list_entry(purbp->td_list.prev, struct uhci_td,
320 list);
321 td = list_entry(urbp->td_list.prev, struct uhci_td,
322 list);
323 ptd->link = td->link;
Alan Sternc5e3b742006-06-05 12:28:57 -0400324 goto done;
Alan Sterna0b458b2006-05-12 11:23:19 -0400325 }
326
Alan Stern0ed8fee2005-12-17 18:02:38 -0500327 /* If the QH element pointer is UHCI_PTR_TERM then then currently
328 * executing URB has already been unlinked, so this one isn't it. */
Alan Sterna0b458b2006-05-12 11:23:19 -0400329 if (qh_element(qh) == UHCI_PTR_TERM)
Alan Sternc5e3b742006-06-05 12:28:57 -0400330 goto done;
Alan Stern0ed8fee2005-12-17 18:02:38 -0500331 qh->element = UHCI_PTR_TERM;
332
Alan Stern85a975d2007-01-08 12:01:43 -0500333 /* Control pipes don't have to worry about toggles */
Alan Sterna0b458b2006-05-12 11:23:19 -0400334 if (qh->type == USB_ENDPOINT_XFER_CONTROL)
Alan Sternc5e3b742006-06-05 12:28:57 -0400335 goto done;
Alan Stern0ed8fee2005-12-17 18:02:38 -0500336
Alan Sterna0b458b2006-05-12 11:23:19 -0400337 /* Save the next toggle value */
Alan Stern59e29ed2006-05-12 11:19:19 -0400338 WARN_ON(list_empty(&urbp->td_list));
339 td = list_entry(urbp->td_list.next, struct uhci_td, list);
340 qh->needs_fixup = 1;
341 qh->initial_toggle = uhci_toggle(td_token(td));
Alan Sternc5e3b742006-06-05 12:28:57 -0400342
343done:
Alan Stern10b8e472006-05-19 16:39:52 -0400344 return ret;
Alan Stern0ed8fee2005-12-17 18:02:38 -0500345}
346
347/*
348 * Fix up the data toggles for URBs in a queue, when one of them
349 * terminates early (short transfer, error, or dequeued).
350 */
351static void uhci_fixup_toggles(struct uhci_qh *qh, int skip_first)
352{
353 struct urb_priv *urbp = NULL;
354 struct uhci_td *td;
355 unsigned int toggle = qh->initial_toggle;
356 unsigned int pipe;
357
358 /* Fixups for a short transfer start with the second URB in the
359 * queue (the short URB is the first). */
360 if (skip_first)
361 urbp = list_entry(qh->queue.next, struct urb_priv, node);
362
363 /* When starting with the first URB, if the QH element pointer is
364 * still valid then we know the URB's toggles are okay. */
365 else if (qh_element(qh) != UHCI_PTR_TERM)
366 toggle = 2;
367
368 /* Fix up the toggle for the URBs in the queue. Normally this
369 * loop won't run more than once: When an error or short transfer
370 * occurs, the queue usually gets emptied. */
Alan Stern1393adb2006-01-31 10:02:55 -0500371 urbp = list_prepare_entry(urbp, &qh->queue, node);
Alan Stern0ed8fee2005-12-17 18:02:38 -0500372 list_for_each_entry_continue(urbp, &qh->queue, node) {
373
374 /* If the first TD has the right toggle value, we don't
375 * need to change any toggles in this URB */
376 td = list_entry(urbp->td_list.next, struct uhci_td, list);
377 if (toggle > 1 || uhci_toggle(td_token(td)) == toggle) {
Alan Sterndb59b462006-08-31 14:18:39 -0400378 td = list_entry(urbp->td_list.prev, struct uhci_td,
Alan Stern0ed8fee2005-12-17 18:02:38 -0500379 list);
380 toggle = uhci_toggle(td_token(td)) ^ 1;
381
382 /* Otherwise all the toggles in the URB have to be switched */
383 } else {
384 list_for_each_entry(td, &urbp->td_list, list) {
385 td->token ^= __constant_cpu_to_le32(
386 TD_TOKEN_TOGGLE);
387 toggle ^= 1;
388 }
389 }
390 }
391
392 wmb();
393 pipe = list_entry(qh->queue.next, struct urb_priv, node)->urb->pipe;
394 usb_settoggle(qh->udev, usb_pipeendpoint(pipe),
395 usb_pipeout(pipe), toggle);
396 qh->needs_fixup = 0;
397}
398
399/*
Alan Sterndccf4a42005-12-17 17:58:46 -0500400 * Put a QH on the schedule in both hardware and software
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 */
Alan Sterndccf4a42005-12-17 17:58:46 -0500402static void uhci_activate_qh(struct uhci_hcd *uhci, struct uhci_qh *qh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403{
Alan Sterndccf4a42005-12-17 17:58:46 -0500404 struct uhci_qh *pqh;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405
Alan Sterndccf4a42005-12-17 17:58:46 -0500406 WARN_ON(list_empty(&qh->queue));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407
Alan Sterndccf4a42005-12-17 17:58:46 -0500408 /* Set the element pointer if it isn't set already.
409 * This isn't needed for Isochronous queues, but it doesn't hurt. */
410 if (qh_element(qh) == UHCI_PTR_TERM) {
411 struct urb_priv *urbp = list_entry(qh->queue.next,
412 struct urb_priv, node);
413 struct uhci_td *td = list_entry(urbp->td_list.next,
414 struct uhci_td, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415
Alan Sterndccf4a42005-12-17 17:58:46 -0500416 qh->element = cpu_to_le32(td->dma_handle);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 }
418
Alan Stern84afddd2006-05-12 11:35:45 -0400419 /* Treat the queue as if it has just advanced */
420 qh->wait_expired = 0;
421 qh->advance_jiffies = jiffies;
422
Alan Sterndccf4a42005-12-17 17:58:46 -0500423 if (qh->state == QH_STATE_ACTIVE)
424 return;
425 qh->state = QH_STATE_ACTIVE;
426
427 /* Move the QH from its old list to the end of the appropriate
428 * skeleton's 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, &qh->skel->node);
433
434 /* Link it into the schedule */
435 pqh = list_entry(qh->node.prev, struct uhci_qh, node);
436 qh->link = pqh->link;
437 wmb();
438 pqh->link = UHCI_PTR_QH | cpu_to_le32(qh->dma_handle);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439}
440
441/*
Alan Sterndccf4a42005-12-17 17:58:46 -0500442 * Take a QH off the hardware schedule
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 */
Alan Sterndccf4a42005-12-17 17:58:46 -0500444static void uhci_unlink_qh(struct uhci_hcd *uhci, struct uhci_qh *qh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445{
446 struct uhci_qh *pqh;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447
Alan Sterndccf4a42005-12-17 17:58:46 -0500448 if (qh->state == QH_STATE_UNLINKING)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 return;
Alan Sterndccf4a42005-12-17 17:58:46 -0500450 WARN_ON(qh->state != QH_STATE_ACTIVE || !qh->udev);
451 qh->state = QH_STATE_UNLINKING;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452
Alan Sterndccf4a42005-12-17 17:58:46 -0500453 /* Unlink the QH from the schedule and record when we did it */
454 pqh = list_entry(qh->node.prev, struct uhci_qh, node);
455 pqh->link = qh->link;
456 mb();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457
458 uhci_get_current_frame_number(uhci);
Alan Sterndccf4a42005-12-17 17:58:46 -0500459 qh->unlink_frame = uhci->frame_number;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460
Alan Sterndccf4a42005-12-17 17:58:46 -0500461 /* Force an interrupt so we know when the QH is fully unlinked */
462 if (list_empty(&uhci->skel_unlink_qh->node))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 uhci_set_next_interrupt(uhci);
464
Alan Sterndccf4a42005-12-17 17:58:46 -0500465 /* Move the QH from its old list to the end of the unlinking list */
Alan Stern0ed8fee2005-12-17 18:02:38 -0500466 if (qh == uhci->next_qh)
467 uhci->next_qh = list_entry(qh->node.next, struct uhci_qh,
468 node);
Alan Sterndccf4a42005-12-17 17:58:46 -0500469 list_move_tail(&qh->node, &uhci->skel_unlink_qh->node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470}
471
Alan Sterndccf4a42005-12-17 17:58:46 -0500472/*
473 * When we and the controller are through with a QH, it becomes IDLE.
474 * This happens when a QH has been off the schedule (on the unlinking
475 * list) for more than one frame, or when an error occurs while adding
476 * the first URB onto a new QH.
477 */
478static void uhci_make_qh_idle(struct uhci_hcd *uhci, struct uhci_qh *qh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479{
Alan Sterndccf4a42005-12-17 17:58:46 -0500480 WARN_ON(qh->state == QH_STATE_ACTIVE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481
Alan Stern0ed8fee2005-12-17 18:02:38 -0500482 if (qh == uhci->next_qh)
483 uhci->next_qh = list_entry(qh->node.next, struct uhci_qh,
484 node);
Alan Sterndccf4a42005-12-17 17:58:46 -0500485 list_move(&qh->node, &uhci->idle_qh_list);
486 qh->state = QH_STATE_IDLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487
Alan Stern59e29ed2006-05-12 11:19:19 -0400488 /* Now that the QH is idle, its post_td isn't being used */
489 if (qh->post_td) {
490 uhci_free_td(uhci, qh->post_td);
491 qh->post_td = NULL;
492 }
493
Alan Sterndccf4a42005-12-17 17:58:46 -0500494 /* If anyone is waiting for a QH to become idle, wake them up */
495 if (uhci->num_waiting)
496 wake_up_all(&uhci->waitqh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497}
498
Alan Sterndccf4a42005-12-17 17:58:46 -0500499static inline struct urb_priv *uhci_alloc_urb_priv(struct uhci_hcd *uhci,
500 struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501{
502 struct urb_priv *urbp;
503
Christoph Lameter54e6ecb2006-12-06 20:33:16 -0800504 urbp = kmem_cache_alloc(uhci_up_cachep, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 if (!urbp)
506 return NULL;
507
508 memset((void *)urbp, 0, sizeof(*urbp));
509
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 urbp->urb = urb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 urb->hcpriv = urbp;
Alan Sterndccf4a42005-12-17 17:58:46 -0500512
513 INIT_LIST_HEAD(&urbp->node);
514 INIT_LIST_HEAD(&urbp->td_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515
516 return urbp;
517}
518
Alan Sterndccf4a42005-12-17 17:58:46 -0500519static void uhci_free_urb_priv(struct uhci_hcd *uhci,
520 struct urb_priv *urbp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521{
522 struct uhci_td *td, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523
Alan Sterndccf4a42005-12-17 17:58:46 -0500524 if (!list_empty(&urbp->node))
525 dev_warn(uhci_dev(uhci), "urb %p still on QH's list!\n",
526 urbp->urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 list_for_each_entry_safe(td, tmp, &urbp->td_list, list) {
Alan Stern04538a22006-05-12 11:29:04 -0400529 uhci_remove_td_from_urbp(td);
530 uhci_free_td(uhci, td);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 }
532
Alan Sterndccf4a42005-12-17 17:58:46 -0500533 urbp->urb->hcpriv = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 kmem_cache_free(uhci_up_cachep, urbp);
535}
536
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537/*
538 * Map status to standard result codes
539 *
540 * <status> is (td_status(td) & 0xF60000), a.k.a.
541 * uhci_status_bits(td_status(td)).
542 * Note: <status> does not include the TD_CTRL_NAK bit.
543 * <dir_out> is True for output TDs and False for input TDs.
544 */
545static int uhci_map_status(int status, int dir_out)
546{
547 if (!status)
548 return 0;
549 if (status & TD_CTRL_BITSTUFF) /* Bitstuff error */
550 return -EPROTO;
551 if (status & TD_CTRL_CRCTIMEO) { /* CRC/Timeout */
552 if (dir_out)
553 return -EPROTO;
554 else
555 return -EILSEQ;
556 }
557 if (status & TD_CTRL_BABBLE) /* Babble */
558 return -EOVERFLOW;
559 if (status & TD_CTRL_DBUFERR) /* Buffer error */
560 return -ENOSR;
561 if (status & TD_CTRL_STALLED) /* Stalled */
562 return -EPIPE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 return 0;
564}
565
566/*
567 * Control transfers
568 */
Alan Sterndccf4a42005-12-17 17:58:46 -0500569static int uhci_submit_control(struct uhci_hcd *uhci, struct urb *urb,
570 struct uhci_qh *qh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 struct uhci_td *td;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 unsigned long destination, status;
Alan Sterndccf4a42005-12-17 17:58:46 -0500574 int maxsze = le16_to_cpu(qh->hep->desc.wMaxPacketSize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 int len = urb->transfer_buffer_length;
576 dma_addr_t data = urb->transfer_dma;
Alan Sterndccf4a42005-12-17 17:58:46 -0500577 __le32 *plink;
Alan Stern04538a22006-05-12 11:29:04 -0400578 struct urb_priv *urbp = urb->hcpriv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579
580 /* The "pipe" thing contains the destination in bits 8--18 */
581 destination = (urb->pipe & PIPE_DEVEP_MASK) | USB_PID_SETUP;
582
Alan Sternaf0bb592005-12-17 18:00:12 -0500583 /* 3 errors, dummy TD remains inactive */
584 status = uhci_maxerr(3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 if (urb->dev->speed == USB_SPEED_LOW)
586 status |= TD_CTRL_LS;
587
588 /*
589 * Build the TD for the control request setup packet
590 */
Alan Sternaf0bb592005-12-17 18:00:12 -0500591 td = qh->dummy_td;
Alan Stern04538a22006-05-12 11:29:04 -0400592 uhci_add_td_to_urbp(td, urbp);
Alan Sternfa346562005-11-30 11:57:51 -0500593 uhci_fill_td(td, status, destination | uhci_explen(8),
Alan Sterndccf4a42005-12-17 17:58:46 -0500594 urb->setup_dma);
595 plink = &td->link;
Alan Sternaf0bb592005-12-17 18:00:12 -0500596 status |= TD_CTRL_ACTIVE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597
598 /*
599 * If direction is "send", change the packet ID from SETUP (0x2D)
600 * to OUT (0xE1). Else change it from SETUP to IN (0x69) and
601 * set Short Packet Detect (SPD) for all data packets.
602 */
603 if (usb_pipeout(urb->pipe))
604 destination ^= (USB_PID_SETUP ^ USB_PID_OUT);
605 else {
606 destination ^= (USB_PID_SETUP ^ USB_PID_IN);
607 status |= TD_CTRL_SPD;
608 }
609
610 /*
Alan Stern687f5f32005-11-30 17:16:19 -0500611 * Build the DATA TDs
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 */
613 while (len > 0) {
Alan Sterndccf4a42005-12-17 17:58:46 -0500614 int pktsze = min(len, maxsze);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615
Alan Stern25321782005-04-25 11:14:31 -0400616 td = uhci_alloc_td(uhci);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 if (!td)
Alan Sternaf0bb592005-12-17 18:00:12 -0500618 goto nomem;
Alan Sterndccf4a42005-12-17 17:58:46 -0500619 *plink = cpu_to_le32(td->dma_handle);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620
621 /* Alternate Data0/1 (start with Data1) */
622 destination ^= TD_TOKEN_TOGGLE;
623
Alan Stern04538a22006-05-12 11:29:04 -0400624 uhci_add_td_to_urbp(td, urbp);
Alan Sternfa346562005-11-30 11:57:51 -0500625 uhci_fill_td(td, status, destination | uhci_explen(pktsze),
Alan Sterndccf4a42005-12-17 17:58:46 -0500626 data);
627 plink = &td->link;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628
629 data += pktsze;
630 len -= pktsze;
631 }
632
633 /*
634 * Build the final TD for control status
635 */
Alan Stern25321782005-04-25 11:14:31 -0400636 td = uhci_alloc_td(uhci);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 if (!td)
Alan Sternaf0bb592005-12-17 18:00:12 -0500638 goto nomem;
Alan Sterndccf4a42005-12-17 17:58:46 -0500639 *plink = cpu_to_le32(td->dma_handle);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640
641 /*
642 * It's IN if the pipe is an output pipe or we're not expecting
643 * data back.
644 */
645 destination &= ~TD_TOKEN_PID_MASK;
646 if (usb_pipeout(urb->pipe) || !urb->transfer_buffer_length)
647 destination |= USB_PID_IN;
648 else
649 destination |= USB_PID_OUT;
650
651 destination |= TD_TOKEN_TOGGLE; /* End in Data1 */
652
653 status &= ~TD_CTRL_SPD;
654
Alan Stern04538a22006-05-12 11:29:04 -0400655 uhci_add_td_to_urbp(td, urbp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 uhci_fill_td(td, status | TD_CTRL_IOC,
Alan Sterndccf4a42005-12-17 17:58:46 -0500657 destination | uhci_explen(0), 0);
Alan Sternaf0bb592005-12-17 18:00:12 -0500658 plink = &td->link;
659
660 /*
661 * Build the new dummy TD and activate the old one
662 */
663 td = uhci_alloc_td(uhci);
664 if (!td)
665 goto nomem;
666 *plink = cpu_to_le32(td->dma_handle);
667
668 uhci_fill_td(td, 0, USB_PID_OUT | uhci_explen(0), 0);
669 wmb();
670 qh->dummy_td->status |= __constant_cpu_to_le32(TD_CTRL_ACTIVE);
671 qh->dummy_td = td;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672
673 /* Low-speed transfers get a different queue, and won't hog the bus.
674 * Also, some devices enumerate better without FSBR; the easiest way
675 * to do that is to put URBs on the low-speed queue while the device
Alan Stern630aa3c2006-01-23 17:17:21 -0500676 * isn't in the CONFIGURED state. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 if (urb->dev->speed == USB_SPEED_LOW ||
Alan Stern630aa3c2006-01-23 17:17:21 -0500678 urb->dev->state != USB_STATE_CONFIGURED)
Alan Sterndccf4a42005-12-17 17:58:46 -0500679 qh->skel = uhci->skel_ls_control_qh;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 else {
Alan Sterndccf4a42005-12-17 17:58:46 -0500681 qh->skel = uhci->skel_fs_control_qh;
Alan Stern84afddd2006-05-12 11:35:45 -0400682 uhci_add_fsbr(uhci, urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 }
Alan Stern59e29ed2006-05-12 11:19:19 -0400684
685 urb->actual_length = -8; /* Account for the SETUP packet */
Alan Sterndccf4a42005-12-17 17:58:46 -0500686 return 0;
Alan Sternaf0bb592005-12-17 18:00:12 -0500687
688nomem:
689 /* Remove the dummy TD from the td_list so it doesn't get freed */
Alan Stern04538a22006-05-12 11:29:04 -0400690 uhci_remove_td_from_urbp(qh->dummy_td);
Alan Sternaf0bb592005-12-17 18:00:12 -0500691 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692}
693
694/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 * Common submit for bulk and interrupt
696 */
Alan Sterndccf4a42005-12-17 17:58:46 -0500697static int uhci_submit_common(struct uhci_hcd *uhci, struct urb *urb,
698 struct uhci_qh *qh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699{
700 struct uhci_td *td;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 unsigned long destination, status;
Alan Sterndccf4a42005-12-17 17:58:46 -0500702 int maxsze = le16_to_cpu(qh->hep->desc.wMaxPacketSize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 int len = urb->transfer_buffer_length;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 dma_addr_t data = urb->transfer_dma;
Alan Sternaf0bb592005-12-17 18:00:12 -0500705 __le32 *plink;
Alan Stern04538a22006-05-12 11:29:04 -0400706 struct urb_priv *urbp = urb->hcpriv;
Alan Sternaf0bb592005-12-17 18:00:12 -0500707 unsigned int toggle;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708
709 if (len < 0)
710 return -EINVAL;
711
712 /* The "pipe" thing contains the destination in bits 8--18 */
713 destination = (urb->pipe & PIPE_DEVEP_MASK) | usb_packetid(urb->pipe);
Alan Sternaf0bb592005-12-17 18:00:12 -0500714 toggle = usb_gettoggle(urb->dev, usb_pipeendpoint(urb->pipe),
715 usb_pipeout(urb->pipe));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716
Alan Sternaf0bb592005-12-17 18:00:12 -0500717 /* 3 errors, dummy TD remains inactive */
718 status = uhci_maxerr(3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 if (urb->dev->speed == USB_SPEED_LOW)
720 status |= TD_CTRL_LS;
721 if (usb_pipein(urb->pipe))
722 status |= TD_CTRL_SPD;
723
724 /*
Alan Stern687f5f32005-11-30 17:16:19 -0500725 * Build the DATA TDs
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 */
Alan Sternaf0bb592005-12-17 18:00:12 -0500727 plink = NULL;
728 td = qh->dummy_td;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 do { /* Allow zero length packets */
730 int pktsze = maxsze;
731
Alan Sterndccf4a42005-12-17 17:58:46 -0500732 if (len <= pktsze) { /* The last packet */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 pktsze = len;
734 if (!(urb->transfer_flags & URB_SHORT_NOT_OK))
735 status &= ~TD_CTRL_SPD;
736 }
737
Alan Sternaf0bb592005-12-17 18:00:12 -0500738 if (plink) {
739 td = uhci_alloc_td(uhci);
740 if (!td)
741 goto nomem;
742 *plink = cpu_to_le32(td->dma_handle);
743 }
Alan Stern04538a22006-05-12 11:29:04 -0400744 uhci_add_td_to_urbp(td, urbp);
Alan Sterndccf4a42005-12-17 17:58:46 -0500745 uhci_fill_td(td, status,
Alan Sternaf0bb592005-12-17 18:00:12 -0500746 destination | uhci_explen(pktsze) |
747 (toggle << TD_TOKEN_TOGGLE_SHIFT),
748 data);
Alan Sterndccf4a42005-12-17 17:58:46 -0500749 plink = &td->link;
Alan Sternaf0bb592005-12-17 18:00:12 -0500750 status |= TD_CTRL_ACTIVE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751
752 data += pktsze;
753 len -= maxsze;
Alan Sternaf0bb592005-12-17 18:00:12 -0500754 toggle ^= 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 } while (len > 0);
756
757 /*
758 * URB_ZERO_PACKET means adding a 0-length packet, if direction
759 * is OUT and the transfer_length was an exact multiple of maxsze,
760 * hence (len = transfer_length - N * maxsze) == 0
761 * however, if transfer_length == 0, the zero packet was already
762 * prepared above.
763 */
Alan Sterndccf4a42005-12-17 17:58:46 -0500764 if ((urb->transfer_flags & URB_ZERO_PACKET) &&
765 usb_pipeout(urb->pipe) && len == 0 &&
766 urb->transfer_buffer_length > 0) {
Alan Stern25321782005-04-25 11:14:31 -0400767 td = uhci_alloc_td(uhci);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 if (!td)
Alan Sternaf0bb592005-12-17 18:00:12 -0500769 goto nomem;
Alan Sterndccf4a42005-12-17 17:58:46 -0500770 *plink = cpu_to_le32(td->dma_handle);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771
Alan Stern04538a22006-05-12 11:29:04 -0400772 uhci_add_td_to_urbp(td, urbp);
Alan Sternaf0bb592005-12-17 18:00:12 -0500773 uhci_fill_td(td, status,
774 destination | uhci_explen(0) |
775 (toggle << TD_TOKEN_TOGGLE_SHIFT),
776 data);
777 plink = &td->link;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778
Alan Sternaf0bb592005-12-17 18:00:12 -0500779 toggle ^= 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 }
781
782 /* Set the interrupt-on-completion flag on the last packet.
783 * A more-or-less typical 4 KB URB (= size of one memory page)
784 * will require about 3 ms to transfer; that's a little on the
785 * fast side but not enough to justify delaying an interrupt
786 * more than 2 or 3 URBs, so we will ignore the URB_NO_INTERRUPT
787 * flag setting. */
Alan Sterndccf4a42005-12-17 17:58:46 -0500788 td->status |= __constant_cpu_to_le32(TD_CTRL_IOC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789
Alan Sternaf0bb592005-12-17 18:00:12 -0500790 /*
791 * Build the new dummy TD and activate the old one
792 */
793 td = uhci_alloc_td(uhci);
794 if (!td)
795 goto nomem;
796 *plink = cpu_to_le32(td->dma_handle);
797
798 uhci_fill_td(td, 0, USB_PID_OUT | uhci_explen(0), 0);
799 wmb();
800 qh->dummy_td->status |= __constant_cpu_to_le32(TD_CTRL_ACTIVE);
801 qh->dummy_td = td;
Alan Sterncaf38272006-05-19 16:44:55 -0400802 qh->period = urb->interval;
Alan Sternaf0bb592005-12-17 18:00:12 -0500803
804 usb_settoggle(urb->dev, usb_pipeendpoint(urb->pipe),
805 usb_pipeout(urb->pipe), toggle);
Alan Sterndccf4a42005-12-17 17:58:46 -0500806 return 0;
Alan Sternaf0bb592005-12-17 18:00:12 -0500807
808nomem:
809 /* Remove the dummy TD from the td_list so it doesn't get freed */
Alan Stern04538a22006-05-12 11:29:04 -0400810 uhci_remove_td_from_urbp(qh->dummy_td);
Alan Sternaf0bb592005-12-17 18:00:12 -0500811 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812}
813
Alan Sterndccf4a42005-12-17 17:58:46 -0500814static inline int uhci_submit_bulk(struct uhci_hcd *uhci, struct urb *urb,
815 struct uhci_qh *qh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816{
817 int ret;
818
819 /* Can't have low-speed bulk transfers */
820 if (urb->dev->speed == USB_SPEED_LOW)
821 return -EINVAL;
822
Alan Sterndccf4a42005-12-17 17:58:46 -0500823 qh->skel = uhci->skel_bulk_qh;
824 ret = uhci_submit_common(uhci, urb, qh);
825 if (ret == 0)
Alan Stern84afddd2006-05-12 11:35:45 -0400826 uhci_add_fsbr(uhci, urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 return ret;
828}
829
Alan Sterncaf38272006-05-19 16:44:55 -0400830static int uhci_submit_interrupt(struct uhci_hcd *uhci, struct urb *urb,
Alan Sterndccf4a42005-12-17 17:58:46 -0500831 struct uhci_qh *qh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832{
Alan Sterncaf38272006-05-19 16:44:55 -0400833 int exponent;
834
Alan Sterndccf4a42005-12-17 17:58:46 -0500835 /* USB 1.1 interrupt transfers only involve one packet per interval.
836 * Drivers can submit URBs of any length, but longer ones will need
837 * multiple intervals to complete.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 */
Alan Sterncaf38272006-05-19 16:44:55 -0400839
840 /* Figure out which power-of-two queue to use */
841 for (exponent = 7; exponent >= 0; --exponent) {
842 if ((1 << exponent) <= urb->interval)
843 break;
844 }
845 if (exponent < 0)
846 return -EINVAL;
847 urb->interval = 1 << exponent;
848
849 if (qh->period == 0)
850 qh->skel = uhci->skelqh[UHCI_SKEL_INDEX(exponent)];
851 else if (qh->period != urb->interval)
852 return -EINVAL; /* Can't change the period */
853
Alan Sterndccf4a42005-12-17 17:58:46 -0500854 return uhci_submit_common(uhci, urb, qh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855}
856
857/*
Alan Sternb1869002006-05-12 11:14:25 -0400858 * Fix up the data structures following a short transfer
859 */
860static int uhci_fixup_short_transfer(struct uhci_hcd *uhci,
Alan Stern59e29ed2006-05-12 11:19:19 -0400861 struct uhci_qh *qh, struct urb_priv *urbp)
Alan Sternb1869002006-05-12 11:14:25 -0400862{
863 struct uhci_td *td;
Alan Stern59e29ed2006-05-12 11:19:19 -0400864 struct list_head *tmp;
865 int ret;
Alan Sternb1869002006-05-12 11:14:25 -0400866
867 td = list_entry(urbp->td_list.prev, struct uhci_td, list);
868 if (qh->type == USB_ENDPOINT_XFER_CONTROL) {
Alan Sternb1869002006-05-12 11:14:25 -0400869
870 /* When a control transfer is short, we have to restart
871 * the queue at the status stage transaction, which is
872 * the last TD. */
Alan Stern59e29ed2006-05-12 11:19:19 -0400873 WARN_ON(list_empty(&urbp->td_list));
Alan Sternb1869002006-05-12 11:14:25 -0400874 qh->element = cpu_to_le32(td->dma_handle);
Alan Stern59e29ed2006-05-12 11:19:19 -0400875 tmp = td->list.prev;
Alan Sternb1869002006-05-12 11:14:25 -0400876 ret = -EINPROGRESS;
877
Alan Stern59e29ed2006-05-12 11:19:19 -0400878 } else {
Alan Sternb1869002006-05-12 11:14:25 -0400879
880 /* When a bulk/interrupt transfer is short, we have to
881 * fix up the toggles of the following URBs on the queue
882 * before restarting the queue at the next URB. */
Alan Stern59e29ed2006-05-12 11:19:19 -0400883 qh->initial_toggle = uhci_toggle(td_token(qh->post_td)) ^ 1;
Alan Sternb1869002006-05-12 11:14:25 -0400884 uhci_fixup_toggles(qh, 1);
885
Alan Stern59e29ed2006-05-12 11:19:19 -0400886 if (list_empty(&urbp->td_list))
887 td = qh->post_td;
Alan Sternb1869002006-05-12 11:14:25 -0400888 qh->element = td->link;
Alan Stern59e29ed2006-05-12 11:19:19 -0400889 tmp = urbp->td_list.prev;
890 ret = 0;
Alan Sternb1869002006-05-12 11:14:25 -0400891 }
892
Alan Stern59e29ed2006-05-12 11:19:19 -0400893 /* Remove all the TDs we skipped over, from tmp back to the start */
894 while (tmp != &urbp->td_list) {
895 td = list_entry(tmp, struct uhci_td, list);
896 tmp = tmp->prev;
897
Alan Stern04538a22006-05-12 11:29:04 -0400898 uhci_remove_td_from_urbp(td);
899 uhci_free_td(uhci, td);
Alan Stern59e29ed2006-05-12 11:19:19 -0400900 }
Alan Sternb1869002006-05-12 11:14:25 -0400901 return ret;
902}
903
904/*
905 * Common result for control, bulk, and interrupt
906 */
907static int uhci_result_common(struct uhci_hcd *uhci, struct urb *urb)
908{
909 struct urb_priv *urbp = urb->hcpriv;
910 struct uhci_qh *qh = urbp->qh;
Alan Stern59e29ed2006-05-12 11:19:19 -0400911 struct uhci_td *td, *tmp;
Alan Sternb1869002006-05-12 11:14:25 -0400912 unsigned status;
913 int ret = 0;
914
Alan Stern59e29ed2006-05-12 11:19:19 -0400915 list_for_each_entry_safe(td, tmp, &urbp->td_list, list) {
Alan Sternb1869002006-05-12 11:14:25 -0400916 unsigned int ctrlstat;
917 int len;
918
Alan Sternb1869002006-05-12 11:14:25 -0400919 ctrlstat = td_status(td);
920 status = uhci_status_bits(ctrlstat);
921 if (status & TD_CTRL_ACTIVE)
922 return -EINPROGRESS;
923
924 len = uhci_actual_length(ctrlstat);
925 urb->actual_length += len;
926
927 if (status) {
928 ret = uhci_map_status(status,
929 uhci_packetout(td_token(td)));
930 if ((debug == 1 && ret != -EPIPE) || debug > 1) {
931 /* Some debugging code */
David Brownellbe3cbc52006-06-05 12:16:39 -0400932 dev_dbg(&urb->dev->dev,
Alan Sternb1869002006-05-12 11:14:25 -0400933 "%s: failed with status %x\n",
934 __FUNCTION__, status);
935
936 if (debug > 1 && errbuf) {
937 /* Print the chain for debugging */
938 uhci_show_qh(urbp->qh, errbuf,
939 ERRBUF_LEN, 0);
940 lprintk(errbuf);
941 }
942 }
943
944 } else if (len < uhci_expected_length(td_token(td))) {
945
946 /* We received a short packet */
947 if (urb->transfer_flags & URB_SHORT_NOT_OK)
948 ret = -EREMOTEIO;
Alan Sternf443ddf2006-07-31 10:16:24 -0400949
950 /* Fixup needed only if this isn't the URB's last TD */
951 else if (&td->list != urbp->td_list.prev)
Alan Sternb1869002006-05-12 11:14:25 -0400952 ret = 1;
953 }
954
Alan Stern04538a22006-05-12 11:29:04 -0400955 uhci_remove_td_from_urbp(td);
Alan Stern59e29ed2006-05-12 11:19:19 -0400956 if (qh->post_td)
Alan Stern04538a22006-05-12 11:29:04 -0400957 uhci_free_td(uhci, qh->post_td);
Alan Stern59e29ed2006-05-12 11:19:19 -0400958 qh->post_td = td;
959
Alan Sternb1869002006-05-12 11:14:25 -0400960 if (ret != 0)
961 goto err;
962 }
963 return ret;
964
965err:
966 if (ret < 0) {
967 /* In case a control transfer gets an error
968 * during the setup stage */
969 urb->actual_length = max(urb->actual_length, 0);
970
971 /* Note that the queue has stopped and save
972 * the next toggle value */
973 qh->element = UHCI_PTR_TERM;
974 qh->is_stopped = 1;
975 qh->needs_fixup = (qh->type != USB_ENDPOINT_XFER_CONTROL);
976 qh->initial_toggle = uhci_toggle(td_token(td)) ^
977 (ret == -EREMOTEIO);
978
979 } else /* Short packet received */
Alan Stern59e29ed2006-05-12 11:19:19 -0400980 ret = uhci_fixup_short_transfer(uhci, qh, urbp);
Alan Sternb1869002006-05-12 11:14:25 -0400981 return ret;
982}
983
984/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 * Isochronous transfers
986 */
Alan Sterndccf4a42005-12-17 17:58:46 -0500987static int uhci_submit_isochronous(struct uhci_hcd *uhci, struct urb *urb,
988 struct uhci_qh *qh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989{
Alan Sterndccf4a42005-12-17 17:58:46 -0500990 struct uhci_td *td = NULL; /* Since urb->number_of_packets > 0 */
Alan Stern0ed8fee2005-12-17 18:02:38 -0500991 int i, frame;
Alan Sterndccf4a42005-12-17 17:58:46 -0500992 unsigned long destination, status;
Alan Sternb81d3432005-10-13 17:00:24 -0400993 struct urb_priv *urbp = (struct urb_priv *) urb->hcpriv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994
Alan Sterncaf38272006-05-19 16:44:55 -0400995 /* Values must not be too big (could overflow below) */
996 if (urb->interval >= UHCI_NUMFRAMES ||
997 urb->number_of_packets >= UHCI_NUMFRAMES)
998 return -EFBIG;
999
1000 /* Check the period and figure out the starting frame number */
Alan Sterncaf38272006-05-19 16:44:55 -04001001 if (qh->period == 0) {
1002 if (urb->transfer_flags & URB_ISO_ASAP) {
Alan Sternc8155cc2006-05-19 16:52:35 -04001003 uhci_get_current_frame_number(uhci);
Alan Sterncaf38272006-05-19 16:44:55 -04001004 urb->start_frame = uhci->frame_number + 10;
1005 } else {
Alan Sternc8155cc2006-05-19 16:52:35 -04001006 i = urb->start_frame - uhci->last_iso_frame;
Alan Sterncaf38272006-05-19 16:44:55 -04001007 if (i <= 0 || i >= UHCI_NUMFRAMES)
1008 return -EINVAL;
1009 }
1010 } else if (qh->period != urb->interval) {
1011 return -EINVAL; /* Can't change the period */
1012
1013 } else { /* Pick up where the last URB leaves off */
1014 if (list_empty(&qh->queue)) {
Alan Sternc8155cc2006-05-19 16:52:35 -04001015 frame = qh->iso_frame;
Alan Sterncaf38272006-05-19 16:44:55 -04001016 } else {
1017 struct urb *lurb;
1018
1019 lurb = list_entry(qh->queue.prev,
1020 struct urb_priv, node)->urb;
1021 frame = lurb->start_frame +
1022 lurb->number_of_packets *
1023 lurb->interval;
1024 }
1025 if (urb->transfer_flags & URB_ISO_ASAP)
1026 urb->start_frame = frame;
Alan Sternc8155cc2006-05-19 16:52:35 -04001027 else if (urb->start_frame != frame)
1028 return -EINVAL;
Alan Sterncaf38272006-05-19 16:44:55 -04001029 }
1030
1031 /* Make sure we won't have to go too far into the future */
Alan Sternc8155cc2006-05-19 16:52:35 -04001032 if (uhci_frame_before_eq(uhci->last_iso_frame + UHCI_NUMFRAMES,
Alan Sterncaf38272006-05-19 16:44:55 -04001033 urb->start_frame + urb->number_of_packets *
1034 urb->interval))
Alan Stern0ed8fee2005-12-17 18:02:38 -05001035 return -EFBIG;
1036
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037 status = TD_CTRL_ACTIVE | TD_CTRL_IOS;
1038 destination = (urb->pipe & PIPE_DEVEP_MASK) | usb_packetid(urb->pipe);
1039
Alan Sternb81d3432005-10-13 17:00:24 -04001040 for (i = 0; i < urb->number_of_packets; i++) {
Alan Stern25321782005-04-25 11:14:31 -04001041 td = uhci_alloc_td(uhci);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042 if (!td)
1043 return -ENOMEM;
1044
Alan Stern04538a22006-05-12 11:29:04 -04001045 uhci_add_td_to_urbp(td, urbp);
Alan Sterndccf4a42005-12-17 17:58:46 -05001046 uhci_fill_td(td, status, destination |
1047 uhci_explen(urb->iso_frame_desc[i].length),
1048 urb->transfer_dma +
1049 urb->iso_frame_desc[i].offset);
Alan Sternb81d3432005-10-13 17:00:24 -04001050 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051
Alan Sterndccf4a42005-12-17 17:58:46 -05001052 /* Set the interrupt-on-completion flag on the last packet. */
1053 td->status |= __constant_cpu_to_le32(TD_CTRL_IOC);
1054
1055 qh->skel = uhci->skel_iso_qh;
Alan Sterncaf38272006-05-19 16:44:55 -04001056 qh->period = urb->interval;
Alan Sterndccf4a42005-12-17 17:58:46 -05001057
1058 /* Add the TDs to the frame list */
Alan Sternb81d3432005-10-13 17:00:24 -04001059 frame = urb->start_frame;
1060 list_for_each_entry(td, &urbp->td_list, list) {
Alan Sterndccf4a42005-12-17 17:58:46 -05001061 uhci_insert_td_in_frame_list(uhci, td, frame);
Alan Sternc8155cc2006-05-19 16:52:35 -04001062 frame += qh->period;
1063 }
1064
1065 if (list_empty(&qh->queue)) {
1066 qh->iso_packet_desc = &urb->iso_frame_desc[0];
1067 qh->iso_frame = urb->start_frame;
1068 qh->iso_status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 }
1070
Alan Sterndccf4a42005-12-17 17:58:46 -05001071 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072}
1073
1074static int uhci_result_isochronous(struct uhci_hcd *uhci, struct urb *urb)
1075{
Alan Sternc8155cc2006-05-19 16:52:35 -04001076 struct uhci_td *td, *tmp;
1077 struct urb_priv *urbp = urb->hcpriv;
1078 struct uhci_qh *qh = urbp->qh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079
Alan Sternc8155cc2006-05-19 16:52:35 -04001080 list_for_each_entry_safe(td, tmp, &urbp->td_list, list) {
1081 unsigned int ctrlstat;
1082 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083 int actlength;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084
Alan Sternc8155cc2006-05-19 16:52:35 -04001085 if (uhci_frame_before_eq(uhci->cur_iso_frame, qh->iso_frame))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086 return -EINPROGRESS;
1087
Alan Sternc8155cc2006-05-19 16:52:35 -04001088 uhci_remove_tds_from_frame(uhci, qh->iso_frame);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089
Alan Sternc8155cc2006-05-19 16:52:35 -04001090 ctrlstat = td_status(td);
1091 if (ctrlstat & TD_CTRL_ACTIVE) {
1092 status = -EXDEV; /* TD was added too late? */
1093 } else {
1094 status = uhci_map_status(uhci_status_bits(ctrlstat),
1095 usb_pipeout(urb->pipe));
1096 actlength = uhci_actual_length(ctrlstat);
1097
1098 urb->actual_length += actlength;
1099 qh->iso_packet_desc->actual_length = actlength;
1100 qh->iso_packet_desc->status = status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101 }
1102
Alan Sternc8155cc2006-05-19 16:52:35 -04001103 if (status) {
1104 urb->error_count++;
1105 qh->iso_status = status;
1106 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107
Alan Sternc8155cc2006-05-19 16:52:35 -04001108 uhci_remove_td_from_urbp(td);
1109 uhci_free_td(uhci, td);
1110 qh->iso_frame += qh->period;
1111 ++qh->iso_packet_desc;
1112 }
1113 return qh->iso_status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114}
1115
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116static int uhci_urb_enqueue(struct usb_hcd *hcd,
Alan Sterndccf4a42005-12-17 17:58:46 -05001117 struct usb_host_endpoint *hep,
Al Viro55016f12005-10-21 03:21:58 -04001118 struct urb *urb, gfp_t mem_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119{
1120 int ret;
1121 struct uhci_hcd *uhci = hcd_to_uhci(hcd);
1122 unsigned long flags;
Alan Sterndccf4a42005-12-17 17:58:46 -05001123 struct urb_priv *urbp;
1124 struct uhci_qh *qh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125 int bustime;
1126
1127 spin_lock_irqsave(&uhci->lock, flags);
1128
1129 ret = urb->status;
1130 if (ret != -EINPROGRESS) /* URB already unlinked! */
Alan Sterndccf4a42005-12-17 17:58:46 -05001131 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132
Alan Sterndccf4a42005-12-17 17:58:46 -05001133 ret = -ENOMEM;
1134 urbp = uhci_alloc_urb_priv(uhci, urb);
1135 if (!urbp)
1136 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137
Alan Sterndccf4a42005-12-17 17:58:46 -05001138 if (hep->hcpriv)
1139 qh = (struct uhci_qh *) hep->hcpriv;
1140 else {
1141 qh = uhci_alloc_qh(uhci, urb->dev, hep);
1142 if (!qh)
1143 goto err_no_qh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144 }
Alan Sterndccf4a42005-12-17 17:58:46 -05001145 urbp->qh = qh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146
Alan Stern4de7d2c2006-05-05 16:26:58 -04001147 switch (qh->type) {
1148 case USB_ENDPOINT_XFER_CONTROL:
Alan Sterndccf4a42005-12-17 17:58:46 -05001149 ret = uhci_submit_control(uhci, urb, qh);
1150 break;
Alan Stern4de7d2c2006-05-05 16:26:58 -04001151 case USB_ENDPOINT_XFER_BULK:
Alan Sterndccf4a42005-12-17 17:58:46 -05001152 ret = uhci_submit_bulk(uhci, urb, qh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153 break;
Alan Stern4de7d2c2006-05-05 16:26:58 -04001154 case USB_ENDPOINT_XFER_INT:
Alan Sterndccf4a42005-12-17 17:58:46 -05001155 if (list_empty(&qh->queue)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156 bustime = usb_check_bandwidth(urb->dev, urb);
1157 if (bustime < 0)
1158 ret = bustime;
1159 else {
Alan Sterndccf4a42005-12-17 17:58:46 -05001160 ret = uhci_submit_interrupt(uhci, urb, qh);
1161 if (ret == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162 usb_claim_bandwidth(urb->dev, urb, bustime, 0);
1163 }
1164 } else { /* inherit from parent */
Alan Sterndccf4a42005-12-17 17:58:46 -05001165 struct urb_priv *eurbp;
1166
1167 eurbp = list_entry(qh->queue.prev, struct urb_priv,
1168 node);
1169 urb->bandwidth = eurbp->urb->bandwidth;
1170 ret = uhci_submit_interrupt(uhci, urb, qh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171 }
1172 break;
Alan Stern4de7d2c2006-05-05 16:26:58 -04001173 case USB_ENDPOINT_XFER_ISOC:
Alan Sternc8155cc2006-05-19 16:52:35 -04001174 urb->error_count = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175 bustime = usb_check_bandwidth(urb->dev, urb);
1176 if (bustime < 0) {
1177 ret = bustime;
1178 break;
1179 }
1180
Alan Sterndccf4a42005-12-17 17:58:46 -05001181 ret = uhci_submit_isochronous(uhci, urb, qh);
1182 if (ret == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183 usb_claim_bandwidth(urb->dev, urb, bustime, 1);
1184 break;
1185 }
Alan Sterndccf4a42005-12-17 17:58:46 -05001186 if (ret != 0)
1187 goto err_submit_failed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188
Alan Sterndccf4a42005-12-17 17:58:46 -05001189 /* Add this URB to the QH */
1190 urbp->qh = qh;
1191 list_add_tail(&urbp->node, &qh->queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192
Alan Sterndccf4a42005-12-17 17:58:46 -05001193 /* If the new URB is the first and only one on this QH then either
1194 * the QH is new and idle or else it's unlinked and waiting to
Alan Stern27755622006-05-05 16:32:02 -04001195 * become idle, so we can activate it right away. But only if the
1196 * queue isn't stopped. */
Alan Stern84afddd2006-05-12 11:35:45 -04001197 if (qh->queue.next == &urbp->node && !qh->is_stopped) {
Alan Sterndccf4a42005-12-17 17:58:46 -05001198 uhci_activate_qh(uhci, qh);
Alan Sternc5e3b742006-06-05 12:28:57 -04001199 uhci_urbp_wants_fsbr(uhci, urbp);
Alan Stern84afddd2006-05-12 11:35:45 -04001200 }
Alan Sterndccf4a42005-12-17 17:58:46 -05001201 goto done;
1202
1203err_submit_failed:
1204 if (qh->state == QH_STATE_IDLE)
1205 uhci_make_qh_idle(uhci, qh); /* Reclaim unused QH */
1206
1207err_no_qh:
1208 uhci_free_urb_priv(uhci, urbp);
1209
1210done:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211 spin_unlock_irqrestore(&uhci->lock, flags);
1212 return ret;
1213}
1214
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215static int uhci_urb_dequeue(struct usb_hcd *hcd, struct urb *urb)
1216{
1217 struct uhci_hcd *uhci = hcd_to_uhci(hcd);
1218 unsigned long flags;
1219 struct urb_priv *urbp;
Alan Stern10b8e472006-05-19 16:39:52 -04001220 struct uhci_qh *qh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221
1222 spin_lock_irqsave(&uhci->lock, flags);
1223 urbp = urb->hcpriv;
1224 if (!urbp) /* URB was never linked! */
1225 goto done;
Alan Stern10b8e472006-05-19 16:39:52 -04001226 qh = urbp->qh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227
Alan Sterndccf4a42005-12-17 17:58:46 -05001228 /* Remove Isochronous TDs from the frame list ASAP */
Alan Stern10b8e472006-05-19 16:39:52 -04001229 if (qh->type == USB_ENDPOINT_XFER_ISOC) {
Alan Sterndccf4a42005-12-17 17:58:46 -05001230 uhci_unlink_isochronous_tds(uhci, urb);
Alan Stern10b8e472006-05-19 16:39:52 -04001231 mb();
1232
1233 /* If the URB has already started, update the QH unlink time */
1234 uhci_get_current_frame_number(uhci);
1235 if (uhci_frame_before_eq(urb->start_frame, uhci->frame_number))
1236 qh->unlink_frame = uhci->frame_number;
1237 }
1238
1239 uhci_unlink_qh(uhci, qh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240
1241done:
1242 spin_unlock_irqrestore(&uhci->lock, flags);
1243 return 0;
1244}
1245
Alan Stern0ed8fee2005-12-17 18:02:38 -05001246/*
1247 * Finish unlinking an URB and give it back
1248 */
1249static void uhci_giveback_urb(struct uhci_hcd *uhci, struct uhci_qh *qh,
David Howells7d12e782006-10-05 14:55:46 +01001250 struct urb *urb)
Alan Stern0ed8fee2005-12-17 18:02:38 -05001251__releases(uhci->lock)
1252__acquires(uhci->lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253{
Alan Stern0ed8fee2005-12-17 18:02:38 -05001254 struct urb_priv *urbp = (struct urb_priv *) urb->hcpriv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255
Alan Sternc8155cc2006-05-19 16:52:35 -04001256 /* When giving back the first URB in an Isochronous queue,
1257 * reinitialize the QH's iso-related members for the next URB. */
1258 if (qh->type == USB_ENDPOINT_XFER_ISOC &&
1259 urbp->node.prev == &qh->queue &&
1260 urbp->node.next != &qh->queue) {
1261 struct urb *nurb = list_entry(urbp->node.next,
1262 struct urb_priv, node)->urb;
1263
1264 qh->iso_packet_desc = &nurb->iso_frame_desc[0];
1265 qh->iso_frame = nurb->start_frame;
1266 qh->iso_status = 0;
1267 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268
Alan Stern0ed8fee2005-12-17 18:02:38 -05001269 /* Take the URB off the QH's queue. If the queue is now empty,
1270 * this is a perfect time for a toggle fixup. */
1271 list_del_init(&urbp->node);
1272 if (list_empty(&qh->queue) && qh->needs_fixup) {
1273 usb_settoggle(urb->dev, usb_pipeendpoint(urb->pipe),
1274 usb_pipeout(urb->pipe), qh->initial_toggle);
1275 qh->needs_fixup = 0;
1276 }
1277
Alan Stern0ed8fee2005-12-17 18:02:38 -05001278 uhci_free_urb_priv(uhci, urbp);
1279
Alan Stern4de7d2c2006-05-05 16:26:58 -04001280 switch (qh->type) {
1281 case USB_ENDPOINT_XFER_ISOC:
Alan Stern0ed8fee2005-12-17 18:02:38 -05001282 /* Release bandwidth for Interrupt or Isoc. transfers */
1283 if (urb->bandwidth)
1284 usb_release_bandwidth(urb->dev, urb, 1);
1285 break;
Alan Stern4de7d2c2006-05-05 16:26:58 -04001286 case USB_ENDPOINT_XFER_INT:
Alan Stern0ed8fee2005-12-17 18:02:38 -05001287 /* Release bandwidth for Interrupt or Isoc. transfers */
1288 /* Make sure we don't release if we have a queued URB */
1289 if (list_empty(&qh->queue) && urb->bandwidth)
1290 usb_release_bandwidth(urb->dev, urb, 0);
1291 else
1292 /* bandwidth was passed on to queued URB, */
1293 /* so don't let usb_unlink_urb() release it */
1294 urb->bandwidth = 0;
1295 break;
1296 }
1297
1298 spin_unlock(&uhci->lock);
David Howells7d12e782006-10-05 14:55:46 +01001299 usb_hcd_giveback_urb(uhci_to_hcd(uhci), urb);
Alan Stern0ed8fee2005-12-17 18:02:38 -05001300 spin_lock(&uhci->lock);
1301
1302 /* If the queue is now empty, we can unlink the QH and give up its
1303 * reserved bandwidth. */
1304 if (list_empty(&qh->queue)) {
1305 uhci_unlink_qh(uhci, qh);
1306
1307 /* Bandwidth stuff not yet implemented */
Alan Sterncaf38272006-05-19 16:44:55 -04001308 qh->period = 0;
Alan Stern0ed8fee2005-12-17 18:02:38 -05001309 }
1310}
1311
1312/*
1313 * Scan the URBs in a QH's queue
1314 */
1315#define QH_FINISHED_UNLINKING(qh) \
1316 (qh->state == QH_STATE_UNLINKING && \
1317 uhci->frame_number + uhci->is_stopped != qh->unlink_frame)
1318
David Howells7d12e782006-10-05 14:55:46 +01001319static void uhci_scan_qh(struct uhci_hcd *uhci, struct uhci_qh *qh)
Alan Stern0ed8fee2005-12-17 18:02:38 -05001320{
1321 struct urb_priv *urbp;
1322 struct urb *urb;
1323 int status;
1324
1325 while (!list_empty(&qh->queue)) {
1326 urbp = list_entry(qh->queue.next, struct urb_priv, node);
1327 urb = urbp->urb;
1328
Alan Sternb1869002006-05-12 11:14:25 -04001329 if (qh->type == USB_ENDPOINT_XFER_ISOC)
Alan Stern0ed8fee2005-12-17 18:02:38 -05001330 status = uhci_result_isochronous(uhci, urb);
Alan Sternb1869002006-05-12 11:14:25 -04001331 else
Alan Stern0ed8fee2005-12-17 18:02:38 -05001332 status = uhci_result_common(uhci, urb);
Alan Stern0ed8fee2005-12-17 18:02:38 -05001333 if (status == -EINPROGRESS)
1334 break;
1335
1336 spin_lock(&urb->lock);
1337 if (urb->status == -EINPROGRESS) /* Not dequeued */
1338 urb->status = status;
1339 else
Alan Stern27755622006-05-05 16:32:02 -04001340 status = ECONNRESET; /* Not -ECONNRESET */
Alan Stern0ed8fee2005-12-17 18:02:38 -05001341 spin_unlock(&urb->lock);
1342
1343 /* Dequeued but completed URBs can't be given back unless
1344 * the QH is stopped or has finished unlinking. */
Alan Stern27755622006-05-05 16:32:02 -04001345 if (status == ECONNRESET) {
1346 if (QH_FINISHED_UNLINKING(qh))
1347 qh->is_stopped = 1;
1348 else if (!qh->is_stopped)
1349 return;
1350 }
Alan Stern0ed8fee2005-12-17 18:02:38 -05001351
David Howells7d12e782006-10-05 14:55:46 +01001352 uhci_giveback_urb(uhci, qh, urb);
Alan Stern7ceb9322006-08-21 11:58:50 -04001353 if (status < 0 && qh->type != USB_ENDPOINT_XFER_ISOC)
Alan Stern0ed8fee2005-12-17 18:02:38 -05001354 break;
1355 }
1356
1357 /* If the QH is neither stopped nor finished unlinking (normal case),
1358 * our work here is done. */
Alan Stern27755622006-05-05 16:32:02 -04001359 if (QH_FINISHED_UNLINKING(qh))
1360 qh->is_stopped = 1;
1361 else if (!qh->is_stopped)
Alan Stern0ed8fee2005-12-17 18:02:38 -05001362 return;
1363
1364 /* Otherwise give back each of the dequeued URBs */
Alan Stern27755622006-05-05 16:32:02 -04001365restart:
Alan Stern0ed8fee2005-12-17 18:02:38 -05001366 list_for_each_entry(urbp, &qh->queue, node) {
1367 urb = urbp->urb;
1368 if (urb->status != -EINPROGRESS) {
Alan Stern10b8e472006-05-19 16:39:52 -04001369
1370 /* Fix up the TD links and save the toggles for
1371 * non-Isochronous queues. For Isochronous queues,
1372 * test for too-recent dequeues. */
1373 if (!uhci_cleanup_queue(uhci, qh, urb)) {
1374 qh->is_stopped = 0;
1375 return;
1376 }
David Howells7d12e782006-10-05 14:55:46 +01001377 uhci_giveback_urb(uhci, qh, urb);
Alan Stern0ed8fee2005-12-17 18:02:38 -05001378 goto restart;
1379 }
1380 }
1381 qh->is_stopped = 0;
1382
1383 /* There are no more dequeued URBs. If there are still URBs on the
1384 * queue, the QH can now be re-activated. */
1385 if (!list_empty(&qh->queue)) {
1386 if (qh->needs_fixup)
1387 uhci_fixup_toggles(qh, 0);
Alan Stern84afddd2006-05-12 11:35:45 -04001388
1389 /* If the first URB on the queue wants FSBR but its time
1390 * limit has expired, set the next TD to interrupt on
1391 * completion before reactivating the QH. */
1392 urbp = list_entry(qh->queue.next, struct urb_priv, node);
1393 if (urbp->fsbr && qh->wait_expired) {
1394 struct uhci_td *td = list_entry(urbp->td_list.next,
1395 struct uhci_td, list);
1396
1397 td->status |= __cpu_to_le32(TD_CTRL_IOC);
1398 }
1399
Alan Stern0ed8fee2005-12-17 18:02:38 -05001400 uhci_activate_qh(uhci, qh);
1401 }
1402
1403 /* The queue is empty. The QH can become idle if it is fully
1404 * unlinked. */
1405 else if (QH_FINISHED_UNLINKING(qh))
1406 uhci_make_qh_idle(uhci, qh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407}
1408
Alan Stern0ed8fee2005-12-17 18:02:38 -05001409/*
Alan Stern84afddd2006-05-12 11:35:45 -04001410 * Check for queues that have made some forward progress.
1411 * Returns 0 if the queue is not Isochronous, is ACTIVE, and
1412 * has not advanced since last examined; 1 otherwise.
Alan Sternb761d9d2006-05-12 11:41:59 -04001413 *
1414 * Early Intel controllers have a bug which causes qh->element sometimes
1415 * not to advance when a TD completes successfully. The queue remains
1416 * stuck on the inactive completed TD. We detect such cases and advance
1417 * the element pointer by hand.
Alan Stern84afddd2006-05-12 11:35:45 -04001418 */
1419static int uhci_advance_check(struct uhci_hcd *uhci, struct uhci_qh *qh)
1420{
1421 struct urb_priv *urbp = NULL;
1422 struct uhci_td *td;
1423 int ret = 1;
1424 unsigned status;
1425
1426 if (qh->type == USB_ENDPOINT_XFER_ISOC)
Alan Sternc5e3b742006-06-05 12:28:57 -04001427 goto done;
Alan Stern84afddd2006-05-12 11:35:45 -04001428
1429 /* Treat an UNLINKING queue as though it hasn't advanced.
1430 * This is okay because reactivation will treat it as though
1431 * it has advanced, and if it is going to become IDLE then
1432 * this doesn't matter anyway. Furthermore it's possible
1433 * for an UNLINKING queue not to have any URBs at all, or
1434 * for its first URB not to have any TDs (if it was dequeued
1435 * just as it completed). So it's not easy in any case to
1436 * test whether such queues have advanced. */
1437 if (qh->state != QH_STATE_ACTIVE) {
1438 urbp = NULL;
1439 status = 0;
1440
1441 } else {
1442 urbp = list_entry(qh->queue.next, struct urb_priv, node);
1443 td = list_entry(urbp->td_list.next, struct uhci_td, list);
1444 status = td_status(td);
1445 if (!(status & TD_CTRL_ACTIVE)) {
1446
1447 /* We're okay, the queue has advanced */
1448 qh->wait_expired = 0;
1449 qh->advance_jiffies = jiffies;
Alan Sternc5e3b742006-06-05 12:28:57 -04001450 goto done;
Alan Stern84afddd2006-05-12 11:35:45 -04001451 }
1452 ret = 0;
1453 }
1454
1455 /* The queue hasn't advanced; check for timeout */
Alan Sternc5e3b742006-06-05 12:28:57 -04001456 if (qh->wait_expired)
1457 goto done;
1458
1459 if (time_after(jiffies, qh->advance_jiffies + QH_WAIT_TIMEOUT)) {
Alan Sternb761d9d2006-05-12 11:41:59 -04001460
1461 /* Detect the Intel bug and work around it */
1462 if (qh->post_td && qh_element(qh) ==
1463 cpu_to_le32(qh->post_td->dma_handle)) {
1464 qh->element = qh->post_td->link;
1465 qh->advance_jiffies = jiffies;
Alan Sternc5e3b742006-06-05 12:28:57 -04001466 ret = 1;
1467 goto done;
Alan Sternb761d9d2006-05-12 11:41:59 -04001468 }
1469
Alan Stern84afddd2006-05-12 11:35:45 -04001470 qh->wait_expired = 1;
1471
1472 /* If the current URB wants FSBR, unlink it temporarily
1473 * so that we can safely set the next TD to interrupt on
1474 * completion. That way we'll know as soon as the queue
1475 * starts moving again. */
1476 if (urbp && urbp->fsbr && !(status & TD_CTRL_IOC))
1477 uhci_unlink_qh(uhci, qh);
Alan Sternc5e3b742006-06-05 12:28:57 -04001478
1479 } else {
1480 /* Unmoving but not-yet-expired queues keep FSBR alive */
1481 if (urbp)
1482 uhci_urbp_wants_fsbr(uhci, urbp);
Alan Stern84afddd2006-05-12 11:35:45 -04001483 }
Alan Sternc5e3b742006-06-05 12:28:57 -04001484
1485done:
Alan Stern84afddd2006-05-12 11:35:45 -04001486 return ret;
1487}
1488
1489/*
Alan Stern0ed8fee2005-12-17 18:02:38 -05001490 * Process events in the schedule, but only in one thread at a time
1491 */
David Howells7d12e782006-10-05 14:55:46 +01001492static void uhci_scan_schedule(struct uhci_hcd *uhci)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493{
Alan Stern0ed8fee2005-12-17 18:02:38 -05001494 int i;
1495 struct uhci_qh *qh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496
1497 /* Don't allow re-entrant calls */
1498 if (uhci->scan_in_progress) {
1499 uhci->need_rescan = 1;
1500 return;
1501 }
1502 uhci->scan_in_progress = 1;
Alan Stern84afddd2006-05-12 11:35:45 -04001503rescan:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504 uhci->need_rescan = 0;
Alan Sternc5e3b742006-06-05 12:28:57 -04001505 uhci->fsbr_is_wanted = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506
Alan Stern6c1b4452005-04-21 16:04:58 -04001507 uhci_clear_next_interrupt(uhci);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508 uhci_get_current_frame_number(uhci);
Alan Sternc8155cc2006-05-19 16:52:35 -04001509 uhci->cur_iso_frame = uhci->frame_number;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510
Alan Stern0ed8fee2005-12-17 18:02:38 -05001511 /* Go through all the QH queues and process the URBs in each one */
1512 for (i = 0; i < UHCI_NUM_SKELQH - 1; ++i) {
1513 uhci->next_qh = list_entry(uhci->skelqh[i]->node.next,
1514 struct uhci_qh, node);
1515 while ((qh = uhci->next_qh) != uhci->skelqh[i]) {
1516 uhci->next_qh = list_entry(qh->node.next,
1517 struct uhci_qh, node);
Alan Stern84afddd2006-05-12 11:35:45 -04001518
1519 if (uhci_advance_check(uhci, qh)) {
David Howells7d12e782006-10-05 14:55:46 +01001520 uhci_scan_qh(uhci, qh);
Alan Sternc5e3b742006-06-05 12:28:57 -04001521 if (qh->state == QH_STATE_ACTIVE) {
1522 uhci_urbp_wants_fsbr(uhci,
1523 list_entry(qh->queue.next, struct urb_priv, node));
1524 }
Alan Stern84afddd2006-05-12 11:35:45 -04001525 }
Alan Stern0ed8fee2005-12-17 18:02:38 -05001526 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528
Alan Sternc8155cc2006-05-19 16:52:35 -04001529 uhci->last_iso_frame = uhci->cur_iso_frame;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530 if (uhci->need_rescan)
1531 goto rescan;
1532 uhci->scan_in_progress = 0;
1533
Alan Sternc5e3b742006-06-05 12:28:57 -04001534 if (uhci->fsbr_is_on && !uhci->fsbr_is_wanted &&
1535 !uhci->fsbr_expiring) {
1536 uhci->fsbr_expiring = 1;
1537 mod_timer(&uhci->fsbr_timer, jiffies + FSBR_OFF_DELAY);
1538 }
Alan Stern84afddd2006-05-12 11:35:45 -04001539
Alan Stern04538a22006-05-12 11:29:04 -04001540 if (list_empty(&uhci->skel_unlink_qh->node))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541 uhci_clear_next_interrupt(uhci);
1542 else
1543 uhci_set_next_interrupt(uhci);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544}