gadget.c 62 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534
  1. /**
  2. * gadget.c - DesignWare USB3 DRD Controller Gadget Framework Link
  3. *
  4. * Copyright (C) 2010-2011 Texas Instruments Incorporated - http://www.ti.com
  5. *
  6. * Authors: Felipe Balbi <balbi@ti.com>,
  7. * Sebastian Andrzej Siewior <bigeasy@linutronix.de>
  8. *
  9. * Redistribution and use in source and binary forms, with or without
  10. * modification, are permitted provided that the following conditions
  11. * are met:
  12. * 1. Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions, and the following disclaimer,
  14. * without modification.
  15. * 2. Redistributions in binary form must reproduce the above copyright
  16. * notice, this list of conditions and the following disclaimer in the
  17. * documentation and/or other materials provided with the distribution.
  18. * 3. The names of the above-listed copyright holders may not be used
  19. * to endorse or promote products derived from this software without
  20. * specific prior written permission.
  21. *
  22. * ALTERNATIVELY, this software may be distributed under the terms of the
  23. * GNU General Public License ("GPL") version 2, as published by the Free
  24. * Software Foundation.
  25. *
  26. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  27. * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  28. * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  29. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  30. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  31. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  32. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  33. * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  34. * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  35. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  36. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  37. */
  38. #include <linux/kernel.h>
  39. #include <linux/delay.h>
  40. #include <linux/slab.h>
  41. #include <linux/spinlock.h>
  42. #include <linux/platform_device.h>
  43. #include <linux/pm_runtime.h>
  44. #include <linux/interrupt.h>
  45. #include <linux/io.h>
  46. #include <linux/list.h>
  47. #include <linux/dma-mapping.h>
  48. #include <linux/usb/ch9.h>
  49. #include <linux/usb/gadget.h>
  50. #include "core.h"
  51. #include "gadget.h"
  52. #include "io.h"
  53. /**
  54. * dwc3_gadget_set_test_mode - Enables USB2 Test Modes
  55. * @dwc: pointer to our context structure
  56. * @mode: the mode to set (J, K SE0 NAK, Force Enable)
  57. *
  58. * Caller should take care of locking. This function will
  59. * return 0 on success or -EINVAL if wrong Test Selector
  60. * is passed
  61. */
  62. int dwc3_gadget_set_test_mode(struct dwc3 *dwc, int mode)
  63. {
  64. u32 reg;
  65. reg = dwc3_readl(dwc->regs, DWC3_DCTL);
  66. reg &= ~DWC3_DCTL_TSTCTRL_MASK;
  67. switch (mode) {
  68. case TEST_J:
  69. case TEST_K:
  70. case TEST_SE0_NAK:
  71. case TEST_PACKET:
  72. case TEST_FORCE_EN:
  73. reg |= mode << 1;
  74. break;
  75. default:
  76. return -EINVAL;
  77. }
  78. dwc3_writel(dwc->regs, DWC3_DCTL, reg);
  79. return 0;
  80. }
  81. /**
  82. * dwc3_gadget_set_link_state - Sets USB Link to a particular State
  83. * @dwc: pointer to our context structure
  84. * @state: the state to put link into
  85. *
  86. * Caller should take care of locking. This function will
  87. * return 0 on success or -ETIMEDOUT.
  88. */
  89. int dwc3_gadget_set_link_state(struct dwc3 *dwc, enum dwc3_link_state state)
  90. {
  91. int retries = 10000;
  92. u32 reg;
  93. /*
  94. * Wait until device controller is ready. Only applies to 1.94a and
  95. * later RTL.
  96. */
  97. if (dwc->revision >= DWC3_REVISION_194A) {
  98. while (--retries) {
  99. reg = dwc3_readl(dwc->regs, DWC3_DSTS);
  100. if (reg & DWC3_DSTS_DCNRD)
  101. udelay(5);
  102. else
  103. break;
  104. }
  105. if (retries <= 0)
  106. return -ETIMEDOUT;
  107. }
  108. reg = dwc3_readl(dwc->regs, DWC3_DCTL);
  109. reg &= ~DWC3_DCTL_ULSTCHNGREQ_MASK;
  110. /* set requested state */
  111. reg |= DWC3_DCTL_ULSTCHNGREQ(state);
  112. dwc3_writel(dwc->regs, DWC3_DCTL, reg);
  113. /*
  114. * The following code is racy when called from dwc3_gadget_wakeup,
  115. * and is not needed, at least on newer versions
  116. */
  117. if (dwc->revision >= DWC3_REVISION_194A)
  118. return 0;
  119. /* wait for a change in DSTS */
  120. retries = 10000;
  121. while (--retries) {
  122. reg = dwc3_readl(dwc->regs, DWC3_DSTS);
  123. if (DWC3_DSTS_USBLNKST(reg) == state)
  124. return 0;
  125. udelay(5);
  126. }
  127. dev_vdbg(dwc->dev, "link state change request timed out\n");
  128. return -ETIMEDOUT;
  129. }
  130. /**
  131. * dwc3_gadget_resize_tx_fifos - reallocate fifo spaces for current use-case
  132. * @dwc: pointer to our context structure
  133. *
  134. * This function will a best effort FIFO allocation in order
  135. * to improve FIFO usage and throughput, while still allowing
  136. * us to enable as many endpoints as possible.
  137. *
  138. * Keep in mind that this operation will be highly dependent
  139. * on the configured size for RAM1 - which contains TxFifo -,
  140. * the amount of endpoints enabled on coreConsultant tool, and
  141. * the width of the Master Bus.
  142. *
  143. * In the ideal world, we would always be able to satisfy the
  144. * following equation:
  145. *
  146. * ((512 + 2 * MDWIDTH-Bytes) + (Number of IN Endpoints - 1) * \
  147. * (3 * (1024 + MDWIDTH-Bytes) + MDWIDTH-Bytes)) / MDWIDTH-Bytes
  148. *
  149. * Unfortunately, due to many variables that's not always the case.
  150. */
  151. int dwc3_gadget_resize_tx_fifos(struct dwc3 *dwc)
  152. {
  153. int last_fifo_depth = 0;
  154. int ram1_depth;
  155. int fifo_size;
  156. int mdwidth;
  157. int num;
  158. if (!dwc->needs_fifo_resize)
  159. return 0;
  160. ram1_depth = DWC3_RAM1_DEPTH(dwc->hwparams.hwparams7);
  161. mdwidth = DWC3_MDWIDTH(dwc->hwparams.hwparams0);
  162. /* MDWIDTH is represented in bits, we need it in bytes */
  163. mdwidth >>= 3;
  164. /*
  165. * FIXME For now we will only allocate 1 wMaxPacketSize space
  166. * for each enabled endpoint, later patches will come to
  167. * improve this algorithm so that we better use the internal
  168. * FIFO space
  169. */
  170. for (num = 0; num < DWC3_ENDPOINTS_NUM; num++) {
  171. struct dwc3_ep *dep = dwc->eps[num];
  172. int fifo_number = dep->number >> 1;
  173. int mult = 1;
  174. int tmp;
  175. if (!(dep->number & 1))
  176. continue;
  177. if (!(dep->flags & DWC3_EP_ENABLED))
  178. continue;
  179. if (usb_endpoint_xfer_bulk(dep->endpoint.desc)
  180. || usb_endpoint_xfer_isoc(dep->endpoint.desc))
  181. mult = 3;
  182. /*
  183. * REVISIT: the following assumes we will always have enough
  184. * space available on the FIFO RAM for all possible use cases.
  185. * Make sure that's true somehow and change FIFO allocation
  186. * accordingly.
  187. *
  188. * If we have Bulk or Isochronous endpoints, we want
  189. * them to be able to be very, very fast. So we're giving
  190. * those endpoints a fifo_size which is enough for 3 full
  191. * packets
  192. */
  193. tmp = mult * (dep->endpoint.maxpacket + mdwidth);
  194. tmp += mdwidth;
  195. fifo_size = DIV_ROUND_UP(tmp, mdwidth);
  196. fifo_size |= (last_fifo_depth << 16);
  197. dev_vdbg(dwc->dev, "%s: Fifo Addr %04x Size %d\n",
  198. dep->name, last_fifo_depth, fifo_size & 0xffff);
  199. dwc3_writel(dwc->regs, DWC3_GTXFIFOSIZ(fifo_number),
  200. fifo_size);
  201. last_fifo_depth += (fifo_size & 0xffff);
  202. }
  203. return 0;
  204. }
  205. void dwc3_gadget_giveback(struct dwc3_ep *dep, struct dwc3_request *req,
  206. int status)
  207. {
  208. struct dwc3 *dwc = dep->dwc;
  209. if (req->queued) {
  210. if (req->request.num_mapped_sgs)
  211. dep->busy_slot += req->request.num_mapped_sgs;
  212. else
  213. dep->busy_slot++;
  214. /*
  215. * Skip LINK TRB. We can't use req->trb and check for
  216. * DWC3_TRBCTL_LINK_TRB because it points the TRB we just
  217. * completed (not the LINK TRB).
  218. */
  219. if (((dep->busy_slot & DWC3_TRB_MASK) == DWC3_TRB_NUM - 1) &&
  220. usb_endpoint_xfer_isoc(dep->endpoint.desc))
  221. dep->busy_slot++;
  222. }
  223. list_del(&req->list);
  224. req->trb = NULL;
  225. if (req->request.status == -EINPROGRESS)
  226. req->request.status = status;
  227. usb_gadget_unmap_request(&dwc->gadget, &req->request,
  228. req->direction);
  229. dev_dbg(dwc->dev, "request %p from %s completed %d/%d ===> %d\n",
  230. req, dep->name, req->request.actual,
  231. req->request.length, status);
  232. spin_unlock(&dwc->lock);
  233. req->request.complete(&dep->endpoint, &req->request);
  234. spin_lock(&dwc->lock);
  235. }
  236. static const char *dwc3_gadget_ep_cmd_string(u8 cmd)
  237. {
  238. switch (cmd) {
  239. case DWC3_DEPCMD_DEPSTARTCFG:
  240. return "Start New Configuration";
  241. case DWC3_DEPCMD_ENDTRANSFER:
  242. return "End Transfer";
  243. case DWC3_DEPCMD_UPDATETRANSFER:
  244. return "Update Transfer";
  245. case DWC3_DEPCMD_STARTTRANSFER:
  246. return "Start Transfer";
  247. case DWC3_DEPCMD_CLEARSTALL:
  248. return "Clear Stall";
  249. case DWC3_DEPCMD_SETSTALL:
  250. return "Set Stall";
  251. case DWC3_DEPCMD_GETEPSTATE:
  252. return "Get Endpoint State";
  253. case DWC3_DEPCMD_SETTRANSFRESOURCE:
  254. return "Set Endpoint Transfer Resource";
  255. case DWC3_DEPCMD_SETEPCONFIG:
  256. return "Set Endpoint Configuration";
  257. default:
  258. return "UNKNOWN command";
  259. }
  260. }
  261. int dwc3_send_gadget_generic_command(struct dwc3 *dwc, int cmd, u32 param)
  262. {
  263. u32 timeout = 500;
  264. u32 reg;
  265. dwc3_writel(dwc->regs, DWC3_DGCMDPAR, param);
  266. dwc3_writel(dwc->regs, DWC3_DGCMD, cmd | DWC3_DGCMD_CMDACT);
  267. do {
  268. reg = dwc3_readl(dwc->regs, DWC3_DGCMD);
  269. if (!(reg & DWC3_DGCMD_CMDACT)) {
  270. dev_vdbg(dwc->dev, "Command Complete --> %d\n",
  271. DWC3_DGCMD_STATUS(reg));
  272. return 0;
  273. }
  274. /*
  275. * We can't sleep here, because it's also called from
  276. * interrupt context.
  277. */
  278. timeout--;
  279. if (!timeout)
  280. return -ETIMEDOUT;
  281. udelay(1);
  282. } while (1);
  283. }
  284. int dwc3_send_gadget_ep_cmd(struct dwc3 *dwc, unsigned ep,
  285. unsigned cmd, struct dwc3_gadget_ep_cmd_params *params)
  286. {
  287. struct dwc3_ep *dep = dwc->eps[ep];
  288. u32 timeout = 500;
  289. u32 reg;
  290. dev_vdbg(dwc->dev, "%s: cmd '%s' params %08x %08x %08x\n",
  291. dep->name,
  292. dwc3_gadget_ep_cmd_string(cmd), params->param0,
  293. params->param1, params->param2);
  294. dwc3_writel(dwc->regs, DWC3_DEPCMDPAR0(ep), params->param0);
  295. dwc3_writel(dwc->regs, DWC3_DEPCMDPAR1(ep), params->param1);
  296. dwc3_writel(dwc->regs, DWC3_DEPCMDPAR2(ep), params->param2);
  297. dwc3_writel(dwc->regs, DWC3_DEPCMD(ep), cmd | DWC3_DEPCMD_CMDACT);
  298. do {
  299. reg = dwc3_readl(dwc->regs, DWC3_DEPCMD(ep));
  300. if (!(reg & DWC3_DEPCMD_CMDACT)) {
  301. dev_vdbg(dwc->dev, "Command Complete --> %d\n",
  302. DWC3_DEPCMD_STATUS(reg));
  303. return 0;
  304. }
  305. /*
  306. * We can't sleep here, because it is also called from
  307. * interrupt context.
  308. */
  309. timeout--;
  310. if (!timeout)
  311. return -ETIMEDOUT;
  312. udelay(1);
  313. } while (1);
  314. }
  315. static dma_addr_t dwc3_trb_dma_offset(struct dwc3_ep *dep,
  316. struct dwc3_trb *trb)
  317. {
  318. u32 offset = (char *) trb - (char *) dep->trb_pool;
  319. return dep->trb_pool_dma + offset;
  320. }
  321. static int dwc3_alloc_trb_pool(struct dwc3_ep *dep)
  322. {
  323. struct dwc3 *dwc = dep->dwc;
  324. if (dep->trb_pool)
  325. return 0;
  326. if (dep->number == 0 || dep->number == 1)
  327. return 0;
  328. dep->trb_pool = dma_alloc_coherent(dwc->dev,
  329. sizeof(struct dwc3_trb) * DWC3_TRB_NUM,
  330. &dep->trb_pool_dma, GFP_KERNEL);
  331. if (!dep->trb_pool) {
  332. dev_err(dep->dwc->dev, "failed to allocate trb pool for %s\n",
  333. dep->name);
  334. return -ENOMEM;
  335. }
  336. return 0;
  337. }
  338. static void dwc3_free_trb_pool(struct dwc3_ep *dep)
  339. {
  340. struct dwc3 *dwc = dep->dwc;
  341. dma_free_coherent(dwc->dev, sizeof(struct dwc3_trb) * DWC3_TRB_NUM,
  342. dep->trb_pool, dep->trb_pool_dma);
  343. dep->trb_pool = NULL;
  344. dep->trb_pool_dma = 0;
  345. }
  346. static int dwc3_gadget_start_config(struct dwc3 *dwc, struct dwc3_ep *dep)
  347. {
  348. struct dwc3_gadget_ep_cmd_params params;
  349. u32 cmd;
  350. memset(&params, 0x00, sizeof(params));
  351. if (dep->number != 1) {
  352. cmd = DWC3_DEPCMD_DEPSTARTCFG;
  353. /* XferRscIdx == 0 for ep0 and 2 for the remaining */
  354. if (dep->number > 1) {
  355. if (dwc->start_config_issued)
  356. return 0;
  357. dwc->start_config_issued = true;
  358. cmd |= DWC3_DEPCMD_PARAM(2);
  359. }
  360. return dwc3_send_gadget_ep_cmd(dwc, 0, cmd, &params);
  361. }
  362. return 0;
  363. }
  364. static int dwc3_gadget_set_ep_config(struct dwc3 *dwc, struct dwc3_ep *dep,
  365. const struct usb_endpoint_descriptor *desc,
  366. const struct usb_ss_ep_comp_descriptor *comp_desc,
  367. bool ignore)
  368. {
  369. struct dwc3_gadget_ep_cmd_params params;
  370. memset(&params, 0x00, sizeof(params));
  371. params.param0 = DWC3_DEPCFG_EP_TYPE(usb_endpoint_type(desc))
  372. | DWC3_DEPCFG_MAX_PACKET_SIZE(usb_endpoint_maxp(desc))
  373. | DWC3_DEPCFG_BURST_SIZE(dep->endpoint.maxburst - 1);
  374. if (ignore)
  375. params.param0 |= DWC3_DEPCFG_IGN_SEQ_NUM;
  376. params.param1 = DWC3_DEPCFG_XFER_COMPLETE_EN
  377. | DWC3_DEPCFG_XFER_NOT_READY_EN;
  378. if (usb_ss_max_streams(comp_desc) && usb_endpoint_xfer_bulk(desc)) {
  379. params.param1 |= DWC3_DEPCFG_STREAM_CAPABLE
  380. | DWC3_DEPCFG_STREAM_EVENT_EN;
  381. dep->stream_capable = true;
  382. }
  383. if (usb_endpoint_xfer_isoc(desc))
  384. params.param1 |= DWC3_DEPCFG_XFER_IN_PROGRESS_EN;
  385. /*
  386. * We are doing 1:1 mapping for endpoints, meaning
  387. * Physical Endpoints 2 maps to Logical Endpoint 2 and
  388. * so on. We consider the direction bit as part of the physical
  389. * endpoint number. So USB endpoint 0x81 is 0x03.
  390. */
  391. params.param1 |= DWC3_DEPCFG_EP_NUMBER(dep->number);
  392. /*
  393. * We must use the lower 16 TX FIFOs even though
  394. * HW might have more
  395. */
  396. if (dep->direction)
  397. params.param0 |= DWC3_DEPCFG_FIFO_NUMBER(dep->number >> 1);
  398. if (desc->bInterval) {
  399. params.param1 |= DWC3_DEPCFG_BINTERVAL_M1(desc->bInterval - 1);
  400. dep->interval = 1 << (desc->bInterval - 1);
  401. }
  402. return dwc3_send_gadget_ep_cmd(dwc, dep->number,
  403. DWC3_DEPCMD_SETEPCONFIG, &params);
  404. }
  405. static int dwc3_gadget_set_xfer_resource(struct dwc3 *dwc, struct dwc3_ep *dep)
  406. {
  407. struct dwc3_gadget_ep_cmd_params params;
  408. memset(&params, 0x00, sizeof(params));
  409. params.param0 = DWC3_DEPXFERCFG_NUM_XFER_RES(1);
  410. return dwc3_send_gadget_ep_cmd(dwc, dep->number,
  411. DWC3_DEPCMD_SETTRANSFRESOURCE, &params);
  412. }
  413. /**
  414. * __dwc3_gadget_ep_enable - Initializes a HW endpoint
  415. * @dep: endpoint to be initialized
  416. * @desc: USB Endpoint Descriptor
  417. *
  418. * Caller should take care of locking
  419. */
  420. static int __dwc3_gadget_ep_enable(struct dwc3_ep *dep,
  421. const struct usb_endpoint_descriptor *desc,
  422. const struct usb_ss_ep_comp_descriptor *comp_desc,
  423. bool ignore)
  424. {
  425. struct dwc3 *dwc = dep->dwc;
  426. u32 reg;
  427. int ret = -ENOMEM;
  428. if (!(dep->flags & DWC3_EP_ENABLED)) {
  429. ret = dwc3_gadget_start_config(dwc, dep);
  430. if (ret)
  431. return ret;
  432. }
  433. ret = dwc3_gadget_set_ep_config(dwc, dep, desc, comp_desc, ignore);
  434. if (ret)
  435. return ret;
  436. if (!(dep->flags & DWC3_EP_ENABLED)) {
  437. struct dwc3_trb *trb_st_hw;
  438. struct dwc3_trb *trb_link;
  439. ret = dwc3_gadget_set_xfer_resource(dwc, dep);
  440. if (ret)
  441. return ret;
  442. dep->endpoint.desc = desc;
  443. dep->comp_desc = comp_desc;
  444. dep->type = usb_endpoint_type(desc);
  445. dep->flags |= DWC3_EP_ENABLED;
  446. reg = dwc3_readl(dwc->regs, DWC3_DALEPENA);
  447. reg |= DWC3_DALEPENA_EP(dep->number);
  448. dwc3_writel(dwc->regs, DWC3_DALEPENA, reg);
  449. if (!usb_endpoint_xfer_isoc(desc))
  450. return 0;
  451. memset(&trb_link, 0, sizeof(trb_link));
  452. /* Link TRB for ISOC. The HWO bit is never reset */
  453. trb_st_hw = &dep->trb_pool[0];
  454. trb_link = &dep->trb_pool[DWC3_TRB_NUM - 1];
  455. trb_link->bpl = lower_32_bits(dwc3_trb_dma_offset(dep, trb_st_hw));
  456. trb_link->bph = upper_32_bits(dwc3_trb_dma_offset(dep, trb_st_hw));
  457. trb_link->ctrl |= DWC3_TRBCTL_LINK_TRB;
  458. trb_link->ctrl |= DWC3_TRB_CTRL_HWO;
  459. }
  460. return 0;
  461. }
  462. static void dwc3_stop_active_transfer(struct dwc3 *dwc, u32 epnum);
  463. static void dwc3_remove_requests(struct dwc3 *dwc, struct dwc3_ep *dep)
  464. {
  465. struct dwc3_request *req;
  466. if (!list_empty(&dep->req_queued)) {
  467. dwc3_stop_active_transfer(dwc, dep->number);
  468. /*
  469. * NOTICE: We are violating what the Databook says about the
  470. * EndTransfer command. Ideally we would _always_ wait for the
  471. * EndTransfer Command Completion IRQ, but that's causing too
  472. * much trouble synchronizing between us and gadget driver.
  473. *
  474. * We have discussed this with the IP Provider and it was
  475. * suggested to giveback all requests here, but give HW some
  476. * extra time to synchronize with the interconnect. We're using
  477. * an arbitraty 100us delay for that.
  478. *
  479. * Note also that a similar handling was tested by Synopsys
  480. * (thanks a lot Paul) and nothing bad has come out of it.
  481. * In short, what we're doing is:
  482. *
  483. * - Issue EndTransfer WITH CMDIOC bit set
  484. * - Wait 100us
  485. * - giveback all requests to gadget driver
  486. */
  487. udelay(100);
  488. while (!list_empty(&dep->req_queued)) {
  489. req = next_request(&dep->req_queued);
  490. dwc3_gadget_giveback(dep, req, -ESHUTDOWN);
  491. }
  492. }
  493. while (!list_empty(&dep->request_list)) {
  494. req = next_request(&dep->request_list);
  495. dwc3_gadget_giveback(dep, req, -ESHUTDOWN);
  496. }
  497. }
  498. /**
  499. * __dwc3_gadget_ep_disable - Disables a HW endpoint
  500. * @dep: the endpoint to disable
  501. *
  502. * This function also removes requests which are currently processed ny the
  503. * hardware and those which are not yet scheduled.
  504. * Caller should take care of locking.
  505. */
  506. static int __dwc3_gadget_ep_disable(struct dwc3_ep *dep)
  507. {
  508. struct dwc3 *dwc = dep->dwc;
  509. u32 reg;
  510. dwc3_remove_requests(dwc, dep);
  511. reg = dwc3_readl(dwc->regs, DWC3_DALEPENA);
  512. reg &= ~DWC3_DALEPENA_EP(dep->number);
  513. dwc3_writel(dwc->regs, DWC3_DALEPENA, reg);
  514. dep->stream_capable = false;
  515. dep->endpoint.desc = NULL;
  516. dep->comp_desc = NULL;
  517. dep->type = 0;
  518. dep->flags = 0;
  519. return 0;
  520. }
  521. /* -------------------------------------------------------------------------- */
  522. static int dwc3_gadget_ep0_enable(struct usb_ep *ep,
  523. const struct usb_endpoint_descriptor *desc)
  524. {
  525. return -EINVAL;
  526. }
  527. static int dwc3_gadget_ep0_disable(struct usb_ep *ep)
  528. {
  529. return -EINVAL;
  530. }
  531. /* -------------------------------------------------------------------------- */
  532. static int dwc3_gadget_ep_enable(struct usb_ep *ep,
  533. const struct usb_endpoint_descriptor *desc)
  534. {
  535. struct dwc3_ep *dep;
  536. struct dwc3 *dwc;
  537. unsigned long flags;
  538. int ret;
  539. if (!ep || !desc || desc->bDescriptorType != USB_DT_ENDPOINT) {
  540. pr_debug("dwc3: invalid parameters\n");
  541. return -EINVAL;
  542. }
  543. if (!desc->wMaxPacketSize) {
  544. pr_debug("dwc3: missing wMaxPacketSize\n");
  545. return -EINVAL;
  546. }
  547. dep = to_dwc3_ep(ep);
  548. dwc = dep->dwc;
  549. switch (usb_endpoint_type(desc)) {
  550. case USB_ENDPOINT_XFER_CONTROL:
  551. strlcat(dep->name, "-control", sizeof(dep->name));
  552. break;
  553. case USB_ENDPOINT_XFER_ISOC:
  554. strlcat(dep->name, "-isoc", sizeof(dep->name));
  555. break;
  556. case USB_ENDPOINT_XFER_BULK:
  557. strlcat(dep->name, "-bulk", sizeof(dep->name));
  558. break;
  559. case USB_ENDPOINT_XFER_INT:
  560. strlcat(dep->name, "-int", sizeof(dep->name));
  561. break;
  562. default:
  563. dev_err(dwc->dev, "invalid endpoint transfer type\n");
  564. }
  565. if (dep->flags & DWC3_EP_ENABLED) {
  566. dev_WARN_ONCE(dwc->dev, true, "%s is already enabled\n",
  567. dep->name);
  568. return 0;
  569. }
  570. dev_vdbg(dwc->dev, "Enabling %s\n", dep->name);
  571. spin_lock_irqsave(&dwc->lock, flags);
  572. ret = __dwc3_gadget_ep_enable(dep, desc, ep->comp_desc, false);
  573. spin_unlock_irqrestore(&dwc->lock, flags);
  574. return ret;
  575. }
  576. static int dwc3_gadget_ep_disable(struct usb_ep *ep)
  577. {
  578. struct dwc3_ep *dep;
  579. struct dwc3 *dwc;
  580. unsigned long flags;
  581. int ret;
  582. if (!ep) {
  583. pr_debug("dwc3: invalid parameters\n");
  584. return -EINVAL;
  585. }
  586. dep = to_dwc3_ep(ep);
  587. dwc = dep->dwc;
  588. if (!(dep->flags & DWC3_EP_ENABLED)) {
  589. dev_WARN_ONCE(dwc->dev, true, "%s is already disabled\n",
  590. dep->name);
  591. return 0;
  592. }
  593. snprintf(dep->name, sizeof(dep->name), "ep%d%s",
  594. dep->number >> 1,
  595. (dep->number & 1) ? "in" : "out");
  596. spin_lock_irqsave(&dwc->lock, flags);
  597. ret = __dwc3_gadget_ep_disable(dep);
  598. spin_unlock_irqrestore(&dwc->lock, flags);
  599. return ret;
  600. }
  601. static struct usb_request *dwc3_gadget_ep_alloc_request(struct usb_ep *ep,
  602. gfp_t gfp_flags)
  603. {
  604. struct dwc3_request *req;
  605. struct dwc3_ep *dep = to_dwc3_ep(ep);
  606. struct dwc3 *dwc = dep->dwc;
  607. req = kzalloc(sizeof(*req), gfp_flags);
  608. if (!req) {
  609. dev_err(dwc->dev, "not enough memory\n");
  610. return NULL;
  611. }
  612. req->epnum = dep->number;
  613. req->dep = dep;
  614. return &req->request;
  615. }
  616. static void dwc3_gadget_ep_free_request(struct usb_ep *ep,
  617. struct usb_request *request)
  618. {
  619. struct dwc3_request *req = to_dwc3_request(request);
  620. kfree(req);
  621. }
  622. /**
  623. * dwc3_prepare_one_trb - setup one TRB from one request
  624. * @dep: endpoint for which this request is prepared
  625. * @req: dwc3_request pointer
  626. */
  627. static void dwc3_prepare_one_trb(struct dwc3_ep *dep,
  628. struct dwc3_request *req, dma_addr_t dma,
  629. unsigned length, unsigned last, unsigned chain)
  630. {
  631. struct dwc3 *dwc = dep->dwc;
  632. struct dwc3_trb *trb;
  633. unsigned int cur_slot;
  634. dev_vdbg(dwc->dev, "%s: req %p dma %08llx length %d%s%s\n",
  635. dep->name, req, (unsigned long long) dma,
  636. length, last ? " last" : "",
  637. chain ? " chain" : "");
  638. trb = &dep->trb_pool[dep->free_slot & DWC3_TRB_MASK];
  639. cur_slot = dep->free_slot;
  640. dep->free_slot++;
  641. /* Skip the LINK-TRB on ISOC */
  642. if (((cur_slot & DWC3_TRB_MASK) == DWC3_TRB_NUM - 1) &&
  643. usb_endpoint_xfer_isoc(dep->endpoint.desc))
  644. return;
  645. if (!req->trb) {
  646. dwc3_gadget_move_request_queued(req);
  647. req->trb = trb;
  648. req->trb_dma = dwc3_trb_dma_offset(dep, trb);
  649. }
  650. trb->size = DWC3_TRB_SIZE_LENGTH(length);
  651. trb->bpl = lower_32_bits(dma);
  652. trb->bph = upper_32_bits(dma);
  653. switch (usb_endpoint_type(dep->endpoint.desc)) {
  654. case USB_ENDPOINT_XFER_CONTROL:
  655. trb->ctrl = DWC3_TRBCTL_CONTROL_SETUP;
  656. break;
  657. case USB_ENDPOINT_XFER_ISOC:
  658. trb->ctrl = DWC3_TRBCTL_ISOCHRONOUS_FIRST;
  659. if (!req->request.no_interrupt)
  660. trb->ctrl |= DWC3_TRB_CTRL_IOC;
  661. break;
  662. case USB_ENDPOINT_XFER_BULK:
  663. case USB_ENDPOINT_XFER_INT:
  664. trb->ctrl = DWC3_TRBCTL_NORMAL;
  665. break;
  666. default:
  667. /*
  668. * This is only possible with faulty memory because we
  669. * checked it already :)
  670. */
  671. BUG();
  672. }
  673. if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
  674. trb->ctrl |= DWC3_TRB_CTRL_ISP_IMI;
  675. trb->ctrl |= DWC3_TRB_CTRL_CSP;
  676. } else {
  677. if (chain)
  678. trb->ctrl |= DWC3_TRB_CTRL_CHN;
  679. if (last)
  680. trb->ctrl |= DWC3_TRB_CTRL_LST;
  681. }
  682. if (usb_endpoint_xfer_bulk(dep->endpoint.desc) && dep->stream_capable)
  683. trb->ctrl |= DWC3_TRB_CTRL_SID_SOFN(req->request.stream_id);
  684. trb->ctrl |= DWC3_TRB_CTRL_HWO;
  685. }
  686. /*
  687. * dwc3_prepare_trbs - setup TRBs from requests
  688. * @dep: endpoint for which requests are being prepared
  689. * @starting: true if the endpoint is idle and no requests are queued.
  690. *
  691. * The function goes through the requests list and sets up TRBs for the
  692. * transfers. The function returns once there are no more TRBs available or
  693. * it runs out of requests.
  694. */
  695. static void dwc3_prepare_trbs(struct dwc3_ep *dep, bool starting)
  696. {
  697. struct dwc3_request *req, *n;
  698. u32 trbs_left;
  699. u32 max;
  700. unsigned int last_one = 0;
  701. BUILD_BUG_ON_NOT_POWER_OF_2(DWC3_TRB_NUM);
  702. /* the first request must not be queued */
  703. trbs_left = (dep->busy_slot - dep->free_slot) & DWC3_TRB_MASK;
  704. /* Can't wrap around on a non-isoc EP since there's no link TRB */
  705. if (!usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
  706. max = DWC3_TRB_NUM - (dep->free_slot & DWC3_TRB_MASK);
  707. if (trbs_left > max)
  708. trbs_left = max;
  709. }
  710. /*
  711. * If busy & slot are equal than it is either full or empty. If we are
  712. * starting to process requests then we are empty. Otherwise we are
  713. * full and don't do anything
  714. */
  715. if (!trbs_left) {
  716. if (!starting)
  717. return;
  718. trbs_left = DWC3_TRB_NUM;
  719. /*
  720. * In case we start from scratch, we queue the ISOC requests
  721. * starting from slot 1. This is done because we use ring
  722. * buffer and have no LST bit to stop us. Instead, we place
  723. * IOC bit every TRB_NUM/4. We try to avoid having an interrupt
  724. * after the first request so we start at slot 1 and have
  725. * 7 requests proceed before we hit the first IOC.
  726. * Other transfer types don't use the ring buffer and are
  727. * processed from the first TRB until the last one. Since we
  728. * don't wrap around we have to start at the beginning.
  729. */
  730. if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
  731. dep->busy_slot = 1;
  732. dep->free_slot = 1;
  733. } else {
  734. dep->busy_slot = 0;
  735. dep->free_slot = 0;
  736. }
  737. }
  738. /* The last TRB is a link TRB, not used for xfer */
  739. if ((trbs_left <= 1) && usb_endpoint_xfer_isoc(dep->endpoint.desc))
  740. return;
  741. list_for_each_entry_safe(req, n, &dep->request_list, list) {
  742. unsigned length;
  743. dma_addr_t dma;
  744. if (req->request.num_mapped_sgs > 0) {
  745. struct usb_request *request = &req->request;
  746. struct scatterlist *sg = request->sg;
  747. struct scatterlist *s;
  748. int i;
  749. for_each_sg(sg, s, request->num_mapped_sgs, i) {
  750. unsigned chain = true;
  751. length = sg_dma_len(s);
  752. dma = sg_dma_address(s);
  753. if (i == (request->num_mapped_sgs - 1) ||
  754. sg_is_last(s)) {
  755. last_one = true;
  756. chain = false;
  757. }
  758. trbs_left--;
  759. if (!trbs_left)
  760. last_one = true;
  761. if (last_one)
  762. chain = false;
  763. dwc3_prepare_one_trb(dep, req, dma, length,
  764. last_one, chain);
  765. if (last_one)
  766. break;
  767. }
  768. } else {
  769. dma = req->request.dma;
  770. length = req->request.length;
  771. trbs_left--;
  772. if (!trbs_left)
  773. last_one = 1;
  774. /* Is this the last request? */
  775. if (list_is_last(&req->list, &dep->request_list))
  776. last_one = 1;
  777. dwc3_prepare_one_trb(dep, req, dma, length,
  778. last_one, false);
  779. if (last_one)
  780. break;
  781. }
  782. }
  783. }
  784. static int __dwc3_gadget_kick_transfer(struct dwc3_ep *dep, u16 cmd_param,
  785. int start_new)
  786. {
  787. struct dwc3_gadget_ep_cmd_params params;
  788. struct dwc3_request *req;
  789. struct dwc3 *dwc = dep->dwc;
  790. int ret;
  791. u32 cmd;
  792. if (start_new && (dep->flags & DWC3_EP_BUSY)) {
  793. dev_vdbg(dwc->dev, "%s: endpoint busy\n", dep->name);
  794. return -EBUSY;
  795. }
  796. dep->flags &= ~DWC3_EP_PENDING_REQUEST;
  797. /*
  798. * If we are getting here after a short-out-packet we don't enqueue any
  799. * new requests as we try to set the IOC bit only on the last request.
  800. */
  801. if (start_new) {
  802. if (list_empty(&dep->req_queued))
  803. dwc3_prepare_trbs(dep, start_new);
  804. /* req points to the first request which will be sent */
  805. req = next_request(&dep->req_queued);
  806. } else {
  807. dwc3_prepare_trbs(dep, start_new);
  808. /*
  809. * req points to the first request where HWO changed from 0 to 1
  810. */
  811. req = next_request(&dep->req_queued);
  812. }
  813. if (!req) {
  814. dep->flags |= DWC3_EP_PENDING_REQUEST;
  815. return 0;
  816. }
  817. memset(&params, 0, sizeof(params));
  818. params.param0 = upper_32_bits(req->trb_dma);
  819. params.param1 = lower_32_bits(req->trb_dma);
  820. if (start_new)
  821. cmd = DWC3_DEPCMD_STARTTRANSFER;
  822. else
  823. cmd = DWC3_DEPCMD_UPDATETRANSFER;
  824. cmd |= DWC3_DEPCMD_PARAM(cmd_param);
  825. ret = dwc3_send_gadget_ep_cmd(dwc, dep->number, cmd, &params);
  826. if (ret < 0) {
  827. dev_dbg(dwc->dev, "failed to send STARTTRANSFER command\n");
  828. /*
  829. * FIXME we need to iterate over the list of requests
  830. * here and stop, unmap, free and del each of the linked
  831. * requests instead of what we do now.
  832. */
  833. usb_gadget_unmap_request(&dwc->gadget, &req->request,
  834. req->direction);
  835. list_del(&req->list);
  836. return ret;
  837. }
  838. dep->flags |= DWC3_EP_BUSY;
  839. if (start_new) {
  840. dep->resource_index = dwc3_gadget_ep_get_transfer_index(dwc,
  841. dep->number);
  842. WARN_ON_ONCE(!dep->resource_index);
  843. }
  844. return 0;
  845. }
  846. static void __dwc3_gadget_start_isoc(struct dwc3 *dwc,
  847. struct dwc3_ep *dep, u32 cur_uf)
  848. {
  849. u32 uf;
  850. if (list_empty(&dep->request_list)) {
  851. dev_vdbg(dwc->dev, "ISOC ep %s run out for requests.\n",
  852. dep->name);
  853. return;
  854. }
  855. /* 4 micro frames in the future */
  856. uf = cur_uf + dep->interval * 4;
  857. __dwc3_gadget_kick_transfer(dep, uf, 1);
  858. }
  859. static void dwc3_gadget_start_isoc(struct dwc3 *dwc,
  860. struct dwc3_ep *dep, const struct dwc3_event_depevt *event)
  861. {
  862. u32 cur_uf, mask;
  863. mask = ~(dep->interval - 1);
  864. cur_uf = event->parameters & mask;
  865. __dwc3_gadget_start_isoc(dwc, dep, cur_uf);
  866. }
  867. static int __dwc3_gadget_ep_queue(struct dwc3_ep *dep, struct dwc3_request *req)
  868. {
  869. struct dwc3 *dwc = dep->dwc;
  870. int ret;
  871. req->request.actual = 0;
  872. req->request.status = -EINPROGRESS;
  873. req->direction = dep->direction;
  874. req->epnum = dep->number;
  875. /*
  876. * We only add to our list of requests now and
  877. * start consuming the list once we get XferNotReady
  878. * IRQ.
  879. *
  880. * That way, we avoid doing anything that we don't need
  881. * to do now and defer it until the point we receive a
  882. * particular token from the Host side.
  883. *
  884. * This will also avoid Host cancelling URBs due to too
  885. * many NAKs.
  886. */
  887. ret = usb_gadget_map_request(&dwc->gadget, &req->request,
  888. dep->direction);
  889. if (ret)
  890. return ret;
  891. list_add_tail(&req->list, &dep->request_list);
  892. /*
  893. * There are a few special cases:
  894. *
  895. * 1. XferNotReady with empty list of requests. We need to kick the
  896. * transfer here in that situation, otherwise we will be NAKing
  897. * forever. If we get XferNotReady before gadget driver has a
  898. * chance to queue a request, we will ACK the IRQ but won't be
  899. * able to receive the data until the next request is queued.
  900. * The following code is handling exactly that.
  901. *
  902. */
  903. if (dep->flags & DWC3_EP_PENDING_REQUEST) {
  904. int ret;
  905. ret = __dwc3_gadget_kick_transfer(dep, 0, true);
  906. if (ret && ret != -EBUSY) {
  907. struct dwc3 *dwc = dep->dwc;
  908. dev_dbg(dwc->dev, "%s: failed to kick transfers\n",
  909. dep->name);
  910. }
  911. }
  912. /*
  913. * 2. XferInProgress on Isoc EP with an active transfer. We need to
  914. * kick the transfer here after queuing a request, otherwise the
  915. * core may not see the modified TRB(s).
  916. */
  917. if (usb_endpoint_xfer_isoc(dep->endpoint.desc) &&
  918. (dep->flags & DWC3_EP_BUSY)) {
  919. WARN_ON_ONCE(!dep->resource_index);
  920. ret = __dwc3_gadget_kick_transfer(dep, dep->resource_index,
  921. false);
  922. if (ret && ret != -EBUSY) {
  923. struct dwc3 *dwc = dep->dwc;
  924. dev_dbg(dwc->dev, "%s: failed to kick transfers\n",
  925. dep->name);
  926. }
  927. }
  928. /*
  929. * 3. Missed ISOC Handling. We need to start isoc transfer on the saved
  930. * uframe number.
  931. */
  932. if (usb_endpoint_xfer_isoc(dep->endpoint.desc) &&
  933. (dep->flags & DWC3_EP_MISSED_ISOC)) {
  934. __dwc3_gadget_start_isoc(dwc, dep, dep->current_uf);
  935. dep->flags &= ~DWC3_EP_MISSED_ISOC;
  936. }
  937. return 0;
  938. }
  939. static int dwc3_gadget_ep_queue(struct usb_ep *ep, struct usb_request *request,
  940. gfp_t gfp_flags)
  941. {
  942. struct dwc3_request *req = to_dwc3_request(request);
  943. struct dwc3_ep *dep = to_dwc3_ep(ep);
  944. struct dwc3 *dwc = dep->dwc;
  945. unsigned long flags;
  946. int ret;
  947. if (!dep->endpoint.desc) {
  948. dev_dbg(dwc->dev, "trying to queue request %p to disabled %s\n",
  949. request, ep->name);
  950. return -ESHUTDOWN;
  951. }
  952. dev_vdbg(dwc->dev, "queing request %p to %s length %d\n",
  953. request, ep->name, request->length);
  954. spin_lock_irqsave(&dwc->lock, flags);
  955. ret = __dwc3_gadget_ep_queue(dep, req);
  956. spin_unlock_irqrestore(&dwc->lock, flags);
  957. return ret;
  958. }
  959. static int dwc3_gadget_ep_dequeue(struct usb_ep *ep,
  960. struct usb_request *request)
  961. {
  962. struct dwc3_request *req = to_dwc3_request(request);
  963. struct dwc3_request *r = NULL;
  964. struct dwc3_ep *dep = to_dwc3_ep(ep);
  965. struct dwc3 *dwc = dep->dwc;
  966. unsigned long flags;
  967. int ret = 0;
  968. spin_lock_irqsave(&dwc->lock, flags);
  969. list_for_each_entry(r, &dep->request_list, list) {
  970. if (r == req)
  971. break;
  972. }
  973. if (r != req) {
  974. list_for_each_entry(r, &dep->req_queued, list) {
  975. if (r == req)
  976. break;
  977. }
  978. if (r == req) {
  979. /* wait until it is processed */
  980. dwc3_stop_active_transfer(dwc, dep->number);
  981. goto out1;
  982. }
  983. dev_err(dwc->dev, "request %p was not queued to %s\n",
  984. request, ep->name);
  985. ret = -EINVAL;
  986. goto out0;
  987. }
  988. out1:
  989. /* giveback the request */
  990. dwc3_gadget_giveback(dep, req, -ECONNRESET);
  991. out0:
  992. spin_unlock_irqrestore(&dwc->lock, flags);
  993. return ret;
  994. }
  995. int __dwc3_gadget_ep_set_halt(struct dwc3_ep *dep, int value)
  996. {
  997. struct dwc3_gadget_ep_cmd_params params;
  998. struct dwc3 *dwc = dep->dwc;
  999. int ret;
  1000. memset(&params, 0x00, sizeof(params));
  1001. if (value) {
  1002. ret = dwc3_send_gadget_ep_cmd(dwc, dep->number,
  1003. DWC3_DEPCMD_SETSTALL, &params);
  1004. if (ret)
  1005. dev_err(dwc->dev, "failed to %s STALL on %s\n",
  1006. value ? "set" : "clear",
  1007. dep->name);
  1008. else
  1009. dep->flags |= DWC3_EP_STALL;
  1010. } else {
  1011. if (dep->flags & DWC3_EP_WEDGE)
  1012. return 0;
  1013. ret = dwc3_send_gadget_ep_cmd(dwc, dep->number,
  1014. DWC3_DEPCMD_CLEARSTALL, &params);
  1015. if (ret)
  1016. dev_err(dwc->dev, "failed to %s STALL on %s\n",
  1017. value ? "set" : "clear",
  1018. dep->name);
  1019. else
  1020. dep->flags &= ~DWC3_EP_STALL;
  1021. }
  1022. return ret;
  1023. }
  1024. static int dwc3_gadget_ep_set_halt(struct usb_ep *ep, int value)
  1025. {
  1026. struct dwc3_ep *dep = to_dwc3_ep(ep);
  1027. struct dwc3 *dwc = dep->dwc;
  1028. unsigned long flags;
  1029. int ret;
  1030. spin_lock_irqsave(&dwc->lock, flags);
  1031. if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
  1032. dev_err(dwc->dev, "%s is of Isochronous type\n", dep->name);
  1033. ret = -EINVAL;
  1034. goto out;
  1035. }
  1036. ret = __dwc3_gadget_ep_set_halt(dep, value);
  1037. out:
  1038. spin_unlock_irqrestore(&dwc->lock, flags);
  1039. return ret;
  1040. }
  1041. static int dwc3_gadget_ep_set_wedge(struct usb_ep *ep)
  1042. {
  1043. struct dwc3_ep *dep = to_dwc3_ep(ep);
  1044. struct dwc3 *dwc = dep->dwc;
  1045. unsigned long flags;
  1046. spin_lock_irqsave(&dwc->lock, flags);
  1047. dep->flags |= DWC3_EP_WEDGE;
  1048. spin_unlock_irqrestore(&dwc->lock, flags);
  1049. if (dep->number == 0 || dep->number == 1)
  1050. return dwc3_gadget_ep0_set_halt(ep, 1);
  1051. else
  1052. return dwc3_gadget_ep_set_halt(ep, 1);
  1053. }
  1054. /* -------------------------------------------------------------------------- */
  1055. static struct usb_endpoint_descriptor dwc3_gadget_ep0_desc = {
  1056. .bLength = USB_DT_ENDPOINT_SIZE,
  1057. .bDescriptorType = USB_DT_ENDPOINT,
  1058. .bmAttributes = USB_ENDPOINT_XFER_CONTROL,
  1059. };
  1060. static const struct usb_ep_ops dwc3_gadget_ep0_ops = {
  1061. .enable = dwc3_gadget_ep0_enable,
  1062. .disable = dwc3_gadget_ep0_disable,
  1063. .alloc_request = dwc3_gadget_ep_alloc_request,
  1064. .free_request = dwc3_gadget_ep_free_request,
  1065. .queue = dwc3_gadget_ep0_queue,
  1066. .dequeue = dwc3_gadget_ep_dequeue,
  1067. .set_halt = dwc3_gadget_ep0_set_halt,
  1068. .set_wedge = dwc3_gadget_ep_set_wedge,
  1069. };
  1070. static const struct usb_ep_ops dwc3_gadget_ep_ops = {
  1071. .enable = dwc3_gadget_ep_enable,
  1072. .disable = dwc3_gadget_ep_disable,
  1073. .alloc_request = dwc3_gadget_ep_alloc_request,
  1074. .free_request = dwc3_gadget_ep_free_request,
  1075. .queue = dwc3_gadget_ep_queue,
  1076. .dequeue = dwc3_gadget_ep_dequeue,
  1077. .set_halt = dwc3_gadget_ep_set_halt,
  1078. .set_wedge = dwc3_gadget_ep_set_wedge,
  1079. };
  1080. /* -------------------------------------------------------------------------- */
  1081. static int dwc3_gadget_get_frame(struct usb_gadget *g)
  1082. {
  1083. struct dwc3 *dwc = gadget_to_dwc(g);
  1084. u32 reg;
  1085. reg = dwc3_readl(dwc->regs, DWC3_DSTS);
  1086. return DWC3_DSTS_SOFFN(reg);
  1087. }
  1088. static int dwc3_gadget_wakeup(struct usb_gadget *g)
  1089. {
  1090. struct dwc3 *dwc = gadget_to_dwc(g);
  1091. unsigned long timeout;
  1092. unsigned long flags;
  1093. u32 reg;
  1094. int ret = 0;
  1095. u8 link_state;
  1096. u8 speed;
  1097. spin_lock_irqsave(&dwc->lock, flags);
  1098. /*
  1099. * According to the Databook Remote wakeup request should
  1100. * be issued only when the device is in early suspend state.
  1101. *
  1102. * We can check that via USB Link State bits in DSTS register.
  1103. */
  1104. reg = dwc3_readl(dwc->regs, DWC3_DSTS);
  1105. speed = reg & DWC3_DSTS_CONNECTSPD;
  1106. if (speed == DWC3_DSTS_SUPERSPEED) {
  1107. dev_dbg(dwc->dev, "no wakeup on SuperSpeed\n");
  1108. ret = -EINVAL;
  1109. goto out;
  1110. }
  1111. link_state = DWC3_DSTS_USBLNKST(reg);
  1112. switch (link_state) {
  1113. case DWC3_LINK_STATE_RX_DET: /* in HS, means Early Suspend */
  1114. case DWC3_LINK_STATE_U3: /* in HS, means SUSPEND */
  1115. break;
  1116. default:
  1117. dev_dbg(dwc->dev, "can't wakeup from link state %d\n",
  1118. link_state);
  1119. ret = -EINVAL;
  1120. goto out;
  1121. }
  1122. ret = dwc3_gadget_set_link_state(dwc, DWC3_LINK_STATE_RECOV);
  1123. if (ret < 0) {
  1124. dev_err(dwc->dev, "failed to put link in Recovery\n");
  1125. goto out;
  1126. }
  1127. /* Recent versions do this automatically */
  1128. if (dwc->revision < DWC3_REVISION_194A) {
  1129. /* write zeroes to Link Change Request */
  1130. reg = dwc3_readl(dwc->regs, DWC3_DCTL);
  1131. reg &= ~DWC3_DCTL_ULSTCHNGREQ_MASK;
  1132. dwc3_writel(dwc->regs, DWC3_DCTL, reg);
  1133. }
  1134. /* poll until Link State changes to ON */
  1135. timeout = jiffies + msecs_to_jiffies(100);
  1136. while (!time_after(jiffies, timeout)) {
  1137. reg = dwc3_readl(dwc->regs, DWC3_DSTS);
  1138. /* in HS, means ON */
  1139. if (DWC3_DSTS_USBLNKST(reg) == DWC3_LINK_STATE_U0)
  1140. break;
  1141. }
  1142. if (DWC3_DSTS_USBLNKST(reg) != DWC3_LINK_STATE_U0) {
  1143. dev_err(dwc->dev, "failed to send remote wakeup\n");
  1144. ret = -EINVAL;
  1145. }
  1146. out:
  1147. spin_unlock_irqrestore(&dwc->lock, flags);
  1148. return ret;
  1149. }
  1150. static int dwc3_gadget_set_selfpowered(struct usb_gadget *g,
  1151. int is_selfpowered)
  1152. {
  1153. struct dwc3 *dwc = gadget_to_dwc(g);
  1154. unsigned long flags;
  1155. spin_lock_irqsave(&dwc->lock, flags);
  1156. dwc->is_selfpowered = !!is_selfpowered;
  1157. spin_unlock_irqrestore(&dwc->lock, flags);
  1158. return 0;
  1159. }
  1160. static int dwc3_gadget_run_stop(struct dwc3 *dwc, int is_on)
  1161. {
  1162. u32 reg;
  1163. u32 timeout = 500;
  1164. reg = dwc3_readl(dwc->regs, DWC3_DCTL);
  1165. if (is_on) {
  1166. if (dwc->revision <= DWC3_REVISION_187A) {
  1167. reg &= ~DWC3_DCTL_TRGTULST_MASK;
  1168. reg |= DWC3_DCTL_TRGTULST_RX_DET;
  1169. }
  1170. if (dwc->revision >= DWC3_REVISION_194A)
  1171. reg &= ~DWC3_DCTL_KEEP_CONNECT;
  1172. reg |= DWC3_DCTL_RUN_STOP;
  1173. } else {
  1174. reg &= ~DWC3_DCTL_RUN_STOP;
  1175. }
  1176. dwc3_writel(dwc->regs, DWC3_DCTL, reg);
  1177. do {
  1178. reg = dwc3_readl(dwc->regs, DWC3_DSTS);
  1179. if (is_on) {
  1180. if (!(reg & DWC3_DSTS_DEVCTRLHLT))
  1181. break;
  1182. } else {
  1183. if (reg & DWC3_DSTS_DEVCTRLHLT)
  1184. break;
  1185. }
  1186. timeout--;
  1187. if (!timeout)
  1188. return -ETIMEDOUT;
  1189. udelay(1);
  1190. } while (1);
  1191. dev_vdbg(dwc->dev, "gadget %s data soft-%s\n",
  1192. dwc->gadget_driver
  1193. ? dwc->gadget_driver->function : "no-function",
  1194. is_on ? "connect" : "disconnect");
  1195. return 0;
  1196. }
  1197. static int dwc3_gadget_pullup(struct usb_gadget *g, int is_on)
  1198. {
  1199. struct dwc3 *dwc = gadget_to_dwc(g);
  1200. unsigned long flags;
  1201. int ret;
  1202. is_on = !!is_on;
  1203. spin_lock_irqsave(&dwc->lock, flags);
  1204. ret = dwc3_gadget_run_stop(dwc, is_on);
  1205. spin_unlock_irqrestore(&dwc->lock, flags);
  1206. return ret;
  1207. }
  1208. static int dwc3_gadget_start(struct usb_gadget *g,
  1209. struct usb_gadget_driver *driver)
  1210. {
  1211. struct dwc3 *dwc = gadget_to_dwc(g);
  1212. struct dwc3_ep *dep;
  1213. unsigned long flags;
  1214. int ret = 0;
  1215. u32 reg;
  1216. spin_lock_irqsave(&dwc->lock, flags);
  1217. if (dwc->gadget_driver) {
  1218. dev_err(dwc->dev, "%s is already bound to %s\n",
  1219. dwc->gadget.name,
  1220. dwc->gadget_driver->driver.name);
  1221. ret = -EBUSY;
  1222. goto err0;
  1223. }
  1224. dwc->gadget_driver = driver;
  1225. dwc->gadget.dev.driver = &driver->driver;
  1226. reg = dwc3_readl(dwc->regs, DWC3_DCFG);
  1227. reg &= ~(DWC3_DCFG_SPEED_MASK);
  1228. /**
  1229. * WORKAROUND: DWC3 revision < 2.20a have an issue
  1230. * which would cause metastability state on Run/Stop
  1231. * bit if we try to force the IP to USB2-only mode.
  1232. *
  1233. * Because of that, we cannot configure the IP to any
  1234. * speed other than the SuperSpeed
  1235. *
  1236. * Refers to:
  1237. *
  1238. * STAR#9000525659: Clock Domain Crossing on DCTL in
  1239. * USB 2.0 Mode
  1240. */
  1241. if (dwc->revision < DWC3_REVISION_220A)
  1242. reg |= DWC3_DCFG_SUPERSPEED;
  1243. else
  1244. reg |= dwc->maximum_speed;
  1245. dwc3_writel(dwc->regs, DWC3_DCFG, reg);
  1246. dwc->start_config_issued = false;
  1247. /* Start with SuperSpeed Default */
  1248. dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512);
  1249. dep = dwc->eps[0];
  1250. ret = __dwc3_gadget_ep_enable(dep, &dwc3_gadget_ep0_desc, NULL, false);
  1251. if (ret) {
  1252. dev_err(dwc->dev, "failed to enable %s\n", dep->name);
  1253. goto err0;
  1254. }
  1255. dep = dwc->eps[1];
  1256. ret = __dwc3_gadget_ep_enable(dep, &dwc3_gadget_ep0_desc, NULL, false);
  1257. if (ret) {
  1258. dev_err(dwc->dev, "failed to enable %s\n", dep->name);
  1259. goto err1;
  1260. }
  1261. /* begin to receive SETUP packets */
  1262. dwc->ep0state = EP0_SETUP_PHASE;
  1263. dwc3_ep0_out_start(dwc);
  1264. spin_unlock_irqrestore(&dwc->lock, flags);
  1265. return 0;
  1266. err1:
  1267. __dwc3_gadget_ep_disable(dwc->eps[0]);
  1268. err0:
  1269. spin_unlock_irqrestore(&dwc->lock, flags);
  1270. return ret;
  1271. }
  1272. static int dwc3_gadget_stop(struct usb_gadget *g,
  1273. struct usb_gadget_driver *driver)
  1274. {
  1275. struct dwc3 *dwc = gadget_to_dwc(g);
  1276. unsigned long flags;
  1277. spin_lock_irqsave(&dwc->lock, flags);
  1278. __dwc3_gadget_ep_disable(dwc->eps[0]);
  1279. __dwc3_gadget_ep_disable(dwc->eps[1]);
  1280. dwc->gadget_driver = NULL;
  1281. dwc->gadget.dev.driver = NULL;
  1282. spin_unlock_irqrestore(&dwc->lock, flags);
  1283. return 0;
  1284. }
  1285. static const struct usb_gadget_ops dwc3_gadget_ops = {
  1286. .get_frame = dwc3_gadget_get_frame,
  1287. .wakeup = dwc3_gadget_wakeup,
  1288. .set_selfpowered = dwc3_gadget_set_selfpowered,
  1289. .pullup = dwc3_gadget_pullup,
  1290. .udc_start = dwc3_gadget_start,
  1291. .udc_stop = dwc3_gadget_stop,
  1292. };
  1293. /* -------------------------------------------------------------------------- */
  1294. static int __devinit dwc3_gadget_init_endpoints(struct dwc3 *dwc)
  1295. {
  1296. struct dwc3_ep *dep;
  1297. u8 epnum;
  1298. INIT_LIST_HEAD(&dwc->gadget.ep_list);
  1299. for (epnum = 0; epnum < DWC3_ENDPOINTS_NUM; epnum++) {
  1300. dep = kzalloc(sizeof(*dep), GFP_KERNEL);
  1301. if (!dep) {
  1302. dev_err(dwc->dev, "can't allocate endpoint %d\n",
  1303. epnum);
  1304. return -ENOMEM;
  1305. }
  1306. dep->dwc = dwc;
  1307. dep->number = epnum;
  1308. dwc->eps[epnum] = dep;
  1309. snprintf(dep->name, sizeof(dep->name), "ep%d%s", epnum >> 1,
  1310. (epnum & 1) ? "in" : "out");
  1311. dep->endpoint.name = dep->name;
  1312. dep->direction = (epnum & 1);
  1313. if (epnum == 0 || epnum == 1) {
  1314. dep->endpoint.maxpacket = 512;
  1315. dep->endpoint.ops = &dwc3_gadget_ep0_ops;
  1316. if (!epnum)
  1317. dwc->gadget.ep0 = &dep->endpoint;
  1318. } else {
  1319. int ret;
  1320. dep->endpoint.maxpacket = 1024;
  1321. dep->endpoint.max_streams = 15;
  1322. dep->endpoint.ops = &dwc3_gadget_ep_ops;
  1323. list_add_tail(&dep->endpoint.ep_list,
  1324. &dwc->gadget.ep_list);
  1325. ret = dwc3_alloc_trb_pool(dep);
  1326. if (ret)
  1327. return ret;
  1328. }
  1329. INIT_LIST_HEAD(&dep->request_list);
  1330. INIT_LIST_HEAD(&dep->req_queued);
  1331. }
  1332. return 0;
  1333. }
  1334. static void dwc3_gadget_free_endpoints(struct dwc3 *dwc)
  1335. {
  1336. struct dwc3_ep *dep;
  1337. u8 epnum;
  1338. for (epnum = 0; epnum < DWC3_ENDPOINTS_NUM; epnum++) {
  1339. dep = dwc->eps[epnum];
  1340. dwc3_free_trb_pool(dep);
  1341. if (epnum != 0 && epnum != 1)
  1342. list_del(&dep->endpoint.ep_list);
  1343. kfree(dep);
  1344. }
  1345. }
  1346. static void dwc3_gadget_release(struct device *dev)
  1347. {
  1348. dev_dbg(dev, "%s\n", __func__);
  1349. }
  1350. /* -------------------------------------------------------------------------- */
  1351. static int dwc3_cleanup_done_reqs(struct dwc3 *dwc, struct dwc3_ep *dep,
  1352. const struct dwc3_event_depevt *event, int status)
  1353. {
  1354. struct dwc3_request *req;
  1355. struct dwc3_trb *trb;
  1356. unsigned int count;
  1357. unsigned int s_pkt = 0;
  1358. unsigned int trb_status;
  1359. do {
  1360. req = next_request(&dep->req_queued);
  1361. if (!req) {
  1362. WARN_ON_ONCE(1);
  1363. return 1;
  1364. }
  1365. trb = req->trb;
  1366. if ((trb->ctrl & DWC3_TRB_CTRL_HWO) && status != -ESHUTDOWN)
  1367. /*
  1368. * We continue despite the error. There is not much we
  1369. * can do. If we don't clean it up we loop forever. If
  1370. * we skip the TRB then it gets overwritten after a
  1371. * while since we use them in a ring buffer. A BUG()
  1372. * would help. Lets hope that if this occurs, someone
  1373. * fixes the root cause instead of looking away :)
  1374. */
  1375. dev_err(dwc->dev, "%s's TRB (%p) still owned by HW\n",
  1376. dep->name, req->trb);
  1377. count = trb->size & DWC3_TRB_SIZE_MASK;
  1378. if (dep->direction) {
  1379. if (count) {
  1380. trb_status = DWC3_TRB_SIZE_TRBSTS(trb->size);
  1381. if (trb_status == DWC3_TRBSTS_MISSED_ISOC) {
  1382. dev_dbg(dwc->dev, "incomplete IN transfer %s\n",
  1383. dep->name);
  1384. dep->current_uf = event->parameters &
  1385. ~(dep->interval - 1);
  1386. dep->flags |= DWC3_EP_MISSED_ISOC;
  1387. } else {
  1388. dev_err(dwc->dev, "incomplete IN transfer %s\n",
  1389. dep->name);
  1390. status = -ECONNRESET;
  1391. }
  1392. }
  1393. } else {
  1394. if (count && (event->status & DEPEVT_STATUS_SHORT))
  1395. s_pkt = 1;
  1396. }
  1397. /*
  1398. * We assume here we will always receive the entire data block
  1399. * which we should receive. Meaning, if we program RX to
  1400. * receive 4K but we receive only 2K, we assume that's all we
  1401. * should receive and we simply bounce the request back to the
  1402. * gadget driver for further processing.
  1403. */
  1404. req->request.actual += req->request.length - count;
  1405. dwc3_gadget_giveback(dep, req, status);
  1406. if (s_pkt)
  1407. break;
  1408. if ((event->status & DEPEVT_STATUS_LST) &&
  1409. (trb->ctrl & (DWC3_TRB_CTRL_LST |
  1410. DWC3_TRB_CTRL_HWO)))
  1411. break;
  1412. if ((event->status & DEPEVT_STATUS_IOC) &&
  1413. (trb->ctrl & DWC3_TRB_CTRL_IOC))
  1414. break;
  1415. } while (1);
  1416. if ((event->status & DEPEVT_STATUS_IOC) &&
  1417. (trb->ctrl & DWC3_TRB_CTRL_IOC))
  1418. return 0;
  1419. return 1;
  1420. }
  1421. static void dwc3_endpoint_transfer_complete(struct dwc3 *dwc,
  1422. struct dwc3_ep *dep, const struct dwc3_event_depevt *event,
  1423. int start_new)
  1424. {
  1425. unsigned status = 0;
  1426. int clean_busy;
  1427. if (event->status & DEPEVT_STATUS_BUSERR)
  1428. status = -ECONNRESET;
  1429. clean_busy = dwc3_cleanup_done_reqs(dwc, dep, event, status);
  1430. if (clean_busy)
  1431. dep->flags &= ~DWC3_EP_BUSY;
  1432. /*
  1433. * WORKAROUND: This is the 2nd half of U1/U2 -> U0 workaround.
  1434. * See dwc3_gadget_linksts_change_interrupt() for 1st half.
  1435. */
  1436. if (dwc->revision < DWC3_REVISION_183A) {
  1437. u32 reg;
  1438. int i;
  1439. for (i = 0; i < DWC3_ENDPOINTS_NUM; i++) {
  1440. struct dwc3_ep *dep = dwc->eps[i];
  1441. if (!(dep->flags & DWC3_EP_ENABLED))
  1442. continue;
  1443. if (!list_empty(&dep->req_queued))
  1444. return;
  1445. }
  1446. reg = dwc3_readl(dwc->regs, DWC3_DCTL);
  1447. reg |= dwc->u1u2;
  1448. dwc3_writel(dwc->regs, DWC3_DCTL, reg);
  1449. dwc->u1u2 = 0;
  1450. }
  1451. }
  1452. static void dwc3_endpoint_interrupt(struct dwc3 *dwc,
  1453. const struct dwc3_event_depevt *event)
  1454. {
  1455. struct dwc3_ep *dep;
  1456. u8 epnum = event->endpoint_number;
  1457. dep = dwc->eps[epnum];
  1458. if (!(dep->flags & DWC3_EP_ENABLED))
  1459. return;
  1460. dev_vdbg(dwc->dev, "%s: %s\n", dep->name,
  1461. dwc3_ep_event_string(event->endpoint_event));
  1462. if (epnum == 0 || epnum == 1) {
  1463. dwc3_ep0_interrupt(dwc, event);
  1464. return;
  1465. }
  1466. switch (event->endpoint_event) {
  1467. case DWC3_DEPEVT_XFERCOMPLETE:
  1468. dep->resource_index = 0;
  1469. if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
  1470. dev_dbg(dwc->dev, "%s is an Isochronous endpoint\n",
  1471. dep->name);
  1472. return;
  1473. }
  1474. dwc3_endpoint_transfer_complete(dwc, dep, event, 1);
  1475. break;
  1476. case DWC3_DEPEVT_XFERINPROGRESS:
  1477. if (!usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
  1478. dev_dbg(dwc->dev, "%s is not an Isochronous endpoint\n",
  1479. dep->name);
  1480. return;
  1481. }
  1482. dwc3_endpoint_transfer_complete(dwc, dep, event, 0);
  1483. break;
  1484. case DWC3_DEPEVT_XFERNOTREADY:
  1485. if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
  1486. dwc3_gadget_start_isoc(dwc, dep, event);
  1487. } else {
  1488. int ret;
  1489. dev_vdbg(dwc->dev, "%s: reason %s\n",
  1490. dep->name, event->status &
  1491. DEPEVT_STATUS_TRANSFER_ACTIVE
  1492. ? "Transfer Active"
  1493. : "Transfer Not Active");
  1494. ret = __dwc3_gadget_kick_transfer(dep, 0, 1);
  1495. if (!ret || ret == -EBUSY)
  1496. return;
  1497. dev_dbg(dwc->dev, "%s: failed to kick transfers\n",
  1498. dep->name);
  1499. }
  1500. break;
  1501. case DWC3_DEPEVT_STREAMEVT:
  1502. if (!usb_endpoint_xfer_bulk(dep->endpoint.desc)) {
  1503. dev_err(dwc->dev, "Stream event for non-Bulk %s\n",
  1504. dep->name);
  1505. return;
  1506. }
  1507. switch (event->status) {
  1508. case DEPEVT_STREAMEVT_FOUND:
  1509. dev_vdbg(dwc->dev, "Stream %d found and started\n",
  1510. event->parameters);
  1511. break;
  1512. case DEPEVT_STREAMEVT_NOTFOUND:
  1513. /* FALLTHROUGH */
  1514. default:
  1515. dev_dbg(dwc->dev, "Couldn't find suitable stream\n");
  1516. }
  1517. break;
  1518. case DWC3_DEPEVT_RXTXFIFOEVT:
  1519. dev_dbg(dwc->dev, "%s FIFO Overrun\n", dep->name);
  1520. break;
  1521. case DWC3_DEPEVT_EPCMDCMPLT:
  1522. dev_vdbg(dwc->dev, "Endpoint Command Complete\n");
  1523. break;
  1524. }
  1525. }
  1526. static void dwc3_disconnect_gadget(struct dwc3 *dwc)
  1527. {
  1528. if (dwc->gadget_driver && dwc->gadget_driver->disconnect) {
  1529. spin_unlock(&dwc->lock);
  1530. dwc->gadget_driver->disconnect(&dwc->gadget);
  1531. spin_lock(&dwc->lock);
  1532. }
  1533. }
  1534. static void dwc3_stop_active_transfer(struct dwc3 *dwc, u32 epnum)
  1535. {
  1536. struct dwc3_ep *dep;
  1537. struct dwc3_gadget_ep_cmd_params params;
  1538. u32 cmd;
  1539. int ret;
  1540. dep = dwc->eps[epnum];
  1541. if (!dep->resource_index)
  1542. return;
  1543. cmd = DWC3_DEPCMD_ENDTRANSFER;
  1544. cmd |= DWC3_DEPCMD_HIPRI_FORCERM | DWC3_DEPCMD_CMDIOC;
  1545. cmd |= DWC3_DEPCMD_PARAM(dep->resource_index);
  1546. memset(&params, 0, sizeof(params));
  1547. ret = dwc3_send_gadget_ep_cmd(dwc, dep->number, cmd, &params);
  1548. WARN_ON_ONCE(ret);
  1549. dep->resource_index = 0;
  1550. }
  1551. static void dwc3_stop_active_transfers(struct dwc3 *dwc)
  1552. {
  1553. u32 epnum;
  1554. for (epnum = 2; epnum < DWC3_ENDPOINTS_NUM; epnum++) {
  1555. struct dwc3_ep *dep;
  1556. dep = dwc->eps[epnum];
  1557. if (!(dep->flags & DWC3_EP_ENABLED))
  1558. continue;
  1559. dwc3_remove_requests(dwc, dep);
  1560. }
  1561. }
  1562. static void dwc3_clear_stall_all_ep(struct dwc3 *dwc)
  1563. {
  1564. u32 epnum;
  1565. for (epnum = 1; epnum < DWC3_ENDPOINTS_NUM; epnum++) {
  1566. struct dwc3_ep *dep;
  1567. struct dwc3_gadget_ep_cmd_params params;
  1568. int ret;
  1569. dep = dwc->eps[epnum];
  1570. if (!(dep->flags & DWC3_EP_STALL))
  1571. continue;
  1572. dep->flags &= ~DWC3_EP_STALL;
  1573. memset(&params, 0, sizeof(params));
  1574. ret = dwc3_send_gadget_ep_cmd(dwc, dep->number,
  1575. DWC3_DEPCMD_CLEARSTALL, &params);
  1576. WARN_ON_ONCE(ret);
  1577. }
  1578. }
  1579. static void dwc3_gadget_disconnect_interrupt(struct dwc3 *dwc)
  1580. {
  1581. int reg;
  1582. dev_vdbg(dwc->dev, "%s\n", __func__);
  1583. reg = dwc3_readl(dwc->regs, DWC3_DCTL);
  1584. reg &= ~DWC3_DCTL_INITU1ENA;
  1585. dwc3_writel(dwc->regs, DWC3_DCTL, reg);
  1586. reg &= ~DWC3_DCTL_INITU2ENA;
  1587. dwc3_writel(dwc->regs, DWC3_DCTL, reg);
  1588. dwc3_disconnect_gadget(dwc);
  1589. dwc->start_config_issued = false;
  1590. dwc->gadget.speed = USB_SPEED_UNKNOWN;
  1591. dwc->setup_packet_pending = false;
  1592. }
  1593. static void dwc3_gadget_usb3_phy_suspend(struct dwc3 *dwc, int suspend)
  1594. {
  1595. u32 reg;
  1596. reg = dwc3_readl(dwc->regs, DWC3_GUSB3PIPECTL(0));
  1597. if (suspend)
  1598. reg |= DWC3_GUSB3PIPECTL_SUSPHY;
  1599. else
  1600. reg &= ~DWC3_GUSB3PIPECTL_SUSPHY;
  1601. dwc3_writel(dwc->regs, DWC3_GUSB3PIPECTL(0), reg);
  1602. }
  1603. static void dwc3_gadget_usb2_phy_suspend(struct dwc3 *dwc, int suspend)
  1604. {
  1605. u32 reg;
  1606. reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0));
  1607. if (suspend)
  1608. reg |= DWC3_GUSB2PHYCFG_SUSPHY;
  1609. else
  1610. reg &= ~DWC3_GUSB2PHYCFG_SUSPHY;
  1611. dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg);
  1612. }
  1613. static void dwc3_gadget_reset_interrupt(struct dwc3 *dwc)
  1614. {
  1615. u32 reg;
  1616. dev_vdbg(dwc->dev, "%s\n", __func__);
  1617. /*
  1618. * WORKAROUND: DWC3 revisions <1.88a have an issue which
  1619. * would cause a missing Disconnect Event if there's a
  1620. * pending Setup Packet in the FIFO.
  1621. *
  1622. * There's no suggested workaround on the official Bug
  1623. * report, which states that "unless the driver/application
  1624. * is doing any special handling of a disconnect event,
  1625. * there is no functional issue".
  1626. *
  1627. * Unfortunately, it turns out that we _do_ some special
  1628. * handling of a disconnect event, namely complete all
  1629. * pending transfers, notify gadget driver of the
  1630. * disconnection, and so on.
  1631. *
  1632. * Our suggested workaround is to follow the Disconnect
  1633. * Event steps here, instead, based on a setup_packet_pending
  1634. * flag. Such flag gets set whenever we have a XferNotReady
  1635. * event on EP0 and gets cleared on XferComplete for the
  1636. * same endpoint.
  1637. *
  1638. * Refers to:
  1639. *
  1640. * STAR#9000466709: RTL: Device : Disconnect event not
  1641. * generated if setup packet pending in FIFO
  1642. */
  1643. if (dwc->revision < DWC3_REVISION_188A) {
  1644. if (dwc->setup_packet_pending)
  1645. dwc3_gadget_disconnect_interrupt(dwc);
  1646. }
  1647. /* after reset -> Default State */
  1648. dwc->dev_state = DWC3_DEFAULT_STATE;
  1649. /* Recent versions support automatic phy suspend and don't need this */
  1650. if (dwc->revision < DWC3_REVISION_194A) {
  1651. /* Resume PHYs */
  1652. dwc3_gadget_usb2_phy_suspend(dwc, false);
  1653. dwc3_gadget_usb3_phy_suspend(dwc, false);
  1654. }
  1655. if (dwc->gadget.speed != USB_SPEED_UNKNOWN)
  1656. dwc3_disconnect_gadget(dwc);
  1657. reg = dwc3_readl(dwc->regs, DWC3_DCTL);
  1658. reg &= ~DWC3_DCTL_TSTCTRL_MASK;
  1659. dwc3_writel(dwc->regs, DWC3_DCTL, reg);
  1660. dwc->test_mode = false;
  1661. dwc3_stop_active_transfers(dwc);
  1662. dwc3_clear_stall_all_ep(dwc);
  1663. dwc->start_config_issued = false;
  1664. /* Reset device address to zero */
  1665. reg = dwc3_readl(dwc->regs, DWC3_DCFG);
  1666. reg &= ~(DWC3_DCFG_DEVADDR_MASK);
  1667. dwc3_writel(dwc->regs, DWC3_DCFG, reg);
  1668. }
  1669. static void dwc3_update_ram_clk_sel(struct dwc3 *dwc, u32 speed)
  1670. {
  1671. u32 reg;
  1672. u32 usb30_clock = DWC3_GCTL_CLK_BUS;
  1673. /*
  1674. * We change the clock only at SS but I dunno why I would want to do
  1675. * this. Maybe it becomes part of the power saving plan.
  1676. */
  1677. if (speed != DWC3_DSTS_SUPERSPEED)
  1678. return;
  1679. /*
  1680. * RAMClkSel is reset to 0 after USB reset, so it must be reprogrammed
  1681. * each time on Connect Done.
  1682. */
  1683. if (!usb30_clock)
  1684. return;
  1685. reg = dwc3_readl(dwc->regs, DWC3_GCTL);
  1686. reg |= DWC3_GCTL_RAMCLKSEL(usb30_clock);
  1687. dwc3_writel(dwc->regs, DWC3_GCTL, reg);
  1688. }
  1689. static void dwc3_gadget_phy_suspend(struct dwc3 *dwc, u8 speed)
  1690. {
  1691. switch (speed) {
  1692. case USB_SPEED_SUPER:
  1693. dwc3_gadget_usb2_phy_suspend(dwc, true);
  1694. break;
  1695. case USB_SPEED_HIGH:
  1696. case USB_SPEED_FULL:
  1697. case USB_SPEED_LOW:
  1698. dwc3_gadget_usb3_phy_suspend(dwc, true);
  1699. break;
  1700. }
  1701. }
  1702. static void dwc3_gadget_conndone_interrupt(struct dwc3 *dwc)
  1703. {
  1704. struct dwc3_gadget_ep_cmd_params params;
  1705. struct dwc3_ep *dep;
  1706. int ret;
  1707. u32 reg;
  1708. u8 speed;
  1709. dev_vdbg(dwc->dev, "%s\n", __func__);
  1710. memset(&params, 0x00, sizeof(params));
  1711. reg = dwc3_readl(dwc->regs, DWC3_DSTS);
  1712. speed = reg & DWC3_DSTS_CONNECTSPD;
  1713. dwc->speed = speed;
  1714. dwc3_update_ram_clk_sel(dwc, speed);
  1715. switch (speed) {
  1716. case DWC3_DCFG_SUPERSPEED:
  1717. /*
  1718. * WORKAROUND: DWC3 revisions <1.90a have an issue which
  1719. * would cause a missing USB3 Reset event.
  1720. *
  1721. * In such situations, we should force a USB3 Reset
  1722. * event by calling our dwc3_gadget_reset_interrupt()
  1723. * routine.
  1724. *
  1725. * Refers to:
  1726. *
  1727. * STAR#9000483510: RTL: SS : USB3 reset event may
  1728. * not be generated always when the link enters poll
  1729. */
  1730. if (dwc->revision < DWC3_REVISION_190A)
  1731. dwc3_gadget_reset_interrupt(dwc);
  1732. dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512);
  1733. dwc->gadget.ep0->maxpacket = 512;
  1734. dwc->gadget.speed = USB_SPEED_SUPER;
  1735. break;
  1736. case DWC3_DCFG_HIGHSPEED:
  1737. dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(64);
  1738. dwc->gadget.ep0->maxpacket = 64;
  1739. dwc->gadget.speed = USB_SPEED_HIGH;
  1740. break;
  1741. case DWC3_DCFG_FULLSPEED2:
  1742. case DWC3_DCFG_FULLSPEED1:
  1743. dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(64);
  1744. dwc->gadget.ep0->maxpacket = 64;
  1745. dwc->gadget.speed = USB_SPEED_FULL;
  1746. break;
  1747. case DWC3_DCFG_LOWSPEED:
  1748. dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(8);
  1749. dwc->gadget.ep0->maxpacket = 8;
  1750. dwc->gadget.speed = USB_SPEED_LOW;
  1751. break;
  1752. }
  1753. /* Recent versions support automatic phy suspend and don't need this */
  1754. if (dwc->revision < DWC3_REVISION_194A) {
  1755. /* Suspend unneeded PHY */
  1756. dwc3_gadget_phy_suspend(dwc, dwc->gadget.speed);
  1757. }
  1758. dep = dwc->eps[0];
  1759. ret = __dwc3_gadget_ep_enable(dep, &dwc3_gadget_ep0_desc, NULL, true);
  1760. if (ret) {
  1761. dev_err(dwc->dev, "failed to enable %s\n", dep->name);
  1762. return;
  1763. }
  1764. dep = dwc->eps[1];
  1765. ret = __dwc3_gadget_ep_enable(dep, &dwc3_gadget_ep0_desc, NULL, true);
  1766. if (ret) {
  1767. dev_err(dwc->dev, "failed to enable %s\n", dep->name);
  1768. return;
  1769. }
  1770. /*
  1771. * Configure PHY via GUSB3PIPECTLn if required.
  1772. *
  1773. * Update GTXFIFOSIZn
  1774. *
  1775. * In both cases reset values should be sufficient.
  1776. */
  1777. }
  1778. static void dwc3_gadget_wakeup_interrupt(struct dwc3 *dwc)
  1779. {
  1780. dev_vdbg(dwc->dev, "%s\n", __func__);
  1781. /*
  1782. * TODO take core out of low power mode when that's
  1783. * implemented.
  1784. */
  1785. dwc->gadget_driver->resume(&dwc->gadget);
  1786. }
  1787. static void dwc3_gadget_linksts_change_interrupt(struct dwc3 *dwc,
  1788. unsigned int evtinfo)
  1789. {
  1790. enum dwc3_link_state next = evtinfo & DWC3_LINK_STATE_MASK;
  1791. /*
  1792. * WORKAROUND: DWC3 Revisions <1.83a have an issue which, depending
  1793. * on the link partner, the USB session might do multiple entry/exit
  1794. * of low power states before a transfer takes place.
  1795. *
  1796. * Due to this problem, we might experience lower throughput. The
  1797. * suggested workaround is to disable DCTL[12:9] bits if we're
  1798. * transitioning from U1/U2 to U0 and enable those bits again
  1799. * after a transfer completes and there are no pending transfers
  1800. * on any of the enabled endpoints.
  1801. *
  1802. * This is the first half of that workaround.
  1803. *
  1804. * Refers to:
  1805. *
  1806. * STAR#9000446952: RTL: Device SS : if U1/U2 ->U0 takes >128us
  1807. * core send LGO_Ux entering U0
  1808. */
  1809. if (dwc->revision < DWC3_REVISION_183A) {
  1810. if (next == DWC3_LINK_STATE_U0) {
  1811. u32 u1u2;
  1812. u32 reg;
  1813. switch (dwc->link_state) {
  1814. case DWC3_LINK_STATE_U1:
  1815. case DWC3_LINK_STATE_U2:
  1816. reg = dwc3_readl(dwc->regs, DWC3_DCTL);
  1817. u1u2 = reg & (DWC3_DCTL_INITU2ENA
  1818. | DWC3_DCTL_ACCEPTU2ENA
  1819. | DWC3_DCTL_INITU1ENA
  1820. | DWC3_DCTL_ACCEPTU1ENA);
  1821. if (!dwc->u1u2)
  1822. dwc->u1u2 = reg & u1u2;
  1823. reg &= ~u1u2;
  1824. dwc3_writel(dwc->regs, DWC3_DCTL, reg);
  1825. break;
  1826. default:
  1827. /* do nothing */
  1828. break;
  1829. }
  1830. }
  1831. }
  1832. dwc->link_state = next;
  1833. dev_vdbg(dwc->dev, "%s link %d\n", __func__, dwc->link_state);
  1834. }
  1835. static void dwc3_gadget_interrupt(struct dwc3 *dwc,
  1836. const struct dwc3_event_devt *event)
  1837. {
  1838. switch (event->type) {
  1839. case DWC3_DEVICE_EVENT_DISCONNECT:
  1840. dwc3_gadget_disconnect_interrupt(dwc);
  1841. break;
  1842. case DWC3_DEVICE_EVENT_RESET:
  1843. dwc3_gadget_reset_interrupt(dwc);
  1844. break;
  1845. case DWC3_DEVICE_EVENT_CONNECT_DONE:
  1846. dwc3_gadget_conndone_interrupt(dwc);
  1847. break;
  1848. case DWC3_DEVICE_EVENT_WAKEUP:
  1849. dwc3_gadget_wakeup_interrupt(dwc);
  1850. break;
  1851. case DWC3_DEVICE_EVENT_LINK_STATUS_CHANGE:
  1852. dwc3_gadget_linksts_change_interrupt(dwc, event->event_info);
  1853. break;
  1854. case DWC3_DEVICE_EVENT_EOPF:
  1855. dev_vdbg(dwc->dev, "End of Periodic Frame\n");
  1856. break;
  1857. case DWC3_DEVICE_EVENT_SOF:
  1858. dev_vdbg(dwc->dev, "Start of Periodic Frame\n");
  1859. break;
  1860. case DWC3_DEVICE_EVENT_ERRATIC_ERROR:
  1861. dev_vdbg(dwc->dev, "Erratic Error\n");
  1862. break;
  1863. case DWC3_DEVICE_EVENT_CMD_CMPL:
  1864. dev_vdbg(dwc->dev, "Command Complete\n");
  1865. break;
  1866. case DWC3_DEVICE_EVENT_OVERFLOW:
  1867. dev_vdbg(dwc->dev, "Overflow\n");
  1868. break;
  1869. default:
  1870. dev_dbg(dwc->dev, "UNKNOWN IRQ %d\n", event->type);
  1871. }
  1872. }
  1873. static void dwc3_process_event_entry(struct dwc3 *dwc,
  1874. const union dwc3_event *event)
  1875. {
  1876. /* Endpoint IRQ, handle it and return early */
  1877. if (event->type.is_devspec == 0) {
  1878. /* depevt */
  1879. return dwc3_endpoint_interrupt(dwc, &event->depevt);
  1880. }
  1881. switch (event->type.type) {
  1882. case DWC3_EVENT_TYPE_DEV:
  1883. dwc3_gadget_interrupt(dwc, &event->devt);
  1884. break;
  1885. /* REVISIT what to do with Carkit and I2C events ? */
  1886. default:
  1887. dev_err(dwc->dev, "UNKNOWN IRQ type %d\n", event->raw);
  1888. }
  1889. }
  1890. static irqreturn_t dwc3_process_event_buf(struct dwc3 *dwc, u32 buf)
  1891. {
  1892. struct dwc3_event_buffer *evt;
  1893. int left;
  1894. u32 count;
  1895. count = dwc3_readl(dwc->regs, DWC3_GEVNTCOUNT(buf));
  1896. count &= DWC3_GEVNTCOUNT_MASK;
  1897. if (!count)
  1898. return IRQ_NONE;
  1899. evt = dwc->ev_buffs[buf];
  1900. left = count;
  1901. while (left > 0) {
  1902. union dwc3_event event;
  1903. event.raw = *(u32 *) (evt->buf + evt->lpos);
  1904. dwc3_process_event_entry(dwc, &event);
  1905. /*
  1906. * XXX we wrap around correctly to the next entry as almost all
  1907. * entries are 4 bytes in size. There is one entry which has 12
  1908. * bytes which is a regular entry followed by 8 bytes data. ATM
  1909. * I don't know how things are organized if were get next to the
  1910. * a boundary so I worry about that once we try to handle that.
  1911. */
  1912. evt->lpos = (evt->lpos + 4) % DWC3_EVENT_BUFFERS_SIZE;
  1913. left -= 4;
  1914. dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(buf), 4);
  1915. }
  1916. return IRQ_HANDLED;
  1917. }
  1918. static irqreturn_t dwc3_interrupt(int irq, void *_dwc)
  1919. {
  1920. struct dwc3 *dwc = _dwc;
  1921. int i;
  1922. irqreturn_t ret = IRQ_NONE;
  1923. spin_lock(&dwc->lock);
  1924. for (i = 0; i < dwc->num_event_buffers; i++) {
  1925. irqreturn_t status;
  1926. status = dwc3_process_event_buf(dwc, i);
  1927. if (status == IRQ_HANDLED)
  1928. ret = status;
  1929. }
  1930. spin_unlock(&dwc->lock);
  1931. return ret;
  1932. }
  1933. /**
  1934. * dwc3_gadget_init - Initializes gadget related registers
  1935. * @dwc: pointer to our controller context structure
  1936. *
  1937. * Returns 0 on success otherwise negative errno.
  1938. */
  1939. int __devinit dwc3_gadget_init(struct dwc3 *dwc)
  1940. {
  1941. u32 reg;
  1942. int ret;
  1943. int irq;
  1944. dwc->ctrl_req = dma_alloc_coherent(dwc->dev, sizeof(*dwc->ctrl_req),
  1945. &dwc->ctrl_req_addr, GFP_KERNEL);
  1946. if (!dwc->ctrl_req) {
  1947. dev_err(dwc->dev, "failed to allocate ctrl request\n");
  1948. ret = -ENOMEM;
  1949. goto err0;
  1950. }
  1951. dwc->ep0_trb = dma_alloc_coherent(dwc->dev, sizeof(*dwc->ep0_trb),
  1952. &dwc->ep0_trb_addr, GFP_KERNEL);
  1953. if (!dwc->ep0_trb) {
  1954. dev_err(dwc->dev, "failed to allocate ep0 trb\n");
  1955. ret = -ENOMEM;
  1956. goto err1;
  1957. }
  1958. dwc->setup_buf = kzalloc(DWC3_EP0_BOUNCE_SIZE, GFP_KERNEL);
  1959. if (!dwc->setup_buf) {
  1960. dev_err(dwc->dev, "failed to allocate setup buffer\n");
  1961. ret = -ENOMEM;
  1962. goto err2;
  1963. }
  1964. dwc->ep0_bounce = dma_alloc_coherent(dwc->dev,
  1965. DWC3_EP0_BOUNCE_SIZE, &dwc->ep0_bounce_addr,
  1966. GFP_KERNEL);
  1967. if (!dwc->ep0_bounce) {
  1968. dev_err(dwc->dev, "failed to allocate ep0 bounce buffer\n");
  1969. ret = -ENOMEM;
  1970. goto err3;
  1971. }
  1972. dev_set_name(&dwc->gadget.dev, "gadget");
  1973. dwc->gadget.ops = &dwc3_gadget_ops;
  1974. dwc->gadget.max_speed = USB_SPEED_SUPER;
  1975. dwc->gadget.speed = USB_SPEED_UNKNOWN;
  1976. dwc->gadget.dev.parent = dwc->dev;
  1977. dwc->gadget.sg_supported = true;
  1978. dma_set_coherent_mask(&dwc->gadget.dev, dwc->dev->coherent_dma_mask);
  1979. dwc->gadget.dev.dma_parms = dwc->dev->dma_parms;
  1980. dwc->gadget.dev.dma_mask = dwc->dev->dma_mask;
  1981. dwc->gadget.dev.release = dwc3_gadget_release;
  1982. dwc->gadget.name = "dwc3-gadget";
  1983. /*
  1984. * REVISIT: Here we should clear all pending IRQs to be
  1985. * sure we're starting from a well known location.
  1986. */
  1987. ret = dwc3_gadget_init_endpoints(dwc);
  1988. if (ret)
  1989. goto err4;
  1990. irq = platform_get_irq(to_platform_device(dwc->dev), 0);
  1991. ret = request_irq(irq, dwc3_interrupt, IRQF_SHARED,
  1992. "dwc3", dwc);
  1993. if (ret) {
  1994. dev_err(dwc->dev, "failed to request irq #%d --> %d\n",
  1995. irq, ret);
  1996. goto err5;
  1997. }
  1998. reg = dwc3_readl(dwc->regs, DWC3_DCFG);
  1999. reg |= DWC3_DCFG_LPM_CAP;
  2000. dwc3_writel(dwc->regs, DWC3_DCFG, reg);
  2001. /* Enable all but Start and End of Frame IRQs */
  2002. reg = (DWC3_DEVTEN_VNDRDEVTSTRCVEDEN |
  2003. DWC3_DEVTEN_EVNTOVERFLOWEN |
  2004. DWC3_DEVTEN_CMDCMPLTEN |
  2005. DWC3_DEVTEN_ERRTICERREN |
  2006. DWC3_DEVTEN_WKUPEVTEN |
  2007. DWC3_DEVTEN_ULSTCNGEN |
  2008. DWC3_DEVTEN_CONNECTDONEEN |
  2009. DWC3_DEVTEN_USBRSTEN |
  2010. DWC3_DEVTEN_DISCONNEVTEN);
  2011. dwc3_writel(dwc->regs, DWC3_DEVTEN, reg);
  2012. /* Enable USB2 LPM and automatic phy suspend only on recent versions */
  2013. if (dwc->revision >= DWC3_REVISION_194A) {
  2014. reg = dwc3_readl(dwc->regs, DWC3_DCFG);
  2015. reg |= DWC3_DCFG_LPM_CAP;
  2016. dwc3_writel(dwc->regs, DWC3_DCFG, reg);
  2017. reg = dwc3_readl(dwc->regs, DWC3_DCTL);
  2018. reg &= ~(DWC3_DCTL_HIRD_THRES_MASK | DWC3_DCTL_L1_HIBER_EN);
  2019. /* TODO: This should be configurable */
  2020. reg |= DWC3_DCTL_HIRD_THRES(28);
  2021. dwc3_writel(dwc->regs, DWC3_DCTL, reg);
  2022. dwc3_gadget_usb2_phy_suspend(dwc, false);
  2023. dwc3_gadget_usb3_phy_suspend(dwc, false);
  2024. }
  2025. ret = device_register(&dwc->gadget.dev);
  2026. if (ret) {
  2027. dev_err(dwc->dev, "failed to register gadget device\n");
  2028. put_device(&dwc->gadget.dev);
  2029. goto err6;
  2030. }
  2031. ret = usb_add_gadget_udc(dwc->dev, &dwc->gadget);
  2032. if (ret) {
  2033. dev_err(dwc->dev, "failed to register udc\n");
  2034. goto err7;
  2035. }
  2036. return 0;
  2037. err7:
  2038. device_unregister(&dwc->gadget.dev);
  2039. err6:
  2040. dwc3_writel(dwc->regs, DWC3_DEVTEN, 0x00);
  2041. free_irq(irq, dwc);
  2042. err5:
  2043. dwc3_gadget_free_endpoints(dwc);
  2044. err4:
  2045. dma_free_coherent(dwc->dev, DWC3_EP0_BOUNCE_SIZE,
  2046. dwc->ep0_bounce, dwc->ep0_bounce_addr);
  2047. err3:
  2048. kfree(dwc->setup_buf);
  2049. err2:
  2050. dma_free_coherent(dwc->dev, sizeof(*dwc->ep0_trb),
  2051. dwc->ep0_trb, dwc->ep0_trb_addr);
  2052. err1:
  2053. dma_free_coherent(dwc->dev, sizeof(*dwc->ctrl_req),
  2054. dwc->ctrl_req, dwc->ctrl_req_addr);
  2055. err0:
  2056. return ret;
  2057. }
  2058. void dwc3_gadget_exit(struct dwc3 *dwc)
  2059. {
  2060. int irq;
  2061. usb_del_gadget_udc(&dwc->gadget);
  2062. irq = platform_get_irq(to_platform_device(dwc->dev), 0);
  2063. dwc3_writel(dwc->regs, DWC3_DEVTEN, 0x00);
  2064. free_irq(irq, dwc);
  2065. dwc3_gadget_free_endpoints(dwc);
  2066. dma_free_coherent(dwc->dev, DWC3_EP0_BOUNCE_SIZE,
  2067. dwc->ep0_bounce, dwc->ep0_bounce_addr);
  2068. kfree(dwc->setup_buf);
  2069. dma_free_coherent(dwc->dev, sizeof(*dwc->ep0_trb),
  2070. dwc->ep0_trb, dwc->ep0_trb_addr);
  2071. dma_free_coherent(dwc->dev, sizeof(*dwc->ctrl_req),
  2072. dwc->ctrl_req, dwc->ctrl_req_addr);
  2073. device_unregister(&dwc->gadget.dev);
  2074. }