gadget.c 58 KB

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