gadget.c 126 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809
  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 - https://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, n) (((d)->frame_number + ((d)->interval * (n))) \
  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 USB_TEST_J:
  43. case USB_TEST_K:
  44. case USB_TEST_SE0_NAK:
  45. case USB_TEST_PACKET:
  46. case USB_TEST_FORCE_ENABLE:
  47. reg |= mode << 1;
  48. break;
  49. default:
  50. return -EINVAL;
  51. }
  52. dwc3_gadget_dctl_write_safe(dwc, 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 (!DWC3_VER_IS_PRIOR(DWC3, 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 no action before sending new link state change */
  98. dwc3_writel(dwc->regs, DWC3_DCTL, reg);
  99. /* set requested state */
  100. reg |= DWC3_DCTL_ULSTCHNGREQ(state);
  101. dwc3_writel(dwc->regs, DWC3_DCTL, reg);
  102. /*
  103. * The following code is racy when called from dwc3_gadget_wakeup,
  104. * and is not needed, at least on newer versions
  105. */
  106. if (!DWC3_VER_IS_PRIOR(DWC3, 194A))
  107. return 0;
  108. /* wait for a change in DSTS */
  109. retries = 10000;
  110. while (--retries) {
  111. reg = dwc3_readl(dwc->regs, DWC3_DSTS);
  112. if (DWC3_DSTS_USBLNKST(reg) == state)
  113. return 0;
  114. udelay(5);
  115. }
  116. return -ETIMEDOUT;
  117. }
  118. static void dwc3_ep0_reset_state(struct dwc3 *dwc)
  119. {
  120. unsigned int dir;
  121. if (dwc->ep0state != EP0_SETUP_PHASE) {
  122. dir = !!dwc->ep0_expect_in;
  123. if (dwc->ep0state == EP0_DATA_PHASE)
  124. dwc3_ep0_end_control_data(dwc, dwc->eps[dir]);
  125. else
  126. dwc3_ep0_end_control_data(dwc, dwc->eps[!dir]);
  127. dwc->eps[0]->trb_enqueue = 0;
  128. dwc->eps[1]->trb_enqueue = 0;
  129. dwc3_ep0_stall_and_restart(dwc);
  130. }
  131. }
  132. /**
  133. * dwc3_ep_inc_trb - increment a trb index.
  134. * @index: Pointer to the TRB index to increment.
  135. *
  136. * The index should never point to the link TRB. After incrementing,
  137. * if it is point to the link TRB, wrap around to the beginning. The
  138. * link TRB is always at the last TRB entry.
  139. */
  140. static void dwc3_ep_inc_trb(u8 *index)
  141. {
  142. (*index)++;
  143. if (*index == (DWC3_TRB_NUM - 1))
  144. *index = 0;
  145. }
  146. /**
  147. * dwc3_ep_inc_enq - increment endpoint's enqueue pointer
  148. * @dep: The endpoint whose enqueue pointer we're incrementing
  149. */
  150. static void dwc3_ep_inc_enq(struct dwc3_ep *dep)
  151. {
  152. dwc3_ep_inc_trb(&dep->trb_enqueue);
  153. }
  154. /**
  155. * dwc3_ep_inc_deq - increment endpoint's dequeue pointer
  156. * @dep: The endpoint whose enqueue pointer we're incrementing
  157. */
  158. static void dwc3_ep_inc_deq(struct dwc3_ep *dep)
  159. {
  160. dwc3_ep_inc_trb(&dep->trb_dequeue);
  161. }
  162. static void dwc3_gadget_del_and_unmap_request(struct dwc3_ep *dep,
  163. struct dwc3_request *req, int status)
  164. {
  165. struct dwc3 *dwc = dep->dwc;
  166. list_del(&req->list);
  167. req->remaining = 0;
  168. req->needs_extra_trb = false;
  169. req->num_trbs = 0;
  170. if (req->request.status == -EINPROGRESS)
  171. req->request.status = status;
  172. if (req->trb)
  173. usb_gadget_unmap_request_by_dev(dwc->sysdev,
  174. &req->request, req->direction);
  175. req->trb = NULL;
  176. trace_dwc3_gadget_giveback(req);
  177. if (dep->number > 1)
  178. pm_runtime_put(dwc->dev);
  179. }
  180. /**
  181. * dwc3_gadget_giveback - call struct usb_request's ->complete callback
  182. * @dep: The endpoint to whom the request belongs to
  183. * @req: The request we're giving back
  184. * @status: completion code for the request
  185. *
  186. * Must be called with controller's lock held and interrupts disabled. This
  187. * function will unmap @req and call its ->complete() callback to notify upper
  188. * layers that it has completed.
  189. */
  190. void dwc3_gadget_giveback(struct dwc3_ep *dep, struct dwc3_request *req,
  191. int status)
  192. {
  193. struct dwc3 *dwc = dep->dwc;
  194. dwc3_gadget_del_and_unmap_request(dep, req, status);
  195. req->status = DWC3_REQUEST_STATUS_COMPLETED;
  196. spin_unlock(&dwc->lock);
  197. usb_gadget_giveback_request(&dep->endpoint, &req->request);
  198. spin_lock(&dwc->lock);
  199. }
  200. /**
  201. * dwc3_send_gadget_generic_command - issue a generic command for the controller
  202. * @dwc: pointer to the controller context
  203. * @cmd: the command to be issued
  204. * @param: command parameter
  205. *
  206. * Caller should take care of locking. Issue @cmd with a given @param to @dwc
  207. * and wait for its completion.
  208. */
  209. int dwc3_send_gadget_generic_command(struct dwc3 *dwc, unsigned int cmd,
  210. u32 param)
  211. {
  212. u32 timeout = 500;
  213. int status = 0;
  214. int ret = 0;
  215. u32 reg;
  216. dwc3_writel(dwc->regs, DWC3_DGCMDPAR, param);
  217. dwc3_writel(dwc->regs, DWC3_DGCMD, cmd | DWC3_DGCMD_CMDACT);
  218. do {
  219. reg = dwc3_readl(dwc->regs, DWC3_DGCMD);
  220. if (!(reg & DWC3_DGCMD_CMDACT)) {
  221. status = DWC3_DGCMD_STATUS(reg);
  222. if (status)
  223. ret = -EINVAL;
  224. break;
  225. }
  226. } while (--timeout);
  227. if (!timeout) {
  228. ret = -ETIMEDOUT;
  229. status = -ETIMEDOUT;
  230. }
  231. trace_dwc3_gadget_generic_cmd(cmd, param, status);
  232. return ret;
  233. }
  234. static int __dwc3_gadget_wakeup(struct dwc3 *dwc, bool async);
  235. /**
  236. * dwc3_send_gadget_ep_cmd - issue an endpoint command
  237. * @dep: the endpoint to which the command is going to be issued
  238. * @cmd: the command to be issued
  239. * @params: parameters to the command
  240. *
  241. * Caller should handle locking. This function will issue @cmd with given
  242. * @params to @dep and wait for its completion.
  243. *
  244. * According to the programming guide, if the link state is in L1/L2/U3,
  245. * then sending the Start Transfer command may not complete. The
  246. * programming guide suggested to bring the link state back to ON/U0 by
  247. * performing remote wakeup prior to sending the command. However, don't
  248. * initiate remote wakeup when the user/function does not send wakeup
  249. * request via wakeup ops. Send the command when it's allowed.
  250. *
  251. * Notes:
  252. * For L1 link state, issuing a command requires the clearing of
  253. * GUSB2PHYCFG.SUSPENDUSB2, which turns on the signal required to complete
  254. * the given command (usually within 50us). This should happen within the
  255. * command timeout set by driver. No additional step is needed.
  256. *
  257. * For L2 or U3 link state, the gadget is in USB suspend. Care should be
  258. * taken when sending Start Transfer command to ensure that it's done after
  259. * USB resume.
  260. */
  261. int dwc3_send_gadget_ep_cmd(struct dwc3_ep *dep, unsigned int cmd,
  262. struct dwc3_gadget_ep_cmd_params *params)
  263. {
  264. const struct usb_endpoint_descriptor *desc = dep->endpoint.desc;
  265. struct dwc3 *dwc = dep->dwc;
  266. u32 timeout = 5000;
  267. u32 saved_config = 0;
  268. u32 reg;
  269. int cmd_status = 0;
  270. int ret = -EINVAL;
  271. /*
  272. * When operating in USB 2.0 speeds (HS/FS), if GUSB2PHYCFG.ENBLSLPM or
  273. * GUSB2PHYCFG.SUSPHY is set, it must be cleared before issuing an
  274. * endpoint command.
  275. *
  276. * Save and clear both GUSB2PHYCFG.ENBLSLPM and GUSB2PHYCFG.SUSPHY
  277. * settings. Restore them after the command is completed.
  278. *
  279. * DWC_usb3 3.30a and DWC_usb31 1.90a programming guide section 3.2.2
  280. */
  281. if (dwc->gadget->speed <= USB_SPEED_HIGH ||
  282. DWC3_DEPCMD_CMD(cmd) == DWC3_DEPCMD_ENDTRANSFER) {
  283. reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0));
  284. if (unlikely(reg & DWC3_GUSB2PHYCFG_SUSPHY)) {
  285. saved_config |= DWC3_GUSB2PHYCFG_SUSPHY;
  286. reg &= ~DWC3_GUSB2PHYCFG_SUSPHY;
  287. }
  288. if (reg & DWC3_GUSB2PHYCFG_ENBLSLPM) {
  289. saved_config |= DWC3_GUSB2PHYCFG_ENBLSLPM;
  290. reg &= ~DWC3_GUSB2PHYCFG_ENBLSLPM;
  291. }
  292. if (saved_config)
  293. dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg);
  294. }
  295. /*
  296. * For some commands such as Update Transfer command, DEPCMDPARn
  297. * registers are reserved. Since the driver often sends Update Transfer
  298. * command, don't write to DEPCMDPARn to avoid register write delays and
  299. * improve performance.
  300. */
  301. if (DWC3_DEPCMD_CMD(cmd) != DWC3_DEPCMD_UPDATETRANSFER) {
  302. dwc3_writel(dep->regs, DWC3_DEPCMDPAR0, params->param0);
  303. dwc3_writel(dep->regs, DWC3_DEPCMDPAR1, params->param1);
  304. dwc3_writel(dep->regs, DWC3_DEPCMDPAR2, params->param2);
  305. }
  306. /*
  307. * Synopsys Databook 2.60a states in section 6.3.2.5.6 of that if we're
  308. * not relying on XferNotReady, we can make use of a special "No
  309. * Response Update Transfer" command where we should clear both CmdAct
  310. * and CmdIOC bits.
  311. *
  312. * With this, we don't need to wait for command completion and can
  313. * straight away issue further commands to the endpoint.
  314. *
  315. * NOTICE: We're making an assumption that control endpoints will never
  316. * make use of Update Transfer command. This is a safe assumption
  317. * because we can never have more than one request at a time with
  318. * Control Endpoints. If anybody changes that assumption, this chunk
  319. * needs to be updated accordingly.
  320. */
  321. if (DWC3_DEPCMD_CMD(cmd) == DWC3_DEPCMD_UPDATETRANSFER &&
  322. !usb_endpoint_xfer_isoc(desc))
  323. cmd &= ~(DWC3_DEPCMD_CMDIOC | DWC3_DEPCMD_CMDACT);
  324. else
  325. cmd |= DWC3_DEPCMD_CMDACT;
  326. dwc3_writel(dep->regs, DWC3_DEPCMD, cmd);
  327. if (!(cmd & DWC3_DEPCMD_CMDACT) ||
  328. (DWC3_DEPCMD_CMD(cmd) == DWC3_DEPCMD_ENDTRANSFER &&
  329. !(cmd & DWC3_DEPCMD_CMDIOC))) {
  330. ret = 0;
  331. goto skip_status;
  332. }
  333. do {
  334. reg = dwc3_readl(dep->regs, DWC3_DEPCMD);
  335. if (!(reg & DWC3_DEPCMD_CMDACT)) {
  336. cmd_status = DWC3_DEPCMD_STATUS(reg);
  337. switch (cmd_status) {
  338. case 0:
  339. ret = 0;
  340. break;
  341. case DEPEVT_TRANSFER_NO_RESOURCE:
  342. dev_WARN(dwc->dev, "No resource for %s\n",
  343. dep->name);
  344. ret = -EINVAL;
  345. break;
  346. case DEPEVT_TRANSFER_BUS_EXPIRY:
  347. /*
  348. * SW issues START TRANSFER command to
  349. * isochronous ep with future frame interval. If
  350. * future interval time has already passed when
  351. * core receives the command, it will respond
  352. * with an error status of 'Bus Expiry'.
  353. *
  354. * Instead of always returning -EINVAL, let's
  355. * give a hint to the gadget driver that this is
  356. * the case by returning -EAGAIN.
  357. */
  358. ret = -EAGAIN;
  359. break;
  360. default:
  361. dev_WARN(dwc->dev, "UNKNOWN cmd status\n");
  362. }
  363. break;
  364. }
  365. } while (--timeout);
  366. if (timeout == 0) {
  367. ret = -ETIMEDOUT;
  368. cmd_status = -ETIMEDOUT;
  369. }
  370. skip_status:
  371. trace_dwc3_gadget_ep_cmd(dep, cmd, params, cmd_status);
  372. if (DWC3_DEPCMD_CMD(cmd) == DWC3_DEPCMD_STARTTRANSFER) {
  373. if (ret == 0)
  374. dep->flags |= DWC3_EP_TRANSFER_STARTED;
  375. if (ret != -ETIMEDOUT)
  376. dwc3_gadget_ep_get_transfer_index(dep);
  377. }
  378. if (DWC3_DEPCMD_CMD(cmd) == DWC3_DEPCMD_ENDTRANSFER &&
  379. !(cmd & DWC3_DEPCMD_CMDIOC))
  380. mdelay(1);
  381. if (saved_config) {
  382. reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0));
  383. reg |= saved_config;
  384. dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg);
  385. }
  386. return ret;
  387. }
  388. static int dwc3_send_clear_stall_ep_cmd(struct dwc3_ep *dep)
  389. {
  390. struct dwc3 *dwc = dep->dwc;
  391. struct dwc3_gadget_ep_cmd_params params;
  392. u32 cmd = DWC3_DEPCMD_CLEARSTALL;
  393. /*
  394. * As of core revision 2.60a the recommended programming model
  395. * is to set the ClearPendIN bit when issuing a Clear Stall EP
  396. * command for IN endpoints. This is to prevent an issue where
  397. * some (non-compliant) hosts may not send ACK TPs for pending
  398. * IN transfers due to a mishandled error condition. Synopsys
  399. * STAR 9000614252.
  400. */
  401. if (dep->direction &&
  402. !DWC3_VER_IS_PRIOR(DWC3, 260A) &&
  403. (dwc->gadget->speed >= USB_SPEED_SUPER))
  404. cmd |= DWC3_DEPCMD_CLEARPENDIN;
  405. memset(&params, 0, sizeof(params));
  406. return dwc3_send_gadget_ep_cmd(dep, cmd, &params);
  407. }
  408. static dma_addr_t dwc3_trb_dma_offset(struct dwc3_ep *dep,
  409. struct dwc3_trb *trb)
  410. {
  411. u32 offset = (char *) trb - (char *) dep->trb_pool;
  412. return dep->trb_pool_dma + offset;
  413. }
  414. static int dwc3_alloc_trb_pool(struct dwc3_ep *dep)
  415. {
  416. struct dwc3 *dwc = dep->dwc;
  417. if (dep->trb_pool)
  418. return 0;
  419. dep->trb_pool = dma_alloc_coherent(dwc->sysdev,
  420. sizeof(struct dwc3_trb) * DWC3_TRB_NUM,
  421. &dep->trb_pool_dma, GFP_KERNEL);
  422. if (!dep->trb_pool) {
  423. dev_err(dep->dwc->dev, "failed to allocate trb pool for %s\n",
  424. dep->name);
  425. return -ENOMEM;
  426. }
  427. return 0;
  428. }
  429. static void dwc3_free_trb_pool(struct dwc3_ep *dep)
  430. {
  431. struct dwc3 *dwc = dep->dwc;
  432. dma_free_coherent(dwc->sysdev, sizeof(struct dwc3_trb) * DWC3_TRB_NUM,
  433. dep->trb_pool, dep->trb_pool_dma);
  434. dep->trb_pool = NULL;
  435. dep->trb_pool_dma = 0;
  436. }
  437. static int dwc3_gadget_set_xfer_resource(struct dwc3_ep *dep)
  438. {
  439. struct dwc3_gadget_ep_cmd_params params;
  440. int ret;
  441. if (dep->flags & DWC3_EP_RESOURCE_ALLOCATED)
  442. return 0;
  443. memset(&params, 0x00, sizeof(params));
  444. params.param0 = DWC3_DEPXFERCFG_NUM_XFER_RES(1);
  445. ret = dwc3_send_gadget_ep_cmd(dep, DWC3_DEPCMD_SETTRANSFRESOURCE,
  446. &params);
  447. if (ret)
  448. return ret;
  449. dep->flags |= DWC3_EP_RESOURCE_ALLOCATED;
  450. return 0;
  451. }
  452. /**
  453. * dwc3_gadget_start_config - reset endpoint resources
  454. * @dwc: pointer to the DWC3 context
  455. * @resource_index: DEPSTARTCFG.XferRscIdx value (must be 0 or 2)
  456. *
  457. * Set resource_index=0 to reset all endpoints' resources allocation. Do this as
  458. * part of the power-on/soft-reset initialization.
  459. *
  460. * Set resource_index=2 to reset only non-control endpoints' resources. Do this
  461. * on receiving the SET_CONFIGURATION request or hibernation resume.
  462. */
  463. int dwc3_gadget_start_config(struct dwc3 *dwc, unsigned int resource_index)
  464. {
  465. struct dwc3_gadget_ep_cmd_params params;
  466. u32 cmd;
  467. int i;
  468. int ret;
  469. if (resource_index != 0 && resource_index != 2)
  470. return -EINVAL;
  471. memset(&params, 0x00, sizeof(params));
  472. cmd = DWC3_DEPCMD_DEPSTARTCFG;
  473. cmd |= DWC3_DEPCMD_PARAM(resource_index);
  474. ret = dwc3_send_gadget_ep_cmd(dwc->eps[0], cmd, &params);
  475. if (ret)
  476. return ret;
  477. /* Reset resource allocation flags */
  478. for (i = resource_index; i < dwc->num_eps && dwc->eps[i]; i++)
  479. dwc->eps[i]->flags &= ~DWC3_EP_RESOURCE_ALLOCATED;
  480. return 0;
  481. }
  482. static int dwc3_gadget_set_ep_config(struct dwc3_ep *dep, unsigned int action)
  483. {
  484. const struct usb_ss_ep_comp_descriptor *comp_desc;
  485. const struct usb_endpoint_descriptor *desc;
  486. struct dwc3_gadget_ep_cmd_params params;
  487. struct dwc3 *dwc = dep->dwc;
  488. comp_desc = dep->endpoint.comp_desc;
  489. desc = dep->endpoint.desc;
  490. memset(&params, 0x00, sizeof(params));
  491. params.param0 = DWC3_DEPCFG_EP_TYPE(usb_endpoint_type(desc))
  492. | DWC3_DEPCFG_MAX_PACKET_SIZE(usb_endpoint_maxp(desc));
  493. /* Burst size is only needed in SuperSpeed mode */
  494. if (dwc->gadget->speed >= USB_SPEED_SUPER) {
  495. u32 burst = dep->endpoint.maxburst;
  496. params.param0 |= DWC3_DEPCFG_BURST_SIZE(burst - 1);
  497. }
  498. params.param0 |= action;
  499. if (action == DWC3_DEPCFG_ACTION_RESTORE)
  500. params.param2 |= dep->saved_state;
  501. if (usb_endpoint_xfer_control(desc))
  502. params.param1 = DWC3_DEPCFG_XFER_COMPLETE_EN;
  503. if (dep->number <= 1 || usb_endpoint_xfer_isoc(desc))
  504. params.param1 |= DWC3_DEPCFG_XFER_NOT_READY_EN;
  505. if (usb_ss_max_streams(comp_desc) && usb_endpoint_xfer_bulk(desc)) {
  506. params.param1 |= DWC3_DEPCFG_STREAM_CAPABLE
  507. | DWC3_DEPCFG_XFER_COMPLETE_EN
  508. | DWC3_DEPCFG_STREAM_EVENT_EN;
  509. dep->stream_capable = true;
  510. }
  511. if (!usb_endpoint_xfer_control(desc))
  512. params.param1 |= DWC3_DEPCFG_XFER_IN_PROGRESS_EN;
  513. /*
  514. * We are doing 1:1 mapping for endpoints, meaning
  515. * Physical Endpoints 2 maps to Logical Endpoint 2 and
  516. * so on. We consider the direction bit as part of the physical
  517. * endpoint number. So USB endpoint 0x81 is 0x03.
  518. */
  519. params.param1 |= DWC3_DEPCFG_EP_NUMBER(dep->number);
  520. /*
  521. * We must use the lower 16 TX FIFOs even though
  522. * HW might have more
  523. */
  524. if (dep->direction)
  525. params.param0 |= DWC3_DEPCFG_FIFO_NUMBER(dep->number >> 1);
  526. if (desc->bInterval) {
  527. u8 bInterval_m1;
  528. /*
  529. * Valid range for DEPCFG.bInterval_m1 is from 0 to 13.
  530. *
  531. * NOTE: The programming guide incorrectly stated bInterval_m1
  532. * must be set to 0 when operating in fullspeed. Internally the
  533. * controller does not have this limitation. See DWC_usb3x
  534. * programming guide section 3.2.2.1.
  535. */
  536. bInterval_m1 = min_t(u8, desc->bInterval - 1, 13);
  537. if (usb_endpoint_type(desc) == USB_ENDPOINT_XFER_INT &&
  538. dwc->gadget->speed == USB_SPEED_FULL)
  539. dep->interval = desc->bInterval;
  540. else
  541. dep->interval = 1 << (desc->bInterval - 1);
  542. params.param1 |= DWC3_DEPCFG_BINTERVAL_M1(bInterval_m1);
  543. }
  544. return dwc3_send_gadget_ep_cmd(dep, DWC3_DEPCMD_SETEPCONFIG, &params);
  545. }
  546. /**
  547. * dwc3_gadget_calc_tx_fifo_size - calculates the txfifo size value
  548. * @dwc: pointer to the DWC3 context
  549. * @mult: multiplier to be used when calculating the fifo_size
  550. *
  551. * Calculates the size value based on the equation below:
  552. *
  553. * DWC3 revision 280A and prior:
  554. * fifo_size = mult * (max_packet / mdwidth) + 1;
  555. *
  556. * DWC3 revision 290A and onwards:
  557. * fifo_size = mult * ((max_packet + mdwidth)/mdwidth + 1) + 1
  558. *
  559. * The max packet size is set to 1024, as the txfifo requirements mainly apply
  560. * to super speed USB use cases. However, it is safe to overestimate the fifo
  561. * allocations for other scenarios, i.e. high speed USB.
  562. */
  563. static int dwc3_gadget_calc_tx_fifo_size(struct dwc3 *dwc, int mult)
  564. {
  565. int max_packet = 1024;
  566. int fifo_size;
  567. int mdwidth;
  568. mdwidth = dwc3_mdwidth(dwc);
  569. /* MDWIDTH is represented in bits, we need it in bytes */
  570. mdwidth >>= 3;
  571. if (DWC3_VER_IS_PRIOR(DWC3, 290A))
  572. fifo_size = mult * (max_packet / mdwidth) + 1;
  573. else
  574. fifo_size = mult * ((max_packet + mdwidth) / mdwidth) + 1;
  575. return fifo_size;
  576. }
  577. /**
  578. * dwc3_gadget_calc_ram_depth - calculates the ram depth for txfifo
  579. * @dwc: pointer to the DWC3 context
  580. */
  581. static int dwc3_gadget_calc_ram_depth(struct dwc3 *dwc)
  582. {
  583. int ram_depth;
  584. int fifo_0_start;
  585. bool is_single_port_ram;
  586. /* Check supporting RAM type by HW */
  587. is_single_port_ram = DWC3_SPRAM_TYPE(dwc->hwparams.hwparams1);
  588. /*
  589. * If a single port RAM is utilized, then allocate TxFIFOs from
  590. * RAM0. otherwise, allocate them from RAM1.
  591. */
  592. ram_depth = is_single_port_ram ? DWC3_RAM0_DEPTH(dwc->hwparams.hwparams6) :
  593. DWC3_RAM1_DEPTH(dwc->hwparams.hwparams7);
  594. /*
  595. * In a single port RAM configuration, the available RAM is shared
  596. * between the RX and TX FIFOs. This means that the txfifo can begin
  597. * at a non-zero address.
  598. */
  599. if (is_single_port_ram) {
  600. u32 reg;
  601. /* Check if TXFIFOs start at non-zero addr */
  602. reg = dwc3_readl(dwc->regs, DWC3_GTXFIFOSIZ(0));
  603. fifo_0_start = DWC3_GTXFIFOSIZ_TXFSTADDR(reg);
  604. ram_depth -= (fifo_0_start >> 16);
  605. }
  606. return ram_depth;
  607. }
  608. /**
  609. * dwc3_gadget_clear_tx_fifos - Clears txfifo allocation
  610. * @dwc: pointer to the DWC3 context
  611. *
  612. * Iterates through all the endpoint registers and clears the previous txfifo
  613. * allocations.
  614. */
  615. void dwc3_gadget_clear_tx_fifos(struct dwc3 *dwc)
  616. {
  617. struct dwc3_ep *dep;
  618. int fifo_depth;
  619. int size;
  620. int num;
  621. if (!dwc->do_fifo_resize)
  622. return;
  623. /* Read ep0IN related TXFIFO size */
  624. dep = dwc->eps[1];
  625. size = dwc3_readl(dwc->regs, DWC3_GTXFIFOSIZ(0));
  626. if (DWC3_IP_IS(DWC3))
  627. fifo_depth = DWC3_GTXFIFOSIZ_TXFDEP(size);
  628. else
  629. fifo_depth = DWC31_GTXFIFOSIZ_TXFDEP(size);
  630. dwc->last_fifo_depth = fifo_depth;
  631. /* Clear existing TXFIFO for all IN eps except ep0 */
  632. for (num = 3; num < min_t(int, dwc->num_eps, DWC3_ENDPOINTS_NUM);
  633. num += 2) {
  634. dep = dwc->eps[num];
  635. /* Don't change TXFRAMNUM on usb31 version */
  636. size = DWC3_IP_IS(DWC3) ? 0 :
  637. dwc3_readl(dwc->regs, DWC3_GTXFIFOSIZ(num >> 1)) &
  638. DWC31_GTXFIFOSIZ_TXFRAMNUM;
  639. dwc3_writel(dwc->regs, DWC3_GTXFIFOSIZ(num >> 1), size);
  640. dep->flags &= ~DWC3_EP_TXFIFO_RESIZED;
  641. }
  642. dwc->num_ep_resized = 0;
  643. }
  644. /*
  645. * dwc3_gadget_resize_tx_fifos - reallocate fifo spaces for current use-case
  646. * @dwc: pointer to our context structure
  647. *
  648. * This function will a best effort FIFO allocation in order
  649. * to improve FIFO usage and throughput, while still allowing
  650. * us to enable as many endpoints as possible.
  651. *
  652. * Keep in mind that this operation will be highly dependent
  653. * on the configured size for RAM1 - which contains TxFifo -,
  654. * the amount of endpoints enabled on coreConsultant tool, and
  655. * the width of the Master Bus.
  656. *
  657. * In general, FIFO depths are represented with the following equation:
  658. *
  659. * fifo_size = mult * ((max_packet + mdwidth)/mdwidth + 1) + 1
  660. *
  661. * In conjunction with dwc3_gadget_check_config(), this resizing logic will
  662. * ensure that all endpoints will have enough internal memory for one max
  663. * packet per endpoint.
  664. */
  665. static int dwc3_gadget_resize_tx_fifos(struct dwc3_ep *dep)
  666. {
  667. struct dwc3 *dwc = dep->dwc;
  668. int fifo_0_start;
  669. int ram_depth;
  670. int fifo_size;
  671. int min_depth;
  672. int num_in_ep;
  673. int remaining;
  674. int num_fifos = 1;
  675. int fifo;
  676. int tmp;
  677. if (!dwc->do_fifo_resize)
  678. return 0;
  679. /* resize IN endpoints except ep0 */
  680. if (!usb_endpoint_dir_in(dep->endpoint.desc) || dep->number <= 1)
  681. return 0;
  682. /* bail if already resized */
  683. if (dep->flags & DWC3_EP_TXFIFO_RESIZED)
  684. return 0;
  685. ram_depth = dwc3_gadget_calc_ram_depth(dwc);
  686. if ((dep->endpoint.maxburst > 1 &&
  687. usb_endpoint_xfer_bulk(dep->endpoint.desc)) ||
  688. usb_endpoint_xfer_isoc(dep->endpoint.desc))
  689. num_fifos = 3;
  690. if (dep->endpoint.maxburst > 6 &&
  691. (usb_endpoint_xfer_bulk(dep->endpoint.desc) ||
  692. usb_endpoint_xfer_isoc(dep->endpoint.desc)) && DWC3_IP_IS(DWC31))
  693. num_fifos = dwc->tx_fifo_resize_max_num;
  694. /* FIFO size for a single buffer */
  695. fifo = dwc3_gadget_calc_tx_fifo_size(dwc, 1);
  696. /* Calculate the number of remaining EPs w/o any FIFO */
  697. num_in_ep = dwc->max_cfg_eps;
  698. num_in_ep -= dwc->num_ep_resized;
  699. /* Reserve at least one FIFO for the number of IN EPs */
  700. min_depth = num_in_ep * (fifo + 1);
  701. remaining = ram_depth - min_depth - dwc->last_fifo_depth;
  702. remaining = max_t(int, 0, remaining);
  703. /*
  704. * We've already reserved 1 FIFO per EP, so check what we can fit in
  705. * addition to it. If there is not enough remaining space, allocate
  706. * all the remaining space to the EP.
  707. */
  708. fifo_size = (num_fifos - 1) * fifo;
  709. if (remaining < fifo_size)
  710. fifo_size = remaining;
  711. fifo_size += fifo;
  712. /* Last increment according to the TX FIFO size equation */
  713. fifo_size++;
  714. /* Check if TXFIFOs start at non-zero addr */
  715. tmp = dwc3_readl(dwc->regs, DWC3_GTXFIFOSIZ(0));
  716. fifo_0_start = DWC3_GTXFIFOSIZ_TXFSTADDR(tmp);
  717. fifo_size |= (fifo_0_start + (dwc->last_fifo_depth << 16));
  718. if (DWC3_IP_IS(DWC3))
  719. dwc->last_fifo_depth += DWC3_GTXFIFOSIZ_TXFDEP(fifo_size);
  720. else
  721. dwc->last_fifo_depth += DWC31_GTXFIFOSIZ_TXFDEP(fifo_size);
  722. /* Check fifo size allocation doesn't exceed available RAM size. */
  723. if (dwc->last_fifo_depth >= ram_depth) {
  724. dev_err(dwc->dev, "Fifosize(%d) > RAM size(%d) %s depth:%d\n",
  725. dwc->last_fifo_depth, ram_depth,
  726. dep->endpoint.name, fifo_size);
  727. if (DWC3_IP_IS(DWC3))
  728. fifo_size = DWC3_GTXFIFOSIZ_TXFDEP(fifo_size);
  729. else
  730. fifo_size = DWC31_GTXFIFOSIZ_TXFDEP(fifo_size);
  731. dwc->last_fifo_depth -= fifo_size;
  732. return -ENOMEM;
  733. }
  734. dwc3_writel(dwc->regs, DWC3_GTXFIFOSIZ(dep->number >> 1), fifo_size);
  735. dep->flags |= DWC3_EP_TXFIFO_RESIZED;
  736. dwc->num_ep_resized++;
  737. return 0;
  738. }
  739. /**
  740. * __dwc3_gadget_ep_enable - initializes a hw endpoint
  741. * @dep: endpoint to be initialized
  742. * @action: one of INIT, MODIFY or RESTORE
  743. *
  744. * Caller should take care of locking. Execute all necessary commands to
  745. * initialize a HW endpoint so it can be used by a gadget driver.
  746. */
  747. static int __dwc3_gadget_ep_enable(struct dwc3_ep *dep, unsigned int action)
  748. {
  749. const struct usb_endpoint_descriptor *desc = dep->endpoint.desc;
  750. struct dwc3 *dwc = dep->dwc;
  751. u32 reg;
  752. int ret;
  753. if (!(dep->flags & DWC3_EP_ENABLED)) {
  754. ret = dwc3_gadget_resize_tx_fifos(dep);
  755. if (ret)
  756. return ret;
  757. }
  758. ret = dwc3_gadget_set_ep_config(dep, action);
  759. if (ret)
  760. return ret;
  761. if (!(dep->flags & DWC3_EP_RESOURCE_ALLOCATED)) {
  762. ret = dwc3_gadget_set_xfer_resource(dep);
  763. if (ret)
  764. return ret;
  765. }
  766. if (!(dep->flags & DWC3_EP_ENABLED)) {
  767. struct dwc3_trb *trb_st_hw;
  768. struct dwc3_trb *trb_link;
  769. dep->type = usb_endpoint_type(desc);
  770. dep->flags |= DWC3_EP_ENABLED;
  771. reg = dwc3_readl(dwc->regs, DWC3_DALEPENA);
  772. reg |= DWC3_DALEPENA_EP(dep->number);
  773. dwc3_writel(dwc->regs, DWC3_DALEPENA, reg);
  774. dep->trb_dequeue = 0;
  775. dep->trb_enqueue = 0;
  776. if (usb_endpoint_xfer_control(desc))
  777. goto out;
  778. /* Initialize the TRB ring */
  779. memset(dep->trb_pool, 0,
  780. sizeof(struct dwc3_trb) * DWC3_TRB_NUM);
  781. /* Link TRB. The HWO bit is never reset */
  782. trb_st_hw = &dep->trb_pool[0];
  783. trb_link = &dep->trb_pool[DWC3_TRB_NUM - 1];
  784. trb_link->bpl = lower_32_bits(dwc3_trb_dma_offset(dep, trb_st_hw));
  785. trb_link->bph = upper_32_bits(dwc3_trb_dma_offset(dep, trb_st_hw));
  786. trb_link->ctrl |= DWC3_TRBCTL_LINK_TRB;
  787. trb_link->ctrl |= DWC3_TRB_CTRL_HWO;
  788. }
  789. /*
  790. * Issue StartTransfer here with no-op TRB so we can always rely on No
  791. * Response Update Transfer command.
  792. */
  793. if (usb_endpoint_xfer_bulk(desc) ||
  794. usb_endpoint_xfer_int(desc)) {
  795. struct dwc3_gadget_ep_cmd_params params;
  796. struct dwc3_trb *trb;
  797. dma_addr_t trb_dma;
  798. u32 cmd;
  799. memset(&params, 0, sizeof(params));
  800. trb = &dep->trb_pool[0];
  801. trb_dma = dwc3_trb_dma_offset(dep, trb);
  802. params.param0 = upper_32_bits(trb_dma);
  803. params.param1 = lower_32_bits(trb_dma);
  804. cmd = DWC3_DEPCMD_STARTTRANSFER;
  805. ret = dwc3_send_gadget_ep_cmd(dep, cmd, &params);
  806. if (ret < 0)
  807. return ret;
  808. if (dep->stream_capable) {
  809. /*
  810. * For streams, at start, there maybe a race where the
  811. * host primes the endpoint before the function driver
  812. * queues a request to initiate a stream. In that case,
  813. * the controller will not see the prime to generate the
  814. * ERDY and start stream. To workaround this, issue a
  815. * no-op TRB as normal, but end it immediately. As a
  816. * result, when the function driver queues the request,
  817. * the next START_TRANSFER command will cause the
  818. * controller to generate an ERDY to initiate the
  819. * stream.
  820. */
  821. dwc3_stop_active_transfer(dep, true, true);
  822. /*
  823. * All stream eps will reinitiate stream on NoStream
  824. * rejection until we can determine that the host can
  825. * prime after the first transfer.
  826. *
  827. * However, if the controller is capable of
  828. * TXF_FLUSH_BYPASS, then IN direction endpoints will
  829. * automatically restart the stream without the driver
  830. * initiation.
  831. */
  832. if (!dep->direction ||
  833. !(dwc->hwparams.hwparams9 &
  834. DWC3_GHWPARAMS9_DEV_TXF_FLUSH_BYPASS))
  835. dep->flags |= DWC3_EP_FORCE_RESTART_STREAM;
  836. }
  837. }
  838. out:
  839. trace_dwc3_gadget_ep_enable(dep);
  840. return 0;
  841. }
  842. void dwc3_remove_requests(struct dwc3 *dwc, struct dwc3_ep *dep, int status)
  843. {
  844. struct dwc3_request *req;
  845. dwc3_stop_active_transfer(dep, true, false);
  846. /* If endxfer is delayed, avoid unmapping requests */
  847. if (dep->flags & DWC3_EP_DELAY_STOP)
  848. return;
  849. /* - giveback all requests to gadget driver */
  850. while (!list_empty(&dep->started_list)) {
  851. req = next_request(&dep->started_list);
  852. dwc3_gadget_giveback(dep, req, status);
  853. }
  854. while (!list_empty(&dep->pending_list)) {
  855. req = next_request(&dep->pending_list);
  856. dwc3_gadget_giveback(dep, req, status);
  857. }
  858. while (!list_empty(&dep->cancelled_list)) {
  859. req = next_request(&dep->cancelled_list);
  860. dwc3_gadget_giveback(dep, req, status);
  861. }
  862. }
  863. /**
  864. * __dwc3_gadget_ep_disable - disables a hw endpoint
  865. * @dep: the endpoint to disable
  866. *
  867. * This function undoes what __dwc3_gadget_ep_enable did and also removes
  868. * requests which are currently being processed by the hardware and those which
  869. * are not yet scheduled.
  870. *
  871. * Caller should take care of locking.
  872. */
  873. static int __dwc3_gadget_ep_disable(struct dwc3_ep *dep)
  874. {
  875. struct dwc3 *dwc = dep->dwc;
  876. u32 reg;
  877. u32 mask;
  878. trace_dwc3_gadget_ep_disable(dep);
  879. /* make sure HW endpoint isn't stalled */
  880. if (dep->flags & DWC3_EP_STALL)
  881. __dwc3_gadget_ep_set_halt(dep, 0, false);
  882. reg = dwc3_readl(dwc->regs, DWC3_DALEPENA);
  883. reg &= ~DWC3_DALEPENA_EP(dep->number);
  884. dwc3_writel(dwc->regs, DWC3_DALEPENA, reg);
  885. dwc3_remove_requests(dwc, dep, -ESHUTDOWN);
  886. dep->stream_capable = false;
  887. dep->type = 0;
  888. mask = DWC3_EP_TXFIFO_RESIZED | DWC3_EP_RESOURCE_ALLOCATED;
  889. /*
  890. * dwc3_remove_requests() can exit early if DWC3 EP delayed stop is
  891. * set. Do not clear DEP flags, so that the end transfer command will
  892. * be reattempted during the next SETUP stage.
  893. */
  894. if (dep->flags & DWC3_EP_DELAY_STOP)
  895. mask |= (DWC3_EP_DELAY_STOP | DWC3_EP_TRANSFER_STARTED);
  896. dep->flags &= mask;
  897. /* Clear out the ep descriptors for non-ep0 */
  898. if (dep->number > 1) {
  899. dep->endpoint.comp_desc = NULL;
  900. dep->endpoint.desc = NULL;
  901. }
  902. return 0;
  903. }
  904. /* -------------------------------------------------------------------------- */
  905. static int dwc3_gadget_ep0_enable(struct usb_ep *ep,
  906. const struct usb_endpoint_descriptor *desc)
  907. {
  908. return -EINVAL;
  909. }
  910. static int dwc3_gadget_ep0_disable(struct usb_ep *ep)
  911. {
  912. return -EINVAL;
  913. }
  914. /* -------------------------------------------------------------------------- */
  915. static int dwc3_gadget_ep_enable(struct usb_ep *ep,
  916. const struct usb_endpoint_descriptor *desc)
  917. {
  918. struct dwc3_ep *dep;
  919. struct dwc3 *dwc;
  920. unsigned long flags;
  921. int ret;
  922. if (!ep || !desc || desc->bDescriptorType != USB_DT_ENDPOINT) {
  923. pr_debug("dwc3: invalid parameters\n");
  924. return -EINVAL;
  925. }
  926. if (!desc->wMaxPacketSize) {
  927. pr_debug("dwc3: missing wMaxPacketSize\n");
  928. return -EINVAL;
  929. }
  930. dep = to_dwc3_ep(ep);
  931. dwc = dep->dwc;
  932. if (dev_WARN_ONCE(dwc->dev, dep->flags & DWC3_EP_ENABLED,
  933. "%s is already enabled\n",
  934. dep->name))
  935. return 0;
  936. spin_lock_irqsave(&dwc->lock, flags);
  937. ret = __dwc3_gadget_ep_enable(dep, DWC3_DEPCFG_ACTION_INIT);
  938. spin_unlock_irqrestore(&dwc->lock, flags);
  939. return ret;
  940. }
  941. static int dwc3_gadget_ep_disable(struct usb_ep *ep)
  942. {
  943. struct dwc3_ep *dep;
  944. struct dwc3 *dwc;
  945. unsigned long flags;
  946. int ret;
  947. if (!ep) {
  948. pr_debug("dwc3: invalid parameters\n");
  949. return -EINVAL;
  950. }
  951. dep = to_dwc3_ep(ep);
  952. dwc = dep->dwc;
  953. if (dev_WARN_ONCE(dwc->dev, !(dep->flags & DWC3_EP_ENABLED),
  954. "%s is already disabled\n",
  955. dep->name))
  956. return 0;
  957. spin_lock_irqsave(&dwc->lock, flags);
  958. ret = __dwc3_gadget_ep_disable(dep);
  959. spin_unlock_irqrestore(&dwc->lock, flags);
  960. return ret;
  961. }
  962. static struct usb_request *dwc3_gadget_ep_alloc_request(struct usb_ep *ep,
  963. gfp_t gfp_flags)
  964. {
  965. struct dwc3_request *req;
  966. struct dwc3_ep *dep = to_dwc3_ep(ep);
  967. req = kzalloc(sizeof(*req), gfp_flags);
  968. if (!req)
  969. return NULL;
  970. req->direction = dep->direction;
  971. req->epnum = dep->number;
  972. req->dep = dep;
  973. req->status = DWC3_REQUEST_STATUS_UNKNOWN;
  974. trace_dwc3_alloc_request(req);
  975. return &req->request;
  976. }
  977. static void dwc3_gadget_ep_free_request(struct usb_ep *ep,
  978. struct usb_request *request)
  979. {
  980. struct dwc3_request *req = to_dwc3_request(request);
  981. trace_dwc3_free_request(req);
  982. kfree(req);
  983. }
  984. /**
  985. * dwc3_ep_prev_trb - returns the previous TRB in the ring
  986. * @dep: The endpoint with the TRB ring
  987. * @index: The index of the current TRB in the ring
  988. *
  989. * Returns the TRB prior to the one pointed to by the index. If the
  990. * index is 0, we will wrap backwards, skip the link TRB, and return
  991. * the one just before that.
  992. */
  993. static struct dwc3_trb *dwc3_ep_prev_trb(struct dwc3_ep *dep, u8 index)
  994. {
  995. u8 tmp = index;
  996. if (!tmp)
  997. tmp = DWC3_TRB_NUM - 1;
  998. return &dep->trb_pool[tmp - 1];
  999. }
  1000. static u32 dwc3_calc_trbs_left(struct dwc3_ep *dep)
  1001. {
  1002. u8 trbs_left;
  1003. /*
  1004. * If the enqueue & dequeue are equal then the TRB ring is either full
  1005. * or empty. It's considered full when there are DWC3_TRB_NUM-1 of TRBs
  1006. * pending to be processed by the driver.
  1007. */
  1008. if (dep->trb_enqueue == dep->trb_dequeue) {
  1009. struct dwc3_request *req;
  1010. /*
  1011. * If there is any request remained in the started_list with
  1012. * active TRBs at this point, then there is no TRB available.
  1013. */
  1014. req = next_request(&dep->started_list);
  1015. if (req && req->num_trbs)
  1016. return 0;
  1017. return DWC3_TRB_NUM - 1;
  1018. }
  1019. trbs_left = dep->trb_dequeue - dep->trb_enqueue;
  1020. trbs_left &= (DWC3_TRB_NUM - 1);
  1021. if (dep->trb_dequeue < dep->trb_enqueue)
  1022. trbs_left--;
  1023. return trbs_left;
  1024. }
  1025. /**
  1026. * dwc3_prepare_one_trb - setup one TRB from one request
  1027. * @dep: endpoint for which this request is prepared
  1028. * @req: dwc3_request pointer
  1029. * @trb_length: buffer size of the TRB
  1030. * @chain: should this TRB be chained to the next?
  1031. * @node: only for isochronous endpoints. First TRB needs different type.
  1032. * @use_bounce_buffer: set to use bounce buffer
  1033. * @must_interrupt: set to interrupt on TRB completion
  1034. */
  1035. static void dwc3_prepare_one_trb(struct dwc3_ep *dep,
  1036. struct dwc3_request *req, unsigned int trb_length,
  1037. unsigned int chain, unsigned int node, bool use_bounce_buffer,
  1038. bool must_interrupt)
  1039. {
  1040. struct dwc3_trb *trb;
  1041. dma_addr_t dma;
  1042. unsigned int stream_id = req->request.stream_id;
  1043. unsigned int short_not_ok = req->request.short_not_ok;
  1044. unsigned int no_interrupt = req->request.no_interrupt;
  1045. unsigned int is_last = req->request.is_last;
  1046. struct dwc3 *dwc = dep->dwc;
  1047. struct usb_gadget *gadget = dwc->gadget;
  1048. enum usb_device_speed speed = gadget->speed;
  1049. if (use_bounce_buffer)
  1050. dma = dep->dwc->bounce_addr;
  1051. else if (req->request.num_sgs > 0)
  1052. dma = sg_dma_address(req->start_sg);
  1053. else
  1054. dma = req->request.dma;
  1055. trb = &dep->trb_pool[dep->trb_enqueue];
  1056. if (!req->trb) {
  1057. dwc3_gadget_move_started_request(req);
  1058. req->trb = trb;
  1059. req->trb_dma = dwc3_trb_dma_offset(dep, trb);
  1060. }
  1061. req->num_trbs++;
  1062. trb->size = DWC3_TRB_SIZE_LENGTH(trb_length);
  1063. trb->bpl = lower_32_bits(dma);
  1064. trb->bph = upper_32_bits(dma);
  1065. switch (usb_endpoint_type(dep->endpoint.desc)) {
  1066. case USB_ENDPOINT_XFER_CONTROL:
  1067. trb->ctrl = DWC3_TRBCTL_CONTROL_SETUP;
  1068. break;
  1069. case USB_ENDPOINT_XFER_ISOC:
  1070. if (!node) {
  1071. trb->ctrl = DWC3_TRBCTL_ISOCHRONOUS_FIRST;
  1072. /*
  1073. * USB Specification 2.0 Section 5.9.2 states that: "If
  1074. * there is only a single transaction in the microframe,
  1075. * only a DATA0 data packet PID is used. If there are
  1076. * two transactions per microframe, DATA1 is used for
  1077. * the first transaction data packet and DATA0 is used
  1078. * for the second transaction data packet. If there are
  1079. * three transactions per microframe, DATA2 is used for
  1080. * the first transaction data packet, DATA1 is used for
  1081. * the second, and DATA0 is used for the third."
  1082. *
  1083. * IOW, we should satisfy the following cases:
  1084. *
  1085. * 1) length <= maxpacket
  1086. * - DATA0
  1087. *
  1088. * 2) maxpacket < length <= (2 * maxpacket)
  1089. * - DATA1, DATA0
  1090. *
  1091. * 3) (2 * maxpacket) < length <= (3 * maxpacket)
  1092. * - DATA2, DATA1, DATA0
  1093. */
  1094. if (speed == USB_SPEED_HIGH) {
  1095. struct usb_ep *ep = &dep->endpoint;
  1096. unsigned int mult = 2;
  1097. unsigned int maxp = usb_endpoint_maxp(ep->desc);
  1098. if (req->request.length <= (2 * maxp))
  1099. mult--;
  1100. if (req->request.length <= maxp)
  1101. mult--;
  1102. trb->size |= DWC3_TRB_SIZE_PCM1(mult);
  1103. }
  1104. } else {
  1105. trb->ctrl = DWC3_TRBCTL_ISOCHRONOUS;
  1106. }
  1107. if (!no_interrupt && !chain)
  1108. trb->ctrl |= DWC3_TRB_CTRL_ISP_IMI;
  1109. break;
  1110. case USB_ENDPOINT_XFER_BULK:
  1111. case USB_ENDPOINT_XFER_INT:
  1112. trb->ctrl = DWC3_TRBCTL_NORMAL;
  1113. break;
  1114. default:
  1115. /*
  1116. * This is only possible with faulty memory because we
  1117. * checked it already :)
  1118. */
  1119. dev_WARN(dwc->dev, "Unknown endpoint type %d\n",
  1120. usb_endpoint_type(dep->endpoint.desc));
  1121. }
  1122. /*
  1123. * Enable Continue on Short Packet
  1124. * when endpoint is not a stream capable
  1125. */
  1126. if (usb_endpoint_dir_out(dep->endpoint.desc)) {
  1127. if (!dep->stream_capable)
  1128. trb->ctrl |= DWC3_TRB_CTRL_CSP;
  1129. if (short_not_ok)
  1130. trb->ctrl |= DWC3_TRB_CTRL_ISP_IMI;
  1131. }
  1132. /* All TRBs setup for MST must set CSP=1 when LST=0 */
  1133. if (dep->stream_capable && DWC3_MST_CAPABLE(&dwc->hwparams))
  1134. trb->ctrl |= DWC3_TRB_CTRL_CSP;
  1135. if ((!no_interrupt && !chain) || must_interrupt)
  1136. trb->ctrl |= DWC3_TRB_CTRL_IOC;
  1137. if (chain)
  1138. trb->ctrl |= DWC3_TRB_CTRL_CHN;
  1139. else if (dep->stream_capable && is_last &&
  1140. !DWC3_MST_CAPABLE(&dwc->hwparams))
  1141. trb->ctrl |= DWC3_TRB_CTRL_LST;
  1142. if (usb_endpoint_xfer_bulk(dep->endpoint.desc) && dep->stream_capable)
  1143. trb->ctrl |= DWC3_TRB_CTRL_SID_SOFN(stream_id);
  1144. /*
  1145. * As per data book 4.2.3.2TRB Control Bit Rules section
  1146. *
  1147. * The controller autonomously checks the HWO field of a TRB to determine if the
  1148. * entire TRB is valid. Therefore, software must ensure that the rest of the TRB
  1149. * is valid before setting the HWO field to '1'. In most systems, this means that
  1150. * software must update the fourth DWORD of a TRB last.
  1151. *
  1152. * However there is a possibility of CPU re-ordering here which can cause
  1153. * controller to observe the HWO bit set prematurely.
  1154. * Add a write memory barrier to prevent CPU re-ordering.
  1155. */
  1156. wmb();
  1157. trb->ctrl |= DWC3_TRB_CTRL_HWO;
  1158. dwc3_ep_inc_enq(dep);
  1159. trace_dwc3_prepare_trb(dep, trb);
  1160. }
  1161. static bool dwc3_needs_extra_trb(struct dwc3_ep *dep, struct dwc3_request *req)
  1162. {
  1163. unsigned int maxp = usb_endpoint_maxp(dep->endpoint.desc);
  1164. unsigned int rem = req->request.length % maxp;
  1165. if ((req->request.length && req->request.zero && !rem &&
  1166. !usb_endpoint_xfer_isoc(dep->endpoint.desc)) ||
  1167. (!req->direction && rem))
  1168. return true;
  1169. return false;
  1170. }
  1171. /**
  1172. * dwc3_prepare_last_sg - prepare TRBs for the last SG entry
  1173. * @dep: The endpoint that the request belongs to
  1174. * @req: The request to prepare
  1175. * @entry_length: The last SG entry size
  1176. * @node: Indicates whether this is not the first entry (for isoc only)
  1177. *
  1178. * Return the number of TRBs prepared.
  1179. */
  1180. static int dwc3_prepare_last_sg(struct dwc3_ep *dep,
  1181. struct dwc3_request *req, unsigned int entry_length,
  1182. unsigned int node)
  1183. {
  1184. unsigned int maxp = usb_endpoint_maxp(dep->endpoint.desc);
  1185. unsigned int rem = req->request.length % maxp;
  1186. unsigned int num_trbs = 1;
  1187. if (dwc3_needs_extra_trb(dep, req))
  1188. num_trbs++;
  1189. if (dwc3_calc_trbs_left(dep) < num_trbs)
  1190. return 0;
  1191. req->needs_extra_trb = num_trbs > 1;
  1192. /* Prepare a normal TRB */
  1193. if (req->direction || req->request.length)
  1194. dwc3_prepare_one_trb(dep, req, entry_length,
  1195. req->needs_extra_trb, node, false, false);
  1196. /* Prepare extra TRBs for ZLP and MPS OUT transfer alignment */
  1197. if ((!req->direction && !req->request.length) || req->needs_extra_trb)
  1198. dwc3_prepare_one_trb(dep, req,
  1199. req->direction ? 0 : maxp - rem,
  1200. false, 1, true, false);
  1201. return num_trbs;
  1202. }
  1203. static int dwc3_prepare_trbs_sg(struct dwc3_ep *dep,
  1204. struct dwc3_request *req)
  1205. {
  1206. struct scatterlist *sg = req->start_sg;
  1207. struct scatterlist *s;
  1208. int i;
  1209. unsigned int length = req->request.length;
  1210. unsigned int remaining = req->num_pending_sgs;
  1211. unsigned int num_queued_sgs = req->request.num_mapped_sgs - remaining;
  1212. unsigned int num_trbs = req->num_trbs;
  1213. bool needs_extra_trb = dwc3_needs_extra_trb(dep, req);
  1214. /*
  1215. * If we resume preparing the request, then get the remaining length of
  1216. * the request and resume where we left off.
  1217. */
  1218. for_each_sg(req->request.sg, s, num_queued_sgs, i)
  1219. length -= sg_dma_len(s);
  1220. for_each_sg(sg, s, remaining, i) {
  1221. unsigned int num_trbs_left = dwc3_calc_trbs_left(dep);
  1222. unsigned int trb_length;
  1223. bool must_interrupt = false;
  1224. bool last_sg = false;
  1225. trb_length = min_t(unsigned int, length, sg_dma_len(s));
  1226. length -= trb_length;
  1227. /*
  1228. * IOMMU driver is coalescing the list of sgs which shares a
  1229. * page boundary into one and giving it to USB driver. With
  1230. * this the number of sgs mapped is not equal to the number of
  1231. * sgs passed. So mark the chain bit to false if it isthe last
  1232. * mapped sg.
  1233. */
  1234. if ((i == remaining - 1) || !length)
  1235. last_sg = true;
  1236. if (!num_trbs_left)
  1237. break;
  1238. if (last_sg) {
  1239. if (!dwc3_prepare_last_sg(dep, req, trb_length, i))
  1240. break;
  1241. } else {
  1242. /*
  1243. * Look ahead to check if we have enough TRBs for the
  1244. * next SG entry. If not, set interrupt on this TRB to
  1245. * resume preparing the next SG entry when more TRBs are
  1246. * free.
  1247. */
  1248. if (num_trbs_left == 1 || (needs_extra_trb &&
  1249. num_trbs_left <= 2 &&
  1250. sg_dma_len(sg_next(s)) >= length)) {
  1251. struct dwc3_request *r;
  1252. /* Check if previous requests already set IOC */
  1253. list_for_each_entry(r, &dep->started_list, list) {
  1254. if (r != req && !r->request.no_interrupt)
  1255. break;
  1256. if (r == req)
  1257. must_interrupt = true;
  1258. }
  1259. }
  1260. dwc3_prepare_one_trb(dep, req, trb_length, 1, i, false,
  1261. must_interrupt);
  1262. }
  1263. /*
  1264. * There can be a situation where all sgs in sglist are not
  1265. * queued because of insufficient trb number. To handle this
  1266. * case, update start_sg to next sg to be queued, so that
  1267. * we have free trbs we can continue queuing from where we
  1268. * previously stopped
  1269. */
  1270. if (!last_sg)
  1271. req->start_sg = sg_next(s);
  1272. req->num_queued_sgs++;
  1273. req->num_pending_sgs--;
  1274. /*
  1275. * The number of pending SG entries may not correspond to the
  1276. * number of mapped SG entries. If all the data are queued, then
  1277. * don't include unused SG entries.
  1278. */
  1279. if (length == 0) {
  1280. req->num_pending_sgs = 0;
  1281. break;
  1282. }
  1283. if (must_interrupt)
  1284. break;
  1285. }
  1286. return req->num_trbs - num_trbs;
  1287. }
  1288. static int dwc3_prepare_trbs_linear(struct dwc3_ep *dep,
  1289. struct dwc3_request *req)
  1290. {
  1291. return dwc3_prepare_last_sg(dep, req, req->request.length, 0);
  1292. }
  1293. /*
  1294. * dwc3_prepare_trbs - setup TRBs from requests
  1295. * @dep: endpoint for which requests are being prepared
  1296. *
  1297. * The function goes through the requests list and sets up TRBs for the
  1298. * transfers. The function returns once there are no more TRBs available or
  1299. * it runs out of requests.
  1300. *
  1301. * Returns the number of TRBs prepared or negative errno.
  1302. */
  1303. static int dwc3_prepare_trbs(struct dwc3_ep *dep)
  1304. {
  1305. struct dwc3_request *req, *n;
  1306. int ret = 0;
  1307. BUILD_BUG_ON_NOT_POWER_OF_2(DWC3_TRB_NUM);
  1308. /*
  1309. * We can get in a situation where there's a request in the started list
  1310. * but there weren't enough TRBs to fully kick it in the first time
  1311. * around, so it has been waiting for more TRBs to be freed up.
  1312. *
  1313. * In that case, we should check if we have a request with pending_sgs
  1314. * in the started list and prepare TRBs for that request first,
  1315. * otherwise we will prepare TRBs completely out of order and that will
  1316. * break things.
  1317. */
  1318. list_for_each_entry(req, &dep->started_list, list) {
  1319. if (req->num_pending_sgs > 0) {
  1320. ret = dwc3_prepare_trbs_sg(dep, req);
  1321. if (!ret || req->num_pending_sgs)
  1322. return ret;
  1323. }
  1324. if (!dwc3_calc_trbs_left(dep))
  1325. return ret;
  1326. /*
  1327. * Don't prepare beyond a transfer. In DWC_usb32, its transfer
  1328. * burst capability may try to read and use TRBs beyond the
  1329. * active transfer instead of stopping.
  1330. */
  1331. if (dep->stream_capable && req->request.is_last &&
  1332. !DWC3_MST_CAPABLE(&dep->dwc->hwparams))
  1333. return ret;
  1334. }
  1335. list_for_each_entry_safe(req, n, &dep->pending_list, list) {
  1336. struct dwc3 *dwc = dep->dwc;
  1337. ret = usb_gadget_map_request_by_dev(dwc->sysdev, &req->request,
  1338. dep->direction);
  1339. if (ret)
  1340. return ret;
  1341. req->sg = req->request.sg;
  1342. req->start_sg = req->sg;
  1343. req->num_queued_sgs = 0;
  1344. req->num_pending_sgs = req->request.num_mapped_sgs;
  1345. if (req->num_pending_sgs > 0) {
  1346. ret = dwc3_prepare_trbs_sg(dep, req);
  1347. if (req->num_pending_sgs)
  1348. return ret;
  1349. } else {
  1350. ret = dwc3_prepare_trbs_linear(dep, req);
  1351. }
  1352. if (!ret || !dwc3_calc_trbs_left(dep))
  1353. return ret;
  1354. /*
  1355. * Don't prepare beyond a transfer. In DWC_usb32, its transfer
  1356. * burst capability may try to read and use TRBs beyond the
  1357. * active transfer instead of stopping.
  1358. */
  1359. if (dep->stream_capable && req->request.is_last &&
  1360. !DWC3_MST_CAPABLE(&dwc->hwparams))
  1361. return ret;
  1362. }
  1363. return ret;
  1364. }
  1365. static void dwc3_gadget_ep_cleanup_cancelled_requests(struct dwc3_ep *dep);
  1366. static int __dwc3_gadget_kick_transfer(struct dwc3_ep *dep)
  1367. {
  1368. struct dwc3_gadget_ep_cmd_params params;
  1369. struct dwc3_request *req;
  1370. int starting;
  1371. int ret;
  1372. u32 cmd;
  1373. /*
  1374. * Note that it's normal to have no new TRBs prepared (i.e. ret == 0).
  1375. * This happens when we need to stop and restart a transfer such as in
  1376. * the case of reinitiating a stream or retrying an isoc transfer.
  1377. */
  1378. ret = dwc3_prepare_trbs(dep);
  1379. if (ret < 0)
  1380. return ret;
  1381. starting = !(dep->flags & DWC3_EP_TRANSFER_STARTED);
  1382. /*
  1383. * If there's no new TRB prepared and we don't need to restart a
  1384. * transfer, there's no need to update the transfer.
  1385. */
  1386. if (!ret && !starting)
  1387. return ret;
  1388. req = next_request(&dep->started_list);
  1389. if (!req) {
  1390. dep->flags |= DWC3_EP_PENDING_REQUEST;
  1391. return 0;
  1392. }
  1393. memset(&params, 0, sizeof(params));
  1394. if (starting) {
  1395. params.param0 = upper_32_bits(req->trb_dma);
  1396. params.param1 = lower_32_bits(req->trb_dma);
  1397. cmd = DWC3_DEPCMD_STARTTRANSFER;
  1398. if (dep->stream_capable)
  1399. cmd |= DWC3_DEPCMD_PARAM(req->request.stream_id);
  1400. if (usb_endpoint_xfer_isoc(dep->endpoint.desc))
  1401. cmd |= DWC3_DEPCMD_PARAM(dep->frame_number);
  1402. } else {
  1403. cmd = DWC3_DEPCMD_UPDATETRANSFER |
  1404. DWC3_DEPCMD_PARAM(dep->resource_index);
  1405. }
  1406. ret = dwc3_send_gadget_ep_cmd(dep, cmd, &params);
  1407. if (ret < 0) {
  1408. struct dwc3_request *tmp;
  1409. if (ret == -EAGAIN)
  1410. return ret;
  1411. dwc3_stop_active_transfer(dep, true, true);
  1412. list_for_each_entry_safe(req, tmp, &dep->started_list, list)
  1413. dwc3_gadget_move_cancelled_request(req, DWC3_REQUEST_STATUS_DEQUEUED);
  1414. /* If ep isn't started, then there's no end transfer pending */
  1415. if (!(dep->flags & DWC3_EP_END_TRANSFER_PENDING))
  1416. dwc3_gadget_ep_cleanup_cancelled_requests(dep);
  1417. return ret;
  1418. }
  1419. if (dep->stream_capable && req->request.is_last &&
  1420. !DWC3_MST_CAPABLE(&dep->dwc->hwparams))
  1421. dep->flags |= DWC3_EP_WAIT_TRANSFER_COMPLETE;
  1422. return 0;
  1423. }
  1424. static int __dwc3_gadget_get_frame(struct dwc3 *dwc)
  1425. {
  1426. u32 reg;
  1427. reg = dwc3_readl(dwc->regs, DWC3_DSTS);
  1428. return DWC3_DSTS_SOFFN(reg);
  1429. }
  1430. /**
  1431. * __dwc3_stop_active_transfer - stop the current active transfer
  1432. * @dep: isoc endpoint
  1433. * @force: set forcerm bit in the command
  1434. * @interrupt: command complete interrupt after End Transfer command
  1435. *
  1436. * When setting force, the ForceRM bit will be set. In that case
  1437. * the controller won't update the TRB progress on command
  1438. * completion. It also won't clear the HWO bit in the TRB.
  1439. * The command will also not complete immediately in that case.
  1440. */
  1441. static int __dwc3_stop_active_transfer(struct dwc3_ep *dep, bool force, bool interrupt)
  1442. {
  1443. struct dwc3_gadget_ep_cmd_params params;
  1444. u32 cmd;
  1445. int ret;
  1446. cmd = DWC3_DEPCMD_ENDTRANSFER;
  1447. cmd |= force ? DWC3_DEPCMD_HIPRI_FORCERM : 0;
  1448. cmd |= interrupt ? DWC3_DEPCMD_CMDIOC : 0;
  1449. cmd |= DWC3_DEPCMD_PARAM(dep->resource_index);
  1450. memset(&params, 0, sizeof(params));
  1451. ret = dwc3_send_gadget_ep_cmd(dep, cmd, &params);
  1452. /*
  1453. * If the End Transfer command was timed out while the device is
  1454. * not in SETUP phase, it's possible that an incoming Setup packet
  1455. * may prevent the command's completion. Let's retry when the
  1456. * ep0state returns to EP0_SETUP_PHASE.
  1457. */
  1458. if (ret == -ETIMEDOUT && dep->dwc->ep0state != EP0_SETUP_PHASE) {
  1459. dep->flags |= DWC3_EP_DELAY_STOP;
  1460. return 0;
  1461. }
  1462. WARN_ON_ONCE(ret);
  1463. dep->resource_index = 0;
  1464. if (!interrupt)
  1465. dep->flags &= ~DWC3_EP_TRANSFER_STARTED;
  1466. else if (!ret)
  1467. dep->flags |= DWC3_EP_END_TRANSFER_PENDING;
  1468. dep->flags &= ~DWC3_EP_DELAY_STOP;
  1469. return ret;
  1470. }
  1471. /**
  1472. * dwc3_gadget_start_isoc_quirk - workaround invalid frame number
  1473. * @dep: isoc endpoint
  1474. *
  1475. * This function tests for the correct combination of BIT[15:14] from the 16-bit
  1476. * microframe number reported by the XferNotReady event for the future frame
  1477. * number to start the isoc transfer.
  1478. *
  1479. * In DWC_usb31 version 1.70a-ea06 and prior, for highspeed and fullspeed
  1480. * isochronous IN, BIT[15:14] of the 16-bit microframe number reported by the
  1481. * XferNotReady event are invalid. The driver uses this number to schedule the
  1482. * isochronous transfer and passes it to the START TRANSFER command. Because
  1483. * this number is invalid, the command may fail. If BIT[15:14] matches the
  1484. * internal 16-bit microframe, the START TRANSFER command will pass and the
  1485. * transfer will start at the scheduled time, if it is off by 1, the command
  1486. * will still pass, but the transfer will start 2 seconds in the future. For all
  1487. * other conditions, the START TRANSFER command will fail with bus-expiry.
  1488. *
  1489. * In order to workaround this issue, we can test for the correct combination of
  1490. * BIT[15:14] by sending START TRANSFER commands with different values of
  1491. * BIT[15:14]: 'b00, 'b01, 'b10, and 'b11. Each combination is 2^14 uframe apart
  1492. * (or 2 seconds). 4 seconds into the future will result in a bus-expiry status.
  1493. * As the result, within the 4 possible combinations for BIT[15:14], there will
  1494. * be 2 successful and 2 failure START COMMAND status. One of the 2 successful
  1495. * command status will result in a 2-second delay start. The smaller BIT[15:14]
  1496. * value is the correct combination.
  1497. *
  1498. * Since there are only 4 outcomes and the results are ordered, we can simply
  1499. * test 2 START TRANSFER commands with BIT[15:14] combinations 'b00 and 'b01 to
  1500. * deduce the smaller successful combination.
  1501. *
  1502. * Let test0 = test status for combination 'b00 and test1 = test status for 'b01
  1503. * of BIT[15:14]. The correct combination is as follow:
  1504. *
  1505. * if test0 fails and test1 passes, BIT[15:14] is 'b01
  1506. * if test0 fails and test1 fails, BIT[15:14] is 'b10
  1507. * if test0 passes and test1 fails, BIT[15:14] is 'b11
  1508. * if test0 passes and test1 passes, BIT[15:14] is 'b00
  1509. *
  1510. * Synopsys STAR 9001202023: Wrong microframe number for isochronous IN
  1511. * endpoints.
  1512. */
  1513. static int dwc3_gadget_start_isoc_quirk(struct dwc3_ep *dep)
  1514. {
  1515. int cmd_status = 0;
  1516. bool test0;
  1517. bool test1;
  1518. while (dep->combo_num < 2) {
  1519. struct dwc3_gadget_ep_cmd_params params;
  1520. u32 test_frame_number;
  1521. u32 cmd;
  1522. /*
  1523. * Check if we can start isoc transfer on the next interval or
  1524. * 4 uframes in the future with BIT[15:14] as dep->combo_num
  1525. */
  1526. test_frame_number = dep->frame_number & DWC3_FRNUMBER_MASK;
  1527. test_frame_number |= dep->combo_num << 14;
  1528. test_frame_number += max_t(u32, 4, dep->interval);
  1529. params.param0 = upper_32_bits(dep->dwc->bounce_addr);
  1530. params.param1 = lower_32_bits(dep->dwc->bounce_addr);
  1531. cmd = DWC3_DEPCMD_STARTTRANSFER;
  1532. cmd |= DWC3_DEPCMD_PARAM(test_frame_number);
  1533. cmd_status = dwc3_send_gadget_ep_cmd(dep, cmd, &params);
  1534. /* Redo if some other failure beside bus-expiry is received */
  1535. if (cmd_status && cmd_status != -EAGAIN) {
  1536. dep->start_cmd_status = 0;
  1537. dep->combo_num = 0;
  1538. return 0;
  1539. }
  1540. /* Store the first test status */
  1541. if (dep->combo_num == 0)
  1542. dep->start_cmd_status = cmd_status;
  1543. dep->combo_num++;
  1544. /*
  1545. * End the transfer if the START_TRANSFER command is successful
  1546. * to wait for the next XferNotReady to test the command again
  1547. */
  1548. if (cmd_status == 0) {
  1549. dwc3_stop_active_transfer(dep, true, true);
  1550. return 0;
  1551. }
  1552. }
  1553. /* test0 and test1 are both completed at this point */
  1554. test0 = (dep->start_cmd_status == 0);
  1555. test1 = (cmd_status == 0);
  1556. if (!test0 && test1)
  1557. dep->combo_num = 1;
  1558. else if (!test0 && !test1)
  1559. dep->combo_num = 2;
  1560. else if (test0 && !test1)
  1561. dep->combo_num = 3;
  1562. else if (test0 && test1)
  1563. dep->combo_num = 0;
  1564. dep->frame_number &= DWC3_FRNUMBER_MASK;
  1565. dep->frame_number |= dep->combo_num << 14;
  1566. dep->frame_number += max_t(u32, 4, dep->interval);
  1567. /* Reinitialize test variables */
  1568. dep->start_cmd_status = 0;
  1569. dep->combo_num = 0;
  1570. return __dwc3_gadget_kick_transfer(dep);
  1571. }
  1572. static int __dwc3_gadget_start_isoc(struct dwc3_ep *dep)
  1573. {
  1574. const struct usb_endpoint_descriptor *desc = dep->endpoint.desc;
  1575. struct dwc3 *dwc = dep->dwc;
  1576. int ret;
  1577. int i;
  1578. if (list_empty(&dep->pending_list) &&
  1579. list_empty(&dep->started_list)) {
  1580. dep->flags |= DWC3_EP_PENDING_REQUEST;
  1581. return -EAGAIN;
  1582. }
  1583. if (!dwc->dis_start_transfer_quirk &&
  1584. (DWC3_VER_IS_PRIOR(DWC31, 170A) ||
  1585. DWC3_VER_TYPE_IS_WITHIN(DWC31, 170A, EA01, EA06))) {
  1586. if (dwc->gadget->speed <= USB_SPEED_HIGH && dep->direction)
  1587. return dwc3_gadget_start_isoc_quirk(dep);
  1588. }
  1589. if (desc->bInterval <= 14 &&
  1590. dwc->gadget->speed >= USB_SPEED_HIGH) {
  1591. u32 frame = __dwc3_gadget_get_frame(dwc);
  1592. bool rollover = frame <
  1593. (dep->frame_number & DWC3_FRNUMBER_MASK);
  1594. /*
  1595. * frame_number is set from XferNotReady and may be already
  1596. * out of date. DSTS only provides the lower 14 bit of the
  1597. * current frame number. So add the upper two bits of
  1598. * frame_number and handle a possible rollover.
  1599. * This will provide the correct frame_number unless more than
  1600. * rollover has happened since XferNotReady.
  1601. */
  1602. dep->frame_number = (dep->frame_number & ~DWC3_FRNUMBER_MASK) |
  1603. frame;
  1604. if (rollover)
  1605. dep->frame_number += BIT(14);
  1606. }
  1607. for (i = 0; i < DWC3_ISOC_MAX_RETRIES; i++) {
  1608. int future_interval = i + 1;
  1609. /* Give the controller at least 500us to schedule transfers */
  1610. if (desc->bInterval < 3)
  1611. future_interval += 3 - desc->bInterval;
  1612. dep->frame_number = DWC3_ALIGN_FRAME(dep, future_interval);
  1613. ret = __dwc3_gadget_kick_transfer(dep);
  1614. if (ret != -EAGAIN)
  1615. break;
  1616. }
  1617. /*
  1618. * After a number of unsuccessful start attempts due to bus-expiry
  1619. * status, issue END_TRANSFER command and retry on the next XferNotReady
  1620. * event.
  1621. */
  1622. if (ret == -EAGAIN)
  1623. ret = __dwc3_stop_active_transfer(dep, false, true);
  1624. return ret;
  1625. }
  1626. static int __dwc3_gadget_ep_queue(struct dwc3_ep *dep, struct dwc3_request *req)
  1627. {
  1628. struct dwc3 *dwc = dep->dwc;
  1629. if (!dep->endpoint.desc || !dwc->pullups_connected || !dwc->connected) {
  1630. dev_dbg(dwc->dev, "%s: can't queue to disabled endpoint\n",
  1631. dep->name);
  1632. return -ESHUTDOWN;
  1633. }
  1634. if (WARN(req->dep != dep, "request %pK belongs to '%s'\n",
  1635. &req->request, req->dep->name))
  1636. return -EINVAL;
  1637. if (WARN(req->status < DWC3_REQUEST_STATUS_COMPLETED,
  1638. "%s: request %pK already in flight\n",
  1639. dep->name, &req->request))
  1640. return -EINVAL;
  1641. pm_runtime_get(dwc->dev);
  1642. req->request.actual = 0;
  1643. req->request.status = -EINPROGRESS;
  1644. trace_dwc3_ep_queue(req);
  1645. list_add_tail(&req->list, &dep->pending_list);
  1646. req->status = DWC3_REQUEST_STATUS_QUEUED;
  1647. if (dep->flags & DWC3_EP_WAIT_TRANSFER_COMPLETE)
  1648. return 0;
  1649. /*
  1650. * Start the transfer only after the END_TRANSFER is completed
  1651. * and endpoint STALL is cleared.
  1652. */
  1653. if ((dep->flags & DWC3_EP_END_TRANSFER_PENDING) ||
  1654. (dep->flags & DWC3_EP_WEDGE) ||
  1655. (dep->flags & DWC3_EP_DELAY_STOP) ||
  1656. (dep->flags & DWC3_EP_STALL)) {
  1657. dep->flags |= DWC3_EP_DELAY_START;
  1658. return 0;
  1659. }
  1660. /*
  1661. * NOTICE: Isochronous endpoints should NEVER be prestarted. We must
  1662. * wait for a XferNotReady event so we will know what's the current
  1663. * (micro-)frame number.
  1664. *
  1665. * Without this trick, we are very, very likely gonna get Bus Expiry
  1666. * errors which will force us issue EndTransfer command.
  1667. */
  1668. if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
  1669. if (!(dep->flags & DWC3_EP_TRANSFER_STARTED)) {
  1670. if ((dep->flags & DWC3_EP_PENDING_REQUEST))
  1671. return __dwc3_gadget_start_isoc(dep);
  1672. return 0;
  1673. }
  1674. }
  1675. __dwc3_gadget_kick_transfer(dep);
  1676. return 0;
  1677. }
  1678. static int dwc3_gadget_ep_queue(struct usb_ep *ep, struct usb_request *request,
  1679. gfp_t gfp_flags)
  1680. {
  1681. struct dwc3_request *req = to_dwc3_request(request);
  1682. struct dwc3_ep *dep = to_dwc3_ep(ep);
  1683. struct dwc3 *dwc = dep->dwc;
  1684. unsigned long flags;
  1685. int ret;
  1686. spin_lock_irqsave(&dwc->lock, flags);
  1687. ret = __dwc3_gadget_ep_queue(dep, req);
  1688. spin_unlock_irqrestore(&dwc->lock, flags);
  1689. return ret;
  1690. }
  1691. static void dwc3_gadget_ep_skip_trbs(struct dwc3_ep *dep, struct dwc3_request *req)
  1692. {
  1693. int i;
  1694. /* If req->trb is not set, then the request has not started */
  1695. if (!req->trb)
  1696. return;
  1697. /*
  1698. * If request was already started, this means we had to
  1699. * stop the transfer. With that we also need to ignore
  1700. * all TRBs used by the request, however TRBs can only
  1701. * be modified after completion of END_TRANSFER
  1702. * command. So what we do here is that we wait for
  1703. * END_TRANSFER completion and only after that, we jump
  1704. * over TRBs by clearing HWO and incrementing dequeue
  1705. * pointer.
  1706. */
  1707. for (i = 0; i < req->num_trbs; i++) {
  1708. struct dwc3_trb *trb;
  1709. trb = &dep->trb_pool[dep->trb_dequeue];
  1710. trb->ctrl &= ~DWC3_TRB_CTRL_HWO;
  1711. dwc3_ep_inc_deq(dep);
  1712. }
  1713. req->num_trbs = 0;
  1714. }
  1715. static void dwc3_gadget_ep_cleanup_cancelled_requests(struct dwc3_ep *dep)
  1716. {
  1717. struct dwc3_request *req;
  1718. struct dwc3 *dwc = dep->dwc;
  1719. while (!list_empty(&dep->cancelled_list)) {
  1720. req = next_request(&dep->cancelled_list);
  1721. dwc3_gadget_ep_skip_trbs(dep, req);
  1722. switch (req->status) {
  1723. case DWC3_REQUEST_STATUS_DISCONNECTED:
  1724. dwc3_gadget_giveback(dep, req, -ESHUTDOWN);
  1725. break;
  1726. case DWC3_REQUEST_STATUS_DEQUEUED:
  1727. dwc3_gadget_giveback(dep, req, -ECONNRESET);
  1728. break;
  1729. case DWC3_REQUEST_STATUS_STALLED:
  1730. dwc3_gadget_giveback(dep, req, -EPIPE);
  1731. break;
  1732. default:
  1733. dev_err(dwc->dev, "request cancelled with wrong reason:%d\n", req->status);
  1734. dwc3_gadget_giveback(dep, req, -ECONNRESET);
  1735. break;
  1736. }
  1737. /*
  1738. * The endpoint is disabled, let the dwc3_remove_requests()
  1739. * handle the cleanup.
  1740. */
  1741. if (!dep->endpoint.desc)
  1742. break;
  1743. }
  1744. }
  1745. static int dwc3_gadget_ep_dequeue(struct usb_ep *ep,
  1746. struct usb_request *request)
  1747. {
  1748. struct dwc3_request *req = to_dwc3_request(request);
  1749. struct dwc3_request *r = NULL;
  1750. struct dwc3_ep *dep = to_dwc3_ep(ep);
  1751. struct dwc3 *dwc = dep->dwc;
  1752. unsigned long flags;
  1753. int ret = 0;
  1754. trace_dwc3_ep_dequeue(req);
  1755. spin_lock_irqsave(&dwc->lock, flags);
  1756. list_for_each_entry(r, &dep->cancelled_list, list) {
  1757. if (r == req)
  1758. goto out;
  1759. }
  1760. list_for_each_entry(r, &dep->pending_list, list) {
  1761. if (r == req) {
  1762. /*
  1763. * Explicitly check for EP0/1 as dequeue for those
  1764. * EPs need to be handled differently. Control EP
  1765. * only deals with one USB req, and giveback will
  1766. * occur during dwc3_ep0_stall_and_restart(). EP0
  1767. * requests are never added to started_list.
  1768. */
  1769. if (dep->number > 1)
  1770. dwc3_gadget_giveback(dep, req, -ECONNRESET);
  1771. else
  1772. dwc3_ep0_reset_state(dwc);
  1773. goto out;
  1774. }
  1775. }
  1776. list_for_each_entry(r, &dep->started_list, list) {
  1777. if (r == req) {
  1778. struct dwc3_request *t;
  1779. /* wait until it is processed */
  1780. dwc3_stop_active_transfer(dep, true, true);
  1781. /*
  1782. * Remove any started request if the transfer is
  1783. * cancelled.
  1784. */
  1785. list_for_each_entry_safe(r, t, &dep->started_list, list)
  1786. dwc3_gadget_move_cancelled_request(r,
  1787. DWC3_REQUEST_STATUS_DEQUEUED);
  1788. dep->flags &= ~DWC3_EP_WAIT_TRANSFER_COMPLETE;
  1789. goto out;
  1790. }
  1791. }
  1792. dev_err(dwc->dev, "request %pK was not queued to %s\n",
  1793. request, ep->name);
  1794. ret = -EINVAL;
  1795. out:
  1796. spin_unlock_irqrestore(&dwc->lock, flags);
  1797. return ret;
  1798. }
  1799. int __dwc3_gadget_ep_set_halt(struct dwc3_ep *dep, int value, int protocol)
  1800. {
  1801. struct dwc3_gadget_ep_cmd_params params;
  1802. struct dwc3 *dwc = dep->dwc;
  1803. struct dwc3_request *req;
  1804. struct dwc3_request *tmp;
  1805. int ret;
  1806. if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
  1807. dev_err(dwc->dev, "%s is of Isochronous type\n", dep->name);
  1808. return -EINVAL;
  1809. }
  1810. memset(&params, 0x00, sizeof(params));
  1811. if (value) {
  1812. struct dwc3_trb *trb;
  1813. unsigned int transfer_in_flight;
  1814. unsigned int started;
  1815. if (dep->number > 1)
  1816. trb = dwc3_ep_prev_trb(dep, dep->trb_enqueue);
  1817. else
  1818. trb = &dwc->ep0_trb[dep->trb_enqueue];
  1819. transfer_in_flight = trb->ctrl & DWC3_TRB_CTRL_HWO;
  1820. started = !list_empty(&dep->started_list);
  1821. if (!protocol && ((dep->direction && transfer_in_flight) ||
  1822. (!dep->direction && started))) {
  1823. return -EAGAIN;
  1824. }
  1825. ret = dwc3_send_gadget_ep_cmd(dep, DWC3_DEPCMD_SETSTALL,
  1826. &params);
  1827. if (ret)
  1828. dev_err(dwc->dev, "failed to set STALL on %s\n",
  1829. dep->name);
  1830. else
  1831. dep->flags |= DWC3_EP_STALL;
  1832. } else {
  1833. /*
  1834. * Don't issue CLEAR_STALL command to control endpoints. The
  1835. * controller automatically clears the STALL when it receives
  1836. * the SETUP token.
  1837. */
  1838. if (dep->number <= 1) {
  1839. dep->flags &= ~(DWC3_EP_STALL | DWC3_EP_WEDGE);
  1840. return 0;
  1841. }
  1842. dwc3_stop_active_transfer(dep, true, true);
  1843. list_for_each_entry_safe(req, tmp, &dep->started_list, list)
  1844. dwc3_gadget_move_cancelled_request(req, DWC3_REQUEST_STATUS_STALLED);
  1845. if (dep->flags & DWC3_EP_END_TRANSFER_PENDING ||
  1846. (dep->flags & DWC3_EP_DELAY_STOP)) {
  1847. dep->flags |= DWC3_EP_PENDING_CLEAR_STALL;
  1848. if (protocol)
  1849. dwc->clear_stall_protocol = dep->number;
  1850. return 0;
  1851. }
  1852. dwc3_gadget_ep_cleanup_cancelled_requests(dep);
  1853. ret = dwc3_send_clear_stall_ep_cmd(dep);
  1854. if (ret) {
  1855. dev_err(dwc->dev, "failed to clear STALL on %s\n",
  1856. dep->name);
  1857. return ret;
  1858. }
  1859. dep->flags &= ~(DWC3_EP_STALL | DWC3_EP_WEDGE);
  1860. if ((dep->flags & DWC3_EP_DELAY_START) &&
  1861. !usb_endpoint_xfer_isoc(dep->endpoint.desc))
  1862. __dwc3_gadget_kick_transfer(dep);
  1863. dep->flags &= ~DWC3_EP_DELAY_START;
  1864. }
  1865. return ret;
  1866. }
  1867. static int dwc3_gadget_ep_set_halt(struct usb_ep *ep, int value)
  1868. {
  1869. struct dwc3_ep *dep = to_dwc3_ep(ep);
  1870. struct dwc3 *dwc = dep->dwc;
  1871. unsigned long flags;
  1872. int ret;
  1873. spin_lock_irqsave(&dwc->lock, flags);
  1874. ret = __dwc3_gadget_ep_set_halt(dep, value, false);
  1875. spin_unlock_irqrestore(&dwc->lock, flags);
  1876. return ret;
  1877. }
  1878. static int dwc3_gadget_ep_set_wedge(struct usb_ep *ep)
  1879. {
  1880. struct dwc3_ep *dep = to_dwc3_ep(ep);
  1881. struct dwc3 *dwc = dep->dwc;
  1882. unsigned long flags;
  1883. int ret;
  1884. spin_lock_irqsave(&dwc->lock, flags);
  1885. dep->flags |= DWC3_EP_WEDGE;
  1886. if (dep->number == 0 || dep->number == 1)
  1887. ret = __dwc3_gadget_ep0_set_halt(ep, 1);
  1888. else
  1889. ret = __dwc3_gadget_ep_set_halt(dep, 1, false);
  1890. spin_unlock_irqrestore(&dwc->lock, flags);
  1891. return ret;
  1892. }
  1893. /* -------------------------------------------------------------------------- */
  1894. static struct usb_endpoint_descriptor dwc3_gadget_ep0_desc = {
  1895. .bLength = USB_DT_ENDPOINT_SIZE,
  1896. .bDescriptorType = USB_DT_ENDPOINT,
  1897. .bmAttributes = USB_ENDPOINT_XFER_CONTROL,
  1898. };
  1899. static const struct usb_ep_ops dwc3_gadget_ep0_ops = {
  1900. .enable = dwc3_gadget_ep0_enable,
  1901. .disable = dwc3_gadget_ep0_disable,
  1902. .alloc_request = dwc3_gadget_ep_alloc_request,
  1903. .free_request = dwc3_gadget_ep_free_request,
  1904. .queue = dwc3_gadget_ep0_queue,
  1905. .dequeue = dwc3_gadget_ep_dequeue,
  1906. .set_halt = dwc3_gadget_ep0_set_halt,
  1907. .set_wedge = dwc3_gadget_ep_set_wedge,
  1908. };
  1909. static const struct usb_ep_ops dwc3_gadget_ep_ops = {
  1910. .enable = dwc3_gadget_ep_enable,
  1911. .disable = dwc3_gadget_ep_disable,
  1912. .alloc_request = dwc3_gadget_ep_alloc_request,
  1913. .free_request = dwc3_gadget_ep_free_request,
  1914. .queue = dwc3_gadget_ep_queue,
  1915. .dequeue = dwc3_gadget_ep_dequeue,
  1916. .set_halt = dwc3_gadget_ep_set_halt,
  1917. .set_wedge = dwc3_gadget_ep_set_wedge,
  1918. };
  1919. /* -------------------------------------------------------------------------- */
  1920. static void dwc3_gadget_enable_linksts_evts(struct dwc3 *dwc, bool set)
  1921. {
  1922. u32 reg;
  1923. if (DWC3_VER_IS_PRIOR(DWC3, 250A))
  1924. return;
  1925. reg = dwc3_readl(dwc->regs, DWC3_DEVTEN);
  1926. if (set)
  1927. reg |= DWC3_DEVTEN_ULSTCNGEN;
  1928. else
  1929. reg &= ~DWC3_DEVTEN_ULSTCNGEN;
  1930. dwc3_writel(dwc->regs, DWC3_DEVTEN, reg);
  1931. }
  1932. static int dwc3_gadget_get_frame(struct usb_gadget *g)
  1933. {
  1934. struct dwc3 *dwc = gadget_to_dwc(g);
  1935. return __dwc3_gadget_get_frame(dwc);
  1936. }
  1937. static int __dwc3_gadget_wakeup(struct dwc3 *dwc, bool async)
  1938. {
  1939. int retries;
  1940. int ret;
  1941. u32 reg;
  1942. u8 link_state;
  1943. /*
  1944. * According to the Databook Remote wakeup request should
  1945. * be issued only when the device is in early suspend state.
  1946. *
  1947. * We can check that via USB Link State bits in DSTS register.
  1948. */
  1949. reg = dwc3_readl(dwc->regs, DWC3_DSTS);
  1950. link_state = DWC3_DSTS_USBLNKST(reg);
  1951. switch (link_state) {
  1952. case DWC3_LINK_STATE_RESET:
  1953. case DWC3_LINK_STATE_RX_DET: /* in HS, means Early Suspend */
  1954. case DWC3_LINK_STATE_U3: /* in HS, means SUSPEND */
  1955. case DWC3_LINK_STATE_U2: /* in HS, means Sleep (L1) */
  1956. case DWC3_LINK_STATE_U1:
  1957. case DWC3_LINK_STATE_RESUME:
  1958. break;
  1959. default:
  1960. return -EINVAL;
  1961. }
  1962. if (async)
  1963. dwc3_gadget_enable_linksts_evts(dwc, true);
  1964. ret = dwc3_gadget_set_link_state(dwc, DWC3_LINK_STATE_RECOV);
  1965. if (ret < 0) {
  1966. dev_err(dwc->dev, "failed to put link in Recovery\n");
  1967. dwc3_gadget_enable_linksts_evts(dwc, false);
  1968. return ret;
  1969. }
  1970. /* Recent versions do this automatically */
  1971. if (DWC3_VER_IS_PRIOR(DWC3, 194A)) {
  1972. /* write zeroes to Link Change Request */
  1973. reg = dwc3_readl(dwc->regs, DWC3_DCTL);
  1974. reg &= ~DWC3_DCTL_ULSTCHNGREQ_MASK;
  1975. dwc3_writel(dwc->regs, DWC3_DCTL, reg);
  1976. }
  1977. /*
  1978. * Since link status change events are enabled we will receive
  1979. * an U0 event when wakeup is successful. So bail out.
  1980. */
  1981. if (async)
  1982. return 0;
  1983. /* poll until Link State changes to ON */
  1984. retries = 20000;
  1985. while (retries--) {
  1986. reg = dwc3_readl(dwc->regs, DWC3_DSTS);
  1987. /* in HS, means ON */
  1988. if (DWC3_DSTS_USBLNKST(reg) == DWC3_LINK_STATE_U0)
  1989. break;
  1990. }
  1991. if (DWC3_DSTS_USBLNKST(reg) != DWC3_LINK_STATE_U0) {
  1992. dev_err(dwc->dev, "failed to send remote wakeup\n");
  1993. return -EINVAL;
  1994. }
  1995. return 0;
  1996. }
  1997. static int dwc3_gadget_wakeup(struct usb_gadget *g)
  1998. {
  1999. struct dwc3 *dwc = gadget_to_dwc(g);
  2000. unsigned long flags;
  2001. int ret;
  2002. if (!dwc->wakeup_configured) {
  2003. dev_err(dwc->dev, "remote wakeup not configured\n");
  2004. return -EINVAL;
  2005. }
  2006. spin_lock_irqsave(&dwc->lock, flags);
  2007. if (!dwc->gadget->wakeup_armed) {
  2008. dev_err(dwc->dev, "not armed for remote wakeup\n");
  2009. spin_unlock_irqrestore(&dwc->lock, flags);
  2010. return -EINVAL;
  2011. }
  2012. ret = __dwc3_gadget_wakeup(dwc, true);
  2013. spin_unlock_irqrestore(&dwc->lock, flags);
  2014. return ret;
  2015. }
  2016. static void dwc3_resume_gadget(struct dwc3 *dwc);
  2017. static int dwc3_gadget_func_wakeup(struct usb_gadget *g, int intf_id)
  2018. {
  2019. struct dwc3 *dwc = gadget_to_dwc(g);
  2020. unsigned long flags;
  2021. int ret;
  2022. int link_state;
  2023. if (!dwc->wakeup_configured) {
  2024. dev_err(dwc->dev, "remote wakeup not configured\n");
  2025. return -EINVAL;
  2026. }
  2027. spin_lock_irqsave(&dwc->lock, flags);
  2028. /*
  2029. * If the link is in U3, signal for remote wakeup and wait for the
  2030. * link to transition to U0 before sending device notification.
  2031. */
  2032. link_state = dwc3_gadget_get_link_state(dwc);
  2033. if (link_state == DWC3_LINK_STATE_U3) {
  2034. ret = __dwc3_gadget_wakeup(dwc, false);
  2035. if (ret) {
  2036. spin_unlock_irqrestore(&dwc->lock, flags);
  2037. return -EINVAL;
  2038. }
  2039. dwc3_resume_gadget(dwc);
  2040. dwc->suspended = false;
  2041. dwc->link_state = DWC3_LINK_STATE_U0;
  2042. }
  2043. ret = dwc3_send_gadget_generic_command(dwc, DWC3_DGCMD_DEV_NOTIFICATION,
  2044. DWC3_DGCMDPAR_DN_FUNC_WAKE |
  2045. DWC3_DGCMDPAR_INTF_SEL(intf_id));
  2046. if (ret)
  2047. dev_err(dwc->dev, "function remote wakeup failed, ret:%d\n", ret);
  2048. spin_unlock_irqrestore(&dwc->lock, flags);
  2049. return ret;
  2050. }
  2051. static int dwc3_gadget_set_remote_wakeup(struct usb_gadget *g, int set)
  2052. {
  2053. struct dwc3 *dwc = gadget_to_dwc(g);
  2054. unsigned long flags;
  2055. spin_lock_irqsave(&dwc->lock, flags);
  2056. dwc->wakeup_configured = !!set;
  2057. spin_unlock_irqrestore(&dwc->lock, flags);
  2058. return 0;
  2059. }
  2060. static int dwc3_gadget_set_selfpowered(struct usb_gadget *g,
  2061. int is_selfpowered)
  2062. {
  2063. struct dwc3 *dwc = gadget_to_dwc(g);
  2064. unsigned long flags;
  2065. spin_lock_irqsave(&dwc->lock, flags);
  2066. g->is_selfpowered = !!is_selfpowered;
  2067. spin_unlock_irqrestore(&dwc->lock, flags);
  2068. return 0;
  2069. }
  2070. static void dwc3_stop_active_transfers(struct dwc3 *dwc)
  2071. {
  2072. u32 epnum;
  2073. for (epnum = 2; epnum < dwc->num_eps; epnum++) {
  2074. struct dwc3_ep *dep;
  2075. dep = dwc->eps[epnum];
  2076. if (!dep)
  2077. continue;
  2078. dwc3_remove_requests(dwc, dep, -ESHUTDOWN);
  2079. }
  2080. }
  2081. static void __dwc3_gadget_set_ssp_rate(struct dwc3 *dwc)
  2082. {
  2083. enum usb_ssp_rate ssp_rate = dwc->gadget_ssp_rate;
  2084. u32 reg;
  2085. if (ssp_rate == USB_SSP_GEN_UNKNOWN)
  2086. ssp_rate = dwc->max_ssp_rate;
  2087. reg = dwc3_readl(dwc->regs, DWC3_DCFG);
  2088. reg &= ~DWC3_DCFG_SPEED_MASK;
  2089. reg &= ~DWC3_DCFG_NUMLANES(~0);
  2090. if (ssp_rate == USB_SSP_GEN_1x2)
  2091. reg |= DWC3_DCFG_SUPERSPEED;
  2092. else if (dwc->max_ssp_rate != USB_SSP_GEN_1x2)
  2093. reg |= DWC3_DCFG_SUPERSPEED_PLUS;
  2094. if (ssp_rate != USB_SSP_GEN_2x1 &&
  2095. dwc->max_ssp_rate != USB_SSP_GEN_2x1)
  2096. reg |= DWC3_DCFG_NUMLANES(1);
  2097. dwc3_writel(dwc->regs, DWC3_DCFG, reg);
  2098. }
  2099. static void __dwc3_gadget_set_speed(struct dwc3 *dwc)
  2100. {
  2101. enum usb_device_speed speed;
  2102. u32 reg;
  2103. speed = dwc->gadget_max_speed;
  2104. if (speed == USB_SPEED_UNKNOWN || speed > dwc->maximum_speed)
  2105. speed = dwc->maximum_speed;
  2106. if (speed == USB_SPEED_SUPER_PLUS &&
  2107. DWC3_IP_IS(DWC32)) {
  2108. __dwc3_gadget_set_ssp_rate(dwc);
  2109. return;
  2110. }
  2111. reg = dwc3_readl(dwc->regs, DWC3_DCFG);
  2112. reg &= ~(DWC3_DCFG_SPEED_MASK);
  2113. /*
  2114. * WORKAROUND: DWC3 revision < 2.20a have an issue
  2115. * which would cause metastability state on Run/Stop
  2116. * bit if we try to force the IP to USB2-only mode.
  2117. *
  2118. * Because of that, we cannot configure the IP to any
  2119. * speed other than the SuperSpeed
  2120. *
  2121. * Refers to:
  2122. *
  2123. * STAR#9000525659: Clock Domain Crossing on DCTL in
  2124. * USB 2.0 Mode
  2125. */
  2126. if (DWC3_VER_IS_PRIOR(DWC3, 220A) &&
  2127. !dwc->dis_metastability_quirk) {
  2128. reg |= DWC3_DCFG_SUPERSPEED;
  2129. } else {
  2130. switch (speed) {
  2131. case USB_SPEED_FULL:
  2132. reg |= DWC3_DCFG_FULLSPEED;
  2133. break;
  2134. case USB_SPEED_HIGH:
  2135. reg |= DWC3_DCFG_HIGHSPEED;
  2136. break;
  2137. case USB_SPEED_SUPER:
  2138. reg |= DWC3_DCFG_SUPERSPEED;
  2139. break;
  2140. case USB_SPEED_SUPER_PLUS:
  2141. if (DWC3_IP_IS(DWC3))
  2142. reg |= DWC3_DCFG_SUPERSPEED;
  2143. else
  2144. reg |= DWC3_DCFG_SUPERSPEED_PLUS;
  2145. break;
  2146. default:
  2147. dev_err(dwc->dev, "invalid speed (%d)\n", speed);
  2148. if (DWC3_IP_IS(DWC3))
  2149. reg |= DWC3_DCFG_SUPERSPEED;
  2150. else
  2151. reg |= DWC3_DCFG_SUPERSPEED_PLUS;
  2152. }
  2153. }
  2154. if (DWC3_IP_IS(DWC32) &&
  2155. speed > USB_SPEED_UNKNOWN &&
  2156. speed < USB_SPEED_SUPER_PLUS)
  2157. reg &= ~DWC3_DCFG_NUMLANES(~0);
  2158. dwc3_writel(dwc->regs, DWC3_DCFG, reg);
  2159. }
  2160. static int dwc3_gadget_run_stop(struct dwc3 *dwc, int is_on)
  2161. {
  2162. u32 reg;
  2163. u32 timeout = 2000;
  2164. u32 saved_config = 0;
  2165. if (pm_runtime_suspended(dwc->dev))
  2166. return 0;
  2167. /*
  2168. * When operating in USB 2.0 speeds (HS/FS), ensure that
  2169. * GUSB2PHYCFG.ENBLSLPM and GUSB2PHYCFG.SUSPHY are cleared before starting
  2170. * or stopping the controller. This resolves timeout issues that occur
  2171. * during frequent role switches between host and device modes.
  2172. *
  2173. * Save and clear these settings, then restore them after completing the
  2174. * controller start or stop sequence.
  2175. *
  2176. * This solution was discovered through experimentation as it is not
  2177. * mentioned in the dwc3 programming guide. It has been tested on an
  2178. * Exynos platforms.
  2179. */
  2180. reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0));
  2181. if (reg & DWC3_GUSB2PHYCFG_SUSPHY) {
  2182. saved_config |= DWC3_GUSB2PHYCFG_SUSPHY;
  2183. reg &= ~DWC3_GUSB2PHYCFG_SUSPHY;
  2184. }
  2185. if (reg & DWC3_GUSB2PHYCFG_ENBLSLPM) {
  2186. saved_config |= DWC3_GUSB2PHYCFG_ENBLSLPM;
  2187. reg &= ~DWC3_GUSB2PHYCFG_ENBLSLPM;
  2188. }
  2189. if (saved_config)
  2190. dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg);
  2191. reg = dwc3_readl(dwc->regs, DWC3_DCTL);
  2192. if (is_on) {
  2193. if (DWC3_VER_IS_WITHIN(DWC3, ANY, 187A)) {
  2194. reg &= ~DWC3_DCTL_TRGTULST_MASK;
  2195. reg |= DWC3_DCTL_TRGTULST_RX_DET;
  2196. }
  2197. if (!DWC3_VER_IS_PRIOR(DWC3, 194A))
  2198. reg &= ~DWC3_DCTL_KEEP_CONNECT;
  2199. reg |= DWC3_DCTL_RUN_STOP;
  2200. __dwc3_gadget_set_speed(dwc);
  2201. dwc->pullups_connected = true;
  2202. } else {
  2203. reg &= ~DWC3_DCTL_RUN_STOP;
  2204. dwc->pullups_connected = false;
  2205. }
  2206. dwc3_gadget_dctl_write_safe(dwc, reg);
  2207. do {
  2208. usleep_range(1000, 2000);
  2209. reg = dwc3_readl(dwc->regs, DWC3_DSTS);
  2210. reg &= DWC3_DSTS_DEVCTRLHLT;
  2211. } while (--timeout && !(!is_on ^ !reg));
  2212. if (saved_config) {
  2213. reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0));
  2214. reg |= saved_config;
  2215. dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg);
  2216. }
  2217. if (!timeout)
  2218. return -ETIMEDOUT;
  2219. return 0;
  2220. }
  2221. static void dwc3_gadget_disable_irq(struct dwc3 *dwc);
  2222. static void __dwc3_gadget_stop(struct dwc3 *dwc);
  2223. static int __dwc3_gadget_start(struct dwc3 *dwc);
  2224. static int dwc3_gadget_soft_disconnect(struct dwc3 *dwc)
  2225. {
  2226. unsigned long flags;
  2227. int ret;
  2228. spin_lock_irqsave(&dwc->lock, flags);
  2229. if (!dwc->pullups_connected) {
  2230. spin_unlock_irqrestore(&dwc->lock, flags);
  2231. return 0;
  2232. }
  2233. dwc->connected = false;
  2234. /*
  2235. * Attempt to end pending SETUP status phase, and not wait for the
  2236. * function to do so.
  2237. */
  2238. if (dwc->delayed_status)
  2239. dwc3_ep0_send_delayed_status(dwc);
  2240. /*
  2241. * In the Synopsys DesignWare Cores USB3 Databook Rev. 3.30a
  2242. * Section 4.1.8 Table 4-7, it states that for a device-initiated
  2243. * disconnect, the SW needs to ensure that it sends "a DEPENDXFER
  2244. * command for any active transfers" before clearing the RunStop
  2245. * bit.
  2246. */
  2247. dwc3_stop_active_transfers(dwc);
  2248. spin_unlock_irqrestore(&dwc->lock, flags);
  2249. /*
  2250. * Per databook, when we want to stop the gadget, if a control transfer
  2251. * is still in process, complete it and get the core into setup phase.
  2252. * In case the host is unresponsive to a SETUP transaction, forcefully
  2253. * stall the transfer, and move back to the SETUP phase, so that any
  2254. * pending endxfers can be executed.
  2255. */
  2256. if (dwc->ep0state != EP0_SETUP_PHASE) {
  2257. reinit_completion(&dwc->ep0_in_setup);
  2258. ret = wait_for_completion_timeout(&dwc->ep0_in_setup,
  2259. msecs_to_jiffies(DWC3_PULL_UP_TIMEOUT));
  2260. if (ret == 0) {
  2261. dev_warn(dwc->dev, "wait for SETUP phase timed out\n");
  2262. spin_lock_irqsave(&dwc->lock, flags);
  2263. dwc3_ep0_reset_state(dwc);
  2264. spin_unlock_irqrestore(&dwc->lock, flags);
  2265. }
  2266. }
  2267. /*
  2268. * Note: if the GEVNTCOUNT indicates events in the event buffer, the
  2269. * driver needs to acknowledge them before the controller can halt.
  2270. * Simply let the interrupt handler acknowledges and handle the
  2271. * remaining event generated by the controller while polling for
  2272. * DSTS.DEVCTLHLT.
  2273. */
  2274. ret = dwc3_gadget_run_stop(dwc, false);
  2275. /*
  2276. * Stop the gadget after controller is halted, so that if needed, the
  2277. * events to update EP0 state can still occur while the run/stop
  2278. * routine polls for the halted state. DEVTEN is cleared as part of
  2279. * gadget stop.
  2280. */
  2281. spin_lock_irqsave(&dwc->lock, flags);
  2282. __dwc3_gadget_stop(dwc);
  2283. spin_unlock_irqrestore(&dwc->lock, flags);
  2284. return ret;
  2285. }
  2286. static int dwc3_gadget_soft_connect(struct dwc3 *dwc)
  2287. {
  2288. int ret;
  2289. /*
  2290. * In the Synopsys DWC_usb31 1.90a programming guide section
  2291. * 4.1.9, it specifies that for a reconnect after a
  2292. * device-initiated disconnect requires a core soft reset
  2293. * (DCTL.CSftRst) before enabling the run/stop bit.
  2294. */
  2295. ret = dwc3_core_soft_reset(dwc);
  2296. if (ret)
  2297. return ret;
  2298. dwc3_event_buffers_setup(dwc);
  2299. __dwc3_gadget_start(dwc);
  2300. return dwc3_gadget_run_stop(dwc, true);
  2301. }
  2302. static int dwc3_gadget_pullup(struct usb_gadget *g, int is_on)
  2303. {
  2304. struct dwc3 *dwc = gadget_to_dwc(g);
  2305. int ret;
  2306. is_on = !!is_on;
  2307. dwc->softconnect = is_on;
  2308. /*
  2309. * Avoid issuing a runtime resume if the device is already in the
  2310. * suspended state during gadget disconnect. DWC3 gadget was already
  2311. * halted/stopped during runtime suspend.
  2312. */
  2313. if (!is_on) {
  2314. pm_runtime_barrier(dwc->dev);
  2315. if (pm_runtime_suspended(dwc->dev))
  2316. return 0;
  2317. }
  2318. /*
  2319. * Check the return value for successful resume, or error. For a
  2320. * successful resume, the DWC3 runtime PM resume routine will handle
  2321. * the run stop sequence, so avoid duplicate operations here.
  2322. */
  2323. ret = pm_runtime_get_sync(dwc->dev);
  2324. if (!ret || ret < 0) {
  2325. pm_runtime_put(dwc->dev);
  2326. if (ret < 0)
  2327. pm_runtime_set_suspended(dwc->dev);
  2328. return ret;
  2329. }
  2330. if (dwc->pullups_connected == is_on) {
  2331. pm_runtime_put(dwc->dev);
  2332. return 0;
  2333. }
  2334. synchronize_irq(dwc->irq_gadget);
  2335. if (!is_on)
  2336. ret = dwc3_gadget_soft_disconnect(dwc);
  2337. else
  2338. ret = dwc3_gadget_soft_connect(dwc);
  2339. pm_runtime_put(dwc->dev);
  2340. return ret;
  2341. }
  2342. static void dwc3_gadget_enable_irq(struct dwc3 *dwc)
  2343. {
  2344. u32 reg;
  2345. /* Enable all but Start and End of Frame IRQs */
  2346. reg = (DWC3_DEVTEN_EVNTOVERFLOWEN |
  2347. DWC3_DEVTEN_CMDCMPLTEN |
  2348. DWC3_DEVTEN_ERRTICERREN |
  2349. DWC3_DEVTEN_WKUPEVTEN |
  2350. DWC3_DEVTEN_CONNECTDONEEN |
  2351. DWC3_DEVTEN_USBRSTEN |
  2352. DWC3_DEVTEN_DISCONNEVTEN);
  2353. if (DWC3_VER_IS_PRIOR(DWC3, 250A))
  2354. reg |= DWC3_DEVTEN_ULSTCNGEN;
  2355. /* On 2.30a and above this bit enables U3/L2-L1 Suspend Events */
  2356. if (!DWC3_VER_IS_PRIOR(DWC3, 230A))
  2357. reg |= DWC3_DEVTEN_U3L2L1SUSPEN;
  2358. dwc3_writel(dwc->regs, DWC3_DEVTEN, reg);
  2359. }
  2360. static void dwc3_gadget_disable_irq(struct dwc3 *dwc)
  2361. {
  2362. /* mask all interrupts */
  2363. dwc3_writel(dwc->regs, DWC3_DEVTEN, 0x00);
  2364. }
  2365. static irqreturn_t dwc3_interrupt(int irq, void *_dwc);
  2366. static irqreturn_t dwc3_thread_interrupt(int irq, void *_dwc);
  2367. /**
  2368. * dwc3_gadget_setup_nump - calculate and initialize NUMP field of %DWC3_DCFG
  2369. * @dwc: pointer to our context structure
  2370. *
  2371. * The following looks like complex but it's actually very simple. In order to
  2372. * calculate the number of packets we can burst at once on OUT transfers, we're
  2373. * gonna use RxFIFO size.
  2374. *
  2375. * To calculate RxFIFO size we need two numbers:
  2376. * MDWIDTH = size, in bits, of the internal memory bus
  2377. * RAM2_DEPTH = depth, in MDWIDTH, of internal RAM2 (where RxFIFO sits)
  2378. *
  2379. * Given these two numbers, the formula is simple:
  2380. *
  2381. * RxFIFO Size = (RAM2_DEPTH * MDWIDTH / 8) - 24 - 16;
  2382. *
  2383. * 24 bytes is for 3x SETUP packets
  2384. * 16 bytes is a clock domain crossing tolerance
  2385. *
  2386. * Given RxFIFO Size, NUMP = RxFIFOSize / 1024;
  2387. */
  2388. static void dwc3_gadget_setup_nump(struct dwc3 *dwc)
  2389. {
  2390. u32 ram2_depth;
  2391. u32 mdwidth;
  2392. u32 nump;
  2393. u32 reg;
  2394. ram2_depth = DWC3_GHWPARAMS7_RAM2_DEPTH(dwc->hwparams.hwparams7);
  2395. mdwidth = dwc3_mdwidth(dwc);
  2396. nump = ((ram2_depth * mdwidth / 8) - 24 - 16) / 1024;
  2397. nump = min_t(u32, nump, 16);
  2398. /* update NumP */
  2399. reg = dwc3_readl(dwc->regs, DWC3_DCFG);
  2400. reg &= ~DWC3_DCFG_NUMP_MASK;
  2401. reg |= nump << DWC3_DCFG_NUMP_SHIFT;
  2402. dwc3_writel(dwc->regs, DWC3_DCFG, reg);
  2403. }
  2404. static int __dwc3_gadget_start(struct dwc3 *dwc)
  2405. {
  2406. struct dwc3_ep *dep;
  2407. int ret = 0;
  2408. u32 reg;
  2409. /*
  2410. * Use IMOD if enabled via dwc->imod_interval. Otherwise, if
  2411. * the core supports IMOD, disable it.
  2412. */
  2413. if (dwc->imod_interval) {
  2414. dwc3_writel(dwc->regs, DWC3_DEV_IMOD(0), dwc->imod_interval);
  2415. dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(0), DWC3_GEVNTCOUNT_EHB);
  2416. } else if (dwc3_has_imod(dwc)) {
  2417. dwc3_writel(dwc->regs, DWC3_DEV_IMOD(0), 0);
  2418. }
  2419. /*
  2420. * We are telling dwc3 that we want to use DCFG.NUMP as ACK TP's NUMP
  2421. * field instead of letting dwc3 itself calculate that automatically.
  2422. *
  2423. * This way, we maximize the chances that we'll be able to get several
  2424. * bursts of data without going through any sort of endpoint throttling.
  2425. */
  2426. reg = dwc3_readl(dwc->regs, DWC3_GRXTHRCFG);
  2427. if (DWC3_IP_IS(DWC3))
  2428. reg &= ~DWC3_GRXTHRCFG_PKTCNTSEL;
  2429. else
  2430. reg &= ~DWC31_GRXTHRCFG_PKTCNTSEL;
  2431. dwc3_writel(dwc->regs, DWC3_GRXTHRCFG, reg);
  2432. dwc3_gadget_setup_nump(dwc);
  2433. /*
  2434. * Currently the controller handles single stream only. So, Ignore
  2435. * Packet Pending bit for stream selection and don't search for another
  2436. * stream if the host sends Data Packet with PP=0 (for OUT direction) or
  2437. * ACK with NumP=0 and PP=0 (for IN direction). This slightly improves
  2438. * the stream performance.
  2439. */
  2440. reg = dwc3_readl(dwc->regs, DWC3_DCFG);
  2441. reg |= DWC3_DCFG_IGNSTRMPP;
  2442. dwc3_writel(dwc->regs, DWC3_DCFG, reg);
  2443. /* Enable MST by default if the device is capable of MST */
  2444. if (DWC3_MST_CAPABLE(&dwc->hwparams)) {
  2445. reg = dwc3_readl(dwc->regs, DWC3_DCFG1);
  2446. reg &= ~DWC3_DCFG1_DIS_MST_ENH;
  2447. dwc3_writel(dwc->regs, DWC3_DCFG1, reg);
  2448. }
  2449. /* Start with SuperSpeed Default */
  2450. dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512);
  2451. ret = dwc3_gadget_start_config(dwc, 0);
  2452. if (ret) {
  2453. dev_err(dwc->dev, "failed to config endpoints\n");
  2454. return ret;
  2455. }
  2456. dep = dwc->eps[0];
  2457. dep->flags = 0;
  2458. ret = __dwc3_gadget_ep_enable(dep, DWC3_DEPCFG_ACTION_INIT);
  2459. if (ret) {
  2460. dev_err(dwc->dev, "failed to enable %s\n", dep->name);
  2461. goto err0;
  2462. }
  2463. dep = dwc->eps[1];
  2464. dep->flags = 0;
  2465. ret = __dwc3_gadget_ep_enable(dep, DWC3_DEPCFG_ACTION_INIT);
  2466. if (ret) {
  2467. dev_err(dwc->dev, "failed to enable %s\n", dep->name);
  2468. goto err1;
  2469. }
  2470. /* begin to receive SETUP packets */
  2471. dwc->ep0state = EP0_SETUP_PHASE;
  2472. dwc->ep0_bounced = false;
  2473. dwc->link_state = DWC3_LINK_STATE_SS_DIS;
  2474. dwc->delayed_status = false;
  2475. dwc3_ep0_out_start(dwc);
  2476. dwc3_gadget_enable_irq(dwc);
  2477. dwc3_enable_susphy(dwc, true);
  2478. return 0;
  2479. err1:
  2480. __dwc3_gadget_ep_disable(dwc->eps[0]);
  2481. err0:
  2482. return ret;
  2483. }
  2484. static int dwc3_gadget_start(struct usb_gadget *g,
  2485. struct usb_gadget_driver *driver)
  2486. {
  2487. struct dwc3 *dwc = gadget_to_dwc(g);
  2488. unsigned long flags;
  2489. int ret;
  2490. int irq;
  2491. irq = dwc->irq_gadget;
  2492. ret = request_threaded_irq(irq, dwc3_interrupt, dwc3_thread_interrupt,
  2493. IRQF_SHARED, "dwc3", dwc->ev_buf);
  2494. if (ret) {
  2495. dev_err(dwc->dev, "failed to request irq #%d --> %d\n",
  2496. irq, ret);
  2497. return ret;
  2498. }
  2499. spin_lock_irqsave(&dwc->lock, flags);
  2500. dwc->gadget_driver = driver;
  2501. spin_unlock_irqrestore(&dwc->lock, flags);
  2502. if (dwc->sys_wakeup)
  2503. device_wakeup_enable(dwc->sysdev);
  2504. return 0;
  2505. }
  2506. static void __dwc3_gadget_stop(struct dwc3 *dwc)
  2507. {
  2508. dwc3_gadget_disable_irq(dwc);
  2509. __dwc3_gadget_ep_disable(dwc->eps[0]);
  2510. __dwc3_gadget_ep_disable(dwc->eps[1]);
  2511. }
  2512. static int dwc3_gadget_stop(struct usb_gadget *g)
  2513. {
  2514. struct dwc3 *dwc = gadget_to_dwc(g);
  2515. unsigned long flags;
  2516. if (dwc->sys_wakeup)
  2517. device_wakeup_disable(dwc->sysdev);
  2518. spin_lock_irqsave(&dwc->lock, flags);
  2519. dwc->gadget_driver = NULL;
  2520. dwc->max_cfg_eps = 0;
  2521. spin_unlock_irqrestore(&dwc->lock, flags);
  2522. free_irq(dwc->irq_gadget, dwc->ev_buf);
  2523. return 0;
  2524. }
  2525. static void dwc3_gadget_config_params(struct usb_gadget *g,
  2526. struct usb_dcd_config_params *params)
  2527. {
  2528. struct dwc3 *dwc = gadget_to_dwc(g);
  2529. params->besl_baseline = USB_DEFAULT_BESL_UNSPECIFIED;
  2530. params->besl_deep = USB_DEFAULT_BESL_UNSPECIFIED;
  2531. /* Recommended BESL */
  2532. if (!dwc->dis_enblslpm_quirk) {
  2533. /*
  2534. * If the recommended BESL baseline is 0 or if the BESL deep is
  2535. * less than 2, Microsoft's Windows 10 host usb stack will issue
  2536. * a usb reset immediately after it receives the extended BOS
  2537. * descriptor and the enumeration will fail. To maintain
  2538. * compatibility with the Windows' usb stack, let's set the
  2539. * recommended BESL baseline to 1 and clamp the BESL deep to be
  2540. * within 2 to 15.
  2541. */
  2542. params->besl_baseline = 1;
  2543. if (dwc->is_utmi_l1_suspend)
  2544. params->besl_deep =
  2545. clamp_t(u8, dwc->hird_threshold, 2, 15);
  2546. }
  2547. /* U1 Device exit Latency */
  2548. if (dwc->dis_u1_entry_quirk)
  2549. params->bU1devExitLat = 0;
  2550. else
  2551. params->bU1devExitLat = DWC3_DEFAULT_U1_DEV_EXIT_LAT;
  2552. /* U2 Device exit Latency */
  2553. if (dwc->dis_u2_entry_quirk)
  2554. params->bU2DevExitLat = 0;
  2555. else
  2556. params->bU2DevExitLat =
  2557. cpu_to_le16(DWC3_DEFAULT_U2_DEV_EXIT_LAT);
  2558. }
  2559. static void dwc3_gadget_set_speed(struct usb_gadget *g,
  2560. enum usb_device_speed speed)
  2561. {
  2562. struct dwc3 *dwc = gadget_to_dwc(g);
  2563. unsigned long flags;
  2564. spin_lock_irqsave(&dwc->lock, flags);
  2565. dwc->gadget_max_speed = speed;
  2566. spin_unlock_irqrestore(&dwc->lock, flags);
  2567. }
  2568. static void dwc3_gadget_set_ssp_rate(struct usb_gadget *g,
  2569. enum usb_ssp_rate rate)
  2570. {
  2571. struct dwc3 *dwc = gadget_to_dwc(g);
  2572. unsigned long flags;
  2573. spin_lock_irqsave(&dwc->lock, flags);
  2574. dwc->gadget_max_speed = USB_SPEED_SUPER_PLUS;
  2575. dwc->gadget_ssp_rate = rate;
  2576. spin_unlock_irqrestore(&dwc->lock, flags);
  2577. }
  2578. static int dwc3_gadget_vbus_draw(struct usb_gadget *g, unsigned int mA)
  2579. {
  2580. struct dwc3 *dwc = gadget_to_dwc(g);
  2581. union power_supply_propval val = {0};
  2582. int ret;
  2583. if (dwc->usb2_phy)
  2584. return usb_phy_set_power(dwc->usb2_phy, mA);
  2585. if (!dwc->usb_psy)
  2586. return -EOPNOTSUPP;
  2587. val.intval = 1000 * mA;
  2588. ret = power_supply_set_property(dwc->usb_psy, POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT, &val);
  2589. return ret;
  2590. }
  2591. /**
  2592. * dwc3_gadget_check_config - ensure dwc3 can support the USB configuration
  2593. * @g: pointer to the USB gadget
  2594. *
  2595. * Used to record the maximum number of endpoints being used in a USB composite
  2596. * device. (across all configurations) This is to be used in the calculation
  2597. * of the TXFIFO sizes when resizing internal memory for individual endpoints.
  2598. * It will help ensured that the resizing logic reserves enough space for at
  2599. * least one max packet.
  2600. */
  2601. static int dwc3_gadget_check_config(struct usb_gadget *g)
  2602. {
  2603. struct dwc3 *dwc = gadget_to_dwc(g);
  2604. struct usb_ep *ep;
  2605. int fifo_size = 0;
  2606. int ram_depth;
  2607. int ep_num = 0;
  2608. if (!dwc->do_fifo_resize)
  2609. return 0;
  2610. list_for_each_entry(ep, &g->ep_list, ep_list) {
  2611. /* Only interested in the IN endpoints */
  2612. if (ep->claimed && (ep->address & USB_DIR_IN))
  2613. ep_num++;
  2614. }
  2615. if (ep_num <= dwc->max_cfg_eps)
  2616. return 0;
  2617. /* Update the max number of eps in the composition */
  2618. dwc->max_cfg_eps = ep_num;
  2619. fifo_size = dwc3_gadget_calc_tx_fifo_size(dwc, dwc->max_cfg_eps);
  2620. /* Based on the equation, increment by one for every ep */
  2621. fifo_size += dwc->max_cfg_eps;
  2622. /* Check if we can fit a single fifo per endpoint */
  2623. ram_depth = dwc3_gadget_calc_ram_depth(dwc);
  2624. if (fifo_size > ram_depth)
  2625. return -ENOMEM;
  2626. return 0;
  2627. }
  2628. static void dwc3_gadget_async_callbacks(struct usb_gadget *g, bool enable)
  2629. {
  2630. struct dwc3 *dwc = gadget_to_dwc(g);
  2631. unsigned long flags;
  2632. spin_lock_irqsave(&dwc->lock, flags);
  2633. dwc->async_callbacks = enable;
  2634. spin_unlock_irqrestore(&dwc->lock, flags);
  2635. }
  2636. static const struct usb_gadget_ops dwc3_gadget_ops = {
  2637. .get_frame = dwc3_gadget_get_frame,
  2638. .wakeup = dwc3_gadget_wakeup,
  2639. .func_wakeup = dwc3_gadget_func_wakeup,
  2640. .set_remote_wakeup = dwc3_gadget_set_remote_wakeup,
  2641. .set_selfpowered = dwc3_gadget_set_selfpowered,
  2642. .pullup = dwc3_gadget_pullup,
  2643. .udc_start = dwc3_gadget_start,
  2644. .udc_stop = dwc3_gadget_stop,
  2645. .udc_set_speed = dwc3_gadget_set_speed,
  2646. .udc_set_ssp_rate = dwc3_gadget_set_ssp_rate,
  2647. .get_config_params = dwc3_gadget_config_params,
  2648. .vbus_draw = dwc3_gadget_vbus_draw,
  2649. .check_config = dwc3_gadget_check_config,
  2650. .udc_async_callbacks = dwc3_gadget_async_callbacks,
  2651. };
  2652. /* -------------------------------------------------------------------------- */
  2653. static int dwc3_gadget_init_control_endpoint(struct dwc3_ep *dep)
  2654. {
  2655. struct dwc3 *dwc = dep->dwc;
  2656. usb_ep_set_maxpacket_limit(&dep->endpoint, 512);
  2657. dep->endpoint.maxburst = 1;
  2658. dep->endpoint.ops = &dwc3_gadget_ep0_ops;
  2659. if (!dep->direction)
  2660. dwc->gadget->ep0 = &dep->endpoint;
  2661. dep->endpoint.caps.type_control = true;
  2662. return 0;
  2663. }
  2664. static int dwc3_gadget_init_in_endpoint(struct dwc3_ep *dep)
  2665. {
  2666. struct dwc3 *dwc = dep->dwc;
  2667. u32 mdwidth;
  2668. int size;
  2669. int maxpacket;
  2670. mdwidth = dwc3_mdwidth(dwc);
  2671. /* MDWIDTH is represented in bits, we need it in bytes */
  2672. mdwidth /= 8;
  2673. size = dwc3_readl(dwc->regs, DWC3_GTXFIFOSIZ(dep->number >> 1));
  2674. if (DWC3_IP_IS(DWC3))
  2675. size = DWC3_GTXFIFOSIZ_TXFDEP(size);
  2676. else
  2677. size = DWC31_GTXFIFOSIZ_TXFDEP(size);
  2678. /*
  2679. * maxpacket size is determined as part of the following, after assuming
  2680. * a mult value of one maxpacket:
  2681. * DWC3 revision 280A and prior:
  2682. * fifo_size = mult * (max_packet / mdwidth) + 1;
  2683. * maxpacket = mdwidth * (fifo_size - 1);
  2684. *
  2685. * DWC3 revision 290A and onwards:
  2686. * fifo_size = mult * ((max_packet + mdwidth)/mdwidth + 1) + 1
  2687. * maxpacket = mdwidth * ((fifo_size - 1) - 1) - mdwidth;
  2688. */
  2689. if (DWC3_VER_IS_PRIOR(DWC3, 290A))
  2690. maxpacket = mdwidth * (size - 1);
  2691. else
  2692. maxpacket = mdwidth * ((size - 1) - 1) - mdwidth;
  2693. /* Functionally, space for one max packet is sufficient */
  2694. size = min_t(int, maxpacket, 1024);
  2695. usb_ep_set_maxpacket_limit(&dep->endpoint, size);
  2696. dep->endpoint.max_streams = 16;
  2697. dep->endpoint.ops = &dwc3_gadget_ep_ops;
  2698. list_add_tail(&dep->endpoint.ep_list,
  2699. &dwc->gadget->ep_list);
  2700. dep->endpoint.caps.type_iso = true;
  2701. dep->endpoint.caps.type_bulk = true;
  2702. dep->endpoint.caps.type_int = true;
  2703. return dwc3_alloc_trb_pool(dep);
  2704. }
  2705. static int dwc3_gadget_init_out_endpoint(struct dwc3_ep *dep)
  2706. {
  2707. struct dwc3 *dwc = dep->dwc;
  2708. u32 mdwidth;
  2709. int size;
  2710. mdwidth = dwc3_mdwidth(dwc);
  2711. /* MDWIDTH is represented in bits, convert to bytes */
  2712. mdwidth /= 8;
  2713. /* All OUT endpoints share a single RxFIFO space */
  2714. size = dwc3_readl(dwc->regs, DWC3_GRXFIFOSIZ(0));
  2715. if (DWC3_IP_IS(DWC3))
  2716. size = DWC3_GRXFIFOSIZ_RXFDEP(size);
  2717. else
  2718. size = DWC31_GRXFIFOSIZ_RXFDEP(size);
  2719. /* FIFO depth is in MDWDITH bytes */
  2720. size *= mdwidth;
  2721. /*
  2722. * To meet performance requirement, a minimum recommended RxFIFO size
  2723. * is defined as follow:
  2724. * RxFIFO size >= (3 x MaxPacketSize) +
  2725. * (3 x 8 bytes setup packets size) + (16 bytes clock crossing margin)
  2726. *
  2727. * Then calculate the max packet limit as below.
  2728. */
  2729. size -= (3 * 8) + 16;
  2730. if (size < 0)
  2731. size = 0;
  2732. else
  2733. size /= 3;
  2734. usb_ep_set_maxpacket_limit(&dep->endpoint, size);
  2735. dep->endpoint.max_streams = 16;
  2736. dep->endpoint.ops = &dwc3_gadget_ep_ops;
  2737. list_add_tail(&dep->endpoint.ep_list,
  2738. &dwc->gadget->ep_list);
  2739. dep->endpoint.caps.type_iso = true;
  2740. dep->endpoint.caps.type_bulk = true;
  2741. dep->endpoint.caps.type_int = true;
  2742. return dwc3_alloc_trb_pool(dep);
  2743. }
  2744. static int dwc3_gadget_init_endpoint(struct dwc3 *dwc, u8 epnum)
  2745. {
  2746. struct dwc3_ep *dep;
  2747. bool direction = epnum & 1;
  2748. int ret;
  2749. u8 num = epnum >> 1;
  2750. dep = kzalloc(sizeof(*dep), GFP_KERNEL);
  2751. if (!dep)
  2752. return -ENOMEM;
  2753. dep->dwc = dwc;
  2754. dep->number = epnum;
  2755. dep->direction = direction;
  2756. dep->regs = dwc->regs + DWC3_DEP_BASE(epnum);
  2757. dwc->eps[epnum] = dep;
  2758. dep->combo_num = 0;
  2759. dep->start_cmd_status = 0;
  2760. snprintf(dep->name, sizeof(dep->name), "ep%u%s", num,
  2761. direction ? "in" : "out");
  2762. dep->endpoint.name = dep->name;
  2763. if (!(dep->number > 1)) {
  2764. dep->endpoint.desc = &dwc3_gadget_ep0_desc;
  2765. dep->endpoint.comp_desc = NULL;
  2766. }
  2767. if (num == 0)
  2768. ret = dwc3_gadget_init_control_endpoint(dep);
  2769. else if (direction)
  2770. ret = dwc3_gadget_init_in_endpoint(dep);
  2771. else
  2772. ret = dwc3_gadget_init_out_endpoint(dep);
  2773. if (ret)
  2774. return ret;
  2775. dep->endpoint.caps.dir_in = direction;
  2776. dep->endpoint.caps.dir_out = !direction;
  2777. INIT_LIST_HEAD(&dep->pending_list);
  2778. INIT_LIST_HEAD(&dep->started_list);
  2779. INIT_LIST_HEAD(&dep->cancelled_list);
  2780. dwc3_debugfs_create_endpoint_dir(dep);
  2781. return 0;
  2782. }
  2783. static int dwc3_gadget_init_endpoints(struct dwc3 *dwc, u8 total)
  2784. {
  2785. u8 epnum;
  2786. INIT_LIST_HEAD(&dwc->gadget->ep_list);
  2787. for (epnum = 0; epnum < total; epnum++) {
  2788. int ret;
  2789. ret = dwc3_gadget_init_endpoint(dwc, epnum);
  2790. if (ret)
  2791. return ret;
  2792. }
  2793. return 0;
  2794. }
  2795. static void dwc3_gadget_free_endpoints(struct dwc3 *dwc)
  2796. {
  2797. struct dwc3_ep *dep;
  2798. u8 epnum;
  2799. for (epnum = 0; epnum < DWC3_ENDPOINTS_NUM; epnum++) {
  2800. dep = dwc->eps[epnum];
  2801. if (!dep)
  2802. continue;
  2803. /*
  2804. * Physical endpoints 0 and 1 are special; they form the
  2805. * bi-directional USB endpoint 0.
  2806. *
  2807. * For those two physical endpoints, we don't allocate a TRB
  2808. * pool nor do we add them the endpoints list. Due to that, we
  2809. * shouldn't do these two operations otherwise we would end up
  2810. * with all sorts of bugs when removing dwc3.ko.
  2811. */
  2812. if (epnum != 0 && epnum != 1) {
  2813. dwc3_free_trb_pool(dep);
  2814. list_del(&dep->endpoint.ep_list);
  2815. }
  2816. dwc3_debugfs_remove_endpoint_dir(dep);
  2817. kfree(dep);
  2818. }
  2819. }
  2820. /* -------------------------------------------------------------------------- */
  2821. static int dwc3_gadget_ep_reclaim_completed_trb(struct dwc3_ep *dep,
  2822. struct dwc3_request *req, struct dwc3_trb *trb,
  2823. const struct dwc3_event_depevt *event, int status, int chain)
  2824. {
  2825. unsigned int count;
  2826. dwc3_ep_inc_deq(dep);
  2827. trace_dwc3_complete_trb(dep, trb);
  2828. req->num_trbs--;
  2829. /*
  2830. * If we're in the middle of series of chained TRBs and we
  2831. * receive a short transfer along the way, DWC3 will skip
  2832. * through all TRBs including the last TRB in the chain (the
  2833. * where CHN bit is zero. DWC3 will also avoid clearing HWO
  2834. * bit and SW has to do it manually.
  2835. *
  2836. * We're going to do that here to avoid problems of HW trying
  2837. * to use bogus TRBs for transfers.
  2838. */
  2839. if (chain && (trb->ctrl & DWC3_TRB_CTRL_HWO))
  2840. trb->ctrl &= ~DWC3_TRB_CTRL_HWO;
  2841. /*
  2842. * For isochronous transfers, the first TRB in a service interval must
  2843. * have the Isoc-First type. Track and report its interval frame number.
  2844. */
  2845. if (usb_endpoint_xfer_isoc(dep->endpoint.desc) &&
  2846. (trb->ctrl & DWC3_TRBCTL_ISOCHRONOUS_FIRST)) {
  2847. unsigned int frame_number;
  2848. frame_number = DWC3_TRB_CTRL_GET_SID_SOFN(trb->ctrl);
  2849. frame_number &= ~(dep->interval - 1);
  2850. req->request.frame_number = frame_number;
  2851. }
  2852. /*
  2853. * We use bounce buffer for requests that needs extra TRB or OUT ZLP. If
  2854. * this TRB points to the bounce buffer address, it's a MPS alignment
  2855. * TRB. Don't add it to req->remaining calculation.
  2856. */
  2857. if (trb->bpl == lower_32_bits(dep->dwc->bounce_addr) &&
  2858. trb->bph == upper_32_bits(dep->dwc->bounce_addr)) {
  2859. trb->ctrl &= ~DWC3_TRB_CTRL_HWO;
  2860. return 1;
  2861. }
  2862. count = trb->size & DWC3_TRB_SIZE_MASK;
  2863. req->remaining += count;
  2864. if ((trb->ctrl & DWC3_TRB_CTRL_HWO) && status != -ESHUTDOWN)
  2865. return 1;
  2866. if (event->status & DEPEVT_STATUS_SHORT && !chain)
  2867. return 1;
  2868. if ((trb->ctrl & DWC3_TRB_CTRL_ISP_IMI) &&
  2869. DWC3_TRB_SIZE_TRBSTS(trb->size) == DWC3_TRBSTS_MISSED_ISOC)
  2870. return 1;
  2871. if ((trb->ctrl & DWC3_TRB_CTRL_IOC) ||
  2872. (trb->ctrl & DWC3_TRB_CTRL_LST))
  2873. return 1;
  2874. return 0;
  2875. }
  2876. static int dwc3_gadget_ep_reclaim_trb_sg(struct dwc3_ep *dep,
  2877. struct dwc3_request *req, const struct dwc3_event_depevt *event,
  2878. int status)
  2879. {
  2880. struct dwc3_trb *trb;
  2881. struct scatterlist *sg = req->sg;
  2882. struct scatterlist *s;
  2883. unsigned int num_queued = req->num_queued_sgs;
  2884. unsigned int i;
  2885. int ret = 0;
  2886. for_each_sg(sg, s, num_queued, i) {
  2887. trb = &dep->trb_pool[dep->trb_dequeue];
  2888. req->sg = sg_next(s);
  2889. req->num_queued_sgs--;
  2890. ret = dwc3_gadget_ep_reclaim_completed_trb(dep, req,
  2891. trb, event, status, true);
  2892. if (ret)
  2893. break;
  2894. }
  2895. return ret;
  2896. }
  2897. static int dwc3_gadget_ep_reclaim_trb_linear(struct dwc3_ep *dep,
  2898. struct dwc3_request *req, const struct dwc3_event_depevt *event,
  2899. int status)
  2900. {
  2901. struct dwc3_trb *trb = &dep->trb_pool[dep->trb_dequeue];
  2902. return dwc3_gadget_ep_reclaim_completed_trb(dep, req, trb,
  2903. event, status, false);
  2904. }
  2905. static bool dwc3_gadget_ep_request_completed(struct dwc3_request *req)
  2906. {
  2907. return req->num_pending_sgs == 0 && req->num_queued_sgs == 0;
  2908. }
  2909. static int dwc3_gadget_ep_cleanup_completed_request(struct dwc3_ep *dep,
  2910. const struct dwc3_event_depevt *event,
  2911. struct dwc3_request *req, int status)
  2912. {
  2913. int request_status;
  2914. int ret;
  2915. if (req->request.num_mapped_sgs)
  2916. ret = dwc3_gadget_ep_reclaim_trb_sg(dep, req, event,
  2917. status);
  2918. else
  2919. ret = dwc3_gadget_ep_reclaim_trb_linear(dep, req, event,
  2920. status);
  2921. req->request.actual = req->request.length - req->remaining;
  2922. if (!dwc3_gadget_ep_request_completed(req))
  2923. goto out;
  2924. if (req->needs_extra_trb) {
  2925. ret = dwc3_gadget_ep_reclaim_trb_linear(dep, req, event,
  2926. status);
  2927. req->needs_extra_trb = false;
  2928. }
  2929. /*
  2930. * The event status only reflects the status of the TRB with IOC set.
  2931. * For the requests that don't set interrupt on completion, the driver
  2932. * needs to check and return the status of the completed TRBs associated
  2933. * with the request. Use the status of the last TRB of the request.
  2934. */
  2935. if (req->request.no_interrupt) {
  2936. struct dwc3_trb *trb;
  2937. trb = dwc3_ep_prev_trb(dep, dep->trb_dequeue);
  2938. switch (DWC3_TRB_SIZE_TRBSTS(trb->size)) {
  2939. case DWC3_TRBSTS_MISSED_ISOC:
  2940. /* Isoc endpoint only */
  2941. request_status = -EXDEV;
  2942. break;
  2943. case DWC3_TRB_STS_XFER_IN_PROG:
  2944. /* Applicable when End Transfer with ForceRM=0 */
  2945. case DWC3_TRBSTS_SETUP_PENDING:
  2946. /* Control endpoint only */
  2947. case DWC3_TRBSTS_OK:
  2948. default:
  2949. request_status = 0;
  2950. break;
  2951. }
  2952. } else {
  2953. request_status = status;
  2954. }
  2955. dwc3_gadget_giveback(dep, req, request_status);
  2956. out:
  2957. return ret;
  2958. }
  2959. static void dwc3_gadget_ep_cleanup_completed_requests(struct dwc3_ep *dep,
  2960. const struct dwc3_event_depevt *event, int status)
  2961. {
  2962. struct dwc3_request *req;
  2963. while (!list_empty(&dep->started_list)) {
  2964. int ret;
  2965. req = next_request(&dep->started_list);
  2966. ret = dwc3_gadget_ep_cleanup_completed_request(dep, event,
  2967. req, status);
  2968. if (ret)
  2969. break;
  2970. /*
  2971. * The endpoint is disabled, let the dwc3_remove_requests()
  2972. * handle the cleanup.
  2973. */
  2974. if (!dep->endpoint.desc)
  2975. break;
  2976. }
  2977. }
  2978. static bool dwc3_gadget_ep_should_continue(struct dwc3_ep *dep)
  2979. {
  2980. struct dwc3_request *req;
  2981. struct dwc3 *dwc = dep->dwc;
  2982. if (!dep->endpoint.desc || !dwc->pullups_connected ||
  2983. !dwc->connected)
  2984. return false;
  2985. if (!list_empty(&dep->pending_list))
  2986. return true;
  2987. /*
  2988. * We only need to check the first entry of the started list. We can
  2989. * assume the completed requests are removed from the started list.
  2990. */
  2991. req = next_request(&dep->started_list);
  2992. if (!req)
  2993. return false;
  2994. return !dwc3_gadget_ep_request_completed(req);
  2995. }
  2996. static void dwc3_gadget_endpoint_frame_from_event(struct dwc3_ep *dep,
  2997. const struct dwc3_event_depevt *event)
  2998. {
  2999. dep->frame_number = event->parameters;
  3000. }
  3001. static bool dwc3_gadget_endpoint_trbs_complete(struct dwc3_ep *dep,
  3002. const struct dwc3_event_depevt *event, int status)
  3003. {
  3004. struct dwc3 *dwc = dep->dwc;
  3005. bool no_started_trb = true;
  3006. dwc3_gadget_ep_cleanup_completed_requests(dep, event, status);
  3007. if (dep->flags & DWC3_EP_END_TRANSFER_PENDING)
  3008. goto out;
  3009. if (!dep->endpoint.desc)
  3010. return no_started_trb;
  3011. if (usb_endpoint_xfer_isoc(dep->endpoint.desc) &&
  3012. list_empty(&dep->started_list) &&
  3013. (list_empty(&dep->pending_list) || status == -EXDEV))
  3014. dwc3_stop_active_transfer(dep, true, true);
  3015. else if (dwc3_gadget_ep_should_continue(dep))
  3016. if (__dwc3_gadget_kick_transfer(dep) == 0)
  3017. no_started_trb = false;
  3018. out:
  3019. /*
  3020. * WORKAROUND: This is the 2nd half of U1/U2 -> U0 workaround.
  3021. * See dwc3_gadget_linksts_change_interrupt() for 1st half.
  3022. */
  3023. if (DWC3_VER_IS_PRIOR(DWC3, 183A)) {
  3024. u32 reg;
  3025. int i;
  3026. for (i = 0; i < DWC3_ENDPOINTS_NUM; i++) {
  3027. dep = dwc->eps[i];
  3028. if (!(dep->flags & DWC3_EP_ENABLED))
  3029. continue;
  3030. if (!list_empty(&dep->started_list))
  3031. return no_started_trb;
  3032. }
  3033. reg = dwc3_readl(dwc->regs, DWC3_DCTL);
  3034. reg |= dwc->u1u2;
  3035. dwc3_writel(dwc->regs, DWC3_DCTL, reg);
  3036. dwc->u1u2 = 0;
  3037. }
  3038. return no_started_trb;
  3039. }
  3040. static void dwc3_gadget_endpoint_transfer_in_progress(struct dwc3_ep *dep,
  3041. const struct dwc3_event_depevt *event)
  3042. {
  3043. int status = 0;
  3044. if (!dep->endpoint.desc)
  3045. return;
  3046. if (usb_endpoint_xfer_isoc(dep->endpoint.desc))
  3047. dwc3_gadget_endpoint_frame_from_event(dep, event);
  3048. if (event->status & DEPEVT_STATUS_BUSERR)
  3049. status = -ECONNRESET;
  3050. if (event->status & DEPEVT_STATUS_MISSED_ISOC)
  3051. status = -EXDEV;
  3052. dwc3_gadget_endpoint_trbs_complete(dep, event, status);
  3053. }
  3054. static void dwc3_gadget_endpoint_transfer_complete(struct dwc3_ep *dep,
  3055. const struct dwc3_event_depevt *event)
  3056. {
  3057. int status = 0;
  3058. dep->flags &= ~DWC3_EP_TRANSFER_STARTED;
  3059. if (event->status & DEPEVT_STATUS_BUSERR)
  3060. status = -ECONNRESET;
  3061. if (dwc3_gadget_endpoint_trbs_complete(dep, event, status))
  3062. dep->flags &= ~DWC3_EP_WAIT_TRANSFER_COMPLETE;
  3063. }
  3064. static void dwc3_gadget_endpoint_transfer_not_ready(struct dwc3_ep *dep,
  3065. const struct dwc3_event_depevt *event)
  3066. {
  3067. dwc3_gadget_endpoint_frame_from_event(dep, event);
  3068. /*
  3069. * The XferNotReady event is generated only once before the endpoint
  3070. * starts. It will be generated again when END_TRANSFER command is
  3071. * issued. For some controller versions, the XferNotReady event may be
  3072. * generated while the END_TRANSFER command is still in process. Ignore
  3073. * it and wait for the next XferNotReady event after the command is
  3074. * completed.
  3075. */
  3076. if (dep->flags & DWC3_EP_END_TRANSFER_PENDING)
  3077. return;
  3078. (void) __dwc3_gadget_start_isoc(dep);
  3079. }
  3080. static void dwc3_gadget_endpoint_command_complete(struct dwc3_ep *dep,
  3081. const struct dwc3_event_depevt *event)
  3082. {
  3083. u8 cmd = DEPEVT_PARAMETER_CMD(event->parameters);
  3084. if (cmd != DWC3_DEPCMD_ENDTRANSFER)
  3085. return;
  3086. /*
  3087. * The END_TRANSFER command will cause the controller to generate a
  3088. * NoStream Event, and it's not due to the host DP NoStream rejection.
  3089. * Ignore the next NoStream event.
  3090. */
  3091. if (dep->stream_capable)
  3092. dep->flags |= DWC3_EP_IGNORE_NEXT_NOSTREAM;
  3093. dep->flags &= ~DWC3_EP_END_TRANSFER_PENDING;
  3094. dep->flags &= ~DWC3_EP_TRANSFER_STARTED;
  3095. dwc3_gadget_ep_cleanup_cancelled_requests(dep);
  3096. if (dep->flags & DWC3_EP_PENDING_CLEAR_STALL) {
  3097. struct dwc3 *dwc = dep->dwc;
  3098. dep->flags &= ~DWC3_EP_PENDING_CLEAR_STALL;
  3099. if (dwc3_send_clear_stall_ep_cmd(dep)) {
  3100. struct usb_ep *ep0 = &dwc->eps[0]->endpoint;
  3101. dev_err(dwc->dev, "failed to clear STALL on %s\n", dep->name);
  3102. if (dwc->delayed_status)
  3103. __dwc3_gadget_ep0_set_halt(ep0, 1);
  3104. return;
  3105. }
  3106. dep->flags &= ~(DWC3_EP_STALL | DWC3_EP_WEDGE);
  3107. if (dwc->clear_stall_protocol == dep->number)
  3108. dwc3_ep0_send_delayed_status(dwc);
  3109. }
  3110. if ((dep->flags & DWC3_EP_DELAY_START) &&
  3111. !usb_endpoint_xfer_isoc(dep->endpoint.desc))
  3112. __dwc3_gadget_kick_transfer(dep);
  3113. dep->flags &= ~DWC3_EP_DELAY_START;
  3114. }
  3115. static void dwc3_gadget_endpoint_stream_event(struct dwc3_ep *dep,
  3116. const struct dwc3_event_depevt *event)
  3117. {
  3118. struct dwc3 *dwc = dep->dwc;
  3119. if (event->status == DEPEVT_STREAMEVT_FOUND) {
  3120. dep->flags |= DWC3_EP_FIRST_STREAM_PRIMED;
  3121. goto out;
  3122. }
  3123. /* Note: NoStream rejection event param value is 0 and not 0xFFFF */
  3124. switch (event->parameters) {
  3125. case DEPEVT_STREAM_PRIME:
  3126. /*
  3127. * If the host can properly transition the endpoint state from
  3128. * idle to prime after a NoStream rejection, there's no need to
  3129. * force restarting the endpoint to reinitiate the stream. To
  3130. * simplify the check, assume the host follows the USB spec if
  3131. * it primed the endpoint more than once.
  3132. */
  3133. if (dep->flags & DWC3_EP_FORCE_RESTART_STREAM) {
  3134. if (dep->flags & DWC3_EP_FIRST_STREAM_PRIMED)
  3135. dep->flags &= ~DWC3_EP_FORCE_RESTART_STREAM;
  3136. else
  3137. dep->flags |= DWC3_EP_FIRST_STREAM_PRIMED;
  3138. }
  3139. break;
  3140. case DEPEVT_STREAM_NOSTREAM:
  3141. if ((dep->flags & DWC3_EP_IGNORE_NEXT_NOSTREAM) ||
  3142. !(dep->flags & DWC3_EP_FORCE_RESTART_STREAM) ||
  3143. (!DWC3_MST_CAPABLE(&dwc->hwparams) &&
  3144. !(dep->flags & DWC3_EP_WAIT_TRANSFER_COMPLETE)))
  3145. break;
  3146. /*
  3147. * If the host rejects a stream due to no active stream, by the
  3148. * USB and xHCI spec, the endpoint will be put back to idle
  3149. * state. When the host is ready (buffer added/updated), it will
  3150. * prime the endpoint to inform the usb device controller. This
  3151. * triggers the device controller to issue ERDY to restart the
  3152. * stream. However, some hosts don't follow this and keep the
  3153. * endpoint in the idle state. No prime will come despite host
  3154. * streams are updated, and the device controller will not be
  3155. * triggered to generate ERDY to move the next stream data. To
  3156. * workaround this and maintain compatibility with various
  3157. * hosts, force to reinitiate the stream until the host is ready
  3158. * instead of waiting for the host to prime the endpoint.
  3159. */
  3160. if (DWC3_VER_IS_WITHIN(DWC32, 100A, ANY)) {
  3161. unsigned int cmd = DWC3_DGCMD_SET_ENDPOINT_PRIME;
  3162. dwc3_send_gadget_generic_command(dwc, cmd, dep->number);
  3163. } else {
  3164. dep->flags |= DWC3_EP_DELAY_START;
  3165. dwc3_stop_active_transfer(dep, true, true);
  3166. return;
  3167. }
  3168. break;
  3169. }
  3170. out:
  3171. dep->flags &= ~DWC3_EP_IGNORE_NEXT_NOSTREAM;
  3172. }
  3173. static void dwc3_endpoint_interrupt(struct dwc3 *dwc,
  3174. const struct dwc3_event_depevt *event)
  3175. {
  3176. struct dwc3_ep *dep;
  3177. u8 epnum = event->endpoint_number;
  3178. dep = dwc->eps[epnum];
  3179. if (!(dep->flags & DWC3_EP_ENABLED)) {
  3180. if ((epnum > 1) && !(dep->flags & DWC3_EP_TRANSFER_STARTED))
  3181. return;
  3182. /* Handle only EPCMDCMPLT when EP disabled */
  3183. if ((event->endpoint_event != DWC3_DEPEVT_EPCMDCMPLT) &&
  3184. !(epnum <= 1 && event->endpoint_event == DWC3_DEPEVT_XFERCOMPLETE))
  3185. return;
  3186. }
  3187. if (epnum == 0 || epnum == 1) {
  3188. dwc3_ep0_interrupt(dwc, event);
  3189. return;
  3190. }
  3191. switch (event->endpoint_event) {
  3192. case DWC3_DEPEVT_XFERINPROGRESS:
  3193. dwc3_gadget_endpoint_transfer_in_progress(dep, event);
  3194. break;
  3195. case DWC3_DEPEVT_XFERNOTREADY:
  3196. dwc3_gadget_endpoint_transfer_not_ready(dep, event);
  3197. break;
  3198. case DWC3_DEPEVT_EPCMDCMPLT:
  3199. dwc3_gadget_endpoint_command_complete(dep, event);
  3200. break;
  3201. case DWC3_DEPEVT_XFERCOMPLETE:
  3202. dwc3_gadget_endpoint_transfer_complete(dep, event);
  3203. break;
  3204. case DWC3_DEPEVT_STREAMEVT:
  3205. dwc3_gadget_endpoint_stream_event(dep, event);
  3206. break;
  3207. case DWC3_DEPEVT_RXTXFIFOEVT:
  3208. break;
  3209. default:
  3210. dev_err(dwc->dev, "unknown endpoint event %d\n", event->endpoint_event);
  3211. break;
  3212. }
  3213. }
  3214. static void dwc3_disconnect_gadget(struct dwc3 *dwc)
  3215. {
  3216. if (dwc->async_callbacks && dwc->gadget_driver->disconnect) {
  3217. spin_unlock(&dwc->lock);
  3218. dwc->gadget_driver->disconnect(dwc->gadget);
  3219. spin_lock(&dwc->lock);
  3220. }
  3221. }
  3222. static void dwc3_suspend_gadget(struct dwc3 *dwc)
  3223. {
  3224. if (dwc->async_callbacks && dwc->gadget_driver->suspend) {
  3225. spin_unlock(&dwc->lock);
  3226. dwc->gadget_driver->suspend(dwc->gadget);
  3227. spin_lock(&dwc->lock);
  3228. }
  3229. }
  3230. static void dwc3_resume_gadget(struct dwc3 *dwc)
  3231. {
  3232. if (dwc->async_callbacks && dwc->gadget_driver->resume) {
  3233. spin_unlock(&dwc->lock);
  3234. dwc->gadget_driver->resume(dwc->gadget);
  3235. spin_lock(&dwc->lock);
  3236. }
  3237. }
  3238. static void dwc3_reset_gadget(struct dwc3 *dwc)
  3239. {
  3240. if (!dwc->gadget_driver)
  3241. return;
  3242. if (dwc->async_callbacks && dwc->gadget->speed != USB_SPEED_UNKNOWN) {
  3243. spin_unlock(&dwc->lock);
  3244. usb_gadget_udc_reset(dwc->gadget, dwc->gadget_driver);
  3245. spin_lock(&dwc->lock);
  3246. }
  3247. }
  3248. void dwc3_stop_active_transfer(struct dwc3_ep *dep, bool force,
  3249. bool interrupt)
  3250. {
  3251. struct dwc3 *dwc = dep->dwc;
  3252. /*
  3253. * Only issue End Transfer command to the control endpoint of a started
  3254. * Data Phase. Typically we should only do so in error cases such as
  3255. * invalid/unexpected direction as described in the control transfer
  3256. * flow of the programming guide.
  3257. */
  3258. if (dep->number <= 1 && dwc->ep0state != EP0_DATA_PHASE)
  3259. return;
  3260. if (interrupt && (dep->flags & DWC3_EP_DELAY_STOP))
  3261. return;
  3262. if (!(dep->flags & DWC3_EP_TRANSFER_STARTED) ||
  3263. (dep->flags & DWC3_EP_END_TRANSFER_PENDING))
  3264. return;
  3265. /*
  3266. * If a Setup packet is received but yet to DMA out, the controller will
  3267. * not process the End Transfer command of any endpoint. Polling of its
  3268. * DEPCMD.CmdAct may block setting up TRB for Setup packet, causing a
  3269. * timeout. Delay issuing the End Transfer command until the Setup TRB is
  3270. * prepared.
  3271. */
  3272. if (dwc->ep0state != EP0_SETUP_PHASE && !dwc->delayed_status) {
  3273. dep->flags |= DWC3_EP_DELAY_STOP;
  3274. return;
  3275. }
  3276. /*
  3277. * NOTICE: We are violating what the Databook says about the
  3278. * EndTransfer command. Ideally we would _always_ wait for the
  3279. * EndTransfer Command Completion IRQ, but that's causing too
  3280. * much trouble synchronizing between us and gadget driver.
  3281. *
  3282. * We have discussed this with the IP Provider and it was
  3283. * suggested to giveback all requests here.
  3284. *
  3285. * Note also that a similar handling was tested by Synopsys
  3286. * (thanks a lot Paul) and nothing bad has come out of it.
  3287. * In short, what we're doing is issuing EndTransfer with
  3288. * CMDIOC bit set and delay kicking transfer until the
  3289. * EndTransfer command had completed.
  3290. *
  3291. * As of IP version 3.10a of the DWC_usb3 IP, the controller
  3292. * supports a mode to work around the above limitation. The
  3293. * software can poll the CMDACT bit in the DEPCMD register
  3294. * after issuing a EndTransfer command. This mode is enabled
  3295. * by writing GUCTL2[14]. This polling is already done in the
  3296. * dwc3_send_gadget_ep_cmd() function so if the mode is
  3297. * enabled, the EndTransfer command will have completed upon
  3298. * returning from this function.
  3299. *
  3300. * This mode is NOT available on the DWC_usb31 IP. In this
  3301. * case, if the IOC bit is not set, then delay by 1ms
  3302. * after issuing the EndTransfer command. This allows for the
  3303. * controller to handle the command completely before DWC3
  3304. * remove requests attempts to unmap USB request buffers.
  3305. */
  3306. __dwc3_stop_active_transfer(dep, force, interrupt);
  3307. }
  3308. static void dwc3_clear_stall_all_ep(struct dwc3 *dwc)
  3309. {
  3310. u32 epnum;
  3311. for (epnum = 1; epnum < DWC3_ENDPOINTS_NUM; epnum++) {
  3312. struct dwc3_ep *dep;
  3313. int ret;
  3314. dep = dwc->eps[epnum];
  3315. if (!dep)
  3316. continue;
  3317. if (!(dep->flags & DWC3_EP_STALL))
  3318. continue;
  3319. dep->flags &= ~DWC3_EP_STALL;
  3320. ret = dwc3_send_clear_stall_ep_cmd(dep);
  3321. WARN_ON_ONCE(ret);
  3322. }
  3323. }
  3324. static void dwc3_gadget_disconnect_interrupt(struct dwc3 *dwc)
  3325. {
  3326. int reg;
  3327. dwc->suspended = false;
  3328. dwc3_gadget_set_link_state(dwc, DWC3_LINK_STATE_RX_DET);
  3329. reg = dwc3_readl(dwc->regs, DWC3_DCTL);
  3330. reg &= ~DWC3_DCTL_INITU1ENA;
  3331. reg &= ~DWC3_DCTL_INITU2ENA;
  3332. dwc3_gadget_dctl_write_safe(dwc, reg);
  3333. dwc->connected = false;
  3334. dwc3_disconnect_gadget(dwc);
  3335. dwc->gadget->speed = USB_SPEED_UNKNOWN;
  3336. dwc->setup_packet_pending = false;
  3337. dwc->gadget->wakeup_armed = false;
  3338. dwc3_gadget_enable_linksts_evts(dwc, false);
  3339. usb_gadget_set_state(dwc->gadget, USB_STATE_NOTATTACHED);
  3340. dwc3_ep0_reset_state(dwc);
  3341. /*
  3342. * Request PM idle to address condition where usage count is
  3343. * already decremented to zero, but waiting for the disconnect
  3344. * interrupt to set dwc->connected to FALSE.
  3345. */
  3346. pm_request_idle(dwc->dev);
  3347. }
  3348. static void dwc3_gadget_reset_interrupt(struct dwc3 *dwc)
  3349. {
  3350. u32 reg;
  3351. dwc->suspended = false;
  3352. /*
  3353. * Ideally, dwc3_reset_gadget() would trigger the function
  3354. * drivers to stop any active transfers through ep disable.
  3355. * However, for functions which defer ep disable, such as mass
  3356. * storage, we will need to rely on the call to stop active
  3357. * transfers here, and avoid allowing of request queuing.
  3358. */
  3359. dwc->connected = false;
  3360. /*
  3361. * WORKAROUND: DWC3 revisions <1.88a have an issue which
  3362. * would cause a missing Disconnect Event if there's a
  3363. * pending Setup Packet in the FIFO.
  3364. *
  3365. * There's no suggested workaround on the official Bug
  3366. * report, which states that "unless the driver/application
  3367. * is doing any special handling of a disconnect event,
  3368. * there is no functional issue".
  3369. *
  3370. * Unfortunately, it turns out that we _do_ some special
  3371. * handling of a disconnect event, namely complete all
  3372. * pending transfers, notify gadget driver of the
  3373. * disconnection, and so on.
  3374. *
  3375. * Our suggested workaround is to follow the Disconnect
  3376. * Event steps here, instead, based on a setup_packet_pending
  3377. * flag. Such flag gets set whenever we have a SETUP_PENDING
  3378. * status for EP0 TRBs and gets cleared on XferComplete for the
  3379. * same endpoint.
  3380. *
  3381. * Refers to:
  3382. *
  3383. * STAR#9000466709: RTL: Device : Disconnect event not
  3384. * generated if setup packet pending in FIFO
  3385. */
  3386. if (DWC3_VER_IS_PRIOR(DWC3, 188A)) {
  3387. if (dwc->setup_packet_pending)
  3388. dwc3_gadget_disconnect_interrupt(dwc);
  3389. }
  3390. dwc3_reset_gadget(dwc);
  3391. /*
  3392. * From SNPS databook section 8.1.2, the EP0 should be in setup
  3393. * phase. So ensure that EP0 is in setup phase by issuing a stall
  3394. * and restart if EP0 is not in setup phase.
  3395. */
  3396. dwc3_ep0_reset_state(dwc);
  3397. /*
  3398. * In the Synopsis DesignWare Cores USB3 Databook Rev. 3.30a
  3399. * Section 4.1.2 Table 4-2, it states that during a USB reset, the SW
  3400. * needs to ensure that it sends "a DEPENDXFER command for any active
  3401. * transfers."
  3402. */
  3403. dwc3_stop_active_transfers(dwc);
  3404. dwc->connected = true;
  3405. reg = dwc3_readl(dwc->regs, DWC3_DCTL);
  3406. reg &= ~DWC3_DCTL_TSTCTRL_MASK;
  3407. dwc3_gadget_dctl_write_safe(dwc, reg);
  3408. dwc->test_mode = false;
  3409. dwc->gadget->wakeup_armed = false;
  3410. dwc3_gadget_enable_linksts_evts(dwc, false);
  3411. dwc3_clear_stall_all_ep(dwc);
  3412. /* Reset device address to zero */
  3413. reg = dwc3_readl(dwc->regs, DWC3_DCFG);
  3414. reg &= ~(DWC3_DCFG_DEVADDR_MASK);
  3415. dwc3_writel(dwc->regs, DWC3_DCFG, reg);
  3416. }
  3417. static void dwc3_gadget_conndone_interrupt(struct dwc3 *dwc)
  3418. {
  3419. struct dwc3_ep *dep;
  3420. int ret;
  3421. u32 reg;
  3422. u8 lanes = 1;
  3423. u8 speed;
  3424. if (!dwc->softconnect)
  3425. return;
  3426. reg = dwc3_readl(dwc->regs, DWC3_DSTS);
  3427. speed = reg & DWC3_DSTS_CONNECTSPD;
  3428. dwc->speed = speed;
  3429. if (DWC3_IP_IS(DWC32))
  3430. lanes = DWC3_DSTS_CONNLANES(reg) + 1;
  3431. dwc->gadget->ssp_rate = USB_SSP_GEN_UNKNOWN;
  3432. /*
  3433. * RAMClkSel is reset to 0 after USB reset, so it must be reprogrammed
  3434. * each time on Connect Done.
  3435. *
  3436. * Currently we always use the reset value. If any platform
  3437. * wants to set this to a different value, we need to add a
  3438. * setting and update GCTL.RAMCLKSEL here.
  3439. */
  3440. switch (speed) {
  3441. case DWC3_DSTS_SUPERSPEED_PLUS:
  3442. dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512);
  3443. dwc->gadget->ep0->maxpacket = 512;
  3444. dwc->gadget->speed = USB_SPEED_SUPER_PLUS;
  3445. if (lanes > 1)
  3446. dwc->gadget->ssp_rate = USB_SSP_GEN_2x2;
  3447. else
  3448. dwc->gadget->ssp_rate = USB_SSP_GEN_2x1;
  3449. break;
  3450. case DWC3_DSTS_SUPERSPEED:
  3451. /*
  3452. * WORKAROUND: DWC3 revisions <1.90a have an issue which
  3453. * would cause a missing USB3 Reset event.
  3454. *
  3455. * In such situations, we should force a USB3 Reset
  3456. * event by calling our dwc3_gadget_reset_interrupt()
  3457. * routine.
  3458. *
  3459. * Refers to:
  3460. *
  3461. * STAR#9000483510: RTL: SS : USB3 reset event may
  3462. * not be generated always when the link enters poll
  3463. */
  3464. if (DWC3_VER_IS_PRIOR(DWC3, 190A))
  3465. dwc3_gadget_reset_interrupt(dwc);
  3466. dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512);
  3467. dwc->gadget->ep0->maxpacket = 512;
  3468. dwc->gadget->speed = USB_SPEED_SUPER;
  3469. if (lanes > 1) {
  3470. dwc->gadget->speed = USB_SPEED_SUPER_PLUS;
  3471. dwc->gadget->ssp_rate = USB_SSP_GEN_1x2;
  3472. }
  3473. break;
  3474. case DWC3_DSTS_HIGHSPEED:
  3475. dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(64);
  3476. dwc->gadget->ep0->maxpacket = 64;
  3477. dwc->gadget->speed = USB_SPEED_HIGH;
  3478. break;
  3479. case DWC3_DSTS_FULLSPEED:
  3480. dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(64);
  3481. dwc->gadget->ep0->maxpacket = 64;
  3482. dwc->gadget->speed = USB_SPEED_FULL;
  3483. break;
  3484. }
  3485. dwc->eps[1]->endpoint.maxpacket = dwc->gadget->ep0->maxpacket;
  3486. /* Enable USB2 LPM Capability */
  3487. if (!DWC3_VER_IS_WITHIN(DWC3, ANY, 194A) &&
  3488. !dwc->usb2_gadget_lpm_disable &&
  3489. (speed != DWC3_DSTS_SUPERSPEED) &&
  3490. (speed != DWC3_DSTS_SUPERSPEED_PLUS)) {
  3491. reg = dwc3_readl(dwc->regs, DWC3_DCFG);
  3492. reg |= DWC3_DCFG_LPM_CAP;
  3493. dwc3_writel(dwc->regs, DWC3_DCFG, reg);
  3494. reg = dwc3_readl(dwc->regs, DWC3_DCTL);
  3495. reg &= ~(DWC3_DCTL_HIRD_THRES_MASK | DWC3_DCTL_L1_HIBER_EN);
  3496. reg |= DWC3_DCTL_HIRD_THRES(dwc->hird_threshold |
  3497. (dwc->is_utmi_l1_suspend << 4));
  3498. /*
  3499. * When dwc3 revisions >= 2.40a, LPM Erratum is enabled and
  3500. * DCFG.LPMCap is set, core responses with an ACK and the
  3501. * BESL value in the LPM token is less than or equal to LPM
  3502. * NYET threshold.
  3503. */
  3504. WARN_ONCE(DWC3_VER_IS_PRIOR(DWC3, 240A) && dwc->has_lpm_erratum,
  3505. "LPM Erratum not available on dwc3 revisions < 2.40a\n");
  3506. if (dwc->has_lpm_erratum && !DWC3_VER_IS_PRIOR(DWC3, 240A)) {
  3507. reg &= ~DWC3_DCTL_NYET_THRES_MASK;
  3508. reg |= DWC3_DCTL_NYET_THRES(dwc->lpm_nyet_threshold);
  3509. }
  3510. dwc3_gadget_dctl_write_safe(dwc, reg);
  3511. } else {
  3512. if (dwc->usb2_gadget_lpm_disable) {
  3513. reg = dwc3_readl(dwc->regs, DWC3_DCFG);
  3514. reg &= ~DWC3_DCFG_LPM_CAP;
  3515. dwc3_writel(dwc->regs, DWC3_DCFG, reg);
  3516. }
  3517. reg = dwc3_readl(dwc->regs, DWC3_DCTL);
  3518. reg &= ~DWC3_DCTL_HIRD_THRES_MASK;
  3519. dwc3_gadget_dctl_write_safe(dwc, reg);
  3520. }
  3521. dep = dwc->eps[0];
  3522. ret = __dwc3_gadget_ep_enable(dep, DWC3_DEPCFG_ACTION_MODIFY);
  3523. if (ret) {
  3524. dev_err(dwc->dev, "failed to enable %s\n", dep->name);
  3525. return;
  3526. }
  3527. dep = dwc->eps[1];
  3528. ret = __dwc3_gadget_ep_enable(dep, DWC3_DEPCFG_ACTION_MODIFY);
  3529. if (ret) {
  3530. dev_err(dwc->dev, "failed to enable %s\n", dep->name);
  3531. return;
  3532. }
  3533. /*
  3534. * Configure PHY via GUSB3PIPECTLn if required.
  3535. *
  3536. * Update GTXFIFOSIZn
  3537. *
  3538. * In both cases reset values should be sufficient.
  3539. */
  3540. }
  3541. static void dwc3_gadget_wakeup_interrupt(struct dwc3 *dwc, unsigned int evtinfo)
  3542. {
  3543. dwc->suspended = false;
  3544. /*
  3545. * TODO take core out of low power mode when that's
  3546. * implemented.
  3547. */
  3548. if (dwc->async_callbacks && dwc->gadget_driver->resume) {
  3549. spin_unlock(&dwc->lock);
  3550. dwc->gadget_driver->resume(dwc->gadget);
  3551. spin_lock(&dwc->lock);
  3552. }
  3553. dwc->link_state = evtinfo & DWC3_LINK_STATE_MASK;
  3554. }
  3555. static void dwc3_gadget_linksts_change_interrupt(struct dwc3 *dwc,
  3556. unsigned int evtinfo)
  3557. {
  3558. enum dwc3_link_state next = evtinfo & DWC3_LINK_STATE_MASK;
  3559. unsigned int pwropt;
  3560. /*
  3561. * WORKAROUND: DWC3 < 2.50a have an issue when configured without
  3562. * Hibernation mode enabled which would show up when device detects
  3563. * host-initiated U3 exit.
  3564. *
  3565. * In that case, device will generate a Link State Change Interrupt
  3566. * from U3 to RESUME which is only necessary if Hibernation is
  3567. * configured in.
  3568. *
  3569. * There are no functional changes due to such spurious event and we
  3570. * just need to ignore it.
  3571. *
  3572. * Refers to:
  3573. *
  3574. * STAR#9000570034 RTL: SS Resume event generated in non-Hibernation
  3575. * operational mode
  3576. */
  3577. pwropt = DWC3_GHWPARAMS1_EN_PWROPT(dwc->hwparams.hwparams1);
  3578. if (DWC3_VER_IS_PRIOR(DWC3, 250A) &&
  3579. (pwropt != DWC3_GHWPARAMS1_EN_PWROPT_HIB)) {
  3580. if ((dwc->link_state == DWC3_LINK_STATE_U3) &&
  3581. (next == DWC3_LINK_STATE_RESUME)) {
  3582. return;
  3583. }
  3584. }
  3585. /*
  3586. * WORKAROUND: DWC3 Revisions <1.83a have an issue which, depending
  3587. * on the link partner, the USB session might do multiple entry/exit
  3588. * of low power states before a transfer takes place.
  3589. *
  3590. * Due to this problem, we might experience lower throughput. The
  3591. * suggested workaround is to disable DCTL[12:9] bits if we're
  3592. * transitioning from U1/U2 to U0 and enable those bits again
  3593. * after a transfer completes and there are no pending transfers
  3594. * on any of the enabled endpoints.
  3595. *
  3596. * This is the first half of that workaround.
  3597. *
  3598. * Refers to:
  3599. *
  3600. * STAR#9000446952: RTL: Device SS : if U1/U2 ->U0 takes >128us
  3601. * core send LGO_Ux entering U0
  3602. */
  3603. if (DWC3_VER_IS_PRIOR(DWC3, 183A)) {
  3604. if (next == DWC3_LINK_STATE_U0) {
  3605. u32 u1u2;
  3606. u32 reg;
  3607. switch (dwc->link_state) {
  3608. case DWC3_LINK_STATE_U1:
  3609. case DWC3_LINK_STATE_U2:
  3610. reg = dwc3_readl(dwc->regs, DWC3_DCTL);
  3611. u1u2 = reg & (DWC3_DCTL_INITU2ENA
  3612. | DWC3_DCTL_ACCEPTU2ENA
  3613. | DWC3_DCTL_INITU1ENA
  3614. | DWC3_DCTL_ACCEPTU1ENA);
  3615. if (!dwc->u1u2)
  3616. dwc->u1u2 = reg & u1u2;
  3617. reg &= ~u1u2;
  3618. dwc3_gadget_dctl_write_safe(dwc, reg);
  3619. break;
  3620. default:
  3621. /* do nothing */
  3622. break;
  3623. }
  3624. }
  3625. }
  3626. switch (next) {
  3627. case DWC3_LINK_STATE_U0:
  3628. if (dwc->gadget->wakeup_armed) {
  3629. dwc3_gadget_enable_linksts_evts(dwc, false);
  3630. dwc3_resume_gadget(dwc);
  3631. dwc->suspended = false;
  3632. }
  3633. break;
  3634. case DWC3_LINK_STATE_U1:
  3635. if (dwc->speed == USB_SPEED_SUPER)
  3636. dwc3_suspend_gadget(dwc);
  3637. break;
  3638. case DWC3_LINK_STATE_U2:
  3639. case DWC3_LINK_STATE_U3:
  3640. dwc3_suspend_gadget(dwc);
  3641. break;
  3642. case DWC3_LINK_STATE_RESUME:
  3643. dwc3_resume_gadget(dwc);
  3644. break;
  3645. default:
  3646. /* do nothing */
  3647. break;
  3648. }
  3649. dwc->link_state = next;
  3650. }
  3651. static void dwc3_gadget_suspend_interrupt(struct dwc3 *dwc,
  3652. unsigned int evtinfo)
  3653. {
  3654. enum dwc3_link_state next = evtinfo & DWC3_LINK_STATE_MASK;
  3655. if (!dwc->suspended && next == DWC3_LINK_STATE_U3) {
  3656. dwc->suspended = true;
  3657. dwc3_suspend_gadget(dwc);
  3658. }
  3659. dwc->link_state = next;
  3660. }
  3661. static void dwc3_gadget_interrupt(struct dwc3 *dwc,
  3662. const struct dwc3_event_devt *event)
  3663. {
  3664. switch (event->type) {
  3665. case DWC3_DEVICE_EVENT_DISCONNECT:
  3666. dwc3_gadget_disconnect_interrupt(dwc);
  3667. break;
  3668. case DWC3_DEVICE_EVENT_RESET:
  3669. dwc3_gadget_reset_interrupt(dwc);
  3670. break;
  3671. case DWC3_DEVICE_EVENT_CONNECT_DONE:
  3672. dwc3_gadget_conndone_interrupt(dwc);
  3673. break;
  3674. case DWC3_DEVICE_EVENT_WAKEUP:
  3675. dwc3_gadget_wakeup_interrupt(dwc, event->event_info);
  3676. break;
  3677. case DWC3_DEVICE_EVENT_HIBER_REQ:
  3678. dev_WARN_ONCE(dwc->dev, true, "unexpected hibernation event\n");
  3679. break;
  3680. case DWC3_DEVICE_EVENT_LINK_STATUS_CHANGE:
  3681. dwc3_gadget_linksts_change_interrupt(dwc, event->event_info);
  3682. break;
  3683. case DWC3_DEVICE_EVENT_SUSPEND:
  3684. /* It changed to be suspend event for version 2.30a and above */
  3685. if (!DWC3_VER_IS_PRIOR(DWC3, 230A))
  3686. dwc3_gadget_suspend_interrupt(dwc, event->event_info);
  3687. break;
  3688. case DWC3_DEVICE_EVENT_SOF:
  3689. case DWC3_DEVICE_EVENT_ERRATIC_ERROR:
  3690. case DWC3_DEVICE_EVENT_CMD_CMPL:
  3691. case DWC3_DEVICE_EVENT_OVERFLOW:
  3692. break;
  3693. default:
  3694. dev_WARN(dwc->dev, "UNKNOWN IRQ %d\n", event->type);
  3695. }
  3696. }
  3697. static void dwc3_process_event_entry(struct dwc3 *dwc,
  3698. const union dwc3_event *event)
  3699. {
  3700. trace_dwc3_event(event->raw, dwc);
  3701. if (!event->type.is_devspec)
  3702. dwc3_endpoint_interrupt(dwc, &event->depevt);
  3703. else if (event->type.type == DWC3_EVENT_TYPE_DEV)
  3704. dwc3_gadget_interrupt(dwc, &event->devt);
  3705. else
  3706. dev_err(dwc->dev, "UNKNOWN IRQ type %d\n", event->raw);
  3707. }
  3708. static irqreturn_t dwc3_process_event_buf(struct dwc3_event_buffer *evt)
  3709. {
  3710. struct dwc3 *dwc = evt->dwc;
  3711. irqreturn_t ret = IRQ_NONE;
  3712. int left;
  3713. left = evt->count;
  3714. if (!(evt->flags & DWC3_EVENT_PENDING))
  3715. return IRQ_NONE;
  3716. while (left > 0) {
  3717. union dwc3_event event;
  3718. event.raw = *(u32 *) (evt->cache + evt->lpos);
  3719. dwc3_process_event_entry(dwc, &event);
  3720. /*
  3721. * FIXME we wrap around correctly to the next entry as
  3722. * almost all entries are 4 bytes in size. There is one
  3723. * entry which has 12 bytes which is a regular entry
  3724. * followed by 8 bytes data. ATM I don't know how
  3725. * things are organized if we get next to the a
  3726. * boundary so I worry about that once we try to handle
  3727. * that.
  3728. */
  3729. evt->lpos = (evt->lpos + 4) % evt->length;
  3730. left -= 4;
  3731. }
  3732. evt->count = 0;
  3733. ret = IRQ_HANDLED;
  3734. /* Unmask interrupt */
  3735. dwc3_writel(dwc->regs, DWC3_GEVNTSIZ(0),
  3736. DWC3_GEVNTSIZ_SIZE(evt->length));
  3737. if (dwc->imod_interval) {
  3738. dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(0), DWC3_GEVNTCOUNT_EHB);
  3739. dwc3_writel(dwc->regs, DWC3_DEV_IMOD(0), dwc->imod_interval);
  3740. }
  3741. /* Keep the clearing of DWC3_EVENT_PENDING at the end */
  3742. evt->flags &= ~DWC3_EVENT_PENDING;
  3743. return ret;
  3744. }
  3745. static irqreturn_t dwc3_thread_interrupt(int irq, void *_evt)
  3746. {
  3747. struct dwc3_event_buffer *evt = _evt;
  3748. struct dwc3 *dwc = evt->dwc;
  3749. unsigned long flags;
  3750. irqreturn_t ret = IRQ_NONE;
  3751. local_bh_disable();
  3752. spin_lock_irqsave(&dwc->lock, flags);
  3753. ret = dwc3_process_event_buf(evt);
  3754. spin_unlock_irqrestore(&dwc->lock, flags);
  3755. local_bh_enable();
  3756. return ret;
  3757. }
  3758. static irqreturn_t dwc3_check_event_buf(struct dwc3_event_buffer *evt)
  3759. {
  3760. struct dwc3 *dwc = evt->dwc;
  3761. u32 amount;
  3762. u32 count;
  3763. if (pm_runtime_suspended(dwc->dev)) {
  3764. dwc->pending_events = true;
  3765. /*
  3766. * Trigger runtime resume. The get() function will be balanced
  3767. * after processing the pending events in dwc3_process_pending
  3768. * events().
  3769. */
  3770. pm_runtime_get(dwc->dev);
  3771. disable_irq_nosync(dwc->irq_gadget);
  3772. return IRQ_HANDLED;
  3773. }
  3774. /*
  3775. * With PCIe legacy interrupt, test shows that top-half irq handler can
  3776. * be called again after HW interrupt deassertion. Check if bottom-half
  3777. * irq event handler completes before caching new event to prevent
  3778. * losing events.
  3779. */
  3780. if (evt->flags & DWC3_EVENT_PENDING)
  3781. return IRQ_HANDLED;
  3782. count = dwc3_readl(dwc->regs, DWC3_GEVNTCOUNT(0));
  3783. count &= DWC3_GEVNTCOUNT_MASK;
  3784. if (!count)
  3785. return IRQ_NONE;
  3786. evt->count = count;
  3787. evt->flags |= DWC3_EVENT_PENDING;
  3788. /* Mask interrupt */
  3789. dwc3_writel(dwc->regs, DWC3_GEVNTSIZ(0),
  3790. DWC3_GEVNTSIZ_INTMASK | DWC3_GEVNTSIZ_SIZE(evt->length));
  3791. amount = min(count, evt->length - evt->lpos);
  3792. memcpy(evt->cache + evt->lpos, evt->buf + evt->lpos, amount);
  3793. if (amount < count)
  3794. memcpy(evt->cache, evt->buf, count - amount);
  3795. dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(0), count);
  3796. return IRQ_WAKE_THREAD;
  3797. }
  3798. static irqreturn_t dwc3_interrupt(int irq, void *_evt)
  3799. {
  3800. struct dwc3_event_buffer *evt = _evt;
  3801. return dwc3_check_event_buf(evt);
  3802. }
  3803. static int dwc3_gadget_get_irq(struct dwc3 *dwc)
  3804. {
  3805. struct platform_device *dwc3_pdev = to_platform_device(dwc->dev);
  3806. int irq;
  3807. irq = platform_get_irq_byname_optional(dwc3_pdev, "peripheral");
  3808. if (irq > 0)
  3809. goto out;
  3810. if (irq == -EPROBE_DEFER)
  3811. goto out;
  3812. irq = platform_get_irq_byname_optional(dwc3_pdev, "dwc_usb3");
  3813. if (irq > 0)
  3814. goto out;
  3815. if (irq == -EPROBE_DEFER)
  3816. goto out;
  3817. irq = platform_get_irq(dwc3_pdev, 0);
  3818. out:
  3819. return irq;
  3820. }
  3821. static void dwc_gadget_release(struct device *dev)
  3822. {
  3823. struct usb_gadget *gadget = container_of(dev, struct usb_gadget, dev);
  3824. kfree(gadget);
  3825. }
  3826. /**
  3827. * dwc3_gadget_init - initializes gadget related registers
  3828. * @dwc: pointer to our controller context structure
  3829. *
  3830. * Returns 0 on success otherwise negative errno.
  3831. */
  3832. int dwc3_gadget_init(struct dwc3 *dwc)
  3833. {
  3834. int ret;
  3835. int irq;
  3836. struct device *dev;
  3837. irq = dwc3_gadget_get_irq(dwc);
  3838. if (irq < 0) {
  3839. ret = irq;
  3840. goto err0;
  3841. }
  3842. dwc->irq_gadget = irq;
  3843. dwc->ep0_trb = dma_alloc_coherent(dwc->sysdev,
  3844. sizeof(*dwc->ep0_trb) * 2,
  3845. &dwc->ep0_trb_addr, GFP_KERNEL);
  3846. if (!dwc->ep0_trb) {
  3847. dev_err(dwc->dev, "failed to allocate ep0 trb\n");
  3848. ret = -ENOMEM;
  3849. goto err0;
  3850. }
  3851. dwc->setup_buf = kzalloc(DWC3_EP0_SETUP_SIZE, GFP_KERNEL);
  3852. if (!dwc->setup_buf) {
  3853. ret = -ENOMEM;
  3854. goto err1;
  3855. }
  3856. dwc->bounce = dma_alloc_coherent(dwc->sysdev, DWC3_BOUNCE_SIZE,
  3857. &dwc->bounce_addr, GFP_KERNEL);
  3858. if (!dwc->bounce) {
  3859. ret = -ENOMEM;
  3860. goto err2;
  3861. }
  3862. init_completion(&dwc->ep0_in_setup);
  3863. dwc->gadget = kzalloc(sizeof(struct usb_gadget), GFP_KERNEL);
  3864. if (!dwc->gadget) {
  3865. ret = -ENOMEM;
  3866. goto err3;
  3867. }
  3868. usb_initialize_gadget(dwc->dev, dwc->gadget, dwc_gadget_release);
  3869. dev = &dwc->gadget->dev;
  3870. dev->platform_data = dwc;
  3871. dwc->gadget->ops = &dwc3_gadget_ops;
  3872. dwc->gadget->speed = USB_SPEED_UNKNOWN;
  3873. dwc->gadget->ssp_rate = USB_SSP_GEN_UNKNOWN;
  3874. dwc->gadget->sg_supported = true;
  3875. dwc->gadget->name = "dwc3-gadget";
  3876. dwc->gadget->lpm_capable = !dwc->usb2_gadget_lpm_disable;
  3877. dwc->gadget->wakeup_capable = true;
  3878. /*
  3879. * FIXME We might be setting max_speed to <SUPER, however versions
  3880. * <2.20a of dwc3 have an issue with metastability (documented
  3881. * elsewhere in this driver) which tells us we can't set max speed to
  3882. * anything lower than SUPER.
  3883. *
  3884. * Because gadget.max_speed is only used by composite.c and function
  3885. * drivers (i.e. it won't go into dwc3's registers) we are allowing this
  3886. * to happen so we avoid sending SuperSpeed Capability descriptor
  3887. * together with our BOS descriptor as that could confuse host into
  3888. * thinking we can handle super speed.
  3889. *
  3890. * Note that, in fact, we won't even support GetBOS requests when speed
  3891. * is less than super speed because we don't have means, yet, to tell
  3892. * composite.c that we are USB 2.0 + LPM ECN.
  3893. */
  3894. if (DWC3_VER_IS_PRIOR(DWC3, 220A) &&
  3895. !dwc->dis_metastability_quirk)
  3896. dev_info(dwc->dev, "changing max_speed on rev %08x\n",
  3897. dwc->revision);
  3898. dwc->gadget->max_speed = dwc->maximum_speed;
  3899. dwc->gadget->max_ssp_rate = dwc->max_ssp_rate;
  3900. /*
  3901. * REVISIT: Here we should clear all pending IRQs to be
  3902. * sure we're starting from a well known location.
  3903. */
  3904. ret = dwc3_gadget_init_endpoints(dwc, dwc->num_eps);
  3905. if (ret)
  3906. goto err4;
  3907. ret = usb_add_gadget(dwc->gadget);
  3908. if (ret) {
  3909. dev_err(dwc->dev, "failed to add gadget\n");
  3910. goto err5;
  3911. }
  3912. if (DWC3_IP_IS(DWC32) && dwc->maximum_speed == USB_SPEED_SUPER_PLUS)
  3913. dwc3_gadget_set_ssp_rate(dwc->gadget, dwc->max_ssp_rate);
  3914. else
  3915. dwc3_gadget_set_speed(dwc->gadget, dwc->maximum_speed);
  3916. /* No system wakeup if no gadget driver bound */
  3917. if (dwc->sys_wakeup)
  3918. device_wakeup_disable(dwc->sysdev);
  3919. return 0;
  3920. err5:
  3921. dwc3_gadget_free_endpoints(dwc);
  3922. err4:
  3923. usb_put_gadget(dwc->gadget);
  3924. dwc->gadget = NULL;
  3925. err3:
  3926. dma_free_coherent(dwc->sysdev, DWC3_BOUNCE_SIZE, dwc->bounce,
  3927. dwc->bounce_addr);
  3928. err2:
  3929. kfree(dwc->setup_buf);
  3930. err1:
  3931. dma_free_coherent(dwc->sysdev, sizeof(*dwc->ep0_trb) * 2,
  3932. dwc->ep0_trb, dwc->ep0_trb_addr);
  3933. err0:
  3934. return ret;
  3935. }
  3936. /* -------------------------------------------------------------------------- */
  3937. void dwc3_gadget_exit(struct dwc3 *dwc)
  3938. {
  3939. if (!dwc->gadget)
  3940. return;
  3941. dwc3_enable_susphy(dwc, false);
  3942. usb_del_gadget(dwc->gadget);
  3943. dwc3_gadget_free_endpoints(dwc);
  3944. usb_put_gadget(dwc->gadget);
  3945. dma_free_coherent(dwc->sysdev, DWC3_BOUNCE_SIZE, dwc->bounce,
  3946. dwc->bounce_addr);
  3947. kfree(dwc->setup_buf);
  3948. dma_free_coherent(dwc->sysdev, sizeof(*dwc->ep0_trb) * 2,
  3949. dwc->ep0_trb, dwc->ep0_trb_addr);
  3950. }
  3951. int dwc3_gadget_suspend(struct dwc3 *dwc)
  3952. {
  3953. unsigned long flags;
  3954. int ret;
  3955. ret = dwc3_gadget_soft_disconnect(dwc);
  3956. if (ret)
  3957. goto err;
  3958. spin_lock_irqsave(&dwc->lock, flags);
  3959. if (dwc->gadget_driver)
  3960. dwc3_disconnect_gadget(dwc);
  3961. spin_unlock_irqrestore(&dwc->lock, flags);
  3962. return 0;
  3963. err:
  3964. /*
  3965. * Attempt to reset the controller's state. Likely no
  3966. * communication can be established until the host
  3967. * performs a port reset.
  3968. */
  3969. if (dwc->softconnect)
  3970. dwc3_gadget_soft_connect(dwc);
  3971. return ret;
  3972. }
  3973. int dwc3_gadget_resume(struct dwc3 *dwc)
  3974. {
  3975. if (!dwc->gadget_driver || !dwc->softconnect)
  3976. return 0;
  3977. return dwc3_gadget_soft_connect(dwc);
  3978. }