gadget.c 76 KB

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