gadget.c 81 KB

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