ehci-sched.c 76 KB

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