ehci-sched.c 65 KB

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