gadget.c 82 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348
  1. /**
  2. * gadget.c - DesignWare USB3 DRD Controller Gadget Framework Link
  3. *
  4. * Copyright (C) 2010-2011 Texas Instruments Incorporated - http://www.ti.com
  5. *
  6. * Authors: Felipe Balbi <balbi@ti.com>,
  7. * Sebastian Andrzej Siewior <bigeasy@linutronix.de>
  8. *
  9. * This program is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License version 2 of
  11. * the License as published by the Free Software Foundation.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. */
  18. #include <linux/kernel.h>
  19. #include <linux/delay.h>
  20. #include <linux/slab.h>
  21. #include <linux/spinlock.h>
  22. #include <linux/platform_device.h>
  23. #include <linux/pm_runtime.h>
  24. #include <linux/interrupt.h>
  25. #include <linux/io.h>
  26. #include <linux/list.h>
  27. #include <linux/dma-mapping.h>
  28. #include <linux/usb/ch9.h>
  29. #include <linux/usb/gadget.h>
  30. #include "debug.h"
  31. #include "core.h"
  32. #include "gadget.h"
  33. #include "io.h"
  34. /**
  35. * dwc3_gadget_set_test_mode - Enables USB2 Test Modes
  36. * @dwc: pointer to our context structure
  37. * @mode: the mode to set (J, K SE0 NAK, Force Enable)
  38. *
  39. * Caller should take care of locking. This function will
  40. * return 0 on success or -EINVAL if wrong Test Selector
  41. * is passed
  42. */
  43. int dwc3_gadget_set_test_mode(struct dwc3 *dwc, int mode)
  44. {
  45. u32 reg;
  46. reg = dwc3_readl(dwc->regs, DWC3_DCTL);
  47. reg &= ~DWC3_DCTL_TSTCTRL_MASK;
  48. switch (mode) {
  49. case TEST_J:
  50. case TEST_K:
  51. case TEST_SE0_NAK:
  52. case TEST_PACKET:
  53. case TEST_FORCE_EN:
  54. reg |= mode << 1;
  55. break;
  56. default:
  57. return -EINVAL;
  58. }
  59. dwc3_writel(dwc->regs, DWC3_DCTL, reg);
  60. return 0;
  61. }
  62. /**
  63. * dwc3_gadget_get_link_state - Gets current state of USB Link
  64. * @dwc: pointer to our context structure
  65. *
  66. * Caller should take care of locking. This function will
  67. * return the link state on success (>= 0) or -ETIMEDOUT.
  68. */
  69. int dwc3_gadget_get_link_state(struct dwc3 *dwc)
  70. {
  71. u32 reg;
  72. reg = dwc3_readl(dwc->regs, DWC3_DSTS);
  73. return DWC3_DSTS_USBLNKST(reg);
  74. }
  75. /**
  76. * dwc3_gadget_set_link_state - Sets USB Link to a particular State
  77. * @dwc: pointer to our context structure
  78. * @state: the state to put link into
  79. *
  80. * Caller should take care of locking. This function will
  81. * return 0 on success or -ETIMEDOUT.
  82. */
  83. int dwc3_gadget_set_link_state(struct dwc3 *dwc, enum dwc3_link_state state)
  84. {
  85. int retries = 10000;
  86. u32 reg;
  87. /*
  88. * Wait until device controller is ready. Only applies to 1.94a and
  89. * later RTL.
  90. */
  91. if (dwc->revision >= DWC3_REVISION_194A) {
  92. while (--retries) {
  93. reg = dwc3_readl(dwc->regs, DWC3_DSTS);
  94. if (reg & DWC3_DSTS_DCNRD)
  95. udelay(5);
  96. else
  97. break;
  98. }
  99. if (retries <= 0)
  100. return -ETIMEDOUT;
  101. }
  102. reg = dwc3_readl(dwc->regs, DWC3_DCTL);
  103. reg &= ~DWC3_DCTL_ULSTCHNGREQ_MASK;
  104. /* set requested state */
  105. reg |= DWC3_DCTL_ULSTCHNGREQ(state);
  106. dwc3_writel(dwc->regs, DWC3_DCTL, reg);
  107. /*
  108. * The following code is racy when called from dwc3_gadget_wakeup,
  109. * and is not needed, at least on newer versions
  110. */
  111. if (dwc->revision >= DWC3_REVISION_194A)
  112. return 0;
  113. /* wait for a change in DSTS */
  114. retries = 10000;
  115. while (--retries) {
  116. reg = dwc3_readl(dwc->regs, DWC3_DSTS);
  117. if (DWC3_DSTS_USBLNKST(reg) == state)
  118. return 0;
  119. udelay(5);
  120. }
  121. return -ETIMEDOUT;
  122. }
  123. /**
  124. * dwc3_ep_inc_trb() - Increment a TRB index.
  125. * @index - Pointer to the TRB index to increment.
  126. *
  127. * The index should never point to the link TRB. After incrementing,
  128. * if it is point to the link TRB, wrap around to the beginning. The
  129. * link TRB is always at the last TRB entry.
  130. */
  131. static void dwc3_ep_inc_trb(u8 *index)
  132. {
  133. (*index)++;
  134. if (*index == (DWC3_TRB_NUM - 1))
  135. *index = 0;
  136. }
  137. static void dwc3_ep_inc_enq(struct dwc3_ep *dep)
  138. {
  139. dwc3_ep_inc_trb(&dep->trb_enqueue);
  140. }
  141. static void dwc3_ep_inc_deq(struct dwc3_ep *dep)
  142. {
  143. dwc3_ep_inc_trb(&dep->trb_dequeue);
  144. }
  145. void dwc3_gadget_giveback(struct dwc3_ep *dep, struct dwc3_request *req,
  146. int status)
  147. {
  148. struct dwc3 *dwc = dep->dwc;
  149. unsigned int unmap_after_complete = false;
  150. req->started = false;
  151. list_del(&req->list);
  152. req->trb = NULL;
  153. req->remaining = 0;
  154. if (req->request.status == -EINPROGRESS)
  155. req->request.status = status;
  156. /*
  157. * NOTICE we don't want to unmap before calling ->complete() if we're
  158. * dealing with a bounced ep0 request. If we unmap it here, we would end
  159. * up overwritting the contents of req->buf and this could confuse the
  160. * gadget driver.
  161. */
  162. if (dwc->ep0_bounced && dep->number <= 1) {
  163. dwc->ep0_bounced = false;
  164. unmap_after_complete = true;
  165. } else {
  166. usb_gadget_unmap_request_by_dev(dwc->sysdev,
  167. &req->request, req->direction);
  168. }
  169. trace_dwc3_gadget_giveback(req);
  170. spin_unlock(&dwc->lock);
  171. usb_gadget_giveback_request(&dep->endpoint, &req->request);
  172. spin_lock(&dwc->lock);
  173. if (unmap_after_complete)
  174. usb_gadget_unmap_request_by_dev(dwc->sysdev,
  175. &req->request, req->direction);
  176. if (dep->number > 1)
  177. pm_runtime_put(dwc->dev);
  178. }
  179. int dwc3_send_gadget_generic_command(struct dwc3 *dwc, unsigned cmd, u32 param)
  180. {
  181. u32 timeout = 500;
  182. int status = 0;
  183. int ret = 0;
  184. u32 reg;
  185. dwc3_writel(dwc->regs, DWC3_DGCMDPAR, param);
  186. dwc3_writel(dwc->regs, DWC3_DGCMD, cmd | DWC3_DGCMD_CMDACT);
  187. do {
  188. reg = dwc3_readl(dwc->regs, DWC3_DGCMD);
  189. if (!(reg & DWC3_DGCMD_CMDACT)) {
  190. status = DWC3_DGCMD_STATUS(reg);
  191. if (status)
  192. ret = -EINVAL;
  193. break;
  194. }
  195. } while (--timeout);
  196. if (!timeout) {
  197. ret = -ETIMEDOUT;
  198. status = -ETIMEDOUT;
  199. }
  200. trace_dwc3_gadget_generic_cmd(cmd, param, status);
  201. return ret;
  202. }
  203. static int __dwc3_gadget_wakeup(struct dwc3 *dwc);
  204. int dwc3_send_gadget_ep_cmd(struct dwc3_ep *dep, unsigned cmd,
  205. struct dwc3_gadget_ep_cmd_params *params)
  206. {
  207. const struct usb_endpoint_descriptor *desc = dep->endpoint.desc;
  208. struct dwc3 *dwc = dep->dwc;
  209. u32 timeout = 500;
  210. u32 reg;
  211. int cmd_status = 0;
  212. int susphy = false;
  213. int ret = -EINVAL;
  214. /*
  215. * Synopsys Databook 2.60a states, on section 6.3.2.5.[1-8], that if
  216. * we're issuing an endpoint command, we must check if
  217. * GUSB2PHYCFG.SUSPHY bit is set. If it is, then we need to clear it.
  218. *
  219. * We will also set SUSPHY bit to what it was before returning as stated
  220. * by the same section on Synopsys databook.
  221. */
  222. if (dwc->gadget.speed <= USB_SPEED_HIGH) {
  223. reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0));
  224. if (unlikely(reg & DWC3_GUSB2PHYCFG_SUSPHY)) {
  225. susphy = true;
  226. reg &= ~DWC3_GUSB2PHYCFG_SUSPHY;
  227. dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg);
  228. }
  229. }
  230. if (DWC3_DEPCMD_CMD(cmd) == DWC3_DEPCMD_STARTTRANSFER) {
  231. int needs_wakeup;
  232. needs_wakeup = (dwc->link_state == DWC3_LINK_STATE_U1 ||
  233. dwc->link_state == DWC3_LINK_STATE_U2 ||
  234. dwc->link_state == DWC3_LINK_STATE_U3);
  235. if (unlikely(needs_wakeup)) {
  236. ret = __dwc3_gadget_wakeup(dwc);
  237. dev_WARN_ONCE(dwc->dev, ret, "wakeup failed --> %d\n",
  238. ret);
  239. }
  240. }
  241. dwc3_writel(dep->regs, DWC3_DEPCMDPAR0, params->param0);
  242. dwc3_writel(dep->regs, DWC3_DEPCMDPAR1, params->param1);
  243. dwc3_writel(dep->regs, DWC3_DEPCMDPAR2, params->param2);
  244. /*
  245. * Synopsys Databook 2.60a states in section 6.3.2.5.6 of that if we're
  246. * not relying on XferNotReady, we can make use of a special "No
  247. * Response Update Transfer" command where we should clear both CmdAct
  248. * and CmdIOC bits.
  249. *
  250. * With this, we don't need to wait for command completion and can
  251. * straight away issue further commands to the endpoint.
  252. *
  253. * NOTICE: We're making an assumption that control endpoints will never
  254. * make use of Update Transfer command. This is a safe assumption
  255. * because we can never have more than one request at a time with
  256. * Control Endpoints. If anybody changes that assumption, this chunk
  257. * needs to be updated accordingly.
  258. */
  259. if (DWC3_DEPCMD_CMD(cmd) == DWC3_DEPCMD_UPDATETRANSFER &&
  260. !usb_endpoint_xfer_isoc(desc))
  261. cmd &= ~(DWC3_DEPCMD_CMDIOC | DWC3_DEPCMD_CMDACT);
  262. else
  263. cmd |= DWC3_DEPCMD_CMDACT;
  264. dwc3_writel(dep->regs, DWC3_DEPCMD, cmd);
  265. do {
  266. reg = dwc3_readl(dep->regs, DWC3_DEPCMD);
  267. if (!(reg & DWC3_DEPCMD_CMDACT)) {
  268. cmd_status = DWC3_DEPCMD_STATUS(reg);
  269. switch (cmd_status) {
  270. case 0:
  271. ret = 0;
  272. break;
  273. case DEPEVT_TRANSFER_NO_RESOURCE:
  274. ret = -EINVAL;
  275. break;
  276. case DEPEVT_TRANSFER_BUS_EXPIRY:
  277. /*
  278. * SW issues START TRANSFER command to
  279. * isochronous ep with future frame interval. If
  280. * future interval time has already passed when
  281. * core receives the command, it will respond
  282. * with an error status of 'Bus Expiry'.
  283. *
  284. * Instead of always returning -EINVAL, let's
  285. * give a hint to the gadget driver that this is
  286. * the case by returning -EAGAIN.
  287. */
  288. ret = -EAGAIN;
  289. break;
  290. default:
  291. dev_WARN(dwc->dev, "UNKNOWN cmd status\n");
  292. }
  293. break;
  294. }
  295. } while (--timeout);
  296. if (timeout == 0) {
  297. ret = -ETIMEDOUT;
  298. cmd_status = -ETIMEDOUT;
  299. }
  300. trace_dwc3_gadget_ep_cmd(dep, cmd, params, cmd_status);
  301. if (ret == 0) {
  302. switch (DWC3_DEPCMD_CMD(cmd)) {
  303. case DWC3_DEPCMD_STARTTRANSFER:
  304. dep->flags |= DWC3_EP_TRANSFER_STARTED;
  305. break;
  306. case DWC3_DEPCMD_ENDTRANSFER:
  307. dep->flags &= ~DWC3_EP_TRANSFER_STARTED;
  308. break;
  309. default:
  310. /* nothing */
  311. break;
  312. }
  313. }
  314. if (unlikely(susphy)) {
  315. reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0));
  316. reg |= DWC3_GUSB2PHYCFG_SUSPHY;
  317. dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg);
  318. }
  319. return ret;
  320. }
  321. static int dwc3_send_clear_stall_ep_cmd(struct dwc3_ep *dep)
  322. {
  323. struct dwc3 *dwc = dep->dwc;
  324. struct dwc3_gadget_ep_cmd_params params;
  325. u32 cmd = DWC3_DEPCMD_CLEARSTALL;
  326. /*
  327. * As of core revision 2.60a the recommended programming model
  328. * is to set the ClearPendIN bit when issuing a Clear Stall EP
  329. * command for IN endpoints. This is to prevent an issue where
  330. * some (non-compliant) hosts may not send ACK TPs for pending
  331. * IN transfers due to a mishandled error condition. Synopsys
  332. * STAR 9000614252.
  333. */
  334. if (dep->direction && (dwc->revision >= DWC3_REVISION_260A) &&
  335. (dwc->gadget.speed >= USB_SPEED_SUPER))
  336. cmd |= DWC3_DEPCMD_CLEARPENDIN;
  337. memset(&params, 0, sizeof(params));
  338. return dwc3_send_gadget_ep_cmd(dep, cmd, &params);
  339. }
  340. static dma_addr_t dwc3_trb_dma_offset(struct dwc3_ep *dep,
  341. struct dwc3_trb *trb)
  342. {
  343. u32 offset = (char *) trb - (char *) dep->trb_pool;
  344. return dep->trb_pool_dma + offset;
  345. }
  346. static int dwc3_alloc_trb_pool(struct dwc3_ep *dep)
  347. {
  348. struct dwc3 *dwc = dep->dwc;
  349. if (dep->trb_pool)
  350. return 0;
  351. dep->trb_pool = dma_alloc_coherent(dwc->sysdev,
  352. sizeof(struct dwc3_trb) * DWC3_TRB_NUM,
  353. &dep->trb_pool_dma, GFP_KERNEL);
  354. if (!dep->trb_pool) {
  355. dev_err(dep->dwc->dev, "failed to allocate trb pool for %s\n",
  356. dep->name);
  357. return -ENOMEM;
  358. }
  359. return 0;
  360. }
  361. static void dwc3_free_trb_pool(struct dwc3_ep *dep)
  362. {
  363. struct dwc3 *dwc = dep->dwc;
  364. dma_free_coherent(dwc->sysdev, sizeof(struct dwc3_trb) * DWC3_TRB_NUM,
  365. dep->trb_pool, dep->trb_pool_dma);
  366. dep->trb_pool = NULL;
  367. dep->trb_pool_dma = 0;
  368. }
  369. static int dwc3_gadget_set_xfer_resource(struct dwc3 *dwc, struct dwc3_ep *dep);
  370. /**
  371. * dwc3_gadget_start_config - Configure EP resources
  372. * @dwc: pointer to our controller context structure
  373. * @dep: endpoint that is being enabled
  374. *
  375. * The assignment of transfer resources cannot perfectly follow the
  376. * data book due to the fact that the controller driver does not have
  377. * all knowledge of the configuration in advance. It is given this
  378. * information piecemeal by the composite gadget framework after every
  379. * SET_CONFIGURATION and SET_INTERFACE. Trying to follow the databook
  380. * programming model in this scenario can cause errors. For two
  381. * reasons:
  382. *
  383. * 1) The databook says to do DEPSTARTCFG for every SET_CONFIGURATION
  384. * and SET_INTERFACE (8.1.5). This is incorrect in the scenario of
  385. * multiple interfaces.
  386. *
  387. * 2) The databook does not mention doing more DEPXFERCFG for new
  388. * endpoint on alt setting (8.1.6).
  389. *
  390. * The following simplified method is used instead:
  391. *
  392. * All hardware endpoints can be assigned a transfer resource and this
  393. * setting will stay persistent until either a core reset or
  394. * hibernation. So whenever we do a DEPSTARTCFG(0) we can go ahead and
  395. * do DEPXFERCFG for every hardware endpoint as well. We are
  396. * guaranteed that there are as many transfer resources as endpoints.
  397. *
  398. * This function is called for each endpoint when it is being enabled
  399. * but is triggered only when called for EP0-out, which always happens
  400. * first, and which should only happen in one of the above conditions.
  401. */
  402. static int dwc3_gadget_start_config(struct dwc3 *dwc, struct dwc3_ep *dep)
  403. {
  404. struct dwc3_gadget_ep_cmd_params params;
  405. u32 cmd;
  406. int i;
  407. int ret;
  408. if (dep->number)
  409. return 0;
  410. memset(&params, 0x00, sizeof(params));
  411. cmd = DWC3_DEPCMD_DEPSTARTCFG;
  412. ret = dwc3_send_gadget_ep_cmd(dep, cmd, &params);
  413. if (ret)
  414. return ret;
  415. for (i = 0; i < DWC3_ENDPOINTS_NUM; i++) {
  416. struct dwc3_ep *dep = dwc->eps[i];
  417. if (!dep)
  418. continue;
  419. ret = dwc3_gadget_set_xfer_resource(dwc, dep);
  420. if (ret)
  421. return ret;
  422. }
  423. return 0;
  424. }
  425. static int dwc3_gadget_set_ep_config(struct dwc3 *dwc, struct dwc3_ep *dep,
  426. bool modify, bool restore)
  427. {
  428. const struct usb_ss_ep_comp_descriptor *comp_desc;
  429. const struct usb_endpoint_descriptor *desc;
  430. struct dwc3_gadget_ep_cmd_params params;
  431. if (dev_WARN_ONCE(dwc->dev, modify && restore,
  432. "Can't modify and restore\n"))
  433. return -EINVAL;
  434. comp_desc = dep->endpoint.comp_desc;
  435. desc = dep->endpoint.desc;
  436. memset(&params, 0x00, sizeof(params));
  437. params.param0 = DWC3_DEPCFG_EP_TYPE(usb_endpoint_type(desc))
  438. | DWC3_DEPCFG_MAX_PACKET_SIZE(usb_endpoint_maxp(desc));
  439. /* Burst size is only needed in SuperSpeed mode */
  440. if (dwc->gadget.speed >= USB_SPEED_SUPER) {
  441. u32 burst = dep->endpoint.maxburst;
  442. params.param0 |= DWC3_DEPCFG_BURST_SIZE(burst - 1);
  443. }
  444. if (modify) {
  445. params.param0 |= DWC3_DEPCFG_ACTION_MODIFY;
  446. } else if (restore) {
  447. params.param0 |= DWC3_DEPCFG_ACTION_RESTORE;
  448. params.param2 |= dep->saved_state;
  449. } else {
  450. params.param0 |= DWC3_DEPCFG_ACTION_INIT;
  451. }
  452. if (usb_endpoint_xfer_control(desc))
  453. params.param1 = DWC3_DEPCFG_XFER_COMPLETE_EN;
  454. if (dep->number <= 1 || usb_endpoint_xfer_isoc(desc))
  455. params.param1 |= DWC3_DEPCFG_XFER_NOT_READY_EN;
  456. if (usb_ss_max_streams(comp_desc) && usb_endpoint_xfer_bulk(desc)) {
  457. params.param1 |= DWC3_DEPCFG_STREAM_CAPABLE
  458. | DWC3_DEPCFG_STREAM_EVENT_EN;
  459. dep->stream_capable = true;
  460. }
  461. if (!usb_endpoint_xfer_control(desc))
  462. params.param1 |= DWC3_DEPCFG_XFER_IN_PROGRESS_EN;
  463. /*
  464. * We are doing 1:1 mapping for endpoints, meaning
  465. * Physical Endpoints 2 maps to Logical Endpoint 2 and
  466. * so on. We consider the direction bit as part of the physical
  467. * endpoint number. So USB endpoint 0x81 is 0x03.
  468. */
  469. params.param1 |= DWC3_DEPCFG_EP_NUMBER(dep->number);
  470. /*
  471. * We must use the lower 16 TX FIFOs even though
  472. * HW might have more
  473. */
  474. if (dep->direction)
  475. params.param0 |= DWC3_DEPCFG_FIFO_NUMBER(dep->number >> 1);
  476. if (desc->bInterval) {
  477. params.param1 |= DWC3_DEPCFG_BINTERVAL_M1(desc->bInterval - 1);
  478. dep->interval = 1 << (desc->bInterval - 1);
  479. }
  480. return dwc3_send_gadget_ep_cmd(dep, DWC3_DEPCMD_SETEPCONFIG, &params);
  481. }
  482. static int dwc3_gadget_set_xfer_resource(struct dwc3 *dwc, struct dwc3_ep *dep)
  483. {
  484. struct dwc3_gadget_ep_cmd_params params;
  485. memset(&params, 0x00, sizeof(params));
  486. params.param0 = DWC3_DEPXFERCFG_NUM_XFER_RES(1);
  487. return dwc3_send_gadget_ep_cmd(dep, DWC3_DEPCMD_SETTRANSFRESOURCE,
  488. &params);
  489. }
  490. /**
  491. * __dwc3_gadget_ep_enable - Initializes a HW endpoint
  492. * @dep: endpoint to be initialized
  493. * @desc: USB Endpoint Descriptor
  494. *
  495. * Caller should take care of locking
  496. */
  497. static int __dwc3_gadget_ep_enable(struct dwc3_ep *dep,
  498. bool modify, bool restore)
  499. {
  500. const struct usb_endpoint_descriptor *desc = dep->endpoint.desc;
  501. struct dwc3 *dwc = dep->dwc;
  502. u32 reg;
  503. int ret;
  504. if (!(dep->flags & DWC3_EP_ENABLED)) {
  505. ret = dwc3_gadget_start_config(dwc, dep);
  506. if (ret)
  507. return ret;
  508. }
  509. ret = dwc3_gadget_set_ep_config(dwc, dep, modify, restore);
  510. if (ret)
  511. return ret;
  512. if (!(dep->flags & DWC3_EP_ENABLED)) {
  513. struct dwc3_trb *trb_st_hw;
  514. struct dwc3_trb *trb_link;
  515. dep->type = usb_endpoint_type(desc);
  516. dep->flags |= DWC3_EP_ENABLED;
  517. dep->flags &= ~DWC3_EP_END_TRANSFER_PENDING;
  518. reg = dwc3_readl(dwc->regs, DWC3_DALEPENA);
  519. reg |= DWC3_DALEPENA_EP(dep->number);
  520. dwc3_writel(dwc->regs, DWC3_DALEPENA, reg);
  521. init_waitqueue_head(&dep->wait_end_transfer);
  522. if (usb_endpoint_xfer_control(desc))
  523. goto out;
  524. /* Initialize the TRB ring */
  525. dep->trb_dequeue = 0;
  526. dep->trb_enqueue = 0;
  527. memset(dep->trb_pool, 0,
  528. sizeof(struct dwc3_trb) * DWC3_TRB_NUM);
  529. /* Link TRB. The HWO bit is never reset */
  530. trb_st_hw = &dep->trb_pool[0];
  531. trb_link = &dep->trb_pool[DWC3_TRB_NUM - 1];
  532. trb_link->bpl = lower_32_bits(dwc3_trb_dma_offset(dep, trb_st_hw));
  533. trb_link->bph = upper_32_bits(dwc3_trb_dma_offset(dep, trb_st_hw));
  534. trb_link->ctrl |= DWC3_TRBCTL_LINK_TRB;
  535. trb_link->ctrl |= DWC3_TRB_CTRL_HWO;
  536. }
  537. /*
  538. * Issue StartTransfer here with no-op TRB so we can always rely on No
  539. * Response Update Transfer command.
  540. */
  541. if (usb_endpoint_xfer_bulk(desc)) {
  542. struct dwc3_gadget_ep_cmd_params params;
  543. struct dwc3_trb *trb;
  544. dma_addr_t trb_dma;
  545. u32 cmd;
  546. memset(&params, 0, sizeof(params));
  547. trb = &dep->trb_pool[0];
  548. trb_dma = dwc3_trb_dma_offset(dep, trb);
  549. params.param0 = upper_32_bits(trb_dma);
  550. params.param1 = lower_32_bits(trb_dma);
  551. cmd = DWC3_DEPCMD_STARTTRANSFER;
  552. ret = dwc3_send_gadget_ep_cmd(dep, cmd, &params);
  553. if (ret < 0)
  554. return ret;
  555. dep->flags |= DWC3_EP_BUSY;
  556. dep->resource_index = dwc3_gadget_ep_get_transfer_index(dep);
  557. WARN_ON_ONCE(!dep->resource_index);
  558. }
  559. out:
  560. trace_dwc3_gadget_ep_enable(dep);
  561. return 0;
  562. }
  563. static void dwc3_stop_active_transfer(struct dwc3 *dwc, u32 epnum, bool force);
  564. static void dwc3_remove_requests(struct dwc3 *dwc, struct dwc3_ep *dep)
  565. {
  566. struct dwc3_request *req;
  567. dwc3_stop_active_transfer(dwc, dep->number, true);
  568. /* - giveback all requests to gadget driver */
  569. while (!list_empty(&dep->started_list)) {
  570. req = next_request(&dep->started_list);
  571. dwc3_gadget_giveback(dep, req, -ESHUTDOWN);
  572. }
  573. while (!list_empty(&dep->pending_list)) {
  574. req = next_request(&dep->pending_list);
  575. dwc3_gadget_giveback(dep, req, -ESHUTDOWN);
  576. }
  577. }
  578. /**
  579. * __dwc3_gadget_ep_disable - Disables a HW endpoint
  580. * @dep: the endpoint to disable
  581. *
  582. * This function also removes requests which are currently processed ny the
  583. * hardware and those which are not yet scheduled.
  584. * Caller should take care of locking.
  585. */
  586. static int __dwc3_gadget_ep_disable(struct dwc3_ep *dep)
  587. {
  588. struct dwc3 *dwc = dep->dwc;
  589. u32 reg;
  590. trace_dwc3_gadget_ep_disable(dep);
  591. dwc3_remove_requests(dwc, dep);
  592. /* make sure HW endpoint isn't stalled */
  593. if (dep->flags & DWC3_EP_STALL)
  594. __dwc3_gadget_ep_set_halt(dep, 0, false);
  595. reg = dwc3_readl(dwc->regs, DWC3_DALEPENA);
  596. reg &= ~DWC3_DALEPENA_EP(dep->number);
  597. dwc3_writel(dwc->regs, DWC3_DALEPENA, reg);
  598. dep->stream_capable = false;
  599. dep->type = 0;
  600. dep->flags &= DWC3_EP_END_TRANSFER_PENDING;
  601. /* Clear out the ep descriptors for non-ep0 */
  602. if (dep->number > 1) {
  603. dep->endpoint.comp_desc = NULL;
  604. dep->endpoint.desc = NULL;
  605. }
  606. return 0;
  607. }
  608. /* -------------------------------------------------------------------------- */
  609. static int dwc3_gadget_ep0_enable(struct usb_ep *ep,
  610. const struct usb_endpoint_descriptor *desc)
  611. {
  612. return -EINVAL;
  613. }
  614. static int dwc3_gadget_ep0_disable(struct usb_ep *ep)
  615. {
  616. return -EINVAL;
  617. }
  618. /* -------------------------------------------------------------------------- */
  619. static int dwc3_gadget_ep_enable(struct usb_ep *ep,
  620. const struct usb_endpoint_descriptor *desc)
  621. {
  622. struct dwc3_ep *dep;
  623. struct dwc3 *dwc;
  624. unsigned long flags;
  625. int ret;
  626. if (!ep || !desc || desc->bDescriptorType != USB_DT_ENDPOINT) {
  627. pr_debug("dwc3: invalid parameters\n");
  628. return -EINVAL;
  629. }
  630. if (!desc->wMaxPacketSize) {
  631. pr_debug("dwc3: missing wMaxPacketSize\n");
  632. return -EINVAL;
  633. }
  634. dep = to_dwc3_ep(ep);
  635. dwc = dep->dwc;
  636. if (dev_WARN_ONCE(dwc->dev, dep->flags & DWC3_EP_ENABLED,
  637. "%s is already enabled\n",
  638. dep->name))
  639. return 0;
  640. spin_lock_irqsave(&dwc->lock, flags);
  641. ret = __dwc3_gadget_ep_enable(dep, false, false);
  642. spin_unlock_irqrestore(&dwc->lock, flags);
  643. return ret;
  644. }
  645. static int dwc3_gadget_ep_disable(struct usb_ep *ep)
  646. {
  647. struct dwc3_ep *dep;
  648. struct dwc3 *dwc;
  649. unsigned long flags;
  650. int ret;
  651. if (!ep) {
  652. pr_debug("dwc3: invalid parameters\n");
  653. return -EINVAL;
  654. }
  655. dep = to_dwc3_ep(ep);
  656. dwc = dep->dwc;
  657. if (dev_WARN_ONCE(dwc->dev, !(dep->flags & DWC3_EP_ENABLED),
  658. "%s is already disabled\n",
  659. dep->name))
  660. return 0;
  661. spin_lock_irqsave(&dwc->lock, flags);
  662. ret = __dwc3_gadget_ep_disable(dep);
  663. spin_unlock_irqrestore(&dwc->lock, flags);
  664. return ret;
  665. }
  666. static struct usb_request *dwc3_gadget_ep_alloc_request(struct usb_ep *ep,
  667. gfp_t gfp_flags)
  668. {
  669. struct dwc3_request *req;
  670. struct dwc3_ep *dep = to_dwc3_ep(ep);
  671. req = kzalloc(sizeof(*req), gfp_flags);
  672. if (!req)
  673. return NULL;
  674. req->epnum = dep->number;
  675. req->dep = dep;
  676. dep->allocated_requests++;
  677. trace_dwc3_alloc_request(req);
  678. return &req->request;
  679. }
  680. static void dwc3_gadget_ep_free_request(struct usb_ep *ep,
  681. struct usb_request *request)
  682. {
  683. struct dwc3_request *req = to_dwc3_request(request);
  684. struct dwc3_ep *dep = to_dwc3_ep(ep);
  685. dep->allocated_requests--;
  686. trace_dwc3_free_request(req);
  687. kfree(req);
  688. }
  689. static u32 dwc3_calc_trbs_left(struct dwc3_ep *dep);
  690. static void __dwc3_prepare_one_trb(struct dwc3_ep *dep, struct dwc3_trb *trb,
  691. dma_addr_t dma, unsigned length, unsigned chain, unsigned node,
  692. unsigned stream_id, unsigned short_not_ok, unsigned no_interrupt)
  693. {
  694. struct dwc3 *dwc = dep->dwc;
  695. struct usb_gadget *gadget = &dwc->gadget;
  696. enum usb_device_speed speed = gadget->speed;
  697. dwc3_ep_inc_enq(dep);
  698. trb->size = DWC3_TRB_SIZE_LENGTH(length);
  699. trb->bpl = lower_32_bits(dma);
  700. trb->bph = upper_32_bits(dma);
  701. switch (usb_endpoint_type(dep->endpoint.desc)) {
  702. case USB_ENDPOINT_XFER_CONTROL:
  703. trb->ctrl = DWC3_TRBCTL_CONTROL_SETUP;
  704. break;
  705. case USB_ENDPOINT_XFER_ISOC:
  706. if (!node) {
  707. trb->ctrl = DWC3_TRBCTL_ISOCHRONOUS_FIRST;
  708. if (speed == USB_SPEED_HIGH) {
  709. struct usb_ep *ep = &dep->endpoint;
  710. trb->size |= DWC3_TRB_SIZE_PCM1(ep->mult - 1);
  711. }
  712. } else {
  713. trb->ctrl = DWC3_TRBCTL_ISOCHRONOUS;
  714. }
  715. /* always enable Interrupt on Missed ISOC */
  716. trb->ctrl |= DWC3_TRB_CTRL_ISP_IMI;
  717. break;
  718. case USB_ENDPOINT_XFER_BULK:
  719. case USB_ENDPOINT_XFER_INT:
  720. trb->ctrl = DWC3_TRBCTL_NORMAL;
  721. break;
  722. default:
  723. /*
  724. * This is only possible with faulty memory because we
  725. * checked it already :)
  726. */
  727. dev_WARN(dwc->dev, "Unknown endpoint type %d\n",
  728. usb_endpoint_type(dep->endpoint.desc));
  729. }
  730. /* always enable Continue on Short Packet */
  731. if (usb_endpoint_dir_out(dep->endpoint.desc)) {
  732. trb->ctrl |= DWC3_TRB_CTRL_CSP;
  733. if (short_not_ok)
  734. trb->ctrl |= DWC3_TRB_CTRL_ISP_IMI;
  735. }
  736. if ((!no_interrupt && !chain) ||
  737. (dwc3_calc_trbs_left(dep) == 0))
  738. trb->ctrl |= DWC3_TRB_CTRL_IOC;
  739. if (chain)
  740. trb->ctrl |= DWC3_TRB_CTRL_CHN;
  741. if (usb_endpoint_xfer_bulk(dep->endpoint.desc) && dep->stream_capable)
  742. trb->ctrl |= DWC3_TRB_CTRL_SID_SOFN(stream_id);
  743. trb->ctrl |= DWC3_TRB_CTRL_HWO;
  744. trace_dwc3_prepare_trb(dep, trb);
  745. }
  746. /**
  747. * dwc3_prepare_one_trb - setup one TRB from one request
  748. * @dep: endpoint for which this request is prepared
  749. * @req: dwc3_request pointer
  750. * @chain: should this TRB be chained to the next?
  751. * @node: only for isochronous endpoints. First TRB needs different type.
  752. */
  753. static void dwc3_prepare_one_trb(struct dwc3_ep *dep,
  754. struct dwc3_request *req, unsigned chain, unsigned node)
  755. {
  756. struct dwc3_trb *trb;
  757. unsigned length = req->request.length;
  758. unsigned stream_id = req->request.stream_id;
  759. unsigned short_not_ok = req->request.short_not_ok;
  760. unsigned no_interrupt = req->request.no_interrupt;
  761. dma_addr_t dma = req->request.dma;
  762. trb = &dep->trb_pool[dep->trb_enqueue];
  763. if (!req->trb) {
  764. dwc3_gadget_move_started_request(req);
  765. req->trb = trb;
  766. req->trb_dma = dwc3_trb_dma_offset(dep, trb);
  767. dep->queued_requests++;
  768. }
  769. __dwc3_prepare_one_trb(dep, trb, dma, length, chain, node,
  770. stream_id, short_not_ok, no_interrupt);
  771. }
  772. /**
  773. * dwc3_ep_prev_trb() - Returns the previous TRB in the ring
  774. * @dep: The endpoint with the TRB ring
  775. * @index: The index of the current TRB in the ring
  776. *
  777. * Returns the TRB prior to the one pointed to by the index. If the
  778. * index is 0, we will wrap backwards, skip the link TRB, and return
  779. * the one just before that.
  780. */
  781. static struct dwc3_trb *dwc3_ep_prev_trb(struct dwc3_ep *dep, u8 index)
  782. {
  783. u8 tmp = index;
  784. if (!tmp)
  785. tmp = DWC3_TRB_NUM - 1;
  786. return &dep->trb_pool[tmp - 1];
  787. }
  788. static u32 dwc3_calc_trbs_left(struct dwc3_ep *dep)
  789. {
  790. struct dwc3_trb *tmp;
  791. struct dwc3 *dwc = dep->dwc;
  792. u8 trbs_left;
  793. /*
  794. * If enqueue & dequeue are equal than it is either full or empty.
  795. *
  796. * One way to know for sure is if the TRB right before us has HWO bit
  797. * set or not. If it has, then we're definitely full and can't fit any
  798. * more transfers in our ring.
  799. */
  800. if (dep->trb_enqueue == dep->trb_dequeue) {
  801. tmp = dwc3_ep_prev_trb(dep, dep->trb_enqueue);
  802. if (dev_WARN_ONCE(dwc->dev, tmp->ctrl & DWC3_TRB_CTRL_HWO,
  803. "%s No TRBS left\n", dep->name))
  804. return 0;
  805. return DWC3_TRB_NUM - 1;
  806. }
  807. trbs_left = dep->trb_dequeue - dep->trb_enqueue;
  808. trbs_left &= (DWC3_TRB_NUM - 1);
  809. if (dep->trb_dequeue < dep->trb_enqueue)
  810. trbs_left--;
  811. return trbs_left;
  812. }
  813. static void dwc3_prepare_one_trb_sg(struct dwc3_ep *dep,
  814. struct dwc3_request *req)
  815. {
  816. struct scatterlist *sg = req->sg;
  817. struct scatterlist *s;
  818. int i;
  819. for_each_sg(sg, s, req->num_pending_sgs, i) {
  820. unsigned int length = req->request.length;
  821. unsigned int maxp = usb_endpoint_maxp(dep->endpoint.desc);
  822. unsigned int rem = length % maxp;
  823. unsigned chain = true;
  824. if (sg_is_last(s))
  825. chain = false;
  826. if (rem && usb_endpoint_dir_out(dep->endpoint.desc) && !chain) {
  827. struct dwc3 *dwc = dep->dwc;
  828. struct dwc3_trb *trb;
  829. req->unaligned = true;
  830. /* prepare normal TRB */
  831. dwc3_prepare_one_trb(dep, req, true, i);
  832. /* Now prepare one extra TRB to align transfer size */
  833. trb = &dep->trb_pool[dep->trb_enqueue];
  834. __dwc3_prepare_one_trb(dep, trb, dwc->bounce_addr,
  835. maxp - rem, false, 0,
  836. req->request.stream_id,
  837. req->request.short_not_ok,
  838. req->request.no_interrupt);
  839. } else {
  840. dwc3_prepare_one_trb(dep, req, chain, i);
  841. }
  842. if (!dwc3_calc_trbs_left(dep))
  843. break;
  844. }
  845. }
  846. static void dwc3_prepare_one_trb_linear(struct dwc3_ep *dep,
  847. struct dwc3_request *req)
  848. {
  849. unsigned int length = req->request.length;
  850. unsigned int maxp = usb_endpoint_maxp(dep->endpoint.desc);
  851. unsigned int rem = length % maxp;
  852. if (rem && usb_endpoint_dir_out(dep->endpoint.desc)) {
  853. struct dwc3 *dwc = dep->dwc;
  854. struct dwc3_trb *trb;
  855. req->unaligned = true;
  856. /* prepare normal TRB */
  857. dwc3_prepare_one_trb(dep, req, true, 0);
  858. /* Now prepare one extra TRB to align transfer size */
  859. trb = &dep->trb_pool[dep->trb_enqueue];
  860. __dwc3_prepare_one_trb(dep, trb, dwc->bounce_addr, maxp - rem,
  861. false, 0, req->request.stream_id,
  862. req->request.short_not_ok,
  863. req->request.no_interrupt);
  864. } else {
  865. dwc3_prepare_one_trb(dep, req, false, 0);
  866. }
  867. }
  868. /*
  869. * dwc3_prepare_trbs - setup TRBs from requests
  870. * @dep: endpoint for which requests are being prepared
  871. *
  872. * The function goes through the requests list and sets up TRBs for the
  873. * transfers. The function returns once there are no more TRBs available or
  874. * it runs out of requests.
  875. */
  876. static void dwc3_prepare_trbs(struct dwc3_ep *dep)
  877. {
  878. struct dwc3_request *req, *n;
  879. BUILD_BUG_ON_NOT_POWER_OF_2(DWC3_TRB_NUM);
  880. if (!dwc3_calc_trbs_left(dep))
  881. return;
  882. /*
  883. * We can get in a situation where there's a request in the started list
  884. * but there weren't enough TRBs to fully kick it in the first time
  885. * around, so it has been waiting for more TRBs to be freed up.
  886. *
  887. * In that case, we should check if we have a request with pending_sgs
  888. * in the started list and prepare TRBs for that request first,
  889. * otherwise we will prepare TRBs completely out of order and that will
  890. * break things.
  891. */
  892. list_for_each_entry(req, &dep->started_list, list) {
  893. if (req->num_pending_sgs > 0)
  894. dwc3_prepare_one_trb_sg(dep, req);
  895. if (!dwc3_calc_trbs_left(dep))
  896. return;
  897. }
  898. list_for_each_entry_safe(req, n, &dep->pending_list, list) {
  899. if (req->num_pending_sgs > 0)
  900. dwc3_prepare_one_trb_sg(dep, req);
  901. else
  902. dwc3_prepare_one_trb_linear(dep, req);
  903. if (!dwc3_calc_trbs_left(dep))
  904. return;
  905. }
  906. }
  907. static int __dwc3_gadget_kick_transfer(struct dwc3_ep *dep, u16 cmd_param)
  908. {
  909. struct dwc3_gadget_ep_cmd_params params;
  910. struct dwc3_request *req;
  911. int starting;
  912. int ret;
  913. u32 cmd;
  914. starting = !(dep->flags & DWC3_EP_BUSY);
  915. dwc3_prepare_trbs(dep);
  916. req = next_request(&dep->started_list);
  917. if (!req) {
  918. dep->flags |= DWC3_EP_PENDING_REQUEST;
  919. return 0;
  920. }
  921. memset(&params, 0, sizeof(params));
  922. if (starting) {
  923. params.param0 = upper_32_bits(req->trb_dma);
  924. params.param1 = lower_32_bits(req->trb_dma);
  925. cmd = DWC3_DEPCMD_STARTTRANSFER |
  926. DWC3_DEPCMD_PARAM(cmd_param);
  927. } else {
  928. cmd = DWC3_DEPCMD_UPDATETRANSFER |
  929. DWC3_DEPCMD_PARAM(dep->resource_index);
  930. }
  931. ret = dwc3_send_gadget_ep_cmd(dep, cmd, &params);
  932. if (ret < 0) {
  933. /*
  934. * FIXME we need to iterate over the list of requests
  935. * here and stop, unmap, free and del each of the linked
  936. * requests instead of what we do now.
  937. */
  938. if (req->trb)
  939. memset(req->trb, 0, sizeof(struct dwc3_trb));
  940. dep->queued_requests--;
  941. dwc3_gadget_giveback(dep, req, ret);
  942. return ret;
  943. }
  944. dep->flags |= DWC3_EP_BUSY;
  945. if (starting) {
  946. dep->resource_index = dwc3_gadget_ep_get_transfer_index(dep);
  947. WARN_ON_ONCE(!dep->resource_index);
  948. }
  949. return 0;
  950. }
  951. static int __dwc3_gadget_get_frame(struct dwc3 *dwc)
  952. {
  953. u32 reg;
  954. reg = dwc3_readl(dwc->regs, DWC3_DSTS);
  955. return DWC3_DSTS_SOFFN(reg);
  956. }
  957. static void __dwc3_gadget_start_isoc(struct dwc3 *dwc,
  958. struct dwc3_ep *dep, u32 cur_uf)
  959. {
  960. u32 uf;
  961. if (list_empty(&dep->pending_list)) {
  962. dev_info(dwc->dev, "%s: ran out of requests\n",
  963. dep->name);
  964. dep->flags |= DWC3_EP_PENDING_REQUEST;
  965. return;
  966. }
  967. /* 4 micro frames in the future */
  968. uf = cur_uf + dep->interval * 4;
  969. __dwc3_gadget_kick_transfer(dep, uf);
  970. }
  971. static void dwc3_gadget_start_isoc(struct dwc3 *dwc,
  972. struct dwc3_ep *dep, const struct dwc3_event_depevt *event)
  973. {
  974. u32 cur_uf, mask;
  975. mask = ~(dep->interval - 1);
  976. cur_uf = event->parameters & mask;
  977. __dwc3_gadget_start_isoc(dwc, dep, cur_uf);
  978. }
  979. static int __dwc3_gadget_ep_queue(struct dwc3_ep *dep, struct dwc3_request *req)
  980. {
  981. struct dwc3 *dwc = dep->dwc;
  982. int ret;
  983. if (!dep->endpoint.desc) {
  984. dev_err(dwc->dev, "%s: can't queue to disabled endpoint\n",
  985. dep->name);
  986. return -ESHUTDOWN;
  987. }
  988. if (WARN(req->dep != dep, "request %p belongs to '%s'\n",
  989. &req->request, req->dep->name)) {
  990. dev_err(dwc->dev, "%s: request %p belongs to '%s'\n",
  991. dep->name, &req->request, req->dep->name);
  992. return -EINVAL;
  993. }
  994. pm_runtime_get(dwc->dev);
  995. req->request.actual = 0;
  996. req->request.status = -EINPROGRESS;
  997. req->direction = dep->direction;
  998. req->epnum = dep->number;
  999. trace_dwc3_ep_queue(req);
  1000. ret = usb_gadget_map_request_by_dev(dwc->sysdev, &req->request,
  1001. dep->direction);
  1002. if (ret)
  1003. return ret;
  1004. req->sg = req->request.sg;
  1005. req->num_pending_sgs = req->request.num_mapped_sgs;
  1006. list_add_tail(&req->list, &dep->pending_list);
  1007. /*
  1008. * NOTICE: Isochronous endpoints should NEVER be prestarted. We must
  1009. * wait for a XferNotReady event so we will know what's the current
  1010. * (micro-)frame number.
  1011. *
  1012. * Without this trick, we are very, very likely gonna get Bus Expiry
  1013. * errors which will force us issue EndTransfer command.
  1014. */
  1015. if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
  1016. if ((dep->flags & DWC3_EP_PENDING_REQUEST)) {
  1017. if (dep->flags & DWC3_EP_TRANSFER_STARTED) {
  1018. dwc3_stop_active_transfer(dwc, dep->number, true);
  1019. dep->flags = DWC3_EP_ENABLED;
  1020. } else {
  1021. u32 cur_uf;
  1022. cur_uf = __dwc3_gadget_get_frame(dwc);
  1023. __dwc3_gadget_start_isoc(dwc, dep, cur_uf);
  1024. dep->flags &= ~DWC3_EP_PENDING_REQUEST;
  1025. }
  1026. }
  1027. return 0;
  1028. }
  1029. if (!dwc3_calc_trbs_left(dep))
  1030. return 0;
  1031. ret = __dwc3_gadget_kick_transfer(dep, 0);
  1032. if (ret == -EBUSY)
  1033. ret = 0;
  1034. return ret;
  1035. }
  1036. static void __dwc3_gadget_ep_zlp_complete(struct usb_ep *ep,
  1037. struct usb_request *request)
  1038. {
  1039. dwc3_gadget_ep_free_request(ep, request);
  1040. }
  1041. static int __dwc3_gadget_ep_queue_zlp(struct dwc3 *dwc, struct dwc3_ep *dep)
  1042. {
  1043. struct dwc3_request *req;
  1044. struct usb_request *request;
  1045. struct usb_ep *ep = &dep->endpoint;
  1046. request = dwc3_gadget_ep_alloc_request(ep, GFP_ATOMIC);
  1047. if (!request)
  1048. return -ENOMEM;
  1049. request->length = 0;
  1050. request->buf = dwc->zlp_buf;
  1051. request->complete = __dwc3_gadget_ep_zlp_complete;
  1052. req = to_dwc3_request(request);
  1053. return __dwc3_gadget_ep_queue(dep, req);
  1054. }
  1055. static int dwc3_gadget_ep_queue(struct usb_ep *ep, struct usb_request *request,
  1056. gfp_t gfp_flags)
  1057. {
  1058. struct dwc3_request *req = to_dwc3_request(request);
  1059. struct dwc3_ep *dep = to_dwc3_ep(ep);
  1060. struct dwc3 *dwc = dep->dwc;
  1061. unsigned long flags;
  1062. int ret;
  1063. spin_lock_irqsave(&dwc->lock, flags);
  1064. ret = __dwc3_gadget_ep_queue(dep, req);
  1065. /*
  1066. * Okay, here's the thing, if gadget driver has requested for a ZLP by
  1067. * setting request->zero, instead of doing magic, we will just queue an
  1068. * extra usb_request ourselves so that it gets handled the same way as
  1069. * any other request.
  1070. */
  1071. if (ret == 0 && request->zero && request->length &&
  1072. (request->length % ep->maxpacket == 0))
  1073. ret = __dwc3_gadget_ep_queue_zlp(dwc, dep);
  1074. spin_unlock_irqrestore(&dwc->lock, flags);
  1075. return ret;
  1076. }
  1077. static int dwc3_gadget_ep_dequeue(struct usb_ep *ep,
  1078. struct usb_request *request)
  1079. {
  1080. struct dwc3_request *req = to_dwc3_request(request);
  1081. struct dwc3_request *r = NULL;
  1082. struct dwc3_ep *dep = to_dwc3_ep(ep);
  1083. struct dwc3 *dwc = dep->dwc;
  1084. unsigned long flags;
  1085. int ret = 0;
  1086. trace_dwc3_ep_dequeue(req);
  1087. spin_lock_irqsave(&dwc->lock, flags);
  1088. list_for_each_entry(r, &dep->pending_list, list) {
  1089. if (r == req)
  1090. break;
  1091. }
  1092. if (r != req) {
  1093. list_for_each_entry(r, &dep->started_list, list) {
  1094. if (r == req)
  1095. break;
  1096. }
  1097. if (r == req) {
  1098. /* wait until it is processed */
  1099. dwc3_stop_active_transfer(dwc, dep->number, true);
  1100. /*
  1101. * If request was already started, this means we had to
  1102. * stop the transfer. With that we also need to ignore
  1103. * all TRBs used by the request, however TRBs can only
  1104. * be modified after completion of END_TRANSFER
  1105. * command. So what we do here is that we wait for
  1106. * END_TRANSFER completion and only after that, we jump
  1107. * over TRBs by clearing HWO and incrementing dequeue
  1108. * pointer.
  1109. *
  1110. * Note that we have 2 possible types of transfers here:
  1111. *
  1112. * i) Linear buffer request
  1113. * ii) SG-list based request
  1114. *
  1115. * SG-list based requests will have r->num_pending_sgs
  1116. * set to a valid number (> 0). Linear requests,
  1117. * normally use a single TRB.
  1118. *
  1119. * For each of these two cases, if r->unaligned flag is
  1120. * set, one extra TRB has been used to align transfer
  1121. * size to wMaxPacketSize.
  1122. *
  1123. * All of these cases need to be taken into
  1124. * consideration so we don't mess up our TRB ring
  1125. * pointers.
  1126. */
  1127. wait_event_lock_irq(dep->wait_end_transfer,
  1128. !(dep->flags & DWC3_EP_END_TRANSFER_PENDING),
  1129. dwc->lock);
  1130. if (!r->trb)
  1131. goto out1;
  1132. if (r->num_pending_sgs) {
  1133. struct dwc3_trb *trb;
  1134. int i = 0;
  1135. for (i = 0; i < r->num_pending_sgs; i++) {
  1136. trb = r->trb + i;
  1137. trb->ctrl &= ~DWC3_TRB_CTRL_HWO;
  1138. dwc3_ep_inc_deq(dep);
  1139. }
  1140. if (r->unaligned) {
  1141. trb = r->trb + r->num_pending_sgs + 1;
  1142. trb->ctrl &= ~DWC3_TRB_CTRL_HWO;
  1143. dwc3_ep_inc_deq(dep);
  1144. }
  1145. } else {
  1146. struct dwc3_trb *trb = r->trb;
  1147. trb->ctrl &= ~DWC3_TRB_CTRL_HWO;
  1148. dwc3_ep_inc_deq(dep);
  1149. if (r->unaligned) {
  1150. trb = r->trb + 1;
  1151. trb->ctrl &= ~DWC3_TRB_CTRL_HWO;
  1152. dwc3_ep_inc_deq(dep);
  1153. }
  1154. }
  1155. goto out1;
  1156. }
  1157. dev_err(dwc->dev, "request %p was not queued to %s\n",
  1158. request, ep->name);
  1159. ret = -EINVAL;
  1160. goto out0;
  1161. }
  1162. out1:
  1163. /* giveback the request */
  1164. dep->queued_requests--;
  1165. dwc3_gadget_giveback(dep, req, -ECONNRESET);
  1166. out0:
  1167. spin_unlock_irqrestore(&dwc->lock, flags);
  1168. return ret;
  1169. }
  1170. int __dwc3_gadget_ep_set_halt(struct dwc3_ep *dep, int value, int protocol)
  1171. {
  1172. struct dwc3_gadget_ep_cmd_params params;
  1173. struct dwc3 *dwc = dep->dwc;
  1174. int ret;
  1175. if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
  1176. dev_err(dwc->dev, "%s is of Isochronous type\n", dep->name);
  1177. return -EINVAL;
  1178. }
  1179. memset(&params, 0x00, sizeof(params));
  1180. if (value) {
  1181. struct dwc3_trb *trb;
  1182. unsigned transfer_in_flight;
  1183. unsigned started;
  1184. if (dep->flags & DWC3_EP_STALL)
  1185. return 0;
  1186. if (dep->number > 1)
  1187. trb = dwc3_ep_prev_trb(dep, dep->trb_enqueue);
  1188. else
  1189. trb = &dwc->ep0_trb[dep->trb_enqueue];
  1190. transfer_in_flight = trb->ctrl & DWC3_TRB_CTRL_HWO;
  1191. started = !list_empty(&dep->started_list);
  1192. if (!protocol && ((dep->direction && transfer_in_flight) ||
  1193. (!dep->direction && started))) {
  1194. return -EAGAIN;
  1195. }
  1196. ret = dwc3_send_gadget_ep_cmd(dep, DWC3_DEPCMD_SETSTALL,
  1197. &params);
  1198. if (ret)
  1199. dev_err(dwc->dev, "failed to set STALL on %s\n",
  1200. dep->name);
  1201. else
  1202. dep->flags |= DWC3_EP_STALL;
  1203. } else {
  1204. if (!(dep->flags & DWC3_EP_STALL))
  1205. return 0;
  1206. ret = dwc3_send_clear_stall_ep_cmd(dep);
  1207. if (ret)
  1208. dev_err(dwc->dev, "failed to clear STALL on %s\n",
  1209. dep->name);
  1210. else
  1211. dep->flags &= ~(DWC3_EP_STALL | DWC3_EP_WEDGE);
  1212. }
  1213. return ret;
  1214. }
  1215. static int dwc3_gadget_ep_set_halt(struct usb_ep *ep, int value)
  1216. {
  1217. struct dwc3_ep *dep = to_dwc3_ep(ep);
  1218. struct dwc3 *dwc = dep->dwc;
  1219. unsigned long flags;
  1220. int ret;
  1221. spin_lock_irqsave(&dwc->lock, flags);
  1222. ret = __dwc3_gadget_ep_set_halt(dep, value, false);
  1223. spin_unlock_irqrestore(&dwc->lock, flags);
  1224. return ret;
  1225. }
  1226. static int dwc3_gadget_ep_set_wedge(struct usb_ep *ep)
  1227. {
  1228. struct dwc3_ep *dep = to_dwc3_ep(ep);
  1229. struct dwc3 *dwc = dep->dwc;
  1230. unsigned long flags;
  1231. int ret;
  1232. spin_lock_irqsave(&dwc->lock, flags);
  1233. dep->flags |= DWC3_EP_WEDGE;
  1234. if (dep->number == 0 || dep->number == 1)
  1235. ret = __dwc3_gadget_ep0_set_halt(ep, 1);
  1236. else
  1237. ret = __dwc3_gadget_ep_set_halt(dep, 1, false);
  1238. spin_unlock_irqrestore(&dwc->lock, flags);
  1239. return ret;
  1240. }
  1241. /* -------------------------------------------------------------------------- */
  1242. static struct usb_endpoint_descriptor dwc3_gadget_ep0_desc = {
  1243. .bLength = USB_DT_ENDPOINT_SIZE,
  1244. .bDescriptorType = USB_DT_ENDPOINT,
  1245. .bmAttributes = USB_ENDPOINT_XFER_CONTROL,
  1246. };
  1247. static const struct usb_ep_ops dwc3_gadget_ep0_ops = {
  1248. .enable = dwc3_gadget_ep0_enable,
  1249. .disable = dwc3_gadget_ep0_disable,
  1250. .alloc_request = dwc3_gadget_ep_alloc_request,
  1251. .free_request = dwc3_gadget_ep_free_request,
  1252. .queue = dwc3_gadget_ep0_queue,
  1253. .dequeue = dwc3_gadget_ep_dequeue,
  1254. .set_halt = dwc3_gadget_ep0_set_halt,
  1255. .set_wedge = dwc3_gadget_ep_set_wedge,
  1256. };
  1257. static const struct usb_ep_ops dwc3_gadget_ep_ops = {
  1258. .enable = dwc3_gadget_ep_enable,
  1259. .disable = dwc3_gadget_ep_disable,
  1260. .alloc_request = dwc3_gadget_ep_alloc_request,
  1261. .free_request = dwc3_gadget_ep_free_request,
  1262. .queue = dwc3_gadget_ep_queue,
  1263. .dequeue = dwc3_gadget_ep_dequeue,
  1264. .set_halt = dwc3_gadget_ep_set_halt,
  1265. .set_wedge = dwc3_gadget_ep_set_wedge,
  1266. };
  1267. /* -------------------------------------------------------------------------- */
  1268. static int dwc3_gadget_get_frame(struct usb_gadget *g)
  1269. {
  1270. struct dwc3 *dwc = gadget_to_dwc(g);
  1271. return __dwc3_gadget_get_frame(dwc);
  1272. }
  1273. static int __dwc3_gadget_wakeup(struct dwc3 *dwc)
  1274. {
  1275. int retries;
  1276. int ret;
  1277. u32 reg;
  1278. u8 link_state;
  1279. u8 speed;
  1280. /*
  1281. * According to the Databook Remote wakeup request should
  1282. * be issued only when the device is in early suspend state.
  1283. *
  1284. * We can check that via USB Link State bits in DSTS register.
  1285. */
  1286. reg = dwc3_readl(dwc->regs, DWC3_DSTS);
  1287. speed = reg & DWC3_DSTS_CONNECTSPD;
  1288. if ((speed == DWC3_DSTS_SUPERSPEED) ||
  1289. (speed == DWC3_DSTS_SUPERSPEED_PLUS))
  1290. return 0;
  1291. link_state = DWC3_DSTS_USBLNKST(reg);
  1292. switch (link_state) {
  1293. case DWC3_LINK_STATE_RX_DET: /* in HS, means Early Suspend */
  1294. case DWC3_LINK_STATE_U3: /* in HS, means SUSPEND */
  1295. break;
  1296. default:
  1297. return -EINVAL;
  1298. }
  1299. ret = dwc3_gadget_set_link_state(dwc, DWC3_LINK_STATE_RECOV);
  1300. if (ret < 0) {
  1301. dev_err(dwc->dev, "failed to put link in Recovery\n");
  1302. return ret;
  1303. }
  1304. /* Recent versions do this automatically */
  1305. if (dwc->revision < DWC3_REVISION_194A) {
  1306. /* write zeroes to Link Change Request */
  1307. reg = dwc3_readl(dwc->regs, DWC3_DCTL);
  1308. reg &= ~DWC3_DCTL_ULSTCHNGREQ_MASK;
  1309. dwc3_writel(dwc->regs, DWC3_DCTL, reg);
  1310. }
  1311. /* poll until Link State changes to ON */
  1312. retries = 20000;
  1313. while (retries--) {
  1314. reg = dwc3_readl(dwc->regs, DWC3_DSTS);
  1315. /* in HS, means ON */
  1316. if (DWC3_DSTS_USBLNKST(reg) == DWC3_LINK_STATE_U0)
  1317. break;
  1318. }
  1319. if (DWC3_DSTS_USBLNKST(reg) != DWC3_LINK_STATE_U0) {
  1320. dev_err(dwc->dev, "failed to send remote wakeup\n");
  1321. return -EINVAL;
  1322. }
  1323. return 0;
  1324. }
  1325. static int dwc3_gadget_wakeup(struct usb_gadget *g)
  1326. {
  1327. struct dwc3 *dwc = gadget_to_dwc(g);
  1328. unsigned long flags;
  1329. int ret;
  1330. spin_lock_irqsave(&dwc->lock, flags);
  1331. ret = __dwc3_gadget_wakeup(dwc);
  1332. spin_unlock_irqrestore(&dwc->lock, flags);
  1333. return ret;
  1334. }
  1335. static int dwc3_gadget_set_selfpowered(struct usb_gadget *g,
  1336. int is_selfpowered)
  1337. {
  1338. struct dwc3 *dwc = gadget_to_dwc(g);
  1339. unsigned long flags;
  1340. spin_lock_irqsave(&dwc->lock, flags);
  1341. g->is_selfpowered = !!is_selfpowered;
  1342. spin_unlock_irqrestore(&dwc->lock, flags);
  1343. return 0;
  1344. }
  1345. static int dwc3_gadget_run_stop(struct dwc3 *dwc, int is_on, int suspend)
  1346. {
  1347. u32 reg;
  1348. u32 timeout = 500;
  1349. if (pm_runtime_suspended(dwc->dev))
  1350. return 0;
  1351. reg = dwc3_readl(dwc->regs, DWC3_DCTL);
  1352. if (is_on) {
  1353. if (dwc->revision <= DWC3_REVISION_187A) {
  1354. reg &= ~DWC3_DCTL_TRGTULST_MASK;
  1355. reg |= DWC3_DCTL_TRGTULST_RX_DET;
  1356. }
  1357. if (dwc->revision >= DWC3_REVISION_194A)
  1358. reg &= ~DWC3_DCTL_KEEP_CONNECT;
  1359. reg |= DWC3_DCTL_RUN_STOP;
  1360. if (dwc->has_hibernation)
  1361. reg |= DWC3_DCTL_KEEP_CONNECT;
  1362. dwc->pullups_connected = true;
  1363. } else {
  1364. reg &= ~DWC3_DCTL_RUN_STOP;
  1365. if (dwc->has_hibernation && !suspend)
  1366. reg &= ~DWC3_DCTL_KEEP_CONNECT;
  1367. dwc->pullups_connected = false;
  1368. }
  1369. dwc3_writel(dwc->regs, DWC3_DCTL, reg);
  1370. do {
  1371. reg = dwc3_readl(dwc->regs, DWC3_DSTS);
  1372. reg &= DWC3_DSTS_DEVCTRLHLT;
  1373. } while (--timeout && !(!is_on ^ !reg));
  1374. if (!timeout)
  1375. return -ETIMEDOUT;
  1376. return 0;
  1377. }
  1378. static int dwc3_gadget_pullup(struct usb_gadget *g, int is_on)
  1379. {
  1380. struct dwc3 *dwc = gadget_to_dwc(g);
  1381. unsigned long flags;
  1382. int ret;
  1383. is_on = !!is_on;
  1384. /*
  1385. * Per databook, when we want to stop the gadget, if a control transfer
  1386. * is still in process, complete it and get the core into setup phase.
  1387. */
  1388. if (!is_on && dwc->ep0state != EP0_SETUP_PHASE) {
  1389. reinit_completion(&dwc->ep0_in_setup);
  1390. ret = wait_for_completion_timeout(&dwc->ep0_in_setup,
  1391. msecs_to_jiffies(DWC3_PULL_UP_TIMEOUT));
  1392. if (ret == 0) {
  1393. dev_err(dwc->dev, "timed out waiting for SETUP phase\n");
  1394. return -ETIMEDOUT;
  1395. }
  1396. }
  1397. spin_lock_irqsave(&dwc->lock, flags);
  1398. ret = dwc3_gadget_run_stop(dwc, is_on, false);
  1399. spin_unlock_irqrestore(&dwc->lock, flags);
  1400. return ret;
  1401. }
  1402. static void dwc3_gadget_enable_irq(struct dwc3 *dwc)
  1403. {
  1404. u32 reg;
  1405. /* Enable all but Start and End of Frame IRQs */
  1406. reg = (DWC3_DEVTEN_VNDRDEVTSTRCVEDEN |
  1407. DWC3_DEVTEN_EVNTOVERFLOWEN |
  1408. DWC3_DEVTEN_CMDCMPLTEN |
  1409. DWC3_DEVTEN_ERRTICERREN |
  1410. DWC3_DEVTEN_WKUPEVTEN |
  1411. DWC3_DEVTEN_CONNECTDONEEN |
  1412. DWC3_DEVTEN_USBRSTEN |
  1413. DWC3_DEVTEN_DISCONNEVTEN);
  1414. if (dwc->revision < DWC3_REVISION_250A)
  1415. reg |= DWC3_DEVTEN_ULSTCNGEN;
  1416. dwc3_writel(dwc->regs, DWC3_DEVTEN, reg);
  1417. }
  1418. static void dwc3_gadget_disable_irq(struct dwc3 *dwc)
  1419. {
  1420. /* mask all interrupts */
  1421. dwc3_writel(dwc->regs, DWC3_DEVTEN, 0x00);
  1422. }
  1423. static irqreturn_t dwc3_interrupt(int irq, void *_dwc);
  1424. static irqreturn_t dwc3_thread_interrupt(int irq, void *_dwc);
  1425. /**
  1426. * dwc3_gadget_setup_nump - Calculate and initialize NUMP field of DCFG
  1427. * dwc: pointer to our context structure
  1428. *
  1429. * The following looks like complex but it's actually very simple. In order to
  1430. * calculate the number of packets we can burst at once on OUT transfers, we're
  1431. * gonna use RxFIFO size.
  1432. *
  1433. * To calculate RxFIFO size we need two numbers:
  1434. * MDWIDTH = size, in bits, of the internal memory bus
  1435. * RAM2_DEPTH = depth, in MDWIDTH, of internal RAM2 (where RxFIFO sits)
  1436. *
  1437. * Given these two numbers, the formula is simple:
  1438. *
  1439. * RxFIFO Size = (RAM2_DEPTH * MDWIDTH / 8) - 24 - 16;
  1440. *
  1441. * 24 bytes is for 3x SETUP packets
  1442. * 16 bytes is a clock domain crossing tolerance
  1443. *
  1444. * Given RxFIFO Size, NUMP = RxFIFOSize / 1024;
  1445. */
  1446. static void dwc3_gadget_setup_nump(struct dwc3 *dwc)
  1447. {
  1448. u32 ram2_depth;
  1449. u32 mdwidth;
  1450. u32 nump;
  1451. u32 reg;
  1452. ram2_depth = DWC3_GHWPARAMS7_RAM2_DEPTH(dwc->hwparams.hwparams7);
  1453. mdwidth = DWC3_GHWPARAMS0_MDWIDTH(dwc->hwparams.hwparams0);
  1454. nump = ((ram2_depth * mdwidth / 8) - 24 - 16) / 1024;
  1455. nump = min_t(u32, nump, 16);
  1456. /* update NumP */
  1457. reg = dwc3_readl(dwc->regs, DWC3_DCFG);
  1458. reg &= ~DWC3_DCFG_NUMP_MASK;
  1459. reg |= nump << DWC3_DCFG_NUMP_SHIFT;
  1460. dwc3_writel(dwc->regs, DWC3_DCFG, reg);
  1461. }
  1462. static int __dwc3_gadget_start(struct dwc3 *dwc)
  1463. {
  1464. struct dwc3_ep *dep;
  1465. int ret = 0;
  1466. u32 reg;
  1467. /*
  1468. * Use IMOD if enabled via dwc->imod_interval. Otherwise, if
  1469. * the core supports IMOD, disable it.
  1470. */
  1471. if (dwc->imod_interval) {
  1472. dwc3_writel(dwc->regs, DWC3_DEV_IMOD(0), dwc->imod_interval);
  1473. dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(0), DWC3_GEVNTCOUNT_EHB);
  1474. } else if (dwc3_has_imod(dwc)) {
  1475. dwc3_writel(dwc->regs, DWC3_DEV_IMOD(0), 0);
  1476. }
  1477. reg = dwc3_readl(dwc->regs, DWC3_DCFG);
  1478. reg &= ~(DWC3_DCFG_SPEED_MASK);
  1479. /**
  1480. * WORKAROUND: DWC3 revision < 2.20a have an issue
  1481. * which would cause metastability state on Run/Stop
  1482. * bit if we try to force the IP to USB2-only mode.
  1483. *
  1484. * Because of that, we cannot configure the IP to any
  1485. * speed other than the SuperSpeed
  1486. *
  1487. * Refers to:
  1488. *
  1489. * STAR#9000525659: Clock Domain Crossing on DCTL in
  1490. * USB 2.0 Mode
  1491. */
  1492. if (dwc->revision < DWC3_REVISION_220A) {
  1493. reg |= DWC3_DCFG_SUPERSPEED;
  1494. } else {
  1495. switch (dwc->maximum_speed) {
  1496. case USB_SPEED_LOW:
  1497. reg |= DWC3_DCFG_LOWSPEED;
  1498. break;
  1499. case USB_SPEED_FULL:
  1500. reg |= DWC3_DCFG_FULLSPEED;
  1501. break;
  1502. case USB_SPEED_HIGH:
  1503. reg |= DWC3_DCFG_HIGHSPEED;
  1504. break;
  1505. case USB_SPEED_SUPER_PLUS:
  1506. reg |= DWC3_DCFG_SUPERSPEED_PLUS;
  1507. break;
  1508. default:
  1509. dev_err(dwc->dev, "invalid dwc->maximum_speed (%d)\n",
  1510. dwc->maximum_speed);
  1511. /* fall through */
  1512. case USB_SPEED_SUPER:
  1513. reg |= DWC3_DCFG_SUPERSPEED;
  1514. break;
  1515. }
  1516. }
  1517. dwc3_writel(dwc->regs, DWC3_DCFG, reg);
  1518. /*
  1519. * We are telling dwc3 that we want to use DCFG.NUMP as ACK TP's NUMP
  1520. * field instead of letting dwc3 itself calculate that automatically.
  1521. *
  1522. * This way, we maximize the chances that we'll be able to get several
  1523. * bursts of data without going through any sort of endpoint throttling.
  1524. */
  1525. reg = dwc3_readl(dwc->regs, DWC3_GRXTHRCFG);
  1526. reg &= ~DWC3_GRXTHRCFG_PKTCNTSEL;
  1527. dwc3_writel(dwc->regs, DWC3_GRXTHRCFG, reg);
  1528. dwc3_gadget_setup_nump(dwc);
  1529. /* Start with SuperSpeed Default */
  1530. dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512);
  1531. dep = dwc->eps[0];
  1532. ret = __dwc3_gadget_ep_enable(dep, false, false);
  1533. if (ret) {
  1534. dev_err(dwc->dev, "failed to enable %s\n", dep->name);
  1535. goto err0;
  1536. }
  1537. dep = dwc->eps[1];
  1538. ret = __dwc3_gadget_ep_enable(dep, false, false);
  1539. if (ret) {
  1540. dev_err(dwc->dev, "failed to enable %s\n", dep->name);
  1541. goto err1;
  1542. }
  1543. /* begin to receive SETUP packets */
  1544. dwc->ep0state = EP0_SETUP_PHASE;
  1545. dwc3_ep0_out_start(dwc);
  1546. dwc3_gadget_enable_irq(dwc);
  1547. return 0;
  1548. err1:
  1549. __dwc3_gadget_ep_disable(dwc->eps[0]);
  1550. err0:
  1551. return ret;
  1552. }
  1553. static int dwc3_gadget_start(struct usb_gadget *g,
  1554. struct usb_gadget_driver *driver)
  1555. {
  1556. struct dwc3 *dwc = gadget_to_dwc(g);
  1557. unsigned long flags;
  1558. int ret = 0;
  1559. int irq;
  1560. irq = dwc->irq_gadget;
  1561. ret = request_threaded_irq(irq, dwc3_interrupt, dwc3_thread_interrupt,
  1562. IRQF_SHARED, "dwc3", dwc->ev_buf);
  1563. if (ret) {
  1564. dev_err(dwc->dev, "failed to request irq #%d --> %d\n",
  1565. irq, ret);
  1566. goto err0;
  1567. }
  1568. spin_lock_irqsave(&dwc->lock, flags);
  1569. if (dwc->gadget_driver) {
  1570. dev_err(dwc->dev, "%s is already bound to %s\n",
  1571. dwc->gadget.name,
  1572. dwc->gadget_driver->driver.name);
  1573. ret = -EBUSY;
  1574. goto err1;
  1575. }
  1576. dwc->gadget_driver = driver;
  1577. if (pm_runtime_active(dwc->dev))
  1578. __dwc3_gadget_start(dwc);
  1579. spin_unlock_irqrestore(&dwc->lock, flags);
  1580. return 0;
  1581. err1:
  1582. spin_unlock_irqrestore(&dwc->lock, flags);
  1583. free_irq(irq, dwc);
  1584. err0:
  1585. return ret;
  1586. }
  1587. static void __dwc3_gadget_stop(struct dwc3 *dwc)
  1588. {
  1589. dwc3_gadget_disable_irq(dwc);
  1590. __dwc3_gadget_ep_disable(dwc->eps[0]);
  1591. __dwc3_gadget_ep_disable(dwc->eps[1]);
  1592. }
  1593. static int dwc3_gadget_stop(struct usb_gadget *g)
  1594. {
  1595. struct dwc3 *dwc = gadget_to_dwc(g);
  1596. unsigned long flags;
  1597. int epnum;
  1598. spin_lock_irqsave(&dwc->lock, flags);
  1599. if (pm_runtime_suspended(dwc->dev))
  1600. goto out;
  1601. __dwc3_gadget_stop(dwc);
  1602. for (epnum = 2; epnum < DWC3_ENDPOINTS_NUM; epnum++) {
  1603. struct dwc3_ep *dep = dwc->eps[epnum];
  1604. if (!dep)
  1605. continue;
  1606. if (!(dep->flags & DWC3_EP_END_TRANSFER_PENDING))
  1607. continue;
  1608. wait_event_lock_irq(dep->wait_end_transfer,
  1609. !(dep->flags & DWC3_EP_END_TRANSFER_PENDING),
  1610. dwc->lock);
  1611. }
  1612. out:
  1613. dwc->gadget_driver = NULL;
  1614. spin_unlock_irqrestore(&dwc->lock, flags);
  1615. free_irq(dwc->irq_gadget, dwc->ev_buf);
  1616. return 0;
  1617. }
  1618. static const struct usb_gadget_ops dwc3_gadget_ops = {
  1619. .get_frame = dwc3_gadget_get_frame,
  1620. .wakeup = dwc3_gadget_wakeup,
  1621. .set_selfpowered = dwc3_gadget_set_selfpowered,
  1622. .pullup = dwc3_gadget_pullup,
  1623. .udc_start = dwc3_gadget_start,
  1624. .udc_stop = dwc3_gadget_stop,
  1625. };
  1626. /* -------------------------------------------------------------------------- */
  1627. static int dwc3_gadget_init_hw_endpoints(struct dwc3 *dwc,
  1628. u8 num, u32 direction)
  1629. {
  1630. struct dwc3_ep *dep;
  1631. u8 i;
  1632. for (i = 0; i < num; i++) {
  1633. u8 epnum = (i << 1) | (direction ? 1 : 0);
  1634. dep = kzalloc(sizeof(*dep), GFP_KERNEL);
  1635. if (!dep)
  1636. return -ENOMEM;
  1637. dep->dwc = dwc;
  1638. dep->number = epnum;
  1639. dep->direction = !!direction;
  1640. dep->regs = dwc->regs + DWC3_DEP_BASE(epnum);
  1641. dwc->eps[epnum] = dep;
  1642. snprintf(dep->name, sizeof(dep->name), "ep%d%s", epnum >> 1,
  1643. (epnum & 1) ? "in" : "out");
  1644. dep->endpoint.name = dep->name;
  1645. if (!(dep->number > 1)) {
  1646. dep->endpoint.desc = &dwc3_gadget_ep0_desc;
  1647. dep->endpoint.comp_desc = NULL;
  1648. }
  1649. spin_lock_init(&dep->lock);
  1650. if (epnum == 0 || epnum == 1) {
  1651. usb_ep_set_maxpacket_limit(&dep->endpoint, 512);
  1652. dep->endpoint.maxburst = 1;
  1653. dep->endpoint.ops = &dwc3_gadget_ep0_ops;
  1654. if (!epnum)
  1655. dwc->gadget.ep0 = &dep->endpoint;
  1656. } else if (direction) {
  1657. int mdwidth;
  1658. int size;
  1659. int ret;
  1660. int num;
  1661. mdwidth = DWC3_MDWIDTH(dwc->hwparams.hwparams0);
  1662. /* MDWIDTH is represented in bits, we need it in bytes */
  1663. mdwidth /= 8;
  1664. size = dwc3_readl(dwc->regs, DWC3_GTXFIFOSIZ(i));
  1665. size = DWC3_GTXFIFOSIZ_TXFDEF(size);
  1666. /* FIFO Depth is in MDWDITH bytes. Multiply */
  1667. size *= mdwidth;
  1668. num = size / 1024;
  1669. if (num == 0)
  1670. num = 1;
  1671. /*
  1672. * FIFO sizes account an extra MDWIDTH * (num + 1) bytes for
  1673. * internal overhead. We don't really know how these are used,
  1674. * but documentation say it exists.
  1675. */
  1676. size -= mdwidth * (num + 1);
  1677. size /= num;
  1678. usb_ep_set_maxpacket_limit(&dep->endpoint, size);
  1679. dep->endpoint.max_streams = 15;
  1680. dep->endpoint.ops = &dwc3_gadget_ep_ops;
  1681. list_add_tail(&dep->endpoint.ep_list,
  1682. &dwc->gadget.ep_list);
  1683. ret = dwc3_alloc_trb_pool(dep);
  1684. if (ret)
  1685. return ret;
  1686. } else {
  1687. int ret;
  1688. usb_ep_set_maxpacket_limit(&dep->endpoint, 1024);
  1689. dep->endpoint.max_streams = 15;
  1690. dep->endpoint.ops = &dwc3_gadget_ep_ops;
  1691. list_add_tail(&dep->endpoint.ep_list,
  1692. &dwc->gadget.ep_list);
  1693. ret = dwc3_alloc_trb_pool(dep);
  1694. if (ret)
  1695. return ret;
  1696. }
  1697. if (epnum == 0 || epnum == 1) {
  1698. dep->endpoint.caps.type_control = true;
  1699. } else {
  1700. dep->endpoint.caps.type_iso = true;
  1701. dep->endpoint.caps.type_bulk = true;
  1702. dep->endpoint.caps.type_int = true;
  1703. }
  1704. dep->endpoint.caps.dir_in = !!direction;
  1705. dep->endpoint.caps.dir_out = !direction;
  1706. INIT_LIST_HEAD(&dep->pending_list);
  1707. INIT_LIST_HEAD(&dep->started_list);
  1708. }
  1709. return 0;
  1710. }
  1711. static int dwc3_gadget_init_endpoints(struct dwc3 *dwc)
  1712. {
  1713. int ret;
  1714. INIT_LIST_HEAD(&dwc->gadget.ep_list);
  1715. ret = dwc3_gadget_init_hw_endpoints(dwc, dwc->num_out_eps, 0);
  1716. if (ret < 0) {
  1717. dev_err(dwc->dev, "failed to initialize OUT endpoints\n");
  1718. return ret;
  1719. }
  1720. ret = dwc3_gadget_init_hw_endpoints(dwc, dwc->num_in_eps, 1);
  1721. if (ret < 0) {
  1722. dev_err(dwc->dev, "failed to initialize IN endpoints\n");
  1723. return ret;
  1724. }
  1725. return 0;
  1726. }
  1727. static void dwc3_gadget_free_endpoints(struct dwc3 *dwc)
  1728. {
  1729. struct dwc3_ep *dep;
  1730. u8 epnum;
  1731. for (epnum = 0; epnum < DWC3_ENDPOINTS_NUM; epnum++) {
  1732. dep = dwc->eps[epnum];
  1733. if (!dep)
  1734. continue;
  1735. /*
  1736. * Physical endpoints 0 and 1 are special; they form the
  1737. * bi-directional USB endpoint 0.
  1738. *
  1739. * For those two physical endpoints, we don't allocate a TRB
  1740. * pool nor do we add them the endpoints list. Due to that, we
  1741. * shouldn't do these two operations otherwise we would end up
  1742. * with all sorts of bugs when removing dwc3.ko.
  1743. */
  1744. if (epnum != 0 && epnum != 1) {
  1745. dwc3_free_trb_pool(dep);
  1746. list_del(&dep->endpoint.ep_list);
  1747. }
  1748. kfree(dep);
  1749. }
  1750. }
  1751. /* -------------------------------------------------------------------------- */
  1752. static int __dwc3_cleanup_done_trbs(struct dwc3 *dwc, struct dwc3_ep *dep,
  1753. struct dwc3_request *req, struct dwc3_trb *trb,
  1754. const struct dwc3_event_depevt *event, int status,
  1755. int chain)
  1756. {
  1757. unsigned int count;
  1758. unsigned int s_pkt = 0;
  1759. unsigned int trb_status;
  1760. dwc3_ep_inc_deq(dep);
  1761. if (req->trb == trb)
  1762. dep->queued_requests--;
  1763. trace_dwc3_complete_trb(dep, trb);
  1764. /*
  1765. * If we're in the middle of series of chained TRBs and we
  1766. * receive a short transfer along the way, DWC3 will skip
  1767. * through all TRBs including the last TRB in the chain (the
  1768. * where CHN bit is zero. DWC3 will also avoid clearing HWO
  1769. * bit and SW has to do it manually.
  1770. *
  1771. * We're going to do that here to avoid problems of HW trying
  1772. * to use bogus TRBs for transfers.
  1773. */
  1774. if (chain && (trb->ctrl & DWC3_TRB_CTRL_HWO))
  1775. trb->ctrl &= ~DWC3_TRB_CTRL_HWO;
  1776. /*
  1777. * If we're dealing with unaligned size OUT transfer, we will be left
  1778. * with one TRB pending in the ring. We need to manually clear HWO bit
  1779. * from that TRB.
  1780. */
  1781. if (req->unaligned && (trb->ctrl & DWC3_TRB_CTRL_HWO)) {
  1782. trb->ctrl &= ~DWC3_TRB_CTRL_HWO;
  1783. return 1;
  1784. }
  1785. count = trb->size & DWC3_TRB_SIZE_MASK;
  1786. req->remaining += count;
  1787. if ((trb->ctrl & DWC3_TRB_CTRL_HWO) && status != -ESHUTDOWN)
  1788. return 1;
  1789. if (dep->direction) {
  1790. if (count) {
  1791. trb_status = DWC3_TRB_SIZE_TRBSTS(trb->size);
  1792. if (trb_status == DWC3_TRBSTS_MISSED_ISOC) {
  1793. /*
  1794. * If missed isoc occurred and there is
  1795. * no request queued then issue END
  1796. * TRANSFER, so that core generates
  1797. * next xfernotready and we will issue
  1798. * a fresh START TRANSFER.
  1799. * If there are still queued request
  1800. * then wait, do not issue either END
  1801. * or UPDATE TRANSFER, just attach next
  1802. * request in pending_list during
  1803. * giveback.If any future queued request
  1804. * is successfully transferred then we
  1805. * will issue UPDATE TRANSFER for all
  1806. * request in the pending_list.
  1807. */
  1808. dep->flags |= DWC3_EP_MISSED_ISOC;
  1809. } else {
  1810. dev_err(dwc->dev, "incomplete IN transfer %s\n",
  1811. dep->name);
  1812. status = -ECONNRESET;
  1813. }
  1814. } else {
  1815. dep->flags &= ~DWC3_EP_MISSED_ISOC;
  1816. }
  1817. } else {
  1818. if (count && (event->status & DEPEVT_STATUS_SHORT))
  1819. s_pkt = 1;
  1820. }
  1821. if (s_pkt && !chain)
  1822. return 1;
  1823. if ((event->status & DEPEVT_STATUS_IOC) &&
  1824. (trb->ctrl & DWC3_TRB_CTRL_IOC))
  1825. return 1;
  1826. return 0;
  1827. }
  1828. static int dwc3_cleanup_done_reqs(struct dwc3 *dwc, struct dwc3_ep *dep,
  1829. const struct dwc3_event_depevt *event, int status)
  1830. {
  1831. struct dwc3_request *req, *n;
  1832. struct dwc3_trb *trb;
  1833. bool ioc = false;
  1834. int ret = 0;
  1835. list_for_each_entry_safe(req, n, &dep->started_list, list) {
  1836. unsigned length;
  1837. int chain;
  1838. length = req->request.length;
  1839. chain = req->num_pending_sgs > 0;
  1840. if (chain) {
  1841. struct scatterlist *sg = req->sg;
  1842. struct scatterlist *s;
  1843. unsigned int pending = req->num_pending_sgs;
  1844. unsigned int i;
  1845. for_each_sg(sg, s, pending, i) {
  1846. trb = &dep->trb_pool[dep->trb_dequeue];
  1847. if (trb->ctrl & DWC3_TRB_CTRL_HWO)
  1848. break;
  1849. req->sg = sg_next(s);
  1850. req->num_pending_sgs--;
  1851. ret = __dwc3_cleanup_done_trbs(dwc, dep, req, trb,
  1852. event, status, chain);
  1853. if (ret)
  1854. break;
  1855. }
  1856. } else {
  1857. trb = &dep->trb_pool[dep->trb_dequeue];
  1858. ret = __dwc3_cleanup_done_trbs(dwc, dep, req, trb,
  1859. event, status, chain);
  1860. }
  1861. if (req->unaligned) {
  1862. trb = &dep->trb_pool[dep->trb_dequeue];
  1863. ret = __dwc3_cleanup_done_trbs(dwc, dep, req, trb,
  1864. event, status, false);
  1865. req->unaligned = false;
  1866. }
  1867. req->request.actual = length - req->remaining;
  1868. if ((req->request.actual < length) && req->num_pending_sgs)
  1869. return __dwc3_gadget_kick_transfer(dep, 0);
  1870. dwc3_gadget_giveback(dep, req, status);
  1871. if (ret) {
  1872. if ((event->status & DEPEVT_STATUS_IOC) &&
  1873. (trb->ctrl & DWC3_TRB_CTRL_IOC))
  1874. ioc = true;
  1875. break;
  1876. }
  1877. }
  1878. /*
  1879. * Our endpoint might get disabled by another thread during
  1880. * dwc3_gadget_giveback(). If that happens, we're just gonna return 1
  1881. * early on so DWC3_EP_BUSY flag gets cleared
  1882. */
  1883. if (!dep->endpoint.desc)
  1884. return 1;
  1885. if (usb_endpoint_xfer_isoc(dep->endpoint.desc) &&
  1886. list_empty(&dep->started_list)) {
  1887. if (list_empty(&dep->pending_list)) {
  1888. /*
  1889. * If there is no entry in request list then do
  1890. * not issue END TRANSFER now. Just set PENDING
  1891. * flag, so that END TRANSFER is issued when an
  1892. * entry is added into request list.
  1893. */
  1894. dep->flags = DWC3_EP_PENDING_REQUEST;
  1895. } else {
  1896. dwc3_stop_active_transfer(dwc, dep->number, true);
  1897. dep->flags = DWC3_EP_ENABLED;
  1898. }
  1899. return 1;
  1900. }
  1901. if (usb_endpoint_xfer_isoc(dep->endpoint.desc) && ioc)
  1902. return 0;
  1903. return 1;
  1904. }
  1905. static void dwc3_endpoint_transfer_complete(struct dwc3 *dwc,
  1906. struct dwc3_ep *dep, const struct dwc3_event_depevt *event)
  1907. {
  1908. unsigned status = 0;
  1909. int clean_busy;
  1910. u32 is_xfer_complete;
  1911. is_xfer_complete = (event->endpoint_event == DWC3_DEPEVT_XFERCOMPLETE);
  1912. if (event->status & DEPEVT_STATUS_BUSERR)
  1913. status = -ECONNRESET;
  1914. clean_busy = dwc3_cleanup_done_reqs(dwc, dep, event, status);
  1915. if (clean_busy && (!dep->endpoint.desc || is_xfer_complete ||
  1916. usb_endpoint_xfer_isoc(dep->endpoint.desc)))
  1917. dep->flags &= ~DWC3_EP_BUSY;
  1918. /*
  1919. * WORKAROUND: This is the 2nd half of U1/U2 -> U0 workaround.
  1920. * See dwc3_gadget_linksts_change_interrupt() for 1st half.
  1921. */
  1922. if (dwc->revision < DWC3_REVISION_183A) {
  1923. u32 reg;
  1924. int i;
  1925. for (i = 0; i < DWC3_ENDPOINTS_NUM; i++) {
  1926. dep = dwc->eps[i];
  1927. if (!(dep->flags & DWC3_EP_ENABLED))
  1928. continue;
  1929. if (!list_empty(&dep->started_list))
  1930. return;
  1931. }
  1932. reg = dwc3_readl(dwc->regs, DWC3_DCTL);
  1933. reg |= dwc->u1u2;
  1934. dwc3_writel(dwc->regs, DWC3_DCTL, reg);
  1935. dwc->u1u2 = 0;
  1936. }
  1937. /*
  1938. * Our endpoint might get disabled by another thread during
  1939. * dwc3_gadget_giveback(). If that happens, we're just gonna return 1
  1940. * early on so DWC3_EP_BUSY flag gets cleared
  1941. */
  1942. if (!dep->endpoint.desc)
  1943. return;
  1944. if (!usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
  1945. int ret;
  1946. ret = __dwc3_gadget_kick_transfer(dep, 0);
  1947. if (!ret || ret == -EBUSY)
  1948. return;
  1949. }
  1950. }
  1951. static void dwc3_endpoint_interrupt(struct dwc3 *dwc,
  1952. const struct dwc3_event_depevt *event)
  1953. {
  1954. struct dwc3_ep *dep;
  1955. u8 epnum = event->endpoint_number;
  1956. u8 cmd;
  1957. dep = dwc->eps[epnum];
  1958. if (!(dep->flags & DWC3_EP_ENABLED)) {
  1959. if (!(dep->flags & DWC3_EP_END_TRANSFER_PENDING))
  1960. return;
  1961. /* Handle only EPCMDCMPLT when EP disabled */
  1962. if (event->endpoint_event != DWC3_DEPEVT_EPCMDCMPLT)
  1963. return;
  1964. }
  1965. if (epnum == 0 || epnum == 1) {
  1966. dwc3_ep0_interrupt(dwc, event);
  1967. return;
  1968. }
  1969. switch (event->endpoint_event) {
  1970. case DWC3_DEPEVT_XFERCOMPLETE:
  1971. dep->resource_index = 0;
  1972. if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
  1973. dev_err(dwc->dev, "XferComplete for Isochronous endpoint\n");
  1974. return;
  1975. }
  1976. dwc3_endpoint_transfer_complete(dwc, dep, event);
  1977. break;
  1978. case DWC3_DEPEVT_XFERINPROGRESS:
  1979. dwc3_endpoint_transfer_complete(dwc, dep, event);
  1980. break;
  1981. case DWC3_DEPEVT_XFERNOTREADY:
  1982. if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
  1983. dwc3_gadget_start_isoc(dwc, dep, event);
  1984. } else {
  1985. int ret;
  1986. ret = __dwc3_gadget_kick_transfer(dep, 0);
  1987. if (!ret || ret == -EBUSY)
  1988. return;
  1989. }
  1990. break;
  1991. case DWC3_DEPEVT_STREAMEVT:
  1992. if (!usb_endpoint_xfer_bulk(dep->endpoint.desc)) {
  1993. dev_err(dwc->dev, "Stream event for non-Bulk %s\n",
  1994. dep->name);
  1995. return;
  1996. }
  1997. break;
  1998. case DWC3_DEPEVT_EPCMDCMPLT:
  1999. cmd = DEPEVT_PARAMETER_CMD(event->parameters);
  2000. if (cmd == DWC3_DEPCMD_ENDTRANSFER) {
  2001. dep->flags &= ~DWC3_EP_END_TRANSFER_PENDING;
  2002. wake_up(&dep->wait_end_transfer);
  2003. }
  2004. break;
  2005. case DWC3_DEPEVT_RXTXFIFOEVT:
  2006. break;
  2007. }
  2008. }
  2009. static void dwc3_disconnect_gadget(struct dwc3 *dwc)
  2010. {
  2011. if (dwc->gadget_driver && dwc->gadget_driver->disconnect) {
  2012. spin_unlock(&dwc->lock);
  2013. dwc->gadget_driver->disconnect(&dwc->gadget);
  2014. spin_lock(&dwc->lock);
  2015. }
  2016. }
  2017. static void dwc3_suspend_gadget(struct dwc3 *dwc)
  2018. {
  2019. if (dwc->gadget_driver && dwc->gadget_driver->suspend) {
  2020. spin_unlock(&dwc->lock);
  2021. dwc->gadget_driver->suspend(&dwc->gadget);
  2022. spin_lock(&dwc->lock);
  2023. }
  2024. }
  2025. static void dwc3_resume_gadget(struct dwc3 *dwc)
  2026. {
  2027. if (dwc->gadget_driver && dwc->gadget_driver->resume) {
  2028. spin_unlock(&dwc->lock);
  2029. dwc->gadget_driver->resume(&dwc->gadget);
  2030. spin_lock(&dwc->lock);
  2031. }
  2032. }
  2033. static void dwc3_reset_gadget(struct dwc3 *dwc)
  2034. {
  2035. if (!dwc->gadget_driver)
  2036. return;
  2037. if (dwc->gadget.speed != USB_SPEED_UNKNOWN) {
  2038. spin_unlock(&dwc->lock);
  2039. usb_gadget_udc_reset(&dwc->gadget, dwc->gadget_driver);
  2040. spin_lock(&dwc->lock);
  2041. }
  2042. }
  2043. static void dwc3_stop_active_transfer(struct dwc3 *dwc, u32 epnum, bool force)
  2044. {
  2045. struct dwc3_ep *dep;
  2046. struct dwc3_gadget_ep_cmd_params params;
  2047. u32 cmd;
  2048. int ret;
  2049. dep = dwc->eps[epnum];
  2050. if ((dep->flags & DWC3_EP_END_TRANSFER_PENDING) ||
  2051. !dep->resource_index)
  2052. return;
  2053. /*
  2054. * NOTICE: We are violating what the Databook says about the
  2055. * EndTransfer command. Ideally we would _always_ wait for the
  2056. * EndTransfer Command Completion IRQ, but that's causing too
  2057. * much trouble synchronizing between us and gadget driver.
  2058. *
  2059. * We have discussed this with the IP Provider and it was
  2060. * suggested to giveback all requests here, but give HW some
  2061. * extra time to synchronize with the interconnect. We're using
  2062. * an arbitrary 100us delay for that.
  2063. *
  2064. * Note also that a similar handling was tested by Synopsys
  2065. * (thanks a lot Paul) and nothing bad has come out of it.
  2066. * In short, what we're doing is:
  2067. *
  2068. * - Issue EndTransfer WITH CMDIOC bit set
  2069. * - Wait 100us
  2070. *
  2071. * As of IP version 3.10a of the DWC_usb3 IP, the controller
  2072. * supports a mode to work around the above limitation. The
  2073. * software can poll the CMDACT bit in the DEPCMD register
  2074. * after issuing a EndTransfer command. This mode is enabled
  2075. * by writing GUCTL2[14]. This polling is already done in the
  2076. * dwc3_send_gadget_ep_cmd() function so if the mode is
  2077. * enabled, the EndTransfer command will have completed upon
  2078. * returning from this function and we don't need to delay for
  2079. * 100us.
  2080. *
  2081. * This mode is NOT available on the DWC_usb31 IP.
  2082. */
  2083. cmd = DWC3_DEPCMD_ENDTRANSFER;
  2084. cmd |= force ? DWC3_DEPCMD_HIPRI_FORCERM : 0;
  2085. cmd |= DWC3_DEPCMD_CMDIOC;
  2086. cmd |= DWC3_DEPCMD_PARAM(dep->resource_index);
  2087. memset(&params, 0, sizeof(params));
  2088. ret = dwc3_send_gadget_ep_cmd(dep, cmd, &params);
  2089. WARN_ON_ONCE(ret);
  2090. dep->resource_index = 0;
  2091. dep->flags &= ~DWC3_EP_BUSY;
  2092. if (dwc3_is_usb31(dwc) || dwc->revision < DWC3_REVISION_310A) {
  2093. dep->flags |= DWC3_EP_END_TRANSFER_PENDING;
  2094. udelay(100);
  2095. }
  2096. }
  2097. static void dwc3_clear_stall_all_ep(struct dwc3 *dwc)
  2098. {
  2099. u32 epnum;
  2100. for (epnum = 1; epnum < DWC3_ENDPOINTS_NUM; epnum++) {
  2101. struct dwc3_ep *dep;
  2102. int ret;
  2103. dep = dwc->eps[epnum];
  2104. if (!dep)
  2105. continue;
  2106. if (!(dep->flags & DWC3_EP_STALL))
  2107. continue;
  2108. dep->flags &= ~DWC3_EP_STALL;
  2109. ret = dwc3_send_clear_stall_ep_cmd(dep);
  2110. WARN_ON_ONCE(ret);
  2111. }
  2112. }
  2113. static void dwc3_gadget_disconnect_interrupt(struct dwc3 *dwc)
  2114. {
  2115. int reg;
  2116. reg = dwc3_readl(dwc->regs, DWC3_DCTL);
  2117. reg &= ~DWC3_DCTL_INITU1ENA;
  2118. dwc3_writel(dwc->regs, DWC3_DCTL, reg);
  2119. reg &= ~DWC3_DCTL_INITU2ENA;
  2120. dwc3_writel(dwc->regs, DWC3_DCTL, reg);
  2121. dwc3_disconnect_gadget(dwc);
  2122. dwc->gadget.speed = USB_SPEED_UNKNOWN;
  2123. dwc->setup_packet_pending = false;
  2124. usb_gadget_set_state(&dwc->gadget, USB_STATE_NOTATTACHED);
  2125. dwc->connected = false;
  2126. }
  2127. static void dwc3_gadget_reset_interrupt(struct dwc3 *dwc)
  2128. {
  2129. u32 reg;
  2130. dwc->connected = true;
  2131. /*
  2132. * WORKAROUND: DWC3 revisions <1.88a have an issue which
  2133. * would cause a missing Disconnect Event if there's a
  2134. * pending Setup Packet in the FIFO.
  2135. *
  2136. * There's no suggested workaround on the official Bug
  2137. * report, which states that "unless the driver/application
  2138. * is doing any special handling of a disconnect event,
  2139. * there is no functional issue".
  2140. *
  2141. * Unfortunately, it turns out that we _do_ some special
  2142. * handling of a disconnect event, namely complete all
  2143. * pending transfers, notify gadget driver of the
  2144. * disconnection, and so on.
  2145. *
  2146. * Our suggested workaround is to follow the Disconnect
  2147. * Event steps here, instead, based on a setup_packet_pending
  2148. * flag. Such flag gets set whenever we have a SETUP_PENDING
  2149. * status for EP0 TRBs and gets cleared on XferComplete for the
  2150. * same endpoint.
  2151. *
  2152. * Refers to:
  2153. *
  2154. * STAR#9000466709: RTL: Device : Disconnect event not
  2155. * generated if setup packet pending in FIFO
  2156. */
  2157. if (dwc->revision < DWC3_REVISION_188A) {
  2158. if (dwc->setup_packet_pending)
  2159. dwc3_gadget_disconnect_interrupt(dwc);
  2160. }
  2161. dwc3_reset_gadget(dwc);
  2162. reg = dwc3_readl(dwc->regs, DWC3_DCTL);
  2163. reg &= ~DWC3_DCTL_TSTCTRL_MASK;
  2164. dwc3_writel(dwc->regs, DWC3_DCTL, reg);
  2165. dwc->test_mode = false;
  2166. dwc3_clear_stall_all_ep(dwc);
  2167. /* Reset device address to zero */
  2168. reg = dwc3_readl(dwc->regs, DWC3_DCFG);
  2169. reg &= ~(DWC3_DCFG_DEVADDR_MASK);
  2170. dwc3_writel(dwc->regs, DWC3_DCFG, reg);
  2171. }
  2172. static void dwc3_gadget_conndone_interrupt(struct dwc3 *dwc)
  2173. {
  2174. struct dwc3_ep *dep;
  2175. int ret;
  2176. u32 reg;
  2177. u8 speed;
  2178. reg = dwc3_readl(dwc->regs, DWC3_DSTS);
  2179. speed = reg & DWC3_DSTS_CONNECTSPD;
  2180. dwc->speed = speed;
  2181. /*
  2182. * RAMClkSel is reset to 0 after USB reset, so it must be reprogrammed
  2183. * each time on Connect Done.
  2184. *
  2185. * Currently we always use the reset value. If any platform
  2186. * wants to set this to a different value, we need to add a
  2187. * setting and update GCTL.RAMCLKSEL here.
  2188. */
  2189. switch (speed) {
  2190. case DWC3_DSTS_SUPERSPEED_PLUS:
  2191. dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512);
  2192. dwc->gadget.ep0->maxpacket = 512;
  2193. dwc->gadget.speed = USB_SPEED_SUPER_PLUS;
  2194. break;
  2195. case DWC3_DSTS_SUPERSPEED:
  2196. /*
  2197. * WORKAROUND: DWC3 revisions <1.90a have an issue which
  2198. * would cause a missing USB3 Reset event.
  2199. *
  2200. * In such situations, we should force a USB3 Reset
  2201. * event by calling our dwc3_gadget_reset_interrupt()
  2202. * routine.
  2203. *
  2204. * Refers to:
  2205. *
  2206. * STAR#9000483510: RTL: SS : USB3 reset event may
  2207. * not be generated always when the link enters poll
  2208. */
  2209. if (dwc->revision < DWC3_REVISION_190A)
  2210. dwc3_gadget_reset_interrupt(dwc);
  2211. dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512);
  2212. dwc->gadget.ep0->maxpacket = 512;
  2213. dwc->gadget.speed = USB_SPEED_SUPER;
  2214. break;
  2215. case DWC3_DSTS_HIGHSPEED:
  2216. dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(64);
  2217. dwc->gadget.ep0->maxpacket = 64;
  2218. dwc->gadget.speed = USB_SPEED_HIGH;
  2219. break;
  2220. case DWC3_DSTS_FULLSPEED:
  2221. dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(64);
  2222. dwc->gadget.ep0->maxpacket = 64;
  2223. dwc->gadget.speed = USB_SPEED_FULL;
  2224. break;
  2225. case DWC3_DSTS_LOWSPEED:
  2226. dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(8);
  2227. dwc->gadget.ep0->maxpacket = 8;
  2228. dwc->gadget.speed = USB_SPEED_LOW;
  2229. break;
  2230. }
  2231. /* Enable USB2 LPM Capability */
  2232. if ((dwc->revision > DWC3_REVISION_194A) &&
  2233. (speed != DWC3_DSTS_SUPERSPEED) &&
  2234. (speed != DWC3_DSTS_SUPERSPEED_PLUS)) {
  2235. reg = dwc3_readl(dwc->regs, DWC3_DCFG);
  2236. reg |= DWC3_DCFG_LPM_CAP;
  2237. dwc3_writel(dwc->regs, DWC3_DCFG, reg);
  2238. reg = dwc3_readl(dwc->regs, DWC3_DCTL);
  2239. reg &= ~(DWC3_DCTL_HIRD_THRES_MASK | DWC3_DCTL_L1_HIBER_EN);
  2240. reg |= DWC3_DCTL_HIRD_THRES(dwc->hird_threshold);
  2241. /*
  2242. * When dwc3 revisions >= 2.40a, LPM Erratum is enabled and
  2243. * DCFG.LPMCap is set, core responses with an ACK and the
  2244. * BESL value in the LPM token is less than or equal to LPM
  2245. * NYET threshold.
  2246. */
  2247. WARN_ONCE(dwc->revision < DWC3_REVISION_240A
  2248. && dwc->has_lpm_erratum,
  2249. "LPM Erratum not available on dwc3 revisions < 2.40a\n");
  2250. if (dwc->has_lpm_erratum && dwc->revision >= DWC3_REVISION_240A)
  2251. reg |= DWC3_DCTL_LPM_ERRATA(dwc->lpm_nyet_threshold);
  2252. dwc3_writel(dwc->regs, DWC3_DCTL, reg);
  2253. } else {
  2254. reg = dwc3_readl(dwc->regs, DWC3_DCTL);
  2255. reg &= ~DWC3_DCTL_HIRD_THRES_MASK;
  2256. dwc3_writel(dwc->regs, DWC3_DCTL, reg);
  2257. }
  2258. dep = dwc->eps[0];
  2259. ret = __dwc3_gadget_ep_enable(dep, true, false);
  2260. if (ret) {
  2261. dev_err(dwc->dev, "failed to enable %s\n", dep->name);
  2262. return;
  2263. }
  2264. dep = dwc->eps[1];
  2265. ret = __dwc3_gadget_ep_enable(dep, true, false);
  2266. if (ret) {
  2267. dev_err(dwc->dev, "failed to enable %s\n", dep->name);
  2268. return;
  2269. }
  2270. /*
  2271. * Configure PHY via GUSB3PIPECTLn if required.
  2272. *
  2273. * Update GTXFIFOSIZn
  2274. *
  2275. * In both cases reset values should be sufficient.
  2276. */
  2277. }
  2278. static void dwc3_gadget_wakeup_interrupt(struct dwc3 *dwc)
  2279. {
  2280. /*
  2281. * TODO take core out of low power mode when that's
  2282. * implemented.
  2283. */
  2284. if (dwc->gadget_driver && dwc->gadget_driver->resume) {
  2285. spin_unlock(&dwc->lock);
  2286. dwc->gadget_driver->resume(&dwc->gadget);
  2287. spin_lock(&dwc->lock);
  2288. }
  2289. }
  2290. static void dwc3_gadget_linksts_change_interrupt(struct dwc3 *dwc,
  2291. unsigned int evtinfo)
  2292. {
  2293. enum dwc3_link_state next = evtinfo & DWC3_LINK_STATE_MASK;
  2294. unsigned int pwropt;
  2295. /*
  2296. * WORKAROUND: DWC3 < 2.50a have an issue when configured without
  2297. * Hibernation mode enabled which would show up when device detects
  2298. * host-initiated U3 exit.
  2299. *
  2300. * In that case, device will generate a Link State Change Interrupt
  2301. * from U3 to RESUME which is only necessary if Hibernation is
  2302. * configured in.
  2303. *
  2304. * There are no functional changes due to such spurious event and we
  2305. * just need to ignore it.
  2306. *
  2307. * Refers to:
  2308. *
  2309. * STAR#9000570034 RTL: SS Resume event generated in non-Hibernation
  2310. * operational mode
  2311. */
  2312. pwropt = DWC3_GHWPARAMS1_EN_PWROPT(dwc->hwparams.hwparams1);
  2313. if ((dwc->revision < DWC3_REVISION_250A) &&
  2314. (pwropt != DWC3_GHWPARAMS1_EN_PWROPT_HIB)) {
  2315. if ((dwc->link_state == DWC3_LINK_STATE_U3) &&
  2316. (next == DWC3_LINK_STATE_RESUME)) {
  2317. return;
  2318. }
  2319. }
  2320. /*
  2321. * WORKAROUND: DWC3 Revisions <1.83a have an issue which, depending
  2322. * on the link partner, the USB session might do multiple entry/exit
  2323. * of low power states before a transfer takes place.
  2324. *
  2325. * Due to this problem, we might experience lower throughput. The
  2326. * suggested workaround is to disable DCTL[12:9] bits if we're
  2327. * transitioning from U1/U2 to U0 and enable those bits again
  2328. * after a transfer completes and there are no pending transfers
  2329. * on any of the enabled endpoints.
  2330. *
  2331. * This is the first half of that workaround.
  2332. *
  2333. * Refers to:
  2334. *
  2335. * STAR#9000446952: RTL: Device SS : if U1/U2 ->U0 takes >128us
  2336. * core send LGO_Ux entering U0
  2337. */
  2338. if (dwc->revision < DWC3_REVISION_183A) {
  2339. if (next == DWC3_LINK_STATE_U0) {
  2340. u32 u1u2;
  2341. u32 reg;
  2342. switch (dwc->link_state) {
  2343. case DWC3_LINK_STATE_U1:
  2344. case DWC3_LINK_STATE_U2:
  2345. reg = dwc3_readl(dwc->regs, DWC3_DCTL);
  2346. u1u2 = reg & (DWC3_DCTL_INITU2ENA
  2347. | DWC3_DCTL_ACCEPTU2ENA
  2348. | DWC3_DCTL_INITU1ENA
  2349. | DWC3_DCTL_ACCEPTU1ENA);
  2350. if (!dwc->u1u2)
  2351. dwc->u1u2 = reg & u1u2;
  2352. reg &= ~u1u2;
  2353. dwc3_writel(dwc->regs, DWC3_DCTL, reg);
  2354. break;
  2355. default:
  2356. /* do nothing */
  2357. break;
  2358. }
  2359. }
  2360. }
  2361. switch (next) {
  2362. case DWC3_LINK_STATE_U1:
  2363. if (dwc->speed == USB_SPEED_SUPER)
  2364. dwc3_suspend_gadget(dwc);
  2365. break;
  2366. case DWC3_LINK_STATE_U2:
  2367. case DWC3_LINK_STATE_U3:
  2368. dwc3_suspend_gadget(dwc);
  2369. break;
  2370. case DWC3_LINK_STATE_RESUME:
  2371. dwc3_resume_gadget(dwc);
  2372. break;
  2373. default:
  2374. /* do nothing */
  2375. break;
  2376. }
  2377. dwc->link_state = next;
  2378. }
  2379. static void dwc3_gadget_suspend_interrupt(struct dwc3 *dwc,
  2380. unsigned int evtinfo)
  2381. {
  2382. enum dwc3_link_state next = evtinfo & DWC3_LINK_STATE_MASK;
  2383. if (dwc->link_state != next && next == DWC3_LINK_STATE_U3)
  2384. dwc3_suspend_gadget(dwc);
  2385. dwc->link_state = next;
  2386. }
  2387. static void dwc3_gadget_hibernation_interrupt(struct dwc3 *dwc,
  2388. unsigned int evtinfo)
  2389. {
  2390. unsigned int is_ss = evtinfo & BIT(4);
  2391. /**
  2392. * WORKAROUND: DWC3 revison 2.20a with hibernation support
  2393. * have a known issue which can cause USB CV TD.9.23 to fail
  2394. * randomly.
  2395. *
  2396. * Because of this issue, core could generate bogus hibernation
  2397. * events which SW needs to ignore.
  2398. *
  2399. * Refers to:
  2400. *
  2401. * STAR#9000546576: Device Mode Hibernation: Issue in USB 2.0
  2402. * Device Fallback from SuperSpeed
  2403. */
  2404. if (is_ss ^ (dwc->speed == USB_SPEED_SUPER))
  2405. return;
  2406. /* enter hibernation here */
  2407. }
  2408. static void dwc3_gadget_interrupt(struct dwc3 *dwc,
  2409. const struct dwc3_event_devt *event)
  2410. {
  2411. switch (event->type) {
  2412. case DWC3_DEVICE_EVENT_DISCONNECT:
  2413. dwc3_gadget_disconnect_interrupt(dwc);
  2414. break;
  2415. case DWC3_DEVICE_EVENT_RESET:
  2416. dwc3_gadget_reset_interrupt(dwc);
  2417. break;
  2418. case DWC3_DEVICE_EVENT_CONNECT_DONE:
  2419. dwc3_gadget_conndone_interrupt(dwc);
  2420. break;
  2421. case DWC3_DEVICE_EVENT_WAKEUP:
  2422. dwc3_gadget_wakeup_interrupt(dwc);
  2423. break;
  2424. case DWC3_DEVICE_EVENT_HIBER_REQ:
  2425. if (dev_WARN_ONCE(dwc->dev, !dwc->has_hibernation,
  2426. "unexpected hibernation event\n"))
  2427. break;
  2428. dwc3_gadget_hibernation_interrupt(dwc, event->event_info);
  2429. break;
  2430. case DWC3_DEVICE_EVENT_LINK_STATUS_CHANGE:
  2431. dwc3_gadget_linksts_change_interrupt(dwc, event->event_info);
  2432. break;
  2433. case DWC3_DEVICE_EVENT_EOPF:
  2434. /* It changed to be suspend event for version 2.30a and above */
  2435. if (dwc->revision >= DWC3_REVISION_230A) {
  2436. /*
  2437. * Ignore suspend event until the gadget enters into
  2438. * USB_STATE_CONFIGURED state.
  2439. */
  2440. if (dwc->gadget.state >= USB_STATE_CONFIGURED)
  2441. dwc3_gadget_suspend_interrupt(dwc,
  2442. event->event_info);
  2443. }
  2444. break;
  2445. case DWC3_DEVICE_EVENT_SOF:
  2446. case DWC3_DEVICE_EVENT_ERRATIC_ERROR:
  2447. case DWC3_DEVICE_EVENT_CMD_CMPL:
  2448. case DWC3_DEVICE_EVENT_OVERFLOW:
  2449. break;
  2450. default:
  2451. dev_WARN(dwc->dev, "UNKNOWN IRQ %d\n", event->type);
  2452. }
  2453. }
  2454. static void dwc3_process_event_entry(struct dwc3 *dwc,
  2455. const union dwc3_event *event)
  2456. {
  2457. trace_dwc3_event(event->raw, dwc);
  2458. /* Endpoint IRQ, handle it and return early */
  2459. if (event->type.is_devspec == 0) {
  2460. /* depevt */
  2461. return dwc3_endpoint_interrupt(dwc, &event->depevt);
  2462. }
  2463. switch (event->type.type) {
  2464. case DWC3_EVENT_TYPE_DEV:
  2465. dwc3_gadget_interrupt(dwc, &event->devt);
  2466. break;
  2467. /* REVISIT what to do with Carkit and I2C events ? */
  2468. default:
  2469. dev_err(dwc->dev, "UNKNOWN IRQ type %d\n", event->raw);
  2470. }
  2471. }
  2472. static irqreturn_t dwc3_process_event_buf(struct dwc3_event_buffer *evt)
  2473. {
  2474. struct dwc3 *dwc = evt->dwc;
  2475. irqreturn_t ret = IRQ_NONE;
  2476. int left;
  2477. u32 reg;
  2478. left = evt->count;
  2479. if (!(evt->flags & DWC3_EVENT_PENDING))
  2480. return IRQ_NONE;
  2481. while (left > 0) {
  2482. union dwc3_event event;
  2483. event.raw = *(u32 *) (evt->cache + evt->lpos);
  2484. dwc3_process_event_entry(dwc, &event);
  2485. /*
  2486. * FIXME we wrap around correctly to the next entry as
  2487. * almost all entries are 4 bytes in size. There is one
  2488. * entry which has 12 bytes which is a regular entry
  2489. * followed by 8 bytes data. ATM I don't know how
  2490. * things are organized if we get next to the a
  2491. * boundary so I worry about that once we try to handle
  2492. * that.
  2493. */
  2494. evt->lpos = (evt->lpos + 4) % evt->length;
  2495. left -= 4;
  2496. }
  2497. evt->count = 0;
  2498. evt->flags &= ~DWC3_EVENT_PENDING;
  2499. ret = IRQ_HANDLED;
  2500. /* Unmask interrupt */
  2501. reg = dwc3_readl(dwc->regs, DWC3_GEVNTSIZ(0));
  2502. reg &= ~DWC3_GEVNTSIZ_INTMASK;
  2503. dwc3_writel(dwc->regs, DWC3_GEVNTSIZ(0), reg);
  2504. if (dwc->imod_interval) {
  2505. dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(0), DWC3_GEVNTCOUNT_EHB);
  2506. dwc3_writel(dwc->regs, DWC3_DEV_IMOD(0), dwc->imod_interval);
  2507. }
  2508. return ret;
  2509. }
  2510. static irqreturn_t dwc3_thread_interrupt(int irq, void *_evt)
  2511. {
  2512. struct dwc3_event_buffer *evt = _evt;
  2513. struct dwc3 *dwc = evt->dwc;
  2514. unsigned long flags;
  2515. irqreturn_t ret = IRQ_NONE;
  2516. spin_lock_irqsave(&dwc->lock, flags);
  2517. ret = dwc3_process_event_buf(evt);
  2518. spin_unlock_irqrestore(&dwc->lock, flags);
  2519. return ret;
  2520. }
  2521. static irqreturn_t dwc3_check_event_buf(struct dwc3_event_buffer *evt)
  2522. {
  2523. struct dwc3 *dwc = evt->dwc;
  2524. u32 amount;
  2525. u32 count;
  2526. u32 reg;
  2527. if (pm_runtime_suspended(dwc->dev)) {
  2528. pm_runtime_get(dwc->dev);
  2529. disable_irq_nosync(dwc->irq_gadget);
  2530. dwc->pending_events = true;
  2531. return IRQ_HANDLED;
  2532. }
  2533. count = dwc3_readl(dwc->regs, DWC3_GEVNTCOUNT(0));
  2534. count &= DWC3_GEVNTCOUNT_MASK;
  2535. if (!count)
  2536. return IRQ_NONE;
  2537. evt->count = count;
  2538. evt->flags |= DWC3_EVENT_PENDING;
  2539. /* Mask interrupt */
  2540. reg = dwc3_readl(dwc->regs, DWC3_GEVNTSIZ(0));
  2541. reg |= DWC3_GEVNTSIZ_INTMASK;
  2542. dwc3_writel(dwc->regs, DWC3_GEVNTSIZ(0), reg);
  2543. amount = min(count, evt->length - evt->lpos);
  2544. memcpy(evt->cache + evt->lpos, evt->buf + evt->lpos, amount);
  2545. if (amount < count)
  2546. memcpy(evt->cache, evt->buf, count - amount);
  2547. dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(0), count);
  2548. return IRQ_WAKE_THREAD;
  2549. }
  2550. static irqreturn_t dwc3_interrupt(int irq, void *_evt)
  2551. {
  2552. struct dwc3_event_buffer *evt = _evt;
  2553. return dwc3_check_event_buf(evt);
  2554. }
  2555. static int dwc3_gadget_get_irq(struct dwc3 *dwc)
  2556. {
  2557. struct platform_device *dwc3_pdev = to_platform_device(dwc->dev);
  2558. int irq;
  2559. irq = platform_get_irq_byname(dwc3_pdev, "peripheral");
  2560. if (irq > 0)
  2561. goto out;
  2562. if (irq == -EPROBE_DEFER)
  2563. goto out;
  2564. irq = platform_get_irq_byname(dwc3_pdev, "dwc_usb3");
  2565. if (irq > 0)
  2566. goto out;
  2567. if (irq == -EPROBE_DEFER)
  2568. goto out;
  2569. irq = platform_get_irq(dwc3_pdev, 0);
  2570. if (irq > 0)
  2571. goto out;
  2572. if (irq != -EPROBE_DEFER)
  2573. dev_err(dwc->dev, "missing peripheral IRQ\n");
  2574. if (!irq)
  2575. irq = -EINVAL;
  2576. out:
  2577. return irq;
  2578. }
  2579. /**
  2580. * dwc3_gadget_init - Initializes gadget related registers
  2581. * @dwc: pointer to our controller context structure
  2582. *
  2583. * Returns 0 on success otherwise negative errno.
  2584. */
  2585. int dwc3_gadget_init(struct dwc3 *dwc)
  2586. {
  2587. int ret;
  2588. int irq;
  2589. irq = dwc3_gadget_get_irq(dwc);
  2590. if (irq < 0) {
  2591. ret = irq;
  2592. goto err0;
  2593. }
  2594. dwc->irq_gadget = irq;
  2595. dwc->ctrl_req = dma_alloc_coherent(dwc->sysdev, sizeof(*dwc->ctrl_req),
  2596. &dwc->ctrl_req_addr, GFP_KERNEL);
  2597. if (!dwc->ctrl_req) {
  2598. dev_err(dwc->dev, "failed to allocate ctrl request\n");
  2599. ret = -ENOMEM;
  2600. goto err0;
  2601. }
  2602. dwc->ep0_trb = dma_alloc_coherent(dwc->sysdev,
  2603. sizeof(*dwc->ep0_trb) * 2,
  2604. &dwc->ep0_trb_addr, GFP_KERNEL);
  2605. if (!dwc->ep0_trb) {
  2606. dev_err(dwc->dev, "failed to allocate ep0 trb\n");
  2607. ret = -ENOMEM;
  2608. goto err1;
  2609. }
  2610. dwc->setup_buf = kzalloc(DWC3_EP0_BOUNCE_SIZE, GFP_KERNEL);
  2611. if (!dwc->setup_buf) {
  2612. ret = -ENOMEM;
  2613. goto err2;
  2614. }
  2615. dwc->ep0_bounce = dma_alloc_coherent(dwc->sysdev,
  2616. DWC3_EP0_BOUNCE_SIZE, &dwc->ep0_bounce_addr,
  2617. GFP_KERNEL);
  2618. if (!dwc->ep0_bounce) {
  2619. dev_err(dwc->dev, "failed to allocate ep0 bounce buffer\n");
  2620. ret = -ENOMEM;
  2621. goto err3;
  2622. }
  2623. dwc->zlp_buf = kzalloc(DWC3_ZLP_BUF_SIZE, GFP_KERNEL);
  2624. if (!dwc->zlp_buf) {
  2625. ret = -ENOMEM;
  2626. goto err4;
  2627. }
  2628. dwc->bounce = dma_alloc_coherent(dwc->sysdev, DWC3_BOUNCE_SIZE,
  2629. &dwc->bounce_addr, GFP_KERNEL);
  2630. if (!dwc->bounce) {
  2631. ret = -ENOMEM;
  2632. goto err5;
  2633. }
  2634. init_completion(&dwc->ep0_in_setup);
  2635. dwc->gadget.ops = &dwc3_gadget_ops;
  2636. dwc->gadget.speed = USB_SPEED_UNKNOWN;
  2637. dwc->gadget.sg_supported = true;
  2638. dwc->gadget.name = "dwc3-gadget";
  2639. dwc->gadget.is_otg = dwc->dr_mode == USB_DR_MODE_OTG;
  2640. /*
  2641. * FIXME We might be setting max_speed to <SUPER, however versions
  2642. * <2.20a of dwc3 have an issue with metastability (documented
  2643. * elsewhere in this driver) which tells us we can't set max speed to
  2644. * anything lower than SUPER.
  2645. *
  2646. * Because gadget.max_speed is only used by composite.c and function
  2647. * drivers (i.e. it won't go into dwc3's registers) we are allowing this
  2648. * to happen so we avoid sending SuperSpeed Capability descriptor
  2649. * together with our BOS descriptor as that could confuse host into
  2650. * thinking we can handle super speed.
  2651. *
  2652. * Note that, in fact, we won't even support GetBOS requests when speed
  2653. * is less than super speed because we don't have means, yet, to tell
  2654. * composite.c that we are USB 2.0 + LPM ECN.
  2655. */
  2656. if (dwc->revision < DWC3_REVISION_220A)
  2657. dev_info(dwc->dev, "changing max_speed on rev %08x\n",
  2658. dwc->revision);
  2659. dwc->gadget.max_speed = dwc->maximum_speed;
  2660. /*
  2661. * REVISIT: Here we should clear all pending IRQs to be
  2662. * sure we're starting from a well known location.
  2663. */
  2664. ret = dwc3_gadget_init_endpoints(dwc);
  2665. if (ret)
  2666. goto err6;
  2667. ret = usb_add_gadget_udc(dwc->dev, &dwc->gadget);
  2668. if (ret) {
  2669. dev_err(dwc->dev, "failed to register udc\n");
  2670. goto err6;
  2671. }
  2672. return 0;
  2673. err6:
  2674. dma_free_coherent(dwc->sysdev, DWC3_BOUNCE_SIZE, dwc->bounce,
  2675. dwc->bounce_addr);
  2676. err5:
  2677. kfree(dwc->zlp_buf);
  2678. err4:
  2679. dwc3_gadget_free_endpoints(dwc);
  2680. dma_free_coherent(dwc->sysdev, DWC3_EP0_BOUNCE_SIZE,
  2681. dwc->ep0_bounce, dwc->ep0_bounce_addr);
  2682. err3:
  2683. kfree(dwc->setup_buf);
  2684. err2:
  2685. dma_free_coherent(dwc->sysdev, sizeof(*dwc->ep0_trb) * 2,
  2686. dwc->ep0_trb, dwc->ep0_trb_addr);
  2687. err1:
  2688. dma_free_coherent(dwc->sysdev, sizeof(*dwc->ctrl_req),
  2689. dwc->ctrl_req, dwc->ctrl_req_addr);
  2690. err0:
  2691. return ret;
  2692. }
  2693. /* -------------------------------------------------------------------------- */
  2694. void dwc3_gadget_exit(struct dwc3 *dwc)
  2695. {
  2696. usb_del_gadget_udc(&dwc->gadget);
  2697. dwc3_gadget_free_endpoints(dwc);
  2698. dma_free_coherent(dwc->sysdev, DWC3_BOUNCE_SIZE, dwc->bounce,
  2699. dwc->bounce_addr);
  2700. dma_free_coherent(dwc->sysdev, DWC3_EP0_BOUNCE_SIZE,
  2701. dwc->ep0_bounce, dwc->ep0_bounce_addr);
  2702. kfree(dwc->setup_buf);
  2703. kfree(dwc->zlp_buf);
  2704. dma_free_coherent(dwc->sysdev, sizeof(*dwc->ep0_trb) * 2,
  2705. dwc->ep0_trb, dwc->ep0_trb_addr);
  2706. dma_free_coherent(dwc->sysdev, sizeof(*dwc->ctrl_req),
  2707. dwc->ctrl_req, dwc->ctrl_req_addr);
  2708. }
  2709. int dwc3_gadget_suspend(struct dwc3 *dwc)
  2710. {
  2711. if (!dwc->gadget_driver)
  2712. return 0;
  2713. dwc3_gadget_run_stop(dwc, false, false);
  2714. dwc3_disconnect_gadget(dwc);
  2715. __dwc3_gadget_stop(dwc);
  2716. return 0;
  2717. }
  2718. int dwc3_gadget_resume(struct dwc3 *dwc)
  2719. {
  2720. int ret;
  2721. if (!dwc->gadget_driver)
  2722. return 0;
  2723. ret = __dwc3_gadget_start(dwc);
  2724. if (ret < 0)
  2725. goto err0;
  2726. ret = dwc3_gadget_run_stop(dwc, true, false);
  2727. if (ret < 0)
  2728. goto err1;
  2729. return 0;
  2730. err1:
  2731. __dwc3_gadget_stop(dwc);
  2732. err0:
  2733. return ret;
  2734. }
  2735. void dwc3_gadget_process_pending_events(struct dwc3 *dwc)
  2736. {
  2737. if (dwc->pending_events) {
  2738. dwc3_interrupt(dwc->irq_gadget, dwc->ev_buf);
  2739. dwc->pending_events = false;
  2740. enable_irq(dwc->irq_gadget);
  2741. }
  2742. }