ehci-sched.c 64 KB

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