gadget.c 77 KB

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