Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 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 Stern | b761d9d | 2006-05-12 11:41:59 -0400 | [diff] [blame^] | 16 | * (C) Copyright 2004-2006 Alan Stern, stern@rowland.harvard.edu |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 17 | */ |
| 18 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 19 | |
| 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 Stern | dccf4a4 | 2005-12-17 17:58:46 -0500 | [diff] [blame] | 28 | static void uhci_set_next_interrupt(struct uhci_hcd *uhci) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 29 | { |
Alan Stern | 6c1b445 | 2005-04-21 16:04:58 -0400 | [diff] [blame] | 30 | if (uhci->is_stopped) |
Alan Stern | 1f09df8 | 2005-09-05 13:59:51 -0400 | [diff] [blame] | 31 | mod_timer(&uhci_to_hcd(uhci)->rh_timer, jiffies); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 32 | uhci->term_td->status |= cpu_to_le32(TD_CTRL_IOC); |
| 33 | } |
| 34 | |
| 35 | static 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 Stern | 84afddd | 2006-05-12 11:35:45 -0400 | [diff] [blame] | 40 | |
| 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 | */ |
| 46 | static void uhci_fsbr_on(struct uhci_hcd *uhci) |
| 47 | { |
| 48 | uhci->fsbr_is_on = 1; |
| 49 | uhci->skel_term_qh->link = cpu_to_le32( |
| 50 | uhci->skel_fs_control_qh->dma_handle) | UHCI_PTR_QH; |
| 51 | } |
| 52 | |
| 53 | static void uhci_fsbr_off(struct uhci_hcd *uhci) |
| 54 | { |
| 55 | uhci->fsbr_is_on = 0; |
| 56 | uhci->skel_term_qh->link = UHCI_PTR_TERM; |
| 57 | } |
| 58 | |
| 59 | static void uhci_add_fsbr(struct uhci_hcd *uhci, struct urb *urb) |
| 60 | { |
| 61 | struct urb_priv *urbp = urb->hcpriv; |
| 62 | |
| 63 | if (!(urb->transfer_flags & URB_NO_FSBR)) |
| 64 | urbp->fsbr = 1; |
| 65 | } |
| 66 | |
| 67 | static void uhci_qh_wants_fsbr(struct uhci_hcd *uhci, struct uhci_qh *qh) |
| 68 | { |
| 69 | struct urb_priv *urbp = |
| 70 | list_entry(qh->queue.next, struct urb_priv, node); |
| 71 | |
| 72 | if (urbp->fsbr) { |
| 73 | uhci->fsbr_jiffies = jiffies; |
| 74 | if (!uhci->fsbr_is_on) |
| 75 | uhci_fsbr_on(uhci); |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | |
Alan Stern | 2532178 | 2005-04-25 11:14:31 -0400 | [diff] [blame] | 80 | static struct uhci_td *uhci_alloc_td(struct uhci_hcd *uhci) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 81 | { |
| 82 | dma_addr_t dma_handle; |
| 83 | struct uhci_td *td; |
| 84 | |
| 85 | td = dma_pool_alloc(uhci->td_pool, GFP_ATOMIC, &dma_handle); |
| 86 | if (!td) |
| 87 | return NULL; |
| 88 | |
| 89 | td->dma_handle = dma_handle; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 90 | td->frame = -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 91 | |
| 92 | INIT_LIST_HEAD(&td->list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 93 | INIT_LIST_HEAD(&td->fl_list); |
| 94 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 95 | return td; |
| 96 | } |
| 97 | |
Alan Stern | dccf4a4 | 2005-12-17 17:58:46 -0500 | [diff] [blame] | 98 | static void uhci_free_td(struct uhci_hcd *uhci, struct uhci_td *td) |
| 99 | { |
| 100 | if (!list_empty(&td->list)) |
| 101 | dev_warn(uhci_dev(uhci), "td %p still in list!\n", td); |
Alan Stern | dccf4a4 | 2005-12-17 17:58:46 -0500 | [diff] [blame] | 102 | if (!list_empty(&td->fl_list)) |
| 103 | dev_warn(uhci_dev(uhci), "td %p still in fl_list!\n", td); |
| 104 | |
| 105 | dma_pool_free(uhci->td_pool, td, td->dma_handle); |
| 106 | } |
| 107 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 108 | static inline void uhci_fill_td(struct uhci_td *td, u32 status, |
| 109 | u32 token, u32 buffer) |
| 110 | { |
| 111 | td->status = cpu_to_le32(status); |
| 112 | td->token = cpu_to_le32(token); |
| 113 | td->buffer = cpu_to_le32(buffer); |
| 114 | } |
| 115 | |
Alan Stern | 04538a2 | 2006-05-12 11:29:04 -0400 | [diff] [blame] | 116 | static void uhci_add_td_to_urbp(struct uhci_td *td, struct urb_priv *urbp) |
| 117 | { |
| 118 | list_add_tail(&td->list, &urbp->td_list); |
| 119 | } |
| 120 | |
| 121 | static void uhci_remove_td_from_urbp(struct uhci_td *td) |
| 122 | { |
| 123 | list_del_init(&td->list); |
| 124 | } |
| 125 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 126 | /* |
Alan Stern | 687f5f3 | 2005-11-30 17:16:19 -0500 | [diff] [blame] | 127 | * We insert Isochronous URBs directly into the frame list at the beginning |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 128 | */ |
Alan Stern | dccf4a4 | 2005-12-17 17:58:46 -0500 | [diff] [blame] | 129 | static inline void uhci_insert_td_in_frame_list(struct uhci_hcd *uhci, |
| 130 | struct uhci_td *td, unsigned framenum) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 131 | { |
| 132 | framenum &= (UHCI_NUMFRAMES - 1); |
| 133 | |
| 134 | td->frame = framenum; |
| 135 | |
| 136 | /* Is there a TD already mapped there? */ |
Alan Stern | a1d59ce | 2005-09-16 14:22:51 -0400 | [diff] [blame] | 137 | if (uhci->frame_cpu[framenum]) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 138 | struct uhci_td *ftd, *ltd; |
| 139 | |
Alan Stern | a1d59ce | 2005-09-16 14:22:51 -0400 | [diff] [blame] | 140 | ftd = uhci->frame_cpu[framenum]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 141 | ltd = list_entry(ftd->fl_list.prev, struct uhci_td, fl_list); |
| 142 | |
| 143 | list_add_tail(&td->fl_list, &ftd->fl_list); |
| 144 | |
| 145 | td->link = ltd->link; |
| 146 | wmb(); |
| 147 | ltd->link = cpu_to_le32(td->dma_handle); |
| 148 | } else { |
Alan Stern | a1d59ce | 2005-09-16 14:22:51 -0400 | [diff] [blame] | 149 | td->link = uhci->frame[framenum]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 150 | wmb(); |
Alan Stern | a1d59ce | 2005-09-16 14:22:51 -0400 | [diff] [blame] | 151 | uhci->frame[framenum] = cpu_to_le32(td->dma_handle); |
| 152 | uhci->frame_cpu[framenum] = td; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 153 | } |
| 154 | } |
| 155 | |
Alan Stern | dccf4a4 | 2005-12-17 17:58:46 -0500 | [diff] [blame] | 156 | static inline void uhci_remove_td_from_frame_list(struct uhci_hcd *uhci, |
Alan Stern | b81d343 | 2005-10-13 17:00:24 -0400 | [diff] [blame] | 157 | struct uhci_td *td) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 158 | { |
| 159 | /* If it's not inserted, don't remove it */ |
Alan Stern | b81d343 | 2005-10-13 17:00:24 -0400 | [diff] [blame] | 160 | if (td->frame == -1) { |
| 161 | WARN_ON(!list_empty(&td->fl_list)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 162 | return; |
Alan Stern | b81d343 | 2005-10-13 17:00:24 -0400 | [diff] [blame] | 163 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 164 | |
Alan Stern | b81d343 | 2005-10-13 17:00:24 -0400 | [diff] [blame] | 165 | if (uhci->frame_cpu[td->frame] == td) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 166 | if (list_empty(&td->fl_list)) { |
Alan Stern | a1d59ce | 2005-09-16 14:22:51 -0400 | [diff] [blame] | 167 | uhci->frame[td->frame] = td->link; |
| 168 | uhci->frame_cpu[td->frame] = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 169 | } else { |
| 170 | struct uhci_td *ntd; |
| 171 | |
| 172 | ntd = list_entry(td->fl_list.next, struct uhci_td, fl_list); |
Alan Stern | a1d59ce | 2005-09-16 14:22:51 -0400 | [diff] [blame] | 173 | uhci->frame[td->frame] = cpu_to_le32(ntd->dma_handle); |
| 174 | uhci->frame_cpu[td->frame] = ntd; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 175 | } |
| 176 | } else { |
| 177 | struct uhci_td *ptd; |
| 178 | |
| 179 | ptd = list_entry(td->fl_list.prev, struct uhci_td, fl_list); |
| 180 | ptd->link = td->link; |
| 181 | } |
| 182 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 183 | list_del_init(&td->fl_list); |
| 184 | td->frame = -1; |
| 185 | } |
| 186 | |
Alan Stern | dccf4a4 | 2005-12-17 17:58:46 -0500 | [diff] [blame] | 187 | /* |
| 188 | * Remove all the TDs for an Isochronous URB from the frame list |
| 189 | */ |
| 190 | static void uhci_unlink_isochronous_tds(struct uhci_hcd *uhci, struct urb *urb) |
Alan Stern | b81d343 | 2005-10-13 17:00:24 -0400 | [diff] [blame] | 191 | { |
| 192 | struct urb_priv *urbp = (struct urb_priv *) urb->hcpriv; |
| 193 | struct uhci_td *td; |
| 194 | |
| 195 | list_for_each_entry(td, &urbp->td_list, list) |
Alan Stern | dccf4a4 | 2005-12-17 17:58:46 -0500 | [diff] [blame] | 196 | uhci_remove_td_from_frame_list(uhci, td); |
Alan Stern | b81d343 | 2005-10-13 17:00:24 -0400 | [diff] [blame] | 197 | wmb(); |
| 198 | } |
| 199 | |
Alan Stern | dccf4a4 | 2005-12-17 17:58:46 -0500 | [diff] [blame] | 200 | static struct uhci_qh *uhci_alloc_qh(struct uhci_hcd *uhci, |
| 201 | struct usb_device *udev, struct usb_host_endpoint *hep) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 202 | { |
| 203 | dma_addr_t dma_handle; |
| 204 | struct uhci_qh *qh; |
| 205 | |
| 206 | qh = dma_pool_alloc(uhci->qh_pool, GFP_ATOMIC, &dma_handle); |
| 207 | if (!qh) |
| 208 | return NULL; |
| 209 | |
Alan Stern | 59e29ed | 2006-05-12 11:19:19 -0400 | [diff] [blame] | 210 | memset(qh, 0, sizeof(*qh)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 211 | qh->dma_handle = dma_handle; |
| 212 | |
| 213 | qh->element = UHCI_PTR_TERM; |
| 214 | qh->link = UHCI_PTR_TERM; |
| 215 | |
Alan Stern | dccf4a4 | 2005-12-17 17:58:46 -0500 | [diff] [blame] | 216 | INIT_LIST_HEAD(&qh->queue); |
| 217 | INIT_LIST_HEAD(&qh->node); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 218 | |
Alan Stern | dccf4a4 | 2005-12-17 17:58:46 -0500 | [diff] [blame] | 219 | if (udev) { /* Normal QH */ |
Alan Stern | af0bb59 | 2005-12-17 18:00:12 -0500 | [diff] [blame] | 220 | qh->dummy_td = uhci_alloc_td(uhci); |
| 221 | if (!qh->dummy_td) { |
| 222 | dma_pool_free(uhci->qh_pool, qh, dma_handle); |
| 223 | return NULL; |
| 224 | } |
Alan Stern | dccf4a4 | 2005-12-17 17:58:46 -0500 | [diff] [blame] | 225 | qh->state = QH_STATE_IDLE; |
| 226 | qh->hep = hep; |
| 227 | qh->udev = udev; |
| 228 | hep->hcpriv = qh; |
Alan Stern | 4de7d2c | 2006-05-05 16:26:58 -0400 | [diff] [blame] | 229 | qh->type = hep->desc.bmAttributes & USB_ENDPOINT_XFERTYPE_MASK; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 230 | |
Alan Stern | dccf4a4 | 2005-12-17 17:58:46 -0500 | [diff] [blame] | 231 | } else { /* Skeleton QH */ |
| 232 | qh->state = QH_STATE_ACTIVE; |
Alan Stern | 4de7d2c | 2006-05-05 16:26:58 -0400 | [diff] [blame] | 233 | qh->type = -1; |
Alan Stern | dccf4a4 | 2005-12-17 17:58:46 -0500 | [diff] [blame] | 234 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 235 | return qh; |
| 236 | } |
| 237 | |
| 238 | static void uhci_free_qh(struct uhci_hcd *uhci, struct uhci_qh *qh) |
| 239 | { |
Alan Stern | dccf4a4 | 2005-12-17 17:58:46 -0500 | [diff] [blame] | 240 | WARN_ON(qh->state != QH_STATE_IDLE && qh->udev); |
| 241 | if (!list_empty(&qh->queue)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 242 | dev_warn(uhci_dev(uhci), "qh %p list not empty!\n", qh); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 243 | |
Alan Stern | dccf4a4 | 2005-12-17 17:58:46 -0500 | [diff] [blame] | 244 | list_del(&qh->node); |
| 245 | if (qh->udev) { |
| 246 | qh->hep->hcpriv = NULL; |
Alan Stern | af0bb59 | 2005-12-17 18:00:12 -0500 | [diff] [blame] | 247 | uhci_free_td(uhci, qh->dummy_td); |
Alan Stern | dccf4a4 | 2005-12-17 17:58:46 -0500 | [diff] [blame] | 248 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 249 | dma_pool_free(uhci->qh_pool, qh, qh->dma_handle); |
| 250 | } |
| 251 | |
| 252 | /* |
Alan Stern | a0b458b | 2006-05-12 11:23:19 -0400 | [diff] [blame] | 253 | * When a queue is stopped and a dequeued URB is given back, adjust |
| 254 | * the previous TD link (if the URB isn't first on the queue) or |
| 255 | * save its toggle value (if it is first and is currently executing). |
Alan Stern | 0ed8fee | 2005-12-17 18:02:38 -0500 | [diff] [blame] | 256 | */ |
Alan Stern | a0b458b | 2006-05-12 11:23:19 -0400 | [diff] [blame] | 257 | static void uhci_cleanup_queue(struct uhci_qh *qh, |
| 258 | struct urb *urb) |
Alan Stern | 0ed8fee | 2005-12-17 18:02:38 -0500 | [diff] [blame] | 259 | { |
Alan Stern | a0b458b | 2006-05-12 11:23:19 -0400 | [diff] [blame] | 260 | struct urb_priv *urbp = urb->hcpriv; |
Alan Stern | 0ed8fee | 2005-12-17 18:02:38 -0500 | [diff] [blame] | 261 | struct uhci_td *td; |
| 262 | |
Alan Stern | a0b458b | 2006-05-12 11:23:19 -0400 | [diff] [blame] | 263 | /* Isochronous pipes don't use toggles and their TD link pointers |
| 264 | * get adjusted during uhci_urb_dequeue(). */ |
| 265 | if (qh->type == USB_ENDPOINT_XFER_ISOC) |
| 266 | return; |
| 267 | |
| 268 | /* If the URB isn't first on its queue, adjust the link pointer |
| 269 | * of the last TD in the previous URB. The toggle doesn't need |
| 270 | * to be saved since this URB can't be executing yet. */ |
| 271 | if (qh->queue.next != &urbp->node) { |
| 272 | struct urb_priv *purbp; |
| 273 | struct uhci_td *ptd; |
| 274 | |
| 275 | purbp = list_entry(urbp->node.prev, struct urb_priv, node); |
| 276 | WARN_ON(list_empty(&purbp->td_list)); |
| 277 | ptd = list_entry(purbp->td_list.prev, struct uhci_td, |
| 278 | list); |
| 279 | td = list_entry(urbp->td_list.prev, struct uhci_td, |
| 280 | list); |
| 281 | ptd->link = td->link; |
| 282 | return; |
| 283 | } |
| 284 | |
Alan Stern | 0ed8fee | 2005-12-17 18:02:38 -0500 | [diff] [blame] | 285 | /* If the QH element pointer is UHCI_PTR_TERM then then currently |
| 286 | * executing URB has already been unlinked, so this one isn't it. */ |
Alan Stern | a0b458b | 2006-05-12 11:23:19 -0400 | [diff] [blame] | 287 | if (qh_element(qh) == UHCI_PTR_TERM) |
Alan Stern | 0ed8fee | 2005-12-17 18:02:38 -0500 | [diff] [blame] | 288 | return; |
| 289 | qh->element = UHCI_PTR_TERM; |
| 290 | |
Alan Stern | a0b458b | 2006-05-12 11:23:19 -0400 | [diff] [blame] | 291 | /* Control pipes have to worry about toggles */ |
| 292 | if (qh->type == USB_ENDPOINT_XFER_CONTROL) |
Alan Stern | 0ed8fee | 2005-12-17 18:02:38 -0500 | [diff] [blame] | 293 | return; |
| 294 | |
Alan Stern | a0b458b | 2006-05-12 11:23:19 -0400 | [diff] [blame] | 295 | /* Save the next toggle value */ |
Alan Stern | 59e29ed | 2006-05-12 11:19:19 -0400 | [diff] [blame] | 296 | WARN_ON(list_empty(&urbp->td_list)); |
| 297 | td = list_entry(urbp->td_list.next, struct uhci_td, list); |
| 298 | qh->needs_fixup = 1; |
| 299 | qh->initial_toggle = uhci_toggle(td_token(td)); |
Alan Stern | 0ed8fee | 2005-12-17 18:02:38 -0500 | [diff] [blame] | 300 | } |
| 301 | |
| 302 | /* |
| 303 | * Fix up the data toggles for URBs in a queue, when one of them |
| 304 | * terminates early (short transfer, error, or dequeued). |
| 305 | */ |
| 306 | static void uhci_fixup_toggles(struct uhci_qh *qh, int skip_first) |
| 307 | { |
| 308 | struct urb_priv *urbp = NULL; |
| 309 | struct uhci_td *td; |
| 310 | unsigned int toggle = qh->initial_toggle; |
| 311 | unsigned int pipe; |
| 312 | |
| 313 | /* Fixups for a short transfer start with the second URB in the |
| 314 | * queue (the short URB is the first). */ |
| 315 | if (skip_first) |
| 316 | urbp = list_entry(qh->queue.next, struct urb_priv, node); |
| 317 | |
| 318 | /* When starting with the first URB, if the QH element pointer is |
| 319 | * still valid then we know the URB's toggles are okay. */ |
| 320 | else if (qh_element(qh) != UHCI_PTR_TERM) |
| 321 | toggle = 2; |
| 322 | |
| 323 | /* Fix up the toggle for the URBs in the queue. Normally this |
| 324 | * loop won't run more than once: When an error or short transfer |
| 325 | * occurs, the queue usually gets emptied. */ |
Alan Stern | 1393adb | 2006-01-31 10:02:55 -0500 | [diff] [blame] | 326 | urbp = list_prepare_entry(urbp, &qh->queue, node); |
Alan Stern | 0ed8fee | 2005-12-17 18:02:38 -0500 | [diff] [blame] | 327 | list_for_each_entry_continue(urbp, &qh->queue, node) { |
| 328 | |
| 329 | /* If the first TD has the right toggle value, we don't |
| 330 | * need to change any toggles in this URB */ |
| 331 | td = list_entry(urbp->td_list.next, struct uhci_td, list); |
| 332 | if (toggle > 1 || uhci_toggle(td_token(td)) == toggle) { |
| 333 | td = list_entry(urbp->td_list.next, struct uhci_td, |
| 334 | list); |
| 335 | toggle = uhci_toggle(td_token(td)) ^ 1; |
| 336 | |
| 337 | /* Otherwise all the toggles in the URB have to be switched */ |
| 338 | } else { |
| 339 | list_for_each_entry(td, &urbp->td_list, list) { |
| 340 | td->token ^= __constant_cpu_to_le32( |
| 341 | TD_TOKEN_TOGGLE); |
| 342 | toggle ^= 1; |
| 343 | } |
| 344 | } |
| 345 | } |
| 346 | |
| 347 | wmb(); |
| 348 | pipe = list_entry(qh->queue.next, struct urb_priv, node)->urb->pipe; |
| 349 | usb_settoggle(qh->udev, usb_pipeendpoint(pipe), |
| 350 | usb_pipeout(pipe), toggle); |
| 351 | qh->needs_fixup = 0; |
| 352 | } |
| 353 | |
| 354 | /* |
Alan Stern | dccf4a4 | 2005-12-17 17:58:46 -0500 | [diff] [blame] | 355 | * Put a QH on the schedule in both hardware and software |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 356 | */ |
Alan Stern | dccf4a4 | 2005-12-17 17:58:46 -0500 | [diff] [blame] | 357 | static void uhci_activate_qh(struct uhci_hcd *uhci, struct uhci_qh *qh) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 358 | { |
Alan Stern | dccf4a4 | 2005-12-17 17:58:46 -0500 | [diff] [blame] | 359 | struct uhci_qh *pqh; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 360 | |
Alan Stern | dccf4a4 | 2005-12-17 17:58:46 -0500 | [diff] [blame] | 361 | WARN_ON(list_empty(&qh->queue)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 362 | |
Alan Stern | dccf4a4 | 2005-12-17 17:58:46 -0500 | [diff] [blame] | 363 | /* Set the element pointer if it isn't set already. |
| 364 | * This isn't needed for Isochronous queues, but it doesn't hurt. */ |
| 365 | if (qh_element(qh) == UHCI_PTR_TERM) { |
| 366 | struct urb_priv *urbp = list_entry(qh->queue.next, |
| 367 | struct urb_priv, node); |
| 368 | struct uhci_td *td = list_entry(urbp->td_list.next, |
| 369 | struct uhci_td, list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 370 | |
Alan Stern | dccf4a4 | 2005-12-17 17:58:46 -0500 | [diff] [blame] | 371 | qh->element = cpu_to_le32(td->dma_handle); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 372 | } |
| 373 | |
Alan Stern | 84afddd | 2006-05-12 11:35:45 -0400 | [diff] [blame] | 374 | /* Treat the queue as if it has just advanced */ |
| 375 | qh->wait_expired = 0; |
| 376 | qh->advance_jiffies = jiffies; |
| 377 | |
Alan Stern | dccf4a4 | 2005-12-17 17:58:46 -0500 | [diff] [blame] | 378 | if (qh->state == QH_STATE_ACTIVE) |
| 379 | return; |
| 380 | qh->state = QH_STATE_ACTIVE; |
| 381 | |
| 382 | /* Move the QH from its old list to the end of the appropriate |
| 383 | * skeleton's list */ |
Alan Stern | 0ed8fee | 2005-12-17 18:02:38 -0500 | [diff] [blame] | 384 | if (qh == uhci->next_qh) |
| 385 | uhci->next_qh = list_entry(qh->node.next, struct uhci_qh, |
| 386 | node); |
Alan Stern | dccf4a4 | 2005-12-17 17:58:46 -0500 | [diff] [blame] | 387 | list_move_tail(&qh->node, &qh->skel->node); |
| 388 | |
| 389 | /* Link it into the schedule */ |
| 390 | pqh = list_entry(qh->node.prev, struct uhci_qh, node); |
| 391 | qh->link = pqh->link; |
| 392 | wmb(); |
| 393 | pqh->link = UHCI_PTR_QH | cpu_to_le32(qh->dma_handle); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 394 | } |
| 395 | |
| 396 | /* |
Alan Stern | dccf4a4 | 2005-12-17 17:58:46 -0500 | [diff] [blame] | 397 | * Take a QH off the hardware schedule |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 398 | */ |
Alan Stern | dccf4a4 | 2005-12-17 17:58:46 -0500 | [diff] [blame] | 399 | static void uhci_unlink_qh(struct uhci_hcd *uhci, struct uhci_qh *qh) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 400 | { |
| 401 | struct uhci_qh *pqh; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 402 | |
Alan Stern | dccf4a4 | 2005-12-17 17:58:46 -0500 | [diff] [blame] | 403 | if (qh->state == QH_STATE_UNLINKING) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 404 | return; |
Alan Stern | dccf4a4 | 2005-12-17 17:58:46 -0500 | [diff] [blame] | 405 | WARN_ON(qh->state != QH_STATE_ACTIVE || !qh->udev); |
| 406 | qh->state = QH_STATE_UNLINKING; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 407 | |
Alan Stern | dccf4a4 | 2005-12-17 17:58:46 -0500 | [diff] [blame] | 408 | /* Unlink the QH from the schedule and record when we did it */ |
| 409 | pqh = list_entry(qh->node.prev, struct uhci_qh, node); |
| 410 | pqh->link = qh->link; |
| 411 | mb(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 412 | |
| 413 | uhci_get_current_frame_number(uhci); |
Alan Stern | dccf4a4 | 2005-12-17 17:58:46 -0500 | [diff] [blame] | 414 | qh->unlink_frame = uhci->frame_number; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 415 | |
Alan Stern | dccf4a4 | 2005-12-17 17:58:46 -0500 | [diff] [blame] | 416 | /* Force an interrupt so we know when the QH is fully unlinked */ |
| 417 | if (list_empty(&uhci->skel_unlink_qh->node)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 418 | uhci_set_next_interrupt(uhci); |
| 419 | |
Alan Stern | dccf4a4 | 2005-12-17 17:58:46 -0500 | [diff] [blame] | 420 | /* Move the QH from its old list to the end of the unlinking list */ |
Alan Stern | 0ed8fee | 2005-12-17 18:02:38 -0500 | [diff] [blame] | 421 | if (qh == uhci->next_qh) |
| 422 | uhci->next_qh = list_entry(qh->node.next, struct uhci_qh, |
| 423 | node); |
Alan Stern | dccf4a4 | 2005-12-17 17:58:46 -0500 | [diff] [blame] | 424 | list_move_tail(&qh->node, &uhci->skel_unlink_qh->node); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 425 | } |
| 426 | |
Alan Stern | dccf4a4 | 2005-12-17 17:58:46 -0500 | [diff] [blame] | 427 | /* |
| 428 | * When we and the controller are through with a QH, it becomes IDLE. |
| 429 | * This happens when a QH has been off the schedule (on the unlinking |
| 430 | * list) for more than one frame, or when an error occurs while adding |
| 431 | * the first URB onto a new QH. |
| 432 | */ |
| 433 | static void uhci_make_qh_idle(struct uhci_hcd *uhci, struct uhci_qh *qh) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 434 | { |
Alan Stern | dccf4a4 | 2005-12-17 17:58:46 -0500 | [diff] [blame] | 435 | WARN_ON(qh->state == QH_STATE_ACTIVE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 436 | |
Alan Stern | 0ed8fee | 2005-12-17 18:02:38 -0500 | [diff] [blame] | 437 | if (qh == uhci->next_qh) |
| 438 | uhci->next_qh = list_entry(qh->node.next, struct uhci_qh, |
| 439 | node); |
Alan Stern | dccf4a4 | 2005-12-17 17:58:46 -0500 | [diff] [blame] | 440 | list_move(&qh->node, &uhci->idle_qh_list); |
| 441 | qh->state = QH_STATE_IDLE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 442 | |
Alan Stern | 59e29ed | 2006-05-12 11:19:19 -0400 | [diff] [blame] | 443 | /* Now that the QH is idle, its post_td isn't being used */ |
| 444 | if (qh->post_td) { |
| 445 | uhci_free_td(uhci, qh->post_td); |
| 446 | qh->post_td = NULL; |
| 447 | } |
| 448 | |
Alan Stern | dccf4a4 | 2005-12-17 17:58:46 -0500 | [diff] [blame] | 449 | /* If anyone is waiting for a QH to become idle, wake them up */ |
| 450 | if (uhci->num_waiting) |
| 451 | wake_up_all(&uhci->waitqh); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 452 | } |
| 453 | |
Alan Stern | dccf4a4 | 2005-12-17 17:58:46 -0500 | [diff] [blame] | 454 | static inline struct urb_priv *uhci_alloc_urb_priv(struct uhci_hcd *uhci, |
| 455 | struct urb *urb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 456 | { |
| 457 | struct urb_priv *urbp; |
| 458 | |
| 459 | urbp = kmem_cache_alloc(uhci_up_cachep, SLAB_ATOMIC); |
| 460 | if (!urbp) |
| 461 | return NULL; |
| 462 | |
| 463 | memset((void *)urbp, 0, sizeof(*urbp)); |
| 464 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 465 | urbp->urb = urb; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 466 | urb->hcpriv = urbp; |
Alan Stern | dccf4a4 | 2005-12-17 17:58:46 -0500 | [diff] [blame] | 467 | |
| 468 | INIT_LIST_HEAD(&urbp->node); |
| 469 | INIT_LIST_HEAD(&urbp->td_list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 470 | |
| 471 | return urbp; |
| 472 | } |
| 473 | |
Alan Stern | dccf4a4 | 2005-12-17 17:58:46 -0500 | [diff] [blame] | 474 | static void uhci_free_urb_priv(struct uhci_hcd *uhci, |
| 475 | struct urb_priv *urbp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 476 | { |
| 477 | struct uhci_td *td, *tmp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 478 | |
Alan Stern | dccf4a4 | 2005-12-17 17:58:46 -0500 | [diff] [blame] | 479 | if (!list_empty(&urbp->node)) |
| 480 | dev_warn(uhci_dev(uhci), "urb %p still on QH's list!\n", |
| 481 | urbp->urb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 482 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 483 | list_for_each_entry_safe(td, tmp, &urbp->td_list, list) { |
Alan Stern | 04538a2 | 2006-05-12 11:29:04 -0400 | [diff] [blame] | 484 | uhci_remove_td_from_urbp(td); |
| 485 | uhci_free_td(uhci, td); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 486 | } |
| 487 | |
Alan Stern | dccf4a4 | 2005-12-17 17:58:46 -0500 | [diff] [blame] | 488 | urbp->urb->hcpriv = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 489 | kmem_cache_free(uhci_up_cachep, urbp); |
| 490 | } |
| 491 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 492 | /* |
| 493 | * Map status to standard result codes |
| 494 | * |
| 495 | * <status> is (td_status(td) & 0xF60000), a.k.a. |
| 496 | * uhci_status_bits(td_status(td)). |
| 497 | * Note: <status> does not include the TD_CTRL_NAK bit. |
| 498 | * <dir_out> is True for output TDs and False for input TDs. |
| 499 | */ |
| 500 | static int uhci_map_status(int status, int dir_out) |
| 501 | { |
| 502 | if (!status) |
| 503 | return 0; |
| 504 | if (status & TD_CTRL_BITSTUFF) /* Bitstuff error */ |
| 505 | return -EPROTO; |
| 506 | if (status & TD_CTRL_CRCTIMEO) { /* CRC/Timeout */ |
| 507 | if (dir_out) |
| 508 | return -EPROTO; |
| 509 | else |
| 510 | return -EILSEQ; |
| 511 | } |
| 512 | if (status & TD_CTRL_BABBLE) /* Babble */ |
| 513 | return -EOVERFLOW; |
| 514 | if (status & TD_CTRL_DBUFERR) /* Buffer error */ |
| 515 | return -ENOSR; |
| 516 | if (status & TD_CTRL_STALLED) /* Stalled */ |
| 517 | return -EPIPE; |
| 518 | WARN_ON(status & TD_CTRL_ACTIVE); /* Active */ |
| 519 | return 0; |
| 520 | } |
| 521 | |
| 522 | /* |
| 523 | * Control transfers |
| 524 | */ |
Alan Stern | dccf4a4 | 2005-12-17 17:58:46 -0500 | [diff] [blame] | 525 | static int uhci_submit_control(struct uhci_hcd *uhci, struct urb *urb, |
| 526 | struct uhci_qh *qh) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 527 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 528 | struct uhci_td *td; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 529 | unsigned long destination, status; |
Alan Stern | dccf4a4 | 2005-12-17 17:58:46 -0500 | [diff] [blame] | 530 | int maxsze = le16_to_cpu(qh->hep->desc.wMaxPacketSize); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 531 | int len = urb->transfer_buffer_length; |
| 532 | dma_addr_t data = urb->transfer_dma; |
Alan Stern | dccf4a4 | 2005-12-17 17:58:46 -0500 | [diff] [blame] | 533 | __le32 *plink; |
Alan Stern | 04538a2 | 2006-05-12 11:29:04 -0400 | [diff] [blame] | 534 | struct urb_priv *urbp = urb->hcpriv; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 535 | |
| 536 | /* The "pipe" thing contains the destination in bits 8--18 */ |
| 537 | destination = (urb->pipe & PIPE_DEVEP_MASK) | USB_PID_SETUP; |
| 538 | |
Alan Stern | af0bb59 | 2005-12-17 18:00:12 -0500 | [diff] [blame] | 539 | /* 3 errors, dummy TD remains inactive */ |
| 540 | status = uhci_maxerr(3); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 541 | if (urb->dev->speed == USB_SPEED_LOW) |
| 542 | status |= TD_CTRL_LS; |
| 543 | |
| 544 | /* |
| 545 | * Build the TD for the control request setup packet |
| 546 | */ |
Alan Stern | af0bb59 | 2005-12-17 18:00:12 -0500 | [diff] [blame] | 547 | td = qh->dummy_td; |
Alan Stern | 04538a2 | 2006-05-12 11:29:04 -0400 | [diff] [blame] | 548 | uhci_add_td_to_urbp(td, urbp); |
Alan Stern | fa34656 | 2005-11-30 11:57:51 -0500 | [diff] [blame] | 549 | uhci_fill_td(td, status, destination | uhci_explen(8), |
Alan Stern | dccf4a4 | 2005-12-17 17:58:46 -0500 | [diff] [blame] | 550 | urb->setup_dma); |
| 551 | plink = &td->link; |
Alan Stern | af0bb59 | 2005-12-17 18:00:12 -0500 | [diff] [blame] | 552 | status |= TD_CTRL_ACTIVE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 553 | |
| 554 | /* |
| 555 | * If direction is "send", change the packet ID from SETUP (0x2D) |
| 556 | * to OUT (0xE1). Else change it from SETUP to IN (0x69) and |
| 557 | * set Short Packet Detect (SPD) for all data packets. |
| 558 | */ |
| 559 | if (usb_pipeout(urb->pipe)) |
| 560 | destination ^= (USB_PID_SETUP ^ USB_PID_OUT); |
| 561 | else { |
| 562 | destination ^= (USB_PID_SETUP ^ USB_PID_IN); |
| 563 | status |= TD_CTRL_SPD; |
| 564 | } |
| 565 | |
| 566 | /* |
Alan Stern | 687f5f3 | 2005-11-30 17:16:19 -0500 | [diff] [blame] | 567 | * Build the DATA TDs |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 568 | */ |
| 569 | while (len > 0) { |
Alan Stern | dccf4a4 | 2005-12-17 17:58:46 -0500 | [diff] [blame] | 570 | int pktsze = min(len, maxsze); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 571 | |
Alan Stern | 2532178 | 2005-04-25 11:14:31 -0400 | [diff] [blame] | 572 | td = uhci_alloc_td(uhci); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 573 | if (!td) |
Alan Stern | af0bb59 | 2005-12-17 18:00:12 -0500 | [diff] [blame] | 574 | goto nomem; |
Alan Stern | dccf4a4 | 2005-12-17 17:58:46 -0500 | [diff] [blame] | 575 | *plink = cpu_to_le32(td->dma_handle); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 576 | |
| 577 | /* Alternate Data0/1 (start with Data1) */ |
| 578 | destination ^= TD_TOKEN_TOGGLE; |
| 579 | |
Alan Stern | 04538a2 | 2006-05-12 11:29:04 -0400 | [diff] [blame] | 580 | uhci_add_td_to_urbp(td, urbp); |
Alan Stern | fa34656 | 2005-11-30 11:57:51 -0500 | [diff] [blame] | 581 | uhci_fill_td(td, status, destination | uhci_explen(pktsze), |
Alan Stern | dccf4a4 | 2005-12-17 17:58:46 -0500 | [diff] [blame] | 582 | data); |
| 583 | plink = &td->link; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 584 | |
| 585 | data += pktsze; |
| 586 | len -= pktsze; |
| 587 | } |
| 588 | |
| 589 | /* |
| 590 | * Build the final TD for control status |
| 591 | */ |
Alan Stern | 2532178 | 2005-04-25 11:14:31 -0400 | [diff] [blame] | 592 | td = uhci_alloc_td(uhci); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 593 | if (!td) |
Alan Stern | af0bb59 | 2005-12-17 18:00:12 -0500 | [diff] [blame] | 594 | goto nomem; |
Alan Stern | dccf4a4 | 2005-12-17 17:58:46 -0500 | [diff] [blame] | 595 | *plink = cpu_to_le32(td->dma_handle); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 596 | |
| 597 | /* |
| 598 | * It's IN if the pipe is an output pipe or we're not expecting |
| 599 | * data back. |
| 600 | */ |
| 601 | destination &= ~TD_TOKEN_PID_MASK; |
| 602 | if (usb_pipeout(urb->pipe) || !urb->transfer_buffer_length) |
| 603 | destination |= USB_PID_IN; |
| 604 | else |
| 605 | destination |= USB_PID_OUT; |
| 606 | |
| 607 | destination |= TD_TOKEN_TOGGLE; /* End in Data1 */ |
| 608 | |
| 609 | status &= ~TD_CTRL_SPD; |
| 610 | |
Alan Stern | 04538a2 | 2006-05-12 11:29:04 -0400 | [diff] [blame] | 611 | uhci_add_td_to_urbp(td, urbp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 612 | uhci_fill_td(td, status | TD_CTRL_IOC, |
Alan Stern | dccf4a4 | 2005-12-17 17:58:46 -0500 | [diff] [blame] | 613 | destination | uhci_explen(0), 0); |
Alan Stern | af0bb59 | 2005-12-17 18:00:12 -0500 | [diff] [blame] | 614 | plink = &td->link; |
| 615 | |
| 616 | /* |
| 617 | * Build the new dummy TD and activate the old one |
| 618 | */ |
| 619 | td = uhci_alloc_td(uhci); |
| 620 | if (!td) |
| 621 | goto nomem; |
| 622 | *plink = cpu_to_le32(td->dma_handle); |
| 623 | |
| 624 | uhci_fill_td(td, 0, USB_PID_OUT | uhci_explen(0), 0); |
| 625 | wmb(); |
| 626 | qh->dummy_td->status |= __constant_cpu_to_le32(TD_CTRL_ACTIVE); |
| 627 | qh->dummy_td = td; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 628 | |
| 629 | /* Low-speed transfers get a different queue, and won't hog the bus. |
| 630 | * Also, some devices enumerate better without FSBR; the easiest way |
| 631 | * to do that is to put URBs on the low-speed queue while the device |
Alan Stern | 630aa3c | 2006-01-23 17:17:21 -0500 | [diff] [blame] | 632 | * isn't in the CONFIGURED state. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 633 | if (urb->dev->speed == USB_SPEED_LOW || |
Alan Stern | 630aa3c | 2006-01-23 17:17:21 -0500 | [diff] [blame] | 634 | urb->dev->state != USB_STATE_CONFIGURED) |
Alan Stern | dccf4a4 | 2005-12-17 17:58:46 -0500 | [diff] [blame] | 635 | qh->skel = uhci->skel_ls_control_qh; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 636 | else { |
Alan Stern | dccf4a4 | 2005-12-17 17:58:46 -0500 | [diff] [blame] | 637 | qh->skel = uhci->skel_fs_control_qh; |
Alan Stern | 84afddd | 2006-05-12 11:35:45 -0400 | [diff] [blame] | 638 | uhci_add_fsbr(uhci, urb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 639 | } |
Alan Stern | 59e29ed | 2006-05-12 11:19:19 -0400 | [diff] [blame] | 640 | |
| 641 | urb->actual_length = -8; /* Account for the SETUP packet */ |
Alan Stern | dccf4a4 | 2005-12-17 17:58:46 -0500 | [diff] [blame] | 642 | return 0; |
Alan Stern | af0bb59 | 2005-12-17 18:00:12 -0500 | [diff] [blame] | 643 | |
| 644 | nomem: |
| 645 | /* Remove the dummy TD from the td_list so it doesn't get freed */ |
Alan Stern | 04538a2 | 2006-05-12 11:29:04 -0400 | [diff] [blame] | 646 | uhci_remove_td_from_urbp(qh->dummy_td); |
Alan Stern | af0bb59 | 2005-12-17 18:00:12 -0500 | [diff] [blame] | 647 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 648 | } |
| 649 | |
| 650 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 651 | * Common submit for bulk and interrupt |
| 652 | */ |
Alan Stern | dccf4a4 | 2005-12-17 17:58:46 -0500 | [diff] [blame] | 653 | static int uhci_submit_common(struct uhci_hcd *uhci, struct urb *urb, |
| 654 | struct uhci_qh *qh) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 655 | { |
| 656 | struct uhci_td *td; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 657 | unsigned long destination, status; |
Alan Stern | dccf4a4 | 2005-12-17 17:58:46 -0500 | [diff] [blame] | 658 | int maxsze = le16_to_cpu(qh->hep->desc.wMaxPacketSize); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 659 | int len = urb->transfer_buffer_length; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 660 | dma_addr_t data = urb->transfer_dma; |
Alan Stern | af0bb59 | 2005-12-17 18:00:12 -0500 | [diff] [blame] | 661 | __le32 *plink; |
Alan Stern | 04538a2 | 2006-05-12 11:29:04 -0400 | [diff] [blame] | 662 | struct urb_priv *urbp = urb->hcpriv; |
Alan Stern | af0bb59 | 2005-12-17 18:00:12 -0500 | [diff] [blame] | 663 | unsigned int toggle; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 664 | |
| 665 | if (len < 0) |
| 666 | return -EINVAL; |
| 667 | |
| 668 | /* The "pipe" thing contains the destination in bits 8--18 */ |
| 669 | destination = (urb->pipe & PIPE_DEVEP_MASK) | usb_packetid(urb->pipe); |
Alan Stern | af0bb59 | 2005-12-17 18:00:12 -0500 | [diff] [blame] | 670 | toggle = usb_gettoggle(urb->dev, usb_pipeendpoint(urb->pipe), |
| 671 | usb_pipeout(urb->pipe)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 672 | |
Alan Stern | af0bb59 | 2005-12-17 18:00:12 -0500 | [diff] [blame] | 673 | /* 3 errors, dummy TD remains inactive */ |
| 674 | status = uhci_maxerr(3); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 675 | if (urb->dev->speed == USB_SPEED_LOW) |
| 676 | status |= TD_CTRL_LS; |
| 677 | if (usb_pipein(urb->pipe)) |
| 678 | status |= TD_CTRL_SPD; |
| 679 | |
| 680 | /* |
Alan Stern | 687f5f3 | 2005-11-30 17:16:19 -0500 | [diff] [blame] | 681 | * Build the DATA TDs |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 682 | */ |
Alan Stern | af0bb59 | 2005-12-17 18:00:12 -0500 | [diff] [blame] | 683 | plink = NULL; |
| 684 | td = qh->dummy_td; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 685 | do { /* Allow zero length packets */ |
| 686 | int pktsze = maxsze; |
| 687 | |
Alan Stern | dccf4a4 | 2005-12-17 17:58:46 -0500 | [diff] [blame] | 688 | if (len <= pktsze) { /* The last packet */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 689 | pktsze = len; |
| 690 | if (!(urb->transfer_flags & URB_SHORT_NOT_OK)) |
| 691 | status &= ~TD_CTRL_SPD; |
| 692 | } |
| 693 | |
Alan Stern | af0bb59 | 2005-12-17 18:00:12 -0500 | [diff] [blame] | 694 | if (plink) { |
| 695 | td = uhci_alloc_td(uhci); |
| 696 | if (!td) |
| 697 | goto nomem; |
| 698 | *plink = cpu_to_le32(td->dma_handle); |
| 699 | } |
Alan Stern | 04538a2 | 2006-05-12 11:29:04 -0400 | [diff] [blame] | 700 | uhci_add_td_to_urbp(td, urbp); |
Alan Stern | dccf4a4 | 2005-12-17 17:58:46 -0500 | [diff] [blame] | 701 | uhci_fill_td(td, status, |
Alan Stern | af0bb59 | 2005-12-17 18:00:12 -0500 | [diff] [blame] | 702 | destination | uhci_explen(pktsze) | |
| 703 | (toggle << TD_TOKEN_TOGGLE_SHIFT), |
| 704 | data); |
Alan Stern | dccf4a4 | 2005-12-17 17:58:46 -0500 | [diff] [blame] | 705 | plink = &td->link; |
Alan Stern | af0bb59 | 2005-12-17 18:00:12 -0500 | [diff] [blame] | 706 | status |= TD_CTRL_ACTIVE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 707 | |
| 708 | data += pktsze; |
| 709 | len -= maxsze; |
Alan Stern | af0bb59 | 2005-12-17 18:00:12 -0500 | [diff] [blame] | 710 | toggle ^= 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 711 | } while (len > 0); |
| 712 | |
| 713 | /* |
| 714 | * URB_ZERO_PACKET means adding a 0-length packet, if direction |
| 715 | * is OUT and the transfer_length was an exact multiple of maxsze, |
| 716 | * hence (len = transfer_length - N * maxsze) == 0 |
| 717 | * however, if transfer_length == 0, the zero packet was already |
| 718 | * prepared above. |
| 719 | */ |
Alan Stern | dccf4a4 | 2005-12-17 17:58:46 -0500 | [diff] [blame] | 720 | if ((urb->transfer_flags & URB_ZERO_PACKET) && |
| 721 | usb_pipeout(urb->pipe) && len == 0 && |
| 722 | urb->transfer_buffer_length > 0) { |
Alan Stern | 2532178 | 2005-04-25 11:14:31 -0400 | [diff] [blame] | 723 | td = uhci_alloc_td(uhci); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 724 | if (!td) |
Alan Stern | af0bb59 | 2005-12-17 18:00:12 -0500 | [diff] [blame] | 725 | goto nomem; |
Alan Stern | dccf4a4 | 2005-12-17 17:58:46 -0500 | [diff] [blame] | 726 | *plink = cpu_to_le32(td->dma_handle); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 727 | |
Alan Stern | 04538a2 | 2006-05-12 11:29:04 -0400 | [diff] [blame] | 728 | uhci_add_td_to_urbp(td, urbp); |
Alan Stern | af0bb59 | 2005-12-17 18:00:12 -0500 | [diff] [blame] | 729 | uhci_fill_td(td, status, |
| 730 | destination | uhci_explen(0) | |
| 731 | (toggle << TD_TOKEN_TOGGLE_SHIFT), |
| 732 | data); |
| 733 | plink = &td->link; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 734 | |
Alan Stern | af0bb59 | 2005-12-17 18:00:12 -0500 | [diff] [blame] | 735 | toggle ^= 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 736 | } |
| 737 | |
| 738 | /* Set the interrupt-on-completion flag on the last packet. |
| 739 | * A more-or-less typical 4 KB URB (= size of one memory page) |
| 740 | * will require about 3 ms to transfer; that's a little on the |
| 741 | * fast side but not enough to justify delaying an interrupt |
| 742 | * more than 2 or 3 URBs, so we will ignore the URB_NO_INTERRUPT |
| 743 | * flag setting. */ |
Alan Stern | dccf4a4 | 2005-12-17 17:58:46 -0500 | [diff] [blame] | 744 | td->status |= __constant_cpu_to_le32(TD_CTRL_IOC); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 745 | |
Alan Stern | af0bb59 | 2005-12-17 18:00:12 -0500 | [diff] [blame] | 746 | /* |
| 747 | * Build the new dummy TD and activate the old one |
| 748 | */ |
| 749 | td = uhci_alloc_td(uhci); |
| 750 | if (!td) |
| 751 | goto nomem; |
| 752 | *plink = cpu_to_le32(td->dma_handle); |
| 753 | |
| 754 | uhci_fill_td(td, 0, USB_PID_OUT | uhci_explen(0), 0); |
| 755 | wmb(); |
| 756 | qh->dummy_td->status |= __constant_cpu_to_le32(TD_CTRL_ACTIVE); |
| 757 | qh->dummy_td = td; |
| 758 | |
| 759 | usb_settoggle(urb->dev, usb_pipeendpoint(urb->pipe), |
| 760 | usb_pipeout(urb->pipe), toggle); |
Alan Stern | dccf4a4 | 2005-12-17 17:58:46 -0500 | [diff] [blame] | 761 | return 0; |
Alan Stern | af0bb59 | 2005-12-17 18:00:12 -0500 | [diff] [blame] | 762 | |
| 763 | nomem: |
| 764 | /* Remove the dummy TD from the td_list so it doesn't get freed */ |
Alan Stern | 04538a2 | 2006-05-12 11:29:04 -0400 | [diff] [blame] | 765 | uhci_remove_td_from_urbp(qh->dummy_td); |
Alan Stern | af0bb59 | 2005-12-17 18:00:12 -0500 | [diff] [blame] | 766 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 767 | } |
| 768 | |
Alan Stern | dccf4a4 | 2005-12-17 17:58:46 -0500 | [diff] [blame] | 769 | static inline int uhci_submit_bulk(struct uhci_hcd *uhci, struct urb *urb, |
| 770 | struct uhci_qh *qh) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 771 | { |
| 772 | int ret; |
| 773 | |
| 774 | /* Can't have low-speed bulk transfers */ |
| 775 | if (urb->dev->speed == USB_SPEED_LOW) |
| 776 | return -EINVAL; |
| 777 | |
Alan Stern | dccf4a4 | 2005-12-17 17:58:46 -0500 | [diff] [blame] | 778 | qh->skel = uhci->skel_bulk_qh; |
| 779 | ret = uhci_submit_common(uhci, urb, qh); |
| 780 | if (ret == 0) |
Alan Stern | 84afddd | 2006-05-12 11:35:45 -0400 | [diff] [blame] | 781 | uhci_add_fsbr(uhci, urb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 782 | return ret; |
| 783 | } |
| 784 | |
Alan Stern | dccf4a4 | 2005-12-17 17:58:46 -0500 | [diff] [blame] | 785 | static inline int uhci_submit_interrupt(struct uhci_hcd *uhci, struct urb *urb, |
| 786 | struct uhci_qh *qh) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 787 | { |
Alan Stern | dccf4a4 | 2005-12-17 17:58:46 -0500 | [diff] [blame] | 788 | /* USB 1.1 interrupt transfers only involve one packet per interval. |
| 789 | * Drivers can submit URBs of any length, but longer ones will need |
| 790 | * multiple intervals to complete. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 791 | */ |
Alan Stern | dccf4a4 | 2005-12-17 17:58:46 -0500 | [diff] [blame] | 792 | qh->skel = uhci->skelqh[__interval_to_skel(urb->interval)]; |
| 793 | return uhci_submit_common(uhci, urb, qh); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 794 | } |
| 795 | |
| 796 | /* |
Alan Stern | b186900 | 2006-05-12 11:14:25 -0400 | [diff] [blame] | 797 | * Fix up the data structures following a short transfer |
| 798 | */ |
| 799 | static int uhci_fixup_short_transfer(struct uhci_hcd *uhci, |
Alan Stern | 59e29ed | 2006-05-12 11:19:19 -0400 | [diff] [blame] | 800 | struct uhci_qh *qh, struct urb_priv *urbp) |
Alan Stern | b186900 | 2006-05-12 11:14:25 -0400 | [diff] [blame] | 801 | { |
| 802 | struct uhci_td *td; |
Alan Stern | 59e29ed | 2006-05-12 11:19:19 -0400 | [diff] [blame] | 803 | struct list_head *tmp; |
| 804 | int ret; |
Alan Stern | b186900 | 2006-05-12 11:14:25 -0400 | [diff] [blame] | 805 | |
| 806 | td = list_entry(urbp->td_list.prev, struct uhci_td, list); |
| 807 | if (qh->type == USB_ENDPOINT_XFER_CONTROL) { |
Alan Stern | b186900 | 2006-05-12 11:14:25 -0400 | [diff] [blame] | 808 | |
| 809 | /* When a control transfer is short, we have to restart |
| 810 | * the queue at the status stage transaction, which is |
| 811 | * the last TD. */ |
Alan Stern | 59e29ed | 2006-05-12 11:19:19 -0400 | [diff] [blame] | 812 | WARN_ON(list_empty(&urbp->td_list)); |
Alan Stern | b186900 | 2006-05-12 11:14:25 -0400 | [diff] [blame] | 813 | qh->element = cpu_to_le32(td->dma_handle); |
Alan Stern | 59e29ed | 2006-05-12 11:19:19 -0400 | [diff] [blame] | 814 | tmp = td->list.prev; |
Alan Stern | b186900 | 2006-05-12 11:14:25 -0400 | [diff] [blame] | 815 | ret = -EINPROGRESS; |
| 816 | |
Alan Stern | 59e29ed | 2006-05-12 11:19:19 -0400 | [diff] [blame] | 817 | } else { |
Alan Stern | b186900 | 2006-05-12 11:14:25 -0400 | [diff] [blame] | 818 | |
| 819 | /* When a bulk/interrupt transfer is short, we have to |
| 820 | * fix up the toggles of the following URBs on the queue |
| 821 | * before restarting the queue at the next URB. */ |
Alan Stern | 59e29ed | 2006-05-12 11:19:19 -0400 | [diff] [blame] | 822 | qh->initial_toggle = uhci_toggle(td_token(qh->post_td)) ^ 1; |
Alan Stern | b186900 | 2006-05-12 11:14:25 -0400 | [diff] [blame] | 823 | uhci_fixup_toggles(qh, 1); |
| 824 | |
Alan Stern | 59e29ed | 2006-05-12 11:19:19 -0400 | [diff] [blame] | 825 | if (list_empty(&urbp->td_list)) |
| 826 | td = qh->post_td; |
Alan Stern | b186900 | 2006-05-12 11:14:25 -0400 | [diff] [blame] | 827 | qh->element = td->link; |
Alan Stern | 59e29ed | 2006-05-12 11:19:19 -0400 | [diff] [blame] | 828 | tmp = urbp->td_list.prev; |
| 829 | ret = 0; |
Alan Stern | b186900 | 2006-05-12 11:14:25 -0400 | [diff] [blame] | 830 | } |
| 831 | |
Alan Stern | 59e29ed | 2006-05-12 11:19:19 -0400 | [diff] [blame] | 832 | /* Remove all the TDs we skipped over, from tmp back to the start */ |
| 833 | while (tmp != &urbp->td_list) { |
| 834 | td = list_entry(tmp, struct uhci_td, list); |
| 835 | tmp = tmp->prev; |
| 836 | |
Alan Stern | 04538a2 | 2006-05-12 11:29:04 -0400 | [diff] [blame] | 837 | uhci_remove_td_from_urbp(td); |
| 838 | uhci_free_td(uhci, td); |
Alan Stern | 59e29ed | 2006-05-12 11:19:19 -0400 | [diff] [blame] | 839 | } |
Alan Stern | b186900 | 2006-05-12 11:14:25 -0400 | [diff] [blame] | 840 | return ret; |
| 841 | } |
| 842 | |
| 843 | /* |
| 844 | * Common result for control, bulk, and interrupt |
| 845 | */ |
| 846 | static int uhci_result_common(struct uhci_hcd *uhci, struct urb *urb) |
| 847 | { |
| 848 | struct urb_priv *urbp = urb->hcpriv; |
| 849 | struct uhci_qh *qh = urbp->qh; |
Alan Stern | 59e29ed | 2006-05-12 11:19:19 -0400 | [diff] [blame] | 850 | struct uhci_td *td, *tmp; |
Alan Stern | b186900 | 2006-05-12 11:14:25 -0400 | [diff] [blame] | 851 | unsigned status; |
| 852 | int ret = 0; |
| 853 | |
Alan Stern | 59e29ed | 2006-05-12 11:19:19 -0400 | [diff] [blame] | 854 | list_for_each_entry_safe(td, tmp, &urbp->td_list, list) { |
Alan Stern | b186900 | 2006-05-12 11:14:25 -0400 | [diff] [blame] | 855 | unsigned int ctrlstat; |
| 856 | int len; |
| 857 | |
Alan Stern | b186900 | 2006-05-12 11:14:25 -0400 | [diff] [blame] | 858 | ctrlstat = td_status(td); |
| 859 | status = uhci_status_bits(ctrlstat); |
| 860 | if (status & TD_CTRL_ACTIVE) |
| 861 | return -EINPROGRESS; |
| 862 | |
| 863 | len = uhci_actual_length(ctrlstat); |
| 864 | urb->actual_length += len; |
| 865 | |
| 866 | if (status) { |
| 867 | ret = uhci_map_status(status, |
| 868 | uhci_packetout(td_token(td))); |
| 869 | if ((debug == 1 && ret != -EPIPE) || debug > 1) { |
| 870 | /* Some debugging code */ |
| 871 | dev_dbg(uhci_dev(uhci), |
| 872 | "%s: failed with status %x\n", |
| 873 | __FUNCTION__, status); |
| 874 | |
| 875 | if (debug > 1 && errbuf) { |
| 876 | /* Print the chain for debugging */ |
| 877 | uhci_show_qh(urbp->qh, errbuf, |
| 878 | ERRBUF_LEN, 0); |
| 879 | lprintk(errbuf); |
| 880 | } |
| 881 | } |
| 882 | |
| 883 | } else if (len < uhci_expected_length(td_token(td))) { |
| 884 | |
| 885 | /* We received a short packet */ |
| 886 | if (urb->transfer_flags & URB_SHORT_NOT_OK) |
| 887 | ret = -EREMOTEIO; |
| 888 | else if (ctrlstat & TD_CTRL_SPD) |
| 889 | ret = 1; |
| 890 | } |
| 891 | |
Alan Stern | 04538a2 | 2006-05-12 11:29:04 -0400 | [diff] [blame] | 892 | uhci_remove_td_from_urbp(td); |
Alan Stern | 59e29ed | 2006-05-12 11:19:19 -0400 | [diff] [blame] | 893 | if (qh->post_td) |
Alan Stern | 04538a2 | 2006-05-12 11:29:04 -0400 | [diff] [blame] | 894 | uhci_free_td(uhci, qh->post_td); |
Alan Stern | 59e29ed | 2006-05-12 11:19:19 -0400 | [diff] [blame] | 895 | qh->post_td = td; |
| 896 | |
Alan Stern | b186900 | 2006-05-12 11:14:25 -0400 | [diff] [blame] | 897 | if (ret != 0) |
| 898 | goto err; |
| 899 | } |
| 900 | return ret; |
| 901 | |
| 902 | err: |
| 903 | if (ret < 0) { |
| 904 | /* In case a control transfer gets an error |
| 905 | * during the setup stage */ |
| 906 | urb->actual_length = max(urb->actual_length, 0); |
| 907 | |
| 908 | /* Note that the queue has stopped and save |
| 909 | * the next toggle value */ |
| 910 | qh->element = UHCI_PTR_TERM; |
| 911 | qh->is_stopped = 1; |
| 912 | qh->needs_fixup = (qh->type != USB_ENDPOINT_XFER_CONTROL); |
| 913 | qh->initial_toggle = uhci_toggle(td_token(td)) ^ |
| 914 | (ret == -EREMOTEIO); |
| 915 | |
| 916 | } else /* Short packet received */ |
Alan Stern | 59e29ed | 2006-05-12 11:19:19 -0400 | [diff] [blame] | 917 | ret = uhci_fixup_short_transfer(uhci, qh, urbp); |
Alan Stern | b186900 | 2006-05-12 11:14:25 -0400 | [diff] [blame] | 918 | return ret; |
| 919 | } |
| 920 | |
| 921 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 922 | * Isochronous transfers |
| 923 | */ |
Alan Stern | dccf4a4 | 2005-12-17 17:58:46 -0500 | [diff] [blame] | 924 | static int uhci_submit_isochronous(struct uhci_hcd *uhci, struct urb *urb, |
| 925 | struct uhci_qh *qh) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 926 | { |
Alan Stern | dccf4a4 | 2005-12-17 17:58:46 -0500 | [diff] [blame] | 927 | struct uhci_td *td = NULL; /* Since urb->number_of_packets > 0 */ |
Alan Stern | 0ed8fee | 2005-12-17 18:02:38 -0500 | [diff] [blame] | 928 | int i, frame; |
Alan Stern | dccf4a4 | 2005-12-17 17:58:46 -0500 | [diff] [blame] | 929 | unsigned long destination, status; |
Alan Stern | b81d343 | 2005-10-13 17:00:24 -0400 | [diff] [blame] | 930 | struct urb_priv *urbp = (struct urb_priv *) urb->hcpriv; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 931 | |
Alan Stern | 0ed8fee | 2005-12-17 18:02:38 -0500 | [diff] [blame] | 932 | if (urb->number_of_packets > 900) /* 900? Why? */ |
| 933 | return -EFBIG; |
| 934 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 935 | status = TD_CTRL_ACTIVE | TD_CTRL_IOS; |
| 936 | destination = (urb->pipe & PIPE_DEVEP_MASK) | usb_packetid(urb->pipe); |
| 937 | |
Alan Stern | 0ed8fee | 2005-12-17 18:02:38 -0500 | [diff] [blame] | 938 | /* Figure out the starting frame number */ |
| 939 | if (urb->transfer_flags & URB_ISO_ASAP) { |
| 940 | if (list_empty(&qh->queue)) { |
| 941 | uhci_get_current_frame_number(uhci); |
| 942 | urb->start_frame = (uhci->frame_number + 10); |
| 943 | |
| 944 | } else { /* Go right after the last one */ |
| 945 | struct urb *last_urb; |
| 946 | |
| 947 | last_urb = list_entry(qh->queue.prev, |
| 948 | struct urb_priv, node)->urb; |
| 949 | urb->start_frame = (last_urb->start_frame + |
| 950 | last_urb->number_of_packets * |
| 951 | last_urb->interval); |
| 952 | } |
| 953 | } else { |
| 954 | /* FIXME: Sanity check */ |
| 955 | } |
| 956 | urb->start_frame &= (UHCI_NUMFRAMES - 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 957 | |
Alan Stern | b81d343 | 2005-10-13 17:00:24 -0400 | [diff] [blame] | 958 | for (i = 0; i < urb->number_of_packets; i++) { |
Alan Stern | 2532178 | 2005-04-25 11:14:31 -0400 | [diff] [blame] | 959 | td = uhci_alloc_td(uhci); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 960 | if (!td) |
| 961 | return -ENOMEM; |
| 962 | |
Alan Stern | 04538a2 | 2006-05-12 11:29:04 -0400 | [diff] [blame] | 963 | uhci_add_td_to_urbp(td, urbp); |
Alan Stern | dccf4a4 | 2005-12-17 17:58:46 -0500 | [diff] [blame] | 964 | uhci_fill_td(td, status, destination | |
| 965 | uhci_explen(urb->iso_frame_desc[i].length), |
| 966 | urb->transfer_dma + |
| 967 | urb->iso_frame_desc[i].offset); |
Alan Stern | b81d343 | 2005-10-13 17:00:24 -0400 | [diff] [blame] | 968 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 969 | |
Alan Stern | dccf4a4 | 2005-12-17 17:58:46 -0500 | [diff] [blame] | 970 | /* Set the interrupt-on-completion flag on the last packet. */ |
| 971 | td->status |= __constant_cpu_to_le32(TD_CTRL_IOC); |
| 972 | |
| 973 | qh->skel = uhci->skel_iso_qh; |
| 974 | |
| 975 | /* Add the TDs to the frame list */ |
Alan Stern | b81d343 | 2005-10-13 17:00:24 -0400 | [diff] [blame] | 976 | frame = urb->start_frame; |
| 977 | list_for_each_entry(td, &urbp->td_list, list) { |
Alan Stern | dccf4a4 | 2005-12-17 17:58:46 -0500 | [diff] [blame] | 978 | uhci_insert_td_in_frame_list(uhci, td, frame); |
Alan Stern | b81d343 | 2005-10-13 17:00:24 -0400 | [diff] [blame] | 979 | frame += urb->interval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 980 | } |
| 981 | |
Alan Stern | dccf4a4 | 2005-12-17 17:58:46 -0500 | [diff] [blame] | 982 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 983 | } |
| 984 | |
| 985 | static int uhci_result_isochronous(struct uhci_hcd *uhci, struct urb *urb) |
| 986 | { |
| 987 | struct uhci_td *td; |
| 988 | struct urb_priv *urbp = (struct urb_priv *)urb->hcpriv; |
| 989 | int status; |
| 990 | int i, ret = 0; |
| 991 | |
Alan Stern | b81d343 | 2005-10-13 17:00:24 -0400 | [diff] [blame] | 992 | urb->actual_length = urb->error_count = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 993 | |
| 994 | i = 0; |
| 995 | list_for_each_entry(td, &urbp->td_list, list) { |
| 996 | int actlength; |
| 997 | unsigned int ctrlstat = td_status(td); |
| 998 | |
| 999 | if (ctrlstat & TD_CTRL_ACTIVE) |
| 1000 | return -EINPROGRESS; |
| 1001 | |
| 1002 | actlength = uhci_actual_length(ctrlstat); |
| 1003 | urb->iso_frame_desc[i].actual_length = actlength; |
| 1004 | urb->actual_length += actlength; |
| 1005 | |
| 1006 | status = uhci_map_status(uhci_status_bits(ctrlstat), |
| 1007 | usb_pipeout(urb->pipe)); |
| 1008 | urb->iso_frame_desc[i].status = status; |
| 1009 | if (status) { |
| 1010 | urb->error_count++; |
| 1011 | ret = status; |
| 1012 | } |
| 1013 | |
| 1014 | i++; |
| 1015 | } |
| 1016 | |
| 1017 | return ret; |
| 1018 | } |
| 1019 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1020 | static int uhci_urb_enqueue(struct usb_hcd *hcd, |
Alan Stern | dccf4a4 | 2005-12-17 17:58:46 -0500 | [diff] [blame] | 1021 | struct usb_host_endpoint *hep, |
Al Viro | 55016f1 | 2005-10-21 03:21:58 -0400 | [diff] [blame] | 1022 | struct urb *urb, gfp_t mem_flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1023 | { |
| 1024 | int ret; |
| 1025 | struct uhci_hcd *uhci = hcd_to_uhci(hcd); |
| 1026 | unsigned long flags; |
Alan Stern | dccf4a4 | 2005-12-17 17:58:46 -0500 | [diff] [blame] | 1027 | struct urb_priv *urbp; |
| 1028 | struct uhci_qh *qh; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1029 | int bustime; |
| 1030 | |
| 1031 | spin_lock_irqsave(&uhci->lock, flags); |
| 1032 | |
| 1033 | ret = urb->status; |
| 1034 | if (ret != -EINPROGRESS) /* URB already unlinked! */ |
Alan Stern | dccf4a4 | 2005-12-17 17:58:46 -0500 | [diff] [blame] | 1035 | goto done; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1036 | |
Alan Stern | dccf4a4 | 2005-12-17 17:58:46 -0500 | [diff] [blame] | 1037 | ret = -ENOMEM; |
| 1038 | urbp = uhci_alloc_urb_priv(uhci, urb); |
| 1039 | if (!urbp) |
| 1040 | goto done; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1041 | |
Alan Stern | dccf4a4 | 2005-12-17 17:58:46 -0500 | [diff] [blame] | 1042 | if (hep->hcpriv) |
| 1043 | qh = (struct uhci_qh *) hep->hcpriv; |
| 1044 | else { |
| 1045 | qh = uhci_alloc_qh(uhci, urb->dev, hep); |
| 1046 | if (!qh) |
| 1047 | goto err_no_qh; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1048 | } |
Alan Stern | dccf4a4 | 2005-12-17 17:58:46 -0500 | [diff] [blame] | 1049 | urbp->qh = qh; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1050 | |
Alan Stern | 4de7d2c | 2006-05-05 16:26:58 -0400 | [diff] [blame] | 1051 | switch (qh->type) { |
| 1052 | case USB_ENDPOINT_XFER_CONTROL: |
Alan Stern | dccf4a4 | 2005-12-17 17:58:46 -0500 | [diff] [blame] | 1053 | ret = uhci_submit_control(uhci, urb, qh); |
| 1054 | break; |
Alan Stern | 4de7d2c | 2006-05-05 16:26:58 -0400 | [diff] [blame] | 1055 | case USB_ENDPOINT_XFER_BULK: |
Alan Stern | dccf4a4 | 2005-12-17 17:58:46 -0500 | [diff] [blame] | 1056 | ret = uhci_submit_bulk(uhci, urb, qh); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1057 | break; |
Alan Stern | 4de7d2c | 2006-05-05 16:26:58 -0400 | [diff] [blame] | 1058 | case USB_ENDPOINT_XFER_INT: |
Alan Stern | dccf4a4 | 2005-12-17 17:58:46 -0500 | [diff] [blame] | 1059 | if (list_empty(&qh->queue)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1060 | bustime = usb_check_bandwidth(urb->dev, urb); |
| 1061 | if (bustime < 0) |
| 1062 | ret = bustime; |
| 1063 | else { |
Alan Stern | dccf4a4 | 2005-12-17 17:58:46 -0500 | [diff] [blame] | 1064 | ret = uhci_submit_interrupt(uhci, urb, qh); |
| 1065 | if (ret == 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1066 | usb_claim_bandwidth(urb->dev, urb, bustime, 0); |
| 1067 | } |
| 1068 | } else { /* inherit from parent */ |
Alan Stern | dccf4a4 | 2005-12-17 17:58:46 -0500 | [diff] [blame] | 1069 | struct urb_priv *eurbp; |
| 1070 | |
| 1071 | eurbp = list_entry(qh->queue.prev, struct urb_priv, |
| 1072 | node); |
| 1073 | urb->bandwidth = eurbp->urb->bandwidth; |
| 1074 | ret = uhci_submit_interrupt(uhci, urb, qh); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1075 | } |
| 1076 | break; |
Alan Stern | 4de7d2c | 2006-05-05 16:26:58 -0400 | [diff] [blame] | 1077 | case USB_ENDPOINT_XFER_ISOC: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1078 | bustime = usb_check_bandwidth(urb->dev, urb); |
| 1079 | if (bustime < 0) { |
| 1080 | ret = bustime; |
| 1081 | break; |
| 1082 | } |
| 1083 | |
Alan Stern | dccf4a4 | 2005-12-17 17:58:46 -0500 | [diff] [blame] | 1084 | ret = uhci_submit_isochronous(uhci, urb, qh); |
| 1085 | if (ret == 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1086 | usb_claim_bandwidth(urb->dev, urb, bustime, 1); |
| 1087 | break; |
| 1088 | } |
Alan Stern | dccf4a4 | 2005-12-17 17:58:46 -0500 | [diff] [blame] | 1089 | if (ret != 0) |
| 1090 | goto err_submit_failed; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1091 | |
Alan Stern | dccf4a4 | 2005-12-17 17:58:46 -0500 | [diff] [blame] | 1092 | /* Add this URB to the QH */ |
| 1093 | urbp->qh = qh; |
| 1094 | list_add_tail(&urbp->node, &qh->queue); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1095 | |
Alan Stern | dccf4a4 | 2005-12-17 17:58:46 -0500 | [diff] [blame] | 1096 | /* If the new URB is the first and only one on this QH then either |
| 1097 | * the QH is new and idle or else it's unlinked and waiting to |
Alan Stern | 2775562 | 2006-05-05 16:32:02 -0400 | [diff] [blame] | 1098 | * become idle, so we can activate it right away. But only if the |
| 1099 | * queue isn't stopped. */ |
Alan Stern | 84afddd | 2006-05-12 11:35:45 -0400 | [diff] [blame] | 1100 | if (qh->queue.next == &urbp->node && !qh->is_stopped) { |
Alan Stern | dccf4a4 | 2005-12-17 17:58:46 -0500 | [diff] [blame] | 1101 | uhci_activate_qh(uhci, qh); |
Alan Stern | 84afddd | 2006-05-12 11:35:45 -0400 | [diff] [blame] | 1102 | uhci_qh_wants_fsbr(uhci, qh); |
| 1103 | } |
Alan Stern | dccf4a4 | 2005-12-17 17:58:46 -0500 | [diff] [blame] | 1104 | goto done; |
| 1105 | |
| 1106 | err_submit_failed: |
| 1107 | if (qh->state == QH_STATE_IDLE) |
| 1108 | uhci_make_qh_idle(uhci, qh); /* Reclaim unused QH */ |
| 1109 | |
| 1110 | err_no_qh: |
| 1111 | uhci_free_urb_priv(uhci, urbp); |
| 1112 | |
| 1113 | done: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1114 | spin_unlock_irqrestore(&uhci->lock, flags); |
| 1115 | return ret; |
| 1116 | } |
| 1117 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1118 | static int uhci_urb_dequeue(struct usb_hcd *hcd, struct urb *urb) |
| 1119 | { |
| 1120 | struct uhci_hcd *uhci = hcd_to_uhci(hcd); |
| 1121 | unsigned long flags; |
| 1122 | struct urb_priv *urbp; |
| 1123 | |
| 1124 | spin_lock_irqsave(&uhci->lock, flags); |
| 1125 | urbp = urb->hcpriv; |
| 1126 | if (!urbp) /* URB was never linked! */ |
| 1127 | goto done; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1128 | |
Alan Stern | dccf4a4 | 2005-12-17 17:58:46 -0500 | [diff] [blame] | 1129 | /* Remove Isochronous TDs from the frame list ASAP */ |
Alan Stern | 4de7d2c | 2006-05-05 16:26:58 -0400 | [diff] [blame] | 1130 | if (urbp->qh->type == USB_ENDPOINT_XFER_ISOC) |
Alan Stern | dccf4a4 | 2005-12-17 17:58:46 -0500 | [diff] [blame] | 1131 | uhci_unlink_isochronous_tds(uhci, urb); |
| 1132 | uhci_unlink_qh(uhci, urbp->qh); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1133 | |
| 1134 | done: |
| 1135 | spin_unlock_irqrestore(&uhci->lock, flags); |
| 1136 | return 0; |
| 1137 | } |
| 1138 | |
Alan Stern | 0ed8fee | 2005-12-17 18:02:38 -0500 | [diff] [blame] | 1139 | /* |
| 1140 | * Finish unlinking an URB and give it back |
| 1141 | */ |
| 1142 | static void uhci_giveback_urb(struct uhci_hcd *uhci, struct uhci_qh *qh, |
| 1143 | struct urb *urb, struct pt_regs *regs) |
| 1144 | __releases(uhci->lock) |
| 1145 | __acquires(uhci->lock) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1146 | { |
Alan Stern | 0ed8fee | 2005-12-17 18:02:38 -0500 | [diff] [blame] | 1147 | struct urb_priv *urbp = (struct urb_priv *) urb->hcpriv; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1148 | |
Alan Stern | 0ed8fee | 2005-12-17 18:02:38 -0500 | [diff] [blame] | 1149 | /* Isochronous TDs get unlinked directly from the frame list */ |
Alan Stern | 4de7d2c | 2006-05-05 16:26:58 -0400 | [diff] [blame] | 1150 | if (qh->type == USB_ENDPOINT_XFER_ISOC) |
Alan Stern | 0ed8fee | 2005-12-17 18:02:38 -0500 | [diff] [blame] | 1151 | uhci_unlink_isochronous_tds(uhci, urb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1152 | |
Alan Stern | 0ed8fee | 2005-12-17 18:02:38 -0500 | [diff] [blame] | 1153 | /* Take the URB off the QH's queue. If the queue is now empty, |
| 1154 | * this is a perfect time for a toggle fixup. */ |
| 1155 | list_del_init(&urbp->node); |
| 1156 | if (list_empty(&qh->queue) && qh->needs_fixup) { |
| 1157 | usb_settoggle(urb->dev, usb_pipeendpoint(urb->pipe), |
| 1158 | usb_pipeout(urb->pipe), qh->initial_toggle); |
| 1159 | qh->needs_fixup = 0; |
| 1160 | } |
| 1161 | |
Alan Stern | 0ed8fee | 2005-12-17 18:02:38 -0500 | [diff] [blame] | 1162 | uhci_free_urb_priv(uhci, urbp); |
| 1163 | |
Alan Stern | 4de7d2c | 2006-05-05 16:26:58 -0400 | [diff] [blame] | 1164 | switch (qh->type) { |
| 1165 | case USB_ENDPOINT_XFER_ISOC: |
Alan Stern | 0ed8fee | 2005-12-17 18:02:38 -0500 | [diff] [blame] | 1166 | /* Release bandwidth for Interrupt or Isoc. transfers */ |
| 1167 | if (urb->bandwidth) |
| 1168 | usb_release_bandwidth(urb->dev, urb, 1); |
| 1169 | break; |
Alan Stern | 4de7d2c | 2006-05-05 16:26:58 -0400 | [diff] [blame] | 1170 | case USB_ENDPOINT_XFER_INT: |
Alan Stern | 0ed8fee | 2005-12-17 18:02:38 -0500 | [diff] [blame] | 1171 | /* Release bandwidth for Interrupt or Isoc. transfers */ |
| 1172 | /* Make sure we don't release if we have a queued URB */ |
| 1173 | if (list_empty(&qh->queue) && urb->bandwidth) |
| 1174 | usb_release_bandwidth(urb->dev, urb, 0); |
| 1175 | else |
| 1176 | /* bandwidth was passed on to queued URB, */ |
| 1177 | /* so don't let usb_unlink_urb() release it */ |
| 1178 | urb->bandwidth = 0; |
| 1179 | break; |
| 1180 | } |
| 1181 | |
| 1182 | spin_unlock(&uhci->lock); |
| 1183 | usb_hcd_giveback_urb(uhci_to_hcd(uhci), urb, regs); |
| 1184 | spin_lock(&uhci->lock); |
| 1185 | |
| 1186 | /* If the queue is now empty, we can unlink the QH and give up its |
| 1187 | * reserved bandwidth. */ |
| 1188 | if (list_empty(&qh->queue)) { |
| 1189 | uhci_unlink_qh(uhci, qh); |
| 1190 | |
| 1191 | /* Bandwidth stuff not yet implemented */ |
| 1192 | } |
| 1193 | } |
| 1194 | |
| 1195 | /* |
| 1196 | * Scan the URBs in a QH's queue |
| 1197 | */ |
| 1198 | #define QH_FINISHED_UNLINKING(qh) \ |
| 1199 | (qh->state == QH_STATE_UNLINKING && \ |
| 1200 | uhci->frame_number + uhci->is_stopped != qh->unlink_frame) |
| 1201 | |
| 1202 | static void uhci_scan_qh(struct uhci_hcd *uhci, struct uhci_qh *qh, |
| 1203 | struct pt_regs *regs) |
| 1204 | { |
| 1205 | struct urb_priv *urbp; |
| 1206 | struct urb *urb; |
| 1207 | int status; |
| 1208 | |
| 1209 | while (!list_empty(&qh->queue)) { |
| 1210 | urbp = list_entry(qh->queue.next, struct urb_priv, node); |
| 1211 | urb = urbp->urb; |
| 1212 | |
Alan Stern | b186900 | 2006-05-12 11:14:25 -0400 | [diff] [blame] | 1213 | if (qh->type == USB_ENDPOINT_XFER_ISOC) |
Alan Stern | 0ed8fee | 2005-12-17 18:02:38 -0500 | [diff] [blame] | 1214 | status = uhci_result_isochronous(uhci, urb); |
Alan Stern | b186900 | 2006-05-12 11:14:25 -0400 | [diff] [blame] | 1215 | else |
Alan Stern | 0ed8fee | 2005-12-17 18:02:38 -0500 | [diff] [blame] | 1216 | status = uhci_result_common(uhci, urb); |
Alan Stern | 0ed8fee | 2005-12-17 18:02:38 -0500 | [diff] [blame] | 1217 | if (status == -EINPROGRESS) |
| 1218 | break; |
| 1219 | |
| 1220 | spin_lock(&urb->lock); |
| 1221 | if (urb->status == -EINPROGRESS) /* Not dequeued */ |
| 1222 | urb->status = status; |
| 1223 | else |
Alan Stern | 2775562 | 2006-05-05 16:32:02 -0400 | [diff] [blame] | 1224 | status = ECONNRESET; /* Not -ECONNRESET */ |
Alan Stern | 0ed8fee | 2005-12-17 18:02:38 -0500 | [diff] [blame] | 1225 | spin_unlock(&urb->lock); |
| 1226 | |
| 1227 | /* Dequeued but completed URBs can't be given back unless |
| 1228 | * the QH is stopped or has finished unlinking. */ |
Alan Stern | 2775562 | 2006-05-05 16:32:02 -0400 | [diff] [blame] | 1229 | if (status == ECONNRESET) { |
| 1230 | if (QH_FINISHED_UNLINKING(qh)) |
| 1231 | qh->is_stopped = 1; |
| 1232 | else if (!qh->is_stopped) |
| 1233 | return; |
| 1234 | } |
Alan Stern | 0ed8fee | 2005-12-17 18:02:38 -0500 | [diff] [blame] | 1235 | |
| 1236 | uhci_giveback_urb(uhci, qh, urb, regs); |
Alan Stern | 2775562 | 2006-05-05 16:32:02 -0400 | [diff] [blame] | 1237 | if (status < 0) |
Alan Stern | 0ed8fee | 2005-12-17 18:02:38 -0500 | [diff] [blame] | 1238 | break; |
| 1239 | } |
| 1240 | |
| 1241 | /* If the QH is neither stopped nor finished unlinking (normal case), |
| 1242 | * our work here is done. */ |
Alan Stern | 2775562 | 2006-05-05 16:32:02 -0400 | [diff] [blame] | 1243 | if (QH_FINISHED_UNLINKING(qh)) |
| 1244 | qh->is_stopped = 1; |
| 1245 | else if (!qh->is_stopped) |
Alan Stern | 0ed8fee | 2005-12-17 18:02:38 -0500 | [diff] [blame] | 1246 | return; |
| 1247 | |
| 1248 | /* Otherwise give back each of the dequeued URBs */ |
Alan Stern | 2775562 | 2006-05-05 16:32:02 -0400 | [diff] [blame] | 1249 | restart: |
Alan Stern | 0ed8fee | 2005-12-17 18:02:38 -0500 | [diff] [blame] | 1250 | list_for_each_entry(urbp, &qh->queue, node) { |
| 1251 | urb = urbp->urb; |
| 1252 | if (urb->status != -EINPROGRESS) { |
Alan Stern | a0b458b | 2006-05-12 11:23:19 -0400 | [diff] [blame] | 1253 | uhci_cleanup_queue(qh, urb); |
Alan Stern | 0ed8fee | 2005-12-17 18:02:38 -0500 | [diff] [blame] | 1254 | uhci_giveback_urb(uhci, qh, urb, regs); |
| 1255 | goto restart; |
| 1256 | } |
| 1257 | } |
| 1258 | qh->is_stopped = 0; |
| 1259 | |
| 1260 | /* There are no more dequeued URBs. If there are still URBs on the |
| 1261 | * queue, the QH can now be re-activated. */ |
| 1262 | if (!list_empty(&qh->queue)) { |
| 1263 | if (qh->needs_fixup) |
| 1264 | uhci_fixup_toggles(qh, 0); |
Alan Stern | 84afddd | 2006-05-12 11:35:45 -0400 | [diff] [blame] | 1265 | |
| 1266 | /* If the first URB on the queue wants FSBR but its time |
| 1267 | * limit has expired, set the next TD to interrupt on |
| 1268 | * completion before reactivating the QH. */ |
| 1269 | urbp = list_entry(qh->queue.next, struct urb_priv, node); |
| 1270 | if (urbp->fsbr && qh->wait_expired) { |
| 1271 | struct uhci_td *td = list_entry(urbp->td_list.next, |
| 1272 | struct uhci_td, list); |
| 1273 | |
| 1274 | td->status |= __cpu_to_le32(TD_CTRL_IOC); |
| 1275 | } |
| 1276 | |
Alan Stern | 0ed8fee | 2005-12-17 18:02:38 -0500 | [diff] [blame] | 1277 | uhci_activate_qh(uhci, qh); |
| 1278 | } |
| 1279 | |
| 1280 | /* The queue is empty. The QH can become idle if it is fully |
| 1281 | * unlinked. */ |
| 1282 | else if (QH_FINISHED_UNLINKING(qh)) |
| 1283 | uhci_make_qh_idle(uhci, qh); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1284 | } |
| 1285 | |
Alan Stern | 0ed8fee | 2005-12-17 18:02:38 -0500 | [diff] [blame] | 1286 | /* |
Alan Stern | 84afddd | 2006-05-12 11:35:45 -0400 | [diff] [blame] | 1287 | * Check for queues that have made some forward progress. |
| 1288 | * Returns 0 if the queue is not Isochronous, is ACTIVE, and |
| 1289 | * has not advanced since last examined; 1 otherwise. |
Alan Stern | b761d9d | 2006-05-12 11:41:59 -0400 | [diff] [blame^] | 1290 | * |
| 1291 | * Early Intel controllers have a bug which causes qh->element sometimes |
| 1292 | * not to advance when a TD completes successfully. The queue remains |
| 1293 | * stuck on the inactive completed TD. We detect such cases and advance |
| 1294 | * the element pointer by hand. |
Alan Stern | 84afddd | 2006-05-12 11:35:45 -0400 | [diff] [blame] | 1295 | */ |
| 1296 | static int uhci_advance_check(struct uhci_hcd *uhci, struct uhci_qh *qh) |
| 1297 | { |
| 1298 | struct urb_priv *urbp = NULL; |
| 1299 | struct uhci_td *td; |
| 1300 | int ret = 1; |
| 1301 | unsigned status; |
| 1302 | |
| 1303 | if (qh->type == USB_ENDPOINT_XFER_ISOC) |
| 1304 | return ret; |
| 1305 | |
| 1306 | /* Treat an UNLINKING queue as though it hasn't advanced. |
| 1307 | * This is okay because reactivation will treat it as though |
| 1308 | * it has advanced, and if it is going to become IDLE then |
| 1309 | * this doesn't matter anyway. Furthermore it's possible |
| 1310 | * for an UNLINKING queue not to have any URBs at all, or |
| 1311 | * for its first URB not to have any TDs (if it was dequeued |
| 1312 | * just as it completed). So it's not easy in any case to |
| 1313 | * test whether such queues have advanced. */ |
| 1314 | if (qh->state != QH_STATE_ACTIVE) { |
| 1315 | urbp = NULL; |
| 1316 | status = 0; |
| 1317 | |
| 1318 | } else { |
| 1319 | urbp = list_entry(qh->queue.next, struct urb_priv, node); |
| 1320 | td = list_entry(urbp->td_list.next, struct uhci_td, list); |
| 1321 | status = td_status(td); |
| 1322 | if (!(status & TD_CTRL_ACTIVE)) { |
| 1323 | |
| 1324 | /* We're okay, the queue has advanced */ |
| 1325 | qh->wait_expired = 0; |
| 1326 | qh->advance_jiffies = jiffies; |
| 1327 | return ret; |
| 1328 | } |
| 1329 | ret = 0; |
| 1330 | } |
| 1331 | |
| 1332 | /* The queue hasn't advanced; check for timeout */ |
| 1333 | if (!qh->wait_expired && time_after(jiffies, |
| 1334 | qh->advance_jiffies + QH_WAIT_TIMEOUT)) { |
Alan Stern | b761d9d | 2006-05-12 11:41:59 -0400 | [diff] [blame^] | 1335 | |
| 1336 | /* Detect the Intel bug and work around it */ |
| 1337 | if (qh->post_td && qh_element(qh) == |
| 1338 | cpu_to_le32(qh->post_td->dma_handle)) { |
| 1339 | qh->element = qh->post_td->link; |
| 1340 | qh->advance_jiffies = jiffies; |
| 1341 | return 1; |
| 1342 | } |
| 1343 | |
Alan Stern | 84afddd | 2006-05-12 11:35:45 -0400 | [diff] [blame] | 1344 | qh->wait_expired = 1; |
| 1345 | |
| 1346 | /* If the current URB wants FSBR, unlink it temporarily |
| 1347 | * so that we can safely set the next TD to interrupt on |
| 1348 | * completion. That way we'll know as soon as the queue |
| 1349 | * starts moving again. */ |
| 1350 | if (urbp && urbp->fsbr && !(status & TD_CTRL_IOC)) |
| 1351 | uhci_unlink_qh(uhci, qh); |
| 1352 | } |
| 1353 | return ret; |
| 1354 | } |
| 1355 | |
| 1356 | /* |
Alan Stern | 0ed8fee | 2005-12-17 18:02:38 -0500 | [diff] [blame] | 1357 | * Process events in the schedule, but only in one thread at a time |
| 1358 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1359 | static void uhci_scan_schedule(struct uhci_hcd *uhci, struct pt_regs *regs) |
| 1360 | { |
Alan Stern | 0ed8fee | 2005-12-17 18:02:38 -0500 | [diff] [blame] | 1361 | int i; |
| 1362 | struct uhci_qh *qh; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1363 | |
| 1364 | /* Don't allow re-entrant calls */ |
| 1365 | if (uhci->scan_in_progress) { |
| 1366 | uhci->need_rescan = 1; |
| 1367 | return; |
| 1368 | } |
| 1369 | uhci->scan_in_progress = 1; |
Alan Stern | 84afddd | 2006-05-12 11:35:45 -0400 | [diff] [blame] | 1370 | rescan: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1371 | uhci->need_rescan = 0; |
| 1372 | |
Alan Stern | 6c1b445 | 2005-04-21 16:04:58 -0400 | [diff] [blame] | 1373 | uhci_clear_next_interrupt(uhci); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1374 | uhci_get_current_frame_number(uhci); |
| 1375 | |
Alan Stern | 0ed8fee | 2005-12-17 18:02:38 -0500 | [diff] [blame] | 1376 | /* Go through all the QH queues and process the URBs in each one */ |
| 1377 | for (i = 0; i < UHCI_NUM_SKELQH - 1; ++i) { |
| 1378 | uhci->next_qh = list_entry(uhci->skelqh[i]->node.next, |
| 1379 | struct uhci_qh, node); |
| 1380 | while ((qh = uhci->next_qh) != uhci->skelqh[i]) { |
| 1381 | uhci->next_qh = list_entry(qh->node.next, |
| 1382 | struct uhci_qh, node); |
Alan Stern | 84afddd | 2006-05-12 11:35:45 -0400 | [diff] [blame] | 1383 | |
| 1384 | if (uhci_advance_check(uhci, qh)) { |
| 1385 | uhci_scan_qh(uhci, qh, regs); |
| 1386 | if (qh->state == QH_STATE_ACTIVE) |
| 1387 | uhci_qh_wants_fsbr(uhci, qh); |
| 1388 | } |
Alan Stern | 0ed8fee | 2005-12-17 18:02:38 -0500 | [diff] [blame] | 1389 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1390 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1391 | |
| 1392 | if (uhci->need_rescan) |
| 1393 | goto rescan; |
| 1394 | uhci->scan_in_progress = 0; |
| 1395 | |
Alan Stern | 84afddd | 2006-05-12 11:35:45 -0400 | [diff] [blame] | 1396 | if (uhci->fsbr_is_on && time_after(jiffies, |
| 1397 | uhci->fsbr_jiffies + FSBR_OFF_DELAY)) |
| 1398 | uhci_fsbr_off(uhci); |
| 1399 | |
Alan Stern | 04538a2 | 2006-05-12 11:29:04 -0400 | [diff] [blame] | 1400 | if (list_empty(&uhci->skel_unlink_qh->node)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1401 | uhci_clear_next_interrupt(uhci); |
| 1402 | else |
| 1403 | uhci_set_next_interrupt(uhci); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1404 | } |