blob: 44bba9a6d196620388587c26a85680bf9df1678e [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 Sterndccf4a42005-12-17 17:58:46 -050016 * (C) Copyright 2004-2005 Alan Stern, stern@rowland.harvard.edu
Linus Torvalds1da177e2005-04-16 15:20:36 -070017 */
18
Linus Torvalds1da177e2005-04-16 15:20:36 -070019static void uhci_free_pending_tds(struct uhci_hcd *uhci);
20
21/*
22 * Technically, updating td->status here is a race, but it's not really a
23 * problem. The worst that can happen is that we set the IOC bit again
24 * generating a spurious interrupt. We could fix this by creating another
25 * QH and leaving the IOC bit always set, but then we would have to play
26 * games with the FSBR code to make sure we get the correct order in all
27 * the cases. I don't think it's worth the effort
28 */
Alan Sterndccf4a42005-12-17 17:58:46 -050029static void uhci_set_next_interrupt(struct uhci_hcd *uhci)
Linus Torvalds1da177e2005-04-16 15:20:36 -070030{
Alan Stern6c1b4452005-04-21 16:04:58 -040031 if (uhci->is_stopped)
Alan Stern1f09df82005-09-05 13:59:51 -040032 mod_timer(&uhci_to_hcd(uhci)->rh_timer, jiffies);
Linus Torvalds1da177e2005-04-16 15:20:36 -070033 uhci->term_td->status |= cpu_to_le32(TD_CTRL_IOC);
34}
35
36static inline void uhci_clear_next_interrupt(struct uhci_hcd *uhci)
37{
38 uhci->term_td->status &= ~cpu_to_le32(TD_CTRL_IOC);
39}
40
Alan Stern25321782005-04-25 11:14:31 -040041static struct uhci_td *uhci_alloc_td(struct uhci_hcd *uhci)
Linus Torvalds1da177e2005-04-16 15:20:36 -070042{
43 dma_addr_t dma_handle;
44 struct uhci_td *td;
45
46 td = dma_pool_alloc(uhci->td_pool, GFP_ATOMIC, &dma_handle);
47 if (!td)
48 return NULL;
49
50 td->dma_handle = dma_handle;
Linus Torvalds1da177e2005-04-16 15:20:36 -070051 td->frame = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070052
53 INIT_LIST_HEAD(&td->list);
54 INIT_LIST_HEAD(&td->remove_list);
55 INIT_LIST_HEAD(&td->fl_list);
56
Linus Torvalds1da177e2005-04-16 15:20:36 -070057 return td;
58}
59
Alan Sterndccf4a42005-12-17 17:58:46 -050060static void uhci_free_td(struct uhci_hcd *uhci, struct uhci_td *td)
61{
62 if (!list_empty(&td->list))
63 dev_warn(uhci_dev(uhci), "td %p still in list!\n", td);
64 if (!list_empty(&td->remove_list))
65 dev_warn(uhci_dev(uhci), "td %p still in remove_list!\n", td);
66 if (!list_empty(&td->fl_list))
67 dev_warn(uhci_dev(uhci), "td %p still in fl_list!\n", td);
68
69 dma_pool_free(uhci->td_pool, td, td->dma_handle);
70}
71
Linus Torvalds1da177e2005-04-16 15:20:36 -070072static inline void uhci_fill_td(struct uhci_td *td, u32 status,
73 u32 token, u32 buffer)
74{
75 td->status = cpu_to_le32(status);
76 td->token = cpu_to_le32(token);
77 td->buffer = cpu_to_le32(buffer);
78}
79
80/*
Alan Stern687f5f32005-11-30 17:16:19 -050081 * We insert Isochronous URBs directly into the frame list at the beginning
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 */
Alan Sterndccf4a42005-12-17 17:58:46 -050083static inline void uhci_insert_td_in_frame_list(struct uhci_hcd *uhci,
84 struct uhci_td *td, unsigned framenum)
Linus Torvalds1da177e2005-04-16 15:20:36 -070085{
86 framenum &= (UHCI_NUMFRAMES - 1);
87
88 td->frame = framenum;
89
90 /* Is there a TD already mapped there? */
Alan Sterna1d59ce2005-09-16 14:22:51 -040091 if (uhci->frame_cpu[framenum]) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 struct uhci_td *ftd, *ltd;
93
Alan Sterna1d59ce2005-09-16 14:22:51 -040094 ftd = uhci->frame_cpu[framenum];
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 ltd = list_entry(ftd->fl_list.prev, struct uhci_td, fl_list);
96
97 list_add_tail(&td->fl_list, &ftd->fl_list);
98
99 td->link = ltd->link;
100 wmb();
101 ltd->link = cpu_to_le32(td->dma_handle);
102 } else {
Alan Sterna1d59ce2005-09-16 14:22:51 -0400103 td->link = uhci->frame[framenum];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 wmb();
Alan Sterna1d59ce2005-09-16 14:22:51 -0400105 uhci->frame[framenum] = cpu_to_le32(td->dma_handle);
106 uhci->frame_cpu[framenum] = td;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 }
108}
109
Alan Sterndccf4a42005-12-17 17:58:46 -0500110static inline void uhci_remove_td_from_frame_list(struct uhci_hcd *uhci,
Alan Sternb81d3432005-10-13 17:00:24 -0400111 struct uhci_td *td)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112{
113 /* If it's not inserted, don't remove it */
Alan Sternb81d3432005-10-13 17:00:24 -0400114 if (td->frame == -1) {
115 WARN_ON(!list_empty(&td->fl_list));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 return;
Alan Sternb81d3432005-10-13 17:00:24 -0400117 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118
Alan Sternb81d3432005-10-13 17:00:24 -0400119 if (uhci->frame_cpu[td->frame] == td) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 if (list_empty(&td->fl_list)) {
Alan Sterna1d59ce2005-09-16 14:22:51 -0400121 uhci->frame[td->frame] = td->link;
122 uhci->frame_cpu[td->frame] = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 } else {
124 struct uhci_td *ntd;
125
126 ntd = list_entry(td->fl_list.next, struct uhci_td, fl_list);
Alan Sterna1d59ce2005-09-16 14:22:51 -0400127 uhci->frame[td->frame] = cpu_to_le32(ntd->dma_handle);
128 uhci->frame_cpu[td->frame] = ntd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 }
130 } else {
131 struct uhci_td *ptd;
132
133 ptd = list_entry(td->fl_list.prev, struct uhci_td, fl_list);
134 ptd->link = td->link;
135 }
136
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 list_del_init(&td->fl_list);
138 td->frame = -1;
139}
140
Alan Sterndccf4a42005-12-17 17:58:46 -0500141/*
142 * Remove all the TDs for an Isochronous URB from the frame list
143 */
144static void uhci_unlink_isochronous_tds(struct uhci_hcd *uhci, struct urb *urb)
Alan Sternb81d3432005-10-13 17:00:24 -0400145{
146 struct urb_priv *urbp = (struct urb_priv *) urb->hcpriv;
147 struct uhci_td *td;
148
149 list_for_each_entry(td, &urbp->td_list, list)
Alan Sterndccf4a42005-12-17 17:58:46 -0500150 uhci_remove_td_from_frame_list(uhci, td);
Alan Sternb81d3432005-10-13 17:00:24 -0400151 wmb();
152}
153
Alan Sterndccf4a42005-12-17 17:58:46 -0500154static struct uhci_qh *uhci_alloc_qh(struct uhci_hcd *uhci,
155 struct usb_device *udev, struct usb_host_endpoint *hep)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156{
157 dma_addr_t dma_handle;
158 struct uhci_qh *qh;
159
160 qh = dma_pool_alloc(uhci->qh_pool, GFP_ATOMIC, &dma_handle);
161 if (!qh)
162 return NULL;
163
164 qh->dma_handle = dma_handle;
165
166 qh->element = UHCI_PTR_TERM;
167 qh->link = UHCI_PTR_TERM;
168
Alan Sterndccf4a42005-12-17 17:58:46 -0500169 INIT_LIST_HEAD(&qh->queue);
170 INIT_LIST_HEAD(&qh->node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171
Alan Sterndccf4a42005-12-17 17:58:46 -0500172 if (udev) { /* Normal QH */
Alan Sternaf0bb592005-12-17 18:00:12 -0500173 qh->dummy_td = uhci_alloc_td(uhci);
174 if (!qh->dummy_td) {
175 dma_pool_free(uhci->qh_pool, qh, dma_handle);
176 return NULL;
177 }
Alan Sterndccf4a42005-12-17 17:58:46 -0500178 qh->state = QH_STATE_IDLE;
179 qh->hep = hep;
180 qh->udev = udev;
181 hep->hcpriv = qh;
182 usb_get_dev(udev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183
Alan Sterndccf4a42005-12-17 17:58:46 -0500184 } else { /* Skeleton QH */
185 qh->state = QH_STATE_ACTIVE;
186 qh->udev = NULL;
187 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 return qh;
189}
190
191static void uhci_free_qh(struct uhci_hcd *uhci, struct uhci_qh *qh)
192{
Alan Sterndccf4a42005-12-17 17:58:46 -0500193 WARN_ON(qh->state != QH_STATE_IDLE && qh->udev);
194 if (!list_empty(&qh->queue))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 dev_warn(uhci_dev(uhci), "qh %p list not empty!\n", qh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196
Alan Sterndccf4a42005-12-17 17:58:46 -0500197 list_del(&qh->node);
198 if (qh->udev) {
199 qh->hep->hcpriv = NULL;
200 usb_put_dev(qh->udev);
Alan Sternaf0bb592005-12-17 18:00:12 -0500201 uhci_free_td(uhci, qh->dummy_td);
Alan Sterndccf4a42005-12-17 17:58:46 -0500202 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 dma_pool_free(uhci->qh_pool, qh, qh->dma_handle);
204}
205
206/*
Alan Stern0ed8fee2005-12-17 18:02:38 -0500207 * When the currently executing URB is dequeued, save its current toggle value
208 */
209static void uhci_save_toggle(struct uhci_qh *qh, struct urb *urb)
210{
211 struct urb_priv *urbp = (struct urb_priv *) urb->hcpriv;
212 struct uhci_td *td;
213
214 /* If the QH element pointer is UHCI_PTR_TERM then then currently
215 * executing URB has already been unlinked, so this one isn't it. */
216 if (qh_element(qh) == UHCI_PTR_TERM ||
217 qh->queue.next != &urbp->node)
218 return;
219 qh->element = UHCI_PTR_TERM;
220
221 /* Only bulk and interrupt pipes have to worry about toggles */
222 if (!(usb_pipetype(urb->pipe) == PIPE_BULK ||
223 usb_pipetype(urb->pipe) == PIPE_INTERRUPT))
224 return;
225
226 /* Find the first active TD; that's the device's toggle state */
227 list_for_each_entry(td, &urbp->td_list, list) {
228 if (td_status(td) & TD_CTRL_ACTIVE) {
229 qh->needs_fixup = 1;
230 qh->initial_toggle = uhci_toggle(td_token(td));
231 return;
232 }
233 }
234
235 WARN_ON(1);
236}
237
238/*
239 * Fix up the data toggles for URBs in a queue, when one of them
240 * terminates early (short transfer, error, or dequeued).
241 */
242static void uhci_fixup_toggles(struct uhci_qh *qh, int skip_first)
243{
244 struct urb_priv *urbp = NULL;
245 struct uhci_td *td;
246 unsigned int toggle = qh->initial_toggle;
247 unsigned int pipe;
248
249 /* Fixups for a short transfer start with the second URB in the
250 * queue (the short URB is the first). */
251 if (skip_first)
252 urbp = list_entry(qh->queue.next, struct urb_priv, node);
253
254 /* When starting with the first URB, if the QH element pointer is
255 * still valid then we know the URB's toggles are okay. */
256 else if (qh_element(qh) != UHCI_PTR_TERM)
257 toggle = 2;
258
259 /* Fix up the toggle for the URBs in the queue. Normally this
260 * loop won't run more than once: When an error or short transfer
261 * occurs, the queue usually gets emptied. */
262 list_prepare_entry(urbp, &qh->queue, node);
263 list_for_each_entry_continue(urbp, &qh->queue, node) {
264
265 /* If the first TD has the right toggle value, we don't
266 * need to change any toggles in this URB */
267 td = list_entry(urbp->td_list.next, struct uhci_td, list);
268 if (toggle > 1 || uhci_toggle(td_token(td)) == toggle) {
269 td = list_entry(urbp->td_list.next, struct uhci_td,
270 list);
271 toggle = uhci_toggle(td_token(td)) ^ 1;
272
273 /* Otherwise all the toggles in the URB have to be switched */
274 } else {
275 list_for_each_entry(td, &urbp->td_list, list) {
276 td->token ^= __constant_cpu_to_le32(
277 TD_TOKEN_TOGGLE);
278 toggle ^= 1;
279 }
280 }
281 }
282
283 wmb();
284 pipe = list_entry(qh->queue.next, struct urb_priv, node)->urb->pipe;
285 usb_settoggle(qh->udev, usb_pipeendpoint(pipe),
286 usb_pipeout(pipe), toggle);
287 qh->needs_fixup = 0;
288}
289
290/*
Alan Sterndccf4a42005-12-17 17:58:46 -0500291 * Put a QH on the schedule in both hardware and software
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 */
Alan Sterndccf4a42005-12-17 17:58:46 -0500293static void uhci_activate_qh(struct uhci_hcd *uhci, struct uhci_qh *qh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294{
Alan Sterndccf4a42005-12-17 17:58:46 -0500295 struct uhci_qh *pqh;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296
Alan Sterndccf4a42005-12-17 17:58:46 -0500297 WARN_ON(list_empty(&qh->queue));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298
Alan Sterndccf4a42005-12-17 17:58:46 -0500299 /* Set the element pointer if it isn't set already.
300 * This isn't needed for Isochronous queues, but it doesn't hurt. */
301 if (qh_element(qh) == UHCI_PTR_TERM) {
302 struct urb_priv *urbp = list_entry(qh->queue.next,
303 struct urb_priv, node);
304 struct uhci_td *td = list_entry(urbp->td_list.next,
305 struct uhci_td, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306
Alan Sterndccf4a42005-12-17 17:58:46 -0500307 qh->element = cpu_to_le32(td->dma_handle);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 }
309
Alan Sterndccf4a42005-12-17 17:58:46 -0500310 if (qh->state == QH_STATE_ACTIVE)
311 return;
312 qh->state = QH_STATE_ACTIVE;
313
314 /* Move the QH from its old list to the end of the appropriate
315 * skeleton's list */
Alan Stern0ed8fee2005-12-17 18:02:38 -0500316 if (qh == uhci->next_qh)
317 uhci->next_qh = list_entry(qh->node.next, struct uhci_qh,
318 node);
Alan Sterndccf4a42005-12-17 17:58:46 -0500319 list_move_tail(&qh->node, &qh->skel->node);
320
321 /* Link it into the schedule */
322 pqh = list_entry(qh->node.prev, struct uhci_qh, node);
323 qh->link = pqh->link;
324 wmb();
325 pqh->link = UHCI_PTR_QH | cpu_to_le32(qh->dma_handle);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326}
327
328/*
Alan Sterndccf4a42005-12-17 17:58:46 -0500329 * Take a QH off the hardware schedule
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 */
Alan Sterndccf4a42005-12-17 17:58:46 -0500331static void uhci_unlink_qh(struct uhci_hcd *uhci, struct uhci_qh *qh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332{
333 struct uhci_qh *pqh;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334
Alan Sterndccf4a42005-12-17 17:58:46 -0500335 if (qh->state == QH_STATE_UNLINKING)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 return;
Alan Sterndccf4a42005-12-17 17:58:46 -0500337 WARN_ON(qh->state != QH_STATE_ACTIVE || !qh->udev);
338 qh->state = QH_STATE_UNLINKING;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339
Alan Sterndccf4a42005-12-17 17:58:46 -0500340 /* Unlink the QH from the schedule and record when we did it */
341 pqh = list_entry(qh->node.prev, struct uhci_qh, node);
342 pqh->link = qh->link;
343 mb();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344
345 uhci_get_current_frame_number(uhci);
Alan Sterndccf4a42005-12-17 17:58:46 -0500346 qh->unlink_frame = uhci->frame_number;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347
Alan Sterndccf4a42005-12-17 17:58:46 -0500348 /* Force an interrupt so we know when the QH is fully unlinked */
349 if (list_empty(&uhci->skel_unlink_qh->node))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 uhci_set_next_interrupt(uhci);
351
Alan Sterndccf4a42005-12-17 17:58:46 -0500352 /* Move the QH from its old list to the end of the unlinking list */
Alan Stern0ed8fee2005-12-17 18:02:38 -0500353 if (qh == uhci->next_qh)
354 uhci->next_qh = list_entry(qh->node.next, struct uhci_qh,
355 node);
Alan Sterndccf4a42005-12-17 17:58:46 -0500356 list_move_tail(&qh->node, &uhci->skel_unlink_qh->node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357}
358
Alan Sterndccf4a42005-12-17 17:58:46 -0500359/*
360 * When we and the controller are through with a QH, it becomes IDLE.
361 * This happens when a QH has been off the schedule (on the unlinking
362 * list) for more than one frame, or when an error occurs while adding
363 * the first URB onto a new QH.
364 */
365static void uhci_make_qh_idle(struct uhci_hcd *uhci, struct uhci_qh *qh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366{
Alan Sterndccf4a42005-12-17 17:58:46 -0500367 WARN_ON(qh->state == QH_STATE_ACTIVE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368
Alan Stern0ed8fee2005-12-17 18:02:38 -0500369 if (qh == uhci->next_qh)
370 uhci->next_qh = list_entry(qh->node.next, struct uhci_qh,
371 node);
Alan Sterndccf4a42005-12-17 17:58:46 -0500372 list_move(&qh->node, &uhci->idle_qh_list);
373 qh->state = QH_STATE_IDLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374
Alan Sterndccf4a42005-12-17 17:58:46 -0500375 /* If anyone is waiting for a QH to become idle, wake them up */
376 if (uhci->num_waiting)
377 wake_up_all(&uhci->waitqh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378}
379
Alan Sterndccf4a42005-12-17 17:58:46 -0500380static inline struct urb_priv *uhci_alloc_urb_priv(struct uhci_hcd *uhci,
381 struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382{
383 struct urb_priv *urbp;
384
385 urbp = kmem_cache_alloc(uhci_up_cachep, SLAB_ATOMIC);
386 if (!urbp)
387 return NULL;
388
389 memset((void *)urbp, 0, sizeof(*urbp));
390
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 urbp->urb = urb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 urb->hcpriv = urbp;
Alan Sterndccf4a42005-12-17 17:58:46 -0500393
394 INIT_LIST_HEAD(&urbp->node);
395 INIT_LIST_HEAD(&urbp->td_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396
397 return urbp;
398}
399
400static void uhci_add_td_to_urb(struct urb *urb, struct uhci_td *td)
401{
402 struct urb_priv *urbp = (struct urb_priv *)urb->hcpriv;
403
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 list_add_tail(&td->list, &urbp->td_list);
405}
406
407static void uhci_remove_td_from_urb(struct uhci_td *td)
408{
409 if (list_empty(&td->list))
410 return;
411
412 list_del_init(&td->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413}
414
Alan Sterndccf4a42005-12-17 17:58:46 -0500415static void uhci_free_urb_priv(struct uhci_hcd *uhci,
416 struct urb_priv *urbp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417{
418 struct uhci_td *td, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419
Alan Sterndccf4a42005-12-17 17:58:46 -0500420 if (!list_empty(&urbp->node))
421 dev_warn(uhci_dev(uhci), "urb %p still on QH's list!\n",
422 urbp->urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423
424 uhci_get_current_frame_number(uhci);
425 if (uhci->frame_number + uhci->is_stopped != uhci->td_remove_age) {
426 uhci_free_pending_tds(uhci);
427 uhci->td_remove_age = uhci->frame_number;
428 }
429
430 /* Check to see if the remove list is empty. Set the IOC bit */
Alan Sterndccf4a42005-12-17 17:58:46 -0500431 /* to force an interrupt so we can remove the TDs. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 if (list_empty(&uhci->td_remove_list))
433 uhci_set_next_interrupt(uhci);
434
435 list_for_each_entry_safe(td, tmp, &urbp->td_list, list) {
436 uhci_remove_td_from_urb(td);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 list_add(&td->remove_list, &uhci->td_remove_list);
438 }
439
Alan Sterndccf4a42005-12-17 17:58:46 -0500440 urbp->urb->hcpriv = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 kmem_cache_free(uhci_up_cachep, urbp);
442}
443
444static void uhci_inc_fsbr(struct uhci_hcd *uhci, struct urb *urb)
445{
446 struct urb_priv *urbp = (struct urb_priv *)urb->hcpriv;
447
448 if ((!(urb->transfer_flags & URB_NO_FSBR)) && !urbp->fsbr) {
449 urbp->fsbr = 1;
450 if (!uhci->fsbr++ && !uhci->fsbrtimeout)
451 uhci->skel_term_qh->link = cpu_to_le32(uhci->skel_fs_control_qh->dma_handle) | UHCI_PTR_QH;
452 }
453}
454
455static void uhci_dec_fsbr(struct uhci_hcd *uhci, struct urb *urb)
456{
457 struct urb_priv *urbp = (struct urb_priv *)urb->hcpriv;
458
459 if ((!(urb->transfer_flags & URB_NO_FSBR)) && urbp->fsbr) {
460 urbp->fsbr = 0;
461 if (!--uhci->fsbr)
462 uhci->fsbrtimeout = jiffies + FSBR_DELAY;
463 }
464}
465
466/*
467 * Map status to standard result codes
468 *
469 * <status> is (td_status(td) & 0xF60000), a.k.a.
470 * uhci_status_bits(td_status(td)).
471 * Note: <status> does not include the TD_CTRL_NAK bit.
472 * <dir_out> is True for output TDs and False for input TDs.
473 */
474static int uhci_map_status(int status, int dir_out)
475{
476 if (!status)
477 return 0;
478 if (status & TD_CTRL_BITSTUFF) /* Bitstuff error */
479 return -EPROTO;
480 if (status & TD_CTRL_CRCTIMEO) { /* CRC/Timeout */
481 if (dir_out)
482 return -EPROTO;
483 else
484 return -EILSEQ;
485 }
486 if (status & TD_CTRL_BABBLE) /* Babble */
487 return -EOVERFLOW;
488 if (status & TD_CTRL_DBUFERR) /* Buffer error */
489 return -ENOSR;
490 if (status & TD_CTRL_STALLED) /* Stalled */
491 return -EPIPE;
492 WARN_ON(status & TD_CTRL_ACTIVE); /* Active */
493 return 0;
494}
495
496/*
497 * Control transfers
498 */
Alan Sterndccf4a42005-12-17 17:58:46 -0500499static int uhci_submit_control(struct uhci_hcd *uhci, struct urb *urb,
500 struct uhci_qh *qh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 struct uhci_td *td;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 unsigned long destination, status;
Alan Sterndccf4a42005-12-17 17:58:46 -0500504 int maxsze = le16_to_cpu(qh->hep->desc.wMaxPacketSize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 int len = urb->transfer_buffer_length;
506 dma_addr_t data = urb->transfer_dma;
Alan Sterndccf4a42005-12-17 17:58:46 -0500507 __le32 *plink;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508
509 /* The "pipe" thing contains the destination in bits 8--18 */
510 destination = (urb->pipe & PIPE_DEVEP_MASK) | USB_PID_SETUP;
511
Alan Sternaf0bb592005-12-17 18:00:12 -0500512 /* 3 errors, dummy TD remains inactive */
513 status = uhci_maxerr(3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 if (urb->dev->speed == USB_SPEED_LOW)
515 status |= TD_CTRL_LS;
516
517 /*
518 * Build the TD for the control request setup packet
519 */
Alan Sternaf0bb592005-12-17 18:00:12 -0500520 td = qh->dummy_td;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 uhci_add_td_to_urb(urb, td);
Alan Sternfa346562005-11-30 11:57:51 -0500522 uhci_fill_td(td, status, destination | uhci_explen(8),
Alan Sterndccf4a42005-12-17 17:58:46 -0500523 urb->setup_dma);
524 plink = &td->link;
Alan Sternaf0bb592005-12-17 18:00:12 -0500525 status |= TD_CTRL_ACTIVE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526
527 /*
528 * If direction is "send", change the packet ID from SETUP (0x2D)
529 * to OUT (0xE1). Else change it from SETUP to IN (0x69) and
530 * set Short Packet Detect (SPD) for all data packets.
531 */
532 if (usb_pipeout(urb->pipe))
533 destination ^= (USB_PID_SETUP ^ USB_PID_OUT);
534 else {
535 destination ^= (USB_PID_SETUP ^ USB_PID_IN);
536 status |= TD_CTRL_SPD;
537 }
538
539 /*
Alan Stern687f5f32005-11-30 17:16:19 -0500540 * Build the DATA TDs
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 */
542 while (len > 0) {
Alan Sterndccf4a42005-12-17 17:58:46 -0500543 int pktsze = min(len, maxsze);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544
Alan Stern25321782005-04-25 11:14:31 -0400545 td = uhci_alloc_td(uhci);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 if (!td)
Alan Sternaf0bb592005-12-17 18:00:12 -0500547 goto nomem;
Alan Sterndccf4a42005-12-17 17:58:46 -0500548 *plink = cpu_to_le32(td->dma_handle);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549
550 /* Alternate Data0/1 (start with Data1) */
551 destination ^= TD_TOKEN_TOGGLE;
552
553 uhci_add_td_to_urb(urb, td);
Alan Sternfa346562005-11-30 11:57:51 -0500554 uhci_fill_td(td, status, destination | uhci_explen(pktsze),
Alan Sterndccf4a42005-12-17 17:58:46 -0500555 data);
556 plink = &td->link;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557
558 data += pktsze;
559 len -= pktsze;
560 }
561
562 /*
563 * Build the final TD for control status
564 */
Alan Stern25321782005-04-25 11:14:31 -0400565 td = uhci_alloc_td(uhci);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 if (!td)
Alan Sternaf0bb592005-12-17 18:00:12 -0500567 goto nomem;
Alan Sterndccf4a42005-12-17 17:58:46 -0500568 *plink = cpu_to_le32(td->dma_handle);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569
570 /*
571 * It's IN if the pipe is an output pipe or we're not expecting
572 * data back.
573 */
574 destination &= ~TD_TOKEN_PID_MASK;
575 if (usb_pipeout(urb->pipe) || !urb->transfer_buffer_length)
576 destination |= USB_PID_IN;
577 else
578 destination |= USB_PID_OUT;
579
580 destination |= TD_TOKEN_TOGGLE; /* End in Data1 */
581
582 status &= ~TD_CTRL_SPD;
583
584 uhci_add_td_to_urb(urb, td);
585 uhci_fill_td(td, status | TD_CTRL_IOC,
Alan Sterndccf4a42005-12-17 17:58:46 -0500586 destination | uhci_explen(0), 0);
Alan Sternaf0bb592005-12-17 18:00:12 -0500587 plink = &td->link;
588
589 /*
590 * Build the new dummy TD and activate the old one
591 */
592 td = uhci_alloc_td(uhci);
593 if (!td)
594 goto nomem;
595 *plink = cpu_to_le32(td->dma_handle);
596
597 uhci_fill_td(td, 0, USB_PID_OUT | uhci_explen(0), 0);
598 wmb();
599 qh->dummy_td->status |= __constant_cpu_to_le32(TD_CTRL_ACTIVE);
600 qh->dummy_td = td;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601
602 /* Low-speed transfers get a different queue, and won't hog the bus.
603 * Also, some devices enumerate better without FSBR; the easiest way
604 * to do that is to put URBs on the low-speed queue while the device
Alan Stern630aa3c2006-01-23 17:17:21 -0500605 * isn't in the CONFIGURED state. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 if (urb->dev->speed == USB_SPEED_LOW ||
Alan Stern630aa3c2006-01-23 17:17:21 -0500607 urb->dev->state != USB_STATE_CONFIGURED)
Alan Sterndccf4a42005-12-17 17:58:46 -0500608 qh->skel = uhci->skel_ls_control_qh;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 else {
Alan Sterndccf4a42005-12-17 17:58:46 -0500610 qh->skel = uhci->skel_fs_control_qh;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 uhci_inc_fsbr(uhci, urb);
612 }
Alan Sterndccf4a42005-12-17 17:58:46 -0500613 return 0;
Alan Sternaf0bb592005-12-17 18:00:12 -0500614
615nomem:
616 /* Remove the dummy TD from the td_list so it doesn't get freed */
617 uhci_remove_td_from_urb(qh->dummy_td);
618 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619}
620
621/*
622 * If control-IN transfer was short, the status packet wasn't sent.
623 * This routine changes the element pointer in the QH to point at the
624 * status TD. It's safe to do this even while the QH is live, because
625 * the hardware only updates the element pointer following a successful
626 * transfer. The inactive TD for the short packet won't cause an update,
627 * so the pointer won't get overwritten. The next time the controller
628 * sees this QH, it will send the status packet.
629 */
630static int usb_control_retrigger_status(struct uhci_hcd *uhci, struct urb *urb)
631{
632 struct urb_priv *urbp = (struct urb_priv *)urb->hcpriv;
633 struct uhci_td *td;
634
Alan Sterndccf4a42005-12-17 17:58:46 -0500635 urbp->short_transfer = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636
637 td = list_entry(urbp->td_list.prev, struct uhci_td, list);
638 urbp->qh->element = cpu_to_le32(td->dma_handle);
639
640 return -EINPROGRESS;
641}
642
643
644static int uhci_result_control(struct uhci_hcd *uhci, struct urb *urb)
645{
646 struct list_head *tmp, *head;
647 struct urb_priv *urbp = urb->hcpriv;
648 struct uhci_td *td;
649 unsigned int status;
650 int ret = 0;
651
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 head = &urbp->td_list;
Alan Sterndccf4a42005-12-17 17:58:46 -0500653 if (urbp->short_transfer) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 tmp = head->prev;
655 goto status_stage;
656 }
657
Alan Sterndccf4a42005-12-17 17:58:46 -0500658 urb->actual_length = 0;
659
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 tmp = head->next;
661 td = list_entry(tmp, struct uhci_td, list);
662
663 /* The first TD is the SETUP stage, check the status, but skip */
664 /* the count */
665 status = uhci_status_bits(td_status(td));
666 if (status & TD_CTRL_ACTIVE)
667 return -EINPROGRESS;
668
669 if (status)
670 goto td_error;
671
Alan Stern687f5f32005-11-30 17:16:19 -0500672 /* The rest of the TDs (but the last) are data */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 tmp = tmp->next;
674 while (tmp != head && tmp->next != head) {
675 unsigned int ctrlstat;
676
677 td = list_entry(tmp, struct uhci_td, list);
678 tmp = tmp->next;
679
680 ctrlstat = td_status(td);
681 status = uhci_status_bits(ctrlstat);
682 if (status & TD_CTRL_ACTIVE)
683 return -EINPROGRESS;
684
685 urb->actual_length += uhci_actual_length(ctrlstat);
686
687 if (status)
688 goto td_error;
689
690 /* Check to see if we received a short packet */
691 if (uhci_actual_length(ctrlstat) <
692 uhci_expected_length(td_token(td))) {
693 if (urb->transfer_flags & URB_SHORT_NOT_OK) {
694 ret = -EREMOTEIO;
695 goto err;
696 }
697
Alan Sterndccf4a42005-12-17 17:58:46 -0500698 return usb_control_retrigger_status(uhci, urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 }
700 }
701
702status_stage:
703 td = list_entry(tmp, struct uhci_td, list);
704
705 /* Control status stage */
706 status = td_status(td);
707
708#ifdef I_HAVE_BUGGY_APC_BACKUPS
709 /* APC BackUPS Pro kludge */
710 /* It tries to send all of the descriptor instead of the amount */
711 /* we requested */
712 if (status & TD_CTRL_IOC && /* IOC is masked out by uhci_status_bits */
713 status & TD_CTRL_ACTIVE &&
714 status & TD_CTRL_NAK)
715 return 0;
716#endif
717
718 status = uhci_status_bits(status);
719 if (status & TD_CTRL_ACTIVE)
720 return -EINPROGRESS;
721
722 if (status)
723 goto td_error;
724
725 return 0;
726
727td_error:
728 ret = uhci_map_status(status, uhci_packetout(td_token(td)));
729
730err:
731 if ((debug == 1 && ret != -EPIPE) || debug > 1) {
732 /* Some debugging code */
733 dev_dbg(uhci_dev(uhci), "%s: failed with status %x\n",
734 __FUNCTION__, status);
735
736 if (errbuf) {
737 /* Print the chain for debugging purposes */
738 uhci_show_qh(urbp->qh, errbuf, ERRBUF_LEN, 0);
739
740 lprintk(errbuf);
741 }
742 }
743
Alan Stern0ed8fee2005-12-17 18:02:38 -0500744 /* Note that the queue has stopped */
745 urbp->qh->element = UHCI_PTR_TERM;
746 urbp->qh->is_stopped = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 return ret;
748}
749
750/*
751 * Common submit for bulk and interrupt
752 */
Alan Sterndccf4a42005-12-17 17:58:46 -0500753static int uhci_submit_common(struct uhci_hcd *uhci, struct urb *urb,
754 struct uhci_qh *qh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755{
756 struct uhci_td *td;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 unsigned long destination, status;
Alan Sterndccf4a42005-12-17 17:58:46 -0500758 int maxsze = le16_to_cpu(qh->hep->desc.wMaxPacketSize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 int len = urb->transfer_buffer_length;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 dma_addr_t data = urb->transfer_dma;
Alan Sternaf0bb592005-12-17 18:00:12 -0500761 __le32 *plink;
762 unsigned int toggle;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763
764 if (len < 0)
765 return -EINVAL;
766
767 /* The "pipe" thing contains the destination in bits 8--18 */
768 destination = (urb->pipe & PIPE_DEVEP_MASK) | usb_packetid(urb->pipe);
Alan Sternaf0bb592005-12-17 18:00:12 -0500769 toggle = usb_gettoggle(urb->dev, usb_pipeendpoint(urb->pipe),
770 usb_pipeout(urb->pipe));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771
Alan Sternaf0bb592005-12-17 18:00:12 -0500772 /* 3 errors, dummy TD remains inactive */
773 status = uhci_maxerr(3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 if (urb->dev->speed == USB_SPEED_LOW)
775 status |= TD_CTRL_LS;
776 if (usb_pipein(urb->pipe))
777 status |= TD_CTRL_SPD;
778
779 /*
Alan Stern687f5f32005-11-30 17:16:19 -0500780 * Build the DATA TDs
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 */
Alan Sternaf0bb592005-12-17 18:00:12 -0500782 plink = NULL;
783 td = qh->dummy_td;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 do { /* Allow zero length packets */
785 int pktsze = maxsze;
786
Alan Sterndccf4a42005-12-17 17:58:46 -0500787 if (len <= pktsze) { /* The last packet */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 pktsze = len;
789 if (!(urb->transfer_flags & URB_SHORT_NOT_OK))
790 status &= ~TD_CTRL_SPD;
791 }
792
Alan Sternaf0bb592005-12-17 18:00:12 -0500793 if (plink) {
794 td = uhci_alloc_td(uhci);
795 if (!td)
796 goto nomem;
797 *plink = cpu_to_le32(td->dma_handle);
798 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 uhci_add_td_to_urb(urb, td);
Alan Sterndccf4a42005-12-17 17:58:46 -0500800 uhci_fill_td(td, status,
Alan Sternaf0bb592005-12-17 18:00:12 -0500801 destination | uhci_explen(pktsze) |
802 (toggle << TD_TOKEN_TOGGLE_SHIFT),
803 data);
Alan Sterndccf4a42005-12-17 17:58:46 -0500804 plink = &td->link;
Alan Sternaf0bb592005-12-17 18:00:12 -0500805 status |= TD_CTRL_ACTIVE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806
807 data += pktsze;
808 len -= maxsze;
Alan Sternaf0bb592005-12-17 18:00:12 -0500809 toggle ^= 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 } while (len > 0);
811
812 /*
813 * URB_ZERO_PACKET means adding a 0-length packet, if direction
814 * is OUT and the transfer_length was an exact multiple of maxsze,
815 * hence (len = transfer_length - N * maxsze) == 0
816 * however, if transfer_length == 0, the zero packet was already
817 * prepared above.
818 */
Alan Sterndccf4a42005-12-17 17:58:46 -0500819 if ((urb->transfer_flags & URB_ZERO_PACKET) &&
820 usb_pipeout(urb->pipe) && len == 0 &&
821 urb->transfer_buffer_length > 0) {
Alan Stern25321782005-04-25 11:14:31 -0400822 td = uhci_alloc_td(uhci);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 if (!td)
Alan Sternaf0bb592005-12-17 18:00:12 -0500824 goto nomem;
Alan Sterndccf4a42005-12-17 17:58:46 -0500825 *plink = cpu_to_le32(td->dma_handle);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826
827 uhci_add_td_to_urb(urb, td);
Alan Sternaf0bb592005-12-17 18:00:12 -0500828 uhci_fill_td(td, status,
829 destination | uhci_explen(0) |
830 (toggle << TD_TOKEN_TOGGLE_SHIFT),
831 data);
832 plink = &td->link;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833
Alan Sternaf0bb592005-12-17 18:00:12 -0500834 toggle ^= 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 }
836
837 /* Set the interrupt-on-completion flag on the last packet.
838 * A more-or-less typical 4 KB URB (= size of one memory page)
839 * will require about 3 ms to transfer; that's a little on the
840 * fast side but not enough to justify delaying an interrupt
841 * more than 2 or 3 URBs, so we will ignore the URB_NO_INTERRUPT
842 * flag setting. */
Alan Sterndccf4a42005-12-17 17:58:46 -0500843 td->status |= __constant_cpu_to_le32(TD_CTRL_IOC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844
Alan Sternaf0bb592005-12-17 18:00:12 -0500845 /*
846 * Build the new dummy TD and activate the old one
847 */
848 td = uhci_alloc_td(uhci);
849 if (!td)
850 goto nomem;
851 *plink = cpu_to_le32(td->dma_handle);
852
853 uhci_fill_td(td, 0, USB_PID_OUT | uhci_explen(0), 0);
854 wmb();
855 qh->dummy_td->status |= __constant_cpu_to_le32(TD_CTRL_ACTIVE);
856 qh->dummy_td = td;
857
858 usb_settoggle(urb->dev, usb_pipeendpoint(urb->pipe),
859 usb_pipeout(urb->pipe), toggle);
Alan Sterndccf4a42005-12-17 17:58:46 -0500860 return 0;
Alan Sternaf0bb592005-12-17 18:00:12 -0500861
862nomem:
863 /* Remove the dummy TD from the td_list so it doesn't get freed */
864 uhci_remove_td_from_urb(qh->dummy_td);
865 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866}
867
868/*
869 * Common result for bulk and interrupt
870 */
871static int uhci_result_common(struct uhci_hcd *uhci, struct urb *urb)
872{
873 struct urb_priv *urbp = urb->hcpriv;
874 struct uhci_td *td;
875 unsigned int status = 0;
876 int ret = 0;
877
878 urb->actual_length = 0;
879
880 list_for_each_entry(td, &urbp->td_list, list) {
881 unsigned int ctrlstat = td_status(td);
882
883 status = uhci_status_bits(ctrlstat);
884 if (status & TD_CTRL_ACTIVE)
885 return -EINPROGRESS;
886
887 urb->actual_length += uhci_actual_length(ctrlstat);
888
889 if (status)
890 goto td_error;
891
892 if (uhci_actual_length(ctrlstat) <
893 uhci_expected_length(td_token(td))) {
894 if (urb->transfer_flags & URB_SHORT_NOT_OK) {
895 ret = -EREMOTEIO;
896 goto err;
Alan Sterndccf4a42005-12-17 17:58:46 -0500897 }
898
899 /*
900 * This URB stopped short of its end. We have to
901 * fix up the toggles of the following URBs on the
902 * queue and restart the queue.
903 *
904 * Do this only the first time we encounter the
905 * short URB.
906 */
907 if (!urbp->short_transfer) {
908 urbp->short_transfer = 1;
Alan Stern0ed8fee2005-12-17 18:02:38 -0500909 urbp->qh->initial_toggle =
910 uhci_toggle(td_token(td)) ^ 1;
911 uhci_fixup_toggles(urbp->qh, 1);
912
Alan Sterndccf4a42005-12-17 17:58:46 -0500913 td = list_entry(urbp->td_list.prev,
914 struct uhci_td, list);
915 urbp->qh->element = td->link;
916 }
917 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918 }
919 }
920
921 return 0;
922
923td_error:
924 ret = uhci_map_status(status, uhci_packetout(td_token(td)));
925
926err:
927 /*
928 * Enable this chunk of code if you want to see some more debugging.
929 * But be careful, it has the tendancy to starve out khubd and prevent
930 * disconnects from happening successfully if you have a slow debug
931 * log interface (like a serial console.
932 */
933#if 0
934 if ((debug == 1 && ret != -EPIPE) || debug > 1) {
935 /* Some debugging code */
936 dev_dbg(uhci_dev(uhci), "%s: failed with status %x\n",
937 __FUNCTION__, status);
938
939 if (errbuf) {
940 /* Print the chain for debugging purposes */
941 uhci_show_qh(urbp->qh, errbuf, ERRBUF_LEN, 0);
942
943 lprintk(errbuf);
944 }
945 }
946#endif
Alan Stern0ed8fee2005-12-17 18:02:38 -0500947
948 /* Note that the queue has stopped and save the next toggle value */
949 urbp->qh->element = UHCI_PTR_TERM;
950 urbp->qh->is_stopped = 1;
951 urbp->qh->needs_fixup = 1;
952 urbp->qh->initial_toggle = uhci_toggle(td_token(td)) ^
953 (ret == -EREMOTEIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954 return ret;
955}
956
Alan Sterndccf4a42005-12-17 17:58:46 -0500957static inline int uhci_submit_bulk(struct uhci_hcd *uhci, struct urb *urb,
958 struct uhci_qh *qh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959{
960 int ret;
961
962 /* Can't have low-speed bulk transfers */
963 if (urb->dev->speed == USB_SPEED_LOW)
964 return -EINVAL;
965
Alan Sterndccf4a42005-12-17 17:58:46 -0500966 qh->skel = uhci->skel_bulk_qh;
967 ret = uhci_submit_common(uhci, urb, qh);
968 if (ret == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 uhci_inc_fsbr(uhci, urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 return ret;
971}
972
Alan Sterndccf4a42005-12-17 17:58:46 -0500973static inline int uhci_submit_interrupt(struct uhci_hcd *uhci, struct urb *urb,
974 struct uhci_qh *qh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975{
Alan Sterndccf4a42005-12-17 17:58:46 -0500976 /* USB 1.1 interrupt transfers only involve one packet per interval.
977 * Drivers can submit URBs of any length, but longer ones will need
978 * multiple intervals to complete.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979 */
Alan Sterndccf4a42005-12-17 17:58:46 -0500980 qh->skel = uhci->skelqh[__interval_to_skel(urb->interval)];
981 return uhci_submit_common(uhci, urb, qh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982}
983
984/*
985 * 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 Stern0ed8fee2005-12-17 18:02:38 -0500995 if (urb->number_of_packets > 900) /* 900? Why? */
996 return -EFBIG;
997
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998 status = TD_CTRL_ACTIVE | TD_CTRL_IOS;
999 destination = (urb->pipe & PIPE_DEVEP_MASK) | usb_packetid(urb->pipe);
1000
Alan Stern0ed8fee2005-12-17 18:02:38 -05001001 /* Figure out the starting frame number */
1002 if (urb->transfer_flags & URB_ISO_ASAP) {
1003 if (list_empty(&qh->queue)) {
1004 uhci_get_current_frame_number(uhci);
1005 urb->start_frame = (uhci->frame_number + 10);
1006
1007 } else { /* Go right after the last one */
1008 struct urb *last_urb;
1009
1010 last_urb = list_entry(qh->queue.prev,
1011 struct urb_priv, node)->urb;
1012 urb->start_frame = (last_urb->start_frame +
1013 last_urb->number_of_packets *
1014 last_urb->interval);
1015 }
1016 } else {
1017 /* FIXME: Sanity check */
1018 }
1019 urb->start_frame &= (UHCI_NUMFRAMES - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020
Alan Sternb81d3432005-10-13 17:00:24 -04001021 for (i = 0; i < urb->number_of_packets; i++) {
Alan Stern25321782005-04-25 11:14:31 -04001022 td = uhci_alloc_td(uhci);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023 if (!td)
1024 return -ENOMEM;
1025
1026 uhci_add_td_to_urb(urb, td);
Alan Sterndccf4a42005-12-17 17:58:46 -05001027 uhci_fill_td(td, status, destination |
1028 uhci_explen(urb->iso_frame_desc[i].length),
1029 urb->transfer_dma +
1030 urb->iso_frame_desc[i].offset);
Alan Sternb81d3432005-10-13 17:00:24 -04001031 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032
Alan Sterndccf4a42005-12-17 17:58:46 -05001033 /* Set the interrupt-on-completion flag on the last packet. */
1034 td->status |= __constant_cpu_to_le32(TD_CTRL_IOC);
1035
1036 qh->skel = uhci->skel_iso_qh;
1037
1038 /* Add the TDs to the frame list */
Alan Sternb81d3432005-10-13 17:00:24 -04001039 frame = urb->start_frame;
1040 list_for_each_entry(td, &urbp->td_list, list) {
Alan Sterndccf4a42005-12-17 17:58:46 -05001041 uhci_insert_td_in_frame_list(uhci, td, frame);
Alan Sternb81d3432005-10-13 17:00:24 -04001042 frame += urb->interval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 }
1044
Alan Sterndccf4a42005-12-17 17:58:46 -05001045 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046}
1047
1048static int uhci_result_isochronous(struct uhci_hcd *uhci, struct urb *urb)
1049{
1050 struct uhci_td *td;
1051 struct urb_priv *urbp = (struct urb_priv *)urb->hcpriv;
1052 int status;
1053 int i, ret = 0;
1054
Alan Sternb81d3432005-10-13 17:00:24 -04001055 urb->actual_length = urb->error_count = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056
1057 i = 0;
1058 list_for_each_entry(td, &urbp->td_list, list) {
1059 int actlength;
1060 unsigned int ctrlstat = td_status(td);
1061
1062 if (ctrlstat & TD_CTRL_ACTIVE)
1063 return -EINPROGRESS;
1064
1065 actlength = uhci_actual_length(ctrlstat);
1066 urb->iso_frame_desc[i].actual_length = actlength;
1067 urb->actual_length += actlength;
1068
1069 status = uhci_map_status(uhci_status_bits(ctrlstat),
1070 usb_pipeout(urb->pipe));
1071 urb->iso_frame_desc[i].status = status;
1072 if (status) {
1073 urb->error_count++;
1074 ret = status;
1075 }
1076
1077 i++;
1078 }
1079
1080 return ret;
1081}
1082
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083static int uhci_urb_enqueue(struct usb_hcd *hcd,
Alan Sterndccf4a42005-12-17 17:58:46 -05001084 struct usb_host_endpoint *hep,
Al Viro55016f12005-10-21 03:21:58 -04001085 struct urb *urb, gfp_t mem_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086{
1087 int ret;
1088 struct uhci_hcd *uhci = hcd_to_uhci(hcd);
1089 unsigned long flags;
Alan Sterndccf4a42005-12-17 17:58:46 -05001090 struct urb_priv *urbp;
1091 struct uhci_qh *qh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092 int bustime;
1093
1094 spin_lock_irqsave(&uhci->lock, flags);
1095
1096 ret = urb->status;
1097 if (ret != -EINPROGRESS) /* URB already unlinked! */
Alan Sterndccf4a42005-12-17 17:58:46 -05001098 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099
Alan Sterndccf4a42005-12-17 17:58:46 -05001100 ret = -ENOMEM;
1101 urbp = uhci_alloc_urb_priv(uhci, urb);
1102 if (!urbp)
1103 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104
Alan Sterndccf4a42005-12-17 17:58:46 -05001105 if (hep->hcpriv)
1106 qh = (struct uhci_qh *) hep->hcpriv;
1107 else {
1108 qh = uhci_alloc_qh(uhci, urb->dev, hep);
1109 if (!qh)
1110 goto err_no_qh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111 }
Alan Sterndccf4a42005-12-17 17:58:46 -05001112 urbp->qh = qh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113
1114 switch (usb_pipetype(urb->pipe)) {
1115 case PIPE_CONTROL:
Alan Sterndccf4a42005-12-17 17:58:46 -05001116 ret = uhci_submit_control(uhci, urb, qh);
1117 break;
1118 case PIPE_BULK:
1119 ret = uhci_submit_bulk(uhci, urb, qh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120 break;
1121 case PIPE_INTERRUPT:
Alan Sterndccf4a42005-12-17 17:58:46 -05001122 if (list_empty(&qh->queue)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123 bustime = usb_check_bandwidth(urb->dev, urb);
1124 if (bustime < 0)
1125 ret = bustime;
1126 else {
Alan Sterndccf4a42005-12-17 17:58:46 -05001127 ret = uhci_submit_interrupt(uhci, urb, qh);
1128 if (ret == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129 usb_claim_bandwidth(urb->dev, urb, bustime, 0);
1130 }
1131 } else { /* inherit from parent */
Alan Sterndccf4a42005-12-17 17:58:46 -05001132 struct urb_priv *eurbp;
1133
1134 eurbp = list_entry(qh->queue.prev, struct urb_priv,
1135 node);
1136 urb->bandwidth = eurbp->urb->bandwidth;
1137 ret = uhci_submit_interrupt(uhci, urb, qh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138 }
1139 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140 case PIPE_ISOCHRONOUS:
1141 bustime = usb_check_bandwidth(urb->dev, urb);
1142 if (bustime < 0) {
1143 ret = bustime;
1144 break;
1145 }
1146
Alan Sterndccf4a42005-12-17 17:58:46 -05001147 ret = uhci_submit_isochronous(uhci, urb, qh);
1148 if (ret == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149 usb_claim_bandwidth(urb->dev, urb, bustime, 1);
1150 break;
1151 }
Alan Sterndccf4a42005-12-17 17:58:46 -05001152 if (ret != 0)
1153 goto err_submit_failed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154
Alan Sterndccf4a42005-12-17 17:58:46 -05001155 /* Add this URB to the QH */
1156 urbp->qh = qh;
1157 list_add_tail(&urbp->node, &qh->queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158
Alan Sterndccf4a42005-12-17 17:58:46 -05001159 /* If the new URB is the first and only one on this QH then either
1160 * the QH is new and idle or else it's unlinked and waiting to
1161 * become idle, so we can activate it right away. */
1162 if (qh->queue.next == &urbp->node)
1163 uhci_activate_qh(uhci, qh);
Alan Sterndccf4a42005-12-17 17:58:46 -05001164 goto done;
1165
1166err_submit_failed:
1167 if (qh->state == QH_STATE_IDLE)
1168 uhci_make_qh_idle(uhci, qh); /* Reclaim unused QH */
1169
1170err_no_qh:
1171 uhci_free_urb_priv(uhci, urbp);
1172
1173done:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174 spin_unlock_irqrestore(&uhci->lock, flags);
1175 return ret;
1176}
1177
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178static int uhci_urb_dequeue(struct usb_hcd *hcd, struct urb *urb)
1179{
1180 struct uhci_hcd *uhci = hcd_to_uhci(hcd);
1181 unsigned long flags;
1182 struct urb_priv *urbp;
1183
1184 spin_lock_irqsave(&uhci->lock, flags);
1185 urbp = urb->hcpriv;
1186 if (!urbp) /* URB was never linked! */
1187 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188
Alan Sterndccf4a42005-12-17 17:58:46 -05001189 /* Remove Isochronous TDs from the frame list ASAP */
Alan Sternb81d3432005-10-13 17:00:24 -04001190 if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS)
Alan Sterndccf4a42005-12-17 17:58:46 -05001191 uhci_unlink_isochronous_tds(uhci, urb);
1192 uhci_unlink_qh(uhci, urbp->qh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193
1194done:
1195 spin_unlock_irqrestore(&uhci->lock, flags);
1196 return 0;
1197}
1198
Alan Stern0ed8fee2005-12-17 18:02:38 -05001199/*
1200 * Finish unlinking an URB and give it back
1201 */
1202static void uhci_giveback_urb(struct uhci_hcd *uhci, struct uhci_qh *qh,
1203 struct urb *urb, struct pt_regs *regs)
1204__releases(uhci->lock)
1205__acquires(uhci->lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206{
Alan Stern0ed8fee2005-12-17 18:02:38 -05001207 struct urb_priv *urbp = (struct urb_priv *) urb->hcpriv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208
Alan Stern0ed8fee2005-12-17 18:02:38 -05001209 /* Isochronous TDs get unlinked directly from the frame list */
1210 if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS)
1211 uhci_unlink_isochronous_tds(uhci, urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212
Alan Stern0ed8fee2005-12-17 18:02:38 -05001213 /* If the URB isn't first on its queue, adjust the link pointer
1214 * of the last TD in the previous URB. */
1215 else if (qh->queue.next != &urbp->node) {
1216 struct urb_priv *purbp;
1217 struct uhci_td *ptd, *ltd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218
Alan Stern0ed8fee2005-12-17 18:02:38 -05001219 purbp = list_entry(urbp->node.prev, struct urb_priv, node);
1220 ptd = list_entry(purbp->td_list.prev, struct uhci_td,
1221 list);
1222 ltd = list_entry(urbp->td_list.prev, struct uhci_td,
1223 list);
1224 ptd->link = ltd->link;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225 }
1226
Alan Stern0ed8fee2005-12-17 18:02:38 -05001227 /* Take the URB off the QH's queue. If the queue is now empty,
1228 * this is a perfect time for a toggle fixup. */
1229 list_del_init(&urbp->node);
1230 if (list_empty(&qh->queue) && qh->needs_fixup) {
1231 usb_settoggle(urb->dev, usb_pipeendpoint(urb->pipe),
1232 usb_pipeout(urb->pipe), qh->initial_toggle);
1233 qh->needs_fixup = 0;
1234 }
1235
1236 uhci_dec_fsbr(uhci, urb); /* Safe since it checks */
1237 uhci_free_urb_priv(uhci, urbp);
1238
1239 switch (usb_pipetype(urb->pipe)) {
1240 case PIPE_ISOCHRONOUS:
1241 /* Release bandwidth for Interrupt or Isoc. transfers */
1242 if (urb->bandwidth)
1243 usb_release_bandwidth(urb->dev, urb, 1);
1244 break;
1245 case PIPE_INTERRUPT:
1246 /* Release bandwidth for Interrupt or Isoc. transfers */
1247 /* Make sure we don't release if we have a queued URB */
1248 if (list_empty(&qh->queue) && urb->bandwidth)
1249 usb_release_bandwidth(urb->dev, urb, 0);
1250 else
1251 /* bandwidth was passed on to queued URB, */
1252 /* so don't let usb_unlink_urb() release it */
1253 urb->bandwidth = 0;
1254 break;
1255 }
1256
1257 spin_unlock(&uhci->lock);
1258 usb_hcd_giveback_urb(uhci_to_hcd(uhci), urb, regs);
1259 spin_lock(&uhci->lock);
1260
1261 /* If the queue is now empty, we can unlink the QH and give up its
1262 * reserved bandwidth. */
1263 if (list_empty(&qh->queue)) {
1264 uhci_unlink_qh(uhci, qh);
1265
1266 /* Bandwidth stuff not yet implemented */
1267 }
1268}
1269
1270/*
1271 * Scan the URBs in a QH's queue
1272 */
1273#define QH_FINISHED_UNLINKING(qh) \
1274 (qh->state == QH_STATE_UNLINKING && \
1275 uhci->frame_number + uhci->is_stopped != qh->unlink_frame)
1276
1277static void uhci_scan_qh(struct uhci_hcd *uhci, struct uhci_qh *qh,
1278 struct pt_regs *regs)
1279{
1280 struct urb_priv *urbp;
1281 struct urb *urb;
1282 int status;
1283
1284 while (!list_empty(&qh->queue)) {
1285 urbp = list_entry(qh->queue.next, struct urb_priv, node);
1286 urb = urbp->urb;
1287
1288 switch (usb_pipetype(urb->pipe)) {
1289 case PIPE_CONTROL:
1290 status = uhci_result_control(uhci, urb);
1291 break;
1292 case PIPE_ISOCHRONOUS:
1293 status = uhci_result_isochronous(uhci, urb);
1294 break;
1295 default: /* PIPE_BULK or PIPE_INTERRUPT */
1296 status = uhci_result_common(uhci, urb);
1297 break;
1298 }
1299 if (status == -EINPROGRESS)
1300 break;
1301
1302 spin_lock(&urb->lock);
1303 if (urb->status == -EINPROGRESS) /* Not dequeued */
1304 urb->status = status;
1305 else
1306 status = -ECONNRESET;
1307 spin_unlock(&urb->lock);
1308
1309 /* Dequeued but completed URBs can't be given back unless
1310 * the QH is stopped or has finished unlinking. */
1311 if (status == -ECONNRESET &&
1312 !(qh->is_stopped || QH_FINISHED_UNLINKING(qh)))
1313 return;
1314
1315 uhci_giveback_urb(uhci, qh, urb, regs);
1316 if (qh->is_stopped)
1317 break;
1318 }
1319
1320 /* If the QH is neither stopped nor finished unlinking (normal case),
1321 * our work here is done. */
1322 restart:
1323 if (!(qh->is_stopped || QH_FINISHED_UNLINKING(qh)))
1324 return;
1325
1326 /* Otherwise give back each of the dequeued URBs */
1327 list_for_each_entry(urbp, &qh->queue, node) {
1328 urb = urbp->urb;
1329 if (urb->status != -EINPROGRESS) {
1330 uhci_save_toggle(qh, urb);
1331 uhci_giveback_urb(uhci, qh, urb, regs);
1332 goto restart;
1333 }
1334 }
1335 qh->is_stopped = 0;
1336
1337 /* There are no more dequeued URBs. If there are still URBs on the
1338 * queue, the QH can now be re-activated. */
1339 if (!list_empty(&qh->queue)) {
1340 if (qh->needs_fixup)
1341 uhci_fixup_toggles(qh, 0);
1342 uhci_activate_qh(uhci, qh);
1343 }
1344
1345 /* The queue is empty. The QH can become idle if it is fully
1346 * unlinked. */
1347 else if (QH_FINISHED_UNLINKING(qh))
1348 uhci_make_qh_idle(uhci, qh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349}
1350
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351static void uhci_free_pending_tds(struct uhci_hcd *uhci)
1352{
1353 struct uhci_td *td, *tmp;
1354
1355 list_for_each_entry_safe(td, tmp, &uhci->td_remove_list, remove_list) {
1356 list_del_init(&td->remove_list);
1357
1358 uhci_free_td(uhci, td);
1359 }
1360}
1361
Alan Stern0ed8fee2005-12-17 18:02:38 -05001362/*
1363 * Process events in the schedule, but only in one thread at a time
1364 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365static void uhci_scan_schedule(struct uhci_hcd *uhci, struct pt_regs *regs)
1366{
Alan Stern0ed8fee2005-12-17 18:02:38 -05001367 int i;
1368 struct uhci_qh *qh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369
1370 /* Don't allow re-entrant calls */
1371 if (uhci->scan_in_progress) {
1372 uhci->need_rescan = 1;
1373 return;
1374 }
1375 uhci->scan_in_progress = 1;
1376 rescan:
1377 uhci->need_rescan = 0;
1378
Alan Stern6c1b4452005-04-21 16:04:58 -04001379 uhci_clear_next_interrupt(uhci);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380 uhci_get_current_frame_number(uhci);
1381
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382 if (uhci->frame_number + uhci->is_stopped != uhci->td_remove_age)
1383 uhci_free_pending_tds(uhci);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384
Alan Stern0ed8fee2005-12-17 18:02:38 -05001385 /* Go through all the QH queues and process the URBs in each one */
1386 for (i = 0; i < UHCI_NUM_SKELQH - 1; ++i) {
1387 uhci->next_qh = list_entry(uhci->skelqh[i]->node.next,
1388 struct uhci_qh, node);
1389 while ((qh = uhci->next_qh) != uhci->skelqh[i]) {
1390 uhci->next_qh = list_entry(qh->node.next,
1391 struct uhci_qh, node);
1392 uhci_scan_qh(uhci, qh, regs);
1393 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395
1396 if (uhci->need_rescan)
1397 goto rescan;
1398 uhci->scan_in_progress = 0;
1399
Alan Stern0ed8fee2005-12-17 18:02:38 -05001400 /* If the controller is stopped, we can finish these off right now */
1401 if (uhci->is_stopped)
1402 uhci_free_pending_tds(uhci);
Alan Sterndccf4a42005-12-17 17:58:46 -05001403
1404 if (list_empty(&uhci->td_remove_list) &&
1405 list_empty(&uhci->skel_unlink_qh->node))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406 uhci_clear_next_interrupt(uhci);
1407 else
1408 uhci_set_next_interrupt(uhci);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409}
Alan Sternf5946f82005-04-09 17:26:00 -04001410
1411static void check_fsbr(struct uhci_hcd *uhci)
1412{
Alan Stern0ed8fee2005-12-17 18:02:38 -05001413 /* For now, don't scan URBs for FSBR timeouts.
1414 * Add it back in later... */
Alan Sternf5946f82005-04-09 17:26:00 -04001415
1416 /* Really disable FSBR */
1417 if (!uhci->fsbr && uhci->fsbrtimeout && time_after_eq(jiffies, uhci->fsbrtimeout)) {
1418 uhci->fsbrtimeout = 0;
1419 uhci->skel_term_qh->link = UHCI_PTR_TERM;
1420 }
1421}