ehci-sched.c 64 KB

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