gadget.c 127 KB

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