blob: a0c6bf6128a32840ee21ef16ebd13fe483eccbfc [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;
Alan Stern28b93252007-02-19 15:51:51 -050049 uhci->skel_term_qh->link = LINK_TO_QH(uhci->skel_fs_control_qh);
Alan Stern84afddd2006-05-12 11:35:45 -040050}
51
52static void uhci_fsbr_off(struct uhci_hcd *uhci)
53{
54 uhci->fsbr_is_on = 0;
55 uhci->skel_term_qh->link = UHCI_PTR_TERM;
56}
57
58static void uhci_add_fsbr(struct uhci_hcd *uhci, struct urb *urb)
59{
60 struct urb_priv *urbp = urb->hcpriv;
61
62 if (!(urb->transfer_flags & URB_NO_FSBR))
63 urbp->fsbr = 1;
64}
65
Alan Sternc5e3b742006-06-05 12:28:57 -040066static void uhci_urbp_wants_fsbr(struct uhci_hcd *uhci, struct urb_priv *urbp)
Alan Stern84afddd2006-05-12 11:35:45 -040067{
Alan Stern84afddd2006-05-12 11:35:45 -040068 if (urbp->fsbr) {
Alan Sternc5e3b742006-06-05 12:28:57 -040069 uhci->fsbr_is_wanted = 1;
Alan Stern84afddd2006-05-12 11:35:45 -040070 if (!uhci->fsbr_is_on)
71 uhci_fsbr_on(uhci);
Alan Sternc5e3b742006-06-05 12:28:57 -040072 else if (uhci->fsbr_expiring) {
73 uhci->fsbr_expiring = 0;
74 del_timer(&uhci->fsbr_timer);
75 }
Alan Stern84afddd2006-05-12 11:35:45 -040076 }
77}
78
Alan Sternc5e3b742006-06-05 12:28:57 -040079static void uhci_fsbr_timeout(unsigned long _uhci)
80{
81 struct uhci_hcd *uhci = (struct uhci_hcd *) _uhci;
82 unsigned long flags;
83
84 spin_lock_irqsave(&uhci->lock, flags);
85 if (uhci->fsbr_expiring) {
86 uhci->fsbr_expiring = 0;
87 uhci_fsbr_off(uhci);
88 }
89 spin_unlock_irqrestore(&uhci->lock, flags);
90}
91
Alan Stern84afddd2006-05-12 11:35:45 -040092
Alan Stern25321782005-04-25 11:14:31 -040093static struct uhci_td *uhci_alloc_td(struct uhci_hcd *uhci)
Linus Torvalds1da177e2005-04-16 15:20:36 -070094{
95 dma_addr_t dma_handle;
96 struct uhci_td *td;
97
98 td = dma_pool_alloc(uhci->td_pool, GFP_ATOMIC, &dma_handle);
99 if (!td)
100 return NULL;
101
102 td->dma_handle = dma_handle;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 td->frame = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104
105 INIT_LIST_HEAD(&td->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 INIT_LIST_HEAD(&td->fl_list);
107
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 return td;
109}
110
Alan Sterndccf4a42005-12-17 17:58:46 -0500111static void uhci_free_td(struct uhci_hcd *uhci, struct uhci_td *td)
112{
113 if (!list_empty(&td->list))
114 dev_warn(uhci_dev(uhci), "td %p still in list!\n", td);
Alan Sterndccf4a42005-12-17 17:58:46 -0500115 if (!list_empty(&td->fl_list))
116 dev_warn(uhci_dev(uhci), "td %p still in fl_list!\n", td);
117
118 dma_pool_free(uhci->td_pool, td, td->dma_handle);
119}
120
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121static inline void uhci_fill_td(struct uhci_td *td, u32 status,
122 u32 token, u32 buffer)
123{
124 td->status = cpu_to_le32(status);
125 td->token = cpu_to_le32(token);
126 td->buffer = cpu_to_le32(buffer);
127}
128
Alan Stern04538a22006-05-12 11:29:04 -0400129static void uhci_add_td_to_urbp(struct uhci_td *td, struct urb_priv *urbp)
130{
131 list_add_tail(&td->list, &urbp->td_list);
132}
133
134static void uhci_remove_td_from_urbp(struct uhci_td *td)
135{
136 list_del_init(&td->list);
137}
138
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139/*
Alan Stern687f5f32005-11-30 17:16:19 -0500140 * We insert Isochronous URBs directly into the frame list at the beginning
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 */
Alan Sterndccf4a42005-12-17 17:58:46 -0500142static inline void uhci_insert_td_in_frame_list(struct uhci_hcd *uhci,
143 struct uhci_td *td, unsigned framenum)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144{
145 framenum &= (UHCI_NUMFRAMES - 1);
146
147 td->frame = framenum;
148
149 /* Is there a TD already mapped there? */
Alan Sterna1d59ce2005-09-16 14:22:51 -0400150 if (uhci->frame_cpu[framenum]) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 struct uhci_td *ftd, *ltd;
152
Alan Sterna1d59ce2005-09-16 14:22:51 -0400153 ftd = uhci->frame_cpu[framenum];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 ltd = list_entry(ftd->fl_list.prev, struct uhci_td, fl_list);
155
156 list_add_tail(&td->fl_list, &ftd->fl_list);
157
158 td->link = ltd->link;
159 wmb();
Alan Stern28b93252007-02-19 15:51:51 -0500160 ltd->link = LINK_TO_TD(td);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 } else {
Alan Sterna1d59ce2005-09-16 14:22:51 -0400162 td->link = uhci->frame[framenum];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 wmb();
Alan Stern28b93252007-02-19 15:51:51 -0500164 uhci->frame[framenum] = LINK_TO_TD(td);
Alan Sterna1d59ce2005-09-16 14:22:51 -0400165 uhci->frame_cpu[framenum] = td;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 }
167}
168
Alan Sterndccf4a42005-12-17 17:58:46 -0500169static inline void uhci_remove_td_from_frame_list(struct uhci_hcd *uhci,
Alan Sternb81d3432005-10-13 17:00:24 -0400170 struct uhci_td *td)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171{
172 /* If it's not inserted, don't remove it */
Alan Sternb81d3432005-10-13 17:00:24 -0400173 if (td->frame == -1) {
174 WARN_ON(!list_empty(&td->fl_list));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 return;
Alan Sternb81d3432005-10-13 17:00:24 -0400176 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177
Alan Sternb81d3432005-10-13 17:00:24 -0400178 if (uhci->frame_cpu[td->frame] == td) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 if (list_empty(&td->fl_list)) {
Alan Sterna1d59ce2005-09-16 14:22:51 -0400180 uhci->frame[td->frame] = td->link;
181 uhci->frame_cpu[td->frame] = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 } else {
183 struct uhci_td *ntd;
184
185 ntd = list_entry(td->fl_list.next, struct uhci_td, fl_list);
Alan Stern28b93252007-02-19 15:51:51 -0500186 uhci->frame[td->frame] = LINK_TO_TD(ntd);
Alan Sterna1d59ce2005-09-16 14:22:51 -0400187 uhci->frame_cpu[td->frame] = ntd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 }
189 } else {
190 struct uhci_td *ptd;
191
192 ptd = list_entry(td->fl_list.prev, struct uhci_td, fl_list);
193 ptd->link = td->link;
194 }
195
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 list_del_init(&td->fl_list);
197 td->frame = -1;
198}
199
Alan Sternc8155cc2006-05-19 16:52:35 -0400200static inline void uhci_remove_tds_from_frame(struct uhci_hcd *uhci,
201 unsigned int framenum)
202{
203 struct uhci_td *ftd, *ltd;
204
205 framenum &= (UHCI_NUMFRAMES - 1);
206
207 ftd = uhci->frame_cpu[framenum];
208 if (ftd) {
209 ltd = list_entry(ftd->fl_list.prev, struct uhci_td, fl_list);
210 uhci->frame[framenum] = ltd->link;
211 uhci->frame_cpu[framenum] = NULL;
212
213 while (!list_empty(&ftd->fl_list))
214 list_del_init(ftd->fl_list.prev);
215 }
216}
217
Alan Sterndccf4a42005-12-17 17:58:46 -0500218/*
219 * Remove all the TDs for an Isochronous URB from the frame list
220 */
221static void uhci_unlink_isochronous_tds(struct uhci_hcd *uhci, struct urb *urb)
Alan Sternb81d3432005-10-13 17:00:24 -0400222{
223 struct urb_priv *urbp = (struct urb_priv *) urb->hcpriv;
224 struct uhci_td *td;
225
226 list_for_each_entry(td, &urbp->td_list, list)
Alan Sterndccf4a42005-12-17 17:58:46 -0500227 uhci_remove_td_from_frame_list(uhci, td);
Alan Sternb81d3432005-10-13 17:00:24 -0400228}
229
Alan Sterndccf4a42005-12-17 17:58:46 -0500230static struct uhci_qh *uhci_alloc_qh(struct uhci_hcd *uhci,
231 struct usb_device *udev, struct usb_host_endpoint *hep)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232{
233 dma_addr_t dma_handle;
234 struct uhci_qh *qh;
235
236 qh = dma_pool_alloc(uhci->qh_pool, GFP_ATOMIC, &dma_handle);
237 if (!qh)
238 return NULL;
239
Alan Stern59e29ed2006-05-12 11:19:19 -0400240 memset(qh, 0, sizeof(*qh));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 qh->dma_handle = dma_handle;
242
243 qh->element = UHCI_PTR_TERM;
244 qh->link = UHCI_PTR_TERM;
245
Alan Sterndccf4a42005-12-17 17:58:46 -0500246 INIT_LIST_HEAD(&qh->queue);
247 INIT_LIST_HEAD(&qh->node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248
Alan Sterndccf4a42005-12-17 17:58:46 -0500249 if (udev) { /* Normal QH */
Alan Stern85a975d2007-01-08 12:01:43 -0500250 qh->type = hep->desc.bmAttributes & USB_ENDPOINT_XFERTYPE_MASK;
251 if (qh->type != USB_ENDPOINT_XFER_ISOC) {
252 qh->dummy_td = uhci_alloc_td(uhci);
253 if (!qh->dummy_td) {
254 dma_pool_free(uhci->qh_pool, qh, dma_handle);
255 return NULL;
256 }
Alan Sternaf0bb592005-12-17 18:00:12 -0500257 }
Alan Sterndccf4a42005-12-17 17:58:46 -0500258 qh->state = QH_STATE_IDLE;
259 qh->hep = hep;
260 qh->udev = udev;
261 hep->hcpriv = qh;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262
Alan Stern3ca2a322007-01-16 11:56:32 -0500263 if (qh->type == USB_ENDPOINT_XFER_INT ||
264 qh->type == USB_ENDPOINT_XFER_ISOC)
265 qh->load = usb_calc_bus_time(udev->speed,
266 usb_endpoint_dir_in(&hep->desc),
267 qh->type == USB_ENDPOINT_XFER_ISOC,
268 le16_to_cpu(hep->desc.wMaxPacketSize))
269 / 1000 + 1;
270
Alan Sterndccf4a42005-12-17 17:58:46 -0500271 } else { /* Skeleton QH */
272 qh->state = QH_STATE_ACTIVE;
Alan Stern4de7d2c2006-05-05 16:26:58 -0400273 qh->type = -1;
Alan Sterndccf4a42005-12-17 17:58:46 -0500274 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 return qh;
276}
277
278static void uhci_free_qh(struct uhci_hcd *uhci, struct uhci_qh *qh)
279{
Alan Sterndccf4a42005-12-17 17:58:46 -0500280 WARN_ON(qh->state != QH_STATE_IDLE && qh->udev);
281 if (!list_empty(&qh->queue))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 dev_warn(uhci_dev(uhci), "qh %p list not empty!\n", qh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283
Alan Sterndccf4a42005-12-17 17:58:46 -0500284 list_del(&qh->node);
285 if (qh->udev) {
286 qh->hep->hcpriv = NULL;
Alan Stern85a975d2007-01-08 12:01:43 -0500287 if (qh->dummy_td)
288 uhci_free_td(uhci, qh->dummy_td);
Alan Sterndccf4a42005-12-17 17:58:46 -0500289 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 dma_pool_free(uhci->qh_pool, qh, qh->dma_handle);
291}
292
293/*
Alan Sterna0b458b2006-05-12 11:23:19 -0400294 * When a queue is stopped and a dequeued URB is given back, adjust
295 * the previous TD link (if the URB isn't first on the queue) or
296 * save its toggle value (if it is first and is currently executing).
Alan Stern10b8e472006-05-19 16:39:52 -0400297 *
298 * Returns 0 if the URB should not yet be given back, 1 otherwise.
Alan Stern0ed8fee2005-12-17 18:02:38 -0500299 */
Alan Stern10b8e472006-05-19 16:39:52 -0400300static int uhci_cleanup_queue(struct uhci_hcd *uhci, struct uhci_qh *qh,
Alan Sterna0b458b2006-05-12 11:23:19 -0400301 struct urb *urb)
Alan Stern0ed8fee2005-12-17 18:02:38 -0500302{
Alan Sterna0b458b2006-05-12 11:23:19 -0400303 struct urb_priv *urbp = urb->hcpriv;
Alan Stern0ed8fee2005-12-17 18:02:38 -0500304 struct uhci_td *td;
Alan Stern10b8e472006-05-19 16:39:52 -0400305 int ret = 1;
Alan Stern0ed8fee2005-12-17 18:02:38 -0500306
Alan Sterna0b458b2006-05-12 11:23:19 -0400307 /* Isochronous pipes don't use toggles and their TD link pointers
Alan Stern10b8e472006-05-19 16:39:52 -0400308 * get adjusted during uhci_urb_dequeue(). But since their queues
309 * cannot truly be stopped, we have to watch out for dequeues
310 * occurring after the nominal unlink frame. */
311 if (qh->type == USB_ENDPOINT_XFER_ISOC) {
312 ret = (uhci->frame_number + uhci->is_stopped !=
313 qh->unlink_frame);
Alan Sternc5e3b742006-06-05 12:28:57 -0400314 goto done;
Alan Stern10b8e472006-05-19 16:39:52 -0400315 }
Alan Sterna0b458b2006-05-12 11:23:19 -0400316
317 /* If the URB isn't first on its queue, adjust the link pointer
318 * of the last TD in the previous URB. The toggle doesn't need
319 * to be saved since this URB can't be executing yet. */
320 if (qh->queue.next != &urbp->node) {
321 struct urb_priv *purbp;
322 struct uhci_td *ptd;
323
324 purbp = list_entry(urbp->node.prev, struct urb_priv, node);
325 WARN_ON(list_empty(&purbp->td_list));
326 ptd = list_entry(purbp->td_list.prev, struct uhci_td,
327 list);
328 td = list_entry(urbp->td_list.prev, struct uhci_td,
329 list);
330 ptd->link = td->link;
Alan Sternc5e3b742006-06-05 12:28:57 -0400331 goto done;
Alan Sterna0b458b2006-05-12 11:23:19 -0400332 }
333
Alan Stern0ed8fee2005-12-17 18:02:38 -0500334 /* If the QH element pointer is UHCI_PTR_TERM then then currently
335 * executing URB has already been unlinked, so this one isn't it. */
Alan Sterna0b458b2006-05-12 11:23:19 -0400336 if (qh_element(qh) == UHCI_PTR_TERM)
Alan Sternc5e3b742006-06-05 12:28:57 -0400337 goto done;
Alan Stern0ed8fee2005-12-17 18:02:38 -0500338 qh->element = UHCI_PTR_TERM;
339
Alan Stern85a975d2007-01-08 12:01:43 -0500340 /* Control pipes don't have to worry about toggles */
Alan Sterna0b458b2006-05-12 11:23:19 -0400341 if (qh->type == USB_ENDPOINT_XFER_CONTROL)
Alan Sternc5e3b742006-06-05 12:28:57 -0400342 goto done;
Alan Stern0ed8fee2005-12-17 18:02:38 -0500343
Alan Sterna0b458b2006-05-12 11:23:19 -0400344 /* Save the next toggle value */
Alan Stern59e29ed2006-05-12 11:19:19 -0400345 WARN_ON(list_empty(&urbp->td_list));
346 td = list_entry(urbp->td_list.next, struct uhci_td, list);
347 qh->needs_fixup = 1;
348 qh->initial_toggle = uhci_toggle(td_token(td));
Alan Sternc5e3b742006-06-05 12:28:57 -0400349
350done:
Alan Stern10b8e472006-05-19 16:39:52 -0400351 return ret;
Alan Stern0ed8fee2005-12-17 18:02:38 -0500352}
353
354/*
355 * Fix up the data toggles for URBs in a queue, when one of them
356 * terminates early (short transfer, error, or dequeued).
357 */
358static void uhci_fixup_toggles(struct uhci_qh *qh, int skip_first)
359{
360 struct urb_priv *urbp = NULL;
361 struct uhci_td *td;
362 unsigned int toggle = qh->initial_toggle;
363 unsigned int pipe;
364
365 /* Fixups for a short transfer start with the second URB in the
366 * queue (the short URB is the first). */
367 if (skip_first)
368 urbp = list_entry(qh->queue.next, struct urb_priv, node);
369
370 /* When starting with the first URB, if the QH element pointer is
371 * still valid then we know the URB's toggles are okay. */
372 else if (qh_element(qh) != UHCI_PTR_TERM)
373 toggle = 2;
374
375 /* Fix up the toggle for the URBs in the queue. Normally this
376 * loop won't run more than once: When an error or short transfer
377 * occurs, the queue usually gets emptied. */
Alan Stern1393adb2006-01-31 10:02:55 -0500378 urbp = list_prepare_entry(urbp, &qh->queue, node);
Alan Stern0ed8fee2005-12-17 18:02:38 -0500379 list_for_each_entry_continue(urbp, &qh->queue, node) {
380
381 /* If the first TD has the right toggle value, we don't
382 * need to change any toggles in this URB */
383 td = list_entry(urbp->td_list.next, struct uhci_td, list);
384 if (toggle > 1 || uhci_toggle(td_token(td)) == toggle) {
Alan Sterndb59b462006-08-31 14:18:39 -0400385 td = list_entry(urbp->td_list.prev, struct uhci_td,
Alan Stern0ed8fee2005-12-17 18:02:38 -0500386 list);
387 toggle = uhci_toggle(td_token(td)) ^ 1;
388
389 /* Otherwise all the toggles in the URB have to be switched */
390 } else {
391 list_for_each_entry(td, &urbp->td_list, list) {
392 td->token ^= __constant_cpu_to_le32(
393 TD_TOKEN_TOGGLE);
394 toggle ^= 1;
395 }
396 }
397 }
398
399 wmb();
400 pipe = list_entry(qh->queue.next, struct urb_priv, node)->urb->pipe;
401 usb_settoggle(qh->udev, usb_pipeendpoint(pipe),
402 usb_pipeout(pipe), toggle);
403 qh->needs_fixup = 0;
404}
405
406/*
Alan Sterndccf4a42005-12-17 17:58:46 -0500407 * Put a QH on the schedule in both hardware and software
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 */
Alan Sterndccf4a42005-12-17 17:58:46 -0500409static void uhci_activate_qh(struct uhci_hcd *uhci, struct uhci_qh *qh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410{
Alan Sterndccf4a42005-12-17 17:58:46 -0500411 struct uhci_qh *pqh;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412
Alan Sterndccf4a42005-12-17 17:58:46 -0500413 WARN_ON(list_empty(&qh->queue));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414
Alan Sterndccf4a42005-12-17 17:58:46 -0500415 /* Set the element pointer if it isn't set already.
416 * This isn't needed for Isochronous queues, but it doesn't hurt. */
417 if (qh_element(qh) == UHCI_PTR_TERM) {
418 struct urb_priv *urbp = list_entry(qh->queue.next,
419 struct urb_priv, node);
420 struct uhci_td *td = list_entry(urbp->td_list.next,
421 struct uhci_td, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422
Alan Stern28b93252007-02-19 15:51:51 -0500423 qh->element = LINK_TO_TD(td);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 }
425
Alan Stern84afddd2006-05-12 11:35:45 -0400426 /* Treat the queue as if it has just advanced */
427 qh->wait_expired = 0;
428 qh->advance_jiffies = jiffies;
429
Alan Sterndccf4a42005-12-17 17:58:46 -0500430 if (qh->state == QH_STATE_ACTIVE)
431 return;
432 qh->state = QH_STATE_ACTIVE;
433
434 /* Move the QH from its old list to the end of the appropriate
435 * skeleton's list */
Alan Stern0ed8fee2005-12-17 18:02:38 -0500436 if (qh == uhci->next_qh)
437 uhci->next_qh = list_entry(qh->node.next, struct uhci_qh,
438 node);
Alan Sterndccf4a42005-12-17 17:58:46 -0500439 list_move_tail(&qh->node, &qh->skel->node);
440
441 /* Link it into the schedule */
442 pqh = list_entry(qh->node.prev, struct uhci_qh, node);
443 qh->link = pqh->link;
444 wmb();
Alan Stern28b93252007-02-19 15:51:51 -0500445 pqh->link = LINK_TO_QH(qh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446}
447
448/*
Alan Sterndccf4a42005-12-17 17:58:46 -0500449 * Take a QH off the hardware schedule
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 */
Alan Sterndccf4a42005-12-17 17:58:46 -0500451static void uhci_unlink_qh(struct uhci_hcd *uhci, struct uhci_qh *qh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452{
453 struct uhci_qh *pqh;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454
Alan Sterndccf4a42005-12-17 17:58:46 -0500455 if (qh->state == QH_STATE_UNLINKING)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 return;
Alan Sterndccf4a42005-12-17 17:58:46 -0500457 WARN_ON(qh->state != QH_STATE_ACTIVE || !qh->udev);
458 qh->state = QH_STATE_UNLINKING;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459
Alan Sterndccf4a42005-12-17 17:58:46 -0500460 /* Unlink the QH from the schedule and record when we did it */
461 pqh = list_entry(qh->node.prev, struct uhci_qh, node);
462 pqh->link = qh->link;
463 mb();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464
465 uhci_get_current_frame_number(uhci);
Alan Sterndccf4a42005-12-17 17:58:46 -0500466 qh->unlink_frame = uhci->frame_number;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467
Alan Sterndccf4a42005-12-17 17:58:46 -0500468 /* Force an interrupt so we know when the QH is fully unlinked */
469 if (list_empty(&uhci->skel_unlink_qh->node))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 uhci_set_next_interrupt(uhci);
471
Alan Sterndccf4a42005-12-17 17:58:46 -0500472 /* Move the QH from its old list to the end of the unlinking list */
Alan Stern0ed8fee2005-12-17 18:02:38 -0500473 if (qh == uhci->next_qh)
474 uhci->next_qh = list_entry(qh->node.next, struct uhci_qh,
475 node);
Alan Sterndccf4a42005-12-17 17:58:46 -0500476 list_move_tail(&qh->node, &uhci->skel_unlink_qh->node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477}
478
Alan Sterndccf4a42005-12-17 17:58:46 -0500479/*
480 * When we and the controller are through with a QH, it becomes IDLE.
481 * This happens when a QH has been off the schedule (on the unlinking
482 * list) for more than one frame, or when an error occurs while adding
483 * the first URB onto a new QH.
484 */
485static void uhci_make_qh_idle(struct uhci_hcd *uhci, struct uhci_qh *qh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486{
Alan Sterndccf4a42005-12-17 17:58:46 -0500487 WARN_ON(qh->state == QH_STATE_ACTIVE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488
Alan Stern0ed8fee2005-12-17 18:02:38 -0500489 if (qh == uhci->next_qh)
490 uhci->next_qh = list_entry(qh->node.next, struct uhci_qh,
491 node);
Alan Sterndccf4a42005-12-17 17:58:46 -0500492 list_move(&qh->node, &uhci->idle_qh_list);
493 qh->state = QH_STATE_IDLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494
Alan Stern59e29ed2006-05-12 11:19:19 -0400495 /* Now that the QH is idle, its post_td isn't being used */
496 if (qh->post_td) {
497 uhci_free_td(uhci, qh->post_td);
498 qh->post_td = NULL;
499 }
500
Alan Sterndccf4a42005-12-17 17:58:46 -0500501 /* If anyone is waiting for a QH to become idle, wake them up */
502 if (uhci->num_waiting)
503 wake_up_all(&uhci->waitqh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504}
505
Alan Stern3ca2a322007-01-16 11:56:32 -0500506/*
507 * Find the highest existing bandwidth load for a given phase and period.
508 */
509static int uhci_highest_load(struct uhci_hcd *uhci, int phase, int period)
510{
511 int highest_load = uhci->load[phase];
512
513 for (phase += period; phase < MAX_PHASE; phase += period)
514 highest_load = max_t(int, highest_load, uhci->load[phase]);
515 return highest_load;
516}
517
518/*
519 * Set qh->phase to the optimal phase for a periodic transfer and
520 * check whether the bandwidth requirement is acceptable.
521 */
522static int uhci_check_bandwidth(struct uhci_hcd *uhci, struct uhci_qh *qh)
523{
524 int minimax_load;
525
526 /* Find the optimal phase (unless it is already set) and get
527 * its load value. */
528 if (qh->phase >= 0)
529 minimax_load = uhci_highest_load(uhci, qh->phase, qh->period);
530 else {
531 int phase, load;
532 int max_phase = min_t(int, MAX_PHASE, qh->period);
533
534 qh->phase = 0;
535 minimax_load = uhci_highest_load(uhci, qh->phase, qh->period);
536 for (phase = 1; phase < max_phase; ++phase) {
537 load = uhci_highest_load(uhci, phase, qh->period);
538 if (load < minimax_load) {
539 minimax_load = load;
540 qh->phase = phase;
541 }
542 }
543 }
544
545 /* Maximum allowable periodic bandwidth is 90%, or 900 us per frame */
546 if (minimax_load + qh->load > 900) {
547 dev_dbg(uhci_dev(uhci), "bandwidth allocation failed: "
548 "period %d, phase %d, %d + %d us\n",
549 qh->period, qh->phase, minimax_load, qh->load);
550 return -ENOSPC;
551 }
552 return 0;
553}
554
555/*
556 * Reserve a periodic QH's bandwidth in the schedule
557 */
558static void uhci_reserve_bandwidth(struct uhci_hcd *uhci, struct uhci_qh *qh)
559{
560 int i;
561 int load = qh->load;
562 char *p = "??";
563
564 for (i = qh->phase; i < MAX_PHASE; i += qh->period) {
565 uhci->load[i] += load;
566 uhci->total_load += load;
567 }
568 uhci_to_hcd(uhci)->self.bandwidth_allocated =
569 uhci->total_load / MAX_PHASE;
570 switch (qh->type) {
571 case USB_ENDPOINT_XFER_INT:
572 ++uhci_to_hcd(uhci)->self.bandwidth_int_reqs;
573 p = "INT";
574 break;
575 case USB_ENDPOINT_XFER_ISOC:
576 ++uhci_to_hcd(uhci)->self.bandwidth_isoc_reqs;
577 p = "ISO";
578 break;
579 }
580 qh->bandwidth_reserved = 1;
581 dev_dbg(uhci_dev(uhci),
582 "%s dev %d ep%02x-%s, period %d, phase %d, %d us\n",
583 "reserve", qh->udev->devnum,
584 qh->hep->desc.bEndpointAddress, p,
585 qh->period, qh->phase, load);
586}
587
588/*
589 * Release a periodic QH's bandwidth reservation
590 */
591static void uhci_release_bandwidth(struct uhci_hcd *uhci, struct uhci_qh *qh)
592{
593 int i;
594 int load = qh->load;
595 char *p = "??";
596
597 for (i = qh->phase; i < MAX_PHASE; i += qh->period) {
598 uhci->load[i] -= load;
599 uhci->total_load -= load;
600 }
601 uhci_to_hcd(uhci)->self.bandwidth_allocated =
602 uhci->total_load / MAX_PHASE;
603 switch (qh->type) {
604 case USB_ENDPOINT_XFER_INT:
605 --uhci_to_hcd(uhci)->self.bandwidth_int_reqs;
606 p = "INT";
607 break;
608 case USB_ENDPOINT_XFER_ISOC:
609 --uhci_to_hcd(uhci)->self.bandwidth_isoc_reqs;
610 p = "ISO";
611 break;
612 }
613 qh->bandwidth_reserved = 0;
614 dev_dbg(uhci_dev(uhci),
615 "%s dev %d ep%02x-%s, period %d, phase %d, %d us\n",
616 "release", qh->udev->devnum,
617 qh->hep->desc.bEndpointAddress, p,
618 qh->period, qh->phase, load);
619}
620
Alan Sterndccf4a42005-12-17 17:58:46 -0500621static inline struct urb_priv *uhci_alloc_urb_priv(struct uhci_hcd *uhci,
622 struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623{
624 struct urb_priv *urbp;
625
Robert P. J. Dayc3762222007-02-10 01:45:03 -0800626 urbp = kmem_cache_zalloc(uhci_up_cachep, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 if (!urbp)
628 return NULL;
629
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 urbp->urb = urb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 urb->hcpriv = urbp;
Alan Sterndccf4a42005-12-17 17:58:46 -0500632
633 INIT_LIST_HEAD(&urbp->node);
634 INIT_LIST_HEAD(&urbp->td_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635
636 return urbp;
637}
638
Alan Sterndccf4a42005-12-17 17:58:46 -0500639static void uhci_free_urb_priv(struct uhci_hcd *uhci,
640 struct urb_priv *urbp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641{
642 struct uhci_td *td, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643
Alan Sterndccf4a42005-12-17 17:58:46 -0500644 if (!list_empty(&urbp->node))
645 dev_warn(uhci_dev(uhci), "urb %p still on QH's list!\n",
646 urbp->urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 list_for_each_entry_safe(td, tmp, &urbp->td_list, list) {
Alan Stern04538a22006-05-12 11:29:04 -0400649 uhci_remove_td_from_urbp(td);
650 uhci_free_td(uhci, td);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 }
652
Alan Sterndccf4a42005-12-17 17:58:46 -0500653 urbp->urb->hcpriv = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 kmem_cache_free(uhci_up_cachep, urbp);
655}
656
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657/*
658 * Map status to standard result codes
659 *
660 * <status> is (td_status(td) & 0xF60000), a.k.a.
661 * uhci_status_bits(td_status(td)).
662 * Note: <status> does not include the TD_CTRL_NAK bit.
663 * <dir_out> is True for output TDs and False for input TDs.
664 */
665static int uhci_map_status(int status, int dir_out)
666{
667 if (!status)
668 return 0;
669 if (status & TD_CTRL_BITSTUFF) /* Bitstuff error */
670 return -EPROTO;
671 if (status & TD_CTRL_CRCTIMEO) { /* CRC/Timeout */
672 if (dir_out)
673 return -EPROTO;
674 else
675 return -EILSEQ;
676 }
677 if (status & TD_CTRL_BABBLE) /* Babble */
678 return -EOVERFLOW;
679 if (status & TD_CTRL_DBUFERR) /* Buffer error */
680 return -ENOSR;
681 if (status & TD_CTRL_STALLED) /* Stalled */
682 return -EPIPE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 return 0;
684}
685
686/*
687 * Control transfers
688 */
Alan Sterndccf4a42005-12-17 17:58:46 -0500689static int uhci_submit_control(struct uhci_hcd *uhci, struct urb *urb,
690 struct uhci_qh *qh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 struct uhci_td *td;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 unsigned long destination, status;
Alan Sterndccf4a42005-12-17 17:58:46 -0500694 int maxsze = le16_to_cpu(qh->hep->desc.wMaxPacketSize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 int len = urb->transfer_buffer_length;
696 dma_addr_t data = urb->transfer_dma;
Alan Sterndccf4a42005-12-17 17:58:46 -0500697 __le32 *plink;
Alan Stern04538a22006-05-12 11:29:04 -0400698 struct urb_priv *urbp = urb->hcpriv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699
700 /* The "pipe" thing contains the destination in bits 8--18 */
701 destination = (urb->pipe & PIPE_DEVEP_MASK) | USB_PID_SETUP;
702
Alan Sternaf0bb592005-12-17 18:00:12 -0500703 /* 3 errors, dummy TD remains inactive */
704 status = uhci_maxerr(3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 if (urb->dev->speed == USB_SPEED_LOW)
706 status |= TD_CTRL_LS;
707
708 /*
709 * Build the TD for the control request setup packet
710 */
Alan Sternaf0bb592005-12-17 18:00:12 -0500711 td = qh->dummy_td;
Alan Stern04538a22006-05-12 11:29:04 -0400712 uhci_add_td_to_urbp(td, urbp);
Alan Sternfa346562005-11-30 11:57:51 -0500713 uhci_fill_td(td, status, destination | uhci_explen(8),
Alan Sterndccf4a42005-12-17 17:58:46 -0500714 urb->setup_dma);
715 plink = &td->link;
Alan Sternaf0bb592005-12-17 18:00:12 -0500716 status |= TD_CTRL_ACTIVE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717
718 /*
719 * If direction is "send", change the packet ID from SETUP (0x2D)
720 * to OUT (0xE1). Else change it from SETUP to IN (0x69) and
721 * set Short Packet Detect (SPD) for all data packets.
722 */
723 if (usb_pipeout(urb->pipe))
724 destination ^= (USB_PID_SETUP ^ USB_PID_OUT);
725 else {
726 destination ^= (USB_PID_SETUP ^ USB_PID_IN);
727 status |= TD_CTRL_SPD;
728 }
729
730 /*
Alan Stern687f5f32005-11-30 17:16:19 -0500731 * Build the DATA TDs
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 */
733 while (len > 0) {
Alan Sterndccf4a42005-12-17 17:58:46 -0500734 int pktsze = min(len, maxsze);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735
Alan Stern25321782005-04-25 11:14:31 -0400736 td = uhci_alloc_td(uhci);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 if (!td)
Alan Sternaf0bb592005-12-17 18:00:12 -0500738 goto nomem;
Alan Stern28b93252007-02-19 15:51:51 -0500739 *plink = LINK_TO_TD(td);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740
741 /* Alternate Data0/1 (start with Data1) */
742 destination ^= TD_TOKEN_TOGGLE;
743
Alan Stern04538a22006-05-12 11:29:04 -0400744 uhci_add_td_to_urbp(td, urbp);
Alan Sternfa346562005-11-30 11:57:51 -0500745 uhci_fill_td(td, status, destination | uhci_explen(pktsze),
Alan Sterndccf4a42005-12-17 17:58:46 -0500746 data);
747 plink = &td->link;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748
749 data += pktsze;
750 len -= pktsze;
751 }
752
753 /*
754 * Build the final TD for control status
755 */
Alan Stern25321782005-04-25 11:14:31 -0400756 td = uhci_alloc_td(uhci);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 if (!td)
Alan Sternaf0bb592005-12-17 18:00:12 -0500758 goto nomem;
Alan Stern28b93252007-02-19 15:51:51 -0500759 *plink = LINK_TO_TD(td);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760
761 /*
762 * It's IN if the pipe is an output pipe or we're not expecting
763 * data back.
764 */
765 destination &= ~TD_TOKEN_PID_MASK;
766 if (usb_pipeout(urb->pipe) || !urb->transfer_buffer_length)
767 destination |= USB_PID_IN;
768 else
769 destination |= USB_PID_OUT;
770
771 destination |= TD_TOKEN_TOGGLE; /* End in Data1 */
772
773 status &= ~TD_CTRL_SPD;
774
Alan Stern04538a22006-05-12 11:29:04 -0400775 uhci_add_td_to_urbp(td, urbp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 uhci_fill_td(td, status | TD_CTRL_IOC,
Alan Sterndccf4a42005-12-17 17:58:46 -0500777 destination | uhci_explen(0), 0);
Alan Sternaf0bb592005-12-17 18:00:12 -0500778 plink = &td->link;
779
780 /*
781 * Build the new dummy TD and activate the old one
782 */
783 td = uhci_alloc_td(uhci);
784 if (!td)
785 goto nomem;
Alan Stern28b93252007-02-19 15:51:51 -0500786 *plink = LINK_TO_TD(td);
Alan Sternaf0bb592005-12-17 18:00:12 -0500787
788 uhci_fill_td(td, 0, USB_PID_OUT | uhci_explen(0), 0);
789 wmb();
790 qh->dummy_td->status |= __constant_cpu_to_le32(TD_CTRL_ACTIVE);
791 qh->dummy_td = td;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792
793 /* Low-speed transfers get a different queue, and won't hog the bus.
794 * Also, some devices enumerate better without FSBR; the easiest way
795 * to do that is to put URBs on the low-speed queue while the device
Alan Stern630aa3c2006-01-23 17:17:21 -0500796 * isn't in the CONFIGURED state. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 if (urb->dev->speed == USB_SPEED_LOW ||
Alan Stern630aa3c2006-01-23 17:17:21 -0500798 urb->dev->state != USB_STATE_CONFIGURED)
Alan Sterndccf4a42005-12-17 17:58:46 -0500799 qh->skel = uhci->skel_ls_control_qh;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 else {
Alan Sterndccf4a42005-12-17 17:58:46 -0500801 qh->skel = uhci->skel_fs_control_qh;
Alan Stern84afddd2006-05-12 11:35:45 -0400802 uhci_add_fsbr(uhci, urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 }
Alan Stern59e29ed2006-05-12 11:19:19 -0400804
805 urb->actual_length = -8; /* Account for the SETUP packet */
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
814/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 * Common submit for bulk and interrupt
816 */
Alan Sterndccf4a42005-12-17 17:58:46 -0500817static int uhci_submit_common(struct uhci_hcd *uhci, struct urb *urb,
818 struct uhci_qh *qh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819{
820 struct uhci_td *td;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 unsigned long destination, status;
Alan Sterndccf4a42005-12-17 17:58:46 -0500822 int maxsze = le16_to_cpu(qh->hep->desc.wMaxPacketSize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 int len = urb->transfer_buffer_length;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 dma_addr_t data = urb->transfer_dma;
Alan Sternaf0bb592005-12-17 18:00:12 -0500825 __le32 *plink;
Alan Stern04538a22006-05-12 11:29:04 -0400826 struct urb_priv *urbp = urb->hcpriv;
Alan Sternaf0bb592005-12-17 18:00:12 -0500827 unsigned int toggle;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828
829 if (len < 0)
830 return -EINVAL;
831
832 /* The "pipe" thing contains the destination in bits 8--18 */
833 destination = (urb->pipe & PIPE_DEVEP_MASK) | usb_packetid(urb->pipe);
Alan Sternaf0bb592005-12-17 18:00:12 -0500834 toggle = usb_gettoggle(urb->dev, usb_pipeendpoint(urb->pipe),
835 usb_pipeout(urb->pipe));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836
Alan Sternaf0bb592005-12-17 18:00:12 -0500837 /* 3 errors, dummy TD remains inactive */
838 status = uhci_maxerr(3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 if (urb->dev->speed == USB_SPEED_LOW)
840 status |= TD_CTRL_LS;
841 if (usb_pipein(urb->pipe))
842 status |= TD_CTRL_SPD;
843
844 /*
Alan Stern687f5f32005-11-30 17:16:19 -0500845 * Build the DATA TDs
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 */
Alan Sternaf0bb592005-12-17 18:00:12 -0500847 plink = NULL;
848 td = qh->dummy_td;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 do { /* Allow zero length packets */
850 int pktsze = maxsze;
851
Alan Sterndccf4a42005-12-17 17:58:46 -0500852 if (len <= pktsze) { /* The last packet */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 pktsze = len;
854 if (!(urb->transfer_flags & URB_SHORT_NOT_OK))
855 status &= ~TD_CTRL_SPD;
856 }
857
Alan Sternaf0bb592005-12-17 18:00:12 -0500858 if (plink) {
859 td = uhci_alloc_td(uhci);
860 if (!td)
861 goto nomem;
Alan Stern28b93252007-02-19 15:51:51 -0500862 *plink = LINK_TO_TD(td);
Alan Sternaf0bb592005-12-17 18:00:12 -0500863 }
Alan Stern04538a22006-05-12 11:29:04 -0400864 uhci_add_td_to_urbp(td, urbp);
Alan Sterndccf4a42005-12-17 17:58:46 -0500865 uhci_fill_td(td, status,
Alan Sternaf0bb592005-12-17 18:00:12 -0500866 destination | uhci_explen(pktsze) |
867 (toggle << TD_TOKEN_TOGGLE_SHIFT),
868 data);
Alan Sterndccf4a42005-12-17 17:58:46 -0500869 plink = &td->link;
Alan Sternaf0bb592005-12-17 18:00:12 -0500870 status |= TD_CTRL_ACTIVE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871
872 data += pktsze;
873 len -= maxsze;
Alan Sternaf0bb592005-12-17 18:00:12 -0500874 toggle ^= 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 } while (len > 0);
876
877 /*
878 * URB_ZERO_PACKET means adding a 0-length packet, if direction
879 * is OUT and the transfer_length was an exact multiple of maxsze,
880 * hence (len = transfer_length - N * maxsze) == 0
881 * however, if transfer_length == 0, the zero packet was already
882 * prepared above.
883 */
Alan Sterndccf4a42005-12-17 17:58:46 -0500884 if ((urb->transfer_flags & URB_ZERO_PACKET) &&
885 usb_pipeout(urb->pipe) && len == 0 &&
886 urb->transfer_buffer_length > 0) {
Alan Stern25321782005-04-25 11:14:31 -0400887 td = uhci_alloc_td(uhci);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 if (!td)
Alan Sternaf0bb592005-12-17 18:00:12 -0500889 goto nomem;
Alan Stern28b93252007-02-19 15:51:51 -0500890 *plink = LINK_TO_TD(td);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891
Alan Stern04538a22006-05-12 11:29:04 -0400892 uhci_add_td_to_urbp(td, urbp);
Alan Sternaf0bb592005-12-17 18:00:12 -0500893 uhci_fill_td(td, status,
894 destination | uhci_explen(0) |
895 (toggle << TD_TOKEN_TOGGLE_SHIFT),
896 data);
897 plink = &td->link;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898
Alan Sternaf0bb592005-12-17 18:00:12 -0500899 toggle ^= 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900 }
901
902 /* Set the interrupt-on-completion flag on the last packet.
903 * A more-or-less typical 4 KB URB (= size of one memory page)
904 * will require about 3 ms to transfer; that's a little on the
905 * fast side but not enough to justify delaying an interrupt
906 * more than 2 or 3 URBs, so we will ignore the URB_NO_INTERRUPT
907 * flag setting. */
Alan Sterndccf4a42005-12-17 17:58:46 -0500908 td->status |= __constant_cpu_to_le32(TD_CTRL_IOC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909
Alan Sternaf0bb592005-12-17 18:00:12 -0500910 /*
911 * Build the new dummy TD and activate the old one
912 */
913 td = uhci_alloc_td(uhci);
914 if (!td)
915 goto nomem;
Alan Stern28b93252007-02-19 15:51:51 -0500916 *plink = LINK_TO_TD(td);
Alan Sternaf0bb592005-12-17 18:00:12 -0500917
918 uhci_fill_td(td, 0, USB_PID_OUT | uhci_explen(0), 0);
919 wmb();
920 qh->dummy_td->status |= __constant_cpu_to_le32(TD_CTRL_ACTIVE);
921 qh->dummy_td = td;
922
923 usb_settoggle(urb->dev, usb_pipeendpoint(urb->pipe),
924 usb_pipeout(urb->pipe), toggle);
Alan Sterndccf4a42005-12-17 17:58:46 -0500925 return 0;
Alan Sternaf0bb592005-12-17 18:00:12 -0500926
927nomem:
928 /* Remove the dummy TD from the td_list so it doesn't get freed */
Alan Stern04538a22006-05-12 11:29:04 -0400929 uhci_remove_td_from_urbp(qh->dummy_td);
Alan Sternaf0bb592005-12-17 18:00:12 -0500930 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931}
932
Alan Sterndccf4a42005-12-17 17:58:46 -0500933static inline int uhci_submit_bulk(struct uhci_hcd *uhci, struct urb *urb,
934 struct uhci_qh *qh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935{
936 int ret;
937
938 /* Can't have low-speed bulk transfers */
939 if (urb->dev->speed == USB_SPEED_LOW)
940 return -EINVAL;
941
Alan Sterndccf4a42005-12-17 17:58:46 -0500942 qh->skel = uhci->skel_bulk_qh;
943 ret = uhci_submit_common(uhci, urb, qh);
944 if (ret == 0)
Alan Stern84afddd2006-05-12 11:35:45 -0400945 uhci_add_fsbr(uhci, urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946 return ret;
947}
948
Alan Sterncaf38272006-05-19 16:44:55 -0400949static int uhci_submit_interrupt(struct uhci_hcd *uhci, struct urb *urb,
Alan Sterndccf4a42005-12-17 17:58:46 -0500950 struct uhci_qh *qh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951{
Alan Stern3ca2a322007-01-16 11:56:32 -0500952 int ret;
Alan Sterncaf38272006-05-19 16:44:55 -0400953
Alan Sterndccf4a42005-12-17 17:58:46 -0500954 /* USB 1.1 interrupt transfers only involve one packet per interval.
955 * Drivers can submit URBs of any length, but longer ones will need
956 * multiple intervals to complete.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957 */
Alan Sterncaf38272006-05-19 16:44:55 -0400958
Alan Stern3ca2a322007-01-16 11:56:32 -0500959 if (!qh->bandwidth_reserved) {
960 int exponent;
Alan Sterncaf38272006-05-19 16:44:55 -0400961
Alan Stern3ca2a322007-01-16 11:56:32 -0500962 /* Figure out which power-of-two queue to use */
963 for (exponent = 7; exponent >= 0; --exponent) {
964 if ((1 << exponent) <= urb->interval)
965 break;
966 }
967 if (exponent < 0)
968 return -EINVAL;
969 qh->period = 1 << exponent;
Alan Sterncaf38272006-05-19 16:44:55 -0400970 qh->skel = uhci->skelqh[UHCI_SKEL_INDEX(exponent)];
Alan Sterncaf38272006-05-19 16:44:55 -0400971
Alan Stern3ca2a322007-01-16 11:56:32 -0500972 /* For now, interrupt phase is fixed by the layout
973 * of the QH lists. */
974 qh->phase = (qh->period / 2) & (MAX_PHASE - 1);
975 ret = uhci_check_bandwidth(uhci, qh);
976 if (ret)
977 return ret;
978 } else if (qh->period > urb->interval)
979 return -EINVAL; /* Can't decrease the period */
980
981 ret = uhci_submit_common(uhci, urb, qh);
982 if (ret == 0) {
983 urb->interval = qh->period;
984 if (!qh->bandwidth_reserved)
985 uhci_reserve_bandwidth(uhci, qh);
986 }
987 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988}
989
990/*
Alan Sternb1869002006-05-12 11:14:25 -0400991 * Fix up the data structures following a short transfer
992 */
993static int uhci_fixup_short_transfer(struct uhci_hcd *uhci,
Alan Stern59e29ed2006-05-12 11:19:19 -0400994 struct uhci_qh *qh, struct urb_priv *urbp)
Alan Sternb1869002006-05-12 11:14:25 -0400995{
996 struct uhci_td *td;
Alan Stern59e29ed2006-05-12 11:19:19 -0400997 struct list_head *tmp;
998 int ret;
Alan Sternb1869002006-05-12 11:14:25 -0400999
1000 td = list_entry(urbp->td_list.prev, struct uhci_td, list);
1001 if (qh->type == USB_ENDPOINT_XFER_CONTROL) {
Alan Sternb1869002006-05-12 11:14:25 -04001002
1003 /* When a control transfer is short, we have to restart
1004 * the queue at the status stage transaction, which is
1005 * the last TD. */
Alan Stern59e29ed2006-05-12 11:19:19 -04001006 WARN_ON(list_empty(&urbp->td_list));
Alan Stern28b93252007-02-19 15:51:51 -05001007 qh->element = LINK_TO_TD(td);
Alan Stern59e29ed2006-05-12 11:19:19 -04001008 tmp = td->list.prev;
Alan Sternb1869002006-05-12 11:14:25 -04001009 ret = -EINPROGRESS;
1010
Alan Stern59e29ed2006-05-12 11:19:19 -04001011 } else {
Alan Sternb1869002006-05-12 11:14:25 -04001012
1013 /* When a bulk/interrupt transfer is short, we have to
1014 * fix up the toggles of the following URBs on the queue
1015 * before restarting the queue at the next URB. */
Alan Stern59e29ed2006-05-12 11:19:19 -04001016 qh->initial_toggle = uhci_toggle(td_token(qh->post_td)) ^ 1;
Alan Sternb1869002006-05-12 11:14:25 -04001017 uhci_fixup_toggles(qh, 1);
1018
Alan Stern59e29ed2006-05-12 11:19:19 -04001019 if (list_empty(&urbp->td_list))
1020 td = qh->post_td;
Alan Sternb1869002006-05-12 11:14:25 -04001021 qh->element = td->link;
Alan Stern59e29ed2006-05-12 11:19:19 -04001022 tmp = urbp->td_list.prev;
1023 ret = 0;
Alan Sternb1869002006-05-12 11:14:25 -04001024 }
1025
Alan Stern59e29ed2006-05-12 11:19:19 -04001026 /* Remove all the TDs we skipped over, from tmp back to the start */
1027 while (tmp != &urbp->td_list) {
1028 td = list_entry(tmp, struct uhci_td, list);
1029 tmp = tmp->prev;
1030
Alan Stern04538a22006-05-12 11:29:04 -04001031 uhci_remove_td_from_urbp(td);
1032 uhci_free_td(uhci, td);
Alan Stern59e29ed2006-05-12 11:19:19 -04001033 }
Alan Sternb1869002006-05-12 11:14:25 -04001034 return ret;
1035}
1036
1037/*
1038 * Common result for control, bulk, and interrupt
1039 */
1040static int uhci_result_common(struct uhci_hcd *uhci, struct urb *urb)
1041{
1042 struct urb_priv *urbp = urb->hcpriv;
1043 struct uhci_qh *qh = urbp->qh;
Alan Stern59e29ed2006-05-12 11:19:19 -04001044 struct uhci_td *td, *tmp;
Alan Sternb1869002006-05-12 11:14:25 -04001045 unsigned status;
1046 int ret = 0;
1047
Alan Stern59e29ed2006-05-12 11:19:19 -04001048 list_for_each_entry_safe(td, tmp, &urbp->td_list, list) {
Alan Sternb1869002006-05-12 11:14:25 -04001049 unsigned int ctrlstat;
1050 int len;
1051
Alan Sternb1869002006-05-12 11:14:25 -04001052 ctrlstat = td_status(td);
1053 status = uhci_status_bits(ctrlstat);
1054 if (status & TD_CTRL_ACTIVE)
1055 return -EINPROGRESS;
1056
1057 len = uhci_actual_length(ctrlstat);
1058 urb->actual_length += len;
1059
1060 if (status) {
1061 ret = uhci_map_status(status,
1062 uhci_packetout(td_token(td)));
1063 if ((debug == 1 && ret != -EPIPE) || debug > 1) {
1064 /* Some debugging code */
David Brownellbe3cbc52006-06-05 12:16:39 -04001065 dev_dbg(&urb->dev->dev,
Alan Sternb1869002006-05-12 11:14:25 -04001066 "%s: failed with status %x\n",
1067 __FUNCTION__, status);
1068
1069 if (debug > 1 && errbuf) {
1070 /* Print the chain for debugging */
1071 uhci_show_qh(urbp->qh, errbuf,
1072 ERRBUF_LEN, 0);
1073 lprintk(errbuf);
1074 }
1075 }
1076
1077 } else if (len < uhci_expected_length(td_token(td))) {
1078
1079 /* We received a short packet */
1080 if (urb->transfer_flags & URB_SHORT_NOT_OK)
1081 ret = -EREMOTEIO;
Alan Sternf443ddf2006-07-31 10:16:24 -04001082
1083 /* Fixup needed only if this isn't the URB's last TD */
1084 else if (&td->list != urbp->td_list.prev)
Alan Sternb1869002006-05-12 11:14:25 -04001085 ret = 1;
1086 }
1087
Alan Stern04538a22006-05-12 11:29:04 -04001088 uhci_remove_td_from_urbp(td);
Alan Stern59e29ed2006-05-12 11:19:19 -04001089 if (qh->post_td)
Alan Stern04538a22006-05-12 11:29:04 -04001090 uhci_free_td(uhci, qh->post_td);
Alan Stern59e29ed2006-05-12 11:19:19 -04001091 qh->post_td = td;
1092
Alan Sternb1869002006-05-12 11:14:25 -04001093 if (ret != 0)
1094 goto err;
1095 }
1096 return ret;
1097
1098err:
1099 if (ret < 0) {
1100 /* In case a control transfer gets an error
1101 * during the setup stage */
1102 urb->actual_length = max(urb->actual_length, 0);
1103
1104 /* Note that the queue has stopped and save
1105 * the next toggle value */
1106 qh->element = UHCI_PTR_TERM;
1107 qh->is_stopped = 1;
1108 qh->needs_fixup = (qh->type != USB_ENDPOINT_XFER_CONTROL);
1109 qh->initial_toggle = uhci_toggle(td_token(td)) ^
1110 (ret == -EREMOTEIO);
1111
1112 } else /* Short packet received */
Alan Stern59e29ed2006-05-12 11:19:19 -04001113 ret = uhci_fixup_short_transfer(uhci, qh, urbp);
Alan Sternb1869002006-05-12 11:14:25 -04001114 return ret;
1115}
1116
1117/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 * Isochronous transfers
1119 */
Alan Sterndccf4a42005-12-17 17:58:46 -05001120static int uhci_submit_isochronous(struct uhci_hcd *uhci, struct urb *urb,
1121 struct uhci_qh *qh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122{
Alan Sterndccf4a42005-12-17 17:58:46 -05001123 struct uhci_td *td = NULL; /* Since urb->number_of_packets > 0 */
Alan Stern0ed8fee2005-12-17 18:02:38 -05001124 int i, frame;
Alan Sterndccf4a42005-12-17 17:58:46 -05001125 unsigned long destination, status;
Alan Sternb81d3432005-10-13 17:00:24 -04001126 struct urb_priv *urbp = (struct urb_priv *) urb->hcpriv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127
Alan Sterncaf38272006-05-19 16:44:55 -04001128 /* Values must not be too big (could overflow below) */
1129 if (urb->interval >= UHCI_NUMFRAMES ||
1130 urb->number_of_packets >= UHCI_NUMFRAMES)
1131 return -EFBIG;
1132
1133 /* Check the period and figure out the starting frame number */
Alan Stern3ca2a322007-01-16 11:56:32 -05001134 if (!qh->bandwidth_reserved) {
1135 qh->period = urb->interval;
Alan Sterncaf38272006-05-19 16:44:55 -04001136 if (urb->transfer_flags & URB_ISO_ASAP) {
Alan Stern3ca2a322007-01-16 11:56:32 -05001137 qh->phase = -1; /* Find the best phase */
1138 i = uhci_check_bandwidth(uhci, qh);
1139 if (i)
1140 return i;
1141
1142 /* Allow a little time to allocate the TDs */
Alan Sternc8155cc2006-05-19 16:52:35 -04001143 uhci_get_current_frame_number(uhci);
Alan Stern3ca2a322007-01-16 11:56:32 -05001144 frame = uhci->frame_number + 10;
1145
1146 /* Move forward to the first frame having the
1147 * correct phase */
1148 urb->start_frame = frame + ((qh->phase - frame) &
1149 (qh->period - 1));
Alan Sterncaf38272006-05-19 16:44:55 -04001150 } else {
Alan Sternc8155cc2006-05-19 16:52:35 -04001151 i = urb->start_frame - uhci->last_iso_frame;
Alan Sterncaf38272006-05-19 16:44:55 -04001152 if (i <= 0 || i >= UHCI_NUMFRAMES)
1153 return -EINVAL;
Alan Stern3ca2a322007-01-16 11:56:32 -05001154 qh->phase = urb->start_frame & (qh->period - 1);
1155 i = uhci_check_bandwidth(uhci, qh);
1156 if (i)
1157 return i;
Alan Sterncaf38272006-05-19 16:44:55 -04001158 }
Alan Stern3ca2a322007-01-16 11:56:32 -05001159
Alan Sterncaf38272006-05-19 16:44:55 -04001160 } else if (qh->period != urb->interval) {
1161 return -EINVAL; /* Can't change the period */
1162
1163 } else { /* Pick up where the last URB leaves off */
1164 if (list_empty(&qh->queue)) {
Alan Sternc8155cc2006-05-19 16:52:35 -04001165 frame = qh->iso_frame;
Alan Sterncaf38272006-05-19 16:44:55 -04001166 } else {
1167 struct urb *lurb;
1168
1169 lurb = list_entry(qh->queue.prev,
1170 struct urb_priv, node)->urb;
1171 frame = lurb->start_frame +
1172 lurb->number_of_packets *
1173 lurb->interval;
1174 }
1175 if (urb->transfer_flags & URB_ISO_ASAP)
1176 urb->start_frame = frame;
Alan Sternc8155cc2006-05-19 16:52:35 -04001177 else if (urb->start_frame != frame)
1178 return -EINVAL;
Alan Sterncaf38272006-05-19 16:44:55 -04001179 }
1180
1181 /* Make sure we won't have to go too far into the future */
Alan Sternc8155cc2006-05-19 16:52:35 -04001182 if (uhci_frame_before_eq(uhci->last_iso_frame + UHCI_NUMFRAMES,
Alan Sterncaf38272006-05-19 16:44:55 -04001183 urb->start_frame + urb->number_of_packets *
1184 urb->interval))
Alan Stern0ed8fee2005-12-17 18:02:38 -05001185 return -EFBIG;
1186
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187 status = TD_CTRL_ACTIVE | TD_CTRL_IOS;
1188 destination = (urb->pipe & PIPE_DEVEP_MASK) | usb_packetid(urb->pipe);
1189
Alan Sternb81d3432005-10-13 17:00:24 -04001190 for (i = 0; i < urb->number_of_packets; i++) {
Alan Stern25321782005-04-25 11:14:31 -04001191 td = uhci_alloc_td(uhci);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192 if (!td)
1193 return -ENOMEM;
1194
Alan Stern04538a22006-05-12 11:29:04 -04001195 uhci_add_td_to_urbp(td, urbp);
Alan Sterndccf4a42005-12-17 17:58:46 -05001196 uhci_fill_td(td, status, destination |
1197 uhci_explen(urb->iso_frame_desc[i].length),
1198 urb->transfer_dma +
1199 urb->iso_frame_desc[i].offset);
Alan Sternb81d3432005-10-13 17:00:24 -04001200 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201
Alan Sterndccf4a42005-12-17 17:58:46 -05001202 /* Set the interrupt-on-completion flag on the last packet. */
1203 td->status |= __constant_cpu_to_le32(TD_CTRL_IOC);
1204
Alan Sterndccf4a42005-12-17 17:58:46 -05001205 /* Add the TDs to the frame list */
Alan Sternb81d3432005-10-13 17:00:24 -04001206 frame = urb->start_frame;
1207 list_for_each_entry(td, &urbp->td_list, list) {
Alan Sterndccf4a42005-12-17 17:58:46 -05001208 uhci_insert_td_in_frame_list(uhci, td, frame);
Alan Sternc8155cc2006-05-19 16:52:35 -04001209 frame += qh->period;
1210 }
1211
1212 if (list_empty(&qh->queue)) {
1213 qh->iso_packet_desc = &urb->iso_frame_desc[0];
1214 qh->iso_frame = urb->start_frame;
1215 qh->iso_status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216 }
1217
Alan Stern3ca2a322007-01-16 11:56:32 -05001218 qh->skel = uhci->skel_iso_qh;
1219 if (!qh->bandwidth_reserved)
1220 uhci_reserve_bandwidth(uhci, qh);
Alan Sterndccf4a42005-12-17 17:58:46 -05001221 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222}
1223
1224static int uhci_result_isochronous(struct uhci_hcd *uhci, struct urb *urb)
1225{
Alan Sternc8155cc2006-05-19 16:52:35 -04001226 struct uhci_td *td, *tmp;
1227 struct urb_priv *urbp = urb->hcpriv;
1228 struct uhci_qh *qh = urbp->qh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229
Alan Sternc8155cc2006-05-19 16:52:35 -04001230 list_for_each_entry_safe(td, tmp, &urbp->td_list, list) {
1231 unsigned int ctrlstat;
1232 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233 int actlength;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234
Alan Sternc8155cc2006-05-19 16:52:35 -04001235 if (uhci_frame_before_eq(uhci->cur_iso_frame, qh->iso_frame))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236 return -EINPROGRESS;
1237
Alan Sternc8155cc2006-05-19 16:52:35 -04001238 uhci_remove_tds_from_frame(uhci, qh->iso_frame);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239
Alan Sternc8155cc2006-05-19 16:52:35 -04001240 ctrlstat = td_status(td);
1241 if (ctrlstat & TD_CTRL_ACTIVE) {
1242 status = -EXDEV; /* TD was added too late? */
1243 } else {
1244 status = uhci_map_status(uhci_status_bits(ctrlstat),
1245 usb_pipeout(urb->pipe));
1246 actlength = uhci_actual_length(ctrlstat);
1247
1248 urb->actual_length += actlength;
1249 qh->iso_packet_desc->actual_length = actlength;
1250 qh->iso_packet_desc->status = status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251 }
1252
Alan Sternc8155cc2006-05-19 16:52:35 -04001253 if (status) {
1254 urb->error_count++;
1255 qh->iso_status = status;
1256 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257
Alan Sternc8155cc2006-05-19 16:52:35 -04001258 uhci_remove_td_from_urbp(td);
1259 uhci_free_td(uhci, td);
1260 qh->iso_frame += qh->period;
1261 ++qh->iso_packet_desc;
1262 }
1263 return qh->iso_status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264}
1265
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266static int uhci_urb_enqueue(struct usb_hcd *hcd,
Alan Sterndccf4a42005-12-17 17:58:46 -05001267 struct usb_host_endpoint *hep,
Al Viro55016f12005-10-21 03:21:58 -04001268 struct urb *urb, gfp_t mem_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269{
1270 int ret;
1271 struct uhci_hcd *uhci = hcd_to_uhci(hcd);
1272 unsigned long flags;
Alan Sterndccf4a42005-12-17 17:58:46 -05001273 struct urb_priv *urbp;
1274 struct uhci_qh *qh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275
1276 spin_lock_irqsave(&uhci->lock, flags);
1277
1278 ret = urb->status;
1279 if (ret != -EINPROGRESS) /* URB already unlinked! */
Alan Sterndccf4a42005-12-17 17:58:46 -05001280 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281
Alan Sterndccf4a42005-12-17 17:58:46 -05001282 ret = -ENOMEM;
1283 urbp = uhci_alloc_urb_priv(uhci, urb);
1284 if (!urbp)
1285 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286
Alan Sterndccf4a42005-12-17 17:58:46 -05001287 if (hep->hcpriv)
1288 qh = (struct uhci_qh *) hep->hcpriv;
1289 else {
1290 qh = uhci_alloc_qh(uhci, urb->dev, hep);
1291 if (!qh)
1292 goto err_no_qh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293 }
Alan Sterndccf4a42005-12-17 17:58:46 -05001294 urbp->qh = qh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295
Alan Stern4de7d2c2006-05-05 16:26:58 -04001296 switch (qh->type) {
1297 case USB_ENDPOINT_XFER_CONTROL:
Alan Sterndccf4a42005-12-17 17:58:46 -05001298 ret = uhci_submit_control(uhci, urb, qh);
1299 break;
Alan Stern4de7d2c2006-05-05 16:26:58 -04001300 case USB_ENDPOINT_XFER_BULK:
Alan Sterndccf4a42005-12-17 17:58:46 -05001301 ret = uhci_submit_bulk(uhci, urb, qh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302 break;
Alan Stern4de7d2c2006-05-05 16:26:58 -04001303 case USB_ENDPOINT_XFER_INT:
Alan Stern3ca2a322007-01-16 11:56:32 -05001304 ret = uhci_submit_interrupt(uhci, urb, qh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305 break;
Alan Stern4de7d2c2006-05-05 16:26:58 -04001306 case USB_ENDPOINT_XFER_ISOC:
Alan Sternc8155cc2006-05-19 16:52:35 -04001307 urb->error_count = 0;
Alan Sterndccf4a42005-12-17 17:58:46 -05001308 ret = uhci_submit_isochronous(uhci, urb, qh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309 break;
1310 }
Alan Sterndccf4a42005-12-17 17:58:46 -05001311 if (ret != 0)
1312 goto err_submit_failed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313
Alan Sterndccf4a42005-12-17 17:58:46 -05001314 /* Add this URB to the QH */
1315 urbp->qh = qh;
1316 list_add_tail(&urbp->node, &qh->queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317
Alan Sterndccf4a42005-12-17 17:58:46 -05001318 /* If the new URB is the first and only one on this QH then either
1319 * the QH is new and idle or else it's unlinked and waiting to
Alan Stern27755622006-05-05 16:32:02 -04001320 * become idle, so we can activate it right away. But only if the
1321 * queue isn't stopped. */
Alan Stern84afddd2006-05-12 11:35:45 -04001322 if (qh->queue.next == &urbp->node && !qh->is_stopped) {
Alan Sterndccf4a42005-12-17 17:58:46 -05001323 uhci_activate_qh(uhci, qh);
Alan Sternc5e3b742006-06-05 12:28:57 -04001324 uhci_urbp_wants_fsbr(uhci, urbp);
Alan Stern84afddd2006-05-12 11:35:45 -04001325 }
Alan Sterndccf4a42005-12-17 17:58:46 -05001326 goto done;
1327
1328err_submit_failed:
1329 if (qh->state == QH_STATE_IDLE)
1330 uhci_make_qh_idle(uhci, qh); /* Reclaim unused QH */
1331
1332err_no_qh:
1333 uhci_free_urb_priv(uhci, urbp);
1334
1335done:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336 spin_unlock_irqrestore(&uhci->lock, flags);
1337 return ret;
1338}
1339
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340static int uhci_urb_dequeue(struct usb_hcd *hcd, struct urb *urb)
1341{
1342 struct uhci_hcd *uhci = hcd_to_uhci(hcd);
1343 unsigned long flags;
1344 struct urb_priv *urbp;
Alan Stern10b8e472006-05-19 16:39:52 -04001345 struct uhci_qh *qh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346
1347 spin_lock_irqsave(&uhci->lock, flags);
1348 urbp = urb->hcpriv;
1349 if (!urbp) /* URB was never linked! */
1350 goto done;
Alan Stern10b8e472006-05-19 16:39:52 -04001351 qh = urbp->qh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352
Alan Sterndccf4a42005-12-17 17:58:46 -05001353 /* Remove Isochronous TDs from the frame list ASAP */
Alan Stern10b8e472006-05-19 16:39:52 -04001354 if (qh->type == USB_ENDPOINT_XFER_ISOC) {
Alan Sterndccf4a42005-12-17 17:58:46 -05001355 uhci_unlink_isochronous_tds(uhci, urb);
Alan Stern10b8e472006-05-19 16:39:52 -04001356 mb();
1357
1358 /* If the URB has already started, update the QH unlink time */
1359 uhci_get_current_frame_number(uhci);
1360 if (uhci_frame_before_eq(urb->start_frame, uhci->frame_number))
1361 qh->unlink_frame = uhci->frame_number;
1362 }
1363
1364 uhci_unlink_qh(uhci, qh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365
1366done:
1367 spin_unlock_irqrestore(&uhci->lock, flags);
1368 return 0;
1369}
1370
Alan Stern0ed8fee2005-12-17 18:02:38 -05001371/*
1372 * Finish unlinking an URB and give it back
1373 */
1374static void uhci_giveback_urb(struct uhci_hcd *uhci, struct uhci_qh *qh,
David Howells7d12e782006-10-05 14:55:46 +01001375 struct urb *urb)
Alan Stern0ed8fee2005-12-17 18:02:38 -05001376__releases(uhci->lock)
1377__acquires(uhci->lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378{
Alan Stern0ed8fee2005-12-17 18:02:38 -05001379 struct urb_priv *urbp = (struct urb_priv *) urb->hcpriv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380
Alan Sternc8155cc2006-05-19 16:52:35 -04001381 /* When giving back the first URB in an Isochronous queue,
1382 * reinitialize the QH's iso-related members for the next URB. */
1383 if (qh->type == USB_ENDPOINT_XFER_ISOC &&
1384 urbp->node.prev == &qh->queue &&
1385 urbp->node.next != &qh->queue) {
1386 struct urb *nurb = list_entry(urbp->node.next,
1387 struct urb_priv, node)->urb;
1388
1389 qh->iso_packet_desc = &nurb->iso_frame_desc[0];
1390 qh->iso_frame = nurb->start_frame;
1391 qh->iso_status = 0;
1392 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393
Alan Stern0ed8fee2005-12-17 18:02:38 -05001394 /* Take the URB off the QH's queue. If the queue is now empty,
1395 * this is a perfect time for a toggle fixup. */
1396 list_del_init(&urbp->node);
1397 if (list_empty(&qh->queue) && qh->needs_fixup) {
1398 usb_settoggle(urb->dev, usb_pipeendpoint(urb->pipe),
1399 usb_pipeout(urb->pipe), qh->initial_toggle);
1400 qh->needs_fixup = 0;
1401 }
1402
Alan Stern0ed8fee2005-12-17 18:02:38 -05001403 uhci_free_urb_priv(uhci, urbp);
1404
Alan Stern0ed8fee2005-12-17 18:02:38 -05001405 spin_unlock(&uhci->lock);
David Howells7d12e782006-10-05 14:55:46 +01001406 usb_hcd_giveback_urb(uhci_to_hcd(uhci), urb);
Alan Stern0ed8fee2005-12-17 18:02:38 -05001407 spin_lock(&uhci->lock);
1408
1409 /* If the queue is now empty, we can unlink the QH and give up its
1410 * reserved bandwidth. */
1411 if (list_empty(&qh->queue)) {
1412 uhci_unlink_qh(uhci, qh);
Alan Stern3ca2a322007-01-16 11:56:32 -05001413 if (qh->bandwidth_reserved)
1414 uhci_release_bandwidth(uhci, qh);
Alan Stern0ed8fee2005-12-17 18:02:38 -05001415 }
1416}
1417
1418/*
1419 * Scan the URBs in a QH's queue
1420 */
1421#define QH_FINISHED_UNLINKING(qh) \
1422 (qh->state == QH_STATE_UNLINKING && \
1423 uhci->frame_number + uhci->is_stopped != qh->unlink_frame)
1424
David Howells7d12e782006-10-05 14:55:46 +01001425static void uhci_scan_qh(struct uhci_hcd *uhci, struct uhci_qh *qh)
Alan Stern0ed8fee2005-12-17 18:02:38 -05001426{
1427 struct urb_priv *urbp;
1428 struct urb *urb;
1429 int status;
1430
1431 while (!list_empty(&qh->queue)) {
1432 urbp = list_entry(qh->queue.next, struct urb_priv, node);
1433 urb = urbp->urb;
1434
Alan Sternb1869002006-05-12 11:14:25 -04001435 if (qh->type == USB_ENDPOINT_XFER_ISOC)
Alan Stern0ed8fee2005-12-17 18:02:38 -05001436 status = uhci_result_isochronous(uhci, urb);
Alan Sternb1869002006-05-12 11:14:25 -04001437 else
Alan Stern0ed8fee2005-12-17 18:02:38 -05001438 status = uhci_result_common(uhci, urb);
Alan Stern0ed8fee2005-12-17 18:02:38 -05001439 if (status == -EINPROGRESS)
1440 break;
1441
1442 spin_lock(&urb->lock);
1443 if (urb->status == -EINPROGRESS) /* Not dequeued */
1444 urb->status = status;
1445 else
Alan Stern27755622006-05-05 16:32:02 -04001446 status = ECONNRESET; /* Not -ECONNRESET */
Alan Stern0ed8fee2005-12-17 18:02:38 -05001447 spin_unlock(&urb->lock);
1448
1449 /* Dequeued but completed URBs can't be given back unless
1450 * the QH is stopped or has finished unlinking. */
Alan Stern27755622006-05-05 16:32:02 -04001451 if (status == ECONNRESET) {
1452 if (QH_FINISHED_UNLINKING(qh))
1453 qh->is_stopped = 1;
1454 else if (!qh->is_stopped)
1455 return;
1456 }
Alan Stern0ed8fee2005-12-17 18:02:38 -05001457
David Howells7d12e782006-10-05 14:55:46 +01001458 uhci_giveback_urb(uhci, qh, urb);
Alan Stern7ceb9322006-08-21 11:58:50 -04001459 if (status < 0 && qh->type != USB_ENDPOINT_XFER_ISOC)
Alan Stern0ed8fee2005-12-17 18:02:38 -05001460 break;
1461 }
1462
1463 /* If the QH is neither stopped nor finished unlinking (normal case),
1464 * our work here is done. */
Alan Stern27755622006-05-05 16:32:02 -04001465 if (QH_FINISHED_UNLINKING(qh))
1466 qh->is_stopped = 1;
1467 else if (!qh->is_stopped)
Alan Stern0ed8fee2005-12-17 18:02:38 -05001468 return;
1469
1470 /* Otherwise give back each of the dequeued URBs */
Alan Stern27755622006-05-05 16:32:02 -04001471restart:
Alan Stern0ed8fee2005-12-17 18:02:38 -05001472 list_for_each_entry(urbp, &qh->queue, node) {
1473 urb = urbp->urb;
1474 if (urb->status != -EINPROGRESS) {
Alan Stern10b8e472006-05-19 16:39:52 -04001475
1476 /* Fix up the TD links and save the toggles for
1477 * non-Isochronous queues. For Isochronous queues,
1478 * test for too-recent dequeues. */
1479 if (!uhci_cleanup_queue(uhci, qh, urb)) {
1480 qh->is_stopped = 0;
1481 return;
1482 }
David Howells7d12e782006-10-05 14:55:46 +01001483 uhci_giveback_urb(uhci, qh, urb);
Alan Stern0ed8fee2005-12-17 18:02:38 -05001484 goto restart;
1485 }
1486 }
1487 qh->is_stopped = 0;
1488
1489 /* There are no more dequeued URBs. If there are still URBs on the
1490 * queue, the QH can now be re-activated. */
1491 if (!list_empty(&qh->queue)) {
1492 if (qh->needs_fixup)
1493 uhci_fixup_toggles(qh, 0);
Alan Stern84afddd2006-05-12 11:35:45 -04001494
1495 /* If the first URB on the queue wants FSBR but its time
1496 * limit has expired, set the next TD to interrupt on
1497 * completion before reactivating the QH. */
1498 urbp = list_entry(qh->queue.next, struct urb_priv, node);
1499 if (urbp->fsbr && qh->wait_expired) {
1500 struct uhci_td *td = list_entry(urbp->td_list.next,
1501 struct uhci_td, list);
1502
1503 td->status |= __cpu_to_le32(TD_CTRL_IOC);
1504 }
1505
Alan Stern0ed8fee2005-12-17 18:02:38 -05001506 uhci_activate_qh(uhci, qh);
1507 }
1508
1509 /* The queue is empty. The QH can become idle if it is fully
1510 * unlinked. */
1511 else if (QH_FINISHED_UNLINKING(qh))
1512 uhci_make_qh_idle(uhci, qh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513}
1514
Alan Stern0ed8fee2005-12-17 18:02:38 -05001515/*
Alan Stern84afddd2006-05-12 11:35:45 -04001516 * Check for queues that have made some forward progress.
1517 * Returns 0 if the queue is not Isochronous, is ACTIVE, and
1518 * has not advanced since last examined; 1 otherwise.
Alan Sternb761d9d2006-05-12 11:41:59 -04001519 *
1520 * Early Intel controllers have a bug which causes qh->element sometimes
1521 * not to advance when a TD completes successfully. The queue remains
1522 * stuck on the inactive completed TD. We detect such cases and advance
1523 * the element pointer by hand.
Alan Stern84afddd2006-05-12 11:35:45 -04001524 */
1525static int uhci_advance_check(struct uhci_hcd *uhci, struct uhci_qh *qh)
1526{
1527 struct urb_priv *urbp = NULL;
1528 struct uhci_td *td;
1529 int ret = 1;
1530 unsigned status;
1531
1532 if (qh->type == USB_ENDPOINT_XFER_ISOC)
Alan Sternc5e3b742006-06-05 12:28:57 -04001533 goto done;
Alan Stern84afddd2006-05-12 11:35:45 -04001534
1535 /* Treat an UNLINKING queue as though it hasn't advanced.
1536 * This is okay because reactivation will treat it as though
1537 * it has advanced, and if it is going to become IDLE then
1538 * this doesn't matter anyway. Furthermore it's possible
1539 * for an UNLINKING queue not to have any URBs at all, or
1540 * for its first URB not to have any TDs (if it was dequeued
1541 * just as it completed). So it's not easy in any case to
1542 * test whether such queues have advanced. */
1543 if (qh->state != QH_STATE_ACTIVE) {
1544 urbp = NULL;
1545 status = 0;
1546
1547 } else {
1548 urbp = list_entry(qh->queue.next, struct urb_priv, node);
1549 td = list_entry(urbp->td_list.next, struct uhci_td, list);
1550 status = td_status(td);
1551 if (!(status & TD_CTRL_ACTIVE)) {
1552
1553 /* We're okay, the queue has advanced */
1554 qh->wait_expired = 0;
1555 qh->advance_jiffies = jiffies;
Alan Sternc5e3b742006-06-05 12:28:57 -04001556 goto done;
Alan Stern84afddd2006-05-12 11:35:45 -04001557 }
1558 ret = 0;
1559 }
1560
1561 /* The queue hasn't advanced; check for timeout */
Alan Sternc5e3b742006-06-05 12:28:57 -04001562 if (qh->wait_expired)
1563 goto done;
1564
1565 if (time_after(jiffies, qh->advance_jiffies + QH_WAIT_TIMEOUT)) {
Alan Sternb761d9d2006-05-12 11:41:59 -04001566
1567 /* Detect the Intel bug and work around it */
Alan Stern28b93252007-02-19 15:51:51 -05001568 if (qh->post_td && qh_element(qh) == LINK_TO_TD(qh->post_td)) {
Alan Sternb761d9d2006-05-12 11:41:59 -04001569 qh->element = qh->post_td->link;
1570 qh->advance_jiffies = jiffies;
Alan Sternc5e3b742006-06-05 12:28:57 -04001571 ret = 1;
1572 goto done;
Alan Sternb761d9d2006-05-12 11:41:59 -04001573 }
1574
Alan Stern84afddd2006-05-12 11:35:45 -04001575 qh->wait_expired = 1;
1576
1577 /* If the current URB wants FSBR, unlink it temporarily
1578 * so that we can safely set the next TD to interrupt on
1579 * completion. That way we'll know as soon as the queue
1580 * starts moving again. */
1581 if (urbp && urbp->fsbr && !(status & TD_CTRL_IOC))
1582 uhci_unlink_qh(uhci, qh);
Alan Sternc5e3b742006-06-05 12:28:57 -04001583
1584 } else {
1585 /* Unmoving but not-yet-expired queues keep FSBR alive */
1586 if (urbp)
1587 uhci_urbp_wants_fsbr(uhci, urbp);
Alan Stern84afddd2006-05-12 11:35:45 -04001588 }
Alan Sternc5e3b742006-06-05 12:28:57 -04001589
1590done:
Alan Stern84afddd2006-05-12 11:35:45 -04001591 return ret;
1592}
1593
1594/*
Alan Stern0ed8fee2005-12-17 18:02:38 -05001595 * Process events in the schedule, but only in one thread at a time
1596 */
David Howells7d12e782006-10-05 14:55:46 +01001597static void uhci_scan_schedule(struct uhci_hcd *uhci)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598{
Alan Stern0ed8fee2005-12-17 18:02:38 -05001599 int i;
1600 struct uhci_qh *qh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601
1602 /* Don't allow re-entrant calls */
1603 if (uhci->scan_in_progress) {
1604 uhci->need_rescan = 1;
1605 return;
1606 }
1607 uhci->scan_in_progress = 1;
Alan Stern84afddd2006-05-12 11:35:45 -04001608rescan:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609 uhci->need_rescan = 0;
Alan Sternc5e3b742006-06-05 12:28:57 -04001610 uhci->fsbr_is_wanted = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611
Alan Stern6c1b4452005-04-21 16:04:58 -04001612 uhci_clear_next_interrupt(uhci);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613 uhci_get_current_frame_number(uhci);
Alan Sternc8155cc2006-05-19 16:52:35 -04001614 uhci->cur_iso_frame = uhci->frame_number;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615
Alan Stern0ed8fee2005-12-17 18:02:38 -05001616 /* Go through all the QH queues and process the URBs in each one */
1617 for (i = 0; i < UHCI_NUM_SKELQH - 1; ++i) {
1618 uhci->next_qh = list_entry(uhci->skelqh[i]->node.next,
1619 struct uhci_qh, node);
1620 while ((qh = uhci->next_qh) != uhci->skelqh[i]) {
1621 uhci->next_qh = list_entry(qh->node.next,
1622 struct uhci_qh, node);
Alan Stern84afddd2006-05-12 11:35:45 -04001623
1624 if (uhci_advance_check(uhci, qh)) {
David Howells7d12e782006-10-05 14:55:46 +01001625 uhci_scan_qh(uhci, qh);
Alan Sternc5e3b742006-06-05 12:28:57 -04001626 if (qh->state == QH_STATE_ACTIVE) {
1627 uhci_urbp_wants_fsbr(uhci,
1628 list_entry(qh->queue.next, struct urb_priv, node));
1629 }
Alan Stern84afddd2006-05-12 11:35:45 -04001630 }
Alan Stern0ed8fee2005-12-17 18:02:38 -05001631 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633
Alan Sternc8155cc2006-05-19 16:52:35 -04001634 uhci->last_iso_frame = uhci->cur_iso_frame;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635 if (uhci->need_rescan)
1636 goto rescan;
1637 uhci->scan_in_progress = 0;
1638
Alan Sternc5e3b742006-06-05 12:28:57 -04001639 if (uhci->fsbr_is_on && !uhci->fsbr_is_wanted &&
1640 !uhci->fsbr_expiring) {
1641 uhci->fsbr_expiring = 1;
1642 mod_timer(&uhci->fsbr_timer, jiffies + FSBR_OFF_DELAY);
1643 }
Alan Stern84afddd2006-05-12 11:35:45 -04001644
Alan Stern04538a22006-05-12 11:29:04 -04001645 if (list_empty(&uhci->skel_unlink_qh->node))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646 uhci_clear_next_interrupt(uhci);
1647 else
1648 uhci_set_next_interrupt(uhci);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649}