gadget.c 127 KB

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