fotg210-hcd.c 157 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /* Faraday FOTG210 EHCI-like driver
  3. *
  4. * Copyright (c) 2013 Faraday Technology Corporation
  5. *
  6. * Author: Yuan-Hsin Chen <[email protected]>
  7. * Feng-Hsin Chiang <[email protected]>
  8. * Po-Yu Chuang <[email protected]>
  9. *
  10. * Most of code borrowed from the Linux-3.7 EHCI driver
  11. */
  12. #include <linux/module.h>
  13. #include <linux/of.h>
  14. #include <linux/device.h>
  15. #include <linux/dmapool.h>
  16. #include <linux/kernel.h>
  17. #include <linux/delay.h>
  18. #include <linux/ioport.h>
  19. #include <linux/sched.h>
  20. #include <linux/vmalloc.h>
  21. #include <linux/errno.h>
  22. #include <linux/init.h>
  23. #include <linux/hrtimer.h>
  24. #include <linux/list.h>
  25. #include <linux/interrupt.h>
  26. #include <linux/usb.h>
  27. #include <linux/usb/hcd.h>
  28. #include <linux/moduleparam.h>
  29. #include <linux/dma-mapping.h>
  30. #include <linux/debugfs.h>
  31. #include <linux/slab.h>
  32. #include <linux/uaccess.h>
  33. #include <linux/platform_device.h>
  34. #include <linux/io.h>
  35. #include <linux/iopoll.h>
  36. #include <linux/clk.h>
  37. #include <asm/byteorder.h>
  38. #include <asm/irq.h>
  39. #include <asm/unaligned.h>
  40. #define DRIVER_AUTHOR "Yuan-Hsin Chen"
  41. #define DRIVER_DESC "FOTG210 Host Controller (EHCI) Driver"
  42. static const char hcd_name[] = "fotg210_hcd";
  43. #undef FOTG210_URB_TRACE
  44. #define FOTG210_STATS
  45. /* magic numbers that can affect system performance */
  46. #define FOTG210_TUNE_CERR 3 /* 0-3 qtd retries; 0 == don't stop */
  47. #define FOTG210_TUNE_RL_HS 4 /* nak throttle; see 4.9 */
  48. #define FOTG210_TUNE_RL_TT 0
  49. #define FOTG210_TUNE_MULT_HS 1 /* 1-3 transactions/uframe; 4.10.3 */
  50. #define FOTG210_TUNE_MULT_TT 1
  51. /* Some drivers think it's safe to schedule isochronous transfers more than 256
  52. * ms into the future (partly as a result of an old bug in the scheduling
  53. * code). In an attempt to avoid trouble, we will use a minimum scheduling
  54. * length of 512 frames instead of 256.
  55. */
  56. #define FOTG210_TUNE_FLS 1 /* (medium) 512-frame schedule */
  57. /* Initial IRQ latency: faster than hw default */
  58. static int log2_irq_thresh; /* 0 to 6 */
  59. module_param(log2_irq_thresh, int, S_IRUGO);
  60. MODULE_PARM_DESC(log2_irq_thresh, "log2 IRQ latency, 1-64 microframes");
  61. /* initial park setting: slower than hw default */
  62. static unsigned park;
  63. module_param(park, uint, S_IRUGO);
  64. MODULE_PARM_DESC(park, "park setting; 1-3 back-to-back async packets");
  65. /* for link power management(LPM) feature */
  66. static unsigned int hird;
  67. module_param(hird, int, S_IRUGO);
  68. MODULE_PARM_DESC(hird, "host initiated resume duration, +1 for each 75us");
  69. #define INTR_MASK (STS_IAA | STS_FATAL | STS_PCD | STS_ERR | STS_INT)
  70. #include "fotg210.h"
  71. #define fotg210_dbg(fotg210, fmt, args...) \
  72. dev_dbg(fotg210_to_hcd(fotg210)->self.controller, fmt, ## args)
  73. #define fotg210_err(fotg210, fmt, args...) \
  74. dev_err(fotg210_to_hcd(fotg210)->self.controller, fmt, ## args)
  75. #define fotg210_info(fotg210, fmt, args...) \
  76. dev_info(fotg210_to_hcd(fotg210)->self.controller, fmt, ## args)
  77. #define fotg210_warn(fotg210, fmt, args...) \
  78. dev_warn(fotg210_to_hcd(fotg210)->self.controller, fmt, ## args)
  79. /* check the values in the HCSPARAMS register (host controller _Structural_
  80. * parameters) see EHCI spec, Table 2-4 for each value
  81. */
  82. static void dbg_hcs_params(struct fotg210_hcd *fotg210, char *label)
  83. {
  84. u32 params = fotg210_readl(fotg210, &fotg210->caps->hcs_params);
  85. fotg210_dbg(fotg210, "%s hcs_params 0x%x ports=%d\n", label, params,
  86. HCS_N_PORTS(params));
  87. }
  88. /* check the values in the HCCPARAMS register (host controller _Capability_
  89. * parameters) see EHCI Spec, Table 2-5 for each value
  90. */
  91. static void dbg_hcc_params(struct fotg210_hcd *fotg210, char *label)
  92. {
  93. u32 params = fotg210_readl(fotg210, &fotg210->caps->hcc_params);
  94. fotg210_dbg(fotg210, "%s hcc_params %04x uframes %s%s\n", label,
  95. params,
  96. HCC_PGM_FRAMELISTLEN(params) ? "256/512/1024" : "1024",
  97. HCC_CANPARK(params) ? " park" : "");
  98. }
  99. static void __maybe_unused
  100. dbg_qtd(const char *label, struct fotg210_hcd *fotg210, struct fotg210_qtd *qtd)
  101. {
  102. fotg210_dbg(fotg210, "%s td %p n%08x %08x t%08x p0=%08x\n", label, qtd,
  103. hc32_to_cpup(fotg210, &qtd->hw_next),
  104. hc32_to_cpup(fotg210, &qtd->hw_alt_next),
  105. hc32_to_cpup(fotg210, &qtd->hw_token),
  106. hc32_to_cpup(fotg210, &qtd->hw_buf[0]));
  107. if (qtd->hw_buf[1])
  108. fotg210_dbg(fotg210, " p1=%08x p2=%08x p3=%08x p4=%08x\n",
  109. hc32_to_cpup(fotg210, &qtd->hw_buf[1]),
  110. hc32_to_cpup(fotg210, &qtd->hw_buf[2]),
  111. hc32_to_cpup(fotg210, &qtd->hw_buf[3]),
  112. hc32_to_cpup(fotg210, &qtd->hw_buf[4]));
  113. }
  114. static void __maybe_unused
  115. dbg_qh(const char *label, struct fotg210_hcd *fotg210, struct fotg210_qh *qh)
  116. {
  117. struct fotg210_qh_hw *hw = qh->hw;
  118. fotg210_dbg(fotg210, "%s qh %p n%08x info %x %x qtd %x\n", label, qh,
  119. hw->hw_next, hw->hw_info1, hw->hw_info2,
  120. hw->hw_current);
  121. dbg_qtd("overlay", fotg210, (struct fotg210_qtd *) &hw->hw_qtd_next);
  122. }
  123. static void __maybe_unused
  124. dbg_itd(const char *label, struct fotg210_hcd *fotg210, struct fotg210_itd *itd)
  125. {
  126. fotg210_dbg(fotg210, "%s[%d] itd %p, next %08x, urb %p\n", label,
  127. itd->frame, itd, hc32_to_cpu(fotg210, itd->hw_next),
  128. itd->urb);
  129. fotg210_dbg(fotg210,
  130. " trans: %08x %08x %08x %08x %08x %08x %08x %08x\n",
  131. hc32_to_cpu(fotg210, itd->hw_transaction[0]),
  132. hc32_to_cpu(fotg210, itd->hw_transaction[1]),
  133. hc32_to_cpu(fotg210, itd->hw_transaction[2]),
  134. hc32_to_cpu(fotg210, itd->hw_transaction[3]),
  135. hc32_to_cpu(fotg210, itd->hw_transaction[4]),
  136. hc32_to_cpu(fotg210, itd->hw_transaction[5]),
  137. hc32_to_cpu(fotg210, itd->hw_transaction[6]),
  138. hc32_to_cpu(fotg210, itd->hw_transaction[7]));
  139. fotg210_dbg(fotg210,
  140. " buf: %08x %08x %08x %08x %08x %08x %08x\n",
  141. hc32_to_cpu(fotg210, itd->hw_bufp[0]),
  142. hc32_to_cpu(fotg210, itd->hw_bufp[1]),
  143. hc32_to_cpu(fotg210, itd->hw_bufp[2]),
  144. hc32_to_cpu(fotg210, itd->hw_bufp[3]),
  145. hc32_to_cpu(fotg210, itd->hw_bufp[4]),
  146. hc32_to_cpu(fotg210, itd->hw_bufp[5]),
  147. hc32_to_cpu(fotg210, itd->hw_bufp[6]));
  148. fotg210_dbg(fotg210, " index: %d %d %d %d %d %d %d %d\n",
  149. itd->index[0], itd->index[1], itd->index[2],
  150. itd->index[3], itd->index[4], itd->index[5],
  151. itd->index[6], itd->index[7]);
  152. }
  153. static int __maybe_unused
  154. dbg_status_buf(char *buf, unsigned len, const char *label, u32 status)
  155. {
  156. return scnprintf(buf, len, "%s%sstatus %04x%s%s%s%s%s%s%s%s%s%s",
  157. label, label[0] ? " " : "", status,
  158. (status & STS_ASS) ? " Async" : "",
  159. (status & STS_PSS) ? " Periodic" : "",
  160. (status & STS_RECL) ? " Recl" : "",
  161. (status & STS_HALT) ? " Halt" : "",
  162. (status & STS_IAA) ? " IAA" : "",
  163. (status & STS_FATAL) ? " FATAL" : "",
  164. (status & STS_FLR) ? " FLR" : "",
  165. (status & STS_PCD) ? " PCD" : "",
  166. (status & STS_ERR) ? " ERR" : "",
  167. (status & STS_INT) ? " INT" : "");
  168. }
  169. static int __maybe_unused
  170. dbg_intr_buf(char *buf, unsigned len, const char *label, u32 enable)
  171. {
  172. return scnprintf(buf, len, "%s%sintrenable %02x%s%s%s%s%s%s",
  173. label, label[0] ? " " : "", enable,
  174. (enable & STS_IAA) ? " IAA" : "",
  175. (enable & STS_FATAL) ? " FATAL" : "",
  176. (enable & STS_FLR) ? " FLR" : "",
  177. (enable & STS_PCD) ? " PCD" : "",
  178. (enable & STS_ERR) ? " ERR" : "",
  179. (enable & STS_INT) ? " INT" : "");
  180. }
  181. static const char *const fls_strings[] = { "1024", "512", "256", "??" };
  182. static int dbg_command_buf(char *buf, unsigned len, const char *label,
  183. u32 command)
  184. {
  185. return scnprintf(buf, len,
  186. "%s%scommand %07x %s=%d ithresh=%d%s%s%s period=%s%s %s",
  187. label, label[0] ? " " : "", command,
  188. (command & CMD_PARK) ? " park" : "(park)",
  189. CMD_PARK_CNT(command),
  190. (command >> 16) & 0x3f,
  191. (command & CMD_IAAD) ? " IAAD" : "",
  192. (command & CMD_ASE) ? " Async" : "",
  193. (command & CMD_PSE) ? " Periodic" : "",
  194. fls_strings[(command >> 2) & 0x3],
  195. (command & CMD_RESET) ? " Reset" : "",
  196. (command & CMD_RUN) ? "RUN" : "HALT");
  197. }
  198. static char *dbg_port_buf(char *buf, unsigned len, const char *label, int port,
  199. u32 status)
  200. {
  201. char *sig;
  202. /* signaling state */
  203. switch (status & (3 << 10)) {
  204. case 0 << 10:
  205. sig = "se0";
  206. break;
  207. case 1 << 10:
  208. sig = "k";
  209. break; /* low speed */
  210. case 2 << 10:
  211. sig = "j";
  212. break;
  213. default:
  214. sig = "?";
  215. break;
  216. }
  217. scnprintf(buf, len, "%s%sport:%d status %06x %d sig=%s%s%s%s%s%s%s%s",
  218. label, label[0] ? " " : "", port, status,
  219. status >> 25, /*device address */
  220. sig,
  221. (status & PORT_RESET) ? " RESET" : "",
  222. (status & PORT_SUSPEND) ? " SUSPEND" : "",
  223. (status & PORT_RESUME) ? " RESUME" : "",
  224. (status & PORT_PEC) ? " PEC" : "",
  225. (status & PORT_PE) ? " PE" : "",
  226. (status & PORT_CSC) ? " CSC" : "",
  227. (status & PORT_CONNECT) ? " CONNECT" : "");
  228. return buf;
  229. }
  230. /* functions have the "wrong" filename when they're output... */
  231. #define dbg_status(fotg210, label, status) { \
  232. char _buf[80]; \
  233. dbg_status_buf(_buf, sizeof(_buf), label, status); \
  234. fotg210_dbg(fotg210, "%s\n", _buf); \
  235. }
  236. #define dbg_cmd(fotg210, label, command) { \
  237. char _buf[80]; \
  238. dbg_command_buf(_buf, sizeof(_buf), label, command); \
  239. fotg210_dbg(fotg210, "%s\n", _buf); \
  240. }
  241. #define dbg_port(fotg210, label, port, status) { \
  242. char _buf[80]; \
  243. fotg210_dbg(fotg210, "%s\n", \
  244. dbg_port_buf(_buf, sizeof(_buf), label, port, status));\
  245. }
  246. /* troubleshooting help: expose state in debugfs */
  247. static int debug_async_open(struct inode *, struct file *);
  248. static int debug_periodic_open(struct inode *, struct file *);
  249. static int debug_registers_open(struct inode *, struct file *);
  250. static int debug_async_open(struct inode *, struct file *);
  251. static ssize_t debug_output(struct file*, char __user*, size_t, loff_t*);
  252. static int debug_close(struct inode *, struct file *);
  253. static const struct file_operations debug_async_fops = {
  254. .owner = THIS_MODULE,
  255. .open = debug_async_open,
  256. .read = debug_output,
  257. .release = debug_close,
  258. .llseek = default_llseek,
  259. };
  260. static const struct file_operations debug_periodic_fops = {
  261. .owner = THIS_MODULE,
  262. .open = debug_periodic_open,
  263. .read = debug_output,
  264. .release = debug_close,
  265. .llseek = default_llseek,
  266. };
  267. static const struct file_operations debug_registers_fops = {
  268. .owner = THIS_MODULE,
  269. .open = debug_registers_open,
  270. .read = debug_output,
  271. .release = debug_close,
  272. .llseek = default_llseek,
  273. };
  274. static struct dentry *fotg210_debug_root;
  275. struct debug_buffer {
  276. ssize_t (*fill_func)(struct debug_buffer *); /* fill method */
  277. struct usb_bus *bus;
  278. struct mutex mutex; /* protect filling of buffer */
  279. size_t count; /* number of characters filled into buffer */
  280. char *output_buf;
  281. size_t alloc_size;
  282. };
  283. static inline char speed_char(u32 scratch)
  284. {
  285. switch (scratch & (3 << 12)) {
  286. case QH_FULL_SPEED:
  287. return 'f';
  288. case QH_LOW_SPEED:
  289. return 'l';
  290. case QH_HIGH_SPEED:
  291. return 'h';
  292. default:
  293. return '?';
  294. }
  295. }
  296. static inline char token_mark(struct fotg210_hcd *fotg210, __hc32 token)
  297. {
  298. __u32 v = hc32_to_cpu(fotg210, token);
  299. if (v & QTD_STS_ACTIVE)
  300. return '*';
  301. if (v & QTD_STS_HALT)
  302. return '-';
  303. if (!IS_SHORT_READ(v))
  304. return ' ';
  305. /* tries to advance through hw_alt_next */
  306. return '/';
  307. }
  308. static void qh_lines(struct fotg210_hcd *fotg210, struct fotg210_qh *qh,
  309. char **nextp, unsigned *sizep)
  310. {
  311. u32 scratch;
  312. u32 hw_curr;
  313. struct fotg210_qtd *td;
  314. unsigned temp;
  315. unsigned size = *sizep;
  316. char *next = *nextp;
  317. char mark;
  318. __le32 list_end = FOTG210_LIST_END(fotg210);
  319. struct fotg210_qh_hw *hw = qh->hw;
  320. if (hw->hw_qtd_next == list_end) /* NEC does this */
  321. mark = '@';
  322. else
  323. mark = token_mark(fotg210, hw->hw_token);
  324. if (mark == '/') { /* qh_alt_next controls qh advance? */
  325. if ((hw->hw_alt_next & QTD_MASK(fotg210)) ==
  326. fotg210->async->hw->hw_alt_next)
  327. mark = '#'; /* blocked */
  328. else if (hw->hw_alt_next == list_end)
  329. mark = '.'; /* use hw_qtd_next */
  330. /* else alt_next points to some other qtd */
  331. }
  332. scratch = hc32_to_cpup(fotg210, &hw->hw_info1);
  333. hw_curr = (mark == '*') ? hc32_to_cpup(fotg210, &hw->hw_current) : 0;
  334. temp = scnprintf(next, size,
  335. "qh/%p dev%d %cs ep%d %08x %08x(%08x%c %s nak%d)",
  336. qh, scratch & 0x007f,
  337. speed_char(scratch),
  338. (scratch >> 8) & 0x000f,
  339. scratch, hc32_to_cpup(fotg210, &hw->hw_info2),
  340. hc32_to_cpup(fotg210, &hw->hw_token), mark,
  341. (cpu_to_hc32(fotg210, QTD_TOGGLE) & hw->hw_token)
  342. ? "data1" : "data0",
  343. (hc32_to_cpup(fotg210, &hw->hw_alt_next) >> 1) & 0x0f);
  344. size -= temp;
  345. next += temp;
  346. /* hc may be modifying the list as we read it ... */
  347. list_for_each_entry(td, &qh->qtd_list, qtd_list) {
  348. scratch = hc32_to_cpup(fotg210, &td->hw_token);
  349. mark = ' ';
  350. if (hw_curr == td->qtd_dma)
  351. mark = '*';
  352. else if (hw->hw_qtd_next == cpu_to_hc32(fotg210, td->qtd_dma))
  353. mark = '+';
  354. else if (QTD_LENGTH(scratch)) {
  355. if (td->hw_alt_next == fotg210->async->hw->hw_alt_next)
  356. mark = '#';
  357. else if (td->hw_alt_next != list_end)
  358. mark = '/';
  359. }
  360. temp = snprintf(next, size,
  361. "\n\t%p%c%s len=%d %08x urb %p",
  362. td, mark, ({ char *tmp;
  363. switch ((scratch>>8)&0x03) {
  364. case 0:
  365. tmp = "out";
  366. break;
  367. case 1:
  368. tmp = "in";
  369. break;
  370. case 2:
  371. tmp = "setup";
  372. break;
  373. default:
  374. tmp = "?";
  375. break;
  376. } tmp; }),
  377. (scratch >> 16) & 0x7fff,
  378. scratch,
  379. td->urb);
  380. if (size < temp)
  381. temp = size;
  382. size -= temp;
  383. next += temp;
  384. if (temp == size)
  385. goto done;
  386. }
  387. temp = snprintf(next, size, "\n");
  388. if (size < temp)
  389. temp = size;
  390. size -= temp;
  391. next += temp;
  392. done:
  393. *sizep = size;
  394. *nextp = next;
  395. }
  396. static ssize_t fill_async_buffer(struct debug_buffer *buf)
  397. {
  398. struct usb_hcd *hcd;
  399. struct fotg210_hcd *fotg210;
  400. unsigned long flags;
  401. unsigned temp, size;
  402. char *next;
  403. struct fotg210_qh *qh;
  404. hcd = bus_to_hcd(buf->bus);
  405. fotg210 = hcd_to_fotg210(hcd);
  406. next = buf->output_buf;
  407. size = buf->alloc_size;
  408. *next = 0;
  409. /* dumps a snapshot of the async schedule.
  410. * usually empty except for long-term bulk reads, or head.
  411. * one QH per line, and TDs we know about
  412. */
  413. spin_lock_irqsave(&fotg210->lock, flags);
  414. for (qh = fotg210->async->qh_next.qh; size > 0 && qh;
  415. qh = qh->qh_next.qh)
  416. qh_lines(fotg210, qh, &next, &size);
  417. if (fotg210->async_unlink && size > 0) {
  418. temp = scnprintf(next, size, "\nunlink =\n");
  419. size -= temp;
  420. next += temp;
  421. for (qh = fotg210->async_unlink; size > 0 && qh;
  422. qh = qh->unlink_next)
  423. qh_lines(fotg210, qh, &next, &size);
  424. }
  425. spin_unlock_irqrestore(&fotg210->lock, flags);
  426. return strlen(buf->output_buf);
  427. }
  428. /* count tds, get ep direction */
  429. static unsigned output_buf_tds_dir(char *buf, struct fotg210_hcd *fotg210,
  430. struct fotg210_qh_hw *hw, struct fotg210_qh *qh, unsigned size)
  431. {
  432. u32 scratch = hc32_to_cpup(fotg210, &hw->hw_info1);
  433. struct fotg210_qtd *qtd;
  434. char *type = "";
  435. unsigned temp = 0;
  436. /* count tds, get ep direction */
  437. list_for_each_entry(qtd, &qh->qtd_list, qtd_list) {
  438. temp++;
  439. switch ((hc32_to_cpu(fotg210, qtd->hw_token) >> 8) & 0x03) {
  440. case 0:
  441. type = "out";
  442. continue;
  443. case 1:
  444. type = "in";
  445. continue;
  446. }
  447. }
  448. return scnprintf(buf, size, "(%c%d ep%d%s [%d/%d] q%d p%d)",
  449. speed_char(scratch), scratch & 0x007f,
  450. (scratch >> 8) & 0x000f, type, qh->usecs,
  451. qh->c_usecs, temp, (scratch >> 16) & 0x7ff);
  452. }
  453. #define DBG_SCHED_LIMIT 64
  454. static ssize_t fill_periodic_buffer(struct debug_buffer *buf)
  455. {
  456. struct usb_hcd *hcd;
  457. struct fotg210_hcd *fotg210;
  458. unsigned long flags;
  459. union fotg210_shadow p, *seen;
  460. unsigned temp, size, seen_count;
  461. char *next;
  462. unsigned i;
  463. __hc32 tag;
  464. seen = kmalloc_array(DBG_SCHED_LIMIT, sizeof(*seen), GFP_ATOMIC);
  465. if (!seen)
  466. return 0;
  467. seen_count = 0;
  468. hcd = bus_to_hcd(buf->bus);
  469. fotg210 = hcd_to_fotg210(hcd);
  470. next = buf->output_buf;
  471. size = buf->alloc_size;
  472. temp = scnprintf(next, size, "size = %d\n", fotg210->periodic_size);
  473. size -= temp;
  474. next += temp;
  475. /* dump a snapshot of the periodic schedule.
  476. * iso changes, interrupt usually doesn't.
  477. */
  478. spin_lock_irqsave(&fotg210->lock, flags);
  479. for (i = 0; i < fotg210->periodic_size; i++) {
  480. p = fotg210->pshadow[i];
  481. if (likely(!p.ptr))
  482. continue;
  483. tag = Q_NEXT_TYPE(fotg210, fotg210->periodic[i]);
  484. temp = scnprintf(next, size, "%4d: ", i);
  485. size -= temp;
  486. next += temp;
  487. do {
  488. struct fotg210_qh_hw *hw;
  489. switch (hc32_to_cpu(fotg210, tag)) {
  490. case Q_TYPE_QH:
  491. hw = p.qh->hw;
  492. temp = scnprintf(next, size, " qh%d-%04x/%p",
  493. p.qh->period,
  494. hc32_to_cpup(fotg210,
  495. &hw->hw_info2)
  496. /* uframe masks */
  497. & (QH_CMASK | QH_SMASK),
  498. p.qh);
  499. size -= temp;
  500. next += temp;
  501. /* don't repeat what follows this qh */
  502. for (temp = 0; temp < seen_count; temp++) {
  503. if (seen[temp].ptr != p.ptr)
  504. continue;
  505. if (p.qh->qh_next.ptr) {
  506. temp = scnprintf(next, size,
  507. " ...");
  508. size -= temp;
  509. next += temp;
  510. }
  511. break;
  512. }
  513. /* show more info the first time around */
  514. if (temp == seen_count) {
  515. temp = output_buf_tds_dir(next,
  516. fotg210, hw,
  517. p.qh, size);
  518. if (seen_count < DBG_SCHED_LIMIT)
  519. seen[seen_count++].qh = p.qh;
  520. } else
  521. temp = 0;
  522. tag = Q_NEXT_TYPE(fotg210, hw->hw_next);
  523. p = p.qh->qh_next;
  524. break;
  525. case Q_TYPE_FSTN:
  526. temp = scnprintf(next, size,
  527. " fstn-%8x/%p",
  528. p.fstn->hw_prev, p.fstn);
  529. tag = Q_NEXT_TYPE(fotg210, p.fstn->hw_next);
  530. p = p.fstn->fstn_next;
  531. break;
  532. case Q_TYPE_ITD:
  533. temp = scnprintf(next, size,
  534. " itd/%p", p.itd);
  535. tag = Q_NEXT_TYPE(fotg210, p.itd->hw_next);
  536. p = p.itd->itd_next;
  537. break;
  538. }
  539. size -= temp;
  540. next += temp;
  541. } while (p.ptr);
  542. temp = scnprintf(next, size, "\n");
  543. size -= temp;
  544. next += temp;
  545. }
  546. spin_unlock_irqrestore(&fotg210->lock, flags);
  547. kfree(seen);
  548. return buf->alloc_size - size;
  549. }
  550. #undef DBG_SCHED_LIMIT
  551. static const char *rh_state_string(struct fotg210_hcd *fotg210)
  552. {
  553. switch (fotg210->rh_state) {
  554. case FOTG210_RH_HALTED:
  555. return "halted";
  556. case FOTG210_RH_SUSPENDED:
  557. return "suspended";
  558. case FOTG210_RH_RUNNING:
  559. return "running";
  560. case FOTG210_RH_STOPPING:
  561. return "stopping";
  562. }
  563. return "?";
  564. }
  565. static ssize_t fill_registers_buffer(struct debug_buffer *buf)
  566. {
  567. struct usb_hcd *hcd;
  568. struct fotg210_hcd *fotg210;
  569. unsigned long flags;
  570. unsigned temp, size, i;
  571. char *next, scratch[80];
  572. static const char fmt[] = "%*s\n";
  573. static const char label[] = "";
  574. hcd = bus_to_hcd(buf->bus);
  575. fotg210 = hcd_to_fotg210(hcd);
  576. next = buf->output_buf;
  577. size = buf->alloc_size;
  578. spin_lock_irqsave(&fotg210->lock, flags);
  579. if (!HCD_HW_ACCESSIBLE(hcd)) {
  580. size = scnprintf(next, size,
  581. "bus %s, device %s\n"
  582. "%s\n"
  583. "SUSPENDED(no register access)\n",
  584. hcd->self.controller->bus->name,
  585. dev_name(hcd->self.controller),
  586. hcd->product_desc);
  587. goto done;
  588. }
  589. /* Capability Registers */
  590. i = HC_VERSION(fotg210, fotg210_readl(fotg210,
  591. &fotg210->caps->hc_capbase));
  592. temp = scnprintf(next, size,
  593. "bus %s, device %s\n"
  594. "%s\n"
  595. "EHCI %x.%02x, rh state %s\n",
  596. hcd->self.controller->bus->name,
  597. dev_name(hcd->self.controller),
  598. hcd->product_desc,
  599. i >> 8, i & 0x0ff, rh_state_string(fotg210));
  600. size -= temp;
  601. next += temp;
  602. /* FIXME interpret both types of params */
  603. i = fotg210_readl(fotg210, &fotg210->caps->hcs_params);
  604. temp = scnprintf(next, size, "structural params 0x%08x\n", i);
  605. size -= temp;
  606. next += temp;
  607. i = fotg210_readl(fotg210, &fotg210->caps->hcc_params);
  608. temp = scnprintf(next, size, "capability params 0x%08x\n", i);
  609. size -= temp;
  610. next += temp;
  611. /* Operational Registers */
  612. temp = dbg_status_buf(scratch, sizeof(scratch), label,
  613. fotg210_readl(fotg210, &fotg210->regs->status));
  614. temp = scnprintf(next, size, fmt, temp, scratch);
  615. size -= temp;
  616. next += temp;
  617. temp = dbg_command_buf(scratch, sizeof(scratch), label,
  618. fotg210_readl(fotg210, &fotg210->regs->command));
  619. temp = scnprintf(next, size, fmt, temp, scratch);
  620. size -= temp;
  621. next += temp;
  622. temp = dbg_intr_buf(scratch, sizeof(scratch), label,
  623. fotg210_readl(fotg210, &fotg210->regs->intr_enable));
  624. temp = scnprintf(next, size, fmt, temp, scratch);
  625. size -= temp;
  626. next += temp;
  627. temp = scnprintf(next, size, "uframe %04x\n",
  628. fotg210_read_frame_index(fotg210));
  629. size -= temp;
  630. next += temp;
  631. if (fotg210->async_unlink) {
  632. temp = scnprintf(next, size, "async unlink qh %p\n",
  633. fotg210->async_unlink);
  634. size -= temp;
  635. next += temp;
  636. }
  637. #ifdef FOTG210_STATS
  638. temp = scnprintf(next, size,
  639. "irq normal %ld err %ld iaa %ld(lost %ld)\n",
  640. fotg210->stats.normal, fotg210->stats.error,
  641. fotg210->stats.iaa, fotg210->stats.lost_iaa);
  642. size -= temp;
  643. next += temp;
  644. temp = scnprintf(next, size, "complete %ld unlink %ld\n",
  645. fotg210->stats.complete, fotg210->stats.unlink);
  646. size -= temp;
  647. next += temp;
  648. #endif
  649. done:
  650. spin_unlock_irqrestore(&fotg210->lock, flags);
  651. return buf->alloc_size - size;
  652. }
  653. static struct debug_buffer
  654. *alloc_buffer(struct usb_bus *bus, ssize_t (*fill_func)(struct debug_buffer *))
  655. {
  656. struct debug_buffer *buf;
  657. buf = kzalloc(sizeof(struct debug_buffer), GFP_KERNEL);
  658. if (buf) {
  659. buf->bus = bus;
  660. buf->fill_func = fill_func;
  661. mutex_init(&buf->mutex);
  662. buf->alloc_size = PAGE_SIZE;
  663. }
  664. return buf;
  665. }
  666. static int fill_buffer(struct debug_buffer *buf)
  667. {
  668. int ret = 0;
  669. if (!buf->output_buf)
  670. buf->output_buf = vmalloc(buf->alloc_size);
  671. if (!buf->output_buf) {
  672. ret = -ENOMEM;
  673. goto out;
  674. }
  675. ret = buf->fill_func(buf);
  676. if (ret >= 0) {
  677. buf->count = ret;
  678. ret = 0;
  679. }
  680. out:
  681. return ret;
  682. }
  683. static ssize_t debug_output(struct file *file, char __user *user_buf,
  684. size_t len, loff_t *offset)
  685. {
  686. struct debug_buffer *buf = file->private_data;
  687. int ret = 0;
  688. mutex_lock(&buf->mutex);
  689. if (buf->count == 0) {
  690. ret = fill_buffer(buf);
  691. if (ret != 0) {
  692. mutex_unlock(&buf->mutex);
  693. goto out;
  694. }
  695. }
  696. mutex_unlock(&buf->mutex);
  697. ret = simple_read_from_buffer(user_buf, len, offset,
  698. buf->output_buf, buf->count);
  699. out:
  700. return ret;
  701. }
  702. static int debug_close(struct inode *inode, struct file *file)
  703. {
  704. struct debug_buffer *buf = file->private_data;
  705. if (buf) {
  706. vfree(buf->output_buf);
  707. kfree(buf);
  708. }
  709. return 0;
  710. }
  711. static int debug_async_open(struct inode *inode, struct file *file)
  712. {
  713. file->private_data = alloc_buffer(inode->i_private, fill_async_buffer);
  714. return file->private_data ? 0 : -ENOMEM;
  715. }
  716. static int debug_periodic_open(struct inode *inode, struct file *file)
  717. {
  718. struct debug_buffer *buf;
  719. buf = alloc_buffer(inode->i_private, fill_periodic_buffer);
  720. if (!buf)
  721. return -ENOMEM;
  722. buf->alloc_size = (sizeof(void *) == 4 ? 6 : 8)*PAGE_SIZE;
  723. file->private_data = buf;
  724. return 0;
  725. }
  726. static int debug_registers_open(struct inode *inode, struct file *file)
  727. {
  728. file->private_data = alloc_buffer(inode->i_private,
  729. fill_registers_buffer);
  730. return file->private_data ? 0 : -ENOMEM;
  731. }
  732. static inline void create_debug_files(struct fotg210_hcd *fotg210)
  733. {
  734. struct usb_bus *bus = &fotg210_to_hcd(fotg210)->self;
  735. struct dentry *root;
  736. root = debugfs_create_dir(bus->bus_name, fotg210_debug_root);
  737. debugfs_create_file("async", S_IRUGO, root, bus, &debug_async_fops);
  738. debugfs_create_file("periodic", S_IRUGO, root, bus,
  739. &debug_periodic_fops);
  740. debugfs_create_file("registers", S_IRUGO, root, bus,
  741. &debug_registers_fops);
  742. }
  743. static inline void remove_debug_files(struct fotg210_hcd *fotg210)
  744. {
  745. struct usb_bus *bus = &fotg210_to_hcd(fotg210)->self;
  746. debugfs_lookup_and_remove(bus->bus_name, fotg210_debug_root);
  747. }
  748. /* handshake - spin reading hc until handshake completes or fails
  749. * @ptr: address of hc register to be read
  750. * @mask: bits to look at in result of read
  751. * @done: value of those bits when handshake succeeds
  752. * @usec: timeout in microseconds
  753. *
  754. * Returns negative errno, or zero on success
  755. *
  756. * Success happens when the "mask" bits have the specified value (hardware
  757. * handshake done). There are two failure modes: "usec" have passed (major
  758. * hardware flakeout), or the register reads as all-ones (hardware removed).
  759. *
  760. * That last failure should_only happen in cases like physical cardbus eject
  761. * before driver shutdown. But it also seems to be caused by bugs in cardbus
  762. * bridge shutdown: shutting down the bridge before the devices using it.
  763. */
  764. static int handshake(struct fotg210_hcd *fotg210, void __iomem *ptr,
  765. u32 mask, u32 done, int usec)
  766. {
  767. u32 result;
  768. int ret;
  769. ret = readl_poll_timeout_atomic(ptr, result,
  770. ((result & mask) == done ||
  771. result == U32_MAX), 1, usec);
  772. if (result == U32_MAX) /* card removed */
  773. return -ENODEV;
  774. return ret;
  775. }
  776. /* Force HC to halt state from unknown (EHCI spec section 2.3).
  777. * Must be called with interrupts enabled and the lock not held.
  778. */
  779. static int fotg210_halt(struct fotg210_hcd *fotg210)
  780. {
  781. u32 temp;
  782. spin_lock_irq(&fotg210->lock);
  783. /* disable any irqs left enabled by previous code */
  784. fotg210_writel(fotg210, 0, &fotg210->regs->intr_enable);
  785. /*
  786. * This routine gets called during probe before fotg210->command
  787. * has been initialized, so we can't rely on its value.
  788. */
  789. fotg210->command &= ~CMD_RUN;
  790. temp = fotg210_readl(fotg210, &fotg210->regs->command);
  791. temp &= ~(CMD_RUN | CMD_IAAD);
  792. fotg210_writel(fotg210, temp, &fotg210->regs->command);
  793. spin_unlock_irq(&fotg210->lock);
  794. synchronize_irq(fotg210_to_hcd(fotg210)->irq);
  795. return handshake(fotg210, &fotg210->regs->status,
  796. STS_HALT, STS_HALT, 16 * 125);
  797. }
  798. /* Reset a non-running (STS_HALT == 1) controller.
  799. * Must be called with interrupts enabled and the lock not held.
  800. */
  801. static int fotg210_reset(struct fotg210_hcd *fotg210)
  802. {
  803. int retval;
  804. u32 command = fotg210_readl(fotg210, &fotg210->regs->command);
  805. /* If the EHCI debug controller is active, special care must be
  806. * taken before and after a host controller reset
  807. */
  808. if (fotg210->debug && !dbgp_reset_prep(fotg210_to_hcd(fotg210)))
  809. fotg210->debug = NULL;
  810. command |= CMD_RESET;
  811. dbg_cmd(fotg210, "reset", command);
  812. fotg210_writel(fotg210, command, &fotg210->regs->command);
  813. fotg210->rh_state = FOTG210_RH_HALTED;
  814. fotg210->next_statechange = jiffies;
  815. retval = handshake(fotg210, &fotg210->regs->command,
  816. CMD_RESET, 0, 250 * 1000);
  817. if (retval)
  818. return retval;
  819. if (fotg210->debug)
  820. dbgp_external_startup(fotg210_to_hcd(fotg210));
  821. fotg210->port_c_suspend = fotg210->suspended_ports =
  822. fotg210->resuming_ports = 0;
  823. return retval;
  824. }
  825. /* Idle the controller (turn off the schedules).
  826. * Must be called with interrupts enabled and the lock not held.
  827. */
  828. static void fotg210_quiesce(struct fotg210_hcd *fotg210)
  829. {
  830. u32 temp;
  831. if (fotg210->rh_state != FOTG210_RH_RUNNING)
  832. return;
  833. /* wait for any schedule enables/disables to take effect */
  834. temp = (fotg210->command << 10) & (STS_ASS | STS_PSS);
  835. handshake(fotg210, &fotg210->regs->status, STS_ASS | STS_PSS, temp,
  836. 16 * 125);
  837. /* then disable anything that's still active */
  838. spin_lock_irq(&fotg210->lock);
  839. fotg210->command &= ~(CMD_ASE | CMD_PSE);
  840. fotg210_writel(fotg210, fotg210->command, &fotg210->regs->command);
  841. spin_unlock_irq(&fotg210->lock);
  842. /* hardware can take 16 microframes to turn off ... */
  843. handshake(fotg210, &fotg210->regs->status, STS_ASS | STS_PSS, 0,
  844. 16 * 125);
  845. }
  846. static void end_unlink_async(struct fotg210_hcd *fotg210);
  847. static void unlink_empty_async(struct fotg210_hcd *fotg210);
  848. static void fotg210_work(struct fotg210_hcd *fotg210);
  849. static void start_unlink_intr(struct fotg210_hcd *fotg210,
  850. struct fotg210_qh *qh);
  851. static void end_unlink_intr(struct fotg210_hcd *fotg210, struct fotg210_qh *qh);
  852. /* Set a bit in the USBCMD register */
  853. static void fotg210_set_command_bit(struct fotg210_hcd *fotg210, u32 bit)
  854. {
  855. fotg210->command |= bit;
  856. fotg210_writel(fotg210, fotg210->command, &fotg210->regs->command);
  857. /* unblock posted write */
  858. fotg210_readl(fotg210, &fotg210->regs->command);
  859. }
  860. /* Clear a bit in the USBCMD register */
  861. static void fotg210_clear_command_bit(struct fotg210_hcd *fotg210, u32 bit)
  862. {
  863. fotg210->command &= ~bit;
  864. fotg210_writel(fotg210, fotg210->command, &fotg210->regs->command);
  865. /* unblock posted write */
  866. fotg210_readl(fotg210, &fotg210->regs->command);
  867. }
  868. /* EHCI timer support... Now using hrtimers.
  869. *
  870. * Lots of different events are triggered from fotg210->hrtimer. Whenever
  871. * the timer routine runs, it checks each possible event; events that are
  872. * currently enabled and whose expiration time has passed get handled.
  873. * The set of enabled events is stored as a collection of bitflags in
  874. * fotg210->enabled_hrtimer_events, and they are numbered in order of
  875. * increasing delay values (ranging between 1 ms and 100 ms).
  876. *
  877. * Rather than implementing a sorted list or tree of all pending events,
  878. * we keep track only of the lowest-numbered pending event, in
  879. * fotg210->next_hrtimer_event. Whenever fotg210->hrtimer gets restarted, its
  880. * expiration time is set to the timeout value for this event.
  881. *
  882. * As a result, events might not get handled right away; the actual delay
  883. * could be anywhere up to twice the requested delay. This doesn't
  884. * matter, because none of the events are especially time-critical. The
  885. * ones that matter most all have a delay of 1 ms, so they will be
  886. * handled after 2 ms at most, which is okay. In addition to this, we
  887. * allow for an expiration range of 1 ms.
  888. */
  889. /* Delay lengths for the hrtimer event types.
  890. * Keep this list sorted by delay length, in the same order as
  891. * the event types indexed by enum fotg210_hrtimer_event in fotg210.h.
  892. */
  893. static unsigned event_delays_ns[] = {
  894. 1 * NSEC_PER_MSEC, /* FOTG210_HRTIMER_POLL_ASS */
  895. 1 * NSEC_PER_MSEC, /* FOTG210_HRTIMER_POLL_PSS */
  896. 1 * NSEC_PER_MSEC, /* FOTG210_HRTIMER_POLL_DEAD */
  897. 1125 * NSEC_PER_USEC, /* FOTG210_HRTIMER_UNLINK_INTR */
  898. 2 * NSEC_PER_MSEC, /* FOTG210_HRTIMER_FREE_ITDS */
  899. 6 * NSEC_PER_MSEC, /* FOTG210_HRTIMER_ASYNC_UNLINKS */
  900. 10 * NSEC_PER_MSEC, /* FOTG210_HRTIMER_IAA_WATCHDOG */
  901. 10 * NSEC_PER_MSEC, /* FOTG210_HRTIMER_DISABLE_PERIODIC */
  902. 15 * NSEC_PER_MSEC, /* FOTG210_HRTIMER_DISABLE_ASYNC */
  903. 100 * NSEC_PER_MSEC, /* FOTG210_HRTIMER_IO_WATCHDOG */
  904. };
  905. /* Enable a pending hrtimer event */
  906. static void fotg210_enable_event(struct fotg210_hcd *fotg210, unsigned event,
  907. bool resched)
  908. {
  909. ktime_t *timeout = &fotg210->hr_timeouts[event];
  910. if (resched)
  911. *timeout = ktime_add(ktime_get(), event_delays_ns[event]);
  912. fotg210->enabled_hrtimer_events |= (1 << event);
  913. /* Track only the lowest-numbered pending event */
  914. if (event < fotg210->next_hrtimer_event) {
  915. fotg210->next_hrtimer_event = event;
  916. hrtimer_start_range_ns(&fotg210->hrtimer, *timeout,
  917. NSEC_PER_MSEC, HRTIMER_MODE_ABS);
  918. }
  919. }
  920. /* Poll the STS_ASS status bit; see when it agrees with CMD_ASE */
  921. static void fotg210_poll_ASS(struct fotg210_hcd *fotg210)
  922. {
  923. unsigned actual, want;
  924. /* Don't enable anything if the controller isn't running (e.g., died) */
  925. if (fotg210->rh_state != FOTG210_RH_RUNNING)
  926. return;
  927. want = (fotg210->command & CMD_ASE) ? STS_ASS : 0;
  928. actual = fotg210_readl(fotg210, &fotg210->regs->status) & STS_ASS;
  929. if (want != actual) {
  930. /* Poll again later, but give up after about 20 ms */
  931. if (fotg210->ASS_poll_count++ < 20) {
  932. fotg210_enable_event(fotg210, FOTG210_HRTIMER_POLL_ASS,
  933. true);
  934. return;
  935. }
  936. fotg210_dbg(fotg210, "Waited too long for the async schedule status (%x/%x), giving up\n",
  937. want, actual);
  938. }
  939. fotg210->ASS_poll_count = 0;
  940. /* The status is up-to-date; restart or stop the schedule as needed */
  941. if (want == 0) { /* Stopped */
  942. if (fotg210->async_count > 0)
  943. fotg210_set_command_bit(fotg210, CMD_ASE);
  944. } else { /* Running */
  945. if (fotg210->async_count == 0) {
  946. /* Turn off the schedule after a while */
  947. fotg210_enable_event(fotg210,
  948. FOTG210_HRTIMER_DISABLE_ASYNC,
  949. true);
  950. }
  951. }
  952. }
  953. /* Turn off the async schedule after a brief delay */
  954. static void fotg210_disable_ASE(struct fotg210_hcd *fotg210)
  955. {
  956. fotg210_clear_command_bit(fotg210, CMD_ASE);
  957. }
  958. /* Poll the STS_PSS status bit; see when it agrees with CMD_PSE */
  959. static void fotg210_poll_PSS(struct fotg210_hcd *fotg210)
  960. {
  961. unsigned actual, want;
  962. /* Don't do anything if the controller isn't running (e.g., died) */
  963. if (fotg210->rh_state != FOTG210_RH_RUNNING)
  964. return;
  965. want = (fotg210->command & CMD_PSE) ? STS_PSS : 0;
  966. actual = fotg210_readl(fotg210, &fotg210->regs->status) & STS_PSS;
  967. if (want != actual) {
  968. /* Poll again later, but give up after about 20 ms */
  969. if (fotg210->PSS_poll_count++ < 20) {
  970. fotg210_enable_event(fotg210, FOTG210_HRTIMER_POLL_PSS,
  971. true);
  972. return;
  973. }
  974. fotg210_dbg(fotg210, "Waited too long for the periodic schedule status (%x/%x), giving up\n",
  975. want, actual);
  976. }
  977. fotg210->PSS_poll_count = 0;
  978. /* The status is up-to-date; restart or stop the schedule as needed */
  979. if (want == 0) { /* Stopped */
  980. if (fotg210->periodic_count > 0)
  981. fotg210_set_command_bit(fotg210, CMD_PSE);
  982. } else { /* Running */
  983. if (fotg210->periodic_count == 0) {
  984. /* Turn off the schedule after a while */
  985. fotg210_enable_event(fotg210,
  986. FOTG210_HRTIMER_DISABLE_PERIODIC,
  987. true);
  988. }
  989. }
  990. }
  991. /* Turn off the periodic schedule after a brief delay */
  992. static void fotg210_disable_PSE(struct fotg210_hcd *fotg210)
  993. {
  994. fotg210_clear_command_bit(fotg210, CMD_PSE);
  995. }
  996. /* Poll the STS_HALT status bit; see when a dead controller stops */
  997. static void fotg210_handle_controller_death(struct fotg210_hcd *fotg210)
  998. {
  999. if (!(fotg210_readl(fotg210, &fotg210->regs->status) & STS_HALT)) {
  1000. /* Give up after a few milliseconds */
  1001. if (fotg210->died_poll_count++ < 5) {
  1002. /* Try again later */
  1003. fotg210_enable_event(fotg210,
  1004. FOTG210_HRTIMER_POLL_DEAD, true);
  1005. return;
  1006. }
  1007. fotg210_warn(fotg210, "Waited too long for the controller to stop, giving up\n");
  1008. }
  1009. /* Clean up the mess */
  1010. fotg210->rh_state = FOTG210_RH_HALTED;
  1011. fotg210_writel(fotg210, 0, &fotg210->regs->intr_enable);
  1012. fotg210_work(fotg210);
  1013. end_unlink_async(fotg210);
  1014. /* Not in process context, so don't try to reset the controller */
  1015. }
  1016. /* Handle unlinked interrupt QHs once they are gone from the hardware */
  1017. static void fotg210_handle_intr_unlinks(struct fotg210_hcd *fotg210)
  1018. {
  1019. bool stopped = (fotg210->rh_state < FOTG210_RH_RUNNING);
  1020. /*
  1021. * Process all the QHs on the intr_unlink list that were added
  1022. * before the current unlink cycle began. The list is in
  1023. * temporal order, so stop when we reach the first entry in the
  1024. * current cycle. But if the root hub isn't running then
  1025. * process all the QHs on the list.
  1026. */
  1027. fotg210->intr_unlinking = true;
  1028. while (fotg210->intr_unlink) {
  1029. struct fotg210_qh *qh = fotg210->intr_unlink;
  1030. if (!stopped && qh->unlink_cycle == fotg210->intr_unlink_cycle)
  1031. break;
  1032. fotg210->intr_unlink = qh->unlink_next;
  1033. qh->unlink_next = NULL;
  1034. end_unlink_intr(fotg210, qh);
  1035. }
  1036. /* Handle remaining entries later */
  1037. if (fotg210->intr_unlink) {
  1038. fotg210_enable_event(fotg210, FOTG210_HRTIMER_UNLINK_INTR,
  1039. true);
  1040. ++fotg210->intr_unlink_cycle;
  1041. }
  1042. fotg210->intr_unlinking = false;
  1043. }
  1044. /* Start another free-iTDs/siTDs cycle */
  1045. static void start_free_itds(struct fotg210_hcd *fotg210)
  1046. {
  1047. if (!(fotg210->enabled_hrtimer_events &
  1048. BIT(FOTG210_HRTIMER_FREE_ITDS))) {
  1049. fotg210->last_itd_to_free = list_entry(
  1050. fotg210->cached_itd_list.prev,
  1051. struct fotg210_itd, itd_list);
  1052. fotg210_enable_event(fotg210, FOTG210_HRTIMER_FREE_ITDS, true);
  1053. }
  1054. }
  1055. /* Wait for controller to stop using old iTDs and siTDs */
  1056. static void end_free_itds(struct fotg210_hcd *fotg210)
  1057. {
  1058. struct fotg210_itd *itd, *n;
  1059. if (fotg210->rh_state < FOTG210_RH_RUNNING)
  1060. fotg210->last_itd_to_free = NULL;
  1061. list_for_each_entry_safe(itd, n, &fotg210->cached_itd_list, itd_list) {
  1062. list_del(&itd->itd_list);
  1063. dma_pool_free(fotg210->itd_pool, itd, itd->itd_dma);
  1064. if (itd == fotg210->last_itd_to_free)
  1065. break;
  1066. }
  1067. if (!list_empty(&fotg210->cached_itd_list))
  1068. start_free_itds(fotg210);
  1069. }
  1070. /* Handle lost (or very late) IAA interrupts */
  1071. static void fotg210_iaa_watchdog(struct fotg210_hcd *fotg210)
  1072. {
  1073. if (fotg210->rh_state != FOTG210_RH_RUNNING)
  1074. return;
  1075. /*
  1076. * Lost IAA irqs wedge things badly; seen first with a vt8235.
  1077. * So we need this watchdog, but must protect it against both
  1078. * (a) SMP races against real IAA firing and retriggering, and
  1079. * (b) clean HC shutdown, when IAA watchdog was pending.
  1080. */
  1081. if (fotg210->async_iaa) {
  1082. u32 cmd, status;
  1083. /* If we get here, IAA is *REALLY* late. It's barely
  1084. * conceivable that the system is so busy that CMD_IAAD
  1085. * is still legitimately set, so let's be sure it's
  1086. * clear before we read STS_IAA. (The HC should clear
  1087. * CMD_IAAD when it sets STS_IAA.)
  1088. */
  1089. cmd = fotg210_readl(fotg210, &fotg210->regs->command);
  1090. /*
  1091. * If IAA is set here it either legitimately triggered
  1092. * after the watchdog timer expired (_way_ late, so we'll
  1093. * still count it as lost) ... or a silicon erratum:
  1094. * - VIA seems to set IAA without triggering the IRQ;
  1095. * - IAAD potentially cleared without setting IAA.
  1096. */
  1097. status = fotg210_readl(fotg210, &fotg210->regs->status);
  1098. if ((status & STS_IAA) || !(cmd & CMD_IAAD)) {
  1099. INCR(fotg210->stats.lost_iaa);
  1100. fotg210_writel(fotg210, STS_IAA,
  1101. &fotg210->regs->status);
  1102. }
  1103. fotg210_dbg(fotg210, "IAA watchdog: status %x cmd %x\n",
  1104. status, cmd);
  1105. end_unlink_async(fotg210);
  1106. }
  1107. }
  1108. /* Enable the I/O watchdog, if appropriate */
  1109. static void turn_on_io_watchdog(struct fotg210_hcd *fotg210)
  1110. {
  1111. /* Not needed if the controller isn't running or it's already enabled */
  1112. if (fotg210->rh_state != FOTG210_RH_RUNNING ||
  1113. (fotg210->enabled_hrtimer_events &
  1114. BIT(FOTG210_HRTIMER_IO_WATCHDOG)))
  1115. return;
  1116. /*
  1117. * Isochronous transfers always need the watchdog.
  1118. * For other sorts we use it only if the flag is set.
  1119. */
  1120. if (fotg210->isoc_count > 0 || (fotg210->need_io_watchdog &&
  1121. fotg210->async_count + fotg210->intr_count > 0))
  1122. fotg210_enable_event(fotg210, FOTG210_HRTIMER_IO_WATCHDOG,
  1123. true);
  1124. }
  1125. /* Handler functions for the hrtimer event types.
  1126. * Keep this array in the same order as the event types indexed by
  1127. * enum fotg210_hrtimer_event in fotg210.h.
  1128. */
  1129. static void (*event_handlers[])(struct fotg210_hcd *) = {
  1130. fotg210_poll_ASS, /* FOTG210_HRTIMER_POLL_ASS */
  1131. fotg210_poll_PSS, /* FOTG210_HRTIMER_POLL_PSS */
  1132. fotg210_handle_controller_death, /* FOTG210_HRTIMER_POLL_DEAD */
  1133. fotg210_handle_intr_unlinks, /* FOTG210_HRTIMER_UNLINK_INTR */
  1134. end_free_itds, /* FOTG210_HRTIMER_FREE_ITDS */
  1135. unlink_empty_async, /* FOTG210_HRTIMER_ASYNC_UNLINKS */
  1136. fotg210_iaa_watchdog, /* FOTG210_HRTIMER_IAA_WATCHDOG */
  1137. fotg210_disable_PSE, /* FOTG210_HRTIMER_DISABLE_PERIODIC */
  1138. fotg210_disable_ASE, /* FOTG210_HRTIMER_DISABLE_ASYNC */
  1139. fotg210_work, /* FOTG210_HRTIMER_IO_WATCHDOG */
  1140. };
  1141. static enum hrtimer_restart fotg210_hrtimer_func(struct hrtimer *t)
  1142. {
  1143. struct fotg210_hcd *fotg210 =
  1144. container_of(t, struct fotg210_hcd, hrtimer);
  1145. ktime_t now;
  1146. unsigned long events;
  1147. unsigned long flags;
  1148. unsigned e;
  1149. spin_lock_irqsave(&fotg210->lock, flags);
  1150. events = fotg210->enabled_hrtimer_events;
  1151. fotg210->enabled_hrtimer_events = 0;
  1152. fotg210->next_hrtimer_event = FOTG210_HRTIMER_NO_EVENT;
  1153. /*
  1154. * Check each pending event. If its time has expired, handle
  1155. * the event; otherwise re-enable it.
  1156. */
  1157. now = ktime_get();
  1158. for_each_set_bit(e, &events, FOTG210_HRTIMER_NUM_EVENTS) {
  1159. if (ktime_compare(now, fotg210->hr_timeouts[e]) >= 0)
  1160. event_handlers[e](fotg210);
  1161. else
  1162. fotg210_enable_event(fotg210, e, false);
  1163. }
  1164. spin_unlock_irqrestore(&fotg210->lock, flags);
  1165. return HRTIMER_NORESTART;
  1166. }
  1167. #define fotg210_bus_suspend NULL
  1168. #define fotg210_bus_resume NULL
  1169. static int check_reset_complete(struct fotg210_hcd *fotg210, int index,
  1170. u32 __iomem *status_reg, int port_status)
  1171. {
  1172. if (!(port_status & PORT_CONNECT))
  1173. return port_status;
  1174. /* if reset finished and it's still not enabled -- handoff */
  1175. if (!(port_status & PORT_PE))
  1176. /* with integrated TT, there's nobody to hand it to! */
  1177. fotg210_dbg(fotg210, "Failed to enable port %d on root hub TT\n",
  1178. index + 1);
  1179. else
  1180. fotg210_dbg(fotg210, "port %d reset complete, port enabled\n",
  1181. index + 1);
  1182. return port_status;
  1183. }
  1184. /* build "status change" packet (one or two bytes) from HC registers */
  1185. static int fotg210_hub_status_data(struct usb_hcd *hcd, char *buf)
  1186. {
  1187. struct fotg210_hcd *fotg210 = hcd_to_fotg210(hcd);
  1188. u32 temp, status;
  1189. u32 mask;
  1190. int retval = 1;
  1191. unsigned long flags;
  1192. /* init status to no-changes */
  1193. buf[0] = 0;
  1194. /* Inform the core about resumes-in-progress by returning
  1195. * a non-zero value even if there are no status changes.
  1196. */
  1197. status = fotg210->resuming_ports;
  1198. mask = PORT_CSC | PORT_PEC;
  1199. /* PORT_RESUME from hardware ~= PORT_STAT_C_SUSPEND */
  1200. /* no hub change reports (bit 0) for now (power, ...) */
  1201. /* port N changes (bit N)? */
  1202. spin_lock_irqsave(&fotg210->lock, flags);
  1203. temp = fotg210_readl(fotg210, &fotg210->regs->port_status);
  1204. /*
  1205. * Return status information even for ports with OWNER set.
  1206. * Otherwise hub_wq wouldn't see the disconnect event when a
  1207. * high-speed device is switched over to the companion
  1208. * controller by the user.
  1209. */
  1210. if ((temp & mask) != 0 || test_bit(0, &fotg210->port_c_suspend) ||
  1211. (fotg210->reset_done[0] &&
  1212. time_after_eq(jiffies, fotg210->reset_done[0]))) {
  1213. buf[0] |= 1 << 1;
  1214. status = STS_PCD;
  1215. }
  1216. /* FIXME autosuspend idle root hubs */
  1217. spin_unlock_irqrestore(&fotg210->lock, flags);
  1218. return status ? retval : 0;
  1219. }
  1220. static void fotg210_hub_descriptor(struct fotg210_hcd *fotg210,
  1221. struct usb_hub_descriptor *desc)
  1222. {
  1223. int ports = HCS_N_PORTS(fotg210->hcs_params);
  1224. u16 temp;
  1225. desc->bDescriptorType = USB_DT_HUB;
  1226. desc->bPwrOn2PwrGood = 10; /* fotg210 1.0, 2.3.9 says 20ms max */
  1227. desc->bHubContrCurrent = 0;
  1228. desc->bNbrPorts = ports;
  1229. temp = 1 + (ports / 8);
  1230. desc->bDescLength = 7 + 2 * temp;
  1231. /* two bitmaps: ports removable, and usb 1.0 legacy PortPwrCtrlMask */
  1232. memset(&desc->u.hs.DeviceRemovable[0], 0, temp);
  1233. memset(&desc->u.hs.DeviceRemovable[temp], 0xff, temp);
  1234. temp = HUB_CHAR_INDV_PORT_OCPM; /* per-port overcurrent reporting */
  1235. temp |= HUB_CHAR_NO_LPSM; /* no power switching */
  1236. desc->wHubCharacteristics = cpu_to_le16(temp);
  1237. }
  1238. static int fotg210_hub_control(struct usb_hcd *hcd, u16 typeReq, u16 wValue,
  1239. u16 wIndex, char *buf, u16 wLength)
  1240. {
  1241. struct fotg210_hcd *fotg210 = hcd_to_fotg210(hcd);
  1242. int ports = HCS_N_PORTS(fotg210->hcs_params);
  1243. u32 __iomem *status_reg = &fotg210->regs->port_status;
  1244. u32 temp, temp1, status;
  1245. unsigned long flags;
  1246. int retval = 0;
  1247. unsigned selector;
  1248. /*
  1249. * FIXME: support SetPortFeatures USB_PORT_FEAT_INDICATOR.
  1250. * HCS_INDICATOR may say we can change LEDs to off/amber/green.
  1251. * (track current state ourselves) ... blink for diagnostics,
  1252. * power, "this is the one", etc. EHCI spec supports this.
  1253. */
  1254. spin_lock_irqsave(&fotg210->lock, flags);
  1255. switch (typeReq) {
  1256. case ClearHubFeature:
  1257. switch (wValue) {
  1258. case C_HUB_LOCAL_POWER:
  1259. case C_HUB_OVER_CURRENT:
  1260. /* no hub-wide feature/status flags */
  1261. break;
  1262. default:
  1263. goto error;
  1264. }
  1265. break;
  1266. case ClearPortFeature:
  1267. if (!wIndex || wIndex > ports)
  1268. goto error;
  1269. wIndex--;
  1270. temp = fotg210_readl(fotg210, status_reg);
  1271. temp &= ~PORT_RWC_BITS;
  1272. /*
  1273. * Even if OWNER is set, so the port is owned by the
  1274. * companion controller, hub_wq needs to be able to clear
  1275. * the port-change status bits (especially
  1276. * USB_PORT_STAT_C_CONNECTION).
  1277. */
  1278. switch (wValue) {
  1279. case USB_PORT_FEAT_ENABLE:
  1280. fotg210_writel(fotg210, temp & ~PORT_PE, status_reg);
  1281. break;
  1282. case USB_PORT_FEAT_C_ENABLE:
  1283. fotg210_writel(fotg210, temp | PORT_PEC, status_reg);
  1284. break;
  1285. case USB_PORT_FEAT_SUSPEND:
  1286. if (temp & PORT_RESET)
  1287. goto error;
  1288. if (!(temp & PORT_SUSPEND))
  1289. break;
  1290. if ((temp & PORT_PE) == 0)
  1291. goto error;
  1292. /* resume signaling for 20 msec */
  1293. fotg210_writel(fotg210, temp | PORT_RESUME, status_reg);
  1294. fotg210->reset_done[wIndex] = jiffies
  1295. + msecs_to_jiffies(USB_RESUME_TIMEOUT);
  1296. break;
  1297. case USB_PORT_FEAT_C_SUSPEND:
  1298. clear_bit(wIndex, &fotg210->port_c_suspend);
  1299. break;
  1300. case USB_PORT_FEAT_C_CONNECTION:
  1301. fotg210_writel(fotg210, temp | PORT_CSC, status_reg);
  1302. break;
  1303. case USB_PORT_FEAT_C_OVER_CURRENT:
  1304. fotg210_writel(fotg210, temp | OTGISR_OVC,
  1305. &fotg210->regs->otgisr);
  1306. break;
  1307. case USB_PORT_FEAT_C_RESET:
  1308. /* GetPortStatus clears reset */
  1309. break;
  1310. default:
  1311. goto error;
  1312. }
  1313. fotg210_readl(fotg210, &fotg210->regs->command);
  1314. break;
  1315. case GetHubDescriptor:
  1316. fotg210_hub_descriptor(fotg210, (struct usb_hub_descriptor *)
  1317. buf);
  1318. break;
  1319. case GetHubStatus:
  1320. /* no hub-wide feature/status flags */
  1321. memset(buf, 0, 4);
  1322. /*cpu_to_le32s ((u32 *) buf); */
  1323. break;
  1324. case GetPortStatus:
  1325. if (!wIndex || wIndex > ports)
  1326. goto error;
  1327. wIndex--;
  1328. status = 0;
  1329. temp = fotg210_readl(fotg210, status_reg);
  1330. /* wPortChange bits */
  1331. if (temp & PORT_CSC)
  1332. status |= USB_PORT_STAT_C_CONNECTION << 16;
  1333. if (temp & PORT_PEC)
  1334. status |= USB_PORT_STAT_C_ENABLE << 16;
  1335. temp1 = fotg210_readl(fotg210, &fotg210->regs->otgisr);
  1336. if (temp1 & OTGISR_OVC)
  1337. status |= USB_PORT_STAT_C_OVERCURRENT << 16;
  1338. /* whoever resumes must GetPortStatus to complete it!! */
  1339. if (temp & PORT_RESUME) {
  1340. /* Remote Wakeup received? */
  1341. if (!fotg210->reset_done[wIndex]) {
  1342. /* resume signaling for 20 msec */
  1343. fotg210->reset_done[wIndex] = jiffies
  1344. + msecs_to_jiffies(20);
  1345. /* check the port again */
  1346. mod_timer(&fotg210_to_hcd(fotg210)->rh_timer,
  1347. fotg210->reset_done[wIndex]);
  1348. }
  1349. /* resume completed? */
  1350. else if (time_after_eq(jiffies,
  1351. fotg210->reset_done[wIndex])) {
  1352. clear_bit(wIndex, &fotg210->suspended_ports);
  1353. set_bit(wIndex, &fotg210->port_c_suspend);
  1354. fotg210->reset_done[wIndex] = 0;
  1355. /* stop resume signaling */
  1356. temp = fotg210_readl(fotg210, status_reg);
  1357. fotg210_writel(fotg210, temp &
  1358. ~(PORT_RWC_BITS | PORT_RESUME),
  1359. status_reg);
  1360. clear_bit(wIndex, &fotg210->resuming_ports);
  1361. retval = handshake(fotg210, status_reg,
  1362. PORT_RESUME, 0, 2000);/* 2ms */
  1363. if (retval != 0) {
  1364. fotg210_err(fotg210,
  1365. "port %d resume error %d\n",
  1366. wIndex + 1, retval);
  1367. goto error;
  1368. }
  1369. temp &= ~(PORT_SUSPEND|PORT_RESUME|(3<<10));
  1370. }
  1371. }
  1372. /* whoever resets must GetPortStatus to complete it!! */
  1373. if ((temp & PORT_RESET) && time_after_eq(jiffies,
  1374. fotg210->reset_done[wIndex])) {
  1375. status |= USB_PORT_STAT_C_RESET << 16;
  1376. fotg210->reset_done[wIndex] = 0;
  1377. clear_bit(wIndex, &fotg210->resuming_ports);
  1378. /* force reset to complete */
  1379. fotg210_writel(fotg210,
  1380. temp & ~(PORT_RWC_BITS | PORT_RESET),
  1381. status_reg);
  1382. /* REVISIT: some hardware needs 550+ usec to clear
  1383. * this bit; seems too long to spin routinely...
  1384. */
  1385. retval = handshake(fotg210, status_reg,
  1386. PORT_RESET, 0, 1000);
  1387. if (retval != 0) {
  1388. fotg210_err(fotg210, "port %d reset error %d\n",
  1389. wIndex + 1, retval);
  1390. goto error;
  1391. }
  1392. /* see what we found out */
  1393. temp = check_reset_complete(fotg210, wIndex, status_reg,
  1394. fotg210_readl(fotg210, status_reg));
  1395. /* restart schedule */
  1396. fotg210->command |= CMD_RUN;
  1397. fotg210_writel(fotg210, fotg210->command, &fotg210->regs->command);
  1398. }
  1399. if (!(temp & (PORT_RESUME|PORT_RESET))) {
  1400. fotg210->reset_done[wIndex] = 0;
  1401. clear_bit(wIndex, &fotg210->resuming_ports);
  1402. }
  1403. /* transfer dedicated ports to the companion hc */
  1404. if ((temp & PORT_CONNECT) &&
  1405. test_bit(wIndex, &fotg210->companion_ports)) {
  1406. temp &= ~PORT_RWC_BITS;
  1407. fotg210_writel(fotg210, temp, status_reg);
  1408. fotg210_dbg(fotg210, "port %d --> companion\n",
  1409. wIndex + 1);
  1410. temp = fotg210_readl(fotg210, status_reg);
  1411. }
  1412. /*
  1413. * Even if OWNER is set, there's no harm letting hub_wq
  1414. * see the wPortStatus values (they should all be 0 except
  1415. * for PORT_POWER anyway).
  1416. */
  1417. if (temp & PORT_CONNECT) {
  1418. status |= USB_PORT_STAT_CONNECTION;
  1419. status |= fotg210_port_speed(fotg210, temp);
  1420. }
  1421. if (temp & PORT_PE)
  1422. status |= USB_PORT_STAT_ENABLE;
  1423. /* maybe the port was unsuspended without our knowledge */
  1424. if (temp & (PORT_SUSPEND|PORT_RESUME)) {
  1425. status |= USB_PORT_STAT_SUSPEND;
  1426. } else if (test_bit(wIndex, &fotg210->suspended_ports)) {
  1427. clear_bit(wIndex, &fotg210->suspended_ports);
  1428. clear_bit(wIndex, &fotg210->resuming_ports);
  1429. fotg210->reset_done[wIndex] = 0;
  1430. if (temp & PORT_PE)
  1431. set_bit(wIndex, &fotg210->port_c_suspend);
  1432. }
  1433. temp1 = fotg210_readl(fotg210, &fotg210->regs->otgisr);
  1434. if (temp1 & OTGISR_OVC)
  1435. status |= USB_PORT_STAT_OVERCURRENT;
  1436. if (temp & PORT_RESET)
  1437. status |= USB_PORT_STAT_RESET;
  1438. if (test_bit(wIndex, &fotg210->port_c_suspend))
  1439. status |= USB_PORT_STAT_C_SUSPEND << 16;
  1440. if (status & ~0xffff) /* only if wPortChange is interesting */
  1441. dbg_port(fotg210, "GetStatus", wIndex + 1, temp);
  1442. put_unaligned_le32(status, buf);
  1443. break;
  1444. case SetHubFeature:
  1445. switch (wValue) {
  1446. case C_HUB_LOCAL_POWER:
  1447. case C_HUB_OVER_CURRENT:
  1448. /* no hub-wide feature/status flags */
  1449. break;
  1450. default:
  1451. goto error;
  1452. }
  1453. break;
  1454. case SetPortFeature:
  1455. selector = wIndex >> 8;
  1456. wIndex &= 0xff;
  1457. if (!wIndex || wIndex > ports)
  1458. goto error;
  1459. wIndex--;
  1460. temp = fotg210_readl(fotg210, status_reg);
  1461. temp &= ~PORT_RWC_BITS;
  1462. switch (wValue) {
  1463. case USB_PORT_FEAT_SUSPEND:
  1464. if ((temp & PORT_PE) == 0
  1465. || (temp & PORT_RESET) != 0)
  1466. goto error;
  1467. /* After above check the port must be connected.
  1468. * Set appropriate bit thus could put phy into low power
  1469. * mode if we have hostpc feature
  1470. */
  1471. fotg210_writel(fotg210, temp | PORT_SUSPEND,
  1472. status_reg);
  1473. set_bit(wIndex, &fotg210->suspended_ports);
  1474. break;
  1475. case USB_PORT_FEAT_RESET:
  1476. if (temp & PORT_RESUME)
  1477. goto error;
  1478. /* line status bits may report this as low speed,
  1479. * which can be fine if this root hub has a
  1480. * transaction translator built in.
  1481. */
  1482. fotg210_dbg(fotg210, "port %d reset\n", wIndex + 1);
  1483. temp |= PORT_RESET;
  1484. temp &= ~PORT_PE;
  1485. /*
  1486. * caller must wait, then call GetPortStatus
  1487. * usb 2.0 spec says 50 ms resets on root
  1488. */
  1489. fotg210->reset_done[wIndex] = jiffies
  1490. + msecs_to_jiffies(50);
  1491. fotg210_writel(fotg210, temp, status_reg);
  1492. break;
  1493. /* For downstream facing ports (these): one hub port is put
  1494. * into test mode according to USB2 11.24.2.13, then the hub
  1495. * must be reset (which for root hub now means rmmod+modprobe,
  1496. * or else system reboot). See EHCI 2.3.9 and 4.14 for info
  1497. * about the EHCI-specific stuff.
  1498. */
  1499. case USB_PORT_FEAT_TEST:
  1500. if (!selector || selector > 5)
  1501. goto error;
  1502. spin_unlock_irqrestore(&fotg210->lock, flags);
  1503. fotg210_quiesce(fotg210);
  1504. spin_lock_irqsave(&fotg210->lock, flags);
  1505. /* Put all enabled ports into suspend */
  1506. temp = fotg210_readl(fotg210, status_reg) &
  1507. ~PORT_RWC_BITS;
  1508. if (temp & PORT_PE)
  1509. fotg210_writel(fotg210, temp | PORT_SUSPEND,
  1510. status_reg);
  1511. spin_unlock_irqrestore(&fotg210->lock, flags);
  1512. fotg210_halt(fotg210);
  1513. spin_lock_irqsave(&fotg210->lock, flags);
  1514. temp = fotg210_readl(fotg210, status_reg);
  1515. temp |= selector << 16;
  1516. fotg210_writel(fotg210, temp, status_reg);
  1517. break;
  1518. default:
  1519. goto error;
  1520. }
  1521. fotg210_readl(fotg210, &fotg210->regs->command);
  1522. break;
  1523. default:
  1524. error:
  1525. /* "stall" on error */
  1526. retval = -EPIPE;
  1527. }
  1528. spin_unlock_irqrestore(&fotg210->lock, flags);
  1529. return retval;
  1530. }
  1531. static void __maybe_unused fotg210_relinquish_port(struct usb_hcd *hcd,
  1532. int portnum)
  1533. {
  1534. return;
  1535. }
  1536. static int __maybe_unused fotg210_port_handed_over(struct usb_hcd *hcd,
  1537. int portnum)
  1538. {
  1539. return 0;
  1540. }
  1541. /* There's basically three types of memory:
  1542. * - data used only by the HCD ... kmalloc is fine
  1543. * - async and periodic schedules, shared by HC and HCD ... these
  1544. * need to use dma_pool or dma_alloc_coherent
  1545. * - driver buffers, read/written by HC ... single shot DMA mapped
  1546. *
  1547. * There's also "register" data (e.g. PCI or SOC), which is memory mapped.
  1548. * No memory seen by this driver is pageable.
  1549. */
  1550. /* Allocate the key transfer structures from the previously allocated pool */
  1551. static inline void fotg210_qtd_init(struct fotg210_hcd *fotg210,
  1552. struct fotg210_qtd *qtd, dma_addr_t dma)
  1553. {
  1554. memset(qtd, 0, sizeof(*qtd));
  1555. qtd->qtd_dma = dma;
  1556. qtd->hw_token = cpu_to_hc32(fotg210, QTD_STS_HALT);
  1557. qtd->hw_next = FOTG210_LIST_END(fotg210);
  1558. qtd->hw_alt_next = FOTG210_LIST_END(fotg210);
  1559. INIT_LIST_HEAD(&qtd->qtd_list);
  1560. }
  1561. static struct fotg210_qtd *fotg210_qtd_alloc(struct fotg210_hcd *fotg210,
  1562. gfp_t flags)
  1563. {
  1564. struct fotg210_qtd *qtd;
  1565. dma_addr_t dma;
  1566. qtd = dma_pool_alloc(fotg210->qtd_pool, flags, &dma);
  1567. if (qtd != NULL)
  1568. fotg210_qtd_init(fotg210, qtd, dma);
  1569. return qtd;
  1570. }
  1571. static inline void fotg210_qtd_free(struct fotg210_hcd *fotg210,
  1572. struct fotg210_qtd *qtd)
  1573. {
  1574. dma_pool_free(fotg210->qtd_pool, qtd, qtd->qtd_dma);
  1575. }
  1576. static void qh_destroy(struct fotg210_hcd *fotg210, struct fotg210_qh *qh)
  1577. {
  1578. /* clean qtds first, and know this is not linked */
  1579. if (!list_empty(&qh->qtd_list) || qh->qh_next.ptr) {
  1580. fotg210_dbg(fotg210, "unused qh not empty!\n");
  1581. BUG();
  1582. }
  1583. if (qh->dummy)
  1584. fotg210_qtd_free(fotg210, qh->dummy);
  1585. dma_pool_free(fotg210->qh_pool, qh->hw, qh->qh_dma);
  1586. kfree(qh);
  1587. }
  1588. static struct fotg210_qh *fotg210_qh_alloc(struct fotg210_hcd *fotg210,
  1589. gfp_t flags)
  1590. {
  1591. struct fotg210_qh *qh;
  1592. dma_addr_t dma;
  1593. qh = kzalloc(sizeof(*qh), GFP_ATOMIC);
  1594. if (!qh)
  1595. goto done;
  1596. qh->hw = (struct fotg210_qh_hw *)
  1597. dma_pool_zalloc(fotg210->qh_pool, flags, &dma);
  1598. if (!qh->hw)
  1599. goto fail;
  1600. qh->qh_dma = dma;
  1601. INIT_LIST_HEAD(&qh->qtd_list);
  1602. /* dummy td enables safe urb queuing */
  1603. qh->dummy = fotg210_qtd_alloc(fotg210, flags);
  1604. if (qh->dummy == NULL) {
  1605. fotg210_dbg(fotg210, "no dummy td\n");
  1606. goto fail1;
  1607. }
  1608. done:
  1609. return qh;
  1610. fail1:
  1611. dma_pool_free(fotg210->qh_pool, qh->hw, qh->qh_dma);
  1612. fail:
  1613. kfree(qh);
  1614. return NULL;
  1615. }
  1616. /* The queue heads and transfer descriptors are managed from pools tied
  1617. * to each of the "per device" structures.
  1618. * This is the initialisation and cleanup code.
  1619. */
  1620. static void fotg210_mem_cleanup(struct fotg210_hcd *fotg210)
  1621. {
  1622. if (fotg210->async)
  1623. qh_destroy(fotg210, fotg210->async);
  1624. fotg210->async = NULL;
  1625. if (fotg210->dummy)
  1626. qh_destroy(fotg210, fotg210->dummy);
  1627. fotg210->dummy = NULL;
  1628. /* DMA consistent memory and pools */
  1629. dma_pool_destroy(fotg210->qtd_pool);
  1630. fotg210->qtd_pool = NULL;
  1631. dma_pool_destroy(fotg210->qh_pool);
  1632. fotg210->qh_pool = NULL;
  1633. dma_pool_destroy(fotg210->itd_pool);
  1634. fotg210->itd_pool = NULL;
  1635. if (fotg210->periodic)
  1636. dma_free_coherent(fotg210_to_hcd(fotg210)->self.controller,
  1637. fotg210->periodic_size * sizeof(u32),
  1638. fotg210->periodic, fotg210->periodic_dma);
  1639. fotg210->periodic = NULL;
  1640. /* shadow periodic table */
  1641. kfree(fotg210->pshadow);
  1642. fotg210->pshadow = NULL;
  1643. }
  1644. /* remember to add cleanup code (above) if you add anything here */
  1645. static int fotg210_mem_init(struct fotg210_hcd *fotg210, gfp_t flags)
  1646. {
  1647. int i;
  1648. /* QTDs for control/bulk/intr transfers */
  1649. fotg210->qtd_pool = dma_pool_create("fotg210_qtd",
  1650. fotg210_to_hcd(fotg210)->self.controller,
  1651. sizeof(struct fotg210_qtd),
  1652. 32 /* byte alignment (for hw parts) */,
  1653. 4096 /* can't cross 4K */);
  1654. if (!fotg210->qtd_pool)
  1655. goto fail;
  1656. /* QHs for control/bulk/intr transfers */
  1657. fotg210->qh_pool = dma_pool_create("fotg210_qh",
  1658. fotg210_to_hcd(fotg210)->self.controller,
  1659. sizeof(struct fotg210_qh_hw),
  1660. 32 /* byte alignment (for hw parts) */,
  1661. 4096 /* can't cross 4K */);
  1662. if (!fotg210->qh_pool)
  1663. goto fail;
  1664. fotg210->async = fotg210_qh_alloc(fotg210, flags);
  1665. if (!fotg210->async)
  1666. goto fail;
  1667. /* ITD for high speed ISO transfers */
  1668. fotg210->itd_pool = dma_pool_create("fotg210_itd",
  1669. fotg210_to_hcd(fotg210)->self.controller,
  1670. sizeof(struct fotg210_itd),
  1671. 64 /* byte alignment (for hw parts) */,
  1672. 4096 /* can't cross 4K */);
  1673. if (!fotg210->itd_pool)
  1674. goto fail;
  1675. /* Hardware periodic table */
  1676. fotg210->periodic =
  1677. dma_alloc_coherent(fotg210_to_hcd(fotg210)->self.controller,
  1678. fotg210->periodic_size * sizeof(__le32),
  1679. &fotg210->periodic_dma, 0);
  1680. if (fotg210->periodic == NULL)
  1681. goto fail;
  1682. for (i = 0; i < fotg210->periodic_size; i++)
  1683. fotg210->periodic[i] = FOTG210_LIST_END(fotg210);
  1684. /* software shadow of hardware table */
  1685. fotg210->pshadow = kcalloc(fotg210->periodic_size, sizeof(void *),
  1686. flags);
  1687. if (fotg210->pshadow != NULL)
  1688. return 0;
  1689. fail:
  1690. fotg210_dbg(fotg210, "couldn't init memory\n");
  1691. fotg210_mem_cleanup(fotg210);
  1692. return -ENOMEM;
  1693. }
  1694. /* EHCI hardware queue manipulation ... the core. QH/QTD manipulation.
  1695. *
  1696. * Control, bulk, and interrupt traffic all use "qh" lists. They list "qtd"
  1697. * entries describing USB transactions, max 16-20kB/entry (with 4kB-aligned
  1698. * buffers needed for the larger number). We use one QH per endpoint, queue
  1699. * multiple urbs (all three types) per endpoint. URBs may need several qtds.
  1700. *
  1701. * ISO traffic uses "ISO TD" (itd) records, and (along with
  1702. * interrupts) needs careful scheduling. Performance improvements can be
  1703. * an ongoing challenge. That's in "ehci-sched.c".
  1704. *
  1705. * USB 1.1 devices are handled (a) by "companion" OHCI or UHCI root hubs,
  1706. * or otherwise through transaction translators (TTs) in USB 2.0 hubs using
  1707. * (b) special fields in qh entries or (c) split iso entries. TTs will
  1708. * buffer low/full speed data so the host collects it at high speed.
  1709. */
  1710. /* fill a qtd, returning how much of the buffer we were able to queue up */
  1711. static int qtd_fill(struct fotg210_hcd *fotg210, struct fotg210_qtd *qtd,
  1712. dma_addr_t buf, size_t len, int token, int maxpacket)
  1713. {
  1714. int i, count;
  1715. u64 addr = buf;
  1716. /* one buffer entry per 4K ... first might be short or unaligned */
  1717. qtd->hw_buf[0] = cpu_to_hc32(fotg210, (u32)addr);
  1718. qtd->hw_buf_hi[0] = cpu_to_hc32(fotg210, (u32)(addr >> 32));
  1719. count = 0x1000 - (buf & 0x0fff); /* rest of that page */
  1720. if (likely(len < count)) /* ... iff needed */
  1721. count = len;
  1722. else {
  1723. buf += 0x1000;
  1724. buf &= ~0x0fff;
  1725. /* per-qtd limit: from 16K to 20K (best alignment) */
  1726. for (i = 1; count < len && i < 5; i++) {
  1727. addr = buf;
  1728. qtd->hw_buf[i] = cpu_to_hc32(fotg210, (u32)addr);
  1729. qtd->hw_buf_hi[i] = cpu_to_hc32(fotg210,
  1730. (u32)(addr >> 32));
  1731. buf += 0x1000;
  1732. if ((count + 0x1000) < len)
  1733. count += 0x1000;
  1734. else
  1735. count = len;
  1736. }
  1737. /* short packets may only terminate transfers */
  1738. if (count != len)
  1739. count -= (count % maxpacket);
  1740. }
  1741. qtd->hw_token = cpu_to_hc32(fotg210, (count << 16) | token);
  1742. qtd->length = count;
  1743. return count;
  1744. }
  1745. static inline void qh_update(struct fotg210_hcd *fotg210,
  1746. struct fotg210_qh *qh, struct fotg210_qtd *qtd)
  1747. {
  1748. struct fotg210_qh_hw *hw = qh->hw;
  1749. /* writes to an active overlay are unsafe */
  1750. BUG_ON(qh->qh_state != QH_STATE_IDLE);
  1751. hw->hw_qtd_next = QTD_NEXT(fotg210, qtd->qtd_dma);
  1752. hw->hw_alt_next = FOTG210_LIST_END(fotg210);
  1753. /* Except for control endpoints, we make hardware maintain data
  1754. * toggle (like OHCI) ... here (re)initialize the toggle in the QH,
  1755. * and set the pseudo-toggle in udev. Only usb_clear_halt() will
  1756. * ever clear it.
  1757. */
  1758. if (!(hw->hw_info1 & cpu_to_hc32(fotg210, QH_TOGGLE_CTL))) {
  1759. unsigned is_out, epnum;
  1760. is_out = qh->is_out;
  1761. epnum = (hc32_to_cpup(fotg210, &hw->hw_info1) >> 8) & 0x0f;
  1762. if (unlikely(!usb_gettoggle(qh->dev, epnum, is_out))) {
  1763. hw->hw_token &= ~cpu_to_hc32(fotg210, QTD_TOGGLE);
  1764. usb_settoggle(qh->dev, epnum, is_out, 1);
  1765. }
  1766. }
  1767. hw->hw_token &= cpu_to_hc32(fotg210, QTD_TOGGLE | QTD_STS_PING);
  1768. }
  1769. /* if it weren't for a common silicon quirk (writing the dummy into the qh
  1770. * overlay, so qh->hw_token wrongly becomes inactive/halted), only fault
  1771. * recovery (including urb dequeue) would need software changes to a QH...
  1772. */
  1773. static void qh_refresh(struct fotg210_hcd *fotg210, struct fotg210_qh *qh)
  1774. {
  1775. struct fotg210_qtd *qtd;
  1776. if (list_empty(&qh->qtd_list))
  1777. qtd = qh->dummy;
  1778. else {
  1779. qtd = list_entry(qh->qtd_list.next,
  1780. struct fotg210_qtd, qtd_list);
  1781. /*
  1782. * first qtd may already be partially processed.
  1783. * If we come here during unlink, the QH overlay region
  1784. * might have reference to the just unlinked qtd. The
  1785. * qtd is updated in qh_completions(). Update the QH
  1786. * overlay here.
  1787. */
  1788. if (cpu_to_hc32(fotg210, qtd->qtd_dma) == qh->hw->hw_current) {
  1789. qh->hw->hw_qtd_next = qtd->hw_next;
  1790. qtd = NULL;
  1791. }
  1792. }
  1793. if (qtd)
  1794. qh_update(fotg210, qh, qtd);
  1795. }
  1796. static void qh_link_async(struct fotg210_hcd *fotg210, struct fotg210_qh *qh);
  1797. static void fotg210_clear_tt_buffer_complete(struct usb_hcd *hcd,
  1798. struct usb_host_endpoint *ep)
  1799. {
  1800. struct fotg210_hcd *fotg210 = hcd_to_fotg210(hcd);
  1801. struct fotg210_qh *qh = ep->hcpriv;
  1802. unsigned long flags;
  1803. spin_lock_irqsave(&fotg210->lock, flags);
  1804. qh->clearing_tt = 0;
  1805. if (qh->qh_state == QH_STATE_IDLE && !list_empty(&qh->qtd_list)
  1806. && fotg210->rh_state == FOTG210_RH_RUNNING)
  1807. qh_link_async(fotg210, qh);
  1808. spin_unlock_irqrestore(&fotg210->lock, flags);
  1809. }
  1810. static void fotg210_clear_tt_buffer(struct fotg210_hcd *fotg210,
  1811. struct fotg210_qh *qh, struct urb *urb, u32 token)
  1812. {
  1813. /* If an async split transaction gets an error or is unlinked,
  1814. * the TT buffer may be left in an indeterminate state. We
  1815. * have to clear the TT buffer.
  1816. *
  1817. * Note: this routine is never called for Isochronous transfers.
  1818. */
  1819. if (urb->dev->tt && !usb_pipeint(urb->pipe) && !qh->clearing_tt) {
  1820. struct usb_device *tt = urb->dev->tt->hub;
  1821. dev_dbg(&tt->dev,
  1822. "clear tt buffer port %d, a%d ep%d t%08x\n",
  1823. urb->dev->ttport, urb->dev->devnum,
  1824. usb_pipeendpoint(urb->pipe), token);
  1825. if (urb->dev->tt->hub !=
  1826. fotg210_to_hcd(fotg210)->self.root_hub) {
  1827. if (usb_hub_clear_tt_buffer(urb) == 0)
  1828. qh->clearing_tt = 1;
  1829. }
  1830. }
  1831. }
  1832. static int qtd_copy_status(struct fotg210_hcd *fotg210, struct urb *urb,
  1833. size_t length, u32 token)
  1834. {
  1835. int status = -EINPROGRESS;
  1836. /* count IN/OUT bytes, not SETUP (even short packets) */
  1837. if (likely(QTD_PID(token) != 2))
  1838. urb->actual_length += length - QTD_LENGTH(token);
  1839. /* don't modify error codes */
  1840. if (unlikely(urb->unlinked))
  1841. return status;
  1842. /* force cleanup after short read; not always an error */
  1843. if (unlikely(IS_SHORT_READ(token)))
  1844. status = -EREMOTEIO;
  1845. /* serious "can't proceed" faults reported by the hardware */
  1846. if (token & QTD_STS_HALT) {
  1847. if (token & QTD_STS_BABBLE) {
  1848. /* FIXME "must" disable babbling device's port too */
  1849. status = -EOVERFLOW;
  1850. /* CERR nonzero + halt --> stall */
  1851. } else if (QTD_CERR(token)) {
  1852. status = -EPIPE;
  1853. /* In theory, more than one of the following bits can be set
  1854. * since they are sticky and the transaction is retried.
  1855. * Which to test first is rather arbitrary.
  1856. */
  1857. } else if (token & QTD_STS_MMF) {
  1858. /* fs/ls interrupt xfer missed the complete-split */
  1859. status = -EPROTO;
  1860. } else if (token & QTD_STS_DBE) {
  1861. status = (QTD_PID(token) == 1) /* IN ? */
  1862. ? -ENOSR /* hc couldn't read data */
  1863. : -ECOMM; /* hc couldn't write data */
  1864. } else if (token & QTD_STS_XACT) {
  1865. /* timeout, bad CRC, wrong PID, etc */
  1866. fotg210_dbg(fotg210, "devpath %s ep%d%s 3strikes\n",
  1867. urb->dev->devpath,
  1868. usb_pipeendpoint(urb->pipe),
  1869. usb_pipein(urb->pipe) ? "in" : "out");
  1870. status = -EPROTO;
  1871. } else { /* unknown */
  1872. status = -EPROTO;
  1873. }
  1874. fotg210_dbg(fotg210,
  1875. "dev%d ep%d%s qtd token %08x --> status %d\n",
  1876. usb_pipedevice(urb->pipe),
  1877. usb_pipeendpoint(urb->pipe),
  1878. usb_pipein(urb->pipe) ? "in" : "out",
  1879. token, status);
  1880. }
  1881. return status;
  1882. }
  1883. static void fotg210_urb_done(struct fotg210_hcd *fotg210, struct urb *urb,
  1884. int status)
  1885. __releases(fotg210->lock)
  1886. __acquires(fotg210->lock)
  1887. {
  1888. if (likely(urb->hcpriv != NULL)) {
  1889. struct fotg210_qh *qh = (struct fotg210_qh *) urb->hcpriv;
  1890. /* S-mask in a QH means it's an interrupt urb */
  1891. if ((qh->hw->hw_info2 & cpu_to_hc32(fotg210, QH_SMASK)) != 0) {
  1892. /* ... update hc-wide periodic stats (for usbfs) */
  1893. fotg210_to_hcd(fotg210)->self.bandwidth_int_reqs--;
  1894. }
  1895. }
  1896. if (unlikely(urb->unlinked)) {
  1897. INCR(fotg210->stats.unlink);
  1898. } else {
  1899. /* report non-error and short read status as zero */
  1900. if (status == -EINPROGRESS || status == -EREMOTEIO)
  1901. status = 0;
  1902. INCR(fotg210->stats.complete);
  1903. }
  1904. #ifdef FOTG210_URB_TRACE
  1905. fotg210_dbg(fotg210,
  1906. "%s %s urb %p ep%d%s status %d len %d/%d\n",
  1907. __func__, urb->dev->devpath, urb,
  1908. usb_pipeendpoint(urb->pipe),
  1909. usb_pipein(urb->pipe) ? "in" : "out",
  1910. status,
  1911. urb->actual_length, urb->transfer_buffer_length);
  1912. #endif
  1913. /* complete() can reenter this HCD */
  1914. usb_hcd_unlink_urb_from_ep(fotg210_to_hcd(fotg210), urb);
  1915. spin_unlock(&fotg210->lock);
  1916. usb_hcd_giveback_urb(fotg210_to_hcd(fotg210), urb, status);
  1917. spin_lock(&fotg210->lock);
  1918. }
  1919. static int qh_schedule(struct fotg210_hcd *fotg210, struct fotg210_qh *qh);
  1920. /* Process and free completed qtds for a qh, returning URBs to drivers.
  1921. * Chases up to qh->hw_current. Returns number of completions called,
  1922. * indicating how much "real" work we did.
  1923. */
  1924. static unsigned qh_completions(struct fotg210_hcd *fotg210,
  1925. struct fotg210_qh *qh)
  1926. {
  1927. struct fotg210_qtd *last, *end = qh->dummy;
  1928. struct fotg210_qtd *qtd, *tmp;
  1929. int last_status;
  1930. int stopped;
  1931. unsigned count = 0;
  1932. u8 state;
  1933. struct fotg210_qh_hw *hw = qh->hw;
  1934. if (unlikely(list_empty(&qh->qtd_list)))
  1935. return count;
  1936. /* completions (or tasks on other cpus) must never clobber HALT
  1937. * till we've gone through and cleaned everything up, even when
  1938. * they add urbs to this qh's queue or mark them for unlinking.
  1939. *
  1940. * NOTE: unlinking expects to be done in queue order.
  1941. *
  1942. * It's a bug for qh->qh_state to be anything other than
  1943. * QH_STATE_IDLE, unless our caller is scan_async() or
  1944. * scan_intr().
  1945. */
  1946. state = qh->qh_state;
  1947. qh->qh_state = QH_STATE_COMPLETING;
  1948. stopped = (state == QH_STATE_IDLE);
  1949. rescan:
  1950. last = NULL;
  1951. last_status = -EINPROGRESS;
  1952. qh->needs_rescan = 0;
  1953. /* remove de-activated QTDs from front of queue.
  1954. * after faults (including short reads), cleanup this urb
  1955. * then let the queue advance.
  1956. * if queue is stopped, handles unlinks.
  1957. */
  1958. list_for_each_entry_safe(qtd, tmp, &qh->qtd_list, qtd_list) {
  1959. struct urb *urb;
  1960. u32 token = 0;
  1961. urb = qtd->urb;
  1962. /* clean up any state from previous QTD ...*/
  1963. if (last) {
  1964. if (likely(last->urb != urb)) {
  1965. fotg210_urb_done(fotg210, last->urb,
  1966. last_status);
  1967. count++;
  1968. last_status = -EINPROGRESS;
  1969. }
  1970. fotg210_qtd_free(fotg210, last);
  1971. last = NULL;
  1972. }
  1973. /* ignore urbs submitted during completions we reported */
  1974. if (qtd == end)
  1975. break;
  1976. /* hardware copies qtd out of qh overlay */
  1977. rmb();
  1978. token = hc32_to_cpu(fotg210, qtd->hw_token);
  1979. /* always clean up qtds the hc de-activated */
  1980. retry_xacterr:
  1981. if ((token & QTD_STS_ACTIVE) == 0) {
  1982. /* Report Data Buffer Error: non-fatal but useful */
  1983. if (token & QTD_STS_DBE)
  1984. fotg210_dbg(fotg210,
  1985. "detected DataBufferErr for urb %p ep%d%s len %d, qtd %p [qh %p]\n",
  1986. urb, usb_endpoint_num(&urb->ep->desc),
  1987. usb_endpoint_dir_in(&urb->ep->desc)
  1988. ? "in" : "out",
  1989. urb->transfer_buffer_length, qtd, qh);
  1990. /* on STALL, error, and short reads this urb must
  1991. * complete and all its qtds must be recycled.
  1992. */
  1993. if ((token & QTD_STS_HALT) != 0) {
  1994. /* retry transaction errors until we
  1995. * reach the software xacterr limit
  1996. */
  1997. if ((token & QTD_STS_XACT) &&
  1998. QTD_CERR(token) == 0 &&
  1999. ++qh->xacterrs < QH_XACTERR_MAX &&
  2000. !urb->unlinked) {
  2001. fotg210_dbg(fotg210,
  2002. "detected XactErr len %zu/%zu retry %d\n",
  2003. qtd->length - QTD_LENGTH(token),
  2004. qtd->length,
  2005. qh->xacterrs);
  2006. /* reset the token in the qtd and the
  2007. * qh overlay (which still contains
  2008. * the qtd) so that we pick up from
  2009. * where we left off
  2010. */
  2011. token &= ~QTD_STS_HALT;
  2012. token |= QTD_STS_ACTIVE |
  2013. (FOTG210_TUNE_CERR << 10);
  2014. qtd->hw_token = cpu_to_hc32(fotg210,
  2015. token);
  2016. wmb();
  2017. hw->hw_token = cpu_to_hc32(fotg210,
  2018. token);
  2019. goto retry_xacterr;
  2020. }
  2021. stopped = 1;
  2022. /* magic dummy for some short reads; qh won't advance.
  2023. * that silicon quirk can kick in with this dummy too.
  2024. *
  2025. * other short reads won't stop the queue, including
  2026. * control transfers (status stage handles that) or
  2027. * most other single-qtd reads ... the queue stops if
  2028. * URB_SHORT_NOT_OK was set so the driver submitting
  2029. * the urbs could clean it up.
  2030. */
  2031. } else if (IS_SHORT_READ(token) &&
  2032. !(qtd->hw_alt_next &
  2033. FOTG210_LIST_END(fotg210))) {
  2034. stopped = 1;
  2035. }
  2036. /* stop scanning when we reach qtds the hc is using */
  2037. } else if (likely(!stopped
  2038. && fotg210->rh_state >= FOTG210_RH_RUNNING)) {
  2039. break;
  2040. /* scan the whole queue for unlinks whenever it stops */
  2041. } else {
  2042. stopped = 1;
  2043. /* cancel everything if we halt, suspend, etc */
  2044. if (fotg210->rh_state < FOTG210_RH_RUNNING)
  2045. last_status = -ESHUTDOWN;
  2046. /* this qtd is active; skip it unless a previous qtd
  2047. * for its urb faulted, or its urb was canceled.
  2048. */
  2049. else if (last_status == -EINPROGRESS && !urb->unlinked)
  2050. continue;
  2051. /* qh unlinked; token in overlay may be most current */
  2052. if (state == QH_STATE_IDLE &&
  2053. cpu_to_hc32(fotg210, qtd->qtd_dma)
  2054. == hw->hw_current) {
  2055. token = hc32_to_cpu(fotg210, hw->hw_token);
  2056. /* An unlink may leave an incomplete
  2057. * async transaction in the TT buffer.
  2058. * We have to clear it.
  2059. */
  2060. fotg210_clear_tt_buffer(fotg210, qh, urb,
  2061. token);
  2062. }
  2063. }
  2064. /* unless we already know the urb's status, collect qtd status
  2065. * and update count of bytes transferred. in common short read
  2066. * cases with only one data qtd (including control transfers),
  2067. * queue processing won't halt. but with two or more qtds (for
  2068. * example, with a 32 KB transfer), when the first qtd gets a
  2069. * short read the second must be removed by hand.
  2070. */
  2071. if (last_status == -EINPROGRESS) {
  2072. last_status = qtd_copy_status(fotg210, urb,
  2073. qtd->length, token);
  2074. if (last_status == -EREMOTEIO &&
  2075. (qtd->hw_alt_next &
  2076. FOTG210_LIST_END(fotg210)))
  2077. last_status = -EINPROGRESS;
  2078. /* As part of low/full-speed endpoint-halt processing
  2079. * we must clear the TT buffer (11.17.5).
  2080. */
  2081. if (unlikely(last_status != -EINPROGRESS &&
  2082. last_status != -EREMOTEIO)) {
  2083. /* The TT's in some hubs malfunction when they
  2084. * receive this request following a STALL (they
  2085. * stop sending isochronous packets). Since a
  2086. * STALL can't leave the TT buffer in a busy
  2087. * state (if you believe Figures 11-48 - 11-51
  2088. * in the USB 2.0 spec), we won't clear the TT
  2089. * buffer in this case. Strictly speaking this
  2090. * is a violation of the spec.
  2091. */
  2092. if (last_status != -EPIPE)
  2093. fotg210_clear_tt_buffer(fotg210, qh,
  2094. urb, token);
  2095. }
  2096. }
  2097. /* if we're removing something not at the queue head,
  2098. * patch the hardware queue pointer.
  2099. */
  2100. if (stopped && qtd->qtd_list.prev != &qh->qtd_list) {
  2101. last = list_entry(qtd->qtd_list.prev,
  2102. struct fotg210_qtd, qtd_list);
  2103. last->hw_next = qtd->hw_next;
  2104. }
  2105. /* remove qtd; it's recycled after possible urb completion */
  2106. list_del(&qtd->qtd_list);
  2107. last = qtd;
  2108. /* reinit the xacterr counter for the next qtd */
  2109. qh->xacterrs = 0;
  2110. }
  2111. /* last urb's completion might still need calling */
  2112. if (likely(last != NULL)) {
  2113. fotg210_urb_done(fotg210, last->urb, last_status);
  2114. count++;
  2115. fotg210_qtd_free(fotg210, last);
  2116. }
  2117. /* Do we need to rescan for URBs dequeued during a giveback? */
  2118. if (unlikely(qh->needs_rescan)) {
  2119. /* If the QH is already unlinked, do the rescan now. */
  2120. if (state == QH_STATE_IDLE)
  2121. goto rescan;
  2122. /* Otherwise we have to wait until the QH is fully unlinked.
  2123. * Our caller will start an unlink if qh->needs_rescan is
  2124. * set. But if an unlink has already started, nothing needs
  2125. * to be done.
  2126. */
  2127. if (state != QH_STATE_LINKED)
  2128. qh->needs_rescan = 0;
  2129. }
  2130. /* restore original state; caller must unlink or relink */
  2131. qh->qh_state = state;
  2132. /* be sure the hardware's done with the qh before refreshing
  2133. * it after fault cleanup, or recovering from silicon wrongly
  2134. * overlaying the dummy qtd (which reduces DMA chatter).
  2135. */
  2136. if (stopped != 0 || hw->hw_qtd_next == FOTG210_LIST_END(fotg210)) {
  2137. switch (state) {
  2138. case QH_STATE_IDLE:
  2139. qh_refresh(fotg210, qh);
  2140. break;
  2141. case QH_STATE_LINKED:
  2142. /* We won't refresh a QH that's linked (after the HC
  2143. * stopped the queue). That avoids a race:
  2144. * - HC reads first part of QH;
  2145. * - CPU updates that first part and the token;
  2146. * - HC reads rest of that QH, including token
  2147. * Result: HC gets an inconsistent image, and then
  2148. * DMAs to/from the wrong memory (corrupting it).
  2149. *
  2150. * That should be rare for interrupt transfers,
  2151. * except maybe high bandwidth ...
  2152. */
  2153. /* Tell the caller to start an unlink */
  2154. qh->needs_rescan = 1;
  2155. break;
  2156. /* otherwise, unlink already started */
  2157. }
  2158. }
  2159. return count;
  2160. }
  2161. /* reverse of qh_urb_transaction: free a list of TDs.
  2162. * used for cleanup after errors, before HC sees an URB's TDs.
  2163. */
  2164. static void qtd_list_free(struct fotg210_hcd *fotg210, struct urb *urb,
  2165. struct list_head *head)
  2166. {
  2167. struct fotg210_qtd *qtd, *temp;
  2168. list_for_each_entry_safe(qtd, temp, head, qtd_list) {
  2169. list_del(&qtd->qtd_list);
  2170. fotg210_qtd_free(fotg210, qtd);
  2171. }
  2172. }
  2173. /* create a list of filled qtds for this URB; won't link into qh.
  2174. */
  2175. static struct list_head *qh_urb_transaction(struct fotg210_hcd *fotg210,
  2176. struct urb *urb, struct list_head *head, gfp_t flags)
  2177. {
  2178. struct fotg210_qtd *qtd, *qtd_prev;
  2179. dma_addr_t buf;
  2180. int len, this_sg_len, maxpacket;
  2181. int is_input;
  2182. u32 token;
  2183. int i;
  2184. struct scatterlist *sg;
  2185. /*
  2186. * URBs map to sequences of QTDs: one logical transaction
  2187. */
  2188. qtd = fotg210_qtd_alloc(fotg210, flags);
  2189. if (unlikely(!qtd))
  2190. return NULL;
  2191. list_add_tail(&qtd->qtd_list, head);
  2192. qtd->urb = urb;
  2193. token = QTD_STS_ACTIVE;
  2194. token |= (FOTG210_TUNE_CERR << 10);
  2195. /* for split transactions, SplitXState initialized to zero */
  2196. len = urb->transfer_buffer_length;
  2197. is_input = usb_pipein(urb->pipe);
  2198. if (usb_pipecontrol(urb->pipe)) {
  2199. /* SETUP pid */
  2200. qtd_fill(fotg210, qtd, urb->setup_dma,
  2201. sizeof(struct usb_ctrlrequest),
  2202. token | (2 /* "setup" */ << 8), 8);
  2203. /* ... and always at least one more pid */
  2204. token ^= QTD_TOGGLE;
  2205. qtd_prev = qtd;
  2206. qtd = fotg210_qtd_alloc(fotg210, flags);
  2207. if (unlikely(!qtd))
  2208. goto cleanup;
  2209. qtd->urb = urb;
  2210. qtd_prev->hw_next = QTD_NEXT(fotg210, qtd->qtd_dma);
  2211. list_add_tail(&qtd->qtd_list, head);
  2212. /* for zero length DATA stages, STATUS is always IN */
  2213. if (len == 0)
  2214. token |= (1 /* "in" */ << 8);
  2215. }
  2216. /*
  2217. * data transfer stage: buffer setup
  2218. */
  2219. i = urb->num_mapped_sgs;
  2220. if (len > 0 && i > 0) {
  2221. sg = urb->sg;
  2222. buf = sg_dma_address(sg);
  2223. /* urb->transfer_buffer_length may be smaller than the
  2224. * size of the scatterlist (or vice versa)
  2225. */
  2226. this_sg_len = min_t(int, sg_dma_len(sg), len);
  2227. } else {
  2228. sg = NULL;
  2229. buf = urb->transfer_dma;
  2230. this_sg_len = len;
  2231. }
  2232. if (is_input)
  2233. token |= (1 /* "in" */ << 8);
  2234. /* else it's already initted to "out" pid (0 << 8) */
  2235. maxpacket = usb_maxpacket(urb->dev, urb->pipe);
  2236. /*
  2237. * buffer gets wrapped in one or more qtds;
  2238. * last one may be "short" (including zero len)
  2239. * and may serve as a control status ack
  2240. */
  2241. for (;;) {
  2242. int this_qtd_len;
  2243. this_qtd_len = qtd_fill(fotg210, qtd, buf, this_sg_len, token,
  2244. maxpacket);
  2245. this_sg_len -= this_qtd_len;
  2246. len -= this_qtd_len;
  2247. buf += this_qtd_len;
  2248. /*
  2249. * short reads advance to a "magic" dummy instead of the next
  2250. * qtd ... that forces the queue to stop, for manual cleanup.
  2251. * (this will usually be overridden later.)
  2252. */
  2253. if (is_input)
  2254. qtd->hw_alt_next = fotg210->async->hw->hw_alt_next;
  2255. /* qh makes control packets use qtd toggle; maybe switch it */
  2256. if ((maxpacket & (this_qtd_len + (maxpacket - 1))) == 0)
  2257. token ^= QTD_TOGGLE;
  2258. if (likely(this_sg_len <= 0)) {
  2259. if (--i <= 0 || len <= 0)
  2260. break;
  2261. sg = sg_next(sg);
  2262. buf = sg_dma_address(sg);
  2263. this_sg_len = min_t(int, sg_dma_len(sg), len);
  2264. }
  2265. qtd_prev = qtd;
  2266. qtd = fotg210_qtd_alloc(fotg210, flags);
  2267. if (unlikely(!qtd))
  2268. goto cleanup;
  2269. qtd->urb = urb;
  2270. qtd_prev->hw_next = QTD_NEXT(fotg210, qtd->qtd_dma);
  2271. list_add_tail(&qtd->qtd_list, head);
  2272. }
  2273. /*
  2274. * unless the caller requires manual cleanup after short reads,
  2275. * have the alt_next mechanism keep the queue running after the
  2276. * last data qtd (the only one, for control and most other cases).
  2277. */
  2278. if (likely((urb->transfer_flags & URB_SHORT_NOT_OK) == 0 ||
  2279. usb_pipecontrol(urb->pipe)))
  2280. qtd->hw_alt_next = FOTG210_LIST_END(fotg210);
  2281. /*
  2282. * control requests may need a terminating data "status" ack;
  2283. * other OUT ones may need a terminating short packet
  2284. * (zero length).
  2285. */
  2286. if (likely(urb->transfer_buffer_length != 0)) {
  2287. int one_more = 0;
  2288. if (usb_pipecontrol(urb->pipe)) {
  2289. one_more = 1;
  2290. token ^= 0x0100; /* "in" <--> "out" */
  2291. token |= QTD_TOGGLE; /* force DATA1 */
  2292. } else if (usb_pipeout(urb->pipe)
  2293. && (urb->transfer_flags & URB_ZERO_PACKET)
  2294. && !(urb->transfer_buffer_length % maxpacket)) {
  2295. one_more = 1;
  2296. }
  2297. if (one_more) {
  2298. qtd_prev = qtd;
  2299. qtd = fotg210_qtd_alloc(fotg210, flags);
  2300. if (unlikely(!qtd))
  2301. goto cleanup;
  2302. qtd->urb = urb;
  2303. qtd_prev->hw_next = QTD_NEXT(fotg210, qtd->qtd_dma);
  2304. list_add_tail(&qtd->qtd_list, head);
  2305. /* never any data in such packets */
  2306. qtd_fill(fotg210, qtd, 0, 0, token, 0);
  2307. }
  2308. }
  2309. /* by default, enable interrupt on urb completion */
  2310. if (likely(!(urb->transfer_flags & URB_NO_INTERRUPT)))
  2311. qtd->hw_token |= cpu_to_hc32(fotg210, QTD_IOC);
  2312. return head;
  2313. cleanup:
  2314. qtd_list_free(fotg210, urb, head);
  2315. return NULL;
  2316. }
  2317. /* Would be best to create all qh's from config descriptors,
  2318. * when each interface/altsetting is established. Unlink
  2319. * any previous qh and cancel its urbs first; endpoints are
  2320. * implicitly reset then (data toggle too).
  2321. * That'd mean updating how usbcore talks to HCDs. (2.7?)
  2322. */
  2323. /* Each QH holds a qtd list; a QH is used for everything except iso.
  2324. *
  2325. * For interrupt urbs, the scheduler must set the microframe scheduling
  2326. * mask(s) each time the QH gets scheduled. For highspeed, that's
  2327. * just one microframe in the s-mask. For split interrupt transactions
  2328. * there are additional complications: c-mask, maybe FSTNs.
  2329. */
  2330. static struct fotg210_qh *qh_make(struct fotg210_hcd *fotg210, struct urb *urb,
  2331. gfp_t flags)
  2332. {
  2333. struct fotg210_qh *qh = fotg210_qh_alloc(fotg210, flags);
  2334. struct usb_host_endpoint *ep;
  2335. u32 info1 = 0, info2 = 0;
  2336. int is_input, type;
  2337. int maxp = 0;
  2338. int mult;
  2339. struct usb_tt *tt = urb->dev->tt;
  2340. struct fotg210_qh_hw *hw;
  2341. if (!qh)
  2342. return qh;
  2343. /*
  2344. * init endpoint/device data for this QH
  2345. */
  2346. info1 |= usb_pipeendpoint(urb->pipe) << 8;
  2347. info1 |= usb_pipedevice(urb->pipe) << 0;
  2348. is_input = usb_pipein(urb->pipe);
  2349. type = usb_pipetype(urb->pipe);
  2350. ep = usb_pipe_endpoint(urb->dev, urb->pipe);
  2351. maxp = usb_endpoint_maxp(&ep->desc);
  2352. mult = usb_endpoint_maxp_mult(&ep->desc);
  2353. /* 1024 byte maxpacket is a hardware ceiling. High bandwidth
  2354. * acts like up to 3KB, but is built from smaller packets.
  2355. */
  2356. if (maxp > 1024) {
  2357. fotg210_dbg(fotg210, "bogus qh maxpacket %d\n", maxp);
  2358. goto done;
  2359. }
  2360. /* Compute interrupt scheduling parameters just once, and save.
  2361. * - allowing for high bandwidth, how many nsec/uframe are used?
  2362. * - split transactions need a second CSPLIT uframe; same question
  2363. * - splits also need a schedule gap (for full/low speed I/O)
  2364. * - qh has a polling interval
  2365. *
  2366. * For control/bulk requests, the HC or TT handles these.
  2367. */
  2368. if (type == PIPE_INTERRUPT) {
  2369. qh->usecs = NS_TO_US(usb_calc_bus_time(USB_SPEED_HIGH,
  2370. is_input, 0, mult * maxp));
  2371. qh->start = NO_FRAME;
  2372. if (urb->dev->speed == USB_SPEED_HIGH) {
  2373. qh->c_usecs = 0;
  2374. qh->gap_uf = 0;
  2375. qh->period = urb->interval >> 3;
  2376. if (qh->period == 0 && urb->interval != 1) {
  2377. /* NOTE interval 2 or 4 uframes could work.
  2378. * But interval 1 scheduling is simpler, and
  2379. * includes high bandwidth.
  2380. */
  2381. urb->interval = 1;
  2382. } else if (qh->period > fotg210->periodic_size) {
  2383. qh->period = fotg210->periodic_size;
  2384. urb->interval = qh->period << 3;
  2385. }
  2386. } else {
  2387. int think_time;
  2388. /* gap is f(FS/LS transfer times) */
  2389. qh->gap_uf = 1 + usb_calc_bus_time(urb->dev->speed,
  2390. is_input, 0, maxp) / (125 * 1000);
  2391. /* FIXME this just approximates SPLIT/CSPLIT times */
  2392. if (is_input) { /* SPLIT, gap, CSPLIT+DATA */
  2393. qh->c_usecs = qh->usecs + HS_USECS(0);
  2394. qh->usecs = HS_USECS(1);
  2395. } else { /* SPLIT+DATA, gap, CSPLIT */
  2396. qh->usecs += HS_USECS(1);
  2397. qh->c_usecs = HS_USECS(0);
  2398. }
  2399. think_time = tt ? tt->think_time : 0;
  2400. qh->tt_usecs = NS_TO_US(think_time +
  2401. usb_calc_bus_time(urb->dev->speed,
  2402. is_input, 0, maxp));
  2403. qh->period = urb->interval;
  2404. if (qh->period > fotg210->periodic_size) {
  2405. qh->period = fotg210->periodic_size;
  2406. urb->interval = qh->period;
  2407. }
  2408. }
  2409. }
  2410. /* support for tt scheduling, and access to toggles */
  2411. qh->dev = urb->dev;
  2412. /* using TT? */
  2413. switch (urb->dev->speed) {
  2414. case USB_SPEED_LOW:
  2415. info1 |= QH_LOW_SPEED;
  2416. fallthrough;
  2417. case USB_SPEED_FULL:
  2418. /* EPS 0 means "full" */
  2419. if (type != PIPE_INTERRUPT)
  2420. info1 |= (FOTG210_TUNE_RL_TT << 28);
  2421. if (type == PIPE_CONTROL) {
  2422. info1 |= QH_CONTROL_EP; /* for TT */
  2423. info1 |= QH_TOGGLE_CTL; /* toggle from qtd */
  2424. }
  2425. info1 |= maxp << 16;
  2426. info2 |= (FOTG210_TUNE_MULT_TT << 30);
  2427. /* Some Freescale processors have an erratum in which the
  2428. * port number in the queue head was 0..N-1 instead of 1..N.
  2429. */
  2430. if (fotg210_has_fsl_portno_bug(fotg210))
  2431. info2 |= (urb->dev->ttport-1) << 23;
  2432. else
  2433. info2 |= urb->dev->ttport << 23;
  2434. /* set the address of the TT; for TDI's integrated
  2435. * root hub tt, leave it zeroed.
  2436. */
  2437. if (tt && tt->hub != fotg210_to_hcd(fotg210)->self.root_hub)
  2438. info2 |= tt->hub->devnum << 16;
  2439. /* NOTE: if (PIPE_INTERRUPT) { scheduler sets c-mask } */
  2440. break;
  2441. case USB_SPEED_HIGH: /* no TT involved */
  2442. info1 |= QH_HIGH_SPEED;
  2443. if (type == PIPE_CONTROL) {
  2444. info1 |= (FOTG210_TUNE_RL_HS << 28);
  2445. info1 |= 64 << 16; /* usb2 fixed maxpacket */
  2446. info1 |= QH_TOGGLE_CTL; /* toggle from qtd */
  2447. info2 |= (FOTG210_TUNE_MULT_HS << 30);
  2448. } else if (type == PIPE_BULK) {
  2449. info1 |= (FOTG210_TUNE_RL_HS << 28);
  2450. /* The USB spec says that high speed bulk endpoints
  2451. * always use 512 byte maxpacket. But some device
  2452. * vendors decided to ignore that, and MSFT is happy
  2453. * to help them do so. So now people expect to use
  2454. * such nonconformant devices with Linux too; sigh.
  2455. */
  2456. info1 |= maxp << 16;
  2457. info2 |= (FOTG210_TUNE_MULT_HS << 30);
  2458. } else { /* PIPE_INTERRUPT */
  2459. info1 |= maxp << 16;
  2460. info2 |= mult << 30;
  2461. }
  2462. break;
  2463. default:
  2464. fotg210_dbg(fotg210, "bogus dev %p speed %d\n", urb->dev,
  2465. urb->dev->speed);
  2466. done:
  2467. qh_destroy(fotg210, qh);
  2468. return NULL;
  2469. }
  2470. /* NOTE: if (PIPE_INTERRUPT) { scheduler sets s-mask } */
  2471. /* init as live, toggle clear, advance to dummy */
  2472. qh->qh_state = QH_STATE_IDLE;
  2473. hw = qh->hw;
  2474. hw->hw_info1 = cpu_to_hc32(fotg210, info1);
  2475. hw->hw_info2 = cpu_to_hc32(fotg210, info2);
  2476. qh->is_out = !is_input;
  2477. usb_settoggle(urb->dev, usb_pipeendpoint(urb->pipe), !is_input, 1);
  2478. qh_refresh(fotg210, qh);
  2479. return qh;
  2480. }
  2481. static void enable_async(struct fotg210_hcd *fotg210)
  2482. {
  2483. if (fotg210->async_count++)
  2484. return;
  2485. /* Stop waiting to turn off the async schedule */
  2486. fotg210->enabled_hrtimer_events &= ~BIT(FOTG210_HRTIMER_DISABLE_ASYNC);
  2487. /* Don't start the schedule until ASS is 0 */
  2488. fotg210_poll_ASS(fotg210);
  2489. turn_on_io_watchdog(fotg210);
  2490. }
  2491. static void disable_async(struct fotg210_hcd *fotg210)
  2492. {
  2493. if (--fotg210->async_count)
  2494. return;
  2495. /* The async schedule and async_unlink list are supposed to be empty */
  2496. WARN_ON(fotg210->async->qh_next.qh || fotg210->async_unlink);
  2497. /* Don't turn off the schedule until ASS is 1 */
  2498. fotg210_poll_ASS(fotg210);
  2499. }
  2500. /* move qh (and its qtds) onto async queue; maybe enable queue. */
  2501. static void qh_link_async(struct fotg210_hcd *fotg210, struct fotg210_qh *qh)
  2502. {
  2503. __hc32 dma = QH_NEXT(fotg210, qh->qh_dma);
  2504. struct fotg210_qh *head;
  2505. /* Don't link a QH if there's a Clear-TT-Buffer pending */
  2506. if (unlikely(qh->clearing_tt))
  2507. return;
  2508. WARN_ON(qh->qh_state != QH_STATE_IDLE);
  2509. /* clear halt and/or toggle; and maybe recover from silicon quirk */
  2510. qh_refresh(fotg210, qh);
  2511. /* splice right after start */
  2512. head = fotg210->async;
  2513. qh->qh_next = head->qh_next;
  2514. qh->hw->hw_next = head->hw->hw_next;
  2515. wmb();
  2516. head->qh_next.qh = qh;
  2517. head->hw->hw_next = dma;
  2518. qh->xacterrs = 0;
  2519. qh->qh_state = QH_STATE_LINKED;
  2520. /* qtd completions reported later by interrupt */
  2521. enable_async(fotg210);
  2522. }
  2523. /* For control/bulk/interrupt, return QH with these TDs appended.
  2524. * Allocates and initializes the QH if necessary.
  2525. * Returns null if it can't allocate a QH it needs to.
  2526. * If the QH has TDs (urbs) already, that's great.
  2527. */
  2528. static struct fotg210_qh *qh_append_tds(struct fotg210_hcd *fotg210,
  2529. struct urb *urb, struct list_head *qtd_list,
  2530. int epnum, void **ptr)
  2531. {
  2532. struct fotg210_qh *qh = NULL;
  2533. __hc32 qh_addr_mask = cpu_to_hc32(fotg210, 0x7f);
  2534. qh = (struct fotg210_qh *) *ptr;
  2535. if (unlikely(qh == NULL)) {
  2536. /* can't sleep here, we have fotg210->lock... */
  2537. qh = qh_make(fotg210, urb, GFP_ATOMIC);
  2538. *ptr = qh;
  2539. }
  2540. if (likely(qh != NULL)) {
  2541. struct fotg210_qtd *qtd;
  2542. if (unlikely(list_empty(qtd_list)))
  2543. qtd = NULL;
  2544. else
  2545. qtd = list_entry(qtd_list->next, struct fotg210_qtd,
  2546. qtd_list);
  2547. /* control qh may need patching ... */
  2548. if (unlikely(epnum == 0)) {
  2549. /* usb_reset_device() briefly reverts to address 0 */
  2550. if (usb_pipedevice(urb->pipe) == 0)
  2551. qh->hw->hw_info1 &= ~qh_addr_mask;
  2552. }
  2553. /* just one way to queue requests: swap with the dummy qtd.
  2554. * only hc or qh_refresh() ever modify the overlay.
  2555. */
  2556. if (likely(qtd != NULL)) {
  2557. struct fotg210_qtd *dummy;
  2558. dma_addr_t dma;
  2559. __hc32 token;
  2560. /* to avoid racing the HC, use the dummy td instead of
  2561. * the first td of our list (becomes new dummy). both
  2562. * tds stay deactivated until we're done, when the
  2563. * HC is allowed to fetch the old dummy (4.10.2).
  2564. */
  2565. token = qtd->hw_token;
  2566. qtd->hw_token = HALT_BIT(fotg210);
  2567. dummy = qh->dummy;
  2568. dma = dummy->qtd_dma;
  2569. *dummy = *qtd;
  2570. dummy->qtd_dma = dma;
  2571. list_del(&qtd->qtd_list);
  2572. list_add(&dummy->qtd_list, qtd_list);
  2573. list_splice_tail(qtd_list, &qh->qtd_list);
  2574. fotg210_qtd_init(fotg210, qtd, qtd->qtd_dma);
  2575. qh->dummy = qtd;
  2576. /* hc must see the new dummy at list end */
  2577. dma = qtd->qtd_dma;
  2578. qtd = list_entry(qh->qtd_list.prev,
  2579. struct fotg210_qtd, qtd_list);
  2580. qtd->hw_next = QTD_NEXT(fotg210, dma);
  2581. /* let the hc process these next qtds */
  2582. wmb();
  2583. dummy->hw_token = token;
  2584. urb->hcpriv = qh;
  2585. }
  2586. }
  2587. return qh;
  2588. }
  2589. static int submit_async(struct fotg210_hcd *fotg210, struct urb *urb,
  2590. struct list_head *qtd_list, gfp_t mem_flags)
  2591. {
  2592. int epnum;
  2593. unsigned long flags;
  2594. struct fotg210_qh *qh = NULL;
  2595. int rc;
  2596. epnum = urb->ep->desc.bEndpointAddress;
  2597. #ifdef FOTG210_URB_TRACE
  2598. {
  2599. struct fotg210_qtd *qtd;
  2600. qtd = list_entry(qtd_list->next, struct fotg210_qtd, qtd_list);
  2601. fotg210_dbg(fotg210,
  2602. "%s %s urb %p ep%d%s len %d, qtd %p [qh %p]\n",
  2603. __func__, urb->dev->devpath, urb,
  2604. epnum & 0x0f, (epnum & USB_DIR_IN)
  2605. ? "in" : "out",
  2606. urb->transfer_buffer_length,
  2607. qtd, urb->ep->hcpriv);
  2608. }
  2609. #endif
  2610. spin_lock_irqsave(&fotg210->lock, flags);
  2611. if (unlikely(!HCD_HW_ACCESSIBLE(fotg210_to_hcd(fotg210)))) {
  2612. rc = -ESHUTDOWN;
  2613. goto done;
  2614. }
  2615. rc = usb_hcd_link_urb_to_ep(fotg210_to_hcd(fotg210), urb);
  2616. if (unlikely(rc))
  2617. goto done;
  2618. qh = qh_append_tds(fotg210, urb, qtd_list, epnum, &urb->ep->hcpriv);
  2619. if (unlikely(qh == NULL)) {
  2620. usb_hcd_unlink_urb_from_ep(fotg210_to_hcd(fotg210), urb);
  2621. rc = -ENOMEM;
  2622. goto done;
  2623. }
  2624. /* Control/bulk operations through TTs don't need scheduling,
  2625. * the HC and TT handle it when the TT has a buffer ready.
  2626. */
  2627. if (likely(qh->qh_state == QH_STATE_IDLE))
  2628. qh_link_async(fotg210, qh);
  2629. done:
  2630. spin_unlock_irqrestore(&fotg210->lock, flags);
  2631. if (unlikely(qh == NULL))
  2632. qtd_list_free(fotg210, urb, qtd_list);
  2633. return rc;
  2634. }
  2635. static void single_unlink_async(struct fotg210_hcd *fotg210,
  2636. struct fotg210_qh *qh)
  2637. {
  2638. struct fotg210_qh *prev;
  2639. /* Add to the end of the list of QHs waiting for the next IAAD */
  2640. qh->qh_state = QH_STATE_UNLINK;
  2641. if (fotg210->async_unlink)
  2642. fotg210->async_unlink_last->unlink_next = qh;
  2643. else
  2644. fotg210->async_unlink = qh;
  2645. fotg210->async_unlink_last = qh;
  2646. /* Unlink it from the schedule */
  2647. prev = fotg210->async;
  2648. while (prev->qh_next.qh != qh)
  2649. prev = prev->qh_next.qh;
  2650. prev->hw->hw_next = qh->hw->hw_next;
  2651. prev->qh_next = qh->qh_next;
  2652. if (fotg210->qh_scan_next == qh)
  2653. fotg210->qh_scan_next = qh->qh_next.qh;
  2654. }
  2655. static void start_iaa_cycle(struct fotg210_hcd *fotg210, bool nested)
  2656. {
  2657. /*
  2658. * Do nothing if an IAA cycle is already running or
  2659. * if one will be started shortly.
  2660. */
  2661. if (fotg210->async_iaa || fotg210->async_unlinking)
  2662. return;
  2663. /* Do all the waiting QHs at once */
  2664. fotg210->async_iaa = fotg210->async_unlink;
  2665. fotg210->async_unlink = NULL;
  2666. /* If the controller isn't running, we don't have to wait for it */
  2667. if (unlikely(fotg210->rh_state < FOTG210_RH_RUNNING)) {
  2668. if (!nested) /* Avoid recursion */
  2669. end_unlink_async(fotg210);
  2670. /* Otherwise start a new IAA cycle */
  2671. } else if (likely(fotg210->rh_state == FOTG210_RH_RUNNING)) {
  2672. /* Make sure the unlinks are all visible to the hardware */
  2673. wmb();
  2674. fotg210_writel(fotg210, fotg210->command | CMD_IAAD,
  2675. &fotg210->regs->command);
  2676. fotg210_readl(fotg210, &fotg210->regs->command);
  2677. fotg210_enable_event(fotg210, FOTG210_HRTIMER_IAA_WATCHDOG,
  2678. true);
  2679. }
  2680. }
  2681. /* the async qh for the qtds being unlinked are now gone from the HC */
  2682. static void end_unlink_async(struct fotg210_hcd *fotg210)
  2683. {
  2684. struct fotg210_qh *qh;
  2685. /* Process the idle QHs */
  2686. restart:
  2687. fotg210->async_unlinking = true;
  2688. while (fotg210->async_iaa) {
  2689. qh = fotg210->async_iaa;
  2690. fotg210->async_iaa = qh->unlink_next;
  2691. qh->unlink_next = NULL;
  2692. qh->qh_state = QH_STATE_IDLE;
  2693. qh->qh_next.qh = NULL;
  2694. qh_completions(fotg210, qh);
  2695. if (!list_empty(&qh->qtd_list) &&
  2696. fotg210->rh_state == FOTG210_RH_RUNNING)
  2697. qh_link_async(fotg210, qh);
  2698. disable_async(fotg210);
  2699. }
  2700. fotg210->async_unlinking = false;
  2701. /* Start a new IAA cycle if any QHs are waiting for it */
  2702. if (fotg210->async_unlink) {
  2703. start_iaa_cycle(fotg210, true);
  2704. if (unlikely(fotg210->rh_state < FOTG210_RH_RUNNING))
  2705. goto restart;
  2706. }
  2707. }
  2708. static void unlink_empty_async(struct fotg210_hcd *fotg210)
  2709. {
  2710. struct fotg210_qh *qh, *next;
  2711. bool stopped = (fotg210->rh_state < FOTG210_RH_RUNNING);
  2712. bool check_unlinks_later = false;
  2713. /* Unlink all the async QHs that have been empty for a timer cycle */
  2714. next = fotg210->async->qh_next.qh;
  2715. while (next) {
  2716. qh = next;
  2717. next = qh->qh_next.qh;
  2718. if (list_empty(&qh->qtd_list) &&
  2719. qh->qh_state == QH_STATE_LINKED) {
  2720. if (!stopped && qh->unlink_cycle ==
  2721. fotg210->async_unlink_cycle)
  2722. check_unlinks_later = true;
  2723. else
  2724. single_unlink_async(fotg210, qh);
  2725. }
  2726. }
  2727. /* Start a new IAA cycle if any QHs are waiting for it */
  2728. if (fotg210->async_unlink)
  2729. start_iaa_cycle(fotg210, false);
  2730. /* QHs that haven't been empty for long enough will be handled later */
  2731. if (check_unlinks_later) {
  2732. fotg210_enable_event(fotg210, FOTG210_HRTIMER_ASYNC_UNLINKS,
  2733. true);
  2734. ++fotg210->async_unlink_cycle;
  2735. }
  2736. }
  2737. /* makes sure the async qh will become idle */
  2738. /* caller must own fotg210->lock */
  2739. static void start_unlink_async(struct fotg210_hcd *fotg210,
  2740. struct fotg210_qh *qh)
  2741. {
  2742. /*
  2743. * If the QH isn't linked then there's nothing we can do
  2744. * unless we were called during a giveback, in which case
  2745. * qh_completions() has to deal with it.
  2746. */
  2747. if (qh->qh_state != QH_STATE_LINKED) {
  2748. if (qh->qh_state == QH_STATE_COMPLETING)
  2749. qh->needs_rescan = 1;
  2750. return;
  2751. }
  2752. single_unlink_async(fotg210, qh);
  2753. start_iaa_cycle(fotg210, false);
  2754. }
  2755. static void scan_async(struct fotg210_hcd *fotg210)
  2756. {
  2757. struct fotg210_qh *qh;
  2758. bool check_unlinks_later = false;
  2759. fotg210->qh_scan_next = fotg210->async->qh_next.qh;
  2760. while (fotg210->qh_scan_next) {
  2761. qh = fotg210->qh_scan_next;
  2762. fotg210->qh_scan_next = qh->qh_next.qh;
  2763. rescan:
  2764. /* clean any finished work for this qh */
  2765. if (!list_empty(&qh->qtd_list)) {
  2766. int temp;
  2767. /*
  2768. * Unlinks could happen here; completion reporting
  2769. * drops the lock. That's why fotg210->qh_scan_next
  2770. * always holds the next qh to scan; if the next qh
  2771. * gets unlinked then fotg210->qh_scan_next is adjusted
  2772. * in single_unlink_async().
  2773. */
  2774. temp = qh_completions(fotg210, qh);
  2775. if (qh->needs_rescan) {
  2776. start_unlink_async(fotg210, qh);
  2777. } else if (list_empty(&qh->qtd_list)
  2778. && qh->qh_state == QH_STATE_LINKED) {
  2779. qh->unlink_cycle = fotg210->async_unlink_cycle;
  2780. check_unlinks_later = true;
  2781. } else if (temp != 0)
  2782. goto rescan;
  2783. }
  2784. }
  2785. /*
  2786. * Unlink empty entries, reducing DMA usage as well
  2787. * as HCD schedule-scanning costs. Delay for any qh
  2788. * we just scanned, there's a not-unusual case that it
  2789. * doesn't stay idle for long.
  2790. */
  2791. if (check_unlinks_later && fotg210->rh_state == FOTG210_RH_RUNNING &&
  2792. !(fotg210->enabled_hrtimer_events &
  2793. BIT(FOTG210_HRTIMER_ASYNC_UNLINKS))) {
  2794. fotg210_enable_event(fotg210,
  2795. FOTG210_HRTIMER_ASYNC_UNLINKS, true);
  2796. ++fotg210->async_unlink_cycle;
  2797. }
  2798. }
  2799. /* EHCI scheduled transaction support: interrupt, iso, split iso
  2800. * These are called "periodic" transactions in the EHCI spec.
  2801. *
  2802. * Note that for interrupt transfers, the QH/QTD manipulation is shared
  2803. * with the "asynchronous" transaction support (control/bulk transfers).
  2804. * The only real difference is in how interrupt transfers are scheduled.
  2805. *
  2806. * For ISO, we make an "iso_stream" head to serve the same role as a QH.
  2807. * It keeps track of every ITD (or SITD) that's linked, and holds enough
  2808. * pre-calculated schedule data to make appending to the queue be quick.
  2809. */
  2810. static int fotg210_get_frame(struct usb_hcd *hcd);
  2811. /* periodic_next_shadow - return "next" pointer on shadow list
  2812. * @periodic: host pointer to qh/itd
  2813. * @tag: hardware tag for type of this record
  2814. */
  2815. static union fotg210_shadow *periodic_next_shadow(struct fotg210_hcd *fotg210,
  2816. union fotg210_shadow *periodic, __hc32 tag)
  2817. {
  2818. switch (hc32_to_cpu(fotg210, tag)) {
  2819. case Q_TYPE_QH:
  2820. return &periodic->qh->qh_next;
  2821. case Q_TYPE_FSTN:
  2822. return &periodic->fstn->fstn_next;
  2823. default:
  2824. return &periodic->itd->itd_next;
  2825. }
  2826. }
  2827. static __hc32 *shadow_next_periodic(struct fotg210_hcd *fotg210,
  2828. union fotg210_shadow *periodic, __hc32 tag)
  2829. {
  2830. switch (hc32_to_cpu(fotg210, tag)) {
  2831. /* our fotg210_shadow.qh is actually software part */
  2832. case Q_TYPE_QH:
  2833. return &periodic->qh->hw->hw_next;
  2834. /* others are hw parts */
  2835. default:
  2836. return periodic->hw_next;
  2837. }
  2838. }
  2839. /* caller must hold fotg210->lock */
  2840. static void periodic_unlink(struct fotg210_hcd *fotg210, unsigned frame,
  2841. void *ptr)
  2842. {
  2843. union fotg210_shadow *prev_p = &fotg210->pshadow[frame];
  2844. __hc32 *hw_p = &fotg210->periodic[frame];
  2845. union fotg210_shadow here = *prev_p;
  2846. /* find predecessor of "ptr"; hw and shadow lists are in sync */
  2847. while (here.ptr && here.ptr != ptr) {
  2848. prev_p = periodic_next_shadow(fotg210, prev_p,
  2849. Q_NEXT_TYPE(fotg210, *hw_p));
  2850. hw_p = shadow_next_periodic(fotg210, &here,
  2851. Q_NEXT_TYPE(fotg210, *hw_p));
  2852. here = *prev_p;
  2853. }
  2854. /* an interrupt entry (at list end) could have been shared */
  2855. if (!here.ptr)
  2856. return;
  2857. /* update shadow and hardware lists ... the old "next" pointers
  2858. * from ptr may still be in use, the caller updates them.
  2859. */
  2860. *prev_p = *periodic_next_shadow(fotg210, &here,
  2861. Q_NEXT_TYPE(fotg210, *hw_p));
  2862. *hw_p = *shadow_next_periodic(fotg210, &here,
  2863. Q_NEXT_TYPE(fotg210, *hw_p));
  2864. }
  2865. /* how many of the uframe's 125 usecs are allocated? */
  2866. static unsigned short periodic_usecs(struct fotg210_hcd *fotg210,
  2867. unsigned frame, unsigned uframe)
  2868. {
  2869. __hc32 *hw_p = &fotg210->periodic[frame];
  2870. union fotg210_shadow *q = &fotg210->pshadow[frame];
  2871. unsigned usecs = 0;
  2872. struct fotg210_qh_hw *hw;
  2873. while (q->ptr) {
  2874. switch (hc32_to_cpu(fotg210, Q_NEXT_TYPE(fotg210, *hw_p))) {
  2875. case Q_TYPE_QH:
  2876. hw = q->qh->hw;
  2877. /* is it in the S-mask? */
  2878. if (hw->hw_info2 & cpu_to_hc32(fotg210, 1 << uframe))
  2879. usecs += q->qh->usecs;
  2880. /* ... or C-mask? */
  2881. if (hw->hw_info2 & cpu_to_hc32(fotg210,
  2882. 1 << (8 + uframe)))
  2883. usecs += q->qh->c_usecs;
  2884. hw_p = &hw->hw_next;
  2885. q = &q->qh->qh_next;
  2886. break;
  2887. /* case Q_TYPE_FSTN: */
  2888. default:
  2889. /* for "save place" FSTNs, count the relevant INTR
  2890. * bandwidth from the previous frame
  2891. */
  2892. if (q->fstn->hw_prev != FOTG210_LIST_END(fotg210))
  2893. fotg210_dbg(fotg210, "ignoring FSTN cost ...\n");
  2894. hw_p = &q->fstn->hw_next;
  2895. q = &q->fstn->fstn_next;
  2896. break;
  2897. case Q_TYPE_ITD:
  2898. if (q->itd->hw_transaction[uframe])
  2899. usecs += q->itd->stream->usecs;
  2900. hw_p = &q->itd->hw_next;
  2901. q = &q->itd->itd_next;
  2902. break;
  2903. }
  2904. }
  2905. if (usecs > fotg210->uframe_periodic_max)
  2906. fotg210_err(fotg210, "uframe %d sched overrun: %d usecs\n",
  2907. frame * 8 + uframe, usecs);
  2908. return usecs;
  2909. }
  2910. static int same_tt(struct usb_device *dev1, struct usb_device *dev2)
  2911. {
  2912. if (!dev1->tt || !dev2->tt)
  2913. return 0;
  2914. if (dev1->tt != dev2->tt)
  2915. return 0;
  2916. if (dev1->tt->multi)
  2917. return dev1->ttport == dev2->ttport;
  2918. else
  2919. return 1;
  2920. }
  2921. /* return true iff the device's transaction translator is available
  2922. * for a periodic transfer starting at the specified frame, using
  2923. * all the uframes in the mask.
  2924. */
  2925. static int tt_no_collision(struct fotg210_hcd *fotg210, unsigned period,
  2926. struct usb_device *dev, unsigned frame, u32 uf_mask)
  2927. {
  2928. if (period == 0) /* error */
  2929. return 0;
  2930. /* note bandwidth wastage: split never follows csplit
  2931. * (different dev or endpoint) until the next uframe.
  2932. * calling convention doesn't make that distinction.
  2933. */
  2934. for (; frame < fotg210->periodic_size; frame += period) {
  2935. union fotg210_shadow here;
  2936. __hc32 type;
  2937. struct fotg210_qh_hw *hw;
  2938. here = fotg210->pshadow[frame];
  2939. type = Q_NEXT_TYPE(fotg210, fotg210->periodic[frame]);
  2940. while (here.ptr) {
  2941. switch (hc32_to_cpu(fotg210, type)) {
  2942. case Q_TYPE_ITD:
  2943. type = Q_NEXT_TYPE(fotg210, here.itd->hw_next);
  2944. here = here.itd->itd_next;
  2945. continue;
  2946. case Q_TYPE_QH:
  2947. hw = here.qh->hw;
  2948. if (same_tt(dev, here.qh->dev)) {
  2949. u32 mask;
  2950. mask = hc32_to_cpu(fotg210,
  2951. hw->hw_info2);
  2952. /* "knows" no gap is needed */
  2953. mask |= mask >> 8;
  2954. if (mask & uf_mask)
  2955. break;
  2956. }
  2957. type = Q_NEXT_TYPE(fotg210, hw->hw_next);
  2958. here = here.qh->qh_next;
  2959. continue;
  2960. /* case Q_TYPE_FSTN: */
  2961. default:
  2962. fotg210_dbg(fotg210,
  2963. "periodic frame %d bogus type %d\n",
  2964. frame, type);
  2965. }
  2966. /* collision or error */
  2967. return 0;
  2968. }
  2969. }
  2970. /* no collision */
  2971. return 1;
  2972. }
  2973. static void enable_periodic(struct fotg210_hcd *fotg210)
  2974. {
  2975. if (fotg210->periodic_count++)
  2976. return;
  2977. /* Stop waiting to turn off the periodic schedule */
  2978. fotg210->enabled_hrtimer_events &=
  2979. ~BIT(FOTG210_HRTIMER_DISABLE_PERIODIC);
  2980. /* Don't start the schedule until PSS is 0 */
  2981. fotg210_poll_PSS(fotg210);
  2982. turn_on_io_watchdog(fotg210);
  2983. }
  2984. static void disable_periodic(struct fotg210_hcd *fotg210)
  2985. {
  2986. if (--fotg210->periodic_count)
  2987. return;
  2988. /* Don't turn off the schedule until PSS is 1 */
  2989. fotg210_poll_PSS(fotg210);
  2990. }
  2991. /* periodic schedule slots have iso tds (normal or split) first, then a
  2992. * sparse tree for active interrupt transfers.
  2993. *
  2994. * this just links in a qh; caller guarantees uframe masks are set right.
  2995. * no FSTN support (yet; fotg210 0.96+)
  2996. */
  2997. static void qh_link_periodic(struct fotg210_hcd *fotg210, struct fotg210_qh *qh)
  2998. {
  2999. unsigned i;
  3000. unsigned period = qh->period;
  3001. dev_dbg(&qh->dev->dev,
  3002. "link qh%d-%04x/%p start %d [%d/%d us]\n", period,
  3003. hc32_to_cpup(fotg210, &qh->hw->hw_info2) &
  3004. (QH_CMASK | QH_SMASK), qh, qh->start, qh->usecs,
  3005. qh->c_usecs);
  3006. /* high bandwidth, or otherwise every microframe */
  3007. if (period == 0)
  3008. period = 1;
  3009. for (i = qh->start; i < fotg210->periodic_size; i += period) {
  3010. union fotg210_shadow *prev = &fotg210->pshadow[i];
  3011. __hc32 *hw_p = &fotg210->periodic[i];
  3012. union fotg210_shadow here = *prev;
  3013. __hc32 type = 0;
  3014. /* skip the iso nodes at list head */
  3015. while (here.ptr) {
  3016. type = Q_NEXT_TYPE(fotg210, *hw_p);
  3017. if (type == cpu_to_hc32(fotg210, Q_TYPE_QH))
  3018. break;
  3019. prev = periodic_next_shadow(fotg210, prev, type);
  3020. hw_p = shadow_next_periodic(fotg210, &here, type);
  3021. here = *prev;
  3022. }
  3023. /* sorting each branch by period (slow-->fast)
  3024. * enables sharing interior tree nodes
  3025. */
  3026. while (here.ptr && qh != here.qh) {
  3027. if (qh->period > here.qh->period)
  3028. break;
  3029. prev = &here.qh->qh_next;
  3030. hw_p = &here.qh->hw->hw_next;
  3031. here = *prev;
  3032. }
  3033. /* link in this qh, unless some earlier pass did that */
  3034. if (qh != here.qh) {
  3035. qh->qh_next = here;
  3036. if (here.qh)
  3037. qh->hw->hw_next = *hw_p;
  3038. wmb();
  3039. prev->qh = qh;
  3040. *hw_p = QH_NEXT(fotg210, qh->qh_dma);
  3041. }
  3042. }
  3043. qh->qh_state = QH_STATE_LINKED;
  3044. qh->xacterrs = 0;
  3045. /* update per-qh bandwidth for usbfs */
  3046. fotg210_to_hcd(fotg210)->self.bandwidth_allocated += qh->period
  3047. ? ((qh->usecs + qh->c_usecs) / qh->period)
  3048. : (qh->usecs * 8);
  3049. list_add(&qh->intr_node, &fotg210->intr_qh_list);
  3050. /* maybe enable periodic schedule processing */
  3051. ++fotg210->intr_count;
  3052. enable_periodic(fotg210);
  3053. }
  3054. static void qh_unlink_periodic(struct fotg210_hcd *fotg210,
  3055. struct fotg210_qh *qh)
  3056. {
  3057. unsigned i;
  3058. unsigned period;
  3059. /*
  3060. * If qh is for a low/full-speed device, simply unlinking it
  3061. * could interfere with an ongoing split transaction. To unlink
  3062. * it safely would require setting the QH_INACTIVATE bit and
  3063. * waiting at least one frame, as described in EHCI 4.12.2.5.
  3064. *
  3065. * We won't bother with any of this. Instead, we assume that the
  3066. * only reason for unlinking an interrupt QH while the current URB
  3067. * is still active is to dequeue all the URBs (flush the whole
  3068. * endpoint queue).
  3069. *
  3070. * If rebalancing the periodic schedule is ever implemented, this
  3071. * approach will no longer be valid.
  3072. */
  3073. /* high bandwidth, or otherwise part of every microframe */
  3074. period = qh->period;
  3075. if (!period)
  3076. period = 1;
  3077. for (i = qh->start; i < fotg210->periodic_size; i += period)
  3078. periodic_unlink(fotg210, i, qh);
  3079. /* update per-qh bandwidth for usbfs */
  3080. fotg210_to_hcd(fotg210)->self.bandwidth_allocated -= qh->period
  3081. ? ((qh->usecs + qh->c_usecs) / qh->period)
  3082. : (qh->usecs * 8);
  3083. dev_dbg(&qh->dev->dev,
  3084. "unlink qh%d-%04x/%p start %d [%d/%d us]\n",
  3085. qh->period, hc32_to_cpup(fotg210, &qh->hw->hw_info2) &
  3086. (QH_CMASK | QH_SMASK), qh, qh->start, qh->usecs,
  3087. qh->c_usecs);
  3088. /* qh->qh_next still "live" to HC */
  3089. qh->qh_state = QH_STATE_UNLINK;
  3090. qh->qh_next.ptr = NULL;
  3091. if (fotg210->qh_scan_next == qh)
  3092. fotg210->qh_scan_next = list_entry(qh->intr_node.next,
  3093. struct fotg210_qh, intr_node);
  3094. list_del(&qh->intr_node);
  3095. }
  3096. static void start_unlink_intr(struct fotg210_hcd *fotg210,
  3097. struct fotg210_qh *qh)
  3098. {
  3099. /* If the QH isn't linked then there's nothing we can do
  3100. * unless we were called during a giveback, in which case
  3101. * qh_completions() has to deal with it.
  3102. */
  3103. if (qh->qh_state != QH_STATE_LINKED) {
  3104. if (qh->qh_state == QH_STATE_COMPLETING)
  3105. qh->needs_rescan = 1;
  3106. return;
  3107. }
  3108. qh_unlink_periodic(fotg210, qh);
  3109. /* Make sure the unlinks are visible before starting the timer */
  3110. wmb();
  3111. /*
  3112. * The EHCI spec doesn't say how long it takes the controller to
  3113. * stop accessing an unlinked interrupt QH. The timer delay is
  3114. * 9 uframes; presumably that will be long enough.
  3115. */
  3116. qh->unlink_cycle = fotg210->intr_unlink_cycle;
  3117. /* New entries go at the end of the intr_unlink list */
  3118. if (fotg210->intr_unlink)
  3119. fotg210->intr_unlink_last->unlink_next = qh;
  3120. else
  3121. fotg210->intr_unlink = qh;
  3122. fotg210->intr_unlink_last = qh;
  3123. if (fotg210->intr_unlinking)
  3124. ; /* Avoid recursive calls */
  3125. else if (fotg210->rh_state < FOTG210_RH_RUNNING)
  3126. fotg210_handle_intr_unlinks(fotg210);
  3127. else if (fotg210->intr_unlink == qh) {
  3128. fotg210_enable_event(fotg210, FOTG210_HRTIMER_UNLINK_INTR,
  3129. true);
  3130. ++fotg210->intr_unlink_cycle;
  3131. }
  3132. }
  3133. static void end_unlink_intr(struct fotg210_hcd *fotg210, struct fotg210_qh *qh)
  3134. {
  3135. struct fotg210_qh_hw *hw = qh->hw;
  3136. int rc;
  3137. qh->qh_state = QH_STATE_IDLE;
  3138. hw->hw_next = FOTG210_LIST_END(fotg210);
  3139. qh_completions(fotg210, qh);
  3140. /* reschedule QH iff another request is queued */
  3141. if (!list_empty(&qh->qtd_list) &&
  3142. fotg210->rh_state == FOTG210_RH_RUNNING) {
  3143. rc = qh_schedule(fotg210, qh);
  3144. /* An error here likely indicates handshake failure
  3145. * or no space left in the schedule. Neither fault
  3146. * should happen often ...
  3147. *
  3148. * FIXME kill the now-dysfunctional queued urbs
  3149. */
  3150. if (rc != 0)
  3151. fotg210_err(fotg210, "can't reschedule qh %p, err %d\n",
  3152. qh, rc);
  3153. }
  3154. /* maybe turn off periodic schedule */
  3155. --fotg210->intr_count;
  3156. disable_periodic(fotg210);
  3157. }
  3158. static int check_period(struct fotg210_hcd *fotg210, unsigned frame,
  3159. unsigned uframe, unsigned period, unsigned usecs)
  3160. {
  3161. int claimed;
  3162. /* complete split running into next frame?
  3163. * given FSTN support, we could sometimes check...
  3164. */
  3165. if (uframe >= 8)
  3166. return 0;
  3167. /* convert "usecs we need" to "max already claimed" */
  3168. usecs = fotg210->uframe_periodic_max - usecs;
  3169. /* we "know" 2 and 4 uframe intervals were rejected; so
  3170. * for period 0, check _every_ microframe in the schedule.
  3171. */
  3172. if (unlikely(period == 0)) {
  3173. do {
  3174. for (uframe = 0; uframe < 7; uframe++) {
  3175. claimed = periodic_usecs(fotg210, frame,
  3176. uframe);
  3177. if (claimed > usecs)
  3178. return 0;
  3179. }
  3180. } while ((frame += 1) < fotg210->periodic_size);
  3181. /* just check the specified uframe, at that period */
  3182. } else {
  3183. do {
  3184. claimed = periodic_usecs(fotg210, frame, uframe);
  3185. if (claimed > usecs)
  3186. return 0;
  3187. } while ((frame += period) < fotg210->periodic_size);
  3188. }
  3189. /* success! */
  3190. return 1;
  3191. }
  3192. static int check_intr_schedule(struct fotg210_hcd *fotg210, unsigned frame,
  3193. unsigned uframe, const struct fotg210_qh *qh, __hc32 *c_maskp)
  3194. {
  3195. int retval = -ENOSPC;
  3196. u8 mask = 0;
  3197. if (qh->c_usecs && uframe >= 6) /* FSTN territory? */
  3198. goto done;
  3199. if (!check_period(fotg210, frame, uframe, qh->period, qh->usecs))
  3200. goto done;
  3201. if (!qh->c_usecs) {
  3202. retval = 0;
  3203. *c_maskp = 0;
  3204. goto done;
  3205. }
  3206. /* Make sure this tt's buffer is also available for CSPLITs.
  3207. * We pessimize a bit; probably the typical full speed case
  3208. * doesn't need the second CSPLIT.
  3209. *
  3210. * NOTE: both SPLIT and CSPLIT could be checked in just
  3211. * one smart pass...
  3212. */
  3213. mask = 0x03 << (uframe + qh->gap_uf);
  3214. *c_maskp = cpu_to_hc32(fotg210, mask << 8);
  3215. mask |= 1 << uframe;
  3216. if (tt_no_collision(fotg210, qh->period, qh->dev, frame, mask)) {
  3217. if (!check_period(fotg210, frame, uframe + qh->gap_uf + 1,
  3218. qh->period, qh->c_usecs))
  3219. goto done;
  3220. if (!check_period(fotg210, frame, uframe + qh->gap_uf,
  3221. qh->period, qh->c_usecs))
  3222. goto done;
  3223. retval = 0;
  3224. }
  3225. done:
  3226. return retval;
  3227. }
  3228. /* "first fit" scheduling policy used the first time through,
  3229. * or when the previous schedule slot can't be re-used.
  3230. */
  3231. static int qh_schedule(struct fotg210_hcd *fotg210, struct fotg210_qh *qh)
  3232. {
  3233. int status;
  3234. unsigned uframe;
  3235. __hc32 c_mask;
  3236. unsigned frame; /* 0..(qh->period - 1), or NO_FRAME */
  3237. struct fotg210_qh_hw *hw = qh->hw;
  3238. qh_refresh(fotg210, qh);
  3239. hw->hw_next = FOTG210_LIST_END(fotg210);
  3240. frame = qh->start;
  3241. /* reuse the previous schedule slots, if we can */
  3242. if (frame < qh->period) {
  3243. uframe = ffs(hc32_to_cpup(fotg210, &hw->hw_info2) & QH_SMASK);
  3244. status = check_intr_schedule(fotg210, frame, --uframe,
  3245. qh, &c_mask);
  3246. } else {
  3247. uframe = 0;
  3248. c_mask = 0;
  3249. status = -ENOSPC;
  3250. }
  3251. /* else scan the schedule to find a group of slots such that all
  3252. * uframes have enough periodic bandwidth available.
  3253. */
  3254. if (status) {
  3255. /* "normal" case, uframing flexible except with splits */
  3256. if (qh->period) {
  3257. int i;
  3258. for (i = qh->period; status && i > 0; --i) {
  3259. frame = ++fotg210->random_frame % qh->period;
  3260. for (uframe = 0; uframe < 8; uframe++) {
  3261. status = check_intr_schedule(fotg210,
  3262. frame, uframe, qh,
  3263. &c_mask);
  3264. if (status == 0)
  3265. break;
  3266. }
  3267. }
  3268. /* qh->period == 0 means every uframe */
  3269. } else {
  3270. frame = 0;
  3271. status = check_intr_schedule(fotg210, 0, 0, qh,
  3272. &c_mask);
  3273. }
  3274. if (status)
  3275. goto done;
  3276. qh->start = frame;
  3277. /* reset S-frame and (maybe) C-frame masks */
  3278. hw->hw_info2 &= cpu_to_hc32(fotg210, ~(QH_CMASK | QH_SMASK));
  3279. hw->hw_info2 |= qh->period
  3280. ? cpu_to_hc32(fotg210, 1 << uframe)
  3281. : cpu_to_hc32(fotg210, QH_SMASK);
  3282. hw->hw_info2 |= c_mask;
  3283. } else
  3284. fotg210_dbg(fotg210, "reused qh %p schedule\n", qh);
  3285. /* stuff into the periodic schedule */
  3286. qh_link_periodic(fotg210, qh);
  3287. done:
  3288. return status;
  3289. }
  3290. static int intr_submit(struct fotg210_hcd *fotg210, struct urb *urb,
  3291. struct list_head *qtd_list, gfp_t mem_flags)
  3292. {
  3293. unsigned epnum;
  3294. unsigned long flags;
  3295. struct fotg210_qh *qh;
  3296. int status;
  3297. struct list_head empty;
  3298. /* get endpoint and transfer/schedule data */
  3299. epnum = urb->ep->desc.bEndpointAddress;
  3300. spin_lock_irqsave(&fotg210->lock, flags);
  3301. if (unlikely(!HCD_HW_ACCESSIBLE(fotg210_to_hcd(fotg210)))) {
  3302. status = -ESHUTDOWN;
  3303. goto done_not_linked;
  3304. }
  3305. status = usb_hcd_link_urb_to_ep(fotg210_to_hcd(fotg210), urb);
  3306. if (unlikely(status))
  3307. goto done_not_linked;
  3308. /* get qh and force any scheduling errors */
  3309. INIT_LIST_HEAD(&empty);
  3310. qh = qh_append_tds(fotg210, urb, &empty, epnum, &urb->ep->hcpriv);
  3311. if (qh == NULL) {
  3312. status = -ENOMEM;
  3313. goto done;
  3314. }
  3315. if (qh->qh_state == QH_STATE_IDLE) {
  3316. status = qh_schedule(fotg210, qh);
  3317. if (status)
  3318. goto done;
  3319. }
  3320. /* then queue the urb's tds to the qh */
  3321. qh = qh_append_tds(fotg210, urb, qtd_list, epnum, &urb->ep->hcpriv);
  3322. BUG_ON(qh == NULL);
  3323. /* ... update usbfs periodic stats */
  3324. fotg210_to_hcd(fotg210)->self.bandwidth_int_reqs++;
  3325. done:
  3326. if (unlikely(status))
  3327. usb_hcd_unlink_urb_from_ep(fotg210_to_hcd(fotg210), urb);
  3328. done_not_linked:
  3329. spin_unlock_irqrestore(&fotg210->lock, flags);
  3330. if (status)
  3331. qtd_list_free(fotg210, urb, qtd_list);
  3332. return status;
  3333. }
  3334. static void scan_intr(struct fotg210_hcd *fotg210)
  3335. {
  3336. struct fotg210_qh *qh;
  3337. list_for_each_entry_safe(qh, fotg210->qh_scan_next,
  3338. &fotg210->intr_qh_list, intr_node) {
  3339. rescan:
  3340. /* clean any finished work for this qh */
  3341. if (!list_empty(&qh->qtd_list)) {
  3342. int temp;
  3343. /*
  3344. * Unlinks could happen here; completion reporting
  3345. * drops the lock. That's why fotg210->qh_scan_next
  3346. * always holds the next qh to scan; if the next qh
  3347. * gets unlinked then fotg210->qh_scan_next is adjusted
  3348. * in qh_unlink_periodic().
  3349. */
  3350. temp = qh_completions(fotg210, qh);
  3351. if (unlikely(qh->needs_rescan ||
  3352. (list_empty(&qh->qtd_list) &&
  3353. qh->qh_state == QH_STATE_LINKED)))
  3354. start_unlink_intr(fotg210, qh);
  3355. else if (temp != 0)
  3356. goto rescan;
  3357. }
  3358. }
  3359. }
  3360. /* fotg210_iso_stream ops work with both ITD and SITD */
  3361. static struct fotg210_iso_stream *iso_stream_alloc(gfp_t mem_flags)
  3362. {
  3363. struct fotg210_iso_stream *stream;
  3364. stream = kzalloc(sizeof(*stream), mem_flags);
  3365. if (likely(stream != NULL)) {
  3366. INIT_LIST_HEAD(&stream->td_list);
  3367. INIT_LIST_HEAD(&stream->free_list);
  3368. stream->next_uframe = -1;
  3369. }
  3370. return stream;
  3371. }
  3372. static void iso_stream_init(struct fotg210_hcd *fotg210,
  3373. struct fotg210_iso_stream *stream, struct usb_device *dev,
  3374. int pipe, unsigned interval)
  3375. {
  3376. u32 buf1;
  3377. unsigned epnum, maxp;
  3378. int is_input;
  3379. long bandwidth;
  3380. unsigned multi;
  3381. struct usb_host_endpoint *ep;
  3382. /*
  3383. * this might be a "high bandwidth" highspeed endpoint,
  3384. * as encoded in the ep descriptor's wMaxPacket field
  3385. */
  3386. epnum = usb_pipeendpoint(pipe);
  3387. is_input = usb_pipein(pipe) ? USB_DIR_IN : 0;
  3388. ep = usb_pipe_endpoint(dev, pipe);
  3389. maxp = usb_endpoint_maxp(&ep->desc);
  3390. if (is_input)
  3391. buf1 = (1 << 11);
  3392. else
  3393. buf1 = 0;
  3394. multi = usb_endpoint_maxp_mult(&ep->desc);
  3395. buf1 |= maxp;
  3396. maxp *= multi;
  3397. stream->buf0 = cpu_to_hc32(fotg210, (epnum << 8) | dev->devnum);
  3398. stream->buf1 = cpu_to_hc32(fotg210, buf1);
  3399. stream->buf2 = cpu_to_hc32(fotg210, multi);
  3400. /* usbfs wants to report the average usecs per frame tied up
  3401. * when transfers on this endpoint are scheduled ...
  3402. */
  3403. if (dev->speed == USB_SPEED_FULL) {
  3404. interval <<= 3;
  3405. stream->usecs = NS_TO_US(usb_calc_bus_time(dev->speed,
  3406. is_input, 1, maxp));
  3407. stream->usecs /= 8;
  3408. } else {
  3409. stream->highspeed = 1;
  3410. stream->usecs = HS_USECS_ISO(maxp);
  3411. }
  3412. bandwidth = stream->usecs * 8;
  3413. bandwidth /= interval;
  3414. stream->bandwidth = bandwidth;
  3415. stream->udev = dev;
  3416. stream->bEndpointAddress = is_input | epnum;
  3417. stream->interval = interval;
  3418. stream->maxp = maxp;
  3419. }
  3420. static struct fotg210_iso_stream *iso_stream_find(struct fotg210_hcd *fotg210,
  3421. struct urb *urb)
  3422. {
  3423. unsigned epnum;
  3424. struct fotg210_iso_stream *stream;
  3425. struct usb_host_endpoint *ep;
  3426. unsigned long flags;
  3427. epnum = usb_pipeendpoint(urb->pipe);
  3428. if (usb_pipein(urb->pipe))
  3429. ep = urb->dev->ep_in[epnum];
  3430. else
  3431. ep = urb->dev->ep_out[epnum];
  3432. spin_lock_irqsave(&fotg210->lock, flags);
  3433. stream = ep->hcpriv;
  3434. if (unlikely(stream == NULL)) {
  3435. stream = iso_stream_alloc(GFP_ATOMIC);
  3436. if (likely(stream != NULL)) {
  3437. ep->hcpriv = stream;
  3438. stream->ep = ep;
  3439. iso_stream_init(fotg210, stream, urb->dev, urb->pipe,
  3440. urb->interval);
  3441. }
  3442. /* if dev->ep[epnum] is a QH, hw is set */
  3443. } else if (unlikely(stream->hw != NULL)) {
  3444. fotg210_dbg(fotg210, "dev %s ep%d%s, not iso??\n",
  3445. urb->dev->devpath, epnum,
  3446. usb_pipein(urb->pipe) ? "in" : "out");
  3447. stream = NULL;
  3448. }
  3449. spin_unlock_irqrestore(&fotg210->lock, flags);
  3450. return stream;
  3451. }
  3452. /* fotg210_iso_sched ops can be ITD-only or SITD-only */
  3453. static struct fotg210_iso_sched *iso_sched_alloc(unsigned packets,
  3454. gfp_t mem_flags)
  3455. {
  3456. struct fotg210_iso_sched *iso_sched;
  3457. iso_sched = kzalloc(struct_size(iso_sched, packet, packets), mem_flags);
  3458. if (likely(iso_sched != NULL))
  3459. INIT_LIST_HEAD(&iso_sched->td_list);
  3460. return iso_sched;
  3461. }
  3462. static inline void itd_sched_init(struct fotg210_hcd *fotg210,
  3463. struct fotg210_iso_sched *iso_sched,
  3464. struct fotg210_iso_stream *stream, struct urb *urb)
  3465. {
  3466. unsigned i;
  3467. dma_addr_t dma = urb->transfer_dma;
  3468. /* how many uframes are needed for these transfers */
  3469. iso_sched->span = urb->number_of_packets * stream->interval;
  3470. /* figure out per-uframe itd fields that we'll need later
  3471. * when we fit new itds into the schedule.
  3472. */
  3473. for (i = 0; i < urb->number_of_packets; i++) {
  3474. struct fotg210_iso_packet *uframe = &iso_sched->packet[i];
  3475. unsigned length;
  3476. dma_addr_t buf;
  3477. u32 trans;
  3478. length = urb->iso_frame_desc[i].length;
  3479. buf = dma + urb->iso_frame_desc[i].offset;
  3480. trans = FOTG210_ISOC_ACTIVE;
  3481. trans |= buf & 0x0fff;
  3482. if (unlikely(((i + 1) == urb->number_of_packets))
  3483. && !(urb->transfer_flags & URB_NO_INTERRUPT))
  3484. trans |= FOTG210_ITD_IOC;
  3485. trans |= length << 16;
  3486. uframe->transaction = cpu_to_hc32(fotg210, trans);
  3487. /* might need to cross a buffer page within a uframe */
  3488. uframe->bufp = (buf & ~(u64)0x0fff);
  3489. buf += length;
  3490. if (unlikely((uframe->bufp != (buf & ~(u64)0x0fff))))
  3491. uframe->cross = 1;
  3492. }
  3493. }
  3494. static void iso_sched_free(struct fotg210_iso_stream *stream,
  3495. struct fotg210_iso_sched *iso_sched)
  3496. {
  3497. if (!iso_sched)
  3498. return;
  3499. /* caller must hold fotg210->lock!*/
  3500. list_splice(&iso_sched->td_list, &stream->free_list);
  3501. kfree(iso_sched);
  3502. }
  3503. static int itd_urb_transaction(struct fotg210_iso_stream *stream,
  3504. struct fotg210_hcd *fotg210, struct urb *urb, gfp_t mem_flags)
  3505. {
  3506. struct fotg210_itd *itd;
  3507. dma_addr_t itd_dma;
  3508. int i;
  3509. unsigned num_itds;
  3510. struct fotg210_iso_sched *sched;
  3511. unsigned long flags;
  3512. sched = iso_sched_alloc(urb->number_of_packets, mem_flags);
  3513. if (unlikely(sched == NULL))
  3514. return -ENOMEM;
  3515. itd_sched_init(fotg210, sched, stream, urb);
  3516. if (urb->interval < 8)
  3517. num_itds = 1 + (sched->span + 7) / 8;
  3518. else
  3519. num_itds = urb->number_of_packets;
  3520. /* allocate/init ITDs */
  3521. spin_lock_irqsave(&fotg210->lock, flags);
  3522. for (i = 0; i < num_itds; i++) {
  3523. /*
  3524. * Use iTDs from the free list, but not iTDs that may
  3525. * still be in use by the hardware.
  3526. */
  3527. if (likely(!list_empty(&stream->free_list))) {
  3528. itd = list_first_entry(&stream->free_list,
  3529. struct fotg210_itd, itd_list);
  3530. if (itd->frame == fotg210->now_frame)
  3531. goto alloc_itd;
  3532. list_del(&itd->itd_list);
  3533. itd_dma = itd->itd_dma;
  3534. } else {
  3535. alloc_itd:
  3536. spin_unlock_irqrestore(&fotg210->lock, flags);
  3537. itd = dma_pool_alloc(fotg210->itd_pool, mem_flags,
  3538. &itd_dma);
  3539. spin_lock_irqsave(&fotg210->lock, flags);
  3540. if (!itd) {
  3541. iso_sched_free(stream, sched);
  3542. spin_unlock_irqrestore(&fotg210->lock, flags);
  3543. return -ENOMEM;
  3544. }
  3545. }
  3546. memset(itd, 0, sizeof(*itd));
  3547. itd->itd_dma = itd_dma;
  3548. list_add(&itd->itd_list, &sched->td_list);
  3549. }
  3550. spin_unlock_irqrestore(&fotg210->lock, flags);
  3551. /* temporarily store schedule info in hcpriv */
  3552. urb->hcpriv = sched;
  3553. urb->error_count = 0;
  3554. return 0;
  3555. }
  3556. static inline int itd_slot_ok(struct fotg210_hcd *fotg210, u32 mod, u32 uframe,
  3557. u8 usecs, u32 period)
  3558. {
  3559. uframe %= period;
  3560. do {
  3561. /* can't commit more than uframe_periodic_max usec */
  3562. if (periodic_usecs(fotg210, uframe >> 3, uframe & 0x7)
  3563. > (fotg210->uframe_periodic_max - usecs))
  3564. return 0;
  3565. /* we know urb->interval is 2^N uframes */
  3566. uframe += period;
  3567. } while (uframe < mod);
  3568. return 1;
  3569. }
  3570. /* This scheduler plans almost as far into the future as it has actual
  3571. * periodic schedule slots. (Affected by TUNE_FLS, which defaults to
  3572. * "as small as possible" to be cache-friendlier.) That limits the size
  3573. * transfers you can stream reliably; avoid more than 64 msec per urb.
  3574. * Also avoid queue depths of less than fotg210's worst irq latency (affected
  3575. * by the per-urb URB_NO_INTERRUPT hint, the log2_irq_thresh module parameter,
  3576. * and other factors); or more than about 230 msec total (for portability,
  3577. * given FOTG210_TUNE_FLS and the slop). Or, write a smarter scheduler!
  3578. */
  3579. #define SCHEDULE_SLOP 80 /* microframes */
  3580. static int iso_stream_schedule(struct fotg210_hcd *fotg210, struct urb *urb,
  3581. struct fotg210_iso_stream *stream)
  3582. {
  3583. u32 now, next, start, period, span;
  3584. int status;
  3585. unsigned mod = fotg210->periodic_size << 3;
  3586. struct fotg210_iso_sched *sched = urb->hcpriv;
  3587. period = urb->interval;
  3588. span = sched->span;
  3589. if (span > mod - SCHEDULE_SLOP) {
  3590. fotg210_dbg(fotg210, "iso request %p too long\n", urb);
  3591. status = -EFBIG;
  3592. goto fail;
  3593. }
  3594. now = fotg210_read_frame_index(fotg210) & (mod - 1);
  3595. /* Typical case: reuse current schedule, stream is still active.
  3596. * Hopefully there are no gaps from the host falling behind
  3597. * (irq delays etc), but if there are we'll take the next
  3598. * slot in the schedule, implicitly assuming URB_ISO_ASAP.
  3599. */
  3600. if (likely(!list_empty(&stream->td_list))) {
  3601. u32 excess;
  3602. /* For high speed devices, allow scheduling within the
  3603. * isochronous scheduling threshold. For full speed devices
  3604. * and Intel PCI-based controllers, don't (work around for
  3605. * Intel ICH9 bug).
  3606. */
  3607. if (!stream->highspeed && fotg210->fs_i_thresh)
  3608. next = now + fotg210->i_thresh;
  3609. else
  3610. next = now;
  3611. /* Fell behind (by up to twice the slop amount)?
  3612. * We decide based on the time of the last currently-scheduled
  3613. * slot, not the time of the next available slot.
  3614. */
  3615. excess = (stream->next_uframe - period - next) & (mod - 1);
  3616. if (excess >= mod - 2 * SCHEDULE_SLOP)
  3617. start = next + excess - mod + period *
  3618. DIV_ROUND_UP(mod - excess, period);
  3619. else
  3620. start = next + excess + period;
  3621. if (start - now >= mod) {
  3622. fotg210_dbg(fotg210, "request %p would overflow (%d+%d >= %d)\n",
  3623. urb, start - now - period, period,
  3624. mod);
  3625. status = -EFBIG;
  3626. goto fail;
  3627. }
  3628. }
  3629. /* need to schedule; when's the next (u)frame we could start?
  3630. * this is bigger than fotg210->i_thresh allows; scheduling itself
  3631. * isn't free, the slop should handle reasonably slow cpus. it
  3632. * can also help high bandwidth if the dma and irq loads don't
  3633. * jump until after the queue is primed.
  3634. */
  3635. else {
  3636. int done = 0;
  3637. start = SCHEDULE_SLOP + (now & ~0x07);
  3638. /* NOTE: assumes URB_ISO_ASAP, to limit complexity/bugs */
  3639. /* find a uframe slot with enough bandwidth.
  3640. * Early uframes are more precious because full-speed
  3641. * iso IN transfers can't use late uframes,
  3642. * and therefore they should be allocated last.
  3643. */
  3644. next = start;
  3645. start += period;
  3646. do {
  3647. start--;
  3648. /* check schedule: enough space? */
  3649. if (itd_slot_ok(fotg210, mod, start,
  3650. stream->usecs, period))
  3651. done = 1;
  3652. } while (start > next && !done);
  3653. /* no room in the schedule */
  3654. if (!done) {
  3655. fotg210_dbg(fotg210, "iso resched full %p (now %d max %d)\n",
  3656. urb, now, now + mod);
  3657. status = -ENOSPC;
  3658. goto fail;
  3659. }
  3660. }
  3661. /* Tried to schedule too far into the future? */
  3662. if (unlikely(start - now + span - period >=
  3663. mod - 2 * SCHEDULE_SLOP)) {
  3664. fotg210_dbg(fotg210, "request %p would overflow (%d+%d >= %d)\n",
  3665. urb, start - now, span - period,
  3666. mod - 2 * SCHEDULE_SLOP);
  3667. status = -EFBIG;
  3668. goto fail;
  3669. }
  3670. stream->next_uframe = start & (mod - 1);
  3671. /* report high speed start in uframes; full speed, in frames */
  3672. urb->start_frame = stream->next_uframe;
  3673. if (!stream->highspeed)
  3674. urb->start_frame >>= 3;
  3675. /* Make sure scan_isoc() sees these */
  3676. if (fotg210->isoc_count == 0)
  3677. fotg210->next_frame = now >> 3;
  3678. return 0;
  3679. fail:
  3680. iso_sched_free(stream, sched);
  3681. urb->hcpriv = NULL;
  3682. return status;
  3683. }
  3684. static inline void itd_init(struct fotg210_hcd *fotg210,
  3685. struct fotg210_iso_stream *stream, struct fotg210_itd *itd)
  3686. {
  3687. int i;
  3688. /* it's been recently zeroed */
  3689. itd->hw_next = FOTG210_LIST_END(fotg210);
  3690. itd->hw_bufp[0] = stream->buf0;
  3691. itd->hw_bufp[1] = stream->buf1;
  3692. itd->hw_bufp[2] = stream->buf2;
  3693. for (i = 0; i < 8; i++)
  3694. itd->index[i] = -1;
  3695. /* All other fields are filled when scheduling */
  3696. }
  3697. static inline void itd_patch(struct fotg210_hcd *fotg210,
  3698. struct fotg210_itd *itd, struct fotg210_iso_sched *iso_sched,
  3699. unsigned index, u16 uframe)
  3700. {
  3701. struct fotg210_iso_packet *uf = &iso_sched->packet[index];
  3702. unsigned pg = itd->pg;
  3703. uframe &= 0x07;
  3704. itd->index[uframe] = index;
  3705. itd->hw_transaction[uframe] = uf->transaction;
  3706. itd->hw_transaction[uframe] |= cpu_to_hc32(fotg210, pg << 12);
  3707. itd->hw_bufp[pg] |= cpu_to_hc32(fotg210, uf->bufp & ~(u32)0);
  3708. itd->hw_bufp_hi[pg] |= cpu_to_hc32(fotg210, (u32)(uf->bufp >> 32));
  3709. /* iso_frame_desc[].offset must be strictly increasing */
  3710. if (unlikely(uf->cross)) {
  3711. u64 bufp = uf->bufp + 4096;
  3712. itd->pg = ++pg;
  3713. itd->hw_bufp[pg] |= cpu_to_hc32(fotg210, bufp & ~(u32)0);
  3714. itd->hw_bufp_hi[pg] |= cpu_to_hc32(fotg210, (u32)(bufp >> 32));
  3715. }
  3716. }
  3717. static inline void itd_link(struct fotg210_hcd *fotg210, unsigned frame,
  3718. struct fotg210_itd *itd)
  3719. {
  3720. union fotg210_shadow *prev = &fotg210->pshadow[frame];
  3721. __hc32 *hw_p = &fotg210->periodic[frame];
  3722. union fotg210_shadow here = *prev;
  3723. __hc32 type = 0;
  3724. /* skip any iso nodes which might belong to previous microframes */
  3725. while (here.ptr) {
  3726. type = Q_NEXT_TYPE(fotg210, *hw_p);
  3727. if (type == cpu_to_hc32(fotg210, Q_TYPE_QH))
  3728. break;
  3729. prev = periodic_next_shadow(fotg210, prev, type);
  3730. hw_p = shadow_next_periodic(fotg210, &here, type);
  3731. here = *prev;
  3732. }
  3733. itd->itd_next = here;
  3734. itd->hw_next = *hw_p;
  3735. prev->itd = itd;
  3736. itd->frame = frame;
  3737. wmb();
  3738. *hw_p = cpu_to_hc32(fotg210, itd->itd_dma | Q_TYPE_ITD);
  3739. }
  3740. /* fit urb's itds into the selected schedule slot; activate as needed */
  3741. static void itd_link_urb(struct fotg210_hcd *fotg210, struct urb *urb,
  3742. unsigned mod, struct fotg210_iso_stream *stream)
  3743. {
  3744. int packet;
  3745. unsigned next_uframe, uframe, frame;
  3746. struct fotg210_iso_sched *iso_sched = urb->hcpriv;
  3747. struct fotg210_itd *itd;
  3748. next_uframe = stream->next_uframe & (mod - 1);
  3749. if (unlikely(list_empty(&stream->td_list))) {
  3750. fotg210_to_hcd(fotg210)->self.bandwidth_allocated
  3751. += stream->bandwidth;
  3752. fotg210_dbg(fotg210,
  3753. "schedule devp %s ep%d%s-iso period %d start %d.%d\n",
  3754. urb->dev->devpath, stream->bEndpointAddress & 0x0f,
  3755. (stream->bEndpointAddress & USB_DIR_IN) ? "in" : "out",
  3756. urb->interval,
  3757. next_uframe >> 3, next_uframe & 0x7);
  3758. }
  3759. /* fill iTDs uframe by uframe */
  3760. for (packet = 0, itd = NULL; packet < urb->number_of_packets;) {
  3761. if (itd == NULL) {
  3762. /* ASSERT: we have all necessary itds */
  3763. /* ASSERT: no itds for this endpoint in this uframe */
  3764. itd = list_entry(iso_sched->td_list.next,
  3765. struct fotg210_itd, itd_list);
  3766. list_move_tail(&itd->itd_list, &stream->td_list);
  3767. itd->stream = stream;
  3768. itd->urb = urb;
  3769. itd_init(fotg210, stream, itd);
  3770. }
  3771. uframe = next_uframe & 0x07;
  3772. frame = next_uframe >> 3;
  3773. itd_patch(fotg210, itd, iso_sched, packet, uframe);
  3774. next_uframe += stream->interval;
  3775. next_uframe &= mod - 1;
  3776. packet++;
  3777. /* link completed itds into the schedule */
  3778. if (((next_uframe >> 3) != frame)
  3779. || packet == urb->number_of_packets) {
  3780. itd_link(fotg210, frame & (fotg210->periodic_size - 1),
  3781. itd);
  3782. itd = NULL;
  3783. }
  3784. }
  3785. stream->next_uframe = next_uframe;
  3786. /* don't need that schedule data any more */
  3787. iso_sched_free(stream, iso_sched);
  3788. urb->hcpriv = NULL;
  3789. ++fotg210->isoc_count;
  3790. enable_periodic(fotg210);
  3791. }
  3792. #define ISO_ERRS (FOTG210_ISOC_BUF_ERR | FOTG210_ISOC_BABBLE |\
  3793. FOTG210_ISOC_XACTERR)
  3794. /* Process and recycle a completed ITD. Return true iff its urb completed,
  3795. * and hence its completion callback probably added things to the hardware
  3796. * schedule.
  3797. *
  3798. * Note that we carefully avoid recycling this descriptor until after any
  3799. * completion callback runs, so that it won't be reused quickly. That is,
  3800. * assuming (a) no more than two urbs per frame on this endpoint, and also
  3801. * (b) only this endpoint's completions submit URBs. It seems some silicon
  3802. * corrupts things if you reuse completed descriptors very quickly...
  3803. */
  3804. static bool itd_complete(struct fotg210_hcd *fotg210, struct fotg210_itd *itd)
  3805. {
  3806. struct urb *urb = itd->urb;
  3807. struct usb_iso_packet_descriptor *desc;
  3808. u32 t;
  3809. unsigned uframe;
  3810. int urb_index = -1;
  3811. struct fotg210_iso_stream *stream = itd->stream;
  3812. struct usb_device *dev;
  3813. bool retval = false;
  3814. /* for each uframe with a packet */
  3815. for (uframe = 0; uframe < 8; uframe++) {
  3816. if (likely(itd->index[uframe] == -1))
  3817. continue;
  3818. urb_index = itd->index[uframe];
  3819. desc = &urb->iso_frame_desc[urb_index];
  3820. t = hc32_to_cpup(fotg210, &itd->hw_transaction[uframe]);
  3821. itd->hw_transaction[uframe] = 0;
  3822. /* report transfer status */
  3823. if (unlikely(t & ISO_ERRS)) {
  3824. urb->error_count++;
  3825. if (t & FOTG210_ISOC_BUF_ERR)
  3826. desc->status = usb_pipein(urb->pipe)
  3827. ? -ENOSR /* hc couldn't read */
  3828. : -ECOMM; /* hc couldn't write */
  3829. else if (t & FOTG210_ISOC_BABBLE)
  3830. desc->status = -EOVERFLOW;
  3831. else /* (t & FOTG210_ISOC_XACTERR) */
  3832. desc->status = -EPROTO;
  3833. /* HC need not update length with this error */
  3834. if (!(t & FOTG210_ISOC_BABBLE)) {
  3835. desc->actual_length = FOTG210_ITD_LENGTH(t);
  3836. urb->actual_length += desc->actual_length;
  3837. }
  3838. } else if (likely((t & FOTG210_ISOC_ACTIVE) == 0)) {
  3839. desc->status = 0;
  3840. desc->actual_length = FOTG210_ITD_LENGTH(t);
  3841. urb->actual_length += desc->actual_length;
  3842. } else {
  3843. /* URB was too late */
  3844. desc->status = -EXDEV;
  3845. }
  3846. }
  3847. /* handle completion now? */
  3848. if (likely((urb_index + 1) != urb->number_of_packets))
  3849. goto done;
  3850. /* ASSERT: it's really the last itd for this urb
  3851. * list_for_each_entry (itd, &stream->td_list, itd_list)
  3852. * BUG_ON (itd->urb == urb);
  3853. */
  3854. /* give urb back to the driver; completion often (re)submits */
  3855. dev = urb->dev;
  3856. fotg210_urb_done(fotg210, urb, 0);
  3857. retval = true;
  3858. urb = NULL;
  3859. --fotg210->isoc_count;
  3860. disable_periodic(fotg210);
  3861. if (unlikely(list_is_singular(&stream->td_list))) {
  3862. fotg210_to_hcd(fotg210)->self.bandwidth_allocated
  3863. -= stream->bandwidth;
  3864. fotg210_dbg(fotg210,
  3865. "deschedule devp %s ep%d%s-iso\n",
  3866. dev->devpath, stream->bEndpointAddress & 0x0f,
  3867. (stream->bEndpointAddress & USB_DIR_IN) ? "in" : "out");
  3868. }
  3869. done:
  3870. itd->urb = NULL;
  3871. /* Add to the end of the free list for later reuse */
  3872. list_move_tail(&itd->itd_list, &stream->free_list);
  3873. /* Recycle the iTDs when the pipeline is empty (ep no longer in use) */
  3874. if (list_empty(&stream->td_list)) {
  3875. list_splice_tail_init(&stream->free_list,
  3876. &fotg210->cached_itd_list);
  3877. start_free_itds(fotg210);
  3878. }
  3879. return retval;
  3880. }
  3881. static int itd_submit(struct fotg210_hcd *fotg210, struct urb *urb,
  3882. gfp_t mem_flags)
  3883. {
  3884. int status = -EINVAL;
  3885. unsigned long flags;
  3886. struct fotg210_iso_stream *stream;
  3887. /* Get iso_stream head */
  3888. stream = iso_stream_find(fotg210, urb);
  3889. if (unlikely(stream == NULL)) {
  3890. fotg210_dbg(fotg210, "can't get iso stream\n");
  3891. return -ENOMEM;
  3892. }
  3893. if (unlikely(urb->interval != stream->interval &&
  3894. fotg210_port_speed(fotg210, 0) ==
  3895. USB_PORT_STAT_HIGH_SPEED)) {
  3896. fotg210_dbg(fotg210, "can't change iso interval %d --> %d\n",
  3897. stream->interval, urb->interval);
  3898. goto done;
  3899. }
  3900. #ifdef FOTG210_URB_TRACE
  3901. fotg210_dbg(fotg210,
  3902. "%s %s urb %p ep%d%s len %d, %d pkts %d uframes[%p]\n",
  3903. __func__, urb->dev->devpath, urb,
  3904. usb_pipeendpoint(urb->pipe),
  3905. usb_pipein(urb->pipe) ? "in" : "out",
  3906. urb->transfer_buffer_length,
  3907. urb->number_of_packets, urb->interval,
  3908. stream);
  3909. #endif
  3910. /* allocate ITDs w/o locking anything */
  3911. status = itd_urb_transaction(stream, fotg210, urb, mem_flags);
  3912. if (unlikely(status < 0)) {
  3913. fotg210_dbg(fotg210, "can't init itds\n");
  3914. goto done;
  3915. }
  3916. /* schedule ... need to lock */
  3917. spin_lock_irqsave(&fotg210->lock, flags);
  3918. if (unlikely(!HCD_HW_ACCESSIBLE(fotg210_to_hcd(fotg210)))) {
  3919. status = -ESHUTDOWN;
  3920. goto done_not_linked;
  3921. }
  3922. status = usb_hcd_link_urb_to_ep(fotg210_to_hcd(fotg210), urb);
  3923. if (unlikely(status))
  3924. goto done_not_linked;
  3925. status = iso_stream_schedule(fotg210, urb, stream);
  3926. if (likely(status == 0))
  3927. itd_link_urb(fotg210, urb, fotg210->periodic_size << 3, stream);
  3928. else
  3929. usb_hcd_unlink_urb_from_ep(fotg210_to_hcd(fotg210), urb);
  3930. done_not_linked:
  3931. spin_unlock_irqrestore(&fotg210->lock, flags);
  3932. done:
  3933. return status;
  3934. }
  3935. static inline int scan_frame_queue(struct fotg210_hcd *fotg210, unsigned frame,
  3936. unsigned now_frame, bool live)
  3937. {
  3938. unsigned uf;
  3939. bool modified;
  3940. union fotg210_shadow q, *q_p;
  3941. __hc32 type, *hw_p;
  3942. /* scan each element in frame's queue for completions */
  3943. q_p = &fotg210->pshadow[frame];
  3944. hw_p = &fotg210->periodic[frame];
  3945. q.ptr = q_p->ptr;
  3946. type = Q_NEXT_TYPE(fotg210, *hw_p);
  3947. modified = false;
  3948. while (q.ptr) {
  3949. switch (hc32_to_cpu(fotg210, type)) {
  3950. case Q_TYPE_ITD:
  3951. /* If this ITD is still active, leave it for
  3952. * later processing ... check the next entry.
  3953. * No need to check for activity unless the
  3954. * frame is current.
  3955. */
  3956. if (frame == now_frame && live) {
  3957. rmb();
  3958. for (uf = 0; uf < 8; uf++) {
  3959. if (q.itd->hw_transaction[uf] &
  3960. ITD_ACTIVE(fotg210))
  3961. break;
  3962. }
  3963. if (uf < 8) {
  3964. q_p = &q.itd->itd_next;
  3965. hw_p = &q.itd->hw_next;
  3966. type = Q_NEXT_TYPE(fotg210,
  3967. q.itd->hw_next);
  3968. q = *q_p;
  3969. break;
  3970. }
  3971. }
  3972. /* Take finished ITDs out of the schedule
  3973. * and process them: recycle, maybe report
  3974. * URB completion. HC won't cache the
  3975. * pointer for much longer, if at all.
  3976. */
  3977. *q_p = q.itd->itd_next;
  3978. *hw_p = q.itd->hw_next;
  3979. type = Q_NEXT_TYPE(fotg210, q.itd->hw_next);
  3980. wmb();
  3981. modified = itd_complete(fotg210, q.itd);
  3982. q = *q_p;
  3983. break;
  3984. default:
  3985. fotg210_dbg(fotg210, "corrupt type %d frame %d shadow %p\n",
  3986. type, frame, q.ptr);
  3987. fallthrough;
  3988. case Q_TYPE_QH:
  3989. case Q_TYPE_FSTN:
  3990. /* End of the iTDs and siTDs */
  3991. q.ptr = NULL;
  3992. break;
  3993. }
  3994. /* assume completion callbacks modify the queue */
  3995. if (unlikely(modified && fotg210->isoc_count > 0))
  3996. return -EINVAL;
  3997. }
  3998. return 0;
  3999. }
  4000. static void scan_isoc(struct fotg210_hcd *fotg210)
  4001. {
  4002. unsigned uf, now_frame, frame, ret;
  4003. unsigned fmask = fotg210->periodic_size - 1;
  4004. bool live;
  4005. /*
  4006. * When running, scan from last scan point up to "now"
  4007. * else clean up by scanning everything that's left.
  4008. * Touches as few pages as possible: cache-friendly.
  4009. */
  4010. if (fotg210->rh_state >= FOTG210_RH_RUNNING) {
  4011. uf = fotg210_read_frame_index(fotg210);
  4012. now_frame = (uf >> 3) & fmask;
  4013. live = true;
  4014. } else {
  4015. now_frame = (fotg210->next_frame - 1) & fmask;
  4016. live = false;
  4017. }
  4018. fotg210->now_frame = now_frame;
  4019. frame = fotg210->next_frame;
  4020. for (;;) {
  4021. ret = 1;
  4022. while (ret != 0)
  4023. ret = scan_frame_queue(fotg210, frame,
  4024. now_frame, live);
  4025. /* Stop when we have reached the current frame */
  4026. if (frame == now_frame)
  4027. break;
  4028. frame = (frame + 1) & fmask;
  4029. }
  4030. fotg210->next_frame = now_frame;
  4031. }
  4032. /* Display / Set uframe_periodic_max
  4033. */
  4034. static ssize_t uframe_periodic_max_show(struct device *dev,
  4035. struct device_attribute *attr, char *buf)
  4036. {
  4037. struct fotg210_hcd *fotg210;
  4038. int n;
  4039. fotg210 = hcd_to_fotg210(bus_to_hcd(dev_get_drvdata(dev)));
  4040. n = scnprintf(buf, PAGE_SIZE, "%d\n", fotg210->uframe_periodic_max);
  4041. return n;
  4042. }
  4043. static ssize_t uframe_periodic_max_store(struct device *dev,
  4044. struct device_attribute *attr, const char *buf, size_t count)
  4045. {
  4046. struct fotg210_hcd *fotg210;
  4047. unsigned uframe_periodic_max;
  4048. unsigned frame, uframe;
  4049. unsigned short allocated_max;
  4050. unsigned long flags;
  4051. ssize_t ret;
  4052. fotg210 = hcd_to_fotg210(bus_to_hcd(dev_get_drvdata(dev)));
  4053. if (kstrtouint(buf, 0, &uframe_periodic_max) < 0)
  4054. return -EINVAL;
  4055. if (uframe_periodic_max < 100 || uframe_periodic_max >= 125) {
  4056. fotg210_info(fotg210, "rejecting invalid request for uframe_periodic_max=%u\n",
  4057. uframe_periodic_max);
  4058. return -EINVAL;
  4059. }
  4060. ret = -EINVAL;
  4061. /*
  4062. * lock, so that our checking does not race with possible periodic
  4063. * bandwidth allocation through submitting new urbs.
  4064. */
  4065. spin_lock_irqsave(&fotg210->lock, flags);
  4066. /*
  4067. * for request to decrease max periodic bandwidth, we have to check
  4068. * every microframe in the schedule to see whether the decrease is
  4069. * possible.
  4070. */
  4071. if (uframe_periodic_max < fotg210->uframe_periodic_max) {
  4072. allocated_max = 0;
  4073. for (frame = 0; frame < fotg210->periodic_size; ++frame)
  4074. for (uframe = 0; uframe < 7; ++uframe)
  4075. allocated_max = max(allocated_max,
  4076. periodic_usecs(fotg210, frame,
  4077. uframe));
  4078. if (allocated_max > uframe_periodic_max) {
  4079. fotg210_info(fotg210,
  4080. "cannot decrease uframe_periodic_max because periodic bandwidth is already allocated (%u > %u)\n",
  4081. allocated_max, uframe_periodic_max);
  4082. goto out_unlock;
  4083. }
  4084. }
  4085. /* increasing is always ok */
  4086. fotg210_info(fotg210,
  4087. "setting max periodic bandwidth to %u%% (== %u usec/uframe)\n",
  4088. 100 * uframe_periodic_max/125, uframe_periodic_max);
  4089. if (uframe_periodic_max != 100)
  4090. fotg210_warn(fotg210, "max periodic bandwidth set is non-standard\n");
  4091. fotg210->uframe_periodic_max = uframe_periodic_max;
  4092. ret = count;
  4093. out_unlock:
  4094. spin_unlock_irqrestore(&fotg210->lock, flags);
  4095. return ret;
  4096. }
  4097. static DEVICE_ATTR_RW(uframe_periodic_max);
  4098. static inline int create_sysfs_files(struct fotg210_hcd *fotg210)
  4099. {
  4100. struct device *controller = fotg210_to_hcd(fotg210)->self.controller;
  4101. return device_create_file(controller, &dev_attr_uframe_periodic_max);
  4102. }
  4103. static inline void remove_sysfs_files(struct fotg210_hcd *fotg210)
  4104. {
  4105. struct device *controller = fotg210_to_hcd(fotg210)->self.controller;
  4106. device_remove_file(controller, &dev_attr_uframe_periodic_max);
  4107. }
  4108. /* On some systems, leaving remote wakeup enabled prevents system shutdown.
  4109. * The firmware seems to think that powering off is a wakeup event!
  4110. * This routine turns off remote wakeup and everything else, on all ports.
  4111. */
  4112. static void fotg210_turn_off_all_ports(struct fotg210_hcd *fotg210)
  4113. {
  4114. u32 __iomem *status_reg = &fotg210->regs->port_status;
  4115. fotg210_writel(fotg210, PORT_RWC_BITS, status_reg);
  4116. }
  4117. /* Halt HC, turn off all ports, and let the BIOS use the companion controllers.
  4118. * Must be called with interrupts enabled and the lock not held.
  4119. */
  4120. static void fotg210_silence_controller(struct fotg210_hcd *fotg210)
  4121. {
  4122. fotg210_halt(fotg210);
  4123. spin_lock_irq(&fotg210->lock);
  4124. fotg210->rh_state = FOTG210_RH_HALTED;
  4125. fotg210_turn_off_all_ports(fotg210);
  4126. spin_unlock_irq(&fotg210->lock);
  4127. }
  4128. /* fotg210_shutdown kick in for silicon on any bus (not just pci, etc).
  4129. * This forcibly disables dma and IRQs, helping kexec and other cases
  4130. * where the next system software may expect clean state.
  4131. */
  4132. static void fotg210_shutdown(struct usb_hcd *hcd)
  4133. {
  4134. struct fotg210_hcd *fotg210 = hcd_to_fotg210(hcd);
  4135. spin_lock_irq(&fotg210->lock);
  4136. fotg210->shutdown = true;
  4137. fotg210->rh_state = FOTG210_RH_STOPPING;
  4138. fotg210->enabled_hrtimer_events = 0;
  4139. spin_unlock_irq(&fotg210->lock);
  4140. fotg210_silence_controller(fotg210);
  4141. hrtimer_cancel(&fotg210->hrtimer);
  4142. }
  4143. /* fotg210_work is called from some interrupts, timers, and so on.
  4144. * it calls driver completion functions, after dropping fotg210->lock.
  4145. */
  4146. static void fotg210_work(struct fotg210_hcd *fotg210)
  4147. {
  4148. /* another CPU may drop fotg210->lock during a schedule scan while
  4149. * it reports urb completions. this flag guards against bogus
  4150. * attempts at re-entrant schedule scanning.
  4151. */
  4152. if (fotg210->scanning) {
  4153. fotg210->need_rescan = true;
  4154. return;
  4155. }
  4156. fotg210->scanning = true;
  4157. rescan:
  4158. fotg210->need_rescan = false;
  4159. if (fotg210->async_count)
  4160. scan_async(fotg210);
  4161. if (fotg210->intr_count > 0)
  4162. scan_intr(fotg210);
  4163. if (fotg210->isoc_count > 0)
  4164. scan_isoc(fotg210);
  4165. if (fotg210->need_rescan)
  4166. goto rescan;
  4167. fotg210->scanning = false;
  4168. /* the IO watchdog guards against hardware or driver bugs that
  4169. * misplace IRQs, and should let us run completely without IRQs.
  4170. * such lossage has been observed on both VT6202 and VT8235.
  4171. */
  4172. turn_on_io_watchdog(fotg210);
  4173. }
  4174. /* Called when the fotg210_hcd module is removed.
  4175. */
  4176. static void fotg210_stop(struct usb_hcd *hcd)
  4177. {
  4178. struct fotg210_hcd *fotg210 = hcd_to_fotg210(hcd);
  4179. fotg210_dbg(fotg210, "stop\n");
  4180. /* no more interrupts ... */
  4181. spin_lock_irq(&fotg210->lock);
  4182. fotg210->enabled_hrtimer_events = 0;
  4183. spin_unlock_irq(&fotg210->lock);
  4184. fotg210_quiesce(fotg210);
  4185. fotg210_silence_controller(fotg210);
  4186. fotg210_reset(fotg210);
  4187. hrtimer_cancel(&fotg210->hrtimer);
  4188. remove_sysfs_files(fotg210);
  4189. remove_debug_files(fotg210);
  4190. /* root hub is shut down separately (first, when possible) */
  4191. spin_lock_irq(&fotg210->lock);
  4192. end_free_itds(fotg210);
  4193. spin_unlock_irq(&fotg210->lock);
  4194. fotg210_mem_cleanup(fotg210);
  4195. #ifdef FOTG210_STATS
  4196. fotg210_dbg(fotg210, "irq normal %ld err %ld iaa %ld (lost %ld)\n",
  4197. fotg210->stats.normal, fotg210->stats.error,
  4198. fotg210->stats.iaa, fotg210->stats.lost_iaa);
  4199. fotg210_dbg(fotg210, "complete %ld unlink %ld\n",
  4200. fotg210->stats.complete, fotg210->stats.unlink);
  4201. #endif
  4202. dbg_status(fotg210, "fotg210_stop completed",
  4203. fotg210_readl(fotg210, &fotg210->regs->status));
  4204. }
  4205. /* one-time init, only for memory state */
  4206. static int hcd_fotg210_init(struct usb_hcd *hcd)
  4207. {
  4208. struct fotg210_hcd *fotg210 = hcd_to_fotg210(hcd);
  4209. u32 temp;
  4210. int retval;
  4211. u32 hcc_params;
  4212. struct fotg210_qh_hw *hw;
  4213. spin_lock_init(&fotg210->lock);
  4214. /*
  4215. * keep io watchdog by default, those good HCDs could turn off it later
  4216. */
  4217. fotg210->need_io_watchdog = 1;
  4218. hrtimer_init(&fotg210->hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
  4219. fotg210->hrtimer.function = fotg210_hrtimer_func;
  4220. fotg210->next_hrtimer_event = FOTG210_HRTIMER_NO_EVENT;
  4221. hcc_params = fotg210_readl(fotg210, &fotg210->caps->hcc_params);
  4222. /*
  4223. * by default set standard 80% (== 100 usec/uframe) max periodic
  4224. * bandwidth as required by USB 2.0
  4225. */
  4226. fotg210->uframe_periodic_max = 100;
  4227. /*
  4228. * hw default: 1K periodic list heads, one per frame.
  4229. * periodic_size can shrink by USBCMD update if hcc_params allows.
  4230. */
  4231. fotg210->periodic_size = DEFAULT_I_TDPS;
  4232. INIT_LIST_HEAD(&fotg210->intr_qh_list);
  4233. INIT_LIST_HEAD(&fotg210->cached_itd_list);
  4234. if (HCC_PGM_FRAMELISTLEN(hcc_params)) {
  4235. /* periodic schedule size can be smaller than default */
  4236. switch (FOTG210_TUNE_FLS) {
  4237. case 0:
  4238. fotg210->periodic_size = 1024;
  4239. break;
  4240. case 1:
  4241. fotg210->periodic_size = 512;
  4242. break;
  4243. case 2:
  4244. fotg210->periodic_size = 256;
  4245. break;
  4246. default:
  4247. BUG();
  4248. }
  4249. }
  4250. retval = fotg210_mem_init(fotg210, GFP_KERNEL);
  4251. if (retval < 0)
  4252. return retval;
  4253. /* controllers may cache some of the periodic schedule ... */
  4254. fotg210->i_thresh = 2;
  4255. /*
  4256. * dedicate a qh for the async ring head, since we couldn't unlink
  4257. * a 'real' qh without stopping the async schedule [4.8]. use it
  4258. * as the 'reclamation list head' too.
  4259. * its dummy is used in hw_alt_next of many tds, to prevent the qh
  4260. * from automatically advancing to the next td after short reads.
  4261. */
  4262. fotg210->async->qh_next.qh = NULL;
  4263. hw = fotg210->async->hw;
  4264. hw->hw_next = QH_NEXT(fotg210, fotg210->async->qh_dma);
  4265. hw->hw_info1 = cpu_to_hc32(fotg210, QH_HEAD);
  4266. hw->hw_token = cpu_to_hc32(fotg210, QTD_STS_HALT);
  4267. hw->hw_qtd_next = FOTG210_LIST_END(fotg210);
  4268. fotg210->async->qh_state = QH_STATE_LINKED;
  4269. hw->hw_alt_next = QTD_NEXT(fotg210, fotg210->async->dummy->qtd_dma);
  4270. /* clear interrupt enables, set irq latency */
  4271. if (log2_irq_thresh < 0 || log2_irq_thresh > 6)
  4272. log2_irq_thresh = 0;
  4273. temp = 1 << (16 + log2_irq_thresh);
  4274. if (HCC_CANPARK(hcc_params)) {
  4275. /* HW default park == 3, on hardware that supports it (like
  4276. * NVidia and ALI silicon), maximizes throughput on the async
  4277. * schedule by avoiding QH fetches between transfers.
  4278. *
  4279. * With fast usb storage devices and NForce2, "park" seems to
  4280. * make problems: throughput reduction (!), data errors...
  4281. */
  4282. if (park) {
  4283. park = min_t(unsigned, park, 3);
  4284. temp |= CMD_PARK;
  4285. temp |= park << 8;
  4286. }
  4287. fotg210_dbg(fotg210, "park %d\n", park);
  4288. }
  4289. if (HCC_PGM_FRAMELISTLEN(hcc_params)) {
  4290. /* periodic schedule size can be smaller than default */
  4291. temp &= ~(3 << 2);
  4292. temp |= (FOTG210_TUNE_FLS << 2);
  4293. }
  4294. fotg210->command = temp;
  4295. /* Accept arbitrarily long scatter-gather lists */
  4296. if (!hcd->localmem_pool)
  4297. hcd->self.sg_tablesize = ~0;
  4298. return 0;
  4299. }
  4300. /* start HC running; it's halted, hcd_fotg210_init() has been run (once) */
  4301. static int fotg210_run(struct usb_hcd *hcd)
  4302. {
  4303. struct fotg210_hcd *fotg210 = hcd_to_fotg210(hcd);
  4304. u32 temp;
  4305. hcd->uses_new_polling = 1;
  4306. /* EHCI spec section 4.1 */
  4307. fotg210_writel(fotg210, fotg210->periodic_dma,
  4308. &fotg210->regs->frame_list);
  4309. fotg210_writel(fotg210, (u32)fotg210->async->qh_dma,
  4310. &fotg210->regs->async_next);
  4311. /*
  4312. * hcc_params controls whether fotg210->regs->segment must (!!!)
  4313. * be used; it constrains QH/ITD/SITD and QTD locations.
  4314. * dma_pool consistent memory always uses segment zero.
  4315. * streaming mappings for I/O buffers, like dma_map_single(),
  4316. * can return segments above 4GB, if the device allows.
  4317. *
  4318. * NOTE: the dma mask is visible through dev->dma_mask, so
  4319. * drivers can pass this info along ... like NETIF_F_HIGHDMA,
  4320. * Scsi_Host.highmem_io, and so forth. It's readonly to all
  4321. * host side drivers though.
  4322. */
  4323. fotg210_readl(fotg210, &fotg210->caps->hcc_params);
  4324. /*
  4325. * Philips, Intel, and maybe others need CMD_RUN before the
  4326. * root hub will detect new devices (why?); NEC doesn't
  4327. */
  4328. fotg210->command &= ~(CMD_IAAD|CMD_PSE|CMD_ASE|CMD_RESET);
  4329. fotg210->command |= CMD_RUN;
  4330. fotg210_writel(fotg210, fotg210->command, &fotg210->regs->command);
  4331. dbg_cmd(fotg210, "init", fotg210->command);
  4332. /*
  4333. * Start, enabling full USB 2.0 functionality ... usb 1.1 devices
  4334. * are explicitly handed to companion controller(s), so no TT is
  4335. * involved with the root hub. (Except where one is integrated,
  4336. * and there's no companion controller unless maybe for USB OTG.)
  4337. *
  4338. * Turning on the CF flag will transfer ownership of all ports
  4339. * from the companions to the EHCI controller. If any of the
  4340. * companions are in the middle of a port reset at the time, it
  4341. * could cause trouble. Write-locking ehci_cf_port_reset_rwsem
  4342. * guarantees that no resets are in progress. After we set CF,
  4343. * a short delay lets the hardware catch up; new resets shouldn't
  4344. * be started before the port switching actions could complete.
  4345. */
  4346. down_write(&ehci_cf_port_reset_rwsem);
  4347. fotg210->rh_state = FOTG210_RH_RUNNING;
  4348. /* unblock posted writes */
  4349. fotg210_readl(fotg210, &fotg210->regs->command);
  4350. usleep_range(5000, 10000);
  4351. up_write(&ehci_cf_port_reset_rwsem);
  4352. fotg210->last_periodic_enable = ktime_get_real();
  4353. temp = HC_VERSION(fotg210,
  4354. fotg210_readl(fotg210, &fotg210->caps->hc_capbase));
  4355. fotg210_info(fotg210,
  4356. "USB %x.%x started, EHCI %x.%02x\n",
  4357. ((fotg210->sbrn & 0xf0) >> 4), (fotg210->sbrn & 0x0f),
  4358. temp >> 8, temp & 0xff);
  4359. fotg210_writel(fotg210, INTR_MASK,
  4360. &fotg210->regs->intr_enable); /* Turn On Interrupts */
  4361. /* GRR this is run-once init(), being done every time the HC starts.
  4362. * So long as they're part of class devices, we can't do it init()
  4363. * since the class device isn't created that early.
  4364. */
  4365. create_debug_files(fotg210);
  4366. create_sysfs_files(fotg210);
  4367. return 0;
  4368. }
  4369. static int fotg210_setup(struct usb_hcd *hcd)
  4370. {
  4371. struct fotg210_hcd *fotg210 = hcd_to_fotg210(hcd);
  4372. int retval;
  4373. fotg210->regs = (void __iomem *)fotg210->caps +
  4374. HC_LENGTH(fotg210,
  4375. fotg210_readl(fotg210, &fotg210->caps->hc_capbase));
  4376. dbg_hcs_params(fotg210, "reset");
  4377. dbg_hcc_params(fotg210, "reset");
  4378. /* cache this readonly data; minimize chip reads */
  4379. fotg210->hcs_params = fotg210_readl(fotg210,
  4380. &fotg210->caps->hcs_params);
  4381. fotg210->sbrn = HCD_USB2;
  4382. /* data structure init */
  4383. retval = hcd_fotg210_init(hcd);
  4384. if (retval)
  4385. return retval;
  4386. retval = fotg210_halt(fotg210);
  4387. if (retval)
  4388. return retval;
  4389. fotg210_reset(fotg210);
  4390. return 0;
  4391. }
  4392. static irqreturn_t fotg210_irq(struct usb_hcd *hcd)
  4393. {
  4394. struct fotg210_hcd *fotg210 = hcd_to_fotg210(hcd);
  4395. u32 status, masked_status, pcd_status = 0, cmd;
  4396. int bh;
  4397. spin_lock(&fotg210->lock);
  4398. status = fotg210_readl(fotg210, &fotg210->regs->status);
  4399. /* e.g. cardbus physical eject */
  4400. if (status == ~(u32) 0) {
  4401. fotg210_dbg(fotg210, "device removed\n");
  4402. goto dead;
  4403. }
  4404. /*
  4405. * We don't use STS_FLR, but some controllers don't like it to
  4406. * remain on, so mask it out along with the other status bits.
  4407. */
  4408. masked_status = status & (INTR_MASK | STS_FLR);
  4409. /* Shared IRQ? */
  4410. if (!masked_status ||
  4411. unlikely(fotg210->rh_state == FOTG210_RH_HALTED)) {
  4412. spin_unlock(&fotg210->lock);
  4413. return IRQ_NONE;
  4414. }
  4415. /* clear (just) interrupts */
  4416. fotg210_writel(fotg210, masked_status, &fotg210->regs->status);
  4417. cmd = fotg210_readl(fotg210, &fotg210->regs->command);
  4418. bh = 0;
  4419. /* unrequested/ignored: Frame List Rollover */
  4420. dbg_status(fotg210, "irq", status);
  4421. /* INT, ERR, and IAA interrupt rates can be throttled */
  4422. /* normal [4.15.1.2] or error [4.15.1.1] completion */
  4423. if (likely((status & (STS_INT|STS_ERR)) != 0)) {
  4424. if (likely((status & STS_ERR) == 0))
  4425. INCR(fotg210->stats.normal);
  4426. else
  4427. INCR(fotg210->stats.error);
  4428. bh = 1;
  4429. }
  4430. /* complete the unlinking of some qh [4.15.2.3] */
  4431. if (status & STS_IAA) {
  4432. /* Turn off the IAA watchdog */
  4433. fotg210->enabled_hrtimer_events &=
  4434. ~BIT(FOTG210_HRTIMER_IAA_WATCHDOG);
  4435. /*
  4436. * Mild optimization: Allow another IAAD to reset the
  4437. * hrtimer, if one occurs before the next expiration.
  4438. * In theory we could always cancel the hrtimer, but
  4439. * tests show that about half the time it will be reset
  4440. * for some other event anyway.
  4441. */
  4442. if (fotg210->next_hrtimer_event == FOTG210_HRTIMER_IAA_WATCHDOG)
  4443. ++fotg210->next_hrtimer_event;
  4444. /* guard against (alleged) silicon errata */
  4445. if (cmd & CMD_IAAD)
  4446. fotg210_dbg(fotg210, "IAA with IAAD still set?\n");
  4447. if (fotg210->async_iaa) {
  4448. INCR(fotg210->stats.iaa);
  4449. end_unlink_async(fotg210);
  4450. } else
  4451. fotg210_dbg(fotg210, "IAA with nothing unlinked?\n");
  4452. }
  4453. /* remote wakeup [4.3.1] */
  4454. if (status & STS_PCD) {
  4455. int pstatus;
  4456. u32 __iomem *status_reg = &fotg210->regs->port_status;
  4457. /* kick root hub later */
  4458. pcd_status = status;
  4459. /* resume root hub? */
  4460. if (fotg210->rh_state == FOTG210_RH_SUSPENDED)
  4461. usb_hcd_resume_root_hub(hcd);
  4462. pstatus = fotg210_readl(fotg210, status_reg);
  4463. if (test_bit(0, &fotg210->suspended_ports) &&
  4464. ((pstatus & PORT_RESUME) ||
  4465. !(pstatus & PORT_SUSPEND)) &&
  4466. (pstatus & PORT_PE) &&
  4467. fotg210->reset_done[0] == 0) {
  4468. /* start 20 msec resume signaling from this port,
  4469. * and make hub_wq collect PORT_STAT_C_SUSPEND to
  4470. * stop that signaling. Use 5 ms extra for safety,
  4471. * like usb_port_resume() does.
  4472. */
  4473. fotg210->reset_done[0] = jiffies + msecs_to_jiffies(25);
  4474. set_bit(0, &fotg210->resuming_ports);
  4475. fotg210_dbg(fotg210, "port 1 remote wakeup\n");
  4476. mod_timer(&hcd->rh_timer, fotg210->reset_done[0]);
  4477. }
  4478. }
  4479. /* PCI errors [4.15.2.4] */
  4480. if (unlikely((status & STS_FATAL) != 0)) {
  4481. fotg210_err(fotg210, "fatal error\n");
  4482. dbg_cmd(fotg210, "fatal", cmd);
  4483. dbg_status(fotg210, "fatal", status);
  4484. dead:
  4485. usb_hc_died(hcd);
  4486. /* Don't let the controller do anything more */
  4487. fotg210->shutdown = true;
  4488. fotg210->rh_state = FOTG210_RH_STOPPING;
  4489. fotg210->command &= ~(CMD_RUN | CMD_ASE | CMD_PSE);
  4490. fotg210_writel(fotg210, fotg210->command,
  4491. &fotg210->regs->command);
  4492. fotg210_writel(fotg210, 0, &fotg210->regs->intr_enable);
  4493. fotg210_handle_controller_death(fotg210);
  4494. /* Handle completions when the controller stops */
  4495. bh = 0;
  4496. }
  4497. if (bh)
  4498. fotg210_work(fotg210);
  4499. spin_unlock(&fotg210->lock);
  4500. if (pcd_status)
  4501. usb_hcd_poll_rh_status(hcd);
  4502. return IRQ_HANDLED;
  4503. }
  4504. /* non-error returns are a promise to giveback() the urb later
  4505. * we drop ownership so next owner (or urb unlink) can get it
  4506. *
  4507. * urb + dev is in hcd.self.controller.urb_list
  4508. * we're queueing TDs onto software and hardware lists
  4509. *
  4510. * hcd-specific init for hcpriv hasn't been done yet
  4511. *
  4512. * NOTE: control, bulk, and interrupt share the same code to append TDs
  4513. * to a (possibly active) QH, and the same QH scanning code.
  4514. */
  4515. static int fotg210_urb_enqueue(struct usb_hcd *hcd, struct urb *urb,
  4516. gfp_t mem_flags)
  4517. {
  4518. struct fotg210_hcd *fotg210 = hcd_to_fotg210(hcd);
  4519. struct list_head qtd_list;
  4520. INIT_LIST_HEAD(&qtd_list);
  4521. switch (usb_pipetype(urb->pipe)) {
  4522. case PIPE_CONTROL:
  4523. /* qh_completions() code doesn't handle all the fault cases
  4524. * in multi-TD control transfers. Even 1KB is rare anyway.
  4525. */
  4526. if (urb->transfer_buffer_length > (16 * 1024))
  4527. return -EMSGSIZE;
  4528. fallthrough;
  4529. /* case PIPE_BULK: */
  4530. default:
  4531. if (!qh_urb_transaction(fotg210, urb, &qtd_list, mem_flags))
  4532. return -ENOMEM;
  4533. return submit_async(fotg210, urb, &qtd_list, mem_flags);
  4534. case PIPE_INTERRUPT:
  4535. if (!qh_urb_transaction(fotg210, urb, &qtd_list, mem_flags))
  4536. return -ENOMEM;
  4537. return intr_submit(fotg210, urb, &qtd_list, mem_flags);
  4538. case PIPE_ISOCHRONOUS:
  4539. return itd_submit(fotg210, urb, mem_flags);
  4540. }
  4541. }
  4542. /* remove from hardware lists
  4543. * completions normally happen asynchronously
  4544. */
  4545. static int fotg210_urb_dequeue(struct usb_hcd *hcd, struct urb *urb, int status)
  4546. {
  4547. struct fotg210_hcd *fotg210 = hcd_to_fotg210(hcd);
  4548. struct fotg210_qh *qh;
  4549. unsigned long flags;
  4550. int rc;
  4551. spin_lock_irqsave(&fotg210->lock, flags);
  4552. rc = usb_hcd_check_unlink_urb(hcd, urb, status);
  4553. if (rc)
  4554. goto done;
  4555. switch (usb_pipetype(urb->pipe)) {
  4556. /* case PIPE_CONTROL: */
  4557. /* case PIPE_BULK:*/
  4558. default:
  4559. qh = (struct fotg210_qh *) urb->hcpriv;
  4560. if (!qh)
  4561. break;
  4562. switch (qh->qh_state) {
  4563. case QH_STATE_LINKED:
  4564. case QH_STATE_COMPLETING:
  4565. start_unlink_async(fotg210, qh);
  4566. break;
  4567. case QH_STATE_UNLINK:
  4568. case QH_STATE_UNLINK_WAIT:
  4569. /* already started */
  4570. break;
  4571. case QH_STATE_IDLE:
  4572. /* QH might be waiting for a Clear-TT-Buffer */
  4573. qh_completions(fotg210, qh);
  4574. break;
  4575. }
  4576. break;
  4577. case PIPE_INTERRUPT:
  4578. qh = (struct fotg210_qh *) urb->hcpriv;
  4579. if (!qh)
  4580. break;
  4581. switch (qh->qh_state) {
  4582. case QH_STATE_LINKED:
  4583. case QH_STATE_COMPLETING:
  4584. start_unlink_intr(fotg210, qh);
  4585. break;
  4586. case QH_STATE_IDLE:
  4587. qh_completions(fotg210, qh);
  4588. break;
  4589. default:
  4590. fotg210_dbg(fotg210, "bogus qh %p state %d\n",
  4591. qh, qh->qh_state);
  4592. goto done;
  4593. }
  4594. break;
  4595. case PIPE_ISOCHRONOUS:
  4596. /* itd... */
  4597. /* wait till next completion, do it then. */
  4598. /* completion irqs can wait up to 1024 msec, */
  4599. break;
  4600. }
  4601. done:
  4602. spin_unlock_irqrestore(&fotg210->lock, flags);
  4603. return rc;
  4604. }
  4605. /* bulk qh holds the data toggle */
  4606. static void fotg210_endpoint_disable(struct usb_hcd *hcd,
  4607. struct usb_host_endpoint *ep)
  4608. {
  4609. struct fotg210_hcd *fotg210 = hcd_to_fotg210(hcd);
  4610. unsigned long flags;
  4611. struct fotg210_qh *qh, *tmp;
  4612. /* ASSERT: any requests/urbs are being unlinked */
  4613. /* ASSERT: nobody can be submitting urbs for this any more */
  4614. rescan:
  4615. spin_lock_irqsave(&fotg210->lock, flags);
  4616. qh = ep->hcpriv;
  4617. if (!qh)
  4618. goto done;
  4619. /* endpoints can be iso streams. for now, we don't
  4620. * accelerate iso completions ... so spin a while.
  4621. */
  4622. if (qh->hw == NULL) {
  4623. struct fotg210_iso_stream *stream = ep->hcpriv;
  4624. if (!list_empty(&stream->td_list))
  4625. goto idle_timeout;
  4626. /* BUG_ON(!list_empty(&stream->free_list)); */
  4627. kfree(stream);
  4628. goto done;
  4629. }
  4630. if (fotg210->rh_state < FOTG210_RH_RUNNING)
  4631. qh->qh_state = QH_STATE_IDLE;
  4632. switch (qh->qh_state) {
  4633. case QH_STATE_LINKED:
  4634. case QH_STATE_COMPLETING:
  4635. for (tmp = fotg210->async->qh_next.qh;
  4636. tmp && tmp != qh;
  4637. tmp = tmp->qh_next.qh)
  4638. continue;
  4639. /* periodic qh self-unlinks on empty, and a COMPLETING qh
  4640. * may already be unlinked.
  4641. */
  4642. if (tmp)
  4643. start_unlink_async(fotg210, qh);
  4644. fallthrough;
  4645. case QH_STATE_UNLINK: /* wait for hw to finish? */
  4646. case QH_STATE_UNLINK_WAIT:
  4647. idle_timeout:
  4648. spin_unlock_irqrestore(&fotg210->lock, flags);
  4649. schedule_timeout_uninterruptible(1);
  4650. goto rescan;
  4651. case QH_STATE_IDLE: /* fully unlinked */
  4652. if (qh->clearing_tt)
  4653. goto idle_timeout;
  4654. if (list_empty(&qh->qtd_list)) {
  4655. qh_destroy(fotg210, qh);
  4656. break;
  4657. }
  4658. fallthrough;
  4659. default:
  4660. /* caller was supposed to have unlinked any requests;
  4661. * that's not our job. just leak this memory.
  4662. */
  4663. fotg210_err(fotg210, "qh %p (#%02x) state %d%s\n",
  4664. qh, ep->desc.bEndpointAddress, qh->qh_state,
  4665. list_empty(&qh->qtd_list) ? "" : "(has tds)");
  4666. break;
  4667. }
  4668. done:
  4669. ep->hcpriv = NULL;
  4670. spin_unlock_irqrestore(&fotg210->lock, flags);
  4671. }
  4672. static void fotg210_endpoint_reset(struct usb_hcd *hcd,
  4673. struct usb_host_endpoint *ep)
  4674. {
  4675. struct fotg210_hcd *fotg210 = hcd_to_fotg210(hcd);
  4676. struct fotg210_qh *qh;
  4677. int eptype = usb_endpoint_type(&ep->desc);
  4678. int epnum = usb_endpoint_num(&ep->desc);
  4679. int is_out = usb_endpoint_dir_out(&ep->desc);
  4680. unsigned long flags;
  4681. if (eptype != USB_ENDPOINT_XFER_BULK && eptype != USB_ENDPOINT_XFER_INT)
  4682. return;
  4683. spin_lock_irqsave(&fotg210->lock, flags);
  4684. qh = ep->hcpriv;
  4685. /* For Bulk and Interrupt endpoints we maintain the toggle state
  4686. * in the hardware; the toggle bits in udev aren't used at all.
  4687. * When an endpoint is reset by usb_clear_halt() we must reset
  4688. * the toggle bit in the QH.
  4689. */
  4690. if (qh) {
  4691. usb_settoggle(qh->dev, epnum, is_out, 0);
  4692. if (!list_empty(&qh->qtd_list)) {
  4693. WARN_ONCE(1, "clear_halt for a busy endpoint\n");
  4694. } else if (qh->qh_state == QH_STATE_LINKED ||
  4695. qh->qh_state == QH_STATE_COMPLETING) {
  4696. /* The toggle value in the QH can't be updated
  4697. * while the QH is active. Unlink it now;
  4698. * re-linking will call qh_refresh().
  4699. */
  4700. if (eptype == USB_ENDPOINT_XFER_BULK)
  4701. start_unlink_async(fotg210, qh);
  4702. else
  4703. start_unlink_intr(fotg210, qh);
  4704. }
  4705. }
  4706. spin_unlock_irqrestore(&fotg210->lock, flags);
  4707. }
  4708. static int fotg210_get_frame(struct usb_hcd *hcd)
  4709. {
  4710. struct fotg210_hcd *fotg210 = hcd_to_fotg210(hcd);
  4711. return (fotg210_read_frame_index(fotg210) >> 3) %
  4712. fotg210->periodic_size;
  4713. }
  4714. /* The EHCI in ChipIdea HDRC cannot be a separate module or device,
  4715. * because its registers (and irq) are shared between host/gadget/otg
  4716. * functions and in order to facilitate role switching we cannot
  4717. * give the fotg210 driver exclusive access to those.
  4718. */
  4719. MODULE_DESCRIPTION(DRIVER_DESC);
  4720. MODULE_AUTHOR(DRIVER_AUTHOR);
  4721. MODULE_LICENSE("GPL");
  4722. static const struct hc_driver fotg210_fotg210_hc_driver = {
  4723. .description = hcd_name,
  4724. .product_desc = "Faraday USB2.0 Host Controller",
  4725. .hcd_priv_size = sizeof(struct fotg210_hcd),
  4726. /*
  4727. * generic hardware linkage
  4728. */
  4729. .irq = fotg210_irq,
  4730. .flags = HCD_MEMORY | HCD_DMA | HCD_USB2,
  4731. /*
  4732. * basic lifecycle operations
  4733. */
  4734. .reset = hcd_fotg210_init,
  4735. .start = fotg210_run,
  4736. .stop = fotg210_stop,
  4737. .shutdown = fotg210_shutdown,
  4738. /*
  4739. * managing i/o requests and associated device resources
  4740. */
  4741. .urb_enqueue = fotg210_urb_enqueue,
  4742. .urb_dequeue = fotg210_urb_dequeue,
  4743. .endpoint_disable = fotg210_endpoint_disable,
  4744. .endpoint_reset = fotg210_endpoint_reset,
  4745. /*
  4746. * scheduling support
  4747. */
  4748. .get_frame_number = fotg210_get_frame,
  4749. /*
  4750. * root hub support
  4751. */
  4752. .hub_status_data = fotg210_hub_status_data,
  4753. .hub_control = fotg210_hub_control,
  4754. .bus_suspend = fotg210_bus_suspend,
  4755. .bus_resume = fotg210_bus_resume,
  4756. .relinquish_port = fotg210_relinquish_port,
  4757. .port_handed_over = fotg210_port_handed_over,
  4758. .clear_tt_buffer_complete = fotg210_clear_tt_buffer_complete,
  4759. };
  4760. static void fotg210_init(struct fotg210_hcd *fotg210)
  4761. {
  4762. u32 value;
  4763. iowrite32(GMIR_MDEV_INT | GMIR_MOTG_INT | GMIR_INT_POLARITY,
  4764. &fotg210->regs->gmir);
  4765. value = ioread32(&fotg210->regs->otgcsr);
  4766. value &= ~OTGCSR_A_BUS_DROP;
  4767. value |= OTGCSR_A_BUS_REQ;
  4768. iowrite32(value, &fotg210->regs->otgcsr);
  4769. }
  4770. /*
  4771. * fotg210_hcd_probe - initialize faraday FOTG210 HCDs
  4772. *
  4773. * Allocates basic resources for this USB host controller, and
  4774. * then invokes the start() method for the HCD associated with it
  4775. * through the hotplug entry's driver_data.
  4776. */
  4777. static int fotg210_hcd_probe(struct platform_device *pdev)
  4778. {
  4779. struct device *dev = &pdev->dev;
  4780. struct usb_hcd *hcd;
  4781. struct resource *res;
  4782. int irq;
  4783. int retval;
  4784. struct fotg210_hcd *fotg210;
  4785. if (usb_disabled())
  4786. return -ENODEV;
  4787. pdev->dev.power.power_state = PMSG_ON;
  4788. irq = platform_get_irq(pdev, 0);
  4789. if (irq < 0)
  4790. return irq;
  4791. hcd = usb_create_hcd(&fotg210_fotg210_hc_driver, dev,
  4792. dev_name(dev));
  4793. if (!hcd) {
  4794. dev_err(dev, "failed to create hcd\n");
  4795. retval = -ENOMEM;
  4796. goto fail_create_hcd;
  4797. }
  4798. hcd->has_tt = 1;
  4799. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  4800. hcd->regs = devm_ioremap_resource(&pdev->dev, res);
  4801. if (IS_ERR(hcd->regs)) {
  4802. retval = PTR_ERR(hcd->regs);
  4803. goto failed_put_hcd;
  4804. }
  4805. hcd->rsrc_start = res->start;
  4806. hcd->rsrc_len = resource_size(res);
  4807. fotg210 = hcd_to_fotg210(hcd);
  4808. fotg210->caps = hcd->regs;
  4809. /* It's OK not to supply this clock */
  4810. fotg210->pclk = clk_get(dev, "PCLK");
  4811. if (!IS_ERR(fotg210->pclk)) {
  4812. retval = clk_prepare_enable(fotg210->pclk);
  4813. if (retval) {
  4814. dev_err(dev, "failed to enable PCLK\n");
  4815. goto failed_put_hcd;
  4816. }
  4817. } else if (PTR_ERR(fotg210->pclk) == -EPROBE_DEFER) {
  4818. /*
  4819. * Percolate deferrals, for anything else,
  4820. * just live without the clocking.
  4821. */
  4822. retval = PTR_ERR(fotg210->pclk);
  4823. goto failed_dis_clk;
  4824. }
  4825. retval = fotg210_setup(hcd);
  4826. if (retval)
  4827. goto failed_dis_clk;
  4828. fotg210_init(fotg210);
  4829. retval = usb_add_hcd(hcd, irq, IRQF_SHARED);
  4830. if (retval) {
  4831. dev_err(dev, "failed to add hcd with err %d\n", retval);
  4832. goto failed_dis_clk;
  4833. }
  4834. device_wakeup_enable(hcd->self.controller);
  4835. platform_set_drvdata(pdev, hcd);
  4836. return retval;
  4837. failed_dis_clk:
  4838. if (!IS_ERR(fotg210->pclk)) {
  4839. clk_disable_unprepare(fotg210->pclk);
  4840. clk_put(fotg210->pclk);
  4841. }
  4842. failed_put_hcd:
  4843. usb_put_hcd(hcd);
  4844. fail_create_hcd:
  4845. dev_err(dev, "init %s fail, %d\n", dev_name(dev), retval);
  4846. return retval;
  4847. }
  4848. /*
  4849. * fotg210_hcd_remove - shutdown processing for EHCI HCDs
  4850. * @dev: USB Host Controller being removed
  4851. *
  4852. */
  4853. static int fotg210_hcd_remove(struct platform_device *pdev)
  4854. {
  4855. struct usb_hcd *hcd = platform_get_drvdata(pdev);
  4856. struct fotg210_hcd *fotg210 = hcd_to_fotg210(hcd);
  4857. if (!IS_ERR(fotg210->pclk)) {
  4858. clk_disable_unprepare(fotg210->pclk);
  4859. clk_put(fotg210->pclk);
  4860. }
  4861. usb_remove_hcd(hcd);
  4862. usb_put_hcd(hcd);
  4863. return 0;
  4864. }
  4865. #ifdef CONFIG_OF
  4866. static const struct of_device_id fotg210_of_match[] = {
  4867. { .compatible = "faraday,fotg210" },
  4868. {},
  4869. };
  4870. MODULE_DEVICE_TABLE(of, fotg210_of_match);
  4871. #endif
  4872. static struct platform_driver fotg210_hcd_driver = {
  4873. .driver = {
  4874. .name = "fotg210-hcd",
  4875. .of_match_table = of_match_ptr(fotg210_of_match),
  4876. },
  4877. .probe = fotg210_hcd_probe,
  4878. .remove = fotg210_hcd_remove,
  4879. };
  4880. static int __init fotg210_hcd_init(void)
  4881. {
  4882. int retval = 0;
  4883. if (usb_disabled())
  4884. return -ENODEV;
  4885. set_bit(USB_EHCI_LOADED, &usb_hcds_loaded);
  4886. if (test_bit(USB_UHCI_LOADED, &usb_hcds_loaded) ||
  4887. test_bit(USB_OHCI_LOADED, &usb_hcds_loaded))
  4888. pr_warn("Warning! fotg210_hcd should always be loaded before uhci_hcd and ohci_hcd, not after\n");
  4889. pr_debug("%s: block sizes: qh %zd qtd %zd itd %zd\n",
  4890. hcd_name, sizeof(struct fotg210_qh),
  4891. sizeof(struct fotg210_qtd),
  4892. sizeof(struct fotg210_itd));
  4893. fotg210_debug_root = debugfs_create_dir("fotg210", usb_debug_root);
  4894. retval = platform_driver_register(&fotg210_hcd_driver);
  4895. if (retval < 0)
  4896. goto clean;
  4897. return retval;
  4898. clean:
  4899. debugfs_remove(fotg210_debug_root);
  4900. fotg210_debug_root = NULL;
  4901. clear_bit(USB_EHCI_LOADED, &usb_hcds_loaded);
  4902. return retval;
  4903. }
  4904. module_init(fotg210_hcd_init);
  4905. static void __exit fotg210_hcd_cleanup(void)
  4906. {
  4907. platform_driver_unregister(&fotg210_hcd_driver);
  4908. debugfs_remove(fotg210_debug_root);
  4909. clear_bit(USB_EHCI_LOADED, &usb_hcds_loaded);
  4910. }
  4911. module_exit(fotg210_hcd_cleanup);