gadget.c 59 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422
  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. /**
  1109. * WORKAROUND: DWC3 revision < 2.20a have an issue
  1110. * which would cause metastability state on Run/Stop
  1111. * bit if we try to force the IP to USB2-only mode.
  1112. *
  1113. * Because of that, we cannot configure the IP to any
  1114. * speed other than the SuperSpeed
  1115. *
  1116. * Refers to:
  1117. *
  1118. * STAR#9000525659: Clock Domain Crossing on DCTL in
  1119. * USB 2.0 Mode
  1120. */
  1121. if (dwc->revision < DWC3_REVISION_220A)
  1122. reg |= DWC3_DCFG_SUPERSPEED;
  1123. else
  1124. reg |= dwc->maximum_speed;
  1125. dwc3_writel(dwc->regs, DWC3_DCFG, reg);
  1126. dwc->start_config_issued = false;
  1127. /* Start with SuperSpeed Default */
  1128. dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512);
  1129. dep = dwc->eps[0];
  1130. ret = __dwc3_gadget_ep_enable(dep, &dwc3_gadget_ep0_desc, NULL);
  1131. if (ret) {
  1132. dev_err(dwc->dev, "failed to enable %s\n", dep->name);
  1133. goto err0;
  1134. }
  1135. dep = dwc->eps[1];
  1136. ret = __dwc3_gadget_ep_enable(dep, &dwc3_gadget_ep0_desc, NULL);
  1137. if (ret) {
  1138. dev_err(dwc->dev, "failed to enable %s\n", dep->name);
  1139. goto err1;
  1140. }
  1141. /* begin to receive SETUP packets */
  1142. dwc->ep0state = EP0_SETUP_PHASE;
  1143. dwc3_ep0_out_start(dwc);
  1144. spin_unlock_irqrestore(&dwc->lock, flags);
  1145. return 0;
  1146. err1:
  1147. __dwc3_gadget_ep_disable(dwc->eps[0]);
  1148. err0:
  1149. spin_unlock_irqrestore(&dwc->lock, flags);
  1150. return ret;
  1151. }
  1152. static int dwc3_gadget_stop(struct usb_gadget *g,
  1153. struct usb_gadget_driver *driver)
  1154. {
  1155. struct dwc3 *dwc = gadget_to_dwc(g);
  1156. unsigned long flags;
  1157. spin_lock_irqsave(&dwc->lock, flags);
  1158. __dwc3_gadget_ep_disable(dwc->eps[0]);
  1159. __dwc3_gadget_ep_disable(dwc->eps[1]);
  1160. dwc->gadget_driver = NULL;
  1161. dwc->gadget.dev.driver = NULL;
  1162. spin_unlock_irqrestore(&dwc->lock, flags);
  1163. return 0;
  1164. }
  1165. static const struct usb_gadget_ops dwc3_gadget_ops = {
  1166. .get_frame = dwc3_gadget_get_frame,
  1167. .wakeup = dwc3_gadget_wakeup,
  1168. .set_selfpowered = dwc3_gadget_set_selfpowered,
  1169. .pullup = dwc3_gadget_pullup,
  1170. .udc_start = dwc3_gadget_start,
  1171. .udc_stop = dwc3_gadget_stop,
  1172. };
  1173. /* -------------------------------------------------------------------------- */
  1174. static int __devinit dwc3_gadget_init_endpoints(struct dwc3 *dwc)
  1175. {
  1176. struct dwc3_ep *dep;
  1177. u8 epnum;
  1178. INIT_LIST_HEAD(&dwc->gadget.ep_list);
  1179. for (epnum = 0; epnum < DWC3_ENDPOINTS_NUM; epnum++) {
  1180. dep = kzalloc(sizeof(*dep), GFP_KERNEL);
  1181. if (!dep) {
  1182. dev_err(dwc->dev, "can't allocate endpoint %d\n",
  1183. epnum);
  1184. return -ENOMEM;
  1185. }
  1186. dep->dwc = dwc;
  1187. dep->number = epnum;
  1188. dwc->eps[epnum] = dep;
  1189. snprintf(dep->name, sizeof(dep->name), "ep%d%s", epnum >> 1,
  1190. (epnum & 1) ? "in" : "out");
  1191. dep->endpoint.name = dep->name;
  1192. dep->direction = (epnum & 1);
  1193. if (epnum == 0 || epnum == 1) {
  1194. dep->endpoint.maxpacket = 512;
  1195. dep->endpoint.ops = &dwc3_gadget_ep0_ops;
  1196. if (!epnum)
  1197. dwc->gadget.ep0 = &dep->endpoint;
  1198. } else {
  1199. int ret;
  1200. dep->endpoint.maxpacket = 1024;
  1201. dep->endpoint.max_streams = 15;
  1202. dep->endpoint.ops = &dwc3_gadget_ep_ops;
  1203. list_add_tail(&dep->endpoint.ep_list,
  1204. &dwc->gadget.ep_list);
  1205. ret = dwc3_alloc_trb_pool(dep);
  1206. if (ret)
  1207. return ret;
  1208. }
  1209. INIT_LIST_HEAD(&dep->request_list);
  1210. INIT_LIST_HEAD(&dep->req_queued);
  1211. }
  1212. return 0;
  1213. }
  1214. static void dwc3_gadget_free_endpoints(struct dwc3 *dwc)
  1215. {
  1216. struct dwc3_ep *dep;
  1217. u8 epnum;
  1218. for (epnum = 0; epnum < DWC3_ENDPOINTS_NUM; epnum++) {
  1219. dep = dwc->eps[epnum];
  1220. dwc3_free_trb_pool(dep);
  1221. if (epnum != 0 && epnum != 1)
  1222. list_del(&dep->endpoint.ep_list);
  1223. kfree(dep);
  1224. }
  1225. }
  1226. static void dwc3_gadget_release(struct device *dev)
  1227. {
  1228. dev_dbg(dev, "%s\n", __func__);
  1229. }
  1230. /* -------------------------------------------------------------------------- */
  1231. static int dwc3_cleanup_done_reqs(struct dwc3 *dwc, struct dwc3_ep *dep,
  1232. const struct dwc3_event_depevt *event, int status)
  1233. {
  1234. struct dwc3_request *req;
  1235. struct dwc3_trb *trb;
  1236. unsigned int count;
  1237. unsigned int s_pkt = 0;
  1238. do {
  1239. req = next_request(&dep->req_queued);
  1240. if (!req) {
  1241. WARN_ON_ONCE(1);
  1242. return 1;
  1243. }
  1244. trb = req->trb;
  1245. if ((trb->ctrl & DWC3_TRB_CTRL_HWO) && status != -ESHUTDOWN)
  1246. /*
  1247. * We continue despite the error. There is not much we
  1248. * can do. If we don't clean it up we loop forever. If
  1249. * we skip the TRB then it gets overwritten after a
  1250. * while since we use them in a ring buffer. A BUG()
  1251. * would help. Lets hope that if this occurs, someone
  1252. * fixes the root cause instead of looking away :)
  1253. */
  1254. dev_err(dwc->dev, "%s's TRB (%p) still owned by HW\n",
  1255. dep->name, req->trb);
  1256. count = trb->size & DWC3_TRB_SIZE_MASK;
  1257. if (dep->direction) {
  1258. if (count) {
  1259. dev_err(dwc->dev, "incomplete IN transfer %s\n",
  1260. dep->name);
  1261. status = -ECONNRESET;
  1262. }
  1263. } else {
  1264. if (count && (event->status & DEPEVT_STATUS_SHORT))
  1265. s_pkt = 1;
  1266. }
  1267. /*
  1268. * We assume here we will always receive the entire data block
  1269. * which we should receive. Meaning, if we program RX to
  1270. * receive 4K but we receive only 2K, we assume that's all we
  1271. * should receive and we simply bounce the request back to the
  1272. * gadget driver for further processing.
  1273. */
  1274. req->request.actual += req->request.length - count;
  1275. dwc3_gadget_giveback(dep, req, status);
  1276. if (s_pkt)
  1277. break;
  1278. if ((event->status & DEPEVT_STATUS_LST) &&
  1279. (trb->ctrl & DWC3_TRB_CTRL_LST))
  1280. break;
  1281. if ((event->status & DEPEVT_STATUS_IOC) &&
  1282. (trb->ctrl & DWC3_TRB_CTRL_IOC))
  1283. break;
  1284. } while (1);
  1285. if ((event->status & DEPEVT_STATUS_IOC) &&
  1286. (trb->ctrl & DWC3_TRB_CTRL_IOC))
  1287. return 0;
  1288. return 1;
  1289. }
  1290. static void dwc3_endpoint_transfer_complete(struct dwc3 *dwc,
  1291. struct dwc3_ep *dep, const struct dwc3_event_depevt *event,
  1292. int start_new)
  1293. {
  1294. unsigned status = 0;
  1295. int clean_busy;
  1296. if (event->status & DEPEVT_STATUS_BUSERR)
  1297. status = -ECONNRESET;
  1298. clean_busy = dwc3_cleanup_done_reqs(dwc, dep, event, status);
  1299. if (clean_busy)
  1300. dep->flags &= ~DWC3_EP_BUSY;
  1301. /*
  1302. * WORKAROUND: This is the 2nd half of U1/U2 -> U0 workaround.
  1303. * See dwc3_gadget_linksts_change_interrupt() for 1st half.
  1304. */
  1305. if (dwc->revision < DWC3_REVISION_183A) {
  1306. u32 reg;
  1307. int i;
  1308. for (i = 0; i < DWC3_ENDPOINTS_NUM; i++) {
  1309. struct dwc3_ep *dep = dwc->eps[i];
  1310. if (!(dep->flags & DWC3_EP_ENABLED))
  1311. continue;
  1312. if (!list_empty(&dep->req_queued))
  1313. return;
  1314. }
  1315. reg = dwc3_readl(dwc->regs, DWC3_DCTL);
  1316. reg |= dwc->u1u2;
  1317. dwc3_writel(dwc->regs, DWC3_DCTL, reg);
  1318. dwc->u1u2 = 0;
  1319. }
  1320. }
  1321. static void dwc3_gadget_start_isoc(struct dwc3 *dwc,
  1322. struct dwc3_ep *dep, const struct dwc3_event_depevt *event)
  1323. {
  1324. u32 uf, mask;
  1325. if (list_empty(&dep->request_list)) {
  1326. dev_vdbg(dwc->dev, "ISOC ep %s run out for requests.\n",
  1327. dep->name);
  1328. return;
  1329. }
  1330. mask = ~(dep->interval - 1);
  1331. uf = event->parameters & mask;
  1332. /* 4 micro frames in the future */
  1333. uf += dep->interval * 4;
  1334. __dwc3_gadget_kick_transfer(dep, uf, 1);
  1335. }
  1336. static void dwc3_process_ep_cmd_complete(struct dwc3_ep *dep,
  1337. const struct dwc3_event_depevt *event)
  1338. {
  1339. struct dwc3 *dwc = dep->dwc;
  1340. struct dwc3_event_depevt mod_ev = *event;
  1341. /*
  1342. * We were asked to remove one request. It is possible that this
  1343. * request and a few others were started together and have the same
  1344. * transfer index. Since we stopped the complete endpoint we don't
  1345. * know how many requests were already completed (and not yet)
  1346. * reported and how could be done (later). We purge them all until
  1347. * the end of the list.
  1348. */
  1349. mod_ev.status = DEPEVT_STATUS_LST;
  1350. dwc3_cleanup_done_reqs(dwc, dep, &mod_ev, -ESHUTDOWN);
  1351. dep->flags &= ~DWC3_EP_BUSY;
  1352. /* pending requests are ignored and are queued on XferNotReady */
  1353. }
  1354. static void dwc3_ep_cmd_compl(struct dwc3_ep *dep,
  1355. const struct dwc3_event_depevt *event)
  1356. {
  1357. u32 param = event->parameters;
  1358. u32 cmd_type = (param >> 8) & ((1 << 5) - 1);
  1359. switch (cmd_type) {
  1360. case DWC3_DEPCMD_ENDTRANSFER:
  1361. dwc3_process_ep_cmd_complete(dep, event);
  1362. break;
  1363. case DWC3_DEPCMD_STARTTRANSFER:
  1364. dep->res_trans_idx = param & 0x7f;
  1365. break;
  1366. default:
  1367. printk(KERN_ERR "%s() unknown /unexpected type: %d\n",
  1368. __func__, cmd_type);
  1369. break;
  1370. };
  1371. }
  1372. static void dwc3_endpoint_interrupt(struct dwc3 *dwc,
  1373. const struct dwc3_event_depevt *event)
  1374. {
  1375. struct dwc3_ep *dep;
  1376. u8 epnum = event->endpoint_number;
  1377. dep = dwc->eps[epnum];
  1378. dev_vdbg(dwc->dev, "%s: %s\n", dep->name,
  1379. dwc3_ep_event_string(event->endpoint_event));
  1380. if (epnum == 0 || epnum == 1) {
  1381. dwc3_ep0_interrupt(dwc, event);
  1382. return;
  1383. }
  1384. switch (event->endpoint_event) {
  1385. case DWC3_DEPEVT_XFERCOMPLETE:
  1386. dep->res_trans_idx = 0;
  1387. if (usb_endpoint_xfer_isoc(dep->desc)) {
  1388. dev_dbg(dwc->dev, "%s is an Isochronous endpoint\n",
  1389. dep->name);
  1390. return;
  1391. }
  1392. dwc3_endpoint_transfer_complete(dwc, dep, event, 1);
  1393. break;
  1394. case DWC3_DEPEVT_XFERINPROGRESS:
  1395. if (!usb_endpoint_xfer_isoc(dep->desc)) {
  1396. dev_dbg(dwc->dev, "%s is not an Isochronous endpoint\n",
  1397. dep->name);
  1398. return;
  1399. }
  1400. dwc3_endpoint_transfer_complete(dwc, dep, event, 0);
  1401. break;
  1402. case DWC3_DEPEVT_XFERNOTREADY:
  1403. if (usb_endpoint_xfer_isoc(dep->desc)) {
  1404. dwc3_gadget_start_isoc(dwc, dep, event);
  1405. } else {
  1406. int ret;
  1407. dev_vdbg(dwc->dev, "%s: reason %s\n",
  1408. dep->name, event->status &
  1409. DEPEVT_STATUS_TRANSFER_ACTIVE
  1410. ? "Transfer Active"
  1411. : "Transfer Not Active");
  1412. ret = __dwc3_gadget_kick_transfer(dep, 0, 1);
  1413. if (!ret || ret == -EBUSY)
  1414. return;
  1415. dev_dbg(dwc->dev, "%s: failed to kick transfers\n",
  1416. dep->name);
  1417. }
  1418. break;
  1419. case DWC3_DEPEVT_STREAMEVT:
  1420. if (!usb_endpoint_xfer_bulk(dep->desc)) {
  1421. dev_err(dwc->dev, "Stream event for non-Bulk %s\n",
  1422. dep->name);
  1423. return;
  1424. }
  1425. switch (event->status) {
  1426. case DEPEVT_STREAMEVT_FOUND:
  1427. dev_vdbg(dwc->dev, "Stream %d found and started\n",
  1428. event->parameters);
  1429. break;
  1430. case DEPEVT_STREAMEVT_NOTFOUND:
  1431. /* FALLTHROUGH */
  1432. default:
  1433. dev_dbg(dwc->dev, "Couldn't find suitable stream\n");
  1434. }
  1435. break;
  1436. case DWC3_DEPEVT_RXTXFIFOEVT:
  1437. dev_dbg(dwc->dev, "%s FIFO Overrun\n", dep->name);
  1438. break;
  1439. case DWC3_DEPEVT_EPCMDCMPLT:
  1440. dwc3_ep_cmd_compl(dep, event);
  1441. break;
  1442. }
  1443. }
  1444. static void dwc3_disconnect_gadget(struct dwc3 *dwc)
  1445. {
  1446. if (dwc->gadget_driver && dwc->gadget_driver->disconnect) {
  1447. spin_unlock(&dwc->lock);
  1448. dwc->gadget_driver->disconnect(&dwc->gadget);
  1449. spin_lock(&dwc->lock);
  1450. }
  1451. }
  1452. static void dwc3_stop_active_transfer(struct dwc3 *dwc, u32 epnum)
  1453. {
  1454. struct dwc3_ep *dep;
  1455. struct dwc3_gadget_ep_cmd_params params;
  1456. u32 cmd;
  1457. int ret;
  1458. dep = dwc->eps[epnum];
  1459. WARN_ON(!dep->res_trans_idx);
  1460. if (dep->res_trans_idx) {
  1461. cmd = DWC3_DEPCMD_ENDTRANSFER;
  1462. cmd |= DWC3_DEPCMD_HIPRI_FORCERM | DWC3_DEPCMD_CMDIOC;
  1463. cmd |= DWC3_DEPCMD_PARAM(dep->res_trans_idx);
  1464. memset(&params, 0, sizeof(params));
  1465. ret = dwc3_send_gadget_ep_cmd(dwc, dep->number, cmd, &params);
  1466. WARN_ON_ONCE(ret);
  1467. dep->res_trans_idx = 0;
  1468. }
  1469. }
  1470. static void dwc3_stop_active_transfers(struct dwc3 *dwc)
  1471. {
  1472. u32 epnum;
  1473. for (epnum = 2; epnum < DWC3_ENDPOINTS_NUM; epnum++) {
  1474. struct dwc3_ep *dep;
  1475. dep = dwc->eps[epnum];
  1476. if (!(dep->flags & DWC3_EP_ENABLED))
  1477. continue;
  1478. dwc3_remove_requests(dwc, dep);
  1479. }
  1480. }
  1481. static void dwc3_clear_stall_all_ep(struct dwc3 *dwc)
  1482. {
  1483. u32 epnum;
  1484. for (epnum = 1; epnum < DWC3_ENDPOINTS_NUM; epnum++) {
  1485. struct dwc3_ep *dep;
  1486. struct dwc3_gadget_ep_cmd_params params;
  1487. int ret;
  1488. dep = dwc->eps[epnum];
  1489. if (!(dep->flags & DWC3_EP_STALL))
  1490. continue;
  1491. dep->flags &= ~DWC3_EP_STALL;
  1492. memset(&params, 0, sizeof(params));
  1493. ret = dwc3_send_gadget_ep_cmd(dwc, dep->number,
  1494. DWC3_DEPCMD_CLEARSTALL, &params);
  1495. WARN_ON_ONCE(ret);
  1496. }
  1497. }
  1498. static void dwc3_gadget_disconnect_interrupt(struct dwc3 *dwc)
  1499. {
  1500. dev_vdbg(dwc->dev, "%s\n", __func__);
  1501. #if 0
  1502. XXX
  1503. U1/U2 is powersave optimization. Skip it for now. Anyway we need to
  1504. enable it before we can disable it.
  1505. reg = dwc3_readl(dwc->regs, DWC3_DCTL);
  1506. reg &= ~DWC3_DCTL_INITU1ENA;
  1507. dwc3_writel(dwc->regs, DWC3_DCTL, reg);
  1508. reg &= ~DWC3_DCTL_INITU2ENA;
  1509. dwc3_writel(dwc->regs, DWC3_DCTL, reg);
  1510. #endif
  1511. dwc3_stop_active_transfers(dwc);
  1512. dwc3_disconnect_gadget(dwc);
  1513. dwc->start_config_issued = false;
  1514. dwc->gadget.speed = USB_SPEED_UNKNOWN;
  1515. dwc->setup_packet_pending = false;
  1516. }
  1517. static void dwc3_gadget_usb3_phy_power(struct dwc3 *dwc, int on)
  1518. {
  1519. u32 reg;
  1520. reg = dwc3_readl(dwc->regs, DWC3_GUSB3PIPECTL(0));
  1521. if (on)
  1522. reg &= ~DWC3_GUSB3PIPECTL_SUSPHY;
  1523. else
  1524. reg |= DWC3_GUSB3PIPECTL_SUSPHY;
  1525. dwc3_writel(dwc->regs, DWC3_GUSB3PIPECTL(0), reg);
  1526. }
  1527. static void dwc3_gadget_usb2_phy_power(struct dwc3 *dwc, int on)
  1528. {
  1529. u32 reg;
  1530. reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0));
  1531. if (on)
  1532. reg &= ~DWC3_GUSB2PHYCFG_SUSPHY;
  1533. else
  1534. reg |= DWC3_GUSB2PHYCFG_SUSPHY;
  1535. dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg);
  1536. }
  1537. static void dwc3_gadget_reset_interrupt(struct dwc3 *dwc)
  1538. {
  1539. u32 reg;
  1540. dev_vdbg(dwc->dev, "%s\n", __func__);
  1541. /*
  1542. * WORKAROUND: DWC3 revisions <1.88a have an issue which
  1543. * would cause a missing Disconnect Event if there's a
  1544. * pending Setup Packet in the FIFO.
  1545. *
  1546. * There's no suggested workaround on the official Bug
  1547. * report, which states that "unless the driver/application
  1548. * is doing any special handling of a disconnect event,
  1549. * there is no functional issue".
  1550. *
  1551. * Unfortunately, it turns out that we _do_ some special
  1552. * handling of a disconnect event, namely complete all
  1553. * pending transfers, notify gadget driver of the
  1554. * disconnection, and so on.
  1555. *
  1556. * Our suggested workaround is to follow the Disconnect
  1557. * Event steps here, instead, based on a setup_packet_pending
  1558. * flag. Such flag gets set whenever we have a XferNotReady
  1559. * event on EP0 and gets cleared on XferComplete for the
  1560. * same endpoint.
  1561. *
  1562. * Refers to:
  1563. *
  1564. * STAR#9000466709: RTL: Device : Disconnect event not
  1565. * generated if setup packet pending in FIFO
  1566. */
  1567. if (dwc->revision < DWC3_REVISION_188A) {
  1568. if (dwc->setup_packet_pending)
  1569. dwc3_gadget_disconnect_interrupt(dwc);
  1570. }
  1571. /* after reset -> Default State */
  1572. dwc->dev_state = DWC3_DEFAULT_STATE;
  1573. /* Enable PHYs */
  1574. dwc3_gadget_usb2_phy_power(dwc, true);
  1575. dwc3_gadget_usb3_phy_power(dwc, true);
  1576. if (dwc->gadget.speed != USB_SPEED_UNKNOWN)
  1577. dwc3_disconnect_gadget(dwc);
  1578. reg = dwc3_readl(dwc->regs, DWC3_DCTL);
  1579. reg &= ~DWC3_DCTL_TSTCTRL_MASK;
  1580. reg &= ~(DWC3_DCTL_INITU1ENA | DWC3_DCTL_INITU2ENA);
  1581. dwc3_writel(dwc->regs, DWC3_DCTL, reg);
  1582. dwc->test_mode = false;
  1583. dwc3_stop_active_transfers(dwc);
  1584. dwc3_clear_stall_all_ep(dwc);
  1585. dwc->start_config_issued = false;
  1586. /* Reset device address to zero */
  1587. reg = dwc3_readl(dwc->regs, DWC3_DCFG);
  1588. reg &= ~(DWC3_DCFG_DEVADDR_MASK);
  1589. dwc3_writel(dwc->regs, DWC3_DCFG, reg);
  1590. }
  1591. static void dwc3_update_ram_clk_sel(struct dwc3 *dwc, u32 speed)
  1592. {
  1593. u32 reg;
  1594. u32 usb30_clock = DWC3_GCTL_CLK_BUS;
  1595. /*
  1596. * We change the clock only at SS but I dunno why I would want to do
  1597. * this. Maybe it becomes part of the power saving plan.
  1598. */
  1599. if (speed != DWC3_DSTS_SUPERSPEED)
  1600. return;
  1601. /*
  1602. * RAMClkSel is reset to 0 after USB reset, so it must be reprogrammed
  1603. * each time on Connect Done.
  1604. */
  1605. if (!usb30_clock)
  1606. return;
  1607. reg = dwc3_readl(dwc->regs, DWC3_GCTL);
  1608. reg |= DWC3_GCTL_RAMCLKSEL(usb30_clock);
  1609. dwc3_writel(dwc->regs, DWC3_GCTL, reg);
  1610. }
  1611. static void dwc3_gadget_disable_phy(struct dwc3 *dwc, u8 speed)
  1612. {
  1613. switch (speed) {
  1614. case USB_SPEED_SUPER:
  1615. dwc3_gadget_usb2_phy_power(dwc, false);
  1616. break;
  1617. case USB_SPEED_HIGH:
  1618. case USB_SPEED_FULL:
  1619. case USB_SPEED_LOW:
  1620. dwc3_gadget_usb3_phy_power(dwc, false);
  1621. break;
  1622. }
  1623. }
  1624. static void dwc3_gadget_conndone_interrupt(struct dwc3 *dwc)
  1625. {
  1626. struct dwc3_gadget_ep_cmd_params params;
  1627. struct dwc3_ep *dep;
  1628. int ret;
  1629. u32 reg;
  1630. u8 speed;
  1631. dev_vdbg(dwc->dev, "%s\n", __func__);
  1632. memset(&params, 0x00, sizeof(params));
  1633. reg = dwc3_readl(dwc->regs, DWC3_DSTS);
  1634. speed = reg & DWC3_DSTS_CONNECTSPD;
  1635. dwc->speed = speed;
  1636. dwc3_update_ram_clk_sel(dwc, speed);
  1637. switch (speed) {
  1638. case DWC3_DCFG_SUPERSPEED:
  1639. /*
  1640. * WORKAROUND: DWC3 revisions <1.90a have an issue which
  1641. * would cause a missing USB3 Reset event.
  1642. *
  1643. * In such situations, we should force a USB3 Reset
  1644. * event by calling our dwc3_gadget_reset_interrupt()
  1645. * routine.
  1646. *
  1647. * Refers to:
  1648. *
  1649. * STAR#9000483510: RTL: SS : USB3 reset event may
  1650. * not be generated always when the link enters poll
  1651. */
  1652. if (dwc->revision < DWC3_REVISION_190A)
  1653. dwc3_gadget_reset_interrupt(dwc);
  1654. dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512);
  1655. dwc->gadget.ep0->maxpacket = 512;
  1656. dwc->gadget.speed = USB_SPEED_SUPER;
  1657. break;
  1658. case DWC3_DCFG_HIGHSPEED:
  1659. dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(64);
  1660. dwc->gadget.ep0->maxpacket = 64;
  1661. dwc->gadget.speed = USB_SPEED_HIGH;
  1662. break;
  1663. case DWC3_DCFG_FULLSPEED2:
  1664. case DWC3_DCFG_FULLSPEED1:
  1665. dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(64);
  1666. dwc->gadget.ep0->maxpacket = 64;
  1667. dwc->gadget.speed = USB_SPEED_FULL;
  1668. break;
  1669. case DWC3_DCFG_LOWSPEED:
  1670. dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(8);
  1671. dwc->gadget.ep0->maxpacket = 8;
  1672. dwc->gadget.speed = USB_SPEED_LOW;
  1673. break;
  1674. }
  1675. /* Disable unneded PHY */
  1676. dwc3_gadget_disable_phy(dwc, dwc->gadget.speed);
  1677. dep = dwc->eps[0];
  1678. ret = __dwc3_gadget_ep_enable(dep, &dwc3_gadget_ep0_desc, NULL);
  1679. if (ret) {
  1680. dev_err(dwc->dev, "failed to enable %s\n", dep->name);
  1681. return;
  1682. }
  1683. dep = dwc->eps[1];
  1684. ret = __dwc3_gadget_ep_enable(dep, &dwc3_gadget_ep0_desc, NULL);
  1685. if (ret) {
  1686. dev_err(dwc->dev, "failed to enable %s\n", dep->name);
  1687. return;
  1688. }
  1689. /*
  1690. * Configure PHY via GUSB3PIPECTLn if required.
  1691. *
  1692. * Update GTXFIFOSIZn
  1693. *
  1694. * In both cases reset values should be sufficient.
  1695. */
  1696. }
  1697. static void dwc3_gadget_wakeup_interrupt(struct dwc3 *dwc)
  1698. {
  1699. dev_vdbg(dwc->dev, "%s\n", __func__);
  1700. /*
  1701. * TODO take core out of low power mode when that's
  1702. * implemented.
  1703. */
  1704. dwc->gadget_driver->resume(&dwc->gadget);
  1705. }
  1706. static void dwc3_gadget_linksts_change_interrupt(struct dwc3 *dwc,
  1707. unsigned int evtinfo)
  1708. {
  1709. enum dwc3_link_state next = evtinfo & DWC3_LINK_STATE_MASK;
  1710. /*
  1711. * WORKAROUND: DWC3 Revisions <1.83a have an issue which, depending
  1712. * on the link partner, the USB session might do multiple entry/exit
  1713. * of low power states before a transfer takes place.
  1714. *
  1715. * Due to this problem, we might experience lower throughput. The
  1716. * suggested workaround is to disable DCTL[12:9] bits if we're
  1717. * transitioning from U1/U2 to U0 and enable those bits again
  1718. * after a transfer completes and there are no pending transfers
  1719. * on any of the enabled endpoints.
  1720. *
  1721. * This is the first half of that workaround.
  1722. *
  1723. * Refers to:
  1724. *
  1725. * STAR#9000446952: RTL: Device SS : if U1/U2 ->U0 takes >128us
  1726. * core send LGO_Ux entering U0
  1727. */
  1728. if (dwc->revision < DWC3_REVISION_183A) {
  1729. if (next == DWC3_LINK_STATE_U0) {
  1730. u32 u1u2;
  1731. u32 reg;
  1732. switch (dwc->link_state) {
  1733. case DWC3_LINK_STATE_U1:
  1734. case DWC3_LINK_STATE_U2:
  1735. reg = dwc3_readl(dwc->regs, DWC3_DCTL);
  1736. u1u2 = reg & (DWC3_DCTL_INITU2ENA
  1737. | DWC3_DCTL_ACCEPTU2ENA
  1738. | DWC3_DCTL_INITU1ENA
  1739. | DWC3_DCTL_ACCEPTU1ENA);
  1740. if (!dwc->u1u2)
  1741. dwc->u1u2 = reg & u1u2;
  1742. reg &= ~u1u2;
  1743. dwc3_writel(dwc->regs, DWC3_DCTL, reg);
  1744. break;
  1745. default:
  1746. /* do nothing */
  1747. break;
  1748. }
  1749. }
  1750. }
  1751. dwc->link_state = next;
  1752. dev_vdbg(dwc->dev, "%s link %d\n", __func__, dwc->link_state);
  1753. }
  1754. static void dwc3_gadget_interrupt(struct dwc3 *dwc,
  1755. const struct dwc3_event_devt *event)
  1756. {
  1757. switch (event->type) {
  1758. case DWC3_DEVICE_EVENT_DISCONNECT:
  1759. dwc3_gadget_disconnect_interrupt(dwc);
  1760. break;
  1761. case DWC3_DEVICE_EVENT_RESET:
  1762. dwc3_gadget_reset_interrupt(dwc);
  1763. break;
  1764. case DWC3_DEVICE_EVENT_CONNECT_DONE:
  1765. dwc3_gadget_conndone_interrupt(dwc);
  1766. break;
  1767. case DWC3_DEVICE_EVENT_WAKEUP:
  1768. dwc3_gadget_wakeup_interrupt(dwc);
  1769. break;
  1770. case DWC3_DEVICE_EVENT_LINK_STATUS_CHANGE:
  1771. dwc3_gadget_linksts_change_interrupt(dwc, event->event_info);
  1772. break;
  1773. case DWC3_DEVICE_EVENT_EOPF:
  1774. dev_vdbg(dwc->dev, "End of Periodic Frame\n");
  1775. break;
  1776. case DWC3_DEVICE_EVENT_SOF:
  1777. dev_vdbg(dwc->dev, "Start of Periodic Frame\n");
  1778. break;
  1779. case DWC3_DEVICE_EVENT_ERRATIC_ERROR:
  1780. dev_vdbg(dwc->dev, "Erratic Error\n");
  1781. break;
  1782. case DWC3_DEVICE_EVENT_CMD_CMPL:
  1783. dev_vdbg(dwc->dev, "Command Complete\n");
  1784. break;
  1785. case DWC3_DEVICE_EVENT_OVERFLOW:
  1786. dev_vdbg(dwc->dev, "Overflow\n");
  1787. break;
  1788. default:
  1789. dev_dbg(dwc->dev, "UNKNOWN IRQ %d\n", event->type);
  1790. }
  1791. }
  1792. static void dwc3_process_event_entry(struct dwc3 *dwc,
  1793. const union dwc3_event *event)
  1794. {
  1795. /* Endpoint IRQ, handle it and return early */
  1796. if (event->type.is_devspec == 0) {
  1797. /* depevt */
  1798. return dwc3_endpoint_interrupt(dwc, &event->depevt);
  1799. }
  1800. switch (event->type.type) {
  1801. case DWC3_EVENT_TYPE_DEV:
  1802. dwc3_gadget_interrupt(dwc, &event->devt);
  1803. break;
  1804. /* REVISIT what to do with Carkit and I2C events ? */
  1805. default:
  1806. dev_err(dwc->dev, "UNKNOWN IRQ type %d\n", event->raw);
  1807. }
  1808. }
  1809. static irqreturn_t dwc3_process_event_buf(struct dwc3 *dwc, u32 buf)
  1810. {
  1811. struct dwc3_event_buffer *evt;
  1812. int left;
  1813. u32 count;
  1814. count = dwc3_readl(dwc->regs, DWC3_GEVNTCOUNT(buf));
  1815. count &= DWC3_GEVNTCOUNT_MASK;
  1816. if (!count)
  1817. return IRQ_NONE;
  1818. evt = dwc->ev_buffs[buf];
  1819. left = count;
  1820. while (left > 0) {
  1821. union dwc3_event event;
  1822. event.raw = *(u32 *) (evt->buf + evt->lpos);
  1823. dwc3_process_event_entry(dwc, &event);
  1824. /*
  1825. * XXX we wrap around correctly to the next entry as almost all
  1826. * entries are 4 bytes in size. There is one entry which has 12
  1827. * bytes which is a regular entry followed by 8 bytes data. ATM
  1828. * I don't know how things are organized if were get next to the
  1829. * a boundary so I worry about that once we try to handle that.
  1830. */
  1831. evt->lpos = (evt->lpos + 4) % DWC3_EVENT_BUFFERS_SIZE;
  1832. left -= 4;
  1833. dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(buf), 4);
  1834. }
  1835. return IRQ_HANDLED;
  1836. }
  1837. static irqreturn_t dwc3_interrupt(int irq, void *_dwc)
  1838. {
  1839. struct dwc3 *dwc = _dwc;
  1840. int i;
  1841. irqreturn_t ret = IRQ_NONE;
  1842. spin_lock(&dwc->lock);
  1843. for (i = 0; i < dwc->num_event_buffers; i++) {
  1844. irqreturn_t status;
  1845. status = dwc3_process_event_buf(dwc, i);
  1846. if (status == IRQ_HANDLED)
  1847. ret = status;
  1848. }
  1849. spin_unlock(&dwc->lock);
  1850. return ret;
  1851. }
  1852. /**
  1853. * dwc3_gadget_init - Initializes gadget related registers
  1854. * @dwc: pointer to our controller context structure
  1855. *
  1856. * Returns 0 on success otherwise negative errno.
  1857. */
  1858. int __devinit dwc3_gadget_init(struct dwc3 *dwc)
  1859. {
  1860. u32 reg;
  1861. int ret;
  1862. int irq;
  1863. dwc->ctrl_req = dma_alloc_coherent(dwc->dev, sizeof(*dwc->ctrl_req),
  1864. &dwc->ctrl_req_addr, GFP_KERNEL);
  1865. if (!dwc->ctrl_req) {
  1866. dev_err(dwc->dev, "failed to allocate ctrl request\n");
  1867. ret = -ENOMEM;
  1868. goto err0;
  1869. }
  1870. dwc->ep0_trb = dma_alloc_coherent(dwc->dev, sizeof(*dwc->ep0_trb),
  1871. &dwc->ep0_trb_addr, GFP_KERNEL);
  1872. if (!dwc->ep0_trb) {
  1873. dev_err(dwc->dev, "failed to allocate ep0 trb\n");
  1874. ret = -ENOMEM;
  1875. goto err1;
  1876. }
  1877. dwc->setup_buf = kzalloc(sizeof(*dwc->setup_buf) * 2,
  1878. GFP_KERNEL);
  1879. if (!dwc->setup_buf) {
  1880. dev_err(dwc->dev, "failed to allocate setup buffer\n");
  1881. ret = -ENOMEM;
  1882. goto err2;
  1883. }
  1884. dwc->ep0_bounce = dma_alloc_coherent(dwc->dev,
  1885. 512, &dwc->ep0_bounce_addr, GFP_KERNEL);
  1886. if (!dwc->ep0_bounce) {
  1887. dev_err(dwc->dev, "failed to allocate ep0 bounce buffer\n");
  1888. ret = -ENOMEM;
  1889. goto err3;
  1890. }
  1891. dev_set_name(&dwc->gadget.dev, "gadget");
  1892. dwc->gadget.ops = &dwc3_gadget_ops;
  1893. dwc->gadget.max_speed = USB_SPEED_SUPER;
  1894. dwc->gadget.speed = USB_SPEED_UNKNOWN;
  1895. dwc->gadget.dev.parent = dwc->dev;
  1896. dwc->gadget.sg_supported = true;
  1897. dma_set_coherent_mask(&dwc->gadget.dev, dwc->dev->coherent_dma_mask);
  1898. dwc->gadget.dev.dma_parms = dwc->dev->dma_parms;
  1899. dwc->gadget.dev.dma_mask = dwc->dev->dma_mask;
  1900. dwc->gadget.dev.release = dwc3_gadget_release;
  1901. dwc->gadget.name = "dwc3-gadget";
  1902. /*
  1903. * REVISIT: Here we should clear all pending IRQs to be
  1904. * sure we're starting from a well known location.
  1905. */
  1906. ret = dwc3_gadget_init_endpoints(dwc);
  1907. if (ret)
  1908. goto err4;
  1909. irq = platform_get_irq(to_platform_device(dwc->dev), 0);
  1910. ret = request_irq(irq, dwc3_interrupt, IRQF_SHARED,
  1911. "dwc3", dwc);
  1912. if (ret) {
  1913. dev_err(dwc->dev, "failed to request irq #%d --> %d\n",
  1914. irq, ret);
  1915. goto err5;
  1916. }
  1917. reg = dwc3_readl(dwc->regs, DWC3_DCFG);
  1918. reg |= DWC3_DCFG_LPM_CAP;
  1919. dwc3_writel(dwc->regs, DWC3_DCFG, reg);
  1920. reg = dwc3_readl(dwc->regs, DWC3_DCTL);
  1921. reg |= DWC3_DCTL_ACCEPTU1ENA | DWC3_DCTL_ACCEPTU2ENA;
  1922. dwc3_writel(dwc->regs, DWC3_DCTL, reg);
  1923. /* Enable all but Start and End of Frame IRQs */
  1924. reg = (DWC3_DEVTEN_VNDRDEVTSTRCVEDEN |
  1925. DWC3_DEVTEN_EVNTOVERFLOWEN |
  1926. DWC3_DEVTEN_CMDCMPLTEN |
  1927. DWC3_DEVTEN_ERRTICERREN |
  1928. DWC3_DEVTEN_WKUPEVTEN |
  1929. DWC3_DEVTEN_ULSTCNGEN |
  1930. DWC3_DEVTEN_CONNECTDONEEN |
  1931. DWC3_DEVTEN_USBRSTEN |
  1932. DWC3_DEVTEN_DISCONNEVTEN);
  1933. dwc3_writel(dwc->regs, DWC3_DEVTEN, reg);
  1934. ret = device_register(&dwc->gadget.dev);
  1935. if (ret) {
  1936. dev_err(dwc->dev, "failed to register gadget device\n");
  1937. put_device(&dwc->gadget.dev);
  1938. goto err6;
  1939. }
  1940. ret = usb_add_gadget_udc(dwc->dev, &dwc->gadget);
  1941. if (ret) {
  1942. dev_err(dwc->dev, "failed to register udc\n");
  1943. goto err7;
  1944. }
  1945. return 0;
  1946. err7:
  1947. device_unregister(&dwc->gadget.dev);
  1948. err6:
  1949. dwc3_writel(dwc->regs, DWC3_DEVTEN, 0x00);
  1950. free_irq(irq, dwc);
  1951. err5:
  1952. dwc3_gadget_free_endpoints(dwc);
  1953. err4:
  1954. dma_free_coherent(dwc->dev, 512, dwc->ep0_bounce,
  1955. dwc->ep0_bounce_addr);
  1956. err3:
  1957. kfree(dwc->setup_buf);
  1958. err2:
  1959. dma_free_coherent(dwc->dev, sizeof(*dwc->ep0_trb),
  1960. dwc->ep0_trb, dwc->ep0_trb_addr);
  1961. err1:
  1962. dma_free_coherent(dwc->dev, sizeof(*dwc->ctrl_req),
  1963. dwc->ctrl_req, dwc->ctrl_req_addr);
  1964. err0:
  1965. return ret;
  1966. }
  1967. void dwc3_gadget_exit(struct dwc3 *dwc)
  1968. {
  1969. int irq;
  1970. usb_del_gadget_udc(&dwc->gadget);
  1971. irq = platform_get_irq(to_platform_device(dwc->dev), 0);
  1972. dwc3_writel(dwc->regs, DWC3_DEVTEN, 0x00);
  1973. free_irq(irq, dwc);
  1974. dwc3_gadget_free_endpoints(dwc);
  1975. dma_free_coherent(dwc->dev, 512, dwc->ep0_bounce,
  1976. dwc->ep0_bounce_addr);
  1977. kfree(dwc->setup_buf);
  1978. dma_free_coherent(dwc->dev, sizeof(*dwc->ep0_trb),
  1979. dwc->ep0_trb, dwc->ep0_trb_addr);
  1980. dma_free_coherent(dwc->dev, sizeof(*dwc->ctrl_req),
  1981. dwc->ctrl_req, dwc->ctrl_req_addr);
  1982. device_unregister(&dwc->gadget.dev);
  1983. }