gadget.c 65 KB

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