gadget.c 81 KB

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