Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2001-2004 by David Brownell |
| 3 | * Copyright (c) 2003 Michal Sojka, for high-speed iso transfers |
David Brownell | 53bd6a6 | 2006-08-30 14:50:06 -0700 | [diff] [blame] | 4 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5 | * This program is free software; you can redistribute it and/or modify it |
| 6 | * under the terms of the GNU General Public License as published by the |
| 7 | * Free Software Foundation; either version 2 of the License, or (at your |
| 8 | * option) any later version. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, but |
| 11 | * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY |
| 12 | * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 13 | * for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this program; if not, write to the Free Software Foundation, |
| 17 | * Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
| 18 | */ |
| 19 | |
| 20 | /* this file is part of ehci-hcd.c */ |
| 21 | |
| 22 | /*-------------------------------------------------------------------------*/ |
| 23 | |
| 24 | /* |
| 25 | * EHCI scheduled transaction support: interrupt, iso, split iso |
| 26 | * These are called "periodic" transactions in the EHCI spec. |
| 27 | * |
| 28 | * Note that for interrupt transfers, the QH/QTD manipulation is shared |
| 29 | * with the "asynchronous" transaction support (control/bulk transfers). |
| 30 | * The only real difference is in how interrupt transfers are scheduled. |
| 31 | * |
| 32 | * For ISO, we make an "iso_stream" head to serve the same role as a QH. |
| 33 | * It keeps track of every ITD (or SITD) that's linked, and holds enough |
| 34 | * pre-calculated schedule data to make appending to the queue be quick. |
| 35 | */ |
| 36 | |
| 37 | static int ehci_get_frame (struct usb_hcd *hcd); |
| 38 | |
| 39 | /*-------------------------------------------------------------------------*/ |
| 40 | |
| 41 | /* |
| 42 | * periodic_next_shadow - return "next" pointer on shadow list |
| 43 | * @periodic: host pointer to qh/itd/sitd |
| 44 | * @tag: hardware tag for type of this record |
| 45 | */ |
| 46 | static union ehci_shadow * |
Stefan Roese | 6dbd682 | 2007-05-01 09:29:37 -0700 | [diff] [blame] | 47 | periodic_next_shadow(struct ehci_hcd *ehci, union ehci_shadow *periodic, |
| 48 | __hc32 tag) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 49 | { |
Stefan Roese | 6dbd682 | 2007-05-01 09:29:37 -0700 | [diff] [blame] | 50 | switch (hc32_to_cpu(ehci, tag)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 51 | case Q_TYPE_QH: |
| 52 | return &periodic->qh->qh_next; |
| 53 | case Q_TYPE_FSTN: |
| 54 | return &periodic->fstn->fstn_next; |
| 55 | case Q_TYPE_ITD: |
| 56 | return &periodic->itd->itd_next; |
| 57 | // case Q_TYPE_SITD: |
| 58 | default: |
| 59 | return &periodic->sitd->sitd_next; |
| 60 | } |
| 61 | } |
| 62 | |
Alek Du | 3807e26 | 2009-07-14 07:23:29 +0800 | [diff] [blame] | 63 | static __hc32 * |
| 64 | shadow_next_periodic(struct ehci_hcd *ehci, union ehci_shadow *periodic, |
| 65 | __hc32 tag) |
| 66 | { |
| 67 | switch (hc32_to_cpu(ehci, tag)) { |
| 68 | /* our ehci_shadow.qh is actually software part */ |
| 69 | case Q_TYPE_QH: |
| 70 | return &periodic->qh->hw->hw_next; |
| 71 | /* others are hw parts */ |
| 72 | default: |
| 73 | return periodic->hw_next; |
| 74 | } |
| 75 | } |
| 76 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 77 | /* caller must hold ehci->lock */ |
| 78 | static void periodic_unlink (struct ehci_hcd *ehci, unsigned frame, void *ptr) |
| 79 | { |
Stefan Roese | 6dbd682 | 2007-05-01 09:29:37 -0700 | [diff] [blame] | 80 | union ehci_shadow *prev_p = &ehci->pshadow[frame]; |
| 81 | __hc32 *hw_p = &ehci->periodic[frame]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 82 | union ehci_shadow here = *prev_p; |
| 83 | |
| 84 | /* find predecessor of "ptr"; hw and shadow lists are in sync */ |
| 85 | while (here.ptr && here.ptr != ptr) { |
Stefan Roese | 6dbd682 | 2007-05-01 09:29:37 -0700 | [diff] [blame] | 86 | prev_p = periodic_next_shadow(ehci, prev_p, |
| 87 | Q_NEXT_TYPE(ehci, *hw_p)); |
Alek Du | 3807e26 | 2009-07-14 07:23:29 +0800 | [diff] [blame] | 88 | hw_p = shadow_next_periodic(ehci, &here, |
| 89 | Q_NEXT_TYPE(ehci, *hw_p)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 90 | here = *prev_p; |
| 91 | } |
| 92 | /* an interrupt entry (at list end) could have been shared */ |
| 93 | if (!here.ptr) |
| 94 | return; |
| 95 | |
| 96 | /* update shadow and hardware lists ... the old "next" pointers |
| 97 | * from ptr may still be in use, the caller updates them. |
| 98 | */ |
Stefan Roese | 6dbd682 | 2007-05-01 09:29:37 -0700 | [diff] [blame] | 99 | *prev_p = *periodic_next_shadow(ehci, &here, |
| 100 | Q_NEXT_TYPE(ehci, *hw_p)); |
Alek Du | 3807e26 | 2009-07-14 07:23:29 +0800 | [diff] [blame] | 101 | *hw_p = *shadow_next_periodic(ehci, &here, Q_NEXT_TYPE(ehci, *hw_p)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | /* how many of the uframe's 125 usecs are allocated? */ |
| 105 | static unsigned short |
| 106 | periodic_usecs (struct ehci_hcd *ehci, unsigned frame, unsigned uframe) |
| 107 | { |
Stefan Roese | 6dbd682 | 2007-05-01 09:29:37 -0700 | [diff] [blame] | 108 | __hc32 *hw_p = &ehci->periodic [frame]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 109 | union ehci_shadow *q = &ehci->pshadow [frame]; |
| 110 | unsigned usecs = 0; |
Alek Du | 3807e26 | 2009-07-14 07:23:29 +0800 | [diff] [blame] | 111 | struct ehci_qh_hw *hw; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 112 | |
| 113 | while (q->ptr) { |
Stefan Roese | 6dbd682 | 2007-05-01 09:29:37 -0700 | [diff] [blame] | 114 | switch (hc32_to_cpu(ehci, Q_NEXT_TYPE(ehci, *hw_p))) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 115 | case Q_TYPE_QH: |
Alek Du | 3807e26 | 2009-07-14 07:23:29 +0800 | [diff] [blame] | 116 | hw = q->qh->hw; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 117 | /* is it in the S-mask? */ |
Alek Du | 3807e26 | 2009-07-14 07:23:29 +0800 | [diff] [blame] | 118 | if (hw->hw_info2 & cpu_to_hc32(ehci, 1 << uframe)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 119 | usecs += q->qh->usecs; |
| 120 | /* ... or C-mask? */ |
Alek Du | 3807e26 | 2009-07-14 07:23:29 +0800 | [diff] [blame] | 121 | if (hw->hw_info2 & cpu_to_hc32(ehci, |
Stefan Roese | 6dbd682 | 2007-05-01 09:29:37 -0700 | [diff] [blame] | 122 | 1 << (8 + uframe))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 123 | usecs += q->qh->c_usecs; |
Alek Du | 3807e26 | 2009-07-14 07:23:29 +0800 | [diff] [blame] | 124 | hw_p = &hw->hw_next; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 125 | q = &q->qh->qh_next; |
| 126 | break; |
| 127 | // case Q_TYPE_FSTN: |
| 128 | default: |
| 129 | /* for "save place" FSTNs, count the relevant INTR |
| 130 | * bandwidth from the previous frame |
| 131 | */ |
Stefan Roese | 6dbd682 | 2007-05-01 09:29:37 -0700 | [diff] [blame] | 132 | if (q->fstn->hw_prev != EHCI_LIST_END(ehci)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 133 | ehci_dbg (ehci, "ignoring FSTN cost ...\n"); |
| 134 | } |
| 135 | hw_p = &q->fstn->hw_next; |
| 136 | q = &q->fstn->fstn_next; |
| 137 | break; |
| 138 | case Q_TYPE_ITD: |
Karsten Wiese | 3b6fcfd | 2007-12-30 21:55:05 -0800 | [diff] [blame] | 139 | if (q->itd->hw_transaction[uframe]) |
| 140 | usecs += q->itd->stream->usecs; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 141 | hw_p = &q->itd->hw_next; |
| 142 | q = &q->itd->itd_next; |
| 143 | break; |
| 144 | case Q_TYPE_SITD: |
| 145 | /* is it in the S-mask? (count SPLIT, DATA) */ |
Stefan Roese | 6dbd682 | 2007-05-01 09:29:37 -0700 | [diff] [blame] | 146 | if (q->sitd->hw_uframe & cpu_to_hc32(ehci, |
| 147 | 1 << uframe)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 148 | if (q->sitd->hw_fullspeed_ep & |
Stefan Roese | 6dbd682 | 2007-05-01 09:29:37 -0700 | [diff] [blame] | 149 | cpu_to_hc32(ehci, 1<<31)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 150 | usecs += q->sitd->stream->usecs; |
| 151 | else /* worst case for OUT start-split */ |
| 152 | usecs += HS_USECS_ISO (188); |
| 153 | } |
| 154 | |
| 155 | /* ... C-mask? (count CSPLIT, DATA) */ |
| 156 | if (q->sitd->hw_uframe & |
Stefan Roese | 6dbd682 | 2007-05-01 09:29:37 -0700 | [diff] [blame] | 157 | cpu_to_hc32(ehci, 1 << (8 + uframe))) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 158 | /* worst case for IN complete-split */ |
| 159 | usecs += q->sitd->stream->c_usecs; |
| 160 | } |
| 161 | |
| 162 | hw_p = &q->sitd->hw_next; |
| 163 | q = &q->sitd->sitd_next; |
| 164 | break; |
| 165 | } |
| 166 | } |
| 167 | #ifdef DEBUG |
| 168 | if (usecs > 100) |
| 169 | ehci_err (ehci, "uframe %d sched overrun: %d usecs\n", |
| 170 | frame * 8 + uframe, usecs); |
| 171 | #endif |
| 172 | return usecs; |
| 173 | } |
| 174 | |
| 175 | /*-------------------------------------------------------------------------*/ |
| 176 | |
| 177 | static int same_tt (struct usb_device *dev1, struct usb_device *dev2) |
| 178 | { |
| 179 | if (!dev1->tt || !dev2->tt) |
| 180 | return 0; |
| 181 | if (dev1->tt != dev2->tt) |
| 182 | return 0; |
| 183 | if (dev1->tt->multi) |
| 184 | return dev1->ttport == dev2->ttport; |
| 185 | else |
| 186 | return 1; |
| 187 | } |
| 188 | |
Dan Streetman | ba47f66 | 2006-05-24 09:39:16 -0700 | [diff] [blame] | 189 | #ifdef CONFIG_USB_EHCI_TT_NEWSCHED |
| 190 | |
| 191 | /* Which uframe does the low/fullspeed transfer start in? |
| 192 | * |
| 193 | * The parameter is the mask of ssplits in "H-frame" terms |
| 194 | * and this returns the transfer start uframe in "B-frame" terms, |
| 195 | * which allows both to match, e.g. a ssplit in "H-frame" uframe 0 |
| 196 | * will cause a transfer in "B-frame" uframe 0. "B-frames" lag |
| 197 | * "H-frames" by 1 uframe. See the EHCI spec sec 4.5 and figure 4.7. |
| 198 | */ |
Stefan Roese | 6dbd682 | 2007-05-01 09:29:37 -0700 | [diff] [blame] | 199 | static inline unsigned char tt_start_uframe(struct ehci_hcd *ehci, __hc32 mask) |
Dan Streetman | ba47f66 | 2006-05-24 09:39:16 -0700 | [diff] [blame] | 200 | { |
Stefan Roese | 6dbd682 | 2007-05-01 09:29:37 -0700 | [diff] [blame] | 201 | unsigned char smask = QH_SMASK & hc32_to_cpu(ehci, mask); |
Dan Streetman | ba47f66 | 2006-05-24 09:39:16 -0700 | [diff] [blame] | 202 | if (!smask) { |
| 203 | ehci_err(ehci, "invalid empty smask!\n"); |
| 204 | /* uframe 7 can't have bw so this will indicate failure */ |
| 205 | return 7; |
| 206 | } |
| 207 | return ffs(smask) - 1; |
| 208 | } |
| 209 | |
| 210 | static const unsigned char |
| 211 | max_tt_usecs[] = { 125, 125, 125, 125, 125, 125, 30, 0 }; |
| 212 | |
| 213 | /* carryover low/fullspeed bandwidth that crosses uframe boundries */ |
| 214 | static inline void carryover_tt_bandwidth(unsigned short tt_usecs[8]) |
| 215 | { |
| 216 | int i; |
| 217 | for (i=0; i<7; i++) { |
| 218 | if (max_tt_usecs[i] < tt_usecs[i]) { |
| 219 | tt_usecs[i+1] += tt_usecs[i] - max_tt_usecs[i]; |
| 220 | tt_usecs[i] = max_tt_usecs[i]; |
| 221 | } |
| 222 | } |
| 223 | } |
| 224 | |
| 225 | /* How many of the tt's periodic downstream 1000 usecs are allocated? |
| 226 | * |
| 227 | * While this measures the bandwidth in terms of usecs/uframe, |
| 228 | * the low/fullspeed bus has no notion of uframes, so any particular |
| 229 | * low/fullspeed transfer can "carry over" from one uframe to the next, |
| 230 | * since the TT just performs downstream transfers in sequence. |
| 231 | * |
Joe Perches | dc0d5c1 | 2007-12-17 11:40:18 -0800 | [diff] [blame] | 232 | * For example two separate 100 usec transfers can start in the same uframe, |
Dan Streetman | ba47f66 | 2006-05-24 09:39:16 -0700 | [diff] [blame] | 233 | * and the second one would "carry over" 75 usecs into the next uframe. |
| 234 | */ |
| 235 | static void |
| 236 | periodic_tt_usecs ( |
| 237 | struct ehci_hcd *ehci, |
| 238 | struct usb_device *dev, |
| 239 | unsigned frame, |
| 240 | unsigned short tt_usecs[8] |
| 241 | ) |
| 242 | { |
Stefan Roese | 6dbd682 | 2007-05-01 09:29:37 -0700 | [diff] [blame] | 243 | __hc32 *hw_p = &ehci->periodic [frame]; |
Dan Streetman | ba47f66 | 2006-05-24 09:39:16 -0700 | [diff] [blame] | 244 | union ehci_shadow *q = &ehci->pshadow [frame]; |
| 245 | unsigned char uf; |
| 246 | |
| 247 | memset(tt_usecs, 0, 16); |
| 248 | |
| 249 | while (q->ptr) { |
Stefan Roese | 6dbd682 | 2007-05-01 09:29:37 -0700 | [diff] [blame] | 250 | switch (hc32_to_cpu(ehci, Q_NEXT_TYPE(ehci, *hw_p))) { |
Dan Streetman | ba47f66 | 2006-05-24 09:39:16 -0700 | [diff] [blame] | 251 | case Q_TYPE_ITD: |
| 252 | hw_p = &q->itd->hw_next; |
| 253 | q = &q->itd->itd_next; |
| 254 | continue; |
| 255 | case Q_TYPE_QH: |
| 256 | if (same_tt(dev, q->qh->dev)) { |
Alek Du | 3807e26 | 2009-07-14 07:23:29 +0800 | [diff] [blame] | 257 | uf = tt_start_uframe(ehci, q->qh->hw->hw_info2); |
Dan Streetman | ba47f66 | 2006-05-24 09:39:16 -0700 | [diff] [blame] | 258 | tt_usecs[uf] += q->qh->tt_usecs; |
| 259 | } |
Alek Du | 3807e26 | 2009-07-14 07:23:29 +0800 | [diff] [blame] | 260 | hw_p = &q->qh->hw->hw_next; |
Dan Streetman | ba47f66 | 2006-05-24 09:39:16 -0700 | [diff] [blame] | 261 | q = &q->qh->qh_next; |
| 262 | continue; |
| 263 | case Q_TYPE_SITD: |
| 264 | if (same_tt(dev, q->sitd->urb->dev)) { |
| 265 | uf = tt_start_uframe(ehci, q->sitd->hw_uframe); |
| 266 | tt_usecs[uf] += q->sitd->stream->tt_usecs; |
| 267 | } |
| 268 | hw_p = &q->sitd->hw_next; |
| 269 | q = &q->sitd->sitd_next; |
| 270 | continue; |
| 271 | // case Q_TYPE_FSTN: |
| 272 | default: |
Stefan Roese | 6dbd682 | 2007-05-01 09:29:37 -0700 | [diff] [blame] | 273 | ehci_dbg(ehci, "ignoring periodic frame %d FSTN\n", |
| 274 | frame); |
Dan Streetman | ba47f66 | 2006-05-24 09:39:16 -0700 | [diff] [blame] | 275 | hw_p = &q->fstn->hw_next; |
| 276 | q = &q->fstn->fstn_next; |
| 277 | } |
| 278 | } |
| 279 | |
| 280 | carryover_tt_bandwidth(tt_usecs); |
| 281 | |
| 282 | if (max_tt_usecs[7] < tt_usecs[7]) |
| 283 | ehci_err(ehci, "frame %d tt sched overrun: %d usecs\n", |
| 284 | frame, tt_usecs[7] - max_tt_usecs[7]); |
| 285 | } |
| 286 | |
| 287 | /* |
| 288 | * Return true if the device's tt's downstream bus is available for a |
| 289 | * periodic transfer of the specified length (usecs), starting at the |
| 290 | * specified frame/uframe. Note that (as summarized in section 11.19 |
| 291 | * of the usb 2.0 spec) TTs can buffer multiple transactions for each |
| 292 | * uframe. |
| 293 | * |
| 294 | * The uframe parameter is when the fullspeed/lowspeed transfer |
| 295 | * should be executed in "B-frame" terms, which is the same as the |
| 296 | * highspeed ssplit's uframe (which is in "H-frame" terms). For example |
| 297 | * a ssplit in "H-frame" 0 causes a transfer in "B-frame" 0. |
| 298 | * See the EHCI spec sec 4.5 and fig 4.7. |
| 299 | * |
| 300 | * This checks if the full/lowspeed bus, at the specified starting uframe, |
| 301 | * has the specified bandwidth available, according to rules listed |
| 302 | * in USB 2.0 spec section 11.18.1 fig 11-60. |
| 303 | * |
| 304 | * This does not check if the transfer would exceed the max ssplit |
| 305 | * limit of 16, specified in USB 2.0 spec section 11.18.4 requirement #4, |
| 306 | * since proper scheduling limits ssplits to less than 16 per uframe. |
| 307 | */ |
| 308 | static int tt_available ( |
| 309 | struct ehci_hcd *ehci, |
| 310 | unsigned period, |
| 311 | struct usb_device *dev, |
| 312 | unsigned frame, |
| 313 | unsigned uframe, |
| 314 | u16 usecs |
| 315 | ) |
| 316 | { |
| 317 | if ((period == 0) || (uframe >= 7)) /* error */ |
| 318 | return 0; |
| 319 | |
| 320 | for (; frame < ehci->periodic_size; frame += period) { |
| 321 | unsigned short tt_usecs[8]; |
| 322 | |
| 323 | periodic_tt_usecs (ehci, dev, frame, tt_usecs); |
| 324 | |
| 325 | ehci_vdbg(ehci, "tt frame %d check %d usecs start uframe %d in" |
| 326 | " schedule %d/%d/%d/%d/%d/%d/%d/%d\n", |
| 327 | frame, usecs, uframe, |
| 328 | tt_usecs[0], tt_usecs[1], tt_usecs[2], tt_usecs[3], |
| 329 | tt_usecs[4], tt_usecs[5], tt_usecs[6], tt_usecs[7]); |
| 330 | |
| 331 | if (max_tt_usecs[uframe] <= tt_usecs[uframe]) { |
| 332 | ehci_vdbg(ehci, "frame %d uframe %d fully scheduled\n", |
| 333 | frame, uframe); |
| 334 | return 0; |
| 335 | } |
| 336 | |
| 337 | /* special case for isoc transfers larger than 125us: |
| 338 | * the first and each subsequent fully used uframe |
| 339 | * must be empty, so as to not illegally delay |
| 340 | * already scheduled transactions |
| 341 | */ |
| 342 | if (125 < usecs) { |
Dan Streetman | c065c60 | 2009-04-21 13:37:12 -0400 | [diff] [blame] | 343 | int ufs = (usecs / 125); |
Dan Streetman | ba47f66 | 2006-05-24 09:39:16 -0700 | [diff] [blame] | 344 | int i; |
| 345 | for (i = uframe; i < (uframe + ufs) && i < 8; i++) |
| 346 | if (0 < tt_usecs[i]) { |
| 347 | ehci_vdbg(ehci, |
| 348 | "multi-uframe xfer can't fit " |
| 349 | "in frame %d uframe %d\n", |
| 350 | frame, i); |
| 351 | return 0; |
| 352 | } |
| 353 | } |
| 354 | |
| 355 | tt_usecs[uframe] += usecs; |
| 356 | |
| 357 | carryover_tt_bandwidth(tt_usecs); |
| 358 | |
| 359 | /* fail if the carryover pushed bw past the last uframe's limit */ |
| 360 | if (max_tt_usecs[7] < tt_usecs[7]) { |
| 361 | ehci_vdbg(ehci, |
| 362 | "tt unavailable usecs %d frame %d uframe %d\n", |
| 363 | usecs, frame, uframe); |
| 364 | return 0; |
| 365 | } |
| 366 | } |
| 367 | |
| 368 | return 1; |
| 369 | } |
| 370 | |
| 371 | #else |
| 372 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 373 | /* return true iff the device's transaction translator is available |
| 374 | * for a periodic transfer starting at the specified frame, using |
| 375 | * all the uframes in the mask. |
| 376 | */ |
| 377 | static int tt_no_collision ( |
| 378 | struct ehci_hcd *ehci, |
| 379 | unsigned period, |
| 380 | struct usb_device *dev, |
| 381 | unsigned frame, |
| 382 | u32 uf_mask |
| 383 | ) |
| 384 | { |
| 385 | if (period == 0) /* error */ |
| 386 | return 0; |
| 387 | |
| 388 | /* note bandwidth wastage: split never follows csplit |
| 389 | * (different dev or endpoint) until the next uframe. |
| 390 | * calling convention doesn't make that distinction. |
| 391 | */ |
| 392 | for (; frame < ehci->periodic_size; frame += period) { |
| 393 | union ehci_shadow here; |
Stefan Roese | 6dbd682 | 2007-05-01 09:29:37 -0700 | [diff] [blame] | 394 | __hc32 type; |
Alek Du | 3807e26 | 2009-07-14 07:23:29 +0800 | [diff] [blame] | 395 | struct ehci_qh_hw *hw; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 396 | |
| 397 | here = ehci->pshadow [frame]; |
Stefan Roese | 6dbd682 | 2007-05-01 09:29:37 -0700 | [diff] [blame] | 398 | type = Q_NEXT_TYPE(ehci, ehci->periodic [frame]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 399 | while (here.ptr) { |
Stefan Roese | 6dbd682 | 2007-05-01 09:29:37 -0700 | [diff] [blame] | 400 | switch (hc32_to_cpu(ehci, type)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 401 | case Q_TYPE_ITD: |
Stefan Roese | 6dbd682 | 2007-05-01 09:29:37 -0700 | [diff] [blame] | 402 | type = Q_NEXT_TYPE(ehci, here.itd->hw_next); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 403 | here = here.itd->itd_next; |
| 404 | continue; |
| 405 | case Q_TYPE_QH: |
Alek Du | 3807e26 | 2009-07-14 07:23:29 +0800 | [diff] [blame] | 406 | hw = here.qh->hw; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 407 | if (same_tt (dev, here.qh->dev)) { |
| 408 | u32 mask; |
| 409 | |
Stefan Roese | 6dbd682 | 2007-05-01 09:29:37 -0700 | [diff] [blame] | 410 | mask = hc32_to_cpu(ehci, |
Alek Du | 3807e26 | 2009-07-14 07:23:29 +0800 | [diff] [blame] | 411 | hw->hw_info2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 412 | /* "knows" no gap is needed */ |
| 413 | mask |= mask >> 8; |
| 414 | if (mask & uf_mask) |
| 415 | break; |
| 416 | } |
Alek Du | 3807e26 | 2009-07-14 07:23:29 +0800 | [diff] [blame] | 417 | type = Q_NEXT_TYPE(ehci, hw->hw_next); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 418 | here = here.qh->qh_next; |
| 419 | continue; |
| 420 | case Q_TYPE_SITD: |
| 421 | if (same_tt (dev, here.sitd->urb->dev)) { |
| 422 | u16 mask; |
| 423 | |
Stefan Roese | 6dbd682 | 2007-05-01 09:29:37 -0700 | [diff] [blame] | 424 | mask = hc32_to_cpu(ehci, here.sitd |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 425 | ->hw_uframe); |
| 426 | /* FIXME assumes no gap for IN! */ |
| 427 | mask |= mask >> 8; |
| 428 | if (mask & uf_mask) |
| 429 | break; |
| 430 | } |
Stefan Roese | 6dbd682 | 2007-05-01 09:29:37 -0700 | [diff] [blame] | 431 | type = Q_NEXT_TYPE(ehci, here.sitd->hw_next); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 432 | here = here.sitd->sitd_next; |
| 433 | continue; |
| 434 | // case Q_TYPE_FSTN: |
| 435 | default: |
| 436 | ehci_dbg (ehci, |
| 437 | "periodic frame %d bogus type %d\n", |
| 438 | frame, type); |
| 439 | } |
| 440 | |
| 441 | /* collision or error */ |
| 442 | return 0; |
| 443 | } |
| 444 | } |
| 445 | |
| 446 | /* no collision */ |
| 447 | return 1; |
| 448 | } |
| 449 | |
Dan Streetman | ba47f66 | 2006-05-24 09:39:16 -0700 | [diff] [blame] | 450 | #endif /* CONFIG_USB_EHCI_TT_NEWSCHED */ |
| 451 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 452 | /*-------------------------------------------------------------------------*/ |
| 453 | |
| 454 | static int enable_periodic (struct ehci_hcd *ehci) |
| 455 | { |
| 456 | u32 cmd; |
| 457 | int status; |
| 458 | |
David Brownell | 01c1714 | 2008-08-26 23:35:04 -0700 | [diff] [blame] | 459 | if (ehci->periodic_sched++) |
| 460 | return 0; |
| 461 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 462 | /* did clearing PSE did take effect yet? |
| 463 | * takes effect only at frame boundaries... |
| 464 | */ |
Karsten Wiese | c765d4c | 2008-02-16 13:44:42 -0800 | [diff] [blame] | 465 | status = handshake_on_error_set_halt(ehci, &ehci->regs->status, |
| 466 | STS_PSS, 0, 9 * 125); |
| 467 | if (status) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 468 | return status; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 469 | |
Benjamin Herrenschmidt | 083522d | 2006-12-15 06:54:08 +1100 | [diff] [blame] | 470 | cmd = ehci_readl(ehci, &ehci->regs->command) | CMD_PSE; |
| 471 | ehci_writel(ehci, cmd, &ehci->regs->command); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 472 | /* posted write ... PSS happens later */ |
| 473 | ehci_to_hcd(ehci)->state = HC_STATE_RUNNING; |
| 474 | |
| 475 | /* make sure ehci_work scans these */ |
Benjamin Herrenschmidt | 083522d | 2006-12-15 06:54:08 +1100 | [diff] [blame] | 476 | ehci->next_uframe = ehci_readl(ehci, &ehci->regs->frame_index) |
| 477 | % (ehci->periodic_size << 3); |
Oliver Neukum | ee4ecb8 | 2009-11-27 15:17:59 +0100 | [diff] [blame] | 478 | if (unlikely(ehci->broken_periodic)) |
| 479 | ehci->last_periodic_enable = ktime_get_real(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 480 | return 0; |
| 481 | } |
| 482 | |
| 483 | static int disable_periodic (struct ehci_hcd *ehci) |
| 484 | { |
| 485 | u32 cmd; |
| 486 | int status; |
| 487 | |
David Brownell | 01c1714 | 2008-08-26 23:35:04 -0700 | [diff] [blame] | 488 | if (--ehci->periodic_sched) |
| 489 | return 0; |
| 490 | |
Oliver Neukum | ee4ecb8 | 2009-11-27 15:17:59 +0100 | [diff] [blame] | 491 | if (unlikely(ehci->broken_periodic)) { |
| 492 | /* delay experimentally determined */ |
| 493 | ktime_t safe = ktime_add_us(ehci->last_periodic_enable, 1000); |
| 494 | ktime_t now = ktime_get_real(); |
| 495 | s64 delay = ktime_us_delta(safe, now); |
| 496 | |
| 497 | if (unlikely(delay > 0)) |
| 498 | udelay(delay); |
| 499 | } |
| 500 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 501 | /* did setting PSE not take effect yet? |
| 502 | * takes effect only at frame boundaries... |
| 503 | */ |
Karsten Wiese | c765d4c | 2008-02-16 13:44:42 -0800 | [diff] [blame] | 504 | status = handshake_on_error_set_halt(ehci, &ehci->regs->status, |
| 505 | STS_PSS, STS_PSS, 9 * 125); |
| 506 | if (status) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 507 | return status; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 508 | |
Benjamin Herrenschmidt | 083522d | 2006-12-15 06:54:08 +1100 | [diff] [blame] | 509 | cmd = ehci_readl(ehci, &ehci->regs->command) & ~CMD_PSE; |
| 510 | ehci_writel(ehci, cmd, &ehci->regs->command); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 511 | /* posted write ... */ |
| 512 | |
Dmitri Epshtein | d63c66d | 2009-12-14 17:17:33 +0200 | [diff] [blame^] | 513 | free_cached_itd_list(ehci); |
| 514 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 515 | ehci->next_uframe = -1; |
| 516 | return 0; |
| 517 | } |
| 518 | |
| 519 | /*-------------------------------------------------------------------------*/ |
| 520 | |
| 521 | /* periodic schedule slots have iso tds (normal or split) first, then a |
| 522 | * sparse tree for active interrupt transfers. |
| 523 | * |
| 524 | * this just links in a qh; caller guarantees uframe masks are set right. |
| 525 | * no FSTN support (yet; ehci 0.96+) |
| 526 | */ |
| 527 | static int qh_link_periodic (struct ehci_hcd *ehci, struct ehci_qh *qh) |
| 528 | { |
| 529 | unsigned i; |
| 530 | unsigned period = qh->period; |
| 531 | |
| 532 | dev_dbg (&qh->dev->dev, |
| 533 | "link qh%d-%04x/%p start %d [%d/%d us]\n", |
Alek Du | 3807e26 | 2009-07-14 07:23:29 +0800 | [diff] [blame] | 534 | period, hc32_to_cpup(ehci, &qh->hw->hw_info2) |
| 535 | & (QH_CMASK | QH_SMASK), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 536 | qh, qh->start, qh->usecs, qh->c_usecs); |
| 537 | |
| 538 | /* high bandwidth, or otherwise every microframe */ |
| 539 | if (period == 0) |
| 540 | period = 1; |
| 541 | |
| 542 | for (i = qh->start; i < ehci->periodic_size; i += period) { |
Stefan Roese | 6dbd682 | 2007-05-01 09:29:37 -0700 | [diff] [blame] | 543 | union ehci_shadow *prev = &ehci->pshadow[i]; |
| 544 | __hc32 *hw_p = &ehci->periodic[i]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 545 | union ehci_shadow here = *prev; |
Stefan Roese | 6dbd682 | 2007-05-01 09:29:37 -0700 | [diff] [blame] | 546 | __hc32 type = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 547 | |
| 548 | /* skip the iso nodes at list head */ |
| 549 | while (here.ptr) { |
Stefan Roese | 6dbd682 | 2007-05-01 09:29:37 -0700 | [diff] [blame] | 550 | type = Q_NEXT_TYPE(ehci, *hw_p); |
| 551 | if (type == cpu_to_hc32(ehci, Q_TYPE_QH)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 552 | break; |
Stefan Roese | 6dbd682 | 2007-05-01 09:29:37 -0700 | [diff] [blame] | 553 | prev = periodic_next_shadow(ehci, prev, type); |
Alek Du | 3807e26 | 2009-07-14 07:23:29 +0800 | [diff] [blame] | 554 | hw_p = shadow_next_periodic(ehci, &here, type); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 555 | here = *prev; |
| 556 | } |
| 557 | |
| 558 | /* sorting each branch by period (slow-->fast) |
| 559 | * enables sharing interior tree nodes |
| 560 | */ |
| 561 | while (here.ptr && qh != here.qh) { |
| 562 | if (qh->period > here.qh->period) |
| 563 | break; |
| 564 | prev = &here.qh->qh_next; |
Alek Du | 3807e26 | 2009-07-14 07:23:29 +0800 | [diff] [blame] | 565 | hw_p = &here.qh->hw->hw_next; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 566 | here = *prev; |
| 567 | } |
| 568 | /* link in this qh, unless some earlier pass did that */ |
| 569 | if (qh != here.qh) { |
| 570 | qh->qh_next = here; |
| 571 | if (here.qh) |
Alek Du | 3807e26 | 2009-07-14 07:23:29 +0800 | [diff] [blame] | 572 | qh->hw->hw_next = *hw_p; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 573 | wmb (); |
| 574 | prev->qh = qh; |
Stefan Roese | 6dbd682 | 2007-05-01 09:29:37 -0700 | [diff] [blame] | 575 | *hw_p = QH_NEXT (ehci, qh->qh_dma); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 576 | } |
| 577 | } |
| 578 | qh->qh_state = QH_STATE_LINKED; |
Alan Stern | ef4638f | 2009-07-31 10:41:40 -0400 | [diff] [blame] | 579 | qh->xacterrs = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 580 | qh_get (qh); |
| 581 | |
| 582 | /* update per-qh bandwidth for usbfs */ |
| 583 | ehci_to_hcd(ehci)->self.bandwidth_allocated += qh->period |
| 584 | ? ((qh->usecs + qh->c_usecs) / qh->period) |
| 585 | : (qh->usecs * 8); |
| 586 | |
| 587 | /* maybe enable periodic schedule processing */ |
David Brownell | 01c1714 | 2008-08-26 23:35:04 -0700 | [diff] [blame] | 588 | return enable_periodic(ehci); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 589 | } |
| 590 | |
David Brownell | 01c1714 | 2008-08-26 23:35:04 -0700 | [diff] [blame] | 591 | static int qh_unlink_periodic(struct ehci_hcd *ehci, struct ehci_qh *qh) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 592 | { |
| 593 | unsigned i; |
| 594 | unsigned period; |
| 595 | |
| 596 | // FIXME: |
| 597 | // IF this isn't high speed |
| 598 | // and this qh is active in the current uframe |
| 599 | // (and overlay token SplitXstate is false?) |
| 600 | // THEN |
Harvey Harrison | 551509d | 2009-02-11 14:11:36 -0800 | [diff] [blame] | 601 | // qh->hw_info1 |= cpu_to_hc32(1 << 7 /* "ignore" */); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 602 | |
| 603 | /* high bandwidth, or otherwise part of every microframe */ |
| 604 | if ((period = qh->period) == 0) |
| 605 | period = 1; |
| 606 | |
| 607 | for (i = qh->start; i < ehci->periodic_size; i += period) |
| 608 | periodic_unlink (ehci, i, qh); |
| 609 | |
| 610 | /* update per-qh bandwidth for usbfs */ |
| 611 | ehci_to_hcd(ehci)->self.bandwidth_allocated -= qh->period |
| 612 | ? ((qh->usecs + qh->c_usecs) / qh->period) |
| 613 | : (qh->usecs * 8); |
| 614 | |
| 615 | dev_dbg (&qh->dev->dev, |
| 616 | "unlink qh%d-%04x/%p start %d [%d/%d us]\n", |
David Brownell | 7dedacf | 2005-08-04 18:06:41 -0700 | [diff] [blame] | 617 | qh->period, |
Alek Du | 3807e26 | 2009-07-14 07:23:29 +0800 | [diff] [blame] | 618 | hc32_to_cpup(ehci, &qh->hw->hw_info2) & (QH_CMASK | QH_SMASK), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 619 | qh, qh->start, qh->usecs, qh->c_usecs); |
| 620 | |
| 621 | /* qh->qh_next still "live" to HC */ |
| 622 | qh->qh_state = QH_STATE_UNLINK; |
| 623 | qh->qh_next.ptr = NULL; |
| 624 | qh_put (qh); |
| 625 | |
| 626 | /* maybe turn off periodic schedule */ |
David Brownell | 01c1714 | 2008-08-26 23:35:04 -0700 | [diff] [blame] | 627 | return disable_periodic(ehci); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 628 | } |
| 629 | |
| 630 | static void intr_deschedule (struct ehci_hcd *ehci, struct ehci_qh *qh) |
| 631 | { |
Alan Stern | a448c9d | 2009-08-19 12:22:44 -0400 | [diff] [blame] | 632 | unsigned wait; |
| 633 | struct ehci_qh_hw *hw = qh->hw; |
| 634 | int rc; |
| 635 | |
| 636 | /* If the QH isn't linked then there's nothing we can do |
| 637 | * unless we were called during a giveback, in which case |
| 638 | * qh_completions() has to deal with it. |
| 639 | */ |
| 640 | if (qh->qh_state != QH_STATE_LINKED) { |
| 641 | if (qh->qh_state == QH_STATE_COMPLETING) |
| 642 | qh->needs_rescan = 1; |
| 643 | return; |
| 644 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 645 | |
| 646 | qh_unlink_periodic (ehci, qh); |
| 647 | |
| 648 | /* simple/paranoid: always delay, expecting the HC needs to read |
| 649 | * qh->hw_next or finish a writeback after SPLIT/CSPLIT ... and |
| 650 | * expect khubd to clean up after any CSPLITs we won't issue. |
| 651 | * active high speed queues may need bigger delays... |
| 652 | */ |
| 653 | if (list_empty (&qh->qtd_list) |
Stefan Roese | 6dbd682 | 2007-05-01 09:29:37 -0700 | [diff] [blame] | 654 | || (cpu_to_hc32(ehci, QH_CMASK) |
Alek Du | 3807e26 | 2009-07-14 07:23:29 +0800 | [diff] [blame] | 655 | & hw->hw_info2) != 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 656 | wait = 2; |
| 657 | else |
| 658 | wait = 55; /* worst case: 3 * 1024 */ |
| 659 | |
| 660 | udelay (wait); |
| 661 | qh->qh_state = QH_STATE_IDLE; |
Alek Du | 3807e26 | 2009-07-14 07:23:29 +0800 | [diff] [blame] | 662 | hw->hw_next = EHCI_LIST_END(ehci); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 663 | wmb (); |
Alan Stern | a448c9d | 2009-08-19 12:22:44 -0400 | [diff] [blame] | 664 | |
| 665 | qh_completions(ehci, qh); |
| 666 | |
| 667 | /* reschedule QH iff another request is queued */ |
| 668 | if (!list_empty(&qh->qtd_list) && |
| 669 | HC_IS_RUNNING(ehci_to_hcd(ehci)->state)) { |
| 670 | rc = qh_schedule(ehci, qh); |
| 671 | |
| 672 | /* An error here likely indicates handshake failure |
| 673 | * or no space left in the schedule. Neither fault |
| 674 | * should happen often ... |
| 675 | * |
| 676 | * FIXME kill the now-dysfunctional queued urbs |
| 677 | */ |
| 678 | if (rc != 0) |
| 679 | ehci_err(ehci, "can't reschedule qh %p, err %d\n", |
| 680 | qh, rc); |
| 681 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 682 | } |
| 683 | |
| 684 | /*-------------------------------------------------------------------------*/ |
| 685 | |
| 686 | static int check_period ( |
David Brownell | 53bd6a6 | 2006-08-30 14:50:06 -0700 | [diff] [blame] | 687 | struct ehci_hcd *ehci, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 688 | unsigned frame, |
| 689 | unsigned uframe, |
| 690 | unsigned period, |
| 691 | unsigned usecs |
| 692 | ) { |
| 693 | int claimed; |
| 694 | |
| 695 | /* complete split running into next frame? |
| 696 | * given FSTN support, we could sometimes check... |
| 697 | */ |
| 698 | if (uframe >= 8) |
| 699 | return 0; |
| 700 | |
| 701 | /* |
| 702 | * 80% periodic == 100 usec/uframe available |
David Brownell | 53bd6a6 | 2006-08-30 14:50:06 -0700 | [diff] [blame] | 703 | * convert "usecs we need" to "max already claimed" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 704 | */ |
| 705 | usecs = 100 - usecs; |
| 706 | |
| 707 | /* we "know" 2 and 4 uframe intervals were rejected; so |
| 708 | * for period 0, check _every_ microframe in the schedule. |
| 709 | */ |
| 710 | if (unlikely (period == 0)) { |
| 711 | do { |
| 712 | for (uframe = 0; uframe < 7; uframe++) { |
| 713 | claimed = periodic_usecs (ehci, frame, uframe); |
| 714 | if (claimed > usecs) |
| 715 | return 0; |
| 716 | } |
| 717 | } while ((frame += 1) < ehci->periodic_size); |
| 718 | |
| 719 | /* just check the specified uframe, at that period */ |
| 720 | } else { |
| 721 | do { |
| 722 | claimed = periodic_usecs (ehci, frame, uframe); |
| 723 | if (claimed > usecs) |
| 724 | return 0; |
| 725 | } while ((frame += period) < ehci->periodic_size); |
| 726 | } |
| 727 | |
| 728 | // success! |
| 729 | return 1; |
| 730 | } |
| 731 | |
| 732 | static int check_intr_schedule ( |
David Brownell | 53bd6a6 | 2006-08-30 14:50:06 -0700 | [diff] [blame] | 733 | struct ehci_hcd *ehci, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 734 | unsigned frame, |
| 735 | unsigned uframe, |
| 736 | const struct ehci_qh *qh, |
Stefan Roese | 6dbd682 | 2007-05-01 09:29:37 -0700 | [diff] [blame] | 737 | __hc32 *c_maskp |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 738 | ) |
| 739 | { |
David Brownell | 53bd6a6 | 2006-08-30 14:50:06 -0700 | [diff] [blame] | 740 | int retval = -ENOSPC; |
Dan Streetman | ba47f66 | 2006-05-24 09:39:16 -0700 | [diff] [blame] | 741 | u8 mask = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 742 | |
| 743 | if (qh->c_usecs && uframe >= 6) /* FSTN territory? */ |
| 744 | goto done; |
| 745 | |
| 746 | if (!check_period (ehci, frame, uframe, qh->period, qh->usecs)) |
| 747 | goto done; |
| 748 | if (!qh->c_usecs) { |
| 749 | retval = 0; |
| 750 | *c_maskp = 0; |
| 751 | goto done; |
| 752 | } |
| 753 | |
Dan Streetman | ba47f66 | 2006-05-24 09:39:16 -0700 | [diff] [blame] | 754 | #ifdef CONFIG_USB_EHCI_TT_NEWSCHED |
| 755 | if (tt_available (ehci, qh->period, qh->dev, frame, uframe, |
| 756 | qh->tt_usecs)) { |
| 757 | unsigned i; |
| 758 | |
| 759 | /* TODO : this may need FSTN for SSPLIT in uframe 5. */ |
| 760 | for (i=uframe+1; i<8 && i<uframe+4; i++) |
| 761 | if (!check_period (ehci, frame, i, |
| 762 | qh->period, qh->c_usecs)) |
| 763 | goto done; |
| 764 | else |
| 765 | mask |= 1 << i; |
| 766 | |
| 767 | retval = 0; |
| 768 | |
Stefan Roese | 6dbd682 | 2007-05-01 09:29:37 -0700 | [diff] [blame] | 769 | *c_maskp = cpu_to_hc32(ehci, mask << 8); |
Dan Streetman | ba47f66 | 2006-05-24 09:39:16 -0700 | [diff] [blame] | 770 | } |
| 771 | #else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 772 | /* Make sure this tt's buffer is also available for CSPLITs. |
| 773 | * We pessimize a bit; probably the typical full speed case |
| 774 | * doesn't need the second CSPLIT. |
David Brownell | 53bd6a6 | 2006-08-30 14:50:06 -0700 | [diff] [blame] | 775 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 776 | * NOTE: both SPLIT and CSPLIT could be checked in just |
| 777 | * one smart pass... |
| 778 | */ |
| 779 | mask = 0x03 << (uframe + qh->gap_uf); |
Stefan Roese | 6dbd682 | 2007-05-01 09:29:37 -0700 | [diff] [blame] | 780 | *c_maskp = cpu_to_hc32(ehci, mask << 8); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 781 | |
| 782 | mask |= 1 << uframe; |
| 783 | if (tt_no_collision (ehci, qh->period, qh->dev, frame, mask)) { |
| 784 | if (!check_period (ehci, frame, uframe + qh->gap_uf + 1, |
| 785 | qh->period, qh->c_usecs)) |
| 786 | goto done; |
| 787 | if (!check_period (ehci, frame, uframe + qh->gap_uf, |
| 788 | qh->period, qh->c_usecs)) |
| 789 | goto done; |
| 790 | retval = 0; |
| 791 | } |
Dan Streetman | ba47f66 | 2006-05-24 09:39:16 -0700 | [diff] [blame] | 792 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 793 | done: |
| 794 | return retval; |
| 795 | } |
| 796 | |
| 797 | /* "first fit" scheduling policy used the first time through, |
| 798 | * or when the previous schedule slot can't be re-used. |
| 799 | */ |
Stefan Roese | 6dbd682 | 2007-05-01 09:29:37 -0700 | [diff] [blame] | 800 | static int qh_schedule(struct ehci_hcd *ehci, struct ehci_qh *qh) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 801 | { |
David Brownell | 53bd6a6 | 2006-08-30 14:50:06 -0700 | [diff] [blame] | 802 | int status; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 803 | unsigned uframe; |
Stefan Roese | 6dbd682 | 2007-05-01 09:29:37 -0700 | [diff] [blame] | 804 | __hc32 c_mask; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 805 | unsigned frame; /* 0..(qh->period - 1), or NO_FRAME */ |
Alek Du | 3807e26 | 2009-07-14 07:23:29 +0800 | [diff] [blame] | 806 | struct ehci_qh_hw *hw = qh->hw; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 807 | |
| 808 | qh_refresh(ehci, qh); |
Alek Du | 3807e26 | 2009-07-14 07:23:29 +0800 | [diff] [blame] | 809 | hw->hw_next = EHCI_LIST_END(ehci); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 810 | frame = qh->start; |
| 811 | |
| 812 | /* reuse the previous schedule slots, if we can */ |
| 813 | if (frame < qh->period) { |
Alek Du | 3807e26 | 2009-07-14 07:23:29 +0800 | [diff] [blame] | 814 | uframe = ffs(hc32_to_cpup(ehci, &hw->hw_info2) & QH_SMASK); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 815 | status = check_intr_schedule (ehci, frame, --uframe, |
| 816 | qh, &c_mask); |
| 817 | } else { |
| 818 | uframe = 0; |
| 819 | c_mask = 0; |
| 820 | status = -ENOSPC; |
| 821 | } |
| 822 | |
| 823 | /* else scan the schedule to find a group of slots such that all |
| 824 | * uframes have enough periodic bandwidth available. |
| 825 | */ |
| 826 | if (status) { |
| 827 | /* "normal" case, uframing flexible except with splits */ |
| 828 | if (qh->period) { |
Alan Stern | 68335e8 | 2009-05-22 17:02:33 -0400 | [diff] [blame] | 829 | int i; |
| 830 | |
| 831 | for (i = qh->period; status && i > 0; --i) { |
| 832 | frame = ++ehci->random_frame % qh->period; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 833 | for (uframe = 0; uframe < 8; uframe++) { |
| 834 | status = check_intr_schedule (ehci, |
| 835 | frame, uframe, qh, |
| 836 | &c_mask); |
| 837 | if (status == 0) |
| 838 | break; |
| 839 | } |
Alan Stern | 68335e8 | 2009-05-22 17:02:33 -0400 | [diff] [blame] | 840 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 841 | |
| 842 | /* qh->period == 0 means every uframe */ |
| 843 | } else { |
| 844 | frame = 0; |
| 845 | status = check_intr_schedule (ehci, 0, 0, qh, &c_mask); |
| 846 | } |
| 847 | if (status) |
| 848 | goto done; |
| 849 | qh->start = frame; |
| 850 | |
| 851 | /* reset S-frame and (maybe) C-frame masks */ |
Alek Du | 3807e26 | 2009-07-14 07:23:29 +0800 | [diff] [blame] | 852 | hw->hw_info2 &= cpu_to_hc32(ehci, ~(QH_CMASK | QH_SMASK)); |
| 853 | hw->hw_info2 |= qh->period |
Stefan Roese | 6dbd682 | 2007-05-01 09:29:37 -0700 | [diff] [blame] | 854 | ? cpu_to_hc32(ehci, 1 << uframe) |
| 855 | : cpu_to_hc32(ehci, QH_SMASK); |
Alek Du | 3807e26 | 2009-07-14 07:23:29 +0800 | [diff] [blame] | 856 | hw->hw_info2 |= c_mask; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 857 | } else |
| 858 | ehci_dbg (ehci, "reused qh %p schedule\n", qh); |
| 859 | |
| 860 | /* stuff into the periodic schedule */ |
David Brownell | 53bd6a6 | 2006-08-30 14:50:06 -0700 | [diff] [blame] | 861 | status = qh_link_periodic (ehci, qh); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 862 | done: |
| 863 | return status; |
| 864 | } |
| 865 | |
| 866 | static int intr_submit ( |
| 867 | struct ehci_hcd *ehci, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 868 | struct urb *urb, |
| 869 | struct list_head *qtd_list, |
Al Viro | 55016f1 | 2005-10-21 03:21:58 -0400 | [diff] [blame] | 870 | gfp_t mem_flags |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 871 | ) { |
| 872 | unsigned epnum; |
| 873 | unsigned long flags; |
| 874 | struct ehci_qh *qh; |
Alan Stern | e9df41c | 2007-08-08 11:48:02 -0400 | [diff] [blame] | 875 | int status; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 876 | struct list_head empty; |
| 877 | |
| 878 | /* get endpoint and transfer/schedule data */ |
Alan Stern | e9df41c | 2007-08-08 11:48:02 -0400 | [diff] [blame] | 879 | epnum = urb->ep->desc.bEndpointAddress; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 880 | |
| 881 | spin_lock_irqsave (&ehci->lock, flags); |
| 882 | |
Benjamin Herrenschmidt | 8de9840 | 2005-11-25 09:59:46 +1100 | [diff] [blame] | 883 | if (unlikely(!test_bit(HCD_FLAG_HW_ACCESSIBLE, |
Stefan Roese | 6dbd682 | 2007-05-01 09:29:37 -0700 | [diff] [blame] | 884 | &ehci_to_hcd(ehci)->flags))) { |
Benjamin Herrenschmidt | 8de9840 | 2005-11-25 09:59:46 +1100 | [diff] [blame] | 885 | status = -ESHUTDOWN; |
Alan Stern | e9df41c | 2007-08-08 11:48:02 -0400 | [diff] [blame] | 886 | goto done_not_linked; |
Benjamin Herrenschmidt | 8de9840 | 2005-11-25 09:59:46 +1100 | [diff] [blame] | 887 | } |
Alan Stern | e9df41c | 2007-08-08 11:48:02 -0400 | [diff] [blame] | 888 | status = usb_hcd_link_urb_to_ep(ehci_to_hcd(ehci), urb); |
| 889 | if (unlikely(status)) |
| 890 | goto done_not_linked; |
Benjamin Herrenschmidt | 8de9840 | 2005-11-25 09:59:46 +1100 | [diff] [blame] | 891 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 892 | /* get qh and force any scheduling errors */ |
| 893 | INIT_LIST_HEAD (&empty); |
Alan Stern | e9df41c | 2007-08-08 11:48:02 -0400 | [diff] [blame] | 894 | qh = qh_append_tds(ehci, urb, &empty, epnum, &urb->ep->hcpriv); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 895 | if (qh == NULL) { |
| 896 | status = -ENOMEM; |
| 897 | goto done; |
| 898 | } |
| 899 | if (qh->qh_state == QH_STATE_IDLE) { |
| 900 | if ((status = qh_schedule (ehci, qh)) != 0) |
| 901 | goto done; |
| 902 | } |
| 903 | |
| 904 | /* then queue the urb's tds to the qh */ |
Alan Stern | e9df41c | 2007-08-08 11:48:02 -0400 | [diff] [blame] | 905 | qh = qh_append_tds(ehci, urb, qtd_list, epnum, &urb->ep->hcpriv); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 906 | BUG_ON (qh == NULL); |
| 907 | |
| 908 | /* ... update usbfs periodic stats */ |
| 909 | ehci_to_hcd(ehci)->self.bandwidth_int_reqs++; |
| 910 | |
| 911 | done: |
Alan Stern | e9df41c | 2007-08-08 11:48:02 -0400 | [diff] [blame] | 912 | if (unlikely(status)) |
| 913 | usb_hcd_unlink_urb_from_ep(ehci_to_hcd(ehci), urb); |
| 914 | done_not_linked: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 915 | spin_unlock_irqrestore (&ehci->lock, flags); |
| 916 | if (status) |
| 917 | qtd_list_free (ehci, urb, qtd_list); |
| 918 | |
| 919 | return status; |
| 920 | } |
| 921 | |
| 922 | /*-------------------------------------------------------------------------*/ |
| 923 | |
| 924 | /* ehci_iso_stream ops work with both ITD and SITD */ |
| 925 | |
| 926 | static struct ehci_iso_stream * |
Al Viro | 55016f1 | 2005-10-21 03:21:58 -0400 | [diff] [blame] | 927 | iso_stream_alloc (gfp_t mem_flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 928 | { |
| 929 | struct ehci_iso_stream *stream; |
| 930 | |
Pekka Enberg | 7b842b6 | 2005-09-06 15:18:34 -0700 | [diff] [blame] | 931 | stream = kzalloc(sizeof *stream, mem_flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 932 | if (likely (stream != NULL)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 933 | INIT_LIST_HEAD(&stream->td_list); |
| 934 | INIT_LIST_HEAD(&stream->free_list); |
| 935 | stream->next_uframe = -1; |
| 936 | stream->refcount = 1; |
| 937 | } |
| 938 | return stream; |
| 939 | } |
| 940 | |
| 941 | static void |
| 942 | iso_stream_init ( |
| 943 | struct ehci_hcd *ehci, |
| 944 | struct ehci_iso_stream *stream, |
| 945 | struct usb_device *dev, |
| 946 | int pipe, |
| 947 | unsigned interval |
| 948 | ) |
| 949 | { |
| 950 | static const u8 smask_out [] = { 0x01, 0x03, 0x07, 0x0f, 0x1f, 0x3f }; |
| 951 | |
| 952 | u32 buf1; |
| 953 | unsigned epnum, maxp; |
| 954 | int is_input; |
| 955 | long bandwidth; |
| 956 | |
| 957 | /* |
| 958 | * this might be a "high bandwidth" highspeed endpoint, |
| 959 | * as encoded in the ep descriptor's wMaxPacket field |
| 960 | */ |
| 961 | epnum = usb_pipeendpoint (pipe); |
| 962 | is_input = usb_pipein (pipe) ? USB_DIR_IN : 0; |
| 963 | maxp = usb_maxpacket(dev, pipe, !is_input); |
| 964 | if (is_input) { |
| 965 | buf1 = (1 << 11); |
| 966 | } else { |
| 967 | buf1 = 0; |
| 968 | } |
| 969 | |
| 970 | /* knows about ITD vs SITD */ |
| 971 | if (dev->speed == USB_SPEED_HIGH) { |
| 972 | unsigned multi = hb_mult(maxp); |
| 973 | |
| 974 | stream->highspeed = 1; |
| 975 | |
| 976 | maxp = max_packet(maxp); |
| 977 | buf1 |= maxp; |
| 978 | maxp *= multi; |
| 979 | |
Stefan Roese | 6dbd682 | 2007-05-01 09:29:37 -0700 | [diff] [blame] | 980 | stream->buf0 = cpu_to_hc32(ehci, (epnum << 8) | dev->devnum); |
| 981 | stream->buf1 = cpu_to_hc32(ehci, buf1); |
| 982 | stream->buf2 = cpu_to_hc32(ehci, multi); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 983 | |
| 984 | /* usbfs wants to report the average usecs per frame tied up |
| 985 | * when transfers on this endpoint are scheduled ... |
| 986 | */ |
| 987 | stream->usecs = HS_USECS_ISO (maxp); |
| 988 | bandwidth = stream->usecs * 8; |
Alan Stern | 372dd6e | 2008-11-12 17:02:57 -0500 | [diff] [blame] | 989 | bandwidth /= interval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 990 | |
| 991 | } else { |
| 992 | u32 addr; |
david-b@pacbell.net | d038420 | 2005-08-13 18:44:58 -0700 | [diff] [blame] | 993 | int think_time; |
Clemens Ladisch | 469d022 | 2006-01-20 13:49:10 -0800 | [diff] [blame] | 994 | int hs_transfers; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 995 | |
| 996 | addr = dev->ttport << 24; |
| 997 | if (!ehci_is_TDI(ehci) |
| 998 | || (dev->tt->hub != |
| 999 | ehci_to_hcd(ehci)->self.root_hub)) |
| 1000 | addr |= dev->tt->hub->devnum << 16; |
| 1001 | addr |= epnum << 8; |
| 1002 | addr |= dev->devnum; |
| 1003 | stream->usecs = HS_USECS_ISO (maxp); |
david-b@pacbell.net | d038420 | 2005-08-13 18:44:58 -0700 | [diff] [blame] | 1004 | think_time = dev->tt ? dev->tt->think_time : 0; |
| 1005 | stream->tt_usecs = NS_TO_US (think_time + usb_calc_bus_time ( |
| 1006 | dev->speed, is_input, 1, maxp)); |
Clemens Ladisch | 469d022 | 2006-01-20 13:49:10 -0800 | [diff] [blame] | 1007 | hs_transfers = max (1u, (maxp + 187) / 188); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1008 | if (is_input) { |
| 1009 | u32 tmp; |
| 1010 | |
| 1011 | addr |= 1 << 31; |
| 1012 | stream->c_usecs = stream->usecs; |
| 1013 | stream->usecs = HS_USECS_ISO (1); |
| 1014 | stream->raw_mask = 1; |
| 1015 | |
Clemens Ladisch | 469d022 | 2006-01-20 13:49:10 -0800 | [diff] [blame] | 1016 | /* c-mask as specified in USB 2.0 11.18.4 3.c */ |
| 1017 | tmp = (1 << (hs_transfers + 2)) - 1; |
| 1018 | stream->raw_mask |= tmp << (8 + 2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1019 | } else |
Clemens Ladisch | 469d022 | 2006-01-20 13:49:10 -0800 | [diff] [blame] | 1020 | stream->raw_mask = smask_out [hs_transfers - 1]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1021 | bandwidth = stream->usecs + stream->c_usecs; |
Alan Stern | 372dd6e | 2008-11-12 17:02:57 -0500 | [diff] [blame] | 1022 | bandwidth /= interval << 3; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1023 | |
| 1024 | /* stream->splits gets created from raw_mask later */ |
Stefan Roese | 6dbd682 | 2007-05-01 09:29:37 -0700 | [diff] [blame] | 1025 | stream->address = cpu_to_hc32(ehci, addr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1026 | } |
| 1027 | stream->bandwidth = bandwidth; |
| 1028 | |
| 1029 | stream->udev = dev; |
| 1030 | |
| 1031 | stream->bEndpointAddress = is_input | epnum; |
| 1032 | stream->interval = interval; |
| 1033 | stream->maxp = maxp; |
| 1034 | } |
| 1035 | |
| 1036 | static void |
| 1037 | iso_stream_put(struct ehci_hcd *ehci, struct ehci_iso_stream *stream) |
| 1038 | { |
| 1039 | stream->refcount--; |
| 1040 | |
| 1041 | /* free whenever just a dev->ep reference remains. |
| 1042 | * not like a QH -- no persistent state (toggle, halt) |
| 1043 | */ |
| 1044 | if (stream->refcount == 1) { |
| 1045 | int is_in; |
| 1046 | |
| 1047 | // BUG_ON (!list_empty(&stream->td_list)); |
| 1048 | |
| 1049 | while (!list_empty (&stream->free_list)) { |
| 1050 | struct list_head *entry; |
| 1051 | |
| 1052 | entry = stream->free_list.next; |
| 1053 | list_del (entry); |
| 1054 | |
| 1055 | /* knows about ITD vs SITD */ |
| 1056 | if (stream->highspeed) { |
| 1057 | struct ehci_itd *itd; |
| 1058 | |
| 1059 | itd = list_entry (entry, struct ehci_itd, |
| 1060 | itd_list); |
| 1061 | dma_pool_free (ehci->itd_pool, itd, |
| 1062 | itd->itd_dma); |
| 1063 | } else { |
| 1064 | struct ehci_sitd *sitd; |
| 1065 | |
| 1066 | sitd = list_entry (entry, struct ehci_sitd, |
| 1067 | sitd_list); |
| 1068 | dma_pool_free (ehci->sitd_pool, sitd, |
| 1069 | sitd->sitd_dma); |
| 1070 | } |
| 1071 | } |
| 1072 | |
| 1073 | is_in = (stream->bEndpointAddress & USB_DIR_IN) ? 0x10 : 0; |
| 1074 | stream->bEndpointAddress &= 0x0f; |
Karsten Wiese | 9aa09d2 | 2009-02-08 16:07:58 -0800 | [diff] [blame] | 1075 | if (stream->ep) |
| 1076 | stream->ep->hcpriv = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1077 | |
| 1078 | if (stream->rescheduled) { |
| 1079 | ehci_info (ehci, "ep%d%s-iso rescheduled " |
| 1080 | "%lu times in %lu seconds\n", |
| 1081 | stream->bEndpointAddress, is_in ? "in" : "out", |
| 1082 | stream->rescheduled, |
| 1083 | ((jiffies - stream->start)/HZ) |
| 1084 | ); |
| 1085 | } |
| 1086 | |
| 1087 | kfree(stream); |
| 1088 | } |
| 1089 | } |
| 1090 | |
| 1091 | static inline struct ehci_iso_stream * |
| 1092 | iso_stream_get (struct ehci_iso_stream *stream) |
| 1093 | { |
| 1094 | if (likely (stream != NULL)) |
| 1095 | stream->refcount++; |
| 1096 | return stream; |
| 1097 | } |
| 1098 | |
| 1099 | static struct ehci_iso_stream * |
| 1100 | iso_stream_find (struct ehci_hcd *ehci, struct urb *urb) |
| 1101 | { |
| 1102 | unsigned epnum; |
| 1103 | struct ehci_iso_stream *stream; |
| 1104 | struct usb_host_endpoint *ep; |
| 1105 | unsigned long flags; |
| 1106 | |
| 1107 | epnum = usb_pipeendpoint (urb->pipe); |
| 1108 | if (usb_pipein(urb->pipe)) |
| 1109 | ep = urb->dev->ep_in[epnum]; |
| 1110 | else |
| 1111 | ep = urb->dev->ep_out[epnum]; |
| 1112 | |
| 1113 | spin_lock_irqsave (&ehci->lock, flags); |
| 1114 | stream = ep->hcpriv; |
| 1115 | |
| 1116 | if (unlikely (stream == NULL)) { |
| 1117 | stream = iso_stream_alloc(GFP_ATOMIC); |
| 1118 | if (likely (stream != NULL)) { |
| 1119 | /* dev->ep owns the initial refcount */ |
| 1120 | ep->hcpriv = stream; |
| 1121 | stream->ep = ep; |
| 1122 | iso_stream_init(ehci, stream, urb->dev, urb->pipe, |
| 1123 | urb->interval); |
| 1124 | } |
| 1125 | |
| 1126 | /* if dev->ep [epnum] is a QH, info1.maxpacket is nonzero */ |
| 1127 | } else if (unlikely (stream->hw_info1 != 0)) { |
| 1128 | ehci_dbg (ehci, "dev %s ep%d%s, not iso??\n", |
| 1129 | urb->dev->devpath, epnum, |
| 1130 | usb_pipein(urb->pipe) ? "in" : "out"); |
| 1131 | stream = NULL; |
| 1132 | } |
| 1133 | |
| 1134 | /* caller guarantees an eventual matching iso_stream_put */ |
| 1135 | stream = iso_stream_get (stream); |
| 1136 | |
| 1137 | spin_unlock_irqrestore (&ehci->lock, flags); |
| 1138 | return stream; |
| 1139 | } |
| 1140 | |
| 1141 | /*-------------------------------------------------------------------------*/ |
| 1142 | |
| 1143 | /* ehci_iso_sched ops can be ITD-only or SITD-only */ |
| 1144 | |
| 1145 | static struct ehci_iso_sched * |
Al Viro | 55016f1 | 2005-10-21 03:21:58 -0400 | [diff] [blame] | 1146 | iso_sched_alloc (unsigned packets, gfp_t mem_flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1147 | { |
| 1148 | struct ehci_iso_sched *iso_sched; |
| 1149 | int size = sizeof *iso_sched; |
| 1150 | |
| 1151 | size += packets * sizeof (struct ehci_iso_packet); |
Eric Sesterhenn | 80b6ca4 | 2006-02-27 21:29:43 +0100 | [diff] [blame] | 1152 | iso_sched = kzalloc(size, mem_flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1153 | if (likely (iso_sched != NULL)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1154 | INIT_LIST_HEAD (&iso_sched->td_list); |
| 1155 | } |
| 1156 | return iso_sched; |
| 1157 | } |
| 1158 | |
| 1159 | static inline void |
Stefan Roese | 6dbd682 | 2007-05-01 09:29:37 -0700 | [diff] [blame] | 1160 | itd_sched_init( |
| 1161 | struct ehci_hcd *ehci, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1162 | struct ehci_iso_sched *iso_sched, |
| 1163 | struct ehci_iso_stream *stream, |
| 1164 | struct urb *urb |
| 1165 | ) |
| 1166 | { |
| 1167 | unsigned i; |
| 1168 | dma_addr_t dma = urb->transfer_dma; |
| 1169 | |
| 1170 | /* how many uframes are needed for these transfers */ |
| 1171 | iso_sched->span = urb->number_of_packets * stream->interval; |
| 1172 | |
| 1173 | /* figure out per-uframe itd fields that we'll need later |
| 1174 | * when we fit new itds into the schedule. |
| 1175 | */ |
| 1176 | for (i = 0; i < urb->number_of_packets; i++) { |
| 1177 | struct ehci_iso_packet *uframe = &iso_sched->packet [i]; |
| 1178 | unsigned length; |
| 1179 | dma_addr_t buf; |
| 1180 | u32 trans; |
| 1181 | |
| 1182 | length = urb->iso_frame_desc [i].length; |
| 1183 | buf = dma + urb->iso_frame_desc [i].offset; |
| 1184 | |
| 1185 | trans = EHCI_ISOC_ACTIVE; |
| 1186 | trans |= buf & 0x0fff; |
| 1187 | if (unlikely (((i + 1) == urb->number_of_packets)) |
| 1188 | && !(urb->transfer_flags & URB_NO_INTERRUPT)) |
| 1189 | trans |= EHCI_ITD_IOC; |
| 1190 | trans |= length << 16; |
Stefan Roese | 6dbd682 | 2007-05-01 09:29:37 -0700 | [diff] [blame] | 1191 | uframe->transaction = cpu_to_hc32(ehci, trans); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1192 | |
David Brownell | 7707857 | 2005-05-28 10:46:18 -0700 | [diff] [blame] | 1193 | /* might need to cross a buffer page within a uframe */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1194 | uframe->bufp = (buf & ~(u64)0x0fff); |
| 1195 | buf += length; |
| 1196 | if (unlikely ((uframe->bufp != (buf & ~(u64)0x0fff)))) |
| 1197 | uframe->cross = 1; |
| 1198 | } |
| 1199 | } |
| 1200 | |
| 1201 | static void |
| 1202 | iso_sched_free ( |
| 1203 | struct ehci_iso_stream *stream, |
| 1204 | struct ehci_iso_sched *iso_sched |
| 1205 | ) |
| 1206 | { |
| 1207 | if (!iso_sched) |
| 1208 | return; |
| 1209 | // caller must hold ehci->lock! |
| 1210 | list_splice (&iso_sched->td_list, &stream->free_list); |
| 1211 | kfree (iso_sched); |
| 1212 | } |
| 1213 | |
| 1214 | static int |
| 1215 | itd_urb_transaction ( |
| 1216 | struct ehci_iso_stream *stream, |
| 1217 | struct ehci_hcd *ehci, |
| 1218 | struct urb *urb, |
Al Viro | 55016f1 | 2005-10-21 03:21:58 -0400 | [diff] [blame] | 1219 | gfp_t mem_flags |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1220 | ) |
| 1221 | { |
| 1222 | struct ehci_itd *itd; |
| 1223 | dma_addr_t itd_dma; |
| 1224 | int i; |
| 1225 | unsigned num_itds; |
| 1226 | struct ehci_iso_sched *sched; |
| 1227 | unsigned long flags; |
| 1228 | |
| 1229 | sched = iso_sched_alloc (urb->number_of_packets, mem_flags); |
| 1230 | if (unlikely (sched == NULL)) |
| 1231 | return -ENOMEM; |
| 1232 | |
Stefan Roese | 6dbd682 | 2007-05-01 09:29:37 -0700 | [diff] [blame] | 1233 | itd_sched_init(ehci, sched, stream, urb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1234 | |
| 1235 | if (urb->interval < 8) |
| 1236 | num_itds = 1 + (sched->span + 7) / 8; |
| 1237 | else |
| 1238 | num_itds = urb->number_of_packets; |
| 1239 | |
| 1240 | /* allocate/init ITDs */ |
| 1241 | spin_lock_irqsave (&ehci->lock, flags); |
| 1242 | for (i = 0; i < num_itds; i++) { |
| 1243 | |
| 1244 | /* free_list.next might be cache-hot ... but maybe |
| 1245 | * the HC caches it too. avoid that issue for now. |
| 1246 | */ |
| 1247 | |
| 1248 | /* prefer previously-allocated itds */ |
| 1249 | if (likely (!list_empty(&stream->free_list))) { |
| 1250 | itd = list_entry (stream->free_list.prev, |
Stefan Roese | 6dbd682 | 2007-05-01 09:29:37 -0700 | [diff] [blame] | 1251 | struct ehci_itd, itd_list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1252 | list_del (&itd->itd_list); |
| 1253 | itd_dma = itd->itd_dma; |
Karsten Wiese | 3d01f0f | 2008-02-19 12:31:49 -0800 | [diff] [blame] | 1254 | } else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1255 | spin_unlock_irqrestore (&ehci->lock, flags); |
| 1256 | itd = dma_pool_alloc (ehci->itd_pool, mem_flags, |
| 1257 | &itd_dma); |
| 1258 | spin_lock_irqsave (&ehci->lock, flags); |
Karsten Wiese | 3d01f0f | 2008-02-19 12:31:49 -0800 | [diff] [blame] | 1259 | if (!itd) { |
| 1260 | iso_sched_free(stream, sched); |
| 1261 | spin_unlock_irqrestore(&ehci->lock, flags); |
| 1262 | return -ENOMEM; |
| 1263 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1264 | } |
| 1265 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1266 | memset (itd, 0, sizeof *itd); |
| 1267 | itd->itd_dma = itd_dma; |
| 1268 | list_add (&itd->itd_list, &sched->td_list); |
| 1269 | } |
| 1270 | spin_unlock_irqrestore (&ehci->lock, flags); |
| 1271 | |
| 1272 | /* temporarily store schedule info in hcpriv */ |
| 1273 | urb->hcpriv = sched; |
| 1274 | urb->error_count = 0; |
| 1275 | return 0; |
| 1276 | } |
| 1277 | |
| 1278 | /*-------------------------------------------------------------------------*/ |
| 1279 | |
| 1280 | static inline int |
| 1281 | itd_slot_ok ( |
| 1282 | struct ehci_hcd *ehci, |
| 1283 | u32 mod, |
| 1284 | u32 uframe, |
| 1285 | u8 usecs, |
| 1286 | u32 period |
| 1287 | ) |
| 1288 | { |
| 1289 | uframe %= period; |
| 1290 | do { |
| 1291 | /* can't commit more than 80% periodic == 100 usec */ |
| 1292 | if (periodic_usecs (ehci, uframe >> 3, uframe & 0x7) |
| 1293 | > (100 - usecs)) |
| 1294 | return 0; |
| 1295 | |
| 1296 | /* we know urb->interval is 2^N uframes */ |
| 1297 | uframe += period; |
| 1298 | } while (uframe < mod); |
| 1299 | return 1; |
| 1300 | } |
| 1301 | |
| 1302 | static inline int |
| 1303 | sitd_slot_ok ( |
| 1304 | struct ehci_hcd *ehci, |
| 1305 | u32 mod, |
| 1306 | struct ehci_iso_stream *stream, |
| 1307 | u32 uframe, |
| 1308 | struct ehci_iso_sched *sched, |
| 1309 | u32 period_uframes |
| 1310 | ) |
| 1311 | { |
| 1312 | u32 mask, tmp; |
| 1313 | u32 frame, uf; |
| 1314 | |
| 1315 | mask = stream->raw_mask << (uframe & 7); |
| 1316 | |
| 1317 | /* for IN, don't wrap CSPLIT into the next frame */ |
| 1318 | if (mask & ~0xffff) |
| 1319 | return 0; |
| 1320 | |
| 1321 | /* this multi-pass logic is simple, but performance may |
| 1322 | * suffer when the schedule data isn't cached. |
| 1323 | */ |
| 1324 | |
| 1325 | /* check bandwidth */ |
| 1326 | uframe %= period_uframes; |
| 1327 | do { |
| 1328 | u32 max_used; |
| 1329 | |
| 1330 | frame = uframe >> 3; |
| 1331 | uf = uframe & 7; |
| 1332 | |
Dan Streetman | ba47f66 | 2006-05-24 09:39:16 -0700 | [diff] [blame] | 1333 | #ifdef CONFIG_USB_EHCI_TT_NEWSCHED |
| 1334 | /* The tt's fullspeed bus bandwidth must be available. |
| 1335 | * tt_available scheduling guarantees 10+% for control/bulk. |
| 1336 | */ |
| 1337 | if (!tt_available (ehci, period_uframes << 3, |
| 1338 | stream->udev, frame, uf, stream->tt_usecs)) |
| 1339 | return 0; |
| 1340 | #else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1341 | /* tt must be idle for start(s), any gap, and csplit. |
| 1342 | * assume scheduling slop leaves 10+% for control/bulk. |
| 1343 | */ |
| 1344 | if (!tt_no_collision (ehci, period_uframes << 3, |
| 1345 | stream->udev, frame, mask)) |
| 1346 | return 0; |
Dan Streetman | ba47f66 | 2006-05-24 09:39:16 -0700 | [diff] [blame] | 1347 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1348 | |
| 1349 | /* check starts (OUT uses more than one) */ |
| 1350 | max_used = 100 - stream->usecs; |
| 1351 | for (tmp = stream->raw_mask & 0xff; tmp; tmp >>= 1, uf++) { |
| 1352 | if (periodic_usecs (ehci, frame, uf) > max_used) |
| 1353 | return 0; |
| 1354 | } |
| 1355 | |
| 1356 | /* for IN, check CSPLIT */ |
| 1357 | if (stream->c_usecs) { |
Clemens Ladisch | 0c73462 | 2006-01-22 10:32:49 -0800 | [diff] [blame] | 1358 | uf = uframe & 7; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1359 | max_used = 100 - stream->c_usecs; |
| 1360 | do { |
| 1361 | tmp = 1 << uf; |
| 1362 | tmp <<= 8; |
| 1363 | if ((stream->raw_mask & tmp) == 0) |
| 1364 | continue; |
| 1365 | if (periodic_usecs (ehci, frame, uf) |
| 1366 | > max_used) |
| 1367 | return 0; |
| 1368 | } while (++uf < 8); |
| 1369 | } |
| 1370 | |
| 1371 | /* we know urb->interval is 2^N uframes */ |
| 1372 | uframe += period_uframes; |
| 1373 | } while (uframe < mod); |
| 1374 | |
Stefan Roese | 6dbd682 | 2007-05-01 09:29:37 -0700 | [diff] [blame] | 1375 | stream->splits = cpu_to_hc32(ehci, stream->raw_mask << (uframe & 7)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1376 | return 1; |
| 1377 | } |
| 1378 | |
| 1379 | /* |
| 1380 | * This scheduler plans almost as far into the future as it has actual |
| 1381 | * periodic schedule slots. (Affected by TUNE_FLS, which defaults to |
| 1382 | * "as small as possible" to be cache-friendlier.) That limits the size |
| 1383 | * transfers you can stream reliably; avoid more than 64 msec per urb. |
| 1384 | * Also avoid queue depths of less than ehci's worst irq latency (affected |
| 1385 | * by the per-urb URB_NO_INTERRUPT hint, the log2_irq_thresh module parameter, |
| 1386 | * and other factors); or more than about 230 msec total (for portability, |
| 1387 | * given EHCI_TUNE_FLS and the slop). Or, write a smarter scheduler! |
| 1388 | */ |
| 1389 | |
Sarah Sharp | d7e055f | 2009-10-27 10:54:49 -0700 | [diff] [blame] | 1390 | #define SCHEDULE_SLOP 80 /* microframes */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1391 | |
| 1392 | static int |
| 1393 | iso_stream_schedule ( |
| 1394 | struct ehci_hcd *ehci, |
| 1395 | struct urb *urb, |
| 1396 | struct ehci_iso_stream *stream |
| 1397 | ) |
| 1398 | { |
Sarah Sharp | dccd574 | 2009-10-27 10:55:05 -0700 | [diff] [blame] | 1399 | u32 now, next, start, period; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1400 | int status; |
| 1401 | unsigned mod = ehci->periodic_size << 3; |
| 1402 | struct ehci_iso_sched *sched = urb->hcpriv; |
Sarah Sharp | dccd574 | 2009-10-27 10:55:05 -0700 | [diff] [blame] | 1403 | struct pci_dev *pdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1404 | |
Sarah Sharp | d7e055f | 2009-10-27 10:54:49 -0700 | [diff] [blame] | 1405 | if (sched->span > (mod - SCHEDULE_SLOP)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1406 | ehci_dbg (ehci, "iso request %p too long\n", urb); |
| 1407 | status = -EFBIG; |
| 1408 | goto fail; |
| 1409 | } |
| 1410 | |
| 1411 | if ((stream->depth + sched->span) > mod) { |
| 1412 | ehci_dbg (ehci, "request %p would overflow (%d+%d>%d)\n", |
| 1413 | urb, stream->depth, sched->span, mod); |
| 1414 | status = -EFBIG; |
| 1415 | goto fail; |
| 1416 | } |
| 1417 | |
Sarah Sharp | 36f2132 | 2009-10-09 12:28:41 -0700 | [diff] [blame] | 1418 | period = urb->interval; |
| 1419 | if (!stream->highspeed) |
| 1420 | period <<= 3; |
| 1421 | |
Benjamin Herrenschmidt | 083522d | 2006-12-15 06:54:08 +1100 | [diff] [blame] | 1422 | now = ehci_readl(ehci, &ehci->regs->frame_index) % mod; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1423 | |
Alan Stern | b40e43f | 2008-05-20 16:59:10 -0400 | [diff] [blame] | 1424 | /* Typical case: reuse current schedule, stream is still active. |
| 1425 | * Hopefully there are no gaps from the host falling behind |
| 1426 | * (irq delays etc), but if there are we'll take the next |
| 1427 | * slot in the schedule, implicitly assuming URB_ISO_ASAP. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1428 | */ |
| 1429 | if (likely (!list_empty (&stream->td_list))) { |
Sarah Sharp | dccd574 | 2009-10-27 10:55:05 -0700 | [diff] [blame] | 1430 | pdev = to_pci_dev(ehci_to_hcd(ehci)->self.controller); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1431 | start = stream->next_uframe; |
Sarah Sharp | dccd574 | 2009-10-27 10:55:05 -0700 | [diff] [blame] | 1432 | |
| 1433 | /* For high speed devices, allow scheduling within the |
| 1434 | * isochronous scheduling threshold. For full speed devices, |
| 1435 | * don't. (Work around for Intel ICH9 bug.) |
| 1436 | */ |
| 1437 | if (!stream->highspeed && |
| 1438 | pdev->vendor == PCI_VENDOR_ID_INTEL) |
| 1439 | next = now + ehci->i_thresh; |
| 1440 | else |
| 1441 | next = now; |
Alan Stern | b40e43f | 2008-05-20 16:59:10 -0400 | [diff] [blame] | 1442 | |
| 1443 | /* Fell behind (by up to twice the slop amount)? */ |
Sarah Sharp | dccd574 | 2009-10-27 10:55:05 -0700 | [diff] [blame] | 1444 | if (((start - next) & (mod - 1)) >= |
| 1445 | mod - 2 * SCHEDULE_SLOP) |
Sarah Sharp | 36f2132 | 2009-10-09 12:28:41 -0700 | [diff] [blame] | 1446 | start += period * DIV_ROUND_UP( |
Sarah Sharp | dccd574 | 2009-10-27 10:55:05 -0700 | [diff] [blame] | 1447 | (next - start) & (mod - 1), |
| 1448 | period); |
Alan Stern | b40e43f | 2008-05-20 16:59:10 -0400 | [diff] [blame] | 1449 | |
| 1450 | /* Tried to schedule too far into the future? */ |
Sarah Sharp | dccd574 | 2009-10-27 10:55:05 -0700 | [diff] [blame] | 1451 | if (unlikely(((start - now) & (mod - 1)) + sched->span |
| 1452 | >= mod - 2 * SCHEDULE_SLOP)) { |
Alan Stern | b40e43f | 2008-05-20 16:59:10 -0400 | [diff] [blame] | 1453 | status = -EFBIG; |
| 1454 | goto fail; |
| 1455 | } |
Sarah Sharp | d555009 | 2009-10-06 13:45:59 -0700 | [diff] [blame] | 1456 | stream->next_uframe = start; |
Alan Stern | b40e43f | 2008-05-20 16:59:10 -0400 | [diff] [blame] | 1457 | goto ready; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1458 | } |
| 1459 | |
| 1460 | /* need to schedule; when's the next (u)frame we could start? |
| 1461 | * this is bigger than ehci->i_thresh allows; scheduling itself |
| 1462 | * isn't free, the slop should handle reasonably slow cpus. it |
| 1463 | * can also help high bandwidth if the dma and irq loads don't |
| 1464 | * jump until after the queue is primed. |
| 1465 | */ |
Sarah Sharp | d7e055f | 2009-10-27 10:54:49 -0700 | [diff] [blame] | 1466 | start = SCHEDULE_SLOP + (now & ~0x07); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1467 | start %= mod; |
| 1468 | stream->next_uframe = start; |
| 1469 | |
| 1470 | /* NOTE: assumes URB_ISO_ASAP, to limit complexity/bugs */ |
| 1471 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1472 | /* find a uframe slot with enough bandwidth */ |
| 1473 | for (; start < (stream->next_uframe + period); start++) { |
| 1474 | int enough_space; |
| 1475 | |
| 1476 | /* check schedule: enough space? */ |
| 1477 | if (stream->highspeed) |
| 1478 | enough_space = itd_slot_ok (ehci, mod, start, |
| 1479 | stream->usecs, period); |
| 1480 | else { |
| 1481 | if ((start % 8) >= 6) |
| 1482 | continue; |
| 1483 | enough_space = sitd_slot_ok (ehci, mod, stream, |
| 1484 | start, sched, period); |
| 1485 | } |
| 1486 | |
| 1487 | /* schedule it here if there's enough bandwidth */ |
| 1488 | if (enough_space) { |
| 1489 | stream->next_uframe = start % mod; |
| 1490 | goto ready; |
| 1491 | } |
| 1492 | } |
| 1493 | |
| 1494 | /* no room in the schedule */ |
| 1495 | ehci_dbg (ehci, "iso %ssched full %p (now %d max %d)\n", |
| 1496 | list_empty (&stream->td_list) ? "" : "re", |
Sarah Sharp | dccd574 | 2009-10-27 10:55:05 -0700 | [diff] [blame] | 1497 | urb, now, now + mod); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1498 | status = -ENOSPC; |
| 1499 | |
| 1500 | fail: |
| 1501 | iso_sched_free (stream, sched); |
| 1502 | urb->hcpriv = NULL; |
| 1503 | return status; |
| 1504 | |
| 1505 | ready: |
| 1506 | /* report high speed start in uframes; full speed, in frames */ |
| 1507 | urb->start_frame = stream->next_uframe; |
| 1508 | if (!stream->highspeed) |
| 1509 | urb->start_frame >>= 3; |
| 1510 | return 0; |
| 1511 | } |
| 1512 | |
| 1513 | /*-------------------------------------------------------------------------*/ |
| 1514 | |
| 1515 | static inline void |
Stefan Roese | 6dbd682 | 2007-05-01 09:29:37 -0700 | [diff] [blame] | 1516 | itd_init(struct ehci_hcd *ehci, struct ehci_iso_stream *stream, |
| 1517 | struct ehci_itd *itd) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1518 | { |
| 1519 | int i; |
| 1520 | |
David Brownell | 7707857 | 2005-05-28 10:46:18 -0700 | [diff] [blame] | 1521 | /* it's been recently zeroed */ |
Stefan Roese | 6dbd682 | 2007-05-01 09:29:37 -0700 | [diff] [blame] | 1522 | itd->hw_next = EHCI_LIST_END(ehci); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1523 | itd->hw_bufp [0] = stream->buf0; |
| 1524 | itd->hw_bufp [1] = stream->buf1; |
| 1525 | itd->hw_bufp [2] = stream->buf2; |
| 1526 | |
| 1527 | for (i = 0; i < 8; i++) |
| 1528 | itd->index[i] = -1; |
| 1529 | |
| 1530 | /* All other fields are filled when scheduling */ |
| 1531 | } |
| 1532 | |
| 1533 | static inline void |
Stefan Roese | 6dbd682 | 2007-05-01 09:29:37 -0700 | [diff] [blame] | 1534 | itd_patch( |
| 1535 | struct ehci_hcd *ehci, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1536 | struct ehci_itd *itd, |
| 1537 | struct ehci_iso_sched *iso_sched, |
| 1538 | unsigned index, |
David Brownell | 7707857 | 2005-05-28 10:46:18 -0700 | [diff] [blame] | 1539 | u16 uframe |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1540 | ) |
| 1541 | { |
| 1542 | struct ehci_iso_packet *uf = &iso_sched->packet [index]; |
| 1543 | unsigned pg = itd->pg; |
| 1544 | |
| 1545 | // BUG_ON (pg == 6 && uf->cross); |
| 1546 | |
| 1547 | uframe &= 0x07; |
| 1548 | itd->index [uframe] = index; |
| 1549 | |
Stefan Roese | 6dbd682 | 2007-05-01 09:29:37 -0700 | [diff] [blame] | 1550 | itd->hw_transaction[uframe] = uf->transaction; |
| 1551 | itd->hw_transaction[uframe] |= cpu_to_hc32(ehci, pg << 12); |
| 1552 | itd->hw_bufp[pg] |= cpu_to_hc32(ehci, uf->bufp & ~(u32)0); |
| 1553 | itd->hw_bufp_hi[pg] |= cpu_to_hc32(ehci, (u32)(uf->bufp >> 32)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1554 | |
| 1555 | /* iso_frame_desc[].offset must be strictly increasing */ |
David Brownell | 7707857 | 2005-05-28 10:46:18 -0700 | [diff] [blame] | 1556 | if (unlikely (uf->cross)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1557 | u64 bufp = uf->bufp + 4096; |
Stefan Roese | 6dbd682 | 2007-05-01 09:29:37 -0700 | [diff] [blame] | 1558 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1559 | itd->pg = ++pg; |
Stefan Roese | 6dbd682 | 2007-05-01 09:29:37 -0700 | [diff] [blame] | 1560 | itd->hw_bufp[pg] |= cpu_to_hc32(ehci, bufp & ~(u32)0); |
| 1561 | itd->hw_bufp_hi[pg] |= cpu_to_hc32(ehci, (u32)(bufp >> 32)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1562 | } |
| 1563 | } |
| 1564 | |
| 1565 | static inline void |
| 1566 | itd_link (struct ehci_hcd *ehci, unsigned frame, struct ehci_itd *itd) |
| 1567 | { |
| 1568 | /* always prepend ITD/SITD ... only QH tree is order-sensitive */ |
| 1569 | itd->itd_next = ehci->pshadow [frame]; |
| 1570 | itd->hw_next = ehci->periodic [frame]; |
| 1571 | ehci->pshadow [frame].itd = itd; |
| 1572 | itd->frame = frame; |
| 1573 | wmb (); |
Stefan Roese | 6dbd682 | 2007-05-01 09:29:37 -0700 | [diff] [blame] | 1574 | ehci->periodic[frame] = cpu_to_hc32(ehci, itd->itd_dma | Q_TYPE_ITD); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1575 | } |
| 1576 | |
| 1577 | /* fit urb's itds into the selected schedule slot; activate as needed */ |
| 1578 | static int |
| 1579 | itd_link_urb ( |
| 1580 | struct ehci_hcd *ehci, |
| 1581 | struct urb *urb, |
| 1582 | unsigned mod, |
| 1583 | struct ehci_iso_stream *stream |
| 1584 | ) |
| 1585 | { |
David Brownell | 7707857 | 2005-05-28 10:46:18 -0700 | [diff] [blame] | 1586 | int packet; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1587 | unsigned next_uframe, uframe, frame; |
| 1588 | struct ehci_iso_sched *iso_sched = urb->hcpriv; |
| 1589 | struct ehci_itd *itd; |
| 1590 | |
| 1591 | next_uframe = stream->next_uframe % mod; |
| 1592 | |
| 1593 | if (unlikely (list_empty(&stream->td_list))) { |
| 1594 | ehci_to_hcd(ehci)->self.bandwidth_allocated |
| 1595 | += stream->bandwidth; |
| 1596 | ehci_vdbg (ehci, |
| 1597 | "schedule devp %s ep%d%s-iso period %d start %d.%d\n", |
| 1598 | urb->dev->devpath, stream->bEndpointAddress & 0x0f, |
| 1599 | (stream->bEndpointAddress & USB_DIR_IN) ? "in" : "out", |
| 1600 | urb->interval, |
| 1601 | next_uframe >> 3, next_uframe & 0x7); |
| 1602 | stream->start = jiffies; |
| 1603 | } |
| 1604 | ehci_to_hcd(ehci)->self.bandwidth_isoc_reqs++; |
| 1605 | |
| 1606 | /* fill iTDs uframe by uframe */ |
| 1607 | for (packet = 0, itd = NULL; packet < urb->number_of_packets; ) { |
| 1608 | if (itd == NULL) { |
| 1609 | /* ASSERT: we have all necessary itds */ |
| 1610 | // BUG_ON (list_empty (&iso_sched->td_list)); |
| 1611 | |
| 1612 | /* ASSERT: no itds for this endpoint in this uframe */ |
| 1613 | |
| 1614 | itd = list_entry (iso_sched->td_list.next, |
| 1615 | struct ehci_itd, itd_list); |
| 1616 | list_move_tail (&itd->itd_list, &stream->td_list); |
| 1617 | itd->stream = iso_stream_get (stream); |
Karsten Wiese | 508db8c | 2009-02-26 01:47:48 +0100 | [diff] [blame] | 1618 | itd->urb = urb; |
Stefan Roese | 6dbd682 | 2007-05-01 09:29:37 -0700 | [diff] [blame] | 1619 | itd_init (ehci, stream, itd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1620 | } |
| 1621 | |
| 1622 | uframe = next_uframe & 0x07; |
| 1623 | frame = next_uframe >> 3; |
| 1624 | |
Stefan Roese | 6dbd682 | 2007-05-01 09:29:37 -0700 | [diff] [blame] | 1625 | itd_patch(ehci, itd, iso_sched, packet, uframe); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1626 | |
| 1627 | next_uframe += stream->interval; |
| 1628 | stream->depth += stream->interval; |
| 1629 | next_uframe %= mod; |
| 1630 | packet++; |
| 1631 | |
| 1632 | /* link completed itds into the schedule */ |
| 1633 | if (((next_uframe >> 3) != frame) |
| 1634 | || packet == urb->number_of_packets) { |
| 1635 | itd_link (ehci, frame % ehci->periodic_size, itd); |
| 1636 | itd = NULL; |
| 1637 | } |
| 1638 | } |
| 1639 | stream->next_uframe = next_uframe; |
| 1640 | |
| 1641 | /* don't need that schedule data any more */ |
| 1642 | iso_sched_free (stream, iso_sched); |
| 1643 | urb->hcpriv = NULL; |
| 1644 | |
| 1645 | timer_action (ehci, TIMER_IO_WATCHDOG); |
David Brownell | 01c1714 | 2008-08-26 23:35:04 -0700 | [diff] [blame] | 1646 | return enable_periodic(ehci); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1647 | } |
| 1648 | |
| 1649 | #define ISO_ERRS (EHCI_ISOC_BUF_ERR | EHCI_ISOC_BABBLE | EHCI_ISOC_XACTERR) |
| 1650 | |
David Brownell | 30bf54e | 2007-12-16 22:37:40 -0800 | [diff] [blame] | 1651 | /* Process and recycle a completed ITD. Return true iff its urb completed, |
| 1652 | * and hence its completion callback probably added things to the hardware |
| 1653 | * schedule. |
| 1654 | * |
| 1655 | * Note that we carefully avoid recycling this descriptor until after any |
| 1656 | * completion callback runs, so that it won't be reused quickly. That is, |
| 1657 | * assuming (a) no more than two urbs per frame on this endpoint, and also |
| 1658 | * (b) only this endpoint's completions submit URBs. It seems some silicon |
| 1659 | * corrupts things if you reuse completed descriptors very quickly... |
| 1660 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1661 | static unsigned |
| 1662 | itd_complete ( |
| 1663 | struct ehci_hcd *ehci, |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 1664 | struct ehci_itd *itd |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1665 | ) { |
| 1666 | struct urb *urb = itd->urb; |
| 1667 | struct usb_iso_packet_descriptor *desc; |
| 1668 | u32 t; |
| 1669 | unsigned uframe; |
| 1670 | int urb_index = -1; |
| 1671 | struct ehci_iso_stream *stream = itd->stream; |
| 1672 | struct usb_device *dev; |
David Brownell | 30bf54e | 2007-12-16 22:37:40 -0800 | [diff] [blame] | 1673 | unsigned retval = false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1674 | |
| 1675 | /* for each uframe with a packet */ |
| 1676 | for (uframe = 0; uframe < 8; uframe++) { |
| 1677 | if (likely (itd->index[uframe] == -1)) |
| 1678 | continue; |
| 1679 | urb_index = itd->index[uframe]; |
| 1680 | desc = &urb->iso_frame_desc [urb_index]; |
| 1681 | |
Stefan Roese | 6dbd682 | 2007-05-01 09:29:37 -0700 | [diff] [blame] | 1682 | t = hc32_to_cpup(ehci, &itd->hw_transaction [uframe]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1683 | itd->hw_transaction [uframe] = 0; |
| 1684 | stream->depth -= stream->interval; |
| 1685 | |
| 1686 | /* report transfer status */ |
| 1687 | if (unlikely (t & ISO_ERRS)) { |
| 1688 | urb->error_count++; |
| 1689 | if (t & EHCI_ISOC_BUF_ERR) |
| 1690 | desc->status = usb_pipein (urb->pipe) |
| 1691 | ? -ENOSR /* hc couldn't read */ |
| 1692 | : -ECOMM; /* hc couldn't write */ |
| 1693 | else if (t & EHCI_ISOC_BABBLE) |
| 1694 | desc->status = -EOVERFLOW; |
| 1695 | else /* (t & EHCI_ISOC_XACTERR) */ |
| 1696 | desc->status = -EPROTO; |
| 1697 | |
| 1698 | /* HC need not update length with this error */ |
Alan Stern | ec6d67e | 2009-06-29 14:34:59 -0400 | [diff] [blame] | 1699 | if (!(t & EHCI_ISOC_BABBLE)) { |
| 1700 | desc->actual_length = EHCI_ITD_LENGTH(t); |
| 1701 | urb->actual_length += desc->actual_length; |
| 1702 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1703 | } else if (likely ((t & EHCI_ISOC_ACTIVE) == 0)) { |
| 1704 | desc->status = 0; |
Alan Stern | ec6d67e | 2009-06-29 14:34:59 -0400 | [diff] [blame] | 1705 | desc->actual_length = EHCI_ITD_LENGTH(t); |
| 1706 | urb->actual_length += desc->actual_length; |
Alan Stern | b40e43f | 2008-05-20 16:59:10 -0400 | [diff] [blame] | 1707 | } else { |
| 1708 | /* URB was too late */ |
| 1709 | desc->status = -EXDEV; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1710 | } |
| 1711 | } |
| 1712 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1713 | /* handle completion now? */ |
| 1714 | if (likely ((urb_index + 1) != urb->number_of_packets)) |
David Brownell | 30bf54e | 2007-12-16 22:37:40 -0800 | [diff] [blame] | 1715 | goto done; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1716 | |
| 1717 | /* ASSERT: it's really the last itd for this urb |
| 1718 | list_for_each_entry (itd, &stream->td_list, itd_list) |
| 1719 | BUG_ON (itd->urb == urb); |
| 1720 | */ |
| 1721 | |
David Brownell | aa16ca3 | 2007-12-30 23:45:19 -0800 | [diff] [blame] | 1722 | /* give urb back to the driver; completion often (re)submits */ |
Alan Stern | 6a8e87b | 2006-01-19 10:46:27 -0500 | [diff] [blame] | 1723 | dev = urb->dev; |
Alan Stern | 14c04c0 | 2007-08-24 15:40:19 -0400 | [diff] [blame] | 1724 | ehci_urb_done(ehci, urb, 0); |
David Brownell | 30bf54e | 2007-12-16 22:37:40 -0800 | [diff] [blame] | 1725 | retval = true; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1726 | urb = NULL; |
David Brownell | 01c1714 | 2008-08-26 23:35:04 -0700 | [diff] [blame] | 1727 | (void) disable_periodic(ehci); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1728 | ehci_to_hcd(ehci)->self.bandwidth_isoc_reqs--; |
| 1729 | |
Karsten Wiese | 508db8c | 2009-02-26 01:47:48 +0100 | [diff] [blame] | 1730 | if (unlikely(list_is_singular(&stream->td_list))) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1731 | ehci_to_hcd(ehci)->self.bandwidth_allocated |
| 1732 | -= stream->bandwidth; |
| 1733 | ehci_vdbg (ehci, |
| 1734 | "deschedule devp %s ep%d%s-iso\n", |
| 1735 | dev->devpath, stream->bEndpointAddress & 0x0f, |
| 1736 | (stream->bEndpointAddress & USB_DIR_IN) ? "in" : "out"); |
| 1737 | } |
| 1738 | iso_stream_put (ehci, stream); |
Karsten Wiese | 9aa09d2 | 2009-02-08 16:07:58 -0800 | [diff] [blame] | 1739 | |
David Brownell | 30bf54e | 2007-12-16 22:37:40 -0800 | [diff] [blame] | 1740 | done: |
David Brownell | 30bf54e | 2007-12-16 22:37:40 -0800 | [diff] [blame] | 1741 | itd->urb = NULL; |
Karsten Wiese | 9aa09d2 | 2009-02-08 16:07:58 -0800 | [diff] [blame] | 1742 | if (ehci->clock_frame != itd->frame || itd->index[7] != -1) { |
| 1743 | /* OK to recycle this ITD now. */ |
| 1744 | itd->stream = NULL; |
| 1745 | list_move(&itd->itd_list, &stream->free_list); |
| 1746 | iso_stream_put(ehci, stream); |
| 1747 | } else { |
| 1748 | /* HW might remember this ITD, so we can't recycle it yet. |
| 1749 | * Move it to a safe place until a new frame starts. |
| 1750 | */ |
| 1751 | list_move(&itd->itd_list, &ehci->cached_itd_list); |
| 1752 | if (stream->refcount == 2) { |
| 1753 | /* If iso_stream_put() were called here, stream |
| 1754 | * would be freed. Instead, just prevent reuse. |
| 1755 | */ |
| 1756 | stream->ep->hcpriv = NULL; |
| 1757 | stream->ep = NULL; |
| 1758 | } |
| 1759 | } |
David Brownell | 30bf54e | 2007-12-16 22:37:40 -0800 | [diff] [blame] | 1760 | return retval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1761 | } |
| 1762 | |
| 1763 | /*-------------------------------------------------------------------------*/ |
| 1764 | |
Olav Kongas | 5db539e | 2005-06-23 20:25:36 +0300 | [diff] [blame] | 1765 | static int itd_submit (struct ehci_hcd *ehci, struct urb *urb, |
Al Viro | 55016f1 | 2005-10-21 03:21:58 -0400 | [diff] [blame] | 1766 | gfp_t mem_flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1767 | { |
| 1768 | int status = -EINVAL; |
| 1769 | unsigned long flags; |
| 1770 | struct ehci_iso_stream *stream; |
| 1771 | |
| 1772 | /* Get iso_stream head */ |
| 1773 | stream = iso_stream_find (ehci, urb); |
| 1774 | if (unlikely (stream == NULL)) { |
| 1775 | ehci_dbg (ehci, "can't get iso stream\n"); |
| 1776 | return -ENOMEM; |
| 1777 | } |
| 1778 | if (unlikely (urb->interval != stream->interval)) { |
| 1779 | ehci_dbg (ehci, "can't change iso interval %d --> %d\n", |
| 1780 | stream->interval, urb->interval); |
| 1781 | goto done; |
| 1782 | } |
| 1783 | |
| 1784 | #ifdef EHCI_URB_TRACE |
| 1785 | ehci_dbg (ehci, |
| 1786 | "%s %s urb %p ep%d%s len %d, %d pkts %d uframes [%p]\n", |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 1787 | __func__, urb->dev->devpath, urb, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1788 | usb_pipeendpoint (urb->pipe), |
| 1789 | usb_pipein (urb->pipe) ? "in" : "out", |
| 1790 | urb->transfer_buffer_length, |
| 1791 | urb->number_of_packets, urb->interval, |
| 1792 | stream); |
| 1793 | #endif |
| 1794 | |
| 1795 | /* allocate ITDs w/o locking anything */ |
| 1796 | status = itd_urb_transaction (stream, ehci, urb, mem_flags); |
| 1797 | if (unlikely (status < 0)) { |
| 1798 | ehci_dbg (ehci, "can't init itds\n"); |
| 1799 | goto done; |
| 1800 | } |
| 1801 | |
| 1802 | /* schedule ... need to lock */ |
| 1803 | spin_lock_irqsave (&ehci->lock, flags); |
Benjamin Herrenschmidt | 8de9840 | 2005-11-25 09:59:46 +1100 | [diff] [blame] | 1804 | if (unlikely(!test_bit(HCD_FLAG_HW_ACCESSIBLE, |
Alan Stern | e9df41c | 2007-08-08 11:48:02 -0400 | [diff] [blame] | 1805 | &ehci_to_hcd(ehci)->flags))) { |
Benjamin Herrenschmidt | 8de9840 | 2005-11-25 09:59:46 +1100 | [diff] [blame] | 1806 | status = -ESHUTDOWN; |
Alan Stern | e9df41c | 2007-08-08 11:48:02 -0400 | [diff] [blame] | 1807 | goto done_not_linked; |
| 1808 | } |
| 1809 | status = usb_hcd_link_urb_to_ep(ehci_to_hcd(ehci), urb); |
| 1810 | if (unlikely(status)) |
| 1811 | goto done_not_linked; |
| 1812 | status = iso_stream_schedule(ehci, urb, stream); |
David Brownell | 53bd6a6 | 2006-08-30 14:50:06 -0700 | [diff] [blame] | 1813 | if (likely (status == 0)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1814 | itd_link_urb (ehci, urb, ehci->periodic_size << 3, stream); |
Alan Stern | e9df41c | 2007-08-08 11:48:02 -0400 | [diff] [blame] | 1815 | else |
| 1816 | usb_hcd_unlink_urb_from_ep(ehci_to_hcd(ehci), urb); |
| 1817 | done_not_linked: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1818 | spin_unlock_irqrestore (&ehci->lock, flags); |
| 1819 | |
| 1820 | done: |
| 1821 | if (unlikely (status < 0)) |
| 1822 | iso_stream_put (ehci, stream); |
| 1823 | return status; |
| 1824 | } |
| 1825 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1826 | /*-------------------------------------------------------------------------*/ |
| 1827 | |
| 1828 | /* |
| 1829 | * "Split ISO TDs" ... used for USB 1.1 devices going through the |
| 1830 | * TTs in USB 2.0 hubs. These need microframe scheduling. |
| 1831 | */ |
| 1832 | |
| 1833 | static inline void |
Stefan Roese | 6dbd682 | 2007-05-01 09:29:37 -0700 | [diff] [blame] | 1834 | sitd_sched_init( |
| 1835 | struct ehci_hcd *ehci, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1836 | struct ehci_iso_sched *iso_sched, |
| 1837 | struct ehci_iso_stream *stream, |
| 1838 | struct urb *urb |
| 1839 | ) |
| 1840 | { |
| 1841 | unsigned i; |
| 1842 | dma_addr_t dma = urb->transfer_dma; |
| 1843 | |
| 1844 | /* how many frames are needed for these transfers */ |
| 1845 | iso_sched->span = urb->number_of_packets * stream->interval; |
| 1846 | |
| 1847 | /* figure out per-frame sitd fields that we'll need later |
| 1848 | * when we fit new sitds into the schedule. |
| 1849 | */ |
| 1850 | for (i = 0; i < urb->number_of_packets; i++) { |
| 1851 | struct ehci_iso_packet *packet = &iso_sched->packet [i]; |
| 1852 | unsigned length; |
| 1853 | dma_addr_t buf; |
| 1854 | u32 trans; |
| 1855 | |
| 1856 | length = urb->iso_frame_desc [i].length & 0x03ff; |
| 1857 | buf = dma + urb->iso_frame_desc [i].offset; |
| 1858 | |
| 1859 | trans = SITD_STS_ACTIVE; |
| 1860 | if (((i + 1) == urb->number_of_packets) |
| 1861 | && !(urb->transfer_flags & URB_NO_INTERRUPT)) |
| 1862 | trans |= SITD_IOC; |
| 1863 | trans |= length << 16; |
Stefan Roese | 6dbd682 | 2007-05-01 09:29:37 -0700 | [diff] [blame] | 1864 | packet->transaction = cpu_to_hc32(ehci, trans); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1865 | |
| 1866 | /* might need to cross a buffer page within a td */ |
| 1867 | packet->bufp = buf; |
| 1868 | packet->buf1 = (buf + length) & ~0x0fff; |
| 1869 | if (packet->buf1 != (buf & ~(u64)0x0fff)) |
| 1870 | packet->cross = 1; |
| 1871 | |
David Brownell | 53bd6a6 | 2006-08-30 14:50:06 -0700 | [diff] [blame] | 1872 | /* OUT uses multiple start-splits */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1873 | if (stream->bEndpointAddress & USB_DIR_IN) |
| 1874 | continue; |
| 1875 | length = (length + 187) / 188; |
| 1876 | if (length > 1) /* BEGIN vs ALL */ |
| 1877 | length |= 1 << 3; |
| 1878 | packet->buf1 |= length; |
| 1879 | } |
| 1880 | } |
| 1881 | |
| 1882 | static int |
| 1883 | sitd_urb_transaction ( |
| 1884 | struct ehci_iso_stream *stream, |
| 1885 | struct ehci_hcd *ehci, |
| 1886 | struct urb *urb, |
Al Viro | 55016f1 | 2005-10-21 03:21:58 -0400 | [diff] [blame] | 1887 | gfp_t mem_flags |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1888 | ) |
| 1889 | { |
| 1890 | struct ehci_sitd *sitd; |
| 1891 | dma_addr_t sitd_dma; |
| 1892 | int i; |
| 1893 | struct ehci_iso_sched *iso_sched; |
| 1894 | unsigned long flags; |
| 1895 | |
| 1896 | iso_sched = iso_sched_alloc (urb->number_of_packets, mem_flags); |
| 1897 | if (iso_sched == NULL) |
| 1898 | return -ENOMEM; |
| 1899 | |
Stefan Roese | 6dbd682 | 2007-05-01 09:29:37 -0700 | [diff] [blame] | 1900 | sitd_sched_init(ehci, iso_sched, stream, urb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1901 | |
| 1902 | /* allocate/init sITDs */ |
| 1903 | spin_lock_irqsave (&ehci->lock, flags); |
| 1904 | for (i = 0; i < urb->number_of_packets; i++) { |
| 1905 | |
| 1906 | /* NOTE: for now, we don't try to handle wraparound cases |
| 1907 | * for IN (using sitd->hw_backpointer, like a FSTN), which |
| 1908 | * means we never need two sitds for full speed packets. |
| 1909 | */ |
| 1910 | |
| 1911 | /* free_list.next might be cache-hot ... but maybe |
| 1912 | * the HC caches it too. avoid that issue for now. |
| 1913 | */ |
| 1914 | |
| 1915 | /* prefer previously-allocated sitds */ |
| 1916 | if (!list_empty(&stream->free_list)) { |
| 1917 | sitd = list_entry (stream->free_list.prev, |
| 1918 | struct ehci_sitd, sitd_list); |
| 1919 | list_del (&sitd->sitd_list); |
| 1920 | sitd_dma = sitd->sitd_dma; |
Karsten Wiese | 3d01f0f | 2008-02-19 12:31:49 -0800 | [diff] [blame] | 1921 | } else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1922 | spin_unlock_irqrestore (&ehci->lock, flags); |
| 1923 | sitd = dma_pool_alloc (ehci->sitd_pool, mem_flags, |
| 1924 | &sitd_dma); |
| 1925 | spin_lock_irqsave (&ehci->lock, flags); |
Karsten Wiese | 3d01f0f | 2008-02-19 12:31:49 -0800 | [diff] [blame] | 1926 | if (!sitd) { |
| 1927 | iso_sched_free(stream, iso_sched); |
| 1928 | spin_unlock_irqrestore(&ehci->lock, flags); |
| 1929 | return -ENOMEM; |
| 1930 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1931 | } |
| 1932 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1933 | memset (sitd, 0, sizeof *sitd); |
| 1934 | sitd->sitd_dma = sitd_dma; |
| 1935 | list_add (&sitd->sitd_list, &iso_sched->td_list); |
| 1936 | } |
| 1937 | |
| 1938 | /* temporarily store schedule info in hcpriv */ |
| 1939 | urb->hcpriv = iso_sched; |
| 1940 | urb->error_count = 0; |
| 1941 | |
| 1942 | spin_unlock_irqrestore (&ehci->lock, flags); |
| 1943 | return 0; |
| 1944 | } |
| 1945 | |
| 1946 | /*-------------------------------------------------------------------------*/ |
| 1947 | |
| 1948 | static inline void |
Stefan Roese | 6dbd682 | 2007-05-01 09:29:37 -0700 | [diff] [blame] | 1949 | sitd_patch( |
| 1950 | struct ehci_hcd *ehci, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1951 | struct ehci_iso_stream *stream, |
| 1952 | struct ehci_sitd *sitd, |
| 1953 | struct ehci_iso_sched *iso_sched, |
| 1954 | unsigned index |
| 1955 | ) |
| 1956 | { |
| 1957 | struct ehci_iso_packet *uf = &iso_sched->packet [index]; |
| 1958 | u64 bufp = uf->bufp; |
| 1959 | |
Stefan Roese | 6dbd682 | 2007-05-01 09:29:37 -0700 | [diff] [blame] | 1960 | sitd->hw_next = EHCI_LIST_END(ehci); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1961 | sitd->hw_fullspeed_ep = stream->address; |
| 1962 | sitd->hw_uframe = stream->splits; |
| 1963 | sitd->hw_results = uf->transaction; |
Stefan Roese | 6dbd682 | 2007-05-01 09:29:37 -0700 | [diff] [blame] | 1964 | sitd->hw_backpointer = EHCI_LIST_END(ehci); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1965 | |
| 1966 | bufp = uf->bufp; |
Stefan Roese | 6dbd682 | 2007-05-01 09:29:37 -0700 | [diff] [blame] | 1967 | sitd->hw_buf[0] = cpu_to_hc32(ehci, bufp); |
| 1968 | sitd->hw_buf_hi[0] = cpu_to_hc32(ehci, bufp >> 32); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1969 | |
Stefan Roese | 6dbd682 | 2007-05-01 09:29:37 -0700 | [diff] [blame] | 1970 | sitd->hw_buf[1] = cpu_to_hc32(ehci, uf->buf1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1971 | if (uf->cross) |
| 1972 | bufp += 4096; |
Stefan Roese | 6dbd682 | 2007-05-01 09:29:37 -0700 | [diff] [blame] | 1973 | sitd->hw_buf_hi[1] = cpu_to_hc32(ehci, bufp >> 32); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1974 | sitd->index = index; |
| 1975 | } |
| 1976 | |
| 1977 | static inline void |
| 1978 | sitd_link (struct ehci_hcd *ehci, unsigned frame, struct ehci_sitd *sitd) |
| 1979 | { |
| 1980 | /* note: sitd ordering could matter (CSPLIT then SSPLIT) */ |
| 1981 | sitd->sitd_next = ehci->pshadow [frame]; |
| 1982 | sitd->hw_next = ehci->periodic [frame]; |
| 1983 | ehci->pshadow [frame].sitd = sitd; |
| 1984 | sitd->frame = frame; |
| 1985 | wmb (); |
Stefan Roese | 6dbd682 | 2007-05-01 09:29:37 -0700 | [diff] [blame] | 1986 | ehci->periodic[frame] = cpu_to_hc32(ehci, sitd->sitd_dma | Q_TYPE_SITD); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1987 | } |
| 1988 | |
| 1989 | /* fit urb's sitds into the selected schedule slot; activate as needed */ |
| 1990 | static int |
| 1991 | sitd_link_urb ( |
| 1992 | struct ehci_hcd *ehci, |
| 1993 | struct urb *urb, |
| 1994 | unsigned mod, |
| 1995 | struct ehci_iso_stream *stream |
| 1996 | ) |
| 1997 | { |
| 1998 | int packet; |
| 1999 | unsigned next_uframe; |
| 2000 | struct ehci_iso_sched *sched = urb->hcpriv; |
| 2001 | struct ehci_sitd *sitd; |
| 2002 | |
| 2003 | next_uframe = stream->next_uframe; |
| 2004 | |
| 2005 | if (list_empty(&stream->td_list)) { |
| 2006 | /* usbfs ignores TT bandwidth */ |
| 2007 | ehci_to_hcd(ehci)->self.bandwidth_allocated |
| 2008 | += stream->bandwidth; |
| 2009 | ehci_vdbg (ehci, |
| 2010 | "sched devp %s ep%d%s-iso [%d] %dms/%04x\n", |
| 2011 | urb->dev->devpath, stream->bEndpointAddress & 0x0f, |
| 2012 | (stream->bEndpointAddress & USB_DIR_IN) ? "in" : "out", |
| 2013 | (next_uframe >> 3) % ehci->periodic_size, |
Stefan Roese | 6dbd682 | 2007-05-01 09:29:37 -0700 | [diff] [blame] | 2014 | stream->interval, hc32_to_cpu(ehci, stream->splits)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2015 | stream->start = jiffies; |
| 2016 | } |
| 2017 | ehci_to_hcd(ehci)->self.bandwidth_isoc_reqs++; |
| 2018 | |
| 2019 | /* fill sITDs frame by frame */ |
| 2020 | for (packet = 0, sitd = NULL; |
| 2021 | packet < urb->number_of_packets; |
| 2022 | packet++) { |
| 2023 | |
| 2024 | /* ASSERT: we have all necessary sitds */ |
| 2025 | BUG_ON (list_empty (&sched->td_list)); |
| 2026 | |
| 2027 | /* ASSERT: no itds for this endpoint in this frame */ |
| 2028 | |
| 2029 | sitd = list_entry (sched->td_list.next, |
| 2030 | struct ehci_sitd, sitd_list); |
| 2031 | list_move_tail (&sitd->sitd_list, &stream->td_list); |
| 2032 | sitd->stream = iso_stream_get (stream); |
Karsten Wiese | 508db8c | 2009-02-26 01:47:48 +0100 | [diff] [blame] | 2033 | sitd->urb = urb; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2034 | |
Stefan Roese | 6dbd682 | 2007-05-01 09:29:37 -0700 | [diff] [blame] | 2035 | sitd_patch(ehci, stream, sitd, sched, packet); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2036 | sitd_link (ehci, (next_uframe >> 3) % ehci->periodic_size, |
| 2037 | sitd); |
| 2038 | |
| 2039 | next_uframe += stream->interval << 3; |
| 2040 | stream->depth += stream->interval << 3; |
| 2041 | } |
| 2042 | stream->next_uframe = next_uframe % mod; |
| 2043 | |
| 2044 | /* don't need that schedule data any more */ |
| 2045 | iso_sched_free (stream, sched); |
| 2046 | urb->hcpriv = NULL; |
| 2047 | |
| 2048 | timer_action (ehci, TIMER_IO_WATCHDOG); |
David Brownell | 01c1714 | 2008-08-26 23:35:04 -0700 | [diff] [blame] | 2049 | return enable_periodic(ehci); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2050 | } |
| 2051 | |
| 2052 | /*-------------------------------------------------------------------------*/ |
| 2053 | |
| 2054 | #define SITD_ERRS (SITD_STS_ERR | SITD_STS_DBE | SITD_STS_BABBLE \ |
David Brownell | 53bd6a6 | 2006-08-30 14:50:06 -0700 | [diff] [blame] | 2055 | | SITD_STS_XACT | SITD_STS_MMF) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2056 | |
David Brownell | 30bf54e | 2007-12-16 22:37:40 -0800 | [diff] [blame] | 2057 | /* Process and recycle a completed SITD. Return true iff its urb completed, |
| 2058 | * and hence its completion callback probably added things to the hardware |
| 2059 | * schedule. |
| 2060 | * |
| 2061 | * Note that we carefully avoid recycling this descriptor until after any |
| 2062 | * completion callback runs, so that it won't be reused quickly. That is, |
| 2063 | * assuming (a) no more than two urbs per frame on this endpoint, and also |
| 2064 | * (b) only this endpoint's completions submit URBs. It seems some silicon |
| 2065 | * corrupts things if you reuse completed descriptors very quickly... |
| 2066 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2067 | static unsigned |
| 2068 | sitd_complete ( |
| 2069 | struct ehci_hcd *ehci, |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 2070 | struct ehci_sitd *sitd |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2071 | ) { |
| 2072 | struct urb *urb = sitd->urb; |
| 2073 | struct usb_iso_packet_descriptor *desc; |
| 2074 | u32 t; |
| 2075 | int urb_index = -1; |
| 2076 | struct ehci_iso_stream *stream = sitd->stream; |
| 2077 | struct usb_device *dev; |
David Brownell | 30bf54e | 2007-12-16 22:37:40 -0800 | [diff] [blame] | 2078 | unsigned retval = false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2079 | |
| 2080 | urb_index = sitd->index; |
| 2081 | desc = &urb->iso_frame_desc [urb_index]; |
Stefan Roese | 6dbd682 | 2007-05-01 09:29:37 -0700 | [diff] [blame] | 2082 | t = hc32_to_cpup(ehci, &sitd->hw_results); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2083 | |
| 2084 | /* report transfer status */ |
| 2085 | if (t & SITD_ERRS) { |
| 2086 | urb->error_count++; |
| 2087 | if (t & SITD_STS_DBE) |
| 2088 | desc->status = usb_pipein (urb->pipe) |
| 2089 | ? -ENOSR /* hc couldn't read */ |
| 2090 | : -ECOMM; /* hc couldn't write */ |
| 2091 | else if (t & SITD_STS_BABBLE) |
| 2092 | desc->status = -EOVERFLOW; |
| 2093 | else /* XACT, MMF, etc */ |
| 2094 | desc->status = -EPROTO; |
| 2095 | } else { |
| 2096 | desc->status = 0; |
Alan Stern | ec6d67e | 2009-06-29 14:34:59 -0400 | [diff] [blame] | 2097 | desc->actual_length = desc->length - SITD_LENGTH(t); |
| 2098 | urb->actual_length += desc->actual_length; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2099 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2100 | stream->depth -= stream->interval << 3; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2101 | |
| 2102 | /* handle completion now? */ |
| 2103 | if ((urb_index + 1) != urb->number_of_packets) |
David Brownell | 30bf54e | 2007-12-16 22:37:40 -0800 | [diff] [blame] | 2104 | goto done; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2105 | |
| 2106 | /* ASSERT: it's really the last sitd for this urb |
| 2107 | list_for_each_entry (sitd, &stream->td_list, sitd_list) |
| 2108 | BUG_ON (sitd->urb == urb); |
| 2109 | */ |
| 2110 | |
David Brownell | aa16ca3 | 2007-12-30 23:45:19 -0800 | [diff] [blame] | 2111 | /* give urb back to the driver; completion often (re)submits */ |
Alan Stern | 6a8e87b | 2006-01-19 10:46:27 -0500 | [diff] [blame] | 2112 | dev = urb->dev; |
Alan Stern | 14c04c0 | 2007-08-24 15:40:19 -0400 | [diff] [blame] | 2113 | ehci_urb_done(ehci, urb, 0); |
David Brownell | 30bf54e | 2007-12-16 22:37:40 -0800 | [diff] [blame] | 2114 | retval = true; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2115 | urb = NULL; |
David Brownell | 01c1714 | 2008-08-26 23:35:04 -0700 | [diff] [blame] | 2116 | (void) disable_periodic(ehci); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2117 | ehci_to_hcd(ehci)->self.bandwidth_isoc_reqs--; |
| 2118 | |
Karsten Wiese | 508db8c | 2009-02-26 01:47:48 +0100 | [diff] [blame] | 2119 | if (list_is_singular(&stream->td_list)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2120 | ehci_to_hcd(ehci)->self.bandwidth_allocated |
| 2121 | -= stream->bandwidth; |
| 2122 | ehci_vdbg (ehci, |
| 2123 | "deschedule devp %s ep%d%s-iso\n", |
| 2124 | dev->devpath, stream->bEndpointAddress & 0x0f, |
| 2125 | (stream->bEndpointAddress & USB_DIR_IN) ? "in" : "out"); |
| 2126 | } |
| 2127 | iso_stream_put (ehci, stream); |
David Brownell | 30bf54e | 2007-12-16 22:37:40 -0800 | [diff] [blame] | 2128 | /* OK to recycle this SITD now that its completion callback ran. */ |
| 2129 | done: |
David Brownell | 30bf54e | 2007-12-16 22:37:40 -0800 | [diff] [blame] | 2130 | sitd->urb = NULL; |
| 2131 | sitd->stream = NULL; |
| 2132 | list_move(&sitd->sitd_list, &stream->free_list); |
| 2133 | iso_stream_put(ehci, stream); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2134 | |
David Brownell | 30bf54e | 2007-12-16 22:37:40 -0800 | [diff] [blame] | 2135 | return retval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2136 | } |
| 2137 | |
| 2138 | |
Olav Kongas | 5db539e | 2005-06-23 20:25:36 +0300 | [diff] [blame] | 2139 | static int sitd_submit (struct ehci_hcd *ehci, struct urb *urb, |
Al Viro | 55016f1 | 2005-10-21 03:21:58 -0400 | [diff] [blame] | 2140 | gfp_t mem_flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2141 | { |
| 2142 | int status = -EINVAL; |
| 2143 | unsigned long flags; |
| 2144 | struct ehci_iso_stream *stream; |
| 2145 | |
| 2146 | /* Get iso_stream head */ |
| 2147 | stream = iso_stream_find (ehci, urb); |
| 2148 | if (stream == NULL) { |
| 2149 | ehci_dbg (ehci, "can't get iso stream\n"); |
| 2150 | return -ENOMEM; |
| 2151 | } |
| 2152 | if (urb->interval != stream->interval) { |
| 2153 | ehci_dbg (ehci, "can't change iso interval %d --> %d\n", |
| 2154 | stream->interval, urb->interval); |
| 2155 | goto done; |
| 2156 | } |
| 2157 | |
| 2158 | #ifdef EHCI_URB_TRACE |
| 2159 | ehci_dbg (ehci, |
| 2160 | "submit %p dev%s ep%d%s-iso len %d\n", |
| 2161 | urb, urb->dev->devpath, |
| 2162 | usb_pipeendpoint (urb->pipe), |
| 2163 | usb_pipein (urb->pipe) ? "in" : "out", |
| 2164 | urb->transfer_buffer_length); |
| 2165 | #endif |
| 2166 | |
| 2167 | /* allocate SITDs */ |
| 2168 | status = sitd_urb_transaction (stream, ehci, urb, mem_flags); |
| 2169 | if (status < 0) { |
| 2170 | ehci_dbg (ehci, "can't init sitds\n"); |
| 2171 | goto done; |
| 2172 | } |
| 2173 | |
| 2174 | /* schedule ... need to lock */ |
| 2175 | spin_lock_irqsave (&ehci->lock, flags); |
Benjamin Herrenschmidt | 8de9840 | 2005-11-25 09:59:46 +1100 | [diff] [blame] | 2176 | if (unlikely(!test_bit(HCD_FLAG_HW_ACCESSIBLE, |
Alan Stern | e9df41c | 2007-08-08 11:48:02 -0400 | [diff] [blame] | 2177 | &ehci_to_hcd(ehci)->flags))) { |
Benjamin Herrenschmidt | 8de9840 | 2005-11-25 09:59:46 +1100 | [diff] [blame] | 2178 | status = -ESHUTDOWN; |
Alan Stern | e9df41c | 2007-08-08 11:48:02 -0400 | [diff] [blame] | 2179 | goto done_not_linked; |
| 2180 | } |
| 2181 | status = usb_hcd_link_urb_to_ep(ehci_to_hcd(ehci), urb); |
| 2182 | if (unlikely(status)) |
| 2183 | goto done_not_linked; |
| 2184 | status = iso_stream_schedule(ehci, urb, stream); |
David Brownell | 53bd6a6 | 2006-08-30 14:50:06 -0700 | [diff] [blame] | 2185 | if (status == 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2186 | sitd_link_urb (ehci, urb, ehci->periodic_size << 3, stream); |
Alan Stern | e9df41c | 2007-08-08 11:48:02 -0400 | [diff] [blame] | 2187 | else |
| 2188 | usb_hcd_unlink_urb_from_ep(ehci_to_hcd(ehci), urb); |
| 2189 | done_not_linked: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2190 | spin_unlock_irqrestore (&ehci->lock, flags); |
| 2191 | |
| 2192 | done: |
| 2193 | if (status < 0) |
| 2194 | iso_stream_put (ehci, stream); |
| 2195 | return status; |
| 2196 | } |
| 2197 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2198 | /*-------------------------------------------------------------------------*/ |
| 2199 | |
Karsten Wiese | 9aa09d2 | 2009-02-08 16:07:58 -0800 | [diff] [blame] | 2200 | static void free_cached_itd_list(struct ehci_hcd *ehci) |
| 2201 | { |
| 2202 | struct ehci_itd *itd, *n; |
| 2203 | |
| 2204 | list_for_each_entry_safe(itd, n, &ehci->cached_itd_list, itd_list) { |
| 2205 | struct ehci_iso_stream *stream = itd->stream; |
| 2206 | itd->stream = NULL; |
| 2207 | list_move(&itd->itd_list, &stream->free_list); |
| 2208 | iso_stream_put(ehci, stream); |
| 2209 | } |
| 2210 | } |
| 2211 | |
| 2212 | /*-------------------------------------------------------------------------*/ |
| 2213 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2214 | static void |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 2215 | scan_periodic (struct ehci_hcd *ehci) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2216 | { |
Alan Stern | b40e43f | 2008-05-20 16:59:10 -0400 | [diff] [blame] | 2217 | unsigned now_uframe, frame, clock, clock_frame, mod; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2218 | unsigned modified; |
| 2219 | |
| 2220 | mod = ehci->periodic_size << 3; |
| 2221 | |
| 2222 | /* |
| 2223 | * When running, scan from last scan point up to "now" |
| 2224 | * else clean up by scanning everything that's left. |
| 2225 | * Touches as few pages as possible: cache-friendly. |
| 2226 | */ |
| 2227 | now_uframe = ehci->next_uframe; |
Karsten Wiese | 9aa09d2 | 2009-02-08 16:07:58 -0800 | [diff] [blame] | 2228 | if (HC_IS_RUNNING(ehci_to_hcd(ehci)->state)) { |
Benjamin Herrenschmidt | 083522d | 2006-12-15 06:54:08 +1100 | [diff] [blame] | 2229 | clock = ehci_readl(ehci, &ehci->regs->frame_index); |
Karsten Wiese | 9aa09d2 | 2009-02-08 16:07:58 -0800 | [diff] [blame] | 2230 | clock_frame = (clock >> 3) % ehci->periodic_size; |
| 2231 | } else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2232 | clock = now_uframe + mod - 1; |
Karsten Wiese | 9aa09d2 | 2009-02-08 16:07:58 -0800 | [diff] [blame] | 2233 | clock_frame = -1; |
| 2234 | } |
| 2235 | if (ehci->clock_frame != clock_frame) { |
| 2236 | free_cached_itd_list(ehci); |
| 2237 | ehci->clock_frame = clock_frame; |
| 2238 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2239 | clock %= mod; |
Alan Stern | b40e43f | 2008-05-20 16:59:10 -0400 | [diff] [blame] | 2240 | clock_frame = clock >> 3; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2241 | |
| 2242 | for (;;) { |
| 2243 | union ehci_shadow q, *q_p; |
Stefan Roese | 6dbd682 | 2007-05-01 09:29:37 -0700 | [diff] [blame] | 2244 | __hc32 type, *hw_p; |
David Brownell | 79592b72 | 2008-01-07 00:47:42 -0800 | [diff] [blame] | 2245 | unsigned incomplete = false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2246 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2247 | frame = now_uframe >> 3; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2248 | |
| 2249 | restart: |
| 2250 | /* scan each element in frame's queue for completions */ |
| 2251 | q_p = &ehci->pshadow [frame]; |
| 2252 | hw_p = &ehci->periodic [frame]; |
| 2253 | q.ptr = q_p->ptr; |
Stefan Roese | 6dbd682 | 2007-05-01 09:29:37 -0700 | [diff] [blame] | 2254 | type = Q_NEXT_TYPE(ehci, *hw_p); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2255 | modified = 0; |
| 2256 | |
| 2257 | while (q.ptr != NULL) { |
| 2258 | unsigned uf; |
| 2259 | union ehci_shadow temp; |
| 2260 | int live; |
| 2261 | |
| 2262 | live = HC_IS_RUNNING (ehci_to_hcd(ehci)->state); |
Stefan Roese | 6dbd682 | 2007-05-01 09:29:37 -0700 | [diff] [blame] | 2263 | switch (hc32_to_cpu(ehci, type)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2264 | case Q_TYPE_QH: |
| 2265 | /* handle any completions */ |
| 2266 | temp.qh = qh_get (q.qh); |
Alek Du | 3807e26 | 2009-07-14 07:23:29 +0800 | [diff] [blame] | 2267 | type = Q_NEXT_TYPE(ehci, q.qh->hw->hw_next); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2268 | q = q.qh->qh_next; |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 2269 | modified = qh_completions (ehci, temp.qh); |
Alan Stern | a448c9d | 2009-08-19 12:22:44 -0400 | [diff] [blame] | 2270 | if (unlikely(list_empty(&temp.qh->qtd_list) || |
| 2271 | temp.qh->needs_rescan)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2272 | intr_deschedule (ehci, temp.qh); |
| 2273 | qh_put (temp.qh); |
| 2274 | break; |
| 2275 | case Q_TYPE_FSTN: |
| 2276 | /* for "save place" FSTNs, look at QH entries |
| 2277 | * in the previous frame for completions. |
| 2278 | */ |
Stefan Roese | 6dbd682 | 2007-05-01 09:29:37 -0700 | [diff] [blame] | 2279 | if (q.fstn->hw_prev != EHCI_LIST_END(ehci)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2280 | dbg ("ignoring completions from FSTNs"); |
| 2281 | } |
Stefan Roese | 6dbd682 | 2007-05-01 09:29:37 -0700 | [diff] [blame] | 2282 | type = Q_NEXT_TYPE(ehci, q.fstn->hw_next); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2283 | q = q.fstn->fstn_next; |
| 2284 | break; |
| 2285 | case Q_TYPE_ITD: |
David Brownell | 79592b72 | 2008-01-07 00:47:42 -0800 | [diff] [blame] | 2286 | /* If this ITD is still active, leave it for |
| 2287 | * later processing ... check the next entry. |
Alan Stern | b40e43f | 2008-05-20 16:59:10 -0400 | [diff] [blame] | 2288 | * No need to check for activity unless the |
| 2289 | * frame is current. |
David Brownell | 79592b72 | 2008-01-07 00:47:42 -0800 | [diff] [blame] | 2290 | */ |
Alan Stern | b40e43f | 2008-05-20 16:59:10 -0400 | [diff] [blame] | 2291 | if (frame == clock_frame && live) { |
| 2292 | rmb(); |
| 2293 | for (uf = 0; uf < 8; uf++) { |
| 2294 | if (q.itd->hw_transaction[uf] & |
| 2295 | ITD_ACTIVE(ehci)) |
| 2296 | break; |
| 2297 | } |
| 2298 | if (uf < 8) { |
| 2299 | incomplete = true; |
| 2300 | q_p = &q.itd->itd_next; |
| 2301 | hw_p = &q.itd->hw_next; |
| 2302 | type = Q_NEXT_TYPE(ehci, |
Stefan Roese | 6dbd682 | 2007-05-01 09:29:37 -0700 | [diff] [blame] | 2303 | q.itd->hw_next); |
Alan Stern | b40e43f | 2008-05-20 16:59:10 -0400 | [diff] [blame] | 2304 | q = *q_p; |
| 2305 | break; |
| 2306 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2307 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2308 | |
David Brownell | 79592b72 | 2008-01-07 00:47:42 -0800 | [diff] [blame] | 2309 | /* Take finished ITDs out of the schedule |
| 2310 | * and process them: recycle, maybe report |
| 2311 | * URB completion. HC won't cache the |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2312 | * pointer for much longer, if at all. |
| 2313 | */ |
| 2314 | *q_p = q.itd->itd_next; |
| 2315 | *hw_p = q.itd->hw_next; |
Stefan Roese | 6dbd682 | 2007-05-01 09:29:37 -0700 | [diff] [blame] | 2316 | type = Q_NEXT_TYPE(ehci, q.itd->hw_next); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2317 | wmb(); |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 2318 | modified = itd_complete (ehci, q.itd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2319 | q = *q_p; |
| 2320 | break; |
| 2321 | case Q_TYPE_SITD: |
David Brownell | 79592b72 | 2008-01-07 00:47:42 -0800 | [diff] [blame] | 2322 | /* If this SITD is still active, leave it for |
| 2323 | * later processing ... check the next entry. |
Alan Stern | b40e43f | 2008-05-20 16:59:10 -0400 | [diff] [blame] | 2324 | * No need to check for activity unless the |
| 2325 | * frame is current. |
David Brownell | 79592b72 | 2008-01-07 00:47:42 -0800 | [diff] [blame] | 2326 | */ |
Alan Stern | b40e43f | 2008-05-20 16:59:10 -0400 | [diff] [blame] | 2327 | if (frame == clock_frame && live && |
| 2328 | (q.sitd->hw_results & |
| 2329 | SITD_ACTIVE(ehci))) { |
David Brownell | 79592b72 | 2008-01-07 00:47:42 -0800 | [diff] [blame] | 2330 | incomplete = true; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2331 | q_p = &q.sitd->sitd_next; |
| 2332 | hw_p = &q.sitd->hw_next; |
Stefan Roese | 6dbd682 | 2007-05-01 09:29:37 -0700 | [diff] [blame] | 2333 | type = Q_NEXT_TYPE(ehci, |
| 2334 | q.sitd->hw_next); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2335 | q = *q_p; |
| 2336 | break; |
| 2337 | } |
David Brownell | 79592b72 | 2008-01-07 00:47:42 -0800 | [diff] [blame] | 2338 | |
| 2339 | /* Take finished SITDs out of the schedule |
| 2340 | * and process them: recycle, maybe report |
| 2341 | * URB completion. |
| 2342 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2343 | *q_p = q.sitd->sitd_next; |
| 2344 | *hw_p = q.sitd->hw_next; |
Stefan Roese | 6dbd682 | 2007-05-01 09:29:37 -0700 | [diff] [blame] | 2345 | type = Q_NEXT_TYPE(ehci, q.sitd->hw_next); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2346 | wmb(); |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 2347 | modified = sitd_complete (ehci, q.sitd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2348 | q = *q_p; |
| 2349 | break; |
| 2350 | default: |
| 2351 | dbg ("corrupt type %d frame %d shadow %p", |
| 2352 | type, frame, q.ptr); |
| 2353 | // BUG (); |
| 2354 | q.ptr = NULL; |
| 2355 | } |
| 2356 | |
| 2357 | /* assume completion callbacks modify the queue */ |
David Brownell | aa16ca3 | 2007-12-30 23:45:19 -0800 | [diff] [blame] | 2358 | if (unlikely (modified)) { |
| 2359 | if (likely(ehci->periodic_sched > 0)) |
| 2360 | goto restart; |
David Brownell | 01c1714 | 2008-08-26 23:35:04 -0700 | [diff] [blame] | 2361 | /* short-circuit this scan */ |
David Brownell | aa16ca3 | 2007-12-30 23:45:19 -0800 | [diff] [blame] | 2362 | now_uframe = clock; |
| 2363 | break; |
| 2364 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2365 | } |
| 2366 | |
David Brownell | 79592b72 | 2008-01-07 00:47:42 -0800 | [diff] [blame] | 2367 | /* If we can tell we caught up to the hardware, stop now. |
| 2368 | * We can't advance our scan without collecting the ISO |
| 2369 | * transfers that are still pending in this frame. |
| 2370 | */ |
| 2371 | if (incomplete && HC_IS_RUNNING(ehci_to_hcd(ehci)->state)) { |
| 2372 | ehci->next_uframe = now_uframe; |
| 2373 | break; |
| 2374 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2375 | |
| 2376 | // FIXME: this assumes we won't get lapped when |
| 2377 | // latencies climb; that should be rare, but... |
| 2378 | // detect it, and just go all the way around. |
| 2379 | // FLR might help detect this case, so long as latencies |
| 2380 | // don't exceed periodic_size msec (default 1.024 sec). |
| 2381 | |
| 2382 | // FIXME: likewise assumes HC doesn't halt mid-scan |
| 2383 | |
| 2384 | if (now_uframe == clock) { |
| 2385 | unsigned now; |
| 2386 | |
David Brownell | aa16ca3 | 2007-12-30 23:45:19 -0800 | [diff] [blame] | 2387 | if (!HC_IS_RUNNING (ehci_to_hcd(ehci)->state) |
| 2388 | || ehci->periodic_sched == 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2389 | break; |
| 2390 | ehci->next_uframe = now_uframe; |
Benjamin Herrenschmidt | 083522d | 2006-12-15 06:54:08 +1100 | [diff] [blame] | 2391 | now = ehci_readl(ehci, &ehci->regs->frame_index) % mod; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2392 | if (now_uframe == now) |
| 2393 | break; |
| 2394 | |
| 2395 | /* rescan the rest of this frame, then ... */ |
| 2396 | clock = now; |
Alan Stern | b40e43f | 2008-05-20 16:59:10 -0400 | [diff] [blame] | 2397 | clock_frame = clock >> 3; |
Karsten Wiese | 9aa09d2 | 2009-02-08 16:07:58 -0800 | [diff] [blame] | 2398 | if (ehci->clock_frame != clock_frame) { |
| 2399 | free_cached_itd_list(ehci); |
| 2400 | ehci->clock_frame = clock_frame; |
| 2401 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2402 | } else { |
| 2403 | now_uframe++; |
| 2404 | now_uframe %= mod; |
| 2405 | } |
David Brownell | 53bd6a6 | 2006-08-30 14:50:06 -0700 | [diff] [blame] | 2406 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2407 | } |