gadget.c 81 KB

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