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