gadget.c 81 KB

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