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