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